rocker_main.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  1. /*
  2. * drivers/net/ethernet/rocker/rocker.c - Rocker switch device driver
  3. * Copyright (c) 2014-2016 Jiri Pirko <jiri@mellanox.com>
  4. * Copyright (c) 2014 Scott Feldman <sfeldma@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/pci.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/sched.h>
  16. #include <linux/wait.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/sort.h>
  19. #include <linux/random.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/socket.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/if_bridge.h>
  28. #include <linux/bitops.h>
  29. #include <linux/ctype.h>
  30. #include <linux/workqueue.h>
  31. #include <net/switchdev.h>
  32. #include <net/rtnetlink.h>
  33. #include <net/netevent.h>
  34. #include <net/arp.h>
  35. #include <net/fib_rules.h>
  36. #include <net/fib_notifier.h>
  37. #include <linux/io-64-nonatomic-lo-hi.h>
  38. #include <generated/utsrelease.h>
  39. #include "rocker_hw.h"
  40. #include "rocker.h"
  41. #include "rocker_tlv.h"
  42. static const char rocker_driver_name[] = "rocker";
  43. static const struct pci_device_id rocker_pci_id_table[] = {
  44. {PCI_VDEVICE(REDHAT, PCI_DEVICE_ID_REDHAT_ROCKER), 0},
  45. {0, }
  46. };
  47. struct rocker_wait {
  48. wait_queue_head_t wait;
  49. bool done;
  50. bool nowait;
  51. };
  52. static void rocker_wait_reset(struct rocker_wait *wait)
  53. {
  54. wait->done = false;
  55. wait->nowait = false;
  56. }
  57. static void rocker_wait_init(struct rocker_wait *wait)
  58. {
  59. init_waitqueue_head(&wait->wait);
  60. rocker_wait_reset(wait);
  61. }
  62. static struct rocker_wait *rocker_wait_create(void)
  63. {
  64. struct rocker_wait *wait;
  65. wait = kzalloc(sizeof(*wait), GFP_KERNEL);
  66. if (!wait)
  67. return NULL;
  68. return wait;
  69. }
  70. static void rocker_wait_destroy(struct rocker_wait *wait)
  71. {
  72. kfree(wait);
  73. }
  74. static bool rocker_wait_event_timeout(struct rocker_wait *wait,
  75. unsigned long timeout)
  76. {
  77. wait_event_timeout(wait->wait, wait->done, HZ / 10);
  78. if (!wait->done)
  79. return false;
  80. return true;
  81. }
  82. static void rocker_wait_wake_up(struct rocker_wait *wait)
  83. {
  84. wait->done = true;
  85. wake_up(&wait->wait);
  86. }
  87. static u32 rocker_msix_vector(const struct rocker *rocker, unsigned int vector)
  88. {
  89. return rocker->msix_entries[vector].vector;
  90. }
  91. static u32 rocker_msix_tx_vector(const struct rocker_port *rocker_port)
  92. {
  93. return rocker_msix_vector(rocker_port->rocker,
  94. ROCKER_MSIX_VEC_TX(rocker_port->port_number));
  95. }
  96. static u32 rocker_msix_rx_vector(const struct rocker_port *rocker_port)
  97. {
  98. return rocker_msix_vector(rocker_port->rocker,
  99. ROCKER_MSIX_VEC_RX(rocker_port->port_number));
  100. }
  101. #define rocker_write32(rocker, reg, val) \
  102. writel((val), (rocker)->hw_addr + (ROCKER_ ## reg))
  103. #define rocker_read32(rocker, reg) \
  104. readl((rocker)->hw_addr + (ROCKER_ ## reg))
  105. #define rocker_write64(rocker, reg, val) \
  106. writeq((val), (rocker)->hw_addr + (ROCKER_ ## reg))
  107. #define rocker_read64(rocker, reg) \
  108. readq((rocker)->hw_addr + (ROCKER_ ## reg))
  109. /*****************************
  110. * HW basic testing functions
  111. *****************************/
  112. static int rocker_reg_test(const struct rocker *rocker)
  113. {
  114. const struct pci_dev *pdev = rocker->pdev;
  115. u64 test_reg;
  116. u64 rnd;
  117. rnd = prandom_u32();
  118. rnd >>= 1;
  119. rocker_write32(rocker, TEST_REG, rnd);
  120. test_reg = rocker_read32(rocker, TEST_REG);
  121. if (test_reg != rnd * 2) {
  122. dev_err(&pdev->dev, "unexpected 32bit register value %08llx, expected %08llx\n",
  123. test_reg, rnd * 2);
  124. return -EIO;
  125. }
  126. rnd = prandom_u32();
  127. rnd <<= 31;
  128. rnd |= prandom_u32();
  129. rocker_write64(rocker, TEST_REG64, rnd);
  130. test_reg = rocker_read64(rocker, TEST_REG64);
  131. if (test_reg != rnd * 2) {
  132. dev_err(&pdev->dev, "unexpected 64bit register value %16llx, expected %16llx\n",
  133. test_reg, rnd * 2);
  134. return -EIO;
  135. }
  136. return 0;
  137. }
  138. static int rocker_dma_test_one(const struct rocker *rocker,
  139. struct rocker_wait *wait, u32 test_type,
  140. dma_addr_t dma_handle, const unsigned char *buf,
  141. const unsigned char *expect, size_t size)
  142. {
  143. const struct pci_dev *pdev = rocker->pdev;
  144. int i;
  145. rocker_wait_reset(wait);
  146. rocker_write32(rocker, TEST_DMA_CTRL, test_type);
  147. if (!rocker_wait_event_timeout(wait, HZ / 10)) {
  148. dev_err(&pdev->dev, "no interrupt received within a timeout\n");
  149. return -EIO;
  150. }
  151. for (i = 0; i < size; i++) {
  152. if (buf[i] != expect[i]) {
  153. dev_err(&pdev->dev, "unexpected memory content %02x at byte %x\n, %02x expected",
  154. buf[i], i, expect[i]);
  155. return -EIO;
  156. }
  157. }
  158. return 0;
  159. }
  160. #define ROCKER_TEST_DMA_BUF_SIZE (PAGE_SIZE * 4)
  161. #define ROCKER_TEST_DMA_FILL_PATTERN 0x96
  162. static int rocker_dma_test_offset(const struct rocker *rocker,
  163. struct rocker_wait *wait, int offset)
  164. {
  165. struct pci_dev *pdev = rocker->pdev;
  166. unsigned char *alloc;
  167. unsigned char *buf;
  168. unsigned char *expect;
  169. dma_addr_t dma_handle;
  170. int i;
  171. int err;
  172. alloc = kzalloc(ROCKER_TEST_DMA_BUF_SIZE * 2 + offset,
  173. GFP_KERNEL | GFP_DMA);
  174. if (!alloc)
  175. return -ENOMEM;
  176. buf = alloc + offset;
  177. expect = buf + ROCKER_TEST_DMA_BUF_SIZE;
  178. dma_handle = pci_map_single(pdev, buf, ROCKER_TEST_DMA_BUF_SIZE,
  179. PCI_DMA_BIDIRECTIONAL);
  180. if (pci_dma_mapping_error(pdev, dma_handle)) {
  181. err = -EIO;
  182. goto free_alloc;
  183. }
  184. rocker_write64(rocker, TEST_DMA_ADDR, dma_handle);
  185. rocker_write32(rocker, TEST_DMA_SIZE, ROCKER_TEST_DMA_BUF_SIZE);
  186. memset(expect, ROCKER_TEST_DMA_FILL_PATTERN, ROCKER_TEST_DMA_BUF_SIZE);
  187. err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_FILL,
  188. dma_handle, buf, expect,
  189. ROCKER_TEST_DMA_BUF_SIZE);
  190. if (err)
  191. goto unmap;
  192. memset(expect, 0, ROCKER_TEST_DMA_BUF_SIZE);
  193. err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_CLEAR,
  194. dma_handle, buf, expect,
  195. ROCKER_TEST_DMA_BUF_SIZE);
  196. if (err)
  197. goto unmap;
  198. prandom_bytes(buf, ROCKER_TEST_DMA_BUF_SIZE);
  199. for (i = 0; i < ROCKER_TEST_DMA_BUF_SIZE; i++)
  200. expect[i] = ~buf[i];
  201. err = rocker_dma_test_one(rocker, wait, ROCKER_TEST_DMA_CTRL_INVERT,
  202. dma_handle, buf, expect,
  203. ROCKER_TEST_DMA_BUF_SIZE);
  204. if (err)
  205. goto unmap;
  206. unmap:
  207. pci_unmap_single(pdev, dma_handle, ROCKER_TEST_DMA_BUF_SIZE,
  208. PCI_DMA_BIDIRECTIONAL);
  209. free_alloc:
  210. kfree(alloc);
  211. return err;
  212. }
  213. static int rocker_dma_test(const struct rocker *rocker,
  214. struct rocker_wait *wait)
  215. {
  216. int i;
  217. int err;
  218. for (i = 0; i < 8; i++) {
  219. err = rocker_dma_test_offset(rocker, wait, i);
  220. if (err)
  221. return err;
  222. }
  223. return 0;
  224. }
  225. static irqreturn_t rocker_test_irq_handler(int irq, void *dev_id)
  226. {
  227. struct rocker_wait *wait = dev_id;
  228. rocker_wait_wake_up(wait);
  229. return IRQ_HANDLED;
  230. }
  231. static int rocker_basic_hw_test(const struct rocker *rocker)
  232. {
  233. const struct pci_dev *pdev = rocker->pdev;
  234. struct rocker_wait wait;
  235. int err;
  236. err = rocker_reg_test(rocker);
  237. if (err) {
  238. dev_err(&pdev->dev, "reg test failed\n");
  239. return err;
  240. }
  241. err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST),
  242. rocker_test_irq_handler, 0,
  243. rocker_driver_name, &wait);
  244. if (err) {
  245. dev_err(&pdev->dev, "cannot assign test irq\n");
  246. return err;
  247. }
  248. rocker_wait_init(&wait);
  249. rocker_write32(rocker, TEST_IRQ, ROCKER_MSIX_VEC_TEST);
  250. if (!rocker_wait_event_timeout(&wait, HZ / 10)) {
  251. dev_err(&pdev->dev, "no interrupt received within a timeout\n");
  252. err = -EIO;
  253. goto free_irq;
  254. }
  255. err = rocker_dma_test(rocker, &wait);
  256. if (err)
  257. dev_err(&pdev->dev, "dma test failed\n");
  258. free_irq:
  259. free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_TEST), &wait);
  260. return err;
  261. }
  262. /******************************************
  263. * DMA rings and descriptors manipulations
  264. ******************************************/
  265. static u32 __pos_inc(u32 pos, size_t limit)
  266. {
  267. return ++pos == limit ? 0 : pos;
  268. }
  269. static int rocker_desc_err(const struct rocker_desc_info *desc_info)
  270. {
  271. int err = desc_info->desc->comp_err & ~ROCKER_DMA_DESC_COMP_ERR_GEN;
  272. switch (err) {
  273. case ROCKER_OK:
  274. return 0;
  275. case -ROCKER_ENOENT:
  276. return -ENOENT;
  277. case -ROCKER_ENXIO:
  278. return -ENXIO;
  279. case -ROCKER_ENOMEM:
  280. return -ENOMEM;
  281. case -ROCKER_EEXIST:
  282. return -EEXIST;
  283. case -ROCKER_EINVAL:
  284. return -EINVAL;
  285. case -ROCKER_EMSGSIZE:
  286. return -EMSGSIZE;
  287. case -ROCKER_ENOTSUP:
  288. return -EOPNOTSUPP;
  289. case -ROCKER_ENOBUFS:
  290. return -ENOBUFS;
  291. }
  292. return -EINVAL;
  293. }
  294. static void rocker_desc_gen_clear(const struct rocker_desc_info *desc_info)
  295. {
  296. desc_info->desc->comp_err &= ~ROCKER_DMA_DESC_COMP_ERR_GEN;
  297. }
  298. static bool rocker_desc_gen(const struct rocker_desc_info *desc_info)
  299. {
  300. u32 comp_err = desc_info->desc->comp_err;
  301. return comp_err & ROCKER_DMA_DESC_COMP_ERR_GEN ? true : false;
  302. }
  303. static void *
  304. rocker_desc_cookie_ptr_get(const struct rocker_desc_info *desc_info)
  305. {
  306. return (void *)(uintptr_t)desc_info->desc->cookie;
  307. }
  308. static void rocker_desc_cookie_ptr_set(const struct rocker_desc_info *desc_info,
  309. void *ptr)
  310. {
  311. desc_info->desc->cookie = (uintptr_t) ptr;
  312. }
  313. static struct rocker_desc_info *
  314. rocker_desc_head_get(const struct rocker_dma_ring_info *info)
  315. {
  316. static struct rocker_desc_info *desc_info;
  317. u32 head = __pos_inc(info->head, info->size);
  318. desc_info = &info->desc_info[info->head];
  319. if (head == info->tail)
  320. return NULL; /* ring full */
  321. desc_info->tlv_size = 0;
  322. return desc_info;
  323. }
  324. static void rocker_desc_commit(const struct rocker_desc_info *desc_info)
  325. {
  326. desc_info->desc->buf_size = desc_info->data_size;
  327. desc_info->desc->tlv_size = desc_info->tlv_size;
  328. }
  329. static void rocker_desc_head_set(const struct rocker *rocker,
  330. struct rocker_dma_ring_info *info,
  331. const struct rocker_desc_info *desc_info)
  332. {
  333. u32 head = __pos_inc(info->head, info->size);
  334. BUG_ON(head == info->tail);
  335. rocker_desc_commit(desc_info);
  336. info->head = head;
  337. rocker_write32(rocker, DMA_DESC_HEAD(info->type), head);
  338. }
  339. static struct rocker_desc_info *
  340. rocker_desc_tail_get(struct rocker_dma_ring_info *info)
  341. {
  342. static struct rocker_desc_info *desc_info;
  343. if (info->tail == info->head)
  344. return NULL; /* nothing to be done between head and tail */
  345. desc_info = &info->desc_info[info->tail];
  346. if (!rocker_desc_gen(desc_info))
  347. return NULL; /* gen bit not set, desc is not ready yet */
  348. info->tail = __pos_inc(info->tail, info->size);
  349. desc_info->tlv_size = desc_info->desc->tlv_size;
  350. return desc_info;
  351. }
  352. static void rocker_dma_ring_credits_set(const struct rocker *rocker,
  353. const struct rocker_dma_ring_info *info,
  354. u32 credits)
  355. {
  356. if (credits)
  357. rocker_write32(rocker, DMA_DESC_CREDITS(info->type), credits);
  358. }
  359. static unsigned long rocker_dma_ring_size_fix(size_t size)
  360. {
  361. return max(ROCKER_DMA_SIZE_MIN,
  362. min(roundup_pow_of_two(size), ROCKER_DMA_SIZE_MAX));
  363. }
  364. static int rocker_dma_ring_create(const struct rocker *rocker,
  365. unsigned int type,
  366. size_t size,
  367. struct rocker_dma_ring_info *info)
  368. {
  369. int i;
  370. BUG_ON(size != rocker_dma_ring_size_fix(size));
  371. info->size = size;
  372. info->type = type;
  373. info->head = 0;
  374. info->tail = 0;
  375. info->desc_info = kcalloc(info->size, sizeof(*info->desc_info),
  376. GFP_KERNEL);
  377. if (!info->desc_info)
  378. return -ENOMEM;
  379. info->desc = pci_alloc_consistent(rocker->pdev,
  380. info->size * sizeof(*info->desc),
  381. &info->mapaddr);
  382. if (!info->desc) {
  383. kfree(info->desc_info);
  384. return -ENOMEM;
  385. }
  386. for (i = 0; i < info->size; i++)
  387. info->desc_info[i].desc = &info->desc[i];
  388. rocker_write32(rocker, DMA_DESC_CTRL(info->type),
  389. ROCKER_DMA_DESC_CTRL_RESET);
  390. rocker_write64(rocker, DMA_DESC_ADDR(info->type), info->mapaddr);
  391. rocker_write32(rocker, DMA_DESC_SIZE(info->type), info->size);
  392. return 0;
  393. }
  394. static void rocker_dma_ring_destroy(const struct rocker *rocker,
  395. const struct rocker_dma_ring_info *info)
  396. {
  397. rocker_write64(rocker, DMA_DESC_ADDR(info->type), 0);
  398. pci_free_consistent(rocker->pdev,
  399. info->size * sizeof(struct rocker_desc),
  400. info->desc, info->mapaddr);
  401. kfree(info->desc_info);
  402. }
  403. static void rocker_dma_ring_pass_to_producer(const struct rocker *rocker,
  404. struct rocker_dma_ring_info *info)
  405. {
  406. int i;
  407. BUG_ON(info->head || info->tail);
  408. /* When ring is consumer, we need to advance head for each desc.
  409. * That tells hw that the desc is ready to be used by it.
  410. */
  411. for (i = 0; i < info->size - 1; i++)
  412. rocker_desc_head_set(rocker, info, &info->desc_info[i]);
  413. rocker_desc_commit(&info->desc_info[i]);
  414. }
  415. static int rocker_dma_ring_bufs_alloc(const struct rocker *rocker,
  416. const struct rocker_dma_ring_info *info,
  417. int direction, size_t buf_size)
  418. {
  419. struct pci_dev *pdev = rocker->pdev;
  420. int i;
  421. int err;
  422. for (i = 0; i < info->size; i++) {
  423. struct rocker_desc_info *desc_info = &info->desc_info[i];
  424. struct rocker_desc *desc = &info->desc[i];
  425. dma_addr_t dma_handle;
  426. char *buf;
  427. buf = kzalloc(buf_size, GFP_KERNEL | GFP_DMA);
  428. if (!buf) {
  429. err = -ENOMEM;
  430. goto rollback;
  431. }
  432. dma_handle = pci_map_single(pdev, buf, buf_size, direction);
  433. if (pci_dma_mapping_error(pdev, dma_handle)) {
  434. kfree(buf);
  435. err = -EIO;
  436. goto rollback;
  437. }
  438. desc_info->data = buf;
  439. desc_info->data_size = buf_size;
  440. dma_unmap_addr_set(desc_info, mapaddr, dma_handle);
  441. desc->buf_addr = dma_handle;
  442. desc->buf_size = buf_size;
  443. }
  444. return 0;
  445. rollback:
  446. for (i--; i >= 0; i--) {
  447. const struct rocker_desc_info *desc_info = &info->desc_info[i];
  448. pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
  449. desc_info->data_size, direction);
  450. kfree(desc_info->data);
  451. }
  452. return err;
  453. }
  454. static void rocker_dma_ring_bufs_free(const struct rocker *rocker,
  455. const struct rocker_dma_ring_info *info,
  456. int direction)
  457. {
  458. struct pci_dev *pdev = rocker->pdev;
  459. int i;
  460. for (i = 0; i < info->size; i++) {
  461. const struct rocker_desc_info *desc_info = &info->desc_info[i];
  462. struct rocker_desc *desc = &info->desc[i];
  463. desc->buf_addr = 0;
  464. desc->buf_size = 0;
  465. pci_unmap_single(pdev, dma_unmap_addr(desc_info, mapaddr),
  466. desc_info->data_size, direction);
  467. kfree(desc_info->data);
  468. }
  469. }
  470. static int rocker_dma_cmd_ring_wait_alloc(struct rocker_desc_info *desc_info)
  471. {
  472. struct rocker_wait *wait;
  473. wait = rocker_wait_create();
  474. if (!wait)
  475. return -ENOMEM;
  476. rocker_desc_cookie_ptr_set(desc_info, wait);
  477. return 0;
  478. }
  479. static void
  480. rocker_dma_cmd_ring_wait_free(const struct rocker_desc_info *desc_info)
  481. {
  482. struct rocker_wait *wait = rocker_desc_cookie_ptr_get(desc_info);
  483. rocker_wait_destroy(wait);
  484. }
  485. static int rocker_dma_cmd_ring_waits_alloc(const struct rocker *rocker)
  486. {
  487. const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
  488. int i;
  489. int err;
  490. for (i = 0; i < cmd_ring->size; i++) {
  491. err = rocker_dma_cmd_ring_wait_alloc(&cmd_ring->desc_info[i]);
  492. if (err)
  493. goto rollback;
  494. }
  495. return 0;
  496. rollback:
  497. for (i--; i >= 0; i--)
  498. rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
  499. return err;
  500. }
  501. static void rocker_dma_cmd_ring_waits_free(const struct rocker *rocker)
  502. {
  503. const struct rocker_dma_ring_info *cmd_ring = &rocker->cmd_ring;
  504. int i;
  505. for (i = 0; i < cmd_ring->size; i++)
  506. rocker_dma_cmd_ring_wait_free(&cmd_ring->desc_info[i]);
  507. }
  508. static int rocker_dma_rings_init(struct rocker *rocker)
  509. {
  510. const struct pci_dev *pdev = rocker->pdev;
  511. int err;
  512. err = rocker_dma_ring_create(rocker, ROCKER_DMA_CMD,
  513. ROCKER_DMA_CMD_DEFAULT_SIZE,
  514. &rocker->cmd_ring);
  515. if (err) {
  516. dev_err(&pdev->dev, "failed to create command dma ring\n");
  517. return err;
  518. }
  519. spin_lock_init(&rocker->cmd_ring_lock);
  520. err = rocker_dma_ring_bufs_alloc(rocker, &rocker->cmd_ring,
  521. PCI_DMA_BIDIRECTIONAL, PAGE_SIZE);
  522. if (err) {
  523. dev_err(&pdev->dev, "failed to alloc command dma ring buffers\n");
  524. goto err_dma_cmd_ring_bufs_alloc;
  525. }
  526. err = rocker_dma_cmd_ring_waits_alloc(rocker);
  527. if (err) {
  528. dev_err(&pdev->dev, "failed to alloc command dma ring waits\n");
  529. goto err_dma_cmd_ring_waits_alloc;
  530. }
  531. err = rocker_dma_ring_create(rocker, ROCKER_DMA_EVENT,
  532. ROCKER_DMA_EVENT_DEFAULT_SIZE,
  533. &rocker->event_ring);
  534. if (err) {
  535. dev_err(&pdev->dev, "failed to create event dma ring\n");
  536. goto err_dma_event_ring_create;
  537. }
  538. err = rocker_dma_ring_bufs_alloc(rocker, &rocker->event_ring,
  539. PCI_DMA_FROMDEVICE, PAGE_SIZE);
  540. if (err) {
  541. dev_err(&pdev->dev, "failed to alloc event dma ring buffers\n");
  542. goto err_dma_event_ring_bufs_alloc;
  543. }
  544. rocker_dma_ring_pass_to_producer(rocker, &rocker->event_ring);
  545. return 0;
  546. err_dma_event_ring_bufs_alloc:
  547. rocker_dma_ring_destroy(rocker, &rocker->event_ring);
  548. err_dma_event_ring_create:
  549. rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
  550. PCI_DMA_BIDIRECTIONAL);
  551. err_dma_cmd_ring_waits_alloc:
  552. rocker_dma_cmd_ring_waits_free(rocker);
  553. err_dma_cmd_ring_bufs_alloc:
  554. rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
  555. return err;
  556. }
  557. static void rocker_dma_rings_fini(struct rocker *rocker)
  558. {
  559. rocker_dma_ring_bufs_free(rocker, &rocker->event_ring,
  560. PCI_DMA_BIDIRECTIONAL);
  561. rocker_dma_ring_destroy(rocker, &rocker->event_ring);
  562. rocker_dma_cmd_ring_waits_free(rocker);
  563. rocker_dma_ring_bufs_free(rocker, &rocker->cmd_ring,
  564. PCI_DMA_BIDIRECTIONAL);
  565. rocker_dma_ring_destroy(rocker, &rocker->cmd_ring);
  566. }
  567. static int rocker_dma_rx_ring_skb_map(const struct rocker_port *rocker_port,
  568. struct rocker_desc_info *desc_info,
  569. struct sk_buff *skb, size_t buf_len)
  570. {
  571. const struct rocker *rocker = rocker_port->rocker;
  572. struct pci_dev *pdev = rocker->pdev;
  573. dma_addr_t dma_handle;
  574. dma_handle = pci_map_single(pdev, skb->data, buf_len,
  575. PCI_DMA_FROMDEVICE);
  576. if (pci_dma_mapping_error(pdev, dma_handle))
  577. return -EIO;
  578. if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_RX_FRAG_ADDR, dma_handle))
  579. goto tlv_put_failure;
  580. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_RX_FRAG_MAX_LEN, buf_len))
  581. goto tlv_put_failure;
  582. return 0;
  583. tlv_put_failure:
  584. pci_unmap_single(pdev, dma_handle, buf_len, PCI_DMA_FROMDEVICE);
  585. desc_info->tlv_size = 0;
  586. return -EMSGSIZE;
  587. }
  588. static size_t rocker_port_rx_buf_len(const struct rocker_port *rocker_port)
  589. {
  590. return rocker_port->dev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  591. }
  592. static int rocker_dma_rx_ring_skb_alloc(const struct rocker_port *rocker_port,
  593. struct rocker_desc_info *desc_info)
  594. {
  595. struct net_device *dev = rocker_port->dev;
  596. struct sk_buff *skb;
  597. size_t buf_len = rocker_port_rx_buf_len(rocker_port);
  598. int err;
  599. /* Ensure that hw will see tlv_size zero in case of an error.
  600. * That tells hw to use another descriptor.
  601. */
  602. rocker_desc_cookie_ptr_set(desc_info, NULL);
  603. desc_info->tlv_size = 0;
  604. skb = netdev_alloc_skb_ip_align(dev, buf_len);
  605. if (!skb)
  606. return -ENOMEM;
  607. err = rocker_dma_rx_ring_skb_map(rocker_port, desc_info, skb, buf_len);
  608. if (err) {
  609. dev_kfree_skb_any(skb);
  610. return err;
  611. }
  612. rocker_desc_cookie_ptr_set(desc_info, skb);
  613. return 0;
  614. }
  615. static void rocker_dma_rx_ring_skb_unmap(const struct rocker *rocker,
  616. const struct rocker_tlv **attrs)
  617. {
  618. struct pci_dev *pdev = rocker->pdev;
  619. dma_addr_t dma_handle;
  620. size_t len;
  621. if (!attrs[ROCKER_TLV_RX_FRAG_ADDR] ||
  622. !attrs[ROCKER_TLV_RX_FRAG_MAX_LEN])
  623. return;
  624. dma_handle = rocker_tlv_get_u64(attrs[ROCKER_TLV_RX_FRAG_ADDR]);
  625. len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_MAX_LEN]);
  626. pci_unmap_single(pdev, dma_handle, len, PCI_DMA_FROMDEVICE);
  627. }
  628. static void rocker_dma_rx_ring_skb_free(const struct rocker *rocker,
  629. const struct rocker_desc_info *desc_info)
  630. {
  631. const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
  632. struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
  633. if (!skb)
  634. return;
  635. rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
  636. rocker_dma_rx_ring_skb_unmap(rocker, attrs);
  637. dev_kfree_skb_any(skb);
  638. }
  639. static int rocker_dma_rx_ring_skbs_alloc(const struct rocker_port *rocker_port)
  640. {
  641. const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
  642. const struct rocker *rocker = rocker_port->rocker;
  643. int i;
  644. int err;
  645. for (i = 0; i < rx_ring->size; i++) {
  646. err = rocker_dma_rx_ring_skb_alloc(rocker_port,
  647. &rx_ring->desc_info[i]);
  648. if (err)
  649. goto rollback;
  650. }
  651. return 0;
  652. rollback:
  653. for (i--; i >= 0; i--)
  654. rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
  655. return err;
  656. }
  657. static void rocker_dma_rx_ring_skbs_free(const struct rocker_port *rocker_port)
  658. {
  659. const struct rocker_dma_ring_info *rx_ring = &rocker_port->rx_ring;
  660. const struct rocker *rocker = rocker_port->rocker;
  661. int i;
  662. for (i = 0; i < rx_ring->size; i++)
  663. rocker_dma_rx_ring_skb_free(rocker, &rx_ring->desc_info[i]);
  664. }
  665. static int rocker_port_dma_rings_init(struct rocker_port *rocker_port)
  666. {
  667. struct rocker *rocker = rocker_port->rocker;
  668. int err;
  669. err = rocker_dma_ring_create(rocker,
  670. ROCKER_DMA_TX(rocker_port->port_number),
  671. ROCKER_DMA_TX_DEFAULT_SIZE,
  672. &rocker_port->tx_ring);
  673. if (err) {
  674. netdev_err(rocker_port->dev, "failed to create tx dma ring\n");
  675. return err;
  676. }
  677. err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->tx_ring,
  678. PCI_DMA_TODEVICE,
  679. ROCKER_DMA_TX_DESC_SIZE);
  680. if (err) {
  681. netdev_err(rocker_port->dev, "failed to alloc tx dma ring buffers\n");
  682. goto err_dma_tx_ring_bufs_alloc;
  683. }
  684. err = rocker_dma_ring_create(rocker,
  685. ROCKER_DMA_RX(rocker_port->port_number),
  686. ROCKER_DMA_RX_DEFAULT_SIZE,
  687. &rocker_port->rx_ring);
  688. if (err) {
  689. netdev_err(rocker_port->dev, "failed to create rx dma ring\n");
  690. goto err_dma_rx_ring_create;
  691. }
  692. err = rocker_dma_ring_bufs_alloc(rocker, &rocker_port->rx_ring,
  693. PCI_DMA_BIDIRECTIONAL,
  694. ROCKER_DMA_RX_DESC_SIZE);
  695. if (err) {
  696. netdev_err(rocker_port->dev, "failed to alloc rx dma ring buffers\n");
  697. goto err_dma_rx_ring_bufs_alloc;
  698. }
  699. err = rocker_dma_rx_ring_skbs_alloc(rocker_port);
  700. if (err) {
  701. netdev_err(rocker_port->dev, "failed to alloc rx dma ring skbs\n");
  702. goto err_dma_rx_ring_skbs_alloc;
  703. }
  704. rocker_dma_ring_pass_to_producer(rocker, &rocker_port->rx_ring);
  705. return 0;
  706. err_dma_rx_ring_skbs_alloc:
  707. rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
  708. PCI_DMA_BIDIRECTIONAL);
  709. err_dma_rx_ring_bufs_alloc:
  710. rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
  711. err_dma_rx_ring_create:
  712. rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
  713. PCI_DMA_TODEVICE);
  714. err_dma_tx_ring_bufs_alloc:
  715. rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
  716. return err;
  717. }
  718. static void rocker_port_dma_rings_fini(struct rocker_port *rocker_port)
  719. {
  720. struct rocker *rocker = rocker_port->rocker;
  721. rocker_dma_rx_ring_skbs_free(rocker_port);
  722. rocker_dma_ring_bufs_free(rocker, &rocker_port->rx_ring,
  723. PCI_DMA_BIDIRECTIONAL);
  724. rocker_dma_ring_destroy(rocker, &rocker_port->rx_ring);
  725. rocker_dma_ring_bufs_free(rocker, &rocker_port->tx_ring,
  726. PCI_DMA_TODEVICE);
  727. rocker_dma_ring_destroy(rocker, &rocker_port->tx_ring);
  728. }
  729. static void rocker_port_set_enable(const struct rocker_port *rocker_port,
  730. bool enable)
  731. {
  732. u64 val = rocker_read64(rocker_port->rocker, PORT_PHYS_ENABLE);
  733. if (enable)
  734. val |= 1ULL << rocker_port->pport;
  735. else
  736. val &= ~(1ULL << rocker_port->pport);
  737. rocker_write64(rocker_port->rocker, PORT_PHYS_ENABLE, val);
  738. }
  739. /********************************
  740. * Interrupt handler and helpers
  741. ********************************/
  742. static irqreturn_t rocker_cmd_irq_handler(int irq, void *dev_id)
  743. {
  744. struct rocker *rocker = dev_id;
  745. const struct rocker_desc_info *desc_info;
  746. struct rocker_wait *wait;
  747. u32 credits = 0;
  748. spin_lock(&rocker->cmd_ring_lock);
  749. while ((desc_info = rocker_desc_tail_get(&rocker->cmd_ring))) {
  750. wait = rocker_desc_cookie_ptr_get(desc_info);
  751. if (wait->nowait) {
  752. rocker_desc_gen_clear(desc_info);
  753. } else {
  754. rocker_wait_wake_up(wait);
  755. }
  756. credits++;
  757. }
  758. spin_unlock(&rocker->cmd_ring_lock);
  759. rocker_dma_ring_credits_set(rocker, &rocker->cmd_ring, credits);
  760. return IRQ_HANDLED;
  761. }
  762. static void rocker_port_link_up(const struct rocker_port *rocker_port)
  763. {
  764. netif_carrier_on(rocker_port->dev);
  765. netdev_info(rocker_port->dev, "Link is up\n");
  766. }
  767. static void rocker_port_link_down(const struct rocker_port *rocker_port)
  768. {
  769. netif_carrier_off(rocker_port->dev);
  770. netdev_info(rocker_port->dev, "Link is down\n");
  771. }
  772. static int rocker_event_link_change(const struct rocker *rocker,
  773. const struct rocker_tlv *info)
  774. {
  775. const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_LINK_CHANGED_MAX + 1];
  776. unsigned int port_number;
  777. bool link_up;
  778. struct rocker_port *rocker_port;
  779. rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_LINK_CHANGED_MAX, info);
  780. if (!attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT] ||
  781. !attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP])
  782. return -EIO;
  783. port_number =
  784. rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_PPORT]) - 1;
  785. link_up = rocker_tlv_get_u8(attrs[ROCKER_TLV_EVENT_LINK_CHANGED_LINKUP]);
  786. if (port_number >= rocker->port_count)
  787. return -EINVAL;
  788. rocker_port = rocker->ports[port_number];
  789. if (netif_carrier_ok(rocker_port->dev) != link_up) {
  790. if (link_up)
  791. rocker_port_link_up(rocker_port);
  792. else
  793. rocker_port_link_down(rocker_port);
  794. }
  795. return 0;
  796. }
  797. static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
  798. const unsigned char *addr,
  799. __be16 vlan_id);
  800. static int rocker_event_mac_vlan_seen(const struct rocker *rocker,
  801. const struct rocker_tlv *info)
  802. {
  803. const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAX + 1];
  804. unsigned int port_number;
  805. struct rocker_port *rocker_port;
  806. const unsigned char *addr;
  807. __be16 vlan_id;
  808. rocker_tlv_parse_nested(attrs, ROCKER_TLV_EVENT_MAC_VLAN_MAX, info);
  809. if (!attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT] ||
  810. !attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC] ||
  811. !attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID])
  812. return -EIO;
  813. port_number =
  814. rocker_tlv_get_u32(attrs[ROCKER_TLV_EVENT_MAC_VLAN_PPORT]) - 1;
  815. addr = rocker_tlv_data(attrs[ROCKER_TLV_EVENT_MAC_VLAN_MAC]);
  816. vlan_id = rocker_tlv_get_be16(attrs[ROCKER_TLV_EVENT_MAC_VLAN_VLAN_ID]);
  817. if (port_number >= rocker->port_count)
  818. return -EINVAL;
  819. rocker_port = rocker->ports[port_number];
  820. return rocker_world_port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
  821. }
  822. static int rocker_event_process(const struct rocker *rocker,
  823. const struct rocker_desc_info *desc_info)
  824. {
  825. const struct rocker_tlv *attrs[ROCKER_TLV_EVENT_MAX + 1];
  826. const struct rocker_tlv *info;
  827. u16 type;
  828. rocker_tlv_parse_desc(attrs, ROCKER_TLV_EVENT_MAX, desc_info);
  829. if (!attrs[ROCKER_TLV_EVENT_TYPE] ||
  830. !attrs[ROCKER_TLV_EVENT_INFO])
  831. return -EIO;
  832. type = rocker_tlv_get_u16(attrs[ROCKER_TLV_EVENT_TYPE]);
  833. info = attrs[ROCKER_TLV_EVENT_INFO];
  834. switch (type) {
  835. case ROCKER_TLV_EVENT_TYPE_LINK_CHANGED:
  836. return rocker_event_link_change(rocker, info);
  837. case ROCKER_TLV_EVENT_TYPE_MAC_VLAN_SEEN:
  838. return rocker_event_mac_vlan_seen(rocker, info);
  839. }
  840. return -EOPNOTSUPP;
  841. }
  842. static irqreturn_t rocker_event_irq_handler(int irq, void *dev_id)
  843. {
  844. struct rocker *rocker = dev_id;
  845. const struct pci_dev *pdev = rocker->pdev;
  846. const struct rocker_desc_info *desc_info;
  847. u32 credits = 0;
  848. int err;
  849. while ((desc_info = rocker_desc_tail_get(&rocker->event_ring))) {
  850. err = rocker_desc_err(desc_info);
  851. if (err) {
  852. dev_err(&pdev->dev, "event desc received with err %d\n",
  853. err);
  854. } else {
  855. err = rocker_event_process(rocker, desc_info);
  856. if (err)
  857. dev_err(&pdev->dev, "event processing failed with err %d\n",
  858. err);
  859. }
  860. rocker_desc_gen_clear(desc_info);
  861. rocker_desc_head_set(rocker, &rocker->event_ring, desc_info);
  862. credits++;
  863. }
  864. rocker_dma_ring_credits_set(rocker, &rocker->event_ring, credits);
  865. return IRQ_HANDLED;
  866. }
  867. static irqreturn_t rocker_tx_irq_handler(int irq, void *dev_id)
  868. {
  869. struct rocker_port *rocker_port = dev_id;
  870. napi_schedule(&rocker_port->napi_tx);
  871. return IRQ_HANDLED;
  872. }
  873. static irqreturn_t rocker_rx_irq_handler(int irq, void *dev_id)
  874. {
  875. struct rocker_port *rocker_port = dev_id;
  876. napi_schedule(&rocker_port->napi_rx);
  877. return IRQ_HANDLED;
  878. }
  879. /********************
  880. * Command interface
  881. ********************/
  882. int rocker_cmd_exec(struct rocker_port *rocker_port, bool nowait,
  883. rocker_cmd_prep_cb_t prepare, void *prepare_priv,
  884. rocker_cmd_proc_cb_t process, void *process_priv)
  885. {
  886. struct rocker *rocker = rocker_port->rocker;
  887. struct rocker_desc_info *desc_info;
  888. struct rocker_wait *wait;
  889. unsigned long lock_flags;
  890. int err;
  891. spin_lock_irqsave(&rocker->cmd_ring_lock, lock_flags);
  892. desc_info = rocker_desc_head_get(&rocker->cmd_ring);
  893. if (!desc_info) {
  894. spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
  895. return -EAGAIN;
  896. }
  897. wait = rocker_desc_cookie_ptr_get(desc_info);
  898. rocker_wait_init(wait);
  899. wait->nowait = nowait;
  900. err = prepare(rocker_port, desc_info, prepare_priv);
  901. if (err) {
  902. spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
  903. return err;
  904. }
  905. rocker_desc_head_set(rocker, &rocker->cmd_ring, desc_info);
  906. spin_unlock_irqrestore(&rocker->cmd_ring_lock, lock_flags);
  907. if (nowait)
  908. return 0;
  909. if (!rocker_wait_event_timeout(wait, HZ / 10))
  910. return -EIO;
  911. err = rocker_desc_err(desc_info);
  912. if (err)
  913. return err;
  914. if (process)
  915. err = process(rocker_port, desc_info, process_priv);
  916. rocker_desc_gen_clear(desc_info);
  917. return err;
  918. }
  919. static int
  920. rocker_cmd_get_port_settings_prep(const struct rocker_port *rocker_port,
  921. struct rocker_desc_info *desc_info,
  922. void *priv)
  923. {
  924. struct rocker_tlv *cmd_info;
  925. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
  926. ROCKER_TLV_CMD_TYPE_GET_PORT_SETTINGS))
  927. return -EMSGSIZE;
  928. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  929. if (!cmd_info)
  930. return -EMSGSIZE;
  931. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
  932. rocker_port->pport))
  933. return -EMSGSIZE;
  934. rocker_tlv_nest_end(desc_info, cmd_info);
  935. return 0;
  936. }
  937. static int
  938. rocker_cmd_get_port_settings_ethtool_proc(const struct rocker_port *rocker_port,
  939. const struct rocker_desc_info *desc_info,
  940. void *priv)
  941. {
  942. struct ethtool_link_ksettings *ecmd = priv;
  943. const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
  944. const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
  945. u32 speed;
  946. u8 duplex;
  947. u8 autoneg;
  948. rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
  949. if (!attrs[ROCKER_TLV_CMD_INFO])
  950. return -EIO;
  951. rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
  952. attrs[ROCKER_TLV_CMD_INFO]);
  953. if (!info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED] ||
  954. !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX] ||
  955. !info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG])
  956. return -EIO;
  957. speed = rocker_tlv_get_u32(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_SPEED]);
  958. duplex = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX]);
  959. autoneg = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG]);
  960. ethtool_link_ksettings_zero_link_mode(ecmd, supported);
  961. ethtool_link_ksettings_add_link_mode(ecmd, supported, TP);
  962. ecmd->base.phy_address = 0xff;
  963. ecmd->base.port = PORT_TP;
  964. ecmd->base.speed = speed;
  965. ecmd->base.duplex = duplex ? DUPLEX_FULL : DUPLEX_HALF;
  966. ecmd->base.autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  967. return 0;
  968. }
  969. static int
  970. rocker_cmd_get_port_settings_macaddr_proc(const struct rocker_port *rocker_port,
  971. const struct rocker_desc_info *desc_info,
  972. void *priv)
  973. {
  974. unsigned char *macaddr = priv;
  975. const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
  976. const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
  977. const struct rocker_tlv *attr;
  978. rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
  979. if (!attrs[ROCKER_TLV_CMD_INFO])
  980. return -EIO;
  981. rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
  982. attrs[ROCKER_TLV_CMD_INFO]);
  983. attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR];
  984. if (!attr)
  985. return -EIO;
  986. if (rocker_tlv_len(attr) != ETH_ALEN)
  987. return -EINVAL;
  988. ether_addr_copy(macaddr, rocker_tlv_data(attr));
  989. return 0;
  990. }
  991. static int
  992. rocker_cmd_get_port_settings_mode_proc(const struct rocker_port *rocker_port,
  993. const struct rocker_desc_info *desc_info,
  994. void *priv)
  995. {
  996. u8 *p_mode = priv;
  997. const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
  998. const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
  999. const struct rocker_tlv *attr;
  1000. rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
  1001. if (!attrs[ROCKER_TLV_CMD_INFO])
  1002. return -EIO;
  1003. rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
  1004. attrs[ROCKER_TLV_CMD_INFO]);
  1005. attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE];
  1006. if (!attr)
  1007. return -EIO;
  1008. *p_mode = rocker_tlv_get_u8(info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MODE]);
  1009. return 0;
  1010. }
  1011. struct port_name {
  1012. char *buf;
  1013. size_t len;
  1014. };
  1015. static int
  1016. rocker_cmd_get_port_settings_phys_name_proc(const struct rocker_port *rocker_port,
  1017. const struct rocker_desc_info *desc_info,
  1018. void *priv)
  1019. {
  1020. const struct rocker_tlv *info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_MAX + 1];
  1021. const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
  1022. struct port_name *name = priv;
  1023. const struct rocker_tlv *attr;
  1024. size_t i, j, len;
  1025. const char *str;
  1026. rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
  1027. if (!attrs[ROCKER_TLV_CMD_INFO])
  1028. return -EIO;
  1029. rocker_tlv_parse_nested(info_attrs, ROCKER_TLV_CMD_PORT_SETTINGS_MAX,
  1030. attrs[ROCKER_TLV_CMD_INFO]);
  1031. attr = info_attrs[ROCKER_TLV_CMD_PORT_SETTINGS_PHYS_NAME];
  1032. if (!attr)
  1033. return -EIO;
  1034. len = min_t(size_t, rocker_tlv_len(attr), name->len);
  1035. str = rocker_tlv_data(attr);
  1036. /* make sure name only contains alphanumeric characters */
  1037. for (i = j = 0; i < len; ++i) {
  1038. if (isalnum(str[i])) {
  1039. name->buf[j] = str[i];
  1040. j++;
  1041. }
  1042. }
  1043. if (j == 0)
  1044. return -EIO;
  1045. name->buf[j] = '\0';
  1046. return 0;
  1047. }
  1048. static int
  1049. rocker_cmd_set_port_settings_ethtool_prep(const struct rocker_port *rocker_port,
  1050. struct rocker_desc_info *desc_info,
  1051. void *priv)
  1052. {
  1053. struct ethtool_link_ksettings *ecmd = priv;
  1054. struct rocker_tlv *cmd_info;
  1055. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
  1056. ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
  1057. return -EMSGSIZE;
  1058. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  1059. if (!cmd_info)
  1060. return -EMSGSIZE;
  1061. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
  1062. rocker_port->pport))
  1063. return -EMSGSIZE;
  1064. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_SPEED,
  1065. ecmd->base.speed))
  1066. return -EMSGSIZE;
  1067. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_DUPLEX,
  1068. ecmd->base.duplex))
  1069. return -EMSGSIZE;
  1070. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_AUTONEG,
  1071. ecmd->base.autoneg))
  1072. return -EMSGSIZE;
  1073. rocker_tlv_nest_end(desc_info, cmd_info);
  1074. return 0;
  1075. }
  1076. static int
  1077. rocker_cmd_set_port_settings_macaddr_prep(const struct rocker_port *rocker_port,
  1078. struct rocker_desc_info *desc_info,
  1079. void *priv)
  1080. {
  1081. const unsigned char *macaddr = priv;
  1082. struct rocker_tlv *cmd_info;
  1083. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
  1084. ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
  1085. return -EMSGSIZE;
  1086. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  1087. if (!cmd_info)
  1088. return -EMSGSIZE;
  1089. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
  1090. rocker_port->pport))
  1091. return -EMSGSIZE;
  1092. if (rocker_tlv_put(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MACADDR,
  1093. ETH_ALEN, macaddr))
  1094. return -EMSGSIZE;
  1095. rocker_tlv_nest_end(desc_info, cmd_info);
  1096. return 0;
  1097. }
  1098. static int
  1099. rocker_cmd_set_port_settings_mtu_prep(const struct rocker_port *rocker_port,
  1100. struct rocker_desc_info *desc_info,
  1101. void *priv)
  1102. {
  1103. int mtu = *(int *)priv;
  1104. struct rocker_tlv *cmd_info;
  1105. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
  1106. ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
  1107. return -EMSGSIZE;
  1108. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  1109. if (!cmd_info)
  1110. return -EMSGSIZE;
  1111. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
  1112. rocker_port->pport))
  1113. return -EMSGSIZE;
  1114. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_MTU,
  1115. mtu))
  1116. return -EMSGSIZE;
  1117. rocker_tlv_nest_end(desc_info, cmd_info);
  1118. return 0;
  1119. }
  1120. static int
  1121. rocker_cmd_set_port_learning_prep(const struct rocker_port *rocker_port,
  1122. struct rocker_desc_info *desc_info,
  1123. void *priv)
  1124. {
  1125. bool learning = *(bool *)priv;
  1126. struct rocker_tlv *cmd_info;
  1127. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
  1128. ROCKER_TLV_CMD_TYPE_SET_PORT_SETTINGS))
  1129. return -EMSGSIZE;
  1130. cmd_info = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  1131. if (!cmd_info)
  1132. return -EMSGSIZE;
  1133. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_PPORT,
  1134. rocker_port->pport))
  1135. return -EMSGSIZE;
  1136. if (rocker_tlv_put_u8(desc_info, ROCKER_TLV_CMD_PORT_SETTINGS_LEARNING,
  1137. learning))
  1138. return -EMSGSIZE;
  1139. rocker_tlv_nest_end(desc_info, cmd_info);
  1140. return 0;
  1141. }
  1142. static int
  1143. rocker_cmd_get_port_settings_ethtool(struct rocker_port *rocker_port,
  1144. struct ethtool_link_ksettings *ecmd)
  1145. {
  1146. return rocker_cmd_exec(rocker_port, false,
  1147. rocker_cmd_get_port_settings_prep, NULL,
  1148. rocker_cmd_get_port_settings_ethtool_proc,
  1149. ecmd);
  1150. }
  1151. static int rocker_cmd_get_port_settings_macaddr(struct rocker_port *rocker_port,
  1152. unsigned char *macaddr)
  1153. {
  1154. return rocker_cmd_exec(rocker_port, false,
  1155. rocker_cmd_get_port_settings_prep, NULL,
  1156. rocker_cmd_get_port_settings_macaddr_proc,
  1157. macaddr);
  1158. }
  1159. static int rocker_cmd_get_port_settings_mode(struct rocker_port *rocker_port,
  1160. u8 *p_mode)
  1161. {
  1162. return rocker_cmd_exec(rocker_port, false,
  1163. rocker_cmd_get_port_settings_prep, NULL,
  1164. rocker_cmd_get_port_settings_mode_proc, p_mode);
  1165. }
  1166. static int
  1167. rocker_cmd_set_port_settings_ethtool(struct rocker_port *rocker_port,
  1168. const struct ethtool_link_ksettings *ecmd)
  1169. {
  1170. struct ethtool_link_ksettings copy_ecmd;
  1171. memcpy(&copy_ecmd, ecmd, sizeof(copy_ecmd));
  1172. return rocker_cmd_exec(rocker_port, false,
  1173. rocker_cmd_set_port_settings_ethtool_prep,
  1174. &copy_ecmd, NULL, NULL);
  1175. }
  1176. static int rocker_cmd_set_port_settings_macaddr(struct rocker_port *rocker_port,
  1177. unsigned char *macaddr)
  1178. {
  1179. return rocker_cmd_exec(rocker_port, false,
  1180. rocker_cmd_set_port_settings_macaddr_prep,
  1181. macaddr, NULL, NULL);
  1182. }
  1183. static int rocker_cmd_set_port_settings_mtu(struct rocker_port *rocker_port,
  1184. int mtu)
  1185. {
  1186. return rocker_cmd_exec(rocker_port, false,
  1187. rocker_cmd_set_port_settings_mtu_prep,
  1188. &mtu, NULL, NULL);
  1189. }
  1190. int rocker_port_set_learning(struct rocker_port *rocker_port,
  1191. bool learning)
  1192. {
  1193. return rocker_cmd_exec(rocker_port, false,
  1194. rocker_cmd_set_port_learning_prep,
  1195. &learning, NULL, NULL);
  1196. }
  1197. /**********************
  1198. * Worlds manipulation
  1199. **********************/
  1200. static struct rocker_world_ops *rocker_world_ops[] = {
  1201. &rocker_ofdpa_ops,
  1202. };
  1203. #define ROCKER_WORLD_OPS_LEN ARRAY_SIZE(rocker_world_ops)
  1204. static struct rocker_world_ops *rocker_world_ops_find(u8 mode)
  1205. {
  1206. int i;
  1207. for (i = 0; i < ROCKER_WORLD_OPS_LEN; i++)
  1208. if (rocker_world_ops[i]->mode == mode)
  1209. return rocker_world_ops[i];
  1210. return NULL;
  1211. }
  1212. static int rocker_world_init(struct rocker *rocker, u8 mode)
  1213. {
  1214. struct rocker_world_ops *wops;
  1215. int err;
  1216. wops = rocker_world_ops_find(mode);
  1217. if (!wops) {
  1218. dev_err(&rocker->pdev->dev, "port mode \"%d\" is not supported\n",
  1219. mode);
  1220. return -EINVAL;
  1221. }
  1222. rocker->wops = wops;
  1223. rocker->wpriv = kzalloc(wops->priv_size, GFP_KERNEL);
  1224. if (!rocker->wpriv)
  1225. return -ENOMEM;
  1226. if (!wops->init)
  1227. return 0;
  1228. err = wops->init(rocker);
  1229. if (err)
  1230. kfree(rocker->wpriv);
  1231. return err;
  1232. }
  1233. static void rocker_world_fini(struct rocker *rocker)
  1234. {
  1235. struct rocker_world_ops *wops = rocker->wops;
  1236. if (!wops || !wops->fini)
  1237. return;
  1238. wops->fini(rocker);
  1239. kfree(rocker->wpriv);
  1240. }
  1241. static int rocker_world_check_init(struct rocker_port *rocker_port)
  1242. {
  1243. struct rocker *rocker = rocker_port->rocker;
  1244. u8 mode;
  1245. int err;
  1246. err = rocker_cmd_get_port_settings_mode(rocker_port, &mode);
  1247. if (err) {
  1248. dev_err(&rocker->pdev->dev, "failed to get port mode\n");
  1249. return err;
  1250. }
  1251. if (rocker->wops) {
  1252. if (rocker->wops->mode != mode) {
  1253. dev_err(&rocker->pdev->dev, "hardware has ports in different worlds, which is not supported\n");
  1254. return -EINVAL;
  1255. }
  1256. return 0;
  1257. }
  1258. return rocker_world_init(rocker, mode);
  1259. }
  1260. static int rocker_world_port_pre_init(struct rocker_port *rocker_port)
  1261. {
  1262. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1263. int err;
  1264. rocker_port->wpriv = kzalloc(wops->port_priv_size, GFP_KERNEL);
  1265. if (!rocker_port->wpriv)
  1266. return -ENOMEM;
  1267. if (!wops->port_pre_init)
  1268. return 0;
  1269. err = wops->port_pre_init(rocker_port);
  1270. if (err)
  1271. kfree(rocker_port->wpriv);
  1272. return 0;
  1273. }
  1274. static int rocker_world_port_init(struct rocker_port *rocker_port)
  1275. {
  1276. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1277. if (!wops->port_init)
  1278. return 0;
  1279. return wops->port_init(rocker_port);
  1280. }
  1281. static void rocker_world_port_fini(struct rocker_port *rocker_port)
  1282. {
  1283. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1284. if (!wops->port_fini)
  1285. return;
  1286. wops->port_fini(rocker_port);
  1287. }
  1288. static void rocker_world_port_post_fini(struct rocker_port *rocker_port)
  1289. {
  1290. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1291. if (!wops->port_post_fini)
  1292. return;
  1293. wops->port_post_fini(rocker_port);
  1294. kfree(rocker_port->wpriv);
  1295. }
  1296. static int rocker_world_port_open(struct rocker_port *rocker_port)
  1297. {
  1298. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1299. if (!wops->port_open)
  1300. return 0;
  1301. return wops->port_open(rocker_port);
  1302. }
  1303. static void rocker_world_port_stop(struct rocker_port *rocker_port)
  1304. {
  1305. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1306. if (!wops->port_stop)
  1307. return;
  1308. wops->port_stop(rocker_port);
  1309. }
  1310. static int rocker_world_port_attr_stp_state_set(struct rocker_port *rocker_port,
  1311. u8 state,
  1312. struct switchdev_trans *trans)
  1313. {
  1314. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1315. if (!wops->port_attr_stp_state_set)
  1316. return -EOPNOTSUPP;
  1317. if (switchdev_trans_ph_prepare(trans))
  1318. return 0;
  1319. return wops->port_attr_stp_state_set(rocker_port, state);
  1320. }
  1321. static int
  1322. rocker_world_port_attr_bridge_flags_set(struct rocker_port *rocker_port,
  1323. unsigned long brport_flags,
  1324. struct switchdev_trans *trans)
  1325. {
  1326. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1327. if (!wops->port_attr_bridge_flags_set)
  1328. return -EOPNOTSUPP;
  1329. if (switchdev_trans_ph_prepare(trans))
  1330. return 0;
  1331. return wops->port_attr_bridge_flags_set(rocker_port, brport_flags,
  1332. trans);
  1333. }
  1334. static int
  1335. rocker_world_port_attr_bridge_flags_get(const struct rocker_port *rocker_port,
  1336. unsigned long *p_brport_flags)
  1337. {
  1338. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1339. if (!wops->port_attr_bridge_flags_get)
  1340. return -EOPNOTSUPP;
  1341. return wops->port_attr_bridge_flags_get(rocker_port, p_brport_flags);
  1342. }
  1343. static int
  1344. rocker_world_port_attr_bridge_flags_support_get(const struct rocker_port *
  1345. rocker_port,
  1346. unsigned long *
  1347. p_brport_flags_support)
  1348. {
  1349. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1350. if (!wops->port_attr_bridge_flags_support_get)
  1351. return -EOPNOTSUPP;
  1352. return wops->port_attr_bridge_flags_support_get(rocker_port,
  1353. p_brport_flags_support);
  1354. }
  1355. static int
  1356. rocker_world_port_attr_bridge_ageing_time_set(struct rocker_port *rocker_port,
  1357. u32 ageing_time,
  1358. struct switchdev_trans *trans)
  1359. {
  1360. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1361. if (!wops->port_attr_bridge_ageing_time_set)
  1362. return -EOPNOTSUPP;
  1363. if (switchdev_trans_ph_prepare(trans))
  1364. return 0;
  1365. return wops->port_attr_bridge_ageing_time_set(rocker_port, ageing_time,
  1366. trans);
  1367. }
  1368. static int
  1369. rocker_world_port_obj_vlan_add(struct rocker_port *rocker_port,
  1370. const struct switchdev_obj_port_vlan *vlan,
  1371. struct switchdev_trans *trans)
  1372. {
  1373. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1374. if (netif_is_bridge_master(vlan->obj.orig_dev))
  1375. return -EOPNOTSUPP;
  1376. if (!wops->port_obj_vlan_add)
  1377. return -EOPNOTSUPP;
  1378. if (switchdev_trans_ph_prepare(trans))
  1379. return 0;
  1380. return wops->port_obj_vlan_add(rocker_port, vlan);
  1381. }
  1382. static int
  1383. rocker_world_port_obj_vlan_del(struct rocker_port *rocker_port,
  1384. const struct switchdev_obj_port_vlan *vlan)
  1385. {
  1386. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1387. if (netif_is_bridge_master(vlan->obj.orig_dev))
  1388. return -EOPNOTSUPP;
  1389. if (!wops->port_obj_vlan_del)
  1390. return -EOPNOTSUPP;
  1391. return wops->port_obj_vlan_del(rocker_port, vlan);
  1392. }
  1393. static int
  1394. rocker_world_port_fdb_add(struct rocker_port *rocker_port,
  1395. struct switchdev_notifier_fdb_info *info)
  1396. {
  1397. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1398. if (!wops->port_obj_fdb_add)
  1399. return -EOPNOTSUPP;
  1400. return wops->port_obj_fdb_add(rocker_port, info->vid, info->addr);
  1401. }
  1402. static int
  1403. rocker_world_port_fdb_del(struct rocker_port *rocker_port,
  1404. struct switchdev_notifier_fdb_info *info)
  1405. {
  1406. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1407. if (!wops->port_obj_fdb_del)
  1408. return -EOPNOTSUPP;
  1409. return wops->port_obj_fdb_del(rocker_port, info->vid, info->addr);
  1410. }
  1411. static int rocker_world_port_master_linked(struct rocker_port *rocker_port,
  1412. struct net_device *master)
  1413. {
  1414. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1415. if (!wops->port_master_linked)
  1416. return -EOPNOTSUPP;
  1417. return wops->port_master_linked(rocker_port, master);
  1418. }
  1419. static int rocker_world_port_master_unlinked(struct rocker_port *rocker_port,
  1420. struct net_device *master)
  1421. {
  1422. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1423. if (!wops->port_master_unlinked)
  1424. return -EOPNOTSUPP;
  1425. return wops->port_master_unlinked(rocker_port, master);
  1426. }
  1427. static int rocker_world_port_neigh_update(struct rocker_port *rocker_port,
  1428. struct neighbour *n)
  1429. {
  1430. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1431. if (!wops->port_neigh_update)
  1432. return -EOPNOTSUPP;
  1433. return wops->port_neigh_update(rocker_port, n);
  1434. }
  1435. static int rocker_world_port_neigh_destroy(struct rocker_port *rocker_port,
  1436. struct neighbour *n)
  1437. {
  1438. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1439. if (!wops->port_neigh_destroy)
  1440. return -EOPNOTSUPP;
  1441. return wops->port_neigh_destroy(rocker_port, n);
  1442. }
  1443. static int rocker_world_port_ev_mac_vlan_seen(struct rocker_port *rocker_port,
  1444. const unsigned char *addr,
  1445. __be16 vlan_id)
  1446. {
  1447. struct rocker_world_ops *wops = rocker_port->rocker->wops;
  1448. if (!wops->port_ev_mac_vlan_seen)
  1449. return -EOPNOTSUPP;
  1450. return wops->port_ev_mac_vlan_seen(rocker_port, addr, vlan_id);
  1451. }
  1452. static int rocker_world_fib4_add(struct rocker *rocker,
  1453. const struct fib_entry_notifier_info *fen_info)
  1454. {
  1455. struct rocker_world_ops *wops = rocker->wops;
  1456. if (!wops->fib4_add)
  1457. return 0;
  1458. return wops->fib4_add(rocker, fen_info);
  1459. }
  1460. static int rocker_world_fib4_del(struct rocker *rocker,
  1461. const struct fib_entry_notifier_info *fen_info)
  1462. {
  1463. struct rocker_world_ops *wops = rocker->wops;
  1464. if (!wops->fib4_del)
  1465. return 0;
  1466. return wops->fib4_del(rocker, fen_info);
  1467. }
  1468. static void rocker_world_fib4_abort(struct rocker *rocker)
  1469. {
  1470. struct rocker_world_ops *wops = rocker->wops;
  1471. if (wops->fib4_abort)
  1472. wops->fib4_abort(rocker);
  1473. }
  1474. /*****************
  1475. * Net device ops
  1476. *****************/
  1477. static int rocker_port_open(struct net_device *dev)
  1478. {
  1479. struct rocker_port *rocker_port = netdev_priv(dev);
  1480. int err;
  1481. err = rocker_port_dma_rings_init(rocker_port);
  1482. if (err)
  1483. return err;
  1484. err = request_irq(rocker_msix_tx_vector(rocker_port),
  1485. rocker_tx_irq_handler, 0,
  1486. rocker_driver_name, rocker_port);
  1487. if (err) {
  1488. netdev_err(rocker_port->dev, "cannot assign tx irq\n");
  1489. goto err_request_tx_irq;
  1490. }
  1491. err = request_irq(rocker_msix_rx_vector(rocker_port),
  1492. rocker_rx_irq_handler, 0,
  1493. rocker_driver_name, rocker_port);
  1494. if (err) {
  1495. netdev_err(rocker_port->dev, "cannot assign rx irq\n");
  1496. goto err_request_rx_irq;
  1497. }
  1498. err = rocker_world_port_open(rocker_port);
  1499. if (err) {
  1500. netdev_err(rocker_port->dev, "cannot open port in world\n");
  1501. goto err_world_port_open;
  1502. }
  1503. napi_enable(&rocker_port->napi_tx);
  1504. napi_enable(&rocker_port->napi_rx);
  1505. if (!dev->proto_down)
  1506. rocker_port_set_enable(rocker_port, true);
  1507. netif_start_queue(dev);
  1508. return 0;
  1509. err_world_port_open:
  1510. free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
  1511. err_request_rx_irq:
  1512. free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
  1513. err_request_tx_irq:
  1514. rocker_port_dma_rings_fini(rocker_port);
  1515. return err;
  1516. }
  1517. static int rocker_port_stop(struct net_device *dev)
  1518. {
  1519. struct rocker_port *rocker_port = netdev_priv(dev);
  1520. netif_stop_queue(dev);
  1521. rocker_port_set_enable(rocker_port, false);
  1522. napi_disable(&rocker_port->napi_rx);
  1523. napi_disable(&rocker_port->napi_tx);
  1524. rocker_world_port_stop(rocker_port);
  1525. free_irq(rocker_msix_rx_vector(rocker_port), rocker_port);
  1526. free_irq(rocker_msix_tx_vector(rocker_port), rocker_port);
  1527. rocker_port_dma_rings_fini(rocker_port);
  1528. return 0;
  1529. }
  1530. static void rocker_tx_desc_frags_unmap(const struct rocker_port *rocker_port,
  1531. const struct rocker_desc_info *desc_info)
  1532. {
  1533. const struct rocker *rocker = rocker_port->rocker;
  1534. struct pci_dev *pdev = rocker->pdev;
  1535. const struct rocker_tlv *attrs[ROCKER_TLV_TX_MAX + 1];
  1536. struct rocker_tlv *attr;
  1537. int rem;
  1538. rocker_tlv_parse_desc(attrs, ROCKER_TLV_TX_MAX, desc_info);
  1539. if (!attrs[ROCKER_TLV_TX_FRAGS])
  1540. return;
  1541. rocker_tlv_for_each_nested(attr, attrs[ROCKER_TLV_TX_FRAGS], rem) {
  1542. const struct rocker_tlv *frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_MAX + 1];
  1543. dma_addr_t dma_handle;
  1544. size_t len;
  1545. if (rocker_tlv_type(attr) != ROCKER_TLV_TX_FRAG)
  1546. continue;
  1547. rocker_tlv_parse_nested(frag_attrs, ROCKER_TLV_TX_FRAG_ATTR_MAX,
  1548. attr);
  1549. if (!frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR] ||
  1550. !frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN])
  1551. continue;
  1552. dma_handle = rocker_tlv_get_u64(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_ADDR]);
  1553. len = rocker_tlv_get_u16(frag_attrs[ROCKER_TLV_TX_FRAG_ATTR_LEN]);
  1554. pci_unmap_single(pdev, dma_handle, len, DMA_TO_DEVICE);
  1555. }
  1556. }
  1557. static int rocker_tx_desc_frag_map_put(const struct rocker_port *rocker_port,
  1558. struct rocker_desc_info *desc_info,
  1559. char *buf, size_t buf_len)
  1560. {
  1561. const struct rocker *rocker = rocker_port->rocker;
  1562. struct pci_dev *pdev = rocker->pdev;
  1563. dma_addr_t dma_handle;
  1564. struct rocker_tlv *frag;
  1565. dma_handle = pci_map_single(pdev, buf, buf_len, DMA_TO_DEVICE);
  1566. if (unlikely(pci_dma_mapping_error(pdev, dma_handle))) {
  1567. if (net_ratelimit())
  1568. netdev_err(rocker_port->dev, "failed to dma map tx frag\n");
  1569. return -EIO;
  1570. }
  1571. frag = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAG);
  1572. if (!frag)
  1573. goto unmap_frag;
  1574. if (rocker_tlv_put_u64(desc_info, ROCKER_TLV_TX_FRAG_ATTR_ADDR,
  1575. dma_handle))
  1576. goto nest_cancel;
  1577. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_TX_FRAG_ATTR_LEN,
  1578. buf_len))
  1579. goto nest_cancel;
  1580. rocker_tlv_nest_end(desc_info, frag);
  1581. return 0;
  1582. nest_cancel:
  1583. rocker_tlv_nest_cancel(desc_info, frag);
  1584. unmap_frag:
  1585. pci_unmap_single(pdev, dma_handle, buf_len, DMA_TO_DEVICE);
  1586. return -EMSGSIZE;
  1587. }
  1588. static netdev_tx_t rocker_port_xmit(struct sk_buff *skb, struct net_device *dev)
  1589. {
  1590. struct rocker_port *rocker_port = netdev_priv(dev);
  1591. struct rocker *rocker = rocker_port->rocker;
  1592. struct rocker_desc_info *desc_info;
  1593. struct rocker_tlv *frags;
  1594. int i;
  1595. int err;
  1596. desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
  1597. if (unlikely(!desc_info)) {
  1598. if (net_ratelimit())
  1599. netdev_err(dev, "tx ring full when queue awake\n");
  1600. return NETDEV_TX_BUSY;
  1601. }
  1602. rocker_desc_cookie_ptr_set(desc_info, skb);
  1603. frags = rocker_tlv_nest_start(desc_info, ROCKER_TLV_TX_FRAGS);
  1604. if (!frags)
  1605. goto out;
  1606. err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
  1607. skb->data, skb_headlen(skb));
  1608. if (err)
  1609. goto nest_cancel;
  1610. if (skb_shinfo(skb)->nr_frags > ROCKER_TX_FRAGS_MAX) {
  1611. err = skb_linearize(skb);
  1612. if (err)
  1613. goto unmap_frags;
  1614. }
  1615. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1616. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1617. err = rocker_tx_desc_frag_map_put(rocker_port, desc_info,
  1618. skb_frag_address(frag),
  1619. skb_frag_size(frag));
  1620. if (err)
  1621. goto unmap_frags;
  1622. }
  1623. rocker_tlv_nest_end(desc_info, frags);
  1624. rocker_desc_gen_clear(desc_info);
  1625. rocker_desc_head_set(rocker, &rocker_port->tx_ring, desc_info);
  1626. desc_info = rocker_desc_head_get(&rocker_port->tx_ring);
  1627. if (!desc_info)
  1628. netif_stop_queue(dev);
  1629. return NETDEV_TX_OK;
  1630. unmap_frags:
  1631. rocker_tx_desc_frags_unmap(rocker_port, desc_info);
  1632. nest_cancel:
  1633. rocker_tlv_nest_cancel(desc_info, frags);
  1634. out:
  1635. dev_kfree_skb(skb);
  1636. dev->stats.tx_dropped++;
  1637. return NETDEV_TX_OK;
  1638. }
  1639. static int rocker_port_set_mac_address(struct net_device *dev, void *p)
  1640. {
  1641. struct sockaddr *addr = p;
  1642. struct rocker_port *rocker_port = netdev_priv(dev);
  1643. int err;
  1644. if (!is_valid_ether_addr(addr->sa_data))
  1645. return -EADDRNOTAVAIL;
  1646. err = rocker_cmd_set_port_settings_macaddr(rocker_port, addr->sa_data);
  1647. if (err)
  1648. return err;
  1649. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1650. return 0;
  1651. }
  1652. static int rocker_port_change_mtu(struct net_device *dev, int new_mtu)
  1653. {
  1654. struct rocker_port *rocker_port = netdev_priv(dev);
  1655. int running = netif_running(dev);
  1656. int err;
  1657. if (running)
  1658. rocker_port_stop(dev);
  1659. netdev_info(dev, "MTU change from %d to %d\n", dev->mtu, new_mtu);
  1660. dev->mtu = new_mtu;
  1661. err = rocker_cmd_set_port_settings_mtu(rocker_port, new_mtu);
  1662. if (err)
  1663. return err;
  1664. if (running)
  1665. err = rocker_port_open(dev);
  1666. return err;
  1667. }
  1668. static int rocker_port_get_phys_port_name(struct net_device *dev,
  1669. char *buf, size_t len)
  1670. {
  1671. struct rocker_port *rocker_port = netdev_priv(dev);
  1672. struct port_name name = { .buf = buf, .len = len };
  1673. int err;
  1674. err = rocker_cmd_exec(rocker_port, false,
  1675. rocker_cmd_get_port_settings_prep, NULL,
  1676. rocker_cmd_get_port_settings_phys_name_proc,
  1677. &name);
  1678. return err ? -EOPNOTSUPP : 0;
  1679. }
  1680. static int rocker_port_change_proto_down(struct net_device *dev,
  1681. bool proto_down)
  1682. {
  1683. struct rocker_port *rocker_port = netdev_priv(dev);
  1684. if (rocker_port->dev->flags & IFF_UP)
  1685. rocker_port_set_enable(rocker_port, !proto_down);
  1686. rocker_port->dev->proto_down = proto_down;
  1687. return 0;
  1688. }
  1689. static void rocker_port_neigh_destroy(struct net_device *dev,
  1690. struct neighbour *n)
  1691. {
  1692. struct rocker_port *rocker_port = netdev_priv(n->dev);
  1693. int err;
  1694. err = rocker_world_port_neigh_destroy(rocker_port, n);
  1695. if (err)
  1696. netdev_warn(rocker_port->dev, "failed to handle neigh destroy (err %d)\n",
  1697. err);
  1698. }
  1699. static const struct net_device_ops rocker_port_netdev_ops = {
  1700. .ndo_open = rocker_port_open,
  1701. .ndo_stop = rocker_port_stop,
  1702. .ndo_start_xmit = rocker_port_xmit,
  1703. .ndo_set_mac_address = rocker_port_set_mac_address,
  1704. .ndo_change_mtu = rocker_port_change_mtu,
  1705. .ndo_get_phys_port_name = rocker_port_get_phys_port_name,
  1706. .ndo_change_proto_down = rocker_port_change_proto_down,
  1707. .ndo_neigh_destroy = rocker_port_neigh_destroy,
  1708. };
  1709. /********************
  1710. * swdev interface
  1711. ********************/
  1712. static int rocker_port_attr_get(struct net_device *dev,
  1713. struct switchdev_attr *attr)
  1714. {
  1715. const struct rocker_port *rocker_port = netdev_priv(dev);
  1716. const struct rocker *rocker = rocker_port->rocker;
  1717. int err = 0;
  1718. switch (attr->id) {
  1719. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  1720. attr->u.ppid.id_len = sizeof(rocker->hw.id);
  1721. memcpy(&attr->u.ppid.id, &rocker->hw.id, attr->u.ppid.id_len);
  1722. break;
  1723. case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
  1724. err = rocker_world_port_attr_bridge_flags_get(rocker_port,
  1725. &attr->u.brport_flags);
  1726. break;
  1727. case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT:
  1728. err = rocker_world_port_attr_bridge_flags_support_get(rocker_port,
  1729. &attr->u.brport_flags_support);
  1730. break;
  1731. default:
  1732. return -EOPNOTSUPP;
  1733. }
  1734. return err;
  1735. }
  1736. static int rocker_port_attr_set(struct net_device *dev,
  1737. const struct switchdev_attr *attr,
  1738. struct switchdev_trans *trans)
  1739. {
  1740. struct rocker_port *rocker_port = netdev_priv(dev);
  1741. int err = 0;
  1742. switch (attr->id) {
  1743. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  1744. err = rocker_world_port_attr_stp_state_set(rocker_port,
  1745. attr->u.stp_state,
  1746. trans);
  1747. break;
  1748. case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
  1749. err = rocker_world_port_attr_bridge_flags_set(rocker_port,
  1750. attr->u.brport_flags,
  1751. trans);
  1752. break;
  1753. case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
  1754. err = rocker_world_port_attr_bridge_ageing_time_set(rocker_port,
  1755. attr->u.ageing_time,
  1756. trans);
  1757. break;
  1758. default:
  1759. err = -EOPNOTSUPP;
  1760. break;
  1761. }
  1762. return err;
  1763. }
  1764. static int rocker_port_obj_add(struct net_device *dev,
  1765. const struct switchdev_obj *obj,
  1766. struct switchdev_trans *trans)
  1767. {
  1768. struct rocker_port *rocker_port = netdev_priv(dev);
  1769. int err = 0;
  1770. switch (obj->id) {
  1771. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1772. err = rocker_world_port_obj_vlan_add(rocker_port,
  1773. SWITCHDEV_OBJ_PORT_VLAN(obj),
  1774. trans);
  1775. break;
  1776. default:
  1777. err = -EOPNOTSUPP;
  1778. break;
  1779. }
  1780. return err;
  1781. }
  1782. static int rocker_port_obj_del(struct net_device *dev,
  1783. const struct switchdev_obj *obj)
  1784. {
  1785. struct rocker_port *rocker_port = netdev_priv(dev);
  1786. int err = 0;
  1787. switch (obj->id) {
  1788. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  1789. err = rocker_world_port_obj_vlan_del(rocker_port,
  1790. SWITCHDEV_OBJ_PORT_VLAN(obj));
  1791. break;
  1792. default:
  1793. err = -EOPNOTSUPP;
  1794. break;
  1795. }
  1796. return err;
  1797. }
  1798. static const struct switchdev_ops rocker_port_switchdev_ops = {
  1799. .switchdev_port_attr_get = rocker_port_attr_get,
  1800. .switchdev_port_attr_set = rocker_port_attr_set,
  1801. .switchdev_port_obj_add = rocker_port_obj_add,
  1802. .switchdev_port_obj_del = rocker_port_obj_del,
  1803. };
  1804. struct rocker_fib_event_work {
  1805. struct work_struct work;
  1806. union {
  1807. struct fib_entry_notifier_info fen_info;
  1808. struct fib_rule_notifier_info fr_info;
  1809. };
  1810. struct rocker *rocker;
  1811. unsigned long event;
  1812. };
  1813. static void rocker_router_fib_event_work(struct work_struct *work)
  1814. {
  1815. struct rocker_fib_event_work *fib_work =
  1816. container_of(work, struct rocker_fib_event_work, work);
  1817. struct rocker *rocker = fib_work->rocker;
  1818. struct fib_rule *rule;
  1819. int err;
  1820. /* Protect internal structures from changes */
  1821. rtnl_lock();
  1822. switch (fib_work->event) {
  1823. case FIB_EVENT_ENTRY_ADD:
  1824. err = rocker_world_fib4_add(rocker, &fib_work->fen_info);
  1825. if (err)
  1826. rocker_world_fib4_abort(rocker);
  1827. fib_info_put(fib_work->fen_info.fi);
  1828. break;
  1829. case FIB_EVENT_ENTRY_DEL:
  1830. rocker_world_fib4_del(rocker, &fib_work->fen_info);
  1831. fib_info_put(fib_work->fen_info.fi);
  1832. break;
  1833. case FIB_EVENT_RULE_ADD: /* fall through */
  1834. case FIB_EVENT_RULE_DEL:
  1835. rule = fib_work->fr_info.rule;
  1836. if (!fib4_rule_default(rule))
  1837. rocker_world_fib4_abort(rocker);
  1838. fib_rule_put(rule);
  1839. break;
  1840. }
  1841. rtnl_unlock();
  1842. kfree(fib_work);
  1843. }
  1844. /* Called with rcu_read_lock() */
  1845. static int rocker_router_fib_event(struct notifier_block *nb,
  1846. unsigned long event, void *ptr)
  1847. {
  1848. struct rocker *rocker = container_of(nb, struct rocker, fib_nb);
  1849. struct rocker_fib_event_work *fib_work;
  1850. struct fib_notifier_info *info = ptr;
  1851. if (info->family != AF_INET)
  1852. return NOTIFY_DONE;
  1853. fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
  1854. if (WARN_ON(!fib_work))
  1855. return NOTIFY_BAD;
  1856. INIT_WORK(&fib_work->work, rocker_router_fib_event_work);
  1857. fib_work->rocker = rocker;
  1858. fib_work->event = event;
  1859. switch (event) {
  1860. case FIB_EVENT_ENTRY_ADD: /* fall through */
  1861. case FIB_EVENT_ENTRY_DEL:
  1862. memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
  1863. /* Take referece on fib_info to prevent it from being
  1864. * freed while work is queued. Release it afterwards.
  1865. */
  1866. fib_info_hold(fib_work->fen_info.fi);
  1867. break;
  1868. case FIB_EVENT_RULE_ADD: /* fall through */
  1869. case FIB_EVENT_RULE_DEL:
  1870. memcpy(&fib_work->fr_info, ptr, sizeof(fib_work->fr_info));
  1871. fib_rule_get(fib_work->fr_info.rule);
  1872. break;
  1873. }
  1874. queue_work(rocker->rocker_owq, &fib_work->work);
  1875. return NOTIFY_DONE;
  1876. }
  1877. /********************
  1878. * ethtool interface
  1879. ********************/
  1880. static int
  1881. rocker_port_get_link_ksettings(struct net_device *dev,
  1882. struct ethtool_link_ksettings *ecmd)
  1883. {
  1884. struct rocker_port *rocker_port = netdev_priv(dev);
  1885. return rocker_cmd_get_port_settings_ethtool(rocker_port, ecmd);
  1886. }
  1887. static int
  1888. rocker_port_set_link_ksettings(struct net_device *dev,
  1889. const struct ethtool_link_ksettings *ecmd)
  1890. {
  1891. struct rocker_port *rocker_port = netdev_priv(dev);
  1892. return rocker_cmd_set_port_settings_ethtool(rocker_port, ecmd);
  1893. }
  1894. static void rocker_port_get_drvinfo(struct net_device *dev,
  1895. struct ethtool_drvinfo *drvinfo)
  1896. {
  1897. strlcpy(drvinfo->driver, rocker_driver_name, sizeof(drvinfo->driver));
  1898. strlcpy(drvinfo->version, UTS_RELEASE, sizeof(drvinfo->version));
  1899. }
  1900. static struct rocker_port_stats {
  1901. char str[ETH_GSTRING_LEN];
  1902. int type;
  1903. } rocker_port_stats[] = {
  1904. { "rx_packets", ROCKER_TLV_CMD_PORT_STATS_RX_PKTS, },
  1905. { "rx_bytes", ROCKER_TLV_CMD_PORT_STATS_RX_BYTES, },
  1906. { "rx_dropped", ROCKER_TLV_CMD_PORT_STATS_RX_DROPPED, },
  1907. { "rx_errors", ROCKER_TLV_CMD_PORT_STATS_RX_ERRORS, },
  1908. { "tx_packets", ROCKER_TLV_CMD_PORT_STATS_TX_PKTS, },
  1909. { "tx_bytes", ROCKER_TLV_CMD_PORT_STATS_TX_BYTES, },
  1910. { "tx_dropped", ROCKER_TLV_CMD_PORT_STATS_TX_DROPPED, },
  1911. { "tx_errors", ROCKER_TLV_CMD_PORT_STATS_TX_ERRORS, },
  1912. };
  1913. #define ROCKER_PORT_STATS_LEN ARRAY_SIZE(rocker_port_stats)
  1914. static void rocker_port_get_strings(struct net_device *netdev, u32 stringset,
  1915. u8 *data)
  1916. {
  1917. u8 *p = data;
  1918. int i;
  1919. switch (stringset) {
  1920. case ETH_SS_STATS:
  1921. for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
  1922. memcpy(p, rocker_port_stats[i].str, ETH_GSTRING_LEN);
  1923. p += ETH_GSTRING_LEN;
  1924. }
  1925. break;
  1926. }
  1927. }
  1928. static int
  1929. rocker_cmd_get_port_stats_prep(const struct rocker_port *rocker_port,
  1930. struct rocker_desc_info *desc_info,
  1931. void *priv)
  1932. {
  1933. struct rocker_tlv *cmd_stats;
  1934. if (rocker_tlv_put_u16(desc_info, ROCKER_TLV_CMD_TYPE,
  1935. ROCKER_TLV_CMD_TYPE_GET_PORT_STATS))
  1936. return -EMSGSIZE;
  1937. cmd_stats = rocker_tlv_nest_start(desc_info, ROCKER_TLV_CMD_INFO);
  1938. if (!cmd_stats)
  1939. return -EMSGSIZE;
  1940. if (rocker_tlv_put_u32(desc_info, ROCKER_TLV_CMD_PORT_STATS_PPORT,
  1941. rocker_port->pport))
  1942. return -EMSGSIZE;
  1943. rocker_tlv_nest_end(desc_info, cmd_stats);
  1944. return 0;
  1945. }
  1946. static int
  1947. rocker_cmd_get_port_stats_ethtool_proc(const struct rocker_port *rocker_port,
  1948. const struct rocker_desc_info *desc_info,
  1949. void *priv)
  1950. {
  1951. const struct rocker_tlv *attrs[ROCKER_TLV_CMD_MAX + 1];
  1952. const struct rocker_tlv *stats_attrs[ROCKER_TLV_CMD_PORT_STATS_MAX + 1];
  1953. const struct rocker_tlv *pattr;
  1954. u32 pport;
  1955. u64 *data = priv;
  1956. int i;
  1957. rocker_tlv_parse_desc(attrs, ROCKER_TLV_CMD_MAX, desc_info);
  1958. if (!attrs[ROCKER_TLV_CMD_INFO])
  1959. return -EIO;
  1960. rocker_tlv_parse_nested(stats_attrs, ROCKER_TLV_CMD_PORT_STATS_MAX,
  1961. attrs[ROCKER_TLV_CMD_INFO]);
  1962. if (!stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT])
  1963. return -EIO;
  1964. pport = rocker_tlv_get_u32(stats_attrs[ROCKER_TLV_CMD_PORT_STATS_PPORT]);
  1965. if (pport != rocker_port->pport)
  1966. return -EIO;
  1967. for (i = 0; i < ARRAY_SIZE(rocker_port_stats); i++) {
  1968. pattr = stats_attrs[rocker_port_stats[i].type];
  1969. if (!pattr)
  1970. continue;
  1971. data[i] = rocker_tlv_get_u64(pattr);
  1972. }
  1973. return 0;
  1974. }
  1975. static int rocker_cmd_get_port_stats_ethtool(struct rocker_port *rocker_port,
  1976. void *priv)
  1977. {
  1978. return rocker_cmd_exec(rocker_port, false,
  1979. rocker_cmd_get_port_stats_prep, NULL,
  1980. rocker_cmd_get_port_stats_ethtool_proc,
  1981. priv);
  1982. }
  1983. static void rocker_port_get_stats(struct net_device *dev,
  1984. struct ethtool_stats *stats, u64 *data)
  1985. {
  1986. struct rocker_port *rocker_port = netdev_priv(dev);
  1987. if (rocker_cmd_get_port_stats_ethtool(rocker_port, data) != 0) {
  1988. int i;
  1989. for (i = 0; i < ARRAY_SIZE(rocker_port_stats); ++i)
  1990. data[i] = 0;
  1991. }
  1992. }
  1993. static int rocker_port_get_sset_count(struct net_device *netdev, int sset)
  1994. {
  1995. switch (sset) {
  1996. case ETH_SS_STATS:
  1997. return ROCKER_PORT_STATS_LEN;
  1998. default:
  1999. return -EOPNOTSUPP;
  2000. }
  2001. }
  2002. static const struct ethtool_ops rocker_port_ethtool_ops = {
  2003. .get_drvinfo = rocker_port_get_drvinfo,
  2004. .get_link = ethtool_op_get_link,
  2005. .get_strings = rocker_port_get_strings,
  2006. .get_ethtool_stats = rocker_port_get_stats,
  2007. .get_sset_count = rocker_port_get_sset_count,
  2008. .get_link_ksettings = rocker_port_get_link_ksettings,
  2009. .set_link_ksettings = rocker_port_set_link_ksettings,
  2010. };
  2011. /*****************
  2012. * NAPI interface
  2013. *****************/
  2014. static struct rocker_port *rocker_port_napi_tx_get(struct napi_struct *napi)
  2015. {
  2016. return container_of(napi, struct rocker_port, napi_tx);
  2017. }
  2018. static int rocker_port_poll_tx(struct napi_struct *napi, int budget)
  2019. {
  2020. struct rocker_port *rocker_port = rocker_port_napi_tx_get(napi);
  2021. const struct rocker *rocker = rocker_port->rocker;
  2022. const struct rocker_desc_info *desc_info;
  2023. u32 credits = 0;
  2024. int err;
  2025. /* Cleanup tx descriptors */
  2026. while ((desc_info = rocker_desc_tail_get(&rocker_port->tx_ring))) {
  2027. struct sk_buff *skb;
  2028. err = rocker_desc_err(desc_info);
  2029. if (err && net_ratelimit())
  2030. netdev_err(rocker_port->dev, "tx desc received with err %d\n",
  2031. err);
  2032. rocker_tx_desc_frags_unmap(rocker_port, desc_info);
  2033. skb = rocker_desc_cookie_ptr_get(desc_info);
  2034. if (err == 0) {
  2035. rocker_port->dev->stats.tx_packets++;
  2036. rocker_port->dev->stats.tx_bytes += skb->len;
  2037. } else {
  2038. rocker_port->dev->stats.tx_errors++;
  2039. }
  2040. dev_kfree_skb_any(skb);
  2041. credits++;
  2042. }
  2043. if (credits && netif_queue_stopped(rocker_port->dev))
  2044. netif_wake_queue(rocker_port->dev);
  2045. napi_complete(napi);
  2046. rocker_dma_ring_credits_set(rocker, &rocker_port->tx_ring, credits);
  2047. return 0;
  2048. }
  2049. static int rocker_port_rx_proc(const struct rocker *rocker,
  2050. const struct rocker_port *rocker_port,
  2051. struct rocker_desc_info *desc_info)
  2052. {
  2053. const struct rocker_tlv *attrs[ROCKER_TLV_RX_MAX + 1];
  2054. struct sk_buff *skb = rocker_desc_cookie_ptr_get(desc_info);
  2055. size_t rx_len;
  2056. u16 rx_flags = 0;
  2057. if (!skb)
  2058. return -ENOENT;
  2059. rocker_tlv_parse_desc(attrs, ROCKER_TLV_RX_MAX, desc_info);
  2060. if (!attrs[ROCKER_TLV_RX_FRAG_LEN])
  2061. return -EINVAL;
  2062. if (attrs[ROCKER_TLV_RX_FLAGS])
  2063. rx_flags = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FLAGS]);
  2064. rocker_dma_rx_ring_skb_unmap(rocker, attrs);
  2065. rx_len = rocker_tlv_get_u16(attrs[ROCKER_TLV_RX_FRAG_LEN]);
  2066. skb_put(skb, rx_len);
  2067. skb->protocol = eth_type_trans(skb, rocker_port->dev);
  2068. if (rx_flags & ROCKER_RX_FLAGS_FWD_OFFLOAD)
  2069. skb->offload_fwd_mark = 1;
  2070. rocker_port->dev->stats.rx_packets++;
  2071. rocker_port->dev->stats.rx_bytes += skb->len;
  2072. netif_receive_skb(skb);
  2073. return rocker_dma_rx_ring_skb_alloc(rocker_port, desc_info);
  2074. }
  2075. static struct rocker_port *rocker_port_napi_rx_get(struct napi_struct *napi)
  2076. {
  2077. return container_of(napi, struct rocker_port, napi_rx);
  2078. }
  2079. static int rocker_port_poll_rx(struct napi_struct *napi, int budget)
  2080. {
  2081. struct rocker_port *rocker_port = rocker_port_napi_rx_get(napi);
  2082. const struct rocker *rocker = rocker_port->rocker;
  2083. struct rocker_desc_info *desc_info;
  2084. u32 credits = 0;
  2085. int err;
  2086. /* Process rx descriptors */
  2087. while (credits < budget &&
  2088. (desc_info = rocker_desc_tail_get(&rocker_port->rx_ring))) {
  2089. err = rocker_desc_err(desc_info);
  2090. if (err) {
  2091. if (net_ratelimit())
  2092. netdev_err(rocker_port->dev, "rx desc received with err %d\n",
  2093. err);
  2094. } else {
  2095. err = rocker_port_rx_proc(rocker, rocker_port,
  2096. desc_info);
  2097. if (err && net_ratelimit())
  2098. netdev_err(rocker_port->dev, "rx processing failed with err %d\n",
  2099. err);
  2100. }
  2101. if (err)
  2102. rocker_port->dev->stats.rx_errors++;
  2103. rocker_desc_gen_clear(desc_info);
  2104. rocker_desc_head_set(rocker, &rocker_port->rx_ring, desc_info);
  2105. credits++;
  2106. }
  2107. if (credits < budget)
  2108. napi_complete_done(napi, credits);
  2109. rocker_dma_ring_credits_set(rocker, &rocker_port->rx_ring, credits);
  2110. return credits;
  2111. }
  2112. /*****************
  2113. * PCI driver ops
  2114. *****************/
  2115. static void rocker_carrier_init(const struct rocker_port *rocker_port)
  2116. {
  2117. const struct rocker *rocker = rocker_port->rocker;
  2118. u64 link_status = rocker_read64(rocker, PORT_PHYS_LINK_STATUS);
  2119. bool link_up;
  2120. link_up = link_status & (1 << rocker_port->pport);
  2121. if (link_up)
  2122. netif_carrier_on(rocker_port->dev);
  2123. else
  2124. netif_carrier_off(rocker_port->dev);
  2125. }
  2126. static void rocker_remove_ports(struct rocker *rocker)
  2127. {
  2128. struct rocker_port *rocker_port;
  2129. int i;
  2130. for (i = 0; i < rocker->port_count; i++) {
  2131. rocker_port = rocker->ports[i];
  2132. if (!rocker_port)
  2133. continue;
  2134. rocker_world_port_fini(rocker_port);
  2135. unregister_netdev(rocker_port->dev);
  2136. rocker_world_port_post_fini(rocker_port);
  2137. free_netdev(rocker_port->dev);
  2138. }
  2139. rocker_world_fini(rocker);
  2140. kfree(rocker->ports);
  2141. }
  2142. static void rocker_port_dev_addr_init(struct rocker_port *rocker_port)
  2143. {
  2144. const struct rocker *rocker = rocker_port->rocker;
  2145. const struct pci_dev *pdev = rocker->pdev;
  2146. int err;
  2147. err = rocker_cmd_get_port_settings_macaddr(rocker_port,
  2148. rocker_port->dev->dev_addr);
  2149. if (err) {
  2150. dev_warn(&pdev->dev, "failed to get mac address, using random\n");
  2151. eth_hw_addr_random(rocker_port->dev);
  2152. }
  2153. }
  2154. #define ROCKER_PORT_MIN_MTU ETH_MIN_MTU
  2155. #define ROCKER_PORT_MAX_MTU 9000
  2156. static int rocker_probe_port(struct rocker *rocker, unsigned int port_number)
  2157. {
  2158. struct pci_dev *pdev = rocker->pdev;
  2159. struct rocker_port *rocker_port;
  2160. struct net_device *dev;
  2161. int err;
  2162. dev = alloc_etherdev(sizeof(struct rocker_port));
  2163. if (!dev)
  2164. return -ENOMEM;
  2165. SET_NETDEV_DEV(dev, &pdev->dev);
  2166. rocker_port = netdev_priv(dev);
  2167. rocker_port->dev = dev;
  2168. rocker_port->rocker = rocker;
  2169. rocker_port->port_number = port_number;
  2170. rocker_port->pport = port_number + 1;
  2171. err = rocker_world_check_init(rocker_port);
  2172. if (err) {
  2173. dev_err(&pdev->dev, "world init failed\n");
  2174. goto err_world_check_init;
  2175. }
  2176. rocker_port_dev_addr_init(rocker_port);
  2177. dev->netdev_ops = &rocker_port_netdev_ops;
  2178. dev->ethtool_ops = &rocker_port_ethtool_ops;
  2179. dev->switchdev_ops = &rocker_port_switchdev_ops;
  2180. netif_tx_napi_add(dev, &rocker_port->napi_tx, rocker_port_poll_tx,
  2181. NAPI_POLL_WEIGHT);
  2182. netif_napi_add(dev, &rocker_port->napi_rx, rocker_port_poll_rx,
  2183. NAPI_POLL_WEIGHT);
  2184. rocker_carrier_init(rocker_port);
  2185. dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_SG;
  2186. /* MTU range: 68 - 9000 */
  2187. dev->min_mtu = ROCKER_PORT_MIN_MTU;
  2188. dev->max_mtu = ROCKER_PORT_MAX_MTU;
  2189. err = rocker_world_port_pre_init(rocker_port);
  2190. if (err) {
  2191. dev_err(&pdev->dev, "port world pre-init failed\n");
  2192. goto err_world_port_pre_init;
  2193. }
  2194. err = register_netdev(dev);
  2195. if (err) {
  2196. dev_err(&pdev->dev, "register_netdev failed\n");
  2197. goto err_register_netdev;
  2198. }
  2199. rocker->ports[port_number] = rocker_port;
  2200. err = rocker_world_port_init(rocker_port);
  2201. if (err) {
  2202. dev_err(&pdev->dev, "port world init failed\n");
  2203. goto err_world_port_init;
  2204. }
  2205. return 0;
  2206. err_world_port_init:
  2207. rocker->ports[port_number] = NULL;
  2208. unregister_netdev(dev);
  2209. err_register_netdev:
  2210. rocker_world_port_post_fini(rocker_port);
  2211. err_world_port_pre_init:
  2212. err_world_check_init:
  2213. free_netdev(dev);
  2214. return err;
  2215. }
  2216. static int rocker_probe_ports(struct rocker *rocker)
  2217. {
  2218. int i;
  2219. size_t alloc_size;
  2220. int err;
  2221. alloc_size = sizeof(struct rocker_port *) * rocker->port_count;
  2222. rocker->ports = kzalloc(alloc_size, GFP_KERNEL);
  2223. if (!rocker->ports)
  2224. return -ENOMEM;
  2225. for (i = 0; i < rocker->port_count; i++) {
  2226. err = rocker_probe_port(rocker, i);
  2227. if (err)
  2228. goto remove_ports;
  2229. }
  2230. return 0;
  2231. remove_ports:
  2232. rocker_remove_ports(rocker);
  2233. return err;
  2234. }
  2235. static int rocker_msix_init(struct rocker *rocker)
  2236. {
  2237. struct pci_dev *pdev = rocker->pdev;
  2238. int msix_entries;
  2239. int i;
  2240. int err;
  2241. msix_entries = pci_msix_vec_count(pdev);
  2242. if (msix_entries < 0)
  2243. return msix_entries;
  2244. if (msix_entries != ROCKER_MSIX_VEC_COUNT(rocker->port_count))
  2245. return -EINVAL;
  2246. rocker->msix_entries = kmalloc_array(msix_entries,
  2247. sizeof(struct msix_entry),
  2248. GFP_KERNEL);
  2249. if (!rocker->msix_entries)
  2250. return -ENOMEM;
  2251. for (i = 0; i < msix_entries; i++)
  2252. rocker->msix_entries[i].entry = i;
  2253. err = pci_enable_msix_exact(pdev, rocker->msix_entries, msix_entries);
  2254. if (err < 0)
  2255. goto err_enable_msix;
  2256. return 0;
  2257. err_enable_msix:
  2258. kfree(rocker->msix_entries);
  2259. return err;
  2260. }
  2261. static void rocker_msix_fini(const struct rocker *rocker)
  2262. {
  2263. pci_disable_msix(rocker->pdev);
  2264. kfree(rocker->msix_entries);
  2265. }
  2266. static bool rocker_port_dev_check(const struct net_device *dev)
  2267. {
  2268. return dev->netdev_ops == &rocker_port_netdev_ops;
  2269. }
  2270. struct rocker_switchdev_event_work {
  2271. struct work_struct work;
  2272. struct switchdev_notifier_fdb_info fdb_info;
  2273. struct rocker_port *rocker_port;
  2274. unsigned long event;
  2275. };
  2276. static void
  2277. rocker_fdb_offload_notify(struct rocker_port *rocker_port,
  2278. struct switchdev_notifier_fdb_info *recv_info)
  2279. {
  2280. struct switchdev_notifier_fdb_info info;
  2281. info.addr = recv_info->addr;
  2282. info.vid = recv_info->vid;
  2283. call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED,
  2284. rocker_port->dev, &info.info);
  2285. }
  2286. static void rocker_switchdev_event_work(struct work_struct *work)
  2287. {
  2288. struct rocker_switchdev_event_work *switchdev_work =
  2289. container_of(work, struct rocker_switchdev_event_work, work);
  2290. struct rocker_port *rocker_port = switchdev_work->rocker_port;
  2291. struct switchdev_notifier_fdb_info *fdb_info;
  2292. int err;
  2293. rtnl_lock();
  2294. switch (switchdev_work->event) {
  2295. case SWITCHDEV_FDB_ADD_TO_DEVICE:
  2296. fdb_info = &switchdev_work->fdb_info;
  2297. if (!fdb_info->added_by_user)
  2298. break;
  2299. err = rocker_world_port_fdb_add(rocker_port, fdb_info);
  2300. if (err) {
  2301. netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
  2302. break;
  2303. }
  2304. rocker_fdb_offload_notify(rocker_port, fdb_info);
  2305. break;
  2306. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  2307. fdb_info = &switchdev_work->fdb_info;
  2308. if (!fdb_info->added_by_user)
  2309. break;
  2310. err = rocker_world_port_fdb_del(rocker_port, fdb_info);
  2311. if (err)
  2312. netdev_dbg(rocker_port->dev, "fdb add failed err=%d\n", err);
  2313. break;
  2314. }
  2315. rtnl_unlock();
  2316. kfree(switchdev_work->fdb_info.addr);
  2317. kfree(switchdev_work);
  2318. dev_put(rocker_port->dev);
  2319. }
  2320. /* called under rcu_read_lock() */
  2321. static int rocker_switchdev_event(struct notifier_block *unused,
  2322. unsigned long event, void *ptr)
  2323. {
  2324. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  2325. struct rocker_switchdev_event_work *switchdev_work;
  2326. struct switchdev_notifier_fdb_info *fdb_info = ptr;
  2327. struct rocker_port *rocker_port;
  2328. if (!rocker_port_dev_check(dev))
  2329. return NOTIFY_DONE;
  2330. rocker_port = netdev_priv(dev);
  2331. switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
  2332. if (WARN_ON(!switchdev_work))
  2333. return NOTIFY_BAD;
  2334. INIT_WORK(&switchdev_work->work, rocker_switchdev_event_work);
  2335. switchdev_work->rocker_port = rocker_port;
  2336. switchdev_work->event = event;
  2337. switch (event) {
  2338. case SWITCHDEV_FDB_ADD_TO_DEVICE: /* fall through */
  2339. case SWITCHDEV_FDB_DEL_TO_DEVICE:
  2340. memcpy(&switchdev_work->fdb_info, ptr,
  2341. sizeof(switchdev_work->fdb_info));
  2342. switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
  2343. ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
  2344. fdb_info->addr);
  2345. /* Take a reference on the rocker device */
  2346. dev_hold(dev);
  2347. break;
  2348. default:
  2349. kfree(switchdev_work);
  2350. return NOTIFY_DONE;
  2351. }
  2352. queue_work(rocker_port->rocker->rocker_owq,
  2353. &switchdev_work->work);
  2354. return NOTIFY_DONE;
  2355. }
  2356. static struct notifier_block rocker_switchdev_notifier = {
  2357. .notifier_call = rocker_switchdev_event,
  2358. };
  2359. static int rocker_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2360. {
  2361. struct rocker *rocker;
  2362. int err;
  2363. rocker = kzalloc(sizeof(*rocker), GFP_KERNEL);
  2364. if (!rocker)
  2365. return -ENOMEM;
  2366. err = pci_enable_device(pdev);
  2367. if (err) {
  2368. dev_err(&pdev->dev, "pci_enable_device failed\n");
  2369. goto err_pci_enable_device;
  2370. }
  2371. err = pci_request_regions(pdev, rocker_driver_name);
  2372. if (err) {
  2373. dev_err(&pdev->dev, "pci_request_regions failed\n");
  2374. goto err_pci_request_regions;
  2375. }
  2376. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  2377. if (!err) {
  2378. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  2379. if (err) {
  2380. dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
  2381. goto err_pci_set_dma_mask;
  2382. }
  2383. } else {
  2384. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2385. if (err) {
  2386. dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
  2387. goto err_pci_set_dma_mask;
  2388. }
  2389. }
  2390. if (pci_resource_len(pdev, 0) < ROCKER_PCI_BAR0_SIZE) {
  2391. dev_err(&pdev->dev, "invalid PCI region size\n");
  2392. err = -EINVAL;
  2393. goto err_pci_resource_len_check;
  2394. }
  2395. rocker->hw_addr = ioremap(pci_resource_start(pdev, 0),
  2396. pci_resource_len(pdev, 0));
  2397. if (!rocker->hw_addr) {
  2398. dev_err(&pdev->dev, "ioremap failed\n");
  2399. err = -EIO;
  2400. goto err_ioremap;
  2401. }
  2402. pci_set_master(pdev);
  2403. rocker->pdev = pdev;
  2404. pci_set_drvdata(pdev, rocker);
  2405. rocker->port_count = rocker_read32(rocker, PORT_PHYS_COUNT);
  2406. err = rocker_msix_init(rocker);
  2407. if (err) {
  2408. dev_err(&pdev->dev, "MSI-X init failed\n");
  2409. goto err_msix_init;
  2410. }
  2411. err = rocker_basic_hw_test(rocker);
  2412. if (err) {
  2413. dev_err(&pdev->dev, "basic hw test failed\n");
  2414. goto err_basic_hw_test;
  2415. }
  2416. rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
  2417. err = rocker_dma_rings_init(rocker);
  2418. if (err)
  2419. goto err_dma_rings_init;
  2420. err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD),
  2421. rocker_cmd_irq_handler, 0,
  2422. rocker_driver_name, rocker);
  2423. if (err) {
  2424. dev_err(&pdev->dev, "cannot assign cmd irq\n");
  2425. goto err_request_cmd_irq;
  2426. }
  2427. err = request_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT),
  2428. rocker_event_irq_handler, 0,
  2429. rocker_driver_name, rocker);
  2430. if (err) {
  2431. dev_err(&pdev->dev, "cannot assign event irq\n");
  2432. goto err_request_event_irq;
  2433. }
  2434. rocker->rocker_owq = alloc_ordered_workqueue(rocker_driver_name,
  2435. WQ_MEM_RECLAIM);
  2436. if (!rocker->rocker_owq) {
  2437. err = -ENOMEM;
  2438. goto err_alloc_ordered_workqueue;
  2439. }
  2440. err = rocker_probe_ports(rocker);
  2441. if (err) {
  2442. dev_err(&pdev->dev, "failed to probe ports\n");
  2443. goto err_probe_ports;
  2444. }
  2445. /* Only FIBs pointing to our own netdevs are programmed into
  2446. * the device, so no need to pass a callback.
  2447. */
  2448. rocker->fib_nb.notifier_call = rocker_router_fib_event;
  2449. err = register_fib_notifier(&rocker->fib_nb, NULL);
  2450. if (err)
  2451. goto err_register_fib_notifier;
  2452. err = register_switchdev_notifier(&rocker_switchdev_notifier);
  2453. if (err) {
  2454. dev_err(&pdev->dev, "Failed to register switchdev notifier\n");
  2455. goto err_register_switchdev_notifier;
  2456. }
  2457. rocker->hw.id = rocker_read64(rocker, SWITCH_ID);
  2458. dev_info(&pdev->dev, "Rocker switch with id %*phN\n",
  2459. (int)sizeof(rocker->hw.id), &rocker->hw.id);
  2460. return 0;
  2461. err_register_switchdev_notifier:
  2462. unregister_fib_notifier(&rocker->fib_nb);
  2463. err_register_fib_notifier:
  2464. rocker_remove_ports(rocker);
  2465. err_probe_ports:
  2466. destroy_workqueue(rocker->rocker_owq);
  2467. err_alloc_ordered_workqueue:
  2468. free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
  2469. err_request_event_irq:
  2470. free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
  2471. err_request_cmd_irq:
  2472. rocker_dma_rings_fini(rocker);
  2473. err_dma_rings_init:
  2474. err_basic_hw_test:
  2475. rocker_msix_fini(rocker);
  2476. err_msix_init:
  2477. iounmap(rocker->hw_addr);
  2478. err_ioremap:
  2479. err_pci_resource_len_check:
  2480. err_pci_set_dma_mask:
  2481. pci_release_regions(pdev);
  2482. err_pci_request_regions:
  2483. pci_disable_device(pdev);
  2484. err_pci_enable_device:
  2485. kfree(rocker);
  2486. return err;
  2487. }
  2488. static void rocker_remove(struct pci_dev *pdev)
  2489. {
  2490. struct rocker *rocker = pci_get_drvdata(pdev);
  2491. unregister_switchdev_notifier(&rocker_switchdev_notifier);
  2492. unregister_fib_notifier(&rocker->fib_nb);
  2493. rocker_remove_ports(rocker);
  2494. rocker_write32(rocker, CONTROL, ROCKER_CONTROL_RESET);
  2495. destroy_workqueue(rocker->rocker_owq);
  2496. free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_EVENT), rocker);
  2497. free_irq(rocker_msix_vector(rocker, ROCKER_MSIX_VEC_CMD), rocker);
  2498. rocker_dma_rings_fini(rocker);
  2499. rocker_msix_fini(rocker);
  2500. iounmap(rocker->hw_addr);
  2501. pci_release_regions(rocker->pdev);
  2502. pci_disable_device(rocker->pdev);
  2503. kfree(rocker);
  2504. }
  2505. static struct pci_driver rocker_pci_driver = {
  2506. .name = rocker_driver_name,
  2507. .id_table = rocker_pci_id_table,
  2508. .probe = rocker_probe,
  2509. .remove = rocker_remove,
  2510. };
  2511. /************************************
  2512. * Net device notifier event handler
  2513. ************************************/
  2514. static bool rocker_port_dev_check_under(const struct net_device *dev,
  2515. struct rocker *rocker)
  2516. {
  2517. struct rocker_port *rocker_port;
  2518. if (!rocker_port_dev_check(dev))
  2519. return false;
  2520. rocker_port = netdev_priv(dev);
  2521. if (rocker_port->rocker != rocker)
  2522. return false;
  2523. return true;
  2524. }
  2525. struct rocker_walk_data {
  2526. struct rocker *rocker;
  2527. struct rocker_port *port;
  2528. };
  2529. static int rocker_lower_dev_walk(struct net_device *lower_dev, void *_data)
  2530. {
  2531. struct rocker_walk_data *data = _data;
  2532. int ret = 0;
  2533. if (rocker_port_dev_check_under(lower_dev, data->rocker)) {
  2534. data->port = netdev_priv(lower_dev);
  2535. ret = 1;
  2536. }
  2537. return ret;
  2538. }
  2539. struct rocker_port *rocker_port_dev_lower_find(struct net_device *dev,
  2540. struct rocker *rocker)
  2541. {
  2542. struct rocker_walk_data data;
  2543. if (rocker_port_dev_check_under(dev, rocker))
  2544. return netdev_priv(dev);
  2545. data.rocker = rocker;
  2546. data.port = NULL;
  2547. netdev_walk_all_lower_dev(dev, rocker_lower_dev_walk, &data);
  2548. return data.port;
  2549. }
  2550. static int rocker_netdevice_event(struct notifier_block *unused,
  2551. unsigned long event, void *ptr)
  2552. {
  2553. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  2554. struct netdev_notifier_changeupper_info *info;
  2555. struct rocker_port *rocker_port;
  2556. int err;
  2557. if (!rocker_port_dev_check(dev))
  2558. return NOTIFY_DONE;
  2559. switch (event) {
  2560. case NETDEV_CHANGEUPPER:
  2561. info = ptr;
  2562. if (!info->master)
  2563. goto out;
  2564. rocker_port = netdev_priv(dev);
  2565. if (info->linking) {
  2566. err = rocker_world_port_master_linked(rocker_port,
  2567. info->upper_dev);
  2568. if (err)
  2569. netdev_warn(dev, "failed to reflect master linked (err %d)\n",
  2570. err);
  2571. } else {
  2572. err = rocker_world_port_master_unlinked(rocker_port,
  2573. info->upper_dev);
  2574. if (err)
  2575. netdev_warn(dev, "failed to reflect master unlinked (err %d)\n",
  2576. err);
  2577. }
  2578. }
  2579. out:
  2580. return NOTIFY_DONE;
  2581. }
  2582. static struct notifier_block rocker_netdevice_nb __read_mostly = {
  2583. .notifier_call = rocker_netdevice_event,
  2584. };
  2585. /************************************
  2586. * Net event notifier event handler
  2587. ************************************/
  2588. static int rocker_netevent_event(struct notifier_block *unused,
  2589. unsigned long event, void *ptr)
  2590. {
  2591. struct rocker_port *rocker_port;
  2592. struct net_device *dev;
  2593. struct neighbour *n = ptr;
  2594. int err;
  2595. switch (event) {
  2596. case NETEVENT_NEIGH_UPDATE:
  2597. if (n->tbl != &arp_tbl)
  2598. return NOTIFY_DONE;
  2599. dev = n->dev;
  2600. if (!rocker_port_dev_check(dev))
  2601. return NOTIFY_DONE;
  2602. rocker_port = netdev_priv(dev);
  2603. err = rocker_world_port_neigh_update(rocker_port, n);
  2604. if (err)
  2605. netdev_warn(dev, "failed to handle neigh update (err %d)\n",
  2606. err);
  2607. break;
  2608. }
  2609. return NOTIFY_DONE;
  2610. }
  2611. static struct notifier_block rocker_netevent_nb __read_mostly = {
  2612. .notifier_call = rocker_netevent_event,
  2613. };
  2614. /***********************
  2615. * Module init and exit
  2616. ***********************/
  2617. static int __init rocker_module_init(void)
  2618. {
  2619. int err;
  2620. register_netdevice_notifier(&rocker_netdevice_nb);
  2621. register_netevent_notifier(&rocker_netevent_nb);
  2622. err = pci_register_driver(&rocker_pci_driver);
  2623. if (err)
  2624. goto err_pci_register_driver;
  2625. return 0;
  2626. err_pci_register_driver:
  2627. unregister_netevent_notifier(&rocker_netevent_nb);
  2628. unregister_netdevice_notifier(&rocker_netdevice_nb);
  2629. return err;
  2630. }
  2631. static void __exit rocker_module_exit(void)
  2632. {
  2633. unregister_netevent_notifier(&rocker_netevent_nb);
  2634. unregister_netdevice_notifier(&rocker_netdevice_nb);
  2635. pci_unregister_driver(&rocker_pci_driver);
  2636. }
  2637. module_init(rocker_module_init);
  2638. module_exit(rocker_module_exit);
  2639. MODULE_LICENSE("GPL v2");
  2640. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  2641. MODULE_AUTHOR("Scott Feldman <sfeldma@gmail.com>");
  2642. MODULE_DESCRIPTION("Rocker switch device driver");
  2643. MODULE_DEVICE_TABLE(pci, rocker_pci_id_table);