The 11/24/2021 18:59, Andrew Lunn wrote: Hi Andrew, > > > +static void lan966x_ifh_inject(u32 *ifh, size_t val, size_t pos, size_t length) > > +{ > > + int i; > > + > > + for (i = pos; i < pos + length; ++i) { > > + if (val & BIT(i - pos)) > > + ifh[IFH_LEN - i / 32 - 1] |= BIT(i % 32); > > + else > > + ifh[IFH_LEN - i / 32 - 1] &= ~(BIT(i % 32)); > > + } > > +} > > + > > +static void lan966x_gen_ifh(u32 *ifh, struct lan966x_frame_info *info, > > + struct lan966x *lan966x) > > +{ > > + lan966x_ifh_inject(ifh, 1, IFH_POS_BYPASS, 1); > > + lan966x_ifh_inject(ifh, info->port, IFH_POS_DSTS, IFH_WID_DSTS); > > + lan966x_ifh_inject(ifh, info->qos_class, IFH_POS_QOS_CLASS, > > + IFH_WID_QOS_CLASS); > > + lan966x_ifh_inject(ifh, info->ipv, IFH_POS_IPV, IFH_WID_IPV); > > +} > > + > > > + /* Write IFH header */ > > + for (i = 0; i < IFH_LEN; ++i) { > > + /* Wait until the fifo is ready */ > > + while (!(QS_INJ_STATUS_FIFO_RDY_GET(lan_rd(lan966x, QS_INJ_STATUS)) & > > + BIT(grp))) > > + ; > > + > > + lan_wr((__force u32)cpu_to_be32(ifh[i]), lan966x, > > + QS_INJ_WR(grp)); > > There is a lot of magic going on here constructing the IFH. Is it > possible to define the structure using bit fields and __be32. You > should then be able to skip this cpu_to_be32 and the ugly cast. And > the actual structure should be a lot clearer. If I undestood you correctly I have tried to do the following: struct lan966x_ifh { __be32 timestamp; __be32 bypass : 1; __be32 port : 3; ... }; But then I start to get errors from sparse: error: invalid bitfield specifier for type restricted __be32. On thing that I can do is to use packing() instead of lan966x_ifh_inject() and declare ifh as __be32. I think this will also work and simplify a little bit the code. > > > +static int lan966x_rx_frame_word(struct lan966x *lan966x, u8 grp, bool ifh, > > + u32 *rval) > > +{ > > + u32 bytes_valid; > > + u32 val; > > + > > + val = lan_rd(lan966x, QS_XTR_RD(grp)); > > + if (val == XTR_NOT_READY) { > > + if (ifh) > > + return -EIO; > > + > > + do { > > + val = lan_rd(lan966x, QS_XTR_RD(grp)); > > + } while (val == XTR_NOT_READY); > > I would add some sort of timeout here, just in case the hardware > breaks. You have quite a few such loops, it would be better to make > use of the helpers in linux/iopoll.h. Yes, I will do that, I will also add sime timeout also for injection. > -- /Horatiu