qlge: Store firmware revision as early as possible.
[pandora-kernel.git] / drivers / net / qlge / qlge_mpi.c
1 #include "qlge.h"
2
3 static void ql_display_mb_sts(struct ql_adapter *qdev,
4                                                 struct mbox_params *mbcp)
5 {
6         int i;
7         static char *err_sts[] = {
8                 "Command Complete",
9                 "Command Not Supported",
10                 "Host Interface Error",
11                 "Checksum Error",
12                 "Unused Completion Status",
13                 "Test Failed",
14                 "Command Parameter Error"};
15
16         QPRINTK(qdev, DRV, DEBUG, "%s.\n",
17                 err_sts[mbcp->mbox_out[0] & 0x0000000f]);
18         for (i = 0; i < mbcp->out_count; i++)
19                 QPRINTK(qdev, DRV, DEBUG, "mbox_out[%d] = 0x%.08x.\n",
20                                 i, mbcp->mbox_out[i]);
21 }
22
23 int ql_read_mpi_reg(struct ql_adapter *qdev, u32 reg, u32 *data)
24 {
25         int status;
26         /* wait for reg to come ready */
27         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
28         if (status)
29                 goto exit;
30         /* set up for reg read */
31         ql_write32(qdev, PROC_ADDR, reg | PROC_ADDR_R);
32         /* wait for reg to come ready */
33         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
34         if (status)
35                 goto exit;
36         /* get the data */
37         *data = ql_read32(qdev, PROC_DATA);
38 exit:
39         return status;
40 }
41
42 int ql_write_mpi_reg(struct ql_adapter *qdev, u32 reg, u32 data)
43 {
44         int status = 0;
45         /* wait for reg to come ready */
46         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
47         if (status)
48                 goto exit;
49         /* write the data to the data reg */
50         ql_write32(qdev, PROC_DATA, data);
51         /* trigger the write */
52         ql_write32(qdev, PROC_ADDR, reg);
53         /* wait for reg to come ready */
54         status = ql_wait_reg_rdy(qdev, PROC_ADDR, PROC_ADDR_RDY, PROC_ADDR_ERR);
55         if (status)
56                 goto exit;
57 exit:
58         return status;
59 }
60
61 int ql_soft_reset_mpi_risc(struct ql_adapter *qdev)
62 {
63         int status;
64         status = ql_write_mpi_reg(qdev, 0x00001010, 1);
65         return status;
66 }
67
68 static int ql_get_mb_sts(struct ql_adapter *qdev, struct mbox_params *mbcp)
69 {
70         int i, status;
71
72         status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK);
73         if (status)
74                 return -EBUSY;
75         for (i = 0; i < mbcp->out_count; i++) {
76                 status =
77                     ql_read_mpi_reg(qdev, qdev->mailbox_out + i,
78                                      &mbcp->mbox_out[i]);
79                 if (status) {
80                         QPRINTK(qdev, DRV, ERR, "Failed mailbox read.\n");
81                         break;
82                 }
83         }
84         ql_sem_unlock(qdev, SEM_PROC_REG_MASK); /* does flush too */
85         return status;
86 }
87
88 /* Wait for a single mailbox command to complete.
89  * Returns zero on success.
90  */
91 static int ql_wait_mbx_cmd_cmplt(struct ql_adapter *qdev)
92 {
93         int count = 100;
94         u32 value;
95
96         do {
97                 value = ql_read32(qdev, STS);
98                 if (value & STS_PI)
99                         return 0;
100                 mdelay(UDELAY_DELAY); /* 100ms */
101         } while (--count);
102         return -ETIMEDOUT;
103 }
104
105 /* Execute a single mailbox command.
106  * Caller must hold PROC_ADDR semaphore.
107  */
108 static int ql_exec_mb_cmd(struct ql_adapter *qdev, struct mbox_params *mbcp)
109 {
110         int i, status;
111
112         /*
113          * Make sure there's nothing pending.
114          * This shouldn't happen.
115          */
116         if (ql_read32(qdev, CSR) & CSR_HRI)
117                 return -EIO;
118
119         status = ql_sem_spinlock(qdev, SEM_PROC_REG_MASK);
120         if (status)
121                 return status;
122
123         /*
124          * Fill the outbound mailboxes.
125          */
126         for (i = 0; i < mbcp->in_count; i++) {
127                 status = ql_write_mpi_reg(qdev, qdev->mailbox_in + i,
128                                                 mbcp->mbox_in[i]);
129                 if (status)
130                         goto end;
131         }
132         /*
133          * Wake up the MPI firmware.
134          */
135         ql_write32(qdev, CSR, CSR_CMD_SET_H2R_INT);
136 end:
137         ql_sem_unlock(qdev, SEM_PROC_REG_MASK);
138         return status;
139 }
140
141 /* We are being asked by firmware to accept
142  * a change to the port.  This is only
143  * a change to max frame sizes (Tx/Rx), pause
144  * parameters, or loopback mode. We wake up a worker
145  * to handler processing this since a mailbox command
146  * will need to be sent to ACK the request.
147  */
148 static int ql_idc_req_aen(struct ql_adapter *qdev)
149 {
150         int status;
151         struct mbox_params *mbcp = &qdev->idc_mbc;
152
153         QPRINTK(qdev, DRV, ERR, "Enter!\n");
154         /* Get the status data and start up a thread to
155          * handle the request.
156          */
157         mbcp = &qdev->idc_mbc;
158         mbcp->out_count = 4;
159         status = ql_get_mb_sts(qdev, mbcp);
160         if (status) {
161                 QPRINTK(qdev, DRV, ERR,
162                         "Could not read MPI, resetting ASIC!\n");
163                 ql_queue_asic_error(qdev);
164         } else  {
165                 /* Begin polled mode early so
166                  * we don't get another interrupt
167                  * when we leave mpi_worker.
168                  */
169                 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
170                 queue_delayed_work(qdev->workqueue, &qdev->mpi_idc_work, 0);
171         }
172         return status;
173 }
174
175 /* Process an inter-device event completion.
176  * If good, signal the caller's completion.
177  */
178 static int ql_idc_cmplt_aen(struct ql_adapter *qdev)
179 {
180         int status;
181         struct mbox_params *mbcp = &qdev->idc_mbc;
182         mbcp->out_count = 4;
183         status = ql_get_mb_sts(qdev, mbcp);
184         if (status) {
185                 QPRINTK(qdev, DRV, ERR,
186                         "Could not read MPI, resetting RISC!\n");
187                 ql_queue_fw_error(qdev);
188         } else
189                 /* Wake up the sleeping mpi_idc_work thread that is
190                  * waiting for this event.
191                  */
192                 complete(&qdev->ide_completion);
193
194         return status;
195 }
196
197 static void ql_link_up(struct ql_adapter *qdev, struct mbox_params *mbcp)
198 {
199         int status;
200         mbcp->out_count = 2;
201
202         status = ql_get_mb_sts(qdev, mbcp);
203         if (status) {
204                 QPRINTK(qdev, DRV, ERR,
205                         "%s: Could not get mailbox status.\n", __func__);
206                 return;
207         }
208
209         qdev->link_status = mbcp->mbox_out[1];
210         QPRINTK(qdev, DRV, ERR, "Link Up.\n");
211
212         /* If we're coming back from an IDC event
213          * then set up the CAM and frame routing.
214          */
215         if (test_bit(QL_CAM_RT_SET, &qdev->flags)) {
216                 status = ql_cam_route_initialize(qdev);
217                 if (status) {
218                         QPRINTK(qdev, IFUP, ERR,
219                         "Failed to init CAM/Routing tables.\n");
220                         return;
221                 } else
222                         clear_bit(QL_CAM_RT_SET, &qdev->flags);
223         }
224
225         /* Queue up a worker to check the frame
226          * size information, and fix it if it's not
227          * to our liking.
228          */
229         if (!test_bit(QL_PORT_CFG, &qdev->flags)) {
230                 QPRINTK(qdev, DRV, ERR, "Queue Port Config Worker!\n");
231                 set_bit(QL_PORT_CFG, &qdev->flags);
232                 /* Begin polled mode early so
233                  * we don't get another interrupt
234                  * when we leave mpi_worker dpc.
235                  */
236                 ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
237                 queue_delayed_work(qdev->workqueue,
238                                 &qdev->mpi_port_cfg_work, 0);
239         }
240
241         ql_link_on(qdev);
242 }
243
244 static void ql_link_down(struct ql_adapter *qdev, struct mbox_params *mbcp)
245 {
246         int status;
247
248         mbcp->out_count = 3;
249
250         status = ql_get_mb_sts(qdev, mbcp);
251         if (status)
252                 QPRINTK(qdev, DRV, ERR, "Link down AEN broken!\n");
253
254         ql_link_off(qdev);
255 }
256
257 static int ql_sfp_in(struct ql_adapter *qdev, struct mbox_params *mbcp)
258 {
259         int status;
260
261         mbcp->out_count = 5;
262
263         status = ql_get_mb_sts(qdev, mbcp);
264         if (status)
265                 QPRINTK(qdev, DRV, ERR, "SFP in AEN broken!\n");
266         else
267                 QPRINTK(qdev, DRV, ERR, "SFP insertion detected.\n");
268
269         return status;
270 }
271
272 static int ql_sfp_out(struct ql_adapter *qdev, struct mbox_params *mbcp)
273 {
274         int status;
275
276         mbcp->out_count = 1;
277
278         status = ql_get_mb_sts(qdev, mbcp);
279         if (status)
280                 QPRINTK(qdev, DRV, ERR, "SFP out AEN broken!\n");
281         else
282                 QPRINTK(qdev, DRV, ERR, "SFP removal detected.\n");
283
284         return status;
285 }
286
287 static int ql_aen_lost(struct ql_adapter *qdev, struct mbox_params *mbcp)
288 {
289         int status;
290
291         mbcp->out_count = 6;
292
293         status = ql_get_mb_sts(qdev, mbcp);
294         if (status)
295                 QPRINTK(qdev, DRV, ERR, "Lost AEN broken!\n");
296         else {
297                 int i;
298                 QPRINTK(qdev, DRV, ERR, "Lost AEN detected.\n");
299                 for (i = 0; i < mbcp->out_count; i++)
300                         QPRINTK(qdev, DRV, ERR, "mbox_out[%d] = 0x%.08x.\n",
301                                         i, mbcp->mbox_out[i]);
302
303         }
304
305         return status;
306 }
307
308 static void ql_init_fw_done(struct ql_adapter *qdev, struct mbox_params *mbcp)
309 {
310         int status;
311
312         mbcp->out_count = 2;
313
314         status = ql_get_mb_sts(qdev, mbcp);
315         if (status) {
316                 QPRINTK(qdev, DRV, ERR, "Firmware did not initialize!\n");
317         } else {
318                 QPRINTK(qdev, DRV, ERR, "Firmware Revision  = 0x%.08x.\n",
319                         mbcp->mbox_out[1]);
320                 qdev->fw_rev_id = mbcp->mbox_out[1];
321                 status = ql_cam_route_initialize(qdev);
322                 if (status)
323                         QPRINTK(qdev, IFUP, ERR,
324                                 "Failed to init CAM/Routing tables.\n");
325         }
326 }
327
328 /* Process an async event and clear it unless it's an
329  * error condition.
330  *  This can get called iteratively from the mpi_work thread
331  *  when events arrive via an interrupt.
332  *  It also gets called when a mailbox command is polling for
333  *  it's completion. */
334 static int ql_mpi_handler(struct ql_adapter *qdev, struct mbox_params *mbcp)
335 {
336         int status;
337         int orig_count = mbcp->out_count;
338
339         /* Just get mailbox zero for now. */
340         mbcp->out_count = 1;
341         status = ql_get_mb_sts(qdev, mbcp);
342         if (status) {
343                 QPRINTK(qdev, DRV, ERR,
344                         "Could not read MPI, resetting ASIC!\n");
345                 ql_queue_asic_error(qdev);
346                 goto end;
347         }
348
349         switch (mbcp->mbox_out[0]) {
350
351         /* This case is only active when we arrive here
352          * as a result of issuing a mailbox command to
353          * the firmware.
354          */
355         case MB_CMD_STS_INTRMDT:
356         case MB_CMD_STS_GOOD:
357         case MB_CMD_STS_INVLD_CMD:
358         case MB_CMD_STS_XFC_ERR:
359         case MB_CMD_STS_CSUM_ERR:
360         case MB_CMD_STS_ERR:
361         case MB_CMD_STS_PARAM_ERR:
362                 /* We can only get mailbox status if we're polling from an
363                  * unfinished command.  Get the rest of the status data and
364                  * return back to the caller.
365                  * We only end up here when we're polling for a mailbox
366                  * command completion.
367                  */
368                 mbcp->out_count = orig_count;
369                 status = ql_get_mb_sts(qdev, mbcp);
370                 return status;
371
372         /* We are being asked by firmware to accept
373          * a change to the port.  This is only
374          * a change to max frame sizes (Tx/Rx), pause
375          * parameters, or loopback mode.
376          */
377         case AEN_IDC_REQ:
378                 status = ql_idc_req_aen(qdev);
379                 break;
380
381         /* Process and inbound IDC event.
382          * This will happen when we're trying to
383          * change tx/rx max frame size, change pause
384          * parameters or loopback mode.
385          */
386         case AEN_IDC_CMPLT:
387         case AEN_IDC_EXT:
388                 status = ql_idc_cmplt_aen(qdev);
389                 break;
390
391         case AEN_LINK_UP:
392                 ql_link_up(qdev, mbcp);
393                 break;
394
395         case AEN_LINK_DOWN:
396                 ql_link_down(qdev, mbcp);
397                 break;
398
399         case AEN_FW_INIT_DONE:
400                 /* If we're in process on executing the firmware,
401                  * then convert the status to normal mailbox status.
402                  */
403                 if (mbcp->mbox_in[0] == MB_CMD_EX_FW) {
404                         mbcp->out_count = orig_count;
405                         status = ql_get_mb_sts(qdev, mbcp);
406                         mbcp->mbox_out[0] = MB_CMD_STS_GOOD;
407                         return status;
408                 }
409                 ql_init_fw_done(qdev, mbcp);
410                 break;
411
412         case AEN_AEN_SFP_IN:
413                 ql_sfp_in(qdev, mbcp);
414                 break;
415
416         case AEN_AEN_SFP_OUT:
417                 ql_sfp_out(qdev, mbcp);
418                 break;
419
420         /* This event can arrive at boot time or after an
421          * MPI reset if the firmware failed to initialize.
422          */
423         case AEN_FW_INIT_FAIL:
424                 /* If we're in process on executing the firmware,
425                  * then convert the status to normal mailbox status.
426                  */
427                 if (mbcp->mbox_in[0] == MB_CMD_EX_FW) {
428                         mbcp->out_count = orig_count;
429                         status = ql_get_mb_sts(qdev, mbcp);
430                         mbcp->mbox_out[0] = MB_CMD_STS_ERR;
431                         return status;
432                 }
433                 QPRINTK(qdev, DRV, ERR,
434                         "Firmware initialization failed.\n");
435                 status = -EIO;
436                 ql_queue_fw_error(qdev);
437                 break;
438
439         case AEN_SYS_ERR:
440                 QPRINTK(qdev, DRV, ERR,
441                         "System Error.\n");
442                 ql_queue_fw_error(qdev);
443                 status = -EIO;
444                 break;
445
446         case AEN_AEN_LOST:
447                 ql_aen_lost(qdev, mbcp);
448                 break;
449
450         case AEN_DCBX_CHG:
451                 /* Need to support AEN 8110 */
452                 break;
453         default:
454                 QPRINTK(qdev, DRV, ERR,
455                         "Unsupported AE %.08x.\n", mbcp->mbox_out[0]);
456                 /* Clear the MPI firmware status. */
457         }
458 end:
459         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
460         /* Restore the original mailbox count to
461          * what the caller asked for.  This can get
462          * changed when a mailbox command is waiting
463          * for a response and an AEN arrives and
464          * is handled.
465          * */
466         mbcp->out_count = orig_count;
467         return status;
468 }
469
470 /* Execute a single mailbox command.
471  * mbcp is a pointer to an array of u32.  Each
472  * element in the array contains the value for it's
473  * respective mailbox register.
474  */
475 static int ql_mailbox_command(struct ql_adapter *qdev, struct mbox_params *mbcp)
476 {
477         int status, count;
478
479
480         /* Begin polled mode for MPI */
481         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
482
483         /* Load the mailbox registers and wake up MPI RISC. */
484         status = ql_exec_mb_cmd(qdev, mbcp);
485         if (status)
486                 goto end;
487
488
489         /* If we're generating a system error, then there's nothing
490          * to wait for.
491          */
492         if (mbcp->mbox_in[0] == MB_CMD_MAKE_SYS_ERR)
493                 goto end;
494
495         /* Wait for the command to complete. We loop
496          * here because some AEN might arrive while
497          * we're waiting for the mailbox command to
498          * complete. If more than 5 arrive then we can
499          * assume something is wrong. */
500         count = 5;
501         do {
502                 /* Wait for the interrupt to come in. */
503                 status = ql_wait_mbx_cmd_cmplt(qdev);
504                 if (status)
505                         goto end;
506
507                 /* Process the event.  If it's an AEN, it
508                  * will be handled in-line or a worker
509                  * will be spawned. If it's our completion
510                  * we will catch it below.
511                  */
512                 status = ql_mpi_handler(qdev, mbcp);
513                 if (status)
514                         goto end;
515
516                 /* It's either the completion for our mailbox
517                  * command complete or an AEN.  If it's our
518                  * completion then get out.
519                  */
520                 if (((mbcp->mbox_out[0] & 0x0000f000) ==
521                                         MB_CMD_STS_GOOD) ||
522                         ((mbcp->mbox_out[0] & 0x0000f000) ==
523                                         MB_CMD_STS_INTRMDT))
524                         break;
525         } while (--count);
526
527         if (!count) {
528                 QPRINTK(qdev, DRV, ERR,
529                         "Timed out waiting for mailbox complete.\n");
530                 status = -ETIMEDOUT;
531                 goto end;
532         }
533
534         /* Now we can clear the interrupt condition
535          * and look at our status.
536          */
537         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
538
539         if (((mbcp->mbox_out[0] & 0x0000f000) !=
540                                         MB_CMD_STS_GOOD) &&
541                 ((mbcp->mbox_out[0] & 0x0000f000) !=
542                                         MB_CMD_STS_INTRMDT)) {
543                 ql_display_mb_sts(qdev, mbcp);
544                 status = -EIO;
545         }
546 end:
547         /* End polled mode for MPI */
548         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
549         return status;
550 }
551
552
553 /* Get MPI firmware version. This will be used for
554  * driver banner and for ethtool info.
555  * Returns zero on success.
556  */
557 int ql_mb_about_fw(struct ql_adapter *qdev)
558 {
559         struct mbox_params mbc;
560         struct mbox_params *mbcp = &mbc;
561         int status = 0;
562
563         memset(mbcp, 0, sizeof(struct mbox_params));
564
565         mbcp->in_count = 1;
566         mbcp->out_count = 3;
567
568         mbcp->mbox_in[0] = MB_CMD_ABOUT_FW;
569
570         status = ql_mailbox_command(qdev, mbcp);
571         if (status)
572                 return status;
573
574         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
575                 QPRINTK(qdev, DRV, ERR,
576                         "Failed about firmware command\n");
577                 status = -EIO;
578         }
579
580         /* Store the firmware version */
581         qdev->fw_rev_id = mbcp->mbox_out[1];
582
583         return status;
584 }
585
586 /* Get functional state for MPI firmware.
587  * Returns zero on success.
588  */
589 int ql_mb_get_fw_state(struct ql_adapter *qdev)
590 {
591         struct mbox_params mbc;
592         struct mbox_params *mbcp = &mbc;
593         int status = 0;
594
595         memset(mbcp, 0, sizeof(struct mbox_params));
596
597         mbcp->in_count = 1;
598         mbcp->out_count = 2;
599
600         mbcp->mbox_in[0] = MB_CMD_GET_FW_STATE;
601
602         status = ql_mailbox_command(qdev, mbcp);
603         if (status)
604                 return status;
605
606         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
607                 QPRINTK(qdev, DRV, ERR,
608                         "Failed Get Firmware State.\n");
609                 status = -EIO;
610         }
611
612         /* If bit zero is set in mbx 1 then the firmware is
613          * running, but not initialized.  This should never
614          * happen.
615          */
616         if (mbcp->mbox_out[1] & 1) {
617                 QPRINTK(qdev, DRV, ERR,
618                         "Firmware waiting for initialization.\n");
619                 status = -EIO;
620         }
621
622         return status;
623 }
624
625 /* Send and ACK mailbox command to the firmware to
626  * let it continue with the change.
627  */
628 int ql_mb_idc_ack(struct ql_adapter *qdev)
629 {
630         struct mbox_params mbc;
631         struct mbox_params *mbcp = &mbc;
632         int status = 0;
633
634         memset(mbcp, 0, sizeof(struct mbox_params));
635
636         mbcp->in_count = 5;
637         mbcp->out_count = 1;
638
639         mbcp->mbox_in[0] = MB_CMD_IDC_ACK;
640         mbcp->mbox_in[1] = qdev->idc_mbc.mbox_out[1];
641         mbcp->mbox_in[2] = qdev->idc_mbc.mbox_out[2];
642         mbcp->mbox_in[3] = qdev->idc_mbc.mbox_out[3];
643         mbcp->mbox_in[4] = qdev->idc_mbc.mbox_out[4];
644
645         status = ql_mailbox_command(qdev, mbcp);
646         if (status)
647                 return status;
648
649         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
650                 QPRINTK(qdev, DRV, ERR,
651                         "Failed IDC ACK send.\n");
652                 status = -EIO;
653         }
654         return status;
655 }
656
657 /* Get link settings and maximum frame size settings
658  * for the current port.
659  * Most likely will block.
660  */
661 static int ql_mb_set_port_cfg(struct ql_adapter *qdev)
662 {
663         struct mbox_params mbc;
664         struct mbox_params *mbcp = &mbc;
665         int status = 0;
666
667         memset(mbcp, 0, sizeof(struct mbox_params));
668
669         mbcp->in_count = 3;
670         mbcp->out_count = 1;
671
672         mbcp->mbox_in[0] = MB_CMD_SET_PORT_CFG;
673         mbcp->mbox_in[1] = qdev->link_config;
674         mbcp->mbox_in[2] = qdev->max_frame_size;
675
676
677         status = ql_mailbox_command(qdev, mbcp);
678         if (status)
679                 return status;
680
681         if (mbcp->mbox_out[0] == MB_CMD_STS_INTRMDT) {
682                 QPRINTK(qdev, DRV, ERR,
683                         "Port Config sent, wait for IDC.\n");
684         } else  if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
685                 QPRINTK(qdev, DRV, ERR,
686                         "Failed Set Port Configuration.\n");
687                 status = -EIO;
688         }
689         return status;
690 }
691
692 /* Get link settings and maximum frame size settings
693  * for the current port.
694  * Most likely will block.
695  */
696 static int ql_mb_get_port_cfg(struct ql_adapter *qdev)
697 {
698         struct mbox_params mbc;
699         struct mbox_params *mbcp = &mbc;
700         int status = 0;
701
702         memset(mbcp, 0, sizeof(struct mbox_params));
703
704         mbcp->in_count = 1;
705         mbcp->out_count = 3;
706
707         mbcp->mbox_in[0] = MB_CMD_GET_PORT_CFG;
708
709         status = ql_mailbox_command(qdev, mbcp);
710         if (status)
711                 return status;
712
713         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
714                 QPRINTK(qdev, DRV, ERR,
715                         "Failed Get Port Configuration.\n");
716                 status = -EIO;
717         } else  {
718                 QPRINTK(qdev, DRV, DEBUG,
719                         "Passed Get Port Configuration.\n");
720                 qdev->link_config = mbcp->mbox_out[1];
721                 qdev->max_frame_size = mbcp->mbox_out[2];
722         }
723         return status;
724 }
725
726 /* IDC - Inter Device Communication...
727  * Some firmware commands require consent of adjacent FCOE
728  * function.  This function waits for the OK, or a
729  * counter-request for a little more time.i
730  * The firmware will complete the request if the other
731  * function doesn't respond.
732  */
733 static int ql_idc_wait(struct ql_adapter *qdev)
734 {
735         int status = -ETIMEDOUT;
736         long wait_time = 1 * HZ;
737         struct mbox_params *mbcp = &qdev->idc_mbc;
738         do {
739                 /* Wait here for the command to complete
740                  * via the IDC process.
741                  */
742                 wait_time =
743                         wait_for_completion_timeout(&qdev->ide_completion,
744                                                         wait_time);
745                 if (!wait_time) {
746                         QPRINTK(qdev, DRV, ERR,
747                                 "IDC Timeout.\n");
748                         break;
749                 }
750                 /* Now examine the response from the IDC process.
751                  * We might have a good completion or a request for
752                  * more wait time.
753                  */
754                 if (mbcp->mbox_out[0] == AEN_IDC_EXT) {
755                         QPRINTK(qdev, DRV, ERR,
756                                 "IDC Time Extension from function.\n");
757                         wait_time += (mbcp->mbox_out[1] >> 8) & 0x0000000f;
758                 } else if (mbcp->mbox_out[0] == AEN_IDC_CMPLT) {
759                         QPRINTK(qdev, DRV, ERR,
760                                 "IDC Success.\n");
761                         status = 0;
762                         break;
763                 } else {
764                         QPRINTK(qdev, DRV, ERR,
765                                 "IDC: Invalid State 0x%.04x.\n",
766                                 mbcp->mbox_out[0]);
767                         status = -EIO;
768                         break;
769                 }
770         } while (wait_time);
771
772         return status;
773 }
774
775 int ql_mb_set_mgmnt_traffic_ctl(struct ql_adapter *qdev, u32 control)
776 {
777         struct mbox_params mbc;
778         struct mbox_params *mbcp = &mbc;
779         int status;
780
781         memset(mbcp, 0, sizeof(struct mbox_params));
782
783         mbcp->in_count = 1;
784         mbcp->out_count = 2;
785
786         mbcp->mbox_in[0] = MB_CMD_SET_MGMNT_TFK_CTL;
787         mbcp->mbox_in[1] = control;
788
789         status = ql_mailbox_command(qdev, mbcp);
790         if (status)
791                 return status;
792
793         if (mbcp->mbox_out[0] == MB_CMD_STS_GOOD)
794                 return status;
795
796         if (mbcp->mbox_out[0] == MB_CMD_STS_INVLD_CMD) {
797                 QPRINTK(qdev, DRV, ERR,
798                         "Command not supported by firmware.\n");
799                 status = -EINVAL;
800         } else if (mbcp->mbox_out[0] == MB_CMD_STS_ERR) {
801                 /* This indicates that the firmware is
802                  * already in the state we are trying to
803                  * change it to.
804                  */
805                 QPRINTK(qdev, DRV, ERR,
806                         "Command parameters make no change.\n");
807         }
808         return status;
809 }
810
811 /* Returns a negative error code or the mailbox command status. */
812 static int ql_mb_get_mgmnt_traffic_ctl(struct ql_adapter *qdev, u32 *control)
813 {
814         struct mbox_params mbc;
815         struct mbox_params *mbcp = &mbc;
816         int status;
817
818         memset(mbcp, 0, sizeof(struct mbox_params));
819         *control = 0;
820
821         mbcp->in_count = 1;
822         mbcp->out_count = 1;
823
824         mbcp->mbox_in[0] = MB_CMD_GET_MGMNT_TFK_CTL;
825
826         status = ql_mailbox_command(qdev, mbcp);
827         if (status)
828                 return status;
829
830         if (mbcp->mbox_out[0] == MB_CMD_STS_GOOD) {
831                 *control = mbcp->mbox_in[1];
832                 return status;
833         }
834
835         if (mbcp->mbox_out[0] == MB_CMD_STS_INVLD_CMD) {
836                 QPRINTK(qdev, DRV, ERR,
837                         "Command not supported by firmware.\n");
838                 status = -EINVAL;
839         } else if (mbcp->mbox_out[0] == MB_CMD_STS_ERR) {
840                 QPRINTK(qdev, DRV, ERR,
841                         "Failed to get MPI traffic control.\n");
842                 status = -EIO;
843         }
844         return status;
845 }
846
847 int ql_wait_fifo_empty(struct ql_adapter *qdev)
848 {
849         int count = 5;
850         u32 mgmnt_fifo_empty;
851         u32 nic_fifo_empty;
852
853         do {
854                 nic_fifo_empty = ql_read32(qdev, STS) & STS_NFE;
855                 ql_mb_get_mgmnt_traffic_ctl(qdev, &mgmnt_fifo_empty);
856                 mgmnt_fifo_empty &= MB_GET_MPI_TFK_FIFO_EMPTY;
857                 if (nic_fifo_empty && mgmnt_fifo_empty)
858                         return 0;
859                 msleep(100);
860         } while (count-- > 0);
861         return -ETIMEDOUT;
862 }
863
864 /* API called in work thread context to set new TX/RX
865  * maximum frame size values to match MTU.
866  */
867 static int ql_set_port_cfg(struct ql_adapter *qdev)
868 {
869         int status;
870         rtnl_lock();
871         status = ql_mb_set_port_cfg(qdev);
872         rtnl_unlock();
873         if (status)
874                 return status;
875         status = ql_idc_wait(qdev);
876         return status;
877 }
878
879 /* The following routines are worker threads that process
880  * events that may sleep waiting for completion.
881  */
882
883 /* This thread gets the maximum TX and RX frame size values
884  * from the firmware and, if necessary, changes them to match
885  * the MTU setting.
886  */
887 void ql_mpi_port_cfg_work(struct work_struct *work)
888 {
889         struct ql_adapter *qdev =
890             container_of(work, struct ql_adapter, mpi_port_cfg_work.work);
891         int status;
892
893         rtnl_lock();
894         status = ql_mb_get_port_cfg(qdev);
895         rtnl_unlock();
896         if (status) {
897                 QPRINTK(qdev, DRV, ERR,
898                         "Bug: Failed to get port config data.\n");
899                 goto err;
900         }
901
902         if (qdev->link_config & CFG_JUMBO_FRAME_SIZE &&
903                         qdev->max_frame_size ==
904                         CFG_DEFAULT_MAX_FRAME_SIZE)
905                 goto end;
906
907         qdev->link_config |=    CFG_JUMBO_FRAME_SIZE;
908         qdev->max_frame_size = CFG_DEFAULT_MAX_FRAME_SIZE;
909         status = ql_set_port_cfg(qdev);
910         if (status) {
911                 QPRINTK(qdev, DRV, ERR,
912                         "Bug: Failed to set port config data.\n");
913                 goto err;
914         }
915 end:
916         clear_bit(QL_PORT_CFG, &qdev->flags);
917         return;
918 err:
919         ql_queue_fw_error(qdev);
920         goto end;
921 }
922
923 /* Process an inter-device request.  This is issues by
924  * the firmware in response to another function requesting
925  * a change to the port. We set a flag to indicate a change
926  * has been made and then send a mailbox command ACKing
927  * the change request.
928  */
929 void ql_mpi_idc_work(struct work_struct *work)
930 {
931         struct ql_adapter *qdev =
932             container_of(work, struct ql_adapter, mpi_idc_work.work);
933         int status;
934         struct mbox_params *mbcp = &qdev->idc_mbc;
935         u32 aen;
936
937         aen = mbcp->mbox_out[1] >> 16;
938
939         switch (aen) {
940         default:
941                 QPRINTK(qdev, DRV, ERR,
942                         "Bug: Unhandled IDC action.\n");
943                 break;
944         case MB_CMD_PORT_RESET:
945         case MB_CMD_SET_PORT_CFG:
946         case MB_CMD_STOP_FW:
947                 ql_link_off(qdev);
948                 /* Signal the resulting link up AEN
949                  * that the frame routing and mac addr
950                  * needs to be set.
951                  * */
952                 set_bit(QL_CAM_RT_SET, &qdev->flags);
953                 rtnl_lock();
954                 status = ql_mb_idc_ack(qdev);
955                 rtnl_unlock();
956                 if (status) {
957                         QPRINTK(qdev, DRV, ERR,
958                         "Bug: No pending IDC!\n");
959                 }
960         }
961 }
962
963 void ql_mpi_work(struct work_struct *work)
964 {
965         struct ql_adapter *qdev =
966             container_of(work, struct ql_adapter, mpi_work.work);
967         struct mbox_params mbc;
968         struct mbox_params *mbcp = &mbc;
969         int err = 0;
970
971         rtnl_lock();
972         /* Begin polled mode for MPI */
973         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
974
975         while (ql_read32(qdev, STS) & STS_PI) {
976                 memset(mbcp, 0, sizeof(struct mbox_params));
977                 mbcp->out_count = 1;
978                 /* Don't continue if an async event
979                  * did not complete properly.
980                  */
981                 err = ql_mpi_handler(qdev, mbcp);
982                 if (err)
983                         break;
984         }
985
986         /* End polled mode for MPI */
987         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
988         rtnl_unlock();
989         ql_enable_completion_interrupt(qdev, 0);
990 }
991
992 void ql_mpi_reset_work(struct work_struct *work)
993 {
994         struct ql_adapter *qdev =
995             container_of(work, struct ql_adapter, mpi_reset_work.work);
996         cancel_delayed_work_sync(&qdev->mpi_work);
997         cancel_delayed_work_sync(&qdev->mpi_port_cfg_work);
998         cancel_delayed_work_sync(&qdev->mpi_idc_work);
999         ql_soft_reset_mpi_risc(qdev);
1000 }