Blame view
net/dsa-uclass.c
10.6 KB
dd38c3336
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
// SPDX-License-Identifier: GPL-2.0+ /* * Copyright 2019-2020 NXP */ #include <net/dsa.h> #include <dm/lists.h> #include <dm/device_compat.h> #include <dm/device-internal.h> #include <dm/uclass-internal.h> #include <miiphy.h> #define DSA_PORT_CHILD_DRV_NAME "dsa-port" /* helper that returns the DSA master Ethernet device */ static struct udevice *dsa_port_get_master(struct udevice *pdev) { struct udevice *dev = dev_get_parent(pdev); struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); return platdata->master_dev; } /* * Start the desired port, the CPU port and the master Eth interface. * TODO: if cascaded we may need to _start ports in other switches too */ static int dsa_port_start(struct udevice *pdev) { struct udevice *dev = dev_get_parent(pdev); struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); struct udevice *master = dsa_port_get_master(pdev); struct dsa_port_platdata *ppriv = dev_get_priv(pdev); struct dsa_ops *ops = dsa_get_ops(dev); int err; if (!ppriv || !platdata) return -EINVAL; if (!master) { dev_err(pdev, "DSA master Ethernet device not found! "); return -EINVAL; } if (ops->port_enable) { err = ops->port_enable(dev, ppriv->index, ppriv->phy); if (err) return err; err = ops->port_enable(dev, platdata->cpu_port, platdata->port[platdata->cpu_port].phy); if (err) return err; } return eth_get_ops(master)->start(master); } /* Stop the desired port, the CPU port and the master Eth interface */ static void dsa_port_stop(struct udevice *pdev) { struct udevice *dev = dev_get_parent(pdev); struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); struct udevice *master = dsa_port_get_master(pdev); struct dsa_port_platdata *ppriv = dev_get_priv(pdev); struct dsa_ops *ops = dsa_get_ops(dev); if (!ppriv || !platdata) return; if (ops->port_disable) { ops->port_disable(dev, ppriv->index, ppriv->phy); ops->port_disable(dev, platdata->cpu_port, platdata->port[platdata->cpu_port].phy); } /* * stop master only if it's active, don't probe it otherwise. * Under normal usage it would be active because we're using it, but * during tear-down it may have been removed ahead of us. */ if (master && device_active(master)) eth_get_ops(master)->stop(master); } /* * Insert a DSA tag and call master Ethernet send on the resulting packet * We copy the frame to a stack buffer where we have reserved headroom and * tailroom space. Headroom and tailroom are set to 0. */ static int dsa_port_send(struct udevice *pdev, void *packet, int length) { struct udevice *dev = dev_get_parent(pdev); struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); int head = platdata->headroom, tail = platdata->tailroom; struct udevice *master = dsa_port_get_master(pdev); struct dsa_port_platdata *ppriv = dev_get_priv(pdev); struct dsa_ops *ops = dsa_get_ops(dev); uchar dsa_packet_tmp[PKTSIZE_ALIGN]; int err; if (!master) return -EINVAL; if (length + head + tail > PKTSIZE_ALIGN) return -EINVAL; memset(dsa_packet_tmp, 0, head); memset(dsa_packet_tmp + head + length, 0, tail); memcpy(dsa_packet_tmp + head, packet, length); length += head + tail; /* copy back to preserve original buffer alignment */ memcpy(packet, dsa_packet_tmp, length); err = ops->xmit(dev, ppriv->index, packet, length); if (err) return err; return eth_get_ops(master)->send(master, packet, length); } /* Receive a frame from master Ethernet, process it and pass it on */ static int dsa_port_recv(struct udevice *pdev, int flags, uchar **packetp) { struct udevice *dev = dev_get_parent(pdev); struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); struct udevice *master = dsa_port_get_master(pdev); struct dsa_port_platdata *ppriv = dev_get_priv(pdev); struct dsa_ops *ops = dsa_get_ops(dev); int head = platdata->headroom, tail = platdata->tailroom; int length, port_index, err; if (!master) return -EINVAL; length = eth_get_ops(master)->recv(master, flags, packetp); if (length <= 0) return length; /* * if we receive frames from a different port or frames that DSA driver * doesn't like we discard them here. * In case of discard we return with no frame and expect to be called * again instead of looping here, so upper layer can deal with timeouts * and ctrl-c */ err = ops->rcv(dev, &port_index, *packetp, length); if (err || port_index != ppriv->index || (length <= head + tail)) { if (eth_get_ops(master)->free_pkt) eth_get_ops(master)->free_pkt(master, *packetp, length); return -EAGAIN; } /* * We move the pointer over headroom here to avoid a copy. If free_pkt * gets called we move the pointer back before calling master free_pkt. */ *packetp += head; return length - head - tail; } static int dsa_port_free_pkt(struct udevice *pdev, uchar *packet, int length) { struct udevice *dev = dev_get_parent(pdev); struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); struct udevice *master = dsa_port_get_master(pdev); if (!master) return -EINVAL; if (eth_get_ops(master)->free_pkt) { /* return the original pointer and length to master Eth */ packet -= platdata->headroom; length += platdata->headroom - platdata->tailroom; return eth_get_ops(master)->free_pkt(master, packet, length); } return 0; } static int dsa_port_probe(struct udevice *pdev) { struct udevice *master = dsa_port_get_master(pdev); unsigned char env_enetaddr[ARP_HLEN]; /* If there is no MAC address in the environment, inherit it * from the DSA master. */ eth_env_get_enetaddr_by_index("eth", pdev->seq, env_enetaddr); if (!is_zero_ethaddr(env_enetaddr)) return 0; if (master) { struct eth_pdata *meth = dev_get_platdata(master); struct eth_pdata *peth = dev_get_platdata(pdev); memcpy(peth->enetaddr, meth->enetaddr, ARP_HLEN); eth_env_set_enetaddr_by_index("eth", pdev->seq, meth->enetaddr); } return 0; } static const struct eth_ops dsa_port_ops = { .start = dsa_port_start, .send = dsa_port_send, .recv = dsa_port_recv, .stop = dsa_port_stop, .free_pkt = dsa_port_free_pkt, }; U_BOOT_DRIVER(dsa_port) = { .name = DSA_PORT_CHILD_DRV_NAME, .id = UCLASS_ETH, .ops = &dsa_port_ops, .probe = dsa_port_probe, .platdata_auto_alloc_size = sizeof(struct eth_pdata), }; /* * reads the DT properties of the given DSA port. * If the return value is != 0 then the port is skipped */ static int dsa_port_parse_dt(struct udevice *dev, int port_index, ofnode ports_node, bool *is_cpu) { struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); struct dsa_port_platdata *port = &platdata->port[port_index]; ofnode temp_node; u32 ethernet; /* * if we don't have a DT we don't do anything here but the port is * registered normally */ if (!ofnode_valid(ports_node)) return 0; ofnode_for_each_subnode(temp_node, ports_node) { const char *port_label; u32 reg; if (ofnode_read_u32(temp_node, "reg", ®) || reg != port_index) continue; port->node = temp_node; /* if the port is explicitly disabled in DT skip it */ if (!ofnode_is_available(temp_node)) return -ENODEV; dev_dbg(dev, "port %d node %s ", port->index, ofnode_get_name(port->node)); /* Use 'label' if present in DT */ port_label = ofnode_read_string(port->node, "label"); if (port_label) strncpy(port->name, port_label, DSA_PORT_NAME_LENGTH); *is_cpu = !ofnode_read_u32(port->node, "ethernet", ðernet); if (*is_cpu) { platdata->master_node = ofnode_get_by_phandle(ethernet); platdata->cpu_port = port_index; dev_dbg(dev, "master node %s on port %d ", ofnode_get_name(platdata->master_node), port_index); } break; } return 0; } /** * This function mostly deals with pulling information out of the device tree * into the platdata structure. * It goes through the list of switch ports, registers an Eth device for each * front panel port and identifies the cpu port connected to master Eth device. * TODO: support cascaded switches */ static int dm_dsa_post_bind(struct udevice *dev) { struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); ofnode ports_node = ofnode_null(); int first_err = 0, err = 0, i; if (!platdata) { dev_err(dev, "missing plaform data "); return -EINVAL; } if (platdata->num_ports <= 0 || platdata->num_ports > DSA_MAX_PORTS) { dev_err(dev, "unexpected num_ports value (%d) ", platdata->num_ports); return -EINVAL; } platdata->master_node = ofnode_null(); if (!ofnode_valid(dev->node)) { dev_dbg(dev, "Device doesn't have a valid DT node! "); } else { ports_node = ofnode_find_subnode(dev->node, "ports"); if (!ofnode_valid(ports_node)) dev_dbg(dev, "ports node is missing under DSA device! "); } for (i = 0; i < platdata->num_ports; i++) { struct dsa_port_platdata *port = &platdata->port[i]; bool skip_port, is_cpu = false; port->index = i; /* * If the driver set up port names in _bind use those, otherwise * use default ones. * If present, DT label is used as name and overrides anything * we may have here. */ if (!strlen(port->name)) snprintf(port->name, DSA_PORT_NAME_LENGTH, "%s@%d", dev->name, i); skip_port = !!dsa_port_parse_dt(dev, i, ports_node, &is_cpu); /* * if this is the CPU port don't register it as an ETH device, * we skip it on purpose since I/O to/from it from the CPU * isn't useful * TODO: cpu port may have a PHY and we don't handle that yet. */ if (is_cpu || skip_port) continue; err = device_bind_driver_to_node(dev, DSA_PORT_CHILD_DRV_NAME, port->name, port->node, &port->dev); /* try to bind all ports but keep 1st error */ if (err && !first_err) first_err = err; } if (!ofnode_valid(platdata->master_node)) dev_dbg(dev, "DSA master Eth device is missing! "); return first_err; } /** * This function deals with additional devices around the switch as these should * have been bound to drivers by now. * TODO: pick up references to other switch devices here, if we're cascaded. */ static int dm_dsa_pre_probe(struct udevice *dev) { struct dsa_perdev_platdata *platdata = dev_get_platdata(dev); int i; if (!platdata) return -EINVAL; if (ofnode_valid(platdata->master_node)) uclass_find_device_by_ofnode(UCLASS_ETH, platdata->master_node, &platdata->master_dev); for (i = 0; i < platdata->num_ports; i++) { struct dsa_port_platdata *port = &platdata->port[i]; /* non-cpu ports only */ if (!port->dev) continue; port->dev->priv = port; port->phy = dm_eth_phy_connect(port->dev); } return 0; } UCLASS_DRIVER(dsa) = { .id = UCLASS_DSA, .name = "dsa", .post_bind = dm_dsa_post_bind, .pre_probe = dm_dsa_pre_probe, .per_device_platdata_auto_alloc_size = sizeof(struct dsa_perdev_platdata), }; |