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 u16 max_num_isles = 0; 107 u32 i; 108 109 if (p_hwfn->hw_info.personality != QED_PCI_ISCSI) { 110 DP_NOTICE(p_hwfn, 111 "Failed to allocate qed_ooo_info: unknown personality\n"); 112 return -EINVAL; 113 } 114 115 max_num_archipelagos = p_hwfn->pf_params.iscsi_pf_params.num_cons; 116 max_num_isles = QED_MAX_NUM_ISLES + max_num_archipelagos; 117 cid_base = (u16)qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_ISCSI); 118 119 if (!max_num_archipelagos) { 120 DP_NOTICE(p_hwfn, 121 "Failed to allocate qed_ooo_info: unknown amount of connections\n"); 122 return -EINVAL; 123 } 124 125 p_ooo_info = kzalloc(sizeof(*p_ooo_info), GFP_KERNEL); 126 if (!p_ooo_info) 127 return -ENOMEM; 128 129 p_ooo_info->cid_base = cid_base; 130 p_ooo_info->max_num_archipelagos = max_num_archipelagos; 131 132 INIT_LIST_HEAD(&p_ooo_info->free_buffers_list); 133 INIT_LIST_HEAD(&p_ooo_info->ready_buffers_list); 134 INIT_LIST_HEAD(&p_ooo_info->free_isles_list); 135 136 p_ooo_info->p_isles_mem = kcalloc(max_num_isles, 137 sizeof(struct qed_ooo_isle), 138 GFP_KERNEL); 139 if (!p_ooo_info->p_isles_mem) 140 goto no_isles_mem; 141 142 for (i = 0; i < max_num_isles; i++) { 143 INIT_LIST_HEAD(&p_ooo_info->p_isles_mem[i].buffers_list); 144 list_add_tail(&p_ooo_info->p_isles_mem[i].list_entry, 145 &p_ooo_info->free_isles_list); 146 } 147 148 p_ooo_info->p_archipelagos_mem = 149 kcalloc(max_num_archipelagos, 150 sizeof(struct qed_ooo_archipelago), 151 GFP_KERNEL); 152 if (!p_ooo_info->p_archipelagos_mem) 153 goto no_archipelagos_mem; 154 155 for (i = 0; i < max_num_archipelagos; i++) 156 INIT_LIST_HEAD(&p_ooo_info->p_archipelagos_mem[i].isles_list); 157 158 p_ooo_info->ooo_history.p_cqes = 159 kcalloc(QED_MAX_NUM_OOO_HISTORY_ENTRIES, 160 sizeof(struct ooo_opaque), 161 GFP_KERNEL); 162 if (!p_ooo_info->ooo_history.p_cqes) 163 goto no_history_mem; 164 165 p_ooo_info->ooo_history.num_of_cqes = QED_MAX_NUM_OOO_HISTORY_ENTRIES; 166 167 p_hwfn->p_ooo_info = p_ooo_info; 168 return 0; 169 170 no_history_mem: 171 kfree(p_ooo_info->p_archipelagos_mem); 172 no_archipelagos_mem: 173 kfree(p_ooo_info->p_isles_mem); 174 no_isles_mem: 175 kfree(p_ooo_info); 176 return -ENOMEM; 177 } 178 179 void qed_ooo_release_connection_isles(struct qed_hwfn *p_hwfn, 180 struct qed_ooo_info *p_ooo_info, u32 cid) 181 { 182 struct qed_ooo_archipelago *p_archipelago; 183 struct qed_ooo_buffer *p_buffer; 184 struct qed_ooo_isle *p_isle; 185 186 p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid); 187 if (!p_archipelago) 188 return; 189 190 while (!list_empty(&p_archipelago->isles_list)) { 191 p_isle = list_first_entry(&p_archipelago->isles_list, 192 struct qed_ooo_isle, list_entry); 193 194 list_del(&p_isle->list_entry); 195 196 while (!list_empty(&p_isle->buffers_list)) { 197 p_buffer = list_first_entry(&p_isle->buffers_list, 198 struct qed_ooo_buffer, 199 list_entry); 200 201 if (!p_buffer) 202 break; 203 204 list_del(&p_buffer->list_entry); 205 list_add_tail(&p_buffer->list_entry, 206 &p_ooo_info->free_buffers_list); 207 } 208 list_add_tail(&p_isle->list_entry, 209 &p_ooo_info->free_isles_list); 210 } 211 } 212 213 void qed_ooo_release_all_isles(struct qed_hwfn *p_hwfn, 214 struct qed_ooo_info *p_ooo_info) 215 { 216 struct qed_ooo_archipelago *p_archipelago; 217 struct qed_ooo_buffer *p_buffer; 218 struct qed_ooo_isle *p_isle; 219 u32 i; 220 221 for (i = 0; i < p_ooo_info->max_num_archipelagos; i++) { 222 p_archipelago = &(p_ooo_info->p_archipelagos_mem[i]); 223 224 while (!list_empty(&p_archipelago->isles_list)) { 225 p_isle = list_first_entry(&p_archipelago->isles_list, 226 struct qed_ooo_isle, 227 list_entry); 228 229 list_del(&p_isle->list_entry); 230 231 while (!list_empty(&p_isle->buffers_list)) { 232 p_buffer = 233 list_first_entry(&p_isle->buffers_list, 234 struct qed_ooo_buffer, 235 list_entry); 236 237 if (!p_buffer) 238 break; 239 240 list_del(&p_buffer->list_entry); 241 list_add_tail(&p_buffer->list_entry, 242 &p_ooo_info->free_buffers_list); 243 } 244 list_add_tail(&p_isle->list_entry, 245 &p_ooo_info->free_isles_list); 246 } 247 } 248 if (!list_empty(&p_ooo_info->ready_buffers_list)) 249 list_splice_tail_init(&p_ooo_info->ready_buffers_list, 250 &p_ooo_info->free_buffers_list); 251 } 252 253 void qed_ooo_setup(struct qed_hwfn *p_hwfn) 254 { 255 qed_ooo_release_all_isles(p_hwfn, p_hwfn->p_ooo_info); 256 memset(p_hwfn->p_ooo_info->ooo_history.p_cqes, 0, 257 p_hwfn->p_ooo_info->ooo_history.num_of_cqes * 258 sizeof(struct ooo_opaque)); 259 p_hwfn->p_ooo_info->ooo_history.head_idx = 0; 260 } 261 262 void qed_ooo_free(struct qed_hwfn *p_hwfn) 263 { 264 struct qed_ooo_info *p_ooo_info = p_hwfn->p_ooo_info; 265 struct qed_ooo_buffer *p_buffer; 266 267 if (!p_ooo_info) 268 return; 269 270 qed_ooo_release_all_isles(p_hwfn, p_ooo_info); 271 while (!list_empty(&p_ooo_info->free_buffers_list)) { 272 p_buffer = list_first_entry(&p_ooo_info->free_buffers_list, 273 struct qed_ooo_buffer, list_entry); 274 275 if (!p_buffer) 276 break; 277 278 list_del(&p_buffer->list_entry); 279 dma_free_coherent(&p_hwfn->cdev->pdev->dev, 280 p_buffer->rx_buffer_size, 281 p_buffer->rx_buffer_virt_addr, 282 p_buffer->rx_buffer_phys_addr); 283 kfree(p_buffer); 284 } 285 286 kfree(p_ooo_info->p_isles_mem); 287 kfree(p_ooo_info->p_archipelagos_mem); 288 kfree(p_ooo_info->ooo_history.p_cqes); 289 kfree(p_ooo_info); 290 p_hwfn->p_ooo_info = NULL; 291 } 292 293 void qed_ooo_put_free_buffer(struct qed_hwfn *p_hwfn, 294 struct qed_ooo_info *p_ooo_info, 295 struct qed_ooo_buffer *p_buffer) 296 { 297 list_add_tail(&p_buffer->list_entry, &p_ooo_info->free_buffers_list); 298 } 299 300 struct qed_ooo_buffer *qed_ooo_get_free_buffer(struct qed_hwfn *p_hwfn, 301 struct qed_ooo_info *p_ooo_info) 302 { 303 struct qed_ooo_buffer *p_buffer = NULL; 304 305 if (!list_empty(&p_ooo_info->free_buffers_list)) { 306 p_buffer = list_first_entry(&p_ooo_info->free_buffers_list, 307 struct qed_ooo_buffer, list_entry); 308 309 list_del(&p_buffer->list_entry); 310 } 311 312 return p_buffer; 313 } 314 315 void qed_ooo_put_ready_buffer(struct qed_hwfn *p_hwfn, 316 struct qed_ooo_info *p_ooo_info, 317 struct qed_ooo_buffer *p_buffer, u8 on_tail) 318 { 319 if (on_tail) 320 list_add_tail(&p_buffer->list_entry, 321 &p_ooo_info->ready_buffers_list); 322 else 323 list_add(&p_buffer->list_entry, 324 &p_ooo_info->ready_buffers_list); 325 } 326 327 struct qed_ooo_buffer *qed_ooo_get_ready_buffer(struct qed_hwfn *p_hwfn, 328 struct qed_ooo_info *p_ooo_info) 329 { 330 struct qed_ooo_buffer *p_buffer = NULL; 331 332 if (!list_empty(&p_ooo_info->ready_buffers_list)) { 333 p_buffer = list_first_entry(&p_ooo_info->ready_buffers_list, 334 struct qed_ooo_buffer, list_entry); 335 336 list_del(&p_buffer->list_entry); 337 } 338 339 return p_buffer; 340 } 341 342 void qed_ooo_delete_isles(struct qed_hwfn *p_hwfn, 343 struct qed_ooo_info *p_ooo_info, 344 u32 cid, u8 drop_isle, u8 drop_size) 345 { 346 struct qed_ooo_archipelago *p_archipelago = NULL; 347 struct qed_ooo_isle *p_isle = NULL; 348 u8 isle_idx; 349 350 p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid); 351 for (isle_idx = 0; isle_idx < drop_size; isle_idx++) { 352 p_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid, drop_isle); 353 if (!p_isle) { 354 DP_NOTICE(p_hwfn, 355 "Isle %d is not found(cid %d)\n", 356 drop_isle, cid); 357 return; 358 } 359 if (list_empty(&p_isle->buffers_list)) 360 DP_NOTICE(p_hwfn, 361 "Isle %d is empty(cid %d)\n", drop_isle, cid); 362 else 363 list_splice_tail_init(&p_isle->buffers_list, 364 &p_ooo_info->free_buffers_list); 365 366 list_del(&p_isle->list_entry); 367 p_ooo_info->cur_isles_number--; 368 list_add(&p_isle->list_entry, &p_ooo_info->free_isles_list); 369 } 370 } 371 372 void qed_ooo_add_new_isle(struct qed_hwfn *p_hwfn, 373 struct qed_ooo_info *p_ooo_info, 374 u32 cid, u8 ooo_isle, 375 struct qed_ooo_buffer *p_buffer) 376 { 377 struct qed_ooo_archipelago *p_archipelago = NULL; 378 struct qed_ooo_isle *p_prev_isle = NULL; 379 struct qed_ooo_isle *p_isle = NULL; 380 381 if (ooo_isle > 1) { 382 p_prev_isle = qed_ooo_seek_isle(p_hwfn, 383 p_ooo_info, cid, ooo_isle - 1); 384 if (!p_prev_isle) { 385 DP_NOTICE(p_hwfn, 386 "Isle %d is not found(cid %d)\n", 387 ooo_isle - 1, cid); 388 return; 389 } 390 } 391 p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid); 392 if (!p_archipelago && (ooo_isle != 1)) { 393 DP_NOTICE(p_hwfn, 394 "Connection %d is not found in OOO list\n", cid); 395 return; 396 } 397 398 if (!list_empty(&p_ooo_info->free_isles_list)) { 399 p_isle = list_first_entry(&p_ooo_info->free_isles_list, 400 struct qed_ooo_isle, list_entry); 401 402 list_del(&p_isle->list_entry); 403 if (!list_empty(&p_isle->buffers_list)) { 404 DP_NOTICE(p_hwfn, "Free isle is not empty\n"); 405 INIT_LIST_HEAD(&p_isle->buffers_list); 406 } 407 } else { 408 DP_NOTICE(p_hwfn, "No more free isles\n"); 409 return; 410 } 411 412 if (!p_archipelago) { 413 u32 idx = (cid & 0xffff) - p_ooo_info->cid_base; 414 415 p_archipelago = &p_ooo_info->p_archipelagos_mem[idx]; 416 } 417 418 list_add(&p_buffer->list_entry, &p_isle->buffers_list); 419 p_ooo_info->cur_isles_number++; 420 p_ooo_info->gen_isles_number++; 421 422 if (p_ooo_info->cur_isles_number > p_ooo_info->max_isles_number) 423 p_ooo_info->max_isles_number = p_ooo_info->cur_isles_number; 424 425 if (!p_prev_isle) 426 list_add(&p_isle->list_entry, &p_archipelago->isles_list); 427 else 428 list_add(&p_isle->list_entry, &p_prev_isle->list_entry); 429 } 430 431 void qed_ooo_add_new_buffer(struct qed_hwfn *p_hwfn, 432 struct qed_ooo_info *p_ooo_info, 433 u32 cid, 434 u8 ooo_isle, 435 struct qed_ooo_buffer *p_buffer, u8 buffer_side) 436 { 437 struct qed_ooo_isle *p_isle = NULL; 438 439 p_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid, ooo_isle); 440 if (!p_isle) { 441 DP_NOTICE(p_hwfn, 442 "Isle %d is not found(cid %d)\n", ooo_isle, cid); 443 return; 444 } 445 446 if (buffer_side == QED_OOO_LEFT_BUF) 447 list_add(&p_buffer->list_entry, &p_isle->buffers_list); 448 else 449 list_add_tail(&p_buffer->list_entry, &p_isle->buffers_list); 450 } 451 452 void qed_ooo_join_isles(struct qed_hwfn *p_hwfn, 453 struct qed_ooo_info *p_ooo_info, u32 cid, u8 left_isle) 454 { 455 struct qed_ooo_archipelago *p_archipelago = NULL; 456 struct qed_ooo_isle *p_right_isle = NULL; 457 struct qed_ooo_isle *p_left_isle = NULL; 458 459 p_right_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid, 460 left_isle + 1); 461 if (!p_right_isle) { 462 DP_NOTICE(p_hwfn, 463 "Right isle %d is not found(cid %d)\n", 464 left_isle + 1, cid); 465 return; 466 } 467 468 p_archipelago = qed_ooo_seek_archipelago(p_hwfn, p_ooo_info, cid); 469 list_del(&p_right_isle->list_entry); 470 p_ooo_info->cur_isles_number--; 471 if (left_isle) { 472 p_left_isle = qed_ooo_seek_isle(p_hwfn, p_ooo_info, cid, 473 left_isle); 474 if (!p_left_isle) { 475 DP_NOTICE(p_hwfn, 476 "Left isle %d is not found(cid %d)\n", 477 left_isle, cid); 478 return; 479 } 480 list_splice_tail_init(&p_right_isle->buffers_list, 481 &p_left_isle->buffers_list); 482 } else { 483 list_splice_tail_init(&p_right_isle->buffers_list, 484 &p_ooo_info->ready_buffers_list); 485 } 486 list_add_tail(&p_right_isle->list_entry, &p_ooo_info->free_isles_list); 487 } 488