ocelot_net.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868
  1. // SPDX-License-Identifier: (GPL-2.0 OR MIT)
  2. /* Microsemi Ocelot Switch driver
  3. *
  4. * This contains glue logic between the switchdev driver operations and the
  5. * mscc_ocelot_switch_lib.
  6. *
  7. * Copyright (c) 2017, 2019 Microsemi Corporation
  8. * Copyright 2020-2021 NXP
  9. */
  10. #include <linux/dsa/ocelot.h>
  11. #include <linux/if_bridge.h>
  12. #include <linux/of_net.h>
  13. #include <linux/phy/phy.h>
  14. #include <net/pkt_cls.h>
  15. #include "ocelot.h"
  16. #include "ocelot_police.h"
  17. #include "ocelot_vcap.h"
  18. #include "ocelot_fdma.h"
  19. #define OCELOT_MAC_QUIRKS OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP
  20. struct ocelot_dump_ctx {
  21. struct net_device *dev;
  22. struct sk_buff *skb;
  23. struct netlink_callback *cb;
  24. int idx;
  25. };
  26. static bool ocelot_netdevice_dev_check(const struct net_device *dev);
  27. static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
  28. {
  29. return devlink_priv(dlp->devlink);
  30. }
  31. static int devlink_port_to_port(struct devlink_port *dlp)
  32. {
  33. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  34. return dlp - ocelot->devlink_ports;
  35. }
  36. static int ocelot_devlink_sb_pool_get(struct devlink *dl,
  37. unsigned int sb_index, u16 pool_index,
  38. struct devlink_sb_pool_info *pool_info)
  39. {
  40. struct ocelot *ocelot = devlink_priv(dl);
  41. return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
  42. }
  43. static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
  44. u16 pool_index, u32 size,
  45. enum devlink_sb_threshold_type threshold_type,
  46. struct netlink_ext_ack *extack)
  47. {
  48. struct ocelot *ocelot = devlink_priv(dl);
  49. return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
  50. threshold_type, extack);
  51. }
  52. static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
  53. unsigned int sb_index, u16 pool_index,
  54. u32 *p_threshold)
  55. {
  56. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  57. int port = devlink_port_to_port(dlp);
  58. return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
  59. p_threshold);
  60. }
  61. static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
  62. unsigned int sb_index, u16 pool_index,
  63. u32 threshold,
  64. struct netlink_ext_ack *extack)
  65. {
  66. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  67. int port = devlink_port_to_port(dlp);
  68. return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
  69. threshold, extack);
  70. }
  71. static int
  72. ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
  73. unsigned int sb_index, u16 tc_index,
  74. enum devlink_sb_pool_type pool_type,
  75. u16 *p_pool_index, u32 *p_threshold)
  76. {
  77. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  78. int port = devlink_port_to_port(dlp);
  79. return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
  80. pool_type, p_pool_index,
  81. p_threshold);
  82. }
  83. static int
  84. ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
  85. unsigned int sb_index, u16 tc_index,
  86. enum devlink_sb_pool_type pool_type,
  87. u16 pool_index, u32 threshold,
  88. struct netlink_ext_ack *extack)
  89. {
  90. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  91. int port = devlink_port_to_port(dlp);
  92. return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
  93. pool_type, pool_index, threshold,
  94. extack);
  95. }
  96. static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
  97. unsigned int sb_index)
  98. {
  99. struct ocelot *ocelot = devlink_priv(dl);
  100. return ocelot_sb_occ_snapshot(ocelot, sb_index);
  101. }
  102. static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
  103. unsigned int sb_index)
  104. {
  105. struct ocelot *ocelot = devlink_priv(dl);
  106. return ocelot_sb_occ_max_clear(ocelot, sb_index);
  107. }
  108. static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
  109. unsigned int sb_index,
  110. u16 pool_index, u32 *p_cur,
  111. u32 *p_max)
  112. {
  113. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  114. int port = devlink_port_to_port(dlp);
  115. return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
  116. p_cur, p_max);
  117. }
  118. static int
  119. ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
  120. unsigned int sb_index, u16 tc_index,
  121. enum devlink_sb_pool_type pool_type,
  122. u32 *p_cur, u32 *p_max)
  123. {
  124. struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
  125. int port = devlink_port_to_port(dlp);
  126. return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
  127. tc_index, pool_type,
  128. p_cur, p_max);
  129. }
  130. const struct devlink_ops ocelot_devlink_ops = {
  131. .sb_pool_get = ocelot_devlink_sb_pool_get,
  132. .sb_pool_set = ocelot_devlink_sb_pool_set,
  133. .sb_port_pool_get = ocelot_devlink_sb_port_pool_get,
  134. .sb_port_pool_set = ocelot_devlink_sb_port_pool_set,
  135. .sb_tc_pool_bind_get = ocelot_devlink_sb_tc_pool_bind_get,
  136. .sb_tc_pool_bind_set = ocelot_devlink_sb_tc_pool_bind_set,
  137. .sb_occ_snapshot = ocelot_devlink_sb_occ_snapshot,
  138. .sb_occ_max_clear = ocelot_devlink_sb_occ_max_clear,
  139. .sb_occ_port_pool_get = ocelot_devlink_sb_occ_port_pool_get,
  140. .sb_occ_tc_port_bind_get = ocelot_devlink_sb_occ_tc_port_bind_get,
  141. };
  142. int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
  143. enum devlink_port_flavour flavour)
  144. {
  145. struct devlink_port *dlp = &ocelot->devlink_ports[port];
  146. int id_len = sizeof(ocelot->base_mac);
  147. struct devlink *dl = ocelot->devlink;
  148. struct devlink_port_attrs attrs = {};
  149. memset(dlp, 0, sizeof(*dlp));
  150. memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
  151. attrs.switch_id.id_len = id_len;
  152. attrs.phys.port_number = port;
  153. attrs.flavour = flavour;
  154. devlink_port_attrs_set(dlp, &attrs);
  155. return devlink_port_register(dl, dlp, port);
  156. }
  157. void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
  158. {
  159. struct devlink_port *dlp = &ocelot->devlink_ports[port];
  160. devlink_port_unregister(dlp);
  161. }
  162. int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
  163. struct flow_cls_offload *f,
  164. bool ingress)
  165. {
  166. struct ocelot *ocelot = priv->port.ocelot;
  167. int port = priv->port.index;
  168. if (!ingress)
  169. return -EOPNOTSUPP;
  170. switch (f->command) {
  171. case FLOW_CLS_REPLACE:
  172. return ocelot_cls_flower_replace(ocelot, port, f, ingress);
  173. case FLOW_CLS_DESTROY:
  174. return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
  175. case FLOW_CLS_STATS:
  176. return ocelot_cls_flower_stats(ocelot, port, f, ingress);
  177. default:
  178. return -EOPNOTSUPP;
  179. }
  180. }
  181. static int ocelot_setup_tc_cls_matchall_police(struct ocelot_port_private *priv,
  182. struct tc_cls_matchall_offload *f,
  183. bool ingress,
  184. struct netlink_ext_ack *extack)
  185. {
  186. struct flow_action_entry *action = &f->rule->action.entries[0];
  187. struct ocelot *ocelot = priv->port.ocelot;
  188. struct ocelot_policer pol = { 0 };
  189. int port = priv->port.index;
  190. int err;
  191. if (!ingress) {
  192. NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
  193. return -EOPNOTSUPP;
  194. }
  195. if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
  196. NL_SET_ERR_MSG_MOD(extack,
  197. "Only one policer per port is supported");
  198. return -EEXIST;
  199. }
  200. err = ocelot_policer_validate(&f->rule->action, action, extack);
  201. if (err)
  202. return err;
  203. pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
  204. pol.burst = action->police.burst;
  205. err = ocelot_port_policer_add(ocelot, port, &pol);
  206. if (err) {
  207. NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
  208. return err;
  209. }
  210. priv->tc.police_id = f->cookie;
  211. priv->tc.offload_cnt++;
  212. return 0;
  213. }
  214. static int ocelot_setup_tc_cls_matchall_mirred(struct ocelot_port_private *priv,
  215. struct tc_cls_matchall_offload *f,
  216. bool ingress,
  217. struct netlink_ext_ack *extack)
  218. {
  219. struct flow_action *action = &f->rule->action;
  220. struct ocelot *ocelot = priv->port.ocelot;
  221. struct ocelot_port_private *other_priv;
  222. const struct flow_action_entry *a;
  223. int err;
  224. if (f->common.protocol != htons(ETH_P_ALL))
  225. return -EOPNOTSUPP;
  226. if (!flow_action_basic_hw_stats_check(action, extack))
  227. return -EOPNOTSUPP;
  228. a = &action->entries[0];
  229. if (!a->dev)
  230. return -EINVAL;
  231. if (!ocelot_netdevice_dev_check(a->dev)) {
  232. NL_SET_ERR_MSG_MOD(extack,
  233. "Destination not an ocelot port");
  234. return -EOPNOTSUPP;
  235. }
  236. other_priv = netdev_priv(a->dev);
  237. err = ocelot_port_mirror_add(ocelot, priv->port.index,
  238. other_priv->port.index, ingress, extack);
  239. if (err)
  240. return err;
  241. if (ingress)
  242. priv->tc.ingress_mirred_id = f->cookie;
  243. else
  244. priv->tc.egress_mirred_id = f->cookie;
  245. priv->tc.offload_cnt++;
  246. return 0;
  247. }
  248. static int ocelot_del_tc_cls_matchall_police(struct ocelot_port_private *priv,
  249. struct netlink_ext_ack *extack)
  250. {
  251. struct ocelot *ocelot = priv->port.ocelot;
  252. int port = priv->port.index;
  253. int err;
  254. err = ocelot_port_policer_del(ocelot, port);
  255. if (err) {
  256. NL_SET_ERR_MSG_MOD(extack,
  257. "Could not delete policer");
  258. return err;
  259. }
  260. priv->tc.police_id = 0;
  261. priv->tc.offload_cnt--;
  262. return 0;
  263. }
  264. static int ocelot_del_tc_cls_matchall_mirred(struct ocelot_port_private *priv,
  265. bool ingress,
  266. struct netlink_ext_ack *extack)
  267. {
  268. struct ocelot *ocelot = priv->port.ocelot;
  269. int port = priv->port.index;
  270. ocelot_port_mirror_del(ocelot, port, ingress);
  271. if (ingress)
  272. priv->tc.ingress_mirred_id = 0;
  273. else
  274. priv->tc.egress_mirred_id = 0;
  275. priv->tc.offload_cnt--;
  276. return 0;
  277. }
  278. static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
  279. struct tc_cls_matchall_offload *f,
  280. bool ingress)
  281. {
  282. struct netlink_ext_ack *extack = f->common.extack;
  283. struct flow_action_entry *action;
  284. switch (f->command) {
  285. case TC_CLSMATCHALL_REPLACE:
  286. if (!flow_offload_has_one_action(&f->rule->action)) {
  287. NL_SET_ERR_MSG_MOD(extack,
  288. "Only one action is supported");
  289. return -EOPNOTSUPP;
  290. }
  291. if (priv->tc.block_shared) {
  292. NL_SET_ERR_MSG_MOD(extack,
  293. "Matchall offloads not supported on shared blocks");
  294. return -EOPNOTSUPP;
  295. }
  296. action = &f->rule->action.entries[0];
  297. switch (action->id) {
  298. case FLOW_ACTION_POLICE:
  299. return ocelot_setup_tc_cls_matchall_police(priv, f,
  300. ingress,
  301. extack);
  302. break;
  303. case FLOW_ACTION_MIRRED:
  304. return ocelot_setup_tc_cls_matchall_mirred(priv, f,
  305. ingress,
  306. extack);
  307. default:
  308. NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
  309. return -EOPNOTSUPP;
  310. }
  311. break;
  312. case TC_CLSMATCHALL_DESTROY:
  313. action = &f->rule->action.entries[0];
  314. if (f->cookie == priv->tc.police_id)
  315. return ocelot_del_tc_cls_matchall_police(priv, extack);
  316. else if (f->cookie == priv->tc.ingress_mirred_id ||
  317. f->cookie == priv->tc.egress_mirred_id)
  318. return ocelot_del_tc_cls_matchall_mirred(priv, ingress,
  319. extack);
  320. else
  321. return -ENOENT;
  322. break;
  323. case TC_CLSMATCHALL_STATS:
  324. default:
  325. return -EOPNOTSUPP;
  326. }
  327. }
  328. static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
  329. void *type_data,
  330. void *cb_priv, bool ingress)
  331. {
  332. struct ocelot_port_private *priv = cb_priv;
  333. if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
  334. return -EOPNOTSUPP;
  335. switch (type) {
  336. case TC_SETUP_CLSMATCHALL:
  337. return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
  338. case TC_SETUP_CLSFLOWER:
  339. return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
  340. default:
  341. return -EOPNOTSUPP;
  342. }
  343. }
  344. static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
  345. void *type_data,
  346. void *cb_priv)
  347. {
  348. return ocelot_setup_tc_block_cb(type, type_data,
  349. cb_priv, true);
  350. }
  351. static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
  352. void *type_data,
  353. void *cb_priv)
  354. {
  355. return ocelot_setup_tc_block_cb(type, type_data,
  356. cb_priv, false);
  357. }
  358. static LIST_HEAD(ocelot_block_cb_list);
  359. static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
  360. struct flow_block_offload *f)
  361. {
  362. struct flow_block_cb *block_cb;
  363. flow_setup_cb_t *cb;
  364. if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
  365. cb = ocelot_setup_tc_block_cb_ig;
  366. priv->tc.block_shared = f->block_shared;
  367. } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
  368. cb = ocelot_setup_tc_block_cb_eg;
  369. } else {
  370. return -EOPNOTSUPP;
  371. }
  372. f->driver_block_list = &ocelot_block_cb_list;
  373. switch (f->command) {
  374. case FLOW_BLOCK_BIND:
  375. if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
  376. return -EBUSY;
  377. block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
  378. if (IS_ERR(block_cb))
  379. return PTR_ERR(block_cb);
  380. flow_block_cb_add(block_cb, f);
  381. list_add_tail(&block_cb->driver_list, f->driver_block_list);
  382. return 0;
  383. case FLOW_BLOCK_UNBIND:
  384. block_cb = flow_block_cb_lookup(f->block, cb, priv);
  385. if (!block_cb)
  386. return -ENOENT;
  387. flow_block_cb_remove(block_cb, f);
  388. list_del(&block_cb->driver_list);
  389. return 0;
  390. default:
  391. return -EOPNOTSUPP;
  392. }
  393. }
  394. static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
  395. void *type_data)
  396. {
  397. struct ocelot_port_private *priv = netdev_priv(dev);
  398. switch (type) {
  399. case TC_SETUP_BLOCK:
  400. return ocelot_setup_tc_block(priv, type_data);
  401. default:
  402. return -EOPNOTSUPP;
  403. }
  404. return 0;
  405. }
  406. static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
  407. bool untagged)
  408. {
  409. struct ocelot_port_private *priv = netdev_priv(dev);
  410. struct ocelot_port *ocelot_port = &priv->port;
  411. struct ocelot *ocelot = ocelot_port->ocelot;
  412. int port = priv->port.index;
  413. int ret;
  414. ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
  415. if (ret)
  416. return ret;
  417. /* Add the port MAC address to with the right VLAN information */
  418. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
  419. ENTRYTYPE_LOCKED);
  420. return 0;
  421. }
  422. static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
  423. {
  424. struct ocelot_port_private *priv = netdev_priv(dev);
  425. struct ocelot *ocelot = priv->port.ocelot;
  426. int port = priv->port.index;
  427. int ret;
  428. /* 8021q removes VID 0 on module unload for all interfaces
  429. * with VLAN filtering feature. We need to keep it to receive
  430. * untagged traffic.
  431. */
  432. if (vid == OCELOT_STANDALONE_PVID)
  433. return 0;
  434. ret = ocelot_vlan_del(ocelot, port, vid);
  435. if (ret)
  436. return ret;
  437. /* Del the port MAC address to with the right VLAN information */
  438. ocelot_mact_forget(ocelot, dev->dev_addr, vid);
  439. return 0;
  440. }
  441. static int ocelot_port_open(struct net_device *dev)
  442. {
  443. struct ocelot_port_private *priv = netdev_priv(dev);
  444. phylink_start(priv->phylink);
  445. return 0;
  446. }
  447. static int ocelot_port_stop(struct net_device *dev)
  448. {
  449. struct ocelot_port_private *priv = netdev_priv(dev);
  450. phylink_stop(priv->phylink);
  451. return 0;
  452. }
  453. static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
  454. {
  455. struct ocelot_port_private *priv = netdev_priv(dev);
  456. struct ocelot_port *ocelot_port = &priv->port;
  457. struct ocelot *ocelot = ocelot_port->ocelot;
  458. int port = priv->port.index;
  459. u32 rew_op = 0;
  460. if (!static_branch_unlikely(&ocelot_fdma_enabled) &&
  461. !ocelot_can_inject(ocelot, 0))
  462. return NETDEV_TX_BUSY;
  463. /* Check if timestamping is needed */
  464. if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
  465. struct sk_buff *clone = NULL;
  466. if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
  467. kfree_skb(skb);
  468. return NETDEV_TX_OK;
  469. }
  470. if (clone)
  471. OCELOT_SKB_CB(skb)->clone = clone;
  472. rew_op = ocelot_ptp_rew_op(skb);
  473. }
  474. if (static_branch_unlikely(&ocelot_fdma_enabled)) {
  475. ocelot_fdma_inject_frame(ocelot, port, rew_op, skb, dev);
  476. } else {
  477. ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
  478. consume_skb(skb);
  479. }
  480. return NETDEV_TX_OK;
  481. }
  482. enum ocelot_action_type {
  483. OCELOT_MACT_LEARN,
  484. OCELOT_MACT_FORGET,
  485. };
  486. struct ocelot_mact_work_ctx {
  487. struct work_struct work;
  488. struct ocelot *ocelot;
  489. enum ocelot_action_type type;
  490. union {
  491. /* OCELOT_MACT_LEARN */
  492. struct {
  493. unsigned char addr[ETH_ALEN];
  494. u16 vid;
  495. enum macaccess_entry_type entry_type;
  496. int pgid;
  497. } learn;
  498. /* OCELOT_MACT_FORGET */
  499. struct {
  500. unsigned char addr[ETH_ALEN];
  501. u16 vid;
  502. } forget;
  503. };
  504. };
  505. #define ocelot_work_to_ctx(x) \
  506. container_of((x), struct ocelot_mact_work_ctx, work)
  507. static void ocelot_mact_work(struct work_struct *work)
  508. {
  509. struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
  510. struct ocelot *ocelot = w->ocelot;
  511. switch (w->type) {
  512. case OCELOT_MACT_LEARN:
  513. ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
  514. w->learn.vid, w->learn.entry_type);
  515. break;
  516. case OCELOT_MACT_FORGET:
  517. ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
  518. break;
  519. default:
  520. break;
  521. }
  522. kfree(w);
  523. }
  524. static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
  525. const struct ocelot_mact_work_ctx *ctx)
  526. {
  527. struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
  528. if (!w)
  529. return -ENOMEM;
  530. w->ocelot = ocelot;
  531. INIT_WORK(&w->work, ocelot_mact_work);
  532. queue_work(ocelot->owq, &w->work);
  533. return 0;
  534. }
  535. static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
  536. {
  537. struct ocelot_port_private *priv = netdev_priv(dev);
  538. struct ocelot_port *ocelot_port = &priv->port;
  539. struct ocelot *ocelot = ocelot_port->ocelot;
  540. struct ocelot_mact_work_ctx w;
  541. ether_addr_copy(w.forget.addr, addr);
  542. w.forget.vid = OCELOT_STANDALONE_PVID;
  543. w.type = OCELOT_MACT_FORGET;
  544. return ocelot_enqueue_mact_action(ocelot, &w);
  545. }
  546. static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
  547. {
  548. struct ocelot_port_private *priv = netdev_priv(dev);
  549. struct ocelot_port *ocelot_port = &priv->port;
  550. struct ocelot *ocelot = ocelot_port->ocelot;
  551. struct ocelot_mact_work_ctx w;
  552. ether_addr_copy(w.learn.addr, addr);
  553. w.learn.vid = OCELOT_STANDALONE_PVID;
  554. w.learn.pgid = PGID_CPU;
  555. w.learn.entry_type = ENTRYTYPE_LOCKED;
  556. w.type = OCELOT_MACT_LEARN;
  557. return ocelot_enqueue_mact_action(ocelot, &w);
  558. }
  559. static void ocelot_set_rx_mode(struct net_device *dev)
  560. {
  561. struct ocelot_port_private *priv = netdev_priv(dev);
  562. struct ocelot *ocelot = priv->port.ocelot;
  563. u32 val;
  564. int i;
  565. /* This doesn't handle promiscuous mode because the bridge core is
  566. * setting IFF_PROMISC on all slave interfaces and all frames would be
  567. * forwarded to the CPU port.
  568. */
  569. val = GENMASK(ocelot->num_phys_ports - 1, 0);
  570. for_each_nonreserved_multicast_dest_pgid(ocelot, i)
  571. ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
  572. __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
  573. }
  574. static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
  575. {
  576. struct ocelot_port_private *priv = netdev_priv(dev);
  577. struct ocelot_port *ocelot_port = &priv->port;
  578. struct ocelot *ocelot = ocelot_port->ocelot;
  579. const struct sockaddr *addr = p;
  580. /* Learn the new net device MAC address in the mac table. */
  581. ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
  582. OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
  583. /* Then forget the previous one. */
  584. ocelot_mact_forget(ocelot, dev->dev_addr, OCELOT_STANDALONE_PVID);
  585. eth_hw_addr_set(dev, addr->sa_data);
  586. return 0;
  587. }
  588. static void ocelot_get_stats64(struct net_device *dev,
  589. struct rtnl_link_stats64 *stats)
  590. {
  591. struct ocelot_port_private *priv = netdev_priv(dev);
  592. struct ocelot *ocelot = priv->port.ocelot;
  593. int port = priv->port.index;
  594. return ocelot_port_get_stats64(ocelot, port, stats);
  595. }
  596. static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  597. struct net_device *dev,
  598. const unsigned char *addr,
  599. u16 vid, u16 flags,
  600. struct netlink_ext_ack *extack)
  601. {
  602. struct ocelot_port_private *priv = netdev_priv(dev);
  603. struct ocelot_port *ocelot_port = &priv->port;
  604. struct ocelot *ocelot = ocelot_port->ocelot;
  605. int port = priv->port.index;
  606. return ocelot_fdb_add(ocelot, port, addr, vid, ocelot_port->bridge);
  607. }
  608. static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  609. struct net_device *dev,
  610. const unsigned char *addr, u16 vid,
  611. struct netlink_ext_ack *extack)
  612. {
  613. struct ocelot_port_private *priv = netdev_priv(dev);
  614. struct ocelot_port *ocelot_port = &priv->port;
  615. struct ocelot *ocelot = ocelot_port->ocelot;
  616. int port = priv->port.index;
  617. return ocelot_fdb_del(ocelot, port, addr, vid, ocelot_port->bridge);
  618. }
  619. static int ocelot_port_fdb_do_dump(const unsigned char *addr, u16 vid,
  620. bool is_static, void *data)
  621. {
  622. struct ocelot_dump_ctx *dump = data;
  623. u32 portid = NETLINK_CB(dump->cb->skb).portid;
  624. u32 seq = dump->cb->nlh->nlmsg_seq;
  625. struct nlmsghdr *nlh;
  626. struct ndmsg *ndm;
  627. if (dump->idx < dump->cb->args[2])
  628. goto skip;
  629. nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
  630. sizeof(*ndm), NLM_F_MULTI);
  631. if (!nlh)
  632. return -EMSGSIZE;
  633. ndm = nlmsg_data(nlh);
  634. ndm->ndm_family = AF_BRIDGE;
  635. ndm->ndm_pad1 = 0;
  636. ndm->ndm_pad2 = 0;
  637. ndm->ndm_flags = NTF_SELF;
  638. ndm->ndm_type = 0;
  639. ndm->ndm_ifindex = dump->dev->ifindex;
  640. ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
  641. if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, addr))
  642. goto nla_put_failure;
  643. if (vid && nla_put_u16(dump->skb, NDA_VLAN, vid))
  644. goto nla_put_failure;
  645. nlmsg_end(dump->skb, nlh);
  646. skip:
  647. dump->idx++;
  648. return 0;
  649. nla_put_failure:
  650. nlmsg_cancel(dump->skb, nlh);
  651. return -EMSGSIZE;
  652. }
  653. static int ocelot_port_fdb_dump(struct sk_buff *skb,
  654. struct netlink_callback *cb,
  655. struct net_device *dev,
  656. struct net_device *filter_dev, int *idx)
  657. {
  658. struct ocelot_port_private *priv = netdev_priv(dev);
  659. struct ocelot *ocelot = priv->port.ocelot;
  660. struct ocelot_dump_ctx dump = {
  661. .dev = dev,
  662. .skb = skb,
  663. .cb = cb,
  664. .idx = *idx,
  665. };
  666. int port = priv->port.index;
  667. int ret;
  668. ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
  669. *idx = dump.idx;
  670. return ret;
  671. }
  672. static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
  673. u16 vid)
  674. {
  675. return ocelot_vlan_vid_add(dev, vid, false, false);
  676. }
  677. static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
  678. u16 vid)
  679. {
  680. return ocelot_vlan_vid_del(dev, vid);
  681. }
  682. static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
  683. netdev_features_t features)
  684. {
  685. u32 val;
  686. /* Filtering */
  687. val = ocelot_read(ocelot, ANA_VLANMASK);
  688. if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
  689. val |= BIT(port);
  690. else
  691. val &= ~BIT(port);
  692. ocelot_write(ocelot, val, ANA_VLANMASK);
  693. }
  694. static int ocelot_set_features(struct net_device *dev,
  695. netdev_features_t features)
  696. {
  697. netdev_features_t changed = dev->features ^ features;
  698. struct ocelot_port_private *priv = netdev_priv(dev);
  699. struct ocelot *ocelot = priv->port.ocelot;
  700. int port = priv->port.index;
  701. if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
  702. priv->tc.offload_cnt) {
  703. netdev_err(dev,
  704. "Cannot disable HW TC offload while offloads active\n");
  705. return -EBUSY;
  706. }
  707. if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
  708. ocelot_vlan_mode(ocelot, port, features);
  709. return 0;
  710. }
  711. static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  712. {
  713. struct ocelot_port_private *priv = netdev_priv(dev);
  714. struct ocelot *ocelot = priv->port.ocelot;
  715. int port = priv->port.index;
  716. /* If the attached PHY device isn't capable of timestamping operations,
  717. * use our own (when possible).
  718. */
  719. if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
  720. switch (cmd) {
  721. case SIOCSHWTSTAMP:
  722. return ocelot_hwstamp_set(ocelot, port, ifr);
  723. case SIOCGHWTSTAMP:
  724. return ocelot_hwstamp_get(ocelot, port, ifr);
  725. }
  726. }
  727. return phy_mii_ioctl(dev->phydev, ifr, cmd);
  728. }
  729. static int ocelot_change_mtu(struct net_device *dev, int new_mtu)
  730. {
  731. struct ocelot_port_private *priv = netdev_priv(dev);
  732. struct ocelot_port *ocelot_port = &priv->port;
  733. struct ocelot *ocelot = ocelot_port->ocelot;
  734. ocelot_port_set_maxlen(ocelot, priv->port.index, new_mtu);
  735. WRITE_ONCE(dev->mtu, new_mtu);
  736. return 0;
  737. }
  738. static const struct net_device_ops ocelot_port_netdev_ops = {
  739. .ndo_open = ocelot_port_open,
  740. .ndo_stop = ocelot_port_stop,
  741. .ndo_start_xmit = ocelot_port_xmit,
  742. .ndo_change_mtu = ocelot_change_mtu,
  743. .ndo_set_rx_mode = ocelot_set_rx_mode,
  744. .ndo_set_mac_address = ocelot_port_set_mac_address,
  745. .ndo_get_stats64 = ocelot_get_stats64,
  746. .ndo_fdb_add = ocelot_port_fdb_add,
  747. .ndo_fdb_del = ocelot_port_fdb_del,
  748. .ndo_fdb_dump = ocelot_port_fdb_dump,
  749. .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
  750. .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
  751. .ndo_set_features = ocelot_set_features,
  752. .ndo_setup_tc = ocelot_setup_tc,
  753. .ndo_eth_ioctl = ocelot_ioctl,
  754. };
  755. struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
  756. {
  757. struct ocelot_port *ocelot_port = ocelot->ports[port];
  758. struct ocelot_port_private *priv;
  759. if (!ocelot_port)
  760. return NULL;
  761. priv = container_of(ocelot_port, struct ocelot_port_private, port);
  762. return priv->dev;
  763. }
  764. /* Checks if the net_device instance given to us originates from our driver */
  765. static bool ocelot_netdevice_dev_check(const struct net_device *dev)
  766. {
  767. return dev->netdev_ops == &ocelot_port_netdev_ops;
  768. }
  769. int ocelot_netdev_to_port(struct net_device *dev)
  770. {
  771. struct ocelot_port_private *priv;
  772. if (!dev || !ocelot_netdevice_dev_check(dev))
  773. return -EINVAL;
  774. priv = netdev_priv(dev);
  775. return priv->port.index;
  776. }
  777. static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
  778. u8 *data)
  779. {
  780. struct ocelot_port_private *priv = netdev_priv(netdev);
  781. struct ocelot *ocelot = priv->port.ocelot;
  782. int port = priv->port.index;
  783. ocelot_get_strings(ocelot, port, sset, data);
  784. }
  785. static void ocelot_port_get_ethtool_stats(struct net_device *dev,
  786. struct ethtool_stats *stats,
  787. u64 *data)
  788. {
  789. struct ocelot_port_private *priv = netdev_priv(dev);
  790. struct ocelot *ocelot = priv->port.ocelot;
  791. int port = priv->port.index;
  792. ocelot_get_ethtool_stats(ocelot, port, data);
  793. }
  794. static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
  795. {
  796. struct ocelot_port_private *priv = netdev_priv(dev);
  797. struct ocelot *ocelot = priv->port.ocelot;
  798. int port = priv->port.index;
  799. return ocelot_get_sset_count(ocelot, port, sset);
  800. }
  801. static int ocelot_port_get_ts_info(struct net_device *dev,
  802. struct kernel_ethtool_ts_info *info)
  803. {
  804. struct ocelot_port_private *priv = netdev_priv(dev);
  805. struct ocelot *ocelot = priv->port.ocelot;
  806. int port = priv->port.index;
  807. if (!ocelot->ptp)
  808. return ethtool_op_get_ts_info(dev, info);
  809. return ocelot_get_ts_info(ocelot, port, info);
  810. }
  811. static const struct ethtool_ops ocelot_ethtool_ops = {
  812. .get_strings = ocelot_port_get_strings,
  813. .get_ethtool_stats = ocelot_port_get_ethtool_stats,
  814. .get_sset_count = ocelot_port_get_sset_count,
  815. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  816. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  817. .get_ts_info = ocelot_port_get_ts_info,
  818. };
  819. static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
  820. u8 state)
  821. {
  822. ocelot_bridge_stp_state_set(ocelot, port, state);
  823. }
  824. static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
  825. unsigned long ageing_clock_t)
  826. {
  827. unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
  828. u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
  829. ocelot_set_ageing_time(ocelot, ageing_time);
  830. }
  831. static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
  832. {
  833. u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
  834. ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
  835. ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
  836. u32 val = 0;
  837. if (mc)
  838. val = cpu_fwd_mcast;
  839. ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
  840. ANA_PORT_CPU_FWD_CFG, port);
  841. }
  842. static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
  843. const struct switchdev_attr *attr,
  844. struct netlink_ext_ack *extack)
  845. {
  846. struct ocelot_port_private *priv = netdev_priv(dev);
  847. struct ocelot *ocelot = priv->port.ocelot;
  848. int port = priv->port.index;
  849. int err = 0;
  850. if (ctx && ctx != priv)
  851. return 0;
  852. switch (attr->id) {
  853. case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
  854. ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
  855. break;
  856. case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
  857. ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
  858. break;
  859. case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
  860. ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering,
  861. extack);
  862. break;
  863. case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
  864. ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
  865. break;
  866. case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
  867. err = ocelot_port_pre_bridge_flags(ocelot, port,
  868. attr->u.brport_flags);
  869. break;
  870. case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
  871. ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
  872. break;
  873. default:
  874. err = -EOPNOTSUPP;
  875. break;
  876. }
  877. return err;
  878. }
  879. static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
  880. bool untagged, struct netlink_ext_ack *extack)
  881. {
  882. struct ocelot_port_private *priv = netdev_priv(dev);
  883. struct ocelot_port *ocelot_port = &priv->port;
  884. struct ocelot *ocelot = ocelot_port->ocelot;
  885. int port = priv->port.index;
  886. return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack);
  887. }
  888. static int ocelot_port_obj_add_vlan(struct net_device *dev,
  889. const struct switchdev_obj_port_vlan *vlan,
  890. struct netlink_ext_ack *extack)
  891. {
  892. bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
  893. bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
  894. int ret;
  895. ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack);
  896. if (ret)
  897. return ret;
  898. return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
  899. }
  900. static int ocelot_port_obj_add_mdb(struct net_device *dev,
  901. const struct switchdev_obj_port_mdb *mdb)
  902. {
  903. struct ocelot_port_private *priv = netdev_priv(dev);
  904. struct ocelot_port *ocelot_port = &priv->port;
  905. struct ocelot *ocelot = ocelot_port->ocelot;
  906. int port = priv->port.index;
  907. return ocelot_port_mdb_add(ocelot, port, mdb, ocelot_port->bridge);
  908. }
  909. static int ocelot_port_obj_del_mdb(struct net_device *dev,
  910. const struct switchdev_obj_port_mdb *mdb)
  911. {
  912. struct ocelot_port_private *priv = netdev_priv(dev);
  913. struct ocelot_port *ocelot_port = &priv->port;
  914. struct ocelot *ocelot = ocelot_port->ocelot;
  915. int port = priv->port.index;
  916. return ocelot_port_mdb_del(ocelot, port, mdb, ocelot_port->bridge);
  917. }
  918. static int ocelot_port_obj_mrp_add(struct net_device *dev,
  919. const struct switchdev_obj_mrp *mrp)
  920. {
  921. struct ocelot_port_private *priv = netdev_priv(dev);
  922. struct ocelot_port *ocelot_port = &priv->port;
  923. struct ocelot *ocelot = ocelot_port->ocelot;
  924. int port = priv->port.index;
  925. return ocelot_mrp_add(ocelot, port, mrp);
  926. }
  927. static int ocelot_port_obj_mrp_del(struct net_device *dev,
  928. const struct switchdev_obj_mrp *mrp)
  929. {
  930. struct ocelot_port_private *priv = netdev_priv(dev);
  931. struct ocelot_port *ocelot_port = &priv->port;
  932. struct ocelot *ocelot = ocelot_port->ocelot;
  933. int port = priv->port.index;
  934. return ocelot_mrp_del(ocelot, port, mrp);
  935. }
  936. static int
  937. ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
  938. const struct switchdev_obj_ring_role_mrp *mrp)
  939. {
  940. struct ocelot_port_private *priv = netdev_priv(dev);
  941. struct ocelot_port *ocelot_port = &priv->port;
  942. struct ocelot *ocelot = ocelot_port->ocelot;
  943. int port = priv->port.index;
  944. return ocelot_mrp_add_ring_role(ocelot, port, mrp);
  945. }
  946. static int
  947. ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
  948. const struct switchdev_obj_ring_role_mrp *mrp)
  949. {
  950. struct ocelot_port_private *priv = netdev_priv(dev);
  951. struct ocelot_port *ocelot_port = &priv->port;
  952. struct ocelot *ocelot = ocelot_port->ocelot;
  953. int port = priv->port.index;
  954. return ocelot_mrp_del_ring_role(ocelot, port, mrp);
  955. }
  956. static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
  957. const struct switchdev_obj *obj,
  958. struct netlink_ext_ack *extack)
  959. {
  960. struct ocelot_port_private *priv = netdev_priv(dev);
  961. int ret = 0;
  962. if (ctx && ctx != priv)
  963. return 0;
  964. switch (obj->id) {
  965. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  966. ret = ocelot_port_obj_add_vlan(dev,
  967. SWITCHDEV_OBJ_PORT_VLAN(obj),
  968. extack);
  969. break;
  970. case SWITCHDEV_OBJ_ID_PORT_MDB:
  971. ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
  972. break;
  973. case SWITCHDEV_OBJ_ID_MRP:
  974. ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
  975. break;
  976. case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
  977. ret = ocelot_port_obj_mrp_add_ring_role(dev,
  978. SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
  979. break;
  980. default:
  981. return -EOPNOTSUPP;
  982. }
  983. return ret;
  984. }
  985. static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
  986. const struct switchdev_obj *obj)
  987. {
  988. struct ocelot_port_private *priv = netdev_priv(dev);
  989. int ret = 0;
  990. if (ctx && ctx != priv)
  991. return 0;
  992. switch (obj->id) {
  993. case SWITCHDEV_OBJ_ID_PORT_VLAN:
  994. ret = ocelot_vlan_vid_del(dev,
  995. SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
  996. break;
  997. case SWITCHDEV_OBJ_ID_PORT_MDB:
  998. ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
  999. break;
  1000. case SWITCHDEV_OBJ_ID_MRP:
  1001. ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
  1002. break;
  1003. case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
  1004. ret = ocelot_port_obj_mrp_del_ring_role(dev,
  1005. SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
  1006. break;
  1007. default:
  1008. return -EOPNOTSUPP;
  1009. }
  1010. return ret;
  1011. }
  1012. static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
  1013. struct net_device *brport_dev)
  1014. {
  1015. struct switchdev_brport_flags flags = {0};
  1016. int flag;
  1017. flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
  1018. for_each_set_bit(flag, &flags.mask, 32)
  1019. if (br_port_flag_is_set(brport_dev, BIT(flag)))
  1020. flags.val |= BIT(flag);
  1021. ocelot_port_bridge_flags(ocelot, port, flags);
  1022. }
  1023. static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
  1024. {
  1025. struct switchdev_brport_flags flags;
  1026. flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
  1027. flags.val = flags.mask & ~BR_LEARNING;
  1028. ocelot_port_bridge_flags(ocelot, port, flags);
  1029. }
  1030. static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
  1031. struct net_device *brport_dev,
  1032. struct net_device *bridge_dev,
  1033. struct netlink_ext_ack *extack)
  1034. {
  1035. clock_t ageing_time;
  1036. u8 stp_state;
  1037. ocelot_inherit_brport_flags(ocelot, port, brport_dev);
  1038. stp_state = br_port_get_stp_state(brport_dev);
  1039. ocelot_bridge_stp_state_set(ocelot, port, stp_state);
  1040. ageing_time = br_get_ageing_time(bridge_dev);
  1041. ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
  1042. return ocelot_port_vlan_filtering(ocelot, port,
  1043. br_vlan_enabled(bridge_dev),
  1044. extack);
  1045. }
  1046. static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
  1047. {
  1048. int err;
  1049. err = ocelot_port_vlan_filtering(ocelot, port, false, NULL);
  1050. if (err)
  1051. return err;
  1052. ocelot_clear_brport_flags(ocelot, port);
  1053. ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
  1054. return 0;
  1055. }
  1056. static int ocelot_bridge_num_get(struct ocelot *ocelot,
  1057. const struct net_device *bridge_dev)
  1058. {
  1059. int bridge_num = ocelot_bridge_num_find(ocelot, bridge_dev);
  1060. if (bridge_num < 0) {
  1061. /* First port that offloads this bridge */
  1062. bridge_num = find_first_zero_bit(&ocelot->bridges,
  1063. ocelot->num_phys_ports);
  1064. set_bit(bridge_num, &ocelot->bridges);
  1065. }
  1066. return bridge_num;
  1067. }
  1068. static void ocelot_bridge_num_put(struct ocelot *ocelot,
  1069. const struct net_device *bridge_dev,
  1070. int bridge_num)
  1071. {
  1072. /* Check if the bridge is still in use, otherwise it is time
  1073. * to clean it up so we can reuse this bridge_num later.
  1074. */
  1075. if (!ocelot_bridge_num_find(ocelot, bridge_dev))
  1076. clear_bit(bridge_num, &ocelot->bridges);
  1077. }
  1078. static int ocelot_netdevice_bridge_join(struct net_device *dev,
  1079. struct net_device *brport_dev,
  1080. struct net_device *bridge,
  1081. struct netlink_ext_ack *extack)
  1082. {
  1083. struct ocelot_port_private *priv = netdev_priv(dev);
  1084. struct ocelot_port *ocelot_port = &priv->port;
  1085. struct ocelot *ocelot = ocelot_port->ocelot;
  1086. int port = priv->port.index;
  1087. int bridge_num, err;
  1088. bridge_num = ocelot_bridge_num_get(ocelot, bridge);
  1089. err = ocelot_port_bridge_join(ocelot, port, bridge, bridge_num,
  1090. extack);
  1091. if (err)
  1092. goto err_join;
  1093. err = switchdev_bridge_port_offload(brport_dev, dev, priv,
  1094. &ocelot_switchdev_nb,
  1095. &ocelot_switchdev_blocking_nb,
  1096. false, extack);
  1097. if (err)
  1098. goto err_switchdev_offload;
  1099. err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
  1100. if (err)
  1101. goto err_switchdev_sync;
  1102. return 0;
  1103. err_switchdev_sync:
  1104. switchdev_bridge_port_unoffload(brport_dev, priv,
  1105. &ocelot_switchdev_nb,
  1106. &ocelot_switchdev_blocking_nb);
  1107. err_switchdev_offload:
  1108. ocelot_port_bridge_leave(ocelot, port, bridge);
  1109. err_join:
  1110. ocelot_bridge_num_put(ocelot, bridge, bridge_num);
  1111. return err;
  1112. }
  1113. static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
  1114. struct net_device *brport_dev)
  1115. {
  1116. struct ocelot_port_private *priv = netdev_priv(dev);
  1117. switchdev_bridge_port_unoffload(brport_dev, priv,
  1118. &ocelot_switchdev_nb,
  1119. &ocelot_switchdev_blocking_nb);
  1120. }
  1121. static int ocelot_netdevice_bridge_leave(struct net_device *dev,
  1122. struct net_device *brport_dev,
  1123. struct net_device *bridge)
  1124. {
  1125. struct ocelot_port_private *priv = netdev_priv(dev);
  1126. struct ocelot_port *ocelot_port = &priv->port;
  1127. struct ocelot *ocelot = ocelot_port->ocelot;
  1128. int bridge_num = ocelot_port->bridge_num;
  1129. int port = priv->port.index;
  1130. int err;
  1131. err = ocelot_switchdev_unsync(ocelot, port);
  1132. if (err)
  1133. return err;
  1134. ocelot_port_bridge_leave(ocelot, port, bridge);
  1135. ocelot_bridge_num_put(ocelot, bridge, bridge_num);
  1136. return 0;
  1137. }
  1138. static int ocelot_netdevice_lag_join(struct net_device *dev,
  1139. struct net_device *bond,
  1140. struct netdev_lag_upper_info *info,
  1141. struct netlink_ext_ack *extack)
  1142. {
  1143. struct ocelot_port_private *priv = netdev_priv(dev);
  1144. struct ocelot_port *ocelot_port = &priv->port;
  1145. struct ocelot *ocelot = ocelot_port->ocelot;
  1146. struct net_device *bridge_dev;
  1147. int port = priv->port.index;
  1148. int err;
  1149. err = ocelot_port_lag_join(ocelot, port, bond, info, extack);
  1150. if (err == -EOPNOTSUPP)
  1151. /* Offloading not supported, fall back to software LAG */
  1152. return 0;
  1153. bridge_dev = netdev_master_upper_dev_get(bond);
  1154. if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
  1155. return 0;
  1156. err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
  1157. if (err)
  1158. goto err_bridge_join;
  1159. return 0;
  1160. err_bridge_join:
  1161. ocelot_port_lag_leave(ocelot, port, bond);
  1162. return err;
  1163. }
  1164. static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
  1165. struct net_device *bond)
  1166. {
  1167. struct net_device *bridge_dev;
  1168. bridge_dev = netdev_master_upper_dev_get(bond);
  1169. if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
  1170. return;
  1171. ocelot_netdevice_pre_bridge_leave(dev, bond);
  1172. }
  1173. static int ocelot_netdevice_lag_leave(struct net_device *dev,
  1174. struct net_device *bond)
  1175. {
  1176. struct ocelot_port_private *priv = netdev_priv(dev);
  1177. struct ocelot_port *ocelot_port = &priv->port;
  1178. struct ocelot *ocelot = ocelot_port->ocelot;
  1179. struct net_device *bridge_dev;
  1180. int port = priv->port.index;
  1181. ocelot_port_lag_leave(ocelot, port, bond);
  1182. bridge_dev = netdev_master_upper_dev_get(bond);
  1183. if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
  1184. return 0;
  1185. return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
  1186. }
  1187. static int ocelot_netdevice_changeupper(struct net_device *dev,
  1188. struct net_device *brport_dev,
  1189. struct netdev_notifier_changeupper_info *info)
  1190. {
  1191. struct netlink_ext_ack *extack;
  1192. int err = 0;
  1193. extack = netdev_notifier_info_to_extack(&info->info);
  1194. if (netif_is_bridge_master(info->upper_dev)) {
  1195. if (info->linking)
  1196. err = ocelot_netdevice_bridge_join(dev, brport_dev,
  1197. info->upper_dev,
  1198. extack);
  1199. else
  1200. err = ocelot_netdevice_bridge_leave(dev, brport_dev,
  1201. info->upper_dev);
  1202. }
  1203. if (netif_is_lag_master(info->upper_dev)) {
  1204. if (info->linking)
  1205. err = ocelot_netdevice_lag_join(dev, info->upper_dev,
  1206. info->upper_info, extack);
  1207. else
  1208. ocelot_netdevice_lag_leave(dev, info->upper_dev);
  1209. }
  1210. return notifier_from_errno(err);
  1211. }
  1212. /* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
  1213. * events for the lower physical ports of the LAG.
  1214. * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
  1215. * In case the LAG joined a bridge, notify that we are offloading it and can do
  1216. * forwarding in hardware towards it.
  1217. */
  1218. static int
  1219. ocelot_netdevice_lag_changeupper(struct net_device *dev,
  1220. struct netdev_notifier_changeupper_info *info)
  1221. {
  1222. struct net_device *lower;
  1223. struct list_head *iter;
  1224. int err = NOTIFY_DONE;
  1225. netdev_for_each_lower_dev(dev, lower, iter) {
  1226. struct ocelot_port_private *priv = netdev_priv(lower);
  1227. struct ocelot_port *ocelot_port = &priv->port;
  1228. if (ocelot_port->bond != dev)
  1229. return NOTIFY_OK;
  1230. err = ocelot_netdevice_changeupper(lower, dev, info);
  1231. if (err)
  1232. return notifier_from_errno(err);
  1233. }
  1234. return NOTIFY_DONE;
  1235. }
  1236. static int
  1237. ocelot_netdevice_prechangeupper(struct net_device *dev,
  1238. struct net_device *brport_dev,
  1239. struct netdev_notifier_changeupper_info *info)
  1240. {
  1241. if (netif_is_bridge_master(info->upper_dev) && !info->linking)
  1242. ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
  1243. if (netif_is_lag_master(info->upper_dev) && !info->linking)
  1244. ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
  1245. return NOTIFY_DONE;
  1246. }
  1247. static int
  1248. ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
  1249. struct netdev_notifier_changeupper_info *info)
  1250. {
  1251. struct net_device *lower;
  1252. struct list_head *iter;
  1253. int err = NOTIFY_DONE;
  1254. netdev_for_each_lower_dev(dev, lower, iter) {
  1255. struct ocelot_port_private *priv = netdev_priv(lower);
  1256. struct ocelot_port *ocelot_port = &priv->port;
  1257. if (ocelot_port->bond != dev)
  1258. return NOTIFY_OK;
  1259. err = ocelot_netdevice_prechangeupper(dev, lower, info);
  1260. if (err)
  1261. return err;
  1262. }
  1263. return NOTIFY_DONE;
  1264. }
  1265. static int
  1266. ocelot_netdevice_changelowerstate(struct net_device *dev,
  1267. struct netdev_lag_lower_state_info *info)
  1268. {
  1269. struct ocelot_port_private *priv = netdev_priv(dev);
  1270. bool is_active = info->link_up && info->tx_enabled;
  1271. struct ocelot_port *ocelot_port = &priv->port;
  1272. struct ocelot *ocelot = ocelot_port->ocelot;
  1273. int port = priv->port.index;
  1274. if (!ocelot_port->bond)
  1275. return NOTIFY_DONE;
  1276. if (ocelot_port->lag_tx_active == is_active)
  1277. return NOTIFY_DONE;
  1278. ocelot_port_lag_change(ocelot, port, is_active);
  1279. return NOTIFY_OK;
  1280. }
  1281. static int ocelot_netdevice_event(struct notifier_block *unused,
  1282. unsigned long event, void *ptr)
  1283. {
  1284. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1285. switch (event) {
  1286. case NETDEV_PRECHANGEUPPER: {
  1287. struct netdev_notifier_changeupper_info *info = ptr;
  1288. if (ocelot_netdevice_dev_check(dev))
  1289. return ocelot_netdevice_prechangeupper(dev, dev, info);
  1290. if (netif_is_lag_master(dev))
  1291. return ocelot_netdevice_lag_prechangeupper(dev, info);
  1292. break;
  1293. }
  1294. case NETDEV_CHANGEUPPER: {
  1295. struct netdev_notifier_changeupper_info *info = ptr;
  1296. if (ocelot_netdevice_dev_check(dev))
  1297. return ocelot_netdevice_changeupper(dev, dev, info);
  1298. if (netif_is_lag_master(dev))
  1299. return ocelot_netdevice_lag_changeupper(dev, info);
  1300. break;
  1301. }
  1302. case NETDEV_CHANGELOWERSTATE: {
  1303. struct netdev_notifier_changelowerstate_info *info = ptr;
  1304. if (!ocelot_netdevice_dev_check(dev))
  1305. break;
  1306. return ocelot_netdevice_changelowerstate(dev,
  1307. info->lower_state_info);
  1308. }
  1309. default:
  1310. break;
  1311. }
  1312. return NOTIFY_DONE;
  1313. }
  1314. struct notifier_block ocelot_netdevice_nb __read_mostly = {
  1315. .notifier_call = ocelot_netdevice_event,
  1316. };
  1317. static int ocelot_switchdev_event(struct notifier_block *unused,
  1318. unsigned long event, void *ptr)
  1319. {
  1320. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1321. int err;
  1322. switch (event) {
  1323. case SWITCHDEV_PORT_ATTR_SET:
  1324. err = switchdev_handle_port_attr_set(dev, ptr,
  1325. ocelot_netdevice_dev_check,
  1326. ocelot_port_attr_set);
  1327. return notifier_from_errno(err);
  1328. }
  1329. return NOTIFY_DONE;
  1330. }
  1331. struct notifier_block ocelot_switchdev_nb __read_mostly = {
  1332. .notifier_call = ocelot_switchdev_event,
  1333. };
  1334. static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
  1335. unsigned long event, void *ptr)
  1336. {
  1337. struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
  1338. int err;
  1339. switch (event) {
  1340. /* Blocking events. */
  1341. case SWITCHDEV_PORT_OBJ_ADD:
  1342. err = switchdev_handle_port_obj_add(dev, ptr,
  1343. ocelot_netdevice_dev_check,
  1344. ocelot_port_obj_add);
  1345. return notifier_from_errno(err);
  1346. case SWITCHDEV_PORT_OBJ_DEL:
  1347. err = switchdev_handle_port_obj_del(dev, ptr,
  1348. ocelot_netdevice_dev_check,
  1349. ocelot_port_obj_del);
  1350. return notifier_from_errno(err);
  1351. case SWITCHDEV_PORT_ATTR_SET:
  1352. err = switchdev_handle_port_attr_set(dev, ptr,
  1353. ocelot_netdevice_dev_check,
  1354. ocelot_port_attr_set);
  1355. return notifier_from_errno(err);
  1356. }
  1357. return NOTIFY_DONE;
  1358. }
  1359. struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
  1360. .notifier_call = ocelot_switchdev_blocking_event,
  1361. };
  1362. static void vsc7514_phylink_mac_config(struct phylink_config *config,
  1363. unsigned int link_an_mode,
  1364. const struct phylink_link_state *state)
  1365. {
  1366. struct net_device *ndev = to_net_dev(config->dev);
  1367. struct ocelot_port_private *priv = netdev_priv(ndev);
  1368. struct ocelot *ocelot = priv->port.ocelot;
  1369. int port = priv->port.index;
  1370. ocelot_phylink_mac_config(ocelot, port, link_an_mode, state);
  1371. }
  1372. static void vsc7514_phylink_mac_link_down(struct phylink_config *config,
  1373. unsigned int link_an_mode,
  1374. phy_interface_t interface)
  1375. {
  1376. struct net_device *ndev = to_net_dev(config->dev);
  1377. struct ocelot_port_private *priv = netdev_priv(ndev);
  1378. struct ocelot *ocelot = priv->port.ocelot;
  1379. int port = priv->port.index;
  1380. ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
  1381. OCELOT_MAC_QUIRKS);
  1382. }
  1383. static void vsc7514_phylink_mac_link_up(struct phylink_config *config,
  1384. struct phy_device *phydev,
  1385. unsigned int link_an_mode,
  1386. phy_interface_t interface,
  1387. int speed, int duplex,
  1388. bool tx_pause, bool rx_pause)
  1389. {
  1390. struct net_device *ndev = to_net_dev(config->dev);
  1391. struct ocelot_port_private *priv = netdev_priv(ndev);
  1392. struct ocelot *ocelot = priv->port.ocelot;
  1393. int port = priv->port.index;
  1394. ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
  1395. interface, speed, duplex,
  1396. tx_pause, rx_pause, OCELOT_MAC_QUIRKS);
  1397. }
  1398. static const struct phylink_mac_ops ocelot_phylink_ops = {
  1399. .mac_config = vsc7514_phylink_mac_config,
  1400. .mac_link_down = vsc7514_phylink_mac_link_down,
  1401. .mac_link_up = vsc7514_phylink_mac_link_up,
  1402. };
  1403. static int ocelot_port_phylink_create(struct ocelot *ocelot, int port,
  1404. struct device_node *portnp)
  1405. {
  1406. struct ocelot_port *ocelot_port = ocelot->ports[port];
  1407. struct ocelot_port_private *priv;
  1408. struct device *dev = ocelot->dev;
  1409. phy_interface_t phy_mode;
  1410. struct phylink *phylink;
  1411. int err;
  1412. of_get_phy_mode(portnp, &phy_mode);
  1413. /* DT bindings of internal PHY ports are broken and don't
  1414. * specify a phy-mode
  1415. */
  1416. if (phy_mode == PHY_INTERFACE_MODE_NA)
  1417. phy_mode = PHY_INTERFACE_MODE_INTERNAL;
  1418. if (phy_mode != PHY_INTERFACE_MODE_SGMII &&
  1419. phy_mode != PHY_INTERFACE_MODE_QSGMII &&
  1420. phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
  1421. dev_err(dev, "unsupported phy mode %s for port %d\n",
  1422. phy_modes(phy_mode), port);
  1423. return -EINVAL;
  1424. }
  1425. ocelot_port->phy_mode = phy_mode;
  1426. err = ocelot_port_configure_serdes(ocelot, port, portnp);
  1427. if (err)
  1428. return err;
  1429. priv = container_of(ocelot_port, struct ocelot_port_private, port);
  1430. priv->phylink_config.dev = &priv->dev->dev;
  1431. priv->phylink_config.type = PHYLINK_NETDEV;
  1432. priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
  1433. MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
  1434. __set_bit(ocelot_port->phy_mode,
  1435. priv->phylink_config.supported_interfaces);
  1436. phylink = phylink_create(&priv->phylink_config,
  1437. of_fwnode_handle(portnp),
  1438. phy_mode, &ocelot_phylink_ops);
  1439. if (IS_ERR(phylink)) {
  1440. err = PTR_ERR(phylink);
  1441. dev_err(dev, "Could not create phylink (%pe)\n", phylink);
  1442. return err;
  1443. }
  1444. priv->phylink = phylink;
  1445. err = phylink_of_phy_connect(phylink, portnp, 0);
  1446. if (err) {
  1447. dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err));
  1448. phylink_destroy(phylink);
  1449. priv->phylink = NULL;
  1450. return err;
  1451. }
  1452. return 0;
  1453. }
  1454. int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
  1455. struct device_node *portnp)
  1456. {
  1457. struct ocelot_port_private *priv;
  1458. struct ocelot_port *ocelot_port;
  1459. struct net_device *dev;
  1460. int err;
  1461. dev = alloc_etherdev(sizeof(struct ocelot_port_private));
  1462. if (!dev)
  1463. return -ENOMEM;
  1464. SET_NETDEV_DEV(dev, ocelot->dev);
  1465. priv = netdev_priv(dev);
  1466. priv->dev = dev;
  1467. ocelot_port = &priv->port;
  1468. ocelot_port->ocelot = ocelot;
  1469. ocelot_port->index = port;
  1470. ocelot_port->target = target;
  1471. ocelot->ports[port] = ocelot_port;
  1472. dev->netdev_ops = &ocelot_port_netdev_ops;
  1473. dev->ethtool_ops = &ocelot_ethtool_ops;
  1474. dev->max_mtu = OCELOT_JUMBO_MTU;
  1475. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
  1476. NETIF_F_HW_TC;
  1477. dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
  1478. err = of_get_ethdev_address(portnp, dev);
  1479. if (err)
  1480. eth_hw_addr_gen(dev, ocelot->base_mac, port);
  1481. ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
  1482. OCELOT_STANDALONE_PVID, ENTRYTYPE_LOCKED);
  1483. ocelot_init_port(ocelot, port);
  1484. err = ocelot_port_phylink_create(ocelot, port, portnp);
  1485. if (err)
  1486. goto out;
  1487. if (ocelot->fdma)
  1488. ocelot_fdma_netdev_init(ocelot, dev);
  1489. SET_NETDEV_DEVLINK_PORT(dev, &ocelot->devlink_ports[port]);
  1490. err = register_netdev(dev);
  1491. if (err) {
  1492. dev_err(ocelot->dev, "register_netdev failed\n");
  1493. goto out_fdma_deinit;
  1494. }
  1495. return 0;
  1496. out_fdma_deinit:
  1497. if (ocelot->fdma)
  1498. ocelot_fdma_netdev_deinit(ocelot, dev);
  1499. out:
  1500. ocelot->ports[port] = NULL;
  1501. free_netdev(dev);
  1502. return err;
  1503. }
  1504. void ocelot_release_port(struct ocelot_port *ocelot_port)
  1505. {
  1506. struct ocelot_port_private *priv = container_of(ocelot_port,
  1507. struct ocelot_port_private,
  1508. port);
  1509. struct ocelot *ocelot = ocelot_port->ocelot;
  1510. struct ocelot_fdma *fdma = ocelot->fdma;
  1511. unregister_netdev(priv->dev);
  1512. if (fdma)
  1513. ocelot_fdma_netdev_deinit(ocelot, priv->dev);
  1514. if (priv->phylink) {
  1515. rtnl_lock();
  1516. phylink_disconnect_phy(priv->phylink);
  1517. rtnl_unlock();
  1518. phylink_destroy(priv->phylink);
  1519. }
  1520. free_netdev(priv->dev);
  1521. }