Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / net / ethernet / qlogic / qlcnic / qlcnic_ctx.c
blobe0c85a5e91d078f63e23bd6ef684877f8d54b252
1 /*
2 * QLogic qlcnic NIC Driver
3 * Copyright (c) 2009-2010 QLogic Corporation
5 * See LICENSE.qlcnic for copyright and licensing details.
6 */
8 #include "qlcnic.h"
10 static u32
11 qlcnic_poll_rsp(struct qlcnic_adapter *adapter)
13 u32 rsp;
14 int timeout = 0;
16 do {
17 /* give atleast 1ms for firmware to respond */
18 msleep(1);
20 if (++timeout > QLCNIC_OS_CRB_RETRY_COUNT)
21 return QLCNIC_CDRP_RSP_TIMEOUT;
23 rsp = QLCRD32(adapter, QLCNIC_CDRP_CRB_OFFSET);
24 } while (!QLCNIC_CDRP_IS_RSP(rsp));
26 return rsp;
29 u32
30 qlcnic_issue_cmd(struct qlcnic_adapter *adapter,
31 u32 pci_fn, u32 version, u32 arg1, u32 arg2, u32 arg3, u32 cmd,
32 u32 *rd_args[3])
34 u32 rsp;
35 u32 signature;
36 u32 rcode = QLCNIC_RCODE_SUCCESS;
37 struct pci_dev *pdev = adapter->pdev;
39 signature = QLCNIC_CDRP_SIGNATURE_MAKE(pci_fn, version);
41 /* Acquire semaphore before accessing CRB */
42 if (qlcnic_api_lock(adapter))
43 return QLCNIC_RCODE_TIMEOUT;
45 QLCWR32(adapter, QLCNIC_SIGN_CRB_OFFSET, signature);
46 QLCWR32(adapter, QLCNIC_ARG1_CRB_OFFSET, arg1);
47 QLCWR32(adapter, QLCNIC_ARG2_CRB_OFFSET, arg2);
48 QLCWR32(adapter, QLCNIC_ARG3_CRB_OFFSET, arg3);
49 QLCWR32(adapter, QLCNIC_CDRP_CRB_OFFSET, QLCNIC_CDRP_FORM_CMD(cmd));
51 rsp = qlcnic_poll_rsp(adapter);
53 if (rsp == QLCNIC_CDRP_RSP_TIMEOUT) {
54 dev_err(&pdev->dev, "card response timeout.\n");
55 rcode = QLCNIC_RCODE_TIMEOUT;
56 } else if (rsp == QLCNIC_CDRP_RSP_FAIL) {
57 rcode = QLCRD32(adapter, QLCNIC_ARG1_CRB_OFFSET);
58 dev_err(&pdev->dev, "failed card response code:0x%x\n",
59 rcode);
60 } else if (rsp == QLCNIC_CDRP_RSP_OK) {
61 if (rd_args[1])
62 *rd_args[1] = QLCRD32(adapter, QLCNIC_ARG2_CRB_OFFSET);
63 if (rd_args[2])
64 *rd_args[2] = QLCRD32(adapter, QLCNIC_ARG3_CRB_OFFSET);
66 if (rd_args[0])
67 *rd_args[0] = QLCRD32(adapter, QLCNIC_ARG1_CRB_OFFSET);
69 /* Release semaphore */
70 qlcnic_api_unlock(adapter);
72 return rcode;
75 static uint32_t qlcnic_temp_checksum(uint32_t *temp_buffer, u16 temp_size)
77 uint64_t sum = 0;
78 int count = temp_size / sizeof(uint32_t);
79 while (count-- > 0)
80 sum += *temp_buffer++;
81 while (sum >> 32)
82 sum = (sum & 0xFFFFFFFF) + (sum >> 32);
83 return ~sum;
86 int qlcnic_fw_cmd_get_minidump_temp(struct qlcnic_adapter *adapter)
88 int err, i;
89 u16 temp_size;
90 void *tmp_addr;
91 u32 version, csum, *template, *tmp_buf, rsp;
92 u32 *rd_args[3];
93 struct qlcnic_hardware_context *ahw;
94 struct qlcnic_dump_template_hdr *tmpl_hdr, *tmp_tmpl;
95 dma_addr_t tmp_addr_t = 0;
97 ahw = adapter->ahw;
98 rd_args[0] = &rsp;
99 rd_args[1] = (u32 *) &temp_size;
100 rd_args[2] = &version;
101 err = qlcnic_issue_cmd(adapter,
102 adapter->ahw->pci_func,
103 adapter->fw_hal_version,
107 QLCNIC_CDRP_CMD_TEMP_SIZE,
108 rd_args);
109 if (err != QLCNIC_RCODE_SUCCESS) {
110 dev_info(&adapter->pdev->dev,
111 "Can't get template size %d\n", rsp);
112 err = -EIO;
113 return err;
115 if (!temp_size)
116 return -EIO;
118 tmp_addr = dma_alloc_coherent(&adapter->pdev->dev, temp_size,
119 &tmp_addr_t, GFP_KERNEL);
120 if (!tmp_addr) {
121 dev_err(&adapter->pdev->dev,
122 "Can't get memory for FW dump template\n");
123 return -ENOMEM;
125 memset(rd_args, 0, sizeof(rd_args));
126 err = qlcnic_issue_cmd(adapter,
127 adapter->ahw->pci_func,
128 adapter->fw_hal_version,
129 LSD(tmp_addr_t),
130 MSD(tmp_addr_t),
131 temp_size,
132 QLCNIC_CDRP_CMD_GET_TEMP_HDR,
133 rd_args);
135 if (err != QLCNIC_RCODE_SUCCESS) {
136 dev_err(&adapter->pdev->dev,
137 "Failed to get mini dump template header %d\n", err);
138 err = -EIO;
139 goto error;
141 tmp_tmpl = tmp_addr;
142 csum = qlcnic_temp_checksum((uint32_t *) tmp_addr, temp_size);
143 if (csum) {
144 dev_err(&adapter->pdev->dev,
145 "Template header checksum validation failed\n");
146 err = -EIO;
147 goto error;
149 ahw->fw_dump.tmpl_hdr = vzalloc(temp_size);
150 if (!ahw->fw_dump.tmpl_hdr) {
151 err = -EIO;
152 goto error;
154 tmp_buf = tmp_addr;
155 template = (u32 *) ahw->fw_dump.tmpl_hdr;
156 for (i = 0; i < temp_size/sizeof(u32); i++)
157 *template++ = __le32_to_cpu(*tmp_buf++);
159 tmpl_hdr = ahw->fw_dump.tmpl_hdr;
160 tmpl_hdr->drv_cap_mask = QLCNIC_DUMP_MASK_DEF;
161 ahw->fw_dump.enable = 1;
162 error:
163 dma_free_coherent(&adapter->pdev->dev, temp_size, tmp_addr, tmp_addr_t);
164 return err;
168 qlcnic_fw_cmd_set_mtu(struct qlcnic_adapter *adapter, int mtu)
170 u32 *rd_args[3];
171 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
173 memset(rd_args, 0, sizeof(rd_args));
174 if (recv_ctx->state == QLCNIC_HOST_CTX_STATE_ACTIVE) {
175 if (qlcnic_issue_cmd(adapter,
176 adapter->ahw->pci_func,
177 adapter->fw_hal_version,
178 recv_ctx->context_id,
179 mtu,
181 QLCNIC_CDRP_CMD_SET_MTU,
182 rd_args)) {
184 dev_err(&adapter->pdev->dev, "Failed to set mtu\n");
185 return -EIO;
189 return 0;
192 static int
193 qlcnic_fw_cmd_create_rx_ctx(struct qlcnic_adapter *adapter)
195 void *addr;
196 struct qlcnic_hostrq_rx_ctx *prq;
197 struct qlcnic_cardrsp_rx_ctx *prsp;
198 struct qlcnic_hostrq_rds_ring *prq_rds;
199 struct qlcnic_hostrq_sds_ring *prq_sds;
200 struct qlcnic_cardrsp_rds_ring *prsp_rds;
201 struct qlcnic_cardrsp_sds_ring *prsp_sds;
202 struct qlcnic_host_rds_ring *rds_ring;
203 struct qlcnic_host_sds_ring *sds_ring;
205 dma_addr_t hostrq_phys_addr, cardrsp_phys_addr;
206 u64 phys_addr;
208 u8 i, nrds_rings, nsds_rings;
209 size_t rq_size, rsp_size;
210 u32 cap, reg, val, reg2;
211 u32 *rd_args[3];
212 int err;
214 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
216 nrds_rings = adapter->max_rds_rings;
217 nsds_rings = adapter->max_sds_rings;
219 rq_size =
220 SIZEOF_HOSTRQ_RX(struct qlcnic_hostrq_rx_ctx, nrds_rings,
221 nsds_rings);
222 rsp_size =
223 SIZEOF_CARDRSP_RX(struct qlcnic_cardrsp_rx_ctx, nrds_rings,
224 nsds_rings);
226 addr = dma_alloc_coherent(&adapter->pdev->dev, rq_size,
227 &hostrq_phys_addr, GFP_KERNEL);
228 if (addr == NULL)
229 return -ENOMEM;
230 prq = addr;
232 addr = dma_alloc_coherent(&adapter->pdev->dev, rsp_size,
233 &cardrsp_phys_addr, GFP_KERNEL);
234 if (addr == NULL) {
235 err = -ENOMEM;
236 goto out_free_rq;
238 prsp = addr;
240 prq->host_rsp_dma_addr = cpu_to_le64(cardrsp_phys_addr);
242 cap = (QLCNIC_CAP0_LEGACY_CONTEXT | QLCNIC_CAP0_LEGACY_MN
243 | QLCNIC_CAP0_VALIDOFF);
244 cap |= (QLCNIC_CAP0_JUMBO_CONTIGUOUS | QLCNIC_CAP0_LRO_CONTIGUOUS);
246 prq->valid_field_offset = offsetof(struct qlcnic_hostrq_rx_ctx,
247 msix_handler);
248 prq->txrx_sds_binding = nsds_rings - 1;
250 prq->capabilities[0] = cpu_to_le32(cap);
251 prq->host_int_crb_mode =
252 cpu_to_le32(QLCNIC_HOST_INT_CRB_MODE_SHARED);
253 prq->host_rds_crb_mode =
254 cpu_to_le32(QLCNIC_HOST_RDS_CRB_MODE_UNIQUE);
256 prq->num_rds_rings = cpu_to_le16(nrds_rings);
257 prq->num_sds_rings = cpu_to_le16(nsds_rings);
258 prq->rds_ring_offset = 0;
260 val = le32_to_cpu(prq->rds_ring_offset) +
261 (sizeof(struct qlcnic_hostrq_rds_ring) * nrds_rings);
262 prq->sds_ring_offset = cpu_to_le32(val);
264 prq_rds = (struct qlcnic_hostrq_rds_ring *)(prq->data +
265 le32_to_cpu(prq->rds_ring_offset));
267 for (i = 0; i < nrds_rings; i++) {
269 rds_ring = &recv_ctx->rds_rings[i];
270 rds_ring->producer = 0;
272 prq_rds[i].host_phys_addr = cpu_to_le64(rds_ring->phys_addr);
273 prq_rds[i].ring_size = cpu_to_le32(rds_ring->num_desc);
274 prq_rds[i].ring_kind = cpu_to_le32(i);
275 prq_rds[i].buff_size = cpu_to_le64(rds_ring->dma_size);
278 prq_sds = (struct qlcnic_hostrq_sds_ring *)(prq->data +
279 le32_to_cpu(prq->sds_ring_offset));
281 for (i = 0; i < nsds_rings; i++) {
283 sds_ring = &recv_ctx->sds_rings[i];
284 sds_ring->consumer = 0;
285 memset(sds_ring->desc_head, 0, STATUS_DESC_RINGSIZE(sds_ring));
287 prq_sds[i].host_phys_addr = cpu_to_le64(sds_ring->phys_addr);
288 prq_sds[i].ring_size = cpu_to_le32(sds_ring->num_desc);
289 prq_sds[i].msi_index = cpu_to_le16(i);
292 phys_addr = hostrq_phys_addr;
293 memset(rd_args, 0, sizeof(rd_args));
294 err = qlcnic_issue_cmd(adapter,
295 adapter->ahw->pci_func,
296 adapter->fw_hal_version,
297 (u32)(phys_addr >> 32),
298 (u32)(phys_addr & 0xffffffff),
299 rq_size,
300 QLCNIC_CDRP_CMD_CREATE_RX_CTX,
301 rd_args);
302 if (err) {
303 dev_err(&adapter->pdev->dev,
304 "Failed to create rx ctx in firmware%d\n", err);
305 goto out_free_rsp;
309 prsp_rds = ((struct qlcnic_cardrsp_rds_ring *)
310 &prsp->data[le32_to_cpu(prsp->rds_ring_offset)]);
312 for (i = 0; i < le16_to_cpu(prsp->num_rds_rings); i++) {
313 rds_ring = &recv_ctx->rds_rings[i];
315 reg = le32_to_cpu(prsp_rds[i].host_producer_crb);
316 rds_ring->crb_rcv_producer = adapter->ahw->pci_base0 + reg;
319 prsp_sds = ((struct qlcnic_cardrsp_sds_ring *)
320 &prsp->data[le32_to_cpu(prsp->sds_ring_offset)]);
322 for (i = 0; i < le16_to_cpu(prsp->num_sds_rings); i++) {
323 sds_ring = &recv_ctx->sds_rings[i];
325 reg = le32_to_cpu(prsp_sds[i].host_consumer_crb);
326 reg2 = le32_to_cpu(prsp_sds[i].interrupt_crb);
328 sds_ring->crb_sts_consumer = adapter->ahw->pci_base0 + reg;
329 sds_ring->crb_intr_mask = adapter->ahw->pci_base0 + reg2;
332 recv_ctx->state = le32_to_cpu(prsp->host_ctx_state);
333 recv_ctx->context_id = le16_to_cpu(prsp->context_id);
334 recv_ctx->virt_port = prsp->virt_port;
336 out_free_rsp:
337 dma_free_coherent(&adapter->pdev->dev, rsp_size, prsp,
338 cardrsp_phys_addr);
339 out_free_rq:
340 dma_free_coherent(&adapter->pdev->dev, rq_size, prq, hostrq_phys_addr);
341 return err;
344 static void
345 qlcnic_fw_cmd_destroy_rx_ctx(struct qlcnic_adapter *adapter)
347 u32 *rd_args[3];
348 struct qlcnic_recv_context *recv_ctx = adapter->recv_ctx;
350 memset(rd_args, 0, sizeof(rd_args));
351 if (qlcnic_issue_cmd(adapter,
352 adapter->ahw->pci_func,
353 adapter->fw_hal_version,
354 recv_ctx->context_id,
355 QLCNIC_DESTROY_CTX_RESET,
357 QLCNIC_CDRP_CMD_DESTROY_RX_CTX,
358 rd_args)) {
360 dev_err(&adapter->pdev->dev,
361 "Failed to destroy rx ctx in firmware\n");
364 recv_ctx->state = QLCNIC_HOST_CTX_STATE_FREED;
367 static int
368 qlcnic_fw_cmd_create_tx_ctx(struct qlcnic_adapter *adapter)
370 struct qlcnic_hostrq_tx_ctx *prq;
371 struct qlcnic_hostrq_cds_ring *prq_cds;
372 struct qlcnic_cardrsp_tx_ctx *prsp;
373 void *rq_addr, *rsp_addr;
374 size_t rq_size, rsp_size;
375 u32 temp;
376 u32 *rd_args[3];
377 int err;
378 u64 phys_addr;
379 dma_addr_t rq_phys_addr, rsp_phys_addr;
380 struct qlcnic_host_tx_ring *tx_ring = adapter->tx_ring;
382 /* reset host resources */
383 tx_ring->producer = 0;
384 tx_ring->sw_consumer = 0;
385 *(tx_ring->hw_consumer) = 0;
387 rq_size = SIZEOF_HOSTRQ_TX(struct qlcnic_hostrq_tx_ctx);
388 rq_addr = dma_alloc_coherent(&adapter->pdev->dev, rq_size,
389 &rq_phys_addr, GFP_KERNEL);
390 if (!rq_addr)
391 return -ENOMEM;
393 rsp_size = SIZEOF_CARDRSP_TX(struct qlcnic_cardrsp_tx_ctx);
394 rsp_addr = dma_alloc_coherent(&adapter->pdev->dev, rsp_size,
395 &rsp_phys_addr, GFP_KERNEL);
396 if (!rsp_addr) {
397 err = -ENOMEM;
398 goto out_free_rq;
401 memset(rq_addr, 0, rq_size);
402 prq = rq_addr;
404 memset(rsp_addr, 0, rsp_size);
405 prsp = rsp_addr;
407 prq->host_rsp_dma_addr = cpu_to_le64(rsp_phys_addr);
409 temp = (QLCNIC_CAP0_LEGACY_CONTEXT | QLCNIC_CAP0_LEGACY_MN |
410 QLCNIC_CAP0_LSO);
411 prq->capabilities[0] = cpu_to_le32(temp);
413 prq->host_int_crb_mode =
414 cpu_to_le32(QLCNIC_HOST_INT_CRB_MODE_SHARED);
416 prq->interrupt_ctl = 0;
417 prq->msi_index = 0;
418 prq->cmd_cons_dma_addr = cpu_to_le64(tx_ring->hw_cons_phys_addr);
420 prq_cds = &prq->cds_ring;
422 prq_cds->host_phys_addr = cpu_to_le64(tx_ring->phys_addr);
423 prq_cds->ring_size = cpu_to_le32(tx_ring->num_desc);
425 phys_addr = rq_phys_addr;
426 memset(rd_args, 0, sizeof(rd_args));
427 err = qlcnic_issue_cmd(adapter,
428 adapter->ahw->pci_func,
429 adapter->fw_hal_version,
430 (u32)(phys_addr >> 32),
431 ((u32)phys_addr & 0xffffffff),
432 rq_size,
433 QLCNIC_CDRP_CMD_CREATE_TX_CTX,
434 rd_args);
436 if (err == QLCNIC_RCODE_SUCCESS) {
437 temp = le32_to_cpu(prsp->cds_ring.host_producer_crb);
438 tx_ring->crb_cmd_producer = adapter->ahw->pci_base0 + temp;
440 adapter->tx_context_id =
441 le16_to_cpu(prsp->context_id);
442 } else {
443 dev_err(&adapter->pdev->dev,
444 "Failed to create tx ctx in firmware%d\n", err);
445 err = -EIO;
448 dma_free_coherent(&adapter->pdev->dev, rsp_size, rsp_addr,
449 rsp_phys_addr);
451 out_free_rq:
452 dma_free_coherent(&adapter->pdev->dev, rq_size, rq_addr, rq_phys_addr);
454 return err;
457 static void
458 qlcnic_fw_cmd_destroy_tx_ctx(struct qlcnic_adapter *adapter)
460 u32 *rd_args[3];
462 memset(rd_args, 0, sizeof(rd_args));
463 if (qlcnic_issue_cmd(adapter,
464 adapter->ahw->pci_func,
465 adapter->fw_hal_version,
466 adapter->tx_context_id,
467 QLCNIC_DESTROY_CTX_RESET,
469 QLCNIC_CDRP_CMD_DESTROY_TX_CTX,
470 rd_args)) {
472 dev_err(&adapter->pdev->dev,
473 "Failed to destroy tx ctx in firmware\n");
478 qlcnic_fw_cmd_set_port(struct qlcnic_adapter *adapter, u32 config)
480 u32 *rd_args[3];
482 memset(rd_args, 0, sizeof(rd_args));
483 return qlcnic_issue_cmd(adapter,
484 adapter->ahw->pci_func,
485 adapter->fw_hal_version,
486 config,
489 QLCNIC_CDRP_CMD_CONFIG_PORT,
490 rd_args);
493 int qlcnic_alloc_hw_resources(struct qlcnic_adapter *adapter)
495 void *addr;
496 int err;
497 int ring;
498 struct qlcnic_recv_context *recv_ctx;
499 struct qlcnic_host_rds_ring *rds_ring;
500 struct qlcnic_host_sds_ring *sds_ring;
501 struct qlcnic_host_tx_ring *tx_ring;
503 struct pci_dev *pdev = adapter->pdev;
505 recv_ctx = adapter->recv_ctx;
506 tx_ring = adapter->tx_ring;
508 tx_ring->hw_consumer = (__le32 *) dma_alloc_coherent(&pdev->dev,
509 sizeof(u32), &tx_ring->hw_cons_phys_addr, GFP_KERNEL);
510 if (tx_ring->hw_consumer == NULL) {
511 dev_err(&pdev->dev, "failed to allocate tx consumer\n");
512 return -ENOMEM;
515 /* cmd desc ring */
516 addr = dma_alloc_coherent(&pdev->dev, TX_DESC_RINGSIZE(tx_ring),
517 &tx_ring->phys_addr, GFP_KERNEL);
519 if (addr == NULL) {
520 dev_err(&pdev->dev, "failed to allocate tx desc ring\n");
521 err = -ENOMEM;
522 goto err_out_free;
525 tx_ring->desc_head = addr;
527 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
528 rds_ring = &recv_ctx->rds_rings[ring];
529 addr = dma_alloc_coherent(&adapter->pdev->dev,
530 RCV_DESC_RINGSIZE(rds_ring),
531 &rds_ring->phys_addr, GFP_KERNEL);
532 if (addr == NULL) {
533 dev_err(&pdev->dev,
534 "failed to allocate rds ring [%d]\n", ring);
535 err = -ENOMEM;
536 goto err_out_free;
538 rds_ring->desc_head = addr;
542 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
543 sds_ring = &recv_ctx->sds_rings[ring];
545 addr = dma_alloc_coherent(&adapter->pdev->dev,
546 STATUS_DESC_RINGSIZE(sds_ring),
547 &sds_ring->phys_addr, GFP_KERNEL);
548 if (addr == NULL) {
549 dev_err(&pdev->dev,
550 "failed to allocate sds ring [%d]\n", ring);
551 err = -ENOMEM;
552 goto err_out_free;
554 sds_ring->desc_head = addr;
557 return 0;
559 err_out_free:
560 qlcnic_free_hw_resources(adapter);
561 return err;
565 int qlcnic_fw_create_ctx(struct qlcnic_adapter *adapter)
567 int err;
569 if (adapter->flags & QLCNIC_NEED_FLR) {
570 pci_reset_function(adapter->pdev);
571 adapter->flags &= ~QLCNIC_NEED_FLR;
574 err = qlcnic_fw_cmd_create_rx_ctx(adapter);
575 if (err)
576 return err;
578 err = qlcnic_fw_cmd_create_tx_ctx(adapter);
579 if (err) {
580 qlcnic_fw_cmd_destroy_rx_ctx(adapter);
581 return err;
584 set_bit(__QLCNIC_FW_ATTACHED, &adapter->state);
585 return 0;
588 void qlcnic_fw_destroy_ctx(struct qlcnic_adapter *adapter)
590 if (test_and_clear_bit(__QLCNIC_FW_ATTACHED, &adapter->state)) {
591 qlcnic_fw_cmd_destroy_rx_ctx(adapter);
592 qlcnic_fw_cmd_destroy_tx_ctx(adapter);
594 /* Allow dma queues to drain after context reset */
595 msleep(20);
599 void qlcnic_free_hw_resources(struct qlcnic_adapter *adapter)
601 struct qlcnic_recv_context *recv_ctx;
602 struct qlcnic_host_rds_ring *rds_ring;
603 struct qlcnic_host_sds_ring *sds_ring;
604 struct qlcnic_host_tx_ring *tx_ring;
605 int ring;
607 recv_ctx = adapter->recv_ctx;
609 tx_ring = adapter->tx_ring;
610 if (tx_ring->hw_consumer != NULL) {
611 dma_free_coherent(&adapter->pdev->dev,
612 sizeof(u32),
613 tx_ring->hw_consumer,
614 tx_ring->hw_cons_phys_addr);
615 tx_ring->hw_consumer = NULL;
618 if (tx_ring->desc_head != NULL) {
619 dma_free_coherent(&adapter->pdev->dev,
620 TX_DESC_RINGSIZE(tx_ring),
621 tx_ring->desc_head, tx_ring->phys_addr);
622 tx_ring->desc_head = NULL;
625 for (ring = 0; ring < adapter->max_rds_rings; ring++) {
626 rds_ring = &recv_ctx->rds_rings[ring];
628 if (rds_ring->desc_head != NULL) {
629 dma_free_coherent(&adapter->pdev->dev,
630 RCV_DESC_RINGSIZE(rds_ring),
631 rds_ring->desc_head,
632 rds_ring->phys_addr);
633 rds_ring->desc_head = NULL;
637 for (ring = 0; ring < adapter->max_sds_rings; ring++) {
638 sds_ring = &recv_ctx->sds_rings[ring];
640 if (sds_ring->desc_head != NULL) {
641 dma_free_coherent(&adapter->pdev->dev,
642 STATUS_DESC_RINGSIZE(sds_ring),
643 sds_ring->desc_head,
644 sds_ring->phys_addr);
645 sds_ring->desc_head = NULL;
651 /* Get MAC address of a NIC partition */
652 int qlcnic_get_mac_address(struct qlcnic_adapter *adapter, u8 *mac)
654 int err;
655 u32 arg1, rd_arg1, rd_arg2;
656 u32 *rd_args[3];
658 arg1 = adapter->ahw->pci_func | BIT_8;
659 rd_args[0] = &rd_arg1;
660 rd_args[1] = &rd_arg2;
661 rd_args[2] = 0;
662 err = qlcnic_issue_cmd(adapter,
663 adapter->ahw->pci_func,
664 adapter->fw_hal_version,
665 arg1,
668 QLCNIC_CDRP_CMD_MAC_ADDRESS,
669 rd_args);
671 if (err == QLCNIC_RCODE_SUCCESS)
672 qlcnic_fetch_mac(adapter, rd_arg1, rd_arg2, 0, mac);
673 else {
674 dev_err(&adapter->pdev->dev,
675 "Failed to get mac address%d\n", err);
676 err = -EIO;
679 return err;
682 /* Get info of a NIC partition */
683 int qlcnic_get_nic_info(struct qlcnic_adapter *adapter,
684 struct qlcnic_info *npar_info, u8 func_id)
686 int err;
687 dma_addr_t nic_dma_t;
688 struct qlcnic_info *nic_info;
689 void *nic_info_addr;
690 u32 *rd_args[3];
691 size_t nic_size = sizeof(struct qlcnic_info);
693 nic_info_addr = dma_alloc_coherent(&adapter->pdev->dev, nic_size,
694 &nic_dma_t, GFP_KERNEL);
695 if (!nic_info_addr)
696 return -ENOMEM;
697 memset(nic_info_addr, 0, nic_size);
699 nic_info = nic_info_addr;
700 memset(rd_args, 0, sizeof(rd_args));
701 err = qlcnic_issue_cmd(adapter,
702 adapter->ahw->pci_func,
703 adapter->fw_hal_version,
704 MSD(nic_dma_t),
705 LSD(nic_dma_t),
706 (func_id << 16 | nic_size),
707 QLCNIC_CDRP_CMD_GET_NIC_INFO,
708 rd_args);
710 if (err == QLCNIC_RCODE_SUCCESS) {
711 npar_info->pci_func = le16_to_cpu(nic_info->pci_func);
712 npar_info->op_mode = le16_to_cpu(nic_info->op_mode);
713 npar_info->phys_port = le16_to_cpu(nic_info->phys_port);
714 npar_info->switch_mode = le16_to_cpu(nic_info->switch_mode);
715 npar_info->max_tx_ques = le16_to_cpu(nic_info->max_tx_ques);
716 npar_info->max_rx_ques = le16_to_cpu(nic_info->max_rx_ques);
717 npar_info->min_tx_bw = le16_to_cpu(nic_info->min_tx_bw);
718 npar_info->max_tx_bw = le16_to_cpu(nic_info->max_tx_bw);
719 npar_info->capabilities = le32_to_cpu(nic_info->capabilities);
720 npar_info->max_mtu = le16_to_cpu(nic_info->max_mtu);
722 dev_info(&adapter->pdev->dev,
723 "phy port: %d switch_mode: %d,\n"
724 "\tmax_tx_q: %d max_rx_q: %d min_tx_bw: 0x%x,\n"
725 "\tmax_tx_bw: 0x%x max_mtu:0x%x, capabilities: 0x%x\n",
726 npar_info->phys_port, npar_info->switch_mode,
727 npar_info->max_tx_ques, npar_info->max_rx_ques,
728 npar_info->min_tx_bw, npar_info->max_tx_bw,
729 npar_info->max_mtu, npar_info->capabilities);
730 } else {
731 dev_err(&adapter->pdev->dev,
732 "Failed to get nic info%d\n", err);
733 err = -EIO;
736 dma_free_coherent(&adapter->pdev->dev, nic_size, nic_info_addr,
737 nic_dma_t);
738 return err;
741 /* Configure a NIC partition */
742 int qlcnic_set_nic_info(struct qlcnic_adapter *adapter, struct qlcnic_info *nic)
744 int err = -EIO;
745 dma_addr_t nic_dma_t;
746 void *nic_info_addr;
747 u32 *rd_args[3];
748 struct qlcnic_info *nic_info;
749 size_t nic_size = sizeof(struct qlcnic_info);
751 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
752 return err;
754 nic_info_addr = dma_alloc_coherent(&adapter->pdev->dev, nic_size,
755 &nic_dma_t, GFP_KERNEL);
756 if (!nic_info_addr)
757 return -ENOMEM;
759 memset(nic_info_addr, 0, nic_size);
760 nic_info = nic_info_addr;
762 nic_info->pci_func = cpu_to_le16(nic->pci_func);
763 nic_info->op_mode = cpu_to_le16(nic->op_mode);
764 nic_info->phys_port = cpu_to_le16(nic->phys_port);
765 nic_info->switch_mode = cpu_to_le16(nic->switch_mode);
766 nic_info->capabilities = cpu_to_le32(nic->capabilities);
767 nic_info->max_mac_filters = nic->max_mac_filters;
768 nic_info->max_tx_ques = cpu_to_le16(nic->max_tx_ques);
769 nic_info->max_rx_ques = cpu_to_le16(nic->max_rx_ques);
770 nic_info->min_tx_bw = cpu_to_le16(nic->min_tx_bw);
771 nic_info->max_tx_bw = cpu_to_le16(nic->max_tx_bw);
773 memset(rd_args, 0, sizeof(rd_args));
774 err = qlcnic_issue_cmd(adapter,
775 adapter->ahw->pci_func,
776 adapter->fw_hal_version,
777 MSD(nic_dma_t),
778 LSD(nic_dma_t),
779 ((nic->pci_func << 16) | nic_size),
780 QLCNIC_CDRP_CMD_SET_NIC_INFO,
781 rd_args);
783 if (err != QLCNIC_RCODE_SUCCESS) {
784 dev_err(&adapter->pdev->dev,
785 "Failed to set nic info%d\n", err);
786 err = -EIO;
789 dma_free_coherent(&adapter->pdev->dev, nic_size, nic_info_addr,
790 nic_dma_t);
791 return err;
794 /* Get PCI Info of a partition */
795 int qlcnic_get_pci_info(struct qlcnic_adapter *adapter,
796 struct qlcnic_pci_info *pci_info)
798 int err = 0, i;
799 u32 *rd_args[3];
800 dma_addr_t pci_info_dma_t;
801 struct qlcnic_pci_info *npar;
802 void *pci_info_addr;
803 size_t npar_size = sizeof(struct qlcnic_pci_info);
804 size_t pci_size = npar_size * QLCNIC_MAX_PCI_FUNC;
806 pci_info_addr = dma_alloc_coherent(&adapter->pdev->dev, pci_size,
807 &pci_info_dma_t, GFP_KERNEL);
808 if (!pci_info_addr)
809 return -ENOMEM;
810 memset(pci_info_addr, 0, pci_size);
812 npar = pci_info_addr;
813 memset(rd_args, 0, sizeof(rd_args));
814 err = qlcnic_issue_cmd(adapter,
815 adapter->ahw->pci_func,
816 adapter->fw_hal_version,
817 MSD(pci_info_dma_t),
818 LSD(pci_info_dma_t),
819 pci_size,
820 QLCNIC_CDRP_CMD_GET_PCI_INFO,
821 rd_args);
823 if (err == QLCNIC_RCODE_SUCCESS) {
824 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++, npar++, pci_info++) {
825 pci_info->id = le16_to_cpu(npar->id);
826 pci_info->active = le16_to_cpu(npar->active);
827 pci_info->type = le16_to_cpu(npar->type);
828 pci_info->default_port =
829 le16_to_cpu(npar->default_port);
830 pci_info->tx_min_bw =
831 le16_to_cpu(npar->tx_min_bw);
832 pci_info->tx_max_bw =
833 le16_to_cpu(npar->tx_max_bw);
834 memcpy(pci_info->mac, npar->mac, ETH_ALEN);
836 } else {
837 dev_err(&adapter->pdev->dev,
838 "Failed to get PCI Info%d\n", err);
839 err = -EIO;
842 dma_free_coherent(&adapter->pdev->dev, pci_size, pci_info_addr,
843 pci_info_dma_t);
844 return err;
847 /* Configure eSwitch for port mirroring */
848 int qlcnic_config_port_mirroring(struct qlcnic_adapter *adapter, u8 id,
849 u8 enable_mirroring, u8 pci_func)
851 int err = -EIO;
852 u32 arg1;
853 u32 *rd_args[3];
855 if (adapter->op_mode != QLCNIC_MGMT_FUNC ||
856 !(adapter->eswitch[id].flags & QLCNIC_SWITCH_ENABLE))
857 return err;
859 arg1 = id | (enable_mirroring ? BIT_4 : 0);
860 arg1 |= pci_func << 8;
862 memset(rd_args, 0, sizeof(rd_args));
863 err = qlcnic_issue_cmd(adapter,
864 adapter->ahw->pci_func,
865 adapter->fw_hal_version,
866 arg1,
869 QLCNIC_CDRP_CMD_SET_PORTMIRRORING,
870 rd_args);
872 if (err != QLCNIC_RCODE_SUCCESS) {
873 dev_err(&adapter->pdev->dev,
874 "Failed to configure port mirroring%d on eswitch:%d\n",
875 pci_func, id);
876 } else {
877 dev_info(&adapter->pdev->dev,
878 "Configured eSwitch %d for port mirroring:%d\n",
879 id, pci_func);
882 return err;
885 int qlcnic_get_port_stats(struct qlcnic_adapter *adapter, const u8 func,
886 const u8 rx_tx, struct __qlcnic_esw_statistics *esw_stats) {
888 size_t stats_size = sizeof(struct __qlcnic_esw_statistics);
889 struct __qlcnic_esw_statistics *stats;
890 dma_addr_t stats_dma_t;
891 void *stats_addr;
892 u32 arg1;
893 u32 *rd_args[3];
894 int err;
896 if (esw_stats == NULL)
897 return -ENOMEM;
899 if (adapter->op_mode != QLCNIC_MGMT_FUNC &&
900 func != adapter->ahw->pci_func) {
901 dev_err(&adapter->pdev->dev,
902 "Not privilege to query stats for func=%d", func);
903 return -EIO;
906 stats_addr = dma_alloc_coherent(&adapter->pdev->dev, stats_size,
907 &stats_dma_t, GFP_KERNEL);
908 if (!stats_addr) {
909 dev_err(&adapter->pdev->dev, "Unable to allocate memory\n");
910 return -ENOMEM;
912 memset(stats_addr, 0, stats_size);
914 arg1 = func | QLCNIC_STATS_VERSION << 8 | QLCNIC_STATS_PORT << 12;
915 arg1 |= rx_tx << 15 | stats_size << 16;
917 memset(rd_args, 0, sizeof(rd_args));
918 err = qlcnic_issue_cmd(adapter,
919 adapter->ahw->pci_func,
920 adapter->fw_hal_version,
921 arg1,
922 MSD(stats_dma_t),
923 LSD(stats_dma_t),
924 QLCNIC_CDRP_CMD_GET_ESWITCH_STATS,
925 rd_args);
927 if (!err) {
928 stats = stats_addr;
929 esw_stats->context_id = le16_to_cpu(stats->context_id);
930 esw_stats->version = le16_to_cpu(stats->version);
931 esw_stats->size = le16_to_cpu(stats->size);
932 esw_stats->multicast_frames =
933 le64_to_cpu(stats->multicast_frames);
934 esw_stats->broadcast_frames =
935 le64_to_cpu(stats->broadcast_frames);
936 esw_stats->unicast_frames = le64_to_cpu(stats->unicast_frames);
937 esw_stats->dropped_frames = le64_to_cpu(stats->dropped_frames);
938 esw_stats->local_frames = le64_to_cpu(stats->local_frames);
939 esw_stats->errors = le64_to_cpu(stats->errors);
940 esw_stats->numbytes = le64_to_cpu(stats->numbytes);
943 dma_free_coherent(&adapter->pdev->dev, stats_size, stats_addr,
944 stats_dma_t);
945 return err;
948 int qlcnic_get_eswitch_stats(struct qlcnic_adapter *adapter, const u8 eswitch,
949 const u8 rx_tx, struct __qlcnic_esw_statistics *esw_stats) {
951 struct __qlcnic_esw_statistics port_stats;
952 u8 i;
953 int ret = -EIO;
955 if (esw_stats == NULL)
956 return -ENOMEM;
957 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
958 return -EIO;
959 if (adapter->npars == NULL)
960 return -EIO;
962 memset(esw_stats, 0, sizeof(u64));
963 esw_stats->unicast_frames = QLCNIC_ESW_STATS_NOT_AVAIL;
964 esw_stats->multicast_frames = QLCNIC_ESW_STATS_NOT_AVAIL;
965 esw_stats->broadcast_frames = QLCNIC_ESW_STATS_NOT_AVAIL;
966 esw_stats->dropped_frames = QLCNIC_ESW_STATS_NOT_AVAIL;
967 esw_stats->errors = QLCNIC_ESW_STATS_NOT_AVAIL;
968 esw_stats->local_frames = QLCNIC_ESW_STATS_NOT_AVAIL;
969 esw_stats->numbytes = QLCNIC_ESW_STATS_NOT_AVAIL;
970 esw_stats->context_id = eswitch;
972 for (i = 0; i < QLCNIC_MAX_PCI_FUNC; i++) {
973 if (adapter->npars[i].phy_port != eswitch)
974 continue;
976 memset(&port_stats, 0, sizeof(struct __qlcnic_esw_statistics));
977 if (qlcnic_get_port_stats(adapter, i, rx_tx, &port_stats))
978 continue;
980 esw_stats->size = port_stats.size;
981 esw_stats->version = port_stats.version;
982 QLCNIC_ADD_ESW_STATS(esw_stats->unicast_frames,
983 port_stats.unicast_frames);
984 QLCNIC_ADD_ESW_STATS(esw_stats->multicast_frames,
985 port_stats.multicast_frames);
986 QLCNIC_ADD_ESW_STATS(esw_stats->broadcast_frames,
987 port_stats.broadcast_frames);
988 QLCNIC_ADD_ESW_STATS(esw_stats->dropped_frames,
989 port_stats.dropped_frames);
990 QLCNIC_ADD_ESW_STATS(esw_stats->errors,
991 port_stats.errors);
992 QLCNIC_ADD_ESW_STATS(esw_stats->local_frames,
993 port_stats.local_frames);
994 QLCNIC_ADD_ESW_STATS(esw_stats->numbytes,
995 port_stats.numbytes);
996 ret = 0;
998 return ret;
1001 int qlcnic_clear_esw_stats(struct qlcnic_adapter *adapter, const u8 func_esw,
1002 const u8 port, const u8 rx_tx)
1005 u32 arg1;
1006 u32 *rd_args[3];
1008 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
1009 return -EIO;
1011 if (func_esw == QLCNIC_STATS_PORT) {
1012 if (port >= QLCNIC_MAX_PCI_FUNC)
1013 goto err_ret;
1014 } else if (func_esw == QLCNIC_STATS_ESWITCH) {
1015 if (port >= QLCNIC_NIU_MAX_XG_PORTS)
1016 goto err_ret;
1017 } else {
1018 goto err_ret;
1021 if (rx_tx > QLCNIC_QUERY_TX_COUNTER)
1022 goto err_ret;
1024 arg1 = port | QLCNIC_STATS_VERSION << 8 | func_esw << 12;
1025 arg1 |= BIT_14 | rx_tx << 15;
1027 memset(rd_args, 0, sizeof(rd_args));
1028 return qlcnic_issue_cmd(adapter,
1029 adapter->ahw->pci_func,
1030 adapter->fw_hal_version,
1031 arg1,
1034 QLCNIC_CDRP_CMD_GET_ESWITCH_STATS,
1035 rd_args);
1037 err_ret:
1038 dev_err(&adapter->pdev->dev, "Invalid argument func_esw=%d port=%d"
1039 "rx_ctx=%d\n", func_esw, port, rx_tx);
1040 return -EIO;
1043 static int
1044 __qlcnic_get_eswitch_port_config(struct qlcnic_adapter *adapter,
1045 u32 *arg1, u32 *arg2)
1047 int err = -EIO;
1048 u32 *rd_args[3];
1049 u8 pci_func;
1050 pci_func = (*arg1 >> 8);
1051 rd_args[0] = arg1;
1052 rd_args[1] = arg2;
1053 rd_args[2] = 0;
1054 err = qlcnic_issue_cmd(adapter,
1055 adapter->ahw->pci_func,
1056 adapter->fw_hal_version,
1057 *arg1,
1060 QLCNIC_CDRP_CMD_GET_ESWITCH_PORT_CONFIG,
1061 rd_args);
1063 if (err == QLCNIC_RCODE_SUCCESS) {
1064 dev_info(&adapter->pdev->dev,
1065 "eSwitch port config for pci func %d\n", pci_func);
1066 } else {
1067 dev_err(&adapter->pdev->dev,
1068 "Failed to get eswitch port config for pci func %d\n",
1069 pci_func);
1071 return err;
1073 /* Configure eSwitch port
1074 op_mode = 0 for setting default port behavior
1075 op_mode = 1 for setting vlan id
1076 op_mode = 2 for deleting vlan id
1077 op_type = 0 for vlan_id
1078 op_type = 1 for port vlan_id
1080 int qlcnic_config_switch_port(struct qlcnic_adapter *adapter,
1081 struct qlcnic_esw_func_cfg *esw_cfg)
1083 int err = -EIO;
1084 u32 arg1, arg2 = 0;
1085 u32 *rd_args[3];
1086 u8 pci_func;
1088 if (adapter->op_mode != QLCNIC_MGMT_FUNC)
1089 return err;
1090 pci_func = esw_cfg->pci_func;
1091 arg1 = (adapter->npars[pci_func].phy_port & BIT_0);
1092 arg1 |= (pci_func << 8);
1094 if (__qlcnic_get_eswitch_port_config(adapter, &arg1, &arg2))
1095 return err;
1096 arg1 &= ~(0x0ff << 8);
1097 arg1 |= (pci_func << 8);
1098 arg1 &= ~(BIT_2 | BIT_3);
1099 switch (esw_cfg->op_mode) {
1100 case QLCNIC_PORT_DEFAULTS:
1101 arg1 |= (BIT_4 | BIT_6 | BIT_7);
1102 arg2 |= (BIT_0 | BIT_1);
1103 if (adapter->capabilities & QLCNIC_FW_CAPABILITY_TSO)
1104 arg2 |= (BIT_2 | BIT_3);
1105 if (!(esw_cfg->discard_tagged))
1106 arg1 &= ~BIT_4;
1107 if (!(esw_cfg->promisc_mode))
1108 arg1 &= ~BIT_6;
1109 if (!(esw_cfg->mac_override))
1110 arg1 &= ~BIT_7;
1111 if (!(esw_cfg->mac_anti_spoof))
1112 arg2 &= ~BIT_0;
1113 if (!(esw_cfg->offload_flags & BIT_0))
1114 arg2 &= ~(BIT_1 | BIT_2 | BIT_3);
1115 if (!(esw_cfg->offload_flags & BIT_1))
1116 arg2 &= ~BIT_2;
1117 if (!(esw_cfg->offload_flags & BIT_2))
1118 arg2 &= ~BIT_3;
1119 break;
1120 case QLCNIC_ADD_VLAN:
1121 arg1 |= (BIT_2 | BIT_5);
1122 arg1 |= (esw_cfg->vlan_id << 16);
1123 break;
1124 case QLCNIC_DEL_VLAN:
1125 arg1 |= (BIT_3 | BIT_5);
1126 arg1 &= ~(0x0ffff << 16);
1127 break;
1128 default:
1129 return err;
1132 memset(rd_args, 0, sizeof(rd_args));
1133 err = qlcnic_issue_cmd(adapter,
1134 adapter->ahw->pci_func,
1135 adapter->fw_hal_version,
1136 arg1,
1137 arg2,
1139 QLCNIC_CDRP_CMD_CONFIGURE_ESWITCH,
1140 rd_args);
1142 if (err != QLCNIC_RCODE_SUCCESS) {
1143 dev_err(&adapter->pdev->dev,
1144 "Failed to configure eswitch pci func %d\n", pci_func);
1145 } else {
1146 dev_info(&adapter->pdev->dev,
1147 "Configured eSwitch for pci func %d\n", pci_func);
1150 return err;
1154 qlcnic_get_eswitch_port_config(struct qlcnic_adapter *adapter,
1155 struct qlcnic_esw_func_cfg *esw_cfg)
1157 u32 arg1, arg2;
1158 u8 phy_port;
1159 if (adapter->op_mode == QLCNIC_MGMT_FUNC)
1160 phy_port = adapter->npars[esw_cfg->pci_func].phy_port;
1161 else
1162 phy_port = adapter->physical_port;
1163 arg1 = phy_port;
1164 arg1 |= (esw_cfg->pci_func << 8);
1165 if (__qlcnic_get_eswitch_port_config(adapter, &arg1, &arg2))
1166 return -EIO;
1168 esw_cfg->discard_tagged = !!(arg1 & BIT_4);
1169 esw_cfg->host_vlan_tag = !!(arg1 & BIT_5);
1170 esw_cfg->promisc_mode = !!(arg1 & BIT_6);
1171 esw_cfg->mac_override = !!(arg1 & BIT_7);
1172 esw_cfg->vlan_id = LSW(arg1 >> 16);
1173 esw_cfg->mac_anti_spoof = (arg2 & 0x1);
1174 esw_cfg->offload_flags = ((arg2 >> 1) & 0x7);
1176 return 0;