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