[SCSI] lpfc 8.3.5: Add AER support
[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 *,
62 uint8_t *, uint32_t *);
6669f9bb
JS
63static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
64 struct hbq_dmabuf *);
4f774513
JS
65static IOCB_t *
66lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
67{
68 return &iocbq->iocb;
69}
70
71/**
72 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
73 * @q: The Work Queue to operate on.
74 * @wqe: The work Queue Entry to put on the Work queue.
75 *
76 * This routine will copy the contents of @wqe to the next available entry on
77 * the @q. This function will then ring the Work Queue Doorbell to signal the
78 * HBA to start processing the Work Queue Entry. This function returns 0 if
79 * successful. If no entries are available on @q then this function will return
80 * -ENOMEM.
81 * The caller is expected to hold the hbalock when calling this routine.
82 **/
83static uint32_t
84lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
85{
86 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
87 struct lpfc_register doorbell;
88 uint32_t host_index;
89
90 /* If the host has not yet processed the next entry then we are done */
91 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
92 return -ENOMEM;
93 /* set consumption flag every once in a while */
94 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
95 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
96
97 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
98
99 /* Update the host index before invoking device */
100 host_index = q->host_index;
101 q->host_index = ((q->host_index + 1) % q->entry_count);
102
103 /* Ring Doorbell */
104 doorbell.word0 = 0;
105 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
106 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
107 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
108 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
109 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
110
111 return 0;
112}
113
114/**
115 * lpfc_sli4_wq_release - Updates internal hba index for WQ
116 * @q: The Work Queue to operate on.
117 * @index: The index to advance the hba index to.
118 *
119 * This routine will update the HBA index of a queue to reflect consumption of
120 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
121 * an entry the host calls this function to update the queue's internal
122 * pointers. This routine returns the number of entries that were consumed by
123 * the HBA.
124 **/
125static uint32_t
126lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
127{
128 uint32_t released = 0;
129
130 if (q->hba_index == index)
131 return 0;
132 do {
133 q->hba_index = ((q->hba_index + 1) % q->entry_count);
134 released++;
135 } while (q->hba_index != index);
136 return released;
137}
138
139/**
140 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
141 * @q: The Mailbox Queue to operate on.
142 * @wqe: The Mailbox Queue Entry to put on the Work queue.
143 *
144 * This routine will copy the contents of @mqe to the next available entry on
145 * the @q. This function will then ring the Work Queue Doorbell to signal the
146 * HBA to start processing the Work Queue Entry. This function returns 0 if
147 * successful. If no entries are available on @q then this function will return
148 * -ENOMEM.
149 * The caller is expected to hold the hbalock when calling this routine.
150 **/
151static uint32_t
152lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
153{
154 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
155 struct lpfc_register doorbell;
156 uint32_t host_index;
157
158 /* If the host has not yet processed the next entry then we are done */
159 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
160 return -ENOMEM;
161 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
162 /* Save off the mailbox pointer for completion */
163 q->phba->mbox = (MAILBOX_t *)temp_mqe;
164
165 /* Update the host index before invoking device */
166 host_index = q->host_index;
167 q->host_index = ((q->host_index + 1) % q->entry_count);
168
169 /* Ring Doorbell */
170 doorbell.word0 = 0;
171 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
172 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
173 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
174 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
175 return 0;
176}
177
178/**
179 * lpfc_sli4_mq_release - Updates internal hba index for MQ
180 * @q: The Mailbox Queue to operate on.
181 *
182 * This routine will update the HBA index of a queue to reflect consumption of
183 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
184 * an entry the host calls this function to update the queue's internal
185 * pointers. This routine returns the number of entries that were consumed by
186 * the HBA.
187 **/
188static uint32_t
189lpfc_sli4_mq_release(struct lpfc_queue *q)
190{
191 /* Clear the mailbox pointer for completion */
192 q->phba->mbox = NULL;
193 q->hba_index = ((q->hba_index + 1) % q->entry_count);
194 return 1;
195}
196
197/**
198 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
199 * @q: The Event Queue to get the first valid EQE from
200 *
201 * This routine will get the first valid Event Queue Entry from @q, update
202 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
203 * the Queue (no more work to do), or the Queue is full of EQEs that have been
204 * processed, but not popped back to the HBA then this routine will return NULL.
205 **/
206static struct lpfc_eqe *
207lpfc_sli4_eq_get(struct lpfc_queue *q)
208{
209 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
210
211 /* If the next EQE is not valid then we are done */
212 if (!bf_get(lpfc_eqe_valid, eqe))
213 return NULL;
214 /* If the host has not yet processed the next entry then we are done */
215 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
216 return NULL;
217
218 q->hba_index = ((q->hba_index + 1) % q->entry_count);
219 return eqe;
220}
221
222/**
223 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
224 * @q: The Event Queue that the host has completed processing for.
225 * @arm: Indicates whether the host wants to arms this CQ.
226 *
227 * This routine will mark all Event Queue Entries on @q, from the last
228 * known completed entry to the last entry that was processed, as completed
229 * by clearing the valid bit for each completion queue entry. Then it will
230 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
231 * The internal host index in the @q will be updated by this routine to indicate
232 * that the host has finished processing the entries. The @arm parameter
233 * indicates that the queue should be rearmed when ringing the doorbell.
234 *
235 * This function will return the number of EQEs that were popped.
236 **/
237uint32_t
238lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
239{
240 uint32_t released = 0;
241 struct lpfc_eqe *temp_eqe;
242 struct lpfc_register doorbell;
243
244 /* while there are valid entries */
245 while (q->hba_index != q->host_index) {
246 temp_eqe = q->qe[q->host_index].eqe;
247 bf_set(lpfc_eqe_valid, temp_eqe, 0);
248 released++;
249 q->host_index = ((q->host_index + 1) % q->entry_count);
250 }
251 if (unlikely(released == 0 && !arm))
252 return 0;
253
254 /* ring doorbell for number popped */
255 doorbell.word0 = 0;
256 if (arm) {
257 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
258 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
259 }
260 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
261 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
262 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
263 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
264 return released;
265}
266
267/**
268 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
269 * @q: The Completion Queue to get the first valid CQE from
270 *
271 * This routine will get the first valid Completion Queue Entry from @q, update
272 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
273 * the Queue (no more work to do), or the Queue is full of CQEs that have been
274 * processed, but not popped back to the HBA then this routine will return NULL.
275 **/
276static struct lpfc_cqe *
277lpfc_sli4_cq_get(struct lpfc_queue *q)
278{
279 struct lpfc_cqe *cqe;
280
281 /* If the next CQE is not valid then we are done */
282 if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
283 return NULL;
284 /* If the host has not yet processed the next entry then we are done */
285 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
286 return NULL;
287
288 cqe = q->qe[q->hba_index].cqe;
289 q->hba_index = ((q->hba_index + 1) % q->entry_count);
290 return cqe;
291}
292
293/**
294 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
295 * @q: The Completion Queue that the host has completed processing for.
296 * @arm: Indicates whether the host wants to arms this CQ.
297 *
298 * This routine will mark all Completion queue entries on @q, from the last
299 * known completed entry to the last entry that was processed, as completed
300 * by clearing the valid bit for each completion queue entry. Then it will
301 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
302 * The internal host index in the @q will be updated by this routine to indicate
303 * that the host has finished processing the entries. The @arm parameter
304 * indicates that the queue should be rearmed when ringing the doorbell.
305 *
306 * This function will return the number of CQEs that were released.
307 **/
308uint32_t
309lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
310{
311 uint32_t released = 0;
312 struct lpfc_cqe *temp_qe;
313 struct lpfc_register doorbell;
314
315 /* while there are valid entries */
316 while (q->hba_index != q->host_index) {
317 temp_qe = q->qe[q->host_index].cqe;
318 bf_set(lpfc_cqe_valid, temp_qe, 0);
319 released++;
320 q->host_index = ((q->host_index + 1) % q->entry_count);
321 }
322 if (unlikely(released == 0 && !arm))
323 return 0;
324
325 /* ring doorbell for number popped */
326 doorbell.word0 = 0;
327 if (arm)
328 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
329 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
330 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
331 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
332 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
333 return released;
334}
335
336/**
337 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
338 * @q: The Header Receive Queue to operate on.
339 * @wqe: The Receive Queue Entry to put on the Receive queue.
340 *
341 * This routine will copy the contents of @wqe to the next available entry on
342 * the @q. This function will then ring the Receive Queue Doorbell to signal the
343 * HBA to start processing the Receive Queue Entry. This function returns the
344 * index that the rqe was copied to if successful. If no entries are available
345 * on @q then this function will return -ENOMEM.
346 * The caller is expected to hold the hbalock when calling this routine.
347 **/
348static int
349lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
350 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
351{
352 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
353 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
354 struct lpfc_register doorbell;
355 int put_index = hq->host_index;
356
357 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
358 return -EINVAL;
359 if (hq->host_index != dq->host_index)
360 return -EINVAL;
361 /* If the host has not yet processed the next entry then we are done */
362 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
363 return -EBUSY;
364 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
365 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
366
367 /* Update the host index to point to the next slot */
368 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
369 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
370
371 /* Ring The Header Receive Queue Doorbell */
372 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
373 doorbell.word0 = 0;
374 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
375 LPFC_RQ_POST_BATCH);
376 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
377 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
378 }
379 return put_index;
380}
381
382/**
383 * lpfc_sli4_rq_release - Updates internal hba index for RQ
384 * @q: The Header Receive Queue to operate on.
385 *
386 * This routine will update the HBA index of a queue to reflect consumption of
387 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
388 * consumed an entry the host calls this function to update the queue's
389 * internal pointers. This routine returns the number of entries that were
390 * consumed by the HBA.
391 **/
392static uint32_t
393lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
394{
395 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
396 return 0;
397 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
398 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
399 return 1;
400}
401
e59058c4 402/**
3621a710 403 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
404 * @phba: Pointer to HBA context object.
405 * @pring: Pointer to driver SLI ring object.
406 *
407 * This function returns pointer to next command iocb entry
408 * in the command ring. The caller must hold hbalock to prevent
409 * other threads consume the next command iocb.
410 * SLI-2/SLI-3 provide different sized iocbs.
411 **/
ed957684
JS
412static inline IOCB_t *
413lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
414{
415 return (IOCB_t *) (((char *) pring->cmdringaddr) +
416 pring->cmdidx * phba->iocb_cmd_size);
417}
418
e59058c4 419/**
3621a710 420 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
421 * @phba: Pointer to HBA context object.
422 * @pring: Pointer to driver SLI ring object.
423 *
424 * This function returns pointer to next response iocb entry
425 * in the response ring. The caller must hold hbalock to make sure
426 * that no other thread consume the next response iocb.
427 * SLI-2/SLI-3 provide different sized iocbs.
428 **/
ed957684
JS
429static inline IOCB_t *
430lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
431{
432 return (IOCB_t *) (((char *) pring->rspringaddr) +
433 pring->rspidx * phba->iocb_rsp_size);
434}
435
e59058c4 436/**
3621a710 437 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
438 * @phba: Pointer to HBA context object.
439 *
440 * This function is called with hbalock held. This function
441 * allocates a new driver iocb object from the iocb pool. If the
442 * allocation is successful, it returns pointer to the newly
443 * allocated iocb object else it returns NULL.
444 **/
2e0fef85
JS
445static struct lpfc_iocbq *
446__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
447{
448 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
449 struct lpfc_iocbq * iocbq = NULL;
450
451 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
452 return iocbq;
453}
454
da0436e9
JS
455/**
456 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
457 * @phba: Pointer to HBA context object.
458 * @xritag: XRI value.
459 *
460 * This function clears the sglq pointer from the array of acive
461 * sglq's. The xritag that is passed in is used to index into the
462 * array. Before the xritag can be used it needs to be adjusted
463 * by subtracting the xribase.
464 *
465 * Returns sglq ponter = success, NULL = Failure.
466 **/
467static struct lpfc_sglq *
468__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
469{
470 uint16_t adj_xri;
471 struct lpfc_sglq *sglq;
472 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
473 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
474 return NULL;
475 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
476 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
477 return sglq;
478}
479
480/**
481 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
482 * @phba: Pointer to HBA context object.
483 * @xritag: XRI value.
484 *
485 * This function returns the sglq pointer from the array of acive
486 * sglq's. The xritag that is passed in is used to index into the
487 * array. Before the xritag can be used it needs to be adjusted
488 * by subtracting the xribase.
489 *
490 * Returns sglq ponter = success, NULL = Failure.
491 **/
492static struct lpfc_sglq *
493__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
494{
495 uint16_t adj_xri;
496 struct lpfc_sglq *sglq;
497 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
498 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
499 return NULL;
500 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
501 return sglq;
502}
503
504/**
505 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
506 * @phba: Pointer to HBA context object.
507 *
508 * This function is called with hbalock held. This function
509 * Gets a new driver sglq object from the sglq list. If the
510 * list is not empty then it is successful, it returns pointer to the newly
511 * allocated sglq object else it returns NULL.
512 **/
513static struct lpfc_sglq *
514__lpfc_sli_get_sglq(struct lpfc_hba *phba)
515{
516 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
517 struct lpfc_sglq *sglq = NULL;
518 uint16_t adj_xri;
519 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
6a9c52cf
JS
520 if (!sglq)
521 return NULL;
da0436e9
JS
522 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
523 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
524 return sglq;
525}
526
e59058c4 527/**
3621a710 528 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
529 * @phba: Pointer to HBA context object.
530 *
531 * This function is called with no lock held. This function
532 * allocates a new driver iocb object from the iocb pool. If the
533 * allocation is successful, it returns pointer to the newly
534 * allocated iocb object else it returns NULL.
535 **/
2e0fef85
JS
536struct lpfc_iocbq *
537lpfc_sli_get_iocbq(struct lpfc_hba *phba)
538{
539 struct lpfc_iocbq * iocbq = NULL;
540 unsigned long iflags;
541
542 spin_lock_irqsave(&phba->hbalock, iflags);
543 iocbq = __lpfc_sli_get_iocbq(phba);
544 spin_unlock_irqrestore(&phba->hbalock, iflags);
545 return iocbq;
546}
547
4f774513
JS
548/**
549 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
550 * @phba: Pointer to HBA context object.
551 * @iocbq: Pointer to driver iocb object.
552 *
553 * This function is called with hbalock held to release driver
554 * iocb object to the iocb pool. The iotag in the iocb object
555 * does not change for each use of the iocb object. This function
556 * clears all other fields of the iocb object when it is freed.
557 * The sqlq structure that holds the xritag and phys and virtual
558 * mappings for the scatter gather list is retrieved from the
559 * active array of sglq. The get of the sglq pointer also clears
560 * the entry in the array. If the status of the IO indiactes that
561 * this IO was aborted then the sglq entry it put on the
562 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
563 * IO has good status or fails for any other reason then the sglq
564 * entry is added to the free list (lpfc_sgl_list).
565 **/
566static void
567__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
568{
569 struct lpfc_sglq *sglq;
570 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
571 unsigned long iflag;
572
573 if (iocbq->sli4_xritag == NO_XRI)
574 sglq = NULL;
575 else
576 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
577 if (sglq) {
578 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
6669f9bb 579 && ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
4f774513 580 && (iocbq->iocb.un.ulpWord[4]
6669f9bb 581 == IOERR_ABORT_REQUESTED))) {
4f774513
JS
582 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
583 iflag);
584 list_add(&sglq->list,
585 &phba->sli4_hba.lpfc_abts_els_sgl_list);
586 spin_unlock_irqrestore(
587 &phba->sli4_hba.abts_sgl_list_lock, iflag);
588 } else
589 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
590 }
591
592
593 /*
594 * Clean all volatile data fields, preserve iotag and node struct.
595 */
596 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
597 iocbq->sli4_xritag = NO_XRI;
598 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
599}
600
e59058c4 601/**
3772a991 602 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
603 * @phba: Pointer to HBA context object.
604 * @iocbq: Pointer to driver iocb object.
605 *
606 * This function is called with hbalock held to release driver
607 * iocb object to the iocb pool. The iotag in the iocb object
608 * does not change for each use of the iocb object. This function
609 * clears all other fields of the iocb object when it is freed.
610 **/
a6ababd2 611static void
3772a991 612__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 613{
2e0fef85 614 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
615
616 /*
617 * Clean all volatile data fields, preserve iotag and node struct.
618 */
619 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 620 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
621 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
622}
623
3772a991
JS
624/**
625 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
626 * @phba: Pointer to HBA context object.
627 * @iocbq: Pointer to driver iocb object.
628 *
629 * This function is called with hbalock held to release driver
630 * iocb object to the iocb pool. The iotag in the iocb object
631 * does not change for each use of the iocb object. This function
632 * clears all other fields of the iocb object when it is freed.
633 **/
634static void
635__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
636{
637 phba->__lpfc_sli_release_iocbq(phba, iocbq);
638}
639
e59058c4 640/**
3621a710 641 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
642 * @phba: Pointer to HBA context object.
643 * @iocbq: Pointer to driver iocb object.
644 *
645 * This function is called with no lock held to release the iocb to
646 * iocb pool.
647 **/
2e0fef85
JS
648void
649lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
650{
651 unsigned long iflags;
652
653 /*
654 * Clean all volatile data fields, preserve iotag and node struct.
655 */
656 spin_lock_irqsave(&phba->hbalock, iflags);
657 __lpfc_sli_release_iocbq(phba, iocbq);
658 spin_unlock_irqrestore(&phba->hbalock, iflags);
659}
660
a257bf90
JS
661/**
662 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
663 * @phba: Pointer to HBA context object.
664 * @iocblist: List of IOCBs.
665 * @ulpstatus: ULP status in IOCB command field.
666 * @ulpWord4: ULP word-4 in IOCB command field.
667 *
668 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
669 * on the list by invoking the complete callback function associated with the
670 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
671 * fields.
672 **/
673void
674lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
675 uint32_t ulpstatus, uint32_t ulpWord4)
676{
677 struct lpfc_iocbq *piocb;
678
679 while (!list_empty(iocblist)) {
680 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
681
682 if (!piocb->iocb_cmpl)
683 lpfc_sli_release_iocbq(phba, piocb);
684 else {
685 piocb->iocb.ulpStatus = ulpstatus;
686 piocb->iocb.un.ulpWord[4] = ulpWord4;
687 (piocb->iocb_cmpl) (phba, piocb, piocb);
688 }
689 }
690 return;
691}
692
e59058c4 693/**
3621a710
JS
694 * lpfc_sli_iocb_cmd_type - Get the iocb type
695 * @iocb_cmnd: iocb command code.
e59058c4
JS
696 *
697 * This function is called by ring event handler function to get the iocb type.
698 * This function translates the iocb command to an iocb command type used to
699 * decide the final disposition of each completed IOCB.
700 * The function returns
701 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
702 * LPFC_SOL_IOCB if it is a solicited iocb completion
703 * LPFC_ABORT_IOCB if it is an abort iocb
704 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
705 *
706 * The caller is not required to hold any lock.
707 **/
dea3101e 708static lpfc_iocb_type
709lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
710{
711 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
712
713 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
714 return 0;
715
716 switch (iocb_cmnd) {
717 case CMD_XMIT_SEQUENCE_CR:
718 case CMD_XMIT_SEQUENCE_CX:
719 case CMD_XMIT_BCAST_CN:
720 case CMD_XMIT_BCAST_CX:
721 case CMD_ELS_REQUEST_CR:
722 case CMD_ELS_REQUEST_CX:
723 case CMD_CREATE_XRI_CR:
724 case CMD_CREATE_XRI_CX:
725 case CMD_GET_RPI_CN:
726 case CMD_XMIT_ELS_RSP_CX:
727 case CMD_GET_RPI_CR:
728 case CMD_FCP_IWRITE_CR:
729 case CMD_FCP_IWRITE_CX:
730 case CMD_FCP_IREAD_CR:
731 case CMD_FCP_IREAD_CX:
732 case CMD_FCP_ICMND_CR:
733 case CMD_FCP_ICMND_CX:
f5603511
JS
734 case CMD_FCP_TSEND_CX:
735 case CMD_FCP_TRSP_CX:
736 case CMD_FCP_TRECEIVE_CX:
737 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 738 case CMD_ADAPTER_MSG:
739 case CMD_ADAPTER_DUMP:
740 case CMD_XMIT_SEQUENCE64_CR:
741 case CMD_XMIT_SEQUENCE64_CX:
742 case CMD_XMIT_BCAST64_CN:
743 case CMD_XMIT_BCAST64_CX:
744 case CMD_ELS_REQUEST64_CR:
745 case CMD_ELS_REQUEST64_CX:
746 case CMD_FCP_IWRITE64_CR:
747 case CMD_FCP_IWRITE64_CX:
748 case CMD_FCP_IREAD64_CR:
749 case CMD_FCP_IREAD64_CX:
750 case CMD_FCP_ICMND64_CR:
751 case CMD_FCP_ICMND64_CX:
f5603511
JS
752 case CMD_FCP_TSEND64_CX:
753 case CMD_FCP_TRSP64_CX:
754 case CMD_FCP_TRECEIVE64_CX:
dea3101e 755 case CMD_GEN_REQUEST64_CR:
756 case CMD_GEN_REQUEST64_CX:
757 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
758 case DSSCMD_IWRITE64_CR:
759 case DSSCMD_IWRITE64_CX:
760 case DSSCMD_IREAD64_CR:
761 case DSSCMD_IREAD64_CX:
762 case DSSCMD_INVALIDATE_DEK:
763 case DSSCMD_SET_KEK:
764 case DSSCMD_GET_KEK_ID:
765 case DSSCMD_GEN_XFER:
dea3101e 766 type = LPFC_SOL_IOCB;
767 break;
768 case CMD_ABORT_XRI_CN:
769 case CMD_ABORT_XRI_CX:
770 case CMD_CLOSE_XRI_CN:
771 case CMD_CLOSE_XRI_CX:
772 case CMD_XRI_ABORTED_CX:
773 case CMD_ABORT_MXRI64_CN:
6669f9bb 774 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 775 type = LPFC_ABORT_IOCB;
776 break;
777 case CMD_RCV_SEQUENCE_CX:
778 case CMD_RCV_ELS_REQ_CX:
779 case CMD_RCV_SEQUENCE64_CX:
780 case CMD_RCV_ELS_REQ64_CX:
57127f15 781 case CMD_ASYNC_STATUS:
ed957684
JS
782 case CMD_IOCB_RCV_SEQ64_CX:
783 case CMD_IOCB_RCV_ELS64_CX:
784 case CMD_IOCB_RCV_CONT64_CX:
3163f725 785 case CMD_IOCB_RET_XRI64_CX:
dea3101e 786 type = LPFC_UNSOL_IOCB;
787 break;
3163f725
JS
788 case CMD_IOCB_XMIT_MSEQ64_CR:
789 case CMD_IOCB_XMIT_MSEQ64_CX:
790 case CMD_IOCB_RCV_SEQ_LIST64_CX:
791 case CMD_IOCB_RCV_ELS_LIST64_CX:
792 case CMD_IOCB_CLOSE_EXTENDED_CN:
793 case CMD_IOCB_ABORT_EXTENDED_CN:
794 case CMD_IOCB_RET_HBQE64_CN:
795 case CMD_IOCB_FCP_IBIDIR64_CR:
796 case CMD_IOCB_FCP_IBIDIR64_CX:
797 case CMD_IOCB_FCP_ITASKMGT64_CX:
798 case CMD_IOCB_LOGENTRY_CN:
799 case CMD_IOCB_LOGENTRY_ASYNC_CN:
800 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 801 __func__, iocb_cmnd);
3163f725
JS
802 type = LPFC_UNKNOWN_IOCB;
803 break;
dea3101e 804 default:
805 type = LPFC_UNKNOWN_IOCB;
806 break;
807 }
808
809 return type;
810}
811
e59058c4 812/**
3621a710 813 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
814 * @phba: Pointer to HBA context object.
815 *
816 * This function is called from SLI initialization code
817 * to configure every ring of the HBA's SLI interface. The
818 * caller is not required to hold any lock. This function issues
819 * a config_ring mailbox command for each ring.
820 * This function returns zero if successful else returns a negative
821 * error code.
822 **/
dea3101e 823static int
ed957684 824lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 825{
826 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
827 LPFC_MBOXQ_t *pmb;
828 MAILBOX_t *pmbox;
829 int i, rc, ret = 0;
dea3101e 830
ed957684
JS
831 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
832 if (!pmb)
833 return -ENOMEM;
04c68496 834 pmbox = &pmb->u.mb;
ed957684 835 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 836 for (i = 0; i < psli->num_rings; i++) {
dea3101e 837 lpfc_config_ring(phba, i, pmb);
838 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
839 if (rc != MBX_SUCCESS) {
92d7f7b0 840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 841 "0446 Adapter failed to init (%d), "
dea3101e 842 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
843 "ring %d\n",
e8b62011
JS
844 rc, pmbox->mbxCommand,
845 pmbox->mbxStatus, i);
2e0fef85 846 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
847 ret = -ENXIO;
848 break;
dea3101e 849 }
850 }
ed957684
JS
851 mempool_free(pmb, phba->mbox_mem_pool);
852 return ret;
dea3101e 853}
854
e59058c4 855/**
3621a710 856 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
857 * @phba: Pointer to HBA context object.
858 * @pring: Pointer to driver SLI ring object.
859 * @piocb: Pointer to the driver iocb object.
860 *
861 * This function is called with hbalock held. The function adds the
862 * new iocb to txcmplq of the given ring. This function always returns
863 * 0. If this function is called for ELS ring, this function checks if
864 * there is a vport associated with the ELS command. This function also
865 * starts els_tmofunc timer if this is an ELS command.
866 **/
dea3101e 867static int
2e0fef85
JS
868lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
869 struct lpfc_iocbq *piocb)
dea3101e 870{
dea3101e 871 list_add_tail(&piocb->list, &pring->txcmplq);
872 pring->txcmplq_cnt++;
92d7f7b0
JS
873 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
874 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
875 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
876 if (!piocb->vport)
877 BUG();
878 else
879 mod_timer(&piocb->vport->els_tmofunc,
880 jiffies + HZ * (phba->fc_ratov << 1));
881 }
882
dea3101e 883
2e0fef85 884 return 0;
dea3101e 885}
886
e59058c4 887/**
3621a710 888 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
889 * @phba: Pointer to HBA context object.
890 * @pring: Pointer to driver SLI ring object.
891 *
892 * This function is called with hbalock held to get next
893 * iocb in txq of the given ring. If there is any iocb in
894 * the txq, the function returns first iocb in the list after
895 * removing the iocb from the list, else it returns NULL.
896 **/
dea3101e 897static struct lpfc_iocbq *
2e0fef85 898lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 899{
dea3101e 900 struct lpfc_iocbq *cmd_iocb;
901
858c9f6c
JS
902 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
903 if (cmd_iocb != NULL)
dea3101e 904 pring->txq_cnt--;
2e0fef85 905 return cmd_iocb;
dea3101e 906}
907
e59058c4 908/**
3621a710 909 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
910 * @phba: Pointer to HBA context object.
911 * @pring: Pointer to driver SLI ring object.
912 *
913 * This function is called with hbalock held and the caller must post the
914 * iocb without releasing the lock. If the caller releases the lock,
915 * iocb slot returned by the function is not guaranteed to be available.
916 * The function returns pointer to the next available iocb slot if there
917 * is available slot in the ring, else it returns NULL.
918 * If the get index of the ring is ahead of the put index, the function
919 * will post an error attention event to the worker thread to take the
920 * HBA to offline state.
921 **/
dea3101e 922static IOCB_t *
923lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
924{
34b02dcd 925 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 926 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e 927 if ((pring->next_cmdidx == pring->cmdidx) &&
928 (++pring->next_cmdidx >= max_cmd_idx))
929 pring->next_cmdidx = 0;
930
931 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
932
933 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
934
935 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
936 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 937 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 938 "is bigger than cmd ring %d\n",
e8b62011 939 pring->ringno,
dea3101e 940 pring->local_getidx, max_cmd_idx);
941
2e0fef85 942 phba->link_state = LPFC_HBA_ERROR;
dea3101e 943 /*
944 * All error attention handlers are posted to
945 * worker thread
946 */
947 phba->work_ha |= HA_ERATT;
948 phba->work_hs = HS_FFER3;
92d7f7b0 949
5e9d9b82 950 lpfc_worker_wake_up(phba);
dea3101e 951
952 return NULL;
953 }
954
955 if (pring->local_getidx == pring->next_cmdidx)
956 return NULL;
957 }
958
ed957684 959 return lpfc_cmd_iocb(phba, pring);
dea3101e 960}
961
e59058c4 962/**
3621a710 963 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
964 * @phba: Pointer to HBA context object.
965 * @iocbq: Pointer to driver iocb object.
966 *
967 * This function gets an iotag for the iocb. If there is no unused iotag and
968 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
969 * array and assigns a new iotag.
970 * The function returns the allocated iotag if successful, else returns zero.
971 * Zero is not a valid iotag.
972 * The caller is not required to hold any lock.
973 **/
604a3e30 974uint16_t
2e0fef85 975lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 976{
2e0fef85
JS
977 struct lpfc_iocbq **new_arr;
978 struct lpfc_iocbq **old_arr;
604a3e30
JB
979 size_t new_len;
980 struct lpfc_sli *psli = &phba->sli;
981 uint16_t iotag;
dea3101e 982
2e0fef85 983 spin_lock_irq(&phba->hbalock);
604a3e30
JB
984 iotag = psli->last_iotag;
985 if(++iotag < psli->iocbq_lookup_len) {
986 psli->last_iotag = iotag;
987 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 988 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
989 iocbq->iotag = iotag;
990 return iotag;
2e0fef85 991 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
992 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
993 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
994 spin_unlock_irq(&phba->hbalock);
995 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
996 GFP_KERNEL);
997 if (new_arr) {
2e0fef85 998 spin_lock_irq(&phba->hbalock);
604a3e30
JB
999 old_arr = psli->iocbq_lookup;
1000 if (new_len <= psli->iocbq_lookup_len) {
1001 /* highly unprobable case */
1002 kfree(new_arr);
1003 iotag = psli->last_iotag;
1004 if(++iotag < psli->iocbq_lookup_len) {
1005 psli->last_iotag = iotag;
1006 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1007 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1008 iocbq->iotag = iotag;
1009 return iotag;
1010 }
2e0fef85 1011 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1012 return 0;
1013 }
1014 if (psli->iocbq_lookup)
1015 memcpy(new_arr, old_arr,
1016 ((psli->last_iotag + 1) *
311464ec 1017 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1018 psli->iocbq_lookup = new_arr;
1019 psli->iocbq_lookup_len = new_len;
1020 psli->last_iotag = iotag;
1021 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1022 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1023 iocbq->iotag = iotag;
1024 kfree(old_arr);
1025 return iotag;
1026 }
8f6d98d2 1027 } else
2e0fef85 1028 spin_unlock_irq(&phba->hbalock);
dea3101e 1029
604a3e30 1030 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
e8b62011
JS
1031 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1032 psli->last_iotag);
dea3101e 1033
604a3e30 1034 return 0;
dea3101e 1035}
1036
e59058c4 1037/**
3621a710 1038 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1039 * @phba: Pointer to HBA context object.
1040 * @pring: Pointer to driver SLI ring object.
1041 * @iocb: Pointer to iocb slot in the ring.
1042 * @nextiocb: Pointer to driver iocb object which need to be
1043 * posted to firmware.
1044 *
1045 * This function is called with hbalock held to post a new iocb to
1046 * the firmware. This function copies the new iocb to ring iocb slot and
1047 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1048 * a completion call back for this iocb else the function will free the
1049 * iocb object.
1050 **/
dea3101e 1051static void
1052lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1053 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1054{
1055 /*
604a3e30 1056 * Set up an iotag
dea3101e 1057 */
604a3e30 1058 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1059
e2a0a9d6 1060
a58cbd52
JS
1061 if (pring->ringno == LPFC_ELS_RING) {
1062 lpfc_debugfs_slow_ring_trc(phba,
1063 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1064 *(((uint32_t *) &nextiocb->iocb) + 4),
1065 *(((uint32_t *) &nextiocb->iocb) + 6),
1066 *(((uint32_t *) &nextiocb->iocb) + 7));
1067 }
1068
dea3101e 1069 /*
1070 * Issue iocb command to adapter
1071 */
92d7f7b0 1072 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1073 wmb();
1074 pring->stats.iocb_cmd++;
1075
1076 /*
1077 * If there is no completion routine to call, we can release the
1078 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1079 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1080 */
1081 if (nextiocb->iocb_cmpl)
1082 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1083 else
2e0fef85 1084 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1085
1086 /*
1087 * Let the HBA know what IOCB slot will be the next one the
1088 * driver will put a command into.
1089 */
1090 pring->cmdidx = pring->next_cmdidx;
ed957684 1091 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1092}
1093
e59058c4 1094/**
3621a710 1095 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1096 * @phba: Pointer to HBA context object.
1097 * @pring: Pointer to driver SLI ring object.
1098 *
1099 * The caller is not required to hold any lock for calling this function.
1100 * This function updates the chip attention bits for the ring to inform firmware
1101 * that there are pending work to be done for this ring and requests an
1102 * interrupt when there is space available in the ring. This function is
1103 * called when the driver is unable to post more iocbs to the ring due
1104 * to unavailability of space in the ring.
1105 **/
dea3101e 1106static void
2e0fef85 1107lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1108{
1109 int ringno = pring->ringno;
1110
1111 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1112
1113 wmb();
1114
1115 /*
1116 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1117 * The HBA will tell us when an IOCB entry is available.
1118 */
1119 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1120 readl(phba->CAregaddr); /* flush */
1121
1122 pring->stats.iocb_cmd_full++;
1123}
1124
e59058c4 1125/**
3621a710 1126 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1127 * @phba: Pointer to HBA context object.
1128 * @pring: Pointer to driver SLI ring object.
1129 *
1130 * This function updates the chip attention register bit for the
1131 * given ring to inform HBA that there is more work to be done
1132 * in this ring. The caller is not required to hold any lock.
1133 **/
dea3101e 1134static void
2e0fef85 1135lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1136{
1137 int ringno = pring->ringno;
1138
1139 /*
1140 * Tell the HBA that there is work to do in this ring.
1141 */
34b02dcd
JS
1142 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1143 wmb();
1144 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1145 readl(phba->CAregaddr); /* flush */
1146 }
dea3101e 1147}
1148
e59058c4 1149/**
3621a710 1150 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1151 * @phba: Pointer to HBA context object.
1152 * @pring: Pointer to driver SLI ring object.
1153 *
1154 * This function is called with hbalock held to post pending iocbs
1155 * in the txq to the firmware. This function is called when driver
1156 * detects space available in the ring.
1157 **/
dea3101e 1158static void
2e0fef85 1159lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1160{
1161 IOCB_t *iocb;
1162 struct lpfc_iocbq *nextiocb;
1163
1164 /*
1165 * Check to see if:
1166 * (a) there is anything on the txq to send
1167 * (b) link is up
1168 * (c) link attention events can be processed (fcp ring only)
1169 * (d) IOCB processing is not blocked by the outstanding mbox command.
1170 */
1171 if (pring->txq_cnt &&
2e0fef85 1172 lpfc_is_link_up(phba) &&
dea3101e 1173 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1174 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1175
1176 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1177 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1178 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1179
1180 if (iocb)
1181 lpfc_sli_update_ring(phba, pring);
1182 else
1183 lpfc_sli_update_full_ring(phba, pring);
1184 }
1185
1186 return;
1187}
1188
e59058c4 1189/**
3621a710 1190 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1191 * @phba: Pointer to HBA context object.
1192 * @hbqno: HBQ number.
1193 *
1194 * This function is called with hbalock held to get the next
1195 * available slot for the given HBQ. If there is free slot
1196 * available for the HBQ it will return pointer to the next available
1197 * HBQ entry else it will return NULL.
1198 **/
a6ababd2 1199static struct lpfc_hbq_entry *
ed957684
JS
1200lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1201{
1202 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1203
1204 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1205 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1206 hbqp->next_hbqPutIdx = 0;
1207
1208 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1209 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1210 uint32_t getidx = le32_to_cpu(raw_index);
1211
1212 hbqp->local_hbqGetIdx = getidx;
1213
1214 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1215 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1216 LOG_SLI | LOG_VPORT,
e8b62011 1217 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1218 "%u is > than hbqp->entry_count %u\n",
e8b62011 1219 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1220 hbqp->entry_count);
1221
1222 phba->link_state = LPFC_HBA_ERROR;
1223 return NULL;
1224 }
1225
1226 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1227 return NULL;
1228 }
1229
51ef4c26
JS
1230 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1231 hbqp->hbqPutIdx;
ed957684
JS
1232}
1233
e59058c4 1234/**
3621a710 1235 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1236 * @phba: Pointer to HBA context object.
1237 *
1238 * This function is called with no lock held to free all the
1239 * hbq buffers while uninitializing the SLI interface. It also
1240 * frees the HBQ buffers returned by the firmware but not yet
1241 * processed by the upper layers.
1242 **/
ed957684
JS
1243void
1244lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1245{
92d7f7b0
JS
1246 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1247 struct hbq_dmabuf *hbq_buf;
3163f725 1248 unsigned long flags;
51ef4c26 1249 int i, hbq_count;
3163f725 1250 uint32_t hbqno;
ed957684 1251
51ef4c26 1252 hbq_count = lpfc_sli_hbq_count();
ed957684 1253 /* Return all memory used by all HBQs */
3163f725 1254 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1255 for (i = 0; i < hbq_count; ++i) {
1256 list_for_each_entry_safe(dmabuf, next_dmabuf,
1257 &phba->hbqs[i].hbq_buffer_list, list) {
1258 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1259 list_del(&hbq_buf->dbuf.list);
1260 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1261 }
a8adb832 1262 phba->hbqs[i].buffer_count = 0;
ed957684 1263 }
3163f725 1264 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1265 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1266 list) {
3163f725
JS
1267 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1268 list_del(&hbq_buf->dbuf.list);
1269 if (hbq_buf->tag == -1) {
1270 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1271 (phba, hbq_buf);
1272 } else {
1273 hbqno = hbq_buf->tag >> 16;
1274 if (hbqno >= LPFC_MAX_HBQS)
1275 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1276 (phba, hbq_buf);
1277 else
1278 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1279 hbq_buf);
1280 }
1281 }
1282
1283 /* Mark the HBQs not in use */
1284 phba->hbq_in_use = 0;
1285 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1286}
1287
e59058c4 1288/**
3621a710 1289 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1290 * @phba: Pointer to HBA context object.
1291 * @hbqno: HBQ number.
1292 * @hbq_buf: Pointer to HBQ buffer.
1293 *
1294 * This function is called with the hbalock held to post a
1295 * hbq buffer to the firmware. If the function finds an empty
1296 * slot in the HBQ, it will post the buffer. The function will return
1297 * pointer to the hbq entry if it successfully post the buffer
1298 * else it will return NULL.
1299 **/
3772a991 1300static int
ed957684 1301lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1302 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1303{
1304 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1305}
1306
1307/**
1308 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1309 * @phba: Pointer to HBA context object.
1310 * @hbqno: HBQ number.
1311 * @hbq_buf: Pointer to HBQ buffer.
1312 *
1313 * This function is called with the hbalock held to post a hbq buffer to the
1314 * firmware. If the function finds an empty slot in the HBQ, it will post the
1315 * buffer and place it on the hbq_buffer_list. The function will return zero if
1316 * it successfully post the buffer else it will return an error.
1317 **/
1318static int
1319lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1320 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1321{
1322 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1323 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1324
1325 /* Get next HBQ entry slot to use */
1326 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1327 if (hbqe) {
1328 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1329
92d7f7b0
JS
1330 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1331 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1332 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1333 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1334 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1335 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1336 /* Sync SLIM */
ed957684
JS
1337 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1338 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1339 /* flush */
ed957684 1340 readl(phba->hbq_put + hbqno);
51ef4c26 1341 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1342 return 0;
1343 } else
1344 return -ENOMEM;
ed957684
JS
1345}
1346
4f774513
JS
1347/**
1348 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1349 * @phba: Pointer to HBA context object.
1350 * @hbqno: HBQ number.
1351 * @hbq_buf: Pointer to HBQ buffer.
1352 *
1353 * This function is called with the hbalock held to post an RQE to the SLI4
1354 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1355 * the hbq_buffer_list and return zero, otherwise it will return an error.
1356 **/
1357static int
1358lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1359 struct hbq_dmabuf *hbq_buf)
1360{
1361 int rc;
1362 struct lpfc_rqe hrqe;
1363 struct lpfc_rqe drqe;
1364
1365 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1366 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1367 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1368 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1369 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1370 &hrqe, &drqe);
1371 if (rc < 0)
1372 return rc;
1373 hbq_buf->tag = rc;
1374 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1375 return 0;
1376}
1377
e59058c4 1378/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1379static struct lpfc_hbq_init lpfc_els_hbq = {
1380 .rn = 1,
1381 .entry_count = 200,
1382 .mask_count = 0,
1383 .profile = 0,
51ef4c26 1384 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1385 .buffer_count = 0,
a257bf90
JS
1386 .init_count = 40,
1387 .add_count = 40,
92d7f7b0 1388};
ed957684 1389
e59058c4 1390/* HBQ for the extra ring if needed */
51ef4c26
JS
1391static struct lpfc_hbq_init lpfc_extra_hbq = {
1392 .rn = 1,
1393 .entry_count = 200,
1394 .mask_count = 0,
1395 .profile = 0,
1396 .ring_mask = (1 << LPFC_EXTRA_RING),
1397 .buffer_count = 0,
1398 .init_count = 0,
1399 .add_count = 5,
1400};
1401
e59058c4 1402/* Array of HBQs */
78b2d852 1403struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1404 &lpfc_els_hbq,
51ef4c26 1405 &lpfc_extra_hbq,
92d7f7b0 1406};
ed957684 1407
e59058c4 1408/**
3621a710 1409 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1410 * @phba: Pointer to HBA context object.
1411 * @hbqno: HBQ number.
1412 * @count: Number of HBQ buffers to be posted.
1413 *
d7c255b2
JS
1414 * This function is called with no lock held to post more hbq buffers to the
1415 * given HBQ. The function returns the number of HBQ buffers successfully
1416 * posted.
e59058c4 1417 **/
311464ec 1418static int
92d7f7b0 1419lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1420{
d7c255b2 1421 uint32_t i, posted = 0;
3163f725 1422 unsigned long flags;
92d7f7b0 1423 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1424 LIST_HEAD(hbq_buf_list);
eafe1df9 1425 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1426 return 0;
51ef4c26 1427
d7c255b2
JS
1428 if ((phba->hbqs[hbqno].buffer_count + count) >
1429 lpfc_hbq_defs[hbqno]->entry_count)
1430 count = lpfc_hbq_defs[hbqno]->entry_count -
1431 phba->hbqs[hbqno].buffer_count;
1432 if (!count)
1433 return 0;
1434 /* Allocate HBQ entries */
1435 for (i = 0; i < count; i++) {
1436 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1437 if (!hbq_buffer)
1438 break;
1439 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1440 }
3163f725
JS
1441 /* Check whether HBQ is still in use */
1442 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1443 if (!phba->hbq_in_use)
d7c255b2
JS
1444 goto err;
1445 while (!list_empty(&hbq_buf_list)) {
1446 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1447 dbuf.list);
1448 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1449 (hbqno << 16));
3772a991 1450 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1451 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1452 posted++;
1453 } else
51ef4c26 1454 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1455 }
3163f725 1456 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1457 return posted;
1458err:
eafe1df9 1459 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1460 while (!list_empty(&hbq_buf_list)) {
1461 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1462 dbuf.list);
1463 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1464 }
1465 return 0;
ed957684
JS
1466}
1467
e59058c4 1468/**
3621a710 1469 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1470 * @phba: Pointer to HBA context object.
1471 * @qno: HBQ number.
1472 *
1473 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1474 * is called with no lock held. The function returns the number of HBQ entries
1475 * successfully allocated.
e59058c4 1476 **/
92d7f7b0
JS
1477int
1478lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1479{
92d7f7b0
JS
1480 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1481 lpfc_hbq_defs[qno]->add_count));
1482}
ed957684 1483
e59058c4 1484/**
3621a710 1485 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1486 * @phba: Pointer to HBA context object.
1487 * @qno: HBQ queue number.
1488 *
1489 * This function is called from SLI initialization code path with
1490 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1491 * function returns the number of HBQ entries successfully allocated.
e59058c4 1492 **/
a6ababd2 1493static int
92d7f7b0
JS
1494lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1495{
1496 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1497 lpfc_hbq_defs[qno]->init_count));
ed957684
JS
1498}
1499
3772a991
JS
1500/**
1501 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1502 * @phba: Pointer to HBA context object.
1503 * @hbqno: HBQ number.
1504 *
1505 * This function removes the first hbq buffer on an hbq list and returns a
1506 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1507 **/
1508static struct hbq_dmabuf *
1509lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1510{
1511 struct lpfc_dmabuf *d_buf;
1512
1513 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1514 if (!d_buf)
1515 return NULL;
1516 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1517}
1518
e59058c4 1519/**
3621a710 1520 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1521 * @phba: Pointer to HBA context object.
1522 * @tag: Tag of the hbq buffer.
1523 *
1524 * This function is called with hbalock held. This function searches
1525 * for the hbq buffer associated with the given tag in the hbq buffer
1526 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1527 * it returns NULL.
1528 **/
a6ababd2 1529static struct hbq_dmabuf *
92d7f7b0 1530lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1531{
92d7f7b0
JS
1532 struct lpfc_dmabuf *d_buf;
1533 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1534 uint32_t hbqno;
1535
1536 hbqno = tag >> 16;
a0a74e45 1537 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1538 return NULL;
ed957684 1539
3772a991 1540 spin_lock_irq(&phba->hbalock);
51ef4c26 1541 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1542 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1543 if (hbq_buf->tag == tag) {
3772a991 1544 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1545 return hbq_buf;
ed957684
JS
1546 }
1547 }
3772a991 1548 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1549 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1550 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1551 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1552 return NULL;
ed957684
JS
1553}
1554
e59058c4 1555/**
3621a710 1556 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1557 * @phba: Pointer to HBA context object.
1558 * @hbq_buffer: Pointer to HBQ buffer.
1559 *
1560 * This function is called with hbalock. This function gives back
1561 * the hbq buffer to firmware. If the HBQ does not have space to
1562 * post the buffer, it will free the buffer.
1563 **/
ed957684 1564void
51ef4c26 1565lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1566{
1567 uint32_t hbqno;
1568
51ef4c26
JS
1569 if (hbq_buffer) {
1570 hbqno = hbq_buffer->tag >> 16;
3772a991 1571 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1572 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1573 }
1574}
1575
e59058c4 1576/**
3621a710 1577 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1578 * @mbxCommand: mailbox command code.
1579 *
1580 * This function is called by the mailbox event handler function to verify
1581 * that the completed mailbox command is a legitimate mailbox command. If the
1582 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1583 * and the mailbox event handler will take the HBA offline.
1584 **/
dea3101e 1585static int
1586lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1587{
1588 uint8_t ret;
1589
1590 switch (mbxCommand) {
1591 case MBX_LOAD_SM:
1592 case MBX_READ_NV:
1593 case MBX_WRITE_NV:
a8adb832 1594 case MBX_WRITE_VPARMS:
dea3101e 1595 case MBX_RUN_BIU_DIAG:
1596 case MBX_INIT_LINK:
1597 case MBX_DOWN_LINK:
1598 case MBX_CONFIG_LINK:
1599 case MBX_CONFIG_RING:
1600 case MBX_RESET_RING:
1601 case MBX_READ_CONFIG:
1602 case MBX_READ_RCONFIG:
1603 case MBX_READ_SPARM:
1604 case MBX_READ_STATUS:
1605 case MBX_READ_RPI:
1606 case MBX_READ_XRI:
1607 case MBX_READ_REV:
1608 case MBX_READ_LNK_STAT:
1609 case MBX_REG_LOGIN:
1610 case MBX_UNREG_LOGIN:
1611 case MBX_READ_LA:
1612 case MBX_CLEAR_LA:
1613 case MBX_DUMP_MEMORY:
1614 case MBX_DUMP_CONTEXT:
1615 case MBX_RUN_DIAGS:
1616 case MBX_RESTART:
1617 case MBX_UPDATE_CFG:
1618 case MBX_DOWN_LOAD:
1619 case MBX_DEL_LD_ENTRY:
1620 case MBX_RUN_PROGRAM:
1621 case MBX_SET_MASK:
09372820 1622 case MBX_SET_VARIABLE:
dea3101e 1623 case MBX_UNREG_D_ID:
41415862 1624 case MBX_KILL_BOARD:
dea3101e 1625 case MBX_CONFIG_FARP:
41415862 1626 case MBX_BEACON:
dea3101e 1627 case MBX_LOAD_AREA:
1628 case MBX_RUN_BIU_DIAG64:
1629 case MBX_CONFIG_PORT:
1630 case MBX_READ_SPARM64:
1631 case MBX_READ_RPI64:
1632 case MBX_REG_LOGIN64:
1633 case MBX_READ_LA64:
09372820 1634 case MBX_WRITE_WWN:
dea3101e 1635 case MBX_SET_DEBUG:
1636 case MBX_LOAD_EXP_ROM:
57127f15 1637 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
1638 case MBX_REG_VPI:
1639 case MBX_UNREG_VPI:
858c9f6c 1640 case MBX_HEARTBEAT:
84774a4d
JS
1641 case MBX_PORT_CAPABILITIES:
1642 case MBX_PORT_IOV_CONTROL:
04c68496
JS
1643 case MBX_SLI4_CONFIG:
1644 case MBX_SLI4_REQ_FTRS:
1645 case MBX_REG_FCFI:
1646 case MBX_UNREG_FCFI:
1647 case MBX_REG_VFI:
1648 case MBX_UNREG_VFI:
1649 case MBX_INIT_VPI:
1650 case MBX_INIT_VFI:
1651 case MBX_RESUME_RPI:
dea3101e 1652 ret = mbxCommand;
1653 break;
1654 default:
1655 ret = MBX_SHUTDOWN;
1656 break;
1657 }
2e0fef85 1658 return ret;
dea3101e 1659}
e59058c4
JS
1660
1661/**
3621a710 1662 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
1663 * @phba: Pointer to HBA context object.
1664 * @pmboxq: Pointer to mailbox command.
1665 *
1666 * This is completion handler function for mailbox commands issued from
1667 * lpfc_sli_issue_mbox_wait function. This function is called by the
1668 * mailbox event handler function with no lock held. This function
1669 * will wake up thread waiting on the wait queue pointed by context1
1670 * of the mailbox.
1671 **/
04c68496 1672void
2e0fef85 1673lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 1674{
1675 wait_queue_head_t *pdone_q;
858c9f6c 1676 unsigned long drvr_flag;
dea3101e 1677
1678 /*
1679 * If pdone_q is empty, the driver thread gave up waiting and
1680 * continued running.
1681 */
7054a606 1682 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 1683 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e 1684 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1685 if (pdone_q)
1686 wake_up_interruptible(pdone_q);
858c9f6c 1687 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 1688 return;
1689}
1690
e59058c4
JS
1691
1692/**
3621a710 1693 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
1694 * @phba: Pointer to HBA context object.
1695 * @pmb: Pointer to mailbox object.
1696 *
1697 * This function is the default mailbox completion handler. It
1698 * frees the memory resources associated with the completed mailbox
1699 * command. If the completed command is a REG_LOGIN mailbox command,
1700 * this function will issue a UREG_LOGIN to re-claim the RPI.
1701 **/
dea3101e 1702void
2e0fef85 1703lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1704{
1705 struct lpfc_dmabuf *mp;
04c68496 1706 uint16_t rpi, vpi;
7054a606
JS
1707 int rc;
1708
dea3101e 1709 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 1710
dea3101e 1711 if (mp) {
1712 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1713 kfree(mp);
1714 }
7054a606 1715
04c68496
JS
1716 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1717 (phba->sli_rev == LPFC_SLI_REV4))
1718 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1719
7054a606
JS
1720 /*
1721 * If a REG_LOGIN succeeded after node is destroyed or node
1722 * is in re-discovery driver need to cleanup the RPI.
1723 */
2e0fef85 1724 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
1725 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1726 !pmb->u.mb.mbxStatus) {
1727 rpi = pmb->u.mb.un.varWords[0];
1728 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1729 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 1730 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
1731 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1732 if (rc != MBX_NOT_FINISHED)
1733 return;
1734 }
1735
04c68496
JS
1736 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1737 lpfc_sli4_mbox_cmd_free(phba, pmb);
1738 else
1739 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1740}
1741
e59058c4 1742/**
3621a710 1743 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
1744 * @phba: Pointer to HBA context object.
1745 *
1746 * This function is called with no lock held. This function processes all
1747 * the completed mailbox commands and gives it to upper layers. The interrupt
1748 * service routine processes mailbox completion interrupt and adds completed
1749 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1750 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1751 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1752 * function returns the mailbox commands to the upper layer by calling the
1753 * completion handler function of each mailbox.
1754 **/
dea3101e 1755int
2e0fef85 1756lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 1757{
92d7f7b0 1758 MAILBOX_t *pmbox;
dea3101e 1759 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
1760 int rc;
1761 LIST_HEAD(cmplq);
dea3101e 1762
1763 phba->sli.slistat.mbox_event++;
1764
92d7f7b0
JS
1765 /* Get all completed mailboxe buffers into the cmplq */
1766 spin_lock_irq(&phba->hbalock);
1767 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1768 spin_unlock_irq(&phba->hbalock);
dea3101e 1769
92d7f7b0
JS
1770 /* Get a Mailbox buffer to setup mailbox commands for callback */
1771 do {
1772 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1773 if (pmb == NULL)
1774 break;
2e0fef85 1775
04c68496 1776 pmbox = &pmb->u.mb;
dea3101e 1777
858c9f6c
JS
1778 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1779 if (pmb->vport) {
1780 lpfc_debugfs_disc_trc(pmb->vport,
1781 LPFC_DISC_TRC_MBOX_VPORT,
1782 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1783 (uint32_t)pmbox->mbxCommand,
1784 pmbox->un.varWords[0],
1785 pmbox->un.varWords[1]);
1786 }
1787 else {
1788 lpfc_debugfs_disc_trc(phba->pport,
1789 LPFC_DISC_TRC_MBOX,
1790 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1791 (uint32_t)pmbox->mbxCommand,
1792 pmbox->un.varWords[0],
1793 pmbox->un.varWords[1]);
1794 }
1795 }
1796
dea3101e 1797 /*
1798 * It is a fatal error if unknown mbox command completion.
1799 */
1800 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1801 MBX_SHUTDOWN) {
dea3101e 1802 /* Unknow mailbox command compl */
92d7f7b0 1803 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 1804 "(%d):0323 Unknown Mailbox command "
04c68496 1805 "x%x (x%x) Cmpl\n",
92d7f7b0 1806 pmb->vport ? pmb->vport->vpi : 0,
04c68496
JS
1807 pmbox->mbxCommand,
1808 lpfc_sli4_mbox_opcode_get(phba, pmb));
2e0fef85 1809 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1810 phba->work_hs = HS_FFER3;
1811 lpfc_handle_eratt(phba);
92d7f7b0 1812 continue;
dea3101e 1813 }
1814
dea3101e 1815 if (pmbox->mbxStatus) {
1816 phba->sli.slistat.mbox_stat_err++;
1817 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1818 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0
JS
1819 lpfc_printf_log(phba, KERN_INFO,
1820 LOG_MBOX | LOG_SLI,
e8b62011 1821 "(%d):0305 Mbox cmd cmpl "
92d7f7b0 1822 "error - RETRYing Data: x%x "
04c68496 1823 "(x%x) x%x x%x x%x\n",
92d7f7b0
JS
1824 pmb->vport ? pmb->vport->vpi :0,
1825 pmbox->mbxCommand,
04c68496
JS
1826 lpfc_sli4_mbox_opcode_get(phba,
1827 pmb),
92d7f7b0
JS
1828 pmbox->mbxStatus,
1829 pmbox->un.varWords[0],
1830 pmb->vport->port_state);
dea3101e 1831 pmbox->mbxStatus = 0;
1832 pmbox->mbxOwner = OWN_HOST;
dea3101e 1833 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 1834 if (rc != MBX_NOT_FINISHED)
92d7f7b0 1835 continue;
dea3101e 1836 }
1837 }
1838
1839 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 1840 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
04c68496 1841 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea3101e 1842 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 1843 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 1844 pmbox->mbxCommand,
04c68496 1845 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea3101e 1846 pmb->mbox_cmpl,
1847 *((uint32_t *) pmbox),
1848 pmbox->un.varWords[0],
1849 pmbox->un.varWords[1],
1850 pmbox->un.varWords[2],
1851 pmbox->un.varWords[3],
1852 pmbox->un.varWords[4],
1853 pmbox->un.varWords[5],
1854 pmbox->un.varWords[6],
1855 pmbox->un.varWords[7]);
1856
92d7f7b0 1857 if (pmb->mbox_cmpl)
dea3101e 1858 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
1859 } while (1);
1860 return 0;
1861}
dea3101e 1862
e59058c4 1863/**
3621a710 1864 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
1865 * @phba: Pointer to HBA context object.
1866 * @pring: Pointer to driver SLI ring object.
1867 * @tag: buffer tag.
1868 *
1869 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1870 * is set in the tag the buffer is posted for a particular exchange,
1871 * the function will return the buffer without replacing the buffer.
1872 * If the buffer is for unsolicited ELS or CT traffic, this function
1873 * returns the buffer and also posts another buffer to the firmware.
1874 **/
76bb24ef
JS
1875static struct lpfc_dmabuf *
1876lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
1877 struct lpfc_sli_ring *pring,
1878 uint32_t tag)
76bb24ef 1879{
9f1e1b50
JS
1880 struct hbq_dmabuf *hbq_entry;
1881
76bb24ef
JS
1882 if (tag & QUE_BUFTAG_BIT)
1883 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
1884 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1885 if (!hbq_entry)
1886 return NULL;
1887 return &hbq_entry->dbuf;
76bb24ef 1888}
57127f15 1889
3772a991
JS
1890/**
1891 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1892 * @phba: Pointer to HBA context object.
1893 * @pring: Pointer to driver SLI ring object.
1894 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1895 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1896 * @fch_type: the type for the first frame of the sequence.
1897 *
1898 * This function is called with no lock held. This function uses the r_ctl and
1899 * type of the received sequence to find the correct callback function to call
1900 * to process the sequence.
1901 **/
1902static int
1903lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1904 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1905 uint32_t fch_type)
1906{
1907 int i;
1908
1909 /* unSolicited Responses */
1910 if (pring->prt[0].profile) {
1911 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1912 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1913 saveq);
1914 return 1;
1915 }
1916 /* We must search, based on rctl / type
1917 for the right routine */
1918 for (i = 0; i < pring->num_mask; i++) {
1919 if ((pring->prt[i].rctl == fch_r_ctl) &&
1920 (pring->prt[i].type == fch_type)) {
1921 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1922 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1923 (phba, pring, saveq);
1924 return 1;
1925 }
1926 }
1927 return 0;
1928}
e59058c4
JS
1929
1930/**
3621a710 1931 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
1932 * @phba: Pointer to HBA context object.
1933 * @pring: Pointer to driver SLI ring object.
1934 * @saveq: Pointer to the unsolicited iocb.
1935 *
1936 * This function is called with no lock held by the ring event handler
1937 * when there is an unsolicited iocb posted to the response ring by the
1938 * firmware. This function gets the buffer associated with the iocbs
1939 * and calls the event handler for the ring. This function handles both
1940 * qring buffers and hbq buffers.
1941 * When the function returns 1 the caller can free the iocb object otherwise
1942 * upper layer functions will free the iocb objects.
1943 **/
dea3101e 1944static int
1945lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1946 struct lpfc_iocbq *saveq)
1947{
1948 IOCB_t * irsp;
1949 WORD5 * w5p;
1950 uint32_t Rctl, Type;
3772a991 1951 uint32_t match;
76bb24ef 1952 struct lpfc_iocbq *iocbq;
3163f725 1953 struct lpfc_dmabuf *dmzbuf;
dea3101e 1954
1955 match = 0;
1956 irsp = &(saveq->iocb);
57127f15
JS
1957
1958 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1959 if (pring->lpfc_sli_rcv_async_status)
1960 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1961 else
1962 lpfc_printf_log(phba,
1963 KERN_WARNING,
1964 LOG_SLI,
1965 "0316 Ring %d handler: unexpected "
1966 "ASYNC_STATUS iocb received evt_code "
1967 "0x%x\n",
1968 pring->ringno,
1969 irsp->un.asyncstat.evt_code);
1970 return 1;
1971 }
1972
3163f725
JS
1973 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1974 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1975 if (irsp->ulpBdeCount > 0) {
1976 dmzbuf = lpfc_sli_get_buff(phba, pring,
1977 irsp->un.ulpWord[3]);
1978 lpfc_in_buf_free(phba, dmzbuf);
1979 }
1980
1981 if (irsp->ulpBdeCount > 1) {
1982 dmzbuf = lpfc_sli_get_buff(phba, pring,
1983 irsp->unsli3.sli3Words[3]);
1984 lpfc_in_buf_free(phba, dmzbuf);
1985 }
1986
1987 if (irsp->ulpBdeCount > 2) {
1988 dmzbuf = lpfc_sli_get_buff(phba, pring,
1989 irsp->unsli3.sli3Words[7]);
1990 lpfc_in_buf_free(phba, dmzbuf);
1991 }
1992
1993 return 1;
1994 }
1995
92d7f7b0 1996 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
1997 if (irsp->ulpBdeCount != 0) {
1998 saveq->context2 = lpfc_sli_get_buff(phba, pring,
1999 irsp->un.ulpWord[3]);
2000 if (!saveq->context2)
2001 lpfc_printf_log(phba,
2002 KERN_ERR,
2003 LOG_SLI,
2004 "0341 Ring %d Cannot find buffer for "
2005 "an unsolicited iocb. tag 0x%x\n",
2006 pring->ringno,
2007 irsp->un.ulpWord[3]);
76bb24ef
JS
2008 }
2009 if (irsp->ulpBdeCount == 2) {
2010 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2011 irsp->unsli3.sli3Words[7]);
2012 if (!saveq->context3)
2013 lpfc_printf_log(phba,
2014 KERN_ERR,
2015 LOG_SLI,
2016 "0342 Ring %d Cannot find buffer for an"
2017 " unsolicited iocb. tag 0x%x\n",
2018 pring->ringno,
2019 irsp->unsli3.sli3Words[7]);
2020 }
2021 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2022 irsp = &(iocbq->iocb);
76bb24ef
JS
2023 if (irsp->ulpBdeCount != 0) {
2024 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2025 irsp->un.ulpWord[3]);
9c2face6 2026 if (!iocbq->context2)
76bb24ef
JS
2027 lpfc_printf_log(phba,
2028 KERN_ERR,
2029 LOG_SLI,
2030 "0343 Ring %d Cannot find "
2031 "buffer for an unsolicited iocb"
2032 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2033 irsp->un.ulpWord[3]);
76bb24ef
JS
2034 }
2035 if (irsp->ulpBdeCount == 2) {
2036 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2037 irsp->unsli3.sli3Words[7]);
9c2face6 2038 if (!iocbq->context3)
76bb24ef
JS
2039 lpfc_printf_log(phba,
2040 KERN_ERR,
2041 LOG_SLI,
2042 "0344 Ring %d Cannot find "
2043 "buffer for an unsolicited "
2044 "iocb. tag 0x%x\n",
2045 pring->ringno,
2046 irsp->unsli3.sli3Words[7]);
2047 }
2048 }
92d7f7b0 2049 }
9c2face6
JS
2050 if (irsp->ulpBdeCount != 0 &&
2051 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2052 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2053 int found = 0;
2054
2055 /* search continue save q for same XRI */
2056 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2057 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2058 list_add_tail(&saveq->list, &iocbq->list);
2059 found = 1;
2060 break;
2061 }
2062 }
2063 if (!found)
2064 list_add_tail(&saveq->clist,
2065 &pring->iocb_continue_saveq);
2066 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2067 list_del_init(&iocbq->clist);
2068 saveq = iocbq;
2069 irsp = &(saveq->iocb);
2070 } else
2071 return 0;
2072 }
2073 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2074 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2075 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2076 Rctl = FC_RCTL_ELS_REQ;
2077 Type = FC_TYPE_ELS;
9c2face6
JS
2078 } else {
2079 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2080 Rctl = w5p->hcsw.Rctl;
2081 Type = w5p->hcsw.Type;
2082
2083 /* Firmware Workaround */
2084 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2085 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2086 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2087 Rctl = FC_RCTL_ELS_REQ;
2088 Type = FC_TYPE_ELS;
9c2face6
JS
2089 w5p->hcsw.Rctl = Rctl;
2090 w5p->hcsw.Type = Type;
2091 }
2092 }
92d7f7b0 2093
3772a991 2094 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2095 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2096 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2097 "Type x%x received\n",
e8b62011 2098 pring->ringno, Rctl, Type);
3772a991 2099
92d7f7b0 2100 return 1;
dea3101e 2101}
2102
e59058c4 2103/**
3621a710 2104 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2105 * @phba: Pointer to HBA context object.
2106 * @pring: Pointer to driver SLI ring object.
2107 * @prspiocb: Pointer to response iocb object.
2108 *
2109 * This function looks up the iocb_lookup table to get the command iocb
2110 * corresponding to the given response iocb using the iotag of the
2111 * response iocb. This function is called with the hbalock held.
2112 * This function returns the command iocb object if it finds the command
2113 * iocb else returns NULL.
2114 **/
dea3101e 2115static struct lpfc_iocbq *
2e0fef85
JS
2116lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2117 struct lpfc_sli_ring *pring,
2118 struct lpfc_iocbq *prspiocb)
dea3101e 2119{
dea3101e 2120 struct lpfc_iocbq *cmd_iocb = NULL;
2121 uint16_t iotag;
2122
604a3e30
JB
2123 iotag = prspiocb->iocb.ulpIoTag;
2124
2125 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2126 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2127 list_del_init(&cmd_iocb->list);
604a3e30
JB
2128 pring->txcmplq_cnt--;
2129 return cmd_iocb;
dea3101e 2130 }
2131
dea3101e 2132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2133 "0317 iotag x%x is out off "
604a3e30 2134 "range: max iotag x%x wd0 x%x\n",
e8b62011 2135 iotag, phba->sli.last_iotag,
604a3e30 2136 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2137 return NULL;
2138}
2139
3772a991
JS
2140/**
2141 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2142 * @phba: Pointer to HBA context object.
2143 * @pring: Pointer to driver SLI ring object.
2144 * @iotag: IOCB tag.
2145 *
2146 * This function looks up the iocb_lookup table to get the command iocb
2147 * corresponding to the given iotag. This function is called with the
2148 * hbalock held.
2149 * This function returns the command iocb object if it finds the command
2150 * iocb else returns NULL.
2151 **/
2152static struct lpfc_iocbq *
2153lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2154 struct lpfc_sli_ring *pring, uint16_t iotag)
2155{
2156 struct lpfc_iocbq *cmd_iocb;
2157
2158 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2159 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2160 list_del_init(&cmd_iocb->list);
2161 pring->txcmplq_cnt--;
2162 return cmd_iocb;
2163 }
2164
2165 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2166 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2167 iotag, phba->sli.last_iotag);
2168 return NULL;
2169}
2170
e59058c4 2171/**
3621a710 2172 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2173 * @phba: Pointer to HBA context object.
2174 * @pring: Pointer to driver SLI ring object.
2175 * @saveq: Pointer to the response iocb to be processed.
2176 *
2177 * This function is called by the ring event handler for non-fcp
2178 * rings when there is a new response iocb in the response ring.
2179 * The caller is not required to hold any locks. This function
2180 * gets the command iocb associated with the response iocb and
2181 * calls the completion handler for the command iocb. If there
2182 * is no completion handler, the function will free the resources
2183 * associated with command iocb. If the response iocb is for
2184 * an already aborted command iocb, the status of the completion
2185 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2186 * This function always returns 1.
2187 **/
dea3101e 2188static int
2e0fef85 2189lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 2190 struct lpfc_iocbq *saveq)
2191{
2e0fef85 2192 struct lpfc_iocbq *cmdiocbp;
dea3101e 2193 int rc = 1;
2194 unsigned long iflag;
2195
2196 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2197 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2198 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2199 spin_unlock_irqrestore(&phba->hbalock, iflag);
2200
dea3101e 2201 if (cmdiocbp) {
2202 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2203 /*
2204 * If an ELS command failed send an event to mgmt
2205 * application.
2206 */
2207 if (saveq->iocb.ulpStatus &&
2208 (pring->ringno == LPFC_ELS_RING) &&
2209 (cmdiocbp->iocb.ulpCommand ==
2210 CMD_ELS_REQUEST64_CR))
2211 lpfc_send_els_failure_event(phba,
2212 cmdiocbp, saveq);
2213
dea3101e 2214 /*
2215 * Post all ELS completions to the worker thread.
2216 * All other are passed to the completion callback.
2217 */
2218 if (pring->ringno == LPFC_ELS_RING) {
07951076
JS
2219 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2220 cmdiocbp->iocb_flag &=
2221 ~LPFC_DRIVER_ABORTED;
2222 saveq->iocb.ulpStatus =
2223 IOSTAT_LOCAL_REJECT;
2224 saveq->iocb.un.ulpWord[4] =
2225 IOERR_SLI_ABORTED;
0ff10d46
JS
2226
2227 /* Firmware could still be in progress
2228 * of DMAing payload, so don't free data
2229 * buffer till after a hbeat.
2230 */
2231 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
07951076 2232 }
dea3101e 2233 }
2e0fef85 2234 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2235 } else
2236 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 2237 } else {
2238 /*
2239 * Unknown initiating command based on the response iotag.
2240 * This could be the case on the ELS ring because of
2241 * lpfc_els_abort().
2242 */
2243 if (pring->ringno != LPFC_ELS_RING) {
2244 /*
2245 * Ring <ringno> handler: unexpected completion IoTag
2246 * <IoTag>
2247 */
a257bf90 2248 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2249 "0322 Ring %d handler: "
2250 "unexpected completion IoTag x%x "
2251 "Data: x%x x%x x%x x%x\n",
2252 pring->ringno,
2253 saveq->iocb.ulpIoTag,
2254 saveq->iocb.ulpStatus,
2255 saveq->iocb.un.ulpWord[4],
2256 saveq->iocb.ulpCommand,
2257 saveq->iocb.ulpContext);
dea3101e 2258 }
2259 }
68876920 2260
dea3101e 2261 return rc;
2262}
2263
e59058c4 2264/**
3621a710 2265 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2266 * @phba: Pointer to HBA context object.
2267 * @pring: Pointer to driver SLI ring object.
2268 *
2269 * This function is called from the iocb ring event handlers when
2270 * put pointer is ahead of the get pointer for a ring. This function signal
2271 * an error attention condition to the worker thread and the worker
2272 * thread will transition the HBA to offline state.
2273 **/
2e0fef85
JS
2274static void
2275lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2276{
34b02dcd 2277 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2278 /*
025dfdaf 2279 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2280 * rsp ring <portRspMax>
2281 */
2282 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2283 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2284 "is bigger than rsp ring %d\n",
e8b62011 2285 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2286 pring->numRiocb);
2287
2e0fef85 2288 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2289
2290 /*
2291 * All error attention handlers are posted to
2292 * worker thread
2293 */
2294 phba->work_ha |= HA_ERATT;
2295 phba->work_hs = HS_FFER3;
92d7f7b0 2296
5e9d9b82 2297 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2298
2299 return;
2300}
2301
9399627f 2302/**
3621a710 2303 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2304 * @ptr: Pointer to address of HBA context object.
2305 *
2306 * This function is invoked by the Error Attention polling timer when the
2307 * timer times out. It will check the SLI Error Attention register for
2308 * possible attention events. If so, it will post an Error Attention event
2309 * and wake up worker thread to process it. Otherwise, it will set up the
2310 * Error Attention polling timer for the next poll.
2311 **/
2312void lpfc_poll_eratt(unsigned long ptr)
2313{
2314 struct lpfc_hba *phba;
2315 uint32_t eratt = 0;
2316
2317 phba = (struct lpfc_hba *)ptr;
2318
2319 /* Check chip HA register for error event */
2320 eratt = lpfc_sli_check_eratt(phba);
2321
2322 if (eratt)
2323 /* Tell the worker thread there is work to do */
2324 lpfc_worker_wake_up(phba);
2325 else
2326 /* Restart the timer for next eratt poll */
2327 mod_timer(&phba->eratt_poll, jiffies +
2328 HZ * LPFC_ERATT_POLL_INTERVAL);
2329 return;
2330}
2331
e59058c4 2332/**
3621a710 2333 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
e59058c4
JS
2334 * @phba: Pointer to HBA context object.
2335 *
2336 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
2337 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
2338 * is enabled.
2339 *
2340 * The caller does not hold any lock.
2341 * The function processes each response iocb in the response ring until it
2342 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2343 * LE bit set. The function will call the completion handler of the command iocb
2344 * if the response iocb indicates a completion for a command iocb or it is
2345 * an abort completion.
2346 **/
2e0fef85 2347void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
875fbdfe 2348{
2e0fef85
JS
2349 struct lpfc_sli *psli = &phba->sli;
2350 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
875fbdfe
JSEC
2351 IOCB_t *irsp = NULL;
2352 IOCB_t *entry = NULL;
2353 struct lpfc_iocbq *cmdiocbq = NULL;
2354 struct lpfc_iocbq rspiocbq;
34b02dcd 2355 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe
JSEC
2356 uint32_t status;
2357 uint32_t portRspPut, portRspMax;
2358 int type;
2359 uint32_t rsp_cmpl = 0;
875fbdfe 2360 uint32_t ha_copy;
2e0fef85 2361 unsigned long iflags;
875fbdfe
JSEC
2362
2363 pring->stats.iocb_event++;
2364
875fbdfe
JSEC
2365 /*
2366 * The next available response entry should never exceed the maximum
2367 * entries. If it does, treat it as an adapter hardware error.
2368 */
2369 portRspMax = pring->numRiocb;
2370 portRspPut = le32_to_cpu(pgp->rspPutInx);
2371 if (unlikely(portRspPut >= portRspMax)) {
2372 lpfc_sli_rsp_pointers_error(phba, pring);
2373 return;
2374 }
2375
2376 rmb();
2377 while (pring->rspidx != portRspPut) {
ed957684 2378 entry = lpfc_resp_iocb(phba, pring);
875fbdfe
JSEC
2379 if (++pring->rspidx >= portRspMax)
2380 pring->rspidx = 0;
2381
2382 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2383 (uint32_t *) &rspiocbq.iocb,
92d7f7b0 2384 phba->iocb_rsp_size);
875fbdfe
JSEC
2385 irsp = &rspiocbq.iocb;
2386 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2387 pring->stats.iocb_rsp++;
2388 rsp_cmpl++;
2389
2390 if (unlikely(irsp->ulpStatus)) {
2391 /* Rsp ring <ringno> error: IOCB */
2392 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2393 "0326 Rsp Ring %d error: IOCB Data: "
875fbdfe 2394 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2395 pring->ringno,
875fbdfe
JSEC
2396 irsp->un.ulpWord[0],
2397 irsp->un.ulpWord[1],
2398 irsp->un.ulpWord[2],
2399 irsp->un.ulpWord[3],
2400 irsp->un.ulpWord[4],
2401 irsp->un.ulpWord[5],
d7c255b2
JS
2402 *(uint32_t *)&irsp->un1,
2403 *((uint32_t *)&irsp->un1 + 1));
875fbdfe
JSEC
2404 }
2405
2406 switch (type) {
2407 case LPFC_ABORT_IOCB:
2408 case LPFC_SOL_IOCB:
2409 /*
2410 * Idle exchange closed via ABTS from port. No iocb
2411 * resources need to be recovered.
2412 */
2413 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2414 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
2415 "0314 IOCB cmd 0x%x "
2416 "processed. Skipping "
2417 "completion",
dca9479b 2418 irsp->ulpCommand);
875fbdfe
JSEC
2419 break;
2420 }
2421
2e0fef85 2422 spin_lock_irqsave(&phba->hbalock, iflags);
875fbdfe
JSEC
2423 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2424 &rspiocbq);
2e0fef85 2425 spin_unlock_irqrestore(&phba->hbalock, iflags);
875fbdfe
JSEC
2426 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2427 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2428 &rspiocbq);
2429 }
2430 break;
2431 default:
2432 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2433 char adaptermsg[LPFC_MAX_ADPTMSG];
2434 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2435 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2436 MAX_MSG_DATA);
898eb71c
JP
2437 dev_warn(&((phba->pcidev)->dev),
2438 "lpfc%d: %s\n",
875fbdfe
JSEC
2439 phba->brd_no, adaptermsg);
2440 } else {
2441 /* Unknown IOCB command */
2442 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2443 "0321 Unknown IOCB command "
875fbdfe 2444 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2445 type, irsp->ulpCommand,
875fbdfe
JSEC
2446 irsp->ulpStatus,
2447 irsp->ulpIoTag,
2448 irsp->ulpContext);
2449 }
2450 break;
2451 }
2452
2453 /*
2454 * The response IOCB has been processed. Update the ring
2455 * pointer in SLIM. If the port response put pointer has not
2456 * been updated, sync the pgp->rspPutInx and fetch the new port
2457 * response put pointer.
2458 */
ed957684 2459 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
875fbdfe
JSEC
2460
2461 if (pring->rspidx == portRspPut)
2462 portRspPut = le32_to_cpu(pgp->rspPutInx);
2463 }
2464
2465 ha_copy = readl(phba->HAregaddr);
2466 ha_copy >>= (LPFC_FCP_RING * 4);
2467
2468 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
2e0fef85 2469 spin_lock_irqsave(&phba->hbalock, iflags);
875fbdfe
JSEC
2470 pring->stats.iocb_rsp_full++;
2471 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
2472 writel(status, phba->CAregaddr);
2473 readl(phba->CAregaddr);
2e0fef85 2474 spin_unlock_irqrestore(&phba->hbalock, iflags);
875fbdfe
JSEC
2475 }
2476 if ((ha_copy & HA_R0CE_RSP) &&
2477 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2e0fef85 2478 spin_lock_irqsave(&phba->hbalock, iflags);
875fbdfe
JSEC
2479 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2480 pring->stats.iocb_cmd_empty++;
2481
2482 /* Force update of the local copy of cmdGetInx */
2483 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2484 lpfc_sli_resume_iocb(phba, pring);
2485
2486 if ((pring->lpfc_sli_cmd_available))
2487 (pring->lpfc_sli_cmd_available) (phba, pring);
2488
2e0fef85 2489 spin_unlock_irqrestore(&phba->hbalock, iflags);
875fbdfe
JSEC
2490 }
2491
2492 return;
2493}
2494
e59058c4 2495/**
3621a710 2496 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2497 * @phba: Pointer to HBA context object.
2498 * @pring: Pointer to driver SLI ring object.
2499 * @mask: Host attention register mask for this ring.
2500 *
2501 * This function is called from the interrupt context when there is a ring
2502 * event for the fcp ring. The caller does not hold any lock.
2503 * The function processes each response iocb in the response ring until it
2504 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2505 * LE bit set. The function will call the completion handler of the command iocb
2506 * if the response iocb indicates a completion for a command iocb or it is
2507 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2508 * function if this is an unsolicited iocb.
dea3101e 2509 * This routine presumes LPFC_FCP_RING handling and doesn't bother
e59058c4
JS
2510 * to check it explicitly. This function always returns 1.
2511 **/
dea3101e 2512static int
2e0fef85
JS
2513lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2514 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2515{
34b02dcd 2516 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2517 IOCB_t *irsp = NULL;
87f6eaff 2518 IOCB_t *entry = NULL;
dea3101e 2519 struct lpfc_iocbq *cmdiocbq = NULL;
2520 struct lpfc_iocbq rspiocbq;
dea3101e 2521 uint32_t status;
2522 uint32_t portRspPut, portRspMax;
2523 int rc = 1;
2524 lpfc_iocb_type type;
2525 unsigned long iflag;
2526 uint32_t rsp_cmpl = 0;
dea3101e 2527
2e0fef85 2528 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2529 pring->stats.iocb_event++;
2530
dea3101e 2531 /*
2532 * The next available response entry should never exceed the maximum
2533 * entries. If it does, treat it as an adapter hardware error.
2534 */
2535 portRspMax = pring->numRiocb;
2536 portRspPut = le32_to_cpu(pgp->rspPutInx);
2537 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2538 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2539 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2540 return 1;
2541 }
2542
2543 rmb();
2544 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2545 /*
2546 * Fetch an entry off the ring and copy it into a local data
2547 * structure. The copy involves a byte-swap since the
2548 * network byte order and pci byte orders are different.
2549 */
ed957684 2550 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2551 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2552
2553 if (++pring->rspidx >= portRspMax)
2554 pring->rspidx = 0;
2555
87f6eaff
JSEC
2556 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2557 (uint32_t *) &rspiocbq.iocb,
ed957684 2558 phba->iocb_rsp_size);
a4bc3379 2559 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2560 irsp = &rspiocbq.iocb;
2561
dea3101e 2562 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2563 pring->stats.iocb_rsp++;
2564 rsp_cmpl++;
2565
2566 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2567 /*
2568 * If resource errors reported from HBA, reduce
2569 * queuedepths of the SCSI device.
2570 */
2571 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2572 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2573 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2574 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2575 spin_lock_irqsave(&phba->hbalock, iflag);
2576 }
2577
dea3101e 2578 /* Rsp ring <ringno> error: IOCB */
2579 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2580 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2581 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2582 pring->ringno,
92d7f7b0
JS
2583 irsp->un.ulpWord[0],
2584 irsp->un.ulpWord[1],
2585 irsp->un.ulpWord[2],
2586 irsp->un.ulpWord[3],
2587 irsp->un.ulpWord[4],
2588 irsp->un.ulpWord[5],
d7c255b2
JS
2589 *(uint32_t *)&irsp->un1,
2590 *((uint32_t *)&irsp->un1 + 1));
dea3101e 2591 }
2592
2593 switch (type) {
2594 case LPFC_ABORT_IOCB:
2595 case LPFC_SOL_IOCB:
2596 /*
2597 * Idle exchange closed via ABTS from port. No iocb
2598 * resources need to be recovered.
2599 */
2600 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2601 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2602 "0333 IOCB cmd 0x%x"
dca9479b 2603 " processed. Skipping"
92d7f7b0 2604 " completion\n",
dca9479b 2605 irsp->ulpCommand);
dea3101e 2606 break;
2607 }
2608
604a3e30
JB
2609 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2610 &rspiocbq);
dea3101e 2611 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
b808608b
JW
2612 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2613 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2614 &rspiocbq);
2615 } else {
2e0fef85
JS
2616 spin_unlock_irqrestore(&phba->hbalock,
2617 iflag);
b808608b
JW
2618 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2619 &rspiocbq);
2e0fef85 2620 spin_lock_irqsave(&phba->hbalock,
b808608b
JW
2621 iflag);
2622 }
dea3101e 2623 }
2624 break;
a4bc3379 2625 case LPFC_UNSOL_IOCB:
2e0fef85 2626 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2627 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2628 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2629 break;
dea3101e 2630 default:
2631 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2632 char adaptermsg[LPFC_MAX_ADPTMSG];
2633 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2634 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2635 MAX_MSG_DATA);
898eb71c
JP
2636 dev_warn(&((phba->pcidev)->dev),
2637 "lpfc%d: %s\n",
dea3101e 2638 phba->brd_no, adaptermsg);
2639 } else {
2640 /* Unknown IOCB command */
2641 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2642 "0334 Unknown IOCB command "
92d7f7b0 2643 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2644 type, irsp->ulpCommand,
92d7f7b0
JS
2645 irsp->ulpStatus,
2646 irsp->ulpIoTag,
2647 irsp->ulpContext);
dea3101e 2648 }
2649 break;
2650 }
2651
2652 /*
2653 * The response IOCB has been processed. Update the ring
2654 * pointer in SLIM. If the port response put pointer has not
2655 * been updated, sync the pgp->rspPutInx and fetch the new port
2656 * response put pointer.
2657 */
ed957684 2658 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2659
2660 if (pring->rspidx == portRspPut)
2661 portRspPut = le32_to_cpu(pgp->rspPutInx);
2662 }
2663
2664 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2665 pring->stats.iocb_rsp_full++;
2666 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2667 writel(status, phba->CAregaddr);
2668 readl(phba->CAregaddr);
2669 }
2670 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2671 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2672 pring->stats.iocb_cmd_empty++;
2673
2674 /* Force update of the local copy of cmdGetInx */
2675 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2676 lpfc_sli_resume_iocb(phba, pring);
2677
2678 if ((pring->lpfc_sli_cmd_available))
2679 (pring->lpfc_sli_cmd_available) (phba, pring);
2680
2681 }
2682
2e0fef85 2683 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2684 return rc;
2685}
2686
e59058c4 2687/**
3772a991
JS
2688 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2689 * @phba: Pointer to HBA context object.
2690 * @pring: Pointer to driver SLI ring object.
2691 * @rspiocbp: Pointer to driver response IOCB object.
2692 *
2693 * This function is called from the worker thread when there is a slow-path
2694 * response IOCB to process. This function chains all the response iocbs until
2695 * seeing the iocb with the LE bit set. The function will call
2696 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2697 * completion of a command iocb. The function will call the
2698 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2699 * The function frees the resources or calls the completion handler if this
2700 * iocb is an abort completion. The function returns NULL when the response
2701 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2702 * this function shall chain the iocb on to the iocb_continueq and return the
2703 * response iocb passed in.
2704 **/
2705static struct lpfc_iocbq *
2706lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2707 struct lpfc_iocbq *rspiocbp)
2708{
2709 struct lpfc_iocbq *saveq;
2710 struct lpfc_iocbq *cmdiocbp;
2711 struct lpfc_iocbq *next_iocb;
2712 IOCB_t *irsp = NULL;
2713 uint32_t free_saveq;
2714 uint8_t iocb_cmd_type;
2715 lpfc_iocb_type type;
2716 unsigned long iflag;
2717 int rc;
2718
2719 spin_lock_irqsave(&phba->hbalock, iflag);
2720 /* First add the response iocb to the countinueq list */
2721 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2722 pring->iocb_continueq_cnt++;
2723
2724 /* Now, determine whetehr the list is completed for processing */
2725 irsp = &rspiocbp->iocb;
2726 if (irsp->ulpLe) {
2727 /*
2728 * By default, the driver expects to free all resources
2729 * associated with this iocb completion.
2730 */
2731 free_saveq = 1;
2732 saveq = list_get_first(&pring->iocb_continueq,
2733 struct lpfc_iocbq, list);
2734 irsp = &(saveq->iocb);
2735 list_del_init(&pring->iocb_continueq);
2736 pring->iocb_continueq_cnt = 0;
2737
2738 pring->stats.iocb_rsp++;
2739
2740 /*
2741 * If resource errors reported from HBA, reduce
2742 * queuedepths of the SCSI device.
2743 */
2744 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2745 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2746 spin_unlock_irqrestore(&phba->hbalock, iflag);
2747 phba->lpfc_rampdown_queue_depth(phba);
2748 spin_lock_irqsave(&phba->hbalock, iflag);
2749 }
2750
2751 if (irsp->ulpStatus) {
2752 /* Rsp ring <ringno> error: IOCB */
2753 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2754 "0328 Rsp Ring %d error: "
2755 "IOCB Data: "
2756 "x%x x%x x%x x%x "
2757 "x%x x%x x%x x%x "
2758 "x%x x%x x%x x%x "
2759 "x%x x%x x%x x%x\n",
2760 pring->ringno,
2761 irsp->un.ulpWord[0],
2762 irsp->un.ulpWord[1],
2763 irsp->un.ulpWord[2],
2764 irsp->un.ulpWord[3],
2765 irsp->un.ulpWord[4],
2766 irsp->un.ulpWord[5],
2767 *(((uint32_t *) irsp) + 6),
2768 *(((uint32_t *) irsp) + 7),
2769 *(((uint32_t *) irsp) + 8),
2770 *(((uint32_t *) irsp) + 9),
2771 *(((uint32_t *) irsp) + 10),
2772 *(((uint32_t *) irsp) + 11),
2773 *(((uint32_t *) irsp) + 12),
2774 *(((uint32_t *) irsp) + 13),
2775 *(((uint32_t *) irsp) + 14),
2776 *(((uint32_t *) irsp) + 15));
2777 }
2778
2779 /*
2780 * Fetch the IOCB command type and call the correct completion
2781 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2782 * get freed back to the lpfc_iocb_list by the discovery
2783 * kernel thread.
2784 */
2785 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2786 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2787 switch (type) {
2788 case LPFC_SOL_IOCB:
2789 spin_unlock_irqrestore(&phba->hbalock, iflag);
2790 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2791 spin_lock_irqsave(&phba->hbalock, iflag);
2792 break;
2793
2794 case LPFC_UNSOL_IOCB:
2795 spin_unlock_irqrestore(&phba->hbalock, iflag);
2796 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2797 spin_lock_irqsave(&phba->hbalock, iflag);
2798 if (!rc)
2799 free_saveq = 0;
2800 break;
2801
2802 case LPFC_ABORT_IOCB:
2803 cmdiocbp = NULL;
2804 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2805 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2806 saveq);
2807 if (cmdiocbp) {
2808 /* Call the specified completion routine */
2809 if (cmdiocbp->iocb_cmpl) {
2810 spin_unlock_irqrestore(&phba->hbalock,
2811 iflag);
2812 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2813 saveq);
2814 spin_lock_irqsave(&phba->hbalock,
2815 iflag);
2816 } else
2817 __lpfc_sli_release_iocbq(phba,
2818 cmdiocbp);
2819 }
2820 break;
2821
2822 case LPFC_UNKNOWN_IOCB:
2823 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2824 char adaptermsg[LPFC_MAX_ADPTMSG];
2825 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2826 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2827 MAX_MSG_DATA);
2828 dev_warn(&((phba->pcidev)->dev),
2829 "lpfc%d: %s\n",
2830 phba->brd_no, adaptermsg);
2831 } else {
2832 /* Unknown IOCB command */
2833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2834 "0335 Unknown IOCB "
2835 "command Data: x%x "
2836 "x%x x%x x%x\n",
2837 irsp->ulpCommand,
2838 irsp->ulpStatus,
2839 irsp->ulpIoTag,
2840 irsp->ulpContext);
2841 }
2842 break;
2843 }
2844
2845 if (free_saveq) {
2846 list_for_each_entry_safe(rspiocbp, next_iocb,
2847 &saveq->list, list) {
2848 list_del(&rspiocbp->list);
2849 __lpfc_sli_release_iocbq(phba, rspiocbp);
2850 }
2851 __lpfc_sli_release_iocbq(phba, saveq);
2852 }
2853 rspiocbp = NULL;
2854 }
2855 spin_unlock_irqrestore(&phba->hbalock, iflag);
2856 return rspiocbp;
2857}
2858
2859/**
2860 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
2861 * @phba: Pointer to HBA context object.
2862 * @pring: Pointer to driver SLI ring object.
2863 * @mask: Host attention register mask for this ring.
2864 *
3772a991
JS
2865 * This routine wraps the actual slow_ring event process routine from the
2866 * API jump table function pointer from the lpfc_hba struct.
e59058c4 2867 **/
3772a991 2868void
2e0fef85
JS
2869lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2870 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
2871{
2872 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2873}
2874
2875/**
2876 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2877 * @phba: Pointer to HBA context object.
2878 * @pring: Pointer to driver SLI ring object.
2879 * @mask: Host attention register mask for this ring.
2880 *
2881 * This function is called from the worker thread when there is a ring event
2882 * for non-fcp rings. The caller does not hold any lock. The function will
2883 * remove each response iocb in the response ring and calls the handle
2884 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2885 **/
2886static void
2887lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2888 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2889{
34b02dcd 2890 struct lpfc_pgp *pgp;
dea3101e 2891 IOCB_t *entry;
2892 IOCB_t *irsp = NULL;
2893 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 2894 uint32_t portRspPut, portRspMax;
dea3101e 2895 unsigned long iflag;
3772a991 2896 uint32_t status;
dea3101e 2897
34b02dcd 2898 pgp = &phba->port_gp[pring->ringno];
2e0fef85 2899 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2900 pring->stats.iocb_event++;
2901
dea3101e 2902 /*
2903 * The next available response entry should never exceed the maximum
2904 * entries. If it does, treat it as an adapter hardware error.
2905 */
2906 portRspMax = pring->numRiocb;
2907 portRspPut = le32_to_cpu(pgp->rspPutInx);
2908 if (portRspPut >= portRspMax) {
2909 /*
025dfdaf 2910 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 2911 * rsp ring <portRspMax>
2912 */
ed957684 2913 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2914 "0303 Ring %d handler: portRspPut %d "
025dfdaf 2915 "is bigger than rsp ring %d\n",
e8b62011 2916 pring->ringno, portRspPut, portRspMax);
dea3101e 2917
2e0fef85
JS
2918 phba->link_state = LPFC_HBA_ERROR;
2919 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2920
2921 phba->work_hs = HS_FFER3;
2922 lpfc_handle_eratt(phba);
2923
3772a991 2924 return;
dea3101e 2925 }
2926
2927 rmb();
dea3101e 2928 while (pring->rspidx != portRspPut) {
2929 /*
2930 * Build a completion list and call the appropriate handler.
2931 * The process is to get the next available response iocb, get
2932 * a free iocb from the list, copy the response data into the
2933 * free iocb, insert to the continuation list, and update the
2934 * next response index to slim. This process makes response
2935 * iocb's in the ring available to DMA as fast as possible but
2936 * pays a penalty for a copy operation. Since the iocb is
2937 * only 32 bytes, this penalty is considered small relative to
2938 * the PCI reads for register values and a slim write. When
2939 * the ulpLe field is set, the entire Command has been
2940 * received.
2941 */
ed957684
JS
2942 entry = lpfc_resp_iocb(phba, pring);
2943
858c9f6c 2944 phba->last_completion_time = jiffies;
2e0fef85 2945 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 2946 if (rspiocbp == NULL) {
2947 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 2948 "completion.\n", __func__);
dea3101e 2949 break;
2950 }
2951
ed957684
JS
2952 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2953 phba->iocb_rsp_size);
dea3101e 2954 irsp = &rspiocbp->iocb;
2955
2956 if (++pring->rspidx >= portRspMax)
2957 pring->rspidx = 0;
2958
a58cbd52
JS
2959 if (pring->ringno == LPFC_ELS_RING) {
2960 lpfc_debugfs_slow_ring_trc(phba,
2961 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2962 *(((uint32_t *) irsp) + 4),
2963 *(((uint32_t *) irsp) + 6),
2964 *(((uint32_t *) irsp) + 7));
2965 }
2966
ed957684 2967 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2968
3772a991
JS
2969 spin_unlock_irqrestore(&phba->hbalock, iflag);
2970 /* Handle the response IOCB */
2971 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2972 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2973
2974 /*
2975 * If the port response put pointer has not been updated, sync
2976 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2977 * response put pointer.
2978 */
2979 if (pring->rspidx == portRspPut) {
2980 portRspPut = le32_to_cpu(pgp->rspPutInx);
2981 }
2982 } /* while (pring->rspidx != portRspPut) */
2983
92d7f7b0 2984 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 2985 /* At least one response entry has been freed */
2986 pring->stats.iocb_rsp_full++;
2987 /* SET RxRE_RSP in Chip Att register */
2988 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2989 writel(status, phba->CAregaddr);
2990 readl(phba->CAregaddr); /* flush */
2991 }
2992 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2993 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2994 pring->stats.iocb_cmd_empty++;
2995
2996 /* Force update of the local copy of cmdGetInx */
2997 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2998 lpfc_sli_resume_iocb(phba, pring);
2999
3000 if ((pring->lpfc_sli_cmd_available))
3001 (pring->lpfc_sli_cmd_available) (phba, pring);
3002
3003 }
3004
2e0fef85 3005 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3006 return;
dea3101e 3007}
3008
4f774513
JS
3009/**
3010 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3011 * @phba: Pointer to HBA context object.
3012 * @pring: Pointer to driver SLI ring object.
3013 * @mask: Host attention register mask for this ring.
3014 *
3015 * This function is called from the worker thread when there is a pending
3016 * ELS response iocb on the driver internal slow-path response iocb worker
3017 * queue. The caller does not hold any lock. The function will remove each
3018 * response iocb from the response worker queue and calls the handle
3019 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3020 **/
3021static void
3022lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3023 struct lpfc_sli_ring *pring, uint32_t mask)
3024{
3025 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3026 struct hbq_dmabuf *dmabuf;
3027 struct lpfc_cq_event *cq_event;
4f774513
JS
3028 unsigned long iflag;
3029
3030 while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
3031 /* Get the response iocb from the head of work queue */
3032 spin_lock_irqsave(&phba->hbalock, iflag);
3033 list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
4d9ab994 3034 cq_event, struct lpfc_cq_event, list);
4f774513 3035 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3036
3037 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3038 case CQE_CODE_COMPL_WQE:
3039 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3040 cq_event);
3041 lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
3042 break;
3043 case CQE_CODE_RECEIVE:
3044 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3045 cq_event);
3046 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3047 break;
3048 default:
3049 break;
3050 }
4f774513
JS
3051 }
3052}
3053
e59058c4 3054/**
3621a710 3055 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3056 * @phba: Pointer to HBA context object.
3057 * @pring: Pointer to driver SLI ring object.
3058 *
3059 * This function aborts all iocbs in the given ring and frees all the iocb
3060 * objects in txq. This function issues an abort iocb for all the iocb commands
3061 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3062 * the return of this function. The caller is not required to hold any locks.
3063 **/
2e0fef85 3064void
dea3101e 3065lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3066{
2534ba75 3067 LIST_HEAD(completions);
dea3101e 3068 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3069
92d7f7b0
JS
3070 if (pring->ringno == LPFC_ELS_RING) {
3071 lpfc_fabric_abort_hba(phba);
3072 }
3073
dea3101e 3074 /* Error everything on txq and txcmplq
3075 * First do the txq.
3076 */
2e0fef85 3077 spin_lock_irq(&phba->hbalock);
2534ba75 3078 list_splice_init(&pring->txq, &completions);
dea3101e 3079 pring->txq_cnt = 0;
dea3101e 3080
3081 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3082 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3083 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3084
2e0fef85 3085 spin_unlock_irq(&phba->hbalock);
dea3101e 3086
a257bf90
JS
3087 /* Cancel all the IOCBs from the completions list */
3088 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3089 IOERR_SLI_ABORTED);
dea3101e 3090}
3091
a8e497d5 3092/**
3621a710 3093 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3094 * @phba: Pointer to HBA context object.
3095 *
3096 * This function flushes all iocbs in the fcp ring and frees all the iocb
3097 * objects in txq and txcmplq. This function will not issue abort iocbs
3098 * for all the iocb commands in txcmplq, they will just be returned with
3099 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3100 * slot has been permanently disabled.
3101 **/
3102void
3103lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3104{
3105 LIST_HEAD(txq);
3106 LIST_HEAD(txcmplq);
a8e497d5
JS
3107 struct lpfc_sli *psli = &phba->sli;
3108 struct lpfc_sli_ring *pring;
3109
3110 /* Currently, only one fcp ring */
3111 pring = &psli->ring[psli->fcp_ring];
3112
3113 spin_lock_irq(&phba->hbalock);
3114 /* Retrieve everything on txq */
3115 list_splice_init(&pring->txq, &txq);
3116 pring->txq_cnt = 0;
3117
3118 /* Retrieve everything on the txcmplq */
3119 list_splice_init(&pring->txcmplq, &txcmplq);
3120 pring->txcmplq_cnt = 0;
3121 spin_unlock_irq(&phba->hbalock);
3122
3123 /* Flush the txq */
a257bf90
JS
3124 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3125 IOERR_SLI_DOWN);
a8e497d5
JS
3126
3127 /* Flush the txcmpq */
a257bf90
JS
3128 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3129 IOERR_SLI_DOWN);
a8e497d5
JS
3130}
3131
e59058c4 3132/**
3772a991 3133 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3134 * @phba: Pointer to HBA context object.
3135 * @mask: Bit mask to be checked.
3136 *
3137 * This function reads the host status register and compares
3138 * with the provided bit mask to check if HBA completed
3139 * the restart. This function will wait in a loop for the
3140 * HBA to complete restart. If the HBA does not restart within
3141 * 15 iterations, the function will reset the HBA again. The
3142 * function returns 1 when HBA fail to restart otherwise returns
3143 * zero.
3144 **/
3772a991
JS
3145static int
3146lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3147{
41415862
JW
3148 uint32_t status;
3149 int i = 0;
3150 int retval = 0;
dea3101e 3151
41415862
JW
3152 /* Read the HBA Host Status Register */
3153 status = readl(phba->HSregaddr);
dea3101e 3154
41415862
JW
3155 /*
3156 * Check status register every 100ms for 5 retries, then every
3157 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3158 * every 2.5 sec for 4.
3159 * Break our of the loop if errors occurred during init.
3160 */
3161 while (((status & mask) != mask) &&
3162 !(status & HS_FFERM) &&
3163 i++ < 20) {
dea3101e 3164
41415862
JW
3165 if (i <= 5)
3166 msleep(10);
3167 else if (i <= 10)
3168 msleep(500);
3169 else
3170 msleep(2500);
dea3101e 3171
41415862 3172 if (i == 15) {
2e0fef85 3173 /* Do post */
92d7f7b0 3174 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3175 lpfc_sli_brdrestart(phba);
3176 }
3177 /* Read the HBA Host Status Register */
3178 status = readl(phba->HSregaddr);
3179 }
dea3101e 3180
41415862
JW
3181 /* Check to see if any errors occurred during init */
3182 if ((status & HS_FFERM) || (i >= 20)) {
2e0fef85 3183 phba->link_state = LPFC_HBA_ERROR;
41415862 3184 retval = 1;
dea3101e 3185 }
dea3101e 3186
41415862
JW
3187 return retval;
3188}
dea3101e 3189
da0436e9
JS
3190/**
3191 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3192 * @phba: Pointer to HBA context object.
3193 * @mask: Bit mask to be checked.
3194 *
3195 * This function checks the host status register to check if HBA is
3196 * ready. This function will wait in a loop for the HBA to be ready
3197 * If the HBA is not ready , the function will will reset the HBA PCI
3198 * function again. The function returns 1 when HBA fail to be ready
3199 * otherwise returns zero.
3200 **/
3201static int
3202lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3203{
3204 uint32_t status;
3205 int retval = 0;
3206
3207 /* Read the HBA Host Status Register */
3208 status = lpfc_sli4_post_status_check(phba);
3209
3210 if (status) {
3211 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3212 lpfc_sli_brdrestart(phba);
3213 status = lpfc_sli4_post_status_check(phba);
3214 }
3215
3216 /* Check to see if any errors occurred during init */
3217 if (status) {
3218 phba->link_state = LPFC_HBA_ERROR;
3219 retval = 1;
3220 } else
3221 phba->sli4_hba.intr_enable = 0;
3222
3223 return retval;
3224}
3225
3226/**
3227 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3228 * @phba: Pointer to HBA context object.
3229 * @mask: Bit mask to be checked.
3230 *
3231 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3232 * from the API jump table function pointer from the lpfc_hba struct.
3233 **/
3234int
3235lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3236{
3237 return phba->lpfc_sli_brdready(phba, mask);
3238}
3239
9290831f
JS
3240#define BARRIER_TEST_PATTERN (0xdeadbeef)
3241
e59058c4 3242/**
3621a710 3243 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3244 * @phba: Pointer to HBA context object.
3245 *
3246 * This function is called before resetting an HBA. This
3247 * function requests HBA to quiesce DMAs before a reset.
3248 **/
2e0fef85 3249void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3250{
65a29c16
JS
3251 uint32_t __iomem *resp_buf;
3252 uint32_t __iomem *mbox_buf;
9290831f
JS
3253 volatile uint32_t mbox;
3254 uint32_t hc_copy;
3255 int i;
3256 uint8_t hdrtype;
3257
3258 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3259 if (hdrtype != 0x80 ||
3260 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3261 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3262 return;
3263
3264 /*
3265 * Tell the other part of the chip to suspend temporarily all
3266 * its DMA activity.
3267 */
65a29c16 3268 resp_buf = phba->MBslimaddr;
9290831f
JS
3269
3270 /* Disable the error attention */
3271 hc_copy = readl(phba->HCregaddr);
3272 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3273 readl(phba->HCregaddr); /* flush */
2e0fef85 3274 phba->link_flag |= LS_IGNORE_ERATT;
9290831f
JS
3275
3276 if (readl(phba->HAregaddr) & HA_ERATT) {
3277 /* Clear Chip error bit */
3278 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3279 phba->pport->stopped = 1;
9290831f
JS
3280 }
3281
3282 mbox = 0;
3283 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3284 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3285
3286 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3287 mbox_buf = phba->MBslimaddr;
9290831f
JS
3288 writel(mbox, mbox_buf);
3289
3290 for (i = 0;
3291 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3292 mdelay(1);
3293
3294 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3295 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3296 phba->pport->stopped)
9290831f
JS
3297 goto restore_hc;
3298 else
3299 goto clear_errat;
3300 }
3301
3302 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3303 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3304 mdelay(1);
3305
3306clear_errat:
3307
3308 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3309 mdelay(1);
3310
3311 if (readl(phba->HAregaddr) & HA_ERATT) {
3312 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3313 phba->pport->stopped = 1;
9290831f
JS
3314 }
3315
3316restore_hc:
2e0fef85 3317 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3318 writel(hc_copy, phba->HCregaddr);
3319 readl(phba->HCregaddr); /* flush */
3320}
3321
e59058c4 3322/**
3621a710 3323 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3324 * @phba: Pointer to HBA context object.
3325 *
3326 * This function issues a kill_board mailbox command and waits for
3327 * the error attention interrupt. This function is called for stopping
3328 * the firmware processing. The caller is not required to hold any
3329 * locks. This function calls lpfc_hba_down_post function to free
3330 * any pending commands after the kill. The function will return 1 when it
3331 * fails to kill the board else will return 0.
3332 **/
41415862 3333int
2e0fef85 3334lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3335{
3336 struct lpfc_sli *psli;
3337 LPFC_MBOXQ_t *pmb;
3338 uint32_t status;
3339 uint32_t ha_copy;
3340 int retval;
3341 int i = 0;
dea3101e 3342
41415862 3343 psli = &phba->sli;
dea3101e 3344
41415862 3345 /* Kill HBA */
ed957684 3346 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3347 "0329 Kill HBA Data: x%x x%x\n",
3348 phba->pport->port_state, psli->sli_flag);
41415862 3349
98c9ea5c
JS
3350 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3351 if (!pmb)
41415862 3352 return 1;
41415862
JW
3353
3354 /* Disable the error attention */
2e0fef85 3355 spin_lock_irq(&phba->hbalock);
41415862
JW
3356 status = readl(phba->HCregaddr);
3357 status &= ~HC_ERINT_ENA;
3358 writel(status, phba->HCregaddr);
3359 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3360 phba->link_flag |= LS_IGNORE_ERATT;
3361 spin_unlock_irq(&phba->hbalock);
41415862
JW
3362
3363 lpfc_kill_board(phba, pmb);
3364 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3365 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3366
3367 if (retval != MBX_SUCCESS) {
3368 if (retval != MBX_BUSY)
3369 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85
JS
3370 spin_lock_irq(&phba->hbalock);
3371 phba->link_flag &= ~LS_IGNORE_ERATT;
3372 spin_unlock_irq(&phba->hbalock);
41415862
JW
3373 return 1;
3374 }
3375
f4b4c68f
JS
3376 spin_lock_irq(&phba->hbalock);
3377 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3378 spin_unlock_irq(&phba->hbalock);
9290831f 3379
41415862
JW
3380 mempool_free(pmb, phba->mbox_mem_pool);
3381
3382 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3383 * attention every 100ms for 3 seconds. If we don't get ERATT after
3384 * 3 seconds we still set HBA_ERROR state because the status of the
3385 * board is now undefined.
3386 */
3387 ha_copy = readl(phba->HAregaddr);
3388
3389 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3390 mdelay(100);
3391 ha_copy = readl(phba->HAregaddr);
3392 }
3393
3394 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3395 if (ha_copy & HA_ERATT) {
3396 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3397 phba->pport->stopped = 1;
9290831f 3398 }
2e0fef85 3399 spin_lock_irq(&phba->hbalock);
41415862 3400 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3401 psli->mbox_active = NULL;
2e0fef85
JS
3402 phba->link_flag &= ~LS_IGNORE_ERATT;
3403 spin_unlock_irq(&phba->hbalock);
41415862 3404
41415862 3405 lpfc_hba_down_post(phba);
2e0fef85 3406 phba->link_state = LPFC_HBA_ERROR;
41415862 3407
2e0fef85 3408 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 3409}
3410
e59058c4 3411/**
3772a991 3412 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3413 * @phba: Pointer to HBA context object.
3414 *
3415 * This function resets the HBA by writing HC_INITFF to the control
3416 * register. After the HBA resets, this function resets all the iocb ring
3417 * indices. This function disables PCI layer parity checking during
3418 * the reset.
3419 * This function returns 0 always.
3420 * The caller is not required to hold any locks.
3421 **/
41415862 3422int
2e0fef85 3423lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3424{
41415862 3425 struct lpfc_sli *psli;
dea3101e 3426 struct lpfc_sli_ring *pring;
41415862 3427 uint16_t cfg_value;
dea3101e 3428 int i;
dea3101e 3429
41415862 3430 psli = &phba->sli;
dea3101e 3431
41415862
JW
3432 /* Reset HBA */
3433 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3434 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3435 phba->pport->port_state, psli->sli_flag);
dea3101e 3436
3437 /* perform board reset */
3438 phba->fc_eventTag = 0;
4d9ab994 3439 phba->link_events = 0;
2e0fef85
JS
3440 phba->pport->fc_myDID = 0;
3441 phba->pport->fc_prevDID = 0;
dea3101e 3442
41415862
JW
3443 /* Turn off parity checking and serr during the physical reset */
3444 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3445 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3446 (cfg_value &
3447 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3448
3772a991
JS
3449 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3450
41415862
JW
3451 /* Now toggle INITFF bit in the Host Control Register */
3452 writel(HC_INITFF, phba->HCregaddr);
3453 mdelay(1);
3454 readl(phba->HCregaddr); /* flush */
3455 writel(0, phba->HCregaddr);
3456 readl(phba->HCregaddr); /* flush */
3457
3458 /* Restore PCI cmd register */
3459 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 3460
3461 /* Initialize relevant SLI info */
41415862
JW
3462 for (i = 0; i < psli->num_rings; i++) {
3463 pring = &psli->ring[i];
dea3101e 3464 pring->flag = 0;
3465 pring->rspidx = 0;
3466 pring->next_cmdidx = 0;
3467 pring->local_getidx = 0;
3468 pring->cmdidx = 0;
3469 pring->missbufcnt = 0;
3470 }
dea3101e 3471
2e0fef85 3472 phba->link_state = LPFC_WARM_START;
41415862
JW
3473 return 0;
3474}
3475
e59058c4 3476/**
da0436e9
JS
3477 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3478 * @phba: Pointer to HBA context object.
3479 *
3480 * This function resets a SLI4 HBA. This function disables PCI layer parity
3481 * checking during resets the device. The caller is not required to hold
3482 * any locks.
3483 *
3484 * This function returns 0 always.
3485 **/
3486int
3487lpfc_sli4_brdreset(struct lpfc_hba *phba)
3488{
3489 struct lpfc_sli *psli = &phba->sli;
3490 uint16_t cfg_value;
3491 uint8_t qindx;
3492
3493 /* Reset HBA */
3494 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3495 "0295 Reset HBA Data: x%x x%x\n",
3496 phba->pport->port_state, psli->sli_flag);
3497
3498 /* perform board reset */
3499 phba->fc_eventTag = 0;
4d9ab994 3500 phba->link_events = 0;
da0436e9
JS
3501 phba->pport->fc_myDID = 0;
3502 phba->pport->fc_prevDID = 0;
3503
3504 /* Turn off parity checking and serr during the physical reset */
3505 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3506 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3507 (cfg_value &
3508 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3509
3510 spin_lock_irq(&phba->hbalock);
3511 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3512 phba->fcf.fcf_flag = 0;
3513 /* Clean up the child queue list for the CQs */
3514 list_del_init(&phba->sli4_hba.mbx_wq->list);
3515 list_del_init(&phba->sli4_hba.els_wq->list);
3516 list_del_init(&phba->sli4_hba.hdr_rq->list);
3517 list_del_init(&phba->sli4_hba.dat_rq->list);
3518 list_del_init(&phba->sli4_hba.mbx_cq->list);
3519 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3520 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3521 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3522 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3523 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3524 spin_unlock_irq(&phba->hbalock);
3525
3526 /* Now physically reset the device */
3527 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3528 "0389 Performing PCI function reset!\n");
3529 /* Perform FCoE PCI function reset */
3530 lpfc_pci_function_reset(phba);
3531
3532 return 0;
3533}
3534
3535/**
3536 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3537 * @phba: Pointer to HBA context object.
3538 *
3539 * This function is called in the SLI initialization code path to
3540 * restart the HBA. The caller is not required to hold any lock.
3541 * This function writes MBX_RESTART mailbox command to the SLIM and
3542 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3543 * function to free any pending commands. The function enables
3544 * POST only during the first initialization. The function returns zero.
3545 * The function does not guarantee completion of MBX_RESTART mailbox
3546 * command before the return of this function.
3547 **/
da0436e9
JS
3548static int
3549lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3550{
3551 MAILBOX_t *mb;
3552 struct lpfc_sli *psli;
41415862
JW
3553 volatile uint32_t word0;
3554 void __iomem *to_slim;
0d878419 3555 uint32_t hba_aer_enabled;
41415862 3556
2e0fef85 3557 spin_lock_irq(&phba->hbalock);
41415862 3558
0d878419
JS
3559 /* Take PCIe device Advanced Error Reporting (AER) state */
3560 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
3561
41415862
JW
3562 psli = &phba->sli;
3563
3564 /* Restart HBA */
3565 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3566 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3567 phba->pport->port_state, psli->sli_flag);
41415862
JW
3568
3569 word0 = 0;
3570 mb = (MAILBOX_t *) &word0;
3571 mb->mbxCommand = MBX_RESTART;
3572 mb->mbxHc = 1;
3573
9290831f
JS
3574 lpfc_reset_barrier(phba);
3575
41415862
JW
3576 to_slim = phba->MBslimaddr;
3577 writel(*(uint32_t *) mb, to_slim);
3578 readl(to_slim); /* flush */
3579
3580 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3581 if (phba->pport->port_state)
41415862 3582 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3583 else
41415862 3584 word0 = 0; /* This is really setting up word1 */
65a29c16 3585 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3586 writel(*(uint32_t *) mb, to_slim);
3587 readl(to_slim); /* flush */
dea3101e 3588
41415862 3589 lpfc_sli_brdreset(phba);
2e0fef85
JS
3590 phba->pport->stopped = 0;
3591 phba->link_state = LPFC_INIT_START;
da0436e9 3592 phba->hba_flag = 0;
2e0fef85 3593 spin_unlock_irq(&phba->hbalock);
41415862 3594
64ba8818
JS
3595 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3596 psli->stats_start = get_seconds();
3597
eaf15d5b
JS
3598 /* Give the INITFF and Post time to settle. */
3599 mdelay(100);
41415862 3600
0d878419
JS
3601 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
3602 if (hba_aer_enabled)
3603 pci_disable_pcie_error_reporting(phba->pcidev);
3604
41415862 3605 lpfc_hba_down_post(phba);
dea3101e 3606
3607 return 0;
3608}
3609
da0436e9
JS
3610/**
3611 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3612 * @phba: Pointer to HBA context object.
3613 *
3614 * This function is called in the SLI initialization code path to restart
3615 * a SLI4 HBA. The caller is not required to hold any lock.
3616 * At the end of the function, it calls lpfc_hba_down_post function to
3617 * free any pending commands.
3618 **/
3619static int
3620lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3621{
3622 struct lpfc_sli *psli = &phba->sli;
3623
3624
3625 /* Restart HBA */
3626 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3627 "0296 Restart HBA Data: x%x x%x\n",
3628 phba->pport->port_state, psli->sli_flag);
3629
3630 lpfc_sli4_brdreset(phba);
3631
3632 spin_lock_irq(&phba->hbalock);
3633 phba->pport->stopped = 0;
3634 phba->link_state = LPFC_INIT_START;
3635 phba->hba_flag = 0;
3636 spin_unlock_irq(&phba->hbalock);
3637
3638 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3639 psli->stats_start = get_seconds();
3640
3641 lpfc_hba_down_post(phba);
3642
3643 return 0;
3644}
3645
3646/**
3647 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3648 * @phba: Pointer to HBA context object.
3649 *
3650 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3651 * API jump table function pointer from the lpfc_hba struct.
3652**/
3653int
3654lpfc_sli_brdrestart(struct lpfc_hba *phba)
3655{
3656 return phba->lpfc_sli_brdrestart(phba);
3657}
3658
e59058c4 3659/**
3621a710 3660 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
3661 * @phba: Pointer to HBA context object.
3662 *
3663 * This function is called after a HBA restart to wait for successful
3664 * restart of the HBA. Successful restart of the HBA is indicated by
3665 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3666 * iteration, the function will restart the HBA again. The function returns
3667 * zero if HBA successfully restarted else returns negative error code.
3668 **/
dea3101e 3669static int
3670lpfc_sli_chipset_init(struct lpfc_hba *phba)
3671{
3672 uint32_t status, i = 0;
3673
3674 /* Read the HBA Host Status Register */
3675 status = readl(phba->HSregaddr);
3676
3677 /* Check status register to see what current state is */
3678 i = 0;
3679 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3680
3681 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3682 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3683 * 4.
3684 */
3685 if (i++ >= 20) {
3686 /* Adapter failed to init, timeout, status reg
3687 <status> */
ed957684 3688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3689 "0436 Adapter failed to init, "
09372820
JS
3690 "timeout, status reg x%x, "
3691 "FW Data: A8 x%x AC x%x\n", status,
3692 readl(phba->MBslimaddr + 0xa8),
3693 readl(phba->MBslimaddr + 0xac));
2e0fef85 3694 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3695 return -ETIMEDOUT;
3696 }
3697
3698 /* Check to see if any errors occurred during init */
3699 if (status & HS_FFERM) {
3700 /* ERROR: During chipset initialization */
3701 /* Adapter failed to init, chipset, status reg
3702 <status> */
ed957684 3703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3704 "0437 Adapter failed to init, "
09372820
JS
3705 "chipset, status reg x%x, "
3706 "FW Data: A8 x%x AC x%x\n", status,
3707 readl(phba->MBslimaddr + 0xa8),
3708 readl(phba->MBslimaddr + 0xac));
2e0fef85 3709 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3710 return -EIO;
3711 }
3712
3713 if (i <= 5) {
3714 msleep(10);
3715 } else if (i <= 10) {
3716 msleep(500);
3717 } else {
3718 msleep(2500);
3719 }
3720
3721 if (i == 15) {
2e0fef85 3722 /* Do post */
92d7f7b0 3723 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3724 lpfc_sli_brdrestart(phba);
dea3101e 3725 }
3726 /* Read the HBA Host Status Register */
3727 status = readl(phba->HSregaddr);
3728 }
3729
3730 /* Check to see if any errors occurred during init */
3731 if (status & HS_FFERM) {
3732 /* ERROR: During chipset initialization */
3733 /* Adapter failed to init, chipset, status reg <status> */
ed957684 3734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3735 "0438 Adapter failed to init, chipset, "
09372820
JS
3736 "status reg x%x, "
3737 "FW Data: A8 x%x AC x%x\n", status,
3738 readl(phba->MBslimaddr + 0xa8),
3739 readl(phba->MBslimaddr + 0xac));
2e0fef85 3740 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3741 return -EIO;
3742 }
3743
3744 /* Clear all interrupt enable conditions */
3745 writel(0, phba->HCregaddr);
3746 readl(phba->HCregaddr); /* flush */
3747
3748 /* setup host attn register */
3749 writel(0xffffffff, phba->HAregaddr);
3750 readl(phba->HAregaddr); /* flush */
3751 return 0;
3752}
3753
e59058c4 3754/**
3621a710 3755 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
3756 *
3757 * This function calculates and returns the number of HBQs required to be
3758 * configured.
3759 **/
78b2d852 3760int
ed957684
JS
3761lpfc_sli_hbq_count(void)
3762{
92d7f7b0 3763 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
3764}
3765
e59058c4 3766/**
3621a710 3767 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
3768 *
3769 * This function adds the number of hbq entries in every HBQ to get
3770 * the total number of hbq entries required for the HBA and returns
3771 * the total count.
3772 **/
ed957684
JS
3773static int
3774lpfc_sli_hbq_entry_count(void)
3775{
3776 int hbq_count = lpfc_sli_hbq_count();
3777 int count = 0;
3778 int i;
3779
3780 for (i = 0; i < hbq_count; ++i)
92d7f7b0 3781 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
3782 return count;
3783}
3784
e59058c4 3785/**
3621a710 3786 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
3787 *
3788 * This function calculates amount of memory required for all hbq entries
3789 * to be configured and returns the total memory required.
3790 **/
dea3101e 3791int
ed957684
JS
3792lpfc_sli_hbq_size(void)
3793{
3794 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3795}
3796
e59058c4 3797/**
3621a710 3798 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
3799 * @phba: Pointer to HBA context object.
3800 *
3801 * This function is called during the SLI initialization to configure
3802 * all the HBQs and post buffers to the HBQ. The caller is not
3803 * required to hold any locks. This function will return zero if successful
3804 * else it will return negative error code.
3805 **/
ed957684
JS
3806static int
3807lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3808{
3809 int hbq_count = lpfc_sli_hbq_count();
3810 LPFC_MBOXQ_t *pmb;
3811 MAILBOX_t *pmbox;
3812 uint32_t hbqno;
3813 uint32_t hbq_entry_index;
ed957684 3814
92d7f7b0
JS
3815 /* Get a Mailbox buffer to setup mailbox
3816 * commands for HBA initialization
3817 */
ed957684
JS
3818 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3819
3820 if (!pmb)
3821 return -ENOMEM;
3822
04c68496 3823 pmbox = &pmb->u.mb;
ed957684
JS
3824
3825 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3826 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 3827 phba->hbq_in_use = 1;
ed957684
JS
3828
3829 hbq_entry_index = 0;
3830 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3831 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3832 phba->hbqs[hbqno].hbqPutIdx = 0;
3833 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3834 phba->hbqs[hbqno].entry_count =
92d7f7b0 3835 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
3836 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3837 hbq_entry_index, pmb);
ed957684
JS
3838 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3839
3840 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3841 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3842 mbxStatus <status>, ring <num> */
3843
3844 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 3845 LOG_SLI | LOG_VPORT,
e8b62011 3846 "1805 Adapter failed to init. "
ed957684 3847 "Data: x%x x%x x%x\n",
e8b62011 3848 pmbox->mbxCommand,
ed957684
JS
3849 pmbox->mbxStatus, hbqno);
3850
3851 phba->link_state = LPFC_HBA_ERROR;
3852 mempool_free(pmb, phba->mbox_mem_pool);
ed957684
JS
3853 return ENXIO;
3854 }
3855 }
3856 phba->hbq_count = hbq_count;
3857
ed957684
JS
3858 mempool_free(pmb, phba->mbox_mem_pool);
3859
92d7f7b0 3860 /* Initially populate or replenish the HBQs */
d7c255b2
JS
3861 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3862 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
3863 return 0;
3864}
3865
4f774513
JS
3866/**
3867 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3868 * @phba: Pointer to HBA context object.
3869 *
3870 * This function is called during the SLI initialization to configure
3871 * all the HBQs and post buffers to the HBQ. The caller is not
3872 * required to hold any locks. This function will return zero if successful
3873 * else it will return negative error code.
3874 **/
3875static int
3876lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3877{
3878 phba->hbq_in_use = 1;
3879 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3880 phba->hbq_count = 1;
3881 /* Initially populate or replenish the HBQs */
3882 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3883 return 0;
3884}
3885
e59058c4 3886/**
3621a710 3887 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
3888 * @phba: Pointer to HBA context object.
3889 * @sli_mode: sli mode - 2/3
3890 *
3891 * This function is called by the sli intialization code path
3892 * to issue config_port mailbox command. This function restarts the
3893 * HBA firmware and issues a config_port mailbox command to configure
3894 * the SLI interface in the sli mode specified by sli_mode
3895 * variable. The caller is not required to hold any locks.
3896 * The function returns 0 if successful, else returns negative error
3897 * code.
3898 **/
9399627f
JS
3899int
3900lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 3901{
3902 LPFC_MBOXQ_t *pmb;
3903 uint32_t resetcount = 0, rc = 0, done = 0;
3904
3905 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3906 if (!pmb) {
2e0fef85 3907 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3908 return -ENOMEM;
3909 }
3910
ed957684 3911 phba->sli_rev = sli_mode;
dea3101e 3912 while (resetcount < 2 && !done) {
2e0fef85 3913 spin_lock_irq(&phba->hbalock);
1c067a42 3914 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3915 spin_unlock_irq(&phba->hbalock);
92d7f7b0 3916 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3917 lpfc_sli_brdrestart(phba);
dea3101e 3918 rc = lpfc_sli_chipset_init(phba);
3919 if (rc)
3920 break;
3921
2e0fef85 3922 spin_lock_irq(&phba->hbalock);
1c067a42 3923 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3924 spin_unlock_irq(&phba->hbalock);
dea3101e 3925 resetcount++;
3926
ed957684
JS
3927 /* Call pre CONFIG_PORT mailbox command initialization. A
3928 * value of 0 means the call was successful. Any other
3929 * nonzero value is a failure, but if ERESTART is returned,
3930 * the driver may reset the HBA and try again.
3931 */
dea3101e 3932 rc = lpfc_config_port_prep(phba);
3933 if (rc == -ERESTART) {
ed957684 3934 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 3935 continue;
34b02dcd 3936 } else if (rc)
dea3101e 3937 break;
2e0fef85 3938 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 3939 lpfc_config_port(phba, pmb);
3940 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
3941 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3942 LPFC_SLI3_HBQ_ENABLED |
3943 LPFC_SLI3_CRP_ENABLED |
e2a0a9d6
JS
3944 LPFC_SLI3_INB_ENABLED |
3945 LPFC_SLI3_BG_ENABLED);
ed957684 3946 if (rc != MBX_SUCCESS) {
dea3101e 3947 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3948 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 3949 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 3950 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 3951 spin_lock_irq(&phba->hbalock);
04c68496 3952 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
3953 spin_unlock_irq(&phba->hbalock);
3954 rc = -ENXIO;
04c68496
JS
3955 } else {
3956 /* Allow asynchronous mailbox command to go through */
3957 spin_lock_irq(&phba->hbalock);
3958 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3959 spin_unlock_irq(&phba->hbalock);
ed957684 3960 done = 1;
04c68496 3961 }
dea3101e 3962 }
ed957684
JS
3963 if (!done) {
3964 rc = -EINVAL;
3965 goto do_prep_failed;
3966 }
04c68496
JS
3967 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3968 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
3969 rc = -ENXIO;
3970 goto do_prep_failed;
3971 }
04c68496 3972 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 3973 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
3974 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3975 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3976 phba->max_vpi : phba->max_vports;
3977
34b02dcd
JS
3978 } else
3979 phba->max_vpi = 0;
04c68496
JS
3980 if (pmb->u.mb.un.varCfgPort.gdss)
3981 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3982 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 3983 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 3984 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 3985 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
04c68496 3986 if (pmb->u.mb.un.varCfgPort.ginb) {
34b02dcd 3987 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
8f34f4ce 3988 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
34b02dcd
JS
3989 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3990 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3991 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3992 phba->inb_last_counter =
3993 phba->mbox->us.s3_inb_pgp.counter;
3994 } else {
8f34f4ce 3995 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
34b02dcd
JS
3996 phba->port_gp = phba->mbox->us.s3_pgp.port;
3997 phba->inb_ha_copy = NULL;
3998 phba->inb_counter = NULL;
3999 }
e2a0a9d6
JS
4000
4001 if (phba->cfg_enable_bg) {
04c68496 4002 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
4003 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4004 else
4005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4006 "0443 Adapter did not grant "
4007 "BlockGuard\n");
4008 }
34b02dcd 4009 } else {
8f34f4ce 4010 phba->hbq_get = NULL;
34b02dcd
JS
4011 phba->port_gp = phba->mbox->us.s2.port;
4012 phba->inb_ha_copy = NULL;
4013 phba->inb_counter = NULL;
d7c255b2 4014 phba->max_vpi = 0;
ed957684 4015 }
92d7f7b0 4016do_prep_failed:
ed957684
JS
4017 mempool_free(pmb, phba->mbox_mem_pool);
4018 return rc;
4019}
4020
e59058c4
JS
4021
4022/**
3621a710 4023 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4024 * @phba: Pointer to HBA context object.
4025 *
4026 * This function is the main SLI intialization function. This function
4027 * is called by the HBA intialization code, HBA reset code and HBA
4028 * error attention handler code. Caller is not required to hold any
4029 * locks. This function issues config_port mailbox command to configure
4030 * the SLI, setup iocb rings and HBQ rings. In the end the function
4031 * calls the config_port_post function to issue init_link mailbox
4032 * command and to start the discovery. The function will return zero
4033 * if successful, else it will return negative error code.
4034 **/
ed957684
JS
4035int
4036lpfc_sli_hba_setup(struct lpfc_hba *phba)
4037{
4038 uint32_t rc;
92d7f7b0 4039 int mode = 3;
ed957684
JS
4040
4041 switch (lpfc_sli_mode) {
4042 case 2:
78b2d852 4043 if (phba->cfg_enable_npiv) {
92d7f7b0 4044 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4045 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4046 "parameter (%d) to auto (0).\n",
e8b62011 4047 lpfc_sli_mode);
92d7f7b0
JS
4048 break;
4049 }
ed957684
JS
4050 mode = 2;
4051 break;
4052 case 0:
4053 case 3:
4054 break;
4055 default:
92d7f7b0 4056 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4057 "1819 Unrecognized lpfc_sli_mode "
4058 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4059
4060 break;
4061 }
4062
9399627f
JS
4063 rc = lpfc_sli_config_port(phba, mode);
4064
ed957684 4065 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4066 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4067 "1820 Unable to select SLI-3. "
4068 "Not supported by adapter.\n");
ed957684 4069 if (rc && mode != 2)
9399627f 4070 rc = lpfc_sli_config_port(phba, 2);
ed957684 4071 if (rc)
dea3101e 4072 goto lpfc_sli_hba_setup_error;
4073
0d878419
JS
4074 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4075 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4076 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4077 if (!rc) {
4078 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4079 "2709 This device supports "
4080 "Advanced Error Reporting (AER)\n");
4081 spin_lock_irq(&phba->hbalock);
4082 phba->hba_flag |= HBA_AER_ENABLED;
4083 spin_unlock_irq(&phba->hbalock);
4084 } else {
4085 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4086 "2708 This device does not support "
4087 "Advanced Error Reporting (AER)\n");
4088 phba->cfg_aer_support = 0;
4089 }
4090 }
4091
ed957684
JS
4092 if (phba->sli_rev == 3) {
4093 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4094 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4095 } else {
4096 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4097 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4098 phba->sli3_options = 0;
ed957684
JS
4099 }
4100
4101 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4102 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4103 phba->sli_rev, phba->max_vpi);
ed957684 4104 rc = lpfc_sli_ring_map(phba);
dea3101e 4105
4106 if (rc)
4107 goto lpfc_sli_hba_setup_error;
4108
9399627f 4109 /* Init HBQs */
ed957684
JS
4110 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4111 rc = lpfc_sli_hbq_setup(phba);
4112 if (rc)
4113 goto lpfc_sli_hba_setup_error;
4114 }
04c68496 4115 spin_lock_irq(&phba->hbalock);
dea3101e 4116 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4117 spin_unlock_irq(&phba->hbalock);
dea3101e 4118
4119 rc = lpfc_config_port_post(phba);
4120 if (rc)
4121 goto lpfc_sli_hba_setup_error;
4122
ed957684
JS
4123 return rc;
4124
92d7f7b0 4125lpfc_sli_hba_setup_error:
2e0fef85 4126 phba->link_state = LPFC_HBA_ERROR;
ed957684 4127 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 4128 "0445 Firmware initialization failed\n");
dea3101e 4129 return rc;
4130}
4131
e59058c4 4132/**
da0436e9
JS
4133 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4134 * @phba: Pointer to HBA context object.
4135 * @mboxq: mailbox pointer.
4136 * This function issue a dump mailbox command to read config region
4137 * 23 and parse the records in the region and populate driver
4138 * data structure.
e59058c4 4139 **/
da0436e9
JS
4140static int
4141lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4142 LPFC_MBOXQ_t *mboxq)
dea3101e 4143{
da0436e9
JS
4144 struct lpfc_dmabuf *mp;
4145 struct lpfc_mqe *mqe;
4146 uint32_t data_length;
4147 int rc;
dea3101e 4148
da0436e9
JS
4149 /* Program the default value of vlan_id and fc_map */
4150 phba->valid_vlan = 0;
4151 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4152 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4153 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4154
da0436e9
JS
4155 mqe = &mboxq->u.mqe;
4156 if (lpfc_dump_fcoe_param(phba, mboxq))
4157 return -ENOMEM;
4158
4159 mp = (struct lpfc_dmabuf *) mboxq->context1;
4160 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4161
4162 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4163 "(%d):2571 Mailbox cmd x%x Status x%x "
4164 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4165 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4166 "CQ: x%x x%x x%x x%x\n",
4167 mboxq->vport ? mboxq->vport->vpi : 0,
4168 bf_get(lpfc_mqe_command, mqe),
4169 bf_get(lpfc_mqe_status, mqe),
4170 mqe->un.mb_words[0], mqe->un.mb_words[1],
4171 mqe->un.mb_words[2], mqe->un.mb_words[3],
4172 mqe->un.mb_words[4], mqe->un.mb_words[5],
4173 mqe->un.mb_words[6], mqe->un.mb_words[7],
4174 mqe->un.mb_words[8], mqe->un.mb_words[9],
4175 mqe->un.mb_words[10], mqe->un.mb_words[11],
4176 mqe->un.mb_words[12], mqe->un.mb_words[13],
4177 mqe->un.mb_words[14], mqe->un.mb_words[15],
4178 mqe->un.mb_words[16], mqe->un.mb_words[50],
4179 mboxq->mcqe.word0,
4180 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4181 mboxq->mcqe.trailer);
4182
4183 if (rc) {
4184 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4185 kfree(mp);
4186 return -EIO;
4187 }
4188 data_length = mqe->un.mb_words[5];
a0c87cbd 4189 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4190 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4191 kfree(mp);
da0436e9 4192 return -EIO;
d11e31dd 4193 }
dea3101e 4194
da0436e9
JS
4195 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4196 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4197 kfree(mp);
4198 return 0;
4199}
e59058c4
JS
4200
4201/**
da0436e9
JS
4202 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4203 * @phba: pointer to lpfc hba data structure.
4204 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4205 * @vpd: pointer to the memory to hold resulting port vpd data.
4206 * @vpd_size: On input, the number of bytes allocated to @vpd.
4207 * On output, the number of data bytes in @vpd.
e59058c4 4208 *
da0436e9
JS
4209 * This routine executes a READ_REV SLI4 mailbox command. In
4210 * addition, this routine gets the port vpd data.
4211 *
4212 * Return codes
4213 * 0 - sucessful
4214 * ENOMEM - could not allocated memory.
e59058c4 4215 **/
da0436e9
JS
4216static int
4217lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4218 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4219{
da0436e9
JS
4220 int rc = 0;
4221 uint32_t dma_size;
4222 struct lpfc_dmabuf *dmabuf;
4223 struct lpfc_mqe *mqe;
dea3101e 4224
da0436e9
JS
4225 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4226 if (!dmabuf)
4227 return -ENOMEM;
4228
4229 /*
4230 * Get a DMA buffer for the vpd data resulting from the READ_REV
4231 * mailbox command.
a257bf90 4232 */
da0436e9
JS
4233 dma_size = *vpd_size;
4234 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4235 dma_size,
4236 &dmabuf->phys,
4237 GFP_KERNEL);
4238 if (!dmabuf->virt) {
4239 kfree(dmabuf);
4240 return -ENOMEM;
a257bf90 4241 }
da0436e9 4242 memset(dmabuf->virt, 0, dma_size);
a257bf90 4243
da0436e9
JS
4244 /*
4245 * The SLI4 implementation of READ_REV conflicts at word1,
4246 * bits 31:16 and SLI4 adds vpd functionality not present
4247 * in SLI3. This code corrects the conflicts.
1dcb58e5 4248 */
da0436e9
JS
4249 lpfc_read_rev(phba, mboxq);
4250 mqe = &mboxq->u.mqe;
4251 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4252 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4253 mqe->un.read_rev.word1 &= 0x0000FFFF;
4254 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4255 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4256
4257 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4258 if (rc) {
4259 dma_free_coherent(&phba->pcidev->dev, dma_size,
4260 dmabuf->virt, dmabuf->phys);
4261 return -EIO;
4262 }
1dcb58e5 4263
da0436e9
JS
4264 /*
4265 * The available vpd length cannot be bigger than the
4266 * DMA buffer passed to the port. Catch the less than
4267 * case and update the caller's size.
4268 */
4269 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4270 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4271
da0436e9
JS
4272 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4273 dma_free_coherent(&phba->pcidev->dev, dma_size,
4274 dmabuf->virt, dmabuf->phys);
4275 kfree(dmabuf);
4276 return 0;
dea3101e 4277}
4278
e59058c4 4279/**
da0436e9
JS
4280 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4281 * @phba: pointer to lpfc hba data structure.
e59058c4 4282 *
da0436e9
JS
4283 * This routine is called to explicitly arm the SLI4 device's completion and
4284 * event queues
4285 **/
4286static void
4287lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4288{
4289 uint8_t fcp_eqidx;
4290
4291 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4292 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
da0436e9
JS
4293 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4294 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4295 LPFC_QUEUE_REARM);
4296 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4297 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4298 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4299 LPFC_QUEUE_REARM);
4300}
4301
4302/**
4303 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4304 * @phba: Pointer to HBA context object.
4305 *
4306 * This function is the main SLI4 device intialization PCI function. This
4307 * function is called by the HBA intialization code, HBA reset code and
4308 * HBA error attention handler code. Caller is not required to hold any
4309 * locks.
4310 **/
4311int
4312lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4313{
4314 int rc;
4315 LPFC_MBOXQ_t *mboxq;
4316 struct lpfc_mqe *mqe;
4317 uint8_t *vpd;
4318 uint32_t vpd_size;
4319 uint32_t ftr_rsp = 0;
4320 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4321 struct lpfc_vport *vport = phba->pport;
4322 struct lpfc_dmabuf *mp;
4323
4324 /* Perform a PCI function reset to start from clean */
4325 rc = lpfc_pci_function_reset(phba);
4326 if (unlikely(rc))
4327 return -ENODEV;
4328
4329 /* Check the HBA Host Status Register for readyness */
4330 rc = lpfc_sli4_post_status_check(phba);
4331 if (unlikely(rc))
4332 return -ENODEV;
4333 else {
4334 spin_lock_irq(&phba->hbalock);
4335 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4336 spin_unlock_irq(&phba->hbalock);
4337 }
4338
4339 /*
4340 * Allocate a single mailbox container for initializing the
4341 * port.
4342 */
4343 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4344 if (!mboxq)
4345 return -ENOMEM;
4346
4347 /*
4348 * Continue initialization with default values even if driver failed
4349 * to read FCoE param config regions
4350 */
4351 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4352 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
e4e74273 4353 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
4354
4355 /* Issue READ_REV to collect vpd and FW information. */
4356 vpd_size = PAGE_SIZE;
4357 vpd = kzalloc(vpd_size, GFP_KERNEL);
4358 if (!vpd) {
4359 rc = -ENOMEM;
4360 goto out_free_mbox;
4361 }
4362
4363 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4364 if (unlikely(rc))
4365 goto out_free_vpd;
4366
4367 mqe = &mboxq->u.mqe;
f1126688
JS
4368 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4369 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4370 phba->hba_flag |= HBA_FCOE_SUPPORT;
4371 if (phba->sli_rev != LPFC_SLI_REV4 ||
4372 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
da0436e9
JS
4373 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4374 "0376 READ_REV Error. SLI Level %d "
4375 "FCoE enabled %d\n",
f1126688 4376 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
da0436e9
JS
4377 rc = -EIO;
4378 goto out_free_vpd;
4379 }
da0436e9
JS
4380 /*
4381 * Evaluate the read rev and vpd data. Populate the driver
4382 * state with the results. If this routine fails, the failure
4383 * is not fatal as the driver will use generic values.
4384 */
4385 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4386 if (unlikely(!rc)) {
4387 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4388 "0377 Error %d parsing vpd. "
4389 "Using defaults.\n", rc);
4390 rc = 0;
4391 }
4392
f1126688
JS
4393 /* Save information as VPD data */
4394 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4395 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4396 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4397 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4398 &mqe->un.read_rev);
4399 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4400 &mqe->un.read_rev);
4401 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4402 &mqe->un.read_rev);
4403 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4404 &mqe->un.read_rev);
4405 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4406 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4407 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4408 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4409 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4410 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4411 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4412 "(%d):0380 READ_REV Status x%x "
4413 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4414 mboxq->vport ? mboxq->vport->vpi : 0,
4415 bf_get(lpfc_mqe_status, mqe),
4416 phba->vpd.rev.opFwName,
4417 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4418 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
4419
4420 /*
4421 * Discover the port's supported feature set and match it against the
4422 * hosts requests.
4423 */
4424 lpfc_request_features(phba, mboxq);
4425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4426 if (unlikely(rc)) {
4427 rc = -EIO;
4428 goto out_free_vpd;
4429 }
4430
4431 /*
4432 * The port must support FCP initiator mode as this is the
4433 * only mode running in the host.
4434 */
4435 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4436 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4437 "0378 No support for fcpi mode.\n");
4438 ftr_rsp++;
4439 }
4440
4441 /*
4442 * If the port cannot support the host's requested features
4443 * then turn off the global config parameters to disable the
4444 * feature in the driver. This is not a fatal error.
4445 */
4446 if ((phba->cfg_enable_bg) &&
4447 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4448 ftr_rsp++;
4449
4450 if (phba->max_vpi && phba->cfg_enable_npiv &&
4451 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4452 ftr_rsp++;
4453
4454 if (ftr_rsp) {
4455 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4456 "0379 Feature Mismatch Data: x%08x %08x "
4457 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4458 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4459 phba->cfg_enable_npiv, phba->max_vpi);
4460 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4461 phba->cfg_enable_bg = 0;
4462 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4463 phba->cfg_enable_npiv = 0;
4464 }
4465
4466 /* These SLI3 features are assumed in SLI4 */
4467 spin_lock_irq(&phba->hbalock);
4468 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4469 spin_unlock_irq(&phba->hbalock);
4470
4471 /* Read the port's service parameters. */
4472 lpfc_read_sparam(phba, mboxq, vport->vpi);
4473 mboxq->vport = vport;
4474 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4475 mp = (struct lpfc_dmabuf *) mboxq->context1;
4476 if (rc == MBX_SUCCESS) {
4477 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4478 rc = 0;
4479 }
4480
4481 /*
4482 * This memory was allocated by the lpfc_read_sparam routine. Release
4483 * it to the mbuf pool.
4484 */
4485 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4486 kfree(mp);
4487 mboxq->context1 = NULL;
4488 if (unlikely(rc)) {
4489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4490 "0382 READ_SPARAM command failed "
4491 "status %d, mbxStatus x%x\n",
4492 rc, bf_get(lpfc_mqe_status, mqe));
4493 phba->link_state = LPFC_HBA_ERROR;
4494 rc = -EIO;
4495 goto out_free_vpd;
4496 }
4497
4498 if (phba->cfg_soft_wwnn)
4499 u64_to_wwn(phba->cfg_soft_wwnn,
4500 vport->fc_sparam.nodeName.u.wwn);
4501 if (phba->cfg_soft_wwpn)
4502 u64_to_wwn(phba->cfg_soft_wwpn,
4503 vport->fc_sparam.portName.u.wwn);
4504 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4505 sizeof(struct lpfc_name));
4506 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4507 sizeof(struct lpfc_name));
4508
4509 /* Update the fc_host data structures with new wwn. */
4510 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4511 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4512
4513 /* Register SGL pool to the device using non-embedded mailbox command */
4514 rc = lpfc_sli4_post_sgl_list(phba);
4515 if (unlikely(rc)) {
4516 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4517 "0582 Error %d during sgl post operation\n",
4518 rc);
da0436e9
JS
4519 rc = -ENODEV;
4520 goto out_free_vpd;
4521 }
4522
4523 /* Register SCSI SGL pool to the device */
4524 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4525 if (unlikely(rc)) {
4526 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4527 "0383 Error %d during scsi sgl post "
4528 "operation\n", rc);
da0436e9
JS
4529 /* Some Scsi buffers were moved to the abort scsi list */
4530 /* A pci function reset will repost them */
4531 rc = -ENODEV;
4532 goto out_free_vpd;
4533 }
4534
4535 /* Post the rpi header region to the device. */
4536 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4537 if (unlikely(rc)) {
4538 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4539 "0393 Error %d during rpi post operation\n",
4540 rc);
4541 rc = -ENODEV;
4542 goto out_free_vpd;
4543 }
0c287589
JS
4544 if (phba->cfg_enable_fip)
4545 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
4546 else
4547 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
da0436e9
JS
4548
4549 /* Set up all the queues to the device */
4550 rc = lpfc_sli4_queue_setup(phba);
4551 if (unlikely(rc)) {
4552 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4553 "0381 Error %d during queue setup.\n ", rc);
4554 goto out_stop_timers;
4555 }
4556
4557 /* Arm the CQs and then EQs on device */
4558 lpfc_sli4_arm_cqeq_intr(phba);
4559
4560 /* Indicate device interrupt mode */
4561 phba->sli4_hba.intr_enable = 1;
4562
4563 /* Allow asynchronous mailbox command to go through */
4564 spin_lock_irq(&phba->hbalock);
4565 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4566 spin_unlock_irq(&phba->hbalock);
4567
4568 /* Post receive buffers to the device */
4569 lpfc_sli4_rb_setup(phba);
4570
4571 /* Start the ELS watchdog timer */
8fa38513
JS
4572 mod_timer(&vport->els_tmofunc,
4573 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
4574
4575 /* Start heart beat timer */
4576 mod_timer(&phba->hb_tmofunc,
4577 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4578 phba->hb_outstanding = 0;
4579 phba->last_completion_time = jiffies;
4580
4581 /* Start error attention (ERATT) polling timer */
4582 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4583
4584 /*
4585 * The port is ready, set the host's link state to LINK_DOWN
4586 * in preparation for link interrupts.
4587 */
4588 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4589 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4590 lpfc_set_loopback_flag(phba);
4591 /* Change driver state to LPFC_LINK_DOWN right before init link */
4592 spin_lock_irq(&phba->hbalock);
4593 phba->link_state = LPFC_LINK_DOWN;
4594 spin_unlock_irq(&phba->hbalock);
4595 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4596 if (unlikely(rc != MBX_NOT_FINISHED)) {
4597 kfree(vpd);
4598 return 0;
4599 } else
4600 rc = -EIO;
4601
4602 /* Unset all the queues set up in this routine when error out */
4603 if (rc)
4604 lpfc_sli4_queue_unset(phba);
4605
4606out_stop_timers:
4607 if (rc)
4608 lpfc_stop_hba_timers(phba);
4609out_free_vpd:
4610 kfree(vpd);
4611out_free_mbox:
4612 mempool_free(mboxq, phba->mbox_mem_pool);
4613 return rc;
4614}
4615
4616/**
4617 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4618 * @ptr: context object - pointer to hba structure.
4619 *
4620 * This is the callback function for mailbox timer. The mailbox
4621 * timer is armed when a new mailbox command is issued and the timer
4622 * is deleted when the mailbox complete. The function is called by
4623 * the kernel timer code when a mailbox does not complete within
4624 * expected time. This function wakes up the worker thread to
4625 * process the mailbox timeout and returns. All the processing is
4626 * done by the worker thread function lpfc_mbox_timeout_handler.
4627 **/
4628void
4629lpfc_mbox_timeout(unsigned long ptr)
4630{
4631 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4632 unsigned long iflag;
4633 uint32_t tmo_posted;
4634
4635 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4636 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4637 if (!tmo_posted)
4638 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4639 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4640
4641 if (!tmo_posted)
4642 lpfc_worker_wake_up(phba);
4643 return;
4644}
4645
4646
4647/**
4648 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4649 * @phba: Pointer to HBA context object.
4650 *
4651 * This function is called from worker thread when a mailbox command times out.
4652 * The caller is not required to hold any locks. This function will reset the
4653 * HBA and recover all the pending commands.
4654 **/
4655void
4656lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4657{
4658 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 4659 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
4660 struct lpfc_sli *psli = &phba->sli;
4661 struct lpfc_sli_ring *pring;
4662
4663 /* Check the pmbox pointer first. There is a race condition
4664 * between the mbox timeout handler getting executed in the
4665 * worklist and the mailbox actually completing. When this
4666 * race condition occurs, the mbox_active will be NULL.
4667 */
4668 spin_lock_irq(&phba->hbalock);
4669 if (pmbox == NULL) {
4670 lpfc_printf_log(phba, KERN_WARNING,
4671 LOG_MBOX | LOG_SLI,
4672 "0353 Active Mailbox cleared - mailbox timeout "
4673 "exiting\n");
4674 spin_unlock_irq(&phba->hbalock);
4675 return;
4676 }
4677
4678 /* Mbox cmd <mbxCommand> timeout */
4679 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4680 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4681 mb->mbxCommand,
4682 phba->pport->port_state,
4683 phba->sli.sli_flag,
4684 phba->sli.mbox_active);
4685 spin_unlock_irq(&phba->hbalock);
4686
4687 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4688 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4689 * it to fail all oustanding SCSI IO.
4690 */
4691 spin_lock_irq(&phba->pport->work_port_lock);
4692 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4693 spin_unlock_irq(&phba->pport->work_port_lock);
4694 spin_lock_irq(&phba->hbalock);
4695 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 4696 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
4697 spin_unlock_irq(&phba->hbalock);
4698
4699 pring = &psli->ring[psli->fcp_ring];
4700 lpfc_sli_abort_iocb_ring(phba, pring);
4701
4702 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4703 "0345 Resetting board due to mailbox timeout\n");
4704
4705 /* Reset the HBA device */
4706 lpfc_reset_hba(phba);
4707}
4708
4709/**
4710 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4711 * @phba: Pointer to HBA context object.
4712 * @pmbox: Pointer to mailbox object.
4713 * @flag: Flag indicating how the mailbox need to be processed.
4714 *
4715 * This function is called by discovery code and HBA management code
4716 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4717 * function gets the hbalock to protect the data structures.
4718 * The mailbox command can be submitted in polling mode, in which case
4719 * this function will wait in a polling loop for the completion of the
4720 * mailbox.
4721 * If the mailbox is submitted in no_wait mode (not polling) the
4722 * function will submit the command and returns immediately without waiting
4723 * for the mailbox completion. The no_wait is supported only when HBA
4724 * is in SLI2/SLI3 mode - interrupts are enabled.
4725 * The SLI interface allows only one mailbox pending at a time. If the
4726 * mailbox is issued in polling mode and there is already a mailbox
4727 * pending, then the function will return an error. If the mailbox is issued
4728 * in NO_WAIT mode and there is a mailbox pending already, the function
4729 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4730 * The sli layer owns the mailbox object until the completion of mailbox
4731 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4732 * return codes the caller owns the mailbox command after the return of
4733 * the function.
e59058c4 4734 **/
3772a991
JS
4735static int
4736lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4737 uint32_t flag)
dea3101e 4738{
dea3101e 4739 MAILBOX_t *mb;
2e0fef85 4740 struct lpfc_sli *psli = &phba->sli;
dea3101e 4741 uint32_t status, evtctr;
4742 uint32_t ha_copy;
4743 int i;
09372820 4744 unsigned long timeout;
dea3101e 4745 unsigned long drvr_flag = 0;
34b02dcd 4746 uint32_t word0, ldata;
dea3101e 4747 void __iomem *to_slim;
58da1ffb
JS
4748 int processing_queue = 0;
4749
4750 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4751 if (!pmbox) {
8568a4d2 4752 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 4753 /* processing mbox queue from intr_handler */
3772a991
JS
4754 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4755 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4756 return MBX_SUCCESS;
4757 }
58da1ffb 4758 processing_queue = 1;
58da1ffb
JS
4759 pmbox = lpfc_mbox_get(phba);
4760 if (!pmbox) {
4761 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4762 return MBX_SUCCESS;
4763 }
4764 }
dea3101e 4765
ed957684 4766 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 4767 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 4768 if(!pmbox->vport) {
58da1ffb 4769 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 4770 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4771 LOG_MBOX | LOG_VPORT,
e8b62011 4772 "1806 Mbox x%x failed. No vport\n",
3772a991 4773 pmbox->u.mb.mbxCommand);
ed957684 4774 dump_stack();
58da1ffb 4775 goto out_not_finished;
ed957684
JS
4776 }
4777 }
4778
8d63f375 4779 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
4780 if (unlikely(pci_channel_offline(phba->pcidev))) {
4781 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4782 goto out_not_finished;
4783 }
8d63f375 4784
a257bf90
JS
4785 /* If HBA has a deferred error attention, fail the iocb. */
4786 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4787 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4788 goto out_not_finished;
4789 }
4790
dea3101e 4791 psli = &phba->sli;
92d7f7b0 4792
3772a991 4793 mb = &pmbox->u.mb;
dea3101e 4794 status = MBX_SUCCESS;
4795
2e0fef85
JS
4796 if (phba->link_state == LPFC_HBA_ERROR) {
4797 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
4798
4799 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4800 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4801 "(%d):0311 Mailbox command x%x cannot "
4802 "issue Data: x%x x%x\n",
4803 pmbox->vport ? pmbox->vport->vpi : 0,
4804 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4805 goto out_not_finished;
41415862
JW
4806 }
4807
9290831f
JS
4808 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4809 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2e0fef85 4810 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3772a991
JS
4811 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4812 "(%d):2528 Mailbox command x%x cannot "
4813 "issue Data: x%x x%x\n",
4814 pmbox->vport ? pmbox->vport->vpi : 0,
4815 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4816 goto out_not_finished;
9290831f
JS
4817 }
4818
dea3101e 4819 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4820 /* Polling for a mbox command when another one is already active
4821 * is not allowed in SLI. Also, the driver must have established
4822 * SLI2 mode to queue and process multiple mbox commands.
4823 */
4824
4825 if (flag & MBX_POLL) {
2e0fef85 4826 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4827
4828 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4829 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4830 "(%d):2529 Mailbox command x%x "
4831 "cannot issue Data: x%x x%x\n",
4832 pmbox->vport ? pmbox->vport->vpi : 0,
4833 pmbox->u.mb.mbxCommand,
4834 psli->sli_flag, flag);
58da1ffb 4835 goto out_not_finished;
dea3101e 4836 }
4837
3772a991 4838 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 4839 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4840 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4841 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4842 "(%d):2530 Mailbox command x%x "
4843 "cannot issue Data: x%x x%x\n",
4844 pmbox->vport ? pmbox->vport->vpi : 0,
4845 pmbox->u.mb.mbxCommand,
4846 psli->sli_flag, flag);
58da1ffb 4847 goto out_not_finished;
dea3101e 4848 }
4849
dea3101e 4850 /* Another mailbox command is still being processed, queue this
4851 * command to be processed later.
4852 */
4853 lpfc_mbox_put(phba, pmbox);
4854
4855 /* Mbox cmd issue - BUSY */
ed957684 4856 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4857 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 4858 "x%x x%x x%x x%x\n",
92d7f7b0
JS
4859 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4860 mb->mbxCommand, phba->pport->port_state,
4861 psli->sli_flag, flag);
dea3101e 4862
4863 psli->slistat.mbox_busy++;
2e0fef85 4864 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4865
858c9f6c
JS
4866 if (pmbox->vport) {
4867 lpfc_debugfs_disc_trc(pmbox->vport,
4868 LPFC_DISC_TRC_MBOX_VPORT,
4869 "MBOX Bsy 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 Bsy: cmd:x%x mb:x%x x%x",
4877 (uint32_t)mb->mbxCommand,
4878 mb->un.varWords[0], mb->un.varWords[1]);
4879 }
4880
2e0fef85 4881 return MBX_BUSY;
dea3101e 4882 }
4883
dea3101e 4884 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4885
4886 /* If we are not polling, we MUST be in SLI2 mode */
4887 if (flag != MBX_POLL) {
3772a991 4888 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 4889 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 4890 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4891 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4892 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4893 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4894 "(%d):2531 Mailbox command x%x "
4895 "cannot issue Data: x%x x%x\n",
4896 pmbox->vport ? pmbox->vport->vpi : 0,
4897 pmbox->u.mb.mbxCommand,
4898 psli->sli_flag, flag);
58da1ffb 4899 goto out_not_finished;
dea3101e 4900 }
4901 /* timeout active mbox command */
a309a6b6
JS
4902 mod_timer(&psli->mbox_tmo, (jiffies +
4903 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea3101e 4904 }
4905
4906 /* Mailbox cmd <cmd> issue */
ed957684 4907 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4908 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 4909 "x%x\n",
e8b62011 4910 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
4911 mb->mbxCommand, phba->pport->port_state,
4912 psli->sli_flag, flag);
dea3101e 4913
858c9f6c
JS
4914 if (mb->mbxCommand != MBX_HEARTBEAT) {
4915 if (pmbox->vport) {
4916 lpfc_debugfs_disc_trc(pmbox->vport,
4917 LPFC_DISC_TRC_MBOX_VPORT,
4918 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4919 (uint32_t)mb->mbxCommand,
4920 mb->un.varWords[0], mb->un.varWords[1]);
4921 }
4922 else {
4923 lpfc_debugfs_disc_trc(phba->pport,
4924 LPFC_DISC_TRC_MBOX,
4925 "MBOX Send: cmd:x%x mb:x%x x%x",
4926 (uint32_t)mb->mbxCommand,
4927 mb->un.varWords[0], mb->un.varWords[1]);
4928 }
4929 }
4930
dea3101e 4931 psli->slistat.mbox_cmd++;
4932 evtctr = psli->slistat.mbox_event;
4933
4934 /* next set own bit for the adapter and copy over command word */
4935 mb->mbxOwner = OWN_CHIP;
4936
3772a991 4937 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4938 /* First copy command data to host SLIM area */
34b02dcd 4939 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4940 } else {
9290831f 4941 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 4942 /* copy command data into host mbox for cmpl */
34b02dcd 4943 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4944 }
4945
4946 /* First copy mbox command data to HBA SLIM, skip past first
4947 word */
4948 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4949 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4950 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4951
4952 /* Next copy over first word, with mbxOwner set */
34b02dcd 4953 ldata = *((uint32_t *)mb);
dea3101e 4954 to_slim = phba->MBslimaddr;
4955 writel(ldata, to_slim);
4956 readl(to_slim); /* flush */
4957
4958 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4959 /* switch over to host mailbox */
3772a991 4960 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 4961 }
4962 }
4963
4964 wmb();
dea3101e 4965
4966 switch (flag) {
4967 case MBX_NOWAIT:
09372820 4968 /* Set up reference to mailbox command */
dea3101e 4969 psli->mbox_active = pmbox;
09372820
JS
4970 /* Interrupt board to do it */
4971 writel(CA_MBATT, phba->CAregaddr);
4972 readl(phba->CAregaddr); /* flush */
4973 /* Don't wait for it to finish, just return */
dea3101e 4974 break;
4975
4976 case MBX_POLL:
09372820 4977 /* Set up null reference to mailbox command */
dea3101e 4978 psli->mbox_active = NULL;
09372820
JS
4979 /* Interrupt board to do it */
4980 writel(CA_MBATT, phba->CAregaddr);
4981 readl(phba->CAregaddr); /* flush */
4982
3772a991 4983 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4984 /* First read mbox status word */
34b02dcd 4985 word0 = *((uint32_t *)phba->mbox);
dea3101e 4986 word0 = le32_to_cpu(word0);
4987 } else {
4988 /* First read mbox status word */
4989 word0 = readl(phba->MBslimaddr);
4990 }
4991
4992 /* Read the HBA Host Attention Register */
4993 ha_copy = readl(phba->HAregaddr);
09372820
JS
4994 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4995 mb->mbxCommand) *
4996 1000) + jiffies;
4997 i = 0;
dea3101e 4998 /* Wait for command to complete */
41415862
JW
4999 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
5000 (!(ha_copy & HA_MBATT) &&
2e0fef85 5001 (phba->link_state > LPFC_WARM_START))) {
09372820 5002 if (time_after(jiffies, timeout)) {
dea3101e 5003 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 5004 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 5005 drvr_flag);
58da1ffb 5006 goto out_not_finished;
dea3101e 5007 }
5008
5009 /* Check if we took a mbox interrupt while we were
5010 polling */
5011 if (((word0 & OWN_CHIP) != OWN_CHIP)
5012 && (evtctr != psli->slistat.mbox_event))
5013 break;
5014
09372820
JS
5015 if (i++ > 10) {
5016 spin_unlock_irqrestore(&phba->hbalock,
5017 drvr_flag);
5018 msleep(1);
5019 spin_lock_irqsave(&phba->hbalock, drvr_flag);
5020 }
dea3101e 5021
3772a991 5022 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5023 /* First copy command data */
34b02dcd 5024 word0 = *((uint32_t *)phba->mbox);
dea3101e 5025 word0 = le32_to_cpu(word0);
5026 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5027 MAILBOX_t *slimmb;
34b02dcd 5028 uint32_t slimword0;
dea3101e 5029 /* Check real SLIM for any errors */
5030 slimword0 = readl(phba->MBslimaddr);
5031 slimmb = (MAILBOX_t *) & slimword0;
5032 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5033 && slimmb->mbxStatus) {
5034 psli->sli_flag &=
3772a991 5035 ~LPFC_SLI_ACTIVE;
dea3101e 5036 word0 = slimword0;
5037 }
5038 }
5039 } else {
5040 /* First copy command data */
5041 word0 = readl(phba->MBslimaddr);
5042 }
5043 /* Read the HBA Host Attention Register */
5044 ha_copy = readl(phba->HAregaddr);
5045 }
5046
3772a991 5047 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5048 /* copy results back to user */
34b02dcd 5049 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea3101e 5050 } else {
5051 /* First copy command data */
5052 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5053 MAILBOX_CMD_SIZE);
5054 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5055 pmbox->context2) {
92d7f7b0 5056 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea3101e 5057 phba->MBslimaddr + DMP_RSP_OFFSET,
5058 mb->un.varDmp.word_cnt);
5059 }
5060 }
5061
5062 writel(HA_MBATT, phba->HAregaddr);
5063 readl(phba->HAregaddr); /* flush */
5064
5065 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5066 status = mb->mbxStatus;
5067 }
5068
2e0fef85
JS
5069 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5070 return status;
58da1ffb
JS
5071
5072out_not_finished:
5073 if (processing_queue) {
da0436e9 5074 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
5075 lpfc_mbox_cmpl_put(phba, pmbox);
5076 }
5077 return MBX_NOT_FINISHED;
dea3101e 5078}
5079
f1126688
JS
5080/**
5081 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5082 * @phba: Pointer to HBA context object.
5083 *
5084 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5085 * the driver internal pending mailbox queue. It will then try to wait out the
5086 * possible outstanding mailbox command before return.
5087 *
5088 * Returns:
5089 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5090 * the outstanding mailbox command timed out.
5091 **/
5092static int
5093lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5094{
5095 struct lpfc_sli *psli = &phba->sli;
5096 uint8_t actcmd = MBX_HEARTBEAT;
5097 int rc = 0;
5098 unsigned long timeout;
5099
5100 /* Mark the asynchronous mailbox command posting as blocked */
5101 spin_lock_irq(&phba->hbalock);
5102 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5103 if (phba->sli.mbox_active)
5104 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5105 spin_unlock_irq(&phba->hbalock);
5106 /* Determine how long we might wait for the active mailbox
5107 * command to be gracefully completed by firmware.
5108 */
5109 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5110 jiffies;
5111 /* Wait for the outstnading mailbox command to complete */
5112 while (phba->sli.mbox_active) {
5113 /* Check active mailbox complete status every 2ms */
5114 msleep(2);
5115 if (time_after(jiffies, timeout)) {
5116 /* Timeout, marked the outstanding cmd not complete */
5117 rc = 1;
5118 break;
5119 }
5120 }
5121
5122 /* Can not cleanly block async mailbox command, fails it */
5123 if (rc) {
5124 spin_lock_irq(&phba->hbalock);
5125 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5126 spin_unlock_irq(&phba->hbalock);
5127 }
5128 return rc;
5129}
5130
5131/**
5132 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5133 * @phba: Pointer to HBA context object.
5134 *
5135 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5136 * commands from the driver internal pending mailbox queue. It makes sure
5137 * that there is no outstanding mailbox command before resuming posting
5138 * asynchronous mailbox commands. If, for any reason, there is outstanding
5139 * mailbox command, it will try to wait it out before resuming asynchronous
5140 * mailbox command posting.
5141 **/
5142static void
5143lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5144{
5145 struct lpfc_sli *psli = &phba->sli;
5146
5147 spin_lock_irq(&phba->hbalock);
5148 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5149 /* Asynchronous mailbox posting is not blocked, do nothing */
5150 spin_unlock_irq(&phba->hbalock);
5151 return;
5152 }
5153
5154 /* Outstanding synchronous mailbox command is guaranteed to be done,
5155 * successful or timeout, after timing-out the outstanding mailbox
5156 * command shall always be removed, so just unblock posting async
5157 * mailbox command and resume
5158 */
5159 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5160 spin_unlock_irq(&phba->hbalock);
5161
5162 /* wake up worker thread to post asynchronlous mailbox command */
5163 lpfc_worker_wake_up(phba);
5164}
5165
da0436e9
JS
5166/**
5167 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5168 * @phba: Pointer to HBA context object.
5169 * @mboxq: Pointer to mailbox object.
5170 *
5171 * The function posts a mailbox to the port. The mailbox is expected
5172 * to be comletely filled in and ready for the port to operate on it.
5173 * This routine executes a synchronous completion operation on the
5174 * mailbox by polling for its completion.
5175 *
5176 * The caller must not be holding any locks when calling this routine.
5177 *
5178 * Returns:
5179 * MBX_SUCCESS - mailbox posted successfully
5180 * Any of the MBX error values.
5181 **/
5182static int
5183lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5184{
5185 int rc = MBX_SUCCESS;
5186 unsigned long iflag;
5187 uint32_t db_ready;
5188 uint32_t mcqe_status;
5189 uint32_t mbx_cmnd;
5190 unsigned long timeout;
5191 struct lpfc_sli *psli = &phba->sli;
5192 struct lpfc_mqe *mb = &mboxq->u.mqe;
5193 struct lpfc_bmbx_create *mbox_rgn;
5194 struct dma_address *dma_address;
5195 struct lpfc_register bmbx_reg;
5196
5197 /*
5198 * Only one mailbox can be active to the bootstrap mailbox region
5199 * at a time and there is no queueing provided.
5200 */
5201 spin_lock_irqsave(&phba->hbalock, iflag);
5202 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5203 spin_unlock_irqrestore(&phba->hbalock, iflag);
5204 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5205 "(%d):2532 Mailbox command x%x (x%x) "
5206 "cannot issue Data: x%x x%x\n",
5207 mboxq->vport ? mboxq->vport->vpi : 0,
5208 mboxq->u.mb.mbxCommand,
5209 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5210 psli->sli_flag, MBX_POLL);
5211 return MBXERR_ERROR;
5212 }
5213 /* The server grabs the token and owns it until release */
5214 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5215 phba->sli.mbox_active = mboxq;
5216 spin_unlock_irqrestore(&phba->hbalock, iflag);
5217
5218 /*
5219 * Initialize the bootstrap memory region to avoid stale data areas
5220 * in the mailbox post. Then copy the caller's mailbox contents to
5221 * the bmbx mailbox region.
5222 */
5223 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5224 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5225 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5226 sizeof(struct lpfc_mqe));
5227
5228 /* Post the high mailbox dma address to the port and wait for ready. */
5229 dma_address = &phba->sli4_hba.bmbx.dma_address;
5230 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5231
5232 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5233 * 1000) + jiffies;
5234 do {
5235 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5236 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5237 if (!db_ready)
5238 msleep(2);
5239
5240 if (time_after(jiffies, timeout)) {
5241 rc = MBXERR_ERROR;
5242 goto exit;
5243 }
5244 } while (!db_ready);
5245
5246 /* Post the low mailbox dma address to the port. */
5247 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5248 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5249 * 1000) + jiffies;
5250 do {
5251 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5252 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5253 if (!db_ready)
5254 msleep(2);
5255
5256 if (time_after(jiffies, timeout)) {
5257 rc = MBXERR_ERROR;
5258 goto exit;
5259 }
5260 } while (!db_ready);
5261
5262 /*
5263 * Read the CQ to ensure the mailbox has completed.
5264 * If so, update the mailbox status so that the upper layers
5265 * can complete the request normally.
5266 */
5267 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5268 sizeof(struct lpfc_mqe));
5269 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5270 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5271 sizeof(struct lpfc_mcqe));
5272 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5273
5274 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5275 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5276 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5277 rc = MBXERR_ERROR;
5278 }
5279
5280 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5281 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5282 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5283 " x%x x%x CQ: x%x x%x x%x x%x\n",
5284 mboxq->vport ? mboxq->vport->vpi : 0,
5285 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5286 bf_get(lpfc_mqe_status, mb),
5287 mb->un.mb_words[0], mb->un.mb_words[1],
5288 mb->un.mb_words[2], mb->un.mb_words[3],
5289 mb->un.mb_words[4], mb->un.mb_words[5],
5290 mb->un.mb_words[6], mb->un.mb_words[7],
5291 mb->un.mb_words[8], mb->un.mb_words[9],
5292 mb->un.mb_words[10], mb->un.mb_words[11],
5293 mb->un.mb_words[12], mboxq->mcqe.word0,
5294 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5295 mboxq->mcqe.trailer);
5296exit:
5297 /* We are holding the token, no needed for lock when release */
5298 spin_lock_irqsave(&phba->hbalock, iflag);
5299 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5300 phba->sli.mbox_active = NULL;
5301 spin_unlock_irqrestore(&phba->hbalock, iflag);
5302 return rc;
5303}
5304
5305/**
5306 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5307 * @phba: Pointer to HBA context object.
5308 * @pmbox: Pointer to mailbox object.
5309 * @flag: Flag indicating how the mailbox need to be processed.
5310 *
5311 * This function is called by discovery code and HBA management code to submit
5312 * a mailbox command to firmware with SLI-4 interface spec.
5313 *
5314 * Return codes the caller owns the mailbox command after the return of the
5315 * function.
5316 **/
5317static int
5318lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5319 uint32_t flag)
5320{
5321 struct lpfc_sli *psli = &phba->sli;
5322 unsigned long iflags;
5323 int rc;
5324
8fa38513
JS
5325 rc = lpfc_mbox_dev_check(phba);
5326 if (unlikely(rc)) {
5327 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5328 "(%d):2544 Mailbox command x%x (x%x) "
5329 "cannot issue Data: x%x x%x\n",
5330 mboxq->vport ? mboxq->vport->vpi : 0,
5331 mboxq->u.mb.mbxCommand,
5332 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5333 psli->sli_flag, flag);
5334 goto out_not_finished;
5335 }
5336
da0436e9
JS
5337 /* Detect polling mode and jump to a handler */
5338 if (!phba->sli4_hba.intr_enable) {
5339 if (flag == MBX_POLL)
5340 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5341 else
5342 rc = -EIO;
5343 if (rc != MBX_SUCCESS)
5344 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5345 "(%d):2541 Mailbox command x%x "
5346 "(x%x) cannot issue Data: x%x x%x\n",
5347 mboxq->vport ? mboxq->vport->vpi : 0,
5348 mboxq->u.mb.mbxCommand,
5349 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5350 psli->sli_flag, flag);
5351 return rc;
5352 } else if (flag == MBX_POLL) {
f1126688
JS
5353 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5354 "(%d):2542 Try to issue mailbox command "
5355 "x%x (x%x) synchronously ahead of async"
5356 "mailbox command queue: x%x x%x\n",
da0436e9
JS
5357 mboxq->vport ? mboxq->vport->vpi : 0,
5358 mboxq->u.mb.mbxCommand,
5359 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5360 psli->sli_flag, flag);
f1126688
JS
5361 /* Try to block the asynchronous mailbox posting */
5362 rc = lpfc_sli4_async_mbox_block(phba);
5363 if (!rc) {
5364 /* Successfully blocked, now issue sync mbox cmd */
5365 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5366 if (rc != MBX_SUCCESS)
5367 lpfc_printf_log(phba, KERN_ERR,
5368 LOG_MBOX | LOG_SLI,
5369 "(%d):2597 Mailbox command "
5370 "x%x (x%x) cannot issue "
5371 "Data: x%x x%x\n",
5372 mboxq->vport ?
5373 mboxq->vport->vpi : 0,
5374 mboxq->u.mb.mbxCommand,
5375 lpfc_sli4_mbox_opcode_get(phba,
5376 mboxq),
5377 psli->sli_flag, flag);
5378 /* Unblock the async mailbox posting afterward */
5379 lpfc_sli4_async_mbox_unblock(phba);
5380 }
5381 return rc;
da0436e9
JS
5382 }
5383
5384 /* Now, interrupt mode asynchrous mailbox command */
5385 rc = lpfc_mbox_cmd_check(phba, mboxq);
5386 if (rc) {
5387 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5388 "(%d):2543 Mailbox command x%x (x%x) "
5389 "cannot issue Data: x%x x%x\n",
5390 mboxq->vport ? mboxq->vport->vpi : 0,
5391 mboxq->u.mb.mbxCommand,
5392 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5393 psli->sli_flag, flag);
5394 goto out_not_finished;
5395 }
da0436e9
JS
5396
5397 /* Put the mailbox command to the driver internal FIFO */
5398 psli->slistat.mbox_busy++;
5399 spin_lock_irqsave(&phba->hbalock, iflags);
5400 lpfc_mbox_put(phba, mboxq);
5401 spin_unlock_irqrestore(&phba->hbalock, iflags);
5402 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5403 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5404 "x%x (x%x) x%x x%x x%x\n",
5405 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5406 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5407 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5408 phba->pport->port_state,
5409 psli->sli_flag, MBX_NOWAIT);
5410 /* Wake up worker thread to transport mailbox command from head */
5411 lpfc_worker_wake_up(phba);
5412
5413 return MBX_BUSY;
5414
5415out_not_finished:
5416 return MBX_NOT_FINISHED;
5417}
5418
5419/**
5420 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5421 * @phba: Pointer to HBA context object.
5422 *
5423 * This function is called by worker thread to send a mailbox command to
5424 * SLI4 HBA firmware.
5425 *
5426 **/
5427int
5428lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5429{
5430 struct lpfc_sli *psli = &phba->sli;
5431 LPFC_MBOXQ_t *mboxq;
5432 int rc = MBX_SUCCESS;
5433 unsigned long iflags;
5434 struct lpfc_mqe *mqe;
5435 uint32_t mbx_cmnd;
5436
5437 /* Check interrupt mode before post async mailbox command */
5438 if (unlikely(!phba->sli4_hba.intr_enable))
5439 return MBX_NOT_FINISHED;
5440
5441 /* Check for mailbox command service token */
5442 spin_lock_irqsave(&phba->hbalock, iflags);
5443 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5444 spin_unlock_irqrestore(&phba->hbalock, iflags);
5445 return MBX_NOT_FINISHED;
5446 }
5447 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5448 spin_unlock_irqrestore(&phba->hbalock, iflags);
5449 return MBX_NOT_FINISHED;
5450 }
5451 if (unlikely(phba->sli.mbox_active)) {
5452 spin_unlock_irqrestore(&phba->hbalock, iflags);
5453 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5454 "0384 There is pending active mailbox cmd\n");
5455 return MBX_NOT_FINISHED;
5456 }
5457 /* Take the mailbox command service token */
5458 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5459
5460 /* Get the next mailbox command from head of queue */
5461 mboxq = lpfc_mbox_get(phba);
5462
5463 /* If no more mailbox command waiting for post, we're done */
5464 if (!mboxq) {
5465 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5466 spin_unlock_irqrestore(&phba->hbalock, iflags);
5467 return MBX_SUCCESS;
5468 }
5469 phba->sli.mbox_active = mboxq;
5470 spin_unlock_irqrestore(&phba->hbalock, iflags);
5471
5472 /* Check device readiness for posting mailbox command */
5473 rc = lpfc_mbox_dev_check(phba);
5474 if (unlikely(rc))
5475 /* Driver clean routine will clean up pending mailbox */
5476 goto out_not_finished;
5477
5478 /* Prepare the mbox command to be posted */
5479 mqe = &mboxq->u.mqe;
5480 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5481
5482 /* Start timer for the mbox_tmo and log some mailbox post messages */
5483 mod_timer(&psli->mbox_tmo, (jiffies +
5484 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5485
5486 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5487 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5488 "x%x x%x\n",
5489 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5490 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5491 phba->pport->port_state, psli->sli_flag);
5492
5493 if (mbx_cmnd != MBX_HEARTBEAT) {
5494 if (mboxq->vport) {
5495 lpfc_debugfs_disc_trc(mboxq->vport,
5496 LPFC_DISC_TRC_MBOX_VPORT,
5497 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5498 mbx_cmnd, mqe->un.mb_words[0],
5499 mqe->un.mb_words[1]);
5500 } else {
5501 lpfc_debugfs_disc_trc(phba->pport,
5502 LPFC_DISC_TRC_MBOX,
5503 "MBOX Send: cmd:x%x mb:x%x x%x",
5504 mbx_cmnd, mqe->un.mb_words[0],
5505 mqe->un.mb_words[1]);
5506 }
5507 }
5508 psli->slistat.mbox_cmd++;
5509
5510 /* Post the mailbox command to the port */
5511 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5512 if (rc != MBX_SUCCESS) {
5513 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5514 "(%d):2533 Mailbox command x%x (x%x) "
5515 "cannot issue Data: x%x x%x\n",
5516 mboxq->vport ? mboxq->vport->vpi : 0,
5517 mboxq->u.mb.mbxCommand,
5518 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5519 psli->sli_flag, MBX_NOWAIT);
5520 goto out_not_finished;
5521 }
5522
5523 return rc;
5524
5525out_not_finished:
5526 spin_lock_irqsave(&phba->hbalock, iflags);
5527 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5528 __lpfc_mbox_cmpl_put(phba, mboxq);
5529 /* Release the token */
5530 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5531 phba->sli.mbox_active = NULL;
5532 spin_unlock_irqrestore(&phba->hbalock, iflags);
5533
5534 return MBX_NOT_FINISHED;
5535}
5536
5537/**
5538 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5539 * @phba: Pointer to HBA context object.
5540 * @pmbox: Pointer to mailbox object.
5541 * @flag: Flag indicating how the mailbox need to be processed.
5542 *
5543 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5544 * the API jump table function pointer from the lpfc_hba struct.
5545 *
5546 * Return codes the caller owns the mailbox command after the return of the
5547 * function.
5548 **/
5549int
5550lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5551{
5552 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5553}
5554
5555/**
5556 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5557 * @phba: The hba struct for which this call is being executed.
5558 * @dev_grp: The HBA PCI-Device group number.
5559 *
5560 * This routine sets up the mbox interface API function jump table in @phba
5561 * struct.
5562 * Returns: 0 - success, -ENODEV - failure.
5563 **/
5564int
5565lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5566{
5567
5568 switch (dev_grp) {
5569 case LPFC_PCI_DEV_LP:
5570 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5571 phba->lpfc_sli_handle_slow_ring_event =
5572 lpfc_sli_handle_slow_ring_event_s3;
5573 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5574 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5575 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5576 break;
5577 case LPFC_PCI_DEV_OC:
5578 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5579 phba->lpfc_sli_handle_slow_ring_event =
5580 lpfc_sli_handle_slow_ring_event_s4;
5581 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5582 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5583 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5584 break;
5585 default:
5586 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5587 "1420 Invalid HBA PCI-device group: 0x%x\n",
5588 dev_grp);
5589 return -ENODEV;
5590 break;
5591 }
5592 return 0;
5593}
5594
e59058c4 5595/**
3621a710 5596 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
5597 * @phba: Pointer to HBA context object.
5598 * @pring: Pointer to driver SLI ring object.
5599 * @piocb: Pointer to address of newly added command iocb.
5600 *
5601 * This function is called with hbalock held to add a command
5602 * iocb to the txq when SLI layer cannot submit the command iocb
5603 * to the ring.
5604 **/
858c9f6c 5605static void
92d7f7b0 5606__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5607 struct lpfc_iocbq *piocb)
dea3101e 5608{
5609 /* Insert the caller's iocb in the txq tail for later processing. */
5610 list_add_tail(&piocb->list, &pring->txq);
5611 pring->txq_cnt++;
dea3101e 5612}
5613
e59058c4 5614/**
3621a710 5615 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
5616 * @phba: Pointer to HBA context object.
5617 * @pring: Pointer to driver SLI ring object.
5618 * @piocb: Pointer to address of newly added command iocb.
5619 *
5620 * This function is called with hbalock held before a new
5621 * iocb is submitted to the firmware. This function checks
5622 * txq to flush the iocbs in txq to Firmware before
5623 * submitting new iocbs to the Firmware.
5624 * If there are iocbs in the txq which need to be submitted
5625 * to firmware, lpfc_sli_next_iocb returns the first element
5626 * of the txq after dequeuing it from txq.
5627 * If there is no iocb in the txq then the function will return
5628 * *piocb and *piocb is set to NULL. Caller needs to check
5629 * *piocb to find if there are more commands in the txq.
5630 **/
dea3101e 5631static struct lpfc_iocbq *
5632lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5633 struct lpfc_iocbq **piocb)
dea3101e 5634{
5635 struct lpfc_iocbq * nextiocb;
5636
5637 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5638 if (!nextiocb) {
5639 nextiocb = *piocb;
5640 *piocb = NULL;
5641 }
5642
5643 return nextiocb;
5644}
5645
e59058c4 5646/**
3772a991 5647 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 5648 * @phba: Pointer to HBA context object.
3772a991 5649 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
5650 * @piocb: Pointer to command iocb.
5651 * @flag: Flag indicating if this command can be put into txq.
5652 *
3772a991
JS
5653 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5654 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5655 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5656 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5657 * this function allows only iocbs for posting buffers. This function finds
5658 * next available slot in the command ring and posts the command to the
5659 * available slot and writes the port attention register to request HBA start
5660 * processing new iocb. If there is no slot available in the ring and
5661 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5662 * the function returns IOCB_BUSY.
e59058c4 5663 *
3772a991
JS
5664 * This function is called with hbalock held. The function will return success
5665 * after it successfully submit the iocb to firmware or after adding to the
5666 * txq.
e59058c4 5667 **/
98c9ea5c 5668static int
3772a991 5669__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 5670 struct lpfc_iocbq *piocb, uint32_t flag)
5671{
5672 struct lpfc_iocbq *nextiocb;
5673 IOCB_t *iocb;
3772a991 5674 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 5675
92d7f7b0
JS
5676 if (piocb->iocb_cmpl && (!piocb->vport) &&
5677 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5678 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5679 lpfc_printf_log(phba, KERN_ERR,
5680 LOG_SLI | LOG_VPORT,
e8b62011 5681 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
5682 piocb->iocb.ulpCommand);
5683 dump_stack();
5684 return IOCB_ERROR;
5685 }
5686
5687
8d63f375
LV
5688 /* If the PCI channel is in offline state, do not post iocbs. */
5689 if (unlikely(pci_channel_offline(phba->pcidev)))
5690 return IOCB_ERROR;
5691
a257bf90
JS
5692 /* If HBA has a deferred error attention, fail the iocb. */
5693 if (unlikely(phba->hba_flag & DEFER_ERATT))
5694 return IOCB_ERROR;
5695
dea3101e 5696 /*
5697 * We should never get an IOCB if we are in a < LINK_DOWN state
5698 */
2e0fef85 5699 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 5700 return IOCB_ERROR;
5701
5702 /*
5703 * Check to see if we are blocking IOCB processing because of a
0b727fea 5704 * outstanding event.
dea3101e 5705 */
0b727fea 5706 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 5707 goto iocb_busy;
5708
2e0fef85 5709 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 5710 /*
2680eeaa 5711 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 5712 * can be issued if the link is not up.
5713 */
5714 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
5715 case CMD_GEN_REQUEST64_CR:
5716 case CMD_GEN_REQUEST64_CX:
5717 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5718 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 5719 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
5720 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5721 MENLO_TRANSPORT_TYPE))
5722
5723 goto iocb_busy;
5724 break;
dea3101e 5725 case CMD_QUE_RING_BUF_CN:
5726 case CMD_QUE_RING_BUF64_CN:
dea3101e 5727 /*
5728 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5729 * completion, iocb_cmpl MUST be 0.
5730 */
5731 if (piocb->iocb_cmpl)
5732 piocb->iocb_cmpl = NULL;
5733 /*FALLTHROUGH*/
5734 case CMD_CREATE_XRI_CR:
2680eeaa
JS
5735 case CMD_CLOSE_XRI_CN:
5736 case CMD_CLOSE_XRI_CX:
dea3101e 5737 break;
5738 default:
5739 goto iocb_busy;
5740 }
5741
5742 /*
5743 * For FCP commands, we must be in a state where we can process link
5744 * attention events.
5745 */
5746 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 5747 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 5748 goto iocb_busy;
92d7f7b0 5749 }
dea3101e 5750
dea3101e 5751 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5752 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5753 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5754
5755 if (iocb)
5756 lpfc_sli_update_ring(phba, pring);
5757 else
5758 lpfc_sli_update_full_ring(phba, pring);
5759
5760 if (!piocb)
5761 return IOCB_SUCCESS;
5762
5763 goto out_busy;
5764
5765 iocb_busy:
5766 pring->stats.iocb_cmd_delay++;
5767
5768 out_busy:
5769
5770 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 5771 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 5772 return IOCB_SUCCESS;
5773 }
5774
5775 return IOCB_BUSY;
5776}
5777
3772a991 5778/**
4f774513
JS
5779 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5780 * @phba: Pointer to HBA context object.
5781 * @piocb: Pointer to command iocb.
5782 * @sglq: Pointer to the scatter gather queue object.
5783 *
5784 * This routine converts the bpl or bde that is in the IOCB
5785 * to a sgl list for the sli4 hardware. The physical address
5786 * of the bpl/bde is converted back to a virtual address.
5787 * If the IOCB contains a BPL then the list of BDE's is
5788 * converted to sli4_sge's. If the IOCB contains a single
5789 * BDE then it is converted to a single sli_sge.
5790 * The IOCB is still in cpu endianess so the contents of
5791 * the bpl can be used without byte swapping.
5792 *
5793 * Returns valid XRI = Success, NO_XRI = Failure.
5794**/
5795static uint16_t
5796lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5797 struct lpfc_sglq *sglq)
3772a991 5798{
4f774513
JS
5799 uint16_t xritag = NO_XRI;
5800 struct ulp_bde64 *bpl = NULL;
5801 struct ulp_bde64 bde;
5802 struct sli4_sge *sgl = NULL;
5803 IOCB_t *icmd;
5804 int numBdes = 0;
5805 int i = 0;
3772a991 5806
4f774513
JS
5807 if (!piocbq || !sglq)
5808 return xritag;
5809
5810 sgl = (struct sli4_sge *)sglq->sgl;
5811 icmd = &piocbq->iocb;
5812 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5813 numBdes = icmd->un.genreq64.bdl.bdeSize /
5814 sizeof(struct ulp_bde64);
5815 /* The addrHigh and addrLow fields within the IOCB
5816 * have not been byteswapped yet so there is no
5817 * need to swap them back.
5818 */
5819 bpl = (struct ulp_bde64 *)
5820 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5821
5822 if (!bpl)
5823 return xritag;
5824
5825 for (i = 0; i < numBdes; i++) {
5826 /* Should already be byte swapped. */
5827 sgl->addr_hi = bpl->addrHigh;
5828 sgl->addr_lo = bpl->addrLow;
5829 /* swap the size field back to the cpu so we
5830 * can assign it to the sgl.
5831 */
5832 bde.tus.w = le32_to_cpu(bpl->tus.w);
5833 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5834 if ((i+1) == numBdes)
5835 bf_set(lpfc_sli4_sge_last, sgl, 1);
5836 else
5837 bf_set(lpfc_sli4_sge_last, sgl, 0);
5838 sgl->word2 = cpu_to_le32(sgl->word2);
5839 sgl->word3 = cpu_to_le32(sgl->word3);
5840 bpl++;
5841 sgl++;
5842 }
5843 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5844 /* The addrHigh and addrLow fields of the BDE have not
5845 * been byteswapped yet so they need to be swapped
5846 * before putting them in the sgl.
5847 */
5848 sgl->addr_hi =
5849 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5850 sgl->addr_lo =
5851 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5852 bf_set(lpfc_sli4_sge_len, sgl,
5853 icmd->un.genreq64.bdl.bdeSize);
5854 bf_set(lpfc_sli4_sge_last, sgl, 1);
5855 sgl->word2 = cpu_to_le32(sgl->word2);
5856 sgl->word3 = cpu_to_le32(sgl->word3);
5857 }
5858 return sglq->sli4_xritag;
3772a991 5859}
92d7f7b0 5860
e59058c4 5861/**
4f774513 5862 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 5863 * @phba: Pointer to HBA context object.
e59058c4 5864 *
4f774513 5865 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
8fa38513
JS
5866 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5867 * held.
4f774513
JS
5868 *
5869 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 5870 **/
4f774513 5871static uint32_t
8fa38513 5872lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 5873{
8fa38513
JS
5874 ++phba->fcp_qidx;
5875 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5876 phba->fcp_qidx = 0;
92d7f7b0 5877
8fa38513 5878 return phba->fcp_qidx;
92d7f7b0
JS
5879}
5880
e59058c4 5881/**
4f774513 5882 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 5883 * @phba: Pointer to HBA context object.
4f774513
JS
5884 * @piocb: Pointer to command iocb.
5885 * @wqe: Pointer to the work queue entry.
e59058c4 5886 *
4f774513
JS
5887 * This routine converts the iocb command to its Work Queue Entry
5888 * equivalent. The wqe pointer should not have any fields set when
5889 * this routine is called because it will memcpy over them.
5890 * This routine does not set the CQ_ID or the WQEC bits in the
5891 * wqe.
e59058c4 5892 *
4f774513 5893 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 5894 **/
cf5bf97e 5895static int
4f774513
JS
5896lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5897 union lpfc_wqe *wqe)
cf5bf97e 5898{
4f774513
JS
5899 uint32_t payload_len = 0;
5900 uint8_t ct = 0;
5901 uint32_t fip;
5902 uint32_t abort_tag;
5903 uint8_t command_type = ELS_COMMAND_NON_FIP;
5904 uint8_t cmnd;
5905 uint16_t xritag;
5906 struct ulp_bde64 *bpl = NULL;
5907
5908 fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
5909 /* The fcp commands will set command type */
0c287589 5910 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 5911 command_type = FCP_COMMAND;
0c287589
JS
5912 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
5913 command_type = ELS_COMMAND_FIP;
5914 else
5915 command_type = ELS_COMMAND_NON_FIP;
5916
4f774513
JS
5917 /* Some of the fields are in the right position already */
5918 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5919 abort_tag = (uint32_t) iocbq->iotag;
5920 xritag = iocbq->sli4_xritag;
5921 wqe->words[7] = 0; /* The ct field has moved so reset */
5922 /* words0-2 bpl convert bde */
5923 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5924 bpl = (struct ulp_bde64 *)
5925 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5926 if (!bpl)
5927 return IOCB_ERROR;
cf5bf97e 5928
4f774513
JS
5929 /* Should already be byte swapped. */
5930 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5931 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5932 /* swap the size field back to the cpu so we
5933 * can assign it to the sgl.
5934 */
5935 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5936 payload_len = wqe->generic.bde.tus.f.bdeSize;
5937 } else
5938 payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 5939
4f774513
JS
5940 iocbq->iocb.ulpIoTag = iocbq->iotag;
5941 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 5942
4f774513
JS
5943 switch (iocbq->iocb.ulpCommand) {
5944 case CMD_ELS_REQUEST64_CR:
5945 if (!iocbq->iocb.ulpLe) {
5946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5947 "2007 Only Limited Edition cmd Format"
5948 " supported 0x%x\n",
5949 iocbq->iocb.ulpCommand);
5950 return IOCB_ERROR;
5951 }
5952 wqe->els_req.payload_len = payload_len;
5953 /* Els_reguest64 has a TMO */
5954 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5955 iocbq->iocb.ulpTimeout);
5956 /* Need a VF for word 4 set the vf bit*/
5957 bf_set(els_req64_vf, &wqe->els_req, 0);
5958 /* And a VFID for word 12 */
5959 bf_set(els_req64_vfid, &wqe->els_req, 0);
5960 /*
5961 * Set ct field to 3, indicates that the context_tag field
5962 * contains the FCFI and remote N_Port_ID is
5963 * in word 5.
5964 */
5965
5966 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5967 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5968 iocbq->iocb.ulpContext);
5969
4f774513
JS
5970 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5971 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5972 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5973 break;
5974 case CMD_XMIT_SEQUENCE64_CR:
5975 /* word3 iocb=io_tag32 wqe=payload_offset */
5976 /* payload offset used for multilpe outstanding
5977 * sequences on the same exchange
5978 */
5979 wqe->words[3] = 0;
5980 /* word4 relative_offset memcpy */
5981 /* word5 r_ctl/df_ctl memcpy */
5982 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5983 wqe->xmit_sequence.xmit_len = payload_len;
5984 break;
5985 case CMD_XMIT_BCAST64_CN:
5986 /* word3 iocb=iotag32 wqe=payload_len */
5987 wqe->words[3] = 0; /* no definition for this in wqe */
5988 /* word4 iocb=rsvd wqe=rsvd */
5989 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5990 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5991 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5992 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5993 break;
5994 case CMD_FCP_IWRITE64_CR:
5995 command_type = FCP_COMMAND_DATA_OUT;
5996 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5997 * confusing.
5998 * word3 is payload_len: byte offset to the sgl entry for the
5999 * fcp_command.
6000 * word4 is total xfer len, same as the IOCB->ulpParameter.
6001 * word5 is initial xfer len 0 = wait for xfer-ready
6002 */
6003
6004 /* Always wait for xfer-ready before sending data */
6005 wqe->fcp_iwrite.initial_xfer_len = 0;
6006 /* word 4 (xfer length) should have been set on the memcpy */
6007
6008 /* allow write to fall through to read */
6009 case CMD_FCP_IREAD64_CR:
6010 /* FCP_CMD is always the 1st sgl entry */
6011 wqe->fcp_iread.payload_len =
6012 payload_len + sizeof(struct fcp_rsp);
6013
6014 /* word 4 (xfer length) should have been set on the memcpy */
6015
6016 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
6017 iocbq->iocb.ulpFCP2Rcvy);
6018 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
6019 /* The XC bit and the XS bit are similar. The driver never
6020 * tracked whether or not the exchange was previouslly open.
6021 * XC = Exchange create, 0 is create. 1 is already open.
6022 * XS = link cmd: 1 do not close the exchange after command.
6023 * XS = 0 close exchange when command completes.
6024 * The only time we would not set the XC bit is when the XS bit
6025 * is set and we are sending our 2nd or greater command on
6026 * this exchange.
6027 */
f1126688
JS
6028 /* Always open the exchange */
6029 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
4f774513 6030
f1126688
JS
6031 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6032 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6033 break;
4f774513
JS
6034 case CMD_FCP_ICMND64_CR:
6035 /* Always open the exchange */
6036 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6037
f1126688 6038 wqe->words[4] = 0;
4f774513 6039 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
f1126688 6040 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
4f774513
JS
6041 break;
6042 case CMD_GEN_REQUEST64_CR:
6043 /* word3 command length is described as byte offset to the
6044 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6045 * sgl[0] = cmnd
6046 * sgl[1] = rsp.
6047 *
6048 */
6049 wqe->gen_req.command_len = payload_len;
6050 /* Word4 parameter copied in the memcpy */
6051 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6052 /* word6 context tag copied in memcpy */
6053 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6054 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6055 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6056 "2015 Invalid CT %x command 0x%x\n",
6057 ct, iocbq->iocb.ulpCommand);
6058 return IOCB_ERROR;
6059 }
6060 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6061 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6062 iocbq->iocb.ulpTimeout);
6063
6064 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6065 command_type = OTHER_COMMAND;
6066 break;
6067 case CMD_XMIT_ELS_RSP64_CX:
6068 /* words0-2 BDE memcpy */
6069 /* word3 iocb=iotag32 wqe=rsvd */
6070 wqe->words[3] = 0;
6071 /* word4 iocb=did wge=rsvd. */
6072 wqe->words[4] = 0;
6073 /* word5 iocb=rsvd wge=did */
6074 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6075 iocbq->iocb.un.elsreq64.remoteID);
6076
6077 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6078 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6079
6080 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6081 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6082 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6083 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6084 iocbq->vport->vpi + phba->vpi_base);
6085 command_type = OTHER_COMMAND;
6086 break;
6087 case CMD_CLOSE_XRI_CN:
6088 case CMD_ABORT_XRI_CN:
6089 case CMD_ABORT_XRI_CX:
6090 /* words 0-2 memcpy should be 0 rserved */
6091 /* port will send abts */
6092 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6093 /*
6094 * The link is down so the fw does not need to send abts
6095 * on the wire.
6096 */
6097 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6098 else
6099 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6100 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6101 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6102 wqe->words[5] = 0;
6103 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6104 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6105 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6106 wqe->generic.abort_tag = abort_tag;
6107 /*
6108 * The abort handler will send us CMD_ABORT_XRI_CN or
6109 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6110 */
6111 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6112 cmnd = CMD_ABORT_XRI_CX;
6113 command_type = OTHER_COMMAND;
6114 xritag = 0;
6115 break;
6669f9bb
JS
6116 case CMD_XMIT_BLS_RSP64_CX:
6117 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6118 * we re-construct this WQE here based on information in
6119 * iocbq from scratch.
6120 */
6121 memset(wqe, 0, sizeof(union lpfc_wqe));
6122 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6123 iocbq->iocb.un.ulpWord[3]);
6124 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6125 iocbq->sli4_xritag);
6126 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6127 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6128 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6129 iocbq->iocb.ulpContext);
6130 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6131 command_type = OTHER_COMMAND;
6132 break;
4f774513
JS
6133 case CMD_XRI_ABORTED_CX:
6134 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6135 /* words0-2 are all 0's no bde */
6136 /* word3 and word4 are rsvrd */
6137 wqe->words[3] = 0;
6138 wqe->words[4] = 0;
6139 /* word5 iocb=rsvd wge=did */
6140 /* There is no remote port id in the IOCB? */
6141 /* Let this fall through and fail */
6142 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6143 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6144 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6145 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6146 default:
6147 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6148 "2014 Invalid command 0x%x\n",
6149 iocbq->iocb.ulpCommand);
6150 return IOCB_ERROR;
6151 break;
6152
6153 }
6154 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6155 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6156 wqe->generic.abort_tag = abort_tag;
6157 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6158 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6159 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6160 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6161
6162 return 0;
6163}
6164
6165/**
6166 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6167 * @phba: Pointer to HBA context object.
6168 * @ring_number: SLI ring number to issue iocb on.
6169 * @piocb: Pointer to command iocb.
6170 * @flag: Flag indicating if this command can be put into txq.
6171 *
6172 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6173 * an iocb command to an HBA with SLI-4 interface spec.
6174 *
6175 * This function is called with hbalock held. The function will return success
6176 * after it successfully submit the iocb to firmware or after adding to the
6177 * txq.
6178 **/
6179static int
6180__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6181 struct lpfc_iocbq *piocb, uint32_t flag)
6182{
6183 struct lpfc_sglq *sglq;
6184 uint16_t xritag;
6185 union lpfc_wqe wqe;
6186 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6187 uint32_t fcp_wqidx;
6188
6189 if (piocb->sli4_xritag == NO_XRI) {
6190 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6669f9bb 6191 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
6192 sglq = NULL;
6193 else {
6194 sglq = __lpfc_sli_get_sglq(phba);
6195 if (!sglq)
6196 return IOCB_ERROR;
6197 piocb->sli4_xritag = sglq->sli4_xritag;
6198 }
6199 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6200 sglq = NULL; /* These IO's already have an XRI and
6201 * a mapped sgl.
6202 */
6203 } else {
6204 /* This is a continuation of a commandi,(CX) so this
6205 * sglq is on the active list
6206 */
6207 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6208 if (!sglq)
6209 return IOCB_ERROR;
6210 }
6211
6212 if (sglq) {
6213 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6214 if (xritag != sglq->sli4_xritag)
6215 return IOCB_ERROR;
6216 }
6217
6218 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6219 return IOCB_ERROR;
6220
6221 if (piocb->iocb_flag & LPFC_IO_FCP) {
8fa38513 6222 fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
4f774513
JS
6223 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
6224 return IOCB_ERROR;
6225 } else {
6226 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6227 return IOCB_ERROR;
6228 }
6229 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6230
6231 return 0;
6232}
6233
6234/**
6235 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6236 *
6237 * This routine wraps the actual lockless version for issusing IOCB function
6238 * pointer from the lpfc_hba struct.
6239 *
6240 * Return codes:
6241 * IOCB_ERROR - Error
6242 * IOCB_SUCCESS - Success
6243 * IOCB_BUSY - Busy
6244 **/
6245static inline int
6246__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6247 struct lpfc_iocbq *piocb, uint32_t flag)
6248{
6249 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6250}
6251
6252/**
6253 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6254 * @phba: The hba struct for which this call is being executed.
6255 * @dev_grp: The HBA PCI-Device group number.
6256 *
6257 * This routine sets up the SLI interface API function jump table in @phba
6258 * struct.
6259 * Returns: 0 - success, -ENODEV - failure.
6260 **/
6261int
6262lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6263{
6264
6265 switch (dev_grp) {
6266 case LPFC_PCI_DEV_LP:
6267 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6268 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6269 break;
6270 case LPFC_PCI_DEV_OC:
6271 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6272 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6273 break;
6274 default:
6275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6276 "1419 Invalid HBA PCI-device group: 0x%x\n",
6277 dev_grp);
6278 return -ENODEV;
6279 break;
6280 }
6281 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6282 return 0;
6283}
6284
6285/**
6286 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6287 * @phba: Pointer to HBA context object.
6288 * @pring: Pointer to driver SLI ring object.
6289 * @piocb: Pointer to command iocb.
6290 * @flag: Flag indicating if this command can be put into txq.
6291 *
6292 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6293 * function. This function gets the hbalock and calls
6294 * __lpfc_sli_issue_iocb function and will return the error returned
6295 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6296 * functions which do not hold hbalock.
6297 **/
6298int
6299lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6300 struct lpfc_iocbq *piocb, uint32_t flag)
6301{
6302 unsigned long iflags;
6303 int rc;
6304
6305 spin_lock_irqsave(&phba->hbalock, iflags);
6306 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6307 spin_unlock_irqrestore(&phba->hbalock, iflags);
6308
6309 return rc;
6310}
6311
6312/**
6313 * lpfc_extra_ring_setup - Extra ring setup function
6314 * @phba: Pointer to HBA context object.
6315 *
6316 * This function is called while driver attaches with the
6317 * HBA to setup the extra ring. The extra ring is used
6318 * only when driver needs to support target mode functionality
6319 * or IP over FC functionalities.
6320 *
6321 * This function is called with no lock held.
6322 **/
6323static int
6324lpfc_extra_ring_setup( struct lpfc_hba *phba)
6325{
6326 struct lpfc_sli *psli;
6327 struct lpfc_sli_ring *pring;
6328
6329 psli = &phba->sli;
6330
6331 /* Adjust cmd/rsp ring iocb entries more evenly */
6332
6333 /* Take some away from the FCP ring */
6334 pring = &psli->ring[psli->fcp_ring];
6335 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6336 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
6337 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6338 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6339
a4bc3379
JS
6340 /* and give them to the extra ring */
6341 pring = &psli->ring[psli->extra_ring];
6342
cf5bf97e
JW
6343 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6344 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6345 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6346 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6347
6348 /* Setup default profile for this ring */
6349 pring->iotag_max = 4096;
6350 pring->num_mask = 1;
6351 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
6352 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6353 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
6354 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6355 return 0;
6356}
6357
e59058c4 6358/**
3621a710 6359 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
6360 * @phba: Pointer to HBA context object.
6361 * @pring: Pointer to driver SLI ring object.
6362 * @iocbq: Pointer to iocb object.
6363 *
6364 * This function is called by the slow ring event handler
6365 * function when there is an ASYNC event iocb in the ring.
6366 * This function is called with no lock held.
6367 * Currently this function handles only temperature related
6368 * ASYNC events. The function decodes the temperature sensor
6369 * event message and posts events for the management applications.
6370 **/
98c9ea5c 6371static void
57127f15
JS
6372lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6373 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6374{
6375 IOCB_t *icmd;
6376 uint16_t evt_code;
6377 uint16_t temp;
6378 struct temp_event temp_event_data;
6379 struct Scsi_Host *shost;
a257bf90 6380 uint32_t *iocb_w;
57127f15
JS
6381
6382 icmd = &iocbq->iocb;
6383 evt_code = icmd->un.asyncstat.evt_code;
6384 temp = icmd->ulpContext;
6385
6386 if ((evt_code != ASYNC_TEMP_WARN) &&
6387 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 6388 iocb_w = (uint32_t *) icmd;
57127f15
JS
6389 lpfc_printf_log(phba,
6390 KERN_ERR,
6391 LOG_SLI,
76bb24ef 6392 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 6393 " evt_code 0x%x\n"
a257bf90
JS
6394 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6395 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6396 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6397 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 6398 pring->ringno,
a257bf90
JS
6399 icmd->un.asyncstat.evt_code,
6400 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6401 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6402 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6403 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6404
57127f15
JS
6405 return;
6406 }
6407 temp_event_data.data = (uint32_t)temp;
6408 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6409 if (evt_code == ASYNC_TEMP_WARN) {
6410 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6411 lpfc_printf_log(phba,
09372820 6412 KERN_ERR,
57127f15 6413 LOG_TEMP,
76bb24ef 6414 "0347 Adapter is very hot, please take "
57127f15
JS
6415 "corrective action. temperature : %d Celsius\n",
6416 temp);
6417 }
6418 if (evt_code == ASYNC_TEMP_SAFE) {
6419 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6420 lpfc_printf_log(phba,
09372820 6421 KERN_ERR,
57127f15
JS
6422 LOG_TEMP,
6423 "0340 Adapter temperature is OK now. "
6424 "temperature : %d Celsius\n",
6425 temp);
6426 }
6427
6428 /* Send temperature change event to applications */
6429 shost = lpfc_shost_from_vport(phba->pport);
6430 fc_host_post_vendor_event(shost, fc_get_event_number(),
6431 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 6432 LPFC_NL_VENDOR_ID);
57127f15
JS
6433
6434}
6435
6436
e59058c4 6437/**
3621a710 6438 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
6439 * @phba: Pointer to HBA context object.
6440 *
6441 * lpfc_sli_setup sets up rings of the SLI interface with
6442 * number of iocbs per ring and iotags. This function is
6443 * called while driver attach to the HBA and before the
6444 * interrupts are enabled. So there is no need for locking.
6445 *
6446 * This function always returns 0.
6447 **/
dea3101e 6448int
6449lpfc_sli_setup(struct lpfc_hba *phba)
6450{
ed957684 6451 int i, totiocbsize = 0;
dea3101e 6452 struct lpfc_sli *psli = &phba->sli;
6453 struct lpfc_sli_ring *pring;
6454
6455 psli->num_rings = MAX_CONFIGURED_RINGS;
6456 psli->sli_flag = 0;
6457 psli->fcp_ring = LPFC_FCP_RING;
6458 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 6459 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 6460
604a3e30
JB
6461 psli->iocbq_lookup = NULL;
6462 psli->iocbq_lookup_len = 0;
6463 psli->last_iotag = 0;
6464
dea3101e 6465 for (i = 0; i < psli->num_rings; i++) {
6466 pring = &psli->ring[i];
6467 switch (i) {
6468 case LPFC_FCP_RING: /* ring 0 - FCP */
6469 /* numCiocb and numRiocb are used in config_port */
6470 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6471 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6472 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6473 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6474 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6475 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 6476 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6477 SLI3_IOCB_CMD_SIZE :
6478 SLI2_IOCB_CMD_SIZE;
ed957684 6479 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6480 SLI3_IOCB_RSP_SIZE :
6481 SLI2_IOCB_RSP_SIZE;
dea3101e 6482 pring->iotag_ctr = 0;
6483 pring->iotag_max =
92d7f7b0 6484 (phba->cfg_hba_queue_depth * 2);
dea3101e 6485 pring->fast_iotag = pring->iotag_max;
6486 pring->num_mask = 0;
6487 break;
a4bc3379 6488 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 6489 /* numCiocb and numRiocb are used in config_port */
6490 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6491 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 6492 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6493 SLI3_IOCB_CMD_SIZE :
6494 SLI2_IOCB_CMD_SIZE;
ed957684 6495 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6496 SLI3_IOCB_RSP_SIZE :
6497 SLI2_IOCB_RSP_SIZE;
2e0fef85 6498 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 6499 pring->num_mask = 0;
6500 break;
6501 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6502 /* numCiocb and numRiocb are used in config_port */
6503 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6504 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 6505 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6506 SLI3_IOCB_CMD_SIZE :
6507 SLI2_IOCB_CMD_SIZE;
ed957684 6508 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6509 SLI3_IOCB_RSP_SIZE :
6510 SLI2_IOCB_RSP_SIZE;
dea3101e 6511 pring->fast_iotag = 0;
6512 pring->iotag_ctr = 0;
6513 pring->iotag_max = 4096;
57127f15
JS
6514 pring->lpfc_sli_rcv_async_status =
6515 lpfc_sli_async_event_handler;
6669f9bb 6516 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 6517 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
6518 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6519 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 6520 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 6521 lpfc_els_unsol_event;
dea3101e 6522 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
6523 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6524 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 6525 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 6526 lpfc_els_unsol_event;
dea3101e 6527 pring->prt[2].profile = 0; /* Mask 2 */
6528 /* NameServer Inquiry */
6a9c52cf 6529 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 6530 /* NameServer */
6a9c52cf 6531 pring->prt[2].type = FC_TYPE_CT;
dea3101e 6532 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 6533 lpfc_ct_unsol_event;
dea3101e 6534 pring->prt[3].profile = 0; /* Mask 3 */
6535 /* NameServer response */
6a9c52cf 6536 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 6537 /* NameServer */
6a9c52cf 6538 pring->prt[3].type = FC_TYPE_CT;
dea3101e 6539 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 6540 lpfc_ct_unsol_event;
6669f9bb
JS
6541 /* abort unsolicited sequence */
6542 pring->prt[4].profile = 0; /* Mask 4 */
6543 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6544 pring->prt[4].type = FC_TYPE_BLS;
6545 pring->prt[4].lpfc_sli_rcv_unsol_event =
6546 lpfc_sli4_ct_abort_unsol_event;
dea3101e 6547 break;
6548 }
ed957684 6549 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 6550 (pring->numRiocb * pring->sizeRiocb);
dea3101e 6551 }
ed957684 6552 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 6553 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
6554 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6555 "SLI2 SLIM Data: x%x x%lx\n",
6556 phba->brd_no, totiocbsize,
6557 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 6558 }
cf5bf97e
JW
6559 if (phba->cfg_multi_ring_support == 2)
6560 lpfc_extra_ring_setup(phba);
dea3101e 6561
6562 return 0;
6563}
6564
e59058c4 6565/**
3621a710 6566 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
6567 * @phba: Pointer to HBA context object.
6568 *
6569 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6570 * ring. This function also initializes ring indices of each ring.
6571 * This function is called during the initialization of the SLI
6572 * interface of an HBA.
6573 * This function is called with no lock held and always returns
6574 * 1.
6575 **/
dea3101e 6576int
2e0fef85 6577lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e 6578{
6579 struct lpfc_sli *psli;
6580 struct lpfc_sli_ring *pring;
604a3e30 6581 int i;
dea3101e 6582
6583 psli = &phba->sli;
2e0fef85 6584 spin_lock_irq(&phba->hbalock);
dea3101e 6585 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 6586 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 6587 /* Initialize list headers for txq and txcmplq as double linked lists */
6588 for (i = 0; i < psli->num_rings; i++) {
6589 pring = &psli->ring[i];
6590 pring->ringno = i;
6591 pring->next_cmdidx = 0;
6592 pring->local_getidx = 0;
6593 pring->cmdidx = 0;
6594 INIT_LIST_HEAD(&pring->txq);
6595 INIT_LIST_HEAD(&pring->txcmplq);
6596 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 6597 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 6598 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 6599 }
2e0fef85
JS
6600 spin_unlock_irq(&phba->hbalock);
6601 return 1;
dea3101e 6602}
6603
04c68496
JS
6604/**
6605 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6606 * @phba: Pointer to HBA context object.
6607 *
6608 * This routine flushes the mailbox command subsystem. It will unconditionally
6609 * flush all the mailbox commands in the three possible stages in the mailbox
6610 * command sub-system: pending mailbox command queue; the outstanding mailbox
6611 * command; and completed mailbox command queue. It is caller's responsibility
6612 * to make sure that the driver is in the proper state to flush the mailbox
6613 * command sub-system. Namely, the posting of mailbox commands into the
6614 * pending mailbox command queue from the various clients must be stopped;
6615 * either the HBA is in a state that it will never works on the outstanding
6616 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6617 * mailbox command has been completed.
6618 **/
6619static void
6620lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6621{
6622 LIST_HEAD(completions);
6623 struct lpfc_sli *psli = &phba->sli;
6624 LPFC_MBOXQ_t *pmb;
6625 unsigned long iflag;
6626
6627 /* Flush all the mailbox commands in the mbox system */
6628 spin_lock_irqsave(&phba->hbalock, iflag);
6629 /* The pending mailbox command queue */
6630 list_splice_init(&phba->sli.mboxq, &completions);
6631 /* The outstanding active mailbox command */
6632 if (psli->mbox_active) {
6633 list_add_tail(&psli->mbox_active->list, &completions);
6634 psli->mbox_active = NULL;
6635 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6636 }
6637 /* The completed mailbox command queue */
6638 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6639 spin_unlock_irqrestore(&phba->hbalock, iflag);
6640
6641 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6642 while (!list_empty(&completions)) {
6643 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6644 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6645 if (pmb->mbox_cmpl)
6646 pmb->mbox_cmpl(phba, pmb);
6647 }
6648}
6649
e59058c4 6650/**
3621a710 6651 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
6652 * @vport: Pointer to virtual port object.
6653 *
6654 * lpfc_sli_host_down is called to clean up the resources
6655 * associated with a vport before destroying virtual
6656 * port data structures.
6657 * This function does following operations:
6658 * - Free discovery resources associated with this virtual
6659 * port.
6660 * - Free iocbs associated with this virtual port in
6661 * the txq.
6662 * - Send abort for all iocb commands associated with this
6663 * vport in txcmplq.
6664 *
6665 * This function is called with no lock held and always returns 1.
6666 **/
92d7f7b0
JS
6667int
6668lpfc_sli_host_down(struct lpfc_vport *vport)
6669{
858c9f6c 6670 LIST_HEAD(completions);
92d7f7b0
JS
6671 struct lpfc_hba *phba = vport->phba;
6672 struct lpfc_sli *psli = &phba->sli;
6673 struct lpfc_sli_ring *pring;
6674 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
6675 int i;
6676 unsigned long flags = 0;
6677 uint16_t prev_pring_flag;
6678
6679 lpfc_cleanup_discovery_resources(vport);
6680
6681 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
6682 for (i = 0; i < psli->num_rings; i++) {
6683 pring = &psli->ring[i];
6684 prev_pring_flag = pring->flag;
5e9d9b82
JS
6685 /* Only slow rings */
6686 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6687 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6688 /* Set the lpfc data pending flag */
6689 set_bit(LPFC_DATA_READY, &phba->data_flags);
6690 }
92d7f7b0
JS
6691 /*
6692 * Error everything on the txq since these iocbs have not been
6693 * given to the FW yet.
6694 */
92d7f7b0
JS
6695 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6696 if (iocb->vport != vport)
6697 continue;
858c9f6c 6698 list_move_tail(&iocb->list, &completions);
92d7f7b0 6699 pring->txq_cnt--;
92d7f7b0
JS
6700 }
6701
6702 /* Next issue ABTS for everything on the txcmplq */
6703 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6704 list) {
6705 if (iocb->vport != vport)
6706 continue;
6707 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6708 }
6709
6710 pring->flag = prev_pring_flag;
6711 }
6712
6713 spin_unlock_irqrestore(&phba->hbalock, flags);
6714
a257bf90
JS
6715 /* Cancel all the IOCBs from the completions list */
6716 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6717 IOERR_SLI_DOWN);
92d7f7b0
JS
6718 return 1;
6719}
6720
e59058c4 6721/**
3621a710 6722 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
6723 * @phba: Pointer to HBA context object.
6724 *
6725 * This function cleans up all iocb, buffers, mailbox commands
6726 * while shutting down the HBA. This function is called with no
6727 * lock held and always returns 1.
6728 * This function does the following to cleanup driver resources:
6729 * - Free discovery resources for each virtual port
6730 * - Cleanup any pending fabric iocbs
6731 * - Iterate through the iocb txq and free each entry
6732 * in the list.
6733 * - Free up any buffer posted to the HBA
6734 * - Free mailbox commands in the mailbox queue.
6735 **/
dea3101e 6736int
2e0fef85 6737lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 6738{
2534ba75 6739 LIST_HEAD(completions);
2e0fef85 6740 struct lpfc_sli *psli = &phba->sli;
dea3101e 6741 struct lpfc_sli_ring *pring;
0ff10d46 6742 struct lpfc_dmabuf *buf_ptr;
dea3101e 6743 unsigned long flags = 0;
04c68496
JS
6744 int i;
6745
6746 /* Shutdown the mailbox command sub-system */
6747 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 6748
dea3101e 6749 lpfc_hba_down_prep(phba);
6750
92d7f7b0
JS
6751 lpfc_fabric_abort_hba(phba);
6752
2e0fef85 6753 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 6754 for (i = 0; i < psli->num_rings; i++) {
6755 pring = &psli->ring[i];
5e9d9b82
JS
6756 /* Only slow rings */
6757 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6758 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6759 /* Set the lpfc data pending flag */
6760 set_bit(LPFC_DATA_READY, &phba->data_flags);
6761 }
dea3101e 6762
6763 /*
6764 * Error everything on the txq since these iocbs have not been
6765 * given to the FW yet.
6766 */
2534ba75 6767 list_splice_init(&pring->txq, &completions);
dea3101e 6768 pring->txq_cnt = 0;
6769
2534ba75 6770 }
2e0fef85 6771 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 6772
a257bf90
JS
6773 /* Cancel all the IOCBs from the completions list */
6774 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6775 IOERR_SLI_DOWN);
dea3101e 6776
0ff10d46
JS
6777 spin_lock_irqsave(&phba->hbalock, flags);
6778 list_splice_init(&phba->elsbuf, &completions);
6779 phba->elsbuf_cnt = 0;
6780 phba->elsbuf_prev_cnt = 0;
6781 spin_unlock_irqrestore(&phba->hbalock, flags);
6782
6783 while (!list_empty(&completions)) {
6784 list_remove_head(&completions, buf_ptr,
6785 struct lpfc_dmabuf, list);
6786 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6787 kfree(buf_ptr);
6788 }
6789
dea3101e 6790 /* Return any active mbox cmds */
6791 del_timer_sync(&psli->mbox_tmo);
2e0fef85 6792
da0436e9 6793 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 6794 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 6795 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 6796
da0436e9
JS
6797 return 1;
6798}
6799
6800/**
6801 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6802 * @phba: Pointer to HBA context object.
6803 *
6804 * This function cleans up all queues, iocb, buffers, mailbox commands while
6805 * shutting down the SLI4 HBA FCoE function. This function is called with no
6806 * lock held and always returns 1.
6807 *
6808 * This function does the following to cleanup driver FCoE function resources:
6809 * - Free discovery resources for each virtual port
6810 * - Cleanup any pending fabric iocbs
6811 * - Iterate through the iocb txq and free each entry in the list.
6812 * - Free up any buffer posted to the HBA.
6813 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6814 * - Free mailbox commands in the mailbox queue.
6815 **/
6816int
6817lpfc_sli4_hba_down(struct lpfc_hba *phba)
6818{
6819 /* Stop the SLI4 device port */
6820 lpfc_stop_port(phba);
6821
6822 /* Tear down the queues in the HBA */
6823 lpfc_sli4_queue_unset(phba);
6824
6825 /* unregister default FCFI from the HBA */
6826 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
92d7f7b0 6827
dea3101e 6828 return 1;
6829}
6830
e59058c4 6831/**
3621a710 6832 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
6833 * @srcp: Source memory pointer.
6834 * @destp: Destination memory pointer.
6835 * @cnt: Number of words required to be copied.
6836 *
6837 * This function is used for copying data between driver memory
6838 * and the SLI memory. This function also changes the endianness
6839 * of each word if native endianness is different from SLI
6840 * endianness. This function can be called with or without
6841 * lock.
6842 **/
dea3101e 6843void
6844lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6845{
6846 uint32_t *src = srcp;
6847 uint32_t *dest = destp;
6848 uint32_t ldata;
6849 int i;
6850
6851 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6852 ldata = *src;
6853 ldata = le32_to_cpu(ldata);
6854 *dest = ldata;
6855 src++;
6856 dest++;
6857 }
6858}
6859
e59058c4 6860
a0c87cbd
JS
6861/**
6862 * lpfc_sli_bemem_bcopy - SLI memory copy function
6863 * @srcp: Source memory pointer.
6864 * @destp: Destination memory pointer.
6865 * @cnt: Number of words required to be copied.
6866 *
6867 * This function is used for copying data between a data structure
6868 * with big endian representation to local endianness.
6869 * This function can be called with or without lock.
6870 **/
6871void
6872lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6873{
6874 uint32_t *src = srcp;
6875 uint32_t *dest = destp;
6876 uint32_t ldata;
6877 int i;
6878
6879 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6880 ldata = *src;
6881 ldata = be32_to_cpu(ldata);
6882 *dest = ldata;
6883 src++;
6884 dest++;
6885 }
6886}
6887
e59058c4 6888/**
3621a710 6889 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
6890 * @phba: Pointer to HBA context object.
6891 * @pring: Pointer to driver SLI ring object.
6892 * @mp: Pointer to driver buffer object.
6893 *
6894 * This function is called with no lock held.
6895 * It always return zero after adding the buffer to the postbufq
6896 * buffer list.
6897 **/
dea3101e 6898int
2e0fef85
JS
6899lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6900 struct lpfc_dmabuf *mp)
dea3101e 6901{
6902 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6903 later */
2e0fef85 6904 spin_lock_irq(&phba->hbalock);
dea3101e 6905 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 6906 pring->postbufq_cnt++;
2e0fef85 6907 spin_unlock_irq(&phba->hbalock);
dea3101e 6908 return 0;
6909}
6910
e59058c4 6911/**
3621a710 6912 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
6913 * @phba: Pointer to HBA context object.
6914 *
6915 * When HBQ is enabled, buffers are searched based on tags. This function
6916 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6917 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6918 * does not conflict with tags of buffer posted for unsolicited events.
6919 * The function returns the allocated tag. The function is called with
6920 * no locks held.
6921 **/
76bb24ef
JS
6922uint32_t
6923lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6924{
6925 spin_lock_irq(&phba->hbalock);
6926 phba->buffer_tag_count++;
6927 /*
6928 * Always set the QUE_BUFTAG_BIT to distiguish between
6929 * a tag assigned by HBQ.
6930 */
6931 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6932 spin_unlock_irq(&phba->hbalock);
6933 return phba->buffer_tag_count;
6934}
6935
e59058c4 6936/**
3621a710 6937 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
6938 * @phba: Pointer to HBA context object.
6939 * @pring: Pointer to driver SLI ring object.
6940 * @tag: Buffer tag.
6941 *
6942 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6943 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6944 * iocb is posted to the response ring with the tag of the buffer.
6945 * This function searches the pring->postbufq list using the tag
6946 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6947 * iocb. If the buffer is found then lpfc_dmabuf object of the
6948 * buffer is returned to the caller else NULL is returned.
6949 * This function is called with no lock held.
6950 **/
76bb24ef
JS
6951struct lpfc_dmabuf *
6952lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6953 uint32_t tag)
6954{
6955 struct lpfc_dmabuf *mp, *next_mp;
6956 struct list_head *slp = &pring->postbufq;
6957
6958 /* Search postbufq, from the begining, looking for a match on tag */
6959 spin_lock_irq(&phba->hbalock);
6960 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6961 if (mp->buffer_tag == tag) {
6962 list_del_init(&mp->list);
6963 pring->postbufq_cnt--;
6964 spin_unlock_irq(&phba->hbalock);
6965 return mp;
6966 }
6967 }
6968
6969 spin_unlock_irq(&phba->hbalock);
6970 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 6971 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
6972 "ring %d Data x%lx x%p x%p x%x\n",
6973 pring->ringno, (unsigned long) tag,
6974 slp->next, slp->prev, pring->postbufq_cnt);
6975
6976 return NULL;
6977}
dea3101e 6978
e59058c4 6979/**
3621a710 6980 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
6981 * @phba: Pointer to HBA context object.
6982 * @pring: Pointer to driver SLI ring object.
6983 * @phys: DMA address of the buffer.
6984 *
6985 * This function searches the buffer list using the dma_address
6986 * of unsolicited event to find the driver's lpfc_dmabuf object
6987 * corresponding to the dma_address. The function returns the
6988 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6989 * This function is called by the ct and els unsolicited event
6990 * handlers to get the buffer associated with the unsolicited
6991 * event.
6992 *
6993 * This function is called with no lock held.
6994 **/
dea3101e 6995struct lpfc_dmabuf *
6996lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6997 dma_addr_t phys)
6998{
6999 struct lpfc_dmabuf *mp, *next_mp;
7000 struct list_head *slp = &pring->postbufq;
7001
7002 /* Search postbufq, from the begining, looking for a match on phys */
2e0fef85 7003 spin_lock_irq(&phba->hbalock);
dea3101e 7004 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
7005 if (mp->phys == phys) {
7006 list_del_init(&mp->list);
7007 pring->postbufq_cnt--;
2e0fef85 7008 spin_unlock_irq(&phba->hbalock);
dea3101e 7009 return mp;
7010 }
7011 }
7012
2e0fef85 7013 spin_unlock_irq(&phba->hbalock);
dea3101e 7014 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 7015 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 7016 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 7017 pring->ringno, (unsigned long long)phys,
dea3101e 7018 slp->next, slp->prev, pring->postbufq_cnt);
7019 return NULL;
7020}
7021
e59058c4 7022/**
3621a710 7023 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
7024 * @phba: Pointer to HBA context object.
7025 * @cmdiocb: Pointer to driver command iocb object.
7026 * @rspiocb: Pointer to driver response iocb object.
7027 *
7028 * This function is the completion handler for the abort iocbs for
7029 * ELS commands. This function is called from the ELS ring event
7030 * handler with no lock held. This function frees memory resources
7031 * associated with the abort iocb.
7032 **/
dea3101e 7033static void
2e0fef85
JS
7034lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7035 struct lpfc_iocbq *rspiocb)
dea3101e 7036{
2e0fef85 7037 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 7038 uint16_t abort_iotag, abort_context;
92d7f7b0 7039 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
7040 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7041
7042 abort_iocb = NULL;
2680eeaa
JS
7043
7044 if (irsp->ulpStatus) {
7045 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7046 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7047
2e0fef85 7048 spin_lock_irq(&phba->hbalock);
2680eeaa
JS
7049 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
7050 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
7051
92d7f7b0 7052 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
e8b62011 7053 "0327 Cannot abort els iocb %p "
92d7f7b0
JS
7054 "with tag %x context %x, abort status %x, "
7055 "abort code %x\n",
e8b62011
JS
7056 abort_iocb, abort_iotag, abort_context,
7057 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa 7058
58da1ffb
JS
7059 /*
7060 * If the iocb is not found in Firmware queue the iocb
7061 * might have completed already. Do not free it again.
7062 */
9b379605 7063 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
58da1ffb
JS
7064 spin_unlock_irq(&phba->hbalock);
7065 lpfc_sli_release_iocbq(phba, cmdiocb);
7066 return;
7067 }
2680eeaa
JS
7068 /*
7069 * make sure we have the right iocbq before taking it
7070 * off the txcmplq and try to call completion routine.
7071 */
2e0fef85
JS
7072 if (!abort_iocb ||
7073 abort_iocb->iocb.ulpContext != abort_context ||
7074 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7075 spin_unlock_irq(&phba->hbalock);
7076 else {
92d7f7b0 7077 list_del_init(&abort_iocb->list);
2680eeaa 7078 pring->txcmplq_cnt--;
2e0fef85 7079 spin_unlock_irq(&phba->hbalock);
2680eeaa 7080
0ff10d46
JS
7081 /* Firmware could still be in progress of DMAing
7082 * payload, so don't free data buffer till after
7083 * a hbeat.
7084 */
7085 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7086
92d7f7b0
JS
7087 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7088 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7089 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
7090 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
2680eeaa
JS
7091 }
7092 }
7093
604a3e30 7094 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 7095 return;
7096}
7097
e59058c4 7098/**
3621a710 7099 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
7100 * @phba: Pointer to HBA context object.
7101 * @cmdiocb: Pointer to driver command iocb object.
7102 * @rspiocb: Pointer to driver response iocb object.
7103 *
7104 * The function is called from SLI ring event handler with no
7105 * lock held. This function is the completion handler for ELS commands
7106 * which are aborted. The function frees memory resources used for
7107 * the aborted ELS commands.
7108 **/
92d7f7b0
JS
7109static void
7110lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7111 struct lpfc_iocbq *rspiocb)
7112{
7113 IOCB_t *irsp = &rspiocb->iocb;
7114
7115 /* ELS cmd tag <ulpIoTag> completes */
7116 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 7117 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 7118 "x%x x%x x%x\n",
e8b62011 7119 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 7120 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
7121 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7122 lpfc_ct_free_iocb(phba, cmdiocb);
7123 else
7124 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
7125 return;
7126}
7127
e59058c4 7128/**
3621a710 7129 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
e59058c4
JS
7130 * @phba: Pointer to HBA context object.
7131 * @pring: Pointer to driver SLI ring object.
7132 * @cmdiocb: Pointer to driver command iocb object.
7133 *
7134 * This function issues an abort iocb for the provided command
7135 * iocb. This function is called with hbalock held.
7136 * The function returns 0 when it fails due to memory allocation
7137 * failure or when the command iocb is an abort request.
7138 **/
dea3101e 7139int
2e0fef85
JS
7140lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7141 struct lpfc_iocbq *cmdiocb)
dea3101e 7142{
2e0fef85 7143 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 7144 struct lpfc_iocbq *abtsiocbp;
dea3101e 7145 IOCB_t *icmd = NULL;
7146 IOCB_t *iabt = NULL;
07951076
JS
7147 int retval = IOCB_ERROR;
7148
92d7f7b0
JS
7149 /*
7150 * There are certain command types we don't want to abort. And we
7151 * don't want to abort commands that are already in the process of
7152 * being aborted.
07951076
JS
7153 */
7154 icmd = &cmdiocb->iocb;
2e0fef85 7155 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
7156 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7157 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
7158 return 0;
7159
858c9f6c
JS
7160 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7161 * callback so that nothing happens when it finishes.
07951076 7162 */
858c9f6c
JS
7163 if ((vport->load_flag & FC_UNLOADING) &&
7164 (pring->ringno == LPFC_ELS_RING)) {
92d7f7b0
JS
7165 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7166 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7167 else
7168 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
07951076 7169 goto abort_iotag_exit;
92d7f7b0 7170 }
dea3101e 7171
7172 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 7173 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 7174 if (abtsiocbp == NULL)
7175 return 0;
dea3101e 7176
07951076
JS
7177 /* This signals the response to set the correct status
7178 * before calling the completion handler.
7179 */
7180 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7181
dea3101e 7182 iabt = &abtsiocbp->iocb;
07951076
JS
7183 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7184 iabt->un.acxri.abortContextTag = icmd->ulpContext;
da0436e9
JS
7185 if (phba->sli_rev == LPFC_SLI_REV4)
7186 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7187 else
7188 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
7189 iabt->ulpLe = 1;
7190 iabt->ulpClass = icmd->ulpClass;
dea3101e 7191
2e0fef85 7192 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
7193 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7194 else
7195 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 7196
07951076 7197 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 7198
e8b62011
JS
7199 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7200 "0339 Abort xri x%x, original iotag x%x, "
7201 "abort cmd iotag x%x\n",
7202 iabt->un.acxri.abortContextTag,
7203 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
da0436e9 7204 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 7205
d7c255b2
JS
7206 if (retval)
7207 __lpfc_sli_release_iocbq(phba, abtsiocbp);
07951076 7208abort_iotag_exit:
2e0fef85
JS
7209 /*
7210 * Caller to this routine should check for IOCB_ERROR
7211 * and handle it properly. This routine no longer removes
7212 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 7213 */
2e0fef85 7214 return retval;
dea3101e 7215}
7216
e59058c4 7217/**
3621a710 7218 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
7219 * @iocbq: Pointer to driver iocb object.
7220 * @vport: Pointer to driver virtual port object.
7221 * @tgt_id: SCSI ID of the target.
7222 * @lun_id: LUN ID of the scsi device.
7223 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7224 *
3621a710 7225 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
7226 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7227 * 0 if the filtering criteria is met for the given iocb and will return
7228 * 1 if the filtering criteria is not met.
7229 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7230 * given iocb is for the SCSI device specified by vport, tgt_id and
7231 * lun_id parameter.
7232 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7233 * given iocb is for the SCSI target specified by vport and tgt_id
7234 * parameters.
7235 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7236 * given iocb is for the SCSI host associated with the given vport.
7237 * This function is called with no locks held.
7238 **/
dea3101e 7239static int
51ef4c26
JS
7240lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7241 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 7242 lpfc_ctx_cmd ctx_cmd)
dea3101e 7243{
0bd4ca25 7244 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7245 int rc = 1;
7246
0bd4ca25
JSEC
7247 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7248 return rc;
7249
51ef4c26
JS
7250 if (iocbq->vport != vport)
7251 return rc;
7252
0bd4ca25 7253 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 7254
495a714c 7255 if (lpfc_cmd->pCmd == NULL)
dea3101e 7256 return rc;
7257
7258 switch (ctx_cmd) {
7259 case LPFC_CTX_LUN:
495a714c
JS
7260 if ((lpfc_cmd->rdata->pnode) &&
7261 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7262 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 7263 rc = 0;
7264 break;
7265 case LPFC_CTX_TGT:
495a714c
JS
7266 if ((lpfc_cmd->rdata->pnode) &&
7267 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 7268 rc = 0;
7269 break;
dea3101e 7270 case LPFC_CTX_HOST:
7271 rc = 0;
7272 break;
7273 default:
7274 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 7275 __func__, ctx_cmd);
dea3101e 7276 break;
7277 }
7278
7279 return rc;
7280}
7281
e59058c4 7282/**
3621a710 7283 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
7284 * @vport: Pointer to virtual port.
7285 * @tgt_id: SCSI ID of the target.
7286 * @lun_id: LUN ID of the scsi device.
7287 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7288 *
7289 * This function returns number of FCP commands pending for the vport.
7290 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7291 * commands pending on the vport associated with SCSI device specified
7292 * by tgt_id and lun_id parameters.
7293 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7294 * commands pending on the vport associated with SCSI target specified
7295 * by tgt_id parameter.
7296 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7297 * commands pending on the vport.
7298 * This function returns the number of iocbs which satisfy the filter.
7299 * This function is called without any lock held.
7300 **/
dea3101e 7301int
51ef4c26
JS
7302lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7303 lpfc_ctx_cmd ctx_cmd)
dea3101e 7304{
51ef4c26 7305 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7306 struct lpfc_iocbq *iocbq;
7307 int sum, i;
dea3101e 7308
0bd4ca25
JSEC
7309 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7310 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7311
51ef4c26
JS
7312 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7313 ctx_cmd) == 0)
0bd4ca25 7314 sum++;
dea3101e 7315 }
0bd4ca25 7316
dea3101e 7317 return sum;
7318}
7319
e59058c4 7320/**
3621a710 7321 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
7322 * @phba: Pointer to HBA context object
7323 * @cmdiocb: Pointer to command iocb object.
7324 * @rspiocb: Pointer to response iocb object.
7325 *
7326 * This function is called when an aborted FCP iocb completes. This
7327 * function is called by the ring event handler with no lock held.
7328 * This function frees the iocb.
7329 **/
5eb95af0 7330void
2e0fef85
JS
7331lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7332 struct lpfc_iocbq *rspiocb)
5eb95af0 7333{
604a3e30 7334 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
7335 return;
7336}
7337
e59058c4 7338/**
3621a710 7339 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
7340 * @vport: Pointer to virtual port.
7341 * @pring: Pointer to driver SLI ring object.
7342 * @tgt_id: SCSI ID of the target.
7343 * @lun_id: LUN ID of the scsi device.
7344 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7345 *
7346 * This function sends an abort command for every SCSI command
7347 * associated with the given virtual port pending on the ring
7348 * filtered by lpfc_sli_validate_fcp_iocb function.
7349 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7350 * FCP iocbs associated with lun specified by tgt_id and lun_id
7351 * parameters
7352 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7353 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7354 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7355 * FCP iocbs associated with virtual port.
7356 * This function returns number of iocbs it failed to abort.
7357 * This function is called with no locks held.
7358 **/
dea3101e 7359int
51ef4c26
JS
7360lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7361 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 7362{
51ef4c26 7363 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7364 struct lpfc_iocbq *iocbq;
7365 struct lpfc_iocbq *abtsiocb;
dea3101e 7366 IOCB_t *cmd = NULL;
dea3101e 7367 int errcnt = 0, ret_val = 0;
0bd4ca25 7368 int i;
dea3101e 7369
0bd4ca25
JSEC
7370 for (i = 1; i <= phba->sli.last_iotag; i++) {
7371 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7372
51ef4c26 7373 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 7374 abort_cmd) != 0)
dea3101e 7375 continue;
7376
7377 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 7378 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 7379 if (abtsiocb == NULL) {
7380 errcnt++;
7381 continue;
7382 }
dea3101e 7383
0bd4ca25 7384 cmd = &iocbq->iocb;
dea3101e 7385 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7386 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
7387 if (phba->sli_rev == LPFC_SLI_REV4)
7388 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7389 else
7390 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 7391 abtsiocb->iocb.ulpLe = 1;
7392 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 7393 abtsiocb->vport = phba->pport;
dea3101e 7394
2e0fef85 7395 if (lpfc_is_link_up(phba))
dea3101e 7396 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7397 else
7398 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7399
5eb95af0
JSEC
7400 /* Setup callback routine and issue the command. */
7401 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
7402 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7403 abtsiocb, 0);
dea3101e 7404 if (ret_val == IOCB_ERROR) {
604a3e30 7405 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 7406 errcnt++;
7407 continue;
7408 }
7409 }
7410
7411 return errcnt;
7412}
7413
e59058c4 7414/**
3621a710 7415 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
7416 * @phba: Pointer to HBA context object.
7417 * @cmdiocbq: Pointer to command iocb.
7418 * @rspiocbq: Pointer to response iocb.
7419 *
7420 * This function is the completion handler for iocbs issued using
7421 * lpfc_sli_issue_iocb_wait function. This function is called by the
7422 * ring event handler function without any lock held. This function
7423 * can be called from both worker thread context and interrupt
7424 * context. This function also can be called from other thread which
7425 * cleans up the SLI layer objects.
7426 * This function copy the contents of the response iocb to the
7427 * response iocb memory object provided by the caller of
7428 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7429 * sleeps for the iocb completion.
7430 **/
68876920
JSEC
7431static void
7432lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7433 struct lpfc_iocbq *cmdiocbq,
7434 struct lpfc_iocbq *rspiocbq)
dea3101e 7435{
68876920
JSEC
7436 wait_queue_head_t *pdone_q;
7437 unsigned long iflags;
dea3101e 7438
2e0fef85 7439 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
7440 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7441 if (cmdiocbq->context2 && rspiocbq)
7442 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7443 &rspiocbq->iocb, sizeof(IOCB_t));
7444
7445 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
7446 if (pdone_q)
7447 wake_up(pdone_q);
858c9f6c 7448 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 7449 return;
7450}
7451
d11e31dd
JS
7452/**
7453 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7454 * @phba: Pointer to HBA context object..
7455 * @piocbq: Pointer to command iocb.
7456 * @flag: Flag to test.
7457 *
7458 * This routine grabs the hbalock and then test the iocb_flag to
7459 * see if the passed in flag is set.
7460 * Returns:
7461 * 1 if flag is set.
7462 * 0 if flag is not set.
7463 **/
7464static int
7465lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7466 struct lpfc_iocbq *piocbq, uint32_t flag)
7467{
7468 unsigned long iflags;
7469 int ret;
7470
7471 spin_lock_irqsave(&phba->hbalock, iflags);
7472 ret = piocbq->iocb_flag & flag;
7473 spin_unlock_irqrestore(&phba->hbalock, iflags);
7474 return ret;
7475
7476}
7477
e59058c4 7478/**
3621a710 7479 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
7480 * @phba: Pointer to HBA context object..
7481 * @pring: Pointer to sli ring.
7482 * @piocb: Pointer to command iocb.
7483 * @prspiocbq: Pointer to response iocb.
7484 * @timeout: Timeout in number of seconds.
7485 *
7486 * This function issues the iocb to firmware and waits for the
7487 * iocb to complete. If the iocb command is not
7488 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7489 * Caller should not free the iocb resources if this function
7490 * returns IOCB_TIMEDOUT.
7491 * The function waits for the iocb completion using an
7492 * non-interruptible wait.
7493 * This function will sleep while waiting for iocb completion.
7494 * So, this function should not be called from any context which
7495 * does not allow sleeping. Due to the same reason, this function
7496 * cannot be called with interrupt disabled.
7497 * This function assumes that the iocb completions occur while
7498 * this function sleep. So, this function cannot be called from
7499 * the thread which process iocb completion for this ring.
7500 * This function clears the iocb_flag of the iocb object before
7501 * issuing the iocb and the iocb completion handler sets this
7502 * flag and wakes this thread when the iocb completes.
7503 * The contents of the response iocb will be copied to prspiocbq
7504 * by the completion handler when the command completes.
7505 * This function returns IOCB_SUCCESS when success.
7506 * This function is called with no lock held.
7507 **/
dea3101e 7508int
2e0fef85 7509lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 7510 uint32_t ring_number,
2e0fef85
JS
7511 struct lpfc_iocbq *piocb,
7512 struct lpfc_iocbq *prspiocbq,
68876920 7513 uint32_t timeout)
dea3101e 7514{
7259f0d0 7515 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
7516 long timeleft, timeout_req = 0;
7517 int retval = IOCB_SUCCESS;
875fbdfe 7518 uint32_t creg_val;
dea3101e 7519
7520 /*
68876920
JSEC
7521 * If the caller has provided a response iocbq buffer, then context2
7522 * is NULL or its an error.
dea3101e 7523 */
68876920
JSEC
7524 if (prspiocbq) {
7525 if (piocb->context2)
7526 return IOCB_ERROR;
7527 piocb->context2 = prspiocbq;
dea3101e 7528 }
7529
68876920
JSEC
7530 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7531 piocb->context_un.wait_queue = &done_q;
7532 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 7533
875fbdfe
JSEC
7534 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7535 creg_val = readl(phba->HCregaddr);
7536 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7537 writel(creg_val, phba->HCregaddr);
7538 readl(phba->HCregaddr); /* flush */
7539 }
7540
da0436e9 7541 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
68876920
JSEC
7542 if (retval == IOCB_SUCCESS) {
7543 timeout_req = timeout * HZ;
68876920 7544 timeleft = wait_event_timeout(done_q,
d11e31dd 7545 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 7546 timeout_req);
dea3101e 7547
7054a606
JS
7548 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7549 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 7550 "0331 IOCB wake signaled\n");
7054a606 7551 } else if (timeleft == 0) {
68876920 7552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7553 "0338 IOCB wait timeout error - no "
7554 "wake response Data x%x\n", timeout);
68876920 7555 retval = IOCB_TIMEDOUT;
7054a606 7556 } else {
68876920 7557 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7558 "0330 IOCB wake NOT set, "
7559 "Data x%x x%lx\n",
68876920
JSEC
7560 timeout, (timeleft / jiffies));
7561 retval = IOCB_TIMEDOUT;
dea3101e 7562 }
68876920
JSEC
7563 } else {
7564 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 7565 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 7566 retval);
68876920 7567 retval = IOCB_ERROR;
dea3101e 7568 }
7569
875fbdfe
JSEC
7570 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7571 creg_val = readl(phba->HCregaddr);
7572 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7573 writel(creg_val, phba->HCregaddr);
7574 readl(phba->HCregaddr); /* flush */
7575 }
7576
68876920
JSEC
7577 if (prspiocbq)
7578 piocb->context2 = NULL;
7579
7580 piocb->context_un.wait_queue = NULL;
7581 piocb->iocb_cmpl = NULL;
dea3101e 7582 return retval;
7583}
68876920 7584
e59058c4 7585/**
3621a710 7586 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
7587 * @phba: Pointer to HBA context object.
7588 * @pmboxq: Pointer to driver mailbox object.
7589 * @timeout: Timeout in number of seconds.
7590 *
7591 * This function issues the mailbox to firmware and waits for the
7592 * mailbox command to complete. If the mailbox command is not
7593 * completed within timeout seconds, it returns MBX_TIMEOUT.
7594 * The function waits for the mailbox completion using an
7595 * interruptible wait. If the thread is woken up due to a
7596 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7597 * should not free the mailbox resources, if this function returns
7598 * MBX_TIMEOUT.
7599 * This function will sleep while waiting for mailbox completion.
7600 * So, this function should not be called from any context which
7601 * does not allow sleeping. Due to the same reason, this function
7602 * cannot be called with interrupt disabled.
7603 * This function assumes that the mailbox completion occurs while
7604 * this function sleep. So, this function cannot be called from
7605 * the worker thread which processes mailbox completion.
7606 * This function is called in the context of HBA management
7607 * applications.
7608 * This function returns MBX_SUCCESS when successful.
7609 * This function is called with no lock held.
7610 **/
dea3101e 7611int
2e0fef85 7612lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 7613 uint32_t timeout)
7614{
7259f0d0 7615 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 7616 int retval;
858c9f6c 7617 unsigned long flag;
dea3101e 7618
7619 /* The caller must leave context1 empty. */
98c9ea5c 7620 if (pmboxq->context1)
2e0fef85 7621 return MBX_NOT_FINISHED;
dea3101e 7622
495a714c 7623 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 7624 /* setup wake call as IOCB callback */
7625 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7626 /* setup context field to pass wait_queue pointer to wake function */
7627 pmboxq->context1 = &done_q;
7628
dea3101e 7629 /* now issue the command */
7630 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7631
7632 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
7633 wait_event_interruptible_timeout(done_q,
7634 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7635 timeout * HZ);
7636
858c9f6c 7637 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 7638 pmboxq->context1 = NULL;
7054a606
JS
7639 /*
7640 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7641 * else do not free the resources.
7642 */
7643 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea3101e 7644 retval = MBX_SUCCESS;
858c9f6c 7645 else {
7054a606 7646 retval = MBX_TIMEOUT;
858c9f6c
JS
7647 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7648 }
7649 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 7650 }
7651
dea3101e 7652 return retval;
7653}
7654
e59058c4 7655/**
3772a991 7656 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
7657 * @phba: Pointer to HBA context.
7658 *
3772a991
JS
7659 * This function is called to shutdown the driver's mailbox sub-system.
7660 * It first marks the mailbox sub-system is in a block state to prevent
7661 * the asynchronous mailbox command from issued off the pending mailbox
7662 * command queue. If the mailbox command sub-system shutdown is due to
7663 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7664 * the mailbox sub-system flush routine to forcefully bring down the
7665 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7666 * as with offline or HBA function reset), this routine will wait for the
7667 * outstanding mailbox command to complete before invoking the mailbox
7668 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 7669 **/
3772a991
JS
7670void
7671lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 7672{
3772a991
JS
7673 struct lpfc_sli *psli = &phba->sli;
7674 uint8_t actcmd = MBX_HEARTBEAT;
7675 unsigned long timeout;
b4c02652 7676
3772a991
JS
7677 spin_lock_irq(&phba->hbalock);
7678 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7679 spin_unlock_irq(&phba->hbalock);
b4c02652 7680
3772a991 7681 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 7682 spin_lock_irq(&phba->hbalock);
3772a991
JS
7683 if (phba->sli.mbox_active)
7684 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
ed957684 7685 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7686 /* Determine how long we might wait for the active mailbox
7687 * command to be gracefully completed by firmware.
7688 */
7689 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7690 1000) + jiffies;
7691 while (phba->sli.mbox_active) {
7692 /* Check active mailbox complete status every 2ms */
7693 msleep(2);
7694 if (time_after(jiffies, timeout))
7695 /* Timeout, let the mailbox flush routine to
7696 * forcefully release active mailbox command
7697 */
7698 break;
7699 }
7700 }
7701 lpfc_sli_mbox_sys_flush(phba);
7702}
ed957684 7703
3772a991
JS
7704/**
7705 * lpfc_sli_eratt_read - read sli-3 error attention events
7706 * @phba: Pointer to HBA context.
7707 *
7708 * This function is called to read the SLI3 device error attention registers
7709 * for possible error attention events. The caller must hold the hostlock
7710 * with spin_lock_irq().
7711 *
7712 * This fucntion returns 1 when there is Error Attention in the Host Attention
7713 * Register and returns 0 otherwise.
7714 **/
7715static int
7716lpfc_sli_eratt_read(struct lpfc_hba *phba)
7717{
7718 uint32_t ha_copy;
b4c02652 7719
3772a991
JS
7720 /* Read chip Host Attention (HA) register */
7721 ha_copy = readl(phba->HAregaddr);
7722 if (ha_copy & HA_ERATT) {
7723 /* Read host status register to retrieve error event */
7724 lpfc_sli_read_hs(phba);
b4c02652 7725
3772a991
JS
7726 /* Check if there is a deferred error condition is active */
7727 if ((HS_FFER1 & phba->work_hs) &&
7728 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7729 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
3772a991 7730 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
7731 /* Clear all interrupt enable conditions */
7732 writel(0, phba->HCregaddr);
7733 readl(phba->HCregaddr);
7734 }
7735
7736 /* Set the driver HA work bitmap */
3772a991
JS
7737 phba->work_ha |= HA_ERATT;
7738 /* Indicate polling handles this ERATT */
7739 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
7740 return 1;
7741 }
7742 return 0;
b4c02652
JS
7743}
7744
da0436e9
JS
7745/**
7746 * lpfc_sli4_eratt_read - read sli-4 error attention events
7747 * @phba: Pointer to HBA context.
7748 *
7749 * This function is called to read the SLI4 device error attention registers
7750 * for possible error attention events. The caller must hold the hostlock
7751 * with spin_lock_irq().
7752 *
7753 * This fucntion returns 1 when there is Error Attention in the Host Attention
7754 * Register and returns 0 otherwise.
7755 **/
7756static int
7757lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7758{
7759 uint32_t uerr_sta_hi, uerr_sta_lo;
7760 uint32_t onlnreg0, onlnreg1;
7761
7762 /* For now, use the SLI4 device internal unrecoverable error
7763 * registers for error attention. This can be changed later.
7764 */
7765 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7766 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7767 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7768 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7769 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7770 if (uerr_sta_lo || uerr_sta_hi) {
7771 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7772 "1423 HBA Unrecoverable error: "
7773 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7774 "online0_reg=0x%x, online1_reg=0x%x\n",
7775 uerr_sta_lo, uerr_sta_hi,
7776 onlnreg0, onlnreg1);
da0436e9
JS
7777 phba->work_status[0] = uerr_sta_lo;
7778 phba->work_status[1] = uerr_sta_hi;
da0436e9
JS
7779 /* Set the driver HA work bitmap */
7780 phba->work_ha |= HA_ERATT;
7781 /* Indicate polling handles this ERATT */
7782 phba->hba_flag |= HBA_ERATT_HANDLED;
da0436e9
JS
7783 return 1;
7784 }
7785 }
7786 return 0;
7787}
7788
e59058c4 7789/**
3621a710 7790 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
7791 * @phba: Pointer to HBA context.
7792 *
3772a991 7793 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
7794 * error attention register bit for error attention events.
7795 *
7796 * This fucntion returns 1 when there is Error Attention in the Host Attention
7797 * Register and returns 0 otherwise.
7798 **/
7799int
7800lpfc_sli_check_eratt(struct lpfc_hba *phba)
7801{
7802 uint32_t ha_copy;
7803
7804 /* If somebody is waiting to handle an eratt, don't process it
7805 * here. The brdkill function will do this.
7806 */
7807 if (phba->link_flag & LS_IGNORE_ERATT)
7808 return 0;
7809
7810 /* Check if interrupt handler handles this ERATT */
7811 spin_lock_irq(&phba->hbalock);
7812 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7813 /* Interrupt handler has handled ERATT */
7814 spin_unlock_irq(&phba->hbalock);
7815 return 0;
7816 }
7817
a257bf90
JS
7818 /*
7819 * If there is deferred error attention, do not check for error
7820 * attention
7821 */
7822 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7823 spin_unlock_irq(&phba->hbalock);
7824 return 0;
7825 }
7826
3772a991
JS
7827 /* If PCI channel is offline, don't process it */
7828 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 7829 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7830 return 0;
7831 }
7832
7833 switch (phba->sli_rev) {
7834 case LPFC_SLI_REV2:
7835 case LPFC_SLI_REV3:
7836 /* Read chip Host Attention (HA) register */
7837 ha_copy = lpfc_sli_eratt_read(phba);
7838 break;
da0436e9
JS
7839 case LPFC_SLI_REV4:
7840 /* Read devcie Uncoverable Error (UERR) registers */
7841 ha_copy = lpfc_sli4_eratt_read(phba);
7842 break;
3772a991
JS
7843 default:
7844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7845 "0299 Invalid SLI revision (%d)\n",
7846 phba->sli_rev);
7847 ha_copy = 0;
7848 break;
9399627f
JS
7849 }
7850 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7851
7852 return ha_copy;
7853}
7854
7855/**
7856 * lpfc_intr_state_check - Check device state for interrupt handling
7857 * @phba: Pointer to HBA context.
7858 *
7859 * This inline routine checks whether a device or its PCI slot is in a state
7860 * that the interrupt should be handled.
7861 *
7862 * This function returns 0 if the device or the PCI slot is in a state that
7863 * interrupt should be handled, otherwise -EIO.
7864 */
7865static inline int
7866lpfc_intr_state_check(struct lpfc_hba *phba)
7867{
7868 /* If the pci channel is offline, ignore all the interrupts */
7869 if (unlikely(pci_channel_offline(phba->pcidev)))
7870 return -EIO;
7871
7872 /* Update device level interrupt statistics */
7873 phba->sli.slistat.sli_intr++;
7874
7875 /* Ignore all interrupts during initialization. */
7876 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7877 return -EIO;
7878
9399627f
JS
7879 return 0;
7880}
7881
7882/**
3772a991 7883 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
7884 * @irq: Interrupt number.
7885 * @dev_id: The device context pointer.
7886 *
9399627f 7887 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
7888 * service routine when device with SLI-3 interface spec is enabled with
7889 * MSI-X multi-message interrupt mode and there are slow-path events in
7890 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7891 * interrupt mode, this function is called as part of the device-level
7892 * interrupt handler. When the PCI slot is in error recovery or the HBA
7893 * is undergoing initialization, the interrupt handler will not process
7894 * the interrupt. The link attention and ELS ring attention events are
7895 * handled by the worker thread. The interrupt handler signals the worker
7896 * thread and returns for these events. This function is called without
7897 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
7898 * structures.
7899 *
7900 * This function returns IRQ_HANDLED when interrupt is handled else it
7901 * returns IRQ_NONE.
e59058c4 7902 **/
dea3101e 7903irqreturn_t
3772a991 7904lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 7905{
2e0fef85 7906 struct lpfc_hba *phba;
dea3101e 7907 uint32_t ha_copy;
7908 uint32_t work_ha_copy;
7909 unsigned long status;
5b75da2f 7910 unsigned long iflag;
dea3101e 7911 uint32_t control;
7912
92d7f7b0 7913 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
7914 struct lpfc_vport *vport;
7915 struct lpfc_nodelist *ndlp;
7916 struct lpfc_dmabuf *mp;
92d7f7b0
JS
7917 LPFC_MBOXQ_t *pmb;
7918 int rc;
7919
dea3101e 7920 /*
7921 * Get the driver's phba structure from the dev_id and
7922 * assume the HBA is not interrupting.
7923 */
9399627f 7924 phba = (struct lpfc_hba *)dev_id;
dea3101e 7925
7926 if (unlikely(!phba))
7927 return IRQ_NONE;
7928
dea3101e 7929 /*
9399627f
JS
7930 * Stuff needs to be attented to when this function is invoked as an
7931 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 7932 */
9399627f 7933 if (phba->intr_type == MSIX) {
3772a991
JS
7934 /* Check device state for handling interrupt */
7935 if (lpfc_intr_state_check(phba))
9399627f
JS
7936 return IRQ_NONE;
7937 /* Need to read HA REG for slow-path events */
5b75da2f 7938 spin_lock_irqsave(&phba->hbalock, iflag);
34b02dcd 7939 ha_copy = readl(phba->HAregaddr);
9399627f
JS
7940 /* If somebody is waiting to handle an eratt don't process it
7941 * here. The brdkill function will do this.
7942 */
7943 if (phba->link_flag & LS_IGNORE_ERATT)
7944 ha_copy &= ~HA_ERATT;
7945 /* Check the need for handling ERATT in interrupt handler */
7946 if (ha_copy & HA_ERATT) {
7947 if (phba->hba_flag & HBA_ERATT_HANDLED)
7948 /* ERATT polling has handled ERATT */
7949 ha_copy &= ~HA_ERATT;
7950 else
7951 /* Indicate interrupt handler handles ERATT */
7952 phba->hba_flag |= HBA_ERATT_HANDLED;
7953 }
a257bf90
JS
7954
7955 /*
7956 * If there is deferred error attention, do not check for any
7957 * interrupt.
7958 */
7959 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 7960 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
7961 return IRQ_NONE;
7962 }
7963
9399627f
JS
7964 /* Clear up only attention source related to slow-path */
7965 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7966 phba->HAregaddr);
7967 readl(phba->HAregaddr); /* flush */
5b75da2f 7968 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
7969 } else
7970 ha_copy = phba->ha_copy;
dea3101e 7971
dea3101e 7972 work_ha_copy = ha_copy & phba->work_ha_mask;
7973
9399627f 7974 if (work_ha_copy) {
dea3101e 7975 if (work_ha_copy & HA_LATT) {
7976 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7977 /*
7978 * Turn off Link Attention interrupts
7979 * until CLEAR_LA done
7980 */
5b75da2f 7981 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 7982 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7983 control = readl(phba->HCregaddr);
7984 control &= ~HC_LAINT_ENA;
7985 writel(control, phba->HCregaddr);
7986 readl(phba->HCregaddr); /* flush */
5b75da2f 7987 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 7988 }
7989 else
7990 work_ha_copy &= ~HA_LATT;
7991 }
7992
9399627f 7993 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
7994 /*
7995 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7996 * the only slow ring.
7997 */
7998 status = (work_ha_copy &
7999 (HA_RXMASK << (4*LPFC_ELS_RING)));
8000 status >>= (4*LPFC_ELS_RING);
8001 if (status & HA_RXMASK) {
5b75da2f 8002 spin_lock_irqsave(&phba->hbalock, iflag);
858c9f6c 8003 control = readl(phba->HCregaddr);
a58cbd52
JS
8004
8005 lpfc_debugfs_slow_ring_trc(phba,
8006 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
8007 control, status,
8008 (uint32_t)phba->sli.slistat.sli_intr);
8009
858c9f6c 8010 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
8011 lpfc_debugfs_slow_ring_trc(phba,
8012 "ISR Disable ring:"
8013 "pwork:x%x hawork:x%x wait:x%x",
8014 phba->work_ha, work_ha_copy,
8015 (uint32_t)((unsigned long)
5e9d9b82 8016 &phba->work_waitq));
a58cbd52 8017
858c9f6c
JS
8018 control &=
8019 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 8020 writel(control, phba->HCregaddr);
8021 readl(phba->HCregaddr); /* flush */
dea3101e 8022 }
a58cbd52
JS
8023 else {
8024 lpfc_debugfs_slow_ring_trc(phba,
8025 "ISR slow ring: pwork:"
8026 "x%x hawork:x%x wait:x%x",
8027 phba->work_ha, work_ha_copy,
8028 (uint32_t)((unsigned long)
5e9d9b82 8029 &phba->work_waitq));
a58cbd52 8030 }
5b75da2f 8031 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 8032 }
8033 }
5b75da2f 8034 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 8035 if (work_ha_copy & HA_ERATT) {
9399627f 8036 lpfc_sli_read_hs(phba);
a257bf90
JS
8037 /*
8038 * Check if there is a deferred error condition
8039 * is active
8040 */
8041 if ((HS_FFER1 & phba->work_hs) &&
8042 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8043 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8044 phba->hba_flag |= DEFER_ERATT;
8045 /* Clear all interrupt enable conditions */
8046 writel(0, phba->HCregaddr);
8047 readl(phba->HCregaddr);
8048 }
8049 }
8050
9399627f 8051 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 8052 pmb = phba->sli.mbox_active;
04c68496 8053 pmbox = &pmb->u.mb;
34b02dcd 8054 mbox = phba->mbox;
858c9f6c 8055 vport = pmb->vport;
92d7f7b0
JS
8056
8057 /* First check out the status word */
8058 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8059 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 8060 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
8061 /*
8062 * Stray Mailbox Interrupt, mbxCommand <cmd>
8063 * mbxStatus <status>
8064 */
09372820 8065 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 8066 LOG_SLI,
e8b62011 8067 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
8068 "Interrupt mbxCommand x%x "
8069 "mbxStatus x%x\n",
e8b62011 8070 (vport ? vport->vpi : 0),
92d7f7b0
JS
8071 pmbox->mbxCommand,
8072 pmbox->mbxStatus);
09372820
JS
8073 /* clear mailbox attention bit */
8074 work_ha_copy &= ~HA_MBATT;
8075 } else {
97eab634 8076 phba->sli.mbox_active = NULL;
5b75da2f 8077 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
8078 phba->last_completion_time = jiffies;
8079 del_timer(&phba->sli.mbox_tmo);
09372820
JS
8080 if (pmb->mbox_cmpl) {
8081 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8082 MAILBOX_CMD_SIZE);
8083 }
8084 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8085 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8086
8087 lpfc_debugfs_disc_trc(vport,
8088 LPFC_DISC_TRC_MBOX_VPORT,
8089 "MBOX dflt rpi: : "
8090 "status:x%x rpi:x%x",
8091 (uint32_t)pmbox->mbxStatus,
8092 pmbox->un.varWords[0], 0);
8093
8094 if (!pmbox->mbxStatus) {
8095 mp = (struct lpfc_dmabuf *)
8096 (pmb->context1);
8097 ndlp = (struct lpfc_nodelist *)
8098 pmb->context2;
8099
8100 /* Reg_LOGIN of dflt RPI was
8101 * successful. new lets get
8102 * rid of the RPI using the
8103 * same mbox buffer.
8104 */
8105 lpfc_unreg_login(phba,
8106 vport->vpi,
8107 pmbox->un.varWords[0],
8108 pmb);
8109 pmb->mbox_cmpl =
8110 lpfc_mbx_cmpl_dflt_rpi;
8111 pmb->context1 = mp;
8112 pmb->context2 = ndlp;
8113 pmb->vport = vport;
58da1ffb
JS
8114 rc = lpfc_sli_issue_mbox(phba,
8115 pmb,
8116 MBX_NOWAIT);
8117 if (rc != MBX_BUSY)
8118 lpfc_printf_log(phba,
8119 KERN_ERR,
8120 LOG_MBOX | LOG_SLI,
d7c255b2 8121 "0350 rc should have"
6a9c52cf 8122 "been MBX_BUSY\n");
3772a991
JS
8123 if (rc != MBX_NOT_FINISHED)
8124 goto send_current_mbox;
09372820 8125 }
858c9f6c 8126 }
5b75da2f
JS
8127 spin_lock_irqsave(
8128 &phba->pport->work_port_lock,
8129 iflag);
09372820
JS
8130 phba->pport->work_port_events &=
8131 ~WORKER_MBOX_TMO;
5b75da2f
JS
8132 spin_unlock_irqrestore(
8133 &phba->pport->work_port_lock,
8134 iflag);
09372820 8135 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 8136 }
97eab634 8137 } else
5b75da2f 8138 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 8139
92d7f7b0
JS
8140 if ((work_ha_copy & HA_MBATT) &&
8141 (phba->sli.mbox_active == NULL)) {
858c9f6c 8142send_current_mbox:
92d7f7b0 8143 /* Process next mailbox command if there is one */
58da1ffb
JS
8144 do {
8145 rc = lpfc_sli_issue_mbox(phba, NULL,
8146 MBX_NOWAIT);
8147 } while (rc == MBX_NOT_FINISHED);
8148 if (rc != MBX_SUCCESS)
8149 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8150 LOG_SLI, "0349 rc should be "
6a9c52cf 8151 "MBX_SUCCESS\n");
92d7f7b0
JS
8152 }
8153
5b75da2f 8154 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8155 phba->work_ha |= work_ha_copy;
5b75da2f 8156 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 8157 lpfc_worker_wake_up(phba);
dea3101e 8158 }
9399627f 8159 return IRQ_HANDLED;
dea3101e 8160
3772a991 8161} /* lpfc_sli_sp_intr_handler */
9399627f
JS
8162
8163/**
3772a991 8164 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
8165 * @irq: Interrupt number.
8166 * @dev_id: The device context pointer.
8167 *
8168 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8169 * service routine when device with SLI-3 interface spec is enabled with
8170 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8171 * ring event in the HBA. However, when the device is enabled with either
8172 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8173 * device-level interrupt handler. When the PCI slot is in error recovery
8174 * or the HBA is undergoing initialization, the interrupt handler will not
8175 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8176 * the intrrupt context. This function is called without any lock held.
8177 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
8178 *
8179 * This function returns IRQ_HANDLED when interrupt is handled else it
8180 * returns IRQ_NONE.
8181 **/
8182irqreturn_t
3772a991 8183lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
8184{
8185 struct lpfc_hba *phba;
8186 uint32_t ha_copy;
8187 unsigned long status;
5b75da2f 8188 unsigned long iflag;
9399627f
JS
8189
8190 /* Get the driver's phba structure from the dev_id and
8191 * assume the HBA is not interrupting.
8192 */
8193 phba = (struct lpfc_hba *) dev_id;
8194
8195 if (unlikely(!phba))
8196 return IRQ_NONE;
8197
8198 /*
8199 * Stuff needs to be attented to when this function is invoked as an
8200 * individual interrupt handler in MSI-X multi-message interrupt mode
8201 */
8202 if (phba->intr_type == MSIX) {
3772a991
JS
8203 /* Check device state for handling interrupt */
8204 if (lpfc_intr_state_check(phba))
9399627f
JS
8205 return IRQ_NONE;
8206 /* Need to read HA REG for FCP ring and other ring events */
8207 ha_copy = readl(phba->HAregaddr);
8208 /* Clear up only attention source related to fast-path */
5b75da2f 8209 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
8210 /*
8211 * If there is deferred error attention, do not check for
8212 * any interrupt.
8213 */
8214 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8215 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8216 return IRQ_NONE;
8217 }
9399627f
JS
8218 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8219 phba->HAregaddr);
8220 readl(phba->HAregaddr); /* flush */
5b75da2f 8221 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8222 } else
8223 ha_copy = phba->ha_copy;
dea3101e 8224
8225 /*
9399627f 8226 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 8227 */
9399627f
JS
8228 ha_copy &= ~(phba->work_ha_mask);
8229
8230 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 8231 status >>= (4*LPFC_FCP_RING);
858c9f6c 8232 if (status & HA_RXMASK)
dea3101e 8233 lpfc_sli_handle_fast_ring_event(phba,
8234 &phba->sli.ring[LPFC_FCP_RING],
8235 status);
a4bc3379
JS
8236
8237 if (phba->cfg_multi_ring_support == 2) {
8238 /*
9399627f
JS
8239 * Process all events on extra ring. Take the optimized path
8240 * for extra ring IO.
a4bc3379 8241 */
9399627f 8242 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 8243 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 8244 if (status & HA_RXMASK) {
a4bc3379
JS
8245 lpfc_sli_handle_fast_ring_event(phba,
8246 &phba->sli.ring[LPFC_EXTRA_RING],
8247 status);
8248 }
8249 }
dea3101e 8250 return IRQ_HANDLED;
3772a991 8251} /* lpfc_sli_fp_intr_handler */
9399627f
JS
8252
8253/**
3772a991 8254 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
8255 * @irq: Interrupt number.
8256 * @dev_id: The device context pointer.
8257 *
3772a991
JS
8258 * This function is the HBA device-level interrupt handler to device with
8259 * SLI-3 interface spec, called from the PCI layer when either MSI or
8260 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8261 * requires driver attention. This function invokes the slow-path interrupt
8262 * attention handling function and fast-path interrupt attention handling
8263 * function in turn to process the relevant HBA attention events. This
8264 * function is called without any lock held. It gets the hbalock to access
8265 * and update SLI data structures.
9399627f
JS
8266 *
8267 * This function returns IRQ_HANDLED when interrupt is handled, else it
8268 * returns IRQ_NONE.
8269 **/
8270irqreturn_t
3772a991 8271lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
8272{
8273 struct lpfc_hba *phba;
8274 irqreturn_t sp_irq_rc, fp_irq_rc;
8275 unsigned long status1, status2;
8276
8277 /*
8278 * Get the driver's phba structure from the dev_id and
8279 * assume the HBA is not interrupting.
8280 */
8281 phba = (struct lpfc_hba *) dev_id;
8282
8283 if (unlikely(!phba))
8284 return IRQ_NONE;
8285
3772a991
JS
8286 /* Check device state for handling interrupt */
8287 if (lpfc_intr_state_check(phba))
9399627f
JS
8288 return IRQ_NONE;
8289
8290 spin_lock(&phba->hbalock);
8291 phba->ha_copy = readl(phba->HAregaddr);
8292 if (unlikely(!phba->ha_copy)) {
8293 spin_unlock(&phba->hbalock);
8294 return IRQ_NONE;
8295 } else if (phba->ha_copy & HA_ERATT) {
8296 if (phba->hba_flag & HBA_ERATT_HANDLED)
8297 /* ERATT polling has handled ERATT */
8298 phba->ha_copy &= ~HA_ERATT;
8299 else
8300 /* Indicate interrupt handler handles ERATT */
8301 phba->hba_flag |= HBA_ERATT_HANDLED;
8302 }
8303
a257bf90
JS
8304 /*
8305 * If there is deferred error attention, do not check for any interrupt.
8306 */
8307 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8308 spin_unlock_irq(&phba->hbalock);
8309 return IRQ_NONE;
8310 }
8311
9399627f
JS
8312 /* Clear attention sources except link and error attentions */
8313 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8314 readl(phba->HAregaddr); /* flush */
8315 spin_unlock(&phba->hbalock);
8316
8317 /*
8318 * Invokes slow-path host attention interrupt handling as appropriate.
8319 */
8320
8321 /* status of events with mailbox and link attention */
8322 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8323
8324 /* status of events with ELS ring */
8325 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8326 status2 >>= (4*LPFC_ELS_RING);
8327
8328 if (status1 || (status2 & HA_RXMASK))
3772a991 8329 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
8330 else
8331 sp_irq_rc = IRQ_NONE;
8332
8333 /*
8334 * Invoke fast-path host attention interrupt handling as appropriate.
8335 */
8336
8337 /* status of events with FCP ring */
8338 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8339 status1 >>= (4*LPFC_FCP_RING);
8340
8341 /* status of events with extra ring */
8342 if (phba->cfg_multi_ring_support == 2) {
8343 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8344 status2 >>= (4*LPFC_EXTRA_RING);
8345 } else
8346 status2 = 0;
8347
8348 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 8349 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
8350 else
8351 fp_irq_rc = IRQ_NONE;
dea3101e 8352
9399627f
JS
8353 /* Return device-level interrupt handling status */
8354 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 8355} /* lpfc_sli_intr_handler */
4f774513
JS
8356
8357/**
8358 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8359 * @phba: pointer to lpfc hba data structure.
8360 *
8361 * This routine is invoked by the worker thread to process all the pending
8362 * SLI4 FCP abort XRI events.
8363 **/
8364void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8365{
8366 struct lpfc_cq_event *cq_event;
8367
8368 /* First, declare the fcp xri abort event has been handled */
8369 spin_lock_irq(&phba->hbalock);
8370 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8371 spin_unlock_irq(&phba->hbalock);
8372 /* Now, handle all the fcp xri abort events */
8373 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8374 /* Get the first event from the head of the event queue */
8375 spin_lock_irq(&phba->hbalock);
8376 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8377 cq_event, struct lpfc_cq_event, list);
8378 spin_unlock_irq(&phba->hbalock);
8379 /* Notify aborted XRI for FCP work queue */
8380 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8381 /* Free the event processed back to the free pool */
8382 lpfc_sli4_cq_event_release(phba, cq_event);
8383 }
8384}
8385
8386/**
8387 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8388 * @phba: pointer to lpfc hba data structure.
8389 *
8390 * This routine is invoked by the worker thread to process all the pending
8391 * SLI4 els abort xri events.
8392 **/
8393void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8394{
8395 struct lpfc_cq_event *cq_event;
8396
8397 /* First, declare the els xri abort event has been handled */
8398 spin_lock_irq(&phba->hbalock);
8399 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8400 spin_unlock_irq(&phba->hbalock);
8401 /* Now, handle all the els xri abort events */
8402 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8403 /* Get the first event from the head of the event queue */
8404 spin_lock_irq(&phba->hbalock);
8405 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8406 cq_event, struct lpfc_cq_event, list);
8407 spin_unlock_irq(&phba->hbalock);
8408 /* Notify aborted XRI for ELS work queue */
8409 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8410 /* Free the event processed back to the free pool */
8411 lpfc_sli4_cq_event_release(phba, cq_event);
8412 }
8413}
8414
8415static void
8416lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8417 struct lpfc_iocbq *pIocbOut,
8418 struct lpfc_wcqe_complete *wcqe)
8419{
8420 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8421
8422 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8423 sizeof(struct lpfc_iocbq) - offset);
4d9ab994 8424 pIocbIn->cq_event.cqe.wcqe_cmpl = *wcqe;
4f774513
JS
8425 /* Map WCQE parameters into irspiocb parameters */
8426 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8427 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8428 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8429 pIocbIn->iocb.un.fcpi.fcpi_parm =
8430 pIocbOut->iocb.un.fcpi.fcpi_parm -
8431 wcqe->total_data_placed;
8432 else
8433 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8434 else
8435 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
4f774513
JS
8436}
8437
04c68496
JS
8438/**
8439 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8440 * @phba: Pointer to HBA context object.
8441 * @cqe: Pointer to mailbox completion queue entry.
8442 *
8443 * This routine process a mailbox completion queue entry with asynchrous
8444 * event.
8445 *
8446 * Return: true if work posted to worker thread, otherwise false.
8447 **/
8448static bool
8449lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8450{
8451 struct lpfc_cq_event *cq_event;
8452 unsigned long iflags;
8453
8454 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8455 "0392 Async Event: word0:x%x, word1:x%x, "
8456 "word2:x%x, word3:x%x\n", mcqe->word0,
8457 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8458
8459 /* Allocate a new internal CQ_EVENT entry */
8460 cq_event = lpfc_sli4_cq_event_alloc(phba);
8461 if (!cq_event) {
8462 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8463 "0394 Failed to allocate CQ_EVENT entry\n");
8464 return false;
8465 }
8466
8467 /* Move the CQE into an asynchronous event entry */
8468 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8469 spin_lock_irqsave(&phba->hbalock, iflags);
8470 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8471 /* Set the async event flag */
8472 phba->hba_flag |= ASYNC_EVENT;
8473 spin_unlock_irqrestore(&phba->hbalock, iflags);
8474
8475 return true;
8476}
8477
8478/**
8479 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8480 * @phba: Pointer to HBA context object.
8481 * @cqe: Pointer to mailbox completion queue entry.
8482 *
8483 * This routine process a mailbox completion queue entry with mailbox
8484 * completion event.
8485 *
8486 * Return: true if work posted to worker thread, otherwise false.
8487 **/
8488static bool
8489lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8490{
8491 uint32_t mcqe_status;
8492 MAILBOX_t *mbox, *pmbox;
8493 struct lpfc_mqe *mqe;
8494 struct lpfc_vport *vport;
8495 struct lpfc_nodelist *ndlp;
8496 struct lpfc_dmabuf *mp;
8497 unsigned long iflags;
8498 LPFC_MBOXQ_t *pmb;
8499 bool workposted = false;
8500 int rc;
8501
8502 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8503 if (!bf_get(lpfc_trailer_completed, mcqe))
8504 goto out_no_mqe_complete;
8505
8506 /* Get the reference to the active mbox command */
8507 spin_lock_irqsave(&phba->hbalock, iflags);
8508 pmb = phba->sli.mbox_active;
8509 if (unlikely(!pmb)) {
8510 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8511 "1832 No pending MBOX command to handle\n");
8512 spin_unlock_irqrestore(&phba->hbalock, iflags);
8513 goto out_no_mqe_complete;
8514 }
8515 spin_unlock_irqrestore(&phba->hbalock, iflags);
8516 mqe = &pmb->u.mqe;
8517 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8518 mbox = phba->mbox;
8519 vport = pmb->vport;
8520
8521 /* Reset heartbeat timer */
8522 phba->last_completion_time = jiffies;
8523 del_timer(&phba->sli.mbox_tmo);
8524
8525 /* Move mbox data to caller's mailbox region, do endian swapping */
8526 if (pmb->mbox_cmpl && mbox)
8527 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8528 /* Set the mailbox status with SLI4 range 0x4000 */
8529 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8530 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8531 bf_set(lpfc_mqe_status, mqe,
8532 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8533
8534 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8535 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8536 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8537 "MBOX dflt rpi: status:x%x rpi:x%x",
8538 mcqe_status,
8539 pmbox->un.varWords[0], 0);
8540 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8541 mp = (struct lpfc_dmabuf *)(pmb->context1);
8542 ndlp = (struct lpfc_nodelist *)pmb->context2;
8543 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8544 * RID of the PPI using the same mbox buffer.
8545 */
8546 lpfc_unreg_login(phba, vport->vpi,
8547 pmbox->un.varWords[0], pmb);
8548 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8549 pmb->context1 = mp;
8550 pmb->context2 = ndlp;
8551 pmb->vport = vport;
8552 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8553 if (rc != MBX_BUSY)
8554 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8555 LOG_SLI, "0385 rc should "
8556 "have been MBX_BUSY\n");
8557 if (rc != MBX_NOT_FINISHED)
8558 goto send_current_mbox;
8559 }
8560 }
8561 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8562 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8563 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8564
8565 /* There is mailbox completion work to do */
8566 spin_lock_irqsave(&phba->hbalock, iflags);
8567 __lpfc_mbox_cmpl_put(phba, pmb);
8568 phba->work_ha |= HA_MBATT;
8569 spin_unlock_irqrestore(&phba->hbalock, iflags);
8570 workposted = true;
8571
8572send_current_mbox:
8573 spin_lock_irqsave(&phba->hbalock, iflags);
8574 /* Release the mailbox command posting token */
8575 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8576 /* Setting active mailbox pointer need to be in sync to flag clear */
8577 phba->sli.mbox_active = NULL;
8578 spin_unlock_irqrestore(&phba->hbalock, iflags);
8579 /* Wake up worker thread to post the next pending mailbox command */
8580 lpfc_worker_wake_up(phba);
8581out_no_mqe_complete:
8582 if (bf_get(lpfc_trailer_consumed, mcqe))
8583 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8584 return workposted;
8585}
8586
8587/**
8588 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8589 * @phba: Pointer to HBA context object.
8590 * @cqe: Pointer to mailbox completion queue entry.
8591 *
8592 * This routine process a mailbox completion queue entry, it invokes the
8593 * proper mailbox complete handling or asynchrous event handling routine
8594 * according to the MCQE's async bit.
8595 *
8596 * Return: true if work posted to worker thread, otherwise false.
8597 **/
8598static bool
8599lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8600{
8601 struct lpfc_mcqe mcqe;
8602 bool workposted;
8603
8604 /* Copy the mailbox MCQE and convert endian order as needed */
8605 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8606
8607 /* Invoke the proper event handling routine */
8608 if (!bf_get(lpfc_trailer_async, &mcqe))
8609 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8610 else
8611 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8612 return workposted;
8613}
8614
4f774513
JS
8615/**
8616 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8617 * @phba: Pointer to HBA context object.
8618 * @wcqe: Pointer to work-queue completion queue entry.
8619 *
8620 * This routine handles an ELS work-queue completion event.
8621 *
8622 * Return: true if work posted to worker thread, otherwise false.
8623 **/
8624static bool
8625lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8626 struct lpfc_wcqe_complete *wcqe)
8627{
8628 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8629 struct lpfc_iocbq *cmdiocbq;
8630 struct lpfc_iocbq *irspiocbq;
8631 unsigned long iflags;
8632 bool workposted = false;
8633
8634 spin_lock_irqsave(&phba->hbalock, iflags);
8635 pring->stats.iocb_event++;
8636 /* Look up the ELS command IOCB and create pseudo response IOCB */
8637 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8638 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8639 spin_unlock_irqrestore(&phba->hbalock, iflags);
8640
8641 if (unlikely(!cmdiocbq)) {
8642 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8643 "0386 ELS complete with no corresponding "
8644 "cmdiocb: iotag (%d)\n",
8645 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8646 return workposted;
8647 }
8648
8649 /* Fake the irspiocbq and copy necessary response information */
8650 irspiocbq = lpfc_sli_get_iocbq(phba);
8651 if (!irspiocbq) {
8652 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8653 "0387 Failed to allocate an iocbq\n");
8654 return workposted;
8655 }
8656 lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8657
8658 /* Add the irspiocb to the response IOCB work list */
8659 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994
JS
8660 list_add_tail(&irspiocbq->cq_event.list,
8661 &phba->sli4_hba.sp_rspiocb_work_queue);
4f774513
JS
8662 /* Indicate ELS ring attention */
8663 phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
8664 spin_unlock_irqrestore(&phba->hbalock, iflags);
8665 workposted = true;
8666
8667 return workposted;
8668}
8669
8670/**
8671 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8672 * @phba: Pointer to HBA context object.
8673 * @wcqe: Pointer to work-queue completion queue entry.
8674 *
8675 * This routine handles slow-path WQ entry comsumed event by invoking the
8676 * proper WQ release routine to the slow-path WQ.
8677 **/
8678static void
8679lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8680 struct lpfc_wcqe_release *wcqe)
8681{
8682 /* Check for the slow-path ELS work queue */
8683 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8684 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8685 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8686 else
8687 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8688 "2579 Slow-path wqe consume event carries "
8689 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8690 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8691 phba->sli4_hba.els_wq->queue_id);
8692}
8693
8694/**
8695 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8696 * @phba: Pointer to HBA context object.
8697 * @cq: Pointer to a WQ completion queue.
8698 * @wcqe: Pointer to work-queue completion queue entry.
8699 *
8700 * This routine handles an XRI abort event.
8701 *
8702 * Return: true if work posted to worker thread, otherwise false.
8703 **/
8704static bool
8705lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8706 struct lpfc_queue *cq,
8707 struct sli4_wcqe_xri_aborted *wcqe)
8708{
8709 bool workposted = false;
8710 struct lpfc_cq_event *cq_event;
8711 unsigned long iflags;
8712
8713 /* Allocate a new internal CQ_EVENT entry */
8714 cq_event = lpfc_sli4_cq_event_alloc(phba);
8715 if (!cq_event) {
8716 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8717 "0602 Failed to allocate CQ_EVENT entry\n");
8718 return false;
8719 }
8720
8721 /* Move the CQE into the proper xri abort event list */
8722 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8723 switch (cq->subtype) {
8724 case LPFC_FCP:
8725 spin_lock_irqsave(&phba->hbalock, iflags);
8726 list_add_tail(&cq_event->list,
8727 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8728 /* Set the fcp xri abort event flag */
8729 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8730 spin_unlock_irqrestore(&phba->hbalock, iflags);
8731 workposted = true;
8732 break;
8733 case LPFC_ELS:
8734 spin_lock_irqsave(&phba->hbalock, iflags);
8735 list_add_tail(&cq_event->list,
8736 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8737 /* Set the els xri abort event flag */
8738 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8739 spin_unlock_irqrestore(&phba->hbalock, iflags);
8740 workposted = true;
8741 break;
8742 default:
8743 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8744 "0603 Invalid work queue CQE subtype (x%x)\n",
8745 cq->subtype);
8746 workposted = false;
8747 break;
8748 }
8749 return workposted;
8750}
8751
4f774513
JS
8752/**
8753 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8754 * @phba: Pointer to HBA context object.
8755 * @rcqe: Pointer to receive-queue completion queue entry.
8756 *
8757 * This routine process a receive-queue completion queue entry.
8758 *
8759 * Return: true if work posted to worker thread, otherwise false.
8760 **/
8761static bool
4d9ab994 8762lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 8763{
4f774513
JS
8764 bool workposted = false;
8765 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8766 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8767 struct hbq_dmabuf *dma_buf;
8768 uint32_t status;
8769 unsigned long iflags;
8770
4f774513 8771 lpfc_sli4_rq_release(hrq, drq);
4d9ab994 8772 if (bf_get(lpfc_rcqe_code, rcqe) != CQE_CODE_RECEIVE)
4f774513 8773 goto out;
4d9ab994 8774 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
4f774513
JS
8775 goto out;
8776
4d9ab994 8777 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
8778 switch (status) {
8779 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8780 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8781 "2537 Receive Frame Truncated!!\n");
8782 case FC_STATUS_RQ_SUCCESS:
8783 spin_lock_irqsave(&phba->hbalock, iflags);
8784 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8785 if (!dma_buf) {
8786 spin_unlock_irqrestore(&phba->hbalock, iflags);
8787 goto out;
8788 }
4d9ab994 8789 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 8790 /* save off the frame for the word thread to process */
4d9ab994
JS
8791 list_add_tail(&dma_buf->cq_event.list,
8792 &phba->sli4_hba.sp_rspiocb_work_queue);
4f774513
JS
8793 /* Frame received */
8794 phba->hba_flag |= HBA_RECEIVE_BUFFER;
8795 spin_unlock_irqrestore(&phba->hbalock, iflags);
8796 workposted = true;
8797 break;
8798 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8799 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8800 /* Post more buffers if possible */
8801 spin_lock_irqsave(&phba->hbalock, iflags);
8802 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8803 spin_unlock_irqrestore(&phba->hbalock, iflags);
8804 workposted = true;
8805 break;
8806 }
8807out:
8808 return workposted;
8809
8810}
8811
4d9ab994
JS
8812/**
8813 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8814 * @phba: Pointer to HBA context object.
8815 * @cq: Pointer to the completion queue.
8816 * @wcqe: Pointer to a completion queue entry.
8817 *
8818 * This routine process a slow-path work-queue or recieve queue completion queue
8819 * entry.
8820 *
8821 * Return: true if work posted to worker thread, otherwise false.
8822 **/
8823static bool
8824lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8825 struct lpfc_cqe *cqe)
8826{
8827 struct lpfc_wcqe_complete wcqe;
8828 bool workposted = false;
8829
8830 /* Copy the work queue CQE and convert endian order if needed */
8831 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8832
8833 /* Check and process for different type of WCQE and dispatch */
8834 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8835 case CQE_CODE_COMPL_WQE:
8836 /* Process the WQ complete event */
8837 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8838 (struct lpfc_wcqe_complete *)&wcqe);
8839 break;
8840 case CQE_CODE_RELEASE_WQE:
8841 /* Process the WQ release event */
8842 lpfc_sli4_sp_handle_rel_wcqe(phba,
8843 (struct lpfc_wcqe_release *)&wcqe);
8844 break;
8845 case CQE_CODE_XRI_ABORTED:
8846 /* Process the WQ XRI abort event */
8847 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8848 (struct sli4_wcqe_xri_aborted *)&wcqe);
8849 break;
8850 case CQE_CODE_RECEIVE:
8851 /* Process the RQ event */
8852 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8853 (struct lpfc_rcqe *)&wcqe);
8854 break;
8855 default:
8856 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8857 "0388 Not a valid WCQE code: x%x\n",
8858 bf_get(lpfc_wcqe_c_code, &wcqe));
8859 break;
8860 }
8861 return workposted;
8862}
8863
4f774513
JS
8864/**
8865 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8866 * @phba: Pointer to HBA context object.
8867 * @eqe: Pointer to fast-path event queue entry.
8868 *
8869 * This routine process a event queue entry from the slow-path event queue.
8870 * It will check the MajorCode and MinorCode to determine this is for a
8871 * completion event on a completion queue, if not, an error shall be logged
8872 * and just return. Otherwise, it will get to the corresponding completion
8873 * queue and process all the entries on that completion queue, rearm the
8874 * completion queue, and then return.
8875 *
8876 **/
8877static void
8878lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8879{
8880 struct lpfc_queue *cq = NULL, *childq, *speq;
8881 struct lpfc_cqe *cqe;
8882 bool workposted = false;
8883 int ecount = 0;
8884 uint16_t cqid;
8885
8886 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8887 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8888 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8889 "0359 Not a valid slow-path completion "
8890 "event: majorcode=x%x, minorcode=x%x\n",
8891 bf_get(lpfc_eqe_major_code, eqe),
8892 bf_get(lpfc_eqe_minor_code, eqe));
8893 return;
8894 }
8895
8896 /* Get the reference to the corresponding CQ */
8897 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8898
8899 /* Search for completion queue pointer matching this cqid */
8900 speq = phba->sli4_hba.sp_eq;
8901 list_for_each_entry(childq, &speq->child_list, list) {
8902 if (childq->queue_id == cqid) {
8903 cq = childq;
8904 break;
8905 }
8906 }
8907 if (unlikely(!cq)) {
8908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8909 "0365 Slow-path CQ identifier (%d) does "
8910 "not exist\n", cqid);
8911 return;
8912 }
8913
8914 /* Process all the entries to the CQ */
8915 switch (cq->type) {
8916 case LPFC_MCQ:
8917 while ((cqe = lpfc_sli4_cq_get(cq))) {
8918 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8919 if (!(++ecount % LPFC_GET_QE_REL_INT))
8920 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8921 }
8922 break;
8923 case LPFC_WCQ:
8924 while ((cqe = lpfc_sli4_cq_get(cq))) {
4d9ab994 8925 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
4f774513
JS
8926 if (!(++ecount % LPFC_GET_QE_REL_INT))
8927 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8928 }
8929 break;
8930 default:
8931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8932 "0370 Invalid completion queue type (%d)\n",
8933 cq->type);
8934 return;
8935 }
8936
8937 /* Catch the no cq entry condition, log an error */
8938 if (unlikely(ecount == 0))
8939 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8940 "0371 No entry from the CQ: identifier "
8941 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8942
8943 /* In any case, flash and re-arm the RCQ */
8944 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8945
8946 /* wake up worker thread if there are works to be done */
8947 if (workposted)
8948 lpfc_worker_wake_up(phba);
8949}
8950
8951/**
8952 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8953 * @eqe: Pointer to fast-path completion queue entry.
8954 *
8955 * This routine process a fast-path work queue completion entry from fast-path
8956 * event queue for FCP command response completion.
8957 **/
8958static void
8959lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8960 struct lpfc_wcqe_complete *wcqe)
8961{
8962 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8963 struct lpfc_iocbq *cmdiocbq;
8964 struct lpfc_iocbq irspiocbq;
8965 unsigned long iflags;
8966
8967 spin_lock_irqsave(&phba->hbalock, iflags);
8968 pring->stats.iocb_event++;
8969 spin_unlock_irqrestore(&phba->hbalock, iflags);
8970
8971 /* Check for response status */
8972 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8973 /* If resource errors reported from HBA, reduce queue
8974 * depth of the SCSI device.
8975 */
8976 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8977 IOSTAT_LOCAL_REJECT) &&
8978 (wcqe->parameter == IOERR_NO_RESOURCES)) {
8979 phba->lpfc_rampdown_queue_depth(phba);
8980 }
8981 /* Log the error status */
8982 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8983 "0373 FCP complete error: status=x%x, "
8984 "hw_status=x%x, total_data_specified=%d, "
8985 "parameter=x%x, word3=x%x\n",
8986 bf_get(lpfc_wcqe_c_status, wcqe),
8987 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8988 wcqe->total_data_placed, wcqe->parameter,
8989 wcqe->word3);
8990 }
8991
8992 /* Look up the FCP command IOCB and create pseudo response IOCB */
8993 spin_lock_irqsave(&phba->hbalock, iflags);
8994 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8995 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8996 spin_unlock_irqrestore(&phba->hbalock, iflags);
8997 if (unlikely(!cmdiocbq)) {
8998 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8999 "0374 FCP complete with no corresponding "
9000 "cmdiocb: iotag (%d)\n",
9001 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9002 return;
9003 }
9004 if (unlikely(!cmdiocbq->iocb_cmpl)) {
9005 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9006 "0375 FCP cmdiocb not callback function "
9007 "iotag: (%d)\n",
9008 bf_get(lpfc_wcqe_c_request_tag, wcqe));
9009 return;
9010 }
9011
9012 /* Fake the irspiocb and copy necessary response information */
9013 lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
9014
9015 /* Pass the cmd_iocb and the rsp state to the upper layer */
9016 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
9017}
9018
9019/**
9020 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
9021 * @phba: Pointer to HBA context object.
9022 * @cq: Pointer to completion queue.
9023 * @wcqe: Pointer to work-queue completion queue entry.
9024 *
9025 * This routine handles an fast-path WQ entry comsumed event by invoking the
9026 * proper WQ release routine to the slow-path WQ.
9027 **/
9028static void
9029lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9030 struct lpfc_wcqe_release *wcqe)
9031{
9032 struct lpfc_queue *childwq;
9033 bool wqid_matched = false;
9034 uint16_t fcp_wqid;
9035
9036 /* Check for fast-path FCP work queue release */
9037 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9038 list_for_each_entry(childwq, &cq->child_list, list) {
9039 if (childwq->queue_id == fcp_wqid) {
9040 lpfc_sli4_wq_release(childwq,
9041 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9042 wqid_matched = true;
9043 break;
9044 }
9045 }
9046 /* Report warning log message if no match found */
9047 if (wqid_matched != true)
9048 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9049 "2580 Fast-path wqe consume event carries "
9050 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9051}
9052
9053/**
9054 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9055 * @cq: Pointer to the completion queue.
9056 * @eqe: Pointer to fast-path completion queue entry.
9057 *
9058 * This routine process a fast-path work queue completion entry from fast-path
9059 * event queue for FCP command response completion.
9060 **/
9061static int
9062lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9063 struct lpfc_cqe *cqe)
9064{
9065 struct lpfc_wcqe_release wcqe;
9066 bool workposted = false;
9067
9068 /* Copy the work queue CQE and convert endian order if needed */
9069 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9070
9071 /* Check and process for different type of WCQE and dispatch */
9072 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9073 case CQE_CODE_COMPL_WQE:
9074 /* Process the WQ complete event */
9075 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9076 (struct lpfc_wcqe_complete *)&wcqe);
9077 break;
9078 case CQE_CODE_RELEASE_WQE:
9079 /* Process the WQ release event */
9080 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9081 (struct lpfc_wcqe_release *)&wcqe);
9082 break;
9083 case CQE_CODE_XRI_ABORTED:
9084 /* Process the WQ XRI abort event */
9085 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9086 (struct sli4_wcqe_xri_aborted *)&wcqe);
9087 break;
9088 default:
9089 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9090 "0144 Not a valid WCQE code: x%x\n",
9091 bf_get(lpfc_wcqe_c_code, &wcqe));
9092 break;
9093 }
9094 return workposted;
9095}
9096
9097/**
9098 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9099 * @phba: Pointer to HBA context object.
9100 * @eqe: Pointer to fast-path event queue entry.
9101 *
9102 * This routine process a event queue entry from the fast-path event queue.
9103 * It will check the MajorCode and MinorCode to determine this is for a
9104 * completion event on a completion queue, if not, an error shall be logged
9105 * and just return. Otherwise, it will get to the corresponding completion
9106 * queue and process all the entries on the completion queue, rearm the
9107 * completion queue, and then return.
9108 **/
9109static void
9110lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9111 uint32_t fcp_cqidx)
9112{
9113 struct lpfc_queue *cq;
9114 struct lpfc_cqe *cqe;
9115 bool workposted = false;
9116 uint16_t cqid;
9117 int ecount = 0;
9118
9119 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9120 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9121 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9122 "0366 Not a valid fast-path completion "
9123 "event: majorcode=x%x, minorcode=x%x\n",
9124 bf_get(lpfc_eqe_major_code, eqe),
9125 bf_get(lpfc_eqe_minor_code, eqe));
9126 return;
9127 }
9128
9129 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9130 if (unlikely(!cq)) {
9131 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9132 "0367 Fast-path completion queue does not "
9133 "exist\n");
9134 return;
9135 }
9136
9137 /* Get the reference to the corresponding CQ */
9138 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9139 if (unlikely(cqid != cq->queue_id)) {
9140 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9141 "0368 Miss-matched fast-path completion "
9142 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9143 cqid, cq->queue_id);
9144 return;
9145 }
9146
9147 /* Process all the entries to the CQ */
9148 while ((cqe = lpfc_sli4_cq_get(cq))) {
9149 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9150 if (!(++ecount % LPFC_GET_QE_REL_INT))
9151 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9152 }
9153
9154 /* Catch the no cq entry condition */
9155 if (unlikely(ecount == 0))
9156 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9157 "0369 No entry from fast-path completion "
9158 "queue fcpcqid=%d\n", cq->queue_id);
9159
9160 /* In any case, flash and re-arm the CQ */
9161 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9162
9163 /* wake up worker thread if there are works to be done */
9164 if (workposted)
9165 lpfc_worker_wake_up(phba);
9166}
9167
9168static void
9169lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9170{
9171 struct lpfc_eqe *eqe;
9172
9173 /* walk all the EQ entries and drop on the floor */
9174 while ((eqe = lpfc_sli4_eq_get(eq)))
9175 ;
9176
9177 /* Clear and re-arm the EQ */
9178 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9179}
9180
9181/**
9182 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9183 * @irq: Interrupt number.
9184 * @dev_id: The device context pointer.
9185 *
9186 * This function is directly called from the PCI layer as an interrupt
9187 * service routine when device with SLI-4 interface spec is enabled with
9188 * MSI-X multi-message interrupt mode and there are slow-path events in
9189 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9190 * interrupt mode, this function is called as part of the device-level
9191 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9192 * undergoing initialization, the interrupt handler will not process the
9193 * interrupt. The link attention and ELS ring attention events are handled
9194 * by the worker thread. The interrupt handler signals the worker thread
9195 * and returns for these events. This function is called without any lock
9196 * held. It gets the hbalock to access and update SLI data structures.
9197 *
9198 * This function returns IRQ_HANDLED when interrupt is handled else it
9199 * returns IRQ_NONE.
9200 **/
9201irqreturn_t
9202lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9203{
9204 struct lpfc_hba *phba;
9205 struct lpfc_queue *speq;
9206 struct lpfc_eqe *eqe;
9207 unsigned long iflag;
9208 int ecount = 0;
9209
9210 /*
9211 * Get the driver's phba structure from the dev_id
9212 */
9213 phba = (struct lpfc_hba *)dev_id;
9214
9215 if (unlikely(!phba))
9216 return IRQ_NONE;
9217
9218 /* Get to the EQ struct associated with this vector */
9219 speq = phba->sli4_hba.sp_eq;
9220
9221 /* Check device state for handling interrupt */
9222 if (unlikely(lpfc_intr_state_check(phba))) {
9223 /* Check again for link_state with lock held */
9224 spin_lock_irqsave(&phba->hbalock, iflag);
9225 if (phba->link_state < LPFC_LINK_DOWN)
9226 /* Flush, clear interrupt, and rearm the EQ */
9227 lpfc_sli4_eq_flush(phba, speq);
9228 spin_unlock_irqrestore(&phba->hbalock, iflag);
9229 return IRQ_NONE;
9230 }
9231
9232 /*
9233 * Process all the event on FCP slow-path EQ
9234 */
9235 while ((eqe = lpfc_sli4_eq_get(speq))) {
9236 lpfc_sli4_sp_handle_eqe(phba, eqe);
9237 if (!(++ecount % LPFC_GET_QE_REL_INT))
9238 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9239 }
9240
9241 /* Always clear and re-arm the slow-path EQ */
9242 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9243
9244 /* Catch the no cq entry condition */
9245 if (unlikely(ecount == 0)) {
9246 if (phba->intr_type == MSIX)
9247 /* MSI-X treated interrupt served as no EQ share INT */
9248 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9249 "0357 MSI-X interrupt with no EQE\n");
9250 else
9251 /* Non MSI-X treated on interrupt as EQ share INT */
9252 return IRQ_NONE;
9253 }
9254
9255 return IRQ_HANDLED;
9256} /* lpfc_sli4_sp_intr_handler */
9257
9258/**
9259 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9260 * @irq: Interrupt number.
9261 * @dev_id: The device context pointer.
9262 *
9263 * This function is directly called from the PCI layer as an interrupt
9264 * service routine when device with SLI-4 interface spec is enabled with
9265 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9266 * ring event in the HBA. However, when the device is enabled with either
9267 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9268 * device-level interrupt handler. When the PCI slot is in error recovery
9269 * or the HBA is undergoing initialization, the interrupt handler will not
9270 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9271 * the intrrupt context. This function is called without any lock held.
9272 * It gets the hbalock to access and update SLI data structures. Note that,
9273 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9274 * equal to that of FCP CQ index.
9275 *
9276 * This function returns IRQ_HANDLED when interrupt is handled else it
9277 * returns IRQ_NONE.
9278 **/
9279irqreturn_t
9280lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9281{
9282 struct lpfc_hba *phba;
9283 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9284 struct lpfc_queue *fpeq;
9285 struct lpfc_eqe *eqe;
9286 unsigned long iflag;
9287 int ecount = 0;
9288 uint32_t fcp_eqidx;
9289
9290 /* Get the driver's phba structure from the dev_id */
9291 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9292 phba = fcp_eq_hdl->phba;
9293 fcp_eqidx = fcp_eq_hdl->idx;
9294
9295 if (unlikely(!phba))
9296 return IRQ_NONE;
9297
9298 /* Get to the EQ struct associated with this vector */
9299 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9300
9301 /* Check device state for handling interrupt */
9302 if (unlikely(lpfc_intr_state_check(phba))) {
9303 /* Check again for link_state with lock held */
9304 spin_lock_irqsave(&phba->hbalock, iflag);
9305 if (phba->link_state < LPFC_LINK_DOWN)
9306 /* Flush, clear interrupt, and rearm the EQ */
9307 lpfc_sli4_eq_flush(phba, fpeq);
9308 spin_unlock_irqrestore(&phba->hbalock, iflag);
9309 return IRQ_NONE;
9310 }
9311
9312 /*
9313 * Process all the event on FCP fast-path EQ
9314 */
9315 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9316 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9317 if (!(++ecount % LPFC_GET_QE_REL_INT))
9318 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9319 }
9320
9321 /* Always clear and re-arm the fast-path EQ */
9322 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9323
9324 if (unlikely(ecount == 0)) {
9325 if (phba->intr_type == MSIX)
9326 /* MSI-X treated interrupt served as no EQ share INT */
9327 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9328 "0358 MSI-X interrupt with no EQE\n");
9329 else
9330 /* Non MSI-X treated on interrupt as EQ share INT */
9331 return IRQ_NONE;
9332 }
9333
9334 return IRQ_HANDLED;
9335} /* lpfc_sli4_fp_intr_handler */
9336
9337/**
9338 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9339 * @irq: Interrupt number.
9340 * @dev_id: The device context pointer.
9341 *
9342 * This function is the device-level interrupt handler to device with SLI-4
9343 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9344 * interrupt mode is enabled and there is an event in the HBA which requires
9345 * driver attention. This function invokes the slow-path interrupt attention
9346 * handling function and fast-path interrupt attention handling function in
9347 * turn to process the relevant HBA attention events. This function is called
9348 * without any lock held. It gets the hbalock to access and update SLI data
9349 * structures.
9350 *
9351 * This function returns IRQ_HANDLED when interrupt is handled, else it
9352 * returns IRQ_NONE.
9353 **/
9354irqreturn_t
9355lpfc_sli4_intr_handler(int irq, void *dev_id)
9356{
9357 struct lpfc_hba *phba;
9358 irqreturn_t sp_irq_rc, fp_irq_rc;
9359 bool fp_handled = false;
9360 uint32_t fcp_eqidx;
9361
9362 /* Get the driver's phba structure from the dev_id */
9363 phba = (struct lpfc_hba *)dev_id;
9364
9365 if (unlikely(!phba))
9366 return IRQ_NONE;
9367
9368 /*
9369 * Invokes slow-path host attention interrupt handling as appropriate.
9370 */
9371 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9372
9373 /*
9374 * Invoke fast-path host attention interrupt handling as appropriate.
9375 */
9376 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9377 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9378 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9379 if (fp_irq_rc == IRQ_HANDLED)
9380 fp_handled |= true;
9381 }
9382
9383 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9384} /* lpfc_sli4_intr_handler */
9385
9386/**
9387 * lpfc_sli4_queue_free - free a queue structure and associated memory
9388 * @queue: The queue structure to free.
9389 *
9390 * This function frees a queue structure and the DMAable memeory used for
9391 * the host resident queue. This function must be called after destroying the
9392 * queue on the HBA.
9393 **/
9394void
9395lpfc_sli4_queue_free(struct lpfc_queue *queue)
9396{
9397 struct lpfc_dmabuf *dmabuf;
9398
9399 if (!queue)
9400 return;
9401
9402 while (!list_empty(&queue->page_list)) {
9403 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9404 list);
9405 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9406 dmabuf->virt, dmabuf->phys);
9407 kfree(dmabuf);
9408 }
9409 kfree(queue);
9410 return;
9411}
9412
9413/**
9414 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9415 * @phba: The HBA that this queue is being created on.
9416 * @entry_size: The size of each queue entry for this queue.
9417 * @entry count: The number of entries that this queue will handle.
9418 *
9419 * This function allocates a queue structure and the DMAable memory used for
9420 * the host resident queue. This function must be called before creating the
9421 * queue on the HBA.
9422 **/
9423struct lpfc_queue *
9424lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9425 uint32_t entry_count)
9426{
9427 struct lpfc_queue *queue;
9428 struct lpfc_dmabuf *dmabuf;
9429 int x, total_qe_count;
9430 void *dma_pointer;
9431
9432
9433 queue = kzalloc(sizeof(struct lpfc_queue) +
9434 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9435 if (!queue)
9436 return NULL;
9437 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9438 INIT_LIST_HEAD(&queue->list);
9439 INIT_LIST_HEAD(&queue->page_list);
9440 INIT_LIST_HEAD(&queue->child_list);
9441 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9442 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9443 if (!dmabuf)
9444 goto out_fail;
9445 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9446 PAGE_SIZE, &dmabuf->phys,
9447 GFP_KERNEL);
9448 if (!dmabuf->virt) {
9449 kfree(dmabuf);
9450 goto out_fail;
9451 }
d11e31dd 9452 memset(dmabuf->virt, 0, PAGE_SIZE);
4f774513
JS
9453 dmabuf->buffer_tag = x;
9454 list_add_tail(&dmabuf->list, &queue->page_list);
9455 /* initialize queue's entry array */
9456 dma_pointer = dmabuf->virt;
9457 for (; total_qe_count < entry_count &&
9458 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9459 total_qe_count++, dma_pointer += entry_size) {
9460 queue->qe[total_qe_count].address = dma_pointer;
9461 }
9462 }
9463 queue->entry_size = entry_size;
9464 queue->entry_count = entry_count;
9465 queue->phba = phba;
9466
9467 return queue;
9468out_fail:
9469 lpfc_sli4_queue_free(queue);
9470 return NULL;
9471}
9472
9473/**
9474 * lpfc_eq_create - Create an Event Queue on the HBA
9475 * @phba: HBA structure that indicates port to create a queue on.
9476 * @eq: The queue structure to use to create the event queue.
9477 * @imax: The maximum interrupt per second limit.
9478 *
9479 * This function creates an event queue, as detailed in @eq, on a port,
9480 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9481 *
9482 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9483 * is used to get the entry count and entry size that are necessary to
9484 * determine the number of pages to allocate and use for this queue. This
9485 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9486 * event queue. This function is asynchronous and will wait for the mailbox
9487 * command to finish before continuing.
9488 *
9489 * On success this function will return a zero. If unable to allocate enough
9490 * memory this function will return ENOMEM. If the queue create mailbox command
9491 * fails this function will return ENXIO.
9492 **/
9493uint32_t
9494lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9495{
9496 struct lpfc_mbx_eq_create *eq_create;
9497 LPFC_MBOXQ_t *mbox;
9498 int rc, length, status = 0;
9499 struct lpfc_dmabuf *dmabuf;
9500 uint32_t shdr_status, shdr_add_status;
9501 union lpfc_sli4_cfg_shdr *shdr;
9502 uint16_t dmult;
9503
9504 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9505 if (!mbox)
9506 return -ENOMEM;
9507 length = (sizeof(struct lpfc_mbx_eq_create) -
9508 sizeof(struct lpfc_sli4_cfg_mhdr));
9509 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9510 LPFC_MBOX_OPCODE_EQ_CREATE,
9511 length, LPFC_SLI4_MBX_EMBED);
9512 eq_create = &mbox->u.mqe.un.eq_create;
9513 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9514 eq->page_count);
9515 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9516 LPFC_EQE_SIZE);
9517 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9518 /* Calculate delay multiper from maximum interrupt per second */
9519 dmult = LPFC_DMULT_CONST/imax - 1;
9520 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9521 dmult);
9522 switch (eq->entry_count) {
9523 default:
9524 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9525 "0360 Unsupported EQ count. (%d)\n",
9526 eq->entry_count);
9527 if (eq->entry_count < 256)
9528 return -EINVAL;
9529 /* otherwise default to smallest count (drop through) */
9530 case 256:
9531 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9532 LPFC_EQ_CNT_256);
9533 break;
9534 case 512:
9535 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9536 LPFC_EQ_CNT_512);
9537 break;
9538 case 1024:
9539 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9540 LPFC_EQ_CNT_1024);
9541 break;
9542 case 2048:
9543 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9544 LPFC_EQ_CNT_2048);
9545 break;
9546 case 4096:
9547 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9548 LPFC_EQ_CNT_4096);
9549 break;
9550 }
9551 list_for_each_entry(dmabuf, &eq->page_list, list) {
9552 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9553 putPaddrLow(dmabuf->phys);
9554 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9555 putPaddrHigh(dmabuf->phys);
9556 }
9557 mbox->vport = phba->pport;
9558 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9559 mbox->context1 = NULL;
9560 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9561 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9562 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9563 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9564 if (shdr_status || shdr_add_status || rc) {
9565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9566 "2500 EQ_CREATE mailbox failed with "
9567 "status x%x add_status x%x, mbx status x%x\n",
9568 shdr_status, shdr_add_status, rc);
9569 status = -ENXIO;
9570 }
9571 eq->type = LPFC_EQ;
9572 eq->subtype = LPFC_NONE;
9573 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9574 if (eq->queue_id == 0xFFFF)
9575 status = -ENXIO;
9576 eq->host_index = 0;
9577 eq->hba_index = 0;
9578
8fa38513 9579 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9580 return status;
9581}
9582
9583/**
9584 * lpfc_cq_create - Create a Completion Queue on the HBA
9585 * @phba: HBA structure that indicates port to create a queue on.
9586 * @cq: The queue structure to use to create the completion queue.
9587 * @eq: The event queue to bind this completion queue to.
9588 *
9589 * This function creates a completion queue, as detailed in @wq, on a port,
9590 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9591 *
9592 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9593 * is used to get the entry count and entry size that are necessary to
9594 * determine the number of pages to allocate and use for this queue. The @eq
9595 * is used to indicate which event queue to bind this completion queue to. This
9596 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9597 * completion queue. This function is asynchronous and will wait for the mailbox
9598 * command to finish before continuing.
9599 *
9600 * On success this function will return a zero. If unable to allocate enough
9601 * memory this function will return ENOMEM. If the queue create mailbox command
9602 * fails this function will return ENXIO.
9603 **/
9604uint32_t
9605lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9606 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9607{
9608 struct lpfc_mbx_cq_create *cq_create;
9609 struct lpfc_dmabuf *dmabuf;
9610 LPFC_MBOXQ_t *mbox;
9611 int rc, length, status = 0;
9612 uint32_t shdr_status, shdr_add_status;
9613 union lpfc_sli4_cfg_shdr *shdr;
9614
9615 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9616 if (!mbox)
9617 return -ENOMEM;
9618 length = (sizeof(struct lpfc_mbx_cq_create) -
9619 sizeof(struct lpfc_sli4_cfg_mhdr));
9620 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9621 LPFC_MBOX_OPCODE_CQ_CREATE,
9622 length, LPFC_SLI4_MBX_EMBED);
9623 cq_create = &mbox->u.mqe.un.cq_create;
9624 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9625 cq->page_count);
9626 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9627 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9628 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9629 switch (cq->entry_count) {
9630 default:
9631 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9632 "0361 Unsupported CQ count. (%d)\n",
9633 cq->entry_count);
9634 if (cq->entry_count < 256)
9635 return -EINVAL;
9636 /* otherwise default to smallest count (drop through) */
9637 case 256:
9638 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9639 LPFC_CQ_CNT_256);
9640 break;
9641 case 512:
9642 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9643 LPFC_CQ_CNT_512);
9644 break;
9645 case 1024:
9646 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9647 LPFC_CQ_CNT_1024);
9648 break;
9649 }
9650 list_for_each_entry(dmabuf, &cq->page_list, list) {
9651 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9652 putPaddrLow(dmabuf->phys);
9653 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9654 putPaddrHigh(dmabuf->phys);
9655 }
9656 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9657
9658 /* The IOCTL status is embedded in the mailbox subheader. */
9659 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9660 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9661 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9662 if (shdr_status || shdr_add_status || rc) {
9663 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9664 "2501 CQ_CREATE mailbox failed with "
9665 "status x%x add_status x%x, mbx status x%x\n",
9666 shdr_status, shdr_add_status, rc);
9667 status = -ENXIO;
9668 goto out;
9669 }
9670 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9671 if (cq->queue_id == 0xFFFF) {
9672 status = -ENXIO;
9673 goto out;
9674 }
9675 /* link the cq onto the parent eq child list */
9676 list_add_tail(&cq->list, &eq->child_list);
9677 /* Set up completion queue's type and subtype */
9678 cq->type = type;
9679 cq->subtype = subtype;
9680 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9681 cq->host_index = 0;
9682 cq->hba_index = 0;
4f774513 9683
8fa38513
JS
9684out:
9685 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9686 return status;
9687}
9688
04c68496
JS
9689/**
9690 * lpfc_mq_create - Create a mailbox Queue on the HBA
9691 * @phba: HBA structure that indicates port to create a queue on.
9692 * @mq: The queue structure to use to create the mailbox queue.
9693 *
9694 * This function creates a mailbox queue, as detailed in @mq, on a port,
9695 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9696 *
9697 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9698 * is used to get the entry count and entry size that are necessary to
9699 * determine the number of pages to allocate and use for this queue. This
9700 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9701 * mailbox queue. This function is asynchronous and will wait for the mailbox
9702 * command to finish before continuing.
9703 *
9704 * On success this function will return a zero. If unable to allocate enough
9705 * memory this function will return ENOMEM. If the queue create mailbox command
9706 * fails this function will return ENXIO.
9707 **/
9708uint32_t
9709lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9710 struct lpfc_queue *cq, uint32_t subtype)
9711{
9712 struct lpfc_mbx_mq_create *mq_create;
9713 struct lpfc_dmabuf *dmabuf;
9714 LPFC_MBOXQ_t *mbox;
9715 int rc, length, status = 0;
9716 uint32_t shdr_status, shdr_add_status;
9717 union lpfc_sli4_cfg_shdr *shdr;
9718
9719 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9720 if (!mbox)
9721 return -ENOMEM;
9722 length = (sizeof(struct lpfc_mbx_mq_create) -
9723 sizeof(struct lpfc_sli4_cfg_mhdr));
9724 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9725 LPFC_MBOX_OPCODE_MQ_CREATE,
9726 length, LPFC_SLI4_MBX_EMBED);
9727 mq_create = &mbox->u.mqe.un.mq_create;
9728 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9729 mq->page_count);
9730 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9731 cq->queue_id);
9732 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9733 switch (mq->entry_count) {
9734 default:
9735 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9736 "0362 Unsupported MQ count. (%d)\n",
9737 mq->entry_count);
9738 if (mq->entry_count < 16)
9739 return -EINVAL;
9740 /* otherwise default to smallest count (drop through) */
9741 case 16:
9742 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9743 LPFC_MQ_CNT_16);
9744 break;
9745 case 32:
9746 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9747 LPFC_MQ_CNT_32);
9748 break;
9749 case 64:
9750 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9751 LPFC_MQ_CNT_64);
9752 break;
9753 case 128:
9754 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9755 LPFC_MQ_CNT_128);
9756 break;
9757 }
9758 list_for_each_entry(dmabuf, &mq->page_list, list) {
9759 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9760 putPaddrLow(dmabuf->phys);
9761 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9762 putPaddrHigh(dmabuf->phys);
9763 }
9764 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9765 /* The IOCTL status is embedded in the mailbox subheader. */
9766 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9767 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9768 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9769 if (shdr_status || shdr_add_status || rc) {
9770 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9771 "2502 MQ_CREATE mailbox failed with "
9772 "status x%x add_status x%x, mbx status x%x\n",
9773 shdr_status, shdr_add_status, rc);
9774 status = -ENXIO;
9775 goto out;
9776 }
9777 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9778 if (mq->queue_id == 0xFFFF) {
9779 status = -ENXIO;
9780 goto out;
9781 }
9782 mq->type = LPFC_MQ;
9783 mq->subtype = subtype;
9784 mq->host_index = 0;
9785 mq->hba_index = 0;
9786
9787 /* link the mq onto the parent cq child list */
9788 list_add_tail(&mq->list, &cq->child_list);
9789out:
8fa38513 9790 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
9791 return status;
9792}
9793
4f774513
JS
9794/**
9795 * lpfc_wq_create - Create a Work Queue on the HBA
9796 * @phba: HBA structure that indicates port to create a queue on.
9797 * @wq: The queue structure to use to create the work queue.
9798 * @cq: The completion queue to bind this work queue to.
9799 * @subtype: The subtype of the work queue indicating its functionality.
9800 *
9801 * This function creates a work queue, as detailed in @wq, on a port, described
9802 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9803 *
9804 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9805 * is used to get the entry count and entry size that are necessary to
9806 * determine the number of pages to allocate and use for this queue. The @cq
9807 * is used to indicate which completion queue to bind this work queue to. This
9808 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9809 * work queue. This function is asynchronous and will wait for the mailbox
9810 * command to finish before continuing.
9811 *
9812 * On success this function will return a zero. If unable to allocate enough
9813 * memory this function will return ENOMEM. If the queue create mailbox command
9814 * fails this function will return ENXIO.
9815 **/
9816uint32_t
9817lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9818 struct lpfc_queue *cq, uint32_t subtype)
9819{
9820 struct lpfc_mbx_wq_create *wq_create;
9821 struct lpfc_dmabuf *dmabuf;
9822 LPFC_MBOXQ_t *mbox;
9823 int rc, length, status = 0;
9824 uint32_t shdr_status, shdr_add_status;
9825 union lpfc_sli4_cfg_shdr *shdr;
9826
9827 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9828 if (!mbox)
9829 return -ENOMEM;
9830 length = (sizeof(struct lpfc_mbx_wq_create) -
9831 sizeof(struct lpfc_sli4_cfg_mhdr));
9832 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9833 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9834 length, LPFC_SLI4_MBX_EMBED);
9835 wq_create = &mbox->u.mqe.un.wq_create;
9836 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9837 wq->page_count);
9838 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9839 cq->queue_id);
9840 list_for_each_entry(dmabuf, &wq->page_list, list) {
9841 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9842 putPaddrLow(dmabuf->phys);
9843 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9844 putPaddrHigh(dmabuf->phys);
9845 }
9846 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9847 /* The IOCTL status is embedded in the mailbox subheader. */
9848 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9849 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9850 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9851 if (shdr_status || shdr_add_status || rc) {
9852 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9853 "2503 WQ_CREATE mailbox failed with "
9854 "status x%x add_status x%x, mbx status x%x\n",
9855 shdr_status, shdr_add_status, rc);
9856 status = -ENXIO;
9857 goto out;
9858 }
9859 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9860 if (wq->queue_id == 0xFFFF) {
9861 status = -ENXIO;
9862 goto out;
9863 }
9864 wq->type = LPFC_WQ;
9865 wq->subtype = subtype;
9866 wq->host_index = 0;
9867 wq->hba_index = 0;
9868
9869 /* link the wq onto the parent cq child list */
9870 list_add_tail(&wq->list, &cq->child_list);
9871out:
8fa38513 9872 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9873 return status;
9874}
9875
9876/**
9877 * lpfc_rq_create - Create a Receive Queue on the HBA
9878 * @phba: HBA structure that indicates port to create a queue on.
9879 * @hrq: The queue structure to use to create the header receive queue.
9880 * @drq: The queue structure to use to create the data receive queue.
9881 * @cq: The completion queue to bind this work queue to.
9882 *
9883 * This function creates a receive buffer queue pair , as detailed in @hrq and
9884 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9885 * to the HBA.
9886 *
9887 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9888 * struct is used to get the entry count that is necessary to determine the
9889 * number of pages to use for this queue. The @cq is used to indicate which
9890 * completion queue to bind received buffers that are posted to these queues to.
9891 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9892 * receive queue pair. This function is asynchronous and will wait for the
9893 * mailbox command to finish before continuing.
9894 *
9895 * On success this function will return a zero. If unable to allocate enough
9896 * memory this function will return ENOMEM. If the queue create mailbox command
9897 * fails this function will return ENXIO.
9898 **/
9899uint32_t
9900lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9901 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9902{
9903 struct lpfc_mbx_rq_create *rq_create;
9904 struct lpfc_dmabuf *dmabuf;
9905 LPFC_MBOXQ_t *mbox;
9906 int rc, length, status = 0;
9907 uint32_t shdr_status, shdr_add_status;
9908 union lpfc_sli4_cfg_shdr *shdr;
9909
9910 if (hrq->entry_count != drq->entry_count)
9911 return -EINVAL;
9912 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9913 if (!mbox)
9914 return -ENOMEM;
9915 length = (sizeof(struct lpfc_mbx_rq_create) -
9916 sizeof(struct lpfc_sli4_cfg_mhdr));
9917 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9918 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9919 length, LPFC_SLI4_MBX_EMBED);
9920 rq_create = &mbox->u.mqe.un.rq_create;
9921 switch (hrq->entry_count) {
9922 default:
9923 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9924 "2535 Unsupported RQ count. (%d)\n",
9925 hrq->entry_count);
9926 if (hrq->entry_count < 512)
9927 return -EINVAL;
9928 /* otherwise default to smallest count (drop through) */
9929 case 512:
9930 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9931 LPFC_RQ_RING_SIZE_512);
9932 break;
9933 case 1024:
9934 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9935 LPFC_RQ_RING_SIZE_1024);
9936 break;
9937 case 2048:
9938 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9939 LPFC_RQ_RING_SIZE_2048);
9940 break;
9941 case 4096:
9942 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9943 LPFC_RQ_RING_SIZE_4096);
9944 break;
9945 }
9946 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9947 cq->queue_id);
9948 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9949 hrq->page_count);
9950 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9951 LPFC_HDR_BUF_SIZE);
9952 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9953 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9954 putPaddrLow(dmabuf->phys);
9955 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9956 putPaddrHigh(dmabuf->phys);
9957 }
9958 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9959 /* The IOCTL status is embedded in the mailbox subheader. */
9960 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9961 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9962 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9963 if (shdr_status || shdr_add_status || rc) {
9964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9965 "2504 RQ_CREATE mailbox failed with "
9966 "status x%x add_status x%x, mbx status x%x\n",
9967 shdr_status, shdr_add_status, rc);
9968 status = -ENXIO;
9969 goto out;
9970 }
9971 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9972 if (hrq->queue_id == 0xFFFF) {
9973 status = -ENXIO;
9974 goto out;
9975 }
9976 hrq->type = LPFC_HRQ;
9977 hrq->subtype = subtype;
9978 hrq->host_index = 0;
9979 hrq->hba_index = 0;
9980
9981 /* now create the data queue */
9982 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9983 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9984 length, LPFC_SLI4_MBX_EMBED);
9985 switch (drq->entry_count) {
9986 default:
9987 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9988 "2536 Unsupported RQ count. (%d)\n",
9989 drq->entry_count);
9990 if (drq->entry_count < 512)
9991 return -EINVAL;
9992 /* otherwise default to smallest count (drop through) */
9993 case 512:
9994 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9995 LPFC_RQ_RING_SIZE_512);
9996 break;
9997 case 1024:
9998 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9999 LPFC_RQ_RING_SIZE_1024);
10000 break;
10001 case 2048:
10002 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10003 LPFC_RQ_RING_SIZE_2048);
10004 break;
10005 case 4096:
10006 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
10007 LPFC_RQ_RING_SIZE_4096);
10008 break;
10009 }
10010 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
10011 cq->queue_id);
10012 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
10013 drq->page_count);
10014 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
10015 LPFC_DATA_BUF_SIZE);
10016 list_for_each_entry(dmabuf, &drq->page_list, list) {
10017 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
10018 putPaddrLow(dmabuf->phys);
10019 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
10020 putPaddrHigh(dmabuf->phys);
10021 }
10022 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10023 /* The IOCTL status is embedded in the mailbox subheader. */
10024 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
10025 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10026 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10027 if (shdr_status || shdr_add_status || rc) {
10028 status = -ENXIO;
10029 goto out;
10030 }
10031 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10032 if (drq->queue_id == 0xFFFF) {
10033 status = -ENXIO;
10034 goto out;
10035 }
10036 drq->type = LPFC_DRQ;
10037 drq->subtype = subtype;
10038 drq->host_index = 0;
10039 drq->hba_index = 0;
10040
10041 /* link the header and data RQs onto the parent cq child list */
10042 list_add_tail(&hrq->list, &cq->child_list);
10043 list_add_tail(&drq->list, &cq->child_list);
10044
10045out:
8fa38513 10046 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10047 return status;
10048}
10049
10050/**
10051 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10052 * @eq: The queue structure associated with the queue to destroy.
10053 *
10054 * This function destroys a queue, as detailed in @eq by sending an mailbox
10055 * command, specific to the type of queue, to the HBA.
10056 *
10057 * The @eq struct is used to get the queue ID of the queue to destroy.
10058 *
10059 * On success this function will return a zero. If the queue destroy mailbox
10060 * command fails this function will return ENXIO.
10061 **/
10062uint32_t
10063lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10064{
10065 LPFC_MBOXQ_t *mbox;
10066 int rc, length, status = 0;
10067 uint32_t shdr_status, shdr_add_status;
10068 union lpfc_sli4_cfg_shdr *shdr;
10069
10070 if (!eq)
10071 return -ENODEV;
10072 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10073 if (!mbox)
10074 return -ENOMEM;
10075 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10076 sizeof(struct lpfc_sli4_cfg_mhdr));
10077 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10078 LPFC_MBOX_OPCODE_EQ_DESTROY,
10079 length, LPFC_SLI4_MBX_EMBED);
10080 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10081 eq->queue_id);
10082 mbox->vport = eq->phba->pport;
10083 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10084
10085 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10086 /* The IOCTL status is embedded in the mailbox subheader. */
10087 shdr = (union lpfc_sli4_cfg_shdr *)
10088 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10089 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10090 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10091 if (shdr_status || shdr_add_status || rc) {
10092 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10093 "2505 EQ_DESTROY mailbox failed with "
10094 "status x%x add_status x%x, mbx status x%x\n",
10095 shdr_status, shdr_add_status, rc);
10096 status = -ENXIO;
10097 }
10098
10099 /* Remove eq from any list */
10100 list_del_init(&eq->list);
8fa38513 10101 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
10102 return status;
10103}
10104
10105/**
10106 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10107 * @cq: The queue structure associated with the queue to destroy.
10108 *
10109 * This function destroys a queue, as detailed in @cq by sending an mailbox
10110 * command, specific to the type of queue, to the HBA.
10111 *
10112 * The @cq struct is used to get the queue ID of the queue to destroy.
10113 *
10114 * On success this function will return a zero. If the queue destroy mailbox
10115 * command fails this function will return ENXIO.
10116 **/
10117uint32_t
10118lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10119{
10120 LPFC_MBOXQ_t *mbox;
10121 int rc, length, status = 0;
10122 uint32_t shdr_status, shdr_add_status;
10123 union lpfc_sli4_cfg_shdr *shdr;
10124
10125 if (!cq)
10126 return -ENODEV;
10127 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10128 if (!mbox)
10129 return -ENOMEM;
10130 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10131 sizeof(struct lpfc_sli4_cfg_mhdr));
10132 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10133 LPFC_MBOX_OPCODE_CQ_DESTROY,
10134 length, LPFC_SLI4_MBX_EMBED);
10135 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10136 cq->queue_id);
10137 mbox->vport = cq->phba->pport;
10138 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10139 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10140 /* The IOCTL status is embedded in the mailbox subheader. */
10141 shdr = (union lpfc_sli4_cfg_shdr *)
10142 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10143 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10144 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10145 if (shdr_status || shdr_add_status || rc) {
10146 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10147 "2506 CQ_DESTROY mailbox failed with "
10148 "status x%x add_status x%x, mbx status x%x\n",
10149 shdr_status, shdr_add_status, rc);
10150 status = -ENXIO;
10151 }
10152 /* Remove cq from any list */
10153 list_del_init(&cq->list);
8fa38513 10154 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
10155 return status;
10156}
10157
04c68496
JS
10158/**
10159 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10160 * @qm: The queue structure associated with the queue to destroy.
10161 *
10162 * This function destroys a queue, as detailed in @mq by sending an mailbox
10163 * command, specific to the type of queue, to the HBA.
10164 *
10165 * The @mq struct is used to get the queue ID of the queue to destroy.
10166 *
10167 * On success this function will return a zero. If the queue destroy mailbox
10168 * command fails this function will return ENXIO.
10169 **/
10170uint32_t
10171lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10172{
10173 LPFC_MBOXQ_t *mbox;
10174 int rc, length, status = 0;
10175 uint32_t shdr_status, shdr_add_status;
10176 union lpfc_sli4_cfg_shdr *shdr;
10177
10178 if (!mq)
10179 return -ENODEV;
10180 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10181 if (!mbox)
10182 return -ENOMEM;
10183 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10184 sizeof(struct lpfc_sli4_cfg_mhdr));
10185 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10186 LPFC_MBOX_OPCODE_MQ_DESTROY,
10187 length, LPFC_SLI4_MBX_EMBED);
10188 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10189 mq->queue_id);
10190 mbox->vport = mq->phba->pport;
10191 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10192 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10193 /* The IOCTL status is embedded in the mailbox subheader. */
10194 shdr = (union lpfc_sli4_cfg_shdr *)
10195 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10196 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10197 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10198 if (shdr_status || shdr_add_status || rc) {
10199 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10200 "2507 MQ_DESTROY mailbox failed with "
10201 "status x%x add_status x%x, mbx status x%x\n",
10202 shdr_status, shdr_add_status, rc);
10203 status = -ENXIO;
10204 }
10205 /* Remove mq from any list */
10206 list_del_init(&mq->list);
8fa38513 10207 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
10208 return status;
10209}
10210
4f774513
JS
10211/**
10212 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10213 * @wq: The queue structure associated with the queue to destroy.
10214 *
10215 * This function destroys a queue, as detailed in @wq by sending an mailbox
10216 * command, specific to the type of queue, to the HBA.
10217 *
10218 * The @wq struct is used to get the queue ID of the queue to destroy.
10219 *
10220 * On success this function will return a zero. If the queue destroy mailbox
10221 * command fails this function will return ENXIO.
10222 **/
10223uint32_t
10224lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10225{
10226 LPFC_MBOXQ_t *mbox;
10227 int rc, length, status = 0;
10228 uint32_t shdr_status, shdr_add_status;
10229 union lpfc_sli4_cfg_shdr *shdr;
10230
10231 if (!wq)
10232 return -ENODEV;
10233 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10234 if (!mbox)
10235 return -ENOMEM;
10236 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10237 sizeof(struct lpfc_sli4_cfg_mhdr));
10238 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10239 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10240 length, LPFC_SLI4_MBX_EMBED);
10241 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10242 wq->queue_id);
10243 mbox->vport = wq->phba->pport;
10244 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10245 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10246 shdr = (union lpfc_sli4_cfg_shdr *)
10247 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10248 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10249 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10250 if (shdr_status || shdr_add_status || rc) {
10251 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10252 "2508 WQ_DESTROY mailbox failed with "
10253 "status x%x add_status x%x, mbx status x%x\n",
10254 shdr_status, shdr_add_status, rc);
10255 status = -ENXIO;
10256 }
10257 /* Remove wq from any list */
10258 list_del_init(&wq->list);
8fa38513 10259 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
10260 return status;
10261}
10262
10263/**
10264 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10265 * @rq: The queue structure associated with the queue to destroy.
10266 *
10267 * This function destroys a queue, as detailed in @rq by sending an mailbox
10268 * command, specific to the type of queue, to the HBA.
10269 *
10270 * The @rq struct is used to get the queue ID of the queue to destroy.
10271 *
10272 * On success this function will return a zero. If the queue destroy mailbox
10273 * command fails this function will return ENXIO.
10274 **/
10275uint32_t
10276lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10277 struct lpfc_queue *drq)
10278{
10279 LPFC_MBOXQ_t *mbox;
10280 int rc, length, status = 0;
10281 uint32_t shdr_status, shdr_add_status;
10282 union lpfc_sli4_cfg_shdr *shdr;
10283
10284 if (!hrq || !drq)
10285 return -ENODEV;
10286 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10287 if (!mbox)
10288 return -ENOMEM;
10289 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10290 sizeof(struct mbox_header));
10291 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10292 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10293 length, LPFC_SLI4_MBX_EMBED);
10294 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10295 hrq->queue_id);
10296 mbox->vport = hrq->phba->pport;
10297 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10298 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10299 /* The IOCTL status is embedded in the mailbox subheader. */
10300 shdr = (union lpfc_sli4_cfg_shdr *)
10301 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10302 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10303 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10304 if (shdr_status || shdr_add_status || rc) {
10305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10306 "2509 RQ_DESTROY mailbox failed with "
10307 "status x%x add_status x%x, mbx status x%x\n",
10308 shdr_status, shdr_add_status, rc);
10309 if (rc != MBX_TIMEOUT)
10310 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10311 return -ENXIO;
10312 }
10313 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10314 drq->queue_id);
10315 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10316 shdr = (union lpfc_sli4_cfg_shdr *)
10317 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10318 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10319 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10320 if (shdr_status || shdr_add_status || rc) {
10321 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10322 "2510 RQ_DESTROY mailbox failed with "
10323 "status x%x add_status x%x, mbx status x%x\n",
10324 shdr_status, shdr_add_status, rc);
10325 status = -ENXIO;
10326 }
10327 list_del_init(&hrq->list);
10328 list_del_init(&drq->list);
8fa38513 10329 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
10330 return status;
10331}
10332
10333/**
10334 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10335 * @phba: The virtual port for which this call being executed.
10336 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10337 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10338 * @xritag: the xritag that ties this io to the SGL pages.
10339 *
10340 * This routine will post the sgl pages for the IO that has the xritag
10341 * that is in the iocbq structure. The xritag is assigned during iocbq
10342 * creation and persists for as long as the driver is loaded.
10343 * if the caller has fewer than 256 scatter gather segments to map then
10344 * pdma_phys_addr1 should be 0.
10345 * If the caller needs to map more than 256 scatter gather segment then
10346 * pdma_phys_addr1 should be a valid physical address.
10347 * physical address for SGLs must be 64 byte aligned.
10348 * If you are going to map 2 SGL's then the first one must have 256 entries
10349 * the second sgl can have between 1 and 256 entries.
10350 *
10351 * Return codes:
10352 * 0 - Success
10353 * -ENXIO, -ENOMEM - Failure
10354 **/
10355int
10356lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10357 dma_addr_t pdma_phys_addr0,
10358 dma_addr_t pdma_phys_addr1,
10359 uint16_t xritag)
10360{
10361 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10362 LPFC_MBOXQ_t *mbox;
10363 int rc;
10364 uint32_t shdr_status, shdr_add_status;
10365 union lpfc_sli4_cfg_shdr *shdr;
10366
10367 if (xritag == NO_XRI) {
10368 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10369 "0364 Invalid param:\n");
10370 return -EINVAL;
10371 }
10372
10373 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10374 if (!mbox)
10375 return -ENOMEM;
10376
10377 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10378 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10379 sizeof(struct lpfc_mbx_post_sgl_pages) -
10380 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10381
10382 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10383 &mbox->u.mqe.un.post_sgl_pages;
10384 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10385 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10386
10387 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10388 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10389 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10390 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10391
10392 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10393 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10394 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10395 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10396 if (!phba->sli4_hba.intr_enable)
10397 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10398 else
10399 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10400 /* The IOCTL status is embedded in the mailbox subheader. */
10401 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10402 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10403 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10404 if (rc != MBX_TIMEOUT)
10405 mempool_free(mbox, phba->mbox_mem_pool);
10406 if (shdr_status || shdr_add_status || rc) {
10407 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10408 "2511 POST_SGL mailbox failed with "
10409 "status x%x add_status x%x, mbx status x%x\n",
10410 shdr_status, shdr_add_status, rc);
10411 rc = -ENXIO;
10412 }
10413 return 0;
10414}
10415/**
10416 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10417 * @phba: The virtual port for which this call being executed.
10418 *
10419 * This routine will remove all of the sgl pages registered with the hba.
10420 *
10421 * Return codes:
10422 * 0 - Success
10423 * -ENXIO, -ENOMEM - Failure
10424 **/
10425int
10426lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10427{
10428 LPFC_MBOXQ_t *mbox;
10429 int rc;
10430 uint32_t shdr_status, shdr_add_status;
10431 union lpfc_sli4_cfg_shdr *shdr;
10432
10433 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10434 if (!mbox)
10435 return -ENOMEM;
10436
10437 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10438 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10439 LPFC_SLI4_MBX_EMBED);
10440 if (!phba->sli4_hba.intr_enable)
10441 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10442 else
10443 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10444 /* The IOCTL status is embedded in the mailbox subheader. */
10445 shdr = (union lpfc_sli4_cfg_shdr *)
10446 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10447 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10448 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10449 if (rc != MBX_TIMEOUT)
10450 mempool_free(mbox, phba->mbox_mem_pool);
10451 if (shdr_status || shdr_add_status || rc) {
10452 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10453 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10454 "status x%x add_status x%x, mbx status x%x\n",
10455 shdr_status, shdr_add_status, rc);
10456 rc = -ENXIO;
10457 }
10458 return rc;
10459}
10460
10461/**
10462 * lpfc_sli4_next_xritag - Get an xritag for the io
10463 * @phba: Pointer to HBA context object.
10464 *
10465 * This function gets an xritag for the iocb. If there is no unused xritag
10466 * it will return 0xffff.
10467 * The function returns the allocated xritag if successful, else returns zero.
10468 * Zero is not a valid xritag.
10469 * The caller is not required to hold any lock.
10470 **/
10471uint16_t
10472lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10473{
10474 uint16_t xritag;
10475
10476 spin_lock_irq(&phba->hbalock);
10477 xritag = phba->sli4_hba.next_xri;
10478 if ((xritag != (uint16_t) -1) && xritag <
10479 (phba->sli4_hba.max_cfg_param.max_xri
10480 + phba->sli4_hba.max_cfg_param.xri_base)) {
10481 phba->sli4_hba.next_xri++;
10482 phba->sli4_hba.max_cfg_param.xri_used++;
10483 spin_unlock_irq(&phba->hbalock);
10484 return xritag;
10485 }
10486 spin_unlock_irq(&phba->hbalock);
6a9c52cf 10487 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4f774513
JS
10488 "2004 Failed to allocate XRI.last XRITAG is %d"
10489 " Max XRI is %d, Used XRI is %d\n",
10490 phba->sli4_hba.next_xri,
10491 phba->sli4_hba.max_cfg_param.max_xri,
10492 phba->sli4_hba.max_cfg_param.xri_used);
10493 return -1;
10494}
10495
10496/**
10497 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10498 * @phba: pointer to lpfc hba data structure.
10499 *
10500 * This routine is invoked to post a block of driver's sgl pages to the
10501 * HBA using non-embedded mailbox command. No Lock is held. This routine
10502 * is only called when the driver is loading and after all IO has been
10503 * stopped.
10504 **/
10505int
10506lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10507{
10508 struct lpfc_sglq *sglq_entry;
10509 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10510 struct sgl_page_pairs *sgl_pg_pairs;
10511 void *viraddr;
10512 LPFC_MBOXQ_t *mbox;
10513 uint32_t reqlen, alloclen, pg_pairs;
10514 uint32_t mbox_tmo;
10515 uint16_t xritag_start = 0;
10516 int els_xri_cnt, rc = 0;
10517 uint32_t shdr_status, shdr_add_status;
10518 union lpfc_sli4_cfg_shdr *shdr;
10519
10520 /* The number of sgls to be posted */
10521 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10522
10523 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10524 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10525 if (reqlen > PAGE_SIZE) {
10526 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10527 "2559 Block sgl registration required DMA "
10528 "size (%d) great than a page\n", reqlen);
10529 return -ENOMEM;
10530 }
10531 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10532 if (!mbox) {
10533 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10534 "2560 Failed to allocate mbox cmd memory\n");
10535 return -ENOMEM;
10536 }
10537
10538 /* Allocate DMA memory and set up the non-embedded mailbox command */
10539 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10540 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10541 LPFC_SLI4_MBX_NEMBED);
10542
10543 if (alloclen < reqlen) {
10544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10545 "0285 Allocated DMA memory size (%d) is "
10546 "less than the requested DMA memory "
10547 "size (%d)\n", alloclen, reqlen);
10548 lpfc_sli4_mbox_cmd_free(phba, mbox);
10549 return -ENOMEM;
10550 }
4f774513 10551 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10552 viraddr = mbox->sge_array->addr[0];
10553
10554 /* Set up the SGL pages in the non-embedded DMA pages */
10555 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10556 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10557
10558 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10559 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10560 /* Set up the sge entry */
10561 sgl_pg_pairs->sgl_pg0_addr_lo =
10562 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10563 sgl_pg_pairs->sgl_pg0_addr_hi =
10564 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10565 sgl_pg_pairs->sgl_pg1_addr_lo =
10566 cpu_to_le32(putPaddrLow(0));
10567 sgl_pg_pairs->sgl_pg1_addr_hi =
10568 cpu_to_le32(putPaddrHigh(0));
10569 /* Keep the first xritag on the list */
10570 if (pg_pairs == 0)
10571 xritag_start = sglq_entry->sli4_xritag;
10572 sgl_pg_pairs++;
10573 }
10574 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 10575 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513
JS
10576 /* Perform endian conversion if necessary */
10577 sgl->word0 = cpu_to_le32(sgl->word0);
10578
10579 if (!phba->sli4_hba.intr_enable)
10580 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10581 else {
10582 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10583 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10584 }
10585 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10586 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10587 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10588 if (rc != MBX_TIMEOUT)
10589 lpfc_sli4_mbox_cmd_free(phba, mbox);
10590 if (shdr_status || shdr_add_status || rc) {
10591 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10592 "2513 POST_SGL_BLOCK mailbox command failed "
10593 "status x%x add_status x%x mbx status x%x\n",
10594 shdr_status, shdr_add_status, rc);
10595 rc = -ENXIO;
10596 }
10597 return rc;
10598}
10599
10600/**
10601 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10602 * @phba: pointer to lpfc hba data structure.
10603 * @sblist: pointer to scsi buffer list.
10604 * @count: number of scsi buffers on the list.
10605 *
10606 * This routine is invoked to post a block of @count scsi sgl pages from a
10607 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10608 * No Lock is held.
10609 *
10610 **/
10611int
10612lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10613 int cnt)
10614{
10615 struct lpfc_scsi_buf *psb;
10616 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10617 struct sgl_page_pairs *sgl_pg_pairs;
10618 void *viraddr;
10619 LPFC_MBOXQ_t *mbox;
10620 uint32_t reqlen, alloclen, pg_pairs;
10621 uint32_t mbox_tmo;
10622 uint16_t xritag_start = 0;
10623 int rc = 0;
10624 uint32_t shdr_status, shdr_add_status;
10625 dma_addr_t pdma_phys_bpl1;
10626 union lpfc_sli4_cfg_shdr *shdr;
10627
10628 /* Calculate the requested length of the dma memory */
10629 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10630 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10631 if (reqlen > PAGE_SIZE) {
10632 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10633 "0217 Block sgl registration required DMA "
10634 "size (%d) great than a page\n", reqlen);
10635 return -ENOMEM;
10636 }
10637 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10638 if (!mbox) {
10639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10640 "0283 Failed to allocate mbox cmd memory\n");
10641 return -ENOMEM;
10642 }
10643
10644 /* Allocate DMA memory and set up the non-embedded mailbox command */
10645 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10646 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10647 LPFC_SLI4_MBX_NEMBED);
10648
10649 if (alloclen < reqlen) {
10650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10651 "2561 Allocated DMA memory size (%d) is "
10652 "less than the requested DMA memory "
10653 "size (%d)\n", alloclen, reqlen);
10654 lpfc_sli4_mbox_cmd_free(phba, mbox);
10655 return -ENOMEM;
10656 }
4f774513 10657 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10658 viraddr = mbox->sge_array->addr[0];
10659
10660 /* Set up the SGL pages in the non-embedded DMA pages */
10661 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10662 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10663
10664 pg_pairs = 0;
10665 list_for_each_entry(psb, sblist, list) {
10666 /* Set up the sge entry */
10667 sgl_pg_pairs->sgl_pg0_addr_lo =
10668 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10669 sgl_pg_pairs->sgl_pg0_addr_hi =
10670 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10671 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10672 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10673 else
10674 pdma_phys_bpl1 = 0;
10675 sgl_pg_pairs->sgl_pg1_addr_lo =
10676 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10677 sgl_pg_pairs->sgl_pg1_addr_hi =
10678 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10679 /* Keep the first xritag on the list */
10680 if (pg_pairs == 0)
10681 xritag_start = psb->cur_iocbq.sli4_xritag;
10682 sgl_pg_pairs++;
10683 pg_pairs++;
10684 }
10685 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10686 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10687 /* Perform endian conversion if necessary */
10688 sgl->word0 = cpu_to_le32(sgl->word0);
10689
10690 if (!phba->sli4_hba.intr_enable)
10691 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10692 else {
10693 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10694 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10695 }
10696 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10697 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10698 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10699 if (rc != MBX_TIMEOUT)
10700 lpfc_sli4_mbox_cmd_free(phba, mbox);
10701 if (shdr_status || shdr_add_status || rc) {
10702 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10703 "2564 POST_SGL_BLOCK mailbox command failed "
10704 "status x%x add_status x%x mbx status x%x\n",
10705 shdr_status, shdr_add_status, rc);
10706 rc = -ENXIO;
10707 }
10708 return rc;
10709}
10710
10711/**
10712 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10713 * @phba: pointer to lpfc_hba struct that the frame was received on
10714 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10715 *
10716 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10717 * valid type of frame that the LPFC driver will handle. This function will
10718 * return a zero if the frame is a valid frame or a non zero value when the
10719 * frame does not pass the check.
10720 **/
10721static int
10722lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10723{
10724 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10725 char *type_names[] = FC_TYPE_NAMES_INIT;
10726 struct fc_vft_header *fc_vft_hdr;
10727
10728 switch (fc_hdr->fh_r_ctl) {
10729 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10730 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10731 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10732 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10733 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10734 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10735 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10736 case FC_RCTL_DD_CMD_STATUS: /* command status */
10737 case FC_RCTL_ELS_REQ: /* extended link services request */
10738 case FC_RCTL_ELS_REP: /* extended link services reply */
10739 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10740 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10741 case FC_RCTL_BA_NOP: /* basic link service NOP */
10742 case FC_RCTL_BA_ABTS: /* basic link service abort */
10743 case FC_RCTL_BA_RMC: /* remove connection */
10744 case FC_RCTL_BA_ACC: /* basic accept */
10745 case FC_RCTL_BA_RJT: /* basic reject */
10746 case FC_RCTL_BA_PRMT:
10747 case FC_RCTL_ACK_1: /* acknowledge_1 */
10748 case FC_RCTL_ACK_0: /* acknowledge_0 */
10749 case FC_RCTL_P_RJT: /* port reject */
10750 case FC_RCTL_F_RJT: /* fabric reject */
10751 case FC_RCTL_P_BSY: /* port busy */
10752 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10753 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10754 case FC_RCTL_LCR: /* link credit reset */
10755 case FC_RCTL_END: /* end */
10756 break;
10757 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10758 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10759 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10760 return lpfc_fc_frame_check(phba, fc_hdr);
10761 default:
10762 goto drop;
10763 }
10764 switch (fc_hdr->fh_type) {
10765 case FC_TYPE_BLS:
10766 case FC_TYPE_ELS:
10767 case FC_TYPE_FCP:
10768 case FC_TYPE_CT:
10769 break;
10770 case FC_TYPE_IP:
10771 case FC_TYPE_ILS:
10772 default:
10773 goto drop;
10774 }
10775 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10776 "2538 Received frame rctl:%s type:%s\n",
10777 rctl_names[fc_hdr->fh_r_ctl],
10778 type_names[fc_hdr->fh_type]);
10779 return 0;
10780drop:
10781 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10782 "2539 Dropped frame rctl:%s type:%s\n",
10783 rctl_names[fc_hdr->fh_r_ctl],
10784 type_names[fc_hdr->fh_type]);
10785 return 1;
10786}
10787
10788/**
10789 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10790 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10791 *
10792 * This function processes the FC header to retrieve the VFI from the VF
10793 * header, if one exists. This function will return the VFI if one exists
10794 * or 0 if no VSAN Header exists.
10795 **/
10796static uint32_t
10797lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10798{
10799 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10800
10801 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10802 return 0;
10803 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10804}
10805
10806/**
10807 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10808 * @phba: Pointer to the HBA structure to search for the vport on
10809 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10810 * @fcfi: The FC Fabric ID that the frame came from
10811 *
10812 * This function searches the @phba for a vport that matches the content of the
10813 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10814 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10815 * returns the matching vport pointer or NULL if unable to match frame to a
10816 * vport.
10817 **/
10818static struct lpfc_vport *
10819lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10820 uint16_t fcfi)
10821{
10822 struct lpfc_vport **vports;
10823 struct lpfc_vport *vport = NULL;
10824 int i;
10825 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10826 fc_hdr->fh_d_id[1] << 8 |
10827 fc_hdr->fh_d_id[2]);
10828
10829 vports = lpfc_create_vport_work_array(phba);
10830 if (vports != NULL)
10831 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10832 if (phba->fcf.fcfi == fcfi &&
10833 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10834 vports[i]->fc_myDID == did) {
10835 vport = vports[i];
10836 break;
10837 }
10838 }
10839 lpfc_destroy_vport_work_array(phba, vports);
10840 return vport;
10841}
10842
10843/**
10844 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10845 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10846 *
10847 * This function searches through the existing incomplete sequences that have
10848 * been sent to this @vport. If the frame matches one of the incomplete
10849 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10850 * make up that sequence. If no sequence is found that matches this frame then
10851 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10852 * This function returns a pointer to the first dmabuf in the sequence list that
10853 * the frame was linked to.
10854 **/
10855static struct hbq_dmabuf *
10856lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10857{
10858 struct fc_frame_header *new_hdr;
10859 struct fc_frame_header *temp_hdr;
10860 struct lpfc_dmabuf *d_buf;
10861 struct lpfc_dmabuf *h_buf;
10862 struct hbq_dmabuf *seq_dmabuf = NULL;
10863 struct hbq_dmabuf *temp_dmabuf = NULL;
10864
4d9ab994 10865 INIT_LIST_HEAD(&dmabuf->dbuf.list);
4f774513
JS
10866 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10867 /* Use the hdr_buf to find the sequence that this frame belongs to */
10868 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10869 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10870 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10871 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10872 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10873 continue;
10874 /* found a pending sequence that matches this frame */
10875 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10876 break;
10877 }
10878 if (!seq_dmabuf) {
10879 /*
10880 * This indicates first frame received for this sequence.
10881 * Queue the buffer on the vport's rcv_buffer_list.
10882 */
10883 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10884 return dmabuf;
10885 }
10886 temp_hdr = seq_dmabuf->hbuf.virt;
10887 if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
4d9ab994
JS
10888 list_del_init(&seq_dmabuf->hbuf.list);
10889 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10890 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
4f774513
JS
10891 return dmabuf;
10892 }
10893 /* find the correct place in the sequence to insert this frame */
10894 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10895 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10896 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10897 /*
10898 * If the frame's sequence count is greater than the frame on
10899 * the list then insert the frame right after this frame
10900 */
10901 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10902 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10903 return seq_dmabuf;
10904 }
10905 }
10906 return NULL;
10907}
10908
6669f9bb
JS
10909/**
10910 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
10911 * @vport: pointer to a vitural port
10912 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10913 *
10914 * This function tries to abort from the partially assembed sequence, described
10915 * by the information from basic abbort @dmabuf. It checks to see whether such
10916 * partially assembled sequence held by the driver. If so, it shall free up all
10917 * the frames from the partially assembled sequence.
10918 *
10919 * Return
10920 * true -- if there is matching partially assembled sequence present and all
10921 * the frames freed with the sequence;
10922 * false -- if there is no matching partially assembled sequence present so
10923 * nothing got aborted in the lower layer driver
10924 **/
10925static bool
10926lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
10927 struct hbq_dmabuf *dmabuf)
10928{
10929 struct fc_frame_header *new_hdr;
10930 struct fc_frame_header *temp_hdr;
10931 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
10932 struct hbq_dmabuf *seq_dmabuf = NULL;
10933
10934 /* Use the hdr_buf to find the sequence that matches this frame */
10935 INIT_LIST_HEAD(&dmabuf->dbuf.list);
10936 INIT_LIST_HEAD(&dmabuf->hbuf.list);
10937 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10938 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10939 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10940 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10941 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10942 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10943 continue;
10944 /* found a pending sequence that matches this frame */
10945 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10946 break;
10947 }
10948
10949 /* Free up all the frames from the partially assembled sequence */
10950 if (seq_dmabuf) {
10951 list_for_each_entry_safe(d_buf, n_buf,
10952 &seq_dmabuf->dbuf.list, list) {
10953 list_del_init(&d_buf->list);
10954 lpfc_in_buf_free(vport->phba, d_buf);
10955 }
10956 return true;
10957 }
10958 return false;
10959}
10960
10961/**
10962 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
10963 * @phba: Pointer to HBA context object.
10964 * @cmd_iocbq: pointer to the command iocbq structure.
10965 * @rsp_iocbq: pointer to the response iocbq structure.
10966 *
10967 * This function handles the sequence abort accept iocb command complete
10968 * event. It properly releases the memory allocated to the sequence abort
10969 * accept iocb.
10970 **/
10971static void
10972lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
10973 struct lpfc_iocbq *cmd_iocbq,
10974 struct lpfc_iocbq *rsp_iocbq)
10975{
10976 if (cmd_iocbq)
10977 lpfc_sli_release_iocbq(phba, cmd_iocbq);
10978}
10979
10980/**
10981 * lpfc_sli4_seq_abort_acc - Accept sequence abort
10982 * @phba: Pointer to HBA context object.
10983 * @fc_hdr: pointer to a FC frame header.
10984 *
10985 * This function sends a basic accept to a previous unsol sequence abort
10986 * event after aborting the sequence handling.
10987 **/
10988static void
10989lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
10990 struct fc_frame_header *fc_hdr)
10991{
10992 struct lpfc_iocbq *ctiocb = NULL;
10993 struct lpfc_nodelist *ndlp;
10994 uint16_t oxid;
10995 uint32_t sid;
10996 IOCB_t *icmd;
10997
10998 if (!lpfc_is_link_up(phba))
10999 return;
11000
11001 sid = sli4_sid_from_fc_hdr(fc_hdr);
11002 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
11003
11004 ndlp = lpfc_findnode_did(phba->pport, sid);
11005 if (!ndlp) {
11006 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
11007 "1268 Find ndlp returned NULL for oxid:x%x "
11008 "SID:x%x\n", oxid, sid);
11009 return;
11010 }
11011
11012 /* Allocate buffer for acc iocb */
11013 ctiocb = lpfc_sli_get_iocbq(phba);
11014 if (!ctiocb)
11015 return;
11016
11017 icmd = &ctiocb->iocb;
11018 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11019 icmd->un.xseq64.bdl.bdeSize = 0;
11020 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11021 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11022 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11023
11024 /* Fill in the rest of iocb fields */
11025 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11026 icmd->ulpBdeCount = 0;
11027 icmd->ulpLe = 1;
11028 icmd->ulpClass = CLASS3;
11029 icmd->ulpContext = ndlp->nlp_rpi;
11030 icmd->un.ulpWord[3] = oxid;
11031
11032 ctiocb->sli4_xritag = NO_XRI;
11033 ctiocb->iocb_cmpl = NULL;
11034 ctiocb->vport = phba->pport;
11035 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11036
11037 /* Xmit CT abts accept on exchange <xid> */
11038 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11039 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11040 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11041 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11042}
11043
11044/**
11045 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11046 * @vport: Pointer to the vport on which this sequence was received
11047 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11048 *
11049 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11050 * receive sequence is only partially assembed by the driver, it shall abort
11051 * the partially assembled frames for the sequence. Otherwise, if the
11052 * unsolicited receive sequence has been completely assembled and passed to
11053 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11054 * unsolicited sequence has been aborted. After that, it will issue a basic
11055 * accept to accept the abort.
11056 **/
11057void
11058lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11059 struct hbq_dmabuf *dmabuf)
11060{
11061 struct lpfc_hba *phba = vport->phba;
11062 struct fc_frame_header fc_hdr;
11063 bool abts_par;
11064
11065 /* Try to abort partially assembled seq */
11066 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11067
11068 /* Make a copy of fc_hdr before the dmabuf being released */
11069 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11070
11071 /* Send abort to ULP if partially seq abort failed */
11072 if (abts_par == false)
11073 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11074 else
11075 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11076 /* Send basic accept (BA_ACC) to the abort requester */
11077 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11078}
11079
4f774513
JS
11080/**
11081 * lpfc_seq_complete - Indicates if a sequence is complete
11082 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11083 *
11084 * This function checks the sequence, starting with the frame described by
11085 * @dmabuf, to see if all the frames associated with this sequence are present.
11086 * the frames associated with this sequence are linked to the @dmabuf using the
11087 * dbuf list. This function looks for two major things. 1) That the first frame
11088 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11089 * set. 3) That there are no holes in the sequence count. The function will
11090 * return 1 when the sequence is complete, otherwise it will return 0.
11091 **/
11092static int
11093lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11094{
11095 struct fc_frame_header *hdr;
11096 struct lpfc_dmabuf *d_buf;
11097 struct hbq_dmabuf *seq_dmabuf;
11098 uint32_t fctl;
11099 int seq_count = 0;
11100
11101 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11102 /* make sure first fame of sequence has a sequence count of zero */
11103 if (hdr->fh_seq_cnt != seq_count)
11104 return 0;
11105 fctl = (hdr->fh_f_ctl[0] << 16 |
11106 hdr->fh_f_ctl[1] << 8 |
11107 hdr->fh_f_ctl[2]);
11108 /* If last frame of sequence we can return success. */
11109 if (fctl & FC_FC_END_SEQ)
11110 return 1;
11111 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11112 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11113 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11114 /* If there is a hole in the sequence count then fail. */
11115 if (++seq_count != hdr->fh_seq_cnt)
11116 return 0;
11117 fctl = (hdr->fh_f_ctl[0] << 16 |
11118 hdr->fh_f_ctl[1] << 8 |
11119 hdr->fh_f_ctl[2]);
11120 /* If last frame of sequence we can return success. */
11121 if (fctl & FC_FC_END_SEQ)
11122 return 1;
11123 }
11124 return 0;
11125}
11126
11127/**
11128 * lpfc_prep_seq - Prep sequence for ULP processing
11129 * @vport: Pointer to the vport on which this sequence was received
11130 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11131 *
11132 * This function takes a sequence, described by a list of frames, and creates
11133 * a list of iocbq structures to describe the sequence. This iocbq list will be
11134 * used to issue to the generic unsolicited sequence handler. This routine
11135 * returns a pointer to the first iocbq in the list. If the function is unable
11136 * to allocate an iocbq then it throw out the received frames that were not
11137 * able to be described and return a pointer to the first iocbq. If unable to
11138 * allocate any iocbqs (including the first) this function will return NULL.
11139 **/
11140static struct lpfc_iocbq *
11141lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11142{
11143 struct lpfc_dmabuf *d_buf, *n_buf;
11144 struct lpfc_iocbq *first_iocbq, *iocbq;
11145 struct fc_frame_header *fc_hdr;
11146 uint32_t sid;
11147
11148 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11149 /* remove from receive buffer list */
11150 list_del_init(&seq_dmabuf->hbuf.list);
11151 /* get the Remote Port's SID */
6669f9bb 11152 sid = sli4_sid_from_fc_hdr(fc_hdr);
4f774513
JS
11153 /* Get an iocbq struct to fill in. */
11154 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11155 if (first_iocbq) {
11156 /* Initialize the first IOCB. */
8fa38513 11157 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
11158 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11159 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11160 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11161 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11162 vport->vpi + vport->phba->vpi_base;
11163 /* put the first buffer into the first IOCBq */
11164 first_iocbq->context2 = &seq_dmabuf->dbuf;
11165 first_iocbq->context3 = NULL;
11166 first_iocbq->iocb.ulpBdeCount = 1;
11167 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11168 LPFC_DATA_BUF_SIZE;
11169 first_iocbq->iocb.un.rcvels.remoteID = sid;
8fa38513 11170 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11171 bf_get(lpfc_rcqe_length,
11172 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11173 }
11174 iocbq = first_iocbq;
11175 /*
11176 * Each IOCBq can have two Buffers assigned, so go through the list
11177 * of buffers for this sequence and save two buffers in each IOCBq
11178 */
11179 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11180 if (!iocbq) {
11181 lpfc_in_buf_free(vport->phba, d_buf);
11182 continue;
11183 }
11184 if (!iocbq->context3) {
11185 iocbq->context3 = d_buf;
11186 iocbq->iocb.ulpBdeCount++;
11187 iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
11188 LPFC_DATA_BUF_SIZE;
8fa38513 11189 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11190 bf_get(lpfc_rcqe_length,
11191 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11192 } else {
11193 iocbq = lpfc_sli_get_iocbq(vport->phba);
11194 if (!iocbq) {
11195 if (first_iocbq) {
11196 first_iocbq->iocb.ulpStatus =
11197 IOSTAT_FCP_RSP_ERROR;
11198 first_iocbq->iocb.un.ulpWord[4] =
11199 IOERR_NO_RESOURCES;
11200 }
11201 lpfc_in_buf_free(vport->phba, d_buf);
11202 continue;
11203 }
11204 iocbq->context2 = d_buf;
11205 iocbq->context3 = NULL;
11206 iocbq->iocb.ulpBdeCount = 1;
11207 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11208 LPFC_DATA_BUF_SIZE;
8fa38513 11209 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11210 bf_get(lpfc_rcqe_length,
11211 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11212 iocbq->iocb.un.rcvels.remoteID = sid;
11213 list_add_tail(&iocbq->list, &first_iocbq->list);
11214 }
11215 }
11216 return first_iocbq;
11217}
11218
6669f9bb
JS
11219static void
11220lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11221 struct hbq_dmabuf *seq_dmabuf)
11222{
11223 struct fc_frame_header *fc_hdr;
11224 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11225 struct lpfc_hba *phba = vport->phba;
11226
11227 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11228 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11229 if (!iocbq) {
11230 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11231 "2707 Ring %d handler: Failed to allocate "
11232 "iocb Rctl x%x Type x%x received\n",
11233 LPFC_ELS_RING,
11234 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11235 return;
11236 }
11237 if (!lpfc_complete_unsol_iocb(phba,
11238 &phba->sli.ring[LPFC_ELS_RING],
11239 iocbq, fc_hdr->fh_r_ctl,
11240 fc_hdr->fh_type))
11241 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11242 "2540 Ring %d handler: unexpected Rctl "
11243 "x%x Type x%x received\n",
11244 LPFC_ELS_RING,
11245 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11246
11247 /* Free iocb created in lpfc_prep_seq */
11248 list_for_each_entry_safe(curr_iocb, next_iocb,
11249 &iocbq->list, list) {
11250 list_del_init(&curr_iocb->list);
11251 lpfc_sli_release_iocbq(phba, curr_iocb);
11252 }
11253 lpfc_sli_release_iocbq(phba, iocbq);
11254}
11255
4f774513
JS
11256/**
11257 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11258 * @phba: Pointer to HBA context object.
11259 *
11260 * This function is called with no lock held. This function processes all
11261 * the received buffers and gives it to upper layers when a received buffer
11262 * indicates that it is the final frame in the sequence. The interrupt
11263 * service routine processes received buffers at interrupt contexts and adds
11264 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11265 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11266 * appropriate receive function when the final frame in a sequence is received.
11267 **/
4d9ab994
JS
11268void
11269lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11270 struct hbq_dmabuf *dmabuf)
4f774513 11271{
4d9ab994 11272 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
11273 struct fc_frame_header *fc_hdr;
11274 struct lpfc_vport *vport;
11275 uint32_t fcfi;
4f774513
JS
11276
11277 /* Clear hba flag and get all received buffers into the cmplq */
11278 spin_lock_irq(&phba->hbalock);
11279 phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
4f774513
JS
11280 spin_unlock_irq(&phba->hbalock);
11281
11282 /* Process each received buffer */
4d9ab994
JS
11283 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11284 /* check to see if this a valid type of frame */
11285 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11286 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11287 return;
11288 }
11289 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11290 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11291 if (!vport) {
11292 /* throw out the frame */
11293 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11294 return;
11295 }
6669f9bb
JS
11296 /* Handle the basic abort sequence (BA_ABTS) event */
11297 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11298 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11299 return;
11300 }
11301
4d9ab994
JS
11302 /* Link this frame */
11303 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11304 if (!seq_dmabuf) {
11305 /* unable to add frame to vport - throw it out */
11306 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11307 return;
11308 }
11309 /* If not last frame in sequence continue processing frames. */
11310 if (!lpfc_seq_complete(seq_dmabuf)) {
11311 /*
11312 * When saving off frames post a new one and mark this
11313 * frame to be freed when it is finished.
11314 **/
11315 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11316 dmabuf->tag = -1;
11317 return;
11318 }
6669f9bb
JS
11319 /* Send the complete sequence to the upper layer protocol */
11320 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 11321}
6fb120a7
JS
11322
11323/**
11324 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11325 * @phba: pointer to lpfc hba data structure.
11326 *
11327 * This routine is invoked to post rpi header templates to the
11328 * HBA consistent with the SLI-4 interface spec. This routine
11329 * posts a PAGE_SIZE memory region to the port to hold up to
11330 * PAGE_SIZE modulo 64 rpi context headers.
11331 *
11332 * This routine does not require any locks. It's usage is expected
11333 * to be driver load or reset recovery when the driver is
11334 * sequential.
11335 *
11336 * Return codes
11337 * 0 - sucessful
11338 * EIO - The mailbox failed to complete successfully.
11339 * When this error occurs, the driver is not guaranteed
11340 * to have any rpi regions posted to the device and
11341 * must either attempt to repost the regions or take a
11342 * fatal error.
11343 **/
11344int
11345lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11346{
11347 struct lpfc_rpi_hdr *rpi_page;
11348 uint32_t rc = 0;
11349
11350 /* Post all rpi memory regions to the port. */
11351 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11352 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11353 if (rc != MBX_SUCCESS) {
11354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11355 "2008 Error %d posting all rpi "
11356 "headers\n", rc);
11357 rc = -EIO;
11358 break;
11359 }
11360 }
11361
11362 return rc;
11363}
11364
11365/**
11366 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11367 * @phba: pointer to lpfc hba data structure.
11368 * @rpi_page: pointer to the rpi memory region.
11369 *
11370 * This routine is invoked to post a single rpi header to the
11371 * HBA consistent with the SLI-4 interface spec. This memory region
11372 * maps up to 64 rpi context regions.
11373 *
11374 * Return codes
11375 * 0 - sucessful
11376 * ENOMEM - No available memory
11377 * EIO - The mailbox failed to complete successfully.
11378 **/
11379int
11380lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11381{
11382 LPFC_MBOXQ_t *mboxq;
11383 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11384 uint32_t rc = 0;
11385 uint32_t mbox_tmo;
11386 uint32_t shdr_status, shdr_add_status;
11387 union lpfc_sli4_cfg_shdr *shdr;
11388
11389 /* The port is notified of the header region via a mailbox command. */
11390 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11391 if (!mboxq) {
11392 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11393 "2001 Unable to allocate memory for issuing "
11394 "SLI_CONFIG_SPECIAL mailbox command\n");
11395 return -ENOMEM;
11396 }
11397
11398 /* Post all rpi memory regions to the port. */
11399 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11400 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11401 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11402 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11403 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11404 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11405 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11406 hdr_tmpl, rpi_page->page_count);
11407 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11408 rpi_page->start_rpi);
11409 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11410 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 11411 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
11412 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11413 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11414 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11415 if (rc != MBX_TIMEOUT)
11416 mempool_free(mboxq, phba->mbox_mem_pool);
11417 if (shdr_status || shdr_add_status || rc) {
11418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11419 "2514 POST_RPI_HDR mailbox failed with "
11420 "status x%x add_status x%x, mbx status x%x\n",
11421 shdr_status, shdr_add_status, rc);
11422 rc = -ENXIO;
11423 }
11424 return rc;
11425}
11426
11427/**
11428 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11429 * @phba: pointer to lpfc hba data structure.
11430 *
11431 * This routine is invoked to post rpi header templates to the
11432 * HBA consistent with the SLI-4 interface spec. This routine
11433 * posts a PAGE_SIZE memory region to the port to hold up to
11434 * PAGE_SIZE modulo 64 rpi context headers.
11435 *
11436 * Returns
11437 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
11438 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11439 **/
11440int
11441lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11442{
11443 int rpi;
11444 uint16_t max_rpi, rpi_base, rpi_limit;
11445 uint16_t rpi_remaining;
11446 struct lpfc_rpi_hdr *rpi_hdr;
11447
11448 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11449 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11450 rpi_limit = phba->sli4_hba.next_rpi;
11451
11452 /*
11453 * The valid rpi range is not guaranteed to be zero-based. Start
11454 * the search at the rpi_base as reported by the port.
11455 */
11456 spin_lock_irq(&phba->hbalock);
11457 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11458 if (rpi >= rpi_limit || rpi < rpi_base)
11459 rpi = LPFC_RPI_ALLOC_ERROR;
11460 else {
11461 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11462 phba->sli4_hba.max_cfg_param.rpi_used++;
11463 phba->sli4_hba.rpi_count++;
11464 }
11465
11466 /*
11467 * Don't try to allocate more rpi header regions if the device limit
11468 * on available rpis max has been exhausted.
11469 */
11470 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11471 (phba->sli4_hba.rpi_count >= max_rpi)) {
11472 spin_unlock_irq(&phba->hbalock);
11473 return rpi;
11474 }
11475
11476 /*
11477 * If the driver is running low on rpi resources, allocate another
11478 * page now. Note that the next_rpi value is used because
11479 * it represents how many are actually in use whereas max_rpi notes
11480 * how many are supported max by the device.
11481 */
11482 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11483 phba->sli4_hba.rpi_count;
11484 spin_unlock_irq(&phba->hbalock);
11485 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11486 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11487 if (!rpi_hdr) {
11488 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11489 "2002 Error Could not grow rpi "
11490 "count\n");
11491 } else {
11492 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11493 }
11494 }
11495
11496 return rpi;
11497}
11498
11499/**
11500 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11501 * @phba: pointer to lpfc hba data structure.
11502 *
11503 * This routine is invoked to release an rpi to the pool of
11504 * available rpis maintained by the driver.
11505 **/
11506void
11507lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11508{
11509 spin_lock_irq(&phba->hbalock);
11510 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11511 phba->sli4_hba.rpi_count--;
11512 phba->sli4_hba.max_cfg_param.rpi_used--;
11513 spin_unlock_irq(&phba->hbalock);
11514}
11515
11516/**
11517 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11518 * @phba: pointer to lpfc hba data structure.
11519 *
11520 * This routine is invoked to remove the memory region that
11521 * provided rpi via a bitmask.
11522 **/
11523void
11524lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11525{
11526 kfree(phba->sli4_hba.rpi_bmask);
11527}
11528
11529/**
11530 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11531 * @phba: pointer to lpfc hba data structure.
11532 *
11533 * This routine is invoked to remove the memory region that
11534 * provided rpi via a bitmask.
11535 **/
11536int
11537lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11538{
11539 LPFC_MBOXQ_t *mboxq;
11540 struct lpfc_hba *phba = ndlp->phba;
11541 int rc;
11542
11543 /* The port is notified of the header region via a mailbox command. */
11544 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11545 if (!mboxq)
11546 return -ENOMEM;
11547
11548 /* Post all rpi memory regions to the port. */
11549 lpfc_resume_rpi(mboxq, ndlp);
11550 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11551 if (rc == MBX_NOT_FINISHED) {
11552 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11553 "2010 Resume RPI Mailbox failed "
11554 "status %d, mbxStatus x%x\n", rc,
11555 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11556 mempool_free(mboxq, phba->mbox_mem_pool);
11557 return -EIO;
11558 }
11559 return 0;
11560}
11561
11562/**
11563 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11564 * @phba: pointer to lpfc hba data structure.
11565 * @vpi: vpi value to activate with the port.
11566 *
11567 * This routine is invoked to activate a vpi with the
11568 * port when the host intends to use vports with a
11569 * nonzero vpi.
11570 *
11571 * Returns:
11572 * 0 success
11573 * -Evalue otherwise
11574 **/
11575int
11576lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11577{
11578 LPFC_MBOXQ_t *mboxq;
11579 int rc = 0;
6a9c52cf 11580 int retval = MBX_SUCCESS;
6fb120a7
JS
11581 uint32_t mbox_tmo;
11582
11583 if (vpi == 0)
11584 return -EINVAL;
11585 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11586 if (!mboxq)
11587 return -ENOMEM;
1c6834a7 11588 lpfc_init_vpi(phba, mboxq, vpi);
6fb120a7
JS
11589 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11590 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7
JS
11591 if (rc != MBX_SUCCESS) {
11592 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11593 "2022 INIT VPI Mailbox failed "
11594 "status %d, mbxStatus x%x\n", rc,
11595 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 11596 retval = -EIO;
6fb120a7 11597 }
6a9c52cf
JS
11598 if (rc != MBX_TIMEOUT)
11599 mempool_free(mboxq, phba->mbox_mem_pool);
11600
11601 return retval;
6fb120a7
JS
11602}
11603
11604/**
11605 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11606 * @phba: pointer to lpfc hba data structure.
11607 * @mboxq: Pointer to mailbox object.
11608 *
11609 * This routine is invoked to manually add a single FCF record. The caller
11610 * must pass a completely initialized FCF_Record. This routine takes
11611 * care of the nonembedded mailbox operations.
11612 **/
11613static void
11614lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11615{
11616 void *virt_addr;
11617 union lpfc_sli4_cfg_shdr *shdr;
11618 uint32_t shdr_status, shdr_add_status;
11619
11620 virt_addr = mboxq->sge_array->addr[0];
11621 /* The IOCTL status is embedded in the mailbox subheader. */
11622 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11623 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11624 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11625
11626 if ((shdr_status || shdr_add_status) &&
11627 (shdr_status != STATUS_FCF_IN_USE))
11628 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11629 "2558 ADD_FCF_RECORD mailbox failed with "
11630 "status x%x add_status x%x\n",
11631 shdr_status, shdr_add_status);
11632
11633 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11634}
11635
11636/**
11637 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11638 * @phba: pointer to lpfc hba data structure.
11639 * @fcf_record: pointer to the initialized fcf record to add.
11640 *
11641 * This routine is invoked to manually add a single FCF record. The caller
11642 * must pass a completely initialized FCF_Record. This routine takes
11643 * care of the nonembedded mailbox operations.
11644 **/
11645int
11646lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11647{
11648 int rc = 0;
11649 LPFC_MBOXQ_t *mboxq;
11650 uint8_t *bytep;
11651 void *virt_addr;
11652 dma_addr_t phys_addr;
11653 struct lpfc_mbx_sge sge;
11654 uint32_t alloc_len, req_len;
11655 uint32_t fcfindex;
11656
11657 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11658 if (!mboxq) {
11659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11660 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11661 return -ENOMEM;
11662 }
11663
11664 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11665 sizeof(uint32_t);
11666
11667 /* Allocate DMA memory and set up the non-embedded mailbox command */
11668 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11669 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11670 req_len, LPFC_SLI4_MBX_NEMBED);
11671 if (alloc_len < req_len) {
11672 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11673 "2523 Allocated DMA memory size (x%x) is "
11674 "less than the requested DMA memory "
11675 "size (x%x)\n", alloc_len, req_len);
11676 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11677 return -ENOMEM;
11678 }
11679
11680 /*
11681 * Get the first SGE entry from the non-embedded DMA memory. This
11682 * routine only uses a single SGE.
11683 */
11684 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11685 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
11686 virt_addr = mboxq->sge_array->addr[0];
11687 /*
11688 * Configure the FCF record for FCFI 0. This is the driver's
11689 * hardcoded default and gets used in nonFIP mode.
11690 */
11691 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11692 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11693 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11694
11695 /*
11696 * Copy the fcf_index and the FCF Record Data. The data starts after
11697 * the FCoE header plus word10. The data copy needs to be endian
11698 * correct.
11699 */
11700 bytep += sizeof(uint32_t);
11701 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11702 mboxq->vport = phba->pport;
11703 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11704 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11705 if (rc == MBX_NOT_FINISHED) {
11706 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11707 "2515 ADD_FCF_RECORD mailbox failed with "
11708 "status 0x%x\n", rc);
11709 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11710 rc = -EIO;
11711 } else
11712 rc = 0;
11713
11714 return rc;
11715}
11716
11717/**
11718 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11719 * @phba: pointer to lpfc hba data structure.
11720 * @fcf_record: pointer to the fcf record to write the default data.
11721 * @fcf_index: FCF table entry index.
11722 *
11723 * This routine is invoked to build the driver's default FCF record. The
11724 * values used are hardcoded. This routine handles memory initialization.
11725 *
11726 **/
11727void
11728lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11729 struct fcf_record *fcf_record,
11730 uint16_t fcf_index)
11731{
11732 memset(fcf_record, 0, sizeof(struct fcf_record));
11733 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11734 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11735 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11736 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11737 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11738 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11739 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11740 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11741 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11742 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11743 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11744 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11745 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 11746 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
11747 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11748 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11749 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11750 /* Set the VLAN bit map */
11751 if (phba->valid_vlan) {
11752 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11753 = 1 << (phba->vlan_id % 8);
11754 }
11755}
11756
11757/**
11758 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11759 * @phba: pointer to lpfc hba data structure.
11760 * @fcf_index: FCF table entry offset.
11761 *
11762 * This routine is invoked to read up to @fcf_num of FCF record from the
11763 * device starting with the given @fcf_index.
11764 **/
11765int
11766lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11767{
11768 int rc = 0, error;
11769 LPFC_MBOXQ_t *mboxq;
11770 void *virt_addr;
11771 dma_addr_t phys_addr;
11772 uint8_t *bytep;
11773 struct lpfc_mbx_sge sge;
11774 uint32_t alloc_len, req_len;
11775 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11776
32b9793f 11777 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
11778 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11779 if (!mboxq) {
11780 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11781 "2000 Failed to allocate mbox for "
11782 "READ_FCF cmd\n");
4d9ab994
JS
11783 error = -ENOMEM;
11784 goto fail_fcfscan;
6fb120a7
JS
11785 }
11786
11787 req_len = sizeof(struct fcf_record) +
11788 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11789
11790 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11791 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11792 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11793 LPFC_SLI4_MBX_NEMBED);
11794
11795 if (alloc_len < req_len) {
11796 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11797 "0291 Allocated DMA memory size (x%x) is "
11798 "less than the requested DMA memory "
11799 "size (x%x)\n", alloc_len, req_len);
4d9ab994
JS
11800 error = -ENOMEM;
11801 goto fail_fcfscan;
6fb120a7
JS
11802 }
11803
11804 /* Get the first SGE entry from the non-embedded DMA memory. This
11805 * routine only uses a single SGE.
11806 */
11807 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11808 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
11809 virt_addr = mboxq->sge_array->addr[0];
11810 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11811
11812 /* Set up command fields */
11813 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11814 /* Perform necessary endian conversion */
11815 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11816 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11817 mboxq->vport = phba->pport;
11818 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11819 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11820 if (rc == MBX_NOT_FINISHED) {
6fb120a7 11821 error = -EIO;
32b9793f
JS
11822 } else {
11823 spin_lock_irq(&phba->hbalock);
11824 phba->hba_flag |= FCF_DISC_INPROGRESS;
11825 spin_unlock_irq(&phba->hbalock);
6fb120a7 11826 error = 0;
32b9793f 11827 }
4d9ab994
JS
11828fail_fcfscan:
11829 if (error) {
11830 if (mboxq)
11831 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11832 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11833 spin_lock_irq(&phba->hbalock);
11834 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11835 spin_unlock_irq(&phba->hbalock);
11836 }
6fb120a7
JS
11837 return error;
11838}
a0c87cbd
JS
11839
11840/**
11841 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
11842 * @phba: pointer to lpfc hba data structure.
11843 *
11844 * This function read region 23 and parse TLV for port status to
11845 * decide if the user disaled the port. If the TLV indicates the
11846 * port is disabled, the hba_flag is set accordingly.
11847 **/
11848void
11849lpfc_sli_read_link_ste(struct lpfc_hba *phba)
11850{
11851 LPFC_MBOXQ_t *pmb = NULL;
11852 MAILBOX_t *mb;
11853 uint8_t *rgn23_data = NULL;
11854 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
11855 int rc;
11856
11857 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11858 if (!pmb) {
11859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11860 "2600 lpfc_sli_read_serdes_param failed to"
11861 " allocate mailbox memory\n");
11862 goto out;
11863 }
11864 mb = &pmb->u.mb;
11865
11866 /* Get adapter Region 23 data */
11867 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
11868 if (!rgn23_data)
11869 goto out;
11870
11871 do {
11872 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
11873 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
11874
11875 if (rc != MBX_SUCCESS) {
11876 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11877 "2601 lpfc_sli_read_link_ste failed to"
11878 " read config region 23 rc 0x%x Status 0x%x\n",
11879 rc, mb->mbxStatus);
11880 mb->un.varDmp.word_cnt = 0;
11881 }
11882 /*
11883 * dump mem may return a zero when finished or we got a
11884 * mailbox error, either way we are done.
11885 */
11886 if (mb->un.varDmp.word_cnt == 0)
11887 break;
11888 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
11889 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
11890
11891 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
11892 rgn23_data + offset,
11893 mb->un.varDmp.word_cnt);
11894 offset += mb->un.varDmp.word_cnt;
11895 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
11896
11897 data_size = offset;
11898 offset = 0;
11899
11900 if (!data_size)
11901 goto out;
11902
11903 /* Check the region signature first */
11904 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
11905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11906 "2619 Config region 23 has bad signature\n");
11907 goto out;
11908 }
11909 offset += 4;
11910
11911 /* Check the data structure version */
11912 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
11913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 "2620 Config region 23 has bad version\n");
11915 goto out;
11916 }
11917 offset += 4;
11918
11919 /* Parse TLV entries in the region */
11920 while (offset < data_size) {
11921 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
11922 break;
11923 /*
11924 * If the TLV is not driver specific TLV or driver id is
11925 * not linux driver id, skip the record.
11926 */
11927 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
11928 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
11929 (rgn23_data[offset + 3] != 0)) {
11930 offset += rgn23_data[offset + 1] * 4 + 4;
11931 continue;
11932 }
11933
11934 /* Driver found a driver specific TLV in the config region */
11935 sub_tlv_len = rgn23_data[offset + 1] * 4;
11936 offset += 4;
11937 tlv_offset = 0;
11938
11939 /*
11940 * Search for configured port state sub-TLV.
11941 */
11942 while ((offset < data_size) &&
11943 (tlv_offset < sub_tlv_len)) {
11944 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
11945 offset += 4;
11946 tlv_offset += 4;
11947 break;
11948 }
11949 if (rgn23_data[offset] != PORT_STE_TYPE) {
11950 offset += rgn23_data[offset + 1] * 4 + 4;
11951 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
11952 continue;
11953 }
11954
11955 /* This HBA contains PORT_STE configured */
11956 if (!rgn23_data[offset + 2])
11957 phba->hba_flag |= LINK_DISABLED;
11958
11959 goto out;
11960 }
11961 }
11962out:
11963 if (pmb)
11964 mempool_free(pmb, phba->mbox_mem_pool);
11965 kfree(rgn23_data);
11966 return;
11967}
This page took 3.143693 seconds and 5 git commands to generate.