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_move_tail(&p_buffer
->list_entry
,
215 &p_ooo_info
->free_buffers_list
);
217 list_add_tail(&p_isle
->list_entry
,
218 &p_ooo_info
->free_isles_list
);
222 void qed_ooo_release_all_isles(struct qed_hwfn
*p_hwfn
,
223 struct qed_ooo_info
*p_ooo_info
)
225 struct qed_ooo_archipelago
*p_archipelago
;
226 struct qed_ooo_buffer
*p_buffer
;
227 struct qed_ooo_isle
*p_isle
;
230 for (i
= 0; i
< p_ooo_info
->max_num_archipelagos
; i
++) {
231 p_archipelago
= &(p_ooo_info
->p_archipelagos_mem
[i
]);
233 while (!list_empty(&p_archipelago
->isles_list
)) {
234 p_isle
= list_first_entry(&p_archipelago
->isles_list
,
238 list_del(&p_isle
->list_entry
);
240 while (!list_empty(&p_isle
->buffers_list
)) {
242 list_first_entry(&p_isle
->buffers_list
,
243 struct qed_ooo_buffer
,
249 list_move_tail(&p_buffer
->list_entry
,
250 &p_ooo_info
->free_buffers_list
);
252 list_add_tail(&p_isle
->list_entry
,
253 &p_ooo_info
->free_isles_list
);
256 if (!list_empty(&p_ooo_info
->ready_buffers_list
))
257 list_splice_tail_init(&p_ooo_info
->ready_buffers_list
,
258 &p_ooo_info
->free_buffers_list
);
261 void qed_ooo_setup(struct qed_hwfn
*p_hwfn
)
263 qed_ooo_release_all_isles(p_hwfn
, p_hwfn
->p_ooo_info
);
264 memset(p_hwfn
->p_ooo_info
->ooo_history
.p_cqes
, 0,
265 p_hwfn
->p_ooo_info
->ooo_history
.num_of_cqes
*
266 sizeof(struct ooo_opaque
));
267 p_hwfn
->p_ooo_info
->ooo_history
.head_idx
= 0;
270 void qed_ooo_free(struct qed_hwfn
*p_hwfn
)
272 struct qed_ooo_info
*p_ooo_info
= p_hwfn
->p_ooo_info
;
273 struct qed_ooo_buffer
*p_buffer
;
278 qed_ooo_release_all_isles(p_hwfn
, p_ooo_info
);
279 while (!list_empty(&p_ooo_info
->free_buffers_list
)) {
280 p_buffer
= list_first_entry(&p_ooo_info
->free_buffers_list
,
281 struct qed_ooo_buffer
, list_entry
);
286 list_del(&p_buffer
->list_entry
);
287 dma_free_coherent(&p_hwfn
->cdev
->pdev
->dev
,
288 p_buffer
->rx_buffer_size
,
289 p_buffer
->rx_buffer_virt_addr
,
290 p_buffer
->rx_buffer_phys_addr
);
294 kfree(p_ooo_info
->p_isles_mem
);
295 kfree(p_ooo_info
->p_archipelagos_mem
);
296 kfree(p_ooo_info
->ooo_history
.p_cqes
);
298 p_hwfn
->p_ooo_info
= NULL
;
301 void qed_ooo_put_free_buffer(struct qed_hwfn
*p_hwfn
,
302 struct qed_ooo_info
*p_ooo_info
,
303 struct qed_ooo_buffer
*p_buffer
)
305 list_add_tail(&p_buffer
->list_entry
, &p_ooo_info
->free_buffers_list
);
308 struct qed_ooo_buffer
*qed_ooo_get_free_buffer(struct qed_hwfn
*p_hwfn
,
309 struct qed_ooo_info
*p_ooo_info
)
311 struct qed_ooo_buffer
*p_buffer
= NULL
;
313 if (!list_empty(&p_ooo_info
->free_buffers_list
)) {
314 p_buffer
= list_first_entry(&p_ooo_info
->free_buffers_list
,
315 struct qed_ooo_buffer
, list_entry
);
317 list_del(&p_buffer
->list_entry
);
323 void qed_ooo_put_ready_buffer(struct qed_hwfn
*p_hwfn
,
324 struct qed_ooo_info
*p_ooo_info
,
325 struct qed_ooo_buffer
*p_buffer
, u8 on_tail
)
328 list_add_tail(&p_buffer
->list_entry
,
329 &p_ooo_info
->ready_buffers_list
);
331 list_add(&p_buffer
->list_entry
,
332 &p_ooo_info
->ready_buffers_list
);
335 struct qed_ooo_buffer
*qed_ooo_get_ready_buffer(struct qed_hwfn
*p_hwfn
,
336 struct qed_ooo_info
*p_ooo_info
)
338 struct qed_ooo_buffer
*p_buffer
= NULL
;
340 if (!list_empty(&p_ooo_info
->ready_buffers_list
)) {
341 p_buffer
= list_first_entry(&p_ooo_info
->ready_buffers_list
,
342 struct qed_ooo_buffer
, list_entry
);
344 list_del(&p_buffer
->list_entry
);
350 void qed_ooo_delete_isles(struct qed_hwfn
*p_hwfn
,
351 struct qed_ooo_info
*p_ooo_info
,
352 u32 cid
, u8 drop_isle
, u8 drop_size
)
354 struct qed_ooo_isle
*p_isle
= NULL
;
357 for (isle_idx
= 0; isle_idx
< drop_size
; isle_idx
++) {
358 p_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
, drop_isle
);
361 "Isle %d is not found(cid %d)\n",
365 if (list_empty(&p_isle
->buffers_list
))
367 "Isle %d is empty(cid %d)\n", drop_isle
, cid
);
369 list_splice_tail_init(&p_isle
->buffers_list
,
370 &p_ooo_info
->free_buffers_list
);
372 list_del(&p_isle
->list_entry
);
373 p_ooo_info
->cur_isles_number
--;
374 list_add(&p_isle
->list_entry
, &p_ooo_info
->free_isles_list
);
378 void qed_ooo_add_new_isle(struct qed_hwfn
*p_hwfn
,
379 struct qed_ooo_info
*p_ooo_info
,
380 u32 cid
, u8 ooo_isle
,
381 struct qed_ooo_buffer
*p_buffer
)
383 struct qed_ooo_archipelago
*p_archipelago
= NULL
;
384 struct qed_ooo_isle
*p_prev_isle
= NULL
;
385 struct qed_ooo_isle
*p_isle
= NULL
;
388 p_prev_isle
= qed_ooo_seek_isle(p_hwfn
,
389 p_ooo_info
, cid
, ooo_isle
- 1);
392 "Isle %d is not found(cid %d)\n",
397 p_archipelago
= qed_ooo_seek_archipelago(p_hwfn
, p_ooo_info
, cid
);
398 if (!p_archipelago
&& (ooo_isle
!= 1)) {
400 "Connection %d is not found in OOO list\n", cid
);
404 if (!list_empty(&p_ooo_info
->free_isles_list
)) {
405 p_isle
= list_first_entry(&p_ooo_info
->free_isles_list
,
406 struct qed_ooo_isle
, list_entry
);
408 list_del(&p_isle
->list_entry
);
409 if (!list_empty(&p_isle
->buffers_list
)) {
410 DP_NOTICE(p_hwfn
, "Free isle is not empty\n");
411 INIT_LIST_HEAD(&p_isle
->buffers_list
);
414 DP_NOTICE(p_hwfn
, "No more free isles\n");
418 if (!p_archipelago
) {
419 u32 idx
= (cid
& 0xffff) - p_ooo_info
->cid_base
;
421 p_archipelago
= &p_ooo_info
->p_archipelagos_mem
[idx
];
424 list_add(&p_buffer
->list_entry
, &p_isle
->buffers_list
);
425 p_ooo_info
->cur_isles_number
++;
426 p_ooo_info
->gen_isles_number
++;
428 if (p_ooo_info
->cur_isles_number
> p_ooo_info
->max_isles_number
)
429 p_ooo_info
->max_isles_number
= p_ooo_info
->cur_isles_number
;
432 list_add(&p_isle
->list_entry
, &p_archipelago
->isles_list
);
434 list_add(&p_isle
->list_entry
, &p_prev_isle
->list_entry
);
437 void qed_ooo_add_new_buffer(struct qed_hwfn
*p_hwfn
,
438 struct qed_ooo_info
*p_ooo_info
,
441 struct qed_ooo_buffer
*p_buffer
, u8 buffer_side
)
443 struct qed_ooo_isle
*p_isle
= NULL
;
445 p_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
, ooo_isle
);
448 "Isle %d is not found(cid %d)\n", ooo_isle
, cid
);
452 if (buffer_side
== QED_OOO_LEFT_BUF
)
453 list_add(&p_buffer
->list_entry
, &p_isle
->buffers_list
);
455 list_add_tail(&p_buffer
->list_entry
, &p_isle
->buffers_list
);
458 void qed_ooo_join_isles(struct qed_hwfn
*p_hwfn
,
459 struct qed_ooo_info
*p_ooo_info
, u32 cid
, u8 left_isle
)
461 struct qed_ooo_isle
*p_right_isle
= NULL
;
462 struct qed_ooo_isle
*p_left_isle
= NULL
;
464 p_right_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
,
468 "Right isle %d is not found(cid %d)\n",
473 list_del(&p_right_isle
->list_entry
);
474 p_ooo_info
->cur_isles_number
--;
476 p_left_isle
= qed_ooo_seek_isle(p_hwfn
, p_ooo_info
, cid
,
480 "Left isle %d is not found(cid %d)\n",
484 list_splice_tail_init(&p_right_isle
->buffers_list
,
485 &p_left_isle
->buffers_list
);
487 list_splice_tail_init(&p_right_isle
->buffers_list
,
488 &p_ooo_info
->ready_buffers_list
);
490 list_add_tail(&p_right_isle
->list_entry
, &p_ooo_info
->free_isles_list
);