[SCSI] lpfc 8.3.28: Miscellaneous fixes in sysfs and mgmt interfaces
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
792581de 4 * Copyright (C) 2004-2011 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
5a0e3ad6 26#include <linux/slab.h>
dea3101e 27
91886523 28#include <scsi/scsi.h>
dea3101e 29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_host.h>
f888ba3c 32#include <scsi/scsi_transport_fc.h>
da0436e9 33#include <scsi/fc/fc_fs.h>
0d878419 34#include <linux/aer.h>
dea3101e 35
da0436e9 36#include "lpfc_hw4.h"
dea3101e 37#include "lpfc_hw.h"
38#include "lpfc_sli.h"
da0436e9 39#include "lpfc_sli4.h"
ea2151b4 40#include "lpfc_nl.h"
dea3101e 41#include "lpfc_disc.h"
42#include "lpfc_scsi.h"
43#include "lpfc.h"
44#include "lpfc_crtn.h"
45#include "lpfc_logmsg.h"
46#include "lpfc_compat.h"
858c9f6c 47#include "lpfc_debugfs.h"
04c68496 48#include "lpfc_vport.h"
dea3101e 49
50/* There are only four IOCB completion types. */
51typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56} lpfc_iocb_type;
57
4f774513
JS
58
59/* Provide function prototypes local to this module. */
60static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
63 uint8_t *, uint32_t *);
64static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
6669f9bb
JS
66static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
0558056c
JS
68static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70
4f774513
JS
71static IOCB_t *
72lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
73{
74 return &iocbq->iocb;
75}
76
77/**
78 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
79 * @q: The Work Queue to operate on.
80 * @wqe: The work Queue Entry to put on the Work queue.
81 *
82 * This routine will copy the contents of @wqe to the next available entry on
83 * the @q. This function will then ring the Work Queue Doorbell to signal the
84 * HBA to start processing the Work Queue Entry. This function returns 0 if
85 * successful. If no entries are available on @q then this function will return
86 * -ENOMEM.
87 * The caller is expected to hold the hbalock when calling this routine.
88 **/
89static uint32_t
90lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
91{
92 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
93 struct lpfc_register doorbell;
94 uint32_t host_index;
95
96 /* If the host has not yet processed the next entry then we are done */
97 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
98 return -ENOMEM;
99 /* set consumption flag every once in a while */
100 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
f0d9bccc 101 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
fedd3b7b
JS
102 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
103 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
4f774513
JS
104 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
105
106 /* Update the host index before invoking device */
107 host_index = q->host_index;
108 q->host_index = ((q->host_index + 1) % q->entry_count);
109
110 /* Ring Doorbell */
111 doorbell.word0 = 0;
112 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
113 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
114 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
115 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
116 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
117
118 return 0;
119}
120
121/**
122 * lpfc_sli4_wq_release - Updates internal hba index for WQ
123 * @q: The Work Queue to operate on.
124 * @index: The index to advance the hba index to.
125 *
126 * This routine will update the HBA index of a queue to reflect consumption of
127 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
128 * an entry the host calls this function to update the queue's internal
129 * pointers. This routine returns the number of entries that were consumed by
130 * the HBA.
131 **/
132static uint32_t
133lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
134{
135 uint32_t released = 0;
136
137 if (q->hba_index == index)
138 return 0;
139 do {
140 q->hba_index = ((q->hba_index + 1) % q->entry_count);
141 released++;
142 } while (q->hba_index != index);
143 return released;
144}
145
146/**
147 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
148 * @q: The Mailbox Queue to operate on.
149 * @wqe: The Mailbox Queue Entry to put on the Work queue.
150 *
151 * This routine will copy the contents of @mqe to the next available entry on
152 * the @q. This function will then ring the Work Queue Doorbell to signal the
153 * HBA to start processing the Work Queue Entry. This function returns 0 if
154 * successful. If no entries are available on @q then this function will return
155 * -ENOMEM.
156 * The caller is expected to hold the hbalock when calling this routine.
157 **/
158static uint32_t
159lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
160{
161 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
162 struct lpfc_register doorbell;
163 uint32_t host_index;
164
165 /* If the host has not yet processed the next entry then we are done */
166 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
167 return -ENOMEM;
168 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
169 /* Save off the mailbox pointer for completion */
170 q->phba->mbox = (MAILBOX_t *)temp_mqe;
171
172 /* Update the host index before invoking device */
173 host_index = q->host_index;
174 q->host_index = ((q->host_index + 1) % q->entry_count);
175
176 /* Ring Doorbell */
177 doorbell.word0 = 0;
178 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
179 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
180 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
181 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
182 return 0;
183}
184
185/**
186 * lpfc_sli4_mq_release - Updates internal hba index for MQ
187 * @q: The Mailbox Queue to operate on.
188 *
189 * This routine will update the HBA index of a queue to reflect consumption of
190 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
191 * an entry the host calls this function to update the queue's internal
192 * pointers. This routine returns the number of entries that were consumed by
193 * the HBA.
194 **/
195static uint32_t
196lpfc_sli4_mq_release(struct lpfc_queue *q)
197{
198 /* Clear the mailbox pointer for completion */
199 q->phba->mbox = NULL;
200 q->hba_index = ((q->hba_index + 1) % q->entry_count);
201 return 1;
202}
203
204/**
205 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
206 * @q: The Event Queue to get the first valid EQE from
207 *
208 * This routine will get the first valid Event Queue Entry from @q, update
209 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
210 * the Queue (no more work to do), or the Queue is full of EQEs that have been
211 * processed, but not popped back to the HBA then this routine will return NULL.
212 **/
213static struct lpfc_eqe *
214lpfc_sli4_eq_get(struct lpfc_queue *q)
215{
216 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
217
218 /* If the next EQE is not valid then we are done */
cb5172ea 219 if (!bf_get_le32(lpfc_eqe_valid, eqe))
4f774513
JS
220 return NULL;
221 /* If the host has not yet processed the next entry then we are done */
222 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
223 return NULL;
224
225 q->hba_index = ((q->hba_index + 1) % q->entry_count);
226 return eqe;
227}
228
229/**
230 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
231 * @q: The Event Queue that the host has completed processing for.
232 * @arm: Indicates whether the host wants to arms this CQ.
233 *
234 * This routine will mark all Event Queue Entries on @q, from the last
235 * known completed entry to the last entry that was processed, as completed
236 * by clearing the valid bit for each completion queue entry. Then it will
237 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
238 * The internal host index in the @q will be updated by this routine to indicate
239 * that the host has finished processing the entries. The @arm parameter
240 * indicates that the queue should be rearmed when ringing the doorbell.
241 *
242 * This function will return the number of EQEs that were popped.
243 **/
244uint32_t
245lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
246{
247 uint32_t released = 0;
248 struct lpfc_eqe *temp_eqe;
249 struct lpfc_register doorbell;
250
251 /* while there are valid entries */
252 while (q->hba_index != q->host_index) {
253 temp_eqe = q->qe[q->host_index].eqe;
cb5172ea 254 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
4f774513
JS
255 released++;
256 q->host_index = ((q->host_index + 1) % q->entry_count);
257 }
258 if (unlikely(released == 0 && !arm))
259 return 0;
260
261 /* ring doorbell for number popped */
262 doorbell.word0 = 0;
263 if (arm) {
264 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
265 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
266 }
267 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
268 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
269 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
270 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
271 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
272 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
273 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
274 return released;
275}
276
277/**
278 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
279 * @q: The Completion Queue to get the first valid CQE from
280 *
281 * This routine will get the first valid Completion Queue Entry from @q, update
282 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
283 * the Queue (no more work to do), or the Queue is full of CQEs that have been
284 * processed, but not popped back to the HBA then this routine will return NULL.
285 **/
286static struct lpfc_cqe *
287lpfc_sli4_cq_get(struct lpfc_queue *q)
288{
289 struct lpfc_cqe *cqe;
290
291 /* If the next CQE is not valid then we are done */
cb5172ea 292 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
4f774513
JS
293 return NULL;
294 /* If the host has not yet processed the next entry then we are done */
295 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
296 return NULL;
297
298 cqe = q->qe[q->hba_index].cqe;
299 q->hba_index = ((q->hba_index + 1) % q->entry_count);
300 return cqe;
301}
302
303/**
304 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
305 * @q: The Completion Queue that the host has completed processing for.
306 * @arm: Indicates whether the host wants to arms this CQ.
307 *
308 * This routine will mark all Completion queue entries on @q, from the last
309 * known completed entry to the last entry that was processed, as completed
310 * by clearing the valid bit for each completion queue entry. Then it will
311 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
312 * The internal host index in the @q will be updated by this routine to indicate
313 * that the host has finished processing the entries. The @arm parameter
314 * indicates that the queue should be rearmed when ringing the doorbell.
315 *
316 * This function will return the number of CQEs that were released.
317 **/
318uint32_t
319lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
320{
321 uint32_t released = 0;
322 struct lpfc_cqe *temp_qe;
323 struct lpfc_register doorbell;
324
325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_qe = q->qe[q->host_index].cqe;
cb5172ea 328 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
4f774513
JS
329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
331 }
332 if (unlikely(released == 0 && !arm))
333 return 0;
334
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm)
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
340 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
341 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
342 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
343 return released;
344}
345
346/**
347 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
348 * @q: The Header Receive Queue to operate on.
349 * @wqe: The Receive Queue Entry to put on the Receive queue.
350 *
351 * This routine will copy the contents of @wqe to the next available entry on
352 * the @q. This function will then ring the Receive Queue Doorbell to signal the
353 * HBA to start processing the Receive Queue Entry. This function returns the
354 * index that the rqe was copied to if successful. If no entries are available
355 * on @q then this function will return -ENOMEM.
356 * The caller is expected to hold the hbalock when calling this routine.
357 **/
358static int
359lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
360 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
361{
362 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
363 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
364 struct lpfc_register doorbell;
365 int put_index = hq->host_index;
366
367 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
368 return -EINVAL;
369 if (hq->host_index != dq->host_index)
370 return -EINVAL;
371 /* If the host has not yet processed the next entry then we are done */
372 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
373 return -EBUSY;
374 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
375 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
376
377 /* Update the host index to point to the next slot */
378 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
379 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
380
381 /* Ring The Header Receive Queue Doorbell */
73d91e50 382 if (!(hq->host_index % hq->entry_repost)) {
4f774513
JS
383 doorbell.word0 = 0;
384 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
73d91e50 385 hq->entry_repost);
4f774513
JS
386 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
387 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
388 }
389 return put_index;
390}
391
392/**
393 * lpfc_sli4_rq_release - Updates internal hba index for RQ
394 * @q: The Header Receive Queue to operate on.
395 *
396 * This routine will update the HBA index of a queue to reflect consumption of
397 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
398 * consumed an entry the host calls this function to update the queue's
399 * internal pointers. This routine returns the number of entries that were
400 * consumed by the HBA.
401 **/
402static uint32_t
403lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
404{
405 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
406 return 0;
407 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
408 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
409 return 1;
410}
411
e59058c4 412/**
3621a710 413 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
414 * @phba: Pointer to HBA context object.
415 * @pring: Pointer to driver SLI ring object.
416 *
417 * This function returns pointer to next command iocb entry
418 * in the command ring. The caller must hold hbalock to prevent
419 * other threads consume the next command iocb.
420 * SLI-2/SLI-3 provide different sized iocbs.
421 **/
ed957684
JS
422static inline IOCB_t *
423lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
424{
425 return (IOCB_t *) (((char *) pring->cmdringaddr) +
426 pring->cmdidx * phba->iocb_cmd_size);
427}
428
e59058c4 429/**
3621a710 430 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
431 * @phba: Pointer to HBA context object.
432 * @pring: Pointer to driver SLI ring object.
433 *
434 * This function returns pointer to next response iocb entry
435 * in the response ring. The caller must hold hbalock to make sure
436 * that no other thread consume the next response iocb.
437 * SLI-2/SLI-3 provide different sized iocbs.
438 **/
ed957684
JS
439static inline IOCB_t *
440lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
441{
442 return (IOCB_t *) (((char *) pring->rspringaddr) +
443 pring->rspidx * phba->iocb_rsp_size);
444}
445
e59058c4 446/**
3621a710 447 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
448 * @phba: Pointer to HBA context object.
449 *
450 * This function is called with hbalock held. This function
451 * allocates a new driver iocb object from the iocb pool. If the
452 * allocation is successful, it returns pointer to the newly
453 * allocated iocb object else it returns NULL.
454 **/
2e0fef85
JS
455static struct lpfc_iocbq *
456__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
457{
458 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
459 struct lpfc_iocbq * iocbq = NULL;
460
461 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
2a9bf3d0
JS
462 if (iocbq)
463 phba->iocb_cnt++;
464 if (phba->iocb_cnt > phba->iocb_max)
465 phba->iocb_max = phba->iocb_cnt;
0bd4ca25
JSEC
466 return iocbq;
467}
468
da0436e9
JS
469/**
470 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
471 * @phba: Pointer to HBA context object.
472 * @xritag: XRI value.
473 *
474 * This function clears the sglq pointer from the array of acive
475 * sglq's. The xritag that is passed in is used to index into the
476 * array. Before the xritag can be used it needs to be adjusted
477 * by subtracting the xribase.
478 *
479 * Returns sglq ponter = success, NULL = Failure.
480 **/
481static struct lpfc_sglq *
482__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
483{
da0436e9 484 struct lpfc_sglq *sglq;
6d368e53
JS
485
486 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
487 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
da0436e9
JS
488 return sglq;
489}
490
491/**
492 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
493 * @phba: Pointer to HBA context object.
494 * @xritag: XRI value.
495 *
496 * This function returns the sglq pointer from the array of acive
497 * sglq's. The xritag that is passed in is used to index into the
498 * array. Before the xritag can be used it needs to be adjusted
499 * by subtracting the xribase.
500 *
501 * Returns sglq ponter = success, NULL = Failure.
502 **/
0f65ff68 503struct lpfc_sglq *
da0436e9
JS
504__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
505{
da0436e9 506 struct lpfc_sglq *sglq;
6d368e53
JS
507
508 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
da0436e9
JS
509 return sglq;
510}
511
19ca7609
JS
512/**
513 * __lpfc_set_rrq_active - set RRQ active bit in the ndlp's xri_bitmap.
514 * @phba: Pointer to HBA context object.
515 * @ndlp: nodelist pointer for this target.
516 * @xritag: xri used in this exchange.
517 * @rxid: Remote Exchange ID.
518 * @send_rrq: Flag used to determine if we should send rrq els cmd.
519 *
520 * This function is called with hbalock held.
521 * The active bit is set in the ndlp's active rrq xri_bitmap. Allocates an
522 * rrq struct and adds it to the active_rrq_list.
523 *
524 * returns 0 for rrq slot for this xri
525 * < 0 Were not able to get rrq mem or invalid parameter.
526 **/
527static int
528__lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
529 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
530{
19ca7609
JS
531 struct lpfc_node_rrq *rrq;
532 int empty;
1151e3ec
JS
533 uint32_t did = 0;
534
535
536 if (!ndlp)
537 return -EINVAL;
538
539 if (!phba->cfg_enable_rrq)
540 return -EINVAL;
541
542 if (phba->pport->load_flag & FC_UNLOADING) {
543 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
544 goto out;
545 }
546 did = ndlp->nlp_DID;
19ca7609
JS
547
548 /*
549 * set the active bit even if there is no mem available.
550 */
1151e3ec
JS
551 if (NLP_CHK_FREE_REQ(ndlp))
552 goto out;
553
554 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
555 goto out;
556
6d368e53 557 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
1151e3ec
JS
558 goto out;
559
19ca7609
JS
560 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
561 if (rrq) {
562 rrq->send_rrq = send_rrq;
7851fe2c 563 rrq->xritag = xritag;
19ca7609
JS
564 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
565 rrq->ndlp = ndlp;
566 rrq->nlp_DID = ndlp->nlp_DID;
567 rrq->vport = ndlp->vport;
568 rrq->rxid = rxid;
569 empty = list_empty(&phba->active_rrq_list);
1151e3ec 570 rrq->send_rrq = send_rrq;
19ca7609
JS
571 list_add_tail(&rrq->list, &phba->active_rrq_list);
572 if (!(phba->hba_flag & HBA_RRQ_ACTIVE)) {
573 phba->hba_flag |= HBA_RRQ_ACTIVE;
574 if (empty)
575 lpfc_worker_wake_up(phba);
576 }
577 return 0;
578 }
1151e3ec
JS
579out:
580 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
581 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
582 " DID:0x%x Send:%d\n",
583 xritag, rxid, did, send_rrq);
584 return -EINVAL;
19ca7609
JS
585}
586
587/**
1151e3ec 588 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
19ca7609
JS
589 * @phba: Pointer to HBA context object.
590 * @xritag: xri used in this exchange.
591 * @rrq: The RRQ to be cleared.
592 *
19ca7609 593 **/
1151e3ec
JS
594void
595lpfc_clr_rrq_active(struct lpfc_hba *phba,
596 uint16_t xritag,
597 struct lpfc_node_rrq *rrq)
19ca7609 598{
1151e3ec 599 struct lpfc_nodelist *ndlp = NULL;
19ca7609 600
1151e3ec
JS
601 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
602 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
19ca7609
JS
603
604 /* The target DID could have been swapped (cable swap)
605 * we should use the ndlp from the findnode if it is
606 * available.
607 */
1151e3ec 608 if ((!ndlp) && rrq->ndlp)
19ca7609
JS
609 ndlp = rrq->ndlp;
610
1151e3ec
JS
611 if (!ndlp)
612 goto out;
613
6d368e53 614 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
19ca7609
JS
615 rrq->send_rrq = 0;
616 rrq->xritag = 0;
617 rrq->rrq_stop_time = 0;
618 }
1151e3ec 619out:
19ca7609
JS
620 mempool_free(rrq, phba->rrq_pool);
621}
622
623/**
624 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
625 * @phba: Pointer to HBA context object.
626 *
627 * This function is called with hbalock held. This function
628 * Checks if stop_time (ratov from setting rrq active) has
629 * been reached, if it has and the send_rrq flag is set then
630 * it will call lpfc_send_rrq. If the send_rrq flag is not set
631 * then it will just call the routine to clear the rrq and
632 * free the rrq resource.
633 * The timer is set to the next rrq that is going to expire before
634 * leaving the routine.
635 *
636 **/
637void
638lpfc_handle_rrq_active(struct lpfc_hba *phba)
639{
640 struct lpfc_node_rrq *rrq;
641 struct lpfc_node_rrq *nextrrq;
642 unsigned long next_time;
643 unsigned long iflags;
1151e3ec 644 LIST_HEAD(send_rrq);
19ca7609
JS
645
646 spin_lock_irqsave(&phba->hbalock, iflags);
647 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
648 next_time = jiffies + HZ * (phba->fc_ratov + 1);
649 list_for_each_entry_safe(rrq, nextrrq,
1151e3ec
JS
650 &phba->active_rrq_list, list) {
651 if (time_after(jiffies, rrq->rrq_stop_time))
652 list_move(&rrq->list, &send_rrq);
653 else if (time_before(rrq->rrq_stop_time, next_time))
19ca7609
JS
654 next_time = rrq->rrq_stop_time;
655 }
656 spin_unlock_irqrestore(&phba->hbalock, iflags);
657 if (!list_empty(&phba->active_rrq_list))
658 mod_timer(&phba->rrq_tmr, next_time);
1151e3ec
JS
659 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
660 list_del(&rrq->list);
661 if (!rrq->send_rrq)
662 /* this call will free the rrq */
663 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
664 else if (lpfc_send_rrq(phba, rrq)) {
665 /* if we send the rrq then the completion handler
666 * will clear the bit in the xribitmap.
667 */
668 lpfc_clr_rrq_active(phba, rrq->xritag,
669 rrq);
670 }
671 }
19ca7609
JS
672}
673
674/**
675 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
676 * @vport: Pointer to vport context object.
677 * @xri: The xri used in the exchange.
678 * @did: The targets DID for this exchange.
679 *
680 * returns NULL = rrq not found in the phba->active_rrq_list.
681 * rrq = rrq for this xri and target.
682 **/
683struct lpfc_node_rrq *
684lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
685{
686 struct lpfc_hba *phba = vport->phba;
687 struct lpfc_node_rrq *rrq;
688 struct lpfc_node_rrq *nextrrq;
689 unsigned long iflags;
690
691 if (phba->sli_rev != LPFC_SLI_REV4)
692 return NULL;
693 spin_lock_irqsave(&phba->hbalock, iflags);
694 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
695 if (rrq->vport == vport && rrq->xritag == xri &&
696 rrq->nlp_DID == did){
697 list_del(&rrq->list);
698 spin_unlock_irqrestore(&phba->hbalock, iflags);
699 return rrq;
700 }
701 }
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 return NULL;
704}
705
706/**
707 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
708 * @vport: Pointer to vport context object.
1151e3ec
JS
709 * @ndlp: Pointer to the lpfc_node_list structure.
710 * If ndlp is NULL Remove all active RRQs for this vport from the
711 * phba->active_rrq_list and clear the rrq.
712 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
19ca7609
JS
713 **/
714void
1151e3ec 715lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
19ca7609
JS
716
717{
718 struct lpfc_hba *phba = vport->phba;
719 struct lpfc_node_rrq *rrq;
720 struct lpfc_node_rrq *nextrrq;
721 unsigned long iflags;
1151e3ec 722 LIST_HEAD(rrq_list);
19ca7609
JS
723
724 if (phba->sli_rev != LPFC_SLI_REV4)
725 return;
1151e3ec
JS
726 if (!ndlp) {
727 lpfc_sli4_vport_delete_els_xri_aborted(vport);
728 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
19ca7609 729 }
1151e3ec
JS
730 spin_lock_irqsave(&phba->hbalock, iflags);
731 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
732 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
733 list_move(&rrq->list, &rrq_list);
19ca7609 734 spin_unlock_irqrestore(&phba->hbalock, iflags);
1151e3ec
JS
735
736 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
737 list_del(&rrq->list);
738 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
739 }
19ca7609
JS
740}
741
742/**
743 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
744 * @phba: Pointer to HBA context object.
745 *
746 * Remove all rrqs from the phba->active_rrq_list and free them by
747 * calling __lpfc_clr_active_rrq
748 *
749 **/
750void
751lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
752{
753 struct lpfc_node_rrq *rrq;
754 struct lpfc_node_rrq *nextrrq;
755 unsigned long next_time;
756 unsigned long iflags;
1151e3ec 757 LIST_HEAD(rrq_list);
19ca7609
JS
758
759 if (phba->sli_rev != LPFC_SLI_REV4)
760 return;
761 spin_lock_irqsave(&phba->hbalock, iflags);
762 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
763 next_time = jiffies + HZ * (phba->fc_ratov * 2);
1151e3ec
JS
764 list_splice_init(&phba->active_rrq_list, &rrq_list);
765 spin_unlock_irqrestore(&phba->hbalock, iflags);
766
767 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
19ca7609 768 list_del(&rrq->list);
1151e3ec 769 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
19ca7609 770 }
19ca7609
JS
771 if (!list_empty(&phba->active_rrq_list))
772 mod_timer(&phba->rrq_tmr, next_time);
773}
774
775
776/**
1151e3ec 777 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
19ca7609
JS
778 * @phba: Pointer to HBA context object.
779 * @ndlp: Targets nodelist pointer for this exchange.
780 * @xritag the xri in the bitmap to test.
781 *
782 * This function is called with hbalock held. This function
783 * returns 0 = rrq not active for this xri
784 * 1 = rrq is valid for this xri.
785 **/
1151e3ec
JS
786int
787lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
19ca7609
JS
788 uint16_t xritag)
789{
19ca7609
JS
790 if (!ndlp)
791 return 0;
6d368e53 792 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
19ca7609
JS
793 return 1;
794 else
795 return 0;
796}
797
798/**
799 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
800 * @phba: Pointer to HBA context object.
801 * @ndlp: nodelist pointer for this target.
802 * @xritag: xri used in this exchange.
803 * @rxid: Remote Exchange ID.
804 * @send_rrq: Flag used to determine if we should send rrq els cmd.
805 *
806 * This function takes the hbalock.
807 * The active bit is always set in the active rrq xri_bitmap even
808 * if there is no slot avaiable for the other rrq information.
809 *
810 * returns 0 rrq actived for this xri
811 * < 0 No memory or invalid ndlp.
812 **/
813int
814lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
815 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
816{
817 int ret;
818 unsigned long iflags;
819
820 spin_lock_irqsave(&phba->hbalock, iflags);
821 ret = __lpfc_set_rrq_active(phba, ndlp, xritag, rxid, send_rrq);
822 spin_unlock_irqrestore(&phba->hbalock, iflags);
823 return ret;
824}
825
da0436e9
JS
826/**
827 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
828 * @phba: Pointer to HBA context object.
19ca7609 829 * @piocb: Pointer to the iocbq.
da0436e9
JS
830 *
831 * This function is called with hbalock held. This function
6d368e53 832 * gets a new driver sglq object from the sglq list. If the
da0436e9
JS
833 * list is not empty then it is successful, it returns pointer to the newly
834 * allocated sglq object else it returns NULL.
835 **/
836static struct lpfc_sglq *
19ca7609 837__lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
da0436e9
JS
838{
839 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
840 struct lpfc_sglq *sglq = NULL;
19ca7609 841 struct lpfc_sglq *start_sglq = NULL;
19ca7609
JS
842 struct lpfc_scsi_buf *lpfc_cmd;
843 struct lpfc_nodelist *ndlp;
844 int found = 0;
845
846 if (piocbq->iocb_flag & LPFC_IO_FCP) {
847 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
848 ndlp = lpfc_cmd->rdata->pnode;
be858b65
JS
849 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
850 !(piocbq->iocb_flag & LPFC_IO_LIBDFC))
19ca7609 851 ndlp = piocbq->context_un.ndlp;
19ca7609
JS
852 else
853 ndlp = piocbq->context1;
854
da0436e9 855 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
19ca7609
JS
856 start_sglq = sglq;
857 while (!found) {
858 if (!sglq)
859 return NULL;
1151e3ec 860 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_xritag)) {
19ca7609
JS
861 /* This xri has an rrq outstanding for this DID.
862 * put it back in the list and get another xri.
863 */
864 list_add_tail(&sglq->list, lpfc_sgl_list);
865 sglq = NULL;
866 list_remove_head(lpfc_sgl_list, sglq,
867 struct lpfc_sglq, list);
868 if (sglq == start_sglq) {
869 sglq = NULL;
870 break;
871 } else
872 continue;
873 }
874 sglq->ndlp = ndlp;
875 found = 1;
6d368e53 876 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
19ca7609
JS
877 sglq->state = SGL_ALLOCATED;
878 }
da0436e9
JS
879 return sglq;
880}
881
e59058c4 882/**
3621a710 883 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
884 * @phba: Pointer to HBA context object.
885 *
886 * This function is called with no lock held. This function
887 * allocates a new driver iocb object from the iocb pool. If the
888 * allocation is successful, it returns pointer to the newly
889 * allocated iocb object else it returns NULL.
890 **/
2e0fef85
JS
891struct lpfc_iocbq *
892lpfc_sli_get_iocbq(struct lpfc_hba *phba)
893{
894 struct lpfc_iocbq * iocbq = NULL;
895 unsigned long iflags;
896
897 spin_lock_irqsave(&phba->hbalock, iflags);
898 iocbq = __lpfc_sli_get_iocbq(phba);
899 spin_unlock_irqrestore(&phba->hbalock, iflags);
900 return iocbq;
901}
902
4f774513
JS
903/**
904 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
905 * @phba: Pointer to HBA context object.
906 * @iocbq: Pointer to driver iocb object.
907 *
908 * This function is called with hbalock held to release driver
909 * iocb object to the iocb pool. The iotag in the iocb object
910 * does not change for each use of the iocb object. This function
911 * clears all other fields of the iocb object when it is freed.
912 * The sqlq structure that holds the xritag and phys and virtual
913 * mappings for the scatter gather list is retrieved from the
914 * active array of sglq. The get of the sglq pointer also clears
915 * the entry in the array. If the status of the IO indiactes that
916 * this IO was aborted then the sglq entry it put on the
917 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
918 * IO has good status or fails for any other reason then the sglq
919 * entry is added to the free list (lpfc_sgl_list).
920 **/
921static void
922__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
923{
924 struct lpfc_sglq *sglq;
925 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
2a9bf3d0
JS
926 unsigned long iflag = 0;
927 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
4f774513
JS
928
929 if (iocbq->sli4_xritag == NO_XRI)
930 sglq = NULL;
931 else
6d368e53
JS
932 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
933
4f774513 934 if (sglq) {
0f65ff68
JS
935 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
936 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
937 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
938 iflag);
939 list_add(&sglq->list,
940 &phba->sli4_hba.lpfc_abts_els_sgl_list);
941 spin_unlock_irqrestore(
942 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
943 } else {
944 sglq->state = SGL_FREED;
19ca7609 945 sglq->ndlp = NULL;
fedd3b7b
JS
946 list_add_tail(&sglq->list,
947 &phba->sli4_hba.lpfc_sgl_list);
2a9bf3d0
JS
948
949 /* Check if TXQ queue needs to be serviced */
589a52d6 950 if (pring->txq_cnt)
2a9bf3d0 951 lpfc_worker_wake_up(phba);
0f65ff68 952 }
4f774513
JS
953 }
954
955
956 /*
957 * Clean all volatile data fields, preserve iotag and node struct.
958 */
959 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
6d368e53 960 iocbq->sli4_lxritag = NO_XRI;
4f774513
JS
961 iocbq->sli4_xritag = NO_XRI;
962 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
963}
964
2a9bf3d0 965
e59058c4 966/**
3772a991 967 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
968 * @phba: Pointer to HBA context object.
969 * @iocbq: Pointer to driver iocb object.
970 *
971 * This function is called with hbalock held to release driver
972 * iocb object to the iocb pool. The iotag in the iocb object
973 * does not change for each use of the iocb object. This function
974 * clears all other fields of the iocb object when it is freed.
975 **/
a6ababd2 976static void
3772a991 977__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 978{
2e0fef85 979 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
980
981 /*
982 * Clean all volatile data fields, preserve iotag and node struct.
983 */
984 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 985 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
986 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
987}
988
3772a991
JS
989/**
990 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
991 * @phba: Pointer to HBA context object.
992 * @iocbq: Pointer to driver iocb object.
993 *
994 * This function is called with hbalock held to release driver
995 * iocb object to the iocb pool. The iotag in the iocb object
996 * does not change for each use of the iocb object. This function
997 * clears all other fields of the iocb object when it is freed.
998 **/
999static void
1000__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1001{
1002 phba->__lpfc_sli_release_iocbq(phba, iocbq);
2a9bf3d0 1003 phba->iocb_cnt--;
3772a991
JS
1004}
1005
e59058c4 1006/**
3621a710 1007 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
1008 * @phba: Pointer to HBA context object.
1009 * @iocbq: Pointer to driver iocb object.
1010 *
1011 * This function is called with no lock held to release the iocb to
1012 * iocb pool.
1013 **/
2e0fef85
JS
1014void
1015lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1016{
1017 unsigned long iflags;
1018
1019 /*
1020 * Clean all volatile data fields, preserve iotag and node struct.
1021 */
1022 spin_lock_irqsave(&phba->hbalock, iflags);
1023 __lpfc_sli_release_iocbq(phba, iocbq);
1024 spin_unlock_irqrestore(&phba->hbalock, iflags);
1025}
1026
a257bf90
JS
1027/**
1028 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1029 * @phba: Pointer to HBA context object.
1030 * @iocblist: List of IOCBs.
1031 * @ulpstatus: ULP status in IOCB command field.
1032 * @ulpWord4: ULP word-4 in IOCB command field.
1033 *
1034 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1035 * on the list by invoking the complete callback function associated with the
1036 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1037 * fields.
1038 **/
1039void
1040lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1041 uint32_t ulpstatus, uint32_t ulpWord4)
1042{
1043 struct lpfc_iocbq *piocb;
1044
1045 while (!list_empty(iocblist)) {
1046 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1047
1048 if (!piocb->iocb_cmpl)
1049 lpfc_sli_release_iocbq(phba, piocb);
1050 else {
1051 piocb->iocb.ulpStatus = ulpstatus;
1052 piocb->iocb.un.ulpWord[4] = ulpWord4;
1053 (piocb->iocb_cmpl) (phba, piocb, piocb);
1054 }
1055 }
1056 return;
1057}
1058
e59058c4 1059/**
3621a710
JS
1060 * lpfc_sli_iocb_cmd_type - Get the iocb type
1061 * @iocb_cmnd: iocb command code.
e59058c4
JS
1062 *
1063 * This function is called by ring event handler function to get the iocb type.
1064 * This function translates the iocb command to an iocb command type used to
1065 * decide the final disposition of each completed IOCB.
1066 * The function returns
1067 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1068 * LPFC_SOL_IOCB if it is a solicited iocb completion
1069 * LPFC_ABORT_IOCB if it is an abort iocb
1070 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1071 *
1072 * The caller is not required to hold any lock.
1073 **/
dea3101e 1074static lpfc_iocb_type
1075lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1076{
1077 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1078
1079 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1080 return 0;
1081
1082 switch (iocb_cmnd) {
1083 case CMD_XMIT_SEQUENCE_CR:
1084 case CMD_XMIT_SEQUENCE_CX:
1085 case CMD_XMIT_BCAST_CN:
1086 case CMD_XMIT_BCAST_CX:
1087 case CMD_ELS_REQUEST_CR:
1088 case CMD_ELS_REQUEST_CX:
1089 case CMD_CREATE_XRI_CR:
1090 case CMD_CREATE_XRI_CX:
1091 case CMD_GET_RPI_CN:
1092 case CMD_XMIT_ELS_RSP_CX:
1093 case CMD_GET_RPI_CR:
1094 case CMD_FCP_IWRITE_CR:
1095 case CMD_FCP_IWRITE_CX:
1096 case CMD_FCP_IREAD_CR:
1097 case CMD_FCP_IREAD_CX:
1098 case CMD_FCP_ICMND_CR:
1099 case CMD_FCP_ICMND_CX:
f5603511
JS
1100 case CMD_FCP_TSEND_CX:
1101 case CMD_FCP_TRSP_CX:
1102 case CMD_FCP_TRECEIVE_CX:
1103 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 1104 case CMD_ADAPTER_MSG:
1105 case CMD_ADAPTER_DUMP:
1106 case CMD_XMIT_SEQUENCE64_CR:
1107 case CMD_XMIT_SEQUENCE64_CX:
1108 case CMD_XMIT_BCAST64_CN:
1109 case CMD_XMIT_BCAST64_CX:
1110 case CMD_ELS_REQUEST64_CR:
1111 case CMD_ELS_REQUEST64_CX:
1112 case CMD_FCP_IWRITE64_CR:
1113 case CMD_FCP_IWRITE64_CX:
1114 case CMD_FCP_IREAD64_CR:
1115 case CMD_FCP_IREAD64_CX:
1116 case CMD_FCP_ICMND64_CR:
1117 case CMD_FCP_ICMND64_CX:
f5603511
JS
1118 case CMD_FCP_TSEND64_CX:
1119 case CMD_FCP_TRSP64_CX:
1120 case CMD_FCP_TRECEIVE64_CX:
dea3101e 1121 case CMD_GEN_REQUEST64_CR:
1122 case CMD_GEN_REQUEST64_CX:
1123 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
1124 case DSSCMD_IWRITE64_CR:
1125 case DSSCMD_IWRITE64_CX:
1126 case DSSCMD_IREAD64_CR:
1127 case DSSCMD_IREAD64_CX:
dea3101e 1128 type = LPFC_SOL_IOCB;
1129 break;
1130 case CMD_ABORT_XRI_CN:
1131 case CMD_ABORT_XRI_CX:
1132 case CMD_CLOSE_XRI_CN:
1133 case CMD_CLOSE_XRI_CX:
1134 case CMD_XRI_ABORTED_CX:
1135 case CMD_ABORT_MXRI64_CN:
6669f9bb 1136 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 1137 type = LPFC_ABORT_IOCB;
1138 break;
1139 case CMD_RCV_SEQUENCE_CX:
1140 case CMD_RCV_ELS_REQ_CX:
1141 case CMD_RCV_SEQUENCE64_CX:
1142 case CMD_RCV_ELS_REQ64_CX:
57127f15 1143 case CMD_ASYNC_STATUS:
ed957684
JS
1144 case CMD_IOCB_RCV_SEQ64_CX:
1145 case CMD_IOCB_RCV_ELS64_CX:
1146 case CMD_IOCB_RCV_CONT64_CX:
3163f725 1147 case CMD_IOCB_RET_XRI64_CX:
dea3101e 1148 type = LPFC_UNSOL_IOCB;
1149 break;
3163f725
JS
1150 case CMD_IOCB_XMIT_MSEQ64_CR:
1151 case CMD_IOCB_XMIT_MSEQ64_CX:
1152 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1153 case CMD_IOCB_RCV_ELS_LIST64_CX:
1154 case CMD_IOCB_CLOSE_EXTENDED_CN:
1155 case CMD_IOCB_ABORT_EXTENDED_CN:
1156 case CMD_IOCB_RET_HBQE64_CN:
1157 case CMD_IOCB_FCP_IBIDIR64_CR:
1158 case CMD_IOCB_FCP_IBIDIR64_CX:
1159 case CMD_IOCB_FCP_ITASKMGT64_CX:
1160 case CMD_IOCB_LOGENTRY_CN:
1161 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1162 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 1163 __func__, iocb_cmnd);
3163f725
JS
1164 type = LPFC_UNKNOWN_IOCB;
1165 break;
dea3101e 1166 default:
1167 type = LPFC_UNKNOWN_IOCB;
1168 break;
1169 }
1170
1171 return type;
1172}
1173
e59058c4 1174/**
3621a710 1175 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
1176 * @phba: Pointer to HBA context object.
1177 *
1178 * This function is called from SLI initialization code
1179 * to configure every ring of the HBA's SLI interface. The
1180 * caller is not required to hold any lock. This function issues
1181 * a config_ring mailbox command for each ring.
1182 * This function returns zero if successful else returns a negative
1183 * error code.
1184 **/
dea3101e 1185static int
ed957684 1186lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 1187{
1188 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
1189 LPFC_MBOXQ_t *pmb;
1190 MAILBOX_t *pmbox;
1191 int i, rc, ret = 0;
dea3101e 1192
ed957684
JS
1193 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1194 if (!pmb)
1195 return -ENOMEM;
04c68496 1196 pmbox = &pmb->u.mb;
ed957684 1197 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 1198 for (i = 0; i < psli->num_rings; i++) {
dea3101e 1199 lpfc_config_ring(phba, i, pmb);
1200 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1201 if (rc != MBX_SUCCESS) {
92d7f7b0 1202 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 1203 "0446 Adapter failed to init (%d), "
dea3101e 1204 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1205 "ring %d\n",
e8b62011
JS
1206 rc, pmbox->mbxCommand,
1207 pmbox->mbxStatus, i);
2e0fef85 1208 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
1209 ret = -ENXIO;
1210 break;
dea3101e 1211 }
1212 }
ed957684
JS
1213 mempool_free(pmb, phba->mbox_mem_pool);
1214 return ret;
dea3101e 1215}
1216
e59058c4 1217/**
3621a710 1218 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
1219 * @phba: Pointer to HBA context object.
1220 * @pring: Pointer to driver SLI ring object.
1221 * @piocb: Pointer to the driver iocb object.
1222 *
1223 * This function is called with hbalock held. The function adds the
1224 * new iocb to txcmplq of the given ring. This function always returns
1225 * 0. If this function is called for ELS ring, this function checks if
1226 * there is a vport associated with the ELS command. This function also
1227 * starts els_tmofunc timer if this is an ELS command.
1228 **/
dea3101e 1229static int
2e0fef85
JS
1230lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1231 struct lpfc_iocbq *piocb)
dea3101e 1232{
dea3101e 1233 list_add_tail(&piocb->list, &pring->txcmplq);
2a9bf3d0 1234 piocb->iocb_flag |= LPFC_IO_ON_Q;
dea3101e 1235 pring->txcmplq_cnt++;
2a9bf3d0
JS
1236 if (pring->txcmplq_cnt > pring->txcmplq_max)
1237 pring->txcmplq_max = pring->txcmplq_cnt;
1238
92d7f7b0
JS
1239 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1240 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1241 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1242 if (!piocb->vport)
1243 BUG();
1244 else
1245 mod_timer(&piocb->vport->els_tmofunc,
1246 jiffies + HZ * (phba->fc_ratov << 1));
1247 }
1248
dea3101e 1249
2e0fef85 1250 return 0;
dea3101e 1251}
1252
e59058c4 1253/**
3621a710 1254 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
1255 * @phba: Pointer to HBA context object.
1256 * @pring: Pointer to driver SLI ring object.
1257 *
1258 * This function is called with hbalock held to get next
1259 * iocb in txq of the given ring. If there is any iocb in
1260 * the txq, the function returns first iocb in the list after
1261 * removing the iocb from the list, else it returns NULL.
1262 **/
2a9bf3d0 1263struct lpfc_iocbq *
2e0fef85 1264lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1265{
dea3101e 1266 struct lpfc_iocbq *cmd_iocb;
1267
858c9f6c
JS
1268 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1269 if (cmd_iocb != NULL)
dea3101e 1270 pring->txq_cnt--;
2e0fef85 1271 return cmd_iocb;
dea3101e 1272}
1273
e59058c4 1274/**
3621a710 1275 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
1276 * @phba: Pointer to HBA context object.
1277 * @pring: Pointer to driver SLI ring object.
1278 *
1279 * This function is called with hbalock held and the caller must post the
1280 * iocb without releasing the lock. If the caller releases the lock,
1281 * iocb slot returned by the function is not guaranteed to be available.
1282 * The function returns pointer to the next available iocb slot if there
1283 * is available slot in the ring, else it returns NULL.
1284 * If the get index of the ring is ahead of the put index, the function
1285 * will post an error attention event to the worker thread to take the
1286 * HBA to offline state.
1287 **/
dea3101e 1288static IOCB_t *
1289lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1290{
34b02dcd 1291 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 1292 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e 1293 if ((pring->next_cmdidx == pring->cmdidx) &&
1294 (++pring->next_cmdidx >= max_cmd_idx))
1295 pring->next_cmdidx = 0;
1296
1297 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
1298
1299 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
1300
1301 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
1302 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 1303 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 1304 "is bigger than cmd ring %d\n",
e8b62011 1305 pring->ringno,
dea3101e 1306 pring->local_getidx, max_cmd_idx);
1307
2e0fef85 1308 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1309 /*
1310 * All error attention handlers are posted to
1311 * worker thread
1312 */
1313 phba->work_ha |= HA_ERATT;
1314 phba->work_hs = HS_FFER3;
92d7f7b0 1315
5e9d9b82 1316 lpfc_worker_wake_up(phba);
dea3101e 1317
1318 return NULL;
1319 }
1320
1321 if (pring->local_getidx == pring->next_cmdidx)
1322 return NULL;
1323 }
1324
ed957684 1325 return lpfc_cmd_iocb(phba, pring);
dea3101e 1326}
1327
e59058c4 1328/**
3621a710 1329 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
1330 * @phba: Pointer to HBA context object.
1331 * @iocbq: Pointer to driver iocb object.
1332 *
1333 * This function gets an iotag for the iocb. If there is no unused iotag and
1334 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1335 * array and assigns a new iotag.
1336 * The function returns the allocated iotag if successful, else returns zero.
1337 * Zero is not a valid iotag.
1338 * The caller is not required to hold any lock.
1339 **/
604a3e30 1340uint16_t
2e0fef85 1341lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 1342{
2e0fef85
JS
1343 struct lpfc_iocbq **new_arr;
1344 struct lpfc_iocbq **old_arr;
604a3e30
JB
1345 size_t new_len;
1346 struct lpfc_sli *psli = &phba->sli;
1347 uint16_t iotag;
dea3101e 1348
2e0fef85 1349 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1350 iotag = psli->last_iotag;
1351 if(++iotag < psli->iocbq_lookup_len) {
1352 psli->last_iotag = iotag;
1353 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1354 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1355 iocbq->iotag = iotag;
1356 return iotag;
2e0fef85 1357 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
1358 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1359 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
1360 spin_unlock_irq(&phba->hbalock);
1361 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
1362 GFP_KERNEL);
1363 if (new_arr) {
2e0fef85 1364 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1365 old_arr = psli->iocbq_lookup;
1366 if (new_len <= psli->iocbq_lookup_len) {
1367 /* highly unprobable case */
1368 kfree(new_arr);
1369 iotag = psli->last_iotag;
1370 if(++iotag < psli->iocbq_lookup_len) {
1371 psli->last_iotag = iotag;
1372 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1373 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1374 iocbq->iotag = iotag;
1375 return iotag;
1376 }
2e0fef85 1377 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1378 return 0;
1379 }
1380 if (psli->iocbq_lookup)
1381 memcpy(new_arr, old_arr,
1382 ((psli->last_iotag + 1) *
311464ec 1383 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1384 psli->iocbq_lookup = new_arr;
1385 psli->iocbq_lookup_len = new_len;
1386 psli->last_iotag = iotag;
1387 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1388 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1389 iocbq->iotag = iotag;
1390 kfree(old_arr);
1391 return iotag;
1392 }
8f6d98d2 1393 } else
2e0fef85 1394 spin_unlock_irq(&phba->hbalock);
dea3101e 1395
bc73905a 1396 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
1397 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1398 psli->last_iotag);
dea3101e 1399
604a3e30 1400 return 0;
dea3101e 1401}
1402
e59058c4 1403/**
3621a710 1404 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1405 * @phba: Pointer to HBA context object.
1406 * @pring: Pointer to driver SLI ring object.
1407 * @iocb: Pointer to iocb slot in the ring.
1408 * @nextiocb: Pointer to driver iocb object which need to be
1409 * posted to firmware.
1410 *
1411 * This function is called with hbalock held to post a new iocb to
1412 * the firmware. This function copies the new iocb to ring iocb slot and
1413 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1414 * a completion call back for this iocb else the function will free the
1415 * iocb object.
1416 **/
dea3101e 1417static void
1418lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1419 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1420{
1421 /*
604a3e30 1422 * Set up an iotag
dea3101e 1423 */
604a3e30 1424 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1425
e2a0a9d6 1426
a58cbd52
JS
1427 if (pring->ringno == LPFC_ELS_RING) {
1428 lpfc_debugfs_slow_ring_trc(phba,
1429 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1430 *(((uint32_t *) &nextiocb->iocb) + 4),
1431 *(((uint32_t *) &nextiocb->iocb) + 6),
1432 *(((uint32_t *) &nextiocb->iocb) + 7));
1433 }
1434
dea3101e 1435 /*
1436 * Issue iocb command to adapter
1437 */
92d7f7b0 1438 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1439 wmb();
1440 pring->stats.iocb_cmd++;
1441
1442 /*
1443 * If there is no completion routine to call, we can release the
1444 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1445 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1446 */
1447 if (nextiocb->iocb_cmpl)
1448 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1449 else
2e0fef85 1450 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1451
1452 /*
1453 * Let the HBA know what IOCB slot will be the next one the
1454 * driver will put a command into.
1455 */
1456 pring->cmdidx = pring->next_cmdidx;
ed957684 1457 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1458}
1459
e59058c4 1460/**
3621a710 1461 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1462 * @phba: Pointer to HBA context object.
1463 * @pring: Pointer to driver SLI ring object.
1464 *
1465 * The caller is not required to hold any lock for calling this function.
1466 * This function updates the chip attention bits for the ring to inform firmware
1467 * that there are pending work to be done for this ring and requests an
1468 * interrupt when there is space available in the ring. This function is
1469 * called when the driver is unable to post more iocbs to the ring due
1470 * to unavailability of space in the ring.
1471 **/
dea3101e 1472static void
2e0fef85 1473lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1474{
1475 int ringno = pring->ringno;
1476
1477 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1478
1479 wmb();
1480
1481 /*
1482 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1483 * The HBA will tell us when an IOCB entry is available.
1484 */
1485 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1486 readl(phba->CAregaddr); /* flush */
1487
1488 pring->stats.iocb_cmd_full++;
1489}
1490
e59058c4 1491/**
3621a710 1492 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1493 * @phba: Pointer to HBA context object.
1494 * @pring: Pointer to driver SLI ring object.
1495 *
1496 * This function updates the chip attention register bit for the
1497 * given ring to inform HBA that there is more work to be done
1498 * in this ring. The caller is not required to hold any lock.
1499 **/
dea3101e 1500static void
2e0fef85 1501lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1502{
1503 int ringno = pring->ringno;
1504
1505 /*
1506 * Tell the HBA that there is work to do in this ring.
1507 */
34b02dcd
JS
1508 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1509 wmb();
1510 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1511 readl(phba->CAregaddr); /* flush */
1512 }
dea3101e 1513}
1514
e59058c4 1515/**
3621a710 1516 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1517 * @phba: Pointer to HBA context object.
1518 * @pring: Pointer to driver SLI ring object.
1519 *
1520 * This function is called with hbalock held to post pending iocbs
1521 * in the txq to the firmware. This function is called when driver
1522 * detects space available in the ring.
1523 **/
dea3101e 1524static void
2e0fef85 1525lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1526{
1527 IOCB_t *iocb;
1528 struct lpfc_iocbq *nextiocb;
1529
1530 /*
1531 * Check to see if:
1532 * (a) there is anything on the txq to send
1533 * (b) link is up
1534 * (c) link attention events can be processed (fcp ring only)
1535 * (d) IOCB processing is not blocked by the outstanding mbox command.
1536 */
1537 if (pring->txq_cnt &&
2e0fef85 1538 lpfc_is_link_up(phba) &&
dea3101e 1539 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1540 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1541
1542 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1543 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1544 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1545
1546 if (iocb)
1547 lpfc_sli_update_ring(phba, pring);
1548 else
1549 lpfc_sli_update_full_ring(phba, pring);
1550 }
1551
1552 return;
1553}
1554
e59058c4 1555/**
3621a710 1556 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1557 * @phba: Pointer to HBA context object.
1558 * @hbqno: HBQ number.
1559 *
1560 * This function is called with hbalock held to get the next
1561 * available slot for the given HBQ. If there is free slot
1562 * available for the HBQ it will return pointer to the next available
1563 * HBQ entry else it will return NULL.
1564 **/
a6ababd2 1565static struct lpfc_hbq_entry *
ed957684
JS
1566lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1567{
1568 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1569
1570 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1571 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1572 hbqp->next_hbqPutIdx = 0;
1573
1574 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1575 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1576 uint32_t getidx = le32_to_cpu(raw_index);
1577
1578 hbqp->local_hbqGetIdx = getidx;
1579
1580 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1581 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1582 LOG_SLI | LOG_VPORT,
e8b62011 1583 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1584 "%u is > than hbqp->entry_count %u\n",
e8b62011 1585 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1586 hbqp->entry_count);
1587
1588 phba->link_state = LPFC_HBA_ERROR;
1589 return NULL;
1590 }
1591
1592 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1593 return NULL;
1594 }
1595
51ef4c26
JS
1596 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1597 hbqp->hbqPutIdx;
ed957684
JS
1598}
1599
e59058c4 1600/**
3621a710 1601 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1602 * @phba: Pointer to HBA context object.
1603 *
1604 * This function is called with no lock held to free all the
1605 * hbq buffers while uninitializing the SLI interface. It also
1606 * frees the HBQ buffers returned by the firmware but not yet
1607 * processed by the upper layers.
1608 **/
ed957684
JS
1609void
1610lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1611{
92d7f7b0
JS
1612 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1613 struct hbq_dmabuf *hbq_buf;
3163f725 1614 unsigned long flags;
51ef4c26 1615 int i, hbq_count;
3163f725 1616 uint32_t hbqno;
ed957684 1617
51ef4c26 1618 hbq_count = lpfc_sli_hbq_count();
ed957684 1619 /* Return all memory used by all HBQs */
3163f725 1620 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1621 for (i = 0; i < hbq_count; ++i) {
1622 list_for_each_entry_safe(dmabuf, next_dmabuf,
1623 &phba->hbqs[i].hbq_buffer_list, list) {
1624 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1625 list_del(&hbq_buf->dbuf.list);
1626 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1627 }
a8adb832 1628 phba->hbqs[i].buffer_count = 0;
ed957684 1629 }
3163f725 1630 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1631 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1632 list) {
3163f725
JS
1633 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1634 list_del(&hbq_buf->dbuf.list);
1635 if (hbq_buf->tag == -1) {
1636 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1637 (phba, hbq_buf);
1638 } else {
1639 hbqno = hbq_buf->tag >> 16;
1640 if (hbqno >= LPFC_MAX_HBQS)
1641 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1642 (phba, hbq_buf);
1643 else
1644 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1645 hbq_buf);
1646 }
1647 }
1648
1649 /* Mark the HBQs not in use */
1650 phba->hbq_in_use = 0;
1651 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1652}
1653
e59058c4 1654/**
3621a710 1655 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1656 * @phba: Pointer to HBA context object.
1657 * @hbqno: HBQ number.
1658 * @hbq_buf: Pointer to HBQ buffer.
1659 *
1660 * This function is called with the hbalock held to post a
1661 * hbq buffer to the firmware. If the function finds an empty
1662 * slot in the HBQ, it will post the buffer. The function will return
1663 * pointer to the hbq entry if it successfully post the buffer
1664 * else it will return NULL.
1665 **/
3772a991 1666static int
ed957684 1667lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1668 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1669{
1670 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1671}
1672
1673/**
1674 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1675 * @phba: Pointer to HBA context object.
1676 * @hbqno: HBQ number.
1677 * @hbq_buf: Pointer to HBQ buffer.
1678 *
1679 * This function is called with the hbalock held to post a hbq buffer to the
1680 * firmware. If the function finds an empty slot in the HBQ, it will post the
1681 * buffer and place it on the hbq_buffer_list. The function will return zero if
1682 * it successfully post the buffer else it will return an error.
1683 **/
1684static int
1685lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1686 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1687{
1688 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1689 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1690
1691 /* Get next HBQ entry slot to use */
1692 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1693 if (hbqe) {
1694 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1695
92d7f7b0
JS
1696 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1697 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1698 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1699 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1700 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1701 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1702 /* Sync SLIM */
ed957684
JS
1703 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1704 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1705 /* flush */
ed957684 1706 readl(phba->hbq_put + hbqno);
51ef4c26 1707 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1708 return 0;
1709 } else
1710 return -ENOMEM;
ed957684
JS
1711}
1712
4f774513
JS
1713/**
1714 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1715 * @phba: Pointer to HBA context object.
1716 * @hbqno: HBQ number.
1717 * @hbq_buf: Pointer to HBQ buffer.
1718 *
1719 * This function is called with the hbalock held to post an RQE to the SLI4
1720 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1721 * the hbq_buffer_list and return zero, otherwise it will return an error.
1722 **/
1723static int
1724lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1725 struct hbq_dmabuf *hbq_buf)
1726{
1727 int rc;
1728 struct lpfc_rqe hrqe;
1729 struct lpfc_rqe drqe;
1730
1731 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1732 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1733 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1734 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1735 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1736 &hrqe, &drqe);
1737 if (rc < 0)
1738 return rc;
1739 hbq_buf->tag = rc;
1740 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1741 return 0;
1742}
1743
e59058c4 1744/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1745static struct lpfc_hbq_init lpfc_els_hbq = {
1746 .rn = 1,
def9c7a9 1747 .entry_count = 256,
92d7f7b0
JS
1748 .mask_count = 0,
1749 .profile = 0,
51ef4c26 1750 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1751 .buffer_count = 0,
a257bf90
JS
1752 .init_count = 40,
1753 .add_count = 40,
92d7f7b0 1754};
ed957684 1755
e59058c4 1756/* HBQ for the extra ring if needed */
51ef4c26
JS
1757static struct lpfc_hbq_init lpfc_extra_hbq = {
1758 .rn = 1,
1759 .entry_count = 200,
1760 .mask_count = 0,
1761 .profile = 0,
1762 .ring_mask = (1 << LPFC_EXTRA_RING),
1763 .buffer_count = 0,
1764 .init_count = 0,
1765 .add_count = 5,
1766};
1767
e59058c4 1768/* Array of HBQs */
78b2d852 1769struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1770 &lpfc_els_hbq,
51ef4c26 1771 &lpfc_extra_hbq,
92d7f7b0 1772};
ed957684 1773
e59058c4 1774/**
3621a710 1775 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1776 * @phba: Pointer to HBA context object.
1777 * @hbqno: HBQ number.
1778 * @count: Number of HBQ buffers to be posted.
1779 *
d7c255b2
JS
1780 * This function is called with no lock held to post more hbq buffers to the
1781 * given HBQ. The function returns the number of HBQ buffers successfully
1782 * posted.
e59058c4 1783 **/
311464ec 1784static int
92d7f7b0 1785lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1786{
d7c255b2 1787 uint32_t i, posted = 0;
3163f725 1788 unsigned long flags;
92d7f7b0 1789 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1790 LIST_HEAD(hbq_buf_list);
eafe1df9 1791 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1792 return 0;
51ef4c26 1793
d7c255b2
JS
1794 if ((phba->hbqs[hbqno].buffer_count + count) >
1795 lpfc_hbq_defs[hbqno]->entry_count)
1796 count = lpfc_hbq_defs[hbqno]->entry_count -
1797 phba->hbqs[hbqno].buffer_count;
1798 if (!count)
1799 return 0;
1800 /* Allocate HBQ entries */
1801 for (i = 0; i < count; i++) {
1802 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1803 if (!hbq_buffer)
1804 break;
1805 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1806 }
3163f725
JS
1807 /* Check whether HBQ is still in use */
1808 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1809 if (!phba->hbq_in_use)
d7c255b2
JS
1810 goto err;
1811 while (!list_empty(&hbq_buf_list)) {
1812 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1813 dbuf.list);
1814 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1815 (hbqno << 16));
3772a991 1816 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1817 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1818 posted++;
1819 } else
51ef4c26 1820 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1821 }
3163f725 1822 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1823 return posted;
1824err:
eafe1df9 1825 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1826 while (!list_empty(&hbq_buf_list)) {
1827 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1828 dbuf.list);
1829 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1830 }
1831 return 0;
ed957684
JS
1832}
1833
e59058c4 1834/**
3621a710 1835 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1836 * @phba: Pointer to HBA context object.
1837 * @qno: HBQ number.
1838 *
1839 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1840 * is called with no lock held. The function returns the number of HBQ entries
1841 * successfully allocated.
e59058c4 1842 **/
92d7f7b0
JS
1843int
1844lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1845{
def9c7a9
JS
1846 if (phba->sli_rev == LPFC_SLI_REV4)
1847 return 0;
1848 else
1849 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1850 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1851}
ed957684 1852
e59058c4 1853/**
3621a710 1854 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1855 * @phba: Pointer to HBA context object.
1856 * @qno: HBQ queue number.
1857 *
1858 * This function is called from SLI initialization code path with
1859 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1860 * function returns the number of HBQ entries successfully allocated.
e59058c4 1861 **/
a6ababd2 1862static int
92d7f7b0
JS
1863lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1864{
def9c7a9
JS
1865 if (phba->sli_rev == LPFC_SLI_REV4)
1866 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
73d91e50 1867 lpfc_hbq_defs[qno]->entry_count);
def9c7a9
JS
1868 else
1869 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1870 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1871}
1872
3772a991
JS
1873/**
1874 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1875 * @phba: Pointer to HBA context object.
1876 * @hbqno: HBQ number.
1877 *
1878 * This function removes the first hbq buffer on an hbq list and returns a
1879 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1880 **/
1881static struct hbq_dmabuf *
1882lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1883{
1884 struct lpfc_dmabuf *d_buf;
1885
1886 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1887 if (!d_buf)
1888 return NULL;
1889 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1890}
1891
e59058c4 1892/**
3621a710 1893 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1894 * @phba: Pointer to HBA context object.
1895 * @tag: Tag of the hbq buffer.
1896 *
1897 * This function is called with hbalock held. This function searches
1898 * for the hbq buffer associated with the given tag in the hbq buffer
1899 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1900 * it returns NULL.
1901 **/
a6ababd2 1902static struct hbq_dmabuf *
92d7f7b0 1903lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1904{
92d7f7b0
JS
1905 struct lpfc_dmabuf *d_buf;
1906 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1907 uint32_t hbqno;
1908
1909 hbqno = tag >> 16;
a0a74e45 1910 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1911 return NULL;
ed957684 1912
3772a991 1913 spin_lock_irq(&phba->hbalock);
51ef4c26 1914 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1915 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1916 if (hbq_buf->tag == tag) {
3772a991 1917 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1918 return hbq_buf;
ed957684
JS
1919 }
1920 }
3772a991 1921 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1922 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1923 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1924 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1925 return NULL;
ed957684
JS
1926}
1927
e59058c4 1928/**
3621a710 1929 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1930 * @phba: Pointer to HBA context object.
1931 * @hbq_buffer: Pointer to HBQ buffer.
1932 *
1933 * This function is called with hbalock. This function gives back
1934 * the hbq buffer to firmware. If the HBQ does not have space to
1935 * post the buffer, it will free the buffer.
1936 **/
ed957684 1937void
51ef4c26 1938lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1939{
1940 uint32_t hbqno;
1941
51ef4c26
JS
1942 if (hbq_buffer) {
1943 hbqno = hbq_buffer->tag >> 16;
3772a991 1944 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1945 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1946 }
1947}
1948
e59058c4 1949/**
3621a710 1950 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1951 * @mbxCommand: mailbox command code.
1952 *
1953 * This function is called by the mailbox event handler function to verify
1954 * that the completed mailbox command is a legitimate mailbox command. If the
1955 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1956 * and the mailbox event handler will take the HBA offline.
1957 **/
dea3101e 1958static int
1959lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1960{
1961 uint8_t ret;
1962
1963 switch (mbxCommand) {
1964 case MBX_LOAD_SM:
1965 case MBX_READ_NV:
1966 case MBX_WRITE_NV:
a8adb832 1967 case MBX_WRITE_VPARMS:
dea3101e 1968 case MBX_RUN_BIU_DIAG:
1969 case MBX_INIT_LINK:
1970 case MBX_DOWN_LINK:
1971 case MBX_CONFIG_LINK:
1972 case MBX_CONFIG_RING:
1973 case MBX_RESET_RING:
1974 case MBX_READ_CONFIG:
1975 case MBX_READ_RCONFIG:
1976 case MBX_READ_SPARM:
1977 case MBX_READ_STATUS:
1978 case MBX_READ_RPI:
1979 case MBX_READ_XRI:
1980 case MBX_READ_REV:
1981 case MBX_READ_LNK_STAT:
1982 case MBX_REG_LOGIN:
1983 case MBX_UNREG_LOGIN:
dea3101e 1984 case MBX_CLEAR_LA:
1985 case MBX_DUMP_MEMORY:
1986 case MBX_DUMP_CONTEXT:
1987 case MBX_RUN_DIAGS:
1988 case MBX_RESTART:
1989 case MBX_UPDATE_CFG:
1990 case MBX_DOWN_LOAD:
1991 case MBX_DEL_LD_ENTRY:
1992 case MBX_RUN_PROGRAM:
1993 case MBX_SET_MASK:
09372820 1994 case MBX_SET_VARIABLE:
dea3101e 1995 case MBX_UNREG_D_ID:
41415862 1996 case MBX_KILL_BOARD:
dea3101e 1997 case MBX_CONFIG_FARP:
41415862 1998 case MBX_BEACON:
dea3101e 1999 case MBX_LOAD_AREA:
2000 case MBX_RUN_BIU_DIAG64:
2001 case MBX_CONFIG_PORT:
2002 case MBX_READ_SPARM64:
2003 case MBX_READ_RPI64:
2004 case MBX_REG_LOGIN64:
76a95d75 2005 case MBX_READ_TOPOLOGY:
09372820 2006 case MBX_WRITE_WWN:
dea3101e 2007 case MBX_SET_DEBUG:
2008 case MBX_LOAD_EXP_ROM:
57127f15 2009 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
2010 case MBX_REG_VPI:
2011 case MBX_UNREG_VPI:
858c9f6c 2012 case MBX_HEARTBEAT:
84774a4d
JS
2013 case MBX_PORT_CAPABILITIES:
2014 case MBX_PORT_IOV_CONTROL:
04c68496
JS
2015 case MBX_SLI4_CONFIG:
2016 case MBX_SLI4_REQ_FTRS:
2017 case MBX_REG_FCFI:
2018 case MBX_UNREG_FCFI:
2019 case MBX_REG_VFI:
2020 case MBX_UNREG_VFI:
2021 case MBX_INIT_VPI:
2022 case MBX_INIT_VFI:
2023 case MBX_RESUME_RPI:
c7495937
JS
2024 case MBX_READ_EVENT_LOG_STATUS:
2025 case MBX_READ_EVENT_LOG:
dcf2a4e0
JS
2026 case MBX_SECURITY_MGMT:
2027 case MBX_AUTH_PORT:
dea3101e 2028 ret = mbxCommand;
2029 break;
2030 default:
2031 ret = MBX_SHUTDOWN;
2032 break;
2033 }
2e0fef85 2034 return ret;
dea3101e 2035}
e59058c4
JS
2036
2037/**
3621a710 2038 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
2039 * @phba: Pointer to HBA context object.
2040 * @pmboxq: Pointer to mailbox command.
2041 *
2042 * This is completion handler function for mailbox commands issued from
2043 * lpfc_sli_issue_mbox_wait function. This function is called by the
2044 * mailbox event handler function with no lock held. This function
2045 * will wake up thread waiting on the wait queue pointed by context1
2046 * of the mailbox.
2047 **/
04c68496 2048void
2e0fef85 2049lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 2050{
2051 wait_queue_head_t *pdone_q;
858c9f6c 2052 unsigned long drvr_flag;
dea3101e 2053
2054 /*
2055 * If pdone_q is empty, the driver thread gave up waiting and
2056 * continued running.
2057 */
7054a606 2058 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 2059 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e 2060 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2061 if (pdone_q)
2062 wake_up_interruptible(pdone_q);
858c9f6c 2063 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 2064 return;
2065}
2066
e59058c4
JS
2067
2068/**
3621a710 2069 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
2070 * @phba: Pointer to HBA context object.
2071 * @pmb: Pointer to mailbox object.
2072 *
2073 * This function is the default mailbox completion handler. It
2074 * frees the memory resources associated with the completed mailbox
2075 * command. If the completed command is a REG_LOGIN mailbox command,
2076 * this function will issue a UREG_LOGIN to re-claim the RPI.
2077 **/
dea3101e 2078void
2e0fef85 2079lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 2080{
d439d286 2081 struct lpfc_vport *vport = pmb->vport;
dea3101e 2082 struct lpfc_dmabuf *mp;
d439d286 2083 struct lpfc_nodelist *ndlp;
5af5eee7 2084 struct Scsi_Host *shost;
04c68496 2085 uint16_t rpi, vpi;
7054a606
JS
2086 int rc;
2087
dea3101e 2088 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 2089
dea3101e 2090 if (mp) {
2091 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2092 kfree(mp);
2093 }
7054a606
JS
2094
2095 /*
2096 * If a REG_LOGIN succeeded after node is destroyed or node
2097 * is in re-discovery driver need to cleanup the RPI.
2098 */
2e0fef85 2099 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
2100 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2101 !pmb->u.mb.mbxStatus) {
2102 rpi = pmb->u.mb.un.varWords[0];
6d368e53 2103 vpi = pmb->u.mb.un.varRegLogin.vpi;
04c68496 2104 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 2105 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
2106 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2107 if (rc != MBX_NOT_FINISHED)
2108 return;
2109 }
2110
695a814e
JS
2111 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2112 !(phba->pport->load_flag & FC_UNLOADING) &&
2113 !pmb->u.mb.mbxStatus) {
5af5eee7
JS
2114 shost = lpfc_shost_from_vport(vport);
2115 spin_lock_irq(shost->host_lock);
2116 vport->vpi_state |= LPFC_VPI_REGISTERED;
2117 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2118 spin_unlock_irq(shost->host_lock);
695a814e
JS
2119 }
2120
d439d286
JS
2121 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2122 ndlp = (struct lpfc_nodelist *)pmb->context2;
2123 lpfc_nlp_put(ndlp);
2124 pmb->context2 = NULL;
2125 }
2126
dcf2a4e0
JS
2127 /* Check security permission status on INIT_LINK mailbox command */
2128 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2129 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2130 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2131 "2860 SLI authentication is required "
2132 "for INIT_LINK but has not done yet\n");
2133
04c68496
JS
2134 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2135 lpfc_sli4_mbox_cmd_free(phba, pmb);
2136 else
2137 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 2138}
2139
e59058c4 2140/**
3621a710 2141 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
2142 * @phba: Pointer to HBA context object.
2143 *
2144 * This function is called with no lock held. This function processes all
2145 * the completed mailbox commands and gives it to upper layers. The interrupt
2146 * service routine processes mailbox completion interrupt and adds completed
2147 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2148 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2149 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2150 * function returns the mailbox commands to the upper layer by calling the
2151 * completion handler function of each mailbox.
2152 **/
dea3101e 2153int
2e0fef85 2154lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 2155{
92d7f7b0 2156 MAILBOX_t *pmbox;
dea3101e 2157 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
2158 int rc;
2159 LIST_HEAD(cmplq);
dea3101e 2160
2161 phba->sli.slistat.mbox_event++;
2162
92d7f7b0
JS
2163 /* Get all completed mailboxe buffers into the cmplq */
2164 spin_lock_irq(&phba->hbalock);
2165 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2166 spin_unlock_irq(&phba->hbalock);
dea3101e 2167
92d7f7b0
JS
2168 /* Get a Mailbox buffer to setup mailbox commands for callback */
2169 do {
2170 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2171 if (pmb == NULL)
2172 break;
2e0fef85 2173
04c68496 2174 pmbox = &pmb->u.mb;
dea3101e 2175
858c9f6c
JS
2176 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2177 if (pmb->vport) {
2178 lpfc_debugfs_disc_trc(pmb->vport,
2179 LPFC_DISC_TRC_MBOX_VPORT,
2180 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2181 (uint32_t)pmbox->mbxCommand,
2182 pmbox->un.varWords[0],
2183 pmbox->un.varWords[1]);
2184 }
2185 else {
2186 lpfc_debugfs_disc_trc(phba->pport,
2187 LPFC_DISC_TRC_MBOX,
2188 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2189 (uint32_t)pmbox->mbxCommand,
2190 pmbox->un.varWords[0],
2191 pmbox->un.varWords[1]);
2192 }
2193 }
2194
dea3101e 2195 /*
2196 * It is a fatal error if unknown mbox command completion.
2197 */
2198 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2199 MBX_SHUTDOWN) {
af901ca1 2200 /* Unknown mailbox command compl */
92d7f7b0 2201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 2202 "(%d):0323 Unknown Mailbox command "
a183a15f 2203 "x%x (x%x/x%x) Cmpl\n",
92d7f7b0 2204 pmb->vport ? pmb->vport->vpi : 0,
04c68496 2205 pmbox->mbxCommand,
a183a15f
JS
2206 lpfc_sli_config_mbox_subsys_get(phba,
2207 pmb),
2208 lpfc_sli_config_mbox_opcode_get(phba,
2209 pmb));
2e0fef85 2210 phba->link_state = LPFC_HBA_ERROR;
dea3101e 2211 phba->work_hs = HS_FFER3;
2212 lpfc_handle_eratt(phba);
92d7f7b0 2213 continue;
dea3101e 2214 }
2215
dea3101e 2216 if (pmbox->mbxStatus) {
2217 phba->sli.slistat.mbox_stat_err++;
2218 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2219 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0 2220 lpfc_printf_log(phba, KERN_INFO,
a183a15f
JS
2221 LOG_MBOX | LOG_SLI,
2222 "(%d):0305 Mbox cmd cmpl "
2223 "error - RETRYing Data: x%x "
2224 "(x%x/x%x) x%x x%x x%x\n",
2225 pmb->vport ? pmb->vport->vpi : 0,
2226 pmbox->mbxCommand,
2227 lpfc_sli_config_mbox_subsys_get(phba,
2228 pmb),
2229 lpfc_sli_config_mbox_opcode_get(phba,
2230 pmb),
2231 pmbox->mbxStatus,
2232 pmbox->un.varWords[0],
2233 pmb->vport->port_state);
dea3101e 2234 pmbox->mbxStatus = 0;
2235 pmbox->mbxOwner = OWN_HOST;
dea3101e 2236 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 2237 if (rc != MBX_NOT_FINISHED)
92d7f7b0 2238 continue;
dea3101e 2239 }
2240 }
2241
2242 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 2243 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 2244 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
dea3101e 2245 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 2246 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 2247 pmbox->mbxCommand,
a183a15f
JS
2248 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2249 lpfc_sli_config_mbox_opcode_get(phba, pmb),
dea3101e 2250 pmb->mbox_cmpl,
2251 *((uint32_t *) pmbox),
2252 pmbox->un.varWords[0],
2253 pmbox->un.varWords[1],
2254 pmbox->un.varWords[2],
2255 pmbox->un.varWords[3],
2256 pmbox->un.varWords[4],
2257 pmbox->un.varWords[5],
2258 pmbox->un.varWords[6],
2259 pmbox->un.varWords[7]);
2260
92d7f7b0 2261 if (pmb->mbox_cmpl)
dea3101e 2262 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
2263 } while (1);
2264 return 0;
2265}
dea3101e 2266
e59058c4 2267/**
3621a710 2268 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
2269 * @phba: Pointer to HBA context object.
2270 * @pring: Pointer to driver SLI ring object.
2271 * @tag: buffer tag.
2272 *
2273 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2274 * is set in the tag the buffer is posted for a particular exchange,
2275 * the function will return the buffer without replacing the buffer.
2276 * If the buffer is for unsolicited ELS or CT traffic, this function
2277 * returns the buffer and also posts another buffer to the firmware.
2278 **/
76bb24ef
JS
2279static struct lpfc_dmabuf *
2280lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
2281 struct lpfc_sli_ring *pring,
2282 uint32_t tag)
76bb24ef 2283{
9f1e1b50
JS
2284 struct hbq_dmabuf *hbq_entry;
2285
76bb24ef
JS
2286 if (tag & QUE_BUFTAG_BIT)
2287 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
2288 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2289 if (!hbq_entry)
2290 return NULL;
2291 return &hbq_entry->dbuf;
76bb24ef 2292}
57127f15 2293
3772a991
JS
2294/**
2295 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2296 * @phba: Pointer to HBA context object.
2297 * @pring: Pointer to driver SLI ring object.
2298 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2299 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2300 * @fch_type: the type for the first frame of the sequence.
2301 *
2302 * This function is called with no lock held. This function uses the r_ctl and
2303 * type of the received sequence to find the correct callback function to call
2304 * to process the sequence.
2305 **/
2306static int
2307lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2308 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2309 uint32_t fch_type)
2310{
2311 int i;
2312
2313 /* unSolicited Responses */
2314 if (pring->prt[0].profile) {
2315 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2316 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2317 saveq);
2318 return 1;
2319 }
2320 /* We must search, based on rctl / type
2321 for the right routine */
2322 for (i = 0; i < pring->num_mask; i++) {
2323 if ((pring->prt[i].rctl == fch_r_ctl) &&
2324 (pring->prt[i].type == fch_type)) {
2325 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2326 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2327 (phba, pring, saveq);
2328 return 1;
2329 }
2330 }
2331 return 0;
2332}
e59058c4
JS
2333
2334/**
3621a710 2335 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
2336 * @phba: Pointer to HBA context object.
2337 * @pring: Pointer to driver SLI ring object.
2338 * @saveq: Pointer to the unsolicited iocb.
2339 *
2340 * This function is called with no lock held by the ring event handler
2341 * when there is an unsolicited iocb posted to the response ring by the
2342 * firmware. This function gets the buffer associated with the iocbs
2343 * and calls the event handler for the ring. This function handles both
2344 * qring buffers and hbq buffers.
2345 * When the function returns 1 the caller can free the iocb object otherwise
2346 * upper layer functions will free the iocb objects.
2347 **/
dea3101e 2348static int
2349lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2350 struct lpfc_iocbq *saveq)
2351{
2352 IOCB_t * irsp;
2353 WORD5 * w5p;
2354 uint32_t Rctl, Type;
3772a991 2355 uint32_t match;
76bb24ef 2356 struct lpfc_iocbq *iocbq;
3163f725 2357 struct lpfc_dmabuf *dmzbuf;
dea3101e 2358
2359 match = 0;
2360 irsp = &(saveq->iocb);
57127f15
JS
2361
2362 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2363 if (pring->lpfc_sli_rcv_async_status)
2364 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2365 else
2366 lpfc_printf_log(phba,
2367 KERN_WARNING,
2368 LOG_SLI,
2369 "0316 Ring %d handler: unexpected "
2370 "ASYNC_STATUS iocb received evt_code "
2371 "0x%x\n",
2372 pring->ringno,
2373 irsp->un.asyncstat.evt_code);
2374 return 1;
2375 }
2376
3163f725
JS
2377 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2378 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2379 if (irsp->ulpBdeCount > 0) {
2380 dmzbuf = lpfc_sli_get_buff(phba, pring,
2381 irsp->un.ulpWord[3]);
2382 lpfc_in_buf_free(phba, dmzbuf);
2383 }
2384
2385 if (irsp->ulpBdeCount > 1) {
2386 dmzbuf = lpfc_sli_get_buff(phba, pring,
2387 irsp->unsli3.sli3Words[3]);
2388 lpfc_in_buf_free(phba, dmzbuf);
2389 }
2390
2391 if (irsp->ulpBdeCount > 2) {
2392 dmzbuf = lpfc_sli_get_buff(phba, pring,
2393 irsp->unsli3.sli3Words[7]);
2394 lpfc_in_buf_free(phba, dmzbuf);
2395 }
2396
2397 return 1;
2398 }
2399
92d7f7b0 2400 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2401 if (irsp->ulpBdeCount != 0) {
2402 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2403 irsp->un.ulpWord[3]);
2404 if (!saveq->context2)
2405 lpfc_printf_log(phba,
2406 KERN_ERR,
2407 LOG_SLI,
2408 "0341 Ring %d Cannot find buffer for "
2409 "an unsolicited iocb. tag 0x%x\n",
2410 pring->ringno,
2411 irsp->un.ulpWord[3]);
76bb24ef
JS
2412 }
2413 if (irsp->ulpBdeCount == 2) {
2414 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2415 irsp->unsli3.sli3Words[7]);
2416 if (!saveq->context3)
2417 lpfc_printf_log(phba,
2418 KERN_ERR,
2419 LOG_SLI,
2420 "0342 Ring %d Cannot find buffer for an"
2421 " unsolicited iocb. tag 0x%x\n",
2422 pring->ringno,
2423 irsp->unsli3.sli3Words[7]);
2424 }
2425 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2426 irsp = &(iocbq->iocb);
76bb24ef
JS
2427 if (irsp->ulpBdeCount != 0) {
2428 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2429 irsp->un.ulpWord[3]);
9c2face6 2430 if (!iocbq->context2)
76bb24ef
JS
2431 lpfc_printf_log(phba,
2432 KERN_ERR,
2433 LOG_SLI,
2434 "0343 Ring %d Cannot find "
2435 "buffer for an unsolicited iocb"
2436 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2437 irsp->un.ulpWord[3]);
76bb24ef
JS
2438 }
2439 if (irsp->ulpBdeCount == 2) {
2440 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2441 irsp->unsli3.sli3Words[7]);
9c2face6 2442 if (!iocbq->context3)
76bb24ef
JS
2443 lpfc_printf_log(phba,
2444 KERN_ERR,
2445 LOG_SLI,
2446 "0344 Ring %d Cannot find "
2447 "buffer for an unsolicited "
2448 "iocb. tag 0x%x\n",
2449 pring->ringno,
2450 irsp->unsli3.sli3Words[7]);
2451 }
2452 }
92d7f7b0 2453 }
9c2face6
JS
2454 if (irsp->ulpBdeCount != 0 &&
2455 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2456 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2457 int found = 0;
2458
2459 /* search continue save q for same XRI */
2460 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
7851fe2c
JS
2461 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2462 saveq->iocb.unsli3.rcvsli3.ox_id) {
9c2face6
JS
2463 list_add_tail(&saveq->list, &iocbq->list);
2464 found = 1;
2465 break;
2466 }
2467 }
2468 if (!found)
2469 list_add_tail(&saveq->clist,
2470 &pring->iocb_continue_saveq);
2471 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2472 list_del_init(&iocbq->clist);
2473 saveq = iocbq;
2474 irsp = &(saveq->iocb);
2475 } else
2476 return 0;
2477 }
2478 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2479 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2480 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2481 Rctl = FC_RCTL_ELS_REQ;
2482 Type = FC_TYPE_ELS;
9c2face6
JS
2483 } else {
2484 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2485 Rctl = w5p->hcsw.Rctl;
2486 Type = w5p->hcsw.Type;
2487
2488 /* Firmware Workaround */
2489 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2490 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2491 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2492 Rctl = FC_RCTL_ELS_REQ;
2493 Type = FC_TYPE_ELS;
9c2face6
JS
2494 w5p->hcsw.Rctl = Rctl;
2495 w5p->hcsw.Type = Type;
2496 }
2497 }
92d7f7b0 2498
3772a991 2499 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2500 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2501 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2502 "Type x%x received\n",
e8b62011 2503 pring->ringno, Rctl, Type);
3772a991 2504
92d7f7b0 2505 return 1;
dea3101e 2506}
2507
e59058c4 2508/**
3621a710 2509 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2510 * @phba: Pointer to HBA context object.
2511 * @pring: Pointer to driver SLI ring object.
2512 * @prspiocb: Pointer to response iocb object.
2513 *
2514 * This function looks up the iocb_lookup table to get the command iocb
2515 * corresponding to the given response iocb using the iotag of the
2516 * response iocb. This function is called with the hbalock held.
2517 * This function returns the command iocb object if it finds the command
2518 * iocb else returns NULL.
2519 **/
dea3101e 2520static struct lpfc_iocbq *
2e0fef85
JS
2521lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2522 struct lpfc_sli_ring *pring,
2523 struct lpfc_iocbq *prspiocb)
dea3101e 2524{
dea3101e 2525 struct lpfc_iocbq *cmd_iocb = NULL;
2526 uint16_t iotag;
2527
604a3e30
JB
2528 iotag = prspiocb->iocb.ulpIoTag;
2529
2530 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2531 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2532 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2533 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2534 pring->txcmplq_cnt--;
2535 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2536 }
604a3e30 2537 return cmd_iocb;
dea3101e 2538 }
2539
dea3101e 2540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2541 "0317 iotag x%x is out off "
604a3e30 2542 "range: max iotag x%x wd0 x%x\n",
e8b62011 2543 iotag, phba->sli.last_iotag,
604a3e30 2544 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2545 return NULL;
2546}
2547
3772a991
JS
2548/**
2549 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2550 * @phba: Pointer to HBA context object.
2551 * @pring: Pointer to driver SLI ring object.
2552 * @iotag: IOCB tag.
2553 *
2554 * This function looks up the iocb_lookup table to get the command iocb
2555 * corresponding to the given iotag. This function is called with the
2556 * hbalock held.
2557 * This function returns the command iocb object if it finds the command
2558 * iocb else returns NULL.
2559 **/
2560static struct lpfc_iocbq *
2561lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2562 struct lpfc_sli_ring *pring, uint16_t iotag)
2563{
2564 struct lpfc_iocbq *cmd_iocb;
2565
2566 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2567 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2568 list_del_init(&cmd_iocb->list);
2a9bf3d0
JS
2569 if (cmd_iocb->iocb_flag & LPFC_IO_ON_Q) {
2570 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
2571 pring->txcmplq_cnt--;
2572 }
3772a991
JS
2573 return cmd_iocb;
2574 }
2575
2576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2577 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2578 iotag, phba->sli.last_iotag);
2579 return NULL;
2580}
2581
e59058c4 2582/**
3621a710 2583 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2584 * @phba: Pointer to HBA context object.
2585 * @pring: Pointer to driver SLI ring object.
2586 * @saveq: Pointer to the response iocb to be processed.
2587 *
2588 * This function is called by the ring event handler for non-fcp
2589 * rings when there is a new response iocb in the response ring.
2590 * The caller is not required to hold any locks. This function
2591 * gets the command iocb associated with the response iocb and
2592 * calls the completion handler for the command iocb. If there
2593 * is no completion handler, the function will free the resources
2594 * associated with command iocb. If the response iocb is for
2595 * an already aborted command iocb, the status of the completion
2596 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2597 * This function always returns 1.
2598 **/
dea3101e 2599static int
2e0fef85 2600lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 2601 struct lpfc_iocbq *saveq)
2602{
2e0fef85 2603 struct lpfc_iocbq *cmdiocbp;
dea3101e 2604 int rc = 1;
2605 unsigned long iflag;
2606
2607 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2608 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2609 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2610 spin_unlock_irqrestore(&phba->hbalock, iflag);
2611
dea3101e 2612 if (cmdiocbp) {
2613 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2614 /*
2615 * If an ELS command failed send an event to mgmt
2616 * application.
2617 */
2618 if (saveq->iocb.ulpStatus &&
2619 (pring->ringno == LPFC_ELS_RING) &&
2620 (cmdiocbp->iocb.ulpCommand ==
2621 CMD_ELS_REQUEST64_CR))
2622 lpfc_send_els_failure_event(phba,
2623 cmdiocbp, saveq);
2624
dea3101e 2625 /*
2626 * Post all ELS completions to the worker thread.
2627 * All other are passed to the completion callback.
2628 */
2629 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2630 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2631 (cmdiocbp->iocb_flag &
2632 LPFC_DRIVER_ABORTED)) {
2633 spin_lock_irqsave(&phba->hbalock,
2634 iflag);
07951076
JS
2635 cmdiocbp->iocb_flag &=
2636 ~LPFC_DRIVER_ABORTED;
341af102
JS
2637 spin_unlock_irqrestore(&phba->hbalock,
2638 iflag);
07951076
JS
2639 saveq->iocb.ulpStatus =
2640 IOSTAT_LOCAL_REJECT;
2641 saveq->iocb.un.ulpWord[4] =
2642 IOERR_SLI_ABORTED;
0ff10d46
JS
2643
2644 /* Firmware could still be in progress
2645 * of DMAing payload, so don't free data
2646 * buffer till after a hbeat.
2647 */
341af102
JS
2648 spin_lock_irqsave(&phba->hbalock,
2649 iflag);
0ff10d46 2650 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2651 spin_unlock_irqrestore(&phba->hbalock,
2652 iflag);
2653 }
0f65ff68
JS
2654 if (phba->sli_rev == LPFC_SLI_REV4) {
2655 if (saveq->iocb_flag &
2656 LPFC_EXCHANGE_BUSY) {
2657 /* Set cmdiocb flag for the
2658 * exchange busy so sgl (xri)
2659 * will not be released until
2660 * the abort xri is received
2661 * from hba.
2662 */
2663 spin_lock_irqsave(
2664 &phba->hbalock, iflag);
2665 cmdiocbp->iocb_flag |=
2666 LPFC_EXCHANGE_BUSY;
2667 spin_unlock_irqrestore(
2668 &phba->hbalock, iflag);
2669 }
2670 if (cmdiocbp->iocb_flag &
2671 LPFC_DRIVER_ABORTED) {
2672 /*
2673 * Clear LPFC_DRIVER_ABORTED
2674 * bit in case it was driver
2675 * initiated abort.
2676 */
2677 spin_lock_irqsave(
2678 &phba->hbalock, iflag);
2679 cmdiocbp->iocb_flag &=
2680 ~LPFC_DRIVER_ABORTED;
2681 spin_unlock_irqrestore(
2682 &phba->hbalock, iflag);
2683 cmdiocbp->iocb.ulpStatus =
2684 IOSTAT_LOCAL_REJECT;
2685 cmdiocbp->iocb.un.ulpWord[4] =
2686 IOERR_ABORT_REQUESTED;
2687 /*
2688 * For SLI4, irsiocb contains
2689 * NO_XRI in sli_xritag, it
2690 * shall not affect releasing
2691 * sgl (xri) process.
2692 */
2693 saveq->iocb.ulpStatus =
2694 IOSTAT_LOCAL_REJECT;
2695 saveq->iocb.un.ulpWord[4] =
2696 IOERR_SLI_ABORTED;
2697 spin_lock_irqsave(
2698 &phba->hbalock, iflag);
2699 saveq->iocb_flag |=
2700 LPFC_DELAY_MEM_FREE;
2701 spin_unlock_irqrestore(
2702 &phba->hbalock, iflag);
2703 }
07951076 2704 }
dea3101e 2705 }
2e0fef85 2706 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2707 } else
2708 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 2709 } else {
2710 /*
2711 * Unknown initiating command based on the response iotag.
2712 * This could be the case on the ELS ring because of
2713 * lpfc_els_abort().
2714 */
2715 if (pring->ringno != LPFC_ELS_RING) {
2716 /*
2717 * Ring <ringno> handler: unexpected completion IoTag
2718 * <IoTag>
2719 */
a257bf90 2720 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2721 "0322 Ring %d handler: "
2722 "unexpected completion IoTag x%x "
2723 "Data: x%x x%x x%x x%x\n",
2724 pring->ringno,
2725 saveq->iocb.ulpIoTag,
2726 saveq->iocb.ulpStatus,
2727 saveq->iocb.un.ulpWord[4],
2728 saveq->iocb.ulpCommand,
2729 saveq->iocb.ulpContext);
dea3101e 2730 }
2731 }
68876920 2732
dea3101e 2733 return rc;
2734}
2735
e59058c4 2736/**
3621a710 2737 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2738 * @phba: Pointer to HBA context object.
2739 * @pring: Pointer to driver SLI ring object.
2740 *
2741 * This function is called from the iocb ring event handlers when
2742 * put pointer is ahead of the get pointer for a ring. This function signal
2743 * an error attention condition to the worker thread and the worker
2744 * thread will transition the HBA to offline state.
2745 **/
2e0fef85
JS
2746static void
2747lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2748{
34b02dcd 2749 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2750 /*
025dfdaf 2751 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2752 * rsp ring <portRspMax>
2753 */
2754 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2755 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2756 "is bigger than rsp ring %d\n",
e8b62011 2757 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2758 pring->numRiocb);
2759
2e0fef85 2760 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2761
2762 /*
2763 * All error attention handlers are posted to
2764 * worker thread
2765 */
2766 phba->work_ha |= HA_ERATT;
2767 phba->work_hs = HS_FFER3;
92d7f7b0 2768
5e9d9b82 2769 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2770
2771 return;
2772}
2773
9399627f 2774/**
3621a710 2775 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2776 * @ptr: Pointer to address of HBA context object.
2777 *
2778 * This function is invoked by the Error Attention polling timer when the
2779 * timer times out. It will check the SLI Error Attention register for
2780 * possible attention events. If so, it will post an Error Attention event
2781 * and wake up worker thread to process it. Otherwise, it will set up the
2782 * Error Attention polling timer for the next poll.
2783 **/
2784void lpfc_poll_eratt(unsigned long ptr)
2785{
2786 struct lpfc_hba *phba;
2787 uint32_t eratt = 0;
2788
2789 phba = (struct lpfc_hba *)ptr;
2790
2791 /* Check chip HA register for error event */
2792 eratt = lpfc_sli_check_eratt(phba);
2793
2794 if (eratt)
2795 /* Tell the worker thread there is work to do */
2796 lpfc_worker_wake_up(phba);
2797 else
2798 /* Restart the timer for next eratt poll */
2799 mod_timer(&phba->eratt_poll, jiffies +
2800 HZ * LPFC_ERATT_POLL_INTERVAL);
2801 return;
2802}
2803
875fbdfe 2804
e59058c4 2805/**
3621a710 2806 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2807 * @phba: Pointer to HBA context object.
2808 * @pring: Pointer to driver SLI ring object.
2809 * @mask: Host attention register mask for this ring.
2810 *
2811 * This function is called from the interrupt context when there is a ring
2812 * event for the fcp ring. The caller does not hold any lock.
2813 * The function processes each response iocb in the response ring until it
25985edc 2814 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
e59058c4
JS
2815 * LE bit set. The function will call the completion handler of the command iocb
2816 * if the response iocb indicates a completion for a command iocb or it is
2817 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2818 * function if this is an unsolicited iocb.
dea3101e 2819 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2820 * to check it explicitly.
2821 */
2822int
2e0fef85
JS
2823lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2824 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2825{
34b02dcd 2826 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2827 IOCB_t *irsp = NULL;
87f6eaff 2828 IOCB_t *entry = NULL;
dea3101e 2829 struct lpfc_iocbq *cmdiocbq = NULL;
2830 struct lpfc_iocbq rspiocbq;
dea3101e 2831 uint32_t status;
2832 uint32_t portRspPut, portRspMax;
2833 int rc = 1;
2834 lpfc_iocb_type type;
2835 unsigned long iflag;
2836 uint32_t rsp_cmpl = 0;
dea3101e 2837
2e0fef85 2838 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2839 pring->stats.iocb_event++;
2840
dea3101e 2841 /*
2842 * The next available response entry should never exceed the maximum
2843 * entries. If it does, treat it as an adapter hardware error.
2844 */
2845 portRspMax = pring->numRiocb;
2846 portRspPut = le32_to_cpu(pgp->rspPutInx);
2847 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2848 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2849 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2850 return 1;
2851 }
45ed1190
JS
2852 if (phba->fcp_ring_in_use) {
2853 spin_unlock_irqrestore(&phba->hbalock, iflag);
2854 return 1;
2855 } else
2856 phba->fcp_ring_in_use = 1;
dea3101e 2857
2858 rmb();
2859 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2860 /*
2861 * Fetch an entry off the ring and copy it into a local data
2862 * structure. The copy involves a byte-swap since the
2863 * network byte order and pci byte orders are different.
2864 */
ed957684 2865 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2866 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2867
2868 if (++pring->rspidx >= portRspMax)
2869 pring->rspidx = 0;
2870
87f6eaff
JSEC
2871 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2872 (uint32_t *) &rspiocbq.iocb,
ed957684 2873 phba->iocb_rsp_size);
a4bc3379 2874 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2875 irsp = &rspiocbq.iocb;
2876
dea3101e 2877 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2878 pring->stats.iocb_rsp++;
2879 rsp_cmpl++;
2880
2881 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2882 /*
2883 * If resource errors reported from HBA, reduce
2884 * queuedepths of the SCSI device.
2885 */
2886 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2887 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2888 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2889 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2890 spin_lock_irqsave(&phba->hbalock, iflag);
2891 }
2892
dea3101e 2893 /* Rsp ring <ringno> error: IOCB */
2894 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2895 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2896 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2897 pring->ringno,
92d7f7b0
JS
2898 irsp->un.ulpWord[0],
2899 irsp->un.ulpWord[1],
2900 irsp->un.ulpWord[2],
2901 irsp->un.ulpWord[3],
2902 irsp->un.ulpWord[4],
2903 irsp->un.ulpWord[5],
d7c255b2
JS
2904 *(uint32_t *)&irsp->un1,
2905 *((uint32_t *)&irsp->un1 + 1));
dea3101e 2906 }
2907
2908 switch (type) {
2909 case LPFC_ABORT_IOCB:
2910 case LPFC_SOL_IOCB:
2911 /*
2912 * Idle exchange closed via ABTS from port. No iocb
2913 * resources need to be recovered.
2914 */
2915 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2916 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2917 "0333 IOCB cmd 0x%x"
dca9479b 2918 " processed. Skipping"
92d7f7b0 2919 " completion\n",
dca9479b 2920 irsp->ulpCommand);
dea3101e 2921 break;
2922 }
2923
604a3e30
JB
2924 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2925 &rspiocbq);
0f65ff68
JS
2926 if (unlikely(!cmdiocbq))
2927 break;
2928 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2929 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2930 if (cmdiocbq->iocb_cmpl) {
2931 spin_unlock_irqrestore(&phba->hbalock, iflag);
2932 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2933 &rspiocbq);
2934 spin_lock_irqsave(&phba->hbalock, iflag);
2935 }
dea3101e 2936 break;
a4bc3379 2937 case LPFC_UNSOL_IOCB:
2e0fef85 2938 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2939 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2940 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2941 break;
dea3101e 2942 default:
2943 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2944 char adaptermsg[LPFC_MAX_ADPTMSG];
2945 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2946 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2947 MAX_MSG_DATA);
898eb71c
JP
2948 dev_warn(&((phba->pcidev)->dev),
2949 "lpfc%d: %s\n",
dea3101e 2950 phba->brd_no, adaptermsg);
2951 } else {
2952 /* Unknown IOCB command */
2953 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2954 "0334 Unknown IOCB command "
92d7f7b0 2955 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2956 type, irsp->ulpCommand,
92d7f7b0
JS
2957 irsp->ulpStatus,
2958 irsp->ulpIoTag,
2959 irsp->ulpContext);
dea3101e 2960 }
2961 break;
2962 }
2963
2964 /*
2965 * The response IOCB has been processed. Update the ring
2966 * pointer in SLIM. If the port response put pointer has not
2967 * been updated, sync the pgp->rspPutInx and fetch the new port
2968 * response put pointer.
2969 */
ed957684 2970 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2971
2972 if (pring->rspidx == portRspPut)
2973 portRspPut = le32_to_cpu(pgp->rspPutInx);
2974 }
2975
2976 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2977 pring->stats.iocb_rsp_full++;
2978 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2979 writel(status, phba->CAregaddr);
2980 readl(phba->CAregaddr);
2981 }
2982 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2983 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2984 pring->stats.iocb_cmd_empty++;
2985
2986 /* Force update of the local copy of cmdGetInx */
2987 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2988 lpfc_sli_resume_iocb(phba, pring);
2989
2990 if ((pring->lpfc_sli_cmd_available))
2991 (pring->lpfc_sli_cmd_available) (phba, pring);
2992
2993 }
2994
45ed1190 2995 phba->fcp_ring_in_use = 0;
2e0fef85 2996 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2997 return rc;
2998}
2999
e59058c4 3000/**
3772a991
JS
3001 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3002 * @phba: Pointer to HBA context object.
3003 * @pring: Pointer to driver SLI ring object.
3004 * @rspiocbp: Pointer to driver response IOCB object.
3005 *
3006 * This function is called from the worker thread when there is a slow-path
3007 * response IOCB to process. This function chains all the response iocbs until
3008 * seeing the iocb with the LE bit set. The function will call
3009 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3010 * completion of a command iocb. The function will call the
3011 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3012 * The function frees the resources or calls the completion handler if this
3013 * iocb is an abort completion. The function returns NULL when the response
3014 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3015 * this function shall chain the iocb on to the iocb_continueq and return the
3016 * response iocb passed in.
3017 **/
3018static struct lpfc_iocbq *
3019lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3020 struct lpfc_iocbq *rspiocbp)
3021{
3022 struct lpfc_iocbq *saveq;
3023 struct lpfc_iocbq *cmdiocbp;
3024 struct lpfc_iocbq *next_iocb;
3025 IOCB_t *irsp = NULL;
3026 uint32_t free_saveq;
3027 uint8_t iocb_cmd_type;
3028 lpfc_iocb_type type;
3029 unsigned long iflag;
3030 int rc;
3031
3032 spin_lock_irqsave(&phba->hbalock, iflag);
3033 /* First add the response iocb to the countinueq list */
3034 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3035 pring->iocb_continueq_cnt++;
3036
70f23fd6 3037 /* Now, determine whether the list is completed for processing */
3772a991
JS
3038 irsp = &rspiocbp->iocb;
3039 if (irsp->ulpLe) {
3040 /*
3041 * By default, the driver expects to free all resources
3042 * associated with this iocb completion.
3043 */
3044 free_saveq = 1;
3045 saveq = list_get_first(&pring->iocb_continueq,
3046 struct lpfc_iocbq, list);
3047 irsp = &(saveq->iocb);
3048 list_del_init(&pring->iocb_continueq);
3049 pring->iocb_continueq_cnt = 0;
3050
3051 pring->stats.iocb_rsp++;
3052
3053 /*
3054 * If resource errors reported from HBA, reduce
3055 * queuedepths of the SCSI device.
3056 */
3057 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3058 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
3059 spin_unlock_irqrestore(&phba->hbalock, iflag);
3060 phba->lpfc_rampdown_queue_depth(phba);
3061 spin_lock_irqsave(&phba->hbalock, iflag);
3062 }
3063
3064 if (irsp->ulpStatus) {
3065 /* Rsp ring <ringno> error: IOCB */
3066 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3067 "0328 Rsp Ring %d error: "
3068 "IOCB Data: "
3069 "x%x x%x x%x x%x "
3070 "x%x x%x x%x x%x "
3071 "x%x x%x x%x x%x "
3072 "x%x x%x x%x x%x\n",
3073 pring->ringno,
3074 irsp->un.ulpWord[0],
3075 irsp->un.ulpWord[1],
3076 irsp->un.ulpWord[2],
3077 irsp->un.ulpWord[3],
3078 irsp->un.ulpWord[4],
3079 irsp->un.ulpWord[5],
3080 *(((uint32_t *) irsp) + 6),
3081 *(((uint32_t *) irsp) + 7),
3082 *(((uint32_t *) irsp) + 8),
3083 *(((uint32_t *) irsp) + 9),
3084 *(((uint32_t *) irsp) + 10),
3085 *(((uint32_t *) irsp) + 11),
3086 *(((uint32_t *) irsp) + 12),
3087 *(((uint32_t *) irsp) + 13),
3088 *(((uint32_t *) irsp) + 14),
3089 *(((uint32_t *) irsp) + 15));
3090 }
3091
3092 /*
3093 * Fetch the IOCB command type and call the correct completion
3094 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3095 * get freed back to the lpfc_iocb_list by the discovery
3096 * kernel thread.
3097 */
3098 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3099 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3100 switch (type) {
3101 case LPFC_SOL_IOCB:
3102 spin_unlock_irqrestore(&phba->hbalock, iflag);
3103 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3104 spin_lock_irqsave(&phba->hbalock, iflag);
3105 break;
3106
3107 case LPFC_UNSOL_IOCB:
3108 spin_unlock_irqrestore(&phba->hbalock, iflag);
3109 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3110 spin_lock_irqsave(&phba->hbalock, iflag);
3111 if (!rc)
3112 free_saveq = 0;
3113 break;
3114
3115 case LPFC_ABORT_IOCB:
3116 cmdiocbp = NULL;
3117 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3118 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3119 saveq);
3120 if (cmdiocbp) {
3121 /* Call the specified completion routine */
3122 if (cmdiocbp->iocb_cmpl) {
3123 spin_unlock_irqrestore(&phba->hbalock,
3124 iflag);
3125 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3126 saveq);
3127 spin_lock_irqsave(&phba->hbalock,
3128 iflag);
3129 } else
3130 __lpfc_sli_release_iocbq(phba,
3131 cmdiocbp);
3132 }
3133 break;
3134
3135 case LPFC_UNKNOWN_IOCB:
3136 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3137 char adaptermsg[LPFC_MAX_ADPTMSG];
3138 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3139 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3140 MAX_MSG_DATA);
3141 dev_warn(&((phba->pcidev)->dev),
3142 "lpfc%d: %s\n",
3143 phba->brd_no, adaptermsg);
3144 } else {
3145 /* Unknown IOCB command */
3146 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3147 "0335 Unknown IOCB "
3148 "command Data: x%x "
3149 "x%x x%x x%x\n",
3150 irsp->ulpCommand,
3151 irsp->ulpStatus,
3152 irsp->ulpIoTag,
3153 irsp->ulpContext);
3154 }
3155 break;
3156 }
3157
3158 if (free_saveq) {
3159 list_for_each_entry_safe(rspiocbp, next_iocb,
3160 &saveq->list, list) {
3161 list_del(&rspiocbp->list);
3162 __lpfc_sli_release_iocbq(phba, rspiocbp);
3163 }
3164 __lpfc_sli_release_iocbq(phba, saveq);
3165 }
3166 rspiocbp = NULL;
3167 }
3168 spin_unlock_irqrestore(&phba->hbalock, iflag);
3169 return rspiocbp;
3170}
3171
3172/**
3173 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
3174 * @phba: Pointer to HBA context object.
3175 * @pring: Pointer to driver SLI ring object.
3176 * @mask: Host attention register mask for this ring.
3177 *
3772a991
JS
3178 * This routine wraps the actual slow_ring event process routine from the
3179 * API jump table function pointer from the lpfc_hba struct.
e59058c4 3180 **/
3772a991 3181void
2e0fef85
JS
3182lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3183 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
3184{
3185 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3186}
3187
3188/**
3189 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3190 * @phba: Pointer to HBA context object.
3191 * @pring: Pointer to driver SLI ring object.
3192 * @mask: Host attention register mask for this ring.
3193 *
3194 * This function is called from the worker thread when there is a ring event
3195 * for non-fcp rings. The caller does not hold any lock. The function will
3196 * remove each response iocb in the response ring and calls the handle
3197 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3198 **/
3199static void
3200lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3201 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 3202{
34b02dcd 3203 struct lpfc_pgp *pgp;
dea3101e 3204 IOCB_t *entry;
3205 IOCB_t *irsp = NULL;
3206 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 3207 uint32_t portRspPut, portRspMax;
dea3101e 3208 unsigned long iflag;
3772a991 3209 uint32_t status;
dea3101e 3210
34b02dcd 3211 pgp = &phba->port_gp[pring->ringno];
2e0fef85 3212 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3213 pring->stats.iocb_event++;
3214
dea3101e 3215 /*
3216 * The next available response entry should never exceed the maximum
3217 * entries. If it does, treat it as an adapter hardware error.
3218 */
3219 portRspMax = pring->numRiocb;
3220 portRspPut = le32_to_cpu(pgp->rspPutInx);
3221 if (portRspPut >= portRspMax) {
3222 /*
025dfdaf 3223 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 3224 * rsp ring <portRspMax>
3225 */
ed957684 3226 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 3227 "0303 Ring %d handler: portRspPut %d "
025dfdaf 3228 "is bigger than rsp ring %d\n",
e8b62011 3229 pring->ringno, portRspPut, portRspMax);
dea3101e 3230
2e0fef85
JS
3231 phba->link_state = LPFC_HBA_ERROR;
3232 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 3233
3234 phba->work_hs = HS_FFER3;
3235 lpfc_handle_eratt(phba);
3236
3772a991 3237 return;
dea3101e 3238 }
3239
3240 rmb();
dea3101e 3241 while (pring->rspidx != portRspPut) {
3242 /*
3243 * Build a completion list and call the appropriate handler.
3244 * The process is to get the next available response iocb, get
3245 * a free iocb from the list, copy the response data into the
3246 * free iocb, insert to the continuation list, and update the
3247 * next response index to slim. This process makes response
3248 * iocb's in the ring available to DMA as fast as possible but
3249 * pays a penalty for a copy operation. Since the iocb is
3250 * only 32 bytes, this penalty is considered small relative to
3251 * the PCI reads for register values and a slim write. When
3252 * the ulpLe field is set, the entire Command has been
3253 * received.
3254 */
ed957684
JS
3255 entry = lpfc_resp_iocb(phba, pring);
3256
858c9f6c 3257 phba->last_completion_time = jiffies;
2e0fef85 3258 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 3259 if (rspiocbp == NULL) {
3260 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 3261 "completion.\n", __func__);
dea3101e 3262 break;
3263 }
3264
ed957684
JS
3265 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3266 phba->iocb_rsp_size);
dea3101e 3267 irsp = &rspiocbp->iocb;
3268
3269 if (++pring->rspidx >= portRspMax)
3270 pring->rspidx = 0;
3271
a58cbd52
JS
3272 if (pring->ringno == LPFC_ELS_RING) {
3273 lpfc_debugfs_slow_ring_trc(phba,
3274 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3275 *(((uint32_t *) irsp) + 4),
3276 *(((uint32_t *) irsp) + 6),
3277 *(((uint32_t *) irsp) + 7));
3278 }
3279
ed957684 3280 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 3281
3772a991
JS
3282 spin_unlock_irqrestore(&phba->hbalock, iflag);
3283 /* Handle the response IOCB */
3284 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3285 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 3286
3287 /*
3288 * If the port response put pointer has not been updated, sync
3289 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3290 * response put pointer.
3291 */
3292 if (pring->rspidx == portRspPut) {
3293 portRspPut = le32_to_cpu(pgp->rspPutInx);
3294 }
3295 } /* while (pring->rspidx != portRspPut) */
3296
92d7f7b0 3297 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 3298 /* At least one response entry has been freed */
3299 pring->stats.iocb_rsp_full++;
3300 /* SET RxRE_RSP in Chip Att register */
3301 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3302 writel(status, phba->CAregaddr);
3303 readl(phba->CAregaddr); /* flush */
3304 }
3305 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3306 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3307 pring->stats.iocb_cmd_empty++;
3308
3309 /* Force update of the local copy of cmdGetInx */
3310 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
3311 lpfc_sli_resume_iocb(phba, pring);
3312
3313 if ((pring->lpfc_sli_cmd_available))
3314 (pring->lpfc_sli_cmd_available) (phba, pring);
3315
3316 }
3317
2e0fef85 3318 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3319 return;
dea3101e 3320}
3321
4f774513
JS
3322/**
3323 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3324 * @phba: Pointer to HBA context object.
3325 * @pring: Pointer to driver SLI ring object.
3326 * @mask: Host attention register mask for this ring.
3327 *
3328 * This function is called from the worker thread when there is a pending
3329 * ELS response iocb on the driver internal slow-path response iocb worker
3330 * queue. The caller does not hold any lock. The function will remove each
3331 * response iocb from the response worker queue and calls the handle
3332 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3333 **/
3334static void
3335lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3336 struct lpfc_sli_ring *pring, uint32_t mask)
3337{
3338 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3339 struct hbq_dmabuf *dmabuf;
3340 struct lpfc_cq_event *cq_event;
4f774513
JS
3341 unsigned long iflag;
3342
45ed1190
JS
3343 spin_lock_irqsave(&phba->hbalock, iflag);
3344 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3345 spin_unlock_irqrestore(&phba->hbalock, iflag);
3346 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
3347 /* Get the response iocb from the head of work queue */
3348 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 3349 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 3350 cq_event, struct lpfc_cq_event, list);
4f774513 3351 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3352
3353 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3354 case CQE_CODE_COMPL_WQE:
3355 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3356 cq_event);
45ed1190
JS
3357 /* Translate ELS WCQE to response IOCBQ */
3358 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3359 irspiocbq);
3360 if (irspiocbq)
3361 lpfc_sli_sp_handle_rspiocb(phba, pring,
3362 irspiocbq);
4d9ab994
JS
3363 break;
3364 case CQE_CODE_RECEIVE:
7851fe2c 3365 case CQE_CODE_RECEIVE_V1:
4d9ab994
JS
3366 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3367 cq_event);
3368 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3369 break;
3370 default:
3371 break;
3372 }
4f774513
JS
3373 }
3374}
3375
e59058c4 3376/**
3621a710 3377 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3378 * @phba: Pointer to HBA context object.
3379 * @pring: Pointer to driver SLI ring object.
3380 *
3381 * This function aborts all iocbs in the given ring and frees all the iocb
3382 * objects in txq. This function issues an abort iocb for all the iocb commands
3383 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3384 * the return of this function. The caller is not required to hold any locks.
3385 **/
2e0fef85 3386void
dea3101e 3387lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3388{
2534ba75 3389 LIST_HEAD(completions);
dea3101e 3390 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3391
92d7f7b0
JS
3392 if (pring->ringno == LPFC_ELS_RING) {
3393 lpfc_fabric_abort_hba(phba);
3394 }
3395
dea3101e 3396 /* Error everything on txq and txcmplq
3397 * First do the txq.
3398 */
2e0fef85 3399 spin_lock_irq(&phba->hbalock);
2534ba75 3400 list_splice_init(&pring->txq, &completions);
dea3101e 3401 pring->txq_cnt = 0;
dea3101e 3402
3403 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3404 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3405 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3406
2e0fef85 3407 spin_unlock_irq(&phba->hbalock);
dea3101e 3408
a257bf90
JS
3409 /* Cancel all the IOCBs from the completions list */
3410 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3411 IOERR_SLI_ABORTED);
dea3101e 3412}
3413
a8e497d5 3414/**
3621a710 3415 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3416 * @phba: Pointer to HBA context object.
3417 *
3418 * This function flushes all iocbs in the fcp ring and frees all the iocb
3419 * objects in txq and txcmplq. This function will not issue abort iocbs
3420 * for all the iocb commands in txcmplq, they will just be returned with
3421 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3422 * slot has been permanently disabled.
3423 **/
3424void
3425lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3426{
3427 LIST_HEAD(txq);
3428 LIST_HEAD(txcmplq);
a8e497d5
JS
3429 struct lpfc_sli *psli = &phba->sli;
3430 struct lpfc_sli_ring *pring;
3431
3432 /* Currently, only one fcp ring */
3433 pring = &psli->ring[psli->fcp_ring];
3434
3435 spin_lock_irq(&phba->hbalock);
3436 /* Retrieve everything on txq */
3437 list_splice_init(&pring->txq, &txq);
3438 pring->txq_cnt = 0;
3439
3440 /* Retrieve everything on the txcmplq */
3441 list_splice_init(&pring->txcmplq, &txcmplq);
3442 pring->txcmplq_cnt = 0;
3443 spin_unlock_irq(&phba->hbalock);
3444
3445 /* Flush the txq */
a257bf90
JS
3446 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3447 IOERR_SLI_DOWN);
a8e497d5
JS
3448
3449 /* Flush the txcmpq */
a257bf90
JS
3450 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3451 IOERR_SLI_DOWN);
a8e497d5
JS
3452}
3453
e59058c4 3454/**
3772a991 3455 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3456 * @phba: Pointer to HBA context object.
3457 * @mask: Bit mask to be checked.
3458 *
3459 * This function reads the host status register and compares
3460 * with the provided bit mask to check if HBA completed
3461 * the restart. This function will wait in a loop for the
3462 * HBA to complete restart. If the HBA does not restart within
3463 * 15 iterations, the function will reset the HBA again. The
3464 * function returns 1 when HBA fail to restart otherwise returns
3465 * zero.
3466 **/
3772a991
JS
3467static int
3468lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3469{
41415862
JW
3470 uint32_t status;
3471 int i = 0;
3472 int retval = 0;
dea3101e 3473
41415862 3474 /* Read the HBA Host Status Register */
9940b97b
JS
3475 if (lpfc_readl(phba->HSregaddr, &status))
3476 return 1;
dea3101e 3477
41415862
JW
3478 /*
3479 * Check status register every 100ms for 5 retries, then every
3480 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3481 * every 2.5 sec for 4.
3482 * Break our of the loop if errors occurred during init.
3483 */
3484 while (((status & mask) != mask) &&
3485 !(status & HS_FFERM) &&
3486 i++ < 20) {
dea3101e 3487
41415862
JW
3488 if (i <= 5)
3489 msleep(10);
3490 else if (i <= 10)
3491 msleep(500);
3492 else
3493 msleep(2500);
dea3101e 3494
41415862 3495 if (i == 15) {
2e0fef85 3496 /* Do post */
92d7f7b0 3497 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3498 lpfc_sli_brdrestart(phba);
3499 }
3500 /* Read the HBA Host Status Register */
9940b97b
JS
3501 if (lpfc_readl(phba->HSregaddr, &status)) {
3502 retval = 1;
3503 break;
3504 }
41415862 3505 }
dea3101e 3506
41415862
JW
3507 /* Check to see if any errors occurred during init */
3508 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3509 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3510 "2751 Adapter failed to restart, "
3511 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3512 status,
3513 readl(phba->MBslimaddr + 0xa8),
3514 readl(phba->MBslimaddr + 0xac));
2e0fef85 3515 phba->link_state = LPFC_HBA_ERROR;
41415862 3516 retval = 1;
dea3101e 3517 }
dea3101e 3518
41415862
JW
3519 return retval;
3520}
dea3101e 3521
da0436e9
JS
3522/**
3523 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3524 * @phba: Pointer to HBA context object.
3525 * @mask: Bit mask to be checked.
3526 *
3527 * This function checks the host status register to check if HBA is
3528 * ready. This function will wait in a loop for the HBA to be ready
3529 * If the HBA is not ready , the function will will reset the HBA PCI
3530 * function again. The function returns 1 when HBA fail to be ready
3531 * otherwise returns zero.
3532 **/
3533static int
3534lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3535{
3536 uint32_t status;
3537 int retval = 0;
3538
3539 /* Read the HBA Host Status Register */
3540 status = lpfc_sli4_post_status_check(phba);
3541
3542 if (status) {
3543 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3544 lpfc_sli_brdrestart(phba);
3545 status = lpfc_sli4_post_status_check(phba);
3546 }
3547
3548 /* Check to see if any errors occurred during init */
3549 if (status) {
3550 phba->link_state = LPFC_HBA_ERROR;
3551 retval = 1;
3552 } else
3553 phba->sli4_hba.intr_enable = 0;
3554
3555 return retval;
3556}
3557
3558/**
3559 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3560 * @phba: Pointer to HBA context object.
3561 * @mask: Bit mask to be checked.
3562 *
3563 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3564 * from the API jump table function pointer from the lpfc_hba struct.
3565 **/
3566int
3567lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3568{
3569 return phba->lpfc_sli_brdready(phba, mask);
3570}
3571
9290831f
JS
3572#define BARRIER_TEST_PATTERN (0xdeadbeef)
3573
e59058c4 3574/**
3621a710 3575 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3576 * @phba: Pointer to HBA context object.
3577 *
3578 * This function is called before resetting an HBA. This
3579 * function requests HBA to quiesce DMAs before a reset.
3580 **/
2e0fef85 3581void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3582{
65a29c16
JS
3583 uint32_t __iomem *resp_buf;
3584 uint32_t __iomem *mbox_buf;
9290831f 3585 volatile uint32_t mbox;
9940b97b 3586 uint32_t hc_copy, ha_copy, resp_data;
9290831f
JS
3587 int i;
3588 uint8_t hdrtype;
3589
3590 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3591 if (hdrtype != 0x80 ||
3592 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3593 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3594 return;
3595
3596 /*
3597 * Tell the other part of the chip to suspend temporarily all
3598 * its DMA activity.
3599 */
65a29c16 3600 resp_buf = phba->MBslimaddr;
9290831f
JS
3601
3602 /* Disable the error attention */
9940b97b
JS
3603 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3604 return;
9290831f
JS
3605 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3606 readl(phba->HCregaddr); /* flush */
2e0fef85 3607 phba->link_flag |= LS_IGNORE_ERATT;
9290831f 3608
9940b97b
JS
3609 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3610 return;
3611 if (ha_copy & HA_ERATT) {
9290831f
JS
3612 /* Clear Chip error bit */
3613 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3614 phba->pport->stopped = 1;
9290831f
JS
3615 }
3616
3617 mbox = 0;
3618 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3619 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3620
3621 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3622 mbox_buf = phba->MBslimaddr;
9290831f
JS
3623 writel(mbox, mbox_buf);
3624
9940b97b
JS
3625 for (i = 0; i < 50; i++) {
3626 if (lpfc_readl((resp_buf + 1), &resp_data))
3627 return;
3628 if (resp_data != ~(BARRIER_TEST_PATTERN))
3629 mdelay(1);
3630 else
3631 break;
3632 }
3633 resp_data = 0;
3634 if (lpfc_readl((resp_buf + 1), &resp_data))
3635 return;
3636 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3637 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3638 phba->pport->stopped)
9290831f
JS
3639 goto restore_hc;
3640 else
3641 goto clear_errat;
3642 }
3643
3644 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
9940b97b
JS
3645 resp_data = 0;
3646 for (i = 0; i < 500; i++) {
3647 if (lpfc_readl(resp_buf, &resp_data))
3648 return;
3649 if (resp_data != mbox)
3650 mdelay(1);
3651 else
3652 break;
3653 }
9290831f
JS
3654
3655clear_errat:
3656
9940b97b
JS
3657 while (++i < 500) {
3658 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3659 return;
3660 if (!(ha_copy & HA_ERATT))
3661 mdelay(1);
3662 else
3663 break;
3664 }
9290831f
JS
3665
3666 if (readl(phba->HAregaddr) & HA_ERATT) {
3667 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3668 phba->pport->stopped = 1;
9290831f
JS
3669 }
3670
3671restore_hc:
2e0fef85 3672 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3673 writel(hc_copy, phba->HCregaddr);
3674 readl(phba->HCregaddr); /* flush */
3675}
3676
e59058c4 3677/**
3621a710 3678 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3679 * @phba: Pointer to HBA context object.
3680 *
3681 * This function issues a kill_board mailbox command and waits for
3682 * the error attention interrupt. This function is called for stopping
3683 * the firmware processing. The caller is not required to hold any
3684 * locks. This function calls lpfc_hba_down_post function to free
3685 * any pending commands after the kill. The function will return 1 when it
3686 * fails to kill the board else will return 0.
3687 **/
41415862 3688int
2e0fef85 3689lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3690{
3691 struct lpfc_sli *psli;
3692 LPFC_MBOXQ_t *pmb;
3693 uint32_t status;
3694 uint32_t ha_copy;
3695 int retval;
3696 int i = 0;
dea3101e 3697
41415862 3698 psli = &phba->sli;
dea3101e 3699
41415862 3700 /* Kill HBA */
ed957684 3701 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3702 "0329 Kill HBA Data: x%x x%x\n",
3703 phba->pport->port_state, psli->sli_flag);
41415862 3704
98c9ea5c
JS
3705 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3706 if (!pmb)
41415862 3707 return 1;
41415862
JW
3708
3709 /* Disable the error attention */
2e0fef85 3710 spin_lock_irq(&phba->hbalock);
9940b97b
JS
3711 if (lpfc_readl(phba->HCregaddr, &status)) {
3712 spin_unlock_irq(&phba->hbalock);
3713 mempool_free(pmb, phba->mbox_mem_pool);
3714 return 1;
3715 }
41415862
JW
3716 status &= ~HC_ERINT_ENA;
3717 writel(status, phba->HCregaddr);
3718 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3719 phba->link_flag |= LS_IGNORE_ERATT;
3720 spin_unlock_irq(&phba->hbalock);
41415862
JW
3721
3722 lpfc_kill_board(phba, pmb);
3723 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3724 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3725
3726 if (retval != MBX_SUCCESS) {
3727 if (retval != MBX_BUSY)
3728 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3729 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3730 "2752 KILL_BOARD command failed retval %d\n",
3731 retval);
2e0fef85
JS
3732 spin_lock_irq(&phba->hbalock);
3733 phba->link_flag &= ~LS_IGNORE_ERATT;
3734 spin_unlock_irq(&phba->hbalock);
41415862
JW
3735 return 1;
3736 }
3737
f4b4c68f
JS
3738 spin_lock_irq(&phba->hbalock);
3739 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3740 spin_unlock_irq(&phba->hbalock);
9290831f 3741
41415862
JW
3742 mempool_free(pmb, phba->mbox_mem_pool);
3743
3744 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3745 * attention every 100ms for 3 seconds. If we don't get ERATT after
3746 * 3 seconds we still set HBA_ERROR state because the status of the
3747 * board is now undefined.
3748 */
9940b97b
JS
3749 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3750 return 1;
41415862
JW
3751 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3752 mdelay(100);
9940b97b
JS
3753 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3754 return 1;
41415862
JW
3755 }
3756
3757 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3758 if (ha_copy & HA_ERATT) {
3759 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3760 phba->pport->stopped = 1;
9290831f 3761 }
2e0fef85 3762 spin_lock_irq(&phba->hbalock);
41415862 3763 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3764 psli->mbox_active = NULL;
2e0fef85
JS
3765 phba->link_flag &= ~LS_IGNORE_ERATT;
3766 spin_unlock_irq(&phba->hbalock);
41415862 3767
41415862 3768 lpfc_hba_down_post(phba);
2e0fef85 3769 phba->link_state = LPFC_HBA_ERROR;
41415862 3770
2e0fef85 3771 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 3772}
3773
e59058c4 3774/**
3772a991 3775 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3776 * @phba: Pointer to HBA context object.
3777 *
3778 * This function resets the HBA by writing HC_INITFF to the control
3779 * register. After the HBA resets, this function resets all the iocb ring
3780 * indices. This function disables PCI layer parity checking during
3781 * the reset.
3782 * This function returns 0 always.
3783 * The caller is not required to hold any locks.
3784 **/
41415862 3785int
2e0fef85 3786lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3787{
41415862 3788 struct lpfc_sli *psli;
dea3101e 3789 struct lpfc_sli_ring *pring;
41415862 3790 uint16_t cfg_value;
dea3101e 3791 int i;
dea3101e 3792
41415862 3793 psli = &phba->sli;
dea3101e 3794
41415862
JW
3795 /* Reset HBA */
3796 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3797 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3798 phba->pport->port_state, psli->sli_flag);
dea3101e 3799
3800 /* perform board reset */
3801 phba->fc_eventTag = 0;
4d9ab994 3802 phba->link_events = 0;
2e0fef85
JS
3803 phba->pport->fc_myDID = 0;
3804 phba->pport->fc_prevDID = 0;
dea3101e 3805
41415862
JW
3806 /* Turn off parity checking and serr during the physical reset */
3807 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3808 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3809 (cfg_value &
3810 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3811
3772a991
JS
3812 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3813
41415862
JW
3814 /* Now toggle INITFF bit in the Host Control Register */
3815 writel(HC_INITFF, phba->HCregaddr);
3816 mdelay(1);
3817 readl(phba->HCregaddr); /* flush */
3818 writel(0, phba->HCregaddr);
3819 readl(phba->HCregaddr); /* flush */
3820
3821 /* Restore PCI cmd register */
3822 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 3823
3824 /* Initialize relevant SLI info */
41415862
JW
3825 for (i = 0; i < psli->num_rings; i++) {
3826 pring = &psli->ring[i];
dea3101e 3827 pring->flag = 0;
3828 pring->rspidx = 0;
3829 pring->next_cmdidx = 0;
3830 pring->local_getidx = 0;
3831 pring->cmdidx = 0;
3832 pring->missbufcnt = 0;
3833 }
dea3101e 3834
2e0fef85 3835 phba->link_state = LPFC_WARM_START;
41415862
JW
3836 return 0;
3837}
3838
e59058c4 3839/**
da0436e9
JS
3840 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3841 * @phba: Pointer to HBA context object.
3842 *
3843 * This function resets a SLI4 HBA. This function disables PCI layer parity
3844 * checking during resets the device. The caller is not required to hold
3845 * any locks.
3846 *
3847 * This function returns 0 always.
3848 **/
3849int
3850lpfc_sli4_brdreset(struct lpfc_hba *phba)
3851{
3852 struct lpfc_sli *psli = &phba->sli;
3853 uint16_t cfg_value;
3854 uint8_t qindx;
3855
3856 /* Reset HBA */
3857 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3858 "0295 Reset HBA Data: x%x x%x\n",
3859 phba->pport->port_state, psli->sli_flag);
3860
3861 /* perform board reset */
3862 phba->fc_eventTag = 0;
4d9ab994 3863 phba->link_events = 0;
da0436e9
JS
3864 phba->pport->fc_myDID = 0;
3865 phba->pport->fc_prevDID = 0;
3866
da0436e9
JS
3867 spin_lock_irq(&phba->hbalock);
3868 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3869 phba->fcf.fcf_flag = 0;
3870 /* Clean up the child queue list for the CQs */
3871 list_del_init(&phba->sli4_hba.mbx_wq->list);
3872 list_del_init(&phba->sli4_hba.els_wq->list);
3873 list_del_init(&phba->sli4_hba.hdr_rq->list);
3874 list_del_init(&phba->sli4_hba.dat_rq->list);
3875 list_del_init(&phba->sli4_hba.mbx_cq->list);
3876 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3877 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3878 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
0558056c
JS
3879 qindx = 0;
3880 do
da0436e9 3881 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
0558056c 3882 while (++qindx < phba->cfg_fcp_eq_count);
da0436e9
JS
3883 spin_unlock_irq(&phba->hbalock);
3884
3885 /* Now physically reset the device */
3886 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3887 "0389 Performing PCI function reset!\n");
be858b65
JS
3888
3889 /* Turn off parity checking and serr during the physical reset */
3890 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3891 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3892 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3893
da0436e9
JS
3894 /* Perform FCoE PCI function reset */
3895 lpfc_pci_function_reset(phba);
3896
be858b65
JS
3897 /* Restore PCI cmd register */
3898 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3899
da0436e9
JS
3900 return 0;
3901}
3902
3903/**
3904 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3905 * @phba: Pointer to HBA context object.
3906 *
3907 * This function is called in the SLI initialization code path to
3908 * restart the HBA. The caller is not required to hold any lock.
3909 * This function writes MBX_RESTART mailbox command to the SLIM and
3910 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3911 * function to free any pending commands. The function enables
3912 * POST only during the first initialization. The function returns zero.
3913 * The function does not guarantee completion of MBX_RESTART mailbox
3914 * command before the return of this function.
3915 **/
da0436e9
JS
3916static int
3917lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3918{
3919 MAILBOX_t *mb;
3920 struct lpfc_sli *psli;
41415862
JW
3921 volatile uint32_t word0;
3922 void __iomem *to_slim;
0d878419 3923 uint32_t hba_aer_enabled;
41415862 3924
2e0fef85 3925 spin_lock_irq(&phba->hbalock);
41415862 3926
0d878419
JS
3927 /* Take PCIe device Advanced Error Reporting (AER) state */
3928 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3929
41415862
JW
3930 psli = &phba->sli;
3931
3932 /* Restart HBA */
3933 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3934 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3935 phba->pport->port_state, psli->sli_flag);
41415862
JW
3936
3937 word0 = 0;
3938 mb = (MAILBOX_t *) &word0;
3939 mb->mbxCommand = MBX_RESTART;
3940 mb->mbxHc = 1;
3941
9290831f
JS
3942 lpfc_reset_barrier(phba);
3943
41415862
JW
3944 to_slim = phba->MBslimaddr;
3945 writel(*(uint32_t *) mb, to_slim);
3946 readl(to_slim); /* flush */
3947
3948 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3949 if (phba->pport->port_state)
41415862 3950 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3951 else
41415862 3952 word0 = 0; /* This is really setting up word1 */
65a29c16 3953 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3954 writel(*(uint32_t *) mb, to_slim);
3955 readl(to_slim); /* flush */
dea3101e 3956
41415862 3957 lpfc_sli_brdreset(phba);
2e0fef85
JS
3958 phba->pport->stopped = 0;
3959 phba->link_state = LPFC_INIT_START;
da0436e9 3960 phba->hba_flag = 0;
2e0fef85 3961 spin_unlock_irq(&phba->hbalock);
41415862 3962
64ba8818
JS
3963 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3964 psli->stats_start = get_seconds();
3965
eaf15d5b
JS
3966 /* Give the INITFF and Post time to settle. */
3967 mdelay(100);
41415862 3968
0d878419
JS
3969 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3970 if (hba_aer_enabled)
3971 pci_disable_pcie_error_reporting(phba->pcidev);
3972
41415862 3973 lpfc_hba_down_post(phba);
dea3101e 3974
3975 return 0;
3976}
3977
da0436e9
JS
3978/**
3979 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3980 * @phba: Pointer to HBA context object.
3981 *
3982 * This function is called in the SLI initialization code path to restart
3983 * a SLI4 HBA. The caller is not required to hold any lock.
3984 * At the end of the function, it calls lpfc_hba_down_post function to
3985 * free any pending commands.
3986 **/
3987static int
3988lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3989{
3990 struct lpfc_sli *psli = &phba->sli;
75baf696 3991 uint32_t hba_aer_enabled;
da0436e9
JS
3992
3993 /* Restart HBA */
3994 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3995 "0296 Restart HBA Data: x%x x%x\n",
3996 phba->pport->port_state, psli->sli_flag);
3997
75baf696
JS
3998 /* Take PCIe device Advanced Error Reporting (AER) state */
3999 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4000
da0436e9
JS
4001 lpfc_sli4_brdreset(phba);
4002
4003 spin_lock_irq(&phba->hbalock);
4004 phba->pport->stopped = 0;
4005 phba->link_state = LPFC_INIT_START;
4006 phba->hba_flag = 0;
4007 spin_unlock_irq(&phba->hbalock);
4008
4009 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4010 psli->stats_start = get_seconds();
4011
75baf696
JS
4012 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4013 if (hba_aer_enabled)
4014 pci_disable_pcie_error_reporting(phba->pcidev);
4015
da0436e9
JS
4016 lpfc_hba_down_post(phba);
4017
4018 return 0;
4019}
4020
4021/**
4022 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4023 * @phba: Pointer to HBA context object.
4024 *
4025 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4026 * API jump table function pointer from the lpfc_hba struct.
4027**/
4028int
4029lpfc_sli_brdrestart(struct lpfc_hba *phba)
4030{
4031 return phba->lpfc_sli_brdrestart(phba);
4032}
4033
e59058c4 4034/**
3621a710 4035 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
4036 * @phba: Pointer to HBA context object.
4037 *
4038 * This function is called after a HBA restart to wait for successful
4039 * restart of the HBA. Successful restart of the HBA is indicated by
4040 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4041 * iteration, the function will restart the HBA again. The function returns
4042 * zero if HBA successfully restarted else returns negative error code.
4043 **/
dea3101e 4044static int
4045lpfc_sli_chipset_init(struct lpfc_hba *phba)
4046{
4047 uint32_t status, i = 0;
4048
4049 /* Read the HBA Host Status Register */
9940b97b
JS
4050 if (lpfc_readl(phba->HSregaddr, &status))
4051 return -EIO;
dea3101e 4052
4053 /* Check status register to see what current state is */
4054 i = 0;
4055 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4056
dcf2a4e0
JS
4057 /* Check every 10ms for 10 retries, then every 100ms for 90
4058 * retries, then every 1 sec for 50 retires for a total of
4059 * ~60 seconds before reset the board again and check every
4060 * 1 sec for 50 retries. The up to 60 seconds before the
4061 * board ready is required by the Falcon FIPS zeroization
4062 * complete, and any reset the board in between shall cause
4063 * restart of zeroization, further delay the board ready.
dea3101e 4064 */
dcf2a4e0 4065 if (i++ >= 200) {
dea3101e 4066 /* Adapter failed to init, timeout, status reg
4067 <status> */
ed957684 4068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4069 "0436 Adapter failed to init, "
09372820
JS
4070 "timeout, status reg x%x, "
4071 "FW Data: A8 x%x AC x%x\n", status,
4072 readl(phba->MBslimaddr + 0xa8),
4073 readl(phba->MBslimaddr + 0xac));
2e0fef85 4074 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4075 return -ETIMEDOUT;
4076 }
4077
4078 /* Check to see if any errors occurred during init */
4079 if (status & HS_FFERM) {
4080 /* ERROR: During chipset initialization */
4081 /* Adapter failed to init, chipset, status reg
4082 <status> */
ed957684 4083 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4084 "0437 Adapter failed to init, "
09372820
JS
4085 "chipset, status reg x%x, "
4086 "FW Data: A8 x%x AC x%x\n", status,
4087 readl(phba->MBslimaddr + 0xa8),
4088 readl(phba->MBslimaddr + 0xac));
2e0fef85 4089 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4090 return -EIO;
4091 }
4092
dcf2a4e0 4093 if (i <= 10)
dea3101e 4094 msleep(10);
dcf2a4e0
JS
4095 else if (i <= 100)
4096 msleep(100);
4097 else
4098 msleep(1000);
dea3101e 4099
dcf2a4e0
JS
4100 if (i == 150) {
4101 /* Do post */
92d7f7b0 4102 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4103 lpfc_sli_brdrestart(phba);
dea3101e 4104 }
4105 /* Read the HBA Host Status Register */
9940b97b
JS
4106 if (lpfc_readl(phba->HSregaddr, &status))
4107 return -EIO;
dea3101e 4108 }
4109
4110 /* Check to see if any errors occurred during init */
4111 if (status & HS_FFERM) {
4112 /* ERROR: During chipset initialization */
4113 /* Adapter failed to init, chipset, status reg <status> */
ed957684 4114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4115 "0438 Adapter failed to init, chipset, "
09372820
JS
4116 "status reg x%x, "
4117 "FW Data: A8 x%x AC x%x\n", status,
4118 readl(phba->MBslimaddr + 0xa8),
4119 readl(phba->MBslimaddr + 0xac));
2e0fef85 4120 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4121 return -EIO;
4122 }
4123
4124 /* Clear all interrupt enable conditions */
4125 writel(0, phba->HCregaddr);
4126 readl(phba->HCregaddr); /* flush */
4127
4128 /* setup host attn register */
4129 writel(0xffffffff, phba->HAregaddr);
4130 readl(phba->HAregaddr); /* flush */
4131 return 0;
4132}
4133
e59058c4 4134/**
3621a710 4135 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
4136 *
4137 * This function calculates and returns the number of HBQs required to be
4138 * configured.
4139 **/
78b2d852 4140int
ed957684
JS
4141lpfc_sli_hbq_count(void)
4142{
92d7f7b0 4143 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
4144}
4145
e59058c4 4146/**
3621a710 4147 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
4148 *
4149 * This function adds the number of hbq entries in every HBQ to get
4150 * the total number of hbq entries required for the HBA and returns
4151 * the total count.
4152 **/
ed957684
JS
4153static int
4154lpfc_sli_hbq_entry_count(void)
4155{
4156 int hbq_count = lpfc_sli_hbq_count();
4157 int count = 0;
4158 int i;
4159
4160 for (i = 0; i < hbq_count; ++i)
92d7f7b0 4161 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
4162 return count;
4163}
4164
e59058c4 4165/**
3621a710 4166 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
4167 *
4168 * This function calculates amount of memory required for all hbq entries
4169 * to be configured and returns the total memory required.
4170 **/
dea3101e 4171int
ed957684
JS
4172lpfc_sli_hbq_size(void)
4173{
4174 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4175}
4176
e59058c4 4177/**
3621a710 4178 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
4179 * @phba: Pointer to HBA context object.
4180 *
4181 * This function is called during the SLI initialization to configure
4182 * all the HBQs and post buffers to the HBQ. The caller is not
4183 * required to hold any locks. This function will return zero if successful
4184 * else it will return negative error code.
4185 **/
ed957684
JS
4186static int
4187lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4188{
4189 int hbq_count = lpfc_sli_hbq_count();
4190 LPFC_MBOXQ_t *pmb;
4191 MAILBOX_t *pmbox;
4192 uint32_t hbqno;
4193 uint32_t hbq_entry_index;
ed957684 4194
92d7f7b0
JS
4195 /* Get a Mailbox buffer to setup mailbox
4196 * commands for HBA initialization
4197 */
ed957684
JS
4198 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4199
4200 if (!pmb)
4201 return -ENOMEM;
4202
04c68496 4203 pmbox = &pmb->u.mb;
ed957684
JS
4204
4205 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4206 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 4207 phba->hbq_in_use = 1;
ed957684
JS
4208
4209 hbq_entry_index = 0;
4210 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4211 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4212 phba->hbqs[hbqno].hbqPutIdx = 0;
4213 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4214 phba->hbqs[hbqno].entry_count =
92d7f7b0 4215 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
4216 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4217 hbq_entry_index, pmb);
ed957684
JS
4218 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4219
4220 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4221 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4222 mbxStatus <status>, ring <num> */
4223
4224 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4225 LOG_SLI | LOG_VPORT,
e8b62011 4226 "1805 Adapter failed to init. "
ed957684 4227 "Data: x%x x%x x%x\n",
e8b62011 4228 pmbox->mbxCommand,
ed957684
JS
4229 pmbox->mbxStatus, hbqno);
4230
4231 phba->link_state = LPFC_HBA_ERROR;
4232 mempool_free(pmb, phba->mbox_mem_pool);
6e7288d9 4233 return -ENXIO;
ed957684
JS
4234 }
4235 }
4236 phba->hbq_count = hbq_count;
4237
ed957684
JS
4238 mempool_free(pmb, phba->mbox_mem_pool);
4239
92d7f7b0 4240 /* Initially populate or replenish the HBQs */
d7c255b2
JS
4241 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4242 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
4243 return 0;
4244}
4245
4f774513
JS
4246/**
4247 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4248 * @phba: Pointer to HBA context object.
4249 *
4250 * This function is called during the SLI initialization to configure
4251 * all the HBQs and post buffers to the HBQ. The caller is not
4252 * required to hold any locks. This function will return zero if successful
4253 * else it will return negative error code.
4254 **/
4255static int
4256lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4257{
4258 phba->hbq_in_use = 1;
4259 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4260 phba->hbq_count = 1;
4261 /* Initially populate or replenish the HBQs */
4262 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4263 return 0;
4264}
4265
e59058c4 4266/**
3621a710 4267 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
4268 * @phba: Pointer to HBA context object.
4269 * @sli_mode: sli mode - 2/3
4270 *
4271 * This function is called by the sli intialization code path
4272 * to issue config_port mailbox command. This function restarts the
4273 * HBA firmware and issues a config_port mailbox command to configure
4274 * the SLI interface in the sli mode specified by sli_mode
4275 * variable. The caller is not required to hold any locks.
4276 * The function returns 0 if successful, else returns negative error
4277 * code.
4278 **/
9399627f
JS
4279int
4280lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 4281{
4282 LPFC_MBOXQ_t *pmb;
4283 uint32_t resetcount = 0, rc = 0, done = 0;
4284
4285 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!pmb) {
2e0fef85 4287 phba->link_state = LPFC_HBA_ERROR;
dea3101e 4288 return -ENOMEM;
4289 }
4290
ed957684 4291 phba->sli_rev = sli_mode;
dea3101e 4292 while (resetcount < 2 && !done) {
2e0fef85 4293 spin_lock_irq(&phba->hbalock);
1c067a42 4294 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4295 spin_unlock_irq(&phba->hbalock);
92d7f7b0 4296 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 4297 lpfc_sli_brdrestart(phba);
dea3101e 4298 rc = lpfc_sli_chipset_init(phba);
4299 if (rc)
4300 break;
4301
2e0fef85 4302 spin_lock_irq(&phba->hbalock);
1c067a42 4303 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4304 spin_unlock_irq(&phba->hbalock);
dea3101e 4305 resetcount++;
4306
ed957684
JS
4307 /* Call pre CONFIG_PORT mailbox command initialization. A
4308 * value of 0 means the call was successful. Any other
4309 * nonzero value is a failure, but if ERESTART is returned,
4310 * the driver may reset the HBA and try again.
4311 */
dea3101e 4312 rc = lpfc_config_port_prep(phba);
4313 if (rc == -ERESTART) {
ed957684 4314 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 4315 continue;
34b02dcd 4316 } else if (rc)
dea3101e 4317 break;
6d368e53 4318
2e0fef85 4319 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 4320 lpfc_config_port(phba, pmb);
4321 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
4322 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4323 LPFC_SLI3_HBQ_ENABLED |
4324 LPFC_SLI3_CRP_ENABLED |
bc73905a
JS
4325 LPFC_SLI3_BG_ENABLED |
4326 LPFC_SLI3_DSS_ENABLED);
ed957684 4327 if (rc != MBX_SUCCESS) {
dea3101e 4328 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4329 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 4330 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 4331 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 4332 spin_lock_irq(&phba->hbalock);
04c68496 4333 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
4334 spin_unlock_irq(&phba->hbalock);
4335 rc = -ENXIO;
04c68496
JS
4336 } else {
4337 /* Allow asynchronous mailbox command to go through */
4338 spin_lock_irq(&phba->hbalock);
4339 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4340 spin_unlock_irq(&phba->hbalock);
ed957684 4341 done = 1;
04c68496 4342 }
dea3101e 4343 }
ed957684
JS
4344 if (!done) {
4345 rc = -EINVAL;
4346 goto do_prep_failed;
4347 }
04c68496
JS
4348 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4349 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
4350 rc = -ENXIO;
4351 goto do_prep_failed;
4352 }
04c68496 4353 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 4354 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
4355 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4356 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4357 phba->max_vpi : phba->max_vports;
4358
34b02dcd
JS
4359 } else
4360 phba->max_vpi = 0;
bc73905a
JS
4361 phba->fips_level = 0;
4362 phba->fips_spec_rev = 0;
4363 if (pmb->u.mb.un.varCfgPort.gdss) {
04c68496 4364 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
bc73905a
JS
4365 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4366 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4367 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4368 "2850 Security Crypto Active. FIPS x%d "
4369 "(Spec Rev: x%d)",
4370 phba->fips_level, phba->fips_spec_rev);
4371 }
4372 if (pmb->u.mb.un.varCfgPort.sec_err) {
4373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4374 "2856 Config Port Security Crypto "
4375 "Error: x%x ",
4376 pmb->u.mb.un.varCfgPort.sec_err);
4377 }
04c68496 4378 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 4379 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 4380 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 4381 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
6e7288d9
JS
4382
4383 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4384 phba->port_gp = phba->mbox->us.s3_pgp.port;
e2a0a9d6
JS
4385
4386 if (phba->cfg_enable_bg) {
04c68496 4387 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4388 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4389 else
4390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4391 "0443 Adapter did not grant "
4392 "BlockGuard\n");
4393 }
34b02dcd 4394 } else {
8f34f4ce 4395 phba->hbq_get = NULL;
34b02dcd 4396 phba->port_gp = phba->mbox->us.s2.port;
d7c255b2 4397 phba->max_vpi = 0;
ed957684 4398 }
92d7f7b0 4399do_prep_failed:
ed957684
JS
4400 mempool_free(pmb, phba->mbox_mem_pool);
4401 return rc;
4402}
4403
e59058c4
JS
4404
4405/**
3621a710 4406 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4407 * @phba: Pointer to HBA context object.
4408 *
4409 * This function is the main SLI intialization function. This function
4410 * is called by the HBA intialization code, HBA reset code and HBA
4411 * error attention handler code. Caller is not required to hold any
4412 * locks. This function issues config_port mailbox command to configure
4413 * the SLI, setup iocb rings and HBQ rings. In the end the function
4414 * calls the config_port_post function to issue init_link mailbox
4415 * command and to start the discovery. The function will return zero
4416 * if successful, else it will return negative error code.
4417 **/
ed957684
JS
4418int
4419lpfc_sli_hba_setup(struct lpfc_hba *phba)
4420{
4421 uint32_t rc;
6d368e53
JS
4422 int mode = 3, i;
4423 int longs;
ed957684
JS
4424
4425 switch (lpfc_sli_mode) {
4426 case 2:
78b2d852 4427 if (phba->cfg_enable_npiv) {
92d7f7b0 4428 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4429 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4430 "parameter (%d) to auto (0).\n",
e8b62011 4431 lpfc_sli_mode);
92d7f7b0
JS
4432 break;
4433 }
ed957684
JS
4434 mode = 2;
4435 break;
4436 case 0:
4437 case 3:
4438 break;
4439 default:
92d7f7b0 4440 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4441 "1819 Unrecognized lpfc_sli_mode "
4442 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4443
4444 break;
4445 }
4446
9399627f
JS
4447 rc = lpfc_sli_config_port(phba, mode);
4448
ed957684 4449 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4450 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4451 "1820 Unable to select SLI-3. "
4452 "Not supported by adapter.\n");
ed957684 4453 if (rc && mode != 2)
9399627f 4454 rc = lpfc_sli_config_port(phba, 2);
ed957684 4455 if (rc)
dea3101e 4456 goto lpfc_sli_hba_setup_error;
4457
0d878419
JS
4458 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4459 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4460 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4461 if (!rc) {
4462 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4463 "2709 This device supports "
4464 "Advanced Error Reporting (AER)\n");
4465 spin_lock_irq(&phba->hbalock);
4466 phba->hba_flag |= HBA_AER_ENABLED;
4467 spin_unlock_irq(&phba->hbalock);
4468 } else {
4469 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4470 "2708 This device does not support "
4471 "Advanced Error Reporting (AER)\n");
4472 phba->cfg_aer_support = 0;
4473 }
4474 }
4475
ed957684
JS
4476 if (phba->sli_rev == 3) {
4477 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4478 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4479 } else {
4480 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4481 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4482 phba->sli3_options = 0;
ed957684
JS
4483 }
4484
4485 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4486 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4487 phba->sli_rev, phba->max_vpi);
ed957684 4488 rc = lpfc_sli_ring_map(phba);
dea3101e 4489
4490 if (rc)
4491 goto lpfc_sli_hba_setup_error;
4492
6d368e53
JS
4493 /* Initialize VPIs. */
4494 if (phba->sli_rev == LPFC_SLI_REV3) {
4495 /*
4496 * The VPI bitmask and physical ID array are allocated
4497 * and initialized once only - at driver load. A port
4498 * reset doesn't need to reinitialize this memory.
4499 */
4500 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4501 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4502 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4503 GFP_KERNEL);
4504 if (!phba->vpi_bmask) {
4505 rc = -ENOMEM;
4506 goto lpfc_sli_hba_setup_error;
4507 }
4508
4509 phba->vpi_ids = kzalloc(
4510 (phba->max_vpi+1) * sizeof(uint16_t),
4511 GFP_KERNEL);
4512 if (!phba->vpi_ids) {
4513 kfree(phba->vpi_bmask);
4514 rc = -ENOMEM;
4515 goto lpfc_sli_hba_setup_error;
4516 }
4517 for (i = 0; i < phba->max_vpi; i++)
4518 phba->vpi_ids[i] = i;
4519 }
4520 }
4521
9399627f 4522 /* Init HBQs */
ed957684
JS
4523 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4524 rc = lpfc_sli_hbq_setup(phba);
4525 if (rc)
4526 goto lpfc_sli_hba_setup_error;
4527 }
04c68496 4528 spin_lock_irq(&phba->hbalock);
dea3101e 4529 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4530 spin_unlock_irq(&phba->hbalock);
dea3101e 4531
4532 rc = lpfc_config_port_post(phba);
4533 if (rc)
4534 goto lpfc_sli_hba_setup_error;
4535
ed957684
JS
4536 return rc;
4537
92d7f7b0 4538lpfc_sli_hba_setup_error:
2e0fef85 4539 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4540 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4541 "0445 Firmware initialization failed\n");
dea3101e 4542 return rc;
4543}
4544
e59058c4 4545/**
da0436e9
JS
4546 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4547 * @phba: Pointer to HBA context object.
4548 * @mboxq: mailbox pointer.
4549 * This function issue a dump mailbox command to read config region
4550 * 23 and parse the records in the region and populate driver
4551 * data structure.
e59058c4 4552 **/
da0436e9
JS
4553static int
4554lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4555 LPFC_MBOXQ_t *mboxq)
dea3101e 4556{
da0436e9
JS
4557 struct lpfc_dmabuf *mp;
4558 struct lpfc_mqe *mqe;
4559 uint32_t data_length;
4560 int rc;
dea3101e 4561
da0436e9
JS
4562 /* Program the default value of vlan_id and fc_map */
4563 phba->valid_vlan = 0;
4564 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4565 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4566 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4567
da0436e9 4568 mqe = &mboxq->u.mqe;
026abb87 4569 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
da0436e9
JS
4570 return -ENOMEM;
4571
4572 mp = (struct lpfc_dmabuf *) mboxq->context1;
4573 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4574
4575 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4576 "(%d):2571 Mailbox cmd x%x Status x%x "
4577 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4578 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4579 "CQ: x%x x%x x%x x%x\n",
4580 mboxq->vport ? mboxq->vport->vpi : 0,
4581 bf_get(lpfc_mqe_command, mqe),
4582 bf_get(lpfc_mqe_status, mqe),
4583 mqe->un.mb_words[0], mqe->un.mb_words[1],
4584 mqe->un.mb_words[2], mqe->un.mb_words[3],
4585 mqe->un.mb_words[4], mqe->un.mb_words[5],
4586 mqe->un.mb_words[6], mqe->un.mb_words[7],
4587 mqe->un.mb_words[8], mqe->un.mb_words[9],
4588 mqe->un.mb_words[10], mqe->un.mb_words[11],
4589 mqe->un.mb_words[12], mqe->un.mb_words[13],
4590 mqe->un.mb_words[14], mqe->un.mb_words[15],
4591 mqe->un.mb_words[16], mqe->un.mb_words[50],
4592 mboxq->mcqe.word0,
4593 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4594 mboxq->mcqe.trailer);
4595
4596 if (rc) {
4597 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4598 kfree(mp);
4599 return -EIO;
4600 }
4601 data_length = mqe->un.mb_words[5];
a0c87cbd 4602 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4603 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4604 kfree(mp);
da0436e9 4605 return -EIO;
d11e31dd 4606 }
dea3101e 4607
da0436e9
JS
4608 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4609 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4610 kfree(mp);
4611 return 0;
4612}
e59058c4
JS
4613
4614/**
da0436e9
JS
4615 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4616 * @phba: pointer to lpfc hba data structure.
4617 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4618 * @vpd: pointer to the memory to hold resulting port vpd data.
4619 * @vpd_size: On input, the number of bytes allocated to @vpd.
4620 * On output, the number of data bytes in @vpd.
e59058c4 4621 *
da0436e9
JS
4622 * This routine executes a READ_REV SLI4 mailbox command. In
4623 * addition, this routine gets the port vpd data.
4624 *
4625 * Return codes
af901ca1 4626 * 0 - successful
d439d286 4627 * -ENOMEM - could not allocated memory.
e59058c4 4628 **/
da0436e9
JS
4629static int
4630lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4631 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4632{
da0436e9
JS
4633 int rc = 0;
4634 uint32_t dma_size;
4635 struct lpfc_dmabuf *dmabuf;
4636 struct lpfc_mqe *mqe;
dea3101e 4637
da0436e9
JS
4638 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4639 if (!dmabuf)
4640 return -ENOMEM;
4641
4642 /*
4643 * Get a DMA buffer for the vpd data resulting from the READ_REV
4644 * mailbox command.
a257bf90 4645 */
da0436e9
JS
4646 dma_size = *vpd_size;
4647 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4648 dma_size,
4649 &dmabuf->phys,
4650 GFP_KERNEL);
4651 if (!dmabuf->virt) {
4652 kfree(dmabuf);
4653 return -ENOMEM;
a257bf90 4654 }
da0436e9 4655 memset(dmabuf->virt, 0, dma_size);
a257bf90 4656
da0436e9
JS
4657 /*
4658 * The SLI4 implementation of READ_REV conflicts at word1,
4659 * bits 31:16 and SLI4 adds vpd functionality not present
4660 * in SLI3. This code corrects the conflicts.
1dcb58e5 4661 */
da0436e9
JS
4662 lpfc_read_rev(phba, mboxq);
4663 mqe = &mboxq->u.mqe;
4664 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4665 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4666 mqe->un.read_rev.word1 &= 0x0000FFFF;
4667 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4668 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4669
4670 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4671 if (rc) {
4672 dma_free_coherent(&phba->pcidev->dev, dma_size,
4673 dmabuf->virt, dmabuf->phys);
def9c7a9 4674 kfree(dmabuf);
da0436e9
JS
4675 return -EIO;
4676 }
1dcb58e5 4677
da0436e9
JS
4678 /*
4679 * The available vpd length cannot be bigger than the
4680 * DMA buffer passed to the port. Catch the less than
4681 * case and update the caller's size.
4682 */
4683 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4684 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4685
d7c47992
JS
4686 memcpy(vpd, dmabuf->virt, *vpd_size);
4687
da0436e9
JS
4688 dma_free_coherent(&phba->pcidev->dev, dma_size,
4689 dmabuf->virt, dmabuf->phys);
4690 kfree(dmabuf);
4691 return 0;
dea3101e 4692}
4693
cd1c8301
JS
4694/**
4695 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4696 * @phba: pointer to lpfc hba data structure.
4697 *
4698 * This routine retrieves SLI4 device physical port name this PCI function
4699 * is attached to.
4700 *
4701 * Return codes
4702 * 0 - sucessful
4703 * otherwise - failed to retrieve physical port name
4704 **/
4705static int
4706lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4707{
4708 LPFC_MBOXQ_t *mboxq;
4709 struct lpfc_mbx_read_config *rd_config;
4710 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4711 struct lpfc_controller_attribute *cntl_attr;
4712 struct lpfc_mbx_get_port_name *get_port_name;
4713 void *virtaddr = NULL;
4714 uint32_t alloclen, reqlen;
4715 uint32_t shdr_status, shdr_add_status;
4716 union lpfc_sli4_cfg_shdr *shdr;
4717 char cport_name = 0;
4718 int rc;
4719
4720 /* We assume nothing at this point */
4721 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4722 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4723
4724 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4725 if (!mboxq)
4726 return -ENOMEM;
4727
4728 /* obtain link type and link number via READ_CONFIG */
4729 lpfc_read_config(phba, mboxq);
4730 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4731 if (rc == MBX_SUCCESS) {
4732 rd_config = &mboxq->u.mqe.un.rd_config;
4733 if (bf_get(lpfc_mbx_rd_conf_lnk_ldv, rd_config)) {
4734 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4735 phba->sli4_hba.lnk_info.lnk_tp =
4736 bf_get(lpfc_mbx_rd_conf_lnk_type, rd_config);
4737 phba->sli4_hba.lnk_info.lnk_no =
4738 bf_get(lpfc_mbx_rd_conf_lnk_numb, rd_config);
4739 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4740 "3081 lnk_type:%d, lnk_numb:%d\n",
4741 phba->sli4_hba.lnk_info.lnk_tp,
4742 phba->sli4_hba.lnk_info.lnk_no);
4743 goto retrieve_ppname;
4744 } else
4745 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4746 "3082 Mailbox (x%x) returned ldv:x0\n",
4747 bf_get(lpfc_mqe_command,
4748 &mboxq->u.mqe));
4749 } else
4750 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4751 "3083 Mailbox (x%x) failed, status:x%x\n",
4752 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4753 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
4754
4755 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4756 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4757 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4758 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4759 LPFC_SLI4_MBX_NEMBED);
4760 if (alloclen < reqlen) {
4761 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4762 "3084 Allocated DMA memory size (%d) is "
4763 "less than the requested DMA memory size "
4764 "(%d)\n", alloclen, reqlen);
4765 rc = -ENOMEM;
4766 goto out_free_mboxq;
4767 }
4768 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4769 virtaddr = mboxq->sge_array->addr[0];
4770 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4771 shdr = &mbx_cntl_attr->cfg_shdr;
4772 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4773 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4774 if (shdr_status || shdr_add_status || rc) {
4775 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4776 "3085 Mailbox x%x (x%x/x%x) failed, "
4777 "rc:x%x, status:x%x, add_status:x%x\n",
4778 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4779 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4780 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4781 rc, shdr_status, shdr_add_status);
4782 rc = -ENXIO;
4783 goto out_free_mboxq;
4784 }
4785 cntl_attr = &mbx_cntl_attr->cntl_attr;
4786 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4787 phba->sli4_hba.lnk_info.lnk_tp =
4788 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4789 phba->sli4_hba.lnk_info.lnk_no =
4790 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4791 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4792 "3086 lnk_type:%d, lnk_numb:%d\n",
4793 phba->sli4_hba.lnk_info.lnk_tp,
4794 phba->sli4_hba.lnk_info.lnk_no);
4795
4796retrieve_ppname:
4797 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4798 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4799 sizeof(struct lpfc_mbx_get_port_name) -
4800 sizeof(struct lpfc_sli4_cfg_mhdr),
4801 LPFC_SLI4_MBX_EMBED);
4802 get_port_name = &mboxq->u.mqe.un.get_port_name;
4803 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4804 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4805 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4806 phba->sli4_hba.lnk_info.lnk_tp);
4807 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4808 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4809 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4810 if (shdr_status || shdr_add_status || rc) {
4811 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4812 "3087 Mailbox x%x (x%x/x%x) failed: "
4813 "rc:x%x, status:x%x, add_status:x%x\n",
4814 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4815 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4816 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4817 rc, shdr_status, shdr_add_status);
4818 rc = -ENXIO;
4819 goto out_free_mboxq;
4820 }
4821 switch (phba->sli4_hba.lnk_info.lnk_no) {
4822 case LPFC_LINK_NUMBER_0:
4823 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4824 &get_port_name->u.response);
4825 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4826 break;
4827 case LPFC_LINK_NUMBER_1:
4828 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4829 &get_port_name->u.response);
4830 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4831 break;
4832 case LPFC_LINK_NUMBER_2:
4833 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4834 &get_port_name->u.response);
4835 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4836 break;
4837 case LPFC_LINK_NUMBER_3:
4838 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4839 &get_port_name->u.response);
4840 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4841 break;
4842 default:
4843 break;
4844 }
4845
4846 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4847 phba->Port[0] = cport_name;
4848 phba->Port[1] = '\0';
4849 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4850 "3091 SLI get port name: %s\n", phba->Port);
4851 }
4852
4853out_free_mboxq:
4854 if (rc != MBX_TIMEOUT) {
4855 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4856 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4857 else
4858 mempool_free(mboxq, phba->mbox_mem_pool);
4859 }
4860 return rc;
4861}
4862
e59058c4 4863/**
da0436e9
JS
4864 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4865 * @phba: pointer to lpfc hba data structure.
e59058c4 4866 *
da0436e9
JS
4867 * This routine is called to explicitly arm the SLI4 device's completion and
4868 * event queues
4869 **/
4870static void
4871lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4872{
4873 uint8_t fcp_eqidx;
4874
4875 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4876 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
0558056c
JS
4877 fcp_eqidx = 0;
4878 do
da0436e9
JS
4879 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4880 LPFC_QUEUE_REARM);
0558056c 4881 while (++fcp_eqidx < phba->cfg_fcp_eq_count);
da0436e9
JS
4882 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4883 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4884 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4885 LPFC_QUEUE_REARM);
4886}
4887
6d368e53
JS
4888/**
4889 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4890 * @phba: Pointer to HBA context object.
4891 * @type: The resource extent type.
b76f2dc9
JS
4892 * @extnt_count: buffer to hold port available extent count.
4893 * @extnt_size: buffer to hold element count per extent.
6d368e53 4894 *
b76f2dc9
JS
4895 * This function calls the port and retrievs the number of available
4896 * extents and their size for a particular extent type.
4897 *
4898 * Returns: 0 if successful. Nonzero otherwise.
6d368e53 4899 **/
b76f2dc9 4900int
6d368e53
JS
4901lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
4902 uint16_t *extnt_count, uint16_t *extnt_size)
4903{
4904 int rc = 0;
4905 uint32_t length;
4906 uint32_t mbox_tmo;
4907 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
4908 LPFC_MBOXQ_t *mbox;
4909
4910 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4911 if (!mbox)
4912 return -ENOMEM;
4913
4914 /* Find out how many extents are available for this resource type */
4915 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
4916 sizeof(struct lpfc_sli4_cfg_mhdr));
4917 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
4918 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
4919 length, LPFC_SLI4_MBX_EMBED);
4920
4921 /* Send an extents count of 0 - the GET doesn't use it. */
4922 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
4923 LPFC_SLI4_MBX_EMBED);
4924 if (unlikely(rc)) {
4925 rc = -EIO;
4926 goto err_exit;
4927 }
4928
4929 if (!phba->sli4_hba.intr_enable)
4930 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
4931 else {
a183a15f 4932 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
4933 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
4934 }
4935 if (unlikely(rc)) {
4936 rc = -EIO;
4937 goto err_exit;
4938 }
4939
4940 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
4941 if (bf_get(lpfc_mbox_hdr_status,
4942 &rsrc_info->header.cfg_shdr.response)) {
4943 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4944 "2930 Failed to get resource extents "
4945 "Status 0x%x Add'l Status 0x%x\n",
4946 bf_get(lpfc_mbox_hdr_status,
4947 &rsrc_info->header.cfg_shdr.response),
4948 bf_get(lpfc_mbox_hdr_add_status,
4949 &rsrc_info->header.cfg_shdr.response));
4950 rc = -EIO;
4951 goto err_exit;
4952 }
4953
4954 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
4955 &rsrc_info->u.rsp);
4956 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
4957 &rsrc_info->u.rsp);
4958 err_exit:
4959 mempool_free(mbox, phba->mbox_mem_pool);
4960 return rc;
4961}
4962
4963/**
4964 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
4965 * @phba: Pointer to HBA context object.
4966 * @type: The extent type to check.
4967 *
4968 * This function reads the current available extents from the port and checks
4969 * if the extent count or extent size has changed since the last access.
4970 * Callers use this routine post port reset to understand if there is a
4971 * extent reprovisioning requirement.
4972 *
4973 * Returns:
4974 * -Error: error indicates problem.
4975 * 1: Extent count or size has changed.
4976 * 0: No changes.
4977 **/
4978static int
4979lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
4980{
4981 uint16_t curr_ext_cnt, rsrc_ext_cnt;
4982 uint16_t size_diff, rsrc_ext_size;
4983 int rc = 0;
4984 struct lpfc_rsrc_blks *rsrc_entry;
4985 struct list_head *rsrc_blk_list = NULL;
4986
4987 size_diff = 0;
4988 curr_ext_cnt = 0;
4989 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
4990 &rsrc_ext_cnt,
4991 &rsrc_ext_size);
4992 if (unlikely(rc))
4993 return -EIO;
4994
4995 switch (type) {
4996 case LPFC_RSC_TYPE_FCOE_RPI:
4997 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
4998 break;
4999 case LPFC_RSC_TYPE_FCOE_VPI:
5000 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5001 break;
5002 case LPFC_RSC_TYPE_FCOE_XRI:
5003 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5004 break;
5005 case LPFC_RSC_TYPE_FCOE_VFI:
5006 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5007 break;
5008 default:
5009 break;
5010 }
5011
5012 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5013 curr_ext_cnt++;
5014 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5015 size_diff++;
5016 }
5017
5018 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5019 rc = 1;
5020
5021 return rc;
5022}
5023
5024/**
5025 * lpfc_sli4_cfg_post_extnts -
5026 * @phba: Pointer to HBA context object.
5027 * @extnt_cnt - number of available extents.
5028 * @type - the extent type (rpi, xri, vfi, vpi).
5029 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5030 * @mbox - pointer to the caller's allocated mailbox structure.
5031 *
5032 * This function executes the extents allocation request. It also
5033 * takes care of the amount of memory needed to allocate or get the
5034 * allocated extents. It is the caller's responsibility to evaluate
5035 * the response.
5036 *
5037 * Returns:
5038 * -Error: Error value describes the condition found.
5039 * 0: if successful
5040 **/
5041static int
5042lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t *extnt_cnt,
5043 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5044{
5045 int rc = 0;
5046 uint32_t req_len;
5047 uint32_t emb_len;
5048 uint32_t alloc_len, mbox_tmo;
5049
5050 /* Calculate the total requested length of the dma memory */
5051 req_len = *extnt_cnt * sizeof(uint16_t);
5052
5053 /*
5054 * Calculate the size of an embedded mailbox. The uint32_t
5055 * accounts for extents-specific word.
5056 */
5057 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5058 sizeof(uint32_t);
5059
5060 /*
5061 * Presume the allocation and response will fit into an embedded
5062 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5063 */
5064 *emb = LPFC_SLI4_MBX_EMBED;
5065 if (req_len > emb_len) {
5066 req_len = *extnt_cnt * sizeof(uint16_t) +
5067 sizeof(union lpfc_sli4_cfg_shdr) +
5068 sizeof(uint32_t);
5069 *emb = LPFC_SLI4_MBX_NEMBED;
5070 }
5071
5072 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5073 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5074 req_len, *emb);
5075 if (alloc_len < req_len) {
5076 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
b76f2dc9 5077 "2982 Allocated DMA memory size (x%x) is "
6d368e53
JS
5078 "less than the requested DMA memory "
5079 "size (x%x)\n", alloc_len, req_len);
5080 return -ENOMEM;
5081 }
5082 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, *extnt_cnt, type, *emb);
5083 if (unlikely(rc))
5084 return -EIO;
5085
5086 if (!phba->sli4_hba.intr_enable)
5087 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5088 else {
a183a15f 5089 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5090 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5091 }
5092
5093 if (unlikely(rc))
5094 rc = -EIO;
5095 return rc;
5096}
5097
5098/**
5099 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5100 * @phba: Pointer to HBA context object.
5101 * @type: The resource extent type to allocate.
5102 *
5103 * This function allocates the number of elements for the specified
5104 * resource type.
5105 **/
5106static int
5107lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5108{
5109 bool emb = false;
5110 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5111 uint16_t rsrc_id, rsrc_start, j, k;
5112 uint16_t *ids;
5113 int i, rc;
5114 unsigned long longs;
5115 unsigned long *bmask;
5116 struct lpfc_rsrc_blks *rsrc_blks;
5117 LPFC_MBOXQ_t *mbox;
5118 uint32_t length;
5119 struct lpfc_id_range *id_array = NULL;
5120 void *virtaddr = NULL;
5121 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5122 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5123 struct list_head *ext_blk_list;
5124
5125 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5126 &rsrc_cnt,
5127 &rsrc_size);
5128 if (unlikely(rc))
5129 return -EIO;
5130
5131 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5133 "3009 No available Resource Extents "
5134 "for resource type 0x%x: Count: 0x%x, "
5135 "Size 0x%x\n", type, rsrc_cnt,
5136 rsrc_size);
5137 return -ENOMEM;
5138 }
5139
5140 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT,
5141 "2903 Available Resource Extents "
5142 "for resource type 0x%x: Count: 0x%x, "
5143 "Size 0x%x\n", type, rsrc_cnt,
5144 rsrc_size);
5145
5146 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5147 if (!mbox)
5148 return -ENOMEM;
5149
5150 rc = lpfc_sli4_cfg_post_extnts(phba, &rsrc_cnt, type, &emb, mbox);
5151 if (unlikely(rc)) {
5152 rc = -EIO;
5153 goto err_exit;
5154 }
5155
5156 /*
5157 * Figure out where the response is located. Then get local pointers
5158 * to the response data. The port does not guarantee to respond to
5159 * all extents counts request so update the local variable with the
5160 * allocated count from the port.
5161 */
5162 if (emb == LPFC_SLI4_MBX_EMBED) {
5163 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5164 id_array = &rsrc_ext->u.rsp.id[0];
5165 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5166 } else {
5167 virtaddr = mbox->sge_array->addr[0];
5168 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5169 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5170 id_array = &n_rsrc->id;
5171 }
5172
5173 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5174 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5175
5176 /*
5177 * Based on the resource size and count, correct the base and max
5178 * resource values.
5179 */
5180 length = sizeof(struct lpfc_rsrc_blks);
5181 switch (type) {
5182 case LPFC_RSC_TYPE_FCOE_RPI:
5183 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5184 sizeof(unsigned long),
5185 GFP_KERNEL);
5186 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5187 rc = -ENOMEM;
5188 goto err_exit;
5189 }
5190 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5191 sizeof(uint16_t),
5192 GFP_KERNEL);
5193 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5194 kfree(phba->sli4_hba.rpi_bmask);
5195 rc = -ENOMEM;
5196 goto err_exit;
5197 }
5198
5199 /*
5200 * The next_rpi was initialized with the maximum available
5201 * count but the port may allocate a smaller number. Catch
5202 * that case and update the next_rpi.
5203 */
5204 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5205
5206 /* Initialize local ptrs for common extent processing later. */
5207 bmask = phba->sli4_hba.rpi_bmask;
5208 ids = phba->sli4_hba.rpi_ids;
5209 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5210 break;
5211 case LPFC_RSC_TYPE_FCOE_VPI:
5212 phba->vpi_bmask = kzalloc(longs *
5213 sizeof(unsigned long),
5214 GFP_KERNEL);
5215 if (unlikely(!phba->vpi_bmask)) {
5216 rc = -ENOMEM;
5217 goto err_exit;
5218 }
5219 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5220 sizeof(uint16_t),
5221 GFP_KERNEL);
5222 if (unlikely(!phba->vpi_ids)) {
5223 kfree(phba->vpi_bmask);
5224 rc = -ENOMEM;
5225 goto err_exit;
5226 }
5227
5228 /* Initialize local ptrs for common extent processing later. */
5229 bmask = phba->vpi_bmask;
5230 ids = phba->vpi_ids;
5231 ext_blk_list = &phba->lpfc_vpi_blk_list;
5232 break;
5233 case LPFC_RSC_TYPE_FCOE_XRI:
5234 phba->sli4_hba.xri_bmask = kzalloc(longs *
5235 sizeof(unsigned long),
5236 GFP_KERNEL);
5237 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5238 rc = -ENOMEM;
5239 goto err_exit;
5240 }
5241 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5242 sizeof(uint16_t),
5243 GFP_KERNEL);
5244 if (unlikely(!phba->sli4_hba.xri_ids)) {
5245 kfree(phba->sli4_hba.xri_bmask);
5246 rc = -ENOMEM;
5247 goto err_exit;
5248 }
5249
5250 /* Initialize local ptrs for common extent processing later. */
5251 bmask = phba->sli4_hba.xri_bmask;
5252 ids = phba->sli4_hba.xri_ids;
5253 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5254 break;
5255 case LPFC_RSC_TYPE_FCOE_VFI:
5256 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5257 sizeof(unsigned long),
5258 GFP_KERNEL);
5259 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5260 rc = -ENOMEM;
5261 goto err_exit;
5262 }
5263 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5264 sizeof(uint16_t),
5265 GFP_KERNEL);
5266 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5267 kfree(phba->sli4_hba.vfi_bmask);
5268 rc = -ENOMEM;
5269 goto err_exit;
5270 }
5271
5272 /* Initialize local ptrs for common extent processing later. */
5273 bmask = phba->sli4_hba.vfi_bmask;
5274 ids = phba->sli4_hba.vfi_ids;
5275 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5276 break;
5277 default:
5278 /* Unsupported Opcode. Fail call. */
5279 id_array = NULL;
5280 bmask = NULL;
5281 ids = NULL;
5282 ext_blk_list = NULL;
5283 goto err_exit;
5284 }
5285
5286 /*
5287 * Complete initializing the extent configuration with the
5288 * allocated ids assigned to this function. The bitmask serves
5289 * as an index into the array and manages the available ids. The
5290 * array just stores the ids communicated to the port via the wqes.
5291 */
5292 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5293 if ((i % 2) == 0)
5294 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5295 &id_array[k]);
5296 else
5297 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5298 &id_array[k]);
5299
5300 rsrc_blks = kzalloc(length, GFP_KERNEL);
5301 if (unlikely(!rsrc_blks)) {
5302 rc = -ENOMEM;
5303 kfree(bmask);
5304 kfree(ids);
5305 goto err_exit;
5306 }
5307 rsrc_blks->rsrc_start = rsrc_id;
5308 rsrc_blks->rsrc_size = rsrc_size;
5309 list_add_tail(&rsrc_blks->list, ext_blk_list);
5310 rsrc_start = rsrc_id;
5311 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5312 phba->sli4_hba.scsi_xri_start = rsrc_start +
5313 lpfc_sli4_get_els_iocb_cnt(phba);
5314
5315 while (rsrc_id < (rsrc_start + rsrc_size)) {
5316 ids[j] = rsrc_id;
5317 rsrc_id++;
5318 j++;
5319 }
5320 /* Entire word processed. Get next word.*/
5321 if ((i % 2) == 1)
5322 k++;
5323 }
5324 err_exit:
5325 lpfc_sli4_mbox_cmd_free(phba, mbox);
5326 return rc;
5327}
5328
5329/**
5330 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5331 * @phba: Pointer to HBA context object.
5332 * @type: the extent's type.
5333 *
5334 * This function deallocates all extents of a particular resource type.
5335 * SLI4 does not allow for deallocating a particular extent range. It
5336 * is the caller's responsibility to release all kernel memory resources.
5337 **/
5338static int
5339lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5340{
5341 int rc;
5342 uint32_t length, mbox_tmo = 0;
5343 LPFC_MBOXQ_t *mbox;
5344 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5345 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5346
5347 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5348 if (!mbox)
5349 return -ENOMEM;
5350
5351 /*
5352 * This function sends an embedded mailbox because it only sends the
5353 * the resource type. All extents of this type are released by the
5354 * port.
5355 */
5356 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5357 sizeof(struct lpfc_sli4_cfg_mhdr));
5358 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5359 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5360 length, LPFC_SLI4_MBX_EMBED);
5361
5362 /* Send an extents count of 0 - the dealloc doesn't use it. */
5363 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5364 LPFC_SLI4_MBX_EMBED);
5365 if (unlikely(rc)) {
5366 rc = -EIO;
5367 goto out_free_mbox;
5368 }
5369 if (!phba->sli4_hba.intr_enable)
5370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5371 else {
a183a15f 5372 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
5373 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5374 }
5375 if (unlikely(rc)) {
5376 rc = -EIO;
5377 goto out_free_mbox;
5378 }
5379
5380 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5381 if (bf_get(lpfc_mbox_hdr_status,
5382 &dealloc_rsrc->header.cfg_shdr.response)) {
5383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5384 "2919 Failed to release resource extents "
5385 "for type %d - Status 0x%x Add'l Status 0x%x. "
5386 "Resource memory not released.\n",
5387 type,
5388 bf_get(lpfc_mbox_hdr_status,
5389 &dealloc_rsrc->header.cfg_shdr.response),
5390 bf_get(lpfc_mbox_hdr_add_status,
5391 &dealloc_rsrc->header.cfg_shdr.response));
5392 rc = -EIO;
5393 goto out_free_mbox;
5394 }
5395
5396 /* Release kernel memory resources for the specific type. */
5397 switch (type) {
5398 case LPFC_RSC_TYPE_FCOE_VPI:
5399 kfree(phba->vpi_bmask);
5400 kfree(phba->vpi_ids);
5401 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5402 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5403 &phba->lpfc_vpi_blk_list, list) {
5404 list_del_init(&rsrc_blk->list);
5405 kfree(rsrc_blk);
5406 }
5407 break;
5408 case LPFC_RSC_TYPE_FCOE_XRI:
5409 kfree(phba->sli4_hba.xri_bmask);
5410 kfree(phba->sli4_hba.xri_ids);
5411 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5412 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5413 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5414 list_del_init(&rsrc_blk->list);
5415 kfree(rsrc_blk);
5416 }
5417 break;
5418 case LPFC_RSC_TYPE_FCOE_VFI:
5419 kfree(phba->sli4_hba.vfi_bmask);
5420 kfree(phba->sli4_hba.vfi_ids);
5421 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5422 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5423 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5424 list_del_init(&rsrc_blk->list);
5425 kfree(rsrc_blk);
5426 }
5427 break;
5428 case LPFC_RSC_TYPE_FCOE_RPI:
5429 /* RPI bitmask and physical id array are cleaned up earlier. */
5430 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5431 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5432 list_del_init(&rsrc_blk->list);
5433 kfree(rsrc_blk);
5434 }
5435 break;
5436 default:
5437 break;
5438 }
5439
5440 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5441
5442 out_free_mbox:
5443 mempool_free(mbox, phba->mbox_mem_pool);
5444 return rc;
5445}
5446
5447/**
5448 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5449 * @phba: Pointer to HBA context object.
5450 *
5451 * This function allocates all SLI4 resource identifiers.
5452 **/
5453int
5454lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5455{
5456 int i, rc, error = 0;
5457 uint16_t count, base;
5458 unsigned long longs;
5459
5460 if (phba->sli4_hba.extents_in_use) {
5461 /*
5462 * The port supports resource extents. The XRI, VPI, VFI, RPI
5463 * resource extent count must be read and allocated before
5464 * provisioning the resource id arrays.
5465 */
5466 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5467 LPFC_IDX_RSRC_RDY) {
5468 /*
5469 * Extent-based resources are set - the driver could
5470 * be in a port reset. Figure out if any corrective
5471 * actions need to be taken.
5472 */
5473 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5474 LPFC_RSC_TYPE_FCOE_VFI);
5475 if (rc != 0)
5476 error++;
5477 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5478 LPFC_RSC_TYPE_FCOE_VPI);
5479 if (rc != 0)
5480 error++;
5481 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5482 LPFC_RSC_TYPE_FCOE_XRI);
5483 if (rc != 0)
5484 error++;
5485 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5486 LPFC_RSC_TYPE_FCOE_RPI);
5487 if (rc != 0)
5488 error++;
5489
5490 /*
5491 * It's possible that the number of resources
5492 * provided to this port instance changed between
5493 * resets. Detect this condition and reallocate
5494 * resources. Otherwise, there is no action.
5495 */
5496 if (error) {
5497 lpfc_printf_log(phba, KERN_INFO,
5498 LOG_MBOX | LOG_INIT,
5499 "2931 Detected extent resource "
5500 "change. Reallocating all "
5501 "extents.\n");
5502 rc = lpfc_sli4_dealloc_extent(phba,
5503 LPFC_RSC_TYPE_FCOE_VFI);
5504 rc = lpfc_sli4_dealloc_extent(phba,
5505 LPFC_RSC_TYPE_FCOE_VPI);
5506 rc = lpfc_sli4_dealloc_extent(phba,
5507 LPFC_RSC_TYPE_FCOE_XRI);
5508 rc = lpfc_sli4_dealloc_extent(phba,
5509 LPFC_RSC_TYPE_FCOE_RPI);
5510 } else
5511 return 0;
5512 }
5513
5514 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5515 if (unlikely(rc))
5516 goto err_exit;
5517
5518 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5519 if (unlikely(rc))
5520 goto err_exit;
5521
5522 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5523 if (unlikely(rc))
5524 goto err_exit;
5525
5526 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5527 if (unlikely(rc))
5528 goto err_exit;
5529 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5530 LPFC_IDX_RSRC_RDY);
5531 return rc;
5532 } else {
5533 /*
5534 * The port does not support resource extents. The XRI, VPI,
5535 * VFI, RPI resource ids were determined from READ_CONFIG.
5536 * Just allocate the bitmasks and provision the resource id
5537 * arrays. If a port reset is active, the resources don't
5538 * need any action - just exit.
5539 */
5540 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5541 LPFC_IDX_RSRC_RDY)
5542 return 0;
5543
5544 /* RPIs. */
5545 count = phba->sli4_hba.max_cfg_param.max_rpi;
5546 base = phba->sli4_hba.max_cfg_param.rpi_base;
5547 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5548 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5549 sizeof(unsigned long),
5550 GFP_KERNEL);
5551 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5552 rc = -ENOMEM;
5553 goto err_exit;
5554 }
5555 phba->sli4_hba.rpi_ids = kzalloc(count *
5556 sizeof(uint16_t),
5557 GFP_KERNEL);
5558 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5559 rc = -ENOMEM;
5560 goto free_rpi_bmask;
5561 }
5562
5563 for (i = 0; i < count; i++)
5564 phba->sli4_hba.rpi_ids[i] = base + i;
5565
5566 /* VPIs. */
5567 count = phba->sli4_hba.max_cfg_param.max_vpi;
5568 base = phba->sli4_hba.max_cfg_param.vpi_base;
5569 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5570 phba->vpi_bmask = kzalloc(longs *
5571 sizeof(unsigned long),
5572 GFP_KERNEL);
5573 if (unlikely(!phba->vpi_bmask)) {
5574 rc = -ENOMEM;
5575 goto free_rpi_ids;
5576 }
5577 phba->vpi_ids = kzalloc(count *
5578 sizeof(uint16_t),
5579 GFP_KERNEL);
5580 if (unlikely(!phba->vpi_ids)) {
5581 rc = -ENOMEM;
5582 goto free_vpi_bmask;
5583 }
5584
5585 for (i = 0; i < count; i++)
5586 phba->vpi_ids[i] = base + i;
5587
5588 /* XRIs. */
5589 count = phba->sli4_hba.max_cfg_param.max_xri;
5590 base = phba->sli4_hba.max_cfg_param.xri_base;
5591 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5592 phba->sli4_hba.xri_bmask = kzalloc(longs *
5593 sizeof(unsigned long),
5594 GFP_KERNEL);
5595 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5596 rc = -ENOMEM;
5597 goto free_vpi_ids;
5598 }
5599 phba->sli4_hba.xri_ids = kzalloc(count *
5600 sizeof(uint16_t),
5601 GFP_KERNEL);
5602 if (unlikely(!phba->sli4_hba.xri_ids)) {
5603 rc = -ENOMEM;
5604 goto free_xri_bmask;
5605 }
5606
5607 for (i = 0; i < count; i++)
5608 phba->sli4_hba.xri_ids[i] = base + i;
5609
5610 /* VFIs. */
5611 count = phba->sli4_hba.max_cfg_param.max_vfi;
5612 base = phba->sli4_hba.max_cfg_param.vfi_base;
5613 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5614 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5615 sizeof(unsigned long),
5616 GFP_KERNEL);
5617 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5618 rc = -ENOMEM;
5619 goto free_xri_ids;
5620 }
5621 phba->sli4_hba.vfi_ids = kzalloc(count *
5622 sizeof(uint16_t),
5623 GFP_KERNEL);
5624 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5625 rc = -ENOMEM;
5626 goto free_vfi_bmask;
5627 }
5628
5629 for (i = 0; i < count; i++)
5630 phba->sli4_hba.vfi_ids[i] = base + i;
5631
5632 /*
5633 * Mark all resources ready. An HBA reset doesn't need
5634 * to reset the initialization.
5635 */
5636 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5637 LPFC_IDX_RSRC_RDY);
5638 return 0;
5639 }
5640
5641 free_vfi_bmask:
5642 kfree(phba->sli4_hba.vfi_bmask);
5643 free_xri_ids:
5644 kfree(phba->sli4_hba.xri_ids);
5645 free_xri_bmask:
5646 kfree(phba->sli4_hba.xri_bmask);
5647 free_vpi_ids:
5648 kfree(phba->vpi_ids);
5649 free_vpi_bmask:
5650 kfree(phba->vpi_bmask);
5651 free_rpi_ids:
5652 kfree(phba->sli4_hba.rpi_ids);
5653 free_rpi_bmask:
5654 kfree(phba->sli4_hba.rpi_bmask);
5655 err_exit:
5656 return rc;
5657}
5658
5659/**
5660 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5661 * @phba: Pointer to HBA context object.
5662 *
5663 * This function allocates the number of elements for the specified
5664 * resource type.
5665 **/
5666int
5667lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5668{
5669 if (phba->sli4_hba.extents_in_use) {
5670 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5671 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5672 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5673 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5674 } else {
5675 kfree(phba->vpi_bmask);
5676 kfree(phba->vpi_ids);
5677 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5678 kfree(phba->sli4_hba.xri_bmask);
5679 kfree(phba->sli4_hba.xri_ids);
5680 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5681 kfree(phba->sli4_hba.vfi_bmask);
5682 kfree(phba->sli4_hba.vfi_ids);
5683 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5684 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5685 }
5686
5687 return 0;
5688}
5689
b76f2dc9
JS
5690/**
5691 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5692 * @phba: Pointer to HBA context object.
5693 * @type: The resource extent type.
5694 * @extnt_count: buffer to hold port extent count response
5695 * @extnt_size: buffer to hold port extent size response.
5696 *
5697 * This function calls the port to read the host allocated extents
5698 * for a particular type.
5699 **/
5700int
5701lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5702 uint16_t *extnt_cnt, uint16_t *extnt_size)
5703{
5704 bool emb;
5705 int rc = 0;
5706 uint16_t curr_blks = 0;
5707 uint32_t req_len, emb_len;
5708 uint32_t alloc_len, mbox_tmo;
5709 struct list_head *blk_list_head;
5710 struct lpfc_rsrc_blks *rsrc_blk;
5711 LPFC_MBOXQ_t *mbox;
5712 void *virtaddr = NULL;
5713 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5714 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5715 union lpfc_sli4_cfg_shdr *shdr;
5716
5717 switch (type) {
5718 case LPFC_RSC_TYPE_FCOE_VPI:
5719 blk_list_head = &phba->lpfc_vpi_blk_list;
5720 break;
5721 case LPFC_RSC_TYPE_FCOE_XRI:
5722 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5723 break;
5724 case LPFC_RSC_TYPE_FCOE_VFI:
5725 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5726 break;
5727 case LPFC_RSC_TYPE_FCOE_RPI:
5728 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5729 break;
5730 default:
5731 return -EIO;
5732 }
5733
5734 /* Count the number of extents currently allocatd for this type. */
5735 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5736 if (curr_blks == 0) {
5737 /*
5738 * The GET_ALLOCATED mailbox does not return the size,
5739 * just the count. The size should be just the size
5740 * stored in the current allocated block and all sizes
5741 * for an extent type are the same so set the return
5742 * value now.
5743 */
5744 *extnt_size = rsrc_blk->rsrc_size;
5745 }
5746 curr_blks++;
5747 }
5748
5749 /* Calculate the total requested length of the dma memory. */
5750 req_len = curr_blks * sizeof(uint16_t);
5751
5752 /*
5753 * Calculate the size of an embedded mailbox. The uint32_t
5754 * accounts for extents-specific word.
5755 */
5756 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5757 sizeof(uint32_t);
5758
5759 /*
5760 * Presume the allocation and response will fit into an embedded
5761 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5762 */
5763 emb = LPFC_SLI4_MBX_EMBED;
5764 req_len = emb_len;
5765 if (req_len > emb_len) {
5766 req_len = curr_blks * sizeof(uint16_t) +
5767 sizeof(union lpfc_sli4_cfg_shdr) +
5768 sizeof(uint32_t);
5769 emb = LPFC_SLI4_MBX_NEMBED;
5770 }
5771
5772 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5773 if (!mbox)
5774 return -ENOMEM;
5775 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5776
5777 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5778 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5779 req_len, emb);
5780 if (alloc_len < req_len) {
5781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5782 "2983 Allocated DMA memory size (x%x) is "
5783 "less than the requested DMA memory "
5784 "size (x%x)\n", alloc_len, req_len);
5785 rc = -ENOMEM;
5786 goto err_exit;
5787 }
5788 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5789 if (unlikely(rc)) {
5790 rc = -EIO;
5791 goto err_exit;
5792 }
5793
5794 if (!phba->sli4_hba.intr_enable)
5795 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5796 else {
a183a15f 5797 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
b76f2dc9
JS
5798 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5799 }
5800
5801 if (unlikely(rc)) {
5802 rc = -EIO;
5803 goto err_exit;
5804 }
5805
5806 /*
5807 * Figure out where the response is located. Then get local pointers
5808 * to the response data. The port does not guarantee to respond to
5809 * all extents counts request so update the local variable with the
5810 * allocated count from the port.
5811 */
5812 if (emb == LPFC_SLI4_MBX_EMBED) {
5813 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5814 shdr = &rsrc_ext->header.cfg_shdr;
5815 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5816 } else {
5817 virtaddr = mbox->sge_array->addr[0];
5818 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5819 shdr = &n_rsrc->cfg_shdr;
5820 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5821 }
5822
5823 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5824 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5825 "2984 Failed to read allocated resources "
5826 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5827 type,
5828 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5829 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5830 rc = -EIO;
5831 goto err_exit;
5832 }
5833 err_exit:
5834 lpfc_sli4_mbox_cmd_free(phba, mbox);
5835 return rc;
5836}
5837
da0436e9
JS
5838/**
5839 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
5840 * @phba: Pointer to HBA context object.
5841 *
5842 * This function is the main SLI4 device intialization PCI function. This
5843 * function is called by the HBA intialization code, HBA reset code and
5844 * HBA error attention handler code. Caller is not required to hold any
5845 * locks.
5846 **/
5847int
5848lpfc_sli4_hba_setup(struct lpfc_hba *phba)
5849{
5850 int rc;
5851 LPFC_MBOXQ_t *mboxq;
5852 struct lpfc_mqe *mqe;
5853 uint8_t *vpd;
5854 uint32_t vpd_size;
5855 uint32_t ftr_rsp = 0;
5856 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
5857 struct lpfc_vport *vport = phba->pport;
5858 struct lpfc_dmabuf *mp;
5859
5860 /* Perform a PCI function reset to start from clean */
5861 rc = lpfc_pci_function_reset(phba);
5862 if (unlikely(rc))
5863 return -ENODEV;
5864
5865 /* Check the HBA Host Status Register for readyness */
5866 rc = lpfc_sli4_post_status_check(phba);
5867 if (unlikely(rc))
5868 return -ENODEV;
5869 else {
5870 spin_lock_irq(&phba->hbalock);
5871 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
5872 spin_unlock_irq(&phba->hbalock);
5873 }
5874
5875 /*
5876 * Allocate a single mailbox container for initializing the
5877 * port.
5878 */
5879 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5880 if (!mboxq)
5881 return -ENOMEM;
5882
5883 /*
5884 * Continue initialization with default values even if driver failed
5885 * to read FCoE param config regions
5886 */
5887 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
0558056c 5888 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
e4e74273 5889 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
5890
5891 /* Issue READ_REV to collect vpd and FW information. */
49198b37 5892 vpd_size = SLI4_PAGE_SIZE;
da0436e9
JS
5893 vpd = kzalloc(vpd_size, GFP_KERNEL);
5894 if (!vpd) {
5895 rc = -ENOMEM;
5896 goto out_free_mbox;
5897 }
5898
5899 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
76a95d75
JS
5900 if (unlikely(rc)) {
5901 kfree(vpd);
5902 goto out_free_mbox;
5903 }
da0436e9 5904 mqe = &mboxq->u.mqe;
f1126688
JS
5905 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
5906 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
76a95d75
JS
5907 phba->hba_flag |= HBA_FCOE_MODE;
5908 else
5909 phba->hba_flag &= ~HBA_FCOE_MODE;
45ed1190
JS
5910
5911 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
5912 LPFC_DCBX_CEE_MODE)
5913 phba->hba_flag |= HBA_FIP_SUPPORT;
5914 else
5915 phba->hba_flag &= ~HBA_FIP_SUPPORT;
5916
c31098ce 5917 if (phba->sli_rev != LPFC_SLI_REV4) {
da0436e9
JS
5918 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5919 "0376 READ_REV Error. SLI Level %d "
5920 "FCoE enabled %d\n",
76a95d75 5921 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
da0436e9 5922 rc = -EIO;
76a95d75
JS
5923 kfree(vpd);
5924 goto out_free_mbox;
da0436e9 5925 }
cd1c8301
JS
5926
5927 /*
5928 * Retrieve sli4 device physical port name, failure of doing it
5929 * is considered as non-fatal.
5930 */
5931 rc = lpfc_sli4_retrieve_pport_name(phba);
5932 if (!rc)
5933 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5934 "3080 Successful retrieving SLI4 device "
5935 "physical port name: %s.\n", phba->Port);
5936
da0436e9
JS
5937 /*
5938 * Evaluate the read rev and vpd data. Populate the driver
5939 * state with the results. If this routine fails, the failure
5940 * is not fatal as the driver will use generic values.
5941 */
5942 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
5943 if (unlikely(!rc)) {
5944 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5945 "0377 Error %d parsing vpd. "
5946 "Using defaults.\n", rc);
5947 rc = 0;
5948 }
76a95d75 5949 kfree(vpd);
da0436e9 5950
f1126688
JS
5951 /* Save information as VPD data */
5952 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
5953 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
5954 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
5955 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
5956 &mqe->un.read_rev);
5957 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
5958 &mqe->un.read_rev);
5959 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
5960 &mqe->un.read_rev);
5961 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
5962 &mqe->un.read_rev);
5963 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
5964 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
5965 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
5966 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
5967 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
5968 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
5969 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5970 "(%d):0380 READ_REV Status x%x "
5971 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
5972 mboxq->vport ? mboxq->vport->vpi : 0,
5973 bf_get(lpfc_mqe_status, mqe),
5974 phba->vpd.rev.opFwName,
5975 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
5976 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
5977
5978 /*
5979 * Discover the port's supported feature set and match it against the
5980 * hosts requests.
5981 */
5982 lpfc_request_features(phba, mboxq);
5983 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5984 if (unlikely(rc)) {
5985 rc = -EIO;
76a95d75 5986 goto out_free_mbox;
da0436e9
JS
5987 }
5988
5989 /*
5990 * The port must support FCP initiator mode as this is the
5991 * only mode running in the host.
5992 */
5993 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
5994 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5995 "0378 No support for fcpi mode.\n");
5996 ftr_rsp++;
5997 }
fedd3b7b
JS
5998 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
5999 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6000 else
6001 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
da0436e9
JS
6002 /*
6003 * If the port cannot support the host's requested features
6004 * then turn off the global config parameters to disable the
6005 * feature in the driver. This is not a fatal error.
6006 */
bf08611b
JS
6007 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6008 if (phba->cfg_enable_bg) {
6009 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6010 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6011 else
6012 ftr_rsp++;
6013 }
da0436e9
JS
6014
6015 if (phba->max_vpi && phba->cfg_enable_npiv &&
6016 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6017 ftr_rsp++;
6018
6019 if (ftr_rsp) {
6020 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6021 "0379 Feature Mismatch Data: x%08x %08x "
6022 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6023 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6024 phba->cfg_enable_npiv, phba->max_vpi);
6025 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6026 phba->cfg_enable_bg = 0;
6027 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6028 phba->cfg_enable_npiv = 0;
6029 }
6030
6031 /* These SLI3 features are assumed in SLI4 */
6032 spin_lock_irq(&phba->hbalock);
6033 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6034 spin_unlock_irq(&phba->hbalock);
6035
6d368e53
JS
6036 /*
6037 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6038 * calls depends on these resources to complete port setup.
6039 */
6040 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6041 if (rc) {
6042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6043 "2920 Failed to alloc Resource IDs "
6044 "rc = x%x\n", rc);
6045 goto out_free_mbox;
6046 }
6047
da0436e9 6048 /* Read the port's service parameters. */
9f1177a3
JS
6049 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6050 if (rc) {
6051 phba->link_state = LPFC_HBA_ERROR;
6052 rc = -ENOMEM;
76a95d75 6053 goto out_free_mbox;
9f1177a3
JS
6054 }
6055
da0436e9
JS
6056 mboxq->vport = vport;
6057 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6058 mp = (struct lpfc_dmabuf *) mboxq->context1;
6059 if (rc == MBX_SUCCESS) {
6060 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6061 rc = 0;
6062 }
6063
6064 /*
6065 * This memory was allocated by the lpfc_read_sparam routine. Release
6066 * it to the mbuf pool.
6067 */
6068 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6069 kfree(mp);
6070 mboxq->context1 = NULL;
6071 if (unlikely(rc)) {
6072 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6073 "0382 READ_SPARAM command failed "
6074 "status %d, mbxStatus x%x\n",
6075 rc, bf_get(lpfc_mqe_status, mqe));
6076 phba->link_state = LPFC_HBA_ERROR;
6077 rc = -EIO;
76a95d75 6078 goto out_free_mbox;
da0436e9
JS
6079 }
6080
0558056c 6081 lpfc_update_vport_wwn(vport);
da0436e9
JS
6082
6083 /* Update the fc_host data structures with new wwn. */
6084 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6085 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6086
6087 /* Register SGL pool to the device using non-embedded mailbox command */
6d368e53
JS
6088 if (!phba->sli4_hba.extents_in_use) {
6089 rc = lpfc_sli4_post_els_sgl_list(phba);
6090 if (unlikely(rc)) {
6091 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6092 "0582 Error %d during els sgl post "
6093 "operation\n", rc);
6094 rc = -ENODEV;
6095 goto out_free_mbox;
6096 }
6097 } else {
6098 rc = lpfc_sli4_post_els_sgl_list_ext(phba);
6099 if (unlikely(rc)) {
6100 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6101 "2560 Error %d during els sgl post "
6102 "operation\n", rc);
6103 rc = -ENODEV;
6104 goto out_free_mbox;
6105 }
da0436e9
JS
6106 }
6107
6108 /* Register SCSI SGL pool to the device */
6109 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6110 if (unlikely(rc)) {
6d368e53 6111 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
6112 "0383 Error %d during scsi sgl post "
6113 "operation\n", rc);
da0436e9
JS
6114 /* Some Scsi buffers were moved to the abort scsi list */
6115 /* A pci function reset will repost them */
6116 rc = -ENODEV;
76a95d75 6117 goto out_free_mbox;
da0436e9
JS
6118 }
6119
6120 /* Post the rpi header region to the device. */
6121 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6122 if (unlikely(rc)) {
6123 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6124 "0393 Error %d during rpi post operation\n",
6125 rc);
6126 rc = -ENODEV;
76a95d75 6127 goto out_free_mbox;
da0436e9 6128 }
da0436e9 6129
5350d872
JS
6130 /* Create all the SLI4 queues */
6131 rc = lpfc_sli4_queue_create(phba);
6132 if (rc) {
6133 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6134 "3089 Failed to allocate queues\n");
6135 rc = -ENODEV;
6136 goto out_stop_timers;
6137 }
da0436e9
JS
6138 /* Set up all the queues to the device */
6139 rc = lpfc_sli4_queue_setup(phba);
6140 if (unlikely(rc)) {
6141 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6142 "0381 Error %d during queue setup.\n ", rc);
5350d872 6143 goto out_destroy_queue;
da0436e9
JS
6144 }
6145
6146 /* Arm the CQs and then EQs on device */
6147 lpfc_sli4_arm_cqeq_intr(phba);
6148
6149 /* Indicate device interrupt mode */
6150 phba->sli4_hba.intr_enable = 1;
6151
6152 /* Allow asynchronous mailbox command to go through */
6153 spin_lock_irq(&phba->hbalock);
6154 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6155 spin_unlock_irq(&phba->hbalock);
6156
6157 /* Post receive buffers to the device */
6158 lpfc_sli4_rb_setup(phba);
6159
fc2b989b
JS
6160 /* Reset HBA FCF states after HBA reset */
6161 phba->fcf.fcf_flag = 0;
6162 phba->fcf.current_rec.flag = 0;
6163
da0436e9 6164 /* Start the ELS watchdog timer */
8fa38513
JS
6165 mod_timer(&vport->els_tmofunc,
6166 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
6167
6168 /* Start heart beat timer */
6169 mod_timer(&phba->hb_tmofunc,
6170 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6171 phba->hb_outstanding = 0;
6172 phba->last_completion_time = jiffies;
6173
6174 /* Start error attention (ERATT) polling timer */
6175 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6176
75baf696
JS
6177 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6178 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6179 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6180 if (!rc) {
6181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6182 "2829 This device supports "
6183 "Advanced Error Reporting (AER)\n");
6184 spin_lock_irq(&phba->hbalock);
6185 phba->hba_flag |= HBA_AER_ENABLED;
6186 spin_unlock_irq(&phba->hbalock);
6187 } else {
6188 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6189 "2830 This device does not support "
6190 "Advanced Error Reporting (AER)\n");
6191 phba->cfg_aer_support = 0;
6192 }
0a96e975 6193 rc = 0;
75baf696
JS
6194 }
6195
76a95d75
JS
6196 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6197 /*
6198 * The FC Port needs to register FCFI (index 0)
6199 */
6200 lpfc_reg_fcfi(phba, mboxq);
6201 mboxq->vport = phba->pport;
6202 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
9589b062 6203 if (rc != MBX_SUCCESS)
76a95d75 6204 goto out_unset_queue;
9589b062
JS
6205 rc = 0;
6206 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6207 &mboxq->u.mqe.un.reg_fcfi);
026abb87
JS
6208
6209 /* Check if the port is configured to be disabled */
6210 lpfc_sli_read_link_ste(phba);
76a95d75 6211 }
026abb87 6212
da0436e9
JS
6213 /*
6214 * The port is ready, set the host's link state to LINK_DOWN
6215 * in preparation for link interrupts.
6216 */
da0436e9
JS
6217 spin_lock_irq(&phba->hbalock);
6218 phba->link_state = LPFC_LINK_DOWN;
6219 spin_unlock_irq(&phba->hbalock);
026abb87
JS
6220 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6221 (phba->hba_flag & LINK_DISABLED)) {
6222 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6223 "3103 Adapter Link is disabled.\n");
6224 lpfc_down_link(phba, mboxq);
6225 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6226 if (rc != MBX_SUCCESS) {
6227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6228 "3104 Adapter failed to issue "
6229 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6230 goto out_unset_queue;
6231 }
6232 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
fedd3b7b 6233 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
5350d872
JS
6234 if (rc)
6235 goto out_unset_queue;
6236 }
6237 mempool_free(mboxq, phba->mbox_mem_pool);
6238 return rc;
76a95d75 6239out_unset_queue:
da0436e9 6240 /* Unset all the queues set up in this routine when error out */
5350d872
JS
6241 lpfc_sli4_queue_unset(phba);
6242out_destroy_queue:
6243 lpfc_sli4_queue_destroy(phba);
da0436e9 6244out_stop_timers:
5350d872 6245 lpfc_stop_hba_timers(phba);
da0436e9
JS
6246out_free_mbox:
6247 mempool_free(mboxq, phba->mbox_mem_pool);
6248 return rc;
6249}
6250
6251/**
6252 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6253 * @ptr: context object - pointer to hba structure.
6254 *
6255 * This is the callback function for mailbox timer. The mailbox
6256 * timer is armed when a new mailbox command is issued and the timer
6257 * is deleted when the mailbox complete. The function is called by
6258 * the kernel timer code when a mailbox does not complete within
6259 * expected time. This function wakes up the worker thread to
6260 * process the mailbox timeout and returns. All the processing is
6261 * done by the worker thread function lpfc_mbox_timeout_handler.
6262 **/
6263void
6264lpfc_mbox_timeout(unsigned long ptr)
6265{
6266 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6267 unsigned long iflag;
6268 uint32_t tmo_posted;
6269
6270 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6271 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6272 if (!tmo_posted)
6273 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6274 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6275
6276 if (!tmo_posted)
6277 lpfc_worker_wake_up(phba);
6278 return;
6279}
6280
6281
6282/**
6283 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6284 * @phba: Pointer to HBA context object.
6285 *
6286 * This function is called from worker thread when a mailbox command times out.
6287 * The caller is not required to hold any locks. This function will reset the
6288 * HBA and recover all the pending commands.
6289 **/
6290void
6291lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6292{
6293 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 6294 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
6295 struct lpfc_sli *psli = &phba->sli;
6296 struct lpfc_sli_ring *pring;
6297
6298 /* Check the pmbox pointer first. There is a race condition
6299 * between the mbox timeout handler getting executed in the
6300 * worklist and the mailbox actually completing. When this
6301 * race condition occurs, the mbox_active will be NULL.
6302 */
6303 spin_lock_irq(&phba->hbalock);
6304 if (pmbox == NULL) {
6305 lpfc_printf_log(phba, KERN_WARNING,
6306 LOG_MBOX | LOG_SLI,
6307 "0353 Active Mailbox cleared - mailbox timeout "
6308 "exiting\n");
6309 spin_unlock_irq(&phba->hbalock);
6310 return;
6311 }
6312
6313 /* Mbox cmd <mbxCommand> timeout */
6314 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6315 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6316 mb->mbxCommand,
6317 phba->pport->port_state,
6318 phba->sli.sli_flag,
6319 phba->sli.mbox_active);
6320 spin_unlock_irq(&phba->hbalock);
6321
6322 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6323 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
25985edc 6324 * it to fail all outstanding SCSI IO.
da0436e9
JS
6325 */
6326 spin_lock_irq(&phba->pport->work_port_lock);
6327 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6328 spin_unlock_irq(&phba->pport->work_port_lock);
6329 spin_lock_irq(&phba->hbalock);
6330 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 6331 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
6332 spin_unlock_irq(&phba->hbalock);
6333
6334 pring = &psli->ring[psli->fcp_ring];
6335 lpfc_sli_abort_iocb_ring(phba, pring);
6336
6337 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6338 "0345 Resetting board due to mailbox timeout\n");
6339
6340 /* Reset the HBA device */
6341 lpfc_reset_hba(phba);
6342}
6343
6344/**
6345 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6346 * @phba: Pointer to HBA context object.
6347 * @pmbox: Pointer to mailbox object.
6348 * @flag: Flag indicating how the mailbox need to be processed.
6349 *
6350 * This function is called by discovery code and HBA management code
6351 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6352 * function gets the hbalock to protect the data structures.
6353 * The mailbox command can be submitted in polling mode, in which case
6354 * this function will wait in a polling loop for the completion of the
6355 * mailbox.
6356 * If the mailbox is submitted in no_wait mode (not polling) the
6357 * function will submit the command and returns immediately without waiting
6358 * for the mailbox completion. The no_wait is supported only when HBA
6359 * is in SLI2/SLI3 mode - interrupts are enabled.
6360 * The SLI interface allows only one mailbox pending at a time. If the
6361 * mailbox is issued in polling mode and there is already a mailbox
6362 * pending, then the function will return an error. If the mailbox is issued
6363 * in NO_WAIT mode and there is a mailbox pending already, the function
6364 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6365 * The sli layer owns the mailbox object until the completion of mailbox
6366 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6367 * return codes the caller owns the mailbox command after the return of
6368 * the function.
e59058c4 6369 **/
3772a991
JS
6370static int
6371lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6372 uint32_t flag)
dea3101e 6373{
dea3101e 6374 MAILBOX_t *mb;
2e0fef85 6375 struct lpfc_sli *psli = &phba->sli;
dea3101e 6376 uint32_t status, evtctr;
9940b97b 6377 uint32_t ha_copy, hc_copy;
dea3101e 6378 int i;
09372820 6379 unsigned long timeout;
dea3101e 6380 unsigned long drvr_flag = 0;
34b02dcd 6381 uint32_t word0, ldata;
dea3101e 6382 void __iomem *to_slim;
58da1ffb
JS
6383 int processing_queue = 0;
6384
6385 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6386 if (!pmbox) {
8568a4d2 6387 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 6388 /* processing mbox queue from intr_handler */
3772a991
JS
6389 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6390 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6391 return MBX_SUCCESS;
6392 }
58da1ffb 6393 processing_queue = 1;
58da1ffb
JS
6394 pmbox = lpfc_mbox_get(phba);
6395 if (!pmbox) {
6396 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6397 return MBX_SUCCESS;
6398 }
6399 }
dea3101e 6400
ed957684 6401 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 6402 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 6403 if(!pmbox->vport) {
58da1ffb 6404 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 6405 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 6406 LOG_MBOX | LOG_VPORT,
e8b62011 6407 "1806 Mbox x%x failed. No vport\n",
3772a991 6408 pmbox->u.mb.mbxCommand);
ed957684 6409 dump_stack();
58da1ffb 6410 goto out_not_finished;
ed957684
JS
6411 }
6412 }
6413
8d63f375 6414 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
6415 if (unlikely(pci_channel_offline(phba->pcidev))) {
6416 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6417 goto out_not_finished;
6418 }
8d63f375 6419
a257bf90
JS
6420 /* If HBA has a deferred error attention, fail the iocb. */
6421 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6422 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6423 goto out_not_finished;
6424 }
6425
dea3101e 6426 psli = &phba->sli;
92d7f7b0 6427
3772a991 6428 mb = &pmbox->u.mb;
dea3101e 6429 status = MBX_SUCCESS;
6430
2e0fef85
JS
6431 if (phba->link_state == LPFC_HBA_ERROR) {
6432 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
6433
6434 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6435 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6436 "(%d):0311 Mailbox command x%x cannot "
6437 "issue Data: x%x x%x\n",
6438 pmbox->vport ? pmbox->vport->vpi : 0,
6439 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 6440 goto out_not_finished;
41415862
JW
6441 }
6442
9940b97b
JS
6443 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6444 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6445 !(hc_copy & HC_MBINT_ENA)) {
6446 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6447 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
3772a991
JS
6448 "(%d):2528 Mailbox command x%x cannot "
6449 "issue Data: x%x x%x\n",
6450 pmbox->vport ? pmbox->vport->vpi : 0,
6451 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
9940b97b
JS
6452 goto out_not_finished;
6453 }
9290831f
JS
6454 }
6455
dea3101e 6456 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6457 /* Polling for a mbox command when another one is already active
6458 * is not allowed in SLI. Also, the driver must have established
6459 * SLI2 mode to queue and process multiple mbox commands.
6460 */
6461
6462 if (flag & MBX_POLL) {
2e0fef85 6463 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6464
6465 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6466 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6467 "(%d):2529 Mailbox command x%x "
6468 "cannot issue Data: x%x x%x\n",
6469 pmbox->vport ? pmbox->vport->vpi : 0,
6470 pmbox->u.mb.mbxCommand,
6471 psli->sli_flag, flag);
58da1ffb 6472 goto out_not_finished;
dea3101e 6473 }
6474
3772a991 6475 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 6476 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6477 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6478 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6479 "(%d):2530 Mailbox command x%x "
6480 "cannot issue Data: x%x x%x\n",
6481 pmbox->vport ? pmbox->vport->vpi : 0,
6482 pmbox->u.mb.mbxCommand,
6483 psli->sli_flag, flag);
58da1ffb 6484 goto out_not_finished;
dea3101e 6485 }
6486
dea3101e 6487 /* Another mailbox command is still being processed, queue this
6488 * command to be processed later.
6489 */
6490 lpfc_mbox_put(phba, pmbox);
6491
6492 /* Mbox cmd issue - BUSY */
ed957684 6493 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6494 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 6495 "x%x x%x x%x x%x\n",
92d7f7b0
JS
6496 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6497 mb->mbxCommand, phba->pport->port_state,
6498 psli->sli_flag, flag);
dea3101e 6499
6500 psli->slistat.mbox_busy++;
2e0fef85 6501 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6502
858c9f6c
JS
6503 if (pmbox->vport) {
6504 lpfc_debugfs_disc_trc(pmbox->vport,
6505 LPFC_DISC_TRC_MBOX_VPORT,
6506 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6507 (uint32_t)mb->mbxCommand,
6508 mb->un.varWords[0], mb->un.varWords[1]);
6509 }
6510 else {
6511 lpfc_debugfs_disc_trc(phba->pport,
6512 LPFC_DISC_TRC_MBOX,
6513 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6514 (uint32_t)mb->mbxCommand,
6515 mb->un.varWords[0], mb->un.varWords[1]);
6516 }
6517
2e0fef85 6518 return MBX_BUSY;
dea3101e 6519 }
6520
dea3101e 6521 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6522
6523 /* If we are not polling, we MUST be in SLI2 mode */
6524 if (flag != MBX_POLL) {
3772a991 6525 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 6526 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 6527 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6528 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 6529 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
6530 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6531 "(%d):2531 Mailbox command x%x "
6532 "cannot issue Data: x%x x%x\n",
6533 pmbox->vport ? pmbox->vport->vpi : 0,
6534 pmbox->u.mb.mbxCommand,
6535 psli->sli_flag, flag);
58da1ffb 6536 goto out_not_finished;
dea3101e 6537 }
6538 /* timeout active mbox command */
a309a6b6 6539 mod_timer(&psli->mbox_tmo, (jiffies +
a183a15f 6540 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
dea3101e 6541 }
6542
6543 /* Mailbox cmd <cmd> issue */
ed957684 6544 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 6545 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 6546 "x%x\n",
e8b62011 6547 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
6548 mb->mbxCommand, phba->pport->port_state,
6549 psli->sli_flag, flag);
dea3101e 6550
858c9f6c
JS
6551 if (mb->mbxCommand != MBX_HEARTBEAT) {
6552 if (pmbox->vport) {
6553 lpfc_debugfs_disc_trc(pmbox->vport,
6554 LPFC_DISC_TRC_MBOX_VPORT,
6555 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6556 (uint32_t)mb->mbxCommand,
6557 mb->un.varWords[0], mb->un.varWords[1]);
6558 }
6559 else {
6560 lpfc_debugfs_disc_trc(phba->pport,
6561 LPFC_DISC_TRC_MBOX,
6562 "MBOX Send: cmd:x%x mb:x%x x%x",
6563 (uint32_t)mb->mbxCommand,
6564 mb->un.varWords[0], mb->un.varWords[1]);
6565 }
6566 }
6567
dea3101e 6568 psli->slistat.mbox_cmd++;
6569 evtctr = psli->slistat.mbox_event;
6570
6571 /* next set own bit for the adapter and copy over command word */
6572 mb->mbxOwner = OWN_CHIP;
6573
3772a991 6574 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7a470277
JS
6575 /* Populate mbox extension offset word. */
6576 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6577 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6578 = (uint8_t *)phba->mbox_ext
6579 - (uint8_t *)phba->mbox;
6580 }
6581
6582 /* Copy the mailbox extension data */
6583 if (pmbox->in_ext_byte_len && pmbox->context2) {
6584 lpfc_sli_pcimem_bcopy(pmbox->context2,
6585 (uint8_t *)phba->mbox_ext,
6586 pmbox->in_ext_byte_len);
6587 }
6588 /* Copy command data to host SLIM area */
34b02dcd 6589 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 6590 } else {
7a470277
JS
6591 /* Populate mbox extension offset word. */
6592 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6593 *(((uint32_t *)mb) + pmbox->mbox_offset_word)
6594 = MAILBOX_HBA_EXT_OFFSET;
6595
6596 /* Copy the mailbox extension data */
6597 if (pmbox->in_ext_byte_len && pmbox->context2) {
6598 lpfc_memcpy_to_slim(phba->MBslimaddr +
6599 MAILBOX_HBA_EXT_OFFSET,
6600 pmbox->context2, pmbox->in_ext_byte_len);
6601
6602 }
9290831f 6603 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 6604 /* copy command data into host mbox for cmpl */
34b02dcd 6605 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 6606 }
6607
6608 /* First copy mbox command data to HBA SLIM, skip past first
6609 word */
6610 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6611 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
6612 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6613
6614 /* Next copy over first word, with mbxOwner set */
34b02dcd 6615 ldata = *((uint32_t *)mb);
dea3101e 6616 to_slim = phba->MBslimaddr;
6617 writel(ldata, to_slim);
6618 readl(to_slim); /* flush */
6619
6620 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6621 /* switch over to host mailbox */
3772a991 6622 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 6623 }
6624 }
6625
6626 wmb();
dea3101e 6627
6628 switch (flag) {
6629 case MBX_NOWAIT:
09372820 6630 /* Set up reference to mailbox command */
dea3101e 6631 psli->mbox_active = pmbox;
09372820
JS
6632 /* Interrupt board to do it */
6633 writel(CA_MBATT, phba->CAregaddr);
6634 readl(phba->CAregaddr); /* flush */
6635 /* Don't wait for it to finish, just return */
dea3101e 6636 break;
6637
6638 case MBX_POLL:
09372820 6639 /* Set up null reference to mailbox command */
dea3101e 6640 psli->mbox_active = NULL;
09372820
JS
6641 /* Interrupt board to do it */
6642 writel(CA_MBATT, phba->CAregaddr);
6643 readl(phba->CAregaddr); /* flush */
6644
3772a991 6645 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6646 /* First read mbox status word */
34b02dcd 6647 word0 = *((uint32_t *)phba->mbox);
dea3101e 6648 word0 = le32_to_cpu(word0);
6649 } else {
6650 /* First read mbox status word */
9940b97b
JS
6651 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6652 spin_unlock_irqrestore(&phba->hbalock,
6653 drvr_flag);
6654 goto out_not_finished;
6655 }
dea3101e 6656 }
6657
6658 /* Read the HBA Host Attention Register */
9940b97b
JS
6659 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6660 spin_unlock_irqrestore(&phba->hbalock,
6661 drvr_flag);
6662 goto out_not_finished;
6663 }
a183a15f
JS
6664 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6665 1000) + jiffies;
09372820 6666 i = 0;
dea3101e 6667 /* Wait for command to complete */
41415862
JW
6668 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6669 (!(ha_copy & HA_MBATT) &&
2e0fef85 6670 (phba->link_state > LPFC_WARM_START))) {
09372820 6671 if (time_after(jiffies, timeout)) {
dea3101e 6672 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 6673 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 6674 drvr_flag);
58da1ffb 6675 goto out_not_finished;
dea3101e 6676 }
6677
6678 /* Check if we took a mbox interrupt while we were
6679 polling */
6680 if (((word0 & OWN_CHIP) != OWN_CHIP)
6681 && (evtctr != psli->slistat.mbox_event))
6682 break;
6683
09372820
JS
6684 if (i++ > 10) {
6685 spin_unlock_irqrestore(&phba->hbalock,
6686 drvr_flag);
6687 msleep(1);
6688 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6689 }
dea3101e 6690
3772a991 6691 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6692 /* First copy command data */
34b02dcd 6693 word0 = *((uint32_t *)phba->mbox);
dea3101e 6694 word0 = le32_to_cpu(word0);
6695 if (mb->mbxCommand == MBX_CONFIG_PORT) {
6696 MAILBOX_t *slimmb;
34b02dcd 6697 uint32_t slimword0;
dea3101e 6698 /* Check real SLIM for any errors */
6699 slimword0 = readl(phba->MBslimaddr);
6700 slimmb = (MAILBOX_t *) & slimword0;
6701 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6702 && slimmb->mbxStatus) {
6703 psli->sli_flag &=
3772a991 6704 ~LPFC_SLI_ACTIVE;
dea3101e 6705 word0 = slimword0;
6706 }
6707 }
6708 } else {
6709 /* First copy command data */
6710 word0 = readl(phba->MBslimaddr);
6711 }
6712 /* Read the HBA Host Attention Register */
9940b97b
JS
6713 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6714 spin_unlock_irqrestore(&phba->hbalock,
6715 drvr_flag);
6716 goto out_not_finished;
6717 }
dea3101e 6718 }
6719
3772a991 6720 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 6721 /* copy results back to user */
34b02dcd 6722 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
7a470277
JS
6723 /* Copy the mailbox extension data */
6724 if (pmbox->out_ext_byte_len && pmbox->context2) {
6725 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
6726 pmbox->context2,
6727 pmbox->out_ext_byte_len);
6728 }
dea3101e 6729 } else {
6730 /* First copy command data */
6731 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
6732 MAILBOX_CMD_SIZE);
7a470277
JS
6733 /* Copy the mailbox extension data */
6734 if (pmbox->out_ext_byte_len && pmbox->context2) {
6735 lpfc_memcpy_from_slim(pmbox->context2,
6736 phba->MBslimaddr +
6737 MAILBOX_HBA_EXT_OFFSET,
6738 pmbox->out_ext_byte_len);
dea3101e 6739 }
6740 }
6741
6742 writel(HA_MBATT, phba->HAregaddr);
6743 readl(phba->HAregaddr); /* flush */
6744
6745 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6746 status = mb->mbxStatus;
6747 }
6748
2e0fef85
JS
6749 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6750 return status;
58da1ffb
JS
6751
6752out_not_finished:
6753 if (processing_queue) {
da0436e9 6754 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
6755 lpfc_mbox_cmpl_put(phba, pmbox);
6756 }
6757 return MBX_NOT_FINISHED;
dea3101e 6758}
6759
f1126688
JS
6760/**
6761 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
6762 * @phba: Pointer to HBA context object.
6763 *
6764 * The function blocks the posting of SLI4 asynchronous mailbox commands from
6765 * the driver internal pending mailbox queue. It will then try to wait out the
6766 * possible outstanding mailbox command before return.
6767 *
6768 * Returns:
6769 * 0 - the outstanding mailbox command completed; otherwise, the wait for
6770 * the outstanding mailbox command timed out.
6771 **/
6772static int
6773lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
6774{
6775 struct lpfc_sli *psli = &phba->sli;
f1126688 6776 int rc = 0;
a183a15f 6777 unsigned long timeout = 0;
f1126688
JS
6778
6779 /* Mark the asynchronous mailbox command posting as blocked */
6780 spin_lock_irq(&phba->hbalock);
6781 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
f1126688
JS
6782 /* Determine how long we might wait for the active mailbox
6783 * command to be gracefully completed by firmware.
6784 */
a183a15f
JS
6785 if (phba->sli.mbox_active)
6786 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
6787 phba->sli.mbox_active) *
6788 1000) + jiffies;
6789 spin_unlock_irq(&phba->hbalock);
6790
f1126688
JS
6791 /* Wait for the outstnading mailbox command to complete */
6792 while (phba->sli.mbox_active) {
6793 /* Check active mailbox complete status every 2ms */
6794 msleep(2);
6795 if (time_after(jiffies, timeout)) {
6796 /* Timeout, marked the outstanding cmd not complete */
6797 rc = 1;
6798 break;
6799 }
6800 }
6801
6802 /* Can not cleanly block async mailbox command, fails it */
6803 if (rc) {
6804 spin_lock_irq(&phba->hbalock);
6805 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6806 spin_unlock_irq(&phba->hbalock);
6807 }
6808 return rc;
6809}
6810
6811/**
6812 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
6813 * @phba: Pointer to HBA context object.
6814 *
6815 * The function unblocks and resume posting of SLI4 asynchronous mailbox
6816 * commands from the driver internal pending mailbox queue. It makes sure
6817 * that there is no outstanding mailbox command before resuming posting
6818 * asynchronous mailbox commands. If, for any reason, there is outstanding
6819 * mailbox command, it will try to wait it out before resuming asynchronous
6820 * mailbox command posting.
6821 **/
6822static void
6823lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
6824{
6825 struct lpfc_sli *psli = &phba->sli;
6826
6827 spin_lock_irq(&phba->hbalock);
6828 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6829 /* Asynchronous mailbox posting is not blocked, do nothing */
6830 spin_unlock_irq(&phba->hbalock);
6831 return;
6832 }
6833
6834 /* Outstanding synchronous mailbox command is guaranteed to be done,
6835 * successful or timeout, after timing-out the outstanding mailbox
6836 * command shall always be removed, so just unblock posting async
6837 * mailbox command and resume
6838 */
6839 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6840 spin_unlock_irq(&phba->hbalock);
6841
6842 /* wake up worker thread to post asynchronlous mailbox command */
6843 lpfc_worker_wake_up(phba);
6844}
6845
da0436e9
JS
6846/**
6847 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
6848 * @phba: Pointer to HBA context object.
6849 * @mboxq: Pointer to mailbox object.
6850 *
6851 * The function posts a mailbox to the port. The mailbox is expected
6852 * to be comletely filled in and ready for the port to operate on it.
6853 * This routine executes a synchronous completion operation on the
6854 * mailbox by polling for its completion.
6855 *
6856 * The caller must not be holding any locks when calling this routine.
6857 *
6858 * Returns:
6859 * MBX_SUCCESS - mailbox posted successfully
6860 * Any of the MBX error values.
6861 **/
6862static int
6863lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
6864{
6865 int rc = MBX_SUCCESS;
6866 unsigned long iflag;
6867 uint32_t db_ready;
6868 uint32_t mcqe_status;
6869 uint32_t mbx_cmnd;
6870 unsigned long timeout;
6871 struct lpfc_sli *psli = &phba->sli;
6872 struct lpfc_mqe *mb = &mboxq->u.mqe;
6873 struct lpfc_bmbx_create *mbox_rgn;
6874 struct dma_address *dma_address;
6875 struct lpfc_register bmbx_reg;
6876
6877 /*
6878 * Only one mailbox can be active to the bootstrap mailbox region
6879 * at a time and there is no queueing provided.
6880 */
6881 spin_lock_irqsave(&phba->hbalock, iflag);
6882 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6883 spin_unlock_irqrestore(&phba->hbalock, iflag);
6884 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 6885 "(%d):2532 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
6886 "cannot issue Data: x%x x%x\n",
6887 mboxq->vport ? mboxq->vport->vpi : 0,
6888 mboxq->u.mb.mbxCommand,
a183a15f
JS
6889 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6890 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6891 psli->sli_flag, MBX_POLL);
6892 return MBXERR_ERROR;
6893 }
6894 /* The server grabs the token and owns it until release */
6895 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6896 phba->sli.mbox_active = mboxq;
6897 spin_unlock_irqrestore(&phba->hbalock, iflag);
6898
6899 /*
6900 * Initialize the bootstrap memory region to avoid stale data areas
6901 * in the mailbox post. Then copy the caller's mailbox contents to
6902 * the bmbx mailbox region.
6903 */
6904 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
6905 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
6906 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
6907 sizeof(struct lpfc_mqe));
6908
6909 /* Post the high mailbox dma address to the port and wait for ready. */
6910 dma_address = &phba->sli4_hba.bmbx.dma_address;
6911 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
6912
a183a15f 6913 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
da0436e9
JS
6914 * 1000) + jiffies;
6915 do {
6916 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6917 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6918 if (!db_ready)
6919 msleep(2);
6920
6921 if (time_after(jiffies, timeout)) {
6922 rc = MBXERR_ERROR;
6923 goto exit;
6924 }
6925 } while (!db_ready);
6926
6927 /* Post the low mailbox dma address to the port. */
6928 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
a183a15f 6929 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
da0436e9
JS
6930 * 1000) + jiffies;
6931 do {
6932 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
6933 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
6934 if (!db_ready)
6935 msleep(2);
6936
6937 if (time_after(jiffies, timeout)) {
6938 rc = MBXERR_ERROR;
6939 goto exit;
6940 }
6941 } while (!db_ready);
6942
6943 /*
6944 * Read the CQ to ensure the mailbox has completed.
6945 * If so, update the mailbox status so that the upper layers
6946 * can complete the request normally.
6947 */
6948 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
6949 sizeof(struct lpfc_mqe));
6950 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
6951 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
6952 sizeof(struct lpfc_mcqe));
6953 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
0558056c
JS
6954 /*
6955 * When the CQE status indicates a failure and the mailbox status
6956 * indicates success then copy the CQE status into the mailbox status
6957 * (and prefix it with x4000).
6958 */
da0436e9 6959 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
0558056c
JS
6960 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
6961 bf_set(lpfc_mqe_status, mb,
6962 (LPFC_MBX_ERROR_RANGE | mcqe_status));
da0436e9 6963 rc = MBXERR_ERROR;
d7c47992
JS
6964 } else
6965 lpfc_sli4_swap_str(phba, mboxq);
da0436e9
JS
6966
6967 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 6968 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
da0436e9
JS
6969 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
6970 " x%x x%x CQ: x%x x%x x%x x%x\n",
a183a15f
JS
6971 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
6972 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
6973 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
6974 bf_get(lpfc_mqe_status, mb),
6975 mb->un.mb_words[0], mb->un.mb_words[1],
6976 mb->un.mb_words[2], mb->un.mb_words[3],
6977 mb->un.mb_words[4], mb->un.mb_words[5],
6978 mb->un.mb_words[6], mb->un.mb_words[7],
6979 mb->un.mb_words[8], mb->un.mb_words[9],
6980 mb->un.mb_words[10], mb->un.mb_words[11],
6981 mb->un.mb_words[12], mboxq->mcqe.word0,
6982 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
6983 mboxq->mcqe.trailer);
6984exit:
6985 /* We are holding the token, no needed for lock when release */
6986 spin_lock_irqsave(&phba->hbalock, iflag);
6987 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6988 phba->sli.mbox_active = NULL;
6989 spin_unlock_irqrestore(&phba->hbalock, iflag);
6990 return rc;
6991}
6992
6993/**
6994 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
6995 * @phba: Pointer to HBA context object.
6996 * @pmbox: Pointer to mailbox object.
6997 * @flag: Flag indicating how the mailbox need to be processed.
6998 *
6999 * This function is called by discovery code and HBA management code to submit
7000 * a mailbox command to firmware with SLI-4 interface spec.
7001 *
7002 * Return codes the caller owns the mailbox command after the return of the
7003 * function.
7004 **/
7005static int
7006lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7007 uint32_t flag)
7008{
7009 struct lpfc_sli *psli = &phba->sli;
7010 unsigned long iflags;
7011 int rc;
7012
b76f2dc9
JS
7013 /* dump from issue mailbox command if setup */
7014 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7015
8fa38513
JS
7016 rc = lpfc_mbox_dev_check(phba);
7017 if (unlikely(rc)) {
7018 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7019 "(%d):2544 Mailbox command x%x (x%x/x%x) "
8fa38513
JS
7020 "cannot issue Data: x%x x%x\n",
7021 mboxq->vport ? mboxq->vport->vpi : 0,
7022 mboxq->u.mb.mbxCommand,
a183a15f
JS
7023 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7024 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
8fa38513
JS
7025 psli->sli_flag, flag);
7026 goto out_not_finished;
7027 }
7028
da0436e9
JS
7029 /* Detect polling mode and jump to a handler */
7030 if (!phba->sli4_hba.intr_enable) {
7031 if (flag == MBX_POLL)
7032 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7033 else
7034 rc = -EIO;
7035 if (rc != MBX_SUCCESS)
0558056c 7036 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
da0436e9 7037 "(%d):2541 Mailbox command x%x "
a183a15f
JS
7038 "(x%x/x%x) cannot issue Data: "
7039 "x%x x%x\n",
da0436e9
JS
7040 mboxq->vport ? mboxq->vport->vpi : 0,
7041 mboxq->u.mb.mbxCommand,
a183a15f
JS
7042 lpfc_sli_config_mbox_subsys_get(phba,
7043 mboxq),
7044 lpfc_sli_config_mbox_opcode_get(phba,
7045 mboxq),
da0436e9
JS
7046 psli->sli_flag, flag);
7047 return rc;
7048 } else if (flag == MBX_POLL) {
f1126688
JS
7049 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7050 "(%d):2542 Try to issue mailbox command "
a183a15f 7051 "x%x (x%x/x%x) synchronously ahead of async"
f1126688 7052 "mailbox command queue: x%x x%x\n",
da0436e9
JS
7053 mboxq->vport ? mboxq->vport->vpi : 0,
7054 mboxq->u.mb.mbxCommand,
a183a15f
JS
7055 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7056 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9 7057 psli->sli_flag, flag);
f1126688
JS
7058 /* Try to block the asynchronous mailbox posting */
7059 rc = lpfc_sli4_async_mbox_block(phba);
7060 if (!rc) {
7061 /* Successfully blocked, now issue sync mbox cmd */
7062 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7063 if (rc != MBX_SUCCESS)
7064 lpfc_printf_log(phba, KERN_ERR,
a183a15f
JS
7065 LOG_MBOX | LOG_SLI,
7066 "(%d):2597 Mailbox command "
7067 "x%x (x%x/x%x) cannot issue "
7068 "Data: x%x x%x\n",
7069 mboxq->vport ?
7070 mboxq->vport->vpi : 0,
7071 mboxq->u.mb.mbxCommand,
7072 lpfc_sli_config_mbox_subsys_get(phba,
7073 mboxq),
7074 lpfc_sli_config_mbox_opcode_get(phba,
7075 mboxq),
7076 psli->sli_flag, flag);
f1126688
JS
7077 /* Unblock the async mailbox posting afterward */
7078 lpfc_sli4_async_mbox_unblock(phba);
7079 }
7080 return rc;
da0436e9
JS
7081 }
7082
7083 /* Now, interrupt mode asynchrous mailbox command */
7084 rc = lpfc_mbox_cmd_check(phba, mboxq);
7085 if (rc) {
7086 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7087 "(%d):2543 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7088 "cannot issue Data: x%x x%x\n",
7089 mboxq->vport ? mboxq->vport->vpi : 0,
7090 mboxq->u.mb.mbxCommand,
a183a15f
JS
7091 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7092 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7093 psli->sli_flag, flag);
7094 goto out_not_finished;
7095 }
da0436e9
JS
7096
7097 /* Put the mailbox command to the driver internal FIFO */
7098 psli->slistat.mbox_busy++;
7099 spin_lock_irqsave(&phba->hbalock, iflags);
7100 lpfc_mbox_put(phba, mboxq);
7101 spin_unlock_irqrestore(&phba->hbalock, iflags);
7102 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7103 "(%d):0354 Mbox cmd issue - Enqueue Data: "
a183a15f 7104 "x%x (x%x/x%x) x%x x%x x%x\n",
da0436e9
JS
7105 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7106 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
a183a15f
JS
7107 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7108 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7109 phba->pport->port_state,
7110 psli->sli_flag, MBX_NOWAIT);
7111 /* Wake up worker thread to transport mailbox command from head */
7112 lpfc_worker_wake_up(phba);
7113
7114 return MBX_BUSY;
7115
7116out_not_finished:
7117 return MBX_NOT_FINISHED;
7118}
7119
7120/**
7121 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7122 * @phba: Pointer to HBA context object.
7123 *
7124 * This function is called by worker thread to send a mailbox command to
7125 * SLI4 HBA firmware.
7126 *
7127 **/
7128int
7129lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7130{
7131 struct lpfc_sli *psli = &phba->sli;
7132 LPFC_MBOXQ_t *mboxq;
7133 int rc = MBX_SUCCESS;
7134 unsigned long iflags;
7135 struct lpfc_mqe *mqe;
7136 uint32_t mbx_cmnd;
7137
7138 /* Check interrupt mode before post async mailbox command */
7139 if (unlikely(!phba->sli4_hba.intr_enable))
7140 return MBX_NOT_FINISHED;
7141
7142 /* Check for mailbox command service token */
7143 spin_lock_irqsave(&phba->hbalock, iflags);
7144 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7145 spin_unlock_irqrestore(&phba->hbalock, iflags);
7146 return MBX_NOT_FINISHED;
7147 }
7148 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7149 spin_unlock_irqrestore(&phba->hbalock, iflags);
7150 return MBX_NOT_FINISHED;
7151 }
7152 if (unlikely(phba->sli.mbox_active)) {
7153 spin_unlock_irqrestore(&phba->hbalock, iflags);
7154 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7155 "0384 There is pending active mailbox cmd\n");
7156 return MBX_NOT_FINISHED;
7157 }
7158 /* Take the mailbox command service token */
7159 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7160
7161 /* Get the next mailbox command from head of queue */
7162 mboxq = lpfc_mbox_get(phba);
7163
7164 /* If no more mailbox command waiting for post, we're done */
7165 if (!mboxq) {
7166 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7167 spin_unlock_irqrestore(&phba->hbalock, iflags);
7168 return MBX_SUCCESS;
7169 }
7170 phba->sli.mbox_active = mboxq;
7171 spin_unlock_irqrestore(&phba->hbalock, iflags);
7172
7173 /* Check device readiness for posting mailbox command */
7174 rc = lpfc_mbox_dev_check(phba);
7175 if (unlikely(rc))
7176 /* Driver clean routine will clean up pending mailbox */
7177 goto out_not_finished;
7178
7179 /* Prepare the mbox command to be posted */
7180 mqe = &mboxq->u.mqe;
7181 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7182
7183 /* Start timer for the mbox_tmo and log some mailbox post messages */
7184 mod_timer(&psli->mbox_tmo, (jiffies +
a183a15f 7185 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
da0436e9
JS
7186
7187 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
a183a15f 7188 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
da0436e9
JS
7189 "x%x x%x\n",
7190 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
a183a15f
JS
7191 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7192 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7193 phba->pport->port_state, psli->sli_flag);
7194
7195 if (mbx_cmnd != MBX_HEARTBEAT) {
7196 if (mboxq->vport) {
7197 lpfc_debugfs_disc_trc(mboxq->vport,
7198 LPFC_DISC_TRC_MBOX_VPORT,
7199 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7200 mbx_cmnd, mqe->un.mb_words[0],
7201 mqe->un.mb_words[1]);
7202 } else {
7203 lpfc_debugfs_disc_trc(phba->pport,
7204 LPFC_DISC_TRC_MBOX,
7205 "MBOX Send: cmd:x%x mb:x%x x%x",
7206 mbx_cmnd, mqe->un.mb_words[0],
7207 mqe->un.mb_words[1]);
7208 }
7209 }
7210 psli->slistat.mbox_cmd++;
7211
7212 /* Post the mailbox command to the port */
7213 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7214 if (rc != MBX_SUCCESS) {
7215 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
a183a15f 7216 "(%d):2533 Mailbox command x%x (x%x/x%x) "
da0436e9
JS
7217 "cannot issue Data: x%x x%x\n",
7218 mboxq->vport ? mboxq->vport->vpi : 0,
7219 mboxq->u.mb.mbxCommand,
a183a15f
JS
7220 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7221 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
da0436e9
JS
7222 psli->sli_flag, MBX_NOWAIT);
7223 goto out_not_finished;
7224 }
7225
7226 return rc;
7227
7228out_not_finished:
7229 spin_lock_irqsave(&phba->hbalock, iflags);
7230 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7231 __lpfc_mbox_cmpl_put(phba, mboxq);
7232 /* Release the token */
7233 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7234 phba->sli.mbox_active = NULL;
7235 spin_unlock_irqrestore(&phba->hbalock, iflags);
7236
7237 return MBX_NOT_FINISHED;
7238}
7239
7240/**
7241 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7242 * @phba: Pointer to HBA context object.
7243 * @pmbox: Pointer to mailbox object.
7244 * @flag: Flag indicating how the mailbox need to be processed.
7245 *
7246 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7247 * the API jump table function pointer from the lpfc_hba struct.
7248 *
7249 * Return codes the caller owns the mailbox command after the return of the
7250 * function.
7251 **/
7252int
7253lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7254{
7255 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7256}
7257
7258/**
25985edc 7259 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
da0436e9
JS
7260 * @phba: The hba struct for which this call is being executed.
7261 * @dev_grp: The HBA PCI-Device group number.
7262 *
7263 * This routine sets up the mbox interface API function jump table in @phba
7264 * struct.
7265 * Returns: 0 - success, -ENODEV - failure.
7266 **/
7267int
7268lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7269{
7270
7271 switch (dev_grp) {
7272 case LPFC_PCI_DEV_LP:
7273 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7274 phba->lpfc_sli_handle_slow_ring_event =
7275 lpfc_sli_handle_slow_ring_event_s3;
7276 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7277 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7278 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7279 break;
7280 case LPFC_PCI_DEV_OC:
7281 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7282 phba->lpfc_sli_handle_slow_ring_event =
7283 lpfc_sli_handle_slow_ring_event_s4;
7284 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7285 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7286 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7287 break;
7288 default:
7289 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7290 "1420 Invalid HBA PCI-device group: 0x%x\n",
7291 dev_grp);
7292 return -ENODEV;
7293 break;
7294 }
7295 return 0;
7296}
7297
e59058c4 7298/**
3621a710 7299 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
7300 * @phba: Pointer to HBA context object.
7301 * @pring: Pointer to driver SLI ring object.
7302 * @piocb: Pointer to address of newly added command iocb.
7303 *
7304 * This function is called with hbalock held to add a command
7305 * iocb to the txq when SLI layer cannot submit the command iocb
7306 * to the ring.
7307 **/
2a9bf3d0 7308void
92d7f7b0 7309__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7310 struct lpfc_iocbq *piocb)
dea3101e 7311{
7312 /* Insert the caller's iocb in the txq tail for later processing. */
7313 list_add_tail(&piocb->list, &pring->txq);
7314 pring->txq_cnt++;
dea3101e 7315}
7316
e59058c4 7317/**
3621a710 7318 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
7319 * @phba: Pointer to HBA context object.
7320 * @pring: Pointer to driver SLI ring object.
7321 * @piocb: Pointer to address of newly added command iocb.
7322 *
7323 * This function is called with hbalock held before a new
7324 * iocb is submitted to the firmware. This function checks
7325 * txq to flush the iocbs in txq to Firmware before
7326 * submitting new iocbs to the Firmware.
7327 * If there are iocbs in the txq which need to be submitted
7328 * to firmware, lpfc_sli_next_iocb returns the first element
7329 * of the txq after dequeuing it from txq.
7330 * If there is no iocb in the txq then the function will return
7331 * *piocb and *piocb is set to NULL. Caller needs to check
7332 * *piocb to find if there are more commands in the txq.
7333 **/
dea3101e 7334static struct lpfc_iocbq *
7335lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 7336 struct lpfc_iocbq **piocb)
dea3101e 7337{
7338 struct lpfc_iocbq * nextiocb;
7339
7340 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7341 if (!nextiocb) {
7342 nextiocb = *piocb;
7343 *piocb = NULL;
7344 }
7345
7346 return nextiocb;
7347}
7348
e59058c4 7349/**
3772a991 7350 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 7351 * @phba: Pointer to HBA context object.
3772a991 7352 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
7353 * @piocb: Pointer to command iocb.
7354 * @flag: Flag indicating if this command can be put into txq.
7355 *
3772a991
JS
7356 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7357 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7358 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7359 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7360 * this function allows only iocbs for posting buffers. This function finds
7361 * next available slot in the command ring and posts the command to the
7362 * available slot and writes the port attention register to request HBA start
7363 * processing new iocb. If there is no slot available in the ring and
7364 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7365 * the function returns IOCB_BUSY.
e59058c4 7366 *
3772a991
JS
7367 * This function is called with hbalock held. The function will return success
7368 * after it successfully submit the iocb to firmware or after adding to the
7369 * txq.
e59058c4 7370 **/
98c9ea5c 7371static int
3772a991 7372__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 7373 struct lpfc_iocbq *piocb, uint32_t flag)
7374{
7375 struct lpfc_iocbq *nextiocb;
7376 IOCB_t *iocb;
3772a991 7377 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 7378
92d7f7b0
JS
7379 if (piocb->iocb_cmpl && (!piocb->vport) &&
7380 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7381 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7382 lpfc_printf_log(phba, KERN_ERR,
7383 LOG_SLI | LOG_VPORT,
e8b62011 7384 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
7385 piocb->iocb.ulpCommand);
7386 dump_stack();
7387 return IOCB_ERROR;
7388 }
7389
7390
8d63f375
LV
7391 /* If the PCI channel is in offline state, do not post iocbs. */
7392 if (unlikely(pci_channel_offline(phba->pcidev)))
7393 return IOCB_ERROR;
7394
a257bf90
JS
7395 /* If HBA has a deferred error attention, fail the iocb. */
7396 if (unlikely(phba->hba_flag & DEFER_ERATT))
7397 return IOCB_ERROR;
7398
dea3101e 7399 /*
7400 * We should never get an IOCB if we are in a < LINK_DOWN state
7401 */
2e0fef85 7402 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 7403 return IOCB_ERROR;
7404
7405 /*
7406 * Check to see if we are blocking IOCB processing because of a
0b727fea 7407 * outstanding event.
dea3101e 7408 */
0b727fea 7409 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 7410 goto iocb_busy;
7411
2e0fef85 7412 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 7413 /*
2680eeaa 7414 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 7415 * can be issued if the link is not up.
7416 */
7417 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
7418 case CMD_GEN_REQUEST64_CR:
7419 case CMD_GEN_REQUEST64_CX:
7420 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7421 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 7422 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
7423 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7424 MENLO_TRANSPORT_TYPE))
7425
7426 goto iocb_busy;
7427 break;
dea3101e 7428 case CMD_QUE_RING_BUF_CN:
7429 case CMD_QUE_RING_BUF64_CN:
dea3101e 7430 /*
7431 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7432 * completion, iocb_cmpl MUST be 0.
7433 */
7434 if (piocb->iocb_cmpl)
7435 piocb->iocb_cmpl = NULL;
7436 /*FALLTHROUGH*/
7437 case CMD_CREATE_XRI_CR:
2680eeaa
JS
7438 case CMD_CLOSE_XRI_CN:
7439 case CMD_CLOSE_XRI_CX:
dea3101e 7440 break;
7441 default:
7442 goto iocb_busy;
7443 }
7444
7445 /*
7446 * For FCP commands, we must be in a state where we can process link
7447 * attention events.
7448 */
7449 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 7450 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 7451 goto iocb_busy;
92d7f7b0 7452 }
dea3101e 7453
dea3101e 7454 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7455 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7456 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7457
7458 if (iocb)
7459 lpfc_sli_update_ring(phba, pring);
7460 else
7461 lpfc_sli_update_full_ring(phba, pring);
7462
7463 if (!piocb)
7464 return IOCB_SUCCESS;
7465
7466 goto out_busy;
7467
7468 iocb_busy:
7469 pring->stats.iocb_cmd_delay++;
7470
7471 out_busy:
7472
7473 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 7474 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 7475 return IOCB_SUCCESS;
7476 }
7477
7478 return IOCB_BUSY;
7479}
7480
3772a991 7481/**
4f774513
JS
7482 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7483 * @phba: Pointer to HBA context object.
7484 * @piocb: Pointer to command iocb.
7485 * @sglq: Pointer to the scatter gather queue object.
7486 *
7487 * This routine converts the bpl or bde that is in the IOCB
7488 * to a sgl list for the sli4 hardware. The physical address
7489 * of the bpl/bde is converted back to a virtual address.
7490 * If the IOCB contains a BPL then the list of BDE's is
7491 * converted to sli4_sge's. If the IOCB contains a single
7492 * BDE then it is converted to a single sli_sge.
7493 * The IOCB is still in cpu endianess so the contents of
7494 * the bpl can be used without byte swapping.
7495 *
7496 * Returns valid XRI = Success, NO_XRI = Failure.
7497**/
7498static uint16_t
7499lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7500 struct lpfc_sglq *sglq)
3772a991 7501{
4f774513
JS
7502 uint16_t xritag = NO_XRI;
7503 struct ulp_bde64 *bpl = NULL;
7504 struct ulp_bde64 bde;
7505 struct sli4_sge *sgl = NULL;
7506 IOCB_t *icmd;
7507 int numBdes = 0;
7508 int i = 0;
63e801ce
JS
7509 uint32_t offset = 0; /* accumulated offset in the sg request list */
7510 int inbound = 0; /* number of sg reply entries inbound from firmware */
3772a991 7511
4f774513
JS
7512 if (!piocbq || !sglq)
7513 return xritag;
7514
7515 sgl = (struct sli4_sge *)sglq->sgl;
7516 icmd = &piocbq->iocb;
7517 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7518 numBdes = icmd->un.genreq64.bdl.bdeSize /
7519 sizeof(struct ulp_bde64);
7520 /* The addrHigh and addrLow fields within the IOCB
7521 * have not been byteswapped yet so there is no
7522 * need to swap them back.
7523 */
7524 bpl = (struct ulp_bde64 *)
7525 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
7526
7527 if (!bpl)
7528 return xritag;
7529
7530 for (i = 0; i < numBdes; i++) {
7531 /* Should already be byte swapped. */
28baac74
JS
7532 sgl->addr_hi = bpl->addrHigh;
7533 sgl->addr_lo = bpl->addrLow;
7534
0558056c 7535 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7536 if ((i+1) == numBdes)
7537 bf_set(lpfc_sli4_sge_last, sgl, 1);
7538 else
7539 bf_set(lpfc_sli4_sge_last, sgl, 0);
28baac74
JS
7540 /* swap the size field back to the cpu so we
7541 * can assign it to the sgl.
7542 */
7543 bde.tus.w = le32_to_cpu(bpl->tus.w);
7544 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
63e801ce
JS
7545 /* The offsets in the sgl need to be accumulated
7546 * separately for the request and reply lists.
7547 * The request is always first, the reply follows.
7548 */
7549 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7550 /* add up the reply sg entries */
7551 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7552 inbound++;
7553 /* first inbound? reset the offset */
7554 if (inbound == 1)
7555 offset = 0;
7556 bf_set(lpfc_sli4_sge_offset, sgl, offset);
f9bb2da1
JS
7557 bf_set(lpfc_sli4_sge_type, sgl,
7558 LPFC_SGE_TYPE_DATA);
63e801ce
JS
7559 offset += bde.tus.f.bdeSize;
7560 }
546fc854 7561 sgl->word2 = cpu_to_le32(sgl->word2);
4f774513
JS
7562 bpl++;
7563 sgl++;
7564 }
7565 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7566 /* The addrHigh and addrLow fields of the BDE have not
7567 * been byteswapped yet so they need to be swapped
7568 * before putting them in the sgl.
7569 */
7570 sgl->addr_hi =
7571 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7572 sgl->addr_lo =
7573 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
0558056c 7574 sgl->word2 = le32_to_cpu(sgl->word2);
4f774513
JS
7575 bf_set(lpfc_sli4_sge_last, sgl, 1);
7576 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
7577 sgl->sge_len =
7578 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
7579 }
7580 return sglq->sli4_xritag;
3772a991 7581}
92d7f7b0 7582
e59058c4 7583/**
4f774513 7584 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 7585 * @phba: Pointer to HBA context object.
e59058c4 7586 *
a93ff37a 7587 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
8fa38513
JS
7588 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7589 * held.
4f774513
JS
7590 *
7591 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 7592 **/
4f774513 7593static uint32_t
8fa38513 7594lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 7595{
8fa38513
JS
7596 ++phba->fcp_qidx;
7597 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
7598 phba->fcp_qidx = 0;
92d7f7b0 7599
8fa38513 7600 return phba->fcp_qidx;
92d7f7b0
JS
7601}
7602
e59058c4 7603/**
4f774513 7604 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 7605 * @phba: Pointer to HBA context object.
4f774513
JS
7606 * @piocb: Pointer to command iocb.
7607 * @wqe: Pointer to the work queue entry.
e59058c4 7608 *
4f774513
JS
7609 * This routine converts the iocb command to its Work Queue Entry
7610 * equivalent. The wqe pointer should not have any fields set when
7611 * this routine is called because it will memcpy over them.
7612 * This routine does not set the CQ_ID or the WQEC bits in the
7613 * wqe.
e59058c4 7614 *
4f774513 7615 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 7616 **/
cf5bf97e 7617static int
4f774513
JS
7618lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7619 union lpfc_wqe *wqe)
cf5bf97e 7620{
5ffc266e 7621 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
7622 uint8_t ct = 0;
7623 uint32_t fip;
7624 uint32_t abort_tag;
7625 uint8_t command_type = ELS_COMMAND_NON_FIP;
7626 uint8_t cmnd;
7627 uint16_t xritag;
dcf2a4e0
JS
7628 uint16_t abrt_iotag;
7629 struct lpfc_iocbq *abrtiocbq;
4f774513 7630 struct ulp_bde64 *bpl = NULL;
f0d9bccc 7631 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
5ffc266e
JS
7632 int numBdes, i;
7633 struct ulp_bde64 bde;
c31098ce 7634 struct lpfc_nodelist *ndlp;
4f774513 7635
45ed1190 7636 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 7637 /* The fcp commands will set command type */
0c287589 7638 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 7639 command_type = FCP_COMMAND;
c868595d 7640 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
7641 command_type = ELS_COMMAND_FIP;
7642 else
7643 command_type = ELS_COMMAND_NON_FIP;
7644
4f774513
JS
7645 /* Some of the fields are in the right position already */
7646 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7647 abort_tag = (uint32_t) iocbq->iotag;
7648 xritag = iocbq->sli4_xritag;
f0d9bccc 7649 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
4f774513
JS
7650 /* words0-2 bpl convert bde */
7651 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
7652 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7653 sizeof(struct ulp_bde64);
4f774513
JS
7654 bpl = (struct ulp_bde64 *)
7655 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7656 if (!bpl)
7657 return IOCB_ERROR;
cf5bf97e 7658
4f774513
JS
7659 /* Should already be byte swapped. */
7660 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7661 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7662 /* swap the size field back to the cpu so we
7663 * can assign it to the sgl.
7664 */
7665 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
7666 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7667 total_len = 0;
7668 for (i = 0; i < numBdes; i++) {
7669 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7670 total_len += bde.tus.f.bdeSize;
7671 }
4f774513 7672 } else
5ffc266e 7673 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 7674
4f774513
JS
7675 iocbq->iocb.ulpIoTag = iocbq->iotag;
7676 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 7677
4f774513
JS
7678 switch (iocbq->iocb.ulpCommand) {
7679 case CMD_ELS_REQUEST64_CR:
c31098ce 7680 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513
JS
7681 if (!iocbq->iocb.ulpLe) {
7682 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7683 "2007 Only Limited Edition cmd Format"
7684 " supported 0x%x\n",
7685 iocbq->iocb.ulpCommand);
7686 return IOCB_ERROR;
7687 }
5ffc266e 7688 wqe->els_req.payload_len = xmit_len;
4f774513
JS
7689 /* Els_reguest64 has a TMO */
7690 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
7691 iocbq->iocb.ulpTimeout);
7692 /* Need a VF for word 4 set the vf bit*/
7693 bf_set(els_req64_vf, &wqe->els_req, 0);
7694 /* And a VFID for word 12 */
7695 bf_set(els_req64_vfid, &wqe->els_req, 0);
4f774513 7696 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
f0d9bccc
JS
7697 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
7698 iocbq->iocb.ulpContext);
7699 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
7700 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
4f774513 7701 /* CCP CCPE PV PRI in word10 were set in the memcpy */
c868595d
JS
7702 if (command_type == ELS_COMMAND_FIP) {
7703 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
7704 >> LPFC_FIP_ELS_ID_SHIFT);
7705 }
6d368e53
JS
7706 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
7707 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
f0d9bccc
JS
7708 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
7709 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
7710 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
7711 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
7712 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7713 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
7851fe2c 7714 break;
5ffc266e 7715 case CMD_XMIT_SEQUENCE64_CX:
f0d9bccc
JS
7716 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
7717 iocbq->iocb.un.ulpWord[3]);
7718 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
7851fe2c 7719 iocbq->iocb.unsli3.rcvsli3.ox_id);
5ffc266e
JS
7720 /* The entire sequence is transmitted for this IOCB */
7721 xmit_len = total_len;
7722 cmnd = CMD_XMIT_SEQUENCE64_CR;
4f774513 7723 case CMD_XMIT_SEQUENCE64_CR:
f0d9bccc
JS
7724 /* word3 iocb=io_tag32 wqe=reserved */
7725 wqe->xmit_sequence.rsvd3 = 0;
4f774513
JS
7726 /* word4 relative_offset memcpy */
7727 /* word5 r_ctl/df_ctl memcpy */
f0d9bccc
JS
7728 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
7729 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
7730 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
7731 LPFC_WQE_IOD_WRITE);
7732 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
7733 LPFC_WQE_LENLOC_WORD12);
7734 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
5ffc266e
JS
7735 wqe->xmit_sequence.xmit_len = xmit_len;
7736 command_type = OTHER_COMMAND;
7851fe2c 7737 break;
4f774513 7738 case CMD_XMIT_BCAST64_CN:
f0d9bccc
JS
7739 /* word3 iocb=iotag32 wqe=seq_payload_len */
7740 wqe->xmit_bcast64.seq_payload_len = xmit_len;
4f774513
JS
7741 /* word4 iocb=rsvd wqe=rsvd */
7742 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
7743 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
f0d9bccc 7744 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
4f774513 7745 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
f0d9bccc
JS
7746 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
7747 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
7748 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
7749 LPFC_WQE_LENLOC_WORD3);
7750 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
7851fe2c 7751 break;
4f774513
JS
7752 case CMD_FCP_IWRITE64_CR:
7753 command_type = FCP_COMMAND_DATA_OUT;
f0d9bccc
JS
7754 /* word3 iocb=iotag wqe=payload_offset_len */
7755 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7756 wqe->fcp_iwrite.payload_offset_len =
7757 xmit_len + sizeof(struct fcp_rsp);
7758 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7759 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7760 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
7761 iocbq->iocb.ulpFCP2Rcvy);
7762 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
7763 /* Always open the exchange */
7764 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
7765 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
7766 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
7767 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
7768 LPFC_WQE_LENLOC_WORD4);
7769 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
7770 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
7851fe2c 7771 break;
4f774513 7772 case CMD_FCP_IREAD64_CR:
f0d9bccc
JS
7773 /* word3 iocb=iotag wqe=payload_offset_len */
7774 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
7775 wqe->fcp_iread.payload_offset_len =
5ffc266e 7776 xmit_len + sizeof(struct fcp_rsp);
f0d9bccc
JS
7777 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
7778 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
7779 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
7780 iocbq->iocb.ulpFCP2Rcvy);
7781 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
f1126688
JS
7782 /* Always open the exchange */
7783 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
f0d9bccc
JS
7784 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
7785 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
7786 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
7787 LPFC_WQE_LENLOC_WORD4);
7788 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
7789 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
7851fe2c 7790 break;
4f774513 7791 case CMD_FCP_ICMND64_CR:
f0d9bccc
JS
7792 /* word3 iocb=IO_TAG wqe=reserved */
7793 wqe->fcp_icmd.rsrvd3 = 0;
7794 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
4f774513 7795 /* Always open the exchange */
f0d9bccc
JS
7796 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
7797 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
7798 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
7799 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
7800 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
7801 LPFC_WQE_LENLOC_NONE);
7802 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
7851fe2c 7803 break;
4f774513 7804 case CMD_GEN_REQUEST64_CR:
63e801ce
JS
7805 /* For this command calculate the xmit length of the
7806 * request bde.
7807 */
7808 xmit_len = 0;
7809 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7810 sizeof(struct ulp_bde64);
7811 for (i = 0; i < numBdes; i++) {
63e801ce 7812 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
546fc854
JS
7813 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
7814 break;
63e801ce
JS
7815 xmit_len += bde.tus.f.bdeSize;
7816 }
f0d9bccc
JS
7817 /* word3 iocb=IO_TAG wqe=request_payload_len */
7818 wqe->gen_req.request_payload_len = xmit_len;
7819 /* word4 iocb=parameter wqe=relative_offset memcpy */
7820 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
4f774513
JS
7821 /* word6 context tag copied in memcpy */
7822 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
7823 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
7824 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7825 "2015 Invalid CT %x command 0x%x\n",
7826 ct, iocbq->iocb.ulpCommand);
7827 return IOCB_ERROR;
7828 }
f0d9bccc
JS
7829 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
7830 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
7831 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
7832 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
7833 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
7834 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
7835 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
7836 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
4f774513 7837 command_type = OTHER_COMMAND;
7851fe2c 7838 break;
4f774513 7839 case CMD_XMIT_ELS_RSP64_CX:
c31098ce 7840 ndlp = (struct lpfc_nodelist *)iocbq->context1;
4f774513 7841 /* words0-2 BDE memcpy */
f0d9bccc
JS
7842 /* word3 iocb=iotag32 wqe=response_payload_len */
7843 wqe->xmit_els_rsp.response_payload_len = xmit_len;
4f774513 7844 /* word4 iocb=did wge=rsvd. */
f0d9bccc 7845 wqe->xmit_els_rsp.rsvd4 = 0;
4f774513
JS
7846 /* word5 iocb=rsvd wge=did */
7847 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
7848 iocbq->iocb.un.elsreq64.remoteID);
f0d9bccc
JS
7849 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
7850 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7851 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
7852 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
7851fe2c 7853 iocbq->iocb.unsli3.rcvsli3.ox_id);
4f774513 7854 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
f0d9bccc 7855 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
6d368e53 7856 phba->vpi_ids[iocbq->vport->vpi]);
f0d9bccc
JS
7857 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
7858 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
7859 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
7860 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
7861 LPFC_WQE_LENLOC_WORD3);
7862 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
6d368e53
JS
7863 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
7864 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
4f774513 7865 command_type = OTHER_COMMAND;
7851fe2c 7866 break;
4f774513
JS
7867 case CMD_CLOSE_XRI_CN:
7868 case CMD_ABORT_XRI_CN:
7869 case CMD_ABORT_XRI_CX:
7870 /* words 0-2 memcpy should be 0 rserved */
7871 /* port will send abts */
dcf2a4e0
JS
7872 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
7873 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
7874 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
7875 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
7876 } else
7877 fip = 0;
7878
7879 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
4f774513 7880 /*
dcf2a4e0
JS
7881 * The link is down, or the command was ELS_FIP
7882 * so the fw does not need to send abts
4f774513
JS
7883 * on the wire.
7884 */
7885 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
7886 else
7887 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
7888 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
f0d9bccc
JS
7889 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
7890 wqe->abort_cmd.rsrvd5 = 0;
7891 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
4f774513
JS
7892 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
7893 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
7894 /*
7895 * The abort handler will send us CMD_ABORT_XRI_CN or
7896 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
7897 */
f0d9bccc
JS
7898 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
7899 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
7900 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
7901 LPFC_WQE_LENLOC_NONE);
4f774513
JS
7902 cmnd = CMD_ABORT_XRI_CX;
7903 command_type = OTHER_COMMAND;
7904 xritag = 0;
7851fe2c 7905 break;
6669f9bb 7906 case CMD_XMIT_BLS_RSP64_CX:
546fc854 7907 /* As BLS ABTS RSP WQE is very different from other WQEs,
6669f9bb
JS
7908 * we re-construct this WQE here based on information in
7909 * iocbq from scratch.
7910 */
7911 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 7912 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 7913 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
546fc854
JS
7914 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
7915 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
5ffc266e
JS
7916 LPFC_ABTS_UNSOL_INT) {
7917 /* ABTS sent by initiator to CT exchange, the
7918 * RX_ID field will be filled with the newly
7919 * allocated responder XRI.
7920 */
7921 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
7922 iocbq->sli4_xritag);
7923 } else {
7924 /* ABTS sent by responder to CT exchange, the
7925 * RX_ID field will be filled with the responder
7926 * RX_ID from ABTS.
7927 */
7928 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
546fc854 7929 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
5ffc266e 7930 }
6669f9bb
JS
7931 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
7932 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
7933 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
7934 iocbq->iocb.ulpContext);
f0d9bccc
JS
7935 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
7936 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
7937 LPFC_WQE_LENLOC_NONE);
6669f9bb
JS
7938 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
7939 command_type = OTHER_COMMAND;
546fc854
JS
7940 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
7941 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
7942 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
7943 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
7944 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
7945 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
7946 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
7947 }
7948
7851fe2c 7949 break;
4f774513
JS
7950 case CMD_XRI_ABORTED_CX:
7951 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
4f774513
JS
7952 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
7953 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
7954 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
7955 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
7956 default:
7957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
7958 "2014 Invalid command 0x%x\n",
7959 iocbq->iocb.ulpCommand);
7960 return IOCB_ERROR;
7851fe2c 7961 break;
4f774513 7962 }
6d368e53 7963
f0d9bccc
JS
7964 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
7965 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
7966 wqe->generic.wqe_com.abort_tag = abort_tag;
7967 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
7968 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
7969 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
7970 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
4f774513
JS
7971 return 0;
7972}
7973
7974/**
7975 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
7976 * @phba: Pointer to HBA context object.
7977 * @ring_number: SLI ring number to issue iocb on.
7978 * @piocb: Pointer to command iocb.
7979 * @flag: Flag indicating if this command can be put into txq.
7980 *
7981 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
7982 * an iocb command to an HBA with SLI-4 interface spec.
7983 *
7984 * This function is called with hbalock held. The function will return success
7985 * after it successfully submit the iocb to firmware or after adding to the
7986 * txq.
7987 **/
7988static int
7989__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
7990 struct lpfc_iocbq *piocb, uint32_t flag)
7991{
7992 struct lpfc_sglq *sglq;
4f774513
JS
7993 union lpfc_wqe wqe;
7994 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
7995
7996 if (piocb->sli4_xritag == NO_XRI) {
7997 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
546fc854
JS
7998 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN ||
7999 piocb->iocb.ulpCommand == CMD_XMIT_BLS_RSP64_CX)
4f774513
JS
8000 sglq = NULL;
8001 else {
2a9bf3d0
JS
8002 if (pring->txq_cnt) {
8003 if (!(flag & SLI_IOCB_RET_IOCB)) {
8004 __lpfc_sli_ringtx_put(phba,
8005 pring, piocb);
8006 return IOCB_SUCCESS;
8007 } else {
8008 return IOCB_BUSY;
8009 }
8010 } else {
6d368e53 8011 sglq = __lpfc_sli_get_sglq(phba, piocb);
2a9bf3d0
JS
8012 if (!sglq) {
8013 if (!(flag & SLI_IOCB_RET_IOCB)) {
8014 __lpfc_sli_ringtx_put(phba,
8015 pring,
8016 piocb);
8017 return IOCB_SUCCESS;
8018 } else
8019 return IOCB_BUSY;
8020 }
8021 }
4f774513
JS
8022 }
8023 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6d368e53
JS
8024 /* These IO's already have an XRI and a mapped sgl. */
8025 sglq = NULL;
4f774513 8026 } else {
6d368e53
JS
8027 /*
8028 * This is a continuation of a commandi,(CX) so this
4f774513
JS
8029 * sglq is on the active list
8030 */
8031 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
8032 if (!sglq)
8033 return IOCB_ERROR;
8034 }
8035
8036 if (sglq) {
6d368e53 8037 piocb->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0 8038 piocb->sli4_xritag = sglq->sli4_xritag;
2a9bf3d0 8039 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
4f774513
JS
8040 return IOCB_ERROR;
8041 }
8042
8043 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8044 return IOCB_ERROR;
8045
341af102
JS
8046 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8047 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
8048 /*
8049 * For FCP command IOCB, get a new WQ index to distribute
8050 * WQE across the WQsr. On the other hand, for abort IOCB,
8051 * it carries the same WQ index to the original command
8052 * IOCB.
8053 */
341af102 8054 if (piocb->iocb_flag & LPFC_IO_FCP)
5ffc266e
JS
8055 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8056 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8057 &wqe))
4f774513
JS
8058 return IOCB_ERROR;
8059 } else {
8060 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8061 return IOCB_ERROR;
8062 }
8063 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8064
8065 return 0;
8066}
8067
8068/**
8069 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8070 *
8071 * This routine wraps the actual lockless version for issusing IOCB function
8072 * pointer from the lpfc_hba struct.
8073 *
8074 * Return codes:
8075 * IOCB_ERROR - Error
8076 * IOCB_SUCCESS - Success
8077 * IOCB_BUSY - Busy
8078 **/
2a9bf3d0 8079int
4f774513
JS
8080__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8081 struct lpfc_iocbq *piocb, uint32_t flag)
8082{
8083 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8084}
8085
8086/**
25985edc 8087 * lpfc_sli_api_table_setup - Set up sli api function jump table
4f774513
JS
8088 * @phba: The hba struct for which this call is being executed.
8089 * @dev_grp: The HBA PCI-Device group number.
8090 *
8091 * This routine sets up the SLI interface API function jump table in @phba
8092 * struct.
8093 * Returns: 0 - success, -ENODEV - failure.
8094 **/
8095int
8096lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8097{
8098
8099 switch (dev_grp) {
8100 case LPFC_PCI_DEV_LP:
8101 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8102 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8103 break;
8104 case LPFC_PCI_DEV_OC:
8105 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8106 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8107 break;
8108 default:
8109 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8110 "1419 Invalid HBA PCI-device group: 0x%x\n",
8111 dev_grp);
8112 return -ENODEV;
8113 break;
8114 }
8115 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8116 return 0;
8117}
8118
8119/**
8120 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8121 * @phba: Pointer to HBA context object.
8122 * @pring: Pointer to driver SLI ring object.
8123 * @piocb: Pointer to command iocb.
8124 * @flag: Flag indicating if this command can be put into txq.
8125 *
8126 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8127 * function. This function gets the hbalock and calls
8128 * __lpfc_sli_issue_iocb function and will return the error returned
8129 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8130 * functions which do not hold hbalock.
8131 **/
8132int
8133lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8134 struct lpfc_iocbq *piocb, uint32_t flag)
8135{
8136 unsigned long iflags;
8137 int rc;
8138
8139 spin_lock_irqsave(&phba->hbalock, iflags);
8140 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8141 spin_unlock_irqrestore(&phba->hbalock, iflags);
8142
8143 return rc;
8144}
8145
8146/**
8147 * lpfc_extra_ring_setup - Extra ring setup function
8148 * @phba: Pointer to HBA context object.
8149 *
8150 * This function is called while driver attaches with the
8151 * HBA to setup the extra ring. The extra ring is used
8152 * only when driver needs to support target mode functionality
8153 * or IP over FC functionalities.
8154 *
8155 * This function is called with no lock held.
8156 **/
8157static int
8158lpfc_extra_ring_setup( struct lpfc_hba *phba)
8159{
8160 struct lpfc_sli *psli;
8161 struct lpfc_sli_ring *pring;
8162
8163 psli = &phba->sli;
8164
8165 /* Adjust cmd/rsp ring iocb entries more evenly */
8166
8167 /* Take some away from the FCP ring */
8168 pring = &psli->ring[psli->fcp_ring];
8169 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8170 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
8171 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8172 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8173
a4bc3379
JS
8174 /* and give them to the extra ring */
8175 pring = &psli->ring[psli->extra_ring];
8176
cf5bf97e
JW
8177 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8178 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8179 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8180 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8181
8182 /* Setup default profile for this ring */
8183 pring->iotag_max = 4096;
8184 pring->num_mask = 1;
8185 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
8186 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8187 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
8188 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8189 return 0;
8190}
8191
e59058c4 8192/**
3621a710 8193 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
8194 * @phba: Pointer to HBA context object.
8195 * @pring: Pointer to driver SLI ring object.
8196 * @iocbq: Pointer to iocb object.
8197 *
8198 * This function is called by the slow ring event handler
8199 * function when there is an ASYNC event iocb in the ring.
8200 * This function is called with no lock held.
8201 * Currently this function handles only temperature related
8202 * ASYNC events. The function decodes the temperature sensor
8203 * event message and posts events for the management applications.
8204 **/
98c9ea5c 8205static void
57127f15
JS
8206lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8207 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8208{
8209 IOCB_t *icmd;
8210 uint16_t evt_code;
8211 uint16_t temp;
8212 struct temp_event temp_event_data;
8213 struct Scsi_Host *shost;
a257bf90 8214 uint32_t *iocb_w;
57127f15
JS
8215
8216 icmd = &iocbq->iocb;
8217 evt_code = icmd->un.asyncstat.evt_code;
8218 temp = icmd->ulpContext;
8219
8220 if ((evt_code != ASYNC_TEMP_WARN) &&
8221 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 8222 iocb_w = (uint32_t *) icmd;
57127f15
JS
8223 lpfc_printf_log(phba,
8224 KERN_ERR,
8225 LOG_SLI,
76bb24ef 8226 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 8227 " evt_code 0x%x\n"
a257bf90
JS
8228 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8229 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8230 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8231 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 8232 pring->ringno,
a257bf90
JS
8233 icmd->un.asyncstat.evt_code,
8234 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8235 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8236 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8237 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8238
57127f15
JS
8239 return;
8240 }
8241 temp_event_data.data = (uint32_t)temp;
8242 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8243 if (evt_code == ASYNC_TEMP_WARN) {
8244 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8245 lpfc_printf_log(phba,
09372820 8246 KERN_ERR,
57127f15 8247 LOG_TEMP,
76bb24ef 8248 "0347 Adapter is very hot, please take "
57127f15
JS
8249 "corrective action. temperature : %d Celsius\n",
8250 temp);
8251 }
8252 if (evt_code == ASYNC_TEMP_SAFE) {
8253 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8254 lpfc_printf_log(phba,
09372820 8255 KERN_ERR,
57127f15
JS
8256 LOG_TEMP,
8257 "0340 Adapter temperature is OK now. "
8258 "temperature : %d Celsius\n",
8259 temp);
8260 }
8261
8262 /* Send temperature change event to applications */
8263 shost = lpfc_shost_from_vport(phba->pport);
8264 fc_host_post_vendor_event(shost, fc_get_event_number(),
8265 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 8266 LPFC_NL_VENDOR_ID);
57127f15
JS
8267
8268}
8269
8270
e59058c4 8271/**
3621a710 8272 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
8273 * @phba: Pointer to HBA context object.
8274 *
8275 * lpfc_sli_setup sets up rings of the SLI interface with
8276 * number of iocbs per ring and iotags. This function is
8277 * called while driver attach to the HBA and before the
8278 * interrupts are enabled. So there is no need for locking.
8279 *
8280 * This function always returns 0.
8281 **/
dea3101e 8282int
8283lpfc_sli_setup(struct lpfc_hba *phba)
8284{
ed957684 8285 int i, totiocbsize = 0;
dea3101e 8286 struct lpfc_sli *psli = &phba->sli;
8287 struct lpfc_sli_ring *pring;
8288
8289 psli->num_rings = MAX_CONFIGURED_RINGS;
8290 psli->sli_flag = 0;
8291 psli->fcp_ring = LPFC_FCP_RING;
8292 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 8293 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 8294
604a3e30
JB
8295 psli->iocbq_lookup = NULL;
8296 psli->iocbq_lookup_len = 0;
8297 psli->last_iotag = 0;
8298
dea3101e 8299 for (i = 0; i < psli->num_rings; i++) {
8300 pring = &psli->ring[i];
8301 switch (i) {
8302 case LPFC_FCP_RING: /* ring 0 - FCP */
8303 /* numCiocb and numRiocb are used in config_port */
8304 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8305 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8306 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8307 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8308 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8309 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 8310 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8311 SLI3_IOCB_CMD_SIZE :
8312 SLI2_IOCB_CMD_SIZE;
ed957684 8313 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8314 SLI3_IOCB_RSP_SIZE :
8315 SLI2_IOCB_RSP_SIZE;
dea3101e 8316 pring->iotag_ctr = 0;
8317 pring->iotag_max =
92d7f7b0 8318 (phba->cfg_hba_queue_depth * 2);
dea3101e 8319 pring->fast_iotag = pring->iotag_max;
8320 pring->num_mask = 0;
8321 break;
a4bc3379 8322 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 8323 /* numCiocb and numRiocb are used in config_port */
8324 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8325 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 8326 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8327 SLI3_IOCB_CMD_SIZE :
8328 SLI2_IOCB_CMD_SIZE;
ed957684 8329 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8330 SLI3_IOCB_RSP_SIZE :
8331 SLI2_IOCB_RSP_SIZE;
2e0fef85 8332 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 8333 pring->num_mask = 0;
8334 break;
8335 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8336 /* numCiocb and numRiocb are used in config_port */
8337 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8338 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 8339 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8340 SLI3_IOCB_CMD_SIZE :
8341 SLI2_IOCB_CMD_SIZE;
ed957684 8342 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
8343 SLI3_IOCB_RSP_SIZE :
8344 SLI2_IOCB_RSP_SIZE;
dea3101e 8345 pring->fast_iotag = 0;
8346 pring->iotag_ctr = 0;
8347 pring->iotag_max = 4096;
57127f15
JS
8348 pring->lpfc_sli_rcv_async_status =
8349 lpfc_sli_async_event_handler;
6669f9bb 8350 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 8351 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
8352 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8353 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 8354 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 8355 lpfc_els_unsol_event;
dea3101e 8356 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
8357 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8358 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 8359 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 8360 lpfc_els_unsol_event;
dea3101e 8361 pring->prt[2].profile = 0; /* Mask 2 */
8362 /* NameServer Inquiry */
6a9c52cf 8363 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 8364 /* NameServer */
6a9c52cf 8365 pring->prt[2].type = FC_TYPE_CT;
dea3101e 8366 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 8367 lpfc_ct_unsol_event;
dea3101e 8368 pring->prt[3].profile = 0; /* Mask 3 */
8369 /* NameServer response */
6a9c52cf 8370 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 8371 /* NameServer */
6a9c52cf 8372 pring->prt[3].type = FC_TYPE_CT;
dea3101e 8373 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 8374 lpfc_ct_unsol_event;
6669f9bb
JS
8375 /* abort unsolicited sequence */
8376 pring->prt[4].profile = 0; /* Mask 4 */
8377 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
8378 pring->prt[4].type = FC_TYPE_BLS;
8379 pring->prt[4].lpfc_sli_rcv_unsol_event =
8380 lpfc_sli4_ct_abort_unsol_event;
dea3101e 8381 break;
8382 }
ed957684 8383 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 8384 (pring->numRiocb * pring->sizeRiocb);
dea3101e 8385 }
ed957684 8386 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 8387 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
8388 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8389 "SLI2 SLIM Data: x%x x%lx\n",
8390 phba->brd_no, totiocbsize,
8391 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 8392 }
cf5bf97e
JW
8393 if (phba->cfg_multi_ring_support == 2)
8394 lpfc_extra_ring_setup(phba);
dea3101e 8395
8396 return 0;
8397}
8398
e59058c4 8399/**
3621a710 8400 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
8401 * @phba: Pointer to HBA context object.
8402 *
8403 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8404 * ring. This function also initializes ring indices of each ring.
8405 * This function is called during the initialization of the SLI
8406 * interface of an HBA.
8407 * This function is called with no lock held and always returns
8408 * 1.
8409 **/
dea3101e 8410int
2e0fef85 8411lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e 8412{
8413 struct lpfc_sli *psli;
8414 struct lpfc_sli_ring *pring;
604a3e30 8415 int i;
dea3101e 8416
8417 psli = &phba->sli;
2e0fef85 8418 spin_lock_irq(&phba->hbalock);
dea3101e 8419 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 8420 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 8421 /* Initialize list headers for txq and txcmplq as double linked lists */
8422 for (i = 0; i < psli->num_rings; i++) {
8423 pring = &psli->ring[i];
8424 pring->ringno = i;
8425 pring->next_cmdidx = 0;
8426 pring->local_getidx = 0;
8427 pring->cmdidx = 0;
8428 INIT_LIST_HEAD(&pring->txq);
8429 INIT_LIST_HEAD(&pring->txcmplq);
8430 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 8431 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 8432 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 8433 }
2e0fef85
JS
8434 spin_unlock_irq(&phba->hbalock);
8435 return 1;
dea3101e 8436}
8437
04c68496
JS
8438/**
8439 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8440 * @phba: Pointer to HBA context object.
8441 *
8442 * This routine flushes the mailbox command subsystem. It will unconditionally
8443 * flush all the mailbox commands in the three possible stages in the mailbox
8444 * command sub-system: pending mailbox command queue; the outstanding mailbox
8445 * command; and completed mailbox command queue. It is caller's responsibility
8446 * to make sure that the driver is in the proper state to flush the mailbox
8447 * command sub-system. Namely, the posting of mailbox commands into the
8448 * pending mailbox command queue from the various clients must be stopped;
8449 * either the HBA is in a state that it will never works on the outstanding
8450 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8451 * mailbox command has been completed.
8452 **/
8453static void
8454lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8455{
8456 LIST_HEAD(completions);
8457 struct lpfc_sli *psli = &phba->sli;
8458 LPFC_MBOXQ_t *pmb;
8459 unsigned long iflag;
8460
8461 /* Flush all the mailbox commands in the mbox system */
8462 spin_lock_irqsave(&phba->hbalock, iflag);
8463 /* The pending mailbox command queue */
8464 list_splice_init(&phba->sli.mboxq, &completions);
8465 /* The outstanding active mailbox command */
8466 if (psli->mbox_active) {
8467 list_add_tail(&psli->mbox_active->list, &completions);
8468 psli->mbox_active = NULL;
8469 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8470 }
8471 /* The completed mailbox command queue */
8472 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
8473 spin_unlock_irqrestore(&phba->hbalock, iflag);
8474
8475 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
8476 while (!list_empty(&completions)) {
8477 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
8478 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
8479 if (pmb->mbox_cmpl)
8480 pmb->mbox_cmpl(phba, pmb);
8481 }
8482}
8483
e59058c4 8484/**
3621a710 8485 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
8486 * @vport: Pointer to virtual port object.
8487 *
8488 * lpfc_sli_host_down is called to clean up the resources
8489 * associated with a vport before destroying virtual
8490 * port data structures.
8491 * This function does following operations:
8492 * - Free discovery resources associated with this virtual
8493 * port.
8494 * - Free iocbs associated with this virtual port in
8495 * the txq.
8496 * - Send abort for all iocb commands associated with this
8497 * vport in txcmplq.
8498 *
8499 * This function is called with no lock held and always returns 1.
8500 **/
92d7f7b0
JS
8501int
8502lpfc_sli_host_down(struct lpfc_vport *vport)
8503{
858c9f6c 8504 LIST_HEAD(completions);
92d7f7b0
JS
8505 struct lpfc_hba *phba = vport->phba;
8506 struct lpfc_sli *psli = &phba->sli;
8507 struct lpfc_sli_ring *pring;
8508 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
8509 int i;
8510 unsigned long flags = 0;
8511 uint16_t prev_pring_flag;
8512
8513 lpfc_cleanup_discovery_resources(vport);
8514
8515 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
8516 for (i = 0; i < psli->num_rings; i++) {
8517 pring = &psli->ring[i];
8518 prev_pring_flag = pring->flag;
5e9d9b82
JS
8519 /* Only slow rings */
8520 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 8521 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
8522 /* Set the lpfc data pending flag */
8523 set_bit(LPFC_DATA_READY, &phba->data_flags);
8524 }
92d7f7b0
JS
8525 /*
8526 * Error everything on the txq since these iocbs have not been
8527 * given to the FW yet.
8528 */
92d7f7b0
JS
8529 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
8530 if (iocb->vport != vport)
8531 continue;
858c9f6c 8532 list_move_tail(&iocb->list, &completions);
92d7f7b0 8533 pring->txq_cnt--;
92d7f7b0
JS
8534 }
8535
8536 /* Next issue ABTS for everything on the txcmplq */
8537 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
8538 list) {
8539 if (iocb->vport != vport)
8540 continue;
8541 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
8542 }
8543
8544 pring->flag = prev_pring_flag;
8545 }
8546
8547 spin_unlock_irqrestore(&phba->hbalock, flags);
8548
a257bf90
JS
8549 /* Cancel all the IOCBs from the completions list */
8550 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8551 IOERR_SLI_DOWN);
92d7f7b0
JS
8552 return 1;
8553}
8554
e59058c4 8555/**
3621a710 8556 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
8557 * @phba: Pointer to HBA context object.
8558 *
8559 * This function cleans up all iocb, buffers, mailbox commands
8560 * while shutting down the HBA. This function is called with no
8561 * lock held and always returns 1.
8562 * This function does the following to cleanup driver resources:
8563 * - Free discovery resources for each virtual port
8564 * - Cleanup any pending fabric iocbs
8565 * - Iterate through the iocb txq and free each entry
8566 * in the list.
8567 * - Free up any buffer posted to the HBA
8568 * - Free mailbox commands in the mailbox queue.
8569 **/
dea3101e 8570int
2e0fef85 8571lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 8572{
2534ba75 8573 LIST_HEAD(completions);
2e0fef85 8574 struct lpfc_sli *psli = &phba->sli;
dea3101e 8575 struct lpfc_sli_ring *pring;
0ff10d46 8576 struct lpfc_dmabuf *buf_ptr;
dea3101e 8577 unsigned long flags = 0;
04c68496
JS
8578 int i;
8579
8580 /* Shutdown the mailbox command sub-system */
8581 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 8582
dea3101e 8583 lpfc_hba_down_prep(phba);
8584
92d7f7b0
JS
8585 lpfc_fabric_abort_hba(phba);
8586
2e0fef85 8587 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 8588 for (i = 0; i < psli->num_rings; i++) {
8589 pring = &psli->ring[i];
5e9d9b82
JS
8590 /* Only slow rings */
8591 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 8592 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
8593 /* Set the lpfc data pending flag */
8594 set_bit(LPFC_DATA_READY, &phba->data_flags);
8595 }
dea3101e 8596
8597 /*
8598 * Error everything on the txq since these iocbs have not been
8599 * given to the FW yet.
8600 */
2534ba75 8601 list_splice_init(&pring->txq, &completions);
dea3101e 8602 pring->txq_cnt = 0;
8603
2534ba75 8604 }
2e0fef85 8605 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 8606
a257bf90
JS
8607 /* Cancel all the IOCBs from the completions list */
8608 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
8609 IOERR_SLI_DOWN);
dea3101e 8610
0ff10d46
JS
8611 spin_lock_irqsave(&phba->hbalock, flags);
8612 list_splice_init(&phba->elsbuf, &completions);
8613 phba->elsbuf_cnt = 0;
8614 phba->elsbuf_prev_cnt = 0;
8615 spin_unlock_irqrestore(&phba->hbalock, flags);
8616
8617 while (!list_empty(&completions)) {
8618 list_remove_head(&completions, buf_ptr,
8619 struct lpfc_dmabuf, list);
8620 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
8621 kfree(buf_ptr);
8622 }
8623
dea3101e 8624 /* Return any active mbox cmds */
8625 del_timer_sync(&psli->mbox_tmo);
2e0fef85 8626
da0436e9 8627 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 8628 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 8629 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 8630
da0436e9
JS
8631 return 1;
8632}
8633
e59058c4 8634/**
3621a710 8635 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
8636 * @srcp: Source memory pointer.
8637 * @destp: Destination memory pointer.
8638 * @cnt: Number of words required to be copied.
8639 *
8640 * This function is used for copying data between driver memory
8641 * and the SLI memory. This function also changes the endianness
8642 * of each word if native endianness is different from SLI
8643 * endianness. This function can be called with or without
8644 * lock.
8645 **/
dea3101e 8646void
8647lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
8648{
8649 uint32_t *src = srcp;
8650 uint32_t *dest = destp;
8651 uint32_t ldata;
8652 int i;
8653
8654 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
8655 ldata = *src;
8656 ldata = le32_to_cpu(ldata);
8657 *dest = ldata;
8658 src++;
8659 dest++;
8660 }
8661}
8662
e59058c4 8663
a0c87cbd
JS
8664/**
8665 * lpfc_sli_bemem_bcopy - SLI memory copy function
8666 * @srcp: Source memory pointer.
8667 * @destp: Destination memory pointer.
8668 * @cnt: Number of words required to be copied.
8669 *
8670 * This function is used for copying data between a data structure
8671 * with big endian representation to local endianness.
8672 * This function can be called with or without lock.
8673 **/
8674void
8675lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
8676{
8677 uint32_t *src = srcp;
8678 uint32_t *dest = destp;
8679 uint32_t ldata;
8680 int i;
8681
8682 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
8683 ldata = *src;
8684 ldata = be32_to_cpu(ldata);
8685 *dest = ldata;
8686 src++;
8687 dest++;
8688 }
8689}
8690
e59058c4 8691/**
3621a710 8692 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
8693 * @phba: Pointer to HBA context object.
8694 * @pring: Pointer to driver SLI ring object.
8695 * @mp: Pointer to driver buffer object.
8696 *
8697 * This function is called with no lock held.
8698 * It always return zero after adding the buffer to the postbufq
8699 * buffer list.
8700 **/
dea3101e 8701int
2e0fef85
JS
8702lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8703 struct lpfc_dmabuf *mp)
dea3101e 8704{
8705 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
8706 later */
2e0fef85 8707 spin_lock_irq(&phba->hbalock);
dea3101e 8708 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 8709 pring->postbufq_cnt++;
2e0fef85 8710 spin_unlock_irq(&phba->hbalock);
dea3101e 8711 return 0;
8712}
8713
e59058c4 8714/**
3621a710 8715 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
8716 * @phba: Pointer to HBA context object.
8717 *
8718 * When HBQ is enabled, buffers are searched based on tags. This function
8719 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
8720 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
8721 * does not conflict with tags of buffer posted for unsolicited events.
8722 * The function returns the allocated tag. The function is called with
8723 * no locks held.
8724 **/
76bb24ef
JS
8725uint32_t
8726lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
8727{
8728 spin_lock_irq(&phba->hbalock);
8729 phba->buffer_tag_count++;
8730 /*
8731 * Always set the QUE_BUFTAG_BIT to distiguish between
8732 * a tag assigned by HBQ.
8733 */
8734 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
8735 spin_unlock_irq(&phba->hbalock);
8736 return phba->buffer_tag_count;
8737}
8738
e59058c4 8739/**
3621a710 8740 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
8741 * @phba: Pointer to HBA context object.
8742 * @pring: Pointer to driver SLI ring object.
8743 * @tag: Buffer tag.
8744 *
8745 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
8746 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
8747 * iocb is posted to the response ring with the tag of the buffer.
8748 * This function searches the pring->postbufq list using the tag
8749 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
8750 * iocb. If the buffer is found then lpfc_dmabuf object of the
8751 * buffer is returned to the caller else NULL is returned.
8752 * This function is called with no lock held.
8753 **/
76bb24ef
JS
8754struct lpfc_dmabuf *
8755lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8756 uint32_t tag)
8757{
8758 struct lpfc_dmabuf *mp, *next_mp;
8759 struct list_head *slp = &pring->postbufq;
8760
25985edc 8761 /* Search postbufq, from the beginning, looking for a match on tag */
76bb24ef
JS
8762 spin_lock_irq(&phba->hbalock);
8763 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8764 if (mp->buffer_tag == tag) {
8765 list_del_init(&mp->list);
8766 pring->postbufq_cnt--;
8767 spin_unlock_irq(&phba->hbalock);
8768 return mp;
8769 }
8770 }
8771
8772 spin_unlock_irq(&phba->hbalock);
8773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 8774 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
8775 "ring %d Data x%lx x%p x%p x%x\n",
8776 pring->ringno, (unsigned long) tag,
8777 slp->next, slp->prev, pring->postbufq_cnt);
8778
8779 return NULL;
8780}
dea3101e 8781
e59058c4 8782/**
3621a710 8783 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
8784 * @phba: Pointer to HBA context object.
8785 * @pring: Pointer to driver SLI ring object.
8786 * @phys: DMA address of the buffer.
8787 *
8788 * This function searches the buffer list using the dma_address
8789 * of unsolicited event to find the driver's lpfc_dmabuf object
8790 * corresponding to the dma_address. The function returns the
8791 * lpfc_dmabuf object if a buffer is found else it returns NULL.
8792 * This function is called by the ct and els unsolicited event
8793 * handlers to get the buffer associated with the unsolicited
8794 * event.
8795 *
8796 * This function is called with no lock held.
8797 **/
dea3101e 8798struct lpfc_dmabuf *
8799lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
8800 dma_addr_t phys)
8801{
8802 struct lpfc_dmabuf *mp, *next_mp;
8803 struct list_head *slp = &pring->postbufq;
8804
25985edc 8805 /* Search postbufq, from the beginning, looking for a match on phys */
2e0fef85 8806 spin_lock_irq(&phba->hbalock);
dea3101e 8807 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
8808 if (mp->phys == phys) {
8809 list_del_init(&mp->list);
8810 pring->postbufq_cnt--;
2e0fef85 8811 spin_unlock_irq(&phba->hbalock);
dea3101e 8812 return mp;
8813 }
8814 }
8815
2e0fef85 8816 spin_unlock_irq(&phba->hbalock);
dea3101e 8817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 8818 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 8819 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 8820 pring->ringno, (unsigned long long)phys,
dea3101e 8821 slp->next, slp->prev, pring->postbufq_cnt);
8822 return NULL;
8823}
8824
e59058c4 8825/**
3621a710 8826 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
8827 * @phba: Pointer to HBA context object.
8828 * @cmdiocb: Pointer to driver command iocb object.
8829 * @rspiocb: Pointer to driver response iocb object.
8830 *
8831 * This function is the completion handler for the abort iocbs for
8832 * ELS commands. This function is called from the ELS ring event
8833 * handler with no lock held. This function frees memory resources
8834 * associated with the abort iocb.
8835 **/
dea3101e 8836static void
2e0fef85
JS
8837lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8838 struct lpfc_iocbq *rspiocb)
dea3101e 8839{
2e0fef85 8840 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 8841 uint16_t abort_iotag, abort_context;
92d7f7b0 8842 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
8843 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8844
8845 abort_iocb = NULL;
2680eeaa
JS
8846
8847 if (irsp->ulpStatus) {
8848 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
8849 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
8850
2e0fef85 8851 spin_lock_irq(&phba->hbalock);
45ed1190
JS
8852 if (phba->sli_rev < LPFC_SLI_REV4) {
8853 if (abort_iotag != 0 &&
8854 abort_iotag <= phba->sli.last_iotag)
8855 abort_iocb =
8856 phba->sli.iocbq_lookup[abort_iotag];
8857 } else
8858 /* For sli4 the abort_tag is the XRI,
8859 * so the abort routine puts the iotag of the iocb
8860 * being aborted in the context field of the abort
8861 * IOCB.
8862 */
8863 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 8864
58da1ffb
JS
8865 /*
8866 * If the iocb is not found in Firmware queue the iocb
8867 * might have completed already. Do not free it again.
8868 */
9b379605 8869 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
45ed1190
JS
8870 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
8871 spin_unlock_irq(&phba->hbalock);
8872 lpfc_sli_release_iocbq(phba, cmdiocb);
8873 return;
8874 }
8875 /* For SLI4 the ulpContext field for abort IOCB
8876 * holds the iotag of the IOCB being aborted so
8877 * the local abort_context needs to be reset to
8878 * match the aborted IOCBs ulpContext.
8879 */
8880 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
8881 abort_context = abort_iocb->iocb.ulpContext;
58da1ffb 8882 }
2a9bf3d0
JS
8883
8884 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
8885 "0327 Cannot abort els iocb %p "
8886 "with tag %x context %x, abort status %x, "
8887 "abort code %x\n",
8888 abort_iocb, abort_iotag, abort_context,
8889 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa
JS
8890 /*
8891 * make sure we have the right iocbq before taking it
8892 * off the txcmplq and try to call completion routine.
8893 */
2e0fef85
JS
8894 if (!abort_iocb ||
8895 abort_iocb->iocb.ulpContext != abort_context ||
8896 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
8897 spin_unlock_irq(&phba->hbalock);
341af102
JS
8898 else if (phba->sli_rev < LPFC_SLI_REV4) {
8899 /*
8900 * leave the SLI4 aborted command on the txcmplq
8901 * list and the command complete WCQE's XB bit
8902 * will tell whether the SGL (XRI) can be released
8903 * immediately or to the aborted SGL list for the
8904 * following abort XRI from the HBA.
8905 */
92d7f7b0 8906 list_del_init(&abort_iocb->list);
2a9bf3d0
JS
8907 if (abort_iocb->iocb_flag & LPFC_IO_ON_Q) {
8908 abort_iocb->iocb_flag &= ~LPFC_IO_ON_Q;
8909 pring->txcmplq_cnt--;
8910 }
2680eeaa 8911
0ff10d46
JS
8912 /* Firmware could still be in progress of DMAing
8913 * payload, so don't free data buffer till after
8914 * a hbeat.
8915 */
8916 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
92d7f7b0 8917 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
341af102
JS
8918 spin_unlock_irq(&phba->hbalock);
8919
92d7f7b0 8920 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
341af102 8921 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
92d7f7b0 8922 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
49198b37
JS
8923 } else
8924 spin_unlock_irq(&phba->hbalock);
2680eeaa
JS
8925 }
8926
604a3e30 8927 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 8928 return;
8929}
8930
e59058c4 8931/**
3621a710 8932 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
8933 * @phba: Pointer to HBA context object.
8934 * @cmdiocb: Pointer to driver command iocb object.
8935 * @rspiocb: Pointer to driver response iocb object.
8936 *
8937 * The function is called from SLI ring event handler with no
8938 * lock held. This function is the completion handler for ELS commands
8939 * which are aborted. The function frees memory resources used for
8940 * the aborted ELS commands.
8941 **/
92d7f7b0
JS
8942static void
8943lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
8944 struct lpfc_iocbq *rspiocb)
8945{
8946 IOCB_t *irsp = &rspiocb->iocb;
8947
8948 /* ELS cmd tag <ulpIoTag> completes */
8949 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 8950 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 8951 "x%x x%x x%x\n",
e8b62011 8952 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 8953 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
8954 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
8955 lpfc_ct_free_iocb(phba, cmdiocb);
8956 else
8957 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
8958 return;
8959}
8960
e59058c4 8961/**
5af5eee7 8962 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
e59058c4
JS
8963 * @phba: Pointer to HBA context object.
8964 * @pring: Pointer to driver SLI ring object.
8965 * @cmdiocb: Pointer to driver command iocb object.
8966 *
5af5eee7
JS
8967 * This function issues an abort iocb for the provided command iocb down to
8968 * the port. Other than the case the outstanding command iocb is an abort
8969 * request, this function issues abort out unconditionally. This function is
8970 * called with hbalock held. The function returns 0 when it fails due to
8971 * memory allocation failure or when the command iocb is an abort request.
e59058c4 8972 **/
5af5eee7
JS
8973static int
8974lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 8975 struct lpfc_iocbq *cmdiocb)
dea3101e 8976{
2e0fef85 8977 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 8978 struct lpfc_iocbq *abtsiocbp;
dea3101e 8979 IOCB_t *icmd = NULL;
8980 IOCB_t *iabt = NULL;
5af5eee7 8981 int retval;
07951076 8982
92d7f7b0
JS
8983 /*
8984 * There are certain command types we don't want to abort. And we
8985 * don't want to abort commands that are already in the process of
8986 * being aborted.
07951076
JS
8987 */
8988 icmd = &cmdiocb->iocb;
2e0fef85 8989 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
8990 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
8991 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
8992 return 0;
8993
dea3101e 8994 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 8995 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 8996 if (abtsiocbp == NULL)
8997 return 0;
dea3101e 8998
07951076 8999 /* This signals the response to set the correct status
341af102 9000 * before calling the completion handler
07951076
JS
9001 */
9002 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9003
dea3101e 9004 iabt = &abtsiocbp->iocb;
07951076
JS
9005 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9006 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 9007 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 9008 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
9009 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9010 }
da0436e9
JS
9011 else
9012 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
9013 iabt->ulpLe = 1;
9014 iabt->ulpClass = icmd->ulpClass;
dea3101e 9015
5ffc266e
JS
9016 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9017 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
9018 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9019 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9020
2e0fef85 9021 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
9022 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9023 else
9024 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 9025
07951076 9026 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 9027
e8b62011
JS
9028 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9029 "0339 Abort xri x%x, original iotag x%x, "
9030 "abort cmd iotag x%x\n",
2a9bf3d0 9031 iabt->un.acxri.abortIoTag,
e8b62011 9032 iabt->un.acxri.abortContextTag,
2a9bf3d0 9033 abtsiocbp->iotag);
da0436e9 9034 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 9035
d7c255b2
JS
9036 if (retval)
9037 __lpfc_sli_release_iocbq(phba, abtsiocbp);
5af5eee7
JS
9038
9039 /*
9040 * Caller to this routine should check for IOCB_ERROR
9041 * and handle it properly. This routine no longer removes
9042 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9043 */
9044 return retval;
9045}
9046
9047/**
9048 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9049 * @phba: Pointer to HBA context object.
9050 * @pring: Pointer to driver SLI ring object.
9051 * @cmdiocb: Pointer to driver command iocb object.
9052 *
9053 * This function issues an abort iocb for the provided command iocb. In case
9054 * of unloading, the abort iocb will not be issued to commands on the ELS
9055 * ring. Instead, the callback function shall be changed to those commands
9056 * so that nothing happens when them finishes. This function is called with
9057 * hbalock held. The function returns 0 when the command iocb is an abort
9058 * request.
9059 **/
9060int
9061lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9062 struct lpfc_iocbq *cmdiocb)
9063{
9064 struct lpfc_vport *vport = cmdiocb->vport;
9065 int retval = IOCB_ERROR;
9066 IOCB_t *icmd = NULL;
9067
9068 /*
9069 * There are certain command types we don't want to abort. And we
9070 * don't want to abort commands that are already in the process of
9071 * being aborted.
9072 */
9073 icmd = &cmdiocb->iocb;
9074 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9075 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9076 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9077 return 0;
9078
9079 /*
9080 * If we're unloading, don't abort iocb on the ELS ring, but change
9081 * the callback so that nothing happens when it finishes.
9082 */
9083 if ((vport->load_flag & FC_UNLOADING) &&
9084 (pring->ringno == LPFC_ELS_RING)) {
9085 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9086 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9087 else
9088 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9089 goto abort_iotag_exit;
9090 }
9091
9092 /* Now, we try to issue the abort to the cmdiocb out */
9093 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9094
07951076 9095abort_iotag_exit:
2e0fef85
JS
9096 /*
9097 * Caller to this routine should check for IOCB_ERROR
9098 * and handle it properly. This routine no longer removes
9099 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 9100 */
2e0fef85 9101 return retval;
dea3101e 9102}
9103
5af5eee7
JS
9104/**
9105 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9106 * @phba: Pointer to HBA context object.
9107 * @pring: Pointer to driver SLI ring object.
9108 *
9109 * This function aborts all iocbs in the given ring and frees all the iocb
9110 * objects in txq. This function issues abort iocbs unconditionally for all
9111 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9112 * to complete before the return of this function. The caller is not required
9113 * to hold any locks.
9114 **/
9115static void
9116lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9117{
9118 LIST_HEAD(completions);
9119 struct lpfc_iocbq *iocb, *next_iocb;
9120
9121 if (pring->ringno == LPFC_ELS_RING)
9122 lpfc_fabric_abort_hba(phba);
9123
9124 spin_lock_irq(&phba->hbalock);
9125
9126 /* Take off all the iocbs on txq for cancelling */
9127 list_splice_init(&pring->txq, &completions);
9128 pring->txq_cnt = 0;
9129
9130 /* Next issue ABTS for everything on the txcmplq */
9131 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9132 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9133
9134 spin_unlock_irq(&phba->hbalock);
9135
9136 /* Cancel all the IOCBs from the completions list */
9137 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9138 IOERR_SLI_ABORTED);
9139}
9140
9141/**
9142 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9143 * @phba: pointer to lpfc HBA data structure.
9144 *
9145 * This routine will abort all pending and outstanding iocbs to an HBA.
9146 **/
9147void
9148lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9149{
9150 struct lpfc_sli *psli = &phba->sli;
9151 struct lpfc_sli_ring *pring;
9152 int i;
9153
9154 for (i = 0; i < psli->num_rings; i++) {
9155 pring = &psli->ring[i];
9156 lpfc_sli_iocb_ring_abort(phba, pring);
9157 }
9158}
9159
e59058c4 9160/**
3621a710 9161 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
9162 * @iocbq: Pointer to driver iocb object.
9163 * @vport: Pointer to driver virtual port object.
9164 * @tgt_id: SCSI ID of the target.
9165 * @lun_id: LUN ID of the scsi device.
9166 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9167 *
3621a710 9168 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
9169 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9170 * 0 if the filtering criteria is met for the given iocb and will return
9171 * 1 if the filtering criteria is not met.
9172 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9173 * given iocb is for the SCSI device specified by vport, tgt_id and
9174 * lun_id parameter.
9175 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9176 * given iocb is for the SCSI target specified by vport and tgt_id
9177 * parameters.
9178 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9179 * given iocb is for the SCSI host associated with the given vport.
9180 * This function is called with no locks held.
9181 **/
dea3101e 9182static int
51ef4c26
JS
9183lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9184 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 9185 lpfc_ctx_cmd ctx_cmd)
dea3101e 9186{
0bd4ca25 9187 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 9188 int rc = 1;
9189
0bd4ca25
JSEC
9190 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9191 return rc;
9192
51ef4c26
JS
9193 if (iocbq->vport != vport)
9194 return rc;
9195
0bd4ca25 9196 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 9197
495a714c 9198 if (lpfc_cmd->pCmd == NULL)
dea3101e 9199 return rc;
9200
9201 switch (ctx_cmd) {
9202 case LPFC_CTX_LUN:
495a714c
JS
9203 if ((lpfc_cmd->rdata->pnode) &&
9204 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9205 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 9206 rc = 0;
9207 break;
9208 case LPFC_CTX_TGT:
495a714c
JS
9209 if ((lpfc_cmd->rdata->pnode) &&
9210 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 9211 rc = 0;
9212 break;
dea3101e 9213 case LPFC_CTX_HOST:
9214 rc = 0;
9215 break;
9216 default:
9217 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 9218 __func__, ctx_cmd);
dea3101e 9219 break;
9220 }
9221
9222 return rc;
9223}
9224
e59058c4 9225/**
3621a710 9226 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
9227 * @vport: Pointer to virtual port.
9228 * @tgt_id: SCSI ID of the target.
9229 * @lun_id: LUN ID of the scsi device.
9230 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9231 *
9232 * This function returns number of FCP commands pending for the vport.
9233 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9234 * commands pending on the vport associated with SCSI device specified
9235 * by tgt_id and lun_id parameters.
9236 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9237 * commands pending on the vport associated with SCSI target specified
9238 * by tgt_id parameter.
9239 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9240 * commands pending on the vport.
9241 * This function returns the number of iocbs which satisfy the filter.
9242 * This function is called without any lock held.
9243 **/
dea3101e 9244int
51ef4c26
JS
9245lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9246 lpfc_ctx_cmd ctx_cmd)
dea3101e 9247{
51ef4c26 9248 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9249 struct lpfc_iocbq *iocbq;
9250 int sum, i;
dea3101e 9251
0bd4ca25
JSEC
9252 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9253 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9254
51ef4c26
JS
9255 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9256 ctx_cmd) == 0)
0bd4ca25 9257 sum++;
dea3101e 9258 }
0bd4ca25 9259
dea3101e 9260 return sum;
9261}
9262
e59058c4 9263/**
3621a710 9264 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
9265 * @phba: Pointer to HBA context object
9266 * @cmdiocb: Pointer to command iocb object.
9267 * @rspiocb: Pointer to response iocb object.
9268 *
9269 * This function is called when an aborted FCP iocb completes. This
9270 * function is called by the ring event handler with no lock held.
9271 * This function frees the iocb.
9272 **/
5eb95af0 9273void
2e0fef85
JS
9274lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9275 struct lpfc_iocbq *rspiocb)
5eb95af0 9276{
604a3e30 9277 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
9278 return;
9279}
9280
e59058c4 9281/**
3621a710 9282 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
9283 * @vport: Pointer to virtual port.
9284 * @pring: Pointer to driver SLI ring object.
9285 * @tgt_id: SCSI ID of the target.
9286 * @lun_id: LUN ID of the scsi device.
9287 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9288 *
9289 * This function sends an abort command for every SCSI command
9290 * associated with the given virtual port pending on the ring
9291 * filtered by lpfc_sli_validate_fcp_iocb function.
9292 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9293 * FCP iocbs associated with lun specified by tgt_id and lun_id
9294 * parameters
9295 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9296 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9297 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9298 * FCP iocbs associated with virtual port.
9299 * This function returns number of iocbs it failed to abort.
9300 * This function is called with no locks held.
9301 **/
dea3101e 9302int
51ef4c26
JS
9303lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9304 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 9305{
51ef4c26 9306 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
9307 struct lpfc_iocbq *iocbq;
9308 struct lpfc_iocbq *abtsiocb;
dea3101e 9309 IOCB_t *cmd = NULL;
dea3101e 9310 int errcnt = 0, ret_val = 0;
0bd4ca25 9311 int i;
dea3101e 9312
0bd4ca25
JSEC
9313 for (i = 1; i <= phba->sli.last_iotag; i++) {
9314 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 9315
51ef4c26 9316 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 9317 abort_cmd) != 0)
dea3101e 9318 continue;
9319
9320 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 9321 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 9322 if (abtsiocb == NULL) {
9323 errcnt++;
9324 continue;
9325 }
dea3101e 9326
0bd4ca25 9327 cmd = &iocbq->iocb;
dea3101e 9328 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9329 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
9330 if (phba->sli_rev == LPFC_SLI_REV4)
9331 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9332 else
9333 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 9334 abtsiocb->iocb.ulpLe = 1;
9335 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 9336 abtsiocb->vport = phba->pport;
dea3101e 9337
5ffc266e
JS
9338 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9339 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
9340 if (iocbq->iocb_flag & LPFC_IO_FCP)
9341 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 9342
2e0fef85 9343 if (lpfc_is_link_up(phba))
dea3101e 9344 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9345 else
9346 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9347
5eb95af0
JSEC
9348 /* Setup callback routine and issue the command. */
9349 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
9350 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9351 abtsiocb, 0);
dea3101e 9352 if (ret_val == IOCB_ERROR) {
604a3e30 9353 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 9354 errcnt++;
9355 continue;
9356 }
9357 }
9358
9359 return errcnt;
9360}
9361
e59058c4 9362/**
3621a710 9363 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
9364 * @phba: Pointer to HBA context object.
9365 * @cmdiocbq: Pointer to command iocb.
9366 * @rspiocbq: Pointer to response iocb.
9367 *
9368 * This function is the completion handler for iocbs issued using
9369 * lpfc_sli_issue_iocb_wait function. This function is called by the
9370 * ring event handler function without any lock held. This function
9371 * can be called from both worker thread context and interrupt
9372 * context. This function also can be called from other thread which
9373 * cleans up the SLI layer objects.
9374 * This function copy the contents of the response iocb to the
9375 * response iocb memory object provided by the caller of
9376 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9377 * sleeps for the iocb completion.
9378 **/
68876920
JSEC
9379static void
9380lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9381 struct lpfc_iocbq *cmdiocbq,
9382 struct lpfc_iocbq *rspiocbq)
dea3101e 9383{
68876920
JSEC
9384 wait_queue_head_t *pdone_q;
9385 unsigned long iflags;
0f65ff68 9386 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 9387
2e0fef85 9388 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
9389 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9390 if (cmdiocbq->context2 && rspiocbq)
9391 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9392 &rspiocbq->iocb, sizeof(IOCB_t));
9393
0f65ff68
JS
9394 /* Set the exchange busy flag for task management commands */
9395 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9396 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9397 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9398 cur_iocbq);
9399 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9400 }
9401
68876920 9402 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
9403 if (pdone_q)
9404 wake_up(pdone_q);
858c9f6c 9405 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 9406 return;
9407}
9408
d11e31dd
JS
9409/**
9410 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9411 * @phba: Pointer to HBA context object..
9412 * @piocbq: Pointer to command iocb.
9413 * @flag: Flag to test.
9414 *
9415 * This routine grabs the hbalock and then test the iocb_flag to
9416 * see if the passed in flag is set.
9417 * Returns:
9418 * 1 if flag is set.
9419 * 0 if flag is not set.
9420 **/
9421static int
9422lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9423 struct lpfc_iocbq *piocbq, uint32_t flag)
9424{
9425 unsigned long iflags;
9426 int ret;
9427
9428 spin_lock_irqsave(&phba->hbalock, iflags);
9429 ret = piocbq->iocb_flag & flag;
9430 spin_unlock_irqrestore(&phba->hbalock, iflags);
9431 return ret;
9432
9433}
9434
e59058c4 9435/**
3621a710 9436 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
9437 * @phba: Pointer to HBA context object..
9438 * @pring: Pointer to sli ring.
9439 * @piocb: Pointer to command iocb.
9440 * @prspiocbq: Pointer to response iocb.
9441 * @timeout: Timeout in number of seconds.
9442 *
9443 * This function issues the iocb to firmware and waits for the
9444 * iocb to complete. If the iocb command is not
9445 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9446 * Caller should not free the iocb resources if this function
9447 * returns IOCB_TIMEDOUT.
9448 * The function waits for the iocb completion using an
9449 * non-interruptible wait.
9450 * This function will sleep while waiting for iocb completion.
9451 * So, this function should not be called from any context which
9452 * does not allow sleeping. Due to the same reason, this function
9453 * cannot be called with interrupt disabled.
9454 * This function assumes that the iocb completions occur while
9455 * this function sleep. So, this function cannot be called from
9456 * the thread which process iocb completion for this ring.
9457 * This function clears the iocb_flag of the iocb object before
9458 * issuing the iocb and the iocb completion handler sets this
9459 * flag and wakes this thread when the iocb completes.
9460 * The contents of the response iocb will be copied to prspiocbq
9461 * by the completion handler when the command completes.
9462 * This function returns IOCB_SUCCESS when success.
9463 * This function is called with no lock held.
9464 **/
dea3101e 9465int
2e0fef85 9466lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 9467 uint32_t ring_number,
2e0fef85
JS
9468 struct lpfc_iocbq *piocb,
9469 struct lpfc_iocbq *prspiocbq,
68876920 9470 uint32_t timeout)
dea3101e 9471{
7259f0d0 9472 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
9473 long timeleft, timeout_req = 0;
9474 int retval = IOCB_SUCCESS;
875fbdfe 9475 uint32_t creg_val;
2a9bf3d0 9476 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
dea3101e 9477 /*
68876920
JSEC
9478 * If the caller has provided a response iocbq buffer, then context2
9479 * is NULL or its an error.
dea3101e 9480 */
68876920
JSEC
9481 if (prspiocbq) {
9482 if (piocb->context2)
9483 return IOCB_ERROR;
9484 piocb->context2 = prspiocbq;
dea3101e 9485 }
9486
68876920
JSEC
9487 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9488 piocb->context_un.wait_queue = &done_q;
9489 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 9490
875fbdfe 9491 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
9492 if (lpfc_readl(phba->HCregaddr, &creg_val))
9493 return IOCB_ERROR;
875fbdfe
JSEC
9494 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9495 writel(creg_val, phba->HCregaddr);
9496 readl(phba->HCregaddr); /* flush */
9497 }
9498
2a9bf3d0
JS
9499 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
9500 SLI_IOCB_RET_IOCB);
68876920
JSEC
9501 if (retval == IOCB_SUCCESS) {
9502 timeout_req = timeout * HZ;
68876920 9503 timeleft = wait_event_timeout(done_q,
d11e31dd 9504 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 9505 timeout_req);
dea3101e 9506
7054a606
JS
9507 if (piocb->iocb_flag & LPFC_IO_WAKE) {
9508 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 9509 "0331 IOCB wake signaled\n");
7054a606 9510 } else if (timeleft == 0) {
68876920 9511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
9512 "0338 IOCB wait timeout error - no "
9513 "wake response Data x%x\n", timeout);
68876920 9514 retval = IOCB_TIMEDOUT;
7054a606 9515 } else {
68876920 9516 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
9517 "0330 IOCB wake NOT set, "
9518 "Data x%x x%lx\n",
68876920
JSEC
9519 timeout, (timeleft / jiffies));
9520 retval = IOCB_TIMEDOUT;
dea3101e 9521 }
2a9bf3d0
JS
9522 } else if (retval == IOCB_BUSY) {
9523 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9524 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
9525 phba->iocb_cnt, pring->txq_cnt, pring->txcmplq_cnt);
9526 return retval;
68876920
JSEC
9527 } else {
9528 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 9529 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 9530 retval);
68876920 9531 retval = IOCB_ERROR;
dea3101e 9532 }
9533
875fbdfe 9534 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9940b97b
JS
9535 if (lpfc_readl(phba->HCregaddr, &creg_val))
9536 return IOCB_ERROR;
875fbdfe
JSEC
9537 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
9538 writel(creg_val, phba->HCregaddr);
9539 readl(phba->HCregaddr); /* flush */
9540 }
9541
68876920
JSEC
9542 if (prspiocbq)
9543 piocb->context2 = NULL;
9544
9545 piocb->context_un.wait_queue = NULL;
9546 piocb->iocb_cmpl = NULL;
dea3101e 9547 return retval;
9548}
68876920 9549
e59058c4 9550/**
3621a710 9551 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
9552 * @phba: Pointer to HBA context object.
9553 * @pmboxq: Pointer to driver mailbox object.
9554 * @timeout: Timeout in number of seconds.
9555 *
9556 * This function issues the mailbox to firmware and waits for the
9557 * mailbox command to complete. If the mailbox command is not
9558 * completed within timeout seconds, it returns MBX_TIMEOUT.
9559 * The function waits for the mailbox completion using an
9560 * interruptible wait. If the thread is woken up due to a
9561 * signal, MBX_TIMEOUT error is returned to the caller. Caller
9562 * should not free the mailbox resources, if this function returns
9563 * MBX_TIMEOUT.
9564 * This function will sleep while waiting for mailbox completion.
9565 * So, this function should not be called from any context which
9566 * does not allow sleeping. Due to the same reason, this function
9567 * cannot be called with interrupt disabled.
9568 * This function assumes that the mailbox completion occurs while
9569 * this function sleep. So, this function cannot be called from
9570 * the worker thread which processes mailbox completion.
9571 * This function is called in the context of HBA management
9572 * applications.
9573 * This function returns MBX_SUCCESS when successful.
9574 * This function is called with no lock held.
9575 **/
dea3101e 9576int
2e0fef85 9577lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 9578 uint32_t timeout)
9579{
7259f0d0 9580 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 9581 int retval;
858c9f6c 9582 unsigned long flag;
dea3101e 9583
9584 /* The caller must leave context1 empty. */
98c9ea5c 9585 if (pmboxq->context1)
2e0fef85 9586 return MBX_NOT_FINISHED;
dea3101e 9587
495a714c 9588 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 9589 /* setup wake call as IOCB callback */
9590 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
9591 /* setup context field to pass wait_queue pointer to wake function */
9592 pmboxq->context1 = &done_q;
9593
dea3101e 9594 /* now issue the command */
9595 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
dea3101e 9596 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
9597 wait_event_interruptible_timeout(done_q,
9598 pmboxq->mbox_flag & LPFC_MBX_WAKE,
9599 timeout * HZ);
9600
858c9f6c 9601 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 9602 pmboxq->context1 = NULL;
7054a606
JS
9603 /*
9604 * if LPFC_MBX_WAKE flag is set the mailbox is completed
9605 * else do not free the resources.
9606 */
d7c47992 9607 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
dea3101e 9608 retval = MBX_SUCCESS;
d7c47992
JS
9609 lpfc_sli4_swap_str(phba, pmboxq);
9610 } else {
7054a606 9611 retval = MBX_TIMEOUT;
858c9f6c
JS
9612 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9613 }
9614 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 9615 }
9616
dea3101e 9617 return retval;
9618}
9619
e59058c4 9620/**
3772a991 9621 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
9622 * @phba: Pointer to HBA context.
9623 *
3772a991
JS
9624 * This function is called to shutdown the driver's mailbox sub-system.
9625 * It first marks the mailbox sub-system is in a block state to prevent
9626 * the asynchronous mailbox command from issued off the pending mailbox
9627 * command queue. If the mailbox command sub-system shutdown is due to
9628 * HBA error conditions such as EEH or ERATT, this routine shall invoke
9629 * the mailbox sub-system flush routine to forcefully bring down the
9630 * mailbox sub-system. Otherwise, if it is due to normal condition (such
9631 * as with offline or HBA function reset), this routine will wait for the
9632 * outstanding mailbox command to complete before invoking the mailbox
9633 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 9634 **/
3772a991
JS
9635void
9636lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 9637{
3772a991 9638 struct lpfc_sli *psli = &phba->sli;
3772a991 9639 unsigned long timeout;
b4c02652 9640
a183a15f 9641 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
3772a991
JS
9642 spin_lock_irq(&phba->hbalock);
9643 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
9644 spin_unlock_irq(&phba->hbalock);
b4c02652 9645
3772a991 9646 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 9647 spin_lock_irq(&phba->hbalock);
3772a991
JS
9648 /* Determine how long we might wait for the active mailbox
9649 * command to be gracefully completed by firmware.
9650 */
a183a15f
JS
9651 if (phba->sli.mbox_active)
9652 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
9653 phba->sli.mbox_active) *
9654 1000) + jiffies;
9655 spin_unlock_irq(&phba->hbalock);
9656
3772a991
JS
9657 while (phba->sli.mbox_active) {
9658 /* Check active mailbox complete status every 2ms */
9659 msleep(2);
9660 if (time_after(jiffies, timeout))
9661 /* Timeout, let the mailbox flush routine to
9662 * forcefully release active mailbox command
9663 */
9664 break;
9665 }
9666 }
9667 lpfc_sli_mbox_sys_flush(phba);
9668}
ed957684 9669
3772a991
JS
9670/**
9671 * lpfc_sli_eratt_read - read sli-3 error attention events
9672 * @phba: Pointer to HBA context.
9673 *
9674 * This function is called to read the SLI3 device error attention registers
9675 * for possible error attention events. The caller must hold the hostlock
9676 * with spin_lock_irq().
9677 *
25985edc 9678 * This function returns 1 when there is Error Attention in the Host Attention
3772a991
JS
9679 * Register and returns 0 otherwise.
9680 **/
9681static int
9682lpfc_sli_eratt_read(struct lpfc_hba *phba)
9683{
9684 uint32_t ha_copy;
b4c02652 9685
3772a991 9686 /* Read chip Host Attention (HA) register */
9940b97b
JS
9687 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9688 goto unplug_err;
9689
3772a991
JS
9690 if (ha_copy & HA_ERATT) {
9691 /* Read host status register to retrieve error event */
9940b97b
JS
9692 if (lpfc_sli_read_hs(phba))
9693 goto unplug_err;
b4c02652 9694
3772a991
JS
9695 /* Check if there is a deferred error condition is active */
9696 if ((HS_FFER1 & phba->work_hs) &&
9697 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0 9698 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
3772a991 9699 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
9700 /* Clear all interrupt enable conditions */
9701 writel(0, phba->HCregaddr);
9702 readl(phba->HCregaddr);
9703 }
9704
9705 /* Set the driver HA work bitmap */
3772a991
JS
9706 phba->work_ha |= HA_ERATT;
9707 /* Indicate polling handles this ERATT */
9708 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
9709 return 1;
9710 }
9711 return 0;
9940b97b
JS
9712
9713unplug_err:
9714 /* Set the driver HS work bitmap */
9715 phba->work_hs |= UNPLUG_ERR;
9716 /* Set the driver HA work bitmap */
9717 phba->work_ha |= HA_ERATT;
9718 /* Indicate polling handles this ERATT */
9719 phba->hba_flag |= HBA_ERATT_HANDLED;
9720 return 1;
b4c02652
JS
9721}
9722
da0436e9
JS
9723/**
9724 * lpfc_sli4_eratt_read - read sli-4 error attention events
9725 * @phba: Pointer to HBA context.
9726 *
9727 * This function is called to read the SLI4 device error attention registers
9728 * for possible error attention events. The caller must hold the hostlock
9729 * with spin_lock_irq().
9730 *
25985edc 9731 * This function returns 1 when there is Error Attention in the Host Attention
da0436e9
JS
9732 * Register and returns 0 otherwise.
9733 **/
9734static int
9735lpfc_sli4_eratt_read(struct lpfc_hba *phba)
9736{
9737 uint32_t uerr_sta_hi, uerr_sta_lo;
2fcee4bf
JS
9738 uint32_t if_type, portsmphr;
9739 struct lpfc_register portstat_reg;
da0436e9 9740
2fcee4bf
JS
9741 /*
9742 * For now, use the SLI4 device internal unrecoverable error
da0436e9
JS
9743 * registers for error attention. This can be changed later.
9744 */
2fcee4bf
JS
9745 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
9746 switch (if_type) {
9747 case LPFC_SLI_INTF_IF_TYPE_0:
9940b97b
JS
9748 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
9749 &uerr_sta_lo) ||
9750 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
9751 &uerr_sta_hi)) {
9752 phba->work_hs |= UNPLUG_ERR;
9753 phba->work_ha |= HA_ERATT;
9754 phba->hba_flag |= HBA_ERATT_HANDLED;
9755 return 1;
9756 }
2fcee4bf
JS
9757 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
9758 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
9759 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9760 "1423 HBA Unrecoverable error: "
9761 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
9762 "ue_mask_lo_reg=0x%x, "
9763 "ue_mask_hi_reg=0x%x\n",
9764 uerr_sta_lo, uerr_sta_hi,
9765 phba->sli4_hba.ue_mask_lo,
9766 phba->sli4_hba.ue_mask_hi);
9767 phba->work_status[0] = uerr_sta_lo;
9768 phba->work_status[1] = uerr_sta_hi;
9769 phba->work_ha |= HA_ERATT;
9770 phba->hba_flag |= HBA_ERATT_HANDLED;
9771 return 1;
9772 }
9773 break;
9774 case LPFC_SLI_INTF_IF_TYPE_2:
9940b97b
JS
9775 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
9776 &portstat_reg.word0) ||
9777 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
9778 &portsmphr)){
9779 phba->work_hs |= UNPLUG_ERR;
9780 phba->work_ha |= HA_ERATT;
9781 phba->hba_flag |= HBA_ERATT_HANDLED;
9782 return 1;
9783 }
2fcee4bf
JS
9784 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
9785 phba->work_status[0] =
9786 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
9787 phba->work_status[1] =
9788 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
9789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9790 "2885 Port Error Detected: "
9791 "port status reg 0x%x, "
9792 "port smphr reg 0x%x, "
9793 "error 1=0x%x, error 2=0x%x\n",
9794 portstat_reg.word0,
9795 portsmphr,
9796 phba->work_status[0],
9797 phba->work_status[1]);
9798 phba->work_ha |= HA_ERATT;
9799 phba->hba_flag |= HBA_ERATT_HANDLED;
9800 return 1;
9801 }
9802 break;
9803 case LPFC_SLI_INTF_IF_TYPE_1:
9804 default:
a747c9ce 9805 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2fcee4bf
JS
9806 "2886 HBA Error Attention on unsupported "
9807 "if type %d.", if_type);
a747c9ce 9808 return 1;
da0436e9 9809 }
2fcee4bf 9810
da0436e9
JS
9811 return 0;
9812}
9813
e59058c4 9814/**
3621a710 9815 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
9816 * @phba: Pointer to HBA context.
9817 *
3772a991 9818 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
9819 * error attention register bit for error attention events.
9820 *
25985edc 9821 * This function returns 1 when there is Error Attention in the Host Attention
9399627f
JS
9822 * Register and returns 0 otherwise.
9823 **/
9824int
9825lpfc_sli_check_eratt(struct lpfc_hba *phba)
9826{
9827 uint32_t ha_copy;
9828
9829 /* If somebody is waiting to handle an eratt, don't process it
9830 * here. The brdkill function will do this.
9831 */
9832 if (phba->link_flag & LS_IGNORE_ERATT)
9833 return 0;
9834
9835 /* Check if interrupt handler handles this ERATT */
9836 spin_lock_irq(&phba->hbalock);
9837 if (phba->hba_flag & HBA_ERATT_HANDLED) {
9838 /* Interrupt handler has handled ERATT */
9839 spin_unlock_irq(&phba->hbalock);
9840 return 0;
9841 }
9842
a257bf90
JS
9843 /*
9844 * If there is deferred error attention, do not check for error
9845 * attention
9846 */
9847 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
9848 spin_unlock_irq(&phba->hbalock);
9849 return 0;
9850 }
9851
3772a991
JS
9852 /* If PCI channel is offline, don't process it */
9853 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 9854 spin_unlock_irq(&phba->hbalock);
3772a991
JS
9855 return 0;
9856 }
9857
9858 switch (phba->sli_rev) {
9859 case LPFC_SLI_REV2:
9860 case LPFC_SLI_REV3:
9861 /* Read chip Host Attention (HA) register */
9862 ha_copy = lpfc_sli_eratt_read(phba);
9863 break;
da0436e9 9864 case LPFC_SLI_REV4:
2fcee4bf 9865 /* Read device Uncoverable Error (UERR) registers */
da0436e9
JS
9866 ha_copy = lpfc_sli4_eratt_read(phba);
9867 break;
3772a991
JS
9868 default:
9869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9870 "0299 Invalid SLI revision (%d)\n",
9871 phba->sli_rev);
9872 ha_copy = 0;
9873 break;
9399627f
JS
9874 }
9875 spin_unlock_irq(&phba->hbalock);
3772a991
JS
9876
9877 return ha_copy;
9878}
9879
9880/**
9881 * lpfc_intr_state_check - Check device state for interrupt handling
9882 * @phba: Pointer to HBA context.
9883 *
9884 * This inline routine checks whether a device or its PCI slot is in a state
9885 * that the interrupt should be handled.
9886 *
9887 * This function returns 0 if the device or the PCI slot is in a state that
9888 * interrupt should be handled, otherwise -EIO.
9889 */
9890static inline int
9891lpfc_intr_state_check(struct lpfc_hba *phba)
9892{
9893 /* If the pci channel is offline, ignore all the interrupts */
9894 if (unlikely(pci_channel_offline(phba->pcidev)))
9895 return -EIO;
9896
9897 /* Update device level interrupt statistics */
9898 phba->sli.slistat.sli_intr++;
9899
9900 /* Ignore all interrupts during initialization. */
9901 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
9902 return -EIO;
9903
9399627f
JS
9904 return 0;
9905}
9906
9907/**
3772a991 9908 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
9909 * @irq: Interrupt number.
9910 * @dev_id: The device context pointer.
9911 *
9399627f 9912 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
9913 * service routine when device with SLI-3 interface spec is enabled with
9914 * MSI-X multi-message interrupt mode and there are slow-path events in
9915 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9916 * interrupt mode, this function is called as part of the device-level
9917 * interrupt handler. When the PCI slot is in error recovery or the HBA
9918 * is undergoing initialization, the interrupt handler will not process
9919 * the interrupt. The link attention and ELS ring attention events are
9920 * handled by the worker thread. The interrupt handler signals the worker
9921 * thread and returns for these events. This function is called without
9922 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
9923 * structures.
9924 *
9925 * This function returns IRQ_HANDLED when interrupt is handled else it
9926 * returns IRQ_NONE.
e59058c4 9927 **/
dea3101e 9928irqreturn_t
3772a991 9929lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 9930{
2e0fef85 9931 struct lpfc_hba *phba;
a747c9ce 9932 uint32_t ha_copy, hc_copy;
dea3101e 9933 uint32_t work_ha_copy;
9934 unsigned long status;
5b75da2f 9935 unsigned long iflag;
dea3101e 9936 uint32_t control;
9937
92d7f7b0 9938 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
9939 struct lpfc_vport *vport;
9940 struct lpfc_nodelist *ndlp;
9941 struct lpfc_dmabuf *mp;
92d7f7b0
JS
9942 LPFC_MBOXQ_t *pmb;
9943 int rc;
9944
dea3101e 9945 /*
9946 * Get the driver's phba structure from the dev_id and
9947 * assume the HBA is not interrupting.
9948 */
9399627f 9949 phba = (struct lpfc_hba *)dev_id;
dea3101e 9950
9951 if (unlikely(!phba))
9952 return IRQ_NONE;
9953
dea3101e 9954 /*
9399627f
JS
9955 * Stuff needs to be attented to when this function is invoked as an
9956 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 9957 */
9399627f 9958 if (phba->intr_type == MSIX) {
3772a991
JS
9959 /* Check device state for handling interrupt */
9960 if (lpfc_intr_state_check(phba))
9399627f
JS
9961 return IRQ_NONE;
9962 /* Need to read HA REG for slow-path events */
5b75da2f 9963 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
9964 if (lpfc_readl(phba->HAregaddr, &ha_copy))
9965 goto unplug_error;
9399627f
JS
9966 /* If somebody is waiting to handle an eratt don't process it
9967 * here. The brdkill function will do this.
9968 */
9969 if (phba->link_flag & LS_IGNORE_ERATT)
9970 ha_copy &= ~HA_ERATT;
9971 /* Check the need for handling ERATT in interrupt handler */
9972 if (ha_copy & HA_ERATT) {
9973 if (phba->hba_flag & HBA_ERATT_HANDLED)
9974 /* ERATT polling has handled ERATT */
9975 ha_copy &= ~HA_ERATT;
9976 else
9977 /* Indicate interrupt handler handles ERATT */
9978 phba->hba_flag |= HBA_ERATT_HANDLED;
9979 }
a257bf90
JS
9980
9981 /*
9982 * If there is deferred error attention, do not check for any
9983 * interrupt.
9984 */
9985 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 9986 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
9987 return IRQ_NONE;
9988 }
9989
9399627f 9990 /* Clear up only attention source related to slow-path */
9940b97b
JS
9991 if (lpfc_readl(phba->HCregaddr, &hc_copy))
9992 goto unplug_error;
9993
a747c9ce
JS
9994 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
9995 HC_LAINT_ENA | HC_ERINT_ENA),
9996 phba->HCregaddr);
9399627f
JS
9997 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
9998 phba->HAregaddr);
a747c9ce 9999 writel(hc_copy, phba->HCregaddr);
9399627f 10000 readl(phba->HAregaddr); /* flush */
5b75da2f 10001 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10002 } else
10003 ha_copy = phba->ha_copy;
dea3101e 10004
dea3101e 10005 work_ha_copy = ha_copy & phba->work_ha_mask;
10006
9399627f 10007 if (work_ha_copy) {
dea3101e 10008 if (work_ha_copy & HA_LATT) {
10009 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10010 /*
10011 * Turn off Link Attention interrupts
10012 * until CLEAR_LA done
10013 */
5b75da2f 10014 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 10015 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
9940b97b
JS
10016 if (lpfc_readl(phba->HCregaddr, &control))
10017 goto unplug_error;
dea3101e 10018 control &= ~HC_LAINT_ENA;
10019 writel(control, phba->HCregaddr);
10020 readl(phba->HCregaddr); /* flush */
5b75da2f 10021 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 10022 }
10023 else
10024 work_ha_copy &= ~HA_LATT;
10025 }
10026
9399627f 10027 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
10028 /*
10029 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10030 * the only slow ring.
10031 */
10032 status = (work_ha_copy &
10033 (HA_RXMASK << (4*LPFC_ELS_RING)));
10034 status >>= (4*LPFC_ELS_RING);
10035 if (status & HA_RXMASK) {
5b75da2f 10036 spin_lock_irqsave(&phba->hbalock, iflag);
9940b97b
JS
10037 if (lpfc_readl(phba->HCregaddr, &control))
10038 goto unplug_error;
a58cbd52
JS
10039
10040 lpfc_debugfs_slow_ring_trc(phba,
10041 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10042 control, status,
10043 (uint32_t)phba->sli.slistat.sli_intr);
10044
858c9f6c 10045 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
10046 lpfc_debugfs_slow_ring_trc(phba,
10047 "ISR Disable ring:"
10048 "pwork:x%x hawork:x%x wait:x%x",
10049 phba->work_ha, work_ha_copy,
10050 (uint32_t)((unsigned long)
5e9d9b82 10051 &phba->work_waitq));
a58cbd52 10052
858c9f6c
JS
10053 control &=
10054 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 10055 writel(control, phba->HCregaddr);
10056 readl(phba->HCregaddr); /* flush */
dea3101e 10057 }
a58cbd52
JS
10058 else {
10059 lpfc_debugfs_slow_ring_trc(phba,
10060 "ISR slow ring: pwork:"
10061 "x%x hawork:x%x wait:x%x",
10062 phba->work_ha, work_ha_copy,
10063 (uint32_t)((unsigned long)
5e9d9b82 10064 &phba->work_waitq));
a58cbd52 10065 }
5b75da2f 10066 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 10067 }
10068 }
5b75da2f 10069 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 10070 if (work_ha_copy & HA_ERATT) {
9940b97b
JS
10071 if (lpfc_sli_read_hs(phba))
10072 goto unplug_error;
a257bf90
JS
10073 /*
10074 * Check if there is a deferred error condition
10075 * is active
10076 */
10077 if ((HS_FFER1 & phba->work_hs) &&
10078 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
dcf2a4e0
JS
10079 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10080 phba->work_hs)) {
a257bf90
JS
10081 phba->hba_flag |= DEFER_ERATT;
10082 /* Clear all interrupt enable conditions */
10083 writel(0, phba->HCregaddr);
10084 readl(phba->HCregaddr);
10085 }
10086 }
10087
9399627f 10088 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 10089 pmb = phba->sli.mbox_active;
04c68496 10090 pmbox = &pmb->u.mb;
34b02dcd 10091 mbox = phba->mbox;
858c9f6c 10092 vport = pmb->vport;
92d7f7b0
JS
10093
10094 /* First check out the status word */
10095 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10096 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 10097 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
10098 /*
10099 * Stray Mailbox Interrupt, mbxCommand <cmd>
10100 * mbxStatus <status>
10101 */
09372820 10102 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 10103 LOG_SLI,
e8b62011 10104 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
10105 "Interrupt mbxCommand x%x "
10106 "mbxStatus x%x\n",
e8b62011 10107 (vport ? vport->vpi : 0),
92d7f7b0
JS
10108 pmbox->mbxCommand,
10109 pmbox->mbxStatus);
09372820
JS
10110 /* clear mailbox attention bit */
10111 work_ha_copy &= ~HA_MBATT;
10112 } else {
97eab634 10113 phba->sli.mbox_active = NULL;
5b75da2f 10114 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
10115 phba->last_completion_time = jiffies;
10116 del_timer(&phba->sli.mbox_tmo);
09372820
JS
10117 if (pmb->mbox_cmpl) {
10118 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10119 MAILBOX_CMD_SIZE);
7a470277
JS
10120 if (pmb->out_ext_byte_len &&
10121 pmb->context2)
10122 lpfc_sli_pcimem_bcopy(
10123 phba->mbox_ext,
10124 pmb->context2,
10125 pmb->out_ext_byte_len);
09372820
JS
10126 }
10127 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10128 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10129
10130 lpfc_debugfs_disc_trc(vport,
10131 LPFC_DISC_TRC_MBOX_VPORT,
10132 "MBOX dflt rpi: : "
10133 "status:x%x rpi:x%x",
10134 (uint32_t)pmbox->mbxStatus,
10135 pmbox->un.varWords[0], 0);
10136
10137 if (!pmbox->mbxStatus) {
10138 mp = (struct lpfc_dmabuf *)
10139 (pmb->context1);
10140 ndlp = (struct lpfc_nodelist *)
10141 pmb->context2;
10142
10143 /* Reg_LOGIN of dflt RPI was
10144 * successful. new lets get
10145 * rid of the RPI using the
10146 * same mbox buffer.
10147 */
10148 lpfc_unreg_login(phba,
10149 vport->vpi,
10150 pmbox->un.varWords[0],
10151 pmb);
10152 pmb->mbox_cmpl =
10153 lpfc_mbx_cmpl_dflt_rpi;
10154 pmb->context1 = mp;
10155 pmb->context2 = ndlp;
10156 pmb->vport = vport;
58da1ffb
JS
10157 rc = lpfc_sli_issue_mbox(phba,
10158 pmb,
10159 MBX_NOWAIT);
10160 if (rc != MBX_BUSY)
10161 lpfc_printf_log(phba,
10162 KERN_ERR,
10163 LOG_MBOX | LOG_SLI,
d7c255b2 10164 "0350 rc should have"
6a9c52cf 10165 "been MBX_BUSY\n");
3772a991
JS
10166 if (rc != MBX_NOT_FINISHED)
10167 goto send_current_mbox;
09372820 10168 }
858c9f6c 10169 }
5b75da2f
JS
10170 spin_lock_irqsave(
10171 &phba->pport->work_port_lock,
10172 iflag);
09372820
JS
10173 phba->pport->work_port_events &=
10174 ~WORKER_MBOX_TMO;
5b75da2f
JS
10175 spin_unlock_irqrestore(
10176 &phba->pport->work_port_lock,
10177 iflag);
09372820 10178 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 10179 }
97eab634 10180 } else
5b75da2f 10181 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 10182
92d7f7b0
JS
10183 if ((work_ha_copy & HA_MBATT) &&
10184 (phba->sli.mbox_active == NULL)) {
858c9f6c 10185send_current_mbox:
92d7f7b0 10186 /* Process next mailbox command if there is one */
58da1ffb
JS
10187 do {
10188 rc = lpfc_sli_issue_mbox(phba, NULL,
10189 MBX_NOWAIT);
10190 } while (rc == MBX_NOT_FINISHED);
10191 if (rc != MBX_SUCCESS)
10192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10193 LOG_SLI, "0349 rc should be "
6a9c52cf 10194 "MBX_SUCCESS\n");
92d7f7b0
JS
10195 }
10196
5b75da2f 10197 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 10198 phba->work_ha |= work_ha_copy;
5b75da2f 10199 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 10200 lpfc_worker_wake_up(phba);
dea3101e 10201 }
9399627f 10202 return IRQ_HANDLED;
9940b97b
JS
10203unplug_error:
10204 spin_unlock_irqrestore(&phba->hbalock, iflag);
10205 return IRQ_HANDLED;
dea3101e 10206
3772a991 10207} /* lpfc_sli_sp_intr_handler */
9399627f
JS
10208
10209/**
3772a991 10210 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
10211 * @irq: Interrupt number.
10212 * @dev_id: The device context pointer.
10213 *
10214 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
10215 * service routine when device with SLI-3 interface spec is enabled with
10216 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10217 * ring event in the HBA. However, when the device is enabled with either
10218 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10219 * device-level interrupt handler. When the PCI slot is in error recovery
10220 * or the HBA is undergoing initialization, the interrupt handler will not
10221 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10222 * the intrrupt context. This function is called without any lock held.
10223 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
10224 *
10225 * This function returns IRQ_HANDLED when interrupt is handled else it
10226 * returns IRQ_NONE.
10227 **/
10228irqreturn_t
3772a991 10229lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
10230{
10231 struct lpfc_hba *phba;
10232 uint32_t ha_copy;
10233 unsigned long status;
5b75da2f 10234 unsigned long iflag;
9399627f
JS
10235
10236 /* Get the driver's phba structure from the dev_id and
10237 * assume the HBA is not interrupting.
10238 */
10239 phba = (struct lpfc_hba *) dev_id;
10240
10241 if (unlikely(!phba))
10242 return IRQ_NONE;
10243
10244 /*
10245 * Stuff needs to be attented to when this function is invoked as an
10246 * individual interrupt handler in MSI-X multi-message interrupt mode
10247 */
10248 if (phba->intr_type == MSIX) {
3772a991
JS
10249 /* Check device state for handling interrupt */
10250 if (lpfc_intr_state_check(phba))
9399627f
JS
10251 return IRQ_NONE;
10252 /* Need to read HA REG for FCP ring and other ring events */
9940b97b
JS
10253 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10254 return IRQ_HANDLED;
9399627f 10255 /* Clear up only attention source related to fast-path */
5b75da2f 10256 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
10257 /*
10258 * If there is deferred error attention, do not check for
10259 * any interrupt.
10260 */
10261 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 10262 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
10263 return IRQ_NONE;
10264 }
9399627f
JS
10265 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10266 phba->HAregaddr);
10267 readl(phba->HAregaddr); /* flush */
5b75da2f 10268 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
10269 } else
10270 ha_copy = phba->ha_copy;
dea3101e 10271
10272 /*
9399627f 10273 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 10274 */
9399627f
JS
10275 ha_copy &= ~(phba->work_ha_mask);
10276
10277 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 10278 status >>= (4*LPFC_FCP_RING);
858c9f6c 10279 if (status & HA_RXMASK)
dea3101e 10280 lpfc_sli_handle_fast_ring_event(phba,
10281 &phba->sli.ring[LPFC_FCP_RING],
10282 status);
a4bc3379
JS
10283
10284 if (phba->cfg_multi_ring_support == 2) {
10285 /*
9399627f
JS
10286 * Process all events on extra ring. Take the optimized path
10287 * for extra ring IO.
a4bc3379 10288 */
9399627f 10289 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 10290 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 10291 if (status & HA_RXMASK) {
a4bc3379
JS
10292 lpfc_sli_handle_fast_ring_event(phba,
10293 &phba->sli.ring[LPFC_EXTRA_RING],
10294 status);
10295 }
10296 }
dea3101e 10297 return IRQ_HANDLED;
3772a991 10298} /* lpfc_sli_fp_intr_handler */
9399627f
JS
10299
10300/**
3772a991 10301 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
10302 * @irq: Interrupt number.
10303 * @dev_id: The device context pointer.
10304 *
3772a991
JS
10305 * This function is the HBA device-level interrupt handler to device with
10306 * SLI-3 interface spec, called from the PCI layer when either MSI or
10307 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10308 * requires driver attention. This function invokes the slow-path interrupt
10309 * attention handling function and fast-path interrupt attention handling
10310 * function in turn to process the relevant HBA attention events. This
10311 * function is called without any lock held. It gets the hbalock to access
10312 * and update SLI data structures.
9399627f
JS
10313 *
10314 * This function returns IRQ_HANDLED when interrupt is handled, else it
10315 * returns IRQ_NONE.
10316 **/
10317irqreturn_t
3772a991 10318lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
10319{
10320 struct lpfc_hba *phba;
10321 irqreturn_t sp_irq_rc, fp_irq_rc;
10322 unsigned long status1, status2;
a747c9ce 10323 uint32_t hc_copy;
9399627f
JS
10324
10325 /*
10326 * Get the driver's phba structure from the dev_id and
10327 * assume the HBA is not interrupting.
10328 */
10329 phba = (struct lpfc_hba *) dev_id;
10330
10331 if (unlikely(!phba))
10332 return IRQ_NONE;
10333
3772a991
JS
10334 /* Check device state for handling interrupt */
10335 if (lpfc_intr_state_check(phba))
9399627f
JS
10336 return IRQ_NONE;
10337
10338 spin_lock(&phba->hbalock);
9940b97b
JS
10339 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10340 spin_unlock(&phba->hbalock);
10341 return IRQ_HANDLED;
10342 }
10343
9399627f
JS
10344 if (unlikely(!phba->ha_copy)) {
10345 spin_unlock(&phba->hbalock);
10346 return IRQ_NONE;
10347 } else if (phba->ha_copy & HA_ERATT) {
10348 if (phba->hba_flag & HBA_ERATT_HANDLED)
10349 /* ERATT polling has handled ERATT */
10350 phba->ha_copy &= ~HA_ERATT;
10351 else
10352 /* Indicate interrupt handler handles ERATT */
10353 phba->hba_flag |= HBA_ERATT_HANDLED;
10354 }
10355
a257bf90
JS
10356 /*
10357 * If there is deferred error attention, do not check for any interrupt.
10358 */
10359 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
ec21b3b0 10360 spin_unlock(&phba->hbalock);
a257bf90
JS
10361 return IRQ_NONE;
10362 }
10363
9399627f 10364 /* Clear attention sources except link and error attentions */
9940b97b
JS
10365 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10366 spin_unlock(&phba->hbalock);
10367 return IRQ_HANDLED;
10368 }
a747c9ce
JS
10369 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10370 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10371 phba->HCregaddr);
9399627f 10372 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 10373 writel(hc_copy, phba->HCregaddr);
9399627f
JS
10374 readl(phba->HAregaddr); /* flush */
10375 spin_unlock(&phba->hbalock);
10376
10377 /*
10378 * Invokes slow-path host attention interrupt handling as appropriate.
10379 */
10380
10381 /* status of events with mailbox and link attention */
10382 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10383
10384 /* status of events with ELS ring */
10385 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10386 status2 >>= (4*LPFC_ELS_RING);
10387
10388 if (status1 || (status2 & HA_RXMASK))
3772a991 10389 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
10390 else
10391 sp_irq_rc = IRQ_NONE;
10392
10393 /*
10394 * Invoke fast-path host attention interrupt handling as appropriate.
10395 */
10396
10397 /* status of events with FCP ring */
10398 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10399 status1 >>= (4*LPFC_FCP_RING);
10400
10401 /* status of events with extra ring */
10402 if (phba->cfg_multi_ring_support == 2) {
10403 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10404 status2 >>= (4*LPFC_EXTRA_RING);
10405 } else
10406 status2 = 0;
10407
10408 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 10409 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
10410 else
10411 fp_irq_rc = IRQ_NONE;
dea3101e 10412
9399627f
JS
10413 /* Return device-level interrupt handling status */
10414 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 10415} /* lpfc_sli_intr_handler */
4f774513
JS
10416
10417/**
10418 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10419 * @phba: pointer to lpfc hba data structure.
10420 *
10421 * This routine is invoked by the worker thread to process all the pending
10422 * SLI4 FCP abort XRI events.
10423 **/
10424void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10425{
10426 struct lpfc_cq_event *cq_event;
10427
10428 /* First, declare the fcp xri abort event has been handled */
10429 spin_lock_irq(&phba->hbalock);
10430 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10431 spin_unlock_irq(&phba->hbalock);
10432 /* Now, handle all the fcp xri abort events */
10433 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10434 /* Get the first event from the head of the event queue */
10435 spin_lock_irq(&phba->hbalock);
10436 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10437 cq_event, struct lpfc_cq_event, list);
10438 spin_unlock_irq(&phba->hbalock);
10439 /* Notify aborted XRI for FCP work queue */
10440 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10441 /* Free the event processed back to the free pool */
10442 lpfc_sli4_cq_event_release(phba, cq_event);
10443 }
10444}
10445
10446/**
10447 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10448 * @phba: pointer to lpfc hba data structure.
10449 *
10450 * This routine is invoked by the worker thread to process all the pending
10451 * SLI4 els abort xri events.
10452 **/
10453void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10454{
10455 struct lpfc_cq_event *cq_event;
10456
10457 /* First, declare the els xri abort event has been handled */
10458 spin_lock_irq(&phba->hbalock);
10459 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10460 spin_unlock_irq(&phba->hbalock);
10461 /* Now, handle all the els xri abort events */
10462 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10463 /* Get the first event from the head of the event queue */
10464 spin_lock_irq(&phba->hbalock);
10465 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10466 cq_event, struct lpfc_cq_event, list);
10467 spin_unlock_irq(&phba->hbalock);
10468 /* Notify aborted XRI for ELS work queue */
10469 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10470 /* Free the event processed back to the free pool */
10471 lpfc_sli4_cq_event_release(phba, cq_event);
10472 }
10473}
10474
341af102
JS
10475/**
10476 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10477 * @phba: pointer to lpfc hba data structure
10478 * @pIocbIn: pointer to the rspiocbq
10479 * @pIocbOut: pointer to the cmdiocbq
10480 * @wcqe: pointer to the complete wcqe
10481 *
10482 * This routine transfers the fields of a command iocbq to a response iocbq
10483 * by copying all the IOCB fields from command iocbq and transferring the
10484 * completion status information from the complete wcqe.
10485 **/
4f774513 10486static void
341af102
JS
10487lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
10488 struct lpfc_iocbq *pIocbIn,
4f774513
JS
10489 struct lpfc_iocbq *pIocbOut,
10490 struct lpfc_wcqe_complete *wcqe)
10491{
341af102 10492 unsigned long iflags;
4f774513
JS
10493 size_t offset = offsetof(struct lpfc_iocbq, iocb);
10494
10495 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
10496 sizeof(struct lpfc_iocbq) - offset);
4f774513
JS
10497 /* Map WCQE parameters into irspiocb parameters */
10498 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
10499 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
10500 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
10501 pIocbIn->iocb.un.fcpi.fcpi_parm =
10502 pIocbOut->iocb.un.fcpi.fcpi_parm -
10503 wcqe->total_data_placed;
10504 else
10505 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 10506 else {
4f774513 10507 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
10508 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
10509 }
341af102
JS
10510
10511 /* Pick up HBA exchange busy condition */
10512 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
10513 spin_lock_irqsave(&phba->hbalock, iflags);
10514 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
10515 spin_unlock_irqrestore(&phba->hbalock, iflags);
10516 }
4f774513
JS
10517}
10518
45ed1190
JS
10519/**
10520 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
10521 * @phba: Pointer to HBA context object.
10522 * @wcqe: Pointer to work-queue completion queue entry.
10523 *
10524 * This routine handles an ELS work-queue completion event and construct
10525 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
10526 * discovery engine to handle.
10527 *
10528 * Return: Pointer to the receive IOCBQ, NULL otherwise.
10529 **/
10530static struct lpfc_iocbq *
10531lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
10532 struct lpfc_iocbq *irspiocbq)
10533{
10534 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10535 struct lpfc_iocbq *cmdiocbq;
10536 struct lpfc_wcqe_complete *wcqe;
10537 unsigned long iflags;
10538
10539 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
10540 spin_lock_irqsave(&phba->hbalock, iflags);
10541 pring->stats.iocb_event++;
10542 /* Look up the ELS command IOCB and create pseudo response IOCB */
10543 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
10544 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10545 spin_unlock_irqrestore(&phba->hbalock, iflags);
10546
10547 if (unlikely(!cmdiocbq)) {
10548 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10549 "0386 ELS complete with no corresponding "
10550 "cmdiocb: iotag (%d)\n",
10551 bf_get(lpfc_wcqe_c_request_tag, wcqe));
10552 lpfc_sli_release_iocbq(phba, irspiocbq);
10553 return NULL;
10554 }
10555
10556 /* Fake the irspiocbq and copy necessary response information */
341af102 10557 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
10558
10559 return irspiocbq;
10560}
10561
04c68496
JS
10562/**
10563 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
10564 * @phba: Pointer to HBA context object.
10565 * @cqe: Pointer to mailbox completion queue entry.
10566 *
10567 * This routine process a mailbox completion queue entry with asynchrous
10568 * event.
10569 *
10570 * Return: true if work posted to worker thread, otherwise false.
10571 **/
10572static bool
10573lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10574{
10575 struct lpfc_cq_event *cq_event;
10576 unsigned long iflags;
10577
10578 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10579 "0392 Async Event: word0:x%x, word1:x%x, "
10580 "word2:x%x, word3:x%x\n", mcqe->word0,
10581 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
10582
10583 /* Allocate a new internal CQ_EVENT entry */
10584 cq_event = lpfc_sli4_cq_event_alloc(phba);
10585 if (!cq_event) {
10586 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10587 "0394 Failed to allocate CQ_EVENT entry\n");
10588 return false;
10589 }
10590
10591 /* Move the CQE into an asynchronous event entry */
10592 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
10593 spin_lock_irqsave(&phba->hbalock, iflags);
10594 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
10595 /* Set the async event flag */
10596 phba->hba_flag |= ASYNC_EVENT;
10597 spin_unlock_irqrestore(&phba->hbalock, iflags);
10598
10599 return true;
10600}
10601
10602/**
10603 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
10604 * @phba: Pointer to HBA context object.
10605 * @cqe: Pointer to mailbox completion queue entry.
10606 *
10607 * This routine process a mailbox completion queue entry with mailbox
10608 * completion event.
10609 *
10610 * Return: true if work posted to worker thread, otherwise false.
10611 **/
10612static bool
10613lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
10614{
10615 uint32_t mcqe_status;
10616 MAILBOX_t *mbox, *pmbox;
10617 struct lpfc_mqe *mqe;
10618 struct lpfc_vport *vport;
10619 struct lpfc_nodelist *ndlp;
10620 struct lpfc_dmabuf *mp;
10621 unsigned long iflags;
10622 LPFC_MBOXQ_t *pmb;
10623 bool workposted = false;
10624 int rc;
10625
10626 /* If not a mailbox complete MCQE, out by checking mailbox consume */
10627 if (!bf_get(lpfc_trailer_completed, mcqe))
10628 goto out_no_mqe_complete;
10629
10630 /* Get the reference to the active mbox command */
10631 spin_lock_irqsave(&phba->hbalock, iflags);
10632 pmb = phba->sli.mbox_active;
10633 if (unlikely(!pmb)) {
10634 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10635 "1832 No pending MBOX command to handle\n");
10636 spin_unlock_irqrestore(&phba->hbalock, iflags);
10637 goto out_no_mqe_complete;
10638 }
10639 spin_unlock_irqrestore(&phba->hbalock, iflags);
10640 mqe = &pmb->u.mqe;
10641 pmbox = (MAILBOX_t *)&pmb->u.mqe;
10642 mbox = phba->mbox;
10643 vport = pmb->vport;
10644
10645 /* Reset heartbeat timer */
10646 phba->last_completion_time = jiffies;
10647 del_timer(&phba->sli.mbox_tmo);
10648
10649 /* Move mbox data to caller's mailbox region, do endian swapping */
10650 if (pmb->mbox_cmpl && mbox)
10651 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
04c68496 10652
73d91e50
JS
10653 /*
10654 * For mcqe errors, conditionally move a modified error code to
10655 * the mbox so that the error will not be missed.
10656 */
10657 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
10658 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
10659 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
10660 bf_set(lpfc_mqe_status, mqe,
10661 (LPFC_MBX_ERROR_RANGE | mcqe_status));
10662 }
04c68496
JS
10663 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10664 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10665 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
10666 "MBOX dflt rpi: status:x%x rpi:x%x",
10667 mcqe_status,
10668 pmbox->un.varWords[0], 0);
10669 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
10670 mp = (struct lpfc_dmabuf *)(pmb->context1);
10671 ndlp = (struct lpfc_nodelist *)pmb->context2;
10672 /* Reg_LOGIN of dflt RPI was successful. Now lets get
10673 * RID of the PPI using the same mbox buffer.
10674 */
10675 lpfc_unreg_login(phba, vport->vpi,
10676 pmbox->un.varWords[0], pmb);
10677 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
10678 pmb->context1 = mp;
10679 pmb->context2 = ndlp;
10680 pmb->vport = vport;
10681 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
10682 if (rc != MBX_BUSY)
10683 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10684 LOG_SLI, "0385 rc should "
10685 "have been MBX_BUSY\n");
10686 if (rc != MBX_NOT_FINISHED)
10687 goto send_current_mbox;
10688 }
10689 }
10690 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
10691 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
10692 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
10693
10694 /* There is mailbox completion work to do */
10695 spin_lock_irqsave(&phba->hbalock, iflags);
10696 __lpfc_mbox_cmpl_put(phba, pmb);
10697 phba->work_ha |= HA_MBATT;
10698 spin_unlock_irqrestore(&phba->hbalock, iflags);
10699 workposted = true;
10700
10701send_current_mbox:
10702 spin_lock_irqsave(&phba->hbalock, iflags);
10703 /* Release the mailbox command posting token */
10704 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
10705 /* Setting active mailbox pointer need to be in sync to flag clear */
10706 phba->sli.mbox_active = NULL;
10707 spin_unlock_irqrestore(&phba->hbalock, iflags);
10708 /* Wake up worker thread to post the next pending mailbox command */
10709 lpfc_worker_wake_up(phba);
10710out_no_mqe_complete:
10711 if (bf_get(lpfc_trailer_consumed, mcqe))
10712 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
10713 return workposted;
10714}
10715
10716/**
10717 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
10718 * @phba: Pointer to HBA context object.
10719 * @cqe: Pointer to mailbox completion queue entry.
10720 *
10721 * This routine process a mailbox completion queue entry, it invokes the
10722 * proper mailbox complete handling or asynchrous event handling routine
10723 * according to the MCQE's async bit.
10724 *
10725 * Return: true if work posted to worker thread, otherwise false.
10726 **/
10727static bool
10728lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
10729{
10730 struct lpfc_mcqe mcqe;
10731 bool workposted;
10732
10733 /* Copy the mailbox MCQE and convert endian order as needed */
10734 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
10735
10736 /* Invoke the proper event handling routine */
10737 if (!bf_get(lpfc_trailer_async, &mcqe))
10738 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
10739 else
10740 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
10741 return workposted;
10742}
10743
4f774513
JS
10744/**
10745 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
10746 * @phba: Pointer to HBA context object.
10747 * @wcqe: Pointer to work-queue completion queue entry.
10748 *
10749 * This routine handles an ELS work-queue completion event.
10750 *
10751 * Return: true if work posted to worker thread, otherwise false.
10752 **/
10753static bool
10754lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
10755 struct lpfc_wcqe_complete *wcqe)
10756{
4f774513
JS
10757 struct lpfc_iocbq *irspiocbq;
10758 unsigned long iflags;
2a9bf3d0 10759 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
4f774513 10760
45ed1190 10761 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
10762 irspiocbq = lpfc_sli_get_iocbq(phba);
10763 if (!irspiocbq) {
10764 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2a9bf3d0
JS
10765 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
10766 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
10767 pring->txq_cnt, phba->iocb_cnt,
10768 phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt,
10769 phba->sli.ring[LPFC_ELS_RING].txcmplq_cnt);
45ed1190 10770 return false;
4f774513 10771 }
4f774513 10772
45ed1190
JS
10773 /* Save off the slow-path queue event for work thread to process */
10774 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 10775 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 10776 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
10777 &phba->sli4_hba.sp_queue_event);
10778 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 10779 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 10780
45ed1190 10781 return true;
4f774513
JS
10782}
10783
10784/**
10785 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
10786 * @phba: Pointer to HBA context object.
10787 * @wcqe: Pointer to work-queue completion queue entry.
10788 *
10789 * This routine handles slow-path WQ entry comsumed event by invoking the
10790 * proper WQ release routine to the slow-path WQ.
10791 **/
10792static void
10793lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
10794 struct lpfc_wcqe_release *wcqe)
10795{
10796 /* Check for the slow-path ELS work queue */
10797 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
10798 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
10799 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
10800 else
10801 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
10802 "2579 Slow-path wqe consume event carries "
10803 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
10804 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
10805 phba->sli4_hba.els_wq->queue_id);
10806}
10807
10808/**
10809 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
10810 * @phba: Pointer to HBA context object.
10811 * @cq: Pointer to a WQ completion queue.
10812 * @wcqe: Pointer to work-queue completion queue entry.
10813 *
10814 * This routine handles an XRI abort event.
10815 *
10816 * Return: true if work posted to worker thread, otherwise false.
10817 **/
10818static bool
10819lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
10820 struct lpfc_queue *cq,
10821 struct sli4_wcqe_xri_aborted *wcqe)
10822{
10823 bool workposted = false;
10824 struct lpfc_cq_event *cq_event;
10825 unsigned long iflags;
10826
10827 /* Allocate a new internal CQ_EVENT entry */
10828 cq_event = lpfc_sli4_cq_event_alloc(phba);
10829 if (!cq_event) {
10830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10831 "0602 Failed to allocate CQ_EVENT entry\n");
10832 return false;
10833 }
10834
10835 /* Move the CQE into the proper xri abort event list */
10836 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
10837 switch (cq->subtype) {
10838 case LPFC_FCP:
10839 spin_lock_irqsave(&phba->hbalock, iflags);
10840 list_add_tail(&cq_event->list,
10841 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
10842 /* Set the fcp xri abort event flag */
10843 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
10844 spin_unlock_irqrestore(&phba->hbalock, iflags);
10845 workposted = true;
10846 break;
10847 case LPFC_ELS:
10848 spin_lock_irqsave(&phba->hbalock, iflags);
10849 list_add_tail(&cq_event->list,
10850 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
10851 /* Set the els xri abort event flag */
10852 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
10853 spin_unlock_irqrestore(&phba->hbalock, iflags);
10854 workposted = true;
10855 break;
10856 default:
10857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10858 "0603 Invalid work queue CQE subtype (x%x)\n",
10859 cq->subtype);
10860 workposted = false;
10861 break;
10862 }
10863 return workposted;
10864}
10865
4f774513
JS
10866/**
10867 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
10868 * @phba: Pointer to HBA context object.
10869 * @rcqe: Pointer to receive-queue completion queue entry.
10870 *
10871 * This routine process a receive-queue completion queue entry.
10872 *
10873 * Return: true if work posted to worker thread, otherwise false.
10874 **/
10875static bool
4d9ab994 10876lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 10877{
4f774513
JS
10878 bool workposted = false;
10879 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
10880 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
10881 struct hbq_dmabuf *dma_buf;
7851fe2c 10882 uint32_t status, rq_id;
4f774513
JS
10883 unsigned long iflags;
10884
7851fe2c
JS
10885 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
10886 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
10887 else
10888 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
10889 if (rq_id != hrq->queue_id)
4f774513
JS
10890 goto out;
10891
4d9ab994 10892 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
10893 switch (status) {
10894 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
10895 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10896 "2537 Receive Frame Truncated!!\n");
10897 case FC_STATUS_RQ_SUCCESS:
5ffc266e 10898 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
10899 spin_lock_irqsave(&phba->hbalock, iflags);
10900 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
10901 if (!dma_buf) {
10902 spin_unlock_irqrestore(&phba->hbalock, iflags);
10903 goto out;
10904 }
4d9ab994 10905 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 10906 /* save off the frame for the word thread to process */
4d9ab994 10907 list_add_tail(&dma_buf->cq_event.list,
45ed1190 10908 &phba->sli4_hba.sp_queue_event);
4f774513 10909 /* Frame received */
45ed1190 10910 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
10911 spin_unlock_irqrestore(&phba->hbalock, iflags);
10912 workposted = true;
10913 break;
10914 case FC_STATUS_INSUFF_BUF_NEED_BUF:
10915 case FC_STATUS_INSUFF_BUF_FRM_DISC:
10916 /* Post more buffers if possible */
10917 spin_lock_irqsave(&phba->hbalock, iflags);
10918 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
10919 spin_unlock_irqrestore(&phba->hbalock, iflags);
10920 workposted = true;
10921 break;
10922 }
10923out:
10924 return workposted;
4f774513
JS
10925}
10926
4d9ab994
JS
10927/**
10928 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
10929 * @phba: Pointer to HBA context object.
10930 * @cq: Pointer to the completion queue.
10931 * @wcqe: Pointer to a completion queue entry.
10932 *
25985edc 10933 * This routine process a slow-path work-queue or receive queue completion queue
4d9ab994
JS
10934 * entry.
10935 *
10936 * Return: true if work posted to worker thread, otherwise false.
10937 **/
10938static bool
10939lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
10940 struct lpfc_cqe *cqe)
10941{
45ed1190 10942 struct lpfc_cqe cqevt;
4d9ab994
JS
10943 bool workposted = false;
10944
10945 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 10946 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
10947
10948 /* Check and process for different type of WCQE and dispatch */
45ed1190 10949 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 10950 case CQE_CODE_COMPL_WQE:
45ed1190 10951 /* Process the WQ/RQ complete event */
bc73905a 10952 phba->last_completion_time = jiffies;
4d9ab994 10953 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
45ed1190 10954 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
10955 break;
10956 case CQE_CODE_RELEASE_WQE:
10957 /* Process the WQ release event */
10958 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 10959 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
10960 break;
10961 case CQE_CODE_XRI_ABORTED:
10962 /* Process the WQ XRI abort event */
bc73905a 10963 phba->last_completion_time = jiffies;
4d9ab994 10964 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 10965 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
10966 break;
10967 case CQE_CODE_RECEIVE:
7851fe2c 10968 case CQE_CODE_RECEIVE_V1:
4d9ab994 10969 /* Process the RQ event */
bc73905a 10970 phba->last_completion_time = jiffies;
4d9ab994 10971 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 10972 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
10973 break;
10974 default:
10975 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10976 "0388 Not a valid WCQE code: x%x\n",
45ed1190 10977 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
10978 break;
10979 }
10980 return workposted;
10981}
10982
4f774513
JS
10983/**
10984 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
10985 * @phba: Pointer to HBA context object.
10986 * @eqe: Pointer to fast-path event queue entry.
10987 *
10988 * This routine process a event queue entry from the slow-path event queue.
10989 * It will check the MajorCode and MinorCode to determine this is for a
10990 * completion event on a completion queue, if not, an error shall be logged
10991 * and just return. Otherwise, it will get to the corresponding completion
10992 * queue and process all the entries on that completion queue, rearm the
10993 * completion queue, and then return.
10994 *
10995 **/
10996static void
10997lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
10998{
10999 struct lpfc_queue *cq = NULL, *childq, *speq;
11000 struct lpfc_cqe *cqe;
11001 bool workposted = false;
11002 int ecount = 0;
11003 uint16_t cqid;
11004
cb5172ea 11005 if (bf_get_le32(lpfc_eqe_major_code, eqe) != 0) {
4f774513
JS
11006 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11007 "0359 Not a valid slow-path completion "
11008 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
11009 bf_get_le32(lpfc_eqe_major_code, eqe),
11010 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
11011 return;
11012 }
11013
11014 /* Get the reference to the corresponding CQ */
cb5172ea 11015 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
11016
11017 /* Search for completion queue pointer matching this cqid */
11018 speq = phba->sli4_hba.sp_eq;
11019 list_for_each_entry(childq, &speq->child_list, list) {
11020 if (childq->queue_id == cqid) {
11021 cq = childq;
11022 break;
11023 }
11024 }
11025 if (unlikely(!cq)) {
75baf696
JS
11026 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11027 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11028 "0365 Slow-path CQ identifier "
11029 "(%d) does not exist\n", cqid);
4f774513
JS
11030 return;
11031 }
11032
11033 /* Process all the entries to the CQ */
11034 switch (cq->type) {
11035 case LPFC_MCQ:
11036 while ((cqe = lpfc_sli4_cq_get(cq))) {
11037 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
73d91e50 11038 if (!(++ecount % cq->entry_repost))
4f774513
JS
11039 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11040 }
11041 break;
11042 case LPFC_WCQ:
11043 while ((cqe = lpfc_sli4_cq_get(cq))) {
0558056c
JS
11044 if (cq->subtype == LPFC_FCP)
11045 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11046 cqe);
11047 else
11048 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11049 cqe);
73d91e50 11050 if (!(++ecount % cq->entry_repost))
4f774513
JS
11051 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11052 }
11053 break;
11054 default:
11055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11056 "0370 Invalid completion queue type (%d)\n",
11057 cq->type);
11058 return;
11059 }
11060
11061 /* Catch the no cq entry condition, log an error */
11062 if (unlikely(ecount == 0))
11063 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11064 "0371 No entry from the CQ: identifier "
11065 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11066
11067 /* In any case, flash and re-arm the RCQ */
11068 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11069
11070 /* wake up worker thread if there are works to be done */
11071 if (workposted)
11072 lpfc_worker_wake_up(phba);
11073}
11074
11075/**
11076 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11077 * @eqe: Pointer to fast-path completion queue entry.
11078 *
11079 * This routine process a fast-path work queue completion entry from fast-path
11080 * event queue for FCP command response completion.
11081 **/
11082static void
11083lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
11084 struct lpfc_wcqe_complete *wcqe)
11085{
11086 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
11087 struct lpfc_iocbq *cmdiocbq;
11088 struct lpfc_iocbq irspiocbq;
11089 unsigned long iflags;
11090
11091 spin_lock_irqsave(&phba->hbalock, iflags);
11092 pring->stats.iocb_event++;
11093 spin_unlock_irqrestore(&phba->hbalock, iflags);
11094
11095 /* Check for response status */
11096 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11097 /* If resource errors reported from HBA, reduce queue
11098 * depth of the SCSI device.
11099 */
11100 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
11101 IOSTAT_LOCAL_REJECT) &&
11102 (wcqe->parameter == IOERR_NO_RESOURCES)) {
11103 phba->lpfc_rampdown_queue_depth(phba);
11104 }
11105 /* Log the error status */
11106 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11107 "0373 FCP complete error: status=x%x, "
11108 "hw_status=x%x, total_data_specified=%d, "
11109 "parameter=x%x, word3=x%x\n",
11110 bf_get(lpfc_wcqe_c_status, wcqe),
11111 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11112 wcqe->total_data_placed, wcqe->parameter,
11113 wcqe->word3);
11114 }
11115
11116 /* Look up the FCP command IOCB and create pseudo response IOCB */
11117 spin_lock_irqsave(&phba->hbalock, iflags);
11118 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11119 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11120 spin_unlock_irqrestore(&phba->hbalock, iflags);
11121 if (unlikely(!cmdiocbq)) {
11122 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11123 "0374 FCP complete with no corresponding "
11124 "cmdiocb: iotag (%d)\n",
11125 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11126 return;
11127 }
11128 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11129 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11130 "0375 FCP cmdiocb not callback function "
11131 "iotag: (%d)\n",
11132 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11133 return;
11134 }
11135
11136 /* Fake the irspiocb and copy necessary response information */
341af102 11137 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 11138
0f65ff68
JS
11139 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11140 spin_lock_irqsave(&phba->hbalock, iflags);
11141 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11142 spin_unlock_irqrestore(&phba->hbalock, iflags);
11143 }
11144
4f774513
JS
11145 /* Pass the cmd_iocb and the rsp state to the upper layer */
11146 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11147}
11148
11149/**
11150 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11151 * @phba: Pointer to HBA context object.
11152 * @cq: Pointer to completion queue.
11153 * @wcqe: Pointer to work-queue completion queue entry.
11154 *
11155 * This routine handles an fast-path WQ entry comsumed event by invoking the
11156 * proper WQ release routine to the slow-path WQ.
11157 **/
11158static void
11159lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11160 struct lpfc_wcqe_release *wcqe)
11161{
11162 struct lpfc_queue *childwq;
11163 bool wqid_matched = false;
11164 uint16_t fcp_wqid;
11165
11166 /* Check for fast-path FCP work queue release */
11167 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11168 list_for_each_entry(childwq, &cq->child_list, list) {
11169 if (childwq->queue_id == fcp_wqid) {
11170 lpfc_sli4_wq_release(childwq,
11171 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11172 wqid_matched = true;
11173 break;
11174 }
11175 }
11176 /* Report warning log message if no match found */
11177 if (wqid_matched != true)
11178 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11179 "2580 Fast-path wqe consume event carries "
11180 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11181}
11182
11183/**
11184 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11185 * @cq: Pointer to the completion queue.
11186 * @eqe: Pointer to fast-path completion queue entry.
11187 *
11188 * This routine process a fast-path work queue completion entry from fast-path
11189 * event queue for FCP command response completion.
11190 **/
11191static int
11192lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11193 struct lpfc_cqe *cqe)
11194{
11195 struct lpfc_wcqe_release wcqe;
11196 bool workposted = false;
11197
11198 /* Copy the work queue CQE and convert endian order if needed */
11199 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11200
11201 /* Check and process for different type of WCQE and dispatch */
11202 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11203 case CQE_CODE_COMPL_WQE:
11204 /* Process the WQ complete event */
98fc5dd9 11205 phba->last_completion_time = jiffies;
4f774513
JS
11206 lpfc_sli4_fp_handle_fcp_wcqe(phba,
11207 (struct lpfc_wcqe_complete *)&wcqe);
11208 break;
11209 case CQE_CODE_RELEASE_WQE:
11210 /* Process the WQ release event */
11211 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11212 (struct lpfc_wcqe_release *)&wcqe);
11213 break;
11214 case CQE_CODE_XRI_ABORTED:
11215 /* Process the WQ XRI abort event */
bc73905a 11216 phba->last_completion_time = jiffies;
4f774513
JS
11217 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11218 (struct sli4_wcqe_xri_aborted *)&wcqe);
11219 break;
11220 default:
11221 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11222 "0144 Not a valid WCQE code: x%x\n",
11223 bf_get(lpfc_wcqe_c_code, &wcqe));
11224 break;
11225 }
11226 return workposted;
11227}
11228
11229/**
11230 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
11231 * @phba: Pointer to HBA context object.
11232 * @eqe: Pointer to fast-path event queue entry.
11233 *
11234 * This routine process a event queue entry from the fast-path event queue.
11235 * It will check the MajorCode and MinorCode to determine this is for a
11236 * completion event on a completion queue, if not, an error shall be logged
11237 * and just return. Otherwise, it will get to the corresponding completion
11238 * queue and process all the entries on the completion queue, rearm the
11239 * completion queue, and then return.
11240 **/
11241static void
11242lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11243 uint32_t fcp_cqidx)
11244{
11245 struct lpfc_queue *cq;
11246 struct lpfc_cqe *cqe;
11247 bool workposted = false;
11248 uint16_t cqid;
11249 int ecount = 0;
11250
cb5172ea 11251 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
4f774513
JS
11252 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11253 "0366 Not a valid fast-path completion "
11254 "event: majorcode=x%x, minorcode=x%x\n",
cb5172ea
JS
11255 bf_get_le32(lpfc_eqe_major_code, eqe),
11256 bf_get_le32(lpfc_eqe_minor_code, eqe));
4f774513
JS
11257 return;
11258 }
11259
11260 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
11261 if (unlikely(!cq)) {
75baf696
JS
11262 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11263 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11264 "0367 Fast-path completion queue "
11265 "does not exist\n");
4f774513
JS
11266 return;
11267 }
11268
11269 /* Get the reference to the corresponding CQ */
cb5172ea 11270 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
4f774513
JS
11271 if (unlikely(cqid != cq->queue_id)) {
11272 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11273 "0368 Miss-matched fast-path completion "
11274 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11275 cqid, cq->queue_id);
11276 return;
11277 }
11278
11279 /* Process all the entries to the CQ */
11280 while ((cqe = lpfc_sli4_cq_get(cq))) {
11281 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
73d91e50 11282 if (!(++ecount % cq->entry_repost))
4f774513
JS
11283 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11284 }
11285
11286 /* Catch the no cq entry condition */
11287 if (unlikely(ecount == 0))
11288 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11289 "0369 No entry from fast-path completion "
11290 "queue fcpcqid=%d\n", cq->queue_id);
11291
11292 /* In any case, flash and re-arm the CQ */
11293 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11294
11295 /* wake up worker thread if there are works to be done */
11296 if (workposted)
11297 lpfc_worker_wake_up(phba);
11298}
11299
11300static void
11301lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11302{
11303 struct lpfc_eqe *eqe;
11304
11305 /* walk all the EQ entries and drop on the floor */
11306 while ((eqe = lpfc_sli4_eq_get(eq)))
11307 ;
11308
11309 /* Clear and re-arm the EQ */
11310 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11311}
11312
11313/**
11314 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
11315 * @irq: Interrupt number.
11316 * @dev_id: The device context pointer.
11317 *
11318 * This function is directly called from the PCI layer as an interrupt
11319 * service routine when device with SLI-4 interface spec is enabled with
11320 * MSI-X multi-message interrupt mode and there are slow-path events in
11321 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11322 * interrupt mode, this function is called as part of the device-level
11323 * interrupt handler. When the PCI slot is in error recovery or the HBA is
11324 * undergoing initialization, the interrupt handler will not process the
11325 * interrupt. The link attention and ELS ring attention events are handled
11326 * by the worker thread. The interrupt handler signals the worker thread
11327 * and returns for these events. This function is called without any lock
11328 * held. It gets the hbalock to access and update SLI data structures.
11329 *
11330 * This function returns IRQ_HANDLED when interrupt is handled else it
11331 * returns IRQ_NONE.
11332 **/
11333irqreturn_t
11334lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
11335{
11336 struct lpfc_hba *phba;
11337 struct lpfc_queue *speq;
11338 struct lpfc_eqe *eqe;
11339 unsigned long iflag;
11340 int ecount = 0;
11341
11342 /*
11343 * Get the driver's phba structure from the dev_id
11344 */
11345 phba = (struct lpfc_hba *)dev_id;
11346
11347 if (unlikely(!phba))
11348 return IRQ_NONE;
11349
11350 /* Get to the EQ struct associated with this vector */
11351 speq = phba->sli4_hba.sp_eq;
5350d872
JS
11352 if (unlikely(!speq))
11353 return IRQ_NONE;
4f774513
JS
11354
11355 /* Check device state for handling interrupt */
11356 if (unlikely(lpfc_intr_state_check(phba))) {
11357 /* Check again for link_state with lock held */
11358 spin_lock_irqsave(&phba->hbalock, iflag);
11359 if (phba->link_state < LPFC_LINK_DOWN)
11360 /* Flush, clear interrupt, and rearm the EQ */
11361 lpfc_sli4_eq_flush(phba, speq);
11362 spin_unlock_irqrestore(&phba->hbalock, iflag);
11363 return IRQ_NONE;
11364 }
11365
11366 /*
11367 * Process all the event on FCP slow-path EQ
11368 */
11369 while ((eqe = lpfc_sli4_eq_get(speq))) {
11370 lpfc_sli4_sp_handle_eqe(phba, eqe);
73d91e50 11371 if (!(++ecount % speq->entry_repost))
4f774513
JS
11372 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
11373 }
11374
11375 /* Always clear and re-arm the slow-path EQ */
11376 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
11377
11378 /* Catch the no cq entry condition */
11379 if (unlikely(ecount == 0)) {
11380 if (phba->intr_type == MSIX)
11381 /* MSI-X treated interrupt served as no EQ share INT */
11382 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11383 "0357 MSI-X interrupt with no EQE\n");
11384 else
11385 /* Non MSI-X treated on interrupt as EQ share INT */
11386 return IRQ_NONE;
11387 }
11388
11389 return IRQ_HANDLED;
11390} /* lpfc_sli4_sp_intr_handler */
11391
11392/**
11393 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
11394 * @irq: Interrupt number.
11395 * @dev_id: The device context pointer.
11396 *
11397 * This function is directly called from the PCI layer as an interrupt
11398 * service routine when device with SLI-4 interface spec is enabled with
11399 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11400 * ring event in the HBA. However, when the device is enabled with either
11401 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11402 * device-level interrupt handler. When the PCI slot is in error recovery
11403 * or the HBA is undergoing initialization, the interrupt handler will not
11404 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11405 * the intrrupt context. This function is called without any lock held.
11406 * It gets the hbalock to access and update SLI data structures. Note that,
11407 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11408 * equal to that of FCP CQ index.
11409 *
11410 * This function returns IRQ_HANDLED when interrupt is handled else it
11411 * returns IRQ_NONE.
11412 **/
11413irqreturn_t
11414lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
11415{
11416 struct lpfc_hba *phba;
11417 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11418 struct lpfc_queue *fpeq;
11419 struct lpfc_eqe *eqe;
11420 unsigned long iflag;
11421 int ecount = 0;
11422 uint32_t fcp_eqidx;
11423
11424 /* Get the driver's phba structure from the dev_id */
11425 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11426 phba = fcp_eq_hdl->phba;
11427 fcp_eqidx = fcp_eq_hdl->idx;
11428
11429 if (unlikely(!phba))
11430 return IRQ_NONE;
5350d872
JS
11431 if (unlikely(!phba->sli4_hba.fp_eq))
11432 return IRQ_NONE;
4f774513
JS
11433
11434 /* Get to the EQ struct associated with this vector */
11435 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
11436
11437 /* Check device state for handling interrupt */
11438 if (unlikely(lpfc_intr_state_check(phba))) {
11439 /* Check again for link_state with lock held */
11440 spin_lock_irqsave(&phba->hbalock, iflag);
11441 if (phba->link_state < LPFC_LINK_DOWN)
11442 /* Flush, clear interrupt, and rearm the EQ */
11443 lpfc_sli4_eq_flush(phba, fpeq);
11444 spin_unlock_irqrestore(&phba->hbalock, iflag);
11445 return IRQ_NONE;
11446 }
11447
11448 /*
11449 * Process all the event on FCP fast-path EQ
11450 */
11451 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11452 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
73d91e50 11453 if (!(++ecount % fpeq->entry_repost))
4f774513
JS
11454 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11455 }
11456
11457 /* Always clear and re-arm the fast-path EQ */
11458 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
11459
11460 if (unlikely(ecount == 0)) {
11461 if (phba->intr_type == MSIX)
11462 /* MSI-X treated interrupt served as no EQ share INT */
11463 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11464 "0358 MSI-X interrupt with no EQE\n");
11465 else
11466 /* Non MSI-X treated on interrupt as EQ share INT */
11467 return IRQ_NONE;
11468 }
11469
11470 return IRQ_HANDLED;
11471} /* lpfc_sli4_fp_intr_handler */
11472
11473/**
11474 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
11475 * @irq: Interrupt number.
11476 * @dev_id: The device context pointer.
11477 *
11478 * This function is the device-level interrupt handler to device with SLI-4
11479 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
11480 * interrupt mode is enabled and there is an event in the HBA which requires
11481 * driver attention. This function invokes the slow-path interrupt attention
11482 * handling function and fast-path interrupt attention handling function in
11483 * turn to process the relevant HBA attention events. This function is called
11484 * without any lock held. It gets the hbalock to access and update SLI data
11485 * structures.
11486 *
11487 * This function returns IRQ_HANDLED when interrupt is handled, else it
11488 * returns IRQ_NONE.
11489 **/
11490irqreturn_t
11491lpfc_sli4_intr_handler(int irq, void *dev_id)
11492{
11493 struct lpfc_hba *phba;
11494 irqreturn_t sp_irq_rc, fp_irq_rc;
11495 bool fp_handled = false;
11496 uint32_t fcp_eqidx;
11497
11498 /* Get the driver's phba structure from the dev_id */
11499 phba = (struct lpfc_hba *)dev_id;
11500
11501 if (unlikely(!phba))
11502 return IRQ_NONE;
11503
11504 /*
11505 * Invokes slow-path host attention interrupt handling as appropriate.
11506 */
11507 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
11508
11509 /*
11510 * Invoke fast-path host attention interrupt handling as appropriate.
11511 */
11512 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
11513 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
11514 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
11515 if (fp_irq_rc == IRQ_HANDLED)
11516 fp_handled |= true;
11517 }
11518
11519 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
11520} /* lpfc_sli4_intr_handler */
11521
11522/**
11523 * lpfc_sli4_queue_free - free a queue structure and associated memory
11524 * @queue: The queue structure to free.
11525 *
b595076a 11526 * This function frees a queue structure and the DMAable memory used for
4f774513
JS
11527 * the host resident queue. This function must be called after destroying the
11528 * queue on the HBA.
11529 **/
11530void
11531lpfc_sli4_queue_free(struct lpfc_queue *queue)
11532{
11533 struct lpfc_dmabuf *dmabuf;
11534
11535 if (!queue)
11536 return;
11537
11538 while (!list_empty(&queue->page_list)) {
11539 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
11540 list);
49198b37 11541 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
4f774513
JS
11542 dmabuf->virt, dmabuf->phys);
11543 kfree(dmabuf);
11544 }
11545 kfree(queue);
11546 return;
11547}
11548
11549/**
11550 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
11551 * @phba: The HBA that this queue is being created on.
11552 * @entry_size: The size of each queue entry for this queue.
11553 * @entry count: The number of entries that this queue will handle.
11554 *
11555 * This function allocates a queue structure and the DMAable memory used for
11556 * the host resident queue. This function must be called before creating the
11557 * queue on the HBA.
11558 **/
11559struct lpfc_queue *
11560lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
11561 uint32_t entry_count)
11562{
11563 struct lpfc_queue *queue;
11564 struct lpfc_dmabuf *dmabuf;
11565 int x, total_qe_count;
11566 void *dma_pointer;
cb5172ea 11567 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
4f774513 11568
cb5172ea
JS
11569 if (!phba->sli4_hba.pc_sli4_params.supported)
11570 hw_page_size = SLI4_PAGE_SIZE;
11571
4f774513
JS
11572 queue = kzalloc(sizeof(struct lpfc_queue) +
11573 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
11574 if (!queue)
11575 return NULL;
cb5172ea
JS
11576 queue->page_count = (ALIGN(entry_size * entry_count,
11577 hw_page_size))/hw_page_size;
4f774513
JS
11578 INIT_LIST_HEAD(&queue->list);
11579 INIT_LIST_HEAD(&queue->page_list);
11580 INIT_LIST_HEAD(&queue->child_list);
11581 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
11582 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
11583 if (!dmabuf)
11584 goto out_fail;
11585 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
cb5172ea 11586 hw_page_size, &dmabuf->phys,
4f774513
JS
11587 GFP_KERNEL);
11588 if (!dmabuf->virt) {
11589 kfree(dmabuf);
11590 goto out_fail;
11591 }
cb5172ea 11592 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
11593 dmabuf->buffer_tag = x;
11594 list_add_tail(&dmabuf->list, &queue->page_list);
11595 /* initialize queue's entry array */
11596 dma_pointer = dmabuf->virt;
11597 for (; total_qe_count < entry_count &&
cb5172ea 11598 dma_pointer < (hw_page_size + dmabuf->virt);
4f774513
JS
11599 total_qe_count++, dma_pointer += entry_size) {
11600 queue->qe[total_qe_count].address = dma_pointer;
11601 }
11602 }
11603 queue->entry_size = entry_size;
11604 queue->entry_count = entry_count;
73d91e50
JS
11605
11606 /*
11607 * entry_repost is calculated based on the number of entries in the
11608 * queue. This works out except for RQs. If buffers are NOT initially
11609 * posted for every RQE, entry_repost should be adjusted accordingly.
11610 */
11611 queue->entry_repost = (entry_count >> 3);
11612 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
11613 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
4f774513
JS
11614 queue->phba = phba;
11615
11616 return queue;
11617out_fail:
11618 lpfc_sli4_queue_free(queue);
11619 return NULL;
11620}
11621
11622/**
11623 * lpfc_eq_create - Create an Event Queue on the HBA
11624 * @phba: HBA structure that indicates port to create a queue on.
11625 * @eq: The queue structure to use to create the event queue.
11626 * @imax: The maximum interrupt per second limit.
11627 *
11628 * This function creates an event queue, as detailed in @eq, on a port,
11629 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
11630 *
11631 * The @phba struct is used to send mailbox command to HBA. The @eq struct
11632 * is used to get the entry count and entry size that are necessary to
11633 * determine the number of pages to allocate and use for this queue. This
11634 * function will send the EQ_CREATE mailbox command to the HBA to setup the
11635 * event queue. This function is asynchronous and will wait for the mailbox
11636 * command to finish before continuing.
11637 *
11638 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11639 * memory this function will return -ENOMEM. If the queue create mailbox command
11640 * fails this function will return -ENXIO.
4f774513
JS
11641 **/
11642uint32_t
11643lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
11644{
11645 struct lpfc_mbx_eq_create *eq_create;
11646 LPFC_MBOXQ_t *mbox;
11647 int rc, length, status = 0;
11648 struct lpfc_dmabuf *dmabuf;
11649 uint32_t shdr_status, shdr_add_status;
11650 union lpfc_sli4_cfg_shdr *shdr;
11651 uint16_t dmult;
49198b37
JS
11652 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11653
11654 if (!phba->sli4_hba.pc_sli4_params.supported)
11655 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
11656
11657 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11658 if (!mbox)
11659 return -ENOMEM;
11660 length = (sizeof(struct lpfc_mbx_eq_create) -
11661 sizeof(struct lpfc_sli4_cfg_mhdr));
11662 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11663 LPFC_MBOX_OPCODE_EQ_CREATE,
11664 length, LPFC_SLI4_MBX_EMBED);
11665 eq_create = &mbox->u.mqe.un.eq_create;
11666 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
11667 eq->page_count);
11668 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
11669 LPFC_EQE_SIZE);
11670 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
11671 /* Calculate delay multiper from maximum interrupt per second */
11672 dmult = LPFC_DMULT_CONST/imax - 1;
11673 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
11674 dmult);
11675 switch (eq->entry_count) {
11676 default:
11677 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11678 "0360 Unsupported EQ count. (%d)\n",
11679 eq->entry_count);
11680 if (eq->entry_count < 256)
11681 return -EINVAL;
11682 /* otherwise default to smallest count (drop through) */
11683 case 256:
11684 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11685 LPFC_EQ_CNT_256);
11686 break;
11687 case 512:
11688 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11689 LPFC_EQ_CNT_512);
11690 break;
11691 case 1024:
11692 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11693 LPFC_EQ_CNT_1024);
11694 break;
11695 case 2048:
11696 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11697 LPFC_EQ_CNT_2048);
11698 break;
11699 case 4096:
11700 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
11701 LPFC_EQ_CNT_4096);
11702 break;
11703 }
11704 list_for_each_entry(dmabuf, &eq->page_list, list) {
49198b37 11705 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
11706 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11707 putPaddrLow(dmabuf->phys);
11708 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11709 putPaddrHigh(dmabuf->phys);
11710 }
11711 mbox->vport = phba->pport;
11712 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
11713 mbox->context1 = NULL;
11714 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11715 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
11716 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11717 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11718 if (shdr_status || shdr_add_status || rc) {
11719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11720 "2500 EQ_CREATE mailbox failed with "
11721 "status x%x add_status x%x, mbx status x%x\n",
11722 shdr_status, shdr_add_status, rc);
11723 status = -ENXIO;
11724 }
11725 eq->type = LPFC_EQ;
11726 eq->subtype = LPFC_NONE;
11727 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
11728 if (eq->queue_id == 0xFFFF)
11729 status = -ENXIO;
11730 eq->host_index = 0;
11731 eq->hba_index = 0;
11732
8fa38513 11733 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
11734 return status;
11735}
11736
11737/**
11738 * lpfc_cq_create - Create a Completion Queue on the HBA
11739 * @phba: HBA structure that indicates port to create a queue on.
11740 * @cq: The queue structure to use to create the completion queue.
11741 * @eq: The event queue to bind this completion queue to.
11742 *
11743 * This function creates a completion queue, as detailed in @wq, on a port,
11744 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
11745 *
11746 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11747 * is used to get the entry count and entry size that are necessary to
11748 * determine the number of pages to allocate and use for this queue. The @eq
11749 * is used to indicate which event queue to bind this completion queue to. This
11750 * function will send the CQ_CREATE mailbox command to the HBA to setup the
11751 * completion queue. This function is asynchronous and will wait for the mailbox
11752 * command to finish before continuing.
11753 *
11754 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11755 * memory this function will return -ENOMEM. If the queue create mailbox command
11756 * fails this function will return -ENXIO.
4f774513
JS
11757 **/
11758uint32_t
11759lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
11760 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
11761{
11762 struct lpfc_mbx_cq_create *cq_create;
11763 struct lpfc_dmabuf *dmabuf;
11764 LPFC_MBOXQ_t *mbox;
11765 int rc, length, status = 0;
11766 uint32_t shdr_status, shdr_add_status;
11767 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
11768 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
11769
11770 if (!phba->sli4_hba.pc_sli4_params.supported)
11771 hw_page_size = SLI4_PAGE_SIZE;
11772
4f774513
JS
11773 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11774 if (!mbox)
11775 return -ENOMEM;
11776 length = (sizeof(struct lpfc_mbx_cq_create) -
11777 sizeof(struct lpfc_sli4_cfg_mhdr));
11778 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11779 LPFC_MBOX_OPCODE_CQ_CREATE,
11780 length, LPFC_SLI4_MBX_EMBED);
11781 cq_create = &mbox->u.mqe.un.cq_create;
5a6f133e 11782 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
4f774513
JS
11783 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
11784 cq->page_count);
11785 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
11786 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
5a6f133e
JS
11787 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11788 phba->sli4_hba.pc_sli4_params.cqv);
11789 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
c31098ce
JS
11790 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
11791 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
5a6f133e
JS
11792 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
11793 eq->queue_id);
11794 } else {
11795 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
11796 eq->queue_id);
11797 }
4f774513
JS
11798 switch (cq->entry_count) {
11799 default:
11800 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11801 "0361 Unsupported CQ count. (%d)\n",
11802 cq->entry_count);
11803 if (cq->entry_count < 256)
11804 return -EINVAL;
11805 /* otherwise default to smallest count (drop through) */
11806 case 256:
11807 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11808 LPFC_CQ_CNT_256);
11809 break;
11810 case 512:
11811 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11812 LPFC_CQ_CNT_512);
11813 break;
11814 case 1024:
11815 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
11816 LPFC_CQ_CNT_1024);
11817 break;
11818 }
11819 list_for_each_entry(dmabuf, &cq->page_list, list) {
49198b37 11820 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
11821 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11822 putPaddrLow(dmabuf->phys);
11823 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11824 putPaddrHigh(dmabuf->phys);
11825 }
11826 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
11827
11828 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
11829 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11830 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11831 if (shdr_status || shdr_add_status || rc) {
11832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11833 "2501 CQ_CREATE mailbox failed with "
11834 "status x%x add_status x%x, mbx status x%x\n",
11835 shdr_status, shdr_add_status, rc);
11836 status = -ENXIO;
11837 goto out;
11838 }
11839 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
11840 if (cq->queue_id == 0xFFFF) {
11841 status = -ENXIO;
11842 goto out;
11843 }
11844 /* link the cq onto the parent eq child list */
11845 list_add_tail(&cq->list, &eq->child_list);
11846 /* Set up completion queue's type and subtype */
11847 cq->type = type;
11848 cq->subtype = subtype;
11849 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
2a622bfb 11850 cq->assoc_qid = eq->queue_id;
4f774513
JS
11851 cq->host_index = 0;
11852 cq->hba_index = 0;
4f774513 11853
8fa38513
JS
11854out:
11855 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
11856 return status;
11857}
11858
b19a061a
JS
11859/**
11860 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
11861 * @phba: HBA structure that indicates port to create a queue on.
11862 * @mq: The queue structure to use to create the mailbox queue.
11863 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
11864 * @cq: The completion queue to associate with this cq.
11865 *
11866 * This function provides failback (fb) functionality when the
11867 * mq_create_ext fails on older FW generations. It's purpose is identical
11868 * to mq_create_ext otherwise.
11869 *
11870 * This routine cannot fail as all attributes were previously accessed and
11871 * initialized in mq_create_ext.
11872 **/
11873static void
11874lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
11875 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
11876{
11877 struct lpfc_mbx_mq_create *mq_create;
11878 struct lpfc_dmabuf *dmabuf;
11879 int length;
11880
11881 length = (sizeof(struct lpfc_mbx_mq_create) -
11882 sizeof(struct lpfc_sli4_cfg_mhdr));
11883 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
11884 LPFC_MBOX_OPCODE_MQ_CREATE,
11885 length, LPFC_SLI4_MBX_EMBED);
11886 mq_create = &mbox->u.mqe.un.mq_create;
11887 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
11888 mq->page_count);
11889 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
11890 cq->queue_id);
11891 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
11892 switch (mq->entry_count) {
11893 case 16:
5a6f133e
JS
11894 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11895 LPFC_MQ_RING_SIZE_16);
b19a061a
JS
11896 break;
11897 case 32:
5a6f133e
JS
11898 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11899 LPFC_MQ_RING_SIZE_32);
b19a061a
JS
11900 break;
11901 case 64:
5a6f133e
JS
11902 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11903 LPFC_MQ_RING_SIZE_64);
b19a061a
JS
11904 break;
11905 case 128:
5a6f133e
JS
11906 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
11907 LPFC_MQ_RING_SIZE_128);
b19a061a
JS
11908 break;
11909 }
11910 list_for_each_entry(dmabuf, &mq->page_list, list) {
11911 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
11912 putPaddrLow(dmabuf->phys);
11913 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
11914 putPaddrHigh(dmabuf->phys);
11915 }
11916}
11917
04c68496
JS
11918/**
11919 * lpfc_mq_create - Create a mailbox Queue on the HBA
11920 * @phba: HBA structure that indicates port to create a queue on.
11921 * @mq: The queue structure to use to create the mailbox queue.
b19a061a
JS
11922 * @cq: The completion queue to associate with this cq.
11923 * @subtype: The queue's subtype.
04c68496
JS
11924 *
11925 * This function creates a mailbox queue, as detailed in @mq, on a port,
11926 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
11927 *
11928 * The @phba struct is used to send mailbox command to HBA. The @cq struct
11929 * is used to get the entry count and entry size that are necessary to
11930 * determine the number of pages to allocate and use for this queue. This
11931 * function will send the MQ_CREATE mailbox command to the HBA to setup the
11932 * mailbox queue. This function is asynchronous and will wait for the mailbox
11933 * command to finish before continuing.
11934 *
11935 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
11936 * memory this function will return -ENOMEM. If the queue create mailbox command
11937 * fails this function will return -ENXIO.
04c68496 11938 **/
b19a061a 11939int32_t
04c68496
JS
11940lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
11941 struct lpfc_queue *cq, uint32_t subtype)
11942{
11943 struct lpfc_mbx_mq_create *mq_create;
b19a061a 11944 struct lpfc_mbx_mq_create_ext *mq_create_ext;
04c68496
JS
11945 struct lpfc_dmabuf *dmabuf;
11946 LPFC_MBOXQ_t *mbox;
11947 int rc, length, status = 0;
11948 uint32_t shdr_status, shdr_add_status;
11949 union lpfc_sli4_cfg_shdr *shdr;
49198b37 11950 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
04c68496 11951
49198b37
JS
11952 if (!phba->sli4_hba.pc_sli4_params.supported)
11953 hw_page_size = SLI4_PAGE_SIZE;
b19a061a 11954
04c68496
JS
11955 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11956 if (!mbox)
11957 return -ENOMEM;
b19a061a 11958 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
04c68496
JS
11959 sizeof(struct lpfc_sli4_cfg_mhdr));
11960 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
b19a061a 11961 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
04c68496 11962 length, LPFC_SLI4_MBX_EMBED);
b19a061a
JS
11963
11964 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
5a6f133e 11965 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
70f3c073
JS
11966 bf_set(lpfc_mbx_mq_create_ext_num_pages,
11967 &mq_create_ext->u.request, mq->page_count);
11968 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
11969 &mq_create_ext->u.request, 1);
11970 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
b19a061a
JS
11971 &mq_create_ext->u.request, 1);
11972 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
11973 &mq_create_ext->u.request, 1);
70f3c073
JS
11974 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
11975 &mq_create_ext->u.request, 1);
11976 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
11977 &mq_create_ext->u.request, 1);
b19a061a 11978 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
5a6f133e
JS
11979 bf_set(lpfc_mbox_hdr_version, &shdr->request,
11980 phba->sli4_hba.pc_sli4_params.mqv);
11981 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
11982 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
11983 cq->queue_id);
11984 else
11985 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
11986 cq->queue_id);
04c68496
JS
11987 switch (mq->entry_count) {
11988 default:
11989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11990 "0362 Unsupported MQ count. (%d)\n",
11991 mq->entry_count);
11992 if (mq->entry_count < 16)
11993 return -EINVAL;
11994 /* otherwise default to smallest count (drop through) */
11995 case 16:
5a6f133e
JS
11996 bf_set(lpfc_mq_context_ring_size,
11997 &mq_create_ext->u.request.context,
11998 LPFC_MQ_RING_SIZE_16);
04c68496
JS
11999 break;
12000 case 32:
5a6f133e
JS
12001 bf_set(lpfc_mq_context_ring_size,
12002 &mq_create_ext->u.request.context,
12003 LPFC_MQ_RING_SIZE_32);
04c68496
JS
12004 break;
12005 case 64:
5a6f133e
JS
12006 bf_set(lpfc_mq_context_ring_size,
12007 &mq_create_ext->u.request.context,
12008 LPFC_MQ_RING_SIZE_64);
04c68496
JS
12009 break;
12010 case 128:
5a6f133e
JS
12011 bf_set(lpfc_mq_context_ring_size,
12012 &mq_create_ext->u.request.context,
12013 LPFC_MQ_RING_SIZE_128);
04c68496
JS
12014 break;
12015 }
12016 list_for_each_entry(dmabuf, &mq->page_list, list) {
49198b37 12017 memset(dmabuf->virt, 0, hw_page_size);
b19a061a 12018 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
04c68496 12019 putPaddrLow(dmabuf->phys);
b19a061a 12020 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
04c68496
JS
12021 putPaddrHigh(dmabuf->phys);
12022 }
12023 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
b19a061a
JS
12024 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12025 &mq_create_ext->u.response);
12026 if (rc != MBX_SUCCESS) {
12027 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12028 "2795 MQ_CREATE_EXT failed with "
12029 "status x%x. Failback to MQ_CREATE.\n",
12030 rc);
12031 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12032 mq_create = &mbox->u.mqe.un.mq_create;
12033 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12034 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12035 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12036 &mq_create->u.response);
12037 }
12038
04c68496 12039 /* The IOCTL status is embedded in the mailbox subheader. */
04c68496
JS
12040 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12041 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12042 if (shdr_status || shdr_add_status || rc) {
12043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12044 "2502 MQ_CREATE mailbox failed with "
12045 "status x%x add_status x%x, mbx status x%x\n",
12046 shdr_status, shdr_add_status, rc);
12047 status = -ENXIO;
12048 goto out;
12049 }
04c68496
JS
12050 if (mq->queue_id == 0xFFFF) {
12051 status = -ENXIO;
12052 goto out;
12053 }
12054 mq->type = LPFC_MQ;
2a622bfb 12055 mq->assoc_qid = cq->queue_id;
04c68496
JS
12056 mq->subtype = subtype;
12057 mq->host_index = 0;
12058 mq->hba_index = 0;
12059
12060 /* link the mq onto the parent cq child list */
12061 list_add_tail(&mq->list, &cq->child_list);
12062out:
8fa38513 12063 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
12064 return status;
12065}
12066
4f774513
JS
12067/**
12068 * lpfc_wq_create - Create a Work Queue on the HBA
12069 * @phba: HBA structure that indicates port to create a queue on.
12070 * @wq: The queue structure to use to create the work queue.
12071 * @cq: The completion queue to bind this work queue to.
12072 * @subtype: The subtype of the work queue indicating its functionality.
12073 *
12074 * This function creates a work queue, as detailed in @wq, on a port, described
12075 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12076 *
12077 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12078 * is used to get the entry count and entry size that are necessary to
12079 * determine the number of pages to allocate and use for this queue. The @cq
12080 * is used to indicate which completion queue to bind this work queue to. This
12081 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12082 * work queue. This function is asynchronous and will wait for the mailbox
12083 * command to finish before continuing.
12084 *
12085 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12086 * memory this function will return -ENOMEM. If the queue create mailbox command
12087 * fails this function will return -ENXIO.
4f774513
JS
12088 **/
12089uint32_t
12090lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12091 struct lpfc_queue *cq, uint32_t subtype)
12092{
12093 struct lpfc_mbx_wq_create *wq_create;
12094 struct lpfc_dmabuf *dmabuf;
12095 LPFC_MBOXQ_t *mbox;
12096 int rc, length, status = 0;
12097 uint32_t shdr_status, shdr_add_status;
12098 union lpfc_sli4_cfg_shdr *shdr;
49198b37 12099 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
5a6f133e 12100 struct dma_address *page;
49198b37
JS
12101
12102 if (!phba->sli4_hba.pc_sli4_params.supported)
12103 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12104
12105 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12106 if (!mbox)
12107 return -ENOMEM;
12108 length = (sizeof(struct lpfc_mbx_wq_create) -
12109 sizeof(struct lpfc_sli4_cfg_mhdr));
12110 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12111 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12112 length, LPFC_SLI4_MBX_EMBED);
12113 wq_create = &mbox->u.mqe.un.wq_create;
5a6f133e 12114 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
4f774513
JS
12115 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12116 wq->page_count);
12117 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12118 cq->queue_id);
5a6f133e
JS
12119 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12120 phba->sli4_hba.pc_sli4_params.wqv);
12121 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12122 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12123 wq->entry_count);
12124 switch (wq->entry_size) {
12125 default:
12126 case 64:
12127 bf_set(lpfc_mbx_wq_create_wqe_size,
12128 &wq_create->u.request_1,
12129 LPFC_WQ_WQE_SIZE_64);
12130 break;
12131 case 128:
12132 bf_set(lpfc_mbx_wq_create_wqe_size,
12133 &wq_create->u.request_1,
12134 LPFC_WQ_WQE_SIZE_128);
12135 break;
12136 }
12137 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12138 (PAGE_SIZE/SLI4_PAGE_SIZE));
12139 page = wq_create->u.request_1.page;
12140 } else {
12141 page = wq_create->u.request.page;
12142 }
4f774513 12143 list_for_each_entry(dmabuf, &wq->page_list, list) {
49198b37 12144 memset(dmabuf->virt, 0, hw_page_size);
5a6f133e
JS
12145 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12146 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
4f774513
JS
12147 }
12148 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12149 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
12150 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12151 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12152 if (shdr_status || shdr_add_status || rc) {
12153 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12154 "2503 WQ_CREATE mailbox failed with "
12155 "status x%x add_status x%x, mbx status x%x\n",
12156 shdr_status, shdr_add_status, rc);
12157 status = -ENXIO;
12158 goto out;
12159 }
12160 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12161 if (wq->queue_id == 0xFFFF) {
12162 status = -ENXIO;
12163 goto out;
12164 }
12165 wq->type = LPFC_WQ;
2a622bfb 12166 wq->assoc_qid = cq->queue_id;
4f774513
JS
12167 wq->subtype = subtype;
12168 wq->host_index = 0;
12169 wq->hba_index = 0;
12170
12171 /* link the wq onto the parent cq child list */
12172 list_add_tail(&wq->list, &cq->child_list);
12173out:
8fa38513 12174 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12175 return status;
12176}
12177
73d91e50
JS
12178/**
12179 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12180 * @phba: HBA structure that indicates port to create a queue on.
12181 * @rq: The queue structure to use for the receive queue.
12182 * @qno: The associated HBQ number
12183 *
12184 *
12185 * For SLI4 we need to adjust the RQ repost value based on
12186 * the number of buffers that are initially posted to the RQ.
12187 */
12188void
12189lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12190{
12191 uint32_t cnt;
12192
12193 cnt = lpfc_hbq_defs[qno]->entry_count;
12194
12195 /* Recalc repost for RQs based on buffers initially posted */
12196 cnt = (cnt >> 3);
12197 if (cnt < LPFC_QUEUE_MIN_REPOST)
12198 cnt = LPFC_QUEUE_MIN_REPOST;
12199
12200 rq->entry_repost = cnt;
12201}
12202
4f774513
JS
12203/**
12204 * lpfc_rq_create - Create a Receive Queue on the HBA
12205 * @phba: HBA structure that indicates port to create a queue on.
12206 * @hrq: The queue structure to use to create the header receive queue.
12207 * @drq: The queue structure to use to create the data receive queue.
12208 * @cq: The completion queue to bind this work queue to.
12209 *
12210 * This function creates a receive buffer queue pair , as detailed in @hrq and
12211 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12212 * to the HBA.
12213 *
12214 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12215 * struct is used to get the entry count that is necessary to determine the
12216 * number of pages to use for this queue. The @cq is used to indicate which
12217 * completion queue to bind received buffers that are posted to these queues to.
12218 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12219 * receive queue pair. This function is asynchronous and will wait for the
12220 * mailbox command to finish before continuing.
12221 *
12222 * On success this function will return a zero. If unable to allocate enough
d439d286
JS
12223 * memory this function will return -ENOMEM. If the queue create mailbox command
12224 * fails this function will return -ENXIO.
4f774513
JS
12225 **/
12226uint32_t
12227lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12228 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12229{
12230 struct lpfc_mbx_rq_create *rq_create;
12231 struct lpfc_dmabuf *dmabuf;
12232 LPFC_MBOXQ_t *mbox;
12233 int rc, length, status = 0;
12234 uint32_t shdr_status, shdr_add_status;
12235 union lpfc_sli4_cfg_shdr *shdr;
49198b37
JS
12236 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12237
12238 if (!phba->sli4_hba.pc_sli4_params.supported)
12239 hw_page_size = SLI4_PAGE_SIZE;
4f774513
JS
12240
12241 if (hrq->entry_count != drq->entry_count)
12242 return -EINVAL;
12243 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12244 if (!mbox)
12245 return -ENOMEM;
12246 length = (sizeof(struct lpfc_mbx_rq_create) -
12247 sizeof(struct lpfc_sli4_cfg_mhdr));
12248 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12249 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12250 length, LPFC_SLI4_MBX_EMBED);
12251 rq_create = &mbox->u.mqe.un.rq_create;
5a6f133e
JS
12252 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12253 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12254 phba->sli4_hba.pc_sli4_params.rqv);
12255 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12256 bf_set(lpfc_rq_context_rqe_count_1,
12257 &rq_create->u.request.context,
12258 hrq->entry_count);
12259 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
c31098ce
JS
12260 bf_set(lpfc_rq_context_rqe_size,
12261 &rq_create->u.request.context,
12262 LPFC_RQE_SIZE_8);
12263 bf_set(lpfc_rq_context_page_size,
12264 &rq_create->u.request.context,
12265 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
12266 } else {
12267 switch (hrq->entry_count) {
12268 default:
12269 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12270 "2535 Unsupported RQ count. (%d)\n",
12271 hrq->entry_count);
12272 if (hrq->entry_count < 512)
12273 return -EINVAL;
12274 /* otherwise default to smallest count (drop through) */
12275 case 512:
12276 bf_set(lpfc_rq_context_rqe_count,
12277 &rq_create->u.request.context,
12278 LPFC_RQ_RING_SIZE_512);
12279 break;
12280 case 1024:
12281 bf_set(lpfc_rq_context_rqe_count,
12282 &rq_create->u.request.context,
12283 LPFC_RQ_RING_SIZE_1024);
12284 break;
12285 case 2048:
12286 bf_set(lpfc_rq_context_rqe_count,
12287 &rq_create->u.request.context,
12288 LPFC_RQ_RING_SIZE_2048);
12289 break;
12290 case 4096:
12291 bf_set(lpfc_rq_context_rqe_count,
12292 &rq_create->u.request.context,
12293 LPFC_RQ_RING_SIZE_4096);
12294 break;
12295 }
12296 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12297 LPFC_HDR_BUF_SIZE);
4f774513
JS
12298 }
12299 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12300 cq->queue_id);
12301 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12302 hrq->page_count);
4f774513 12303 list_for_each_entry(dmabuf, &hrq->page_list, list) {
49198b37 12304 memset(dmabuf->virt, 0, hw_page_size);
4f774513
JS
12305 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12306 putPaddrLow(dmabuf->phys);
12307 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12308 putPaddrHigh(dmabuf->phys);
12309 }
12310 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12311 /* The IOCTL status is embedded in the mailbox subheader. */
4f774513
JS
12312 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12313 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12314 if (shdr_status || shdr_add_status || rc) {
12315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12316 "2504 RQ_CREATE mailbox failed with "
12317 "status x%x add_status x%x, mbx status x%x\n",
12318 shdr_status, shdr_add_status, rc);
12319 status = -ENXIO;
12320 goto out;
12321 }
12322 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12323 if (hrq->queue_id == 0xFFFF) {
12324 status = -ENXIO;
12325 goto out;
12326 }
12327 hrq->type = LPFC_HRQ;
2a622bfb 12328 hrq->assoc_qid = cq->queue_id;
4f774513
JS
12329 hrq->subtype = subtype;
12330 hrq->host_index = 0;
12331 hrq->hba_index = 0;
12332
12333 /* now create the data queue */
12334 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12335 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
12336 length, LPFC_SLI4_MBX_EMBED);
5a6f133e
JS
12337 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12338 phba->sli4_hba.pc_sli4_params.rqv);
12339 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
12340 bf_set(lpfc_rq_context_rqe_count_1,
c31098ce 12341 &rq_create->u.request.context, hrq->entry_count);
5a6f133e 12342 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
c31098ce
JS
12343 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
12344 LPFC_RQE_SIZE_8);
12345 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
12346 (PAGE_SIZE/SLI4_PAGE_SIZE));
5a6f133e
JS
12347 } else {
12348 switch (drq->entry_count) {
12349 default:
12350 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12351 "2536 Unsupported RQ count. (%d)\n",
12352 drq->entry_count);
12353 if (drq->entry_count < 512)
12354 return -EINVAL;
12355 /* otherwise default to smallest count (drop through) */
12356 case 512:
12357 bf_set(lpfc_rq_context_rqe_count,
12358 &rq_create->u.request.context,
12359 LPFC_RQ_RING_SIZE_512);
12360 break;
12361 case 1024:
12362 bf_set(lpfc_rq_context_rqe_count,
12363 &rq_create->u.request.context,
12364 LPFC_RQ_RING_SIZE_1024);
12365 break;
12366 case 2048:
12367 bf_set(lpfc_rq_context_rqe_count,
12368 &rq_create->u.request.context,
12369 LPFC_RQ_RING_SIZE_2048);
12370 break;
12371 case 4096:
12372 bf_set(lpfc_rq_context_rqe_count,
12373 &rq_create->u.request.context,
12374 LPFC_RQ_RING_SIZE_4096);
12375 break;
12376 }
12377 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
12378 LPFC_DATA_BUF_SIZE);
4f774513
JS
12379 }
12380 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
12381 cq->queue_id);
12382 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
12383 drq->page_count);
4f774513
JS
12384 list_for_each_entry(dmabuf, &drq->page_list, list) {
12385 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12386 putPaddrLow(dmabuf->phys);
12387 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12388 putPaddrHigh(dmabuf->phys);
12389 }
12390 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12391 /* The IOCTL status is embedded in the mailbox subheader. */
12392 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
12393 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12394 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12395 if (shdr_status || shdr_add_status || rc) {
12396 status = -ENXIO;
12397 goto out;
12398 }
12399 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
12400 if (drq->queue_id == 0xFFFF) {
12401 status = -ENXIO;
12402 goto out;
12403 }
12404 drq->type = LPFC_DRQ;
2a622bfb 12405 drq->assoc_qid = cq->queue_id;
4f774513
JS
12406 drq->subtype = subtype;
12407 drq->host_index = 0;
12408 drq->hba_index = 0;
12409
12410 /* link the header and data RQs onto the parent cq child list */
12411 list_add_tail(&hrq->list, &cq->child_list);
12412 list_add_tail(&drq->list, &cq->child_list);
12413
12414out:
8fa38513 12415 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
12416 return status;
12417}
12418
12419/**
12420 * lpfc_eq_destroy - Destroy an event Queue on the HBA
12421 * @eq: The queue structure associated with the queue to destroy.
12422 *
12423 * This function destroys a queue, as detailed in @eq by sending an mailbox
12424 * command, specific to the type of queue, to the HBA.
12425 *
12426 * The @eq struct is used to get the queue ID of the queue to destroy.
12427 *
12428 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12429 * command fails this function will return -ENXIO.
4f774513
JS
12430 **/
12431uint32_t
12432lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
12433{
12434 LPFC_MBOXQ_t *mbox;
12435 int rc, length, status = 0;
12436 uint32_t shdr_status, shdr_add_status;
12437 union lpfc_sli4_cfg_shdr *shdr;
12438
12439 if (!eq)
12440 return -ENODEV;
12441 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
12442 if (!mbox)
12443 return -ENOMEM;
12444 length = (sizeof(struct lpfc_mbx_eq_destroy) -
12445 sizeof(struct lpfc_sli4_cfg_mhdr));
12446 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12447 LPFC_MBOX_OPCODE_EQ_DESTROY,
12448 length, LPFC_SLI4_MBX_EMBED);
12449 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
12450 eq->queue_id);
12451 mbox->vport = eq->phba->pport;
12452 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12453
12454 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
12455 /* The IOCTL status is embedded in the mailbox subheader. */
12456 shdr = (union lpfc_sli4_cfg_shdr *)
12457 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
12458 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12459 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12460 if (shdr_status || shdr_add_status || rc) {
12461 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12462 "2505 EQ_DESTROY mailbox failed with "
12463 "status x%x add_status x%x, mbx status x%x\n",
12464 shdr_status, shdr_add_status, rc);
12465 status = -ENXIO;
12466 }
12467
12468 /* Remove eq from any list */
12469 list_del_init(&eq->list);
8fa38513 12470 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
12471 return status;
12472}
12473
12474/**
12475 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
12476 * @cq: The queue structure associated with the queue to destroy.
12477 *
12478 * This function destroys a queue, as detailed in @cq by sending an mailbox
12479 * command, specific to the type of queue, to the HBA.
12480 *
12481 * The @cq struct is used to get the queue ID of the queue to destroy.
12482 *
12483 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12484 * command fails this function will return -ENXIO.
4f774513
JS
12485 **/
12486uint32_t
12487lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
12488{
12489 LPFC_MBOXQ_t *mbox;
12490 int rc, length, status = 0;
12491 uint32_t shdr_status, shdr_add_status;
12492 union lpfc_sli4_cfg_shdr *shdr;
12493
12494 if (!cq)
12495 return -ENODEV;
12496 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
12497 if (!mbox)
12498 return -ENOMEM;
12499 length = (sizeof(struct lpfc_mbx_cq_destroy) -
12500 sizeof(struct lpfc_sli4_cfg_mhdr));
12501 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12502 LPFC_MBOX_OPCODE_CQ_DESTROY,
12503 length, LPFC_SLI4_MBX_EMBED);
12504 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
12505 cq->queue_id);
12506 mbox->vport = cq->phba->pport;
12507 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12508 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
12509 /* The IOCTL status is embedded in the mailbox subheader. */
12510 shdr = (union lpfc_sli4_cfg_shdr *)
12511 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
12512 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12513 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12514 if (shdr_status || shdr_add_status || rc) {
12515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12516 "2506 CQ_DESTROY mailbox failed with "
12517 "status x%x add_status x%x, mbx status x%x\n",
12518 shdr_status, shdr_add_status, rc);
12519 status = -ENXIO;
12520 }
12521 /* Remove cq from any list */
12522 list_del_init(&cq->list);
8fa38513 12523 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
12524 return status;
12525}
12526
04c68496
JS
12527/**
12528 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
12529 * @qm: The queue structure associated with the queue to destroy.
12530 *
12531 * This function destroys a queue, as detailed in @mq by sending an mailbox
12532 * command, specific to the type of queue, to the HBA.
12533 *
12534 * The @mq struct is used to get the queue ID of the queue to destroy.
12535 *
12536 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12537 * command fails this function will return -ENXIO.
04c68496
JS
12538 **/
12539uint32_t
12540lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
12541{
12542 LPFC_MBOXQ_t *mbox;
12543 int rc, length, status = 0;
12544 uint32_t shdr_status, shdr_add_status;
12545 union lpfc_sli4_cfg_shdr *shdr;
12546
12547 if (!mq)
12548 return -ENODEV;
12549 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
12550 if (!mbox)
12551 return -ENOMEM;
12552 length = (sizeof(struct lpfc_mbx_mq_destroy) -
12553 sizeof(struct lpfc_sli4_cfg_mhdr));
12554 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12555 LPFC_MBOX_OPCODE_MQ_DESTROY,
12556 length, LPFC_SLI4_MBX_EMBED);
12557 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
12558 mq->queue_id);
12559 mbox->vport = mq->phba->pport;
12560 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12561 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
12562 /* The IOCTL status is embedded in the mailbox subheader. */
12563 shdr = (union lpfc_sli4_cfg_shdr *)
12564 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
12565 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12566 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12567 if (shdr_status || shdr_add_status || rc) {
12568 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12569 "2507 MQ_DESTROY mailbox failed with "
12570 "status x%x add_status x%x, mbx status x%x\n",
12571 shdr_status, shdr_add_status, rc);
12572 status = -ENXIO;
12573 }
12574 /* Remove mq from any list */
12575 list_del_init(&mq->list);
8fa38513 12576 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
12577 return status;
12578}
12579
4f774513
JS
12580/**
12581 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
12582 * @wq: The queue structure associated with the queue to destroy.
12583 *
12584 * This function destroys a queue, as detailed in @wq by sending an mailbox
12585 * command, specific to the type of queue, to the HBA.
12586 *
12587 * The @wq struct is used to get the queue ID of the queue to destroy.
12588 *
12589 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12590 * command fails this function will return -ENXIO.
4f774513
JS
12591 **/
12592uint32_t
12593lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
12594{
12595 LPFC_MBOXQ_t *mbox;
12596 int rc, length, status = 0;
12597 uint32_t shdr_status, shdr_add_status;
12598 union lpfc_sli4_cfg_shdr *shdr;
12599
12600 if (!wq)
12601 return -ENODEV;
12602 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
12603 if (!mbox)
12604 return -ENOMEM;
12605 length = (sizeof(struct lpfc_mbx_wq_destroy) -
12606 sizeof(struct lpfc_sli4_cfg_mhdr));
12607 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12608 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
12609 length, LPFC_SLI4_MBX_EMBED);
12610 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
12611 wq->queue_id);
12612 mbox->vport = wq->phba->pport;
12613 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12614 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
12615 shdr = (union lpfc_sli4_cfg_shdr *)
12616 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
12617 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12618 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12619 if (shdr_status || shdr_add_status || rc) {
12620 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12621 "2508 WQ_DESTROY mailbox failed with "
12622 "status x%x add_status x%x, mbx status x%x\n",
12623 shdr_status, shdr_add_status, rc);
12624 status = -ENXIO;
12625 }
12626 /* Remove wq from any list */
12627 list_del_init(&wq->list);
8fa38513 12628 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
12629 return status;
12630}
12631
12632/**
12633 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
12634 * @rq: The queue structure associated with the queue to destroy.
12635 *
12636 * This function destroys a queue, as detailed in @rq by sending an mailbox
12637 * command, specific to the type of queue, to the HBA.
12638 *
12639 * The @rq struct is used to get the queue ID of the queue to destroy.
12640 *
12641 * On success this function will return a zero. If the queue destroy mailbox
d439d286 12642 * command fails this function will return -ENXIO.
4f774513
JS
12643 **/
12644uint32_t
12645lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12646 struct lpfc_queue *drq)
12647{
12648 LPFC_MBOXQ_t *mbox;
12649 int rc, length, status = 0;
12650 uint32_t shdr_status, shdr_add_status;
12651 union lpfc_sli4_cfg_shdr *shdr;
12652
12653 if (!hrq || !drq)
12654 return -ENODEV;
12655 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
12656 if (!mbox)
12657 return -ENOMEM;
12658 length = (sizeof(struct lpfc_mbx_rq_destroy) -
fedd3b7b 12659 sizeof(struct lpfc_sli4_cfg_mhdr));
4f774513
JS
12660 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12661 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
12662 length, LPFC_SLI4_MBX_EMBED);
12663 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12664 hrq->queue_id);
12665 mbox->vport = hrq->phba->pport;
12666 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12667 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
12668 /* The IOCTL status is embedded in the mailbox subheader. */
12669 shdr = (union lpfc_sli4_cfg_shdr *)
12670 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12671 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12672 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12673 if (shdr_status || shdr_add_status || rc) {
12674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12675 "2509 RQ_DESTROY mailbox failed with "
12676 "status x%x add_status x%x, mbx status x%x\n",
12677 shdr_status, shdr_add_status, rc);
12678 if (rc != MBX_TIMEOUT)
12679 mempool_free(mbox, hrq->phba->mbox_mem_pool);
12680 return -ENXIO;
12681 }
12682 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
12683 drq->queue_id);
12684 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
12685 shdr = (union lpfc_sli4_cfg_shdr *)
12686 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
12687 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12688 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12689 if (shdr_status || shdr_add_status || rc) {
12690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12691 "2510 RQ_DESTROY mailbox failed with "
12692 "status x%x add_status x%x, mbx status x%x\n",
12693 shdr_status, shdr_add_status, rc);
12694 status = -ENXIO;
12695 }
12696 list_del_init(&hrq->list);
12697 list_del_init(&drq->list);
8fa38513 12698 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
12699 return status;
12700}
12701
12702/**
12703 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
12704 * @phba: The virtual port for which this call being executed.
12705 * @pdma_phys_addr0: Physical address of the 1st SGL page.
12706 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
12707 * @xritag: the xritag that ties this io to the SGL pages.
12708 *
12709 * This routine will post the sgl pages for the IO that has the xritag
12710 * that is in the iocbq structure. The xritag is assigned during iocbq
12711 * creation and persists for as long as the driver is loaded.
12712 * if the caller has fewer than 256 scatter gather segments to map then
12713 * pdma_phys_addr1 should be 0.
12714 * If the caller needs to map more than 256 scatter gather segment then
12715 * pdma_phys_addr1 should be a valid physical address.
12716 * physical address for SGLs must be 64 byte aligned.
12717 * If you are going to map 2 SGL's then the first one must have 256 entries
12718 * the second sgl can have between 1 and 256 entries.
12719 *
12720 * Return codes:
12721 * 0 - Success
12722 * -ENXIO, -ENOMEM - Failure
12723 **/
12724int
12725lpfc_sli4_post_sgl(struct lpfc_hba *phba,
12726 dma_addr_t pdma_phys_addr0,
12727 dma_addr_t pdma_phys_addr1,
12728 uint16_t xritag)
12729{
12730 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
12731 LPFC_MBOXQ_t *mbox;
12732 int rc;
12733 uint32_t shdr_status, shdr_add_status;
6d368e53 12734 uint32_t mbox_tmo;
4f774513
JS
12735 union lpfc_sli4_cfg_shdr *shdr;
12736
12737 if (xritag == NO_XRI) {
12738 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12739 "0364 Invalid param:\n");
12740 return -EINVAL;
12741 }
12742
12743 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12744 if (!mbox)
12745 return -ENOMEM;
12746
12747 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12748 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
12749 sizeof(struct lpfc_mbx_post_sgl_pages) -
fedd3b7b 12750 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
4f774513
JS
12751
12752 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
12753 &mbox->u.mqe.un.post_sgl_pages;
12754 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
12755 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
12756
12757 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
12758 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
12759 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
12760 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
12761
12762 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
12763 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
12764 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
12765 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
12766 if (!phba->sli4_hba.intr_enable)
12767 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6d368e53 12768 else {
a183a15f 12769 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
12770 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12771 }
4f774513
JS
12772 /* The IOCTL status is embedded in the mailbox subheader. */
12773 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
12774 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12775 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12776 if (rc != MBX_TIMEOUT)
12777 mempool_free(mbox, phba->mbox_mem_pool);
12778 if (shdr_status || shdr_add_status || rc) {
12779 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12780 "2511 POST_SGL mailbox failed with "
12781 "status x%x add_status x%x, mbx status x%x\n",
12782 shdr_status, shdr_add_status, rc);
12783 rc = -ENXIO;
12784 }
12785 return 0;
12786}
4f774513 12787
6d368e53 12788/**
88a2cfbb 12789 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
6d368e53
JS
12790 * @phba: pointer to lpfc hba data structure.
12791 *
12792 * This routine is invoked to post rpi header templates to the
88a2cfbb
JS
12793 * HBA consistent with the SLI-4 interface spec. This routine
12794 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
12795 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6d368e53 12796 *
88a2cfbb
JS
12797 * Returns
12798 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
12799 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
12800 **/
6d368e53
JS
12801uint16_t
12802lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
12803{
12804 unsigned long xri;
12805
12806 /*
12807 * Fetch the next logical xri. Because this index is logical,
12808 * the driver starts at 0 each time.
12809 */
12810 spin_lock_irq(&phba->hbalock);
12811 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
12812 phba->sli4_hba.max_cfg_param.max_xri, 0);
12813 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
12814 spin_unlock_irq(&phba->hbalock);
12815 return NO_XRI;
12816 } else {
12817 set_bit(xri, phba->sli4_hba.xri_bmask);
12818 phba->sli4_hba.max_cfg_param.xri_used++;
12819 phba->sli4_hba.xri_count++;
12820 }
12821
12822 spin_unlock_irq(&phba->hbalock);
12823 return xri;
12824}
12825
12826/**
12827 * lpfc_sli4_free_xri - Release an xri for reuse.
12828 * @phba: pointer to lpfc hba data structure.
12829 *
12830 * This routine is invoked to release an xri to the pool of
12831 * available rpis maintained by the driver.
12832 **/
12833void
12834__lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12835{
12836 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
12837 phba->sli4_hba.xri_count--;
12838 phba->sli4_hba.max_cfg_param.xri_used--;
12839 }
12840}
12841
12842/**
12843 * lpfc_sli4_free_xri - Release an xri for reuse.
12844 * @phba: pointer to lpfc hba data structure.
12845 *
12846 * This routine is invoked to release an xri to the pool of
12847 * available rpis maintained by the driver.
12848 **/
12849void
12850lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
12851{
12852 spin_lock_irq(&phba->hbalock);
12853 __lpfc_sli4_free_xri(phba, xri);
12854 spin_unlock_irq(&phba->hbalock);
12855}
12856
4f774513
JS
12857/**
12858 * lpfc_sli4_next_xritag - Get an xritag for the io
12859 * @phba: Pointer to HBA context object.
12860 *
12861 * This function gets an xritag for the iocb. If there is no unused xritag
12862 * it will return 0xffff.
12863 * The function returns the allocated xritag if successful, else returns zero.
12864 * Zero is not a valid xritag.
12865 * The caller is not required to hold any lock.
12866 **/
12867uint16_t
12868lpfc_sli4_next_xritag(struct lpfc_hba *phba)
12869{
6d368e53 12870 uint16_t xri_index;
4f774513 12871
6d368e53
JS
12872 xri_index = lpfc_sli4_alloc_xri(phba);
12873 if (xri_index != NO_XRI)
12874 return xri_index;
12875
12876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4f774513
JS
12877 "2004 Failed to allocate XRI.last XRITAG is %d"
12878 " Max XRI is %d, Used XRI is %d\n",
6d368e53 12879 xri_index,
4f774513
JS
12880 phba->sli4_hba.max_cfg_param.max_xri,
12881 phba->sli4_hba.max_cfg_param.xri_used);
6d368e53 12882 return NO_XRI;
4f774513
JS
12883}
12884
12885/**
6d368e53 12886 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
4f774513
JS
12887 * @phba: pointer to lpfc hba data structure.
12888 *
12889 * This routine is invoked to post a block of driver's sgl pages to the
12890 * HBA using non-embedded mailbox command. No Lock is held. This routine
12891 * is only called when the driver is loading and after all IO has been
12892 * stopped.
12893 **/
12894int
6d368e53 12895lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba)
4f774513
JS
12896{
12897 struct lpfc_sglq *sglq_entry;
12898 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
12899 struct sgl_page_pairs *sgl_pg_pairs;
12900 void *viraddr;
12901 LPFC_MBOXQ_t *mbox;
12902 uint32_t reqlen, alloclen, pg_pairs;
12903 uint32_t mbox_tmo;
6d368e53 12904 uint16_t xritag_start = 0, lxri = 0;
4f774513
JS
12905 int els_xri_cnt, rc = 0;
12906 uint32_t shdr_status, shdr_add_status;
12907 union lpfc_sli4_cfg_shdr *shdr;
12908
12909 /* The number of sgls to be posted */
12910 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
12911
12912 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
12913 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 12914 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
12915 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
12916 "2559 Block sgl registration required DMA "
12917 "size (%d) great than a page\n", reqlen);
12918 return -ENOMEM;
12919 }
12920 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6d368e53 12921 if (!mbox)
4f774513 12922 return -ENOMEM;
4f774513
JS
12923
12924 /* Allocate DMA memory and set up the non-embedded mailbox command */
12925 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12926 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
12927 LPFC_SLI4_MBX_NEMBED);
12928
12929 if (alloclen < reqlen) {
12930 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12931 "0285 Allocated DMA memory size (%d) is "
12932 "less than the requested DMA memory "
12933 "size (%d)\n", alloclen, reqlen);
12934 lpfc_sli4_mbox_cmd_free(phba, mbox);
12935 return -ENOMEM;
12936 }
4f774513 12937 /* Set up the SGL pages in the non-embedded DMA pages */
6d368e53 12938 viraddr = mbox->sge_array->addr[0];
4f774513
JS
12939 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
12940 sgl_pg_pairs = &sgl->sgl_pg_pairs;
12941
12942 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
12943 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
6d368e53
JS
12944
12945 /*
12946 * Assign the sglq a physical xri only if the driver has not
12947 * initialized those resources. A port reset only needs
12948 * the sglq's posted.
12949 */
12950 if (bf_get(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
12951 LPFC_XRI_RSRC_RDY) {
12952 lxri = lpfc_sli4_next_xritag(phba);
12953 if (lxri == NO_XRI) {
12954 lpfc_sli4_mbox_cmd_free(phba, mbox);
12955 return -ENOMEM;
12956 }
12957 sglq_entry->sli4_lxritag = lxri;
12958 sglq_entry->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
12959 }
12960
4f774513
JS
12961 /* Set up the sge entry */
12962 sgl_pg_pairs->sgl_pg0_addr_lo =
12963 cpu_to_le32(putPaddrLow(sglq_entry->phys));
12964 sgl_pg_pairs->sgl_pg0_addr_hi =
12965 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
12966 sgl_pg_pairs->sgl_pg1_addr_lo =
12967 cpu_to_le32(putPaddrLow(0));
12968 sgl_pg_pairs->sgl_pg1_addr_hi =
12969 cpu_to_le32(putPaddrHigh(0));
6d368e53 12970
4f774513
JS
12971 /* Keep the first xritag on the list */
12972 if (pg_pairs == 0)
12973 xritag_start = sglq_entry->sli4_xritag;
12974 sgl_pg_pairs++;
12975 }
6d368e53
JS
12976
12977 /* Complete initialization and perform endian conversion. */
4f774513 12978 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 12979 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513 12980 sgl->word0 = cpu_to_le32(sgl->word0);
4f774513
JS
12981 if (!phba->sli4_hba.intr_enable)
12982 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12983 else {
a183a15f 12984 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
12985 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
12986 }
12987 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
12988 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12989 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12990 if (rc != MBX_TIMEOUT)
12991 lpfc_sli4_mbox_cmd_free(phba, mbox);
12992 if (shdr_status || shdr_add_status || rc) {
12993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12994 "2513 POST_SGL_BLOCK mailbox command failed "
12995 "status x%x add_status x%x mbx status x%x\n",
12996 shdr_status, shdr_add_status, rc);
12997 rc = -ENXIO;
12998 }
6d368e53
JS
12999
13000 if (rc == 0)
13001 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13002 LPFC_XRI_RSRC_RDY);
13003 return rc;
13004}
13005
13006/**
13007 * lpfc_sli4_post_els_sgl_list_ext - post a block of ELS sgls to the port.
13008 * @phba: pointer to lpfc hba data structure.
13009 *
13010 * This routine is invoked to post a block of driver's sgl pages to the
13011 * HBA using non-embedded mailbox command. No Lock is held. This routine
13012 * is only called when the driver is loading and after all IO has been
13013 * stopped.
13014 **/
13015int
13016lpfc_sli4_post_els_sgl_list_ext(struct lpfc_hba *phba)
13017{
13018 struct lpfc_sglq *sglq_entry;
13019 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13020 struct sgl_page_pairs *sgl_pg_pairs;
13021 void *viraddr;
13022 LPFC_MBOXQ_t *mbox;
13023 uint32_t reqlen, alloclen, index;
13024 uint32_t mbox_tmo;
13025 uint16_t rsrc_start, rsrc_size, els_xri_cnt;
13026 uint16_t xritag_start = 0, lxri = 0;
13027 struct lpfc_rsrc_blks *rsrc_blk;
13028 int cnt, ttl_cnt, rc = 0;
13029 int loop_cnt;
13030 uint32_t shdr_status, shdr_add_status;
13031 union lpfc_sli4_cfg_shdr *shdr;
13032
13033 /* The number of sgls to be posted */
13034 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
13035
13036 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
13037 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13038 if (reqlen > SLI4_PAGE_SIZE) {
13039 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13040 "2989 Block sgl registration required DMA "
13041 "size (%d) great than a page\n", reqlen);
13042 return -ENOMEM;
13043 }
13044
13045 cnt = 0;
13046 ttl_cnt = 0;
13047 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13048 list) {
13049 rsrc_start = rsrc_blk->rsrc_start;
13050 rsrc_size = rsrc_blk->rsrc_size;
13051
13052 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13053 "3014 Working ELS Extent start %d, cnt %d\n",
13054 rsrc_start, rsrc_size);
13055
13056 loop_cnt = min(els_xri_cnt, rsrc_size);
13057 if (ttl_cnt + loop_cnt >= els_xri_cnt) {
13058 loop_cnt = els_xri_cnt - ttl_cnt;
13059 ttl_cnt = els_xri_cnt;
13060 }
13061
13062 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13063 if (!mbox)
13064 return -ENOMEM;
13065 /*
13066 * Allocate DMA memory and set up the non-embedded mailbox
13067 * command.
13068 */
13069 alloclen = lpfc_sli4_config(phba, mbox,
13070 LPFC_MBOX_SUBSYSTEM_FCOE,
13071 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13072 reqlen, LPFC_SLI4_MBX_NEMBED);
13073 if (alloclen < reqlen) {
13074 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13075 "2987 Allocated DMA memory size (%d) "
13076 "is less than the requested DMA memory "
13077 "size (%d)\n", alloclen, reqlen);
13078 lpfc_sli4_mbox_cmd_free(phba, mbox);
13079 return -ENOMEM;
13080 }
13081
13082 /* Set up the SGL pages in the non-embedded DMA pages */
13083 viraddr = mbox->sge_array->addr[0];
13084 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13085 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13086
13087 /*
13088 * The starting resource may not begin at zero. Control
13089 * the loop variants via the block resource parameters,
13090 * but handle the sge pointers with a zero-based index
13091 * that doesn't get reset per loop pass.
13092 */
13093 for (index = rsrc_start;
13094 index < rsrc_start + loop_cnt;
13095 index++) {
13096 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[cnt];
13097
13098 /*
13099 * Assign the sglq a physical xri only if the driver
13100 * has not initialized those resources. A port reset
13101 * only needs the sglq's posted.
13102 */
13103 if (bf_get(lpfc_xri_rsrc_rdy,
13104 &phba->sli4_hba.sli4_flags) !=
13105 LPFC_XRI_RSRC_RDY) {
13106 lxri = lpfc_sli4_next_xritag(phba);
13107 if (lxri == NO_XRI) {
13108 lpfc_sli4_mbox_cmd_free(phba, mbox);
13109 rc = -ENOMEM;
13110 goto err_exit;
13111 }
13112 sglq_entry->sli4_lxritag = lxri;
13113 sglq_entry->sli4_xritag =
13114 phba->sli4_hba.xri_ids[lxri];
13115 }
13116
13117 /* Set up the sge entry */
13118 sgl_pg_pairs->sgl_pg0_addr_lo =
13119 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13120 sgl_pg_pairs->sgl_pg0_addr_hi =
13121 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13122 sgl_pg_pairs->sgl_pg1_addr_lo =
13123 cpu_to_le32(putPaddrLow(0));
13124 sgl_pg_pairs->sgl_pg1_addr_hi =
13125 cpu_to_le32(putPaddrHigh(0));
13126
13127 /* Track the starting physical XRI for the mailbox. */
13128 if (index == rsrc_start)
13129 xritag_start = sglq_entry->sli4_xritag;
13130 sgl_pg_pairs++;
13131 cnt++;
13132 }
13133
13134 /* Complete initialization and perform endian conversion. */
13135 rsrc_blk->rsrc_used += loop_cnt;
13136 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13137 bf_set(lpfc_post_sgl_pages_xricnt, sgl, loop_cnt);
13138 sgl->word0 = cpu_to_le32(sgl->word0);
13139
13140 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13141 "3015 Post ELS Extent SGL, start %d, "
13142 "cnt %d, used %d\n",
13143 xritag_start, loop_cnt, rsrc_blk->rsrc_used);
13144 if (!phba->sli4_hba.intr_enable)
13145 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13146 else {
a183a15f 13147 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
13148 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13149 }
13150 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13151 shdr_status = bf_get(lpfc_mbox_hdr_status,
13152 &shdr->response);
13153 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13154 &shdr->response);
13155 if (rc != MBX_TIMEOUT)
13156 lpfc_sli4_mbox_cmd_free(phba, mbox);
13157 if (shdr_status || shdr_add_status || rc) {
13158 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13159 "2988 POST_SGL_BLOCK mailbox "
13160 "command failed status x%x "
13161 "add_status x%x mbx status x%x\n",
13162 shdr_status, shdr_add_status, rc);
13163 rc = -ENXIO;
13164 goto err_exit;
13165 }
13166 if (ttl_cnt >= els_xri_cnt)
13167 break;
13168 }
13169
13170 err_exit:
13171 if (rc == 0)
13172 bf_set(lpfc_xri_rsrc_rdy, &phba->sli4_hba.sli4_flags,
13173 LPFC_XRI_RSRC_RDY);
4f774513
JS
13174 return rc;
13175}
13176
13177/**
13178 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13179 * @phba: pointer to lpfc hba data structure.
13180 * @sblist: pointer to scsi buffer list.
13181 * @count: number of scsi buffers on the list.
13182 *
13183 * This routine is invoked to post a block of @count scsi sgl pages from a
13184 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13185 * No Lock is held.
13186 *
13187 **/
13188int
13189lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
13190 int cnt)
13191{
13192 struct lpfc_scsi_buf *psb;
13193 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13194 struct sgl_page_pairs *sgl_pg_pairs;
13195 void *viraddr;
13196 LPFC_MBOXQ_t *mbox;
13197 uint32_t reqlen, alloclen, pg_pairs;
13198 uint32_t mbox_tmo;
13199 uint16_t xritag_start = 0;
13200 int rc = 0;
13201 uint32_t shdr_status, shdr_add_status;
13202 dma_addr_t pdma_phys_bpl1;
13203 union lpfc_sli4_cfg_shdr *shdr;
13204
13205 /* Calculate the requested length of the dma memory */
13206 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13207 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
49198b37 13208 if (reqlen > SLI4_PAGE_SIZE) {
4f774513
JS
13209 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13210 "0217 Block sgl registration required DMA "
13211 "size (%d) great than a page\n", reqlen);
13212 return -ENOMEM;
13213 }
13214 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13215 if (!mbox) {
13216 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13217 "0283 Failed to allocate mbox cmd memory\n");
13218 return -ENOMEM;
13219 }
13220
13221 /* Allocate DMA memory and set up the non-embedded mailbox command */
13222 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13223 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13224 LPFC_SLI4_MBX_NEMBED);
13225
13226 if (alloclen < reqlen) {
13227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13228 "2561 Allocated DMA memory size (%d) is "
13229 "less than the requested DMA memory "
13230 "size (%d)\n", alloclen, reqlen);
13231 lpfc_sli4_mbox_cmd_free(phba, mbox);
13232 return -ENOMEM;
13233 }
6d368e53 13234
4f774513 13235 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
13236 viraddr = mbox->sge_array->addr[0];
13237
13238 /* Set up the SGL pages in the non-embedded DMA pages */
13239 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13240 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13241
13242 pg_pairs = 0;
13243 list_for_each_entry(psb, sblist, list) {
13244 /* Set up the sge entry */
13245 sgl_pg_pairs->sgl_pg0_addr_lo =
13246 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13247 sgl_pg_pairs->sgl_pg0_addr_hi =
13248 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13249 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13250 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13251 else
13252 pdma_phys_bpl1 = 0;
13253 sgl_pg_pairs->sgl_pg1_addr_lo =
13254 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13255 sgl_pg_pairs->sgl_pg1_addr_hi =
13256 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13257 /* Keep the first xritag on the list */
13258 if (pg_pairs == 0)
13259 xritag_start = psb->cur_iocbq.sli4_xritag;
13260 sgl_pg_pairs++;
13261 pg_pairs++;
13262 }
13263 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13264 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13265 /* Perform endian conversion if necessary */
13266 sgl->word0 = cpu_to_le32(sgl->word0);
13267
13268 if (!phba->sli4_hba.intr_enable)
13269 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13270 else {
a183a15f 13271 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
4f774513
JS
13272 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13273 }
13274 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13275 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13276 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13277 if (rc != MBX_TIMEOUT)
13278 lpfc_sli4_mbox_cmd_free(phba, mbox);
13279 if (shdr_status || shdr_add_status || rc) {
13280 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13281 "2564 POST_SGL_BLOCK mailbox command failed "
13282 "status x%x add_status x%x mbx status x%x\n",
13283 shdr_status, shdr_add_status, rc);
13284 rc = -ENXIO;
13285 }
13286 return rc;
13287}
13288
6d368e53
JS
13289/**
13290 * lpfc_sli4_post_scsi_sgl_blk_ext - post a block of scsi sgls to the port.
13291 * @phba: pointer to lpfc hba data structure.
13292 * @sblist: pointer to scsi buffer list.
13293 * @count: number of scsi buffers on the list.
13294 *
13295 * This routine is invoked to post a block of @count scsi sgl pages from a
13296 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13297 * No Lock is held.
13298 *
13299 **/
13300int
13301lpfc_sli4_post_scsi_sgl_blk_ext(struct lpfc_hba *phba, struct list_head *sblist,
13302 int cnt)
13303{
13304 struct lpfc_scsi_buf *psb = NULL;
13305 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13306 struct sgl_page_pairs *sgl_pg_pairs;
13307 void *viraddr;
13308 LPFC_MBOXQ_t *mbox;
13309 uint32_t reqlen, alloclen, pg_pairs;
13310 uint32_t mbox_tmo;
13311 uint16_t xri_start = 0, scsi_xri_start;
13312 uint16_t rsrc_range;
13313 int rc = 0, avail_cnt;
13314 uint32_t shdr_status, shdr_add_status;
13315 dma_addr_t pdma_phys_bpl1;
13316 union lpfc_sli4_cfg_shdr *shdr;
13317 struct lpfc_rsrc_blks *rsrc_blk;
13318 uint32_t xri_cnt = 0;
13319
13320 /* Calculate the total requested length of the dma memory */
13321 reqlen = cnt * sizeof(struct sgl_page_pairs) +
13322 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13323 if (reqlen > SLI4_PAGE_SIZE) {
13324 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13325 "2932 Block sgl registration required DMA "
13326 "size (%d) great than a page\n", reqlen);
13327 return -ENOMEM;
13328 }
13329
13330 /*
13331 * The use of extents requires the driver to post the sgl headers
13332 * in multiple postings to meet the contiguous resource assignment.
13333 */
13334 psb = list_prepare_entry(psb, sblist, list);
13335 scsi_xri_start = phba->sli4_hba.scsi_xri_start;
13336 list_for_each_entry(rsrc_blk, &phba->sli4_hba.lpfc_xri_blk_list,
13337 list) {
13338 rsrc_range = rsrc_blk->rsrc_start + rsrc_blk->rsrc_size;
13339 if (rsrc_range < scsi_xri_start)
13340 continue;
13341 else if (rsrc_blk->rsrc_used >= rsrc_blk->rsrc_size)
13342 continue;
13343 else
13344 avail_cnt = rsrc_blk->rsrc_size - rsrc_blk->rsrc_used;
13345
13346 reqlen = (avail_cnt * sizeof(struct sgl_page_pairs)) +
13347 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13348 /*
13349 * Allocate DMA memory and set up the non-embedded mailbox
13350 * command. The mbox is used to post an SGL page per loop
13351 * but the DMA memory has a use-once semantic so the mailbox
13352 * is used and freed per loop pass.
13353 */
13354 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13355 if (!mbox) {
13356 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13357 "2933 Failed to allocate mbox cmd "
13358 "memory\n");
13359 return -ENOMEM;
13360 }
13361 alloclen = lpfc_sli4_config(phba, mbox,
13362 LPFC_MBOX_SUBSYSTEM_FCOE,
13363 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13364 reqlen,
13365 LPFC_SLI4_MBX_NEMBED);
13366 if (alloclen < reqlen) {
13367 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13368 "2934 Allocated DMA memory size (%d) "
13369 "is less than the requested DMA memory "
13370 "size (%d)\n", alloclen, reqlen);
13371 lpfc_sli4_mbox_cmd_free(phba, mbox);
13372 return -ENOMEM;
13373 }
13374
13375 /* Get the first SGE entry from the non-embedded DMA memory */
13376 viraddr = mbox->sge_array->addr[0];
13377
13378 /* Set up the SGL pages in the non-embedded DMA pages */
13379 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13380 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13381
13382 /* pg_pairs tracks posted SGEs per loop iteration. */
13383 pg_pairs = 0;
13384 list_for_each_entry_continue(psb, sblist, list) {
13385 /* Set up the sge entry */
13386 sgl_pg_pairs->sgl_pg0_addr_lo =
13387 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13388 sgl_pg_pairs->sgl_pg0_addr_hi =
13389 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13390 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13391 pdma_phys_bpl1 = psb->dma_phys_bpl +
13392 SGL_PAGE_SIZE;
13393 else
13394 pdma_phys_bpl1 = 0;
13395 sgl_pg_pairs->sgl_pg1_addr_lo =
13396 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13397 sgl_pg_pairs->sgl_pg1_addr_hi =
13398 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13399 /* Keep the first xri for this extent. */
13400 if (pg_pairs == 0)
13401 xri_start = psb->cur_iocbq.sli4_xritag;
13402 sgl_pg_pairs++;
13403 pg_pairs++;
13404 xri_cnt++;
13405
13406 /*
13407 * Track two exit conditions - the loop has constructed
13408 * all of the caller's SGE pairs or all available
13409 * resource IDs in this extent are consumed.
13410 */
13411 if ((xri_cnt == cnt) || (pg_pairs >= avail_cnt))
13412 break;
13413 }
13414 rsrc_blk->rsrc_used += pg_pairs;
13415 bf_set(lpfc_post_sgl_pages_xri, sgl, xri_start);
13416 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13417
13418 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13419 "3016 Post SCSI Extent SGL, start %d, cnt %d "
13420 "blk use %d\n",
13421 xri_start, pg_pairs, rsrc_blk->rsrc_used);
13422 /* Perform endian conversion if necessary */
13423 sgl->word0 = cpu_to_le32(sgl->word0);
13424 if (!phba->sli4_hba.intr_enable)
13425 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13426 else {
a183a15f 13427 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6d368e53
JS
13428 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13429 }
13430 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13431 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13432 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
13433 &shdr->response);
13434 if (rc != MBX_TIMEOUT)
13435 lpfc_sli4_mbox_cmd_free(phba, mbox);
13436 if (shdr_status || shdr_add_status || rc) {
13437 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13438 "2935 POST_SGL_BLOCK mailbox command "
13439 "failed status x%x add_status x%x "
13440 "mbx status x%x\n",
13441 shdr_status, shdr_add_status, rc);
13442 return -ENXIO;
13443 }
13444
13445 /* Post only what is requested. */
13446 if (xri_cnt >= cnt)
13447 break;
13448 }
13449 return rc;
13450}
13451
4f774513
JS
13452/**
13453 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13454 * @phba: pointer to lpfc_hba struct that the frame was received on
13455 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13456 *
13457 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13458 * valid type of frame that the LPFC driver will handle. This function will
13459 * return a zero if the frame is a valid frame or a non zero value when the
13460 * frame does not pass the check.
13461 **/
13462static int
13463lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13464{
474ffb74
TH
13465 /* make rctl_names static to save stack space */
13466 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
4f774513
JS
13467 char *type_names[] = FC_TYPE_NAMES_INIT;
13468 struct fc_vft_header *fc_vft_hdr;
546fc854 13469 uint32_t *header = (uint32_t *) fc_hdr;
4f774513
JS
13470
13471 switch (fc_hdr->fh_r_ctl) {
13472 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13473 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13474 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13475 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13476 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13477 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13478 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13479 case FC_RCTL_DD_CMD_STATUS: /* command status */
13480 case FC_RCTL_ELS_REQ: /* extended link services request */
13481 case FC_RCTL_ELS_REP: /* extended link services reply */
13482 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13483 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13484 case FC_RCTL_BA_NOP: /* basic link service NOP */
13485 case FC_RCTL_BA_ABTS: /* basic link service abort */
13486 case FC_RCTL_BA_RMC: /* remove connection */
13487 case FC_RCTL_BA_ACC: /* basic accept */
13488 case FC_RCTL_BA_RJT: /* basic reject */
13489 case FC_RCTL_BA_PRMT:
13490 case FC_RCTL_ACK_1: /* acknowledge_1 */
13491 case FC_RCTL_ACK_0: /* acknowledge_0 */
13492 case FC_RCTL_P_RJT: /* port reject */
13493 case FC_RCTL_F_RJT: /* fabric reject */
13494 case FC_RCTL_P_BSY: /* port busy */
13495 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13496 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13497 case FC_RCTL_LCR: /* link credit reset */
13498 case FC_RCTL_END: /* end */
13499 break;
13500 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13501 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13502 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13503 return lpfc_fc_frame_check(phba, fc_hdr);
13504 default:
13505 goto drop;
13506 }
13507 switch (fc_hdr->fh_type) {
13508 case FC_TYPE_BLS:
13509 case FC_TYPE_ELS:
13510 case FC_TYPE_FCP:
13511 case FC_TYPE_CT:
13512 break;
13513 case FC_TYPE_IP:
13514 case FC_TYPE_ILS:
13515 default:
13516 goto drop;
13517 }
546fc854 13518
4f774513 13519 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
546fc854
JS
13520 "2538 Received frame rctl:%s type:%s "
13521 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
4f774513 13522 rctl_names[fc_hdr->fh_r_ctl],
546fc854
JS
13523 type_names[fc_hdr->fh_type],
13524 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13525 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13526 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
4f774513
JS
13527 return 0;
13528drop:
13529 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13530 "2539 Dropped frame rctl:%s type:%s\n",
13531 rctl_names[fc_hdr->fh_r_ctl],
13532 type_names[fc_hdr->fh_type]);
13533 return 1;
13534}
13535
13536/**
13537 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13538 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13539 *
13540 * This function processes the FC header to retrieve the VFI from the VF
13541 * header, if one exists. This function will return the VFI if one exists
13542 * or 0 if no VSAN Header exists.
13543 **/
13544static uint32_t
13545lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
13546{
13547 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13548
13549 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
13550 return 0;
13551 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
13552}
13553
13554/**
13555 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
13556 * @phba: Pointer to the HBA structure to search for the vport on
13557 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13558 * @fcfi: The FC Fabric ID that the frame came from
13559 *
13560 * This function searches the @phba for a vport that matches the content of the
13561 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
13562 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
13563 * returns the matching vport pointer or NULL if unable to match frame to a
13564 * vport.
13565 **/
13566static struct lpfc_vport *
13567lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
13568 uint16_t fcfi)
13569{
13570 struct lpfc_vport **vports;
13571 struct lpfc_vport *vport = NULL;
13572 int i;
13573 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
13574 fc_hdr->fh_d_id[1] << 8 |
13575 fc_hdr->fh_d_id[2]);
bf08611b
JS
13576 if (did == Fabric_DID)
13577 return phba->pport;
4f774513
JS
13578 vports = lpfc_create_vport_work_array(phba);
13579 if (vports != NULL)
13580 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
13581 if (phba->fcf.fcfi == fcfi &&
13582 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
13583 vports[i]->fc_myDID == did) {
13584 vport = vports[i];
13585 break;
13586 }
13587 }
13588 lpfc_destroy_vport_work_array(phba, vports);
13589 return vport;
13590}
13591
45ed1190
JS
13592/**
13593 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
13594 * @vport: The vport to work on.
13595 *
13596 * This function updates the receive sequence time stamp for this vport. The
13597 * receive sequence time stamp indicates the time that the last frame of the
13598 * the sequence that has been idle for the longest amount of time was received.
13599 * the driver uses this time stamp to indicate if any received sequences have
13600 * timed out.
13601 **/
13602void
13603lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
13604{
13605 struct lpfc_dmabuf *h_buf;
13606 struct hbq_dmabuf *dmabuf = NULL;
13607
13608 /* get the oldest sequence on the rcv list */
13609 h_buf = list_get_first(&vport->rcv_buffer_list,
13610 struct lpfc_dmabuf, list);
13611 if (!h_buf)
13612 return;
13613 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13614 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
13615}
13616
13617/**
13618 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
13619 * @vport: The vport that the received sequences were sent to.
13620 *
13621 * This function cleans up all outstanding received sequences. This is called
13622 * by the driver when a link event or user action invalidates all the received
13623 * sequences.
13624 **/
13625void
13626lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
13627{
13628 struct lpfc_dmabuf *h_buf, *hnext;
13629 struct lpfc_dmabuf *d_buf, *dnext;
13630 struct hbq_dmabuf *dmabuf = NULL;
13631
13632 /* start with the oldest sequence on the rcv list */
13633 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13634 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13635 list_del_init(&dmabuf->hbuf.list);
13636 list_for_each_entry_safe(d_buf, dnext,
13637 &dmabuf->dbuf.list, list) {
13638 list_del_init(&d_buf->list);
13639 lpfc_in_buf_free(vport->phba, d_buf);
13640 }
13641 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13642 }
13643}
13644
13645/**
13646 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
13647 * @vport: The vport that the received sequences were sent to.
13648 *
13649 * This function determines whether any received sequences have timed out by
13650 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
13651 * indicates that there is at least one timed out sequence this routine will
13652 * go through the received sequences one at a time from most inactive to most
13653 * active to determine which ones need to be cleaned up. Once it has determined
13654 * that a sequence needs to be cleaned up it will simply free up the resources
13655 * without sending an abort.
13656 **/
13657void
13658lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
13659{
13660 struct lpfc_dmabuf *h_buf, *hnext;
13661 struct lpfc_dmabuf *d_buf, *dnext;
13662 struct hbq_dmabuf *dmabuf = NULL;
13663 unsigned long timeout;
13664 int abort_count = 0;
13665
13666 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13667 vport->rcv_buffer_time_stamp);
13668 if (list_empty(&vport->rcv_buffer_list) ||
13669 time_before(jiffies, timeout))
13670 return;
13671 /* start with the oldest sequence on the rcv list */
13672 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
13673 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13674 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
13675 dmabuf->time_stamp);
13676 if (time_before(jiffies, timeout))
13677 break;
13678 abort_count++;
13679 list_del_init(&dmabuf->hbuf.list);
13680 list_for_each_entry_safe(d_buf, dnext,
13681 &dmabuf->dbuf.list, list) {
13682 list_del_init(&d_buf->list);
13683 lpfc_in_buf_free(vport->phba, d_buf);
13684 }
13685 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
13686 }
13687 if (abort_count)
13688 lpfc_update_rcv_time_stamp(vport);
13689}
13690
4f774513
JS
13691/**
13692 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
13693 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
13694 *
13695 * This function searches through the existing incomplete sequences that have
13696 * been sent to this @vport. If the frame matches one of the incomplete
13697 * sequences then the dbuf in the @dmabuf is added to the list of frames that
13698 * make up that sequence. If no sequence is found that matches this frame then
13699 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
13700 * This function returns a pointer to the first dmabuf in the sequence list that
13701 * the frame was linked to.
13702 **/
13703static struct hbq_dmabuf *
13704lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
13705{
13706 struct fc_frame_header *new_hdr;
13707 struct fc_frame_header *temp_hdr;
13708 struct lpfc_dmabuf *d_buf;
13709 struct lpfc_dmabuf *h_buf;
13710 struct hbq_dmabuf *seq_dmabuf = NULL;
13711 struct hbq_dmabuf *temp_dmabuf = NULL;
13712
4d9ab994 13713 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 13714 dmabuf->time_stamp = jiffies;
4f774513
JS
13715 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13716 /* Use the hdr_buf to find the sequence that this frame belongs to */
13717 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13718 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13719 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13720 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13721 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13722 continue;
13723 /* found a pending sequence that matches this frame */
13724 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13725 break;
13726 }
13727 if (!seq_dmabuf) {
13728 /*
13729 * This indicates first frame received for this sequence.
13730 * Queue the buffer on the vport's rcv_buffer_list.
13731 */
13732 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 13733 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
13734 return dmabuf;
13735 }
13736 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
13737 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
13738 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
13739 list_del_init(&seq_dmabuf->hbuf.list);
13740 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
13741 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 13742 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
13743 return dmabuf;
13744 }
45ed1190
JS
13745 /* move this sequence to the tail to indicate a young sequence */
13746 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
13747 seq_dmabuf->time_stamp = jiffies;
13748 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
13749 if (list_empty(&seq_dmabuf->dbuf.list)) {
13750 temp_hdr = dmabuf->hbuf.virt;
13751 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
13752 return seq_dmabuf;
13753 }
4f774513
JS
13754 /* find the correct place in the sequence to insert this frame */
13755 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
13756 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
13757 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
13758 /*
13759 * If the frame's sequence count is greater than the frame on
13760 * the list then insert the frame right after this frame
13761 */
eeead811
JS
13762 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
13763 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
13764 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
13765 return seq_dmabuf;
13766 }
13767 }
13768 return NULL;
13769}
13770
6669f9bb
JS
13771/**
13772 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
13773 * @vport: pointer to a vitural port
13774 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13775 *
13776 * This function tries to abort from the partially assembed sequence, described
13777 * by the information from basic abbort @dmabuf. It checks to see whether such
13778 * partially assembled sequence held by the driver. If so, it shall free up all
13779 * the frames from the partially assembled sequence.
13780 *
13781 * Return
13782 * true -- if there is matching partially assembled sequence present and all
13783 * the frames freed with the sequence;
13784 * false -- if there is no matching partially assembled sequence present so
13785 * nothing got aborted in the lower layer driver
13786 **/
13787static bool
13788lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
13789 struct hbq_dmabuf *dmabuf)
13790{
13791 struct fc_frame_header *new_hdr;
13792 struct fc_frame_header *temp_hdr;
13793 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
13794 struct hbq_dmabuf *seq_dmabuf = NULL;
13795
13796 /* Use the hdr_buf to find the sequence that matches this frame */
13797 INIT_LIST_HEAD(&dmabuf->dbuf.list);
13798 INIT_LIST_HEAD(&dmabuf->hbuf.list);
13799 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
13800 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
13801 temp_hdr = (struct fc_frame_header *)h_buf->virt;
13802 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
13803 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
13804 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
13805 continue;
13806 /* found a pending sequence that matches this frame */
13807 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
13808 break;
13809 }
13810
13811 /* Free up all the frames from the partially assembled sequence */
13812 if (seq_dmabuf) {
13813 list_for_each_entry_safe(d_buf, n_buf,
13814 &seq_dmabuf->dbuf.list, list) {
13815 list_del_init(&d_buf->list);
13816 lpfc_in_buf_free(vport->phba, d_buf);
13817 }
13818 return true;
13819 }
13820 return false;
13821}
13822
13823/**
546fc854 13824 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
6669f9bb
JS
13825 * @phba: Pointer to HBA context object.
13826 * @cmd_iocbq: pointer to the command iocbq structure.
13827 * @rsp_iocbq: pointer to the response iocbq structure.
13828 *
546fc854 13829 * This function handles the sequence abort response iocb command complete
6669f9bb
JS
13830 * event. It properly releases the memory allocated to the sequence abort
13831 * accept iocb.
13832 **/
13833static void
546fc854 13834lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
6669f9bb
JS
13835 struct lpfc_iocbq *cmd_iocbq,
13836 struct lpfc_iocbq *rsp_iocbq)
13837{
13838 if (cmd_iocbq)
13839 lpfc_sli_release_iocbq(phba, cmd_iocbq);
13840}
13841
6d368e53
JS
13842/**
13843 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
13844 * @phba: Pointer to HBA context object.
13845 * @xri: xri id in transaction.
13846 *
13847 * This function validates the xri maps to the known range of XRIs allocated an
13848 * used by the driver.
13849 **/
7851fe2c 13850uint16_t
6d368e53
JS
13851lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
13852 uint16_t xri)
13853{
13854 int i;
13855
13856 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
13857 if (xri == phba->sli4_hba.xri_ids[i])
13858 return i;
13859 }
13860 return NO_XRI;
13861}
13862
13863
6669f9bb 13864/**
546fc854 13865 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
6669f9bb
JS
13866 * @phba: Pointer to HBA context object.
13867 * @fc_hdr: pointer to a FC frame header.
13868 *
546fc854 13869 * This function sends a basic response to a previous unsol sequence abort
6669f9bb
JS
13870 * event after aborting the sequence handling.
13871 **/
13872static void
546fc854 13873lpfc_sli4_seq_abort_rsp(struct lpfc_hba *phba,
6669f9bb
JS
13874 struct fc_frame_header *fc_hdr)
13875{
13876 struct lpfc_iocbq *ctiocb = NULL;
13877 struct lpfc_nodelist *ndlp;
5ffc266e
JS
13878 uint16_t oxid, rxid;
13879 uint32_t sid, fctl;
6669f9bb 13880 IOCB_t *icmd;
546fc854 13881 int rc;
6669f9bb
JS
13882
13883 if (!lpfc_is_link_up(phba))
13884 return;
13885
13886 sid = sli4_sid_from_fc_hdr(fc_hdr);
13887 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 13888 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb
JS
13889
13890 ndlp = lpfc_findnode_did(phba->pport, sid);
13891 if (!ndlp) {
13892 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13893 "1268 Find ndlp returned NULL for oxid:x%x "
13894 "SID:x%x\n", oxid, sid);
13895 return;
13896 }
6d368e53 13897 if (lpfc_sli4_xri_inrange(phba, rxid))
19ca7609 13898 lpfc_set_rrq_active(phba, ndlp, rxid, oxid, 0);
6669f9bb 13899
546fc854 13900 /* Allocate buffer for rsp iocb */
6669f9bb
JS
13901 ctiocb = lpfc_sli_get_iocbq(phba);
13902 if (!ctiocb)
13903 return;
13904
5ffc266e
JS
13905 /* Extract the F_CTL field from FC_HDR */
13906 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
13907
6669f9bb 13908 icmd = &ctiocb->iocb;
6669f9bb 13909 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 13910 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
13911 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
13912 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
13913 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
13914
13915 /* Fill in the rest of iocb fields */
13916 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
13917 icmd->ulpBdeCount = 0;
13918 icmd->ulpLe = 1;
13919 icmd->ulpClass = CLASS3;
6d368e53 13920 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
be858b65 13921 ctiocb->context1 = ndlp;
6669f9bb 13922
6669f9bb
JS
13923 ctiocb->iocb_cmpl = NULL;
13924 ctiocb->vport = phba->pport;
546fc854 13925 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
6d368e53 13926 ctiocb->sli4_lxritag = NO_XRI;
546fc854
JS
13927 ctiocb->sli4_xritag = NO_XRI;
13928
13929 /* If the oxid maps to the FCP XRI range or if it is out of range,
13930 * send a BLS_RJT. The driver no longer has that exchange.
13931 * Override the IOCB for a BA_RJT.
13932 */
13933 if (oxid > (phba->sli4_hba.max_cfg_param.max_xri +
13934 phba->sli4_hba.max_cfg_param.xri_base) ||
13935 oxid > (lpfc_sli4_get_els_iocb_cnt(phba) +
13936 phba->sli4_hba.max_cfg_param.xri_base)) {
13937 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
13938 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
13939 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
13940 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
13941 }
6669f9bb 13942
5ffc266e
JS
13943 if (fctl & FC_FC_EX_CTX) {
13944 /* ABTS sent by responder to CT exchange, construction
13945 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
13946 * field and RX_ID from ABTS for RX_ID field.
13947 */
546fc854
JS
13948 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
13949 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
5ffc266e
JS
13950 } else {
13951 /* ABTS sent by initiator to CT exchange, construction
13952 * of BA_ACC will need to allocate a new XRI as for the
13953 * XRI_TAG and RX_ID fields.
13954 */
546fc854
JS
13955 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
13956 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, NO_XRI);
5ffc266e 13957 }
546fc854 13958 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
5ffc266e 13959
546fc854 13960 /* Xmit CT abts response on exchange <xid> */
6669f9bb 13961 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
546fc854
JS
13962 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
13963 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
13964
13965 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
13966 if (rc == IOCB_ERROR) {
13967 lpfc_printf_log(phba, KERN_ERR, LOG_ELS,
13968 "2925 Failed to issue CT ABTS RSP x%x on "
13969 "xri x%x, Data x%x\n",
13970 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
13971 phba->link_state);
13972 lpfc_sli_release_iocbq(phba, ctiocb);
13973 }
6669f9bb
JS
13974}
13975
13976/**
13977 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
13978 * @vport: Pointer to the vport on which this sequence was received
13979 * @dmabuf: pointer to a dmabuf that describes the FC sequence
13980 *
13981 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
13982 * receive sequence is only partially assembed by the driver, it shall abort
13983 * the partially assembled frames for the sequence. Otherwise, if the
13984 * unsolicited receive sequence has been completely assembled and passed to
13985 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
13986 * unsolicited sequence has been aborted. After that, it will issue a basic
13987 * accept to accept the abort.
13988 **/
13989void
13990lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
13991 struct hbq_dmabuf *dmabuf)
13992{
13993 struct lpfc_hba *phba = vport->phba;
13994 struct fc_frame_header fc_hdr;
5ffc266e 13995 uint32_t fctl;
6669f9bb
JS
13996 bool abts_par;
13997
6669f9bb
JS
13998 /* Make a copy of fc_hdr before the dmabuf being released */
13999 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 14000 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 14001
5ffc266e
JS
14002 if (fctl & FC_FC_EX_CTX) {
14003 /*
14004 * ABTS sent by responder to exchange, just free the buffer
14005 */
6669f9bb 14006 lpfc_in_buf_free(phba, &dmabuf->dbuf);
5ffc266e
JS
14007 } else {
14008 /*
14009 * ABTS sent by initiator to exchange, need to do cleanup
14010 */
14011 /* Try to abort partially assembled seq */
14012 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14013
14014 /* Send abort to ULP if partially seq abort failed */
14015 if (abts_par == false)
14016 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
14017 else
14018 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14019 }
6669f9bb 14020 /* Send basic accept (BA_ACC) to the abort requester */
546fc854 14021 lpfc_sli4_seq_abort_rsp(phba, &fc_hdr);
6669f9bb
JS
14022}
14023
4f774513
JS
14024/**
14025 * lpfc_seq_complete - Indicates if a sequence is complete
14026 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14027 *
14028 * This function checks the sequence, starting with the frame described by
14029 * @dmabuf, to see if all the frames associated with this sequence are present.
14030 * the frames associated with this sequence are linked to the @dmabuf using the
14031 * dbuf list. This function looks for two major things. 1) That the first frame
14032 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14033 * set. 3) That there are no holes in the sequence count. The function will
14034 * return 1 when the sequence is complete, otherwise it will return 0.
14035 **/
14036static int
14037lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14038{
14039 struct fc_frame_header *hdr;
14040 struct lpfc_dmabuf *d_buf;
14041 struct hbq_dmabuf *seq_dmabuf;
14042 uint32_t fctl;
14043 int seq_count = 0;
14044
14045 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14046 /* make sure first fame of sequence has a sequence count of zero */
14047 if (hdr->fh_seq_cnt != seq_count)
14048 return 0;
14049 fctl = (hdr->fh_f_ctl[0] << 16 |
14050 hdr->fh_f_ctl[1] << 8 |
14051 hdr->fh_f_ctl[2]);
14052 /* If last frame of sequence we can return success. */
14053 if (fctl & FC_FC_END_SEQ)
14054 return 1;
14055 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14056 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14057 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14058 /* If there is a hole in the sequence count then fail. */
eeead811 14059 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
14060 return 0;
14061 fctl = (hdr->fh_f_ctl[0] << 16 |
14062 hdr->fh_f_ctl[1] << 8 |
14063 hdr->fh_f_ctl[2]);
14064 /* If last frame of sequence we can return success. */
14065 if (fctl & FC_FC_END_SEQ)
14066 return 1;
14067 }
14068 return 0;
14069}
14070
14071/**
14072 * lpfc_prep_seq - Prep sequence for ULP processing
14073 * @vport: Pointer to the vport on which this sequence was received
14074 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14075 *
14076 * This function takes a sequence, described by a list of frames, and creates
14077 * a list of iocbq structures to describe the sequence. This iocbq list will be
14078 * used to issue to the generic unsolicited sequence handler. This routine
14079 * returns a pointer to the first iocbq in the list. If the function is unable
14080 * to allocate an iocbq then it throw out the received frames that were not
14081 * able to be described and return a pointer to the first iocbq. If unable to
14082 * allocate any iocbqs (including the first) this function will return NULL.
14083 **/
14084static struct lpfc_iocbq *
14085lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14086{
7851fe2c 14087 struct hbq_dmabuf *hbq_buf;
4f774513
JS
14088 struct lpfc_dmabuf *d_buf, *n_buf;
14089 struct lpfc_iocbq *first_iocbq, *iocbq;
14090 struct fc_frame_header *fc_hdr;
14091 uint32_t sid;
7851fe2c 14092 uint32_t len, tot_len;
eeead811 14093 struct ulp_bde64 *pbde;
4f774513
JS
14094
14095 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14096 /* remove from receive buffer list */
14097 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 14098 lpfc_update_rcv_time_stamp(vport);
4f774513 14099 /* get the Remote Port's SID */
6669f9bb 14100 sid = sli4_sid_from_fc_hdr(fc_hdr);
7851fe2c 14101 tot_len = 0;
4f774513
JS
14102 /* Get an iocbq struct to fill in. */
14103 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14104 if (first_iocbq) {
14105 /* Initialize the first IOCB. */
8fa38513 14106 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
14107 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14108 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
7851fe2c
JS
14109 first_iocbq->iocb.ulpContext = NO_XRI;
14110 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14111 be16_to_cpu(fc_hdr->fh_ox_id);
14112 /* iocbq is prepped for internal consumption. Physical vpi. */
14113 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14114 vport->phba->vpi_ids[vport->vpi];
4f774513
JS
14115 /* put the first buffer into the first IOCBq */
14116 first_iocbq->context2 = &seq_dmabuf->dbuf;
14117 first_iocbq->context3 = NULL;
14118 first_iocbq->iocb.ulpBdeCount = 1;
14119 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14120 LPFC_DATA_BUF_SIZE;
14121 first_iocbq->iocb.un.rcvels.remoteID = sid;
7851fe2c 14122 tot_len = bf_get(lpfc_rcqe_length,
4d9ab994 14123 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
7851fe2c 14124 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
4f774513
JS
14125 }
14126 iocbq = first_iocbq;
14127 /*
14128 * Each IOCBq can have two Buffers assigned, so go through the list
14129 * of buffers for this sequence and save two buffers in each IOCBq
14130 */
14131 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14132 if (!iocbq) {
14133 lpfc_in_buf_free(vport->phba, d_buf);
14134 continue;
14135 }
14136 if (!iocbq->context3) {
14137 iocbq->context3 = d_buf;
14138 iocbq->iocb.ulpBdeCount++;
eeead811
JS
14139 pbde = (struct ulp_bde64 *)
14140 &iocbq->iocb.unsli3.sli3Words[4];
14141 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
7851fe2c
JS
14142
14143 /* We need to get the size out of the right CQE */
14144 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14145 len = bf_get(lpfc_rcqe_length,
14146 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14147 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14148 tot_len += len;
4f774513
JS
14149 } else {
14150 iocbq = lpfc_sli_get_iocbq(vport->phba);
14151 if (!iocbq) {
14152 if (first_iocbq) {
14153 first_iocbq->iocb.ulpStatus =
14154 IOSTAT_FCP_RSP_ERROR;
14155 first_iocbq->iocb.un.ulpWord[4] =
14156 IOERR_NO_RESOURCES;
14157 }
14158 lpfc_in_buf_free(vport->phba, d_buf);
14159 continue;
14160 }
14161 iocbq->context2 = d_buf;
14162 iocbq->context3 = NULL;
14163 iocbq->iocb.ulpBdeCount = 1;
14164 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14165 LPFC_DATA_BUF_SIZE;
7851fe2c
JS
14166
14167 /* We need to get the size out of the right CQE */
14168 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14169 len = bf_get(lpfc_rcqe_length,
14170 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14171 tot_len += len;
14172 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14173
4f774513
JS
14174 iocbq->iocb.un.rcvels.remoteID = sid;
14175 list_add_tail(&iocbq->list, &first_iocbq->list);
14176 }
14177 }
14178 return first_iocbq;
14179}
14180
6669f9bb
JS
14181static void
14182lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14183 struct hbq_dmabuf *seq_dmabuf)
14184{
14185 struct fc_frame_header *fc_hdr;
14186 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14187 struct lpfc_hba *phba = vport->phba;
14188
14189 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14190 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14191 if (!iocbq) {
14192 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14193 "2707 Ring %d handler: Failed to allocate "
14194 "iocb Rctl x%x Type x%x received\n",
14195 LPFC_ELS_RING,
14196 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14197 return;
14198 }
14199 if (!lpfc_complete_unsol_iocb(phba,
14200 &phba->sli.ring[LPFC_ELS_RING],
14201 iocbq, fc_hdr->fh_r_ctl,
14202 fc_hdr->fh_type))
6d368e53 14203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6669f9bb
JS
14204 "2540 Ring %d handler: unexpected Rctl "
14205 "x%x Type x%x received\n",
14206 LPFC_ELS_RING,
14207 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14208
14209 /* Free iocb created in lpfc_prep_seq */
14210 list_for_each_entry_safe(curr_iocb, next_iocb,
14211 &iocbq->list, list) {
14212 list_del_init(&curr_iocb->list);
14213 lpfc_sli_release_iocbq(phba, curr_iocb);
14214 }
14215 lpfc_sli_release_iocbq(phba, iocbq);
14216}
14217
4f774513
JS
14218/**
14219 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14220 * @phba: Pointer to HBA context object.
14221 *
14222 * This function is called with no lock held. This function processes all
14223 * the received buffers and gives it to upper layers when a received buffer
14224 * indicates that it is the final frame in the sequence. The interrupt
14225 * service routine processes received buffers at interrupt contexts and adds
14226 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14227 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14228 * appropriate receive function when the final frame in a sequence is received.
14229 **/
4d9ab994
JS
14230void
14231lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14232 struct hbq_dmabuf *dmabuf)
4f774513 14233{
4d9ab994 14234 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
14235 struct fc_frame_header *fc_hdr;
14236 struct lpfc_vport *vport;
14237 uint32_t fcfi;
4f774513 14238
4f774513 14239 /* Process each received buffer */
4d9ab994
JS
14240 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14241 /* check to see if this a valid type of frame */
14242 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14243 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14244 return;
14245 }
7851fe2c
JS
14246 if ((bf_get(lpfc_cqe_code,
14247 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14248 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14249 &dmabuf->cq_event.cqe.rcqe_cmpl);
14250 else
14251 fcfi = bf_get(lpfc_rcqe_fcf_id,
14252 &dmabuf->cq_event.cqe.rcqe_cmpl);
4d9ab994 14253 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
c868595d 14254 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
4d9ab994
JS
14255 /* throw out the frame */
14256 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14257 return;
14258 }
6669f9bb
JS
14259 /* Handle the basic abort sequence (BA_ABTS) event */
14260 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14261 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14262 return;
14263 }
14264
4d9ab994
JS
14265 /* Link this frame */
14266 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14267 if (!seq_dmabuf) {
14268 /* unable to add frame to vport - throw it out */
14269 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14270 return;
14271 }
14272 /* If not last frame in sequence continue processing frames. */
def9c7a9 14273 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 14274 return;
def9c7a9 14275
6669f9bb
JS
14276 /* Send the complete sequence to the upper layer protocol */
14277 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 14278}
6fb120a7
JS
14279
14280/**
14281 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14282 * @phba: pointer to lpfc hba data structure.
14283 *
14284 * This routine is invoked to post rpi header templates to the
14285 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
14286 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14287 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
14288 *
14289 * This routine does not require any locks. It's usage is expected
14290 * to be driver load or reset recovery when the driver is
14291 * sequential.
14292 *
14293 * Return codes
af901ca1 14294 * 0 - successful
d439d286 14295 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
14296 * When this error occurs, the driver is not guaranteed
14297 * to have any rpi regions posted to the device and
14298 * must either attempt to repost the regions or take a
14299 * fatal error.
14300 **/
14301int
14302lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14303{
14304 struct lpfc_rpi_hdr *rpi_page;
14305 uint32_t rc = 0;
6d368e53
JS
14306 uint16_t lrpi = 0;
14307
14308 /* SLI4 ports that support extents do not require RPI headers. */
14309 if (!phba->sli4_hba.rpi_hdrs_in_use)
14310 goto exit;
14311 if (phba->sli4_hba.extents_in_use)
14312 return -EIO;
6fb120a7 14313
6fb120a7 14314 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
6d368e53
JS
14315 /*
14316 * Assign the rpi headers a physical rpi only if the driver
14317 * has not initialized those resources. A port reset only
14318 * needs the headers posted.
14319 */
14320 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14321 LPFC_RPI_RSRC_RDY)
14322 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14323
6fb120a7
JS
14324 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14325 if (rc != MBX_SUCCESS) {
14326 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14327 "2008 Error %d posting all rpi "
14328 "headers\n", rc);
14329 rc = -EIO;
14330 break;
14331 }
14332 }
14333
6d368e53
JS
14334 exit:
14335 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14336 LPFC_RPI_RSRC_RDY);
6fb120a7
JS
14337 return rc;
14338}
14339
14340/**
14341 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14342 * @phba: pointer to lpfc hba data structure.
14343 * @rpi_page: pointer to the rpi memory region.
14344 *
14345 * This routine is invoked to post a single rpi header to the
14346 * HBA consistent with the SLI-4 interface spec. This memory region
14347 * maps up to 64 rpi context regions.
14348 *
14349 * Return codes
af901ca1 14350 * 0 - successful
d439d286
JS
14351 * -ENOMEM - No available memory
14352 * -EIO - The mailbox failed to complete successfully.
6fb120a7
JS
14353 **/
14354int
14355lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14356{
14357 LPFC_MBOXQ_t *mboxq;
14358 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14359 uint32_t rc = 0;
6fb120a7
JS
14360 uint32_t shdr_status, shdr_add_status;
14361 union lpfc_sli4_cfg_shdr *shdr;
14362
6d368e53
JS
14363 /* SLI4 ports that support extents do not require RPI headers. */
14364 if (!phba->sli4_hba.rpi_hdrs_in_use)
14365 return rc;
14366 if (phba->sli4_hba.extents_in_use)
14367 return -EIO;
14368
6fb120a7
JS
14369 /* The port is notified of the header region via a mailbox command. */
14370 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14371 if (!mboxq) {
14372 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14373 "2001 Unable to allocate memory for issuing "
14374 "SLI_CONFIG_SPECIAL mailbox command\n");
14375 return -ENOMEM;
14376 }
14377
14378 /* Post all rpi memory regions to the port. */
14379 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
6fb120a7
JS
14380 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14381 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14382 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
fedd3b7b
JS
14383 sizeof(struct lpfc_sli4_cfg_mhdr),
14384 LPFC_SLI4_MBX_EMBED);
6d368e53
JS
14385
14386
14387 /* Post the physical rpi to the port for this rpi header. */
6fb120a7
JS
14388 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14389 rpi_page->start_rpi);
6d368e53
JS
14390 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14391 hdr_tmpl, rpi_page->page_count);
14392
6fb120a7
JS
14393 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14394 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 14395 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
14396 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14397 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14398 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14399 if (rc != MBX_TIMEOUT)
14400 mempool_free(mboxq, phba->mbox_mem_pool);
14401 if (shdr_status || shdr_add_status || rc) {
14402 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14403 "2514 POST_RPI_HDR mailbox failed with "
14404 "status x%x add_status x%x, mbx status x%x\n",
14405 shdr_status, shdr_add_status, rc);
14406 rc = -ENXIO;
14407 }
14408 return rc;
14409}
14410
14411/**
14412 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14413 * @phba: pointer to lpfc hba data structure.
14414 *
14415 * This routine is invoked to post rpi header templates to the
14416 * HBA consistent with the SLI-4 interface spec. This routine
49198b37
JS
14417 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14418 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
6fb120a7
JS
14419 *
14420 * Returns
af901ca1 14421 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
14422 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14423 **/
14424int
14425lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14426{
6d368e53
JS
14427 unsigned long rpi;
14428 uint16_t max_rpi, rpi_limit;
14429 uint16_t rpi_remaining, lrpi = 0;
6fb120a7
JS
14430 struct lpfc_rpi_hdr *rpi_hdr;
14431
14432 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
6fb120a7
JS
14433 rpi_limit = phba->sli4_hba.next_rpi;
14434
14435 /*
6d368e53
JS
14436 * Fetch the next logical rpi. Because this index is logical,
14437 * the driver starts at 0 each time.
6fb120a7
JS
14438 */
14439 spin_lock_irq(&phba->hbalock);
6d368e53
JS
14440 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
14441 if (rpi >= rpi_limit)
6fb120a7
JS
14442 rpi = LPFC_RPI_ALLOC_ERROR;
14443 else {
14444 set_bit(rpi, phba->sli4_hba.rpi_bmask);
14445 phba->sli4_hba.max_cfg_param.rpi_used++;
14446 phba->sli4_hba.rpi_count++;
14447 }
14448
14449 /*
14450 * Don't try to allocate more rpi header regions if the device limit
6d368e53 14451 * has been exhausted.
6fb120a7
JS
14452 */
14453 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
14454 (phba->sli4_hba.rpi_count >= max_rpi)) {
14455 spin_unlock_irq(&phba->hbalock);
14456 return rpi;
14457 }
14458
6d368e53
JS
14459 /*
14460 * RPI header postings are not required for SLI4 ports capable of
14461 * extents.
14462 */
14463 if (!phba->sli4_hba.rpi_hdrs_in_use) {
14464 spin_unlock_irq(&phba->hbalock);
14465 return rpi;
14466 }
14467
6fb120a7
JS
14468 /*
14469 * If the driver is running low on rpi resources, allocate another
14470 * page now. Note that the next_rpi value is used because
14471 * it represents how many are actually in use whereas max_rpi notes
14472 * how many are supported max by the device.
14473 */
6d368e53 14474 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
6fb120a7
JS
14475 spin_unlock_irq(&phba->hbalock);
14476 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
14477 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
14478 if (!rpi_hdr) {
14479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14480 "2002 Error Could not grow rpi "
14481 "count\n");
14482 } else {
6d368e53
JS
14483 lrpi = rpi_hdr->start_rpi;
14484 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
6fb120a7
JS
14485 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
14486 }
14487 }
14488
14489 return rpi;
14490}
14491
d7c47992
JS
14492/**
14493 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14494 * @phba: pointer to lpfc hba data structure.
14495 *
14496 * This routine is invoked to release an rpi to the pool of
14497 * available rpis maintained by the driver.
14498 **/
14499void
14500__lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14501{
14502 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
14503 phba->sli4_hba.rpi_count--;
14504 phba->sli4_hba.max_cfg_param.rpi_used--;
14505 }
14506}
14507
6fb120a7
JS
14508/**
14509 * lpfc_sli4_free_rpi - Release an rpi for reuse.
14510 * @phba: pointer to lpfc hba data structure.
14511 *
14512 * This routine is invoked to release an rpi to the pool of
14513 * available rpis maintained by the driver.
14514 **/
14515void
14516lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
14517{
14518 spin_lock_irq(&phba->hbalock);
d7c47992 14519 __lpfc_sli4_free_rpi(phba, rpi);
6fb120a7
JS
14520 spin_unlock_irq(&phba->hbalock);
14521}
14522
14523/**
14524 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
14525 * @phba: pointer to lpfc hba data structure.
14526 *
14527 * This routine is invoked to remove the memory region that
14528 * provided rpi via a bitmask.
14529 **/
14530void
14531lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
14532{
14533 kfree(phba->sli4_hba.rpi_bmask);
6d368e53
JS
14534 kfree(phba->sli4_hba.rpi_ids);
14535 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
6fb120a7
JS
14536}
14537
14538/**
14539 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
14540 * @phba: pointer to lpfc hba data structure.
14541 *
14542 * This routine is invoked to remove the memory region that
14543 * provided rpi via a bitmask.
14544 **/
14545int
14546lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
14547{
14548 LPFC_MBOXQ_t *mboxq;
14549 struct lpfc_hba *phba = ndlp->phba;
14550 int rc;
14551
14552 /* The port is notified of the header region via a mailbox command. */
14553 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14554 if (!mboxq)
14555 return -ENOMEM;
14556
14557 /* Post all rpi memory regions to the port. */
14558 lpfc_resume_rpi(mboxq, ndlp);
14559 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14560 if (rc == MBX_NOT_FINISHED) {
14561 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14562 "2010 Resume RPI Mailbox failed "
14563 "status %d, mbxStatus x%x\n", rc,
14564 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
14565 mempool_free(mboxq, phba->mbox_mem_pool);
14566 return -EIO;
14567 }
14568 return 0;
14569}
14570
14571/**
14572 * lpfc_sli4_init_vpi - Initialize a vpi with the port
76a95d75 14573 * @vport: Pointer to the vport for which the vpi is being initialized
6fb120a7 14574 *
76a95d75 14575 * This routine is invoked to activate a vpi with the port.
6fb120a7
JS
14576 *
14577 * Returns:
14578 * 0 success
14579 * -Evalue otherwise
14580 **/
14581int
76a95d75 14582lpfc_sli4_init_vpi(struct lpfc_vport *vport)
6fb120a7
JS
14583{
14584 LPFC_MBOXQ_t *mboxq;
14585 int rc = 0;
6a9c52cf 14586 int retval = MBX_SUCCESS;
6fb120a7 14587 uint32_t mbox_tmo;
76a95d75 14588 struct lpfc_hba *phba = vport->phba;
6fb120a7
JS
14589 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14590 if (!mboxq)
14591 return -ENOMEM;
76a95d75 14592 lpfc_init_vpi(phba, mboxq, vport->vpi);
a183a15f 14593 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
6fb120a7 14594 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7 14595 if (rc != MBX_SUCCESS) {
76a95d75 14596 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
6fb120a7
JS
14597 "2022 INIT VPI Mailbox failed "
14598 "status %d, mbxStatus x%x\n", rc,
14599 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 14600 retval = -EIO;
6fb120a7 14601 }
6a9c52cf 14602 if (rc != MBX_TIMEOUT)
76a95d75 14603 mempool_free(mboxq, vport->phba->mbox_mem_pool);
6a9c52cf
JS
14604
14605 return retval;
6fb120a7
JS
14606}
14607
14608/**
14609 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
14610 * @phba: pointer to lpfc hba data structure.
14611 * @mboxq: Pointer to mailbox object.
14612 *
14613 * This routine is invoked to manually add a single FCF record. The caller
14614 * must pass a completely initialized FCF_Record. This routine takes
14615 * care of the nonembedded mailbox operations.
14616 **/
14617static void
14618lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
14619{
14620 void *virt_addr;
14621 union lpfc_sli4_cfg_shdr *shdr;
14622 uint32_t shdr_status, shdr_add_status;
14623
14624 virt_addr = mboxq->sge_array->addr[0];
14625 /* The IOCTL status is embedded in the mailbox subheader. */
14626 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
14627 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14628 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14629
14630 if ((shdr_status || shdr_add_status) &&
14631 (shdr_status != STATUS_FCF_IN_USE))
14632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14633 "2558 ADD_FCF_RECORD mailbox failed with "
14634 "status x%x add_status x%x\n",
14635 shdr_status, shdr_add_status);
14636
14637 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14638}
14639
14640/**
14641 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
14642 * @phba: pointer to lpfc hba data structure.
14643 * @fcf_record: pointer to the initialized fcf record to add.
14644 *
14645 * This routine is invoked to manually add a single FCF record. The caller
14646 * must pass a completely initialized FCF_Record. This routine takes
14647 * care of the nonembedded mailbox operations.
14648 **/
14649int
14650lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
14651{
14652 int rc = 0;
14653 LPFC_MBOXQ_t *mboxq;
14654 uint8_t *bytep;
14655 void *virt_addr;
14656 dma_addr_t phys_addr;
14657 struct lpfc_mbx_sge sge;
14658 uint32_t alloc_len, req_len;
14659 uint32_t fcfindex;
14660
14661 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14662 if (!mboxq) {
14663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14664 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
14665 return -ENOMEM;
14666 }
14667
14668 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
14669 sizeof(uint32_t);
14670
14671 /* Allocate DMA memory and set up the non-embedded mailbox command */
14672 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14673 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
14674 req_len, LPFC_SLI4_MBX_NEMBED);
14675 if (alloc_len < req_len) {
14676 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14677 "2523 Allocated DMA memory size (x%x) is "
14678 "less than the requested DMA memory "
14679 "size (x%x)\n", alloc_len, req_len);
14680 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14681 return -ENOMEM;
14682 }
14683
14684 /*
14685 * Get the first SGE entry from the non-embedded DMA memory. This
14686 * routine only uses a single SGE.
14687 */
14688 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
14689 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
14690 virt_addr = mboxq->sge_array->addr[0];
14691 /*
14692 * Configure the FCF record for FCFI 0. This is the driver's
14693 * hardcoded default and gets used in nonFIP mode.
14694 */
14695 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
14696 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
14697 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
14698
14699 /*
14700 * Copy the fcf_index and the FCF Record Data. The data starts after
14701 * the FCoE header plus word10. The data copy needs to be endian
14702 * correct.
14703 */
14704 bytep += sizeof(uint32_t);
14705 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
14706 mboxq->vport = phba->pport;
14707 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
14708 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14709 if (rc == MBX_NOT_FINISHED) {
14710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14711 "2515 ADD_FCF_RECORD mailbox failed with "
14712 "status 0x%x\n", rc);
14713 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14714 rc = -EIO;
14715 } else
14716 rc = 0;
14717
14718 return rc;
14719}
14720
14721/**
14722 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
14723 * @phba: pointer to lpfc hba data structure.
14724 * @fcf_record: pointer to the fcf record to write the default data.
14725 * @fcf_index: FCF table entry index.
14726 *
14727 * This routine is invoked to build the driver's default FCF record. The
14728 * values used are hardcoded. This routine handles memory initialization.
14729 *
14730 **/
14731void
14732lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
14733 struct fcf_record *fcf_record,
14734 uint16_t fcf_index)
14735{
14736 memset(fcf_record, 0, sizeof(struct fcf_record));
14737 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
14738 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
14739 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
14740 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
14741 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
14742 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
14743 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
14744 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
14745 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
14746 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
14747 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
14748 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
14749 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 14750 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
14751 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
14752 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
14753 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
14754 /* Set the VLAN bit map */
14755 if (phba->valid_vlan) {
14756 fcf_record->vlan_bitmap[phba->vlan_id / 8]
14757 = 1 << (phba->vlan_id % 8);
14758 }
14759}
14760
14761/**
0c9ab6f5 14762 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
6fb120a7
JS
14763 * @phba: pointer to lpfc hba data structure.
14764 * @fcf_index: FCF table entry offset.
14765 *
0c9ab6f5
JS
14766 * This routine is invoked to scan the entire FCF table by reading FCF
14767 * record and processing it one at a time starting from the @fcf_index
14768 * for initial FCF discovery or fast FCF failover rediscovery.
14769 *
25985edc 14770 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5 14771 * otherwise.
6fb120a7
JS
14772 **/
14773int
0c9ab6f5 14774lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
6fb120a7
JS
14775{
14776 int rc = 0, error;
14777 LPFC_MBOXQ_t *mboxq;
6fb120a7 14778
32b9793f 14779 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
14780 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14781 if (!mboxq) {
14782 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14783 "2000 Failed to allocate mbox for "
14784 "READ_FCF cmd\n");
4d9ab994 14785 error = -ENOMEM;
0c9ab6f5 14786 goto fail_fcf_scan;
6fb120a7 14787 }
ecfd03c6 14788 /* Construct the read FCF record mailbox command */
0c9ab6f5 14789 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
ecfd03c6
JS
14790 if (rc) {
14791 error = -EINVAL;
0c9ab6f5 14792 goto fail_fcf_scan;
6fb120a7 14793 }
ecfd03c6 14794 /* Issue the mailbox command asynchronously */
6fb120a7 14795 mboxq->vport = phba->pport;
0c9ab6f5 14796 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
a93ff37a
JS
14797
14798 spin_lock_irq(&phba->hbalock);
14799 phba->hba_flag |= FCF_TS_INPROG;
14800 spin_unlock_irq(&phba->hbalock);
14801
6fb120a7 14802 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 14803 if (rc == MBX_NOT_FINISHED)
6fb120a7 14804 error = -EIO;
ecfd03c6 14805 else {
38b92ef8
JS
14806 /* Reset eligible FCF count for new scan */
14807 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
999d813f 14808 phba->fcf.eligible_fcf_cnt = 0;
6fb120a7 14809 error = 0;
32b9793f 14810 }
0c9ab6f5 14811fail_fcf_scan:
4d9ab994
JS
14812 if (error) {
14813 if (mboxq)
14814 lpfc_sli4_mbox_cmd_free(phba, mboxq);
a93ff37a 14815 /* FCF scan failed, clear FCF_TS_INPROG flag */
4d9ab994 14816 spin_lock_irq(&phba->hbalock);
a93ff37a 14817 phba->hba_flag &= ~FCF_TS_INPROG;
4d9ab994
JS
14818 spin_unlock_irq(&phba->hbalock);
14819 }
6fb120a7
JS
14820 return error;
14821}
a0c87cbd 14822
0c9ab6f5 14823/**
a93ff37a 14824 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
0c9ab6f5
JS
14825 * @phba: pointer to lpfc hba data structure.
14826 * @fcf_index: FCF table entry offset.
14827 *
14828 * This routine is invoked to read an FCF record indicated by @fcf_index
a93ff37a 14829 * and to use it for FLOGI roundrobin FCF failover.
0c9ab6f5 14830 *
25985edc 14831 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
14832 * otherwise.
14833 **/
14834int
14835lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14836{
14837 int rc = 0, error;
14838 LPFC_MBOXQ_t *mboxq;
14839
14840 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14841 if (!mboxq) {
14842 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14843 "2763 Failed to allocate mbox for "
14844 "READ_FCF cmd\n");
14845 error = -ENOMEM;
14846 goto fail_fcf_read;
14847 }
14848 /* Construct the read FCF record mailbox command */
14849 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14850 if (rc) {
14851 error = -EINVAL;
14852 goto fail_fcf_read;
14853 }
14854 /* Issue the mailbox command asynchronously */
14855 mboxq->vport = phba->pport;
14856 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
14857 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14858 if (rc == MBX_NOT_FINISHED)
14859 error = -EIO;
14860 else
14861 error = 0;
14862
14863fail_fcf_read:
14864 if (error && mboxq)
14865 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14866 return error;
14867}
14868
14869/**
14870 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
14871 * @phba: pointer to lpfc hba data structure.
14872 * @fcf_index: FCF table entry offset.
14873 *
14874 * This routine is invoked to read an FCF record indicated by @fcf_index to
a93ff37a 14875 * determine whether it's eligible for FLOGI roundrobin failover list.
0c9ab6f5 14876 *
25985edc 14877 * Return 0 if the mailbox command is submitted successfully, none 0
0c9ab6f5
JS
14878 * otherwise.
14879 **/
14880int
14881lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
14882{
14883 int rc = 0, error;
14884 LPFC_MBOXQ_t *mboxq;
14885
14886 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14887 if (!mboxq) {
14888 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
14889 "2758 Failed to allocate mbox for "
14890 "READ_FCF cmd\n");
14891 error = -ENOMEM;
14892 goto fail_fcf_read;
14893 }
14894 /* Construct the read FCF record mailbox command */
14895 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
14896 if (rc) {
14897 error = -EINVAL;
14898 goto fail_fcf_read;
14899 }
14900 /* Issue the mailbox command asynchronously */
14901 mboxq->vport = phba->pport;
14902 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
14903 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
14904 if (rc == MBX_NOT_FINISHED)
14905 error = -EIO;
14906 else
14907 error = 0;
14908
14909fail_fcf_read:
14910 if (error && mboxq)
14911 lpfc_sli4_mbox_cmd_free(phba, mboxq);
14912 return error;
14913}
14914
7d791df7
JS
14915/**
14916 * lpfc_check_next_fcf_pri
14917 * phba pointer to the lpfc_hba struct for this port.
14918 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
14919 * routine when the rr_bmask is empty. The FCF indecies are put into the
14920 * rr_bmask based on their priority level. Starting from the highest priority
14921 * to the lowest. The most likely FCF candidate will be in the highest
14922 * priority group. When this routine is called it searches the fcf_pri list for
14923 * next lowest priority group and repopulates the rr_bmask with only those
14924 * fcf_indexes.
14925 * returns:
14926 * 1=success 0=failure
14927 **/
14928int
14929lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
14930{
14931 uint16_t next_fcf_pri;
14932 uint16_t last_index;
14933 struct lpfc_fcf_pri *fcf_pri;
14934 int rc;
14935 int ret = 0;
14936
14937 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
14938 LPFC_SLI4_FCF_TBL_INDX_MAX);
14939 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
14940 "3060 Last IDX %d\n", last_index);
14941 if (list_empty(&phba->fcf.fcf_pri_list)) {
14942 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
14943 "3061 Last IDX %d\n", last_index);
14944 return 0; /* Empty rr list */
14945 }
14946 next_fcf_pri = 0;
14947 /*
14948 * Clear the rr_bmask and set all of the bits that are at this
14949 * priority.
14950 */
14951 memset(phba->fcf.fcf_rr_bmask, 0,
14952 sizeof(*phba->fcf.fcf_rr_bmask));
14953 spin_lock_irq(&phba->hbalock);
14954 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14955 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
14956 continue;
14957 /*
14958 * the 1st priority that has not FLOGI failed
14959 * will be the highest.
14960 */
14961 if (!next_fcf_pri)
14962 next_fcf_pri = fcf_pri->fcf_rec.priority;
14963 spin_unlock_irq(&phba->hbalock);
14964 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14965 rc = lpfc_sli4_fcf_rr_index_set(phba,
14966 fcf_pri->fcf_rec.fcf_index);
14967 if (rc)
14968 return 0;
14969 }
14970 spin_lock_irq(&phba->hbalock);
14971 }
14972 /*
14973 * if next_fcf_pri was not set above and the list is not empty then
14974 * we have failed flogis on all of them. So reset flogi failed
14975 * and start at the begining.
14976 */
14977 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
14978 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
14979 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
14980 /*
14981 * the 1st priority that has not FLOGI failed
14982 * will be the highest.
14983 */
14984 if (!next_fcf_pri)
14985 next_fcf_pri = fcf_pri->fcf_rec.priority;
14986 spin_unlock_irq(&phba->hbalock);
14987 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
14988 rc = lpfc_sli4_fcf_rr_index_set(phba,
14989 fcf_pri->fcf_rec.fcf_index);
14990 if (rc)
14991 return 0;
14992 }
14993 spin_lock_irq(&phba->hbalock);
14994 }
14995 } else
14996 ret = 1;
14997 spin_unlock_irq(&phba->hbalock);
14998
14999 return ret;
15000}
0c9ab6f5
JS
15001/**
15002 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15003 * @phba: pointer to lpfc hba data structure.
15004 *
15005 * This routine is to get the next eligible FCF record index in a round
15006 * robin fashion. If the next eligible FCF record index equals to the
a93ff37a 15007 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
0c9ab6f5
JS
15008 * shall be returned, otherwise, the next eligible FCF record's index
15009 * shall be returned.
15010 **/
15011uint16_t
15012lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15013{
15014 uint16_t next_fcf_index;
15015
3804dc84 15016 /* Search start from next bit of currently registered FCF index */
7d791df7 15017next_priority:
3804dc84
JS
15018 next_fcf_index = (phba->fcf.current_rec.fcf_indx + 1) %
15019 LPFC_SLI4_FCF_TBL_INDX_MAX;
0c9ab6f5
JS
15020 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15021 LPFC_SLI4_FCF_TBL_INDX_MAX,
3804dc84
JS
15022 next_fcf_index);
15023
0c9ab6f5 15024 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
7d791df7
JS
15025 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15026 /*
15027 * If we have wrapped then we need to clear the bits that
15028 * have been tested so that we can detect when we should
15029 * change the priority level.
15030 */
0c9ab6f5
JS
15031 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15032 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
7d791df7
JS
15033 }
15034
3804dc84
JS
15035
15036 /* Check roundrobin failover list empty condition */
7d791df7
JS
15037 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15038 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15039 /*
15040 * If next fcf index is not found check if there are lower
15041 * Priority level fcf's in the fcf_priority list.
15042 * Set up the rr_bmask with all of the avaiable fcf bits
15043 * at that level and continue the selection process.
15044 */
15045 if (lpfc_check_next_fcf_pri_level(phba))
15046 goto next_priority;
3804dc84
JS
15047 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15048 "2844 No roundrobin failover FCF available\n");
7d791df7
JS
15049 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15050 return LPFC_FCOE_FCF_NEXT_NONE;
15051 else {
15052 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15053 "3063 Only FCF available idx %d, flag %x\n",
15054 next_fcf_index,
15055 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15056 return next_fcf_index;
15057 }
3804dc84
JS
15058 }
15059
7d791df7
JS
15060 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15061 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15062 LPFC_FCF_FLOGI_FAILED)
15063 goto next_priority;
15064
3804dc84 15065 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a
JS
15066 "2845 Get next roundrobin failover FCF (x%x)\n",
15067 next_fcf_index);
15068
0c9ab6f5
JS
15069 return next_fcf_index;
15070}
15071
15072/**
15073 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15074 * @phba: pointer to lpfc hba data structure.
15075 *
15076 * This routine sets the FCF record index in to the eligible bmask for
a93ff37a 15077 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
15078 * does not go beyond the range of the driver allocated bmask dimension
15079 * before setting the bit.
15080 *
15081 * Returns 0 if the index bit successfully set, otherwise, it returns
15082 * -EINVAL.
15083 **/
15084int
15085lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15086{
15087 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15088 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
15089 "2610 FCF (x%x) reached driver's book "
15090 "keeping dimension:x%x\n",
0c9ab6f5
JS
15091 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15092 return -EINVAL;
15093 }
15094 /* Set the eligible FCF record index bmask */
15095 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15096
3804dc84 15097 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15098 "2790 Set FCF (x%x) to roundrobin FCF failover "
3804dc84
JS
15099 "bmask\n", fcf_index);
15100
0c9ab6f5
JS
15101 return 0;
15102}
15103
15104/**
3804dc84 15105 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
0c9ab6f5
JS
15106 * @phba: pointer to lpfc hba data structure.
15107 *
15108 * This routine clears the FCF record index from the eligible bmask for
a93ff37a 15109 * roundrobin failover search. It checks to make sure that the index
0c9ab6f5
JS
15110 * does not go beyond the range of the driver allocated bmask dimension
15111 * before clearing the bit.
15112 **/
15113void
15114lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15115{
7d791df7 15116 struct lpfc_fcf_pri *fcf_pri;
0c9ab6f5
JS
15117 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15118 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
a93ff37a
JS
15119 "2762 FCF (x%x) reached driver's book "
15120 "keeping dimension:x%x\n",
0c9ab6f5
JS
15121 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15122 return;
15123 }
15124 /* Clear the eligible FCF record index bmask */
7d791df7
JS
15125 spin_lock_irq(&phba->hbalock);
15126 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15127 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15128 list_del_init(&fcf_pri->list);
15129 break;
15130 }
15131 }
15132 spin_unlock_irq(&phba->hbalock);
0c9ab6f5 15133 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
3804dc84
JS
15134
15135 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15136 "2791 Clear FCF (x%x) from roundrobin failover "
3804dc84 15137 "bmask\n", fcf_index);
0c9ab6f5
JS
15138}
15139
ecfd03c6
JS
15140/**
15141 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15142 * @phba: pointer to lpfc hba data structure.
15143 *
15144 * This routine is the completion routine for the rediscover FCF table mailbox
15145 * command. If the mailbox command returned failure, it will try to stop the
15146 * FCF rediscover wait timer.
15147 **/
15148void
15149lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15150{
15151 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15152 uint32_t shdr_status, shdr_add_status;
15153
15154 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15155
15156 shdr_status = bf_get(lpfc_mbox_hdr_status,
15157 &redisc_fcf->header.cfg_shdr.response);
15158 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15159 &redisc_fcf->header.cfg_shdr.response);
15160 if (shdr_status || shdr_add_status) {
0c9ab6f5 15161 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
ecfd03c6
JS
15162 "2746 Requesting for FCF rediscovery failed "
15163 "status x%x add_status x%x\n",
15164 shdr_status, shdr_add_status);
0c9ab6f5 15165 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
fc2b989b 15166 spin_lock_irq(&phba->hbalock);
0c9ab6f5 15167 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
fc2b989b
JS
15168 spin_unlock_irq(&phba->hbalock);
15169 /*
15170 * CVL event triggered FCF rediscover request failed,
15171 * last resort to re-try current registered FCF entry.
15172 */
15173 lpfc_retry_pport_discovery(phba);
15174 } else {
15175 spin_lock_irq(&phba->hbalock);
0c9ab6f5 15176 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
fc2b989b
JS
15177 spin_unlock_irq(&phba->hbalock);
15178 /*
15179 * DEAD FCF event triggered FCF rediscover request
15180 * failed, last resort to fail over as a link down
15181 * to FCF registration.
15182 */
15183 lpfc_sli4_fcf_dead_failthrough(phba);
15184 }
0c9ab6f5
JS
15185 } else {
15186 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
a93ff37a 15187 "2775 Start FCF rediscover quiescent timer\n");
ecfd03c6
JS
15188 /*
15189 * Start FCF rediscovery wait timer for pending FCF
15190 * before rescan FCF record table.
15191 */
15192 lpfc_fcf_redisc_wait_start_timer(phba);
0c9ab6f5 15193 }
ecfd03c6
JS
15194
15195 mempool_free(mbox, phba->mbox_mem_pool);
15196}
15197
15198/**
3804dc84 15199 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
ecfd03c6
JS
15200 * @phba: pointer to lpfc hba data structure.
15201 *
15202 * This routine is invoked to request for rediscovery of the entire FCF table
15203 * by the port.
15204 **/
15205int
15206lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15207{
15208 LPFC_MBOXQ_t *mbox;
15209 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15210 int rc, length;
15211
0c9ab6f5
JS
15212 /* Cancel retry delay timers to all vports before FCF rediscover */
15213 lpfc_cancel_all_vport_retry_delay_timer(phba);
15214
ecfd03c6
JS
15215 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15216 if (!mbox) {
15217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15218 "2745 Failed to allocate mbox for "
15219 "requesting FCF rediscover.\n");
15220 return -ENOMEM;
15221 }
15222
15223 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15224 sizeof(struct lpfc_sli4_cfg_mhdr));
15225 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15226 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15227 length, LPFC_SLI4_MBX_EMBED);
15228
15229 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15230 /* Set count to 0 for invalidating the entire FCF database */
15231 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15232
15233 /* Issue the mailbox command asynchronously */
15234 mbox->vport = phba->pport;
15235 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15236 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15237
15238 if (rc == MBX_NOT_FINISHED) {
15239 mempool_free(mbox, phba->mbox_mem_pool);
15240 return -EIO;
15241 }
15242 return 0;
15243}
15244
fc2b989b
JS
15245/**
15246 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15247 * @phba: pointer to lpfc hba data structure.
15248 *
15249 * This function is the failover routine as a last resort to the FCF DEAD
15250 * event when driver failed to perform fast FCF failover.
15251 **/
15252void
15253lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15254{
15255 uint32_t link_state;
15256
15257 /*
15258 * Last resort as FCF DEAD event failover will treat this as
15259 * a link down, but save the link state because we don't want
15260 * it to be changed to Link Down unless it is already down.
15261 */
15262 link_state = phba->link_state;
15263 lpfc_linkdown(phba);
15264 phba->link_state = link_state;
15265
15266 /* Unregister FCF if no devices connected to it */
15267 lpfc_unregister_unused_fcf(phba);
15268}
15269
a0c87cbd 15270/**
026abb87 15271 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
a0c87cbd 15272 * @phba: pointer to lpfc hba data structure.
026abb87 15273 * @rgn23_data: pointer to configure region 23 data.
a0c87cbd 15274 *
026abb87
JS
15275 * This function gets SLI3 port configure region 23 data through memory dump
15276 * mailbox command. When it successfully retrieves data, the size of the data
15277 * will be returned, otherwise, 0 will be returned.
a0c87cbd 15278 **/
026abb87
JS
15279static uint32_t
15280lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
a0c87cbd
JS
15281{
15282 LPFC_MBOXQ_t *pmb = NULL;
15283 MAILBOX_t *mb;
026abb87 15284 uint32_t offset = 0;
a0c87cbd
JS
15285 int rc;
15286
026abb87
JS
15287 if (!rgn23_data)
15288 return 0;
15289
a0c87cbd
JS
15290 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15291 if (!pmb) {
15292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
026abb87
JS
15293 "2600 failed to allocate mailbox memory\n");
15294 return 0;
a0c87cbd
JS
15295 }
15296 mb = &pmb->u.mb;
15297
a0c87cbd
JS
15298 do {
15299 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15300 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15301
15302 if (rc != MBX_SUCCESS) {
15303 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
026abb87
JS
15304 "2601 failed to read config "
15305 "region 23, rc 0x%x Status 0x%x\n",
15306 rc, mb->mbxStatus);
a0c87cbd
JS
15307 mb->un.varDmp.word_cnt = 0;
15308 }
15309 /*
15310 * dump mem may return a zero when finished or we got a
15311 * mailbox error, either way we are done.
15312 */
15313 if (mb->un.varDmp.word_cnt == 0)
15314 break;
15315 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15316 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15317
15318 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
026abb87
JS
15319 rgn23_data + offset,
15320 mb->un.varDmp.word_cnt);
a0c87cbd
JS
15321 offset += mb->un.varDmp.word_cnt;
15322 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15323
026abb87
JS
15324 mempool_free(pmb, phba->mbox_mem_pool);
15325 return offset;
15326}
15327
15328/**
15329 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15330 * @phba: pointer to lpfc hba data structure.
15331 * @rgn23_data: pointer to configure region 23 data.
15332 *
15333 * This function gets SLI4 port configure region 23 data through memory dump
15334 * mailbox command. When it successfully retrieves data, the size of the data
15335 * will be returned, otherwise, 0 will be returned.
15336 **/
15337static uint32_t
15338lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15339{
15340 LPFC_MBOXQ_t *mboxq = NULL;
15341 struct lpfc_dmabuf *mp = NULL;
15342 struct lpfc_mqe *mqe;
15343 uint32_t data_length = 0;
15344 int rc;
15345
15346 if (!rgn23_data)
15347 return 0;
15348
15349 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15350 if (!mboxq) {
15351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15352 "3105 failed to allocate mailbox memory\n");
15353 return 0;
15354 }
15355
15356 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15357 goto out;
15358 mqe = &mboxq->u.mqe;
15359 mp = (struct lpfc_dmabuf *) mboxq->context1;
15360 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15361 if (rc)
15362 goto out;
15363 data_length = mqe->un.mb_words[5];
15364 if (data_length == 0)
15365 goto out;
15366 if (data_length > DMP_RGN23_SIZE) {
15367 data_length = 0;
15368 goto out;
15369 }
15370 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15371out:
15372 mempool_free(mboxq, phba->mbox_mem_pool);
15373 if (mp) {
15374 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15375 kfree(mp);
15376 }
15377 return data_length;
15378}
15379
15380/**
15381 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15382 * @phba: pointer to lpfc hba data structure.
15383 *
15384 * This function read region 23 and parse TLV for port status to
15385 * decide if the user disaled the port. If the TLV indicates the
15386 * port is disabled, the hba_flag is set accordingly.
15387 **/
15388void
15389lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15390{
15391 uint8_t *rgn23_data = NULL;
15392 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15393 uint32_t offset = 0;
15394
15395 /* Get adapter Region 23 data */
15396 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15397 if (!rgn23_data)
15398 goto out;
15399
15400 if (phba->sli_rev < LPFC_SLI_REV4)
15401 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15402 else {
15403 if_type = bf_get(lpfc_sli_intf_if_type,
15404 &phba->sli4_hba.sli_intf);
15405 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15406 goto out;
15407 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15408 }
a0c87cbd
JS
15409
15410 if (!data_size)
15411 goto out;
15412
15413 /* Check the region signature first */
15414 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15415 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15416 "2619 Config region 23 has bad signature\n");
15417 goto out;
15418 }
15419 offset += 4;
15420
15421 /* Check the data structure version */
15422 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
15423 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15424 "2620 Config region 23 has bad version\n");
15425 goto out;
15426 }
15427 offset += 4;
15428
15429 /* Parse TLV entries in the region */
15430 while (offset < data_size) {
15431 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
15432 break;
15433 /*
15434 * If the TLV is not driver specific TLV or driver id is
15435 * not linux driver id, skip the record.
15436 */
15437 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
15438 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
15439 (rgn23_data[offset + 3] != 0)) {
15440 offset += rgn23_data[offset + 1] * 4 + 4;
15441 continue;
15442 }
15443
15444 /* Driver found a driver specific TLV in the config region */
15445 sub_tlv_len = rgn23_data[offset + 1] * 4;
15446 offset += 4;
15447 tlv_offset = 0;
15448
15449 /*
15450 * Search for configured port state sub-TLV.
15451 */
15452 while ((offset < data_size) &&
15453 (tlv_offset < sub_tlv_len)) {
15454 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
15455 offset += 4;
15456 tlv_offset += 4;
15457 break;
15458 }
15459 if (rgn23_data[offset] != PORT_STE_TYPE) {
15460 offset += rgn23_data[offset + 1] * 4 + 4;
15461 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
15462 continue;
15463 }
15464
15465 /* This HBA contains PORT_STE configured */
15466 if (!rgn23_data[offset + 2])
15467 phba->hba_flag |= LINK_DISABLED;
15468
15469 goto out;
15470 }
15471 }
026abb87 15472
a0c87cbd 15473out:
a0c87cbd
JS
15474 kfree(rgn23_data);
15475 return;
15476}
695a814e 15477
52d52440
JS
15478/**
15479 * lpfc_wr_object - write an object to the firmware
15480 * @phba: HBA structure that indicates port to create a queue on.
15481 * @dmabuf_list: list of dmabufs to write to the port.
15482 * @size: the total byte value of the objects to write to the port.
15483 * @offset: the current offset to be used to start the transfer.
15484 *
15485 * This routine will create a wr_object mailbox command to send to the port.
15486 * the mailbox command will be constructed using the dma buffers described in
15487 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
15488 * BDEs that the imbedded mailbox can support. The @offset variable will be
15489 * used to indicate the starting offset of the transfer and will also return
15490 * the offset after the write object mailbox has completed. @size is used to
15491 * determine the end of the object and whether the eof bit should be set.
15492 *
15493 * Return 0 is successful and offset will contain the the new offset to use
15494 * for the next write.
15495 * Return negative value for error cases.
15496 **/
15497int
15498lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
15499 uint32_t size, uint32_t *offset)
15500{
15501 struct lpfc_mbx_wr_object *wr_object;
15502 LPFC_MBOXQ_t *mbox;
15503 int rc = 0, i = 0;
15504 uint32_t shdr_status, shdr_add_status;
15505 uint32_t mbox_tmo;
15506 union lpfc_sli4_cfg_shdr *shdr;
15507 struct lpfc_dmabuf *dmabuf;
15508 uint32_t written = 0;
15509
15510 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15511 if (!mbox)
15512 return -ENOMEM;
15513
15514 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
15515 LPFC_MBOX_OPCODE_WRITE_OBJECT,
15516 sizeof(struct lpfc_mbx_wr_object) -
15517 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
15518
15519 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
15520 wr_object->u.request.write_offset = *offset;
15521 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
15522 wr_object->u.request.object_name[0] =
15523 cpu_to_le32(wr_object->u.request.object_name[0]);
15524 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
15525 list_for_each_entry(dmabuf, dmabuf_list, list) {
15526 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
15527 break;
15528 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
15529 wr_object->u.request.bde[i].addrHigh =
15530 putPaddrHigh(dmabuf->phys);
15531 if (written + SLI4_PAGE_SIZE >= size) {
15532 wr_object->u.request.bde[i].tus.f.bdeSize =
15533 (size - written);
15534 written += (size - written);
15535 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
15536 } else {
15537 wr_object->u.request.bde[i].tus.f.bdeSize =
15538 SLI4_PAGE_SIZE;
15539 written += SLI4_PAGE_SIZE;
15540 }
15541 i++;
15542 }
15543 wr_object->u.request.bde_count = i;
15544 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
15545 if (!phba->sli4_hba.intr_enable)
15546 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
15547 else {
a183a15f 15548 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
52d52440
JS
15549 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
15550 }
15551 /* The IOCTL status is embedded in the mailbox subheader. */
15552 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
15553 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15554 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15555 if (rc != MBX_TIMEOUT)
15556 mempool_free(mbox, phba->mbox_mem_pool);
15557 if (shdr_status || shdr_add_status || rc) {
15558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15559 "3025 Write Object mailbox failed with "
15560 "status x%x add_status x%x, mbx status x%x\n",
15561 shdr_status, shdr_add_status, rc);
15562 rc = -ENXIO;
15563 } else
15564 *offset += wr_object->u.response.actual_write_length;
15565 return rc;
15566}
15567
695a814e
JS
15568/**
15569 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
15570 * @vport: pointer to vport data structure.
15571 *
15572 * This function iterate through the mailboxq and clean up all REG_LOGIN
15573 * and REG_VPI mailbox commands associated with the vport. This function
15574 * is called when driver want to restart discovery of the vport due to
15575 * a Clear Virtual Link event.
15576 **/
15577void
15578lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
15579{
15580 struct lpfc_hba *phba = vport->phba;
15581 LPFC_MBOXQ_t *mb, *nextmb;
15582 struct lpfc_dmabuf *mp;
78730cfe 15583 struct lpfc_nodelist *ndlp;
d439d286 15584 struct lpfc_nodelist *act_mbx_ndlp = NULL;
589a52d6 15585 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
d439d286 15586 LIST_HEAD(mbox_cmd_list);
63e801ce 15587 uint8_t restart_loop;
695a814e 15588
d439d286 15589 /* Clean up internally queued mailbox commands with the vport */
695a814e
JS
15590 spin_lock_irq(&phba->hbalock);
15591 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
15592 if (mb->vport != vport)
15593 continue;
15594
15595 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15596 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15597 continue;
15598
d439d286
JS
15599 list_del(&mb->list);
15600 list_add_tail(&mb->list, &mbox_cmd_list);
15601 }
15602 /* Clean up active mailbox command with the vport */
15603 mb = phba->sli.mbox_active;
15604 if (mb && (mb->vport == vport)) {
15605 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
15606 (mb->u.mb.mbxCommand == MBX_REG_VPI))
15607 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15608 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15609 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
15610 /* Put reference count for delayed processing */
15611 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
15612 /* Unregister the RPI when mailbox complete */
15613 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15614 }
15615 }
63e801ce
JS
15616 /* Cleanup any mailbox completions which are not yet processed */
15617 do {
15618 restart_loop = 0;
15619 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
15620 /*
15621 * If this mailox is already processed or it is
15622 * for another vport ignore it.
15623 */
15624 if ((mb->vport != vport) ||
15625 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
15626 continue;
15627
15628 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
15629 (mb->u.mb.mbxCommand != MBX_REG_VPI))
15630 continue;
15631
15632 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15633 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15634 ndlp = (struct lpfc_nodelist *)mb->context2;
15635 /* Unregister the RPI when mailbox complete */
15636 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
15637 restart_loop = 1;
15638 spin_unlock_irq(&phba->hbalock);
15639 spin_lock(shost->host_lock);
15640 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15641 spin_unlock(shost->host_lock);
15642 spin_lock_irq(&phba->hbalock);
15643 break;
15644 }
15645 }
15646 } while (restart_loop);
15647
d439d286
JS
15648 spin_unlock_irq(&phba->hbalock);
15649
15650 /* Release the cleaned-up mailbox commands */
15651 while (!list_empty(&mbox_cmd_list)) {
15652 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
695a814e
JS
15653 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
15654 mp = (struct lpfc_dmabuf *) (mb->context1);
15655 if (mp) {
15656 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
15657 kfree(mp);
15658 }
78730cfe 15659 ndlp = (struct lpfc_nodelist *) mb->context2;
d439d286 15660 mb->context2 = NULL;
78730cfe 15661 if (ndlp) {
ec21b3b0 15662 spin_lock(shost->host_lock);
589a52d6 15663 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
ec21b3b0 15664 spin_unlock(shost->host_lock);
78730cfe 15665 lpfc_nlp_put(ndlp);
78730cfe 15666 }
695a814e 15667 }
695a814e
JS
15668 mempool_free(mb, phba->mbox_mem_pool);
15669 }
d439d286
JS
15670
15671 /* Release the ndlp with the cleaned-up active mailbox command */
15672 if (act_mbx_ndlp) {
15673 spin_lock(shost->host_lock);
15674 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
15675 spin_unlock(shost->host_lock);
15676 lpfc_nlp_put(act_mbx_ndlp);
695a814e 15677 }
695a814e
JS
15678}
15679
2a9bf3d0
JS
15680/**
15681 * lpfc_drain_txq - Drain the txq
15682 * @phba: Pointer to HBA context object.
15683 *
15684 * This function attempt to submit IOCBs on the txq
15685 * to the adapter. For SLI4 adapters, the txq contains
15686 * ELS IOCBs that have been deferred because the there
15687 * are no SGLs. This congestion can occur with large
15688 * vport counts during node discovery.
15689 **/
15690
15691uint32_t
15692lpfc_drain_txq(struct lpfc_hba *phba)
15693{
15694 LIST_HEAD(completions);
15695 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
15696 struct lpfc_iocbq *piocbq = 0;
15697 unsigned long iflags = 0;
15698 char *fail_msg = NULL;
15699 struct lpfc_sglq *sglq;
15700 union lpfc_wqe wqe;
15701
15702 spin_lock_irqsave(&phba->hbalock, iflags);
15703 if (pring->txq_cnt > pring->txq_max)
15704 pring->txq_max = pring->txq_cnt;
15705
15706 spin_unlock_irqrestore(&phba->hbalock, iflags);
15707
15708 while (pring->txq_cnt) {
15709 spin_lock_irqsave(&phba->hbalock, iflags);
15710
19ca7609
JS
15711 piocbq = lpfc_sli_ringtx_get(phba, pring);
15712 sglq = __lpfc_sli_get_sglq(phba, piocbq);
2a9bf3d0 15713 if (!sglq) {
19ca7609 15714 __lpfc_sli_ringtx_put(phba, pring, piocbq);
2a9bf3d0
JS
15715 spin_unlock_irqrestore(&phba->hbalock, iflags);
15716 break;
15717 } else {
2a9bf3d0
JS
15718 if (!piocbq) {
15719 /* The txq_cnt out of sync. This should
15720 * never happen
15721 */
15722 sglq = __lpfc_clear_active_sglq(phba,
6d368e53 15723 sglq->sli4_lxritag);
2a9bf3d0
JS
15724 spin_unlock_irqrestore(&phba->hbalock, iflags);
15725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15726 "2823 txq empty and txq_cnt is %d\n ",
15727 pring->txq_cnt);
15728 break;
15729 }
15730 }
15731
15732 /* The xri and iocb resources secured,
15733 * attempt to issue request
15734 */
6d368e53 15735 piocbq->sli4_lxritag = sglq->sli4_lxritag;
2a9bf3d0
JS
15736 piocbq->sli4_xritag = sglq->sli4_xritag;
15737 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
15738 fail_msg = "to convert bpl to sgl";
15739 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
15740 fail_msg = "to convert iocb to wqe";
15741 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
15742 fail_msg = " - Wq is full";
15743 else
15744 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
15745
15746 if (fail_msg) {
15747 /* Failed means we can't issue and need to cancel */
15748 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15749 "2822 IOCB failed %s iotag 0x%x "
15750 "xri 0x%x\n",
15751 fail_msg,
15752 piocbq->iotag, piocbq->sli4_xritag);
15753 list_add_tail(&piocbq->list, &completions);
15754 }
15755 spin_unlock_irqrestore(&phba->hbalock, iflags);
15756 }
15757
2a9bf3d0
JS
15758 /* Cancel all the IOCBs that cannot be issued */
15759 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
15760 IOERR_SLI_ABORTED);
15761
15762 return pring->txq_cnt;
15763}
This page took 1.548179 seconds and 5 git commands to generate.