[SCSI] lpfc 8.3.10: Added management for LP21000 through BSG.
[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. *
d8e93df1 4 * Copyright (C) 2004-2009 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>
26
91886523 27#include <scsi/scsi.h>
dea3101e 28#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
f888ba3c 31#include <scsi/scsi_transport_fc.h>
da0436e9 32#include <scsi/fc/fc_fs.h>
0d878419 33#include <linux/aer.h>
dea3101e 34
da0436e9 35#include "lpfc_hw4.h"
dea3101e 36#include "lpfc_hw.h"
37#include "lpfc_sli.h"
da0436e9 38#include "lpfc_sli4.h"
ea2151b4 39#include "lpfc_nl.h"
dea3101e 40#include "lpfc_disc.h"
41#include "lpfc_scsi.h"
42#include "lpfc.h"
43#include "lpfc_crtn.h"
44#include "lpfc_logmsg.h"
45#include "lpfc_compat.h"
858c9f6c 46#include "lpfc_debugfs.h"
04c68496 47#include "lpfc_vport.h"
dea3101e 48
49/* There are only four IOCB completion types. */
50typedef enum _lpfc_iocb_type {
51 LPFC_UNKNOWN_IOCB,
52 LPFC_UNSOL_IOCB,
53 LPFC_SOL_IOCB,
54 LPFC_ABORT_IOCB
55} lpfc_iocb_type;
56
4f774513
JS
57
58/* Provide function prototypes local to this module. */
59static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
60 uint32_t);
61static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
45ed1190
JS
62 uint8_t *, uint32_t *);
63static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
64 struct lpfc_iocbq *);
6669f9bb
JS
65static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
66 struct hbq_dmabuf *);
4f774513
JS
67static IOCB_t *
68lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
69{
70 return &iocbq->iocb;
71}
72
73/**
74 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
75 * @q: The Work Queue to operate on.
76 * @wqe: The work Queue Entry to put on the Work queue.
77 *
78 * This routine will copy the contents of @wqe to the next available entry on
79 * the @q. This function will then ring the Work Queue Doorbell to signal the
80 * HBA to start processing the Work Queue Entry. This function returns 0 if
81 * successful. If no entries are available on @q then this function will return
82 * -ENOMEM.
83 * The caller is expected to hold the hbalock when calling this routine.
84 **/
85static uint32_t
86lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
87{
88 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
89 struct lpfc_register doorbell;
90 uint32_t host_index;
91
92 /* If the host has not yet processed the next entry then we are done */
93 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
94 return -ENOMEM;
95 /* set consumption flag every once in a while */
96 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
97 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
98
99 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
100
101 /* Update the host index before invoking device */
102 host_index = q->host_index;
103 q->host_index = ((q->host_index + 1) % q->entry_count);
104
105 /* Ring Doorbell */
106 doorbell.word0 = 0;
107 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
108 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
109 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
110 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
111 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
112
113 return 0;
114}
115
116/**
117 * lpfc_sli4_wq_release - Updates internal hba index for WQ
118 * @q: The Work Queue to operate on.
119 * @index: The index to advance the hba index to.
120 *
121 * This routine will update the HBA index of a queue to reflect consumption of
122 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
123 * an entry the host calls this function to update the queue's internal
124 * pointers. This routine returns the number of entries that were consumed by
125 * the HBA.
126 **/
127static uint32_t
128lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
129{
130 uint32_t released = 0;
131
132 if (q->hba_index == index)
133 return 0;
134 do {
135 q->hba_index = ((q->hba_index + 1) % q->entry_count);
136 released++;
137 } while (q->hba_index != index);
138 return released;
139}
140
141/**
142 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
143 * @q: The Mailbox Queue to operate on.
144 * @wqe: The Mailbox Queue Entry to put on the Work queue.
145 *
146 * This routine will copy the contents of @mqe to the next available entry on
147 * the @q. This function will then ring the Work Queue Doorbell to signal the
148 * HBA to start processing the Work Queue Entry. This function returns 0 if
149 * successful. If no entries are available on @q then this function will return
150 * -ENOMEM.
151 * The caller is expected to hold the hbalock when calling this routine.
152 **/
153static uint32_t
154lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
155{
156 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
157 struct lpfc_register doorbell;
158 uint32_t host_index;
159
160 /* If the host has not yet processed the next entry then we are done */
161 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
162 return -ENOMEM;
163 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
164 /* Save off the mailbox pointer for completion */
165 q->phba->mbox = (MAILBOX_t *)temp_mqe;
166
167 /* Update the host index before invoking device */
168 host_index = q->host_index;
169 q->host_index = ((q->host_index + 1) % q->entry_count);
170
171 /* Ring Doorbell */
172 doorbell.word0 = 0;
173 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
174 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
175 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
176 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
177 return 0;
178}
179
180/**
181 * lpfc_sli4_mq_release - Updates internal hba index for MQ
182 * @q: The Mailbox Queue to operate on.
183 *
184 * This routine will update the HBA index of a queue to reflect consumption of
185 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
186 * an entry the host calls this function to update the queue's internal
187 * pointers. This routine returns the number of entries that were consumed by
188 * the HBA.
189 **/
190static uint32_t
191lpfc_sli4_mq_release(struct lpfc_queue *q)
192{
193 /* Clear the mailbox pointer for completion */
194 q->phba->mbox = NULL;
195 q->hba_index = ((q->hba_index + 1) % q->entry_count);
196 return 1;
197}
198
199/**
200 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
201 * @q: The Event Queue to get the first valid EQE from
202 *
203 * This routine will get the first valid Event Queue Entry from @q, update
204 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
205 * the Queue (no more work to do), or the Queue is full of EQEs that have been
206 * processed, but not popped back to the HBA then this routine will return NULL.
207 **/
208static struct lpfc_eqe *
209lpfc_sli4_eq_get(struct lpfc_queue *q)
210{
211 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
212
213 /* If the next EQE is not valid then we are done */
214 if (!bf_get(lpfc_eqe_valid, eqe))
215 return NULL;
216 /* If the host has not yet processed the next entry then we are done */
217 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
218 return NULL;
219
220 q->hba_index = ((q->hba_index + 1) % q->entry_count);
221 return eqe;
222}
223
224/**
225 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
226 * @q: The Event Queue that the host has completed processing for.
227 * @arm: Indicates whether the host wants to arms this CQ.
228 *
229 * This routine will mark all Event Queue Entries on @q, from the last
230 * known completed entry to the last entry that was processed, as completed
231 * by clearing the valid bit for each completion queue entry. Then it will
232 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
233 * The internal host index in the @q will be updated by this routine to indicate
234 * that the host has finished processing the entries. The @arm parameter
235 * indicates that the queue should be rearmed when ringing the doorbell.
236 *
237 * This function will return the number of EQEs that were popped.
238 **/
239uint32_t
240lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
241{
242 uint32_t released = 0;
243 struct lpfc_eqe *temp_eqe;
244 struct lpfc_register doorbell;
245
246 /* while there are valid entries */
247 while (q->hba_index != q->host_index) {
248 temp_eqe = q->qe[q->host_index].eqe;
249 bf_set(lpfc_eqe_valid, temp_eqe, 0);
250 released++;
251 q->host_index = ((q->host_index + 1) % q->entry_count);
252 }
253 if (unlikely(released == 0 && !arm))
254 return 0;
255
256 /* ring doorbell for number popped */
257 doorbell.word0 = 0;
258 if (arm) {
259 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
260 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
261 }
262 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
263 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
264 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
265 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
a747c9ce
JS
266 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
267 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
268 readl(q->phba->sli4_hba.EQCQDBregaddr);
4f774513
JS
269 return released;
270}
271
272/**
273 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
274 * @q: The Completion Queue to get the first valid CQE from
275 *
276 * This routine will get the first valid Completion Queue Entry from @q, update
277 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
278 * the Queue (no more work to do), or the Queue is full of CQEs that have been
279 * processed, but not popped back to the HBA then this routine will return NULL.
280 **/
281static struct lpfc_cqe *
282lpfc_sli4_cq_get(struct lpfc_queue *q)
283{
284 struct lpfc_cqe *cqe;
285
286 /* If the next CQE is not valid then we are done */
287 if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
288 return NULL;
289 /* If the host has not yet processed the next entry then we are done */
290 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
291 return NULL;
292
293 cqe = q->qe[q->hba_index].cqe;
294 q->hba_index = ((q->hba_index + 1) % q->entry_count);
295 return cqe;
296}
297
298/**
299 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
300 * @q: The Completion Queue that the host has completed processing for.
301 * @arm: Indicates whether the host wants to arms this CQ.
302 *
303 * This routine will mark all Completion queue entries on @q, from the last
304 * known completed entry to the last entry that was processed, as completed
305 * by clearing the valid bit for each completion queue entry. Then it will
306 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
307 * The internal host index in the @q will be updated by this routine to indicate
308 * that the host has finished processing the entries. The @arm parameter
309 * indicates that the queue should be rearmed when ringing the doorbell.
310 *
311 * This function will return the number of CQEs that were released.
312 **/
313uint32_t
314lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
315{
316 uint32_t released = 0;
317 struct lpfc_cqe *temp_qe;
318 struct lpfc_register doorbell;
319
320 /* while there are valid entries */
321 while (q->hba_index != q->host_index) {
322 temp_qe = q->qe[q->host_index].cqe;
323 bf_set(lpfc_cqe_valid, temp_qe, 0);
324 released++;
325 q->host_index = ((q->host_index + 1) % q->entry_count);
326 }
327 if (unlikely(released == 0 && !arm))
328 return 0;
329
330 /* ring doorbell for number popped */
331 doorbell.word0 = 0;
332 if (arm)
333 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
334 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
335 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
336 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
337 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
338 return released;
339}
340
341/**
342 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
343 * @q: The Header Receive Queue to operate on.
344 * @wqe: The Receive Queue Entry to put on the Receive queue.
345 *
346 * This routine will copy the contents of @wqe to the next available entry on
347 * the @q. This function will then ring the Receive Queue Doorbell to signal the
348 * HBA to start processing the Receive Queue Entry. This function returns the
349 * index that the rqe was copied to if successful. If no entries are available
350 * on @q then this function will return -ENOMEM.
351 * The caller is expected to hold the hbalock when calling this routine.
352 **/
353static int
354lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
355 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
356{
357 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
358 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
359 struct lpfc_register doorbell;
360 int put_index = hq->host_index;
361
362 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
363 return -EINVAL;
364 if (hq->host_index != dq->host_index)
365 return -EINVAL;
366 /* If the host has not yet processed the next entry then we are done */
367 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
368 return -EBUSY;
369 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
370 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
371
372 /* Update the host index to point to the next slot */
373 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
374 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
375
376 /* Ring The Header Receive Queue Doorbell */
377 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
378 doorbell.word0 = 0;
379 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
380 LPFC_RQ_POST_BATCH);
381 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
382 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
383 }
384 return put_index;
385}
386
387/**
388 * lpfc_sli4_rq_release - Updates internal hba index for RQ
389 * @q: The Header Receive Queue to operate on.
390 *
391 * This routine will update the HBA index of a queue to reflect consumption of
392 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
393 * consumed an entry the host calls this function to update the queue's
394 * internal pointers. This routine returns the number of entries that were
395 * consumed by the HBA.
396 **/
397static uint32_t
398lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
399{
400 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
401 return 0;
402 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
403 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
404 return 1;
405}
406
e59058c4 407/**
3621a710 408 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
409 * @phba: Pointer to HBA context object.
410 * @pring: Pointer to driver SLI ring object.
411 *
412 * This function returns pointer to next command iocb entry
413 * in the command ring. The caller must hold hbalock to prevent
414 * other threads consume the next command iocb.
415 * SLI-2/SLI-3 provide different sized iocbs.
416 **/
ed957684
JS
417static inline IOCB_t *
418lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
419{
420 return (IOCB_t *) (((char *) pring->cmdringaddr) +
421 pring->cmdidx * phba->iocb_cmd_size);
422}
423
e59058c4 424/**
3621a710 425 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
426 * @phba: Pointer to HBA context object.
427 * @pring: Pointer to driver SLI ring object.
428 *
429 * This function returns pointer to next response iocb entry
430 * in the response ring. The caller must hold hbalock to make sure
431 * that no other thread consume the next response iocb.
432 * SLI-2/SLI-3 provide different sized iocbs.
433 **/
ed957684
JS
434static inline IOCB_t *
435lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
436{
437 return (IOCB_t *) (((char *) pring->rspringaddr) +
438 pring->rspidx * phba->iocb_rsp_size);
439}
440
e59058c4 441/**
3621a710 442 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
443 * @phba: Pointer to HBA context object.
444 *
445 * This function is called with hbalock held. This function
446 * allocates a new driver iocb object from the iocb pool. If the
447 * allocation is successful, it returns pointer to the newly
448 * allocated iocb object else it returns NULL.
449 **/
2e0fef85
JS
450static struct lpfc_iocbq *
451__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
452{
453 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
454 struct lpfc_iocbq * iocbq = NULL;
455
456 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
457 return iocbq;
458}
459
da0436e9
JS
460/**
461 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
462 * @phba: Pointer to HBA context object.
463 * @xritag: XRI value.
464 *
465 * This function clears the sglq pointer from the array of acive
466 * sglq's. The xritag that is passed in is used to index into the
467 * array. Before the xritag can be used it needs to be adjusted
468 * by subtracting the xribase.
469 *
470 * Returns sglq ponter = success, NULL = Failure.
471 **/
472static struct lpfc_sglq *
473__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
474{
475 uint16_t adj_xri;
476 struct lpfc_sglq *sglq;
477 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
478 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
479 return NULL;
480 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
481 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
482 return sglq;
483}
484
485/**
486 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
487 * @phba: Pointer to HBA context object.
488 * @xritag: XRI value.
489 *
490 * This function returns the sglq pointer from the array of acive
491 * sglq's. The xritag that is passed in is used to index into the
492 * array. Before the xritag can be used it needs to be adjusted
493 * by subtracting the xribase.
494 *
495 * Returns sglq ponter = success, NULL = Failure.
496 **/
0f65ff68 497struct lpfc_sglq *
da0436e9
JS
498__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
499{
500 uint16_t adj_xri;
501 struct lpfc_sglq *sglq;
502 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
503 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
504 return NULL;
505 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
506 return sglq;
507}
508
509/**
510 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
511 * @phba: Pointer to HBA context object.
512 *
513 * This function is called with hbalock held. This function
514 * Gets a new driver sglq object from the sglq list. If the
515 * list is not empty then it is successful, it returns pointer to the newly
516 * allocated sglq object else it returns NULL.
517 **/
518static struct lpfc_sglq *
519__lpfc_sli_get_sglq(struct lpfc_hba *phba)
520{
521 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
522 struct lpfc_sglq *sglq = NULL;
523 uint16_t adj_xri;
524 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
6a9c52cf
JS
525 if (!sglq)
526 return NULL;
da0436e9
JS
527 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
528 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
0f65ff68 529 sglq->state = SGL_ALLOCATED;
da0436e9
JS
530 return sglq;
531}
532
e59058c4 533/**
3621a710 534 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
535 * @phba: Pointer to HBA context object.
536 *
537 * This function is called with no lock held. This function
538 * allocates a new driver iocb object from the iocb pool. If the
539 * allocation is successful, it returns pointer to the newly
540 * allocated iocb object else it returns NULL.
541 **/
2e0fef85
JS
542struct lpfc_iocbq *
543lpfc_sli_get_iocbq(struct lpfc_hba *phba)
544{
545 struct lpfc_iocbq * iocbq = NULL;
546 unsigned long iflags;
547
548 spin_lock_irqsave(&phba->hbalock, iflags);
549 iocbq = __lpfc_sli_get_iocbq(phba);
550 spin_unlock_irqrestore(&phba->hbalock, iflags);
551 return iocbq;
552}
553
4f774513
JS
554/**
555 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
556 * @phba: Pointer to HBA context object.
557 * @iocbq: Pointer to driver iocb object.
558 *
559 * This function is called with hbalock held to release driver
560 * iocb object to the iocb pool. The iotag in the iocb object
561 * does not change for each use of the iocb object. This function
562 * clears all other fields of the iocb object when it is freed.
563 * The sqlq structure that holds the xritag and phys and virtual
564 * mappings for the scatter gather list is retrieved from the
565 * active array of sglq. The get of the sglq pointer also clears
566 * the entry in the array. If the status of the IO indiactes that
567 * this IO was aborted then the sglq entry it put on the
568 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
569 * IO has good status or fails for any other reason then the sglq
570 * entry is added to the free list (lpfc_sgl_list).
571 **/
572static void
573__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
574{
575 struct lpfc_sglq *sglq;
576 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
577 unsigned long iflag;
578
579 if (iocbq->sli4_xritag == NO_XRI)
580 sglq = NULL;
581 else
582 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
583 if (sglq) {
0f65ff68
JS
584 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
585 (sglq->state != SGL_XRI_ABORTED)) {
4f774513
JS
586 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
587 iflag);
588 list_add(&sglq->list,
589 &phba->sli4_hba.lpfc_abts_els_sgl_list);
590 spin_unlock_irqrestore(
591 &phba->sli4_hba.abts_sgl_list_lock, iflag);
0f65ff68
JS
592 } else {
593 sglq->state = SGL_FREED;
4f774513 594 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
0f65ff68 595 }
4f774513
JS
596 }
597
598
599 /*
600 * Clean all volatile data fields, preserve iotag and node struct.
601 */
602 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
603 iocbq->sli4_xritag = NO_XRI;
604 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
605}
606
e59058c4 607/**
3772a991 608 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
609 * @phba: Pointer to HBA context object.
610 * @iocbq: Pointer to driver iocb object.
611 *
612 * This function is called with hbalock held to release driver
613 * iocb object to the iocb pool. The iotag in the iocb object
614 * does not change for each use of the iocb object. This function
615 * clears all other fields of the iocb object when it is freed.
616 **/
a6ababd2 617static void
3772a991 618__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 619{
2e0fef85 620 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
621
622 /*
623 * Clean all volatile data fields, preserve iotag and node struct.
624 */
625 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 626 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
627 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
628}
629
3772a991
JS
630/**
631 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
632 * @phba: Pointer to HBA context object.
633 * @iocbq: Pointer to driver iocb object.
634 *
635 * This function is called with hbalock held to release driver
636 * iocb object to the iocb pool. The iotag in the iocb object
637 * does not change for each use of the iocb object. This function
638 * clears all other fields of the iocb object when it is freed.
639 **/
640static void
641__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
642{
643 phba->__lpfc_sli_release_iocbq(phba, iocbq);
644}
645
e59058c4 646/**
3621a710 647 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
648 * @phba: Pointer to HBA context object.
649 * @iocbq: Pointer to driver iocb object.
650 *
651 * This function is called with no lock held to release the iocb to
652 * iocb pool.
653 **/
2e0fef85
JS
654void
655lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
656{
657 unsigned long iflags;
658
659 /*
660 * Clean all volatile data fields, preserve iotag and node struct.
661 */
662 spin_lock_irqsave(&phba->hbalock, iflags);
663 __lpfc_sli_release_iocbq(phba, iocbq);
664 spin_unlock_irqrestore(&phba->hbalock, iflags);
665}
666
a257bf90
JS
667/**
668 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
669 * @phba: Pointer to HBA context object.
670 * @iocblist: List of IOCBs.
671 * @ulpstatus: ULP status in IOCB command field.
672 * @ulpWord4: ULP word-4 in IOCB command field.
673 *
674 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
675 * on the list by invoking the complete callback function associated with the
676 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
677 * fields.
678 **/
679void
680lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
681 uint32_t ulpstatus, uint32_t ulpWord4)
682{
683 struct lpfc_iocbq *piocb;
684
685 while (!list_empty(iocblist)) {
686 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
687
688 if (!piocb->iocb_cmpl)
689 lpfc_sli_release_iocbq(phba, piocb);
690 else {
691 piocb->iocb.ulpStatus = ulpstatus;
692 piocb->iocb.un.ulpWord[4] = ulpWord4;
693 (piocb->iocb_cmpl) (phba, piocb, piocb);
694 }
695 }
696 return;
697}
698
e59058c4 699/**
3621a710
JS
700 * lpfc_sli_iocb_cmd_type - Get the iocb type
701 * @iocb_cmnd: iocb command code.
e59058c4
JS
702 *
703 * This function is called by ring event handler function to get the iocb type.
704 * This function translates the iocb command to an iocb command type used to
705 * decide the final disposition of each completed IOCB.
706 * The function returns
707 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
708 * LPFC_SOL_IOCB if it is a solicited iocb completion
709 * LPFC_ABORT_IOCB if it is an abort iocb
710 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
711 *
712 * The caller is not required to hold any lock.
713 **/
dea3101e 714static lpfc_iocb_type
715lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
716{
717 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
718
719 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
720 return 0;
721
722 switch (iocb_cmnd) {
723 case CMD_XMIT_SEQUENCE_CR:
724 case CMD_XMIT_SEQUENCE_CX:
725 case CMD_XMIT_BCAST_CN:
726 case CMD_XMIT_BCAST_CX:
727 case CMD_ELS_REQUEST_CR:
728 case CMD_ELS_REQUEST_CX:
729 case CMD_CREATE_XRI_CR:
730 case CMD_CREATE_XRI_CX:
731 case CMD_GET_RPI_CN:
732 case CMD_XMIT_ELS_RSP_CX:
733 case CMD_GET_RPI_CR:
734 case CMD_FCP_IWRITE_CR:
735 case CMD_FCP_IWRITE_CX:
736 case CMD_FCP_IREAD_CR:
737 case CMD_FCP_IREAD_CX:
738 case CMD_FCP_ICMND_CR:
739 case CMD_FCP_ICMND_CX:
f5603511
JS
740 case CMD_FCP_TSEND_CX:
741 case CMD_FCP_TRSP_CX:
742 case CMD_FCP_TRECEIVE_CX:
743 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 744 case CMD_ADAPTER_MSG:
745 case CMD_ADAPTER_DUMP:
746 case CMD_XMIT_SEQUENCE64_CR:
747 case CMD_XMIT_SEQUENCE64_CX:
748 case CMD_XMIT_BCAST64_CN:
749 case CMD_XMIT_BCAST64_CX:
750 case CMD_ELS_REQUEST64_CR:
751 case CMD_ELS_REQUEST64_CX:
752 case CMD_FCP_IWRITE64_CR:
753 case CMD_FCP_IWRITE64_CX:
754 case CMD_FCP_IREAD64_CR:
755 case CMD_FCP_IREAD64_CX:
756 case CMD_FCP_ICMND64_CR:
757 case CMD_FCP_ICMND64_CX:
f5603511
JS
758 case CMD_FCP_TSEND64_CX:
759 case CMD_FCP_TRSP64_CX:
760 case CMD_FCP_TRECEIVE64_CX:
dea3101e 761 case CMD_GEN_REQUEST64_CR:
762 case CMD_GEN_REQUEST64_CX:
763 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
764 case DSSCMD_IWRITE64_CR:
765 case DSSCMD_IWRITE64_CX:
766 case DSSCMD_IREAD64_CR:
767 case DSSCMD_IREAD64_CX:
dea3101e 768 type = LPFC_SOL_IOCB;
769 break;
770 case CMD_ABORT_XRI_CN:
771 case CMD_ABORT_XRI_CX:
772 case CMD_CLOSE_XRI_CN:
773 case CMD_CLOSE_XRI_CX:
774 case CMD_XRI_ABORTED_CX:
775 case CMD_ABORT_MXRI64_CN:
6669f9bb 776 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 777 type = LPFC_ABORT_IOCB;
778 break;
779 case CMD_RCV_SEQUENCE_CX:
780 case CMD_RCV_ELS_REQ_CX:
781 case CMD_RCV_SEQUENCE64_CX:
782 case CMD_RCV_ELS_REQ64_CX:
57127f15 783 case CMD_ASYNC_STATUS:
ed957684
JS
784 case CMD_IOCB_RCV_SEQ64_CX:
785 case CMD_IOCB_RCV_ELS64_CX:
786 case CMD_IOCB_RCV_CONT64_CX:
3163f725 787 case CMD_IOCB_RET_XRI64_CX:
dea3101e 788 type = LPFC_UNSOL_IOCB;
789 break;
3163f725
JS
790 case CMD_IOCB_XMIT_MSEQ64_CR:
791 case CMD_IOCB_XMIT_MSEQ64_CX:
792 case CMD_IOCB_RCV_SEQ_LIST64_CX:
793 case CMD_IOCB_RCV_ELS_LIST64_CX:
794 case CMD_IOCB_CLOSE_EXTENDED_CN:
795 case CMD_IOCB_ABORT_EXTENDED_CN:
796 case CMD_IOCB_RET_HBQE64_CN:
797 case CMD_IOCB_FCP_IBIDIR64_CR:
798 case CMD_IOCB_FCP_IBIDIR64_CX:
799 case CMD_IOCB_FCP_ITASKMGT64_CX:
800 case CMD_IOCB_LOGENTRY_CN:
801 case CMD_IOCB_LOGENTRY_ASYNC_CN:
802 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 803 __func__, iocb_cmnd);
3163f725
JS
804 type = LPFC_UNKNOWN_IOCB;
805 break;
dea3101e 806 default:
807 type = LPFC_UNKNOWN_IOCB;
808 break;
809 }
810
811 return type;
812}
813
e59058c4 814/**
3621a710 815 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
816 * @phba: Pointer to HBA context object.
817 *
818 * This function is called from SLI initialization code
819 * to configure every ring of the HBA's SLI interface. The
820 * caller is not required to hold any lock. This function issues
821 * a config_ring mailbox command for each ring.
822 * This function returns zero if successful else returns a negative
823 * error code.
824 **/
dea3101e 825static int
ed957684 826lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 827{
828 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
829 LPFC_MBOXQ_t *pmb;
830 MAILBOX_t *pmbox;
831 int i, rc, ret = 0;
dea3101e 832
ed957684
JS
833 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
834 if (!pmb)
835 return -ENOMEM;
04c68496 836 pmbox = &pmb->u.mb;
ed957684 837 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 838 for (i = 0; i < psli->num_rings; i++) {
dea3101e 839 lpfc_config_ring(phba, i, pmb);
840 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
841 if (rc != MBX_SUCCESS) {
92d7f7b0 842 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 843 "0446 Adapter failed to init (%d), "
dea3101e 844 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
845 "ring %d\n",
e8b62011
JS
846 rc, pmbox->mbxCommand,
847 pmbox->mbxStatus, i);
2e0fef85 848 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
849 ret = -ENXIO;
850 break;
dea3101e 851 }
852 }
ed957684
JS
853 mempool_free(pmb, phba->mbox_mem_pool);
854 return ret;
dea3101e 855}
856
e59058c4 857/**
3621a710 858 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
859 * @phba: Pointer to HBA context object.
860 * @pring: Pointer to driver SLI ring object.
861 * @piocb: Pointer to the driver iocb object.
862 *
863 * This function is called with hbalock held. The function adds the
864 * new iocb to txcmplq of the given ring. This function always returns
865 * 0. If this function is called for ELS ring, this function checks if
866 * there is a vport associated with the ELS command. This function also
867 * starts els_tmofunc timer if this is an ELS command.
868 **/
dea3101e 869static int
2e0fef85
JS
870lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
871 struct lpfc_iocbq *piocb)
dea3101e 872{
dea3101e 873 list_add_tail(&piocb->list, &pring->txcmplq);
874 pring->txcmplq_cnt++;
92d7f7b0
JS
875 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
876 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
877 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
878 if (!piocb->vport)
879 BUG();
880 else
881 mod_timer(&piocb->vport->els_tmofunc,
882 jiffies + HZ * (phba->fc_ratov << 1));
883 }
884
dea3101e 885
2e0fef85 886 return 0;
dea3101e 887}
888
e59058c4 889/**
3621a710 890 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
891 * @phba: Pointer to HBA context object.
892 * @pring: Pointer to driver SLI ring object.
893 *
894 * This function is called with hbalock held to get next
895 * iocb in txq of the given ring. If there is any iocb in
896 * the txq, the function returns first iocb in the list after
897 * removing the iocb from the list, else it returns NULL.
898 **/
dea3101e 899static struct lpfc_iocbq *
2e0fef85 900lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 901{
dea3101e 902 struct lpfc_iocbq *cmd_iocb;
903
858c9f6c
JS
904 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
905 if (cmd_iocb != NULL)
dea3101e 906 pring->txq_cnt--;
2e0fef85 907 return cmd_iocb;
dea3101e 908}
909
e59058c4 910/**
3621a710 911 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
912 * @phba: Pointer to HBA context object.
913 * @pring: Pointer to driver SLI ring object.
914 *
915 * This function is called with hbalock held and the caller must post the
916 * iocb without releasing the lock. If the caller releases the lock,
917 * iocb slot returned by the function is not guaranteed to be available.
918 * The function returns pointer to the next available iocb slot if there
919 * is available slot in the ring, else it returns NULL.
920 * If the get index of the ring is ahead of the put index, the function
921 * will post an error attention event to the worker thread to take the
922 * HBA to offline state.
923 **/
dea3101e 924static IOCB_t *
925lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
926{
34b02dcd 927 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 928 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e 929 if ((pring->next_cmdidx == pring->cmdidx) &&
930 (++pring->next_cmdidx >= max_cmd_idx))
931 pring->next_cmdidx = 0;
932
933 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
934
935 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
936
937 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
938 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 939 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 940 "is bigger than cmd ring %d\n",
e8b62011 941 pring->ringno,
dea3101e 942 pring->local_getidx, max_cmd_idx);
943
2e0fef85 944 phba->link_state = LPFC_HBA_ERROR;
dea3101e 945 /*
946 * All error attention handlers are posted to
947 * worker thread
948 */
949 phba->work_ha |= HA_ERATT;
950 phba->work_hs = HS_FFER3;
92d7f7b0 951
5e9d9b82 952 lpfc_worker_wake_up(phba);
dea3101e 953
954 return NULL;
955 }
956
957 if (pring->local_getidx == pring->next_cmdidx)
958 return NULL;
959 }
960
ed957684 961 return lpfc_cmd_iocb(phba, pring);
dea3101e 962}
963
e59058c4 964/**
3621a710 965 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
966 * @phba: Pointer to HBA context object.
967 * @iocbq: Pointer to driver iocb object.
968 *
969 * This function gets an iotag for the iocb. If there is no unused iotag and
970 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
971 * array and assigns a new iotag.
972 * The function returns the allocated iotag if successful, else returns zero.
973 * Zero is not a valid iotag.
974 * The caller is not required to hold any lock.
975 **/
604a3e30 976uint16_t
2e0fef85 977lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 978{
2e0fef85
JS
979 struct lpfc_iocbq **new_arr;
980 struct lpfc_iocbq **old_arr;
604a3e30
JB
981 size_t new_len;
982 struct lpfc_sli *psli = &phba->sli;
983 uint16_t iotag;
dea3101e 984
2e0fef85 985 spin_lock_irq(&phba->hbalock);
604a3e30
JB
986 iotag = psli->last_iotag;
987 if(++iotag < psli->iocbq_lookup_len) {
988 psli->last_iotag = iotag;
989 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 990 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
991 iocbq->iotag = iotag;
992 return iotag;
2e0fef85 993 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
994 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
995 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
996 spin_unlock_irq(&phba->hbalock);
997 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
998 GFP_KERNEL);
999 if (new_arr) {
2e0fef85 1000 spin_lock_irq(&phba->hbalock);
604a3e30
JB
1001 old_arr = psli->iocbq_lookup;
1002 if (new_len <= psli->iocbq_lookup_len) {
1003 /* highly unprobable case */
1004 kfree(new_arr);
1005 iotag = psli->last_iotag;
1006 if(++iotag < psli->iocbq_lookup_len) {
1007 psli->last_iotag = iotag;
1008 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1009 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1010 iocbq->iotag = iotag;
1011 return iotag;
1012 }
2e0fef85 1013 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1014 return 0;
1015 }
1016 if (psli->iocbq_lookup)
1017 memcpy(new_arr, old_arr,
1018 ((psli->last_iotag + 1) *
311464ec 1019 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1020 psli->iocbq_lookup = new_arr;
1021 psli->iocbq_lookup_len = new_len;
1022 psli->last_iotag = iotag;
1023 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1024 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1025 iocbq->iotag = iotag;
1026 kfree(old_arr);
1027 return iotag;
1028 }
8f6d98d2 1029 } else
2e0fef85 1030 spin_unlock_irq(&phba->hbalock);
dea3101e 1031
604a3e30 1032 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
e8b62011
JS
1033 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1034 psli->last_iotag);
dea3101e 1035
604a3e30 1036 return 0;
dea3101e 1037}
1038
e59058c4 1039/**
3621a710 1040 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1041 * @phba: Pointer to HBA context object.
1042 * @pring: Pointer to driver SLI ring object.
1043 * @iocb: Pointer to iocb slot in the ring.
1044 * @nextiocb: Pointer to driver iocb object which need to be
1045 * posted to firmware.
1046 *
1047 * This function is called with hbalock held to post a new iocb to
1048 * the firmware. This function copies the new iocb to ring iocb slot and
1049 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1050 * a completion call back for this iocb else the function will free the
1051 * iocb object.
1052 **/
dea3101e 1053static void
1054lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1055 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1056{
1057 /*
604a3e30 1058 * Set up an iotag
dea3101e 1059 */
604a3e30 1060 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1061
e2a0a9d6 1062
a58cbd52
JS
1063 if (pring->ringno == LPFC_ELS_RING) {
1064 lpfc_debugfs_slow_ring_trc(phba,
1065 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1066 *(((uint32_t *) &nextiocb->iocb) + 4),
1067 *(((uint32_t *) &nextiocb->iocb) + 6),
1068 *(((uint32_t *) &nextiocb->iocb) + 7));
1069 }
1070
dea3101e 1071 /*
1072 * Issue iocb command to adapter
1073 */
92d7f7b0 1074 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1075 wmb();
1076 pring->stats.iocb_cmd++;
1077
1078 /*
1079 * If there is no completion routine to call, we can release the
1080 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1081 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1082 */
1083 if (nextiocb->iocb_cmpl)
1084 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1085 else
2e0fef85 1086 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1087
1088 /*
1089 * Let the HBA know what IOCB slot will be the next one the
1090 * driver will put a command into.
1091 */
1092 pring->cmdidx = pring->next_cmdidx;
ed957684 1093 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1094}
1095
e59058c4 1096/**
3621a710 1097 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1098 * @phba: Pointer to HBA context object.
1099 * @pring: Pointer to driver SLI ring object.
1100 *
1101 * The caller is not required to hold any lock for calling this function.
1102 * This function updates the chip attention bits for the ring to inform firmware
1103 * that there are pending work to be done for this ring and requests an
1104 * interrupt when there is space available in the ring. This function is
1105 * called when the driver is unable to post more iocbs to the ring due
1106 * to unavailability of space in the ring.
1107 **/
dea3101e 1108static void
2e0fef85 1109lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1110{
1111 int ringno = pring->ringno;
1112
1113 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1114
1115 wmb();
1116
1117 /*
1118 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1119 * The HBA will tell us when an IOCB entry is available.
1120 */
1121 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1122 readl(phba->CAregaddr); /* flush */
1123
1124 pring->stats.iocb_cmd_full++;
1125}
1126
e59058c4 1127/**
3621a710 1128 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1129 * @phba: Pointer to HBA context object.
1130 * @pring: Pointer to driver SLI ring object.
1131 *
1132 * This function updates the chip attention register bit for the
1133 * given ring to inform HBA that there is more work to be done
1134 * in this ring. The caller is not required to hold any lock.
1135 **/
dea3101e 1136static void
2e0fef85 1137lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1138{
1139 int ringno = pring->ringno;
1140
1141 /*
1142 * Tell the HBA that there is work to do in this ring.
1143 */
34b02dcd
JS
1144 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1145 wmb();
1146 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1147 readl(phba->CAregaddr); /* flush */
1148 }
dea3101e 1149}
1150
e59058c4 1151/**
3621a710 1152 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1153 * @phba: Pointer to HBA context object.
1154 * @pring: Pointer to driver SLI ring object.
1155 *
1156 * This function is called with hbalock held to post pending iocbs
1157 * in the txq to the firmware. This function is called when driver
1158 * detects space available in the ring.
1159 **/
dea3101e 1160static void
2e0fef85 1161lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1162{
1163 IOCB_t *iocb;
1164 struct lpfc_iocbq *nextiocb;
1165
1166 /*
1167 * Check to see if:
1168 * (a) there is anything on the txq to send
1169 * (b) link is up
1170 * (c) link attention events can be processed (fcp ring only)
1171 * (d) IOCB processing is not blocked by the outstanding mbox command.
1172 */
1173 if (pring->txq_cnt &&
2e0fef85 1174 lpfc_is_link_up(phba) &&
dea3101e 1175 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1176 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1177
1178 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1179 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1180 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1181
1182 if (iocb)
1183 lpfc_sli_update_ring(phba, pring);
1184 else
1185 lpfc_sli_update_full_ring(phba, pring);
1186 }
1187
1188 return;
1189}
1190
e59058c4 1191/**
3621a710 1192 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1193 * @phba: Pointer to HBA context object.
1194 * @hbqno: HBQ number.
1195 *
1196 * This function is called with hbalock held to get the next
1197 * available slot for the given HBQ. If there is free slot
1198 * available for the HBQ it will return pointer to the next available
1199 * HBQ entry else it will return NULL.
1200 **/
a6ababd2 1201static struct lpfc_hbq_entry *
ed957684
JS
1202lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1203{
1204 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1205
1206 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1207 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1208 hbqp->next_hbqPutIdx = 0;
1209
1210 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1211 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1212 uint32_t getidx = le32_to_cpu(raw_index);
1213
1214 hbqp->local_hbqGetIdx = getidx;
1215
1216 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1217 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1218 LOG_SLI | LOG_VPORT,
e8b62011 1219 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1220 "%u is > than hbqp->entry_count %u\n",
e8b62011 1221 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1222 hbqp->entry_count);
1223
1224 phba->link_state = LPFC_HBA_ERROR;
1225 return NULL;
1226 }
1227
1228 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1229 return NULL;
1230 }
1231
51ef4c26
JS
1232 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1233 hbqp->hbqPutIdx;
ed957684
JS
1234}
1235
e59058c4 1236/**
3621a710 1237 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1238 * @phba: Pointer to HBA context object.
1239 *
1240 * This function is called with no lock held to free all the
1241 * hbq buffers while uninitializing the SLI interface. It also
1242 * frees the HBQ buffers returned by the firmware but not yet
1243 * processed by the upper layers.
1244 **/
ed957684
JS
1245void
1246lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1247{
92d7f7b0
JS
1248 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1249 struct hbq_dmabuf *hbq_buf;
3163f725 1250 unsigned long flags;
51ef4c26 1251 int i, hbq_count;
3163f725 1252 uint32_t hbqno;
ed957684 1253
51ef4c26 1254 hbq_count = lpfc_sli_hbq_count();
ed957684 1255 /* Return all memory used by all HBQs */
3163f725 1256 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1257 for (i = 0; i < hbq_count; ++i) {
1258 list_for_each_entry_safe(dmabuf, next_dmabuf,
1259 &phba->hbqs[i].hbq_buffer_list, list) {
1260 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1261 list_del(&hbq_buf->dbuf.list);
1262 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1263 }
a8adb832 1264 phba->hbqs[i].buffer_count = 0;
ed957684 1265 }
3163f725 1266 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1267 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1268 list) {
3163f725
JS
1269 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1270 list_del(&hbq_buf->dbuf.list);
1271 if (hbq_buf->tag == -1) {
1272 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1273 (phba, hbq_buf);
1274 } else {
1275 hbqno = hbq_buf->tag >> 16;
1276 if (hbqno >= LPFC_MAX_HBQS)
1277 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1278 (phba, hbq_buf);
1279 else
1280 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1281 hbq_buf);
1282 }
1283 }
1284
1285 /* Mark the HBQs not in use */
1286 phba->hbq_in_use = 0;
1287 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1288}
1289
e59058c4 1290/**
3621a710 1291 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1292 * @phba: Pointer to HBA context object.
1293 * @hbqno: HBQ number.
1294 * @hbq_buf: Pointer to HBQ buffer.
1295 *
1296 * This function is called with the hbalock held to post a
1297 * hbq buffer to the firmware. If the function finds an empty
1298 * slot in the HBQ, it will post the buffer. The function will return
1299 * pointer to the hbq entry if it successfully post the buffer
1300 * else it will return NULL.
1301 **/
3772a991 1302static int
ed957684 1303lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1304 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1305{
1306 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1307}
1308
1309/**
1310 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1311 * @phba: Pointer to HBA context object.
1312 * @hbqno: HBQ number.
1313 * @hbq_buf: Pointer to HBQ buffer.
1314 *
1315 * This function is called with the hbalock held to post a hbq buffer to the
1316 * firmware. If the function finds an empty slot in the HBQ, it will post the
1317 * buffer and place it on the hbq_buffer_list. The function will return zero if
1318 * it successfully post the buffer else it will return an error.
1319 **/
1320static int
1321lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1322 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1323{
1324 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1325 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1326
1327 /* Get next HBQ entry slot to use */
1328 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1329 if (hbqe) {
1330 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1331
92d7f7b0
JS
1332 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1333 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1334 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1335 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1336 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1337 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1338 /* Sync SLIM */
ed957684
JS
1339 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1340 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1341 /* flush */
ed957684 1342 readl(phba->hbq_put + hbqno);
51ef4c26 1343 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1344 return 0;
1345 } else
1346 return -ENOMEM;
ed957684
JS
1347}
1348
4f774513
JS
1349/**
1350 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1351 * @phba: Pointer to HBA context object.
1352 * @hbqno: HBQ number.
1353 * @hbq_buf: Pointer to HBQ buffer.
1354 *
1355 * This function is called with the hbalock held to post an RQE to the SLI4
1356 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1357 * the hbq_buffer_list and return zero, otherwise it will return an error.
1358 **/
1359static int
1360lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1361 struct hbq_dmabuf *hbq_buf)
1362{
1363 int rc;
1364 struct lpfc_rqe hrqe;
1365 struct lpfc_rqe drqe;
1366
1367 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1368 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1369 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1370 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1371 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1372 &hrqe, &drqe);
1373 if (rc < 0)
1374 return rc;
1375 hbq_buf->tag = rc;
1376 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1377 return 0;
1378}
1379
e59058c4 1380/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1381static struct lpfc_hbq_init lpfc_els_hbq = {
1382 .rn = 1,
def9c7a9 1383 .entry_count = 256,
92d7f7b0
JS
1384 .mask_count = 0,
1385 .profile = 0,
51ef4c26 1386 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1387 .buffer_count = 0,
a257bf90
JS
1388 .init_count = 40,
1389 .add_count = 40,
92d7f7b0 1390};
ed957684 1391
e59058c4 1392/* HBQ for the extra ring if needed */
51ef4c26
JS
1393static struct lpfc_hbq_init lpfc_extra_hbq = {
1394 .rn = 1,
1395 .entry_count = 200,
1396 .mask_count = 0,
1397 .profile = 0,
1398 .ring_mask = (1 << LPFC_EXTRA_RING),
1399 .buffer_count = 0,
1400 .init_count = 0,
1401 .add_count = 5,
1402};
1403
e59058c4 1404/* Array of HBQs */
78b2d852 1405struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1406 &lpfc_els_hbq,
51ef4c26 1407 &lpfc_extra_hbq,
92d7f7b0 1408};
ed957684 1409
e59058c4 1410/**
3621a710 1411 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1412 * @phba: Pointer to HBA context object.
1413 * @hbqno: HBQ number.
1414 * @count: Number of HBQ buffers to be posted.
1415 *
d7c255b2
JS
1416 * This function is called with no lock held to post more hbq buffers to the
1417 * given HBQ. The function returns the number of HBQ buffers successfully
1418 * posted.
e59058c4 1419 **/
311464ec 1420static int
92d7f7b0 1421lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1422{
d7c255b2 1423 uint32_t i, posted = 0;
3163f725 1424 unsigned long flags;
92d7f7b0 1425 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1426 LIST_HEAD(hbq_buf_list);
eafe1df9 1427 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1428 return 0;
51ef4c26 1429
d7c255b2
JS
1430 if ((phba->hbqs[hbqno].buffer_count + count) >
1431 lpfc_hbq_defs[hbqno]->entry_count)
1432 count = lpfc_hbq_defs[hbqno]->entry_count -
1433 phba->hbqs[hbqno].buffer_count;
1434 if (!count)
1435 return 0;
1436 /* Allocate HBQ entries */
1437 for (i = 0; i < count; i++) {
1438 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1439 if (!hbq_buffer)
1440 break;
1441 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1442 }
3163f725
JS
1443 /* Check whether HBQ is still in use */
1444 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1445 if (!phba->hbq_in_use)
d7c255b2
JS
1446 goto err;
1447 while (!list_empty(&hbq_buf_list)) {
1448 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1449 dbuf.list);
1450 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1451 (hbqno << 16));
3772a991 1452 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1453 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1454 posted++;
1455 } else
51ef4c26 1456 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1457 }
3163f725 1458 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1459 return posted;
1460err:
eafe1df9 1461 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1462 while (!list_empty(&hbq_buf_list)) {
1463 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1464 dbuf.list);
1465 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1466 }
1467 return 0;
ed957684
JS
1468}
1469
e59058c4 1470/**
3621a710 1471 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1472 * @phba: Pointer to HBA context object.
1473 * @qno: HBQ number.
1474 *
1475 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1476 * is called with no lock held. The function returns the number of HBQ entries
1477 * successfully allocated.
e59058c4 1478 **/
92d7f7b0
JS
1479int
1480lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1481{
def9c7a9
JS
1482 if (phba->sli_rev == LPFC_SLI_REV4)
1483 return 0;
1484 else
1485 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1486 lpfc_hbq_defs[qno]->add_count);
92d7f7b0 1487}
ed957684 1488
e59058c4 1489/**
3621a710 1490 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1491 * @phba: Pointer to HBA context object.
1492 * @qno: HBQ queue number.
1493 *
1494 * This function is called from SLI initialization code path with
1495 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1496 * function returns the number of HBQ entries successfully allocated.
e59058c4 1497 **/
a6ababd2 1498static int
92d7f7b0
JS
1499lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1500{
def9c7a9
JS
1501 if (phba->sli_rev == LPFC_SLI_REV4)
1502 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1503 lpfc_hbq_defs[qno]->entry_count);
1504 else
1505 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1506 lpfc_hbq_defs[qno]->init_count);
ed957684
JS
1507}
1508
3772a991
JS
1509/**
1510 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1511 * @phba: Pointer to HBA context object.
1512 * @hbqno: HBQ number.
1513 *
1514 * This function removes the first hbq buffer on an hbq list and returns a
1515 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1516 **/
1517static struct hbq_dmabuf *
1518lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1519{
1520 struct lpfc_dmabuf *d_buf;
1521
1522 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1523 if (!d_buf)
1524 return NULL;
1525 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1526}
1527
e59058c4 1528/**
3621a710 1529 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1530 * @phba: Pointer to HBA context object.
1531 * @tag: Tag of the hbq buffer.
1532 *
1533 * This function is called with hbalock held. This function searches
1534 * for the hbq buffer associated with the given tag in the hbq buffer
1535 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1536 * it returns NULL.
1537 **/
a6ababd2 1538static struct hbq_dmabuf *
92d7f7b0 1539lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1540{
92d7f7b0
JS
1541 struct lpfc_dmabuf *d_buf;
1542 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1543 uint32_t hbqno;
1544
1545 hbqno = tag >> 16;
a0a74e45 1546 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1547 return NULL;
ed957684 1548
3772a991 1549 spin_lock_irq(&phba->hbalock);
51ef4c26 1550 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1551 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1552 if (hbq_buf->tag == tag) {
3772a991 1553 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1554 return hbq_buf;
ed957684
JS
1555 }
1556 }
3772a991 1557 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1558 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1559 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1560 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1561 return NULL;
ed957684
JS
1562}
1563
e59058c4 1564/**
3621a710 1565 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1566 * @phba: Pointer to HBA context object.
1567 * @hbq_buffer: Pointer to HBQ buffer.
1568 *
1569 * This function is called with hbalock. This function gives back
1570 * the hbq buffer to firmware. If the HBQ does not have space to
1571 * post the buffer, it will free the buffer.
1572 **/
ed957684 1573void
51ef4c26 1574lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1575{
1576 uint32_t hbqno;
1577
51ef4c26
JS
1578 if (hbq_buffer) {
1579 hbqno = hbq_buffer->tag >> 16;
3772a991 1580 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1581 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1582 }
1583}
1584
e59058c4 1585/**
3621a710 1586 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1587 * @mbxCommand: mailbox command code.
1588 *
1589 * This function is called by the mailbox event handler function to verify
1590 * that the completed mailbox command is a legitimate mailbox command. If the
1591 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1592 * and the mailbox event handler will take the HBA offline.
1593 **/
dea3101e 1594static int
1595lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1596{
1597 uint8_t ret;
1598
1599 switch (mbxCommand) {
1600 case MBX_LOAD_SM:
1601 case MBX_READ_NV:
1602 case MBX_WRITE_NV:
a8adb832 1603 case MBX_WRITE_VPARMS:
dea3101e 1604 case MBX_RUN_BIU_DIAG:
1605 case MBX_INIT_LINK:
1606 case MBX_DOWN_LINK:
1607 case MBX_CONFIG_LINK:
1608 case MBX_CONFIG_RING:
1609 case MBX_RESET_RING:
1610 case MBX_READ_CONFIG:
1611 case MBX_READ_RCONFIG:
1612 case MBX_READ_SPARM:
1613 case MBX_READ_STATUS:
1614 case MBX_READ_RPI:
1615 case MBX_READ_XRI:
1616 case MBX_READ_REV:
1617 case MBX_READ_LNK_STAT:
1618 case MBX_REG_LOGIN:
1619 case MBX_UNREG_LOGIN:
1620 case MBX_READ_LA:
1621 case MBX_CLEAR_LA:
1622 case MBX_DUMP_MEMORY:
1623 case MBX_DUMP_CONTEXT:
1624 case MBX_RUN_DIAGS:
1625 case MBX_RESTART:
1626 case MBX_UPDATE_CFG:
1627 case MBX_DOWN_LOAD:
1628 case MBX_DEL_LD_ENTRY:
1629 case MBX_RUN_PROGRAM:
1630 case MBX_SET_MASK:
09372820 1631 case MBX_SET_VARIABLE:
dea3101e 1632 case MBX_UNREG_D_ID:
41415862 1633 case MBX_KILL_BOARD:
dea3101e 1634 case MBX_CONFIG_FARP:
41415862 1635 case MBX_BEACON:
dea3101e 1636 case MBX_LOAD_AREA:
1637 case MBX_RUN_BIU_DIAG64:
1638 case MBX_CONFIG_PORT:
1639 case MBX_READ_SPARM64:
1640 case MBX_READ_RPI64:
1641 case MBX_REG_LOGIN64:
1642 case MBX_READ_LA64:
09372820 1643 case MBX_WRITE_WWN:
dea3101e 1644 case MBX_SET_DEBUG:
1645 case MBX_LOAD_EXP_ROM:
57127f15 1646 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
1647 case MBX_REG_VPI:
1648 case MBX_UNREG_VPI:
858c9f6c 1649 case MBX_HEARTBEAT:
84774a4d
JS
1650 case MBX_PORT_CAPABILITIES:
1651 case MBX_PORT_IOV_CONTROL:
04c68496
JS
1652 case MBX_SLI4_CONFIG:
1653 case MBX_SLI4_REQ_FTRS:
1654 case MBX_REG_FCFI:
1655 case MBX_UNREG_FCFI:
1656 case MBX_REG_VFI:
1657 case MBX_UNREG_VFI:
1658 case MBX_INIT_VPI:
1659 case MBX_INIT_VFI:
1660 case MBX_RESUME_RPI:
dea3101e 1661 ret = mbxCommand;
1662 break;
1663 default:
1664 ret = MBX_SHUTDOWN;
1665 break;
1666 }
2e0fef85 1667 return ret;
dea3101e 1668}
e59058c4
JS
1669
1670/**
3621a710 1671 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
1672 * @phba: Pointer to HBA context object.
1673 * @pmboxq: Pointer to mailbox command.
1674 *
1675 * This is completion handler function for mailbox commands issued from
1676 * lpfc_sli_issue_mbox_wait function. This function is called by the
1677 * mailbox event handler function with no lock held. This function
1678 * will wake up thread waiting on the wait queue pointed by context1
1679 * of the mailbox.
1680 **/
04c68496 1681void
2e0fef85 1682lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 1683{
1684 wait_queue_head_t *pdone_q;
858c9f6c 1685 unsigned long drvr_flag;
dea3101e 1686
1687 /*
1688 * If pdone_q is empty, the driver thread gave up waiting and
1689 * continued running.
1690 */
7054a606 1691 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 1692 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e 1693 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1694 if (pdone_q)
1695 wake_up_interruptible(pdone_q);
858c9f6c 1696 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 1697 return;
1698}
1699
e59058c4
JS
1700
1701/**
3621a710 1702 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
1703 * @phba: Pointer to HBA context object.
1704 * @pmb: Pointer to mailbox object.
1705 *
1706 * This function is the default mailbox completion handler. It
1707 * frees the memory resources associated with the completed mailbox
1708 * command. If the completed command is a REG_LOGIN mailbox command,
1709 * this function will issue a UREG_LOGIN to re-claim the RPI.
1710 **/
dea3101e 1711void
2e0fef85 1712lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1713{
1714 struct lpfc_dmabuf *mp;
04c68496 1715 uint16_t rpi, vpi;
7054a606 1716 int rc;
695a814e 1717 struct lpfc_vport *vport = pmb->vport;
7054a606 1718
dea3101e 1719 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 1720
dea3101e 1721 if (mp) {
1722 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1723 kfree(mp);
1724 }
7054a606 1725
04c68496
JS
1726 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1727 (phba->sli_rev == LPFC_SLI_REV4))
1728 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1729
7054a606
JS
1730 /*
1731 * If a REG_LOGIN succeeded after node is destroyed or node
1732 * is in re-discovery driver need to cleanup the RPI.
1733 */
2e0fef85 1734 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
1735 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1736 !pmb->u.mb.mbxStatus) {
1737 rpi = pmb->u.mb.un.varWords[0];
1738 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1739 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 1740 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
1741 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1742 if (rc != MBX_NOT_FINISHED)
1743 return;
1744 }
1745
695a814e
JS
1746 /* Unreg VPI, if the REG_VPI succeed after VLink failure */
1747 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
1748 !(phba->pport->load_flag & FC_UNLOADING) &&
1749 !pmb->u.mb.mbxStatus) {
1750 lpfc_unreg_vpi(phba, pmb->u.mb.un.varRegVpi.vpi, pmb);
1751 pmb->vport = vport;
1752 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
1753 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1754 if (rc != MBX_NOT_FINISHED)
1755 return;
1756 }
1757
04c68496
JS
1758 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1759 lpfc_sli4_mbox_cmd_free(phba, pmb);
1760 else
1761 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1762}
1763
e59058c4 1764/**
3621a710 1765 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
1766 * @phba: Pointer to HBA context object.
1767 *
1768 * This function is called with no lock held. This function processes all
1769 * the completed mailbox commands and gives it to upper layers. The interrupt
1770 * service routine processes mailbox completion interrupt and adds completed
1771 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1772 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1773 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1774 * function returns the mailbox commands to the upper layer by calling the
1775 * completion handler function of each mailbox.
1776 **/
dea3101e 1777int
2e0fef85 1778lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 1779{
92d7f7b0 1780 MAILBOX_t *pmbox;
dea3101e 1781 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
1782 int rc;
1783 LIST_HEAD(cmplq);
dea3101e 1784
1785 phba->sli.slistat.mbox_event++;
1786
92d7f7b0
JS
1787 /* Get all completed mailboxe buffers into the cmplq */
1788 spin_lock_irq(&phba->hbalock);
1789 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1790 spin_unlock_irq(&phba->hbalock);
dea3101e 1791
92d7f7b0
JS
1792 /* Get a Mailbox buffer to setup mailbox commands for callback */
1793 do {
1794 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1795 if (pmb == NULL)
1796 break;
2e0fef85 1797
04c68496 1798 pmbox = &pmb->u.mb;
dea3101e 1799
858c9f6c
JS
1800 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1801 if (pmb->vport) {
1802 lpfc_debugfs_disc_trc(pmb->vport,
1803 LPFC_DISC_TRC_MBOX_VPORT,
1804 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1805 (uint32_t)pmbox->mbxCommand,
1806 pmbox->un.varWords[0],
1807 pmbox->un.varWords[1]);
1808 }
1809 else {
1810 lpfc_debugfs_disc_trc(phba->pport,
1811 LPFC_DISC_TRC_MBOX,
1812 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1813 (uint32_t)pmbox->mbxCommand,
1814 pmbox->un.varWords[0],
1815 pmbox->un.varWords[1]);
1816 }
1817 }
1818
dea3101e 1819 /*
1820 * It is a fatal error if unknown mbox command completion.
1821 */
1822 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1823 MBX_SHUTDOWN) {
af901ca1 1824 /* Unknown mailbox command compl */
92d7f7b0 1825 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 1826 "(%d):0323 Unknown Mailbox command "
04c68496 1827 "x%x (x%x) Cmpl\n",
92d7f7b0 1828 pmb->vport ? pmb->vport->vpi : 0,
04c68496
JS
1829 pmbox->mbxCommand,
1830 lpfc_sli4_mbox_opcode_get(phba, pmb));
2e0fef85 1831 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1832 phba->work_hs = HS_FFER3;
1833 lpfc_handle_eratt(phba);
92d7f7b0 1834 continue;
dea3101e 1835 }
1836
dea3101e 1837 if (pmbox->mbxStatus) {
1838 phba->sli.slistat.mbox_stat_err++;
1839 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1840 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0
JS
1841 lpfc_printf_log(phba, KERN_INFO,
1842 LOG_MBOX | LOG_SLI,
e8b62011 1843 "(%d):0305 Mbox cmd cmpl "
92d7f7b0 1844 "error - RETRYing Data: x%x "
04c68496 1845 "(x%x) x%x x%x x%x\n",
92d7f7b0
JS
1846 pmb->vport ? pmb->vport->vpi :0,
1847 pmbox->mbxCommand,
04c68496
JS
1848 lpfc_sli4_mbox_opcode_get(phba,
1849 pmb),
92d7f7b0
JS
1850 pmbox->mbxStatus,
1851 pmbox->un.varWords[0],
1852 pmb->vport->port_state);
dea3101e 1853 pmbox->mbxStatus = 0;
1854 pmbox->mbxOwner = OWN_HOST;
dea3101e 1855 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 1856 if (rc != MBX_NOT_FINISHED)
92d7f7b0 1857 continue;
dea3101e 1858 }
1859 }
1860
1861 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 1862 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
04c68496 1863 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea3101e 1864 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 1865 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 1866 pmbox->mbxCommand,
04c68496 1867 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea3101e 1868 pmb->mbox_cmpl,
1869 *((uint32_t *) pmbox),
1870 pmbox->un.varWords[0],
1871 pmbox->un.varWords[1],
1872 pmbox->un.varWords[2],
1873 pmbox->un.varWords[3],
1874 pmbox->un.varWords[4],
1875 pmbox->un.varWords[5],
1876 pmbox->un.varWords[6],
1877 pmbox->un.varWords[7]);
1878
92d7f7b0 1879 if (pmb->mbox_cmpl)
dea3101e 1880 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
1881 } while (1);
1882 return 0;
1883}
dea3101e 1884
e59058c4 1885/**
3621a710 1886 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
1887 * @phba: Pointer to HBA context object.
1888 * @pring: Pointer to driver SLI ring object.
1889 * @tag: buffer tag.
1890 *
1891 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1892 * is set in the tag the buffer is posted for a particular exchange,
1893 * the function will return the buffer without replacing the buffer.
1894 * If the buffer is for unsolicited ELS or CT traffic, this function
1895 * returns the buffer and also posts another buffer to the firmware.
1896 **/
76bb24ef
JS
1897static struct lpfc_dmabuf *
1898lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
1899 struct lpfc_sli_ring *pring,
1900 uint32_t tag)
76bb24ef 1901{
9f1e1b50
JS
1902 struct hbq_dmabuf *hbq_entry;
1903
76bb24ef
JS
1904 if (tag & QUE_BUFTAG_BIT)
1905 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
1906 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1907 if (!hbq_entry)
1908 return NULL;
1909 return &hbq_entry->dbuf;
76bb24ef 1910}
57127f15 1911
3772a991
JS
1912/**
1913 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1914 * @phba: Pointer to HBA context object.
1915 * @pring: Pointer to driver SLI ring object.
1916 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1917 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1918 * @fch_type: the type for the first frame of the sequence.
1919 *
1920 * This function is called with no lock held. This function uses the r_ctl and
1921 * type of the received sequence to find the correct callback function to call
1922 * to process the sequence.
1923 **/
1924static int
1925lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1926 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1927 uint32_t fch_type)
1928{
1929 int i;
1930
1931 /* unSolicited Responses */
1932 if (pring->prt[0].profile) {
1933 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1934 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1935 saveq);
1936 return 1;
1937 }
1938 /* We must search, based on rctl / type
1939 for the right routine */
1940 for (i = 0; i < pring->num_mask; i++) {
1941 if ((pring->prt[i].rctl == fch_r_ctl) &&
1942 (pring->prt[i].type == fch_type)) {
1943 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1944 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1945 (phba, pring, saveq);
1946 return 1;
1947 }
1948 }
1949 return 0;
1950}
e59058c4
JS
1951
1952/**
3621a710 1953 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
1954 * @phba: Pointer to HBA context object.
1955 * @pring: Pointer to driver SLI ring object.
1956 * @saveq: Pointer to the unsolicited iocb.
1957 *
1958 * This function is called with no lock held by the ring event handler
1959 * when there is an unsolicited iocb posted to the response ring by the
1960 * firmware. This function gets the buffer associated with the iocbs
1961 * and calls the event handler for the ring. This function handles both
1962 * qring buffers and hbq buffers.
1963 * When the function returns 1 the caller can free the iocb object otherwise
1964 * upper layer functions will free the iocb objects.
1965 **/
dea3101e 1966static int
1967lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1968 struct lpfc_iocbq *saveq)
1969{
1970 IOCB_t * irsp;
1971 WORD5 * w5p;
1972 uint32_t Rctl, Type;
3772a991 1973 uint32_t match;
76bb24ef 1974 struct lpfc_iocbq *iocbq;
3163f725 1975 struct lpfc_dmabuf *dmzbuf;
dea3101e 1976
1977 match = 0;
1978 irsp = &(saveq->iocb);
57127f15
JS
1979
1980 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1981 if (pring->lpfc_sli_rcv_async_status)
1982 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1983 else
1984 lpfc_printf_log(phba,
1985 KERN_WARNING,
1986 LOG_SLI,
1987 "0316 Ring %d handler: unexpected "
1988 "ASYNC_STATUS iocb received evt_code "
1989 "0x%x\n",
1990 pring->ringno,
1991 irsp->un.asyncstat.evt_code);
1992 return 1;
1993 }
1994
3163f725
JS
1995 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1996 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1997 if (irsp->ulpBdeCount > 0) {
1998 dmzbuf = lpfc_sli_get_buff(phba, pring,
1999 irsp->un.ulpWord[3]);
2000 lpfc_in_buf_free(phba, dmzbuf);
2001 }
2002
2003 if (irsp->ulpBdeCount > 1) {
2004 dmzbuf = lpfc_sli_get_buff(phba, pring,
2005 irsp->unsli3.sli3Words[3]);
2006 lpfc_in_buf_free(phba, dmzbuf);
2007 }
2008
2009 if (irsp->ulpBdeCount > 2) {
2010 dmzbuf = lpfc_sli_get_buff(phba, pring,
2011 irsp->unsli3.sli3Words[7]);
2012 lpfc_in_buf_free(phba, dmzbuf);
2013 }
2014
2015 return 1;
2016 }
2017
92d7f7b0 2018 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
2019 if (irsp->ulpBdeCount != 0) {
2020 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2021 irsp->un.ulpWord[3]);
2022 if (!saveq->context2)
2023 lpfc_printf_log(phba,
2024 KERN_ERR,
2025 LOG_SLI,
2026 "0341 Ring %d Cannot find buffer for "
2027 "an unsolicited iocb. tag 0x%x\n",
2028 pring->ringno,
2029 irsp->un.ulpWord[3]);
76bb24ef
JS
2030 }
2031 if (irsp->ulpBdeCount == 2) {
2032 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2033 irsp->unsli3.sli3Words[7]);
2034 if (!saveq->context3)
2035 lpfc_printf_log(phba,
2036 KERN_ERR,
2037 LOG_SLI,
2038 "0342 Ring %d Cannot find buffer for an"
2039 " unsolicited iocb. tag 0x%x\n",
2040 pring->ringno,
2041 irsp->unsli3.sli3Words[7]);
2042 }
2043 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2044 irsp = &(iocbq->iocb);
76bb24ef
JS
2045 if (irsp->ulpBdeCount != 0) {
2046 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2047 irsp->un.ulpWord[3]);
9c2face6 2048 if (!iocbq->context2)
76bb24ef
JS
2049 lpfc_printf_log(phba,
2050 KERN_ERR,
2051 LOG_SLI,
2052 "0343 Ring %d Cannot find "
2053 "buffer for an unsolicited iocb"
2054 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2055 irsp->un.ulpWord[3]);
76bb24ef
JS
2056 }
2057 if (irsp->ulpBdeCount == 2) {
2058 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2059 irsp->unsli3.sli3Words[7]);
9c2face6 2060 if (!iocbq->context3)
76bb24ef
JS
2061 lpfc_printf_log(phba,
2062 KERN_ERR,
2063 LOG_SLI,
2064 "0344 Ring %d Cannot find "
2065 "buffer for an unsolicited "
2066 "iocb. tag 0x%x\n",
2067 pring->ringno,
2068 irsp->unsli3.sli3Words[7]);
2069 }
2070 }
92d7f7b0 2071 }
9c2face6
JS
2072 if (irsp->ulpBdeCount != 0 &&
2073 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2074 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2075 int found = 0;
2076
2077 /* search continue save q for same XRI */
2078 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2079 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2080 list_add_tail(&saveq->list, &iocbq->list);
2081 found = 1;
2082 break;
2083 }
2084 }
2085 if (!found)
2086 list_add_tail(&saveq->clist,
2087 &pring->iocb_continue_saveq);
2088 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2089 list_del_init(&iocbq->clist);
2090 saveq = iocbq;
2091 irsp = &(saveq->iocb);
2092 } else
2093 return 0;
2094 }
2095 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2096 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2097 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2098 Rctl = FC_RCTL_ELS_REQ;
2099 Type = FC_TYPE_ELS;
9c2face6
JS
2100 } else {
2101 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2102 Rctl = w5p->hcsw.Rctl;
2103 Type = w5p->hcsw.Type;
2104
2105 /* Firmware Workaround */
2106 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2107 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2108 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2109 Rctl = FC_RCTL_ELS_REQ;
2110 Type = FC_TYPE_ELS;
9c2face6
JS
2111 w5p->hcsw.Rctl = Rctl;
2112 w5p->hcsw.Type = Type;
2113 }
2114 }
92d7f7b0 2115
3772a991 2116 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2117 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2118 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2119 "Type x%x received\n",
e8b62011 2120 pring->ringno, Rctl, Type);
3772a991 2121
92d7f7b0 2122 return 1;
dea3101e 2123}
2124
e59058c4 2125/**
3621a710 2126 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2127 * @phba: Pointer to HBA context object.
2128 * @pring: Pointer to driver SLI ring object.
2129 * @prspiocb: Pointer to response iocb object.
2130 *
2131 * This function looks up the iocb_lookup table to get the command iocb
2132 * corresponding to the given response iocb using the iotag of the
2133 * response iocb. This function is called with the hbalock held.
2134 * This function returns the command iocb object if it finds the command
2135 * iocb else returns NULL.
2136 **/
dea3101e 2137static struct lpfc_iocbq *
2e0fef85
JS
2138lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2139 struct lpfc_sli_ring *pring,
2140 struct lpfc_iocbq *prspiocb)
dea3101e 2141{
dea3101e 2142 struct lpfc_iocbq *cmd_iocb = NULL;
2143 uint16_t iotag;
2144
604a3e30
JB
2145 iotag = prspiocb->iocb.ulpIoTag;
2146
2147 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2148 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2149 list_del_init(&cmd_iocb->list);
604a3e30
JB
2150 pring->txcmplq_cnt--;
2151 return cmd_iocb;
dea3101e 2152 }
2153
dea3101e 2154 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2155 "0317 iotag x%x is out off "
604a3e30 2156 "range: max iotag x%x wd0 x%x\n",
e8b62011 2157 iotag, phba->sli.last_iotag,
604a3e30 2158 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2159 return NULL;
2160}
2161
3772a991
JS
2162/**
2163 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2164 * @phba: Pointer to HBA context object.
2165 * @pring: Pointer to driver SLI ring object.
2166 * @iotag: IOCB tag.
2167 *
2168 * This function looks up the iocb_lookup table to get the command iocb
2169 * corresponding to the given iotag. This function is called with the
2170 * hbalock held.
2171 * This function returns the command iocb object if it finds the command
2172 * iocb else returns NULL.
2173 **/
2174static struct lpfc_iocbq *
2175lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2176 struct lpfc_sli_ring *pring, uint16_t iotag)
2177{
2178 struct lpfc_iocbq *cmd_iocb;
2179
2180 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2181 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2182 list_del_init(&cmd_iocb->list);
2183 pring->txcmplq_cnt--;
2184 return cmd_iocb;
2185 }
2186
2187 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2188 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2189 iotag, phba->sli.last_iotag);
2190 return NULL;
2191}
2192
e59058c4 2193/**
3621a710 2194 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2195 * @phba: Pointer to HBA context object.
2196 * @pring: Pointer to driver SLI ring object.
2197 * @saveq: Pointer to the response iocb to be processed.
2198 *
2199 * This function is called by the ring event handler for non-fcp
2200 * rings when there is a new response iocb in the response ring.
2201 * The caller is not required to hold any locks. This function
2202 * gets the command iocb associated with the response iocb and
2203 * calls the completion handler for the command iocb. If there
2204 * is no completion handler, the function will free the resources
2205 * associated with command iocb. If the response iocb is for
2206 * an already aborted command iocb, the status of the completion
2207 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2208 * This function always returns 1.
2209 **/
dea3101e 2210static int
2e0fef85 2211lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 2212 struct lpfc_iocbq *saveq)
2213{
2e0fef85 2214 struct lpfc_iocbq *cmdiocbp;
dea3101e 2215 int rc = 1;
2216 unsigned long iflag;
2217
2218 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2219 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2220 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2221 spin_unlock_irqrestore(&phba->hbalock, iflag);
2222
dea3101e 2223 if (cmdiocbp) {
2224 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2225 /*
2226 * If an ELS command failed send an event to mgmt
2227 * application.
2228 */
2229 if (saveq->iocb.ulpStatus &&
2230 (pring->ringno == LPFC_ELS_RING) &&
2231 (cmdiocbp->iocb.ulpCommand ==
2232 CMD_ELS_REQUEST64_CR))
2233 lpfc_send_els_failure_event(phba,
2234 cmdiocbp, saveq);
2235
dea3101e 2236 /*
2237 * Post all ELS completions to the worker thread.
2238 * All other are passed to the completion callback.
2239 */
2240 if (pring->ringno == LPFC_ELS_RING) {
341af102
JS
2241 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2242 (cmdiocbp->iocb_flag &
2243 LPFC_DRIVER_ABORTED)) {
2244 spin_lock_irqsave(&phba->hbalock,
2245 iflag);
07951076
JS
2246 cmdiocbp->iocb_flag &=
2247 ~LPFC_DRIVER_ABORTED;
341af102
JS
2248 spin_unlock_irqrestore(&phba->hbalock,
2249 iflag);
07951076
JS
2250 saveq->iocb.ulpStatus =
2251 IOSTAT_LOCAL_REJECT;
2252 saveq->iocb.un.ulpWord[4] =
2253 IOERR_SLI_ABORTED;
0ff10d46
JS
2254
2255 /* Firmware could still be in progress
2256 * of DMAing payload, so don't free data
2257 * buffer till after a hbeat.
2258 */
341af102
JS
2259 spin_lock_irqsave(&phba->hbalock,
2260 iflag);
0ff10d46 2261 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
341af102
JS
2262 spin_unlock_irqrestore(&phba->hbalock,
2263 iflag);
2264 }
0f65ff68
JS
2265 if (phba->sli_rev == LPFC_SLI_REV4) {
2266 if (saveq->iocb_flag &
2267 LPFC_EXCHANGE_BUSY) {
2268 /* Set cmdiocb flag for the
2269 * exchange busy so sgl (xri)
2270 * will not be released until
2271 * the abort xri is received
2272 * from hba.
2273 */
2274 spin_lock_irqsave(
2275 &phba->hbalock, iflag);
2276 cmdiocbp->iocb_flag |=
2277 LPFC_EXCHANGE_BUSY;
2278 spin_unlock_irqrestore(
2279 &phba->hbalock, iflag);
2280 }
2281 if (cmdiocbp->iocb_flag &
2282 LPFC_DRIVER_ABORTED) {
2283 /*
2284 * Clear LPFC_DRIVER_ABORTED
2285 * bit in case it was driver
2286 * initiated abort.
2287 */
2288 spin_lock_irqsave(
2289 &phba->hbalock, iflag);
2290 cmdiocbp->iocb_flag &=
2291 ~LPFC_DRIVER_ABORTED;
2292 spin_unlock_irqrestore(
2293 &phba->hbalock, iflag);
2294 cmdiocbp->iocb.ulpStatus =
2295 IOSTAT_LOCAL_REJECT;
2296 cmdiocbp->iocb.un.ulpWord[4] =
2297 IOERR_ABORT_REQUESTED;
2298 /*
2299 * For SLI4, irsiocb contains
2300 * NO_XRI in sli_xritag, it
2301 * shall not affect releasing
2302 * sgl (xri) process.
2303 */
2304 saveq->iocb.ulpStatus =
2305 IOSTAT_LOCAL_REJECT;
2306 saveq->iocb.un.ulpWord[4] =
2307 IOERR_SLI_ABORTED;
2308 spin_lock_irqsave(
2309 &phba->hbalock, iflag);
2310 saveq->iocb_flag |=
2311 LPFC_DELAY_MEM_FREE;
2312 spin_unlock_irqrestore(
2313 &phba->hbalock, iflag);
2314 }
07951076 2315 }
dea3101e 2316 }
2e0fef85 2317 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2318 } else
2319 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 2320 } else {
2321 /*
2322 * Unknown initiating command based on the response iotag.
2323 * This could be the case on the ELS ring because of
2324 * lpfc_els_abort().
2325 */
2326 if (pring->ringno != LPFC_ELS_RING) {
2327 /*
2328 * Ring <ringno> handler: unexpected completion IoTag
2329 * <IoTag>
2330 */
a257bf90 2331 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2332 "0322 Ring %d handler: "
2333 "unexpected completion IoTag x%x "
2334 "Data: x%x x%x x%x x%x\n",
2335 pring->ringno,
2336 saveq->iocb.ulpIoTag,
2337 saveq->iocb.ulpStatus,
2338 saveq->iocb.un.ulpWord[4],
2339 saveq->iocb.ulpCommand,
2340 saveq->iocb.ulpContext);
dea3101e 2341 }
2342 }
68876920 2343
dea3101e 2344 return rc;
2345}
2346
e59058c4 2347/**
3621a710 2348 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2349 * @phba: Pointer to HBA context object.
2350 * @pring: Pointer to driver SLI ring object.
2351 *
2352 * This function is called from the iocb ring event handlers when
2353 * put pointer is ahead of the get pointer for a ring. This function signal
2354 * an error attention condition to the worker thread and the worker
2355 * thread will transition the HBA to offline state.
2356 **/
2e0fef85
JS
2357static void
2358lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2359{
34b02dcd 2360 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2361 /*
025dfdaf 2362 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2363 * rsp ring <portRspMax>
2364 */
2365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2366 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2367 "is bigger than rsp ring %d\n",
e8b62011 2368 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2369 pring->numRiocb);
2370
2e0fef85 2371 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2372
2373 /*
2374 * All error attention handlers are posted to
2375 * worker thread
2376 */
2377 phba->work_ha |= HA_ERATT;
2378 phba->work_hs = HS_FFER3;
92d7f7b0 2379
5e9d9b82 2380 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2381
2382 return;
2383}
2384
9399627f 2385/**
3621a710 2386 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2387 * @ptr: Pointer to address of HBA context object.
2388 *
2389 * This function is invoked by the Error Attention polling timer when the
2390 * timer times out. It will check the SLI Error Attention register for
2391 * possible attention events. If so, it will post an Error Attention event
2392 * and wake up worker thread to process it. Otherwise, it will set up the
2393 * Error Attention polling timer for the next poll.
2394 **/
2395void lpfc_poll_eratt(unsigned long ptr)
2396{
2397 struct lpfc_hba *phba;
2398 uint32_t eratt = 0;
2399
2400 phba = (struct lpfc_hba *)ptr;
2401
2402 /* Check chip HA register for error event */
2403 eratt = lpfc_sli_check_eratt(phba);
2404
2405 if (eratt)
2406 /* Tell the worker thread there is work to do */
2407 lpfc_worker_wake_up(phba);
2408 else
2409 /* Restart the timer for next eratt poll */
2410 mod_timer(&phba->eratt_poll, jiffies +
2411 HZ * LPFC_ERATT_POLL_INTERVAL);
2412 return;
2413}
2414
875fbdfe 2415
e59058c4 2416/**
3621a710 2417 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2418 * @phba: Pointer to HBA context object.
2419 * @pring: Pointer to driver SLI ring object.
2420 * @mask: Host attention register mask for this ring.
2421 *
2422 * This function is called from the interrupt context when there is a ring
2423 * event for the fcp ring. The caller does not hold any lock.
2424 * The function processes each response iocb in the response ring until it
2425 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2426 * LE bit set. The function will call the completion handler of the command iocb
2427 * if the response iocb indicates a completion for a command iocb or it is
2428 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2429 * function if this is an unsolicited iocb.
dea3101e 2430 * This routine presumes LPFC_FCP_RING handling and doesn't bother
45ed1190
JS
2431 * to check it explicitly.
2432 */
2433int
2e0fef85
JS
2434lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2435 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2436{
34b02dcd 2437 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2438 IOCB_t *irsp = NULL;
87f6eaff 2439 IOCB_t *entry = NULL;
dea3101e 2440 struct lpfc_iocbq *cmdiocbq = NULL;
2441 struct lpfc_iocbq rspiocbq;
dea3101e 2442 uint32_t status;
2443 uint32_t portRspPut, portRspMax;
2444 int rc = 1;
2445 lpfc_iocb_type type;
2446 unsigned long iflag;
2447 uint32_t rsp_cmpl = 0;
dea3101e 2448
2e0fef85 2449 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2450 pring->stats.iocb_event++;
2451
dea3101e 2452 /*
2453 * The next available response entry should never exceed the maximum
2454 * entries. If it does, treat it as an adapter hardware error.
2455 */
2456 portRspMax = pring->numRiocb;
2457 portRspPut = le32_to_cpu(pgp->rspPutInx);
2458 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2459 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2460 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2461 return 1;
2462 }
45ed1190
JS
2463 if (phba->fcp_ring_in_use) {
2464 spin_unlock_irqrestore(&phba->hbalock, iflag);
2465 return 1;
2466 } else
2467 phba->fcp_ring_in_use = 1;
dea3101e 2468
2469 rmb();
2470 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2471 /*
2472 * Fetch an entry off the ring and copy it into a local data
2473 * structure. The copy involves a byte-swap since the
2474 * network byte order and pci byte orders are different.
2475 */
ed957684 2476 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2477 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2478
2479 if (++pring->rspidx >= portRspMax)
2480 pring->rspidx = 0;
2481
87f6eaff
JSEC
2482 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2483 (uint32_t *) &rspiocbq.iocb,
ed957684 2484 phba->iocb_rsp_size);
a4bc3379 2485 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2486 irsp = &rspiocbq.iocb;
2487
dea3101e 2488 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2489 pring->stats.iocb_rsp++;
2490 rsp_cmpl++;
2491
2492 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2493 /*
2494 * If resource errors reported from HBA, reduce
2495 * queuedepths of the SCSI device.
2496 */
2497 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2498 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2499 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2500 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2501 spin_lock_irqsave(&phba->hbalock, iflag);
2502 }
2503
dea3101e 2504 /* Rsp ring <ringno> error: IOCB */
2505 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2506 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2507 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2508 pring->ringno,
92d7f7b0
JS
2509 irsp->un.ulpWord[0],
2510 irsp->un.ulpWord[1],
2511 irsp->un.ulpWord[2],
2512 irsp->un.ulpWord[3],
2513 irsp->un.ulpWord[4],
2514 irsp->un.ulpWord[5],
d7c255b2
JS
2515 *(uint32_t *)&irsp->un1,
2516 *((uint32_t *)&irsp->un1 + 1));
dea3101e 2517 }
2518
2519 switch (type) {
2520 case LPFC_ABORT_IOCB:
2521 case LPFC_SOL_IOCB:
2522 /*
2523 * Idle exchange closed via ABTS from port. No iocb
2524 * resources need to be recovered.
2525 */
2526 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2527 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2528 "0333 IOCB cmd 0x%x"
dca9479b 2529 " processed. Skipping"
92d7f7b0 2530 " completion\n",
dca9479b 2531 irsp->ulpCommand);
dea3101e 2532 break;
2533 }
2534
604a3e30
JB
2535 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2536 &rspiocbq);
0f65ff68
JS
2537 if (unlikely(!cmdiocbq))
2538 break;
2539 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
2540 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
2541 if (cmdiocbq->iocb_cmpl) {
2542 spin_unlock_irqrestore(&phba->hbalock, iflag);
2543 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2544 &rspiocbq);
2545 spin_lock_irqsave(&phba->hbalock, iflag);
2546 }
dea3101e 2547 break;
a4bc3379 2548 case LPFC_UNSOL_IOCB:
2e0fef85 2549 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2550 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2551 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2552 break;
dea3101e 2553 default:
2554 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2555 char adaptermsg[LPFC_MAX_ADPTMSG];
2556 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2557 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2558 MAX_MSG_DATA);
898eb71c
JP
2559 dev_warn(&((phba->pcidev)->dev),
2560 "lpfc%d: %s\n",
dea3101e 2561 phba->brd_no, adaptermsg);
2562 } else {
2563 /* Unknown IOCB command */
2564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2565 "0334 Unknown IOCB command "
92d7f7b0 2566 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2567 type, irsp->ulpCommand,
92d7f7b0
JS
2568 irsp->ulpStatus,
2569 irsp->ulpIoTag,
2570 irsp->ulpContext);
dea3101e 2571 }
2572 break;
2573 }
2574
2575 /*
2576 * The response IOCB has been processed. Update the ring
2577 * pointer in SLIM. If the port response put pointer has not
2578 * been updated, sync the pgp->rspPutInx and fetch the new port
2579 * response put pointer.
2580 */
ed957684 2581 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2582
2583 if (pring->rspidx == portRspPut)
2584 portRspPut = le32_to_cpu(pgp->rspPutInx);
2585 }
2586
2587 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2588 pring->stats.iocb_rsp_full++;
2589 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2590 writel(status, phba->CAregaddr);
2591 readl(phba->CAregaddr);
2592 }
2593 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2594 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2595 pring->stats.iocb_cmd_empty++;
2596
2597 /* Force update of the local copy of cmdGetInx */
2598 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2599 lpfc_sli_resume_iocb(phba, pring);
2600
2601 if ((pring->lpfc_sli_cmd_available))
2602 (pring->lpfc_sli_cmd_available) (phba, pring);
2603
2604 }
2605
45ed1190 2606 phba->fcp_ring_in_use = 0;
2e0fef85 2607 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2608 return rc;
2609}
2610
e59058c4 2611/**
3772a991
JS
2612 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2613 * @phba: Pointer to HBA context object.
2614 * @pring: Pointer to driver SLI ring object.
2615 * @rspiocbp: Pointer to driver response IOCB object.
2616 *
2617 * This function is called from the worker thread when there is a slow-path
2618 * response IOCB to process. This function chains all the response iocbs until
2619 * seeing the iocb with the LE bit set. The function will call
2620 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2621 * completion of a command iocb. The function will call the
2622 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2623 * The function frees the resources or calls the completion handler if this
2624 * iocb is an abort completion. The function returns NULL when the response
2625 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2626 * this function shall chain the iocb on to the iocb_continueq and return the
2627 * response iocb passed in.
2628 **/
2629static struct lpfc_iocbq *
2630lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2631 struct lpfc_iocbq *rspiocbp)
2632{
2633 struct lpfc_iocbq *saveq;
2634 struct lpfc_iocbq *cmdiocbp;
2635 struct lpfc_iocbq *next_iocb;
2636 IOCB_t *irsp = NULL;
2637 uint32_t free_saveq;
2638 uint8_t iocb_cmd_type;
2639 lpfc_iocb_type type;
2640 unsigned long iflag;
2641 int rc;
2642
2643 spin_lock_irqsave(&phba->hbalock, iflag);
2644 /* First add the response iocb to the countinueq list */
2645 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2646 pring->iocb_continueq_cnt++;
2647
2648 /* Now, determine whetehr the list is completed for processing */
2649 irsp = &rspiocbp->iocb;
2650 if (irsp->ulpLe) {
2651 /*
2652 * By default, the driver expects to free all resources
2653 * associated with this iocb completion.
2654 */
2655 free_saveq = 1;
2656 saveq = list_get_first(&pring->iocb_continueq,
2657 struct lpfc_iocbq, list);
2658 irsp = &(saveq->iocb);
2659 list_del_init(&pring->iocb_continueq);
2660 pring->iocb_continueq_cnt = 0;
2661
2662 pring->stats.iocb_rsp++;
2663
2664 /*
2665 * If resource errors reported from HBA, reduce
2666 * queuedepths of the SCSI device.
2667 */
2668 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2669 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2670 spin_unlock_irqrestore(&phba->hbalock, iflag);
2671 phba->lpfc_rampdown_queue_depth(phba);
2672 spin_lock_irqsave(&phba->hbalock, iflag);
2673 }
2674
2675 if (irsp->ulpStatus) {
2676 /* Rsp ring <ringno> error: IOCB */
2677 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2678 "0328 Rsp Ring %d error: "
2679 "IOCB Data: "
2680 "x%x x%x x%x x%x "
2681 "x%x x%x x%x x%x "
2682 "x%x x%x x%x x%x "
2683 "x%x x%x x%x x%x\n",
2684 pring->ringno,
2685 irsp->un.ulpWord[0],
2686 irsp->un.ulpWord[1],
2687 irsp->un.ulpWord[2],
2688 irsp->un.ulpWord[3],
2689 irsp->un.ulpWord[4],
2690 irsp->un.ulpWord[5],
2691 *(((uint32_t *) irsp) + 6),
2692 *(((uint32_t *) irsp) + 7),
2693 *(((uint32_t *) irsp) + 8),
2694 *(((uint32_t *) irsp) + 9),
2695 *(((uint32_t *) irsp) + 10),
2696 *(((uint32_t *) irsp) + 11),
2697 *(((uint32_t *) irsp) + 12),
2698 *(((uint32_t *) irsp) + 13),
2699 *(((uint32_t *) irsp) + 14),
2700 *(((uint32_t *) irsp) + 15));
2701 }
2702
2703 /*
2704 * Fetch the IOCB command type and call the correct completion
2705 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2706 * get freed back to the lpfc_iocb_list by the discovery
2707 * kernel thread.
2708 */
2709 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2710 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2711 switch (type) {
2712 case LPFC_SOL_IOCB:
2713 spin_unlock_irqrestore(&phba->hbalock, iflag);
2714 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2715 spin_lock_irqsave(&phba->hbalock, iflag);
2716 break;
2717
2718 case LPFC_UNSOL_IOCB:
2719 spin_unlock_irqrestore(&phba->hbalock, iflag);
2720 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2721 spin_lock_irqsave(&phba->hbalock, iflag);
2722 if (!rc)
2723 free_saveq = 0;
2724 break;
2725
2726 case LPFC_ABORT_IOCB:
2727 cmdiocbp = NULL;
2728 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2729 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2730 saveq);
2731 if (cmdiocbp) {
2732 /* Call the specified completion routine */
2733 if (cmdiocbp->iocb_cmpl) {
2734 spin_unlock_irqrestore(&phba->hbalock,
2735 iflag);
2736 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2737 saveq);
2738 spin_lock_irqsave(&phba->hbalock,
2739 iflag);
2740 } else
2741 __lpfc_sli_release_iocbq(phba,
2742 cmdiocbp);
2743 }
2744 break;
2745
2746 case LPFC_UNKNOWN_IOCB:
2747 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2748 char adaptermsg[LPFC_MAX_ADPTMSG];
2749 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2750 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2751 MAX_MSG_DATA);
2752 dev_warn(&((phba->pcidev)->dev),
2753 "lpfc%d: %s\n",
2754 phba->brd_no, adaptermsg);
2755 } else {
2756 /* Unknown IOCB command */
2757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2758 "0335 Unknown IOCB "
2759 "command Data: x%x "
2760 "x%x x%x x%x\n",
2761 irsp->ulpCommand,
2762 irsp->ulpStatus,
2763 irsp->ulpIoTag,
2764 irsp->ulpContext);
2765 }
2766 break;
2767 }
2768
2769 if (free_saveq) {
2770 list_for_each_entry_safe(rspiocbp, next_iocb,
2771 &saveq->list, list) {
2772 list_del(&rspiocbp->list);
2773 __lpfc_sli_release_iocbq(phba, rspiocbp);
2774 }
2775 __lpfc_sli_release_iocbq(phba, saveq);
2776 }
2777 rspiocbp = NULL;
2778 }
2779 spin_unlock_irqrestore(&phba->hbalock, iflag);
2780 return rspiocbp;
2781}
2782
2783/**
2784 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
2785 * @phba: Pointer to HBA context object.
2786 * @pring: Pointer to driver SLI ring object.
2787 * @mask: Host attention register mask for this ring.
2788 *
3772a991
JS
2789 * This routine wraps the actual slow_ring event process routine from the
2790 * API jump table function pointer from the lpfc_hba struct.
e59058c4 2791 **/
3772a991 2792void
2e0fef85
JS
2793lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2794 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
2795{
2796 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2797}
2798
2799/**
2800 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2801 * @phba: Pointer to HBA context object.
2802 * @pring: Pointer to driver SLI ring object.
2803 * @mask: Host attention register mask for this ring.
2804 *
2805 * This function is called from the worker thread when there is a ring event
2806 * for non-fcp rings. The caller does not hold any lock. The function will
2807 * remove each response iocb in the response ring and calls the handle
2808 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2809 **/
2810static void
2811lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2812 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2813{
34b02dcd 2814 struct lpfc_pgp *pgp;
dea3101e 2815 IOCB_t *entry;
2816 IOCB_t *irsp = NULL;
2817 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 2818 uint32_t portRspPut, portRspMax;
dea3101e 2819 unsigned long iflag;
3772a991 2820 uint32_t status;
dea3101e 2821
34b02dcd 2822 pgp = &phba->port_gp[pring->ringno];
2e0fef85 2823 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2824 pring->stats.iocb_event++;
2825
dea3101e 2826 /*
2827 * The next available response entry should never exceed the maximum
2828 * entries. If it does, treat it as an adapter hardware error.
2829 */
2830 portRspMax = pring->numRiocb;
2831 portRspPut = le32_to_cpu(pgp->rspPutInx);
2832 if (portRspPut >= portRspMax) {
2833 /*
025dfdaf 2834 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 2835 * rsp ring <portRspMax>
2836 */
ed957684 2837 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2838 "0303 Ring %d handler: portRspPut %d "
025dfdaf 2839 "is bigger than rsp ring %d\n",
e8b62011 2840 pring->ringno, portRspPut, portRspMax);
dea3101e 2841
2e0fef85
JS
2842 phba->link_state = LPFC_HBA_ERROR;
2843 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2844
2845 phba->work_hs = HS_FFER3;
2846 lpfc_handle_eratt(phba);
2847
3772a991 2848 return;
dea3101e 2849 }
2850
2851 rmb();
dea3101e 2852 while (pring->rspidx != portRspPut) {
2853 /*
2854 * Build a completion list and call the appropriate handler.
2855 * The process is to get the next available response iocb, get
2856 * a free iocb from the list, copy the response data into the
2857 * free iocb, insert to the continuation list, and update the
2858 * next response index to slim. This process makes response
2859 * iocb's in the ring available to DMA as fast as possible but
2860 * pays a penalty for a copy operation. Since the iocb is
2861 * only 32 bytes, this penalty is considered small relative to
2862 * the PCI reads for register values and a slim write. When
2863 * the ulpLe field is set, the entire Command has been
2864 * received.
2865 */
ed957684
JS
2866 entry = lpfc_resp_iocb(phba, pring);
2867
858c9f6c 2868 phba->last_completion_time = jiffies;
2e0fef85 2869 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 2870 if (rspiocbp == NULL) {
2871 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 2872 "completion.\n", __func__);
dea3101e 2873 break;
2874 }
2875
ed957684
JS
2876 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2877 phba->iocb_rsp_size);
dea3101e 2878 irsp = &rspiocbp->iocb;
2879
2880 if (++pring->rspidx >= portRspMax)
2881 pring->rspidx = 0;
2882
a58cbd52
JS
2883 if (pring->ringno == LPFC_ELS_RING) {
2884 lpfc_debugfs_slow_ring_trc(phba,
2885 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2886 *(((uint32_t *) irsp) + 4),
2887 *(((uint32_t *) irsp) + 6),
2888 *(((uint32_t *) irsp) + 7));
2889 }
2890
ed957684 2891 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2892
3772a991
JS
2893 spin_unlock_irqrestore(&phba->hbalock, iflag);
2894 /* Handle the response IOCB */
2895 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2896 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2897
2898 /*
2899 * If the port response put pointer has not been updated, sync
2900 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2901 * response put pointer.
2902 */
2903 if (pring->rspidx == portRspPut) {
2904 portRspPut = le32_to_cpu(pgp->rspPutInx);
2905 }
2906 } /* while (pring->rspidx != portRspPut) */
2907
92d7f7b0 2908 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 2909 /* At least one response entry has been freed */
2910 pring->stats.iocb_rsp_full++;
2911 /* SET RxRE_RSP in Chip Att register */
2912 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2913 writel(status, phba->CAregaddr);
2914 readl(phba->CAregaddr); /* flush */
2915 }
2916 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2917 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2918 pring->stats.iocb_cmd_empty++;
2919
2920 /* Force update of the local copy of cmdGetInx */
2921 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2922 lpfc_sli_resume_iocb(phba, pring);
2923
2924 if ((pring->lpfc_sli_cmd_available))
2925 (pring->lpfc_sli_cmd_available) (phba, pring);
2926
2927 }
2928
2e0fef85 2929 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2930 return;
dea3101e 2931}
2932
4f774513
JS
2933/**
2934 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
2935 * @phba: Pointer to HBA context object.
2936 * @pring: Pointer to driver SLI ring object.
2937 * @mask: Host attention register mask for this ring.
2938 *
2939 * This function is called from the worker thread when there is a pending
2940 * ELS response iocb on the driver internal slow-path response iocb worker
2941 * queue. The caller does not hold any lock. The function will remove each
2942 * response iocb from the response worker queue and calls the handle
2943 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2944 **/
2945static void
2946lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
2947 struct lpfc_sli_ring *pring, uint32_t mask)
2948{
2949 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
2950 struct hbq_dmabuf *dmabuf;
2951 struct lpfc_cq_event *cq_event;
4f774513
JS
2952 unsigned long iflag;
2953
45ed1190
JS
2954 spin_lock_irqsave(&phba->hbalock, iflag);
2955 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
2956 spin_unlock_irqrestore(&phba->hbalock, iflag);
2957 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
4f774513
JS
2958 /* Get the response iocb from the head of work queue */
2959 spin_lock_irqsave(&phba->hbalock, iflag);
45ed1190 2960 list_remove_head(&phba->sli4_hba.sp_queue_event,
4d9ab994 2961 cq_event, struct lpfc_cq_event, list);
4f774513 2962 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
2963
2964 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
2965 case CQE_CODE_COMPL_WQE:
2966 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
2967 cq_event);
45ed1190
JS
2968 /* Translate ELS WCQE to response IOCBQ */
2969 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
2970 irspiocbq);
2971 if (irspiocbq)
2972 lpfc_sli_sp_handle_rspiocb(phba, pring,
2973 irspiocbq);
4d9ab994
JS
2974 break;
2975 case CQE_CODE_RECEIVE:
2976 dmabuf = container_of(cq_event, struct hbq_dmabuf,
2977 cq_event);
2978 lpfc_sli4_handle_received_buffer(phba, dmabuf);
2979 break;
2980 default:
2981 break;
2982 }
4f774513
JS
2983 }
2984}
2985
e59058c4 2986/**
3621a710 2987 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
2988 * @phba: Pointer to HBA context object.
2989 * @pring: Pointer to driver SLI ring object.
2990 *
2991 * This function aborts all iocbs in the given ring and frees all the iocb
2992 * objects in txq. This function issues an abort iocb for all the iocb commands
2993 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
2994 * the return of this function. The caller is not required to hold any locks.
2995 **/
2e0fef85 2996void
dea3101e 2997lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2998{
2534ba75 2999 LIST_HEAD(completions);
dea3101e 3000 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3001
92d7f7b0
JS
3002 if (pring->ringno == LPFC_ELS_RING) {
3003 lpfc_fabric_abort_hba(phba);
3004 }
3005
dea3101e 3006 /* Error everything on txq and txcmplq
3007 * First do the txq.
3008 */
2e0fef85 3009 spin_lock_irq(&phba->hbalock);
2534ba75 3010 list_splice_init(&pring->txq, &completions);
dea3101e 3011 pring->txq_cnt = 0;
dea3101e 3012
3013 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3014 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3015 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3016
2e0fef85 3017 spin_unlock_irq(&phba->hbalock);
dea3101e 3018
a257bf90
JS
3019 /* Cancel all the IOCBs from the completions list */
3020 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3021 IOERR_SLI_ABORTED);
dea3101e 3022}
3023
a8e497d5 3024/**
3621a710 3025 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3026 * @phba: Pointer to HBA context object.
3027 *
3028 * This function flushes all iocbs in the fcp ring and frees all the iocb
3029 * objects in txq and txcmplq. This function will not issue abort iocbs
3030 * for all the iocb commands in txcmplq, they will just be returned with
3031 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3032 * slot has been permanently disabled.
3033 **/
3034void
3035lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3036{
3037 LIST_HEAD(txq);
3038 LIST_HEAD(txcmplq);
a8e497d5
JS
3039 struct lpfc_sli *psli = &phba->sli;
3040 struct lpfc_sli_ring *pring;
3041
3042 /* Currently, only one fcp ring */
3043 pring = &psli->ring[psli->fcp_ring];
3044
3045 spin_lock_irq(&phba->hbalock);
3046 /* Retrieve everything on txq */
3047 list_splice_init(&pring->txq, &txq);
3048 pring->txq_cnt = 0;
3049
3050 /* Retrieve everything on the txcmplq */
3051 list_splice_init(&pring->txcmplq, &txcmplq);
3052 pring->txcmplq_cnt = 0;
3053 spin_unlock_irq(&phba->hbalock);
3054
3055 /* Flush the txq */
a257bf90
JS
3056 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3057 IOERR_SLI_DOWN);
a8e497d5
JS
3058
3059 /* Flush the txcmpq */
a257bf90
JS
3060 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3061 IOERR_SLI_DOWN);
a8e497d5
JS
3062}
3063
e59058c4 3064/**
3772a991 3065 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3066 * @phba: Pointer to HBA context object.
3067 * @mask: Bit mask to be checked.
3068 *
3069 * This function reads the host status register and compares
3070 * with the provided bit mask to check if HBA completed
3071 * the restart. This function will wait in a loop for the
3072 * HBA to complete restart. If the HBA does not restart within
3073 * 15 iterations, the function will reset the HBA again. The
3074 * function returns 1 when HBA fail to restart otherwise returns
3075 * zero.
3076 **/
3772a991
JS
3077static int
3078lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3079{
41415862
JW
3080 uint32_t status;
3081 int i = 0;
3082 int retval = 0;
dea3101e 3083
41415862
JW
3084 /* Read the HBA Host Status Register */
3085 status = readl(phba->HSregaddr);
dea3101e 3086
41415862
JW
3087 /*
3088 * Check status register every 100ms for 5 retries, then every
3089 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3090 * every 2.5 sec for 4.
3091 * Break our of the loop if errors occurred during init.
3092 */
3093 while (((status & mask) != mask) &&
3094 !(status & HS_FFERM) &&
3095 i++ < 20) {
dea3101e 3096
41415862
JW
3097 if (i <= 5)
3098 msleep(10);
3099 else if (i <= 10)
3100 msleep(500);
3101 else
3102 msleep(2500);
dea3101e 3103
41415862 3104 if (i == 15) {
2e0fef85 3105 /* Do post */
92d7f7b0 3106 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3107 lpfc_sli_brdrestart(phba);
3108 }
3109 /* Read the HBA Host Status Register */
3110 status = readl(phba->HSregaddr);
3111 }
dea3101e 3112
41415862
JW
3113 /* Check to see if any errors occurred during init */
3114 if ((status & HS_FFERM) || (i >= 20)) {
e40a02c1
JS
3115 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3116 "2751 Adapter failed to restart, "
3117 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3118 status,
3119 readl(phba->MBslimaddr + 0xa8),
3120 readl(phba->MBslimaddr + 0xac));
2e0fef85 3121 phba->link_state = LPFC_HBA_ERROR;
41415862 3122 retval = 1;
dea3101e 3123 }
dea3101e 3124
41415862
JW
3125 return retval;
3126}
dea3101e 3127
da0436e9
JS
3128/**
3129 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3130 * @phba: Pointer to HBA context object.
3131 * @mask: Bit mask to be checked.
3132 *
3133 * This function checks the host status register to check if HBA is
3134 * ready. This function will wait in a loop for the HBA to be ready
3135 * If the HBA is not ready , the function will will reset the HBA PCI
3136 * function again. The function returns 1 when HBA fail to be ready
3137 * otherwise returns zero.
3138 **/
3139static int
3140lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3141{
3142 uint32_t status;
3143 int retval = 0;
3144
3145 /* Read the HBA Host Status Register */
3146 status = lpfc_sli4_post_status_check(phba);
3147
3148 if (status) {
3149 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3150 lpfc_sli_brdrestart(phba);
3151 status = lpfc_sli4_post_status_check(phba);
3152 }
3153
3154 /* Check to see if any errors occurred during init */
3155 if (status) {
3156 phba->link_state = LPFC_HBA_ERROR;
3157 retval = 1;
3158 } else
3159 phba->sli4_hba.intr_enable = 0;
3160
3161 return retval;
3162}
3163
3164/**
3165 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3166 * @phba: Pointer to HBA context object.
3167 * @mask: Bit mask to be checked.
3168 *
3169 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3170 * from the API jump table function pointer from the lpfc_hba struct.
3171 **/
3172int
3173lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3174{
3175 return phba->lpfc_sli_brdready(phba, mask);
3176}
3177
9290831f
JS
3178#define BARRIER_TEST_PATTERN (0xdeadbeef)
3179
e59058c4 3180/**
3621a710 3181 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3182 * @phba: Pointer to HBA context object.
3183 *
3184 * This function is called before resetting an HBA. This
3185 * function requests HBA to quiesce DMAs before a reset.
3186 **/
2e0fef85 3187void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3188{
65a29c16
JS
3189 uint32_t __iomem *resp_buf;
3190 uint32_t __iomem *mbox_buf;
9290831f
JS
3191 volatile uint32_t mbox;
3192 uint32_t hc_copy;
3193 int i;
3194 uint8_t hdrtype;
3195
3196 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3197 if (hdrtype != 0x80 ||
3198 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3199 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3200 return;
3201
3202 /*
3203 * Tell the other part of the chip to suspend temporarily all
3204 * its DMA activity.
3205 */
65a29c16 3206 resp_buf = phba->MBslimaddr;
9290831f
JS
3207
3208 /* Disable the error attention */
3209 hc_copy = readl(phba->HCregaddr);
3210 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3211 readl(phba->HCregaddr); /* flush */
2e0fef85 3212 phba->link_flag |= LS_IGNORE_ERATT;
9290831f
JS
3213
3214 if (readl(phba->HAregaddr) & HA_ERATT) {
3215 /* Clear Chip error bit */
3216 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3217 phba->pport->stopped = 1;
9290831f
JS
3218 }
3219
3220 mbox = 0;
3221 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3222 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3223
3224 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3225 mbox_buf = phba->MBslimaddr;
9290831f
JS
3226 writel(mbox, mbox_buf);
3227
3228 for (i = 0;
3229 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3230 mdelay(1);
3231
3232 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3233 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3234 phba->pport->stopped)
9290831f
JS
3235 goto restore_hc;
3236 else
3237 goto clear_errat;
3238 }
3239
3240 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3241 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3242 mdelay(1);
3243
3244clear_errat:
3245
3246 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3247 mdelay(1);
3248
3249 if (readl(phba->HAregaddr) & HA_ERATT) {
3250 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3251 phba->pport->stopped = 1;
9290831f
JS
3252 }
3253
3254restore_hc:
2e0fef85 3255 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3256 writel(hc_copy, phba->HCregaddr);
3257 readl(phba->HCregaddr); /* flush */
3258}
3259
e59058c4 3260/**
3621a710 3261 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3262 * @phba: Pointer to HBA context object.
3263 *
3264 * This function issues a kill_board mailbox command and waits for
3265 * the error attention interrupt. This function is called for stopping
3266 * the firmware processing. The caller is not required to hold any
3267 * locks. This function calls lpfc_hba_down_post function to free
3268 * any pending commands after the kill. The function will return 1 when it
3269 * fails to kill the board else will return 0.
3270 **/
41415862 3271int
2e0fef85 3272lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3273{
3274 struct lpfc_sli *psli;
3275 LPFC_MBOXQ_t *pmb;
3276 uint32_t status;
3277 uint32_t ha_copy;
3278 int retval;
3279 int i = 0;
dea3101e 3280
41415862 3281 psli = &phba->sli;
dea3101e 3282
41415862 3283 /* Kill HBA */
ed957684 3284 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3285 "0329 Kill HBA Data: x%x x%x\n",
3286 phba->pport->port_state, psli->sli_flag);
41415862 3287
98c9ea5c
JS
3288 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3289 if (!pmb)
41415862 3290 return 1;
41415862
JW
3291
3292 /* Disable the error attention */
2e0fef85 3293 spin_lock_irq(&phba->hbalock);
41415862
JW
3294 status = readl(phba->HCregaddr);
3295 status &= ~HC_ERINT_ENA;
3296 writel(status, phba->HCregaddr);
3297 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3298 phba->link_flag |= LS_IGNORE_ERATT;
3299 spin_unlock_irq(&phba->hbalock);
41415862
JW
3300
3301 lpfc_kill_board(phba, pmb);
3302 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3303 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3304
3305 if (retval != MBX_SUCCESS) {
3306 if (retval != MBX_BUSY)
3307 mempool_free(pmb, phba->mbox_mem_pool);
e40a02c1
JS
3308 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3309 "2752 KILL_BOARD command failed retval %d\n",
3310 retval);
2e0fef85
JS
3311 spin_lock_irq(&phba->hbalock);
3312 phba->link_flag &= ~LS_IGNORE_ERATT;
3313 spin_unlock_irq(&phba->hbalock);
41415862
JW
3314 return 1;
3315 }
3316
f4b4c68f
JS
3317 spin_lock_irq(&phba->hbalock);
3318 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3319 spin_unlock_irq(&phba->hbalock);
9290831f 3320
41415862
JW
3321 mempool_free(pmb, phba->mbox_mem_pool);
3322
3323 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3324 * attention every 100ms for 3 seconds. If we don't get ERATT after
3325 * 3 seconds we still set HBA_ERROR state because the status of the
3326 * board is now undefined.
3327 */
3328 ha_copy = readl(phba->HAregaddr);
3329
3330 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3331 mdelay(100);
3332 ha_copy = readl(phba->HAregaddr);
3333 }
3334
3335 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3336 if (ha_copy & HA_ERATT) {
3337 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3338 phba->pport->stopped = 1;
9290831f 3339 }
2e0fef85 3340 spin_lock_irq(&phba->hbalock);
41415862 3341 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3342 psli->mbox_active = NULL;
2e0fef85
JS
3343 phba->link_flag &= ~LS_IGNORE_ERATT;
3344 spin_unlock_irq(&phba->hbalock);
41415862 3345
41415862 3346 lpfc_hba_down_post(phba);
2e0fef85 3347 phba->link_state = LPFC_HBA_ERROR;
41415862 3348
2e0fef85 3349 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 3350}
3351
e59058c4 3352/**
3772a991 3353 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3354 * @phba: Pointer to HBA context object.
3355 *
3356 * This function resets the HBA by writing HC_INITFF to the control
3357 * register. After the HBA resets, this function resets all the iocb ring
3358 * indices. This function disables PCI layer parity checking during
3359 * the reset.
3360 * This function returns 0 always.
3361 * The caller is not required to hold any locks.
3362 **/
41415862 3363int
2e0fef85 3364lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3365{
41415862 3366 struct lpfc_sli *psli;
dea3101e 3367 struct lpfc_sli_ring *pring;
41415862 3368 uint16_t cfg_value;
dea3101e 3369 int i;
dea3101e 3370
41415862 3371 psli = &phba->sli;
dea3101e 3372
41415862
JW
3373 /* Reset HBA */
3374 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3375 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3376 phba->pport->port_state, psli->sli_flag);
dea3101e 3377
3378 /* perform board reset */
3379 phba->fc_eventTag = 0;
4d9ab994 3380 phba->link_events = 0;
2e0fef85
JS
3381 phba->pport->fc_myDID = 0;
3382 phba->pport->fc_prevDID = 0;
dea3101e 3383
41415862
JW
3384 /* Turn off parity checking and serr during the physical reset */
3385 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3386 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3387 (cfg_value &
3388 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3389
3772a991
JS
3390 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3391
41415862
JW
3392 /* Now toggle INITFF bit in the Host Control Register */
3393 writel(HC_INITFF, phba->HCregaddr);
3394 mdelay(1);
3395 readl(phba->HCregaddr); /* flush */
3396 writel(0, phba->HCregaddr);
3397 readl(phba->HCregaddr); /* flush */
3398
3399 /* Restore PCI cmd register */
3400 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 3401
3402 /* Initialize relevant SLI info */
41415862
JW
3403 for (i = 0; i < psli->num_rings; i++) {
3404 pring = &psli->ring[i];
dea3101e 3405 pring->flag = 0;
3406 pring->rspidx = 0;
3407 pring->next_cmdidx = 0;
3408 pring->local_getidx = 0;
3409 pring->cmdidx = 0;
3410 pring->missbufcnt = 0;
3411 }
dea3101e 3412
2e0fef85 3413 phba->link_state = LPFC_WARM_START;
41415862
JW
3414 return 0;
3415}
3416
e59058c4 3417/**
da0436e9
JS
3418 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3419 * @phba: Pointer to HBA context object.
3420 *
3421 * This function resets a SLI4 HBA. This function disables PCI layer parity
3422 * checking during resets the device. The caller is not required to hold
3423 * any locks.
3424 *
3425 * This function returns 0 always.
3426 **/
3427int
3428lpfc_sli4_brdreset(struct lpfc_hba *phba)
3429{
3430 struct lpfc_sli *psli = &phba->sli;
3431 uint16_t cfg_value;
3432 uint8_t qindx;
3433
3434 /* Reset HBA */
3435 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3436 "0295 Reset HBA Data: x%x x%x\n",
3437 phba->pport->port_state, psli->sli_flag);
3438
3439 /* perform board reset */
3440 phba->fc_eventTag = 0;
4d9ab994 3441 phba->link_events = 0;
da0436e9
JS
3442 phba->pport->fc_myDID = 0;
3443 phba->pport->fc_prevDID = 0;
3444
3445 /* Turn off parity checking and serr during the physical reset */
3446 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3447 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3448 (cfg_value &
3449 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3450
3451 spin_lock_irq(&phba->hbalock);
3452 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3453 phba->fcf.fcf_flag = 0;
3454 /* Clean up the child queue list for the CQs */
3455 list_del_init(&phba->sli4_hba.mbx_wq->list);
3456 list_del_init(&phba->sli4_hba.els_wq->list);
3457 list_del_init(&phba->sli4_hba.hdr_rq->list);
3458 list_del_init(&phba->sli4_hba.dat_rq->list);
3459 list_del_init(&phba->sli4_hba.mbx_cq->list);
3460 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3461 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3462 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3463 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3464 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3465 spin_unlock_irq(&phba->hbalock);
3466
3467 /* Now physically reset the device */
3468 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3469 "0389 Performing PCI function reset!\n");
3470 /* Perform FCoE PCI function reset */
3471 lpfc_pci_function_reset(phba);
3472
3473 return 0;
3474}
3475
3476/**
3477 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3478 * @phba: Pointer to HBA context object.
3479 *
3480 * This function is called in the SLI initialization code path to
3481 * restart the HBA. The caller is not required to hold any lock.
3482 * This function writes MBX_RESTART mailbox command to the SLIM and
3483 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3484 * function to free any pending commands. The function enables
3485 * POST only during the first initialization. The function returns zero.
3486 * The function does not guarantee completion of MBX_RESTART mailbox
3487 * command before the return of this function.
3488 **/
da0436e9
JS
3489static int
3490lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3491{
3492 MAILBOX_t *mb;
3493 struct lpfc_sli *psli;
41415862
JW
3494 volatile uint32_t word0;
3495 void __iomem *to_slim;
0d878419 3496 uint32_t hba_aer_enabled;
41415862 3497
2e0fef85 3498 spin_lock_irq(&phba->hbalock);
41415862 3499
0d878419
JS
3500 /* Take PCIe device Advanced Error Reporting (AER) state */
3501 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3502
41415862
JW
3503 psli = &phba->sli;
3504
3505 /* Restart HBA */
3506 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3507 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3508 phba->pport->port_state, psli->sli_flag);
41415862
JW
3509
3510 word0 = 0;
3511 mb = (MAILBOX_t *) &word0;
3512 mb->mbxCommand = MBX_RESTART;
3513 mb->mbxHc = 1;
3514
9290831f
JS
3515 lpfc_reset_barrier(phba);
3516
41415862
JW
3517 to_slim = phba->MBslimaddr;
3518 writel(*(uint32_t *) mb, to_slim);
3519 readl(to_slim); /* flush */
3520
3521 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3522 if (phba->pport->port_state)
41415862 3523 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3524 else
41415862 3525 word0 = 0; /* This is really setting up word1 */
65a29c16 3526 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3527 writel(*(uint32_t *) mb, to_slim);
3528 readl(to_slim); /* flush */
dea3101e 3529
41415862 3530 lpfc_sli_brdreset(phba);
2e0fef85
JS
3531 phba->pport->stopped = 0;
3532 phba->link_state = LPFC_INIT_START;
da0436e9 3533 phba->hba_flag = 0;
2e0fef85 3534 spin_unlock_irq(&phba->hbalock);
41415862 3535
64ba8818
JS
3536 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3537 psli->stats_start = get_seconds();
3538
eaf15d5b
JS
3539 /* Give the INITFF and Post time to settle. */
3540 mdelay(100);
41415862 3541
0d878419
JS
3542 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3543 if (hba_aer_enabled)
3544 pci_disable_pcie_error_reporting(phba->pcidev);
3545
41415862 3546 lpfc_hba_down_post(phba);
dea3101e 3547
3548 return 0;
3549}
3550
da0436e9
JS
3551/**
3552 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3553 * @phba: Pointer to HBA context object.
3554 *
3555 * This function is called in the SLI initialization code path to restart
3556 * a SLI4 HBA. The caller is not required to hold any lock.
3557 * At the end of the function, it calls lpfc_hba_down_post function to
3558 * free any pending commands.
3559 **/
3560static int
3561lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3562{
3563 struct lpfc_sli *psli = &phba->sli;
3564
3565
3566 /* Restart HBA */
3567 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3568 "0296 Restart HBA Data: x%x x%x\n",
3569 phba->pport->port_state, psli->sli_flag);
3570
3571 lpfc_sli4_brdreset(phba);
3572
3573 spin_lock_irq(&phba->hbalock);
3574 phba->pport->stopped = 0;
3575 phba->link_state = LPFC_INIT_START;
3576 phba->hba_flag = 0;
3577 spin_unlock_irq(&phba->hbalock);
3578
3579 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3580 psli->stats_start = get_seconds();
3581
3582 lpfc_hba_down_post(phba);
3583
3584 return 0;
3585}
3586
3587/**
3588 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3589 * @phba: Pointer to HBA context object.
3590 *
3591 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3592 * API jump table function pointer from the lpfc_hba struct.
3593**/
3594int
3595lpfc_sli_brdrestart(struct lpfc_hba *phba)
3596{
3597 return phba->lpfc_sli_brdrestart(phba);
3598}
3599
e59058c4 3600/**
3621a710 3601 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
3602 * @phba: Pointer to HBA context object.
3603 *
3604 * This function is called after a HBA restart to wait for successful
3605 * restart of the HBA. Successful restart of the HBA is indicated by
3606 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3607 * iteration, the function will restart the HBA again. The function returns
3608 * zero if HBA successfully restarted else returns negative error code.
3609 **/
dea3101e 3610static int
3611lpfc_sli_chipset_init(struct lpfc_hba *phba)
3612{
3613 uint32_t status, i = 0;
3614
3615 /* Read the HBA Host Status Register */
3616 status = readl(phba->HSregaddr);
3617
3618 /* Check status register to see what current state is */
3619 i = 0;
3620 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3621
3622 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3623 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3624 * 4.
3625 */
3626 if (i++ >= 20) {
3627 /* Adapter failed to init, timeout, status reg
3628 <status> */
ed957684 3629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3630 "0436 Adapter failed to init, "
09372820
JS
3631 "timeout, status reg x%x, "
3632 "FW Data: A8 x%x AC x%x\n", status,
3633 readl(phba->MBslimaddr + 0xa8),
3634 readl(phba->MBslimaddr + 0xac));
2e0fef85 3635 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3636 return -ETIMEDOUT;
3637 }
3638
3639 /* Check to see if any errors occurred during init */
3640 if (status & HS_FFERM) {
3641 /* ERROR: During chipset initialization */
3642 /* Adapter failed to init, chipset, status reg
3643 <status> */
ed957684 3644 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3645 "0437 Adapter failed to init, "
09372820
JS
3646 "chipset, status reg x%x, "
3647 "FW Data: A8 x%x AC x%x\n", status,
3648 readl(phba->MBslimaddr + 0xa8),
3649 readl(phba->MBslimaddr + 0xac));
2e0fef85 3650 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3651 return -EIO;
3652 }
3653
3654 if (i <= 5) {
3655 msleep(10);
3656 } else if (i <= 10) {
3657 msleep(500);
3658 } else {
3659 msleep(2500);
3660 }
3661
3662 if (i == 15) {
2e0fef85 3663 /* Do post */
92d7f7b0 3664 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3665 lpfc_sli_brdrestart(phba);
dea3101e 3666 }
3667 /* Read the HBA Host Status Register */
3668 status = readl(phba->HSregaddr);
3669 }
3670
3671 /* Check to see if any errors occurred during init */
3672 if (status & HS_FFERM) {
3673 /* ERROR: During chipset initialization */
3674 /* Adapter failed to init, chipset, status reg <status> */
ed957684 3675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3676 "0438 Adapter failed to init, chipset, "
09372820
JS
3677 "status reg x%x, "
3678 "FW Data: A8 x%x AC x%x\n", status,
3679 readl(phba->MBslimaddr + 0xa8),
3680 readl(phba->MBslimaddr + 0xac));
2e0fef85 3681 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3682 return -EIO;
3683 }
3684
3685 /* Clear all interrupt enable conditions */
3686 writel(0, phba->HCregaddr);
3687 readl(phba->HCregaddr); /* flush */
3688
3689 /* setup host attn register */
3690 writel(0xffffffff, phba->HAregaddr);
3691 readl(phba->HAregaddr); /* flush */
3692 return 0;
3693}
3694
e59058c4 3695/**
3621a710 3696 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
3697 *
3698 * This function calculates and returns the number of HBQs required to be
3699 * configured.
3700 **/
78b2d852 3701int
ed957684
JS
3702lpfc_sli_hbq_count(void)
3703{
92d7f7b0 3704 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
3705}
3706
e59058c4 3707/**
3621a710 3708 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
3709 *
3710 * This function adds the number of hbq entries in every HBQ to get
3711 * the total number of hbq entries required for the HBA and returns
3712 * the total count.
3713 **/
ed957684
JS
3714static int
3715lpfc_sli_hbq_entry_count(void)
3716{
3717 int hbq_count = lpfc_sli_hbq_count();
3718 int count = 0;
3719 int i;
3720
3721 for (i = 0; i < hbq_count; ++i)
92d7f7b0 3722 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
3723 return count;
3724}
3725
e59058c4 3726/**
3621a710 3727 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
3728 *
3729 * This function calculates amount of memory required for all hbq entries
3730 * to be configured and returns the total memory required.
3731 **/
dea3101e 3732int
ed957684
JS
3733lpfc_sli_hbq_size(void)
3734{
3735 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3736}
3737
e59058c4 3738/**
3621a710 3739 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
3740 * @phba: Pointer to HBA context object.
3741 *
3742 * This function is called during the SLI initialization to configure
3743 * all the HBQs and post buffers to the HBQ. The caller is not
3744 * required to hold any locks. This function will return zero if successful
3745 * else it will return negative error code.
3746 **/
ed957684
JS
3747static int
3748lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3749{
3750 int hbq_count = lpfc_sli_hbq_count();
3751 LPFC_MBOXQ_t *pmb;
3752 MAILBOX_t *pmbox;
3753 uint32_t hbqno;
3754 uint32_t hbq_entry_index;
ed957684 3755
92d7f7b0
JS
3756 /* Get a Mailbox buffer to setup mailbox
3757 * commands for HBA initialization
3758 */
ed957684
JS
3759 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3760
3761 if (!pmb)
3762 return -ENOMEM;
3763
04c68496 3764 pmbox = &pmb->u.mb;
ed957684
JS
3765
3766 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3767 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 3768 phba->hbq_in_use = 1;
ed957684
JS
3769
3770 hbq_entry_index = 0;
3771 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3772 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3773 phba->hbqs[hbqno].hbqPutIdx = 0;
3774 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3775 phba->hbqs[hbqno].entry_count =
92d7f7b0 3776 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
3777 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3778 hbq_entry_index, pmb);
ed957684
JS
3779 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3780
3781 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3782 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3783 mbxStatus <status>, ring <num> */
3784
3785 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 3786 LOG_SLI | LOG_VPORT,
e8b62011 3787 "1805 Adapter failed to init. "
ed957684 3788 "Data: x%x x%x x%x\n",
e8b62011 3789 pmbox->mbxCommand,
ed957684
JS
3790 pmbox->mbxStatus, hbqno);
3791
3792 phba->link_state = LPFC_HBA_ERROR;
3793 mempool_free(pmb, phba->mbox_mem_pool);
ed957684
JS
3794 return ENXIO;
3795 }
3796 }
3797 phba->hbq_count = hbq_count;
3798
ed957684
JS
3799 mempool_free(pmb, phba->mbox_mem_pool);
3800
92d7f7b0 3801 /* Initially populate or replenish the HBQs */
d7c255b2
JS
3802 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3803 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
3804 return 0;
3805}
3806
4f774513
JS
3807/**
3808 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3809 * @phba: Pointer to HBA context object.
3810 *
3811 * This function is called during the SLI initialization to configure
3812 * all the HBQs and post buffers to the HBQ. The caller is not
3813 * required to hold any locks. This function will return zero if successful
3814 * else it will return negative error code.
3815 **/
3816static int
3817lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3818{
3819 phba->hbq_in_use = 1;
3820 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3821 phba->hbq_count = 1;
3822 /* Initially populate or replenish the HBQs */
3823 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3824 return 0;
3825}
3826
e59058c4 3827/**
3621a710 3828 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
3829 * @phba: Pointer to HBA context object.
3830 * @sli_mode: sli mode - 2/3
3831 *
3832 * This function is called by the sli intialization code path
3833 * to issue config_port mailbox command. This function restarts the
3834 * HBA firmware and issues a config_port mailbox command to configure
3835 * the SLI interface in the sli mode specified by sli_mode
3836 * variable. The caller is not required to hold any locks.
3837 * The function returns 0 if successful, else returns negative error
3838 * code.
3839 **/
9399627f
JS
3840int
3841lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 3842{
3843 LPFC_MBOXQ_t *pmb;
3844 uint32_t resetcount = 0, rc = 0, done = 0;
3845
3846 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3847 if (!pmb) {
2e0fef85 3848 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3849 return -ENOMEM;
3850 }
3851
ed957684 3852 phba->sli_rev = sli_mode;
dea3101e 3853 while (resetcount < 2 && !done) {
2e0fef85 3854 spin_lock_irq(&phba->hbalock);
1c067a42 3855 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3856 spin_unlock_irq(&phba->hbalock);
92d7f7b0 3857 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3858 lpfc_sli_brdrestart(phba);
dea3101e 3859 rc = lpfc_sli_chipset_init(phba);
3860 if (rc)
3861 break;
3862
2e0fef85 3863 spin_lock_irq(&phba->hbalock);
1c067a42 3864 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3865 spin_unlock_irq(&phba->hbalock);
dea3101e 3866 resetcount++;
3867
ed957684
JS
3868 /* Call pre CONFIG_PORT mailbox command initialization. A
3869 * value of 0 means the call was successful. Any other
3870 * nonzero value is a failure, but if ERESTART is returned,
3871 * the driver may reset the HBA and try again.
3872 */
dea3101e 3873 rc = lpfc_config_port_prep(phba);
3874 if (rc == -ERESTART) {
ed957684 3875 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 3876 continue;
34b02dcd 3877 } else if (rc)
dea3101e 3878 break;
2e0fef85 3879 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 3880 lpfc_config_port(phba, pmb);
3881 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
3882 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3883 LPFC_SLI3_HBQ_ENABLED |
3884 LPFC_SLI3_CRP_ENABLED |
e2a0a9d6
JS
3885 LPFC_SLI3_INB_ENABLED |
3886 LPFC_SLI3_BG_ENABLED);
ed957684 3887 if (rc != MBX_SUCCESS) {
dea3101e 3888 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3889 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 3890 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 3891 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 3892 spin_lock_irq(&phba->hbalock);
04c68496 3893 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
3894 spin_unlock_irq(&phba->hbalock);
3895 rc = -ENXIO;
04c68496
JS
3896 } else {
3897 /* Allow asynchronous mailbox command to go through */
3898 spin_lock_irq(&phba->hbalock);
3899 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3900 spin_unlock_irq(&phba->hbalock);
ed957684 3901 done = 1;
04c68496 3902 }
dea3101e 3903 }
ed957684
JS
3904 if (!done) {
3905 rc = -EINVAL;
3906 goto do_prep_failed;
3907 }
04c68496
JS
3908 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3909 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
3910 rc = -ENXIO;
3911 goto do_prep_failed;
3912 }
04c68496 3913 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 3914 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
3915 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3916 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3917 phba->max_vpi : phba->max_vports;
3918
34b02dcd
JS
3919 } else
3920 phba->max_vpi = 0;
04c68496
JS
3921 if (pmb->u.mb.un.varCfgPort.gdss)
3922 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3923 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 3924 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 3925 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 3926 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
04c68496 3927 if (pmb->u.mb.un.varCfgPort.ginb) {
34b02dcd 3928 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
8f34f4ce 3929 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
34b02dcd
JS
3930 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3931 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3932 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3933 phba->inb_last_counter =
3934 phba->mbox->us.s3_inb_pgp.counter;
3935 } else {
8f34f4ce 3936 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
34b02dcd
JS
3937 phba->port_gp = phba->mbox->us.s3_pgp.port;
3938 phba->inb_ha_copy = NULL;
3939 phba->inb_counter = NULL;
3940 }
e2a0a9d6
JS
3941
3942 if (phba->cfg_enable_bg) {
04c68496 3943 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
3944 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3945 else
3946 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3947 "0443 Adapter did not grant "
3948 "BlockGuard\n");
3949 }
34b02dcd 3950 } else {
8f34f4ce 3951 phba->hbq_get = NULL;
34b02dcd
JS
3952 phba->port_gp = phba->mbox->us.s2.port;
3953 phba->inb_ha_copy = NULL;
3954 phba->inb_counter = NULL;
d7c255b2 3955 phba->max_vpi = 0;
ed957684 3956 }
92d7f7b0 3957do_prep_failed:
ed957684
JS
3958 mempool_free(pmb, phba->mbox_mem_pool);
3959 return rc;
3960}
3961
e59058c4
JS
3962
3963/**
3621a710 3964 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
3965 * @phba: Pointer to HBA context object.
3966 *
3967 * This function is the main SLI intialization function. This function
3968 * is called by the HBA intialization code, HBA reset code and HBA
3969 * error attention handler code. Caller is not required to hold any
3970 * locks. This function issues config_port mailbox command to configure
3971 * the SLI, setup iocb rings and HBQ rings. In the end the function
3972 * calls the config_port_post function to issue init_link mailbox
3973 * command and to start the discovery. The function will return zero
3974 * if successful, else it will return negative error code.
3975 **/
ed957684
JS
3976int
3977lpfc_sli_hba_setup(struct lpfc_hba *phba)
3978{
3979 uint32_t rc;
92d7f7b0 3980 int mode = 3;
ed957684
JS
3981
3982 switch (lpfc_sli_mode) {
3983 case 2:
78b2d852 3984 if (phba->cfg_enable_npiv) {
92d7f7b0 3985 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 3986 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 3987 "parameter (%d) to auto (0).\n",
e8b62011 3988 lpfc_sli_mode);
92d7f7b0
JS
3989 break;
3990 }
ed957684
JS
3991 mode = 2;
3992 break;
3993 case 0:
3994 case 3:
3995 break;
3996 default:
92d7f7b0 3997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
3998 "1819 Unrecognized lpfc_sli_mode "
3999 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4000
4001 break;
4002 }
4003
9399627f
JS
4004 rc = lpfc_sli_config_port(phba, mode);
4005
ed957684 4006 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4007 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4008 "1820 Unable to select SLI-3. "
4009 "Not supported by adapter.\n");
ed957684 4010 if (rc && mode != 2)
9399627f 4011 rc = lpfc_sli_config_port(phba, 2);
ed957684 4012 if (rc)
dea3101e 4013 goto lpfc_sli_hba_setup_error;
4014
0d878419
JS
4015 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4016 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4017 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4018 if (!rc) {
4019 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4020 "2709 This device supports "
4021 "Advanced Error Reporting (AER)\n");
4022 spin_lock_irq(&phba->hbalock);
4023 phba->hba_flag |= HBA_AER_ENABLED;
4024 spin_unlock_irq(&phba->hbalock);
4025 } else {
4026 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4027 "2708 This device does not support "
4028 "Advanced Error Reporting (AER)\n");
4029 phba->cfg_aer_support = 0;
4030 }
4031 }
4032
ed957684
JS
4033 if (phba->sli_rev == 3) {
4034 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4035 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4036 } else {
4037 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4038 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4039 phba->sli3_options = 0;
ed957684
JS
4040 }
4041
4042 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4043 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4044 phba->sli_rev, phba->max_vpi);
ed957684 4045 rc = lpfc_sli_ring_map(phba);
dea3101e 4046
4047 if (rc)
4048 goto lpfc_sli_hba_setup_error;
4049
9399627f 4050 /* Init HBQs */
ed957684
JS
4051 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4052 rc = lpfc_sli_hbq_setup(phba);
4053 if (rc)
4054 goto lpfc_sli_hba_setup_error;
4055 }
04c68496 4056 spin_lock_irq(&phba->hbalock);
dea3101e 4057 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4058 spin_unlock_irq(&phba->hbalock);
dea3101e 4059
4060 rc = lpfc_config_port_post(phba);
4061 if (rc)
4062 goto lpfc_sli_hba_setup_error;
4063
ed957684
JS
4064 return rc;
4065
92d7f7b0 4066lpfc_sli_hba_setup_error:
2e0fef85 4067 phba->link_state = LPFC_HBA_ERROR;
e40a02c1 4068 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 4069 "0445 Firmware initialization failed\n");
dea3101e 4070 return rc;
4071}
4072
e59058c4 4073/**
da0436e9
JS
4074 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4075 * @phba: Pointer to HBA context object.
4076 * @mboxq: mailbox pointer.
4077 * This function issue a dump mailbox command to read config region
4078 * 23 and parse the records in the region and populate driver
4079 * data structure.
e59058c4 4080 **/
da0436e9
JS
4081static int
4082lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4083 LPFC_MBOXQ_t *mboxq)
dea3101e 4084{
da0436e9
JS
4085 struct lpfc_dmabuf *mp;
4086 struct lpfc_mqe *mqe;
4087 uint32_t data_length;
4088 int rc;
dea3101e 4089
da0436e9
JS
4090 /* Program the default value of vlan_id and fc_map */
4091 phba->valid_vlan = 0;
4092 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4093 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4094 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4095
da0436e9
JS
4096 mqe = &mboxq->u.mqe;
4097 if (lpfc_dump_fcoe_param(phba, mboxq))
4098 return -ENOMEM;
4099
4100 mp = (struct lpfc_dmabuf *) mboxq->context1;
4101 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4102
4103 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4104 "(%d):2571 Mailbox cmd x%x Status x%x "
4105 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4106 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4107 "CQ: x%x x%x x%x x%x\n",
4108 mboxq->vport ? mboxq->vport->vpi : 0,
4109 bf_get(lpfc_mqe_command, mqe),
4110 bf_get(lpfc_mqe_status, mqe),
4111 mqe->un.mb_words[0], mqe->un.mb_words[1],
4112 mqe->un.mb_words[2], mqe->un.mb_words[3],
4113 mqe->un.mb_words[4], mqe->un.mb_words[5],
4114 mqe->un.mb_words[6], mqe->un.mb_words[7],
4115 mqe->un.mb_words[8], mqe->un.mb_words[9],
4116 mqe->un.mb_words[10], mqe->un.mb_words[11],
4117 mqe->un.mb_words[12], mqe->un.mb_words[13],
4118 mqe->un.mb_words[14], mqe->un.mb_words[15],
4119 mqe->un.mb_words[16], mqe->un.mb_words[50],
4120 mboxq->mcqe.word0,
4121 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4122 mboxq->mcqe.trailer);
4123
4124 if (rc) {
4125 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4126 kfree(mp);
4127 return -EIO;
4128 }
4129 data_length = mqe->un.mb_words[5];
a0c87cbd 4130 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4131 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4132 kfree(mp);
da0436e9 4133 return -EIO;
d11e31dd 4134 }
dea3101e 4135
da0436e9
JS
4136 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4137 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4138 kfree(mp);
4139 return 0;
4140}
e59058c4
JS
4141
4142/**
da0436e9
JS
4143 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4144 * @phba: pointer to lpfc hba data structure.
4145 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4146 * @vpd: pointer to the memory to hold resulting port vpd data.
4147 * @vpd_size: On input, the number of bytes allocated to @vpd.
4148 * On output, the number of data bytes in @vpd.
e59058c4 4149 *
da0436e9
JS
4150 * This routine executes a READ_REV SLI4 mailbox command. In
4151 * addition, this routine gets the port vpd data.
4152 *
4153 * Return codes
af901ca1 4154 * 0 - successful
da0436e9 4155 * ENOMEM - could not allocated memory.
e59058c4 4156 **/
da0436e9
JS
4157static int
4158lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4159 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4160{
da0436e9
JS
4161 int rc = 0;
4162 uint32_t dma_size;
4163 struct lpfc_dmabuf *dmabuf;
4164 struct lpfc_mqe *mqe;
dea3101e 4165
da0436e9
JS
4166 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4167 if (!dmabuf)
4168 return -ENOMEM;
4169
4170 /*
4171 * Get a DMA buffer for the vpd data resulting from the READ_REV
4172 * mailbox command.
a257bf90 4173 */
da0436e9
JS
4174 dma_size = *vpd_size;
4175 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4176 dma_size,
4177 &dmabuf->phys,
4178 GFP_KERNEL);
4179 if (!dmabuf->virt) {
4180 kfree(dmabuf);
4181 return -ENOMEM;
a257bf90 4182 }
da0436e9 4183 memset(dmabuf->virt, 0, dma_size);
a257bf90 4184
da0436e9
JS
4185 /*
4186 * The SLI4 implementation of READ_REV conflicts at word1,
4187 * bits 31:16 and SLI4 adds vpd functionality not present
4188 * in SLI3. This code corrects the conflicts.
1dcb58e5 4189 */
da0436e9
JS
4190 lpfc_read_rev(phba, mboxq);
4191 mqe = &mboxq->u.mqe;
4192 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4193 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4194 mqe->un.read_rev.word1 &= 0x0000FFFF;
4195 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4196 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4197
4198 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4199 if (rc) {
4200 dma_free_coherent(&phba->pcidev->dev, dma_size,
4201 dmabuf->virt, dmabuf->phys);
def9c7a9 4202 kfree(dmabuf);
da0436e9
JS
4203 return -EIO;
4204 }
1dcb58e5 4205
da0436e9
JS
4206 /*
4207 * The available vpd length cannot be bigger than the
4208 * DMA buffer passed to the port. Catch the less than
4209 * case and update the caller's size.
4210 */
4211 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4212 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4213
da0436e9
JS
4214 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4215 dma_free_coherent(&phba->pcidev->dev, dma_size,
4216 dmabuf->virt, dmabuf->phys);
4217 kfree(dmabuf);
4218 return 0;
dea3101e 4219}
4220
e59058c4 4221/**
da0436e9
JS
4222 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4223 * @phba: pointer to lpfc hba data structure.
e59058c4 4224 *
da0436e9
JS
4225 * This routine is called to explicitly arm the SLI4 device's completion and
4226 * event queues
4227 **/
4228static void
4229lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4230{
4231 uint8_t fcp_eqidx;
4232
4233 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4234 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
da0436e9
JS
4235 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4236 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4237 LPFC_QUEUE_REARM);
4238 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4239 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4240 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4241 LPFC_QUEUE_REARM);
4242}
4243
4244/**
4245 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4246 * @phba: Pointer to HBA context object.
4247 *
4248 * This function is the main SLI4 device intialization PCI function. This
4249 * function is called by the HBA intialization code, HBA reset code and
4250 * HBA error attention handler code. Caller is not required to hold any
4251 * locks.
4252 **/
4253int
4254lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4255{
4256 int rc;
4257 LPFC_MBOXQ_t *mboxq;
4258 struct lpfc_mqe *mqe;
4259 uint8_t *vpd;
4260 uint32_t vpd_size;
4261 uint32_t ftr_rsp = 0;
4262 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4263 struct lpfc_vport *vport = phba->pport;
4264 struct lpfc_dmabuf *mp;
4265
4266 /* Perform a PCI function reset to start from clean */
4267 rc = lpfc_pci_function_reset(phba);
4268 if (unlikely(rc))
4269 return -ENODEV;
4270
4271 /* Check the HBA Host Status Register for readyness */
4272 rc = lpfc_sli4_post_status_check(phba);
4273 if (unlikely(rc))
4274 return -ENODEV;
4275 else {
4276 spin_lock_irq(&phba->hbalock);
4277 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4278 spin_unlock_irq(&phba->hbalock);
4279 }
4280
4281 /*
4282 * Allocate a single mailbox container for initializing the
4283 * port.
4284 */
4285 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4286 if (!mboxq)
4287 return -ENOMEM;
4288
4289 /*
4290 * Continue initialization with default values even if driver failed
4291 * to read FCoE param config regions
4292 */
4293 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4294 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
e4e74273 4295 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
4296
4297 /* Issue READ_REV to collect vpd and FW information. */
4298 vpd_size = PAGE_SIZE;
4299 vpd = kzalloc(vpd_size, GFP_KERNEL);
4300 if (!vpd) {
4301 rc = -ENOMEM;
4302 goto out_free_mbox;
4303 }
4304
4305 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4306 if (unlikely(rc))
4307 goto out_free_vpd;
4308
4309 mqe = &mboxq->u.mqe;
f1126688
JS
4310 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4311 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4312 phba->hba_flag |= HBA_FCOE_SUPPORT;
45ed1190
JS
4313
4314 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
4315 LPFC_DCBX_CEE_MODE)
4316 phba->hba_flag |= HBA_FIP_SUPPORT;
4317 else
4318 phba->hba_flag &= ~HBA_FIP_SUPPORT;
4319
f1126688
JS
4320 if (phba->sli_rev != LPFC_SLI_REV4 ||
4321 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
da0436e9
JS
4322 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4323 "0376 READ_REV Error. SLI Level %d "
4324 "FCoE enabled %d\n",
f1126688 4325 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
da0436e9
JS
4326 rc = -EIO;
4327 goto out_free_vpd;
4328 }
da0436e9
JS
4329 /*
4330 * Evaluate the read rev and vpd data. Populate the driver
4331 * state with the results. If this routine fails, the failure
4332 * is not fatal as the driver will use generic values.
4333 */
4334 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4335 if (unlikely(!rc)) {
4336 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4337 "0377 Error %d parsing vpd. "
4338 "Using defaults.\n", rc);
4339 rc = 0;
4340 }
4341
f1126688
JS
4342 /* Save information as VPD data */
4343 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4344 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4345 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4346 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4347 &mqe->un.read_rev);
4348 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4349 &mqe->un.read_rev);
4350 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4351 &mqe->un.read_rev);
4352 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4353 &mqe->un.read_rev);
4354 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4355 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4356 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4357 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4358 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4359 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4360 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4361 "(%d):0380 READ_REV Status x%x "
4362 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4363 mboxq->vport ? mboxq->vport->vpi : 0,
4364 bf_get(lpfc_mqe_status, mqe),
4365 phba->vpd.rev.opFwName,
4366 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4367 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
4368
4369 /*
4370 * Discover the port's supported feature set and match it against the
4371 * hosts requests.
4372 */
4373 lpfc_request_features(phba, mboxq);
4374 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4375 if (unlikely(rc)) {
4376 rc = -EIO;
4377 goto out_free_vpd;
4378 }
4379
4380 /*
4381 * The port must support FCP initiator mode as this is the
4382 * only mode running in the host.
4383 */
4384 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4385 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4386 "0378 No support for fcpi mode.\n");
4387 ftr_rsp++;
4388 }
4389
4390 /*
4391 * If the port cannot support the host's requested features
4392 * then turn off the global config parameters to disable the
4393 * feature in the driver. This is not a fatal error.
4394 */
4395 if ((phba->cfg_enable_bg) &&
4396 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4397 ftr_rsp++;
4398
4399 if (phba->max_vpi && phba->cfg_enable_npiv &&
4400 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4401 ftr_rsp++;
4402
4403 if (ftr_rsp) {
4404 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4405 "0379 Feature Mismatch Data: x%08x %08x "
4406 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4407 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4408 phba->cfg_enable_npiv, phba->max_vpi);
4409 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4410 phba->cfg_enable_bg = 0;
4411 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4412 phba->cfg_enable_npiv = 0;
4413 }
4414
4415 /* These SLI3 features are assumed in SLI4 */
4416 spin_lock_irq(&phba->hbalock);
4417 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4418 spin_unlock_irq(&phba->hbalock);
4419
4420 /* Read the port's service parameters. */
9f1177a3
JS
4421 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
4422 if (rc) {
4423 phba->link_state = LPFC_HBA_ERROR;
4424 rc = -ENOMEM;
4425 goto out_free_vpd;
4426 }
4427
da0436e9
JS
4428 mboxq->vport = vport;
4429 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4430 mp = (struct lpfc_dmabuf *) mboxq->context1;
4431 if (rc == MBX_SUCCESS) {
4432 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4433 rc = 0;
4434 }
4435
4436 /*
4437 * This memory was allocated by the lpfc_read_sparam routine. Release
4438 * it to the mbuf pool.
4439 */
4440 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4441 kfree(mp);
4442 mboxq->context1 = NULL;
4443 if (unlikely(rc)) {
4444 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4445 "0382 READ_SPARAM command failed "
4446 "status %d, mbxStatus x%x\n",
4447 rc, bf_get(lpfc_mqe_status, mqe));
4448 phba->link_state = LPFC_HBA_ERROR;
4449 rc = -EIO;
4450 goto out_free_vpd;
4451 }
4452
4453 if (phba->cfg_soft_wwnn)
4454 u64_to_wwn(phba->cfg_soft_wwnn,
4455 vport->fc_sparam.nodeName.u.wwn);
4456 if (phba->cfg_soft_wwpn)
4457 u64_to_wwn(phba->cfg_soft_wwpn,
4458 vport->fc_sparam.portName.u.wwn);
4459 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4460 sizeof(struct lpfc_name));
4461 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4462 sizeof(struct lpfc_name));
4463
4464 /* Update the fc_host data structures with new wwn. */
4465 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4466 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4467
4468 /* Register SGL pool to the device using non-embedded mailbox command */
4469 rc = lpfc_sli4_post_sgl_list(phba);
4470 if (unlikely(rc)) {
4471 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4472 "0582 Error %d during sgl post operation\n",
4473 rc);
da0436e9
JS
4474 rc = -ENODEV;
4475 goto out_free_vpd;
4476 }
4477
4478 /* Register SCSI SGL pool to the device */
4479 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4480 if (unlikely(rc)) {
4481 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4482 "0383 Error %d during scsi sgl post "
4483 "operation\n", rc);
da0436e9
JS
4484 /* Some Scsi buffers were moved to the abort scsi list */
4485 /* A pci function reset will repost them */
4486 rc = -ENODEV;
4487 goto out_free_vpd;
4488 }
4489
4490 /* Post the rpi header region to the device. */
4491 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4492 if (unlikely(rc)) {
4493 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4494 "0393 Error %d during rpi post operation\n",
4495 rc);
4496 rc = -ENODEV;
4497 goto out_free_vpd;
4498 }
da0436e9
JS
4499
4500 /* Set up all the queues to the device */
4501 rc = lpfc_sli4_queue_setup(phba);
4502 if (unlikely(rc)) {
4503 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4504 "0381 Error %d during queue setup.\n ", rc);
4505 goto out_stop_timers;
4506 }
4507
4508 /* Arm the CQs and then EQs on device */
4509 lpfc_sli4_arm_cqeq_intr(phba);
4510
4511 /* Indicate device interrupt mode */
4512 phba->sli4_hba.intr_enable = 1;
4513
4514 /* Allow asynchronous mailbox command to go through */
4515 spin_lock_irq(&phba->hbalock);
4516 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4517 spin_unlock_irq(&phba->hbalock);
4518
4519 /* Post receive buffers to the device */
4520 lpfc_sli4_rb_setup(phba);
4521
4522 /* Start the ELS watchdog timer */
8fa38513
JS
4523 mod_timer(&vport->els_tmofunc,
4524 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
4525
4526 /* Start heart beat timer */
4527 mod_timer(&phba->hb_tmofunc,
4528 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4529 phba->hb_outstanding = 0;
4530 phba->last_completion_time = jiffies;
4531
4532 /* Start error attention (ERATT) polling timer */
4533 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4534
4535 /*
4536 * The port is ready, set the host's link state to LINK_DOWN
4537 * in preparation for link interrupts.
4538 */
4539 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4540 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4541 lpfc_set_loopback_flag(phba);
4542 /* Change driver state to LPFC_LINK_DOWN right before init link */
4543 spin_lock_irq(&phba->hbalock);
4544 phba->link_state = LPFC_LINK_DOWN;
4545 spin_unlock_irq(&phba->hbalock);
4546 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4547 if (unlikely(rc != MBX_NOT_FINISHED)) {
4548 kfree(vpd);
4549 return 0;
4550 } else
4551 rc = -EIO;
4552
4553 /* Unset all the queues set up in this routine when error out */
4554 if (rc)
4555 lpfc_sli4_queue_unset(phba);
4556
4557out_stop_timers:
4558 if (rc)
4559 lpfc_stop_hba_timers(phba);
4560out_free_vpd:
4561 kfree(vpd);
4562out_free_mbox:
4563 mempool_free(mboxq, phba->mbox_mem_pool);
4564 return rc;
4565}
4566
4567/**
4568 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4569 * @ptr: context object - pointer to hba structure.
4570 *
4571 * This is the callback function for mailbox timer. The mailbox
4572 * timer is armed when a new mailbox command is issued and the timer
4573 * is deleted when the mailbox complete. The function is called by
4574 * the kernel timer code when a mailbox does not complete within
4575 * expected time. This function wakes up the worker thread to
4576 * process the mailbox timeout and returns. All the processing is
4577 * done by the worker thread function lpfc_mbox_timeout_handler.
4578 **/
4579void
4580lpfc_mbox_timeout(unsigned long ptr)
4581{
4582 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4583 unsigned long iflag;
4584 uint32_t tmo_posted;
4585
4586 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4587 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4588 if (!tmo_posted)
4589 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4590 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4591
4592 if (!tmo_posted)
4593 lpfc_worker_wake_up(phba);
4594 return;
4595}
4596
4597
4598/**
4599 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4600 * @phba: Pointer to HBA context object.
4601 *
4602 * This function is called from worker thread when a mailbox command times out.
4603 * The caller is not required to hold any locks. This function will reset the
4604 * HBA and recover all the pending commands.
4605 **/
4606void
4607lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4608{
4609 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 4610 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
4611 struct lpfc_sli *psli = &phba->sli;
4612 struct lpfc_sli_ring *pring;
4613
4614 /* Check the pmbox pointer first. There is a race condition
4615 * between the mbox timeout handler getting executed in the
4616 * worklist and the mailbox actually completing. When this
4617 * race condition occurs, the mbox_active will be NULL.
4618 */
4619 spin_lock_irq(&phba->hbalock);
4620 if (pmbox == NULL) {
4621 lpfc_printf_log(phba, KERN_WARNING,
4622 LOG_MBOX | LOG_SLI,
4623 "0353 Active Mailbox cleared - mailbox timeout "
4624 "exiting\n");
4625 spin_unlock_irq(&phba->hbalock);
4626 return;
4627 }
4628
4629 /* Mbox cmd <mbxCommand> timeout */
4630 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4631 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4632 mb->mbxCommand,
4633 phba->pport->port_state,
4634 phba->sli.sli_flag,
4635 phba->sli.mbox_active);
4636 spin_unlock_irq(&phba->hbalock);
4637
4638 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4639 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4640 * it to fail all oustanding SCSI IO.
4641 */
4642 spin_lock_irq(&phba->pport->work_port_lock);
4643 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4644 spin_unlock_irq(&phba->pport->work_port_lock);
4645 spin_lock_irq(&phba->hbalock);
4646 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 4647 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
4648 spin_unlock_irq(&phba->hbalock);
4649
4650 pring = &psli->ring[psli->fcp_ring];
4651 lpfc_sli_abort_iocb_ring(phba, pring);
4652
4653 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4654 "0345 Resetting board due to mailbox timeout\n");
4655
4656 /* Reset the HBA device */
4657 lpfc_reset_hba(phba);
4658}
4659
4660/**
4661 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4662 * @phba: Pointer to HBA context object.
4663 * @pmbox: Pointer to mailbox object.
4664 * @flag: Flag indicating how the mailbox need to be processed.
4665 *
4666 * This function is called by discovery code and HBA management code
4667 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4668 * function gets the hbalock to protect the data structures.
4669 * The mailbox command can be submitted in polling mode, in which case
4670 * this function will wait in a polling loop for the completion of the
4671 * mailbox.
4672 * If the mailbox is submitted in no_wait mode (not polling) the
4673 * function will submit the command and returns immediately without waiting
4674 * for the mailbox completion. The no_wait is supported only when HBA
4675 * is in SLI2/SLI3 mode - interrupts are enabled.
4676 * The SLI interface allows only one mailbox pending at a time. If the
4677 * mailbox is issued in polling mode and there is already a mailbox
4678 * pending, then the function will return an error. If the mailbox is issued
4679 * in NO_WAIT mode and there is a mailbox pending already, the function
4680 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4681 * The sli layer owns the mailbox object until the completion of mailbox
4682 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4683 * return codes the caller owns the mailbox command after the return of
4684 * the function.
e59058c4 4685 **/
3772a991
JS
4686static int
4687lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4688 uint32_t flag)
dea3101e 4689{
dea3101e 4690 MAILBOX_t *mb;
2e0fef85 4691 struct lpfc_sli *psli = &phba->sli;
dea3101e 4692 uint32_t status, evtctr;
4693 uint32_t ha_copy;
4694 int i;
09372820 4695 unsigned long timeout;
dea3101e 4696 unsigned long drvr_flag = 0;
34b02dcd 4697 uint32_t word0, ldata;
dea3101e 4698 void __iomem *to_slim;
58da1ffb
JS
4699 int processing_queue = 0;
4700
4701 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4702 if (!pmbox) {
8568a4d2 4703 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 4704 /* processing mbox queue from intr_handler */
3772a991
JS
4705 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4706 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4707 return MBX_SUCCESS;
4708 }
58da1ffb 4709 processing_queue = 1;
58da1ffb
JS
4710 pmbox = lpfc_mbox_get(phba);
4711 if (!pmbox) {
4712 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4713 return MBX_SUCCESS;
4714 }
4715 }
dea3101e 4716
ed957684 4717 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 4718 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 4719 if(!pmbox->vport) {
58da1ffb 4720 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 4721 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4722 LOG_MBOX | LOG_VPORT,
e8b62011 4723 "1806 Mbox x%x failed. No vport\n",
3772a991 4724 pmbox->u.mb.mbxCommand);
ed957684 4725 dump_stack();
58da1ffb 4726 goto out_not_finished;
ed957684
JS
4727 }
4728 }
4729
8d63f375 4730 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
4731 if (unlikely(pci_channel_offline(phba->pcidev))) {
4732 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4733 goto out_not_finished;
4734 }
8d63f375 4735
a257bf90
JS
4736 /* If HBA has a deferred error attention, fail the iocb. */
4737 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4738 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4739 goto out_not_finished;
4740 }
4741
dea3101e 4742 psli = &phba->sli;
92d7f7b0 4743
3772a991 4744 mb = &pmbox->u.mb;
dea3101e 4745 status = MBX_SUCCESS;
4746
2e0fef85
JS
4747 if (phba->link_state == LPFC_HBA_ERROR) {
4748 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
4749
4750 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4751 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4752 "(%d):0311 Mailbox command x%x cannot "
4753 "issue Data: x%x x%x\n",
4754 pmbox->vport ? pmbox->vport->vpi : 0,
4755 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4756 goto out_not_finished;
41415862
JW
4757 }
4758
9290831f
JS
4759 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4760 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2e0fef85 4761 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3772a991
JS
4762 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4763 "(%d):2528 Mailbox command x%x cannot "
4764 "issue Data: x%x x%x\n",
4765 pmbox->vport ? pmbox->vport->vpi : 0,
4766 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4767 goto out_not_finished;
9290831f
JS
4768 }
4769
dea3101e 4770 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4771 /* Polling for a mbox command when another one is already active
4772 * is not allowed in SLI. Also, the driver must have established
4773 * SLI2 mode to queue and process multiple mbox commands.
4774 */
4775
4776 if (flag & MBX_POLL) {
2e0fef85 4777 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4778
4779 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4780 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4781 "(%d):2529 Mailbox command x%x "
4782 "cannot issue Data: x%x x%x\n",
4783 pmbox->vport ? pmbox->vport->vpi : 0,
4784 pmbox->u.mb.mbxCommand,
4785 psli->sli_flag, flag);
58da1ffb 4786 goto out_not_finished;
dea3101e 4787 }
4788
3772a991 4789 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 4790 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4791 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4792 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4793 "(%d):2530 Mailbox command x%x "
4794 "cannot issue Data: x%x x%x\n",
4795 pmbox->vport ? pmbox->vport->vpi : 0,
4796 pmbox->u.mb.mbxCommand,
4797 psli->sli_flag, flag);
58da1ffb 4798 goto out_not_finished;
dea3101e 4799 }
4800
dea3101e 4801 /* Another mailbox command is still being processed, queue this
4802 * command to be processed later.
4803 */
4804 lpfc_mbox_put(phba, pmbox);
4805
4806 /* Mbox cmd issue - BUSY */
ed957684 4807 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4808 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 4809 "x%x x%x x%x x%x\n",
92d7f7b0
JS
4810 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4811 mb->mbxCommand, phba->pport->port_state,
4812 psli->sli_flag, flag);
dea3101e 4813
4814 psli->slistat.mbox_busy++;
2e0fef85 4815 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4816
858c9f6c
JS
4817 if (pmbox->vport) {
4818 lpfc_debugfs_disc_trc(pmbox->vport,
4819 LPFC_DISC_TRC_MBOX_VPORT,
4820 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4821 (uint32_t)mb->mbxCommand,
4822 mb->un.varWords[0], mb->un.varWords[1]);
4823 }
4824 else {
4825 lpfc_debugfs_disc_trc(phba->pport,
4826 LPFC_DISC_TRC_MBOX,
4827 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4828 (uint32_t)mb->mbxCommand,
4829 mb->un.varWords[0], mb->un.varWords[1]);
4830 }
4831
2e0fef85 4832 return MBX_BUSY;
dea3101e 4833 }
4834
dea3101e 4835 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4836
4837 /* If we are not polling, we MUST be in SLI2 mode */
4838 if (flag != MBX_POLL) {
3772a991 4839 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 4840 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 4841 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4842 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4843 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4844 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4845 "(%d):2531 Mailbox command x%x "
4846 "cannot issue Data: x%x x%x\n",
4847 pmbox->vport ? pmbox->vport->vpi : 0,
4848 pmbox->u.mb.mbxCommand,
4849 psli->sli_flag, flag);
58da1ffb 4850 goto out_not_finished;
dea3101e 4851 }
4852 /* timeout active mbox command */
a309a6b6
JS
4853 mod_timer(&psli->mbox_tmo, (jiffies +
4854 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea3101e 4855 }
4856
4857 /* Mailbox cmd <cmd> issue */
ed957684 4858 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4859 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 4860 "x%x\n",
e8b62011 4861 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
4862 mb->mbxCommand, phba->pport->port_state,
4863 psli->sli_flag, flag);
dea3101e 4864
858c9f6c
JS
4865 if (mb->mbxCommand != MBX_HEARTBEAT) {
4866 if (pmbox->vport) {
4867 lpfc_debugfs_disc_trc(pmbox->vport,
4868 LPFC_DISC_TRC_MBOX_VPORT,
4869 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4870 (uint32_t)mb->mbxCommand,
4871 mb->un.varWords[0], mb->un.varWords[1]);
4872 }
4873 else {
4874 lpfc_debugfs_disc_trc(phba->pport,
4875 LPFC_DISC_TRC_MBOX,
4876 "MBOX Send: cmd:x%x mb:x%x x%x",
4877 (uint32_t)mb->mbxCommand,
4878 mb->un.varWords[0], mb->un.varWords[1]);
4879 }
4880 }
4881
dea3101e 4882 psli->slistat.mbox_cmd++;
4883 evtctr = psli->slistat.mbox_event;
4884
4885 /* next set own bit for the adapter and copy over command word */
4886 mb->mbxOwner = OWN_CHIP;
4887
3772a991 4888 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4889 /* First copy command data to host SLIM area */
34b02dcd 4890 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4891 } else {
9290831f 4892 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 4893 /* copy command data into host mbox for cmpl */
34b02dcd 4894 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4895 }
4896
4897 /* First copy mbox command data to HBA SLIM, skip past first
4898 word */
4899 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4900 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4901 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4902
4903 /* Next copy over first word, with mbxOwner set */
34b02dcd 4904 ldata = *((uint32_t *)mb);
dea3101e 4905 to_slim = phba->MBslimaddr;
4906 writel(ldata, to_slim);
4907 readl(to_slim); /* flush */
4908
4909 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4910 /* switch over to host mailbox */
3772a991 4911 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 4912 }
4913 }
4914
4915 wmb();
dea3101e 4916
4917 switch (flag) {
4918 case MBX_NOWAIT:
09372820 4919 /* Set up reference to mailbox command */
dea3101e 4920 psli->mbox_active = pmbox;
09372820
JS
4921 /* Interrupt board to do it */
4922 writel(CA_MBATT, phba->CAregaddr);
4923 readl(phba->CAregaddr); /* flush */
4924 /* Don't wait for it to finish, just return */
dea3101e 4925 break;
4926
4927 case MBX_POLL:
09372820 4928 /* Set up null reference to mailbox command */
dea3101e 4929 psli->mbox_active = NULL;
09372820
JS
4930 /* Interrupt board to do it */
4931 writel(CA_MBATT, phba->CAregaddr);
4932 readl(phba->CAregaddr); /* flush */
4933
3772a991 4934 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4935 /* First read mbox status word */
34b02dcd 4936 word0 = *((uint32_t *)phba->mbox);
dea3101e 4937 word0 = le32_to_cpu(word0);
4938 } else {
4939 /* First read mbox status word */
4940 word0 = readl(phba->MBslimaddr);
4941 }
4942
4943 /* Read the HBA Host Attention Register */
4944 ha_copy = readl(phba->HAregaddr);
09372820
JS
4945 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4946 mb->mbxCommand) *
4947 1000) + jiffies;
4948 i = 0;
dea3101e 4949 /* Wait for command to complete */
41415862
JW
4950 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4951 (!(ha_copy & HA_MBATT) &&
2e0fef85 4952 (phba->link_state > LPFC_WARM_START))) {
09372820 4953 if (time_after(jiffies, timeout)) {
dea3101e 4954 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4955 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 4956 drvr_flag);
58da1ffb 4957 goto out_not_finished;
dea3101e 4958 }
4959
4960 /* Check if we took a mbox interrupt while we were
4961 polling */
4962 if (((word0 & OWN_CHIP) != OWN_CHIP)
4963 && (evtctr != psli->slistat.mbox_event))
4964 break;
4965
09372820
JS
4966 if (i++ > 10) {
4967 spin_unlock_irqrestore(&phba->hbalock,
4968 drvr_flag);
4969 msleep(1);
4970 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4971 }
dea3101e 4972
3772a991 4973 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4974 /* First copy command data */
34b02dcd 4975 word0 = *((uint32_t *)phba->mbox);
dea3101e 4976 word0 = le32_to_cpu(word0);
4977 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4978 MAILBOX_t *slimmb;
34b02dcd 4979 uint32_t slimword0;
dea3101e 4980 /* Check real SLIM for any errors */
4981 slimword0 = readl(phba->MBslimaddr);
4982 slimmb = (MAILBOX_t *) & slimword0;
4983 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4984 && slimmb->mbxStatus) {
4985 psli->sli_flag &=
3772a991 4986 ~LPFC_SLI_ACTIVE;
dea3101e 4987 word0 = slimword0;
4988 }
4989 }
4990 } else {
4991 /* First copy command data */
4992 word0 = readl(phba->MBslimaddr);
4993 }
4994 /* Read the HBA Host Attention Register */
4995 ha_copy = readl(phba->HAregaddr);
4996 }
4997
3772a991 4998 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4999 /* copy results back to user */
34b02dcd 5000 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea3101e 5001 } else {
5002 /* First copy command data */
5003 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5004 MAILBOX_CMD_SIZE);
5005 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5006 pmbox->context2) {
92d7f7b0 5007 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea3101e 5008 phba->MBslimaddr + DMP_RSP_OFFSET,
5009 mb->un.varDmp.word_cnt);
5010 }
5011 }
5012
5013 writel(HA_MBATT, phba->HAregaddr);
5014 readl(phba->HAregaddr); /* flush */
5015
5016 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5017 status = mb->mbxStatus;
5018 }
5019
2e0fef85
JS
5020 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5021 return status;
58da1ffb
JS
5022
5023out_not_finished:
5024 if (processing_queue) {
da0436e9 5025 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
5026 lpfc_mbox_cmpl_put(phba, pmbox);
5027 }
5028 return MBX_NOT_FINISHED;
dea3101e 5029}
5030
f1126688
JS
5031/**
5032 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5033 * @phba: Pointer to HBA context object.
5034 *
5035 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5036 * the driver internal pending mailbox queue. It will then try to wait out the
5037 * possible outstanding mailbox command before return.
5038 *
5039 * Returns:
5040 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5041 * the outstanding mailbox command timed out.
5042 **/
5043static int
5044lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5045{
5046 struct lpfc_sli *psli = &phba->sli;
5047 uint8_t actcmd = MBX_HEARTBEAT;
5048 int rc = 0;
5049 unsigned long timeout;
5050
5051 /* Mark the asynchronous mailbox command posting as blocked */
5052 spin_lock_irq(&phba->hbalock);
5053 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5054 if (phba->sli.mbox_active)
5055 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5056 spin_unlock_irq(&phba->hbalock);
5057 /* Determine how long we might wait for the active mailbox
5058 * command to be gracefully completed by firmware.
5059 */
5060 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5061 jiffies;
5062 /* Wait for the outstnading mailbox command to complete */
5063 while (phba->sli.mbox_active) {
5064 /* Check active mailbox complete status every 2ms */
5065 msleep(2);
5066 if (time_after(jiffies, timeout)) {
5067 /* Timeout, marked the outstanding cmd not complete */
5068 rc = 1;
5069 break;
5070 }
5071 }
5072
5073 /* Can not cleanly block async mailbox command, fails it */
5074 if (rc) {
5075 spin_lock_irq(&phba->hbalock);
5076 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5077 spin_unlock_irq(&phba->hbalock);
5078 }
5079 return rc;
5080}
5081
5082/**
5083 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5084 * @phba: Pointer to HBA context object.
5085 *
5086 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5087 * commands from the driver internal pending mailbox queue. It makes sure
5088 * that there is no outstanding mailbox command before resuming posting
5089 * asynchronous mailbox commands. If, for any reason, there is outstanding
5090 * mailbox command, it will try to wait it out before resuming asynchronous
5091 * mailbox command posting.
5092 **/
5093static void
5094lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5095{
5096 struct lpfc_sli *psli = &phba->sli;
5097
5098 spin_lock_irq(&phba->hbalock);
5099 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5100 /* Asynchronous mailbox posting is not blocked, do nothing */
5101 spin_unlock_irq(&phba->hbalock);
5102 return;
5103 }
5104
5105 /* Outstanding synchronous mailbox command is guaranteed to be done,
5106 * successful or timeout, after timing-out the outstanding mailbox
5107 * command shall always be removed, so just unblock posting async
5108 * mailbox command and resume
5109 */
5110 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5111 spin_unlock_irq(&phba->hbalock);
5112
5113 /* wake up worker thread to post asynchronlous mailbox command */
5114 lpfc_worker_wake_up(phba);
5115}
5116
da0436e9
JS
5117/**
5118 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5119 * @phba: Pointer to HBA context object.
5120 * @mboxq: Pointer to mailbox object.
5121 *
5122 * The function posts a mailbox to the port. The mailbox is expected
5123 * to be comletely filled in and ready for the port to operate on it.
5124 * This routine executes a synchronous completion operation on the
5125 * mailbox by polling for its completion.
5126 *
5127 * The caller must not be holding any locks when calling this routine.
5128 *
5129 * Returns:
5130 * MBX_SUCCESS - mailbox posted successfully
5131 * Any of the MBX error values.
5132 **/
5133static int
5134lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5135{
5136 int rc = MBX_SUCCESS;
5137 unsigned long iflag;
5138 uint32_t db_ready;
5139 uint32_t mcqe_status;
5140 uint32_t mbx_cmnd;
5141 unsigned long timeout;
5142 struct lpfc_sli *psli = &phba->sli;
5143 struct lpfc_mqe *mb = &mboxq->u.mqe;
5144 struct lpfc_bmbx_create *mbox_rgn;
5145 struct dma_address *dma_address;
5146 struct lpfc_register bmbx_reg;
5147
5148 /*
5149 * Only one mailbox can be active to the bootstrap mailbox region
5150 * at a time and there is no queueing provided.
5151 */
5152 spin_lock_irqsave(&phba->hbalock, iflag);
5153 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5154 spin_unlock_irqrestore(&phba->hbalock, iflag);
5155 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5156 "(%d):2532 Mailbox command x%x (x%x) "
5157 "cannot issue Data: x%x x%x\n",
5158 mboxq->vport ? mboxq->vport->vpi : 0,
5159 mboxq->u.mb.mbxCommand,
5160 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5161 psli->sli_flag, MBX_POLL);
5162 return MBXERR_ERROR;
5163 }
5164 /* The server grabs the token and owns it until release */
5165 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5166 phba->sli.mbox_active = mboxq;
5167 spin_unlock_irqrestore(&phba->hbalock, iflag);
5168
5169 /*
5170 * Initialize the bootstrap memory region to avoid stale data areas
5171 * in the mailbox post. Then copy the caller's mailbox contents to
5172 * the bmbx mailbox region.
5173 */
5174 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5175 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5176 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5177 sizeof(struct lpfc_mqe));
5178
5179 /* Post the high mailbox dma address to the port and wait for ready. */
5180 dma_address = &phba->sli4_hba.bmbx.dma_address;
5181 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5182
5183 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5184 * 1000) + jiffies;
5185 do {
5186 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5187 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5188 if (!db_ready)
5189 msleep(2);
5190
5191 if (time_after(jiffies, timeout)) {
5192 rc = MBXERR_ERROR;
5193 goto exit;
5194 }
5195 } while (!db_ready);
5196
5197 /* Post the low mailbox dma address to the port. */
5198 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5199 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5200 * 1000) + jiffies;
5201 do {
5202 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5203 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5204 if (!db_ready)
5205 msleep(2);
5206
5207 if (time_after(jiffies, timeout)) {
5208 rc = MBXERR_ERROR;
5209 goto exit;
5210 }
5211 } while (!db_ready);
5212
5213 /*
5214 * Read the CQ to ensure the mailbox has completed.
5215 * If so, update the mailbox status so that the upper layers
5216 * can complete the request normally.
5217 */
5218 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5219 sizeof(struct lpfc_mqe));
5220 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5221 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5222 sizeof(struct lpfc_mcqe));
5223 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5224
5225 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5226 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5227 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5228 rc = MBXERR_ERROR;
5229 }
5230
5231 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5232 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5233 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5234 " x%x x%x CQ: x%x x%x x%x x%x\n",
5235 mboxq->vport ? mboxq->vport->vpi : 0,
5236 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5237 bf_get(lpfc_mqe_status, mb),
5238 mb->un.mb_words[0], mb->un.mb_words[1],
5239 mb->un.mb_words[2], mb->un.mb_words[3],
5240 mb->un.mb_words[4], mb->un.mb_words[5],
5241 mb->un.mb_words[6], mb->un.mb_words[7],
5242 mb->un.mb_words[8], mb->un.mb_words[9],
5243 mb->un.mb_words[10], mb->un.mb_words[11],
5244 mb->un.mb_words[12], mboxq->mcqe.word0,
5245 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5246 mboxq->mcqe.trailer);
5247exit:
5248 /* We are holding the token, no needed for lock when release */
5249 spin_lock_irqsave(&phba->hbalock, iflag);
5250 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5251 phba->sli.mbox_active = NULL;
5252 spin_unlock_irqrestore(&phba->hbalock, iflag);
5253 return rc;
5254}
5255
5256/**
5257 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5258 * @phba: Pointer to HBA context object.
5259 * @pmbox: Pointer to mailbox object.
5260 * @flag: Flag indicating how the mailbox need to be processed.
5261 *
5262 * This function is called by discovery code and HBA management code to submit
5263 * a mailbox command to firmware with SLI-4 interface spec.
5264 *
5265 * Return codes the caller owns the mailbox command after the return of the
5266 * function.
5267 **/
5268static int
5269lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5270 uint32_t flag)
5271{
5272 struct lpfc_sli *psli = &phba->sli;
5273 unsigned long iflags;
5274 int rc;
5275
8fa38513
JS
5276 rc = lpfc_mbox_dev_check(phba);
5277 if (unlikely(rc)) {
5278 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5279 "(%d):2544 Mailbox command x%x (x%x) "
5280 "cannot issue Data: x%x x%x\n",
5281 mboxq->vport ? mboxq->vport->vpi : 0,
5282 mboxq->u.mb.mbxCommand,
5283 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5284 psli->sli_flag, flag);
5285 goto out_not_finished;
5286 }
5287
da0436e9
JS
5288 /* Detect polling mode and jump to a handler */
5289 if (!phba->sli4_hba.intr_enable) {
5290 if (flag == MBX_POLL)
5291 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5292 else
5293 rc = -EIO;
5294 if (rc != MBX_SUCCESS)
5295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5296 "(%d):2541 Mailbox command x%x "
5297 "(x%x) cannot issue Data: x%x x%x\n",
5298 mboxq->vport ? mboxq->vport->vpi : 0,
5299 mboxq->u.mb.mbxCommand,
5300 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5301 psli->sli_flag, flag);
5302 return rc;
5303 } else if (flag == MBX_POLL) {
f1126688
JS
5304 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5305 "(%d):2542 Try to issue mailbox command "
5306 "x%x (x%x) synchronously ahead of async"
5307 "mailbox command queue: x%x x%x\n",
da0436e9
JS
5308 mboxq->vport ? mboxq->vport->vpi : 0,
5309 mboxq->u.mb.mbxCommand,
5310 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5311 psli->sli_flag, flag);
f1126688
JS
5312 /* Try to block the asynchronous mailbox posting */
5313 rc = lpfc_sli4_async_mbox_block(phba);
5314 if (!rc) {
5315 /* Successfully blocked, now issue sync mbox cmd */
5316 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5317 if (rc != MBX_SUCCESS)
5318 lpfc_printf_log(phba, KERN_ERR,
5319 LOG_MBOX | LOG_SLI,
5320 "(%d):2597 Mailbox command "
5321 "x%x (x%x) cannot issue "
5322 "Data: x%x x%x\n",
5323 mboxq->vport ?
5324 mboxq->vport->vpi : 0,
5325 mboxq->u.mb.mbxCommand,
5326 lpfc_sli4_mbox_opcode_get(phba,
5327 mboxq),
5328 psli->sli_flag, flag);
5329 /* Unblock the async mailbox posting afterward */
5330 lpfc_sli4_async_mbox_unblock(phba);
5331 }
5332 return rc;
da0436e9
JS
5333 }
5334
5335 /* Now, interrupt mode asynchrous mailbox command */
5336 rc = lpfc_mbox_cmd_check(phba, mboxq);
5337 if (rc) {
5338 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5339 "(%d):2543 Mailbox command x%x (x%x) "
5340 "cannot issue Data: x%x x%x\n",
5341 mboxq->vport ? mboxq->vport->vpi : 0,
5342 mboxq->u.mb.mbxCommand,
5343 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5344 psli->sli_flag, flag);
5345 goto out_not_finished;
5346 }
da0436e9
JS
5347
5348 /* Put the mailbox command to the driver internal FIFO */
5349 psli->slistat.mbox_busy++;
5350 spin_lock_irqsave(&phba->hbalock, iflags);
5351 lpfc_mbox_put(phba, mboxq);
5352 spin_unlock_irqrestore(&phba->hbalock, iflags);
5353 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5354 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5355 "x%x (x%x) x%x x%x x%x\n",
5356 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5357 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5358 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5359 phba->pport->port_state,
5360 psli->sli_flag, MBX_NOWAIT);
5361 /* Wake up worker thread to transport mailbox command from head */
5362 lpfc_worker_wake_up(phba);
5363
5364 return MBX_BUSY;
5365
5366out_not_finished:
5367 return MBX_NOT_FINISHED;
5368}
5369
5370/**
5371 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5372 * @phba: Pointer to HBA context object.
5373 *
5374 * This function is called by worker thread to send a mailbox command to
5375 * SLI4 HBA firmware.
5376 *
5377 **/
5378int
5379lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5380{
5381 struct lpfc_sli *psli = &phba->sli;
5382 LPFC_MBOXQ_t *mboxq;
5383 int rc = MBX_SUCCESS;
5384 unsigned long iflags;
5385 struct lpfc_mqe *mqe;
5386 uint32_t mbx_cmnd;
5387
5388 /* Check interrupt mode before post async mailbox command */
5389 if (unlikely(!phba->sli4_hba.intr_enable))
5390 return MBX_NOT_FINISHED;
5391
5392 /* Check for mailbox command service token */
5393 spin_lock_irqsave(&phba->hbalock, iflags);
5394 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5395 spin_unlock_irqrestore(&phba->hbalock, iflags);
5396 return MBX_NOT_FINISHED;
5397 }
5398 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5399 spin_unlock_irqrestore(&phba->hbalock, iflags);
5400 return MBX_NOT_FINISHED;
5401 }
5402 if (unlikely(phba->sli.mbox_active)) {
5403 spin_unlock_irqrestore(&phba->hbalock, iflags);
5404 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5405 "0384 There is pending active mailbox cmd\n");
5406 return MBX_NOT_FINISHED;
5407 }
5408 /* Take the mailbox command service token */
5409 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5410
5411 /* Get the next mailbox command from head of queue */
5412 mboxq = lpfc_mbox_get(phba);
5413
5414 /* If no more mailbox command waiting for post, we're done */
5415 if (!mboxq) {
5416 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5417 spin_unlock_irqrestore(&phba->hbalock, iflags);
5418 return MBX_SUCCESS;
5419 }
5420 phba->sli.mbox_active = mboxq;
5421 spin_unlock_irqrestore(&phba->hbalock, iflags);
5422
5423 /* Check device readiness for posting mailbox command */
5424 rc = lpfc_mbox_dev_check(phba);
5425 if (unlikely(rc))
5426 /* Driver clean routine will clean up pending mailbox */
5427 goto out_not_finished;
5428
5429 /* Prepare the mbox command to be posted */
5430 mqe = &mboxq->u.mqe;
5431 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5432
5433 /* Start timer for the mbox_tmo and log some mailbox post messages */
5434 mod_timer(&psli->mbox_tmo, (jiffies +
5435 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5436
5437 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5438 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5439 "x%x x%x\n",
5440 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5441 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5442 phba->pport->port_state, psli->sli_flag);
5443
5444 if (mbx_cmnd != MBX_HEARTBEAT) {
5445 if (mboxq->vport) {
5446 lpfc_debugfs_disc_trc(mboxq->vport,
5447 LPFC_DISC_TRC_MBOX_VPORT,
5448 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5449 mbx_cmnd, mqe->un.mb_words[0],
5450 mqe->un.mb_words[1]);
5451 } else {
5452 lpfc_debugfs_disc_trc(phba->pport,
5453 LPFC_DISC_TRC_MBOX,
5454 "MBOX Send: cmd:x%x mb:x%x x%x",
5455 mbx_cmnd, mqe->un.mb_words[0],
5456 mqe->un.mb_words[1]);
5457 }
5458 }
5459 psli->slistat.mbox_cmd++;
5460
5461 /* Post the mailbox command to the port */
5462 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5463 if (rc != MBX_SUCCESS) {
5464 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5465 "(%d):2533 Mailbox command x%x (x%x) "
5466 "cannot issue Data: x%x x%x\n",
5467 mboxq->vport ? mboxq->vport->vpi : 0,
5468 mboxq->u.mb.mbxCommand,
5469 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5470 psli->sli_flag, MBX_NOWAIT);
5471 goto out_not_finished;
5472 }
5473
5474 return rc;
5475
5476out_not_finished:
5477 spin_lock_irqsave(&phba->hbalock, iflags);
5478 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5479 __lpfc_mbox_cmpl_put(phba, mboxq);
5480 /* Release the token */
5481 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5482 phba->sli.mbox_active = NULL;
5483 spin_unlock_irqrestore(&phba->hbalock, iflags);
5484
5485 return MBX_NOT_FINISHED;
5486}
5487
5488/**
5489 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5490 * @phba: Pointer to HBA context object.
5491 * @pmbox: Pointer to mailbox object.
5492 * @flag: Flag indicating how the mailbox need to be processed.
5493 *
5494 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5495 * the API jump table function pointer from the lpfc_hba struct.
5496 *
5497 * Return codes the caller owns the mailbox command after the return of the
5498 * function.
5499 **/
5500int
5501lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5502{
5503 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5504}
5505
5506/**
5507 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5508 * @phba: The hba struct for which this call is being executed.
5509 * @dev_grp: The HBA PCI-Device group number.
5510 *
5511 * This routine sets up the mbox interface API function jump table in @phba
5512 * struct.
5513 * Returns: 0 - success, -ENODEV - failure.
5514 **/
5515int
5516lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5517{
5518
5519 switch (dev_grp) {
5520 case LPFC_PCI_DEV_LP:
5521 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5522 phba->lpfc_sli_handle_slow_ring_event =
5523 lpfc_sli_handle_slow_ring_event_s3;
5524 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5525 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5526 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5527 break;
5528 case LPFC_PCI_DEV_OC:
5529 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5530 phba->lpfc_sli_handle_slow_ring_event =
5531 lpfc_sli_handle_slow_ring_event_s4;
5532 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5533 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5534 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5535 break;
5536 default:
5537 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5538 "1420 Invalid HBA PCI-device group: 0x%x\n",
5539 dev_grp);
5540 return -ENODEV;
5541 break;
5542 }
5543 return 0;
5544}
5545
e59058c4 5546/**
3621a710 5547 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
5548 * @phba: Pointer to HBA context object.
5549 * @pring: Pointer to driver SLI ring object.
5550 * @piocb: Pointer to address of newly added command iocb.
5551 *
5552 * This function is called with hbalock held to add a command
5553 * iocb to the txq when SLI layer cannot submit the command iocb
5554 * to the ring.
5555 **/
858c9f6c 5556static void
92d7f7b0 5557__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5558 struct lpfc_iocbq *piocb)
dea3101e 5559{
5560 /* Insert the caller's iocb in the txq tail for later processing. */
5561 list_add_tail(&piocb->list, &pring->txq);
5562 pring->txq_cnt++;
dea3101e 5563}
5564
e59058c4 5565/**
3621a710 5566 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
5567 * @phba: Pointer to HBA context object.
5568 * @pring: Pointer to driver SLI ring object.
5569 * @piocb: Pointer to address of newly added command iocb.
5570 *
5571 * This function is called with hbalock held before a new
5572 * iocb is submitted to the firmware. This function checks
5573 * txq to flush the iocbs in txq to Firmware before
5574 * submitting new iocbs to the Firmware.
5575 * If there are iocbs in the txq which need to be submitted
5576 * to firmware, lpfc_sli_next_iocb returns the first element
5577 * of the txq after dequeuing it from txq.
5578 * If there is no iocb in the txq then the function will return
5579 * *piocb and *piocb is set to NULL. Caller needs to check
5580 * *piocb to find if there are more commands in the txq.
5581 **/
dea3101e 5582static struct lpfc_iocbq *
5583lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5584 struct lpfc_iocbq **piocb)
dea3101e 5585{
5586 struct lpfc_iocbq * nextiocb;
5587
5588 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5589 if (!nextiocb) {
5590 nextiocb = *piocb;
5591 *piocb = NULL;
5592 }
5593
5594 return nextiocb;
5595}
5596
e59058c4 5597/**
3772a991 5598 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 5599 * @phba: Pointer to HBA context object.
3772a991 5600 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
5601 * @piocb: Pointer to command iocb.
5602 * @flag: Flag indicating if this command can be put into txq.
5603 *
3772a991
JS
5604 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5605 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5606 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5607 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5608 * this function allows only iocbs for posting buffers. This function finds
5609 * next available slot in the command ring and posts the command to the
5610 * available slot and writes the port attention register to request HBA start
5611 * processing new iocb. If there is no slot available in the ring and
5612 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5613 * the function returns IOCB_BUSY.
e59058c4 5614 *
3772a991
JS
5615 * This function is called with hbalock held. The function will return success
5616 * after it successfully submit the iocb to firmware or after adding to the
5617 * txq.
e59058c4 5618 **/
98c9ea5c 5619static int
3772a991 5620__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 5621 struct lpfc_iocbq *piocb, uint32_t flag)
5622{
5623 struct lpfc_iocbq *nextiocb;
5624 IOCB_t *iocb;
3772a991 5625 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 5626
92d7f7b0
JS
5627 if (piocb->iocb_cmpl && (!piocb->vport) &&
5628 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5629 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5630 lpfc_printf_log(phba, KERN_ERR,
5631 LOG_SLI | LOG_VPORT,
e8b62011 5632 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
5633 piocb->iocb.ulpCommand);
5634 dump_stack();
5635 return IOCB_ERROR;
5636 }
5637
5638
8d63f375
LV
5639 /* If the PCI channel is in offline state, do not post iocbs. */
5640 if (unlikely(pci_channel_offline(phba->pcidev)))
5641 return IOCB_ERROR;
5642
a257bf90
JS
5643 /* If HBA has a deferred error attention, fail the iocb. */
5644 if (unlikely(phba->hba_flag & DEFER_ERATT))
5645 return IOCB_ERROR;
5646
dea3101e 5647 /*
5648 * We should never get an IOCB if we are in a < LINK_DOWN state
5649 */
2e0fef85 5650 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 5651 return IOCB_ERROR;
5652
5653 /*
5654 * Check to see if we are blocking IOCB processing because of a
0b727fea 5655 * outstanding event.
dea3101e 5656 */
0b727fea 5657 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 5658 goto iocb_busy;
5659
2e0fef85 5660 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 5661 /*
2680eeaa 5662 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 5663 * can be issued if the link is not up.
5664 */
5665 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
5666 case CMD_GEN_REQUEST64_CR:
5667 case CMD_GEN_REQUEST64_CX:
5668 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5669 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 5670 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
5671 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5672 MENLO_TRANSPORT_TYPE))
5673
5674 goto iocb_busy;
5675 break;
dea3101e 5676 case CMD_QUE_RING_BUF_CN:
5677 case CMD_QUE_RING_BUF64_CN:
dea3101e 5678 /*
5679 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5680 * completion, iocb_cmpl MUST be 0.
5681 */
5682 if (piocb->iocb_cmpl)
5683 piocb->iocb_cmpl = NULL;
5684 /*FALLTHROUGH*/
5685 case CMD_CREATE_XRI_CR:
2680eeaa
JS
5686 case CMD_CLOSE_XRI_CN:
5687 case CMD_CLOSE_XRI_CX:
dea3101e 5688 break;
5689 default:
5690 goto iocb_busy;
5691 }
5692
5693 /*
5694 * For FCP commands, we must be in a state where we can process link
5695 * attention events.
5696 */
5697 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 5698 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 5699 goto iocb_busy;
92d7f7b0 5700 }
dea3101e 5701
dea3101e 5702 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5703 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5704 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5705
5706 if (iocb)
5707 lpfc_sli_update_ring(phba, pring);
5708 else
5709 lpfc_sli_update_full_ring(phba, pring);
5710
5711 if (!piocb)
5712 return IOCB_SUCCESS;
5713
5714 goto out_busy;
5715
5716 iocb_busy:
5717 pring->stats.iocb_cmd_delay++;
5718
5719 out_busy:
5720
5721 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 5722 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 5723 return IOCB_SUCCESS;
5724 }
5725
5726 return IOCB_BUSY;
5727}
5728
3772a991 5729/**
4f774513
JS
5730 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5731 * @phba: Pointer to HBA context object.
5732 * @piocb: Pointer to command iocb.
5733 * @sglq: Pointer to the scatter gather queue object.
5734 *
5735 * This routine converts the bpl or bde that is in the IOCB
5736 * to a sgl list for the sli4 hardware. The physical address
5737 * of the bpl/bde is converted back to a virtual address.
5738 * If the IOCB contains a BPL then the list of BDE's is
5739 * converted to sli4_sge's. If the IOCB contains a single
5740 * BDE then it is converted to a single sli_sge.
5741 * The IOCB is still in cpu endianess so the contents of
5742 * the bpl can be used without byte swapping.
5743 *
5744 * Returns valid XRI = Success, NO_XRI = Failure.
5745**/
5746static uint16_t
5747lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5748 struct lpfc_sglq *sglq)
3772a991 5749{
4f774513
JS
5750 uint16_t xritag = NO_XRI;
5751 struct ulp_bde64 *bpl = NULL;
5752 struct ulp_bde64 bde;
5753 struct sli4_sge *sgl = NULL;
5754 IOCB_t *icmd;
5755 int numBdes = 0;
5756 int i = 0;
3772a991 5757
4f774513
JS
5758 if (!piocbq || !sglq)
5759 return xritag;
5760
5761 sgl = (struct sli4_sge *)sglq->sgl;
5762 icmd = &piocbq->iocb;
5763 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5764 numBdes = icmd->un.genreq64.bdl.bdeSize /
5765 sizeof(struct ulp_bde64);
5766 /* The addrHigh and addrLow fields within the IOCB
5767 * have not been byteswapped yet so there is no
5768 * need to swap them back.
5769 */
5770 bpl = (struct ulp_bde64 *)
5771 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5772
5773 if (!bpl)
5774 return xritag;
5775
5776 for (i = 0; i < numBdes; i++) {
5777 /* Should already be byte swapped. */
28baac74
JS
5778 sgl->addr_hi = bpl->addrHigh;
5779 sgl->addr_lo = bpl->addrLow;
5780
4f774513
JS
5781 if ((i+1) == numBdes)
5782 bf_set(lpfc_sli4_sge_last, sgl, 1);
5783 else
5784 bf_set(lpfc_sli4_sge_last, sgl, 0);
5785 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5786 /* swap the size field back to the cpu so we
5787 * can assign it to the sgl.
5788 */
5789 bde.tus.w = le32_to_cpu(bpl->tus.w);
5790 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
4f774513
JS
5791 bpl++;
5792 sgl++;
5793 }
5794 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5795 /* The addrHigh and addrLow fields of the BDE have not
5796 * been byteswapped yet so they need to be swapped
5797 * before putting them in the sgl.
5798 */
5799 sgl->addr_hi =
5800 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5801 sgl->addr_lo =
5802 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
4f774513
JS
5803 bf_set(lpfc_sli4_sge_last, sgl, 1);
5804 sgl->word2 = cpu_to_le32(sgl->word2);
28baac74
JS
5805 sgl->sge_len =
5806 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
4f774513
JS
5807 }
5808 return sglq->sli4_xritag;
3772a991 5809}
92d7f7b0 5810
e59058c4 5811/**
4f774513 5812 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 5813 * @phba: Pointer to HBA context object.
e59058c4 5814 *
4f774513 5815 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
8fa38513
JS
5816 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5817 * held.
4f774513
JS
5818 *
5819 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 5820 **/
4f774513 5821static uint32_t
8fa38513 5822lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 5823{
8fa38513
JS
5824 ++phba->fcp_qidx;
5825 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5826 phba->fcp_qidx = 0;
92d7f7b0 5827
8fa38513 5828 return phba->fcp_qidx;
92d7f7b0
JS
5829}
5830
e59058c4 5831/**
4f774513 5832 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 5833 * @phba: Pointer to HBA context object.
4f774513
JS
5834 * @piocb: Pointer to command iocb.
5835 * @wqe: Pointer to the work queue entry.
e59058c4 5836 *
4f774513
JS
5837 * This routine converts the iocb command to its Work Queue Entry
5838 * equivalent. The wqe pointer should not have any fields set when
5839 * this routine is called because it will memcpy over them.
5840 * This routine does not set the CQ_ID or the WQEC bits in the
5841 * wqe.
e59058c4 5842 *
4f774513 5843 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 5844 **/
cf5bf97e 5845static int
4f774513
JS
5846lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5847 union lpfc_wqe *wqe)
cf5bf97e 5848{
5ffc266e 5849 uint32_t xmit_len = 0, total_len = 0;
4f774513
JS
5850 uint8_t ct = 0;
5851 uint32_t fip;
5852 uint32_t abort_tag;
5853 uint8_t command_type = ELS_COMMAND_NON_FIP;
5854 uint8_t cmnd;
5855 uint16_t xritag;
5856 struct ulp_bde64 *bpl = NULL;
c868595d 5857 uint32_t els_id = ELS_ID_DEFAULT;
5ffc266e
JS
5858 int numBdes, i;
5859 struct ulp_bde64 bde;
4f774513 5860
45ed1190 5861 fip = phba->hba_flag & HBA_FIP_SUPPORT;
4f774513 5862 /* The fcp commands will set command type */
0c287589 5863 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 5864 command_type = FCP_COMMAND;
c868595d 5865 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
0c287589
JS
5866 command_type = ELS_COMMAND_FIP;
5867 else
5868 command_type = ELS_COMMAND_NON_FIP;
5869
4f774513
JS
5870 /* Some of the fields are in the right position already */
5871 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5872 abort_tag = (uint32_t) iocbq->iotag;
5873 xritag = iocbq->sli4_xritag;
5874 wqe->words[7] = 0; /* The ct field has moved so reset */
5875 /* words0-2 bpl convert bde */
5876 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5ffc266e
JS
5877 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
5878 sizeof(struct ulp_bde64);
4f774513
JS
5879 bpl = (struct ulp_bde64 *)
5880 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5881 if (!bpl)
5882 return IOCB_ERROR;
cf5bf97e 5883
4f774513
JS
5884 /* Should already be byte swapped. */
5885 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5886 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5887 /* swap the size field back to the cpu so we
5888 * can assign it to the sgl.
5889 */
5890 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5ffc266e
JS
5891 xmit_len = wqe->generic.bde.tus.f.bdeSize;
5892 total_len = 0;
5893 for (i = 0; i < numBdes; i++) {
5894 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
5895 total_len += bde.tus.f.bdeSize;
5896 }
4f774513 5897 } else
5ffc266e 5898 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 5899
4f774513
JS
5900 iocbq->iocb.ulpIoTag = iocbq->iotag;
5901 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 5902
4f774513
JS
5903 switch (iocbq->iocb.ulpCommand) {
5904 case CMD_ELS_REQUEST64_CR:
5905 if (!iocbq->iocb.ulpLe) {
5906 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5907 "2007 Only Limited Edition cmd Format"
5908 " supported 0x%x\n",
5909 iocbq->iocb.ulpCommand);
5910 return IOCB_ERROR;
5911 }
5ffc266e 5912 wqe->els_req.payload_len = xmit_len;
4f774513
JS
5913 /* Els_reguest64 has a TMO */
5914 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5915 iocbq->iocb.ulpTimeout);
5916 /* Need a VF for word 4 set the vf bit*/
5917 bf_set(els_req64_vf, &wqe->els_req, 0);
5918 /* And a VFID for word 12 */
5919 bf_set(els_req64_vfid, &wqe->els_req, 0);
5920 /*
5921 * Set ct field to 3, indicates that the context_tag field
5922 * contains the FCFI and remote N_Port_ID is
5923 * in word 5.
5924 */
5925
5926 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5927 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5928 iocbq->iocb.ulpContext);
5929
4f774513
JS
5930 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5931 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5932 /* CCP CCPE PV PRI in word10 were set in the memcpy */
c868595d
JS
5933
5934 if (command_type == ELS_COMMAND_FIP) {
5935 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
5936 >> LPFC_FIP_ELS_ID_SHIFT);
5937 }
5938 bf_set(lpfc_wqe_gen_els_id, &wqe->generic, els_id);
5939
4f774513 5940 break;
5ffc266e
JS
5941 case CMD_XMIT_SEQUENCE64_CX:
5942 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5943 iocbq->iocb.un.ulpWord[3]);
5944 wqe->generic.word3 = 0;
5945 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
5ffc266e
JS
5946 /* The entire sequence is transmitted for this IOCB */
5947 xmit_len = total_len;
5948 cmnd = CMD_XMIT_SEQUENCE64_CR;
4f774513
JS
5949 case CMD_XMIT_SEQUENCE64_CR:
5950 /* word3 iocb=io_tag32 wqe=payload_offset */
5951 /* payload offset used for multilpe outstanding
5952 * sequences on the same exchange
5953 */
5954 wqe->words[3] = 0;
5955 /* word4 relative_offset memcpy */
5956 /* word5 r_ctl/df_ctl memcpy */
5957 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5ffc266e
JS
5958 wqe->xmit_sequence.xmit_len = xmit_len;
5959 command_type = OTHER_COMMAND;
4f774513
JS
5960 break;
5961 case CMD_XMIT_BCAST64_CN:
5962 /* word3 iocb=iotag32 wqe=payload_len */
5963 wqe->words[3] = 0; /* no definition for this in wqe */
5964 /* word4 iocb=rsvd wqe=rsvd */
5965 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5966 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5967 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5968 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5969 break;
5970 case CMD_FCP_IWRITE64_CR:
5971 command_type = FCP_COMMAND_DATA_OUT;
5972 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5973 * confusing.
5974 * word3 is payload_len: byte offset to the sgl entry for the
5975 * fcp_command.
5976 * word4 is total xfer len, same as the IOCB->ulpParameter.
5977 * word5 is initial xfer len 0 = wait for xfer-ready
5978 */
5979
5980 /* Always wait for xfer-ready before sending data */
5981 wqe->fcp_iwrite.initial_xfer_len = 0;
5982 /* word 4 (xfer length) should have been set on the memcpy */
5983
5984 /* allow write to fall through to read */
5985 case CMD_FCP_IREAD64_CR:
5986 /* FCP_CMD is always the 1st sgl entry */
5987 wqe->fcp_iread.payload_len =
5ffc266e 5988 xmit_len + sizeof(struct fcp_rsp);
4f774513
JS
5989
5990 /* word 4 (xfer length) should have been set on the memcpy */
5991
5992 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5993 iocbq->iocb.ulpFCP2Rcvy);
5994 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5995 /* The XC bit and the XS bit are similar. The driver never
5996 * tracked whether or not the exchange was previouslly open.
5997 * XC = Exchange create, 0 is create. 1 is already open.
5998 * XS = link cmd: 1 do not close the exchange after command.
5999 * XS = 0 close exchange when command completes.
6000 * The only time we would not set the XC bit is when the XS bit
6001 * is set and we are sending our 2nd or greater command on
6002 * this exchange.
6003 */
f1126688
JS
6004 /* Always open the exchange */
6005 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
4f774513 6006
f1126688
JS
6007 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6008 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6009 break;
4f774513
JS
6010 case CMD_FCP_ICMND64_CR:
6011 /* Always open the exchange */
6012 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6013
f1126688 6014 wqe->words[4] = 0;
4f774513 6015 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
f1126688 6016 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
4f774513
JS
6017 break;
6018 case CMD_GEN_REQUEST64_CR:
6019 /* word3 command length is described as byte offset to the
6020 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6021 * sgl[0] = cmnd
6022 * sgl[1] = rsp.
6023 *
6024 */
5ffc266e 6025 wqe->gen_req.command_len = xmit_len;
4f774513
JS
6026 /* Word4 parameter copied in the memcpy */
6027 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6028 /* word6 context tag copied in memcpy */
6029 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6030 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6032 "2015 Invalid CT %x command 0x%x\n",
6033 ct, iocbq->iocb.ulpCommand);
6034 return IOCB_ERROR;
6035 }
6036 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6037 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6038 iocbq->iocb.ulpTimeout);
6039
6040 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6041 command_type = OTHER_COMMAND;
6042 break;
6043 case CMD_XMIT_ELS_RSP64_CX:
6044 /* words0-2 BDE memcpy */
6045 /* word3 iocb=iotag32 wqe=rsvd */
6046 wqe->words[3] = 0;
6047 /* word4 iocb=did wge=rsvd. */
6048 wqe->words[4] = 0;
6049 /* word5 iocb=rsvd wge=did */
6050 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6051 iocbq->iocb.un.elsreq64.remoteID);
6052
6053 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6054 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6055
6056 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6057 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6058 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6059 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6060 iocbq->vport->vpi + phba->vpi_base);
6061 command_type = OTHER_COMMAND;
6062 break;
6063 case CMD_CLOSE_XRI_CN:
6064 case CMD_ABORT_XRI_CN:
6065 case CMD_ABORT_XRI_CX:
6066 /* words 0-2 memcpy should be 0 rserved */
6067 /* port will send abts */
6068 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6069 /*
6070 * The link is down so the fw does not need to send abts
6071 * on the wire.
6072 */
6073 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6074 else
6075 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6076 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
4f774513
JS
6077 wqe->words[5] = 0;
6078 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6079 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6080 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
4f774513
JS
6081 /*
6082 * The abort handler will send us CMD_ABORT_XRI_CN or
6083 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6084 */
6085 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6086 cmnd = CMD_ABORT_XRI_CX;
6087 command_type = OTHER_COMMAND;
6088 xritag = 0;
6089 break;
6669f9bb
JS
6090 case CMD_XMIT_BLS_RSP64_CX:
6091 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6092 * we re-construct this WQE here based on information in
6093 * iocbq from scratch.
6094 */
6095 memset(wqe, 0, sizeof(union lpfc_wqe));
5ffc266e 6096 /* OX_ID is invariable to who sent ABTS to CT exchange */
6669f9bb 6097 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
5ffc266e
JS
6098 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_acc));
6099 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_acc) ==
6100 LPFC_ABTS_UNSOL_INT) {
6101 /* ABTS sent by initiator to CT exchange, the
6102 * RX_ID field will be filled with the newly
6103 * allocated responder XRI.
6104 */
6105 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6106 iocbq->sli4_xritag);
6107 } else {
6108 /* ABTS sent by responder to CT exchange, the
6109 * RX_ID field will be filled with the responder
6110 * RX_ID from ABTS.
6111 */
6112 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6113 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_acc));
6114 }
6669f9bb
JS
6115 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6116 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6117 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6118 iocbq->iocb.ulpContext);
6119 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6120 command_type = OTHER_COMMAND;
6121 break;
4f774513
JS
6122 case CMD_XRI_ABORTED_CX:
6123 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6124 /* words0-2 are all 0's no bde */
6125 /* word3 and word4 are rsvrd */
6126 wqe->words[3] = 0;
6127 wqe->words[4] = 0;
6128 /* word5 iocb=rsvd wge=did */
6129 /* There is no remote port id in the IOCB? */
6130 /* Let this fall through and fail */
6131 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6132 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6133 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6134 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6135 default:
6136 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6137 "2014 Invalid command 0x%x\n",
6138 iocbq->iocb.ulpCommand);
6139 return IOCB_ERROR;
6140 break;
6141
6142 }
6143 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6144 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6145 wqe->generic.abort_tag = abort_tag;
6146 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6147 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6148 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6149 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6150
6151 return 0;
6152}
6153
6154/**
6155 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6156 * @phba: Pointer to HBA context object.
6157 * @ring_number: SLI ring number to issue iocb on.
6158 * @piocb: Pointer to command iocb.
6159 * @flag: Flag indicating if this command can be put into txq.
6160 *
6161 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6162 * an iocb command to an HBA with SLI-4 interface spec.
6163 *
6164 * This function is called with hbalock held. The function will return success
6165 * after it successfully submit the iocb to firmware or after adding to the
6166 * txq.
6167 **/
6168static int
6169__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6170 struct lpfc_iocbq *piocb, uint32_t flag)
6171{
6172 struct lpfc_sglq *sglq;
6173 uint16_t xritag;
6174 union lpfc_wqe wqe;
6175 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
4f774513
JS
6176
6177 if (piocb->sli4_xritag == NO_XRI) {
6178 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6669f9bb 6179 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
6180 sglq = NULL;
6181 else {
6182 sglq = __lpfc_sli_get_sglq(phba);
6183 if (!sglq)
6184 return IOCB_ERROR;
6185 piocb->sli4_xritag = sglq->sli4_xritag;
6186 }
6187 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6188 sglq = NULL; /* These IO's already have an XRI and
6189 * a mapped sgl.
6190 */
6191 } else {
6192 /* This is a continuation of a commandi,(CX) so this
6193 * sglq is on the active list
6194 */
6195 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6196 if (!sglq)
6197 return IOCB_ERROR;
6198 }
6199
6200 if (sglq) {
6201 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6202 if (xritag != sglq->sli4_xritag)
6203 return IOCB_ERROR;
6204 }
6205
6206 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6207 return IOCB_ERROR;
6208
341af102
JS
6209 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
6210 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
5ffc266e
JS
6211 /*
6212 * For FCP command IOCB, get a new WQ index to distribute
6213 * WQE across the WQsr. On the other hand, for abort IOCB,
6214 * it carries the same WQ index to the original command
6215 * IOCB.
6216 */
341af102 6217 if (piocb->iocb_flag & LPFC_IO_FCP)
5ffc266e
JS
6218 piocb->fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
6219 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
6220 &wqe))
4f774513
JS
6221 return IOCB_ERROR;
6222 } else {
6223 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6224 return IOCB_ERROR;
6225 }
6226 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6227
6228 return 0;
6229}
6230
6231/**
6232 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6233 *
6234 * This routine wraps the actual lockless version for issusing IOCB function
6235 * pointer from the lpfc_hba struct.
6236 *
6237 * Return codes:
6238 * IOCB_ERROR - Error
6239 * IOCB_SUCCESS - Success
6240 * IOCB_BUSY - Busy
6241 **/
6242static inline int
6243__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6244 struct lpfc_iocbq *piocb, uint32_t flag)
6245{
6246 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6247}
6248
6249/**
6250 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6251 * @phba: The hba struct for which this call is being executed.
6252 * @dev_grp: The HBA PCI-Device group number.
6253 *
6254 * This routine sets up the SLI interface API function jump table in @phba
6255 * struct.
6256 * Returns: 0 - success, -ENODEV - failure.
6257 **/
6258int
6259lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6260{
6261
6262 switch (dev_grp) {
6263 case LPFC_PCI_DEV_LP:
6264 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6265 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6266 break;
6267 case LPFC_PCI_DEV_OC:
6268 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6269 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6270 break;
6271 default:
6272 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6273 "1419 Invalid HBA PCI-device group: 0x%x\n",
6274 dev_grp);
6275 return -ENODEV;
6276 break;
6277 }
6278 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6279 return 0;
6280}
6281
6282/**
6283 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6284 * @phba: Pointer to HBA context object.
6285 * @pring: Pointer to driver SLI ring object.
6286 * @piocb: Pointer to command iocb.
6287 * @flag: Flag indicating if this command can be put into txq.
6288 *
6289 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6290 * function. This function gets the hbalock and calls
6291 * __lpfc_sli_issue_iocb function and will return the error returned
6292 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6293 * functions which do not hold hbalock.
6294 **/
6295int
6296lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6297 struct lpfc_iocbq *piocb, uint32_t flag)
6298{
6299 unsigned long iflags;
6300 int rc;
6301
6302 spin_lock_irqsave(&phba->hbalock, iflags);
6303 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6304 spin_unlock_irqrestore(&phba->hbalock, iflags);
6305
6306 return rc;
6307}
6308
6309/**
6310 * lpfc_extra_ring_setup - Extra ring setup function
6311 * @phba: Pointer to HBA context object.
6312 *
6313 * This function is called while driver attaches with the
6314 * HBA to setup the extra ring. The extra ring is used
6315 * only when driver needs to support target mode functionality
6316 * or IP over FC functionalities.
6317 *
6318 * This function is called with no lock held.
6319 **/
6320static int
6321lpfc_extra_ring_setup( struct lpfc_hba *phba)
6322{
6323 struct lpfc_sli *psli;
6324 struct lpfc_sli_ring *pring;
6325
6326 psli = &phba->sli;
6327
6328 /* Adjust cmd/rsp ring iocb entries more evenly */
6329
6330 /* Take some away from the FCP ring */
6331 pring = &psli->ring[psli->fcp_ring];
6332 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6333 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
6334 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6335 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6336
a4bc3379
JS
6337 /* and give them to the extra ring */
6338 pring = &psli->ring[psli->extra_ring];
6339
cf5bf97e
JW
6340 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6341 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6342 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6343 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6344
6345 /* Setup default profile for this ring */
6346 pring->iotag_max = 4096;
6347 pring->num_mask = 1;
6348 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
6349 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6350 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
6351 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6352 return 0;
6353}
6354
e59058c4 6355/**
3621a710 6356 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
6357 * @phba: Pointer to HBA context object.
6358 * @pring: Pointer to driver SLI ring object.
6359 * @iocbq: Pointer to iocb object.
6360 *
6361 * This function is called by the slow ring event handler
6362 * function when there is an ASYNC event iocb in the ring.
6363 * This function is called with no lock held.
6364 * Currently this function handles only temperature related
6365 * ASYNC events. The function decodes the temperature sensor
6366 * event message and posts events for the management applications.
6367 **/
98c9ea5c 6368static void
57127f15
JS
6369lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6370 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6371{
6372 IOCB_t *icmd;
6373 uint16_t evt_code;
6374 uint16_t temp;
6375 struct temp_event temp_event_data;
6376 struct Scsi_Host *shost;
a257bf90 6377 uint32_t *iocb_w;
57127f15
JS
6378
6379 icmd = &iocbq->iocb;
6380 evt_code = icmd->un.asyncstat.evt_code;
6381 temp = icmd->ulpContext;
6382
6383 if ((evt_code != ASYNC_TEMP_WARN) &&
6384 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 6385 iocb_w = (uint32_t *) icmd;
57127f15
JS
6386 lpfc_printf_log(phba,
6387 KERN_ERR,
6388 LOG_SLI,
76bb24ef 6389 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 6390 " evt_code 0x%x\n"
a257bf90
JS
6391 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6392 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6393 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6394 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 6395 pring->ringno,
a257bf90
JS
6396 icmd->un.asyncstat.evt_code,
6397 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6398 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6399 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6400 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6401
57127f15
JS
6402 return;
6403 }
6404 temp_event_data.data = (uint32_t)temp;
6405 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6406 if (evt_code == ASYNC_TEMP_WARN) {
6407 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6408 lpfc_printf_log(phba,
09372820 6409 KERN_ERR,
57127f15 6410 LOG_TEMP,
76bb24ef 6411 "0347 Adapter is very hot, please take "
57127f15
JS
6412 "corrective action. temperature : %d Celsius\n",
6413 temp);
6414 }
6415 if (evt_code == ASYNC_TEMP_SAFE) {
6416 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6417 lpfc_printf_log(phba,
09372820 6418 KERN_ERR,
57127f15
JS
6419 LOG_TEMP,
6420 "0340 Adapter temperature is OK now. "
6421 "temperature : %d Celsius\n",
6422 temp);
6423 }
6424
6425 /* Send temperature change event to applications */
6426 shost = lpfc_shost_from_vport(phba->pport);
6427 fc_host_post_vendor_event(shost, fc_get_event_number(),
6428 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 6429 LPFC_NL_VENDOR_ID);
57127f15
JS
6430
6431}
6432
6433
e59058c4 6434/**
3621a710 6435 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
6436 * @phba: Pointer to HBA context object.
6437 *
6438 * lpfc_sli_setup sets up rings of the SLI interface with
6439 * number of iocbs per ring and iotags. This function is
6440 * called while driver attach to the HBA and before the
6441 * interrupts are enabled. So there is no need for locking.
6442 *
6443 * This function always returns 0.
6444 **/
dea3101e 6445int
6446lpfc_sli_setup(struct lpfc_hba *phba)
6447{
ed957684 6448 int i, totiocbsize = 0;
dea3101e 6449 struct lpfc_sli *psli = &phba->sli;
6450 struct lpfc_sli_ring *pring;
6451
6452 psli->num_rings = MAX_CONFIGURED_RINGS;
6453 psli->sli_flag = 0;
6454 psli->fcp_ring = LPFC_FCP_RING;
6455 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 6456 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 6457
604a3e30
JB
6458 psli->iocbq_lookup = NULL;
6459 psli->iocbq_lookup_len = 0;
6460 psli->last_iotag = 0;
6461
dea3101e 6462 for (i = 0; i < psli->num_rings; i++) {
6463 pring = &psli->ring[i];
6464 switch (i) {
6465 case LPFC_FCP_RING: /* ring 0 - FCP */
6466 /* numCiocb and numRiocb are used in config_port */
6467 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6468 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6469 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6470 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6471 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6472 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 6473 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6474 SLI3_IOCB_CMD_SIZE :
6475 SLI2_IOCB_CMD_SIZE;
ed957684 6476 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6477 SLI3_IOCB_RSP_SIZE :
6478 SLI2_IOCB_RSP_SIZE;
dea3101e 6479 pring->iotag_ctr = 0;
6480 pring->iotag_max =
92d7f7b0 6481 (phba->cfg_hba_queue_depth * 2);
dea3101e 6482 pring->fast_iotag = pring->iotag_max;
6483 pring->num_mask = 0;
6484 break;
a4bc3379 6485 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 6486 /* numCiocb and numRiocb are used in config_port */
6487 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6488 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 6489 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6490 SLI3_IOCB_CMD_SIZE :
6491 SLI2_IOCB_CMD_SIZE;
ed957684 6492 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6493 SLI3_IOCB_RSP_SIZE :
6494 SLI2_IOCB_RSP_SIZE;
2e0fef85 6495 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 6496 pring->num_mask = 0;
6497 break;
6498 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6499 /* numCiocb and numRiocb are used in config_port */
6500 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6501 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 6502 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6503 SLI3_IOCB_CMD_SIZE :
6504 SLI2_IOCB_CMD_SIZE;
ed957684 6505 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6506 SLI3_IOCB_RSP_SIZE :
6507 SLI2_IOCB_RSP_SIZE;
dea3101e 6508 pring->fast_iotag = 0;
6509 pring->iotag_ctr = 0;
6510 pring->iotag_max = 4096;
57127f15
JS
6511 pring->lpfc_sli_rcv_async_status =
6512 lpfc_sli_async_event_handler;
6669f9bb 6513 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 6514 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
6515 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6516 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 6517 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 6518 lpfc_els_unsol_event;
dea3101e 6519 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
6520 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6521 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 6522 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 6523 lpfc_els_unsol_event;
dea3101e 6524 pring->prt[2].profile = 0; /* Mask 2 */
6525 /* NameServer Inquiry */
6a9c52cf 6526 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 6527 /* NameServer */
6a9c52cf 6528 pring->prt[2].type = FC_TYPE_CT;
dea3101e 6529 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 6530 lpfc_ct_unsol_event;
dea3101e 6531 pring->prt[3].profile = 0; /* Mask 3 */
6532 /* NameServer response */
6a9c52cf 6533 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 6534 /* NameServer */
6a9c52cf 6535 pring->prt[3].type = FC_TYPE_CT;
dea3101e 6536 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 6537 lpfc_ct_unsol_event;
6669f9bb
JS
6538 /* abort unsolicited sequence */
6539 pring->prt[4].profile = 0; /* Mask 4 */
6540 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6541 pring->prt[4].type = FC_TYPE_BLS;
6542 pring->prt[4].lpfc_sli_rcv_unsol_event =
6543 lpfc_sli4_ct_abort_unsol_event;
dea3101e 6544 break;
6545 }
ed957684 6546 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 6547 (pring->numRiocb * pring->sizeRiocb);
dea3101e 6548 }
ed957684 6549 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 6550 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
6551 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6552 "SLI2 SLIM Data: x%x x%lx\n",
6553 phba->brd_no, totiocbsize,
6554 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 6555 }
cf5bf97e
JW
6556 if (phba->cfg_multi_ring_support == 2)
6557 lpfc_extra_ring_setup(phba);
dea3101e 6558
6559 return 0;
6560}
6561
e59058c4 6562/**
3621a710 6563 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
6564 * @phba: Pointer to HBA context object.
6565 *
6566 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6567 * ring. This function also initializes ring indices of each ring.
6568 * This function is called during the initialization of the SLI
6569 * interface of an HBA.
6570 * This function is called with no lock held and always returns
6571 * 1.
6572 **/
dea3101e 6573int
2e0fef85 6574lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e 6575{
6576 struct lpfc_sli *psli;
6577 struct lpfc_sli_ring *pring;
604a3e30 6578 int i;
dea3101e 6579
6580 psli = &phba->sli;
2e0fef85 6581 spin_lock_irq(&phba->hbalock);
dea3101e 6582 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 6583 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 6584 /* Initialize list headers for txq and txcmplq as double linked lists */
6585 for (i = 0; i < psli->num_rings; i++) {
6586 pring = &psli->ring[i];
6587 pring->ringno = i;
6588 pring->next_cmdidx = 0;
6589 pring->local_getidx = 0;
6590 pring->cmdidx = 0;
6591 INIT_LIST_HEAD(&pring->txq);
6592 INIT_LIST_HEAD(&pring->txcmplq);
6593 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 6594 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 6595 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 6596 }
2e0fef85
JS
6597 spin_unlock_irq(&phba->hbalock);
6598 return 1;
dea3101e 6599}
6600
04c68496
JS
6601/**
6602 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6603 * @phba: Pointer to HBA context object.
6604 *
6605 * This routine flushes the mailbox command subsystem. It will unconditionally
6606 * flush all the mailbox commands in the three possible stages in the mailbox
6607 * command sub-system: pending mailbox command queue; the outstanding mailbox
6608 * command; and completed mailbox command queue. It is caller's responsibility
6609 * to make sure that the driver is in the proper state to flush the mailbox
6610 * command sub-system. Namely, the posting of mailbox commands into the
6611 * pending mailbox command queue from the various clients must be stopped;
6612 * either the HBA is in a state that it will never works on the outstanding
6613 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6614 * mailbox command has been completed.
6615 **/
6616static void
6617lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6618{
6619 LIST_HEAD(completions);
6620 struct lpfc_sli *psli = &phba->sli;
6621 LPFC_MBOXQ_t *pmb;
6622 unsigned long iflag;
6623
6624 /* Flush all the mailbox commands in the mbox system */
6625 spin_lock_irqsave(&phba->hbalock, iflag);
6626 /* The pending mailbox command queue */
6627 list_splice_init(&phba->sli.mboxq, &completions);
6628 /* The outstanding active mailbox command */
6629 if (psli->mbox_active) {
6630 list_add_tail(&psli->mbox_active->list, &completions);
6631 psli->mbox_active = NULL;
6632 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6633 }
6634 /* The completed mailbox command queue */
6635 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6636 spin_unlock_irqrestore(&phba->hbalock, iflag);
6637
6638 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6639 while (!list_empty(&completions)) {
6640 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6641 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6642 if (pmb->mbox_cmpl)
6643 pmb->mbox_cmpl(phba, pmb);
6644 }
6645}
6646
e59058c4 6647/**
3621a710 6648 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
6649 * @vport: Pointer to virtual port object.
6650 *
6651 * lpfc_sli_host_down is called to clean up the resources
6652 * associated with a vport before destroying virtual
6653 * port data structures.
6654 * This function does following operations:
6655 * - Free discovery resources associated with this virtual
6656 * port.
6657 * - Free iocbs associated with this virtual port in
6658 * the txq.
6659 * - Send abort for all iocb commands associated with this
6660 * vport in txcmplq.
6661 *
6662 * This function is called with no lock held and always returns 1.
6663 **/
92d7f7b0
JS
6664int
6665lpfc_sli_host_down(struct lpfc_vport *vport)
6666{
858c9f6c 6667 LIST_HEAD(completions);
92d7f7b0
JS
6668 struct lpfc_hba *phba = vport->phba;
6669 struct lpfc_sli *psli = &phba->sli;
6670 struct lpfc_sli_ring *pring;
6671 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
6672 int i;
6673 unsigned long flags = 0;
6674 uint16_t prev_pring_flag;
6675
6676 lpfc_cleanup_discovery_resources(vport);
6677
6678 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
6679 for (i = 0; i < psli->num_rings; i++) {
6680 pring = &psli->ring[i];
6681 prev_pring_flag = pring->flag;
5e9d9b82
JS
6682 /* Only slow rings */
6683 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6684 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6685 /* Set the lpfc data pending flag */
6686 set_bit(LPFC_DATA_READY, &phba->data_flags);
6687 }
92d7f7b0
JS
6688 /*
6689 * Error everything on the txq since these iocbs have not been
6690 * given to the FW yet.
6691 */
92d7f7b0
JS
6692 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6693 if (iocb->vport != vport)
6694 continue;
858c9f6c 6695 list_move_tail(&iocb->list, &completions);
92d7f7b0 6696 pring->txq_cnt--;
92d7f7b0
JS
6697 }
6698
6699 /* Next issue ABTS for everything on the txcmplq */
6700 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6701 list) {
6702 if (iocb->vport != vport)
6703 continue;
6704 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6705 }
6706
6707 pring->flag = prev_pring_flag;
6708 }
6709
6710 spin_unlock_irqrestore(&phba->hbalock, flags);
6711
a257bf90
JS
6712 /* Cancel all the IOCBs from the completions list */
6713 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6714 IOERR_SLI_DOWN);
92d7f7b0
JS
6715 return 1;
6716}
6717
e59058c4 6718/**
3621a710 6719 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
6720 * @phba: Pointer to HBA context object.
6721 *
6722 * This function cleans up all iocb, buffers, mailbox commands
6723 * while shutting down the HBA. This function is called with no
6724 * lock held and always returns 1.
6725 * This function does the following to cleanup driver resources:
6726 * - Free discovery resources for each virtual port
6727 * - Cleanup any pending fabric iocbs
6728 * - Iterate through the iocb txq and free each entry
6729 * in the list.
6730 * - Free up any buffer posted to the HBA
6731 * - Free mailbox commands in the mailbox queue.
6732 **/
dea3101e 6733int
2e0fef85 6734lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 6735{
2534ba75 6736 LIST_HEAD(completions);
2e0fef85 6737 struct lpfc_sli *psli = &phba->sli;
dea3101e 6738 struct lpfc_sli_ring *pring;
0ff10d46 6739 struct lpfc_dmabuf *buf_ptr;
dea3101e 6740 unsigned long flags = 0;
04c68496
JS
6741 int i;
6742
6743 /* Shutdown the mailbox command sub-system */
6744 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 6745
dea3101e 6746 lpfc_hba_down_prep(phba);
6747
92d7f7b0
JS
6748 lpfc_fabric_abort_hba(phba);
6749
2e0fef85 6750 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 6751 for (i = 0; i < psli->num_rings; i++) {
6752 pring = &psli->ring[i];
5e9d9b82
JS
6753 /* Only slow rings */
6754 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6755 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6756 /* Set the lpfc data pending flag */
6757 set_bit(LPFC_DATA_READY, &phba->data_flags);
6758 }
dea3101e 6759
6760 /*
6761 * Error everything on the txq since these iocbs have not been
6762 * given to the FW yet.
6763 */
2534ba75 6764 list_splice_init(&pring->txq, &completions);
dea3101e 6765 pring->txq_cnt = 0;
6766
2534ba75 6767 }
2e0fef85 6768 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 6769
a257bf90
JS
6770 /* Cancel all the IOCBs from the completions list */
6771 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6772 IOERR_SLI_DOWN);
dea3101e 6773
0ff10d46
JS
6774 spin_lock_irqsave(&phba->hbalock, flags);
6775 list_splice_init(&phba->elsbuf, &completions);
6776 phba->elsbuf_cnt = 0;
6777 phba->elsbuf_prev_cnt = 0;
6778 spin_unlock_irqrestore(&phba->hbalock, flags);
6779
6780 while (!list_empty(&completions)) {
6781 list_remove_head(&completions, buf_ptr,
6782 struct lpfc_dmabuf, list);
6783 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6784 kfree(buf_ptr);
6785 }
6786
dea3101e 6787 /* Return any active mbox cmds */
6788 del_timer_sync(&psli->mbox_tmo);
2e0fef85 6789
da0436e9 6790 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 6791 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 6792 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 6793
da0436e9
JS
6794 return 1;
6795}
6796
6797/**
6798 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6799 * @phba: Pointer to HBA context object.
6800 *
6801 * This function cleans up all queues, iocb, buffers, mailbox commands while
6802 * shutting down the SLI4 HBA FCoE function. This function is called with no
6803 * lock held and always returns 1.
6804 *
6805 * This function does the following to cleanup driver FCoE function resources:
6806 * - Free discovery resources for each virtual port
6807 * - Cleanup any pending fabric iocbs
6808 * - Iterate through the iocb txq and free each entry in the list.
6809 * - Free up any buffer posted to the HBA.
6810 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6811 * - Free mailbox commands in the mailbox queue.
6812 **/
6813int
6814lpfc_sli4_hba_down(struct lpfc_hba *phba)
6815{
6816 /* Stop the SLI4 device port */
6817 lpfc_stop_port(phba);
6818
6819 /* Tear down the queues in the HBA */
6820 lpfc_sli4_queue_unset(phba);
6821
6822 /* unregister default FCFI from the HBA */
6823 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
92d7f7b0 6824
dea3101e 6825 return 1;
6826}
6827
e59058c4 6828/**
3621a710 6829 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
6830 * @srcp: Source memory pointer.
6831 * @destp: Destination memory pointer.
6832 * @cnt: Number of words required to be copied.
6833 *
6834 * This function is used for copying data between driver memory
6835 * and the SLI memory. This function also changes the endianness
6836 * of each word if native endianness is different from SLI
6837 * endianness. This function can be called with or without
6838 * lock.
6839 **/
dea3101e 6840void
6841lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6842{
6843 uint32_t *src = srcp;
6844 uint32_t *dest = destp;
6845 uint32_t ldata;
6846 int i;
6847
6848 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6849 ldata = *src;
6850 ldata = le32_to_cpu(ldata);
6851 *dest = ldata;
6852 src++;
6853 dest++;
6854 }
6855}
6856
e59058c4 6857
a0c87cbd
JS
6858/**
6859 * lpfc_sli_bemem_bcopy - SLI memory copy function
6860 * @srcp: Source memory pointer.
6861 * @destp: Destination memory pointer.
6862 * @cnt: Number of words required to be copied.
6863 *
6864 * This function is used for copying data between a data structure
6865 * with big endian representation to local endianness.
6866 * This function can be called with or without lock.
6867 **/
6868void
6869lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6870{
6871 uint32_t *src = srcp;
6872 uint32_t *dest = destp;
6873 uint32_t ldata;
6874 int i;
6875
6876 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6877 ldata = *src;
6878 ldata = be32_to_cpu(ldata);
6879 *dest = ldata;
6880 src++;
6881 dest++;
6882 }
6883}
6884
e59058c4 6885/**
3621a710 6886 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
6887 * @phba: Pointer to HBA context object.
6888 * @pring: Pointer to driver SLI ring object.
6889 * @mp: Pointer to driver buffer object.
6890 *
6891 * This function is called with no lock held.
6892 * It always return zero after adding the buffer to the postbufq
6893 * buffer list.
6894 **/
dea3101e 6895int
2e0fef85
JS
6896lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6897 struct lpfc_dmabuf *mp)
dea3101e 6898{
6899 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6900 later */
2e0fef85 6901 spin_lock_irq(&phba->hbalock);
dea3101e 6902 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 6903 pring->postbufq_cnt++;
2e0fef85 6904 spin_unlock_irq(&phba->hbalock);
dea3101e 6905 return 0;
6906}
6907
e59058c4 6908/**
3621a710 6909 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
6910 * @phba: Pointer to HBA context object.
6911 *
6912 * When HBQ is enabled, buffers are searched based on tags. This function
6913 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6914 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6915 * does not conflict with tags of buffer posted for unsolicited events.
6916 * The function returns the allocated tag. The function is called with
6917 * no locks held.
6918 **/
76bb24ef
JS
6919uint32_t
6920lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6921{
6922 spin_lock_irq(&phba->hbalock);
6923 phba->buffer_tag_count++;
6924 /*
6925 * Always set the QUE_BUFTAG_BIT to distiguish between
6926 * a tag assigned by HBQ.
6927 */
6928 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6929 spin_unlock_irq(&phba->hbalock);
6930 return phba->buffer_tag_count;
6931}
6932
e59058c4 6933/**
3621a710 6934 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
6935 * @phba: Pointer to HBA context object.
6936 * @pring: Pointer to driver SLI ring object.
6937 * @tag: Buffer tag.
6938 *
6939 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6940 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6941 * iocb is posted to the response ring with the tag of the buffer.
6942 * This function searches the pring->postbufq list using the tag
6943 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6944 * iocb. If the buffer is found then lpfc_dmabuf object of the
6945 * buffer is returned to the caller else NULL is returned.
6946 * This function is called with no lock held.
6947 **/
76bb24ef
JS
6948struct lpfc_dmabuf *
6949lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6950 uint32_t tag)
6951{
6952 struct lpfc_dmabuf *mp, *next_mp;
6953 struct list_head *slp = &pring->postbufq;
6954
6955 /* Search postbufq, from the begining, looking for a match on tag */
6956 spin_lock_irq(&phba->hbalock);
6957 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6958 if (mp->buffer_tag == tag) {
6959 list_del_init(&mp->list);
6960 pring->postbufq_cnt--;
6961 spin_unlock_irq(&phba->hbalock);
6962 return mp;
6963 }
6964 }
6965
6966 spin_unlock_irq(&phba->hbalock);
6967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 6968 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
6969 "ring %d Data x%lx x%p x%p x%x\n",
6970 pring->ringno, (unsigned long) tag,
6971 slp->next, slp->prev, pring->postbufq_cnt);
6972
6973 return NULL;
6974}
dea3101e 6975
e59058c4 6976/**
3621a710 6977 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
6978 * @phba: Pointer to HBA context object.
6979 * @pring: Pointer to driver SLI ring object.
6980 * @phys: DMA address of the buffer.
6981 *
6982 * This function searches the buffer list using the dma_address
6983 * of unsolicited event to find the driver's lpfc_dmabuf object
6984 * corresponding to the dma_address. The function returns the
6985 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6986 * This function is called by the ct and els unsolicited event
6987 * handlers to get the buffer associated with the unsolicited
6988 * event.
6989 *
6990 * This function is called with no lock held.
6991 **/
dea3101e 6992struct lpfc_dmabuf *
6993lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6994 dma_addr_t phys)
6995{
6996 struct lpfc_dmabuf *mp, *next_mp;
6997 struct list_head *slp = &pring->postbufq;
6998
6999 /* Search postbufq, from the begining, looking for a match on phys */
2e0fef85 7000 spin_lock_irq(&phba->hbalock);
dea3101e 7001 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7002 if (mp->phys == phys) {
7003 list_del_init(&mp->list);
7004 pring->postbufq_cnt--;
2e0fef85 7005 spin_unlock_irq(&phba->hbalock);
dea3101e 7006 return mp;
7007 }
7008 }
7009
2e0fef85 7010 spin_unlock_irq(&phba->hbalock);
dea3101e 7011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 7012 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 7013 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 7014 pring->ringno, (unsigned long long)phys,
dea3101e 7015 slp->next, slp->prev, pring->postbufq_cnt);
7016 return NULL;
7017}
7018
e59058c4 7019/**
3621a710 7020 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
7021 * @phba: Pointer to HBA context object.
7022 * @cmdiocb: Pointer to driver command iocb object.
7023 * @rspiocb: Pointer to driver response iocb object.
7024 *
7025 * This function is the completion handler for the abort iocbs for
7026 * ELS commands. This function is called from the ELS ring event
7027 * handler with no lock held. This function frees memory resources
7028 * associated with the abort iocb.
7029 **/
dea3101e 7030static void
2e0fef85
JS
7031lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7032 struct lpfc_iocbq *rspiocb)
dea3101e 7033{
2e0fef85 7034 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 7035 uint16_t abort_iotag, abort_context;
92d7f7b0 7036 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
7037 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7038
7039 abort_iocb = NULL;
2680eeaa
JS
7040
7041 if (irsp->ulpStatus) {
7042 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7043 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7044
2e0fef85 7045 spin_lock_irq(&phba->hbalock);
45ed1190
JS
7046 if (phba->sli_rev < LPFC_SLI_REV4) {
7047 if (abort_iotag != 0 &&
7048 abort_iotag <= phba->sli.last_iotag)
7049 abort_iocb =
7050 phba->sli.iocbq_lookup[abort_iotag];
7051 } else
7052 /* For sli4 the abort_tag is the XRI,
7053 * so the abort routine puts the iotag of the iocb
7054 * being aborted in the context field of the abort
7055 * IOCB.
7056 */
7057 abort_iocb = phba->sli.iocbq_lookup[abort_context];
2680eeaa 7058
92d7f7b0 7059 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
e8b62011 7060 "0327 Cannot abort els iocb %p "
92d7f7b0
JS
7061 "with tag %x context %x, abort status %x, "
7062 "abort code %x\n",
e8b62011
JS
7063 abort_iocb, abort_iotag, abort_context,
7064 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa 7065
58da1ffb
JS
7066 /*
7067 * If the iocb is not found in Firmware queue the iocb
7068 * might have completed already. Do not free it again.
7069 */
9b379605 7070 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
45ed1190
JS
7071 if (irsp->un.ulpWord[4] != IOERR_NO_XRI) {
7072 spin_unlock_irq(&phba->hbalock);
7073 lpfc_sli_release_iocbq(phba, cmdiocb);
7074 return;
7075 }
7076 /* For SLI4 the ulpContext field for abort IOCB
7077 * holds the iotag of the IOCB being aborted so
7078 * the local abort_context needs to be reset to
7079 * match the aborted IOCBs ulpContext.
7080 */
7081 if (abort_iocb && phba->sli_rev == LPFC_SLI_REV4)
7082 abort_context = abort_iocb->iocb.ulpContext;
58da1ffb 7083 }
2680eeaa
JS
7084 /*
7085 * make sure we have the right iocbq before taking it
7086 * off the txcmplq and try to call completion routine.
7087 */
2e0fef85
JS
7088 if (!abort_iocb ||
7089 abort_iocb->iocb.ulpContext != abort_context ||
7090 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7091 spin_unlock_irq(&phba->hbalock);
341af102
JS
7092 else if (phba->sli_rev < LPFC_SLI_REV4) {
7093 /*
7094 * leave the SLI4 aborted command on the txcmplq
7095 * list and the command complete WCQE's XB bit
7096 * will tell whether the SGL (XRI) can be released
7097 * immediately or to the aborted SGL list for the
7098 * following abort XRI from the HBA.
7099 */
92d7f7b0 7100 list_del_init(&abort_iocb->list);
2680eeaa 7101 pring->txcmplq_cnt--;
2e0fef85 7102 spin_unlock_irq(&phba->hbalock);
2680eeaa 7103
0ff10d46
JS
7104 /* Firmware could still be in progress of DMAing
7105 * payload, so don't free data buffer till after
7106 * a hbeat.
7107 */
341af102 7108 spin_lock_irq(&phba->hbalock);
0ff10d46 7109 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
92d7f7b0 7110 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
341af102
JS
7111 spin_unlock_irq(&phba->hbalock);
7112
92d7f7b0 7113 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
341af102 7114 abort_iocb->iocb.un.ulpWord[4] = IOERR_ABORT_REQUESTED;
92d7f7b0 7115 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
2680eeaa
JS
7116 }
7117 }
7118
604a3e30 7119 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 7120 return;
7121}
7122
e59058c4 7123/**
3621a710 7124 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
7125 * @phba: Pointer to HBA context object.
7126 * @cmdiocb: Pointer to driver command iocb object.
7127 * @rspiocb: Pointer to driver response iocb object.
7128 *
7129 * The function is called from SLI ring event handler with no
7130 * lock held. This function is the completion handler for ELS commands
7131 * which are aborted. The function frees memory resources used for
7132 * the aborted ELS commands.
7133 **/
92d7f7b0
JS
7134static void
7135lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7136 struct lpfc_iocbq *rspiocb)
7137{
7138 IOCB_t *irsp = &rspiocb->iocb;
7139
7140 /* ELS cmd tag <ulpIoTag> completes */
7141 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 7142 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 7143 "x%x x%x x%x\n",
e8b62011 7144 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 7145 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
7146 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7147 lpfc_ct_free_iocb(phba, cmdiocb);
7148 else
7149 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
7150 return;
7151}
7152
e59058c4 7153/**
3621a710 7154 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
e59058c4
JS
7155 * @phba: Pointer to HBA context object.
7156 * @pring: Pointer to driver SLI ring object.
7157 * @cmdiocb: Pointer to driver command iocb object.
7158 *
7159 * This function issues an abort iocb for the provided command
7160 * iocb. This function is called with hbalock held.
7161 * The function returns 0 when it fails due to memory allocation
7162 * failure or when the command iocb is an abort request.
7163 **/
dea3101e 7164int
2e0fef85
JS
7165lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7166 struct lpfc_iocbq *cmdiocb)
dea3101e 7167{
2e0fef85 7168 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 7169 struct lpfc_iocbq *abtsiocbp;
dea3101e 7170 IOCB_t *icmd = NULL;
7171 IOCB_t *iabt = NULL;
07951076
JS
7172 int retval = IOCB_ERROR;
7173
92d7f7b0
JS
7174 /*
7175 * There are certain command types we don't want to abort. And we
7176 * don't want to abort commands that are already in the process of
7177 * being aborted.
07951076
JS
7178 */
7179 icmd = &cmdiocb->iocb;
2e0fef85 7180 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
7181 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7182 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
7183 return 0;
7184
858c9f6c
JS
7185 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7186 * callback so that nothing happens when it finishes.
07951076 7187 */
858c9f6c
JS
7188 if ((vport->load_flag & FC_UNLOADING) &&
7189 (pring->ringno == LPFC_ELS_RING)) {
92d7f7b0
JS
7190 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7191 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7192 else
7193 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
07951076 7194 goto abort_iotag_exit;
92d7f7b0 7195 }
dea3101e 7196
7197 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 7198 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 7199 if (abtsiocbp == NULL)
7200 return 0;
dea3101e 7201
07951076 7202 /* This signals the response to set the correct status
341af102 7203 * before calling the completion handler
07951076
JS
7204 */
7205 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7206
dea3101e 7207 iabt = &abtsiocbp->iocb;
07951076
JS
7208 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7209 iabt->un.acxri.abortContextTag = icmd->ulpContext;
45ed1190 7210 if (phba->sli_rev == LPFC_SLI_REV4) {
da0436e9 7211 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
45ed1190
JS
7212 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
7213 }
da0436e9
JS
7214 else
7215 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
7216 iabt->ulpLe = 1;
7217 iabt->ulpClass = icmd->ulpClass;
dea3101e 7218
5ffc266e
JS
7219 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7220 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
341af102
JS
7221 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
7222 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7223
2e0fef85 7224 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
7225 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7226 else
7227 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 7228
07951076 7229 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 7230
e8b62011
JS
7231 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7232 "0339 Abort xri x%x, original iotag x%x, "
7233 "abort cmd iotag x%x\n",
7234 iabt->un.acxri.abortContextTag,
7235 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
da0436e9 7236 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 7237
d7c255b2
JS
7238 if (retval)
7239 __lpfc_sli_release_iocbq(phba, abtsiocbp);
07951076 7240abort_iotag_exit:
2e0fef85
JS
7241 /*
7242 * Caller to this routine should check for IOCB_ERROR
7243 * and handle it properly. This routine no longer removes
7244 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 7245 */
2e0fef85 7246 return retval;
dea3101e 7247}
7248
e59058c4 7249/**
3621a710 7250 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
7251 * @iocbq: Pointer to driver iocb object.
7252 * @vport: Pointer to driver virtual port object.
7253 * @tgt_id: SCSI ID of the target.
7254 * @lun_id: LUN ID of the scsi device.
7255 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7256 *
3621a710 7257 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
7258 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7259 * 0 if the filtering criteria is met for the given iocb and will return
7260 * 1 if the filtering criteria is not met.
7261 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7262 * given iocb is for the SCSI device specified by vport, tgt_id and
7263 * lun_id parameter.
7264 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7265 * given iocb is for the SCSI target specified by vport and tgt_id
7266 * parameters.
7267 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7268 * given iocb is for the SCSI host associated with the given vport.
7269 * This function is called with no locks held.
7270 **/
dea3101e 7271static int
51ef4c26
JS
7272lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7273 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 7274 lpfc_ctx_cmd ctx_cmd)
dea3101e 7275{
0bd4ca25 7276 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7277 int rc = 1;
7278
0bd4ca25
JSEC
7279 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7280 return rc;
7281
51ef4c26
JS
7282 if (iocbq->vport != vport)
7283 return rc;
7284
0bd4ca25 7285 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 7286
495a714c 7287 if (lpfc_cmd->pCmd == NULL)
dea3101e 7288 return rc;
7289
7290 switch (ctx_cmd) {
7291 case LPFC_CTX_LUN:
495a714c
JS
7292 if ((lpfc_cmd->rdata->pnode) &&
7293 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7294 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 7295 rc = 0;
7296 break;
7297 case LPFC_CTX_TGT:
495a714c
JS
7298 if ((lpfc_cmd->rdata->pnode) &&
7299 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 7300 rc = 0;
7301 break;
dea3101e 7302 case LPFC_CTX_HOST:
7303 rc = 0;
7304 break;
7305 default:
7306 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 7307 __func__, ctx_cmd);
dea3101e 7308 break;
7309 }
7310
7311 return rc;
7312}
7313
e59058c4 7314/**
3621a710 7315 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
7316 * @vport: Pointer to virtual port.
7317 * @tgt_id: SCSI ID of the target.
7318 * @lun_id: LUN ID of the scsi device.
7319 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7320 *
7321 * This function returns number of FCP commands pending for the vport.
7322 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7323 * commands pending on the vport associated with SCSI device specified
7324 * by tgt_id and lun_id parameters.
7325 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7326 * commands pending on the vport associated with SCSI target specified
7327 * by tgt_id parameter.
7328 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7329 * commands pending on the vport.
7330 * This function returns the number of iocbs which satisfy the filter.
7331 * This function is called without any lock held.
7332 **/
dea3101e 7333int
51ef4c26
JS
7334lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7335 lpfc_ctx_cmd ctx_cmd)
dea3101e 7336{
51ef4c26 7337 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7338 struct lpfc_iocbq *iocbq;
7339 int sum, i;
dea3101e 7340
0bd4ca25
JSEC
7341 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7342 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7343
51ef4c26
JS
7344 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7345 ctx_cmd) == 0)
0bd4ca25 7346 sum++;
dea3101e 7347 }
0bd4ca25 7348
dea3101e 7349 return sum;
7350}
7351
e59058c4 7352/**
3621a710 7353 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
7354 * @phba: Pointer to HBA context object
7355 * @cmdiocb: Pointer to command iocb object.
7356 * @rspiocb: Pointer to response iocb object.
7357 *
7358 * This function is called when an aborted FCP iocb completes. This
7359 * function is called by the ring event handler with no lock held.
7360 * This function frees the iocb.
7361 **/
5eb95af0 7362void
2e0fef85
JS
7363lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7364 struct lpfc_iocbq *rspiocb)
5eb95af0 7365{
604a3e30 7366 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
7367 return;
7368}
7369
e59058c4 7370/**
3621a710 7371 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
7372 * @vport: Pointer to virtual port.
7373 * @pring: Pointer to driver SLI ring object.
7374 * @tgt_id: SCSI ID of the target.
7375 * @lun_id: LUN ID of the scsi device.
7376 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7377 *
7378 * This function sends an abort command for every SCSI command
7379 * associated with the given virtual port pending on the ring
7380 * filtered by lpfc_sli_validate_fcp_iocb function.
7381 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7382 * FCP iocbs associated with lun specified by tgt_id and lun_id
7383 * parameters
7384 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7385 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7386 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7387 * FCP iocbs associated with virtual port.
7388 * This function returns number of iocbs it failed to abort.
7389 * This function is called with no locks held.
7390 **/
dea3101e 7391int
51ef4c26
JS
7392lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7393 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 7394{
51ef4c26 7395 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7396 struct lpfc_iocbq *iocbq;
7397 struct lpfc_iocbq *abtsiocb;
dea3101e 7398 IOCB_t *cmd = NULL;
dea3101e 7399 int errcnt = 0, ret_val = 0;
0bd4ca25 7400 int i;
dea3101e 7401
0bd4ca25
JSEC
7402 for (i = 1; i <= phba->sli.last_iotag; i++) {
7403 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7404
51ef4c26 7405 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 7406 abort_cmd) != 0)
dea3101e 7407 continue;
7408
7409 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 7410 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 7411 if (abtsiocb == NULL) {
7412 errcnt++;
7413 continue;
7414 }
dea3101e 7415
0bd4ca25 7416 cmd = &iocbq->iocb;
dea3101e 7417 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7418 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
7419 if (phba->sli_rev == LPFC_SLI_REV4)
7420 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7421 else
7422 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 7423 abtsiocb->iocb.ulpLe = 1;
7424 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 7425 abtsiocb->vport = phba->pport;
dea3101e 7426
5ffc266e
JS
7427 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
7428 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
341af102
JS
7429 if (iocbq->iocb_flag & LPFC_IO_FCP)
7430 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
5ffc266e 7431
2e0fef85 7432 if (lpfc_is_link_up(phba))
dea3101e 7433 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7434 else
7435 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7436
5eb95af0
JSEC
7437 /* Setup callback routine and issue the command. */
7438 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
7439 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7440 abtsiocb, 0);
dea3101e 7441 if (ret_val == IOCB_ERROR) {
604a3e30 7442 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 7443 errcnt++;
7444 continue;
7445 }
7446 }
7447
7448 return errcnt;
7449}
7450
e59058c4 7451/**
3621a710 7452 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
7453 * @phba: Pointer to HBA context object.
7454 * @cmdiocbq: Pointer to command iocb.
7455 * @rspiocbq: Pointer to response iocb.
7456 *
7457 * This function is the completion handler for iocbs issued using
7458 * lpfc_sli_issue_iocb_wait function. This function is called by the
7459 * ring event handler function without any lock held. This function
7460 * can be called from both worker thread context and interrupt
7461 * context. This function also can be called from other thread which
7462 * cleans up the SLI layer objects.
7463 * This function copy the contents of the response iocb to the
7464 * response iocb memory object provided by the caller of
7465 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7466 * sleeps for the iocb completion.
7467 **/
68876920
JSEC
7468static void
7469lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7470 struct lpfc_iocbq *cmdiocbq,
7471 struct lpfc_iocbq *rspiocbq)
dea3101e 7472{
68876920
JSEC
7473 wait_queue_head_t *pdone_q;
7474 unsigned long iflags;
0f65ff68 7475 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7476
2e0fef85 7477 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
7478 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7479 if (cmdiocbq->context2 && rspiocbq)
7480 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7481 &rspiocbq->iocb, sizeof(IOCB_t));
7482
0f65ff68
JS
7483 /* Set the exchange busy flag for task management commands */
7484 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
7485 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
7486 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
7487 cur_iocbq);
7488 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
7489 }
7490
68876920 7491 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
7492 if (pdone_q)
7493 wake_up(pdone_q);
858c9f6c 7494 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 7495 return;
7496}
7497
d11e31dd
JS
7498/**
7499 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7500 * @phba: Pointer to HBA context object..
7501 * @piocbq: Pointer to command iocb.
7502 * @flag: Flag to test.
7503 *
7504 * This routine grabs the hbalock and then test the iocb_flag to
7505 * see if the passed in flag is set.
7506 * Returns:
7507 * 1 if flag is set.
7508 * 0 if flag is not set.
7509 **/
7510static int
7511lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7512 struct lpfc_iocbq *piocbq, uint32_t flag)
7513{
7514 unsigned long iflags;
7515 int ret;
7516
7517 spin_lock_irqsave(&phba->hbalock, iflags);
7518 ret = piocbq->iocb_flag & flag;
7519 spin_unlock_irqrestore(&phba->hbalock, iflags);
7520 return ret;
7521
7522}
7523
e59058c4 7524/**
3621a710 7525 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
7526 * @phba: Pointer to HBA context object..
7527 * @pring: Pointer to sli ring.
7528 * @piocb: Pointer to command iocb.
7529 * @prspiocbq: Pointer to response iocb.
7530 * @timeout: Timeout in number of seconds.
7531 *
7532 * This function issues the iocb to firmware and waits for the
7533 * iocb to complete. If the iocb command is not
7534 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7535 * Caller should not free the iocb resources if this function
7536 * returns IOCB_TIMEDOUT.
7537 * The function waits for the iocb completion using an
7538 * non-interruptible wait.
7539 * This function will sleep while waiting for iocb completion.
7540 * So, this function should not be called from any context which
7541 * does not allow sleeping. Due to the same reason, this function
7542 * cannot be called with interrupt disabled.
7543 * This function assumes that the iocb completions occur while
7544 * this function sleep. So, this function cannot be called from
7545 * the thread which process iocb completion for this ring.
7546 * This function clears the iocb_flag of the iocb object before
7547 * issuing the iocb and the iocb completion handler sets this
7548 * flag and wakes this thread when the iocb completes.
7549 * The contents of the response iocb will be copied to prspiocbq
7550 * by the completion handler when the command completes.
7551 * This function returns IOCB_SUCCESS when success.
7552 * This function is called with no lock held.
7553 **/
dea3101e 7554int
2e0fef85 7555lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 7556 uint32_t ring_number,
2e0fef85
JS
7557 struct lpfc_iocbq *piocb,
7558 struct lpfc_iocbq *prspiocbq,
68876920 7559 uint32_t timeout)
dea3101e 7560{
7259f0d0 7561 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
7562 long timeleft, timeout_req = 0;
7563 int retval = IOCB_SUCCESS;
875fbdfe 7564 uint32_t creg_val;
dea3101e 7565
7566 /*
68876920
JSEC
7567 * If the caller has provided a response iocbq buffer, then context2
7568 * is NULL or its an error.
dea3101e 7569 */
68876920
JSEC
7570 if (prspiocbq) {
7571 if (piocb->context2)
7572 return IOCB_ERROR;
7573 piocb->context2 = prspiocbq;
dea3101e 7574 }
7575
68876920
JSEC
7576 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7577 piocb->context_un.wait_queue = &done_q;
7578 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 7579
875fbdfe
JSEC
7580 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7581 creg_val = readl(phba->HCregaddr);
7582 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7583 writel(creg_val, phba->HCregaddr);
7584 readl(phba->HCregaddr); /* flush */
7585 }
7586
da0436e9 7587 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
68876920
JSEC
7588 if (retval == IOCB_SUCCESS) {
7589 timeout_req = timeout * HZ;
68876920 7590 timeleft = wait_event_timeout(done_q,
d11e31dd 7591 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 7592 timeout_req);
dea3101e 7593
7054a606
JS
7594 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7595 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 7596 "0331 IOCB wake signaled\n");
7054a606 7597 } else if (timeleft == 0) {
68876920 7598 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7599 "0338 IOCB wait timeout error - no "
7600 "wake response Data x%x\n", timeout);
68876920 7601 retval = IOCB_TIMEDOUT;
7054a606 7602 } else {
68876920 7603 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7604 "0330 IOCB wake NOT set, "
7605 "Data x%x x%lx\n",
68876920
JSEC
7606 timeout, (timeleft / jiffies));
7607 retval = IOCB_TIMEDOUT;
dea3101e 7608 }
68876920
JSEC
7609 } else {
7610 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 7611 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 7612 retval);
68876920 7613 retval = IOCB_ERROR;
dea3101e 7614 }
7615
875fbdfe
JSEC
7616 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7617 creg_val = readl(phba->HCregaddr);
7618 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7619 writel(creg_val, phba->HCregaddr);
7620 readl(phba->HCregaddr); /* flush */
7621 }
7622
68876920
JSEC
7623 if (prspiocbq)
7624 piocb->context2 = NULL;
7625
7626 piocb->context_un.wait_queue = NULL;
7627 piocb->iocb_cmpl = NULL;
dea3101e 7628 return retval;
7629}
68876920 7630
e59058c4 7631/**
3621a710 7632 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
7633 * @phba: Pointer to HBA context object.
7634 * @pmboxq: Pointer to driver mailbox object.
7635 * @timeout: Timeout in number of seconds.
7636 *
7637 * This function issues the mailbox to firmware and waits for the
7638 * mailbox command to complete. If the mailbox command is not
7639 * completed within timeout seconds, it returns MBX_TIMEOUT.
7640 * The function waits for the mailbox completion using an
7641 * interruptible wait. If the thread is woken up due to a
7642 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7643 * should not free the mailbox resources, if this function returns
7644 * MBX_TIMEOUT.
7645 * This function will sleep while waiting for mailbox completion.
7646 * So, this function should not be called from any context which
7647 * does not allow sleeping. Due to the same reason, this function
7648 * cannot be called with interrupt disabled.
7649 * This function assumes that the mailbox completion occurs while
7650 * this function sleep. So, this function cannot be called from
7651 * the worker thread which processes mailbox completion.
7652 * This function is called in the context of HBA management
7653 * applications.
7654 * This function returns MBX_SUCCESS when successful.
7655 * This function is called with no lock held.
7656 **/
dea3101e 7657int
2e0fef85 7658lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 7659 uint32_t timeout)
7660{
7259f0d0 7661 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 7662 int retval;
858c9f6c 7663 unsigned long flag;
dea3101e 7664
7665 /* The caller must leave context1 empty. */
98c9ea5c 7666 if (pmboxq->context1)
2e0fef85 7667 return MBX_NOT_FINISHED;
dea3101e 7668
495a714c 7669 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 7670 /* setup wake call as IOCB callback */
7671 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7672 /* setup context field to pass wait_queue pointer to wake function */
7673 pmboxq->context1 = &done_q;
7674
dea3101e 7675 /* now issue the command */
7676 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7677
7678 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
7679 wait_event_interruptible_timeout(done_q,
7680 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7681 timeout * HZ);
7682
858c9f6c 7683 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 7684 pmboxq->context1 = NULL;
7054a606
JS
7685 /*
7686 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7687 * else do not free the resources.
7688 */
7689 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea3101e 7690 retval = MBX_SUCCESS;
858c9f6c 7691 else {
7054a606 7692 retval = MBX_TIMEOUT;
858c9f6c
JS
7693 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7694 }
7695 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 7696 }
7697
dea3101e 7698 return retval;
7699}
7700
e59058c4 7701/**
3772a991 7702 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
7703 * @phba: Pointer to HBA context.
7704 *
3772a991
JS
7705 * This function is called to shutdown the driver's mailbox sub-system.
7706 * It first marks the mailbox sub-system is in a block state to prevent
7707 * the asynchronous mailbox command from issued off the pending mailbox
7708 * command queue. If the mailbox command sub-system shutdown is due to
7709 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7710 * the mailbox sub-system flush routine to forcefully bring down the
7711 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7712 * as with offline or HBA function reset), this routine will wait for the
7713 * outstanding mailbox command to complete before invoking the mailbox
7714 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 7715 **/
3772a991
JS
7716void
7717lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 7718{
3772a991
JS
7719 struct lpfc_sli *psli = &phba->sli;
7720 uint8_t actcmd = MBX_HEARTBEAT;
7721 unsigned long timeout;
b4c02652 7722
3772a991
JS
7723 spin_lock_irq(&phba->hbalock);
7724 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7725 spin_unlock_irq(&phba->hbalock);
b4c02652 7726
3772a991 7727 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 7728 spin_lock_irq(&phba->hbalock);
3772a991
JS
7729 if (phba->sli.mbox_active)
7730 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
ed957684 7731 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7732 /* Determine how long we might wait for the active mailbox
7733 * command to be gracefully completed by firmware.
7734 */
7735 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7736 1000) + jiffies;
7737 while (phba->sli.mbox_active) {
7738 /* Check active mailbox complete status every 2ms */
7739 msleep(2);
7740 if (time_after(jiffies, timeout))
7741 /* Timeout, let the mailbox flush routine to
7742 * forcefully release active mailbox command
7743 */
7744 break;
7745 }
7746 }
7747 lpfc_sli_mbox_sys_flush(phba);
7748}
ed957684 7749
3772a991
JS
7750/**
7751 * lpfc_sli_eratt_read - read sli-3 error attention events
7752 * @phba: Pointer to HBA context.
7753 *
7754 * This function is called to read the SLI3 device error attention registers
7755 * for possible error attention events. The caller must hold the hostlock
7756 * with spin_lock_irq().
7757 *
7758 * This fucntion returns 1 when there is Error Attention in the Host Attention
7759 * Register and returns 0 otherwise.
7760 **/
7761static int
7762lpfc_sli_eratt_read(struct lpfc_hba *phba)
7763{
7764 uint32_t ha_copy;
b4c02652 7765
3772a991
JS
7766 /* Read chip Host Attention (HA) register */
7767 ha_copy = readl(phba->HAregaddr);
7768 if (ha_copy & HA_ERATT) {
7769 /* Read host status register to retrieve error event */
7770 lpfc_sli_read_hs(phba);
b4c02652 7771
3772a991
JS
7772 /* Check if there is a deferred error condition is active */
7773 if ((HS_FFER1 & phba->work_hs) &&
7774 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7775 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
3772a991 7776 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
7777 /* Clear all interrupt enable conditions */
7778 writel(0, phba->HCregaddr);
7779 readl(phba->HCregaddr);
7780 }
7781
7782 /* Set the driver HA work bitmap */
3772a991
JS
7783 phba->work_ha |= HA_ERATT;
7784 /* Indicate polling handles this ERATT */
7785 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
7786 return 1;
7787 }
7788 return 0;
b4c02652
JS
7789}
7790
da0436e9
JS
7791/**
7792 * lpfc_sli4_eratt_read - read sli-4 error attention events
7793 * @phba: Pointer to HBA context.
7794 *
7795 * This function is called to read the SLI4 device error attention registers
7796 * for possible error attention events. The caller must hold the hostlock
7797 * with spin_lock_irq().
7798 *
7799 * This fucntion returns 1 when there is Error Attention in the Host Attention
7800 * Register and returns 0 otherwise.
7801 **/
7802static int
7803lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7804{
7805 uint32_t uerr_sta_hi, uerr_sta_lo;
da0436e9
JS
7806
7807 /* For now, use the SLI4 device internal unrecoverable error
7808 * registers for error attention. This can be changed later.
7809 */
a747c9ce
JS
7810 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7811 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7812 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
7813 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
7814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7815 "1423 HBA Unrecoverable error: "
7816 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7817 "ue_mask_lo_reg=0x%x, ue_mask_hi_reg=0x%x\n",
7818 uerr_sta_lo, uerr_sta_hi,
7819 phba->sli4_hba.ue_mask_lo,
7820 phba->sli4_hba.ue_mask_hi);
7821 phba->work_status[0] = uerr_sta_lo;
7822 phba->work_status[1] = uerr_sta_hi;
7823 /* Set the driver HA work bitmap */
7824 phba->work_ha |= HA_ERATT;
7825 /* Indicate polling handles this ERATT */
7826 phba->hba_flag |= HBA_ERATT_HANDLED;
7827 return 1;
da0436e9
JS
7828 }
7829 return 0;
7830}
7831
e59058c4 7832/**
3621a710 7833 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
7834 * @phba: Pointer to HBA context.
7835 *
3772a991 7836 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
7837 * error attention register bit for error attention events.
7838 *
7839 * This fucntion returns 1 when there is Error Attention in the Host Attention
7840 * Register and returns 0 otherwise.
7841 **/
7842int
7843lpfc_sli_check_eratt(struct lpfc_hba *phba)
7844{
7845 uint32_t ha_copy;
7846
7847 /* If somebody is waiting to handle an eratt, don't process it
7848 * here. The brdkill function will do this.
7849 */
7850 if (phba->link_flag & LS_IGNORE_ERATT)
7851 return 0;
7852
7853 /* Check if interrupt handler handles this ERATT */
7854 spin_lock_irq(&phba->hbalock);
7855 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7856 /* Interrupt handler has handled ERATT */
7857 spin_unlock_irq(&phba->hbalock);
7858 return 0;
7859 }
7860
a257bf90
JS
7861 /*
7862 * If there is deferred error attention, do not check for error
7863 * attention
7864 */
7865 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7866 spin_unlock_irq(&phba->hbalock);
7867 return 0;
7868 }
7869
3772a991
JS
7870 /* If PCI channel is offline, don't process it */
7871 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 7872 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7873 return 0;
7874 }
7875
7876 switch (phba->sli_rev) {
7877 case LPFC_SLI_REV2:
7878 case LPFC_SLI_REV3:
7879 /* Read chip Host Attention (HA) register */
7880 ha_copy = lpfc_sli_eratt_read(phba);
7881 break;
da0436e9
JS
7882 case LPFC_SLI_REV4:
7883 /* Read devcie Uncoverable Error (UERR) registers */
7884 ha_copy = lpfc_sli4_eratt_read(phba);
7885 break;
3772a991
JS
7886 default:
7887 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7888 "0299 Invalid SLI revision (%d)\n",
7889 phba->sli_rev);
7890 ha_copy = 0;
7891 break;
9399627f
JS
7892 }
7893 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7894
7895 return ha_copy;
7896}
7897
7898/**
7899 * lpfc_intr_state_check - Check device state for interrupt handling
7900 * @phba: Pointer to HBA context.
7901 *
7902 * This inline routine checks whether a device or its PCI slot is in a state
7903 * that the interrupt should be handled.
7904 *
7905 * This function returns 0 if the device or the PCI slot is in a state that
7906 * interrupt should be handled, otherwise -EIO.
7907 */
7908static inline int
7909lpfc_intr_state_check(struct lpfc_hba *phba)
7910{
7911 /* If the pci channel is offline, ignore all the interrupts */
7912 if (unlikely(pci_channel_offline(phba->pcidev)))
7913 return -EIO;
7914
7915 /* Update device level interrupt statistics */
7916 phba->sli.slistat.sli_intr++;
7917
7918 /* Ignore all interrupts during initialization. */
7919 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7920 return -EIO;
7921
9399627f
JS
7922 return 0;
7923}
7924
7925/**
3772a991 7926 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
7927 * @irq: Interrupt number.
7928 * @dev_id: The device context pointer.
7929 *
9399627f 7930 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
7931 * service routine when device with SLI-3 interface spec is enabled with
7932 * MSI-X multi-message interrupt mode and there are slow-path events in
7933 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7934 * interrupt mode, this function is called as part of the device-level
7935 * interrupt handler. When the PCI slot is in error recovery or the HBA
7936 * is undergoing initialization, the interrupt handler will not process
7937 * the interrupt. The link attention and ELS ring attention events are
7938 * handled by the worker thread. The interrupt handler signals the worker
7939 * thread and returns for these events. This function is called without
7940 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
7941 * structures.
7942 *
7943 * This function returns IRQ_HANDLED when interrupt is handled else it
7944 * returns IRQ_NONE.
e59058c4 7945 **/
dea3101e 7946irqreturn_t
3772a991 7947lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 7948{
2e0fef85 7949 struct lpfc_hba *phba;
a747c9ce 7950 uint32_t ha_copy, hc_copy;
dea3101e 7951 uint32_t work_ha_copy;
7952 unsigned long status;
5b75da2f 7953 unsigned long iflag;
dea3101e 7954 uint32_t control;
7955
92d7f7b0 7956 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
7957 struct lpfc_vport *vport;
7958 struct lpfc_nodelist *ndlp;
7959 struct lpfc_dmabuf *mp;
92d7f7b0
JS
7960 LPFC_MBOXQ_t *pmb;
7961 int rc;
7962
dea3101e 7963 /*
7964 * Get the driver's phba structure from the dev_id and
7965 * assume the HBA is not interrupting.
7966 */
9399627f 7967 phba = (struct lpfc_hba *)dev_id;
dea3101e 7968
7969 if (unlikely(!phba))
7970 return IRQ_NONE;
7971
dea3101e 7972 /*
9399627f
JS
7973 * Stuff needs to be attented to when this function is invoked as an
7974 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 7975 */
9399627f 7976 if (phba->intr_type == MSIX) {
3772a991
JS
7977 /* Check device state for handling interrupt */
7978 if (lpfc_intr_state_check(phba))
9399627f
JS
7979 return IRQ_NONE;
7980 /* Need to read HA REG for slow-path events */
5b75da2f 7981 spin_lock_irqsave(&phba->hbalock, iflag);
34b02dcd 7982 ha_copy = readl(phba->HAregaddr);
9399627f
JS
7983 /* If somebody is waiting to handle an eratt don't process it
7984 * here. The brdkill function will do this.
7985 */
7986 if (phba->link_flag & LS_IGNORE_ERATT)
7987 ha_copy &= ~HA_ERATT;
7988 /* Check the need for handling ERATT in interrupt handler */
7989 if (ha_copy & HA_ERATT) {
7990 if (phba->hba_flag & HBA_ERATT_HANDLED)
7991 /* ERATT polling has handled ERATT */
7992 ha_copy &= ~HA_ERATT;
7993 else
7994 /* Indicate interrupt handler handles ERATT */
7995 phba->hba_flag |= HBA_ERATT_HANDLED;
7996 }
a257bf90
JS
7997
7998 /*
7999 * If there is deferred error attention, do not check for any
8000 * interrupt.
8001 */
8002 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8003 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8004 return IRQ_NONE;
8005 }
8006
9399627f 8007 /* Clear up only attention source related to slow-path */
a747c9ce
JS
8008 hc_copy = readl(phba->HCregaddr);
8009 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
8010 HC_LAINT_ENA | HC_ERINT_ENA),
8011 phba->HCregaddr);
9399627f
JS
8012 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
8013 phba->HAregaddr);
a747c9ce 8014 writel(hc_copy, phba->HCregaddr);
9399627f 8015 readl(phba->HAregaddr); /* flush */
5b75da2f 8016 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8017 } else
8018 ha_copy = phba->ha_copy;
dea3101e 8019
dea3101e 8020 work_ha_copy = ha_copy & phba->work_ha_mask;
8021
9399627f 8022 if (work_ha_copy) {
dea3101e 8023 if (work_ha_copy & HA_LATT) {
8024 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
8025 /*
8026 * Turn off Link Attention interrupts
8027 * until CLEAR_LA done
8028 */
5b75da2f 8029 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8030 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
8031 control = readl(phba->HCregaddr);
8032 control &= ~HC_LAINT_ENA;
8033 writel(control, phba->HCregaddr);
8034 readl(phba->HCregaddr); /* flush */
5b75da2f 8035 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 8036 }
8037 else
8038 work_ha_copy &= ~HA_LATT;
8039 }
8040
9399627f 8041 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
8042 /*
8043 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
8044 * the only slow ring.
8045 */
8046 status = (work_ha_copy &
8047 (HA_RXMASK << (4*LPFC_ELS_RING)));
8048 status >>= (4*LPFC_ELS_RING);
8049 if (status & HA_RXMASK) {
5b75da2f 8050 spin_lock_irqsave(&phba->hbalock, iflag);
858c9f6c 8051 control = readl(phba->HCregaddr);
a58cbd52
JS
8052
8053 lpfc_debugfs_slow_ring_trc(phba,
8054 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8055 control, status,
8056 (uint32_t)phba->sli.slistat.sli_intr);
8057
858c9f6c 8058 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
8059 lpfc_debugfs_slow_ring_trc(phba,
8060 "ISR Disable ring:"
8061 "pwork:x%x hawork:x%x wait:x%x",
8062 phba->work_ha, work_ha_copy,
8063 (uint32_t)((unsigned long)
5e9d9b82 8064 &phba->work_waitq));
a58cbd52 8065
858c9f6c
JS
8066 control &=
8067 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 8068 writel(control, phba->HCregaddr);
8069 readl(phba->HCregaddr); /* flush */
dea3101e 8070 }
a58cbd52
JS
8071 else {
8072 lpfc_debugfs_slow_ring_trc(phba,
8073 "ISR slow ring: pwork:"
8074 "x%x hawork:x%x wait:x%x",
8075 phba->work_ha, work_ha_copy,
8076 (uint32_t)((unsigned long)
5e9d9b82 8077 &phba->work_waitq));
a58cbd52 8078 }
5b75da2f 8079 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 8080 }
8081 }
5b75da2f 8082 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 8083 if (work_ha_copy & HA_ERATT) {
9399627f 8084 lpfc_sli_read_hs(phba);
a257bf90
JS
8085 /*
8086 * Check if there is a deferred error condition
8087 * is active
8088 */
8089 if ((HS_FFER1 & phba->work_hs) &&
8090 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8091 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8092 phba->hba_flag |= DEFER_ERATT;
8093 /* Clear all interrupt enable conditions */
8094 writel(0, phba->HCregaddr);
8095 readl(phba->HCregaddr);
8096 }
8097 }
8098
9399627f 8099 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 8100 pmb = phba->sli.mbox_active;
04c68496 8101 pmbox = &pmb->u.mb;
34b02dcd 8102 mbox = phba->mbox;
858c9f6c 8103 vport = pmb->vport;
92d7f7b0
JS
8104
8105 /* First check out the status word */
8106 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8107 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 8108 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
8109 /*
8110 * Stray Mailbox Interrupt, mbxCommand <cmd>
8111 * mbxStatus <status>
8112 */
09372820 8113 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 8114 LOG_SLI,
e8b62011 8115 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
8116 "Interrupt mbxCommand x%x "
8117 "mbxStatus x%x\n",
e8b62011 8118 (vport ? vport->vpi : 0),
92d7f7b0
JS
8119 pmbox->mbxCommand,
8120 pmbox->mbxStatus);
09372820
JS
8121 /* clear mailbox attention bit */
8122 work_ha_copy &= ~HA_MBATT;
8123 } else {
97eab634 8124 phba->sli.mbox_active = NULL;
5b75da2f 8125 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
8126 phba->last_completion_time = jiffies;
8127 del_timer(&phba->sli.mbox_tmo);
09372820
JS
8128 if (pmb->mbox_cmpl) {
8129 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8130 MAILBOX_CMD_SIZE);
8131 }
8132 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8133 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8134
8135 lpfc_debugfs_disc_trc(vport,
8136 LPFC_DISC_TRC_MBOX_VPORT,
8137 "MBOX dflt rpi: : "
8138 "status:x%x rpi:x%x",
8139 (uint32_t)pmbox->mbxStatus,
8140 pmbox->un.varWords[0], 0);
8141
8142 if (!pmbox->mbxStatus) {
8143 mp = (struct lpfc_dmabuf *)
8144 (pmb->context1);
8145 ndlp = (struct lpfc_nodelist *)
8146 pmb->context2;
8147
8148 /* Reg_LOGIN of dflt RPI was
8149 * successful. new lets get
8150 * rid of the RPI using the
8151 * same mbox buffer.
8152 */
8153 lpfc_unreg_login(phba,
8154 vport->vpi,
8155 pmbox->un.varWords[0],
8156 pmb);
8157 pmb->mbox_cmpl =
8158 lpfc_mbx_cmpl_dflt_rpi;
8159 pmb->context1 = mp;
8160 pmb->context2 = ndlp;
8161 pmb->vport = vport;
58da1ffb
JS
8162 rc = lpfc_sli_issue_mbox(phba,
8163 pmb,
8164 MBX_NOWAIT);
8165 if (rc != MBX_BUSY)
8166 lpfc_printf_log(phba,
8167 KERN_ERR,
8168 LOG_MBOX | LOG_SLI,
d7c255b2 8169 "0350 rc should have"
6a9c52cf 8170 "been MBX_BUSY\n");
3772a991
JS
8171 if (rc != MBX_NOT_FINISHED)
8172 goto send_current_mbox;
09372820 8173 }
858c9f6c 8174 }
5b75da2f
JS
8175 spin_lock_irqsave(
8176 &phba->pport->work_port_lock,
8177 iflag);
09372820
JS
8178 phba->pport->work_port_events &=
8179 ~WORKER_MBOX_TMO;
5b75da2f
JS
8180 spin_unlock_irqrestore(
8181 &phba->pport->work_port_lock,
8182 iflag);
09372820 8183 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 8184 }
97eab634 8185 } else
5b75da2f 8186 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 8187
92d7f7b0
JS
8188 if ((work_ha_copy & HA_MBATT) &&
8189 (phba->sli.mbox_active == NULL)) {
858c9f6c 8190send_current_mbox:
92d7f7b0 8191 /* Process next mailbox command if there is one */
58da1ffb
JS
8192 do {
8193 rc = lpfc_sli_issue_mbox(phba, NULL,
8194 MBX_NOWAIT);
8195 } while (rc == MBX_NOT_FINISHED);
8196 if (rc != MBX_SUCCESS)
8197 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8198 LOG_SLI, "0349 rc should be "
6a9c52cf 8199 "MBX_SUCCESS\n");
92d7f7b0
JS
8200 }
8201
5b75da2f 8202 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8203 phba->work_ha |= work_ha_copy;
5b75da2f 8204 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 8205 lpfc_worker_wake_up(phba);
dea3101e 8206 }
9399627f 8207 return IRQ_HANDLED;
dea3101e 8208
3772a991 8209} /* lpfc_sli_sp_intr_handler */
9399627f
JS
8210
8211/**
3772a991 8212 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
8213 * @irq: Interrupt number.
8214 * @dev_id: The device context pointer.
8215 *
8216 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8217 * service routine when device with SLI-3 interface spec is enabled with
8218 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8219 * ring event in the HBA. However, when the device is enabled with either
8220 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8221 * device-level interrupt handler. When the PCI slot is in error recovery
8222 * or the HBA is undergoing initialization, the interrupt handler will not
8223 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8224 * the intrrupt context. This function is called without any lock held.
8225 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
8226 *
8227 * This function returns IRQ_HANDLED when interrupt is handled else it
8228 * returns IRQ_NONE.
8229 **/
8230irqreturn_t
3772a991 8231lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
8232{
8233 struct lpfc_hba *phba;
8234 uint32_t ha_copy;
8235 unsigned long status;
5b75da2f 8236 unsigned long iflag;
9399627f
JS
8237
8238 /* Get the driver's phba structure from the dev_id and
8239 * assume the HBA is not interrupting.
8240 */
8241 phba = (struct lpfc_hba *) dev_id;
8242
8243 if (unlikely(!phba))
8244 return IRQ_NONE;
8245
8246 /*
8247 * Stuff needs to be attented to when this function is invoked as an
8248 * individual interrupt handler in MSI-X multi-message interrupt mode
8249 */
8250 if (phba->intr_type == MSIX) {
3772a991
JS
8251 /* Check device state for handling interrupt */
8252 if (lpfc_intr_state_check(phba))
9399627f
JS
8253 return IRQ_NONE;
8254 /* Need to read HA REG for FCP ring and other ring events */
8255 ha_copy = readl(phba->HAregaddr);
8256 /* Clear up only attention source related to fast-path */
5b75da2f 8257 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
8258 /*
8259 * If there is deferred error attention, do not check for
8260 * any interrupt.
8261 */
8262 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8263 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8264 return IRQ_NONE;
8265 }
9399627f
JS
8266 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8267 phba->HAregaddr);
8268 readl(phba->HAregaddr); /* flush */
5b75da2f 8269 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8270 } else
8271 ha_copy = phba->ha_copy;
dea3101e 8272
8273 /*
9399627f 8274 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 8275 */
9399627f
JS
8276 ha_copy &= ~(phba->work_ha_mask);
8277
8278 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 8279 status >>= (4*LPFC_FCP_RING);
858c9f6c 8280 if (status & HA_RXMASK)
dea3101e 8281 lpfc_sli_handle_fast_ring_event(phba,
8282 &phba->sli.ring[LPFC_FCP_RING],
8283 status);
a4bc3379
JS
8284
8285 if (phba->cfg_multi_ring_support == 2) {
8286 /*
9399627f
JS
8287 * Process all events on extra ring. Take the optimized path
8288 * for extra ring IO.
a4bc3379 8289 */
9399627f 8290 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 8291 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 8292 if (status & HA_RXMASK) {
a4bc3379
JS
8293 lpfc_sli_handle_fast_ring_event(phba,
8294 &phba->sli.ring[LPFC_EXTRA_RING],
8295 status);
8296 }
8297 }
dea3101e 8298 return IRQ_HANDLED;
3772a991 8299} /* lpfc_sli_fp_intr_handler */
9399627f
JS
8300
8301/**
3772a991 8302 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
8303 * @irq: Interrupt number.
8304 * @dev_id: The device context pointer.
8305 *
3772a991
JS
8306 * This function is the HBA device-level interrupt handler to device with
8307 * SLI-3 interface spec, called from the PCI layer when either MSI or
8308 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8309 * requires driver attention. This function invokes the slow-path interrupt
8310 * attention handling function and fast-path interrupt attention handling
8311 * function in turn to process the relevant HBA attention events. This
8312 * function is called without any lock held. It gets the hbalock to access
8313 * and update SLI data structures.
9399627f
JS
8314 *
8315 * This function returns IRQ_HANDLED when interrupt is handled, else it
8316 * returns IRQ_NONE.
8317 **/
8318irqreturn_t
3772a991 8319lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
8320{
8321 struct lpfc_hba *phba;
8322 irqreturn_t sp_irq_rc, fp_irq_rc;
8323 unsigned long status1, status2;
a747c9ce 8324 uint32_t hc_copy;
9399627f
JS
8325
8326 /*
8327 * Get the driver's phba structure from the dev_id and
8328 * assume the HBA is not interrupting.
8329 */
8330 phba = (struct lpfc_hba *) dev_id;
8331
8332 if (unlikely(!phba))
8333 return IRQ_NONE;
8334
3772a991
JS
8335 /* Check device state for handling interrupt */
8336 if (lpfc_intr_state_check(phba))
9399627f
JS
8337 return IRQ_NONE;
8338
8339 spin_lock(&phba->hbalock);
8340 phba->ha_copy = readl(phba->HAregaddr);
8341 if (unlikely(!phba->ha_copy)) {
8342 spin_unlock(&phba->hbalock);
8343 return IRQ_NONE;
8344 } else if (phba->ha_copy & HA_ERATT) {
8345 if (phba->hba_flag & HBA_ERATT_HANDLED)
8346 /* ERATT polling has handled ERATT */
8347 phba->ha_copy &= ~HA_ERATT;
8348 else
8349 /* Indicate interrupt handler handles ERATT */
8350 phba->hba_flag |= HBA_ERATT_HANDLED;
8351 }
8352
a257bf90
JS
8353 /*
8354 * If there is deferred error attention, do not check for any interrupt.
8355 */
8356 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8357 spin_unlock_irq(&phba->hbalock);
8358 return IRQ_NONE;
8359 }
8360
9399627f 8361 /* Clear attention sources except link and error attentions */
a747c9ce
JS
8362 hc_copy = readl(phba->HCregaddr);
8363 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
8364 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
8365 phba->HCregaddr);
9399627f 8366 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
a747c9ce 8367 writel(hc_copy, phba->HCregaddr);
9399627f
JS
8368 readl(phba->HAregaddr); /* flush */
8369 spin_unlock(&phba->hbalock);
8370
8371 /*
8372 * Invokes slow-path host attention interrupt handling as appropriate.
8373 */
8374
8375 /* status of events with mailbox and link attention */
8376 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8377
8378 /* status of events with ELS ring */
8379 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8380 status2 >>= (4*LPFC_ELS_RING);
8381
8382 if (status1 || (status2 & HA_RXMASK))
3772a991 8383 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
8384 else
8385 sp_irq_rc = IRQ_NONE;
8386
8387 /*
8388 * Invoke fast-path host attention interrupt handling as appropriate.
8389 */
8390
8391 /* status of events with FCP ring */
8392 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8393 status1 >>= (4*LPFC_FCP_RING);
8394
8395 /* status of events with extra ring */
8396 if (phba->cfg_multi_ring_support == 2) {
8397 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8398 status2 >>= (4*LPFC_EXTRA_RING);
8399 } else
8400 status2 = 0;
8401
8402 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 8403 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
8404 else
8405 fp_irq_rc = IRQ_NONE;
dea3101e 8406
9399627f
JS
8407 /* Return device-level interrupt handling status */
8408 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 8409} /* lpfc_sli_intr_handler */
4f774513
JS
8410
8411/**
8412 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8413 * @phba: pointer to lpfc hba data structure.
8414 *
8415 * This routine is invoked by the worker thread to process all the pending
8416 * SLI4 FCP abort XRI events.
8417 **/
8418void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8419{
8420 struct lpfc_cq_event *cq_event;
8421
8422 /* First, declare the fcp xri abort event has been handled */
8423 spin_lock_irq(&phba->hbalock);
8424 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8425 spin_unlock_irq(&phba->hbalock);
8426 /* Now, handle all the fcp xri abort events */
8427 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8428 /* Get the first event from the head of the event queue */
8429 spin_lock_irq(&phba->hbalock);
8430 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8431 cq_event, struct lpfc_cq_event, list);
8432 spin_unlock_irq(&phba->hbalock);
8433 /* Notify aborted XRI for FCP work queue */
8434 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8435 /* Free the event processed back to the free pool */
8436 lpfc_sli4_cq_event_release(phba, cq_event);
8437 }
8438}
8439
8440/**
8441 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8442 * @phba: pointer to lpfc hba data structure.
8443 *
8444 * This routine is invoked by the worker thread to process all the pending
8445 * SLI4 els abort xri events.
8446 **/
8447void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8448{
8449 struct lpfc_cq_event *cq_event;
8450
8451 /* First, declare the els xri abort event has been handled */
8452 spin_lock_irq(&phba->hbalock);
8453 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8454 spin_unlock_irq(&phba->hbalock);
8455 /* Now, handle all the els xri abort events */
8456 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8457 /* Get the first event from the head of the event queue */
8458 spin_lock_irq(&phba->hbalock);
8459 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8460 cq_event, struct lpfc_cq_event, list);
8461 spin_unlock_irq(&phba->hbalock);
8462 /* Notify aborted XRI for ELS work queue */
8463 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8464 /* Free the event processed back to the free pool */
8465 lpfc_sli4_cq_event_release(phba, cq_event);
8466 }
8467}
8468
341af102
JS
8469/**
8470 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
8471 * @phba: pointer to lpfc hba data structure
8472 * @pIocbIn: pointer to the rspiocbq
8473 * @pIocbOut: pointer to the cmdiocbq
8474 * @wcqe: pointer to the complete wcqe
8475 *
8476 * This routine transfers the fields of a command iocbq to a response iocbq
8477 * by copying all the IOCB fields from command iocbq and transferring the
8478 * completion status information from the complete wcqe.
8479 **/
4f774513 8480static void
341af102
JS
8481lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
8482 struct lpfc_iocbq *pIocbIn,
4f774513
JS
8483 struct lpfc_iocbq *pIocbOut,
8484 struct lpfc_wcqe_complete *wcqe)
8485{
341af102 8486 unsigned long iflags;
4f774513
JS
8487 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8488
8489 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8490 sizeof(struct lpfc_iocbq) - offset);
4f774513
JS
8491 /* Map WCQE parameters into irspiocb parameters */
8492 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8493 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8494 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8495 pIocbIn->iocb.un.fcpi.fcpi_parm =
8496 pIocbOut->iocb.un.fcpi.fcpi_parm -
8497 wcqe->total_data_placed;
8498 else
8499 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e 8500 else {
4f774513 8501 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
695a814e
JS
8502 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
8503 }
341af102
JS
8504
8505 /* Pick up HBA exchange busy condition */
8506 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
8507 spin_lock_irqsave(&phba->hbalock, iflags);
8508 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
8509 spin_unlock_irqrestore(&phba->hbalock, iflags);
8510 }
4f774513
JS
8511}
8512
45ed1190
JS
8513/**
8514 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
8515 * @phba: Pointer to HBA context object.
8516 * @wcqe: Pointer to work-queue completion queue entry.
8517 *
8518 * This routine handles an ELS work-queue completion event and construct
8519 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
8520 * discovery engine to handle.
8521 *
8522 * Return: Pointer to the receive IOCBQ, NULL otherwise.
8523 **/
8524static struct lpfc_iocbq *
8525lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
8526 struct lpfc_iocbq *irspiocbq)
8527{
8528 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8529 struct lpfc_iocbq *cmdiocbq;
8530 struct lpfc_wcqe_complete *wcqe;
8531 unsigned long iflags;
8532
8533 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
8534 spin_lock_irqsave(&phba->hbalock, iflags);
8535 pring->stats.iocb_event++;
8536 /* Look up the ELS command IOCB and create pseudo response IOCB */
8537 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8538 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8539 spin_unlock_irqrestore(&phba->hbalock, iflags);
8540
8541 if (unlikely(!cmdiocbq)) {
8542 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8543 "0386 ELS complete with no corresponding "
8544 "cmdiocb: iotag (%d)\n",
8545 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8546 lpfc_sli_release_iocbq(phba, irspiocbq);
8547 return NULL;
8548 }
8549
8550 /* Fake the irspiocbq and copy necessary response information */
341af102 8551 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
45ed1190
JS
8552
8553 return irspiocbq;
8554}
8555
04c68496
JS
8556/**
8557 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8558 * @phba: Pointer to HBA context object.
8559 * @cqe: Pointer to mailbox completion queue entry.
8560 *
8561 * This routine process a mailbox completion queue entry with asynchrous
8562 * event.
8563 *
8564 * Return: true if work posted to worker thread, otherwise false.
8565 **/
8566static bool
8567lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8568{
8569 struct lpfc_cq_event *cq_event;
8570 unsigned long iflags;
8571
8572 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8573 "0392 Async Event: word0:x%x, word1:x%x, "
8574 "word2:x%x, word3:x%x\n", mcqe->word0,
8575 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8576
8577 /* Allocate a new internal CQ_EVENT entry */
8578 cq_event = lpfc_sli4_cq_event_alloc(phba);
8579 if (!cq_event) {
8580 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8581 "0394 Failed to allocate CQ_EVENT entry\n");
8582 return false;
8583 }
8584
8585 /* Move the CQE into an asynchronous event entry */
8586 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8587 spin_lock_irqsave(&phba->hbalock, iflags);
8588 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8589 /* Set the async event flag */
8590 phba->hba_flag |= ASYNC_EVENT;
8591 spin_unlock_irqrestore(&phba->hbalock, iflags);
8592
8593 return true;
8594}
8595
8596/**
8597 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8598 * @phba: Pointer to HBA context object.
8599 * @cqe: Pointer to mailbox completion queue entry.
8600 *
8601 * This routine process a mailbox completion queue entry with mailbox
8602 * completion event.
8603 *
8604 * Return: true if work posted to worker thread, otherwise false.
8605 **/
8606static bool
8607lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8608{
8609 uint32_t mcqe_status;
8610 MAILBOX_t *mbox, *pmbox;
8611 struct lpfc_mqe *mqe;
8612 struct lpfc_vport *vport;
8613 struct lpfc_nodelist *ndlp;
8614 struct lpfc_dmabuf *mp;
8615 unsigned long iflags;
8616 LPFC_MBOXQ_t *pmb;
8617 bool workposted = false;
8618 int rc;
8619
8620 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8621 if (!bf_get(lpfc_trailer_completed, mcqe))
8622 goto out_no_mqe_complete;
8623
8624 /* Get the reference to the active mbox command */
8625 spin_lock_irqsave(&phba->hbalock, iflags);
8626 pmb = phba->sli.mbox_active;
8627 if (unlikely(!pmb)) {
8628 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8629 "1832 No pending MBOX command to handle\n");
8630 spin_unlock_irqrestore(&phba->hbalock, iflags);
8631 goto out_no_mqe_complete;
8632 }
8633 spin_unlock_irqrestore(&phba->hbalock, iflags);
8634 mqe = &pmb->u.mqe;
8635 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8636 mbox = phba->mbox;
8637 vport = pmb->vport;
8638
8639 /* Reset heartbeat timer */
8640 phba->last_completion_time = jiffies;
8641 del_timer(&phba->sli.mbox_tmo);
8642
8643 /* Move mbox data to caller's mailbox region, do endian swapping */
8644 if (pmb->mbox_cmpl && mbox)
8645 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8646 /* Set the mailbox status with SLI4 range 0x4000 */
8647 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8648 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8649 bf_set(lpfc_mqe_status, mqe,
8650 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8651
8652 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8653 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8654 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8655 "MBOX dflt rpi: status:x%x rpi:x%x",
8656 mcqe_status,
8657 pmbox->un.varWords[0], 0);
8658 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8659 mp = (struct lpfc_dmabuf *)(pmb->context1);
8660 ndlp = (struct lpfc_nodelist *)pmb->context2;
8661 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8662 * RID of the PPI using the same mbox buffer.
8663 */
8664 lpfc_unreg_login(phba, vport->vpi,
8665 pmbox->un.varWords[0], pmb);
8666 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8667 pmb->context1 = mp;
8668 pmb->context2 = ndlp;
8669 pmb->vport = vport;
8670 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8671 if (rc != MBX_BUSY)
8672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8673 LOG_SLI, "0385 rc should "
8674 "have been MBX_BUSY\n");
8675 if (rc != MBX_NOT_FINISHED)
8676 goto send_current_mbox;
8677 }
8678 }
8679 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8680 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8681 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8682
8683 /* There is mailbox completion work to do */
8684 spin_lock_irqsave(&phba->hbalock, iflags);
8685 __lpfc_mbox_cmpl_put(phba, pmb);
8686 phba->work_ha |= HA_MBATT;
8687 spin_unlock_irqrestore(&phba->hbalock, iflags);
8688 workposted = true;
8689
8690send_current_mbox:
8691 spin_lock_irqsave(&phba->hbalock, iflags);
8692 /* Release the mailbox command posting token */
8693 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8694 /* Setting active mailbox pointer need to be in sync to flag clear */
8695 phba->sli.mbox_active = NULL;
8696 spin_unlock_irqrestore(&phba->hbalock, iflags);
8697 /* Wake up worker thread to post the next pending mailbox command */
8698 lpfc_worker_wake_up(phba);
8699out_no_mqe_complete:
8700 if (bf_get(lpfc_trailer_consumed, mcqe))
8701 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8702 return workposted;
8703}
8704
8705/**
8706 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8707 * @phba: Pointer to HBA context object.
8708 * @cqe: Pointer to mailbox completion queue entry.
8709 *
8710 * This routine process a mailbox completion queue entry, it invokes the
8711 * proper mailbox complete handling or asynchrous event handling routine
8712 * according to the MCQE's async bit.
8713 *
8714 * Return: true if work posted to worker thread, otherwise false.
8715 **/
8716static bool
8717lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8718{
8719 struct lpfc_mcqe mcqe;
8720 bool workposted;
8721
8722 /* Copy the mailbox MCQE and convert endian order as needed */
8723 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8724
8725 /* Invoke the proper event handling routine */
8726 if (!bf_get(lpfc_trailer_async, &mcqe))
8727 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8728 else
8729 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8730 return workposted;
8731}
8732
4f774513
JS
8733/**
8734 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8735 * @phba: Pointer to HBA context object.
8736 * @wcqe: Pointer to work-queue completion queue entry.
8737 *
8738 * This routine handles an ELS work-queue completion event.
8739 *
8740 * Return: true if work posted to worker thread, otherwise false.
8741 **/
8742static bool
8743lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8744 struct lpfc_wcqe_complete *wcqe)
8745{
4f774513
JS
8746 struct lpfc_iocbq *irspiocbq;
8747 unsigned long iflags;
4f774513 8748
45ed1190 8749 /* Get an irspiocbq for later ELS response processing use */
4f774513
JS
8750 irspiocbq = lpfc_sli_get_iocbq(phba);
8751 if (!irspiocbq) {
8752 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8753 "0387 Failed to allocate an iocbq\n");
45ed1190 8754 return false;
4f774513 8755 }
4f774513 8756
45ed1190
JS
8757 /* Save off the slow-path queue event for work thread to process */
8758 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
4f774513 8759 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994 8760 list_add_tail(&irspiocbq->cq_event.list,
45ed1190
JS
8761 &phba->sli4_hba.sp_queue_event);
8762 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513 8763 spin_unlock_irqrestore(&phba->hbalock, iflags);
4f774513 8764
45ed1190 8765 return true;
4f774513
JS
8766}
8767
8768/**
8769 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8770 * @phba: Pointer to HBA context object.
8771 * @wcqe: Pointer to work-queue completion queue entry.
8772 *
8773 * This routine handles slow-path WQ entry comsumed event by invoking the
8774 * proper WQ release routine to the slow-path WQ.
8775 **/
8776static void
8777lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8778 struct lpfc_wcqe_release *wcqe)
8779{
8780 /* Check for the slow-path ELS work queue */
8781 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8782 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8783 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8784 else
8785 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8786 "2579 Slow-path wqe consume event carries "
8787 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8788 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8789 phba->sli4_hba.els_wq->queue_id);
8790}
8791
8792/**
8793 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8794 * @phba: Pointer to HBA context object.
8795 * @cq: Pointer to a WQ completion queue.
8796 * @wcqe: Pointer to work-queue completion queue entry.
8797 *
8798 * This routine handles an XRI abort event.
8799 *
8800 * Return: true if work posted to worker thread, otherwise false.
8801 **/
8802static bool
8803lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8804 struct lpfc_queue *cq,
8805 struct sli4_wcqe_xri_aborted *wcqe)
8806{
8807 bool workposted = false;
8808 struct lpfc_cq_event *cq_event;
8809 unsigned long iflags;
8810
8811 /* Allocate a new internal CQ_EVENT entry */
8812 cq_event = lpfc_sli4_cq_event_alloc(phba);
8813 if (!cq_event) {
8814 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8815 "0602 Failed to allocate CQ_EVENT entry\n");
8816 return false;
8817 }
8818
8819 /* Move the CQE into the proper xri abort event list */
8820 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8821 switch (cq->subtype) {
8822 case LPFC_FCP:
8823 spin_lock_irqsave(&phba->hbalock, iflags);
8824 list_add_tail(&cq_event->list,
8825 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8826 /* Set the fcp xri abort event flag */
8827 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8828 spin_unlock_irqrestore(&phba->hbalock, iflags);
8829 workposted = true;
8830 break;
8831 case LPFC_ELS:
8832 spin_lock_irqsave(&phba->hbalock, iflags);
8833 list_add_tail(&cq_event->list,
8834 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8835 /* Set the els xri abort event flag */
8836 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8837 spin_unlock_irqrestore(&phba->hbalock, iflags);
8838 workposted = true;
8839 break;
8840 default:
8841 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8842 "0603 Invalid work queue CQE subtype (x%x)\n",
8843 cq->subtype);
8844 workposted = false;
8845 break;
8846 }
8847 return workposted;
8848}
8849
4f774513
JS
8850/**
8851 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8852 * @phba: Pointer to HBA context object.
8853 * @rcqe: Pointer to receive-queue completion queue entry.
8854 *
8855 * This routine process a receive-queue completion queue entry.
8856 *
8857 * Return: true if work posted to worker thread, otherwise false.
8858 **/
8859static bool
4d9ab994 8860lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 8861{
4f774513
JS
8862 bool workposted = false;
8863 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8864 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8865 struct hbq_dmabuf *dma_buf;
8866 uint32_t status;
8867 unsigned long iflags;
8868
4d9ab994 8869 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
4f774513
JS
8870 goto out;
8871
4d9ab994 8872 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
8873 switch (status) {
8874 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8875 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8876 "2537 Receive Frame Truncated!!\n");
8877 case FC_STATUS_RQ_SUCCESS:
5ffc266e 8878 lpfc_sli4_rq_release(hrq, drq);
4f774513
JS
8879 spin_lock_irqsave(&phba->hbalock, iflags);
8880 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8881 if (!dma_buf) {
8882 spin_unlock_irqrestore(&phba->hbalock, iflags);
8883 goto out;
8884 }
4d9ab994 8885 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 8886 /* save off the frame for the word thread to process */
4d9ab994 8887 list_add_tail(&dma_buf->cq_event.list,
45ed1190 8888 &phba->sli4_hba.sp_queue_event);
4f774513 8889 /* Frame received */
45ed1190 8890 phba->hba_flag |= HBA_SP_QUEUE_EVT;
4f774513
JS
8891 spin_unlock_irqrestore(&phba->hbalock, iflags);
8892 workposted = true;
8893 break;
8894 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8895 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8896 /* Post more buffers if possible */
8897 spin_lock_irqsave(&phba->hbalock, iflags);
8898 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8899 spin_unlock_irqrestore(&phba->hbalock, iflags);
8900 workposted = true;
8901 break;
8902 }
8903out:
8904 return workposted;
4f774513
JS
8905}
8906
4d9ab994
JS
8907/**
8908 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8909 * @phba: Pointer to HBA context object.
8910 * @cq: Pointer to the completion queue.
8911 * @wcqe: Pointer to a completion queue entry.
8912 *
8913 * This routine process a slow-path work-queue or recieve queue completion queue
8914 * entry.
8915 *
8916 * Return: true if work posted to worker thread, otherwise false.
8917 **/
8918static bool
8919lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8920 struct lpfc_cqe *cqe)
8921{
45ed1190 8922 struct lpfc_cqe cqevt;
4d9ab994
JS
8923 bool workposted = false;
8924
8925 /* Copy the work queue CQE and convert endian order if needed */
45ed1190 8926 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
4d9ab994
JS
8927
8928 /* Check and process for different type of WCQE and dispatch */
45ed1190 8929 switch (bf_get(lpfc_cqe_code, &cqevt)) {
4d9ab994 8930 case CQE_CODE_COMPL_WQE:
45ed1190 8931 /* Process the WQ/RQ complete event */
4d9ab994 8932 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
45ed1190 8933 (struct lpfc_wcqe_complete *)&cqevt);
4d9ab994
JS
8934 break;
8935 case CQE_CODE_RELEASE_WQE:
8936 /* Process the WQ release event */
8937 lpfc_sli4_sp_handle_rel_wcqe(phba,
45ed1190 8938 (struct lpfc_wcqe_release *)&cqevt);
4d9ab994
JS
8939 break;
8940 case CQE_CODE_XRI_ABORTED:
8941 /* Process the WQ XRI abort event */
8942 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
45ed1190 8943 (struct sli4_wcqe_xri_aborted *)&cqevt);
4d9ab994
JS
8944 break;
8945 case CQE_CODE_RECEIVE:
8946 /* Process the RQ event */
8947 workposted = lpfc_sli4_sp_handle_rcqe(phba,
45ed1190 8948 (struct lpfc_rcqe *)&cqevt);
4d9ab994
JS
8949 break;
8950 default:
8951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8952 "0388 Not a valid WCQE code: x%x\n",
45ed1190 8953 bf_get(lpfc_cqe_code, &cqevt));
4d9ab994
JS
8954 break;
8955 }
8956 return workposted;
8957}
8958
4f774513
JS
8959/**
8960 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8961 * @phba: Pointer to HBA context object.
8962 * @eqe: Pointer to fast-path event queue entry.
8963 *
8964 * This routine process a event queue entry from the slow-path event queue.
8965 * It will check the MajorCode and MinorCode to determine this is for a
8966 * completion event on a completion queue, if not, an error shall be logged
8967 * and just return. Otherwise, it will get to the corresponding completion
8968 * queue and process all the entries on that completion queue, rearm the
8969 * completion queue, and then return.
8970 *
8971 **/
8972static void
8973lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8974{
8975 struct lpfc_queue *cq = NULL, *childq, *speq;
8976 struct lpfc_cqe *cqe;
8977 bool workposted = false;
8978 int ecount = 0;
8979 uint16_t cqid;
8980
28baac74 8981 if (bf_get(lpfc_eqe_major_code, eqe) != 0) {
4f774513
JS
8982 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8983 "0359 Not a valid slow-path completion "
8984 "event: majorcode=x%x, minorcode=x%x\n",
8985 bf_get(lpfc_eqe_major_code, eqe),
8986 bf_get(lpfc_eqe_minor_code, eqe));
8987 return;
8988 }
8989
8990 /* Get the reference to the corresponding CQ */
8991 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8992
8993 /* Search for completion queue pointer matching this cqid */
8994 speq = phba->sli4_hba.sp_eq;
8995 list_for_each_entry(childq, &speq->child_list, list) {
8996 if (childq->queue_id == cqid) {
8997 cq = childq;
8998 break;
8999 }
9000 }
9001 if (unlikely(!cq)) {
9002 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9003 "0365 Slow-path CQ identifier (%d) does "
9004 "not exist\n", cqid);
9005 return;
9006 }
9007
9008 /* Process all the entries to the CQ */
9009 switch (cq->type) {
9010 case LPFC_MCQ:
9011 while ((cqe = lpfc_sli4_cq_get(cq))) {
9012 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
9013 if (!(++ecount % LPFC_GET_QE_REL_INT))
9014 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9015 }
9016 break;
9017 case LPFC_WCQ:
9018 while ((cqe = lpfc_sli4_cq_get(cq))) {
4d9ab994 9019 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
4f774513
JS
9020 if (!(++ecount % LPFC_GET_QE_REL_INT))
9021 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9022 }
9023 break;
9024 default:
9025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9026 "0370 Invalid completion queue type (%d)\n",
9027 cq->type);
9028 return;
9029 }
9030
9031 /* Catch the no cq entry condition, log an error */
9032 if (unlikely(ecount == 0))
9033 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9034 "0371 No entry from the CQ: identifier "
9035 "(x%x), type (%d)\n", cq->queue_id, cq->type);
9036
9037 /* In any case, flash and re-arm the RCQ */
9038 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9039
9040 /* wake up worker thread if there are works to be done */
9041 if (workposted)
9042 lpfc_worker_wake_up(phba);
9043}
9044
9045/**
9046 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
9047 * @eqe: Pointer to fast-path completion queue entry.
9048 *
9049 * This routine process a fast-path work queue completion entry from fast-path
9050 * event queue for FCP command response completion.
9051 **/
9052static void
9053lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
9054 struct lpfc_wcqe_complete *wcqe)
9055{
9056 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
9057 struct lpfc_iocbq *cmdiocbq;
9058 struct lpfc_iocbq irspiocbq;
9059 unsigned long iflags;
9060
9061 spin_lock_irqsave(&phba->hbalock, iflags);
9062 pring->stats.iocb_event++;
9063 spin_unlock_irqrestore(&phba->hbalock, iflags);
9064
9065 /* Check for response status */
9066 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
9067 /* If resource errors reported from HBA, reduce queue
9068 * depth of the SCSI device.
9069 */
9070 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
9071 IOSTAT_LOCAL_REJECT) &&
9072 (wcqe->parameter == IOERR_NO_RESOURCES)) {
9073 phba->lpfc_rampdown_queue_depth(phba);
9074 }
9075 /* Log the error status */
9076 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9077 "0373 FCP complete error: status=x%x, "
9078 "hw_status=x%x, total_data_specified=%d, "
9079 "parameter=x%x, word3=x%x\n",
9080 bf_get(lpfc_wcqe_c_status, wcqe),
9081 bf_get(lpfc_wcqe_c_hw_status, wcqe),
9082 wcqe->total_data_placed, wcqe->parameter,
9083 wcqe->word3);
9084 }
9085
9086 /* Look up the FCP command IOCB and create pseudo response IOCB */
9087 spin_lock_irqsave(&phba->hbalock, iflags);
9088 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
9089 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9090 spin_unlock_irqrestore(&phba->hbalock, iflags);
9091 if (unlikely(!cmdiocbq)) {
9092 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9093 "0374 FCP complete with no corresponding "
9094 "cmdiocb: iotag (%d)\n",
9095 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9096 return;
9097 }
9098 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9099 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9100 "0375 FCP cmdiocb not callback function "
9101 "iotag: (%d)\n",
9102 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9103 return;
9104 }
9105
9106 /* Fake the irspiocb and copy necessary response information */
341af102 9107 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
4f774513 9108
0f65ff68
JS
9109 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
9110 spin_lock_irqsave(&phba->hbalock, iflags);
9111 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
9112 spin_unlock_irqrestore(&phba->hbalock, iflags);
9113 }
9114
4f774513
JS
9115 /* Pass the cmd_iocb and the rsp state to the upper layer */
9116 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9117}
9118
9119/**
9120 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9121 * @phba: Pointer to HBA context object.
9122 * @cq: Pointer to completion queue.
9123 * @wcqe: Pointer to work-queue completion queue entry.
9124 *
9125 * This routine handles an fast-path WQ entry comsumed event by invoking the
9126 * proper WQ release routine to the slow-path WQ.
9127 **/
9128static void
9129lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9130 struct lpfc_wcqe_release *wcqe)
9131{
9132 struct lpfc_queue *childwq;
9133 bool wqid_matched = false;
9134 uint16_t fcp_wqid;
9135
9136 /* Check for fast-path FCP work queue release */
9137 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9138 list_for_each_entry(childwq, &cq->child_list, list) {
9139 if (childwq->queue_id == fcp_wqid) {
9140 lpfc_sli4_wq_release(childwq,
9141 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9142 wqid_matched = true;
9143 break;
9144 }
9145 }
9146 /* Report warning log message if no match found */
9147 if (wqid_matched != true)
9148 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9149 "2580 Fast-path wqe consume event carries "
9150 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9151}
9152
9153/**
9154 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9155 * @cq: Pointer to the completion queue.
9156 * @eqe: Pointer to fast-path completion queue entry.
9157 *
9158 * This routine process a fast-path work queue completion entry from fast-path
9159 * event queue for FCP command response completion.
9160 **/
9161static int
9162lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9163 struct lpfc_cqe *cqe)
9164{
9165 struct lpfc_wcqe_release wcqe;
9166 bool workposted = false;
9167
9168 /* Copy the work queue CQE and convert endian order if needed */
9169 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9170
9171 /* Check and process for different type of WCQE and dispatch */
9172 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9173 case CQE_CODE_COMPL_WQE:
9174 /* Process the WQ complete event */
9175 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9176 (struct lpfc_wcqe_complete *)&wcqe);
9177 break;
9178 case CQE_CODE_RELEASE_WQE:
9179 /* Process the WQ release event */
9180 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9181 (struct lpfc_wcqe_release *)&wcqe);
9182 break;
9183 case CQE_CODE_XRI_ABORTED:
9184 /* Process the WQ XRI abort event */
9185 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9186 (struct sli4_wcqe_xri_aborted *)&wcqe);
9187 break;
9188 default:
9189 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9190 "0144 Not a valid WCQE code: x%x\n",
9191 bf_get(lpfc_wcqe_c_code, &wcqe));
9192 break;
9193 }
9194 return workposted;
9195}
9196
9197/**
9198 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9199 * @phba: Pointer to HBA context object.
9200 * @eqe: Pointer to fast-path event queue entry.
9201 *
9202 * This routine process a event queue entry from the fast-path event queue.
9203 * It will check the MajorCode and MinorCode to determine this is for a
9204 * completion event on a completion queue, if not, an error shall be logged
9205 * and just return. Otherwise, it will get to the corresponding completion
9206 * queue and process all the entries on the completion queue, rearm the
9207 * completion queue, and then return.
9208 **/
9209static void
9210lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9211 uint32_t fcp_cqidx)
9212{
9213 struct lpfc_queue *cq;
9214 struct lpfc_cqe *cqe;
9215 bool workposted = false;
9216 uint16_t cqid;
9217 int ecount = 0;
9218
28baac74 9219 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0)) {
4f774513
JS
9220 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9221 "0366 Not a valid fast-path completion "
9222 "event: majorcode=x%x, minorcode=x%x\n",
9223 bf_get(lpfc_eqe_major_code, eqe),
9224 bf_get(lpfc_eqe_minor_code, eqe));
9225 return;
9226 }
9227
9228 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9229 if (unlikely(!cq)) {
9230 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9231 "0367 Fast-path completion queue does not "
9232 "exist\n");
9233 return;
9234 }
9235
9236 /* Get the reference to the corresponding CQ */
9237 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9238 if (unlikely(cqid != cq->queue_id)) {
9239 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9240 "0368 Miss-matched fast-path completion "
9241 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9242 cqid, cq->queue_id);
9243 return;
9244 }
9245
9246 /* Process all the entries to the CQ */
9247 while ((cqe = lpfc_sli4_cq_get(cq))) {
9248 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9249 if (!(++ecount % LPFC_GET_QE_REL_INT))
9250 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9251 }
9252
9253 /* Catch the no cq entry condition */
9254 if (unlikely(ecount == 0))
9255 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9256 "0369 No entry from fast-path completion "
9257 "queue fcpcqid=%d\n", cq->queue_id);
9258
9259 /* In any case, flash and re-arm the CQ */
9260 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9261
9262 /* wake up worker thread if there are works to be done */
9263 if (workposted)
9264 lpfc_worker_wake_up(phba);
9265}
9266
9267static void
9268lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9269{
9270 struct lpfc_eqe *eqe;
9271
9272 /* walk all the EQ entries and drop on the floor */
9273 while ((eqe = lpfc_sli4_eq_get(eq)))
9274 ;
9275
9276 /* Clear and re-arm the EQ */
9277 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9278}
9279
9280/**
9281 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9282 * @irq: Interrupt number.
9283 * @dev_id: The device context pointer.
9284 *
9285 * This function is directly called from the PCI layer as an interrupt
9286 * service routine when device with SLI-4 interface spec is enabled with
9287 * MSI-X multi-message interrupt mode and there are slow-path events in
9288 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9289 * interrupt mode, this function is called as part of the device-level
9290 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9291 * undergoing initialization, the interrupt handler will not process the
9292 * interrupt. The link attention and ELS ring attention events are handled
9293 * by the worker thread. The interrupt handler signals the worker thread
9294 * and returns for these events. This function is called without any lock
9295 * held. It gets the hbalock to access and update SLI data structures.
9296 *
9297 * This function returns IRQ_HANDLED when interrupt is handled else it
9298 * returns IRQ_NONE.
9299 **/
9300irqreturn_t
9301lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9302{
9303 struct lpfc_hba *phba;
9304 struct lpfc_queue *speq;
9305 struct lpfc_eqe *eqe;
9306 unsigned long iflag;
9307 int ecount = 0;
9308
9309 /*
9310 * Get the driver's phba structure from the dev_id
9311 */
9312 phba = (struct lpfc_hba *)dev_id;
9313
9314 if (unlikely(!phba))
9315 return IRQ_NONE;
9316
9317 /* Get to the EQ struct associated with this vector */
9318 speq = phba->sli4_hba.sp_eq;
9319
9320 /* Check device state for handling interrupt */
9321 if (unlikely(lpfc_intr_state_check(phba))) {
9322 /* Check again for link_state with lock held */
9323 spin_lock_irqsave(&phba->hbalock, iflag);
9324 if (phba->link_state < LPFC_LINK_DOWN)
9325 /* Flush, clear interrupt, and rearm the EQ */
9326 lpfc_sli4_eq_flush(phba, speq);
9327 spin_unlock_irqrestore(&phba->hbalock, iflag);
9328 return IRQ_NONE;
9329 }
9330
9331 /*
9332 * Process all the event on FCP slow-path EQ
9333 */
9334 while ((eqe = lpfc_sli4_eq_get(speq))) {
9335 lpfc_sli4_sp_handle_eqe(phba, eqe);
9336 if (!(++ecount % LPFC_GET_QE_REL_INT))
9337 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9338 }
9339
9340 /* Always clear and re-arm the slow-path EQ */
9341 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9342
9343 /* Catch the no cq entry condition */
9344 if (unlikely(ecount == 0)) {
9345 if (phba->intr_type == MSIX)
9346 /* MSI-X treated interrupt served as no EQ share INT */
9347 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9348 "0357 MSI-X interrupt with no EQE\n");
9349 else
9350 /* Non MSI-X treated on interrupt as EQ share INT */
9351 return IRQ_NONE;
9352 }
9353
9354 return IRQ_HANDLED;
9355} /* lpfc_sli4_sp_intr_handler */
9356
9357/**
9358 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9359 * @irq: Interrupt number.
9360 * @dev_id: The device context pointer.
9361 *
9362 * This function is directly called from the PCI layer as an interrupt
9363 * service routine when device with SLI-4 interface spec is enabled with
9364 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9365 * ring event in the HBA. However, when the device is enabled with either
9366 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9367 * device-level interrupt handler. When the PCI slot is in error recovery
9368 * or the HBA is undergoing initialization, the interrupt handler will not
9369 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9370 * the intrrupt context. This function is called without any lock held.
9371 * It gets the hbalock to access and update SLI data structures. Note that,
9372 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9373 * equal to that of FCP CQ index.
9374 *
9375 * This function returns IRQ_HANDLED when interrupt is handled else it
9376 * returns IRQ_NONE.
9377 **/
9378irqreturn_t
9379lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9380{
9381 struct lpfc_hba *phba;
9382 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9383 struct lpfc_queue *fpeq;
9384 struct lpfc_eqe *eqe;
9385 unsigned long iflag;
9386 int ecount = 0;
9387 uint32_t fcp_eqidx;
9388
9389 /* Get the driver's phba structure from the dev_id */
9390 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9391 phba = fcp_eq_hdl->phba;
9392 fcp_eqidx = fcp_eq_hdl->idx;
9393
9394 if (unlikely(!phba))
9395 return IRQ_NONE;
9396
9397 /* Get to the EQ struct associated with this vector */
9398 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9399
9400 /* Check device state for handling interrupt */
9401 if (unlikely(lpfc_intr_state_check(phba))) {
9402 /* Check again for link_state with lock held */
9403 spin_lock_irqsave(&phba->hbalock, iflag);
9404 if (phba->link_state < LPFC_LINK_DOWN)
9405 /* Flush, clear interrupt, and rearm the EQ */
9406 lpfc_sli4_eq_flush(phba, fpeq);
9407 spin_unlock_irqrestore(&phba->hbalock, iflag);
9408 return IRQ_NONE;
9409 }
9410
9411 /*
9412 * Process all the event on FCP fast-path EQ
9413 */
9414 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9415 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9416 if (!(++ecount % LPFC_GET_QE_REL_INT))
9417 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9418 }
9419
9420 /* Always clear and re-arm the fast-path EQ */
9421 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9422
9423 if (unlikely(ecount == 0)) {
9424 if (phba->intr_type == MSIX)
9425 /* MSI-X treated interrupt served as no EQ share INT */
9426 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9427 "0358 MSI-X interrupt with no EQE\n");
9428 else
9429 /* Non MSI-X treated on interrupt as EQ share INT */
9430 return IRQ_NONE;
9431 }
9432
9433 return IRQ_HANDLED;
9434} /* lpfc_sli4_fp_intr_handler */
9435
9436/**
9437 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9438 * @irq: Interrupt number.
9439 * @dev_id: The device context pointer.
9440 *
9441 * This function is the device-level interrupt handler to device with SLI-4
9442 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9443 * interrupt mode is enabled and there is an event in the HBA which requires
9444 * driver attention. This function invokes the slow-path interrupt attention
9445 * handling function and fast-path interrupt attention handling function in
9446 * turn to process the relevant HBA attention events. This function is called
9447 * without any lock held. It gets the hbalock to access and update SLI data
9448 * structures.
9449 *
9450 * This function returns IRQ_HANDLED when interrupt is handled, else it
9451 * returns IRQ_NONE.
9452 **/
9453irqreturn_t
9454lpfc_sli4_intr_handler(int irq, void *dev_id)
9455{
9456 struct lpfc_hba *phba;
9457 irqreturn_t sp_irq_rc, fp_irq_rc;
9458 bool fp_handled = false;
9459 uint32_t fcp_eqidx;
9460
9461 /* Get the driver's phba structure from the dev_id */
9462 phba = (struct lpfc_hba *)dev_id;
9463
9464 if (unlikely(!phba))
9465 return IRQ_NONE;
9466
9467 /*
9468 * Invokes slow-path host attention interrupt handling as appropriate.
9469 */
9470 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9471
9472 /*
9473 * Invoke fast-path host attention interrupt handling as appropriate.
9474 */
9475 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9476 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9477 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9478 if (fp_irq_rc == IRQ_HANDLED)
9479 fp_handled |= true;
9480 }
9481
9482 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9483} /* lpfc_sli4_intr_handler */
9484
9485/**
9486 * lpfc_sli4_queue_free - free a queue structure and associated memory
9487 * @queue: The queue structure to free.
9488 *
9489 * This function frees a queue structure and the DMAable memeory used for
9490 * the host resident queue. This function must be called after destroying the
9491 * queue on the HBA.
9492 **/
9493void
9494lpfc_sli4_queue_free(struct lpfc_queue *queue)
9495{
9496 struct lpfc_dmabuf *dmabuf;
9497
9498 if (!queue)
9499 return;
9500
9501 while (!list_empty(&queue->page_list)) {
9502 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9503 list);
9504 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9505 dmabuf->virt, dmabuf->phys);
9506 kfree(dmabuf);
9507 }
9508 kfree(queue);
9509 return;
9510}
9511
9512/**
9513 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9514 * @phba: The HBA that this queue is being created on.
9515 * @entry_size: The size of each queue entry for this queue.
9516 * @entry count: The number of entries that this queue will handle.
9517 *
9518 * This function allocates a queue structure and the DMAable memory used for
9519 * the host resident queue. This function must be called before creating the
9520 * queue on the HBA.
9521 **/
9522struct lpfc_queue *
9523lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9524 uint32_t entry_count)
9525{
9526 struct lpfc_queue *queue;
9527 struct lpfc_dmabuf *dmabuf;
9528 int x, total_qe_count;
9529 void *dma_pointer;
9530
9531
9532 queue = kzalloc(sizeof(struct lpfc_queue) +
9533 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9534 if (!queue)
9535 return NULL;
9536 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9537 INIT_LIST_HEAD(&queue->list);
9538 INIT_LIST_HEAD(&queue->page_list);
9539 INIT_LIST_HEAD(&queue->child_list);
9540 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9541 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9542 if (!dmabuf)
9543 goto out_fail;
9544 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9545 PAGE_SIZE, &dmabuf->phys,
9546 GFP_KERNEL);
9547 if (!dmabuf->virt) {
9548 kfree(dmabuf);
9549 goto out_fail;
9550 }
d11e31dd 9551 memset(dmabuf->virt, 0, PAGE_SIZE);
4f774513
JS
9552 dmabuf->buffer_tag = x;
9553 list_add_tail(&dmabuf->list, &queue->page_list);
9554 /* initialize queue's entry array */
9555 dma_pointer = dmabuf->virt;
9556 for (; total_qe_count < entry_count &&
9557 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9558 total_qe_count++, dma_pointer += entry_size) {
9559 queue->qe[total_qe_count].address = dma_pointer;
9560 }
9561 }
9562 queue->entry_size = entry_size;
9563 queue->entry_count = entry_count;
9564 queue->phba = phba;
9565
9566 return queue;
9567out_fail:
9568 lpfc_sli4_queue_free(queue);
9569 return NULL;
9570}
9571
9572/**
9573 * lpfc_eq_create - Create an Event Queue on the HBA
9574 * @phba: HBA structure that indicates port to create a queue on.
9575 * @eq: The queue structure to use to create the event queue.
9576 * @imax: The maximum interrupt per second limit.
9577 *
9578 * This function creates an event queue, as detailed in @eq, on a port,
9579 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9580 *
9581 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9582 * is used to get the entry count and entry size that are necessary to
9583 * determine the number of pages to allocate and use for this queue. This
9584 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9585 * event queue. This function is asynchronous and will wait for the mailbox
9586 * command to finish before continuing.
9587 *
9588 * On success this function will return a zero. If unable to allocate enough
9589 * memory this function will return ENOMEM. If the queue create mailbox command
9590 * fails this function will return ENXIO.
9591 **/
9592uint32_t
9593lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9594{
9595 struct lpfc_mbx_eq_create *eq_create;
9596 LPFC_MBOXQ_t *mbox;
9597 int rc, length, status = 0;
9598 struct lpfc_dmabuf *dmabuf;
9599 uint32_t shdr_status, shdr_add_status;
9600 union lpfc_sli4_cfg_shdr *shdr;
9601 uint16_t dmult;
9602
9603 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9604 if (!mbox)
9605 return -ENOMEM;
9606 length = (sizeof(struct lpfc_mbx_eq_create) -
9607 sizeof(struct lpfc_sli4_cfg_mhdr));
9608 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9609 LPFC_MBOX_OPCODE_EQ_CREATE,
9610 length, LPFC_SLI4_MBX_EMBED);
9611 eq_create = &mbox->u.mqe.un.eq_create;
9612 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9613 eq->page_count);
9614 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9615 LPFC_EQE_SIZE);
9616 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9617 /* Calculate delay multiper from maximum interrupt per second */
9618 dmult = LPFC_DMULT_CONST/imax - 1;
9619 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9620 dmult);
9621 switch (eq->entry_count) {
9622 default:
9623 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9624 "0360 Unsupported EQ count. (%d)\n",
9625 eq->entry_count);
9626 if (eq->entry_count < 256)
9627 return -EINVAL;
9628 /* otherwise default to smallest count (drop through) */
9629 case 256:
9630 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9631 LPFC_EQ_CNT_256);
9632 break;
9633 case 512:
9634 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9635 LPFC_EQ_CNT_512);
9636 break;
9637 case 1024:
9638 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9639 LPFC_EQ_CNT_1024);
9640 break;
9641 case 2048:
9642 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9643 LPFC_EQ_CNT_2048);
9644 break;
9645 case 4096:
9646 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9647 LPFC_EQ_CNT_4096);
9648 break;
9649 }
9650 list_for_each_entry(dmabuf, &eq->page_list, list) {
9651 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9652 putPaddrLow(dmabuf->phys);
9653 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9654 putPaddrHigh(dmabuf->phys);
9655 }
9656 mbox->vport = phba->pport;
9657 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9658 mbox->context1 = NULL;
9659 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9660 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9661 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9662 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9663 if (shdr_status || shdr_add_status || rc) {
9664 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9665 "2500 EQ_CREATE mailbox failed with "
9666 "status x%x add_status x%x, mbx status x%x\n",
9667 shdr_status, shdr_add_status, rc);
9668 status = -ENXIO;
9669 }
9670 eq->type = LPFC_EQ;
9671 eq->subtype = LPFC_NONE;
9672 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9673 if (eq->queue_id == 0xFFFF)
9674 status = -ENXIO;
9675 eq->host_index = 0;
9676 eq->hba_index = 0;
9677
8fa38513 9678 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9679 return status;
9680}
9681
9682/**
9683 * lpfc_cq_create - Create a Completion Queue on the HBA
9684 * @phba: HBA structure that indicates port to create a queue on.
9685 * @cq: The queue structure to use to create the completion queue.
9686 * @eq: The event queue to bind this completion queue to.
9687 *
9688 * This function creates a completion queue, as detailed in @wq, on a port,
9689 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9690 *
9691 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9692 * is used to get the entry count and entry size that are necessary to
9693 * determine the number of pages to allocate and use for this queue. The @eq
9694 * is used to indicate which event queue to bind this completion queue to. This
9695 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9696 * completion queue. This function is asynchronous and will wait for the mailbox
9697 * command to finish before continuing.
9698 *
9699 * On success this function will return a zero. If unable to allocate enough
9700 * memory this function will return ENOMEM. If the queue create mailbox command
9701 * fails this function will return ENXIO.
9702 **/
9703uint32_t
9704lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9705 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9706{
9707 struct lpfc_mbx_cq_create *cq_create;
9708 struct lpfc_dmabuf *dmabuf;
9709 LPFC_MBOXQ_t *mbox;
9710 int rc, length, status = 0;
9711 uint32_t shdr_status, shdr_add_status;
9712 union lpfc_sli4_cfg_shdr *shdr;
9713
9714 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9715 if (!mbox)
9716 return -ENOMEM;
9717 length = (sizeof(struct lpfc_mbx_cq_create) -
9718 sizeof(struct lpfc_sli4_cfg_mhdr));
9719 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9720 LPFC_MBOX_OPCODE_CQ_CREATE,
9721 length, LPFC_SLI4_MBX_EMBED);
9722 cq_create = &mbox->u.mqe.un.cq_create;
9723 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9724 cq->page_count);
9725 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9726 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9727 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9728 switch (cq->entry_count) {
9729 default:
9730 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9731 "0361 Unsupported CQ count. (%d)\n",
9732 cq->entry_count);
9733 if (cq->entry_count < 256)
9734 return -EINVAL;
9735 /* otherwise default to smallest count (drop through) */
9736 case 256:
9737 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9738 LPFC_CQ_CNT_256);
9739 break;
9740 case 512:
9741 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9742 LPFC_CQ_CNT_512);
9743 break;
9744 case 1024:
9745 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9746 LPFC_CQ_CNT_1024);
9747 break;
9748 }
9749 list_for_each_entry(dmabuf, &cq->page_list, list) {
9750 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9751 putPaddrLow(dmabuf->phys);
9752 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9753 putPaddrHigh(dmabuf->phys);
9754 }
9755 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9756
9757 /* The IOCTL status is embedded in the mailbox subheader. */
9758 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9759 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9760 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9761 if (shdr_status || shdr_add_status || rc) {
9762 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9763 "2501 CQ_CREATE mailbox failed with "
9764 "status x%x add_status x%x, mbx status x%x\n",
9765 shdr_status, shdr_add_status, rc);
9766 status = -ENXIO;
9767 goto out;
9768 }
9769 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9770 if (cq->queue_id == 0xFFFF) {
9771 status = -ENXIO;
9772 goto out;
9773 }
9774 /* link the cq onto the parent eq child list */
9775 list_add_tail(&cq->list, &eq->child_list);
9776 /* Set up completion queue's type and subtype */
9777 cq->type = type;
9778 cq->subtype = subtype;
9779 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9780 cq->host_index = 0;
9781 cq->hba_index = 0;
4f774513 9782
8fa38513
JS
9783out:
9784 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9785 return status;
9786}
9787
04c68496
JS
9788/**
9789 * lpfc_mq_create - Create a mailbox Queue on the HBA
9790 * @phba: HBA structure that indicates port to create a queue on.
9791 * @mq: The queue structure to use to create the mailbox queue.
9792 *
9793 * This function creates a mailbox queue, as detailed in @mq, on a port,
9794 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9795 *
9796 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9797 * is used to get the entry count and entry size that are necessary to
9798 * determine the number of pages to allocate and use for this queue. This
9799 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9800 * mailbox queue. This function is asynchronous and will wait for the mailbox
9801 * command to finish before continuing.
9802 *
9803 * On success this function will return a zero. If unable to allocate enough
9804 * memory this function will return ENOMEM. If the queue create mailbox command
9805 * fails this function will return ENXIO.
9806 **/
9807uint32_t
9808lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9809 struct lpfc_queue *cq, uint32_t subtype)
9810{
9811 struct lpfc_mbx_mq_create *mq_create;
9812 struct lpfc_dmabuf *dmabuf;
9813 LPFC_MBOXQ_t *mbox;
9814 int rc, length, status = 0;
9815 uint32_t shdr_status, shdr_add_status;
9816 union lpfc_sli4_cfg_shdr *shdr;
9817
9818 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9819 if (!mbox)
9820 return -ENOMEM;
9821 length = (sizeof(struct lpfc_mbx_mq_create) -
9822 sizeof(struct lpfc_sli4_cfg_mhdr));
9823 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9824 LPFC_MBOX_OPCODE_MQ_CREATE,
9825 length, LPFC_SLI4_MBX_EMBED);
9826 mq_create = &mbox->u.mqe.un.mq_create;
9827 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9828 mq->page_count);
9829 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9830 cq->queue_id);
9831 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9832 switch (mq->entry_count) {
9833 default:
9834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9835 "0362 Unsupported MQ count. (%d)\n",
9836 mq->entry_count);
9837 if (mq->entry_count < 16)
9838 return -EINVAL;
9839 /* otherwise default to smallest count (drop through) */
9840 case 16:
9841 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9842 LPFC_MQ_CNT_16);
9843 break;
9844 case 32:
9845 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9846 LPFC_MQ_CNT_32);
9847 break;
9848 case 64:
9849 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9850 LPFC_MQ_CNT_64);
9851 break;
9852 case 128:
9853 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9854 LPFC_MQ_CNT_128);
9855 break;
9856 }
9857 list_for_each_entry(dmabuf, &mq->page_list, list) {
9858 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9859 putPaddrLow(dmabuf->phys);
9860 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9861 putPaddrHigh(dmabuf->phys);
9862 }
9863 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9864 /* The IOCTL status is embedded in the mailbox subheader. */
9865 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9866 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9867 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9868 if (shdr_status || shdr_add_status || rc) {
9869 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9870 "2502 MQ_CREATE mailbox failed with "
9871 "status x%x add_status x%x, mbx status x%x\n",
9872 shdr_status, shdr_add_status, rc);
9873 status = -ENXIO;
9874 goto out;
9875 }
9876 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9877 if (mq->queue_id == 0xFFFF) {
9878 status = -ENXIO;
9879 goto out;
9880 }
9881 mq->type = LPFC_MQ;
9882 mq->subtype = subtype;
9883 mq->host_index = 0;
9884 mq->hba_index = 0;
9885
9886 /* link the mq onto the parent cq child list */
9887 list_add_tail(&mq->list, &cq->child_list);
9888out:
8fa38513 9889 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
9890 return status;
9891}
9892
4f774513
JS
9893/**
9894 * lpfc_wq_create - Create a Work Queue on the HBA
9895 * @phba: HBA structure that indicates port to create a queue on.
9896 * @wq: The queue structure to use to create the work queue.
9897 * @cq: The completion queue to bind this work queue to.
9898 * @subtype: The subtype of the work queue indicating its functionality.
9899 *
9900 * This function creates a work queue, as detailed in @wq, on a port, described
9901 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9902 *
9903 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9904 * is used to get the entry count and entry size that are necessary to
9905 * determine the number of pages to allocate and use for this queue. The @cq
9906 * is used to indicate which completion queue to bind this work queue to. This
9907 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9908 * work queue. This function is asynchronous and will wait for the mailbox
9909 * command to finish before continuing.
9910 *
9911 * On success this function will return a zero. If unable to allocate enough
9912 * memory this function will return ENOMEM. If the queue create mailbox command
9913 * fails this function will return ENXIO.
9914 **/
9915uint32_t
9916lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9917 struct lpfc_queue *cq, uint32_t subtype)
9918{
9919 struct lpfc_mbx_wq_create *wq_create;
9920 struct lpfc_dmabuf *dmabuf;
9921 LPFC_MBOXQ_t *mbox;
9922 int rc, length, status = 0;
9923 uint32_t shdr_status, shdr_add_status;
9924 union lpfc_sli4_cfg_shdr *shdr;
9925
9926 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9927 if (!mbox)
9928 return -ENOMEM;
9929 length = (sizeof(struct lpfc_mbx_wq_create) -
9930 sizeof(struct lpfc_sli4_cfg_mhdr));
9931 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9932 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9933 length, LPFC_SLI4_MBX_EMBED);
9934 wq_create = &mbox->u.mqe.un.wq_create;
9935 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9936 wq->page_count);
9937 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9938 cq->queue_id);
9939 list_for_each_entry(dmabuf, &wq->page_list, list) {
9940 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9941 putPaddrLow(dmabuf->phys);
9942 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9943 putPaddrHigh(dmabuf->phys);
9944 }
9945 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9946 /* The IOCTL status is embedded in the mailbox subheader. */
9947 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9948 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9949 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9950 if (shdr_status || shdr_add_status || rc) {
9951 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9952 "2503 WQ_CREATE mailbox failed with "
9953 "status x%x add_status x%x, mbx status x%x\n",
9954 shdr_status, shdr_add_status, rc);
9955 status = -ENXIO;
9956 goto out;
9957 }
9958 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9959 if (wq->queue_id == 0xFFFF) {
9960 status = -ENXIO;
9961 goto out;
9962 }
9963 wq->type = LPFC_WQ;
9964 wq->subtype = subtype;
9965 wq->host_index = 0;
9966 wq->hba_index = 0;
9967
9968 /* link the wq onto the parent cq child list */
9969 list_add_tail(&wq->list, &cq->child_list);
9970out:
8fa38513 9971 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9972 return status;
9973}
9974
9975/**
9976 * lpfc_rq_create - Create a Receive Queue on the HBA
9977 * @phba: HBA structure that indicates port to create a queue on.
9978 * @hrq: The queue structure to use to create the header receive queue.
9979 * @drq: The queue structure to use to create the data receive queue.
9980 * @cq: The completion queue to bind this work queue to.
9981 *
9982 * This function creates a receive buffer queue pair , as detailed in @hrq and
9983 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9984 * to the HBA.
9985 *
9986 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9987 * struct is used to get the entry count that is necessary to determine the
9988 * number of pages to use for this queue. The @cq is used to indicate which
9989 * completion queue to bind received buffers that are posted to these queues to.
9990 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9991 * receive queue pair. This function is asynchronous and will wait for the
9992 * mailbox command to finish before continuing.
9993 *
9994 * On success this function will return a zero. If unable to allocate enough
9995 * memory this function will return ENOMEM. If the queue create mailbox command
9996 * fails this function will return ENXIO.
9997 **/
9998uint32_t
9999lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10000 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
10001{
10002 struct lpfc_mbx_rq_create *rq_create;
10003 struct lpfc_dmabuf *dmabuf;
10004 LPFC_MBOXQ_t *mbox;
10005 int rc, length, status = 0;
10006 uint32_t shdr_status, shdr_add_status;
10007 union lpfc_sli4_cfg_shdr *shdr;
10008
10009 if (hrq->entry_count != drq->entry_count)
10010 return -EINVAL;
10011 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10012 if (!mbox)
10013 return -ENOMEM;
10014 length = (sizeof(struct lpfc_mbx_rq_create) -
10015 sizeof(struct lpfc_sli4_cfg_mhdr));
10016 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10017 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10018 length, LPFC_SLI4_MBX_EMBED);
10019 rq_create = &mbox->u.mqe.un.rq_create;
10020 switch (hrq->entry_count) {
10021 default:
10022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10023 "2535 Unsupported RQ count. (%d)\n",
10024 hrq->entry_count);
10025 if (hrq->entry_count < 512)
10026 return -EINVAL;
10027 /* otherwise default to smallest count (drop through) */
10028 case 512:
10029 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10030 LPFC_RQ_RING_SIZE_512);
10031 break;
10032 case 1024:
10033 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10034 LPFC_RQ_RING_SIZE_1024);
10035 break;
10036 case 2048:
10037 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10038 LPFC_RQ_RING_SIZE_2048);
10039 break;
10040 case 4096:
10041 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10042 LPFC_RQ_RING_SIZE_4096);
10043 break;
10044 }
10045 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10046 cq->queue_id);
10047 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10048 hrq->page_count);
10049 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10050 LPFC_HDR_BUF_SIZE);
10051 list_for_each_entry(dmabuf, &hrq->page_list, list) {
10052 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10053 putPaddrLow(dmabuf->phys);
10054 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10055 putPaddrHigh(dmabuf->phys);
10056 }
10057 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10058 /* The IOCTL status is embedded in the mailbox subheader. */
10059 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10060 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10061 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10062 if (shdr_status || shdr_add_status || rc) {
10063 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10064 "2504 RQ_CREATE mailbox failed with "
10065 "status x%x add_status x%x, mbx status x%x\n",
10066 shdr_status, shdr_add_status, rc);
10067 status = -ENXIO;
10068 goto out;
10069 }
10070 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10071 if (hrq->queue_id == 0xFFFF) {
10072 status = -ENXIO;
10073 goto out;
10074 }
10075 hrq->type = LPFC_HRQ;
10076 hrq->subtype = subtype;
10077 hrq->host_index = 0;
10078 hrq->hba_index = 0;
10079
10080 /* now create the data queue */
10081 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10082 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
10083 length, LPFC_SLI4_MBX_EMBED);
10084 switch (drq->entry_count) {
10085 default:
10086 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10087 "2536 Unsupported RQ count. (%d)\n",
10088 drq->entry_count);
10089 if (drq->entry_count < 512)
10090 return -EINVAL;
10091 /* otherwise default to smallest count (drop through) */
10092 case 512:
10093 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10094 LPFC_RQ_RING_SIZE_512);
10095 break;
10096 case 1024:
10097 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10098 LPFC_RQ_RING_SIZE_1024);
10099 break;
10100 case 2048:
10101 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10102 LPFC_RQ_RING_SIZE_2048);
10103 break;
10104 case 4096:
10105 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10106 LPFC_RQ_RING_SIZE_4096);
10107 break;
10108 }
10109 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10110 cq->queue_id);
10111 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10112 drq->page_count);
10113 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10114 LPFC_DATA_BUF_SIZE);
10115 list_for_each_entry(dmabuf, &drq->page_list, list) {
10116 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10117 putPaddrLow(dmabuf->phys);
10118 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10119 putPaddrHigh(dmabuf->phys);
10120 }
10121 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10122 /* The IOCTL status is embedded in the mailbox subheader. */
10123 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10124 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10125 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10126 if (shdr_status || shdr_add_status || rc) {
10127 status = -ENXIO;
10128 goto out;
10129 }
10130 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10131 if (drq->queue_id == 0xFFFF) {
10132 status = -ENXIO;
10133 goto out;
10134 }
10135 drq->type = LPFC_DRQ;
10136 drq->subtype = subtype;
10137 drq->host_index = 0;
10138 drq->hba_index = 0;
10139
10140 /* link the header and data RQs onto the parent cq child list */
10141 list_add_tail(&hrq->list, &cq->child_list);
10142 list_add_tail(&drq->list, &cq->child_list);
10143
10144out:
8fa38513 10145 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10146 return status;
10147}
10148
10149/**
10150 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10151 * @eq: The queue structure associated with the queue to destroy.
10152 *
10153 * This function destroys a queue, as detailed in @eq by sending an mailbox
10154 * command, specific to the type of queue, to the HBA.
10155 *
10156 * The @eq struct is used to get the queue ID of the queue to destroy.
10157 *
10158 * On success this function will return a zero. If the queue destroy mailbox
10159 * command fails this function will return ENXIO.
10160 **/
10161uint32_t
10162lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10163{
10164 LPFC_MBOXQ_t *mbox;
10165 int rc, length, status = 0;
10166 uint32_t shdr_status, shdr_add_status;
10167 union lpfc_sli4_cfg_shdr *shdr;
10168
10169 if (!eq)
10170 return -ENODEV;
10171 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10172 if (!mbox)
10173 return -ENOMEM;
10174 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10175 sizeof(struct lpfc_sli4_cfg_mhdr));
10176 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10177 LPFC_MBOX_OPCODE_EQ_DESTROY,
10178 length, LPFC_SLI4_MBX_EMBED);
10179 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10180 eq->queue_id);
10181 mbox->vport = eq->phba->pport;
10182 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10183
10184 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10185 /* The IOCTL status is embedded in the mailbox subheader. */
10186 shdr = (union lpfc_sli4_cfg_shdr *)
10187 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10188 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10189 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10190 if (shdr_status || shdr_add_status || rc) {
10191 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10192 "2505 EQ_DESTROY mailbox failed with "
10193 "status x%x add_status x%x, mbx status x%x\n",
10194 shdr_status, shdr_add_status, rc);
10195 status = -ENXIO;
10196 }
10197
10198 /* Remove eq from any list */
10199 list_del_init(&eq->list);
8fa38513 10200 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
10201 return status;
10202}
10203
10204/**
10205 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10206 * @cq: The queue structure associated with the queue to destroy.
10207 *
10208 * This function destroys a queue, as detailed in @cq by sending an mailbox
10209 * command, specific to the type of queue, to the HBA.
10210 *
10211 * The @cq struct is used to get the queue ID of the queue to destroy.
10212 *
10213 * On success this function will return a zero. If the queue destroy mailbox
10214 * command fails this function will return ENXIO.
10215 **/
10216uint32_t
10217lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10218{
10219 LPFC_MBOXQ_t *mbox;
10220 int rc, length, status = 0;
10221 uint32_t shdr_status, shdr_add_status;
10222 union lpfc_sli4_cfg_shdr *shdr;
10223
10224 if (!cq)
10225 return -ENODEV;
10226 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10227 if (!mbox)
10228 return -ENOMEM;
10229 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10230 sizeof(struct lpfc_sli4_cfg_mhdr));
10231 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10232 LPFC_MBOX_OPCODE_CQ_DESTROY,
10233 length, LPFC_SLI4_MBX_EMBED);
10234 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10235 cq->queue_id);
10236 mbox->vport = cq->phba->pport;
10237 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10238 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10239 /* The IOCTL status is embedded in the mailbox subheader. */
10240 shdr = (union lpfc_sli4_cfg_shdr *)
10241 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10242 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10243 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10244 if (shdr_status || shdr_add_status || rc) {
10245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10246 "2506 CQ_DESTROY mailbox failed with "
10247 "status x%x add_status x%x, mbx status x%x\n",
10248 shdr_status, shdr_add_status, rc);
10249 status = -ENXIO;
10250 }
10251 /* Remove cq from any list */
10252 list_del_init(&cq->list);
8fa38513 10253 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
10254 return status;
10255}
10256
04c68496
JS
10257/**
10258 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10259 * @qm: The queue structure associated with the queue to destroy.
10260 *
10261 * This function destroys a queue, as detailed in @mq by sending an mailbox
10262 * command, specific to the type of queue, to the HBA.
10263 *
10264 * The @mq struct is used to get the queue ID of the queue to destroy.
10265 *
10266 * On success this function will return a zero. If the queue destroy mailbox
10267 * command fails this function will return ENXIO.
10268 **/
10269uint32_t
10270lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10271{
10272 LPFC_MBOXQ_t *mbox;
10273 int rc, length, status = 0;
10274 uint32_t shdr_status, shdr_add_status;
10275 union lpfc_sli4_cfg_shdr *shdr;
10276
10277 if (!mq)
10278 return -ENODEV;
10279 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10280 if (!mbox)
10281 return -ENOMEM;
10282 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10283 sizeof(struct lpfc_sli4_cfg_mhdr));
10284 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10285 LPFC_MBOX_OPCODE_MQ_DESTROY,
10286 length, LPFC_SLI4_MBX_EMBED);
10287 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10288 mq->queue_id);
10289 mbox->vport = mq->phba->pport;
10290 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10291 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10292 /* The IOCTL status is embedded in the mailbox subheader. */
10293 shdr = (union lpfc_sli4_cfg_shdr *)
10294 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10295 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10296 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10297 if (shdr_status || shdr_add_status || rc) {
10298 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10299 "2507 MQ_DESTROY mailbox failed with "
10300 "status x%x add_status x%x, mbx status x%x\n",
10301 shdr_status, shdr_add_status, rc);
10302 status = -ENXIO;
10303 }
10304 /* Remove mq from any list */
10305 list_del_init(&mq->list);
8fa38513 10306 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
10307 return status;
10308}
10309
4f774513
JS
10310/**
10311 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10312 * @wq: The queue structure associated with the queue to destroy.
10313 *
10314 * This function destroys a queue, as detailed in @wq by sending an mailbox
10315 * command, specific to the type of queue, to the HBA.
10316 *
10317 * The @wq struct is used to get the queue ID of the queue to destroy.
10318 *
10319 * On success this function will return a zero. If the queue destroy mailbox
10320 * command fails this function will return ENXIO.
10321 **/
10322uint32_t
10323lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10324{
10325 LPFC_MBOXQ_t *mbox;
10326 int rc, length, status = 0;
10327 uint32_t shdr_status, shdr_add_status;
10328 union lpfc_sli4_cfg_shdr *shdr;
10329
10330 if (!wq)
10331 return -ENODEV;
10332 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10333 if (!mbox)
10334 return -ENOMEM;
10335 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10336 sizeof(struct lpfc_sli4_cfg_mhdr));
10337 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10338 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10339 length, LPFC_SLI4_MBX_EMBED);
10340 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10341 wq->queue_id);
10342 mbox->vport = wq->phba->pport;
10343 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10344 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10345 shdr = (union lpfc_sli4_cfg_shdr *)
10346 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10347 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10348 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10349 if (shdr_status || shdr_add_status || rc) {
10350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10351 "2508 WQ_DESTROY mailbox failed with "
10352 "status x%x add_status x%x, mbx status x%x\n",
10353 shdr_status, shdr_add_status, rc);
10354 status = -ENXIO;
10355 }
10356 /* Remove wq from any list */
10357 list_del_init(&wq->list);
8fa38513 10358 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
10359 return status;
10360}
10361
10362/**
10363 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10364 * @rq: The queue structure associated with the queue to destroy.
10365 *
10366 * This function destroys a queue, as detailed in @rq by sending an mailbox
10367 * command, specific to the type of queue, to the HBA.
10368 *
10369 * The @rq struct is used to get the queue ID of the queue to destroy.
10370 *
10371 * On success this function will return a zero. If the queue destroy mailbox
10372 * command fails this function will return ENXIO.
10373 **/
10374uint32_t
10375lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10376 struct lpfc_queue *drq)
10377{
10378 LPFC_MBOXQ_t *mbox;
10379 int rc, length, status = 0;
10380 uint32_t shdr_status, shdr_add_status;
10381 union lpfc_sli4_cfg_shdr *shdr;
10382
10383 if (!hrq || !drq)
10384 return -ENODEV;
10385 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10386 if (!mbox)
10387 return -ENOMEM;
10388 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10389 sizeof(struct mbox_header));
10390 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10391 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10392 length, LPFC_SLI4_MBX_EMBED);
10393 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10394 hrq->queue_id);
10395 mbox->vport = hrq->phba->pport;
10396 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10397 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10398 /* The IOCTL status is embedded in the mailbox subheader. */
10399 shdr = (union lpfc_sli4_cfg_shdr *)
10400 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10403 if (shdr_status || shdr_add_status || rc) {
10404 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10405 "2509 RQ_DESTROY mailbox failed with "
10406 "status x%x add_status x%x, mbx status x%x\n",
10407 shdr_status, shdr_add_status, rc);
10408 if (rc != MBX_TIMEOUT)
10409 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10410 return -ENXIO;
10411 }
10412 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10413 drq->queue_id);
10414 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10415 shdr = (union lpfc_sli4_cfg_shdr *)
10416 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10417 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10418 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10419 if (shdr_status || shdr_add_status || rc) {
10420 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10421 "2510 RQ_DESTROY mailbox failed with "
10422 "status x%x add_status x%x, mbx status x%x\n",
10423 shdr_status, shdr_add_status, rc);
10424 status = -ENXIO;
10425 }
10426 list_del_init(&hrq->list);
10427 list_del_init(&drq->list);
8fa38513 10428 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
10429 return status;
10430}
10431
10432/**
10433 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10434 * @phba: The virtual port for which this call being executed.
10435 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10436 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10437 * @xritag: the xritag that ties this io to the SGL pages.
10438 *
10439 * This routine will post the sgl pages for the IO that has the xritag
10440 * that is in the iocbq structure. The xritag is assigned during iocbq
10441 * creation and persists for as long as the driver is loaded.
10442 * if the caller has fewer than 256 scatter gather segments to map then
10443 * pdma_phys_addr1 should be 0.
10444 * If the caller needs to map more than 256 scatter gather segment then
10445 * pdma_phys_addr1 should be a valid physical address.
10446 * physical address for SGLs must be 64 byte aligned.
10447 * If you are going to map 2 SGL's then the first one must have 256 entries
10448 * the second sgl can have between 1 and 256 entries.
10449 *
10450 * Return codes:
10451 * 0 - Success
10452 * -ENXIO, -ENOMEM - Failure
10453 **/
10454int
10455lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10456 dma_addr_t pdma_phys_addr0,
10457 dma_addr_t pdma_phys_addr1,
10458 uint16_t xritag)
10459{
10460 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10461 LPFC_MBOXQ_t *mbox;
10462 int rc;
10463 uint32_t shdr_status, shdr_add_status;
10464 union lpfc_sli4_cfg_shdr *shdr;
10465
10466 if (xritag == NO_XRI) {
10467 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10468 "0364 Invalid param:\n");
10469 return -EINVAL;
10470 }
10471
10472 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10473 if (!mbox)
10474 return -ENOMEM;
10475
10476 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10477 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10478 sizeof(struct lpfc_mbx_post_sgl_pages) -
10479 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10480
10481 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10482 &mbox->u.mqe.un.post_sgl_pages;
10483 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10484 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10485
10486 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10487 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10488 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10489 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10490
10491 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10492 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10493 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10494 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10495 if (!phba->sli4_hba.intr_enable)
10496 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10497 else
10498 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10499 /* The IOCTL status is embedded in the mailbox subheader. */
10500 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10501 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10502 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10503 if (rc != MBX_TIMEOUT)
10504 mempool_free(mbox, phba->mbox_mem_pool);
10505 if (shdr_status || shdr_add_status || rc) {
10506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10507 "2511 POST_SGL mailbox failed with "
10508 "status x%x add_status x%x, mbx status x%x\n",
10509 shdr_status, shdr_add_status, rc);
10510 rc = -ENXIO;
10511 }
10512 return 0;
10513}
10514/**
10515 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10516 * @phba: The virtual port for which this call being executed.
10517 *
10518 * This routine will remove all of the sgl pages registered with the hba.
10519 *
10520 * Return codes:
10521 * 0 - Success
10522 * -ENXIO, -ENOMEM - Failure
10523 **/
10524int
10525lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10526{
10527 LPFC_MBOXQ_t *mbox;
10528 int rc;
10529 uint32_t shdr_status, shdr_add_status;
10530 union lpfc_sli4_cfg_shdr *shdr;
10531
10532 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10533 if (!mbox)
10534 return -ENOMEM;
10535
10536 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10537 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10538 LPFC_SLI4_MBX_EMBED);
10539 if (!phba->sli4_hba.intr_enable)
10540 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10541 else
10542 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10543 /* The IOCTL status is embedded in the mailbox subheader. */
10544 shdr = (union lpfc_sli4_cfg_shdr *)
10545 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10546 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10547 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10548 if (rc != MBX_TIMEOUT)
10549 mempool_free(mbox, phba->mbox_mem_pool);
10550 if (shdr_status || shdr_add_status || rc) {
10551 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10552 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10553 "status x%x add_status x%x, mbx status x%x\n",
10554 shdr_status, shdr_add_status, rc);
10555 rc = -ENXIO;
10556 }
10557 return rc;
10558}
10559
10560/**
10561 * lpfc_sli4_next_xritag - Get an xritag for the io
10562 * @phba: Pointer to HBA context object.
10563 *
10564 * This function gets an xritag for the iocb. If there is no unused xritag
10565 * it will return 0xffff.
10566 * The function returns the allocated xritag if successful, else returns zero.
10567 * Zero is not a valid xritag.
10568 * The caller is not required to hold any lock.
10569 **/
10570uint16_t
10571lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10572{
10573 uint16_t xritag;
10574
10575 spin_lock_irq(&phba->hbalock);
10576 xritag = phba->sli4_hba.next_xri;
10577 if ((xritag != (uint16_t) -1) && xritag <
10578 (phba->sli4_hba.max_cfg_param.max_xri
10579 + phba->sli4_hba.max_cfg_param.xri_base)) {
10580 phba->sli4_hba.next_xri++;
10581 phba->sli4_hba.max_cfg_param.xri_used++;
10582 spin_unlock_irq(&phba->hbalock);
10583 return xritag;
10584 }
10585 spin_unlock_irq(&phba->hbalock);
6a9c52cf 10586 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4f774513
JS
10587 "2004 Failed to allocate XRI.last XRITAG is %d"
10588 " Max XRI is %d, Used XRI is %d\n",
10589 phba->sli4_hba.next_xri,
10590 phba->sli4_hba.max_cfg_param.max_xri,
10591 phba->sli4_hba.max_cfg_param.xri_used);
10592 return -1;
10593}
10594
10595/**
10596 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10597 * @phba: pointer to lpfc hba data structure.
10598 *
10599 * This routine is invoked to post a block of driver's sgl pages to the
10600 * HBA using non-embedded mailbox command. No Lock is held. This routine
10601 * is only called when the driver is loading and after all IO has been
10602 * stopped.
10603 **/
10604int
10605lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10606{
10607 struct lpfc_sglq *sglq_entry;
10608 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10609 struct sgl_page_pairs *sgl_pg_pairs;
10610 void *viraddr;
10611 LPFC_MBOXQ_t *mbox;
10612 uint32_t reqlen, alloclen, pg_pairs;
10613 uint32_t mbox_tmo;
10614 uint16_t xritag_start = 0;
10615 int els_xri_cnt, rc = 0;
10616 uint32_t shdr_status, shdr_add_status;
10617 union lpfc_sli4_cfg_shdr *shdr;
10618
10619 /* The number of sgls to be posted */
10620 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10621
10622 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10623 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10624 if (reqlen > PAGE_SIZE) {
10625 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10626 "2559 Block sgl registration required DMA "
10627 "size (%d) great than a page\n", reqlen);
10628 return -ENOMEM;
10629 }
10630 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10631 if (!mbox) {
10632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10633 "2560 Failed to allocate mbox cmd memory\n");
10634 return -ENOMEM;
10635 }
10636
10637 /* Allocate DMA memory and set up the non-embedded mailbox command */
10638 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10639 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10640 LPFC_SLI4_MBX_NEMBED);
10641
10642 if (alloclen < reqlen) {
10643 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10644 "0285 Allocated DMA memory size (%d) is "
10645 "less than the requested DMA memory "
10646 "size (%d)\n", alloclen, reqlen);
10647 lpfc_sli4_mbox_cmd_free(phba, mbox);
10648 return -ENOMEM;
10649 }
4f774513 10650 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10651 viraddr = mbox->sge_array->addr[0];
10652
10653 /* Set up the SGL pages in the non-embedded DMA pages */
10654 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10655 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10656
10657 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10658 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10659 /* Set up the sge entry */
10660 sgl_pg_pairs->sgl_pg0_addr_lo =
10661 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10662 sgl_pg_pairs->sgl_pg0_addr_hi =
10663 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10664 sgl_pg_pairs->sgl_pg1_addr_lo =
10665 cpu_to_le32(putPaddrLow(0));
10666 sgl_pg_pairs->sgl_pg1_addr_hi =
10667 cpu_to_le32(putPaddrHigh(0));
10668 /* Keep the first xritag on the list */
10669 if (pg_pairs == 0)
10670 xritag_start = sglq_entry->sli4_xritag;
10671 sgl_pg_pairs++;
10672 }
10673 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 10674 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513
JS
10675 /* Perform endian conversion if necessary */
10676 sgl->word0 = cpu_to_le32(sgl->word0);
10677
10678 if (!phba->sli4_hba.intr_enable)
10679 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10680 else {
10681 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10682 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10683 }
10684 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10685 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10686 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10687 if (rc != MBX_TIMEOUT)
10688 lpfc_sli4_mbox_cmd_free(phba, mbox);
10689 if (shdr_status || shdr_add_status || rc) {
10690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10691 "2513 POST_SGL_BLOCK mailbox command failed "
10692 "status x%x add_status x%x mbx status x%x\n",
10693 shdr_status, shdr_add_status, rc);
10694 rc = -ENXIO;
10695 }
10696 return rc;
10697}
10698
10699/**
10700 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10701 * @phba: pointer to lpfc hba data structure.
10702 * @sblist: pointer to scsi buffer list.
10703 * @count: number of scsi buffers on the list.
10704 *
10705 * This routine is invoked to post a block of @count scsi sgl pages from a
10706 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10707 * No Lock is held.
10708 *
10709 **/
10710int
10711lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10712 int cnt)
10713{
10714 struct lpfc_scsi_buf *psb;
10715 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10716 struct sgl_page_pairs *sgl_pg_pairs;
10717 void *viraddr;
10718 LPFC_MBOXQ_t *mbox;
10719 uint32_t reqlen, alloclen, pg_pairs;
10720 uint32_t mbox_tmo;
10721 uint16_t xritag_start = 0;
10722 int rc = 0;
10723 uint32_t shdr_status, shdr_add_status;
10724 dma_addr_t pdma_phys_bpl1;
10725 union lpfc_sli4_cfg_shdr *shdr;
10726
10727 /* Calculate the requested length of the dma memory */
10728 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10729 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10730 if (reqlen > PAGE_SIZE) {
10731 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10732 "0217 Block sgl registration required DMA "
10733 "size (%d) great than a page\n", reqlen);
10734 return -ENOMEM;
10735 }
10736 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10737 if (!mbox) {
10738 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10739 "0283 Failed to allocate mbox cmd memory\n");
10740 return -ENOMEM;
10741 }
10742
10743 /* Allocate DMA memory and set up the non-embedded mailbox command */
10744 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10745 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10746 LPFC_SLI4_MBX_NEMBED);
10747
10748 if (alloclen < reqlen) {
10749 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10750 "2561 Allocated DMA memory size (%d) is "
10751 "less than the requested DMA memory "
10752 "size (%d)\n", alloclen, reqlen);
10753 lpfc_sli4_mbox_cmd_free(phba, mbox);
10754 return -ENOMEM;
10755 }
4f774513 10756 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10757 viraddr = mbox->sge_array->addr[0];
10758
10759 /* Set up the SGL pages in the non-embedded DMA pages */
10760 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10761 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10762
10763 pg_pairs = 0;
10764 list_for_each_entry(psb, sblist, list) {
10765 /* Set up the sge entry */
10766 sgl_pg_pairs->sgl_pg0_addr_lo =
10767 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10768 sgl_pg_pairs->sgl_pg0_addr_hi =
10769 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10770 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10771 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10772 else
10773 pdma_phys_bpl1 = 0;
10774 sgl_pg_pairs->sgl_pg1_addr_lo =
10775 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10776 sgl_pg_pairs->sgl_pg1_addr_hi =
10777 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10778 /* Keep the first xritag on the list */
10779 if (pg_pairs == 0)
10780 xritag_start = psb->cur_iocbq.sli4_xritag;
10781 sgl_pg_pairs++;
10782 pg_pairs++;
10783 }
10784 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10785 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10786 /* Perform endian conversion if necessary */
10787 sgl->word0 = cpu_to_le32(sgl->word0);
10788
10789 if (!phba->sli4_hba.intr_enable)
10790 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10791 else {
10792 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10793 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10794 }
10795 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10796 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10797 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10798 if (rc != MBX_TIMEOUT)
10799 lpfc_sli4_mbox_cmd_free(phba, mbox);
10800 if (shdr_status || shdr_add_status || rc) {
10801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10802 "2564 POST_SGL_BLOCK mailbox command failed "
10803 "status x%x add_status x%x mbx status x%x\n",
10804 shdr_status, shdr_add_status, rc);
10805 rc = -ENXIO;
10806 }
10807 return rc;
10808}
10809
10810/**
10811 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10812 * @phba: pointer to lpfc_hba struct that the frame was received on
10813 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10814 *
10815 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10816 * valid type of frame that the LPFC driver will handle. This function will
10817 * return a zero if the frame is a valid frame or a non zero value when the
10818 * frame does not pass the check.
10819 **/
10820static int
10821lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10822{
10823 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10824 char *type_names[] = FC_TYPE_NAMES_INIT;
10825 struct fc_vft_header *fc_vft_hdr;
10826
10827 switch (fc_hdr->fh_r_ctl) {
10828 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10829 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10830 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10831 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10832 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10833 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10834 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10835 case FC_RCTL_DD_CMD_STATUS: /* command status */
10836 case FC_RCTL_ELS_REQ: /* extended link services request */
10837 case FC_RCTL_ELS_REP: /* extended link services reply */
10838 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10839 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10840 case FC_RCTL_BA_NOP: /* basic link service NOP */
10841 case FC_RCTL_BA_ABTS: /* basic link service abort */
10842 case FC_RCTL_BA_RMC: /* remove connection */
10843 case FC_RCTL_BA_ACC: /* basic accept */
10844 case FC_RCTL_BA_RJT: /* basic reject */
10845 case FC_RCTL_BA_PRMT:
10846 case FC_RCTL_ACK_1: /* acknowledge_1 */
10847 case FC_RCTL_ACK_0: /* acknowledge_0 */
10848 case FC_RCTL_P_RJT: /* port reject */
10849 case FC_RCTL_F_RJT: /* fabric reject */
10850 case FC_RCTL_P_BSY: /* port busy */
10851 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10852 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10853 case FC_RCTL_LCR: /* link credit reset */
10854 case FC_RCTL_END: /* end */
10855 break;
10856 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10857 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10858 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10859 return lpfc_fc_frame_check(phba, fc_hdr);
10860 default:
10861 goto drop;
10862 }
10863 switch (fc_hdr->fh_type) {
10864 case FC_TYPE_BLS:
10865 case FC_TYPE_ELS:
10866 case FC_TYPE_FCP:
10867 case FC_TYPE_CT:
10868 break;
10869 case FC_TYPE_IP:
10870 case FC_TYPE_ILS:
10871 default:
10872 goto drop;
10873 }
10874 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10875 "2538 Received frame rctl:%s type:%s\n",
10876 rctl_names[fc_hdr->fh_r_ctl],
10877 type_names[fc_hdr->fh_type]);
10878 return 0;
10879drop:
10880 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10881 "2539 Dropped frame rctl:%s type:%s\n",
10882 rctl_names[fc_hdr->fh_r_ctl],
10883 type_names[fc_hdr->fh_type]);
10884 return 1;
10885}
10886
10887/**
10888 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10889 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10890 *
10891 * This function processes the FC header to retrieve the VFI from the VF
10892 * header, if one exists. This function will return the VFI if one exists
10893 * or 0 if no VSAN Header exists.
10894 **/
10895static uint32_t
10896lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10897{
10898 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10899
10900 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10901 return 0;
10902 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10903}
10904
10905/**
10906 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10907 * @phba: Pointer to the HBA structure to search for the vport on
10908 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10909 * @fcfi: The FC Fabric ID that the frame came from
10910 *
10911 * This function searches the @phba for a vport that matches the content of the
10912 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10913 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10914 * returns the matching vport pointer or NULL if unable to match frame to a
10915 * vport.
10916 **/
10917static struct lpfc_vport *
10918lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10919 uint16_t fcfi)
10920{
10921 struct lpfc_vport **vports;
10922 struct lpfc_vport *vport = NULL;
10923 int i;
10924 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10925 fc_hdr->fh_d_id[1] << 8 |
10926 fc_hdr->fh_d_id[2]);
10927
10928 vports = lpfc_create_vport_work_array(phba);
10929 if (vports != NULL)
10930 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10931 if (phba->fcf.fcfi == fcfi &&
10932 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10933 vports[i]->fc_myDID == did) {
10934 vport = vports[i];
10935 break;
10936 }
10937 }
10938 lpfc_destroy_vport_work_array(phba, vports);
10939 return vport;
10940}
10941
45ed1190
JS
10942/**
10943 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
10944 * @vport: The vport to work on.
10945 *
10946 * This function updates the receive sequence time stamp for this vport. The
10947 * receive sequence time stamp indicates the time that the last frame of the
10948 * the sequence that has been idle for the longest amount of time was received.
10949 * the driver uses this time stamp to indicate if any received sequences have
10950 * timed out.
10951 **/
10952void
10953lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
10954{
10955 struct lpfc_dmabuf *h_buf;
10956 struct hbq_dmabuf *dmabuf = NULL;
10957
10958 /* get the oldest sequence on the rcv list */
10959 h_buf = list_get_first(&vport->rcv_buffer_list,
10960 struct lpfc_dmabuf, list);
10961 if (!h_buf)
10962 return;
10963 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10964 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
10965}
10966
10967/**
10968 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
10969 * @vport: The vport that the received sequences were sent to.
10970 *
10971 * This function cleans up all outstanding received sequences. This is called
10972 * by the driver when a link event or user action invalidates all the received
10973 * sequences.
10974 **/
10975void
10976lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
10977{
10978 struct lpfc_dmabuf *h_buf, *hnext;
10979 struct lpfc_dmabuf *d_buf, *dnext;
10980 struct hbq_dmabuf *dmabuf = NULL;
10981
10982 /* start with the oldest sequence on the rcv list */
10983 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
10984 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10985 list_del_init(&dmabuf->hbuf.list);
10986 list_for_each_entry_safe(d_buf, dnext,
10987 &dmabuf->dbuf.list, list) {
10988 list_del_init(&d_buf->list);
10989 lpfc_in_buf_free(vport->phba, d_buf);
10990 }
10991 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
10992 }
10993}
10994
10995/**
10996 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
10997 * @vport: The vport that the received sequences were sent to.
10998 *
10999 * This function determines whether any received sequences have timed out by
11000 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
11001 * indicates that there is at least one timed out sequence this routine will
11002 * go through the received sequences one at a time from most inactive to most
11003 * active to determine which ones need to be cleaned up. Once it has determined
11004 * that a sequence needs to be cleaned up it will simply free up the resources
11005 * without sending an abort.
11006 **/
11007void
11008lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
11009{
11010 struct lpfc_dmabuf *h_buf, *hnext;
11011 struct lpfc_dmabuf *d_buf, *dnext;
11012 struct hbq_dmabuf *dmabuf = NULL;
11013 unsigned long timeout;
11014 int abort_count = 0;
11015
11016 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11017 vport->rcv_buffer_time_stamp);
11018 if (list_empty(&vport->rcv_buffer_list) ||
11019 time_before(jiffies, timeout))
11020 return;
11021 /* start with the oldest sequence on the rcv list */
11022 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
11023 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11024 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
11025 dmabuf->time_stamp);
11026 if (time_before(jiffies, timeout))
11027 break;
11028 abort_count++;
11029 list_del_init(&dmabuf->hbuf.list);
11030 list_for_each_entry_safe(d_buf, dnext,
11031 &dmabuf->dbuf.list, list) {
11032 list_del_init(&d_buf->list);
11033 lpfc_in_buf_free(vport->phba, d_buf);
11034 }
11035 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
11036 }
11037 if (abort_count)
11038 lpfc_update_rcv_time_stamp(vport);
11039}
11040
4f774513
JS
11041/**
11042 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
11043 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
11044 *
11045 * This function searches through the existing incomplete sequences that have
11046 * been sent to this @vport. If the frame matches one of the incomplete
11047 * sequences then the dbuf in the @dmabuf is added to the list of frames that
11048 * make up that sequence. If no sequence is found that matches this frame then
11049 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
11050 * This function returns a pointer to the first dmabuf in the sequence list that
11051 * the frame was linked to.
11052 **/
11053static struct hbq_dmabuf *
11054lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
11055{
11056 struct fc_frame_header *new_hdr;
11057 struct fc_frame_header *temp_hdr;
11058 struct lpfc_dmabuf *d_buf;
11059 struct lpfc_dmabuf *h_buf;
11060 struct hbq_dmabuf *seq_dmabuf = NULL;
11061 struct hbq_dmabuf *temp_dmabuf = NULL;
11062
4d9ab994 11063 INIT_LIST_HEAD(&dmabuf->dbuf.list);
45ed1190 11064 dmabuf->time_stamp = jiffies;
4f774513
JS
11065 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11066 /* Use the hdr_buf to find the sequence that this frame belongs to */
11067 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11068 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11069 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11070 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11071 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11072 continue;
11073 /* found a pending sequence that matches this frame */
11074 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11075 break;
11076 }
11077 if (!seq_dmabuf) {
11078 /*
11079 * This indicates first frame received for this sequence.
11080 * Queue the buffer on the vport's rcv_buffer_list.
11081 */
11082 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
45ed1190 11083 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11084 return dmabuf;
11085 }
11086 temp_hdr = seq_dmabuf->hbuf.virt;
eeead811
JS
11087 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
11088 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4d9ab994
JS
11089 list_del_init(&seq_dmabuf->hbuf.list);
11090 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
11091 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
45ed1190 11092 lpfc_update_rcv_time_stamp(vport);
4f774513
JS
11093 return dmabuf;
11094 }
45ed1190
JS
11095 /* move this sequence to the tail to indicate a young sequence */
11096 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
11097 seq_dmabuf->time_stamp = jiffies;
11098 lpfc_update_rcv_time_stamp(vport);
eeead811
JS
11099 if (list_empty(&seq_dmabuf->dbuf.list)) {
11100 temp_hdr = dmabuf->hbuf.virt;
11101 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
11102 return seq_dmabuf;
11103 }
4f774513
JS
11104 /* find the correct place in the sequence to insert this frame */
11105 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
11106 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11107 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
11108 /*
11109 * If the frame's sequence count is greater than the frame on
11110 * the list then insert the frame right after this frame
11111 */
eeead811
JS
11112 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
11113 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
4f774513
JS
11114 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
11115 return seq_dmabuf;
11116 }
11117 }
11118 return NULL;
11119}
11120
6669f9bb
JS
11121/**
11122 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
11123 * @vport: pointer to a vitural port
11124 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11125 *
11126 * This function tries to abort from the partially assembed sequence, described
11127 * by the information from basic abbort @dmabuf. It checks to see whether such
11128 * partially assembled sequence held by the driver. If so, it shall free up all
11129 * the frames from the partially assembled sequence.
11130 *
11131 * Return
11132 * true -- if there is matching partially assembled sequence present and all
11133 * the frames freed with the sequence;
11134 * false -- if there is no matching partially assembled sequence present so
11135 * nothing got aborted in the lower layer driver
11136 **/
11137static bool
11138lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
11139 struct hbq_dmabuf *dmabuf)
11140{
11141 struct fc_frame_header *new_hdr;
11142 struct fc_frame_header *temp_hdr;
11143 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
11144 struct hbq_dmabuf *seq_dmabuf = NULL;
11145
11146 /* Use the hdr_buf to find the sequence that matches this frame */
11147 INIT_LIST_HEAD(&dmabuf->dbuf.list);
11148 INIT_LIST_HEAD(&dmabuf->hbuf.list);
11149 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11150 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
11151 temp_hdr = (struct fc_frame_header *)h_buf->virt;
11152 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
11153 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
11154 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
11155 continue;
11156 /* found a pending sequence that matches this frame */
11157 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
11158 break;
11159 }
11160
11161 /* Free up all the frames from the partially assembled sequence */
11162 if (seq_dmabuf) {
11163 list_for_each_entry_safe(d_buf, n_buf,
11164 &seq_dmabuf->dbuf.list, list) {
11165 list_del_init(&d_buf->list);
11166 lpfc_in_buf_free(vport->phba, d_buf);
11167 }
11168 return true;
11169 }
11170 return false;
11171}
11172
11173/**
11174 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
11175 * @phba: Pointer to HBA context object.
11176 * @cmd_iocbq: pointer to the command iocbq structure.
11177 * @rsp_iocbq: pointer to the response iocbq structure.
11178 *
11179 * This function handles the sequence abort accept iocb command complete
11180 * event. It properly releases the memory allocated to the sequence abort
11181 * accept iocb.
11182 **/
11183static void
11184lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
11185 struct lpfc_iocbq *cmd_iocbq,
11186 struct lpfc_iocbq *rsp_iocbq)
11187{
11188 if (cmd_iocbq)
11189 lpfc_sli_release_iocbq(phba, cmd_iocbq);
11190}
11191
11192/**
11193 * lpfc_sli4_seq_abort_acc - Accept sequence abort
11194 * @phba: Pointer to HBA context object.
11195 * @fc_hdr: pointer to a FC frame header.
11196 *
11197 * This function sends a basic accept to a previous unsol sequence abort
11198 * event after aborting the sequence handling.
11199 **/
11200static void
11201lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
11202 struct fc_frame_header *fc_hdr)
11203{
11204 struct lpfc_iocbq *ctiocb = NULL;
11205 struct lpfc_nodelist *ndlp;
5ffc266e
JS
11206 uint16_t oxid, rxid;
11207 uint32_t sid, fctl;
6669f9bb
JS
11208 IOCB_t *icmd;
11209
11210 if (!lpfc_is_link_up(phba))
11211 return;
11212
11213 sid = sli4_sid_from_fc_hdr(fc_hdr);
11214 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
5ffc266e 11215 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
6669f9bb
JS
11216
11217 ndlp = lpfc_findnode_did(phba->pport, sid);
11218 if (!ndlp) {
11219 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11220 "1268 Find ndlp returned NULL for oxid:x%x "
11221 "SID:x%x\n", oxid, sid);
11222 return;
11223 }
11224
11225 /* Allocate buffer for acc iocb */
11226 ctiocb = lpfc_sli_get_iocbq(phba);
11227 if (!ctiocb)
11228 return;
11229
5ffc266e
JS
11230 /* Extract the F_CTL field from FC_HDR */
11231 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
11232
6669f9bb 11233 icmd = &ctiocb->iocb;
6669f9bb 11234 icmd->un.xseq64.bdl.bdeSize = 0;
5ffc266e 11235 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
6669f9bb
JS
11236 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11237 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11238 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11239
11240 /* Fill in the rest of iocb fields */
11241 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11242 icmd->ulpBdeCount = 0;
11243 icmd->ulpLe = 1;
11244 icmd->ulpClass = CLASS3;
11245 icmd->ulpContext = ndlp->nlp_rpi;
6669f9bb 11246
6669f9bb
JS
11247 ctiocb->iocb_cmpl = NULL;
11248 ctiocb->vport = phba->pport;
11249 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11250
5ffc266e
JS
11251 if (fctl & FC_FC_EX_CTX) {
11252 /* ABTS sent by responder to CT exchange, construction
11253 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
11254 * field and RX_ID from ABTS for RX_ID field.
11255 */
11256 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_RSP);
11257 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, rxid);
11258 ctiocb->sli4_xritag = oxid;
11259 } else {
11260 /* ABTS sent by initiator to CT exchange, construction
11261 * of BA_ACC will need to allocate a new XRI as for the
11262 * XRI_TAG and RX_ID fields.
11263 */
11264 bf_set(lpfc_abts_orig, &icmd->un.bls_acc, LPFC_ABTS_UNSOL_INT);
11265 bf_set(lpfc_abts_rxid, &icmd->un.bls_acc, NO_XRI);
11266 ctiocb->sli4_xritag = NO_XRI;
11267 }
11268 bf_set(lpfc_abts_oxid, &icmd->un.bls_acc, oxid);
11269
6669f9bb
JS
11270 /* Xmit CT abts accept on exchange <xid> */
11271 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11272 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11273 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11274 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11275}
11276
11277/**
11278 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11279 * @vport: Pointer to the vport on which this sequence was received
11280 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11281 *
11282 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11283 * receive sequence is only partially assembed by the driver, it shall abort
11284 * the partially assembled frames for the sequence. Otherwise, if the
11285 * unsolicited receive sequence has been completely assembled and passed to
11286 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11287 * unsolicited sequence has been aborted. After that, it will issue a basic
11288 * accept to accept the abort.
11289 **/
11290void
11291lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11292 struct hbq_dmabuf *dmabuf)
11293{
11294 struct lpfc_hba *phba = vport->phba;
11295 struct fc_frame_header fc_hdr;
5ffc266e 11296 uint32_t fctl;
6669f9bb
JS
11297 bool abts_par;
11298
6669f9bb
JS
11299 /* Make a copy of fc_hdr before the dmabuf being released */
11300 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
5ffc266e 11301 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
6669f9bb 11302
5ffc266e
JS
11303 if (fctl & FC_FC_EX_CTX) {
11304 /*
11305 * ABTS sent by responder to exchange, just free the buffer
11306 */
6669f9bb 11307 lpfc_in_buf_free(phba, &dmabuf->dbuf);
5ffc266e
JS
11308 } else {
11309 /*
11310 * ABTS sent by initiator to exchange, need to do cleanup
11311 */
11312 /* Try to abort partially assembled seq */
11313 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11314
11315 /* Send abort to ULP if partially seq abort failed */
11316 if (abts_par == false)
11317 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11318 else
11319 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11320 }
6669f9bb
JS
11321 /* Send basic accept (BA_ACC) to the abort requester */
11322 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11323}
11324
4f774513
JS
11325/**
11326 * lpfc_seq_complete - Indicates if a sequence is complete
11327 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11328 *
11329 * This function checks the sequence, starting with the frame described by
11330 * @dmabuf, to see if all the frames associated with this sequence are present.
11331 * the frames associated with this sequence are linked to the @dmabuf using the
11332 * dbuf list. This function looks for two major things. 1) That the first frame
11333 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11334 * set. 3) That there are no holes in the sequence count. The function will
11335 * return 1 when the sequence is complete, otherwise it will return 0.
11336 **/
11337static int
11338lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11339{
11340 struct fc_frame_header *hdr;
11341 struct lpfc_dmabuf *d_buf;
11342 struct hbq_dmabuf *seq_dmabuf;
11343 uint32_t fctl;
11344 int seq_count = 0;
11345
11346 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11347 /* make sure first fame of sequence has a sequence count of zero */
11348 if (hdr->fh_seq_cnt != seq_count)
11349 return 0;
11350 fctl = (hdr->fh_f_ctl[0] << 16 |
11351 hdr->fh_f_ctl[1] << 8 |
11352 hdr->fh_f_ctl[2]);
11353 /* If last frame of sequence we can return success. */
11354 if (fctl & FC_FC_END_SEQ)
11355 return 1;
11356 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11357 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11358 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11359 /* If there is a hole in the sequence count then fail. */
eeead811 11360 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
4f774513
JS
11361 return 0;
11362 fctl = (hdr->fh_f_ctl[0] << 16 |
11363 hdr->fh_f_ctl[1] << 8 |
11364 hdr->fh_f_ctl[2]);
11365 /* If last frame of sequence we can return success. */
11366 if (fctl & FC_FC_END_SEQ)
11367 return 1;
11368 }
11369 return 0;
11370}
11371
11372/**
11373 * lpfc_prep_seq - Prep sequence for ULP processing
11374 * @vport: Pointer to the vport on which this sequence was received
11375 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11376 *
11377 * This function takes a sequence, described by a list of frames, and creates
11378 * a list of iocbq structures to describe the sequence. This iocbq list will be
11379 * used to issue to the generic unsolicited sequence handler. This routine
11380 * returns a pointer to the first iocbq in the list. If the function is unable
11381 * to allocate an iocbq then it throw out the received frames that were not
11382 * able to be described and return a pointer to the first iocbq. If unable to
11383 * allocate any iocbqs (including the first) this function will return NULL.
11384 **/
11385static struct lpfc_iocbq *
11386lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11387{
11388 struct lpfc_dmabuf *d_buf, *n_buf;
11389 struct lpfc_iocbq *first_iocbq, *iocbq;
11390 struct fc_frame_header *fc_hdr;
11391 uint32_t sid;
eeead811 11392 struct ulp_bde64 *pbde;
4f774513
JS
11393
11394 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11395 /* remove from receive buffer list */
11396 list_del_init(&seq_dmabuf->hbuf.list);
45ed1190 11397 lpfc_update_rcv_time_stamp(vport);
4f774513 11398 /* get the Remote Port's SID */
6669f9bb 11399 sid = sli4_sid_from_fc_hdr(fc_hdr);
4f774513
JS
11400 /* Get an iocbq struct to fill in. */
11401 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11402 if (first_iocbq) {
11403 /* Initialize the first IOCB. */
8fa38513 11404 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
11405 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11406 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11407 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11408 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11409 vport->vpi + vport->phba->vpi_base;
11410 /* put the first buffer into the first IOCBq */
11411 first_iocbq->context2 = &seq_dmabuf->dbuf;
11412 first_iocbq->context3 = NULL;
11413 first_iocbq->iocb.ulpBdeCount = 1;
11414 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11415 LPFC_DATA_BUF_SIZE;
11416 first_iocbq->iocb.un.rcvels.remoteID = sid;
8fa38513 11417 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11418 bf_get(lpfc_rcqe_length,
11419 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11420 }
11421 iocbq = first_iocbq;
11422 /*
11423 * Each IOCBq can have two Buffers assigned, so go through the list
11424 * of buffers for this sequence and save two buffers in each IOCBq
11425 */
11426 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11427 if (!iocbq) {
11428 lpfc_in_buf_free(vport->phba, d_buf);
11429 continue;
11430 }
11431 if (!iocbq->context3) {
11432 iocbq->context3 = d_buf;
11433 iocbq->iocb.ulpBdeCount++;
eeead811
JS
11434 pbde = (struct ulp_bde64 *)
11435 &iocbq->iocb.unsli3.sli3Words[4];
11436 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
8fa38513 11437 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11438 bf_get(lpfc_rcqe_length,
11439 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11440 } else {
11441 iocbq = lpfc_sli_get_iocbq(vport->phba);
11442 if (!iocbq) {
11443 if (first_iocbq) {
11444 first_iocbq->iocb.ulpStatus =
11445 IOSTAT_FCP_RSP_ERROR;
11446 first_iocbq->iocb.un.ulpWord[4] =
11447 IOERR_NO_RESOURCES;
11448 }
11449 lpfc_in_buf_free(vport->phba, d_buf);
11450 continue;
11451 }
11452 iocbq->context2 = d_buf;
11453 iocbq->context3 = NULL;
11454 iocbq->iocb.ulpBdeCount = 1;
11455 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11456 LPFC_DATA_BUF_SIZE;
8fa38513 11457 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11458 bf_get(lpfc_rcqe_length,
11459 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11460 iocbq->iocb.un.rcvels.remoteID = sid;
11461 list_add_tail(&iocbq->list, &first_iocbq->list);
11462 }
11463 }
11464 return first_iocbq;
11465}
11466
6669f9bb
JS
11467static void
11468lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11469 struct hbq_dmabuf *seq_dmabuf)
11470{
11471 struct fc_frame_header *fc_hdr;
11472 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11473 struct lpfc_hba *phba = vport->phba;
11474
11475 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11476 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11477 if (!iocbq) {
11478 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11479 "2707 Ring %d handler: Failed to allocate "
11480 "iocb Rctl x%x Type x%x received\n",
11481 LPFC_ELS_RING,
11482 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11483 return;
11484 }
11485 if (!lpfc_complete_unsol_iocb(phba,
11486 &phba->sli.ring[LPFC_ELS_RING],
11487 iocbq, fc_hdr->fh_r_ctl,
11488 fc_hdr->fh_type))
11489 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11490 "2540 Ring %d handler: unexpected Rctl "
11491 "x%x Type x%x received\n",
11492 LPFC_ELS_RING,
11493 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11494
11495 /* Free iocb created in lpfc_prep_seq */
11496 list_for_each_entry_safe(curr_iocb, next_iocb,
11497 &iocbq->list, list) {
11498 list_del_init(&curr_iocb->list);
11499 lpfc_sli_release_iocbq(phba, curr_iocb);
11500 }
11501 lpfc_sli_release_iocbq(phba, iocbq);
11502}
11503
4f774513
JS
11504/**
11505 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11506 * @phba: Pointer to HBA context object.
11507 *
11508 * This function is called with no lock held. This function processes all
11509 * the received buffers and gives it to upper layers when a received buffer
11510 * indicates that it is the final frame in the sequence. The interrupt
11511 * service routine processes received buffers at interrupt contexts and adds
11512 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11513 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11514 * appropriate receive function when the final frame in a sequence is received.
11515 **/
4d9ab994
JS
11516void
11517lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11518 struct hbq_dmabuf *dmabuf)
4f774513 11519{
4d9ab994 11520 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
11521 struct fc_frame_header *fc_hdr;
11522 struct lpfc_vport *vport;
11523 uint32_t fcfi;
4f774513 11524
4f774513 11525 /* Process each received buffer */
4d9ab994
JS
11526 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11527 /* check to see if this a valid type of frame */
11528 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11529 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11530 return;
11531 }
11532 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11533 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
c868595d 11534 if (!vport || !(vport->vpi_state & LPFC_VPI_REGISTERED)) {
4d9ab994
JS
11535 /* throw out the frame */
11536 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11537 return;
11538 }
6669f9bb
JS
11539 /* Handle the basic abort sequence (BA_ABTS) event */
11540 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11541 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11542 return;
11543 }
11544
4d9ab994
JS
11545 /* Link this frame */
11546 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11547 if (!seq_dmabuf) {
11548 /* unable to add frame to vport - throw it out */
11549 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11550 return;
11551 }
11552 /* If not last frame in sequence continue processing frames. */
def9c7a9 11553 if (!lpfc_seq_complete(seq_dmabuf))
4d9ab994 11554 return;
def9c7a9 11555
6669f9bb
JS
11556 /* Send the complete sequence to the upper layer protocol */
11557 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 11558}
6fb120a7
JS
11559
11560/**
11561 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11562 * @phba: pointer to lpfc hba data structure.
11563 *
11564 * This routine is invoked to post rpi header templates to the
11565 * HBA consistent with the SLI-4 interface spec. This routine
11566 * posts a PAGE_SIZE memory region to the port to hold up to
11567 * PAGE_SIZE modulo 64 rpi context headers.
11568 *
11569 * This routine does not require any locks. It's usage is expected
11570 * to be driver load or reset recovery when the driver is
11571 * sequential.
11572 *
11573 * Return codes
af901ca1 11574 * 0 - successful
6fb120a7
JS
11575 * EIO - The mailbox failed to complete successfully.
11576 * When this error occurs, the driver is not guaranteed
11577 * to have any rpi regions posted to the device and
11578 * must either attempt to repost the regions or take a
11579 * fatal error.
11580 **/
11581int
11582lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11583{
11584 struct lpfc_rpi_hdr *rpi_page;
11585 uint32_t rc = 0;
11586
11587 /* Post all rpi memory regions to the port. */
11588 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11589 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11590 if (rc != MBX_SUCCESS) {
11591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11592 "2008 Error %d posting all rpi "
11593 "headers\n", rc);
11594 rc = -EIO;
11595 break;
11596 }
11597 }
11598
11599 return rc;
11600}
11601
11602/**
11603 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11604 * @phba: pointer to lpfc hba data structure.
11605 * @rpi_page: pointer to the rpi memory region.
11606 *
11607 * This routine is invoked to post a single rpi header to the
11608 * HBA consistent with the SLI-4 interface spec. This memory region
11609 * maps up to 64 rpi context regions.
11610 *
11611 * Return codes
af901ca1 11612 * 0 - successful
6fb120a7
JS
11613 * ENOMEM - No available memory
11614 * EIO - The mailbox failed to complete successfully.
11615 **/
11616int
11617lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11618{
11619 LPFC_MBOXQ_t *mboxq;
11620 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11621 uint32_t rc = 0;
11622 uint32_t mbox_tmo;
11623 uint32_t shdr_status, shdr_add_status;
11624 union lpfc_sli4_cfg_shdr *shdr;
11625
11626 /* The port is notified of the header region via a mailbox command. */
11627 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11628 if (!mboxq) {
11629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11630 "2001 Unable to allocate memory for issuing "
11631 "SLI_CONFIG_SPECIAL mailbox command\n");
11632 return -ENOMEM;
11633 }
11634
11635 /* Post all rpi memory regions to the port. */
11636 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11637 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11638 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11639 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11640 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11641 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11642 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11643 hdr_tmpl, rpi_page->page_count);
11644 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11645 rpi_page->start_rpi);
11646 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11647 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 11648 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
11649 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11650 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11651 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11652 if (rc != MBX_TIMEOUT)
11653 mempool_free(mboxq, phba->mbox_mem_pool);
11654 if (shdr_status || shdr_add_status || rc) {
11655 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11656 "2514 POST_RPI_HDR mailbox failed with "
11657 "status x%x add_status x%x, mbx status x%x\n",
11658 shdr_status, shdr_add_status, rc);
11659 rc = -ENXIO;
11660 }
11661 return rc;
11662}
11663
11664/**
11665 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11666 * @phba: pointer to lpfc hba data structure.
11667 *
11668 * This routine is invoked to post rpi header templates to the
11669 * HBA consistent with the SLI-4 interface spec. This routine
11670 * posts a PAGE_SIZE memory region to the port to hold up to
11671 * PAGE_SIZE modulo 64 rpi context headers.
11672 *
11673 * Returns
af901ca1 11674 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
6fb120a7
JS
11675 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11676 **/
11677int
11678lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11679{
11680 int rpi;
11681 uint16_t max_rpi, rpi_base, rpi_limit;
11682 uint16_t rpi_remaining;
11683 struct lpfc_rpi_hdr *rpi_hdr;
11684
11685 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11686 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11687 rpi_limit = phba->sli4_hba.next_rpi;
11688
11689 /*
11690 * The valid rpi range is not guaranteed to be zero-based. Start
11691 * the search at the rpi_base as reported by the port.
11692 */
11693 spin_lock_irq(&phba->hbalock);
11694 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11695 if (rpi >= rpi_limit || rpi < rpi_base)
11696 rpi = LPFC_RPI_ALLOC_ERROR;
11697 else {
11698 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11699 phba->sli4_hba.max_cfg_param.rpi_used++;
11700 phba->sli4_hba.rpi_count++;
11701 }
11702
11703 /*
11704 * Don't try to allocate more rpi header regions if the device limit
11705 * on available rpis max has been exhausted.
11706 */
11707 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11708 (phba->sli4_hba.rpi_count >= max_rpi)) {
11709 spin_unlock_irq(&phba->hbalock);
11710 return rpi;
11711 }
11712
11713 /*
11714 * If the driver is running low on rpi resources, allocate another
11715 * page now. Note that the next_rpi value is used because
11716 * it represents how many are actually in use whereas max_rpi notes
11717 * how many are supported max by the device.
11718 */
11719 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11720 phba->sli4_hba.rpi_count;
11721 spin_unlock_irq(&phba->hbalock);
11722 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11723 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11724 if (!rpi_hdr) {
11725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11726 "2002 Error Could not grow rpi "
11727 "count\n");
11728 } else {
11729 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11730 }
11731 }
11732
11733 return rpi;
11734}
11735
11736/**
11737 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11738 * @phba: pointer to lpfc hba data structure.
11739 *
11740 * This routine is invoked to release an rpi to the pool of
11741 * available rpis maintained by the driver.
11742 **/
11743void
11744lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11745{
11746 spin_lock_irq(&phba->hbalock);
11747 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11748 phba->sli4_hba.rpi_count--;
11749 phba->sli4_hba.max_cfg_param.rpi_used--;
11750 spin_unlock_irq(&phba->hbalock);
11751}
11752
11753/**
11754 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11755 * @phba: pointer to lpfc hba data structure.
11756 *
11757 * This routine is invoked to remove the memory region that
11758 * provided rpi via a bitmask.
11759 **/
11760void
11761lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11762{
11763 kfree(phba->sli4_hba.rpi_bmask);
11764}
11765
11766/**
11767 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11768 * @phba: pointer to lpfc hba data structure.
11769 *
11770 * This routine is invoked to remove the memory region that
11771 * provided rpi via a bitmask.
11772 **/
11773int
11774lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11775{
11776 LPFC_MBOXQ_t *mboxq;
11777 struct lpfc_hba *phba = ndlp->phba;
11778 int rc;
11779
11780 /* The port is notified of the header region via a mailbox command. */
11781 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11782 if (!mboxq)
11783 return -ENOMEM;
11784
11785 /* Post all rpi memory regions to the port. */
11786 lpfc_resume_rpi(mboxq, ndlp);
11787 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11788 if (rc == MBX_NOT_FINISHED) {
11789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11790 "2010 Resume RPI Mailbox failed "
11791 "status %d, mbxStatus x%x\n", rc,
11792 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11793 mempool_free(mboxq, phba->mbox_mem_pool);
11794 return -EIO;
11795 }
11796 return 0;
11797}
11798
11799/**
11800 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11801 * @phba: pointer to lpfc hba data structure.
11802 * @vpi: vpi value to activate with the port.
11803 *
11804 * This routine is invoked to activate a vpi with the
11805 * port when the host intends to use vports with a
11806 * nonzero vpi.
11807 *
11808 * Returns:
11809 * 0 success
11810 * -Evalue otherwise
11811 **/
11812int
11813lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11814{
11815 LPFC_MBOXQ_t *mboxq;
11816 int rc = 0;
6a9c52cf 11817 int retval = MBX_SUCCESS;
6fb120a7
JS
11818 uint32_t mbox_tmo;
11819
11820 if (vpi == 0)
11821 return -EINVAL;
11822 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11823 if (!mboxq)
11824 return -ENOMEM;
1c6834a7 11825 lpfc_init_vpi(phba, mboxq, vpi);
6fb120a7
JS
11826 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11827 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7
JS
11828 if (rc != MBX_SUCCESS) {
11829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11830 "2022 INIT VPI Mailbox failed "
11831 "status %d, mbxStatus x%x\n", rc,
11832 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 11833 retval = -EIO;
6fb120a7 11834 }
6a9c52cf
JS
11835 if (rc != MBX_TIMEOUT)
11836 mempool_free(mboxq, phba->mbox_mem_pool);
11837
11838 return retval;
6fb120a7
JS
11839}
11840
11841/**
11842 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11843 * @phba: pointer to lpfc hba data structure.
11844 * @mboxq: Pointer to mailbox object.
11845 *
11846 * This routine is invoked to manually add a single FCF record. The caller
11847 * must pass a completely initialized FCF_Record. This routine takes
11848 * care of the nonembedded mailbox operations.
11849 **/
11850static void
11851lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11852{
11853 void *virt_addr;
11854 union lpfc_sli4_cfg_shdr *shdr;
11855 uint32_t shdr_status, shdr_add_status;
11856
11857 virt_addr = mboxq->sge_array->addr[0];
11858 /* The IOCTL status is embedded in the mailbox subheader. */
11859 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11860 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11861 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11862
11863 if ((shdr_status || shdr_add_status) &&
11864 (shdr_status != STATUS_FCF_IN_USE))
11865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11866 "2558 ADD_FCF_RECORD mailbox failed with "
11867 "status x%x add_status x%x\n",
11868 shdr_status, shdr_add_status);
11869
11870 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11871}
11872
11873/**
11874 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11875 * @phba: pointer to lpfc hba data structure.
11876 * @fcf_record: pointer to the initialized fcf record to add.
11877 *
11878 * This routine is invoked to manually add a single FCF record. The caller
11879 * must pass a completely initialized FCF_Record. This routine takes
11880 * care of the nonembedded mailbox operations.
11881 **/
11882int
11883lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11884{
11885 int rc = 0;
11886 LPFC_MBOXQ_t *mboxq;
11887 uint8_t *bytep;
11888 void *virt_addr;
11889 dma_addr_t phys_addr;
11890 struct lpfc_mbx_sge sge;
11891 uint32_t alloc_len, req_len;
11892 uint32_t fcfindex;
11893
11894 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11895 if (!mboxq) {
11896 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11897 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11898 return -ENOMEM;
11899 }
11900
11901 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11902 sizeof(uint32_t);
11903
11904 /* Allocate DMA memory and set up the non-embedded mailbox command */
11905 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11906 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11907 req_len, LPFC_SLI4_MBX_NEMBED);
11908 if (alloc_len < req_len) {
11909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11910 "2523 Allocated DMA memory size (x%x) is "
11911 "less than the requested DMA memory "
11912 "size (x%x)\n", alloc_len, req_len);
11913 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11914 return -ENOMEM;
11915 }
11916
11917 /*
11918 * Get the first SGE entry from the non-embedded DMA memory. This
11919 * routine only uses a single SGE.
11920 */
11921 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11922 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
11923 virt_addr = mboxq->sge_array->addr[0];
11924 /*
11925 * Configure the FCF record for FCFI 0. This is the driver's
11926 * hardcoded default and gets used in nonFIP mode.
11927 */
11928 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11929 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11930 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11931
11932 /*
11933 * Copy the fcf_index and the FCF Record Data. The data starts after
11934 * the FCoE header plus word10. The data copy needs to be endian
11935 * correct.
11936 */
11937 bytep += sizeof(uint32_t);
11938 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11939 mboxq->vport = phba->pport;
11940 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11941 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11942 if (rc == MBX_NOT_FINISHED) {
11943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11944 "2515 ADD_FCF_RECORD mailbox failed with "
11945 "status 0x%x\n", rc);
11946 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11947 rc = -EIO;
11948 } else
11949 rc = 0;
11950
11951 return rc;
11952}
11953
11954/**
11955 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11956 * @phba: pointer to lpfc hba data structure.
11957 * @fcf_record: pointer to the fcf record to write the default data.
11958 * @fcf_index: FCF table entry index.
11959 *
11960 * This routine is invoked to build the driver's default FCF record. The
11961 * values used are hardcoded. This routine handles memory initialization.
11962 *
11963 **/
11964void
11965lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11966 struct fcf_record *fcf_record,
11967 uint16_t fcf_index)
11968{
11969 memset(fcf_record, 0, sizeof(struct fcf_record));
11970 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11971 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11972 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11973 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11974 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11975 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11976 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11977 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11978 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11979 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11980 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11981 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11982 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 11983 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
11984 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11985 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11986 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11987 /* Set the VLAN bit map */
11988 if (phba->valid_vlan) {
11989 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11990 = 1 << (phba->vlan_id % 8);
11991 }
11992}
11993
11994/**
11995 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11996 * @phba: pointer to lpfc hba data structure.
11997 * @fcf_index: FCF table entry offset.
11998 *
11999 * This routine is invoked to read up to @fcf_num of FCF record from the
12000 * device starting with the given @fcf_index.
12001 **/
12002int
12003lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
12004{
12005 int rc = 0, error;
12006 LPFC_MBOXQ_t *mboxq;
6fb120a7 12007
32b9793f 12008 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
12009 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12010 if (!mboxq) {
12011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12012 "2000 Failed to allocate mbox for "
12013 "READ_FCF cmd\n");
4d9ab994
JS
12014 error = -ENOMEM;
12015 goto fail_fcfscan;
6fb120a7 12016 }
ecfd03c6
JS
12017 /* Construct the read FCF record mailbox command */
12018 rc = lpfc_sli4_mbx_read_fcf_record(phba, mboxq, fcf_index);
12019 if (rc) {
12020 error = -EINVAL;
4d9ab994 12021 goto fail_fcfscan;
6fb120a7 12022 }
ecfd03c6 12023 /* Issue the mailbox command asynchronously */
6fb120a7
JS
12024 mboxq->vport = phba->pport;
12025 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
12026 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
ecfd03c6 12027 if (rc == MBX_NOT_FINISHED)
6fb120a7 12028 error = -EIO;
ecfd03c6 12029 else {
32b9793f
JS
12030 spin_lock_irq(&phba->hbalock);
12031 phba->hba_flag |= FCF_DISC_INPROGRESS;
12032 spin_unlock_irq(&phba->hbalock);
6fb120a7 12033 error = 0;
32b9793f 12034 }
4d9ab994
JS
12035fail_fcfscan:
12036 if (error) {
12037 if (mboxq)
12038 lpfc_sli4_mbox_cmd_free(phba, mboxq);
12039 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
12040 spin_lock_irq(&phba->hbalock);
12041 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
12042 spin_unlock_irq(&phba->hbalock);
12043 }
6fb120a7
JS
12044 return error;
12045}
a0c87cbd 12046
ecfd03c6
JS
12047/**
12048 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
12049 * @phba: pointer to lpfc hba data structure.
12050 *
12051 * This routine is the completion routine for the rediscover FCF table mailbox
12052 * command. If the mailbox command returned failure, it will try to stop the
12053 * FCF rediscover wait timer.
12054 **/
12055void
12056lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
12057{
12058 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12059 uint32_t shdr_status, shdr_add_status;
12060
12061 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12062
12063 shdr_status = bf_get(lpfc_mbox_hdr_status,
12064 &redisc_fcf->header.cfg_shdr.response);
12065 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
12066 &redisc_fcf->header.cfg_shdr.response);
12067 if (shdr_status || shdr_add_status) {
12068 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12069 "2746 Requesting for FCF rediscovery failed "
12070 "status x%x add_status x%x\n",
12071 shdr_status, shdr_add_status);
12072 /*
12073 * Request failed, last resort to re-try current
12074 * registered FCF entry
12075 */
12076 lpfc_retry_pport_discovery(phba);
12077 } else
12078 /*
12079 * Start FCF rediscovery wait timer for pending FCF
12080 * before rescan FCF record table.
12081 */
12082 lpfc_fcf_redisc_wait_start_timer(phba);
12083
12084 mempool_free(mbox, phba->mbox_mem_pool);
12085}
12086
12087/**
12088 * lpfc_sli4_redisc_all_fcf - Request to rediscover entire FCF table by port.
12089 * @phba: pointer to lpfc hba data structure.
12090 *
12091 * This routine is invoked to request for rediscovery of the entire FCF table
12092 * by the port.
12093 **/
12094int
12095lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
12096{
12097 LPFC_MBOXQ_t *mbox;
12098 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
12099 int rc, length;
12100
12101 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12102 if (!mbox) {
12103 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12104 "2745 Failed to allocate mbox for "
12105 "requesting FCF rediscover.\n");
12106 return -ENOMEM;
12107 }
12108
12109 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
12110 sizeof(struct lpfc_sli4_cfg_mhdr));
12111 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12112 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
12113 length, LPFC_SLI4_MBX_EMBED);
12114
12115 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
12116 /* Set count to 0 for invalidating the entire FCF database */
12117 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
12118
12119 /* Issue the mailbox command asynchronously */
12120 mbox->vport = phba->pport;
12121 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
12122 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
12123
12124 if (rc == MBX_NOT_FINISHED) {
12125 mempool_free(mbox, phba->mbox_mem_pool);
12126 return -EIO;
12127 }
12128 return 0;
12129}
12130
a0c87cbd
JS
12131/**
12132 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
12133 * @phba: pointer to lpfc hba data structure.
12134 *
12135 * This function read region 23 and parse TLV for port status to
12136 * decide if the user disaled the port. If the TLV indicates the
12137 * port is disabled, the hba_flag is set accordingly.
12138 **/
12139void
12140lpfc_sli_read_link_ste(struct lpfc_hba *phba)
12141{
12142 LPFC_MBOXQ_t *pmb = NULL;
12143 MAILBOX_t *mb;
12144 uint8_t *rgn23_data = NULL;
12145 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
12146 int rc;
12147
12148 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12149 if (!pmb) {
12150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12151 "2600 lpfc_sli_read_serdes_param failed to"
12152 " allocate mailbox memory\n");
12153 goto out;
12154 }
12155 mb = &pmb->u.mb;
12156
12157 /* Get adapter Region 23 data */
12158 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
12159 if (!rgn23_data)
12160 goto out;
12161
12162 do {
12163 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
12164 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
12165
12166 if (rc != MBX_SUCCESS) {
12167 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12168 "2601 lpfc_sli_read_link_ste failed to"
12169 " read config region 23 rc 0x%x Status 0x%x\n",
12170 rc, mb->mbxStatus);
12171 mb->un.varDmp.word_cnt = 0;
12172 }
12173 /*
12174 * dump mem may return a zero when finished or we got a
12175 * mailbox error, either way we are done.
12176 */
12177 if (mb->un.varDmp.word_cnt == 0)
12178 break;
12179 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
12180 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
12181
12182 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
12183 rgn23_data + offset,
12184 mb->un.varDmp.word_cnt);
12185 offset += mb->un.varDmp.word_cnt;
12186 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
12187
12188 data_size = offset;
12189 offset = 0;
12190
12191 if (!data_size)
12192 goto out;
12193
12194 /* Check the region signature first */
12195 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
12196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12197 "2619 Config region 23 has bad signature\n");
12198 goto out;
12199 }
12200 offset += 4;
12201
12202 /* Check the data structure version */
12203 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
12204 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12205 "2620 Config region 23 has bad version\n");
12206 goto out;
12207 }
12208 offset += 4;
12209
12210 /* Parse TLV entries in the region */
12211 while (offset < data_size) {
12212 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
12213 break;
12214 /*
12215 * If the TLV is not driver specific TLV or driver id is
12216 * not linux driver id, skip the record.
12217 */
12218 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
12219 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
12220 (rgn23_data[offset + 3] != 0)) {
12221 offset += rgn23_data[offset + 1] * 4 + 4;
12222 continue;
12223 }
12224
12225 /* Driver found a driver specific TLV in the config region */
12226 sub_tlv_len = rgn23_data[offset + 1] * 4;
12227 offset += 4;
12228 tlv_offset = 0;
12229
12230 /*
12231 * Search for configured port state sub-TLV.
12232 */
12233 while ((offset < data_size) &&
12234 (tlv_offset < sub_tlv_len)) {
12235 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
12236 offset += 4;
12237 tlv_offset += 4;
12238 break;
12239 }
12240 if (rgn23_data[offset] != PORT_STE_TYPE) {
12241 offset += rgn23_data[offset + 1] * 4 + 4;
12242 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
12243 continue;
12244 }
12245
12246 /* This HBA contains PORT_STE configured */
12247 if (!rgn23_data[offset + 2])
12248 phba->hba_flag |= LINK_DISABLED;
12249
12250 goto out;
12251 }
12252 }
12253out:
12254 if (pmb)
12255 mempool_free(pmb, phba->mbox_mem_pool);
12256 kfree(rgn23_data);
12257 return;
12258}
695a814e
JS
12259
12260/**
12261 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
12262 * @vport: pointer to vport data structure.
12263 *
12264 * This function iterate through the mailboxq and clean up all REG_LOGIN
12265 * and REG_VPI mailbox commands associated with the vport. This function
12266 * is called when driver want to restart discovery of the vport due to
12267 * a Clear Virtual Link event.
12268 **/
12269void
12270lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
12271{
12272 struct lpfc_hba *phba = vport->phba;
12273 LPFC_MBOXQ_t *mb, *nextmb;
12274 struct lpfc_dmabuf *mp;
12275
12276 spin_lock_irq(&phba->hbalock);
12277 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
12278 if (mb->vport != vport)
12279 continue;
12280
12281 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
12282 (mb->u.mb.mbxCommand != MBX_REG_VPI))
12283 continue;
12284
12285 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
12286 mp = (struct lpfc_dmabuf *) (mb->context1);
12287 if (mp) {
12288 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
12289 kfree(mp);
12290 }
12291 }
12292 list_del(&mb->list);
12293 mempool_free(mb, phba->mbox_mem_pool);
12294 }
12295 mb = phba->sli.mbox_active;
12296 if (mb && (mb->vport == vport)) {
12297 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
12298 (mb->u.mb.mbxCommand == MBX_REG_VPI))
12299 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12300 }
12301 spin_unlock_irq(&phba->hbalock);
12302}
12303
This page took 1.573143 seconds and 5 git commands to generate.