]> Git Repo - linux.git/blob - drivers/staging/qlge/qlge_mpi.c
efi/libstub: Optimize for size instead of speed
[linux.git] / drivers / staging / qlge / qlge_mpi.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include "qlge.h"
3
4 int ql_unpause_mpi_risc(struct ql_adapter *qdev)
5 {
6         u32 tmp;
7
8         /* Un-pause the RISC */
9         tmp = ql_read32(qdev, CSR);
10         if (!(tmp & CSR_RP))
11                 return -EIO;
12
13         ql_write32(qdev, CSR, CSR_CMD_CLR_PAUSE);
14         return 0;
15 }
16
17 int ql_pause_mpi_risc(struct ql_adapter *qdev)
18 {
19         u32 tmp;
20         int count = UDELAY_COUNT;
21
22         /* Pause the RISC */
23         ql_write32(qdev, CSR, CSR_CMD_SET_PAUSE);
24         do {
25                 tmp = ql_read32(qdev, CSR);
26                 if (tmp & CSR_RP)
27                         break;
28                 mdelay(UDELAY_DELAY);
29                 count--;
30         } while (count);
31         return (count == 0) ? -ETIMEDOUT : 0;
32 }
33
34 int ql_hard_reset_mpi_risc(struct ql_adapter *qdev)
35 {
36         u32 tmp;
37         int count = UDELAY_COUNT;
38
39         /* Reset the RISC */
40         ql_write32(qdev, CSR, CSR_CMD_SET_RST);
41         do {
42                 tmp = ql_read32(qdev, CSR);
43                 if (tmp & CSR_RR) {
44                         ql_write32(qdev, CSR, CSR_CMD_CLR_RST);
45                         break;
46                 }
47                 mdelay(UDELAY_DELAY);
48                 count--;
49         } while (count);
50         return (count == 0) ? -ETIMEDOUT : 0;
51 }
52
53 int ql_read_mpi_reg(struct ql_adapter *qdev, u32 reg, u32 *data)
54 {
55         int status;
56         /* wait for reg to come ready */
57         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
58         if (status)
59                 goto exit;
60         /* set up for reg read */
61         ql_write32(qdev, PROC_ADDR, reg | PROC_ADDR_R);
62         /* wait for reg to come ready */
63         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
64         if (status)
65                 goto exit;
66         /* get the data */
67         *data = ql_read32(qdev, PROC_DATA);
68 exit:
69         return status;
70 }
71
72 int ql_write_mpi_reg(struct ql_adapter *qdev, u32 reg, u32 data)
73 {
74         int status = 0;
75         /* wait for reg to come ready */
76         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
77         if (status)
78                 goto exit;
79         /* write the data to the data reg */
80         ql_write32(qdev, PROC_DATA, data);
81         /* trigger the write */
82         ql_write32(qdev, PROC_ADDR, reg);
83         /* wait for reg to come ready */
84         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
85         if (status)
86                 goto exit;
87 exit:
88         return status;
89 }
90
91 int ql_soft_reset_mpi_risc(struct ql_adapter *qdev)
92 {
93         return ql_write_mpi_reg(qdev, 0x00001010, 1);
94 }
95
96 /* Determine if we are in charge of the firwmare. If
97  * we are the lower of the 2 NIC pcie functions, or if
98  * we are the higher function and the lower function
99  * is not enabled.
100  */
101 int ql_own_firmware(struct ql_adapter *qdev)
102 {
103         u32 temp;
104
105         /* If we are the lower of the 2 NIC functions
106          * on the chip the we are responsible for
107          * core dump and firmware reset after an error.
108          */
109         if (qdev->func < qdev->alt_func)
110                 return 1;
111
112         /* If we are the higher of the 2 NIC functions
113          * on the chip and the lower function is not
114          * enabled, then we are responsible for
115          * core dump and firmware reset after an error.
116          */
117         temp =  ql_read32(qdev, STS);
118         if (!(temp & (1 << (8 + qdev->alt_func))))
119                 return 1;
120
121         return 0;
122
123 }
124
125 static int ql_get_mb_sts(struct ql_adapter *qdev, struct mbox_params *mbcp)
126 {
127         int i, status;
128
129         status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK);
130         if (status)
131                 return -EBUSY;
132         for (i = 0; i < mbcp->out_count; i++) {
133                 status =
134                     ql_read_mpi_reg(qdev, qdev->mailbox_out + i,
135                                     &mbcp->mbox_out[i]);
136                 if (status) {
137                         netif_err(qdev, drv, qdev->ndev, "Failed mailbox read.\n");
138                         break;
139                 }
140         }
141         ql_sem_unlock(qdev, SEM_PROC_REG_MASK); /* does flush too */
142         return status;
143 }
144
145 /* Wait for a single mailbox command to complete.
146  * Returns zero on success.
147  */
148 static int ql_wait_mbx_cmd_cmplt(struct ql_adapter *qdev)
149 {
150         int count = 100;
151         u32 value;
152
153         do {
154                 value = ql_read32(qdev, STS);
155                 if (value & STS_PI)
156                         return 0;
157                 mdelay(UDELAY_DELAY); /* 100ms */
158         } while (--count);
159         return -ETIMEDOUT;
160 }
161
162 /* Execute a single mailbox command.
163  * Caller must hold PROC_ADDR semaphore.
164  */
165 static int ql_exec_mb_cmd(struct ql_adapter *qdev, struct mbox_params *mbcp)
166 {
167         int i, status;
168
169         /*
170          * Make sure there's nothing pending.
171          * This shouldn't happen.
172          */
173         if (ql_read32(qdev, CSR) & CSR_HRI)
174                 return -EIO;
175
176         status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK);
177         if (status)
178                 return status;
179
180         /*
181          * Fill the outbound mailboxes.
182          */
183         for (i = 0; i < mbcp->in_count; i++) {
184                 status = ql_write_mpi_reg(qdev, qdev->mailbox_in + i,
185                                           mbcp->mbox_in[i]);
186                 if (status)
187                         goto end;
188         }
189         /*
190          * Wake up the MPI firmware.
191          */
192         ql_write32(qdev, CSR, CSR_CMD_SET_H2R_INT);
193 end:
194         ql_sem_unlock(qdev, SEM_PROC_REG_MASK);
195         return status;
196 }
197
198 /* We are being asked by firmware to accept
199  * a change to the port.  This is only
200  * a change to max frame sizes (Tx/Rx), pause
201  * parameters, or loopback mode. We wake up a worker
202  * to handler processing this since a mailbox command
203  * will need to be sent to ACK the request.
204  */
205 static int ql_idc_req_aen(struct ql_adapter *qdev)
206 {
207         int status;
208         struct mbox_params *mbcp = &qdev->idc_mbc;
209
210         netif_err(qdev, drv, qdev->ndev, "Enter!\n");
211         /* Get the status data and start up a thread to
212          * handle the request.
213          */
214         mbcp->out_count = 4;
215         status = ql_get_mb_sts(qdev, mbcp);
216         if (status) {
217                 netif_err(qdev, drv, qdev->ndev,
218                           "Could not read MPI, resetting ASIC!\n");
219                 ql_queue_asic_error(qdev);
220         } else  {
221                 /* Begin polled mode early so
222                  * we don't get another interrupt
223                  * when we leave mpi_worker.
224                  */
225                 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
226                 queue_delayed_work(qdev->workqueue, &qdev->mpi_idc_work, 0);
227         }
228         return status;
229 }
230
231 /* Process an inter-device event completion.
232  * If good, signal the caller's completion.
233  */
234 static int ql_idc_cmplt_aen(struct ql_adapter *qdev)
235 {
236         int status;
237         struct mbox_params *mbcp = &qdev->idc_mbc;
238
239         mbcp->out_count = 4;
240         status = ql_get_mb_sts(qdev, mbcp);
241         if (status) {
242                 netif_err(qdev, drv, qdev->ndev,
243                           "Could not read MPI, resetting RISC!\n");
244                 ql_queue_fw_error(qdev);
245         } else
246                 /* Wake up the sleeping mpi_idc_work thread that is
247                  * waiting for this event.
248                  */
249                 complete(&qdev->ide_completion);
250
251         return status;
252 }
253
254 static void ql_link_up(struct ql_adapter *qdev, struct mbox_params *mbcp)
255 {
256         int status;
257
258         mbcp->out_count = 2;
259
260         status = ql_get_mb_sts(qdev, mbcp);
261         if (status) {
262                 netif_err(qdev, drv, qdev->ndev,
263                           "%s: Could not get mailbox status.\n", __func__);
264                 return;
265         }
266
267         qdev->link_status = mbcp->mbox_out[1];
268         netif_err(qdev, drv, qdev->ndev, "Link Up.\n");
269
270         /* If we're coming back from an IDC event
271          * then set up the CAM and frame routing.
272          */
273         if (test_bit(QL_CAM_RT_SET, &qdev->flags)) {
274                 status = ql_cam_route_initialize(qdev);
275                 if (status) {
276                         netif_err(qdev, ifup, qdev->ndev,
277                                   "Failed to init CAM/Routing tables.\n");
278                         return;
279                 } else
280                         clear_bit(QL_CAM_RT_SET, &qdev->flags);
281         }
282
283         /* Queue up a worker to check the frame
284          * size information, and fix it if it's not
285          * to our liking.
286          */
287         if (!test_bit(QL_PORT_CFG, &qdev->flags)) {
288                 netif_err(qdev, drv, qdev->ndev, "Queue Port Config Worker!\n");
289                 set_bit(QL_PORT_CFG, &qdev->flags);
290                 /* Begin polled mode early so
291                  * we don't get another interrupt
292                  * when we leave mpi_worker dpc.
293                  */
294                 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
295                 queue_delayed_work(qdev->workqueue,
296                                    &qdev->mpi_port_cfg_work, 0);
297         }
298
299         ql_link_on(qdev);
300 }
301
302 static void ql_link_down(struct ql_adapter *qdev, struct mbox_params *mbcp)
303 {
304         int status;
305
306         mbcp->out_count = 3;
307
308         status = ql_get_mb_sts(qdev, mbcp);
309         if (status)
310                 netif_err(qdev, drv, qdev->ndev, "Link down AEN broken!\n");
311
312         ql_link_off(qdev);
313 }
314
315 static int ql_sfp_in(struct ql_adapter *qdev, struct mbox_params *mbcp)
316 {
317         int status;
318
319         mbcp->out_count = 5;
320
321         status = ql_get_mb_sts(qdev, mbcp);
322         if (status)
323                 netif_err(qdev, drv, qdev->ndev, "SFP in AEN broken!\n");
324         else
325                 netif_err(qdev, drv, qdev->ndev, "SFP insertion detected.\n");
326
327         return status;
328 }
329
330 static int ql_sfp_out(struct ql_adapter *qdev, struct mbox_params *mbcp)
331 {
332         int status;
333
334         mbcp->out_count = 1;
335
336         status = ql_get_mb_sts(qdev, mbcp);
337         if (status)
338                 netif_err(qdev, drv, qdev->ndev, "SFP out AEN broken!\n");
339         else
340                 netif_err(qdev, drv, qdev->ndev, "SFP removal detected.\n");
341
342         return status;
343 }
344
345 static int ql_aen_lost(struct ql_adapter *qdev, struct mbox_params *mbcp)
346 {
347         int status;
348
349         mbcp->out_count = 6;
350
351         status = ql_get_mb_sts(qdev, mbcp);
352         if (status)
353                 netif_err(qdev, drv, qdev->ndev, "Lost AEN broken!\n");
354         else {
355                 int i;
356
357                 netif_err(qdev, drv, qdev->ndev, "Lost AEN detected.\n");
358                 for (i = 0; i < mbcp->out_count; i++)
359                         netif_err(qdev, drv, qdev->ndev, "mbox_out[%d] = 0x%.08x.\n",
360                                   i, mbcp->mbox_out[i]);
361
362         }
363
364         return status;
365 }
366
367 static void ql_init_fw_done(struct ql_adapter *qdev, struct mbox_params *mbcp)
368 {
369         int status;
370
371         mbcp->out_count = 2;
372
373         status = ql_get_mb_sts(qdev, mbcp);
374         if (status) {
375                 netif_err(qdev, drv, qdev->ndev, "Firmware did not initialize!\n");
376         } else {
377                 netif_err(qdev, drv, qdev->ndev, "Firmware Revision  = 0x%.08x.\n",
378                           mbcp->mbox_out[1]);
379                 qdev->fw_rev_id = mbcp->mbox_out[1];
380                 status = ql_cam_route_initialize(qdev);
381                 if (status)
382                         netif_err(qdev, ifup, qdev->ndev,
383                                   "Failed to init CAM/Routing tables.\n");
384         }
385 }
386
387 /* Process an async event and clear it unless it's an
388  * error condition.
389  *  This can get called iteratively from the mpi_work thread
390  *  when events arrive via an interrupt.
391  *  It also gets called when a mailbox command is polling for
392  *  it's completion. */
393 static int ql_mpi_handler(struct ql_adapter *qdev, struct mbox_params *mbcp)
394 {
395         int status;
396         int orig_count = mbcp->out_count;
397
398         /* Just get mailbox zero for now. */
399         mbcp->out_count = 1;
400         status = ql_get_mb_sts(qdev, mbcp);
401         if (status) {
402                 netif_err(qdev, drv, qdev->ndev,
403                           "Could not read MPI, resetting ASIC!\n");
404                 ql_queue_asic_error(qdev);
405                 goto end;
406         }
407
408         switch (mbcp->mbox_out[0]) {
409
410         /* This case is only active when we arrive here
411          * as a result of issuing a mailbox command to
412          * the firmware.
413          */
414         case MB_CMD_STS_INTRMDT:
415         case MB_CMD_STS_GOOD:
416         case MB_CMD_STS_INVLD_CMD:
417         case MB_CMD_STS_XFC_ERR:
418         case MB_CMD_STS_CSUM_ERR:
419         case MB_CMD_STS_ERR:
420         case MB_CMD_STS_PARAM_ERR:
421                 /* We can only get mailbox status if we're polling from an
422                  * unfinished command.  Get the rest of the status data and
423                  * return back to the caller.
424                  * We only end up here when we're polling for a mailbox
425                  * command completion.
426                  */
427                 mbcp->out_count = orig_count;
428                 status = ql_get_mb_sts(qdev, mbcp);
429                 return status;
430
431         /* We are being asked by firmware to accept
432          * a change to the port.  This is only
433          * a change to max frame sizes (Tx/Rx), pause
434          * parameters, or loopback mode.
435          */
436         case AEN_IDC_REQ:
437                 status = ql_idc_req_aen(qdev);
438                 break;
439
440         /* Process and inbound IDC event.
441          * This will happen when we're trying to
442          * change tx/rx max frame size, change pause
443          * parameters or loopback mode.
444          */
445         case AEN_IDC_CMPLT:
446         case AEN_IDC_EXT:
447                 status = ql_idc_cmplt_aen(qdev);
448                 break;
449
450         case AEN_LINK_UP:
451                 ql_link_up(qdev, mbcp);
452                 break;
453
454         case AEN_LINK_DOWN:
455                 ql_link_down(qdev, mbcp);
456                 break;
457
458         case AEN_FW_INIT_DONE:
459                 /* If we're in process on executing the firmware,
460                  * then convert the status to normal mailbox status.
461                  */
462                 if (mbcp->mbox_in[0] == MB_CMD_EX_FW) {
463                         mbcp->out_count = orig_count;
464                         status = ql_get_mb_sts(qdev, mbcp);
465                         mbcp->mbox_out[0] = MB_CMD_STS_GOOD;
466                         return status;
467                 }
468                 ql_init_fw_done(qdev, mbcp);
469                 break;
470
471         case AEN_AEN_SFP_IN:
472                 ql_sfp_in(qdev, mbcp);
473                 break;
474
475         case AEN_AEN_SFP_OUT:
476                 ql_sfp_out(qdev, mbcp);
477                 break;
478
479         /* This event can arrive at boot time or after an
480          * MPI reset if the firmware failed to initialize.
481          */
482         case AEN_FW_INIT_FAIL:
483                 /* If we're in process on executing the firmware,
484                  * then convert the status to normal mailbox status.
485                  */
486                 if (mbcp->mbox_in[0] == MB_CMD_EX_FW) {
487                         mbcp->out_count = orig_count;
488                         status = ql_get_mb_sts(qdev, mbcp);
489                         mbcp->mbox_out[0] = MB_CMD_STS_ERR;
490                         return status;
491                 }
492                 netif_err(qdev, drv, qdev->ndev,
493                           "Firmware initialization failed.\n");
494                 status = -EIO;
495                 ql_queue_fw_error(qdev);
496                 break;
497
498         case AEN_SYS_ERR:
499                 netif_err(qdev, drv, qdev->ndev, "System Error.\n");
500                 ql_queue_fw_error(qdev);
501                 status = -EIO;
502                 break;
503
504         case AEN_AEN_LOST:
505                 ql_aen_lost(qdev, mbcp);
506                 break;
507
508         case AEN_DCBX_CHG:
509                 /* Need to support AEN 8110 */
510                 break;
511         default:
512                 netif_err(qdev, drv, qdev->ndev,
513                           "Unsupported AE %.08x.\n", mbcp->mbox_out[0]);
514                 /* Clear the MPI firmware status. */
515         }
516 end:
517         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
518         /* Restore the original mailbox count to
519          * what the caller asked for.  This can get
520          * changed when a mailbox command is waiting
521          * for a response and an AEN arrives and
522          * is handled.
523          * */
524         mbcp->out_count = orig_count;
525         return status;
526 }
527
528 /* Execute a single mailbox command.
529  * mbcp is a pointer to an array of u32.  Each
530  * element in the array contains the value for it's
531  * respective mailbox register.
532  */
533 static int ql_mailbox_command(struct ql_adapter *qdev, struct mbox_params *mbcp)
534 {
535         int status;
536         unsigned long count;
537
538         mutex_lock(&qdev->mpi_mutex);
539
540         /* Begin polled mode for MPI */
541         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
542
543         /* Load the mailbox registers and wake up MPI RISC. */
544         status = ql_exec_mb_cmd(qdev, mbcp);
545         if (status)
546                 goto end;
547
548         /* If we're generating a system error, then there's nothing
549          * to wait for.
550          */
551         if (mbcp->mbox_in[0] == MB_CMD_MAKE_SYS_ERR)
552                 goto end;
553
554         /* Wait for the command to complete. We loop
555          * here because some AEN might arrive while
556          * we're waiting for the mailbox command to
557          * complete. If more than 5 seconds expire we can
558          * assume something is wrong. */
559         count = jiffies + HZ * MAILBOX_TIMEOUT;
560         do {
561                 /* Wait for the interrupt to come in. */
562                 status = ql_wait_mbx_cmd_cmplt(qdev);
563                 if (status)
564                         continue;
565
566                 /* Process the event.  If it's an AEN, it
567                  * will be handled in-line or a worker
568                  * will be spawned. If it's our completion
569                  * we will catch it below.
570                  */
571                 status = ql_mpi_handler(qdev, mbcp);
572                 if (status)
573                         goto end;
574
575                 /* It's either the completion for our mailbox
576                  * command complete or an AEN.  If it's our
577                  * completion then get out.
578                  */
579                 if (((mbcp->mbox_out[0] & 0x0000f000) ==
580                                         MB_CMD_STS_GOOD) ||
581                         ((mbcp->mbox_out[0] & 0x0000f000) ==
582                                         MB_CMD_STS_INTRMDT))
583                         goto done;
584         } while (time_before(jiffies, count));
585
586         netif_err(qdev, drv, qdev->ndev,
587                   "Timed out waiting for mailbox complete.\n");
588         status = -ETIMEDOUT;
589         goto end;
590
591 done:
592
593         /* Now we can clear the interrupt condition
594          * and look at our status.
595          */
596         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
597
598         if (((mbcp->mbox_out[0] & 0x0000f000) !=
599                                         MB_CMD_STS_GOOD) &&
600                 ((mbcp->mbox_out[0] & 0x0000f000) !=
601                                         MB_CMD_STS_INTRMDT)) {
602                 status = -EIO;
603         }
604 end:
605         /* End polled mode for MPI */
606         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
607         mutex_unlock(&qdev->mpi_mutex);
608         return status;
609 }
610
611 /* Get MPI firmware version. This will be used for
612  * driver banner and for ethtool info.
613  * Returns zero on success.
614  */
615 int ql_mb_about_fw(struct ql_adapter *qdev)
616 {
617         struct mbox_params mbc;
618         struct mbox_params *mbcp = &mbc;
619         int status = 0;
620
621         memset(mbcp, 0, sizeof(struct mbox_params));
622
623         mbcp->in_count = 1;
624         mbcp->out_count = 3;
625
626         mbcp->mbox_in[0] = MB_CMD_ABOUT_FW;
627
628         status = ql_mailbox_command(qdev, mbcp);
629         if (status)
630                 return status;
631
632         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
633                 netif_err(qdev, drv, qdev->ndev,
634                           "Failed about firmware command\n");
635                 status = -EIO;
636         }
637
638         /* Store the firmware version */
639         qdev->fw_rev_id = mbcp->mbox_out[1];
640
641         return status;
642 }
643
644 /* Get functional state for MPI firmware.
645  * Returns zero on success.
646  */
647 int ql_mb_get_fw_state(struct ql_adapter *qdev)
648 {
649         struct mbox_params mbc;
650         struct mbox_params *mbcp = &mbc;
651         int status = 0;
652
653         memset(mbcp, 0, sizeof(struct mbox_params));
654
655         mbcp->in_count = 1;
656         mbcp->out_count = 2;
657
658         mbcp->mbox_in[0] = MB_CMD_GET_FW_STATE;
659
660         status = ql_mailbox_command(qdev, mbcp);
661         if (status)
662                 return status;
663
664         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
665                 netif_err(qdev, drv, qdev->ndev,
666                           "Failed Get Firmware State.\n");
667                 status = -EIO;
668         }
669
670         /* If bit zero is set in mbx 1 then the firmware is
671          * running, but not initialized.  This should never
672          * happen.
673          */
674         if (mbcp->mbox_out[1] & 1) {
675                 netif_err(qdev, drv, qdev->ndev,
676                           "Firmware waiting for initialization.\n");
677                 status = -EIO;
678         }
679
680         return status;
681 }
682
683 /* Send and ACK mailbox command to the firmware to
684  * let it continue with the change.
685  */
686 static int ql_mb_idc_ack(struct ql_adapter *qdev)
687 {
688         struct mbox_params mbc;
689         struct mbox_params *mbcp = &mbc;
690         int status = 0;
691
692         memset(mbcp, 0, sizeof(struct mbox_params));
693
694         mbcp->in_count = 5;
695         mbcp->out_count = 1;
696
697         mbcp->mbox_in[0] = MB_CMD_IDC_ACK;
698         mbcp->mbox_in[1] = qdev->idc_mbc.mbox_out[1];
699         mbcp->mbox_in[2] = qdev->idc_mbc.mbox_out[2];
700         mbcp->mbox_in[3] = qdev->idc_mbc.mbox_out[3];
701         mbcp->mbox_in[4] = qdev->idc_mbc.mbox_out[4];
702
703         status = ql_mailbox_command(qdev, mbcp);
704         if (status)
705                 return status;
706
707         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
708                 netif_err(qdev, drv, qdev->ndev, "Failed IDC ACK send.\n");
709                 status = -EIO;
710         }
711         return status;
712 }
713
714 /* Get link settings and maximum frame size settings
715  * for the current port.
716  * Most likely will block.
717  */
718 int ql_mb_set_port_cfg(struct ql_adapter *qdev)
719 {
720         struct mbox_params mbc;
721         struct mbox_params *mbcp = &mbc;
722         int status = 0;
723
724         memset(mbcp, 0, sizeof(struct mbox_params));
725
726         mbcp->in_count = 3;
727         mbcp->out_count = 1;
728
729         mbcp->mbox_in[0] = MB_CMD_SET_PORT_CFG;
730         mbcp->mbox_in[1] = qdev->link_config;
731         mbcp->mbox_in[2] = qdev->max_frame_size;
732
733         status = ql_mailbox_command(qdev, mbcp);
734         if (status)
735                 return status;
736
737         if (mbcp->mbox_out[0] == MB_CMD_STS_INTRMDT) {
738                 netif_err(qdev, drv, qdev->ndev,
739                           "Port Config sent, wait for IDC.\n");
740         } else  if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
741                 netif_err(qdev, drv, qdev->ndev,
742                           "Failed Set Port Configuration.\n");
743                 status = -EIO;
744         }
745         return status;
746 }
747
748 static int ql_mb_dump_ram(struct ql_adapter *qdev, u64 req_dma, u32 addr,
749                           u32 size)
750 {
751         int status = 0;
752         struct mbox_params mbc;
753         struct mbox_params *mbcp = &mbc;
754
755         memset(mbcp, 0, sizeof(struct mbox_params));
756
757         mbcp->in_count = 9;
758         mbcp->out_count = 1;
759
760         mbcp->mbox_in[0] = MB_CMD_DUMP_RISC_RAM;
761         mbcp->mbox_in[1] = LSW(addr);
762         mbcp->mbox_in[2] = MSW(req_dma);
763         mbcp->mbox_in[3] = LSW(req_dma);
764         mbcp->mbox_in[4] = MSW(size);
765         mbcp->mbox_in[5] = LSW(size);
766         mbcp->mbox_in[6] = MSW(MSD(req_dma));
767         mbcp->mbox_in[7] = LSW(MSD(req_dma));
768         mbcp->mbox_in[8] = MSW(addr);
769
770         status = ql_mailbox_command(qdev, mbcp);
771         if (status)
772                 return status;
773
774         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
775                 netif_err(qdev, drv, qdev->ndev, "Failed to dump risc RAM.\n");
776                 status = -EIO;
777         }
778         return status;
779 }
780
781 /* Issue a mailbox command to dump RISC RAM. */
782 int ql_dump_risc_ram_area(struct ql_adapter *qdev, void *buf,
783                           u32 ram_addr, int word_count)
784 {
785         int status;
786         char *my_buf;
787         dma_addr_t buf_dma;
788
789         my_buf = pci_alloc_consistent(qdev->pdev, word_count * sizeof(u32),
790                                       &buf_dma);
791         if (!my_buf)
792                 return -EIO;
793
794         status = ql_mb_dump_ram(qdev, buf_dma, ram_addr, word_count);
795         if (!status)
796                 memcpy(buf, my_buf, word_count * sizeof(u32));
797
798         pci_free_consistent(qdev->pdev, word_count * sizeof(u32), my_buf,
799                             buf_dma);
800         return status;
801 }
802
803 /* Get link settings and maximum frame size settings
804  * for the current port.
805  * Most likely will block.
806  */
807 int ql_mb_get_port_cfg(struct ql_adapter *qdev)
808 {
809         struct mbox_params mbc;
810         struct mbox_params *mbcp = &mbc;
811         int status = 0;
812
813         memset(mbcp, 0, sizeof(struct mbox_params));
814
815         mbcp->in_count = 1;
816         mbcp->out_count = 3;
817
818         mbcp->mbox_in[0] = MB_CMD_GET_PORT_CFG;
819
820         status = ql_mailbox_command(qdev, mbcp);
821         if (status)
822                 return status;
823
824         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
825                 netif_err(qdev, drv, qdev->ndev,
826                           "Failed Get Port Configuration.\n");
827                 status = -EIO;
828         } else  {
829                 netif_printk(qdev, drv, KERN_DEBUG, qdev->ndev,
830                              "Passed Get Port Configuration.\n");
831                 qdev->link_config = mbcp->mbox_out[1];
832                 qdev->max_frame_size = mbcp->mbox_out[2];
833         }
834         return status;
835 }
836
837 int ql_mb_wol_mode(struct ql_adapter *qdev, u32 wol)
838 {
839         struct mbox_params mbc;
840         struct mbox_params *mbcp = &mbc;
841         int status;
842
843         memset(mbcp, 0, sizeof(struct mbox_params));
844
845         mbcp->in_count = 2;
846         mbcp->out_count = 1;
847
848         mbcp->mbox_in[0] = MB_CMD_SET_WOL_MODE;
849         mbcp->mbox_in[1] = wol;
850
851         status = ql_mailbox_command(qdev, mbcp);
852         if (status)
853                 return status;
854
855         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
856                 netif_err(qdev, drv, qdev->ndev, "Failed to set WOL mode.\n");
857                 status = -EIO;
858         }
859         return status;
860 }
861
862 int ql_mb_wol_set_magic(struct ql_adapter *qdev, u32 enable_wol)
863 {
864         struct mbox_params mbc;
865         struct mbox_params *mbcp = &mbc;
866         int status;
867         u8 *addr = qdev->ndev->dev_addr;
868
869         memset(mbcp, 0, sizeof(struct mbox_params));
870
871         mbcp->in_count = 8;
872         mbcp->out_count = 1;
873
874         mbcp->mbox_in[0] = MB_CMD_SET_WOL_MAGIC;
875         if (enable_wol) {
876                 mbcp->mbox_in[1] = (u32)addr[0];
877                 mbcp->mbox_in[2] = (u32)addr[1];
878                 mbcp->mbox_in[3] = (u32)addr[2];
879                 mbcp->mbox_in[4] = (u32)addr[3];
880                 mbcp->mbox_in[5] = (u32)addr[4];
881                 mbcp->mbox_in[6] = (u32)addr[5];
882                 mbcp->mbox_in[7] = 0;
883         } else {
884                 mbcp->mbox_in[1] = 0;
885                 mbcp->mbox_in[2] = 1;
886                 mbcp->mbox_in[3] = 1;
887                 mbcp->mbox_in[4] = 1;
888                 mbcp->mbox_in[5] = 1;
889                 mbcp->mbox_in[6] = 1;
890                 mbcp->mbox_in[7] = 0;
891         }
892
893         status = ql_mailbox_command(qdev, mbcp);
894         if (status)
895                 return status;
896
897         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
898                 netif_err(qdev, drv, qdev->ndev, "Failed to set WOL mode.\n");
899                 status = -EIO;
900         }
901         return status;
902 }
903
904 /* IDC - Inter Device Communication...
905  * Some firmware commands require consent of adjacent FCOE
906  * function.  This function waits for the OK, or a
907  * counter-request for a little more time.i
908  * The firmware will complete the request if the other
909  * function doesn't respond.
910  */
911 static int ql_idc_wait(struct ql_adapter *qdev)
912 {
913         int status = -ETIMEDOUT;
914         long wait_time = 1 * HZ;
915         struct mbox_params *mbcp = &qdev->idc_mbc;
916
917         do {
918                 /* Wait here for the command to complete
919                  * via the IDC process.
920                  */
921                 wait_time =
922                         wait_for_completion_timeout(&qdev->ide_completion,
923                                                     wait_time);
924                 if (!wait_time) {
925                         netif_err(qdev, drv, qdev->ndev, "IDC Timeout.\n");
926                         break;
927                 }
928                 /* Now examine the response from the IDC process.
929                  * We might have a good completion or a request for
930                  * more wait time.
931                  */
932                 if (mbcp->mbox_out[0] == AEN_IDC_EXT) {
933                         netif_err(qdev, drv, qdev->ndev,
934                                   "IDC Time Extension from function.\n");
935                         wait_time += (mbcp->mbox_out[1] >> 8) & 0x0000000f;
936                 } else if (mbcp->mbox_out[0] == AEN_IDC_CMPLT) {
937                         netif_err(qdev, drv, qdev->ndev, "IDC Success.\n");
938                         status = 0;
939                         break;
940                 } else {
941                         netif_err(qdev, drv, qdev->ndev,
942                                   "IDC: Invalid State 0x%.04x.\n",
943                                   mbcp->mbox_out[0]);
944                         status = -EIO;
945                         break;
946                 }
947         } while (wait_time);
948
949         return status;
950 }
951
952 int ql_mb_set_led_cfg(struct ql_adapter *qdev, u32 led_config)
953 {
954         struct mbox_params mbc;
955         struct mbox_params *mbcp = &mbc;
956         int status;
957
958         memset(mbcp, 0, sizeof(struct mbox_params));
959
960         mbcp->in_count = 2;
961         mbcp->out_count = 1;
962
963         mbcp->mbox_in[0] = MB_CMD_SET_LED_CFG;
964         mbcp->mbox_in[1] = led_config;
965
966         status = ql_mailbox_command(qdev, mbcp);
967         if (status)
968                 return status;
969
970         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
971                 netif_err(qdev, drv, qdev->ndev,
972                           "Failed to set LED Configuration.\n");
973                 status = -EIO;
974         }
975
976         return status;
977 }
978
979 int ql_mb_get_led_cfg(struct ql_adapter *qdev)
980 {
981         struct mbox_params mbc;
982         struct mbox_params *mbcp = &mbc;
983         int status;
984
985         memset(mbcp, 0, sizeof(struct mbox_params));
986
987         mbcp->in_count = 1;
988         mbcp->out_count = 2;
989
990         mbcp->mbox_in[0] = MB_CMD_GET_LED_CFG;
991
992         status = ql_mailbox_command(qdev, mbcp);
993         if (status)
994                 return status;
995
996         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
997                 netif_err(qdev, drv, qdev->ndev,
998                           "Failed to get LED Configuration.\n");
999                 status = -EIO;
1000         } else
1001                 qdev->led_config = mbcp->mbox_out[1];
1002
1003         return status;
1004 }
1005
1006 int ql_mb_set_mgmnt_traffic_ctl(struct ql_adapter *qdev, u32 control)
1007 {
1008         struct mbox_params mbc;
1009         struct mbox_params *mbcp = &mbc;
1010         int status;
1011
1012         memset(mbcp, 0, sizeof(struct mbox_params));
1013
1014         mbcp->in_count = 1;
1015         mbcp->out_count = 2;
1016
1017         mbcp->mbox_in[0] = MB_CMD_SET_MGMNT_TFK_CTL;
1018         mbcp->mbox_in[1] = control;
1019
1020         status = ql_mailbox_command(qdev, mbcp);
1021         if (status)
1022                 return status;
1023
1024         if (mbcp->mbox_out[0] == MB_CMD_STS_GOOD)
1025                 return status;
1026
1027         if (mbcp->mbox_out[0] == MB_CMD_STS_INVLD_CMD) {
1028                 netif_err(qdev, drv, qdev->ndev,
1029                           "Command not supported by firmware.\n");
1030                 status = -EINVAL;
1031         } else if (mbcp->mbox_out[0] == MB_CMD_STS_ERR) {
1032                 /* This indicates that the firmware is
1033                  * already in the state we are trying to
1034                  * change it to.
1035                  */
1036                 netif_err(qdev, drv, qdev->ndev,
1037                           "Command parameters make no change.\n");
1038         }
1039         return status;
1040 }
1041
1042 /* Returns a negative error code or the mailbox command status. */
1043 static int ql_mb_get_mgmnt_traffic_ctl(struct ql_adapter *qdev, u32 *control)
1044 {
1045         struct mbox_params mbc;
1046         struct mbox_params *mbcp = &mbc;
1047         int status;
1048
1049         memset(mbcp, 0, sizeof(struct mbox_params));
1050         *control = 0;
1051
1052         mbcp->in_count = 1;
1053         mbcp->out_count = 1;
1054
1055         mbcp->mbox_in[0] = MB_CMD_GET_MGMNT_TFK_CTL;
1056
1057         status = ql_mailbox_command(qdev, mbcp);
1058         if (status)
1059                 return status;
1060
1061         if (mbcp->mbox_out[0] == MB_CMD_STS_GOOD) {
1062                 *control = mbcp->mbox_in[1];
1063                 return status;
1064         }
1065
1066         if (mbcp->mbox_out[0] == MB_CMD_STS_INVLD_CMD) {
1067                 netif_err(qdev, drv, qdev->ndev,
1068                           "Command not supported by firmware.\n");
1069                 status = -EINVAL;
1070         } else if (mbcp->mbox_out[0] == MB_CMD_STS_ERR) {
1071                 netif_err(qdev, drv, qdev->ndev,
1072                           "Failed to get MPI traffic control.\n");
1073                 status = -EIO;
1074         }
1075         return status;
1076 }
1077
1078 int ql_wait_fifo_empty(struct ql_adapter *qdev)
1079 {
1080         int count = 5;
1081         u32 mgmnt_fifo_empty;
1082         u32 nic_fifo_empty;
1083
1084         do {
1085                 nic_fifo_empty = ql_read32(qdev, STS) & STS_NFE;
1086                 ql_mb_get_mgmnt_traffic_ctl(qdev, &mgmnt_fifo_empty);
1087                 mgmnt_fifo_empty &= MB_GET_MPI_TFK_FIFO_EMPTY;
1088                 if (nic_fifo_empty && mgmnt_fifo_empty)
1089                         return 0;
1090                 msleep(100);
1091         } while (count-- > 0);
1092         return -ETIMEDOUT;
1093 }
1094
1095 /* API called in work thread context to set new TX/RX
1096  * maximum frame size values to match MTU.
1097  */
1098 static int ql_set_port_cfg(struct ql_adapter *qdev)
1099 {
1100         int status;
1101
1102         status = ql_mb_set_port_cfg(qdev);
1103         if (status)
1104                 return status;
1105         status = ql_idc_wait(qdev);
1106         return status;
1107 }
1108
1109 /* The following routines are worker threads that process
1110  * events that may sleep waiting for completion.
1111  */
1112
1113 /* This thread gets the maximum TX and RX frame size values
1114  * from the firmware and, if necessary, changes them to match
1115  * the MTU setting.
1116  */
1117 void ql_mpi_port_cfg_work(struct work_struct *work)
1118 {
1119         struct ql_adapter *qdev =
1120             container_of(work, struct ql_adapter, mpi_port_cfg_work.work);
1121         int status;
1122
1123         status = ql_mb_get_port_cfg(qdev);
1124         if (status) {
1125                 netif_err(qdev, drv, qdev->ndev,
1126                           "Bug: Failed to get port config data.\n");
1127                 goto err;
1128         }
1129
1130         if (qdev->link_config & CFG_JUMBO_FRAME_SIZE &&
1131             qdev->max_frame_size == CFG_DEFAULT_MAX_FRAME_SIZE)
1132                 goto end;
1133
1134         qdev->link_config |=    CFG_JUMBO_FRAME_SIZE;
1135         qdev->max_frame_size = CFG_DEFAULT_MAX_FRAME_SIZE;
1136         status = ql_set_port_cfg(qdev);
1137         if (status) {
1138                 netif_err(qdev, drv, qdev->ndev,
1139                           "Bug: Failed to set port config data.\n");
1140                 goto err;
1141         }
1142 end:
1143         clear_bit(QL_PORT_CFG, &qdev->flags);
1144         return;
1145 err:
1146         ql_queue_fw_error(qdev);
1147         goto end;
1148 }
1149
1150 /* Process an inter-device request.  This is issues by
1151  * the firmware in response to another function requesting
1152  * a change to the port. We set a flag to indicate a change
1153  * has been made and then send a mailbox command ACKing
1154  * the change request.
1155  */
1156 void ql_mpi_idc_work(struct work_struct *work)
1157 {
1158         struct ql_adapter *qdev =
1159             container_of(work, struct ql_adapter, mpi_idc_work.work);
1160         int status;
1161         struct mbox_params *mbcp = &qdev->idc_mbc;
1162         u32 aen;
1163         int timeout;
1164
1165         aen = mbcp->mbox_out[1] >> 16;
1166         timeout = (mbcp->mbox_out[1] >> 8) & 0xf;
1167
1168         switch (aen) {
1169         default:
1170                 netif_err(qdev, drv, qdev->ndev,
1171                           "Bug: Unhandled IDC action.\n");
1172                 break;
1173         case MB_CMD_PORT_RESET:
1174         case MB_CMD_STOP_FW:
1175                 ql_link_off(qdev);
1176                 /* Fall through */
1177         case MB_CMD_SET_PORT_CFG:
1178                 /* Signal the resulting link up AEN
1179                  * that the frame routing and mac addr
1180                  * needs to be set.
1181                  * */
1182                 set_bit(QL_CAM_RT_SET, &qdev->flags);
1183                 /* Do ACK if required */
1184                 if (timeout) {
1185                         status = ql_mb_idc_ack(qdev);
1186                         if (status)
1187                                 netif_err(qdev, drv, qdev->ndev,
1188                                           "Bug: No pending IDC!\n");
1189                 } else {
1190                         netif_printk(qdev, drv, KERN_DEBUG, qdev->ndev,
1191                                      "IDC ACK not required\n");
1192                         status = 0; /* success */
1193                 }
1194                 break;
1195
1196         /* These sub-commands issued by another (FCoE)
1197          * function are requesting to do an operation
1198          * on the shared resource (MPI environment).
1199          * We currently don't issue these so we just
1200          * ACK the request.
1201          */
1202         case MB_CMD_IOP_RESTART_MPI:
1203         case MB_CMD_IOP_PREP_LINK_DOWN:
1204                 /* Drop the link, reload the routing
1205                  * table when link comes up.
1206                  */
1207                 ql_link_off(qdev);
1208                 set_bit(QL_CAM_RT_SET, &qdev->flags);
1209                 /* Fall through. */
1210         case MB_CMD_IOP_DVR_START:
1211         case MB_CMD_IOP_FLASH_ACC:
1212         case MB_CMD_IOP_CORE_DUMP_MPI:
1213         case MB_CMD_IOP_PREP_UPDATE_MPI:
1214         case MB_CMD_IOP_COMP_UPDATE_MPI:
1215         case MB_CMD_IOP_NONE:   /*  an IDC without params */
1216                 /* Do ACK if required */
1217                 if (timeout) {
1218                         status = ql_mb_idc_ack(qdev);
1219                         if (status)
1220                                 netif_err(qdev, drv, qdev->ndev,
1221                                           "Bug: No pending IDC!\n");
1222                 } else {
1223                         netif_printk(qdev, drv, KERN_DEBUG, qdev->ndev,
1224                                      "IDC ACK not required\n");
1225                         status = 0; /* success */
1226                 }
1227                 break;
1228         }
1229 }
1230
1231 void ql_mpi_work(struct work_struct *work)
1232 {
1233         struct ql_adapter *qdev =
1234             container_of(work, struct ql_adapter, mpi_work.work);
1235         struct mbox_params mbc;
1236         struct mbox_params *mbcp = &mbc;
1237         int err = 0;
1238
1239         mutex_lock(&qdev->mpi_mutex);
1240         /* Begin polled mode for MPI */
1241         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
1242
1243         while (ql_read32(qdev, STS) & STS_PI) {
1244                 memset(mbcp, 0, sizeof(struct mbox_params));
1245                 mbcp->out_count = 1;
1246                 /* Don't continue if an async event
1247                  * did not complete properly.
1248                  */
1249                 err = ql_mpi_handler(qdev, mbcp);
1250                 if (err)
1251                         break;
1252         }
1253
1254         /* End polled mode for MPI */
1255         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
1256         mutex_unlock(&qdev->mpi_mutex);
1257 }
1258
1259 void ql_mpi_reset_work(struct work_struct *work)
1260 {
1261         struct ql_adapter *qdev =
1262             container_of(work, struct ql_adapter, mpi_reset_work.work);
1263         cancel_delayed_work_sync(&qdev->mpi_work);
1264         cancel_delayed_work_sync(&qdev->mpi_port_cfg_work);
1265         cancel_delayed_work_sync(&qdev->mpi_idc_work);
1266         /* If we're not the dominant NIC function,
1267          * then there is nothing to do.
1268          */
1269         if (!ql_own_firmware(qdev)) {
1270                 netif_err(qdev, drv, qdev->ndev, "Don't own firmware!\n");
1271                 return;
1272         }
1273
1274         if (qdev->mpi_coredump && !ql_core_dump(qdev, qdev->mpi_coredump)) {
1275                 netif_err(qdev, drv, qdev->ndev, "Core is dumped!\n");
1276                 qdev->core_is_dumped = 1;
1277                 queue_delayed_work(qdev->workqueue,
1278                                    &qdev->mpi_core_to_log, 5 * HZ);
1279         }
1280         ql_soft_reset_mpi_risc(qdev);
1281 }
This page took 0.103295 seconds and 4 git commands to generate.