ebb7b0c48dea5ed4aca79fbcf9f1c36d782dab26
[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 = 50; /* TODO: arbitrary for now. */
94         u32 value;
95
96         do {
97                 value = ql_read32(qdev, STS);
98                 if (value & STS_PI)
99                         return 0;
100                 udelay(UDELAY_DELAY); /* 10us */
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 /* Process an inter-device event completion.
142  * If good, signal the caller's completion.
143  */
144 static int ql_idc_cmplt_aen(struct ql_adapter *qdev)
145 {
146         int status;
147         struct mbox_params *mbcp = &qdev->idc_mbc;
148         mbcp->out_count = 4;
149         status = ql_get_mb_sts(qdev, mbcp);
150         if (status) {
151                 QPRINTK(qdev, DRV, ERR,
152                         "Could not read MPI, resetting RISC!\n");
153                 ql_queue_fw_error(qdev);
154         } else
155                 /* Wake up the sleeping mpi_idc_work thread that is
156                  * waiting for this event.
157                  */
158                 complete(&qdev->ide_completion);
159
160         return status;
161 }
162 static void ql_link_up(struct ql_adapter *qdev, struct mbox_params *mbcp)
163 {
164         mbcp->out_count = 2;
165
166         if (ql_get_mb_sts(qdev, mbcp))
167                 goto exit;
168
169         qdev->link_status = mbcp->mbox_out[1];
170         QPRINTK(qdev, DRV, ERR, "Link Up.\n");
171         QPRINTK(qdev, DRV, INFO, "Link Status = 0x%.08x.\n", mbcp->mbox_out[1]);
172         if (!netif_carrier_ok(qdev->ndev)) {
173                 QPRINTK(qdev, LINK, INFO, "Link is Up.\n");
174                 netif_carrier_on(qdev->ndev);
175                 netif_wake_queue(qdev->ndev);
176         }
177 exit:
178         /* Clear the MPI firmware status. */
179         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
180 }
181
182 static void ql_link_down(struct ql_adapter *qdev, struct mbox_params *mbcp)
183 {
184         mbcp->out_count = 3;
185
186         if (ql_get_mb_sts(qdev, mbcp)) {
187                 QPRINTK(qdev, DRV, ERR, "Firmware did not initialize!\n");
188                 goto exit;
189         }
190
191         if (netif_carrier_ok(qdev->ndev)) {
192                 QPRINTK(qdev, LINK, INFO, "Link is Down.\n");
193                 netif_carrier_off(qdev->ndev);
194                 netif_stop_queue(qdev->ndev);
195         }
196         QPRINTK(qdev, DRV, ERR, "Link Down.\n");
197         QPRINTK(qdev, DRV, ERR, "Link Status = 0x%.08x.\n", mbcp->mbox_out[1]);
198 exit:
199         /* Clear the MPI firmware status. */
200         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
201 }
202
203 static int ql_sfp_in(struct ql_adapter *qdev, struct mbox_params *mbcp)
204 {
205         int status;
206
207         mbcp->out_count = 5;
208
209         status = ql_get_mb_sts(qdev, mbcp);
210         if (status)
211                 QPRINTK(qdev, DRV, ERR, "SFP in AEN broken!\n");
212         else
213                 QPRINTK(qdev, DRV, ERR, "SFP insertion detected.\n");
214
215         return status;
216 }
217
218 static int ql_sfp_out(struct ql_adapter *qdev, struct mbox_params *mbcp)
219 {
220         int status;
221
222         mbcp->out_count = 1;
223
224         status = ql_get_mb_sts(qdev, mbcp);
225         if (status)
226                 QPRINTK(qdev, DRV, ERR, "SFP out AEN broken!\n");
227         else
228                 QPRINTK(qdev, DRV, ERR, "SFP removal detected.\n");
229
230         return status;
231 }
232
233 static void ql_init_fw_done(struct ql_adapter *qdev, struct mbox_params *mbcp)
234 {
235         mbcp->out_count = 2;
236
237         if (ql_get_mb_sts(qdev, mbcp)) {
238                 QPRINTK(qdev, DRV, ERR, "Firmware did not initialize!\n");
239                 goto exit;
240         }
241         QPRINTK(qdev, DRV, ERR, "Firmware initialized!\n");
242         QPRINTK(qdev, DRV, ERR, "Firmware status = 0x%.08x.\n",
243                 mbcp->mbox_out[0]);
244         QPRINTK(qdev, DRV, ERR, "Firmware Revision  = 0x%.08x.\n",
245                 mbcp->mbox_out[1]);
246 exit:
247         /* Clear the MPI firmware status. */
248         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
249 }
250
251 /* Process an async event and clear it unless it's an
252  * error condition.
253  *  This can get called iteratively from the mpi_work thread
254  *  when events arrive via an interrupt.
255  *  It also gets called when a mailbox command is polling for
256  *  it's completion. */
257 static int ql_mpi_handler(struct ql_adapter *qdev, struct mbox_params *mbcp)
258 {
259         int status;
260         int orig_count = mbcp->out_count;
261
262         /* Just get mailbox zero for now. */
263         mbcp->out_count = 1;
264         status = ql_get_mb_sts(qdev, mbcp);
265         if (status) {
266                 QPRINTK(qdev, DRV, ERR,
267                         "Could not read MPI, resetting ASIC!\n");
268                 ql_queue_asic_error(qdev);
269                 goto end;
270         }
271
272         switch (mbcp->mbox_out[0]) {
273
274         /* This case is only active when we arrive here
275          * as a result of issuing a mailbox command to
276          * the firmware.
277          */
278         case MB_CMD_STS_INTRMDT:
279         case MB_CMD_STS_GOOD:
280         case MB_CMD_STS_INVLD_CMD:
281         case MB_CMD_STS_XFC_ERR:
282         case MB_CMD_STS_CSUM_ERR:
283         case MB_CMD_STS_ERR:
284         case MB_CMD_STS_PARAM_ERR:
285                 /* We can only get mailbox status if we're polling from an
286                  * unfinished command.  Get the rest of the status data and
287                  * return back to the caller.
288                  * We only end up here when we're polling for a mailbox
289                  * command completion.
290                  */
291                 mbcp->out_count = orig_count;
292                 status = ql_get_mb_sts(qdev, mbcp);
293                 return status;
294
295         /* Process and inbound IDC event.
296          * This will happen when we're trying to
297          * change tx/rx max frame size, change pause
298          * paramters or loopback mode.
299          */
300         case AEN_IDC_CMPLT:
301         case AEN_IDC_EXT:
302                 status = ql_idc_cmplt_aen(qdev);
303                 break;
304
305         case AEN_LINK_UP:
306                 ql_link_up(qdev, mbcp);
307                 break;
308
309         case AEN_LINK_DOWN:
310                 ql_link_down(qdev, mbcp);
311                 break;
312
313         case AEN_FW_INIT_DONE:
314                 ql_init_fw_done(qdev, mbcp);
315                 break;
316
317         case AEN_AEN_SFP_IN:
318                 ql_sfp_in(qdev, mbcp);
319                 break;
320
321         case AEN_AEN_SFP_OUT:
322                 ql_sfp_out(qdev, mbcp);
323                 break;
324
325         case AEN_FW_INIT_FAIL:
326         case AEN_SYS_ERR:
327                 ql_queue_fw_error(qdev);
328                 break;
329
330         default:
331                 QPRINTK(qdev, DRV, ERR,
332                         "Unsupported AE %.08x.\n", mbcp->mbox_out[0]);
333                 /* Clear the MPI firmware status. */
334         }
335 end:
336         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
337         return status;
338 }
339
340 /* Execute a single mailbox command.
341  * mbcp is a pointer to an array of u32.  Each
342  * element in the array contains the value for it's
343  * respective mailbox register.
344  */
345 static int ql_mailbox_command(struct ql_adapter *qdev, struct mbox_params *mbcp)
346 {
347         int status, count;
348
349         mutex_lock(&qdev->mpi_mutex);
350
351         /* Begin polled mode for MPI */
352         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16));
353
354         /* Load the mailbox registers and wake up MPI RISC. */
355         status = ql_exec_mb_cmd(qdev, mbcp);
356         if (status)
357                 goto end;
358
359
360         /* If we're generating a system error, then there's nothing
361          * to wait for.
362          */
363         if (mbcp->mbox_in[0] == MB_CMD_MAKE_SYS_ERR)
364                 goto end;
365
366         /* Wait for the command to complete. We loop
367          * here because some AEN might arrive while
368          * we're waiting for the mailbox command to
369          * complete. If more than 5 arrive then we can
370          * assume something is wrong. */
371         count = 5;
372         do {
373                 /* Wait for the interrupt to come in. */
374                 status = ql_wait_mbx_cmd_cmplt(qdev);
375                 if (status)
376                         goto end;
377
378                 /* Process the event.  If it's an AEN, it
379                  * will be handled in-line or a worker
380                  * will be spawned. If it's our completion
381                  * we will catch it below.
382                  */
383                 status = ql_mpi_handler(qdev, mbcp);
384                 if (status)
385                         goto end;
386
387                 /* It's either the completion for our mailbox
388                  * command complete or an AEN.  If it's our
389                  * completion then get out.
390                  */
391                 if (((mbcp->mbox_out[0] & 0x0000f000) ==
392                                         MB_CMD_STS_GOOD) ||
393                         ((mbcp->mbox_out[0] & 0x0000f000) ==
394                                         MB_CMD_STS_INTRMDT))
395                         break;
396         } while (--count);
397
398         if (!count) {
399                 QPRINTK(qdev, DRV, ERR,
400                         "Timed out waiting for mailbox complete.\n");
401                 status = -ETIMEDOUT;
402                 goto end;
403         }
404
405         /* Now we can clear the interrupt condition
406          * and look at our status.
407          */
408         ql_write32(qdev, CSR, CSR_CMD_CLR_R2PCI_INT);
409
410         if (((mbcp->mbox_out[0] & 0x0000f000) !=
411                                         MB_CMD_STS_GOOD) &&
412                 ((mbcp->mbox_out[0] & 0x0000f000) !=
413                                         MB_CMD_STS_INTRMDT)) {
414                 ql_display_mb_sts(qdev, mbcp);
415                 status = -EIO;
416         }
417 end:
418         mutex_unlock(&qdev->mpi_mutex);
419         /* End polled mode for MPI */
420         ql_write32(qdev, INTR_MASK, (INTR_MASK_PI << 16) | INTR_MASK_PI);
421         return status;
422 }
423
424 /* Get functional state for MPI firmware.
425  * Returns zero on success.
426  */
427 int ql_mb_get_fw_state(struct ql_adapter *qdev)
428 {
429         struct mbox_params mbc;
430         struct mbox_params *mbcp = &mbc;
431         int status = 0;
432
433         memset(mbcp, 0, sizeof(struct mbox_params));
434
435         mbcp->in_count = 1;
436         mbcp->out_count = 2;
437
438         mbcp->mbox_in[0] = MB_CMD_GET_FW_STATE;
439
440         status = ql_mailbox_command(qdev, mbcp);
441         if (status)
442                 return status;
443
444         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
445                 QPRINTK(qdev, DRV, ERR,
446                         "Failed Get Firmware State.\n");
447                 status = -EIO;
448         }
449
450         /* If bit zero is set in mbx 1 then the firmware is
451          * running, but not initialized.  This should never
452          * happen.
453          */
454         if (mbcp->mbox_out[1] & 1) {
455                 QPRINTK(qdev, DRV, ERR,
456                         "Firmware waiting for initialization.\n");
457                 status = -EIO;
458         }
459
460         return status;
461 }
462
463 /* Get link settings and maximum frame size settings
464  * for the current port.
465  * Most likely will block.
466  */
467 static int ql_mb_set_port_cfg(struct ql_adapter *qdev)
468 {
469         struct mbox_params mbc;
470         struct mbox_params *mbcp = &mbc;
471         int status = 0;
472
473         memset(mbcp, 0, sizeof(struct mbox_params));
474
475         mbcp->in_count = 3;
476         mbcp->out_count = 1;
477
478         mbcp->mbox_in[0] = MB_CMD_SET_PORT_CFG;
479         mbcp->mbox_in[1] = qdev->link_config;
480         mbcp->mbox_in[2] = qdev->max_frame_size;
481
482
483         status = ql_mailbox_command(qdev, mbcp);
484         if (status)
485                 return status;
486
487         if (mbcp->mbox_out[0] == MB_CMD_STS_INTRMDT) {
488                 QPRINTK(qdev, DRV, ERR,
489                         "Port Config sent, wait for IDC.\n");
490         } else  if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
491                 QPRINTK(qdev, DRV, ERR,
492                         "Failed Set Port Configuration.\n");
493                 status = -EIO;
494         }
495         return status;
496 }
497
498 /* Get link settings and maximum frame size settings
499  * for the current port.
500  * Most likely will block.
501  */
502 static int ql_mb_get_port_cfg(struct ql_adapter *qdev)
503 {
504         struct mbox_params mbc;
505         struct mbox_params *mbcp = &mbc;
506         int status = 0;
507
508         memset(mbcp, 0, sizeof(struct mbox_params));
509
510         mbcp->in_count = 1;
511         mbcp->out_count = 3;
512
513         mbcp->mbox_in[0] = MB_CMD_GET_PORT_CFG;
514
515         status = ql_mailbox_command(qdev, mbcp);
516         if (status)
517                 return status;
518
519         if (mbcp->mbox_out[0] != MB_CMD_STS_GOOD) {
520                 QPRINTK(qdev, DRV, ERR,
521                         "Failed Get Port Configuration.\n");
522                 status = -EIO;
523         } else  {
524                 QPRINTK(qdev, DRV, DEBUG,
525                         "Passed Get Port Configuration.\n");
526                 qdev->link_config = mbcp->mbox_out[1];
527                 qdev->max_frame_size = mbcp->mbox_out[2];
528         }
529         return status;
530 }
531
532 /* IDC - Inter Device Communication...
533  * Some firmware commands require consent of adjacent FCOE
534  * function.  This function waits for the OK, or a
535  * counter-request for a little more time.i
536  * The firmware will complete the request if the other
537  * function doesn't respond.
538  */
539 static int ql_idc_wait(struct ql_adapter *qdev)
540 {
541         int status = -ETIMEDOUT;
542         long wait_time = 1 * HZ;
543         struct mbox_params *mbcp = &qdev->idc_mbc;
544         do {
545                 /* Wait here for the command to complete
546                  * via the IDC process.
547                  */
548                 wait_time =
549                         wait_for_completion_timeout(&qdev->ide_completion,
550                                                         wait_time);
551                 if (!wait_time) {
552                         QPRINTK(qdev, DRV, ERR,
553                                 "IDC Timeout.\n");
554                         break;
555                 }
556                 /* Now examine the response from the IDC process.
557                  * We might have a good completion or a request for
558                  * more wait time.
559                  */
560                 if (mbcp->mbox_out[0] == AEN_IDC_EXT) {
561                         QPRINTK(qdev, DRV, ERR,
562                                 "IDC Time Extension from function.\n");
563                         wait_time += (mbcp->mbox_out[1] >> 8) & 0x0000000f;
564                 } else if (mbcp->mbox_out[0] == AEN_IDC_CMPLT) {
565                         QPRINTK(qdev, DRV, ERR,
566                                 "IDC Success.\n");
567                         status = 0;
568                         break;
569                 } else {
570                         QPRINTK(qdev, DRV, ERR,
571                                 "IDC: Invalid State 0x%.04x.\n",
572                                 mbcp->mbox_out[0]);
573                         status = -EIO;
574                         break;
575                 }
576         } while (wait_time);
577
578         return status;
579 }
580
581 /* API called in work thread context to set new TX/RX
582  * maximum frame size values to match MTU.
583  */
584 static int ql_set_port_cfg(struct ql_adapter *qdev)
585 {
586         int status;
587         status = ql_mb_set_port_cfg(qdev);
588         if (status)
589                 return status;
590         status = ql_idc_wait(qdev);
591         return status;
592 }
593
594 /* The following routines are worker threads that process
595  * events that may sleep waiting for completion.
596  */
597
598 /* This thread gets the maximum TX and RX frame size values
599  * from the firmware and, if necessary, changes them to match
600  * the MTU setting.
601  */
602 void ql_mpi_port_cfg_work(struct work_struct *work)
603 {
604         struct ql_adapter *qdev =
605             container_of(work, struct ql_adapter, mpi_port_cfg_work.work);
606         struct net_device *ndev = qdev->ndev;
607         int status;
608
609         status = ql_mb_get_port_cfg(qdev);
610         if (status) {
611                 QPRINTK(qdev, DRV, ERR,
612                         "Bug: Failed to get port config data.\n");
613                 goto err;
614         }
615
616         if (ndev->mtu <= 2500)
617                 goto end;
618         else if (qdev->link_config & CFG_JUMBO_FRAME_SIZE &&
619                         qdev->max_frame_size ==
620                         CFG_DEFAULT_MAX_FRAME_SIZE)
621                 goto end;
622
623         qdev->link_config |=    CFG_JUMBO_FRAME_SIZE;
624         qdev->max_frame_size = CFG_DEFAULT_MAX_FRAME_SIZE;
625         status = ql_set_port_cfg(qdev);
626         if (status) {
627                 QPRINTK(qdev, DRV, ERR,
628                         "Bug: Failed to set port config data.\n");
629                 goto err;
630         }
631 end:
632         clear_bit(QL_PORT_CFG, &qdev->flags);
633         return;
634 err:
635         ql_queue_fw_error(qdev);
636         goto end;
637 }
638
639 void ql_mpi_work(struct work_struct *work)
640 {
641         struct ql_adapter *qdev =
642             container_of(work, struct ql_adapter, mpi_work.work);
643         struct mbox_params mbc;
644         struct mbox_params *mbcp = &mbc;
645
646         mutex_lock(&qdev->mpi_mutex);
647
648         while (ql_read32(qdev, STS) & STS_PI) {
649                 memset(mbcp, 0, sizeof(struct mbox_params));
650                 mbcp->out_count = 1;
651                 ql_mpi_handler(qdev, mbcp);
652         }
653
654         mutex_unlock(&qdev->mpi_mutex);
655         ql_enable_completion_interrupt(qdev, 0);
656 }
657
658 void ql_mpi_reset_work(struct work_struct *work)
659 {
660         struct ql_adapter *qdev =
661             container_of(work, struct ql_adapter, mpi_reset_work.work);
662         cancel_delayed_work_sync(&qdev->mpi_work);
663         cancel_delayed_work_sync(&qdev->mpi_port_cfg_work);
664         ql_soft_reset_mpi_risc(qdev);
665 }