1 /* 2 * Copyright (c) 2014-2015 Hisilicon Limited. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 */ 9 10 #include <linux/acpi.h> 11 #include <linux/init.h> 12 #include <linux/interrupt.h> 13 #include <linux/kernel.h> 14 #include <linux/mfd/syscon.h> 15 #include <linux/module.h> 16 #include <linux/netdevice.h> 17 #include <linux/of.h> 18 #include <linux/of_address.h> 19 #include <linux/of_mdio.h> 20 #include <linux/phy.h> 21 #include <linux/platform_device.h> 22 23 #include "hns_dsaf_main.h" 24 #include "hns_dsaf_misc.h" 25 #include "hns_dsaf_rcb.h" 26 27 #define MAC_EN_FLAG_V 0xada0328 28 29 static const u16 mac_phy_to_speed[] = { 30 [PHY_INTERFACE_MODE_MII] = MAC_SPEED_100, 31 [PHY_INTERFACE_MODE_GMII] = MAC_SPEED_1000, 32 [PHY_INTERFACE_MODE_SGMII] = MAC_SPEED_1000, 33 [PHY_INTERFACE_MODE_TBI] = MAC_SPEED_1000, 34 [PHY_INTERFACE_MODE_RMII] = MAC_SPEED_100, 35 [PHY_INTERFACE_MODE_RGMII] = MAC_SPEED_1000, 36 [PHY_INTERFACE_MODE_RGMII_ID] = MAC_SPEED_1000, 37 [PHY_INTERFACE_MODE_RGMII_RXID] = MAC_SPEED_1000, 38 [PHY_INTERFACE_MODE_RGMII_TXID] = MAC_SPEED_1000, 39 [PHY_INTERFACE_MODE_RTBI] = MAC_SPEED_1000, 40 [PHY_INTERFACE_MODE_XGMII] = MAC_SPEED_10000 41 }; 42 43 static const enum mac_mode g_mac_mode_100[] = { 44 [PHY_INTERFACE_MODE_MII] = MAC_MODE_MII_100, 45 [PHY_INTERFACE_MODE_RMII] = MAC_MODE_RMII_100 46 }; 47 48 static const enum mac_mode g_mac_mode_1000[] = { 49 [PHY_INTERFACE_MODE_GMII] = MAC_MODE_GMII_1000, 50 [PHY_INTERFACE_MODE_SGMII] = MAC_MODE_SGMII_1000, 51 [PHY_INTERFACE_MODE_TBI] = MAC_MODE_TBI_1000, 52 [PHY_INTERFACE_MODE_RGMII] = MAC_MODE_RGMII_1000, 53 [PHY_INTERFACE_MODE_RGMII_ID] = MAC_MODE_RGMII_1000, 54 [PHY_INTERFACE_MODE_RGMII_RXID] = MAC_MODE_RGMII_1000, 55 [PHY_INTERFACE_MODE_RGMII_TXID] = MAC_MODE_RGMII_1000, 56 [PHY_INTERFACE_MODE_RTBI] = MAC_MODE_RTBI_1000 57 }; 58 59 static enum mac_mode hns_get_enet_interface(const struct hns_mac_cb *mac_cb) 60 { 61 switch (mac_cb->max_speed) { 62 case MAC_SPEED_100: 63 return g_mac_mode_100[mac_cb->phy_if]; 64 case MAC_SPEED_1000: 65 return g_mac_mode_1000[mac_cb->phy_if]; 66 case MAC_SPEED_10000: 67 return MAC_MODE_XGMII_10000; 68 default: 69 return MAC_MODE_MII_100; 70 } 71 } 72 73 void hns_mac_get_link_status(struct hns_mac_cb *mac_cb, u32 *link_status) 74 { 75 struct mac_driver *mac_ctrl_drv; 76 int ret, sfp_prsnt; 77 78 mac_ctrl_drv = hns_mac_get_drv(mac_cb); 79 80 if (mac_ctrl_drv->get_link_status) 81 mac_ctrl_drv->get_link_status(mac_ctrl_drv, link_status); 82 else 83 *link_status = 0; 84 85 ret = mac_cb->dsaf_dev->misc_op->get_sfp_prsnt(mac_cb, &sfp_prsnt); 86 if (!ret) 87 *link_status = *link_status && sfp_prsnt; 88 89 mac_cb->link = *link_status; 90 } 91 92 int hns_mac_get_port_info(struct hns_mac_cb *mac_cb, 93 u8 *auto_neg, u16 *speed, u8 *duplex) 94 { 95 struct mac_driver *mac_ctrl_drv; 96 struct mac_info info; 97 98 mac_ctrl_drv = hns_mac_get_drv(mac_cb); 99 100 if (!mac_ctrl_drv->get_info) 101 return -ENODEV; 102 103 mac_ctrl_drv->get_info(mac_ctrl_drv, &info); 104 if (auto_neg) 105 *auto_neg = info.auto_neg; 106 if (speed) 107 *speed = info.speed; 108 if (duplex) 109 *duplex = info.duplex; 110 111 return 0; 112 } 113 114 void hns_mac_adjust_link(struct hns_mac_cb *mac_cb, int speed, int duplex) 115 { 116 int ret; 117 struct mac_driver *mac_ctrl_drv; 118 119 mac_ctrl_drv = (struct mac_driver *)(mac_cb->priv.mac); 120 121 mac_cb->speed = speed; 122 mac_cb->half_duplex = !duplex; 123 124 if (mac_ctrl_drv->adjust_link) { 125 ret = mac_ctrl_drv->adjust_link(mac_ctrl_drv, 126 (enum mac_speed)speed, duplex); 127 if (ret) { 128 dev_err(mac_cb->dev, 129 "adjust_link failed,%s mac%d ret = %#x!\n", 130 mac_cb->dsaf_dev->ae_dev.name, 131 mac_cb->mac_id, ret); 132 return; 133 } 134 } 135 } 136 137 /** 138 *hns_mac_get_inner_port_num - get mac table inner port number 139 *@mac_cb: mac device 140 *@vmid: vm id 141 *@port_num:port number 142 * 143 */ 144 static int hns_mac_get_inner_port_num(struct hns_mac_cb *mac_cb, 145 u8 vmid, u8 *port_num) 146 { 147 u8 tmp_port; 148 149 if (mac_cb->dsaf_dev->dsaf_mode <= DSAF_MODE_ENABLE) { 150 if (mac_cb->mac_id != DSAF_MAX_PORT_NUM) { 151 dev_err(mac_cb->dev, 152 "input invalid,%s mac%d vmid%d !\n", 153 mac_cb->dsaf_dev->ae_dev.name, 154 mac_cb->mac_id, vmid); 155 return -EINVAL; 156 } 157 } else if (mac_cb->dsaf_dev->dsaf_mode < DSAF_MODE_MAX) { 158 if (mac_cb->mac_id >= DSAF_MAX_PORT_NUM) { 159 dev_err(mac_cb->dev, 160 "input invalid,%s mac%d vmid%d!\n", 161 mac_cb->dsaf_dev->ae_dev.name, 162 mac_cb->mac_id, vmid); 163 return -EINVAL; 164 } 165 } else { 166 dev_err(mac_cb->dev, "dsaf mode invalid,%s mac%d!\n", 167 mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id); 168 return -EINVAL; 169 } 170 171 if (vmid >= mac_cb->dsaf_dev->rcb_common[0]->max_vfn) { 172 dev_err(mac_cb->dev, "input invalid,%s mac%d vmid%d !\n", 173 mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id, vmid); 174 return -EINVAL; 175 } 176 177 switch (mac_cb->dsaf_dev->dsaf_mode) { 178 case DSAF_MODE_ENABLE_FIX: 179 tmp_port = 0; 180 break; 181 case DSAF_MODE_DISABLE_FIX: 182 tmp_port = 0; 183 break; 184 case DSAF_MODE_ENABLE_0VM: 185 case DSAF_MODE_ENABLE_8VM: 186 case DSAF_MODE_ENABLE_16VM: 187 case DSAF_MODE_ENABLE_32VM: 188 case DSAF_MODE_ENABLE_128VM: 189 case DSAF_MODE_DISABLE_2PORT_8VM: 190 case DSAF_MODE_DISABLE_2PORT_16VM: 191 case DSAF_MODE_DISABLE_2PORT_64VM: 192 case DSAF_MODE_DISABLE_6PORT_0VM: 193 case DSAF_MODE_DISABLE_6PORT_2VM: 194 case DSAF_MODE_DISABLE_6PORT_4VM: 195 case DSAF_MODE_DISABLE_6PORT_16VM: 196 tmp_port = vmid; 197 break; 198 default: 199 dev_err(mac_cb->dev, "dsaf mode invalid,%s mac%d!\n", 200 mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id); 201 return -EINVAL; 202 } 203 tmp_port += DSAF_BASE_INNER_PORT_NUM; 204 205 *port_num = tmp_port; 206 207 return 0; 208 } 209 210 /** 211 *hns_mac_change_vf_addr - change vf mac address 212 *@mac_cb: mac device 213 *@vmid: vmid 214 *@addr:mac address 215 */ 216 int hns_mac_change_vf_addr(struct hns_mac_cb *mac_cb, 217 u32 vmid, char *addr) 218 { 219 int ret; 220 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 221 struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; 222 struct dsaf_drv_mac_single_dest_entry mac_entry; 223 struct mac_entry_idx *old_entry; 224 225 old_entry = &mac_cb->addr_entry_idx[vmid]; 226 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) { 227 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); 228 mac_entry.in_vlan_id = old_entry->vlan_id; 229 mac_entry.in_port_num = mac_cb->mac_id; 230 ret = hns_mac_get_inner_port_num(mac_cb, (u8)vmid, 231 &mac_entry.port_num); 232 if (ret) 233 return ret; 234 235 if ((old_entry->valid != 0) && 236 (memcmp(old_entry->addr, 237 addr, sizeof(mac_entry.addr)) != 0)) { 238 ret = hns_dsaf_del_mac_entry(dsaf_dev, 239 old_entry->vlan_id, 240 mac_cb->mac_id, 241 old_entry->addr); 242 if (ret) 243 return ret; 244 } 245 246 ret = hns_dsaf_set_mac_uc_entry(dsaf_dev, &mac_entry); 247 if (ret) 248 return ret; 249 } 250 251 if ((mac_ctrl_drv->set_mac_addr) && (vmid == 0)) 252 mac_ctrl_drv->set_mac_addr(mac_cb->priv.mac, addr); 253 254 memcpy(old_entry->addr, addr, sizeof(old_entry->addr)); 255 old_entry->valid = 1; 256 return 0; 257 } 258 259 int hns_mac_set_multi(struct hns_mac_cb *mac_cb, 260 u32 port_num, char *addr, bool enable) 261 { 262 int ret; 263 struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; 264 struct dsaf_drv_mac_single_dest_entry mac_entry; 265 266 if (!HNS_DSAF_IS_DEBUG(dsaf_dev) && addr) { 267 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); 268 mac_entry.in_vlan_id = 0;/*vlan_id;*/ 269 mac_entry.in_port_num = mac_cb->mac_id; 270 mac_entry.port_num = port_num; 271 272 if (!enable) 273 ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry); 274 else 275 ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry); 276 if (ret) { 277 dev_err(dsaf_dev->dev, 278 "set mac mc port failed,%s mac%d ret = %#x!\n", 279 mac_cb->dsaf_dev->ae_dev.name, 280 mac_cb->mac_id, ret); 281 return ret; 282 } 283 } 284 285 return 0; 286 } 287 288 /** 289 *hns_mac_del_mac - delete mac address into dsaf table,can't delete the same 290 * address twice 291 *@net_dev: net device 292 *@vfn : vf lan 293 *@mac : mac address 294 *return status 295 */ 296 int hns_mac_del_mac(struct hns_mac_cb *mac_cb, u32 vfn, char *mac) 297 { 298 struct mac_entry_idx *old_mac; 299 struct dsaf_device *dsaf_dev; 300 u32 ret; 301 302 dsaf_dev = mac_cb->dsaf_dev; 303 304 if (vfn < DSAF_MAX_VM_NUM) { 305 old_mac = &mac_cb->addr_entry_idx[vfn]; 306 } else { 307 dev_err(mac_cb->dev, 308 "vf queue is too large,%s mac%d queue = %#x!\n", 309 mac_cb->dsaf_dev->ae_dev.name, mac_cb->mac_id, vfn); 310 return -EINVAL; 311 } 312 313 if (dsaf_dev) { 314 ret = hns_dsaf_del_mac_entry(dsaf_dev, old_mac->vlan_id, 315 mac_cb->mac_id, old_mac->addr); 316 if (ret) 317 return ret; 318 319 if (memcmp(old_mac->addr, mac, sizeof(old_mac->addr)) == 0) 320 old_mac->valid = 0; 321 } 322 323 return 0; 324 } 325 326 static void hns_mac_param_get(struct mac_params *param, 327 struct hns_mac_cb *mac_cb) 328 { 329 param->vaddr = (void *)mac_cb->vaddr; 330 param->mac_mode = hns_get_enet_interface(mac_cb); 331 memcpy(param->addr, mac_cb->addr_entry_idx[0].addr, 332 MAC_NUM_OCTETS_PER_ADDR); 333 param->mac_id = mac_cb->mac_id; 334 param->dev = mac_cb->dev; 335 } 336 337 /** 338 *hns_mac_queue_config_bc_en - set broadcast rx&tx enable 339 *@mac_cb: mac device 340 *@queue: queue number 341 *@en:enable 342 *retuen 0 - success , negative --fail 343 */ 344 static int hns_mac_port_config_bc_en(struct hns_mac_cb *mac_cb, 345 u32 port_num, u16 vlan_id, bool enable) 346 { 347 int ret; 348 struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; 349 u8 addr[MAC_NUM_OCTETS_PER_ADDR] 350 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 351 struct dsaf_drv_mac_single_dest_entry mac_entry; 352 353 /* directy return ok in debug network mode */ 354 if (mac_cb->mac_type == HNAE_PORT_DEBUG) 355 return 0; 356 357 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) { 358 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); 359 mac_entry.in_vlan_id = vlan_id; 360 mac_entry.in_port_num = mac_cb->mac_id; 361 mac_entry.port_num = port_num; 362 363 if (!enable) 364 ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry); 365 else 366 ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry); 367 return ret; 368 } 369 370 return 0; 371 } 372 373 /** 374 *hns_mac_vm_config_bc_en - set broadcast rx&tx enable 375 *@mac_cb: mac device 376 *@vmid: vm id 377 *@en:enable 378 *retuen 0 - success , negative --fail 379 */ 380 int hns_mac_vm_config_bc_en(struct hns_mac_cb *mac_cb, u32 vmid, bool enable) 381 { 382 int ret; 383 struct dsaf_device *dsaf_dev = mac_cb->dsaf_dev; 384 u8 port_num; 385 u8 addr[MAC_NUM_OCTETS_PER_ADDR] 386 = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; 387 struct mac_entry_idx *uc_mac_entry; 388 struct dsaf_drv_mac_single_dest_entry mac_entry; 389 390 if (mac_cb->mac_type == HNAE_PORT_DEBUG) 391 return 0; 392 393 uc_mac_entry = &mac_cb->addr_entry_idx[vmid]; 394 395 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) { 396 memcpy(mac_entry.addr, addr, sizeof(mac_entry.addr)); 397 mac_entry.in_vlan_id = uc_mac_entry->vlan_id; 398 mac_entry.in_port_num = mac_cb->mac_id; 399 ret = hns_mac_get_inner_port_num(mac_cb, vmid, &port_num); 400 if (ret) 401 return ret; 402 mac_entry.port_num = port_num; 403 404 if (!enable) 405 ret = hns_dsaf_del_mac_mc_port(dsaf_dev, &mac_entry); 406 else 407 ret = hns_dsaf_add_mac_mc_port(dsaf_dev, &mac_entry); 408 return ret; 409 } 410 411 return 0; 412 } 413 414 void hns_mac_reset(struct hns_mac_cb *mac_cb) 415 { 416 struct mac_driver *drv = hns_mac_get_drv(mac_cb); 417 bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver); 418 419 drv->mac_init(drv); 420 421 if (drv->config_max_frame_length) 422 drv->config_max_frame_length(drv, mac_cb->max_frm); 423 424 if (drv->set_tx_auto_pause_frames) 425 drv->set_tx_auto_pause_frames(drv, mac_cb->tx_pause_frm_time); 426 427 if (drv->set_an_mode) 428 drv->set_an_mode(drv, 1); 429 430 if (drv->mac_pausefrm_cfg) { 431 if (mac_cb->mac_type == HNAE_PORT_DEBUG) 432 drv->mac_pausefrm_cfg(drv, !is_ver1, !is_ver1); 433 else /* mac rx must disable, dsaf pfc close instead of it*/ 434 drv->mac_pausefrm_cfg(drv, 0, 1); 435 } 436 } 437 438 int hns_mac_set_mtu(struct hns_mac_cb *mac_cb, u32 new_mtu) 439 { 440 struct mac_driver *drv = hns_mac_get_drv(mac_cb); 441 u32 buf_size = mac_cb->dsaf_dev->buf_size; 442 u32 new_frm = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN; 443 u32 max_frm = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver) ? 444 MAC_MAX_MTU : MAC_MAX_MTU_V2; 445 446 if (mac_cb->mac_type == HNAE_PORT_DEBUG) 447 max_frm = MAC_MAX_MTU_DBG; 448 449 if ((new_mtu < MAC_MIN_MTU) || (new_frm > max_frm) || 450 (new_frm > HNS_RCB_RING_MAX_BD_PER_PKT * buf_size)) 451 return -EINVAL; 452 453 if (!drv->config_max_frame_length) 454 return -ECHILD; 455 456 /* adjust max frame to be at least the size of a standard frame */ 457 if (new_frm < (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN)) 458 new_frm = (ETH_FRAME_LEN + ETH_FCS_LEN + VLAN_HLEN); 459 460 drv->config_max_frame_length(drv, new_frm); 461 462 mac_cb->max_frm = new_frm; 463 464 return 0; 465 } 466 467 void hns_mac_start(struct hns_mac_cb *mac_cb) 468 { 469 struct mac_driver *mac_drv = hns_mac_get_drv(mac_cb); 470 471 /* for virt */ 472 if (mac_drv->mac_en_flg == MAC_EN_FLAG_V) { 473 /*plus 1 when the virtual mac has been enabled */ 474 mac_drv->virt_dev_num += 1; 475 return; 476 } 477 478 if (mac_drv->mac_enable) { 479 mac_drv->mac_enable(mac_cb->priv.mac, MAC_COMM_MODE_RX_AND_TX); 480 mac_drv->mac_en_flg = MAC_EN_FLAG_V; 481 } 482 } 483 484 void hns_mac_stop(struct hns_mac_cb *mac_cb) 485 { 486 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 487 488 /*modified for virtualization */ 489 if (mac_ctrl_drv->virt_dev_num > 0) { 490 mac_ctrl_drv->virt_dev_num -= 1; 491 if (mac_ctrl_drv->virt_dev_num > 0) 492 return; 493 } 494 495 if (mac_ctrl_drv->mac_disable) 496 mac_ctrl_drv->mac_disable(mac_cb->priv.mac, 497 MAC_COMM_MODE_RX_AND_TX); 498 499 mac_ctrl_drv->mac_en_flg = 0; 500 mac_cb->link = 0; 501 mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb); 502 } 503 504 /** 505 * hns_mac_get_autoneg - get auto autonegotiation 506 * @mac_cb: mac control block 507 * @enable: enable or not 508 * retuen 0 - success , negative --fail 509 */ 510 void hns_mac_get_autoneg(struct hns_mac_cb *mac_cb, u32 *auto_neg) 511 { 512 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 513 514 if (mac_ctrl_drv->autoneg_stat) 515 mac_ctrl_drv->autoneg_stat(mac_ctrl_drv, auto_neg); 516 else 517 *auto_neg = 0; 518 } 519 520 /** 521 * hns_mac_get_pauseparam - set rx & tx pause parameter 522 * @mac_cb: mac control block 523 * @rx_en: rx enable status 524 * @tx_en: tx enable status 525 * retuen 0 - success , negative --fail 526 */ 527 void hns_mac_get_pauseparam(struct hns_mac_cb *mac_cb, u32 *rx_en, u32 *tx_en) 528 { 529 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 530 531 if (mac_ctrl_drv->get_pause_enable) { 532 mac_ctrl_drv->get_pause_enable(mac_ctrl_drv, rx_en, tx_en); 533 } else { 534 *rx_en = 0; 535 *tx_en = 0; 536 } 537 } 538 539 /** 540 * hns_mac_set_autoneg - set auto autonegotiation 541 * @mac_cb: mac control block 542 * @enable: enable or not 543 * retuen 0 - success , negative --fail 544 */ 545 int hns_mac_set_autoneg(struct hns_mac_cb *mac_cb, u8 enable) 546 { 547 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 548 549 if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII && enable) { 550 dev_err(mac_cb->dev, "enable autoneg is not allowed!"); 551 return -ENOTSUPP; 552 } 553 554 if (mac_ctrl_drv->set_an_mode) 555 mac_ctrl_drv->set_an_mode(mac_ctrl_drv, enable); 556 557 return 0; 558 } 559 560 /** 561 * hns_mac_set_autoneg - set rx & tx pause parameter 562 * @mac_cb: mac control block 563 * @rx_en: rx enable or not 564 * @tx_en: tx enable or not 565 * return 0 - success , negative --fail 566 */ 567 int hns_mac_set_pauseparam(struct hns_mac_cb *mac_cb, u32 rx_en, u32 tx_en) 568 { 569 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 570 bool is_ver1 = AE_IS_VER1(mac_cb->dsaf_dev->dsaf_ver); 571 572 if (mac_cb->mac_type == HNAE_PORT_DEBUG) { 573 if (is_ver1 && (tx_en || rx_en)) { 574 dev_err(mac_cb->dev, "macv1 cann't enable tx/rx_pause!"); 575 return -EINVAL; 576 } 577 } 578 579 if (mac_ctrl_drv->mac_pausefrm_cfg) 580 mac_ctrl_drv->mac_pausefrm_cfg(mac_ctrl_drv, rx_en, tx_en); 581 582 return 0; 583 } 584 585 /** 586 * hns_mac_init_ex - mac init 587 * @mac_cb: mac control block 588 * retuen 0 - success , negative --fail 589 */ 590 static int hns_mac_init_ex(struct hns_mac_cb *mac_cb) 591 { 592 int ret; 593 struct mac_params param; 594 struct mac_driver *drv; 595 596 hns_dsaf_fix_mac_mode(mac_cb); 597 598 memset(¶m, 0, sizeof(struct mac_params)); 599 hns_mac_param_get(¶m, mac_cb); 600 601 if (MAC_SPEED_FROM_MODE(param.mac_mode) < MAC_SPEED_10000) 602 drv = (struct mac_driver *)hns_gmac_config(mac_cb, ¶m); 603 else 604 drv = (struct mac_driver *)hns_xgmac_config(mac_cb, ¶m); 605 606 if (!drv) 607 return -ENOMEM; 608 609 mac_cb->priv.mac = (void *)drv; 610 hns_mac_reset(mac_cb); 611 612 hns_mac_adjust_link(mac_cb, mac_cb->speed, !mac_cb->half_duplex); 613 614 ret = hns_mac_port_config_bc_en(mac_cb, mac_cb->mac_id, 0, true); 615 if (ret) 616 goto free_mac_drv; 617 618 return 0; 619 620 free_mac_drv: 621 drv->mac_free(mac_cb->priv.mac); 622 mac_cb->priv.mac = NULL; 623 624 return ret; 625 } 626 627 static int 628 hns_mac_phy_parse_addr(struct device *dev, struct fwnode_handle *fwnode) 629 { 630 u32 addr; 631 int ret; 632 633 ret = fwnode_property_read_u32(fwnode, "phy-addr", &addr); 634 if (ret) { 635 dev_err(dev, "has invalid PHY address ret:%d\n", ret); 636 return ret; 637 } 638 639 if (addr >= PHY_MAX_ADDR) { 640 dev_err(dev, "PHY address %i is too large\n", addr); 641 return -EINVAL; 642 } 643 644 return addr; 645 } 646 647 static int hns_mac_phydev_match(struct device *dev, void *fwnode) 648 { 649 return dev->fwnode == fwnode; 650 } 651 652 static struct 653 platform_device *hns_mac_find_platform_device(struct fwnode_handle *fwnode) 654 { 655 struct device *dev; 656 657 dev = bus_find_device(&platform_bus_type, NULL, 658 fwnode, hns_mac_phydev_match); 659 return dev ? to_platform_device(dev) : NULL; 660 } 661 662 static int 663 hns_mac_register_phydev(struct mii_bus *mdio, struct hns_mac_cb *mac_cb, 664 u32 addr) 665 { 666 struct phy_device *phy; 667 const char *phy_type; 668 bool is_c45; 669 int rc; 670 671 rc = fwnode_property_read_string(mac_cb->fw_port, 672 "phy-mode", &phy_type); 673 if (rc < 0) 674 return rc; 675 676 if (!strcmp(phy_type, phy_modes(PHY_INTERFACE_MODE_XGMII))) 677 is_c45 = 1; 678 else if (!strcmp(phy_type, phy_modes(PHY_INTERFACE_MODE_SGMII))) 679 is_c45 = 0; 680 else 681 return -ENODATA; 682 683 phy = get_phy_device(mdio, addr, is_c45); 684 if (!phy || IS_ERR(phy)) 685 return -EIO; 686 687 if (mdio->irq) 688 phy->irq = mdio->irq[addr]; 689 690 /* All data is now stored in the phy struct; 691 * register it 692 */ 693 rc = phy_device_register(phy); 694 if (rc) { 695 phy_device_free(phy); 696 return -ENODEV; 697 } 698 699 mac_cb->phy_dev = phy; 700 701 dev_dbg(&mdio->dev, "registered phy at address %i\n", addr); 702 703 return 0; 704 } 705 706 static void hns_mac_register_phy(struct hns_mac_cb *mac_cb) 707 { 708 struct acpi_reference_args args; 709 struct platform_device *pdev; 710 struct mii_bus *mii_bus; 711 int rc; 712 int addr; 713 714 /* Loop over the child nodes and register a phy_device for each one */ 715 if (!to_acpi_device_node(mac_cb->fw_port)) 716 return; 717 718 rc = acpi_node_get_property_reference( 719 mac_cb->fw_port, "mdio-node", 0, &args); 720 if (rc) 721 return; 722 723 addr = hns_mac_phy_parse_addr(mac_cb->dev, mac_cb->fw_port); 724 if (addr < 0) 725 return; 726 727 /* dev address in adev */ 728 pdev = hns_mac_find_platform_device(acpi_fwnode_handle(args.adev)); 729 mii_bus = platform_get_drvdata(pdev); 730 rc = hns_mac_register_phydev(mii_bus, mac_cb, addr); 731 if (!rc) 732 dev_dbg(mac_cb->dev, "mac%d register phy addr:%d\n", 733 mac_cb->mac_id, addr); 734 } 735 736 #define MAC_MEDIA_TYPE_MAX_LEN 16 737 738 static const struct { 739 enum hnae_media_type value; 740 const char *name; 741 } media_type_defs[] = { 742 {HNAE_MEDIA_TYPE_UNKNOWN, "unknown" }, 743 {HNAE_MEDIA_TYPE_FIBER, "fiber" }, 744 {HNAE_MEDIA_TYPE_COPPER, "copper" }, 745 {HNAE_MEDIA_TYPE_BACKPLANE, "backplane" }, 746 }; 747 748 /** 749 *hns_mac_get_info - get mac information from device node 750 *@mac_cb: mac device 751 *@np:device node 752 * return: 0 --success, negative --fail 753 */ 754 static int hns_mac_get_info(struct hns_mac_cb *mac_cb) 755 { 756 struct device_node *np; 757 struct regmap *syscon; 758 struct of_phandle_args cpld_args; 759 const char *media_type; 760 u32 i; 761 u32 ret; 762 763 mac_cb->link = false; 764 mac_cb->half_duplex = false; 765 mac_cb->media_type = HNAE_MEDIA_TYPE_UNKNOWN; 766 mac_cb->speed = mac_phy_to_speed[mac_cb->phy_if]; 767 mac_cb->max_speed = mac_cb->speed; 768 769 if (mac_cb->phy_if == PHY_INTERFACE_MODE_SGMII) { 770 mac_cb->if_support = MAC_GMAC_SUPPORTED; 771 mac_cb->if_support |= SUPPORTED_1000baseT_Full; 772 } else if (mac_cb->phy_if == PHY_INTERFACE_MODE_XGMII) { 773 mac_cb->if_support = SUPPORTED_10000baseR_FEC; 774 mac_cb->if_support |= SUPPORTED_10000baseKR_Full; 775 } 776 777 mac_cb->max_frm = MAC_DEFAULT_MTU; 778 mac_cb->tx_pause_frm_time = MAC_DEFAULT_PAUSE_TIME; 779 mac_cb->port_rst_off = mac_cb->mac_id; 780 mac_cb->port_mode_off = 0; 781 782 /* if the dsaf node doesn't contain a port subnode, get phy-handle 783 * from dsaf node 784 */ 785 if (!mac_cb->fw_port) { 786 np = of_parse_phandle(mac_cb->dev->of_node, "phy-handle", 787 mac_cb->mac_id); 788 mac_cb->phy_dev = of_phy_find_device(np); 789 if (mac_cb->phy_dev) { 790 /* refcount is held by of_phy_find_device() 791 * if the phy_dev is found 792 */ 793 put_device(&mac_cb->phy_dev->mdio.dev); 794 795 dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n", 796 mac_cb->mac_id, np->name); 797 } 798 799 return 0; 800 } 801 802 if (is_of_node(mac_cb->fw_port)) { 803 /* parse property from port subnode in dsaf */ 804 np = of_parse_phandle(to_of_node(mac_cb->fw_port), 805 "phy-handle", 0); 806 mac_cb->phy_dev = of_phy_find_device(np); 807 if (mac_cb->phy_dev) { 808 /* refcount is held by of_phy_find_device() 809 * if the phy_dev is found 810 */ 811 put_device(&mac_cb->phy_dev->mdio.dev); 812 dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n", 813 mac_cb->mac_id, np->name); 814 } 815 816 syscon = syscon_node_to_regmap( 817 of_parse_phandle(to_of_node(mac_cb->fw_port), 818 "serdes-syscon", 0)); 819 if (IS_ERR_OR_NULL(syscon)) { 820 dev_err(mac_cb->dev, "serdes-syscon is needed!\n"); 821 return -EINVAL; 822 } 823 mac_cb->serdes_ctrl = syscon; 824 825 ret = fwnode_property_read_u32(mac_cb->fw_port, 826 "port-rst-offset", 827 &mac_cb->port_rst_off); 828 if (ret) { 829 dev_dbg(mac_cb->dev, 830 "mac%d port-rst-offset not found, use default value.\n", 831 mac_cb->mac_id); 832 } 833 834 ret = fwnode_property_read_u32(mac_cb->fw_port, 835 "port-mode-offset", 836 &mac_cb->port_mode_off); 837 if (ret) { 838 dev_dbg(mac_cb->dev, 839 "mac%d port-mode-offset not found, use default value.\n", 840 mac_cb->mac_id); 841 } 842 843 ret = of_parse_phandle_with_fixed_args( 844 to_of_node(mac_cb->fw_port), "cpld-syscon", 1, 0, 845 &cpld_args); 846 if (ret) { 847 dev_dbg(mac_cb->dev, "mac%d no cpld-syscon found.\n", 848 mac_cb->mac_id); 849 mac_cb->cpld_ctrl = NULL; 850 } else { 851 syscon = syscon_node_to_regmap(cpld_args.np); 852 if (IS_ERR_OR_NULL(syscon)) { 853 dev_dbg(mac_cb->dev, "no cpld-syscon found!\n"); 854 mac_cb->cpld_ctrl = NULL; 855 } else { 856 mac_cb->cpld_ctrl = syscon; 857 mac_cb->cpld_ctrl_reg = cpld_args.args[0]; 858 } 859 } 860 } else if (is_acpi_node(mac_cb->fw_port)) { 861 hns_mac_register_phy(mac_cb); 862 } else { 863 dev_err(mac_cb->dev, "mac%d cannot find phy node\n", 864 mac_cb->mac_id); 865 } 866 867 if (!fwnode_property_read_string(mac_cb->fw_port, "media-type", 868 &media_type)) { 869 for (i = 0; i < ARRAY_SIZE(media_type_defs); i++) { 870 if (!strncmp(media_type_defs[i].name, media_type, 871 MAC_MEDIA_TYPE_MAX_LEN)) { 872 mac_cb->media_type = media_type_defs[i].value; 873 break; 874 } 875 } 876 } 877 878 return 0; 879 } 880 881 /** 882 * hns_mac_get_mode - get mac mode 883 * @phy_if: phy interface 884 * retuen 0 - gmac, 1 - xgmac , negative --fail 885 */ 886 static int hns_mac_get_mode(phy_interface_t phy_if) 887 { 888 switch (phy_if) { 889 case PHY_INTERFACE_MODE_SGMII: 890 return MAC_GMAC_IDX; 891 case PHY_INTERFACE_MODE_XGMII: 892 return MAC_XGMAC_IDX; 893 default: 894 return -EINVAL; 895 } 896 } 897 898 u8 __iomem *hns_mac_get_vaddr(struct dsaf_device *dsaf_dev, 899 struct hns_mac_cb *mac_cb, u32 mac_mode_idx) 900 { 901 u8 __iomem *base = dsaf_dev->io_base; 902 int mac_id = mac_cb->mac_id; 903 904 if (mac_cb->mac_type == HNAE_PORT_SERVICE) 905 return base + 0x40000 + mac_id * 0x4000 - 906 mac_mode_idx * 0x20000; 907 else 908 return dsaf_dev->ppe_base + 0x1000; 909 } 910 911 /** 912 * hns_mac_get_cfg - get mac cfg from dtb or acpi table 913 * @dsaf_dev: dsa fabric device struct pointer 914 * @mac_cb: mac control block 915 * return 0 - success , negative --fail 916 */ 917 int hns_mac_get_cfg(struct dsaf_device *dsaf_dev, struct hns_mac_cb *mac_cb) 918 { 919 int ret; 920 u32 mac_mode_idx; 921 922 mac_cb->dsaf_dev = dsaf_dev; 923 mac_cb->dev = dsaf_dev->dev; 924 925 mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base; 926 mac_cb->serdes_vaddr = dsaf_dev->sds_base; 927 928 mac_cb->sfp_prsnt = 0; 929 mac_cb->txpkt_for_led = 0; 930 mac_cb->rxpkt_for_led = 0; 931 932 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) 933 mac_cb->mac_type = HNAE_PORT_SERVICE; 934 else 935 mac_cb->mac_type = HNAE_PORT_DEBUG; 936 937 mac_cb->phy_if = dsaf_dev->misc_op->get_phy_if(mac_cb); 938 939 ret = hns_mac_get_mode(mac_cb->phy_if); 940 if (ret < 0) { 941 dev_err(dsaf_dev->dev, 942 "hns_mac_get_mode failed,mac%d ret = %#x!\n", 943 mac_cb->mac_id, ret); 944 return ret; 945 } 946 mac_mode_idx = (u32)ret; 947 948 ret = hns_mac_get_info(mac_cb); 949 if (ret) 950 return ret; 951 952 mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb); 953 mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx); 954 955 return 0; 956 } 957 958 static int hns_mac_get_max_port_num(struct dsaf_device *dsaf_dev) 959 { 960 if (HNS_DSAF_IS_DEBUG(dsaf_dev)) 961 return 1; 962 else 963 return DSAF_MAX_PORT_NUM; 964 } 965 966 /** 967 * hns_mac_init - init mac 968 * @dsaf_dev: dsa fabric device struct pointer 969 * return 0 - success , negative --fail 970 */ 971 int hns_mac_init(struct dsaf_device *dsaf_dev) 972 { 973 bool found = false; 974 int ret; 975 u32 port_id; 976 int max_port_num = hns_mac_get_max_port_num(dsaf_dev); 977 struct hns_mac_cb *mac_cb; 978 struct fwnode_handle *child; 979 980 device_for_each_child_node(dsaf_dev->dev, child) { 981 ret = fwnode_property_read_u32(child, "reg", &port_id); 982 if (ret) { 983 dev_err(dsaf_dev->dev, 984 "get reg fail, ret=%d!\n", ret); 985 return ret; 986 } 987 if (port_id >= max_port_num) { 988 dev_err(dsaf_dev->dev, 989 "reg(%u) out of range!\n", port_id); 990 return -EINVAL; 991 } 992 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb), 993 GFP_KERNEL); 994 if (!mac_cb) 995 return -ENOMEM; 996 mac_cb->fw_port = child; 997 mac_cb->mac_id = (u8)port_id; 998 dsaf_dev->mac_cb[port_id] = mac_cb; 999 found = true; 1000 } 1001 1002 /* if don't get any port subnode from dsaf node 1003 * will init all port then, this is compatible with the old dts 1004 */ 1005 if (!found) { 1006 for (port_id = 0; port_id < max_port_num; port_id++) { 1007 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb), 1008 GFP_KERNEL); 1009 if (!mac_cb) 1010 return -ENOMEM; 1011 1012 mac_cb->mac_id = port_id; 1013 dsaf_dev->mac_cb[port_id] = mac_cb; 1014 } 1015 } 1016 /* init mac_cb for all port */ 1017 for (port_id = 0; port_id < max_port_num; port_id++) { 1018 mac_cb = dsaf_dev->mac_cb[port_id]; 1019 if (!mac_cb) 1020 continue; 1021 1022 ret = hns_mac_get_cfg(dsaf_dev, mac_cb); 1023 if (ret) 1024 return ret; 1025 ret = hns_mac_init_ex(mac_cb); 1026 if (ret) 1027 return ret; 1028 } 1029 1030 return 0; 1031 } 1032 1033 void hns_mac_uninit(struct dsaf_device *dsaf_dev) 1034 { 1035 int i; 1036 int max_port_num = hns_mac_get_max_port_num(dsaf_dev); 1037 1038 for (i = 0; i < max_port_num; i++) { 1039 dsaf_dev->misc_op->cpld_reset_led(dsaf_dev->mac_cb[i]); 1040 dsaf_dev->mac_cb[i] = NULL; 1041 } 1042 } 1043 1044 int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb, 1045 enum hnae_loop loop, int en) 1046 { 1047 int ret; 1048 struct mac_driver *drv = hns_mac_get_drv(mac_cb); 1049 1050 if (drv->config_loopback) 1051 ret = drv->config_loopback(drv, loop, en); 1052 else 1053 ret = -ENOTSUPP; 1054 1055 return ret; 1056 } 1057 1058 void hns_mac_update_stats(struct hns_mac_cb *mac_cb) 1059 { 1060 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1061 1062 mac_ctrl_drv->update_stats(mac_ctrl_drv); 1063 } 1064 1065 void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data) 1066 { 1067 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1068 1069 mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data); 1070 } 1071 1072 void hns_mac_get_strings(struct hns_mac_cb *mac_cb, 1073 int stringset, u8 *data) 1074 { 1075 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1076 1077 mac_ctrl_drv->get_strings(stringset, data); 1078 } 1079 1080 int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset) 1081 { 1082 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1083 1084 return mac_ctrl_drv->get_sset_count(stringset); 1085 } 1086 1087 void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en) 1088 { 1089 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1090 1091 if (mac_ctrl_drv->set_promiscuous) 1092 mac_ctrl_drv->set_promiscuous(mac_ctrl_drv, en); 1093 } 1094 1095 int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb) 1096 { 1097 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1098 1099 return mac_ctrl_drv->get_regs_count(); 1100 } 1101 1102 void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data) 1103 { 1104 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1105 1106 mac_ctrl_drv->get_regs(mac_ctrl_drv, data); 1107 } 1108 1109 void hns_set_led_opt(struct hns_mac_cb *mac_cb) 1110 { 1111 int nic_data = 0; 1112 int txpkts, rxpkts; 1113 1114 txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts; 1115 rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts; 1116 if (txpkts || rxpkts) 1117 nic_data = 1; 1118 else 1119 nic_data = 0; 1120 mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts; 1121 mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts; 1122 mac_cb->dsaf_dev->misc_op->cpld_set_led(mac_cb, (int)mac_cb->link, 1123 mac_cb->speed, nic_data); 1124 } 1125 1126 int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb, 1127 enum hnae_led_state status) 1128 { 1129 if (!mac_cb || !mac_cb->cpld_ctrl) 1130 return 0; 1131 1132 return mac_cb->dsaf_dev->misc_op->cpld_set_led_id(mac_cb, status); 1133 } 1134