1 /* QLogic qed NIC Driver
2 * Copyright (c) 2015-2017 QLogic Corporation
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and /or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/types.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/pci.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
41 #include "qed_iscsi.h"
46 static struct qed_ooo_archipelago
47 *qed_ooo_seek_archipelago(struct qed_hwfn
*p_hwfn
,
52 u32 idx
= (cid
& 0xffff) - p_ooo_info
->cid_base
;
53 struct qed_ooo_archipelago
*p_archipelago
;
55 if (idx
>= p_ooo_info
->max_num_archipelagos
)
58 p_archipelago
= &p_ooo_info
->p_archipelagos_mem
[idx
];
60 if (list_empty(&p_archipelago
->isles_list
))
66 static struct qed_ooo_isle
*qed_ooo_seek_isle(struct qed_hwfn
*p_hwfn
,
67 struct qed_ooo_info
*p_ooo_info
,
70 struct qed_ooo_archipelago
*p_archipelago
= NULL
;
71 struct qed_ooo_isle
*p_isle
= NULL
;
72 u8 the_num_of_isle
= 1;
74 p_archipelago
= qed_ooo_seek_archipelago(p_hwfn
, p_ooo_info
, cid
);
77 "Connection %d is not found in OOO list\n", cid
);
81 list_for_each_entry(p_isle
, &p_archipelago
->isles_list
, list_entry
) {
82 if (the_num_of_isle
== isle
)
90 void qed_ooo_save_history_entry(struct qed_hwfn
*p_hwfn
,
91 struct qed_ooo_info
*p_ooo_info
,
92 struct ooo_opaque
*p_cqe
)
94 struct qed_ooo_history
*p_history
= &p_ooo_info
->ooo_history
;
96 if (p_history
->head_idx
== p_history
->num_of_cqes
)
97 p_history
->head_idx
= 0;
98 p_history
->p_cqes
[p_history
->head_idx
] = *p_cqe
;
99 p_history
->head_idx
++;
102 int qed_ooo_alloc(struct qed_hwfn
*p_hwfn
)
104 u16 max_num_archipelagos
= 0, cid_base
;
105 struct qed_ooo_info
*p_ooo_info
;
106 enum protocol_type proto
;
107 u16 max_num_isles
= 0;
110 switch (p_hwfn
->hw_info
.personality
) {
112 proto
= PROTOCOLID_ISCSI
;
114 case QED_PCI_ETH_RDMA
:
115 case QED_PCI_ETH_IWARP
:
116 proto
= PROTOCOLID_IWARP
;
120 "Failed to allocate qed_ooo_info: unknown personality\n");
124 max_num_archipelagos
= (u16
)qed_cxt_get_proto_cid_count(p_hwfn
, proto
,
126 max_num_isles
= QED_MAX_NUM_ISLES
+ max_num_archipelagos
;
127 cid_base
= (u16
)qed_cxt_get_proto_cid_start(p_hwfn
, proto
);
129 if (!max_num_archipelagos
) {
131 "Failed to allocate qed_ooo_info: unknown amount of connections\n");
135 p_ooo_info
= kzalloc(sizeof(*p_ooo_info
), GFP_KERNEL
);
139 p_ooo_info
->cid_base
= cid_base
;
140 p_ooo_info
->max_num_archipelagos
= max_num_archipelagos
;
142 INIT_LIST_HEAD(&p_ooo_info
->free_buffers_list
);
143 INIT_LIST_HEAD(&p_ooo_info
->ready_buffers_list
);
144 INIT_LIST_HEAD(&p_ooo_info
->free_isles_list
);
146 p_ooo_info
->p_isles_mem
= kcalloc(max_num_isles
,
147 sizeof(struct qed_ooo_isle
),
149 if (!p_ooo_info
->p_isles_mem
)
152 for (i
= 0; i
< max_num_isles
; i
++) {
153 INIT_LIST_HEAD(&p_ooo_info
->p_isles_mem
[i
].buffers_list
);
154 list_add_tail(&p_ooo_info
->p_isles_mem
[i
].list_entry
,
155 &p_ooo_info
->free_isles_list
);
158 p_ooo_info
->p_archipelagos_mem
=
159 kcalloc(max_num_archipelagos
,
160 sizeof(struct qed_ooo_archipelago
),
162 if (!p_ooo_info
->p_archipelagos_mem
)
163 goto no_archipelagos_mem
;
165 for (i
= 0; i
< max_num_archipelagos
; i
++)
166 INIT_LIST_HEAD(&p_ooo_info
->p_archipelagos_mem
[i
].isles_list
);
168 p_ooo_info
->ooo_history
.p_cqes
=
169 kcalloc(QED_MAX_NUM_OOO_HISTORY_ENTRIES
,
170 sizeof(struct ooo_opaque
),
172 if (!p_ooo_info
->ooo_history
.p_cqes
)
175 p_ooo_info
->ooo_history
.num_of_cqes
= QED_MAX_NUM_OOO_HISTORY_ENTRIES
;
177 p_hwfn
->p_ooo_info
= p_ooo_info
;
181 kfree(p_ooo_info
->p_archipelagos_mem
);
183 kfree(p_ooo_info
->p_isles_mem
);
189 void qed_ooo_release_connection_isles(struct qed_hwfn
*p_hwfn
,
190 struct qed_ooo_info
*p_ooo_info
, u32 cid
)
192 struct qed_ooo_archipelago
*p_archipelago
;
193 struct qed_ooo_buffer
*p_buffer
;
194 struct qed_ooo_isle
*p_isle
;
196 p_archipelago
= qed_ooo_seek_archipelago(p_hwfn
, p_ooo_info
, cid
);
200 while (!list_empty(&p_archipelago
->isles_list
)) {
201 p_isle
= list_first_entry(&p_archipelago
->isles_list
,
202 struct qed_ooo_isle
, list_entry
);
204 list_del(&p_isle
->list_entry
);
206 while (!list_empty(&p_isle
->buffers_list
)) {
207 p_buffer
= list_first_entry(&p_isle
->buffers_list
,
208 struct qed_ooo_buffer
,
214 list_del(&p_buffer
->list_entry
);
215 list_add_tail(&p_buffer
->list_entry
,
216 &p_ooo_info
->free_buffers_list
);
218 list_add_tail(&p_isle
->list_entry
,
219 &p_ooo_info
->free_isles_list
);
223 void qed_ooo_release_all_isles(struct qed_hwfn
*p_hwfn
,
224 struct qed_ooo_info
*p_ooo_info
)
226 struct qed_ooo_archipelago
*p_archipelago
;
227 struct qed_ooo_buffer
*p_buffer
;
228 struct qed_ooo_isle
*p_isle
;
231 for (i
= 0; i
< p_ooo_info
->max_num_archipelagos
; i
++) {
232 p_archipelago
= &(p_ooo_info
->p_archipelagos_mem
[i
]);
234 while (!list_empty(&p_archipelago
->isles_list
)) {
235 p_isle
= list_first_entry(&p_archipelago
->isles_list
,
239 list_del(&p_isle
->list_entry
);
241 while (!list_empty(&p_isle
->buffers_list
)) {
243 list_first_entry(&p_isle
->buffers_list
,
244 struct qed_ooo_buffer
,
250 list_del(&p_buffer
->list_entry
);
251 list_add_tail(&p_buffer
->list_entry
,
252 &p_ooo_info
->free_buffers_list
);
254 list_add_tail(&p_isle
->list_entry
,
255 &p_ooo_info
->free_isles_list
);
258 if (!list_empty(&p_ooo_info
->ready_buffers_list
))
259 list_splice_tail_init(&p_ooo_info
->ready_buffers_list
,
260 &p_ooo_info
->free_buffers_list
);
263 void qed_ooo_setup(struct qed_hwfn
*p_hwfn
)
265 qed_ooo_release_all_isles(p_hwfn
, p_hwfn
->p_ooo_info
);
266 memset(p_hwfn
->p_ooo_info
->ooo_history
.p_cqes
, 0,
267 p_hwfn
->p_ooo_info
->ooo_history
.num_of_cqes
*
268 sizeof(struct ooo_opaque
));
269 p_hwfn
->p_ooo_info
->ooo_history
.head_idx
= 0;
272 void qed_ooo_free(struct qed_hwfn
*p_hwfn
)
274 struct qed_ooo_info
*p_ooo_info
= p_hwfn
->p_ooo_info
;
275 struct qed_ooo_buffer
*p_buffer
;
280 qed_ooo_release_all_isles(p_hwfn
, p_ooo_info
);
281 while (!list_empty(&p_ooo_info
->free_buffers_list
)) {
282 p_buffer
= list_first_entry(&p_ooo_info
->free_buffers_list
,
283 struct qed_ooo_buffer
, list_entry
);
288 list_del(&p_buffer
->list_entry
);
289 dma_free_coherent(&p_hwfn
->cdev
->pdev
->dev
,
290 p_buffer
->rx_buffer_size
,
291 p_buffer
->rx_buffer_virt_addr
,
292 p_buffer
->rx_buffer_phys_addr
);
296 kfree(p_ooo_info
->p_isles_mem
);
297 kfree(p_ooo_info
->p_archipelagos_mem
);
298 kfree(p_ooo_info
->ooo_history
.p_cqes
);
300 p_hwfn
->p_ooo_info
= NULL
;
303 void qed_ooo_put_free_buffer(struct qed_hwfn
*p_hwfn
,
304 struct qed_ooo_info
*p_ooo_info
,
305 struct qed_ooo_buffer
*p_buffer
)
307 list_add_tail(&p_buffer
->list_entry
, &p_ooo_info
->free_buffers_list
);
310 struct qed_ooo_buffer
*qed_ooo_get_free_buffer(struct qed_hwfn
*p_hwfn
,
311 struct qed_ooo_info
*p_ooo_info
)
313 struct qed_ooo_buffer
*p_buffer
= NULL
;
315 if (!list_empty(&p_ooo_info
->free_buffers_list
)) {
316 p_buffer
= list_first_entry(&p_ooo_info
->free_buffers_list
,
317 struct qed_ooo_buffer
, list_entry
);
319 list_del(&p_buffer
->list_entry
);
325 void qed_ooo_put_ready_buffer(struct qed_hwfn
*p_hwfn
,
326 struct qed_ooo_info
*p_ooo_info
,
327 struct qed_ooo_buffer
*p_buffer
, u8 on_tail
)
330 list_add_tail(&p_buffer
->list_entry
,
331 &p_ooo_info
->ready_buffers_list
);
333 list_add(&p_buffer
->list_entry
,
334 &p_ooo_info
->ready_buffers_list
);
337 struct qed_ooo_buffer
*qed_ooo_get_ready_buffer(struct qed_hwfn
*p_hwfn
,
338 struct qed_ooo_info
*p_ooo_info
)
340 struct qed_ooo_buffer
*p_buffer
= NULL
;
342 if (!list_empty(&p_ooo_info
->ready_buffers_list
)) {
343 p_buffer
= list_first_entry(&p_ooo_info
->ready_buffers_list
,
344 struct qed_ooo_buffer
, list_entry
);
346 list_del(&p_buffer
->list_entry
);
352 void qed_ooo_delete_isles(struct qed_hwfn
*p_hwfn
,
353 struct qed_ooo_info
*p_ooo_info
,
354 u32 cid
, u8 drop_isle
, u8 drop_size
)
356 struct qed_ooo_archipelago
*p_archipelago
= NULL
;
357 struct qed_ooo_isle
*p_isle
= NULL
;
360 p_archipelago
= qed_ooo_seek_archipelago(p_hwfn
, p_ooo_info
, cid
);
361 for (isle_idx
= 0; isle_idx
< drop_size
; isle_idx
++) {
362 p_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
, drop_isle
);
365 "Isle %d is not found(cid %d)\n",
369 if (list_empty(&p_isle
->buffers_list
))
371 "Isle %d is empty(cid %d)\n", drop_isle
, cid
);
373 list_splice_tail_init(&p_isle
->buffers_list
,
374 &p_ooo_info
->free_buffers_list
);
376 list_del(&p_isle
->list_entry
);
377 p_ooo_info
->cur_isles_number
--;
378 list_add(&p_isle
->list_entry
, &p_ooo_info
->free_isles_list
);
382 void qed_ooo_add_new_isle(struct qed_hwfn
*p_hwfn
,
383 struct qed_ooo_info
*p_ooo_info
,
384 u32 cid
, u8 ooo_isle
,
385 struct qed_ooo_buffer
*p_buffer
)
387 struct qed_ooo_archipelago
*p_archipelago
= NULL
;
388 struct qed_ooo_isle
*p_prev_isle
= NULL
;
389 struct qed_ooo_isle
*p_isle
= NULL
;
392 p_prev_isle
= qed_ooo_seek_isle(p_hwfn
,
393 p_ooo_info
, cid
, ooo_isle
- 1);
396 "Isle %d is not found(cid %d)\n",
401 p_archipelago
= qed_ooo_seek_archipelago(p_hwfn
, p_ooo_info
, cid
);
402 if (!p_archipelago
&& (ooo_isle
!= 1)) {
404 "Connection %d is not found in OOO list\n", cid
);
408 if (!list_empty(&p_ooo_info
->free_isles_list
)) {
409 p_isle
= list_first_entry(&p_ooo_info
->free_isles_list
,
410 struct qed_ooo_isle
, list_entry
);
412 list_del(&p_isle
->list_entry
);
413 if (!list_empty(&p_isle
->buffers_list
)) {
414 DP_NOTICE(p_hwfn
, "Free isle is not empty\n");
415 INIT_LIST_HEAD(&p_isle
->buffers_list
);
418 DP_NOTICE(p_hwfn
, "No more free isles\n");
422 if (!p_archipelago
) {
423 u32 idx
= (cid
& 0xffff) - p_ooo_info
->cid_base
;
425 p_archipelago
= &p_ooo_info
->p_archipelagos_mem
[idx
];
428 list_add(&p_buffer
->list_entry
, &p_isle
->buffers_list
);
429 p_ooo_info
->cur_isles_number
++;
430 p_ooo_info
->gen_isles_number
++;
432 if (p_ooo_info
->cur_isles_number
> p_ooo_info
->max_isles_number
)
433 p_ooo_info
->max_isles_number
= p_ooo_info
->cur_isles_number
;
436 list_add(&p_isle
->list_entry
, &p_archipelago
->isles_list
);
438 list_add(&p_isle
->list_entry
, &p_prev_isle
->list_entry
);
441 void qed_ooo_add_new_buffer(struct qed_hwfn
*p_hwfn
,
442 struct qed_ooo_info
*p_ooo_info
,
445 struct qed_ooo_buffer
*p_buffer
, u8 buffer_side
)
447 struct qed_ooo_isle
*p_isle
= NULL
;
449 p_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
, ooo_isle
);
452 "Isle %d is not found(cid %d)\n", ooo_isle
, cid
);
456 if (buffer_side
== QED_OOO_LEFT_BUF
)
457 list_add(&p_buffer
->list_entry
, &p_isle
->buffers_list
);
459 list_add_tail(&p_buffer
->list_entry
, &p_isle
->buffers_list
);
462 void qed_ooo_join_isles(struct qed_hwfn
*p_hwfn
,
463 struct qed_ooo_info
*p_ooo_info
, u32 cid
, u8 left_isle
)
465 struct qed_ooo_archipelago
*p_archipelago
= NULL
;
466 struct qed_ooo_isle
*p_right_isle
= NULL
;
467 struct qed_ooo_isle
*p_left_isle
= NULL
;
469 p_right_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
,
473 "Right isle %d is not found(cid %d)\n",
478 p_archipelago
= qed_ooo_seek_archipelago(p_hwfn
, p_ooo_info
, cid
);
479 list_del(&p_right_isle
->list_entry
);
480 p_ooo_info
->cur_isles_number
--;
482 p_left_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
,
486 "Left isle %d is not found(cid %d)\n",
490 list_splice_tail_init(&p_right_isle
->buffers_list
,
491 &p_left_isle
->buffers_list
);
493 list_splice_tail_init(&p_right_isle
->buffers_list
,
494 &p_ooo_info
->ready_buffers_list
);
496 list_add_tail(&p_right_isle
->list_entry
, &p_ooo_info
->free_isles_list
);