2 * Copyright(c) 2017 - 2018 Intel Corporation.
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
24 * - Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * - Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in
28 * the documentation and/or other materials provided with the
30 * - Neither the name of Intel Corporation nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 * This file contains HFI1 support for VNIC SDMA functionality
55 #define HFI1_VNIC_SDMA_Q_ACTIVE BIT(0)
56 #define HFI1_VNIC_SDMA_Q_DEFERRED BIT(1)
58 #define HFI1_VNIC_TXREQ_NAME_LEN 32
59 #define HFI1_VNIC_SDMA_DESC_WTRMRK 64
62 * struct vnic_txreq - VNIC transmit descriptor
63 * @txreq: sdma transmit request
64 * @sdma: vnic sdma pointer
71 struct sdma_txreq txreq
;
72 struct hfi1_vnic_sdma
*sdma
;
75 unsigned char pad
[HFI1_VNIC_MAX_PAD
];
80 static void vnic_sdma_complete(struct sdma_txreq
*txreq
,
83 struct vnic_txreq
*tx
= container_of(txreq
, struct vnic_txreq
, txreq
);
84 struct hfi1_vnic_sdma
*vnic_sdma
= tx
->sdma
;
86 sdma_txclean(vnic_sdma
->dd
, txreq
);
87 dev_kfree_skb_any(tx
->skb
);
88 kmem_cache_free(vnic_sdma
->dd
->vnic
.txreq_cache
, tx
);
91 static noinline
int build_vnic_ulp_payload(struct sdma_engine
*sde
,
92 struct vnic_txreq
*tx
)
96 ret
= sdma_txadd_kvaddr(
100 skb_headlen(tx
->skb
));
104 for (i
= 0; i
< skb_shinfo(tx
->skb
)->nr_frags
; i
++) {
105 skb_frag_t
*frag
= &skb_shinfo(tx
->skb
)->frags
[i
];
107 /* combine physically continuous fragments later? */
108 ret
= sdma_txadd_page(sde
->dd
,
112 skb_frag_size(frag
));
118 ret
= sdma_txadd_kvaddr(sde
->dd
, &tx
->txreq
,
119 tx
->pad
+ HFI1_VNIC_MAX_PAD
- tx
->plen
,
126 static int build_vnic_tx_desc(struct sdma_engine
*sde
,
127 struct vnic_txreq
*tx
,
131 u16 hdrbytes
= 2 << 2; /* PBC */
133 ret
= sdma_txinit_ahg(
136 hdrbytes
+ tx
->skb
->len
+ tx
->plen
,
146 tx
->pbc_val
= cpu_to_le64(pbc
);
147 ret
= sdma_txadd_kvaddr(
155 /* add the ulp payload */
156 ret
= build_vnic_ulp_payload(sde
, tx
);
161 /* setup the last plen bypes of pad */
162 static inline void hfi1_vnic_update_pad(unsigned char *pad
, u8 plen
)
164 pad
[HFI1_VNIC_MAX_PAD
- 1] = plen
- OPA_VNIC_ICRC_TAIL_LEN
;
167 int hfi1_vnic_send_dma(struct hfi1_devdata
*dd
, u8 q_idx
,
168 struct hfi1_vnic_vport_info
*vinfo
,
169 struct sk_buff
*skb
, u64 pbc
, u8 plen
)
171 struct hfi1_vnic_sdma
*vnic_sdma
= &vinfo
->sdma
[q_idx
];
172 struct sdma_engine
*sde
= vnic_sdma
->sde
;
173 struct vnic_txreq
*tx
;
176 if (unlikely(READ_ONCE(vnic_sdma
->state
) != HFI1_VNIC_SDMA_Q_ACTIVE
))
179 if (unlikely(!sde
|| !sdma_running(sde
)))
182 tx
= kmem_cache_alloc(dd
->vnic
.txreq_cache
, GFP_ATOMIC
);
188 tx
->sdma
= vnic_sdma
;
190 hfi1_vnic_update_pad(tx
->pad
, plen
);
192 ret
= build_vnic_tx_desc(sde
, tx
, pbc
);
196 ret
= sdma_send_txreq(sde
, iowait_get_ib_work(&vnic_sdma
->wait
),
197 &tx
->txreq
, vnic_sdma
->pkts_sent
);
198 /* When -ECOMM, sdma callback will be called with ABORT status */
199 if (unlikely(ret
&& unlikely(ret
!= -ECOMM
)))
203 vnic_sdma
->pkts_sent
= true;
204 iowait_starve_clear(vnic_sdma
->pkts_sent
, &vnic_sdma
->wait
);
209 sdma_txclean(dd
, &tx
->txreq
);
210 kmem_cache_free(dd
->vnic
.txreq_cache
, tx
);
213 dev_kfree_skb_any(skb
);
215 vnic_sdma
->pkts_sent
= false;
220 * hfi1_vnic_sdma_sleep - vnic sdma sleep function
222 * This function gets called from sdma_send_txreq() when there are not enough
223 * sdma descriptors available to send the packet. It adds Tx queue's wait
224 * structure to sdma engine's dmawait list to be woken up when descriptors
227 static int hfi1_vnic_sdma_sleep(struct sdma_engine
*sde
,
228 struct iowait_work
*wait
,
229 struct sdma_txreq
*txreq
,
233 struct hfi1_vnic_sdma
*vnic_sdma
=
234 container_of(wait
->iow
, struct hfi1_vnic_sdma
, wait
);
236 write_seqlock(&sde
->waitlock
);
237 if (sdma_progress(sde
, seq
, txreq
)) {
238 write_sequnlock(&sde
->waitlock
);
242 vnic_sdma
->state
= HFI1_VNIC_SDMA_Q_DEFERRED
;
243 if (list_empty(&vnic_sdma
->wait
.list
)) {
244 iowait_get_priority(wait
->iow
);
245 iowait_queue(pkts_sent
, wait
->iow
, &sde
->dmawait
);
247 write_sequnlock(&sde
->waitlock
);
252 * hfi1_vnic_sdma_wakeup - vnic sdma wakeup function
254 * This function gets called when SDMA descriptors becomes available and Tx
255 * queue's wait structure was previously added to sdma engine's dmawait list.
256 * It notifies the upper driver about Tx queue wakeup.
258 static void hfi1_vnic_sdma_wakeup(struct iowait
*wait
, int reason
)
260 struct hfi1_vnic_sdma
*vnic_sdma
=
261 container_of(wait
, struct hfi1_vnic_sdma
, wait
);
262 struct hfi1_vnic_vport_info
*vinfo
= vnic_sdma
->vinfo
;
264 vnic_sdma
->state
= HFI1_VNIC_SDMA_Q_ACTIVE
;
265 if (__netif_subqueue_stopped(vinfo
->netdev
, vnic_sdma
->q_idx
))
266 netif_wake_subqueue(vinfo
->netdev
, vnic_sdma
->q_idx
);
269 inline bool hfi1_vnic_sdma_write_avail(struct hfi1_vnic_vport_info
*vinfo
,
272 struct hfi1_vnic_sdma
*vnic_sdma
= &vinfo
->sdma
[q_idx
];
274 return (READ_ONCE(vnic_sdma
->state
) == HFI1_VNIC_SDMA_Q_ACTIVE
);
277 void hfi1_vnic_sdma_init(struct hfi1_vnic_vport_info
*vinfo
)
281 for (i
= 0; i
< vinfo
->num_tx_q
; i
++) {
282 struct hfi1_vnic_sdma
*vnic_sdma
= &vinfo
->sdma
[i
];
284 iowait_init(&vnic_sdma
->wait
, 0, NULL
, NULL
,
285 hfi1_vnic_sdma_sleep
,
286 hfi1_vnic_sdma_wakeup
, NULL
, NULL
);
287 vnic_sdma
->sde
= &vinfo
->dd
->per_sdma
[i
];
288 vnic_sdma
->dd
= vinfo
->dd
;
289 vnic_sdma
->vinfo
= vinfo
;
290 vnic_sdma
->q_idx
= i
;
291 vnic_sdma
->state
= HFI1_VNIC_SDMA_Q_ACTIVE
;
293 /* Add a free descriptor watermark for wakeups */
294 if (vnic_sdma
->sde
->descq_cnt
> HFI1_VNIC_SDMA_DESC_WTRMRK
) {
295 struct iowait_work
*work
;
297 INIT_LIST_HEAD(&vnic_sdma
->stx
.list
);
298 vnic_sdma
->stx
.num_desc
= HFI1_VNIC_SDMA_DESC_WTRMRK
;
299 work
= iowait_get_ib_work(&vnic_sdma
->wait
);
300 list_add_tail(&vnic_sdma
->stx
.list
, &work
->tx_head
);
305 int hfi1_vnic_txreq_init(struct hfi1_devdata
*dd
)
307 char buf
[HFI1_VNIC_TXREQ_NAME_LEN
];
309 snprintf(buf
, sizeof(buf
), "hfi1_%u_vnic_txreq_cache", dd
->unit
);
310 dd
->vnic
.txreq_cache
= kmem_cache_create(buf
,
311 sizeof(struct vnic_txreq
),
312 0, SLAB_HWCACHE_ALIGN
,
314 if (!dd
->vnic
.txreq_cache
)
319 void hfi1_vnic_txreq_deinit(struct hfi1_devdata
*dd
)
321 kmem_cache_destroy(dd
->vnic
.txreq_cache
);
322 dd
->vnic
.txreq_cache
= NULL
;