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