License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / drivers / net / ethernet / qlogic / 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         int status;
94         status = ql_write_mpi_reg(qdev, 0x00001010, 1);
95         return status;
96 }
97
98 /* Determine if we are in charge of the firwmare. If
99  * we are the lower of the 2 NIC pcie functions, or if
100  * we are the higher function and the lower function
101  * is not enabled.
102  */
103 int ql_own_firmware(struct ql_adapter *qdev)
104 {
105         u32 temp;
106
107         /* If we are the lower of the 2 NIC functions
108          * on the chip the we are responsible for
109          * core dump and firmware reset after an error.
110          */
111         if (qdev->func < qdev->alt_func)
112                 return 1;
113
114         /* If we are the higher of the 2 NIC functions
115          * on the chip and the lower function is not
116          * enabled, then we are responsible for
117          * core dump and firmware reset after an error.
118          */
119         temp =  ql_read32(qdev, STS);
120         if (!(temp & (1 << (8 + qdev->alt_func))))
121                 return 1;
122
123         return 0;
124
125 }
126
127 static int ql_get_mb_sts(struct ql_adapter *qdev, struct mbox_params *mbcp)
128 {
129         int i, status;
130
131         status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK);
132         if (status)
133                 return -EBUSY;
134         for (i = 0; i < mbcp->out_count; i++) {
135                 status =
136                     ql_read_mpi_reg(qdev, qdev->mailbox_out + i,
137                                      &mbcp->mbox_out[i]);
138                 if (status) {
139                         netif_err(qdev, drv, qdev->ndev, "Failed mailbox read.\n");
140                         break;
141                 }
142         }
143         ql_sem_unlock(qdev, SEM_PROC_REG_MASK); /* does flush too */
144         return status;
145 }
146
147 /* Wait for a single mailbox command to complete.
148  * Returns zero on success.
149  */
150 static int ql_wait_mbx_cmd_cmplt(struct ql_adapter *qdev)
151 {
152         int count = 100;
153         u32 value;
154
155         do {
156                 value = ql_read32(qdev, STS);
157                 if (value & STS_PI)
158                         return 0;
159                 mdelay(UDELAY_DELAY); /* 100ms */
160         } while (--count);
161         return -ETIMEDOUT;
162 }
163
164 /* Execute a single mailbox command.
165  * Caller must hold PROC_ADDR semaphore.
166  */
167 static int ql_exec_mb_cmd(struct ql_adapter *qdev, struct mbox_params *mbcp)
168 {
169         int i, status;
170
171         /*
172          * Make sure there's nothing pending.
173          * This shouldn't happen.
174          */
175         if (ql_read32(qdev, CSR) & CSR_HRI)
176                 return -EIO;
177
178         status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK);
179         if (status)
180                 return status;
181
182         /*
183          * Fill the outbound mailboxes.
184          */
185         for (i = 0; i < mbcp->in_count; i++) {
186                 status = ql_write_mpi_reg(qdev, qdev->mailbox_in + i,
187                                                 mbcp->mbox_in[i]);
188                 if (status)
189                         goto end;
190         }
191         /*
192          * Wake up the MPI firmware.
193          */
194         ql_write32(qdev, CSR, CSR_CMD_SET_H2R_INT);
195 end:
196         ql_sem_unlock(qdev, SEM_PROC_REG_MASK);
197         return status;
198 }
199
200 /* We are being asked by firmware to accept
201  * a change to the port.  This is only
202  * a change to max frame sizes (Tx/Rx), pause
203  * parameters, or loopback mode. We wake up a worker
204  * to handler processing this since a mailbox command
205  * will need to be sent to ACK the request.
206  */
207 static int ql_idc_req_aen(struct ql_adapter *qdev)
208 {
209         int status;
210         struct mbox_params *mbcp = &qdev->idc_mbc;
211
212         netif_err(qdev, drv, qdev->ndev, "Enter!\n");
213         /* Get the status data and start up a thread to
214          * handle the request.
215          */
216         mbcp = &qdev->idc_mbc;
217         mbcp->out_count = 4;
218         status = ql_get_mb_sts(qdev, mbcp);
219         if (status) {
220                 netif_err(qdev, drv, qdev->ndev,
221                           "Could not read MPI, resetting ASIC!\n");
222                 ql_queue_asic_error(qdev);
223         } else  {
224                 /* Begin polled mode early so
225                  * we don't get another interrupt
226                  * when we leave mpi_worker.
227                  */
228                 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
229                 queue_delayed_work(qdev->workqueue, &qdev->mpi_idc_work, 0);
230         }
231         return status;
232 }
233
234 /* Process an inter-device event completion.
235  * If good, signal the caller's completion.
236  */
237 static int ql_idc_cmplt_aen(struct ql_adapter *qdev)
238 {
239         int status;
240         struct mbox_params *mbcp = &qdev->idc_mbc;
241         mbcp->out_count = 4;
242         status = ql_get_mb_sts(qdev, mbcp);
243         if (status) {
244                 netif_err(qdev, drv, qdev->ndev,
245                           "Could not read MPI, resetting RISC!\n");
246                 ql_queue_fw_error(qdev);
247         } else
248                 /* Wake up the sleeping mpi_idc_work thread that is
249                  * waiting for this event.
250                  */
251                 complete(&qdev->ide_completion);
252
253         return status;
254 }
255
256 static void ql_link_up(struct ql_adapter *qdev, struct mbox_params *mbcp)
257 {
258         int status;
259         mbcp->out_count = 2;
260
261         status = ql_get_mb_sts(qdev, mbcp);
262         if (status) {
263                 netif_err(qdev, drv, qdev->ndev,
264                           "%s: Could not get mailbox status.\n", __func__);
265                 return;
266         }
267
268         qdev->link_status = mbcp->mbox_out[1];
269         netif_err(qdev, drv, qdev->ndev, "Link Up.\n");
270
271         /* If we're coming back from an IDC event
272          * then set up the CAM and frame routing.
273          */
274         if (test_bit(QL_CAM_RT_SET, &qdev->flags)) {
275                 status = ql_cam_route_initialize(qdev);
276                 if (status) {
277                         netif_err(qdev, ifup, qdev->ndev,
278                                   "Failed to init CAM/Routing tables.\n");
279                         return;
280                 } else
281                         clear_bit(QL_CAM_RT_SET, &qdev->flags);
282         }
283
284         /* Queue up a worker to check the frame
285          * size information, and fix it if it's not
286          * to our liking.
287          */
288         if (!test_bit(QL_PORT_CFG, &qdev->flags)) {
289                 netif_err(qdev, drv, qdev->ndev, "Queue Port Config Worker!\n");
290                 set_bit(QL_PORT_CFG, &qdev->flags);
291                 /* Begin polled mode early so
292                  * we don't get another interrupt
293                  * when we leave mpi_worker dpc.
294                  */
295                 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
296                 queue_delayed_work(qdev->workqueue,
297                                 &qdev->mpi_port_cfg_work, 0);
298         }
299
300         ql_link_on(qdev);
301 }
302
303 static void ql_link_down(struct ql_adapter *qdev, struct mbox_params *mbcp)
304 {
305         int status;
306
307         mbcp->out_count = 3;
308
309         status = ql_get_mb_sts(qdev, mbcp);
310         if (status)
311                 netif_err(qdev, drv, qdev->ndev, "Link down AEN broken!\n");
312
313         ql_link_off(qdev);
314 }
315
316 static int ql_sfp_in(struct ql_adapter *qdev, struct mbox_params *mbcp)
317 {
318         int status;
319
320         mbcp->out_count = 5;
321
322         status = ql_get_mb_sts(qdev, mbcp);
323         if (status)
324                 netif_err(qdev, drv, qdev->ndev, "SFP in AEN broken!\n");
325         else
326                 netif_err(qdev, drv, qdev->ndev, "SFP insertion detected.\n");
327
328         return status;
329 }
330
331 static int ql_sfp_out(struct ql_adapter *qdev, struct mbox_params *mbcp)
332 {
333         int status;
334
335         mbcp->out_count = 1;
336
337         status = ql_get_mb_sts(qdev, mbcp);
338         if (status)
339                 netif_err(qdev, drv, qdev->ndev, "SFP out AEN broken!\n");
340         else
341                 netif_err(qdev, drv, qdev->ndev, "SFP removal detected.\n");
342
343         return status;
344 }
345
346 static int ql_aen_lost(struct ql_adapter *qdev, struct mbox_params *mbcp)
347 {
348         int status;
349
350         mbcp->out_count = 6;
351
352         status = ql_get_mb_sts(qdev, mbcp);
353         if (status)
354                 netif_err(qdev, drv, qdev->ndev, "Lost AEN broken!\n");
355         else {
356                 int i;
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
549         /* If we're generating a system error, then there's nothing
550          * to wait for.
551          */
552         if (mbcp->mbox_in[0] == MB_CMD_MAKE_SYS_ERR)
553                 goto end;
554
555         /* Wait for the command to complete. We loop
556          * here because some AEN might arrive while
557          * we're waiting for the mailbox command to
558          * complete. If more than 5 seconds expire we can
559          * assume something is wrong. */
560         count = jiffies + HZ * MAILBOX_TIMEOUT;
561         do {
562                 /* Wait for the interrupt to come in. */
563                 status = ql_wait_mbx_cmd_cmplt(qdev);
564                 if (status)
565                         continue;
566
567                 /* Process the event.  If it's an AEN, it
568                  * will be handled in-line or a worker
569                  * will be spawned. If it's our completion
570                  * we will catch it below.
571                  */
572                 status = ql_mpi_handler(qdev, mbcp);
573                 if (status)
574                         goto end;
575
576                 /* It's either the completion for our mailbox
577                  * command complete or an AEN.  If it's our
578                  * completion then get out.
579                  */
580                 if (((mbcp->mbox_out[0] & 0x0000f000) ==
581                                         MB_CMD_STS_GOOD) ||
582                         ((mbcp->mbox_out[0] & 0x0000f000) ==
583                                         MB_CMD_STS_INTRMDT))
584                         goto done;
585         } while (time_before(jiffies, count));
586
587         netif_err(qdev, drv, qdev->ndev,
588                   "Timed out waiting for mailbox complete.\n");
589         status = -ETIMEDOUT;
590         goto end;
591
592 done:
593
594         /* Now we can clear the interrupt condition
595          * and look at our status.
596          */
597         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
598
599         if (((mbcp->mbox_out[0] & 0x0000f000) !=
600                                         MB_CMD_STS_GOOD) &&
601                 ((mbcp->mbox_out[0] & 0x0000f000) !=
602                                         MB_CMD_STS_INTRMDT)) {
603                 status = -EIO;
604         }
605 end:
606         /* End polled mode for MPI */
607         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
608         mutex_unlock(&qdev->mpi_mutex);
609         return status;
610 }
611
612 /* Get MPI firmware version. This will be used for
613  * driver banner and for ethtool info.
614  * Returns zero on success.
615  */
616 int ql_mb_about_fw(struct ql_adapter *qdev)
617 {
618         struct mbox_params mbc;
619         struct mbox_params *mbcp = &mbc;
620         int status = 0;
621
622         memset(mbcp, 0, sizeof(struct mbox_params));
623
624         mbcp->in_count = 1;
625         mbcp->out_count = 3;
626
627         mbcp->mbox_in[0] = MB_CMD_ABOUT_FW;
628
629         status = ql_mailbox_command(qdev, mbcp);
630         if (status)
631                 return status;
632
633         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
634                 netif_err(qdev, drv, qdev->ndev,
635                           "Failed about firmware command\n");
636                 status = -EIO;
637         }
638
639         /* Store the firmware version */
640         qdev->fw_rev_id = mbcp->mbox_out[1];
641
642         return status;
643 }
644
645 /* Get functional state for MPI firmware.
646  * Returns zero on success.
647  */
648 int ql_mb_get_fw_state(struct ql_adapter *qdev)
649 {
650         struct mbox_params mbc;
651         struct mbox_params *mbcp = &mbc;
652         int status = 0;
653
654         memset(mbcp, 0, sizeof(struct mbox_params));
655
656         mbcp->in_count = 1;
657         mbcp->out_count = 2;
658
659         mbcp->mbox_in[0] = MB_CMD_GET_FW_STATE;
660
661         status = ql_mailbox_command(qdev, mbcp);
662         if (status)
663                 return status;
664
665         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
666                 netif_err(qdev, drv, qdev->ndev,
667                           "Failed Get Firmware State.\n");
668                 status = -EIO;
669         }
670
671         /* If bit zero is set in mbx 1 then the firmware is
672          * running, but not initialized.  This should never
673          * happen.
674          */
675         if (mbcp->mbox_out[1] & 1) {
676                 netif_err(qdev, drv, qdev->ndev,
677                           "Firmware waiting for initialization.\n");
678                 status = -EIO;
679         }
680
681         return status;
682 }
683
684 /* Send and ACK mailbox command to the firmware to
685  * let it continue with the change.
686  */
687 static int ql_mb_idc_ack(struct ql_adapter *qdev)
688 {
689         struct mbox_params mbc;
690         struct mbox_params *mbcp = &mbc;
691         int status = 0;
692
693         memset(mbcp, 0, sizeof(struct mbox_params));
694
695         mbcp->in_count = 5;
696         mbcp->out_count = 1;
697
698         mbcp->mbox_in[0] = MB_CMD_IDC_ACK;
699         mbcp->mbox_in[1] = qdev->idc_mbc.mbox_out[1];
700         mbcp->mbox_in[2] = qdev->idc_mbc.mbox_out[2];
701         mbcp->mbox_in[3] = qdev->idc_mbc.mbox_out[3];
702         mbcp->mbox_in[4] = qdev->idc_mbc.mbox_out[4];
703
704         status = ql_mailbox_command(qdev, mbcp);
705         if (status)
706                 return status;
707
708         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
709                 netif_err(qdev, drv, qdev->ndev, "Failed IDC ACK send.\n");
710                 status = -EIO;
711         }
712         return status;
713 }
714
715 /* Get link settings and maximum frame size settings
716  * for the current port.
717  * Most likely will block.
718  */
719 int ql_mb_set_port_cfg(struct ql_adapter *qdev)
720 {
721         struct mbox_params mbc;
722         struct mbox_params *mbcp = &mbc;
723         int status = 0;
724
725         memset(mbcp, 0, sizeof(struct mbox_params));
726
727         mbcp->in_count = 3;
728         mbcp->out_count = 1;
729
730         mbcp->mbox_in[0] = MB_CMD_SET_PORT_CFG;
731         mbcp->mbox_in[1] = qdev->link_config;
732         mbcp->mbox_in[2] = qdev->max_frame_size;
733
734
735         status = ql_mailbox_command(qdev, mbcp);
736         if (status)
737                 return status;
738
739         if (mbcp->mbox_out[0] == MB_CMD_STS_INTRMDT) {
740                 netif_err(qdev, drv, qdev->ndev,
741                           "Port Config sent, wait for IDC.\n");
742         } else  if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
743                 netif_err(qdev, drv, qdev->ndev,
744                           "Failed Set Port Configuration.\n");
745                 status = -EIO;
746         }
747         return status;
748 }
749
750 static int ql_mb_dump_ram(struct ql_adapter *qdev, u64 req_dma, u32 addr,
751         u32 size)
752 {
753         int status = 0;
754         struct mbox_params mbc;
755         struct mbox_params *mbcp = &mbc;
756
757         memset(mbcp, 0, sizeof(struct mbox_params));
758
759         mbcp->in_count = 9;
760         mbcp->out_count = 1;
761
762         mbcp->mbox_in[0] = MB_CMD_DUMP_RISC_RAM;
763         mbcp->mbox_in[1] = LSW(addr);
764         mbcp->mbox_in[2] = MSW(req_dma);
765         mbcp->mbox_in[3] = LSW(req_dma);
766         mbcp->mbox_in[4] = MSW(size);
767         mbcp->mbox_in[5] = LSW(size);
768         mbcp->mbox_in[6] = MSW(MSD(req_dma));
769         mbcp->mbox_in[7] = LSW(MSD(req_dma));
770         mbcp->mbox_in[8] = MSW(addr);
771
772
773         status = ql_mailbox_command(qdev, mbcp);
774         if (status)
775                 return status;
776
777         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
778                 netif_err(qdev, drv, qdev->ndev, "Failed to dump risc RAM.\n");
779                 status = -EIO;
780         }
781         return status;
782 }
783
784 /* Issue a mailbox command to dump RISC RAM. */
785 int ql_dump_risc_ram_area(struct ql_adapter *qdev, void *buf,
786                 u32 ram_addr, int word_count)
787 {
788         int status;
789         char *my_buf;
790         dma_addr_t buf_dma;
791
792         my_buf = pci_alloc_consistent(qdev->pdev, word_count * sizeof(u32),
793                                         &buf_dma);
794         if (!my_buf)
795                 return -EIO;
796
797         status = ql_mb_dump_ram(qdev, buf_dma, ram_addr, word_count);
798         if (!status)
799                 memcpy(buf, my_buf, word_count * sizeof(u32));
800
801         pci_free_consistent(qdev->pdev, word_count * sizeof(u32), my_buf,
802                                 buf_dma);
803         return status;
804 }
805
806 /* Get link settings and maximum frame size settings
807  * for the current port.
808  * Most likely will block.
809  */
810 int ql_mb_get_port_cfg(struct ql_adapter *qdev)
811 {
812         struct mbox_params mbc;
813         struct mbox_params *mbcp = &mbc;
814         int status = 0;
815
816         memset(mbcp, 0, sizeof(struct mbox_params));
817
818         mbcp->in_count = 1;
819         mbcp->out_count = 3;
820
821         mbcp->mbox_in[0] = MB_CMD_GET_PORT_CFG;
822
823         status = ql_mailbox_command(qdev, mbcp);
824         if (status)
825                 return status;
826
827         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
828                 netif_err(qdev, drv, qdev->ndev,
829                           "Failed Get Port Configuration.\n");
830                 status = -EIO;
831         } else  {
832                 netif_printk(qdev, drv, KERN_DEBUG, qdev->ndev,
833                              "Passed Get Port Configuration.\n");
834                 qdev->link_config = mbcp->mbox_out[1];
835                 qdev->max_frame_size = mbcp->mbox_out[2];
836         }
837         return status;
838 }
839
840 int ql_mb_wol_mode(struct ql_adapter *qdev, u32 wol)
841 {
842         struct mbox_params mbc;
843         struct mbox_params *mbcp = &mbc;
844         int status;
845
846         memset(mbcp, 0, sizeof(struct mbox_params));
847
848         mbcp->in_count = 2;
849         mbcp->out_count = 1;
850
851         mbcp->mbox_in[0] = MB_CMD_SET_WOL_MODE;
852         mbcp->mbox_in[1] = wol;
853
854
855         status = ql_mailbox_command(qdev, mbcp);
856         if (status)
857                 return status;
858
859         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
860                 netif_err(qdev, drv, qdev->ndev, "Failed to set WOL mode.\n");
861                 status = -EIO;
862         }
863         return status;
864 }
865
866 int ql_mb_wol_set_magic(struct ql_adapter *qdev, u32 enable_wol)
867 {
868         struct mbox_params mbc;
869         struct mbox_params *mbcp = &mbc;
870         int status;
871         u8 *addr = qdev->ndev->dev_addr;
872
873         memset(mbcp, 0, sizeof(struct mbox_params));
874
875         mbcp->in_count = 8;
876         mbcp->out_count = 1;
877
878         mbcp->mbox_in[0] = MB_CMD_SET_WOL_MAGIC;
879         if (enable_wol) {
880                 mbcp->mbox_in[1] = (u32)addr[0];
881                 mbcp->mbox_in[2] = (u32)addr[1];
882                 mbcp->mbox_in[3] = (u32)addr[2];
883                 mbcp->mbox_in[4] = (u32)addr[3];
884                 mbcp->mbox_in[5] = (u32)addr[4];
885                 mbcp->mbox_in[6] = (u32)addr[5];
886                 mbcp->mbox_in[7] = 0;
887         } else {
888                 mbcp->mbox_in[1] = 0;
889                 mbcp->mbox_in[2] = 1;
890                 mbcp->mbox_in[3] = 1;
891                 mbcp->mbox_in[4] = 1;
892                 mbcp->mbox_in[5] = 1;
893                 mbcp->mbox_in[6] = 1;
894                 mbcp->mbox_in[7] = 0;
895         }
896
897         status = ql_mailbox_command(qdev, mbcp);
898         if (status)
899                 return status;
900
901         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
902                 netif_err(qdev, drv, qdev->ndev, "Failed to set WOL mode.\n");
903                 status = -EIO;
904         }
905         return status;
906 }
907
908 /* IDC - Inter Device Communication...
909  * Some firmware commands require consent of adjacent FCOE
910  * function.  This function waits for the OK, or a
911  * counter-request for a little more time.i
912  * The firmware will complete the request if the other
913  * function doesn't respond.
914  */
915 static int ql_idc_wait(struct ql_adapter *qdev)
916 {
917         int status = -ETIMEDOUT;
918         long wait_time = 1 * HZ;
919         struct mbox_params *mbcp = &qdev->idc_mbc;
920         do {
921                 /* Wait here for the command to complete
922                  * via the IDC process.
923                  */
924                 wait_time =
925                         wait_for_completion_timeout(&qdev->ide_completion,
926                                                         wait_time);
927                 if (!wait_time) {
928                         netif_err(qdev, drv, qdev->ndev, "IDC Timeout.\n");
929                         break;
930                 }
931                 /* Now examine the response from the IDC process.
932                  * We might have a good completion or a request for
933                  * more wait time.
934                  */
935                 if (mbcp->mbox_out[0] == AEN_IDC_EXT) {
936                         netif_err(qdev, drv, qdev->ndev,
937                                   "IDC Time Extension from function.\n");
938                         wait_time += (mbcp->mbox_out[1] >> 8) & 0x0000000f;
939                 } else if (mbcp->mbox_out[0] == AEN_IDC_CMPLT) {
940                         netif_err(qdev, drv, qdev->ndev, "IDC Success.\n");
941                         status = 0;
942                         break;
943                 } else {
944                         netif_err(qdev, drv, qdev->ndev,
945                                   "IDC: Invalid State 0x%.04x.\n",
946                                   mbcp->mbox_out[0]);
947                         status = -EIO;
948                         break;
949                 }
950         } while (wait_time);
951
952         return status;
953 }
954
955 int ql_mb_set_led_cfg(struct ql_adapter *qdev, u32 led_config)
956 {
957         struct mbox_params mbc;
958         struct mbox_params *mbcp = &mbc;
959         int status;
960
961         memset(mbcp, 0, sizeof(struct mbox_params));
962
963         mbcp->in_count = 2;
964         mbcp->out_count = 1;
965
966         mbcp->mbox_in[0] = MB_CMD_SET_LED_CFG;
967         mbcp->mbox_in[1] = led_config;
968
969
970         status = ql_mailbox_command(qdev, mbcp);
971         if (status)
972                 return status;
973
974         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
975                 netif_err(qdev, drv, qdev->ndev,
976                           "Failed to set LED Configuration.\n");
977                 status = -EIO;
978         }
979
980         return status;
981 }
982
983 int ql_mb_get_led_cfg(struct ql_adapter *qdev)
984 {
985         struct mbox_params mbc;
986         struct mbox_params *mbcp = &mbc;
987         int status;
988
989         memset(mbcp, 0, sizeof(struct mbox_params));
990
991         mbcp->in_count = 1;
992         mbcp->out_count = 2;
993
994         mbcp->mbox_in[0] = MB_CMD_GET_LED_CFG;
995
996         status = ql_mailbox_command(qdev, mbcp);
997         if (status)
998                 return status;
999
1000         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
1001                 netif_err(qdev, drv, qdev->ndev,
1002                           "Failed to get LED Configuration.\n");
1003                 status = -EIO;
1004         } else
1005                 qdev->led_config = mbcp->mbox_out[1];
1006
1007         return status;
1008 }
1009
1010 int ql_mb_set_mgmnt_traffic_ctl(struct ql_adapter *qdev, u32 control)
1011 {
1012         struct mbox_params mbc;
1013         struct mbox_params *mbcp = &mbc;
1014         int status;
1015
1016         memset(mbcp, 0, sizeof(struct mbox_params));
1017
1018         mbcp->in_count = 1;
1019         mbcp->out_count = 2;
1020
1021         mbcp->mbox_in[0] = MB_CMD_SET_MGMNT_TFK_CTL;
1022         mbcp->mbox_in[1] = control;
1023
1024         status = ql_mailbox_command(qdev, mbcp);
1025         if (status)
1026                 return status;
1027
1028         if (mbcp->mbox_out[0] == MB_CMD_STS_GOOD)
1029                 return status;
1030
1031         if (mbcp->mbox_out[0] == MB_CMD_STS_INVLD_CMD) {
1032                 netif_err(qdev, drv, qdev->ndev,
1033                           "Command not supported by firmware.\n");
1034                 status = -EINVAL;
1035         } else if (mbcp->mbox_out[0] == MB_CMD_STS_ERR) {
1036                 /* This indicates that the firmware is
1037                  * already in the state we are trying to
1038                  * change it to.
1039                  */
1040                 netif_err(qdev, drv, qdev->ndev,
1041                           "Command parameters make no change.\n");
1042         }
1043         return status;
1044 }
1045
1046 /* Returns a negative error code or the mailbox command status. */
1047 static int ql_mb_get_mgmnt_traffic_ctl(struct ql_adapter *qdev, u32 *control)
1048 {
1049         struct mbox_params mbc;
1050         struct mbox_params *mbcp = &mbc;
1051         int status;
1052
1053         memset(mbcp, 0, sizeof(struct mbox_params));
1054         *control = 0;
1055
1056         mbcp->in_count = 1;
1057         mbcp->out_count = 1;
1058
1059         mbcp->mbox_in[0] = MB_CMD_GET_MGMNT_TFK_CTL;
1060
1061         status = ql_mailbox_command(qdev, mbcp);
1062         if (status)
1063                 return status;
1064
1065         if (mbcp->mbox_out[0] == MB_CMD_STS_GOOD) {
1066                 *control = mbcp->mbox_in[1];
1067                 return status;
1068         }
1069
1070         if (mbcp->mbox_out[0] == MB_CMD_STS_INVLD_CMD) {
1071                 netif_err(qdev, drv, qdev->ndev,
1072                           "Command not supported by firmware.\n");
1073                 status = -EINVAL;
1074         } else if (mbcp->mbox_out[0] == MB_CMD_STS_ERR) {
1075                 netif_err(qdev, drv, qdev->ndev,
1076                           "Failed to get MPI traffic control.\n");
1077                 status = -EIO;
1078         }
1079         return status;
1080 }
1081
1082 int ql_wait_fifo_empty(struct ql_adapter *qdev)
1083 {
1084         int count = 5;
1085         u32 mgmnt_fifo_empty;
1086         u32 nic_fifo_empty;
1087
1088         do {
1089                 nic_fifo_empty = ql_read32(qdev, STS) & STS_NFE;
1090                 ql_mb_get_mgmnt_traffic_ctl(qdev, &mgmnt_fifo_empty);
1091                 mgmnt_fifo_empty &= MB_GET_MPI_TFK_FIFO_EMPTY;
1092                 if (nic_fifo_empty && mgmnt_fifo_empty)
1093                         return 0;
1094                 msleep(100);
1095         } while (count-- > 0);
1096         return -ETIMEDOUT;
1097 }
1098
1099 /* API called in work thread context to set new TX/RX
1100  * maximum frame size values to match MTU.
1101  */
1102 static int ql_set_port_cfg(struct ql_adapter *qdev)
1103 {
1104         int status;
1105         status = ql_mb_set_port_cfg(qdev);
1106         if (status)
1107                 return status;
1108         status = ql_idc_wait(qdev);
1109         return status;
1110 }
1111
1112 /* The following routines are worker threads that process
1113  * events that may sleep waiting for completion.
1114  */
1115
1116 /* This thread gets the maximum TX and RX frame size values
1117  * from the firmware and, if necessary, changes them to match
1118  * the MTU setting.
1119  */
1120 void ql_mpi_port_cfg_work(struct work_struct *work)
1121 {
1122         struct ql_adapter *qdev =
1123             container_of(work, struct ql_adapter, mpi_port_cfg_work.work);
1124         int status;
1125
1126         status = ql_mb_get_port_cfg(qdev);
1127         if (status) {
1128                 netif_err(qdev, drv, qdev->ndev,
1129                           "Bug: Failed to get port config data.\n");
1130                 goto err;
1131         }
1132
1133         if (qdev->link_config & CFG_JUMBO_FRAME_SIZE &&
1134                         qdev->max_frame_size ==
1135                         CFG_DEFAULT_MAX_FRAME_SIZE)
1136                 goto end;
1137
1138         qdev->link_config |=    CFG_JUMBO_FRAME_SIZE;
1139         qdev->max_frame_size = CFG_DEFAULT_MAX_FRAME_SIZE;
1140         status = ql_set_port_cfg(qdev);
1141         if (status) {
1142                 netif_err(qdev, drv, qdev->ndev,
1143                           "Bug: Failed to set port config data.\n");
1144                 goto err;
1145         }
1146 end:
1147         clear_bit(QL_PORT_CFG, &qdev->flags);
1148         return;
1149 err:
1150         ql_queue_fw_error(qdev);
1151         goto end;
1152 }
1153
1154 /* Process an inter-device request.  This is issues by
1155  * the firmware in response to another function requesting
1156  * a change to the port. We set a flag to indicate a change
1157  * has been made and then send a mailbox command ACKing
1158  * the change request.
1159  */
1160 void ql_mpi_idc_work(struct work_struct *work)
1161 {
1162         struct ql_adapter *qdev =
1163             container_of(work, struct ql_adapter, mpi_idc_work.work);
1164         int status;
1165         struct mbox_params *mbcp = &qdev->idc_mbc;
1166         u32 aen;
1167         int timeout;
1168
1169         aen = mbcp->mbox_out[1] >> 16;
1170         timeout = (mbcp->mbox_out[1] >> 8) & 0xf;
1171
1172         switch (aen) {
1173         default:
1174                 netif_err(qdev, drv, qdev->ndev,
1175                           "Bug: Unhandled IDC action.\n");
1176                 break;
1177         case MB_CMD_PORT_RESET:
1178         case MB_CMD_STOP_FW:
1179                 ql_link_off(qdev);
1180         case MB_CMD_SET_PORT_CFG:
1181                 /* Signal the resulting link up AEN
1182                  * that the frame routing and mac addr
1183                  * needs to be set.
1184                  * */
1185                 set_bit(QL_CAM_RT_SET, &qdev->flags);
1186                 /* Do ACK if required */
1187                 if (timeout) {
1188                         status = ql_mb_idc_ack(qdev);
1189                         if (status)
1190                                 netif_err(qdev, drv, qdev->ndev,
1191                                           "Bug: No pending IDC!\n");
1192                 } else {
1193                         netif_printk(qdev, drv, KERN_DEBUG, qdev->ndev,
1194                                      "IDC ACK not required\n");
1195                         status = 0; /* success */
1196                 }
1197                 break;
1198
1199         /* These sub-commands issued by another (FCoE)
1200          * function are requesting to do an operation
1201          * on the shared resource (MPI environment).
1202          * We currently don't issue these so we just
1203          * ACK the request.
1204          */
1205         case MB_CMD_IOP_RESTART_MPI:
1206         case MB_CMD_IOP_PREP_LINK_DOWN:
1207                 /* Drop the link, reload the routing
1208                  * table when link comes up.
1209                  */
1210                 ql_link_off(qdev);
1211                 set_bit(QL_CAM_RT_SET, &qdev->flags);
1212                 /* Fall through. */
1213         case MB_CMD_IOP_DVR_START:
1214         case MB_CMD_IOP_FLASH_ACC:
1215         case MB_CMD_IOP_CORE_DUMP_MPI:
1216         case MB_CMD_IOP_PREP_UPDATE_MPI:
1217         case MB_CMD_IOP_COMP_UPDATE_MPI:
1218         case MB_CMD_IOP_NONE:   /*  an IDC without params */
1219                 /* Do ACK if required */
1220                 if (timeout) {
1221                         status = ql_mb_idc_ack(qdev);
1222                         if (status)
1223                                 netif_err(qdev, drv, qdev->ndev,
1224                                           "Bug: No pending IDC!\n");
1225                 } else {
1226                         netif_printk(qdev, drv, KERN_DEBUG, qdev->ndev,
1227                                      "IDC ACK not required\n");
1228                         status = 0; /* success */
1229                 }
1230                 break;
1231         }
1232 }
1233
1234 void ql_mpi_work(struct work_struct *work)
1235 {
1236         struct ql_adapter *qdev =
1237             container_of(work, struct ql_adapter, mpi_work.work);
1238         struct mbox_params mbc;
1239         struct mbox_params *mbcp = &mbc;
1240         int err = 0;
1241
1242         mutex_lock(&qdev->mpi_mutex);
1243         /* Begin polled mode for MPI */
1244         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
1245
1246         while (ql_read32(qdev, STS) & STS_PI) {
1247                 memset(mbcp, 0, sizeof(struct mbox_params));
1248                 mbcp->out_count = 1;
1249                 /* Don't continue if an async event
1250                  * did not complete properly.
1251                  */
1252                 err = ql_mpi_handler(qdev, mbcp);
1253                 if (err)
1254                         break;
1255         }
1256
1257         /* End polled mode for MPI */
1258         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
1259         mutex_unlock(&qdev->mpi_mutex);
1260         ql_enable_completion_interrupt(qdev, 0);
1261 }
1262
1263 void ql_mpi_reset_work(struct work_struct *work)
1264 {
1265         struct ql_adapter *qdev =
1266             container_of(work, struct ql_adapter, mpi_reset_work.work);
1267         cancel_delayed_work_sync(&qdev->mpi_work);
1268         cancel_delayed_work_sync(&qdev->mpi_port_cfg_work);
1269         cancel_delayed_work_sync(&qdev->mpi_idc_work);
1270         /* If we're not the dominant NIC function,
1271          * then there is nothing to do.
1272          */
1273         if (!ql_own_firmware(qdev)) {
1274                 netif_err(qdev, drv, qdev->ndev, "Don't own firmware!\n");
1275                 return;
1276         }
1277
1278         if (qdev->mpi_coredump && !ql_core_dump(qdev, qdev->mpi_coredump)) {
1279                 netif_err(qdev, drv, qdev->ndev, "Core is dumped!\n");
1280                 qdev->core_is_dumped = 1;
1281                 queue_delayed_work(qdev->workqueue,
1282                         &qdev->mpi_core_to_log, 5 * HZ);
1283         }
1284         ql_soft_reset_mpi_risc(qdev);
1285 }