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 of_node_put(np); 799 800 return 0; 801 } 802 803 if (is_of_node(mac_cb->fw_port)) { 804 /* parse property from port subnode in dsaf */ 805 np = of_parse_phandle(to_of_node(mac_cb->fw_port), 806 "phy-handle", 0); 807 mac_cb->phy_dev = of_phy_find_device(np); 808 if (mac_cb->phy_dev) { 809 /* refcount is held by of_phy_find_device() 810 * if the phy_dev is found 811 */ 812 put_device(&mac_cb->phy_dev->mdio.dev); 813 dev_dbg(mac_cb->dev, "mac%d phy_node: %s\n", 814 mac_cb->mac_id, np->name); 815 } 816 of_node_put(np); 817 818 np = of_parse_phandle(to_of_node(mac_cb->fw_port), 819 "serdes-syscon", 0); 820 syscon = syscon_node_to_regmap(np); 821 of_node_put(np); 822 if (IS_ERR_OR_NULL(syscon)) { 823 dev_err(mac_cb->dev, "serdes-syscon is needed!\n"); 824 return -EINVAL; 825 } 826 mac_cb->serdes_ctrl = syscon; 827 828 ret = fwnode_property_read_u32(mac_cb->fw_port, 829 "port-rst-offset", 830 &mac_cb->port_rst_off); 831 if (ret) { 832 dev_dbg(mac_cb->dev, 833 "mac%d port-rst-offset not found, use default value.\n", 834 mac_cb->mac_id); 835 } 836 837 ret = fwnode_property_read_u32(mac_cb->fw_port, 838 "port-mode-offset", 839 &mac_cb->port_mode_off); 840 if (ret) { 841 dev_dbg(mac_cb->dev, 842 "mac%d port-mode-offset not found, use default value.\n", 843 mac_cb->mac_id); 844 } 845 846 ret = of_parse_phandle_with_fixed_args( 847 to_of_node(mac_cb->fw_port), "cpld-syscon", 1, 0, 848 &cpld_args); 849 if (ret) { 850 dev_dbg(mac_cb->dev, "mac%d no cpld-syscon found.\n", 851 mac_cb->mac_id); 852 mac_cb->cpld_ctrl = NULL; 853 } else { 854 syscon = syscon_node_to_regmap(cpld_args.np); 855 if (IS_ERR_OR_NULL(syscon)) { 856 dev_dbg(mac_cb->dev, "no cpld-syscon found!\n"); 857 mac_cb->cpld_ctrl = NULL; 858 } else { 859 mac_cb->cpld_ctrl = syscon; 860 mac_cb->cpld_ctrl_reg = cpld_args.args[0]; 861 } 862 } 863 } else if (is_acpi_node(mac_cb->fw_port)) { 864 hns_mac_register_phy(mac_cb); 865 } else { 866 dev_err(mac_cb->dev, "mac%d cannot find phy node\n", 867 mac_cb->mac_id); 868 } 869 870 if (!fwnode_property_read_string(mac_cb->fw_port, "media-type", 871 &media_type)) { 872 for (i = 0; i < ARRAY_SIZE(media_type_defs); i++) { 873 if (!strncmp(media_type_defs[i].name, media_type, 874 MAC_MEDIA_TYPE_MAX_LEN)) { 875 mac_cb->media_type = media_type_defs[i].value; 876 break; 877 } 878 } 879 } 880 881 return 0; 882 } 883 884 /** 885 * hns_mac_get_mode - get mac mode 886 * @phy_if: phy interface 887 * retuen 0 - gmac, 1 - xgmac , negative --fail 888 */ 889 static int hns_mac_get_mode(phy_interface_t phy_if) 890 { 891 switch (phy_if) { 892 case PHY_INTERFACE_MODE_SGMII: 893 return MAC_GMAC_IDX; 894 case PHY_INTERFACE_MODE_XGMII: 895 return MAC_XGMAC_IDX; 896 default: 897 return -EINVAL; 898 } 899 } 900 901 u8 __iomem *hns_mac_get_vaddr(struct dsaf_device *dsaf_dev, 902 struct hns_mac_cb *mac_cb, u32 mac_mode_idx) 903 { 904 u8 __iomem *base = dsaf_dev->io_base; 905 int mac_id = mac_cb->mac_id; 906 907 if (mac_cb->mac_type == HNAE_PORT_SERVICE) 908 return base + 0x40000 + mac_id * 0x4000 - 909 mac_mode_idx * 0x20000; 910 else 911 return dsaf_dev->ppe_base + 0x1000; 912 } 913 914 /** 915 * hns_mac_get_cfg - get mac cfg from dtb or acpi table 916 * @dsaf_dev: dsa fabric device struct pointer 917 * @mac_cb: mac control block 918 * return 0 - success , negative --fail 919 */ 920 int hns_mac_get_cfg(struct dsaf_device *dsaf_dev, struct hns_mac_cb *mac_cb) 921 { 922 int ret; 923 u32 mac_mode_idx; 924 925 mac_cb->dsaf_dev = dsaf_dev; 926 mac_cb->dev = dsaf_dev->dev; 927 928 mac_cb->sys_ctl_vaddr = dsaf_dev->sc_base; 929 mac_cb->serdes_vaddr = dsaf_dev->sds_base; 930 931 mac_cb->sfp_prsnt = 0; 932 mac_cb->txpkt_for_led = 0; 933 mac_cb->rxpkt_for_led = 0; 934 935 if (!HNS_DSAF_IS_DEBUG(dsaf_dev)) 936 mac_cb->mac_type = HNAE_PORT_SERVICE; 937 else 938 mac_cb->mac_type = HNAE_PORT_DEBUG; 939 940 mac_cb->phy_if = dsaf_dev->misc_op->get_phy_if(mac_cb); 941 942 ret = hns_mac_get_mode(mac_cb->phy_if); 943 if (ret < 0) { 944 dev_err(dsaf_dev->dev, 945 "hns_mac_get_mode failed,mac%d ret = %#x!\n", 946 mac_cb->mac_id, ret); 947 return ret; 948 } 949 mac_mode_idx = (u32)ret; 950 951 ret = hns_mac_get_info(mac_cb); 952 if (ret) 953 return ret; 954 955 mac_cb->dsaf_dev->misc_op->cpld_reset_led(mac_cb); 956 mac_cb->vaddr = hns_mac_get_vaddr(dsaf_dev, mac_cb, mac_mode_idx); 957 958 return 0; 959 } 960 961 static int hns_mac_get_max_port_num(struct dsaf_device *dsaf_dev) 962 { 963 if (HNS_DSAF_IS_DEBUG(dsaf_dev)) 964 return 1; 965 else 966 return DSAF_MAX_PORT_NUM; 967 } 968 969 /** 970 * hns_mac_init - init mac 971 * @dsaf_dev: dsa fabric device struct pointer 972 * return 0 - success , negative --fail 973 */ 974 int hns_mac_init(struct dsaf_device *dsaf_dev) 975 { 976 bool found = false; 977 int ret; 978 u32 port_id; 979 int max_port_num = hns_mac_get_max_port_num(dsaf_dev); 980 struct hns_mac_cb *mac_cb; 981 struct fwnode_handle *child; 982 983 device_for_each_child_node(dsaf_dev->dev, child) { 984 ret = fwnode_property_read_u32(child, "reg", &port_id); 985 if (ret) { 986 dev_err(dsaf_dev->dev, 987 "get reg fail, ret=%d!\n", ret); 988 return ret; 989 } 990 if (port_id >= max_port_num) { 991 dev_err(dsaf_dev->dev, 992 "reg(%u) out of range!\n", port_id); 993 return -EINVAL; 994 } 995 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb), 996 GFP_KERNEL); 997 if (!mac_cb) 998 return -ENOMEM; 999 mac_cb->fw_port = child; 1000 mac_cb->mac_id = (u8)port_id; 1001 dsaf_dev->mac_cb[port_id] = mac_cb; 1002 found = true; 1003 } 1004 1005 /* if don't get any port subnode from dsaf node 1006 * will init all port then, this is compatible with the old dts 1007 */ 1008 if (!found) { 1009 for (port_id = 0; port_id < max_port_num; port_id++) { 1010 mac_cb = devm_kzalloc(dsaf_dev->dev, sizeof(*mac_cb), 1011 GFP_KERNEL); 1012 if (!mac_cb) 1013 return -ENOMEM; 1014 1015 mac_cb->mac_id = port_id; 1016 dsaf_dev->mac_cb[port_id] = mac_cb; 1017 } 1018 } 1019 /* init mac_cb for all port */ 1020 for (port_id = 0; port_id < max_port_num; port_id++) { 1021 mac_cb = dsaf_dev->mac_cb[port_id]; 1022 if (!mac_cb) 1023 continue; 1024 1025 ret = hns_mac_get_cfg(dsaf_dev, mac_cb); 1026 if (ret) 1027 return ret; 1028 ret = hns_mac_init_ex(mac_cb); 1029 if (ret) 1030 return ret; 1031 } 1032 1033 return 0; 1034 } 1035 1036 void hns_mac_uninit(struct dsaf_device *dsaf_dev) 1037 { 1038 int i; 1039 int max_port_num = hns_mac_get_max_port_num(dsaf_dev); 1040 1041 for (i = 0; i < max_port_num; i++) { 1042 dsaf_dev->misc_op->cpld_reset_led(dsaf_dev->mac_cb[i]); 1043 dsaf_dev->mac_cb[i] = NULL; 1044 } 1045 } 1046 1047 int hns_mac_config_mac_loopback(struct hns_mac_cb *mac_cb, 1048 enum hnae_loop loop, int en) 1049 { 1050 int ret; 1051 struct mac_driver *drv = hns_mac_get_drv(mac_cb); 1052 1053 if (drv->config_loopback) 1054 ret = drv->config_loopback(drv, loop, en); 1055 else 1056 ret = -ENOTSUPP; 1057 1058 return ret; 1059 } 1060 1061 void hns_mac_update_stats(struct hns_mac_cb *mac_cb) 1062 { 1063 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1064 1065 mac_ctrl_drv->update_stats(mac_ctrl_drv); 1066 } 1067 1068 void hns_mac_get_stats(struct hns_mac_cb *mac_cb, u64 *data) 1069 { 1070 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1071 1072 mac_ctrl_drv->get_ethtool_stats(mac_ctrl_drv, data); 1073 } 1074 1075 void hns_mac_get_strings(struct hns_mac_cb *mac_cb, 1076 int stringset, u8 *data) 1077 { 1078 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1079 1080 mac_ctrl_drv->get_strings(stringset, data); 1081 } 1082 1083 int hns_mac_get_sset_count(struct hns_mac_cb *mac_cb, int stringset) 1084 { 1085 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1086 1087 return mac_ctrl_drv->get_sset_count(stringset); 1088 } 1089 1090 void hns_mac_set_promisc(struct hns_mac_cb *mac_cb, u8 en) 1091 { 1092 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1093 1094 if (mac_ctrl_drv->set_promiscuous) 1095 mac_ctrl_drv->set_promiscuous(mac_ctrl_drv, en); 1096 } 1097 1098 int hns_mac_get_regs_count(struct hns_mac_cb *mac_cb) 1099 { 1100 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1101 1102 return mac_ctrl_drv->get_regs_count(); 1103 } 1104 1105 void hns_mac_get_regs(struct hns_mac_cb *mac_cb, void *data) 1106 { 1107 struct mac_driver *mac_ctrl_drv = hns_mac_get_drv(mac_cb); 1108 1109 mac_ctrl_drv->get_regs(mac_ctrl_drv, data); 1110 } 1111 1112 void hns_set_led_opt(struct hns_mac_cb *mac_cb) 1113 { 1114 int nic_data = 0; 1115 int txpkts, rxpkts; 1116 1117 txpkts = mac_cb->txpkt_for_led - mac_cb->hw_stats.tx_good_pkts; 1118 rxpkts = mac_cb->rxpkt_for_led - mac_cb->hw_stats.rx_good_pkts; 1119 if (txpkts || rxpkts) 1120 nic_data = 1; 1121 else 1122 nic_data = 0; 1123 mac_cb->txpkt_for_led = mac_cb->hw_stats.tx_good_pkts; 1124 mac_cb->rxpkt_for_led = mac_cb->hw_stats.rx_good_pkts; 1125 mac_cb->dsaf_dev->misc_op->cpld_set_led(mac_cb, (int)mac_cb->link, 1126 mac_cb->speed, nic_data); 1127 } 1128 1129 int hns_cpld_led_set_id(struct hns_mac_cb *mac_cb, 1130 enum hnae_led_state status) 1131 { 1132 if (!mac_cb || !mac_cb->cpld_ctrl) 1133 return 0; 1134 1135 return mac_cb->dsaf_dev->misc_op->cpld_set_led_id(mac_cb, status); 1136 } 1137