treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / ethernet / qlogic / qed / qed_ooo.c
blobffac4ac8739418f82f880c401fd205bc08130dad
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
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>
40 #include "qed.h"
41 #include "qed_iscsi.h"
42 #include "qed_ll2.h"
43 #include "qed_ooo.h"
44 #include "qed_cxt.h"
46 static struct qed_ooo_archipelago
47 *qed_ooo_seek_archipelago(struct qed_hwfn *p_hwfn,
48 struct qed_ooo_info
49 *p_ooo_info,
50 u32 cid)
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)
56 return NULL;
58 p_archipelago = &p_ooo_info->p_archipelagos_mem[idx];
60 if (list_empty(&p_archipelago->isles_list))
61 return NULL;
63 return p_archipelago;
66 static struct qed_ooo_isle *qed_ooo_seek_isle(struct qed_hwfn *p_hwfn,
67 struct qed_ooo_info *p_ooo_info,
68 u32 cid, u8 isle)
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);
75 if (!p_archipelago) {
76 DP_NOTICE(p_hwfn,
77 "Connection %d is not found in OOO list\n", cid);
78 return NULL;
81 list_for_each_entry(p_isle, &p_archipelago->isles_list, list_entry) {
82 if (the_num_of_isle == isle)
83 return p_isle;
84 the_num_of_isle++;
87 return NULL;
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;
108 u32 i;
110 switch (p_hwfn->hw_info.personality) {
111 case QED_PCI_ISCSI:
112 proto = PROTOCOLID_ISCSI;
113 break;
114 case QED_PCI_ETH_RDMA:
115 case QED_PCI_ETH_IWARP:
116 proto = PROTOCOLID_IWARP;
117 break;
118 default:
119 DP_NOTICE(p_hwfn,
120 "Failed to allocate qed_ooo_info: unknown personality\n");
121 return -EINVAL;
124 max_num_archipelagos = (u16)qed_cxt_get_proto_cid_count(p_hwfn, proto,
125 NULL);
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) {
130 DP_NOTICE(p_hwfn,
131 "Failed to allocate qed_ooo_info: unknown amount of connections\n");
132 return -EINVAL;
135 p_ooo_info = kzalloc(sizeof(*p_ooo_info), GFP_KERNEL);
136 if (!p_ooo_info)
137 return -ENOMEM;
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),
148 GFP_KERNEL);
149 if (!p_ooo_info->p_isles_mem)
150 goto no_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),
161 GFP_KERNEL);
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),
171 GFP_KERNEL);
172 if (!p_ooo_info->ooo_history.p_cqes)
173 goto no_history_mem;
175 p_ooo_info->ooo_history.num_of_cqes = QED_MAX_NUM_OOO_HISTORY_ENTRIES;
177 p_hwfn->p_ooo_info = p_ooo_info;
178 return 0;
180 no_history_mem:
181 kfree(p_ooo_info->p_archipelagos_mem);
182 no_archipelagos_mem:
183 kfree(p_ooo_info->p_isles_mem);
184 no_isles_mem:
185 kfree(p_ooo_info);
186 return -ENOMEM;
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);
197 if (!p_archipelago)
198 return;
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,
209 list_entry);
211 if (!p_buffer)
212 break;
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;
228 u32 i;
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,
235 struct qed_ooo_isle,
236 list_entry);
238 list_del(&p_isle->list_entry);
240 while (!list_empty(&p_isle->buffers_list)) {
241 p_buffer =
242 list_first_entry(&p_isle->buffers_list,
243 struct qed_ooo_buffer,
244 list_entry);
246 if (!p_buffer)
247 break;
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;
275 if (!p_ooo_info)
276 return;
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);
283 if (!p_buffer)
284 break;
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);
291 kfree(p_buffer);
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);
297 kfree(p_ooo_info);
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);
320 return p_buffer;
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)
327 if (on_tail)
328 list_add_tail(&p_buffer->list_entry,
329 &p_ooo_info->ready_buffers_list);
330 else
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);
347 return p_buffer;
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;
355 u8 isle_idx;
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);
359 if (!p_isle) {
360 DP_NOTICE(p_hwfn,
361 "Isle %d is not found(cid %d)\n",
362 drop_isle, cid);
363 return;
365 if (list_empty(&p_isle->buffers_list))
366 DP_NOTICE(p_hwfn,
367 "Isle %d is empty(cid %d)\n", drop_isle, cid);
368 else
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;
387 if (ooo_isle > 1) {
388 p_prev_isle = qed_ooo_seek_isle(p_hwfn,
389 p_ooo_info, cid, ooo_isle - 1);
390 if (!p_prev_isle) {
391 DP_NOTICE(p_hwfn,
392 "Isle %d is not found(cid %d)\n",
393 ooo_isle - 1, cid);
394 return;
397 p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid);
398 if (!p_archipelago && (ooo_isle != 1)) {
399 DP_NOTICE(p_hwfn,
400 "Connection %d is not found in OOO list\n", cid);
401 return;
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);
413 } else {
414 DP_NOTICE(p_hwfn, "No more free isles\n");
415 return;
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;
431 if (!p_prev_isle)
432 list_add(&p_isle->list_entry, &p_archipelago->isles_list);
433 else
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,
439 u32 cid,
440 u8 ooo_isle,
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);
446 if (!p_isle) {
447 DP_NOTICE(p_hwfn,
448 "Isle %d is not found(cid %d)\n", ooo_isle, cid);
449 return;
452 if (buffer_side == QED_OOO_LEFT_BUF)
453 list_add(&p_buffer->list_entry, &p_isle->buffers_list);
454 else
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,
465 left_isle + 1);
466 if (!p_right_isle) {
467 DP_NOTICE(p_hwfn,
468 "Right isle %d is not found(cid %d)\n",
469 left_isle + 1, cid);
470 return;
473 list_del(&p_right_isle->list_entry);
474 p_ooo_info->cur_isles_number--;
475 if (left_isle) {
476 p_left_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid,
477 left_isle);
478 if (!p_left_isle) {
479 DP_NOTICE(p_hwfn,
480 "Left isle %d is not found(cid %d)\n",
481 left_isle, cid);
482 return;
484 list_splice_tail_init(&p_right_isle->buffers_list,
485 &p_left_isle->buffers_list);
486 } else {
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);