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