1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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.
31  */
32 
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"
45 
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)
51 {
52 	u32 idx = (cid & 0xffff) - p_ooo_info->cid_base;
53 	struct qed_ooo_archipelago *p_archipelago;
54 
55 	if (idx >= p_ooo_info->max_num_archipelagos)
56 		return NULL;
57 
58 	p_archipelago = &p_ooo_info->p_archipelagos_mem[idx];
59 
60 	if (list_empty(&p_archipelago->isles_list))
61 		return NULL;
62 
63 	return p_archipelago;
64 }
65 
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)
69 {
70 	struct qed_ooo_archipelago *p_archipelago = NULL;
71 	struct qed_ooo_isle *p_isle = NULL;
72 	u8 the_num_of_isle = 1;
73 
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;
79 	}
80 
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++;
85 	}
86 
87 	return NULL;
88 }
89 
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)
93 {
94 	struct qed_ooo_history *p_history = &p_ooo_info->ooo_history;
95 
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++;
100 }
101 
102 int qed_ooo_alloc(struct qed_hwfn *p_hwfn)
103 {
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;
109 
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;
122 	}
123 
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);
128 
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;
133 	}
134 
135 	p_ooo_info = kzalloc(sizeof(*p_ooo_info), GFP_KERNEL);
136 	if (!p_ooo_info)
137 		return -ENOMEM;
138 
139 	p_ooo_info->cid_base = cid_base;
140 	p_ooo_info->max_num_archipelagos = max_num_archipelagos;
141 
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);
145 
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;
151 
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);
156 	}
157 
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;
164 
165 	for (i = 0; i < max_num_archipelagos; i++)
166 		INIT_LIST_HEAD(&p_ooo_info->p_archipelagos_mem[i].isles_list);
167 
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;
174 
175 	p_ooo_info->ooo_history.num_of_cqes = QED_MAX_NUM_OOO_HISTORY_ENTRIES;
176 
177 	p_hwfn->p_ooo_info = p_ooo_info;
178 	return 0;
179 
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;
187 }
188 
189 void qed_ooo_release_connection_isles(struct qed_hwfn *p_hwfn,
190 				      struct qed_ooo_info *p_ooo_info, u32 cid)
191 {
192 	struct qed_ooo_archipelago *p_archipelago;
193 	struct qed_ooo_buffer *p_buffer;
194 	struct qed_ooo_isle *p_isle;
195 
196 	p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid);
197 	if (!p_archipelago)
198 		return;
199 
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);
203 
204 		list_del(&p_isle->list_entry);
205 
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);
210 
211 			if (!p_buffer)
212 				break;
213 
214 			list_del(&p_buffer->list_entry);
215 			list_add_tail(&p_buffer->list_entry,
216 				      &p_ooo_info->free_buffers_list);
217 		}
218 		list_add_tail(&p_isle->list_entry,
219 			      &p_ooo_info->free_isles_list);
220 	}
221 }
222 
223 void qed_ooo_release_all_isles(struct qed_hwfn *p_hwfn,
224 			       struct qed_ooo_info *p_ooo_info)
225 {
226 	struct qed_ooo_archipelago *p_archipelago;
227 	struct qed_ooo_buffer *p_buffer;
228 	struct qed_ooo_isle *p_isle;
229 	u32 i;
230 
231 	for (i = 0; i < p_ooo_info->max_num_archipelagos; i++) {
232 		p_archipelago = &(p_ooo_info->p_archipelagos_mem[i]);
233 
234 		while (!list_empty(&p_archipelago->isles_list)) {
235 			p_isle = list_first_entry(&p_archipelago->isles_list,
236 						  struct qed_ooo_isle,
237 						  list_entry);
238 
239 			list_del(&p_isle->list_entry);
240 
241 			while (!list_empty(&p_isle->buffers_list)) {
242 				p_buffer =
243 				    list_first_entry(&p_isle->buffers_list,
244 						     struct qed_ooo_buffer,
245 						     list_entry);
246 
247 				if (!p_buffer)
248 					break;
249 
250 			list_del(&p_buffer->list_entry);
251 				list_add_tail(&p_buffer->list_entry,
252 					      &p_ooo_info->free_buffers_list);
253 			}
254 			list_add_tail(&p_isle->list_entry,
255 				      &p_ooo_info->free_isles_list);
256 		}
257 	}
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);
261 }
262 
263 void qed_ooo_setup(struct qed_hwfn *p_hwfn)
264 {
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;
270 }
271 
272 void qed_ooo_free(struct qed_hwfn *p_hwfn)
273 {
274 	struct qed_ooo_info *p_ooo_info  = p_hwfn->p_ooo_info;
275 	struct qed_ooo_buffer *p_buffer;
276 
277 	if (!p_ooo_info)
278 		return;
279 
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);
284 
285 		if (!p_buffer)
286 			break;
287 
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);
293 		kfree(p_buffer);
294 	}
295 
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);
299 	kfree(p_ooo_info);
300 	p_hwfn->p_ooo_info = NULL;
301 }
302 
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)
306 {
307 	list_add_tail(&p_buffer->list_entry, &p_ooo_info->free_buffers_list);
308 }
309 
310 struct qed_ooo_buffer *qed_ooo_get_free_buffer(struct qed_hwfn *p_hwfn,
311 					       struct qed_ooo_info *p_ooo_info)
312 {
313 	struct qed_ooo_buffer *p_buffer = NULL;
314 
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);
318 
319 		list_del(&p_buffer->list_entry);
320 	}
321 
322 	return p_buffer;
323 }
324 
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)
328 {
329 	if (on_tail)
330 		list_add_tail(&p_buffer->list_entry,
331 			      &p_ooo_info->ready_buffers_list);
332 	else
333 		list_add(&p_buffer->list_entry,
334 			 &p_ooo_info->ready_buffers_list);
335 }
336 
337 struct qed_ooo_buffer *qed_ooo_get_ready_buffer(struct qed_hwfn *p_hwfn,
338 						struct qed_ooo_info *p_ooo_info)
339 {
340 	struct qed_ooo_buffer *p_buffer = NULL;
341 
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);
345 
346 		list_del(&p_buffer->list_entry);
347 	}
348 
349 	return p_buffer;
350 }
351 
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)
355 {
356 	struct qed_ooo_archipelago *p_archipelago = NULL;
357 	struct qed_ooo_isle *p_isle = NULL;
358 	u8 isle_idx;
359 
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);
363 		if (!p_isle) {
364 			DP_NOTICE(p_hwfn,
365 				  "Isle %d is not found(cid %d)\n",
366 				  drop_isle, cid);
367 			return;
368 		}
369 		if (list_empty(&p_isle->buffers_list))
370 			DP_NOTICE(p_hwfn,
371 				  "Isle %d is empty(cid %d)\n", drop_isle, cid);
372 		else
373 			list_splice_tail_init(&p_isle->buffers_list,
374 					      &p_ooo_info->free_buffers_list);
375 
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);
379 	}
380 }
381 
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)
386 {
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;
390 
391 	if (ooo_isle > 1) {
392 		p_prev_isle = qed_ooo_seek_isle(p_hwfn,
393 						p_ooo_info, cid, ooo_isle - 1);
394 		if (!p_prev_isle) {
395 			DP_NOTICE(p_hwfn,
396 				  "Isle %d is not found(cid %d)\n",
397 				  ooo_isle - 1, cid);
398 			return;
399 		}
400 	}
401 	p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid);
402 	if (!p_archipelago && (ooo_isle != 1)) {
403 		DP_NOTICE(p_hwfn,
404 			  "Connection %d is not found in OOO list\n", cid);
405 		return;
406 	}
407 
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);
411 
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);
416 		}
417 	} else {
418 		DP_NOTICE(p_hwfn, "No more free isles\n");
419 		return;
420 	}
421 
422 	if (!p_archipelago) {
423 		u32 idx = (cid & 0xffff) - p_ooo_info->cid_base;
424 
425 		p_archipelago = &p_ooo_info->p_archipelagos_mem[idx];
426 	}
427 
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++;
431 
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;
434 
435 	if (!p_prev_isle)
436 		list_add(&p_isle->list_entry, &p_archipelago->isles_list);
437 	else
438 		list_add(&p_isle->list_entry, &p_prev_isle->list_entry);
439 }
440 
441 void qed_ooo_add_new_buffer(struct qed_hwfn *p_hwfn,
442 			    struct qed_ooo_info *p_ooo_info,
443 			    u32 cid,
444 			    u8 ooo_isle,
445 			    struct qed_ooo_buffer *p_buffer, u8 buffer_side)
446 {
447 	struct qed_ooo_isle *p_isle = NULL;
448 
449 	p_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid, ooo_isle);
450 	if (!p_isle) {
451 		DP_NOTICE(p_hwfn,
452 			  "Isle %d is not found(cid %d)\n", ooo_isle, cid);
453 		return;
454 	}
455 
456 	if (buffer_side == QED_OOO_LEFT_BUF)
457 		list_add(&p_buffer->list_entry, &p_isle->buffers_list);
458 	else
459 		list_add_tail(&p_buffer->list_entry, &p_isle->buffers_list);
460 }
461 
462 void qed_ooo_join_isles(struct qed_hwfn *p_hwfn,
463 			struct qed_ooo_info *p_ooo_info, u32 cid, u8 left_isle)
464 {
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;
468 
469 	p_right_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid,
470 					 left_isle + 1);
471 	if (!p_right_isle) {
472 		DP_NOTICE(p_hwfn,
473 			  "Right isle %d is not found(cid %d)\n",
474 			  left_isle + 1, cid);
475 		return;
476 	}
477 
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--;
481 	if (left_isle) {
482 		p_left_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid,
483 						left_isle);
484 		if (!p_left_isle) {
485 			DP_NOTICE(p_hwfn,
486 				  "Left isle %d is not found(cid %d)\n",
487 				  left_isle, cid);
488 			return;
489 		}
490 		list_splice_tail_init(&p_right_isle->buffers_list,
491 				      &p_left_isle->buffers_list);
492 	} else {
493 		list_splice_tail_init(&p_right_isle->buffers_list,
494 				      &p_ooo_info->ready_buffers_list);
495 	}
496 	list_add_tail(&p_right_isle->list_entry, &p_ooo_info->free_isles_list);
497 }
498