Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / 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-2008 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_nl.h"
36 #include "lpfc_disc.h"
37 #include "lpfc_scsi.h"
38 #include "lpfc.h"
39 #include "lpfc_crtn.h"
40 #include "lpfc_logmsg.h"
41 #include "lpfc_compat.h"
42 #include "lpfc_debugfs.h"
43
44 /*
45  * Define macro to log: Mailbox command x%x cannot issue Data
46  * This allows multiple uses of lpfc_msgBlk0311
47  * w/o perturbing log msg utility.
48  */
49 #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
50                         lpfc_printf_log(phba, \
51                                 KERN_INFO, \
52                                 LOG_MBOX | LOG_SLI, \
53                                 "(%d):0311 Mailbox command x%x cannot " \
54                                 "issue Data: x%x x%x x%x\n", \
55                                 pmbox->vport ? pmbox->vport->vpi : 0, \
56                                 pmbox->mb.mbxCommand,           \
57                                 phba->pport->port_state,        \
58                                 psli->sli_flag, \
59                                 flag)
60
61
62 /* There are only four IOCB completion types. */
63 typedef enum _lpfc_iocb_type {
64         LPFC_UNKNOWN_IOCB,
65         LPFC_UNSOL_IOCB,
66         LPFC_SOL_IOCB,
67         LPFC_ABORT_IOCB
68 } lpfc_iocb_type;
69
70 /**
71  * lpfc_cmd_iocb: Get next command iocb entry in the ring.
72  * @phba: Pointer to HBA context object.
73  * @pring: Pointer to driver SLI ring object.
74  *
75  * This function returns pointer to next command iocb entry
76  * in the command ring. The caller must hold hbalock to prevent
77  * other threads consume the next command iocb.
78  * SLI-2/SLI-3 provide different sized iocbs.
79  **/
80 static inline IOCB_t *
81 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
82 {
83         return (IOCB_t *) (((char *) pring->cmdringaddr) +
84                            pring->cmdidx * phba->iocb_cmd_size);
85 }
86
87 /**
88  * lpfc_resp_iocb: Get next response iocb entry in the ring.
89  * @phba: Pointer to HBA context object.
90  * @pring: Pointer to driver SLI ring object.
91  *
92  * This function returns pointer to next response iocb entry
93  * in the response ring. The caller must hold hbalock to make sure
94  * that no other thread consume the next response iocb.
95  * SLI-2/SLI-3 provide different sized iocbs.
96  **/
97 static inline IOCB_t *
98 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
99 {
100         return (IOCB_t *) (((char *) pring->rspringaddr) +
101                            pring->rspidx * phba->iocb_rsp_size);
102 }
103
104 /**
105  * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
106  * @phba: Pointer to HBA context object.
107  *
108  * This function is called with hbalock held. This function
109  * allocates a new driver iocb object from the iocb pool. If the
110  * allocation is successful, it returns pointer to the newly
111  * allocated iocb object else it returns NULL.
112  **/
113 static struct lpfc_iocbq *
114 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
115 {
116         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
117         struct lpfc_iocbq * iocbq = NULL;
118
119         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
120         return iocbq;
121 }
122
123 /**
124  * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
125  * @phba: Pointer to HBA context object.
126  *
127  * This function is called with no lock held. This function
128  * allocates a new driver iocb object from the iocb pool. If the
129  * allocation is successful, it returns pointer to the newly
130  * allocated iocb object else it returns NULL.
131  **/
132 struct lpfc_iocbq *
133 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
134 {
135         struct lpfc_iocbq * iocbq = NULL;
136         unsigned long iflags;
137
138         spin_lock_irqsave(&phba->hbalock, iflags);
139         iocbq = __lpfc_sli_get_iocbq(phba);
140         spin_unlock_irqrestore(&phba->hbalock, iflags);
141         return iocbq;
142 }
143
144 /**
145  * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
146  * @phba: Pointer to HBA context object.
147  * @iocbq: Pointer to driver iocb object.
148  *
149  * This function is called with hbalock held to release driver
150  * iocb object to the iocb pool. The iotag in the iocb object
151  * does not change for each use of the iocb object. This function
152  * clears all other fields of the iocb object when it is freed.
153  **/
154 static void
155 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
156 {
157         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
158
159         /*
160          * Clean all volatile data fields, preserve iotag and node struct.
161          */
162         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
163         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
164 }
165
166 /**
167  * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
168  * @phba: Pointer to HBA context object.
169  * @iocbq: Pointer to driver iocb object.
170  *
171  * This function is called with no lock held to release the iocb to
172  * iocb pool.
173  **/
174 void
175 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
176 {
177         unsigned long iflags;
178
179         /*
180          * Clean all volatile data fields, preserve iotag and node struct.
181          */
182         spin_lock_irqsave(&phba->hbalock, iflags);
183         __lpfc_sli_release_iocbq(phba, iocbq);
184         spin_unlock_irqrestore(&phba->hbalock, iflags);
185 }
186
187 /**
188  * lpfc_sli_iocb_cmd_type: Get the iocb type.
189  * @iocb_cmnd : iocb command code.
190  *
191  * This function is called by ring event handler function to get the iocb type.
192  * This function translates the iocb command to an iocb command type used to
193  * decide the final disposition of each completed IOCB.
194  * The function returns
195  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
196  * LPFC_SOL_IOCB     if it is a solicited iocb completion
197  * LPFC_ABORT_IOCB   if it is an abort iocb
198  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
199  *
200  * The caller is not required to hold any lock.
201  **/
202 static lpfc_iocb_type
203 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
204 {
205         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
206
207         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
208                 return 0;
209
210         switch (iocb_cmnd) {
211         case CMD_XMIT_SEQUENCE_CR:
212         case CMD_XMIT_SEQUENCE_CX:
213         case CMD_XMIT_BCAST_CN:
214         case CMD_XMIT_BCAST_CX:
215         case CMD_ELS_REQUEST_CR:
216         case CMD_ELS_REQUEST_CX:
217         case CMD_CREATE_XRI_CR:
218         case CMD_CREATE_XRI_CX:
219         case CMD_GET_RPI_CN:
220         case CMD_XMIT_ELS_RSP_CX:
221         case CMD_GET_RPI_CR:
222         case CMD_FCP_IWRITE_CR:
223         case CMD_FCP_IWRITE_CX:
224         case CMD_FCP_IREAD_CR:
225         case CMD_FCP_IREAD_CX:
226         case CMD_FCP_ICMND_CR:
227         case CMD_FCP_ICMND_CX:
228         case CMD_FCP_TSEND_CX:
229         case CMD_FCP_TRSP_CX:
230         case CMD_FCP_TRECEIVE_CX:
231         case CMD_FCP_AUTO_TRSP_CX:
232         case CMD_ADAPTER_MSG:
233         case CMD_ADAPTER_DUMP:
234         case CMD_XMIT_SEQUENCE64_CR:
235         case CMD_XMIT_SEQUENCE64_CX:
236         case CMD_XMIT_BCAST64_CN:
237         case CMD_XMIT_BCAST64_CX:
238         case CMD_ELS_REQUEST64_CR:
239         case CMD_ELS_REQUEST64_CX:
240         case CMD_FCP_IWRITE64_CR:
241         case CMD_FCP_IWRITE64_CX:
242         case CMD_FCP_IREAD64_CR:
243         case CMD_FCP_IREAD64_CX:
244         case CMD_FCP_ICMND64_CR:
245         case CMD_FCP_ICMND64_CX:
246         case CMD_FCP_TSEND64_CX:
247         case CMD_FCP_TRSP64_CX:
248         case CMD_FCP_TRECEIVE64_CX:
249         case CMD_GEN_REQUEST64_CR:
250         case CMD_GEN_REQUEST64_CX:
251         case CMD_XMIT_ELS_RSP64_CX:
252                 type = LPFC_SOL_IOCB;
253                 break;
254         case CMD_ABORT_XRI_CN:
255         case CMD_ABORT_XRI_CX:
256         case CMD_CLOSE_XRI_CN:
257         case CMD_CLOSE_XRI_CX:
258         case CMD_XRI_ABORTED_CX:
259         case CMD_ABORT_MXRI64_CN:
260                 type = LPFC_ABORT_IOCB;
261                 break;
262         case CMD_RCV_SEQUENCE_CX:
263         case CMD_RCV_ELS_REQ_CX:
264         case CMD_RCV_SEQUENCE64_CX:
265         case CMD_RCV_ELS_REQ64_CX:
266         case CMD_ASYNC_STATUS:
267         case CMD_IOCB_RCV_SEQ64_CX:
268         case CMD_IOCB_RCV_ELS64_CX:
269         case CMD_IOCB_RCV_CONT64_CX:
270         case CMD_IOCB_RET_XRI64_CX:
271                 type = LPFC_UNSOL_IOCB;
272                 break;
273         case CMD_IOCB_XMIT_MSEQ64_CR:
274         case CMD_IOCB_XMIT_MSEQ64_CX:
275         case CMD_IOCB_RCV_SEQ_LIST64_CX:
276         case CMD_IOCB_RCV_ELS_LIST64_CX:
277         case CMD_IOCB_CLOSE_EXTENDED_CN:
278         case CMD_IOCB_ABORT_EXTENDED_CN:
279         case CMD_IOCB_RET_HBQE64_CN:
280         case CMD_IOCB_FCP_IBIDIR64_CR:
281         case CMD_IOCB_FCP_IBIDIR64_CX:
282         case CMD_IOCB_FCP_ITASKMGT64_CX:
283         case CMD_IOCB_LOGENTRY_CN:
284         case CMD_IOCB_LOGENTRY_ASYNC_CN:
285                 printk("%s - Unhandled SLI-3 Command x%x\n",
286                                 __func__, iocb_cmnd);
287                 type = LPFC_UNKNOWN_IOCB;
288                 break;
289         default:
290                 type = LPFC_UNKNOWN_IOCB;
291                 break;
292         }
293
294         return type;
295 }
296
297 /**
298  * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
299  * @phba: Pointer to HBA context object.
300  *
301  * This function is called from SLI initialization code
302  * to configure every ring of the HBA's SLI interface. The
303  * caller is not required to hold any lock. This function issues
304  * a config_ring mailbox command for each ring.
305  * This function returns zero if successful else returns a negative
306  * error code.
307  **/
308 static int
309 lpfc_sli_ring_map(struct lpfc_hba *phba)
310 {
311         struct lpfc_sli *psli = &phba->sli;
312         LPFC_MBOXQ_t *pmb;
313         MAILBOX_t *pmbox;
314         int i, rc, ret = 0;
315
316         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
317         if (!pmb)
318                 return -ENOMEM;
319         pmbox = &pmb->mb;
320         phba->link_state = LPFC_INIT_MBX_CMDS;
321         for (i = 0; i < psli->num_rings; i++) {
322                 lpfc_config_ring(phba, i, pmb);
323                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
324                 if (rc != MBX_SUCCESS) {
325                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
326                                         "0446 Adapter failed to init (%d), "
327                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
328                                         "ring %d\n",
329                                         rc, pmbox->mbxCommand,
330                                         pmbox->mbxStatus, i);
331                         phba->link_state = LPFC_HBA_ERROR;
332                         ret = -ENXIO;
333                         break;
334                 }
335         }
336         mempool_free(pmb, phba->mbox_mem_pool);
337         return ret;
338 }
339
340 /**
341  * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
342  * @phba: Pointer to HBA context object.
343  * @pring: Pointer to driver SLI ring object.
344  * @piocb: Pointer to the driver iocb object.
345  *
346  * This function is called with hbalock held. The function adds the
347  * new iocb to txcmplq of the given ring. This function always returns
348  * 0. If this function is called for ELS ring, this function checks if
349  * there is a vport associated with the ELS command. This function also
350  * starts els_tmofunc timer if this is an ELS command.
351  **/
352 static int
353 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
354                         struct lpfc_iocbq *piocb)
355 {
356         list_add_tail(&piocb->list, &pring->txcmplq);
357         pring->txcmplq_cnt++;
358         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
359            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
360            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
361                 if (!piocb->vport)
362                         BUG();
363                 else
364                         mod_timer(&piocb->vport->els_tmofunc,
365                                   jiffies + HZ * (phba->fc_ratov << 1));
366         }
367
368
369         return 0;
370 }
371
372 /**
373  * lpfc_sli_ringtx_get: Get first element of the txq.
374  * @phba: Pointer to HBA context object.
375  * @pring: Pointer to driver SLI ring object.
376  *
377  * This function is called with hbalock held to get next
378  * iocb in txq of the given ring. If there is any iocb in
379  * the txq, the function returns first iocb in the list after
380  * removing the iocb from the list, else it returns NULL.
381  **/
382 static struct lpfc_iocbq *
383 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
384 {
385         struct lpfc_iocbq *cmd_iocb;
386
387         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
388         if (cmd_iocb != NULL)
389                 pring->txq_cnt--;
390         return cmd_iocb;
391 }
392
393 /**
394  * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
395  * @phba: Pointer to HBA context object.
396  * @pring: Pointer to driver SLI ring object.
397  *
398  * This function is called with hbalock held and the caller must post the
399  * iocb without releasing the lock. If the caller releases the lock,
400  * iocb slot returned by the function is not guaranteed to be available.
401  * The function returns pointer to the next available iocb slot if there
402  * is available slot in the ring, else it returns NULL.
403  * If the get index of the ring is ahead of the put index, the function
404  * will post an error attention event to the worker thread to take the
405  * HBA to offline state.
406  **/
407 static IOCB_t *
408 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
409 {
410         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
411         uint32_t  max_cmd_idx = pring->numCiocb;
412         if ((pring->next_cmdidx == pring->cmdidx) &&
413            (++pring->next_cmdidx >= max_cmd_idx))
414                 pring->next_cmdidx = 0;
415
416         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
417
418                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
419
420                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
421                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
422                                         "0315 Ring %d issue: portCmdGet %d "
423                                         "is bigger than cmd ring %d\n",
424                                         pring->ringno,
425                                         pring->local_getidx, max_cmd_idx);
426
427                         phba->link_state = LPFC_HBA_ERROR;
428                         /*
429                          * All error attention handlers are posted to
430                          * worker thread
431                          */
432                         phba->work_ha |= HA_ERATT;
433                         phba->work_hs = HS_FFER3;
434
435                         lpfc_worker_wake_up(phba);
436
437                         return NULL;
438                 }
439
440                 if (pring->local_getidx == pring->next_cmdidx)
441                         return NULL;
442         }
443
444         return lpfc_cmd_iocb(phba, pring);
445 }
446
447 /**
448  * lpfc_sli_next_iotag: Get an iotag for the iocb.
449  * @phba: Pointer to HBA context object.
450  * @iocbq: Pointer to driver iocb object.
451  *
452  * This function gets an iotag for the iocb. If there is no unused iotag and
453  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
454  * array and assigns a new iotag.
455  * The function returns the allocated iotag if successful, else returns zero.
456  * Zero is not a valid iotag.
457  * The caller is not required to hold any lock.
458  **/
459 uint16_t
460 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
461 {
462         struct lpfc_iocbq **new_arr;
463         struct lpfc_iocbq **old_arr;
464         size_t new_len;
465         struct lpfc_sli *psli = &phba->sli;
466         uint16_t iotag;
467
468         spin_lock_irq(&phba->hbalock);
469         iotag = psli->last_iotag;
470         if(++iotag < psli->iocbq_lookup_len) {
471                 psli->last_iotag = iotag;
472                 psli->iocbq_lookup[iotag] = iocbq;
473                 spin_unlock_irq(&phba->hbalock);
474                 iocbq->iotag = iotag;
475                 return iotag;
476         } else if (psli->iocbq_lookup_len < (0xffff
477                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
478                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
479                 spin_unlock_irq(&phba->hbalock);
480                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
481                                   GFP_KERNEL);
482                 if (new_arr) {
483                         spin_lock_irq(&phba->hbalock);
484                         old_arr = psli->iocbq_lookup;
485                         if (new_len <= psli->iocbq_lookup_len) {
486                                 /* highly unprobable case */
487                                 kfree(new_arr);
488                                 iotag = psli->last_iotag;
489                                 if(++iotag < psli->iocbq_lookup_len) {
490                                         psli->last_iotag = iotag;
491                                         psli->iocbq_lookup[iotag] = iocbq;
492                                         spin_unlock_irq(&phba->hbalock);
493                                         iocbq->iotag = iotag;
494                                         return iotag;
495                                 }
496                                 spin_unlock_irq(&phba->hbalock);
497                                 return 0;
498                         }
499                         if (psli->iocbq_lookup)
500                                 memcpy(new_arr, old_arr,
501                                        ((psli->last_iotag  + 1) *
502                                         sizeof (struct lpfc_iocbq *)));
503                         psli->iocbq_lookup = new_arr;
504                         psli->iocbq_lookup_len = new_len;
505                         psli->last_iotag = iotag;
506                         psli->iocbq_lookup[iotag] = iocbq;
507                         spin_unlock_irq(&phba->hbalock);
508                         iocbq->iotag = iotag;
509                         kfree(old_arr);
510                         return iotag;
511                 }
512         } else
513                 spin_unlock_irq(&phba->hbalock);
514
515         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
516                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
517                         psli->last_iotag);
518
519         return 0;
520 }
521
522 /**
523  * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
524  * @phba: Pointer to HBA context object.
525  * @pring: Pointer to driver SLI ring object.
526  * @iocb: Pointer to iocb slot in the ring.
527  * @nextiocb: Pointer to driver iocb object which need to be
528  *            posted to firmware.
529  *
530  * This function is called with hbalock held to post a new iocb to
531  * the firmware. This function copies the new iocb to ring iocb slot and
532  * updates the ring pointers. It adds the new iocb to txcmplq if there is
533  * a completion call back for this iocb else the function will free the
534  * iocb object.
535  **/
536 static void
537 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
538                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
539 {
540         /*
541          * Set up an iotag
542          */
543         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
544
545
546         if (pring->ringno == LPFC_ELS_RING) {
547                 lpfc_debugfs_slow_ring_trc(phba,
548                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
549                         *(((uint32_t *) &nextiocb->iocb) + 4),
550                         *(((uint32_t *) &nextiocb->iocb) + 6),
551                         *(((uint32_t *) &nextiocb->iocb) + 7));
552         }
553
554         /*
555          * Issue iocb command to adapter
556          */
557         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
558         wmb();
559         pring->stats.iocb_cmd++;
560
561         /*
562          * If there is no completion routine to call, we can release the
563          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
564          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
565          */
566         if (nextiocb->iocb_cmpl)
567                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
568         else
569                 __lpfc_sli_release_iocbq(phba, nextiocb);
570
571         /*
572          * Let the HBA know what IOCB slot will be the next one the
573          * driver will put a command into.
574          */
575         pring->cmdidx = pring->next_cmdidx;
576         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
577 }
578
579 /**
580  * lpfc_sli_update_full_ring: Update the chip attention register.
581  * @phba: Pointer to HBA context object.
582  * @pring: Pointer to driver SLI ring object.
583  *
584  * The caller is not required to hold any lock for calling this function.
585  * This function updates the chip attention bits for the ring to inform firmware
586  * that there are pending work to be done for this ring and requests an
587  * interrupt when there is space available in the ring. This function is
588  * called when the driver is unable to post more iocbs to the ring due
589  * to unavailability of space in the ring.
590  **/
591 static void
592 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
593 {
594         int ringno = pring->ringno;
595
596         pring->flag |= LPFC_CALL_RING_AVAILABLE;
597
598         wmb();
599
600         /*
601          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
602          * The HBA will tell us when an IOCB entry is available.
603          */
604         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
605         readl(phba->CAregaddr); /* flush */
606
607         pring->stats.iocb_cmd_full++;
608 }
609
610 /**
611  * lpfc_sli_update_ring: Update chip attention register.
612  * @phba: Pointer to HBA context object.
613  * @pring: Pointer to driver SLI ring object.
614  *
615  * This function updates the chip attention register bit for the
616  * given ring to inform HBA that there is more work to be done
617  * in this ring. The caller is not required to hold any lock.
618  **/
619 static void
620 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
621 {
622         int ringno = pring->ringno;
623
624         /*
625          * Tell the HBA that there is work to do in this ring.
626          */
627         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
628                 wmb();
629                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
630                 readl(phba->CAregaddr); /* flush */
631         }
632 }
633
634 /**
635  * lpfc_sli_resume_iocb: Process iocbs in the txq.
636  * @phba: Pointer to HBA context object.
637  * @pring: Pointer to driver SLI ring object.
638  *
639  * This function is called with hbalock held to post pending iocbs
640  * in the txq to the firmware. This function is called when driver
641  * detects space available in the ring.
642  **/
643 static void
644 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
645 {
646         IOCB_t *iocb;
647         struct lpfc_iocbq *nextiocb;
648
649         /*
650          * Check to see if:
651          *  (a) there is anything on the txq to send
652          *  (b) link is up
653          *  (c) link attention events can be processed (fcp ring only)
654          *  (d) IOCB processing is not blocked by the outstanding mbox command.
655          */
656         if (pring->txq_cnt &&
657             lpfc_is_link_up(phba) &&
658             (pring->ringno != phba->sli.fcp_ring ||
659              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
660
661                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
662                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
663                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
664
665                 if (iocb)
666                         lpfc_sli_update_ring(phba, pring);
667                 else
668                         lpfc_sli_update_full_ring(phba, pring);
669         }
670
671         return;
672 }
673
674 /**
675  * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
676  * @phba: Pointer to HBA context object.
677  * @hbqno: HBQ number.
678  *
679  * This function is called with hbalock held to get the next
680  * available slot for the given HBQ. If there is free slot
681  * available for the HBQ it will return pointer to the next available
682  * HBQ entry else it will return NULL.
683  **/
684 static struct lpfc_hbq_entry *
685 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
686 {
687         struct hbq_s *hbqp = &phba->hbqs[hbqno];
688
689         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
690             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
691                 hbqp->next_hbqPutIdx = 0;
692
693         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
694                 uint32_t raw_index = phba->hbq_get[hbqno];
695                 uint32_t getidx = le32_to_cpu(raw_index);
696
697                 hbqp->local_hbqGetIdx = getidx;
698
699                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
700                         lpfc_printf_log(phba, KERN_ERR,
701                                         LOG_SLI | LOG_VPORT,
702                                         "1802 HBQ %d: local_hbqGetIdx "
703                                         "%u is > than hbqp->entry_count %u\n",
704                                         hbqno, hbqp->local_hbqGetIdx,
705                                         hbqp->entry_count);
706
707                         phba->link_state = LPFC_HBA_ERROR;
708                         return NULL;
709                 }
710
711                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
712                         return NULL;
713         }
714
715         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
716                         hbqp->hbqPutIdx;
717 }
718
719 /**
720  * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
721  * @phba: Pointer to HBA context object.
722  *
723  * This function is called with no lock held to free all the
724  * hbq buffers while uninitializing the SLI interface. It also
725  * frees the HBQ buffers returned by the firmware but not yet
726  * processed by the upper layers.
727  **/
728 void
729 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
730 {
731         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
732         struct hbq_dmabuf *hbq_buf;
733         unsigned long flags;
734         int i, hbq_count;
735         uint32_t hbqno;
736
737         hbq_count = lpfc_sli_hbq_count();
738         /* Return all memory used by all HBQs */
739         spin_lock_irqsave(&phba->hbalock, flags);
740         for (i = 0; i < hbq_count; ++i) {
741                 list_for_each_entry_safe(dmabuf, next_dmabuf,
742                                 &phba->hbqs[i].hbq_buffer_list, list) {
743                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
744                         list_del(&hbq_buf->dbuf.list);
745                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
746                 }
747                 phba->hbqs[i].buffer_count = 0;
748         }
749         /* Return all HBQ buffer that are in-fly */
750         list_for_each_entry_safe(dmabuf, next_dmabuf,
751                         &phba->hbqbuf_in_list, list) {
752                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
753                 list_del(&hbq_buf->dbuf.list);
754                 if (hbq_buf->tag == -1) {
755                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
756                                 (phba, hbq_buf);
757                 } else {
758                         hbqno = hbq_buf->tag >> 16;
759                         if (hbqno >= LPFC_MAX_HBQS)
760                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
761                                         (phba, hbq_buf);
762                         else
763                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
764                                         hbq_buf);
765                 }
766         }
767
768         /* Mark the HBQs not in use */
769         phba->hbq_in_use = 0;
770         spin_unlock_irqrestore(&phba->hbalock, flags);
771 }
772
773 /**
774  * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
775  * @phba: Pointer to HBA context object.
776  * @hbqno: HBQ number.
777  * @hbq_buf: Pointer to HBQ buffer.
778  *
779  * This function is called with the hbalock held to post a
780  * hbq buffer to the firmware. If the function finds an empty
781  * slot in the HBQ, it will post the buffer. The function will return
782  * pointer to the hbq entry if it successfully post the buffer
783  * else it will return NULL.
784  **/
785 static struct lpfc_hbq_entry *
786 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
787                          struct hbq_dmabuf *hbq_buf)
788 {
789         struct lpfc_hbq_entry *hbqe;
790         dma_addr_t physaddr = hbq_buf->dbuf.phys;
791
792         /* Get next HBQ entry slot to use */
793         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
794         if (hbqe) {
795                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
796
797                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
798                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
799                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
800                 hbqe->bde.tus.f.bdeFlags = 0;
801                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
802                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
803                                 /* Sync SLIM */
804                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
805                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
806                                 /* flush */
807                 readl(phba->hbq_put + hbqno);
808                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
809         }
810         return hbqe;
811 }
812
813 /* HBQ for ELS and CT traffic. */
814 static struct lpfc_hbq_init lpfc_els_hbq = {
815         .rn = 1,
816         .entry_count = 200,
817         .mask_count = 0,
818         .profile = 0,
819         .ring_mask = (1 << LPFC_ELS_RING),
820         .buffer_count = 0,
821         .init_count = 20,
822         .add_count = 5,
823 };
824
825 /* HBQ for the extra ring if needed */
826 static struct lpfc_hbq_init lpfc_extra_hbq = {
827         .rn = 1,
828         .entry_count = 200,
829         .mask_count = 0,
830         .profile = 0,
831         .ring_mask = (1 << LPFC_EXTRA_RING),
832         .buffer_count = 0,
833         .init_count = 0,
834         .add_count = 5,
835 };
836
837 /* Array of HBQs */
838 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
839         &lpfc_els_hbq,
840         &lpfc_extra_hbq,
841 };
842
843 /**
844  * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
845  * @phba: Pointer to HBA context object.
846  * @hbqno: HBQ number.
847  * @count: Number of HBQ buffers to be posted.
848  *
849  * This function is called with no lock held to post more hbq buffers to the
850  * given HBQ. The function returns the number of HBQ buffers successfully
851  * posted.
852  **/
853 static int
854 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
855 {
856         uint32_t i, posted = 0;
857         unsigned long flags;
858         struct hbq_dmabuf *hbq_buffer;
859         LIST_HEAD(hbq_buf_list);
860         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
861                 return 0;
862
863         if ((phba->hbqs[hbqno].buffer_count + count) >
864             lpfc_hbq_defs[hbqno]->entry_count)
865                 count = lpfc_hbq_defs[hbqno]->entry_count -
866                                         phba->hbqs[hbqno].buffer_count;
867         if (!count)
868                 return 0;
869         /* Allocate HBQ entries */
870         for (i = 0; i < count; i++) {
871                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
872                 if (!hbq_buffer)
873                         break;
874                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
875         }
876         /* Check whether HBQ is still in use */
877         spin_lock_irqsave(&phba->hbalock, flags);
878         if (!phba->hbq_in_use)
879                 goto err;
880         while (!list_empty(&hbq_buf_list)) {
881                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
882                                  dbuf.list);
883                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
884                                       (hbqno << 16));
885                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
886                         phba->hbqs[hbqno].buffer_count++;
887                         posted++;
888                 } else
889                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
890         }
891         spin_unlock_irqrestore(&phba->hbalock, flags);
892         return posted;
893 err:
894         spin_unlock_irqrestore(&phba->hbalock, flags);
895         while (!list_empty(&hbq_buf_list)) {
896                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
897                                  dbuf.list);
898                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
899         }
900         return 0;
901 }
902
903 /**
904  * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
905  * @phba: Pointer to HBA context object.
906  * @qno: HBQ number.
907  *
908  * This function posts more buffers to the HBQ. This function
909  * is called with no lock held. The function returns the number of HBQ entries
910  * successfully allocated.
911  **/
912 int
913 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
914 {
915         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
916                                          lpfc_hbq_defs[qno]->add_count));
917 }
918
919 /**
920  * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
921  * @phba: Pointer to HBA context object.
922  * @qno:  HBQ queue number.
923  *
924  * This function is called from SLI initialization code path with
925  * no lock held to post initial HBQ buffers to firmware. The
926  * function returns the number of HBQ entries successfully allocated.
927  **/
928 static int
929 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
930 {
931         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
932                                          lpfc_hbq_defs[qno]->init_count));
933 }
934
935 /**
936  * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
937  * @phba: Pointer to HBA context object.
938  * @tag: Tag of the hbq buffer.
939  *
940  * This function is called with hbalock held. This function searches
941  * for the hbq buffer associated with the given tag in the hbq buffer
942  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
943  * it returns NULL.
944  **/
945 static struct hbq_dmabuf *
946 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
947 {
948         struct lpfc_dmabuf *d_buf;
949         struct hbq_dmabuf *hbq_buf;
950         uint32_t hbqno;
951
952         hbqno = tag >> 16;
953         if (hbqno >= LPFC_MAX_HBQS)
954                 return NULL;
955
956         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
957                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
958                 if (hbq_buf->tag == tag) {
959                         return hbq_buf;
960                 }
961         }
962         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
963                         "1803 Bad hbq tag. Data: x%x x%x\n",
964                         tag, phba->hbqs[tag >> 16].buffer_count);
965         return NULL;
966 }
967
968 /**
969  * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
970  * @phba: Pointer to HBA context object.
971  * @hbq_buffer: Pointer to HBQ buffer.
972  *
973  * This function is called with hbalock. This function gives back
974  * the hbq buffer to firmware. If the HBQ does not have space to
975  * post the buffer, it will free the buffer.
976  **/
977 void
978 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
979 {
980         uint32_t hbqno;
981
982         if (hbq_buffer) {
983                 hbqno = hbq_buffer->tag >> 16;
984                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
985                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
986                 }
987         }
988 }
989
990 /**
991  * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
992  * @mbxCommand: mailbox command code.
993  *
994  * This function is called by the mailbox event handler function to verify
995  * that the completed mailbox command is a legitimate mailbox command. If the
996  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
997  * and the mailbox event handler will take the HBA offline.
998  **/
999 static int
1000 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1001 {
1002         uint8_t ret;
1003
1004         switch (mbxCommand) {
1005         case MBX_LOAD_SM:
1006         case MBX_READ_NV:
1007         case MBX_WRITE_NV:
1008         case MBX_WRITE_VPARMS:
1009         case MBX_RUN_BIU_DIAG:
1010         case MBX_INIT_LINK:
1011         case MBX_DOWN_LINK:
1012         case MBX_CONFIG_LINK:
1013         case MBX_CONFIG_RING:
1014         case MBX_RESET_RING:
1015         case MBX_READ_CONFIG:
1016         case MBX_READ_RCONFIG:
1017         case MBX_READ_SPARM:
1018         case MBX_READ_STATUS:
1019         case MBX_READ_RPI:
1020         case MBX_READ_XRI:
1021         case MBX_READ_REV:
1022         case MBX_READ_LNK_STAT:
1023         case MBX_REG_LOGIN:
1024         case MBX_UNREG_LOGIN:
1025         case MBX_READ_LA:
1026         case MBX_CLEAR_LA:
1027         case MBX_DUMP_MEMORY:
1028         case MBX_DUMP_CONTEXT:
1029         case MBX_RUN_DIAGS:
1030         case MBX_RESTART:
1031         case MBX_UPDATE_CFG:
1032         case MBX_DOWN_LOAD:
1033         case MBX_DEL_LD_ENTRY:
1034         case MBX_RUN_PROGRAM:
1035         case MBX_SET_MASK:
1036         case MBX_SET_VARIABLE:
1037         case MBX_UNREG_D_ID:
1038         case MBX_KILL_BOARD:
1039         case MBX_CONFIG_FARP:
1040         case MBX_BEACON:
1041         case MBX_LOAD_AREA:
1042         case MBX_RUN_BIU_DIAG64:
1043         case MBX_CONFIG_PORT:
1044         case MBX_READ_SPARM64:
1045         case MBX_READ_RPI64:
1046         case MBX_REG_LOGIN64:
1047         case MBX_READ_LA64:
1048         case MBX_WRITE_WWN:
1049         case MBX_SET_DEBUG:
1050         case MBX_LOAD_EXP_ROM:
1051         case MBX_ASYNCEVT_ENABLE:
1052         case MBX_REG_VPI:
1053         case MBX_UNREG_VPI:
1054         case MBX_HEARTBEAT:
1055         case MBX_PORT_CAPABILITIES:
1056         case MBX_PORT_IOV_CONTROL:
1057                 ret = mbxCommand;
1058                 break;
1059         default:
1060                 ret = MBX_SHUTDOWN;
1061                 break;
1062         }
1063         return ret;
1064 }
1065
1066 /**
1067  * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1068  *          lpfc_sli_issue_mbox_wait.
1069  * @phba: Pointer to HBA context object.
1070  * @pmboxq: Pointer to mailbox command.
1071  *
1072  * This is completion handler function for mailbox commands issued from
1073  * lpfc_sli_issue_mbox_wait function. This function is called by the
1074  * mailbox event handler function with no lock held. This function
1075  * will wake up thread waiting on the wait queue pointed by context1
1076  * of the mailbox.
1077  **/
1078 static void
1079 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1080 {
1081         wait_queue_head_t *pdone_q;
1082         unsigned long drvr_flag;
1083
1084         /*
1085          * If pdone_q is empty, the driver thread gave up waiting and
1086          * continued running.
1087          */
1088         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1089         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1090         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1091         if (pdone_q)
1092                 wake_up_interruptible(pdone_q);
1093         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1094         return;
1095 }
1096
1097
1098 /**
1099  * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1100  * @phba: Pointer to HBA context object.
1101  * @pmb: Pointer to mailbox object.
1102  *
1103  * This function is the default mailbox completion handler. It
1104  * frees the memory resources associated with the completed mailbox
1105  * command. If the completed command is a REG_LOGIN mailbox command,
1106  * this function will issue a UREG_LOGIN to re-claim the RPI.
1107  **/
1108 void
1109 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1110 {
1111         struct lpfc_dmabuf *mp;
1112         uint16_t rpi;
1113         int rc;
1114
1115         mp = (struct lpfc_dmabuf *) (pmb->context1);
1116
1117         if (mp) {
1118                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1119                 kfree(mp);
1120         }
1121
1122         /*
1123          * If a REG_LOGIN succeeded  after node is destroyed or node
1124          * is in re-discovery driver need to cleanup the RPI.
1125          */
1126         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1127             pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1128             !pmb->mb.mbxStatus) {
1129
1130                 rpi = pmb->mb.un.varWords[0];
1131                 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1132                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1133                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1134                 if (rc != MBX_NOT_FINISHED)
1135                         return;
1136         }
1137
1138         mempool_free(pmb, phba->mbox_mem_pool);
1139         return;
1140 }
1141
1142 /**
1143  * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1144  * @phba: Pointer to HBA context object.
1145  *
1146  * This function is called with no lock held. This function processes all
1147  * the completed mailbox commands and gives it to upper layers. The interrupt
1148  * service routine processes mailbox completion interrupt and adds completed
1149  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1150  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1151  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1152  * function returns the mailbox commands to the upper layer by calling the
1153  * completion handler function of each mailbox.
1154  **/
1155 int
1156 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1157 {
1158         MAILBOX_t *pmbox;
1159         LPFC_MBOXQ_t *pmb;
1160         int rc;
1161         LIST_HEAD(cmplq);
1162
1163         phba->sli.slistat.mbox_event++;
1164
1165         /* Get all completed mailboxe buffers into the cmplq */
1166         spin_lock_irq(&phba->hbalock);
1167         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1168         spin_unlock_irq(&phba->hbalock);
1169
1170         /* Get a Mailbox buffer to setup mailbox commands for callback */
1171         do {
1172                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1173                 if (pmb == NULL)
1174                         break;
1175
1176                 pmbox = &pmb->mb;
1177
1178                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1179                         if (pmb->vport) {
1180                                 lpfc_debugfs_disc_trc(pmb->vport,
1181                                         LPFC_DISC_TRC_MBOX_VPORT,
1182                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1183                                         (uint32_t)pmbox->mbxCommand,
1184                                         pmbox->un.varWords[0],
1185                                         pmbox->un.varWords[1]);
1186                         }
1187                         else {
1188                                 lpfc_debugfs_disc_trc(phba->pport,
1189                                         LPFC_DISC_TRC_MBOX,
1190                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1191                                         (uint32_t)pmbox->mbxCommand,
1192                                         pmbox->un.varWords[0],
1193                                         pmbox->un.varWords[1]);
1194                         }
1195                 }
1196
1197                 /*
1198                  * It is a fatal error if unknown mbox command completion.
1199                  */
1200                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1201                     MBX_SHUTDOWN) {
1202                         /* Unknow mailbox command compl */
1203                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1204                                         "(%d):0323 Unknown Mailbox command "
1205                                         "%x Cmpl\n",
1206                                         pmb->vport ? pmb->vport->vpi : 0,
1207                                         pmbox->mbxCommand);
1208                         phba->link_state = LPFC_HBA_ERROR;
1209                         phba->work_hs = HS_FFER3;
1210                         lpfc_handle_eratt(phba);
1211                         continue;
1212                 }
1213
1214                 if (pmbox->mbxStatus) {
1215                         phba->sli.slistat.mbox_stat_err++;
1216                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1217                                 /* Mbox cmd cmpl error - RETRYing */
1218                                 lpfc_printf_log(phba, KERN_INFO,
1219                                                 LOG_MBOX | LOG_SLI,
1220                                                 "(%d):0305 Mbox cmd cmpl "
1221                                                 "error - RETRYing Data: x%x "
1222                                                 "x%x x%x x%x\n",
1223                                                 pmb->vport ? pmb->vport->vpi :0,
1224                                                 pmbox->mbxCommand,
1225                                                 pmbox->mbxStatus,
1226                                                 pmbox->un.varWords[0],
1227                                                 pmb->vport->port_state);
1228                                 pmbox->mbxStatus = 0;
1229                                 pmbox->mbxOwner = OWN_HOST;
1230                                 spin_lock_irq(&phba->hbalock);
1231                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1232                                 spin_unlock_irq(&phba->hbalock);
1233                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1234                                 if (rc == MBX_SUCCESS)
1235                                         continue;
1236                         }
1237                 }
1238
1239                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1240                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1241                                 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
1242                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1243                                 pmb->vport ? pmb->vport->vpi : 0,
1244                                 pmbox->mbxCommand,
1245                                 pmb->mbox_cmpl,
1246                                 *((uint32_t *) pmbox),
1247                                 pmbox->un.varWords[0],
1248                                 pmbox->un.varWords[1],
1249                                 pmbox->un.varWords[2],
1250                                 pmbox->un.varWords[3],
1251                                 pmbox->un.varWords[4],
1252                                 pmbox->un.varWords[5],
1253                                 pmbox->un.varWords[6],
1254                                 pmbox->un.varWords[7]);
1255
1256                 if (pmb->mbox_cmpl)
1257                         pmb->mbox_cmpl(phba,pmb);
1258         } while (1);
1259         return 0;
1260 }
1261
1262 /**
1263  * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1264  * @phba: Pointer to HBA context object.
1265  * @pring: Pointer to driver SLI ring object.
1266  * @tag: buffer tag.
1267  *
1268  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1269  * is set in the tag the buffer is posted for a particular exchange,
1270  * the function will return the buffer without replacing the buffer.
1271  * If the buffer is for unsolicited ELS or CT traffic, this function
1272  * returns the buffer and also posts another buffer to the firmware.
1273  **/
1274 static struct lpfc_dmabuf *
1275 lpfc_sli_get_buff(struct lpfc_hba *phba,
1276                   struct lpfc_sli_ring *pring,
1277                   uint32_t tag)
1278 {
1279         struct hbq_dmabuf *hbq_entry;
1280
1281         if (tag & QUE_BUFTAG_BIT)
1282                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1283         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1284         if (!hbq_entry)
1285                 return NULL;
1286         return &hbq_entry->dbuf;
1287 }
1288
1289
1290 /**
1291  * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1292  * @phba: Pointer to HBA context object.
1293  * @pring: Pointer to driver SLI ring object.
1294  * @saveq: Pointer to the unsolicited iocb.
1295  *
1296  * This function is called with no lock held by the ring event handler
1297  * when there is an unsolicited iocb posted to the response ring by the
1298  * firmware. This function gets the buffer associated with the iocbs
1299  * and calls the event handler for the ring. This function handles both
1300  * qring buffers and hbq buffers.
1301  * When the function returns 1 the caller can free the iocb object otherwise
1302  * upper layer functions will free the iocb objects.
1303  **/
1304 static int
1305 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1306                             struct lpfc_iocbq *saveq)
1307 {
1308         IOCB_t           * irsp;
1309         WORD5            * w5p;
1310         uint32_t           Rctl, Type;
1311         uint32_t           match, i;
1312         struct lpfc_iocbq *iocbq;
1313         struct lpfc_dmabuf *dmzbuf;
1314
1315         match = 0;
1316         irsp = &(saveq->iocb);
1317
1318         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1319                 if (pring->lpfc_sli_rcv_async_status)
1320                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1321                 else
1322                         lpfc_printf_log(phba,
1323                                         KERN_WARNING,
1324                                         LOG_SLI,
1325                                         "0316 Ring %d handler: unexpected "
1326                                         "ASYNC_STATUS iocb received evt_code "
1327                                         "0x%x\n",
1328                                         pring->ringno,
1329                                         irsp->un.asyncstat.evt_code);
1330                 return 1;
1331         }
1332
1333         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1334                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1335                 if (irsp->ulpBdeCount > 0) {
1336                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1337                                         irsp->un.ulpWord[3]);
1338                         lpfc_in_buf_free(phba, dmzbuf);
1339                 }
1340
1341                 if (irsp->ulpBdeCount > 1) {
1342                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1343                                         irsp->unsli3.sli3Words[3]);
1344                         lpfc_in_buf_free(phba, dmzbuf);
1345                 }
1346
1347                 if (irsp->ulpBdeCount > 2) {
1348                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1349                                 irsp->unsli3.sli3Words[7]);
1350                         lpfc_in_buf_free(phba, dmzbuf);
1351                 }
1352
1353                 return 1;
1354         }
1355
1356         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1357                 if (irsp->ulpBdeCount != 0) {
1358                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
1359                                                 irsp->un.ulpWord[3]);
1360                         if (!saveq->context2)
1361                                 lpfc_printf_log(phba,
1362                                         KERN_ERR,
1363                                         LOG_SLI,
1364                                         "0341 Ring %d Cannot find buffer for "
1365                                         "an unsolicited iocb. tag 0x%x\n",
1366                                         pring->ringno,
1367                                         irsp->un.ulpWord[3]);
1368                 }
1369                 if (irsp->ulpBdeCount == 2) {
1370                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
1371                                                 irsp->unsli3.sli3Words[7]);
1372                         if (!saveq->context3)
1373                                 lpfc_printf_log(phba,
1374                                         KERN_ERR,
1375                                         LOG_SLI,
1376                                         "0342 Ring %d Cannot find buffer for an"
1377                                         " unsolicited iocb. tag 0x%x\n",
1378                                         pring->ringno,
1379                                         irsp->unsli3.sli3Words[7]);
1380                 }
1381                 list_for_each_entry(iocbq, &saveq->list, list) {
1382                         irsp = &(iocbq->iocb);
1383                         if (irsp->ulpBdeCount != 0) {
1384                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1385                                                         irsp->un.ulpWord[3]);
1386                                 if (!iocbq->context2)
1387                                         lpfc_printf_log(phba,
1388                                                 KERN_ERR,
1389                                                 LOG_SLI,
1390                                                 "0343 Ring %d Cannot find "
1391                                                 "buffer for an unsolicited iocb"
1392                                                 ". tag 0x%x\n", pring->ringno,
1393                                                 irsp->un.ulpWord[3]);
1394                         }
1395                         if (irsp->ulpBdeCount == 2) {
1396                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1397                                                 irsp->unsli3.sli3Words[7]);
1398                                 if (!iocbq->context3)
1399                                         lpfc_printf_log(phba,
1400                                                 KERN_ERR,
1401                                                 LOG_SLI,
1402                                                 "0344 Ring %d Cannot find "
1403                                                 "buffer for an unsolicited "
1404                                                 "iocb. tag 0x%x\n",
1405                                                 pring->ringno,
1406                                                 irsp->unsli3.sli3Words[7]);
1407                         }
1408                 }
1409         }
1410         if (irsp->ulpBdeCount != 0 &&
1411             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1412              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1413                 int found = 0;
1414
1415                 /* search continue save q for same XRI */
1416                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1417                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1418                                 list_add_tail(&saveq->list, &iocbq->list);
1419                                 found = 1;
1420                                 break;
1421                         }
1422                 }
1423                 if (!found)
1424                         list_add_tail(&saveq->clist,
1425                                       &pring->iocb_continue_saveq);
1426                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1427                         list_del_init(&iocbq->clist);
1428                         saveq = iocbq;
1429                         irsp = &(saveq->iocb);
1430                 } else
1431                         return 0;
1432         }
1433         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1434             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1435             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1436                 Rctl = FC_ELS_REQ;
1437                 Type = FC_ELS_DATA;
1438         } else {
1439                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1440                 Rctl = w5p->hcsw.Rctl;
1441                 Type = w5p->hcsw.Type;
1442
1443                 /* Firmware Workaround */
1444                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1445                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1446                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1447                         Rctl = FC_ELS_REQ;
1448                         Type = FC_ELS_DATA;
1449                         w5p->hcsw.Rctl = Rctl;
1450                         w5p->hcsw.Type = Type;
1451                 }
1452         }
1453
1454         /* unSolicited Responses */
1455         if (pring->prt[0].profile) {
1456                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1457                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1458                                                                         saveq);
1459                 match = 1;
1460         } else {
1461                 /* We must search, based on rctl / type
1462                    for the right routine */
1463                 for (i = 0; i < pring->num_mask; i++) {
1464                         if ((pring->prt[i].rctl == Rctl)
1465                             && (pring->prt[i].type == Type)) {
1466                                 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1467                                         (pring->prt[i].lpfc_sli_rcv_unsol_event)
1468                                                         (phba, pring, saveq);
1469                                 match = 1;
1470                                 break;
1471                         }
1472                 }
1473         }
1474         if (match == 0) {
1475                 /* Unexpected Rctl / Type received */
1476                 /* Ring <ringno> handler: unexpected
1477                    Rctl <Rctl> Type <Type> received */
1478                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1479                                 "0313 Ring %d handler: unexpected Rctl x%x "
1480                                 "Type x%x received\n",
1481                                 pring->ringno, Rctl, Type);
1482         }
1483         return 1;
1484 }
1485
1486 /**
1487  * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1488  * @phba: Pointer to HBA context object.
1489  * @pring: Pointer to driver SLI ring object.
1490  * @prspiocb: Pointer to response iocb object.
1491  *
1492  * This function looks up the iocb_lookup table to get the command iocb
1493  * corresponding to the given response iocb using the iotag of the
1494  * response iocb. This function is called with the hbalock held.
1495  * This function returns the command iocb object if it finds the command
1496  * iocb else returns NULL.
1497  **/
1498 static struct lpfc_iocbq *
1499 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1500                       struct lpfc_sli_ring *pring,
1501                       struct lpfc_iocbq *prspiocb)
1502 {
1503         struct lpfc_iocbq *cmd_iocb = NULL;
1504         uint16_t iotag;
1505
1506         iotag = prspiocb->iocb.ulpIoTag;
1507
1508         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1509                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
1510                 list_del_init(&cmd_iocb->list);
1511                 pring->txcmplq_cnt--;
1512                 return cmd_iocb;
1513         }
1514
1515         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1516                         "0317 iotag x%x is out off "
1517                         "range: max iotag x%x wd0 x%x\n",
1518                         iotag, phba->sli.last_iotag,
1519                         *(((uint32_t *) &prspiocb->iocb) + 7));
1520         return NULL;
1521 }
1522
1523 /**
1524  * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1525  * @phba: Pointer to HBA context object.
1526  * @pring: Pointer to driver SLI ring object.
1527  * @saveq: Pointer to the response iocb to be processed.
1528  *
1529  * This function is called by the ring event handler for non-fcp
1530  * rings when there is a new response iocb in the response ring.
1531  * The caller is not required to hold any locks. This function
1532  * gets the command iocb associated with the response iocb and
1533  * calls the completion handler for the command iocb. If there
1534  * is no completion handler, the function will free the resources
1535  * associated with command iocb. If the response iocb is for
1536  * an already aborted command iocb, the status of the completion
1537  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1538  * This function always returns 1.
1539  **/
1540 static int
1541 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1542                           struct lpfc_iocbq *saveq)
1543 {
1544         struct lpfc_iocbq *cmdiocbp;
1545         int rc = 1;
1546         unsigned long iflag;
1547
1548         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
1549         spin_lock_irqsave(&phba->hbalock, iflag);
1550         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
1551         spin_unlock_irqrestore(&phba->hbalock, iflag);
1552
1553         if (cmdiocbp) {
1554                 if (cmdiocbp->iocb_cmpl) {
1555                         /*
1556                          * If an ELS command failed send an event to mgmt
1557                          * application.
1558                          */
1559                         if (saveq->iocb.ulpStatus &&
1560                              (pring->ringno == LPFC_ELS_RING) &&
1561                              (cmdiocbp->iocb.ulpCommand ==
1562                                 CMD_ELS_REQUEST64_CR))
1563                                 lpfc_send_els_failure_event(phba,
1564                                         cmdiocbp, saveq);
1565
1566                         /*
1567                          * Post all ELS completions to the worker thread.
1568                          * All other are passed to the completion callback.
1569                          */
1570                         if (pring->ringno == LPFC_ELS_RING) {
1571                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1572                                         cmdiocbp->iocb_flag &=
1573                                                 ~LPFC_DRIVER_ABORTED;
1574                                         saveq->iocb.ulpStatus =
1575                                                 IOSTAT_LOCAL_REJECT;
1576                                         saveq->iocb.un.ulpWord[4] =
1577                                                 IOERR_SLI_ABORTED;
1578
1579                                         /* Firmware could still be in progress
1580                                          * of DMAing payload, so don't free data
1581                                          * buffer till after a hbeat.
1582                                          */
1583                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
1584                                 }
1585                         }
1586                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
1587                 } else
1588                         lpfc_sli_release_iocbq(phba, cmdiocbp);
1589         } else {
1590                 /*
1591                  * Unknown initiating command based on the response iotag.
1592                  * This could be the case on the ELS ring because of
1593                  * lpfc_els_abort().
1594                  */
1595                 if (pring->ringno != LPFC_ELS_RING) {
1596                         /*
1597                          * Ring <ringno> handler: unexpected completion IoTag
1598                          * <IoTag>
1599                          */
1600                         lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1601                                          "0322 Ring %d handler: "
1602                                          "unexpected completion IoTag x%x "
1603                                          "Data: x%x x%x x%x x%x\n",
1604                                          pring->ringno,
1605                                          saveq->iocb.ulpIoTag,
1606                                          saveq->iocb.ulpStatus,
1607                                          saveq->iocb.un.ulpWord[4],
1608                                          saveq->iocb.ulpCommand,
1609                                          saveq->iocb.ulpContext);
1610                 }
1611         }
1612
1613         return rc;
1614 }
1615
1616 /**
1617  * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1618  * @phba: Pointer to HBA context object.
1619  * @pring: Pointer to driver SLI ring object.
1620  *
1621  * This function is called from the iocb ring event handlers when
1622  * put pointer is ahead of the get pointer for a ring. This function signal
1623  * an error attention condition to the worker thread and the worker
1624  * thread will transition the HBA to offline state.
1625  **/
1626 static void
1627 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1628 {
1629         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1630         /*
1631          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
1632          * rsp ring <portRspMax>
1633          */
1634         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1635                         "0312 Ring %d handler: portRspPut %d "
1636                         "is bigger than rsp ring %d\n",
1637                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
1638                         pring->numRiocb);
1639
1640         phba->link_state = LPFC_HBA_ERROR;
1641
1642         /*
1643          * All error attention handlers are posted to
1644          * worker thread
1645          */
1646         phba->work_ha |= HA_ERATT;
1647         phba->work_hs = HS_FFER3;
1648
1649         lpfc_worker_wake_up(phba);
1650
1651         return;
1652 }
1653
1654 /**
1655  * lpfc_poll_eratt: Error attention polling timer timeout handler.
1656  * @ptr: Pointer to address of HBA context object.
1657  *
1658  * This function is invoked by the Error Attention polling timer when the
1659  * timer times out. It will check the SLI Error Attention register for
1660  * possible attention events. If so, it will post an Error Attention event
1661  * and wake up worker thread to process it. Otherwise, it will set up the
1662  * Error Attention polling timer for the next poll.
1663  **/
1664 void lpfc_poll_eratt(unsigned long ptr)
1665 {
1666         struct lpfc_hba *phba;
1667         uint32_t eratt = 0;
1668
1669         phba = (struct lpfc_hba *)ptr;
1670
1671         /* Check chip HA register for error event */
1672         eratt = lpfc_sli_check_eratt(phba);
1673
1674         if (eratt)
1675                 /* Tell the worker thread there is work to do */
1676                 lpfc_worker_wake_up(phba);
1677         else
1678                 /* Restart the timer for next eratt poll */
1679                 mod_timer(&phba->eratt_poll, jiffies +
1680                                         HZ * LPFC_ERATT_POLL_INTERVAL);
1681         return;
1682 }
1683
1684 /**
1685  * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1686  * @phba: Pointer to HBA context object.
1687  *
1688  * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1689  * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1690  * is enabled.
1691  *
1692  * The caller does not hold any lock.
1693  * The function processes each response iocb in the response ring until it
1694  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1695  * LE bit set. The function will call the completion handler of the command iocb
1696  * if the response iocb indicates a completion for a command iocb or it is
1697  * an abort completion.
1698  **/
1699 void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
1700 {
1701         struct lpfc_sli      *psli  = &phba->sli;
1702         struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1703         IOCB_t *irsp = NULL;
1704         IOCB_t *entry = NULL;
1705         struct lpfc_iocbq *cmdiocbq = NULL;
1706         struct lpfc_iocbq rspiocbq;
1707         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1708         uint32_t status;
1709         uint32_t portRspPut, portRspMax;
1710         int type;
1711         uint32_t rsp_cmpl = 0;
1712         uint32_t ha_copy;
1713         unsigned long iflags;
1714
1715         pring->stats.iocb_event++;
1716
1717         /*
1718          * The next available response entry should never exceed the maximum
1719          * entries.  If it does, treat it as an adapter hardware error.
1720          */
1721         portRspMax = pring->numRiocb;
1722         portRspPut = le32_to_cpu(pgp->rspPutInx);
1723         if (unlikely(portRspPut >= portRspMax)) {
1724                 lpfc_sli_rsp_pointers_error(phba, pring);
1725                 return;
1726         }
1727
1728         rmb();
1729         while (pring->rspidx != portRspPut) {
1730                 entry = lpfc_resp_iocb(phba, pring);
1731                 if (++pring->rspidx >= portRspMax)
1732                         pring->rspidx = 0;
1733
1734                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1735                                       (uint32_t *) &rspiocbq.iocb,
1736                                       phba->iocb_rsp_size);
1737                 irsp = &rspiocbq.iocb;
1738                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1739                 pring->stats.iocb_rsp++;
1740                 rsp_cmpl++;
1741
1742                 if (unlikely(irsp->ulpStatus)) {
1743                         /* Rsp ring <ringno> error: IOCB */
1744                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1745                                         "0326 Rsp Ring %d error: IOCB Data: "
1746                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1747                                         pring->ringno,
1748                                         irsp->un.ulpWord[0],
1749                                         irsp->un.ulpWord[1],
1750                                         irsp->un.ulpWord[2],
1751                                         irsp->un.ulpWord[3],
1752                                         irsp->un.ulpWord[4],
1753                                         irsp->un.ulpWord[5],
1754                                         *(uint32_t *)&irsp->un1,
1755                                         *((uint32_t *)&irsp->un1 + 1));
1756                 }
1757
1758                 switch (type) {
1759                 case LPFC_ABORT_IOCB:
1760                 case LPFC_SOL_IOCB:
1761                         /*
1762                          * Idle exchange closed via ABTS from port.  No iocb
1763                          * resources need to be recovered.
1764                          */
1765                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1766                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1767                                                 "0314 IOCB cmd 0x%x "
1768                                                 "processed. Skipping "
1769                                                 "completion",
1770                                                 irsp->ulpCommand);
1771                                 break;
1772                         }
1773
1774                         spin_lock_irqsave(&phba->hbalock, iflags);
1775                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1776                                                          &rspiocbq);
1777                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1778                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1779                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1780                                                       &rspiocbq);
1781                         }
1782                         break;
1783                 default:
1784                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1785                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1786                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1787                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1788                                        MAX_MSG_DATA);
1789                                 dev_warn(&((phba->pcidev)->dev),
1790                                          "lpfc%d: %s\n",
1791                                          phba->brd_no, adaptermsg);
1792                         } else {
1793                                 /* Unknown IOCB command */
1794                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1795                                                 "0321 Unknown IOCB command "
1796                                                 "Data: x%x, x%x x%x x%x x%x\n",
1797                                                 type, irsp->ulpCommand,
1798                                                 irsp->ulpStatus,
1799                                                 irsp->ulpIoTag,
1800                                                 irsp->ulpContext);
1801                         }
1802                         break;
1803                 }
1804
1805                 /*
1806                  * The response IOCB has been processed.  Update the ring
1807                  * pointer in SLIM.  If the port response put pointer has not
1808                  * been updated, sync the pgp->rspPutInx and fetch the new port
1809                  * response put pointer.
1810                  */
1811                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1812
1813                 if (pring->rspidx == portRspPut)
1814                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1815         }
1816
1817         ha_copy = readl(phba->HAregaddr);
1818         ha_copy >>= (LPFC_FCP_RING * 4);
1819
1820         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1821                 spin_lock_irqsave(&phba->hbalock, iflags);
1822                 pring->stats.iocb_rsp_full++;
1823                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1824                 writel(status, phba->CAregaddr);
1825                 readl(phba->CAregaddr);
1826                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1827         }
1828         if ((ha_copy & HA_R0CE_RSP) &&
1829             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1830                 spin_lock_irqsave(&phba->hbalock, iflags);
1831                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1832                 pring->stats.iocb_cmd_empty++;
1833
1834                 /* Force update of the local copy of cmdGetInx */
1835                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1836                 lpfc_sli_resume_iocb(phba, pring);
1837
1838                 if ((pring->lpfc_sli_cmd_available))
1839                         (pring->lpfc_sli_cmd_available) (phba, pring);
1840
1841                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1842         }
1843
1844         return;
1845 }
1846
1847 /**
1848  * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1849  * @phba: Pointer to HBA context object.
1850  * @pring: Pointer to driver SLI ring object.
1851  * @mask: Host attention register mask for this ring.
1852  *
1853  * This function is called from the interrupt context when there is a ring
1854  * event for the fcp ring. The caller does not hold any lock.
1855  * The function processes each response iocb in the response ring until it
1856  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1857  * LE bit set. The function will call the completion handler of the command iocb
1858  * if the response iocb indicates a completion for a command iocb or it is
1859  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1860  * function if this is an unsolicited iocb.
1861  * This routine presumes LPFC_FCP_RING handling and doesn't bother
1862  * to check it explicitly. This function always returns 1.
1863  **/
1864 static int
1865 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1866                                 struct lpfc_sli_ring *pring, uint32_t mask)
1867 {
1868         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1869         IOCB_t *irsp = NULL;
1870         IOCB_t *entry = NULL;
1871         struct lpfc_iocbq *cmdiocbq = NULL;
1872         struct lpfc_iocbq rspiocbq;
1873         uint32_t status;
1874         uint32_t portRspPut, portRspMax;
1875         int rc = 1;
1876         lpfc_iocb_type type;
1877         unsigned long iflag;
1878         uint32_t rsp_cmpl = 0;
1879
1880         spin_lock_irqsave(&phba->hbalock, iflag);
1881         pring->stats.iocb_event++;
1882
1883         /*
1884          * The next available response entry should never exceed the maximum
1885          * entries.  If it does, treat it as an adapter hardware error.
1886          */
1887         portRspMax = pring->numRiocb;
1888         portRspPut = le32_to_cpu(pgp->rspPutInx);
1889         if (unlikely(portRspPut >= portRspMax)) {
1890                 lpfc_sli_rsp_pointers_error(phba, pring);
1891                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1892                 return 1;
1893         }
1894
1895         rmb();
1896         while (pring->rspidx != portRspPut) {
1897                 /*
1898                  * Fetch an entry off the ring and copy it into a local data
1899                  * structure.  The copy involves a byte-swap since the
1900                  * network byte order and pci byte orders are different.
1901                  */
1902                 entry = lpfc_resp_iocb(phba, pring);
1903                 phba->last_completion_time = jiffies;
1904
1905                 if (++pring->rspidx >= portRspMax)
1906                         pring->rspidx = 0;
1907
1908                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1909                                       (uint32_t *) &rspiocbq.iocb,
1910                                       phba->iocb_rsp_size);
1911                 INIT_LIST_HEAD(&(rspiocbq.list));
1912                 irsp = &rspiocbq.iocb;
1913
1914                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1915                 pring->stats.iocb_rsp++;
1916                 rsp_cmpl++;
1917
1918                 if (unlikely(irsp->ulpStatus)) {
1919                         /*
1920                          * If resource errors reported from HBA, reduce
1921                          * queuedepths of the SCSI device.
1922                          */
1923                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1924                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1925                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1926                                 lpfc_rampdown_queue_depth(phba);
1927                                 spin_lock_irqsave(&phba->hbalock, iflag);
1928                         }
1929
1930                         /* Rsp ring <ringno> error: IOCB */
1931                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1932                                         "0336 Rsp Ring %d error: IOCB Data: "
1933                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1934                                         pring->ringno,
1935                                         irsp->un.ulpWord[0],
1936                                         irsp->un.ulpWord[1],
1937                                         irsp->un.ulpWord[2],
1938                                         irsp->un.ulpWord[3],
1939                                         irsp->un.ulpWord[4],
1940                                         irsp->un.ulpWord[5],
1941                                         *(uint32_t *)&irsp->un1,
1942                                         *((uint32_t *)&irsp->un1 + 1));
1943                 }
1944
1945                 switch (type) {
1946                 case LPFC_ABORT_IOCB:
1947                 case LPFC_SOL_IOCB:
1948                         /*
1949                          * Idle exchange closed via ABTS from port.  No iocb
1950                          * resources need to be recovered.
1951                          */
1952                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1953                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1954                                                 "0333 IOCB cmd 0x%x"
1955                                                 " processed. Skipping"
1956                                                 " completion\n",
1957                                                 irsp->ulpCommand);
1958                                 break;
1959                         }
1960
1961                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1962                                                          &rspiocbq);
1963                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1964                                 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1965                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1966                                                               &rspiocbq);
1967                                 } else {
1968                                         spin_unlock_irqrestore(&phba->hbalock,
1969                                                                iflag);
1970                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1971                                                               &rspiocbq);
1972                                         spin_lock_irqsave(&phba->hbalock,
1973                                                           iflag);
1974                                 }
1975                         }
1976                         break;
1977                 case LPFC_UNSOL_IOCB:
1978                         spin_unlock_irqrestore(&phba->hbalock, iflag);
1979                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
1980                         spin_lock_irqsave(&phba->hbalock, iflag);
1981                         break;
1982                 default:
1983                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1984                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1985                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1986                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1987                                        MAX_MSG_DATA);
1988                                 dev_warn(&((phba->pcidev)->dev),
1989                                          "lpfc%d: %s\n",
1990                                          phba->brd_no, adaptermsg);
1991                         } else {
1992                                 /* Unknown IOCB command */
1993                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1994                                                 "0334 Unknown IOCB command "
1995                                                 "Data: x%x, x%x x%x x%x x%x\n",
1996                                                 type, irsp->ulpCommand,
1997                                                 irsp->ulpStatus,
1998                                                 irsp->ulpIoTag,
1999                                                 irsp->ulpContext);
2000                         }
2001                         break;
2002                 }
2003
2004                 /*
2005                  * The response IOCB has been processed.  Update the ring
2006                  * pointer in SLIM.  If the port response put pointer has not
2007                  * been updated, sync the pgp->rspPutInx and fetch the new port
2008                  * response put pointer.
2009                  */
2010                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2011
2012                 if (pring->rspidx == portRspPut)
2013                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2014         }
2015
2016         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2017                 pring->stats.iocb_rsp_full++;
2018                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2019                 writel(status, phba->CAregaddr);
2020                 readl(phba->CAregaddr);
2021         }
2022         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2023                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2024                 pring->stats.iocb_cmd_empty++;
2025
2026                 /* Force update of the local copy of cmdGetInx */
2027                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2028                 lpfc_sli_resume_iocb(phba, pring);
2029
2030                 if ((pring->lpfc_sli_cmd_available))
2031                         (pring->lpfc_sli_cmd_available) (phba, pring);
2032
2033         }
2034
2035         spin_unlock_irqrestore(&phba->hbalock, iflag);
2036         return rc;
2037 }
2038
2039 /**
2040  * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2041  * @phba: Pointer to HBA context object.
2042  * @pring: Pointer to driver SLI ring object.
2043  * @mask: Host attention register mask for this ring.
2044  *
2045  * This function is called from the worker thread when there is a ring
2046  * event for non-fcp rings. The caller does not hold any lock .
2047  * The function processes each response iocb in the response ring until it
2048  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2049  * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2050  * response iocb indicates a completion of a command iocb. The function
2051  * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2052  * iocb. The function frees the resources or calls the completion handler if
2053  * this iocb is an abort completion. The function returns 0 when the allocated
2054  * iocbs are not freed, otherwise returns 1.
2055  **/
2056 int
2057 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2058                                 struct lpfc_sli_ring *pring, uint32_t mask)
2059 {
2060         struct lpfc_pgp *pgp;
2061         IOCB_t *entry;
2062         IOCB_t *irsp = NULL;
2063         struct lpfc_iocbq *rspiocbp = NULL;
2064         struct lpfc_iocbq *next_iocb;
2065         struct lpfc_iocbq *cmdiocbp;
2066         struct lpfc_iocbq *saveq;
2067         uint8_t iocb_cmd_type;
2068         lpfc_iocb_type type;
2069         uint32_t status, free_saveq;
2070         uint32_t portRspPut, portRspMax;
2071         int rc = 1;
2072         unsigned long iflag;
2073
2074         pgp = &phba->port_gp[pring->ringno];
2075         spin_lock_irqsave(&phba->hbalock, iflag);
2076         pring->stats.iocb_event++;
2077
2078         /*
2079          * The next available response entry should never exceed the maximum
2080          * entries.  If it does, treat it as an adapter hardware error.
2081          */
2082         portRspMax = pring->numRiocb;
2083         portRspPut = le32_to_cpu(pgp->rspPutInx);
2084         if (portRspPut >= portRspMax) {
2085                 /*
2086                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2087                  * rsp ring <portRspMax>
2088                  */
2089                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2090                                 "0303 Ring %d handler: portRspPut %d "
2091                                 "is bigger than rsp ring %d\n",
2092                                 pring->ringno, portRspPut, portRspMax);
2093
2094                 phba->link_state = LPFC_HBA_ERROR;
2095                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2096
2097                 phba->work_hs = HS_FFER3;
2098                 lpfc_handle_eratt(phba);
2099
2100                 return 1;
2101         }
2102
2103         rmb();
2104         while (pring->rspidx != portRspPut) {
2105                 /*
2106                  * Build a completion list and call the appropriate handler.
2107                  * The process is to get the next available response iocb, get
2108                  * a free iocb from the list, copy the response data into the
2109                  * free iocb, insert to the continuation list, and update the
2110                  * next response index to slim.  This process makes response
2111                  * iocb's in the ring available to DMA as fast as possible but
2112                  * pays a penalty for a copy operation.  Since the iocb is
2113                  * only 32 bytes, this penalty is considered small relative to
2114                  * the PCI reads for register values and a slim write.  When
2115                  * the ulpLe field is set, the entire Command has been
2116                  * received.
2117                  */
2118                 entry = lpfc_resp_iocb(phba, pring);
2119
2120                 phba->last_completion_time = jiffies;
2121                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2122                 if (rspiocbp == NULL) {
2123                         printk(KERN_ERR "%s: out of buffers! Failing "
2124                                "completion.\n", __func__);
2125                         break;
2126                 }
2127
2128                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2129                                       phba->iocb_rsp_size);
2130                 irsp = &rspiocbp->iocb;
2131
2132                 if (++pring->rspidx >= portRspMax)
2133                         pring->rspidx = 0;
2134
2135                 if (pring->ringno == LPFC_ELS_RING) {
2136                         lpfc_debugfs_slow_ring_trc(phba,
2137                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2138                                 *(((uint32_t *) irsp) + 4),
2139                                 *(((uint32_t *) irsp) + 6),
2140                                 *(((uint32_t *) irsp) + 7));
2141                 }
2142
2143                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2144
2145                 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2146
2147                 pring->iocb_continueq_cnt++;
2148                 if (irsp->ulpLe) {
2149                         /*
2150                          * By default, the driver expects to free all resources
2151                          * associated with this iocb completion.
2152                          */
2153                         free_saveq = 1;
2154                         saveq = list_get_first(&pring->iocb_continueq,
2155                                                struct lpfc_iocbq, list);
2156                         irsp = &(saveq->iocb);
2157                         list_del_init(&pring->iocb_continueq);
2158                         pring->iocb_continueq_cnt = 0;
2159
2160                         pring->stats.iocb_rsp++;
2161
2162                         /*
2163                          * If resource errors reported from HBA, reduce
2164                          * queuedepths of the SCSI device.
2165                          */
2166                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2167                              (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2168                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2169                                 lpfc_rampdown_queue_depth(phba);
2170                                 spin_lock_irqsave(&phba->hbalock, iflag);
2171                         }
2172
2173                         if (irsp->ulpStatus) {
2174                                 /* Rsp ring <ringno> error: IOCB */
2175                                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2176                                                 "0328 Rsp Ring %d error: "
2177                                                 "IOCB Data: "
2178                                                 "x%x x%x x%x x%x "
2179                                                 "x%x x%x x%x x%x "
2180                                                 "x%x x%x x%x x%x "
2181                                                 "x%x x%x x%x x%x\n",
2182                                                 pring->ringno,
2183                                                 irsp->un.ulpWord[0],
2184                                                 irsp->un.ulpWord[1],
2185                                                 irsp->un.ulpWord[2],
2186                                                 irsp->un.ulpWord[3],
2187                                                 irsp->un.ulpWord[4],
2188                                                 irsp->un.ulpWord[5],
2189                                                 *(((uint32_t *) irsp) + 6),
2190                                                 *(((uint32_t *) irsp) + 7),
2191                                                 *(((uint32_t *) irsp) + 8),
2192                                                 *(((uint32_t *) irsp) + 9),
2193                                                 *(((uint32_t *) irsp) + 10),
2194                                                 *(((uint32_t *) irsp) + 11),
2195                                                 *(((uint32_t *) irsp) + 12),
2196                                                 *(((uint32_t *) irsp) + 13),
2197                                                 *(((uint32_t *) irsp) + 14),
2198                                                 *(((uint32_t *) irsp) + 15));
2199                         }
2200
2201                         /*
2202                          * Fetch the IOCB command type and call the correct
2203                          * completion routine.  Solicited and Unsolicited
2204                          * IOCBs on the ELS ring get freed back to the
2205                          * lpfc_iocb_list by the discovery kernel thread.
2206                          */
2207                         iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2208                         type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2209                         if (type == LPFC_SOL_IOCB) {
2210                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2211                                 rc = lpfc_sli_process_sol_iocb(phba, pring,
2212                                                                saveq);
2213                                 spin_lock_irqsave(&phba->hbalock, iflag);
2214                         } else if (type == LPFC_UNSOL_IOCB) {
2215                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2216                                 rc = lpfc_sli_process_unsol_iocb(phba, pring,
2217                                                                  saveq);
2218                                 spin_lock_irqsave(&phba->hbalock, iflag);
2219                                 if (!rc)
2220                                         free_saveq = 0;
2221                         } else if (type == LPFC_ABORT_IOCB) {
2222                                 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2223                                     ((cmdiocbp =
2224                                       lpfc_sli_iocbq_lookup(phba, pring,
2225                                                             saveq)))) {
2226                                         /* Call the specified completion
2227                                            routine */
2228                                         if (cmdiocbp->iocb_cmpl) {
2229                                                 spin_unlock_irqrestore(
2230                                                        &phba->hbalock,
2231                                                        iflag);
2232                                                 (cmdiocbp->iocb_cmpl) (phba,
2233                                                              cmdiocbp, saveq);
2234                                                 spin_lock_irqsave(
2235                                                           &phba->hbalock,
2236                                                           iflag);
2237                                         } else
2238                                                 __lpfc_sli_release_iocbq(phba,
2239                                                                       cmdiocbp);
2240                                 }
2241                         } else if (type == LPFC_UNKNOWN_IOCB) {
2242                                 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2243
2244                                         char adaptermsg[LPFC_MAX_ADPTMSG];
2245
2246                                         memset(adaptermsg, 0,
2247                                                LPFC_MAX_ADPTMSG);
2248                                         memcpy(&adaptermsg[0], (uint8_t *) irsp,
2249                                                MAX_MSG_DATA);
2250                                         dev_warn(&((phba->pcidev)->dev),
2251                                                  "lpfc%d: %s\n",
2252                                                  phba->brd_no, adaptermsg);
2253                                 } else {
2254                                         /* Unknown IOCB command */
2255                                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2256                                                         "0335 Unknown IOCB "
2257                                                         "command Data: x%x "
2258                                                         "x%x x%x x%x\n",
2259                                                         irsp->ulpCommand,
2260                                                         irsp->ulpStatus,
2261                                                         irsp->ulpIoTag,
2262                                                         irsp->ulpContext);
2263                                 }
2264                         }
2265
2266                         if (free_saveq) {
2267                                 list_for_each_entry_safe(rspiocbp, next_iocb,
2268                                                          &saveq->list, list) {
2269                                         list_del(&rspiocbp->list);
2270                                         __lpfc_sli_release_iocbq(phba,
2271                                                                  rspiocbp);
2272                                 }
2273                                 __lpfc_sli_release_iocbq(phba, saveq);
2274                         }
2275                         rspiocbp = NULL;
2276                 }
2277
2278                 /*
2279                  * If the port response put pointer has not been updated, sync
2280                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2281                  * response put pointer.
2282                  */
2283                 if (pring->rspidx == portRspPut) {
2284                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2285                 }
2286         } /* while (pring->rspidx != portRspPut) */
2287
2288         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2289                 /* At least one response entry has been freed */
2290                 pring->stats.iocb_rsp_full++;
2291                 /* SET RxRE_RSP in Chip Att register */
2292                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2293                 writel(status, phba->CAregaddr);
2294                 readl(phba->CAregaddr); /* flush */
2295         }
2296         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2297                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2298                 pring->stats.iocb_cmd_empty++;
2299
2300                 /* Force update of the local copy of cmdGetInx */
2301                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2302                 lpfc_sli_resume_iocb(phba, pring);
2303
2304                 if ((pring->lpfc_sli_cmd_available))
2305                         (pring->lpfc_sli_cmd_available) (phba, pring);
2306
2307         }
2308
2309         spin_unlock_irqrestore(&phba->hbalock, iflag);
2310         return rc;
2311 }
2312
2313 /**
2314  * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2315  * @phba: Pointer to HBA context object.
2316  * @pring: Pointer to driver SLI ring object.
2317  *
2318  * This function aborts all iocbs in the given ring and frees all the iocb
2319  * objects in txq. This function issues an abort iocb for all the iocb commands
2320  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2321  * the return of this function. The caller is not required to hold any locks.
2322  **/
2323 void
2324 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2325 {
2326         LIST_HEAD(completions);
2327         struct lpfc_iocbq *iocb, *next_iocb;
2328         IOCB_t *cmd = NULL;
2329
2330         if (pring->ringno == LPFC_ELS_RING) {
2331                 lpfc_fabric_abort_hba(phba);
2332         }
2333
2334         /* Error everything on txq and txcmplq
2335          * First do the txq.
2336          */
2337         spin_lock_irq(&phba->hbalock);
2338         list_splice_init(&pring->txq, &completions);
2339         pring->txq_cnt = 0;
2340
2341         /* Next issue ABTS for everything on the txcmplq */
2342         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2343                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2344
2345         spin_unlock_irq(&phba->hbalock);
2346
2347         while (!list_empty(&completions)) {
2348                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
2349                 cmd = &iocb->iocb;
2350                 list_del_init(&iocb->list);
2351
2352                 if (!iocb->iocb_cmpl)
2353                         lpfc_sli_release_iocbq(phba, iocb);
2354                 else {
2355                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2356                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2357                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2358                 }
2359         }
2360 }
2361
2362 /**
2363  * lpfc_sli_flush_fcp_rings: flush all iocbs in the fcp ring.
2364  * @phba: Pointer to HBA context object.
2365  *
2366  * This function flushes all iocbs in the fcp ring and frees all the iocb
2367  * objects in txq and txcmplq. This function will not issue abort iocbs
2368  * for all the iocb commands in txcmplq, they will just be returned with
2369  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2370  * slot has been permanently disabled.
2371  **/
2372 void
2373 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
2374 {
2375         LIST_HEAD(txq);
2376         LIST_HEAD(txcmplq);
2377         struct lpfc_iocbq *iocb;
2378         IOCB_t *cmd = NULL;
2379         struct lpfc_sli *psli = &phba->sli;
2380         struct lpfc_sli_ring  *pring;
2381
2382         /* Currently, only one fcp ring */
2383         pring = &psli->ring[psli->fcp_ring];
2384
2385         spin_lock_irq(&phba->hbalock);
2386         /* Retrieve everything on txq */
2387         list_splice_init(&pring->txq, &txq);
2388         pring->txq_cnt = 0;
2389
2390         /* Retrieve everything on the txcmplq */
2391         list_splice_init(&pring->txcmplq, &txcmplq);
2392         pring->txcmplq_cnt = 0;
2393         spin_unlock_irq(&phba->hbalock);
2394
2395         /* Flush the txq */
2396         while (!list_empty(&txq)) {
2397                 iocb = list_get_first(&txq, struct lpfc_iocbq, list);
2398                 cmd = &iocb->iocb;
2399                 list_del_init(&iocb->list);
2400
2401                 if (!iocb->iocb_cmpl)
2402                         lpfc_sli_release_iocbq(phba, iocb);
2403                 else {
2404                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2405                         cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2406                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2407                 }
2408         }
2409
2410         /* Flush the txcmpq */
2411         while (!list_empty(&txcmplq)) {
2412                 iocb = list_get_first(&txcmplq, struct lpfc_iocbq, list);
2413                 cmd = &iocb->iocb;
2414                 list_del_init(&iocb->list);
2415
2416                 if (!iocb->iocb_cmpl)
2417                         lpfc_sli_release_iocbq(phba, iocb);
2418                 else {
2419                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2420                         cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
2421                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2422                 }
2423         }
2424 }
2425
2426 /**
2427  * lpfc_sli_brdready: Check for host status bits.
2428  * @phba: Pointer to HBA context object.
2429  * @mask: Bit mask to be checked.
2430  *
2431  * This function reads the host status register and compares
2432  * with the provided bit mask to check if HBA completed
2433  * the restart. This function will wait in a loop for the
2434  * HBA to complete restart. If the HBA does not restart within
2435  * 15 iterations, the function will reset the HBA again. The
2436  * function returns 1 when HBA fail to restart otherwise returns
2437  * zero.
2438  **/
2439 int
2440 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
2441 {
2442         uint32_t status;
2443         int i = 0;
2444         int retval = 0;
2445
2446         /* Read the HBA Host Status Register */
2447         status = readl(phba->HSregaddr);
2448
2449         /*
2450          * Check status register every 100ms for 5 retries, then every
2451          * 500ms for 5, then every 2.5 sec for 5, then reset board and
2452          * every 2.5 sec for 4.
2453          * Break our of the loop if errors occurred during init.
2454          */
2455         while (((status & mask) != mask) &&
2456                !(status & HS_FFERM) &&
2457                i++ < 20) {
2458
2459                 if (i <= 5)
2460                         msleep(10);
2461                 else if (i <= 10)
2462                         msleep(500);
2463                 else
2464                         msleep(2500);
2465
2466                 if (i == 15) {
2467                                 /* Do post */
2468                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2469                         lpfc_sli_brdrestart(phba);
2470                 }
2471                 /* Read the HBA Host Status Register */
2472                 status = readl(phba->HSregaddr);
2473         }
2474
2475         /* Check to see if any errors occurred during init */
2476         if ((status & HS_FFERM) || (i >= 20)) {
2477                 phba->link_state = LPFC_HBA_ERROR;
2478                 retval = 1;
2479         }
2480
2481         return retval;
2482 }
2483
2484 #define BARRIER_TEST_PATTERN (0xdeadbeef)
2485
2486 /**
2487  * lpfc_reset_barrier: Make HBA ready for HBA reset.
2488  * @phba: Pointer to HBA context object.
2489  *
2490  * This function is called before resetting an HBA. This
2491  * function requests HBA to quiesce DMAs before a reset.
2492  **/
2493 void lpfc_reset_barrier(struct lpfc_hba *phba)
2494 {
2495         uint32_t __iomem *resp_buf;
2496         uint32_t __iomem *mbox_buf;
2497         volatile uint32_t mbox;
2498         uint32_t hc_copy;
2499         int  i;
2500         uint8_t hdrtype;
2501
2502         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2503         if (hdrtype != 0x80 ||
2504             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2505              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2506                 return;
2507
2508         /*
2509          * Tell the other part of the chip to suspend temporarily all
2510          * its DMA activity.
2511          */
2512         resp_buf = phba->MBslimaddr;
2513
2514         /* Disable the error attention */
2515         hc_copy = readl(phba->HCregaddr);
2516         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2517         readl(phba->HCregaddr); /* flush */
2518         phba->link_flag |= LS_IGNORE_ERATT;
2519
2520         if (readl(phba->HAregaddr) & HA_ERATT) {
2521                 /* Clear Chip error bit */
2522                 writel(HA_ERATT, phba->HAregaddr);
2523                 phba->pport->stopped = 1;
2524         }
2525
2526         mbox = 0;
2527         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2528         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2529
2530         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
2531         mbox_buf = phba->MBslimaddr;
2532         writel(mbox, mbox_buf);
2533
2534         for (i = 0;
2535              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2536                 mdelay(1);
2537
2538         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2539                 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
2540                     phba->pport->stopped)
2541                         goto restore_hc;
2542                 else
2543                         goto clear_errat;
2544         }
2545
2546         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2547         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
2548                 mdelay(1);
2549
2550 clear_errat:
2551
2552         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2553                 mdelay(1);
2554
2555         if (readl(phba->HAregaddr) & HA_ERATT) {
2556                 writel(HA_ERATT, phba->HAregaddr);
2557                 phba->pport->stopped = 1;
2558         }
2559
2560 restore_hc:
2561         phba->link_flag &= ~LS_IGNORE_ERATT;
2562         writel(hc_copy, phba->HCregaddr);
2563         readl(phba->HCregaddr); /* flush */
2564 }
2565
2566 /**
2567  * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2568  * @phba: Pointer to HBA context object.
2569  *
2570  * This function issues a kill_board mailbox command and waits for
2571  * the error attention interrupt. This function is called for stopping
2572  * the firmware processing. The caller is not required to hold any
2573  * locks. This function calls lpfc_hba_down_post function to free
2574  * any pending commands after the kill. The function will return 1 when it
2575  * fails to kill the board else will return 0.
2576  **/
2577 int
2578 lpfc_sli_brdkill(struct lpfc_hba *phba)
2579 {
2580         struct lpfc_sli *psli;
2581         LPFC_MBOXQ_t *pmb;
2582         uint32_t status;
2583         uint32_t ha_copy;
2584         int retval;
2585         int i = 0;
2586
2587         psli = &phba->sli;
2588
2589         /* Kill HBA */
2590         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2591                         "0329 Kill HBA Data: x%x x%x\n",
2592                         phba->pport->port_state, psli->sli_flag);
2593
2594         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2595         if (!pmb)
2596                 return 1;
2597
2598         /* Disable the error attention */
2599         spin_lock_irq(&phba->hbalock);
2600         status = readl(phba->HCregaddr);
2601         status &= ~HC_ERINT_ENA;
2602         writel(status, phba->HCregaddr);
2603         readl(phba->HCregaddr); /* flush */
2604         phba->link_flag |= LS_IGNORE_ERATT;
2605         spin_unlock_irq(&phba->hbalock);
2606
2607         lpfc_kill_board(phba, pmb);
2608         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2609         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2610
2611         if (retval != MBX_SUCCESS) {
2612                 if (retval != MBX_BUSY)
2613                         mempool_free(pmb, phba->mbox_mem_pool);
2614                 spin_lock_irq(&phba->hbalock);
2615                 phba->link_flag &= ~LS_IGNORE_ERATT;
2616                 spin_unlock_irq(&phba->hbalock);
2617                 return 1;
2618         }
2619
2620         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2621
2622         mempool_free(pmb, phba->mbox_mem_pool);
2623
2624         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2625          * attention every 100ms for 3 seconds. If we don't get ERATT after
2626          * 3 seconds we still set HBA_ERROR state because the status of the
2627          * board is now undefined.
2628          */
2629         ha_copy = readl(phba->HAregaddr);
2630
2631         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2632                 mdelay(100);
2633                 ha_copy = readl(phba->HAregaddr);
2634         }
2635
2636         del_timer_sync(&psli->mbox_tmo);
2637         if (ha_copy & HA_ERATT) {
2638                 writel(HA_ERATT, phba->HAregaddr);
2639                 phba->pport->stopped = 1;
2640         }
2641         spin_lock_irq(&phba->hbalock);
2642         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2643         phba->link_flag &= ~LS_IGNORE_ERATT;
2644         spin_unlock_irq(&phba->hbalock);
2645
2646         psli->mbox_active = NULL;
2647         lpfc_hba_down_post(phba);
2648         phba->link_state = LPFC_HBA_ERROR;
2649
2650         return ha_copy & HA_ERATT ? 0 : 1;
2651 }
2652
2653 /**
2654  * lpfc_sli_brdreset: Reset the HBA.
2655  * @phba: Pointer to HBA context object.
2656  *
2657  * This function resets the HBA by writing HC_INITFF to the control
2658  * register. After the HBA resets, this function resets all the iocb ring
2659  * indices. This function disables PCI layer parity checking during
2660  * the reset.
2661  * This function returns 0 always.
2662  * The caller is not required to hold any locks.
2663  **/
2664 int
2665 lpfc_sli_brdreset(struct lpfc_hba *phba)
2666 {
2667         struct lpfc_sli *psli;
2668         struct lpfc_sli_ring *pring;
2669         uint16_t cfg_value;
2670         int i;
2671
2672         psli = &phba->sli;
2673
2674         /* Reset HBA */
2675         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2676                         "0325 Reset HBA Data: x%x x%x\n",
2677                         phba->pport->port_state, psli->sli_flag);
2678
2679         /* perform board reset */
2680         phba->fc_eventTag = 0;
2681         phba->pport->fc_myDID = 0;
2682         phba->pport->fc_prevDID = 0;
2683
2684         /* Turn off parity checking and serr during the physical reset */
2685         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2686         pci_write_config_word(phba->pcidev, PCI_COMMAND,
2687                               (cfg_value &
2688                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2689
2690         psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
2691         /* Now toggle INITFF bit in the Host Control Register */
2692         writel(HC_INITFF, phba->HCregaddr);
2693         mdelay(1);
2694         readl(phba->HCregaddr); /* flush */
2695         writel(0, phba->HCregaddr);
2696         readl(phba->HCregaddr); /* flush */
2697
2698         /* Restore PCI cmd register */
2699         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
2700
2701         /* Initialize relevant SLI info */
2702         for (i = 0; i < psli->num_rings; i++) {
2703                 pring = &psli->ring[i];
2704                 pring->flag = 0;
2705                 pring->rspidx = 0;
2706                 pring->next_cmdidx  = 0;
2707                 pring->local_getidx = 0;
2708                 pring->cmdidx = 0;
2709                 pring->missbufcnt = 0;
2710         }
2711
2712         phba->link_state = LPFC_WARM_START;
2713         return 0;
2714 }
2715
2716 /**
2717  * lpfc_sli_brdrestart: Restart the HBA.
2718  * @phba: Pointer to HBA context object.
2719  *
2720  * This function is called in the SLI initialization code path to
2721  * restart the HBA. The caller is not required to hold any lock.
2722  * This function writes MBX_RESTART mailbox command to the SLIM and
2723  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2724  * function to free any pending commands. The function enables
2725  * POST only during the first initialization. The function returns zero.
2726  * The function does not guarantee completion of MBX_RESTART mailbox
2727  * command before the return of this function.
2728  **/
2729 int
2730 lpfc_sli_brdrestart(struct lpfc_hba *phba)
2731 {
2732         MAILBOX_t *mb;
2733         struct lpfc_sli *psli;
2734         volatile uint32_t word0;
2735         void __iomem *to_slim;
2736
2737         spin_lock_irq(&phba->hbalock);
2738
2739         psli = &phba->sli;
2740
2741         /* Restart HBA */
2742         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2743                         "0337 Restart HBA Data: x%x x%x\n",
2744                         phba->pport->port_state, psli->sli_flag);
2745
2746         word0 = 0;
2747         mb = (MAILBOX_t *) &word0;
2748         mb->mbxCommand = MBX_RESTART;
2749         mb->mbxHc = 1;
2750
2751         lpfc_reset_barrier(phba);
2752
2753         to_slim = phba->MBslimaddr;
2754         writel(*(uint32_t *) mb, to_slim);
2755         readl(to_slim); /* flush */
2756
2757         /* Only skip post after fc_ffinit is completed */
2758         if (phba->pport->port_state)
2759                 word0 = 1;      /* This is really setting up word1 */
2760         else
2761                 word0 = 0;      /* This is really setting up word1 */
2762         to_slim = phba->MBslimaddr + sizeof (uint32_t);
2763         writel(*(uint32_t *) mb, to_slim);
2764         readl(to_slim); /* flush */
2765
2766         lpfc_sli_brdreset(phba);
2767         phba->pport->stopped = 0;
2768         phba->link_state = LPFC_INIT_START;
2769
2770         spin_unlock_irq(&phba->hbalock);
2771
2772         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2773         psli->stats_start = get_seconds();
2774
2775         /* Give the INITFF and Post time to settle. */
2776         mdelay(100);
2777
2778         lpfc_hba_down_post(phba);
2779
2780         return 0;
2781 }
2782
2783 /**
2784  * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2785  * @phba: Pointer to HBA context object.
2786  *
2787  * This function is called after a HBA restart to wait for successful
2788  * restart of the HBA. Successful restart of the HBA is indicated by
2789  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2790  * iteration, the function will restart the HBA again. The function returns
2791  * zero if HBA successfully restarted else returns negative error code.
2792  **/
2793 static int
2794 lpfc_sli_chipset_init(struct lpfc_hba *phba)
2795 {
2796         uint32_t status, i = 0;
2797
2798         /* Read the HBA Host Status Register */
2799         status = readl(phba->HSregaddr);
2800
2801         /* Check status register to see what current state is */
2802         i = 0;
2803         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2804
2805                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2806                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
2807                  * 4.
2808                  */
2809                 if (i++ >= 20) {
2810                         /* Adapter failed to init, timeout, status reg
2811                            <status> */
2812                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2813                                         "0436 Adapter failed to init, "
2814                                         "timeout, status reg x%x, "
2815                                         "FW Data: A8 x%x AC x%x\n", status,
2816                                         readl(phba->MBslimaddr + 0xa8),
2817                                         readl(phba->MBslimaddr + 0xac));
2818                         phba->link_state = LPFC_HBA_ERROR;
2819                         return -ETIMEDOUT;
2820                 }
2821
2822                 /* Check to see if any errors occurred during init */
2823                 if (status & HS_FFERM) {
2824                         /* ERROR: During chipset initialization */
2825                         /* Adapter failed to init, chipset, status reg
2826                            <status> */
2827                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2828                                         "0437 Adapter failed to init, "
2829                                         "chipset, status reg x%x, "
2830                                         "FW Data: A8 x%x AC x%x\n", status,
2831                                         readl(phba->MBslimaddr + 0xa8),
2832                                         readl(phba->MBslimaddr + 0xac));
2833                         phba->link_state = LPFC_HBA_ERROR;
2834                         return -EIO;
2835                 }
2836
2837                 if (i <= 5) {
2838                         msleep(10);
2839                 } else if (i <= 10) {
2840                         msleep(500);
2841                 } else {
2842                         msleep(2500);
2843                 }
2844
2845                 if (i == 15) {
2846                                 /* Do post */
2847                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2848                         lpfc_sli_brdrestart(phba);
2849                 }
2850                 /* Read the HBA Host Status Register */
2851                 status = readl(phba->HSregaddr);
2852         }
2853
2854         /* Check to see if any errors occurred during init */
2855         if (status & HS_FFERM) {
2856                 /* ERROR: During chipset initialization */
2857                 /* Adapter failed to init, chipset, status reg <status> */
2858                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2859                                 "0438 Adapter failed to init, chipset, "
2860                                 "status reg x%x, "
2861                                 "FW Data: A8 x%x AC x%x\n", status,
2862                                 readl(phba->MBslimaddr + 0xa8),
2863                                 readl(phba->MBslimaddr + 0xac));
2864                 phba->link_state = LPFC_HBA_ERROR;
2865                 return -EIO;
2866         }
2867
2868         /* Clear all interrupt enable conditions */
2869         writel(0, phba->HCregaddr);
2870         readl(phba->HCregaddr); /* flush */
2871
2872         /* setup host attn register */
2873         writel(0xffffffff, phba->HAregaddr);
2874         readl(phba->HAregaddr); /* flush */
2875         return 0;
2876 }
2877
2878 /**
2879  * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2880  *
2881  * This function calculates and returns the number of HBQs required to be
2882  * configured.
2883  **/
2884 int
2885 lpfc_sli_hbq_count(void)
2886 {
2887         return ARRAY_SIZE(lpfc_hbq_defs);
2888 }
2889
2890 /**
2891  * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2892  *
2893  * This function adds the number of hbq entries in every HBQ to get
2894  * the total number of hbq entries required for the HBA and returns
2895  * the total count.
2896  **/
2897 static int
2898 lpfc_sli_hbq_entry_count(void)
2899 {
2900         int  hbq_count = lpfc_sli_hbq_count();
2901         int  count = 0;
2902         int  i;
2903
2904         for (i = 0; i < hbq_count; ++i)
2905                 count += lpfc_hbq_defs[i]->entry_count;
2906         return count;
2907 }
2908
2909 /**
2910  * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2911  *
2912  * This function calculates amount of memory required for all hbq entries
2913  * to be configured and returns the total memory required.
2914  **/
2915 int
2916 lpfc_sli_hbq_size(void)
2917 {
2918         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2919 }
2920
2921 /**
2922  * lpfc_sli_hbq_setup: configure and initialize HBQs.
2923  * @phba: Pointer to HBA context object.
2924  *
2925  * This function is called during the SLI initialization to configure
2926  * all the HBQs and post buffers to the HBQ. The caller is not
2927  * required to hold any locks. This function will return zero if successful
2928  * else it will return negative error code.
2929  **/
2930 static int
2931 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2932 {
2933         int  hbq_count = lpfc_sli_hbq_count();
2934         LPFC_MBOXQ_t *pmb;
2935         MAILBOX_t *pmbox;
2936         uint32_t hbqno;
2937         uint32_t hbq_entry_index;
2938
2939                                 /* Get a Mailbox buffer to setup mailbox
2940                                  * commands for HBA initialization
2941                                  */
2942         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2943
2944         if (!pmb)
2945                 return -ENOMEM;
2946
2947         pmbox = &pmb->mb;
2948
2949         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2950         phba->link_state = LPFC_INIT_MBX_CMDS;
2951         phba->hbq_in_use = 1;
2952
2953         hbq_entry_index = 0;
2954         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2955                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2956                 phba->hbqs[hbqno].hbqPutIdx      = 0;
2957                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
2958                 phba->hbqs[hbqno].entry_count =
2959                         lpfc_hbq_defs[hbqno]->entry_count;
2960                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2961                         hbq_entry_index, pmb);
2962                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2963
2964                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2965                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2966                            mbxStatus <status>, ring <num> */
2967
2968                         lpfc_printf_log(phba, KERN_ERR,
2969                                         LOG_SLI | LOG_VPORT,
2970                                         "1805 Adapter failed to init. "
2971                                         "Data: x%x x%x x%x\n",
2972                                         pmbox->mbxCommand,
2973                                         pmbox->mbxStatus, hbqno);
2974
2975                         phba->link_state = LPFC_HBA_ERROR;
2976                         mempool_free(pmb, phba->mbox_mem_pool);
2977