]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/lpfc/lpfc_sli.c
[SCSI] lpfc 8.2.8 : Add new FCOE hardware support
[mv-sheeva.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_disc.h"
36 #include "lpfc_scsi.h"
37 #include "lpfc.h"
38 #include "lpfc_crtn.h"
39 #include "lpfc_logmsg.h"
40 #include "lpfc_compat.h"
41 #include "lpfc_debugfs.h"
42
43 /*
44  * Define macro to log: Mailbox command x%x cannot issue Data
45  * This allows multiple uses of lpfc_msgBlk0311
46  * w/o perturbing log msg utility.
47  */
48 #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
49                         lpfc_printf_log(phba, \
50                                 KERN_INFO, \
51                                 LOG_MBOX | LOG_SLI, \
52                                 "(%d):0311 Mailbox command x%x cannot " \
53                                 "issue Data: x%x x%x x%x\n", \
54                                 pmbox->vport ? pmbox->vport->vpi : 0, \
55                                 pmbox->mb.mbxCommand,           \
56                                 phba->pport->port_state,        \
57                                 psli->sli_flag, \
58                                 flag)
59
60
61 /* There are only four IOCB completion types. */
62 typedef enum _lpfc_iocb_type {
63         LPFC_UNKNOWN_IOCB,
64         LPFC_UNSOL_IOCB,
65         LPFC_SOL_IOCB,
66         LPFC_ABORT_IOCB
67 } lpfc_iocb_type;
68
69 /**
70  * lpfc_cmd_iocb: Get next command iocb entry in the ring.
71  * @phba: Pointer to HBA context object.
72  * @pring: Pointer to driver SLI ring object.
73  *
74  * This function returns pointer to next command iocb entry
75  * in the command ring. The caller must hold hbalock to prevent
76  * other threads consume the next command iocb.
77  * SLI-2/SLI-3 provide different sized iocbs.
78  **/
79 static inline IOCB_t *
80 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
81 {
82         return (IOCB_t *) (((char *) pring->cmdringaddr) +
83                            pring->cmdidx * phba->iocb_cmd_size);
84 }
85
86 /**
87  * lpfc_resp_iocb: Get next response iocb entry in the ring.
88  * @phba: Pointer to HBA context object.
89  * @pring: Pointer to driver SLI ring object.
90  *
91  * This function returns pointer to next response iocb entry
92  * in the response ring. The caller must hold hbalock to make sure
93  * that no other thread consume the next response iocb.
94  * SLI-2/SLI-3 provide different sized iocbs.
95  **/
96 static inline IOCB_t *
97 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
98 {
99         return (IOCB_t *) (((char *) pring->rspringaddr) +
100                            pring->rspidx * phba->iocb_rsp_size);
101 }
102
103 /**
104  * __lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
105  * @phba: Pointer to HBA context object.
106  *
107  * This function is called with hbalock held. This function
108  * allocates a new driver iocb object from the iocb pool. If the
109  * allocation is successful, it returns pointer to the newly
110  * allocated iocb object else it returns NULL.
111  **/
112 static struct lpfc_iocbq *
113 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
114 {
115         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
116         struct lpfc_iocbq * iocbq = NULL;
117
118         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
119         return iocbq;
120 }
121
122 /**
123  * lpfc_sli_get_iocbq: Allocates an iocb object from iocb pool.
124  * @phba: Pointer to HBA context object.
125  *
126  * This function is called with no lock held. This function
127  * allocates a new driver iocb object from the iocb pool. If the
128  * allocation is successful, it returns pointer to the newly
129  * allocated iocb object else it returns NULL.
130  **/
131 struct lpfc_iocbq *
132 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
133 {
134         struct lpfc_iocbq * iocbq = NULL;
135         unsigned long iflags;
136
137         spin_lock_irqsave(&phba->hbalock, iflags);
138         iocbq = __lpfc_sli_get_iocbq(phba);
139         spin_unlock_irqrestore(&phba->hbalock, iflags);
140         return iocbq;
141 }
142
143 /**
144  * __lpfc_sli_release_iocbq: Release iocb to the iocb pool.
145  * @phba: Pointer to HBA context object.
146  * @iocbq: Pointer to driver iocb object.
147  *
148  * This function is called with hbalock held to release driver
149  * iocb object to the iocb pool. The iotag in the iocb object
150  * does not change for each use of the iocb object. This function
151  * clears all other fields of the iocb object when it is freed.
152  **/
153 static void
154 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
155 {
156         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
157
158         /*
159          * Clean all volatile data fields, preserve iotag and node struct.
160          */
161         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
162         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
163 }
164
165 /**
166  * lpfc_sli_release_iocbq: Release iocb to the iocb pool.
167  * @phba: Pointer to HBA context object.
168  * @iocbq: Pointer to driver iocb object.
169  *
170  * This function is called with no lock held to release the iocb to
171  * iocb pool.
172  **/
173 void
174 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
175 {
176         unsigned long iflags;
177
178         /*
179          * Clean all volatile data fields, preserve iotag and node struct.
180          */
181         spin_lock_irqsave(&phba->hbalock, iflags);
182         __lpfc_sli_release_iocbq(phba, iocbq);
183         spin_unlock_irqrestore(&phba->hbalock, iflags);
184 }
185
186 /**
187  * lpfc_sli_iocb_cmd_type: Get the iocb type.
188  * @iocb_cmnd : iocb command code.
189  *
190  * This function is called by ring event handler function to get the iocb type.
191  * This function translates the iocb command to an iocb command type used to
192  * decide the final disposition of each completed IOCB.
193  * The function returns
194  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
195  * LPFC_SOL_IOCB     if it is a solicited iocb completion
196  * LPFC_ABORT_IOCB   if it is an abort iocb
197  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
198  *
199  * The caller is not required to hold any lock.
200  **/
201 static lpfc_iocb_type
202 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
203 {
204         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
205
206         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
207                 return 0;
208
209         switch (iocb_cmnd) {
210         case CMD_XMIT_SEQUENCE_CR:
211         case CMD_XMIT_SEQUENCE_CX:
212         case CMD_XMIT_BCAST_CN:
213         case CMD_XMIT_BCAST_CX:
214         case CMD_ELS_REQUEST_CR:
215         case CMD_ELS_REQUEST_CX:
216         case CMD_CREATE_XRI_CR:
217         case CMD_CREATE_XRI_CX:
218         case CMD_GET_RPI_CN:
219         case CMD_XMIT_ELS_RSP_CX:
220         case CMD_GET_RPI_CR:
221         case CMD_FCP_IWRITE_CR:
222         case CMD_FCP_IWRITE_CX:
223         case CMD_FCP_IREAD_CR:
224         case CMD_FCP_IREAD_CX:
225         case CMD_FCP_ICMND_CR:
226         case CMD_FCP_ICMND_CX:
227         case CMD_FCP_TSEND_CX:
228         case CMD_FCP_TRSP_CX:
229         case CMD_FCP_TRECEIVE_CX:
230         case CMD_FCP_AUTO_TRSP_CX:
231         case CMD_ADAPTER_MSG:
232         case CMD_ADAPTER_DUMP:
233         case CMD_XMIT_SEQUENCE64_CR:
234         case CMD_XMIT_SEQUENCE64_CX:
235         case CMD_XMIT_BCAST64_CN:
236         case CMD_XMIT_BCAST64_CX:
237         case CMD_ELS_REQUEST64_CR:
238         case CMD_ELS_REQUEST64_CX:
239         case CMD_FCP_IWRITE64_CR:
240         case CMD_FCP_IWRITE64_CX:
241         case CMD_FCP_IREAD64_CR:
242         case CMD_FCP_IREAD64_CX:
243         case CMD_FCP_ICMND64_CR:
244         case CMD_FCP_ICMND64_CX:
245         case CMD_FCP_TSEND64_CX:
246         case CMD_FCP_TRSP64_CX:
247         case CMD_FCP_TRECEIVE64_CX:
248         case CMD_GEN_REQUEST64_CR:
249         case CMD_GEN_REQUEST64_CX:
250         case CMD_XMIT_ELS_RSP64_CX:
251                 type = LPFC_SOL_IOCB;
252                 break;
253         case CMD_ABORT_XRI_CN:
254         case CMD_ABORT_XRI_CX:
255         case CMD_CLOSE_XRI_CN:
256         case CMD_CLOSE_XRI_CX:
257         case CMD_XRI_ABORTED_CX:
258         case CMD_ABORT_MXRI64_CN:
259                 type = LPFC_ABORT_IOCB;
260                 break;
261         case CMD_RCV_SEQUENCE_CX:
262         case CMD_RCV_ELS_REQ_CX:
263         case CMD_RCV_SEQUENCE64_CX:
264         case CMD_RCV_ELS_REQ64_CX:
265         case CMD_ASYNC_STATUS:
266         case CMD_IOCB_RCV_SEQ64_CX:
267         case CMD_IOCB_RCV_ELS64_CX:
268         case CMD_IOCB_RCV_CONT64_CX:
269         case CMD_IOCB_RET_XRI64_CX:
270                 type = LPFC_UNSOL_IOCB;
271                 break;
272         case CMD_IOCB_XMIT_MSEQ64_CR:
273         case CMD_IOCB_XMIT_MSEQ64_CX:
274         case CMD_IOCB_RCV_SEQ_LIST64_CX:
275         case CMD_IOCB_RCV_ELS_LIST64_CX:
276         case CMD_IOCB_CLOSE_EXTENDED_CN:
277         case CMD_IOCB_ABORT_EXTENDED_CN:
278         case CMD_IOCB_RET_HBQE64_CN:
279         case CMD_IOCB_FCP_IBIDIR64_CR:
280         case CMD_IOCB_FCP_IBIDIR64_CX:
281         case CMD_IOCB_FCP_ITASKMGT64_CX:
282         case CMD_IOCB_LOGENTRY_CN:
283         case CMD_IOCB_LOGENTRY_ASYNC_CN:
284                 printk("%s - Unhandled SLI-3 Command x%x\n",
285                                 __func__, iocb_cmnd);
286                 type = LPFC_UNKNOWN_IOCB;
287                 break;
288         default:
289                 type = LPFC_UNKNOWN_IOCB;
290                 break;
291         }
292
293         return type;
294 }
295
296 /**
297  * lpfc_sli_ring_map: Issue config_ring mbox for all rings.
298  * @phba: Pointer to HBA context object.
299  *
300  * This function is called from SLI initialization code
301  * to configure every ring of the HBA's SLI interface. The
302  * caller is not required to hold any lock. This function issues
303  * a config_ring mailbox command for each ring.
304  * This function returns zero if successful else returns a negative
305  * error code.
306  **/
307 static int
308 lpfc_sli_ring_map(struct lpfc_hba *phba)
309 {
310         struct lpfc_sli *psli = &phba->sli;
311         LPFC_MBOXQ_t *pmb;
312         MAILBOX_t *pmbox;
313         int i, rc, ret = 0;
314
315         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
316         if (!pmb)
317                 return -ENOMEM;
318         pmbox = &pmb->mb;
319         phba->link_state = LPFC_INIT_MBX_CMDS;
320         for (i = 0; i < psli->num_rings; i++) {
321                 lpfc_config_ring(phba, i, pmb);
322                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
323                 if (rc != MBX_SUCCESS) {
324                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
325                                         "0446 Adapter failed to init (%d), "
326                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
327                                         "ring %d\n",
328                                         rc, pmbox->mbxCommand,
329                                         pmbox->mbxStatus, i);
330                         phba->link_state = LPFC_HBA_ERROR;
331                         ret = -ENXIO;
332                         break;
333                 }
334         }
335         mempool_free(pmb, phba->mbox_mem_pool);
336         return ret;
337 }
338
339 /**
340  * lpfc_sli_ringtxcmpl_put: Adds new iocb to the txcmplq.
341  * @phba: Pointer to HBA context object.
342  * @pring: Pointer to driver SLI ring object.
343  * @piocb: Pointer to the driver iocb object.
344  *
345  * This function is called with hbalock held. The function adds the
346  * new iocb to txcmplq of the given ring. This function always returns
347  * 0. If this function is called for ELS ring, this function checks if
348  * there is a vport associated with the ELS command. This function also
349  * starts els_tmofunc timer if this is an ELS command.
350  **/
351 static int
352 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
353                         struct lpfc_iocbq *piocb)
354 {
355         list_add_tail(&piocb->list, &pring->txcmplq);
356         pring->txcmplq_cnt++;
357         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
358            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
359            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
360                 if (!piocb->vport)
361                         BUG();
362                 else
363                         mod_timer(&piocb->vport->els_tmofunc,
364                                   jiffies + HZ * (phba->fc_ratov << 1));
365         }
366
367
368         return 0;
369 }
370
371 /**
372  * lpfc_sli_ringtx_get: Get first element of the txq.
373  * @phba: Pointer to HBA context object.
374  * @pring: Pointer to driver SLI ring object.
375  *
376  * This function is called with hbalock held to get next
377  * iocb in txq of the given ring. If there is any iocb in
378  * the txq, the function returns first iocb in the list after
379  * removing the iocb from the list, else it returns NULL.
380  **/
381 static struct lpfc_iocbq *
382 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
383 {
384         struct lpfc_iocbq *cmd_iocb;
385
386         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
387         if (cmd_iocb != NULL)
388                 pring->txq_cnt--;
389         return cmd_iocb;
390 }
391
392 /**
393  * lpfc_sli_next_iocb_slot: Get next iocb slot in the ring.
394  * @phba: Pointer to HBA context object.
395  * @pring: Pointer to driver SLI ring object.
396  *
397  * This function is called with hbalock held and the caller must post the
398  * iocb without releasing the lock. If the caller releases the lock,
399  * iocb slot returned by the function is not guaranteed to be available.
400  * The function returns pointer to the next available iocb slot if there
401  * is available slot in the ring, else it returns NULL.
402  * If the get index of the ring is ahead of the put index, the function
403  * will post an error attention event to the worker thread to take the
404  * HBA to offline state.
405  **/
406 static IOCB_t *
407 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
408 {
409         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
410         uint32_t  max_cmd_idx = pring->numCiocb;
411         if ((pring->next_cmdidx == pring->cmdidx) &&
412            (++pring->next_cmdidx >= max_cmd_idx))
413                 pring->next_cmdidx = 0;
414
415         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
416
417                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
418
419                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
420                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
421                                         "0315 Ring %d issue: portCmdGet %d "
422                                         "is bigger then cmd ring %d\n",
423                                         pring->ringno,
424                                         pring->local_getidx, max_cmd_idx);
425
426                         phba->link_state = LPFC_HBA_ERROR;
427                         /*
428                          * All error attention handlers are posted to
429                          * worker thread
430                          */
431                         phba->work_ha |= HA_ERATT;
432                         phba->work_hs = HS_FFER3;
433
434                         lpfc_worker_wake_up(phba);
435
436                         return NULL;
437                 }
438
439                 if (pring->local_getidx == pring->next_cmdidx)
440                         return NULL;
441         }
442
443         return lpfc_cmd_iocb(phba, pring);
444 }
445
446 /**
447  * lpfc_sli_next_iotag: Get an iotag for the iocb.
448  * @phba: Pointer to HBA context object.
449  * @iocbq: Pointer to driver iocb object.
450  *
451  * This function gets an iotag for the iocb. If there is no unused iotag and
452  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
453  * array and assigns a new iotag.
454  * The function returns the allocated iotag if successful, else returns zero.
455  * Zero is not a valid iotag.
456  * The caller is not required to hold any lock.
457  **/
458 uint16_t
459 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
460 {
461         struct lpfc_iocbq **new_arr;
462         struct lpfc_iocbq **old_arr;
463         size_t new_len;
464         struct lpfc_sli *psli = &phba->sli;
465         uint16_t iotag;
466
467         spin_lock_irq(&phba->hbalock);
468         iotag = psli->last_iotag;
469         if(++iotag < psli->iocbq_lookup_len) {
470                 psli->last_iotag = iotag;
471                 psli->iocbq_lookup[iotag] = iocbq;
472                 spin_unlock_irq(&phba->hbalock);
473                 iocbq->iotag = iotag;
474                 return iotag;
475         } else if (psli->iocbq_lookup_len < (0xffff
476                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
477                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
478                 spin_unlock_irq(&phba->hbalock);
479                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
480                                   GFP_KERNEL);
481                 if (new_arr) {
482                         spin_lock_irq(&phba->hbalock);
483                         old_arr = psli->iocbq_lookup;
484                         if (new_len <= psli->iocbq_lookup_len) {
485                                 /* highly unprobable case */
486                                 kfree(new_arr);
487                                 iotag = psli->last_iotag;
488                                 if(++iotag < psli->iocbq_lookup_len) {
489                                         psli->last_iotag = iotag;
490                                         psli->iocbq_lookup[iotag] = iocbq;
491                                         spin_unlock_irq(&phba->hbalock);
492                                         iocbq->iotag = iotag;
493                                         return iotag;
494                                 }
495                                 spin_unlock_irq(&phba->hbalock);
496                                 return 0;
497                         }
498                         if (psli->iocbq_lookup)
499                                 memcpy(new_arr, old_arr,
500                                        ((psli->last_iotag  + 1) *
501                                         sizeof (struct lpfc_iocbq *)));
502                         psli->iocbq_lookup = new_arr;
503                         psli->iocbq_lookup_len = new_len;
504                         psli->last_iotag = iotag;
505                         psli->iocbq_lookup[iotag] = iocbq;
506                         spin_unlock_irq(&phba->hbalock);
507                         iocbq->iotag = iotag;
508                         kfree(old_arr);
509                         return iotag;
510                 }
511         } else
512                 spin_unlock_irq(&phba->hbalock);
513
514         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
515                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
516                         psli->last_iotag);
517
518         return 0;
519 }
520
521 /**
522  * lpfc_sli_submit_iocb: Submit an iocb to the firmware.
523  * @phba: Pointer to HBA context object.
524  * @pring: Pointer to driver SLI ring object.
525  * @iocb: Pointer to iocb slot in the ring.
526  * @nextiocb: Pointer to driver iocb object which need to be
527  *            posted to firmware.
528  *
529  * This function is called with hbalock held to post a new iocb to
530  * the firmware. This function copies the new iocb to ring iocb slot and
531  * updates the ring pointers. It adds the new iocb to txcmplq if there is
532  * a completion call back for this iocb else the function will free the
533  * iocb object.
534  **/
535 static void
536 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
537                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
538 {
539         /*
540          * Set up an iotag
541          */
542         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
543
544         if (pring->ringno == LPFC_ELS_RING) {
545                 lpfc_debugfs_slow_ring_trc(phba,
546                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
547                         *(((uint32_t *) &nextiocb->iocb) + 4),
548                         *(((uint32_t *) &nextiocb->iocb) + 6),
549                         *(((uint32_t *) &nextiocb->iocb) + 7));
550         }
551
552         /*
553          * Issue iocb command to adapter
554          */
555         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
556         wmb();
557         pring->stats.iocb_cmd++;
558
559         /*
560          * If there is no completion routine to call, we can release the
561          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
562          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
563          */
564         if (nextiocb->iocb_cmpl)
565                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
566         else
567                 __lpfc_sli_release_iocbq(phba, nextiocb);
568
569         /*
570          * Let the HBA know what IOCB slot will be the next one the
571          * driver will put a command into.
572          */
573         pring->cmdidx = pring->next_cmdidx;
574         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
575 }
576
577 /**
578  * lpfc_sli_update_full_ring: Update the chip attention register.
579  * @phba: Pointer to HBA context object.
580  * @pring: Pointer to driver SLI ring object.
581  *
582  * The caller is not required to hold any lock for calling this function.
583  * This function updates the chip attention bits for the ring to inform firmware
584  * that there are pending work to be done for this ring and requests an
585  * interrupt when there is space available in the ring. This function is
586  * called when the driver is unable to post more iocbs to the ring due
587  * to unavailability of space in the ring.
588  **/
589 static void
590 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
591 {
592         int ringno = pring->ringno;
593
594         pring->flag |= LPFC_CALL_RING_AVAILABLE;
595
596         wmb();
597
598         /*
599          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
600          * The HBA will tell us when an IOCB entry is available.
601          */
602         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
603         readl(phba->CAregaddr); /* flush */
604
605         pring->stats.iocb_cmd_full++;
606 }
607
608 /**
609  * lpfc_sli_update_ring: Update chip attention register.
610  * @phba: Pointer to HBA context object.
611  * @pring: Pointer to driver SLI ring object.
612  *
613  * This function updates the chip attention register bit for the
614  * given ring to inform HBA that there is more work to be done
615  * in this ring. The caller is not required to hold any lock.
616  **/
617 static void
618 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
619 {
620         int ringno = pring->ringno;
621
622         /*
623          * Tell the HBA that there is work to do in this ring.
624          */
625         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
626                 wmb();
627                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
628                 readl(phba->CAregaddr); /* flush */
629         }
630 }
631
632 /**
633  * lpfc_sli_resume_iocb: Process iocbs in the txq.
634  * @phba: Pointer to HBA context object.
635  * @pring: Pointer to driver SLI ring object.
636  *
637  * This function is called with hbalock held to post pending iocbs
638  * in the txq to the firmware. This function is called when driver
639  * detects space available in the ring.
640  **/
641 static void
642 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
643 {
644         IOCB_t *iocb;
645         struct lpfc_iocbq *nextiocb;
646
647         /*
648          * Check to see if:
649          *  (a) there is anything on the txq to send
650          *  (b) link is up
651          *  (c) link attention events can be processed (fcp ring only)
652          *  (d) IOCB processing is not blocked by the outstanding mbox command.
653          */
654         if (pring->txq_cnt &&
655             lpfc_is_link_up(phba) &&
656             (pring->ringno != phba->sli.fcp_ring ||
657              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
658
659                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
660                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
661                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
662
663                 if (iocb)
664                         lpfc_sli_update_ring(phba, pring);
665                 else
666                         lpfc_sli_update_full_ring(phba, pring);
667         }
668
669         return;
670 }
671
672 /**
673  * lpfc_sli_next_hbq_slot: Get next hbq entry for the HBQ.
674  * @phba: Pointer to HBA context object.
675  * @hbqno: HBQ number.
676  *
677  * This function is called with hbalock held to get the next
678  * available slot for the given HBQ. If there is free slot
679  * available for the HBQ it will return pointer to the next available
680  * HBQ entry else it will return NULL.
681  **/
682 static struct lpfc_hbq_entry *
683 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
684 {
685         struct hbq_s *hbqp = &phba->hbqs[hbqno];
686
687         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
688             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
689                 hbqp->next_hbqPutIdx = 0;
690
691         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
692                 uint32_t raw_index = phba->hbq_get[hbqno];
693                 uint32_t getidx = le32_to_cpu(raw_index);
694
695                 hbqp->local_hbqGetIdx = getidx;
696
697                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
698                         lpfc_printf_log(phba, KERN_ERR,
699                                         LOG_SLI | LOG_VPORT,
700                                         "1802 HBQ %d: local_hbqGetIdx "
701                                         "%u is > than hbqp->entry_count %u\n",
702                                         hbqno, hbqp->local_hbqGetIdx,
703                                         hbqp->entry_count);
704
705                         phba->link_state = LPFC_HBA_ERROR;
706                         return NULL;
707                 }
708
709                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
710                         return NULL;
711         }
712
713         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
714                         hbqp->hbqPutIdx;
715 }
716
717 /**
718  * lpfc_sli_hbqbuf_free_all: Free all the hbq buffers.
719  * @phba: Pointer to HBA context object.
720  *
721  * This function is called with no lock held to free all the
722  * hbq buffers while uninitializing the SLI interface. It also
723  * frees the HBQ buffers returned by the firmware but not yet
724  * processed by the upper layers.
725  **/
726 void
727 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
728 {
729         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
730         struct hbq_dmabuf *hbq_buf;
731         unsigned long flags;
732         int i, hbq_count;
733         uint32_t hbqno;
734
735         hbq_count = lpfc_sli_hbq_count();
736         /* Return all memory used by all HBQs */
737         spin_lock_irqsave(&phba->hbalock, flags);
738         for (i = 0; i < hbq_count; ++i) {
739                 list_for_each_entry_safe(dmabuf, next_dmabuf,
740                                 &phba->hbqs[i].hbq_buffer_list, list) {
741                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
742                         list_del(&hbq_buf->dbuf.list);
743                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
744                 }
745                 phba->hbqs[i].buffer_count = 0;
746         }
747         /* Return all HBQ buffer that are in-fly */
748         list_for_each_entry_safe(dmabuf, next_dmabuf,
749                         &phba->hbqbuf_in_list, list) {
750                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
751                 list_del(&hbq_buf->dbuf.list);
752                 if (hbq_buf->tag == -1) {
753                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
754                                 (phba, hbq_buf);
755                 } else {
756                         hbqno = hbq_buf->tag >> 16;
757                         if (hbqno >= LPFC_MAX_HBQS)
758                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
759                                         (phba, hbq_buf);
760                         else
761                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
762                                         hbq_buf);
763                 }
764         }
765
766         /* Mark the HBQs not in use */
767         phba->hbq_in_use = 0;
768         spin_unlock_irqrestore(&phba->hbalock, flags);
769 }
770
771 /**
772  * lpfc_sli_hbq_to_firmware: Post the hbq buffer to firmware.
773  * @phba: Pointer to HBA context object.
774  * @hbqno: HBQ number.
775  * @hbq_buf: Pointer to HBQ buffer.
776  *
777  * This function is called with the hbalock held to post a
778  * hbq buffer to the firmware. If the function finds an empty
779  * slot in the HBQ, it will post the buffer. The function will return
780  * pointer to the hbq entry if it successfully post the buffer
781  * else it will return NULL.
782  **/
783 static struct lpfc_hbq_entry *
784 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
785                          struct hbq_dmabuf *hbq_buf)
786 {
787         struct lpfc_hbq_entry *hbqe;
788         dma_addr_t physaddr = hbq_buf->dbuf.phys;
789
790         /* Get next HBQ entry slot to use */
791         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
792         if (hbqe) {
793                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
794
795                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
796                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
797                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
798                 hbqe->bde.tus.f.bdeFlags = 0;
799                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
800                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
801                                 /* Sync SLIM */
802                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
803                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
804                                 /* flush */
805                 readl(phba->hbq_put + hbqno);
806                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
807         }
808         return hbqe;
809 }
810
811 /* HBQ for ELS and CT traffic. */
812 static struct lpfc_hbq_init lpfc_els_hbq = {
813         .rn = 1,
814         .entry_count = 200,
815         .mask_count = 0,
816         .profile = 0,
817         .ring_mask = (1 << LPFC_ELS_RING),
818         .buffer_count = 0,
819         .init_count = 20,
820         .add_count = 5,
821 };
822
823 /* HBQ for the extra ring if needed */
824 static struct lpfc_hbq_init lpfc_extra_hbq = {
825         .rn = 1,
826         .entry_count = 200,
827         .mask_count = 0,
828         .profile = 0,
829         .ring_mask = (1 << LPFC_EXTRA_RING),
830         .buffer_count = 0,
831         .init_count = 0,
832         .add_count = 5,
833 };
834
835 /* Array of HBQs */
836 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
837         &lpfc_els_hbq,
838         &lpfc_extra_hbq,
839 };
840
841 /**
842  * lpfc_sli_hbqbuf_fill_hbqs: Post more hbq buffers to HBQ.
843  * @phba: Pointer to HBA context object.
844  * @hbqno: HBQ number.
845  * @count: Number of HBQ buffers to be posted.
846  *
847  * This function is called with no lock held to post more hbq buffers to the
848  * given HBQ. The function returns the number of HBQ buffers successfully
849  * posted.
850  **/
851 static int
852 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
853 {
854         uint32_t i, posted = 0;
855         unsigned long flags;
856         struct hbq_dmabuf *hbq_buffer;
857         LIST_HEAD(hbq_buf_list);
858         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
859                 return 0;
860
861         if ((phba->hbqs[hbqno].buffer_count + count) >
862             lpfc_hbq_defs[hbqno]->entry_count)
863                 count = lpfc_hbq_defs[hbqno]->entry_count -
864                                         phba->hbqs[hbqno].buffer_count;
865         if (!count)
866                 return 0;
867         /* Allocate HBQ entries */
868         for (i = 0; i < count; i++) {
869                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
870                 if (!hbq_buffer)
871                         break;
872                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
873         }
874         /* Check whether HBQ is still in use */
875         spin_lock_irqsave(&phba->hbalock, flags);
876         if (!phba->hbq_in_use)
877                 goto err;
878         while (!list_empty(&hbq_buf_list)) {
879                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
880                                  dbuf.list);
881                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
882                                       (hbqno << 16));
883                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
884                         phba->hbqs[hbqno].buffer_count++;
885                         posted++;
886                 } else
887                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
888         }
889         spin_unlock_irqrestore(&phba->hbalock, flags);
890         return posted;
891 err:
892         spin_unlock_irqrestore(&phba->hbalock, flags);
893         while (!list_empty(&hbq_buf_list)) {
894                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
895                                  dbuf.list);
896                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
897         }
898         return 0;
899 }
900
901 /**
902  * lpfc_sli_hbqbuf_add_hbqs: Post more HBQ buffers to firmware.
903  * @phba: Pointer to HBA context object.
904  * @qno: HBQ number.
905  *
906  * This function posts more buffers to the HBQ. This function
907  * is called with no lock held. The function returns the number of HBQ entries
908  * successfully allocated.
909  **/
910 int
911 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
912 {
913         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
914                                          lpfc_hbq_defs[qno]->add_count));
915 }
916
917 /**
918  * lpfc_sli_hbqbuf_init_hbqs: Post initial buffers to the HBQ.
919  * @phba: Pointer to HBA context object.
920  * @qno:  HBQ queue number.
921  *
922  * This function is called from SLI initialization code path with
923  * no lock held to post initial HBQ buffers to firmware. The
924  * function returns the number of HBQ entries successfully allocated.
925  **/
926 static int
927 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
928 {
929         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
930                                          lpfc_hbq_defs[qno]->init_count));
931 }
932
933 /**
934  * lpfc_sli_hbqbuf_find: Find the hbq buffer associated with a tag.
935  * @phba: Pointer to HBA context object.
936  * @tag: Tag of the hbq buffer.
937  *
938  * This function is called with hbalock held. This function searches
939  * for the hbq buffer associated with the given tag in the hbq buffer
940  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
941  * it returns NULL.
942  **/
943 static struct hbq_dmabuf *
944 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
945 {
946         struct lpfc_dmabuf *d_buf;
947         struct hbq_dmabuf *hbq_buf;
948         uint32_t hbqno;
949
950         hbqno = tag >> 16;
951         if (hbqno >= LPFC_MAX_HBQS)
952                 return NULL;
953
954         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
955                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
956                 if (hbq_buf->tag == tag) {
957                         return hbq_buf;
958                 }
959         }
960         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
961                         "1803 Bad hbq tag. Data: x%x x%x\n",
962                         tag, phba->hbqs[tag >> 16].buffer_count);
963         return NULL;
964 }
965
966 /**
967  * lpfc_sli_free_hbq: Give back the hbq buffer to firmware.
968  * @phba: Pointer to HBA context object.
969  * @hbq_buffer: Pointer to HBQ buffer.
970  *
971  * This function is called with hbalock. This function gives back
972  * the hbq buffer to firmware. If the HBQ does not have space to
973  * post the buffer, it will free the buffer.
974  **/
975 void
976 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
977 {
978         uint32_t hbqno;
979
980         if (hbq_buffer) {
981                 hbqno = hbq_buffer->tag >> 16;
982                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
983                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
984                 }
985         }
986 }
987
988 /**
989  * lpfc_sli_chk_mbx_command: Check if the mailbox is a legitimate mailbox.
990  * @mbxCommand: mailbox command code.
991  *
992  * This function is called by the mailbox event handler function to verify
993  * that the completed mailbox command is a legitimate mailbox command. If the
994  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
995  * and the mailbox event handler will take the HBA offline.
996  **/
997 static int
998 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
999 {
1000         uint8_t ret;
1001
1002         switch (mbxCommand) {
1003         case MBX_LOAD_SM:
1004         case MBX_READ_NV:
1005         case MBX_WRITE_NV:
1006         case MBX_WRITE_VPARMS:
1007         case MBX_RUN_BIU_DIAG:
1008         case MBX_INIT_LINK:
1009         case MBX_DOWN_LINK:
1010         case MBX_CONFIG_LINK:
1011         case MBX_CONFIG_RING:
1012         case MBX_RESET_RING:
1013         case MBX_READ_CONFIG:
1014         case MBX_READ_RCONFIG:
1015         case MBX_READ_SPARM:
1016         case MBX_READ_STATUS:
1017         case MBX_READ_RPI:
1018         case MBX_READ_XRI:
1019         case MBX_READ_REV:
1020         case MBX_READ_LNK_STAT:
1021         case MBX_REG_LOGIN:
1022         case MBX_UNREG_LOGIN:
1023         case MBX_READ_LA:
1024         case MBX_CLEAR_LA:
1025         case MBX_DUMP_MEMORY:
1026         case MBX_DUMP_CONTEXT:
1027         case MBX_RUN_DIAGS:
1028         case MBX_RESTART:
1029         case MBX_UPDATE_CFG:
1030         case MBX_DOWN_LOAD:
1031         case MBX_DEL_LD_ENTRY:
1032         case MBX_RUN_PROGRAM:
1033         case MBX_SET_MASK:
1034         case MBX_SET_VARIABLE:
1035         case MBX_UNREG_D_ID:
1036         case MBX_KILL_BOARD:
1037         case MBX_CONFIG_FARP:
1038         case MBX_BEACON:
1039         case MBX_LOAD_AREA:
1040         case MBX_RUN_BIU_DIAG64:
1041         case MBX_CONFIG_PORT:
1042         case MBX_READ_SPARM64:
1043         case MBX_READ_RPI64:
1044         case MBX_REG_LOGIN64:
1045         case MBX_READ_LA64:
1046         case MBX_WRITE_WWN:
1047         case MBX_SET_DEBUG:
1048         case MBX_LOAD_EXP_ROM:
1049         case MBX_ASYNCEVT_ENABLE:
1050         case MBX_REG_VPI:
1051         case MBX_UNREG_VPI:
1052         case MBX_HEARTBEAT:
1053         case MBX_PORT_CAPABILITIES:
1054         case MBX_PORT_IOV_CONTROL:
1055                 ret = mbxCommand;
1056                 break;
1057         default:
1058                 ret = MBX_SHUTDOWN;
1059                 break;
1060         }
1061         return ret;
1062 }
1063
1064 /**
1065  * lpfc_sli_wake_mbox_wait: Completion handler for mbox issued from
1066  *          lpfc_sli_issue_mbox_wait.
1067  * @phba: Pointer to HBA context object.
1068  * @pmboxq: Pointer to mailbox command.
1069  *
1070  * This is completion handler function for mailbox commands issued from
1071  * lpfc_sli_issue_mbox_wait function. This function is called by the
1072  * mailbox event handler function with no lock held. This function
1073  * will wake up thread waiting on the wait queue pointed by context1
1074  * of the mailbox.
1075  **/
1076 static void
1077 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1078 {
1079         wait_queue_head_t *pdone_q;
1080         unsigned long drvr_flag;
1081
1082         /*
1083          * If pdone_q is empty, the driver thread gave up waiting and
1084          * continued running.
1085          */
1086         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1087         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1088         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1089         if (pdone_q)
1090                 wake_up_interruptible(pdone_q);
1091         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1092         return;
1093 }
1094
1095
1096 /**
1097  * lpfc_sli_def_mbox_cmpl: Default mailbox completion handler.
1098  * @phba: Pointer to HBA context object.
1099  * @pmb: Pointer to mailbox object.
1100  *
1101  * This function is the default mailbox completion handler. It
1102  * frees the memory resources associated with the completed mailbox
1103  * command. If the completed command is a REG_LOGIN mailbox command,
1104  * this function will issue a UREG_LOGIN to re-claim the RPI.
1105  **/
1106 void
1107 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1108 {
1109         struct lpfc_dmabuf *mp;
1110         uint16_t rpi;
1111         int rc;
1112
1113         mp = (struct lpfc_dmabuf *) (pmb->context1);
1114
1115         if (mp) {
1116                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1117                 kfree(mp);
1118         }
1119
1120         /*
1121          * If a REG_LOGIN succeeded  after node is destroyed or node
1122          * is in re-discovery driver need to cleanup the RPI.
1123          */
1124         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1125             pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1126             !pmb->mb.mbxStatus) {
1127
1128                 rpi = pmb->mb.un.varWords[0];
1129                 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1130                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1131                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1132                 if (rc != MBX_NOT_FINISHED)
1133                         return;
1134         }
1135
1136         mempool_free(pmb, phba->mbox_mem_pool);
1137         return;
1138 }
1139
1140 /**
1141  * lpfc_sli_handle_mb_event: Handle mailbox completions from firmware.
1142  * @phba: Pointer to HBA context object.
1143  *
1144  * This function is called with no lock held. This function processes all
1145  * the completed mailbox commands and gives it to upper layers. The interrupt
1146  * service routine processes mailbox completion interrupt and adds completed
1147  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1148  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1149  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1150  * function returns the mailbox commands to the upper layer by calling the
1151  * completion handler function of each mailbox.
1152  **/
1153 int
1154 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1155 {
1156         MAILBOX_t *pmbox;
1157         LPFC_MBOXQ_t *pmb;
1158         int rc;
1159         LIST_HEAD(cmplq);
1160
1161         phba->sli.slistat.mbox_event++;
1162
1163         /* Get all completed mailboxe buffers into the cmplq */
1164         spin_lock_irq(&phba->hbalock);
1165         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1166         spin_unlock_irq(&phba->hbalock);
1167
1168         /* Get a Mailbox buffer to setup mailbox commands for callback */
1169         do {
1170                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1171                 if (pmb == NULL)
1172                         break;
1173
1174                 pmbox = &pmb->mb;
1175
1176                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1177                         if (pmb->vport) {
1178                                 lpfc_debugfs_disc_trc(pmb->vport,
1179                                         LPFC_DISC_TRC_MBOX_VPORT,
1180                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1181                                         (uint32_t)pmbox->mbxCommand,
1182                                         pmbox->un.varWords[0],
1183                                         pmbox->un.varWords[1]);
1184                         }
1185                         else {
1186                                 lpfc_debugfs_disc_trc(phba->pport,
1187                                         LPFC_DISC_TRC_MBOX,
1188                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1189                                         (uint32_t)pmbox->mbxCommand,
1190                                         pmbox->un.varWords[0],
1191                                         pmbox->un.varWords[1]);
1192                         }
1193                 }
1194
1195                 /*
1196                  * It is a fatal error if unknown mbox command completion.
1197                  */
1198                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1199                     MBX_SHUTDOWN) {
1200                         /* Unknow mailbox command compl */
1201                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1202                                         "(%d):0323 Unknown Mailbox command "
1203                                         "%x Cmpl\n",
1204                                         pmb->vport ? pmb->vport->vpi : 0,
1205                                         pmbox->mbxCommand);
1206                         phba->link_state = LPFC_HBA_ERROR;
1207                         phba->work_hs = HS_FFER3;
1208                         lpfc_handle_eratt(phba);
1209                         continue;
1210                 }
1211
1212                 if (pmbox->mbxStatus) {
1213                         phba->sli.slistat.mbox_stat_err++;
1214                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1215                                 /* Mbox cmd cmpl error - RETRYing */
1216                                 lpfc_printf_log(phba, KERN_INFO,
1217                                                 LOG_MBOX | LOG_SLI,
1218                                                 "(%d):0305 Mbox cmd cmpl "
1219                                                 "error - RETRYing Data: x%x "
1220                                                 "x%x x%x x%x\n",
1221                                                 pmb->vport ? pmb->vport->vpi :0,
1222                                                 pmbox->mbxCommand,
1223                                                 pmbox->mbxStatus,
1224                                                 pmbox->un.varWords[0],
1225                                                 pmb->vport->port_state);
1226                                 pmbox->mbxStatus = 0;
1227                                 pmbox->mbxOwner = OWN_HOST;
1228                                 spin_lock_irq(&phba->hbalock);
1229                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1230                                 spin_unlock_irq(&phba->hbalock);
1231                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1232                                 if (rc == MBX_SUCCESS)
1233                                         continue;
1234                         }
1235                 }
1236
1237                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1238                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1239                                 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
1240                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1241                                 pmb->vport ? pmb->vport->vpi : 0,
1242                                 pmbox->mbxCommand,
1243                                 pmb->mbox_cmpl,
1244                                 *((uint32_t *) pmbox),
1245                                 pmbox->un.varWords[0],
1246                                 pmbox->un.varWords[1],
1247                                 pmbox->un.varWords[2],
1248                                 pmbox->un.varWords[3],
1249                                 pmbox->un.varWords[4],
1250                                 pmbox->un.varWords[5],
1251                                 pmbox->un.varWords[6],
1252                                 pmbox->un.varWords[7]);
1253
1254                 if (pmb->mbox_cmpl)
1255                         pmb->mbox_cmpl(phba,pmb);
1256         } while (1);
1257         return 0;
1258 }
1259
1260 /**
1261  * lpfc_sli_replace_hbqbuff: Replace the HBQ buffer with a new buffer.
1262  * @phba: Pointer to HBA context object.
1263  * @tag: Tag for the HBQ buffer.
1264  *
1265  * This function is called from unsolicited event handler code path to get the
1266  * HBQ buffer associated with an unsolicited iocb. This function is called with
1267  * no lock held. It returns the buffer associated with the given tag and posts
1268  * another buffer to the firmware. Note that the new buffer must be allocated
1269  * before taking the hbalock and that the hba lock must be held until it is
1270  * finished with the hbq entry swap.
1271  **/
1272 static struct lpfc_dmabuf *
1273 lpfc_sli_replace_hbqbuff(struct lpfc_hba *phba, uint32_t tag)
1274 {
1275         struct hbq_dmabuf *hbq_entry, *new_hbq_entry;
1276         uint32_t hbqno;
1277         void *virt;             /* virtual address ptr */
1278         dma_addr_t phys;        /* mapped address */
1279         unsigned long flags;
1280
1281         hbqno = tag >> 16;
1282         new_hbq_entry = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1283         /* Check whether HBQ is still in use */
1284         spin_lock_irqsave(&phba->hbalock, flags);
1285         if (!phba->hbq_in_use) {
1286                 if (new_hbq_entry)
1287                         (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1288                                                             new_hbq_entry);
1289                 spin_unlock_irqrestore(&phba->hbalock, flags);
1290                 return NULL;
1291         }
1292
1293         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1294         if (hbq_entry == NULL) {
1295                 if (new_hbq_entry)
1296                         (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1297                                                             new_hbq_entry);
1298                 spin_unlock_irqrestore(&phba->hbalock, flags);
1299                 return NULL;
1300         }
1301         list_del(&hbq_entry->dbuf.list);
1302
1303         if (new_hbq_entry == NULL) {
1304                 list_add_tail(&hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1305                 spin_unlock_irqrestore(&phba->hbalock, flags);
1306                 return &hbq_entry->dbuf;
1307         }
1308         new_hbq_entry->tag = -1;
1309         phys = new_hbq_entry->dbuf.phys;
1310         virt = new_hbq_entry->dbuf.virt;
1311         new_hbq_entry->dbuf.phys = hbq_entry->dbuf.phys;
1312         new_hbq_entry->dbuf.virt = hbq_entry->dbuf.virt;
1313         hbq_entry->dbuf.phys = phys;
1314         hbq_entry->dbuf.virt = virt;
1315         lpfc_sli_free_hbq(phba, hbq_entry);
1316         list_add_tail(&new_hbq_entry->dbuf.list, &phba->hbqbuf_in_list);
1317         spin_unlock_irqrestore(&phba->hbalock, flags);
1318
1319         return &new_hbq_entry->dbuf;
1320 }
1321
1322 /**
1323  * lpfc_sli_get_buff: Get the buffer associated with the buffer tag.
1324  * @phba: Pointer to HBA context object.
1325  * @pring: Pointer to driver SLI ring object.
1326  * @tag: buffer tag.
1327  *
1328  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1329  * is set in the tag the buffer is posted for a particular exchange,
1330  * the function will return the buffer without replacing the buffer.
1331  * If the buffer is for unsolicited ELS or CT traffic, this function
1332  * returns the buffer and also posts another buffer to the firmware.
1333  **/
1334 static struct lpfc_dmabuf *
1335 lpfc_sli_get_buff(struct lpfc_hba *phba,
1336                         struct lpfc_sli_ring *pring,
1337                         uint32_t tag)
1338 {
1339         if (tag & QUE_BUFTAG_BIT)
1340                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1341         else
1342                 return lpfc_sli_replace_hbqbuff(phba, tag);
1343 }
1344
1345
1346 /**
1347  * lpfc_sli_process_unsol_iocb: Unsolicited iocb handler.
1348  * @phba: Pointer to HBA context object.
1349  * @pring: Pointer to driver SLI ring object.
1350  * @saveq: Pointer to the unsolicited iocb.
1351  *
1352  * This function is called with no lock held by the ring event handler
1353  * when there is an unsolicited iocb posted to the response ring by the
1354  * firmware. This function gets the buffer associated with the iocbs
1355  * and calls the event handler for the ring. This function handles both
1356  * qring buffers and hbq buffers.
1357  * When the function returns 1 the caller can free the iocb object otherwise
1358  * upper layer functions will free the iocb objects.
1359  **/
1360 static int
1361 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1362                             struct lpfc_iocbq *saveq)
1363 {
1364         IOCB_t           * irsp;
1365         WORD5            * w5p;
1366         uint32_t           Rctl, Type;
1367         uint32_t           match, i;
1368         struct lpfc_iocbq *iocbq;
1369         struct lpfc_dmabuf *dmzbuf;
1370
1371         match = 0;
1372         irsp = &(saveq->iocb);
1373
1374         if (irsp->ulpStatus == IOSTAT_NEED_BUFFER)
1375                 return 1;
1376         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1377                 if (pring->lpfc_sli_rcv_async_status)
1378                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1379                 else
1380                         lpfc_printf_log(phba,
1381                                         KERN_WARNING,
1382                                         LOG_SLI,
1383                                         "0316 Ring %d handler: unexpected "
1384                                         "ASYNC_STATUS iocb received evt_code "
1385                                         "0x%x\n",
1386                                         pring->ringno,
1387                                         irsp->un.asyncstat.evt_code);
1388                 return 1;
1389         }
1390
1391         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1392                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1393                 if (irsp->ulpBdeCount > 0) {
1394                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1395                                         irsp->un.ulpWord[3]);
1396                         lpfc_in_buf_free(phba, dmzbuf);
1397                 }
1398
1399                 if (irsp->ulpBdeCount > 1) {
1400                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1401                                         irsp->unsli3.sli3Words[3]);
1402                         lpfc_in_buf_free(phba, dmzbuf);
1403                 }
1404
1405                 if (irsp->ulpBdeCount > 2) {
1406                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1407                                 irsp->unsli3.sli3Words[7]);
1408                         lpfc_in_buf_free(phba, dmzbuf);
1409                 }
1410
1411                 return 1;
1412         }
1413
1414         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1415                 if (irsp->ulpBdeCount != 0) {
1416                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
1417                                                 irsp->un.ulpWord[3]);
1418                         if (!saveq->context2)
1419                                 lpfc_printf_log(phba,
1420                                         KERN_ERR,
1421                                         LOG_SLI,
1422                                         "0341 Ring %d Cannot find buffer for "
1423                                         "an unsolicited iocb. tag 0x%x\n",
1424                                         pring->ringno,
1425                                         irsp->un.ulpWord[3]);
1426                 }
1427                 if (irsp->ulpBdeCount == 2) {
1428                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
1429                                                 irsp->unsli3.sli3Words[7]);
1430                         if (!saveq->context3)
1431                                 lpfc_printf_log(phba,
1432                                         KERN_ERR,
1433                                         LOG_SLI,
1434                                         "0342 Ring %d Cannot find buffer for an"
1435                                         " unsolicited iocb. tag 0x%x\n",
1436                                         pring->ringno,
1437                                         irsp->unsli3.sli3Words[7]);
1438                 }
1439                 list_for_each_entry(iocbq, &saveq->list, list) {
1440                         irsp = &(iocbq->iocb);
1441                         if (irsp->ulpBdeCount != 0) {
1442                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
1443                                                         irsp->un.ulpWord[3]);
1444                                 if (!iocbq->context2)
1445                                         lpfc_printf_log(phba,
1446                                                 KERN_ERR,
1447                                                 LOG_SLI,
1448                                                 "0343 Ring %d Cannot find "
1449                                                 "buffer for an unsolicited iocb"
1450                                                 ". tag 0x%x\n", pring->ringno,
1451                                                 irsp->un.ulpWord[3]);
1452                         }
1453                         if (irsp->ulpBdeCount == 2) {
1454                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
1455                                                 irsp->unsli3.sli3Words[7]);
1456                                 if (!iocbq->context3)
1457                                         lpfc_printf_log(phba,
1458                                                 KERN_ERR,
1459                                                 LOG_SLI,
1460                                                 "0344 Ring %d Cannot find "
1461                                                 "buffer for an unsolicited "
1462                                                 "iocb. tag 0x%x\n",
1463                                                 pring->ringno,
1464                                                 irsp->unsli3.sli3Words[7]);
1465                         }
1466                 }
1467         }
1468         if (irsp->ulpBdeCount != 0 &&
1469             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
1470              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
1471                 int found = 0;
1472
1473                 /* search continue save q for same XRI */
1474                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
1475                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
1476                                 list_add_tail(&saveq->list, &iocbq->list);
1477                                 found = 1;
1478                                 break;
1479                         }
1480                 }
1481                 if (!found)
1482                         list_add_tail(&saveq->clist,
1483                                       &pring->iocb_continue_saveq);
1484                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
1485                         list_del_init(&iocbq->clist);
1486                         saveq = iocbq;
1487                         irsp = &(saveq->iocb);
1488                 } else
1489                         return 0;
1490         }
1491         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
1492             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
1493             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
1494                 Rctl = FC_ELS_REQ;
1495                 Type = FC_ELS_DATA;
1496         } else {
1497                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
1498                 Rctl = w5p->hcsw.Rctl;
1499                 Type = w5p->hcsw.Type;
1500
1501                 /* Firmware Workaround */
1502                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
1503                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
1504                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
1505                         Rctl = FC_ELS_REQ;
1506                         Type = FC_ELS_DATA;
1507                         w5p->hcsw.Rctl = Rctl;
1508                         w5p->hcsw.Type = Type;
1509                 }
1510         }
1511
1512         /* unSolicited Responses */
1513         if (pring->prt[0].profile) {
1514                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1515                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1516                                                                         saveq);
1517                 match = 1;
1518         } else {
1519                 /* We must search, based on rctl / type
1520                    for the right routine */
1521                 for (i = 0; i < pring->num_mask; i++) {
1522                         if ((pring->prt[i].rctl == Rctl)
1523                             && (pring->prt[i].type == Type)) {
1524                                 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1525                                         (pring->prt[i].lpfc_sli_rcv_unsol_event)
1526                                                         (phba, pring, saveq);
1527                                 match = 1;
1528                                 break;
1529                         }
1530                 }
1531         }
1532         if (match == 0) {
1533                 /* Unexpected Rctl / Type received */
1534                 /* Ring <ringno> handler: unexpected
1535                    Rctl <Rctl> Type <Type> received */
1536                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1537                                 "0313 Ring %d handler: unexpected Rctl x%x "
1538                                 "Type x%x received\n",
1539                                 pring->ringno, Rctl, Type);
1540         }
1541         return 1;
1542 }
1543
1544 /**
1545  * lpfc_sli_iocbq_lookup: Find command iocb for the given response iocb.
1546  * @phba: Pointer to HBA context object.
1547  * @pring: Pointer to driver SLI ring object.
1548  * @prspiocb: Pointer to response iocb object.
1549  *
1550  * This function looks up the iocb_lookup table to get the command iocb
1551  * corresponding to the given response iocb using the iotag of the
1552  * response iocb. This function is called with the hbalock held.
1553  * This function returns the command iocb object if it finds the command
1554  * iocb else returns NULL.
1555  **/
1556 static struct lpfc_iocbq *
1557 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
1558                       struct lpfc_sli_ring *pring,
1559                       struct lpfc_iocbq *prspiocb)
1560 {
1561         struct lpfc_iocbq *cmd_iocb = NULL;
1562         uint16_t iotag;
1563
1564         iotag = prspiocb->iocb.ulpIoTag;
1565
1566         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
1567                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
1568                 list_del_init(&cmd_iocb->list);
1569                 pring->txcmplq_cnt--;
1570                 return cmd_iocb;
1571         }
1572
1573         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1574                         "0317 iotag x%x is out off "
1575                         "range: max iotag x%x wd0 x%x\n",
1576                         iotag, phba->sli.last_iotag,
1577                         *(((uint32_t *) &prspiocb->iocb) + 7));
1578         return NULL;
1579 }
1580
1581 /**
1582  * lpfc_sli_process_sol_iocb: process solicited iocb completion.
1583  * @phba: Pointer to HBA context object.
1584  * @pring: Pointer to driver SLI ring object.
1585  * @saveq: Pointer to the response iocb to be processed.
1586  *
1587  * This function is called by the ring event handler for non-fcp
1588  * rings when there is a new response iocb in the response ring.
1589  * The caller is not required to hold any locks. This function
1590  * gets the command iocb associated with the response iocb and
1591  * calls the completion handler for the command iocb. If there
1592  * is no completion handler, the function will free the resources
1593  * associated with command iocb. If the response iocb is for
1594  * an already aborted command iocb, the status of the completion
1595  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
1596  * This function always returns 1.
1597  **/
1598 static int
1599 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1600                           struct lpfc_iocbq *saveq)
1601 {
1602         struct lpfc_iocbq *cmdiocbp;
1603         int rc = 1;
1604         unsigned long iflag;
1605
1606         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
1607         spin_lock_irqsave(&phba->hbalock, iflag);
1608         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
1609         spin_unlock_irqrestore(&phba->hbalock, iflag);
1610
1611         if (cmdiocbp) {
1612                 if (cmdiocbp->iocb_cmpl) {
1613                         /*
1614                          * Post all ELS completions to the worker thread.
1615                          * All other are passed to the completion callback.
1616                          */
1617                         if (pring->ringno == LPFC_ELS_RING) {
1618                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
1619                                         cmdiocbp->iocb_flag &=
1620                                                 ~LPFC_DRIVER_ABORTED;
1621                                         saveq->iocb.ulpStatus =
1622                                                 IOSTAT_LOCAL_REJECT;
1623                                         saveq->iocb.un.ulpWord[4] =
1624                                                 IOERR_SLI_ABORTED;
1625
1626                                         /* Firmware could still be in progress
1627                                          * of DMAing payload, so don't free data
1628                                          * buffer till after a hbeat.
1629                                          */
1630                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
1631                                 }
1632                         }
1633                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
1634                 } else
1635                         lpfc_sli_release_iocbq(phba, cmdiocbp);
1636         } else {
1637                 /*
1638                  * Unknown initiating command based on the response iotag.
1639                  * This could be the case on the ELS ring because of
1640                  * lpfc_els_abort().
1641                  */
1642                 if (pring->ringno != LPFC_ELS_RING) {
1643                         /*
1644                          * Ring <ringno> handler: unexpected completion IoTag
1645                          * <IoTag>
1646                          */
1647                         lpfc_printf_vlog(cmdiocbp->vport, KERN_WARNING, LOG_SLI,
1648                                          "0322 Ring %d handler: "
1649                                          "unexpected completion IoTag x%x "
1650                                          "Data: x%x x%x x%x x%x\n",
1651                                          pring->ringno,
1652                                          saveq->iocb.ulpIoTag,
1653                                          saveq->iocb.ulpStatus,
1654                                          saveq->iocb.un.ulpWord[4],
1655                                          saveq->iocb.ulpCommand,
1656                                          saveq->iocb.ulpContext);
1657                 }
1658         }
1659
1660         return rc;
1661 }
1662
1663 /**
1664  * lpfc_sli_rsp_pointers_error: Response ring pointer error handler.
1665  * @phba: Pointer to HBA context object.
1666  * @pring: Pointer to driver SLI ring object.
1667  *
1668  * This function is called from the iocb ring event handlers when
1669  * put pointer is ahead of the get pointer for a ring. This function signal
1670  * an error attention condition to the worker thread and the worker
1671  * thread will transition the HBA to offline state.
1672  **/
1673 static void
1674 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1675 {
1676         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1677         /*
1678          * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
1679          * rsp ring <portRspMax>
1680          */
1681         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1682                         "0312 Ring %d handler: portRspPut %d "
1683                         "is bigger then rsp ring %d\n",
1684                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
1685                         pring->numRiocb);
1686
1687         phba->link_state = LPFC_HBA_ERROR;
1688
1689         /*
1690          * All error attention handlers are posted to
1691          * worker thread
1692          */
1693         phba->work_ha |= HA_ERATT;
1694         phba->work_hs = HS_FFER3;
1695
1696         lpfc_worker_wake_up(phba);
1697
1698         return;
1699 }
1700
1701 /**
1702  * lpfc_sli_poll_fcp_ring: Handle FCP ring completion in polling mode.
1703  * @phba: Pointer to HBA context object.
1704  *
1705  * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
1706  * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
1707  * is enabled.
1708  *
1709  * The caller does not hold any lock.
1710  * The function processes each response iocb in the response ring until it
1711  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1712  * LE bit set. The function will call the completion handler of the command iocb
1713  * if the response iocb indicates a completion for a command iocb or it is
1714  * an abort completion.
1715  **/
1716 void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
1717 {
1718         struct lpfc_sli      *psli  = &phba->sli;
1719         struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
1720         IOCB_t *irsp = NULL;
1721         IOCB_t *entry = NULL;
1722         struct lpfc_iocbq *cmdiocbq = NULL;
1723         struct lpfc_iocbq rspiocbq;
1724         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1725         uint32_t status;
1726         uint32_t portRspPut, portRspMax;
1727         int type;
1728         uint32_t rsp_cmpl = 0;
1729         uint32_t ha_copy;
1730         unsigned long iflags;
1731
1732         pring->stats.iocb_event++;
1733
1734         /*
1735          * The next available response entry should never exceed the maximum
1736          * entries.  If it does, treat it as an adapter hardware error.
1737          */
1738         portRspMax = pring->numRiocb;
1739         portRspPut = le32_to_cpu(pgp->rspPutInx);
1740         if (unlikely(portRspPut >= portRspMax)) {
1741                 lpfc_sli_rsp_pointers_error(phba, pring);
1742                 return;
1743         }
1744
1745         rmb();
1746         while (pring->rspidx != portRspPut) {
1747                 entry = lpfc_resp_iocb(phba, pring);
1748                 if (++pring->rspidx >= portRspMax)
1749                         pring->rspidx = 0;
1750
1751                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1752                                       (uint32_t *) &rspiocbq.iocb,
1753                                       phba->iocb_rsp_size);
1754                 irsp = &rspiocbq.iocb;
1755                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1756                 pring->stats.iocb_rsp++;
1757                 rsp_cmpl++;
1758
1759                 if (unlikely(irsp->ulpStatus)) {
1760                         /* Rsp ring <ringno> error: IOCB */
1761                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1762                                         "0326 Rsp Ring %d error: IOCB Data: "
1763                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1764                                         pring->ringno,
1765                                         irsp->un.ulpWord[0],
1766                                         irsp->un.ulpWord[1],
1767                                         irsp->un.ulpWord[2],
1768                                         irsp->un.ulpWord[3],
1769                                         irsp->un.ulpWord[4],
1770                                         irsp->un.ulpWord[5],
1771                                         *(uint32_t *)&irsp->un1,
1772                                         *((uint32_t *)&irsp->un1 + 1));
1773                 }
1774
1775                 switch (type) {
1776                 case LPFC_ABORT_IOCB:
1777                 case LPFC_SOL_IOCB:
1778                         /*
1779                          * Idle exchange closed via ABTS from port.  No iocb
1780                          * resources need to be recovered.
1781                          */
1782                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1783                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1784                                                 "0314 IOCB cmd 0x%x "
1785                                                 "processed. Skipping "
1786                                                 "completion",
1787                                                 irsp->ulpCommand);
1788                                 break;
1789                         }
1790
1791                         spin_lock_irqsave(&phba->hbalock, iflags);
1792                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1793                                                          &rspiocbq);
1794                         spin_unlock_irqrestore(&phba->hbalock, iflags);
1795                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1796                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1797                                                       &rspiocbq);
1798                         }
1799                         break;
1800                 default:
1801                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
1802                                 char adaptermsg[LPFC_MAX_ADPTMSG];
1803                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
1804                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
1805                                        MAX_MSG_DATA);
1806                                 dev_warn(&((phba->pcidev)->dev),
1807                                          "lpfc%d: %s\n",
1808                                          phba->brd_no, adaptermsg);
1809                         } else {
1810                                 /* Unknown IOCB command */
1811                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1812                                                 "0321 Unknown IOCB command "
1813                                                 "Data: x%x, x%x x%x x%x x%x\n",
1814                                                 type, irsp->ulpCommand,
1815                                                 irsp->ulpStatus,
1816                                                 irsp->ulpIoTag,
1817                                                 irsp->ulpContext);
1818                         }
1819                         break;
1820                 }
1821
1822                 /*
1823                  * The response IOCB has been processed.  Update the ring
1824                  * pointer in SLIM.  If the port response put pointer has not
1825                  * been updated, sync the pgp->rspPutInx and fetch the new port
1826                  * response put pointer.
1827                  */
1828                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
1829
1830                 if (pring->rspidx == portRspPut)
1831                         portRspPut = le32_to_cpu(pgp->rspPutInx);
1832         }
1833
1834         ha_copy = readl(phba->HAregaddr);
1835         ha_copy >>= (LPFC_FCP_RING * 4);
1836
1837         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
1838                 spin_lock_irqsave(&phba->hbalock, iflags);
1839                 pring->stats.iocb_rsp_full++;
1840                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
1841                 writel(status, phba->CAregaddr);
1842                 readl(phba->CAregaddr);
1843                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1844         }
1845         if ((ha_copy & HA_R0CE_RSP) &&
1846             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
1847                 spin_lock_irqsave(&phba->hbalock, iflags);
1848                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
1849                 pring->stats.iocb_cmd_empty++;
1850
1851                 /* Force update of the local copy of cmdGetInx */
1852                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1853                 lpfc_sli_resume_iocb(phba, pring);
1854
1855                 if ((pring->lpfc_sli_cmd_available))
1856                         (pring->lpfc_sli_cmd_available) (phba, pring);
1857
1858                 spin_unlock_irqrestore(&phba->hbalock, iflags);
1859         }
1860
1861         return;
1862 }
1863
1864 /**
1865  * lpfc_sli_handle_fast_ring_event: Handle ring events on FCP ring.
1866  * @phba: Pointer to HBA context object.
1867  * @pring: Pointer to driver SLI ring object.
1868  * @mask: Host attention register mask for this ring.
1869  *
1870  * This function is called from the interrupt context when there is a ring
1871  * event for the fcp ring. The caller does not hold any lock.
1872  * The function processes each response iocb in the response ring until it
1873  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
1874  * LE bit set. The function will call the completion handler of the command iocb
1875  * if the response iocb indicates a completion for a command iocb or it is
1876  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
1877  * function if this is an unsolicited iocb.
1878  * This routine presumes LPFC_FCP_RING handling and doesn't bother
1879  * to check it explicitly. This function always returns 1.
1880  **/
1881 static int
1882 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
1883                                 struct lpfc_sli_ring *pring, uint32_t mask)
1884 {
1885         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1886         IOCB_t *irsp = NULL;
1887         IOCB_t *entry = NULL;
1888         struct lpfc_iocbq *cmdiocbq = NULL;
1889         struct lpfc_iocbq rspiocbq;
1890         uint32_t status;
1891         uint32_t portRspPut, portRspMax;
1892         int rc = 1;
1893         lpfc_iocb_type type;
1894         unsigned long iflag;
1895         uint32_t rsp_cmpl = 0;
1896
1897         spin_lock_irqsave(&phba->hbalock, iflag);
1898         pring->stats.iocb_event++;
1899
1900         /*
1901          * The next available response entry should never exceed the maximum
1902          * entries.  If it does, treat it as an adapter hardware error.
1903          */
1904         portRspMax = pring->numRiocb;
1905         portRspPut = le32_to_cpu(pgp->rspPutInx);
1906         if (unlikely(portRspPut >= portRspMax)) {
1907                 lpfc_sli_rsp_pointers_error(phba, pring);
1908                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1909                 return 1;
1910         }
1911
1912         rmb();
1913         while (pring->rspidx != portRspPut) {
1914                 /*
1915                  * Fetch an entry off the ring and copy it into a local data
1916                  * structure.  The copy involves a byte-swap since the
1917                  * network byte order and pci byte orders are different.
1918                  */
1919                 entry = lpfc_resp_iocb(phba, pring);
1920                 phba->last_completion_time = jiffies;
1921
1922                 if (++pring->rspidx >= portRspMax)
1923                         pring->rspidx = 0;
1924
1925                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
1926                                       (uint32_t *) &rspiocbq.iocb,
1927                                       phba->iocb_rsp_size);
1928                 INIT_LIST_HEAD(&(rspiocbq.list));
1929                 irsp = &rspiocbq.iocb;
1930
1931                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
1932                 pring->stats.iocb_rsp++;
1933                 rsp_cmpl++;
1934
1935                 if (unlikely(irsp->ulpStatus)) {
1936                         /*
1937                          * If resource errors reported from HBA, reduce
1938                          * queuedepths of the SCSI device.
1939                          */
1940                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
1941                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
1942                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
1943                                 lpfc_adjust_queue_depth(phba);
1944                                 spin_lock_irqsave(&phba->hbalock, iflag);
1945                         }
1946
1947                         /* Rsp ring <ringno> error: IOCB */
1948                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1949                                         "0336 Rsp Ring %d error: IOCB Data: "
1950                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
1951                                         pring->ringno,
1952                                         irsp->un.ulpWord[0],
1953                                         irsp->un.ulpWord[1],
1954                                         irsp->un.ulpWord[2],
1955                                         irsp->un.ulpWord[3],
1956                                         irsp->un.ulpWord[4],
1957                                         irsp->un.ulpWord[5],
1958                                         *(uint32_t *)&irsp->un1,
1959                                         *((uint32_t *)&irsp->un1 + 1));
1960                 }
1961
1962                 switch (type) {
1963                 case LPFC_ABORT_IOCB:
1964                 case LPFC_SOL_IOCB:
1965                         /*
1966                          * Idle exchange closed via ABTS from port.  No iocb
1967                          * resources need to be recovered.
1968                          */
1969                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
1970                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
1971                                                 "0333 IOCB cmd 0x%x"
1972                                                 " processed. Skipping"
1973                                                 " completion\n",
1974                                                 irsp->ulpCommand);
1975                                 break;
1976                         }
1977
1978                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
1979                                                          &rspiocbq);
1980                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
1981                                 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1982                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1983                                                               &rspiocbq);
1984                                 } else {
1985                                         spin_unlock_irqrestore(&phba->hbalock,
1986                                                                iflag);
1987                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
1988                                                               &rspiocbq);
1989                                         spin_lock_irqsave(&phba->hbalock,
1990                                                           iflag);
1991                                 }
1992                         }
1993                         break;
1994                 case LPFC_UNSOL_IOCB:
1995                         spin_unlock_irqrestore(&phba->hbalock, iflag);
1996                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
1997                         spin_lock_irqsave(&phba->hbalock, iflag);
1998                         break;
1999                 default:
2000                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2001                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2002                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2003                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2004                                        MAX_MSG_DATA);
2005                                 dev_warn(&((phba->pcidev)->dev),
2006                                          "lpfc%d: %s\n",
2007                                          phba->brd_no, adaptermsg);
2008                         } else {
2009                                 /* Unknown IOCB command */
2010                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2011                                                 "0334 Unknown IOCB command "
2012                                                 "Data: x%x, x%x x%x x%x x%x\n",
2013                                                 type, irsp->ulpCommand,
2014                                                 irsp->ulpStatus,
2015                                                 irsp->ulpIoTag,
2016                                                 irsp->ulpContext);
2017                         }
2018                         break;
2019                 }
2020
2021                 /*
2022                  * The response IOCB has been processed.  Update the ring
2023                  * pointer in SLIM.  If the port response put pointer has not
2024                  * been updated, sync the pgp->rspPutInx and fetch the new port
2025                  * response put pointer.
2026                  */
2027                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2028
2029                 if (pring->rspidx == portRspPut)
2030                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2031         }
2032
2033         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2034                 pring->stats.iocb_rsp_full++;
2035                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2036                 writel(status, phba->CAregaddr);
2037                 readl(phba->CAregaddr);
2038         }
2039         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2040                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2041                 pring->stats.iocb_cmd_empty++;
2042
2043                 /* Force update of the local copy of cmdGetInx */
2044                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2045                 lpfc_sli_resume_iocb(phba, pring);
2046
2047                 if ((pring->lpfc_sli_cmd_available))
2048                         (pring->lpfc_sli_cmd_available) (phba, pring);
2049
2050         }
2051
2052         spin_unlock_irqrestore(&phba->hbalock, iflag);
2053         return rc;
2054 }
2055
2056 /**
2057  * lpfc_sli_handle_slow_ring_event: Handle ring events for non-FCP rings.
2058  * @phba: Pointer to HBA context object.
2059  * @pring: Pointer to driver SLI ring object.
2060  * @mask: Host attention register mask for this ring.
2061  *
2062  * This function is called from the worker thread when there is a ring
2063  * event for non-fcp rings. The caller does not hold any lock .
2064  * The function processes each response iocb in the response ring until it
2065  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2066  * LE bit set. The function will call lpfc_sli_process_sol_iocb function if the
2067  * response iocb indicates a completion of a command iocb. The function
2068  * will call lpfc_sli_process_unsol_iocb function if this is an unsolicited
2069  * iocb. The function frees the resources or calls the completion handler if
2070  * this iocb is an abort completion. The function returns 0 when the allocated
2071  * iocbs are not freed, otherwise returns 1.
2072  **/
2073 int
2074 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2075                                 struct lpfc_sli_ring *pring, uint32_t mask)
2076 {
2077         struct lpfc_pgp *pgp;
2078         IOCB_t *entry;
2079         IOCB_t *irsp = NULL;
2080         struct lpfc_iocbq *rspiocbp = NULL;
2081         struct lpfc_iocbq *next_iocb;
2082         struct lpfc_iocbq *cmdiocbp;
2083         struct lpfc_iocbq *saveq;
2084         uint8_t iocb_cmd_type;
2085         lpfc_iocb_type type;
2086         uint32_t status, free_saveq;
2087         uint32_t portRspPut, portRspMax;
2088         int rc = 1;
2089         unsigned long iflag;
2090
2091         pgp = &phba->port_gp[pring->ringno];
2092         spin_lock_irqsave(&phba->hbalock, iflag);
2093         pring->stats.iocb_event++;
2094
2095         /*
2096          * The next available response entry should never exceed the maximum
2097          * entries.  If it does, treat it as an adapter hardware error.
2098          */
2099         portRspMax = pring->numRiocb;
2100         portRspPut = le32_to_cpu(pgp->rspPutInx);
2101         if (portRspPut >= portRspMax) {
2102                 /*
2103                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger then
2104                  * rsp ring <portRspMax>
2105                  */
2106                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2107                                 "0303 Ring %d handler: portRspPut %d "
2108                                 "is bigger then rsp ring %d\n",
2109                                 pring->ringno, portRspPut, portRspMax);
2110
2111                 phba->link_state = LPFC_HBA_ERROR;
2112                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2113
2114                 phba->work_hs = HS_FFER3;
2115                 lpfc_handle_eratt(phba);
2116
2117                 return 1;
2118         }
2119
2120         rmb();
2121         while (pring->rspidx != portRspPut) {
2122                 /*
2123                  * Build a completion list and call the appropriate handler.
2124                  * The process is to get the next available response iocb, get
2125                  * a free iocb from the list, copy the response data into the
2126                  * free iocb, insert to the continuation list, and update the
2127                  * next response index to slim.  This process makes response
2128                  * iocb's in the ring available to DMA as fast as possible but
2129                  * pays a penalty for a copy operation.  Since the iocb is
2130                  * only 32 bytes, this penalty is considered small relative to
2131                  * the PCI reads for register values and a slim write.  When
2132                  * the ulpLe field is set, the entire Command has been
2133                  * received.
2134                  */
2135                 entry = lpfc_resp_iocb(phba, pring);
2136
2137                 phba->last_completion_time = jiffies;
2138                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2139                 if (rspiocbp == NULL) {
2140                         printk(KERN_ERR "%s: out of buffers! Failing "
2141                                "completion.\n", __func__);
2142                         break;
2143                 }
2144
2145                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2146                                       phba->iocb_rsp_size);
2147                 irsp = &rspiocbp->iocb;
2148
2149                 if (++pring->rspidx >= portRspMax)
2150                         pring->rspidx = 0;
2151
2152                 if (pring->ringno == LPFC_ELS_RING) {
2153                         lpfc_debugfs_slow_ring_trc(phba,
2154                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2155                                 *(((uint32_t *) irsp) + 4),
2156                                 *(((uint32_t *) irsp) + 6),
2157                                 *(((uint32_t *) irsp) + 7));
2158                 }
2159
2160                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2161
2162                 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2163
2164                 pring->iocb_continueq_cnt++;
2165                 if (irsp->ulpLe) {
2166                         /*
2167                          * By default, the driver expects to free all resources
2168                          * associated with this iocb completion.
2169                          */
2170                         free_saveq = 1;
2171                         saveq = list_get_first(&pring->iocb_continueq,
2172                                                struct lpfc_iocbq, list);
2173                         irsp = &(saveq->iocb);
2174                         list_del_init(&pring->iocb_continueq);
2175                         pring->iocb_continueq_cnt = 0;
2176
2177                         pring->stats.iocb_rsp++;
2178
2179                         /*
2180                          * If resource errors reported from HBA, reduce
2181                          * queuedepths of the SCSI device.
2182                          */
2183                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2184                              (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2185                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2186                                 lpfc_adjust_queue_depth(phba);
2187                                 spin_lock_irqsave(&phba->hbalock, iflag);
2188                         }
2189
2190                         if (irsp->ulpStatus) {
2191                                 /* Rsp ring <ringno> error: IOCB */
2192                                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2193                                                 "0328 Rsp Ring %d error: "
2194                                                 "IOCB Data: "
2195                                                 "x%x x%x x%x x%x "
2196                                                 "x%x x%x x%x x%x "
2197                                                 "x%x x%x x%x x%x "
2198                                                 "x%x x%x x%x x%x\n",
2199                                                 pring->ringno,
2200                                                 irsp->un.ulpWord[0],
2201                                                 irsp->un.ulpWord[1],
2202                                                 irsp->un.ulpWord[2],
2203                                                 irsp->un.ulpWord[3],
2204                                                 irsp->un.ulpWord[4],
2205                                                 irsp->un.ulpWord[5],
2206                                                 *(((uint32_t *) irsp) + 6),
2207                                                 *(((uint32_t *) irsp) + 7),
2208                                                 *(((uint32_t *) irsp) + 8),
2209                                                 *(((uint32_t *) irsp) + 9),
2210                                                 *(((uint32_t *) irsp) + 10),
2211                                                 *(((uint32_t *) irsp) + 11),
2212                                                 *(((uint32_t *) irsp) + 12),
2213                                                 *(((uint32_t *) irsp) + 13),
2214                                                 *(((uint32_t *) irsp) + 14),
2215                                                 *(((uint32_t *) irsp) + 15));
2216                         }
2217
2218                         /*
2219                          * Fetch the IOCB command type and call the correct
2220                          * completion routine.  Solicited and Unsolicited
2221                          * IOCBs on the ELS ring get freed back to the
2222                          * lpfc_iocb_list by the discovery kernel thread.
2223                          */
2224                         iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2225                         type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2226                         if (type == LPFC_SOL_IOCB) {
2227                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2228                                 rc = lpfc_sli_process_sol_iocb(phba, pring,
2229                                                                saveq);
2230                                 spin_lock_irqsave(&phba->hbalock, iflag);
2231                         } else if (type == LPFC_UNSOL_IOCB) {
2232                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2233                                 rc = lpfc_sli_process_unsol_iocb(phba, pring,
2234                                                                  saveq);
2235                                 spin_lock_irqsave(&phba->hbalock, iflag);
2236                                 if (!rc)
2237                                         free_saveq = 0;
2238                         } else if (type == LPFC_ABORT_IOCB) {
2239                                 if ((irsp->ulpCommand != CMD_XRI_ABORTED_CX) &&
2240                                     ((cmdiocbp =
2241                                       lpfc_sli_iocbq_lookup(phba, pring,
2242                                                             saveq)))) {
2243                                         /* Call the specified completion
2244                                            routine */
2245                                         if (cmdiocbp->iocb_cmpl) {
2246                                                 spin_unlock_irqrestore(
2247                                                        &phba->hbalock,
2248                                                        iflag);
2249                                                 (cmdiocbp->iocb_cmpl) (phba,
2250                                                              cmdiocbp, saveq);
2251                                                 spin_lock_irqsave(
2252                                                           &phba->hbalock,
2253                                                           iflag);
2254                                         } else
2255                                                 __lpfc_sli_release_iocbq(phba,
2256                                                                       cmdiocbp);
2257                                 }
2258                         } else if (type == LPFC_UNKNOWN_IOCB) {
2259                                 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2260
2261                                         char adaptermsg[LPFC_MAX_ADPTMSG];
2262
2263                                         memset(adaptermsg, 0,
2264                                                LPFC_MAX_ADPTMSG);
2265                                         memcpy(&adaptermsg[0], (uint8_t *) irsp,
2266                                                MAX_MSG_DATA);
2267                                         dev_warn(&((phba->pcidev)->dev),
2268                                                  "lpfc%d: %s\n",
2269                                                  phba->brd_no, adaptermsg);
2270                                 } else {
2271                                         /* Unknown IOCB command */
2272                                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2273                                                         "0335 Unknown IOCB "
2274                                                         "command Data: x%x "
2275                                                         "x%x x%x x%x\n",
2276                                                         irsp->ulpCommand,
2277                                                         irsp->ulpStatus,
2278                                                         irsp->ulpIoTag,
2279                                                         irsp->ulpContext);
2280                                 }
2281                         }
2282
2283                         if (free_saveq) {
2284                                 list_for_each_entry_safe(rspiocbp, next_iocb,
2285                                                          &saveq->list, list) {
2286                                         list_del(&rspiocbp->list);
2287                                         __lpfc_sli_release_iocbq(phba,
2288                                                                  rspiocbp);
2289                                 }
2290                                 __lpfc_sli_release_iocbq(phba, saveq);
2291                         }
2292                         rspiocbp = NULL;
2293                 }
2294
2295                 /*
2296                  * If the port response put pointer has not been updated, sync
2297                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2298                  * response put pointer.
2299                  */
2300                 if (pring->rspidx == portRspPut) {
2301                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2302                 }
2303         } /* while (pring->rspidx != portRspPut) */
2304
2305         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2306                 /* At least one response entry has been freed */
2307                 pring->stats.iocb_rsp_full++;
2308                 /* SET RxRE_RSP in Chip Att register */
2309                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2310                 writel(status, phba->CAregaddr);
2311                 readl(phba->CAregaddr); /* flush */
2312         }
2313         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2314                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2315                 pring->stats.iocb_cmd_empty++;
2316
2317                 /* Force update of the local copy of cmdGetInx */
2318                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2319                 lpfc_sli_resume_iocb(phba, pring);
2320
2321                 if ((pring->lpfc_sli_cmd_available))
2322                         (pring->lpfc_sli_cmd_available) (phba, pring);
2323
2324         }
2325
2326         spin_unlock_irqrestore(&phba->hbalock, iflag);
2327         return rc;
2328 }
2329
2330 /**
2331  * lpfc_sli_abort_iocb_ring: Abort all iocbs in the ring.
2332  * @phba: Pointer to HBA context object.
2333  * @pring: Pointer to driver SLI ring object.
2334  *
2335  * This function aborts all iocbs in the given ring and frees all the iocb
2336  * objects in txq. This function issues an abort iocb for all the iocb commands
2337  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2338  * the return of this function. The caller is not required to hold any locks.
2339  **/
2340 void
2341 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2342 {
2343         LIST_HEAD(completions);
2344         struct lpfc_iocbq *iocb, *next_iocb;
2345         IOCB_t *cmd = NULL;
2346
2347         if (pring->ringno == LPFC_ELS_RING) {
2348                 lpfc_fabric_abort_hba(phba);
2349         }
2350
2351         /* Error everything on txq and txcmplq
2352          * First do the txq.
2353          */
2354         spin_lock_irq(&phba->hbalock);
2355         list_splice_init(&pring->txq, &completions);
2356         pring->txq_cnt = 0;
2357
2358         /* Next issue ABTS for everything on the txcmplq */
2359         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
2360                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
2361
2362         spin_unlock_irq(&phba->hbalock);
2363
2364         while (!list_empty(&completions)) {
2365                 iocb = list_get_first(&completions, struct lpfc_iocbq, list);
2366                 cmd = &iocb->iocb;
2367                 list_del_init(&iocb->list);
2368
2369                 if (!iocb->iocb_cmpl)
2370                         lpfc_sli_release_iocbq(phba, iocb);
2371                 else {
2372                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
2373                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
2374                         (iocb->iocb_cmpl) (phba, iocb, iocb);
2375                 }
2376         }
2377 }
2378
2379 /**
2380  * lpfc_sli_brdready: Check for host status bits.
2381  * @phba: Pointer to HBA context object.
2382  * @mask: Bit mask to be checked.
2383  *
2384  * This function reads the host status register and compares
2385  * with the provided bit mask to check if HBA completed
2386  * the restart. This function will wait in a loop for the
2387  * HBA to complete restart. If the HBA does not restart within
2388  * 15 iterations, the function will reset the HBA again. The
2389  * function returns 1 when HBA fail to restart otherwise returns
2390  * zero.
2391  **/
2392 int
2393 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
2394 {
2395         uint32_t status;
2396         int i = 0;
2397         int retval = 0;
2398
2399         /* Read the HBA Host Status Register */
2400         status = readl(phba->HSregaddr);
2401
2402         /*
2403          * Check status register every 100ms for 5 retries, then every
2404          * 500ms for 5, then every 2.5 sec for 5, then reset board and
2405          * every 2.5 sec for 4.
2406          * Break our of the loop if errors occurred during init.
2407          */
2408         while (((status & mask) != mask) &&
2409                !(status & HS_FFERM) &&
2410                i++ < 20) {
2411
2412                 if (i <= 5)
2413                         msleep(10);
2414                 else if (i <= 10)
2415                         msleep(500);
2416                 else
2417                         msleep(2500);
2418
2419                 if (i == 15) {
2420                                 /* Do post */
2421                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2422                         lpfc_sli_brdrestart(phba);
2423                 }
2424                 /* Read the HBA Host Status Register */
2425                 status = readl(phba->HSregaddr);
2426         }
2427
2428         /* Check to see if any errors occurred during init */
2429         if ((status & HS_FFERM) || (i >= 20)) {
2430                 phba->link_state = LPFC_HBA_ERROR;
2431                 retval = 1;
2432         }
2433
2434         return retval;
2435 }
2436
2437 #define BARRIER_TEST_PATTERN (0xdeadbeef)
2438
2439 /**
2440  * lpfc_reset_barrier: Make HBA ready for HBA reset.
2441  * @phba: Pointer to HBA context object.
2442  *
2443  * This function is called before resetting an HBA. This
2444  * function requests HBA to quiesce DMAs before a reset.
2445  **/
2446 void lpfc_reset_barrier(struct lpfc_hba *phba)
2447 {
2448         uint32_t __iomem *resp_buf;
2449         uint32_t __iomem *mbox_buf;
2450         volatile uint32_t mbox;
2451         uint32_t hc_copy;
2452         int  i;
2453         uint8_t hdrtype;
2454
2455         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
2456         if (hdrtype != 0x80 ||
2457             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
2458              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
2459                 return;
2460
2461         /*
2462          * Tell the other part of the chip to suspend temporarily all
2463          * its DMA activity.
2464          */
2465         resp_buf = phba->MBslimaddr;
2466
2467         /* Disable the error attention */
2468         hc_copy = readl(phba->HCregaddr);
2469         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
2470         readl(phba->HCregaddr); /* flush */
2471         phba->link_flag |= LS_IGNORE_ERATT;
2472
2473         if (readl(phba->HAregaddr) & HA_ERATT) {
2474                 /* Clear Chip error bit */
2475                 writel(HA_ERATT, phba->HAregaddr);
2476                 phba->pport->stopped = 1;
2477         }
2478
2479         mbox = 0;
2480         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
2481         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
2482
2483         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
2484         mbox_buf = phba->MBslimaddr;
2485         writel(mbox, mbox_buf);
2486
2487         for (i = 0;
2488              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
2489                 mdelay(1);
2490
2491         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
2492                 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
2493                     phba->pport->stopped)
2494                         goto restore_hc;
2495                 else
2496                         goto clear_errat;
2497         }
2498
2499         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
2500         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
2501                 mdelay(1);
2502
2503 clear_errat:
2504
2505         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
2506                 mdelay(1);
2507
2508         if (readl(phba->HAregaddr) & HA_ERATT) {
2509                 writel(HA_ERATT, phba->HAregaddr);
2510                 phba->pport->stopped = 1;
2511         }
2512
2513 restore_hc:
2514         phba->link_flag &= ~LS_IGNORE_ERATT;
2515         writel(hc_copy, phba->HCregaddr);
2516         readl(phba->HCregaddr); /* flush */
2517 }
2518
2519 /**
2520  * lpfc_sli_brdkill: Issue a kill_board mailbox command.
2521  * @phba: Pointer to HBA context object.
2522  *
2523  * This function issues a kill_board mailbox command and waits for
2524  * the error attention interrupt. This function is called for stopping
2525  * the firmware processing. The caller is not required to hold any
2526  * locks. This function calls lpfc_hba_down_post function to free
2527  * any pending commands after the kill. The function will return 1 when it
2528  * fails to kill the board else will return 0.
2529  **/
2530 int
2531 lpfc_sli_brdkill(struct lpfc_hba *phba)
2532 {
2533         struct lpfc_sli *psli;
2534         LPFC_MBOXQ_t *pmb;
2535         uint32_t status;
2536         uint32_t ha_copy;
2537         int retval;
2538         int i = 0;
2539
2540         psli = &phba->sli;
2541
2542         /* Kill HBA */
2543         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2544                         "0329 Kill HBA Data: x%x x%x\n",
2545                         phba->pport->port_state, psli->sli_flag);
2546
2547         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2548         if (!pmb)
2549                 return 1;
2550
2551         /* Disable the error attention */
2552         spin_lock_irq(&phba->hbalock);
2553         status = readl(phba->HCregaddr);
2554         status &= ~HC_ERINT_ENA;
2555         writel(status, phba->HCregaddr);
2556         readl(phba->HCregaddr); /* flush */
2557         phba->link_flag |= LS_IGNORE_ERATT;
2558         spin_unlock_irq(&phba->hbalock);
2559
2560         lpfc_kill_board(phba, pmb);
2561         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2562         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2563
2564         if (retval != MBX_SUCCESS) {
2565                 if (retval != MBX_BUSY)
2566                         mempool_free(pmb, phba->mbox_mem_pool);
2567                 spin_lock_irq(&phba->hbalock);
2568                 phba->link_flag &= ~LS_IGNORE_ERATT;
2569                 spin_unlock_irq(&phba->hbalock);
2570                 return 1;
2571         }
2572
2573         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
2574
2575         mempool_free(pmb, phba->mbox_mem_pool);
2576
2577         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
2578          * attention every 100ms for 3 seconds. If we don't get ERATT after
2579          * 3 seconds we still set HBA_ERROR state because the status of the
2580          * board is now undefined.
2581          */
2582         ha_copy = readl(phba->HAregaddr);
2583
2584         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
2585                 mdelay(100);
2586                 ha_copy = readl(phba->HAregaddr);
2587         }
2588
2589         del_timer_sync(&psli->mbox_tmo);
2590         if (ha_copy & HA_ERATT) {
2591                 writel(HA_ERATT, phba->HAregaddr);
2592                 phba->pport->stopped = 1;
2593         }
2594         spin_lock_irq(&phba->hbalock);
2595         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2596         phba->link_flag &= ~LS_IGNORE_ERATT;
2597         spin_unlock_irq(&phba->hbalock);
2598
2599         psli->mbox_active = NULL;
2600         lpfc_hba_down_post(phba);
2601         phba->link_state = LPFC_HBA_ERROR;
2602
2603         return ha_copy & HA_ERATT ? 0 : 1;
2604 }
2605
2606 /**
2607  * lpfc_sli_brdreset: Reset the HBA.
2608  * @phba: Pointer to HBA context object.
2609  *
2610  * This function resets the HBA by writing HC_INITFF to the control
2611  * register. After the HBA resets, this function resets all the iocb ring
2612  * indices. This function disables PCI layer parity checking during
2613  * the reset.
2614  * This function returns 0 always.
2615  * The caller is not required to hold any locks.
2616  **/
2617 int
2618 lpfc_sli_brdreset(struct lpfc_hba *phba)
2619 {
2620         struct lpfc_sli *psli;
2621         struct lpfc_sli_ring *pring;
2622         uint16_t cfg_value;
2623         int i;
2624
2625         psli = &phba->sli;
2626
2627         /* Reset HBA */
2628         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2629                         "0325 Reset HBA Data: x%x x%x\n",
2630                         phba->pport->port_state, psli->sli_flag);
2631
2632         /* perform board reset */
2633         phba->fc_eventTag = 0;
2634         phba->pport->fc_myDID = 0;
2635         phba->pport->fc_prevDID = 0;
2636
2637         /* Turn off parity checking and serr during the physical reset */
2638         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
2639         pci_write_config_word(phba->pcidev, PCI_COMMAND,
2640                               (cfg_value &
2641                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
2642
2643         psli->sli_flag &= ~(LPFC_SLI2_ACTIVE | LPFC_PROCESS_LA);
2644         /* Now toggle INITFF bit in the Host Control Register */
2645         writel(HC_INITFF, phba->HCregaddr);
2646         mdelay(1);
2647         readl(phba->HCregaddr); /* flush */
2648         writel(0, phba->HCregaddr);
2649         readl(phba->HCregaddr); /* flush */
2650
2651         /* Restore PCI cmd register */
2652         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
2653
2654         /* Initialize relevant SLI info */
2655         for (i = 0; i < psli->num_rings; i++) {
2656                 pring = &psli->ring[i];
2657                 pring->flag = 0;
2658                 pring->rspidx = 0;
2659                 pring->next_cmdidx  = 0;
2660                 pring->local_getidx = 0;
2661                 pring->cmdidx = 0;
2662                 pring->missbufcnt = 0;
2663         }
2664
2665         phba->link_state = LPFC_WARM_START;
2666         return 0;
2667 }
2668
2669 /**
2670  * lpfc_sli_brdrestart: Restart the HBA.
2671  * @phba: Pointer to HBA context object.
2672  *
2673  * This function is called in the SLI initialization code path to
2674  * restart the HBA. The caller is not required to hold any lock.
2675  * This function writes MBX_RESTART mailbox command to the SLIM and
2676  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
2677  * function to free any pending commands. The function enables
2678  * POST only during the first initialization. The function returns zero.
2679  * The function does not guarantee completion of MBX_RESTART mailbox
2680  * command before the return of this function.
2681  **/
2682 int
2683 lpfc_sli_brdrestart(struct lpfc_hba *phba)
2684 {
2685         MAILBOX_t *mb;
2686         struct lpfc_sli *psli;
2687         uint16_t skip_post;
2688         volatile uint32_t word0;
2689         void __iomem *to_slim;
2690
2691         spin_lock_irq(&phba->hbalock);
2692
2693         psli = &phba->sli;
2694
2695         /* Restart HBA */
2696         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2697                         "0337 Restart HBA Data: x%x x%x\n",
2698                         phba->pport->port_state, psli->sli_flag);
2699
2700         word0 = 0;
2701         mb = (MAILBOX_t *) &word0;
2702         mb->mbxCommand = MBX_RESTART;
2703         mb->mbxHc = 1;
2704
2705         lpfc_reset_barrier(phba);
2706
2707         to_slim = phba->MBslimaddr;
2708         writel(*(uint32_t *) mb, to_slim);
2709         readl(to_slim); /* flush */
2710
2711         /* Only skip post after fc_ffinit is completed */
2712         if (phba->pport->port_state) {
2713                 skip_post = 1;
2714                 word0 = 1;      /* This is really setting up word1 */
2715         } else {
2716                 skip_post = 0;
2717                 word0 = 0;      /* This is really setting up word1 */
2718         }
2719         to_slim = phba->MBslimaddr + sizeof (uint32_t);
2720         writel(*(uint32_t *) mb, to_slim);
2721         readl(to_slim); /* flush */
2722
2723         lpfc_sli_brdreset(phba);
2724         phba->pport->stopped = 0;
2725         phba->link_state = LPFC_INIT_START;
2726
2727         spin_unlock_irq(&phba->hbalock);
2728
2729         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
2730         psli->stats_start = get_seconds();
2731
2732         if (skip_post)
2733                 mdelay(100);
2734         else
2735                 mdelay(2000);
2736
2737         lpfc_hba_down_post(phba);
2738
2739         return 0;
2740 }
2741
2742 /**
2743  * lpfc_sli_chipset_init: Wait for the restart of the HBA after a restart.
2744  * @phba: Pointer to HBA context object.
2745  *
2746  * This function is called after a HBA restart to wait for successful
2747  * restart of the HBA. Successful restart of the HBA is indicated by
2748  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
2749  * iteration, the function will restart the HBA again. The function returns
2750  * zero if HBA successfully restarted else returns negative error code.
2751  **/
2752 static int
2753 lpfc_sli_chipset_init(struct lpfc_hba *phba)
2754 {
2755         uint32_t status, i = 0;
2756
2757         /* Read the HBA Host Status Register */
2758         status = readl(phba->HSregaddr);
2759
2760         /* Check status register to see what current state is */
2761         i = 0;
2762         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
2763
2764                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
2765                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
2766                  * 4.
2767                  */
2768                 if (i++ >= 20) {
2769                         /* Adapter failed to init, timeout, status reg
2770                            <status> */
2771                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2772                                         "0436 Adapter failed to init, "
2773                                         "timeout, status reg x%x, "
2774                                         "FW Data: A8 x%x AC x%x\n", status,
2775                                         readl(phba->MBslimaddr + 0xa8),
2776                                         readl(phba->MBslimaddr + 0xac));
2777                         phba->link_state = LPFC_HBA_ERROR;
2778                         return -ETIMEDOUT;
2779                 }
2780
2781                 /* Check to see if any errors occurred during init */
2782                 if (status & HS_FFERM) {
2783                         /* ERROR: During chipset initialization */
2784                         /* Adapter failed to init, chipset, status reg
2785                            <status> */
2786                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2787                                         "0437 Adapter failed to init, "
2788                                         "chipset, status reg x%x, "
2789                                         "FW Data: A8 x%x AC x%x\n", status,
2790                                         readl(phba->MBslimaddr + 0xa8),
2791                                         readl(phba->MBslimaddr + 0xac));
2792                         phba->link_state = LPFC_HBA_ERROR;
2793                         return -EIO;
2794                 }
2795
2796                 if (i <= 5) {
2797                         msleep(10);
2798                 } else if (i <= 10) {
2799                         msleep(500);
2800                 } else {
2801                         msleep(2500);
2802                 }
2803
2804                 if (i == 15) {
2805                                 /* Do post */
2806                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2807                         lpfc_sli_brdrestart(phba);
2808                 }
2809                 /* Read the HBA Host Status Register */
2810                 status = readl(phba->HSregaddr);
2811         }
2812
2813         /* Check to see if any errors occurred during init */
2814         if (status & HS_FFERM) {
2815                 /* ERROR: During chipset initialization */
2816                 /* Adapter failed to init, chipset, status reg <status> */
2817                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2818                                 "0438 Adapter failed to init, chipset, "
2819                                 "status reg x%x, "
2820                                 "FW Data: A8 x%x AC x%x\n", status,
2821                                 readl(phba->MBslimaddr + 0xa8),
2822                                 readl(phba->MBslimaddr + 0xac));
2823                 phba->link_state = LPFC_HBA_ERROR;
2824                 return -EIO;
2825         }
2826
2827         /* Clear all interrupt enable conditions */
2828         writel(0, phba->HCregaddr);
2829         readl(phba->HCregaddr); /* flush */
2830
2831         /* setup host attn register */
2832         writel(0xffffffff, phba->HAregaddr);
2833         readl(phba->HAregaddr); /* flush */
2834         return 0;
2835 }
2836
2837 /**
2838  * lpfc_sli_hbq_count: Get the number of HBQs to be configured.
2839  *
2840  * This function calculates and returns the number of HBQs required to be
2841  * configured.
2842  **/
2843 int
2844 lpfc_sli_hbq_count(void)
2845 {
2846         return ARRAY_SIZE(lpfc_hbq_defs);
2847 }
2848
2849 /**
2850  * lpfc_sli_hbq_entry_count: Calculate total number of hbq entries.
2851  *
2852  * This function adds the number of hbq entries in every HBQ to get
2853  * the total number of hbq entries required for the HBA and returns
2854  * the total count.
2855  **/
2856 static int
2857 lpfc_sli_hbq_entry_count(void)
2858 {
2859         int  hbq_count = lpfc_sli_hbq_count();
2860         int  count = 0;
2861         int  i;
2862
2863         for (i = 0; i < hbq_count; ++i)
2864                 count += lpfc_hbq_defs[i]->entry_count;
2865         return count;
2866 }
2867
2868 /**
2869  * lpfc_sli_hbq_size: Calculate memory required for all hbq entries.
2870  *
2871  * This function calculates amount of memory required for all hbq entries
2872  * to be configured and returns the total memory required.
2873  **/
2874 int
2875 lpfc_sli_hbq_size(void)
2876 {
2877         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
2878 }
2879
2880 /**
2881  * lpfc_sli_hbq_setup: configure and initialize HBQs.
2882  * @phba: Pointer to HBA context object.
2883  *
2884  * This function is called during the SLI initialization to configure
2885  * all the HBQs and post buffers to the HBQ. The caller is not
2886  * required to hold any locks. This function will return zero if successful
2887  * else it will return negative error code.
2888  **/
2889 static int
2890 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
2891 {
2892         int  hbq_count = lpfc_sli_hbq_count();
2893         LPFC_MBOXQ_t *pmb;
2894         MAILBOX_t *pmbox;
2895         uint32_t hbqno;
2896         uint32_t hbq_entry_index;
2897
2898                                 /* Get a Mailbox buffer to setup mailbox
2899                                  * commands for HBA initialization
2900                                  */
2901         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2902
2903         if (!pmb)
2904                 return -ENOMEM;
2905
2906         pmbox = &pmb->mb;
2907
2908         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
2909         phba->link_state = LPFC_INIT_MBX_CMDS;
2910         phba->hbq_in_use = 1;
2911
2912         hbq_entry_index = 0;
2913         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
2914                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
2915                 phba->hbqs[hbqno].hbqPutIdx      = 0;
2916                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
2917                 phba->hbqs[hbqno].entry_count =
2918                         lpfc_hbq_defs[hbqno]->entry_count;
2919                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
2920                         hbq_entry_index, pmb);
2921                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
2922
2923                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
2924                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
2925                            mbxStatus <status>, ring <num> */
2926
2927                         lpfc_printf_log(phba, KERN_ERR,
2928                                         LOG_SLI | LOG_VPORT,
2929                                         "1805 Adapter failed to init. "
2930                                         "Data: x%x x%x x%x\n",
2931                                         pmbox->mbxCommand,
2932                                         pmbox->mbxStatus, hbqno);
2933
2934                         phba->link_state = LPFC_HBA_ERROR;
2935                         mempool_free(pmb, phba->mbox_mem_pool);
2936                         return ENXIO;
2937                 }
2938         }
2939         phba->hbq_count = hbq_count;
2940
2941         mempool_free(pmb, phba->mbox_mem_pool);
2942
2943         /* Initially populate or replenish the HBQs */
2944         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
2945                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
2946         return 0;
2947 }
2948
2949 /**
2950  * lpfc_do_config_port: Issue config port mailbox command.
2951  * @phba: Pointer to HBA context object.
2952  * @sli_mode: sli mode - 2/3
2953  *
2954  * This function is called by the sli intialization code path
2955  * to issue config_port mailbox command. This function restarts the
2956  * HBA firmware and issues a config_port mailbox command to configure
2957  * the SLI interface in the sli mode specified by sli_mode
2958  * variable. The caller is not required to hold any locks.
2959  * The function returns 0 if successful, else returns negative error
2960  * code.
2961  **/
2962 static int
2963 lpfc_do_config_port(struct lpfc_hba *phba, int sli_mode)
2964 {
2965         LPFC_MBOXQ_t *pmb;
2966         uint32_t resetcount = 0, rc = 0, done = 0;
2967
2968         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
2969         if (!pmb) {
2970                 phba->link_state = LPFC_HBA_ERROR;
2971                 return -ENOMEM;
2972         }
2973
2974         phba->sli_rev = sli_mode;
2975         while (resetcount < 2 && !done) {
2976                 spin_lock_irq(&phba->hbalock);
2977                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2978                 spin_unlock_irq(&phba->hbalock);
2979                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
2980                 lpfc_sli_brdrestart(phba);
2981                 msleep(2500);
2982                 rc = lpfc_sli_chipset_init(phba);
2983                 if (rc)
2984                         break;
2985
2986                 spin_lock_irq(&phba->hbalock);
2987                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2988                 spin_unlock_irq(&phba->hbalock);
2989                 resetcount++;
2990
2991                 /* Call pre CONFIG_PORT mailbox command initialization.  A
2992                  * value of 0 means the call was successful.  Any other
2993                  * nonzero value is a failure, but if ERESTART is returned,
2994                  * the driver may reset the HBA and try again.
2995                  */
2996                 rc = lpfc_config_port_prep(phba);
2997                 if (rc == -ERESTART) {
2998                         phba->link_state = LPFC_LINK_UNKNOWN;
2999                         continue;
3000                 } else if (rc)
3001                         break;
3002                 phba->link_state = LPFC_INIT_MBX_CMDS;
3003                 lpfc_config_port(phba, pmb);
3004                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3005                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3006                                         LPFC_SLI3_HBQ_ENABLED |
3007                                         LPFC_SLI3_CRP_ENABLED |
3008                                         LPFC_SLI3_INB_ENABLED);
3009                 if (rc != MBX_SUCCESS) {
3010                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3011                                 "0442 Adapter failed to init, mbxCmd x%x "
3012                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3013                                 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
3014                         spin_lock_irq(&phba->hbalock);
3015                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
3016                         spin_unlock_irq(&phba->hbalock);
3017                         rc = -ENXIO;
3018                 } else
3019                         done = 1;
3020         }
3021         if (!done) {
3022                 rc = -EINVAL;
3023                 goto do_prep_failed;
3024         }
3025         if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3026                 if (!pmb->mb.un.varCfgPort.cMA) {
3027                         rc = -ENXIO;
3028                         goto do_prep_failed;
3029                 }
3030                 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3031                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3032                         phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3033                 } else
3034                         phba->max_vpi = 0;
3035                 if (pmb->mb.un.varCfgPort.gerbm)
3036                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3037                 if (pmb->mb.un.varCfgPort.gcrp)
3038                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3039                 if (pmb->mb.un.varCfgPort.ginb) {
3040                         phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3041                         phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3042                         phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3043                         phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3044                         phba->inb_last_counter =
3045                                         phba->mbox->us.s3_inb_pgp.counter;
3046                 } else {
3047                         phba->port_gp = phba->mbox->us.s3_pgp.port;
3048                         phba->inb_ha_copy = NULL;
3049                         phba->inb_counter = NULL;
3050                 }
3051         } else {
3052                 phba->port_gp = phba->mbox->us.s2.port;
3053                 phba->inb_ha_copy = NULL;
3054                 phba->inb_counter = NULL;
3055                 phba->max_vpi = 0;
3056         }
3057 do_prep_failed:
3058         mempool_free(pmb, phba->mbox_mem_pool);
3059         return rc;
3060 }
3061
3062
3063 /**
3064  * lpfc_sli_hba_setup: SLI intialization function.
3065  * @phba: Pointer to HBA context object.
3066  *
3067  * This function is the main SLI intialization function. This function
3068  * is called by the HBA intialization code, HBA reset code and HBA
3069  * error attention handler code. Caller is not required to hold any
3070  * locks. This function issues config_port mailbox command to configure
3071  * the SLI, setup iocb rings and HBQ rings. In the end the function
3072  * calls the config_port_post function to issue init_link mailbox
3073  * command and to start the discovery. The function will return zero
3074  * if successful, else it will return negative error code.
3075  **/
3076 int
3077 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3078 {
3079         uint32_t rc;
3080         int  mode = 3;
3081
3082         switch (lpfc_sli_mode) {
3083         case 2:
3084                 if (phba->cfg_enable_npiv) {
3085                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3086                                 "1824 NPIV enabled: Override lpfc_sli_mode "
3087                                 "parameter (%d) to auto (0).\n",
3088                                 lpfc_sli_mode);
3089                         break;
3090                 }
3091                 mode = 2;
3092                 break;
3093         case 0:
3094         case 3:
3095                 break;
3096         default:
3097                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3098                                 "1819 Unrecognized lpfc_sli_mode "
3099                                 "parameter: %d.\n", lpfc_sli_mode);
3100
3101                 break;
3102         }
3103
3104         rc = lpfc_do_config_port(phba, mode);
3105         if (rc && lpfc_sli_mode == 3)
3106                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
3107                                 "1820 Unable to select SLI-3.  "
3108                                 "Not supported by adapter.\n");
3109         if (rc && mode != 2)
3110                 rc = lpfc_do_config_port(phba, 2);
3111         if (rc)
3112                 goto lpfc_sli_hba_setup_error;
3113
3114         if (phba->sli_rev == 3) {
3115                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
3116                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
3117         } else {
3118                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
3119                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
3120                 phba->sli3_options = 0;
3121         }
3122
3123         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3124                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
3125                         phba->sli_rev, phba->max_vpi);
3126         rc = lpfc_sli_ring_map(phba);
3127
3128         if (rc)
3129                 goto lpfc_sli_hba_setup_error;
3130
3131                                 /* Init HBQs */
3132
3133         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
3134                 rc = lpfc_sli_hbq_setup(phba);
3135                 if (rc)
3136                         goto lpfc_sli_hba_setup_error;
3137         }
3138
3139         phba->sli.sli_flag |= LPFC_PROCESS_LA;
3140
3141         rc = lpfc_config_port_post(phba);
3142         if (rc)
3143                 goto lpfc_sli_hba_setup_error;
3144
3145         return rc;
3146
3147 lpfc_sli_hba_setup_error:
3148         phba->link_state = LPFC_HBA_ERROR;
3149         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3150                         "0445 Firmware initialization failed\n");
3151         return rc;
3152 }
3153
3154
3155 /**
3156  * lpfc_mbox_timeout: Timeout call back function for mbox timer.
3157  * @ptr: context object - pointer to hba structure.
3158  *
3159  * This is the callback function for mailbox timer. The mailbox
3160  * timer is armed when a new mailbox command is issued and the timer
3161  * is deleted when the mailbox complete. The function is called by
3162  * the kernel timer code when a mailbox does not complete within
3163  * expected time. This function wakes up the worker thread to
3164  * process the mailbox timeout and returns. All the processing is
3165  * done by the worker thread function lpfc_mbox_timeout_handler.
3166  **/
3167 void
3168 lpfc_mbox_timeout(unsigned long ptr)
3169 {
3170         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
3171         unsigned long iflag;
3172         uint32_t tmo_posted;
3173
3174         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
3175         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
3176         if (!tmo_posted)
3177                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
3178         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
3179
3180         if (!tmo_posted)
3181                 lpfc_worker_wake_up(phba);
3182         return;
3183 }
3184
3185
3186 /**
3187  * lpfc_mbox_timeout_handler: Worker thread function to handle mailbox timeout.
3188  * @phba: Pointer to HBA context object.
3189  *
3190  * This function is called from worker thread when a mailbox command times out.
3191  * The caller is not required to hold any locks. This function will reset the
3192  * HBA and recover all the pending commands.
3193  **/
3194 void
3195 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
3196 {
3197         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
3198         MAILBOX_t *mb = &pmbox->mb;
3199         struct lpfc_sli *psli = &phba->sli;
3200         struct lpfc_sli_ring *pring;
3201
3202         if (!(phba->pport->work_port_events & WORKER_MBOX_TMO)) {
3203                 return;
3204         }
3205
3206         /* Mbox cmd <mbxCommand> timeout */
3207         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3208                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
3209                         mb->mbxCommand,
3210                         phba->pport->port_state,
3211                         phba->sli.sli_flag,
3212                         phba->sli.mbox_active);
3213
3214         /* Setting state unknown so lpfc_sli_abort_iocb_ring
3215          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
3216          * it to fail all oustanding SCSI IO.
3217          */
3218         spin_lock_irq(&phba->pport->work_port_lock);
3219         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
3220         spin_unlock_irq(&phba->pport->work_port_lock);
3221         spin_lock_irq(&phba->hbalock);
3222         phba->link_state = LPFC_LINK_UNKNOWN;
3223         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
3224         spin_unlock_irq(&phba->hbalock);
3225
3226         pring = &psli->ring[psli->fcp_ring];
3227         lpfc_sli_abort_iocb_ring(phba, pring);
3228
3229         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3230                         "0345 Resetting board due to mailbox timeout\n");
3231         /*
3232          * lpfc_offline calls lpfc_sli_hba_down which will clean up
3233          * on oustanding mailbox commands.
3234          */
3235         /* If resets are disabled then set error state and return. */
3236         if (!phba->cfg_enable_hba_reset) {
3237                 phba->link_state = LPFC_HBA_ERROR;
3238                 return;
3239         }
3240         lpfc_offline_prep(phba);
3241         lpfc_offline(phba);
3242         lpfc_sli_brdrestart(phba);
3243         lpfc_online(phba);
3244         lpfc_unblock_mgmt_io(phba);
3245         return;
3246 }
3247
3248 /**
3249  * lpfc_sli_issue_mbox: Issue a mailbox command to firmware.
3250  * @phba: Pointer to HBA context object.
3251  * @pmbox: Pointer to mailbox object.
3252  * @flag: Flag indicating how the mailbox need to be processed.
3253  *
3254  * This function is called by discovery code and HBA management code
3255  * to submit a mailbox command to firmware. This function gets the
3256  * hbalock to protect the data structures.
3257  * The mailbox command can be submitted in polling mode, in which case
3258  * this function will wait in a polling loop for the completion of the
3259  * mailbox.
3260  * If the mailbox is submitted in no_wait mode (not polling) the
3261  * function will submit the command and returns immediately without waiting
3262  * for the mailbox completion. The no_wait is supported only when HBA
3263  * is in SLI2/SLI3 mode - interrupts are enabled.
3264  * The SLI interface allows only one mailbox pending at a time. If the
3265  * mailbox is issued in polling mode and there is already a mailbox
3266  * pending, then the function will return an error. If the mailbox is issued
3267  * in NO_WAIT mode and there is a mailbox pending already, the function
3268  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
3269  * The sli layer owns the mailbox object until the completion of mailbox
3270  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
3271  * return codes the caller owns the mailbox command after the return of
3272  * the function.
3273  **/
3274 int
3275 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
3276 {
3277         MAILBOX_t *mb;
3278         struct lpfc_sli *psli = &phba->sli;
3279         uint32_t status, evtctr;
3280         uint32_t ha_copy;
3281         int i;
3282         unsigned long timeout;
3283         unsigned long drvr_flag = 0;
3284         uint32_t word0, ldata;
3285         void __iomem *to_slim;
3286         int processing_queue = 0;
3287
3288         spin_lock_irqsave(&phba->hbalock, drvr_flag);
3289         if (!pmbox) {
3290                 /* processing mbox queue from intr_handler */
3291                 processing_queue = 1;
3292                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3293                 pmbox = lpfc_mbox_get(phba);
3294                 if (!pmbox) {
3295                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3296                         return MBX_SUCCESS;
3297                 }
3298         }
3299
3300         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
3301                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
3302                 if(!pmbox->vport) {
3303                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3304                         lpfc_printf_log(phba, KERN_ERR,
3305                                         LOG_MBOX | LOG_VPORT,
3306                                         "1806 Mbox x%x failed. No vport\n",
3307                                         pmbox->mb.mbxCommand);
3308                         dump_stack();
3309                         goto out_not_finished;
3310                 }
3311         }
3312
3313         /* If the PCI channel is in offline state, do not post mbox. */
3314         if (unlikely(pci_channel_offline(phba->pcidev))) {
3315                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3316                 goto out_not_finished;
3317         }
3318
3319         psli = &phba->sli;
3320
3321         mb = &pmbox->mb;
3322         status = MBX_SUCCESS;
3323
3324         if (phba->link_state == LPFC_HBA_ERROR) {
3325                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3326
3327                 /* Mbox command <mbxCommand> cannot issue */
3328                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
3329                 goto out_not_finished;
3330         }
3331
3332         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
3333             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
3334                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3335                 LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
3336                 goto out_not_finished;
3337         }
3338
3339         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
3340                 /* Polling for a mbox command when another one is already active
3341                  * is not allowed in SLI. Also, the driver must have established
3342                  * SLI2 mode to queue and process multiple mbox commands.
3343                  */
3344
3345                 if (flag & MBX_POLL) {
3346                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3347
3348                         /* Mbox command <mbxCommand> cannot issue */
3349                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
3350                         goto out_not_finished;
3351                 }
3352
3353                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE)) {
3354                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3355                         /* Mbox command <mbxCommand> cannot issue */
3356                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
3357                         goto out_not_finished;
3358                 }
3359
3360                 /* Another mailbox command is still being processed, queue this
3361                  * command to be processed later.
3362                  */
3363                 lpfc_mbox_put(phba, pmbox);
3364
3365                 /* Mbox cmd issue - BUSY */
3366                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3367                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
3368                                 "x%x x%x x%x x%x\n",
3369                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
3370                                 mb->mbxCommand, phba->pport->port_state,
3371                                 psli->sli_flag, flag);
3372
3373                 psli->slistat.mbox_busy++;
3374                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3375
3376                 if (pmbox->vport) {
3377                         lpfc_debugfs_disc_trc(pmbox->vport,
3378                                 LPFC_DISC_TRC_MBOX_VPORT,
3379                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
3380                                 (uint32_t)mb->mbxCommand,
3381                                 mb->un.varWords[0], mb->un.varWords[1]);
3382                 }
3383                 else {
3384                         lpfc_debugfs_disc_trc(phba->pport,
3385                                 LPFC_DISC_TRC_MBOX,
3386                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
3387                                 (uint32_t)mb->mbxCommand,
3388                                 mb->un.varWords[0], mb->un.varWords[1]);
3389                 }
3390
3391                 return MBX_BUSY;
3392         }
3393
3394         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3395
3396         /* If we are not polling, we MUST be in SLI2 mode */
3397         if (flag != MBX_POLL) {
3398                 if (!(psli->sli_flag & LPFC_SLI2_ACTIVE) &&
3399                     (mb->mbxCommand != MBX_KILL_BOARD)) {
3400                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3401                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3402                         /* Mbox command <mbxCommand> cannot issue */
3403                         LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag);
3404                         goto out_not_finished;
3405                 }
3406                 /* timeout active mbox command */
3407                 mod_timer(&psli->mbox_tmo, (jiffies +
3408                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
3409         }
3410
3411         /* Mailbox cmd <cmd> issue */
3412         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
3413                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
3414                         "x%x\n",
3415                         pmbox->vport ? pmbox->vport->vpi : 0,
3416                         mb->mbxCommand, phba->pport->port_state,
3417                         psli->sli_flag, flag);
3418
3419         if (mb->mbxCommand != MBX_HEARTBEAT) {
3420                 if (pmbox->vport) {
3421                         lpfc_debugfs_disc_trc(pmbox->vport,
3422                                 LPFC_DISC_TRC_MBOX_VPORT,
3423                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
3424                                 (uint32_t)mb->mbxCommand,
3425                                 mb->un.varWords[0], mb->un.varWords[1]);
3426                 }
3427                 else {
3428                         lpfc_debugfs_disc_trc(phba->pport,
3429                                 LPFC_DISC_TRC_MBOX,
3430                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
3431                                 (uint32_t)mb->mbxCommand,
3432                                 mb->un.varWords[0], mb->un.varWords[1]);
3433                 }
3434         }
3435
3436         psli->slistat.mbox_cmd++;
3437         evtctr = psli->slistat.mbox_event;
3438
3439         /* next set own bit for the adapter and copy over command word */
3440         mb->mbxOwner = OWN_CHIP;
3441
3442         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3443                 /* First copy command data to host SLIM area */
3444                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
3445         } else {
3446                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3447                         /* copy command data into host mbox for cmpl */
3448                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
3449                 }
3450
3451                 /* First copy mbox command data to HBA SLIM, skip past first
3452                    word */
3453                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
3454                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
3455                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
3456
3457                 /* Next copy over first word, with mbxOwner set */
3458                 ldata = *((uint32_t *)mb);
3459                 to_slim = phba->MBslimaddr;
3460                 writel(ldata, to_slim);
3461                 readl(to_slim); /* flush */
3462
3463                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3464                         /* switch over to host mailbox */
3465                         psli->sli_flag |= LPFC_SLI2_ACTIVE;
3466                 }
3467         }
3468
3469         wmb();
3470
3471         switch (flag) {
3472         case MBX_NOWAIT:
3473                 /* Set up reference to mailbox command */
3474                 psli->mbox_active = pmbox;
3475                 /* Interrupt board to do it */
3476                 writel(CA_MBATT, phba->CAregaddr);
3477                 readl(phba->CAregaddr); /* flush */
3478                 /* Don't wait for it to finish, just return */
3479                 break;
3480
3481         case MBX_POLL:
3482                 /* Set up null reference to mailbox command */
3483                 psli->mbox_active = NULL;
3484                 /* Interrupt board to do it */
3485                 writel(CA_MBATT, phba->CAregaddr);
3486                 readl(phba->CAregaddr); /* flush */
3487
3488                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3489                         /* First read mbox status word */
3490                         word0 = *((uint32_t *)phba->mbox);
3491                         word0 = le32_to_cpu(word0);
3492                 } else {
3493                         /* First read mbox status word */
3494                         word0 = readl(phba->MBslimaddr);
3495                 }
3496
3497                 /* Read the HBA Host Attention Register */
3498                 ha_copy = readl(phba->HAregaddr);
3499                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
3500                                                              mb->mbxCommand) *
3501                                            1000) + jiffies;
3502                 i = 0;
3503                 /* Wait for command to complete */
3504                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
3505                        (!(ha_copy & HA_MBATT) &&
3506                         (phba->link_state > LPFC_WARM_START))) {
3507                         if (time_after(jiffies, timeout)) {
3508                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3509                                 spin_unlock_irqrestore(&phba->hbalock,
3510                                                        drvr_flag);
3511                                 goto out_not_finished;
3512                         }
3513
3514                         /* Check if we took a mbox interrupt while we were
3515                            polling */
3516                         if (((word0 & OWN_CHIP) != OWN_CHIP)
3517                             && (evtctr != psli->slistat.mbox_event))
3518                                 break;
3519
3520                         if (i++ > 10) {
3521                                 spin_unlock_irqrestore(&phba->hbalock,
3522                                                        drvr_flag);
3523                                 msleep(1);
3524                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
3525                         }
3526
3527                         if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3528                                 /* First copy command data */
3529                                 word0 = *((uint32_t *)phba->mbox);
3530                                 word0 = le32_to_cpu(word0);
3531                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
3532                                         MAILBOX_t *slimmb;
3533                                         uint32_t slimword0;
3534                                         /* Check real SLIM for any errors */
3535                                         slimword0 = readl(phba->MBslimaddr);
3536                                         slimmb = (MAILBOX_t *) & slimword0;
3537                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
3538                                             && slimmb->mbxStatus) {
3539                                                 psli->sli_flag &=
3540                                                     ~LPFC_SLI2_ACTIVE;
3541                                                 word0 = slimword0;
3542                                         }
3543                                 }
3544                         } else {
3545                                 /* First copy command data */
3546                                 word0 = readl(phba->MBslimaddr);
3547                         }
3548                         /* Read the HBA Host Attention Register */
3549                         ha_copy = readl(phba->HAregaddr);
3550                 }
3551
3552                 if (psli->sli_flag & LPFC_SLI2_ACTIVE) {
3553                         /* copy results back to user */
3554                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
3555                 } else {
3556                         /* First copy command data */
3557                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
3558                                                         MAILBOX_CMD_SIZE);
3559                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
3560                                 pmbox->context2) {
3561                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
3562                                       phba->MBslimaddr + DMP_RSP_OFFSET,
3563                                                       mb->un.varDmp.word_cnt);
3564                         }
3565                 }
3566
3567                 writel(HA_MBATT, phba->HAregaddr);
3568                 readl(phba->HAregaddr); /* flush */
3569
3570                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3571                 status = mb->mbxStatus;
3572         }
3573
3574         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3575         return status;
3576
3577 out_not_finished:
3578         if (processing_queue) {
3579                 pmbox->mb.mbxStatus = MBX_NOT_FINISHED;
3580                 lpfc_mbox_cmpl_put(phba, pmbox);
3581         }
3582         return MBX_NOT_FINISHED;
3583 }
3584
3585 /**
3586  * __lpfc_sli_ringtx_put: Add an iocb to the txq.
3587  * @phba: Pointer to HBA context object.
3588  * @pring: Pointer to driver SLI ring object.
3589  * @piocb: Pointer to address of newly added command iocb.
3590  *
3591  * This function is called with hbalock held to add a command
3592  * iocb to the txq when SLI layer cannot submit the command iocb
3593  * to the ring.
3594  **/
3595 static void
3596 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3597                     struct lpfc_iocbq *piocb)
3598 {
3599         /* Insert the caller's iocb in the txq tail for later processing. */
3600         list_add_tail(&piocb->list, &pring->txq);
3601         pring->txq_cnt++;
3602 }
3603
3604 /**
3605  * lpfc_sli_next_iocb: Get the next iocb in the txq.
3606  * @phba: Pointer to HBA context object.
3607  * @pring: Pointer to driver SLI ring object.
3608  * @piocb: Pointer to address of newly added command iocb.
3609  *
3610  * This function is called with hbalock held before a new
3611  * iocb is submitted to the firmware. This function checks
3612  * txq to flush the iocbs in txq to Firmware before
3613  * submitting new iocbs to the Firmware.
3614  * If there are iocbs in the txq which need to be submitted
3615  * to firmware, lpfc_sli_next_iocb returns the first element
3616  * of the txq after dequeuing it from txq.
3617  * If there is no iocb in the txq then the function will return
3618  * *piocb and *piocb is set to NULL. Caller needs to check
3619  * *piocb to find if there are more commands in the txq.
3620  **/
3621 static struct lpfc_iocbq *
3622 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3623                    struct lpfc_iocbq **piocb)
3624 {
3625         struct lpfc_iocbq * nextiocb;
3626
3627         nextiocb = lpfc_sli_ringtx_get(phba, pring);
3628         if (!nextiocb) {
3629                 nextiocb = *piocb;
3630                 *piocb = NULL;
3631         }
3632
3633         return nextiocb;
3634 }
3635
3636 /**
3637  * __lpfc_sli_issue_iocb: Lockless version of lpfc_sli_issue_iocb.
3638  * @phba: Pointer to HBA context object.
3639  * @pring: Pointer to driver SLI ring object.
3640  * @piocb: Pointer to command iocb.
3641  * @flag: Flag indicating if this command can be put into txq.
3642  *
3643  * __lpfc_sli_issue_iocb is used by other functions in the driver
3644  * to issue an iocb command to the HBA. If the PCI slot is recovering
3645  * from error state or if HBA is resetting or if LPFC_STOP_IOCB_EVENT
3646  * flag is turned on, the function returns IOCB_ERROR.
3647  * When the link is down, this function allows only iocbs for
3648  * posting buffers.
3649  * This function finds next available slot in the command ring and
3650  * posts the command to the available slot and writes the port
3651  * attention register to request HBA start processing new iocb.
3652  * If there is no slot available in the ring and
3653  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the
3654  * txq, otherwise the function returns IOCB_BUSY.
3655  *
3656  * This function is called with hbalock held.
3657  * The function will return success after it successfully submit the
3658  * iocb to firmware or after adding to the txq.
3659  **/
3660 static int
3661 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3662                     struct lpfc_iocbq *piocb, uint32_t flag)
3663 {
3664         struct lpfc_iocbq *nextiocb;
3665         IOCB_t *iocb;
3666
3667         if (piocb->iocb_cmpl && (!piocb->vport) &&
3668            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
3669            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
3670                 lpfc_printf_log(phba, KERN_ERR,
3671                                 LOG_SLI | LOG_VPORT,
3672                                 "1807 IOCB x%x failed. No vport\n",
3673                                 piocb->iocb.ulpCommand);
3674                 dump_stack();
3675                 return IOCB_ERROR;
3676         }
3677
3678
3679         /* If the PCI channel is in offline state, do not post iocbs. */
3680         if (unlikely(pci_channel_offline(phba->pcidev)))
3681                 return IOCB_ERROR;
3682
3683         /*
3684          * We should never get an IOCB if we are in a < LINK_DOWN state
3685          */
3686         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
3687                 return IOCB_ERROR;
3688
3689         /*
3690          * Check to see if we are blocking IOCB processing because of a
3691          * outstanding event.
3692          */
3693         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
3694                 goto iocb_busy;
3695
3696         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
3697                 /*
3698                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
3699                  * can be issued if the link is not up.
3700                  */
3701                 switch (piocb->iocb.ulpCommand) {
3702                 case CMD_GEN_REQUEST64_CR:
3703                 case CMD_GEN_REQUEST64_CX:
3704                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
3705                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
3706                                         FC_FCP_CMND) ||
3707                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
3708                                         MENLO_TRANSPORT_TYPE))
3709
3710                                 goto iocb_busy;
3711                         break;
3712                 case CMD_QUE_RING_BUF_CN:
3713                 case CMD_QUE_RING_BUF64_CN:
3714                         /*
3715                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
3716                          * completion, iocb_cmpl MUST be 0.
3717                          */
3718                         if (piocb->iocb_cmpl)
3719                                 piocb->iocb_cmpl = NULL;
3720                         /*FALLTHROUGH*/
3721                 case CMD_CREATE_XRI_CR:
3722                 case CMD_CLOSE_XRI_CN:
3723                 case CMD_CLOSE_XRI_CX:
3724                         break;
3725                 default:
3726                         goto iocb_busy;
3727                 }
3728
3729         /*
3730          * For FCP commands, we must be in a state where we can process link
3731          * attention events.
3732          */
3733         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
3734                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
3735                 goto iocb_busy;
3736         }
3737
3738         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
3739                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
3740                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
3741
3742         if (iocb)
3743                 lpfc_sli_update_ring(phba, pring);
3744         else
3745                 lpfc_sli_update_full_ring(phba, pring);
3746
3747         if (!piocb)
3748                 return IOCB_SUCCESS;
3749
3750         goto out_busy;
3751
3752  iocb_busy:
3753         pring->stats.iocb_cmd_delay++;
3754
3755  out_busy:
3756
3757         if (!(flag & SLI_IOCB_RET_IOCB)) {
3758                 __lpfc_sli_ringtx_put(phba, pring, piocb);
3759                 return IOCB_SUCCESS;
3760         }
3761
3762         return IOCB_BUSY;
3763 }
3764
3765
3766 /**
3767  * lpfc_sli_issue_iocb: Wrapper function for __lpfc_sli_issue_iocb.
3768  * @phba: Pointer to HBA context object.
3769  * @pring: Pointer to driver SLI ring object.
3770  * @piocb: Pointer to command iocb.
3771  * @flag: Flag indicating if this command can be put into txq.
3772  *
3773  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
3774  * function. This function gets the hbalock and calls
3775  * __lpfc_sli_issue_iocb function and will return the error returned
3776  * by __lpfc_sli_issue_iocb function. This wrapper is used by
3777  * functions which do not hold hbalock.
3778  **/
3779 int
3780 lpfc_sli_issue_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3781                     struct lpfc_iocbq *piocb, uint32_t flag)
3782 {
3783         unsigned long iflags;
3784         int rc;
3785
3786         spin_lock_irqsave(&phba->hbalock, iflags);
3787         rc = __lpfc_sli_issue_iocb(phba, pring, piocb, flag);
3788         spin_unlock_irqrestore(&phba->hbalock, iflags);
3789
3790         return rc;
3791 }
3792
3793 /**
3794  * lpfc_extra_ring_setup: Extra ring setup function.
3795  * @phba: Pointer to HBA context object.
3796  *
3797  * This function is called while driver attaches with the
3798  * HBA to setup the extra ring. The extra ring is used
3799  * only when driver needs to support target mode functionality
3800  * or IP over FC functionalities.
3801  *
3802  * This function is called with no lock held.
3803  **/
3804 static int
3805 lpfc_extra_ring_setup( struct lpfc_hba *phba)
3806 {
3807         struct lpfc_sli *psli;
3808         struct lpfc_sli_ring *pring;
3809
3810         psli = &phba->sli;
3811
3812         /* Adjust cmd/rsp ring iocb entries more evenly */
3813
3814         /* Take some away from the FCP ring */
3815         pring = &psli->ring[psli->fcp_ring];
3816         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3817         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3818         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3819         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3820
3821         /* and give them to the extra ring */
3822         pring = &psli->ring[psli->extra_ring];
3823
3824         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3825         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3826         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3827         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3828
3829         /* Setup default profile for this ring */
3830         pring->iotag_max = 4096;
3831         pring->num_mask = 1;
3832         pring->prt[0].profile = 0;      /* Mask 0 */
3833         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
3834         pring->prt[0].type = phba->cfg_multi_ring_type;
3835         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
3836         return 0;
3837 }
3838
3839 /**
3840  * lpfc_sli_async_event_handler: ASYNC iocb handler function.
3841  * @phba: Pointer to HBA context object.
3842  * @pring: Pointer to driver SLI ring object.
3843  * @iocbq: Pointer to iocb object.
3844  *
3845  * This function is called by the slow ring event handler
3846  * function when there is an ASYNC event iocb in the ring.
3847  * This function is called with no lock held.
3848  * Currently this function handles only temperature related
3849  * ASYNC events. The function decodes the temperature sensor
3850  * event message and posts events for the management applications.
3851  **/
3852 static void
3853 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
3854         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
3855 {
3856         IOCB_t *icmd;
3857         uint16_t evt_code;
3858         uint16_t temp;
3859         struct temp_event temp_event_data;
3860         struct Scsi_Host *shost;
3861
3862         icmd = &iocbq->iocb;
3863         evt_code = icmd->un.asyncstat.evt_code;
3864         temp = icmd->ulpContext;
3865
3866         if ((evt_code != ASYNC_TEMP_WARN) &&
3867                 (evt_code != ASYNC_TEMP_SAFE)) {
3868                 lpfc_printf_log(phba,
3869                         KERN_ERR,
3870                         LOG_SLI,
3871                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
3872                         " evt_code 0x%x\n",
3873                         pring->ringno,
3874                         icmd->un.asyncstat.evt_code);
3875                 return;
3876         }
3877         temp_event_data.data = (uint32_t)temp;
3878         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
3879         if (evt_code == ASYNC_TEMP_WARN) {
3880                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
3881                 lpfc_printf_log(phba,
3882                                 KERN_ERR,
3883                                 LOG_TEMP,
3884                                 "0347 Adapter is very hot, please take "
3885                                 "corrective action. temperature : %d Celsius\n",
3886                                 temp);
3887         }
3888         if (evt_code == ASYNC_TEMP_SAFE) {
3889                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
3890                 lpfc_printf_log(phba,
3891                                 KERN_ERR,
3892                                 LOG_TEMP,
3893                                 "0340 Adapter temperature is OK now. "
3894                                 "temperature : %d Celsius\n",
3895                                 temp);
3896         }
3897
3898         /* Send temperature change event to applications */
3899         shost = lpfc_shost_from_vport(phba->pport);
3900         fc_host_post_vendor_event(shost, fc_get_event_number(),
3901                 sizeof(temp_event_data), (char *) &temp_event_data,
3902                 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
3903
3904 }
3905
3906
3907 /**
3908  * lpfc_sli_setup: SLI ring setup function.
3909  * @phba: Pointer to HBA context object.
3910  *
3911  * lpfc_sli_setup sets up rings of the SLI interface with
3912  * number of iocbs per ring and iotags. This function is
3913  * called while driver attach to the HBA and before the
3914  * interrupts are enabled. So there is no need for locking.
3915  *
3916  * This function always returns 0.
3917  **/
3918 int
3919 lpfc_sli_setup(struct lpfc_hba *phba)
3920 {
3921         int i, totiocbsize = 0;
3922         struct lpfc_sli *psli = &phba->sli;
3923         struct lpfc_sli_ring *pring;
3924
3925         psli->num_rings = MAX_CONFIGURED_RINGS;
3926         psli->sli_flag = 0;
3927         psli->fcp_ring = LPFC_FCP_RING;
3928         psli->next_ring = LPFC_FCP_NEXT_RING;
3929         psli->extra_ring = LPFC_EXTRA_RING;
3930
3931         psli->iocbq_lookup = NULL;
3932         psli->iocbq_lookup_len = 0;
3933         psli->last_iotag = 0;
3934
3935         for (i = 0; i < psli->num_rings; i++) {
3936                 pring = &psli->ring[i];
3937                 switch (i) {
3938                 case LPFC_FCP_RING:     /* ring 0 - FCP */
3939                         /* numCiocb and numRiocb are used in config_port */
3940                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
3941                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
3942                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
3943                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
3944                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
3945                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
3946                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3947                                                         SLI3_IOCB_CMD_SIZE :
3948                                                         SLI2_IOCB_CMD_SIZE;
3949                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3950                                                         SLI3_IOCB_RSP_SIZE :
3951                                                         SLI2_IOCB_RSP_SIZE;
3952                         pring->iotag_ctr = 0;
3953                         pring->iotag_max =
3954                             (phba->cfg_hba_queue_depth * 2);
3955                         pring->fast_iotag = pring->iotag_max;
3956                         pring->num_mask = 0;
3957                         break;
3958                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
3959                         /* numCiocb and numRiocb are used in config_port */
3960                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
3961                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
3962                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3963                                                         SLI3_IOCB_CMD_SIZE :
3964                                                         SLI2_IOCB_CMD_SIZE;
3965                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3966                                                         SLI3_IOCB_RSP_SIZE :
3967                                                         SLI2_IOCB_RSP_SIZE;
3968                         pring->iotag_max = phba->cfg_hba_queue_depth;
3969                         pring->num_mask = 0;
3970                         break;
3971                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
3972                         /* numCiocb and numRiocb are used in config_port */
3973                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
3974                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
3975                         pring->sizeCiocb = (phba->sli_rev == 3) ?
3976                                                         SLI3_IOCB_CMD_SIZE :
3977                                                         SLI2_IOCB_CMD_SIZE;
3978                         pring->sizeRiocb = (phba->sli_rev == 3) ?
3979                                                         SLI3_IOCB_RSP_SIZE :
3980                                                         SLI2_IOCB_RSP_SIZE;
3981                         pring->fast_iotag = 0;
3982                         pring->iotag_ctr = 0;
3983                         pring->iotag_max = 4096;
3984                         pring->lpfc_sli_rcv_async_status =
3985                                 lpfc_sli_async_event_handler;
3986                         pring->num_mask = 4;
3987                         pring->prt[0].profile = 0;      /* Mask 0 */
3988                         pring->prt[0].rctl = FC_ELS_REQ;
3989                         pring->prt[0].type = FC_ELS_DATA;
3990                         pring->prt[0].lpfc_sli_rcv_unsol_event =
3991                             lpfc_els_unsol_event;
3992                         pring->prt[1].profile = 0;      /* Mask 1 */
3993                         pring->prt[1].rctl = FC_ELS_RSP;
3994                         pring->prt[1].type = FC_ELS_DATA;
3995                         pring->prt[1].lpfc_sli_rcv_unsol_event =
3996                             lpfc_els_unsol_event;
3997                         pring->prt[2].profile = 0;      /* Mask 2 */
3998                         /* NameServer Inquiry */
3999                         pring->prt[2].rctl = FC_UNSOL_CTL;
4000                         /* NameServer */
4001                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
4002                         pring->prt[2].lpfc_sli_rcv_unsol_event =
4003                             lpfc_ct_unsol_event;
4004                         pring->prt[3].profile = 0;      /* Mask 3 */
4005                         /* NameServer response */
4006                         pring->prt[3].rctl = FC_SOL_CTL;
4007                         /* NameServer */
4008                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
4009                         pring->prt[3].lpfc_sli_rcv_unsol_event =
4010                             lpfc_ct_unsol_event;
4011                         break;
4012                 }
4013                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
4014                                 (pring->numRiocb * pring->sizeRiocb);
4015         }
4016         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
4017                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
4018                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
4019                        "SLI2 SLIM Data: x%x x%lx\n",
4020                        phba->brd_no, totiocbsize,
4021                        (unsigned long) MAX_SLIM_IOCB_SIZE);
4022         }
4023         if (phba->cfg_multi_ring_support == 2)
4024                 lpfc_extra_ring_setup(phba);
4025
4026         return 0;
4027 }
4028
4029 /**
4030  * lpfc_sli_queue_setup: Queue initialization function.
4031  * @phba: Pointer to HBA context object.
4032  *
4033  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
4034  * ring. This function also initializes ring indices of each ring.
4035  * This function is called during the initialization of the SLI
4036  * interface of an HBA.
4037  * This function is called with no lock held and always returns
4038  * 1.
4039  **/
4040 int
4041 lpfc_sli_queue_setup(struct lpfc_hba *phba)
4042 {
4043         struct lpfc_sli *psli;
4044         struct lpfc_sli_ring *pring;
4045         int i;
4046
4047         psli = &phba->sli;
4048         spin_lock_irq(&phba->hbalock);
4049         INIT_LIST_HEAD(&psli->mboxq);
4050         INIT_LIST_HEAD(&psli->mboxq_cmpl);
4051         /* Initialize list headers for txq and txcmplq as double linked lists */
4052         for (i = 0; i < psli->num_rings; i++) {
4053                 pring = &psli->ring[i];
4054                 pring->ringno = i;
4055                 pring->next_cmdidx  = 0;
4056                 pring->local_getidx = 0;
4057                 pring->cmdidx = 0;
4058                 INIT_LIST_HEAD(&pring->txq);
4059                 INIT_LIST_HEAD(&pring->txcmplq);
4060                 INIT_LIST_HEAD(&pring->iocb_continueq);
4061                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
4062                 INIT_LIST_HEAD(&pring->postbufq);
4063         }
4064         spin_unlock_irq(&phba->hbalock);
4065         return 1;
4066 }
4067
4068 /**
4069  * lpfc_sli_host_down: Vport cleanup function.
4070  * @vport: Pointer to virtual port object.
4071  *
4072  * lpfc_sli_host_down is called to clean up the resources
4073  * associated with a vport before destroying virtual
4074  * port data structures.
4075  * This function does following operations:
4076  * - Free discovery resources associated with this virtual
4077  *   port.
4078  * - Free iocbs associated with this virtual port in
4079  *   the txq.
4080  * - Send abort for all iocb commands associated with this
4081  *   vport in txcmplq.
4082  *
4083  * This function is called with no lock held and always returns 1.
4084  **/
4085 int
4086 lpfc_sli_host_down(struct lpfc_vport *vport)
4087 {
4088         LIST_HEAD(completions);
4089         struct lpfc_hba *phba = vport->phba;
4090         struct lpfc_sli *psli = &phba->sli;
4091         struct lpfc_sli_ring *pring;
4092         struct lpfc_iocbq *iocb, *next_iocb;
4093         int i;
4094         unsigned long flags = 0;
4095         uint16_t prev_pring_flag;
4096
4097         lpfc_cleanup_discovery_resources(vport);
4098
4099         spin_lock_irqsave(&phba->hbalock, flags);
4100         for (i = 0; i < psli->num_rings; i++) {
4101                 pring = &psli->ring[i];
4102                 prev_pring_flag = pring->flag;
4103                 /* Only slow rings */
4104                 if (pring->ringno == LPFC_ELS_RING) {
4105                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
4106                         /* Set the lpfc data pending flag */
4107                         set_bit(LPFC_DATA_READY, &phba->data_flags);
4108                 }
4109                 /*
4110                  * Error everything on the txq since these iocbs have not been
4111                  * given to the FW yet.
4112                  */
4113                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
4114                         if (iocb->vport != vport)
4115                                 continue;
4116                         list_move_tail(&iocb->list, &completions);
4117                         pring->txq_cnt--;
4118                 }
4119
4120                 /* Next issue ABTS for everything on the txcmplq */
4121                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
4122                                                                         list) {
4123                         if (iocb->vport != vport)
4124                                 continue;
4125                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
4126                 }
4127
4128                 pring->flag = prev_pring_flag;
4129         }
4130
4131         spin_unlock_irqrestore(&phba->hbalock, flags);
4132
4133         while (!list_empty(&completions)) {
4134                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4135
4136                 if (!iocb->iocb_cmpl)
4137                         lpfc_sli_release_iocbq(phba, iocb);
4138                 else {
4139                         iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4140                         iocb->iocb.un.ulpWord[4] = IOERR_SLI_DOWN;
4141                         (iocb->iocb_cmpl) (phba, iocb, iocb);
4142                 }
4143         }
4144         return 1;
4145 }
4146
4147 /**
4148  * lpfc_sli_hba_down: Resource cleanup function for the HBA.
4149  * @phba: Pointer to HBA context object.
4150  *
4151  * This function cleans up all iocb, buffers, mailbox commands
4152  * while shutting down the HBA. This function is called with no
4153  * lock held and always returns 1.
4154  * This function does the following to cleanup driver resources:
4155  * - Free discovery resources for each virtual port
4156  * - Cleanup any pending fabric iocbs
4157  * - Iterate through the iocb txq and free each entry
4158  *   in the list.
4159  * - Free up any buffer posted to the HBA
4160  * - Free mailbox commands in the mailbox queue.
4161  **/
4162 int
4163 lpfc_sli_hba_down(struct lpfc_hba *phba)
4164 {
4165         LIST_HEAD(completions);
4166         struct lpfc_sli *psli = &phba->sli;
4167         struct lpfc_sli_ring *pring;
4168         struct lpfc_dmabuf *buf_ptr;
4169         LPFC_MBOXQ_t *pmb;
4170         struct lpfc_iocbq *iocb;
4171         IOCB_t *cmd = NULL;
4172         int i;
4173         unsigned long flags = 0;
4174
4175         lpfc_hba_down_prep(phba);
4176
4177         lpfc_fabric_abort_hba(phba);
4178
4179         spin_lock_irqsave(&phba->hbalock, flags);
4180         for (i = 0; i < psli->num_rings; i++) {
4181                 pring = &psli->ring[i];
4182                 /* Only slow rings */
4183                 if (pring->ringno == LPFC_ELS_RING) {
4184                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
4185                         /* Set the lpfc data pending flag */
4186                         set_bit(LPFC_DATA_READY, &phba->data_flags);
4187                 }
4188
4189                 /*
4190                  * Error everything on the txq since these iocbs have not been
4191                  * given to the FW yet.
4192                  */
4193                 list_splice_init(&pring->txq, &completions);
4194                 pring->txq_cnt = 0;
4195
4196         }
4197         spin_unlock_irqrestore(&phba->hbalock, flags);
4198
4199         while (!list_empty(&completions)) {
4200                 list_remove_head(&completions, iocb, struct lpfc_iocbq, list);
4201                 cmd = &iocb->iocb;
4202
4203                 if (!iocb->iocb_cmpl)
4204                         lpfc_sli_release_iocbq(phba, iocb);
4205                 else {
4206                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
4207                         cmd->un.ulpWord[4] = IOERR_SLI_DOWN;
4208                         (iocb->iocb_cmpl) (phba, iocb, iocb);
4209                 }
4210         }
4211
4212         spin_lock_irqsave(&phba->hbalock, flags);
4213         list_splice_init(&phba->elsbuf, &completions);
4214         phba->elsbuf_cnt = 0;
4215         phba->elsbuf_prev_cnt = 0;
4216         spin_unlock_irqrestore(&phba->hbalock, flags);
4217
4218         while (!list_empty(&completions)) {
4219                 list_remove_head(&completions, buf_ptr,
4220                         struct lpfc_dmabuf, list);
4221                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
4222                 kfree(buf_ptr);
4223         }
4224
4225         /* Return any active mbox cmds */
4226         del_timer_sync(&psli->mbox_tmo);
4227         spin_lock_irqsave(&phba->hbalock, flags);
4228
4229         spin_lock(&phba->pport->work_port_lock);
4230         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4231         spin_unlock(&phba->pport->work_port_lock);
4232
4233         /* Return any pending or completed mbox cmds */
4234         list_splice_init(&phba->sli.mboxq, &completions);
4235         if (psli->mbox_active) {
4236                 list_add_tail(&psli->mbox_active->list, &completions);
4237                 psli->mbox_active = NULL;
4238                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4239         }
4240         list_splice_init(&phba->sli.mboxq_cmpl, &completions);
4241         spin_unlock_irqrestore(&phba->hbalock, flags);
4242
4243         while (!list_empty(&completions)) {
4244                 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
4245                 pmb->mb.mbxStatus = MBX_NOT_FINISHED;
4246                 if (pmb->mbox_cmpl)
4247                         pmb->mbox_cmpl(phba,pmb);
4248         }
4249         return 1;
4250 }
4251
4252 /**
4253  * lpfc_sli_pcimem_bcopy: SLI memory copy function.
4254  * @srcp: Source memory pointer.
4255  * @destp: Destination memory pointer.
4256  * @cnt: Number of words required to be copied.
4257  *
4258  * This function is used for copying data between driver memory
4259  * and the SLI memory. This function also changes the endianness
4260  * of each word if native endianness is different from SLI
4261  * endianness. This function can be called with or without
4262  * lock.
4263  **/
4264 void
4265 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
4266 {
4267         uint32_t *src = srcp;
4268         uint32_t *dest = destp;
4269         uint32_t ldata;
4270         int i;
4271
4272         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
4273                 ldata = *src;
4274                 ldata = le32_to_cpu(ldata);
4275                 *dest = ldata;
4276                 src++;
4277                 dest++;
4278         }
4279 }
4280
4281
4282 /**
4283  * lpfc_sli_ringpostbuf_put: Function to add a buffer to postbufq.
4284  * @phba: Pointer to HBA context object.
4285  * @pring: Pointer to driver SLI ring object.
4286  * @mp: Pointer to driver buffer object.
4287  *
4288  * This function is called with no lock held.
4289  * It always return zero after adding the buffer to the postbufq
4290  * buffer list.
4291  **/
4292 int
4293 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4294                          struct lpfc_dmabuf *mp)
4295 {
4296         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
4297            later */
4298         spin_lock_irq(&phba->hbalock);
4299         list_add_tail(&mp->list, &pring->postbufq);
4300         pring->postbufq_cnt++;
4301         spin_unlock_irq(&phba->hbalock);
4302         return 0;
4303 }
4304
4305 /**
4306  * lpfc_sli_get_buffer_tag: Tag allocation function for a buffer posted
4307  *          using CMD_QUE_XRI64_CX iocb.
4308  * @phba: Pointer to HBA context object.
4309  *
4310  * When HBQ is enabled, buffers are searched based on tags. This function
4311  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
4312  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
4313  * does not conflict with tags of buffer posted for unsolicited events.
4314  * The function returns the allocated tag. The function is called with
4315  * no locks held.
4316  **/
4317 uint32_t
4318 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
4319 {
4320         spin_lock_irq(&phba->hbalock);
4321         phba->buffer_tag_count++;
4322         /*
4323          * Always set the QUE_BUFTAG_BIT to distiguish between
4324          * a tag assigned by HBQ.
4325          */
4326         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
4327         spin_unlock_irq(&phba->hbalock);
4328         return phba->buffer_tag_count;
4329 }
4330
4331 /**
4332  * lpfc_sli_ring_taggedbuf_get: Search HBQ buffer associated with
4333  *              posted using CMD_QUE_XRI64_CX iocb.
4334  * @phba: Pointer to HBA context object.
4335  * @pring: Pointer to driver SLI ring object.
4336  * @tag: Buffer tag.
4337  *
4338  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
4339  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
4340  * iocb is posted to the response ring with the tag of the buffer.
4341  * This function searches the pring->postbufq list using the tag
4342  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
4343  * iocb. If the buffer is found then lpfc_dmabuf object of the
4344  * buffer is returned to the caller else NULL is returned.
4345  * This function is called with no lock held.
4346  **/
4347 struct lpfc_dmabuf *
4348 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4349                         uint32_t tag)
4350 {
4351         struct lpfc_dmabuf *mp, *next_mp;
4352         struct list_head *slp = &pring->postbufq;
4353
4354         /* Search postbufq, from the begining, looking for a match on tag */
4355         spin_lock_irq(&phba->hbalock);
4356         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4357                 if (mp->buffer_tag == tag) {
4358                         list_del_init(&mp->list);
4359                         pring->postbufq_cnt--;
4360                         spin_unlock_irq(&phba->hbalock);
4361                         return mp;
4362                 }
4363         }
4364
4365         spin_unlock_irq(&phba->hbalock);
4366         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4367                         "0402 Cannot find virtual addr for buffer tag on "
4368                         "ring %d Data x%lx x%p x%p x%x\n",
4369                         pring->ringno, (unsigned long) tag,
4370                         slp->next, slp->prev, pring->postbufq_cnt);
4371
4372         return NULL;
4373 }
4374
4375 /**
4376  * lpfc_sli_ringpostbuf_get: SLI2 buffer search function for
4377  *      unsolicited ct and els events.
4378  * @phba: Pointer to HBA context object.
4379  * @pring: Pointer to driver SLI ring object.
4380  * @phys: DMA address of the buffer.
4381  *
4382  * This function searches the buffer list using the dma_address
4383  * of unsolicited event to find the driver's lpfc_dmabuf object
4384  * corresponding to the dma_address. The function returns the
4385  * lpfc_dmabuf object if a buffer is found else it returns NULL.
4386  * This function is called by the ct and els unsolicited event
4387  * handlers to get the buffer associated with the unsolicited
4388  * event.
4389  *
4390  * This function is called with no lock held.
4391  **/
4392 struct lpfc_dmabuf *
4393 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4394                          dma_addr_t phys)
4395 {
4396         struct lpfc_dmabuf *mp, *next_mp;
4397         struct list_head *slp = &pring->postbufq;
4398
4399         /* Search postbufq, from the begining, looking for a match on phys */
4400         spin_lock_irq(&phba->hbalock);
4401         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
4402                 if (mp->phys == phys) {
4403                         list_del_init(&mp->list);
4404                         pring->postbufq_cnt--;
4405                         spin_unlock_irq(&phba->hbalock);
4406                         return mp;
4407                 }
4408         }
4409
4410         spin_unlock_irq(&phba->hbalock);
4411         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4412                         "0410 Cannot find virtual addr for mapped buf on "
4413                         "ring %d Data x%llx x%p x%p x%x\n",
4414                         pring->ringno, (unsigned long long)phys,
4415                         slp->next, slp->prev, pring->postbufq_cnt);
4416         return NULL;
4417 }
4418
4419 /**
4420  * lpfc_sli_abort_els_cmpl: Completion handler for the els abort iocbs.
4421  * @phba: Pointer to HBA context object.
4422  * @cmdiocb: Pointer to driver command iocb object.
4423  * @rspiocb: Pointer to driver response iocb object.
4424  *
4425  * This function is the completion handler for the abort iocbs for
4426  * ELS commands. This function is called from the ELS ring event
4427  * handler with no lock held. This function frees memory resources
4428  * associated with the abort iocb.
4429  **/
4430 static void
4431 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4432                         struct lpfc_iocbq *rspiocb)
4433 {
4434         IOCB_t *irsp = &rspiocb->iocb;
4435         uint16_t abort_iotag, abort_context;
4436         struct lpfc_iocbq *abort_iocb;
4437         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4438
4439         abort_iocb = NULL;
4440
4441         if (irsp->ulpStatus) {
4442                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
4443                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
4444
4445                 spin_lock_irq(&phba->hbalock);
4446                 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
4447                         abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
4448
4449                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
4450                                 "0327 Cannot abort els iocb %p "
4451                                 "with tag %x context %x, abort status %x, "
4452                                 "abort code %x\n",
4453                                 abort_iocb, abort_iotag, abort_context,
4454                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
4455
4456                 /*
4457                  *  If the iocb is not found in Firmware queue the iocb
4458                  *  might have completed already. Do not free it again.
4459                  */
4460                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
4461                         spin_unlock_irq(&phba->hbalock);
4462                         lpfc_sli_release_iocbq(phba, cmdiocb);
4463                         return;
4464                 }
4465                 /*
4466                  * make sure we have the right iocbq before taking it
4467                  * off the txcmplq and try to call completion routine.
4468                  */
4469                 if (!abort_iocb ||
4470                     abort_iocb->iocb.ulpContext != abort_context ||
4471                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
4472                         spin_unlock_irq(&phba->hbalock);
4473                 else {
4474                         list_del_init(&abort_iocb->list);
4475                         pring->txcmplq_cnt--;
4476                         spin_unlock_irq(&phba->hbalock);
4477
4478                         /* Firmware could still be in progress of DMAing
4479                          * payload, so don't free data buffer till after
4480                          * a hbeat.
4481                          */
4482                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
4483
4484                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4485                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
4486                         abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
4487                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
4488                 }
4489         }
4490
4491         lpfc_sli_release_iocbq(phba, cmdiocb);
4492         return;
4493 }
4494
4495 /**
4496  * lpfc_ignore_els_cmpl: Completion handler for aborted ELS command.
4497  * @phba: Pointer to HBA context object.
4498  * @cmdiocb: Pointer to driver command iocb object.
4499  * @rspiocb: Pointer to driver response iocb object.
4500  *
4501  * The function is called from SLI ring event handler with no
4502  * lock held. This function is the completion handler for ELS commands
4503  * which are aborted. The function frees memory resources used for
4504  * the aborted ELS commands.
4505  **/
4506 static void
4507 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4508                      struct lpfc_iocbq *rspiocb)
4509 {
4510         IOCB_t *irsp = &rspiocb->iocb;
4511
4512         /* ELS cmd tag <ulpIoTag> completes */
4513         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
4514                         "0139 Ignoring ELS cmd tag x%x completion Data: "
4515                         "x%x x%x x%x\n",
4516                         irsp->ulpIoTag, irsp->ulpStatus,
4517                         irsp->un.ulpWord[4], irsp->ulpTimeout);
4518         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
4519                 lpfc_ct_free_iocb(phba, cmdiocb);
4520         else
4521                 lpfc_els_free_iocb(phba, cmdiocb);
4522         return;
4523 }
4524
4525 /**
4526  * lpfc_sli_issue_abort_iotag: Abort function for a command iocb.
4527  * @phba: Pointer to HBA context object.
4528  * @pring: Pointer to driver SLI ring object.
4529  * @cmdiocb: Pointer to driver command iocb object.
4530  *
4531  * This function issues an abort iocb for the provided command
4532  * iocb. This function is called with hbalock held.
4533  * The function returns 0 when it fails due to memory allocation
4534  * failure or when the command iocb is an abort request.
4535  **/
4536 int
4537 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
4538                            struct lpfc_iocbq *cmdiocb)
4539 {
4540         struct lpfc_vport *vport = cmdiocb->vport;
4541         struct lpfc_iocbq *abtsiocbp;
4542         IOCB_t *icmd = NULL;
4543         IOCB_t *iabt = NULL;
4544         int retval = IOCB_ERROR;
4545
4546         /*
4547          * There are certain command types we don't want to abort.  And we
4548          * don't want to abort commands that are already in the process of
4549          * being aborted.
4550          */
4551         icmd = &cmdiocb->iocb;
4552         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
4553             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
4554             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
4555                 return 0;
4556
4557         /* If we're unloading, don't abort iocb on the ELS ring, but change the
4558          * callback so that nothing happens when it finishes.
4559          */
4560         if ((vport->load_flag & FC_UNLOADING) &&
4561             (pring->ringno == LPFC_ELS_RING)) {
4562                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
4563                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
4564                 else
4565                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
4566                 goto abort_iotag_exit;
4567         }
4568
4569         /* issue ABTS for this IOCB based on iotag */
4570         abtsiocbp = __lpfc_sli_get_iocbq(phba);
4571         if (abtsiocbp == NULL)
4572                 return 0;
4573
4574         /* This signals the response to set the correct status
4575          * before calling the completion handler.
4576          */
4577         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4578
4579         iabt = &abtsiocbp->iocb;
4580         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
4581         iabt->un.acxri.abortContextTag = icmd->ulpContext;
4582         iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
4583         iabt->ulpLe = 1;
4584         iabt->ulpClass = icmd->ulpClass;
4585
4586         if (phba->link_state >= LPFC_LINK_UP)
4587                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
4588         else
4589                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
4590
4591         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
4592
4593         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
4594                          "0339 Abort xri x%x, original iotag x%x, "
4595                          "abort cmd iotag x%x\n",
4596                          iabt->un.acxri.abortContextTag,
4597                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
4598         retval = __lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0);
4599
4600         if (retval)
4601                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
4602 abort_iotag_exit:
4603         /*
4604          * Caller to this routine should check for IOCB_ERROR
4605          * and handle it properly.  This routine no longer removes
4606          * iocb off txcmplq and call compl in case of IOCB_ERROR.
4607          */
4608         return retval;
4609 }
4610
4611 /**
4612  * lpfc_sli_validate_fcp_iocb: Filtering function, used to find commands
4613  *                    associated with a vport/SCSI target/lun.
4614  * @iocbq: Pointer to driver iocb object.
4615  * @vport: Pointer to driver virtual port object.
4616  * @tgt_id: SCSI ID of the target.
4617  * @lun_id: LUN ID of the scsi device.
4618  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
4619  *
4620  * This function acts as iocb filter for functions which abort or count
4621  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
4622  * 0 if the filtering criteria is met for the given iocb and will return
4623  * 1 if the filtering criteria is not met.
4624  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
4625  * given iocb is for the SCSI device specified by vport, tgt_id and
4626  * lun_id parameter.
4627  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
4628  * given iocb is for the SCSI target specified by vport and tgt_id
4629  * parameters.
4630  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
4631  * given iocb is for the SCSI host associated with the given vport.
4632  * This function is called with no locks held.
4633  **/
4634 static int
4635 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
4636                            uint16_t tgt_id, uint64_t lun_id,
4637                            lpfc_ctx_cmd ctx_cmd)
4638 {
4639         struct lpfc_scsi_buf *lpfc_cmd;
4640         int rc = 1;
4641
4642         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
4643                 return rc;
4644
4645         if (iocbq->vport != vport)
4646                 return rc;
4647
4648         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
4649
4650         if (lpfc_cmd->pCmd == NULL)
4651                 return rc;
4652
4653         switch (ctx_cmd) {
4654         case LPFC_CTX_LUN:
4655                 if ((lpfc_cmd->rdata->pnode) &&
4656                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
4657                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
4658                         rc = 0;
4659                 break;
4660         case LPFC_CTX_TGT:
4661                 if ((lpfc_cmd->rdata->pnode) &&
4662                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
4663                         rc = 0;
4664                 break;
4665         case LPFC_CTX_HOST:
4666                 rc = 0;
4667                 break;
4668         default:
4669                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
4670                         __func__, ctx_cmd);
4671                 break;
4672         }
4673
4674         return rc;
4675 }
4676
4677 /**
4678  * lpfc_sli_sum_iocb: Function to count the number of FCP iocbs pending.
4679  * @vport: Pointer to virtual port.
4680  * @tgt_id: SCSI ID of the target.
4681  * @lun_id: LUN ID of the scsi device.
4682  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4683  *
4684  * This function returns number of FCP commands pending for the vport.
4685  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
4686  * commands pending on the vport associated with SCSI device specified
4687  * by tgt_id and lun_id parameters.
4688  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
4689  * commands pending on the vport associated with SCSI target specified
4690  * by tgt_id parameter.
4691  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
4692  * commands pending on the vport.
4693  * This function returns the number of iocbs which satisfy the filter.
4694  * This function is called without any lock held.
4695  **/
4696 int
4697 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
4698                   lpfc_ctx_cmd ctx_cmd)
4699 {
4700         struct lpfc_hba *phba = vport->phba;
4701         struct lpfc_iocbq *iocbq;
4702         int sum, i;
4703
4704         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
4705                 iocbq = phba->sli.iocbq_lookup[i];
4706
4707                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
4708                                                 ctx_cmd) == 0)
4709                         sum++;
4710         }
4711
4712         return sum;
4713 }
4714
4715 /**
4716  * lpfc_sli_abort_fcp_cmpl: Completion handler function for an aborted
4717  *       FCP iocb.
4718  * @phba: Pointer to HBA context object
4719  * @cmdiocb: Pointer to command iocb object.
4720  * @rspiocb: Pointer to response iocb object.
4721  *
4722  * This function is called when an aborted FCP iocb completes. This
4723  * function is called by the ring event handler with no lock held.
4724  * This function frees the iocb.
4725  **/
4726 void
4727 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
4728                         struct lpfc_iocbq *rspiocb)
4729 {
4730         lpfc_sli_release_iocbq(phba, cmdiocb);
4731         return;
4732 }
4733
4734 /**
4735  * lpfc_sli_abort_iocb: This function issue abort for all SCSI commands
4736  *          pending on a SCSI host(vport)/target/lun.
4737  * @vport: Pointer to virtual port.
4738  * @pring: Pointer to driver SLI ring object.
4739  * @tgt_id: SCSI ID of the target.
4740  * @lun_id: LUN ID of the scsi device.
4741  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
4742  *
4743  * This function sends an abort command for every SCSI command
4744  * associated with the given virtual port pending on the ring
4745  * filtered by lpfc_sli_validate_fcp_iocb function.
4746  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
4747  * FCP iocbs associated with lun specified by tgt_id and lun_id
4748  * parameters
4749  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
4750  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
4751  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
4752  * FCP iocbs associated with virtual port.
4753  * This function returns number of iocbs it failed to abort.
4754  * This function is called with no locks held.
4755  **/
4756 int
4757 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
4758                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
4759 {
4760         struct lpfc_hba *phba = vport->phba;
4761         struct lpfc_iocbq *iocbq;
4762         struct lpfc_iocbq *abtsiocb;
4763         IOCB_t *cmd = NULL;
4764         int errcnt = 0, ret_val = 0;
4765         int i;
4766
4767         for (i = 1; i <= phba->sli.last_iotag; i++) {
4768                 iocbq = phba->sli.iocbq_lookup[i];
4769
4770                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
4771                                                abort_cmd) != 0)
4772                         continue;
4773
4774                 /* issue ABTS for this IOCB based on iotag */
4775                 abtsiocb = lpfc_sli_get_iocbq(phba);
4776                 if (abtsiocb == NULL) {
4777                         errcnt++;
4778                         continue;
4779                 }
4780
4781                 cmd = &iocbq->iocb;
4782                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
4783                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
4784                 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
4785                 abtsiocb->iocb.ulpLe = 1;
4786                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
4787                 abtsiocb->vport = phba->pport;
4788
4789                 if (lpfc_is_link_up(phba))
4790                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
4791                 else
4792                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
4793
4794                 /* Setup callback routine and issue the command. */
4795                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4796                 ret_val = lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0);
4797                 if (ret_val == IOCB_ERROR) {
4798                         lpfc_sli_release_iocbq(phba, abtsiocb);
4799                         errcnt++;
4800                         continue;
4801                 }
4802         }
4803
4804         return errcnt;
4805 }
4806
4807 /**
4808  * lpfc_sli_wake_iocb_wait: iocb completion handler for iocb issued using
4809  *                          lpfc_sli_issue_iocb_wait.
4810  * @phba: Pointer to HBA context object.
4811  * @cmdiocbq: Pointer to command iocb.
4812  * @rspiocbq: Pointer to response iocb.
4813  *
4814  * This function is the completion handler for iocbs issued using
4815  * lpfc_sli_issue_iocb_wait function. This function is called by the
4816  * ring event handler function without any lock held. This function
4817  * can be called from both worker thread context and interrupt
4818  * context. This function also can be called from other thread which
4819  * cleans up the SLI layer objects.
4820  * This function copy the contents of the response iocb to the
4821  * response iocb memory object provided by the caller of
4822  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
4823  * sleeps for the iocb completion.
4824  **/
4825 static void
4826 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
4827                         struct lpfc_iocbq *cmdiocbq,
4828                         struct lpfc_iocbq *rspiocbq)
4829 {
4830         wait_queue_head_t *pdone_q;
4831         unsigned long iflags;
4832
4833         spin_lock_irqsave(&phba->hbalock, iflags);
4834         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
4835         if (cmdiocbq->context2 && rspiocbq)
4836                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
4837                        &rspiocbq->iocb, sizeof(IOCB_t));
4838
4839         pdone_q = cmdiocbq->context_un.wait_queue;
4840         if (pdone_q)
4841                 wake_up(pdone_q);
4842         spin_unlock_irqrestore(&phba->hbalock, iflags);
4843         return;
4844 }
4845
4846 /**
4847  * lpfc_sli_issue_iocb_wait: Synchronous function to issue iocb commands.
4848  * @phba: Pointer to HBA context object..
4849  * @pring: Pointer to sli ring.
4850  * @piocb: Pointer to command iocb.
4851  * @prspiocbq: Pointer to response iocb.
4852  * @timeout: Timeout in number of seconds.
4853  *
4854  * This function issues the iocb to firmware and waits for the
4855  * iocb to complete. If the iocb command is not
4856  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
4857  * Caller should not free the iocb resources if this function
4858  * returns IOCB_TIMEDOUT.
4859  * The function waits for the iocb completion using an
4860  * non-interruptible wait.
4861  * This function will sleep while waiting for iocb completion.
4862  * So, this function should not be called from any context which
4863  * does not allow sleeping. Due to the same reason, this function
4864  * cannot be called with interrupt disabled.
4865  * This function assumes that the iocb completions occur while
4866  * this function sleep. So, this function cannot be called from
4867  * the thread which process iocb completion for this ring.
4868  * This function clears the iocb_flag of the iocb object before
4869  * issuing the iocb and the iocb completion handler sets this
4870  * flag and wakes this thread when the iocb completes.
4871  * The contents of the response iocb will be copied to prspiocbq
4872  * by the completion handler when the command completes.
4873  * This function returns IOCB_SUCCESS when success.
4874  * This function is called with no lock held.
4875  **/
4876 int
4877 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
4878                          struct lpfc_sli_ring *pring,
4879                          struct lpfc_iocbq *piocb,
4880                          struct lpfc_iocbq *prspiocbq,
4881                          uint32_t timeout)
4882 {
4883         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
4884         long timeleft, timeout_req = 0;
4885         int retval = IOCB_SUCCESS;
4886         uint32_t creg_val;
4887
4888         /*
4889          * If the caller has provided a response iocbq buffer, then context2
4890          * is NULL or its an error.
4891          */
4892         if (prspiocbq) {
4893                 if (piocb->context2)
4894                         return IOCB_ERROR;
4895                 piocb->context2 = prspiocbq;
4896         }
4897
4898         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
4899         piocb->context_un.wait_queue = &done_q;
4900         piocb->iocb_flag &= ~LPFC_IO_WAKE;
4901
4902         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4903                 creg_val = readl(phba->HCregaddr);
4904                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
4905                 writel(creg_val, phba->HCregaddr);
4906                 readl(phba->HCregaddr); /* flush */
4907         }
4908
4909         retval = lpfc_sli_issue_iocb(phba, pring, piocb, 0);
4910         if (retval == IOCB_SUCCESS) {
4911                 timeout_req = timeout * HZ;
4912                 timeleft = wait_event_timeout(done_q,
4913                                 piocb->iocb_flag & LPFC_IO_WAKE,
4914                                 timeout_req);
4915
4916                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
4917                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4918                                         "0331 IOCB wake signaled\n");
4919                 } else if (timeleft == 0) {
4920                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4921                                         "0338 IOCB wait timeout error - no "
4922                                         "wake response Data x%x\n", timeout);
4923                         retval = IOCB_TIMEDOUT;
4924                 } else {
4925                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4926                                         "0330 IOCB wake NOT set, "
4927                                         "Data x%x x%lx\n",
4928                                         timeout, (timeleft / jiffies));
4929                         retval = IOCB_TIMEDOUT;
4930                 }
4931         } else {
4932                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4933                                 "0332 IOCB wait issue failed, Data x%x\n",
4934                                 retval);
4935                 retval = IOCB_ERROR;
4936         }
4937
4938         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
4939                 creg_val = readl(phba->HCregaddr);
4940                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
4941                 writel(creg_val, phba->HCregaddr);
4942                 readl(phba->HCregaddr); /* flush */
4943         }
4944
4945         if (prspiocbq)
4946                 piocb->context2 = NULL;
4947
4948         piocb->context_un.wait_queue = NULL;
4949         piocb->iocb_cmpl = NULL;
4950         return retval;
4951 }
4952
4953 /**
4954  * lpfc_sli_issue_mbox_wait: Synchronous function to issue mailbox.
4955  * @phba: Pointer to HBA context object.
4956  * @pmboxq: Pointer to driver mailbox object.
4957  * @timeout: Timeout in number of seconds.
4958  *
4959  * This function issues the mailbox to firmware and waits for the
4960  * mailbox command to complete. If the mailbox command is not
4961  * completed within timeout seconds, it returns MBX_TIMEOUT.
4962  * The function waits for the mailbox completion using an
4963  * interruptible wait. If the thread is woken up due to a
4964  * signal, MBX_TIMEOUT error is returned to the caller. Caller
4965  * should not free the mailbox resources, if this function returns
4966  * MBX_TIMEOUT.
4967  * This function will sleep while waiting for mailbox completion.
4968  * So, this function should not be called from any context which
4969  * does not allow sleeping. Due to the same reason, this function
4970  * cannot be called with interrupt disabled.
4971  * This function assumes that the mailbox completion occurs while
4972  * this function sleep. So, this function cannot be called from
4973  * the worker thread which processes mailbox completion.
4974  * This function is called in the context of HBA management
4975  * applications.
4976  * This function returns MBX_SUCCESS when successful.
4977  * This function is called with no lock held.
4978  **/
4979 int
4980 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
4981                          uint32_t timeout)
4982 {
4983         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
4984         int retval;
4985         unsigned long flag;
4986
4987         /* The caller must leave context1 empty. */
4988         if (pmboxq->context1)
4989                 return MBX_NOT_FINISHED;
4990
4991         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
4992         /* setup wake call as IOCB callback */
4993         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
4994         /* setup context field to pass wait_queue pointer to wake function  */
4995         pmboxq->context1 = &done_q;
4996
4997         /* now issue the command */
4998         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
4999
5000         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
5001                 wait_event_interruptible_timeout(done_q,
5002                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
5003                                 timeout * HZ);
5004
5005                 spin_lock_irqsave(&phba->hbalock, flag);
5006                 pmboxq->context1 = NULL;
5007                 /*
5008                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
5009                  * else do not free the resources.
5010                  */
5011                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
5012                         retval = MBX_SUCCESS;
5013                 else {
5014                         retval = MBX_TIMEOUT;
5015                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
5016                 }
5017                 spin_unlock_irqrestore(&phba->hbalock, flag);
5018         }
5019
5020         return retval;
5021 }
5022
5023 /**
5024  * lpfc_sli_flush_mbox_queue: mailbox queue cleanup function.
5025  * @phba: Pointer to HBA context.
5026  *
5027  * This function is called to cleanup any pending mailbox
5028  * objects in the driver queue before bringing the HBA offline.
5029  * This function is called while resetting the HBA.
5030  * The function is called without any lock held. The function
5031  * takes hbalock to update SLI data structure.
5032  * This function returns 1 when there is an active mailbox
5033  * command pending else returns 0.
5034  **/
5035 int
5036 lpfc_sli_flush_mbox_queue(struct lpfc_hba * phba)
5037 {
5038         struct lpfc_vport *vport = phba->pport;
5039         int i = 0;
5040         uint32_t ha_copy;
5041
5042         while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE && !vport->stopped) {
5043                 if (i++ > LPFC_MBOX_TMO * 1000)
5044                         return 1;
5045
5046                 /*
5047                  * Call lpfc_sli_handle_mb_event only if a mailbox cmd
5048                  * did finish. This way we won't get the misleading
5049                  * "Stray Mailbox Interrupt" message.
5050                  */
5051                 spin_lock_irq(&phba->hbalock);
5052                 ha_copy = phba->work_ha;
5053                 phba->work_ha &= ~HA_MBATT;
5054                 spin_unlock_irq(&phba->hbalock);
5055
5056                 if (ha_copy & HA_MBATT)
5057                         if (lpfc_sli_handle_mb_event(phba) == 0)
5058                                 i = 0;
5059
5060                 msleep(1);
5061         }
5062
5063         return (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) ? 1 : 0;
5064 }
5065
5066 /**
5067  * lpfc_intr_handler: The interrupt handler of lpfc driver.
5068  * @irq: Interrupt number.
5069  * @dev_id: The device context pointer.
5070  *
5071  * This function is called from the PCI layer when there is
5072  * an event in the HBA which requires driver attention. When
5073  * the PCI slot is in error recovery or the HBA is undergoing
5074  * initialization the interrupt handler will not process the
5075  * interrupt.
5076  * The error attention, link attention and els ring attention
5077  * events are handled by the worker thread. The interrupt
5078  * handler signals the worker thread and returns for these
5079  * events.
5080  * The SCSI ring event and mailbox events are handled in the
5081  * interrupt context.
5082  * This function is called without any lock held. It gets the
5083  * hbalock to access and update SLI data structures.
5084  * This function returns IRQ_HANDLED when interrupt is handled
5085  * else it returns IRQ_NONE.
5086  **/
5087 irqreturn_t
5088 lpfc_intr_handler(int irq, void *dev_id)
5089 {
5090         struct lpfc_hba  *phba;
5091         uint32_t ha_copy;
5092         uint32_t work_ha_copy;
5093         unsigned long status;
5094         uint32_t control;
5095
5096         MAILBOX_t *mbox, *pmbox;
5097         struct lpfc_vport *vport;
5098         struct lpfc_nodelist *ndlp;
5099         struct lpfc_dmabuf *mp;
5100         LPFC_MBOXQ_t *pmb;
5101         int rc;
5102
5103         /*
5104          * Get the driver's phba structure from the dev_id and
5105          * assume the HBA is not interrupting.
5106          */
5107         phba = (struct lpfc_hba *) dev_id;
5108
5109         if (unlikely(!phba))
5110                 return IRQ_NONE;
5111
5112         /* If the pci channel is offline, ignore all the interrupts. */
5113         if (unlikely(pci_channel_offline(phba->pcidev)))
5114                 return IRQ_NONE;
5115
5116         phba->sli.slistat.sli_intr++;
5117
5118         /*
5119          * Call the HBA to see if it is interrupting.  If not, don't claim
5120          * the interrupt
5121          */
5122
5123         /* Ignore all interrupts during initialization. */
5124         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5125                 return IRQ_NONE;
5126
5127         /*
5128          * Read host attention register to determine interrupt source
5129          * Clear Attention Sources, except Error Attention (to
5130          * preserve status) and Link Attention
5131          */
5132         spin_lock(&phba->hbalock);
5133         if (phba->sli3_options & LPFC_SLI3_INB_ENABLED &&
5134             (phba->inb_last_counter != *phba->inb_counter)) {
5135                 phba->inb_last_counter = *phba->inb_counter;
5136                 ha_copy = le32_to_cpu(*phba->inb_ha_copy);
5137         } else
5138                 ha_copy = readl(phba->HAregaddr);
5139         if (unlikely(!ha_copy)) {
5140                 spin_unlock(&phba->hbalock);
5141                 return IRQ_NONE;
5142         }
5143         /* If somebody is waiting to handle an eratt don't process it
5144          * here.  The brdkill function will do this.
5145          */
5146         if (phba->link_flag & LS_IGNORE_ERATT)
5147                 ha_copy &= ~HA_ERATT;
5148         writel((ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
5149         readl(phba->HAregaddr); /* flush */
5150         spin_unlock(&phba->hbalock);
5151
5152         work_ha_copy = ha_copy & phba->work_ha_mask;
5153
5154         if (unlikely(work_ha_copy)) {
5155                 if (work_ha_copy & HA_LATT) {
5156                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
5157                                 /*
5158                                  * Turn off Link Attention interrupts
5159                                  * until CLEAR_LA done
5160                                  */
5161                                 spin_lock(&phba->hbalock);
5162                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
5163                                 control = readl(phba->HCregaddr);
5164                                 control &= ~HC_LAINT_ENA;
5165                                 writel(control, phba->HCregaddr);
5166                                 readl(phba->HCregaddr); /* flush */
5167                                 spin_unlock(&phba->hbalock);
5168                         }
5169                         else
5170                                 work_ha_copy &= ~HA_LATT;
5171                 }
5172
5173                 if (work_ha_copy & ~(HA_ERATT|HA_MBATT|HA_LATT)) {
5174                         /*
5175                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
5176                          * the only slow ring.
5177                          */
5178                         status = (work_ha_copy &
5179                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
5180                         status >>= (4*LPFC_ELS_RING);
5181                         if (status & HA_RXMASK) {
5182                                 spin_lock(&phba->hbalock);
5183                                 control = readl(phba->HCregaddr);
5184
5185                                 lpfc_debugfs_slow_ring_trc(phba,
5186                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
5187                                 control, status,
5188                                 (uint32_t)phba->sli.slistat.sli_intr);
5189
5190                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
5191                                         lpfc_debugfs_slow_ring_trc(phba,
5192                                                 "ISR Disable ring:"
5193                                                 "pwork:x%x hawork:x%x wait:x%x",
5194                                                 phba->work_ha, work_ha_copy,
5195                                                 (uint32_t)((unsigned long)
5196                                                 &phba->work_waitq));
5197
5198                                         control &=
5199                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
5200                                         writel(control, phba->HCregaddr);
5201                                         readl(phba->HCregaddr); /* flush */
5202                                 }
5203                                 else {
5204                                         lpfc_debugfs_slow_ring_trc(phba,
5205                                                 "ISR slow ring:   pwork:"
5206                                                 "x%x hawork:x%x wait:x%x",
5207                                                 phba->work_ha, work_ha_copy,
5208                                                 (uint32_t)((unsigned long)
5209                                                 &phba->work_waitq));
5210                                 }
5211                                 spin_unlock(&phba->hbalock);
5212                         }
5213                 }
5214
5215                 if (work_ha_copy & HA_ERATT) {
5216                         /*
5217                          * There was a link/board error.  Read the
5218                          * status register to retrieve the error event
5219                          * and process it.
5220                          */
5221                         phba->sli.slistat.err_attn_event++;
5222                         /* Save status info */
5223                         phba->work_hs = readl(phba->HSregaddr);
5224                         phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
5225                         phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
5226
5227                         /* Clear Chip error bit */
5228                         writel(HA_ERATT, phba->HAregaddr);
5229                         readl(phba->HAregaddr); /* flush */
5230                         phba->pport->stopped = 1;
5231                 }
5232
5233                 spin_lock(&phba->hbalock);
5234                 if ((work_ha_copy & HA_MBATT) &&
5235                     (phba->sli.mbox_active)) {
5236                         pmb = phba->sli.mbox_active;
5237                         pmbox = &pmb->mb;
5238                         mbox = phba->mbox;
5239                         vport = pmb->vport;
5240
5241                         /* First check out the status word */
5242                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
5243                         if (pmbox->mbxOwner != OWN_HOST) {
5244                                 spin_unlock(&phba->hbalock);
5245                                 /*
5246                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
5247                                  * mbxStatus <status>
5248                                  */
5249                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5250                                                 LOG_SLI,
5251                                                 "(%d):0304 Stray Mailbox "
5252                                                 "Interrupt mbxCommand x%x "
5253                                                 "mbxStatus x%x\n",
5254                                                 (vport ? vport->vpi : 0),
5255                                                 pmbox->mbxCommand,
5256                                                 pmbox->mbxStatus);
5257                                 /* clear mailbox attention bit */
5258                                 work_ha_copy &= ~HA_MBATT;
5259                         } else {
5260                                 phba->sli.mbox_active = NULL;
5261                                 spin_unlock(&phba->hbalock);
5262                                 phba->last_completion_time = jiffies;
5263                                 del_timer(&phba->sli.mbox_tmo);
5264                                 if (pmb->mbox_cmpl) {
5265                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
5266                                                         MAILBOX_CMD_SIZE);
5267                                 }
5268                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
5269                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
5270
5271                                         lpfc_debugfs_disc_trc(vport,
5272                                                 LPFC_DISC_TRC_MBOX_VPORT,
5273                                                 "MBOX dflt rpi: : "
5274                                                 "status:x%x rpi:x%x",
5275                                                 (uint32_t)pmbox->mbxStatus,
5276                                                 pmbox->un.varWords[0], 0);
5277
5278                                         if (!pmbox->mbxStatus) {
5279                                                 mp = (struct lpfc_dmabuf *)
5280                                                         (pmb->context1);
5281                                                 ndlp = (struct lpfc_nodelist *)
5282                                                         pmb->context2;
5283
5284                                                 /* Reg_LOGIN of dflt RPI was
5285                                                  * successful. new lets get
5286                                                  * rid of the RPI using the
5287                                                  * same mbox buffer.
5288                                                  */
5289                                                 lpfc_unreg_login(phba,
5290                                                         vport->vpi,
5291                                                         pmbox->un.varWords[0],
5292                                                         pmb);
5293                                                 pmb->mbox_cmpl =
5294                                                         lpfc_mbx_cmpl_dflt_rpi;
5295                                                 pmb->context1 = mp;
5296                                                 pmb->context2 = ndlp;
5297                                                 pmb->vport = vport;
5298                                                 rc = lpfc_sli_issue_mbox(phba,
5299                                                                 pmb,
5300                                                                 MBX_NOWAIT);
5301                                                 if (rc != MBX_BUSY)
5302                                                         lpfc_printf_log(phba,
5303                                                         KERN_ERR,
5304                                                         LOG_MBOX | LOG_SLI,
5305                                                         "0350 rc should have"
5306                                                         "been MBX_BUSY");
5307                                                 goto send_current_mbox;
5308                                         }
5309                                 }
5310                                 spin_lock(&phba->pport->work_port_lock);
5311                                 phba->pport->work_port_events &=
5312                                         ~WORKER_MBOX_TMO;
5313                                 spin_unlock(&phba->pport->work_port_lock);
5314                                 lpfc_mbox_cmpl_put(phba, pmb);
5315                         }
5316                 } else
5317                         spin_unlock(&phba->hbalock);
5318                 if ((work_ha_copy & HA_MBATT) &&
5319                     (phba->sli.mbox_active == NULL)) {
5320 send_current_mbox:
5321                         /* Process next mailbox command if there is one */
5322                         do {
5323                                 rc = lpfc_sli_issue_mbox(phba, NULL,
5324                                                          MBX_NOWAIT);
5325                         } while (rc == MBX_NOT_FINISHED);
5326                         if (rc != MBX_SUCCESS)
5327                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
5328                                                 LOG_SLI, "0349 rc should be "
5329                                                 "MBX_SUCCESS");
5330                 }
5331
5332                 spin_lock(&phba->hbalock);
5333                 phba->work_ha |= work_ha_copy;
5334                 spin_unlock(&phba->hbalock);
5335                 lpfc_worker_wake_up(phba);
5336         }
5337
5338         ha_copy &= ~(phba->work_ha_mask);
5339
5340         /*
5341          * Process all events on FCP ring.  Take the optimized path for
5342          * FCP IO.  Any other IO is slow path and is handled by
5343          * the worker thread.
5344          */
5345         status = (ha_copy & (HA_RXMASK  << (4*LPFC_FCP_RING)));
5346         status >>= (4*LPFC_FCP_RING);
5347         if (status & HA_RXMASK)
5348                 lpfc_sli_handle_fast_ring_event(phba,
5349                                                 &phba->sli.ring[LPFC_FCP_RING],
5350                                                 status);
5351
5352         if (phba->cfg_multi_ring_support == 2) {
5353                 /*
5354                  * Process all events on extra ring.  Take the optimized path
5355                  * for extra ring IO.  Any other IO is slow path and is handled
5356                  * by the worker thread.
5357                  */
5358                 status = (ha_copy & (HA_RXMASK  << (4*LPFC_EXTRA_RING)));
5359                 status >>= (4*LPFC_EXTRA_RING);
5360                 if (status & HA_RXMASK) {
5361                         lpfc_sli_handle_fast_ring_event(phba,
5362                                         &phba->sli.ring[LPFC_EXTRA_RING],
5363                                         status);
5364                 }
5365         }
5366         return IRQ_HANDLED;
5367
5368 } /* lpfc_intr_handler */