[SCSI] lpfc 8.3.2 : Addition of SLI4 Interface - Queues
[safe/jmp/linux-2.6] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-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 #include <scsi/fc/fc_fs.h>
33
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.h"
42 #include "lpfc_crtn.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_compat.h"
45 #include "lpfc_debugfs.h"
46
47 /*
48  * Define macro to log: Mailbox command x%x cannot issue Data
49  * This allows multiple uses of lpfc_msgBlk0311
50  * w/o perturbing log msg utility.
51  */
52 #define LOG_MBOX_CANNOT_ISSUE_DATA(phba, pmbox, psli, flag) \
53                         lpfc_printf_log(phba, \
54                                 KERN_INFO, \
55                                 LOG_MBOX | LOG_SLI, \
56                                 "(%d):0311 Mailbox command x%x cannot " \
57                                 "issue Data: x%x x%x x%x\n", \
58                                 pmbox->vport ? pmbox->vport->vpi : 0, \
59                                 pmbox->mb.mbxCommand,           \
60                                 phba->pport->port_state,        \
61                                 psli->sli_flag, \
62                                 flag)
63
64
65 /* There are only four IOCB completion types. */
66 typedef enum _lpfc_iocb_type {
67         LPFC_UNKNOWN_IOCB,
68         LPFC_UNSOL_IOCB,
69         LPFC_SOL_IOCB,
70         LPFC_ABORT_IOCB
71 } lpfc_iocb_type;
72
73
74 /* Provide function prototypes local to this module. */
75 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
76                                   uint32_t);
77 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
78                             uint8_t *, uint32_t *);
79
80 static IOCB_t *
81 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
82 {
83         return &iocbq->iocb;
84 }
85
86 /**
87  * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
88  * @q: The Work Queue to operate on.
89  * @wqe: The work Queue Entry to put on the Work queue.
90  *
91  * This routine will copy the contents of @wqe to the next available entry on
92  * the @q. This function will then ring the Work Queue Doorbell to signal the
93  * HBA to start processing the Work Queue Entry. This function returns 0 if
94  * successful. If no entries are available on @q then this function will return
95  * -ENOMEM.
96  * The caller is expected to hold the hbalock when calling this routine.
97  **/
98 static uint32_t
99 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
100 {
101         union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
102         struct lpfc_register doorbell;
103         uint32_t host_index;
104
105         /* If the host has not yet processed the next entry then we are done */
106         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
107                 return -ENOMEM;
108         /* set consumption flag every once in a while */
109         if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
110                 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
111
112         lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
113
114         /* Update the host index before invoking device */
115         host_index = q->host_index;
116         q->host_index = ((q->host_index + 1) % q->entry_count);
117
118         /* Ring Doorbell */
119         doorbell.word0 = 0;
120         bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
121         bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
122         bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
123         writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
124         readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
125
126         return 0;
127 }
128
129 /**
130  * lpfc_sli4_wq_release - Updates internal hba index for WQ
131  * @q: The Work Queue to operate on.
132  * @index: The index to advance the hba index to.
133  *
134  * This routine will update the HBA index of a queue to reflect consumption of
135  * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
136  * an entry the host calls this function to update the queue's internal
137  * pointers. This routine returns the number of entries that were consumed by
138  * the HBA.
139  **/
140 static uint32_t
141 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
142 {
143         uint32_t released = 0;
144
145         if (q->hba_index == index)
146                 return 0;
147         do {
148                 q->hba_index = ((q->hba_index + 1) % q->entry_count);
149                 released++;
150         } while (q->hba_index != index);
151         return released;
152 }
153
154 /**
155  * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
156  * @q: The Mailbox Queue to operate on.
157  * @wqe: The Mailbox Queue Entry to put on the Work queue.
158  *
159  * This routine will copy the contents of @mqe to the next available entry on
160  * the @q. This function will then ring the Work Queue Doorbell to signal the
161  * HBA to start processing the Work Queue Entry. This function returns 0 if
162  * successful. If no entries are available on @q then this function will return
163  * -ENOMEM.
164  * The caller is expected to hold the hbalock when calling this routine.
165  **/
166 static uint32_t
167 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
168 {
169         struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
170         struct lpfc_register doorbell;
171         uint32_t host_index;
172
173         /* If the host has not yet processed the next entry then we are done */
174         if (((q->host_index + 1) % q->entry_count) == q->hba_index)
175                 return -ENOMEM;
176         lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
177         /* Save off the mailbox pointer for completion */
178         q->phba->mbox = (MAILBOX_t *)temp_mqe;
179
180         /* Update the host index before invoking device */
181         host_index = q->host_index;
182         q->host_index = ((q->host_index + 1) % q->entry_count);
183
184         /* Ring Doorbell */
185         doorbell.word0 = 0;
186         bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
187         bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
188         writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
189         readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
190         return 0;
191 }
192
193 /**
194  * lpfc_sli4_mq_release - Updates internal hba index for MQ
195  * @q: The Mailbox Queue to operate on.
196  *
197  * This routine will update the HBA index of a queue to reflect consumption of
198  * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
199  * an entry the host calls this function to update the queue's internal
200  * pointers. This routine returns the number of entries that were consumed by
201  * the HBA.
202  **/
203 static uint32_t
204 lpfc_sli4_mq_release(struct lpfc_queue *q)
205 {
206         /* Clear the mailbox pointer for completion */
207         q->phba->mbox = NULL;
208         q->hba_index = ((q->hba_index + 1) % q->entry_count);
209         return 1;
210 }
211
212 /**
213  * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
214  * @q: The Event Queue to get the first valid EQE from
215  *
216  * This routine will get the first valid Event Queue Entry from @q, update
217  * the queue's internal hba index, and return the EQE. If no valid EQEs are in
218  * the Queue (no more work to do), or the Queue is full of EQEs that have been
219  * processed, but not popped back to the HBA then this routine will return NULL.
220  **/
221 static struct lpfc_eqe *
222 lpfc_sli4_eq_get(struct lpfc_queue *q)
223 {
224         struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
225
226         /* If the next EQE is not valid then we are done */
227         if (!bf_get(lpfc_eqe_valid, eqe))
228                 return NULL;
229         /* If the host has not yet processed the next entry then we are done */
230         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
231                 return NULL;
232
233         q->hba_index = ((q->hba_index + 1) % q->entry_count);
234         return eqe;
235 }
236
237 /**
238  * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
239  * @q: The Event Queue that the host has completed processing for.
240  * @arm: Indicates whether the host wants to arms this CQ.
241  *
242  * This routine will mark all Event Queue Entries on @q, from the last
243  * known completed entry to the last entry that was processed, as completed
244  * by clearing the valid bit for each completion queue entry. Then it will
245  * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
246  * The internal host index in the @q will be updated by this routine to indicate
247  * that the host has finished processing the entries. The @arm parameter
248  * indicates that the queue should be rearmed when ringing the doorbell.
249  *
250  * This function will return the number of EQEs that were popped.
251  **/
252 uint32_t
253 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
254 {
255         uint32_t released = 0;
256         struct lpfc_eqe *temp_eqe;
257         struct lpfc_register doorbell;
258
259         /* while there are valid entries */
260         while (q->hba_index != q->host_index) {
261                 temp_eqe = q->qe[q->host_index].eqe;
262                 bf_set(lpfc_eqe_valid, temp_eqe, 0);
263                 released++;
264                 q->host_index = ((q->host_index + 1) % q->entry_count);
265         }
266         if (unlikely(released == 0 && !arm))
267                 return 0;
268
269         /* ring doorbell for number popped */
270         doorbell.word0 = 0;
271         if (arm) {
272                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
273                 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
274         }
275         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
276         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
277         bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
278         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
279         return released;
280 }
281
282 /**
283  * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
284  * @q: The Completion Queue to get the first valid CQE from
285  *
286  * This routine will get the first valid Completion Queue Entry from @q, update
287  * the queue's internal hba index, and return the CQE. If no valid CQEs are in
288  * the Queue (no more work to do), or the Queue is full of CQEs that have been
289  * processed, but not popped back to the HBA then this routine will return NULL.
290  **/
291 static struct lpfc_cqe *
292 lpfc_sli4_cq_get(struct lpfc_queue *q)
293 {
294         struct lpfc_cqe *cqe;
295
296         /* If the next CQE is not valid then we are done */
297         if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
298                 return NULL;
299         /* If the host has not yet processed the next entry then we are done */
300         if (((q->hba_index + 1) % q->entry_count) == q->host_index)
301                 return NULL;
302
303         cqe = q->qe[q->hba_index].cqe;
304         q->hba_index = ((q->hba_index + 1) % q->entry_count);
305         return cqe;
306 }
307
308 /**
309  * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
310  * @q: The Completion Queue that the host has completed processing for.
311  * @arm: Indicates whether the host wants to arms this CQ.
312  *
313  * This routine will mark all Completion queue entries on @q, from the last
314  * known completed entry to the last entry that was processed, as completed
315  * by clearing the valid bit for each completion queue entry. Then it will
316  * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
317  * The internal host index in the @q will be updated by this routine to indicate
318  * that the host has finished processing the entries. The @arm parameter
319  * indicates that the queue should be rearmed when ringing the doorbell.
320  *
321  * This function will return the number of CQEs that were released.
322  **/
323 uint32_t
324 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
325 {
326         uint32_t released = 0;
327         struct lpfc_cqe *temp_qe;
328         struct lpfc_register doorbell;
329
330         /* while there are valid entries */
331         while (q->hba_index != q->host_index) {
332                 temp_qe = q->qe[q->host_index].cqe;
333                 bf_set(lpfc_cqe_valid, temp_qe, 0);
334                 released++;
335                 q->host_index = ((q->host_index + 1) % q->entry_count);
336         }
337         if (unlikely(released == 0 && !arm))
338                 return 0;
339
340         /* ring doorbell for number popped */
341         doorbell.word0 = 0;
342         if (arm)
343                 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
344         bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
345         bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
346         bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
347         writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
348         return released;
349 }
350
351 /**
352  * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
353  * @q: The Header Receive Queue to operate on.
354  * @wqe: The Receive Queue Entry to put on the Receive queue.
355  *
356  * This routine will copy the contents of @wqe to the next available entry on
357  * the @q. This function will then ring the Receive Queue Doorbell to signal the
358  * HBA to start processing the Receive Queue Entry. This function returns the
359  * index that the rqe was copied to if successful. If no entries are available
360  * on @q then this function will return -ENOMEM.
361  * The caller is expected to hold the hbalock when calling this routine.
362  **/
363 static int
364 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
365                  struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
366 {
367         struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
368         struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
369         struct lpfc_register doorbell;
370         int put_index = hq->host_index;
371
372         if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
373                 return -EINVAL;
374         if (hq->host_index != dq->host_index)
375                 return -EINVAL;
376         /* If the host has not yet processed the next entry then we are done */
377         if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
378                 return -EBUSY;
379         lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
380         lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
381
382         /* Update the host index to point to the next slot */
383         hq->host_index = ((hq->host_index + 1) % hq->entry_count);
384         dq->host_index = ((dq->host_index + 1) % dq->entry_count);
385
386         /* Ring The Header Receive Queue Doorbell */
387         if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
388                 doorbell.word0 = 0;
389                 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
390                        LPFC_RQ_POST_BATCH);
391                 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
392                 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
393         }
394         return put_index;
395 }
396
397 /**
398  * lpfc_sli4_rq_release - Updates internal hba index for RQ
399  * @q: The Header Receive Queue to operate on.
400  *
401  * This routine will update the HBA index of a queue to reflect consumption of
402  * one Receive Queue Entry by the HBA. When the HBA indicates that it has
403  * consumed an entry the host calls this function to update the queue's
404  * internal pointers. This routine returns the number of entries that were
405  * consumed by the HBA.
406  **/
407 static uint32_t
408 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
409 {
410         if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
411                 return 0;
412         hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
413         dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
414         return 1;
415 }
416
417 /**
418  * lpfc_cmd_iocb - Get next command iocb entry in the ring
419  * @phba: Pointer to HBA context object.
420  * @pring: Pointer to driver SLI ring object.
421  *
422  * This function returns pointer to next command iocb entry
423  * in the command ring. The caller must hold hbalock to prevent
424  * other threads consume the next command iocb.
425  * SLI-2/SLI-3 provide different sized iocbs.
426  **/
427 static inline IOCB_t *
428 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
429 {
430         return (IOCB_t *) (((char *) pring->cmdringaddr) +
431                            pring->cmdidx * phba->iocb_cmd_size);
432 }
433
434 /**
435  * lpfc_resp_iocb - Get next response iocb entry in the ring
436  * @phba: Pointer to HBA context object.
437  * @pring: Pointer to driver SLI ring object.
438  *
439  * This function returns pointer to next response iocb entry
440  * in the response ring. The caller must hold hbalock to make sure
441  * that no other thread consume the next response iocb.
442  * SLI-2/SLI-3 provide different sized iocbs.
443  **/
444 static inline IOCB_t *
445 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
446 {
447         return (IOCB_t *) (((char *) pring->rspringaddr) +
448                            pring->rspidx * phba->iocb_rsp_size);
449 }
450
451 /**
452  * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
453  * @phba: Pointer to HBA context object.
454  *
455  * This function is called with hbalock held. This function
456  * allocates a new driver iocb object from the iocb pool. If the
457  * allocation is successful, it returns pointer to the newly
458  * allocated iocb object else it returns NULL.
459  **/
460 static struct lpfc_iocbq *
461 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
462 {
463         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
464         struct lpfc_iocbq * iocbq = NULL;
465
466         list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
467         return iocbq;
468 }
469
470 /**
471  * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
472  * @phba: Pointer to HBA context object.
473  * @xritag: XRI value.
474  *
475  * This function clears the sglq pointer from the array of acive
476  * sglq's. The xritag that is passed in is used to index into the
477  * array. Before the xritag can be used it needs to be adjusted
478  * by subtracting the xribase.
479  *
480  * Returns sglq ponter = success, NULL = Failure.
481  **/
482 static struct lpfc_sglq *
483 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
484 {
485         uint16_t adj_xri;
486         struct lpfc_sglq *sglq;
487         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
488         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
489                 return NULL;
490         sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
491         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
492         return sglq;
493 }
494
495 /**
496  * __lpfc_get_active_sglq - Get the active sglq for this XRI.
497  * @phba: Pointer to HBA context object.
498  * @xritag: XRI value.
499  *
500  * This function returns the sglq pointer from the array of acive
501  * sglq's. The xritag that is passed in is used to index into the
502  * array. Before the xritag can be used it needs to be adjusted
503  * by subtracting the xribase.
504  *
505  * Returns sglq ponter = success, NULL = Failure.
506  **/
507 static struct lpfc_sglq *
508 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
509 {
510         uint16_t adj_xri;
511         struct lpfc_sglq *sglq;
512         adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
513         if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
514                 return NULL;
515         sglq =  phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
516         return sglq;
517 }
518
519 /**
520  * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
521  * @phba: Pointer to HBA context object.
522  *
523  * This function is called with hbalock held. This function
524  * Gets a new driver sglq object from the sglq list. If the
525  * list is not empty then it is successful, it returns pointer to the newly
526  * allocated sglq object else it returns NULL.
527  **/
528 static struct lpfc_sglq *
529 __lpfc_sli_get_sglq(struct lpfc_hba *phba)
530 {
531         struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
532         struct lpfc_sglq *sglq = NULL;
533         uint16_t adj_xri;
534         list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
535         adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
536         phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
537         return sglq;
538 }
539
540 /**
541  * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
542  * @phba: Pointer to HBA context object.
543  *
544  * This function is called with no lock held. This function
545  * allocates a new driver iocb object from the iocb pool. If the
546  * allocation is successful, it returns pointer to the newly
547  * allocated iocb object else it returns NULL.
548  **/
549 struct lpfc_iocbq *
550 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
551 {
552         struct lpfc_iocbq * iocbq = NULL;
553         unsigned long iflags;
554
555         spin_lock_irqsave(&phba->hbalock, iflags);
556         iocbq = __lpfc_sli_get_iocbq(phba);
557         spin_unlock_irqrestore(&phba->hbalock, iflags);
558         return iocbq;
559 }
560
561 /**
562  * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
563  * @phba: Pointer to HBA context object.
564  * @iocbq: Pointer to driver iocb object.
565  *
566  * This function is called with hbalock held to release driver
567  * iocb object to the iocb pool. The iotag in the iocb object
568  * does not change for each use of the iocb object. This function
569  * clears all other fields of the iocb object when it is freed.
570  * The sqlq structure that holds the xritag and phys and virtual
571  * mappings for the scatter gather list is retrieved from the
572  * active array of sglq. The get of the sglq pointer also clears
573  * the entry in the array. If the status of the IO indiactes that
574  * this IO was aborted then the sglq entry it put on the
575  * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
576  * IO has good status or fails for any other reason then the sglq
577  * entry is added to the free list (lpfc_sgl_list).
578  **/
579 static void
580 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
581 {
582         struct lpfc_sglq *sglq;
583         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
584         unsigned long iflag;
585
586         if (iocbq->sli4_xritag == NO_XRI)
587                 sglq = NULL;
588         else
589                 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
590         if (sglq)  {
591                 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
592                         || ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
593                         && (iocbq->iocb.un.ulpWord[4]
594                                 == IOERR_SLI_ABORTED))) {
595                         spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
596                                         iflag);
597                         list_add(&sglq->list,
598                                 &phba->sli4_hba.lpfc_abts_els_sgl_list);
599                         spin_unlock_irqrestore(
600                                 &phba->sli4_hba.abts_sgl_list_lock, iflag);
601                 } else
602                         list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
603         }
604
605
606         /*
607          * Clean all volatile data fields, preserve iotag and node struct.
608          */
609         memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
610         iocbq->sli4_xritag = NO_XRI;
611         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
612 }
613
614 /**
615  * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
616  * @phba: Pointer to HBA context object.
617  * @iocbq: Pointer to driver iocb object.
618  *
619  * This function is called with hbalock held to release driver
620  * iocb object to the iocb pool. The iotag in the iocb object
621  * does not change for each use of the iocb object. This function
622  * clears all other fields of the iocb object when it is freed.
623  **/
624 static void
625 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
626 {
627         size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
628
629         /*
630          * Clean all volatile data fields, preserve iotag and node struct.
631          */
632         memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
633         iocbq->sli4_xritag = NO_XRI;
634         list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
635 }
636
637 /**
638  * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
639  * @phba: Pointer to HBA context object.
640  * @iocbq: Pointer to driver iocb object.
641  *
642  * This function is called with hbalock held to release driver
643  * iocb object to the iocb pool. The iotag in the iocb object
644  * does not change for each use of the iocb object. This function
645  * clears all other fields of the iocb object when it is freed.
646  **/
647 static void
648 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
649 {
650         phba->__lpfc_sli_release_iocbq(phba, iocbq);
651 }
652
653 /**
654  * lpfc_sli_release_iocbq - Release iocb to the iocb pool
655  * @phba: Pointer to HBA context object.
656  * @iocbq: Pointer to driver iocb object.
657  *
658  * This function is called with no lock held to release the iocb to
659  * iocb pool.
660  **/
661 void
662 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
663 {
664         unsigned long iflags;
665
666         /*
667          * Clean all volatile data fields, preserve iotag and node struct.
668          */
669         spin_lock_irqsave(&phba->hbalock, iflags);
670         __lpfc_sli_release_iocbq(phba, iocbq);
671         spin_unlock_irqrestore(&phba->hbalock, iflags);
672 }
673
674 /**
675  * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
676  * @phba: Pointer to HBA context object.
677  * @iocblist: List of IOCBs.
678  * @ulpstatus: ULP status in IOCB command field.
679  * @ulpWord4: ULP word-4 in IOCB command field.
680  *
681  * This function is called with a list of IOCBs to cancel. It cancels the IOCB
682  * on the list by invoking the complete callback function associated with the
683  * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
684  * fields.
685  **/
686 void
687 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
688                       uint32_t ulpstatus, uint32_t ulpWord4)
689 {
690         struct lpfc_iocbq *piocb;
691
692         while (!list_empty(iocblist)) {
693                 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
694
695                 if (!piocb->iocb_cmpl)
696                         lpfc_sli_release_iocbq(phba, piocb);
697                 else {
698                         piocb->iocb.ulpStatus = ulpstatus;
699                         piocb->iocb.un.ulpWord[4] = ulpWord4;
700                         (piocb->iocb_cmpl) (phba, piocb, piocb);
701                 }
702         }
703         return;
704 }
705
706 /**
707  * lpfc_sli_iocb_cmd_type - Get the iocb type
708  * @iocb_cmnd: iocb command code.
709  *
710  * This function is called by ring event handler function to get the iocb type.
711  * This function translates the iocb command to an iocb command type used to
712  * decide the final disposition of each completed IOCB.
713  * The function returns
714  * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
715  * LPFC_SOL_IOCB     if it is a solicited iocb completion
716  * LPFC_ABORT_IOCB   if it is an abort iocb
717  * LPFC_UNSOL_IOCB   if it is an unsolicited iocb
718  *
719  * The caller is not required to hold any lock.
720  **/
721 static lpfc_iocb_type
722 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
723 {
724         lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
725
726         if (iocb_cmnd > CMD_MAX_IOCB_CMD)
727                 return 0;
728
729         switch (iocb_cmnd) {
730         case CMD_XMIT_SEQUENCE_CR:
731         case CMD_XMIT_SEQUENCE_CX:
732         case CMD_XMIT_BCAST_CN:
733         case CMD_XMIT_BCAST_CX:
734         case CMD_ELS_REQUEST_CR:
735         case CMD_ELS_REQUEST_CX:
736         case CMD_CREATE_XRI_CR:
737         case CMD_CREATE_XRI_CX:
738         case CMD_GET_RPI_CN:
739         case CMD_XMIT_ELS_RSP_CX:
740         case CMD_GET_RPI_CR:
741         case CMD_FCP_IWRITE_CR:
742         case CMD_FCP_IWRITE_CX:
743         case CMD_FCP_IREAD_CR:
744         case CMD_FCP_IREAD_CX:
745         case CMD_FCP_ICMND_CR:
746         case CMD_FCP_ICMND_CX:
747         case CMD_FCP_TSEND_CX:
748         case CMD_FCP_TRSP_CX:
749         case CMD_FCP_TRECEIVE_CX:
750         case CMD_FCP_AUTO_TRSP_CX:
751         case CMD_ADAPTER_MSG:
752         case CMD_ADAPTER_DUMP:
753         case CMD_XMIT_SEQUENCE64_CR:
754         case CMD_XMIT_SEQUENCE64_CX:
755         case CMD_XMIT_BCAST64_CN:
756         case CMD_XMIT_BCAST64_CX:
757         case CMD_ELS_REQUEST64_CR:
758         case CMD_ELS_REQUEST64_CX:
759         case CMD_FCP_IWRITE64_CR:
760         case CMD_FCP_IWRITE64_CX:
761         case CMD_FCP_IREAD64_CR:
762         case CMD_FCP_IREAD64_CX:
763         case CMD_FCP_ICMND64_CR:
764         case CMD_FCP_ICMND64_CX:
765         case CMD_FCP_TSEND64_CX:
766         case CMD_FCP_TRSP64_CX:
767         case CMD_FCP_TRECEIVE64_CX:
768         case CMD_GEN_REQUEST64_CR:
769         case CMD_GEN_REQUEST64_CX:
770         case CMD_XMIT_ELS_RSP64_CX:
771         case DSSCMD_IWRITE64_CR:
772         case DSSCMD_IWRITE64_CX:
773         case DSSCMD_IREAD64_CR:
774         case DSSCMD_IREAD64_CX:
775         case DSSCMD_INVALIDATE_DEK:
776         case DSSCMD_SET_KEK:
777         case DSSCMD_GET_KEK_ID:
778         case DSSCMD_GEN_XFER:
779                 type = LPFC_SOL_IOCB;
780                 break;
781         case CMD_ABORT_XRI_CN:
782         case CMD_ABORT_XRI_CX:
783         case CMD_CLOSE_XRI_CN:
784         case CMD_CLOSE_XRI_CX:
785         case CMD_XRI_ABORTED_CX:
786         case CMD_ABORT_MXRI64_CN:
787                 type = LPFC_ABORT_IOCB;
788                 break;
789         case CMD_RCV_SEQUENCE_CX:
790         case CMD_RCV_ELS_REQ_CX:
791         case CMD_RCV_SEQUENCE64_CX:
792         case CMD_RCV_ELS_REQ64_CX:
793         case CMD_ASYNC_STATUS:
794         case CMD_IOCB_RCV_SEQ64_CX:
795         case CMD_IOCB_RCV_ELS64_CX:
796         case CMD_IOCB_RCV_CONT64_CX:
797         case CMD_IOCB_RET_XRI64_CX:
798                 type = LPFC_UNSOL_IOCB;
799                 break;
800         case CMD_IOCB_XMIT_MSEQ64_CR:
801         case CMD_IOCB_XMIT_MSEQ64_CX:
802         case CMD_IOCB_RCV_SEQ_LIST64_CX:
803         case CMD_IOCB_RCV_ELS_LIST64_CX:
804         case CMD_IOCB_CLOSE_EXTENDED_CN:
805         case CMD_IOCB_ABORT_EXTENDED_CN:
806         case CMD_IOCB_RET_HBQE64_CN:
807         case CMD_IOCB_FCP_IBIDIR64_CR:
808         case CMD_IOCB_FCP_IBIDIR64_CX:
809         case CMD_IOCB_FCP_ITASKMGT64_CX:
810         case CMD_IOCB_LOGENTRY_CN:
811         case CMD_IOCB_LOGENTRY_ASYNC_CN:
812                 printk("%s - Unhandled SLI-3 Command x%x\n",
813                                 __func__, iocb_cmnd);
814                 type = LPFC_UNKNOWN_IOCB;
815                 break;
816         default:
817                 type = LPFC_UNKNOWN_IOCB;
818                 break;
819         }
820
821         return type;
822 }
823
824 /**
825  * lpfc_sli_ring_map - Issue config_ring mbox for all rings
826  * @phba: Pointer to HBA context object.
827  *
828  * This function is called from SLI initialization code
829  * to configure every ring of the HBA's SLI interface. The
830  * caller is not required to hold any lock. This function issues
831  * a config_ring mailbox command for each ring.
832  * This function returns zero if successful else returns a negative
833  * error code.
834  **/
835 static int
836 lpfc_sli_ring_map(struct lpfc_hba *phba)
837 {
838         struct lpfc_sli *psli = &phba->sli;
839         LPFC_MBOXQ_t *pmb;
840         MAILBOX_t *pmbox;
841         int i, rc, ret = 0;
842
843         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
844         if (!pmb)
845                 return -ENOMEM;
846         pmbox = &pmb->mb;
847         phba->link_state = LPFC_INIT_MBX_CMDS;
848         for (i = 0; i < psli->num_rings; i++) {
849                 lpfc_config_ring(phba, i, pmb);
850                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
851                 if (rc != MBX_SUCCESS) {
852                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
853                                         "0446 Adapter failed to init (%d), "
854                                         "mbxCmd x%x CFG_RING, mbxStatus x%x, "
855                                         "ring %d\n",
856                                         rc, pmbox->mbxCommand,
857                                         pmbox->mbxStatus, i);
858                         phba->link_state = LPFC_HBA_ERROR;
859                         ret = -ENXIO;
860                         break;
861                 }
862         }
863         mempool_free(pmb, phba->mbox_mem_pool);
864         return ret;
865 }
866
867 /**
868  * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
869  * @phba: Pointer to HBA context object.
870  * @pring: Pointer to driver SLI ring object.
871  * @piocb: Pointer to the driver iocb object.
872  *
873  * This function is called with hbalock held. The function adds the
874  * new iocb to txcmplq of the given ring. This function always returns
875  * 0. If this function is called for ELS ring, this function checks if
876  * there is a vport associated with the ELS command. This function also
877  * starts els_tmofunc timer if this is an ELS command.
878  **/
879 static int
880 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
881                         struct lpfc_iocbq *piocb)
882 {
883         list_add_tail(&piocb->list, &pring->txcmplq);
884         pring->txcmplq_cnt++;
885         if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
886            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
887            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
888                 if (!piocb->vport)
889                         BUG();
890                 else
891                         mod_timer(&piocb->vport->els_tmofunc,
892                                   jiffies + HZ * (phba->fc_ratov << 1));
893         }
894
895
896         return 0;
897 }
898
899 /**
900  * lpfc_sli_ringtx_get - Get first element of the txq
901  * @phba: Pointer to HBA context object.
902  * @pring: Pointer to driver SLI ring object.
903  *
904  * This function is called with hbalock held to get next
905  * iocb in txq of the given ring. If there is any iocb in
906  * the txq, the function returns first iocb in the list after
907  * removing the iocb from the list, else it returns NULL.
908  **/
909 static struct lpfc_iocbq *
910 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
911 {
912         struct lpfc_iocbq *cmd_iocb;
913
914         list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
915         if (cmd_iocb != NULL)
916                 pring->txq_cnt--;
917         return cmd_iocb;
918 }
919
920 /**
921  * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
922  * @phba: Pointer to HBA context object.
923  * @pring: Pointer to driver SLI ring object.
924  *
925  * This function is called with hbalock held and the caller must post the
926  * iocb without releasing the lock. If the caller releases the lock,
927  * iocb slot returned by the function is not guaranteed to be available.
928  * The function returns pointer to the next available iocb slot if there
929  * is available slot in the ring, else it returns NULL.
930  * If the get index of the ring is ahead of the put index, the function
931  * will post an error attention event to the worker thread to take the
932  * HBA to offline state.
933  **/
934 static IOCB_t *
935 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
936 {
937         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
938         uint32_t  max_cmd_idx = pring->numCiocb;
939         if ((pring->next_cmdidx == pring->cmdidx) &&
940            (++pring->next_cmdidx >= max_cmd_idx))
941                 pring->next_cmdidx = 0;
942
943         if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
944
945                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
946
947                 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
948                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
949                                         "0315 Ring %d issue: portCmdGet %d "
950                                         "is bigger than cmd ring %d\n",
951                                         pring->ringno,
952                                         pring->local_getidx, max_cmd_idx);
953
954                         phba->link_state = LPFC_HBA_ERROR;
955                         /*
956                          * All error attention handlers are posted to
957                          * worker thread
958                          */
959                         phba->work_ha |= HA_ERATT;
960                         phba->work_hs = HS_FFER3;
961
962                         lpfc_worker_wake_up(phba);
963
964                         return NULL;
965                 }
966
967                 if (pring->local_getidx == pring->next_cmdidx)
968                         return NULL;
969         }
970
971         return lpfc_cmd_iocb(phba, pring);
972 }
973
974 /**
975  * lpfc_sli_next_iotag - Get an iotag for the iocb
976  * @phba: Pointer to HBA context object.
977  * @iocbq: Pointer to driver iocb object.
978  *
979  * This function gets an iotag for the iocb. If there is no unused iotag and
980  * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
981  * array and assigns a new iotag.
982  * The function returns the allocated iotag if successful, else returns zero.
983  * Zero is not a valid iotag.
984  * The caller is not required to hold any lock.
985  **/
986 uint16_t
987 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
988 {
989         struct lpfc_iocbq **new_arr;
990         struct lpfc_iocbq **old_arr;
991         size_t new_len;
992         struct lpfc_sli *psli = &phba->sli;
993         uint16_t iotag;
994
995         spin_lock_irq(&phba->hbalock);
996         iotag = psli->last_iotag;
997         if(++iotag < psli->iocbq_lookup_len) {
998                 psli->last_iotag = iotag;
999                 psli->iocbq_lookup[iotag] = iocbq;
1000                 spin_unlock_irq(&phba->hbalock);
1001                 iocbq->iotag = iotag;
1002                 return iotag;
1003         } else if (psli->iocbq_lookup_len < (0xffff
1004                                            - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1005                 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1006                 spin_unlock_irq(&phba->hbalock);
1007                 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1008                                   GFP_KERNEL);
1009                 if (new_arr) {
1010                         spin_lock_irq(&phba->hbalock);
1011                         old_arr = psli->iocbq_lookup;
1012                         if (new_len <= psli->iocbq_lookup_len) {
1013                                 /* highly unprobable case */
1014                                 kfree(new_arr);
1015                                 iotag = psli->last_iotag;
1016                                 if(++iotag < psli->iocbq_lookup_len) {
1017                                         psli->last_iotag = iotag;
1018                                         psli->iocbq_lookup[iotag] = iocbq;
1019                                         spin_unlock_irq(&phba->hbalock);
1020                                         iocbq->iotag = iotag;
1021                                         return iotag;
1022                                 }
1023                                 spin_unlock_irq(&phba->hbalock);
1024                                 return 0;
1025                         }
1026                         if (psli->iocbq_lookup)
1027                                 memcpy(new_arr, old_arr,
1028                                        ((psli->last_iotag  + 1) *
1029                                         sizeof (struct lpfc_iocbq *)));
1030                         psli->iocbq_lookup = new_arr;
1031                         psli->iocbq_lookup_len = new_len;
1032                         psli->last_iotag = iotag;
1033                         psli->iocbq_lookup[iotag] = iocbq;
1034                         spin_unlock_irq(&phba->hbalock);
1035                         iocbq->iotag = iotag;
1036                         kfree(old_arr);
1037                         return iotag;
1038                 }
1039         } else
1040                 spin_unlock_irq(&phba->hbalock);
1041
1042         lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
1043                         "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1044                         psli->last_iotag);
1045
1046         return 0;
1047 }
1048
1049 /**
1050  * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1051  * @phba: Pointer to HBA context object.
1052  * @pring: Pointer to driver SLI ring object.
1053  * @iocb: Pointer to iocb slot in the ring.
1054  * @nextiocb: Pointer to driver iocb object which need to be
1055  *            posted to firmware.
1056  *
1057  * This function is called with hbalock held to post a new iocb to
1058  * the firmware. This function copies the new iocb to ring iocb slot and
1059  * updates the ring pointers. It adds the new iocb to txcmplq if there is
1060  * a completion call back for this iocb else the function will free the
1061  * iocb object.
1062  **/
1063 static void
1064 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1065                 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1066 {
1067         /*
1068          * Set up an iotag
1069          */
1070         nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1071
1072
1073         if (pring->ringno == LPFC_ELS_RING) {
1074                 lpfc_debugfs_slow_ring_trc(phba,
1075                         "IOCB cmd ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
1076                         *(((uint32_t *) &nextiocb->iocb) + 4),
1077                         *(((uint32_t *) &nextiocb->iocb) + 6),
1078                         *(((uint32_t *) &nextiocb->iocb) + 7));
1079         }
1080
1081         /*
1082          * Issue iocb command to adapter
1083          */
1084         lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1085         wmb();
1086         pring->stats.iocb_cmd++;
1087
1088         /*
1089          * If there is no completion routine to call, we can release the
1090          * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1091          * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1092          */
1093         if (nextiocb->iocb_cmpl)
1094                 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1095         else
1096                 __lpfc_sli_release_iocbq(phba, nextiocb);
1097
1098         /*
1099          * Let the HBA know what IOCB slot will be the next one the
1100          * driver will put a command into.
1101          */
1102         pring->cmdidx = pring->next_cmdidx;
1103         writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1104 }
1105
1106 /**
1107  * lpfc_sli_update_full_ring - Update the chip attention register
1108  * @phba: Pointer to HBA context object.
1109  * @pring: Pointer to driver SLI ring object.
1110  *
1111  * The caller is not required to hold any lock for calling this function.
1112  * This function updates the chip attention bits for the ring to inform firmware
1113  * that there are pending work to be done for this ring and requests an
1114  * interrupt when there is space available in the ring. This function is
1115  * called when the driver is unable to post more iocbs to the ring due
1116  * to unavailability of space in the ring.
1117  **/
1118 static void
1119 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1120 {
1121         int ringno = pring->ringno;
1122
1123         pring->flag |= LPFC_CALL_RING_AVAILABLE;
1124
1125         wmb();
1126
1127         /*
1128          * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1129          * The HBA will tell us when an IOCB entry is available.
1130          */
1131         writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1132         readl(phba->CAregaddr); /* flush */
1133
1134         pring->stats.iocb_cmd_full++;
1135 }
1136
1137 /**
1138  * lpfc_sli_update_ring - Update chip attention register
1139  * @phba: Pointer to HBA context object.
1140  * @pring: Pointer to driver SLI ring object.
1141  *
1142  * This function updates the chip attention register bit for the
1143  * given ring to inform HBA that there is more work to be done
1144  * in this ring. The caller is not required to hold any lock.
1145  **/
1146 static void
1147 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1148 {
1149         int ringno = pring->ringno;
1150
1151         /*
1152          * Tell the HBA that there is work to do in this ring.
1153          */
1154         if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1155                 wmb();
1156                 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1157                 readl(phba->CAregaddr); /* flush */
1158         }
1159 }
1160
1161 /**
1162  * lpfc_sli_resume_iocb - Process iocbs in the txq
1163  * @phba: Pointer to HBA context object.
1164  * @pring: Pointer to driver SLI ring object.
1165  *
1166  * This function is called with hbalock held to post pending iocbs
1167  * in the txq to the firmware. This function is called when driver
1168  * detects space available in the ring.
1169  **/
1170 static void
1171 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1172 {
1173         IOCB_t *iocb;
1174         struct lpfc_iocbq *nextiocb;
1175
1176         /*
1177          * Check to see if:
1178          *  (a) there is anything on the txq to send
1179          *  (b) link is up
1180          *  (c) link attention events can be processed (fcp ring only)
1181          *  (d) IOCB processing is not blocked by the outstanding mbox command.
1182          */
1183         if (pring->txq_cnt &&
1184             lpfc_is_link_up(phba) &&
1185             (pring->ringno != phba->sli.fcp_ring ||
1186              phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1187
1188                 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1189                        (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1190                         lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1191
1192                 if (iocb)
1193                         lpfc_sli_update_ring(phba, pring);
1194                 else
1195                         lpfc_sli_update_full_ring(phba, pring);
1196         }
1197
1198         return;
1199 }
1200
1201 /**
1202  * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1203  * @phba: Pointer to HBA context object.
1204  * @hbqno: HBQ number.
1205  *
1206  * This function is called with hbalock held to get the next
1207  * available slot for the given HBQ. If there is free slot
1208  * available for the HBQ it will return pointer to the next available
1209  * HBQ entry else it will return NULL.
1210  **/
1211 static struct lpfc_hbq_entry *
1212 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1213 {
1214         struct hbq_s *hbqp = &phba->hbqs[hbqno];
1215
1216         if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1217             ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1218                 hbqp->next_hbqPutIdx = 0;
1219
1220         if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1221                 uint32_t raw_index = phba->hbq_get[hbqno];
1222                 uint32_t getidx = le32_to_cpu(raw_index);
1223
1224                 hbqp->local_hbqGetIdx = getidx;
1225
1226                 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1227                         lpfc_printf_log(phba, KERN_ERR,
1228                                         LOG_SLI | LOG_VPORT,
1229                                         "1802 HBQ %d: local_hbqGetIdx "
1230                                         "%u is > than hbqp->entry_count %u\n",
1231                                         hbqno, hbqp->local_hbqGetIdx,
1232                                         hbqp->entry_count);
1233
1234                         phba->link_state = LPFC_HBA_ERROR;
1235                         return NULL;
1236                 }
1237
1238                 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1239                         return NULL;
1240         }
1241
1242         return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1243                         hbqp->hbqPutIdx;
1244 }
1245
1246 /**
1247  * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1248  * @phba: Pointer to HBA context object.
1249  *
1250  * This function is called with no lock held to free all the
1251  * hbq buffers while uninitializing the SLI interface. It also
1252  * frees the HBQ buffers returned by the firmware but not yet
1253  * processed by the upper layers.
1254  **/
1255 void
1256 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1257 {
1258         struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1259         struct hbq_dmabuf *hbq_buf;
1260         unsigned long flags;
1261         int i, hbq_count;
1262         uint32_t hbqno;
1263
1264         hbq_count = lpfc_sli_hbq_count();
1265         /* Return all memory used by all HBQs */
1266         spin_lock_irqsave(&phba->hbalock, flags);
1267         for (i = 0; i < hbq_count; ++i) {
1268                 list_for_each_entry_safe(dmabuf, next_dmabuf,
1269                                 &phba->hbqs[i].hbq_buffer_list, list) {
1270                         hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1271                         list_del(&hbq_buf->dbuf.list);
1272                         (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1273                 }
1274                 phba->hbqs[i].buffer_count = 0;
1275         }
1276         /* Return all HBQ buffer that are in-fly */
1277         list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1278                                  list) {
1279                 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1280                 list_del(&hbq_buf->dbuf.list);
1281                 if (hbq_buf->tag == -1) {
1282                         (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1283                                 (phba, hbq_buf);
1284                 } else {
1285                         hbqno = hbq_buf->tag >> 16;
1286                         if (hbqno >= LPFC_MAX_HBQS)
1287                                 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1288                                         (phba, hbq_buf);
1289                         else
1290                                 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1291                                         hbq_buf);
1292                 }
1293         }
1294
1295         /* Mark the HBQs not in use */
1296         phba->hbq_in_use = 0;
1297         spin_unlock_irqrestore(&phba->hbalock, flags);
1298 }
1299
1300 /**
1301  * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1302  * @phba: Pointer to HBA context object.
1303  * @hbqno: HBQ number.
1304  * @hbq_buf: Pointer to HBQ buffer.
1305  *
1306  * This function is called with the hbalock held to post a
1307  * hbq buffer to the firmware. If the function finds an empty
1308  * slot in the HBQ, it will post the buffer. The function will return
1309  * pointer to the hbq entry if it successfully post the buffer
1310  * else it will return NULL.
1311  **/
1312 static int
1313 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1314                          struct hbq_dmabuf *hbq_buf)
1315 {
1316         return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1317 }
1318
1319 /**
1320  * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1321  * @phba: Pointer to HBA context object.
1322  * @hbqno: HBQ number.
1323  * @hbq_buf: Pointer to HBQ buffer.
1324  *
1325  * This function is called with the hbalock held to post a hbq buffer to the
1326  * firmware. If the function finds an empty slot in the HBQ, it will post the
1327  * buffer and place it on the hbq_buffer_list. The function will return zero if
1328  * it successfully post the buffer else it will return an error.
1329  **/
1330 static int
1331 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1332                             struct hbq_dmabuf *hbq_buf)
1333 {
1334         struct lpfc_hbq_entry *hbqe;
1335         dma_addr_t physaddr = hbq_buf->dbuf.phys;
1336
1337         /* Get next HBQ entry slot to use */
1338         hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1339         if (hbqe) {
1340                 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1341
1342                 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1343                 hbqe->bde.addrLow  = le32_to_cpu(putPaddrLow(physaddr));
1344                 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1345                 hbqe->bde.tus.f.bdeFlags = 0;
1346                 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1347                 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1348                                 /* Sync SLIM */
1349                 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1350                 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1351                                 /* flush */
1352                 readl(phba->hbq_put + hbqno);
1353                 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1354                 return 0;
1355         } else
1356                 return -ENOMEM;
1357 }
1358
1359 /**
1360  * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1361  * @phba: Pointer to HBA context object.
1362  * @hbqno: HBQ number.
1363  * @hbq_buf: Pointer to HBQ buffer.
1364  *
1365  * This function is called with the hbalock held to post an RQE to the SLI4
1366  * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1367  * the hbq_buffer_list and return zero, otherwise it will return an error.
1368  **/
1369 static int
1370 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1371                             struct hbq_dmabuf *hbq_buf)
1372 {
1373         int rc;
1374         struct lpfc_rqe hrqe;
1375         struct lpfc_rqe drqe;
1376
1377         hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1378         hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1379         drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1380         drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1381         rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1382                               &hrqe, &drqe);
1383         if (rc < 0)
1384                 return rc;
1385         hbq_buf->tag = rc;
1386         list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1387         return 0;
1388 }
1389
1390 /* HBQ for ELS and CT traffic. */
1391 static struct lpfc_hbq_init lpfc_els_hbq = {
1392         .rn = 1,
1393         .entry_count = 200,
1394         .mask_count = 0,
1395         .profile = 0,
1396         .ring_mask = (1 << LPFC_ELS_RING),
1397         .buffer_count = 0,
1398         .init_count = 40,
1399         .add_count = 40,
1400 };
1401
1402 /* HBQ for the extra ring if needed */
1403 static struct lpfc_hbq_init lpfc_extra_hbq = {
1404         .rn = 1,
1405         .entry_count = 200,
1406         .mask_count = 0,
1407         .profile = 0,
1408         .ring_mask = (1 << LPFC_EXTRA_RING),
1409         .buffer_count = 0,
1410         .init_count = 0,
1411         .add_count = 5,
1412 };
1413
1414 /* Array of HBQs */
1415 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1416         &lpfc_els_hbq,
1417         &lpfc_extra_hbq,
1418 };
1419
1420 /**
1421  * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1422  * @phba: Pointer to HBA context object.
1423  * @hbqno: HBQ number.
1424  * @count: Number of HBQ buffers to be posted.
1425  *
1426  * This function is called with no lock held to post more hbq buffers to the
1427  * given HBQ. The function returns the number of HBQ buffers successfully
1428  * posted.
1429  **/
1430 static int
1431 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1432 {
1433         uint32_t i, posted = 0;
1434         unsigned long flags;
1435         struct hbq_dmabuf *hbq_buffer;
1436         LIST_HEAD(hbq_buf_list);
1437         if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1438                 return 0;
1439
1440         if ((phba->hbqs[hbqno].buffer_count + count) >
1441             lpfc_hbq_defs[hbqno]->entry_count)
1442                 count = lpfc_hbq_defs[hbqno]->entry_count -
1443                                         phba->hbqs[hbqno].buffer_count;
1444         if (!count)
1445                 return 0;
1446         /* Allocate HBQ entries */
1447         for (i = 0; i < count; i++) {
1448                 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1449                 if (!hbq_buffer)
1450                         break;
1451                 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1452         }
1453         /* Check whether HBQ is still in use */
1454         spin_lock_irqsave(&phba->hbalock, flags);
1455         if (!phba->hbq_in_use)
1456                 goto err;
1457         while (!list_empty(&hbq_buf_list)) {
1458                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1459                                  dbuf.list);
1460                 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1461                                       (hbqno << 16));
1462                 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1463                         phba->hbqs[hbqno].buffer_count++;
1464                         posted++;
1465                 } else
1466                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1467         }
1468         spin_unlock_irqrestore(&phba->hbalock, flags);
1469         return posted;
1470 err:
1471         spin_unlock_irqrestore(&phba->hbalock, flags);
1472         while (!list_empty(&hbq_buf_list)) {
1473                 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1474                                  dbuf.list);
1475                 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1476         }
1477         return 0;
1478 }
1479
1480 /**
1481  * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1482  * @phba: Pointer to HBA context object.
1483  * @qno: HBQ number.
1484  *
1485  * This function posts more buffers to the HBQ. This function
1486  * is called with no lock held. The function returns the number of HBQ entries
1487  * successfully allocated.
1488  **/
1489 int
1490 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1491 {
1492         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1493                                          lpfc_hbq_defs[qno]->add_count));
1494 }
1495
1496 /**
1497  * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1498  * @phba: Pointer to HBA context object.
1499  * @qno:  HBQ queue number.
1500  *
1501  * This function is called from SLI initialization code path with
1502  * no lock held to post initial HBQ buffers to firmware. The
1503  * function returns the number of HBQ entries successfully allocated.
1504  **/
1505 static int
1506 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1507 {
1508         return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1509                                          lpfc_hbq_defs[qno]->init_count));
1510 }
1511
1512 /**
1513  * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1514  * @phba: Pointer to HBA context object.
1515  * @hbqno: HBQ number.
1516  *
1517  * This function removes the first hbq buffer on an hbq list and returns a
1518  * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1519  **/
1520 static struct hbq_dmabuf *
1521 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1522 {
1523         struct lpfc_dmabuf *d_buf;
1524
1525         list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1526         if (!d_buf)
1527                 return NULL;
1528         return container_of(d_buf, struct hbq_dmabuf, dbuf);
1529 }
1530
1531 /**
1532  * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1533  * @phba: Pointer to HBA context object.
1534  * @tag: Tag of the hbq buffer.
1535  *
1536  * This function is called with hbalock held. This function searches
1537  * for the hbq buffer associated with the given tag in the hbq buffer
1538  * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1539  * it returns NULL.
1540  **/
1541 static struct hbq_dmabuf *
1542 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1543 {
1544         struct lpfc_dmabuf *d_buf;
1545         struct hbq_dmabuf *hbq_buf;
1546         uint32_t hbqno;
1547
1548         hbqno = tag >> 16;
1549         if (hbqno >= LPFC_MAX_HBQS)
1550                 return NULL;
1551
1552         spin_lock_irq(&phba->hbalock);
1553         list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
1554                 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1555                 if (hbq_buf->tag == tag) {
1556                         spin_unlock_irq(&phba->hbalock);
1557                         return hbq_buf;
1558                 }
1559         }
1560         spin_unlock_irq(&phba->hbalock);
1561         lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
1562                         "1803 Bad hbq tag. Data: x%x x%x\n",
1563                         tag, phba->hbqs[tag >> 16].buffer_count);
1564         return NULL;
1565 }
1566
1567 /**
1568  * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
1569  * @phba: Pointer to HBA context object.
1570  * @hbq_buffer: Pointer to HBQ buffer.
1571  *
1572  * This function is called with hbalock. This function gives back
1573  * the hbq buffer to firmware. If the HBQ does not have space to
1574  * post the buffer, it will free the buffer.
1575  **/
1576 void
1577 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
1578 {
1579         uint32_t hbqno;
1580
1581         if (hbq_buffer) {
1582                 hbqno = hbq_buffer->tag >> 16;
1583                 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
1584                         (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1585         }
1586 }
1587
1588 /**
1589  * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
1590  * @mbxCommand: mailbox command code.
1591  *
1592  * This function is called by the mailbox event handler function to verify
1593  * that the completed mailbox command is a legitimate mailbox command. If the
1594  * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1595  * and the mailbox event handler will take the HBA offline.
1596  **/
1597 static int
1598 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1599 {
1600         uint8_t ret;
1601
1602         switch (mbxCommand) {
1603         case MBX_LOAD_SM:
1604         case MBX_READ_NV:
1605         case MBX_WRITE_NV:
1606         case MBX_WRITE_VPARMS:
1607         case MBX_RUN_BIU_DIAG:
1608         case MBX_INIT_LINK:
1609         case MBX_DOWN_LINK:
1610         case MBX_CONFIG_LINK:
1611         case MBX_CONFIG_RING:
1612         case MBX_RESET_RING:
1613         case MBX_READ_CONFIG:
1614         case MBX_READ_RCONFIG:
1615         case MBX_READ_SPARM:
1616         case MBX_READ_STATUS:
1617         case MBX_READ_RPI:
1618         case MBX_READ_XRI:
1619         case MBX_READ_REV:
1620         case MBX_READ_LNK_STAT:
1621         case MBX_REG_LOGIN:
1622         case MBX_UNREG_LOGIN:
1623         case MBX_READ_LA:
1624         case MBX_CLEAR_LA:
1625         case MBX_DUMP_MEMORY:
1626         case MBX_DUMP_CONTEXT:
1627         case MBX_RUN_DIAGS:
1628         case MBX_RESTART:
1629         case MBX_UPDATE_CFG:
1630         case MBX_DOWN_LOAD:
1631         case MBX_DEL_LD_ENTRY:
1632         case MBX_RUN_PROGRAM:
1633         case MBX_SET_MASK:
1634         case MBX_SET_VARIABLE:
1635         case MBX_UNREG_D_ID:
1636         case MBX_KILL_BOARD:
1637         case MBX_CONFIG_FARP:
1638         case MBX_BEACON:
1639         case MBX_LOAD_AREA:
1640         case MBX_RUN_BIU_DIAG64:
1641         case MBX_CONFIG_PORT:
1642         case MBX_READ_SPARM64:
1643         case MBX_READ_RPI64:
1644         case MBX_REG_LOGIN64:
1645         case MBX_READ_LA64:
1646         case MBX_WRITE_WWN:
1647         case MBX_SET_DEBUG:
1648         case MBX_LOAD_EXP_ROM:
1649         case MBX_ASYNCEVT_ENABLE:
1650         case MBX_REG_VPI:
1651         case MBX_UNREG_VPI:
1652         case MBX_HEARTBEAT:
1653         case MBX_PORT_CAPABILITIES:
1654         case MBX_PORT_IOV_CONTROL:
1655                 ret = mbxCommand;
1656                 break;
1657         default:
1658                 ret = MBX_SHUTDOWN;
1659                 break;
1660         }
1661         return ret;
1662 }
1663
1664 /**
1665  * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
1666  * @phba: Pointer to HBA context object.
1667  * @pmboxq: Pointer to mailbox command.
1668  *
1669  * This is completion handler function for mailbox commands issued from
1670  * lpfc_sli_issue_mbox_wait function. This function is called by the
1671  * mailbox event handler function with no lock held. This function
1672  * will wake up thread waiting on the wait queue pointed by context1
1673  * of the mailbox.
1674  **/
1675 static void
1676 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
1677 {
1678         wait_queue_head_t *pdone_q;
1679         unsigned long drvr_flag;
1680
1681         /*
1682          * If pdone_q is empty, the driver thread gave up waiting and
1683          * continued running.
1684          */
1685         pmboxq->mbox_flag |= LPFC_MBX_WAKE;
1686         spin_lock_irqsave(&phba->hbalock, drvr_flag);
1687         pdone_q = (wait_queue_head_t *) pmboxq->context1;
1688         if (pdone_q)
1689                 wake_up_interruptible(pdone_q);
1690         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1691         return;
1692 }
1693
1694
1695 /**
1696  * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
1697  * @phba: Pointer to HBA context object.
1698  * @pmb: Pointer to mailbox object.
1699  *
1700  * This function is the default mailbox completion handler. It
1701  * frees the memory resources associated with the completed mailbox
1702  * command. If the completed command is a REG_LOGIN mailbox command,
1703  * this function will issue a UREG_LOGIN to re-claim the RPI.
1704  **/
1705 void
1706 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
1707 {
1708         struct lpfc_dmabuf *mp;
1709         uint16_t rpi;
1710         int rc;
1711
1712         mp = (struct lpfc_dmabuf *) (pmb->context1);
1713
1714         if (mp) {
1715                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1716                 kfree(mp);
1717         }
1718
1719         /*
1720          * If a REG_LOGIN succeeded  after node is destroyed or node
1721          * is in re-discovery driver need to cleanup the RPI.
1722          */
1723         if (!(phba->pport->load_flag & FC_UNLOADING) &&
1724             pmb->mb.mbxCommand == MBX_REG_LOGIN64 &&
1725             !pmb->mb.mbxStatus) {
1726
1727                 rpi = pmb->mb.un.varWords[0];
1728                 lpfc_unreg_login(phba, pmb->mb.un.varRegLogin.vpi, rpi, pmb);
1729                 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1730                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1731                 if (rc != MBX_NOT_FINISHED)
1732                         return;
1733         }
1734
1735         mempool_free(pmb, phba->mbox_mem_pool);
1736         return;
1737 }
1738
1739 /**
1740  * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
1741  * @phba: Pointer to HBA context object.
1742  *
1743  * This function is called with no lock held. This function processes all
1744  * the completed mailbox commands and gives it to upper layers. The interrupt
1745  * service routine processes mailbox completion interrupt and adds completed
1746  * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1747  * Worker thread call lpfc_sli_handle_mb_event, which will return the
1748  * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1749  * function returns the mailbox commands to the upper layer by calling the
1750  * completion handler function of each mailbox.
1751  **/
1752 int
1753 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
1754 {
1755         MAILBOX_t *pmbox;
1756         LPFC_MBOXQ_t *pmb;
1757         int rc;
1758         LIST_HEAD(cmplq);
1759
1760         phba->sli.slistat.mbox_event++;
1761
1762         /* Get all completed mailboxe buffers into the cmplq */
1763         spin_lock_irq(&phba->hbalock);
1764         list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1765         spin_unlock_irq(&phba->hbalock);
1766
1767         /* Get a Mailbox buffer to setup mailbox commands for callback */
1768         do {
1769                 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1770                 if (pmb == NULL)
1771                         break;
1772
1773                 pmbox = &pmb->mb;
1774
1775                 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1776                         if (pmb->vport) {
1777                                 lpfc_debugfs_disc_trc(pmb->vport,
1778                                         LPFC_DISC_TRC_MBOX_VPORT,
1779                                         "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1780                                         (uint32_t)pmbox->mbxCommand,
1781                                         pmbox->un.varWords[0],
1782                                         pmbox->un.varWords[1]);
1783                         }
1784                         else {
1785                                 lpfc_debugfs_disc_trc(phba->pport,
1786                                         LPFC_DISC_TRC_MBOX,
1787                                         "MBOX cmpl:       cmd:x%x mb:x%x x%x",
1788                                         (uint32_t)pmbox->mbxCommand,
1789                                         pmbox->un.varWords[0],
1790                                         pmbox->un.varWords[1]);
1791                         }
1792                 }
1793
1794                 /*
1795                  * It is a fatal error if unknown mbox command completion.
1796                  */
1797                 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1798                     MBX_SHUTDOWN) {
1799                         /* Unknow mailbox command compl */
1800                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
1801                                         "(%d):0323 Unknown Mailbox command "
1802                                         "%x Cmpl\n",
1803                                         pmb->vport ? pmb->vport->vpi : 0,
1804                                         pmbox->mbxCommand);
1805                         phba->link_state = LPFC_HBA_ERROR;
1806                         phba->work_hs = HS_FFER3;
1807                         lpfc_handle_eratt(phba);
1808                         continue;
1809                 }
1810
1811                 if (pmbox->mbxStatus) {
1812                         phba->sli.slistat.mbox_stat_err++;
1813                         if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1814                                 /* Mbox cmd cmpl error - RETRYing */
1815                                 lpfc_printf_log(phba, KERN_INFO,
1816                                                 LOG_MBOX | LOG_SLI,
1817                                                 "(%d):0305 Mbox cmd cmpl "
1818                                                 "error - RETRYing Data: x%x "
1819                                                 "x%x x%x x%x\n",
1820                                                 pmb->vport ? pmb->vport->vpi :0,
1821                                                 pmbox->mbxCommand,
1822                                                 pmbox->mbxStatus,
1823                                                 pmbox->un.varWords[0],
1824                                                 pmb->vport->port_state);
1825                                 pmbox->mbxStatus = 0;
1826                                 pmbox->mbxOwner = OWN_HOST;
1827                                 spin_lock_irq(&phba->hbalock);
1828                                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
1829                                 spin_unlock_irq(&phba->hbalock);
1830                                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1831                                 if (rc == MBX_SUCCESS)
1832                                         continue;
1833                         }
1834                 }
1835
1836                 /* Mailbox cmd <cmd> Cmpl <cmpl> */
1837                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
1838                                 "(%d):0307 Mailbox cmd x%x Cmpl x%p "
1839                                 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
1840                                 pmb->vport ? pmb->vport->vpi : 0,
1841                                 pmbox->mbxCommand,
1842                                 pmb->mbox_cmpl,
1843                                 *((uint32_t *) pmbox),
1844                                 pmbox->un.varWords[0],
1845                                 pmbox->un.varWords[1],
1846                                 pmbox->un.varWords[2],
1847                                 pmbox->un.varWords[3],
1848                                 pmbox->un.varWords[4],
1849                                 pmbox->un.varWords[5],
1850                                 pmbox->un.varWords[6],
1851                                 pmbox->un.varWords[7]);
1852
1853                 if (pmb->mbox_cmpl)
1854                         pmb->mbox_cmpl(phba,pmb);
1855         } while (1);
1856         return 0;
1857 }
1858
1859 /**
1860  * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
1861  * @phba: Pointer to HBA context object.
1862  * @pring: Pointer to driver SLI ring object.
1863  * @tag: buffer tag.
1864  *
1865  * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1866  * is set in the tag the buffer is posted for a particular exchange,
1867  * the function will return the buffer without replacing the buffer.
1868  * If the buffer is for unsolicited ELS or CT traffic, this function
1869  * returns the buffer and also posts another buffer to the firmware.
1870  **/
1871 static struct lpfc_dmabuf *
1872 lpfc_sli_get_buff(struct lpfc_hba *phba,
1873                   struct lpfc_sli_ring *pring,
1874                   uint32_t tag)
1875 {
1876         struct hbq_dmabuf *hbq_entry;
1877
1878         if (tag & QUE_BUFTAG_BIT)
1879                 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
1880         hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1881         if (!hbq_entry)
1882                 return NULL;
1883         return &hbq_entry->dbuf;
1884 }
1885
1886 /**
1887  * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1888  * @phba: Pointer to HBA context object.
1889  * @pring: Pointer to driver SLI ring object.
1890  * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1891  * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1892  * @fch_type: the type for the first frame of the sequence.
1893  *
1894  * This function is called with no lock held. This function uses the r_ctl and
1895  * type of the received sequence to find the correct callback function to call
1896  * to process the sequence.
1897  **/
1898 static int
1899 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1900                          struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1901                          uint32_t fch_type)
1902 {
1903         int i;
1904
1905         /* unSolicited Responses */
1906         if (pring->prt[0].profile) {
1907                 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1908                         (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1909                                                                         saveq);
1910                 return 1;
1911         }
1912         /* We must search, based on rctl / type
1913            for the right routine */
1914         for (i = 0; i < pring->num_mask; i++) {
1915                 if ((pring->prt[i].rctl == fch_r_ctl) &&
1916                     (pring->prt[i].type == fch_type)) {
1917                         if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1918                                 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1919                                                 (phba, pring, saveq);
1920                         return 1;
1921                 }
1922         }
1923         return 0;
1924 }
1925
1926 /**
1927  * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
1928  * @phba: Pointer to HBA context object.
1929  * @pring: Pointer to driver SLI ring object.
1930  * @saveq: Pointer to the unsolicited iocb.
1931  *
1932  * This function is called with no lock held by the ring event handler
1933  * when there is an unsolicited iocb posted to the response ring by the
1934  * firmware. This function gets the buffer associated with the iocbs
1935  * and calls the event handler for the ring. This function handles both
1936  * qring buffers and hbq buffers.
1937  * When the function returns 1 the caller can free the iocb object otherwise
1938  * upper layer functions will free the iocb objects.
1939  **/
1940 static int
1941 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1942                             struct lpfc_iocbq *saveq)
1943 {
1944         IOCB_t           * irsp;
1945         WORD5            * w5p;
1946         uint32_t           Rctl, Type;
1947         uint32_t           match;
1948         struct lpfc_iocbq *iocbq;
1949         struct lpfc_dmabuf *dmzbuf;
1950
1951         match = 0;
1952         irsp = &(saveq->iocb);
1953
1954         if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1955                 if (pring->lpfc_sli_rcv_async_status)
1956                         pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1957                 else
1958                         lpfc_printf_log(phba,
1959                                         KERN_WARNING,
1960                                         LOG_SLI,
1961                                         "0316 Ring %d handler: unexpected "
1962                                         "ASYNC_STATUS iocb received evt_code "
1963                                         "0x%x\n",
1964                                         pring->ringno,
1965                                         irsp->un.asyncstat.evt_code);
1966                 return 1;
1967         }
1968
1969         if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1970                 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1971                 if (irsp->ulpBdeCount > 0) {
1972                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1973                                         irsp->un.ulpWord[3]);
1974                         lpfc_in_buf_free(phba, dmzbuf);
1975                 }
1976
1977                 if (irsp->ulpBdeCount > 1) {
1978                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1979                                         irsp->unsli3.sli3Words[3]);
1980                         lpfc_in_buf_free(phba, dmzbuf);
1981                 }
1982
1983                 if (irsp->ulpBdeCount > 2) {
1984                         dmzbuf = lpfc_sli_get_buff(phba, pring,
1985                                 irsp->unsli3.sli3Words[7]);
1986                         lpfc_in_buf_free(phba, dmzbuf);
1987                 }
1988
1989                 return 1;
1990         }
1991
1992         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
1993                 if (irsp->ulpBdeCount != 0) {
1994                         saveq->context2 = lpfc_sli_get_buff(phba, pring,
1995                                                 irsp->un.ulpWord[3]);
1996                         if (!saveq->context2)
1997                                 lpfc_printf_log(phba,
1998                                         KERN_ERR,
1999                                         LOG_SLI,
2000                                         "0341 Ring %d Cannot find buffer for "
2001                                         "an unsolicited iocb. tag 0x%x\n",
2002                                         pring->ringno,
2003                                         irsp->un.ulpWord[3]);
2004                 }
2005                 if (irsp->ulpBdeCount == 2) {
2006                         saveq->context3 = lpfc_sli_get_buff(phba, pring,
2007                                                 irsp->unsli3.sli3Words[7]);
2008                         if (!saveq->context3)
2009                                 lpfc_printf_log(phba,
2010                                         KERN_ERR,
2011                                         LOG_SLI,
2012                                         "0342 Ring %d Cannot find buffer for an"
2013                                         " unsolicited iocb. tag 0x%x\n",
2014                                         pring->ringno,
2015                                         irsp->unsli3.sli3Words[7]);
2016                 }
2017                 list_for_each_entry(iocbq, &saveq->list, list) {
2018                         irsp = &(iocbq->iocb);
2019                         if (irsp->ulpBdeCount != 0) {
2020                                 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2021                                                         irsp->un.ulpWord[3]);
2022                                 if (!iocbq->context2)
2023                                         lpfc_printf_log(phba,
2024                                                 KERN_ERR,
2025                                                 LOG_SLI,
2026                                                 "0343 Ring %d Cannot find "
2027                                                 "buffer for an unsolicited iocb"
2028                                                 ". tag 0x%x\n", pring->ringno,
2029                                                 irsp->un.ulpWord[3]);
2030                         }
2031                         if (irsp->ulpBdeCount == 2) {
2032                                 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2033                                                 irsp->unsli3.sli3Words[7]);
2034                                 if (!iocbq->context3)
2035                                         lpfc_printf_log(phba,
2036                                                 KERN_ERR,
2037                                                 LOG_SLI,
2038                                                 "0344 Ring %d Cannot find "
2039                                                 "buffer for an unsolicited "
2040                                                 "iocb. tag 0x%x\n",
2041                                                 pring->ringno,
2042                                                 irsp->unsli3.sli3Words[7]);
2043                         }
2044                 }
2045         }
2046         if (irsp->ulpBdeCount != 0 &&
2047             (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2048              irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2049                 int found = 0;
2050
2051                 /* search continue save q for same XRI */
2052                 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2053                         if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2054                                 list_add_tail(&saveq->list, &iocbq->list);
2055                                 found = 1;
2056                                 break;
2057                         }
2058                 }
2059                 if (!found)
2060                         list_add_tail(&saveq->clist,
2061                                       &pring->iocb_continue_saveq);
2062                 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2063                         list_del_init(&iocbq->clist);
2064                         saveq = iocbq;
2065                         irsp = &(saveq->iocb);
2066                 } else
2067                         return 0;
2068         }
2069         if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2070             (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2071             (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2072                 Rctl = FC_ELS_REQ;
2073                 Type = FC_ELS_DATA;
2074         } else {
2075                 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2076                 Rctl = w5p->hcsw.Rctl;
2077                 Type = w5p->hcsw.Type;
2078
2079                 /* Firmware Workaround */
2080                 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2081                         (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2082                          irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2083                         Rctl = FC_ELS_REQ;
2084                         Type = FC_ELS_DATA;
2085                         w5p->hcsw.Rctl = Rctl;
2086                         w5p->hcsw.Type = Type;
2087                 }
2088         }
2089
2090         if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2091                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2092                                 "0313 Ring %d handler: unexpected Rctl x%x "
2093                                 "Type x%x received\n",
2094                                 pring->ringno, Rctl, Type);
2095
2096         return 1;
2097 }
2098
2099 /**
2100  * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2101  * @phba: Pointer to HBA context object.
2102  * @pring: Pointer to driver SLI ring object.
2103  * @prspiocb: Pointer to response iocb object.
2104  *
2105  * This function looks up the iocb_lookup table to get the command iocb
2106  * corresponding to the given response iocb using the iotag of the
2107  * response iocb. This function is called with the hbalock held.
2108  * This function returns the command iocb object if it finds the command
2109  * iocb else returns NULL.
2110  **/
2111 static struct lpfc_iocbq *
2112 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2113                       struct lpfc_sli_ring *pring,
2114                       struct lpfc_iocbq *prspiocb)
2115 {
2116         struct lpfc_iocbq *cmd_iocb = NULL;
2117         uint16_t iotag;
2118
2119         iotag = prspiocb->iocb.ulpIoTag;
2120
2121         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2122                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2123                 list_del_init(&cmd_iocb->list);
2124                 pring->txcmplq_cnt--;
2125                 return cmd_iocb;
2126         }
2127
2128         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2129                         "0317 iotag x%x is out off "
2130                         "range: max iotag x%x wd0 x%x\n",
2131                         iotag, phba->sli.last_iotag,
2132                         *(((uint32_t *) &prspiocb->iocb) + 7));
2133         return NULL;
2134 }
2135
2136 /**
2137  * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2138  * @phba: Pointer to HBA context object.
2139  * @pring: Pointer to driver SLI ring object.
2140  * @iotag: IOCB tag.
2141  *
2142  * This function looks up the iocb_lookup table to get the command iocb
2143  * corresponding to the given iotag. This function is called with the
2144  * hbalock held.
2145  * This function returns the command iocb object if it finds the command
2146  * iocb else returns NULL.
2147  **/
2148 static struct lpfc_iocbq *
2149 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2150                              struct lpfc_sli_ring *pring, uint16_t iotag)
2151 {
2152         struct lpfc_iocbq *cmd_iocb;
2153
2154         if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2155                 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2156                 list_del_init(&cmd_iocb->list);
2157                 pring->txcmplq_cnt--;
2158                 return cmd_iocb;
2159         }
2160
2161         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2162                         "0372 iotag x%x is out off range: max iotag (x%x)\n",
2163                         iotag, phba->sli.last_iotag);
2164         return NULL;
2165 }
2166
2167 /**
2168  * lpfc_sli_process_sol_iocb - process solicited iocb completion
2169  * @phba: Pointer to HBA context object.
2170  * @pring: Pointer to driver SLI ring object.
2171  * @saveq: Pointer to the response iocb to be processed.
2172  *
2173  * This function is called by the ring event handler for non-fcp
2174  * rings when there is a new response iocb in the response ring.
2175  * The caller is not required to hold any locks. This function
2176  * gets the command iocb associated with the response iocb and
2177  * calls the completion handler for the command iocb. If there
2178  * is no completion handler, the function will free the resources
2179  * associated with command iocb. If the response iocb is for
2180  * an already aborted command iocb, the status of the completion
2181  * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2182  * This function always returns 1.
2183  **/
2184 static int
2185 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2186                           struct lpfc_iocbq *saveq)
2187 {
2188         struct lpfc_iocbq *cmdiocbp;
2189         int rc = 1;
2190         unsigned long iflag;
2191
2192         /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2193         spin_lock_irqsave(&phba->hbalock, iflag);
2194         cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2195         spin_unlock_irqrestore(&phba->hbalock, iflag);
2196
2197         if (cmdiocbp) {
2198                 if (cmdiocbp->iocb_cmpl) {
2199                         /*
2200                          * If an ELS command failed send an event to mgmt
2201                          * application.
2202                          */
2203                         if (saveq->iocb.ulpStatus &&
2204                              (pring->ringno == LPFC_ELS_RING) &&
2205                              (cmdiocbp->iocb.ulpCommand ==
2206                                 CMD_ELS_REQUEST64_CR))
2207                                 lpfc_send_els_failure_event(phba,
2208                                         cmdiocbp, saveq);
2209
2210                         /*
2211                          * Post all ELS completions to the worker thread.
2212                          * All other are passed to the completion callback.
2213                          */
2214                         if (pring->ringno == LPFC_ELS_RING) {
2215                                 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2216                                         cmdiocbp->iocb_flag &=
2217                                                 ~LPFC_DRIVER_ABORTED;
2218                                         saveq->iocb.ulpStatus =
2219                                                 IOSTAT_LOCAL_REJECT;
2220                                         saveq->iocb.un.ulpWord[4] =
2221                                                 IOERR_SLI_ABORTED;
2222
2223                                         /* Firmware could still be in progress
2224                                          * of DMAing payload, so don't free data
2225                                          * buffer till after a hbeat.
2226                                          */
2227                                         saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2228                                 }
2229                         }
2230                         (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2231                 } else
2232                         lpfc_sli_release_iocbq(phba, cmdiocbp);
2233         } else {
2234                 /*
2235                  * Unknown initiating command based on the response iotag.
2236                  * This could be the case on the ELS ring because of
2237                  * lpfc_els_abort().
2238                  */
2239                 if (pring->ringno != LPFC_ELS_RING) {
2240                         /*
2241                          * Ring <ringno> handler: unexpected completion IoTag
2242                          * <IoTag>
2243                          */
2244                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2245                                          "0322 Ring %d handler: "
2246                                          "unexpected completion IoTag x%x "
2247                                          "Data: x%x x%x x%x x%x\n",
2248                                          pring->ringno,
2249                                          saveq->iocb.ulpIoTag,
2250                                          saveq->iocb.ulpStatus,
2251                                          saveq->iocb.un.ulpWord[4],
2252                                          saveq->iocb.ulpCommand,
2253                                          saveq->iocb.ulpContext);
2254                 }
2255         }
2256
2257         return rc;
2258 }
2259
2260 /**
2261  * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2262  * @phba: Pointer to HBA context object.
2263  * @pring: Pointer to driver SLI ring object.
2264  *
2265  * This function is called from the iocb ring event handlers when
2266  * put pointer is ahead of the get pointer for a ring. This function signal
2267  * an error attention condition to the worker thread and the worker
2268  * thread will transition the HBA to offline state.
2269  **/
2270 static void
2271 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2272 {
2273         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2274         /*
2275          * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2276          * rsp ring <portRspMax>
2277          */
2278         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2279                         "0312 Ring %d handler: portRspPut %d "
2280                         "is bigger than rsp ring %d\n",
2281                         pring->ringno, le32_to_cpu(pgp->rspPutInx),
2282                         pring->numRiocb);
2283
2284         phba->link_state = LPFC_HBA_ERROR;
2285
2286         /*
2287          * All error attention handlers are posted to
2288          * worker thread
2289          */
2290         phba->work_ha |= HA_ERATT;
2291         phba->work_hs = HS_FFER3;
2292
2293         lpfc_worker_wake_up(phba);
2294
2295         return;
2296 }
2297
2298 /**
2299  * lpfc_poll_eratt - Error attention polling timer timeout handler
2300  * @ptr: Pointer to address of HBA context object.
2301  *
2302  * This function is invoked by the Error Attention polling timer when the
2303  * timer times out. It will check the SLI Error Attention register for
2304  * possible attention events. If so, it will post an Error Attention event
2305  * and wake up worker thread to process it. Otherwise, it will set up the
2306  * Error Attention polling timer for the next poll.
2307  **/
2308 void lpfc_poll_eratt(unsigned long ptr)
2309 {
2310         struct lpfc_hba *phba;
2311         uint32_t eratt = 0;
2312
2313         phba = (struct lpfc_hba *)ptr;
2314
2315         /* Check chip HA register for error event */
2316         eratt = lpfc_sli_check_eratt(phba);
2317
2318         if (eratt)
2319                 /* Tell the worker thread there is work to do */
2320                 lpfc_worker_wake_up(phba);
2321         else
2322                 /* Restart the timer for next eratt poll */
2323                 mod_timer(&phba->eratt_poll, jiffies +
2324                                         HZ * LPFC_ERATT_POLL_INTERVAL);
2325         return;
2326 }
2327
2328 /**
2329  * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
2330  * @phba: Pointer to HBA context object.
2331  *
2332  * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
2333  * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
2334  * is enabled.
2335  *
2336  * The caller does not hold any lock.
2337  * The function processes each response iocb in the response ring until it
2338  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2339  * LE bit set. The function will call the completion handler of the command iocb
2340  * if the response iocb indicates a completion for a command iocb or it is
2341  * an abort completion.
2342  **/
2343 void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
2344 {
2345         struct lpfc_sli      *psli  = &phba->sli;
2346         struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
2347         IOCB_t *irsp = NULL;
2348         IOCB_t *entry = NULL;
2349         struct lpfc_iocbq *cmdiocbq = NULL;
2350         struct lpfc_iocbq rspiocbq;
2351         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2352         uint32_t status;
2353         uint32_t portRspPut, portRspMax;
2354         int type;
2355         uint32_t rsp_cmpl = 0;
2356         uint32_t ha_copy;
2357         unsigned long iflags;
2358
2359         pring->stats.iocb_event++;
2360
2361         /*
2362          * The next available response entry should never exceed the maximum
2363          * entries.  If it does, treat it as an adapter hardware error.
2364          */
2365         portRspMax = pring->numRiocb;
2366         portRspPut = le32_to_cpu(pgp->rspPutInx);
2367         if (unlikely(portRspPut >= portRspMax)) {
2368                 lpfc_sli_rsp_pointers_error(phba, pring);
2369                 return;
2370         }
2371
2372         rmb();
2373         while (pring->rspidx != portRspPut) {
2374                 entry = lpfc_resp_iocb(phba, pring);
2375                 if (++pring->rspidx >= portRspMax)
2376                         pring->rspidx = 0;
2377
2378                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2379                                       (uint32_t *) &rspiocbq.iocb,
2380                                       phba->iocb_rsp_size);
2381                 irsp = &rspiocbq.iocb;
2382                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2383                 pring->stats.iocb_rsp++;
2384                 rsp_cmpl++;
2385
2386                 if (unlikely(irsp->ulpStatus)) {
2387                         /* Rsp ring <ringno> error: IOCB */
2388                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2389                                         "0326 Rsp Ring %d error: IOCB Data: "
2390                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2391                                         pring->ringno,
2392                                         irsp->un.ulpWord[0],
2393                                         irsp->un.ulpWord[1],
2394                                         irsp->un.ulpWord[2],
2395                                         irsp->un.ulpWord[3],
2396                                         irsp->un.ulpWord[4],
2397                                         irsp->un.ulpWord[5],
2398                                         *(uint32_t *)&irsp->un1,
2399                                         *((uint32_t *)&irsp->un1 + 1));
2400                 }
2401
2402                 switch (type) {
2403                 case LPFC_ABORT_IOCB:
2404                 case LPFC_SOL_IOCB:
2405                         /*
2406                          * Idle exchange closed via ABTS from port.  No iocb
2407                          * resources need to be recovered.
2408                          */
2409                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2410                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2411                                                 "0314 IOCB cmd 0x%x "
2412                                                 "processed. Skipping "
2413                                                 "completion",
2414                                                 irsp->ulpCommand);
2415                                 break;
2416                         }
2417
2418                         spin_lock_irqsave(&phba->hbalock, iflags);
2419                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2420                                                          &rspiocbq);
2421                         spin_unlock_irqrestore(&phba->hbalock, iflags);
2422                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2423                                 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2424                                                       &rspiocbq);
2425                         }
2426                         break;
2427                 default:
2428                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2429                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2430                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2431                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2432                                        MAX_MSG_DATA);
2433                                 dev_warn(&((phba->pcidev)->dev),
2434                                          "lpfc%d: %s\n",
2435                                          phba->brd_no, adaptermsg);
2436                         } else {
2437                                 /* Unknown IOCB command */
2438                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2439                                                 "0321 Unknown IOCB command "
2440                                                 "Data: x%x, x%x x%x x%x x%x\n",
2441                                                 type, irsp->ulpCommand,
2442                                                 irsp->ulpStatus,
2443                                                 irsp->ulpIoTag,
2444                                                 irsp->ulpContext);
2445                         }
2446                         break;
2447                 }
2448
2449                 /*
2450                  * The response IOCB has been processed.  Update the ring
2451                  * pointer in SLIM.  If the port response put pointer has not
2452                  * been updated, sync the pgp->rspPutInx and fetch the new port
2453                  * response put pointer.
2454                  */
2455                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2456
2457                 if (pring->rspidx == portRspPut)
2458                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2459         }
2460
2461         ha_copy = readl(phba->HAregaddr);
2462         ha_copy >>= (LPFC_FCP_RING * 4);
2463
2464         if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
2465                 spin_lock_irqsave(&phba->hbalock, iflags);
2466                 pring->stats.iocb_rsp_full++;
2467                 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
2468                 writel(status, phba->CAregaddr);
2469                 readl(phba->CAregaddr);
2470                 spin_unlock_irqrestore(&phba->hbalock, iflags);
2471         }
2472         if ((ha_copy & HA_R0CE_RSP) &&
2473             (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2474                 spin_lock_irqsave(&phba->hbalock, iflags);
2475                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2476                 pring->stats.iocb_cmd_empty++;
2477
2478                 /* Force update of the local copy of cmdGetInx */
2479                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2480                 lpfc_sli_resume_iocb(phba, pring);
2481
2482                 if ((pring->lpfc_sli_cmd_available))
2483                         (pring->lpfc_sli_cmd_available) (phba, pring);
2484
2485                 spin_unlock_irqrestore(&phba->hbalock, iflags);
2486         }
2487
2488         return;
2489 }
2490
2491 /**
2492  * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2493  * @phba: Pointer to HBA context object.
2494  * @pring: Pointer to driver SLI ring object.
2495  * @mask: Host attention register mask for this ring.
2496  *
2497  * This function is called from the interrupt context when there is a ring
2498  * event for the fcp ring. The caller does not hold any lock.
2499  * The function processes each response iocb in the response ring until it
2500  * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2501  * LE bit set. The function will call the completion handler of the command iocb
2502  * if the response iocb indicates a completion for a command iocb or it is
2503  * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2504  * function if this is an unsolicited iocb.
2505  * This routine presumes LPFC_FCP_RING handling and doesn't bother
2506  * to check it explicitly. This function always returns 1.
2507  **/
2508 static int
2509 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2510                                 struct lpfc_sli_ring *pring, uint32_t mask)
2511 {
2512         struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2513         IOCB_t *irsp = NULL;
2514         IOCB_t *entry = NULL;
2515         struct lpfc_iocbq *cmdiocbq = NULL;
2516         struct lpfc_iocbq rspiocbq;
2517         uint32_t status;
2518         uint32_t portRspPut, portRspMax;
2519         int rc = 1;
2520         lpfc_iocb_type type;
2521         unsigned long iflag;
2522         uint32_t rsp_cmpl = 0;
2523
2524         spin_lock_irqsave(&phba->hbalock, iflag);
2525         pring->stats.iocb_event++;
2526
2527         /*
2528          * The next available response entry should never exceed the maximum
2529          * entries.  If it does, treat it as an adapter hardware error.
2530          */
2531         portRspMax = pring->numRiocb;
2532         portRspPut = le32_to_cpu(pgp->rspPutInx);
2533         if (unlikely(portRspPut >= portRspMax)) {
2534                 lpfc_sli_rsp_pointers_error(phba, pring);
2535                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2536                 return 1;
2537         }
2538
2539         rmb();
2540         while (pring->rspidx != portRspPut) {
2541                 /*
2542                  * Fetch an entry off the ring and copy it into a local data
2543                  * structure.  The copy involves a byte-swap since the
2544                  * network byte order and pci byte orders are different.
2545                  */
2546                 entry = lpfc_resp_iocb(phba, pring);
2547                 phba->last_completion_time = jiffies;
2548
2549                 if (++pring->rspidx >= portRspMax)
2550                         pring->rspidx = 0;
2551
2552                 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2553                                       (uint32_t *) &rspiocbq.iocb,
2554                                       phba->iocb_rsp_size);
2555                 INIT_LIST_HEAD(&(rspiocbq.list));
2556                 irsp = &rspiocbq.iocb;
2557
2558                 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2559                 pring->stats.iocb_rsp++;
2560                 rsp_cmpl++;
2561
2562                 if (unlikely(irsp->ulpStatus)) {
2563                         /*
2564                          * If resource errors reported from HBA, reduce
2565                          * queuedepths of the SCSI device.
2566                          */
2567                         if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2568                                 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2569                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2570                                 phba->lpfc_rampdown_queue_depth(phba);
2571                                 spin_lock_irqsave(&phba->hbalock, iflag);
2572                         }
2573
2574                         /* Rsp ring <ringno> error: IOCB */
2575                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2576                                         "0336 Rsp Ring %d error: IOCB Data: "
2577                                         "x%x x%x x%x x%x x%x x%x x%x x%x\n",
2578                                         pring->ringno,
2579                                         irsp->un.ulpWord[0],
2580                                         irsp->un.ulpWord[1],
2581                                         irsp->un.ulpWord[2],
2582                                         irsp->un.ulpWord[3],
2583                                         irsp->un.ulpWord[4],
2584                                         irsp->un.ulpWord[5],
2585                                         *(uint32_t *)&irsp->un1,
2586                                         *((uint32_t *)&irsp->un1 + 1));
2587                 }
2588
2589                 switch (type) {
2590                 case LPFC_ABORT_IOCB:
2591                 case LPFC_SOL_IOCB:
2592                         /*
2593                          * Idle exchange closed via ABTS from port.  No iocb
2594                          * resources need to be recovered.
2595                          */
2596                         if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
2597                                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
2598                                                 "0333 IOCB cmd 0x%x"
2599                                                 " processed. Skipping"
2600                                                 " completion\n",
2601                                                 irsp->ulpCommand);
2602                                 break;
2603                         }
2604
2605                         cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2606                                                          &rspiocbq);
2607                         if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2608                                 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2609                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2610                                                               &rspiocbq);
2611                                 } else {
2612                                         spin_unlock_irqrestore(&phba->hbalock,
2613                                                                iflag);
2614                                         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2615                                                               &rspiocbq);
2616                                         spin_lock_irqsave(&phba->hbalock,
2617                                                           iflag);
2618                                 }
2619                         }
2620                         break;
2621                 case LPFC_UNSOL_IOCB:
2622                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2623                         lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2624                         spin_lock_irqsave(&phba->hbalock, iflag);
2625                         break;
2626                 default:
2627                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2628                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2629                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2630                                 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2631                                        MAX_MSG_DATA);
2632                                 dev_warn(&((phba->pcidev)->dev),
2633                                          "lpfc%d: %s\n",
2634                                          phba->brd_no, adaptermsg);
2635                         } else {
2636                                 /* Unknown IOCB command */
2637                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2638                                                 "0334 Unknown IOCB command "
2639                                                 "Data: x%x, x%x x%x x%x x%x\n",
2640                                                 type, irsp->ulpCommand,
2641                                                 irsp->ulpStatus,
2642                                                 irsp->ulpIoTag,
2643                                                 irsp->ulpContext);
2644                         }
2645                         break;
2646                 }
2647
2648                 /*
2649                  * The response IOCB has been processed.  Update the ring
2650                  * pointer in SLIM.  If the port response put pointer has not
2651                  * been updated, sync the pgp->rspPutInx and fetch the new port
2652                  * response put pointer.
2653                  */
2654                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2655
2656                 if (pring->rspidx == portRspPut)
2657                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2658         }
2659
2660         if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2661                 pring->stats.iocb_rsp_full++;
2662                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2663                 writel(status, phba->CAregaddr);
2664                 readl(phba->CAregaddr);
2665         }
2666         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2667                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2668                 pring->stats.iocb_cmd_empty++;
2669
2670                 /* Force update of the local copy of cmdGetInx */
2671                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2672                 lpfc_sli_resume_iocb(phba, pring);
2673
2674                 if ((pring->lpfc_sli_cmd_available))
2675                         (pring->lpfc_sli_cmd_available) (phba, pring);
2676
2677         }
2678
2679         spin_unlock_irqrestore(&phba->hbalock, iflag);
2680         return rc;
2681 }
2682
2683 /**
2684  * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2685  * @phba: Pointer to HBA context object.
2686  * @pring: Pointer to driver SLI ring object.
2687  * @rspiocbp: Pointer to driver response IOCB object.
2688  *
2689  * This function is called from the worker thread when there is a slow-path
2690  * response IOCB to process. This function chains all the response iocbs until
2691  * seeing the iocb with the LE bit set. The function will call
2692  * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2693  * completion of a command iocb. The function will call the
2694  * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2695  * The function frees the resources or calls the completion handler if this
2696  * iocb is an abort completion. The function returns NULL when the response
2697  * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2698  * this function shall chain the iocb on to the iocb_continueq and return the
2699  * response iocb passed in.
2700  **/
2701 static struct lpfc_iocbq *
2702 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2703                         struct lpfc_iocbq *rspiocbp)
2704 {
2705         struct lpfc_iocbq *saveq;
2706         struct lpfc_iocbq *cmdiocbp;
2707         struct lpfc_iocbq *next_iocb;
2708         IOCB_t *irsp = NULL;
2709         uint32_t free_saveq;
2710         uint8_t iocb_cmd_type;
2711         lpfc_iocb_type type;
2712         unsigned long iflag;
2713         int rc;
2714
2715         spin_lock_irqsave(&phba->hbalock, iflag);
2716         /* First add the response iocb to the countinueq list */
2717         list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2718         pring->iocb_continueq_cnt++;
2719
2720         /* Now, determine whetehr the list is completed for processing */
2721         irsp = &rspiocbp->iocb;
2722         if (irsp->ulpLe) {
2723                 /*
2724                  * By default, the driver expects to free all resources
2725                  * associated with this iocb completion.
2726                  */
2727                 free_saveq = 1;
2728                 saveq = list_get_first(&pring->iocb_continueq,
2729                                        struct lpfc_iocbq, list);
2730                 irsp = &(saveq->iocb);
2731                 list_del_init(&pring->iocb_continueq);
2732                 pring->iocb_continueq_cnt = 0;
2733
2734                 pring->stats.iocb_rsp++;
2735
2736                 /*
2737                  * If resource errors reported from HBA, reduce
2738                  * queuedepths of the SCSI device.
2739                  */
2740                 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2741                     (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2742                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2743                         phba->lpfc_rampdown_queue_depth(phba);
2744                         spin_lock_irqsave(&phba->hbalock, iflag);
2745                 }
2746
2747                 if (irsp->ulpStatus) {
2748                         /* Rsp ring <ringno> error: IOCB */
2749                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2750                                         "0328 Rsp Ring %d error: "
2751                                         "IOCB Data: "
2752                                         "x%x x%x x%x x%x "
2753                                         "x%x x%x x%x x%x "
2754                                         "x%x x%x x%x x%x "
2755                                         "x%x x%x x%x x%x\n",
2756                                         pring->ringno,
2757                                         irsp->un.ulpWord[0],
2758                                         irsp->un.ulpWord[1],
2759                                         irsp->un.ulpWord[2],
2760                                         irsp->un.ulpWord[3],
2761                                         irsp->un.ulpWord[4],
2762                                         irsp->un.ulpWord[5],
2763                                         *(((uint32_t *) irsp) + 6),
2764                                         *(((uint32_t *) irsp) + 7),
2765                                         *(((uint32_t *) irsp) + 8),
2766                                         *(((uint32_t *) irsp) + 9),
2767                                         *(((uint32_t *) irsp) + 10),
2768                                         *(((uint32_t *) irsp) + 11),
2769                                         *(((uint32_t *) irsp) + 12),
2770                                         *(((uint32_t *) irsp) + 13),
2771                                         *(((uint32_t *) irsp) + 14),
2772                                         *(((uint32_t *) irsp) + 15));
2773                 }
2774
2775                 /*
2776                  * Fetch the IOCB command type and call the correct completion
2777                  * routine. Solicited and Unsolicited IOCBs on the ELS ring
2778                  * get freed back to the lpfc_iocb_list by the discovery
2779                  * kernel thread.
2780                  */
2781                 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2782                 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2783                 switch (type) {
2784                 case LPFC_SOL_IOCB:
2785                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2786                         rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2787                         spin_lock_irqsave(&phba->hbalock, iflag);
2788                         break;
2789
2790                 case LPFC_UNSOL_IOCB:
2791                         spin_unlock_irqrestore(&phba->hbalock, iflag);
2792                         rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2793                         spin_lock_irqsave(&phba->hbalock, iflag);
2794                         if (!rc)
2795                                 free_saveq = 0;
2796                         break;
2797
2798                 case LPFC_ABORT_IOCB:
2799                         cmdiocbp = NULL;
2800                         if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2801                                 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2802                                                                  saveq);
2803                         if (cmdiocbp) {
2804                                 /* Call the specified completion routine */
2805                                 if (cmdiocbp->iocb_cmpl) {
2806                                         spin_unlock_irqrestore(&phba->hbalock,
2807                                                                iflag);
2808                                         (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2809                                                               saveq);
2810                                         spin_lock_irqsave(&phba->hbalock,
2811                                                           iflag);
2812                                 } else
2813                                         __lpfc_sli_release_iocbq(phba,
2814                                                                  cmdiocbp);
2815                         }
2816                         break;
2817
2818                 case LPFC_UNKNOWN_IOCB:
2819                         if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2820                                 char adaptermsg[LPFC_MAX_ADPTMSG];
2821                                 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2822                                 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2823                                        MAX_MSG_DATA);
2824                                 dev_warn(&((phba->pcidev)->dev),
2825                                          "lpfc%d: %s\n",
2826                                          phba->brd_no, adaptermsg);
2827                         } else {
2828                                 /* Unknown IOCB command */
2829                                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2830                                                 "0335 Unknown IOCB "
2831                                                 "command Data: x%x "
2832                                                 "x%x x%x x%x\n",
2833                                                 irsp->ulpCommand,
2834                                                 irsp->ulpStatus,
2835                                                 irsp->ulpIoTag,
2836                                                 irsp->ulpContext);
2837                         }
2838                         break;
2839                 }
2840
2841                 if (free_saveq) {
2842                         list_for_each_entry_safe(rspiocbp, next_iocb,
2843                                                  &saveq->list, list) {
2844                                 list_del(&rspiocbp->list);
2845                                 __lpfc_sli_release_iocbq(phba, rspiocbp);
2846                         }
2847                         __lpfc_sli_release_iocbq(phba, saveq);
2848                 }
2849                 rspiocbp = NULL;
2850         }
2851         spin_unlock_irqrestore(&phba->hbalock, iflag);
2852         return rspiocbp;
2853 }
2854
2855 /**
2856  * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
2857  * @phba: Pointer to HBA context object.
2858  * @pring: Pointer to driver SLI ring object.
2859  * @mask: Host attention register mask for this ring.
2860  *
2861  * This routine wraps the actual slow_ring event process routine from the
2862  * API jump table function pointer from the lpfc_hba struct.
2863  **/
2864 void
2865 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2866                                 struct lpfc_sli_ring *pring, uint32_t mask)
2867 {
2868         phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2869 }
2870
2871 /**
2872  * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2873  * @phba: Pointer to HBA context object.
2874  * @pring: Pointer to driver SLI ring object.
2875  * @mask: Host attention register mask for this ring.
2876  *
2877  * This function is called from the worker thread when there is a ring event
2878  * for non-fcp rings. The caller does not hold any lock. The function will
2879  * remove each response iocb in the response ring and calls the handle
2880  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2881  **/
2882 static void
2883 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2884                                    struct lpfc_sli_ring *pring, uint32_t mask)
2885 {
2886         struct lpfc_pgp *pgp;
2887         IOCB_t *entry;
2888         IOCB_t *irsp = NULL;
2889         struct lpfc_iocbq *rspiocbp = NULL;
2890         uint32_t portRspPut, portRspMax;
2891         unsigned long iflag;
2892         uint32_t status;
2893
2894         pgp = &phba->port_gp[pring->ringno];
2895         spin_lock_irqsave(&phba->hbalock, iflag);
2896         pring->stats.iocb_event++;
2897
2898         /*
2899          * The next available response entry should never exceed the maximum
2900          * entries.  If it does, treat it as an adapter hardware error.
2901          */
2902         portRspMax = pring->numRiocb;
2903         portRspPut = le32_to_cpu(pgp->rspPutInx);
2904         if (portRspPut >= portRspMax) {
2905                 /*
2906                  * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2907                  * rsp ring <portRspMax>
2908                  */
2909                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2910                                 "0303 Ring %d handler: portRspPut %d "
2911                                 "is bigger than rsp ring %d\n",
2912                                 pring->ringno, portRspPut, portRspMax);
2913
2914                 phba->link_state = LPFC_HBA_ERROR;
2915                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2916
2917                 phba->work_hs = HS_FFER3;
2918                 lpfc_handle_eratt(phba);
2919
2920                 return;
2921         }
2922
2923         rmb();
2924         while (pring->rspidx != portRspPut) {
2925                 /*
2926                  * Build a completion list and call the appropriate handler.
2927                  * The process is to get the next available response iocb, get
2928                  * a free iocb from the list, copy the response data into the
2929                  * free iocb, insert to the continuation list, and update the
2930                  * next response index to slim.  This process makes response
2931                  * iocb's in the ring available to DMA as fast as possible but
2932                  * pays a penalty for a copy operation.  Since the iocb is
2933                  * only 32 bytes, this penalty is considered small relative to
2934                  * the PCI reads for register values and a slim write.  When
2935                  * the ulpLe field is set, the entire Command has been
2936                  * received.
2937                  */
2938                 entry = lpfc_resp_iocb(phba, pring);
2939
2940                 phba->last_completion_time = jiffies;
2941                 rspiocbp = __lpfc_sli_get_iocbq(phba);
2942                 if (rspiocbp == NULL) {
2943                         printk(KERN_ERR "%s: out of buffers! Failing "
2944                                "completion.\n", __func__);
2945                         break;
2946                 }
2947
2948                 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2949                                       phba->iocb_rsp_size);
2950                 irsp = &rspiocbp->iocb;
2951
2952                 if (++pring->rspidx >= portRspMax)
2953                         pring->rspidx = 0;
2954
2955                 if (pring->ringno == LPFC_ELS_RING) {
2956                         lpfc_debugfs_slow_ring_trc(phba,
2957                         "IOCB rsp ring:   wd4:x%08x wd6:x%08x wd7:x%08x",
2958                                 *(((uint32_t *) irsp) + 4),
2959                                 *(((uint32_t *) irsp) + 6),
2960                                 *(((uint32_t *) irsp) + 7));
2961                 }
2962
2963                 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
2964
2965                 spin_unlock_irqrestore(&phba->hbalock, iflag);
2966                 /* Handle the response IOCB */
2967                 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2968                 spin_lock_irqsave(&phba->hbalock, iflag);
2969
2970                 /*
2971                  * If the port response put pointer has not been updated, sync
2972                  * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2973                  * response put pointer.
2974                  */
2975                 if (pring->rspidx == portRspPut) {
2976                         portRspPut = le32_to_cpu(pgp->rspPutInx);
2977                 }
2978         } /* while (pring->rspidx != portRspPut) */
2979
2980         if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
2981                 /* At least one response entry has been freed */
2982                 pring->stats.iocb_rsp_full++;
2983                 /* SET RxRE_RSP in Chip Att register */
2984                 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2985                 writel(status, phba->CAregaddr);
2986                 readl(phba->CAregaddr); /* flush */
2987         }
2988         if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2989                 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2990                 pring->stats.iocb_cmd_empty++;
2991
2992                 /* Force update of the local copy of cmdGetInx */
2993                 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2994                 lpfc_sli_resume_iocb(phba, pring);
2995
2996                 if ((pring->lpfc_sli_cmd_available))
2997                         (pring->lpfc_sli_cmd_available) (phba, pring);
2998
2999         }
3000
3001         spin_unlock_irqrestore(&phba->hbalock, iflag);
3002         return;
3003 }
3004
3005 /**
3006  * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3007  * @phba: Pointer to HBA context object.
3008  * @pring: Pointer to driver SLI ring object.
3009  * @mask: Host attention register mask for this ring.
3010  *
3011  * This function is called from the worker thread when there is a pending
3012  * ELS response iocb on the driver internal slow-path response iocb worker
3013  * queue. The caller does not hold any lock. The function will remove each
3014  * response iocb from the response worker queue and calls the handle
3015  * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3016  **/
3017 static void
3018 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3019                                    struct lpfc_sli_ring *pring, uint32_t mask)
3020 {
3021         struct lpfc_iocbq *irspiocbq;
3022         unsigned long iflag;
3023
3024         while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
3025                 /* Get the response iocb from the head of work queue */
3026                 spin_lock_irqsave(&phba->hbalock, iflag);
3027                 list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
3028                                  irspiocbq, struct lpfc_iocbq, list);
3029                 spin_unlock_irqrestore(&phba->hbalock, iflag);
3030                 /* Process the response iocb */
3031                 lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
3032         }
3033 }
3034
3035 /**
3036  * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3037  * @phba: Pointer to HBA context object.
3038  * @pring: Pointer to driver SLI ring object.
3039  *
3040  * This function aborts all iocbs in the given ring and frees all the iocb
3041  * objects in txq. This function issues an abort iocb for all the iocb commands
3042  * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3043  * the return of this function. The caller is not required to hold any locks.
3044  **/
3045 void
3046 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3047 {
3048         LIST_HEAD(completions);
3049         struct lpfc_iocbq *iocb, *next_iocb;
3050
3051         if (pring->ringno == LPFC_ELS_RING) {
3052                 lpfc_fabric_abort_hba(phba);
3053         }
3054
3055         /* Error everything on txq and txcmplq
3056          * First do the txq.
3057          */
3058         spin_lock_irq(&phba->hbalock);
3059         list_splice_init(&pring->txq, &completions);
3060         pring->txq_cnt = 0;
3061
3062         /* Next issue ABTS for everything on the txcmplq */
3063         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3064                 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3065
3066         spin_unlock_irq(&phba->hbalock);
3067
3068         /* Cancel all the IOCBs from the completions list */
3069         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3070                               IOERR_SLI_ABORTED);
3071 }
3072
3073 /**
3074  * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3075  * @phba: Pointer to HBA context object.
3076  *
3077  * This function flushes all iocbs in the fcp ring and frees all the iocb
3078  * objects in txq and txcmplq. This function will not issue abort iocbs
3079  * for all the iocb commands in txcmplq, they will just be returned with
3080  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3081  * slot has been permanently disabled.
3082  **/
3083 void
3084 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3085 {
3086         LIST_HEAD(txq);
3087         LIST_HEAD(txcmplq);
3088         struct lpfc_sli *psli = &phba->sli;
3089         struct lpfc_sli_ring  *pring;
3090
3091         /* Currently, only one fcp ring */
3092         pring = &psli->ring[psli->fcp_ring];
3093
3094         spin_lock_irq(&phba->hbalock);
3095         /* Retrieve everything on txq */
3096         list_splice_init(&pring->txq, &txq);
3097         pring->txq_cnt = 0;
3098
3099         /* Retrieve everything on the txcmplq */
3100         list_splice_init(&pring->txcmplq, &txcmplq);
3101         pring->txcmplq_cnt = 0;
3102         spin_unlock_irq(&phba->hbalock);
3103
3104         /* Flush the txq */
3105         lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3106                               IOERR_SLI_DOWN);
3107
3108         /* Flush the txcmpq */
3109         lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3110                               IOERR_SLI_DOWN);
3111 }
3112
3113 /**
3114  * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3115  * @phba: Pointer to HBA context object.
3116  * @mask: Bit mask to be checked.
3117  *
3118  * This function reads the host status register and compares
3119  * with the provided bit mask to check if HBA completed
3120  * the restart. This function will wait in a loop for the
3121  * HBA to complete restart. If the HBA does not restart within
3122  * 15 iterations, the function will reset the HBA again. The
3123  * function returns 1 when HBA fail to restart otherwise returns
3124  * zero.
3125  **/
3126 static int
3127 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3128 {
3129         uint32_t status;
3130         int i = 0;
3131         int retval = 0;
3132
3133         /* Read the HBA Host Status Register */
3134         status = readl(phba->HSregaddr);
3135
3136         /*
3137          * Check status register every 100ms for 5 retries, then every
3138          * 500ms for 5, then every 2.5 sec for 5, then reset board and
3139          * every 2.5 sec for 4.
3140          * Break our of the loop if errors occurred during init.
3141          */
3142         while (((status & mask) != mask) &&
3143                !(status & HS_FFERM) &&
3144                i++ < 20) {
3145
3146                 if (i <= 5)
3147                         msleep(10);
3148                 else if (i <= 10)
3149                         msleep(500);
3150                 else
3151                         msleep(2500);
3152
3153                 if (i == 15) {
3154                                 /* Do post */
3155                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3156                         lpfc_sli_brdrestart(phba);
3157                 }
3158                 /* Read the HBA Host Status Register */
3159                 status = readl(phba->HSregaddr);
3160         }
3161
3162         /* Check to see if any errors occurred during init */
3163         if ((status & HS_FFERM) || (i >= 20)) {
3164                 phba->link_state = LPFC_HBA_ERROR;
3165                 retval = 1;
3166         }
3167
3168         return retval;
3169 }
3170
3171 /**
3172  * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3173  * @phba: Pointer to HBA context object.
3174  * @mask: Bit mask to be checked.
3175  *
3176  * This function checks the host status register to check if HBA is
3177  * ready. This function will wait in a loop for the HBA to be ready
3178  * If the HBA is not ready , the function will will reset the HBA PCI
3179  * function again. The function returns 1 when HBA fail to be ready
3180  * otherwise returns zero.
3181  **/
3182 static int
3183 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3184 {
3185         uint32_t status;
3186         int retval = 0;
3187
3188         /* Read the HBA Host Status Register */
3189         status = lpfc_sli4_post_status_check(phba);
3190
3191         if (status) {
3192                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3193                 lpfc_sli_brdrestart(phba);
3194                 status = lpfc_sli4_post_status_check(phba);
3195         }
3196
3197         /* Check to see if any errors occurred during init */
3198         if (status) {
3199                 phba->link_state = LPFC_HBA_ERROR;
3200                 retval = 1;
3201         } else
3202                 phba->sli4_hba.intr_enable = 0;
3203
3204         return retval;
3205 }
3206
3207 /**
3208  * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3209  * @phba: Pointer to HBA context object.
3210  * @mask: Bit mask to be checked.
3211  *
3212  * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3213  * from the API jump table function pointer from the lpfc_hba struct.
3214  **/
3215 int
3216 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3217 {
3218         return phba->lpfc_sli_brdready(phba, mask);
3219 }
3220
3221 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3222
3223 /**
3224  * lpfc_reset_barrier - Make HBA ready for HBA reset
3225  * @phba: Pointer to HBA context object.
3226  *
3227  * This function is called before resetting an HBA. This
3228  * function requests HBA to quiesce DMAs before a reset.
3229  **/
3230 void lpfc_reset_barrier(struct lpfc_hba *phba)
3231 {
3232         uint32_t __iomem *resp_buf;
3233         uint32_t __iomem *mbox_buf;
3234         volatile uint32_t mbox;
3235         uint32_t hc_copy;
3236         int  i;
3237         uint8_t hdrtype;
3238
3239         pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3240         if (hdrtype != 0x80 ||
3241             (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3242              FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3243                 return;
3244
3245         /*
3246          * Tell the other part of the chip to suspend temporarily all
3247          * its DMA activity.
3248          */
3249         resp_buf = phba->MBslimaddr;
3250
3251         /* Disable the error attention */
3252         hc_copy = readl(phba->HCregaddr);
3253         writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3254         readl(phba->HCregaddr); /* flush */
3255         phba->link_flag |= LS_IGNORE_ERATT;
3256
3257         if (readl(phba->HAregaddr) & HA_ERATT) {
3258                 /* Clear Chip error bit */
3259                 writel(HA_ERATT, phba->HAregaddr);
3260                 phba->pport->stopped = 1;
3261         }
3262
3263         mbox = 0;
3264         ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3265         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3266
3267         writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3268         mbox_buf = phba->MBslimaddr;
3269         writel(mbox, mbox_buf);
3270
3271         for (i = 0;
3272              readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3273                 mdelay(1);
3274
3275         if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
3276                 if (phba->sli.sli_flag & LPFC_SLI2_ACTIVE ||
3277                     phba->pport->stopped)
3278                         goto restore_hc;
3279                 else
3280                         goto clear_errat;
3281         }
3282
3283         ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3284         for (i = 0; readl(resp_buf) != mbox &&  i < 500; i++)
3285                 mdelay(1);
3286
3287 clear_errat:
3288
3289         while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3290                 mdelay(1);
3291
3292         if (readl(phba->HAregaddr) & HA_ERATT) {
3293                 writel(HA_ERATT, phba->HAregaddr);
3294                 phba->pport->stopped = 1;
3295         }
3296
3297 restore_hc:
3298         phba->link_flag &= ~LS_IGNORE_ERATT;
3299         writel(hc_copy, phba->HCregaddr);
3300         readl(phba->HCregaddr); /* flush */
3301 }
3302
3303 /**
3304  * lpfc_sli_brdkill - Issue a kill_board mailbox command
3305  * @phba: Pointer to HBA context object.
3306  *
3307  * This function issues a kill_board mailbox command and waits for
3308  * the error attention interrupt. This function is called for stopping
3309  * the firmware processing. The caller is not required to hold any
3310  * locks. This function calls lpfc_hba_down_post function to free
3311  * any pending commands after the kill. The function will return 1 when it
3312  * fails to kill the board else will return 0.
3313  **/
3314 int
3315 lpfc_sli_brdkill(struct lpfc_hba *phba)
3316 {
3317         struct lpfc_sli *psli;
3318         LPFC_MBOXQ_t *pmb;
3319         uint32_t status;
3320         uint32_t ha_copy;
3321         int retval;
3322         int i = 0;
3323
3324         psli = &phba->sli;
3325
3326         /* Kill HBA */
3327         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3328                         "0329 Kill HBA Data: x%x x%x\n",
3329                         phba->pport->port_state, psli->sli_flag);
3330
3331         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3332         if (!pmb)
3333                 return 1;
3334
3335         /* Disable the error attention */
3336         spin_lock_irq(&phba->hbalock);
3337         status = readl(phba->HCregaddr);
3338         status &= ~HC_ERINT_ENA;
3339         writel(status, phba->HCregaddr);
3340         readl(phba->HCregaddr); /* flush */
3341         phba->link_flag |= LS_IGNORE_ERATT;
3342         spin_unlock_irq(&phba->hbalock);
3343
3344         lpfc_kill_board(phba, pmb);
3345         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3346         retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3347
3348         if (retval != MBX_SUCCESS) {
3349                 if (retval != MBX_BUSY)
3350                         mempool_free(pmb, phba->mbox_mem_pool);
3351                 spin_lock_irq(&phba->hbalock);
3352                 phba->link_flag &= ~LS_IGNORE_ERATT;
3353                 spin_unlock_irq(&phba->hbalock);
3354                 return 1;
3355         }
3356
3357         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
3358
3359         mempool_free(pmb, phba->mbox_mem_pool);
3360
3361         /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3362          * attention every 100ms for 3 seconds. If we don't get ERATT after
3363          * 3 seconds we still set HBA_ERROR state because the status of the
3364          * board is now undefined.
3365          */
3366         ha_copy = readl(phba->HAregaddr);
3367
3368         while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3369                 mdelay(100);
3370                 ha_copy = readl(phba->HAregaddr);
3371         }
3372
3373         del_timer_sync(&psli->mbox_tmo);
3374         if (ha_copy & HA_ERATT) {
3375                 writel(HA_ERATT, phba->HAregaddr);
3376                 phba->pport->stopped = 1;
3377         }
3378         spin_lock_irq(&phba->hbalock);
3379         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3380         phba->link_flag &= ~LS_IGNORE_ERATT;
3381         spin_unlock_irq(&phba->hbalock);
3382
3383         psli->mbox_active = NULL;
3384         lpfc_hba_down_post(phba);
3385         phba->link_state = LPFC_HBA_ERROR;
3386
3387         return ha_copy & HA_ERATT ? 0 : 1;
3388 }
3389
3390 /**
3391  * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3392  * @phba: Pointer to HBA context object.
3393  *
3394  * This function resets the HBA by writing HC_INITFF to the control
3395  * register. After the HBA resets, this function resets all the iocb ring
3396  * indices. This function disables PCI layer parity checking during
3397  * the reset.
3398  * This function returns 0 always.
3399  * The caller is not required to hold any locks.
3400  **/
3401 int
3402 lpfc_sli_brdreset(struct lpfc_hba *phba)
3403 {
3404         struct lpfc_sli *psli;
3405         struct lpfc_sli_ring *pring;
3406         uint16_t cfg_value;
3407         int i;
3408
3409         psli = &phba->sli;
3410
3411         /* Reset HBA */
3412         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3413                         "0325 Reset HBA Data: x%x x%x\n",
3414                         phba->pport->port_state, psli->sli_flag);
3415
3416         /* perform board reset */
3417         phba->fc_eventTag = 0;
3418         phba->pport->fc_myDID = 0;
3419         phba->pport->fc_prevDID = 0;
3420
3421         /* Turn off parity checking and serr during the physical reset */
3422         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3423         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3424                               (cfg_value &
3425                                ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3426
3427         psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3428
3429         /* Now toggle INITFF bit in the Host Control Register */
3430         writel(HC_INITFF, phba->HCregaddr);
3431         mdelay(1);
3432         readl(phba->HCregaddr); /* flush */
3433         writel(0, phba->HCregaddr);
3434         readl(phba->HCregaddr); /* flush */
3435
3436         /* Restore PCI cmd register */
3437         pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3438
3439         /* Initialize relevant SLI info */
3440         for (i = 0; i < psli->num_rings; i++) {
3441                 pring = &psli->ring[i];
3442                 pring->flag = 0;
3443                 pring->rspidx = 0;
3444                 pring->next_cmdidx  = 0;
3445                 pring->local_getidx = 0;
3446                 pring->cmdidx = 0;
3447                 pring->missbufcnt = 0;
3448         }
3449
3450         phba->link_state = LPFC_WARM_START;
3451         return 0;
3452 }
3453
3454 /**
3455  * lpfc_sli4_brdreset - Reset a sli-4 HBA
3456  * @phba: Pointer to HBA context object.
3457  *
3458  * This function resets a SLI4 HBA. This function disables PCI layer parity
3459  * checking during resets the device. The caller is not required to hold
3460  * any locks.
3461  *
3462  * This function returns 0 always.
3463  **/
3464 int
3465 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3466 {
3467         struct lpfc_sli *psli = &phba->sli;
3468         uint16_t cfg_value;
3469         uint8_t qindx;
3470
3471         /* Reset HBA */
3472         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3473                         "0295 Reset HBA Data: x%x x%x\n",
3474                         phba->pport->port_state, psli->sli_flag);
3475
3476         /* perform board reset */
3477         phba->fc_eventTag = 0;
3478         phba->pport->fc_myDID = 0;
3479         phba->pport->fc_prevDID = 0;
3480
3481         /* Turn off parity checking and serr during the physical reset */
3482         pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3483         pci_write_config_word(phba->pcidev, PCI_COMMAND,
3484                               (cfg_value &
3485                               ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3486
3487         spin_lock_irq(&phba->hbalock);
3488         psli->sli_flag &= ~(LPFC_PROCESS_LA);
3489         phba->fcf.fcf_flag = 0;
3490         /* Clean up the child queue list for the CQs */
3491         list_del_init(&phba->sli4_hba.mbx_wq->list);
3492         list_del_init(&phba->sli4_hba.els_wq->list);
3493         list_del_init(&phba->sli4_hba.hdr_rq->list);
3494         list_del_init(&phba->sli4_hba.dat_rq->list);
3495         list_del_init(&phba->sli4_hba.mbx_cq->list);
3496         list_del_init(&phba->sli4_hba.els_cq->list);
3497         list_del_init(&phba->sli4_hba.rxq_cq->list);
3498         for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3499                 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3500         for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3501                 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3502         spin_unlock_irq(&phba->hbalock);
3503
3504         /* Now physically reset the device */
3505         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3506                         "0389 Performing PCI function reset!\n");
3507         /* Perform FCoE PCI function reset */
3508         lpfc_pci_function_reset(phba);
3509
3510         return 0;
3511 }
3512
3513 /**
3514  * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
3515  * @phba: Pointer to HBA context object.
3516  *
3517  * This function is called in the SLI initialization code path to
3518  * restart the HBA. The caller is not required to hold any lock.
3519  * This function writes MBX_RESTART mailbox command to the SLIM and
3520  * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3521  * function to free any pending commands. The function enables
3522  * POST only during the first initialization. The function returns zero.
3523  * The function does not guarantee completion of MBX_RESTART mailbox
3524  * command before the return of this function.
3525  **/
3526 static int
3527 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
3528 {
3529         MAILBOX_t *mb;
3530         struct lpfc_sli *psli;
3531         volatile uint32_t word0;
3532         void __iomem *to_slim;
3533
3534         spin_lock_irq(&phba->hbalock);
3535
3536         psli = &phba->sli;
3537
3538         /* Restart HBA */
3539         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3540                         "0337 Restart HBA Data: x%x x%x\n",
3541                         phba->pport->port_state, psli->sli_flag);
3542
3543         word0 = 0;
3544         mb = (MAILBOX_t *) &word0;
3545         mb->mbxCommand = MBX_RESTART;
3546         mb->mbxHc = 1;
3547
3548         lpfc_reset_barrier(phba);
3549
3550         to_slim = phba->MBslimaddr;
3551         writel(*(uint32_t *) mb, to_slim);
3552         readl(to_slim); /* flush */
3553
3554         /* Only skip post after fc_ffinit is completed */
3555         if (phba->pport->port_state)
3556                 word0 = 1;      /* This is really setting up word1 */
3557         else
3558                 word0 = 0;      /* This is really setting up word1 */
3559         to_slim = phba->MBslimaddr + sizeof (uint32_t);
3560         writel(*(uint32_t *) mb, to_slim);
3561         readl(to_slim); /* flush */
3562
3563         lpfc_sli_brdreset(phba);
3564         phba->pport->stopped = 0;
3565         phba->link_state = LPFC_INIT_START;
3566         phba->hba_flag = 0;
3567         spin_unlock_irq(&phba->hbalock);
3568
3569         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3570         psli->stats_start = get_seconds();
3571
3572         /* Give the INITFF and Post time to settle. */
3573         mdelay(100);
3574
3575         lpfc_hba_down_post(phba);
3576
3577         return 0;
3578 }
3579
3580 /**
3581  * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3582  * @phba: Pointer to HBA context object.
3583  *
3584  * This function is called in the SLI initialization code path to restart
3585  * a SLI4 HBA. The caller is not required to hold any lock.
3586  * At the end of the function, it calls lpfc_hba_down_post function to
3587  * free any pending commands.
3588  **/
3589 static int
3590 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3591 {
3592         struct lpfc_sli *psli = &phba->sli;
3593
3594
3595         /* Restart HBA */
3596         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3597                         "0296 Restart HBA Data: x%x x%x\n",
3598                         phba->pport->port_state, psli->sli_flag);
3599
3600         lpfc_sli4_brdreset(phba);
3601
3602         spin_lock_irq(&phba->hbalock);
3603         phba->pport->stopped = 0;
3604         phba->link_state = LPFC_INIT_START;
3605         phba->hba_flag = 0;
3606         spin_unlock_irq(&phba->hbalock);
3607
3608         memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3609         psli->stats_start = get_seconds();
3610
3611         lpfc_hba_down_post(phba);
3612
3613         return 0;
3614 }
3615
3616 /**
3617  * lpfc_sli_brdrestart - Wrapper func for restarting hba
3618  * @phba: Pointer to HBA context object.
3619  *
3620  * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3621  * API jump table function pointer from the lpfc_hba struct.
3622 **/
3623 int
3624 lpfc_sli_brdrestart(struct lpfc_hba *phba)
3625 {
3626         return phba->lpfc_sli_brdrestart(phba);
3627 }
3628
3629 /**
3630  * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
3631  * @phba: Pointer to HBA context object.
3632  *
3633  * This function is called after a HBA restart to wait for successful
3634  * restart of the HBA. Successful restart of the HBA is indicated by
3635  * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3636  * iteration, the function will restart the HBA again. The function returns
3637  * zero if HBA successfully restarted else returns negative error code.
3638  **/
3639 static int
3640 lpfc_sli_chipset_init(struct lpfc_hba *phba)
3641 {
3642         uint32_t status, i = 0;
3643
3644         /* Read the HBA Host Status Register */
3645         status = readl(phba->HSregaddr);
3646
3647         /* Check status register to see what current state is */
3648         i = 0;
3649         while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3650
3651                 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3652                  * every 2.5 sec for 5, then reset board and every 2.5 sec for
3653                  * 4.
3654                  */
3655                 if (i++ >= 20) {
3656                         /* Adapter failed to init, timeout, status reg
3657                            <status> */
3658                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3659                                         "0436 Adapter failed to init, "
3660                                         "timeout, status reg x%x, "
3661                                         "FW Data: A8 x%x AC x%x\n", status,
3662                                         readl(phba->MBslimaddr + 0xa8),
3663                                         readl(phba->MBslimaddr + 0xac));
3664                         phba->link_state = LPFC_HBA_ERROR;
3665                         return -ETIMEDOUT;
3666                 }
3667
3668                 /* Check to see if any errors occurred during init */
3669                 if (status & HS_FFERM) {
3670                         /* ERROR: During chipset initialization */
3671                         /* Adapter failed to init, chipset, status reg
3672                            <status> */
3673                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3674                                         "0437 Adapter failed to init, "
3675                                         "chipset, status reg x%x, "
3676                                         "FW Data: A8 x%x AC x%x\n", status,
3677                                         readl(phba->MBslimaddr + 0xa8),
3678                                         readl(phba->MBslimaddr + 0xac));
3679                         phba->link_state = LPFC_HBA_ERROR;
3680                         return -EIO;
3681                 }
3682
3683                 if (i <= 5) {
3684                         msleep(10);
3685                 } else if (i <= 10) {
3686                         msleep(500);
3687                 } else {
3688                         msleep(2500);
3689                 }
3690
3691                 if (i == 15) {
3692                                 /* Do post */
3693                         phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3694                         lpfc_sli_brdrestart(phba);
3695                 }
3696                 /* Read the HBA Host Status Register */
3697                 status = readl(phba->HSregaddr);
3698         }
3699
3700         /* Check to see if any errors occurred during init */
3701         if (status & HS_FFERM) {
3702                 /* ERROR: During chipset initialization */
3703                 /* Adapter failed to init, chipset, status reg <status> */
3704                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3705                                 "0438 Adapter failed to init, chipset, "
3706                                 "status reg x%x, "
3707                                 "FW Data: A8 x%x AC x%x\n", status,
3708                                 readl(phba->MBslimaddr + 0xa8),
3709                                 readl(phba->MBslimaddr + 0xac));
3710                 phba->link_state = LPFC_HBA_ERROR;
3711                 return -EIO;
3712         }
3713
3714         /* Clear all interrupt enable conditions */
3715         writel(0, phba->HCregaddr);
3716         readl(phba->HCregaddr); /* flush */
3717
3718         /* setup host attn register */
3719         writel(0xffffffff, phba->HAregaddr);
3720         readl(phba->HAregaddr); /* flush */
3721         return 0;
3722 }
3723
3724 /**
3725  * lpfc_sli_hbq_count - Get the number of HBQs to be configured
3726  *
3727  * This function calculates and returns the number of HBQs required to be
3728  * configured.
3729  **/
3730 int
3731 lpfc_sli_hbq_count(void)
3732 {
3733         return ARRAY_SIZE(lpfc_hbq_defs);
3734 }
3735
3736 /**
3737  * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
3738  *
3739  * This function adds the number of hbq entries in every HBQ to get
3740  * the total number of hbq entries required for the HBA and returns
3741  * the total count.
3742  **/
3743 static int
3744 lpfc_sli_hbq_entry_count(void)
3745 {
3746         int  hbq_count = lpfc_sli_hbq_count();
3747         int  count = 0;
3748         int  i;
3749
3750         for (i = 0; i < hbq_count; ++i)
3751                 count += lpfc_hbq_defs[i]->entry_count;
3752         return count;
3753 }
3754
3755 /**
3756  * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
3757  *
3758  * This function calculates amount of memory required for all hbq entries
3759  * to be configured and returns the total memory required.
3760  **/
3761 int
3762 lpfc_sli_hbq_size(void)
3763 {
3764         return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3765 }
3766
3767 /**
3768  * lpfc_sli_hbq_setup - configure and initialize HBQs
3769  * @phba: Pointer to HBA context object.
3770  *
3771  * This function is called during the SLI initialization to configure
3772  * all the HBQs and post buffers to the HBQ. The caller is not
3773  * required to hold any locks. This function will return zero if successful
3774  * else it will return negative error code.
3775  **/
3776 static int
3777 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3778 {
3779         int  hbq_count = lpfc_sli_hbq_count();
3780         LPFC_MBOXQ_t *pmb;
3781         MAILBOX_t *pmbox;
3782         uint32_t hbqno;
3783         uint32_t hbq_entry_index;
3784
3785                                 /* Get a Mailbox buffer to setup mailbox
3786                                  * commands for HBA initialization
3787                                  */
3788         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3789
3790         if (!pmb)
3791                 return -ENOMEM;
3792
3793         pmbox = &pmb->mb;
3794
3795         /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3796         phba->link_state = LPFC_INIT_MBX_CMDS;
3797         phba->hbq_in_use = 1;
3798
3799         hbq_entry_index = 0;
3800         for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3801                 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3802                 phba->hbqs[hbqno].hbqPutIdx      = 0;
3803                 phba->hbqs[hbqno].local_hbqGetIdx   = 0;
3804                 phba->hbqs[hbqno].entry_count =
3805                         lpfc_hbq_defs[hbqno]->entry_count;
3806                 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3807                         hbq_entry_index, pmb);
3808                 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3809
3810                 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3811                         /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3812                            mbxStatus <status>, ring <num> */
3813
3814                         lpfc_printf_log(phba, KERN_ERR,
3815                                         LOG_SLI | LOG_VPORT,
3816                                         "1805 Adapter failed to init. "
3817                                         "Data: x%x x%x x%x\n",
3818                                         pmbox->mbxCommand,
3819                                         pmbox->mbxStatus, hbqno);
3820
3821                         phba->link_state = LPFC_HBA_ERROR;
3822                         mempool_free(pmb, phba->mbox_mem_pool);
3823                         return ENXIO;
3824                 }
3825         }
3826         phba->hbq_count = hbq_count;
3827
3828         mempool_free(pmb, phba->mbox_mem_pool);
3829
3830         /* Initially populate or replenish the HBQs */
3831         for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3832                 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
3833         return 0;
3834 }
3835
3836 /**
3837  * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3838  * @phba: Pointer to HBA context object.
3839  *
3840  * This function is called during the SLI initialization to configure
3841  * all the HBQs and post buffers to the HBQ. The caller is not
3842  * required to hold any locks. This function will return zero if successful
3843  * else it will return negative error code.
3844  **/
3845 static int
3846 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3847 {
3848         phba->hbq_in_use = 1;
3849         phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3850         phba->hbq_count = 1;
3851         /* Initially populate or replenish the HBQs */
3852         lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3853         return 0;
3854 }
3855
3856 /**
3857  * lpfc_sli_config_port - Issue config port mailbox command
3858  * @phba: Pointer to HBA context object.
3859  * @sli_mode: sli mode - 2/3
3860  *
3861  * This function is called by the sli intialization code path
3862  * to issue config_port mailbox command. This function restarts the
3863  * HBA firmware and issues a config_port mailbox command to configure
3864  * the SLI interface in the sli mode specified by sli_mode
3865  * variable. The caller is not required to hold any locks.
3866  * The function returns 0 if successful, else returns negative error
3867  * code.
3868  **/
3869 int
3870 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
3871 {
3872         LPFC_MBOXQ_t *pmb;
3873         uint32_t resetcount = 0, rc = 0, done = 0;
3874
3875         pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3876         if (!pmb) {
3877                 phba->link_state = LPFC_HBA_ERROR;
3878                 return -ENOMEM;
3879         }
3880
3881         phba->sli_rev = sli_mode;
3882         while (resetcount < 2 && !done) {
3883                 spin_lock_irq(&phba->hbalock);
3884                 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
3885                 spin_unlock_irq(&phba->hbalock);
3886                 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3887                 lpfc_sli_brdrestart(phba);
3888                 rc = lpfc_sli_chipset_init(phba);
3889                 if (rc)
3890                         break;
3891
3892                 spin_lock_irq(&phba->hbalock);
3893                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3894                 spin_unlock_irq(&phba->hbalock);
3895                 resetcount++;
3896
3897                 /* Call pre CONFIG_PORT mailbox command initialization.  A
3898                  * value of 0 means the call was successful.  Any other
3899                  * nonzero value is a failure, but if ERESTART is returned,
3900                  * the driver may reset the HBA and try again.
3901                  */
3902                 rc = lpfc_config_port_prep(phba);
3903                 if (rc == -ERESTART) {
3904                         phba->link_state = LPFC_LINK_UNKNOWN;
3905                         continue;
3906                 } else if (rc)
3907                         break;
3908                 phba->link_state = LPFC_INIT_MBX_CMDS;
3909                 lpfc_config_port(phba, pmb);
3910                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
3911                 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3912                                         LPFC_SLI3_HBQ_ENABLED |
3913                                         LPFC_SLI3_CRP_ENABLED |
3914                                         LPFC_SLI3_INB_ENABLED |
3915                                         LPFC_SLI3_BG_ENABLED);
3916                 if (rc != MBX_SUCCESS) {
3917                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3918                                 "0442 Adapter failed to init, mbxCmd x%x "
3919                                 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
3920                                 pmb->mb.mbxCommand, pmb->mb.mbxStatus, 0);
3921                         spin_lock_irq(&phba->hbalock);
3922                         phba->sli.sli_flag &= ~LPFC_SLI2_ACTIVE;
3923                         spin_unlock_irq(&phba->hbalock);
3924                         rc = -ENXIO;
3925                 } else
3926                         done = 1;
3927         }
3928         if (!done) {
3929                 rc = -EINVAL;
3930                 goto do_prep_failed;
3931         }
3932         if (pmb->mb.un.varCfgPort.sli_mode == 3) {
3933                 if (!pmb->mb.un.varCfgPort.cMA) {
3934                         rc = -ENXIO;
3935                         goto do_prep_failed;
3936                 }
3937                 if (phba->max_vpi && pmb->mb.un.varCfgPort.gmv) {
3938                         phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
3939                         phba->max_vpi = pmb->mb.un.varCfgPort.max_vpi;
3940                 } else
3941                         phba->max_vpi = 0;
3942                 if (pmb->mb.un.varCfgPort.gerbm)
3943                         phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
3944                 if (pmb->mb.un.varCfgPort.gcrp)
3945                         phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
3946                 if (pmb->mb.un.varCfgPort.ginb) {
3947                         phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
3948                         phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
3949                         phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3950                         phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3951                         phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3952                         phba->inb_last_counter =
3953                                         phba->mbox->us.s3_inb_pgp.counter;
3954                 } else {
3955                         phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
3956                         phba->port_gp = phba->mbox->us.s3_pgp.port;
3957                         phba->inb_ha_copy = NULL;
3958                         phba->inb_counter = NULL;
3959                 }
3960
3961                 if (phba->cfg_enable_bg) {
3962                         if (pmb->mb.un.varCfgPort.gbg)
3963                                 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3964                         else
3965                                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3966                                                 "0443 Adapter did not grant "
3967                                                 "BlockGuard\n");
3968                 }
3969         } else {
3970                 phba->hbq_get = NULL;
3971                 phba->port_gp = phba->mbox->us.s2.port;
3972                 phba->inb_ha_copy = NULL;
3973                 phba->inb_counter = NULL;
3974                 phba->max_vpi = 0;
3975         }
3976 do_prep_failed:
3977         mempool_free(pmb, phba->mbox_mem_pool);
3978         return rc;
3979 }
3980
3981
3982 /**
3983  * lpfc_sli_hba_setup - SLI intialization function
3984  * @phba: Pointer to HBA context object.
3985  *
3986  * This function is the main SLI intialization function. This function
3987  * is called by the HBA intialization code, HBA reset code and HBA
3988  * error attention handler code. Caller is not required to hold any
3989  * locks. This function issues config_port mailbox command to configure
3990  * the SLI, setup iocb rings and HBQ rings. In the end the function
3991  * calls the config_port_post function to issue init_link mailbox
3992  * command and to start the discovery. The function will return zero
3993  * if successful, else it will return negative error code.
3994  **/
3995 int
3996 lpfc_sli_hba_setup(struct lpfc_hba *phba)
3997 {
3998         uint32_t rc;
3999         int  mode = 3;
4000
4001         switch (lpfc_sli_mode) {
4002         case 2:
4003                 if (phba->cfg_enable_npiv) {
4004                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4005                                 "1824 NPIV enabled: Override lpfc_sli_mode "
4006                                 "parameter (%d) to auto (0).\n",
4007                                 lpfc_sli_mode);
4008                         break;
4009                 }
4010                 mode = 2;
4011                 break;
4012         case 0:
4013         case 3:
4014                 break;
4015         default:
4016                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4017                                 "1819 Unrecognized lpfc_sli_mode "
4018                                 "parameter: %d.\n", lpfc_sli_mode);
4019
4020                 break;
4021         }
4022
4023         rc = lpfc_sli_config_port(phba, mode);
4024
4025         if (rc && lpfc_sli_mode == 3)
4026                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4027                                 "1820 Unable to select SLI-3.  "
4028                                 "Not supported by adapter.\n");
4029         if (rc && mode != 2)
4030                 rc = lpfc_sli_config_port(phba, 2);
4031         if (rc)
4032                 goto lpfc_sli_hba_setup_error;
4033
4034         if (phba->sli_rev == 3) {
4035                 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4036                 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4037         } else {
4038                 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4039                 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4040                 phba->sli3_options = 0;
4041         }
4042
4043         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4044                         "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4045                         phba->sli_rev, phba->max_vpi);
4046         rc = lpfc_sli_ring_map(phba);
4047
4048         if (rc)
4049                 goto lpfc_sli_hba_setup_error;
4050
4051         /* Init HBQs */
4052         if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4053                 rc = lpfc_sli_hbq_setup(phba);
4054                 if (rc)
4055                         goto lpfc_sli_hba_setup_error;
4056         }
4057
4058         phba->sli.sli_flag |= LPFC_PROCESS_LA;
4059
4060         rc = lpfc_config_port_post(phba);
4061         if (rc)
4062                 goto lpfc_sli_hba_setup_error;
4063
4064         return rc;
4065
4066 lpfc_sli_hba_setup_error:
4067         phba->link_state = LPFC_HBA_ERROR;
4068         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4069                         "0445 Firmware initialization failed\n");
4070         return rc;
4071 }
4072
4073 /**
4074  * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4075  * @phba: Pointer to HBA context object.
4076  * @mboxq: mailbox pointer.
4077  * This function issue a dump mailbox command to read config region
4078  * 23 and parse the records in the region and populate driver
4079  * data structure.
4080  **/
4081 static int
4082 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4083                 LPFC_MBOXQ_t *mboxq)
4084 {
4085         struct lpfc_dmabuf *mp;
4086         struct lpfc_mqe *mqe;
4087         uint32_t data_length;
4088         int rc;
4089
4090         /* Program the default value of vlan_id and fc_map */
4091         phba->valid_vlan = 0;
4092         phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4093         phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4094         phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4095
4096         mqe = &mboxq->u.mqe;
4097         if (lpfc_dump_fcoe_param(phba, mboxq))
4098                 return -ENOMEM;
4099
4100         mp = (struct lpfc_dmabuf *) mboxq->context1;
4101         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4102
4103         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4104                         "(%d):2571 Mailbox cmd x%x Status x%x "
4105                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4106                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4107                         "CQ: x%x x%x x%x x%x\n",
4108                         mboxq->vport ? mboxq->vport->vpi : 0,
4109                         bf_get(lpfc_mqe_command, mqe),
4110                         bf_get(lpfc_mqe_status, mqe),
4111                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4112                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4113                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4114                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4115                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4116                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4117                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4118                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4119                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4120                         mboxq->mcqe.word0,
4121                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4122                         mboxq->mcqe.trailer);
4123
4124         if (rc) {
4125                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4126                 kfree(mp);
4127                 return -EIO;
4128         }
4129         data_length = mqe->un.mb_words[5];
4130         if (data_length > DMP_FCOEPARAM_RGN_SIZE)
4131                 return -EIO;
4132
4133         lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4134         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4135         kfree(mp);
4136         return 0;
4137 }
4138
4139 /**
4140  * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4141  * @phba: pointer to lpfc hba data structure.
4142  * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4143  * @vpd: pointer to the memory to hold resulting port vpd data.
4144  * @vpd_size: On input, the number of bytes allocated to @vpd.
4145  *            On output, the number of data bytes in @vpd.
4146  *
4147  * This routine executes a READ_REV SLI4 mailbox command.  In
4148  * addition, this routine gets the port vpd data.
4149  *
4150  * Return codes
4151  *      0 - sucessful
4152  *      ENOMEM - could not allocated memory.
4153  **/
4154 static int
4155 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4156                     uint8_t *vpd, uint32_t *vpd_size)
4157 {
4158         int rc = 0;
4159         uint32_t dma_size;
4160         struct lpfc_dmabuf *dmabuf;
4161         struct lpfc_mqe *mqe;
4162
4163         dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4164         if (!dmabuf)
4165                 return -ENOMEM;
4166
4167         /*
4168          * Get a DMA buffer for the vpd data resulting from the READ_REV
4169          * mailbox command.
4170          */
4171         dma_size = *vpd_size;
4172         dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4173                                           dma_size,
4174                                           &dmabuf->phys,
4175                                           GFP_KERNEL);
4176         if (!dmabuf->virt) {
4177                 kfree(dmabuf);
4178                 return -ENOMEM;
4179         }
4180         memset(dmabuf->virt, 0, dma_size);
4181
4182         /*
4183          * The SLI4 implementation of READ_REV conflicts at word1,
4184          * bits 31:16 and SLI4 adds vpd functionality not present
4185          * in SLI3.  This code corrects the conflicts.
4186          */
4187         lpfc_read_rev(phba, mboxq);
4188         mqe = &mboxq->u.mqe;
4189         mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4190         mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4191         mqe->un.read_rev.word1 &= 0x0000FFFF;
4192         bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4193         bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4194
4195         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4196         if (rc) {
4197                 dma_free_coherent(&phba->pcidev->dev, dma_size,
4198                                   dmabuf->virt, dmabuf->phys);
4199                 return -EIO;
4200         }
4201
4202         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4203                         "(%d):0380 Mailbox cmd x%x Status x%x "
4204                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4205                         "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4206                         "CQ: x%x x%x x%x x%x\n",
4207                         mboxq->vport ? mboxq->vport->vpi : 0,
4208                         bf_get(lpfc_mqe_command, mqe),
4209                         bf_get(lpfc_mqe_status, mqe),
4210                         mqe->un.mb_words[0], mqe->un.mb_words[1],
4211                         mqe->un.mb_words[2], mqe->un.mb_words[3],
4212                         mqe->un.mb_words[4], mqe->un.mb_words[5],
4213                         mqe->un.mb_words[6], mqe->un.mb_words[7],
4214                         mqe->un.mb_words[8], mqe->un.mb_words[9],
4215                         mqe->un.mb_words[10], mqe->un.mb_words[11],
4216                         mqe->un.mb_words[12], mqe->un.mb_words[13],
4217                         mqe->un.mb_words[14], mqe->un.mb_words[15],
4218                         mqe->un.mb_words[16], mqe->un.mb_words[50],
4219                         mboxq->mcqe.word0,
4220                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
4221                         mboxq->mcqe.trailer);
4222
4223         /*
4224          * The available vpd length cannot be bigger than the
4225          * DMA buffer passed to the port.  Catch the less than
4226          * case and update the caller's size.
4227          */
4228         if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4229                 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4230
4231         lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4232         dma_free_coherent(&phba->pcidev->dev, dma_size,
4233                           dmabuf->virt, dmabuf->phys);
4234         kfree(dmabuf);
4235         return 0;
4236 }
4237
4238 /**
4239  * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4240  * @phba: pointer to lpfc hba data structure.
4241  *
4242  * This routine is called to explicitly arm the SLI4 device's completion and
4243  * event queues
4244  **/
4245 static void
4246 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4247 {
4248         uint8_t fcp_eqidx;
4249
4250         lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4251         lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4252         lpfc_sli4_cq_release(phba->sli4_hba.rxq_cq, LPFC_QUEUE_REARM);
4253         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4254                 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4255                                      LPFC_QUEUE_REARM);
4256         lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4257         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4258                 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4259                                      LPFC_QUEUE_REARM);
4260 }
4261
4262 /**
4263  * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4264  * @phba: Pointer to HBA context object.
4265  *
4266  * This function is the main SLI4 device intialization PCI function. This
4267  * function is called by the HBA intialization code, HBA reset code and
4268  * HBA error attention handler code. Caller is not required to hold any
4269  * locks.
4270  **/
4271 int
4272 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4273 {
4274         int rc;
4275         LPFC_MBOXQ_t *mboxq;
4276         struct lpfc_mqe *mqe;
4277         uint8_t *vpd;
4278         uint32_t vpd_size;
4279         uint32_t ftr_rsp = 0;
4280         struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4281         struct lpfc_vport *vport = phba->pport;
4282         struct lpfc_dmabuf *mp;
4283
4284         /* Perform a PCI function reset to start from clean */
4285         rc = lpfc_pci_function_reset(phba);
4286         if (unlikely(rc))
4287                 return -ENODEV;
4288
4289         /* Check the HBA Host Status Register for readyness */
4290         rc = lpfc_sli4_post_status_check(phba);
4291         if (unlikely(rc))
4292                 return -ENODEV;
4293         else {
4294                 spin_lock_irq(&phba->hbalock);
4295                 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4296                 spin_unlock_irq(&phba->hbalock);
4297         }
4298
4299         /*
4300          * Allocate a single mailbox container for initializing the
4301          * port.
4302          */
4303         mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4304         if (!mboxq)
4305                 return -ENOMEM;
4306
4307         /*
4308          * Continue initialization with default values even if driver failed
4309          * to read FCoE param config regions
4310          */
4311         if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4312                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4313                         "2570 Failed to read FCoE parameters \n");
4314
4315         /* Issue READ_REV to collect vpd and FW information. */
4316         vpd_size = PAGE_SIZE;
4317         vpd = kzalloc(vpd_size, GFP_KERNEL);
4318         if (!vpd) {
4319                 rc = -ENOMEM;
4320                 goto out_free_mbox;
4321         }
4322
4323         rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4324         if (unlikely(rc))
4325                 goto out_free_vpd;
4326
4327         mqe = &mboxq->u.mqe;
4328         if ((bf_get(lpfc_mbx_rd_rev_sli_lvl,
4329                     &mqe->un.read_rev) != LPFC_SLI_REV4) ||
4330             (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev) == 0)) {
4331                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4332                         "0376 READ_REV Error. SLI Level %d "
4333                         "FCoE enabled %d\n",
4334                         bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev),
4335                         bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev));
4336                 rc = -EIO;
4337                 goto out_free_vpd;
4338         }
4339         /* Single threaded at this point, no need for lock */
4340         spin_lock_irq(&phba->hbalock);
4341         phba->hba_flag |= HBA_FCOE_SUPPORT;
4342         spin_unlock_irq(&phba->hbalock);
4343         /*
4344          * Evaluate the read rev and vpd data. Populate the driver
4345          * state with the results. If this routine fails, the failure
4346          * is not fatal as the driver will use generic values.
4347          */
4348         rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4349         if (unlikely(!rc)) {
4350                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4351                                 "0377 Error %d parsing vpd. "
4352                                 "Using defaults.\n", rc);
4353                 rc = 0;
4354         }
4355
4356         /* By now, we should determine the SLI revision, hard code for now */
4357         phba->sli_rev = LPFC_SLI_REV4;
4358
4359         /*
4360          * Discover the port's supported feature set and match it against the
4361          * hosts requests.
4362          */
4363         lpfc_request_features(phba, mboxq);
4364         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4365         if (unlikely(rc)) {
4366                 rc = -EIO;
4367                 goto out_free_vpd;
4368         }
4369
4370         /*
4371          * The port must support FCP initiator mode as this is the
4372          * only mode running in the host.
4373          */
4374         if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4375                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4376                                 "0378 No support for fcpi mode.\n");
4377                 ftr_rsp++;
4378         }
4379
4380         /*
4381          * If the port cannot support the host's requested features
4382          * then turn off the global config parameters to disable the
4383          * feature in the driver.  This is not a fatal error.
4384          */
4385         if ((phba->cfg_enable_bg) &&
4386             !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4387                 ftr_rsp++;
4388
4389         if (phba->max_vpi && phba->cfg_enable_npiv &&
4390             !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4391                 ftr_rsp++;
4392
4393         if (ftr_rsp) {
4394                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4395                                 "0379 Feature Mismatch Data: x%08x %08x "
4396                                 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4397                                 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4398                                 phba->cfg_enable_npiv, phba->max_vpi);
4399                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4400                         phba->cfg_enable_bg = 0;
4401                 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4402                         phba->cfg_enable_npiv = 0;
4403         }
4404
4405         /* These SLI3 features are assumed in SLI4 */
4406         spin_lock_irq(&phba->hbalock);
4407         phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4408         spin_unlock_irq(&phba->hbalock);
4409
4410         /* Read the port's service parameters. */
4411         lpfc_read_sparam(phba, mboxq, vport->vpi);
4412         mboxq->vport = vport;
4413         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4414         mp = (struct lpfc_dmabuf *) mboxq->context1;
4415         if (rc == MBX_SUCCESS) {
4416                 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4417                 rc = 0;
4418         }
4419
4420         /*
4421          * This memory was allocated by the lpfc_read_sparam routine. Release
4422          * it to the mbuf pool.
4423          */
4424         lpfc_mbuf_free(phba, mp->virt, mp->phys);
4425         kfree(mp);
4426         mboxq->context1 = NULL;
4427         if (unlikely(rc)) {
4428                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4429                                 "0382 READ_SPARAM command failed "
4430                                 "status %d, mbxStatus x%x\n",
4431                                 rc, bf_get(lpfc_mqe_status, mqe));
4432                 phba->link_state = LPFC_HBA_ERROR;
4433                 rc = -EIO;
4434                 goto out_free_vpd;
4435         }
4436
4437         if (phba->cfg_soft_wwnn)
4438                 u64_to_wwn(phba->cfg_soft_wwnn,
4439                            vport->fc_sparam.nodeName.u.wwn);
4440         if (phba->cfg_soft_wwpn)
4441                 u64_to_wwn(phba->cfg_soft_wwpn,
4442                            vport->fc_sparam.portName.u.wwn);
4443         memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4444                sizeof(struct lpfc_name));
4445         memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4446                sizeof(struct lpfc_name));
4447
4448         /* Update the fc_host data structures with new wwn. */
4449         fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4450         fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4451
4452         /* Register SGL pool to the device using non-embedded mailbox command */
4453         rc = lpfc_sli4_post_sgl_list(phba);
4454         if (unlikely(rc)) {
4455                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4456                                 "0582 Error %d during sgl post operation", rc);
4457                 rc = -ENODEV;
4458                 goto out_free_vpd;
4459         }
4460
4461         /* Register SCSI SGL pool to the device */
4462         rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4463         if (unlikely(rc)) {
4464                 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4465                                 "0383 Error %d during scsi sgl post opeation",
4466                                 rc);
4467                 /* Some Scsi buffers were moved to the abort scsi list */
4468                 /* A pci function reset will repost them */
4469                 rc = -ENODEV;
4470                 goto out_free_vpd;
4471         }
4472
4473         /* Post the rpi header region to the device. */
4474         rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4475         if (unlikely(rc)) {
4476                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4477                                 "0393 Error %d during rpi post operation\n",
4478                                 rc);
4479                 rc = -ENODEV;
4480                 goto out_free_vpd;
4481         }
4482         /* Temporary initialization of lpfc_fip_flag to non-fip */
4483         bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
4484
4485         /* Set up all the queues to the device */
4486         rc = lpfc_sli4_queue_setup(phba);
4487         if (unlikely(rc)) {
4488                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4489                                 "0381 Error %d during queue setup.\n ", rc);
4490                 goto out_stop_timers;
4491         }
4492
4493         /* Arm the CQs and then EQs on device */
4494         lpfc_sli4_arm_cqeq_intr(phba);
4495
4496         /* Indicate device interrupt mode */
4497         phba->sli4_hba.intr_enable = 1;
4498
4499         /* Allow asynchronous mailbox command to go through */
4500         spin_lock_irq(&phba->hbalock);
4501         phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4502         spin_unlock_irq(&phba->hbalock);
4503
4504         /* Post receive buffers to the device */
4505         lpfc_sli4_rb_setup(phba);
4506
4507         /* Start the ELS watchdog timer */
4508         /*
4509          * The driver for SLI4 is not yet ready to process timeouts
4510          * or interrupts.  Once it is, the comment bars can be removed.
4511          */
4512         /* mod_timer(&vport->els_tmofunc,
4513          *           jiffies + HZ * (phba->fc_ratov*2)); */
4514
4515         /* Start heart beat timer */
4516         mod_timer(&phba->hb_tmofunc,
4517                   jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4518         phba->hb_outstanding = 0;
4519         phba->last_completion_time = jiffies;
4520
4521         /* Start error attention (ERATT) polling timer */
4522         mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4523
4524         /*
4525          * The port is ready, set the host's link state to LINK_DOWN
4526          * in preparation for link interrupts.
4527          */
4528         lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4529         mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4530         lpfc_set_loopback_flag(phba);
4531         /* Change driver state to LPFC_LINK_DOWN right before init link */
4532         spin_lock_irq(&phba->hbalock);
4533         phba->link_state = LPFC_LINK_DOWN;
4534         spin_unlock_irq(&phba->hbalock);
4535         rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4536         if (unlikely(rc != MBX_NOT_FINISHED)) {
4537                 kfree(vpd);
4538                 return 0;
4539         } else
4540                 rc = -EIO;
4541
4542         /* Unset all the queues set up in this routine when error out */
4543         if (rc)
4544                 lpfc_sli4_queue_unset(phba);
4545
4546 out_stop_timers:
4547         if (rc)
4548                 lpfc_stop_hba_timers(phba);
4549 out_free_vpd:
4550         kfree(vpd);
4551 out_free_mbox:
4552         mempool_free(mboxq, phba->mbox_mem_pool);
4553         return rc;
4554 }
4555
4556 /**
4557  * lpfc_mbox_timeout - Timeout call back function for mbox timer
4558  * @ptr: context object - pointer to hba structure.
4559  *
4560  * This is the callback function for mailbox timer. The mailbox
4561  * timer is armed when a new mailbox command is issued and the timer
4562  * is deleted when the mailbox complete. The function is called by
4563  * the kernel timer code when a mailbox does not complete within
4564  * expected time. This function wakes up the worker thread to
4565  * process the mailbox timeout and returns. All the processing is
4566  * done by the worker thread function lpfc_mbox_timeout_handler.
4567  **/
4568 void
4569 lpfc_mbox_timeout(unsigned long ptr)
4570 {
4571         struct lpfc_hba  *phba = (struct lpfc_hba *) ptr;
4572         unsigned long iflag;
4573         uint32_t tmo_posted;
4574
4575         spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4576         tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4577         if (!tmo_posted)
4578                 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4579         spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4580
4581         if (!tmo_posted)
4582                 lpfc_worker_wake_up(phba);
4583         return;
4584 }
4585
4586
4587 /**
4588  * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4589  * @phba: Pointer to HBA context object.
4590  *
4591  * This function is called from worker thread when a mailbox command times out.
4592  * The caller is not required to hold any locks. This function will reset the
4593  * HBA and recover all the pending commands.
4594  **/
4595 void
4596 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4597 {
4598         LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
4599         MAILBOX_t *mb = &pmbox->mb;
4600         struct lpfc_sli *psli = &phba->sli;
4601         struct lpfc_sli_ring *pring;
4602
4603         /* Check the pmbox pointer first.  There is a race condition
4604          * between the mbox timeout handler getting executed in the
4605          * worklist and the mailbox actually completing. When this
4606          * race condition occurs, the mbox_active will be NULL.
4607          */
4608         spin_lock_irq(&phba->hbalock);
4609         if (pmbox == NULL) {
4610                 lpfc_printf_log(phba, KERN_WARNING,
4611                                 LOG_MBOX | LOG_SLI,
4612                                 "0353 Active Mailbox cleared - mailbox timeout "
4613                                 "exiting\n");
4614                 spin_unlock_irq(&phba->hbalock);
4615                 return;
4616         }
4617
4618         /* Mbox cmd <mbxCommand> timeout */
4619         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4620                         "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4621                         mb->mbxCommand,
4622                         phba->pport->port_state,
4623                         phba->sli.sli_flag,
4624                         phba->sli.mbox_active);
4625         spin_unlock_irq(&phba->hbalock);
4626
4627         /* Setting state unknown so lpfc_sli_abort_iocb_ring
4628          * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4629          * it to fail all oustanding SCSI IO.
4630          */
4631         spin_lock_irq(&phba->pport->work_port_lock);
4632         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4633         spin_unlock_irq(&phba->pport->work_port_lock);
4634         spin_lock_irq(&phba->hbalock);
4635         phba->link_state = LPFC_LINK_UNKNOWN;
4636         psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
4637         spin_unlock_irq(&phba->hbalock);
4638
4639         pring = &psli->ring[psli->fcp_ring];
4640         lpfc_sli_abort_iocb_ring(phba, pring);
4641
4642         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4643                         "0345 Resetting board due to mailbox timeout\n");
4644
4645         /* Reset the HBA device */
4646         lpfc_reset_hba(phba);
4647 }
4648
4649 /**
4650  * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4651  * @phba: Pointer to HBA context object.
4652  * @pmbox: Pointer to mailbox object.
4653  * @flag: Flag indicating how the mailbox need to be processed.
4654  *
4655  * This function is called by discovery code and HBA management code
4656  * to submit a mailbox command to firmware with SLI-3 interface spec. This
4657  * function gets the hbalock to protect the data structures.
4658  * The mailbox command can be submitted in polling mode, in which case
4659  * this function will wait in a polling loop for the completion of the
4660  * mailbox.
4661  * If the mailbox is submitted in no_wait mode (not polling) the
4662  * function will submit the command and returns immediately without waiting
4663  * for the mailbox completion. The no_wait is supported only when HBA
4664  * is in SLI2/SLI3 mode - interrupts are enabled.
4665  * The SLI interface allows only one mailbox pending at a time. If the
4666  * mailbox is issued in polling mode and there is already a mailbox
4667  * pending, then the function will return an error. If the mailbox is issued
4668  * in NO_WAIT mode and there is a mailbox pending already, the function
4669  * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4670  * The sli layer owns the mailbox object until the completion of mailbox
4671  * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4672  * return codes the caller owns the mailbox command after the return of
4673  * the function.
4674  **/
4675 static int
4676 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4677                        uint32_t flag)
4678 {
4679         MAILBOX_t *mb;
4680         struct lpfc_sli *psli = &phba->sli;
4681         uint32_t status, evtctr;
4682         uint32_t ha_copy;
4683         int i;
4684         unsigned long timeout;
4685         unsigned long drvr_flag = 0;
4686         uint32_t word0, ldata;
4687         void __iomem *to_slim;
4688         int processing_queue = 0;
4689
4690         spin_lock_irqsave(&phba->hbalock, drvr_flag);
4691         if (!pmbox) {
4692                 /* processing mbox queue from intr_handler */
4693                 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4694                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4695                         return MBX_SUCCESS;
4696                 }
4697                 processing_queue = 1;
4698                 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4699                 pmbox = lpfc_mbox_get(phba);
4700                 if (!pmbox) {
4701                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4702                         return MBX_SUCCESS;
4703                 }
4704         }
4705
4706         if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
4707                 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
4708                 if(!pmbox->vport) {
4709                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4710                         lpfc_printf_log(phba, KERN_ERR,
4711                                         LOG_MBOX | LOG_VPORT,
4712                                         "1806 Mbox x%x failed. No vport\n",
4713                                         pmbox->u.mb.mbxCommand);
4714                         dump_stack();
4715                         goto out_not_finished;
4716                 }
4717         }
4718
4719         /* If the PCI channel is in offline state, do not post mbox. */
4720         if (unlikely(pci_channel_offline(phba->pcidev))) {
4721                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4722                 goto out_not_finished;
4723         }
4724
4725         /* If HBA has a deferred error attention, fail the iocb. */
4726         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4727                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4728                 goto out_not_finished;
4729         }
4730
4731         psli = &phba->sli;
4732
4733         mb = &pmbox->u.mb;
4734         status = MBX_SUCCESS;
4735
4736         if (phba->link_state == LPFC_HBA_ERROR) {
4737                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4738
4739                 /* Mbox command <mbxCommand> cannot issue */
4740                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4741                                 "(%d):0311 Mailbox command x%x cannot "
4742                                 "issue Data: x%x x%x\n",
4743                                 pmbox->vport ? pmbox->vport->vpi : 0,
4744                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4745                 goto out_not_finished;
4746         }
4747
4748         if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4749             !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
4750                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4751                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4752                                 "(%d):2528 Mailbox command x%x cannot "
4753                                 "issue Data: x%x x%x\n",
4754                                 pmbox->vport ? pmbox->vport->vpi : 0,
4755                                 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
4756                 goto out_not_finished;
4757         }
4758
4759         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4760                 /* Polling for a mbox command when another one is already active
4761                  * is not allowed in SLI. Also, the driver must have established
4762                  * SLI2 mode to queue and process multiple mbox commands.
4763                  */
4764
4765                 if (flag & MBX_POLL) {
4766                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4767
4768                         /* Mbox command <mbxCommand> cannot issue */
4769                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4770                                         "(%d):2529 Mailbox command x%x "
4771                                         "cannot issue Data: x%x x%x\n",
4772                                         pmbox->vport ? pmbox->vport->vpi : 0,
4773                                         pmbox->u.mb.mbxCommand,
4774                                         psli->sli_flag, flag);
4775                         goto out_not_finished;
4776                 }
4777
4778                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
4779                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4780                         /* Mbox command <mbxCommand> cannot issue */
4781                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4782                                         "(%d):2530 Mailbox command x%x "
4783                                         "cannot issue Data: x%x x%x\n",
4784                                         pmbox->vport ? pmbox->vport->vpi : 0,
4785                                         pmbox->u.mb.mbxCommand,
4786                                         psli->sli_flag, flag);
4787                         goto out_not_finished;
4788                 }
4789
4790                 /* Another mailbox command is still being processed, queue this
4791                  * command to be processed later.
4792                  */
4793                 lpfc_mbox_put(phba, pmbox);
4794
4795                 /* Mbox cmd issue - BUSY */
4796                 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4797                                 "(%d):0308 Mbox cmd issue - BUSY Data: "
4798                                 "x%x x%x x%x x%x\n",
4799                                 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4800                                 mb->mbxCommand, phba->pport->port_state,
4801                                 psli->sli_flag, flag);
4802
4803                 psli->slistat.mbox_busy++;
4804                 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4805
4806                 if (pmbox->vport) {
4807                         lpfc_debugfs_disc_trc(pmbox->vport,
4808                                 LPFC_DISC_TRC_MBOX_VPORT,
4809                                 "MBOX Bsy vport:  cmd:x%x mb:x%x x%x",
4810                                 (uint32_t)mb->mbxCommand,
4811                                 mb->un.varWords[0], mb->un.varWords[1]);
4812                 }
4813                 else {
4814                         lpfc_debugfs_disc_trc(phba->pport,
4815                                 LPFC_DISC_TRC_MBOX,
4816                                 "MBOX Bsy:        cmd:x%x mb:x%x x%x",
4817                                 (uint32_t)mb->mbxCommand,
4818                                 mb->un.varWords[0], mb->un.varWords[1]);
4819                 }
4820
4821                 return MBX_BUSY;
4822         }
4823
4824         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4825
4826         /* If we are not polling, we MUST be in SLI2 mode */
4827         if (flag != MBX_POLL) {
4828                 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
4829                     (mb->mbxCommand != MBX_KILL_BOARD)) {
4830                         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4831                         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4832                         /* Mbox command <mbxCommand> cannot issue */
4833                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4834                                         "(%d):2531 Mailbox command x%x "
4835                                         "cannot issue Data: x%x x%x\n",
4836                                         pmbox->vport ? pmbox->vport->vpi : 0,
4837                                         pmbox->u.mb.mbxCommand,
4838                                         psli->sli_flag, flag);
4839                         goto out_not_finished;
4840                 }
4841                 /* timeout active mbox command */
4842                 mod_timer(&psli->mbox_tmo, (jiffies +
4843                                (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
4844         }
4845
4846         /* Mailbox cmd <cmd> issue */
4847         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4848                         "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
4849                         "x%x\n",
4850                         pmbox->vport ? pmbox->vport->vpi : 0,
4851                         mb->mbxCommand, phba->pport->port_state,
4852                         psli->sli_flag, flag);
4853
4854         if (mb->mbxCommand != MBX_HEARTBEAT) {
4855                 if (pmbox->vport) {
4856                         lpfc_debugfs_disc_trc(pmbox->vport,
4857                                 LPFC_DISC_TRC_MBOX_VPORT,
4858                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4859                                 (uint32_t)mb->mbxCommand,
4860                                 mb->un.varWords[0], mb->un.varWords[1]);
4861                 }
4862                 else {
4863                         lpfc_debugfs_disc_trc(phba->pport,
4864                                 LPFC_DISC_TRC_MBOX,
4865                                 "MBOX Send:       cmd:x%x mb:x%x x%x",
4866                                 (uint32_t)mb->mbxCommand,
4867                                 mb->un.varWords[0], mb->un.varWords[1]);
4868                 }
4869         }
4870
4871         psli->slistat.mbox_cmd++;
4872         evtctr = psli->slistat.mbox_event;
4873
4874         /* next set own bit for the adapter and copy over command word */
4875         mb->mbxOwner = OWN_CHIP;
4876
4877         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4878                 /* First copy command data to host SLIM area */
4879                 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4880         } else {
4881                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4882                         /* copy command data into host mbox for cmpl */
4883                         lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
4884                 }
4885
4886                 /* First copy mbox command data to HBA SLIM, skip past first
4887                    word */
4888                 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4889                 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4890                             MAILBOX_CMD_SIZE - sizeof (uint32_t));
4891
4892                 /* Next copy over first word, with mbxOwner set */
4893                 ldata = *((uint32_t *)mb);
4894                 to_slim = phba->MBslimaddr;
4895                 writel(ldata, to_slim);
4896                 readl(to_slim); /* flush */
4897
4898                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4899                         /* switch over to host mailbox */
4900                         psli->sli_flag |= LPFC_SLI_ACTIVE;
4901                 }
4902         }
4903
4904         wmb();
4905
4906         switch (flag) {
4907         case MBX_NOWAIT:
4908                 /* Set up reference to mailbox command */
4909                 psli->mbox_active = pmbox;
4910                 /* Interrupt board to do it */
4911                 writel(CA_MBATT, phba->CAregaddr);
4912                 readl(phba->CAregaddr); /* flush */
4913                 /* Don't wait for it to finish, just return */
4914                 break;
4915
4916         case MBX_POLL:
4917                 /* Set up null reference to mailbox command */
4918                 psli->mbox_active = NULL;
4919                 /* Interrupt board to do it */
4920                 writel(CA_MBATT, phba->CAregaddr);
4921                 readl(phba->CAregaddr); /* flush */
4922
4923                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4924                         /* First read mbox status word */
4925                         word0 = *((uint32_t *)phba->mbox);
4926                         word0 = le32_to_cpu(word0);
4927                 } else {
4928                         /* First read mbox status word */
4929                         word0 = readl(phba->MBslimaddr);
4930                 }
4931
4932                 /* Read the HBA Host Attention Register */
4933                 ha_copy = readl(phba->HAregaddr);
4934                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4935                                                              mb->mbxCommand) *
4936                                            1000) + jiffies;
4937                 i = 0;
4938                 /* Wait for command to complete */
4939                 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4940                        (!(ha_copy & HA_MBATT) &&
4941                         (phba->link_state > LPFC_WARM_START))) {
4942                         if (time_after(jiffies, timeout)) {
4943                                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4944                                 spin_unlock_irqrestore(&phba->hbalock,
4945                                                        drvr_flag);
4946                                 goto out_not_finished;
4947                         }
4948
4949                         /* Check if we took a mbox interrupt while we were
4950                            polling */
4951                         if (((word0 & OWN_CHIP) != OWN_CHIP)
4952                             && (evtctr != psli->slistat.mbox_event))
4953                                 break;
4954
4955                         if (i++ > 10) {
4956                                 spin_unlock_irqrestore(&phba->hbalock,
4957                                                        drvr_flag);
4958                                 msleep(1);
4959                                 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4960                         }
4961
4962                         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4963                                 /* First copy command data */
4964                                 word0 = *((uint32_t *)phba->mbox);
4965                                 word0 = le32_to_cpu(word0);
4966                                 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4967                                         MAILBOX_t *slimmb;
4968                                         uint32_t slimword0;
4969                                         /* Check real SLIM for any errors */
4970                                         slimword0 = readl(phba->MBslimaddr);
4971                                         slimmb = (MAILBOX_t *) & slimword0;
4972                                         if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4973                                             && slimmb->mbxStatus) {
4974                                                 psli->sli_flag &=
4975                                                     ~LPFC_SLI_ACTIVE;
4976                                                 word0 = slimword0;
4977                                         }
4978                                 }
4979                         } else {
4980                                 /* First copy command data */
4981                                 word0 = readl(phba->MBslimaddr);
4982                         }
4983                         /* Read the HBA Host Attention Register */
4984                         ha_copy = readl(phba->HAregaddr);
4985                 }
4986
4987                 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
4988                         /* copy results back to user */
4989                         lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
4990                 } else {
4991                         /* First copy command data */
4992                         lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
4993                                                         MAILBOX_CMD_SIZE);
4994                         if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
4995                                 pmbox->context2) {
4996                                 lpfc_memcpy_from_slim((void *)pmbox->context2,
4997                                       phba->MBslimaddr + DMP_RSP_OFFSET,
4998                                                       mb->un.varDmp.word_cnt);
4999                         }
5000                 }
5001
5002                 writel(HA_MBATT, phba->HAregaddr);
5003                 readl(phba->HAregaddr); /* flush */
5004
5005                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5006                 status = mb->mbxStatus;
5007         }
5008
5009         spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5010         return status;
5011
5012 out_not_finished:
5013         if (processing_queue) {
5014                 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
5015                 lpfc_mbox_cmpl_put(phba, pmbox);
5016         }
5017         return MBX_NOT_FINISHED;
5018 }
5019
5020 /**
5021  * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5022  * @phba: Pointer to HBA context object.
5023  * @mboxq: Pointer to mailbox object.
5024  *
5025  * The function posts a mailbox to the port.  The mailbox is expected
5026  * to be comletely filled in and ready for the port to operate on it.
5027  * This routine executes a synchronous completion operation on the
5028  * mailbox by polling for its completion.
5029  *
5030  * The caller must not be holding any locks when calling this routine.
5031  *
5032  * Returns:
5033  *      MBX_SUCCESS - mailbox posted successfully
5034  *      Any of the MBX error values.
5035  **/
5036 static int
5037 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5038 {
5039         int rc = MBX_SUCCESS;
5040         unsigned long iflag;
5041         uint32_t db_ready;
5042         uint32_t mcqe_status;
5043         uint32_t mbx_cmnd;
5044         unsigned long timeout;
5045         struct lpfc_sli *psli = &phba->sli;
5046         struct lpfc_mqe *mb = &mboxq->u.mqe;
5047         struct lpfc_bmbx_create *mbox_rgn;
5048         struct dma_address *dma_address;
5049         struct lpfc_register bmbx_reg;
5050
5051         /*
5052          * Only one mailbox can be active to the bootstrap mailbox region
5053          * at a time and there is no queueing provided.
5054          */
5055         spin_lock_irqsave(&phba->hbalock, iflag);
5056         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5057                 spin_unlock_irqrestore(&phba->hbalock, iflag);
5058                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5059                                 "(%d):2532 Mailbox command x%x (x%x) "
5060                                 "cannot issue Data: x%x x%x\n",
5061                                 mboxq->vport ? mboxq->vport->vpi : 0,
5062                                 mboxq->u.mb.mbxCommand,
5063                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5064                                 psli->sli_flag, MBX_POLL);
5065                 return MBXERR_ERROR;
5066         }
5067         /* The server grabs the token and owns it until release */
5068         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5069         phba->sli.mbox_active = mboxq;
5070         spin_unlock_irqrestore(&phba->hbalock, iflag);
5071
5072         /*
5073          * Initialize the bootstrap memory region to avoid stale data areas
5074          * in the mailbox post.  Then copy the caller's mailbox contents to
5075          * the bmbx mailbox region.
5076          */
5077         mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5078         memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5079         lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5080                               sizeof(struct lpfc_mqe));
5081
5082         /* Post the high mailbox dma address to the port and wait for ready. */
5083         dma_address = &phba->sli4_hba.bmbx.dma_address;
5084         writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5085
5086         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5087                                    * 1000) + jiffies;
5088         do {
5089                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5090                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5091                 if (!db_ready)
5092                         msleep(2);
5093
5094                 if (time_after(jiffies, timeout)) {
5095                         rc = MBXERR_ERROR;
5096                         goto exit;
5097                 }
5098         } while (!db_ready);
5099
5100         /* Post the low mailbox dma address to the port. */
5101         writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5102         timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5103                                    * 1000) + jiffies;
5104         do {
5105                 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5106                 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5107                 if (!db_ready)
5108                         msleep(2);
5109
5110                 if (time_after(jiffies, timeout)) {
5111                         rc = MBXERR_ERROR;
5112                         goto exit;
5113                 }
5114         } while (!db_ready);
5115
5116         /*
5117          * Read the CQ to ensure the mailbox has completed.
5118          * If so, update the mailbox status so that the upper layers
5119          * can complete the request normally.
5120          */
5121         lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5122                               sizeof(struct lpfc_mqe));
5123         mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5124         lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5125                               sizeof(struct lpfc_mcqe));
5126         mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5127
5128         /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5129         if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5130                 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5131                 rc = MBXERR_ERROR;
5132         }
5133
5134         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5135                         "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5136                         "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5137                         " x%x x%x CQ: x%x x%x x%x x%x\n",
5138                         mboxq->vport ? mboxq->vport->vpi : 0,
5139                         mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5140                         bf_get(lpfc_mqe_status, mb),
5141                         mb->un.mb_words[0], mb->un.mb_words[1],
5142                         mb->un.mb_words[2], mb->un.mb_words[3],
5143                         mb->un.mb_words[4], mb->un.mb_words[5],
5144                         mb->un.mb_words[6], mb->un.mb_words[7],
5145                         mb->un.mb_words[8], mb->un.mb_words[9],
5146                         mb->un.mb_words[10], mb->un.mb_words[11],
5147                         mb->un.mb_words[12], mboxq->mcqe.word0,
5148                         mboxq->mcqe.mcqe_tag0,  mboxq->mcqe.mcqe_tag1,
5149                         mboxq->mcqe.trailer);
5150 exit:
5151         /* We are holding the token, no needed for lock when release */
5152         spin_lock_irqsave(&phba->hbalock, iflag);
5153         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5154         phba->sli.mbox_active = NULL;
5155         spin_unlock_irqrestore(&phba->hbalock, iflag);
5156         return rc;
5157 }
5158
5159 /**
5160  * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5161  * @phba: Pointer to HBA context object.
5162  * @pmbox: Pointer to mailbox object.
5163  * @flag: Flag indicating how the mailbox need to be processed.
5164  *
5165  * This function is called by discovery code and HBA management code to submit
5166  * a mailbox command to firmware with SLI-4 interface spec.
5167  *
5168  * Return codes the caller owns the mailbox command after the return of the
5169  * function.
5170  **/
5171 static int
5172 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5173                        uint32_t flag)
5174 {
5175         struct lpfc_sli *psli = &phba->sli;
5176         unsigned long iflags;
5177         int rc;
5178
5179         /* Detect polling mode and jump to a handler */
5180         if (!phba->sli4_hba.intr_enable) {
5181                 if (flag == MBX_POLL)
5182                         rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5183                 else
5184                         rc = -EIO;
5185                 if (rc != MBX_SUCCESS)
5186                         lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5187                                         "(%d):2541 Mailbox command x%x "
5188                                         "(x%x) cannot issue Data: x%x x%x\n",
5189                                         mboxq->vport ? mboxq->vport->vpi : 0,
5190                                         mboxq->u.mb.mbxCommand,
5191                                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5192                                         psli->sli_flag, flag);
5193                 return rc;
5194         } else if (flag == MBX_POLL) {
5195                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5196                                 "(%d):2542 Mailbox command x%x (x%x) "
5197                                 "cannot issue Data: x%x x%x\n",
5198                                 mboxq->vport ? mboxq->vport->vpi : 0,
5199                                 mboxq->u.mb.mbxCommand,
5200                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5201                                 psli->sli_flag, flag);
5202                 return -EIO;
5203         }
5204
5205         /* Now, interrupt mode asynchrous mailbox command */
5206         rc = lpfc_mbox_cmd_check(phba, mboxq);
5207         if (rc) {
5208                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5209                                 "(%d):2543 Mailbox command x%x (x%x) "
5210                                 "cannot issue Data: x%x x%x\n",
5211                                 mboxq->vport ? mboxq->vport->vpi : 0,
5212                                 mboxq->u.mb.mbxCommand,
5213                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5214                                 psli->sli_flag, flag);
5215                 goto out_not_finished;
5216         }
5217         rc = lpfc_mbox_dev_check(phba);
5218         if (unlikely(rc)) {
5219                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5220                                 "(%d):2544 Mailbox command x%x (x%x) "
5221                                 "cannot issue Data: x%x x%x\n",
5222                                 mboxq->vport ? mboxq->vport->vpi : 0,
5223                                 mboxq->u.mb.mbxCommand,
5224                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5225                                 psli->sli_flag, flag);
5226                 goto out_not_finished;
5227         }
5228
5229         /* Put the mailbox command to the driver internal FIFO */
5230         psli->slistat.mbox_busy++;
5231         spin_lock_irqsave(&phba->hbalock, iflags);
5232         lpfc_mbox_put(phba, mboxq);
5233         spin_unlock_irqrestore(&phba->hbalock, iflags);
5234         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5235                         "(%d):0354 Mbox cmd issue - Enqueue Data: "
5236                         "x%x (x%x) x%x x%x x%x\n",
5237                         mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5238                         bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5239                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5240                         phba->pport->port_state,
5241                         psli->sli_flag, MBX_NOWAIT);
5242         /* Wake up worker thread to transport mailbox command from head */
5243         lpfc_worker_wake_up(phba);
5244
5245         return MBX_BUSY;
5246
5247 out_not_finished:
5248         return MBX_NOT_FINISHED;
5249 }
5250
5251 /**
5252  * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5253  * @phba: Pointer to HBA context object.
5254  *
5255  * This function is called by worker thread to send a mailbox command to
5256  * SLI4 HBA firmware.
5257  *
5258  **/
5259 int
5260 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5261 {
5262         struct lpfc_sli *psli = &phba->sli;
5263         LPFC_MBOXQ_t *mboxq;
5264         int rc = MBX_SUCCESS;
5265         unsigned long iflags;
5266         struct lpfc_mqe *mqe;
5267         uint32_t mbx_cmnd;
5268
5269         /* Check interrupt mode before post async mailbox command */
5270         if (unlikely(!phba->sli4_hba.intr_enable))
5271                 return MBX_NOT_FINISHED;
5272
5273         /* Check for mailbox command service token */
5274         spin_lock_irqsave(&phba->hbalock, iflags);
5275         if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5276                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5277                 return MBX_NOT_FINISHED;
5278         }
5279         if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5280                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5281                 return MBX_NOT_FINISHED;
5282         }
5283         if (unlikely(phba->sli.mbox_active)) {
5284                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5285                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5286                                 "0384 There is pending active mailbox cmd\n");
5287                 return MBX_NOT_FINISHED;
5288         }
5289         /* Take the mailbox command service token */
5290         psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5291
5292         /* Get the next mailbox command from head of queue */
5293         mboxq = lpfc_mbox_get(phba);
5294
5295         /* If no more mailbox command waiting for post, we're done */
5296         if (!mboxq) {
5297                 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5298                 spin_unlock_irqrestore(&phba->hbalock, iflags);
5299                 return MBX_SUCCESS;
5300         }
5301         phba->sli.mbox_active = mboxq;
5302         spin_unlock_irqrestore(&phba->hbalock, iflags);
5303
5304         /* Check device readiness for posting mailbox command */
5305         rc = lpfc_mbox_dev_check(phba);
5306         if (unlikely(rc))
5307                 /* Driver clean routine will clean up pending mailbox */
5308                 goto out_not_finished;
5309
5310         /* Prepare the mbox command to be posted */
5311         mqe = &mboxq->u.mqe;
5312         mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5313
5314         /* Start timer for the mbox_tmo and log some mailbox post messages */
5315         mod_timer(&psli->mbox_tmo, (jiffies +
5316                   (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5317
5318         lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5319                         "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5320                         "x%x x%x\n",
5321                         mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5322                         lpfc_sli4_mbox_opcode_get(phba, mboxq),
5323                         phba->pport->port_state, psli->sli_flag);
5324
5325         if (mbx_cmnd != MBX_HEARTBEAT) {
5326                 if (mboxq->vport) {
5327                         lpfc_debugfs_disc_trc(mboxq->vport,
5328                                 LPFC_DISC_TRC_MBOX_VPORT,
5329                                 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5330                                 mbx_cmnd, mqe->un.mb_words[0],
5331                                 mqe->un.mb_words[1]);
5332                 } else {
5333                         lpfc_debugfs_disc_trc(phba->pport,
5334                                 LPFC_DISC_TRC_MBOX,
5335                                 "MBOX Send: cmd:x%x mb:x%x x%x",
5336                                 mbx_cmnd, mqe->un.mb_words[0],
5337                                 mqe->un.mb_words[1]);
5338                 }
5339         }
5340         psli->slistat.mbox_cmd++;
5341
5342         /* Post the mailbox command to the port */
5343         rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5344         if (rc != MBX_SUCCESS) {
5345                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5346                                 "(%d):2533 Mailbox command x%x (x%x) "
5347                                 "cannot issue Data: x%x x%x\n",
5348                                 mboxq->vport ? mboxq->vport->vpi : 0,
5349                                 mboxq->u.mb.mbxCommand,
5350                                 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5351                                 psli->sli_flag, MBX_NOWAIT);
5352                 goto out_not_finished;
5353         }
5354
5355         return rc;
5356
5357 out_not_finished:
5358         spin_lock_irqsave(&phba->hbalock, iflags);
5359         mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5360         __lpfc_mbox_cmpl_put(phba, mboxq);
5361         /* Release the token */
5362         psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5363         phba->sli.mbox_active = NULL;
5364         spin_unlock_irqrestore(&phba->hbalock, iflags);
5365
5366         return MBX_NOT_FINISHED;
5367 }
5368
5369 /**
5370  * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5371  * @phba: Pointer to HBA context object.
5372  * @pmbox: Pointer to mailbox object.
5373  * @flag: Flag indicating how the mailbox need to be processed.
5374  *
5375  * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5376  * the API jump table function pointer from the lpfc_hba struct.
5377  *
5378  * Return codes the caller owns the mailbox command after the return of the
5379  * function.
5380  **/
5381 int
5382 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5383 {
5384         return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5385 }
5386
5387 /**
5388  * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5389  * @phba: The hba struct for which this call is being executed.
5390  * @dev_grp: The HBA PCI-Device group number.
5391  *
5392  * This routine sets up the mbox interface API function jump table in @phba
5393  * struct.
5394  * Returns: 0 - success, -ENODEV - failure.
5395  **/
5396 int
5397 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5398 {
5399
5400         switch (dev_grp) {
5401         case LPFC_PCI_DEV_LP:
5402                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5403                 phba->lpfc_sli_handle_slow_ring_event =
5404                                 lpfc_sli_handle_slow_ring_event_s3;
5405                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5406                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5407                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5408                 break;
5409         case LPFC_PCI_DEV_OC:
5410                 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5411                 phba->lpfc_sli_handle_slow_ring_event =
5412                                 lpfc_sli_handle_slow_ring_event_s4;
5413                 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5414                 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5415                 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5416                 break;
5417         default:
5418                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5419                                 "1420 Invalid HBA PCI-device group: 0x%x\n",
5420                                 dev_grp);
5421                 return -ENODEV;
5422                 break;
5423         }
5424         return 0;
5425 }
5426
5427 /**
5428  * __lpfc_sli_ringtx_put - Add an iocb to the txq
5429  * @phba: Pointer to HBA context object.
5430  * @pring: Pointer to driver SLI ring object.
5431  * @piocb: Pointer to address of newly added command iocb.
5432  *
5433  * This function is called with hbalock held to add a command
5434  * iocb to the txq when SLI layer cannot submit the command iocb
5435  * to the ring.
5436  **/
5437 static void
5438 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5439                     struct lpfc_iocbq *piocb)
5440 {
5441         /* Insert the caller's iocb in the txq tail for later processing. */
5442         list_add_tail(&piocb->list, &pring->txq);
5443         pring->txq_cnt++;
5444 }
5445
5446 /**
5447  * lpfc_sli_next_iocb - Get the next iocb in the txq
5448  * @phba: Pointer to HBA context object.
5449  * @pring: Pointer to driver SLI ring object.
5450  * @piocb: Pointer to address of newly added command iocb.
5451  *
5452  * This function is called with hbalock held before a new
5453  * iocb is submitted to the firmware. This function checks
5454  * txq to flush the iocbs in txq to Firmware before
5455  * submitting new iocbs to the Firmware.
5456  * If there are iocbs in the txq which need to be submitted
5457  * to firmware, lpfc_sli_next_iocb returns the first element
5458  * of the txq after dequeuing it from txq.
5459  * If there is no iocb in the txq then the function will return
5460  * *piocb and *piocb is set to NULL. Caller needs to check
5461  * *piocb to find if there are more commands in the txq.
5462  **/
5463 static struct lpfc_iocbq *
5464 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
5465                    struct lpfc_iocbq **piocb)
5466 {
5467         struct lpfc_iocbq * nextiocb;
5468
5469         nextiocb = lpfc_sli_ringtx_get(phba, pring);
5470         if (!nextiocb) {
5471                 nextiocb = *piocb;
5472                 *piocb = NULL;
5473         }
5474
5475         return nextiocb;
5476 }
5477
5478 /**
5479  * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
5480  * @phba: Pointer to HBA context object.
5481  * @ring_number: SLI ring number to issue iocb on.
5482  * @piocb: Pointer to command iocb.
5483  * @flag: Flag indicating if this command can be put into txq.
5484  *
5485  * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5486  * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5487  * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5488  * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5489  * this function allows only iocbs for posting buffers. This function finds
5490  * next available slot in the command ring and posts the command to the
5491  * available slot and writes the port attention register to request HBA start
5492  * processing new iocb. If there is no slot available in the ring and
5493  * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5494  * the function returns IOCB_BUSY.
5495  *
5496  * This function is called with hbalock held. The function will return success
5497  * after it successfully submit the iocb to firmware or after adding to the
5498  * txq.
5499  **/
5500 static int
5501 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
5502                     struct lpfc_iocbq *piocb, uint32_t flag)
5503 {
5504         struct lpfc_iocbq *nextiocb;
5505         IOCB_t *iocb;
5506         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
5507
5508         if (piocb->iocb_cmpl && (!piocb->vport) &&
5509            (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5510            (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5511                 lpfc_printf_log(phba, KERN_ERR,
5512                                 LOG_SLI | LOG_VPORT,
5513                                 "1807 IOCB x%x failed. No vport\n",
5514                                 piocb->iocb.ulpCommand);
5515                 dump_stack();
5516                 return IOCB_ERROR;
5517         }
5518
5519
5520         /* If the PCI channel is in offline state, do not post iocbs. */
5521         if (unlikely(pci_channel_offline(phba->pcidev)))
5522                 return IOCB_ERROR;
5523
5524         /* If HBA has a deferred error attention, fail the iocb. */
5525         if (unlikely(phba->hba_flag & DEFER_ERATT))
5526                 return IOCB_ERROR;
5527
5528         /*
5529          * We should never get an IOCB if we are in a < LINK_DOWN state
5530          */
5531         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
5532                 return IOCB_ERROR;
5533
5534         /*
5535          * Check to see if we are blocking IOCB processing because of a
5536          * outstanding event.
5537          */
5538         if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
5539                 goto iocb_busy;
5540
5541         if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
5542                 /*
5543                  * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
5544                  * can be issued if the link is not up.
5545                  */
5546                 switch (piocb->iocb.ulpCommand) {
5547                 case CMD_GEN_REQUEST64_CR:
5548                 case CMD_GEN_REQUEST64_CX:
5549                         if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5550                                 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5551                                         FC_FCP_CMND) ||
5552                                 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5553                                         MENLO_TRANSPORT_TYPE))
5554
5555                                 goto iocb_busy;
5556                         break;
5557                 case CMD_QUE_RING_BUF_CN:
5558                 case CMD_QUE_RING_BUF64_CN:
5559                         /*
5560                          * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5561                          * completion, iocb_cmpl MUST be 0.
5562                          */
5563                         if (piocb->iocb_cmpl)
5564                                 piocb->iocb_cmpl = NULL;
5565                         /*FALLTHROUGH*/
5566                 case CMD_CREATE_XRI_CR:
5567                 case CMD_CLOSE_XRI_CN:
5568                 case CMD_CLOSE_XRI_CX:
5569                         break;
5570                 default:
5571                         goto iocb_busy;
5572                 }
5573
5574         /*
5575          * For FCP commands, we must be in a state where we can process link
5576          * attention events.
5577          */
5578         } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
5579                             !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
5580                 goto iocb_busy;
5581         }
5582
5583         while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5584                (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5585                 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5586
5587         if (iocb)
5588                 lpfc_sli_update_ring(phba, pring);
5589         else
5590                 lpfc_sli_update_full_ring(phba, pring);
5591
5592         if (!piocb)
5593                 return IOCB_SUCCESS;
5594
5595         goto out_busy;
5596
5597  iocb_busy:
5598         pring->stats.iocb_cmd_delay++;
5599
5600  out_busy:
5601
5602         if (!(flag & SLI_IOCB_RET_IOCB)) {
5603                 __lpfc_sli_ringtx_put(phba, pring, piocb);
5604                 return IOCB_SUCCESS;
5605         }
5606
5607         return IOCB_BUSY;
5608 }
5609
5610 /**
5611  * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5612  * @phba: Pointer to HBA context object.
5613  * @piocb: Pointer to command iocb.
5614  * @sglq: Pointer to the scatter gather queue object.
5615  *
5616  * This routine converts the bpl or bde that is in the IOCB
5617  * to a sgl list for the sli4 hardware. The physical address
5618  * of the bpl/bde is converted back to a virtual address.
5619  * If the IOCB contains a BPL then the list of BDE's is
5620  * converted to sli4_sge's. If the IOCB contains a single
5621  * BDE then it is converted to a single sli_sge.
5622  * The IOCB is still in cpu endianess so the contents of
5623  * the bpl can be used without byte swapping.
5624  *
5625  * Returns valid XRI = Success, NO_XRI = Failure.
5626 **/
5627 static uint16_t
5628 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5629                 struct lpfc_sglq *sglq)
5630 {
5631         uint16_t xritag = NO_XRI;
5632         struct ulp_bde64 *bpl = NULL;
5633         struct ulp_bde64 bde;
5634         struct sli4_sge *sgl  = NULL;
5635         IOCB_t *icmd;
5636         int numBdes = 0;
5637         int i = 0;
5638
5639         if (!piocbq || !sglq)
5640                 return xritag;
5641
5642         sgl  = (struct sli4_sge *)sglq->sgl;
5643         icmd = &piocbq->iocb;
5644         if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5645                 numBdes = icmd->un.genreq64.bdl.bdeSize /
5646                                 sizeof(struct ulp_bde64);
5647                 /* The addrHigh and addrLow fields within the IOCB
5648                  * have not been byteswapped yet so there is no
5649                  * need to swap them back.
5650                  */
5651                 bpl  = (struct ulp_bde64 *)
5652                         ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5653
5654                 if (!bpl)
5655                         return xritag;
5656
5657                 for (i = 0; i < numBdes; i++) {
5658                         /* Should already be byte swapped. */
5659                         sgl->addr_hi =  bpl->addrHigh;
5660                         sgl->addr_lo =  bpl->addrLow;
5661                         /* swap the size field back to the cpu so we
5662                          * can assign it to the sgl.
5663                          */
5664                         bde.tus.w  = le32_to_cpu(bpl->tus.w);
5665                         bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5666                         if ((i+1) == numBdes)
5667                                 bf_set(lpfc_sli4_sge_last, sgl, 1);
5668                         else
5669                                 bf_set(lpfc_sli4_sge_last, sgl, 0);
5670                         sgl->word2 = cpu_to_le32(sgl->word2);
5671                         sgl->word3 = cpu_to_le32(sgl->word3);
5672                         bpl++;
5673                         sgl++;
5674                 }
5675         } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5676                         /* The addrHigh and addrLow fields of the BDE have not
5677                          * been byteswapped yet so they need to be swapped
5678                          * before putting them in the sgl.
5679                          */
5680                         sgl->addr_hi =
5681                                 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5682                         sgl->addr_lo =
5683                                 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5684                         bf_set(lpfc_sli4_sge_len, sgl,
5685                                 icmd->un.genreq64.bdl.bdeSize);
5686                         bf_set(lpfc_sli4_sge_last, sgl, 1);
5687                         sgl->word2 = cpu_to_le32(sgl->word2);
5688                         sgl->word3 = cpu_to_le32(sgl->word3);
5689         }
5690         return sglq->sli4_xritag;
5691 }
5692
5693 /**
5694  * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5695  * @phba: Pointer to HBA context object.
5696  * @piocb: Pointer to command iocb.
5697  *
5698  * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5699  * distribution.
5700  *
5701  * Return: index into SLI4 fast-path FCP queue index.
5702  **/
5703 static uint32_t
5704 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
5705 {
5706         static uint32_t fcp_qidx;
5707
5708         return fcp_qidx++ % phba->cfg_fcp_wq_count;
5709 }
5710
5711 /**
5712  * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5713  * @phba: Pointer to HBA context object.
5714  * @piocb: Pointer to command iocb.
5715  * @wqe: Pointer to the work queue entry.
5716  *
5717  * This routine converts the iocb command to its Work Queue Entry
5718  * equivalent. The wqe pointer should not have any fields set when
5719  * this routine is called because it will memcpy over them.
5720  * This routine does not set the CQ_ID or the WQEC bits in the
5721  * wqe.
5722  *
5723  * Returns: 0 = Success, IOCB_ERROR = Failure.
5724  **/
5725 static int
5726 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5727                 union lpfc_wqe *wqe)
5728 {
5729         uint32_t payload_len = 0;
5730         uint8_t ct = 0;
5731         uint32_t fip;
5732         uint32_t abort_tag;
5733         uint8_t command_type = ELS_COMMAND_NON_FIP;
5734         uint8_t cmnd;
5735         uint16_t xritag;
5736         struct ulp_bde64 *bpl = NULL;
5737
5738         fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
5739         /* The fcp commands will set command type */
5740         if ((!(iocbq->iocb_flag &  LPFC_IO_FCP)) && (!fip))
5741                 command_type = ELS_COMMAND_NON_FIP;
5742         else if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
5743                 command_type = ELS_COMMAND_FIP;
5744         else if (iocbq->iocb_flag &  LPFC_IO_FCP)
5745                 command_type = FCP_COMMAND;
5746         else {
5747                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5748                         "2019 Invalid cmd 0x%x\n",
5749                         iocbq->iocb.ulpCommand);
5750                 return IOCB_ERROR;
5751         }
5752         /* Some of the fields are in the right position already */
5753         memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5754         abort_tag = (uint32_t) iocbq->iotag;
5755         xritag = iocbq->sli4_xritag;
5756         wqe->words[7] = 0; /* The ct field has moved so reset */
5757         /* words0-2 bpl convert bde */
5758         if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5759                 bpl  = (struct ulp_bde64 *)
5760                         ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5761                 if (!bpl)
5762                         return IOCB_ERROR;
5763
5764                 /* Should already be byte swapped. */
5765                 wqe->generic.bde.addrHigh =  le32_to_cpu(bpl->addrHigh);
5766                 wqe->generic.bde.addrLow =  le32_to_cpu(bpl->addrLow);
5767                 /* swap the size field back to the cpu so we
5768                  * can assign it to the sgl.
5769                  */
5770                 wqe->generic.bde.tus.w  = le32_to_cpu(bpl->tus.w);
5771                 payload_len = wqe->generic.bde.tus.f.bdeSize;
5772         } else
5773                 payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5774
5775         iocbq->iocb.ulpIoTag = iocbq->iotag;
5776         cmnd = iocbq->iocb.ulpCommand;
5777
5778         switch (iocbq->iocb.ulpCommand) {
5779         case CMD_ELS_REQUEST64_CR:
5780                 if (!iocbq->iocb.ulpLe) {
5781                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5782                                 "2007 Only Limited Edition cmd Format"
5783                                 " supported 0x%x\n",
5784                                 iocbq->iocb.ulpCommand);
5785                         return IOCB_ERROR;
5786                 }
5787                 wqe->els_req.payload_len = payload_len;
5788                 /* Els_reguest64 has a TMO */
5789                 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5790                         iocbq->iocb.ulpTimeout);
5791                 /* Need a VF for word 4 set the vf bit*/
5792                 bf_set(els_req64_vf, &wqe->els_req, 0);
5793                 /* And a VFID for word 12 */
5794                 bf_set(els_req64_vfid, &wqe->els_req, 0);
5795                 /*
5796                  * Set ct field to 3, indicates that the context_tag field
5797                  * contains the FCFI and remote N_Port_ID is
5798                  * in word 5.
5799                  */
5800
5801                 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5802                 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5803                                 iocbq->iocb.ulpContext);
5804
5805                 if (iocbq->vport->fc_myDID != 0) {
5806                         bf_set(els_req64_sid, &wqe->els_req,
5807                                  iocbq->vport->fc_myDID);
5808                         bf_set(els_req64_sp, &wqe->els_req, 1);
5809                 }
5810                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5811                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5812                 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5813         break;
5814         case CMD_XMIT_SEQUENCE64_CR:
5815                 /* word3 iocb=io_tag32 wqe=payload_offset */
5816                 /* payload offset used for multilpe outstanding
5817                  * sequences on the same exchange
5818                  */
5819                 wqe->words[3] = 0;
5820                 /* word4 relative_offset memcpy */
5821                 /* word5 r_ctl/df_ctl memcpy */
5822                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5823                 wqe->xmit_sequence.xmit_len = payload_len;
5824         break;
5825         case CMD_XMIT_BCAST64_CN:
5826                 /* word3 iocb=iotag32 wqe=payload_len */
5827                 wqe->words[3] = 0; /* no definition for this in wqe */
5828                 /* word4 iocb=rsvd wqe=rsvd */
5829                 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5830                 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5831                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5832                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5833         break;
5834         case CMD_FCP_IWRITE64_CR:
5835                 command_type = FCP_COMMAND_DATA_OUT;
5836                 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5837                  * confusing.
5838                  * word3 is payload_len: byte offset to the sgl entry for the
5839                  * fcp_command.
5840                  * word4 is total xfer len, same as the IOCB->ulpParameter.
5841                  * word5 is initial xfer len 0 = wait for xfer-ready
5842                  */
5843
5844                 /* Always wait for xfer-ready before sending data */
5845                 wqe->fcp_iwrite.initial_xfer_len = 0;
5846                 /* word 4 (xfer length) should have been set on the memcpy */
5847
5848         /* allow write to fall through to read */
5849         case CMD_FCP_IREAD64_CR:
5850                 /* FCP_CMD is always the 1st sgl entry */
5851                 wqe->fcp_iread.payload_len =
5852                         payload_len + sizeof(struct fcp_rsp);
5853
5854                 /* word 4 (xfer length) should have been set on the memcpy */
5855
5856                 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5857                         iocbq->iocb.ulpFCP2Rcvy);
5858                 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5859                 /* The XC bit and the XS bit are similar. The driver never
5860                  * tracked whether or not the exchange was previouslly open.
5861                  * XC = Exchange create, 0 is create. 1 is already open.
5862                  * XS = link cmd: 1 do not close the exchange after command.
5863                  * XS = 0 close exchange when command completes.
5864                  * The only time we would not set the XC bit is when the XS bit
5865                  * is set and we are sending our 2nd or greater command on
5866                  * this exchange.
5867                  */
5868
5869         /* ALLOW read & write to fall through to ICMD64 */
5870         case CMD_FCP_ICMND64_CR:
5871                 /* Always open the exchange */
5872                 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5873
5874                 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5875                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5876         break;
5877         case CMD_GEN_REQUEST64_CR:
5878                 /* word3 command length is described as byte offset to the
5879                  * rsp_data. Would always be 16, sizeof(struct sli4_sge)
5880                  * sgl[0] = cmnd
5881                  * sgl[1] = rsp.
5882                  *
5883                  */
5884                 wqe->gen_req.command_len = payload_len;
5885                 /* Word4 parameter  copied in the memcpy */
5886                 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
5887                 /* word6 context tag copied in memcpy */
5888                 if (iocbq->iocb.ulpCt_h  || iocbq->iocb.ulpCt_l) {
5889                         ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5890                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5891                                 "2015 Invalid CT %x command 0x%x\n",
5892                                 ct, iocbq->iocb.ulpCommand);
5893                         return IOCB_ERROR;
5894                 }
5895                 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
5896                 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
5897                         iocbq->iocb.ulpTimeout);
5898
5899                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5900                 command_type = OTHER_COMMAND;
5901         break;
5902         case CMD_XMIT_ELS_RSP64_CX:
5903                 /* words0-2 BDE memcpy */
5904                 /* word3 iocb=iotag32 wqe=rsvd */
5905                 wqe->words[3] = 0;
5906                 /* word4 iocb=did wge=rsvd. */
5907                 wqe->words[4] = 0;
5908                 /* word5 iocb=rsvd wge=did */
5909                 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
5910                          iocbq->iocb.un.elsreq64.remoteID);
5911
5912                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5913                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5914
5915                 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
5916                 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5917                 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
5918                         bf_set(lpfc_wqe_gen_context, &wqe->generic,
5919                                iocbq->vport->vpi + phba->vpi_base);
5920                 command_type = OTHER_COMMAND;
5921         break;
5922         case CMD_CLOSE_XRI_CN:
5923         case CMD_ABORT_XRI_CN:
5924         case CMD_ABORT_XRI_CX:
5925                 /* words 0-2 memcpy should be 0 rserved */
5926                 /* port will send abts */
5927                 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
5928                         /*
5929                          * The link is down so the fw does not need to send abts
5930                          * on the wire.
5931                          */
5932                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
5933                 else
5934                         bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
5935                 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
5936                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
5937                 wqe->words[5] = 0;
5938                 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5939                         ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5940                 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
5941                 wqe->generic.abort_tag = abort_tag;
5942                 /*
5943                  * The abort handler will send us CMD_ABORT_XRI_CN or
5944                  * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
5945                  */
5946                 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
5947                 cmnd = CMD_ABORT_XRI_CX;
5948                 command_type = OTHER_COMMAND;
5949                 xritag = 0;
5950         break;
5951         case CMD_XRI_ABORTED_CX:
5952         case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
5953                 /* words0-2 are all 0's no bde */
5954                 /* word3 and word4 are rsvrd */
5955                 wqe->words[3] = 0;
5956                 wqe->words[4] = 0;
5957                 /* word5 iocb=rsvd wge=did */
5958                 /* There is no remote port id in the IOCB? */
5959                 /* Let this fall through and fail */
5960         case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
5961         case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
5962         case CMD_FCP_TRSP64_CX: /* Target mode rcv */
5963         case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
5964         default:
5965                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5966                                 "2014 Invalid command 0x%x\n",
5967                                 iocbq->iocb.ulpCommand);
5968                 return IOCB_ERROR;
5969         break;
5970
5971         }
5972         bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
5973         bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
5974         wqe->generic.abort_tag = abort_tag;
5975         bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
5976         bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
5977         bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
5978         bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
5979
5980         return 0;
5981 }
5982
5983 /**
5984  * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
5985  * @phba: Pointer to HBA context object.
5986  * @ring_number: SLI ring number to issue iocb on.
5987  * @piocb: Pointer to command iocb.
5988  * @flag: Flag indicating if this command can be put into txq.
5989  *
5990  * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
5991  * an iocb command to an HBA with SLI-4 interface spec.
5992  *
5993  * This function is called with hbalock held. The function will return success
5994  * after it successfully submit the iocb to firmware or after adding to the
5995  * txq.
5996  **/
5997 static int
5998 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
5999                          struct lpfc_iocbq *piocb, uint32_t flag)
6000 {
6001         struct lpfc_sglq *sglq;
6002         uint16_t xritag;
6003         union lpfc_wqe wqe;
6004         struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6005         uint32_t fcp_wqidx;
6006
6007         if (piocb->sli4_xritag == NO_XRI) {
6008                 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6009                         piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6010                         sglq = NULL;
6011                 else {
6012                         sglq = __lpfc_sli_get_sglq(phba);
6013                         if (!sglq)
6014                                 return IOCB_ERROR;
6015                         piocb->sli4_xritag = sglq->sli4_xritag;
6016                 }
6017         } else if (piocb->iocb_flag &  LPFC_IO_FCP) {
6018                 sglq = NULL; /* These IO's already have an XRI and
6019                               * a mapped sgl.
6020                               */
6021         } else {
6022                 /* This is a continuation of a commandi,(CX) so this
6023                  * sglq is on the active list
6024                  */
6025                 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6026                 if (!sglq)
6027                         return IOCB_ERROR;
6028         }
6029
6030         if (sglq) {
6031                 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6032                 if (xritag != sglq->sli4_xritag)
6033                         return IOCB_ERROR;
6034         }
6035
6036         if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6037                 return IOCB_ERROR;
6038
6039         if (piocb->iocb_flag &  LPFC_IO_FCP) {
6040                 fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba, piocb);
6041                 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
6042                         return IOCB_ERROR;
6043         } else {
6044                 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6045                         return IOCB_ERROR;
6046         }
6047         lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6048
6049         return 0;
6050 }
6051
6052 /**
6053  * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6054  *
6055  * This routine wraps the actual lockless version for issusing IOCB function
6056  * pointer from the lpfc_hba struct.
6057  *
6058  * Return codes:
6059  *      IOCB_ERROR - Error
6060  *      IOCB_SUCCESS - Success
6061  *      IOCB_BUSY - Busy
6062  **/
6063 static inline int
6064 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6065                 struct lpfc_iocbq *piocb, uint32_t flag)
6066 {
6067         return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6068 }
6069
6070 /**
6071  * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6072  * @phba: The hba struct for which this call is being executed.
6073  * @dev_grp: The HBA PCI-Device group number.
6074  *
6075  * This routine sets up the SLI interface API function jump table in @phba
6076  * struct.
6077  * Returns: 0 - success, -ENODEV - failure.
6078  **/
6079 int
6080 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6081 {
6082
6083         switch (dev_grp) {
6084         case LPFC_PCI_DEV_LP:
6085                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6086                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6087                 break;
6088         case LPFC_PCI_DEV_OC:
6089                 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6090                 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6091                 break;
6092         default:
6093                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6094                                 "1419 Invalid HBA PCI-device group: 0x%x\n",
6095                                 dev_grp);
6096                 return -ENODEV;
6097                 break;
6098         }
6099         phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6100         return 0;
6101 }
6102
6103 /**
6104  * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6105  * @phba: Pointer to HBA context object.
6106  * @pring: Pointer to driver SLI ring object.
6107  * @piocb: Pointer to command iocb.
6108  * @flag: Flag indicating if this command can be put into txq.
6109  *
6110  * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6111  * function. This function gets the hbalock and calls
6112  * __lpfc_sli_issue_iocb function and will return the error returned
6113  * by __lpfc_sli_issue_iocb function. This wrapper is used by
6114  * functions which do not hold hbalock.
6115  **/
6116 int
6117 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6118                     struct lpfc_iocbq *piocb, uint32_t flag)
6119 {
6120         unsigned long iflags;
6121         int rc;
6122
6123         spin_lock_irqsave(&phba->hbalock, iflags);
6124         rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6125         spin_unlock_irqrestore(&phba->hbalock, iflags);
6126
6127         return rc;
6128 }
6129
6130 /**
6131  * lpfc_extra_ring_setup - Extra ring setup function
6132  * @phba: Pointer to HBA context object.
6133  *
6134  * This function is called while driver attaches with the
6135  * HBA to setup the extra ring. The extra ring is used
6136  * only when driver needs to support target mode functionality
6137  * or IP over FC functionalities.
6138  *
6139  * This function is called with no lock held.
6140  **/
6141 static int
6142 lpfc_extra_ring_setup( struct lpfc_hba *phba)
6143 {
6144         struct lpfc_sli *psli;
6145         struct lpfc_sli_ring *pring;
6146
6147         psli = &phba->sli;
6148
6149         /* Adjust cmd/rsp ring iocb entries more evenly */
6150
6151         /* Take some away from the FCP ring */
6152         pring = &psli->ring[psli->fcp_ring];
6153         pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6154         pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6155         pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6156         pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6157
6158         /* and give them to the extra ring */
6159         pring = &psli->ring[psli->extra_ring];
6160
6161         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6162         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6163         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6164         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6165
6166         /* Setup default profile for this ring */
6167         pring->iotag_max = 4096;
6168         pring->num_mask = 1;
6169         pring->prt[0].profile = 0;      /* Mask 0 */
6170         pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6171         pring->prt[0].type = phba->cfg_multi_ring_type;
6172         pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6173         return 0;
6174 }
6175
6176 /**
6177  * lpfc_sli_async_event_handler - ASYNC iocb handler function
6178  * @phba: Pointer to HBA context object.
6179  * @pring: Pointer to driver SLI ring object.
6180  * @iocbq: Pointer to iocb object.
6181  *
6182  * This function is called by the slow ring event handler
6183  * function when there is an ASYNC event iocb in the ring.
6184  * This function is called with no lock held.
6185  * Currently this function handles only temperature related
6186  * ASYNC events. The function decodes the temperature sensor
6187  * event message and posts events for the management applications.
6188  **/
6189 static void
6190 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6191         struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6192 {
6193         IOCB_t *icmd;
6194         uint16_t evt_code;
6195         uint16_t temp;
6196         struct temp_event temp_event_data;
6197         struct Scsi_Host *shost;
6198         uint32_t *iocb_w;
6199
6200         icmd = &iocbq->iocb;
6201         evt_code = icmd->un.asyncstat.evt_code;
6202         temp = icmd->ulpContext;
6203
6204         if ((evt_code != ASYNC_TEMP_WARN) &&
6205                 (evt_code != ASYNC_TEMP_SAFE)) {
6206                 iocb_w = (uint32_t *) icmd;
6207                 lpfc_printf_log(phba,
6208                         KERN_ERR,
6209                         LOG_SLI,
6210                         "0346 Ring %d handler: unexpected ASYNC_STATUS"
6211                         " evt_code 0x%x \n"
6212                         "W0  0x%08x W1  0x%08x W2  0x%08x W3  0x%08x\n"
6213                         "W4  0x%08x W5  0x%08x W6  0x%08x W7  0x%08x\n"
6214                         "W8  0x%08x W9  0x%08x W10 0x%08x W11 0x%08x\n"
6215                         "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
6216                         pring->ringno,
6217                         icmd->un.asyncstat.evt_code,
6218                         iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6219                         iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6220                         iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6221                         iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6222
6223                 return;
6224         }
6225         temp_event_data.data = (uint32_t)temp;
6226         temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6227         if (evt_code == ASYNC_TEMP_WARN) {
6228                 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6229                 lpfc_printf_log(phba,
6230                                 KERN_ERR,
6231                                 LOG_TEMP,
6232                                 "0347 Adapter is very hot, please take "
6233                                 "corrective action. temperature : %d Celsius\n",
6234                                 temp);
6235         }
6236         if (evt_code == ASYNC_TEMP_SAFE) {
6237                 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6238                 lpfc_printf_log(phba,
6239                                 KERN_ERR,
6240                                 LOG_TEMP,
6241                                 "0340 Adapter temperature is OK now. "
6242                                 "temperature : %d Celsius\n",
6243                                 temp);
6244         }
6245
6246         /* Send temperature change event to applications */
6247         shost = lpfc_shost_from_vport(phba->pport);
6248         fc_host_post_vendor_event(shost, fc_get_event_number(),
6249                 sizeof(temp_event_data), (char *) &temp_event_data,
6250                 LPFC_NL_VENDOR_ID);
6251
6252 }
6253
6254
6255 /**
6256  * lpfc_sli_setup - SLI ring setup function
6257  * @phba: Pointer to HBA context object.
6258  *
6259  * lpfc_sli_setup sets up rings of the SLI interface with
6260  * number of iocbs per ring and iotags. This function is
6261  * called while driver attach to the HBA and before the
6262  * interrupts are enabled. So there is no need for locking.
6263  *
6264  * This function always returns 0.
6265  **/
6266 int
6267 lpfc_sli_setup(struct lpfc_hba *phba)
6268 {
6269         int i, totiocbsize = 0;
6270         struct lpfc_sli *psli = &phba->sli;
6271         struct lpfc_sli_ring *pring;
6272
6273         psli->num_rings = MAX_CONFIGURED_RINGS;
6274         psli->sli_flag = 0;
6275         psli->fcp_ring = LPFC_FCP_RING;
6276         psli->next_ring = LPFC_FCP_NEXT_RING;
6277         psli->extra_ring = LPFC_EXTRA_RING;
6278
6279         psli->iocbq_lookup = NULL;
6280         psli->iocbq_lookup_len = 0;
6281         psli->last_iotag = 0;
6282
6283         for (i = 0; i < psli->num_rings; i++) {
6284                 pring = &psli->ring[i];
6285                 switch (i) {
6286                 case LPFC_FCP_RING:     /* ring 0 - FCP */
6287                         /* numCiocb and numRiocb are used in config_port */
6288                         pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6289                         pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6290                         pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6291                         pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6292                         pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6293                         pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6294                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6295                                                         SLI3_IOCB_CMD_SIZE :
6296                                                         SLI2_IOCB_CMD_SIZE;
6297                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6298                                                         SLI3_IOCB_RSP_SIZE :
6299                                                         SLI2_IOCB_RSP_SIZE;
6300                         pring->iotag_ctr = 0;
6301                         pring->iotag_max =
6302                             (phba->cfg_hba_queue_depth * 2);
6303                         pring->fast_iotag = pring->iotag_max;
6304                         pring->num_mask = 0;
6305                         break;
6306                 case LPFC_EXTRA_RING:   /* ring 1 - EXTRA */
6307                         /* numCiocb and numRiocb are used in config_port */
6308                         pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6309                         pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
6310                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6311                                                         SLI3_IOCB_CMD_SIZE :
6312                                                         SLI2_IOCB_CMD_SIZE;
6313                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6314                                                         SLI3_IOCB_RSP_SIZE :
6315                                                         SLI2_IOCB_RSP_SIZE;
6316                         pring->iotag_max = phba->cfg_hba_queue_depth;
6317                         pring->num_mask = 0;
6318                         break;
6319                 case LPFC_ELS_RING:     /* ring 2 - ELS / CT */
6320                         /* numCiocb and numRiocb are used in config_port */
6321                         pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6322                         pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
6323                         pring->sizeCiocb = (phba->sli_rev == 3) ?
6324                                                         SLI3_IOCB_CMD_SIZE :
6325                                                         SLI2_IOCB_CMD_SIZE;
6326                         pring->sizeRiocb = (phba->sli_rev == 3) ?
6327                                                         SLI3_IOCB_RSP_SIZE :
6328                                                         SLI2_IOCB_RSP_SIZE;
6329                         pring->fast_iotag = 0;
6330                         pring->iotag_ctr = 0;
6331                         pring->iotag_max = 4096;
6332                         pring->lpfc_sli_rcv_async_status =
6333                                 lpfc_sli_async_event_handler;
6334                         pring->num_mask = 4;
6335                         pring->prt[0].profile = 0;      /* Mask 0 */
6336                         pring->prt[0].rctl = FC_ELS_REQ;
6337                         pring->prt[0].type = FC_ELS_DATA;
6338                         pring->prt[0].lpfc_sli_rcv_unsol_event =
6339                             lpfc_els_unsol_event;
6340                         pring->prt[1].profile = 0;      /* Mask 1 */
6341                         pring->prt[1].rctl = FC_ELS_RSP;
6342                         pring->prt[1].type = FC_ELS_DATA;
6343                         pring->prt[1].lpfc_sli_rcv_unsol_event =
6344                             lpfc_els_unsol_event;
6345                         pring->prt[2].profile = 0;      /* Mask 2 */
6346                         /* NameServer Inquiry */
6347                         pring->prt[2].rctl = FC_UNSOL_CTL;
6348                         /* NameServer */
6349                         pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
6350                         pring->prt[2].lpfc_sli_rcv_unsol_event =
6351                             lpfc_ct_unsol_event;
6352                         pring->prt[3].profile = 0;      /* Mask 3 */
6353                         /* NameServer response */
6354                         pring->prt[3].rctl = FC_SOL_CTL;
6355                         /* NameServer */
6356                         pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
6357                         pring->prt[3].lpfc_sli_rcv_unsol_event =
6358                             lpfc_ct_unsol_event;
6359                         break;
6360                 }
6361                 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
6362                                 (pring->numRiocb * pring->sizeRiocb);
6363         }
6364         if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
6365                 /* Too many cmd / rsp ring entries in SLI2 SLIM */
6366                 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6367                        "SLI2 SLIM Data: x%x x%lx\n",
6368                        phba->brd_no, totiocbsize,
6369                        (unsigned long) MAX_SLIM_IOCB_SIZE);
6370         }
6371         if (phba->cfg_multi_ring_support == 2)
6372                 lpfc_extra_ring_setup(phba);
6373
6374         return 0;
6375 }
6376
6377 /**
6378  * lpfc_sli_queue_setup - Queue initialization function
6379  * @phba: Pointer to HBA context object.
6380  *
6381  * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6382  * ring. This function also initializes ring indices of each ring.
6383  * This function is called during the initialization of the SLI
6384  * interface of an HBA.
6385  * This function is called with no lock held and always returns
6386  * 1.
6387  **/
6388 int
6389 lpfc_sli_queue_setup(struct lpfc_hba *phba)
6390 {
6391         struct lpfc_sli *psli;
6392         struct lpfc_sli_ring *pring;
6393         int i;
6394
6395         psli = &phba->sli;
6396         spin_lock_irq(&phba->hbalock);
6397         INIT_LIST_HEAD(&psli->mboxq);
6398         INIT_LIST_HEAD(&psli->mboxq_cmpl);
6399         /* Initialize list headers for txq and txcmplq as double linked lists */
6400         for (i = 0; i < psli->num_rings; i++) {
6401                 pring = &psli->ring[i];
6402                 pring->ringno = i;
6403                 pring->next_cmdidx  = 0;
6404                 pring->local_getidx = 0;
6405                 pring->cmdidx = 0;
6406                 INIT_LIST_HEAD(&pring->txq);
6407                 INIT_LIST_HEAD(&pring->txcmplq);
6408                 INIT_LIST_HEAD(&pring->iocb_continueq);
6409                 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
6410                 INIT_LIST_HEAD(&pring->postbufq);
6411         }
6412         spin_unlock_irq(&phba->hbalock);
6413         return 1;
6414 }
6415
6416 /**
6417  * lpfc_sli_host_down - Vport cleanup function
6418  * @vport: Pointer to virtual port object.
6419  *
6420  * lpfc_sli_host_down is called to clean up the resources
6421  * associated with a vport before destroying virtual
6422  * port data structures.
6423  * This function does following operations:
6424  * - Free discovery resources associated with this virtual
6425  *   port.
6426  * - Free iocbs associated with this virtual port in
6427  *   the txq.
6428  * - Send abort for all iocb commands associated with this
6429  *   vport in txcmplq.
6430  *
6431  * This function is called with no lock held and always returns 1.
6432  **/
6433 int
6434 lpfc_sli_host_down(struct lpfc_vport *vport)
6435 {
6436         LIST_HEAD(completions);
6437         struct lpfc_hba *phba = vport->phba;
6438         struct lpfc_sli *psli = &phba->sli;
6439         struct lpfc_sli_ring *pring;
6440         struct lpfc_iocbq *iocb, *next_iocb;
6441         int i;
6442         unsigned long flags = 0;
6443         uint16_t prev_pring_flag;
6444
6445         lpfc_cleanup_discovery_resources(vport);
6446
6447         spin_lock_irqsave(&phba->hbalock, flags);
6448         for (i = 0; i < psli->num_rings; i++) {
6449                 pring = &psli->ring[i];
6450                 prev_pring_flag = pring->flag;
6451                 /* Only slow rings */
6452                 if (pring->ringno == LPFC_ELS_RING) {
6453                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6454                         /* Set the lpfc data pending flag */
6455                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6456                 }
6457                 /*
6458                  * Error everything on the txq since these iocbs have not been
6459                  * given to the FW yet.
6460                  */
6461                 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6462                         if (iocb->vport != vport)
6463                                 continue;
6464                         list_move_tail(&iocb->list, &completions);
6465                         pring->txq_cnt--;
6466                 }
6467
6468                 /* Next issue ABTS for everything on the txcmplq */
6469                 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6470                                                                         list) {
6471                         if (iocb->vport != vport)
6472                                 continue;
6473                         lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6474                 }
6475
6476                 pring->flag = prev_pring_flag;
6477         }
6478
6479         spin_unlock_irqrestore(&phba->hbalock, flags);
6480
6481         /* Cancel all the IOCBs from the completions list */
6482         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6483                               IOERR_SLI_DOWN);
6484         return 1;
6485 }
6486
6487 /**
6488  * lpfc_sli_hba_down - Resource cleanup function for the HBA
6489  * @phba: Pointer to HBA context object.
6490  *
6491  * This function cleans up all iocb, buffers, mailbox commands
6492  * while shutting down the HBA. This function is called with no
6493  * lock held and always returns 1.
6494  * This function does the following to cleanup driver resources:
6495  * - Free discovery resources for each virtual port
6496  * - Cleanup any pending fabric iocbs
6497  * - Iterate through the iocb txq and free each entry
6498  *   in the list.
6499  * - Free up any buffer posted to the HBA
6500  * - Free mailbox commands in the mailbox queue.
6501  **/
6502 int
6503 lpfc_sli_hba_down(struct lpfc_hba *phba)
6504 {
6505         LIST_HEAD(completions);
6506         struct lpfc_sli *psli = &phba->sli;
6507         struct lpfc_sli_ring *pring;
6508         struct lpfc_dmabuf *buf_ptr;
6509         LPFC_MBOXQ_t *pmb;
6510         int i;
6511         unsigned long flags = 0;
6512
6513         lpfc_hba_down_prep(phba);
6514
6515         lpfc_fabric_abort_hba(phba);
6516
6517         spin_lock_irqsave(&phba->hbalock, flags);
6518         for (i = 0; i < psli->num_rings; i++) {
6519                 pring = &psli->ring[i];
6520                 /* Only slow rings */
6521                 if (pring->ringno == LPFC_ELS_RING) {
6522                         pring->flag |= LPFC_DEFERRED_RING_EVENT;
6523                         /* Set the lpfc data pending flag */
6524                         set_bit(LPFC_DATA_READY, &phba->data_flags);
6525                 }
6526
6527                 /*
6528                  * Error everything on the txq since these iocbs have not been
6529                  * given to the FW yet.
6530                  */
6531                 list_splice_init(&pring->txq, &completions);
6532                 pring->txq_cnt = 0;
6533
6534         }
6535         spin_unlock_irqrestore(&phba->hbalock, flags);
6536
6537         /* Cancel all the IOCBs from the completions list */
6538         lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6539                               IOERR_SLI_DOWN);
6540
6541         spin_lock_irqsave(&phba->hbalock, flags);
6542         list_splice_init(&phba->elsbuf, &completions);
6543         phba->elsbuf_cnt = 0;
6544         phba->elsbuf_prev_cnt = 0;
6545         spin_unlock_irqrestore(&phba->hbalock, flags);
6546
6547         while (!list_empty(&completions)) {
6548                 list_remove_head(&completions, buf_ptr,
6549                         struct lpfc_dmabuf, list);
6550                 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6551                 kfree(buf_ptr);
6552         }
6553
6554         /* Return any active mbox cmds */
6555         del_timer_sync(&psli->mbox_tmo);
6556
6557         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
6558         phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6559         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
6560
6561         return 1;
6562 }
6563
6564 /**
6565  * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6566  * @phba: Pointer to HBA context object.
6567  *
6568  * This function cleans up all queues, iocb, buffers, mailbox commands while
6569  * shutting down the SLI4 HBA FCoE function. This function is called with no
6570  * lock held and always returns 1.
6571  *
6572  * This function does the following to cleanup driver FCoE function resources:
6573  * - Free discovery resources for each virtual port
6574  * - Cleanup any pending fabric iocbs
6575  * - Iterate through the iocb txq and free each entry in the list.
6576  * - Free up any buffer posted to the HBA.
6577  * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6578  * - Free mailbox commands in the mailbox queue.
6579  **/
6580 int
6581 lpfc_sli4_hba_down(struct lpfc_hba *phba)
6582 {
6583         /* Stop the SLI4 device port */
6584         lpfc_stop_port(phba);
6585
6586         /* Tear down the queues in the HBA */
6587         lpfc_sli4_queue_unset(phba);
6588
6589         /* unregister default FCFI from the HBA */
6590         lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6591
6592         return 1;
6593 }
6594
6595 /**
6596  * lpfc_sli_pcimem_bcopy - SLI memory copy function
6597  * @srcp: Source memory pointer.
6598  * @destp: Destination memory pointer.
6599  * @cnt: Number of words required to be copied.
6600  *
6601  * This function is used for copying data between driver memory
6602  * and the SLI memory. This function also changes the endianness
6603  * of each word if native endianness is different from SLI
6604  * endianness. This function can be called with or without
6605  * lock.
6606  **/
6607 void
6608 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6609 {
6610         uint32_t *src = srcp;
6611         uint32_t *dest = destp;
6612         uint32_t ldata;
6613         int i;
6614
6615         for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6616                 ldata = *src;
6617                 ldata = le32_to_cpu(ldata);
6618                 *dest = ldata;
6619                 src++;
6620                 dest++;
6621         }
6622 }
6623
6624
6625 /**
6626  * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
6627  * @phba: Pointer to HBA context object.
6628  * @pring: Pointer to driver SLI ring object.
6629  * @mp: Pointer to driver buffer object.
6630  *
6631  * This function is called with no lock held.
6632  * It always return zero after adding the buffer to the postbufq
6633  * buffer list.
6634  **/
6635 int
6636 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6637                          struct lpfc_dmabuf *mp)
6638 {
6639         /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6640            later */
6641         spin_lock_irq(&phba->hbalock);
6642         list_add_tail(&mp->list, &pring->postbufq);
6643         pring->postbufq_cnt++;
6644         spin_unlock_irq(&phba->hbalock);
6645         return 0;
6646 }
6647
6648 /**
6649  * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
6650  * @phba: Pointer to HBA context object.
6651  *
6652  * When HBQ is enabled, buffers are searched based on tags. This function
6653  * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6654  * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6655  * does not conflict with tags of buffer posted for unsolicited events.
6656  * The function returns the allocated tag. The function is called with
6657  * no locks held.
6658  **/
6659 uint32_t
6660 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6661 {
6662         spin_lock_irq(&phba->hbalock);
6663         phba->buffer_tag_count++;
6664         /*
6665          * Always set the QUE_BUFTAG_BIT to distiguish between
6666          * a tag assigned by HBQ.
6667          */
6668         phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6669         spin_unlock_irq(&phba->hbalock);
6670         return phba->buffer_tag_count;
6671 }
6672
6673 /**
6674  * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
6675  * @phba: Pointer to HBA context object.
6676  * @pring: Pointer to driver SLI ring object.
6677  * @tag: Buffer tag.
6678  *
6679  * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6680  * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6681  * iocb is posted to the response ring with the tag of the buffer.
6682  * This function searches the pring->postbufq list using the tag
6683  * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6684  * iocb. If the buffer is found then lpfc_dmabuf object of the
6685  * buffer is returned to the caller else NULL is returned.
6686  * This function is called with no lock held.
6687  **/
6688 struct lpfc_dmabuf *
6689 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6690                         uint32_t tag)
6691 {
6692         struct lpfc_dmabuf *mp, *next_mp;
6693         struct list_head *slp = &pring->postbufq;
6694
6695         /* Search postbufq, from the begining, looking for a match on tag */
6696         spin_lock_irq(&phba->hbalock);
6697         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6698                 if (mp->buffer_tag == tag) {
6699                         list_del_init(&mp->list);
6700                         pring->postbufq_cnt--;
6701                         spin_unlock_irq(&phba->hbalock);
6702                         return mp;
6703                 }
6704         }
6705
6706         spin_unlock_irq(&phba->hbalock);
6707         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6708                         "0402 Cannot find virtual addr for buffer tag on "
6709                         "ring %d Data x%lx x%p x%p x%x\n",
6710                         pring->ringno, (unsigned long) tag,
6711                         slp->next, slp->prev, pring->postbufq_cnt);
6712
6713         return NULL;
6714 }
6715
6716 /**
6717  * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
6718  * @phba: Pointer to HBA context object.
6719  * @pring: Pointer to driver SLI ring object.
6720  * @phys: DMA address of the buffer.
6721  *
6722  * This function searches the buffer list using the dma_address
6723  * of unsolicited event to find the driver's lpfc_dmabuf object
6724  * corresponding to the dma_address. The function returns the
6725  * lpfc_dmabuf object if a buffer is found else it returns NULL.
6726  * This function is called by the ct and els unsolicited event
6727  * handlers to get the buffer associated with the unsolicited
6728  * event.
6729  *
6730  * This function is called with no lock held.
6731  **/
6732 struct lpfc_dmabuf *
6733 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6734                          dma_addr_t phys)
6735 {
6736         struct lpfc_dmabuf *mp, *next_mp;
6737         struct list_head *slp = &pring->postbufq;
6738
6739         /* Search postbufq, from the begining, looking for a match on phys */
6740         spin_lock_irq(&phba->hbalock);
6741         list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6742                 if (mp->phys == phys) {
6743                         list_del_init(&mp->list);
6744                         pring->postbufq_cnt--;
6745                         spin_unlock_irq(&phba->hbalock);
6746                         return mp;
6747                 }
6748         }
6749
6750         spin_unlock_irq(&phba->hbalock);
6751         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6752                         "0410 Cannot find virtual addr for mapped buf on "
6753                         "ring %d Data x%llx x%p x%p x%x\n",
6754                         pring->ringno, (unsigned long long)phys,
6755                         slp->next, slp->prev, pring->postbufq_cnt);
6756         return NULL;
6757 }
6758
6759 /**
6760  * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
6761  * @phba: Pointer to HBA context object.
6762  * @cmdiocb: Pointer to driver command iocb object.
6763  * @rspiocb: Pointer to driver response iocb object.
6764  *
6765  * This function is the completion handler for the abort iocbs for
6766  * ELS commands. This function is called from the ELS ring event
6767  * handler with no lock held. This function frees memory resources
6768  * associated with the abort iocb.
6769  **/
6770 static void
6771 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6772                         struct lpfc_iocbq *rspiocb)
6773 {
6774         IOCB_t *irsp = &rspiocb->iocb;
6775         uint16_t abort_iotag, abort_context;
6776         struct lpfc_iocbq *abort_iocb;
6777         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6778
6779         abort_iocb = NULL;
6780
6781         if (irsp->ulpStatus) {
6782                 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
6783                 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
6784
6785                 spin_lock_irq(&phba->hbalock);
6786                 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
6787                         abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
6788
6789                 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
6790                                 "0327 Cannot abort els iocb %p "
6791                                 "with tag %x context %x, abort status %x, "
6792                                 "abort code %x\n",
6793                                 abort_iocb, abort_iotag, abort_context,
6794                                 irsp->ulpStatus, irsp->un.ulpWord[4]);
6795
6796                 /*
6797                  *  If the iocb is not found in Firmware queue the iocb
6798                  *  might have completed already. Do not free it again.
6799                  */
6800                 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
6801                         spin_unlock_irq(&phba->hbalock);
6802                         lpfc_sli_release_iocbq(phba, cmdiocb);
6803                         return;
6804                 }
6805                 /*
6806                  * make sure we have the right iocbq before taking it
6807                  * off the txcmplq and try to call completion routine.
6808                  */
6809                 if (!abort_iocb ||
6810                     abort_iocb->iocb.ulpContext != abort_context ||
6811                     (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
6812                         spin_unlock_irq(&phba->hbalock);
6813                 else {
6814                         list_del_init(&abort_iocb->list);
6815                         pring->txcmplq_cnt--;
6816                         spin_unlock_irq(&phba->hbalock);
6817
6818                         /* Firmware could still be in progress of DMAing
6819                          * payload, so don't free data buffer till after
6820                          * a hbeat.
6821                          */
6822                         abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
6823
6824                         abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
6825                         abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
6826                         abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
6827                         (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
6828                 }
6829         }
6830
6831         lpfc_sli_release_iocbq(phba, cmdiocb);
6832         return;
6833 }
6834
6835 /**
6836  * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
6837  * @phba: Pointer to HBA context object.
6838  * @cmdiocb: Pointer to driver command iocb object.
6839  * @rspiocb: Pointer to driver response iocb object.
6840  *
6841  * The function is called from SLI ring event handler with no
6842  * lock held. This function is the completion handler for ELS commands
6843  * which are aborted. The function frees memory resources used for
6844  * the aborted ELS commands.
6845  **/
6846 static void
6847 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6848                      struct lpfc_iocbq *rspiocb)
6849 {
6850         IOCB_t *irsp = &rspiocb->iocb;
6851
6852         /* ELS cmd tag <ulpIoTag> completes */
6853         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
6854                         "0139 Ignoring ELS cmd tag x%x completion Data: "
6855                         "x%x x%x x%x\n",
6856                         irsp->ulpIoTag, irsp->ulpStatus,
6857                         irsp->un.ulpWord[4], irsp->ulpTimeout);
6858         if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
6859                 lpfc_ct_free_iocb(phba, cmdiocb);
6860         else
6861                 lpfc_els_free_iocb(phba, cmdiocb);
6862         return;
6863 }
6864
6865 /**
6866  * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
6867  * @phba: Pointer to HBA context object.
6868  * @pring: Pointer to driver SLI ring object.
6869  * @cmdiocb: Pointer to driver command iocb object.
6870  *
6871  * This function issues an abort iocb for the provided command
6872  * iocb. This function is called with hbalock held.
6873  * The function returns 0 when it fails due to memory allocation
6874  * failure or when the command iocb is an abort request.
6875  **/
6876 int
6877 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6878                            struct lpfc_iocbq *cmdiocb)
6879 {
6880         struct lpfc_vport *vport = cmdiocb->vport;
6881         struct lpfc_iocbq *abtsiocbp;
6882         IOCB_t *icmd = NULL;
6883         IOCB_t *iabt = NULL;
6884         int retval = IOCB_ERROR;
6885
6886         /*
6887          * There are certain command types we don't want to abort.  And we
6888          * don't want to abort commands that are already in the process of
6889          * being aborted.
6890          */
6891         icmd = &cmdiocb->iocb;
6892         if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
6893             icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
6894             (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
6895                 return 0;
6896
6897         /* If we're unloading, don't abort iocb on the ELS ring, but change the
6898          * callback so that nothing happens when it finishes.
6899          */
6900         if ((vport->load_flag & FC_UNLOADING) &&
6901             (pring->ringno == LPFC_ELS_RING)) {
6902                 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
6903                         cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
6904                 else
6905                         cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
6906                 goto abort_iotag_exit;
6907         }
6908
6909         /* issue ABTS for this IOCB based on iotag */
6910         abtsiocbp = __lpfc_sli_get_iocbq(phba);
6911         if (abtsiocbp == NULL)
6912                 return 0;
6913
6914         /* This signals the response to set the correct status
6915          * before calling the completion handler.
6916          */
6917         cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
6918
6919         iabt = &abtsiocbp->iocb;
6920         iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
6921         iabt->un.acxri.abortContextTag = icmd->ulpContext;
6922         if (phba->sli_rev == LPFC_SLI_REV4)
6923                 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
6924         else
6925                 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
6926         iabt->ulpLe = 1;
6927         iabt->ulpClass = icmd->ulpClass;
6928
6929         if (phba->link_state >= LPFC_LINK_UP)
6930                 iabt->ulpCommand = CMD_ABORT_XRI_CN;
6931         else
6932                 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
6933
6934         abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
6935
6936         lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
6937                          "0339 Abort xri x%x, original iotag x%x, "
6938                          "abort cmd iotag x%x\n",
6939                          iabt->un.acxri.abortContextTag,
6940                          iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
6941         retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
6942
6943         if (retval)
6944                 __lpfc_sli_release_iocbq(phba, abtsiocbp);
6945 abort_iotag_exit:
6946         /*
6947          * Caller to this routine should check for IOCB_ERROR
6948          * and handle it properly.  This routine no longer removes
6949          * iocb off txcmplq and call compl in case of IOCB_ERROR.
6950          */
6951         return retval;
6952 }
6953
6954 /**
6955  * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
6956  * @iocbq: Pointer to driver iocb object.
6957  * @vport: Pointer to driver virtual port object.
6958  * @tgt_id: SCSI ID of the target.
6959  * @lun_id: LUN ID of the scsi device.
6960  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
6961  *
6962  * This function acts as an iocb filter for functions which abort or count
6963  * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
6964  * 0 if the filtering criteria is met for the given iocb and will return
6965  * 1 if the filtering criteria is not met.
6966  * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
6967  * given iocb is for the SCSI device specified by vport, tgt_id and
6968  * lun_id parameter.
6969  * If ctx_cmd == LPFC_CTX_TGT,  the function returns 0 only if the
6970  * given iocb is for the SCSI target specified by vport and tgt_id
6971  * parameters.
6972  * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
6973  * given iocb is for the SCSI host associated with the given vport.
6974  * This function is called with no locks held.
6975  **/
6976 static int
6977 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
6978                            uint16_t tgt_id, uint64_t lun_id,
6979                            lpfc_ctx_cmd ctx_cmd)
6980 {
6981         struct lpfc_scsi_buf *lpfc_cmd;
6982         int rc = 1;
6983
6984         if (!(iocbq->iocb_flag &  LPFC_IO_FCP))
6985                 return rc;
6986
6987         if (iocbq->vport != vport)
6988                 return rc;
6989
6990         lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
6991
6992         if (lpfc_cmd->pCmd == NULL)
6993                 return rc;
6994
6995         switch (ctx_cmd) {
6996         case LPFC_CTX_LUN:
6997                 if ((lpfc_cmd->rdata->pnode) &&
6998                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
6999                     (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
7000                         rc = 0;
7001                 break;
7002         case LPFC_CTX_TGT:
7003                 if ((lpfc_cmd->rdata->pnode) &&
7004                     (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
7005                         rc = 0;
7006                 break;
7007         case LPFC_CTX_HOST:
7008                 rc = 0;
7009                 break;
7010         default:
7011                 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
7012                         __func__, ctx_cmd);
7013                 break;
7014         }
7015
7016         return rc;
7017 }
7018
7019 /**
7020  * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
7021  * @vport: Pointer to virtual port.
7022  * @tgt_id: SCSI ID of the target.
7023  * @lun_id: LUN ID of the scsi device.
7024  * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7025  *
7026  * This function returns number of FCP commands pending for the vport.
7027  * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7028  * commands pending on the vport associated with SCSI device specified
7029  * by tgt_id and lun_id parameters.
7030  * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7031  * commands pending on the vport associated with SCSI target specified
7032  * by tgt_id parameter.
7033  * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7034  * commands pending on the vport.
7035  * This function returns the number of iocbs which satisfy the filter.
7036  * This function is called without any lock held.
7037  **/
7038 int
7039 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7040                   lpfc_ctx_cmd ctx_cmd)
7041 {
7042         struct lpfc_hba *phba = vport->phba;
7043         struct lpfc_iocbq *iocbq;
7044         int sum, i;
7045
7046         for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7047                 iocbq = phba->sli.iocbq_lookup[i];
7048
7049                 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7050                                                 ctx_cmd) == 0)
7051                         sum++;
7052         }
7053
7054         return sum;
7055 }
7056
7057 /**
7058  * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
7059  * @phba: Pointer to HBA context object
7060  * @cmdiocb: Pointer to command iocb object.
7061  * @rspiocb: Pointer to response iocb object.
7062  *
7063  * This function is called when an aborted FCP iocb completes. This
7064  * function is called by the ring event handler with no lock held.
7065  * This function frees the iocb.
7066  **/
7067 void
7068 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7069                         struct lpfc_iocbq *rspiocb)
7070 {
7071         lpfc_sli_release_iocbq(phba, cmdiocb);
7072         return;
7073 }
7074
7075 /**
7076  * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
7077  * @vport: Pointer to virtual port.
7078  * @pring: Pointer to driver SLI ring object.
7079  * @tgt_id: SCSI ID of the target.
7080  * @lun_id: LUN ID of the scsi device.
7081  * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7082  *
7083  * This function sends an abort command for every SCSI command
7084  * associated with the given virtual port pending on the ring
7085  * filtered by lpfc_sli_validate_fcp_iocb function.
7086  * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7087  * FCP iocbs associated with lun specified by tgt_id and lun_id
7088  * parameters
7089  * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7090  * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7091  * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7092  * FCP iocbs associated with virtual port.
7093  * This function returns number of iocbs it failed to abort.
7094  * This function is called with no locks held.
7095  **/
7096 int
7097 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7098                     uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
7099 {
7100         struct lpfc_hba *phba = vport->phba;
7101         struct lpfc_iocbq *iocbq;
7102         struct lpfc_iocbq *abtsiocb;
7103         IOCB_t *cmd = NULL;
7104         int errcnt = 0, ret_val = 0;
7105         int i;
7106
7107         for (i = 1; i <= phba->sli.last_iotag; i++) {
7108                 iocbq = phba->sli.iocbq_lookup[i];
7109
7110                 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
7111                                                abort_cmd) != 0)
7112                         continue;
7113
7114                 /* issue ABTS for this IOCB based on iotag */
7115                 abtsiocb = lpfc_sli_get_iocbq(phba);
7116                 if (abtsiocb == NULL) {
7117                         errcnt++;
7118                         continue;
7119                 }
7120
7121                 cmd = &iocbq->iocb;
7122                 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7123                 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
7124                 if (phba->sli_rev == LPFC_SLI_REV4)
7125                         abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7126                 else
7127                         abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
7128                 abtsiocb->iocb.ulpLe = 1;
7129                 abtsiocb->iocb.ulpClass = cmd->ulpClass;
7130                 abtsiocb->vport = phba->pport;
7131
7132                 if (lpfc_is_link_up(phba))
7133                         abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7134                 else
7135                         abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7136
7137                 /* Setup callback routine and issue the command. */
7138                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
7139                 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7140                                               abtsiocb, 0);
7141                 if (ret_val == IOCB_ERROR) {
7142                         lpfc_sli_release_iocbq(phba, abtsiocb);
7143                         errcnt++;
7144                         continue;
7145                 }
7146         }
7147
7148         return errcnt;
7149 }
7150
7151 /**
7152  * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
7153  * @phba: Pointer to HBA context object.
7154  * @cmdiocbq: Pointer to command iocb.
7155  * @rspiocbq: Pointer to response iocb.
7156  *
7157  * This function is the completion handler for iocbs issued using
7158  * lpfc_sli_issue_iocb_wait function. This function is called by the
7159  * ring event handler function without any lock held. This function
7160  * can be called from both worker thread context and interrupt
7161  * context. This function also can be called from other thread which
7162  * cleans up the SLI layer objects.
7163  * This function copy the contents of the response iocb to the
7164  * response iocb memory object provided by the caller of
7165  * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7166  * sleeps for the iocb completion.
7167  **/
7168 static void
7169 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7170                         struct lpfc_iocbq *cmdiocbq,
7171                         struct lpfc_iocbq *rspiocbq)
7172 {
7173         wait_queue_head_t *pdone_q;
7174         unsigned long iflags;
7175
7176         spin_lock_irqsave(&phba->hbalock, iflags);
7177         cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7178         if (cmdiocbq->context2 && rspiocbq)
7179                 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7180                        &rspiocbq->iocb, sizeof(IOCB_t));
7181
7182         pdone_q = cmdiocbq->context_un.wait_queue;
7183         if (pdone_q)
7184                 wake_up(pdone_q);
7185         spin_unlock_irqrestore(&phba->hbalock, iflags);
7186         return;
7187 }
7188
7189 /**
7190  * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
7191  * @phba: Pointer to HBA context object..
7192  * @pring: Pointer to sli ring.
7193  * @piocb: Pointer to command iocb.
7194  * @prspiocbq: Pointer to response iocb.
7195  * @timeout: Timeout in number of seconds.
7196  *
7197  * This function issues the iocb to firmware and waits for the
7198  * iocb to complete. If the iocb command is not
7199  * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7200  * Caller should not free the iocb resources if this function
7201  * returns IOCB_TIMEDOUT.
7202  * The function waits for the iocb completion using an
7203  * non-interruptible wait.
7204  * This function will sleep while waiting for iocb completion.
7205  * So, this function should not be called from any context which
7206  * does not allow sleeping. Due to the same reason, this function
7207  * cannot be called with interrupt disabled.
7208  * This function assumes that the iocb completions occur while
7209  * this function sleep. So, this function cannot be called from
7210  * the thread which process iocb completion for this ring.
7211  * This function clears the iocb_flag of the iocb object before
7212  * issuing the iocb and the iocb completion handler sets this
7213  * flag and wakes this thread when the iocb completes.
7214  * The contents of the response iocb will be copied to prspiocbq
7215  * by the completion handler when the command completes.
7216  * This function returns IOCB_SUCCESS when success.
7217  * This function is called with no lock held.
7218  **/
7219 int
7220 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
7221                          uint32_t ring_number,
7222                          struct lpfc_iocbq *piocb,
7223                          struct lpfc_iocbq *prspiocbq,
7224                          uint32_t timeout)
7225 {
7226         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7227         long timeleft, timeout_req = 0;
7228         int retval = IOCB_SUCCESS;
7229         uint32_t creg_val;
7230
7231         /*
7232          * If the caller has provided a response iocbq buffer, then context2
7233          * is NULL or its an error.
7234          */
7235         if (prspiocbq) {
7236                 if (piocb->context2)
7237                         return IOCB_ERROR;
7238                 piocb->context2 = prspiocbq;
7239         }
7240
7241         piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7242         piocb->context_un.wait_queue = &done_q;
7243         piocb->iocb_flag &= ~LPFC_IO_WAKE;
7244
7245         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7246                 creg_val = readl(phba->HCregaddr);
7247                 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7248                 writel(creg_val, phba->HCregaddr);
7249                 readl(phba->HCregaddr); /* flush */
7250         }
7251
7252         retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
7253         if (retval == IOCB_SUCCESS) {
7254                 timeout_req = timeout * HZ;
7255                 timeleft = wait_event_timeout(done_q,
7256                                 piocb->iocb_flag & LPFC_IO_WAKE,
7257                                 timeout_req);
7258
7259                 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7260                         lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7261                                         "0331 IOCB wake signaled\n");
7262                 } else if (timeleft == 0) {
7263                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7264                                         "0338 IOCB wait timeout error - no "
7265                                         "wake response Data x%x\n", timeout);
7266                         retval = IOCB_TIMEDOUT;
7267                 } else {
7268                         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7269                                         "0330 IOCB wake NOT set, "
7270                                         "Data x%x x%lx\n",
7271                                         timeout, (timeleft / jiffies));
7272                         retval = IOCB_TIMEDOUT;
7273                 }
7274         } else {
7275                 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
7276                                 "0332 IOCB wait issue failed, Data x%x\n",
7277                                 retval);
7278                 retval = IOCB_ERROR;
7279         }
7280
7281         if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7282                 creg_val = readl(phba->HCregaddr);
7283                 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7284                 writel(creg_val, phba->HCregaddr);
7285                 readl(phba->HCregaddr); /* flush */
7286         }
7287
7288         if (prspiocbq)
7289                 piocb->context2 = NULL;
7290
7291         piocb->context_un.wait_queue = NULL;
7292         piocb->iocb_cmpl = NULL;
7293         return retval;
7294 }
7295
7296 /**
7297  * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
7298  * @phba: Pointer to HBA context object.
7299  * @pmboxq: Pointer to driver mailbox object.
7300  * @timeout: Timeout in number of seconds.
7301  *
7302  * This function issues the mailbox to firmware and waits for the
7303  * mailbox command to complete. If the mailbox command is not
7304  * completed within timeout seconds, it returns MBX_TIMEOUT.
7305  * The function waits for the mailbox completion using an
7306  * interruptible wait. If the thread is woken up due to a
7307  * signal, MBX_TIMEOUT error is returned to the caller. Caller
7308  * should not free the mailbox resources, if this function returns
7309  * MBX_TIMEOUT.
7310  * This function will sleep while waiting for mailbox completion.
7311  * So, this function should not be called from any context which
7312  * does not allow sleeping. Due to the same reason, this function
7313  * cannot be called with interrupt disabled.
7314  * This function assumes that the mailbox completion occurs while
7315  * this function sleep. So, this function cannot be called from
7316  * the worker thread which processes mailbox completion.
7317  * This function is called in the context of HBA management
7318  * applications.
7319  * This function returns MBX_SUCCESS when successful.
7320  * This function is called with no lock held.
7321  **/
7322 int
7323 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
7324                          uint32_t timeout)
7325 {
7326         DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
7327         int retval;
7328         unsigned long flag;
7329
7330         /* The caller must leave context1 empty. */
7331         if (pmboxq->context1)
7332                 return MBX_NOT_FINISHED;
7333
7334         pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
7335         /* setup wake call as IOCB callback */
7336         pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7337         /* setup context field to pass wait_queue pointer to wake function  */
7338         pmboxq->context1 = &done_q;
7339
7340         /* now issue the command */
7341         retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7342
7343         if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7344                 wait_event_interruptible_timeout(done_q,
7345                                 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7346                                 timeout * HZ);
7347
7348                 spin_lock_irqsave(&phba->hbalock, flag);
7349                 pmboxq->context1 = NULL;
7350                 /*
7351                  * if LPFC_MBX_WAKE flag is set the mailbox is completed
7352                  * else do not free the resources.
7353                  */
7354                 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
7355                         retval = MBX_SUCCESS;
7356                 else {
7357                         retval = MBX_TIMEOUT;
7358                         pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7359                 }
7360                 spin_unlock_irqrestore(&phba->hbalock, flag);
7361         }
7362
7363         return retval;
7364 }
7365
7366 /**
7367  * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
7368  * @phba: Pointer to HBA context.
7369  *
7370  * This function is called to shutdown the driver's mailbox sub-system.
7371  * It first marks the mailbox sub-system is in a block state to prevent
7372  * the asynchronous mailbox command from issued off the pending mailbox
7373  * command queue. If the mailbox command sub-system shutdown is due to
7374  * HBA error conditions such as EEH or ERATT, this routine shall invoke
7375  * the mailbox sub-system flush routine to forcefully bring down the
7376  * mailbox sub-system. Otherwise, if it is due to normal condition (such
7377  * as with offline or HBA function reset), this routine will wait for the
7378  * outstanding mailbox command to complete before invoking the mailbox
7379  * sub-system flush routine to gracefully bring down mailbox sub-system.
7380  **/
7381 void
7382 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
7383 {
7384         struct lpfc_sli *psli = &phba->sli;
7385         uint8_t actcmd = MBX_HEARTBEAT;
7386         unsigned long timeout;
7387
7388         spin_lock_irq(&phba->hbalock);
7389         psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7390         spin_unlock_irq(&phba->hbalock);
7391
7392         if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7393                 spin_lock_irq(&phba->hbalock);
7394                 if (phba->sli.mbox_active)
7395                         actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7396                 spin_unlock_irq(&phba->hbalock);
7397                 /* Determine how long we might wait for the active mailbox
7398                  * command to be gracefully completed by firmware.
7399                  */
7400                 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7401                                            1000) + jiffies;
7402                 while (phba->sli.mbox_active) {
7403                         /* Check active mailbox complete status every 2ms */
7404                         msleep(2);
7405                         if (time_after(jiffies, timeout))
7406                                 /* Timeout, let the mailbox flush routine to
7407                                  * forcefully release active mailbox command
7408                                  */
7409                                 break;
7410                 }
7411         }
7412         lpfc_sli_mbox_sys_flush(phba);
7413 }
7414
7415 /**
7416  * lpfc_sli_eratt_read - read sli-3 error attention events
7417  * @phba: Pointer to HBA context.
7418  *
7419  * This function is called to read the SLI3 device error attention registers
7420  * for possible error attention events. The caller must hold the hostlock
7421  * with spin_lock_irq().
7422  *
7423  * This fucntion returns 1 when there is Error Attention in the Host Attention
7424  * Register and returns 0 otherwise.
7425  **/
7426 static int
7427 lpfc_sli_eratt_read(struct lpfc_hba *phba)
7428 {
7429         uint32_t ha_copy;
7430
7431         /* Read chip Host Attention (HA) register */
7432         ha_copy = readl(phba->HAregaddr);
7433         if (ha_copy & HA_ERATT) {
7434                 /* Read host status register to retrieve error event */
7435                 lpfc_sli_read_hs(phba);
7436
7437                 /* Check if there is a deferred error condition is active */
7438                 if ((HS_FFER1 & phba->work_hs) &&
7439                     ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7440                      HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7441                         spin_lock_irq(&phba->hbalock);
7442                         phba->hba_flag |= DEFER_ERATT;
7443                         spin_unlock_irq(&phba->hbalock);
7444                         /* Clear all interrupt enable conditions */
7445                         writel(0, phba->HCregaddr);
7446                         readl(phba->HCregaddr);
7447                 }
7448
7449                 /* Set the driver HA work bitmap */
7450                 spin_lock_irq(&phba->hbalock);
7451                 phba->work_ha |= HA_ERATT;
7452                 /* Indicate polling handles this ERATT */
7453                 phba->hba_flag |= HBA_ERATT_HANDLED;
7454                 spin_unlock_irq(&phba->hbalock);
7455                 return 1;
7456         }
7457         return 0;
7458 }
7459
7460 /**
7461  * lpfc_sli4_eratt_read - read sli-4 error attention events
7462  * @phba: Pointer to HBA context.
7463  *
7464  * This function is called to read the SLI4 device error attention registers
7465  * for possible error attention events. The caller must hold the hostlock
7466  * with spin_lock_irq().
7467  *
7468  * This fucntion returns 1 when there is Error Attention in the Host Attention
7469  * Register and returns 0 otherwise.
7470  **/
7471 static int
7472 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7473 {
7474         uint32_t uerr_sta_hi, uerr_sta_lo;
7475         uint32_t onlnreg0, onlnreg1;
7476
7477         /* For now, use the SLI4 device internal unrecoverable error
7478          * registers for error attention. This can be changed later.
7479          */
7480         onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7481         onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7482         if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7483                 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7484                 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7485                 if (uerr_sta_lo || uerr_sta_hi) {
7486                         lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7487                                         "1423 HBA Unrecoverable error: "
7488                                         "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7489                                         "online0_reg=0x%x, online1_reg=0x%x\n",
7490                                         uerr_sta_lo, uerr_sta_hi,
7491                                         onlnreg0, onlnreg1);
7492                         /* TEMP: as the driver error recover logic is not
7493                          * fully developed, we just log the error message
7494                          * and the device error attention action is now
7495                          * temporarily disabled.
7496                          */
7497                         return 0;
7498                         phba->work_status[0] = uerr_sta_lo;
7499                         phba->work_status[1] = uerr_sta_hi;
7500                         spin_lock_irq(&phba->hbalock);
7501                         /* Set the driver HA work bitmap */
7502                         phba->work_ha |= HA_ERATT;
7503                         /* Indicate polling handles this ERATT */
7504                         phba->hba_flag |= HBA_ERATT_HANDLED;
7505                         spin_unlock_irq(&phba->hbalock);
7506                         return 1;
7507                 }
7508         }
7509         return 0;
7510 }
7511
7512 /**
7513  * lpfc_sli_check_eratt - check error attention events
7514  * @phba: Pointer to HBA context.
7515  *
7516  * This function is called from timer soft interrupt context to check HBA's
7517  * error attention register bit for error attention events.
7518  *
7519  * This fucntion returns 1 when there is Error Attention in the Host Attention
7520  * Register and returns 0 otherwise.
7521  **/
7522 int
7523 lpfc_sli_check_eratt(struct lpfc_hba *phba)
7524 {
7525         uint32_t ha_copy;
7526
7527         /* If somebody is waiting to handle an eratt, don't process it
7528          * here. The brdkill function will do this.
7529          */
7530         if (phba->link_flag & LS_IGNORE_ERATT)
7531                 return 0;
7532
7533         /* Check if interrupt handler handles this ERATT */
7534         spin_lock_irq(&phba->hbalock);
7535         if (phba->hba_flag & HBA_ERATT_HANDLED) {
7536                 /* Interrupt handler has handled ERATT */
7537                 spin_unlock_irq(&phba->hbalock);
7538                 return 0;
7539         }
7540
7541         /*
7542          * If there is deferred error attention, do not check for error
7543          * attention
7544          */
7545         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7546                 spin_unlock_irq(&phba->hbalock);
7547                 return 0;
7548         }
7549
7550         /* If PCI channel is offline, don't process it */
7551         if (unlikely(pci_channel_offline(phba->pcidev))) {
7552                 spin_unlock_irq(&phba->hbalock);
7553                 return 0;
7554         }
7555
7556         switch (phba->sli_rev) {
7557         case LPFC_SLI_REV2:
7558         case LPFC_SLI_REV3:
7559                 /* Read chip Host Attention (HA) register */
7560                 ha_copy = lpfc_sli_eratt_read(phba);
7561                 break;
7562         case LPFC_SLI_REV4:
7563                 /* Read devcie Uncoverable Error (UERR) registers */
7564                 ha_copy = lpfc_sli4_eratt_read(phba);
7565                 break;
7566         default:
7567                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7568                                 "0299 Invalid SLI revision (%d)\n",
7569                                 phba->sli_rev);
7570                 ha_copy = 0;
7571                 break;
7572         }
7573         spin_unlock_irq(&phba->hbalock);
7574
7575         return ha_copy;
7576 }
7577
7578 /**
7579  * lpfc_intr_state_check - Check device state for interrupt handling
7580  * @phba: Pointer to HBA context.
7581  *
7582  * This inline routine checks whether a device or its PCI slot is in a state
7583  * that the interrupt should be handled.
7584  *
7585  * This function returns 0 if the device or the PCI slot is in a state that
7586  * interrupt should be handled, otherwise -EIO.
7587  */
7588 static inline int
7589 lpfc_intr_state_check(struct lpfc_hba *phba)
7590 {
7591         /* If the pci channel is offline, ignore all the interrupts */
7592         if (unlikely(pci_channel_offline(phba->pcidev)))
7593                 return -EIO;
7594
7595         /* Update device level interrupt statistics */
7596         phba->sli.slistat.sli_intr++;
7597
7598         /* Ignore all interrupts during initialization. */
7599         if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7600                 return -EIO;
7601
7602         return 0;
7603 }
7604
7605 /**
7606  * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
7607  * @irq: Interrupt number.
7608  * @dev_id: The device context pointer.
7609  *
7610  * This function is directly called from the PCI layer as an interrupt
7611  * service routine when device with SLI-3 interface spec is enabled with
7612  * MSI-X multi-message interrupt mode and there are slow-path events in
7613  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7614  * interrupt mode, this function is called as part of the device-level
7615  * interrupt handler. When the PCI slot is in error recovery or the HBA
7616  * is undergoing initialization, the interrupt handler will not process
7617  * the interrupt. The link attention and ELS ring attention events are
7618  * handled by the worker thread. The interrupt handler signals the worker
7619  * thread and returns for these events. This function is called without
7620  * any lock held. It gets the hbalock to access and update SLI data
7621  * structures.
7622  *
7623  * This function returns IRQ_HANDLED when interrupt is handled else it
7624  * returns IRQ_NONE.
7625  **/
7626 irqreturn_t
7627 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
7628 {
7629         struct lpfc_hba  *phba;
7630         uint32_t ha_copy;
7631         uint32_t work_ha_copy;
7632         unsigned long status;
7633         unsigned long iflag;
7634         uint32_t control;
7635
7636         MAILBOX_t *mbox, *pmbox;
7637         struct lpfc_vport *vport;
7638         struct lpfc_nodelist *ndlp;
7639         struct lpfc_dmabuf *mp;
7640         LPFC_MBOXQ_t *pmb;
7641         int rc;
7642
7643         /*
7644          * Get the driver's phba structure from the dev_id and
7645          * assume the HBA is not interrupting.
7646          */
7647         phba = (struct lpfc_hba *)dev_id;
7648
7649         if (unlikely(!phba))
7650                 return IRQ_NONE;
7651
7652         /*
7653          * Stuff needs to be attented to when this function is invoked as an
7654          * individual interrupt handler in MSI-X multi-message interrupt mode
7655          */
7656         if (phba->intr_type == MSIX) {
7657                 /* Check device state for handling interrupt */
7658                 if (lpfc_intr_state_check(phba))
7659                         return IRQ_NONE;
7660                 /* Need to read HA REG for slow-path events */
7661                 spin_lock_irqsave(&phba->hbalock, iflag);
7662                 ha_copy = readl(phba->HAregaddr);
7663                 /* If somebody is waiting to handle an eratt don't process it
7664                  * here. The brdkill function will do this.
7665                  */
7666                 if (phba->link_flag & LS_IGNORE_ERATT)
7667                         ha_copy &= ~HA_ERATT;
7668                 /* Check the need for handling ERATT in interrupt handler */
7669                 if (ha_copy & HA_ERATT) {
7670                         if (phba->hba_flag & HBA_ERATT_HANDLED)
7671                                 /* ERATT polling has handled ERATT */
7672                                 ha_copy &= ~HA_ERATT;
7673                         else
7674                                 /* Indicate interrupt handler handles ERATT */
7675                                 phba->hba_flag |= HBA_ERATT_HANDLED;
7676                 }
7677
7678                 /*
7679                  * If there is deferred error attention, do not check for any
7680                  * interrupt.
7681                  */
7682                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7683                         spin_unlock_irqrestore(&phba->hbalock, iflag);
7684                         return IRQ_NONE;
7685                 }
7686
7687                 /* Clear up only attention source related to slow-path */
7688                 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7689                         phba->HAregaddr);
7690                 readl(phba->HAregaddr); /* flush */
7691                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7692         } else
7693                 ha_copy = phba->ha_copy;
7694
7695         work_ha_copy = ha_copy & phba->work_ha_mask;
7696
7697         if (work_ha_copy) {
7698                 if (work_ha_copy & HA_LATT) {
7699                         if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7700                                 /*
7701                                  * Turn off Link Attention interrupts
7702                                  * until CLEAR_LA done
7703                                  */
7704                                 spin_lock_irqsave(&phba->hbalock, iflag);
7705                                 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7706                                 control = readl(phba->HCregaddr);
7707                                 control &= ~HC_LAINT_ENA;
7708                                 writel(control, phba->HCregaddr);
7709                                 readl(phba->HCregaddr); /* flush */
7710                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7711                         }
7712                         else
7713                                 work_ha_copy &= ~HA_LATT;
7714                 }
7715
7716                 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
7717                         /*
7718                          * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7719                          * the only slow ring.
7720                          */
7721                         status = (work_ha_copy &
7722                                 (HA_RXMASK  << (4*LPFC_ELS_RING)));
7723                         status >>= (4*LPFC_ELS_RING);
7724                         if (status & HA_RXMASK) {
7725                                 spin_lock_irqsave(&phba->hbalock, iflag);
7726                                 control = readl(phba->HCregaddr);
7727
7728                                 lpfc_debugfs_slow_ring_trc(phba,
7729                                 "ISR slow ring:   ctl:x%x stat:x%x isrcnt:x%x",
7730                                 control, status,
7731                                 (uint32_t)phba->sli.slistat.sli_intr);
7732
7733                                 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
7734                                         lpfc_debugfs_slow_ring_trc(phba,
7735                                                 "ISR Disable ring:"
7736                                                 "pwork:x%x hawork:x%x wait:x%x",
7737                                                 phba->work_ha, work_ha_copy,
7738                                                 (uint32_t)((unsigned long)
7739                                                 &phba->work_waitq));
7740
7741                                         control &=
7742                                             ~(HC_R0INT_ENA << LPFC_ELS_RING);
7743                                         writel(control, phba->HCregaddr);
7744                                         readl(phba->HCregaddr); /* flush */
7745                                 }
7746                                 else {
7747                                         lpfc_debugfs_slow_ring_trc(phba,
7748                                                 "ISR slow ring:   pwork:"
7749                                                 "x%x hawork:x%x wait:x%x",
7750                                                 phba->work_ha, work_ha_copy,
7751                                                 (uint32_t)((unsigned long)
7752                                                 &phba->work_waitq));
7753                                 }
7754                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7755                         }
7756                 }
7757                 spin_lock_irqsave(&phba->hbalock, iflag);
7758                 if (work_ha_copy & HA_ERATT) {
7759                         lpfc_sli_read_hs(phba);
7760                         /*
7761                          * Check if there is a deferred error condition
7762                          * is active
7763                          */
7764                         if ((HS_FFER1 & phba->work_hs) &&
7765                                 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7766                                 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7767                                 phba->hba_flag |= DEFER_ERATT;
7768                                 /* Clear all interrupt enable conditions */
7769                                 writel(0, phba->HCregaddr);
7770                                 readl(phba->HCregaddr);
7771                         }
7772                 }
7773
7774                 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
7775                         pmb = phba->sli.mbox_active;
7776                         pmbox = &pmb->mb;
7777                         mbox = phba->mbox;
7778                         vport = pmb->vport;
7779
7780                         /* First check out the status word */
7781                         lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
7782                         if (pmbox->mbxOwner != OWN_HOST) {
7783                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7784                                 /*
7785                                  * Stray Mailbox Interrupt, mbxCommand <cmd>
7786                                  * mbxStatus <status>
7787                                  */
7788                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
7789                                                 LOG_SLI,
7790                                                 "(%d):0304 Stray Mailbox "
7791                                                 "Interrupt mbxCommand x%x "
7792                                                 "mbxStatus x%x\n",
7793                                                 (vport ? vport->vpi : 0),
7794                                                 pmbox->mbxCommand,
7795                                                 pmbox->mbxStatus);
7796                                 /* clear mailbox attention bit */
7797                                 work_ha_copy &= ~HA_MBATT;
7798                         } else {
7799                                 phba->sli.mbox_active = NULL;
7800                                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7801                                 phba->last_completion_time = jiffies;
7802                                 del_timer(&phba->sli.mbox_tmo);
7803                                 if (pmb->mbox_cmpl) {
7804                                         lpfc_sli_pcimem_bcopy(mbox, pmbox,
7805                                                         MAILBOX_CMD_SIZE);
7806                                 }
7807                                 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
7808                                         pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
7809
7810                                         lpfc_debugfs_disc_trc(vport,
7811                                                 LPFC_DISC_TRC_MBOX_VPORT,
7812                                                 "MBOX dflt rpi: : "
7813                                                 "status:x%x rpi:x%x",
7814                                                 (uint32_t)pmbox->mbxStatus,
7815                                                 pmbox->un.varWords[0], 0);
7816
7817                                         if (!pmbox->mbxStatus) {
7818                                                 mp = (struct lpfc_dmabuf *)
7819                                                         (pmb->context1);
7820                                                 ndlp = (struct lpfc_nodelist *)
7821                                                         pmb->context2;
7822
7823                                                 /* Reg_LOGIN of dflt RPI was
7824                                                  * successful. new lets get
7825                                                  * rid of the RPI using the
7826                                                  * same mbox buffer.
7827                                                  */
7828                                                 lpfc_unreg_login(phba,
7829                                                         vport->vpi,
7830                                                         pmbox->un.varWords[0],
7831                                                         pmb);
7832                                                 pmb->mbox_cmpl =
7833                                                         lpfc_mbx_cmpl_dflt_rpi;
7834                                                 pmb->context1 = mp;
7835                                                 pmb->context2 = ndlp;
7836                                                 pmb->vport = vport;
7837                                                 rc = lpfc_sli_issue_mbox(phba,
7838                                                                 pmb,
7839                                                                 MBX_NOWAIT);
7840                                                 if (rc != MBX_BUSY)
7841                                                         lpfc_printf_log(phba,
7842                                                         KERN_ERR,
7843                                                         LOG_MBOX | LOG_SLI,
7844                                                         "0350 rc should have"
7845                                                         "been MBX_BUSY");
7846                                                 if (rc != MBX_NOT_FINISHED)
7847                                                         goto send_current_mbox;
7848                                         }
7849                                 }
7850                                 spin_lock_irqsave(
7851                                                 &phba->pport->work_port_lock,
7852                                                 iflag);
7853                                 phba->pport->work_port_events &=
7854                                         ~WORKER_MBOX_TMO;
7855                                 spin_unlock_irqrestore(
7856                                                 &phba->pport->work_port_lock,
7857                                                 iflag);
7858                                 lpfc_mbox_cmpl_put(phba, pmb);
7859                         }
7860                 } else
7861                         spin_unlock_irqrestore(&phba->hbalock, iflag);
7862
7863                 if ((work_ha_copy & HA_MBATT) &&
7864                     (phba->sli.mbox_active == NULL)) {
7865 send_current_mbox:
7866                         /* Process next mailbox command if there is one */
7867                         do {
7868                                 rc = lpfc_sli_issue_mbox(phba, NULL,
7869                                                          MBX_NOWAIT);
7870                         } while (rc == MBX_NOT_FINISHED);
7871                         if (rc != MBX_SUCCESS)
7872                                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
7873                                                 LOG_SLI, "0349 rc should be "
7874                                                 "MBX_SUCCESS");
7875                 }
7876
7877                 spin_lock_irqsave(&phba->hbalock, iflag);
7878                 phba->work_ha |= work_ha_copy;
7879                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7880                 lpfc_worker_wake_up(phba);
7881         }
7882         return IRQ_HANDLED;
7883
7884 } /* lpfc_sli_sp_intr_handler */
7885
7886 /**
7887  * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
7888  * @irq: Interrupt number.
7889  * @dev_id: The device context pointer.
7890  *
7891  * This function is directly called from the PCI layer as an interrupt
7892  * service routine when device with SLI-3 interface spec is enabled with
7893  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
7894  * ring event in the HBA. However, when the device is enabled with either
7895  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
7896  * device-level interrupt handler. When the PCI slot is in error recovery
7897  * or the HBA is undergoing initialization, the interrupt handler will not
7898  * process the interrupt. The SCSI FCP fast-path ring event are handled in
7899  * the intrrupt context. This function is called without any lock held.
7900  * It gets the hbalock to access and update SLI data structures.
7901  *
7902  * This function returns IRQ_HANDLED when interrupt is handled else it
7903  * returns IRQ_NONE.
7904  **/
7905 irqreturn_t
7906 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
7907 {
7908         struct lpfc_hba  *phba;
7909         uint32_t ha_copy;
7910         unsigned long status;
7911         unsigned long iflag;
7912
7913         /* Get the driver's phba structure from the dev_id and
7914          * assume the HBA is not interrupting.
7915          */
7916         phba = (struct lpfc_hba *) dev_id;
7917
7918         if (unlikely(!phba))
7919                 return IRQ_NONE;
7920
7921         /*
7922          * Stuff needs to be attented to when this function is invoked as an
7923          * individual interrupt handler in MSI-X multi-message interrupt mode
7924          */
7925         if (phba->intr_type == MSIX) {
7926                 /* Check device state for handling interrupt */
7927                 if (lpfc_intr_state_check(phba))
7928                         return IRQ_NONE;
7929                 /* Need to read HA REG for FCP ring and other ring events */
7930                 ha_copy = readl(phba->HAregaddr);
7931                 /* Clear up only attention source related to fast-path */
7932                 spin_lock_irqsave(&phba->hbalock, iflag);
7933                 /*
7934                  * If there is deferred error attention, do not check for
7935                  * any interrupt.
7936                  */
7937                 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7938                         spin_unlock_irqrestore(&phba->hbalock, iflag);
7939                         return IRQ_NONE;
7940                 }
7941                 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
7942                         phba->HAregaddr);
7943                 readl(phba->HAregaddr); /* flush */
7944                 spin_unlock_irqrestore(&phba->hbalock, iflag);
7945         } else
7946                 ha_copy = phba->ha_copy;
7947
7948         /*
7949          * Process all events on FCP ring. Take the optimized path for FCP IO.
7950          */
7951         ha_copy &= ~(phba->work_ha_mask);
7952
7953         status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
7954         status >>= (4*LPFC_FCP_RING);
7955         if (status & HA_RXMASK)
7956                 lpfc_sli_handle_fast_ring_event(phba,
7957                                                 &phba->sli.ring[LPFC_FCP_RING],
7958                                                 status);
7959
7960         if (phba->cfg_multi_ring_support == 2) {
7961                 /*
7962                  * Process all events on extra ring. Take the optimized path
7963                  * for extra ring IO.
7964                  */
7965                 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
7966                 status >>= (4*LPFC_EXTRA_RING);
7967                 if (status & HA_RXMASK) {
7968                         lpfc_sli_handle_fast_ring_event(phba,
7969                                         &phba->sli.ring[LPFC_EXTRA_RING],
7970                                         status);
7971                 }
7972         }
7973         return IRQ_HANDLED;
7974 }  /* lpfc_sli_fp_intr_handler */
7975
7976 /**
7977  * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
7978  * @irq: Interrupt number.
7979  * @dev_id: The device context pointer.
7980  *
7981  * This function is the HBA device-level interrupt handler to device with
7982  * SLI-3 interface spec, called from the PCI layer when either MSI or
7983  * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
7984  * requires driver attention. This function invokes the slow-path interrupt
7985  * attention handling function and fast-path interrupt attention handling
7986  * function in turn to process the relevant HBA attention events. This
7987  * function is called without any lock held. It gets the hbalock to access
7988  * and update SLI data structures.
7989  *
7990  * This function returns IRQ_HANDLED when interrupt is handled, else it
7991  * returns IRQ_NONE.
7992  **/
7993 irqreturn_t
7994 lpfc_sli_intr_handler(int irq, void *dev_id)
7995 {
7996         struct lpfc_hba  *phba;
7997         irqreturn_t sp_irq_rc, fp_irq_rc;
7998         unsigned long status1, status2;
7999
8000         /*
8001          * Get the driver's phba structure from the dev_id and
8002          * assume the HBA is not interrupting.
8003          */
8004         phba = (struct lpfc_hba *) dev_id;
8005
8006         if (unlikely(!phba))
8007                 return IRQ_NONE;
8008
8009         /* Check device state for handling interrupt */
8010         if (lpfc_intr_state_check(phba))
8011                 return IRQ_NONE;
8012
8013         spin_lock(&phba->hbalock);
8014         phba->ha_copy = readl(phba->HAregaddr);
8015         if (unlikely(!phba->ha_copy)) {
8016                 spin_unlock(&phba->hbalock);
8017                 return IRQ_NONE;
8018         } else if (phba->ha_copy & HA_ERATT) {
8019                 if (phba->hba_flag & HBA_ERATT_HANDLED)
8020                         /* ERATT polling has handled ERATT */
8021                         phba->ha_copy &= ~HA_ERATT;
8022                 else
8023                         /* Indicate interrupt handler handles ERATT */
8024                         phba->hba_flag |= HBA_ERATT_HANDLED;
8025         }
8026
8027         /*
8028          * If there is deferred error attention, do not check for any interrupt.
8029          */
8030         if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8031                 spin_unlock_irq(&phba->hbalock);
8032                 return IRQ_NONE;
8033         }
8034
8035         /* Clear attention sources except link and error attentions */
8036         writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8037         readl(phba->HAregaddr); /* flush */
8038         spin_unlock(&phba->hbalock);
8039
8040         /*
8041          * Invokes slow-path host attention interrupt handling as appropriate.
8042          */
8043
8044         /* status of events with mailbox and link attention */
8045         status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8046
8047         /* status of events with ELS ring */
8048         status2 = (phba->ha_copy & (HA_RXMASK  << (4*LPFC_ELS_RING)));
8049         status2 >>= (4*LPFC_ELS_RING);
8050
8051         if (status1 || (status2 & HA_RXMASK))
8052                 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
8053         else
8054                 sp_irq_rc = IRQ_NONE;
8055
8056         /*
8057          * Invoke fast-path host attention interrupt handling as appropriate.
8058          */
8059
8060         /* status of events with FCP ring */
8061         status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8062         status1 >>= (4*LPFC_FCP_RING);
8063
8064         /* status of events with extra ring */
8065         if (phba->cfg_multi_ring_support == 2) {
8066                 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8067                 status2 >>= (4*LPFC_EXTRA_RING);
8068         } else
8069                 status2 = 0;
8070
8071         if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
8072                 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
8073         else
8074                 fp_irq_rc = IRQ_NONE;
8075
8076         /* Return device-level interrupt handling status */
8077         return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
8078 }  /* lpfc_sli_intr_handler */
8079
8080 /**
8081  * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8082  * @phba: pointer to lpfc hba data structure.
8083  *
8084  * This routine is invoked by the worker thread to process all the pending
8085  * SLI4 FCP abort XRI events.
8086  **/
8087 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8088 {
8089         struct lpfc_cq_event *cq_event;
8090
8091         /* First, declare the fcp xri abort event has been handled */
8092         spin_lock_irq(&phba->hbalock);
8093         phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8094         spin_unlock_irq(&phba->hbalock);
8095         /* Now, handle all the fcp xri abort events */
8096         while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8097                 /* Get the first event from the head of the event queue */
8098                 spin_lock_irq(&phba->hbalock);
8099                 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8100                                  cq_event, struct lpfc_cq_event, list);
8101                 spin_unlock_irq(&phba->hbalock);
8102                 /* Notify aborted XRI for FCP work queue */
8103                 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8104                 /* Free the event processed back to the free pool */
8105                 lpfc_sli4_cq_event_release(phba, cq_event);
8106         }
8107 }
8108
8109 /**
8110  * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8111  * @phba: pointer to lpfc hba data structure.
8112  *
8113  * This routine is invoked by the worker thread to process all the pending
8114  * SLI4 els abort xri events.
8115  **/
8116 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8117 {
8118         struct lpfc_cq_event *cq_event;
8119
8120         /* First, declare the els xri abort event has been handled */
8121         spin_lock_irq(&phba->hbalock);
8122         phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8123         spin_unlock_irq(&phba->hbalock);
8124         /* Now, handle all the els xri abort events */
8125         while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8126                 /* Get the first event from the head of the event queue */
8127                 spin_lock_irq(&phba->hbalock);
8128                 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8129                                  cq_event, struct lpfc_cq_event, list);
8130                 spin_unlock_irq(&phba->hbalock);
8131                 /* Notify aborted XRI for ELS work queue */
8132                 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8133                 /* Free the event processed back to the free pool */
8134                 lpfc_sli4_cq_event_release(phba, cq_event);
8135         }
8136 }
8137
8138 static void
8139 lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8140                               struct lpfc_iocbq *pIocbOut,
8141                               struct lpfc_wcqe_complete *wcqe)
8142 {
8143         size_t offset = offsetof(struct lpfc_iocbq, iocb);
8144
8145         memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8146                sizeof(struct lpfc_iocbq) - offset);
8147         memset(&pIocbIn->sli4_info, 0,
8148                sizeof(struct lpfc_sli4_rspiocb_info));
8149         /* Map WCQE parameters into irspiocb parameters */
8150         pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8151         if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8152                 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8153                         pIocbIn->iocb.un.fcpi.fcpi_parm =
8154                                         pIocbOut->iocb.un.fcpi.fcpi_parm -
8155                                         wcqe->total_data_placed;
8156                 else
8157                         pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8158         else
8159                 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8160         /* Load in additional WCQE parameters */
8161         pIocbIn->sli4_info.hw_status = bf_get(lpfc_wcqe_c_hw_status, wcqe);
8162         pIocbIn->sli4_info.bfield = 0;
8163         if (bf_get(lpfc_wcqe_c_xb, wcqe))
8164                 pIocbIn->sli4_info.bfield |= LPFC_XB;
8165         if (bf_get(lpfc_wcqe_c_pv, wcqe)) {
8166                 pIocbIn->sli4_info.bfield |= LPFC_PV;
8167                 pIocbIn->sli4_info.priority =
8168                                         bf_get(lpfc_wcqe_c_priority, wcqe);
8169         }
8170 }
8171
8172 /**
8173  * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8174  * @phba: Pointer to HBA context object.
8175  * @wcqe: Pointer to work-queue completion queue entry.
8176  *
8177  * This routine handles an ELS work-queue completion event.
8178  *
8179  * Return: true if work posted to worker thread, otherwise false.
8180  **/
8181 static bool
8182 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8183                              struct lpfc_wcqe_complete *wcqe)
8184 {
8185         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8186         struct lpfc_iocbq *cmdiocbq;
8187         struct lpfc_iocbq *irspiocbq;
8188         unsigned long iflags;
8189         bool workposted = false;
8190
8191         spin_lock_irqsave(&phba->hbalock, iflags);
8192         pring->stats.iocb_event++;
8193         /* Look up the ELS command IOCB and create pseudo response IOCB */
8194         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8195                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8196         spin_unlock_irqrestore(&phba->hbalock, iflags);
8197
8198         if (unlikely(!cmdiocbq)) {
8199                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8200                                 "0386 ELS complete with no corresponding "
8201                                 "cmdiocb: iotag (%d)\n",
8202                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8203                 return workposted;
8204         }
8205
8206         /* Fake the irspiocbq and copy necessary response information */
8207         irspiocbq = lpfc_sli_get_iocbq(phba);
8208         if (!irspiocbq) {
8209                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8210                                 "0387 Failed to allocate an iocbq\n");
8211                 return workposted;
8212         }
8213         lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8214
8215         /* Add the irspiocb to the response IOCB work list */
8216         spin_lock_irqsave(&phba->hbalock, iflags);
8217         list_add_tail(&irspiocbq->list, &phba->sli4_hba.sp_rspiocb_work_queue);
8218         /* Indicate ELS ring attention */
8219         phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
8220         spin_unlock_irqrestore(&phba->hbalock, iflags);
8221         workposted = true;
8222
8223         return workposted;
8224 }
8225
8226 /**
8227  * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8228  * @phba: Pointer to HBA context object.
8229  * @wcqe: Pointer to work-queue completion queue entry.
8230  *
8231  * This routine handles slow-path WQ entry comsumed event by invoking the
8232  * proper WQ release routine to the slow-path WQ.
8233  **/
8234 static void
8235 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8236                              struct lpfc_wcqe_release *wcqe)
8237 {
8238         /* Check for the slow-path ELS work queue */
8239         if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8240                 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8241                                      bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8242         else
8243                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8244                                 "2579 Slow-path wqe consume event carries "
8245                                 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8246                                 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8247                                 phba->sli4_hba.els_wq->queue_id);
8248 }
8249
8250 /**
8251  * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8252  * @phba: Pointer to HBA context object.
8253  * @cq: Pointer to a WQ completion queue.
8254  * @wcqe: Pointer to work-queue completion queue entry.
8255  *
8256  * This routine handles an XRI abort event.
8257  *
8258  * Return: true if work posted to worker thread, otherwise false.
8259  **/
8260 static bool
8261 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8262                                    struct lpfc_queue *cq,
8263                                    struct sli4_wcqe_xri_aborted *wcqe)
8264 {
8265         bool workposted = false;
8266         struct lpfc_cq_event *cq_event;
8267         unsigned long iflags;
8268
8269         /* Allocate a new internal CQ_EVENT entry */
8270         cq_event = lpfc_sli4_cq_event_alloc(phba);
8271         if (!cq_event) {
8272                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8273                                 "0602 Failed to allocate CQ_EVENT entry\n");
8274                 return false;
8275         }
8276
8277         /* Move the CQE into the proper xri abort event list */
8278         memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8279         switch (cq->subtype) {
8280         case LPFC_FCP:
8281                 spin_lock_irqsave(&phba->hbalock, iflags);
8282                 list_add_tail(&cq_event->list,
8283                               &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8284                 /* Set the fcp xri abort event flag */
8285                 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8286                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8287                 workposted = true;
8288                 break;
8289         case LPFC_ELS:
8290                 spin_lock_irqsave(&phba->hbalock, iflags);
8291                 list_add_tail(&cq_event->list,
8292                               &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8293                 /* Set the els xri abort event flag */
8294                 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8295                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8296                 workposted = true;
8297                 break;
8298         default:
8299                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8300                                 "0603 Invalid work queue CQE subtype (x%x)\n",
8301                                 cq->subtype);
8302                 workposted = false;
8303                 break;
8304         }
8305         return workposted;
8306 }
8307
8308 /**
8309  * lpfc_sli4_sp_handle_wcqe - Process a work-queue completion queue entry
8310  * @phba: Pointer to HBA context object.
8311  * @cq: Pointer to the completion queue.
8312  * @wcqe: Pointer to a completion queue entry.
8313  *
8314  * This routine process a slow-path work-queue completion queue entry.
8315  *
8316  * Return: true if work posted to worker thread, otherwise false.
8317  **/
8318 static bool
8319 lpfc_sli4_sp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8320                          struct lpfc_cqe *cqe)
8321 {
8322         struct lpfc_wcqe_complete wcqe;
8323         bool workposted = false;
8324
8325         /* Copy the work queue CQE and convert endian order if needed */
8326         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8327
8328         /* Check and process for different type of WCQE and dispatch */
8329         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8330         case CQE_CODE_COMPL_WQE:
8331                 /* Process the WQ complete event */
8332                 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8333                                         (struct lpfc_wcqe_complete *)&wcqe);
8334                 break;
8335         case CQE_CODE_RELEASE_WQE:
8336                 /* Process the WQ release event */
8337                 lpfc_sli4_sp_handle_rel_wcqe(phba,
8338                                         (struct lpfc_wcqe_release *)&wcqe);
8339                 break;
8340         case CQE_CODE_XRI_ABORTED:
8341                 /* Process the WQ XRI abort event */
8342                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8343                                         (struct sli4_wcqe_xri_aborted *)&wcqe);
8344                 break;
8345         default:
8346                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8347                                 "0388 Not a valid WCQE code: x%x\n",
8348                                 bf_get(lpfc_wcqe_c_code, &wcqe));
8349                 break;
8350         }
8351         return workposted;
8352 }
8353
8354 /**
8355  * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8356  * @phba: Pointer to HBA context object.
8357  * @rcqe: Pointer to receive-queue completion queue entry.
8358  *
8359  * This routine process a receive-queue completion queue entry.
8360  *
8361  * Return: true if work posted to worker thread, otherwise false.
8362  **/
8363 static bool
8364 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8365 {
8366         struct lpfc_rcqe rcqe;
8367         bool workposted = false;
8368         struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8369         struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8370         struct hbq_dmabuf *dma_buf;
8371         uint32_t status;
8372         unsigned long iflags;
8373
8374         /* Copy the receive queue CQE and convert endian order if needed */
8375         lpfc_sli_pcimem_bcopy(cqe, &rcqe, sizeof(struct lpfc_rcqe));
8376         lpfc_sli4_rq_release(hrq, drq);
8377         if (bf_get(lpfc_rcqe_code, &rcqe) != CQE_CODE_RECEIVE)
8378                 goto out;
8379         if (bf_get(lpfc_rcqe_rq_id, &rcqe) != hrq->queue_id)
8380                 goto out;
8381
8382         status = bf_get(lpfc_rcqe_status, &rcqe);
8383         switch (status) {
8384         case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8385                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8386                                 "2537 Receive Frame Truncated!!\n");
8387         case FC_STATUS_RQ_SUCCESS:
8388                 spin_lock_irqsave(&phba->hbalock, iflags);
8389                 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8390                 if (!dma_buf) {
8391                         spin_unlock_irqrestore(&phba->hbalock, iflags);
8392                         goto out;
8393                 }
8394                 memcpy(&dma_buf->rcqe, &rcqe, sizeof(rcqe));
8395                 /* save off the frame for the word thread to process */
8396                 list_add_tail(&dma_buf->dbuf.list, &phba->rb_pend_list);
8397                 /* Frame received */
8398                 phba->hba_flag |= HBA_RECEIVE_BUFFER;
8399                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8400                 workposted = true;
8401                 break;
8402         case FC_STATUS_INSUFF_BUF_NEED_BUF:
8403         case FC_STATUS_INSUFF_BUF_FRM_DISC:
8404                 /* Post more buffers if possible */
8405                 spin_lock_irqsave(&phba->hbalock, iflags);
8406                 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8407                 spin_unlock_irqrestore(&phba->hbalock, iflags);
8408                 workposted = true;
8409                 break;
8410         }
8411 out:
8412         return workposted;
8413
8414 }
8415
8416 /**
8417  * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8418  * @phba: Pointer to HBA context object.
8419  * @eqe: Pointer to fast-path event queue entry.
8420  *
8421  * This routine process a event queue entry from the slow-path event queue.
8422  * It will check the MajorCode and MinorCode to determine this is for a
8423  * completion event on a completion queue, if not, an error shall be logged
8424  * and just return. Otherwise, it will get to the corresponding completion
8425  * queue and process all the entries on that completion queue, rearm the
8426  * completion queue, and then return.
8427  *
8428  **/
8429 static void
8430 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8431 {
8432         struct lpfc_queue *cq = NULL, *childq, *speq;
8433         struct lpfc_cqe *cqe;
8434         bool workposted = false;
8435         int ecount = 0;
8436         uint16_t cqid;
8437
8438         if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8439             bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8440                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8441                                 "0359 Not a valid slow-path completion "
8442                                 "event: majorcode=x%x, minorcode=x%x\n",
8443                                 bf_get(lpfc_eqe_major_code, eqe),
8444                                 bf_get(lpfc_eqe_minor_code, eqe));
8445                 return;
8446         }
8447
8448         /* Get the reference to the corresponding CQ */
8449         cqid = bf_get(lpfc_eqe_resource_id, eqe);
8450
8451         /* Search for completion queue pointer matching this cqid */
8452         speq = phba->sli4_hba.sp_eq;
8453         list_for_each_entry(childq, &speq->child_list, list) {
8454                 if (childq->queue_id == cqid) {
8455                         cq = childq;
8456                         break;
8457                 }
8458         }
8459         if (unlikely(!cq)) {
8460                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8461                                 "0365 Slow-path CQ identifier (%d) does "
8462                                 "not exist\n", cqid);
8463                 return;
8464         }
8465
8466         /* Process all the entries to the CQ */
8467         switch (cq->type) {
8468         case LPFC_MCQ:
8469                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8470                         workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8471                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8472                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8473                 }
8474                 break;
8475         case LPFC_WCQ:
8476                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8477                         workposted |= lpfc_sli4_sp_handle_wcqe(phba, cq, cqe);
8478                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8479                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8480                 }
8481                 break;
8482         case LPFC_RCQ:
8483                 while ((cqe = lpfc_sli4_cq_get(cq))) {
8484                         workposted |= lpfc_sli4_sp_handle_rcqe(phba, cqe);
8485                         if (!(++ecount % LPFC_GET_QE_REL_INT))
8486                                 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8487                 }
8488                 break;
8489         default:
8490                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8491                                 "0370 Invalid completion queue type (%d)\n",
8492                                 cq->type);
8493                 return;
8494         }
8495
8496         /* Catch the no cq entry condition, log an error */
8497         if (unlikely(ecount == 0))
8498                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8499                                 "0371 No entry from the CQ: identifier "
8500                                 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8501
8502         /* In any case, flash and re-arm the RCQ */
8503         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8504
8505         /* wake up worker thread if there are works to be done */
8506         if (workposted)
8507                 lpfc_worker_wake_up(phba);
8508 }
8509
8510 /**
8511  * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8512  * @eqe: Pointer to fast-path completion queue entry.
8513  *
8514  * This routine process a fast-path work queue completion entry from fast-path
8515  * event queue for FCP command response completion.
8516  **/
8517 static void
8518 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8519                              struct lpfc_wcqe_complete *wcqe)
8520 {
8521         struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8522         struct lpfc_iocbq *cmdiocbq;
8523         struct lpfc_iocbq irspiocbq;
8524         unsigned long iflags;
8525
8526         spin_lock_irqsave(&phba->hbalock, iflags);
8527         pring->stats.iocb_event++;
8528         spin_unlock_irqrestore(&phba->hbalock, iflags);
8529
8530         /* Check for response status */
8531         if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8532                 /* If resource errors reported from HBA, reduce queue
8533                  * depth of the SCSI device.
8534                  */
8535                 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8536                      IOSTAT_LOCAL_REJECT) &&
8537                     (wcqe->parameter == IOERR_NO_RESOURCES)) {
8538                         phba->lpfc_rampdown_queue_depth(phba);
8539                 }
8540                 /* Log the error status */
8541                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8542                                 "0373 FCP complete error: status=x%x, "
8543                                 "hw_status=x%x, total_data_specified=%d, "
8544                                 "parameter=x%x, word3=x%x\n",
8545                                 bf_get(lpfc_wcqe_c_status, wcqe),
8546                                 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8547                                 wcqe->total_data_placed, wcqe->parameter,
8548                                 wcqe->word3);
8549         }
8550
8551         /* Look up the FCP command IOCB and create pseudo response IOCB */
8552         spin_lock_irqsave(&phba->hbalock, iflags);
8553         cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8554                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8555         spin_unlock_irqrestore(&phba->hbalock, iflags);
8556         if (unlikely(!cmdiocbq)) {
8557                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8558                                 "0374 FCP complete with no corresponding "
8559                                 "cmdiocb: iotag (%d)\n",
8560                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8561                 return;
8562         }
8563         if (unlikely(!cmdiocbq->iocb_cmpl)) {
8564                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8565                                 "0375 FCP cmdiocb not callback function "
8566                                 "iotag: (%d)\n",
8567                                 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8568                 return;
8569         }
8570
8571         /* Fake the irspiocb and copy necessary response information */
8572         lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8573
8574         /* Pass the cmd_iocb and the rsp state to the upper layer */
8575         (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8576 }
8577
8578 /**
8579  * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8580  * @phba: Pointer to HBA context object.
8581  * @cq: Pointer to completion queue.
8582  * @wcqe: Pointer to work-queue completion queue entry.
8583  *
8584  * This routine handles an fast-path WQ entry comsumed event by invoking the
8585  * proper WQ release routine to the slow-path WQ.
8586  **/
8587 static void
8588 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8589                              struct lpfc_wcqe_release *wcqe)
8590 {
8591         struct lpfc_queue *childwq;
8592         bool wqid_matched = false;
8593         uint16_t fcp_wqid;
8594
8595         /* Check for fast-path FCP work queue release */
8596         fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
8597         list_for_each_entry(childwq, &cq->child_list, list) {
8598                 if (childwq->queue_id == fcp_wqid) {
8599                         lpfc_sli4_wq_release(childwq,
8600                                         bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8601                         wqid_matched = true;
8602                         break;
8603                 }
8604         }
8605         /* Report warning log message if no match found */
8606         if (wqid_matched != true)
8607                 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8608                                 "2580 Fast-path wqe consume event carries "
8609                                 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
8610 }
8611
8612 /**
8613  * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
8614  * @cq: Pointer to the completion queue.
8615  * @eqe: Pointer to fast-path completion queue entry.
8616  *
8617  * This routine process a fast-path work queue completion entry from fast-path
8618  * event queue for FCP command response completion.
8619  **/
8620 static int
8621 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8622                          struct lpfc_cqe *cqe)
8623 {
8624         struct lpfc_wcqe_release wcqe;
8625         bool workposted = false;
8626
8627         /* Copy the work queue CQE and convert endian order if needed */
8628         lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8629
8630         /* Check and process for different type of WCQE and dispatch */
8631         switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8632         case CQE_CODE_COMPL_WQE:
8633                 /* Process the WQ complete event */
8634                 lpfc_sli4_fp_handle_fcp_wcqe(phba,
8635                                 (struct lpfc_wcqe_complete *)&wcqe);
8636                 break;
8637         case CQE_CODE_RELEASE_WQE:
8638                 /* Process the WQ release event */
8639                 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
8640                                 (struct lpfc_wcqe_release *)&wcqe);
8641                 break;
8642         case CQE_CODE_XRI_ABORTED:
8643                 /* Process the WQ XRI abort event */
8644                 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8645                                 (struct sli4_wcqe_xri_aborted *)&wcqe);
8646                 break;
8647         default:
8648                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8649                                 "0144 Not a valid WCQE code: x%x\n",
8650                                 bf_get(lpfc_wcqe_c_code, &wcqe));
8651                 break;
8652         }
8653         return workposted;
8654 }
8655
8656 /**
8657  * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
8658  * @phba: Pointer to HBA context object.
8659  * @eqe: Pointer to fast-path event queue entry.
8660  *
8661  * This routine process a event queue entry from the fast-path event queue.
8662  * It will check the MajorCode and MinorCode to determine this is for a
8663  * completion event on a completion queue, if not, an error shall be logged
8664  * and just return. Otherwise, it will get to the corresponding completion
8665  * queue and process all the entries on the completion queue, rearm the
8666  * completion queue, and then return.
8667  **/
8668 static void
8669 lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
8670                         uint32_t fcp_cqidx)
8671 {
8672         struct lpfc_queue *cq;
8673         struct lpfc_cqe *cqe;
8674         bool workposted = false;
8675         uint16_t cqid;
8676         int ecount = 0;
8677
8678         if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
8679             unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
8680                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8681                                 "0366 Not a valid fast-path completion "
8682                                 "event: majorcode=x%x, minorcode=x%x\n",
8683                                 bf_get(lpfc_eqe_major_code, eqe),
8684                                 bf_get(lpfc_eqe_minor_code, eqe));
8685                 return;
8686         }
8687
8688         cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
8689         if (unlikely(!cq)) {
8690                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8691                                 "0367 Fast-path completion queue does not "
8692                                 "exist\n");
8693                 return;
8694         }
8695
8696         /* Get the reference to the corresponding CQ */
8697         cqid = bf_get(lpfc_eqe_resource_id, eqe);
8698         if (unlikely(cqid != cq->queue_id)) {
8699                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8700                                 "0368 Miss-matched fast-path completion "
8701                                 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
8702                                 cqid, cq->queue_id);
8703                 return;
8704         }
8705
8706         /* Process all the entries to the CQ */
8707         while ((cqe = lpfc_sli4_cq_get(cq))) {
8708                 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
8709                 if (!(++ecount % LPFC_GET_QE_REL_INT))
8710                         lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8711         }
8712
8713         /* Catch the no cq entry condition */
8714         if (unlikely(ecount == 0))
8715                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8716                                 "0369 No entry from fast-path completion "
8717                                 "queue fcpcqid=%d\n", cq->queue_id);
8718
8719         /* In any case, flash and re-arm the CQ */
8720         lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8721
8722         /* wake up worker thread if there are works to be done */
8723         if (workposted)
8724                 lpfc_worker_wake_up(phba);
8725 }
8726
8727 static void
8728 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
8729 {
8730         struct lpfc_eqe *eqe;
8731
8732         /* walk all the EQ entries and drop on the floor */
8733         while ((eqe = lpfc_sli4_eq_get(eq)))
8734                 ;
8735
8736         /* Clear and re-arm the EQ */
8737         lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
8738 }
8739
8740 /**
8741  * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
8742  * @irq: Interrupt number.
8743  * @dev_id: The device context pointer.
8744  *
8745  * This function is directly called from the PCI layer as an interrupt
8746  * service routine when device with SLI-4 interface spec is enabled with
8747  * MSI-X multi-message interrupt mode and there are slow-path events in
8748  * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
8749  * interrupt mode, this function is called as part of the device-level
8750  * interrupt handler. When the PCI slot is in error recovery or the HBA is
8751  * undergoing initialization, the interrupt handler will not process the
8752  * interrupt. The link attention and ELS ring attention events are handled
8753  * by the worker thread. The interrupt handler signals the worker thread
8754  * and returns for these events. This function is called without any lock
8755  * held. It gets the hbalock to access and update SLI data structures.
8756  *
8757  * This function returns IRQ_HANDLED when interrupt is handled else it
8758  * returns IRQ_NONE.
8759  **/
8760 irqreturn_t
8761 lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
8762 {
8763         struct lpfc_hba *phba;
8764         struct lpfc_queue *speq;
8765         struct lpfc_eqe *eqe;
8766         unsigned long iflag;
8767         int ecount = 0;
8768
8769         /*
8770          * Get the driver's phba structure from the dev_id
8771          */
8772         phba = (struct lpfc_hba *)dev_id;
8773
8774         if (unlikely(!phba))
8775                 return IRQ_NONE;
8776
8777         /* Get to the EQ struct associated with this vector */
8778         speq = phba->sli4_hba.sp_eq;
8779
8780         /* Check device state for handling interrupt */
8781         if (unlikely(lpfc_intr_state_check(phba))) {
8782                 /* Check again for link_state with lock held */
8783                 spin_lock_irqsave(&phba->hbalock, iflag);
8784                 if (phba->link_state < LPFC_LINK_DOWN)
8785                         /* Flush, clear interrupt, and rearm the EQ */
8786                         lpfc_sli4_eq_flush(phba, speq);
8787                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8788                 return IRQ_NONE;
8789         }
8790
8791         /*
8792          * Process all the event on FCP slow-path EQ
8793          */
8794         while ((eqe = lpfc_sli4_eq_get(speq))) {
8795                 lpfc_sli4_sp_handle_eqe(phba, eqe);
8796                 if (!(++ecount % LPFC_GET_QE_REL_INT))
8797                         lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
8798         }
8799
8800         /* Always clear and re-arm the slow-path EQ */
8801         lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
8802
8803         /* Catch the no cq entry condition */
8804         if (unlikely(ecount == 0)) {
8805                 if (phba->intr_type == MSIX)
8806                         /* MSI-X treated interrupt served as no EQ share INT */
8807                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8808                                         "0357 MSI-X interrupt with no EQE\n");
8809                 else
8810                         /* Non MSI-X treated on interrupt as EQ share INT */
8811                         return IRQ_NONE;
8812         }
8813
8814         return IRQ_HANDLED;
8815 } /* lpfc_sli4_sp_intr_handler */
8816
8817 /**
8818  * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
8819  * @irq: Interrupt number.
8820  * @dev_id: The device context pointer.
8821  *
8822  * This function is directly called from the PCI layer as an interrupt
8823  * service routine when device with SLI-4 interface spec is enabled with
8824  * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8825  * ring event in the HBA. However, when the device is enabled with either
8826  * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8827  * device-level interrupt handler. When the PCI slot is in error recovery
8828  * or the HBA is undergoing initialization, the interrupt handler will not
8829  * process the interrupt. The SCSI FCP fast-path ring event are handled in
8830  * the intrrupt context. This function is called without any lock held.
8831  * It gets the hbalock to access and update SLI data structures. Note that,
8832  * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
8833  * equal to that of FCP CQ index.
8834  *
8835  * This function returns IRQ_HANDLED when interrupt is handled else it
8836  * returns IRQ_NONE.
8837  **/
8838 irqreturn_t
8839 lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
8840 {
8841         struct lpfc_hba *phba;
8842         struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
8843         struct lpfc_queue *fpeq;
8844         struct lpfc_eqe *eqe;
8845         unsigned long iflag;
8846         int ecount = 0;
8847         uint32_t fcp_eqidx;
8848
8849         /* Get the driver's phba structure from the dev_id */
8850         fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
8851         phba = fcp_eq_hdl->phba;
8852         fcp_eqidx = fcp_eq_hdl->idx;
8853
8854         if (unlikely(!phba))
8855                 return IRQ_NONE;
8856
8857         /* Get to the EQ struct associated with this vector */
8858         fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
8859
8860         /* Check device state for handling interrupt */
8861         if (unlikely(lpfc_intr_state_check(phba))) {
8862                 /* Check again for link_state with lock held */
8863                 spin_lock_irqsave(&phba->hbalock, iflag);
8864                 if (phba->link_state < LPFC_LINK_DOWN)
8865                         /* Flush, clear interrupt, and rearm the EQ */
8866                         lpfc_sli4_eq_flush(phba, fpeq);
8867                 spin_unlock_irqrestore(&phba->hbalock, iflag);
8868                 return IRQ_NONE;
8869         }
8870
8871         /*
8872          * Process all the event on FCP fast-path EQ
8873          */
8874         while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8875                 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
8876                 if (!(++ecount % LPFC_GET_QE_REL_INT))
8877                         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
8878         }
8879
8880         /* Always clear and re-arm the fast-path EQ */
8881         lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
8882
8883         if (unlikely(ecount == 0)) {
8884                 if (phba->intr_type == MSIX)
8885                         /* MSI-X treated interrupt served as no EQ share INT */
8886                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8887                                         "0358 MSI-X interrupt with no EQE\n");
8888                 else
8889                         /* Non MSI-X treated on interrupt as EQ share INT */
8890                         return IRQ_NONE;
8891         }
8892
8893         return IRQ_HANDLED;
8894 } /* lpfc_sli4_fp_intr_handler */
8895
8896 /**
8897  * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
8898  * @irq: Interrupt number.
8899  * @dev_id: The device context pointer.
8900  *
8901  * This function is the device-level interrupt handler to device with SLI-4
8902  * interface spec, called from the PCI layer when either MSI or Pin-IRQ
8903  * interrupt mode is enabled and there is an event in the HBA which requires
8904  * driver attention. This function invokes the slow-path interrupt attention
8905  * handling function and fast-path interrupt attention handling function in
8906  * turn to process the relevant HBA attention events. This function is called
8907  * without any lock held. It gets the hbalock to access and update SLI data
8908  * structures.
8909  *
8910  * This function returns IRQ_HANDLED when interrupt is handled, else it
8911  * returns IRQ_NONE.
8912  **/
8913 irqreturn_t
8914 lpfc_sli4_intr_handler(int irq, void *dev_id)
8915 {
8916         struct lpfc_hba  *phba;
8917         irqreturn_t sp_irq_rc, fp_irq_rc;
8918         bool fp_handled = false;
8919         uint32_t fcp_eqidx;
8920
8921         /* Get the driver's phba structure from the dev_id */
8922         phba = (struct lpfc_hba *)dev_id;
8923
8924         if (unlikely(!phba))
8925                 return IRQ_NONE;
8926
8927         /*
8928          * Invokes slow-path host attention interrupt handling as appropriate.
8929          */
8930         sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
8931
8932         /*
8933          * Invoke fast-path host attention interrupt handling as appropriate.
8934          */
8935         for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
8936                 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
8937                                         &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
8938                 if (fp_irq_rc == IRQ_HANDLED)
8939                         fp_handled |= true;
8940         }
8941
8942         return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
8943 } /* lpfc_sli4_intr_handler */
8944
8945 /**
8946  * lpfc_sli4_queue_free - free a queue structure and associated memory
8947  * @queue: The queue structure to free.
8948  *
8949  * This function frees a queue structure and the DMAable memeory used for
8950  * the host resident queue. This function must be called after destroying the
8951  * queue on the HBA.
8952  **/
8953 void
8954 lpfc_sli4_queue_free(struct lpfc_queue *queue)
8955 {
8956         struct lpfc_dmabuf *dmabuf;
8957
8958         if (!queue)
8959                 return;
8960
8961         while (!list_empty(&queue->page_list)) {
8962                 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
8963                                  list);
8964                 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
8965                                   dmabuf->virt, dmabuf->phys);
8966                 kfree(dmabuf);
8967         }
8968         kfree(queue);
8969         return;
8970 }
8971
8972 /**
8973  * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
8974  * @phba: The HBA that this queue is being created on.
8975  * @entry_size: The size of each queue entry for this queue.
8976  * @entry count: The number of entries that this queue will handle.
8977  *
8978  * This function allocates a queue structure and the DMAable memory used for
8979  * the host resident queue. This function must be called before creating the
8980  * queue on the HBA.
8981  **/
8982 struct lpfc_queue *
8983 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
8984                       uint32_t entry_count)
8985 {
8986         struct lpfc_queue *queue;
8987         struct lpfc_dmabuf *dmabuf;
8988         int x, total_qe_count;
8989         void *dma_pointer;
8990
8991
8992         queue = kzalloc(sizeof(struct lpfc_queue) +
8993                         (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
8994         if (!queue)
8995                 return NULL;
8996         queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
8997         INIT_LIST_HEAD(&queue->list);
8998         INIT_LIST_HEAD(&queue->page_list);
8999         INIT_LIST_HEAD(&queue->child_list);
9000         for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9001                 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9002                 if (!dmabuf)
9003                         goto out_fail;
9004                 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9005                                                   PAGE_SIZE, &dmabuf->phys,
9006                                                   GFP_KERNEL);
9007                 if (!dmabuf->virt) {
9008                         kfree(dmabuf);
9009                         goto out_fail;
9010                 }
9011                 dmabuf->buffer_tag = x;
9012                 list_add_tail(&dmabuf->list, &queue->page_list);
9013                 /* initialize queue's entry array */
9014                 dma_pointer = dmabuf->virt;
9015                 for (; total_qe_count < entry_count &&
9016                      dma_pointer < (PAGE_SIZE + dmabuf->virt);
9017                      total_qe_count++, dma_pointer += entry_size) {
9018                         queue->qe[total_qe_count].address = dma_pointer;
9019                 }
9020         }
9021         queue->entry_size = entry_size;
9022         queue->entry_count = entry_count;
9023         queue->phba = phba;
9024
9025         return queue;
9026 out_fail:
9027         lpfc_sli4_queue_free(queue);
9028         return NULL;
9029 }
9030
9031 /**
9032  * lpfc_eq_create - Create an Event Queue on the HBA
9033  * @phba: HBA structure that indicates port to create a queue on.
9034  * @eq: The queue structure to use to create the event queue.
9035  * @imax: The maximum interrupt per second limit.
9036  *
9037  * This function creates an event queue, as detailed in @eq, on a port,
9038  * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9039  *
9040  * The @phba struct is used to send mailbox command to HBA. The @eq struct
9041  * is used to get the entry count and entry size that are necessary to
9042  * determine the number of pages to allocate and use for this queue. This
9043  * function will send the EQ_CREATE mailbox command to the HBA to setup the
9044  * event queue. This function is asynchronous and will wait for the mailbox
9045  * command to finish before continuing.
9046  *
9047  * On success this function will return a zero. If unable to allocate enough
9048  * memory this function will return ENOMEM. If the queue create mailbox command
9049  * fails this function will return ENXIO.
9050  **/
9051 uint32_t
9052 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9053 {
9054         struct lpfc_mbx_eq_create *eq_create;
9055         LPFC_MBOXQ_t *mbox;
9056         int rc, length, status = 0;
9057         struct lpfc_dmabuf *dmabuf;
9058         uint32_t shdr_status, shdr_add_status;
9059         union lpfc_sli4_cfg_shdr *shdr;
9060         uint16_t dmult;
9061
9062         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9063         if (!mbox)
9064                 return -ENOMEM;
9065         length = (sizeof(struct lpfc_mbx_eq_create) -
9066                   sizeof(struct lpfc_sli4_cfg_mhdr));
9067         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9068                          LPFC_MBOX_OPCODE_EQ_CREATE,
9069                          length, LPFC_SLI4_MBX_EMBED);
9070         eq_create = &mbox->u.mqe.un.eq_create;
9071         bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9072                eq->page_count);
9073         bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9074                LPFC_EQE_SIZE);
9075         bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9076         /* Calculate delay multiper from maximum interrupt per second */
9077         dmult = LPFC_DMULT_CONST/imax - 1;
9078         bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9079                dmult);
9080         switch (eq->entry_count) {
9081         default:
9082                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9083                                 "0360 Unsupported EQ count. (%d)\n",
9084                                 eq->entry_count);
9085                 if (eq->entry_count < 256)
9086                         return -EINVAL;
9087                 /* otherwise default to smallest count (drop through) */
9088         case 256:
9089                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9090                        LPFC_EQ_CNT_256);
9091                 break;
9092         case 512:
9093                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9094                        LPFC_EQ_CNT_512);
9095                 break;
9096         case 1024:
9097                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9098                        LPFC_EQ_CNT_1024);
9099                 break;
9100         case 2048:
9101                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9102                        LPFC_EQ_CNT_2048);
9103                 break;
9104         case 4096:
9105                 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9106                        LPFC_EQ_CNT_4096);
9107                 break;
9108         }
9109         list_for_each_entry(dmabuf, &eq->page_list, list) {
9110                 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9111                                         putPaddrLow(dmabuf->phys);
9112                 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9113                                         putPaddrHigh(dmabuf->phys);
9114         }
9115         mbox->vport = phba->pport;
9116         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9117         mbox->context1 = NULL;
9118         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9119         shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9120         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9121         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9122         if (shdr_status || shdr_add_status || rc) {
9123                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9124                                 "2500 EQ_CREATE mailbox failed with "
9125                                 "status x%x add_status x%x, mbx status x%x\n",
9126                                 shdr_status, shdr_add_status, rc);
9127                 status = -ENXIO;
9128         }
9129         eq->type = LPFC_EQ;
9130         eq->subtype = LPFC_NONE;
9131         eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9132         if (eq->queue_id == 0xFFFF)
9133                 status = -ENXIO;
9134         eq->host_index = 0;
9135         eq->hba_index = 0;
9136
9137         if (rc != MBX_TIMEOUT)
9138                 mempool_free(mbox, phba->mbox_mem_pool);
9139         return status;
9140 }
9141
9142 /**
9143  * lpfc_cq_create - Create a Completion Queue on the HBA
9144  * @phba: HBA structure that indicates port to create a queue on.
9145  * @cq: The queue structure to use to create the completion queue.
9146  * @eq: The event queue to bind this completion queue to.
9147  *
9148  * This function creates a completion queue, as detailed in @wq, on a port,
9149  * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9150  *
9151  * The @phba struct is used to send mailbox command to HBA. The @cq struct
9152  * is used to get the entry count and entry size that are necessary to
9153  * determine the number of pages to allocate and use for this queue. The @eq
9154  * is used to indicate which event queue to bind this completion queue to. This
9155  * function will send the CQ_CREATE mailbox command to the HBA to setup the
9156  * completion queue. This function is asynchronous and will wait for the mailbox
9157  * command to finish before continuing.
9158  *
9159  * On success this function will return a zero. If unable to allocate enough
9160  * memory this function will return ENOMEM. If the queue create mailbox command
9161  * fails this function will return ENXIO.
9162  **/
9163 uint32_t
9164 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9165                struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9166 {
9167         struct lpfc_mbx_cq_create *cq_create;
9168         struct lpfc_dmabuf *dmabuf;
9169         LPFC_MBOXQ_t *mbox;
9170         int rc, length, status = 0;
9171         uint32_t shdr_status, shdr_add_status;
9172         union lpfc_sli4_cfg_shdr *shdr;
9173
9174         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9175         if (!mbox)
9176                 return -ENOMEM;
9177         length = (sizeof(struct lpfc_mbx_cq_create) -
9178                   sizeof(struct lpfc_sli4_cfg_mhdr));
9179         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9180                          LPFC_MBOX_OPCODE_CQ_CREATE,
9181                          length, LPFC_SLI4_MBX_EMBED);
9182         cq_create = &mbox->u.mqe.un.cq_create;
9183         bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9184                     cq->page_count);
9185         bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9186         bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9187         bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9188         switch (cq->entry_count) {
9189         default:
9190                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9191                                 "0361 Unsupported CQ count. (%d)\n",
9192                                 cq->entry_count);
9193                 if (cq->entry_count < 256)
9194                         return -EINVAL;
9195                 /* otherwise default to smallest count (drop through) */
9196         case 256:
9197                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9198                        LPFC_CQ_CNT_256);
9199                 break;
9200         case 512:
9201                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9202                        LPFC_CQ_CNT_512);
9203                 break;
9204         case 1024:
9205                 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9206                        LPFC_CQ_CNT_1024);
9207                 break;
9208         }
9209         list_for_each_entry(dmabuf, &cq->page_list, list) {
9210                 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9211                                         putPaddrLow(dmabuf->phys);
9212                 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9213                                         putPaddrHigh(dmabuf->phys);
9214         }
9215         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9216
9217         /* The IOCTL status is embedded in the mailbox subheader. */
9218         shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9219         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9220         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9221         if (shdr_status || shdr_add_status || rc) {
9222                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9223                                 "2501 CQ_CREATE mailbox failed with "
9224                                 "status x%x add_status x%x, mbx status x%x\n",
9225                                 shdr_status, shdr_add_status, rc);
9226                 status = -ENXIO;
9227                 goto out;
9228         }
9229         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9230         if (cq->queue_id == 0xFFFF) {
9231                 status = -ENXIO;
9232                 goto out;
9233         }
9234         /* link the cq onto the parent eq child list */
9235         list_add_tail(&cq->list, &eq->child_list);
9236         /* Set up completion queue's type and subtype */
9237         cq->type = type;
9238         cq->subtype = subtype;
9239         cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9240         cq->host_index = 0;
9241         cq->hba_index = 0;
9242 out:
9243
9244         if (rc != MBX_TIMEOUT)
9245                 mempool_free(mbox, phba->mbox_mem_pool);
9246         return status;
9247 }
9248
9249 /**
9250  * lpfc_wq_create - Create a Work Queue on the HBA
9251  * @phba: HBA structure that indicates port to create a queue on.
9252  * @wq: The queue structure to use to create the work queue.
9253  * @cq: The completion queue to bind this work queue to.
9254  * @subtype: The subtype of the work queue indicating its functionality.
9255  *
9256  * This function creates a work queue, as detailed in @wq, on a port, described
9257  * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9258  *
9259  * The @phba struct is used to send mailbox command to HBA. The @wq struct
9260  * is used to get the entry count and entry size that are necessary to
9261  * determine the number of pages to allocate and use for this queue. The @cq
9262  * is used to indicate which completion queue to bind this work queue to. This
9263  * function will send the WQ_CREATE mailbox command to the HBA to setup the
9264  * work queue. This function is asynchronous and will wait for the mailbox
9265  * command to finish before continuing.
9266  *
9267  * On success this function will return a zero. If unable to allocate enough
9268  * memory this function will return ENOMEM. If the queue create mailbox command
9269  * fails this function will return ENXIO.
9270  **/
9271 uint32_t
9272 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9273                struct lpfc_queue *cq, uint32_t subtype)
9274 {
9275         struct lpfc_mbx_wq_create *wq_create;
9276         struct lpfc_dmabuf *dmabuf;
9277         LPFC_MBOXQ_t *mbox;
9278         int rc, length, status = 0;
9279         uint32_t shdr_status, shdr_add_status;
9280         union lpfc_sli4_cfg_shdr *shdr;
9281
9282         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9283         if (!mbox)
9284                 return -ENOMEM;
9285         length = (sizeof(struct lpfc_mbx_wq_create) -
9286                   sizeof(struct lpfc_sli4_cfg_mhdr));
9287         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9288                          LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9289                          length, LPFC_SLI4_MBX_EMBED);
9290         wq_create = &mbox->u.mqe.un.wq_create;
9291         bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9292                     wq->page_count);
9293         bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9294                     cq->queue_id);
9295         list_for_each_entry(dmabuf, &wq->page_list, list) {
9296                 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9297                                         putPaddrLow(dmabuf->phys);
9298                 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9299                                         putPaddrHigh(dmabuf->phys);
9300         }
9301         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9302         /* The IOCTL status is embedded in the mailbox subheader. */
9303         shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9304         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9305         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9306         if (shdr_status || shdr_add_status || rc) {
9307                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9308                                 "2503 WQ_CREATE mailbox failed with "
9309                                 "status x%x add_status x%x, mbx status x%x\n",
9310                                 shdr_status, shdr_add_status, rc);
9311                 status = -ENXIO;
9312                 goto out;
9313         }
9314         wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9315         if (wq->queue_id == 0xFFFF) {
9316                 status = -ENXIO;
9317                 goto out;
9318         }
9319         wq->type = LPFC_WQ;
9320         wq->subtype = subtype;
9321         wq->host_index = 0;
9322         wq->hba_index = 0;
9323
9324         /* link the wq onto the parent cq child list */
9325         list_add_tail(&wq->list, &cq->child_list);
9326 out:
9327         if (rc == MBX_TIMEOUT)
9328                 mempool_free(mbox, phba->mbox_mem_pool);
9329         return status;
9330 }
9331
9332 /**
9333  * lpfc_rq_create - Create a Receive Queue on the HBA
9334  * @phba: HBA structure that indicates port to create a queue on.
9335  * @hrq: The queue structure to use to create the header receive queue.
9336  * @drq: The queue structure to use to create the data receive queue.
9337  * @cq: The completion queue to bind this work queue to.
9338  *
9339  * This function creates a receive buffer queue pair , as detailed in @hrq and
9340  * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9341  * to the HBA.
9342  *
9343  * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9344  * struct is used to get the entry count that is necessary to determine the
9345  * number of pages to use for this queue. The @cq is used to indicate which
9346  * completion queue to bind received buffers that are posted to these queues to.
9347  * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9348  * receive queue pair. This function is asynchronous and will wait for the
9349  * mailbox command to finish before continuing.
9350  *
9351  * On success this function will return a zero. If unable to allocate enough
9352  * memory this function will return ENOMEM. If the queue create mailbox command
9353  * fails this function will return ENXIO.
9354  **/
9355 uint32_t
9356 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9357                struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9358 {
9359         struct lpfc_mbx_rq_create *rq_create;
9360         struct lpfc_dmabuf *dmabuf;
9361         LPFC_MBOXQ_t *mbox;
9362         int rc, length, status = 0;
9363         uint32_t shdr_status, shdr_add_status;
9364         union lpfc_sli4_cfg_shdr *shdr;
9365
9366         if (hrq->entry_count != drq->entry_count)
9367                 return -EINVAL;
9368         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9369         if (!mbox)
9370                 return -ENOMEM;
9371         length = (sizeof(struct lpfc_mbx_rq_create) -
9372                   sizeof(struct lpfc_sli4_cfg_mhdr));
9373         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9374                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9375                          length, LPFC_SLI4_MBX_EMBED);
9376         rq_create = &mbox->u.mqe.un.rq_create;
9377         switch (hrq->entry_count) {
9378         default:
9379                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9380                                 "2535 Unsupported RQ count. (%d)\n",
9381                                 hrq->entry_count);
9382                 if (hrq->entry_count < 512)
9383                         return -EINVAL;
9384                 /* otherwise default to smallest count (drop through) */
9385         case 512:
9386                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9387                        LPFC_RQ_RING_SIZE_512);
9388                 break;
9389         case 1024:
9390                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9391                        LPFC_RQ_RING_SIZE_1024);
9392                 break;
9393         case 2048:
9394                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9395                        LPFC_RQ_RING_SIZE_2048);
9396                 break;
9397         case 4096:
9398                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9399                        LPFC_RQ_RING_SIZE_4096);
9400                 break;
9401         }
9402         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9403                cq->queue_id);
9404         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9405                hrq->page_count);
9406         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9407                LPFC_HDR_BUF_SIZE);
9408         list_for_each_entry(dmabuf, &hrq->page_list, list) {
9409                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9410                                         putPaddrLow(dmabuf->phys);
9411                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9412                                         putPaddrHigh(dmabuf->phys);
9413         }
9414         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9415         /* The IOCTL status is embedded in the mailbox subheader. */
9416         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9417         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9418         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9419         if (shdr_status || shdr_add_status || rc) {
9420                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9421                                 "2504 RQ_CREATE mailbox failed with "
9422                                 "status x%x add_status x%x, mbx status x%x\n",
9423                                 shdr_status, shdr_add_status, rc);
9424                 status = -ENXIO;
9425                 goto out;
9426         }
9427         hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9428         if (hrq->queue_id == 0xFFFF) {
9429                 status = -ENXIO;
9430                 goto out;
9431         }
9432         hrq->type = LPFC_HRQ;
9433         hrq->subtype = subtype;
9434         hrq->host_index = 0;
9435         hrq->hba_index = 0;
9436
9437         /* now create the data queue */
9438         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9439                          LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9440                          length, LPFC_SLI4_MBX_EMBED);
9441         switch (drq->entry_count) {
9442         default:
9443                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9444                                 "2536 Unsupported RQ count. (%d)\n",
9445                                 drq->entry_count);
9446                 if (drq->entry_count < 512)
9447                         return -EINVAL;
9448                 /* otherwise default to smallest count (drop through) */
9449         case 512:
9450                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9451                        LPFC_RQ_RING_SIZE_512);
9452                 break;
9453         case 1024:
9454                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9455                        LPFC_RQ_RING_SIZE_1024);
9456                 break;
9457         case 2048:
9458                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9459                        LPFC_RQ_RING_SIZE_2048);
9460                 break;
9461         case 4096:
9462                 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9463                        LPFC_RQ_RING_SIZE_4096);
9464                 break;
9465         }
9466         bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9467                cq->queue_id);
9468         bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9469                drq->page_count);
9470         bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9471                LPFC_DATA_BUF_SIZE);
9472         list_for_each_entry(dmabuf, &drq->page_list, list) {
9473                 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9474                                         putPaddrLow(dmabuf->phys);
9475                 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9476                                         putPaddrHigh(dmabuf->phys);
9477         }
9478         rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9479         /* The IOCTL status is embedded in the mailbox subheader. */
9480         shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9481         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9482         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9483         if (shdr_status || shdr_add_status || rc) {
9484                 status = -ENXIO;
9485                 goto out;
9486         }
9487         drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9488         if (drq->queue_id == 0xFFFF) {
9489                 status = -ENXIO;
9490                 goto out;
9491         }
9492         drq->type = LPFC_DRQ;
9493         drq->subtype = subtype;
9494         drq->host_index = 0;
9495         drq->hba_index = 0;
9496
9497         /* link the header and data RQs onto the parent cq child list */
9498         list_add_tail(&hrq->list, &cq->child_list);
9499         list_add_tail(&drq->list, &cq->child_list);
9500
9501 out:
9502         if (rc != MBX_TIMEOUT)
9503                 mempool_free(mbox, phba->mbox_mem_pool);
9504         return status;
9505 }
9506
9507 /**
9508  * lpfc_eq_destroy - Destroy an event Queue on the HBA
9509  * @eq: The queue structure associated with the queue to destroy.
9510  *
9511  * This function destroys a queue, as detailed in @eq by sending an mailbox
9512  * command, specific to the type of queue, to the HBA.
9513  *
9514  * The @eq struct is used to get the queue ID of the queue to destroy.
9515  *
9516  * On success this function will return a zero. If the queue destroy mailbox
9517  * command fails this function will return ENXIO.
9518  **/
9519 uint32_t
9520 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
9521 {
9522         LPFC_MBOXQ_t *mbox;
9523         int rc, length, status = 0;
9524         uint32_t shdr_status, shdr_add_status;
9525         union lpfc_sli4_cfg_shdr *shdr;
9526
9527         if (!eq)
9528                 return -ENODEV;
9529         mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
9530         if (!mbox)
9531                 return -ENOMEM;
9532         length = (sizeof(struct lpfc_mbx_eq_destroy) -
9533                   sizeof(struct lpfc_sli4_cfg_mhdr));
9534         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9535                          LPFC_MBOX_OPCODE_EQ_DESTROY,
9536                          length, LPFC_SLI4_MBX_EMBED);
9537         bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
9538                eq->queue_id);
9539         mbox->vport = eq->phba->pport;
9540         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9541
9542         rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
9543         /* The IOCTL status is embedded in the mailbox subheader. */
9544         shdr = (union lpfc_sli4_cfg_shdr *)
9545                 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
9546         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9547         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9548         if (shdr_status || shdr_add_status || rc) {
9549                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9550                                 "2505 EQ_DESTROY mailbox failed with "
9551                                 "status x%x add_status x%x, mbx status x%x\n",
9552                                 shdr_status, shdr_add_status, rc);
9553                 status = -ENXIO;
9554         }
9555
9556         /* Remove eq from any list */
9557         list_del_init(&eq->list);
9558         if (rc != MBX_TIMEOUT)
9559                 mempool_free(mbox, eq->phba->mbox_mem_pool);
9560         return status;
9561 }
9562
9563 /**
9564  * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
9565  * @cq: The queue structure associated with the queue to destroy.
9566  *
9567  * This function destroys a queue, as detailed in @cq by sending an mailbox
9568  * command, specific to the type of queue, to the HBA.
9569  *
9570  * The @cq struct is used to get the queue ID of the queue to destroy.
9571  *
9572  * On success this function will return a zero. If the queue destroy mailbox
9573  * command fails this function will return ENXIO.
9574  **/
9575 uint32_t
9576 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
9577 {
9578         LPFC_MBOXQ_t *mbox;
9579         int rc, length, status = 0;
9580         uint32_t shdr_status, shdr_add_status;
9581         union lpfc_sli4_cfg_shdr *shdr;
9582
9583         if (!cq)
9584                 return -ENODEV;
9585         mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
9586         if (!mbox)
9587                 return -ENOMEM;
9588         length = (sizeof(struct lpfc_mbx_cq_destroy) -
9589                   sizeof(struct lpfc_sli4_cfg_mhdr));
9590         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9591                          LPFC_MBOX_OPCODE_CQ_DESTROY,
9592                          length, LPFC_SLI4_MBX_EMBED);
9593         bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
9594                cq->queue_id);
9595         mbox->vport = cq->phba->pport;
9596         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9597         rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
9598         /* The IOCTL status is embedded in the mailbox subheader. */
9599         shdr = (union lpfc_sli4_cfg_shdr *)
9600                 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
9601         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9602         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9603         if (shdr_status || shdr_add_status || rc) {
9604                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9605                                 "2506 CQ_DESTROY mailbox failed with "
9606                                 "status x%x add_status x%x, mbx status x%x\n",
9607                                 shdr_status, shdr_add_status, rc);
9608                 status = -ENXIO;
9609         }
9610         /* Remove cq from any list */
9611         list_del_init(&cq->list);
9612         if (rc != MBX_TIMEOUT)
9613                 mempool_free(mbox, cq->phba->mbox_mem_pool);
9614         return status;
9615 }
9616
9617 /**
9618  * lpfc_wq_destroy - Destroy a Work Queue on the HBA
9619  * @wq: The queue structure associated with the queue to destroy.
9620  *
9621  * This function destroys a queue, as detailed in @wq by sending an mailbox
9622  * command, specific to the type of queue, to the HBA.
9623  *
9624  * The @wq struct is used to get the queue ID of the queue to destroy.
9625  *
9626  * On success this function will return a zero. If the queue destroy mailbox
9627  * command fails this function will return ENXIO.
9628  **/
9629 uint32_t
9630 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
9631 {
9632         LPFC_MBOXQ_t *mbox;
9633         int rc, length, status = 0;
9634         uint32_t shdr_status, shdr_add_status;
9635         union lpfc_sli4_cfg_shdr *shdr;
9636
9637         if (!wq)
9638                 return -ENODEV;
9639         mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
9640         if (!mbox)
9641                 return -ENOMEM;
9642         length = (sizeof(struct lpfc_mbx_wq_destroy) -
9643                   sizeof(struct lpfc_sli4_cfg_mhdr));
9644         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9645                          LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
9646                          length, LPFC_SLI4_MBX_EMBED);
9647         bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
9648                wq->queue_id);
9649         mbox->vport = wq->phba->pport;
9650         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9651         rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
9652         shdr = (union lpfc_sli4_cfg_shdr *)
9653                 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
9654         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9655         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9656         if (shdr_status || shdr_add_status || rc) {
9657                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9658                                 "2508 WQ_DESTROY mailbox failed with "
9659                                 "status x%x add_status x%x, mbx status x%x\n",
9660                                 shdr_status, shdr_add_status, rc);
9661                 status = -ENXIO;
9662         }
9663         /* Remove wq from any list */
9664         list_del_init(&wq->list);
9665         if (rc != MBX_TIMEOUT)
9666                 mempool_free(mbox, wq->phba->mbox_mem_pool);
9667         return status;
9668 }
9669
9670 /**
9671  * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
9672  * @rq: The queue structure associated with the queue to destroy.
9673  *
9674  * This function destroys a queue, as detailed in @rq by sending an mailbox
9675  * command, specific to the type of queue, to the HBA.
9676  *
9677  * The @rq struct is used to get the queue ID of the queue to destroy.
9678  *
9679  * On success this function will return a zero. If the queue destroy mailbox
9680  * command fails this function will return ENXIO.
9681  **/
9682 uint32_t
9683 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9684                 struct lpfc_queue *drq)
9685 {
9686         LPFC_MBOXQ_t *mbox;
9687         int rc, length, status = 0;
9688         uint32_t shdr_status, shdr_add_status;
9689         union lpfc_sli4_cfg_shdr *shdr;
9690
9691         if (!hrq || !drq)
9692                 return -ENODEV;
9693         mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
9694         if (!mbox)
9695                 return -ENOMEM;
9696         length = (sizeof(struct lpfc_mbx_rq_destroy) -
9697                   sizeof(struct mbox_header));
9698         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9699                          LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
9700                          length, LPFC_SLI4_MBX_EMBED);
9701         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
9702                hrq->queue_id);
9703         mbox->vport = hrq->phba->pport;
9704         mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9705         rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
9706         /* The IOCTL status is embedded in the mailbox subheader. */
9707         shdr = (union lpfc_sli4_cfg_shdr *)
9708                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
9709         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9710         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9711         if (shdr_status || shdr_add_status || rc) {
9712                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9713                                 "2509 RQ_DESTROY mailbox failed with "
9714                                 "status x%x add_status x%x, mbx status x%x\n",
9715                                 shdr_status, shdr_add_status, rc);
9716                 if (rc != MBX_TIMEOUT)
9717                         mempool_free(mbox, hrq->phba->mbox_mem_pool);
9718                 return -ENXIO;
9719         }
9720         bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
9721                drq->queue_id);
9722         rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
9723         shdr = (union lpfc_sli4_cfg_shdr *)
9724                 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
9725         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9726         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9727         if (shdr_status || shdr_add_status || rc) {
9728                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9729                                 "2510 RQ_DESTROY mailbox failed with "
9730                                 "status x%x add_status x%x, mbx status x%x\n",
9731                                 shdr_status, shdr_add_status, rc);
9732                 status = -ENXIO;
9733         }
9734         list_del_init(&hrq->list);
9735         list_del_init(&drq->list);
9736         if (rc != MBX_TIMEOUT)
9737                 mempool_free(mbox, hrq->phba->mbox_mem_pool);
9738         return status;
9739 }
9740
9741 /**
9742  * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
9743  * @phba: The virtual port for which this call being executed.
9744  * @pdma_phys_addr0: Physical address of the 1st SGL page.
9745  * @pdma_phys_addr1: Physical address of the 2nd SGL page.
9746  * @xritag: the xritag that ties this io to the SGL pages.
9747  *
9748  * This routine will post the sgl pages for the IO that has the xritag
9749  * that is in the iocbq structure. The xritag is assigned during iocbq
9750  * creation and persists for as long as the driver is loaded.
9751  * if the caller has fewer than 256 scatter gather segments to map then
9752  * pdma_phys_addr1 should be 0.
9753  * If the caller needs to map more than 256 scatter gather segment then
9754  * pdma_phys_addr1 should be a valid physical address.
9755  * physical address for SGLs must be 64 byte aligned.
9756  * If you are going to map 2 SGL's then the first one must have 256 entries
9757  * the second sgl can have between 1 and 256 entries.
9758  *
9759  * Return codes:
9760  *      0 - Success
9761  *      -ENXIO, -ENOMEM - Failure
9762  **/
9763 int
9764 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
9765                 dma_addr_t pdma_phys_addr0,
9766                 dma_addr_t pdma_phys_addr1,
9767                 uint16_t xritag)
9768 {
9769         struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
9770         LPFC_MBOXQ_t *mbox;
9771         int rc;
9772         uint32_t shdr_status, shdr_add_status;
9773         union lpfc_sli4_cfg_shdr *shdr;
9774
9775         if (xritag == NO_XRI) {
9776                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9777                                 "0364 Invalid param:\n");
9778                 return -EINVAL;
9779         }
9780
9781         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9782         if (!mbox)
9783                 return -ENOMEM;
9784
9785         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9786                         LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
9787                         sizeof(struct lpfc_mbx_post_sgl_pages) -
9788                         sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
9789
9790         post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
9791                                 &mbox->u.mqe.un.post_sgl_pages;
9792         bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
9793         bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
9794
9795         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
9796                                 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
9797         post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
9798                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
9799
9800         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
9801                                 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
9802         post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
9803                                 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
9804         if (!phba->sli4_hba.intr_enable)
9805                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9806         else
9807                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
9808         /* The IOCTL status is embedded in the mailbox subheader. */
9809         shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
9810         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9811         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9812         if (rc != MBX_TIMEOUT)
9813                 mempool_free(mbox, phba->mbox_mem_pool);
9814         if (shdr_status || shdr_add_status || rc) {
9815                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9816                                 "2511 POST_SGL mailbox failed with "
9817                                 "status x%x add_status x%x, mbx status x%x\n",
9818                                 shdr_status, shdr_add_status, rc);
9819                 rc = -ENXIO;
9820         }
9821         return 0;
9822 }
9823 /**
9824  * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
9825  * @phba: The virtual port for which this call being executed.
9826  *
9827  * This routine will remove all of the sgl pages registered with the hba.
9828  *
9829  * Return codes:
9830  *      0 - Success
9831  *      -ENXIO, -ENOMEM - Failure
9832  **/
9833 int
9834 lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
9835 {
9836         LPFC_MBOXQ_t *mbox;
9837         int rc;
9838         uint32_t shdr_status, shdr_add_status;
9839         union lpfc_sli4_cfg_shdr *shdr;
9840
9841         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9842         if (!mbox)
9843                 return -ENOMEM;
9844
9845         lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9846                         LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
9847                         LPFC_SLI4_MBX_EMBED);
9848         if (!phba->sli4_hba.intr_enable)
9849                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9850         else
9851                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
9852         /* The IOCTL status is embedded in the mailbox subheader. */
9853         shdr = (union lpfc_sli4_cfg_shdr *)
9854                 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
9855         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9856         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9857         if (rc != MBX_TIMEOUT)
9858                 mempool_free(mbox, phba->mbox_mem_pool);
9859         if (shdr_status || shdr_add_status || rc) {
9860                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9861                                 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
9862                                 "status x%x add_status x%x, mbx status x%x\n",
9863                                 shdr_status, shdr_add_status, rc);
9864                 rc = -ENXIO;
9865         }
9866         return rc;
9867 }
9868
9869 /**
9870  * lpfc_sli4_next_xritag - Get an xritag for the io
9871  * @phba: Pointer to HBA context object.
9872  *
9873  * This function gets an xritag for the iocb. If there is no unused xritag
9874  * it will return 0xffff.
9875  * The function returns the allocated xritag if successful, else returns zero.
9876  * Zero is not a valid xritag.
9877  * The caller is not required to hold any lock.
9878  **/
9879 uint16_t
9880 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
9881 {
9882         uint16_t xritag;
9883
9884         spin_lock_irq(&phba->hbalock);
9885         xritag = phba->sli4_hba.next_xri;
9886         if ((xritag != (uint16_t) -1) && xritag <
9887                 (phba->sli4_hba.max_cfg_param.max_xri
9888                         + phba->sli4_hba.max_cfg_param.xri_base)) {
9889                 phba->sli4_hba.next_xri++;
9890                 phba->sli4_hba.max_cfg_param.xri_used++;
9891                 spin_unlock_irq(&phba->hbalock);
9892                 return xritag;
9893         }
9894         spin_unlock_irq(&phba->hbalock);
9895
9896         lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9897                         "2004 Failed to allocate XRI.last XRITAG is %d"
9898                         " Max XRI is %d, Used XRI is %d\n",
9899                         phba->sli4_hba.next_xri,
9900                         phba->sli4_hba.max_cfg_param.max_xri,
9901                         phba->sli4_hba.max_cfg_param.xri_used);
9902         return -1;
9903 }
9904
9905 /**
9906  * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
9907  * @phba: pointer to lpfc hba data structure.
9908  *
9909  * This routine is invoked to post a block of driver's sgl pages to the
9910  * HBA using non-embedded mailbox command. No Lock is held. This routine
9911  * is only called when the driver is loading and after all IO has been
9912  * stopped.
9913  **/
9914 int
9915 lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
9916 {
9917         struct lpfc_sglq *sglq_entry;
9918         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
9919         struct sgl_page_pairs *sgl_pg_pairs;
9920         void *viraddr;
9921         LPFC_MBOXQ_t *mbox;
9922         uint32_t reqlen, alloclen, pg_pairs;
9923         uint32_t mbox_tmo;
9924         uint16_t xritag_start = 0;
9925         int els_xri_cnt, rc = 0;
9926         uint32_t shdr_status, shdr_add_status;
9927         union lpfc_sli4_cfg_shdr *shdr;
9928
9929         /* The number of sgls to be posted */
9930         els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
9931
9932         reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
9933                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
9934         if (reqlen > PAGE_SIZE) {
9935                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
9936                                 "2559 Block sgl registration required DMA "
9937                                 "size (%d) great than a page\n", reqlen);
9938                 return -ENOMEM;
9939         }
9940         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9941         if (!mbox) {
9942                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9943                                 "2560 Failed to allocate mbox cmd memory\n");
9944                 return -ENOMEM;
9945         }
9946
9947         /* Allocate DMA memory and set up the non-embedded mailbox command */
9948         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9949                          LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
9950                          LPFC_SLI4_MBX_NEMBED);
9951
9952         if (alloclen < reqlen) {
9953                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9954                                 "0285 Allocated DMA memory size (%d) is "
9955                                 "less than the requested DMA memory "
9956                                 "size (%d)\n", alloclen, reqlen);
9957                 lpfc_sli4_mbox_cmd_free(phba, mbox);
9958                 return -ENOMEM;
9959         }
9960
9961         /* Get the first SGE entry from the non-embedded DMA memory */
9962         if (unlikely(!mbox->sge_array)) {
9963                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
9964                                 "2525 Failed to get the non-embedded SGE "
9965                                 "virtual address\n");
9966                 lpfc_sli4_mbox_cmd_free(phba, mbox);
9967                 return -ENOMEM;
9968         }
9969         viraddr = mbox->sge_array->addr[0];
9970
9971         /* Set up the SGL pages in the non-embedded DMA pages */
9972         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
9973         sgl_pg_pairs = &sgl->sgl_pg_pairs;
9974
9975         for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
9976                 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
9977                 /* Set up the sge entry */
9978                 sgl_pg_pairs->sgl_pg0_addr_lo =
9979                                 cpu_to_le32(putPaddrLow(sglq_entry->phys));
9980                 sgl_pg_pairs->sgl_pg0_addr_hi =
9981                                 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
9982                 sgl_pg_pairs->sgl_pg1_addr_lo =
9983                                 cpu_to_le32(putPaddrLow(0));
9984                 sgl_pg_pairs->sgl_pg1_addr_hi =
9985                                 cpu_to_le32(putPaddrHigh(0));
9986                 /* Keep the first xritag on the list */
9987                 if (pg_pairs == 0)
9988                         xritag_start = sglq_entry->sli4_xritag;
9989                 sgl_pg_pairs++;
9990         }
9991         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
9992         pg_pairs = (pg_pairs > 0) ? (pg_pairs - 1) : pg_pairs;
9993         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
9994         /* Perform endian conversion if necessary */
9995         sgl->word0 = cpu_to_le32(sgl->word0);
9996
9997         if (!phba->sli4_hba.intr_enable)
9998                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9999         else {
10000                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10001                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10002         }
10003         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10004         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10005         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10006         if (rc != MBX_TIMEOUT)
10007                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10008         if (shdr_status || shdr_add_status || rc) {
10009                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10010                                 "2513 POST_SGL_BLOCK mailbox command failed "
10011                                 "status x%x add_status x%x mbx status x%x\n",
10012                                 shdr_status, shdr_add_status, rc);
10013                 rc = -ENXIO;
10014         }
10015         return rc;
10016 }
10017
10018 /**
10019  * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10020  * @phba: pointer to lpfc hba data structure.
10021  * @sblist: pointer to scsi buffer list.
10022  * @count: number of scsi buffers on the list.
10023  *
10024  * This routine is invoked to post a block of @count scsi sgl pages from a
10025  * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10026  * No Lock is held.
10027  *
10028  **/
10029 int
10030 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10031                               int cnt)
10032 {
10033         struct lpfc_scsi_buf *psb;
10034         struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10035         struct sgl_page_pairs *sgl_pg_pairs;
10036         void *viraddr;
10037         LPFC_MBOXQ_t *mbox;
10038         uint32_t reqlen, alloclen, pg_pairs;
10039         uint32_t mbox_tmo;
10040         uint16_t xritag_start = 0;
10041         int rc = 0;
10042         uint32_t shdr_status, shdr_add_status;
10043         dma_addr_t pdma_phys_bpl1;
10044         union lpfc_sli4_cfg_shdr *shdr;
10045
10046         /* Calculate the requested length of the dma memory */
10047         reqlen = cnt * sizeof(struct sgl_page_pairs) +
10048                  sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10049         if (reqlen > PAGE_SIZE) {
10050                 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10051                                 "0217 Block sgl registration required DMA "
10052                                 "size (%d) great than a page\n", reqlen);
10053                 return -ENOMEM;
10054         }
10055         mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10056         if (!mbox) {
10057                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10058                                 "0283 Failed to allocate mbox cmd memory\n");
10059                 return -ENOMEM;
10060         }
10061
10062         /* Allocate DMA memory and set up the non-embedded mailbox command */
10063         alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10064                                 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10065                                 LPFC_SLI4_MBX_NEMBED);
10066
10067         if (alloclen < reqlen) {
10068                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10069                                 "2561 Allocated DMA memory size (%d) is "
10070                                 "less than the requested DMA memory "
10071                                 "size (%d)\n", alloclen, reqlen);
10072                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10073                 return -ENOMEM;
10074         }
10075
10076         /* Get the first SGE entry from the non-embedded DMA memory */
10077         if (unlikely(!mbox->sge_array)) {
10078                 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10079                                 "2565 Failed to get the non-embedded SGE "
10080                                 "virtual address\n");
10081                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10082                 return -ENOMEM;
10083         }
10084         viraddr = mbox->sge_array->addr[0];
10085
10086         /* Set up the SGL pages in the non-embedded DMA pages */
10087         sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10088         sgl_pg_pairs = &sgl->sgl_pg_pairs;
10089
10090         pg_pairs = 0;
10091         list_for_each_entry(psb, sblist, list) {
10092                 /* Set up the sge entry */
10093                 sgl_pg_pairs->sgl_pg0_addr_lo =
10094                         cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10095                 sgl_pg_pairs->sgl_pg0_addr_hi =
10096                         cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10097                 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10098                         pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10099                 else
10100                         pdma_phys_bpl1 = 0;
10101                 sgl_pg_pairs->sgl_pg1_addr_lo =
10102                         cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10103                 sgl_pg_pairs->sgl_pg1_addr_hi =
10104                         cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10105                 /* Keep the first xritag on the list */
10106                 if (pg_pairs == 0)
10107                         xritag_start = psb->cur_iocbq.sli4_xritag;
10108                 sgl_pg_pairs++;
10109                 pg_pairs++;
10110         }
10111         bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10112         bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10113         /* Perform endian conversion if necessary */
10114         sgl->word0 = cpu_to_le32(sgl->word0);
10115
10116         if (!phba->sli4_hba.intr_enable)
10117                 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10118         else {
10119                 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10120                 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10121         }
10122         shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10123         shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10124         shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10125         if (rc != MBX_TIMEOUT)
10126                 lpfc_sli4_mbox_cmd_free(phba, mbox);
10127         if (shdr_status || shdr_add_status || rc) {
10128                 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10129                                 "2564 POST_SGL_BLOCK mailbox command failed "
10130                                 "status x%x add_status x%x mbx status x%x\n",
10131                                 shdr_status, shdr_add_status, rc);
10132                 rc = -ENXIO;
10133         }
10134         return rc;
10135 }
10136
10137 /**
10138  * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10139  * @phba: pointer to lpfc_hba struct that the frame was received on
10140  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10141  *
10142  * This function checks the fields in the @fc_hdr to see if the FC frame is a
10143  * valid type of frame that the LPFC driver will handle. This function will
10144  * return a zero if the frame is a valid frame or a non zero value when the
10145  * frame does not pass the check.
10146  **/
10147 static int
10148 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10149 {
10150         char *rctl_names[] = FC_RCTL_NAMES_INIT;
10151         char *type_names[] = FC_TYPE_NAMES_INIT;
10152         struct fc_vft_header *fc_vft_hdr;
10153
10154         switch (fc_hdr->fh_r_ctl) {
10155         case FC_RCTL_DD_UNCAT:          /* uncategorized information */
10156         case FC_RCTL_DD_SOL_DATA:       /* solicited data */
10157         case FC_RCTL_DD_UNSOL_CTL:      /* unsolicited control */
10158         case FC_RCTL_DD_SOL_CTL:        /* solicited control or reply */
10159         case FC_RCTL_DD_UNSOL_DATA:     /* unsolicited data */
10160         case FC_RCTL_DD_DATA_DESC:      /* data descriptor */
10161         case FC_RCTL_DD_UNSOL_CMD:      /* unsolicited command */
10162         case FC_RCTL_DD_CMD_STATUS:     /* command status */
10163         case FC_RCTL_ELS_REQ:   /* extended link services request */
10164         case FC_RCTL_ELS_REP:   /* extended link services reply */
10165         case FC_RCTL_ELS4_REQ:  /* FC-4 ELS request */
10166         case FC_RCTL_ELS4_REP:  /* FC-4 ELS reply */
10167         case FC_RCTL_BA_NOP:    /* basic link service NOP */
10168         case FC_RCTL_BA_ABTS:   /* basic link service abort */
10169         case FC_RCTL_BA_RMC:    /* remove connection */
10170         case FC_RCTL_BA_ACC:    /* basic accept */
10171         case FC_RCTL_BA_RJT:    /* basic reject */
10172         case FC_RCTL_BA_PRMT:
10173         case FC_RCTL_ACK_1:     /* acknowledge_1 */
10174         case FC_RCTL_ACK_0:     /* acknowledge_0 */
10175         case FC_RCTL_P_RJT:     /* port reject */
10176         case FC_RCTL_F_RJT:     /* fabric reject */
10177         case FC_RCTL_P_BSY:     /* port busy */
10178         case FC_RCTL_F_BSY:     /* fabric busy to data frame */
10179         case FC_RCTL_F_BSYL:    /* fabric busy to link control frame */
10180         case FC_RCTL_LCR:       /* link credit reset */
10181         case FC_RCTL_END:       /* end */
10182                 break;
10183         case FC_RCTL_VFTH:      /* Virtual Fabric tagging Header */
10184                 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10185                 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10186                 return lpfc_fc_frame_check(phba, fc_hdr);
10187         default:
10188                 goto drop;
10189         }
10190         switch (fc_hdr->fh_type) {
10191         case FC_TYPE_BLS:
10192         case FC_TYPE_ELS:
10193         case FC_TYPE_FCP:
10194         case FC_TYPE_CT:
10195                 break;
10196         case FC_TYPE_IP:
10197         case FC_TYPE_ILS:
10198         default:
10199                 goto drop;
10200         }
10201         lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10202                         "2538 Received frame rctl:%s type:%s\n",
10203                         rctl_names[fc_hdr->fh_r_ctl],
10204                         type_names[fc_hdr->fh_type]);
10205         return 0;
10206 drop:
10207         lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10208                         "2539 Dropped frame rctl:%s type:%s\n",
10209                         rctl_names[fc_hdr->fh_r_ctl],
10210                         type_names[fc_hdr->fh_type]);
10211         return 1;
10212 }
10213
10214 /**
10215  * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10216  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10217  *
10218  * This function processes the FC header to retrieve the VFI from the VF
10219  * header, if one exists. This function will return the VFI if one exists
10220  * or 0 if no VSAN Header exists.
10221  **/
10222 static uint32_t
10223 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10224 {
10225         struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10226
10227         if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10228                 return 0;
10229         return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10230 }
10231
10232 /**
10233  * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10234  * @phba: Pointer to the HBA structure to search for the vport on
10235  * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10236  * @fcfi: The FC Fabric ID that the frame came from
10237  *
10238  * This function searches the @phba for a vport that matches the content of the
10239  * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10240  * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10241  * returns the matching vport pointer or NULL if unable to match frame to a
10242  * vport.
10243  **/
10244 static struct lpfc_vport *
10245 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10246                        uint16_t fcfi)
10247 {
10248         struct lpfc_vport **vports;
10249         struct lpfc_vport *vport = NULL;
10250         int i;
10251         uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10252                         fc_hdr->fh_d_id[1] << 8 |
10253                         fc_hdr->fh_d_id[2]);
10254
10255         vports = lpfc_create_vport_work_array(phba);
10256         if (vports != NULL)
10257                 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10258                         if (phba->fcf.fcfi == fcfi &&
10259                             vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10260                             vports[i]->fc_myDID == did) {
10261                                 vport = vports[i];
10262                                 break;
10263                         }
10264                 }
10265         lpfc_destroy_vport_work_array(phba, vports);
10266         return vport;
10267 }
10268
10269 /**
10270  * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10271  * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10272  *
10273  * This function searches through the existing incomplete sequences that have
10274  * been sent to this @vport. If the frame matches one of the incomplete
10275  * sequences then the dbuf in the @dmabuf is added to the list of frames that
10276  * make up that sequence. If no sequence is found that matches this frame then
10277  * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10278  * This function returns a pointer to the first dmabuf in the sequence list that
10279  * the frame was linked to.
10280  **/
10281 static struct hbq_dmabuf *
10282 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10283 {
10284         struct fc_frame_header *new_hdr;
10285         struct fc_frame_header *temp_hdr;
10286         struct lpfc_dmabuf *d_buf;
10287         struct lpfc_dmabuf *h_buf;
10288         struct hbq_dmabuf *seq_dmabuf = NULL;
10289         struct hbq_dmabuf *temp_dmabuf = NULL;
10290
10291         new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10292         /* Use the hdr_buf to find the sequence that this frame belongs to */
10293         list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10294                 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10295                 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10296                     (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10297                     (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10298                         continue;
10299                 /* found a pending sequence that matches this frame */
10300                 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10301                 break;
10302         }
10303         if (!seq_dmabuf) {
10304                 /*
10305                  * This indicates first frame received for this sequence.
10306                  * Queue the buffer on the vport's rcv_buffer_list.
10307                  */
10308                 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10309                 return dmabuf;
10310         }
10311         temp_hdr = seq_dmabuf->hbuf.virt;
10312         if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
10313                 list_add(&seq_dmabuf->dbuf.list, &dmabuf->dbuf.list);
10314                 return dmabuf;
10315         }
10316         /* find the correct place in the sequence to insert this frame */
10317         list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10318                 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10319                 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10320                 /*
10321                  * If the frame's sequence count is greater than the frame on
10322                  * the list then insert the frame right after this frame
10323                  */
10324                 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10325                         list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10326                         return seq_dmabuf;
10327                 }
10328         }
10329         return NULL;
10330 }
10331
10332 /**
10333  * lpfc_seq_complete - Indicates if a sequence is complete
10334  * @dmabuf: pointer to a dmabuf that describes the FC sequence
10335  *
10336  * This function checks the sequence, starting with the frame described by
10337  * @dmabuf, to see if all the frames associated with this sequence are present.
10338  * the frames associated with this sequence are linked to the @dmabuf using the
10339  * dbuf list. This function looks for two major things. 1) That the first frame
10340  * has a sequence count of zero. 2) There is a frame with last frame of sequence
10341  * set. 3) That there are no holes in the sequence count. The function will
10342  * return 1 when the sequence is complete, otherwise it will return 0.
10343  **/
10344 static int
10345 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
10346 {
10347         struct fc_frame_header *hdr;
10348         struct lpfc_dmabuf *d_buf;
10349         struct hbq_dmabuf *seq_dmabuf;
10350         uint32_t fctl;
10351         int seq_count = 0;
10352
10353         hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10354         /* make sure first fame of sequence has a sequence count of zero */
10355         if (hdr->fh_seq_cnt != seq_count)
10356                 return 0;
10357         fctl = (hdr->fh_f_ctl[0] << 16 |
10358                 hdr->fh_f_ctl[1] << 8 |
10359                 hdr->fh_f_ctl[2]);
10360         /* If last frame of sequence we can return success. */
10361         if (fctl & FC_FC_END_SEQ)
10362                 return 1;
10363         list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
10364                 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10365                 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10366                 /* If there is a hole in the sequence count then fail. */
10367                 if (++seq_count != hdr->fh_seq_cnt)
10368                         return 0;
10369                 fctl = (hdr->fh_f_ctl[0] << 16 |
10370                         hdr->fh_f_ctl[1] << 8 |
10371                         hdr->fh_f_ctl[2]);
10372                 /* If last frame of sequence we can return success. */
10373                 if (fctl & FC_FC_END_SEQ)
10374                         return 1;
10375         }
10376         return 0;
10377 }
10378
10379 /**
10380  * lpfc_prep_seq - Prep sequence for ULP processing
10381  * @vport: Pointer to the vport on which this sequence was received
10382  * @dmabuf: pointer to a dmabuf that describes the FC sequence
10383  *
10384  * This function takes a sequence, described by a list of frames, and creates
10385  * a list of iocbq structures to describe the sequence. This iocbq list will be
10386  * used to issue to the generic unsolicited sequence handler. This routine
10387  * returns a pointer to the first iocbq in the list. If the function is unable
10388  * to allocate an iocbq then it throw out the received frames that were not
10389  * able to be described and return a pointer to the first iocbq. If unable to
10390  * allocate any iocbqs (including the first) this function will return NULL.
10391  **/
10392 static struct lpfc_iocbq *
10393 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
10394 {
10395         struct lpfc_dmabuf *d_buf, *n_buf;
10396         struct lpfc_iocbq *first_iocbq, *iocbq;
10397         struct fc_frame_header *fc_hdr;
10398         uint32_t sid;
10399
10400         fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10401         /* remove from receive buffer list */
10402         list_del_init(&seq_dmabuf->hbuf.list);
10403         /* get the Remote Port's SID */
10404         sid = (fc_hdr->fh_s_id[0] << 16 |
10405                fc_hdr->fh_s_id[1] << 8 |
10406                fc_hdr->fh_s_id[2]);
10407         /* Get an iocbq struct to fill in. */
10408         first_iocbq = lpfc_sli_get_iocbq(vport->phba);
10409         if (first_iocbq) {
10410                 /* Initialize the first IOCB. */
10411                 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
10412                 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
10413                 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
10414                 first_iocbq->iocb.unsli3.rcvsli3.vpi =
10415                                         vport->vpi + vport->phba->vpi_base;
10416                 /* put the first buffer into the first IOCBq */
10417                 first_iocbq->context2 = &seq_dmabuf->dbuf;
10418                 first_iocbq->context3 = NULL;
10419                 first_iocbq->iocb.ulpBdeCount = 1;
10420                 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
10421                                                         LPFC_DATA_BUF_SIZE;
10422                 first_iocbq->iocb.un.rcvels.remoteID = sid;
10423         }
10424         iocbq = first_iocbq;
10425         /*
10426          * Each IOCBq can have two Buffers assigned, so go through the list
10427          * of buffers for this sequence and save two buffers in each IOCBq
10428          */
10429         list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
10430                 if (!iocbq) {
10431                         lpfc_in_buf_free(vport->phba, d_buf);
10432                         continue;
10433                 }
10434                 if (!iocbq->context3) {
10435                         iocbq->context3 = d_buf;
10436                         iocbq->iocb.ulpBdeCount++;
10437                         iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
10438                                                         LPFC_DATA_BUF_SIZE;
10439                 } else {
10440                         iocbq = lpfc_sli_get_iocbq(vport->phba);
10441                         if (!iocbq) {
10442                                 if (first_iocbq) {
10443                                         first_iocbq->iocb.ulpStatus =
10444                                                         IOSTAT_FCP_RSP_ERROR;
10445                                         first_iocbq->iocb.un.ulpWord[4] =
10446                                                         IOERR_NO_RESOURCES;
10447                                 }
10448                                 lpfc_in_buf_free(vport->phba, d_buf);
10449                                 continue;
10450                         }
10451                         iocbq->context2 = d_buf;
10452                         iocbq->context3 = NULL;
10453                         iocbq->iocb.ulpBdeCount = 1;
10454                         iocbq->iocb.un.cont64[0].tus.f.bdeSize =
10455                                                         LPFC_DATA_BUF_SIZE;
10456                         iocbq->iocb.un.rcvels.remoteID = sid;
10457                         list_add_tail(&iocbq->list, &first_iocbq->list);
10458                 }
10459         }
10460         return first_iocbq;
10461 }
10462
10463 /**
10464  * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
10465  * @phba: Pointer to HBA context object.
10466  *
10467  * This function is called with no lock held. This function processes all
10468  * the received buffers and gives it to upper layers when a received buffer
10469  * indicates that it is the final frame in the sequence. The interrupt
10470  * service routine processes received buffers at interrupt contexts and adds
10471  * received dma buffers to the rb_pend_list queue and signals the worker thread.
10472  * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
10473  * appropriate receive function when the final frame in a sequence is received.
10474  **/
10475 int
10476 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba)
10477 {
10478         LIST_HEAD(cmplq);
10479         struct hbq_dmabuf *dmabuf, *seq_dmabuf;
10480         struct fc_frame_header *fc_hdr;
10481         struct lpfc_vport *vport;
10482         uint32_t fcfi;
10483         struct lpfc_iocbq *iocbq;
10484
10485         /* Clear hba flag and get all received buffers into the cmplq */
10486         spin_lock_irq(&phba->hbalock);
10487         phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
10488         list_splice_init(&phba->rb_pend_list, &cmplq);
10489         spin_unlock_irq(&phba->hbalock);
10490
10491         /* Process each received buffer */
10492         while ((dmabuf = lpfc_sli_hbqbuf_get(&cmplq)) != NULL) {
10493                 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10494                 /* check to see if this a valid type of frame */
10495                 if (lpfc_fc_frame_check(phba, fc_hdr)) {
10496                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
10497                         continue;
10498                 }
10499                 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->rcqe);
10500                 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
10501                 if (!vport) {
10502                         /* throw out the frame */
10503                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
10504                         continue;
10505                 }
10506                 /* Link this frame */
10507                 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
10508                 if (!seq_dmabuf) {
10509                         /* unable to add frame to vport - throw it out */
10510                         lpfc_in_buf_free(phba, &dmabuf->dbuf);
10511                         continue;
10512                 }
10513                 /* If not last frame in sequence continue processing frames. */
10514                 if (!lpfc_seq_complete(seq_dmabuf)) {
10515                         /*
10516                          * When saving off frames post a new one and mark this
10517                          * frame to be freed when it is finished.
10518                          **/
10519                         lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
10520                         dmabuf->tag = -1;
10521                         continue;
10522                 }
10523                 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10524                 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
10525                 if (!lpfc_complete_unsol_iocb(phba,
10526                                               &phba->sli.ring[LPFC_ELS_RING],
10527                                               iocbq, fc_hdr->fh_r_ctl,
10528                                               fc_hdr->fh_type))
10529                         lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10530                                         "2540 Ring %d handler: unexpected Rctl "
10531                                         "x%x Type x%x received\n",
10532                                         LPFC_ELS_RING,
10533                                         fc_hdr->fh_r_ctl, fc_hdr->fh_type);
10534         };
10535         return 0;
10536 }