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