[SCSI] lpfc 8.2.3 : Added support for ASICs that report temperature
[safe/jmp/linux-2.6] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2007 Emulex.  All rights reserved.           *
5  * EMULEX and SLI are trademarks of Emulex.                        *
6  * www.emulex.com                                                  *
7  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of version 2 of the GNU General       *
11  * Public License as published by the Free Software Foundation.    *
12  * This program is distributed in the hope that it will be useful. *
13  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18  * more details, a copy of which can be found in the file COPYING  *
19  * included with this package.                                     *
20  *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26
27 #include <scsi/scsi.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_transport_fc.h>
32
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_scsi.h"
37 #include "lpfc.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_logmsg.h"
40 #include "lpfc_compat.h"
41 #include "lpfc_debugfs.h"
42
43 /*
44  * Define macro to log: Mailbox command x%x cannot issue Data
45  * This allows multiple uses of lpfc_msgBlk0311
46  * w/o perturbing log msg utility.
47  */
48 #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
49                         lpfc_printf_log(phba, \
50                                 KERN_INFO, \
51                                 LOG_MBOX | LOG_SLI, \
52                                 "(%d):0311 Mailbox command x%x cannot " \
53                                 "issue Data: x%x x%x x%x\n", \
54                                 pmbox->vport ? pmbox->vport->vpi : 0, \
55                                 pmbox->mb.mbxCommand,           \
56                                 phba->pport->port_state,        \
57                                 psli->sli_flag, \
58                                 flag)
59
60
61 /* There are only four IOCB completion types. */
62 typedef enum _lpfc_iocb_type {
63         LPFC_UNKNOWN_IOCB,
64         LPFC_UNSOL_IOCB,
65         LPFC_SOL_IOCB,
66         LPFC_ABORT_IOCB
67 } lpfc_iocb_type;
68
69                 /* SLI-2/SLI-3 provide different sized iocbs.  Given a pointer
70                  * to the start of the ring, and the slot number of the
71                  * desired iocb entry, calc a pointer to that entry.
72                  */
73 static inline IOCB_t *
74 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
75 {
76         return (IOCB_t *) (((char *) pring->cmdringaddr) +
77                            pring->cmdidx * phba->iocb_cmd_size);
78 }
79
80 static inline IOCB_t *
81 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
82 {
83         return (IOCB_t *) (((char *) pring->rspringaddr) +
84                            pring->rspidx * phba->iocb_rsp_size);
85 }
86
87 static struct lpfc_iocbq *
88 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
89 {
90         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
91         struct lpfc_iocbq * iocbq = NULL;
92
93         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
94         return iocbq;
95 }
96
97 struct lpfc_iocbq *
98 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
99 {
100         struct lpfc_iocbq * iocbq = NULL;
101         unsigned long iflags;
102
103         spin_lock_irqsave(&phba->hbalock, iflags);
104         iocbq = __lpfc_sli_get_iocbq(phba);
105         spin_unlock_irqrestore(&phba->hbalock, iflags);
106         return iocbq;
107 }
108
109 void
110 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
111 {
112         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
113
114         /*
115          * Clean all volatile data fields, preserve iotag and node struct.
116          */
117         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
118         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
119 }
120
121 void
122 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
123 {
124         unsigned long iflags;
125
126         /*
127          * Clean all volatile data fields, preserve iotag and node struct.
128          */
129         spin_lock_irqsave(&phba->hbalock, iflags);
130         __lpfc_sli_release_iocbq(phba, iocbq);
131         spin_unlock_irqrestore(&phba->hbalock, iflags);
132 }
133
134 /*
135  * Translate the iocb command to an iocb command type used to decide the final
136  * disposition of each completed IOCB.
137  */
138 static lpfc_iocb_type
139 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
140 {
141         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
142
143         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
144                 return 0;
145
146         switch (iocb_cmnd) {
147         case CMD_XMIT_SEQUENCE_CR:
148         case CMD_XMIT_SEQUENCE_CX:
149         case CMD_XMIT_BCAST_CN:
150         case CMD_XMIT_BCAST_CX:
151         case CMD_ELS_REQUEST_CR:
152         case CMD_ELS_REQUEST_CX:
153         case CMD_CREATE_XRI_CR:
154         case CMD_CREATE_XRI_CX:
155         case CMD_GET_RPI_CN:
156         case CMD_XMIT_ELS_RSP_CX:
157         case CMD_GET_RPI_CR:
158         case CMD_FCP_IWRITE_CR:
159         case CMD_FCP_IWRITE_CX:
160         case CMD_FCP_IREAD_CR:
161         case CMD_FCP_IREAD_CX:
162         case CMD_FCP_ICMND_CR:
163         case CMD_FCP_ICMND_CX:
164         case CMD_FCP_TSEND_CX:
165         case CMD_FCP_TRSP_CX:
166         case CMD_FCP_TRECEIVE_CX:
167         case CMD_FCP_AUTO_TRSP_CX:
168         case CMD_ADAPTER_MSG:
169         case CMD_ADAPTER_DUMP:
170         case CMD_XMIT_SEQUENCE64_CR:
171         case CMD_XMIT_SEQUENCE64_CX:
172         case CMD_XMIT_BCAST64_CN:
173         case CMD_XMIT_BCAST64_CX:
174         case CMD_ELS_REQUEST64_CR:
175         case CMD_ELS_REQUEST64_CX:
176         case CMD_FCP_IWRITE64_CR:
177         case CMD_FCP_IWRITE64_CX:
178         case CMD_FCP_IREAD64_CR:
179         case CMD_FCP_IREAD64_CX:
180         case CMD_FCP_ICMND64_CR:
181         case CMD_FCP_ICMND64_CX:
182         case CMD_FCP_TSEND64_CX:
183         case CMD_FCP_TRSP64_CX:
184         case CMD_FCP_TRECEIVE64_CX:
185         case CMD_GEN_REQUEST64_CR:
186         case CMD_GEN_REQUEST64_CX:
187         case CMD_XMIT_ELS_RSP64_CX:
188                 type = LPFC_SOL_IOCB;
189                 break;
190         case CMD_ABORT_XRI_CN:
191         case CMD_ABORT_XRI_CX:
192         case CMD_CLOSE_XRI_CN:
193         case CMD_CLOSE_XRI_CX:
194         case CMD_XRI_ABORTED_CX:
195         case CMD_ABORT_MXRI64_CN:
196                 type = LPFC_ABORT_IOCB;
197                 break;
198         case CMD_RCV_SEQUENCE_CX:
199         case CMD_RCV_ELS_REQ_CX:
200         case CMD_RCV_SEQUENCE64_CX:
201         case CMD_RCV_ELS_REQ64_CX:
202         case CMD_ASYNC_STATUS:
203         case CMD_IOCB_RCV_SEQ64_CX:
204         case CMD_IOCB_RCV_ELS64_CX:
205         case CMD_IOCB_RCV_CONT64_CX:
206                 type = LPFC_UNSOL_IOCB;
207                 break;
208         default:
209                 type = LPFC_UNKNOWN_IOCB;
210                 break;
211         }
212
213         return type;
214 }
215
216 static int
217 lpfc_sli_ring_map(struct lpfc_hba *phba)
218 {
219         struct lpfc_sli *psli = &phba->sli;
220         LPFC_MBOXQ_t *pmb;
221         MAILBOX_t *pmbox;
222         int i, rc, ret = 0;
223
224         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
225         if (!pmb)
226                 return -ENOMEM;
227         pmbox = &pmb->mb;
228         phba->link_state = LPFC_INIT_MBX_CMDS;
229         for (i = 0; i < psli->num_rings; i++) {
230                 lpfc_config_ring(phba, i, pmb);
231                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
232                 if (rc != MBX_SUCCESS) {
233                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
234                                         "0446 Adapter failed to init (%d), "
235                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
236                                         "ring %d\n",
237                                         rc, pmbox->mbxCommand,
238                                         pmbox->mbxStatus, i);
239                         phba->link_state = LPFC_HBA_ERROR;
240                         ret = -ENXIO;
241                         break;
242                 }
243         }
244         mempool_free(pmb, phba->mbox_mem_pool);
245         return ret;
246 }
247
248 static int
249 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
250                         struct lpfc_iocbq *piocb)
251 {
252         list_add_tail(&piocb->list, &pring->txcmplq);
253         pring->txcmplq_cnt++;
254         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
255            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
256            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
257                 if (!piocb->vport)
258                         BUG();
259                 else
260                         mod_timer(&piocb->vport->els_tmofunc,
261                                   jiffies + HZ * (phba->fc_ratov << 1));
262         }
263
264
265         return 0;
266 }
267
268 static struct lpfc_iocbq *
269 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
270 {
271         struct lpfc_iocbq *cmd_iocb;
272
273         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
274         if (cmd_iocb != NULL)
275                 pring->txq_cnt--;
276         return cmd_iocb;
277 }
278
279 static IOCB_t *
280 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
281 {
282         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
283                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
284                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
285         uint32_t  max_cmd_idx = pring->numCiocb;
286
287         if ((pring->next_cmdidx == pring->cmdidx) &&
288            (++pring->next_cmdidx >= max_cmd_idx))
289                 pring->next_cmdidx = 0;
290
291         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
292
293                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
294
295                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
296                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
297                                         "0315 Ring %d issue: portCmdGet %d "
298                                         "is bigger then cmd ring %d\n",
299                                         pring->ringno,
300                                         pring->local_getidx, max_cmd_idx);
301
302                         phba->link_state = LPFC_HBA_ERROR;
303                         /*
304                          * All error attention handlers are posted to
305                          * worker thread
306                          */
307                         phba->work_ha |= HA_ERATT;
308                         phba->work_hs = HS_FFER3;
309
310                         /* hbalock should already be held */
311                         if (phba->work_wait)
312                                 lpfc_worker_wake_up(phba);
313
314                         return NULL;
315                 }
316
317                 if (pring->local_getidx == pring->next_cmdidx)
318                         return NULL;
319         }
320
321         return lpfc_cmd_iocb(phba, pring);
322 }
323
324 uint16_t
325 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
326 {
327         struct lpfc_iocbq **new_arr;
328         struct lpfc_iocbq **old_arr;
329         size_t new_len;
330         struct lpfc_sli *psli = &phba->sli;
331         uint16_t iotag;
332
333         spin_lock_irq(&phba->hbalock);
334         iotag = psli->last_iotag;
335         if(++iotag < psli->iocbq_lookup_len) {
336                 psli->last_iotag = iotag;
337                 psli->iocbq_lookup[iotag] = iocbq;
338                 spin_unlock_irq(&phba->hbalock);
339                 iocbq->iotag = iotag;
340                 return iotag;
341         } else if (psli->iocbq_lookup_len < (0xffff
342                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
343                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
344                 spin_unlock_irq(&phba->hbalock);
345                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
346                                   GFP_KERNEL);
347                 if (new_arr) {
348                         spin_lock_irq(&phba->hbalock);
349                         old_arr = psli->iocbq_lookup;
350                         if (new_len <= psli->iocbq_lookup_len) {
351                                 /* highly unprobable case */
352                                 kfree(new_arr);
353                                 iotag = psli->last_iotag;
354                                 if(++iotag < psli->iocbq_lookup_len) {
355                                         psli->last_iotag = iotag;
356                                         psli->iocbq_lookup[iotag] = iocbq;
357                                         spin_unlock_irq(&phba->hbalock);
358                                         iocbq->iotag = iotag;
359                                         return iotag;
360                                 }
361                                 spin_unlock_irq(&phba->hbalock);
362                                 return 0;
363                         }
364                         if (psli->iocbq_lookup)
365                                 memcpy(new_arr, old_arr,
366                                        ((psli->last_iotag  + 1) *
367                                         sizeof (struct lpfc_iocbq *)));
368                         psli->iocbq_lookup = new_arr;
369                         psli->iocbq_lookup_len = new_len;
370                         psli->last_iotag = iotag;
371                         psli->iocbq_lookup[iotag] = iocbq;
372                         spin_unlock_irq(&phba->hbalock);
373                         iocbq->iotag = iotag;
374                         kfree(old_arr);
375                         return iotag;
376                 }
377         } else
378                 spin_unlock_irq(&phba->hbalock);
379
380         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
381                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
382                         psli->last_iotag);
383
384         return 0;
385 }
386
387 static void
388 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
389                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
390 {
391         /*
392          * Set up an iotag
393          */
394         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
395
396         if (pring->ringno == LPFC_ELS_RING) {
397                 lpfc_debugfs_slow_ring_trc(phba,
398                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
399                         *(((uint32_t *) &nextiocb->iocb) + 4),
400                         *(((uint32_t *) &nextiocb->iocb) + 6),
401                         *(((uint32_t *) &nextiocb->iocb) + 7));
402         }
403
404         /*
405          * Issue iocb command to adapter
406          */
407         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
408         wmb();
409         pring->stats.iocb_cmd++;
410
411         /*
412          * If there is no completion routine to call, we can release the
413          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
414          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
415          */
416         if (nextiocb->iocb_cmpl)
417                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
418         else
419                 __lpfc_sli_release_iocbq(phba, nextiocb);
420
421         /*
422          * Let the HBA know what IOCB slot will be the next one the
423          * driver will put a command into.
424          */
425         pring->cmdidx = pring->next_cmdidx;
426         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
427 }
428
429 static void
430 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
431 {
432         int ringno = pring->ringno;
433
434         pring->flag |= LPFC_CALL_RING_AVAILABLE;
435
436         wmb();
437
438         /*
439          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
440          * The HBA will tell us when an IOCB entry is available.
441          */
442         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
443         readl(phba->CAregaddr); /* flush */
444
445         pring->stats.iocb_cmd_full++;
446 }
447
448 static void
449 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
450 {
451         int ringno = pring->ringno;
452
453         /*
454          * Tell the HBA that there is work to do in this ring.
455          */
456         wmb();
457         writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
458         readl(phba->CAregaddr); /* flush */
459 }
460
461 static void
462 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
463 {
464         IOCB_t *iocb;
465         struct lpfc_iocbq *nextiocb;
466
467         /*
468          * Check to see if:
469          *  (a) there is anything on the txq to send
470          *  (b) link is up
471          *  (c) link attention events can be processed (fcp ring only)
472          *  (d) IOCB processing is not blocked by the outstanding mbox command.
473          */
474         if (pring->txq_cnt &&
475             lpfc_is_link_up(phba) &&
476             (pring->ringno != phba->sli.fcp_ring ||
477              phba->sli.sli_flag & LPFC_PROCESS_LA) &&
478             !(pring->flag & LPFC_STOP_IOCB_MBX)) {
479
480                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
481                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
482                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
483
484                 if (iocb)
485                         lpfc_sli_update_ring(phba, pring);
486                 else
487                         lpfc_sli_update_full_ring(phba, pring);
488         }
489
490         return;
491 }
492
493 /* lpfc_sli_turn_on_ring is only called by lpfc_sli_handle_mb_event below */
494 static void
495 lpfc_sli_turn_on_ring(struct lpfc_hba *phba, int ringno)
496 {
497         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
498                 &phba->slim2p->mbx.us.s3_pgp.port[ringno] :
499                 &phba->slim2p->mbx.us.s2.port[ringno];
500         unsigned long iflags;
501
502         /* If the ring is active, flag it */
503         spin_lock_irqsave(&phba->hbalock, iflags);
504         if (phba->sli.ring[ringno].cmdringaddr) {
505                 if (phba->sli.ring[ringno].flag & LPFC_STOP_IOCB_MBX) {
506                         phba->sli.ring[ringno].flag &= ~LPFC_STOP_IOCB_MBX;
507                         /*
508                          * Force update of the local copy of cmdGetInx
509                          */
510                         phba->sli.ring[ringno].local_getidx
511                                 = le32_to_cpu(pgp->cmdGetInx);
512                         lpfc_sli_resume_iocb(phba, &phba->sli.ring[ringno]);
513                 }
514         }
515         spin_unlock_irqrestore(&phba->hbalock, iflags);
516 }
517
518 struct lpfc_hbq_entry *
519 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
520 {
521         struct hbq_s *hbqp = &phba->hbqs[hbqno];
522
523         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
524             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
525                 hbqp->next_hbqPutIdx = 0;
526
527         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
528                 uint32_t raw_index = phba->hbq_get[hbqno];
529                 uint32_t getidx = le32_to_cpu(raw_index);
530
531                 hbqp->local_hbqGetIdx = getidx;
532
533                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
534                         lpfc_printf_log(phba, KERN_ERR,
535                                         LOG_SLI | LOG_VPORT,
536                                         "1802 HBQ %d: local_hbqGetIdx "
537                                         "%u is > than hbqp->entry_count %u\n",
538                                         hbqno, hbqp->local_hbqGetIdx,
539                                         hbqp->entry_count);
540
541                         phba->link_state = LPFC_HBA_ERROR;
542                         return NULL;
543                 }
544
545                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
546                         return NULL;
547         }
548
549         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
550                         hbqp->hbqPutIdx;
551 }
552
553 void
554 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
555 {
556         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
557         struct hbq_dmabuf *hbq_buf;
558         int i, hbq_count;
559
560         hbq_count = lpfc_sli_hbq_count();
561         /* Return all memory used by all HBQs */
562         for (i = 0; i < hbq_count; ++i) {
563                 list_for_each_entry_safe(dmabuf, next_dmabuf,
564                                 &phba->hbqs[i].hbq_buffer_list, list) {
565                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
566                         list_del(&hbq_buf->dbuf.list);
567                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
568                 }
569         }
570 }
571
572 static struct lpfc_hbq_entry *
573 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
574                          struct hbq_dmabuf *hbq_buf)
575 {
576         struct lpfc_hbq_entry *hbqe;
577         dma_addr_t physaddr = hbq_buf->dbuf.phys;
578
579         /* Get next HBQ entry slot to use */
580         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
581         if (hbqe) {
582                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
583
584                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
585                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
586                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
587                 hbqe->bde.tus.f.bdeFlags = 0;
588                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
589                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
590                                 /* Sync SLIM */
591                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
592                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
593                                 /* flush */
594                 readl(phba->hbq_put + hbqno);
595                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
596         }
597         return hbqe;
598 }
599
600 static struct lpfc_hbq_init lpfc_els_hbq = {
601         .rn = 1,
602         .entry_count = 200,
603         .mask_count = 0,
604         .profile = 0,
605         .ring_mask = (1 << LPFC_ELS_RING),
606         .buffer_count = 0,
607         .init_count = 20,
608         .add_count = 5,
609 };
610
611 static struct lpfc_hbq_init lpfc_extra_hbq = {
612         .rn = 1,
613         .entry_count = 200,
614         .mask_count = 0,
615         .profile = 0,
616         .ring_mask = (1 << LPFC_EXTRA_RING),
617         .buffer_count = 0,
618         .init_count = 0,
619         .add_count = 5,
620 };
621
622 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
623         &lpfc_els_hbq,
624         &lpfc_extra_hbq,
625 };
626
627 static int
628 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
629 {
630         uint32_t i, start, end;
631         struct hbq_dmabuf *hbq_buffer;
632
633         if (!phba->hbqs[hbqno].hbq_alloc_buffer) {
634                 return 0;
635         }
636
637         start = lpfc_hbq_defs[hbqno]->buffer_count;
638         end = count + lpfc_hbq_defs[hbqno]->buffer_count;
639         if (end > lpfc_hbq_defs[hbqno]->entry_count) {
640                 end = lpfc_hbq_defs[hbqno]->entry_count;
641         }
642
643         /* Populate HBQ entries */
644         for (i = start; i < end; i++) {
645                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
646                 if (!hbq_buffer)
647                         return 1;
648                 hbq_buffer->tag = (i | (hbqno << 16));
649                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
650                         lpfc_hbq_defs[hbqno]->buffer_count++;
651                 else
652                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
653         }
654         return 0;
655 }
656
657 int
658 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
659 {
660         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
661                                          lpfc_hbq_defs[qno]->add_count));
662 }
663
664 int
665 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
666 {
667         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
668                                          lpfc_hbq_defs[qno]->init_count));
669 }
670
671 struct hbq_dmabuf *
672 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
673 {
674         struct lpfc_dmabuf *d_buf;
675         struct hbq_dmabuf *hbq_buf;
676         uint32_t hbqno;
677
678         hbqno = tag >> 16;
679         if (hbqno >= LPFC_MAX_HBQS)
680                 return NULL;
681
682         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
683                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
684                 if (hbq_buf->tag == tag) {
685                         return hbq_buf;
686                 }
687         }
688         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
689                         "1803 Bad hbq tag. Data: x%x x%x\n",
690                         tag, lpfc_hbq_defs[tag >> 16]->buffer_count);
691         return NULL;
692 }
693
694 void
695 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
696 {
697         uint32_t hbqno;
698
699         if (hbq_buffer) {
700                 hbqno = hbq_buffer->tag >> 16;
701                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
702                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
703                 }
704         }
705 }
706
707 static int
708 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
709 {
710         uint8_t ret;
711
712         switch (mbxCommand) {
713         case MBX_LOAD_SM:
714         case MBX_READ_NV:
715         case MBX_WRITE_NV:
716         case MBX_RUN_BIU_DIAG:
717         case MBX_INIT_LINK:
718         case MBX_DOWN_LINK:
719         case MBX_CONFIG_LINK:
720         case MBX_CONFIG_RING:
721         case MBX_RESET_RING:
722         case MBX_READ_CONFIG:
723         case MBX_READ_RCONFIG:
724         case MBX_READ_SPARM:
725         case MBX_READ_STATUS:
726         case MBX_READ_RPI:
727         case MBX_READ_XRI:
728         case MBX_READ_REV:
729         case MBX_READ_LNK_STAT:
730         case MBX_REG_LOGIN:
731         case MBX_UNREG_LOGIN:
732         case MBX_READ_LA:
733         case MBX_CLEAR_LA:
734         case MBX_DUMP_MEMORY:
735         case MBX_DUMP_CONTEXT:
736         case MBX_RUN_DIAGS:
737         case MBX_RESTART:
738         case MBX_UPDATE_CFG:
739         case MBX_DOWN_LOAD:
740         case MBX_DEL_LD_ENTRY:
741         case MBX_RUN_PROGRAM:
742         case MBX_SET_MASK:
743         case MBX_SET_SLIM:
744         case MBX_UNREG_D_ID:
745         case MBX_KILL_BOARD:
746         case MBX_CONFIG_FARP:
747         case MBX_BEACON:
748         case MBX_LOAD_AREA:
749         case MBX_RUN_BIU_DIAG64:
750         case MBX_CONFIG_PORT:
751         case MBX_READ_SPARM64:
752         case MBX_READ_RPI64:
753         case MBX_REG_LOGIN64:
754         case MBX_READ_LA64:
755         case MBX_FLASH_WR_ULA:
756         case MBX_SET_DEBUG:
757         case MBX_LOAD_EXP_ROM:
758         case MBX_ASYNCEVT_ENABLE:
759         case MBX_REG_VPI:
760         case MBX_UNREG_VPI:
761         case MBX_HEARTBEAT:
762                 ret = mbxCommand;
763                 break;
764         default:
765                 ret = MBX_SHUTDOWN;
766                 break;
767         }
768         return ret;
769 }
770 static void
771 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
772 {
773         wait_queue_head_t *pdone_q;
774         unsigned long drvr_flag;
775
776         /*
777          * If pdone_q is empty, the driver thread gave up waiting and
778          * continued running.
779          */
780         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
781         spin_lock_irqsave(&phba->hbalock, drvr_flag);
782         pdone_q = (wait_queue_head_t *) pmboxq->context1;
783         if (pdone_q)
784                 wake_up_interruptible(pdone_q);
785         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
786         return;
787 }
788
789 void
790 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
791 {
792         struct lpfc_dmabuf *mp;
793         uint16_t rpi;
794         int rc;
795
796         mp = (struct lpfc_dmabuf *) (pmb->context1);
797
798         if (mp) {
799                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
800                 kfree(mp);
801         }
802
803         /*
804          * If a REG_LOGIN succeeded  after node is destroyed or node
805          * is in re-discovery driver need to cleanup the RPI.
806          */
807         if (!(phba->pport->load_flag & FC_UNLOADING) &&
808             pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
809             !pmb->mb.mbxStatus) {
810
811                 rpi = pmb->mb.un.varWords[0];
812                 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
813                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
814                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
815                 if (rc != MBX_NOT_FINISHED)
816                         return;
817         }
818
819         mempool_free(pmb, phba->mbox_mem_pool);
820         return;
821 }
822
823 int
824 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
825 {
826         MAILBOX_t *pmbox;
827         LPFC_MBOXQ_t *pmb;
828         int rc;
829         LIST_HEAD(cmplq);
830
831         phba->sli.slistat.mbox_event++;
832
833         /* Get all completed mailboxe buffers into the cmplq */
834         spin_lock_irq(&phba->hbalock);
835         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
836         spin_unlock_irq(&phba->hbalock);
837
838         /* Get a Mailbox buffer to setup mailbox commands for callback */
839         do {
840                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
841                 if (pmb == NULL)
842                         break;
843
844                 pmbox = &pmb->mb;
845
846                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
847                         if (pmb->vport) {
848                                 lpfc_debugfs_disc_trc(pmb->vport,
849                                         LPFC_DISC_TRC_MBOX_VPORT,
850                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
851                                         (uint32_t)pmbox->mbxCommand,
852                                         pmbox->un.varWords[0],
853                                         pmbox->un.varWords[1]);
854                         }
855                         else {
856                                 lpfc_debugfs_disc_trc(phba->pport,
857                                         LPFC_DISC_TRC_MBOX,
858                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
859                                         (uint32_t)pmbox->mbxCommand,
860                                         pmbox->un.varWords[0],
861                                         pmbox->un.varWords[1]);
862                         }
863                 }
864
865                 /*
866                  * It is a fatal error if unknown mbox command completion.
867                  */
868                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
869                     MBX_SHUTDOWN) {
870                         /* Unknow mailbox command compl */
871                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
872                                         "(%d):0323 Unknown Mailbox command "
873                                         "%x Cmpl\n",
874                                         pmb->vport ? pmb->vport->vpi : 0,
875                                         pmbox->mbxCommand);
876                         phba->link_state = LPFC_HBA_ERROR;
877                         phba->work_hs = HS_FFER3;
878                         lpfc_handle_eratt(phba);
879                         continue;
880                 }
881
882                 if (pmbox->mbxStatus) {
883                         phba->sli.slistat.mbox_stat_err++;
884                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
885                                 /* Mbox cmd cmpl error - RETRYing */
886                                 lpfc_printf_log(phba, KERN_INFO,
887                                                 LOG_MBOX | LOG_SLI,
888                                                 "(%d):0305 Mbox cmd cmpl "
889                                                 "error - RETRYing Data: x%x "
890                                                 "x%x x%x x%x\n",
891                                                 pmb->vport ? pmb->vport->vpi :0,
892                                                 pmbox->mbxCommand,
893                                                 pmbox->mbxStatus,
894                                                 pmbox->un.varWords[0],
895                                                 pmb->vport->port_state);
896                                 pmbox->mbxStatus = 0;
897                                 pmbox->mbxOwner = OWN_HOST;
898                                 spin_lock_irq(&phba->hbalock);
899                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
900                                 spin_unlock_irq(&phba->hbalock);
901                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
902                                 if (rc == MBX_SUCCESS)
903                                         continue;
904                         }
905                 }
906
907                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
908                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
909                                 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
910                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
911                                 pmb->vport ? pmb->vport->vpi : 0,
912                                 pmbox->mbxCommand,
913                                 pmb->mbox_cmpl,
914                                 *((uint32_t *) pmbox),
915                                 pmbox->un.varWords[0],
916                                 pmbox->un.varWords[1],
917                                 pmbox->un.varWords[2],
918                                 pmbox->un.varWords[3],
919                                 pmbox->un.varWords[4],
920                                 pmbox->un.varWords[5],
921                                 pmbox->un.varWords[6],
922                                 pmbox->un.varWords[7]);
923
924                 if (pmb->mbox_cmpl)
925                         pmb->mbox_cmpl(phba,pmb);
926         } while (1);
927         return 0;
928 }
929
930 static struct lpfc_dmabuf *
931 lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
932 {
933         struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
934         uint32_t hbqno;
935         void *virt;             /* virtual address ptr */
936         dma_addr_t phys;        /* mapped address */
937
938         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
939         if (hbq_entry == NULL)
940                 return NULL;
941         list_del(&hbq_entry->dbuf.list);
942
943         hbqno = tag >> 16;
944         new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
945         if (new_hbq_entry == NULL)
946                 return &hbq_entry->dbuf;
947         new_hbq_entry->tag = -1;
948         phys = new_hbq_entry->dbuf.phys;
949         virt = new_hbq_entry->dbuf.virt;
950         new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
951         new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
952         hbq_entry->dbuf.phys = phys;
953         hbq_entry->dbuf.virt = virt;
954         lpfc_sli_free_hbq(phba, hbq_entry);
955         return &new_hbq_entry->dbuf;
956 }
957
958
959 static int
960 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
961                             struct lpfc_iocbq *saveq)
962 {
963         IOCB_t           * irsp;
964         WORD5            * w5p;
965         uint32_t           Rctl, Type;
966         uint32_t           match, i;
967
968         match = 0;
969         irsp = &(saveq->iocb);
970
971         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
972                 if (pring->lpfc_sli_rcv_async_status)
973                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
974                 else
975                         lpfc_printf_log(phba,
976                                         KERN_WARNING,
977                                         LOG_SLI,
978                                         "0316 Ring %d handler: unexpected "
979                                         "ASYNC_STATUS iocb received evt_code "
980                                         "0x%x\n",
981                                         pring->ringno,
982                                         irsp->un.asyncstat.evt_code);
983                 return 1;
984         }
985
986         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX)
987             || (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX)
988             || (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)
989             || (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX)) {
990                 Rctl = FC_ELS_REQ;
991                 Type = FC_ELS_DATA;
992         } else {
993                 w5p =
994                     (WORD5 *) & (saveq->iocb.un.
995                                  ulpWord[5]);
996                 Rctl = w5p->hcsw.Rctl;
997                 Type = w5p->hcsw.Type;
998
999                 /* Firmware Workaround */
1000                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1001                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1002                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1003                         Rctl = FC_ELS_REQ;
1004                         Type = FC_ELS_DATA;
1005                         w5p->hcsw.Rctl = Rctl;
1006                         w5p->hcsw.Type = Type;
1007                 }
1008         }
1009
1010         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1011                 if (irsp->ulpBdeCount != 0)
1012                         saveq->context2 = lpfc_sli_replace_hbqbuff(phba,
1013                                                 irsp->un.ulpWord[3]);
1014                 if (irsp->ulpBdeCount == 2)
1015                         saveq->context3 = lpfc_sli_replace_hbqbuff(phba,
1016                                                 irsp->unsli3.sli3Words[7]);
1017         }
1018
1019         /* unSolicited Responses */
1020         if (pring->prt[0].profile) {
1021                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1022                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1023                                                                         saveq);
1024                 match = 1;
1025         } else {
1026                 /* We must search, based on rctl / type
1027                    for the right routine */
1028                 for (i = 0; i < pring->num_mask;
1029                      i++) {
1030                         if ((pring->prt[i].rctl ==
1031                              Rctl)
1032                             && (pring->prt[i].
1033                                 type == Type)) {
1034                                 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1035                                         (pring->prt[i].lpfc_sli_rcv_unsol_event)
1036                                                         (phba, pring, saveq);
1037                                 match = 1;
1038                                 break;
1039                         }
1040                 }
1041         }
1042         if (match == 0) {
1043                 /* Unexpected Rctl / Type received */
1044                 /* Ring <ringno> handler: unexpected
1045                    Rctl <Rctl> Type <Type> received */
1046                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1047                                 "0313 Ring %d handler: unexpected Rctl x%x "
1048                                 "Type x%x received\n",
1049                                 pring->ringno, Rctl, Type);
1050         }
1051         return 1;
1052 }
1053
1054 static struct lpfc_iocbq *
1055 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1056                       struct lpfc_sli_ring *pring,
1057                       struct lpfc_iocbq *prspiocb)
1058 {
1059         struct lpfc_iocbq *cmd_iocb = NULL;
1060         uint16_t iotag;
1061
1062         iotag = prspiocb->iocb.ulpIoTag;
1063
1064         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1065                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
1066                 list_del_init(&cmd_iocb->list);
1067                 pring->txcmplq_cnt--;
1068                 return cmd_iocb;
1069         }
1070
1071         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1072                         "0317 iotag x%x is out off "
1073                         "range: max iotag x%x wd0 x%x\n",
1074                         iotag, phba->sli.last_iotag,
1075                         *(((uint32_t *) &prspiocb->iocb) + 7));
1076         return NULL;
1077 }
1078
1079 static int
1080 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1081                           struct lpfc_iocbq *saveq)
1082 {
1083         struct lpfc_iocbq *cmdiocbp;
1084         int rc = 1;
1085         unsigned long iflag;
1086
1087         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
1088         spin_lock_irqsave(&phba->hbalock, iflag);
1089         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
1090         spin_unlock_irqrestore(&phba->hbalock, iflag);
1091
1092         if (cmdiocbp) {
1093                 if (cmdiocbp->iocb_cmpl) {
1094                         /*
1095                          * Post all ELS completions to the worker thread.
1096                          * All other are passed to the completion callback.
1097                          */
1098                         if (pring->ringno == LPFC_ELS_RING) {
1099                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1100                                         cmdiocbp->iocb_flag &=
1101                                                 ~LPFC_DRIVER_ABORTED;
1102                                         saveq->iocb.ulpStatus =
1103                                                 IOSTAT_LOCAL_REJECT;
1104                                         saveq->iocb.un.ulpWord[4] =
1105                                                 IOERR_SLI_ABORTED;
1106                                 }
1107                         }
1108                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
1109                 } else
1110                         lpfc_sli_release_iocbq(phba, cmdiocbp);
1111         } else {
1112                 /*
1113                  * Unknown initiating command based on the response iotag.
1114                  * This could be the case on the ELS ring because of
1115                  * lpfc_els_abort().
1116                  */
1117                 if (pring->ringno != LPFC_ELS_RING) {
1118                         /*
1119                          * Ring <ringno> handler: unexpected completion IoTag
1120                          * <IoTag>
1121                          */
1122                         lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1123                                          "0322 Ring %d handler: "
1124                                          "unexpected completion IoTag x%x "
1125                                          "Data: x%x x%x x%x x%x\n",
1126                                          pring->ringno,
1127                                          saveq->iocb.ulpIoTag,
1128                                          saveq->iocb.ulpStatus,
1129                                          saveq->iocb.un.ulpWord[4],
1130                                          saveq->iocb.ulpCommand,
1131                                          saveq->iocb.ulpContext);
1132                 }
1133         }
1134
1135         return rc;
1136 }
1137
1138 static void
1139 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1140 {
1141         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1142                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1143                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1144         /*
1145          * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1146          * rsp ring <portRspMax>
1147          */
1148         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1149                         "0312 Ring %d handler: portRspPut %d "
1150                         "is bigger then rsp ring %d\n",
1151                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
1152                         pring->numRiocb);
1153
1154         phba->link_state = LPFC_HBA_ERROR;
1155
1156         /*
1157          * All error attention handlers are posted to
1158          * worker thread
1159          */
1160         phba->work_ha |= HA_ERATT;
1161         phba->work_hs = HS_FFER3;
1162
1163         /* hbalock should already be held */
1164         if (phba->work_wait)
1165                 lpfc_worker_wake_up(phba);
1166
1167         return;
1168 }
1169
1170 void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
1171 {
1172         struct lpfc_sli      *psli  = &phba->sli;
1173         struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1174         IOCB_t *irsp = NULL;
1175         IOCB_t *entry = NULL;
1176         struct lpfc_iocbq *cmdiocbq = NULL;
1177         struct lpfc_iocbq rspiocbq;
1178         struct lpfc_pgp *pgp;
1179         uint32_t status;
1180         uint32_t portRspPut, portRspMax;
1181         int type;
1182         uint32_t rsp_cmpl = 0;
1183         uint32_t ha_copy;
1184         unsigned long iflags;
1185
1186         pring->stats.iocb_event++;
1187
1188         pgp = (phba->sli_rev == 3) ?
1189                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1190                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1191
1192
1193         /*
1194          * The next available response entry should never exceed the maximum
1195          * entries.  If it does, treat it as an adapter hardware error.
1196          */
1197         portRspMax = pring->numRiocb;
1198         portRspPut = le32_to_cpu(pgp->rspPutInx);
1199         if (unlikely(portRspPut >= portRspMax)) {
1200                 lpfc_sli_rsp_pointers_error(phba, pring);
1201                 return;
1202         }
1203
1204         rmb();
1205         while (pring->rspidx != portRspPut) {
1206                 entry = lpfc_resp_iocb(phba, pring);
1207                 if (++pring->rspidx >= portRspMax)
1208                         pring->rspidx = 0;
1209
1210                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1211                                       (uint32_t *) &rspiocbq.iocb,
1212                                       phba->iocb_rsp_size);
1213                 irsp = &rspiocbq.iocb;
1214                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1215                 pring->stats.iocb_rsp++;
1216                 rsp_cmpl++;
1217
1218                 if (unlikely(irsp->ulpStatus)) {
1219                         /* Rsp ring <ringno> error: IOCB */
1220                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1221                                         "0326 Rsp Ring %d error: IOCB Data: "
1222                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1223                                         pring->ringno,
1224                                         irsp->un.ulpWord[0],
1225                                         irsp->un.ulpWord[1],
1226                                         irsp->un.ulpWord[2],
1227                                         irsp->un.ulpWord[3],
1228                                         irsp->un.ulpWord[4],
1229                                         irsp->un.ulpWord[5],
1230                                         *(((uint32_t *) irsp) + 6),
1231                                         *(((uint32_t *) irsp) + 7));
1232                 }
1233
1234                 switch (type) {
1235                 case LPFC_ABORT_IOCB:
1236                 case LPFC_SOL_IOCB:
1237                         /*
1238                          * Idle exchange closed via ABTS from port.  No iocb
1239                          * resources need to be recovered.
1240                          */
1241                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1242                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1243                                                 "0314 IOCB cmd 0x%x "
1244                                                 "processed. Skipping "
1245                                                 "completion",
1246                                                 irsp->ulpCommand);
1247                                 break;
1248                         }
1249
1250                         spin_lock_irqsave(&phba->hbalock, iflags);
1251                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1252                                                          &rspiocbq);
1253                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1254                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1255                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1256                                                       &rspiocbq);
1257                         }
1258                         break;
1259                 default:
1260                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1261                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1262                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1263                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1264                                        MAX_MSG_DATA);
1265                                 dev_warn(&((phba->pcidev)->dev),
1266                                          "lpfc%d: %s\n",
1267                                          phba->brd_no, adaptermsg);
1268                         } else {
1269                                 /* Unknown IOCB command */
1270                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1271                                                 "0321 Unknown IOCB command "
1272                                                 "Data: x%x, x%x x%x x%x x%x\n",
1273                                                 type, irsp->ulpCommand,
1274                                                 irsp->ulpStatus,
1275                                                 irsp->ulpIoTag,
1276                                                 irsp->ulpContext);
1277                         }
1278                         break;
1279                 }
1280
1281                 /*
1282                  * The response IOCB has been processed.  Update the ring
1283                  * pointer in SLIM.  If the port response put pointer has not
1284                  * been updated, sync the pgp->rspPutInx and fetch the new port
1285                  * response put pointer.
1286                  */
1287                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1288
1289                 if (pring->rspidx == portRspPut)
1290                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1291         }
1292
1293         ha_copy = readl(phba->HAregaddr);
1294         ha_copy >>= (LPFC_FCP_RING * 4);
1295
1296         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1297                 spin_lock_irqsave(&phba->hbalock, iflags);
1298                 pring->stats.iocb_rsp_full++;
1299                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1300                 writel(status, phba->CAregaddr);
1301                 readl(phba->CAregaddr);
1302                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1303         }
1304         if ((ha_copy & HA_R0CE_RSP) &&
1305             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1306                 spin_lock_irqsave(&phba->hbalock, iflags);
1307                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1308                 pring->stats.iocb_cmd_empty++;
1309
1310                 /* Force update of the local copy of cmdGetInx */
1311                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1312                 lpfc_sli_resume_iocb(phba, pring);
1313
1314                 if ((pring->lpfc_sli_cmd_available))
1315                         (pring->lpfc_sli_cmd_available) (phba, pring);
1316
1317                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1318         }
1319
1320         return;
1321 }
1322
1323 /*
1324  * This routine presumes LPFC_FCP_RING handling and doesn't bother
1325  * to check it explicitly.
1326  */
1327 static int
1328 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1329                                 struct lpfc_sli_ring *pring, uint32_t mask)
1330 {
1331         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1332                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1333                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1334         IOCB_t *irsp = NULL;
1335         IOCB_t *entry = NULL;
1336         struct lpfc_iocbq *cmdiocbq = NULL;
1337         struct lpfc_iocbq rspiocbq;
1338         uint32_t status;
1339         uint32_t portRspPut, portRspMax;
1340         int rc = 1;
1341         lpfc_iocb_type type;
1342         unsigned long iflag;
1343         uint32_t rsp_cmpl = 0;
1344
1345         spin_lock_irqsave(&phba->hbalock, iflag);
1346         pring->stats.iocb_event++;
1347
1348         /*
1349          * The next available response entry should never exceed the maximum
1350          * entries.  If it does, treat it as an adapter hardware error.
1351          */
1352         portRspMax = pring->numRiocb;
1353         portRspPut = le32_to_cpu(pgp->rspPutInx);
1354         if (unlikely(portRspPut >= portRspMax)) {
1355                 lpfc_sli_rsp_pointers_error(phba, pring);
1356                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1357                 return 1;
1358         }
1359
1360         rmb();
1361         while (pring->rspidx != portRspPut) {
1362                 /*
1363                  * Fetch an entry off the ring and copy it into a local data
1364                  * structure.  The copy involves a byte-swap since the
1365                  * network byte order and pci byte orders are different.
1366                  */
1367                 entry = lpfc_resp_iocb(phba, pring);
1368                 phba->last_completion_time = jiffies;
1369
1370                 if (++pring->rspidx >= portRspMax)
1371                         pring->rspidx = 0;
1372
1373                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1374                                       (uint32_t *) &rspiocbq.iocb,
1375                                       phba->iocb_rsp_size);
1376                 INIT_LIST_HEAD(&(rspiocbq.list));
1377                 irsp = &rspiocbq.iocb;
1378
1379                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1380                 pring->stats.iocb_rsp++;
1381                 rsp_cmpl++;
1382
1383                 if (unlikely(irsp->ulpStatus)) {
1384                         /*
1385                          * If resource errors reported from HBA, reduce
1386                          * queuedepths of the SCSI device.
1387                          */
1388                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1389                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1390                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1391                                 lpfc_adjust_queue_depth(phba);
1392                                 spin_lock_irqsave(&phba->hbalock, iflag);
1393                         }
1394
1395                         /* Rsp ring <ringno> error: IOCB */
1396                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1397                                         "0336 Rsp Ring %d error: IOCB Data: "
1398                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1399                                         pring->ringno,
1400                                         irsp->un.ulpWord[0],
1401                                         irsp->un.ulpWord[1],
1402                                         irsp->un.ulpWord[2],
1403                                         irsp->un.ulpWord[3],
1404                                         irsp->un.ulpWord[4],
1405                                         irsp->un.ulpWord[5],
1406                                         *(((uint32_t *) irsp) + 6),
1407                                         *(((uint32_t *) irsp) + 7));
1408                 }
1409
1410                 switch (type) {
1411                 case LPFC_ABORT_IOCB:
1412                 case LPFC_SOL_IOCB:
1413                         /*
1414                          * Idle exchange closed via ABTS from port.  No iocb
1415                          * resources need to be recovered.
1416                          */
1417                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1418                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1419                                                 "0333 IOCB cmd 0x%x"
1420                                                 " processed. Skipping"
1421                                                 " completion\n",
1422                                                 irsp->ulpCommand);
1423                                 break;
1424                         }
1425
1426                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1427                                                          &rspiocbq);
1428                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1429                                 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1430                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1431                                                               &rspiocbq);
1432                                 } else {
1433                                         spin_unlock_irqrestore(&phba->hbalock,
1434                                                                iflag);
1435                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1436                                                               &rspiocbq);
1437                                         spin_lock_irqsave(&phba->hbalock,
1438                                                           iflag);
1439                                 }
1440                         }
1441                         break;
1442                 case LPFC_UNSOL_IOCB:
1443                         spin_unlock_irqrestore(&phba->hbalock, iflag);
1444                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
1445                         spin_lock_irqsave(&phba->hbalock, iflag);
1446                         break;
1447                 default:
1448                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1449                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1450                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1451                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1452                                        MAX_MSG_DATA);
1453                                 dev_warn(&((phba->pcidev)->dev),
1454                                          "lpfc%d: %s\n",
1455                                          phba->brd_no, adaptermsg);
1456                         } else {
1457                                 /* Unknown IOCB command */
1458                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1459                                                 "0334 Unknown IOCB command "
1460                                                 "Data: x%x, x%x x%x x%x x%x\n",
1461                                                 type, irsp->ulpCommand,
1462                                                 irsp->ulpStatus,
1463                                                 irsp->ulpIoTag,
1464                                                 irsp->ulpContext);
1465                         }
1466                         break;
1467                 }
1468
1469                 /*
1470                  * The response IOCB has been processed.  Update the ring
1471                  * pointer in SLIM.  If the port response put pointer has not
1472                  * been updated, sync the pgp->rspPutInx and fetch the new port
1473                  * response put pointer.
1474                  */
1475                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1476
1477                 if (pring->rspidx == portRspPut)
1478                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1479         }
1480
1481         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
1482                 pring->stats.iocb_rsp_full++;
1483                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1484                 writel(status, phba->CAregaddr);
1485                 readl(phba->CAregaddr);
1486         }
1487         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1488                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1489                 pring->stats.iocb_cmd_empty++;
1490
1491                 /* Force update of the local copy of cmdGetInx */
1492                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1493                 lpfc_sli_resume_iocb(phba, pring);
1494
1495                 if ((pring->lpfc_sli_cmd_available))
1496                         (pring->lpfc_sli_cmd_available) (phba, pring);
1497
1498         }
1499
1500         spin_unlock_irqrestore(&phba->hbalock, iflag);
1501         return rc;
1502 }
1503
1504 int
1505 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
1506                                 struct lpfc_sli_ring *pring, uint32_t mask)
1507 {
1508         struct lpfc_pgp *pgp = (phba->sli_rev == 3) ?
1509                 &phba->slim2p->mbx.us.s3_pgp.port[pring->ringno] :
1510                 &phba->slim2p->mbx.us.s2.port[pring->ringno];
1511         IOCB_t *entry;
1512         IOCB_t *irsp = NULL;
1513         struct lpfc_iocbq *rspiocbp = NULL;
1514         struct lpfc_iocbq *next_iocb;
1515         struct lpfc_iocbq *cmdiocbp;
1516         struct lpfc_iocbq *saveq;
1517         uint8_t iocb_cmd_type;
1518         lpfc_iocb_type type;
1519         uint32_t status, free_saveq;
1520         uint32_t portRspPut, portRspMax;
1521         int rc = 1;
1522         unsigned long iflag;
1523
1524         spin_lock_irqsave(&phba->hbalock, iflag);
1525         pring->stats.iocb_event++;
1526
1527         /*
1528          * The next available response entry should never exceed the maximum
1529          * entries.  If it does, treat it as an adapter hardware error.
1530          */
1531         portRspMax = pring->numRiocb;
1532         portRspPut = le32_to_cpu(pgp->rspPutInx);
1533         if (portRspPut >= portRspMax) {
1534                 /*
1535                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1536                  * rsp ring <portRspMax>
1537                  */
1538                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1539                                 "0303 Ring %d handler: portRspPut %d "
1540                                 "is bigger then rsp ring %d\n",
1541                                 pring->ringno, portRspPut, portRspMax);
1542
1543                 phba->link_state = LPFC_HBA_ERROR;
1544                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1545
1546                 phba->work_hs = HS_FFER3;
1547                 lpfc_handle_eratt(phba);
1548
1549                 return 1;
1550         }
1551
1552         rmb();
1553         while (pring->rspidx != portRspPut) {
1554                 /*
1555                  * Build a completion list and call the appropriate handler.
1556                  * The process is to get the next available response iocb, get
1557                  * a free iocb from the list, copy the response data into the
1558                  * free iocb, insert to the continuation list, and update the
1559                  * next response index to slim.  This process makes response
1560                  * iocb's in the ring available to DMA as fast as possible but
1561                  * pays a penalty for a copy operation.  Since the iocb is
1562                  * only 32 bytes, this penalty is considered small relative to
1563                  * the PCI reads for register values and a slim write.  When
1564                  * the ulpLe field is set, the entire Command has been
1565                  * received.
1566                  */
1567                 entry = lpfc_resp_iocb(phba, pring);
1568
1569                 phba->last_completion_time = jiffies;
1570                 rspiocbp = __lpfc_sli_get_iocbq(phba);
1571                 if (rspiocbp == NULL) {
1572                         printk(KERN_ERR "%s: out of buffers! Failing "
1573                                "completion.\n", __FUNCTION__);
1574                         break;
1575                 }
1576
1577                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
1578                                       phba->iocb_rsp_size);
1579                 irsp = &rspiocbp->iocb;
1580
1581                 if (++pring->rspidx >= portRspMax)
1582                         pring->rspidx = 0;
1583
1584                 if (pring->ringno == LPFC_ELS_RING) {
1585                         lpfc_debugfs_slow_ring_trc(phba,
1586                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1587                                 *(((uint32_t *) irsp) + 4),
1588                                 *(((uint32_t *) irsp) + 6),
1589                                 *(((uint32_t *) irsp) + 7));
1590                 }
1591
1592                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1593
1594                 if (list_empty(&(pring->iocb_continueq))) {
1595                         list_add(&rspiocbp->list, &(pring->iocb_continueq));
1596                 } else {
1597                         list_add_tail(&rspiocbp->list,
1598                                       &(pring->iocb_continueq));
1599                 }
1600
1601                 pring->iocb_continueq_cnt++;
1602                 if (irsp->ulpLe) {
1603                         /*
1604                          * By default, the driver expects to free all resources
1605                          * associated with this iocb completion.
1606                          */
1607                         free_saveq = 1;
1608                         saveq = list_get_first(&pring->iocb_continueq,
1609                                                struct lpfc_iocbq, list);
1610                         irsp = &(saveq->iocb);
1611                         list_del_init(&pring->iocb_continueq);
1612                         pring->iocb_continueq_cnt = 0;
1613
1614                         pring->stats.iocb_rsp++;
1615
1616                         /*
1617                          * If resource errors reported from HBA, reduce
1618                          * queuedepths of the SCSI device.
1619                          */
1620                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1621                              (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1622                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1623                                 lpfc_adjust_queue_depth(phba);
1624                                 spin_lock_irqsave(&phba->hbalock, iflag);
1625                         }
1626
1627                         if (irsp->ulpStatus) {
1628                                 /* Rsp ring <ringno> error: IOCB */
1629                                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1630                                                 "0328 Rsp Ring %d error: "
1631                                                 "IOCB Data: "
1632                                                 "x%x x%x x%x x%x "
1633                                                 "x%x x%x x%x x%x "
1634                                                 "x%x x%x x%x x%x "
1635                                                 "x%x x%x x%x x%x\n",
1636                                                 pring->ringno,
1637                                                 irsp->un.ulpWord[0],
1638                                                 irsp->un.ulpWord[1],
1639                                                 irsp->un.ulpWord[2],
1640                                                 irsp->un.ulpWord[3],
1641                                                 irsp->un.ulpWord[4],
1642                                                 irsp->un.ulpWord[5],
1643                                                 *(((uint32_t *) irsp) + 6),
1644                                                 *(((uint32_t *) irsp) + 7),
1645                                                 *(((uint32_t *) irsp) + 8),
1646                                                 *(((uint32_t *) irsp) + 9),
1647                                                 *(((uint32_t *) irsp) + 10),
1648                                                 *(((uint32_t *) irsp) + 11),
1649                                                 *(((uint32_t *) irsp) + 12),
1650                                                 *(((uint32_t *) irsp) + 13),
1651                                                 *(((uint32_t *) irsp) + 14),
1652                                                 *(((uint32_t *) irsp) + 15));
1653                         }
1654
1655                         /*
1656                          * Fetch the IOCB command type and call the correct
1657                          * completion routine.  Solicited and Unsolicited
1658                          * IOCBs on the ELS ring get freed back to the
1659                          * lpfc_iocb_list by the discovery kernel thread.
1660                          */
1661                         iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
1662                         type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
1663                         if (type == LPFC_SOL_IOCB) {
1664                                 spin_unlock_irqrestore(&phba->hbalock,
1665                                                        iflag);
1666                                 rc = lpfc_sli_process_sol_iocb(phba, pring,
1667                                                                saveq);
1668                                 spin_lock_irqsave(&phba->hbalock, iflag);
1669                         } else if (type == LPFC_UNSOL_IOCB) {
1670                                 spin_unlock_irqrestore(&phba->hbalock,
1671                                                        iflag);
1672                                 rc = lpfc_sli_process_unsol_iocb(phba, pring,
1673                                                                  saveq);
1674                                 spin_lock_irqsave(&phba->hbalock, iflag);
1675                         } else if (type == LPFC_ABORT_IOCB) {
1676                                 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
1677                                     ((cmdiocbp =
1678                                       lpfc_sli_iocbq_lookup(phba, pring,
1679                                                             saveq)))) {
1680                                         /* Call the specified completion
1681                                            routine */
1682                                         if (cmdiocbp->iocb_cmpl) {
1683                                                 spin_unlock_irqrestore(
1684                                                        &phba->hbalock,
1685                                                        iflag);
1686                                                 (cmdiocbp->iocb_cmpl) (phba,
1687                                                              cmdiocbp, saveq);
1688                                                 spin_lock_irqsave(
1689                                                           &phba->hbalock,
1690                                                           iflag);
1691                                         } else
1692                                                 __lpfc_sli_release_iocbq(phba,
1693                                                                       cmdiocbp);
1694                                 }
1695                         } else if (type == LPFC_UNKNOWN_IOCB) {
1696                                 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1697
1698                                         char adaptermsg[LPFC_MAX_ADPTMSG];
1699
1700                                         memset(adaptermsg, 0,
1701                                                LPFC_MAX_ADPTMSG);
1702                                         memcpy(&adaptermsg[0], (uint8_t *) irsp,
1703                                                MAX_MSG_DATA);
1704                                         dev_warn(&((phba->pcidev)->dev),
1705                                                  "lpfc%d: %s\n",
1706                                                  phba->brd_no, adaptermsg);
1707                                 } else {
1708                                         /* Unknown IOCB command */
1709                                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1710                                                         "0335 Unknown IOCB "
1711                                                         "command Data: x%x "
1712                                                         "x%x x%x x%x\n",
1713                                                         irsp->ulpCommand,
1714                                                         irsp->ulpStatus,
1715                                                         irsp->ulpIoTag,
1716                                                         irsp->ulpContext);
1717                                 }
1718                         }
1719
1720                         if (free_saveq) {
1721                                 list_for_each_entry_safe(rspiocbp, next_iocb,
1722                                                          &saveq->list, list) {
1723                                         list_del(&rspiocbp->list);
1724                                         __lpfc_sli_release_iocbq(phba,
1725                                                                  rspiocbp);
1726                                 }
1727                                 __lpfc_sli_release_iocbq(phba, saveq);
1728                         }
1729                         rspiocbp = NULL;
1730                 }
1731
1732                 /*
1733                  * If the port response put pointer has not been updated, sync
1734                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
1735                  * response put pointer.
1736                  */
1737                 if (pring->rspidx == portRspPut) {
1738                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1739                 }
1740         } /* while (pring->rspidx != portRspPut) */
1741
1742         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
1743                 /* At least one response entry has been freed */
1744                 pring->stats.iocb_rsp_full++;
1745                 /* SET RxRE_RSP in Chip Att register */
1746                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
1747                 writel(status, phba->CAregaddr);
1748                 readl(phba->CAregaddr); /* flush */
1749         }
1750         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1751                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1752                 pring->stats.iocb_cmd_empty++;
1753
1754                 /* Force update of the local copy of cmdGetInx */
1755                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1756                 lpfc_sli_resume_iocb(phba, pring);
1757
1758                 if ((pring->lpfc_sli_cmd_available))
1759                         (pring->lpfc_sli_cmd_available) (phba, pring);
1760
1761         }
1762
1763         spin_unlock_irqrestore(&phba->hbalock, iflag);
1764         return rc;
1765 }
1766
1767 void
1768 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1769 {
1770         LIST_HEAD(completions);
1771         struct lpfc_iocbq *iocb, *next_iocb;
1772         IOCB_t *cmd = NULL;
1773
1774         if (pring->ringno == LPFC_ELS_RING) {
1775                 lpfc_fabric_abort_hba(phba);
1776         }
1777
1778         /* Error everything on txq and txcmplq
1779          * First do the txq.
1780          */
1781         spin_lock_irq(&phba->hbalock);
1782         list_splice_init(&pring->txq, &completions);
1783         pring->txq_cnt = 0;
1784
1785         /* Next issue ABTS for everything on the txcmplq */
1786         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
1787                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
1788
1789         spin_unlock_irq(&phba->hbalock);
1790
1791         while (!list_empty(&completions)) {
1792                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
1793                 cmd = &iocb->iocb;
1794                 list_del_init(&iocb->list);
1795
1796                 if (!iocb->iocb_cmpl)
1797                         lpfc_sli_release_iocbq(phba, iocb);
1798                 else {
1799                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1800                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1801                         (iocb->iocb_cmpl) (phba, iocb, iocb);
1802                 }
1803         }
1804 }
1805
1806 int
1807 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
1808 {
1809         uint32_t status;
1810         int i = 0;
1811         int retval = 0;
1812
1813         /* Read the HBA Host Status Register */
1814         status = readl(phba->HSregaddr);
1815
1816         /*
1817          * Check status register every 100ms for 5 retries, then every
1818          * 500ms for 5, then every 2.5 sec for 5, then reset board and
1819          * every 2.5 sec for 4.
1820          * Break our of the loop if errors occurred during init.
1821          */
1822         while (((status & mask) != mask) &&
1823                !(status & HS_FFERM) &&
1824                i++ < 20) {
1825
1826                 if (i <= 5)
1827                         msleep(10);
1828                 else if (i <= 10)
1829                         msleep(500);
1830                 else
1831                         msleep(2500);
1832
1833                 if (i == 15) {
1834                                 /* Do post */
1835                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
1836                         lpfc_sli_brdrestart(phba);
1837                 }
1838                 /* Read the HBA Host Status Register */
1839                 status = readl(phba->HSregaddr);
1840         }
1841
1842         /* Check to see if any errors occurred during init */
1843         if ((status & HS_FFERM) || (i >= 20)) {
1844                 phba->link_state = LPFC_HBA_ERROR;
1845                 retval = 1;
1846         }
1847
1848         return retval;
1849 }
1850
1851 #define BARRIER_TEST_PATTERN (0xdeadbeef)
1852
1853 void lpfc_reset_barrier(struct lpfc_hba *phba)
1854 {
1855         uint32_t __iomem *resp_buf;
1856         uint32_t __iomem *mbox_buf;
1857         volatile uint32_t mbox;
1858         uint32_t hc_copy;
1859         int  i;
1860         uint8_t hdrtype;
1861
1862         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
1863         if (hdrtype != 0x80 ||
1864             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
1865              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
1866                 return;
1867
1868         /*
1869          * Tell the other part of the chip to suspend temporarily all
1870          * its DMA activity.
1871          */
1872         resp_buf = phba->MBslimaddr;
1873
1874         /* Disable the error attention */
1875         hc_copy = readl(phba->HCregaddr);
1876         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
1877         readl(phba->HCregaddr); /* flush */
1878         phba->link_flag |= LS_IGNORE_ERATT;
1879
1880         if (readl(phba->HAregaddr) & HA_ERATT) {
1881                 /* Clear Chip error bit */
1882                 writel(HA_ERATT, phba->HAregaddr);
1883                 phba->pport->stopped = 1;
1884         }
1885
1886         mbox = 0;
1887         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
1888         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
1889
1890         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
1891         mbox_buf = phba->MBslimaddr;
1892         writel(mbox, mbox_buf);
1893
1894         for (i = 0;
1895              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
1896                 mdelay(1);
1897
1898         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
1899                 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
1900                     phba->pport->stopped)
1901                         goto restore_hc;
1902                 else
1903                         goto clear_errat;
1904         }
1905
1906         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
1907         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
1908                 mdelay(1);
1909
1910 clear_errat:
1911
1912         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
1913                 mdelay(1);
1914
1915         if (readl(phba->HAregaddr) & HA_ERATT) {
1916                 writel(HA_ERATT, phba->HAregaddr);
1917                 phba->pport->stopped = 1;
1918         }
1919
1920 restore_hc:
1921         phba->link_flag &= ~LS_IGNORE_ERATT;
1922         writel(hc_copy, phba->HCregaddr);
1923         readl(phba->HCregaddr); /* flush */
1924 }
1925
1926 int
1927 lpfc_sli_brdkill(struct lpfc_hba *phba)
1928 {
1929         struct lpfc_sli *psli;
1930         LPFC_MBOXQ_t *pmb;
1931         uint32_t status;
1932         uint32_t ha_copy;
1933         int retval;
1934         int i = 0;
1935
1936         psli = &phba->sli;
1937
1938         /* Kill HBA */
1939         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1940                         "0329 Kill HBA Data: x%x x%x\n",
1941                         phba->pport->port_state, psli->sli_flag);
1942
1943         if ((pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
1944                                                   GFP_KERNEL)) == 0)
1945                 return 1;
1946
1947         /* Disable the error attention */
1948         spin_lock_irq(&phba->hbalock);
1949         status = readl(phba->HCregaddr);
1950         status &= ~HC_ERINT_ENA;
1951         writel(status, phba->HCregaddr);
1952         readl(phba->HCregaddr); /* flush */
1953         phba->link_flag |= LS_IGNORE_ERATT;
1954         spin_unlock_irq(&phba->hbalock);
1955
1956         lpfc_kill_board(phba, pmb);
1957         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1958         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1959
1960         if (retval != MBX_SUCCESS) {
1961                 if (retval != MBX_BUSY)
1962                         mempool_free(pmb, phba->mbox_mem_pool);
1963                 spin_lock_irq(&phba->hbalock);
1964                 phba->link_flag &= ~LS_IGNORE_ERATT;
1965                 spin_unlock_irq(&phba->hbalock);
1966                 return 1;
1967         }
1968
1969         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
1970
1971         mempool_free(pmb, phba->mbox_mem_pool);
1972
1973         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
1974          * attention every 100ms for 3 seconds. If we don't get ERATT after
1975          * 3 seconds we still set HBA_ERROR state because the status of the
1976          * board is now undefined.
1977          */
1978         ha_copy = readl(phba->HAregaddr);
1979
1980         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
1981                 mdelay(100);
1982                 ha_copy = readl(phba->HAregaddr);
1983         }
1984
1985         del_timer_sync(&psli->mbox_tmo);
1986         if (ha_copy & HA_ERATT) {
1987                 writel(HA_ERATT, phba->HAregaddr);
1988                 phba->pport->stopped = 1;
1989         }
1990         spin_lock_irq(&phba->hbalock);
1991         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1992         phba->link_flag &= ~LS_IGNORE_ERATT;
1993         spin_unlock_irq(&phba->hbalock);
1994
1995         psli->mbox_active = NULL;
1996         lpfc_hba_down_post(phba);
1997         phba->link_state = LPFC_HBA_ERROR;
1998
1999         return ha_copy & HA_ERATT ? 0 : 1;
2000 }
2001
2002 int
2003 lpfc_sli_brdreset(struct lpfc_hba *phba)
2004 {
2005         struct lpfc_sli *psli;
2006         struct lpfc_sli_ring *pring;
2007         uint16_t cfg_value;
2008         int i;
2009
2010         psli = &phba->sli;
2011
2012         /* Reset HBA */
2013         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2014                         "0325 Reset HBA Data: x%x x%x\n",
2015                         phba->pport->port_state, psli->sli_flag);
2016
2017         /* perform board reset */
2018         phba->fc_eventTag = 0;
2019         phba->pport->fc_myDID = 0;
2020         phba->pport->fc_prevDID = 0;
2021
2022         /* Turn off parity checking and serr during the physical reset */
2023         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2024         pci_write_config_word(phba->pcidev, PCI_COMMAND,
2025                               (cfg_value &
2026                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2027
2028         psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
2029         /* Now toggle INITFF bit in the Host Control Register */
2030         writel(HC_INITFF, phba->HCregaddr);
2031         mdelay(1);
2032         readl(phba->HCregaddr); /* flush */
2033         writel(0, phba->HCregaddr);
2034         readl(phba->HCregaddr); /* flush */
2035
2036         /* Restore PCI cmd register */
2037         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
2038
2039         /* Initialize relevant SLI info */
2040         for (i = 0; i < psli->num_rings; i++) {
2041                 pring = &psli->ring[i];
2042                 pring->flag = 0;
2043                 pring->rspidx = 0;
2044                 pring->next_cmdidx  = 0;
2045                 pring->local_getidx = 0;
2046                 pring->cmdidx = 0;
2047                 pring->missbufcnt = 0;
2048         }
2049
2050         phba->link_state = LPFC_WARM_START;
2051         return 0;
2052 }
2053
2054 int
2055 lpfc_sli_brdrestart(struct lpfc_hba *phba)
2056 {
2057         MAILBOX_t *mb;
2058         struct lpfc_sli *psli;
2059         uint16_t skip_post;
2060         volatile uint32_t word0;
2061         void __iomem *to_slim;
2062
2063         spin_lock_irq(&phba->hbalock);
2064
2065         psli = &phba->sli;
2066
2067         /* Restart HBA */
2068         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2069                         "0337 Restart HBA Data: x%x x%x\n",
2070                         phba->pport->port_state, psli->sli_flag);
2071
2072         word0 = 0;
2073         mb = (MAILBOX_t *) &word0;
2074         mb->mbxCommand = MBX_RESTART;
2075         mb->mbxHc = 1;
2076
2077         lpfc_reset_barrier(phba);
2078
2079         to_slim = phba->MBslimaddr;
2080         writel(*(uint32_t *) mb, to_slim);
2081         readl(to_slim); /* flush */
2082
2083         /* Only skip post after fc_ffinit is completed */
2084         if (phba->pport->port_state) {
2085                 skip_post = 1;
2086                 word0 = 1;      /* This is really setting up word1 */
2087         } else {
2088                 skip_post = 0;
2089                 word0 = 0;      /* This is really setting up word1 */
2090         }
2091         to_slim = phba->MBslimaddr + sizeof (uint32_t);
2092         writel(*(uint32_t *) mb, to_slim);
2093         readl(to_slim); /* flush */
2094
2095         lpfc_sli_brdreset(phba);
2096         phba->pport->stopped = 0;
2097         phba->link_state = LPFC_INIT_START;
2098
2099         spin_unlock_irq(&phba->hbalock);
2100
2101         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2102         psli->stats_start = get_seconds();
2103
2104         if (skip_post)
2105                 mdelay(100);
2106         else
2107                 mdelay(2000);
2108
2109         lpfc_hba_down_post(phba);
2110
2111         return 0;
2112 }
2113
2114 static int
2115 lpfc_sli_chipset_init(struct lpfc_hba *phba)
2116 {
2117         uint32_t status, i = 0;
2118
2119         /* Read the HBA Host Status Register */
2120         status = readl(phba->HSregaddr);
2121
2122         /* Check status register to see what current state is */
2123         i = 0;
2124         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2125
2126                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2127                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
2128                  * 4.
2129                  */
2130                 if (i++ >= 20) {
2131                         /* Adapter failed to init, timeout, status reg
2132                            <status> */
2133                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2134                                         "0436 Adapter failed to init, "
2135                                         "timeout, status reg x%x\n", status);
2136                         phba->link_state = LPFC_HBA_ERROR;
2137                         return -ETIMEDOUT;
2138                 }
2139
2140                 /* Check to see if any errors occurred during init */
2141                 if (status & HS_FFERM) {
2142                         /* ERROR: During chipset initialization */
2143                         /* Adapter failed to init, chipset, status reg
2144                            <status> */
2145                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2146                                         "0437 Adapter failed to init, "
2147                                         "chipset, status reg x%x\n", status);
2148                         phba->link_state = LPFC_HBA_ERROR;
2149                         return -EIO;
2150                 }
2151
2152                 if (i <= 5) {
2153                         msleep(10);
2154                 } else if (i <= 10) {
2155                         msleep(500);
2156                 } else {
2157                         msleep(2500);
2158                 }
2159
2160                 if (i == 15) {
2161                                 /* Do post */
2162                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2163                         lpfc_sli_brdrestart(phba);
2164                 }
2165                 /* Read the HBA Host Status Register */
2166                 status = readl(phba->HSregaddr);
2167         }
2168
2169         /* Check to see if any errors occurred during init */
2170         if (status & HS_FFERM) {
2171                 /* ERROR: During chipset initialization */
2172                 /* Adapter failed to init, chipset, status reg <status> */
2173                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2174                                 "0438 Adapter failed to init, chipset, "
2175                                 "status reg x%x\n", status);
2176                 phba->link_state = LPFC_HBA_ERROR;
2177                 return -EIO;
2178         }
2179
2180         /* Clear all interrupt enable conditions */
2181         writel(0, phba->HCregaddr);
2182         readl(phba->HCregaddr); /* flush */
2183
2184         /* setup host attn register */
2185         writel(0xffffffff, phba->HAregaddr);
2186         readl(phba->HAregaddr); /* flush */
2187         return 0;
2188 }
2189
2190 int
2191 lpfc_sli_hbq_count(void)
2192 {
2193         return ARRAY_SIZE(lpfc_hbq_defs);
2194 }
2195
2196 static int
2197 lpfc_sli_hbq_entry_count(void)
2198 {
2199         int  hbq_count = lpfc_sli_hbq_count();
2200         int  count = 0;
2201         int  i;
2202
2203         for (i = 0; i < hbq_count; ++i)
2204                 count += lpfc_hbq_defs[i]->entry_count;
2205         return count;
2206 }
2207
2208 int
2209 lpfc_sli_hbq_size(void)
2210 {
2211         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2212 }
2213
2214 static int
2215 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2216 {
2217         int  hbq_count = lpfc_sli_hbq_count();
2218         LPFC_MBOXQ_t *pmb;
2219         MAILBOX_t *pmbox;
2220         uint32_t hbqno;
2221         uint32_t hbq_entry_index;
2222
2223                                 /* Get a Mailbox buffer to setup mailbox
2224                                  * commands for HBA initialization
2225                                  */
2226         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2227
2228         if (!pmb)
2229                 return -ENOMEM;
2230
2231         pmbox = &pmb->mb;
2232
2233         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2234         phba->link_state = LPFC_INIT_MBX_CMDS;
2235
2236         hbq_entry_index = 0;
2237         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2238                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2239                 phba->hbqs[hbqno].hbqPutIdx      = 0;
2240                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
2241                 phba->hbqs[hbqno].entry_count =
2242                         lpfc_hbq_defs[hbqno]->entry_count;
2243                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2244                         hbq_entry_index, pmb);
2245                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2246
2247                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2248                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2249                            mbxStatus <status>, ring <num> */
2250
2251                         lpfc_printf_log(phba, KERN_ERR,
2252                                         LOG_SLI | LOG_VPORT,
2253                                         "1805 Adapter failed to init. "
2254                                         "Data: x%x x%x x%x\n",
2255                                         pmbox->mbxCommand,
2256                                         pmbox->mbxStatus, hbqno);
2257
2258                         phba->link_state = LPFC_HBA_ERROR;
2259                         mempool_free(pmb, phba->mbox_mem_pool);
2260                         return ENXIO;
2261                 }
2262         }
2263         phba->hbq_count = hbq_count;
2264
2265         mempool_free(pmb, phba->mbox_mem_pool);
2266
2267         /* Initially populate or replenish the HBQs */
2268         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2269                 if (lpfc_sli_hbqbuf_init_hbqs(phba, hbqno))
2270                         return -ENOMEM;
2271         }
2272         return 0;
2273 }
2274
2275 static int
2276 lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
2277 {
2278         LPFC_MBOXQ_t *pmb;
2279         uint32_t resetcount = 0, rc = 0, done = 0;
2280
2281         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2282         if (!pmb) {
2283                 phba->link_state = LPFC_HBA_ERROR;
2284                 return -ENOMEM;
2285         }
2286
2287         phba->sli_rev = sli_mode;
2288         while (resetcount < 2 && !done) {
2289                 spin_lock_irq(&phba->hbalock);
2290                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2291                 spin_unlock_irq(&phba->hbalock);
2292                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2293                 lpfc_sli_brdrestart(phba);
2294                 msleep(2500);
2295                 rc = lpfc_sli_chipset_init(phba);
2296                 if (rc)
2297                         break;
2298
2299                 spin_lock_irq(&phba->hbalock);
2300                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2301                 spin_unlock_irq(&phba->hbalock);
2302                 resetcount++;
2303
2304                 /* Call pre CONFIG_PORT mailbox command initialization.  A
2305                  * value of 0 means the call was successful.  Any other
2306                  * nonzero value is a failure, but if ERESTART is returned,
2307                  * the driver may reset the HBA and try again.
2308                  */
2309                 rc = lpfc_config_port_prep(phba);
2310                 if (rc == -ERESTART) {
2311                         phba->link_state = LPFC_LINK_UNKNOWN;
2312                         continue;
2313                 } else if (rc) {
2314                         break;
2315                 }
2316
2317                 phba->link_state = LPFC_INIT_MBX_CMDS;
2318                 lpfc_config_port(phba, pmb);
2319                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
2320                 if (rc != MBX_SUCCESS) {
2321                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2322                                 "0442 Adapter failed to init, mbxCmd x%x "
2323                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
2324                                 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
2325                         spin_lock_irq(&phba->hbalock);
2326                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
2327                         spin_unlock_irq(&phba->hbalock);
2328                         rc = -ENXIO;
2329                 } else {
2330                         done = 1;
2331                         phba->max_vpi = (phba->max_vpi &&
2332                                          pmb->mb.un.varCfgPort.gmv) != 0
2333                                 ? pmb->mb.un.varCfgPort.max_vpi
2334                                 : 0;
2335                 }
2336         }
2337
2338         if (!done) {
2339                 rc = -EINVAL;
2340                 goto do_prep_failed;
2341         }
2342
2343         if ((pmb->mb.un.varCfgPort.sli_mode == 3) &&
2344                 (!pmb->mb.un.varCfgPort.cMA)) {
2345                 rc = -ENXIO;
2346                 goto do_prep_failed;
2347         }
2348         return rc;
2349
2350 do_prep_failed:
2351         mempool_free(pmb, phba->mbox_mem_pool);
2352         return rc;
2353 }
2354
2355 int
2356 lpfc_sli_hba_setup(struct lpfc_hba *phba)
2357 {
2358         uint32_t rc;
2359         int  mode = 3;
2360
2361         switch (lpfc_sli_mode) {
2362         case 2:
2363                 if (phba->cfg_enable_npiv) {
2364                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2365                                 "1824 NPIV enabled: Override lpfc_sli_mode "
2366                                 "parameter (%d) to auto (0).\n",
2367                                 lpfc_sli_mode);
2368                         break;
2369                 }
2370                 mode = 2;
2371                 break;
2372         case 0:
2373         case 3:
2374                 break;
2375         default:
2376                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2377                                 "1819 Unrecognized lpfc_sli_mode "
2378                                 "parameter: %d.\n", lpfc_sli_mode);
2379
2380                 break;
2381         }
2382
2383         rc = lpfc_do_config_port(phba, mode);
2384         if (rc && lpfc_sli_mode == 3)
2385                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
2386                                 "1820 Unable to select SLI-3.  "
2387                                 "Not supported by adapter.\n");
2388         if (rc && mode != 2)
2389                 rc = lpfc_do_config_port(phba, 2);
2390         if (rc)
2391                 goto lpfc_sli_hba_setup_error;
2392
2393         if (phba->sli_rev == 3) {
2394                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
2395                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
2396                 phba->sli3_options |= LPFC_SLI3_ENABLED;
2397                 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
2398
2399         } else {
2400                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
2401                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
2402                 phba->sli3_options = 0;
2403         }
2404
2405         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2406                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
2407                         phba->sli_rev, phba->max_vpi);
2408         rc = lpfc_sli_ring_map(phba);
2409
2410         if (rc)
2411                 goto lpfc_sli_hba_setup_error;
2412
2413                                 /* Init HBQs */
2414
2415         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2416                 rc = lpfc_sli_hbq_setup(phba);
2417                 if (rc)
2418                         goto lpfc_sli_hba_setup_error;
2419         }
2420
2421         phba->sli.sli_flag |= LPFC_PROCESS_LA;
2422
2423         rc = lpfc_config_port_post(phba);
2424         if (rc)
2425                 goto lpfc_sli_hba_setup_error;
2426
2427         return rc;
2428
2429 lpfc_sli_hba_setup_error:
2430         phba->link_state = LPFC_HBA_ERROR;
2431         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2432                         "0445 Firmware initialization failed\n");
2433         return rc;
2434 }
2435
2436 /*! lpfc_mbox_timeout
2437  *
2438  * \pre
2439  * \post
2440  * \param hba Pointer to per struct lpfc_hba structure
2441  * \param l1  Pointer to the driver's mailbox queue.
2442  * \return
2443  *   void
2444  *
2445  * \b Description:
2446  *
2447  * This routine handles mailbox timeout events at timer interrupt context.
2448  */
2449 void
2450 lpfc_mbox_timeout(unsigned long ptr)
2451 {
2452         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
2453         unsigned long iflag;
2454         uint32_t tmo_posted;
2455
2456         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
2457         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
2458         if (!tmo_posted)
2459                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
2460         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
2461
2462         if (!tmo_posted) {
2463                 spin_lock_irqsave(&phba->hbalock, iflag);
2464                 if (phba->work_wait)
2465                         lpfc_worker_wake_up(phba);
2466                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2467         }
2468 }
2469
2470 void
2471 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
2472 {
2473         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
2474         MAILBOX_t *mb = &pmbox->mb;
2475         struct lpfc_sli *psli = &phba->sli;
2476         struct lpfc_sli_ring *pring;
2477
2478         if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
2479                 return;
2480         }
2481
2482         /* Mbox cmd <mbxCommand> timeout */
2483         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2484                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
2485                         mb->mbxCommand,
2486                         phba->pport->port_state,
2487                         phba->sli.sli_flag,
2488                         phba->sli.mbox_active);
2489
2490         /* Setting state unknown so lpfc_sli_abort_iocb_ring
2491          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
2492          * it to fail all oustanding SCSI IO.
2493          */
2494         spin_lock_irq(&phba->pport->work_port_lock);
2495         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
2496         spin_unlock_irq(&phba->pport->work_port_lock);
2497         spin_lock_irq(&phba->hbalock);
2498         phba->link_state = LPFC_LINK_UNKNOWN;
2499         phba->pport->fc_flag |= FC_ESTABLISH_LINK;
2500         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2501         spin_unlock_irq(&phba->hbalock);
2502
2503         pring = &psli->ring[psli->fcp_ring];
2504         lpfc_sli_abort_iocb_ring(phba, pring);
2505
2506         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2507                         "0316 Resetting board due to mailbox timeout\n");
2508         /*
2509          * lpfc_offline calls lpfc_sli_hba_down which will clean up
2510          * on oustanding mailbox commands.
2511          */
2512         lpfc_offline_prep(phba);
2513         lpfc_offline(phba);
2514         lpfc_sli_brdrestart(phba);
2515         if (lpfc_online(phba) == 0)             /* Initialize the HBA */
2516                 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
2517         lpfc_unblock_mgmt_io(phba);
2518         return;
2519 }
2520
2521 int
2522 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
2523 {
2524         MAILBOX_t *mb;
2525         struct lpfc_sli *psli = &phba->sli;
2526         uint32_t status, evtctr;
2527         uint32_t ha_copy;
2528         int i;
2529         unsigned long drvr_flag = 0;
2530         volatile uint32_t word0, ldata;
2531         void __iomem *to_slim;
2532
2533         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
2534                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
2535                 if(!pmbox->vport) {
2536                         lpfc_printf_log(phba, KERN_ERR,
2537                                         LOG_MBOX | LOG_VPORT,
2538                                         "1806 Mbox x%x failed. No vport\n",
2539                                         pmbox->mb.mbxCommand);
2540                         dump_stack();
2541                         return MBXERR_ERROR;
2542                 }
2543         }
2544
2545
2546         /* If the PCI channel is in offline state, do not post mbox. */
2547         if (unlikely(pci_channel_offline(phba->pcidev)))
2548                 return MBX_NOT_FINISHED;
2549
2550         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2551         psli = &phba->sli;
2552
2553
2554         mb = &pmbox->mb;
2555         status = MBX_SUCCESS;
2556
2557         if (phba->link_state == LPFC_HBA_ERROR) {
2558                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2559
2560                 /* Mbox command <mbxCommand> cannot issue */
2561                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
2562                 return MBX_NOT_FINISHED;
2563         }
2564
2565         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
2566             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2567                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2568                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag)
2569                 return MBX_NOT_FINISHED;
2570         }
2571
2572         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
2573                 /* Polling for a mbox command when another one is already active
2574                  * is not allowed in SLI. Also, the driver must have established
2575                  * SLI2 mode to queue and process multiple mbox commands.
2576                  */
2577
2578                 if (flag & MBX_POLL) {
2579                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2580
2581                         /* Mbox command <mbxCommand> cannot issue */
2582                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
2583                         return MBX_NOT_FINISHED;
2584                 }
2585
2586                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
2587                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2588                         /* Mbox command <mbxCommand> cannot issue */
2589                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
2590                         return MBX_NOT_FINISHED;
2591                 }
2592
2593                 /* Handle STOP IOCB processing flag. This is only meaningful
2594                  * if we are not polling for mbox completion.
2595                  */
2596                 if (flag & MBX_STOP_IOCB) {
2597                         flag &= ~MBX_STOP_IOCB;
2598                         /* Now flag each ring */
2599                         for (i = 0; i < psli->num_rings; i++) {
2600                                 /* If the ring is active, flag it */
2601                                 if (psli->ring[i].cmdringaddr) {
2602                                         psli->ring[i].flag |=
2603                                             LPFC_STOP_IOCB_MBX;
2604                                 }
2605                         }
2606                 }
2607
2608                 /* Another mailbox command is still being processed, queue this
2609                  * command to be processed later.
2610                  */
2611                 lpfc_mbox_put(phba, pmbox);
2612
2613                 /* Mbox cmd issue - BUSY */
2614                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2615                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
2616                                 "x%x x%x x%x x%x\n",
2617                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
2618                                 mb->mbxCommand, phba->pport->port_state,
2619                                 psli->sli_flag, flag);
2620
2621                 psli->slistat.mbox_busy++;
2622                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2623
2624                 if (pmbox->vport) {
2625                         lpfc_debugfs_disc_trc(pmbox->vport,
2626                                 LPFC_DISC_TRC_MBOX_VPORT,
2627                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
2628                                 (uint32_t)mb->mbxCommand,
2629                                 mb->un.varWords[0], mb->un.varWords[1]);
2630                 }
2631                 else {
2632                         lpfc_debugfs_disc_trc(phba->pport,
2633                                 LPFC_DISC_TRC_MBOX,
2634                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
2635                                 (uint32_t)mb->mbxCommand,
2636                                 mb->un.varWords[0], mb->un.varWords[1]);
2637                 }
2638
2639                 return MBX_BUSY;
2640         }
2641
2642         /* Handle STOP IOCB processing flag. This is only meaningful
2643          * if we are not polling for mbox completion.
2644          */
2645         if (flag & MBX_STOP_IOCB) {
2646                 flag &= ~MBX_STOP_IOCB;
2647                 if (flag == MBX_NOWAIT) {
2648                         /* Now flag each ring */
2649                         for (i = 0; i < psli->num_rings; i++) {
2650                                 /* If the ring is active, flag it */
2651                                 if (psli->ring[i].cmdringaddr) {
2652                                         psli->ring[i].flag |=
2653                                             LPFC_STOP_IOCB_MBX;
2654                                 }
2655                         }
2656                 }
2657         }
2658
2659         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2660
2661         /* If we are not polling, we MUST be in SLI2 mode */
2662         if (flag != MBX_POLL) {
2663                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
2664                     (mb->mbxCommand != MBX_KILL_BOARD)) {
2665                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2666                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2667                         /* Mbox command <mbxCommand> cannot issue */
2668                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
2669                         return MBX_NOT_FINISHED;
2670                 }
2671                 /* timeout active mbox command */
2672                 mod_timer(&psli->mbox_tmo, (jiffies +
2673                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
2674         }
2675
2676         /* Mailbox cmd <cmd> issue */
2677         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2678                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
2679                         "x%x\n",
2680                         pmbox->vport ? pmbox->vport->vpi : 0,
2681                         mb->mbxCommand, phba->pport->port_state,
2682                         psli->sli_flag, flag);
2683
2684         if (mb->mbxCommand != MBX_HEARTBEAT) {
2685                 if (pmbox->vport) {
2686                         lpfc_debugfs_disc_trc(pmbox->vport,
2687                                 LPFC_DISC_TRC_MBOX_VPORT,
2688                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
2689                                 (uint32_t)mb->mbxCommand,
2690                                 mb->un.varWords[0], mb->un.varWords[1]);
2691                 }
2692                 else {
2693                         lpfc_debugfs_disc_trc(phba->pport,
2694                                 LPFC_DISC_TRC_MBOX,
2695                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
2696                                 (uint32_t)mb->mbxCommand,
2697                                 mb->un.varWords[0], mb->un.varWords[1]);
2698                 }
2699         }
2700
2701         psli->slistat.mbox_cmd++;
2702         evtctr = psli->slistat.mbox_event;
2703
2704         /* next set own bit for the adapter and copy over command word */
2705         mb->mbxOwner = OWN_CHIP;
2706
2707         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2708                 /* First copy command data to host SLIM area */
2709                 lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx, MAILBOX_CMD_SIZE);
2710         } else {
2711                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2712                         /* copy command data into host mbox for cmpl */
2713                         lpfc_sli_pcimem_bcopy(mb, &phba->slim2p->mbx,
2714                                               MAILBOX_CMD_SIZE);
2715                 }
2716
2717                 /* First copy mbox command data to HBA SLIM, skip past first
2718                    word */
2719                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
2720                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
2721                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
2722
2723                 /* Next copy over first word, with mbxOwner set */
2724                 ldata = *((volatile uint32_t *)mb);
2725                 to_slim = phba->MBslimaddr;
2726                 writel(ldata, to_slim);
2727                 readl(to_slim); /* flush */
2728
2729                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2730                         /* switch over to host mailbox */
2731                         psli->sli_flag |= LPFC_SLI2_ACTIVE;
2732                 }
2733         }
2734
2735         wmb();
2736         /* interrupt board to doit right away */
2737         writel(CA_MBATT, phba->CAregaddr);
2738         readl(phba->CAregaddr); /* flush */
2739
2740         switch (flag) {
2741         case MBX_NOWAIT:
2742                 /* Don't wait for it to finish, just return */
2743                 psli->mbox_active = pmbox;
2744                 break;
2745
2746         case MBX_POLL:
2747                 psli->mbox_active = NULL;
2748                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2749                         /* First read mbox status word */
2750                         word0 = *((volatile uint32_t *)&phba->slim2p->mbx);
2751                         word0 = le32_to_cpu(word0);
2752                 } else {
2753                         /* First read mbox status word */
2754                         word0 = readl(phba->MBslimaddr);
2755                 }
2756
2757                 /* Read the HBA Host Attention Register */
2758                 ha_copy = readl(phba->HAregaddr);
2759
2760                 i = lpfc_mbox_tmo_val(phba, mb->mbxCommand);
2761                 i *= 1000; /* Convert to ms */
2762
2763                 /* Wait for command to complete */
2764                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
2765                        (!(ha_copy & HA_MBATT) &&
2766                         (phba->link_state > LPFC_WARM_START))) {
2767                         if (i-- <= 0) {
2768                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2769                                 spin_unlock_irqrestore(&phba->hbalock,
2770                                                        drvr_flag);
2771                                 return MBX_NOT_FINISHED;
2772                         }
2773
2774                         /* Check if we took a mbox interrupt while we were
2775                            polling */
2776                         if (((word0 & OWN_CHIP) != OWN_CHIP)
2777                             && (evtctr != psli->slistat.mbox_event))
2778                                 break;
2779
2780                         spin_unlock_irqrestore(&phba->hbalock,
2781                                                drvr_flag);
2782
2783                         msleep(1);
2784
2785                         spin_lock_irqsave(&phba->hbalock, drvr_flag);
2786
2787                         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2788                                 /* First copy command data */
2789                                 word0 = *((volatile uint32_t *)
2790                                                 &phba->slim2p->mbx);
2791                                 word0 = le32_to_cpu(word0);
2792                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
2793                                         MAILBOX_t *slimmb;
2794                                         volatile uint32_t slimword0;
2795                                         /* Check real SLIM for any errors */
2796                                         slimword0 = readl(phba->MBslimaddr);
2797                                         slimmb = (MAILBOX_t *) & slimword0;
2798                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
2799                                             && slimmb->mbxStatus) {
2800                                                 psli->sli_flag &=
2801                                                     ~LPFC_SLI2_ACTIVE;
2802                                                 word0 = slimword0;
2803                                         }
2804                                 }
2805                         } else {
2806                                 /* First copy command data */
2807                                 word0 = readl(phba->MBslimaddr);
2808                         }
2809                         /* Read the HBA Host Attention Register */
2810                         ha_copy = readl(phba->HAregaddr);
2811                 }
2812
2813                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
2814                         /* copy results back to user */
2815                         lpfc_sli_pcimem_bcopy(&phba->slim2p->mbx, mb,
2816                                               MAILBOX_CMD_SIZE);
2817                 } else {
2818                         /* First copy command data */
2819                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
2820                                                         MAILBOX_CMD_SIZE);
2821                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
2822                                 pmbox->context2) {
2823                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
2824                                       phba->MBslimaddr + DMP_RSP_OFFSET,
2825                                                       mb->un.varDmp.word_cnt);
2826                         }
2827                 }
2828
2829                 writel(HA_MBATT, phba->HAregaddr);
2830                 readl(phba->HAregaddr); /* flush */
2831
2832                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2833                 status = mb->mbxStatus;
2834         }
2835
2836         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2837         return status;
2838 }
2839
2840 /*
2841  * Caller needs to hold lock.
2842  */
2843 static void
2844 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2845                     struct lpfc_iocbq *piocb)
2846 {
2847         /* Insert the caller's iocb in the txq tail for later processing. */
2848         list_add_tail(&piocb->list, &pring->txq);
2849         pring->txq_cnt++;
2850 }
2851
2852 static struct lpfc_iocbq *
2853 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2854                    struct lpfc_iocbq **piocb)
2855 {
2856         struct lpfc_iocbq * nextiocb;
2857
2858         nextiocb = lpfc_sli_ringtx_get(phba, pring);
2859         if (!nextiocb) {
2860                 nextiocb = *piocb;
2861                 *piocb = NULL;
2862         }
2863
2864         return nextiocb;
2865 }
2866
2867 /*
2868  * Lockless version of lpfc_sli_issue_iocb.
2869  */
2870 int
2871 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2872                     struct lpfc_iocbq *piocb, uint32_t flag)
2873 {
2874         struct lpfc_iocbq *nextiocb;
2875         IOCB_t *iocb;
2876
2877         if (piocb->iocb_cmpl && (!piocb->vport) &&
2878            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
2879            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
2880                 lpfc_printf_log(phba, KERN_ERR,
2881                                 LOG_SLI | LOG_VPORT,
2882                                 "1807 IOCB x%x failed. No vport\n",
2883                                 piocb->iocb.ulpCommand);
2884                 dump_stack();
2885                 return IOCB_ERROR;
2886         }
2887
2888
2889         /* If the PCI channel is in offline state, do not post iocbs. */
2890         if (unlikely(pci_channel_offline(phba->pcidev)))
2891                 return IOCB_ERROR;
2892
2893         /*
2894          * We should never get an IOCB if we are in a < LINK_DOWN state
2895          */
2896         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
2897                 return IOCB_ERROR;
2898
2899         /*
2900          * Check to see if we are blocking IOCB processing because of a
2901          * outstanding mbox command.
2902          */
2903         if (unlikely(pring->flag & LPFC_STOP_IOCB_MBX))
2904                 goto iocb_busy;
2905
2906         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
2907                 /*
2908                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
2909                  * can be issued if the link is not up.
2910                  */
2911                 switch (piocb->iocb.ulpCommand) {
2912                 case CMD_QUE_RING_BUF_CN:
2913                 case CMD_QUE_RING_BUF64_CN:
2914                         /*
2915                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
2916                          * completion, iocb_cmpl MUST be 0.
2917                          */
2918                         if (piocb->iocb_cmpl)
2919                                 piocb->iocb_cmpl = NULL;
2920                         /*FALLTHROUGH*/
2921                 case CMD_CREATE_XRI_CR:
2922                 case CMD_CLOSE_XRI_CN:
2923                 case CMD_CLOSE_XRI_CX:
2924                         break;
2925                 default:
2926                         goto iocb_busy;
2927                 }
2928
2929         /*
2930          * For FCP commands, we must be in a state where we can process link
2931          * attention events.
2932          */
2933         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
2934                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
2935                 goto iocb_busy;
2936         }
2937
2938         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
2939                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
2940                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
2941
2942         if (iocb)
2943                 lpfc_sli_update_ring(phba, pring);
2944         else
2945                 lpfc_sli_update_full_ring(phba, pring);
2946
2947         if (!piocb)
2948                 return IOCB_SUCCESS;
2949
2950         goto out_busy;
2951
2952  iocb_busy:
2953         pring->stats.iocb_cmd_delay++;
2954
2955  out_busy:
2956
2957         if (!(flag & SLI_IOCB_RET_IOCB)) {
2958                 __lpfc_sli_ringtx_put(phba, pring, piocb);
2959                 return IOCB_SUCCESS;
2960         }
2961
2962         return IOCB_BUSY;
2963 }
2964
2965
2966 int
2967 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2968                     struct lpfc_iocbq *piocb, uint32_t flag)
2969 {
2970         unsigned long iflags;
2971         int rc;
2972
2973         spin_lock_irqsave(&phba->hbalock, iflags);
2974         rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
2975         spin_unlock_irqrestore(&phba->hbalock, iflags);
2976
2977         return rc;
2978 }
2979
2980 static int
2981 lpfc_extra_ring_setup( struct lpfc_hba *phba)
2982 {
2983         struct lpfc_sli *psli;
2984         struct lpfc_sli_ring *pring;
2985
2986         psli = &phba->sli;
2987
2988         /* Adjust cmd/rsp ring iocb entries more evenly */
2989
2990         /* Take some away from the FCP ring */
2991         pring = &psli->ring[psli->fcp_ring];
2992         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
2993         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
2994         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
2995         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
2996
2997         /* and give them to the extra ring */
2998         pring = &psli->ring[psli->extra_ring];
2999
3000         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3001         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3002         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3003         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3004
3005         /* Setup default profile for this ring */
3006         pring->iotag_max = 4096;
3007         pring->num_mask = 1;
3008         pring->prt[0].profile = 0;      /* Mask 0 */
3009         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3010         pring->prt[0].type = phba->cfg_multi_ring_type;
3011         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3012         return 0;
3013 }
3014
3015 void
3016 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3017         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3018 {
3019         IOCB_t *icmd;
3020         uint16_t evt_code;
3021         uint16_t temp;
3022         struct temp_event temp_event_data;
3023         struct Scsi_Host *shost;
3024
3025         icmd = &iocbq->iocb;
3026         evt_code = icmd->un.asyncstat.evt_code;
3027         temp = icmd->ulpContext;
3028
3029         if ((evt_code != ASYNC_TEMP_WARN) &&
3030                 (evt_code != ASYNC_TEMP_SAFE)) {
3031                 lpfc_printf_log(phba,
3032                         KERN_ERR,
3033                         LOG_SLI,
3034                         "0327 Ring %d handler: unexpected ASYNC_STATUS"
3035                         " evt_code 0x%x\n",
3036                         pring->ringno,
3037                         icmd->un.asyncstat.evt_code);
3038                 return;
3039         }
3040         temp_event_data.data = (uint32_t)temp;
3041         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3042         if (evt_code == ASYNC_TEMP_WARN) {
3043                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3044                 lpfc_printf_log(phba,
3045                                 KERN_WARNING,
3046                                 LOG_TEMP,
3047                                 "0339 Adapter is very hot, please take "
3048                                 "corrective action. temperature : %d Celsius\n",
3049                                 temp);
3050         }
3051         if (evt_code == ASYNC_TEMP_SAFE) {
3052                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3053                 lpfc_printf_log(phba,
3054                                 KERN_INFO,
3055                                 LOG_TEMP,
3056                                 "0340 Adapter temperature is OK now. "
3057                                 "temperature : %d Celsius\n",
3058                                 temp);
3059         }
3060
3061         /* Send temperature change event to applications */
3062         shost = lpfc_shost_from_vport(phba->pport);
3063         fc_host_post_vendor_event(shost, fc_get_event_number(),
3064                 sizeof(temp_event_data), (char *) &temp_event_data,
3065                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3066
3067 }
3068
3069
3070 int
3071 lpfc_sli_setup(struct lpfc_hba *phba)
3072 {
3073         int i, totiocbsize = 0;
3074         struct lpfc_sli *psli = &phba->sli;
3075         struct lpfc_sli_ring *pring;
3076
3077         psli->num_rings = MAX_CONFIGURED_RINGS;
3078         psli->sli_flag = 0;
3079         psli->fcp_ring = LPFC_FCP_RING;
3080         psli->next_ring = LPFC_FCP_NEXT_RING;
3081         psli->extra_ring = LPFC_EXTRA_RING;
3082
3083         psli->iocbq_lookup = NULL;
3084         psli->iocbq_lookup_len = 0;
3085         psli->last_iotag = 0;
3086
3087         for (i = 0; i < psli->num_rings; i++) {
3088                 pring = &psli->ring[i];
3089                 switch (i) {
3090                 case LPFC_FCP_RING:     /* ring 0 - FCP */
3091                         /* numCiocb and numRiocb are used in config_port */
3092                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3093                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3094                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3095                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3096                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3097                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3098                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3099                                                         SLI3_IOCB_CMD_SIZE :
3100                                                         SLI2_IOCB_CMD_SIZE;
3101                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3102                                                         SLI3_IOCB_RSP_SIZE :
3103                                                         SLI2_IOCB_RSP_SIZE;
3104                         pring->iotag_ctr = 0;
3105                         pring->iotag_max =
3106                             (phba->cfg_hba_queue_depth * 2);
3107                         pring->fast_iotag = pring->iotag_max;
3108                         pring->num_mask = 0;
3109                         break;
3110                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
3111                         /* numCiocb and numRiocb are used in config_port */
3112                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3113                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
3114                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3115                                                         SLI3_IOCB_CMD_SIZE :
3116                                                         SLI2_IOCB_CMD_SIZE;
3117                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3118                                                         SLI3_IOCB_RSP_SIZE :
3119                                                         SLI2_IOCB_RSP_SIZE;
3120                         pring->iotag_max = phba->cfg_hba_queue_depth;
3121                         pring->num_mask = 0;
3122                         break;
3123                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
3124                         /* numCiocb and numRiocb are used in config_port */
3125                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3126                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
3127                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3128                                                         SLI3_IOCB_CMD_SIZE :
3129                                                         SLI2_IOCB_CMD_SIZE;
3130                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3131                                                         SLI3_IOCB_RSP_SIZE :
3132                                                         SLI2_IOCB_RSP_SIZE;
3133                         pring->fast_iotag = 0;
3134                         pring->iotag_ctr = 0;
3135                         pring->iotag_max = 4096;
3136                         pring->lpfc_sli_rcv_async_status =
3137                                 lpfc_sli_async_event_handler;
3138                         pring->num_mask = 4;
3139                         pring->prt[0].profile = 0;      /* Mask 0 */
3140                         pring->prt[0].rctl = FC_ELS_REQ;
3141                         pring->prt[0].type = FC_ELS_DATA;
3142                         pring->prt[0].lpfc_sli_rcv_unsol_event =
3143                             lpfc_els_unsol_event;
3144                         pring->prt[1].profile = 0;      /* Mask 1 */
3145                         pring->prt[1].rctl = FC_ELS_RSP;
3146                         pring->prt[1].type = FC_ELS_DATA;
3147                         pring->prt[1].lpfc_sli_rcv_unsol_event =
3148                             lpfc_els_unsol_event;
3149                         pring->prt[2].profile = 0;      /* Mask 2 */
3150                         /* NameServer Inquiry */
3151                         pring->prt[2].rctl = FC_UNSOL_CTL;
3152                         /* NameServer */
3153                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
3154                         pring->prt[2].lpfc_sli_rcv_unsol_event =
3155                             lpfc_ct_unsol_event;
3156                         pring->prt[3].profile = 0;      /* Mask 3 */
3157                         /* NameServer response */
3158                         pring->prt[3].rctl = FC_SOL_CTL;
3159                         /* NameServer */
3160                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
3161                         pring->prt[3].lpfc_sli_rcv_unsol_event =
3162                             lpfc_ct_unsol_event;
3163                         break;
3164                 }
3165                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
3166                                 (pring->numRiocb * pring->sizeRiocb);
3167         }
3168         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
3169                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
3170                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
3171                        "SLI2 SLIM Data: x%x x%lx\n",
3172                        phba->brd_no, totiocbsize,
3173                        (unsigned long) MAX_SLIM_IOCB_SIZE);
3174         }
3175         if (phba->cfg_multi_ring_support == 2)
3176                 lpfc_extra_ring_setup(phba);
3177
3178         return 0;
3179 }
3180
3181 int
3182 lpfc_sli_queue_setup(struct lpfc_hba *phba)
3183 {
3184         struct lpfc_sli *psli;
3185         struct lpfc_sli_ring *pring;
3186         int i;
3187
3188         psli = &phba->sli;
3189         spin_lock_irq(&phba->hbalock);
3190         INIT_LIST_HEAD(&psli->mboxq);
3191         INIT_LIST_HEAD(&psli->mboxq_cmpl);
3192         /* Initialize list headers for txq and txcmplq as double linked lists */
3193         for (i = 0; i < psli->num_rings; i++) {
3194                 pring = &psli->ring[i];
3195                 pring->ringno = i;
3196                 pring->next_cmdidx  = 0;
3197                 pring->local_getidx = 0;
3198                 pring->cmdidx = 0;
3199                 INIT_LIST_HEAD(&pring->txq);
3200                 INIT_LIST_HEAD(&pring->txcmplq);
3201                 INIT_LIST_HEAD(&pring->iocb_continueq);
3202                 INIT_LIST_HEAD(&pring->postbufq);
3203         }
3204         spin_unlock_irq(&phba->hbalock);
3205         return 1;
3206 }
3207
3208 int
3209 lpfc_sli_host_down(struct lpfc_vport *vport)
3210 {
3211         LIST_HEAD(completions);
3212         struct lpfc_hba *phba = vport->phba;
3213         struct lpfc_sli *psli = &phba->sli;
3214         struct lpfc_sli_ring *pring;
3215         struct lpfc_iocbq *iocb, *next_iocb;
3216         int i;
3217         unsigned long flags = 0;
3218         uint16_t prev_pring_flag;
3219
3220         lpfc_cleanup_discovery_resources(vport);
3221
3222         spin_lock_irqsave(&phba->hbalock, flags);
3223         for (i = 0; i < psli->num_rings; i++) {
3224                 pring = &psli->ring[i];
3225                 prev_pring_flag = pring->flag;
3226                 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3227                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
3228                 /*
3229                  * Error everything on the txq since these iocbs have not been
3230                  * given to the FW yet.
3231                  */
3232                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
3233                         if (iocb->vport != vport)
3234                                 continue;
3235                         list_move_tail(&iocb->list, &completions);
3236                         pring->txq_cnt--;
3237                 }
3238
3239                 /* Next issue ABTS for everything on the txcmplq */
3240                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
3241                                                                         list) {
3242                         if (iocb->vport != vport)
3243                                 continue;
3244                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3245                 }
3246
3247                 pring->flag = prev_pring_flag;
3248         }
3249
3250         spin_unlock_irqrestore(&phba->hbalock, flags);
3251
3252         while (!list_empty(&completions)) {
3253                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3254
3255                 if (!iocb->iocb_cmpl)
3256                         lpfc_sli_release_iocbq(phba, iocb);
3257                 else {
3258                         iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3259                         iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
3260                         (iocb->iocb_cmpl) (phba, iocb, iocb);
3261                 }
3262         }
3263         return 1;
3264 }
3265
3266 int
3267 lpfc_sli_hba_down(struct lpfc_hba *phba)
3268 {
3269         LIST_HEAD(completions);
3270         struct lpfc_sli *psli = &phba->sli;
3271         struct lpfc_sli_ring *pring;
3272         LPFC_MBOXQ_t *pmb;
3273         struct lpfc_iocbq *iocb;
3274         IOCB_t *cmd = NULL;
3275         int i;
3276         unsigned long flags = 0;
3277
3278         lpfc_hba_down_prep(phba);
3279
3280         lpfc_fabric_abort_hba(phba);
3281
3282         spin_lock_irqsave(&phba->hbalock, flags);
3283         for (i = 0; i < psli->num_rings; i++) {
3284                 pring = &psli->ring[i];
3285                 if (pring->ringno == LPFC_ELS_RING) /* Only slow rings */
3286                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
3287
3288                 /*
3289                  * Error everything on the txq since these iocbs have not been
3290                  * given to the FW yet.
3291                  */
3292                 list_splice_init(&pring->txq, &completions);
3293                 pring->txq_cnt = 0;
3294
3295         }
3296         spin_unlock_irqrestore(&phba->hbalock, flags);
3297
3298         while (!list_empty(&completions)) {
3299                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
3300                 cmd = &iocb->iocb;
3301
3302                 if (!iocb->iocb_cmpl)
3303                         lpfc_sli_release_iocbq(phba, iocb);
3304                 else {
3305                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
3306                         cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
3307                         (iocb->iocb_cmpl) (phba, iocb, iocb);
3308                 }
3309         }
3310
3311         /* Return any active mbox cmds */
3312         del_timer_sync(&psli->mbox_tmo);
3313         spin_lock_irqsave(&phba->hbalock, flags);
3314
3315         spin_lock(&phba->pport->work_port_lock);
3316         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3317         spin_unlock(&phba->pport->work_port_lock);
3318
3319         if (psli->mbox_active) {
3320                 list_add_tail(&psli->mbox_active->list, &completions);
3321                 psli->mbox_active = NULL;
3322                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3323         }
3324
3325         /* Return any pending or completed mbox cmds */
3326         list_splice_init(&phba->sli.mboxq, &completions);
3327         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
3328         INIT_LIST_HEAD(&psli->mboxq);
3329         INIT_LIST_HEAD(&psli->mboxq_cmpl);
3330
3331         spin_unlock_irqrestore(&phba->hbalock, flags);
3332
3333         while (!list_empty(&completions)) {
3334                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
3335                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
3336                 if (pmb->mbox_cmpl) {
3337                         pmb->mbox_cmpl(phba,pmb);
3338                 }
3339         }
3340         return 1;
3341 }
3342
3343 void
3344 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
3345 {
3346         uint32_t *src = srcp;
3347         uint32_t *dest = destp;
3348         uint32_t ldata;
3349         int i;
3350
3351         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
3352                 ldata = *src;
3353                 ldata = le32_to_cpu(ldata);
3354                 *dest = ldata;
3355                 src++;
3356                 dest++;
3357         }
3358 }
3359
3360 int
3361 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3362                          struct lpfc_dmabuf *mp)
3363 {
3364         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
3365            later */
3366         spin_lock_irq(&phba->hbalock);
3367         list_add_tail(&mp->list, &pring->postbufq);
3368         pring->postbufq_cnt++;
3369         spin_unlock_irq(&phba->hbalock);
3370         return 0;
3371 }
3372
3373
3374 struct lpfc_dmabuf *
3375 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3376                          dma_addr_t phys)
3377 {
3378         struct lpfc_dmabuf *mp, *next_mp;
3379         struct list_head *slp = &pring->postbufq;
3380
3381         /* Search postbufq, from the begining, looking for a match on phys */
3382         spin_lock_irq(&phba->hbalock);
3383         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
3384                 if (mp->phys == phys) {
3385                         list_del_init(&mp->list);
3386                         pring->postbufq_cnt--;
3387                         spin_unlock_irq(&phba->hbalock);
3388                         return mp;
3389                 }
3390         }
3391
3392         spin_unlock_irq(&phba->hbalock);
3393         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3394                         "0410 Cannot find virtual addr for mapped buf on "
3395                         "ring %d Data x%llx x%p x%p x%x\n",
3396                         pring->ringno, (unsigned long long)phys,
3397                         slp->next, slp->prev, pring->postbufq_cnt);
3398         return NULL;
3399 }
3400
3401 static void
3402 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3403                         struct lpfc_iocbq *rspiocb)
3404 {
3405         IOCB_t *irsp = &rspiocb->iocb;
3406         uint16_t abort_iotag, abort_context;
3407         struct lpfc_iocbq *abort_iocb;
3408         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
3409
3410         abort_iocb = NULL;
3411
3412         if (irsp->ulpStatus) {
3413                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
3414                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
3415
3416                 spin_lock_irq(&phba->hbalock);
3417                 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
3418                         abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
3419
3420                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
3421                                 "0327 Cannot abort els iocb %p "
3422                                 "with tag %x context %x, abort status %x, "
3423                                 "abort code %x\n",
3424                                 abort_iocb, abort_iotag, abort_context,
3425                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
3426
3427                 /*
3428                  * make sure we have the right iocbq before taking it
3429                  * off the txcmplq and try to call completion routine.
3430                  */
3431                 if (!abort_iocb ||
3432                     abort_iocb->iocb.ulpContext != abort_context ||
3433                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
3434                         spin_unlock_irq(&phba->hbalock);
3435                 else {
3436                         list_del_init(&abort_iocb->list);
3437                         pring->txcmplq_cnt--;
3438                         spin_unlock_irq(&phba->hbalock);
3439
3440                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3441                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
3442                         abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
3443                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
3444                 }
3445         }
3446
3447         lpfc_sli_release_iocbq(phba, cmdiocb);
3448         return;
3449 }
3450
3451 static void
3452 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3453                      struct lpfc_iocbq *rspiocb)
3454 {
3455         IOCB_t *irsp = &rspiocb->iocb;
3456
3457         /* ELS cmd tag <ulpIoTag> completes */
3458         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
3459                         "0133 Ignoring ELS cmd tag x%x completion Data: "
3460                         "x%x x%x x%x\n",
3461                         irsp->ulpIoTag, irsp->ulpStatus,
3462                         irsp->un.ulpWord[4], irsp->ulpTimeout);
3463         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
3464                 lpfc_ct_free_iocb(phba, cmdiocb);
3465         else
3466                 lpfc_els_free_iocb(phba, cmdiocb);
3467         return;
3468 }
3469
3470 int
3471 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3472                            struct lpfc_iocbq *cmdiocb)
3473 {
3474         struct lpfc_vport *vport = cmdiocb->vport;
3475         struct lpfc_iocbq *abtsiocbp;
3476         IOCB_t *icmd = NULL;
3477         IOCB_t *iabt = NULL;
3478         int retval = IOCB_ERROR;
3479
3480         /*
3481          * There are certain command types we don't want to abort.  And we
3482          * don't want to abort commands that are already in the process of
3483          * being aborted.
3484          */
3485         icmd = &cmdiocb->iocb;
3486         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
3487             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
3488             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
3489                 return 0;
3490
3491         /* If we're unloading, don't abort iocb on the ELS ring, but change the
3492          * callback so that nothing happens when it finishes.
3493          */
3494         if ((vport->load_flag & FC_UNLOADING) &&
3495             (pring->ringno == LPFC_ELS_RING)) {
3496                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
3497                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
3498                 else
3499                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
3500                 goto abort_iotag_exit;
3501         }
3502
3503         /* issue ABTS for this IOCB based on iotag */
3504         abtsiocbp = __lpfc_sli_get_iocbq(phba);
3505         if (abtsiocbp == NULL)
3506                 return 0;
3507
3508         /* This signals the response to set the correct status
3509          * before calling the completion handler.
3510          */
3511         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
3512
3513         iabt = &abtsiocbp->iocb;
3514         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
3515         iabt->un.acxri.abortContextTag = icmd->ulpContext;
3516         iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
3517         iabt->ulpLe = 1;
3518         iabt->ulpClass = icmd->ulpClass;
3519
3520         if (phba->link_state >= LPFC_LINK_UP)
3521                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
3522         else
3523                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
3524
3525         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
3526
3527         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
3528                          "0339 Abort xri x%x, original iotag x%x, "
3529                          "abort cmd iotag x%x\n",
3530                          iabt->un.acxri.abortContextTag,
3531                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
3532         retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
3533
3534 abort_iotag_exit:
3535         /*
3536          * Caller to this routine should check for IOCB_ERROR
3537          * and handle it properly.  This routine no longer removes
3538          * iocb off txcmplq and call compl in case of IOCB_ERROR.
3539          */
3540         return retval;
3541 }
3542
3543 static int
3544 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
3545                            uint16_t tgt_id, uint64_t lun_id,
3546                            lpfc_ctx_cmd ctx_cmd)
3547 {
3548         struct lpfc_scsi_buf *lpfc_cmd;
3549         struct scsi_cmnd *cmnd;
3550         int rc = 1;
3551
3552         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
3553                 return rc;
3554
3555         if (iocbq->vport != vport)
3556                 return rc;
3557
3558         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
3559         cmnd = lpfc_cmd->pCmd;
3560
3561         if (cmnd == NULL)
3562                 return rc;
3563
3564         switch (ctx_cmd) {
3565         case LPFC_CTX_LUN:
3566                 if ((cmnd->device->id == tgt_id) &&
3567                     (cmnd->device->lun == lun_id))
3568                         rc = 0;
3569                 break;
3570         case LPFC_CTX_TGT:
3571                 if (cmnd->device->id == tgt_id)
3572                         rc = 0;
3573                 break;
3574         case LPFC_CTX_HOST:
3575                 rc = 0;
3576                 break;
3577         default:
3578                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
3579                         __FUNCTION__, ctx_cmd);
3580                 break;
3581         }
3582
3583         return rc;
3584 }
3585
3586 int
3587 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
3588                   lpfc_ctx_cmd ctx_cmd)
3589 {
3590         struct lpfc_hba *phba = vport->phba;
3591         struct lpfc_iocbq *iocbq;
3592         int sum, i;
3593
3594         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
3595                 iocbq = phba->sli.iocbq_lookup[i];
3596
3597                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
3598                                                 ctx_cmd) == 0)
3599                         sum++;
3600         }
3601
3602         return sum;
3603 }
3604
3605 void
3606 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
3607                         struct lpfc_iocbq *rspiocb)
3608 {
3609         lpfc_sli_release_iocbq(phba, cmdiocb);
3610         return;
3611 }
3612
3613 int
3614 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
3615                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
3616 {
3617         struct lpfc_hba *phba = vport->phba;
3618         struct lpfc_iocbq *iocbq;
3619         struct lpfc_iocbq *abtsiocb;
3620         IOCB_t *cmd = NULL;
3621         int errcnt = 0, ret_val = 0;
3622         int i;
3623
3624         for (i = 1; i <= phba->sli.last_iotag; i++) {
3625                 iocbq = phba->sli.iocbq_lookup[i];
3626
3627                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
3628                                                abort_cmd) != 0)
3629                         continue;
3630
3631                 /* issue ABTS for this IOCB based on iotag */
3632                 abtsiocb = lpfc_sli_get_iocbq(phba);
3633                 if (abtsiocb == NULL) {
3634                         errcnt++;
3635                         continue;
3636                 }
3637
3638                 cmd = &iocbq->iocb;
3639                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
3640                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
3641                 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
3642                 abtsiocb->iocb.ulpLe = 1;
3643                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
3644                 abtsiocb->vport = phba->pport;
3645
3646                 if (lpfc_is_link_up(phba))
3647                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
3648                 else
3649                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
3650
3651                 /* Setup callback routine and issue the command. */
3652                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
3653                 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
3654                 if (ret_val == IOCB_ERROR) {
3655                         lpfc_sli_release_iocbq(phba, abtsiocb);
3656                         errcnt++;
3657                         continue;
3658                 }
3659         }
3660
3661         return errcnt;
3662 }
3663
3664 static void
3665 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
3666                         struct lpfc_iocbq *cmdiocbq,
3667                         struct lpfc_iocbq *rspiocbq)
3668 {
3669         wait_queue_head_t *pdone_q;
3670         unsigned long iflags;
3671
3672         spin_lock_irqsave(&phba->hbalock, iflags);
3673         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
3674         if (cmdiocbq->context2 && rspiocbq)
3675                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
3676                        &rspiocbq->iocb, sizeof(IOCB_t));
3677
3678         pdone_q = cmdiocbq->context_un.wait_queue;
3679         if (pdone_q)
3680                 wake_up(pdone_q);
3681         spin_unlock_irqrestore(&phba->hbalock, iflags);
3682         return;
3683 }
3684
3685 /*
3686  * Issue the caller's iocb and wait for its completion, but no longer than the
3687  * caller's timeout.  Note that iocb_flags is cleared before the
3688  * lpfc_sli_issue_call since the wake routine sets a unique value and by
3689  * definition this is a wait function.
3690  */
3691
3692 int
3693 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
3694                          struct lpfc_sli_ring *pring,
3695                          struct lpfc_iocbq *piocb,
3696                          struct lpfc_iocbq *prspiocbq,
3697                          uint32_t timeout)
3698 {
3699         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
3700         long timeleft, timeout_req = 0;
3701         int retval = IOCB_SUCCESS;
3702         uint32_t creg_val;
3703
3704         /*
3705          * If the caller has provided a response iocbq buffer, then context2
3706          * is NULL or its an error.
3707          */
3708         if (prspiocbq) {
3709                 if (piocb->context2)
3710                         return IOCB_ERROR;
3711                 piocb->context2 = prspiocbq;
3712         }
3713
3714         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
3715         piocb->context_un.wait_queue = &done_q;
3716         piocb->iocb_flag &= ~LPFC_IO_WAKE;
3717
3718         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3719                 creg_val = readl(phba->HCregaddr);
3720                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
3721                 writel(creg_val, phba->HCregaddr);
3722                 readl(phba->HCregaddr); /* flush */
3723         }
3724
3725         retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
3726         if (retval == IOCB_SUCCESS) {
3727                 timeout_req = timeout * HZ;
3728                 timeleft = wait_event_timeout(done_q,
3729                                 piocb->iocb_flag & LPFC_IO_WAKE,
3730                                 timeout_req);
3731
3732                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
3733                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3734                                         "0331 IOCB wake signaled\n");
3735                 } else if (timeleft == 0) {
3736                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3737                                         "0338 IOCB wait timeout error - no "
3738                                         "wake response Data x%x\n", timeout);
3739                         retval = IOCB_TIMEDOUT;
3740                 } else {
3741                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3742                                         "0330 IOCB wake NOT set, "
3743                                         "Data x%x x%lx\n",
3744                                         timeout, (timeleft / jiffies));
3745                         retval = IOCB_TIMEDOUT;
3746                 }
3747         } else {
3748                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3749                                 ":0332 IOCB wait issue failed, Data x%x\n",
3750                                 retval);
3751                 retval = IOCB_ERROR;
3752         }
3753
3754         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
3755                 creg_val = readl(phba->HCregaddr);
3756                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
3757                 writel(creg_val, phba->HCregaddr);
3758                 readl(phba->HCregaddr); /* flush */
3759         }
3760
3761         if (prspiocbq)
3762                 piocb->context2 = NULL;
3763
3764         piocb->context_un.wait_queue = NULL;
3765         piocb->iocb_cmpl = NULL;
3766         return retval;
3767 }
3768
3769 int
3770 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
3771                          uint32_t timeout)
3772 {
3773         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
3774         int retval;
3775         unsigned long flag;
3776
3777         /* The caller must leave context1 empty. */
3778         if (pmboxq->context1 != 0)
3779                 return MBX_NOT_FINISHED;
3780
3781         /* setup wake call as IOCB callback */
3782         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
3783         /* setup context field to pass wait_queue pointer to wake function  */
3784         pmboxq->context1 = &done_q;
3785
3786         /* now issue the command */
3787         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
3788
3789         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
3790                 wait_event_interruptible_timeout(done_q,
3791                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
3792                                 timeout * HZ);
3793
3794                 spin_lock_irqsave(&phba->hbalock, flag);
3795                 pmboxq->context1 = NULL;
3796                 /*
3797                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
3798                  * else do not free the resources.
3799                  */
3800                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
3801                         retval = MBX_SUCCESS;
3802                 else {
3803                         retval = MBX_TIMEOUT;
3804                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3805                 }
3806                 spin_unlock_irqrestore(&phba->hbalock, flag);
3807         }
3808
3809         return retval;
3810 }
3811
3812 int
3813 lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
3814 {
3815         struct lpfc_vport *vport = phba->pport;
3816         int i = 0;
3817         uint32_t ha_copy;
3818
3819         while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
3820                 if (i++ > LPFC_MBOX_TMO * 1000)
3821                         return 1;
3822
3823                 /*
3824                  * Call lpfc_sli_handle_mb_event only if a mailbox cmd
3825                  * did finish. This way we won't get the misleading
3826                  * "Stray Mailbox Interrupt" message.
3827                  */
3828                 spin_lock_irq(&phba->hbalock);
3829                 ha_copy = phba->work_ha;
3830                 phba->work_ha &= ~HA_MBATT;
3831                 spin_unlock_irq(&phba->hbalock);
3832
3833                 if (ha_copy & HA_MBATT)
3834                         if (lpfc_sli_handle_mb_event(phba) == 0)
3835                                 i = 0;
3836
3837                 msleep(1);
3838         }
3839
3840         return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
3841 }
3842
3843 irqreturn_t
3844 lpfc_intr_handler(int irq, void *dev_id)
3845 {
3846         struct lpfc_hba  *phba;
3847         uint32_t ha_copy;
3848         uint32_t work_ha_copy;
3849         unsigned long status;
3850         int i;
3851         uint32_t control;
3852
3853         MAILBOX_t *mbox, *pmbox;
3854         struct lpfc_vport *vport;
3855         struct lpfc_nodelist *ndlp;
3856         struct lpfc_dmabuf *mp;
3857         LPFC_MBOXQ_t *pmb;
3858         int rc;
3859
3860         /*
3861          * Get the driver's phba structure from the dev_id and
3862          * assume the HBA is not interrupting.
3863          */
3864         phba = (struct lpfc_hba *) dev_id;
3865
3866         if (unlikely(!phba))
3867                 return IRQ_NONE;
3868
3869         /* If the pci channel is offline, ignore all the interrupts. */
3870         if (unlikely(pci_channel_offline(phba->pcidev)))
3871                 return IRQ_NONE;
3872
3873         phba->sli.slistat.sli_intr++;
3874
3875         /*
3876          * Call the HBA to see if it is interrupting.  If not, don't claim
3877          * the interrupt
3878          */
3879
3880         /* Ignore all interrupts during initialization. */
3881         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
3882                 return IRQ_NONE;
3883
3884         /*
3885          * Read host attention register to determine interrupt source
3886          * Clear Attention Sources, except Error Attention (to
3887          * preserve status) and Link Attention
3888          */
3889         spin_lock(&phba->hbalock);
3890         ha_copy = readl(phba->HAregaddr);
3891         /* If somebody is waiting to handle an eratt don't process it
3892          * here.  The brdkill function will do this.
3893          */
3894         if (phba->link_flag & LS_IGNORE_ERATT)
3895                 ha_copy &= ~HA_ERATT;
3896         writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
3897         readl(phba->HAregaddr); /* flush */
3898         spin_unlock(&phba->hbalock);
3899
3900         if (unlikely(!ha_copy))
3901                 return IRQ_NONE;
3902
3903         work_ha_copy = ha_copy & phba->work_ha_mask;
3904
3905         if (unlikely(work_ha_copy)) {
3906                 if (work_ha_copy & HA_LATT) {
3907                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
3908                                 /*
3909                                  * Turn off Link Attention interrupts
3910                                  * until CLEAR_LA done
3911                                  */
3912                                 spin_lock(&phba->hbalock);
3913                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
3914                                 control = readl(phba->HCregaddr);
3915                                 control &= ~HC_LAINT_ENA;
3916                                 writel(control, phba->HCregaddr);
3917                                 readl(phba->HCregaddr); /* flush */
3918                                 spin_unlock(&phba->hbalock);
3919                         }
3920                         else
3921                                 work_ha_copy &= ~HA_LATT;
3922                 }
3923
3924                 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
3925                         /*
3926                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
3927                          * the only slow ring.
3928                          */
3929                         status = (work_ha_copy &
3930                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
3931                         status >>= (4*LPFC_ELS_RING);
3932                         if (status & HA_RXMASK) {
3933                                 spin_lock(&phba->hbalock);
3934                                 control = readl(phba->HCregaddr);
3935
3936                                 lpfc_debugfs_slow_ring_trc(phba,
3937                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
3938                                 control, status,
3939                                 (uint32_t)phba->sli.slistat.sli_intr);
3940
3941                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
3942                                         lpfc_debugfs_slow_ring_trc(phba,
3943                                                 "ISR Disable ring:"
3944                                                 "pwork:x%x hawork:x%x wait:x%x",
3945                                                 phba->work_ha, work_ha_copy,
3946                                                 (uint32_t)((unsigned long)
3947                                                 phba->work_wait));
3948
3949                                         control &=
3950                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
3951                                         writel(control, phba->HCregaddr);
3952                                         readl(phba->HCregaddr); /* flush */
3953                                 }
3954                                 else {
3955                                         lpfc_debugfs_slow_ring_trc(phba,
3956                                                 "ISR slow ring:   pwork:"
3957                                                 "x%x hawork:x%x wait:x%x",
3958                                                 phba->work_ha, work_ha_copy,
3959                                                 (uint32_t)((unsigned long)
3960                                                 phba->work_wait));
3961                                 }
3962                                 spin_unlock(&phba->hbalock);
3963                         }
3964                 }
3965
3966                 if (work_ha_copy & HA_ERATT) {
3967                         phba->link_state = LPFC_HBA_ERROR;
3968                         /*
3969                          * There was a link/board error.  Read the
3970                          * status register to retrieve the error event
3971                          * and process it.
3972                          */
3973                         phba->sli.slistat.err_attn_event++;
3974                         /* Save status info */
3975                         phba->work_hs = readl(phba->HSregaddr);
3976                         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
3977                         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
3978
3979                         /* Clear Chip error bit */
3980                         writel(HA_ERATT, phba->HAregaddr);
3981                         readl(phba->HAregaddr); /* flush */
3982                         phba->pport->stopped = 1;
3983                 }
3984
3985                 if ((work_ha_copy & HA_MBATT) &&
3986                     (phba->sli.mbox_active)) {
3987                         pmb = phba->sli.mbox_active;
3988                         pmbox = &pmb->mb;
3989                         mbox = &phba->slim2p->mbx;
3990                         vport = pmb->vport;
3991
3992                         /* First check out the status word */
3993                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
3994                         if (pmbox->mbxOwner != OWN_HOST) {
3995                                 /*
3996                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
3997                                  * mbxStatus <status>
3998                                  */
3999                                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX |
4000                                                 LOG_SLI,
4001                                                 "(%d):0304 Stray Mailbox "
4002                                                 "Interrupt mbxCommand x%x "
4003                                                 "mbxStatus x%x\n",
4004                                                 (vport ? vport->vpi : 0),
4005                                                 pmbox->mbxCommand,
4006                                                 pmbox->mbxStatus);
4007                         }
4008                         phba->last_completion_time = jiffies;
4009                         del_timer_sync(&phba->sli.mbox_tmo);
4010
4011                         phba->sli.mbox_active = NULL;
4012                         if (pmb->mbox_cmpl) {
4013                                 lpfc_sli_pcimem_bcopy(mbox, pmbox,
4014                                                       MAILBOX_CMD_SIZE);
4015                         }
4016                         if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
4017                                 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
4018
4019                                 lpfc_debugfs_disc_trc(vport,
4020                                         LPFC_DISC_TRC_MBOX_VPORT,
4021                                         "MBOX dflt rpi: : status:x%x rpi:x%x",
4022                                         (uint32_t)pmbox->mbxStatus,
4023                                         pmbox->un.varWords[0], 0);
4024
4025                                 if ( !pmbox->mbxStatus) {
4026                                         mp = (struct lpfc_dmabuf *)
4027                                                 (pmb->context1);
4028                                         ndlp = (struct lpfc_nodelist *)
4029                                                 pmb->context2;
4030
4031                                         /* Reg_LOGIN of dflt RPI was successful.
4032                                          * new lets get rid of the RPI using the
4033                                          * same mbox buffer.
4034                                          */
4035                                         lpfc_unreg_login(phba, vport->vpi,
4036                                                 pmbox->un.varWords[0], pmb);
4037                                         pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
4038                                         pmb->context1 = mp;
4039                                         pmb->context2 = ndlp;
4040                                         pmb->vport = vport;
4041                                         spin_lock(&phba->hbalock);
4042                                         phba->sli.sli_flag &=
4043                                                 ~LPFC_SLI_MBOX_ACTIVE;
4044                                         spin_unlock(&phba->hbalock);
4045                                         goto send_current_mbox;
4046                                 }
4047                         }
4048                         spin_lock(&phba->pport->work_port_lock);
4049                         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4050                         spin_unlock(&phba->pport->work_port_lock);
4051                         lpfc_mbox_cmpl_put(phba, pmb);
4052                 }
4053                 if ((work_ha_copy & HA_MBATT) &&
4054                     (phba->sli.mbox_active == NULL)) {
4055 send_next_mbox:
4056                         spin_lock(&phba->hbalock);
4057                         phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4058                         pmb = lpfc_mbox_get(phba);
4059                         spin_unlock(&phba->hbalock);
4060 send_current_mbox:
4061                         /* Process next mailbox command if there is one */
4062                         if (pmb != NULL) {
4063                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
4064                                 if (rc == MBX_NOT_FINISHED) {
4065                                         pmb->mb.mbxStatus = MBX_NOT_FINISHED;
4066                                         lpfc_mbox_cmpl_put(phba, pmb);
4067                                         goto send_next_mbox;
4068                                 }
4069                         } else {
4070                                 /* Turn on IOCB processing */
4071                                 for (i = 0; i < phba->sli.num_rings; i++)
4072                                         lpfc_sli_turn_on_ring(phba, i);
4073                         }
4074
4075                 }
4076
4077                 spin_lock(&phba->hbalock);
4078                 phba->work_ha |= work_ha_copy;
4079                 if (phba->work_wait)
4080                         lpfc_worker_wake_up(phba);
4081                 spin_unlock(&phba->hbalock);
4082         }
4083
4084         ha_copy &= ~(phba->work_ha_mask);
4085
4086         /*
4087          * Process all events on FCP ring.  Take the optimized path for
4088          * FCP IO.  Any other IO is slow path and is handled by
4089          * the worker thread.
4090          */
4091         status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
4092         status >>= (4*LPFC_FCP_RING);
4093         if (status & HA_RXMASK)
4094                 lpfc_sli_handle_fast_ring_event(phba,
4095                                                 &phba->sli.ring[LPFC_FCP_RING],
4096                                                 status);
4097
4098         if (phba->cfg_multi_ring_support == 2) {
4099                 /*
4100                  * Process all events on extra ring.  Take the optimized path
4101                  * for extra ring IO.  Any other IO is slow path and is handled
4102                  * by the worker thread.
4103                  */
4104                 status = (ha_copy & (HA_RXMASK  << (4*LPFC_EXTRA_RING)));
4105                 status >>= (4*LPFC_EXTRA_RING);
4106                 if (status & HA_RXMASK) {
4107                         lpfc_sli_handle_fast_ring_event(phba,
4108                                         &phba->sli.ring[LPFC_EXTRA_RING],
4109                                         status);
4110                 }
4111         }
4112         return IRQ_HANDLED;
4113
4114 } /* lpfc_intr_handler */