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