blob: ba698d5f31af06b326b01c0e421a2ab66fb72ead [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smartd8e93df2009-05-22 14:53:05 -04004 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040027#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050028#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -040031#include <scsi/scsi_transport_fc.h>
James Smartda0436e2009-05-22 14:51:39 -040032#include <scsi/fc/fc_fs.h>
dea31012005-04-17 16:05:31 -050033
James Smartda0436e2009-05-22 14:51:39 -040034#include "lpfc_hw4.h"
dea31012005-04-17 16:05:31 -050035#include "lpfc_hw.h"
36#include "lpfc_sli.h"
James Smartda0436e2009-05-22 14:51:39 -040037#include "lpfc_sli4.h"
James Smartea2151b2008-09-07 11:52:10 -040038#include "lpfc_nl.h"
dea31012005-04-17 16:05:31 -050039#include "lpfc_disc.h"
40#include "lpfc_scsi.h"
41#include "lpfc.h"
42#include "lpfc_crtn.h"
43#include "lpfc_logmsg.h"
44#include "lpfc_compat.h"
James Smart858c9f62007-06-17 19:56:39 -050045#include "lpfc_debugfs.h"
James Smart04c68492009-05-22 14:52:52 -040046#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050047
48/* There are only four IOCB completion types. */
49typedef enum _lpfc_iocb_type {
50 LPFC_UNKNOWN_IOCB,
51 LPFC_UNSOL_IOCB,
52 LPFC_SOL_IOCB,
53 LPFC_ABORT_IOCB
54} lpfc_iocb_type;
55
James Smart4f774512009-05-22 14:52:35 -040056
57/* Provide function prototypes local to this module. */
58static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
59 uint32_t);
60static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint8_t *, uint32_t *);
62
63static IOCB_t *
64lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
65{
66 return &iocbq->iocb;
67}
68
69/**
70 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
71 * @q: The Work Queue to operate on.
72 * @wqe: The work Queue Entry to put on the Work queue.
73 *
74 * This routine will copy the contents of @wqe to the next available entry on
75 * the @q. This function will then ring the Work Queue Doorbell to signal the
76 * HBA to start processing the Work Queue Entry. This function returns 0 if
77 * successful. If no entries are available on @q then this function will return
78 * -ENOMEM.
79 * The caller is expected to hold the hbalock when calling this routine.
80 **/
81static uint32_t
82lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
83{
84 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
85 struct lpfc_register doorbell;
86 uint32_t host_index;
87
88 /* If the host has not yet processed the next entry then we are done */
89 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
90 return -ENOMEM;
91 /* set consumption flag every once in a while */
92 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
93 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
94
95 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
96
97 /* Update the host index before invoking device */
98 host_index = q->host_index;
99 q->host_index = ((q->host_index + 1) % q->entry_count);
100
101 /* Ring Doorbell */
102 doorbell.word0 = 0;
103 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
104 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
105 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
106 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
107 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
108
109 return 0;
110}
111
112/**
113 * lpfc_sli4_wq_release - Updates internal hba index for WQ
114 * @q: The Work Queue to operate on.
115 * @index: The index to advance the hba index to.
116 *
117 * This routine will update the HBA index of a queue to reflect consumption of
118 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
119 * an entry the host calls this function to update the queue's internal
120 * pointers. This routine returns the number of entries that were consumed by
121 * the HBA.
122 **/
123static uint32_t
124lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
125{
126 uint32_t released = 0;
127
128 if (q->hba_index == index)
129 return 0;
130 do {
131 q->hba_index = ((q->hba_index + 1) % q->entry_count);
132 released++;
133 } while (q->hba_index != index);
134 return released;
135}
136
137/**
138 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
139 * @q: The Mailbox Queue to operate on.
140 * @wqe: The Mailbox Queue Entry to put on the Work queue.
141 *
142 * This routine will copy the contents of @mqe to the next available entry on
143 * the @q. This function will then ring the Work Queue Doorbell to signal the
144 * HBA to start processing the Work Queue Entry. This function returns 0 if
145 * successful. If no entries are available on @q then this function will return
146 * -ENOMEM.
147 * The caller is expected to hold the hbalock when calling this routine.
148 **/
149static uint32_t
150lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
151{
152 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
153 struct lpfc_register doorbell;
154 uint32_t host_index;
155
156 /* If the host has not yet processed the next entry then we are done */
157 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
158 return -ENOMEM;
159 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
160 /* Save off the mailbox pointer for completion */
161 q->phba->mbox = (MAILBOX_t *)temp_mqe;
162
163 /* Update the host index before invoking device */
164 host_index = q->host_index;
165 q->host_index = ((q->host_index + 1) % q->entry_count);
166
167 /* Ring Doorbell */
168 doorbell.word0 = 0;
169 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
170 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
171 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
172 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
173 return 0;
174}
175
176/**
177 * lpfc_sli4_mq_release - Updates internal hba index for MQ
178 * @q: The Mailbox Queue to operate on.
179 *
180 * This routine will update the HBA index of a queue to reflect consumption of
181 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
182 * an entry the host calls this function to update the queue's internal
183 * pointers. This routine returns the number of entries that were consumed by
184 * the HBA.
185 **/
186static uint32_t
187lpfc_sli4_mq_release(struct lpfc_queue *q)
188{
189 /* Clear the mailbox pointer for completion */
190 q->phba->mbox = NULL;
191 q->hba_index = ((q->hba_index + 1) % q->entry_count);
192 return 1;
193}
194
195/**
196 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
197 * @q: The Event Queue to get the first valid EQE from
198 *
199 * This routine will get the first valid Event Queue Entry from @q, update
200 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
201 * the Queue (no more work to do), or the Queue is full of EQEs that have been
202 * processed, but not popped back to the HBA then this routine will return NULL.
203 **/
204static struct lpfc_eqe *
205lpfc_sli4_eq_get(struct lpfc_queue *q)
206{
207 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
208
209 /* If the next EQE is not valid then we are done */
210 if (!bf_get(lpfc_eqe_valid, eqe))
211 return NULL;
212 /* If the host has not yet processed the next entry then we are done */
213 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
214 return NULL;
215
216 q->hba_index = ((q->hba_index + 1) % q->entry_count);
217 return eqe;
218}
219
220/**
221 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
222 * @q: The Event Queue that the host has completed processing for.
223 * @arm: Indicates whether the host wants to arms this CQ.
224 *
225 * This routine will mark all Event Queue Entries on @q, from the last
226 * known completed entry to the last entry that was processed, as completed
227 * by clearing the valid bit for each completion queue entry. Then it will
228 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
229 * The internal host index in the @q will be updated by this routine to indicate
230 * that the host has finished processing the entries. The @arm parameter
231 * indicates that the queue should be rearmed when ringing the doorbell.
232 *
233 * This function will return the number of EQEs that were popped.
234 **/
235uint32_t
236lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
237{
238 uint32_t released = 0;
239 struct lpfc_eqe *temp_eqe;
240 struct lpfc_register doorbell;
241
242 /* while there are valid entries */
243 while (q->hba_index != q->host_index) {
244 temp_eqe = q->qe[q->host_index].eqe;
245 bf_set(lpfc_eqe_valid, temp_eqe, 0);
246 released++;
247 q->host_index = ((q->host_index + 1) % q->entry_count);
248 }
249 if (unlikely(released == 0 && !arm))
250 return 0;
251
252 /* ring doorbell for number popped */
253 doorbell.word0 = 0;
254 if (arm) {
255 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
256 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
257 }
258 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
259 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
260 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
261 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
262 return released;
263}
264
265/**
266 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
267 * @q: The Completion Queue to get the first valid CQE from
268 *
269 * This routine will get the first valid Completion Queue Entry from @q, update
270 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
271 * the Queue (no more work to do), or the Queue is full of CQEs that have been
272 * processed, but not popped back to the HBA then this routine will return NULL.
273 **/
274static struct lpfc_cqe *
275lpfc_sli4_cq_get(struct lpfc_queue *q)
276{
277 struct lpfc_cqe *cqe;
278
279 /* If the next CQE is not valid then we are done */
280 if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
281 return NULL;
282 /* If the host has not yet processed the next entry then we are done */
283 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
284 return NULL;
285
286 cqe = q->qe[q->hba_index].cqe;
287 q->hba_index = ((q->hba_index + 1) % q->entry_count);
288 return cqe;
289}
290
291/**
292 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
293 * @q: The Completion Queue that the host has completed processing for.
294 * @arm: Indicates whether the host wants to arms this CQ.
295 *
296 * This routine will mark all Completion queue entries on @q, from the last
297 * known completed entry to the last entry that was processed, as completed
298 * by clearing the valid bit for each completion queue entry. Then it will
299 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
300 * The internal host index in the @q will be updated by this routine to indicate
301 * that the host has finished processing the entries. The @arm parameter
302 * indicates that the queue should be rearmed when ringing the doorbell.
303 *
304 * This function will return the number of CQEs that were released.
305 **/
306uint32_t
307lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
308{
309 uint32_t released = 0;
310 struct lpfc_cqe *temp_qe;
311 struct lpfc_register doorbell;
312
313 /* while there are valid entries */
314 while (q->hba_index != q->host_index) {
315 temp_qe = q->qe[q->host_index].cqe;
316 bf_set(lpfc_cqe_valid, temp_qe, 0);
317 released++;
318 q->host_index = ((q->host_index + 1) % q->entry_count);
319 }
320 if (unlikely(released == 0 && !arm))
321 return 0;
322
323 /* ring doorbell for number popped */
324 doorbell.word0 = 0;
325 if (arm)
326 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
327 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
328 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
329 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
330 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
331 return released;
332}
333
334/**
335 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
336 * @q: The Header Receive Queue to operate on.
337 * @wqe: The Receive Queue Entry to put on the Receive queue.
338 *
339 * This routine will copy the contents of @wqe to the next available entry on
340 * the @q. This function will then ring the Receive Queue Doorbell to signal the
341 * HBA to start processing the Receive Queue Entry. This function returns the
342 * index that the rqe was copied to if successful. If no entries are available
343 * on @q then this function will return -ENOMEM.
344 * The caller is expected to hold the hbalock when calling this routine.
345 **/
346static int
347lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
348 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
349{
350 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
351 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
352 struct lpfc_register doorbell;
353 int put_index = hq->host_index;
354
355 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
356 return -EINVAL;
357 if (hq->host_index != dq->host_index)
358 return -EINVAL;
359 /* If the host has not yet processed the next entry then we are done */
360 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
361 return -EBUSY;
362 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
363 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
364
365 /* Update the host index to point to the next slot */
366 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
367 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
368
369 /* Ring The Header Receive Queue Doorbell */
370 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
371 doorbell.word0 = 0;
372 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
373 LPFC_RQ_POST_BATCH);
374 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
375 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
376 }
377 return put_index;
378}
379
380/**
381 * lpfc_sli4_rq_release - Updates internal hba index for RQ
382 * @q: The Header Receive Queue to operate on.
383 *
384 * This routine will update the HBA index of a queue to reflect consumption of
385 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
386 * consumed an entry the host calls this function to update the queue's
387 * internal pointers. This routine returns the number of entries that were
388 * consumed by the HBA.
389 **/
390static uint32_t
391lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
392{
393 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
394 return 0;
395 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
396 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
397 return 1;
398}
399
James Smarte59058c2008-08-24 21:49:00 -0400400/**
James Smart3621a712009-04-06 18:47:14 -0400401 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400402 * @phba: Pointer to HBA context object.
403 * @pring: Pointer to driver SLI ring object.
404 *
405 * This function returns pointer to next command iocb entry
406 * in the command ring. The caller must hold hbalock to prevent
407 * other threads consume the next command iocb.
408 * SLI-2/SLI-3 provide different sized iocbs.
409 **/
James Smarted957682007-06-17 19:56:37 -0500410static inline IOCB_t *
411lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
412{
413 return (IOCB_t *) (((char *) pring->cmdringaddr) +
414 pring->cmdidx * phba->iocb_cmd_size);
415}
416
James Smarte59058c2008-08-24 21:49:00 -0400417/**
James Smart3621a712009-04-06 18:47:14 -0400418 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400419 * @phba: Pointer to HBA context object.
420 * @pring: Pointer to driver SLI ring object.
421 *
422 * This function returns pointer to next response iocb entry
423 * in the response ring. The caller must hold hbalock to make sure
424 * that no other thread consume the next response iocb.
425 * SLI-2/SLI-3 provide different sized iocbs.
426 **/
James Smarted957682007-06-17 19:56:37 -0500427static inline IOCB_t *
428lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
429{
430 return (IOCB_t *) (((char *) pring->rspringaddr) +
431 pring->rspidx * phba->iocb_rsp_size);
432}
433
James Smarte59058c2008-08-24 21:49:00 -0400434/**
James Smart3621a712009-04-06 18:47:14 -0400435 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400436 * @phba: Pointer to HBA context object.
437 *
438 * This function is called with hbalock held. This function
439 * allocates a new driver iocb object from the iocb pool. If the
440 * allocation is successful, it returns pointer to the newly
441 * allocated iocb object else it returns NULL.
442 **/
James Smart2e0fef82007-06-17 19:56:36 -0500443static struct lpfc_iocbq *
444__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400445{
446 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
447 struct lpfc_iocbq * iocbq = NULL;
448
449 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
450 return iocbq;
451}
452
James Smarte59058c2008-08-24 21:49:00 -0400453/**
James Smartda0436e2009-05-22 14:51:39 -0400454 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
455 * @phba: Pointer to HBA context object.
456 * @xritag: XRI value.
457 *
458 * This function clears the sglq pointer from the array of acive
459 * sglq's. The xritag that is passed in is used to index into the
460 * array. Before the xritag can be used it needs to be adjusted
461 * by subtracting the xribase.
462 *
463 * Returns sglq ponter = success, NULL = Failure.
464 **/
465static struct lpfc_sglq *
466__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
467{
468 uint16_t adj_xri;
469 struct lpfc_sglq *sglq;
470 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
471 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
472 return NULL;
473 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
474 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
475 return sglq;
476}
477
478/**
479 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
480 * @phba: Pointer to HBA context object.
481 * @xritag: XRI value.
482 *
483 * This function returns the sglq pointer from the array of acive
484 * sglq's. The xritag that is passed in is used to index into the
485 * array. Before the xritag can be used it needs to be adjusted
486 * by subtracting the xribase.
487 *
488 * Returns sglq ponter = success, NULL = Failure.
489 **/
490static struct lpfc_sglq *
491__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
492{
493 uint16_t adj_xri;
494 struct lpfc_sglq *sglq;
495 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
496 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
497 return NULL;
498 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
499 return sglq;
500}
501
502/**
503 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
504 * @phba: Pointer to HBA context object.
505 *
506 * This function is called with hbalock held. This function
507 * Gets a new driver sglq object from the sglq list. If the
508 * list is not empty then it is successful, it returns pointer to the newly
509 * allocated sglq object else it returns NULL.
510 **/
511static struct lpfc_sglq *
512__lpfc_sli_get_sglq(struct lpfc_hba *phba)
513{
514 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
515 struct lpfc_sglq *sglq = NULL;
516 uint16_t adj_xri;
517 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
518 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
519 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
520 return sglq;
521}
522
523/**
James Smart3621a712009-04-06 18:47:14 -0400524 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400525 * @phba: Pointer to HBA context object.
526 *
527 * This function is called with no lock held. This function
528 * allocates a new driver iocb object from the iocb pool. If the
529 * allocation is successful, it returns pointer to the newly
530 * allocated iocb object else it returns NULL.
531 **/
James Smart2e0fef82007-06-17 19:56:36 -0500532struct lpfc_iocbq *
533lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500534{
James Smart2e0fef82007-06-17 19:56:36 -0500535 struct lpfc_iocbq * iocbq = NULL;
536 unsigned long iflags;
537
538 spin_lock_irqsave(&phba->hbalock, iflags);
539 iocbq = __lpfc_sli_get_iocbq(phba);
540 spin_unlock_irqrestore(&phba->hbalock, iflags);
541 return iocbq;
542}
543
James Smarte59058c2008-08-24 21:49:00 -0400544/**
James Smart4f774512009-05-22 14:52:35 -0400545 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
546 * @phba: Pointer to HBA context object.
547 * @iocbq: Pointer to driver iocb object.
548 *
549 * This function is called with hbalock held to release driver
550 * iocb object to the iocb pool. The iotag in the iocb object
551 * does not change for each use of the iocb object. This function
552 * clears all other fields of the iocb object when it is freed.
553 * The sqlq structure that holds the xritag and phys and virtual
554 * mappings for the scatter gather list is retrieved from the
555 * active array of sglq. The get of the sglq pointer also clears
556 * the entry in the array. If the status of the IO indiactes that
557 * this IO was aborted then the sglq entry it put on the
558 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
559 * IO has good status or fails for any other reason then the sglq
560 * entry is added to the free list (lpfc_sgl_list).
561 **/
562static void
563__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
564{
565 struct lpfc_sglq *sglq;
566 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
567 unsigned long iflag;
568
569 if (iocbq->sli4_xritag == NO_XRI)
570 sglq = NULL;
571 else
572 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
573 if (sglq) {
574 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
575 || ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
576 && (iocbq->iocb.un.ulpWord[4]
577 == IOERR_SLI_ABORTED))) {
578 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
579 iflag);
580 list_add(&sglq->list,
581 &phba->sli4_hba.lpfc_abts_els_sgl_list);
582 spin_unlock_irqrestore(
583 &phba->sli4_hba.abts_sgl_list_lock, iflag);
584 } else
585 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
586 }
587
588
589 /*
590 * Clean all volatile data fields, preserve iotag and node struct.
591 */
592 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
593 iocbq->sli4_xritag = NO_XRI;
594 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
595}
596
597/**
James Smart3772a992009-05-22 14:50:54 -0400598 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
599 * @phba: Pointer to HBA context object.
600 * @iocbq: Pointer to driver iocb object.
601 *
602 * This function is called with hbalock held to release driver
603 * iocb object to the iocb pool. The iotag in the iocb object
604 * does not change for each use of the iocb object. This function
605 * clears all other fields of the iocb object when it is freed.
606 **/
607static void
608__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
609{
610 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
611
612 /*
613 * Clean all volatile data fields, preserve iotag and node struct.
614 */
615 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
616 iocbq->sli4_xritag = NO_XRI;
617 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
618}
619
620/**
James Smart3621a712009-04-06 18:47:14 -0400621 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400622 * @phba: Pointer to HBA context object.
623 * @iocbq: Pointer to driver iocb object.
624 *
625 * This function is called with hbalock held to release driver
626 * iocb object to the iocb pool. The iotag in the iocb object
627 * does not change for each use of the iocb object. This function
628 * clears all other fields of the iocb object when it is freed.
629 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100630static void
James Smart2e0fef82007-06-17 19:56:36 -0500631__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
632{
James Smart3772a992009-05-22 14:50:54 -0400633 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500634}
635
James Smarte59058c2008-08-24 21:49:00 -0400636/**
James Smart3621a712009-04-06 18:47:14 -0400637 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400638 * @phba: Pointer to HBA context object.
639 * @iocbq: Pointer to driver iocb object.
640 *
641 * This function is called with no lock held to release the iocb to
642 * iocb pool.
643 **/
James Smart2e0fef82007-06-17 19:56:36 -0500644void
645lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
646{
647 unsigned long iflags;
648
649 /*
650 * Clean all volatile data fields, preserve iotag and node struct.
651 */
652 spin_lock_irqsave(&phba->hbalock, iflags);
653 __lpfc_sli_release_iocbq(phba, iocbq);
654 spin_unlock_irqrestore(&phba->hbalock, iflags);
655}
656
James Smarte59058c2008-08-24 21:49:00 -0400657/**
James Smarta257bf92009-04-06 18:48:10 -0400658 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
659 * @phba: Pointer to HBA context object.
660 * @iocblist: List of IOCBs.
661 * @ulpstatus: ULP status in IOCB command field.
662 * @ulpWord4: ULP word-4 in IOCB command field.
663 *
664 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
665 * on the list by invoking the complete callback function associated with the
666 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
667 * fields.
668 **/
669void
670lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
671 uint32_t ulpstatus, uint32_t ulpWord4)
672{
673 struct lpfc_iocbq *piocb;
674
675 while (!list_empty(iocblist)) {
676 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
677
678 if (!piocb->iocb_cmpl)
679 lpfc_sli_release_iocbq(phba, piocb);
680 else {
681 piocb->iocb.ulpStatus = ulpstatus;
682 piocb->iocb.un.ulpWord[4] = ulpWord4;
683 (piocb->iocb_cmpl) (phba, piocb, piocb);
684 }
685 }
686 return;
687}
688
689/**
James Smart3621a712009-04-06 18:47:14 -0400690 * lpfc_sli_iocb_cmd_type - Get the iocb type
691 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400692 *
693 * This function is called by ring event handler function to get the iocb type.
694 * This function translates the iocb command to an iocb command type used to
695 * decide the final disposition of each completed IOCB.
696 * The function returns
697 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
698 * LPFC_SOL_IOCB if it is a solicited iocb completion
699 * LPFC_ABORT_IOCB if it is an abort iocb
700 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
701 *
702 * The caller is not required to hold any lock.
703 **/
dea31012005-04-17 16:05:31 -0500704static lpfc_iocb_type
705lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
706{
707 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
708
709 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
710 return 0;
711
712 switch (iocb_cmnd) {
713 case CMD_XMIT_SEQUENCE_CR:
714 case CMD_XMIT_SEQUENCE_CX:
715 case CMD_XMIT_BCAST_CN:
716 case CMD_XMIT_BCAST_CX:
717 case CMD_ELS_REQUEST_CR:
718 case CMD_ELS_REQUEST_CX:
719 case CMD_CREATE_XRI_CR:
720 case CMD_CREATE_XRI_CX:
721 case CMD_GET_RPI_CN:
722 case CMD_XMIT_ELS_RSP_CX:
723 case CMD_GET_RPI_CR:
724 case CMD_FCP_IWRITE_CR:
725 case CMD_FCP_IWRITE_CX:
726 case CMD_FCP_IREAD_CR:
727 case CMD_FCP_IREAD_CX:
728 case CMD_FCP_ICMND_CR:
729 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500730 case CMD_FCP_TSEND_CX:
731 case CMD_FCP_TRSP_CX:
732 case CMD_FCP_TRECEIVE_CX:
733 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500734 case CMD_ADAPTER_MSG:
735 case CMD_ADAPTER_DUMP:
736 case CMD_XMIT_SEQUENCE64_CR:
737 case CMD_XMIT_SEQUENCE64_CX:
738 case CMD_XMIT_BCAST64_CN:
739 case CMD_XMIT_BCAST64_CX:
740 case CMD_ELS_REQUEST64_CR:
741 case CMD_ELS_REQUEST64_CX:
742 case CMD_FCP_IWRITE64_CR:
743 case CMD_FCP_IWRITE64_CX:
744 case CMD_FCP_IREAD64_CR:
745 case CMD_FCP_IREAD64_CX:
746 case CMD_FCP_ICMND64_CR:
747 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500748 case CMD_FCP_TSEND64_CX:
749 case CMD_FCP_TRSP64_CX:
750 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500751 case CMD_GEN_REQUEST64_CR:
752 case CMD_GEN_REQUEST64_CX:
753 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400754 case DSSCMD_IWRITE64_CR:
755 case DSSCMD_IWRITE64_CX:
756 case DSSCMD_IREAD64_CR:
757 case DSSCMD_IREAD64_CX:
758 case DSSCMD_INVALIDATE_DEK:
759 case DSSCMD_SET_KEK:
760 case DSSCMD_GET_KEK_ID:
761 case DSSCMD_GEN_XFER:
dea31012005-04-17 16:05:31 -0500762 type = LPFC_SOL_IOCB;
763 break;
764 case CMD_ABORT_XRI_CN:
765 case CMD_ABORT_XRI_CX:
766 case CMD_CLOSE_XRI_CN:
767 case CMD_CLOSE_XRI_CX:
768 case CMD_XRI_ABORTED_CX:
769 case CMD_ABORT_MXRI64_CN:
770 type = LPFC_ABORT_IOCB;
771 break;
772 case CMD_RCV_SEQUENCE_CX:
773 case CMD_RCV_ELS_REQ_CX:
774 case CMD_RCV_SEQUENCE64_CX:
775 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400776 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500777 case CMD_IOCB_RCV_SEQ64_CX:
778 case CMD_IOCB_RCV_ELS64_CX:
779 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500780 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500781 type = LPFC_UNSOL_IOCB;
782 break;
James Smart3163f722008-02-08 18:50:25 -0500783 case CMD_IOCB_XMIT_MSEQ64_CR:
784 case CMD_IOCB_XMIT_MSEQ64_CX:
785 case CMD_IOCB_RCV_SEQ_LIST64_CX:
786 case CMD_IOCB_RCV_ELS_LIST64_CX:
787 case CMD_IOCB_CLOSE_EXTENDED_CN:
788 case CMD_IOCB_ABORT_EXTENDED_CN:
789 case CMD_IOCB_RET_HBQE64_CN:
790 case CMD_IOCB_FCP_IBIDIR64_CR:
791 case CMD_IOCB_FCP_IBIDIR64_CX:
792 case CMD_IOCB_FCP_ITASKMGT64_CX:
793 case CMD_IOCB_LOGENTRY_CN:
794 case CMD_IOCB_LOGENTRY_ASYNC_CN:
795 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700796 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500797 type = LPFC_UNKNOWN_IOCB;
798 break;
dea31012005-04-17 16:05:31 -0500799 default:
800 type = LPFC_UNKNOWN_IOCB;
801 break;
802 }
803
804 return type;
805}
806
James Smarte59058c2008-08-24 21:49:00 -0400807/**
James Smart3621a712009-04-06 18:47:14 -0400808 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400809 * @phba: Pointer to HBA context object.
810 *
811 * This function is called from SLI initialization code
812 * to configure every ring of the HBA's SLI interface. The
813 * caller is not required to hold any lock. This function issues
814 * a config_ring mailbox command for each ring.
815 * This function returns zero if successful else returns a negative
816 * error code.
817 **/
dea31012005-04-17 16:05:31 -0500818static int
James Smarted957682007-06-17 19:56:37 -0500819lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500820{
821 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500822 LPFC_MBOXQ_t *pmb;
823 MAILBOX_t *pmbox;
824 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500825
James Smarted957682007-06-17 19:56:37 -0500826 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
827 if (!pmb)
828 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400829 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500830 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500831 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500832 lpfc_config_ring(phba, i, pmb);
833 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
834 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400836 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500837 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
838 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400839 rc, pmbox->mbxCommand,
840 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500841 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500842 ret = -ENXIO;
843 break;
dea31012005-04-17 16:05:31 -0500844 }
845 }
James Smarted957682007-06-17 19:56:37 -0500846 mempool_free(pmb, phba->mbox_mem_pool);
847 return ret;
dea31012005-04-17 16:05:31 -0500848}
849
James Smarte59058c2008-08-24 21:49:00 -0400850/**
James Smart3621a712009-04-06 18:47:14 -0400851 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400852 * @phba: Pointer to HBA context object.
853 * @pring: Pointer to driver SLI ring object.
854 * @piocb: Pointer to the driver iocb object.
855 *
856 * This function is called with hbalock held. The function adds the
857 * new iocb to txcmplq of the given ring. This function always returns
858 * 0. If this function is called for ELS ring, this function checks if
859 * there is a vport associated with the ELS command. This function also
860 * starts els_tmofunc timer if this is an ELS command.
861 **/
dea31012005-04-17 16:05:31 -0500862static int
James Smart2e0fef82007-06-17 19:56:36 -0500863lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
864 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500865{
dea31012005-04-17 16:05:31 -0500866 list_add_tail(&piocb->list, &pring->txcmplq);
867 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500868 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
869 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
870 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
871 if (!piocb->vport)
872 BUG();
873 else
874 mod_timer(&piocb->vport->els_tmofunc,
875 jiffies + HZ * (phba->fc_ratov << 1));
876 }
877
dea31012005-04-17 16:05:31 -0500878
James Smart2e0fef82007-06-17 19:56:36 -0500879 return 0;
dea31012005-04-17 16:05:31 -0500880}
881
James Smarte59058c2008-08-24 21:49:00 -0400882/**
James Smart3621a712009-04-06 18:47:14 -0400883 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400884 * @phba: Pointer to HBA context object.
885 * @pring: Pointer to driver SLI ring object.
886 *
887 * This function is called with hbalock held to get next
888 * iocb in txq of the given ring. If there is any iocb in
889 * the txq, the function returns first iocb in the list after
890 * removing the iocb from the list, else it returns NULL.
891 **/
dea31012005-04-17 16:05:31 -0500892static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500893lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500894{
dea31012005-04-17 16:05:31 -0500895 struct lpfc_iocbq *cmd_iocb;
896
James Smart858c9f62007-06-17 19:56:39 -0500897 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
898 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500899 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500900 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500901}
902
James Smarte59058c2008-08-24 21:49:00 -0400903/**
James Smart3621a712009-04-06 18:47:14 -0400904 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400905 * @phba: Pointer to HBA context object.
906 * @pring: Pointer to driver SLI ring object.
907 *
908 * This function is called with hbalock held and the caller must post the
909 * iocb without releasing the lock. If the caller releases the lock,
910 * iocb slot returned by the function is not guaranteed to be available.
911 * The function returns pointer to the next available iocb slot if there
912 * is available slot in the ring, else it returns NULL.
913 * If the get index of the ring is ahead of the put index, the function
914 * will post an error attention event to the worker thread to take the
915 * HBA to offline state.
916 **/
dea31012005-04-17 16:05:31 -0500917static IOCB_t *
918lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
919{
James Smart34b02dc2008-08-24 21:49:55 -0400920 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500921 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500922 if ((pring->next_cmdidx == pring->cmdidx) &&
923 (++pring->next_cmdidx >= max_cmd_idx))
924 pring->next_cmdidx = 0;
925
926 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
927
928 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
929
930 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
931 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400932 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200933 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400934 pring->ringno,
dea31012005-04-17 16:05:31 -0500935 pring->local_getidx, max_cmd_idx);
936
James Smart2e0fef82007-06-17 19:56:36 -0500937 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500938 /*
939 * All error attention handlers are posted to
940 * worker thread
941 */
942 phba->work_ha |= HA_ERATT;
943 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500944
James Smart5e9d9b82008-06-14 22:52:53 -0400945 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500946
947 return NULL;
948 }
949
950 if (pring->local_getidx == pring->next_cmdidx)
951 return NULL;
952 }
953
James Smarted957682007-06-17 19:56:37 -0500954 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500955}
956
James Smarte59058c2008-08-24 21:49:00 -0400957/**
James Smart3621a712009-04-06 18:47:14 -0400958 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400959 * @phba: Pointer to HBA context object.
960 * @iocbq: Pointer to driver iocb object.
961 *
962 * This function gets an iotag for the iocb. If there is no unused iotag and
963 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
964 * array and assigns a new iotag.
965 * The function returns the allocated iotag if successful, else returns zero.
966 * Zero is not a valid iotag.
967 * The caller is not required to hold any lock.
968 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500969uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500970lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500971{
James Smart2e0fef82007-06-17 19:56:36 -0500972 struct lpfc_iocbq **new_arr;
973 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500974 size_t new_len;
975 struct lpfc_sli *psli = &phba->sli;
976 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500977
James Smart2e0fef82007-06-17 19:56:36 -0500978 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500979 iotag = psli->last_iotag;
980 if(++iotag < psli->iocbq_lookup_len) {
981 psli->last_iotag = iotag;
982 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500983 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500984 iocbq->iotag = iotag;
985 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500986 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500987 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
988 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500989 spin_unlock_irq(&phba->hbalock);
990 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500991 GFP_KERNEL);
992 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500993 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500994 old_arr = psli->iocbq_lookup;
995 if (new_len <= psli->iocbq_lookup_len) {
996 /* highly unprobable case */
997 kfree(new_arr);
998 iotag = psli->last_iotag;
999 if(++iotag < psli->iocbq_lookup_len) {
1000 psli->last_iotag = iotag;
1001 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001002 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001003 iocbq->iotag = iotag;
1004 return iotag;
1005 }
James Smart2e0fef82007-06-17 19:56:36 -05001006 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001007 return 0;
1008 }
1009 if (psli->iocbq_lookup)
1010 memcpy(new_arr, old_arr,
1011 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001012 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001013 psli->iocbq_lookup = new_arr;
1014 psli->iocbq_lookup_len = new_len;
1015 psli->last_iotag = iotag;
1016 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001017 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001018 iocbq->iotag = iotag;
1019 kfree(old_arr);
1020 return iotag;
1021 }
James Smart8f6d98d2006-08-01 07:34:00 -04001022 } else
James Smart2e0fef82007-06-17 19:56:36 -05001023 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001024
James Bottomley604a3e32005-10-29 10:28:33 -05001025 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001026 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1027 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001028
James Bottomley604a3e32005-10-29 10:28:33 -05001029 return 0;
dea31012005-04-17 16:05:31 -05001030}
1031
James Smarte59058c2008-08-24 21:49:00 -04001032/**
James Smart3621a712009-04-06 18:47:14 -04001033 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001034 * @phba: Pointer to HBA context object.
1035 * @pring: Pointer to driver SLI ring object.
1036 * @iocb: Pointer to iocb slot in the ring.
1037 * @nextiocb: Pointer to driver iocb object which need to be
1038 * posted to firmware.
1039 *
1040 * This function is called with hbalock held to post a new iocb to
1041 * the firmware. This function copies the new iocb to ring iocb slot and
1042 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1043 * a completion call back for this iocb else the function will free the
1044 * iocb object.
1045 **/
dea31012005-04-17 16:05:31 -05001046static void
1047lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1048 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1049{
1050 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001051 * Set up an iotag
dea31012005-04-17 16:05:31 -05001052 */
James Bottomley604a3e32005-10-29 10:28:33 -05001053 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001054
James Smarte2a0a9d2008-12-04 22:40:02 -05001055
James Smarta58cbd52007-08-02 11:09:43 -04001056 if (pring->ringno == LPFC_ELS_RING) {
1057 lpfc_debugfs_slow_ring_trc(phba,
1058 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1059 *(((uint32_t *) &nextiocb->iocb) + 4),
1060 *(((uint32_t *) &nextiocb->iocb) + 6),
1061 *(((uint32_t *) &nextiocb->iocb) + 7));
1062 }
1063
dea31012005-04-17 16:05:31 -05001064 /*
1065 * Issue iocb command to adapter
1066 */
James Smart92d7f7b2007-06-17 19:56:38 -05001067 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001068 wmb();
1069 pring->stats.iocb_cmd++;
1070
1071 /*
1072 * If there is no completion routine to call, we can release the
1073 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1074 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1075 */
1076 if (nextiocb->iocb_cmpl)
1077 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001078 else
James Smart2e0fef82007-06-17 19:56:36 -05001079 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001080
1081 /*
1082 * Let the HBA know what IOCB slot will be the next one the
1083 * driver will put a command into.
1084 */
1085 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001086 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001087}
1088
James Smarte59058c2008-08-24 21:49:00 -04001089/**
James Smart3621a712009-04-06 18:47:14 -04001090 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001091 * @phba: Pointer to HBA context object.
1092 * @pring: Pointer to driver SLI ring object.
1093 *
1094 * The caller is not required to hold any lock for calling this function.
1095 * This function updates the chip attention bits for the ring to inform firmware
1096 * that there are pending work to be done for this ring and requests an
1097 * interrupt when there is space available in the ring. This function is
1098 * called when the driver is unable to post more iocbs to the ring due
1099 * to unavailability of space in the ring.
1100 **/
dea31012005-04-17 16:05:31 -05001101static void
James Smart2e0fef82007-06-17 19:56:36 -05001102lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001103{
1104 int ringno = pring->ringno;
1105
1106 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1107
1108 wmb();
1109
1110 /*
1111 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1112 * The HBA will tell us when an IOCB entry is available.
1113 */
1114 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1115 readl(phba->CAregaddr); /* flush */
1116
1117 pring->stats.iocb_cmd_full++;
1118}
1119
James Smarte59058c2008-08-24 21:49:00 -04001120/**
James Smart3621a712009-04-06 18:47:14 -04001121 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001122 * @phba: Pointer to HBA context object.
1123 * @pring: Pointer to driver SLI ring object.
1124 *
1125 * This function updates the chip attention register bit for the
1126 * given ring to inform HBA that there is more work to be done
1127 * in this ring. The caller is not required to hold any lock.
1128 **/
dea31012005-04-17 16:05:31 -05001129static void
James Smart2e0fef82007-06-17 19:56:36 -05001130lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001131{
1132 int ringno = pring->ringno;
1133
1134 /*
1135 * Tell the HBA that there is work to do in this ring.
1136 */
James Smart34b02dc2008-08-24 21:49:55 -04001137 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1138 wmb();
1139 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1140 readl(phba->CAregaddr); /* flush */
1141 }
dea31012005-04-17 16:05:31 -05001142}
1143
James Smarte59058c2008-08-24 21:49:00 -04001144/**
James Smart3621a712009-04-06 18:47:14 -04001145 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001146 * @phba: Pointer to HBA context object.
1147 * @pring: Pointer to driver SLI ring object.
1148 *
1149 * This function is called with hbalock held to post pending iocbs
1150 * in the txq to the firmware. This function is called when driver
1151 * detects space available in the ring.
1152 **/
dea31012005-04-17 16:05:31 -05001153static void
James Smart2e0fef82007-06-17 19:56:36 -05001154lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001155{
1156 IOCB_t *iocb;
1157 struct lpfc_iocbq *nextiocb;
1158
1159 /*
1160 * Check to see if:
1161 * (a) there is anything on the txq to send
1162 * (b) link is up
1163 * (c) link attention events can be processed (fcp ring only)
1164 * (d) IOCB processing is not blocked by the outstanding mbox command.
1165 */
1166 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001167 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001168 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001169 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001170
1171 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1172 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1173 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1174
1175 if (iocb)
1176 lpfc_sli_update_ring(phba, pring);
1177 else
1178 lpfc_sli_update_full_ring(phba, pring);
1179 }
1180
1181 return;
1182}
1183
James Smarte59058c2008-08-24 21:49:00 -04001184/**
James Smart3621a712009-04-06 18:47:14 -04001185 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001186 * @phba: Pointer to HBA context object.
1187 * @hbqno: HBQ number.
1188 *
1189 * This function is called with hbalock held to get the next
1190 * available slot for the given HBQ. If there is free slot
1191 * available for the HBQ it will return pointer to the next available
1192 * HBQ entry else it will return NULL.
1193 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001194static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001195lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1196{
1197 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1198
1199 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1200 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1201 hbqp->next_hbqPutIdx = 0;
1202
1203 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001204 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001205 uint32_t getidx = le32_to_cpu(raw_index);
1206
1207 hbqp->local_hbqGetIdx = getidx;
1208
1209 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1210 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001211 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001212 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001213 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001214 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001215 hbqp->entry_count);
1216
1217 phba->link_state = LPFC_HBA_ERROR;
1218 return NULL;
1219 }
1220
1221 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1222 return NULL;
1223 }
1224
James Smart51ef4c22007-08-02 11:10:31 -04001225 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1226 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001227}
1228
James Smarte59058c2008-08-24 21:49:00 -04001229/**
James Smart3621a712009-04-06 18:47:14 -04001230 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001231 * @phba: Pointer to HBA context object.
1232 *
1233 * This function is called with no lock held to free all the
1234 * hbq buffers while uninitializing the SLI interface. It also
1235 * frees the HBQ buffers returned by the firmware but not yet
1236 * processed by the upper layers.
1237 **/
James Smarted957682007-06-17 19:56:37 -05001238void
1239lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1240{
James Smart92d7f7b2007-06-17 19:56:38 -05001241 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1242 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001243 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001244 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001245 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001246
James Smart51ef4c22007-08-02 11:10:31 -04001247 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001248 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001249 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001250 for (i = 0; i < hbq_count; ++i) {
1251 list_for_each_entry_safe(dmabuf, next_dmabuf,
1252 &phba->hbqs[i].hbq_buffer_list, list) {
1253 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1254 list_del(&hbq_buf->dbuf.list);
1255 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1256 }
James Smarta8adb832007-10-27 13:37:53 -04001257 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001258 }
James Smart3163f722008-02-08 18:50:25 -05001259 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001260 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1261 list) {
James Smart3163f722008-02-08 18:50:25 -05001262 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1263 list_del(&hbq_buf->dbuf.list);
1264 if (hbq_buf->tag == -1) {
1265 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1266 (phba, hbq_buf);
1267 } else {
1268 hbqno = hbq_buf->tag >> 16;
1269 if (hbqno >= LPFC_MAX_HBQS)
1270 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1271 (phba, hbq_buf);
1272 else
1273 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1274 hbq_buf);
1275 }
1276 }
1277
1278 /* Mark the HBQs not in use */
1279 phba->hbq_in_use = 0;
1280 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001281}
1282
James Smarte59058c2008-08-24 21:49:00 -04001283/**
James Smart3621a712009-04-06 18:47:14 -04001284 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001285 * @phba: Pointer to HBA context object.
1286 * @hbqno: HBQ number.
1287 * @hbq_buf: Pointer to HBQ buffer.
1288 *
1289 * This function is called with the hbalock held to post a
1290 * hbq buffer to the firmware. If the function finds an empty
1291 * slot in the HBQ, it will post the buffer. The function will return
1292 * pointer to the hbq entry if it successfully post the buffer
1293 * else it will return NULL.
1294 **/
James Smart3772a992009-05-22 14:50:54 -04001295static int
James Smarted957682007-06-17 19:56:37 -05001296lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001297 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001298{
James Smart3772a992009-05-22 14:50:54 -04001299 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1300}
1301
1302/**
1303 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1304 * @phba: Pointer to HBA context object.
1305 * @hbqno: HBQ number.
1306 * @hbq_buf: Pointer to HBQ buffer.
1307 *
1308 * This function is called with the hbalock held to post a hbq buffer to the
1309 * firmware. If the function finds an empty slot in the HBQ, it will post the
1310 * buffer and place it on the hbq_buffer_list. The function will return zero if
1311 * it successfully post the buffer else it will return an error.
1312 **/
1313static int
1314lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1315 struct hbq_dmabuf *hbq_buf)
1316{
James Smarted957682007-06-17 19:56:37 -05001317 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001318 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001319
1320 /* Get next HBQ entry slot to use */
1321 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1322 if (hbqe) {
1323 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1324
James Smart92d7f7b2007-06-17 19:56:38 -05001325 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1326 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001327 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001328 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001329 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1330 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1331 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001332 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1333 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001334 /* flush */
James Smarted957682007-06-17 19:56:37 -05001335 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001336 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001337 return 0;
1338 } else
1339 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001340}
1341
James Smart4f774512009-05-22 14:52:35 -04001342/**
1343 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1344 * @phba: Pointer to HBA context object.
1345 * @hbqno: HBQ number.
1346 * @hbq_buf: Pointer to HBQ buffer.
1347 *
1348 * This function is called with the hbalock held to post an RQE to the SLI4
1349 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1350 * the hbq_buffer_list and return zero, otherwise it will return an error.
1351 **/
1352static int
1353lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1354 struct hbq_dmabuf *hbq_buf)
1355{
1356 int rc;
1357 struct lpfc_rqe hrqe;
1358 struct lpfc_rqe drqe;
1359
1360 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1361 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1362 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1363 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1364 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1365 &hrqe, &drqe);
1366 if (rc < 0)
1367 return rc;
1368 hbq_buf->tag = rc;
1369 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1370 return 0;
1371}
1372
James Smarte59058c2008-08-24 21:49:00 -04001373/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001374static struct lpfc_hbq_init lpfc_els_hbq = {
1375 .rn = 1,
1376 .entry_count = 200,
1377 .mask_count = 0,
1378 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001379 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001380 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001381 .init_count = 40,
1382 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001383};
James Smarted957682007-06-17 19:56:37 -05001384
James Smarte59058c2008-08-24 21:49:00 -04001385/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001386static struct lpfc_hbq_init lpfc_extra_hbq = {
1387 .rn = 1,
1388 .entry_count = 200,
1389 .mask_count = 0,
1390 .profile = 0,
1391 .ring_mask = (1 << LPFC_EXTRA_RING),
1392 .buffer_count = 0,
1393 .init_count = 0,
1394 .add_count = 5,
1395};
1396
James Smarte59058c2008-08-24 21:49:00 -04001397/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001398struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001399 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001400 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001401};
1402
James Smarte59058c2008-08-24 21:49:00 -04001403/**
James Smart3621a712009-04-06 18:47:14 -04001404 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001405 * @phba: Pointer to HBA context object.
1406 * @hbqno: HBQ number.
1407 * @count: Number of HBQ buffers to be posted.
1408 *
James Smartd7c255b2008-08-24 21:50:00 -04001409 * This function is called with no lock held to post more hbq buffers to the
1410 * given HBQ. The function returns the number of HBQ buffers successfully
1411 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001412 **/
James Smart311464e2007-08-02 11:10:37 -04001413static int
James Smart92d7f7b2007-06-17 19:56:38 -05001414lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1415{
James Smartd7c255b2008-08-24 21:50:00 -04001416 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001417 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001418 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001419 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001420 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001421 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001422
James Smartd7c255b2008-08-24 21:50:00 -04001423 if ((phba->hbqs[hbqno].buffer_count + count) >
1424 lpfc_hbq_defs[hbqno]->entry_count)
1425 count = lpfc_hbq_defs[hbqno]->entry_count -
1426 phba->hbqs[hbqno].buffer_count;
1427 if (!count)
1428 return 0;
1429 /* Allocate HBQ entries */
1430 for (i = 0; i < count; i++) {
1431 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1432 if (!hbq_buffer)
1433 break;
1434 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1435 }
James Smart3163f722008-02-08 18:50:25 -05001436 /* Check whether HBQ is still in use */
1437 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001438 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001439 goto err;
1440 while (!list_empty(&hbq_buf_list)) {
1441 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1442 dbuf.list);
1443 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1444 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001445 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001446 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001447 posted++;
1448 } else
James Smart51ef4c22007-08-02 11:10:31 -04001449 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001450 }
James Smart3163f722008-02-08 18:50:25 -05001451 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001452 return posted;
1453err:
1454 spin_unlock_irqrestore(&phba->hbalock, flags);
1455 while (!list_empty(&hbq_buf_list)) {
1456 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1457 dbuf.list);
1458 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1459 }
James Smart92d7f7b2007-06-17 19:56:38 -05001460 return 0;
James Smarted957682007-06-17 19:56:37 -05001461}
1462
James Smarte59058c2008-08-24 21:49:00 -04001463/**
James Smart3621a712009-04-06 18:47:14 -04001464 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001465 * @phba: Pointer to HBA context object.
1466 * @qno: HBQ number.
1467 *
1468 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001469 * is called with no lock held. The function returns the number of HBQ entries
1470 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001471 **/
James Smarted957682007-06-17 19:56:37 -05001472int
James Smart92d7f7b2007-06-17 19:56:38 -05001473lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001474{
James Smart92d7f7b2007-06-17 19:56:38 -05001475 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1476 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -05001477}
1478
James Smarte59058c2008-08-24 21:49:00 -04001479/**
James Smart3621a712009-04-06 18:47:14 -04001480 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001481 * @phba: Pointer to HBA context object.
1482 * @qno: HBQ queue number.
1483 *
1484 * This function is called from SLI initialization code path with
1485 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001486 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001487 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001488static int
James Smart92d7f7b2007-06-17 19:56:38 -05001489lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001490{
James Smart92d7f7b2007-06-17 19:56:38 -05001491 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1492 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -05001493}
1494
James Smarte59058c2008-08-24 21:49:00 -04001495/**
James Smart3772a992009-05-22 14:50:54 -04001496 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1497 * @phba: Pointer to HBA context object.
1498 * @hbqno: HBQ number.
1499 *
1500 * This function removes the first hbq buffer on an hbq list and returns a
1501 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1502 **/
1503static struct hbq_dmabuf *
1504lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1505{
1506 struct lpfc_dmabuf *d_buf;
1507
1508 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1509 if (!d_buf)
1510 return NULL;
1511 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1512}
1513
1514/**
James Smart3621a712009-04-06 18:47:14 -04001515 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001516 * @phba: Pointer to HBA context object.
1517 * @tag: Tag of the hbq buffer.
1518 *
1519 * This function is called with hbalock held. This function searches
1520 * for the hbq buffer associated with the given tag in the hbq buffer
1521 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1522 * it returns NULL.
1523 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001524static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001525lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1526{
James Smart92d7f7b2007-06-17 19:56:38 -05001527 struct lpfc_dmabuf *d_buf;
1528 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001529 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001530
James Smart51ef4c22007-08-02 11:10:31 -04001531 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001532 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001533 return NULL;
1534
James Smart3772a992009-05-22 14:50:54 -04001535 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001536 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001537 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001538 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001539 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001540 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001541 }
1542 }
James Smart3772a992009-05-22 14:50:54 -04001543 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001545 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001546 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001547 return NULL;
James Smarted957682007-06-17 19:56:37 -05001548}
1549
James Smarte59058c2008-08-24 21:49:00 -04001550/**
James Smart3621a712009-04-06 18:47:14 -04001551 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001552 * @phba: Pointer to HBA context object.
1553 * @hbq_buffer: Pointer to HBQ buffer.
1554 *
1555 * This function is called with hbalock. This function gives back
1556 * the hbq buffer to firmware. If the HBQ does not have space to
1557 * post the buffer, it will free the buffer.
1558 **/
James Smarted957682007-06-17 19:56:37 -05001559void
James Smart51ef4c22007-08-02 11:10:31 -04001560lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001561{
1562 uint32_t hbqno;
1563
James Smart51ef4c22007-08-02 11:10:31 -04001564 if (hbq_buffer) {
1565 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001566 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001567 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001568 }
1569}
1570
James Smarte59058c2008-08-24 21:49:00 -04001571/**
James Smart3621a712009-04-06 18:47:14 -04001572 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001573 * @mbxCommand: mailbox command code.
1574 *
1575 * This function is called by the mailbox event handler function to verify
1576 * that the completed mailbox command is a legitimate mailbox command. If the
1577 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1578 * and the mailbox event handler will take the HBA offline.
1579 **/
dea31012005-04-17 16:05:31 -05001580static int
1581lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1582{
1583 uint8_t ret;
1584
1585 switch (mbxCommand) {
1586 case MBX_LOAD_SM:
1587 case MBX_READ_NV:
1588 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001589 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001590 case MBX_RUN_BIU_DIAG:
1591 case MBX_INIT_LINK:
1592 case MBX_DOWN_LINK:
1593 case MBX_CONFIG_LINK:
1594 case MBX_CONFIG_RING:
1595 case MBX_RESET_RING:
1596 case MBX_READ_CONFIG:
1597 case MBX_READ_RCONFIG:
1598 case MBX_READ_SPARM:
1599 case MBX_READ_STATUS:
1600 case MBX_READ_RPI:
1601 case MBX_READ_XRI:
1602 case MBX_READ_REV:
1603 case MBX_READ_LNK_STAT:
1604 case MBX_REG_LOGIN:
1605 case MBX_UNREG_LOGIN:
1606 case MBX_READ_LA:
1607 case MBX_CLEAR_LA:
1608 case MBX_DUMP_MEMORY:
1609 case MBX_DUMP_CONTEXT:
1610 case MBX_RUN_DIAGS:
1611 case MBX_RESTART:
1612 case MBX_UPDATE_CFG:
1613 case MBX_DOWN_LOAD:
1614 case MBX_DEL_LD_ENTRY:
1615 case MBX_RUN_PROGRAM:
1616 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001617 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001618 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001619 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001620 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001621 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001622 case MBX_LOAD_AREA:
1623 case MBX_RUN_BIU_DIAG64:
1624 case MBX_CONFIG_PORT:
1625 case MBX_READ_SPARM64:
1626 case MBX_READ_RPI64:
1627 case MBX_REG_LOGIN64:
1628 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001629 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001630 case MBX_SET_DEBUG:
1631 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001632 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001633 case MBX_REG_VPI:
1634 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001635 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001636 case MBX_PORT_CAPABILITIES:
1637 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001638 case MBX_SLI4_CONFIG:
1639 case MBX_SLI4_REQ_FTRS:
1640 case MBX_REG_FCFI:
1641 case MBX_UNREG_FCFI:
1642 case MBX_REG_VFI:
1643 case MBX_UNREG_VFI:
1644 case MBX_INIT_VPI:
1645 case MBX_INIT_VFI:
1646 case MBX_RESUME_RPI:
dea31012005-04-17 16:05:31 -05001647 ret = mbxCommand;
1648 break;
1649 default:
1650 ret = MBX_SHUTDOWN;
1651 break;
1652 }
James Smart2e0fef82007-06-17 19:56:36 -05001653 return ret;
dea31012005-04-17 16:05:31 -05001654}
James Smarte59058c2008-08-24 21:49:00 -04001655
1656/**
James Smart3621a712009-04-06 18:47:14 -04001657 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001658 * @phba: Pointer to HBA context object.
1659 * @pmboxq: Pointer to mailbox command.
1660 *
1661 * This is completion handler function for mailbox commands issued from
1662 * lpfc_sli_issue_mbox_wait function. This function is called by the
1663 * mailbox event handler function with no lock held. This function
1664 * will wake up thread waiting on the wait queue pointed by context1
1665 * of the mailbox.
1666 **/
James Smart04c68492009-05-22 14:52:52 -04001667void
James Smart2e0fef82007-06-17 19:56:36 -05001668lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001669{
1670 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001671 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001672
1673 /*
1674 * If pdone_q is empty, the driver thread gave up waiting and
1675 * continued running.
1676 */
James Smart7054a602007-04-25 09:52:34 -04001677 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001678 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001679 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1680 if (pdone_q)
1681 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001682 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001683 return;
1684}
1685
James Smarte59058c2008-08-24 21:49:00 -04001686
1687/**
James Smart3621a712009-04-06 18:47:14 -04001688 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001689 * @phba: Pointer to HBA context object.
1690 * @pmb: Pointer to mailbox object.
1691 *
1692 * This function is the default mailbox completion handler. It
1693 * frees the memory resources associated with the completed mailbox
1694 * command. If the completed command is a REG_LOGIN mailbox command,
1695 * this function will issue a UREG_LOGIN to re-claim the RPI.
1696 **/
dea31012005-04-17 16:05:31 -05001697void
James Smart2e0fef82007-06-17 19:56:36 -05001698lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001699{
1700 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001701 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001702 int rc;
1703
dea31012005-04-17 16:05:31 -05001704 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001705
dea31012005-04-17 16:05:31 -05001706 if (mp) {
1707 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1708 kfree(mp);
1709 }
James Smart7054a602007-04-25 09:52:34 -04001710
James Smart04c68492009-05-22 14:52:52 -04001711 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1712 (phba->sli_rev == LPFC_SLI_REV4))
1713 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1714
James Smart7054a602007-04-25 09:52:34 -04001715 /*
1716 * If a REG_LOGIN succeeded after node is destroyed or node
1717 * is in re-discovery driver need to cleanup the RPI.
1718 */
James Smart2e0fef82007-06-17 19:56:36 -05001719 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001720 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1721 !pmb->u.mb.mbxStatus) {
1722 rpi = pmb->u.mb.un.varWords[0];
1723 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1724 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001725 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001726 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1727 if (rc != MBX_NOT_FINISHED)
1728 return;
1729 }
1730
James Smart04c68492009-05-22 14:52:52 -04001731 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1732 lpfc_sli4_mbox_cmd_free(phba, pmb);
1733 else
1734 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001735}
1736
James Smarte59058c2008-08-24 21:49:00 -04001737/**
James Smart3621a712009-04-06 18:47:14 -04001738 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001739 * @phba: Pointer to HBA context object.
1740 *
1741 * This function is called with no lock held. This function processes all
1742 * the completed mailbox commands and gives it to upper layers. The interrupt
1743 * service routine processes mailbox completion interrupt and adds completed
1744 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1745 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1746 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1747 * function returns the mailbox commands to the upper layer by calling the
1748 * completion handler function of each mailbox.
1749 **/
dea31012005-04-17 16:05:31 -05001750int
James Smart2e0fef82007-06-17 19:56:36 -05001751lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001752{
James Smart92d7f7b2007-06-17 19:56:38 -05001753 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001754 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001755 int rc;
1756 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001757
1758 phba->sli.slistat.mbox_event++;
1759
James Smart92d7f7b2007-06-17 19:56:38 -05001760 /* Get all completed mailboxe buffers into the cmplq */
1761 spin_lock_irq(&phba->hbalock);
1762 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1763 spin_unlock_irq(&phba->hbalock);
1764
dea31012005-04-17 16:05:31 -05001765 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001766 do {
1767 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1768 if (pmb == NULL)
1769 break;
1770
James Smart04c68492009-05-22 14:52:52 -04001771 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001772
James Smart858c9f62007-06-17 19:56:39 -05001773 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1774 if (pmb->vport) {
1775 lpfc_debugfs_disc_trc(pmb->vport,
1776 LPFC_DISC_TRC_MBOX_VPORT,
1777 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1778 (uint32_t)pmbox->mbxCommand,
1779 pmbox->un.varWords[0],
1780 pmbox->un.varWords[1]);
1781 }
1782 else {
1783 lpfc_debugfs_disc_trc(phba->pport,
1784 LPFC_DISC_TRC_MBOX,
1785 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1786 (uint32_t)pmbox->mbxCommand,
1787 pmbox->un.varWords[0],
1788 pmbox->un.varWords[1]);
1789 }
1790 }
1791
dea31012005-04-17 16:05:31 -05001792 /*
1793 * It is a fatal error if unknown mbox command completion.
1794 */
1795 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1796 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001797 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001798 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001799 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001800 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001801 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001802 pmbox->mbxCommand,
1803 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001804 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001805 phba->work_hs = HS_FFER3;
1806 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001807 continue;
dea31012005-04-17 16:05:31 -05001808 }
1809
dea31012005-04-17 16:05:31 -05001810 if (pmbox->mbxStatus) {
1811 phba->sli.slistat.mbox_stat_err++;
1812 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1813 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001814 lpfc_printf_log(phba, KERN_INFO,
1815 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001816 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001817 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001818 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001819 pmb->vport ? pmb->vport->vpi :0,
1820 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001821 lpfc_sli4_mbox_opcode_get(phba,
1822 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001823 pmbox->mbxStatus,
1824 pmbox->un.varWords[0],
1825 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001826 pmbox->mbxStatus = 0;
1827 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001828 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001829 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001830 continue;
dea31012005-04-17 16:05:31 -05001831 }
1832 }
1833
1834 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001835 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001836 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001837 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001838 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001839 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001840 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001841 pmb->mbox_cmpl,
1842 *((uint32_t *) pmbox),
1843 pmbox->un.varWords[0],
1844 pmbox->un.varWords[1],
1845 pmbox->un.varWords[2],
1846 pmbox->un.varWords[3],
1847 pmbox->un.varWords[4],
1848 pmbox->un.varWords[5],
1849 pmbox->un.varWords[6],
1850 pmbox->un.varWords[7]);
1851
James Smart92d7f7b2007-06-17 19:56:38 -05001852 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001853 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001854 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001855 return 0;
dea31012005-04-17 16:05:31 -05001856}
James Smart92d7f7b2007-06-17 19:56:38 -05001857
James Smarte59058c2008-08-24 21:49:00 -04001858/**
James Smart3621a712009-04-06 18:47:14 -04001859 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001860 * @phba: Pointer to HBA context object.
1861 * @pring: Pointer to driver SLI ring object.
1862 * @tag: buffer tag.
1863 *
1864 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1865 * is set in the tag the buffer is posted for a particular exchange,
1866 * the function will return the buffer without replacing the buffer.
1867 * If the buffer is for unsolicited ELS or CT traffic, this function
1868 * returns the buffer and also posts another buffer to the firmware.
1869 **/
James Smart76bb24e2007-10-27 13:38:00 -04001870static struct lpfc_dmabuf *
1871lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001872 struct lpfc_sli_ring *pring,
1873 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001874{
James Smart9f1e1b52008-12-04 22:39:40 -05001875 struct hbq_dmabuf *hbq_entry;
1876
James Smart76bb24e2007-10-27 13:38:00 -04001877 if (tag & QUE_BUFTAG_BIT)
1878 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001879 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1880 if (!hbq_entry)
1881 return NULL;
1882 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001883}
James Smart57127f12007-10-27 13:37:05 -04001884
James Smart3772a992009-05-22 14:50:54 -04001885/**
1886 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1887 * @phba: Pointer to HBA context object.
1888 * @pring: Pointer to driver SLI ring object.
1889 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1890 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1891 * @fch_type: the type for the first frame of the sequence.
1892 *
1893 * This function is called with no lock held. This function uses the r_ctl and
1894 * type of the received sequence to find the correct callback function to call
1895 * to process the sequence.
1896 **/
1897static int
1898lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1899 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1900 uint32_t fch_type)
1901{
1902 int i;
1903
1904 /* unSolicited Responses */
1905 if (pring->prt[0].profile) {
1906 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1907 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1908 saveq);
1909 return 1;
1910 }
1911 /* We must search, based on rctl / type
1912 for the right routine */
1913 for (i = 0; i < pring->num_mask; i++) {
1914 if ((pring->prt[i].rctl == fch_r_ctl) &&
1915 (pring->prt[i].type == fch_type)) {
1916 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1917 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1918 (phba, pring, saveq);
1919 return 1;
1920 }
1921 }
1922 return 0;
1923}
James Smarte59058c2008-08-24 21:49:00 -04001924
1925/**
James Smart3621a712009-04-06 18:47:14 -04001926 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001927 * @phba: Pointer to HBA context object.
1928 * @pring: Pointer to driver SLI ring object.
1929 * @saveq: Pointer to the unsolicited iocb.
1930 *
1931 * This function is called with no lock held by the ring event handler
1932 * when there is an unsolicited iocb posted to the response ring by the
1933 * firmware. This function gets the buffer associated with the iocbs
1934 * and calls the event handler for the ring. This function handles both
1935 * qring buffers and hbq buffers.
1936 * When the function returns 1 the caller can free the iocb object otherwise
1937 * upper layer functions will free the iocb objects.
1938 **/
dea31012005-04-17 16:05:31 -05001939static int
1940lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1941 struct lpfc_iocbq *saveq)
1942{
1943 IOCB_t * irsp;
1944 WORD5 * w5p;
1945 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001946 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001947 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001948 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001949
1950 match = 0;
1951 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001952
1953 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1954 if (pring->lpfc_sli_rcv_async_status)
1955 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1956 else
1957 lpfc_printf_log(phba,
1958 KERN_WARNING,
1959 LOG_SLI,
1960 "0316 Ring %d handler: unexpected "
1961 "ASYNC_STATUS iocb received evt_code "
1962 "0x%x\n",
1963 pring->ringno,
1964 irsp->un.asyncstat.evt_code);
1965 return 1;
1966 }
1967
James Smart3163f722008-02-08 18:50:25 -05001968 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1969 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1970 if (irsp->ulpBdeCount > 0) {
1971 dmzbuf = lpfc_sli_get_buff(phba, pring,
1972 irsp->un.ulpWord[3]);
1973 lpfc_in_buf_free(phba, dmzbuf);
1974 }
1975
1976 if (irsp->ulpBdeCount > 1) {
1977 dmzbuf = lpfc_sli_get_buff(phba, pring,
1978 irsp->unsli3.sli3Words[3]);
1979 lpfc_in_buf_free(phba, dmzbuf);
1980 }
1981
1982 if (irsp->ulpBdeCount > 2) {
1983 dmzbuf = lpfc_sli_get_buff(phba, pring,
1984 irsp->unsli3.sli3Words[7]);
1985 lpfc_in_buf_free(phba, dmzbuf);
1986 }
1987
1988 return 1;
1989 }
1990
James Smart92d7f7b2007-06-17 19:56:38 -05001991 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001992 if (irsp->ulpBdeCount != 0) {
1993 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001994 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001995 if (!saveq->context2)
1996 lpfc_printf_log(phba,
1997 KERN_ERR,
1998 LOG_SLI,
1999 "0341 Ring %d Cannot find buffer for "
2000 "an unsolicited iocb. tag 0x%x\n",
2001 pring->ringno,
2002 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002003 }
2004 if (irsp->ulpBdeCount == 2) {
2005 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002006 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002007 if (!saveq->context3)
2008 lpfc_printf_log(phba,
2009 KERN_ERR,
2010 LOG_SLI,
2011 "0342 Ring %d Cannot find buffer for an"
2012 " unsolicited iocb. tag 0x%x\n",
2013 pring->ringno,
2014 irsp->unsli3.sli3Words[7]);
2015 }
2016 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002017 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002018 if (irsp->ulpBdeCount != 0) {
2019 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2020 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002021 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002022 lpfc_printf_log(phba,
2023 KERN_ERR,
2024 LOG_SLI,
2025 "0343 Ring %d Cannot find "
2026 "buffer for an unsolicited iocb"
2027 ". tag 0x%x\n", pring->ringno,
2028 irsp->un.ulpWord[3]);
2029 }
2030 if (irsp->ulpBdeCount == 2) {
2031 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2032 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002033 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002034 lpfc_printf_log(phba,
2035 KERN_ERR,
2036 LOG_SLI,
2037 "0344 Ring %d Cannot find "
2038 "buffer for an unsolicited "
2039 "iocb. tag 0x%x\n",
2040 pring->ringno,
2041 irsp->unsli3.sli3Words[7]);
2042 }
2043 }
James Smart92d7f7b2007-06-17 19:56:38 -05002044 }
James Smart9c2face2008-01-11 01:53:18 -05002045 if (irsp->ulpBdeCount != 0 &&
2046 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2047 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2048 int found = 0;
2049
2050 /* search continue save q for same XRI */
2051 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2052 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2053 list_add_tail(&saveq->list, &iocbq->list);
2054 found = 1;
2055 break;
2056 }
2057 }
2058 if (!found)
2059 list_add_tail(&saveq->clist,
2060 &pring->iocb_continue_saveq);
2061 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2062 list_del_init(&iocbq->clist);
2063 saveq = iocbq;
2064 irsp = &(saveq->iocb);
2065 } else
2066 return 0;
2067 }
2068 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2069 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2070 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2071 Rctl = FC_ELS_REQ;
2072 Type = FC_ELS_DATA;
2073 } else {
2074 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2075 Rctl = w5p->hcsw.Rctl;
2076 Type = w5p->hcsw.Type;
2077
2078 /* Firmware Workaround */
2079 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2080 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2081 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2082 Rctl = FC_ELS_REQ;
2083 Type = FC_ELS_DATA;
2084 w5p->hcsw.Rctl = Rctl;
2085 w5p->hcsw.Type = Type;
2086 }
2087 }
James Smart92d7f7b2007-06-17 19:56:38 -05002088
James Smart3772a992009-05-22 14:50:54 -04002089 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002090 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002091 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002092 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002093 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002094
James Smart92d7f7b2007-06-17 19:56:38 -05002095 return 1;
dea31012005-04-17 16:05:31 -05002096}
2097
James Smarte59058c2008-08-24 21:49:00 -04002098/**
James Smart3621a712009-04-06 18:47:14 -04002099 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002100 * @phba: Pointer to HBA context object.
2101 * @pring: Pointer to driver SLI ring object.
2102 * @prspiocb: Pointer to response iocb object.
2103 *
2104 * This function looks up the iocb_lookup table to get the command iocb
2105 * corresponding to the given response iocb using the iotag of the
2106 * response iocb. This function is called with the hbalock held.
2107 * This function returns the command iocb object if it finds the command
2108 * iocb else returns NULL.
2109 **/
dea31012005-04-17 16:05:31 -05002110static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002111lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2112 struct lpfc_sli_ring *pring,
2113 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002114{
dea31012005-04-17 16:05:31 -05002115 struct lpfc_iocbq *cmd_iocb = NULL;
2116 uint16_t iotag;
2117
James Bottomley604a3e32005-10-29 10:28:33 -05002118 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002119
James Bottomley604a3e32005-10-29 10:28:33 -05002120 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2121 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002122 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002123 pring->txcmplq_cnt--;
2124 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002125 }
2126
dea31012005-04-17 16:05:31 -05002127 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002128 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002129 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002130 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002131 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002132 return NULL;
2133}
2134
James Smarte59058c2008-08-24 21:49:00 -04002135/**
James Smart3772a992009-05-22 14:50:54 -04002136 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2137 * @phba: Pointer to HBA context object.
2138 * @pring: Pointer to driver SLI ring object.
2139 * @iotag: IOCB tag.
2140 *
2141 * This function looks up the iocb_lookup table to get the command iocb
2142 * corresponding to the given iotag. This function is called with the
2143 * hbalock held.
2144 * This function returns the command iocb object if it finds the command
2145 * iocb else returns NULL.
2146 **/
2147static struct lpfc_iocbq *
2148lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2149 struct lpfc_sli_ring *pring, uint16_t iotag)
2150{
2151 struct lpfc_iocbq *cmd_iocb;
2152
2153 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2154 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2155 list_del_init(&cmd_iocb->list);
2156 pring->txcmplq_cnt--;
2157 return cmd_iocb;
2158 }
2159
2160 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2161 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2162 iotag, phba->sli.last_iotag);
2163 return NULL;
2164}
2165
2166/**
James Smart3621a712009-04-06 18:47:14 -04002167 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002168 * @phba: Pointer to HBA context object.
2169 * @pring: Pointer to driver SLI ring object.
2170 * @saveq: Pointer to the response iocb to be processed.
2171 *
2172 * This function is called by the ring event handler for non-fcp
2173 * rings when there is a new response iocb in the response ring.
2174 * The caller is not required to hold any locks. This function
2175 * gets the command iocb associated with the response iocb and
2176 * calls the completion handler for the command iocb. If there
2177 * is no completion handler, the function will free the resources
2178 * associated with command iocb. If the response iocb is for
2179 * an already aborted command iocb, the status of the completion
2180 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2181 * This function always returns 1.
2182 **/
dea31012005-04-17 16:05:31 -05002183static int
James Smart2e0fef82007-06-17 19:56:36 -05002184lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002185 struct lpfc_iocbq *saveq)
2186{
James Smart2e0fef82007-06-17 19:56:36 -05002187 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002188 int rc = 1;
2189 unsigned long iflag;
2190
2191 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002192 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002193 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002194 spin_unlock_irqrestore(&phba->hbalock, iflag);
2195
dea31012005-04-17 16:05:31 -05002196 if (cmdiocbp) {
2197 if (cmdiocbp->iocb_cmpl) {
2198 /*
James Smartea2151b2008-09-07 11:52:10 -04002199 * If an ELS command failed send an event to mgmt
2200 * application.
2201 */
2202 if (saveq->iocb.ulpStatus &&
2203 (pring->ringno == LPFC_ELS_RING) &&
2204 (cmdiocbp->iocb.ulpCommand ==
2205 CMD_ELS_REQUEST64_CR))
2206 lpfc_send_els_failure_event(phba,
2207 cmdiocbp, saveq);
2208
2209 /*
dea31012005-04-17 16:05:31 -05002210 * Post all ELS completions to the worker thread.
2211 * All other are passed to the completion callback.
2212 */
2213 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04002214 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2215 cmdiocbp->iocb_flag &=
2216 ~LPFC_DRIVER_ABORTED;
2217 saveq->iocb.ulpStatus =
2218 IOSTAT_LOCAL_REJECT;
2219 saveq->iocb.un.ulpWord[4] =
2220 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002221
2222 /* Firmware could still be in progress
2223 * of DMAing payload, so don't free data
2224 * buffer till after a hbeat.
2225 */
2226 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04002227 }
dea31012005-04-17 16:05:31 -05002228 }
James Smart2e0fef82007-06-17 19:56:36 -05002229 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002230 } else
2231 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002232 } else {
2233 /*
2234 * Unknown initiating command based on the response iotag.
2235 * This could be the case on the ELS ring because of
2236 * lpfc_els_abort().
2237 */
2238 if (pring->ringno != LPFC_ELS_RING) {
2239 /*
2240 * Ring <ringno> handler: unexpected completion IoTag
2241 * <IoTag>
2242 */
James Smarta257bf92009-04-06 18:48:10 -04002243 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002244 "0322 Ring %d handler: "
2245 "unexpected completion IoTag x%x "
2246 "Data: x%x x%x x%x x%x\n",
2247 pring->ringno,
2248 saveq->iocb.ulpIoTag,
2249 saveq->iocb.ulpStatus,
2250 saveq->iocb.un.ulpWord[4],
2251 saveq->iocb.ulpCommand,
2252 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002253 }
2254 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002255
dea31012005-04-17 16:05:31 -05002256 return rc;
2257}
2258
James Smarte59058c2008-08-24 21:49:00 -04002259/**
James Smart3621a712009-04-06 18:47:14 -04002260 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002261 * @phba: Pointer to HBA context object.
2262 * @pring: Pointer to driver SLI ring object.
2263 *
2264 * This function is called from the iocb ring event handlers when
2265 * put pointer is ahead of the get pointer for a ring. This function signal
2266 * an error attention condition to the worker thread and the worker
2267 * thread will transition the HBA to offline state.
2268 **/
James Smart2e0fef82007-06-17 19:56:36 -05002269static void
2270lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002271{
James Smart34b02dc2008-08-24 21:49:55 -04002272 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002273 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002274 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002275 * rsp ring <portRspMax>
2276 */
2277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002278 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002279 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002280 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002281 pring->numRiocb);
2282
James Smart2e0fef82007-06-17 19:56:36 -05002283 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002284
2285 /*
2286 * All error attention handlers are posted to
2287 * worker thread
2288 */
2289 phba->work_ha |= HA_ERATT;
2290 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002291
James Smart5e9d9b82008-06-14 22:52:53 -04002292 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002293
2294 return;
2295}
2296
James Smarte59058c2008-08-24 21:49:00 -04002297/**
James Smart3621a712009-04-06 18:47:14 -04002298 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002299 * @ptr: Pointer to address of HBA context object.
2300 *
2301 * This function is invoked by the Error Attention polling timer when the
2302 * timer times out. It will check the SLI Error Attention register for
2303 * possible attention events. If so, it will post an Error Attention event
2304 * and wake up worker thread to process it. Otherwise, it will set up the
2305 * Error Attention polling timer for the next poll.
2306 **/
2307void lpfc_poll_eratt(unsigned long ptr)
2308{
2309 struct lpfc_hba *phba;
2310 uint32_t eratt = 0;
2311
2312 phba = (struct lpfc_hba *)ptr;
2313
2314 /* Check chip HA register for error event */
2315 eratt = lpfc_sli_check_eratt(phba);
2316
2317 if (eratt)
2318 /* Tell the worker thread there is work to do */
2319 lpfc_worker_wake_up(phba);
2320 else
2321 /* Restart the timer for next eratt poll */
2322 mod_timer(&phba->eratt_poll, jiffies +
2323 HZ * LPFC_ERATT_POLL_INTERVAL);
2324 return;
2325}
2326
2327/**
James Smart3621a712009-04-06 18:47:14 -04002328 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
James Smarte59058c2008-08-24 21:49:00 -04002329 * @phba: Pointer to HBA context object.
2330 *
2331 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
2332 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
2333 * is enabled.
2334 *
2335 * The caller does not hold any lock.
2336 * The function processes each response iocb in the response ring until it
2337 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2338 * LE bit set. The function will call the completion handler of the command iocb
2339 * if the response iocb indicates a completion for a command iocb or it is
2340 * an abort completion.
2341 **/
James Smart2e0fef82007-06-17 19:56:36 -05002342void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002343{
James Smart2e0fef82007-06-17 19:56:36 -05002344 struct lpfc_sli *psli = &phba->sli;
2345 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002346 IOCB_t *irsp = NULL;
2347 IOCB_t *entry = NULL;
2348 struct lpfc_iocbq *cmdiocbq = NULL;
2349 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04002350 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002351 uint32_t status;
2352 uint32_t portRspPut, portRspMax;
2353 int type;
2354 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002355 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05002356 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002357
2358 pring->stats.iocb_event++;
2359
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002360 /*
2361 * The next available response entry should never exceed the maximum
2362 * entries. If it does, treat it as an adapter hardware error.
2363 */
2364 portRspMax = pring->numRiocb;
2365 portRspPut = le32_to_cpu(pgp->rspPutInx);
2366 if (unlikely(portRspPut >= portRspMax)) {
2367 lpfc_sli_rsp_pointers_error(phba, pring);
2368 return;
2369 }
2370
2371 rmb();
2372 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05002373 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002374 if (++pring->rspidx >= portRspMax)
2375 pring->rspidx = 0;
2376
2377 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2378 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05002379 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002380 irsp = &rspiocbq.iocb;
2381 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2382 pring->stats.iocb_rsp++;
2383 rsp_cmpl++;
2384
2385 if (unlikely(irsp->ulpStatus)) {
2386 /* Rsp ring <ringno> error: IOCB */
2387 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002388 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002389 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002390 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002391 irsp->un.ulpWord[0],
2392 irsp->un.ulpWord[1],
2393 irsp->un.ulpWord[2],
2394 irsp->un.ulpWord[3],
2395 irsp->un.ulpWord[4],
2396 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002397 *(uint32_t *)&irsp->un1,
2398 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002399 }
2400
2401 switch (type) {
2402 case LPFC_ABORT_IOCB:
2403 case LPFC_SOL_IOCB:
2404 /*
2405 * Idle exchange closed via ABTS from port. No iocb
2406 * resources need to be recovered.
2407 */
2408 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002409 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002410 "0314 IOCB cmd 0x%x "
2411 "processed. Skipping "
2412 "completion",
James Smartdca94792006-08-01 07:34:08 -04002413 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002414 break;
2415 }
2416
James Smart2e0fef82007-06-17 19:56:36 -05002417 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002418 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2419 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002420 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002421 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2422 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2423 &rspiocbq);
2424 }
2425 break;
2426 default:
2427 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2428 char adaptermsg[LPFC_MAX_ADPTMSG];
2429 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2430 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2431 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002432 dev_warn(&((phba->pcidev)->dev),
2433 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002434 phba->brd_no, adaptermsg);
2435 } else {
2436 /* Unknown IOCB command */
2437 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002438 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002439 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002440 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002441 irsp->ulpStatus,
2442 irsp->ulpIoTag,
2443 irsp->ulpContext);
2444 }
2445 break;
2446 }
2447
2448 /*
2449 * The response IOCB has been processed. Update the ring
2450 * pointer in SLIM. If the port response put pointer has not
2451 * been updated, sync the pgp->rspPutInx and fetch the new port
2452 * response put pointer.
2453 */
James Smarted957682007-06-17 19:56:37 -05002454 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002455
2456 if (pring->rspidx == portRspPut)
2457 portRspPut = le32_to_cpu(pgp->rspPutInx);
2458 }
2459
2460 ha_copy = readl(phba->HAregaddr);
2461 ha_copy >>= (LPFC_FCP_RING * 4);
2462
2463 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05002464 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002465 pring->stats.iocb_rsp_full++;
2466 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
2467 writel(status, phba->CAregaddr);
2468 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002469 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002470 }
2471 if ((ha_copy & HA_R0CE_RSP) &&
2472 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002473 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002474 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2475 pring->stats.iocb_cmd_empty++;
2476
2477 /* Force update of the local copy of cmdGetInx */
2478 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2479 lpfc_sli_resume_iocb(phba, pring);
2480
2481 if ((pring->lpfc_sli_cmd_available))
2482 (pring->lpfc_sli_cmd_available) (phba, pring);
2483
James Smart2e0fef82007-06-17 19:56:36 -05002484 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002485 }
2486
2487 return;
2488}
2489
James Smarte59058c2008-08-24 21:49:00 -04002490/**
James Smart3621a712009-04-06 18:47:14 -04002491 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002492 * @phba: Pointer to HBA context object.
2493 * @pring: Pointer to driver SLI ring object.
2494 * @mask: Host attention register mask for this ring.
2495 *
2496 * This function is called from the interrupt context when there is a ring
2497 * event for the fcp ring. The caller does not hold any lock.
2498 * The function processes each response iocb in the response ring until it
2499 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2500 * LE bit set. The function will call the completion handler of the command iocb
2501 * if the response iocb indicates a completion for a command iocb or it is
2502 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2503 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002504 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04002505 * to check it explicitly. This function always returns 1.
2506 **/
dea31012005-04-17 16:05:31 -05002507static int
James Smart2e0fef82007-06-17 19:56:36 -05002508lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2509 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002510{
James Smart34b02dc2008-08-24 21:49:55 -04002511 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002512 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002513 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002514 struct lpfc_iocbq *cmdiocbq = NULL;
2515 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002516 uint32_t status;
2517 uint32_t portRspPut, portRspMax;
2518 int rc = 1;
2519 lpfc_iocb_type type;
2520 unsigned long iflag;
2521 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002522
James Smart2e0fef82007-06-17 19:56:36 -05002523 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002524 pring->stats.iocb_event++;
2525
dea31012005-04-17 16:05:31 -05002526 /*
2527 * The next available response entry should never exceed the maximum
2528 * entries. If it does, treat it as an adapter hardware error.
2529 */
2530 portRspMax = pring->numRiocb;
2531 portRspPut = le32_to_cpu(pgp->rspPutInx);
2532 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002533 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002534 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002535 return 1;
2536 }
2537
2538 rmb();
2539 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002540 /*
2541 * Fetch an entry off the ring and copy it into a local data
2542 * structure. The copy involves a byte-swap since the
2543 * network byte order and pci byte orders are different.
2544 */
James Smarted957682007-06-17 19:56:37 -05002545 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002546 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002547
2548 if (++pring->rspidx >= portRspMax)
2549 pring->rspidx = 0;
2550
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002551 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2552 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002553 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002554 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002555 irsp = &rspiocbq.iocb;
2556
dea31012005-04-17 16:05:31 -05002557 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2558 pring->stats.iocb_rsp++;
2559 rsp_cmpl++;
2560
2561 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002562 /*
2563 * If resource errors reported from HBA, reduce
2564 * queuedepths of the SCSI device.
2565 */
2566 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2567 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2568 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002569 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002570 spin_lock_irqsave(&phba->hbalock, iflag);
2571 }
2572
dea31012005-04-17 16:05:31 -05002573 /* Rsp ring <ringno> error: IOCB */
2574 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002575 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002576 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002577 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002578 irsp->un.ulpWord[0],
2579 irsp->un.ulpWord[1],
2580 irsp->un.ulpWord[2],
2581 irsp->un.ulpWord[3],
2582 irsp->un.ulpWord[4],
2583 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002584 *(uint32_t *)&irsp->un1,
2585 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002586 }
2587
2588 switch (type) {
2589 case LPFC_ABORT_IOCB:
2590 case LPFC_SOL_IOCB:
2591 /*
2592 * Idle exchange closed via ABTS from port. No iocb
2593 * resources need to be recovered.
2594 */
2595 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002596 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002597 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002598 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002599 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002600 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002601 break;
2602 }
2603
James Bottomley604a3e32005-10-29 10:28:33 -05002604 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2605 &rspiocbq);
dea31012005-04-17 16:05:31 -05002606 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002607 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2608 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2609 &rspiocbq);
2610 } else {
James Smart2e0fef82007-06-17 19:56:36 -05002611 spin_unlock_irqrestore(&phba->hbalock,
2612 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002613 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2614 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002615 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002616 iflag);
2617 }
dea31012005-04-17 16:05:31 -05002618 }
2619 break;
James Smarta4bc3372006-12-02 13:34:16 -05002620 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002621 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002622 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002623 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002624 break;
dea31012005-04-17 16:05:31 -05002625 default:
2626 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2627 char adaptermsg[LPFC_MAX_ADPTMSG];
2628 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2629 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2630 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002631 dev_warn(&((phba->pcidev)->dev),
2632 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002633 phba->brd_no, adaptermsg);
2634 } else {
2635 /* Unknown IOCB command */
2636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002637 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002638 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002639 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002640 irsp->ulpStatus,
2641 irsp->ulpIoTag,
2642 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002643 }
2644 break;
2645 }
2646
2647 /*
2648 * The response IOCB has been processed. Update the ring
2649 * pointer in SLIM. If the port response put pointer has not
2650 * been updated, sync the pgp->rspPutInx and fetch the new port
2651 * response put pointer.
2652 */
James Smarted957682007-06-17 19:56:37 -05002653 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002654
2655 if (pring->rspidx == portRspPut)
2656 portRspPut = le32_to_cpu(pgp->rspPutInx);
2657 }
2658
2659 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2660 pring->stats.iocb_rsp_full++;
2661 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2662 writel(status, phba->CAregaddr);
2663 readl(phba->CAregaddr);
2664 }
2665 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2666 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2667 pring->stats.iocb_cmd_empty++;
2668
2669 /* Force update of the local copy of cmdGetInx */
2670 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2671 lpfc_sli_resume_iocb(phba, pring);
2672
2673 if ((pring->lpfc_sli_cmd_available))
2674 (pring->lpfc_sli_cmd_available) (phba, pring);
2675
2676 }
2677
James Smart2e0fef82007-06-17 19:56:36 -05002678 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002679 return rc;
2680}
2681
James Smarte59058c2008-08-24 21:49:00 -04002682/**
James Smart3772a992009-05-22 14:50:54 -04002683 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2684 * @phba: Pointer to HBA context object.
2685 * @pring: Pointer to driver SLI ring object.
2686 * @rspiocbp: Pointer to driver response IOCB object.
2687 *
2688 * This function is called from the worker thread when there is a slow-path
2689 * response IOCB to process. This function chains all the response iocbs until
2690 * seeing the iocb with the LE bit set. The function will call
2691 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2692 * completion of a command iocb. The function will call the
2693 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2694 * The function frees the resources or calls the completion handler if this
2695 * iocb is an abort completion. The function returns NULL when the response
2696 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2697 * this function shall chain the iocb on to the iocb_continueq and return the
2698 * response iocb passed in.
2699 **/
2700static struct lpfc_iocbq *
2701lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2702 struct lpfc_iocbq *rspiocbp)
2703{
2704 struct lpfc_iocbq *saveq;
2705 struct lpfc_iocbq *cmdiocbp;
2706 struct lpfc_iocbq *next_iocb;
2707 IOCB_t *irsp = NULL;
2708 uint32_t free_saveq;
2709 uint8_t iocb_cmd_type;
2710 lpfc_iocb_type type;
2711 unsigned long iflag;
2712 int rc;
2713
2714 spin_lock_irqsave(&phba->hbalock, iflag);
2715 /* First add the response iocb to the countinueq list */
2716 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2717 pring->iocb_continueq_cnt++;
2718
2719 /* Now, determine whetehr the list is completed for processing */
2720 irsp = &rspiocbp->iocb;
2721 if (irsp->ulpLe) {
2722 /*
2723 * By default, the driver expects to free all resources
2724 * associated with this iocb completion.
2725 */
2726 free_saveq = 1;
2727 saveq = list_get_first(&pring->iocb_continueq,
2728 struct lpfc_iocbq, list);
2729 irsp = &(saveq->iocb);
2730 list_del_init(&pring->iocb_continueq);
2731 pring->iocb_continueq_cnt = 0;
2732
2733 pring->stats.iocb_rsp++;
2734
2735 /*
2736 * If resource errors reported from HBA, reduce
2737 * queuedepths of the SCSI device.
2738 */
2739 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2740 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2741 spin_unlock_irqrestore(&phba->hbalock, iflag);
2742 phba->lpfc_rampdown_queue_depth(phba);
2743 spin_lock_irqsave(&phba->hbalock, iflag);
2744 }
2745
2746 if (irsp->ulpStatus) {
2747 /* Rsp ring <ringno> error: IOCB */
2748 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2749 "0328 Rsp Ring %d error: "
2750 "IOCB Data: "
2751 "x%x x%x x%x x%x "
2752 "x%x x%x x%x x%x "
2753 "x%x x%x x%x x%x "
2754 "x%x x%x x%x x%x\n",
2755 pring->ringno,
2756 irsp->un.ulpWord[0],
2757 irsp->un.ulpWord[1],
2758 irsp->un.ulpWord[2],
2759 irsp->un.ulpWord[3],
2760 irsp->un.ulpWord[4],
2761 irsp->un.ulpWord[5],
2762 *(((uint32_t *) irsp) + 6),
2763 *(((uint32_t *) irsp) + 7),
2764 *(((uint32_t *) irsp) + 8),
2765 *(((uint32_t *) irsp) + 9),
2766 *(((uint32_t *) irsp) + 10),
2767 *(((uint32_t *) irsp) + 11),
2768 *(((uint32_t *) irsp) + 12),
2769 *(((uint32_t *) irsp) + 13),
2770 *(((uint32_t *) irsp) + 14),
2771 *(((uint32_t *) irsp) + 15));
2772 }
2773
2774 /*
2775 * Fetch the IOCB command type and call the correct completion
2776 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2777 * get freed back to the lpfc_iocb_list by the discovery
2778 * kernel thread.
2779 */
2780 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2781 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2782 switch (type) {
2783 case LPFC_SOL_IOCB:
2784 spin_unlock_irqrestore(&phba->hbalock, iflag);
2785 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2786 spin_lock_irqsave(&phba->hbalock, iflag);
2787 break;
2788
2789 case LPFC_UNSOL_IOCB:
2790 spin_unlock_irqrestore(&phba->hbalock, iflag);
2791 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2792 spin_lock_irqsave(&phba->hbalock, iflag);
2793 if (!rc)
2794 free_saveq = 0;
2795 break;
2796
2797 case LPFC_ABORT_IOCB:
2798 cmdiocbp = NULL;
2799 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2800 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2801 saveq);
2802 if (cmdiocbp) {
2803 /* Call the specified completion routine */
2804 if (cmdiocbp->iocb_cmpl) {
2805 spin_unlock_irqrestore(&phba->hbalock,
2806 iflag);
2807 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2808 saveq);
2809 spin_lock_irqsave(&phba->hbalock,
2810 iflag);
2811 } else
2812 __lpfc_sli_release_iocbq(phba,
2813 cmdiocbp);
2814 }
2815 break;
2816
2817 case LPFC_UNKNOWN_IOCB:
2818 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2819 char adaptermsg[LPFC_MAX_ADPTMSG];
2820 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2821 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2822 MAX_MSG_DATA);
2823 dev_warn(&((phba->pcidev)->dev),
2824 "lpfc%d: %s\n",
2825 phba->brd_no, adaptermsg);
2826 } else {
2827 /* Unknown IOCB command */
2828 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2829 "0335 Unknown IOCB "
2830 "command Data: x%x "
2831 "x%x x%x x%x\n",
2832 irsp->ulpCommand,
2833 irsp->ulpStatus,
2834 irsp->ulpIoTag,
2835 irsp->ulpContext);
2836 }
2837 break;
2838 }
2839
2840 if (free_saveq) {
2841 list_for_each_entry_safe(rspiocbp, next_iocb,
2842 &saveq->list, list) {
2843 list_del(&rspiocbp->list);
2844 __lpfc_sli_release_iocbq(phba, rspiocbp);
2845 }
2846 __lpfc_sli_release_iocbq(phba, saveq);
2847 }
2848 rspiocbp = NULL;
2849 }
2850 spin_unlock_irqrestore(&phba->hbalock, iflag);
2851 return rspiocbp;
2852}
2853
2854/**
2855 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002856 * @phba: Pointer to HBA context object.
2857 * @pring: Pointer to driver SLI ring object.
2858 * @mask: Host attention register mask for this ring.
2859 *
James Smart3772a992009-05-22 14:50:54 -04002860 * This routine wraps the actual slow_ring event process routine from the
2861 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002862 **/
James Smart3772a992009-05-22 14:50:54 -04002863void
James Smart2e0fef82007-06-17 19:56:36 -05002864lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2865 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002866{
James Smart3772a992009-05-22 14:50:54 -04002867 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2868}
2869
2870/**
2871 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2872 * @phba: Pointer to HBA context object.
2873 * @pring: Pointer to driver SLI ring object.
2874 * @mask: Host attention register mask for this ring.
2875 *
2876 * This function is called from the worker thread when there is a ring event
2877 * for non-fcp rings. The caller does not hold any lock. The function will
2878 * remove each response iocb in the response ring and calls the handle
2879 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2880 **/
2881static void
2882lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2883 struct lpfc_sli_ring *pring, uint32_t mask)
2884{
James Smart34b02dc2008-08-24 21:49:55 -04002885 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002886 IOCB_t *entry;
2887 IOCB_t *irsp = NULL;
2888 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002889 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002890 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002891 uint32_t status;
dea31012005-04-17 16:05:31 -05002892
James Smart34b02dc2008-08-24 21:49:55 -04002893 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002894 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002895 pring->stats.iocb_event++;
2896
dea31012005-04-17 16:05:31 -05002897 /*
2898 * The next available response entry should never exceed the maximum
2899 * entries. If it does, treat it as an adapter hardware error.
2900 */
2901 portRspMax = pring->numRiocb;
2902 portRspPut = le32_to_cpu(pgp->rspPutInx);
2903 if (portRspPut >= portRspMax) {
2904 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002905 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002906 * rsp ring <portRspMax>
2907 */
James Smarted957682007-06-17 19:56:37 -05002908 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002909 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002910 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002911 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002912
James Smart2e0fef82007-06-17 19:56:36 -05002913 phba->link_state = LPFC_HBA_ERROR;
2914 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002915
2916 phba->work_hs = HS_FFER3;
2917 lpfc_handle_eratt(phba);
2918
James Smart3772a992009-05-22 14:50:54 -04002919 return;
dea31012005-04-17 16:05:31 -05002920 }
2921
2922 rmb();
dea31012005-04-17 16:05:31 -05002923 while (pring->rspidx != portRspPut) {
2924 /*
2925 * Build a completion list and call the appropriate handler.
2926 * The process is to get the next available response iocb, get
2927 * a free iocb from the list, copy the response data into the
2928 * free iocb, insert to the continuation list, and update the
2929 * next response index to slim. This process makes response
2930 * iocb's in the ring available to DMA as fast as possible but
2931 * pays a penalty for a copy operation. Since the iocb is
2932 * only 32 bytes, this penalty is considered small relative to
2933 * the PCI reads for register values and a slim write. When
2934 * the ulpLe field is set, the entire Command has been
2935 * received.
2936 */
James Smarted957682007-06-17 19:56:37 -05002937 entry = lpfc_resp_iocb(phba, pring);
2938
James Smart858c9f62007-06-17 19:56:39 -05002939 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002940 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002941 if (rspiocbp == NULL) {
2942 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002943 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002944 break;
2945 }
2946
James Smarted957682007-06-17 19:56:37 -05002947 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2948 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002949 irsp = &rspiocbp->iocb;
2950
2951 if (++pring->rspidx >= portRspMax)
2952 pring->rspidx = 0;
2953
James Smarta58cbd52007-08-02 11:09:43 -04002954 if (pring->ringno == LPFC_ELS_RING) {
2955 lpfc_debugfs_slow_ring_trc(phba,
2956 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2957 *(((uint32_t *) irsp) + 4),
2958 *(((uint32_t *) irsp) + 6),
2959 *(((uint32_t *) irsp) + 7));
2960 }
2961
James Smarted957682007-06-17 19:56:37 -05002962 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002963
James Smart3772a992009-05-22 14:50:54 -04002964 spin_unlock_irqrestore(&phba->hbalock, iflag);
2965 /* Handle the response IOCB */
2966 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2967 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002968
2969 /*
2970 * If the port response put pointer has not been updated, sync
2971 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2972 * response put pointer.
2973 */
2974 if (pring->rspidx == portRspPut) {
2975 portRspPut = le32_to_cpu(pgp->rspPutInx);
2976 }
2977 } /* while (pring->rspidx != portRspPut) */
2978
James Smart92d7f7b2007-06-17 19:56:38 -05002979 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002980 /* At least one response entry has been freed */
2981 pring->stats.iocb_rsp_full++;
2982 /* SET RxRE_RSP in Chip Att register */
2983 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2984 writel(status, phba->CAregaddr);
2985 readl(phba->CAregaddr); /* flush */
2986 }
2987 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2988 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2989 pring->stats.iocb_cmd_empty++;
2990
2991 /* Force update of the local copy of cmdGetInx */
2992 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2993 lpfc_sli_resume_iocb(phba, pring);
2994
2995 if ((pring->lpfc_sli_cmd_available))
2996 (pring->lpfc_sli_cmd_available) (phba, pring);
2997
2998 }
2999
James Smart2e0fef82007-06-17 19:56:36 -05003000 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003001 return;
dea31012005-04-17 16:05:31 -05003002}
3003
James Smarte59058c2008-08-24 21:49:00 -04003004/**
James Smart4f774512009-05-22 14:52:35 -04003005 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3006 * @phba: Pointer to HBA context object.
3007 * @pring: Pointer to driver SLI ring object.
3008 * @mask: Host attention register mask for this ring.
3009 *
3010 * This function is called from the worker thread when there is a pending
3011 * ELS response iocb on the driver internal slow-path response iocb worker
3012 * queue. The caller does not hold any lock. The function will remove each
3013 * response iocb from the response worker queue and calls the handle
3014 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3015 **/
3016static void
3017lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3018 struct lpfc_sli_ring *pring, uint32_t mask)
3019{
3020 struct lpfc_iocbq *irspiocbq;
3021 unsigned long iflag;
3022
3023 while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
3024 /* Get the response iocb from the head of work queue */
3025 spin_lock_irqsave(&phba->hbalock, iflag);
3026 list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
3027 irspiocbq, struct lpfc_iocbq, list);
3028 spin_unlock_irqrestore(&phba->hbalock, iflag);
3029 /* Process the response iocb */
3030 lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
3031 }
3032}
3033
3034/**
James Smart3621a712009-04-06 18:47:14 -04003035 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003036 * @phba: Pointer to HBA context object.
3037 * @pring: Pointer to driver SLI ring object.
3038 *
3039 * This function aborts all iocbs in the given ring and frees all the iocb
3040 * objects in txq. This function issues an abort iocb for all the iocb commands
3041 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3042 * the return of this function. The caller is not required to hold any locks.
3043 **/
James Smart2e0fef82007-06-17 19:56:36 -05003044void
dea31012005-04-17 16:05:31 -05003045lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3046{
James Smart2534ba72007-04-25 09:52:20 -04003047 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003048 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003049
James Smart92d7f7b2007-06-17 19:56:38 -05003050 if (pring->ringno == LPFC_ELS_RING) {
3051 lpfc_fabric_abort_hba(phba);
3052 }
3053
dea31012005-04-17 16:05:31 -05003054 /* Error everything on txq and txcmplq
3055 * First do the txq.
3056 */
James Smart2e0fef82007-06-17 19:56:36 -05003057 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003058 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003059 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003060
3061 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003062 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3063 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3064
James Smart2e0fef82007-06-17 19:56:36 -05003065 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003066
James Smarta257bf92009-04-06 18:48:10 -04003067 /* Cancel all the IOCBs from the completions list */
3068 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3069 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003070}
3071
James Smarte59058c2008-08-24 21:49:00 -04003072/**
James Smart3621a712009-04-06 18:47:14 -04003073 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003074 * @phba: Pointer to HBA context object.
3075 *
3076 * This function flushes all iocbs in the fcp ring and frees all the iocb
3077 * objects in txq and txcmplq. This function will not issue abort iocbs
3078 * for all the iocb commands in txcmplq, they will just be returned with
3079 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3080 * slot has been permanently disabled.
3081 **/
3082void
3083lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3084{
3085 LIST_HEAD(txq);
3086 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003087 struct lpfc_sli *psli = &phba->sli;
3088 struct lpfc_sli_ring *pring;
3089
3090 /* Currently, only one fcp ring */
3091 pring = &psli->ring[psli->fcp_ring];
3092
3093 spin_lock_irq(&phba->hbalock);
3094 /* Retrieve everything on txq */
3095 list_splice_init(&pring->txq, &txq);
3096 pring->txq_cnt = 0;
3097
3098 /* Retrieve everything on the txcmplq */
3099 list_splice_init(&pring->txcmplq, &txcmplq);
3100 pring->txcmplq_cnt = 0;
3101 spin_unlock_irq(&phba->hbalock);
3102
3103 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003104 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3105 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003106
3107 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003108 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3109 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003110}
3111
3112/**
James Smart3772a992009-05-22 14:50:54 -04003113 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003114 * @phba: Pointer to HBA context object.
3115 * @mask: Bit mask to be checked.
3116 *
3117 * This function reads the host status register and compares
3118 * with the provided bit mask to check if HBA completed
3119 * the restart. This function will wait in a loop for the
3120 * HBA to complete restart. If the HBA does not restart within
3121 * 15 iterations, the function will reset the HBA again. The
3122 * function returns 1 when HBA fail to restart otherwise returns
3123 * zero.
3124 **/
James Smart3772a992009-05-22 14:50:54 -04003125static int
3126lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003127{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003128 uint32_t status;
3129 int i = 0;
3130 int retval = 0;
dea31012005-04-17 16:05:31 -05003131
Jamie Wellnitz41415862006-02-28 19:25:27 -05003132 /* Read the HBA Host Status Register */
3133 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003134
Jamie Wellnitz41415862006-02-28 19:25:27 -05003135 /*
3136 * Check status register every 100ms for 5 retries, then every
3137 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3138 * every 2.5 sec for 4.
3139 * Break our of the loop if errors occurred during init.
3140 */
3141 while (((status & mask) != mask) &&
3142 !(status & HS_FFERM) &&
3143 i++ < 20) {
dea31012005-04-17 16:05:31 -05003144
Jamie Wellnitz41415862006-02-28 19:25:27 -05003145 if (i <= 5)
3146 msleep(10);
3147 else if (i <= 10)
3148 msleep(500);
3149 else
3150 msleep(2500);
dea31012005-04-17 16:05:31 -05003151
Jamie Wellnitz41415862006-02-28 19:25:27 -05003152 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003153 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003154 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003155 lpfc_sli_brdrestart(phba);
3156 }
3157 /* Read the HBA Host Status Register */
3158 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003159 }
dea31012005-04-17 16:05:31 -05003160
Jamie Wellnitz41415862006-02-28 19:25:27 -05003161 /* Check to see if any errors occurred during init */
3162 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05003163 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003164 retval = 1;
3165 }
dea31012005-04-17 16:05:31 -05003166
Jamie Wellnitz41415862006-02-28 19:25:27 -05003167 return retval;
dea31012005-04-17 16:05:31 -05003168}
3169
James Smartda0436e2009-05-22 14:51:39 -04003170/**
3171 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3172 * @phba: Pointer to HBA context object.
3173 * @mask: Bit mask to be checked.
3174 *
3175 * This function checks the host status register to check if HBA is
3176 * ready. This function will wait in a loop for the HBA to be ready
3177 * If the HBA is not ready , the function will will reset the HBA PCI
3178 * function again. The function returns 1 when HBA fail to be ready
3179 * otherwise returns zero.
3180 **/
3181static int
3182lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3183{
3184 uint32_t status;
3185 int retval = 0;
3186
3187 /* Read the HBA Host Status Register */
3188 status = lpfc_sli4_post_status_check(phba);
3189
3190 if (status) {
3191 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3192 lpfc_sli_brdrestart(phba);
3193 status = lpfc_sli4_post_status_check(phba);
3194 }
3195
3196 /* Check to see if any errors occurred during init */
3197 if (status) {
3198 phba->link_state = LPFC_HBA_ERROR;
3199 retval = 1;
3200 } else
3201 phba->sli4_hba.intr_enable = 0;
3202
3203 return retval;
3204}
3205
3206/**
3207 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3208 * @phba: Pointer to HBA context object.
3209 * @mask: Bit mask to be checked.
3210 *
3211 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3212 * from the API jump table function pointer from the lpfc_hba struct.
3213 **/
3214int
3215lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3216{
3217 return phba->lpfc_sli_brdready(phba, mask);
3218}
3219
James Smart92908312006-03-07 15:04:13 -05003220#define BARRIER_TEST_PATTERN (0xdeadbeef)
3221
James Smarte59058c2008-08-24 21:49:00 -04003222/**
James Smart3621a712009-04-06 18:47:14 -04003223 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003224 * @phba: Pointer to HBA context object.
3225 *
3226 * This function is called before resetting an HBA. This
3227 * function requests HBA to quiesce DMAs before a reset.
3228 **/
James Smart2e0fef82007-06-17 19:56:36 -05003229void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003230{
James Smart65a29c12006-07-06 15:50:50 -04003231 uint32_t __iomem *resp_buf;
3232 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003233 volatile uint32_t mbox;
3234 uint32_t hc_copy;
3235 int i;
3236 uint8_t hdrtype;
3237
3238 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3239 if (hdrtype != 0x80 ||
3240 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3241 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3242 return;
3243
3244 /*
3245 * Tell the other part of the chip to suspend temporarily all
3246 * its DMA activity.
3247 */
James Smart65a29c12006-07-06 15:50:50 -04003248 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003249
3250 /* Disable the error attention */
3251 hc_copy = readl(phba->HCregaddr);
3252 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3253 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003254 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003255
3256 if (readl(phba->HAregaddr) & HA_ERATT) {
3257 /* Clear Chip error bit */
3258 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003259 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003260 }
3261
3262 mbox = 0;
3263 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3264 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3265
3266 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003267 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003268 writel(mbox, mbox_buf);
3269
3270 for (i = 0;
3271 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3272 mdelay(1);
3273
3274 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003275 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003276 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003277 goto restore_hc;
3278 else
3279 goto clear_errat;
3280 }
3281
3282 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3283 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3284 mdelay(1);
3285
3286clear_errat:
3287
3288 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3289 mdelay(1);
3290
3291 if (readl(phba->HAregaddr) & HA_ERATT) {
3292 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003293 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003294 }
3295
3296restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003297 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003298 writel(hc_copy, phba->HCregaddr);
3299 readl(phba->HCregaddr); /* flush */
3300}
3301
James Smarte59058c2008-08-24 21:49:00 -04003302/**
James Smart3621a712009-04-06 18:47:14 -04003303 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003304 * @phba: Pointer to HBA context object.
3305 *
3306 * This function issues a kill_board mailbox command and waits for
3307 * the error attention interrupt. This function is called for stopping
3308 * the firmware processing. The caller is not required to hold any
3309 * locks. This function calls lpfc_hba_down_post function to free
3310 * any pending commands after the kill. The function will return 1 when it
3311 * fails to kill the board else will return 0.
3312 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003313int
James Smart2e0fef82007-06-17 19:56:36 -05003314lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003315{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003316 struct lpfc_sli *psli;
3317 LPFC_MBOXQ_t *pmb;
3318 uint32_t status;
3319 uint32_t ha_copy;
3320 int retval;
3321 int i = 0;
3322
3323 psli = &phba->sli;
3324
3325 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003326 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003327 "0329 Kill HBA Data: x%x x%x\n",
3328 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003329
James Smart98c9ea52007-10-27 13:37:33 -04003330 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3331 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003332 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003333
3334 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003335 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003336 status = readl(phba->HCregaddr);
3337 status &= ~HC_ERINT_ENA;
3338 writel(status, phba->HCregaddr);
3339 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003340 phba->link_flag |= LS_IGNORE_ERATT;
3341 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003342
3343 lpfc_kill_board(phba, pmb);
3344 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3345 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3346
3347 if (retval != MBX_SUCCESS) {
3348 if (retval != MBX_BUSY)
3349 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003350 spin_lock_irq(&phba->hbalock);
3351 phba->link_flag &= ~LS_IGNORE_ERATT;
3352 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003353 return 1;
3354 }
3355
James Smartf4b4c682009-05-22 14:53:12 -04003356 spin_lock_irq(&phba->hbalock);
3357 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3358 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003359
Jamie Wellnitz41415862006-02-28 19:25:27 -05003360 mempool_free(pmb, phba->mbox_mem_pool);
3361
3362 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3363 * attention every 100ms for 3 seconds. If we don't get ERATT after
3364 * 3 seconds we still set HBA_ERROR state because the status of the
3365 * board is now undefined.
3366 */
3367 ha_copy = readl(phba->HAregaddr);
3368
3369 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3370 mdelay(100);
3371 ha_copy = readl(phba->HAregaddr);
3372 }
3373
3374 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003375 if (ha_copy & HA_ERATT) {
3376 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003377 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003378 }
James Smart2e0fef82007-06-17 19:56:36 -05003379 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003380 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003381 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003382 phba->link_flag &= ~LS_IGNORE_ERATT;
3383 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003384
Jamie Wellnitz41415862006-02-28 19:25:27 -05003385 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003386 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003387
James Smart2e0fef82007-06-17 19:56:36 -05003388 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003389}
3390
James Smarte59058c2008-08-24 21:49:00 -04003391/**
James Smart3772a992009-05-22 14:50:54 -04003392 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003393 * @phba: Pointer to HBA context object.
3394 *
3395 * This function resets the HBA by writing HC_INITFF to the control
3396 * register. After the HBA resets, this function resets all the iocb ring
3397 * indices. This function disables PCI layer parity checking during
3398 * the reset.
3399 * This function returns 0 always.
3400 * The caller is not required to hold any locks.
3401 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003402int
James Smart2e0fef82007-06-17 19:56:36 -05003403lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003404{
3405 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003406 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003407 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003408 int i;
dea31012005-04-17 16:05:31 -05003409
Jamie Wellnitz41415862006-02-28 19:25:27 -05003410 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003411
Jamie Wellnitz41415862006-02-28 19:25:27 -05003412 /* Reset HBA */
3413 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003414 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003415 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003416
3417 /* perform board reset */
3418 phba->fc_eventTag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003419 phba->pport->fc_myDID = 0;
3420 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003421
Jamie Wellnitz41415862006-02-28 19:25:27 -05003422 /* Turn off parity checking and serr during the physical reset */
3423 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3424 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3425 (cfg_value &
3426 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3427
James Smart3772a992009-05-22 14:50:54 -04003428 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3429
Jamie Wellnitz41415862006-02-28 19:25:27 -05003430 /* Now toggle INITFF bit in the Host Control Register */
3431 writel(HC_INITFF, phba->HCregaddr);
3432 mdelay(1);
3433 readl(phba->HCregaddr); /* flush */
3434 writel(0, phba->HCregaddr);
3435 readl(phba->HCregaddr); /* flush */
3436
3437 /* Restore PCI cmd register */
3438 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003439
3440 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003441 for (i = 0; i < psli->num_rings; i++) {
3442 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003443 pring->flag = 0;
3444 pring->rspidx = 0;
3445 pring->next_cmdidx = 0;
3446 pring->local_getidx = 0;
3447 pring->cmdidx = 0;
3448 pring->missbufcnt = 0;
3449 }
dea31012005-04-17 16:05:31 -05003450
James Smart2e0fef82007-06-17 19:56:36 -05003451 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003452 return 0;
3453}
3454
James Smarte59058c2008-08-24 21:49:00 -04003455/**
James Smartda0436e2009-05-22 14:51:39 -04003456 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3457 * @phba: Pointer to HBA context object.
3458 *
3459 * This function resets a SLI4 HBA. This function disables PCI layer parity
3460 * checking during resets the device. The caller is not required to hold
3461 * any locks.
3462 *
3463 * This function returns 0 always.
3464 **/
3465int
3466lpfc_sli4_brdreset(struct lpfc_hba *phba)
3467{
3468 struct lpfc_sli *psli = &phba->sli;
3469 uint16_t cfg_value;
3470 uint8_t qindx;
3471
3472 /* Reset HBA */
3473 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3474 "0295 Reset HBA Data: x%x x%x\n",
3475 phba->pport->port_state, psli->sli_flag);
3476
3477 /* perform board reset */
3478 phba->fc_eventTag = 0;
3479 phba->pport->fc_myDID = 0;
3480 phba->pport->fc_prevDID = 0;
3481
3482 /* Turn off parity checking and serr during the physical reset */
3483 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3484 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3485 (cfg_value &
3486 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3487
3488 spin_lock_irq(&phba->hbalock);
3489 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3490 phba->fcf.fcf_flag = 0;
3491 /* Clean up the child queue list for the CQs */
3492 list_del_init(&phba->sli4_hba.mbx_wq->list);
3493 list_del_init(&phba->sli4_hba.els_wq->list);
3494 list_del_init(&phba->sli4_hba.hdr_rq->list);
3495 list_del_init(&phba->sli4_hba.dat_rq->list);
3496 list_del_init(&phba->sli4_hba.mbx_cq->list);
3497 list_del_init(&phba->sli4_hba.els_cq->list);
3498 list_del_init(&phba->sli4_hba.rxq_cq->list);
3499 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3500 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3501 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3502 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3503 spin_unlock_irq(&phba->hbalock);
3504
3505 /* Now physically reset the device */
3506 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3507 "0389 Performing PCI function reset!\n");
3508 /* Perform FCoE PCI function reset */
3509 lpfc_pci_function_reset(phba);
3510
3511 return 0;
3512}
3513
3514/**
3515 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003516 * @phba: Pointer to HBA context object.
3517 *
3518 * This function is called in the SLI initialization code path to
3519 * restart the HBA. The caller is not required to hold any lock.
3520 * This function writes MBX_RESTART mailbox command to the SLIM and
3521 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3522 * function to free any pending commands. The function enables
3523 * POST only during the first initialization. The function returns zero.
3524 * The function does not guarantee completion of MBX_RESTART mailbox
3525 * command before the return of this function.
3526 **/
James Smartda0436e2009-05-22 14:51:39 -04003527static int
3528lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003529{
3530 MAILBOX_t *mb;
3531 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003532 volatile uint32_t word0;
3533 void __iomem *to_slim;
3534
James Smart2e0fef82007-06-17 19:56:36 -05003535 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003536
3537 psli = &phba->sli;
3538
3539 /* Restart HBA */
3540 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003541 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003542 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003543
3544 word0 = 0;
3545 mb = (MAILBOX_t *) &word0;
3546 mb->mbxCommand = MBX_RESTART;
3547 mb->mbxHc = 1;
3548
James Smart92908312006-03-07 15:04:13 -05003549 lpfc_reset_barrier(phba);
3550
Jamie Wellnitz41415862006-02-28 19:25:27 -05003551 to_slim = phba->MBslimaddr;
3552 writel(*(uint32_t *) mb, to_slim);
3553 readl(to_slim); /* flush */
3554
3555 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003556 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003557 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003558 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003559 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003560 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003561 writel(*(uint32_t *) mb, to_slim);
3562 readl(to_slim); /* flush */
3563
3564 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003565 phba->pport->stopped = 0;
3566 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003567 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003568 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003569
James Smart64ba8812006-08-02 15:24:34 -04003570 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3571 psli->stats_start = get_seconds();
3572
James Smarteaf15d52008-12-04 22:39:29 -05003573 /* Give the INITFF and Post time to settle. */
3574 mdelay(100);
dea31012005-04-17 16:05:31 -05003575
Jamie Wellnitz41415862006-02-28 19:25:27 -05003576 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003577
3578 return 0;
3579}
3580
James Smarte59058c2008-08-24 21:49:00 -04003581/**
James Smartda0436e2009-05-22 14:51:39 -04003582 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3583 * @phba: Pointer to HBA context object.
3584 *
3585 * This function is called in the SLI initialization code path to restart
3586 * a SLI4 HBA. The caller is not required to hold any lock.
3587 * At the end of the function, it calls lpfc_hba_down_post function to
3588 * free any pending commands.
3589 **/
3590static int
3591lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3592{
3593 struct lpfc_sli *psli = &phba->sli;
3594
3595
3596 /* Restart HBA */
3597 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3598 "0296 Restart HBA Data: x%x x%x\n",
3599 phba->pport->port_state, psli->sli_flag);
3600
3601 lpfc_sli4_brdreset(phba);
3602
3603 spin_lock_irq(&phba->hbalock);
3604 phba->pport->stopped = 0;
3605 phba->link_state = LPFC_INIT_START;
3606 phba->hba_flag = 0;
3607 spin_unlock_irq(&phba->hbalock);
3608
3609 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3610 psli->stats_start = get_seconds();
3611
3612 lpfc_hba_down_post(phba);
3613
3614 return 0;
3615}
3616
3617/**
3618 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3619 * @phba: Pointer to HBA context object.
3620 *
3621 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3622 * API jump table function pointer from the lpfc_hba struct.
3623**/
3624int
3625lpfc_sli_brdrestart(struct lpfc_hba *phba)
3626{
3627 return phba->lpfc_sli_brdrestart(phba);
3628}
3629
3630/**
James Smart3621a712009-04-06 18:47:14 -04003631 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003632 * @phba: Pointer to HBA context object.
3633 *
3634 * This function is called after a HBA restart to wait for successful
3635 * restart of the HBA. Successful restart of the HBA is indicated by
3636 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3637 * iteration, the function will restart the HBA again. The function returns
3638 * zero if HBA successfully restarted else returns negative error code.
3639 **/
dea31012005-04-17 16:05:31 -05003640static int
3641lpfc_sli_chipset_init(struct lpfc_hba *phba)
3642{
3643 uint32_t status, i = 0;
3644
3645 /* Read the HBA Host Status Register */
3646 status = readl(phba->HSregaddr);
3647
3648 /* Check status register to see what current state is */
3649 i = 0;
3650 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3651
3652 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3653 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3654 * 4.
3655 */
3656 if (i++ >= 20) {
3657 /* Adapter failed to init, timeout, status reg
3658 <status> */
James Smarted957682007-06-17 19:56:37 -05003659 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003660 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003661 "timeout, status reg x%x, "
3662 "FW Data: A8 x%x AC x%x\n", status,
3663 readl(phba->MBslimaddr + 0xa8),
3664 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003665 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003666 return -ETIMEDOUT;
3667 }
3668
3669 /* Check to see if any errors occurred during init */
3670 if (status & HS_FFERM) {
3671 /* ERROR: During chipset initialization */
3672 /* Adapter failed to init, chipset, status reg
3673 <status> */
James Smarted957682007-06-17 19:56:37 -05003674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003675 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003676 "chipset, status reg x%x, "
3677 "FW Data: A8 x%x AC x%x\n", status,
3678 readl(phba->MBslimaddr + 0xa8),
3679 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003680 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003681 return -EIO;
3682 }
3683
3684 if (i <= 5) {
3685 msleep(10);
3686 } else if (i <= 10) {
3687 msleep(500);
3688 } else {
3689 msleep(2500);
3690 }
3691
3692 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003693 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003694 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003695 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003696 }
3697 /* Read the HBA Host Status Register */
3698 status = readl(phba->HSregaddr);
3699 }
3700
3701 /* Check to see if any errors occurred during init */
3702 if (status & HS_FFERM) {
3703 /* ERROR: During chipset initialization */
3704 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003706 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003707 "status reg x%x, "
3708 "FW Data: A8 x%x AC x%x\n", status,
3709 readl(phba->MBslimaddr + 0xa8),
3710 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003711 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003712 return -EIO;
3713 }
3714
3715 /* Clear all interrupt enable conditions */
3716 writel(0, phba->HCregaddr);
3717 readl(phba->HCregaddr); /* flush */
3718
3719 /* setup host attn register */
3720 writel(0xffffffff, phba->HAregaddr);
3721 readl(phba->HAregaddr); /* flush */
3722 return 0;
3723}
3724
James Smarte59058c2008-08-24 21:49:00 -04003725/**
James Smart3621a712009-04-06 18:47:14 -04003726 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003727 *
3728 * This function calculates and returns the number of HBQs required to be
3729 * configured.
3730 **/
James Smart78b2d852007-08-02 11:10:21 -04003731int
James Smarted957682007-06-17 19:56:37 -05003732lpfc_sli_hbq_count(void)
3733{
James Smart92d7f7b2007-06-17 19:56:38 -05003734 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003735}
3736
James Smarte59058c2008-08-24 21:49:00 -04003737/**
James Smart3621a712009-04-06 18:47:14 -04003738 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003739 *
3740 * This function adds the number of hbq entries in every HBQ to get
3741 * the total number of hbq entries required for the HBA and returns
3742 * the total count.
3743 **/
James Smarted957682007-06-17 19:56:37 -05003744static int
3745lpfc_sli_hbq_entry_count(void)
3746{
3747 int hbq_count = lpfc_sli_hbq_count();
3748 int count = 0;
3749 int i;
3750
3751 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003752 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003753 return count;
3754}
3755
James Smarte59058c2008-08-24 21:49:00 -04003756/**
James Smart3621a712009-04-06 18:47:14 -04003757 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003758 *
3759 * This function calculates amount of memory required for all hbq entries
3760 * to be configured and returns the total memory required.
3761 **/
dea31012005-04-17 16:05:31 -05003762int
James Smarted957682007-06-17 19:56:37 -05003763lpfc_sli_hbq_size(void)
3764{
3765 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3766}
3767
James Smarte59058c2008-08-24 21:49:00 -04003768/**
James Smart3621a712009-04-06 18:47:14 -04003769 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003770 * @phba: Pointer to HBA context object.
3771 *
3772 * This function is called during the SLI initialization to configure
3773 * all the HBQs and post buffers to the HBQ. The caller is not
3774 * required to hold any locks. This function will return zero if successful
3775 * else it will return negative error code.
3776 **/
James Smarted957682007-06-17 19:56:37 -05003777static int
3778lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3779{
3780 int hbq_count = lpfc_sli_hbq_count();
3781 LPFC_MBOXQ_t *pmb;
3782 MAILBOX_t *pmbox;
3783 uint32_t hbqno;
3784 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003785
James Smart92d7f7b2007-06-17 19:56:38 -05003786 /* Get a Mailbox buffer to setup mailbox
3787 * commands for HBA initialization
3788 */
James Smarted957682007-06-17 19:56:37 -05003789 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3790
3791 if (!pmb)
3792 return -ENOMEM;
3793
James Smart04c68492009-05-22 14:52:52 -04003794 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003795
3796 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3797 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003798 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003799
3800 hbq_entry_index = 0;
3801 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3802 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3803 phba->hbqs[hbqno].hbqPutIdx = 0;
3804 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3805 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003806 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003807 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3808 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003809 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3810
3811 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3812 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3813 mbxStatus <status>, ring <num> */
3814
3815 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003816 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003817 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003818 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003819 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003820 pmbox->mbxStatus, hbqno);
3821
3822 phba->link_state = LPFC_HBA_ERROR;
3823 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003824 return ENXIO;
3825 }
3826 }
3827 phba->hbq_count = hbq_count;
3828
James Smarted957682007-06-17 19:56:37 -05003829 mempool_free(pmb, phba->mbox_mem_pool);
3830
James Smart92d7f7b2007-06-17 19:56:38 -05003831 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003832 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3833 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003834 return 0;
3835}
3836
James Smarte59058c2008-08-24 21:49:00 -04003837/**
James Smart4f774512009-05-22 14:52:35 -04003838 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3839 * @phba: Pointer to HBA context object.
3840 *
3841 * This function is called during the SLI initialization to configure
3842 * all the HBQs and post buffers to the HBQ. The caller is not
3843 * required to hold any locks. This function will return zero if successful
3844 * else it will return negative error code.
3845 **/
3846static int
3847lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3848{
3849 phba->hbq_in_use = 1;
3850 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3851 phba->hbq_count = 1;
3852 /* Initially populate or replenish the HBQs */
3853 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3854 return 0;
3855}
3856
3857/**
James Smart3621a712009-04-06 18:47:14 -04003858 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003859 * @phba: Pointer to HBA context object.
3860 * @sli_mode: sli mode - 2/3
3861 *
3862 * This function is called by the sli intialization code path
3863 * to issue config_port mailbox command. This function restarts the
3864 * HBA firmware and issues a config_port mailbox command to configure
3865 * the SLI interface in the sli mode specified by sli_mode
3866 * variable. The caller is not required to hold any locks.
3867 * The function returns 0 if successful, else returns negative error
3868 * code.
3869 **/
James Smart93996272008-08-24 21:50:30 -04003870int
3871lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003872{
3873 LPFC_MBOXQ_t *pmb;
3874 uint32_t resetcount = 0, rc = 0, done = 0;
3875
3876 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3877 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003878 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003879 return -ENOMEM;
3880 }
3881
James Smarted957682007-06-17 19:56:37 -05003882 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003883 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003884 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003885 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003886 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003887 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003888 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003889 rc = lpfc_sli_chipset_init(phba);
3890 if (rc)
3891 break;
3892
James Smart2e0fef82007-06-17 19:56:36 -05003893 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003894 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003895 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003896 resetcount++;
3897
James Smarted957682007-06-17 19:56:37 -05003898 /* Call pre CONFIG_PORT mailbox command initialization. A
3899 * value of 0 means the call was successful. Any other
3900 * nonzero value is a failure, but if ERESTART is returned,
3901 * the driver may reset the HBA and try again.
3902 */
dea31012005-04-17 16:05:31 -05003903 rc = lpfc_config_port_prep(phba);
3904 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003905 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003906 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003907 } else if (rc)
dea31012005-04-17 16:05:31 -05003908 break;
James Smart2e0fef82007-06-17 19:56:36 -05003909 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003910 lpfc_config_port(phba, pmb);
3911 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003912 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3913 LPFC_SLI3_HBQ_ENABLED |
3914 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003915 LPFC_SLI3_INB_ENABLED |
3916 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003917 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003919 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003920 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003921 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003922 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003923 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003924 spin_unlock_irq(&phba->hbalock);
3925 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003926 } else {
3927 /* Allow asynchronous mailbox command to go through */
3928 spin_lock_irq(&phba->hbalock);
3929 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3930 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003931 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003932 }
dea31012005-04-17 16:05:31 -05003933 }
James Smarted957682007-06-17 19:56:37 -05003934 if (!done) {
3935 rc = -EINVAL;
3936 goto do_prep_failed;
3937 }
James Smart04c68492009-05-22 14:52:52 -04003938 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3939 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003940 rc = -ENXIO;
3941 goto do_prep_failed;
3942 }
James Smart04c68492009-05-22 14:52:52 -04003943 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003944 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003945 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3946 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3947 phba->max_vpi : phba->max_vports;
3948
James Smart34b02dc2008-08-24 21:49:55 -04003949 } else
3950 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003951 if (pmb->u.mb.un.varCfgPort.gdss)
3952 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3953 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003954 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003955 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003956 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003957 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003958 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003959 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003960 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3961 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3962 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3963 phba->inb_last_counter =
3964 phba->mbox->us.s3_inb_pgp.counter;
3965 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003966 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003967 phba->port_gp = phba->mbox->us.s3_pgp.port;
3968 phba->inb_ha_copy = NULL;
3969 phba->inb_counter = NULL;
3970 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003971
3972 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003973 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003974 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3975 else
3976 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3977 "0443 Adapter did not grant "
3978 "BlockGuard\n");
3979 }
James Smart34b02dc2008-08-24 21:49:55 -04003980 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003981 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003982 phba->port_gp = phba->mbox->us.s2.port;
3983 phba->inb_ha_copy = NULL;
3984 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04003985 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05003986 }
James Smart92d7f7b2007-06-17 19:56:38 -05003987do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05003988 mempool_free(pmb, phba->mbox_mem_pool);
3989 return rc;
3990}
3991
James Smarte59058c2008-08-24 21:49:00 -04003992
3993/**
James Smart3621a712009-04-06 18:47:14 -04003994 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04003995 * @phba: Pointer to HBA context object.
3996 *
3997 * This function is the main SLI intialization function. This function
3998 * is called by the HBA intialization code, HBA reset code and HBA
3999 * error attention handler code. Caller is not required to hold any
4000 * locks. This function issues config_port mailbox command to configure
4001 * the SLI, setup iocb rings and HBQ rings. In the end the function
4002 * calls the config_port_post function to issue init_link mailbox
4003 * command and to start the discovery. The function will return zero
4004 * if successful, else it will return negative error code.
4005 **/
James Smarted957682007-06-17 19:56:37 -05004006int
4007lpfc_sli_hba_setup(struct lpfc_hba *phba)
4008{
4009 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004010 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004011
4012 switch (lpfc_sli_mode) {
4013 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004014 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004015 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004016 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004017 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004018 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004019 break;
4020 }
James Smarted957682007-06-17 19:56:37 -05004021 mode = 2;
4022 break;
4023 case 0:
4024 case 3:
4025 break;
4026 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004027 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004028 "1819 Unrecognized lpfc_sli_mode "
4029 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004030
4031 break;
4032 }
4033
James Smart93996272008-08-24 21:50:30 -04004034 rc = lpfc_sli_config_port(phba, mode);
4035
James Smarted957682007-06-17 19:56:37 -05004036 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004038 "1820 Unable to select SLI-3. "
4039 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004040 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004041 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004042 if (rc)
dea31012005-04-17 16:05:31 -05004043 goto lpfc_sli_hba_setup_error;
4044
James Smarted957682007-06-17 19:56:37 -05004045 if (phba->sli_rev == 3) {
4046 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4047 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004048 } else {
4049 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4050 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004051 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004052 }
4053
4054 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004055 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4056 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004057 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004058
4059 if (rc)
4060 goto lpfc_sli_hba_setup_error;
4061
James Smart93996272008-08-24 21:50:30 -04004062 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004063 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4064 rc = lpfc_sli_hbq_setup(phba);
4065 if (rc)
4066 goto lpfc_sli_hba_setup_error;
4067 }
James Smart04c68492009-05-22 14:52:52 -04004068 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004069 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004070 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004071
4072 rc = lpfc_config_port_post(phba);
4073 if (rc)
4074 goto lpfc_sli_hba_setup_error;
4075
James Smarted957682007-06-17 19:56:37 -05004076 return rc;
4077
James Smart92d7f7b2007-06-17 19:56:38 -05004078lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004079 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05004080 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004081 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004082 return rc;
4083}
4084
James Smartda0436e2009-05-22 14:51:39 -04004085/**
4086 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4087 * @phba: Pointer to HBA context object.
4088 * @mboxq: mailbox pointer.
4089 * This function issue a dump mailbox command to read config region
4090 * 23 and parse the records in the region and populate driver
4091 * data structure.
4092 **/
4093static int
4094lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4095 LPFC_MBOXQ_t *mboxq)
4096{
4097 struct lpfc_dmabuf *mp;
4098 struct lpfc_mqe *mqe;
4099 uint32_t data_length;
4100 int rc;
4101
4102 /* Program the default value of vlan_id and fc_map */
4103 phba->valid_vlan = 0;
4104 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4105 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4106 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4107
4108 mqe = &mboxq->u.mqe;
4109 if (lpfc_dump_fcoe_param(phba, mboxq))
4110 return -ENOMEM;
4111
4112 mp = (struct lpfc_dmabuf *) mboxq->context1;
4113 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4114
4115 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4116 "(%d):2571 Mailbox cmd x%x Status x%x "
4117 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4118 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4119 "CQ: x%x x%x x%x x%x\n",
4120 mboxq->vport ? mboxq->vport->vpi : 0,
4121 bf_get(lpfc_mqe_command, mqe),
4122 bf_get(lpfc_mqe_status, mqe),
4123 mqe->un.mb_words[0], mqe->un.mb_words[1],
4124 mqe->un.mb_words[2], mqe->un.mb_words[3],
4125 mqe->un.mb_words[4], mqe->un.mb_words[5],
4126 mqe->un.mb_words[6], mqe->un.mb_words[7],
4127 mqe->un.mb_words[8], mqe->un.mb_words[9],
4128 mqe->un.mb_words[10], mqe->un.mb_words[11],
4129 mqe->un.mb_words[12], mqe->un.mb_words[13],
4130 mqe->un.mb_words[14], mqe->un.mb_words[15],
4131 mqe->un.mb_words[16], mqe->un.mb_words[50],
4132 mboxq->mcqe.word0,
4133 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4134 mboxq->mcqe.trailer);
4135
4136 if (rc) {
4137 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4138 kfree(mp);
4139 return -EIO;
4140 }
4141 data_length = mqe->un.mb_words[5];
4142 if (data_length > DMP_FCOEPARAM_RGN_SIZE)
4143 return -EIO;
4144
4145 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4146 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4147 kfree(mp);
4148 return 0;
4149}
4150
4151/**
4152 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4153 * @phba: pointer to lpfc hba data structure.
4154 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4155 * @vpd: pointer to the memory to hold resulting port vpd data.
4156 * @vpd_size: On input, the number of bytes allocated to @vpd.
4157 * On output, the number of data bytes in @vpd.
4158 *
4159 * This routine executes a READ_REV SLI4 mailbox command. In
4160 * addition, this routine gets the port vpd data.
4161 *
4162 * Return codes
4163 * 0 - sucessful
4164 * ENOMEM - could not allocated memory.
4165 **/
4166static int
4167lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4168 uint8_t *vpd, uint32_t *vpd_size)
4169{
4170 int rc = 0;
4171 uint32_t dma_size;
4172 struct lpfc_dmabuf *dmabuf;
4173 struct lpfc_mqe *mqe;
4174
4175 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4176 if (!dmabuf)
4177 return -ENOMEM;
4178
4179 /*
4180 * Get a DMA buffer for the vpd data resulting from the READ_REV
4181 * mailbox command.
4182 */
4183 dma_size = *vpd_size;
4184 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4185 dma_size,
4186 &dmabuf->phys,
4187 GFP_KERNEL);
4188 if (!dmabuf->virt) {
4189 kfree(dmabuf);
4190 return -ENOMEM;
4191 }
4192 memset(dmabuf->virt, 0, dma_size);
4193
4194 /*
4195 * The SLI4 implementation of READ_REV conflicts at word1,
4196 * bits 31:16 and SLI4 adds vpd functionality not present
4197 * in SLI3. This code corrects the conflicts.
4198 */
4199 lpfc_read_rev(phba, mboxq);
4200 mqe = &mboxq->u.mqe;
4201 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4202 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4203 mqe->un.read_rev.word1 &= 0x0000FFFF;
4204 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4205 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4206
4207 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4208 if (rc) {
4209 dma_free_coherent(&phba->pcidev->dev, dma_size,
4210 dmabuf->virt, dmabuf->phys);
4211 return -EIO;
4212 }
4213
James Smartda0436e2009-05-22 14:51:39 -04004214 /*
4215 * The available vpd length cannot be bigger than the
4216 * DMA buffer passed to the port. Catch the less than
4217 * case and update the caller's size.
4218 */
4219 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4220 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4221
4222 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4223 dma_free_coherent(&phba->pcidev->dev, dma_size,
4224 dmabuf->virt, dmabuf->phys);
4225 kfree(dmabuf);
4226 return 0;
4227}
4228
4229/**
4230 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4231 * @phba: pointer to lpfc hba data structure.
4232 *
4233 * This routine is called to explicitly arm the SLI4 device's completion and
4234 * event queues
4235 **/
4236static void
4237lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4238{
4239 uint8_t fcp_eqidx;
4240
4241 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4242 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4243 lpfc_sli4_cq_release(phba->sli4_hba.rxq_cq, LPFC_QUEUE_REARM);
4244 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4245 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4246 LPFC_QUEUE_REARM);
4247 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4248 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4249 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4250 LPFC_QUEUE_REARM);
4251}
4252
4253/**
4254 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4255 * @phba: Pointer to HBA context object.
4256 *
4257 * This function is the main SLI4 device intialization PCI function. This
4258 * function is called by the HBA intialization code, HBA reset code and
4259 * HBA error attention handler code. Caller is not required to hold any
4260 * locks.
4261 **/
4262int
4263lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4264{
4265 int rc;
4266 LPFC_MBOXQ_t *mboxq;
4267 struct lpfc_mqe *mqe;
4268 uint8_t *vpd;
4269 uint32_t vpd_size;
4270 uint32_t ftr_rsp = 0;
4271 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4272 struct lpfc_vport *vport = phba->pport;
4273 struct lpfc_dmabuf *mp;
4274
4275 /* Perform a PCI function reset to start from clean */
4276 rc = lpfc_pci_function_reset(phba);
4277 if (unlikely(rc))
4278 return -ENODEV;
4279
4280 /* Check the HBA Host Status Register for readyness */
4281 rc = lpfc_sli4_post_status_check(phba);
4282 if (unlikely(rc))
4283 return -ENODEV;
4284 else {
4285 spin_lock_irq(&phba->hbalock);
4286 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4287 spin_unlock_irq(&phba->hbalock);
4288 }
4289
4290 /*
4291 * Allocate a single mailbox container for initializing the
4292 * port.
4293 */
4294 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4295 if (!mboxq)
4296 return -ENOMEM;
4297
4298 /*
4299 * Continue initialization with default values even if driver failed
4300 * to read FCoE param config regions
4301 */
4302 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4303 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
4304 "2570 Failed to read FCoE parameters \n");
4305
4306 /* Issue READ_REV to collect vpd and FW information. */
4307 vpd_size = PAGE_SIZE;
4308 vpd = kzalloc(vpd_size, GFP_KERNEL);
4309 if (!vpd) {
4310 rc = -ENOMEM;
4311 goto out_free_mbox;
4312 }
4313
4314 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4315 if (unlikely(rc))
4316 goto out_free_vpd;
4317
4318 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004319 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4320 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4321 phba->hba_flag |= HBA_FCOE_SUPPORT;
4322 if (phba->sli_rev != LPFC_SLI_REV4 ||
4323 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004324 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4325 "0376 READ_REV Error. SLI Level %d "
4326 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004327 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004328 rc = -EIO;
4329 goto out_free_vpd;
4330 }
James Smartda0436e2009-05-22 14:51:39 -04004331 /*
4332 * Evaluate the read rev and vpd data. Populate the driver
4333 * state with the results. If this routine fails, the failure
4334 * is not fatal as the driver will use generic values.
4335 */
4336 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4337 if (unlikely(!rc)) {
4338 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4339 "0377 Error %d parsing vpd. "
4340 "Using defaults.\n", rc);
4341 rc = 0;
4342 }
4343
James Smartf1126682009-06-10 17:22:44 -04004344 /* Save information as VPD data */
4345 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4346 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4347 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4348 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4349 &mqe->un.read_rev);
4350 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4351 &mqe->un.read_rev);
4352 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4353 &mqe->un.read_rev);
4354 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4355 &mqe->un.read_rev);
4356 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4357 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4358 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4359 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4360 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4361 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4362 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4363 "(%d):0380 READ_REV Status x%x "
4364 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4365 mboxq->vport ? mboxq->vport->vpi : 0,
4366 bf_get(lpfc_mqe_status, mqe),
4367 phba->vpd.rev.opFwName,
4368 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4369 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004370
4371 /*
4372 * Discover the port's supported feature set and match it against the
4373 * hosts requests.
4374 */
4375 lpfc_request_features(phba, mboxq);
4376 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4377 if (unlikely(rc)) {
4378 rc = -EIO;
4379 goto out_free_vpd;
4380 }
4381
4382 /*
4383 * The port must support FCP initiator mode as this is the
4384 * only mode running in the host.
4385 */
4386 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4387 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4388 "0378 No support for fcpi mode.\n");
4389 ftr_rsp++;
4390 }
4391
4392 /*
4393 * If the port cannot support the host's requested features
4394 * then turn off the global config parameters to disable the
4395 * feature in the driver. This is not a fatal error.
4396 */
4397 if ((phba->cfg_enable_bg) &&
4398 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4399 ftr_rsp++;
4400
4401 if (phba->max_vpi && phba->cfg_enable_npiv &&
4402 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4403 ftr_rsp++;
4404
4405 if (ftr_rsp) {
4406 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4407 "0379 Feature Mismatch Data: x%08x %08x "
4408 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4409 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4410 phba->cfg_enable_npiv, phba->max_vpi);
4411 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4412 phba->cfg_enable_bg = 0;
4413 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4414 phba->cfg_enable_npiv = 0;
4415 }
4416
4417 /* These SLI3 features are assumed in SLI4 */
4418 spin_lock_irq(&phba->hbalock);
4419 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4420 spin_unlock_irq(&phba->hbalock);
4421
4422 /* Read the port's service parameters. */
4423 lpfc_read_sparam(phba, mboxq, vport->vpi);
4424 mboxq->vport = vport;
4425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4426 mp = (struct lpfc_dmabuf *) mboxq->context1;
4427 if (rc == MBX_SUCCESS) {
4428 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4429 rc = 0;
4430 }
4431
4432 /*
4433 * This memory was allocated by the lpfc_read_sparam routine. Release
4434 * it to the mbuf pool.
4435 */
4436 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4437 kfree(mp);
4438 mboxq->context1 = NULL;
4439 if (unlikely(rc)) {
4440 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4441 "0382 READ_SPARAM command failed "
4442 "status %d, mbxStatus x%x\n",
4443 rc, bf_get(lpfc_mqe_status, mqe));
4444 phba->link_state = LPFC_HBA_ERROR;
4445 rc = -EIO;
4446 goto out_free_vpd;
4447 }
4448
4449 if (phba->cfg_soft_wwnn)
4450 u64_to_wwn(phba->cfg_soft_wwnn,
4451 vport->fc_sparam.nodeName.u.wwn);
4452 if (phba->cfg_soft_wwpn)
4453 u64_to_wwn(phba->cfg_soft_wwpn,
4454 vport->fc_sparam.portName.u.wwn);
4455 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4456 sizeof(struct lpfc_name));
4457 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4458 sizeof(struct lpfc_name));
4459
4460 /* Update the fc_host data structures with new wwn. */
4461 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4462 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4463
4464 /* Register SGL pool to the device using non-embedded mailbox command */
4465 rc = lpfc_sli4_post_sgl_list(phba);
4466 if (unlikely(rc)) {
4467 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4468 "0582 Error %d during sgl post operation", rc);
4469 rc = -ENODEV;
4470 goto out_free_vpd;
4471 }
4472
4473 /* Register SCSI SGL pool to the device */
4474 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4475 if (unlikely(rc)) {
4476 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4477 "0383 Error %d during scsi sgl post opeation",
4478 rc);
4479 /* Some Scsi buffers were moved to the abort scsi list */
4480 /* A pci function reset will repost them */
4481 rc = -ENODEV;
4482 goto out_free_vpd;
4483 }
4484
4485 /* Post the rpi header region to the device. */
4486 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4487 if (unlikely(rc)) {
4488 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4489 "0393 Error %d during rpi post operation\n",
4490 rc);
4491 rc = -ENODEV;
4492 goto out_free_vpd;
4493 }
James Smart0c287582009-06-10 17:22:56 -04004494 if (phba->cfg_enable_fip)
4495 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
4496 else
4497 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
James Smartda0436e2009-05-22 14:51:39 -04004498
4499 /* Set up all the queues to the device */
4500 rc = lpfc_sli4_queue_setup(phba);
4501 if (unlikely(rc)) {
4502 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4503 "0381 Error %d during queue setup.\n ", rc);
4504 goto out_stop_timers;
4505 }
4506
4507 /* Arm the CQs and then EQs on device */
4508 lpfc_sli4_arm_cqeq_intr(phba);
4509
4510 /* Indicate device interrupt mode */
4511 phba->sli4_hba.intr_enable = 1;
4512
4513 /* Allow asynchronous mailbox command to go through */
4514 spin_lock_irq(&phba->hbalock);
4515 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4516 spin_unlock_irq(&phba->hbalock);
4517
4518 /* Post receive buffers to the device */
4519 lpfc_sli4_rb_setup(phba);
4520
4521 /* Start the ELS watchdog timer */
4522 /*
4523 * The driver for SLI4 is not yet ready to process timeouts
4524 * or interrupts. Once it is, the comment bars can be removed.
4525 */
4526 /* mod_timer(&vport->els_tmofunc,
4527 * jiffies + HZ * (phba->fc_ratov*2)); */
4528
4529 /* Start heart beat timer */
4530 mod_timer(&phba->hb_tmofunc,
4531 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4532 phba->hb_outstanding = 0;
4533 phba->last_completion_time = jiffies;
4534
4535 /* Start error attention (ERATT) polling timer */
4536 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4537
4538 /*
4539 * The port is ready, set the host's link state to LINK_DOWN
4540 * in preparation for link interrupts.
4541 */
4542 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4543 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4544 lpfc_set_loopback_flag(phba);
4545 /* Change driver state to LPFC_LINK_DOWN right before init link */
4546 spin_lock_irq(&phba->hbalock);
4547 phba->link_state = LPFC_LINK_DOWN;
4548 spin_unlock_irq(&phba->hbalock);
4549 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4550 if (unlikely(rc != MBX_NOT_FINISHED)) {
4551 kfree(vpd);
4552 return 0;
4553 } else
4554 rc = -EIO;
4555
4556 /* Unset all the queues set up in this routine when error out */
4557 if (rc)
4558 lpfc_sli4_queue_unset(phba);
4559
4560out_stop_timers:
4561 if (rc)
4562 lpfc_stop_hba_timers(phba);
4563out_free_vpd:
4564 kfree(vpd);
4565out_free_mbox:
4566 mempool_free(mboxq, phba->mbox_mem_pool);
4567 return rc;
4568}
James Smarte59058c2008-08-24 21:49:00 -04004569
4570/**
James Smart3621a712009-04-06 18:47:14 -04004571 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004572 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004573 *
James Smarte59058c2008-08-24 21:49:00 -04004574 * This is the callback function for mailbox timer. The mailbox
4575 * timer is armed when a new mailbox command is issued and the timer
4576 * is deleted when the mailbox complete. The function is called by
4577 * the kernel timer code when a mailbox does not complete within
4578 * expected time. This function wakes up the worker thread to
4579 * process the mailbox timeout and returns. All the processing is
4580 * done by the worker thread function lpfc_mbox_timeout_handler.
4581 **/
dea31012005-04-17 16:05:31 -05004582void
4583lpfc_mbox_timeout(unsigned long ptr)
4584{
James Smart92d7f7b2007-06-17 19:56:38 -05004585 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004586 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004587 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004588
James Smart2e0fef82007-06-17 19:56:36 -05004589 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004590 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004591 if (!tmo_posted)
4592 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4593 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4594
James Smart5e9d9b82008-06-14 22:52:53 -04004595 if (!tmo_posted)
4596 lpfc_worker_wake_up(phba);
4597 return;
dea31012005-04-17 16:05:31 -05004598}
4599
James Smarte59058c2008-08-24 21:49:00 -04004600
4601/**
James Smart3621a712009-04-06 18:47:14 -04004602 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004603 * @phba: Pointer to HBA context object.
4604 *
4605 * This function is called from worker thread when a mailbox command times out.
4606 * The caller is not required to hold any locks. This function will reset the
4607 * HBA and recover all the pending commands.
4608 **/
dea31012005-04-17 16:05:31 -05004609void
4610lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4611{
James Smart2e0fef82007-06-17 19:56:36 -05004612 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004613 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004614 struct lpfc_sli *psli = &phba->sli;
4615 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004616
James Smarta257bf92009-04-06 18:48:10 -04004617 /* Check the pmbox pointer first. There is a race condition
4618 * between the mbox timeout handler getting executed in the
4619 * worklist and the mailbox actually completing. When this
4620 * race condition occurs, the mbox_active will be NULL.
4621 */
4622 spin_lock_irq(&phba->hbalock);
4623 if (pmbox == NULL) {
4624 lpfc_printf_log(phba, KERN_WARNING,
4625 LOG_MBOX | LOG_SLI,
4626 "0353 Active Mailbox cleared - mailbox timeout "
4627 "exiting\n");
4628 spin_unlock_irq(&phba->hbalock);
4629 return;
4630 }
4631
dea31012005-04-17 16:05:31 -05004632 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004633 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004634 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004635 mb->mbxCommand,
4636 phba->pport->port_state,
4637 phba->sli.sli_flag,
4638 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004639 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004640
James Smart1dcb58e2007-04-25 09:51:30 -04004641 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4642 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4643 * it to fail all oustanding SCSI IO.
4644 */
James Smart2e0fef82007-06-17 19:56:36 -05004645 spin_lock_irq(&phba->pport->work_port_lock);
4646 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4647 spin_unlock_irq(&phba->pport->work_port_lock);
4648 spin_lock_irq(&phba->hbalock);
4649 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004650 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004651 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004652
4653 pring = &psli->ring[psli->fcp_ring];
4654 lpfc_sli_abort_iocb_ring(phba, pring);
4655
4656 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004657 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004658
4659 /* Reset the HBA device */
4660 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004661}
4662
James Smarte59058c2008-08-24 21:49:00 -04004663/**
James Smart3772a992009-05-22 14:50:54 -04004664 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004665 * @phba: Pointer to HBA context object.
4666 * @pmbox: Pointer to mailbox object.
4667 * @flag: Flag indicating how the mailbox need to be processed.
4668 *
4669 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004670 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4671 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004672 * The mailbox command can be submitted in polling mode, in which case
4673 * this function will wait in a polling loop for the completion of the
4674 * mailbox.
4675 * If the mailbox is submitted in no_wait mode (not polling) the
4676 * function will submit the command and returns immediately without waiting
4677 * for the mailbox completion. The no_wait is supported only when HBA
4678 * is in SLI2/SLI3 mode - interrupts are enabled.
4679 * The SLI interface allows only one mailbox pending at a time. If the
4680 * mailbox is issued in polling mode and there is already a mailbox
4681 * pending, then the function will return an error. If the mailbox is issued
4682 * in NO_WAIT mode and there is a mailbox pending already, the function
4683 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4684 * The sli layer owns the mailbox object until the completion of mailbox
4685 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4686 * return codes the caller owns the mailbox command after the return of
4687 * the function.
4688 **/
James Smart3772a992009-05-22 14:50:54 -04004689static int
4690lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4691 uint32_t flag)
dea31012005-04-17 16:05:31 -05004692{
dea31012005-04-17 16:05:31 -05004693 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004694 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004695 uint32_t status, evtctr;
4696 uint32_t ha_copy;
4697 int i;
James Smart09372822008-01-11 01:52:54 -05004698 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004699 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004700 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004701 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004702 int processing_queue = 0;
4703
4704 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4705 if (!pmbox) {
4706 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004707 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4708 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4709 return MBX_SUCCESS;
4710 }
James Smart58da1ff2008-04-07 10:15:56 -04004711 processing_queue = 1;
4712 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4713 pmbox = lpfc_mbox_get(phba);
4714 if (!pmbox) {
4715 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4716 return MBX_SUCCESS;
4717 }
4718 }
dea31012005-04-17 16:05:31 -05004719
James Smarted957682007-06-17 19:56:37 -05004720 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004721 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004722 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004723 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004724 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004725 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004726 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004727 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004728 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004729 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004730 }
4731 }
4732
Linas Vepstas8d63f372007-02-14 14:28:36 -06004733 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004734 if (unlikely(pci_channel_offline(phba->pcidev))) {
4735 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4736 goto out_not_finished;
4737 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004738
James Smarta257bf92009-04-06 18:48:10 -04004739 /* If HBA has a deferred error attention, fail the iocb. */
4740 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4741 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4742 goto out_not_finished;
4743 }
4744
dea31012005-04-17 16:05:31 -05004745 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004746
James Smart3772a992009-05-22 14:50:54 -04004747 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004748 status = MBX_SUCCESS;
4749
James Smart2e0fef82007-06-17 19:56:36 -05004750 if (phba->link_state == LPFC_HBA_ERROR) {
4751 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004752
4753 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004754 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4755 "(%d):0311 Mailbox command x%x cannot "
4756 "issue Data: x%x x%x\n",
4757 pmbox->vport ? pmbox->vport->vpi : 0,
4758 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004759 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004760 }
4761
James Smart92908312006-03-07 15:04:13 -05004762 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4763 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004764 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004765 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4766 "(%d):2528 Mailbox command x%x cannot "
4767 "issue Data: x%x x%x\n",
4768 pmbox->vport ? pmbox->vport->vpi : 0,
4769 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004770 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004771 }
4772
dea31012005-04-17 16:05:31 -05004773 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4774 /* Polling for a mbox command when another one is already active
4775 * is not allowed in SLI. Also, the driver must have established
4776 * SLI2 mode to queue and process multiple mbox commands.
4777 */
4778
4779 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004780 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004781
4782 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004783 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4784 "(%d):2529 Mailbox command x%x "
4785 "cannot issue Data: x%x x%x\n",
4786 pmbox->vport ? pmbox->vport->vpi : 0,
4787 pmbox->u.mb.mbxCommand,
4788 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004789 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004790 }
4791
James Smart3772a992009-05-22 14:50:54 -04004792 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004793 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004794 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004795 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4796 "(%d):2530 Mailbox command x%x "
4797 "cannot issue Data: x%x x%x\n",
4798 pmbox->vport ? pmbox->vport->vpi : 0,
4799 pmbox->u.mb.mbxCommand,
4800 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004801 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004802 }
4803
dea31012005-04-17 16:05:31 -05004804 /* Another mailbox command is still being processed, queue this
4805 * command to be processed later.
4806 */
4807 lpfc_mbox_put(phba, pmbox);
4808
4809 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004810 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004811 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004812 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004813 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4814 mb->mbxCommand, phba->pport->port_state,
4815 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004816
4817 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004818 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004819
James Smart858c9f62007-06-17 19:56:39 -05004820 if (pmbox->vport) {
4821 lpfc_debugfs_disc_trc(pmbox->vport,
4822 LPFC_DISC_TRC_MBOX_VPORT,
4823 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4824 (uint32_t)mb->mbxCommand,
4825 mb->un.varWords[0], mb->un.varWords[1]);
4826 }
4827 else {
4828 lpfc_debugfs_disc_trc(phba->pport,
4829 LPFC_DISC_TRC_MBOX,
4830 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4831 (uint32_t)mb->mbxCommand,
4832 mb->un.varWords[0], mb->un.varWords[1]);
4833 }
4834
James Smart2e0fef82007-06-17 19:56:36 -05004835 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004836 }
4837
dea31012005-04-17 16:05:31 -05004838 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4839
4840 /* If we are not polling, we MUST be in SLI2 mode */
4841 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004842 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004843 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004844 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004845 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004846 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004847 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4848 "(%d):2531 Mailbox command x%x "
4849 "cannot issue Data: x%x x%x\n",
4850 pmbox->vport ? pmbox->vport->vpi : 0,
4851 pmbox->u.mb.mbxCommand,
4852 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004853 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004854 }
4855 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004856 mod_timer(&psli->mbox_tmo, (jiffies +
4857 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004858 }
4859
4860 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004861 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004862 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004863 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004864 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004865 mb->mbxCommand, phba->pport->port_state,
4866 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004867
James Smart858c9f62007-06-17 19:56:39 -05004868 if (mb->mbxCommand != MBX_HEARTBEAT) {
4869 if (pmbox->vport) {
4870 lpfc_debugfs_disc_trc(pmbox->vport,
4871 LPFC_DISC_TRC_MBOX_VPORT,
4872 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4873 (uint32_t)mb->mbxCommand,
4874 mb->un.varWords[0], mb->un.varWords[1]);
4875 }
4876 else {
4877 lpfc_debugfs_disc_trc(phba->pport,
4878 LPFC_DISC_TRC_MBOX,
4879 "MBOX Send: cmd:x%x mb:x%x x%x",
4880 (uint32_t)mb->mbxCommand,
4881 mb->un.varWords[0], mb->un.varWords[1]);
4882 }
4883 }
4884
dea31012005-04-17 16:05:31 -05004885 psli->slistat.mbox_cmd++;
4886 evtctr = psli->slistat.mbox_event;
4887
4888 /* next set own bit for the adapter and copy over command word */
4889 mb->mbxOwner = OWN_CHIP;
4890
James Smart3772a992009-05-22 14:50:54 -04004891 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004892 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004893 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004894 } else {
James Smart92908312006-03-07 15:04:13 -05004895 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004896 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004897 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004898 }
4899
4900 /* First copy mbox command data to HBA SLIM, skip past first
4901 word */
4902 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4903 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4904 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4905
4906 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004907 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004908 to_slim = phba->MBslimaddr;
4909 writel(ldata, to_slim);
4910 readl(to_slim); /* flush */
4911
4912 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4913 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004914 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004915 }
4916 }
4917
4918 wmb();
dea31012005-04-17 16:05:31 -05004919
4920 switch (flag) {
4921 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004922 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004923 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004924 /* Interrupt board to do it */
4925 writel(CA_MBATT, phba->CAregaddr);
4926 readl(phba->CAregaddr); /* flush */
4927 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004928 break;
4929
4930 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004931 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004932 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004933 /* Interrupt board to do it */
4934 writel(CA_MBATT, phba->CAregaddr);
4935 readl(phba->CAregaddr); /* flush */
4936
James Smart3772a992009-05-22 14:50:54 -04004937 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004938 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004939 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004940 word0 = le32_to_cpu(word0);
4941 } else {
4942 /* First read mbox status word */
4943 word0 = readl(phba->MBslimaddr);
4944 }
4945
4946 /* Read the HBA Host Attention Register */
4947 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004948 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4949 mb->mbxCommand) *
4950 1000) + jiffies;
4951 i = 0;
dea31012005-04-17 16:05:31 -05004952 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004953 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4954 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004955 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004956 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004957 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004958 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004959 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004960 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004961 }
4962
4963 /* Check if we took a mbox interrupt while we were
4964 polling */
4965 if (((word0 & OWN_CHIP) != OWN_CHIP)
4966 && (evtctr != psli->slistat.mbox_event))
4967 break;
4968
James Smart09372822008-01-11 01:52:54 -05004969 if (i++ > 10) {
4970 spin_unlock_irqrestore(&phba->hbalock,
4971 drvr_flag);
4972 msleep(1);
4973 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4974 }
dea31012005-04-17 16:05:31 -05004975
James Smart3772a992009-05-22 14:50:54 -04004976 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004977 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004978 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004979 word0 = le32_to_cpu(word0);
4980 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4981 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004982 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004983 /* Check real SLIM for any errors */
4984 slimword0 = readl(phba->MBslimaddr);
4985 slimmb = (MAILBOX_t *) & slimword0;
4986 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
4987 && slimmb->mbxStatus) {
4988 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04004989 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004990 word0 = slimword0;
4991 }
4992 }
4993 } else {
4994 /* First copy command data */
4995 word0 = readl(phba->MBslimaddr);
4996 }
4997 /* Read the HBA Host Attention Register */
4998 ha_copy = readl(phba->HAregaddr);
4999 }
5000
James Smart3772a992009-05-22 14:50:54 -04005001 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005002 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005003 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005004 } else {
5005 /* First copy command data */
5006 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5007 MAILBOX_CMD_SIZE);
5008 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5009 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05005010 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05005011 phba->MBslimaddr + DMP_RSP_OFFSET,
5012 mb->un.varDmp.word_cnt);
5013 }
5014 }
5015
5016 writel(HA_MBATT, phba->HAregaddr);
5017 readl(phba->HAregaddr); /* flush */
5018
5019 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5020 status = mb->mbxStatus;
5021 }
5022
James Smart2e0fef82007-06-17 19:56:36 -05005023 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5024 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005025
5026out_not_finished:
5027 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005028 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005029 lpfc_mbox_cmpl_put(phba, pmbox);
5030 }
5031 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005032}
5033
James Smarte59058c2008-08-24 21:49:00 -04005034/**
James Smartf1126682009-06-10 17:22:44 -04005035 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5036 * @phba: Pointer to HBA context object.
5037 *
5038 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5039 * the driver internal pending mailbox queue. It will then try to wait out the
5040 * possible outstanding mailbox command before return.
5041 *
5042 * Returns:
5043 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5044 * the outstanding mailbox command timed out.
5045 **/
5046static int
5047lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5048{
5049 struct lpfc_sli *psli = &phba->sli;
5050 uint8_t actcmd = MBX_HEARTBEAT;
5051 int rc = 0;
5052 unsigned long timeout;
5053
5054 /* Mark the asynchronous mailbox command posting as blocked */
5055 spin_lock_irq(&phba->hbalock);
5056 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5057 if (phba->sli.mbox_active)
5058 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5059 spin_unlock_irq(&phba->hbalock);
5060 /* Determine how long we might wait for the active mailbox
5061 * command to be gracefully completed by firmware.
5062 */
5063 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5064 jiffies;
5065 /* Wait for the outstnading mailbox command to complete */
5066 while (phba->sli.mbox_active) {
5067 /* Check active mailbox complete status every 2ms */
5068 msleep(2);
5069 if (time_after(jiffies, timeout)) {
5070 /* Timeout, marked the outstanding cmd not complete */
5071 rc = 1;
5072 break;
5073 }
5074 }
5075
5076 /* Can not cleanly block async mailbox command, fails it */
5077 if (rc) {
5078 spin_lock_irq(&phba->hbalock);
5079 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5080 spin_unlock_irq(&phba->hbalock);
5081 }
5082 return rc;
5083}
5084
5085/**
5086 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5087 * @phba: Pointer to HBA context object.
5088 *
5089 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5090 * commands from the driver internal pending mailbox queue. It makes sure
5091 * that there is no outstanding mailbox command before resuming posting
5092 * asynchronous mailbox commands. If, for any reason, there is outstanding
5093 * mailbox command, it will try to wait it out before resuming asynchronous
5094 * mailbox command posting.
5095 **/
5096static void
5097lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5098{
5099 struct lpfc_sli *psli = &phba->sli;
5100
5101 spin_lock_irq(&phba->hbalock);
5102 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5103 /* Asynchronous mailbox posting is not blocked, do nothing */
5104 spin_unlock_irq(&phba->hbalock);
5105 return;
5106 }
5107
5108 /* Outstanding synchronous mailbox command is guaranteed to be done,
5109 * successful or timeout, after timing-out the outstanding mailbox
5110 * command shall always be removed, so just unblock posting async
5111 * mailbox command and resume
5112 */
5113 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5114 spin_unlock_irq(&phba->hbalock);
5115
5116 /* wake up worker thread to post asynchronlous mailbox command */
5117 lpfc_worker_wake_up(phba);
5118}
5119
5120/**
James Smartda0436e2009-05-22 14:51:39 -04005121 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5122 * @phba: Pointer to HBA context object.
5123 * @mboxq: Pointer to mailbox object.
5124 *
5125 * The function posts a mailbox to the port. The mailbox is expected
5126 * to be comletely filled in and ready for the port to operate on it.
5127 * This routine executes a synchronous completion operation on the
5128 * mailbox by polling for its completion.
5129 *
5130 * The caller must not be holding any locks when calling this routine.
5131 *
5132 * Returns:
5133 * MBX_SUCCESS - mailbox posted successfully
5134 * Any of the MBX error values.
5135 **/
5136static int
5137lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5138{
5139 int rc = MBX_SUCCESS;
5140 unsigned long iflag;
5141 uint32_t db_ready;
5142 uint32_t mcqe_status;
5143 uint32_t mbx_cmnd;
5144 unsigned long timeout;
5145 struct lpfc_sli *psli = &phba->sli;
5146 struct lpfc_mqe *mb = &mboxq->u.mqe;
5147 struct lpfc_bmbx_create *mbox_rgn;
5148 struct dma_address *dma_address;
5149 struct lpfc_register bmbx_reg;
5150
5151 /*
5152 * Only one mailbox can be active to the bootstrap mailbox region
5153 * at a time and there is no queueing provided.
5154 */
5155 spin_lock_irqsave(&phba->hbalock, iflag);
5156 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5157 spin_unlock_irqrestore(&phba->hbalock, iflag);
5158 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5159 "(%d):2532 Mailbox command x%x (x%x) "
5160 "cannot issue Data: x%x x%x\n",
5161 mboxq->vport ? mboxq->vport->vpi : 0,
5162 mboxq->u.mb.mbxCommand,
5163 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5164 psli->sli_flag, MBX_POLL);
5165 return MBXERR_ERROR;
5166 }
5167 /* The server grabs the token and owns it until release */
5168 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5169 phba->sli.mbox_active = mboxq;
5170 spin_unlock_irqrestore(&phba->hbalock, iflag);
5171
5172 /*
5173 * Initialize the bootstrap memory region to avoid stale data areas
5174 * in the mailbox post. Then copy the caller's mailbox contents to
5175 * the bmbx mailbox region.
5176 */
5177 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5178 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5179 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5180 sizeof(struct lpfc_mqe));
5181
5182 /* Post the high mailbox dma address to the port and wait for ready. */
5183 dma_address = &phba->sli4_hba.bmbx.dma_address;
5184 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5185
5186 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5187 * 1000) + jiffies;
5188 do {
5189 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5190 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5191 if (!db_ready)
5192 msleep(2);
5193
5194 if (time_after(jiffies, timeout)) {
5195 rc = MBXERR_ERROR;
5196 goto exit;
5197 }
5198 } while (!db_ready);
5199
5200 /* Post the low mailbox dma address to the port. */
5201 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5202 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5203 * 1000) + jiffies;
5204 do {
5205 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5206 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5207 if (!db_ready)
5208 msleep(2);
5209
5210 if (time_after(jiffies, timeout)) {
5211 rc = MBXERR_ERROR;
5212 goto exit;
5213 }
5214 } while (!db_ready);
5215
5216 /*
5217 * Read the CQ to ensure the mailbox has completed.
5218 * If so, update the mailbox status so that the upper layers
5219 * can complete the request normally.
5220 */
5221 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5222 sizeof(struct lpfc_mqe));
5223 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5224 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5225 sizeof(struct lpfc_mcqe));
5226 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5227
5228 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5229 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5230 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5231 rc = MBXERR_ERROR;
5232 }
5233
5234 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5235 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5236 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5237 " x%x x%x CQ: x%x x%x x%x x%x\n",
5238 mboxq->vport ? mboxq->vport->vpi : 0,
5239 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5240 bf_get(lpfc_mqe_status, mb),
5241 mb->un.mb_words[0], mb->un.mb_words[1],
5242 mb->un.mb_words[2], mb->un.mb_words[3],
5243 mb->un.mb_words[4], mb->un.mb_words[5],
5244 mb->un.mb_words[6], mb->un.mb_words[7],
5245 mb->un.mb_words[8], mb->un.mb_words[9],
5246 mb->un.mb_words[10], mb->un.mb_words[11],
5247 mb->un.mb_words[12], mboxq->mcqe.word0,
5248 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5249 mboxq->mcqe.trailer);
5250exit:
5251 /* We are holding the token, no needed for lock when release */
5252 spin_lock_irqsave(&phba->hbalock, iflag);
5253 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5254 phba->sli.mbox_active = NULL;
5255 spin_unlock_irqrestore(&phba->hbalock, iflag);
5256 return rc;
5257}
5258
5259/**
5260 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5261 * @phba: Pointer to HBA context object.
5262 * @pmbox: Pointer to mailbox object.
5263 * @flag: Flag indicating how the mailbox need to be processed.
5264 *
5265 * This function is called by discovery code and HBA management code to submit
5266 * a mailbox command to firmware with SLI-4 interface spec.
5267 *
5268 * Return codes the caller owns the mailbox command after the return of the
5269 * function.
5270 **/
5271static int
5272lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5273 uint32_t flag)
5274{
5275 struct lpfc_sli *psli = &phba->sli;
5276 unsigned long iflags;
5277 int rc;
5278
5279 /* Detect polling mode and jump to a handler */
5280 if (!phba->sli4_hba.intr_enable) {
5281 if (flag == MBX_POLL)
5282 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5283 else
5284 rc = -EIO;
5285 if (rc != MBX_SUCCESS)
5286 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5287 "(%d):2541 Mailbox command x%x "
5288 "(x%x) cannot issue Data: x%x x%x\n",
5289 mboxq->vport ? mboxq->vport->vpi : 0,
5290 mboxq->u.mb.mbxCommand,
5291 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5292 psli->sli_flag, flag);
5293 return rc;
5294 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005295 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5296 "(%d):2542 Try to issue mailbox command "
5297 "x%x (x%x) synchronously ahead of async"
5298 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005299 mboxq->vport ? mboxq->vport->vpi : 0,
5300 mboxq->u.mb.mbxCommand,
5301 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5302 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005303 /* Try to block the asynchronous mailbox posting */
5304 rc = lpfc_sli4_async_mbox_block(phba);
5305 if (!rc) {
5306 /* Successfully blocked, now issue sync mbox cmd */
5307 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5308 if (rc != MBX_SUCCESS)
5309 lpfc_printf_log(phba, KERN_ERR,
5310 LOG_MBOX | LOG_SLI,
5311 "(%d):2597 Mailbox command "
5312 "x%x (x%x) cannot issue "
5313 "Data: x%x x%x\n",
5314 mboxq->vport ?
5315 mboxq->vport->vpi : 0,
5316 mboxq->u.mb.mbxCommand,
5317 lpfc_sli4_mbox_opcode_get(phba,
5318 mboxq),
5319 psli->sli_flag, flag);
5320 /* Unblock the async mailbox posting afterward */
5321 lpfc_sli4_async_mbox_unblock(phba);
5322 }
5323 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005324 }
5325
5326 /* Now, interrupt mode asynchrous mailbox command */
5327 rc = lpfc_mbox_cmd_check(phba, mboxq);
5328 if (rc) {
5329 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5330 "(%d):2543 Mailbox command x%x (x%x) "
5331 "cannot issue Data: x%x x%x\n",
5332 mboxq->vport ? mboxq->vport->vpi : 0,
5333 mboxq->u.mb.mbxCommand,
5334 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5335 psli->sli_flag, flag);
5336 goto out_not_finished;
5337 }
5338 rc = lpfc_mbox_dev_check(phba);
5339 if (unlikely(rc)) {
5340 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5341 "(%d):2544 Mailbox command x%x (x%x) "
5342 "cannot issue Data: x%x x%x\n",
5343 mboxq->vport ? mboxq->vport->vpi : 0,
5344 mboxq->u.mb.mbxCommand,
5345 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5346 psli->sli_flag, flag);
5347 goto out_not_finished;
5348 }
5349
5350 /* Put the mailbox command to the driver internal FIFO */
5351 psli->slistat.mbox_busy++;
5352 spin_lock_irqsave(&phba->hbalock, iflags);
5353 lpfc_mbox_put(phba, mboxq);
5354 spin_unlock_irqrestore(&phba->hbalock, iflags);
5355 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5356 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5357 "x%x (x%x) x%x x%x x%x\n",
5358 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5359 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5360 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5361 phba->pport->port_state,
5362 psli->sli_flag, MBX_NOWAIT);
5363 /* Wake up worker thread to transport mailbox command from head */
5364 lpfc_worker_wake_up(phba);
5365
5366 return MBX_BUSY;
5367
5368out_not_finished:
5369 return MBX_NOT_FINISHED;
5370}
5371
5372/**
5373 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5374 * @phba: Pointer to HBA context object.
5375 *
5376 * This function is called by worker thread to send a mailbox command to
5377 * SLI4 HBA firmware.
5378 *
5379 **/
5380int
5381lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5382{
5383 struct lpfc_sli *psli = &phba->sli;
5384 LPFC_MBOXQ_t *mboxq;
5385 int rc = MBX_SUCCESS;
5386 unsigned long iflags;
5387 struct lpfc_mqe *mqe;
5388 uint32_t mbx_cmnd;
5389
5390 /* Check interrupt mode before post async mailbox command */
5391 if (unlikely(!phba->sli4_hba.intr_enable))
5392 return MBX_NOT_FINISHED;
5393
5394 /* Check for mailbox command service token */
5395 spin_lock_irqsave(&phba->hbalock, iflags);
5396 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5397 spin_unlock_irqrestore(&phba->hbalock, iflags);
5398 return MBX_NOT_FINISHED;
5399 }
5400 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5401 spin_unlock_irqrestore(&phba->hbalock, iflags);
5402 return MBX_NOT_FINISHED;
5403 }
5404 if (unlikely(phba->sli.mbox_active)) {
5405 spin_unlock_irqrestore(&phba->hbalock, iflags);
5406 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5407 "0384 There is pending active mailbox cmd\n");
5408 return MBX_NOT_FINISHED;
5409 }
5410 /* Take the mailbox command service token */
5411 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5412
5413 /* Get the next mailbox command from head of queue */
5414 mboxq = lpfc_mbox_get(phba);
5415
5416 /* If no more mailbox command waiting for post, we're done */
5417 if (!mboxq) {
5418 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5419 spin_unlock_irqrestore(&phba->hbalock, iflags);
5420 return MBX_SUCCESS;
5421 }
5422 phba->sli.mbox_active = mboxq;
5423 spin_unlock_irqrestore(&phba->hbalock, iflags);
5424
5425 /* Check device readiness for posting mailbox command */
5426 rc = lpfc_mbox_dev_check(phba);
5427 if (unlikely(rc))
5428 /* Driver clean routine will clean up pending mailbox */
5429 goto out_not_finished;
5430
5431 /* Prepare the mbox command to be posted */
5432 mqe = &mboxq->u.mqe;
5433 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5434
5435 /* Start timer for the mbox_tmo and log some mailbox post messages */
5436 mod_timer(&psli->mbox_tmo, (jiffies +
5437 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5438
5439 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5440 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5441 "x%x x%x\n",
5442 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5443 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5444 phba->pport->port_state, psli->sli_flag);
5445
5446 if (mbx_cmnd != MBX_HEARTBEAT) {
5447 if (mboxq->vport) {
5448 lpfc_debugfs_disc_trc(mboxq->vport,
5449 LPFC_DISC_TRC_MBOX_VPORT,
5450 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5451 mbx_cmnd, mqe->un.mb_words[0],
5452 mqe->un.mb_words[1]);
5453 } else {
5454 lpfc_debugfs_disc_trc(phba->pport,
5455 LPFC_DISC_TRC_MBOX,
5456 "MBOX Send: cmd:x%x mb:x%x x%x",
5457 mbx_cmnd, mqe->un.mb_words[0],
5458 mqe->un.mb_words[1]);
5459 }
5460 }
5461 psli->slistat.mbox_cmd++;
5462
5463 /* Post the mailbox command to the port */
5464 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5465 if (rc != MBX_SUCCESS) {
5466 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5467 "(%d):2533 Mailbox command x%x (x%x) "
5468 "cannot issue Data: x%x x%x\n",
5469 mboxq->vport ? mboxq->vport->vpi : 0,
5470 mboxq->u.mb.mbxCommand,
5471 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5472 psli->sli_flag, MBX_NOWAIT);
5473 goto out_not_finished;
5474 }
5475
5476 return rc;
5477
5478out_not_finished:
5479 spin_lock_irqsave(&phba->hbalock, iflags);
5480 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5481 __lpfc_mbox_cmpl_put(phba, mboxq);
5482 /* Release the token */
5483 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5484 phba->sli.mbox_active = NULL;
5485 spin_unlock_irqrestore(&phba->hbalock, iflags);
5486
5487 return MBX_NOT_FINISHED;
5488}
5489
5490/**
5491 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5492 * @phba: Pointer to HBA context object.
5493 * @pmbox: Pointer to mailbox object.
5494 * @flag: Flag indicating how the mailbox need to be processed.
5495 *
5496 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5497 * the API jump table function pointer from the lpfc_hba struct.
5498 *
5499 * Return codes the caller owns the mailbox command after the return of the
5500 * function.
5501 **/
5502int
5503lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5504{
5505 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5506}
5507
5508/**
5509 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5510 * @phba: The hba struct for which this call is being executed.
5511 * @dev_grp: The HBA PCI-Device group number.
5512 *
5513 * This routine sets up the mbox interface API function jump table in @phba
5514 * struct.
5515 * Returns: 0 - success, -ENODEV - failure.
5516 **/
5517int
5518lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5519{
5520
5521 switch (dev_grp) {
5522 case LPFC_PCI_DEV_LP:
5523 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5524 phba->lpfc_sli_handle_slow_ring_event =
5525 lpfc_sli_handle_slow_ring_event_s3;
5526 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5527 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5528 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5529 break;
5530 case LPFC_PCI_DEV_OC:
5531 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5532 phba->lpfc_sli_handle_slow_ring_event =
5533 lpfc_sli_handle_slow_ring_event_s4;
5534 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5535 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5536 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5537 break;
5538 default:
5539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5540 "1420 Invalid HBA PCI-device group: 0x%x\n",
5541 dev_grp);
5542 return -ENODEV;
5543 break;
5544 }
5545 return 0;
5546}
5547
5548/**
James Smart3621a712009-04-06 18:47:14 -04005549 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005550 * @phba: Pointer to HBA context object.
5551 * @pring: Pointer to driver SLI ring object.
5552 * @piocb: Pointer to address of newly added command iocb.
5553 *
5554 * This function is called with hbalock held to add a command
5555 * iocb to the txq when SLI layer cannot submit the command iocb
5556 * to the ring.
5557 **/
James Smart858c9f62007-06-17 19:56:39 -05005558static void
James Smart92d7f7b2007-06-17 19:56:38 -05005559__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005560 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005561{
5562 /* Insert the caller's iocb in the txq tail for later processing. */
5563 list_add_tail(&piocb->list, &pring->txq);
5564 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005565}
5566
James Smarte59058c2008-08-24 21:49:00 -04005567/**
James Smart3621a712009-04-06 18:47:14 -04005568 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005569 * @phba: Pointer to HBA context object.
5570 * @pring: Pointer to driver SLI ring object.
5571 * @piocb: Pointer to address of newly added command iocb.
5572 *
5573 * This function is called with hbalock held before a new
5574 * iocb is submitted to the firmware. This function checks
5575 * txq to flush the iocbs in txq to Firmware before
5576 * submitting new iocbs to the Firmware.
5577 * If there are iocbs in the txq which need to be submitted
5578 * to firmware, lpfc_sli_next_iocb returns the first element
5579 * of the txq after dequeuing it from txq.
5580 * If there is no iocb in the txq then the function will return
5581 * *piocb and *piocb is set to NULL. Caller needs to check
5582 * *piocb to find if there are more commands in the txq.
5583 **/
dea31012005-04-17 16:05:31 -05005584static struct lpfc_iocbq *
5585lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005586 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005587{
5588 struct lpfc_iocbq * nextiocb;
5589
5590 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5591 if (!nextiocb) {
5592 nextiocb = *piocb;
5593 *piocb = NULL;
5594 }
5595
5596 return nextiocb;
5597}
5598
James Smarte59058c2008-08-24 21:49:00 -04005599/**
James Smart3772a992009-05-22 14:50:54 -04005600 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005601 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005602 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005603 * @piocb: Pointer to command iocb.
5604 * @flag: Flag indicating if this command can be put into txq.
5605 *
James Smart3772a992009-05-22 14:50:54 -04005606 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5607 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5608 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5609 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5610 * this function allows only iocbs for posting buffers. This function finds
5611 * next available slot in the command ring and posts the command to the
5612 * available slot and writes the port attention register to request HBA start
5613 * processing new iocb. If there is no slot available in the ring and
5614 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5615 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005616 *
James Smart3772a992009-05-22 14:50:54 -04005617 * This function is called with hbalock held. The function will return success
5618 * after it successfully submit the iocb to firmware or after adding to the
5619 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005620 **/
James Smart98c9ea52007-10-27 13:37:33 -04005621static int
James Smart3772a992009-05-22 14:50:54 -04005622__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005623 struct lpfc_iocbq *piocb, uint32_t flag)
5624{
5625 struct lpfc_iocbq *nextiocb;
5626 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005627 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005628
James Smart92d7f7b2007-06-17 19:56:38 -05005629 if (piocb->iocb_cmpl && (!piocb->vport) &&
5630 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5631 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5632 lpfc_printf_log(phba, KERN_ERR,
5633 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005634 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005635 piocb->iocb.ulpCommand);
5636 dump_stack();
5637 return IOCB_ERROR;
5638 }
5639
5640
Linas Vepstas8d63f372007-02-14 14:28:36 -06005641 /* If the PCI channel is in offline state, do not post iocbs. */
5642 if (unlikely(pci_channel_offline(phba->pcidev)))
5643 return IOCB_ERROR;
5644
James Smarta257bf92009-04-06 18:48:10 -04005645 /* If HBA has a deferred error attention, fail the iocb. */
5646 if (unlikely(phba->hba_flag & DEFER_ERATT))
5647 return IOCB_ERROR;
5648
dea31012005-04-17 16:05:31 -05005649 /*
5650 * We should never get an IOCB if we are in a < LINK_DOWN state
5651 */
James Smart2e0fef82007-06-17 19:56:36 -05005652 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005653 return IOCB_ERROR;
5654
5655 /*
5656 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005657 * outstanding event.
dea31012005-04-17 16:05:31 -05005658 */
James Smart0b727fe2007-10-27 13:37:25 -04005659 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005660 goto iocb_busy;
5661
James Smart2e0fef82007-06-17 19:56:36 -05005662 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005663 /*
James Smart2680eea2007-04-25 09:52:55 -04005664 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005665 * can be issued if the link is not up.
5666 */
5667 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005668 case CMD_GEN_REQUEST64_CR:
5669 case CMD_GEN_REQUEST64_CX:
5670 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5671 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5672 FC_FCP_CMND) ||
5673 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5674 MENLO_TRANSPORT_TYPE))
5675
5676 goto iocb_busy;
5677 break;
dea31012005-04-17 16:05:31 -05005678 case CMD_QUE_RING_BUF_CN:
5679 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005680 /*
5681 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5682 * completion, iocb_cmpl MUST be 0.
5683 */
5684 if (piocb->iocb_cmpl)
5685 piocb->iocb_cmpl = NULL;
5686 /*FALLTHROUGH*/
5687 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005688 case CMD_CLOSE_XRI_CN:
5689 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005690 break;
5691 default:
5692 goto iocb_busy;
5693 }
5694
5695 /*
5696 * For FCP commands, we must be in a state where we can process link
5697 * attention events.
5698 */
5699 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005700 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005701 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005702 }
dea31012005-04-17 16:05:31 -05005703
dea31012005-04-17 16:05:31 -05005704 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5705 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5706 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5707
5708 if (iocb)
5709 lpfc_sli_update_ring(phba, pring);
5710 else
5711 lpfc_sli_update_full_ring(phba, pring);
5712
5713 if (!piocb)
5714 return IOCB_SUCCESS;
5715
5716 goto out_busy;
5717
5718 iocb_busy:
5719 pring->stats.iocb_cmd_delay++;
5720
5721 out_busy:
5722
5723 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005724 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005725 return IOCB_SUCCESS;
5726 }
5727
5728 return IOCB_BUSY;
5729}
5730
James Smart3772a992009-05-22 14:50:54 -04005731/**
James Smart4f774512009-05-22 14:52:35 -04005732 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5733 * @phba: Pointer to HBA context object.
5734 * @piocb: Pointer to command iocb.
5735 * @sglq: Pointer to the scatter gather queue object.
5736 *
5737 * This routine converts the bpl or bde that is in the IOCB
5738 * to a sgl list for the sli4 hardware. The physical address
5739 * of the bpl/bde is converted back to a virtual address.
5740 * If the IOCB contains a BPL then the list of BDE's is
5741 * converted to sli4_sge's. If the IOCB contains a single
5742 * BDE then it is converted to a single sli_sge.
5743 * The IOCB is still in cpu endianess so the contents of
5744 * the bpl can be used without byte swapping.
5745 *
5746 * Returns valid XRI = Success, NO_XRI = Failure.
5747**/
5748static uint16_t
5749lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5750 struct lpfc_sglq *sglq)
5751{
5752 uint16_t xritag = NO_XRI;
5753 struct ulp_bde64 *bpl = NULL;
5754 struct ulp_bde64 bde;
5755 struct sli4_sge *sgl = NULL;
5756 IOCB_t *icmd;
5757 int numBdes = 0;
5758 int i = 0;
5759
5760 if (!piocbq || !sglq)
5761 return xritag;
5762
5763 sgl = (struct sli4_sge *)sglq->sgl;
5764 icmd = &piocbq->iocb;
5765 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5766 numBdes = icmd->un.genreq64.bdl.bdeSize /
5767 sizeof(struct ulp_bde64);
5768 /* The addrHigh and addrLow fields within the IOCB
5769 * have not been byteswapped yet so there is no
5770 * need to swap them back.
5771 */
5772 bpl = (struct ulp_bde64 *)
5773 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5774
5775 if (!bpl)
5776 return xritag;
5777
5778 for (i = 0; i < numBdes; i++) {
5779 /* Should already be byte swapped. */
5780 sgl->addr_hi = bpl->addrHigh;
5781 sgl->addr_lo = bpl->addrLow;
5782 /* swap the size field back to the cpu so we
5783 * can assign it to the sgl.
5784 */
5785 bde.tus.w = le32_to_cpu(bpl->tus.w);
5786 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5787 if ((i+1) == numBdes)
5788 bf_set(lpfc_sli4_sge_last, sgl, 1);
5789 else
5790 bf_set(lpfc_sli4_sge_last, sgl, 0);
5791 sgl->word2 = cpu_to_le32(sgl->word2);
5792 sgl->word3 = cpu_to_le32(sgl->word3);
5793 bpl++;
5794 sgl++;
5795 }
5796 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5797 /* The addrHigh and addrLow fields of the BDE have not
5798 * been byteswapped yet so they need to be swapped
5799 * before putting them in the sgl.
5800 */
5801 sgl->addr_hi =
5802 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5803 sgl->addr_lo =
5804 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5805 bf_set(lpfc_sli4_sge_len, sgl,
5806 icmd->un.genreq64.bdl.bdeSize);
5807 bf_set(lpfc_sli4_sge_last, sgl, 1);
5808 sgl->word2 = cpu_to_le32(sgl->word2);
5809 sgl->word3 = cpu_to_le32(sgl->word3);
5810 }
5811 return sglq->sli4_xritag;
5812}
5813
5814/**
5815 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5816 * @phba: Pointer to HBA context object.
5817 * @piocb: Pointer to command iocb.
5818 *
5819 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
5820 * distribution.
5821 *
5822 * Return: index into SLI4 fast-path FCP queue index.
5823 **/
5824static uint32_t
5825lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba, struct lpfc_iocbq *piocb)
5826{
5827 static uint32_t fcp_qidx;
5828
5829 return fcp_qidx++ % phba->cfg_fcp_wq_count;
5830}
5831
5832/**
5833 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5834 * @phba: Pointer to HBA context object.
5835 * @piocb: Pointer to command iocb.
5836 * @wqe: Pointer to the work queue entry.
5837 *
5838 * This routine converts the iocb command to its Work Queue Entry
5839 * equivalent. The wqe pointer should not have any fields set when
5840 * this routine is called because it will memcpy over them.
5841 * This routine does not set the CQ_ID or the WQEC bits in the
5842 * wqe.
5843 *
5844 * Returns: 0 = Success, IOCB_ERROR = Failure.
5845 **/
5846static int
5847lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5848 union lpfc_wqe *wqe)
5849{
5850 uint32_t payload_len = 0;
5851 uint8_t ct = 0;
5852 uint32_t fip;
5853 uint32_t abort_tag;
5854 uint8_t command_type = ELS_COMMAND_NON_FIP;
5855 uint8_t cmnd;
5856 uint16_t xritag;
5857 struct ulp_bde64 *bpl = NULL;
5858
5859 fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
5860 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005861 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005862 command_type = FCP_COMMAND;
James Smart0c287582009-06-10 17:22:56 -04005863 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
5864 command_type = ELS_COMMAND_FIP;
5865 else
5866 command_type = ELS_COMMAND_NON_FIP;
5867
James Smart4f774512009-05-22 14:52:35 -04005868 /* Some of the fields are in the right position already */
5869 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5870 abort_tag = (uint32_t) iocbq->iotag;
5871 xritag = iocbq->sli4_xritag;
5872 wqe->words[7] = 0; /* The ct field has moved so reset */
5873 /* words0-2 bpl convert bde */
5874 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5875 bpl = (struct ulp_bde64 *)
5876 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5877 if (!bpl)
5878 return IOCB_ERROR;
5879
5880 /* Should already be byte swapped. */
5881 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5882 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5883 /* swap the size field back to the cpu so we
5884 * can assign it to the sgl.
5885 */
5886 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5887 payload_len = wqe->generic.bde.tus.f.bdeSize;
5888 } else
5889 payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5890
5891 iocbq->iocb.ulpIoTag = iocbq->iotag;
5892 cmnd = iocbq->iocb.ulpCommand;
5893
5894 switch (iocbq->iocb.ulpCommand) {
5895 case CMD_ELS_REQUEST64_CR:
5896 if (!iocbq->iocb.ulpLe) {
5897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5898 "2007 Only Limited Edition cmd Format"
5899 " supported 0x%x\n",
5900 iocbq->iocb.ulpCommand);
5901 return IOCB_ERROR;
5902 }
5903 wqe->els_req.payload_len = payload_len;
5904 /* Els_reguest64 has a TMO */
5905 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5906 iocbq->iocb.ulpTimeout);
5907 /* Need a VF for word 4 set the vf bit*/
5908 bf_set(els_req64_vf, &wqe->els_req, 0);
5909 /* And a VFID for word 12 */
5910 bf_set(els_req64_vfid, &wqe->els_req, 0);
5911 /*
5912 * Set ct field to 3, indicates that the context_tag field
5913 * contains the FCFI and remote N_Port_ID is
5914 * in word 5.
5915 */
5916
5917 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5918 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5919 iocbq->iocb.ulpContext);
5920
James Smart4f774512009-05-22 14:52:35 -04005921 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5922 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5923 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5924 break;
5925 case CMD_XMIT_SEQUENCE64_CR:
5926 /* word3 iocb=io_tag32 wqe=payload_offset */
5927 /* payload offset used for multilpe outstanding
5928 * sequences on the same exchange
5929 */
5930 wqe->words[3] = 0;
5931 /* word4 relative_offset memcpy */
5932 /* word5 r_ctl/df_ctl memcpy */
5933 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5934 wqe->xmit_sequence.xmit_len = payload_len;
5935 break;
5936 case CMD_XMIT_BCAST64_CN:
5937 /* word3 iocb=iotag32 wqe=payload_len */
5938 wqe->words[3] = 0; /* no definition for this in wqe */
5939 /* word4 iocb=rsvd wqe=rsvd */
5940 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5941 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5942 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5943 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5944 break;
5945 case CMD_FCP_IWRITE64_CR:
5946 command_type = FCP_COMMAND_DATA_OUT;
5947 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5948 * confusing.
5949 * word3 is payload_len: byte offset to the sgl entry for the
5950 * fcp_command.
5951 * word4 is total xfer len, same as the IOCB->ulpParameter.
5952 * word5 is initial xfer len 0 = wait for xfer-ready
5953 */
5954
5955 /* Always wait for xfer-ready before sending data */
5956 wqe->fcp_iwrite.initial_xfer_len = 0;
5957 /* word 4 (xfer length) should have been set on the memcpy */
5958
5959 /* allow write to fall through to read */
5960 case CMD_FCP_IREAD64_CR:
5961 /* FCP_CMD is always the 1st sgl entry */
5962 wqe->fcp_iread.payload_len =
5963 payload_len + sizeof(struct fcp_rsp);
5964
5965 /* word 4 (xfer length) should have been set on the memcpy */
5966
5967 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5968 iocbq->iocb.ulpFCP2Rcvy);
5969 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5970 /* The XC bit and the XS bit are similar. The driver never
5971 * tracked whether or not the exchange was previouslly open.
5972 * XC = Exchange create, 0 is create. 1 is already open.
5973 * XS = link cmd: 1 do not close the exchange after command.
5974 * XS = 0 close exchange when command completes.
5975 * The only time we would not set the XC bit is when the XS bit
5976 * is set and we are sending our 2nd or greater command on
5977 * this exchange.
5978 */
James Smart4f774512009-05-22 14:52:35 -04005979 /* Always open the exchange */
5980 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5981
5982 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5983 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04005984 break;
5985 case CMD_FCP_ICMND64_CR:
5986 /* Always open the exchange */
5987 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5988
5989 wqe->words[4] = 0;
5990 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
5991 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04005992 break;
5993 case CMD_GEN_REQUEST64_CR:
5994 /* word3 command length is described as byte offset to the
5995 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
5996 * sgl[0] = cmnd
5997 * sgl[1] = rsp.
5998 *
5999 */
6000 wqe->gen_req.command_len = payload_len;
6001 /* Word4 parameter copied in the memcpy */
6002 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6003 /* word6 context tag copied in memcpy */
6004 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6005 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6006 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6007 "2015 Invalid CT %x command 0x%x\n",
6008 ct, iocbq->iocb.ulpCommand);
6009 return IOCB_ERROR;
6010 }
6011 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6012 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6013 iocbq->iocb.ulpTimeout);
6014
6015 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6016 command_type = OTHER_COMMAND;
6017 break;
6018 case CMD_XMIT_ELS_RSP64_CX:
6019 /* words0-2 BDE memcpy */
6020 /* word3 iocb=iotag32 wqe=rsvd */
6021 wqe->words[3] = 0;
6022 /* word4 iocb=did wge=rsvd. */
6023 wqe->words[4] = 0;
6024 /* word5 iocb=rsvd wge=did */
6025 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6026 iocbq->iocb.un.elsreq64.remoteID);
6027
6028 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6029 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6030
6031 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6032 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6033 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6034 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6035 iocbq->vport->vpi + phba->vpi_base);
6036 command_type = OTHER_COMMAND;
6037 break;
6038 case CMD_CLOSE_XRI_CN:
6039 case CMD_ABORT_XRI_CN:
6040 case CMD_ABORT_XRI_CX:
6041 /* words 0-2 memcpy should be 0 rserved */
6042 /* port will send abts */
6043 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6044 /*
6045 * The link is down so the fw does not need to send abts
6046 * on the wire.
6047 */
6048 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6049 else
6050 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6051 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6052 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6053 wqe->words[5] = 0;
6054 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6055 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6056 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6057 wqe->generic.abort_tag = abort_tag;
6058 /*
6059 * The abort handler will send us CMD_ABORT_XRI_CN or
6060 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6061 */
6062 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6063 cmnd = CMD_ABORT_XRI_CX;
6064 command_type = OTHER_COMMAND;
6065 xritag = 0;
6066 break;
6067 case CMD_XRI_ABORTED_CX:
6068 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6069 /* words0-2 are all 0's no bde */
6070 /* word3 and word4 are rsvrd */
6071 wqe->words[3] = 0;
6072 wqe->words[4] = 0;
6073 /* word5 iocb=rsvd wge=did */
6074 /* There is no remote port id in the IOCB? */
6075 /* Let this fall through and fail */
6076 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6077 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6078 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6079 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6080 default:
6081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6082 "2014 Invalid command 0x%x\n",
6083 iocbq->iocb.ulpCommand);
6084 return IOCB_ERROR;
6085 break;
6086
6087 }
6088 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6089 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6090 wqe->generic.abort_tag = abort_tag;
6091 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6092 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6093 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6094 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6095
6096 return 0;
6097}
6098
6099/**
6100 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6101 * @phba: Pointer to HBA context object.
6102 * @ring_number: SLI ring number to issue iocb on.
6103 * @piocb: Pointer to command iocb.
6104 * @flag: Flag indicating if this command can be put into txq.
6105 *
6106 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6107 * an iocb command to an HBA with SLI-4 interface spec.
6108 *
6109 * This function is called with hbalock held. The function will return success
6110 * after it successfully submit the iocb to firmware or after adding to the
6111 * txq.
6112 **/
6113static int
6114__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6115 struct lpfc_iocbq *piocb, uint32_t flag)
6116{
6117 struct lpfc_sglq *sglq;
6118 uint16_t xritag;
6119 union lpfc_wqe wqe;
6120 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6121 uint32_t fcp_wqidx;
6122
6123 if (piocb->sli4_xritag == NO_XRI) {
6124 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6125 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6126 sglq = NULL;
6127 else {
6128 sglq = __lpfc_sli_get_sglq(phba);
6129 if (!sglq)
6130 return IOCB_ERROR;
6131 piocb->sli4_xritag = sglq->sli4_xritag;
6132 }
6133 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6134 sglq = NULL; /* These IO's already have an XRI and
6135 * a mapped sgl.
6136 */
6137 } else {
6138 /* This is a continuation of a commandi,(CX) so this
6139 * sglq is on the active list
6140 */
6141 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6142 if (!sglq)
6143 return IOCB_ERROR;
6144 }
6145
6146 if (sglq) {
6147 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6148 if (xritag != sglq->sli4_xritag)
6149 return IOCB_ERROR;
6150 }
6151
6152 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6153 return IOCB_ERROR;
6154
6155 if (piocb->iocb_flag & LPFC_IO_FCP) {
6156 fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba, piocb);
6157 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
6158 return IOCB_ERROR;
6159 } else {
6160 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6161 return IOCB_ERROR;
6162 }
6163 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6164
6165 return 0;
6166}
6167
6168/**
James Smart3772a992009-05-22 14:50:54 -04006169 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6170 *
6171 * This routine wraps the actual lockless version for issusing IOCB function
6172 * pointer from the lpfc_hba struct.
6173 *
6174 * Return codes:
6175 * IOCB_ERROR - Error
6176 * IOCB_SUCCESS - Success
6177 * IOCB_BUSY - Busy
6178 **/
6179static inline int
6180__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6181 struct lpfc_iocbq *piocb, uint32_t flag)
6182{
6183 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6184}
6185
6186/**
6187 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6188 * @phba: The hba struct for which this call is being executed.
6189 * @dev_grp: The HBA PCI-Device group number.
6190 *
6191 * This routine sets up the SLI interface API function jump table in @phba
6192 * struct.
6193 * Returns: 0 - success, -ENODEV - failure.
6194 **/
6195int
6196lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6197{
6198
6199 switch (dev_grp) {
6200 case LPFC_PCI_DEV_LP:
6201 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6202 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6203 break;
James Smart4f774512009-05-22 14:52:35 -04006204 case LPFC_PCI_DEV_OC:
6205 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6206 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6207 break;
James Smart3772a992009-05-22 14:50:54 -04006208 default:
6209 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6210 "1419 Invalid HBA PCI-device group: 0x%x\n",
6211 dev_grp);
6212 return -ENODEV;
6213 break;
6214 }
6215 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6216 return 0;
6217}
James Smart92d7f7b2007-06-17 19:56:38 -05006218
James Smarte59058c2008-08-24 21:49:00 -04006219/**
James Smart3621a712009-04-06 18:47:14 -04006220 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006221 * @phba: Pointer to HBA context object.
6222 * @pring: Pointer to driver SLI ring object.
6223 * @piocb: Pointer to command iocb.
6224 * @flag: Flag indicating if this command can be put into txq.
6225 *
6226 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6227 * function. This function gets the hbalock and calls
6228 * __lpfc_sli_issue_iocb function and will return the error returned
6229 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6230 * functions which do not hold hbalock.
6231 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006232int
James Smart3772a992009-05-22 14:50:54 -04006233lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006234 struct lpfc_iocbq *piocb, uint32_t flag)
6235{
6236 unsigned long iflags;
6237 int rc;
6238
6239 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006240 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006241 spin_unlock_irqrestore(&phba->hbalock, iflags);
6242
6243 return rc;
6244}
6245
James Smarte59058c2008-08-24 21:49:00 -04006246/**
James Smart3621a712009-04-06 18:47:14 -04006247 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006248 * @phba: Pointer to HBA context object.
6249 *
6250 * This function is called while driver attaches with the
6251 * HBA to setup the extra ring. The extra ring is used
6252 * only when driver needs to support target mode functionality
6253 * or IP over FC functionalities.
6254 *
6255 * This function is called with no lock held.
6256 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006257static int
6258lpfc_extra_ring_setup( struct lpfc_hba *phba)
6259{
6260 struct lpfc_sli *psli;
6261 struct lpfc_sli_ring *pring;
6262
6263 psli = &phba->sli;
6264
6265 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006266
6267 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006268 pring = &psli->ring[psli->fcp_ring];
6269 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6270 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6271 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6272 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6273
James Smarta4bc3372006-12-02 13:34:16 -05006274 /* and give them to the extra ring */
6275 pring = &psli->ring[psli->extra_ring];
6276
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006277 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6278 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6279 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6280 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6281
6282 /* Setup default profile for this ring */
6283 pring->iotag_max = 4096;
6284 pring->num_mask = 1;
6285 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006286 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6287 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006288 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6289 return 0;
6290}
6291
James Smarte59058c2008-08-24 21:49:00 -04006292/**
James Smart3621a712009-04-06 18:47:14 -04006293 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006294 * @phba: Pointer to HBA context object.
6295 * @pring: Pointer to driver SLI ring object.
6296 * @iocbq: Pointer to iocb object.
6297 *
6298 * This function is called by the slow ring event handler
6299 * function when there is an ASYNC event iocb in the ring.
6300 * This function is called with no lock held.
6301 * Currently this function handles only temperature related
6302 * ASYNC events. The function decodes the temperature sensor
6303 * event message and posts events for the management applications.
6304 **/
James Smart98c9ea52007-10-27 13:37:33 -04006305static void
James Smart57127f12007-10-27 13:37:05 -04006306lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6307 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6308{
6309 IOCB_t *icmd;
6310 uint16_t evt_code;
6311 uint16_t temp;
6312 struct temp_event temp_event_data;
6313 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006314 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006315
6316 icmd = &iocbq->iocb;
6317 evt_code = icmd->un.asyncstat.evt_code;
6318 temp = icmd->ulpContext;
6319
6320 if ((evt_code != ASYNC_TEMP_WARN) &&
6321 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006322 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006323 lpfc_printf_log(phba,
6324 KERN_ERR,
6325 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006326 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarta257bf92009-04-06 18:48:10 -04006327 " evt_code 0x%x \n"
6328 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6329 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6330 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6331 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006332 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006333 icmd->un.asyncstat.evt_code,
6334 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6335 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6336 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6337 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6338
James Smart57127f12007-10-27 13:37:05 -04006339 return;
6340 }
6341 temp_event_data.data = (uint32_t)temp;
6342 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6343 if (evt_code == ASYNC_TEMP_WARN) {
6344 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6345 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006346 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006347 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006348 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006349 "corrective action. temperature : %d Celsius\n",
6350 temp);
6351 }
6352 if (evt_code == ASYNC_TEMP_SAFE) {
6353 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6354 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006355 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006356 LOG_TEMP,
6357 "0340 Adapter temperature is OK now. "
6358 "temperature : %d Celsius\n",
6359 temp);
6360 }
6361
6362 /* Send temperature change event to applications */
6363 shost = lpfc_shost_from_vport(phba->pport);
6364 fc_host_post_vendor_event(shost, fc_get_event_number(),
6365 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006366 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006367
6368}
6369
6370
James Smarte59058c2008-08-24 21:49:00 -04006371/**
James Smart3621a712009-04-06 18:47:14 -04006372 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006373 * @phba: Pointer to HBA context object.
6374 *
6375 * lpfc_sli_setup sets up rings of the SLI interface with
6376 * number of iocbs per ring and iotags. This function is
6377 * called while driver attach to the HBA and before the
6378 * interrupts are enabled. So there is no need for locking.
6379 *
6380 * This function always returns 0.
6381 **/
dea31012005-04-17 16:05:31 -05006382int
6383lpfc_sli_setup(struct lpfc_hba *phba)
6384{
James Smarted957682007-06-17 19:56:37 -05006385 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006386 struct lpfc_sli *psli = &phba->sli;
6387 struct lpfc_sli_ring *pring;
6388
6389 psli->num_rings = MAX_CONFIGURED_RINGS;
6390 psli->sli_flag = 0;
6391 psli->fcp_ring = LPFC_FCP_RING;
6392 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006393 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006394
James Bottomley604a3e32005-10-29 10:28:33 -05006395 psli->iocbq_lookup = NULL;
6396 psli->iocbq_lookup_len = 0;
6397 psli->last_iotag = 0;
6398
dea31012005-04-17 16:05:31 -05006399 for (i = 0; i < psli->num_rings; i++) {
6400 pring = &psli->ring[i];
6401 switch (i) {
6402 case LPFC_FCP_RING: /* ring 0 - FCP */
6403 /* numCiocb and numRiocb are used in config_port */
6404 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6405 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6406 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6407 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6408 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6409 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006410 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006411 SLI3_IOCB_CMD_SIZE :
6412 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006413 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006414 SLI3_IOCB_RSP_SIZE :
6415 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006416 pring->iotag_ctr = 0;
6417 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006418 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006419 pring->fast_iotag = pring->iotag_max;
6420 pring->num_mask = 0;
6421 break;
James Smarta4bc3372006-12-02 13:34:16 -05006422 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006423 /* numCiocb and numRiocb are used in config_port */
6424 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6425 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006426 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006427 SLI3_IOCB_CMD_SIZE :
6428 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006429 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006430 SLI3_IOCB_RSP_SIZE :
6431 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006432 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006433 pring->num_mask = 0;
6434 break;
6435 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6436 /* numCiocb and numRiocb are used in config_port */
6437 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6438 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006439 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006440 SLI3_IOCB_CMD_SIZE :
6441 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006442 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006443 SLI3_IOCB_RSP_SIZE :
6444 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006445 pring->fast_iotag = 0;
6446 pring->iotag_ctr = 0;
6447 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006448 pring->lpfc_sli_rcv_async_status =
6449 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05006450 pring->num_mask = 4;
6451 pring->prt[0].profile = 0; /* Mask 0 */
6452 pring->prt[0].rctl = FC_ELS_REQ;
6453 pring->prt[0].type = FC_ELS_DATA;
6454 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006455 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006456 pring->prt[1].profile = 0; /* Mask 1 */
6457 pring->prt[1].rctl = FC_ELS_RSP;
6458 pring->prt[1].type = FC_ELS_DATA;
6459 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006460 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006461 pring->prt[2].profile = 0; /* Mask 2 */
6462 /* NameServer Inquiry */
6463 pring->prt[2].rctl = FC_UNSOL_CTL;
6464 /* NameServer */
6465 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
6466 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006467 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006468 pring->prt[3].profile = 0; /* Mask 3 */
6469 /* NameServer response */
6470 pring->prt[3].rctl = FC_SOL_CTL;
6471 /* NameServer */
6472 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
6473 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006474 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006475 break;
6476 }
James Smarted957682007-06-17 19:56:37 -05006477 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006478 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006479 }
James Smarted957682007-06-17 19:56:37 -05006480 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006481 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006482 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6483 "SLI2 SLIM Data: x%x x%lx\n",
6484 phba->brd_no, totiocbsize,
6485 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006486 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006487 if (phba->cfg_multi_ring_support == 2)
6488 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006489
6490 return 0;
6491}
6492
James Smarte59058c2008-08-24 21:49:00 -04006493/**
James Smart3621a712009-04-06 18:47:14 -04006494 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006495 * @phba: Pointer to HBA context object.
6496 *
6497 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6498 * ring. This function also initializes ring indices of each ring.
6499 * This function is called during the initialization of the SLI
6500 * interface of an HBA.
6501 * This function is called with no lock held and always returns
6502 * 1.
6503 **/
dea31012005-04-17 16:05:31 -05006504int
James Smart2e0fef82007-06-17 19:56:36 -05006505lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006506{
6507 struct lpfc_sli *psli;
6508 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006509 int i;
dea31012005-04-17 16:05:31 -05006510
6511 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006512 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006513 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006514 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006515 /* Initialize list headers for txq and txcmplq as double linked lists */
6516 for (i = 0; i < psli->num_rings; i++) {
6517 pring = &psli->ring[i];
6518 pring->ringno = i;
6519 pring->next_cmdidx = 0;
6520 pring->local_getidx = 0;
6521 pring->cmdidx = 0;
6522 INIT_LIST_HEAD(&pring->txq);
6523 INIT_LIST_HEAD(&pring->txcmplq);
6524 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006525 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006526 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006527 }
James Smart2e0fef82007-06-17 19:56:36 -05006528 spin_unlock_irq(&phba->hbalock);
6529 return 1;
dea31012005-04-17 16:05:31 -05006530}
6531
James Smarte59058c2008-08-24 21:49:00 -04006532/**
James Smart04c68492009-05-22 14:52:52 -04006533 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6534 * @phba: Pointer to HBA context object.
6535 *
6536 * This routine flushes the mailbox command subsystem. It will unconditionally
6537 * flush all the mailbox commands in the three possible stages in the mailbox
6538 * command sub-system: pending mailbox command queue; the outstanding mailbox
6539 * command; and completed mailbox command queue. It is caller's responsibility
6540 * to make sure that the driver is in the proper state to flush the mailbox
6541 * command sub-system. Namely, the posting of mailbox commands into the
6542 * pending mailbox command queue from the various clients must be stopped;
6543 * either the HBA is in a state that it will never works on the outstanding
6544 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6545 * mailbox command has been completed.
6546 **/
6547static void
6548lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6549{
6550 LIST_HEAD(completions);
6551 struct lpfc_sli *psli = &phba->sli;
6552 LPFC_MBOXQ_t *pmb;
6553 unsigned long iflag;
6554
6555 /* Flush all the mailbox commands in the mbox system */
6556 spin_lock_irqsave(&phba->hbalock, iflag);
6557 /* The pending mailbox command queue */
6558 list_splice_init(&phba->sli.mboxq, &completions);
6559 /* The outstanding active mailbox command */
6560 if (psli->mbox_active) {
6561 list_add_tail(&psli->mbox_active->list, &completions);
6562 psli->mbox_active = NULL;
6563 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6564 }
6565 /* The completed mailbox command queue */
6566 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6567 spin_unlock_irqrestore(&phba->hbalock, iflag);
6568
6569 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6570 while (!list_empty(&completions)) {
6571 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6572 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6573 if (pmb->mbox_cmpl)
6574 pmb->mbox_cmpl(phba, pmb);
6575 }
6576}
6577
6578/**
James Smart3621a712009-04-06 18:47:14 -04006579 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006580 * @vport: Pointer to virtual port object.
6581 *
6582 * lpfc_sli_host_down is called to clean up the resources
6583 * associated with a vport before destroying virtual
6584 * port data structures.
6585 * This function does following operations:
6586 * - Free discovery resources associated with this virtual
6587 * port.
6588 * - Free iocbs associated with this virtual port in
6589 * the txq.
6590 * - Send abort for all iocb commands associated with this
6591 * vport in txcmplq.
6592 *
6593 * This function is called with no lock held and always returns 1.
6594 **/
dea31012005-04-17 16:05:31 -05006595int
James Smart92d7f7b2007-06-17 19:56:38 -05006596lpfc_sli_host_down(struct lpfc_vport *vport)
6597{
James Smart858c9f62007-06-17 19:56:39 -05006598 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006599 struct lpfc_hba *phba = vport->phba;
6600 struct lpfc_sli *psli = &phba->sli;
6601 struct lpfc_sli_ring *pring;
6602 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006603 int i;
6604 unsigned long flags = 0;
6605 uint16_t prev_pring_flag;
6606
6607 lpfc_cleanup_discovery_resources(vport);
6608
6609 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006610 for (i = 0; i < psli->num_rings; i++) {
6611 pring = &psli->ring[i];
6612 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006613 /* Only slow rings */
6614 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006615 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006616 /* Set the lpfc data pending flag */
6617 set_bit(LPFC_DATA_READY, &phba->data_flags);
6618 }
James Smart92d7f7b2007-06-17 19:56:38 -05006619 /*
6620 * Error everything on the txq since these iocbs have not been
6621 * given to the FW yet.
6622 */
James Smart92d7f7b2007-06-17 19:56:38 -05006623 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6624 if (iocb->vport != vport)
6625 continue;
James Smart858c9f62007-06-17 19:56:39 -05006626 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006627 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006628 }
6629
6630 /* Next issue ABTS for everything on the txcmplq */
6631 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6632 list) {
6633 if (iocb->vport != vport)
6634 continue;
6635 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6636 }
6637
6638 pring->flag = prev_pring_flag;
6639 }
6640
6641 spin_unlock_irqrestore(&phba->hbalock, flags);
6642
James Smarta257bf92009-04-06 18:48:10 -04006643 /* Cancel all the IOCBs from the completions list */
6644 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6645 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006646 return 1;
6647}
6648
James Smarte59058c2008-08-24 21:49:00 -04006649/**
James Smart3621a712009-04-06 18:47:14 -04006650 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006651 * @phba: Pointer to HBA context object.
6652 *
6653 * This function cleans up all iocb, buffers, mailbox commands
6654 * while shutting down the HBA. This function is called with no
6655 * lock held and always returns 1.
6656 * This function does the following to cleanup driver resources:
6657 * - Free discovery resources for each virtual port
6658 * - Cleanup any pending fabric iocbs
6659 * - Iterate through the iocb txq and free each entry
6660 * in the list.
6661 * - Free up any buffer posted to the HBA
6662 * - Free mailbox commands in the mailbox queue.
6663 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006664int
James Smart2e0fef82007-06-17 19:56:36 -05006665lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006666{
James Smart2534ba72007-04-25 09:52:20 -04006667 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006668 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006669 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006670 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006671 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006672 int i;
6673
6674 /* Shutdown the mailbox command sub-system */
6675 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006676
dea31012005-04-17 16:05:31 -05006677 lpfc_hba_down_prep(phba);
6678
James Smart92d7f7b2007-06-17 19:56:38 -05006679 lpfc_fabric_abort_hba(phba);
6680
James Smart2e0fef82007-06-17 19:56:36 -05006681 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006682 for (i = 0; i < psli->num_rings; i++) {
6683 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006684 /* Only slow rings */
6685 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006686 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006687 /* Set the lpfc data pending flag */
6688 set_bit(LPFC_DATA_READY, &phba->data_flags);
6689 }
dea31012005-04-17 16:05:31 -05006690
6691 /*
6692 * Error everything on the txq since these iocbs have not been
6693 * given to the FW yet.
6694 */
James Smart2534ba72007-04-25 09:52:20 -04006695 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006696 pring->txq_cnt = 0;
6697
dea31012005-04-17 16:05:31 -05006698 }
James Smart2e0fef82007-06-17 19:56:36 -05006699 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006700
James Smarta257bf92009-04-06 18:48:10 -04006701 /* Cancel all the IOCBs from the completions list */
6702 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6703 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006704
James Smart0ff10d42008-01-11 01:52:36 -05006705 spin_lock_irqsave(&phba->hbalock, flags);
6706 list_splice_init(&phba->elsbuf, &completions);
6707 phba->elsbuf_cnt = 0;
6708 phba->elsbuf_prev_cnt = 0;
6709 spin_unlock_irqrestore(&phba->hbalock, flags);
6710
6711 while (!list_empty(&completions)) {
6712 list_remove_head(&completions, buf_ptr,
6713 struct lpfc_dmabuf, list);
6714 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6715 kfree(buf_ptr);
6716 }
6717
dea31012005-04-17 16:05:31 -05006718 /* Return any active mbox cmds */
6719 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006720
James Smartda0436e2009-05-22 14:51:39 -04006721 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006722 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006723 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006724
James Smartda0436e2009-05-22 14:51:39 -04006725 return 1;
6726}
James Smart92d7f7b2007-06-17 19:56:38 -05006727
James Smartda0436e2009-05-22 14:51:39 -04006728/**
6729 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6730 * @phba: Pointer to HBA context object.
6731 *
6732 * This function cleans up all queues, iocb, buffers, mailbox commands while
6733 * shutting down the SLI4 HBA FCoE function. This function is called with no
6734 * lock held and always returns 1.
6735 *
6736 * This function does the following to cleanup driver FCoE function resources:
6737 * - Free discovery resources for each virtual port
6738 * - Cleanup any pending fabric iocbs
6739 * - Iterate through the iocb txq and free each entry in the list.
6740 * - Free up any buffer posted to the HBA.
6741 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6742 * - Free mailbox commands in the mailbox queue.
6743 **/
6744int
6745lpfc_sli4_hba_down(struct lpfc_hba *phba)
6746{
6747 /* Stop the SLI4 device port */
6748 lpfc_stop_port(phba);
6749
6750 /* Tear down the queues in the HBA */
6751 lpfc_sli4_queue_unset(phba);
6752
6753 /* unregister default FCFI from the HBA */
6754 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6755
dea31012005-04-17 16:05:31 -05006756 return 1;
6757}
6758
James Smarte59058c2008-08-24 21:49:00 -04006759/**
James Smart3621a712009-04-06 18:47:14 -04006760 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006761 * @srcp: Source memory pointer.
6762 * @destp: Destination memory pointer.
6763 * @cnt: Number of words required to be copied.
6764 *
6765 * This function is used for copying data between driver memory
6766 * and the SLI memory. This function also changes the endianness
6767 * of each word if native endianness is different from SLI
6768 * endianness. This function can be called with or without
6769 * lock.
6770 **/
dea31012005-04-17 16:05:31 -05006771void
6772lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6773{
6774 uint32_t *src = srcp;
6775 uint32_t *dest = destp;
6776 uint32_t ldata;
6777 int i;
6778
6779 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6780 ldata = *src;
6781 ldata = le32_to_cpu(ldata);
6782 *dest = ldata;
6783 src++;
6784 dest++;
6785 }
6786}
6787
James Smarte59058c2008-08-24 21:49:00 -04006788
6789/**
James Smart3621a712009-04-06 18:47:14 -04006790 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006791 * @phba: Pointer to HBA context object.
6792 * @pring: Pointer to driver SLI ring object.
6793 * @mp: Pointer to driver buffer object.
6794 *
6795 * This function is called with no lock held.
6796 * It always return zero after adding the buffer to the postbufq
6797 * buffer list.
6798 **/
dea31012005-04-17 16:05:31 -05006799int
James Smart2e0fef82007-06-17 19:56:36 -05006800lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6801 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006802{
6803 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6804 later */
James Smart2e0fef82007-06-17 19:56:36 -05006805 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006806 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006807 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006808 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006809 return 0;
6810}
6811
James Smarte59058c2008-08-24 21:49:00 -04006812/**
James Smart3621a712009-04-06 18:47:14 -04006813 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006814 * @phba: Pointer to HBA context object.
6815 *
6816 * When HBQ is enabled, buffers are searched based on tags. This function
6817 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6818 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6819 * does not conflict with tags of buffer posted for unsolicited events.
6820 * The function returns the allocated tag. The function is called with
6821 * no locks held.
6822 **/
James Smart76bb24e2007-10-27 13:38:00 -04006823uint32_t
6824lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6825{
6826 spin_lock_irq(&phba->hbalock);
6827 phba->buffer_tag_count++;
6828 /*
6829 * Always set the QUE_BUFTAG_BIT to distiguish between
6830 * a tag assigned by HBQ.
6831 */
6832 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6833 spin_unlock_irq(&phba->hbalock);
6834 return phba->buffer_tag_count;
6835}
6836
James Smarte59058c2008-08-24 21:49:00 -04006837/**
James Smart3621a712009-04-06 18:47:14 -04006838 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006839 * @phba: Pointer to HBA context object.
6840 * @pring: Pointer to driver SLI ring object.
6841 * @tag: Buffer tag.
6842 *
6843 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6844 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6845 * iocb is posted to the response ring with the tag of the buffer.
6846 * This function searches the pring->postbufq list using the tag
6847 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6848 * iocb. If the buffer is found then lpfc_dmabuf object of the
6849 * buffer is returned to the caller else NULL is returned.
6850 * This function is called with no lock held.
6851 **/
James Smart76bb24e2007-10-27 13:38:00 -04006852struct lpfc_dmabuf *
6853lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6854 uint32_t tag)
6855{
6856 struct lpfc_dmabuf *mp, *next_mp;
6857 struct list_head *slp = &pring->postbufq;
6858
6859 /* Search postbufq, from the begining, looking for a match on tag */
6860 spin_lock_irq(&phba->hbalock);
6861 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6862 if (mp->buffer_tag == tag) {
6863 list_del_init(&mp->list);
6864 pring->postbufq_cnt--;
6865 spin_unlock_irq(&phba->hbalock);
6866 return mp;
6867 }
6868 }
6869
6870 spin_unlock_irq(&phba->hbalock);
6871 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006872 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006873 "ring %d Data x%lx x%p x%p x%x\n",
6874 pring->ringno, (unsigned long) tag,
6875 slp->next, slp->prev, pring->postbufq_cnt);
6876
6877 return NULL;
6878}
dea31012005-04-17 16:05:31 -05006879
James Smarte59058c2008-08-24 21:49:00 -04006880/**
James Smart3621a712009-04-06 18:47:14 -04006881 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006882 * @phba: Pointer to HBA context object.
6883 * @pring: Pointer to driver SLI ring object.
6884 * @phys: DMA address of the buffer.
6885 *
6886 * This function searches the buffer list using the dma_address
6887 * of unsolicited event to find the driver's lpfc_dmabuf object
6888 * corresponding to the dma_address. The function returns the
6889 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6890 * This function is called by the ct and els unsolicited event
6891 * handlers to get the buffer associated with the unsolicited
6892 * event.
6893 *
6894 * This function is called with no lock held.
6895 **/
dea31012005-04-17 16:05:31 -05006896struct lpfc_dmabuf *
6897lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6898 dma_addr_t phys)
6899{
6900 struct lpfc_dmabuf *mp, *next_mp;
6901 struct list_head *slp = &pring->postbufq;
6902
6903 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05006904 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006905 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6906 if (mp->phys == phys) {
6907 list_del_init(&mp->list);
6908 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006909 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006910 return mp;
6911 }
6912 }
6913
James Smart2e0fef82007-06-17 19:56:36 -05006914 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04006916 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05006917 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006918 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05006919 slp->next, slp->prev, pring->postbufq_cnt);
6920 return NULL;
6921}
6922
James Smarte59058c2008-08-24 21:49:00 -04006923/**
James Smart3621a712009-04-06 18:47:14 -04006924 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04006925 * @phba: Pointer to HBA context object.
6926 * @cmdiocb: Pointer to driver command iocb object.
6927 * @rspiocb: Pointer to driver response iocb object.
6928 *
6929 * This function is the completion handler for the abort iocbs for
6930 * ELS commands. This function is called from the ELS ring event
6931 * handler with no lock held. This function frees memory resources
6932 * associated with the abort iocb.
6933 **/
dea31012005-04-17 16:05:31 -05006934static void
James Smart2e0fef82007-06-17 19:56:36 -05006935lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6936 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05006937{
James Smart2e0fef82007-06-17 19:56:36 -05006938 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04006939 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05006940 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04006941 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6942
6943 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04006944
6945 if (irsp->ulpStatus) {
6946 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
6947 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
6948
James Smart2e0fef82007-06-17 19:56:36 -05006949 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04006950 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
6951 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
6952
James Smart92d7f7b2007-06-17 19:56:38 -05006953 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006954 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05006955 "with tag %x context %x, abort status %x, "
6956 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04006957 abort_iocb, abort_iotag, abort_context,
6958 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04006959
6960 /*
James Smart58da1ff2008-04-07 10:15:56 -04006961 * If the iocb is not found in Firmware queue the iocb
6962 * might have completed already. Do not free it again.
6963 */
James Smart9b379602008-04-07 10:16:00 -04006964 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04006965 spin_unlock_irq(&phba->hbalock);
6966 lpfc_sli_release_iocbq(phba, cmdiocb);
6967 return;
6968 }
6969 /*
James Smart2680eea2007-04-25 09:52:55 -04006970 * make sure we have the right iocbq before taking it
6971 * off the txcmplq and try to call completion routine.
6972 */
James Smart2e0fef82007-06-17 19:56:36 -05006973 if (!abort_iocb ||
6974 abort_iocb->iocb.ulpContext != abort_context ||
6975 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
6976 spin_unlock_irq(&phba->hbalock);
6977 else {
James Smart92d7f7b2007-06-17 19:56:38 -05006978 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04006979 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006980 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04006981
James Smart0ff10d42008-01-11 01:52:36 -05006982 /* Firmware could still be in progress of DMAing
6983 * payload, so don't free data buffer till after
6984 * a hbeat.
6985 */
6986 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
6987
James Smart92d7f7b2007-06-17 19:56:38 -05006988 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
6989 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
6990 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
6991 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04006992 }
6993 }
6994
James Bottomley604a3e32005-10-29 10:28:33 -05006995 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05006996 return;
6997}
6998
James Smarte59058c2008-08-24 21:49:00 -04006999/**
James Smart3621a712009-04-06 18:47:14 -04007000 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007001 * @phba: Pointer to HBA context object.
7002 * @cmdiocb: Pointer to driver command iocb object.
7003 * @rspiocb: Pointer to driver response iocb object.
7004 *
7005 * The function is called from SLI ring event handler with no
7006 * lock held. This function is the completion handler for ELS commands
7007 * which are aborted. The function frees memory resources used for
7008 * the aborted ELS commands.
7009 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007010static void
7011lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7012 struct lpfc_iocbq *rspiocb)
7013{
7014 IOCB_t *irsp = &rspiocb->iocb;
7015
7016 /* ELS cmd tag <ulpIoTag> completes */
7017 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007018 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007019 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007020 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007021 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007022 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7023 lpfc_ct_free_iocb(phba, cmdiocb);
7024 else
7025 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007026 return;
7027}
7028
James Smarte59058c2008-08-24 21:49:00 -04007029/**
James Smart3621a712009-04-06 18:47:14 -04007030 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007031 * @phba: Pointer to HBA context object.
7032 * @pring: Pointer to driver SLI ring object.
7033 * @cmdiocb: Pointer to driver command iocb object.
7034 *
7035 * This function issues an abort iocb for the provided command
7036 * iocb. This function is called with hbalock held.
7037 * The function returns 0 when it fails due to memory allocation
7038 * failure or when the command iocb is an abort request.
7039 **/
dea31012005-04-17 16:05:31 -05007040int
James Smart2e0fef82007-06-17 19:56:36 -05007041lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7042 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007043{
James Smart2e0fef82007-06-17 19:56:36 -05007044 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007045 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007046 IOCB_t *icmd = NULL;
7047 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007048 int retval = IOCB_ERROR;
7049
James Smart92d7f7b2007-06-17 19:56:38 -05007050 /*
7051 * There are certain command types we don't want to abort. And we
7052 * don't want to abort commands that are already in the process of
7053 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007054 */
7055 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007056 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007057 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7058 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007059 return 0;
7060
James Smart858c9f62007-06-17 19:56:39 -05007061 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7062 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007063 */
James Smart858c9f62007-06-17 19:56:39 -05007064 if ((vport->load_flag & FC_UNLOADING) &&
7065 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007066 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7067 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7068 else
7069 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007070 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007071 }
dea31012005-04-17 16:05:31 -05007072
7073 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007074 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007075 if (abtsiocbp == NULL)
7076 return 0;
dea31012005-04-17 16:05:31 -05007077
James Smart07951072007-04-25 09:51:38 -04007078 /* This signals the response to set the correct status
7079 * before calling the completion handler.
7080 */
7081 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7082
dea31012005-04-17 16:05:31 -05007083 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007084 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7085 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007086 if (phba->sli_rev == LPFC_SLI_REV4)
7087 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7088 else
7089 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007090 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007091 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007092
James Smart2e0fef82007-06-17 19:56:36 -05007093 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007094 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7095 else
7096 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7097
7098 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007099
James Smarte8b62012007-08-02 11:10:09 -04007100 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7101 "0339 Abort xri x%x, original iotag x%x, "
7102 "abort cmd iotag x%x\n",
7103 iabt->un.acxri.abortContextTag,
7104 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007105 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007106
James Smartd7c255b2008-08-24 21:50:00 -04007107 if (retval)
7108 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007109abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007110 /*
7111 * Caller to this routine should check for IOCB_ERROR
7112 * and handle it properly. This routine no longer removes
7113 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007114 */
James Smart2e0fef82007-06-17 19:56:36 -05007115 return retval;
dea31012005-04-17 16:05:31 -05007116}
7117
James Smarte59058c2008-08-24 21:49:00 -04007118/**
James Smart3621a712009-04-06 18:47:14 -04007119 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007120 * @iocbq: Pointer to driver iocb object.
7121 * @vport: Pointer to driver virtual port object.
7122 * @tgt_id: SCSI ID of the target.
7123 * @lun_id: LUN ID of the scsi device.
7124 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7125 *
James Smart3621a712009-04-06 18:47:14 -04007126 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007127 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7128 * 0 if the filtering criteria is met for the given iocb and will return
7129 * 1 if the filtering criteria is not met.
7130 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7131 * given iocb is for the SCSI device specified by vport, tgt_id and
7132 * lun_id parameter.
7133 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7134 * given iocb is for the SCSI target specified by vport and tgt_id
7135 * parameters.
7136 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7137 * given iocb is for the SCSI host associated with the given vport.
7138 * This function is called with no locks held.
7139 **/
dea31012005-04-17 16:05:31 -05007140static int
James Smart51ef4c22007-08-02 11:10:31 -04007141lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7142 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007143 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007144{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007145 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007146 int rc = 1;
7147
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007148 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7149 return rc;
7150
James Smart51ef4c22007-08-02 11:10:31 -04007151 if (iocbq->vport != vport)
7152 return rc;
7153
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007154 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007155
James Smart495a7142008-06-14 22:52:59 -04007156 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007157 return rc;
7158
7159 switch (ctx_cmd) {
7160 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007161 if ((lpfc_cmd->rdata->pnode) &&
7162 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7163 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007164 rc = 0;
7165 break;
7166 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007167 if ((lpfc_cmd->rdata->pnode) &&
7168 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007169 rc = 0;
7170 break;
dea31012005-04-17 16:05:31 -05007171 case LPFC_CTX_HOST:
7172 rc = 0;
7173 break;
7174 default:
7175 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007176 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007177 break;
7178 }
7179
7180 return rc;
7181}
7182
James Smarte59058c2008-08-24 21:49:00 -04007183/**
James Smart3621a712009-04-06 18:47:14 -04007184 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007185 * @vport: Pointer to virtual port.
7186 * @tgt_id: SCSI ID of the target.
7187 * @lun_id: LUN ID of the scsi device.
7188 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7189 *
7190 * This function returns number of FCP commands pending for the vport.
7191 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7192 * commands pending on the vport associated with SCSI device specified
7193 * by tgt_id and lun_id parameters.
7194 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7195 * commands pending on the vport associated with SCSI target specified
7196 * by tgt_id parameter.
7197 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7198 * commands pending on the vport.
7199 * This function returns the number of iocbs which satisfy the filter.
7200 * This function is called without any lock held.
7201 **/
dea31012005-04-17 16:05:31 -05007202int
James Smart51ef4c22007-08-02 11:10:31 -04007203lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7204 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007205{
James Smart51ef4c22007-08-02 11:10:31 -04007206 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007207 struct lpfc_iocbq *iocbq;
7208 int sum, i;
dea31012005-04-17 16:05:31 -05007209
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007210 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7211 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007212
James Smart51ef4c22007-08-02 11:10:31 -04007213 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7214 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007215 sum++;
dea31012005-04-17 16:05:31 -05007216 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007217
dea31012005-04-17 16:05:31 -05007218 return sum;
7219}
7220
James Smarte59058c2008-08-24 21:49:00 -04007221/**
James Smart3621a712009-04-06 18:47:14 -04007222 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007223 * @phba: Pointer to HBA context object
7224 * @cmdiocb: Pointer to command iocb object.
7225 * @rspiocb: Pointer to response iocb object.
7226 *
7227 * This function is called when an aborted FCP iocb completes. This
7228 * function is called by the ring event handler with no lock held.
7229 * This function frees the iocb.
7230 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007231void
James Smart2e0fef82007-06-17 19:56:36 -05007232lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7233 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007234{
James Bottomley604a3e32005-10-29 10:28:33 -05007235 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007236 return;
7237}
7238
James Smarte59058c2008-08-24 21:49:00 -04007239/**
James Smart3621a712009-04-06 18:47:14 -04007240 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007241 * @vport: Pointer to virtual port.
7242 * @pring: Pointer to driver SLI ring object.
7243 * @tgt_id: SCSI ID of the target.
7244 * @lun_id: LUN ID of the scsi device.
7245 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7246 *
7247 * This function sends an abort command for every SCSI command
7248 * associated with the given virtual port pending on the ring
7249 * filtered by lpfc_sli_validate_fcp_iocb function.
7250 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7251 * FCP iocbs associated with lun specified by tgt_id and lun_id
7252 * parameters
7253 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7254 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7255 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7256 * FCP iocbs associated with virtual port.
7257 * This function returns number of iocbs it failed to abort.
7258 * This function is called with no locks held.
7259 **/
dea31012005-04-17 16:05:31 -05007260int
James Smart51ef4c22007-08-02 11:10:31 -04007261lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7262 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007263{
James Smart51ef4c22007-08-02 11:10:31 -04007264 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007265 struct lpfc_iocbq *iocbq;
7266 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007267 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007268 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007269 int i;
dea31012005-04-17 16:05:31 -05007270
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007271 for (i = 1; i <= phba->sli.last_iotag; i++) {
7272 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007273
James Smart51ef4c22007-08-02 11:10:31 -04007274 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007275 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007276 continue;
7277
7278 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007279 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007280 if (abtsiocb == NULL) {
7281 errcnt++;
7282 continue;
7283 }
dea31012005-04-17 16:05:31 -05007284
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007285 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007286 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7287 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007288 if (phba->sli_rev == LPFC_SLI_REV4)
7289 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7290 else
7291 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007292 abtsiocb->iocb.ulpLe = 1;
7293 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007294 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007295
James Smart2e0fef82007-06-17 19:56:36 -05007296 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007297 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7298 else
7299 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7300
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007301 /* Setup callback routine and issue the command. */
7302 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007303 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7304 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007305 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007306 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007307 errcnt++;
7308 continue;
7309 }
7310 }
7311
7312 return errcnt;
7313}
7314
James Smarte59058c2008-08-24 21:49:00 -04007315/**
James Smart3621a712009-04-06 18:47:14 -04007316 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007317 * @phba: Pointer to HBA context object.
7318 * @cmdiocbq: Pointer to command iocb.
7319 * @rspiocbq: Pointer to response iocb.
7320 *
7321 * This function is the completion handler for iocbs issued using
7322 * lpfc_sli_issue_iocb_wait function. This function is called by the
7323 * ring event handler function without any lock held. This function
7324 * can be called from both worker thread context and interrupt
7325 * context. This function also can be called from other thread which
7326 * cleans up the SLI layer objects.
7327 * This function copy the contents of the response iocb to the
7328 * response iocb memory object provided by the caller of
7329 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7330 * sleeps for the iocb completion.
7331 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007332static void
7333lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7334 struct lpfc_iocbq *cmdiocbq,
7335 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007336{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007337 wait_queue_head_t *pdone_q;
7338 unsigned long iflags;
dea31012005-04-17 16:05:31 -05007339
James Smart2e0fef82007-06-17 19:56:36 -05007340 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007341 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7342 if (cmdiocbq->context2 && rspiocbq)
7343 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7344 &rspiocbq->iocb, sizeof(IOCB_t));
7345
7346 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007347 if (pdone_q)
7348 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007349 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007350 return;
7351}
7352
James Smarte59058c2008-08-24 21:49:00 -04007353/**
James Smart3621a712009-04-06 18:47:14 -04007354 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007355 * @phba: Pointer to HBA context object..
7356 * @pring: Pointer to sli ring.
7357 * @piocb: Pointer to command iocb.
7358 * @prspiocbq: Pointer to response iocb.
7359 * @timeout: Timeout in number of seconds.
7360 *
7361 * This function issues the iocb to firmware and waits for the
7362 * iocb to complete. If the iocb command is not
7363 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7364 * Caller should not free the iocb resources if this function
7365 * returns IOCB_TIMEDOUT.
7366 * The function waits for the iocb completion using an
7367 * non-interruptible wait.
7368 * This function will sleep while waiting for iocb completion.
7369 * So, this function should not be called from any context which
7370 * does not allow sleeping. Due to the same reason, this function
7371 * cannot be called with interrupt disabled.
7372 * This function assumes that the iocb completions occur while
7373 * this function sleep. So, this function cannot be called from
7374 * the thread which process iocb completion for this ring.
7375 * This function clears the iocb_flag of the iocb object before
7376 * issuing the iocb and the iocb completion handler sets this
7377 * flag and wakes this thread when the iocb completes.
7378 * The contents of the response iocb will be copied to prspiocbq
7379 * by the completion handler when the command completes.
7380 * This function returns IOCB_SUCCESS when success.
7381 * This function is called with no lock held.
7382 **/
dea31012005-04-17 16:05:31 -05007383int
James Smart2e0fef82007-06-17 19:56:36 -05007384lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007385 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007386 struct lpfc_iocbq *piocb,
7387 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007388 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007389{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007390 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007391 long timeleft, timeout_req = 0;
7392 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007393 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007394
7395 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007396 * If the caller has provided a response iocbq buffer, then context2
7397 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007398 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007399 if (prspiocbq) {
7400 if (piocb->context2)
7401 return IOCB_ERROR;
7402 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007403 }
7404
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007405 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7406 piocb->context_un.wait_queue = &done_q;
7407 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007408
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007409 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7410 creg_val = readl(phba->HCregaddr);
7411 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7412 writel(creg_val, phba->HCregaddr);
7413 readl(phba->HCregaddr); /* flush */
7414 }
7415
James Smartda0436e2009-05-22 14:51:39 -04007416 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007417 if (retval == IOCB_SUCCESS) {
7418 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007419 timeleft = wait_event_timeout(done_q,
7420 piocb->iocb_flag & LPFC_IO_WAKE,
7421 timeout_req);
dea31012005-04-17 16:05:31 -05007422
James Smart7054a602007-04-25 09:52:34 -04007423 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7424 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007425 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007426 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007427 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007428 "0338 IOCB wait timeout error - no "
7429 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007430 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007431 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007433 "0330 IOCB wake NOT set, "
7434 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007435 timeout, (timeleft / jiffies));
7436 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007437 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007438 } else {
7439 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007440 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007441 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007442 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007443 }
7444
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007445 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7446 creg_val = readl(phba->HCregaddr);
7447 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7448 writel(creg_val, phba->HCregaddr);
7449 readl(phba->HCregaddr); /* flush */
7450 }
7451
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007452 if (prspiocbq)
7453 piocb->context2 = NULL;
7454
7455 piocb->context_un.wait_queue = NULL;
7456 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007457 return retval;
7458}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007459
James Smarte59058c2008-08-24 21:49:00 -04007460/**
James Smart3621a712009-04-06 18:47:14 -04007461 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007462 * @phba: Pointer to HBA context object.
7463 * @pmboxq: Pointer to driver mailbox object.
7464 * @timeout: Timeout in number of seconds.
7465 *
7466 * This function issues the mailbox to firmware and waits for the
7467 * mailbox command to complete. If the mailbox command is not
7468 * completed within timeout seconds, it returns MBX_TIMEOUT.
7469 * The function waits for the mailbox completion using an
7470 * interruptible wait. If the thread is woken up due to a
7471 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7472 * should not free the mailbox resources, if this function returns
7473 * MBX_TIMEOUT.
7474 * This function will sleep while waiting for mailbox completion.
7475 * So, this function should not be called from any context which
7476 * does not allow sleeping. Due to the same reason, this function
7477 * cannot be called with interrupt disabled.
7478 * This function assumes that the mailbox completion occurs while
7479 * this function sleep. So, this function cannot be called from
7480 * the worker thread which processes mailbox completion.
7481 * This function is called in the context of HBA management
7482 * applications.
7483 * This function returns MBX_SUCCESS when successful.
7484 * This function is called with no lock held.
7485 **/
dea31012005-04-17 16:05:31 -05007486int
James Smart2e0fef82007-06-17 19:56:36 -05007487lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007488 uint32_t timeout)
7489{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007490 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007491 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007492 unsigned long flag;
dea31012005-04-17 16:05:31 -05007493
7494 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007495 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007496 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007497
James Smart495a7142008-06-14 22:52:59 -04007498 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007499 /* setup wake call as IOCB callback */
7500 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7501 /* setup context field to pass wait_queue pointer to wake function */
7502 pmboxq->context1 = &done_q;
7503
dea31012005-04-17 16:05:31 -05007504 /* now issue the command */
7505 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7506
7507 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007508 wait_event_interruptible_timeout(done_q,
7509 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7510 timeout * HZ);
7511
James Smart858c9f62007-06-17 19:56:39 -05007512 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007513 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007514 /*
7515 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7516 * else do not free the resources.
7517 */
7518 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007519 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007520 else {
James Smart7054a602007-04-25 09:52:34 -04007521 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007522 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7523 }
7524 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007525 }
7526
dea31012005-04-17 16:05:31 -05007527 return retval;
7528}
7529
James Smarte59058c2008-08-24 21:49:00 -04007530/**
James Smart3772a992009-05-22 14:50:54 -04007531 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007532 * @phba: Pointer to HBA context.
7533 *
James Smart3772a992009-05-22 14:50:54 -04007534 * This function is called to shutdown the driver's mailbox sub-system.
7535 * It first marks the mailbox sub-system is in a block state to prevent
7536 * the asynchronous mailbox command from issued off the pending mailbox
7537 * command queue. If the mailbox command sub-system shutdown is due to
7538 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7539 * the mailbox sub-system flush routine to forcefully bring down the
7540 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7541 * as with offline or HBA function reset), this routine will wait for the
7542 * outstanding mailbox command to complete before invoking the mailbox
7543 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007544 **/
James Smart3772a992009-05-22 14:50:54 -04007545void
7546lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007547{
James Smart3772a992009-05-22 14:50:54 -04007548 struct lpfc_sli *psli = &phba->sli;
7549 uint8_t actcmd = MBX_HEARTBEAT;
7550 unsigned long timeout;
7551
7552 spin_lock_irq(&phba->hbalock);
7553 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7554 spin_unlock_irq(&phba->hbalock);
7555
7556 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7557 spin_lock_irq(&phba->hbalock);
7558 if (phba->sli.mbox_active)
7559 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7560 spin_unlock_irq(&phba->hbalock);
7561 /* Determine how long we might wait for the active mailbox
7562 * command to be gracefully completed by firmware.
7563 */
7564 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7565 1000) + jiffies;
7566 while (phba->sli.mbox_active) {
7567 /* Check active mailbox complete status every 2ms */
7568 msleep(2);
7569 if (time_after(jiffies, timeout))
7570 /* Timeout, let the mailbox flush routine to
7571 * forcefully release active mailbox command
7572 */
7573 break;
7574 }
7575 }
7576 lpfc_sli_mbox_sys_flush(phba);
7577}
7578
7579/**
7580 * lpfc_sli_eratt_read - read sli-3 error attention events
7581 * @phba: Pointer to HBA context.
7582 *
7583 * This function is called to read the SLI3 device error attention registers
7584 * for possible error attention events. The caller must hold the hostlock
7585 * with spin_lock_irq().
7586 *
7587 * This fucntion returns 1 when there is Error Attention in the Host Attention
7588 * Register and returns 0 otherwise.
7589 **/
7590static int
7591lpfc_sli_eratt_read(struct lpfc_hba *phba)
7592{
James Smarted957682007-06-17 19:56:37 -05007593 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007594
James Smart3772a992009-05-22 14:50:54 -04007595 /* Read chip Host Attention (HA) register */
7596 ha_copy = readl(phba->HAregaddr);
7597 if (ha_copy & HA_ERATT) {
7598 /* Read host status register to retrieve error event */
7599 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007600
James Smart3772a992009-05-22 14:50:54 -04007601 /* Check if there is a deferred error condition is active */
7602 if ((HS_FFER1 & phba->work_hs) &&
7603 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7604 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7605 spin_lock_irq(&phba->hbalock);
7606 phba->hba_flag |= DEFER_ERATT;
7607 spin_unlock_irq(&phba->hbalock);
7608 /* Clear all interrupt enable conditions */
7609 writel(0, phba->HCregaddr);
7610 readl(phba->HCregaddr);
7611 }
7612
7613 /* Set the driver HA work bitmap */
James Smarted957682007-06-17 19:56:37 -05007614 spin_lock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007615 phba->work_ha |= HA_ERATT;
7616 /* Indicate polling handles this ERATT */
7617 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smarted957682007-06-17 19:56:37 -05007618 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007619 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007620 }
James Smart3772a992009-05-22 14:50:54 -04007621 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007622}
7623
James Smarte59058c2008-08-24 21:49:00 -04007624/**
James Smartda0436e2009-05-22 14:51:39 -04007625 * lpfc_sli4_eratt_read - read sli-4 error attention events
7626 * @phba: Pointer to HBA context.
7627 *
7628 * This function is called to read the SLI4 device error attention registers
7629 * for possible error attention events. The caller must hold the hostlock
7630 * with spin_lock_irq().
7631 *
7632 * This fucntion returns 1 when there is Error Attention in the Host Attention
7633 * Register and returns 0 otherwise.
7634 **/
7635static int
7636lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7637{
7638 uint32_t uerr_sta_hi, uerr_sta_lo;
7639 uint32_t onlnreg0, onlnreg1;
7640
7641 /* For now, use the SLI4 device internal unrecoverable error
7642 * registers for error attention. This can be changed later.
7643 */
7644 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7645 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7646 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7647 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7648 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7649 if (uerr_sta_lo || uerr_sta_hi) {
7650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7651 "1423 HBA Unrecoverable error: "
7652 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7653 "online0_reg=0x%x, online1_reg=0x%x\n",
7654 uerr_sta_lo, uerr_sta_hi,
7655 onlnreg0, onlnreg1);
7656 /* TEMP: as the driver error recover logic is not
7657 * fully developed, we just log the error message
7658 * and the device error attention action is now
7659 * temporarily disabled.
7660 */
7661 return 0;
7662 phba->work_status[0] = uerr_sta_lo;
7663 phba->work_status[1] = uerr_sta_hi;
7664 spin_lock_irq(&phba->hbalock);
7665 /* Set the driver HA work bitmap */
7666 phba->work_ha |= HA_ERATT;
7667 /* Indicate polling handles this ERATT */
7668 phba->hba_flag |= HBA_ERATT_HANDLED;
7669 spin_unlock_irq(&phba->hbalock);
7670 return 1;
7671 }
7672 }
7673 return 0;
7674}
7675
7676/**
James Smart3621a712009-04-06 18:47:14 -04007677 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007678 * @phba: Pointer to HBA context.
7679 *
James Smart3772a992009-05-22 14:50:54 -04007680 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007681 * error attention register bit for error attention events.
7682 *
7683 * This fucntion returns 1 when there is Error Attention in the Host Attention
7684 * Register and returns 0 otherwise.
7685 **/
7686int
7687lpfc_sli_check_eratt(struct lpfc_hba *phba)
7688{
7689 uint32_t ha_copy;
7690
7691 /* If somebody is waiting to handle an eratt, don't process it
7692 * here. The brdkill function will do this.
7693 */
7694 if (phba->link_flag & LS_IGNORE_ERATT)
7695 return 0;
7696
7697 /* Check if interrupt handler handles this ERATT */
7698 spin_lock_irq(&phba->hbalock);
7699 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7700 /* Interrupt handler has handled ERATT */
7701 spin_unlock_irq(&phba->hbalock);
7702 return 0;
7703 }
7704
James Smarta257bf92009-04-06 18:48:10 -04007705 /*
7706 * If there is deferred error attention, do not check for error
7707 * attention
7708 */
7709 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7710 spin_unlock_irq(&phba->hbalock);
7711 return 0;
7712 }
7713
James Smart3772a992009-05-22 14:50:54 -04007714 /* If PCI channel is offline, don't process it */
7715 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007716 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007717 return 0;
7718 }
7719
7720 switch (phba->sli_rev) {
7721 case LPFC_SLI_REV2:
7722 case LPFC_SLI_REV3:
7723 /* Read chip Host Attention (HA) register */
7724 ha_copy = lpfc_sli_eratt_read(phba);
7725 break;
James Smartda0436e2009-05-22 14:51:39 -04007726 case LPFC_SLI_REV4:
7727 /* Read devcie Uncoverable Error (UERR) registers */
7728 ha_copy = lpfc_sli4_eratt_read(phba);
7729 break;
James Smart3772a992009-05-22 14:50:54 -04007730 default:
7731 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7732 "0299 Invalid SLI revision (%d)\n",
7733 phba->sli_rev);
7734 ha_copy = 0;
7735 break;
James Smart93996272008-08-24 21:50:30 -04007736 }
7737 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007738
7739 return ha_copy;
7740}
7741
7742/**
7743 * lpfc_intr_state_check - Check device state for interrupt handling
7744 * @phba: Pointer to HBA context.
7745 *
7746 * This inline routine checks whether a device or its PCI slot is in a state
7747 * that the interrupt should be handled.
7748 *
7749 * This function returns 0 if the device or the PCI slot is in a state that
7750 * interrupt should be handled, otherwise -EIO.
7751 */
7752static inline int
7753lpfc_intr_state_check(struct lpfc_hba *phba)
7754{
7755 /* If the pci channel is offline, ignore all the interrupts */
7756 if (unlikely(pci_channel_offline(phba->pcidev)))
7757 return -EIO;
7758
7759 /* Update device level interrupt statistics */
7760 phba->sli.slistat.sli_intr++;
7761
7762 /* Ignore all interrupts during initialization. */
7763 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7764 return -EIO;
7765
James Smart93996272008-08-24 21:50:30 -04007766 return 0;
7767}
7768
7769/**
James Smart3772a992009-05-22 14:50:54 -04007770 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007771 * @irq: Interrupt number.
7772 * @dev_id: The device context pointer.
7773 *
James Smart93996272008-08-24 21:50:30 -04007774 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007775 * service routine when device with SLI-3 interface spec is enabled with
7776 * MSI-X multi-message interrupt mode and there are slow-path events in
7777 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7778 * interrupt mode, this function is called as part of the device-level
7779 * interrupt handler. When the PCI slot is in error recovery or the HBA
7780 * is undergoing initialization, the interrupt handler will not process
7781 * the interrupt. The link attention and ELS ring attention events are
7782 * handled by the worker thread. The interrupt handler signals the worker
7783 * thread and returns for these events. This function is called without
7784 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007785 * structures.
7786 *
7787 * This function returns IRQ_HANDLED when interrupt is handled else it
7788 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007789 **/
dea31012005-04-17 16:05:31 -05007790irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007791lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007792{
James Smart2e0fef82007-06-17 19:56:36 -05007793 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05007794 uint32_t ha_copy;
7795 uint32_t work_ha_copy;
7796 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007797 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007798 uint32_t control;
7799
James Smart92d7f7b2007-06-17 19:56:38 -05007800 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007801 struct lpfc_vport *vport;
7802 struct lpfc_nodelist *ndlp;
7803 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007804 LPFC_MBOXQ_t *pmb;
7805 int rc;
7806
dea31012005-04-17 16:05:31 -05007807 /*
7808 * Get the driver's phba structure from the dev_id and
7809 * assume the HBA is not interrupting.
7810 */
James Smart93996272008-08-24 21:50:30 -04007811 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007812
7813 if (unlikely(!phba))
7814 return IRQ_NONE;
7815
dea31012005-04-17 16:05:31 -05007816 /*
James Smart93996272008-08-24 21:50:30 -04007817 * Stuff needs to be attented to when this function is invoked as an
7818 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007819 */
James Smart93996272008-08-24 21:50:30 -04007820 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007821 /* Check device state for handling interrupt */
7822 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007823 return IRQ_NONE;
7824 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007825 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007826 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007827 /* If somebody is waiting to handle an eratt don't process it
7828 * here. The brdkill function will do this.
7829 */
7830 if (phba->link_flag & LS_IGNORE_ERATT)
7831 ha_copy &= ~HA_ERATT;
7832 /* Check the need for handling ERATT in interrupt handler */
7833 if (ha_copy & HA_ERATT) {
7834 if (phba->hba_flag & HBA_ERATT_HANDLED)
7835 /* ERATT polling has handled ERATT */
7836 ha_copy &= ~HA_ERATT;
7837 else
7838 /* Indicate interrupt handler handles ERATT */
7839 phba->hba_flag |= HBA_ERATT_HANDLED;
7840 }
James Smarta257bf92009-04-06 18:48:10 -04007841
7842 /*
7843 * If there is deferred error attention, do not check for any
7844 * interrupt.
7845 */
7846 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007847 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007848 return IRQ_NONE;
7849 }
7850
James Smart93996272008-08-24 21:50:30 -04007851 /* Clear up only attention source related to slow-path */
7852 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7853 phba->HAregaddr);
7854 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007855 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007856 } else
7857 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05007858
dea31012005-04-17 16:05:31 -05007859 work_ha_copy = ha_copy & phba->work_ha_mask;
7860
James Smart93996272008-08-24 21:50:30 -04007861 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05007862 if (work_ha_copy & HA_LATT) {
7863 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7864 /*
7865 * Turn off Link Attention interrupts
7866 * until CLEAR_LA done
7867 */
James Smart5b75da22008-12-04 22:39:35 -05007868 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007869 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7870 control = readl(phba->HCregaddr);
7871 control &= ~HC_LAINT_ENA;
7872 writel(control, phba->HCregaddr);
7873 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007874 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007875 }
7876 else
7877 work_ha_copy &= ~HA_LATT;
7878 }
7879
James Smart93996272008-08-24 21:50:30 -04007880 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05007881 /*
7882 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7883 * the only slow ring.
7884 */
7885 status = (work_ha_copy &
7886 (HA_RXMASK << (4*LPFC_ELS_RING)));
7887 status >>= (4*LPFC_ELS_RING);
7888 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05007889 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05007890 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04007891
7892 lpfc_debugfs_slow_ring_trc(phba,
7893 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
7894 control, status,
7895 (uint32_t)phba->sli.slistat.sli_intr);
7896
James Smart858c9f62007-06-17 19:56:39 -05007897 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04007898 lpfc_debugfs_slow_ring_trc(phba,
7899 "ISR Disable ring:"
7900 "pwork:x%x hawork:x%x wait:x%x",
7901 phba->work_ha, work_ha_copy,
7902 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007903 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007904
James Smart858c9f62007-06-17 19:56:39 -05007905 control &=
7906 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05007907 writel(control, phba->HCregaddr);
7908 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05007909 }
James Smarta58cbd52007-08-02 11:09:43 -04007910 else {
7911 lpfc_debugfs_slow_ring_trc(phba,
7912 "ISR slow ring: pwork:"
7913 "x%x hawork:x%x wait:x%x",
7914 phba->work_ha, work_ha_copy,
7915 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007916 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007917 }
James Smart5b75da22008-12-04 22:39:35 -05007918 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007919 }
7920 }
James Smart5b75da22008-12-04 22:39:35 -05007921 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007922 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04007923 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04007924 /*
7925 * Check if there is a deferred error condition
7926 * is active
7927 */
7928 if ((HS_FFER1 & phba->work_hs) &&
7929 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7930 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7931 phba->hba_flag |= DEFER_ERATT;
7932 /* Clear all interrupt enable conditions */
7933 writel(0, phba->HCregaddr);
7934 readl(phba->HCregaddr);
7935 }
7936 }
7937
James Smart93996272008-08-24 21:50:30 -04007938 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007939 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04007940 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04007941 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05007942 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05007943
7944 /* First check out the status word */
7945 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
7946 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05007947 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05007948 /*
7949 * Stray Mailbox Interrupt, mbxCommand <cmd>
7950 * mbxStatus <status>
7951 */
James Smart09372822008-01-11 01:52:54 -05007952 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05007953 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007954 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05007955 "Interrupt mbxCommand x%x "
7956 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007957 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05007958 pmbox->mbxCommand,
7959 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05007960 /* clear mailbox attention bit */
7961 work_ha_copy &= ~HA_MBATT;
7962 } else {
James Smart97eab632008-04-07 10:16:05 -04007963 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05007964 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05007965 phba->last_completion_time = jiffies;
7966 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05007967 if (pmb->mbox_cmpl) {
7968 lpfc_sli_pcimem_bcopy(mbox, pmbox,
7969 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05007970 }
James Smart09372822008-01-11 01:52:54 -05007971 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
7972 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
7973
7974 lpfc_debugfs_disc_trc(vport,
7975 LPFC_DISC_TRC_MBOX_VPORT,
7976 "MBOX dflt rpi: : "
7977 "status:x%x rpi:x%x",
7978 (uint32_t)pmbox->mbxStatus,
7979 pmbox->un.varWords[0], 0);
7980
7981 if (!pmbox->mbxStatus) {
7982 mp = (struct lpfc_dmabuf *)
7983 (pmb->context1);
7984 ndlp = (struct lpfc_nodelist *)
7985 pmb->context2;
7986
7987 /* Reg_LOGIN of dflt RPI was
7988 * successful. new lets get
7989 * rid of the RPI using the
7990 * same mbox buffer.
7991 */
7992 lpfc_unreg_login(phba,
7993 vport->vpi,
7994 pmbox->un.varWords[0],
7995 pmb);
7996 pmb->mbox_cmpl =
7997 lpfc_mbx_cmpl_dflt_rpi;
7998 pmb->context1 = mp;
7999 pmb->context2 = ndlp;
8000 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008001 rc = lpfc_sli_issue_mbox(phba,
8002 pmb,
8003 MBX_NOWAIT);
8004 if (rc != MBX_BUSY)
8005 lpfc_printf_log(phba,
8006 KERN_ERR,
8007 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008008 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04008009 "been MBX_BUSY");
James Smart3772a992009-05-22 14:50:54 -04008010 if (rc != MBX_NOT_FINISHED)
8011 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008012 }
8013 }
James Smart5b75da22008-12-04 22:39:35 -05008014 spin_lock_irqsave(
8015 &phba->pport->work_port_lock,
8016 iflag);
James Smart09372822008-01-11 01:52:54 -05008017 phba->pport->work_port_events &=
8018 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008019 spin_unlock_irqrestore(
8020 &phba->pport->work_port_lock,
8021 iflag);
James Smart09372822008-01-11 01:52:54 -05008022 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008023 }
James Smart97eab632008-04-07 10:16:05 -04008024 } else
James Smart5b75da22008-12-04 22:39:35 -05008025 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008026
James Smart92d7f7b2007-06-17 19:56:38 -05008027 if ((work_ha_copy & HA_MBATT) &&
8028 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008029send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008030 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008031 do {
8032 rc = lpfc_sli_issue_mbox(phba, NULL,
8033 MBX_NOWAIT);
8034 } while (rc == MBX_NOT_FINISHED);
8035 if (rc != MBX_SUCCESS)
8036 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8037 LOG_SLI, "0349 rc should be "
8038 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05008039 }
8040
James Smart5b75da22008-12-04 22:39:35 -05008041 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008042 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008043 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008044 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008045 }
James Smart93996272008-08-24 21:50:30 -04008046 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008047
James Smart3772a992009-05-22 14:50:54 -04008048} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008049
8050/**
James Smart3772a992009-05-22 14:50:54 -04008051 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008052 * @irq: Interrupt number.
8053 * @dev_id: The device context pointer.
8054 *
8055 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008056 * service routine when device with SLI-3 interface spec is enabled with
8057 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8058 * ring event in the HBA. However, when the device is enabled with either
8059 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8060 * device-level interrupt handler. When the PCI slot is in error recovery
8061 * or the HBA is undergoing initialization, the interrupt handler will not
8062 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8063 * the intrrupt context. This function is called without any lock held.
8064 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008065 *
8066 * This function returns IRQ_HANDLED when interrupt is handled else it
8067 * returns IRQ_NONE.
8068 **/
8069irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008070lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008071{
8072 struct lpfc_hba *phba;
8073 uint32_t ha_copy;
8074 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008075 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008076
8077 /* Get the driver's phba structure from the dev_id and
8078 * assume the HBA is not interrupting.
8079 */
8080 phba = (struct lpfc_hba *) dev_id;
8081
8082 if (unlikely(!phba))
8083 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008084
8085 /*
James Smart93996272008-08-24 21:50:30 -04008086 * Stuff needs to be attented to when this function is invoked as an
8087 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008088 */
James Smart93996272008-08-24 21:50:30 -04008089 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008090 /* Check device state for handling interrupt */
8091 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008092 return IRQ_NONE;
8093 /* Need to read HA REG for FCP ring and other ring events */
8094 ha_copy = readl(phba->HAregaddr);
8095 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008096 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008097 /*
8098 * If there is deferred error attention, do not check for
8099 * any interrupt.
8100 */
8101 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008102 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008103 return IRQ_NONE;
8104 }
James Smart93996272008-08-24 21:50:30 -04008105 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8106 phba->HAregaddr);
8107 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008108 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008109 } else
8110 ha_copy = phba->ha_copy;
8111
8112 /*
8113 * Process all events on FCP ring. Take the optimized path for FCP IO.
8114 */
8115 ha_copy &= ~(phba->work_ha_mask);
8116
8117 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008118 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008119 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008120 lpfc_sli_handle_fast_ring_event(phba,
8121 &phba->sli.ring[LPFC_FCP_RING],
8122 status);
James Smarta4bc3372006-12-02 13:34:16 -05008123
8124 if (phba->cfg_multi_ring_support == 2) {
8125 /*
James Smart93996272008-08-24 21:50:30 -04008126 * Process all events on extra ring. Take the optimized path
8127 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008128 */
James Smart93996272008-08-24 21:50:30 -04008129 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008130 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008131 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008132 lpfc_sli_handle_fast_ring_event(phba,
8133 &phba->sli.ring[LPFC_EXTRA_RING],
8134 status);
8135 }
8136 }
dea31012005-04-17 16:05:31 -05008137 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008138} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008139
James Smart93996272008-08-24 21:50:30 -04008140/**
James Smart3772a992009-05-22 14:50:54 -04008141 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008142 * @irq: Interrupt number.
8143 * @dev_id: The device context pointer.
8144 *
James Smart3772a992009-05-22 14:50:54 -04008145 * This function is the HBA device-level interrupt handler to device with
8146 * SLI-3 interface spec, called from the PCI layer when either MSI or
8147 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8148 * requires driver attention. This function invokes the slow-path interrupt
8149 * attention handling function and fast-path interrupt attention handling
8150 * function in turn to process the relevant HBA attention events. This
8151 * function is called without any lock held. It gets the hbalock to access
8152 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008153 *
8154 * This function returns IRQ_HANDLED when interrupt is handled, else it
8155 * returns IRQ_NONE.
8156 **/
8157irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008158lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008159{
8160 struct lpfc_hba *phba;
8161 irqreturn_t sp_irq_rc, fp_irq_rc;
8162 unsigned long status1, status2;
8163
8164 /*
8165 * Get the driver's phba structure from the dev_id and
8166 * assume the HBA is not interrupting.
8167 */
8168 phba = (struct lpfc_hba *) dev_id;
8169
8170 if (unlikely(!phba))
8171 return IRQ_NONE;
8172
James Smart3772a992009-05-22 14:50:54 -04008173 /* Check device state for handling interrupt */
8174 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008175 return IRQ_NONE;
8176
8177 spin_lock(&phba->hbalock);
8178 phba->ha_copy = readl(phba->HAregaddr);
8179 if (unlikely(!phba->ha_copy)) {
8180 spin_unlock(&phba->hbalock);
8181 return IRQ_NONE;
8182 } else if (phba->ha_copy & HA_ERATT) {
8183 if (phba->hba_flag & HBA_ERATT_HANDLED)
8184 /* ERATT polling has handled ERATT */
8185 phba->ha_copy &= ~HA_ERATT;
8186 else
8187 /* Indicate interrupt handler handles ERATT */
8188 phba->hba_flag |= HBA_ERATT_HANDLED;
8189 }
8190
James Smarta257bf92009-04-06 18:48:10 -04008191 /*
8192 * If there is deferred error attention, do not check for any interrupt.
8193 */
8194 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8195 spin_unlock_irq(&phba->hbalock);
8196 return IRQ_NONE;
8197 }
8198
James Smart93996272008-08-24 21:50:30 -04008199 /* Clear attention sources except link and error attentions */
8200 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8201 readl(phba->HAregaddr); /* flush */
8202 spin_unlock(&phba->hbalock);
8203
8204 /*
8205 * Invokes slow-path host attention interrupt handling as appropriate.
8206 */
8207
8208 /* status of events with mailbox and link attention */
8209 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8210
8211 /* status of events with ELS ring */
8212 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8213 status2 >>= (4*LPFC_ELS_RING);
8214
8215 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008216 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008217 else
8218 sp_irq_rc = IRQ_NONE;
8219
8220 /*
8221 * Invoke fast-path host attention interrupt handling as appropriate.
8222 */
8223
8224 /* status of events with FCP ring */
8225 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8226 status1 >>= (4*LPFC_FCP_RING);
8227
8228 /* status of events with extra ring */
8229 if (phba->cfg_multi_ring_support == 2) {
8230 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8231 status2 >>= (4*LPFC_EXTRA_RING);
8232 } else
8233 status2 = 0;
8234
8235 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008236 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008237 else
8238 fp_irq_rc = IRQ_NONE;
8239
8240 /* Return device-level interrupt handling status */
8241 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008242} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008243
8244/**
8245 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8246 * @phba: pointer to lpfc hba data structure.
8247 *
8248 * This routine is invoked by the worker thread to process all the pending
8249 * SLI4 FCP abort XRI events.
8250 **/
8251void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8252{
8253 struct lpfc_cq_event *cq_event;
8254
8255 /* First, declare the fcp xri abort event has been handled */
8256 spin_lock_irq(&phba->hbalock);
8257 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8258 spin_unlock_irq(&phba->hbalock);
8259 /* Now, handle all the fcp xri abort events */
8260 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8261 /* Get the first event from the head of the event queue */
8262 spin_lock_irq(&phba->hbalock);
8263 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8264 cq_event, struct lpfc_cq_event, list);
8265 spin_unlock_irq(&phba->hbalock);
8266 /* Notify aborted XRI for FCP work queue */
8267 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8268 /* Free the event processed back to the free pool */
8269 lpfc_sli4_cq_event_release(phba, cq_event);
8270 }
8271}
8272
8273/**
8274 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8275 * @phba: pointer to lpfc hba data structure.
8276 *
8277 * This routine is invoked by the worker thread to process all the pending
8278 * SLI4 els abort xri events.
8279 **/
8280void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8281{
8282 struct lpfc_cq_event *cq_event;
8283
8284 /* First, declare the els xri abort event has been handled */
8285 spin_lock_irq(&phba->hbalock);
8286 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8287 spin_unlock_irq(&phba->hbalock);
8288 /* Now, handle all the els xri abort events */
8289 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8290 /* Get the first event from the head of the event queue */
8291 spin_lock_irq(&phba->hbalock);
8292 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8293 cq_event, struct lpfc_cq_event, list);
8294 spin_unlock_irq(&phba->hbalock);
8295 /* Notify aborted XRI for ELS work queue */
8296 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8297 /* Free the event processed back to the free pool */
8298 lpfc_sli4_cq_event_release(phba, cq_event);
8299 }
8300}
8301
8302static void
8303lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8304 struct lpfc_iocbq *pIocbOut,
8305 struct lpfc_wcqe_complete *wcqe)
8306{
8307 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8308
8309 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8310 sizeof(struct lpfc_iocbq) - offset);
8311 memset(&pIocbIn->sli4_info, 0,
8312 sizeof(struct lpfc_sli4_rspiocb_info));
8313 /* Map WCQE parameters into irspiocb parameters */
8314 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8315 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8316 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8317 pIocbIn->iocb.un.fcpi.fcpi_parm =
8318 pIocbOut->iocb.un.fcpi.fcpi_parm -
8319 wcqe->total_data_placed;
8320 else
8321 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8322 else
8323 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8324 /* Load in additional WCQE parameters */
8325 pIocbIn->sli4_info.hw_status = bf_get(lpfc_wcqe_c_hw_status, wcqe);
8326 pIocbIn->sli4_info.bfield = 0;
8327 if (bf_get(lpfc_wcqe_c_xb, wcqe))
8328 pIocbIn->sli4_info.bfield |= LPFC_XB;
8329 if (bf_get(lpfc_wcqe_c_pv, wcqe)) {
8330 pIocbIn->sli4_info.bfield |= LPFC_PV;
8331 pIocbIn->sli4_info.priority =
8332 bf_get(lpfc_wcqe_c_priority, wcqe);
8333 }
8334}
8335
8336/**
James Smart04c68492009-05-22 14:52:52 -04008337 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8338 * @phba: Pointer to HBA context object.
8339 * @cqe: Pointer to mailbox completion queue entry.
8340 *
8341 * This routine process a mailbox completion queue entry with asynchrous
8342 * event.
8343 *
8344 * Return: true if work posted to worker thread, otherwise false.
8345 **/
8346static bool
8347lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8348{
8349 struct lpfc_cq_event *cq_event;
8350 unsigned long iflags;
8351
8352 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8353 "0392 Async Event: word0:x%x, word1:x%x, "
8354 "word2:x%x, word3:x%x\n", mcqe->word0,
8355 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8356
8357 /* Allocate a new internal CQ_EVENT entry */
8358 cq_event = lpfc_sli4_cq_event_alloc(phba);
8359 if (!cq_event) {
8360 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8361 "0394 Failed to allocate CQ_EVENT entry\n");
8362 return false;
8363 }
8364
8365 /* Move the CQE into an asynchronous event entry */
8366 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8367 spin_lock_irqsave(&phba->hbalock, iflags);
8368 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8369 /* Set the async event flag */
8370 phba->hba_flag |= ASYNC_EVENT;
8371 spin_unlock_irqrestore(&phba->hbalock, iflags);
8372
8373 return true;
8374}
8375
8376/**
8377 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8378 * @phba: Pointer to HBA context object.
8379 * @cqe: Pointer to mailbox completion queue entry.
8380 *
8381 * This routine process a mailbox completion queue entry with mailbox
8382 * completion event.
8383 *
8384 * Return: true if work posted to worker thread, otherwise false.
8385 **/
8386static bool
8387lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8388{
8389 uint32_t mcqe_status;
8390 MAILBOX_t *mbox, *pmbox;
8391 struct lpfc_mqe *mqe;
8392 struct lpfc_vport *vport;
8393 struct lpfc_nodelist *ndlp;
8394 struct lpfc_dmabuf *mp;
8395 unsigned long iflags;
8396 LPFC_MBOXQ_t *pmb;
8397 bool workposted = false;
8398 int rc;
8399
8400 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8401 if (!bf_get(lpfc_trailer_completed, mcqe))
8402 goto out_no_mqe_complete;
8403
8404 /* Get the reference to the active mbox command */
8405 spin_lock_irqsave(&phba->hbalock, iflags);
8406 pmb = phba->sli.mbox_active;
8407 if (unlikely(!pmb)) {
8408 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8409 "1832 No pending MBOX command to handle\n");
8410 spin_unlock_irqrestore(&phba->hbalock, iflags);
8411 goto out_no_mqe_complete;
8412 }
8413 spin_unlock_irqrestore(&phba->hbalock, iflags);
8414 mqe = &pmb->u.mqe;
8415 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8416 mbox = phba->mbox;
8417 vport = pmb->vport;
8418
8419 /* Reset heartbeat timer */
8420 phba->last_completion_time = jiffies;
8421 del_timer(&phba->sli.mbox_tmo);
8422
8423 /* Move mbox data to caller's mailbox region, do endian swapping */
8424 if (pmb->mbox_cmpl && mbox)
8425 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8426 /* Set the mailbox status with SLI4 range 0x4000 */
8427 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8428 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8429 bf_set(lpfc_mqe_status, mqe,
8430 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8431
8432 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8433 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8434 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8435 "MBOX dflt rpi: status:x%x rpi:x%x",
8436 mcqe_status,
8437 pmbox->un.varWords[0], 0);
8438 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8439 mp = (struct lpfc_dmabuf *)(pmb->context1);
8440 ndlp = (struct lpfc_nodelist *)pmb->context2;
8441 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8442 * RID of the PPI using the same mbox buffer.
8443 */
8444 lpfc_unreg_login(phba, vport->vpi,
8445 pmbox->un.varWords[0], pmb);
8446 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8447 pmb->context1 = mp;
8448 pmb->context2 = ndlp;
8449 pmb->vport = vport;
8450 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8451 if (rc != MBX_BUSY)
8452 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8453 LOG_SLI, "0385 rc should "
8454 "have been MBX_BUSY\n");
8455 if (rc != MBX_NOT_FINISHED)
8456 goto send_current_mbox;
8457 }
8458 }
8459 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8460 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8461 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8462
8463 /* There is mailbox completion work to do */
8464 spin_lock_irqsave(&phba->hbalock, iflags);
8465 __lpfc_mbox_cmpl_put(phba, pmb);
8466 phba->work_ha |= HA_MBATT;
8467 spin_unlock_irqrestore(&phba->hbalock, iflags);
8468 workposted = true;
8469
8470send_current_mbox:
8471 spin_lock_irqsave(&phba->hbalock, iflags);
8472 /* Release the mailbox command posting token */
8473 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8474 /* Setting active mailbox pointer need to be in sync to flag clear */
8475 phba->sli.mbox_active = NULL;
8476 spin_unlock_irqrestore(&phba->hbalock, iflags);
8477 /* Wake up worker thread to post the next pending mailbox command */
8478 lpfc_worker_wake_up(phba);
8479out_no_mqe_complete:
8480 if (bf_get(lpfc_trailer_consumed, mcqe))
8481 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8482 return workposted;
8483}
8484
8485/**
8486 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8487 * @phba: Pointer to HBA context object.
8488 * @cqe: Pointer to mailbox completion queue entry.
8489 *
8490 * This routine process a mailbox completion queue entry, it invokes the
8491 * proper mailbox complete handling or asynchrous event handling routine
8492 * according to the MCQE's async bit.
8493 *
8494 * Return: true if work posted to worker thread, otherwise false.
8495 **/
8496static bool
8497lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8498{
8499 struct lpfc_mcqe mcqe;
8500 bool workposted;
8501
8502 /* Copy the mailbox MCQE and convert endian order as needed */
8503 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8504
8505 /* Invoke the proper event handling routine */
8506 if (!bf_get(lpfc_trailer_async, &mcqe))
8507 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8508 else
8509 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8510 return workposted;
8511}
8512
8513/**
James Smart4f774512009-05-22 14:52:35 -04008514 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8515 * @phba: Pointer to HBA context object.
8516 * @wcqe: Pointer to work-queue completion queue entry.
8517 *
8518 * This routine handles an ELS work-queue completion event.
8519 *
8520 * Return: true if work posted to worker thread, otherwise false.
8521 **/
8522static bool
8523lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8524 struct lpfc_wcqe_complete *wcqe)
8525{
8526 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8527 struct lpfc_iocbq *cmdiocbq;
8528 struct lpfc_iocbq *irspiocbq;
8529 unsigned long iflags;
8530 bool workposted = false;
8531
8532 spin_lock_irqsave(&phba->hbalock, iflags);
8533 pring->stats.iocb_event++;
8534 /* Look up the ELS command IOCB and create pseudo response IOCB */
8535 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8536 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8537 spin_unlock_irqrestore(&phba->hbalock, iflags);
8538
8539 if (unlikely(!cmdiocbq)) {
8540 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8541 "0386 ELS complete with no corresponding "
8542 "cmdiocb: iotag (%d)\n",
8543 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8544 return workposted;
8545 }
8546
8547 /* Fake the irspiocbq and copy necessary response information */
8548 irspiocbq = lpfc_sli_get_iocbq(phba);
8549 if (!irspiocbq) {
8550 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8551 "0387 Failed to allocate an iocbq\n");
8552 return workposted;
8553 }
8554 lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8555
8556 /* Add the irspiocb to the response IOCB work list */
8557 spin_lock_irqsave(&phba->hbalock, iflags);
8558 list_add_tail(&irspiocbq->list, &phba->sli4_hba.sp_rspiocb_work_queue);
8559 /* Indicate ELS ring attention */
8560 phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
8561 spin_unlock_irqrestore(&phba->hbalock, iflags);
8562 workposted = true;
8563
8564 return workposted;
8565}
8566
8567/**
8568 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8569 * @phba: Pointer to HBA context object.
8570 * @wcqe: Pointer to work-queue completion queue entry.
8571 *
8572 * This routine handles slow-path WQ entry comsumed event by invoking the
8573 * proper WQ release routine to the slow-path WQ.
8574 **/
8575static void
8576lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8577 struct lpfc_wcqe_release *wcqe)
8578{
8579 /* Check for the slow-path ELS work queue */
8580 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8581 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8582 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8583 else
8584 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8585 "2579 Slow-path wqe consume event carries "
8586 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8587 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8588 phba->sli4_hba.els_wq->queue_id);
8589}
8590
8591/**
8592 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8593 * @phba: Pointer to HBA context object.
8594 * @cq: Pointer to a WQ completion queue.
8595 * @wcqe: Pointer to work-queue completion queue entry.
8596 *
8597 * This routine handles an XRI abort event.
8598 *
8599 * Return: true if work posted to worker thread, otherwise false.
8600 **/
8601static bool
8602lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8603 struct lpfc_queue *cq,
8604 struct sli4_wcqe_xri_aborted *wcqe)
8605{
8606 bool workposted = false;
8607 struct lpfc_cq_event *cq_event;
8608 unsigned long iflags;
8609
8610 /* Allocate a new internal CQ_EVENT entry */
8611 cq_event = lpfc_sli4_cq_event_alloc(phba);
8612 if (!cq_event) {
8613 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8614 "0602 Failed to allocate CQ_EVENT entry\n");
8615 return false;
8616 }
8617
8618 /* Move the CQE into the proper xri abort event list */
8619 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8620 switch (cq->subtype) {
8621 case LPFC_FCP:
8622 spin_lock_irqsave(&phba->hbalock, iflags);
8623 list_add_tail(&cq_event->list,
8624 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8625 /* Set the fcp xri abort event flag */
8626 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8627 spin_unlock_irqrestore(&phba->hbalock, iflags);
8628 workposted = true;
8629 break;
8630 case LPFC_ELS:
8631 spin_lock_irqsave(&phba->hbalock, iflags);
8632 list_add_tail(&cq_event->list,
8633 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8634 /* Set the els xri abort event flag */
8635 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8636 spin_unlock_irqrestore(&phba->hbalock, iflags);
8637 workposted = true;
8638 break;
8639 default:
8640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8641 "0603 Invalid work queue CQE subtype (x%x)\n",
8642 cq->subtype);
8643 workposted = false;
8644 break;
8645 }
8646 return workposted;
8647}
8648
8649/**
8650 * lpfc_sli4_sp_handle_wcqe - Process a work-queue completion queue entry
8651 * @phba: Pointer to HBA context object.
8652 * @cq: Pointer to the completion queue.
8653 * @wcqe: Pointer to a completion queue entry.
8654 *
8655 * This routine process a slow-path work-queue completion queue entry.
8656 *
8657 * Return: true if work posted to worker thread, otherwise false.
8658 **/
8659static bool
8660lpfc_sli4_sp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8661 struct lpfc_cqe *cqe)
8662{
8663 struct lpfc_wcqe_complete wcqe;
8664 bool workposted = false;
8665
8666 /* Copy the work queue CQE and convert endian order if needed */
8667 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8668
8669 /* Check and process for different type of WCQE and dispatch */
8670 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8671 case CQE_CODE_COMPL_WQE:
8672 /* Process the WQ complete event */
8673 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8674 (struct lpfc_wcqe_complete *)&wcqe);
8675 break;
8676 case CQE_CODE_RELEASE_WQE:
8677 /* Process the WQ release event */
8678 lpfc_sli4_sp_handle_rel_wcqe(phba,
8679 (struct lpfc_wcqe_release *)&wcqe);
8680 break;
8681 case CQE_CODE_XRI_ABORTED:
8682 /* Process the WQ XRI abort event */
8683 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8684 (struct sli4_wcqe_xri_aborted *)&wcqe);
8685 break;
8686 default:
8687 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8688 "0388 Not a valid WCQE code: x%x\n",
8689 bf_get(lpfc_wcqe_c_code, &wcqe));
8690 break;
8691 }
8692 return workposted;
8693}
8694
8695/**
8696 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8697 * @phba: Pointer to HBA context object.
8698 * @rcqe: Pointer to receive-queue completion queue entry.
8699 *
8700 * This routine process a receive-queue completion queue entry.
8701 *
8702 * Return: true if work posted to worker thread, otherwise false.
8703 **/
8704static bool
8705lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8706{
8707 struct lpfc_rcqe rcqe;
8708 bool workposted = false;
8709 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8710 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8711 struct hbq_dmabuf *dma_buf;
8712 uint32_t status;
8713 unsigned long iflags;
8714
8715 /* Copy the receive queue CQE and convert endian order if needed */
8716 lpfc_sli_pcimem_bcopy(cqe, &rcqe, sizeof(struct lpfc_rcqe));
8717 lpfc_sli4_rq_release(hrq, drq);
8718 if (bf_get(lpfc_rcqe_code, &rcqe) != CQE_CODE_RECEIVE)
8719 goto out;
8720 if (bf_get(lpfc_rcqe_rq_id, &rcqe) != hrq->queue_id)
8721 goto out;
8722
8723 status = bf_get(lpfc_rcqe_status, &rcqe);
8724 switch (status) {
8725 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8726 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8727 "2537 Receive Frame Truncated!!\n");
8728 case FC_STATUS_RQ_SUCCESS:
8729 spin_lock_irqsave(&phba->hbalock, iflags);
8730 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8731 if (!dma_buf) {
8732 spin_unlock_irqrestore(&phba->hbalock, iflags);
8733 goto out;
8734 }
8735 memcpy(&dma_buf->rcqe, &rcqe, sizeof(rcqe));
8736 /* save off the frame for the word thread to process */
8737 list_add_tail(&dma_buf->dbuf.list, &phba->rb_pend_list);
8738 /* Frame received */
8739 phba->hba_flag |= HBA_RECEIVE_BUFFER;
8740 spin_unlock_irqrestore(&phba->hbalock, iflags);
8741 workposted = true;
8742 break;
8743 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8744 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8745 /* Post more buffers if possible */
8746 spin_lock_irqsave(&phba->hbalock, iflags);
8747 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8748 spin_unlock_irqrestore(&phba->hbalock, iflags);
8749 workposted = true;
8750 break;
8751 }
8752out:
8753 return workposted;
8754
8755}
8756
8757/**
8758 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8759 * @phba: Pointer to HBA context object.
8760 * @eqe: Pointer to fast-path event queue entry.
8761 *
8762 * This routine process a event queue entry from the slow-path event queue.
8763 * It will check the MajorCode and MinorCode to determine this is for a
8764 * completion event on a completion queue, if not, an error shall be logged
8765 * and just return. Otherwise, it will get to the corresponding completion
8766 * queue and process all the entries on that completion queue, rearm the
8767 * completion queue, and then return.
8768 *
8769 **/
8770static void
8771lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8772{
8773 struct lpfc_queue *cq = NULL, *childq, *speq;
8774 struct lpfc_cqe *cqe;
8775 bool workposted = false;
8776 int ecount = 0;
8777 uint16_t cqid;
8778
8779 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8780 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8781 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8782 "0359 Not a valid slow-path completion "
8783 "event: majorcode=x%x, minorcode=x%x\n",
8784 bf_get(lpfc_eqe_major_code, eqe),
8785 bf_get(lpfc_eqe_minor_code, eqe));
8786 return;
8787 }
8788
8789 /* Get the reference to the corresponding CQ */
8790 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8791
8792 /* Search for completion queue pointer matching this cqid */
8793 speq = phba->sli4_hba.sp_eq;
8794 list_for_each_entry(childq, &speq->child_list, list) {
8795 if (childq->queue_id == cqid) {
8796 cq = childq;
8797 break;
8798 }
8799 }
8800 if (unlikely(!cq)) {
8801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8802 "0365 Slow-path CQ identifier (%d) does "
8803 "not exist\n", cqid);
8804 return;
8805 }
8806
8807 /* Process all the entries to the CQ */
8808 switch (cq->type) {
8809 case LPFC_MCQ:
8810 while ((cqe = lpfc_sli4_cq_get(cq))) {
8811 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8812 if (!(++ecount % LPFC_GET_QE_REL_INT))
8813 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8814 }
8815 break;
8816 case LPFC_WCQ:
8817 while ((cqe = lpfc_sli4_cq_get(cq))) {
8818 workposted |= lpfc_sli4_sp_handle_wcqe(phba, cq, cqe);
8819 if (!(++ecount % LPFC_GET_QE_REL_INT))
8820 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8821 }
8822 break;
8823 case LPFC_RCQ:
8824 while ((cqe = lpfc_sli4_cq_get(cq))) {
8825 workposted |= lpfc_sli4_sp_handle_rcqe(phba, cqe);
8826 if (!(++ecount % LPFC_GET_QE_REL_INT))
8827 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8828 }
8829 break;
8830 default:
8831 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8832 "0370 Invalid completion queue type (%d)\n",
8833 cq->type);
8834 return;
8835 }
8836
8837 /* Catch the no cq entry condition, log an error */
8838 if (unlikely(ecount == 0))
8839 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8840 "0371 No entry from the CQ: identifier "
8841 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8842
8843 /* In any case, flash and re-arm the RCQ */
8844 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8845
8846 /* wake up worker thread if there are works to be done */
8847 if (workposted)
8848 lpfc_worker_wake_up(phba);
8849}
8850
8851/**
8852 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8853 * @eqe: Pointer to fast-path completion queue entry.
8854 *
8855 * This routine process a fast-path work queue completion entry from fast-path
8856 * event queue for FCP command response completion.
8857 **/
8858static void
8859lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8860 struct lpfc_wcqe_complete *wcqe)
8861{
8862 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8863 struct lpfc_iocbq *cmdiocbq;
8864 struct lpfc_iocbq irspiocbq;
8865 unsigned long iflags;
8866
8867 spin_lock_irqsave(&phba->hbalock, iflags);
8868 pring->stats.iocb_event++;
8869 spin_unlock_irqrestore(&phba->hbalock, iflags);
8870
8871 /* Check for response status */
8872 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8873 /* If resource errors reported from HBA, reduce queue
8874 * depth of the SCSI device.
8875 */
8876 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8877 IOSTAT_LOCAL_REJECT) &&
8878 (wcqe->parameter == IOERR_NO_RESOURCES)) {
8879 phba->lpfc_rampdown_queue_depth(phba);
8880 }
8881 /* Log the error status */
8882 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8883 "0373 FCP complete error: status=x%x, "
8884 "hw_status=x%x, total_data_specified=%d, "
8885 "parameter=x%x, word3=x%x\n",
8886 bf_get(lpfc_wcqe_c_status, wcqe),
8887 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8888 wcqe->total_data_placed, wcqe->parameter,
8889 wcqe->word3);
8890 }
8891
8892 /* Look up the FCP command IOCB and create pseudo response IOCB */
8893 spin_lock_irqsave(&phba->hbalock, iflags);
8894 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8895 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8896 spin_unlock_irqrestore(&phba->hbalock, iflags);
8897 if (unlikely(!cmdiocbq)) {
8898 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8899 "0374 FCP complete with no corresponding "
8900 "cmdiocb: iotag (%d)\n",
8901 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8902 return;
8903 }
8904 if (unlikely(!cmdiocbq->iocb_cmpl)) {
8905 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8906 "0375 FCP cmdiocb not callback function "
8907 "iotag: (%d)\n",
8908 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8909 return;
8910 }
8911
8912 /* Fake the irspiocb and copy necessary response information */
8913 lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8914
8915 /* Pass the cmd_iocb and the rsp state to the upper layer */
8916 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8917}
8918
8919/**
8920 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8921 * @phba: Pointer to HBA context object.
8922 * @cq: Pointer to completion queue.
8923 * @wcqe: Pointer to work-queue completion queue entry.
8924 *
8925 * This routine handles an fast-path WQ entry comsumed event by invoking the
8926 * proper WQ release routine to the slow-path WQ.
8927 **/
8928static void
8929lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8930 struct lpfc_wcqe_release *wcqe)
8931{
8932 struct lpfc_queue *childwq;
8933 bool wqid_matched = false;
8934 uint16_t fcp_wqid;
8935
8936 /* Check for fast-path FCP work queue release */
8937 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
8938 list_for_each_entry(childwq, &cq->child_list, list) {
8939 if (childwq->queue_id == fcp_wqid) {
8940 lpfc_sli4_wq_release(childwq,
8941 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8942 wqid_matched = true;
8943 break;
8944 }
8945 }
8946 /* Report warning log message if no match found */
8947 if (wqid_matched != true)
8948 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8949 "2580 Fast-path wqe consume event carries "
8950 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
8951}
8952
8953/**
8954 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
8955 * @cq: Pointer to the completion queue.
8956 * @eqe: Pointer to fast-path completion queue entry.
8957 *
8958 * This routine process a fast-path work queue completion entry from fast-path
8959 * event queue for FCP command response completion.
8960 **/
8961static int
8962lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8963 struct lpfc_cqe *cqe)
8964{
8965 struct lpfc_wcqe_release wcqe;
8966 bool workposted = false;
8967
8968 /* Copy the work queue CQE and convert endian order if needed */
8969 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8970
8971 /* Check and process for different type of WCQE and dispatch */
8972 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8973 case CQE_CODE_COMPL_WQE:
8974 /* Process the WQ complete event */
8975 lpfc_sli4_fp_handle_fcp_wcqe(phba,
8976 (struct lpfc_wcqe_complete *)&wcqe);
8977 break;
8978 case CQE_CODE_RELEASE_WQE:
8979 /* Process the WQ release event */
8980 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
8981 (struct lpfc_wcqe_release *)&wcqe);
8982 break;
8983 case CQE_CODE_XRI_ABORTED:
8984 /* Process the WQ XRI abort event */
8985 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8986 (struct sli4_wcqe_xri_aborted *)&wcqe);
8987 break;
8988 default:
8989 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8990 "0144 Not a valid WCQE code: x%x\n",
8991 bf_get(lpfc_wcqe_c_code, &wcqe));
8992 break;
8993 }
8994 return workposted;
8995}
8996
8997/**
8998 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
8999 * @phba: Pointer to HBA context object.
9000 * @eqe: Pointer to fast-path event queue entry.
9001 *
9002 * This routine process a event queue entry from the fast-path event queue.
9003 * It will check the MajorCode and MinorCode to determine this is for a
9004 * completion event on a completion queue, if not, an error shall be logged
9005 * and just return. Otherwise, it will get to the corresponding completion
9006 * queue and process all the entries on the completion queue, rearm the
9007 * completion queue, and then return.
9008 **/
9009static void
9010lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9011 uint32_t fcp_cqidx)
9012{
9013 struct lpfc_queue *cq;
9014 struct lpfc_cqe *cqe;
9015 bool workposted = false;
9016 uint16_t cqid;
9017 int ecount = 0;
9018
9019 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9020 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9021 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9022 "0366 Not a valid fast-path completion "
9023 "event: majorcode=x%x, minorcode=x%x\n",
9024 bf_get(lpfc_eqe_major_code, eqe),
9025 bf_get(lpfc_eqe_minor_code, eqe));
9026 return;
9027 }
9028
9029 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9030 if (unlikely(!cq)) {
9031 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9032 "0367 Fast-path completion queue does not "
9033 "exist\n");
9034 return;
9035 }
9036
9037 /* Get the reference to the corresponding CQ */
9038 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9039 if (unlikely(cqid != cq->queue_id)) {
9040 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9041 "0368 Miss-matched fast-path completion "
9042 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9043 cqid, cq->queue_id);
9044 return;
9045 }
9046
9047 /* Process all the entries to the CQ */
9048 while ((cqe = lpfc_sli4_cq_get(cq))) {
9049 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9050 if (!(++ecount % LPFC_GET_QE_REL_INT))
9051 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9052 }
9053
9054 /* Catch the no cq entry condition */
9055 if (unlikely(ecount == 0))
9056 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9057 "0369 No entry from fast-path completion "
9058 "queue fcpcqid=%d\n", cq->queue_id);
9059
9060 /* In any case, flash and re-arm the CQ */
9061 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9062
9063 /* wake up worker thread if there are works to be done */
9064 if (workposted)
9065 lpfc_worker_wake_up(phba);
9066}
9067
9068static void
9069lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9070{
9071 struct lpfc_eqe *eqe;
9072
9073 /* walk all the EQ entries and drop on the floor */
9074 while ((eqe = lpfc_sli4_eq_get(eq)))
9075 ;
9076
9077 /* Clear and re-arm the EQ */
9078 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9079}
9080
9081/**
9082 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9083 * @irq: Interrupt number.
9084 * @dev_id: The device context pointer.
9085 *
9086 * This function is directly called from the PCI layer as an interrupt
9087 * service routine when device with SLI-4 interface spec is enabled with
9088 * MSI-X multi-message interrupt mode and there are slow-path events in
9089 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9090 * interrupt mode, this function is called as part of the device-level
9091 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9092 * undergoing initialization, the interrupt handler will not process the
9093 * interrupt. The link attention and ELS ring attention events are handled
9094 * by the worker thread. The interrupt handler signals the worker thread
9095 * and returns for these events. This function is called without any lock
9096 * held. It gets the hbalock to access and update SLI data structures.
9097 *
9098 * This function returns IRQ_HANDLED when interrupt is handled else it
9099 * returns IRQ_NONE.
9100 **/
9101irqreturn_t
9102lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9103{
9104 struct lpfc_hba *phba;
9105 struct lpfc_queue *speq;
9106 struct lpfc_eqe *eqe;
9107 unsigned long iflag;
9108 int ecount = 0;
9109
9110 /*
9111 * Get the driver's phba structure from the dev_id
9112 */
9113 phba = (struct lpfc_hba *)dev_id;
9114
9115 if (unlikely(!phba))
9116 return IRQ_NONE;
9117
9118 /* Get to the EQ struct associated with this vector */
9119 speq = phba->sli4_hba.sp_eq;
9120
9121 /* Check device state for handling interrupt */
9122 if (unlikely(lpfc_intr_state_check(phba))) {
9123 /* Check again for link_state with lock held */
9124 spin_lock_irqsave(&phba->hbalock, iflag);
9125 if (phba->link_state < LPFC_LINK_DOWN)
9126 /* Flush, clear interrupt, and rearm the EQ */
9127 lpfc_sli4_eq_flush(phba, speq);
9128 spin_unlock_irqrestore(&phba->hbalock, iflag);
9129 return IRQ_NONE;
9130 }
9131
9132 /*
9133 * Process all the event on FCP slow-path EQ
9134 */
9135 while ((eqe = lpfc_sli4_eq_get(speq))) {
9136 lpfc_sli4_sp_handle_eqe(phba, eqe);
9137 if (!(++ecount % LPFC_GET_QE_REL_INT))
9138 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9139 }
9140
9141 /* Always clear and re-arm the slow-path EQ */
9142 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9143
9144 /* Catch the no cq entry condition */
9145 if (unlikely(ecount == 0)) {
9146 if (phba->intr_type == MSIX)
9147 /* MSI-X treated interrupt served as no EQ share INT */
9148 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9149 "0357 MSI-X interrupt with no EQE\n");
9150 else
9151 /* Non MSI-X treated on interrupt as EQ share INT */
9152 return IRQ_NONE;
9153 }
9154
9155 return IRQ_HANDLED;
9156} /* lpfc_sli4_sp_intr_handler */
9157
9158/**
9159 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9160 * @irq: Interrupt number.
9161 * @dev_id: The device context pointer.
9162 *
9163 * This function is directly called from the PCI layer as an interrupt
9164 * service routine when device with SLI-4 interface spec is enabled with
9165 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9166 * ring event in the HBA. However, when the device is enabled with either
9167 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9168 * device-level interrupt handler. When the PCI slot is in error recovery
9169 * or the HBA is undergoing initialization, the interrupt handler will not
9170 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9171 * the intrrupt context. This function is called without any lock held.
9172 * It gets the hbalock to access and update SLI data structures. Note that,
9173 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9174 * equal to that of FCP CQ index.
9175 *
9176 * This function returns IRQ_HANDLED when interrupt is handled else it
9177 * returns IRQ_NONE.
9178 **/
9179irqreturn_t
9180lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9181{
9182 struct lpfc_hba *phba;
9183 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9184 struct lpfc_queue *fpeq;
9185 struct lpfc_eqe *eqe;
9186 unsigned long iflag;
9187 int ecount = 0;
9188 uint32_t fcp_eqidx;
9189
9190 /* Get the driver's phba structure from the dev_id */
9191 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9192 phba = fcp_eq_hdl->phba;
9193 fcp_eqidx = fcp_eq_hdl->idx;
9194
9195 if (unlikely(!phba))
9196 return IRQ_NONE;
9197
9198 /* Get to the EQ struct associated with this vector */
9199 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9200
9201 /* Check device state for handling interrupt */
9202 if (unlikely(lpfc_intr_state_check(phba))) {
9203 /* Check again for link_state with lock held */
9204 spin_lock_irqsave(&phba->hbalock, iflag);
9205 if (phba->link_state < LPFC_LINK_DOWN)
9206 /* Flush, clear interrupt, and rearm the EQ */
9207 lpfc_sli4_eq_flush(phba, fpeq);
9208 spin_unlock_irqrestore(&phba->hbalock, iflag);
9209 return IRQ_NONE;
9210 }
9211
9212 /*
9213 * Process all the event on FCP fast-path EQ
9214 */
9215 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9216 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9217 if (!(++ecount % LPFC_GET_QE_REL_INT))
9218 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9219 }
9220
9221 /* Always clear and re-arm the fast-path EQ */
9222 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9223
9224 if (unlikely(ecount == 0)) {
9225 if (phba->intr_type == MSIX)
9226 /* MSI-X treated interrupt served as no EQ share INT */
9227 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9228 "0358 MSI-X interrupt with no EQE\n");
9229 else
9230 /* Non MSI-X treated on interrupt as EQ share INT */
9231 return IRQ_NONE;
9232 }
9233
9234 return IRQ_HANDLED;
9235} /* lpfc_sli4_fp_intr_handler */
9236
9237/**
9238 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9239 * @irq: Interrupt number.
9240 * @dev_id: The device context pointer.
9241 *
9242 * This function is the device-level interrupt handler to device with SLI-4
9243 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9244 * interrupt mode is enabled and there is an event in the HBA which requires
9245 * driver attention. This function invokes the slow-path interrupt attention
9246 * handling function and fast-path interrupt attention handling function in
9247 * turn to process the relevant HBA attention events. This function is called
9248 * without any lock held. It gets the hbalock to access and update SLI data
9249 * structures.
9250 *
9251 * This function returns IRQ_HANDLED when interrupt is handled, else it
9252 * returns IRQ_NONE.
9253 **/
9254irqreturn_t
9255lpfc_sli4_intr_handler(int irq, void *dev_id)
9256{
9257 struct lpfc_hba *phba;
9258 irqreturn_t sp_irq_rc, fp_irq_rc;
9259 bool fp_handled = false;
9260 uint32_t fcp_eqidx;
9261
9262 /* Get the driver's phba structure from the dev_id */
9263 phba = (struct lpfc_hba *)dev_id;
9264
9265 if (unlikely(!phba))
9266 return IRQ_NONE;
9267
9268 /*
9269 * Invokes slow-path host attention interrupt handling as appropriate.
9270 */
9271 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9272
9273 /*
9274 * Invoke fast-path host attention interrupt handling as appropriate.
9275 */
9276 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9277 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9278 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9279 if (fp_irq_rc == IRQ_HANDLED)
9280 fp_handled |= true;
9281 }
9282
9283 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9284} /* lpfc_sli4_intr_handler */
9285
9286/**
9287 * lpfc_sli4_queue_free - free a queue structure and associated memory
9288 * @queue: The queue structure to free.
9289 *
9290 * This function frees a queue structure and the DMAable memeory used for
9291 * the host resident queue. This function must be called after destroying the
9292 * queue on the HBA.
9293 **/
9294void
9295lpfc_sli4_queue_free(struct lpfc_queue *queue)
9296{
9297 struct lpfc_dmabuf *dmabuf;
9298
9299 if (!queue)
9300 return;
9301
9302 while (!list_empty(&queue->page_list)) {
9303 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9304 list);
9305 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9306 dmabuf->virt, dmabuf->phys);
9307 kfree(dmabuf);
9308 }
9309 kfree(queue);
9310 return;
9311}
9312
9313/**
9314 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9315 * @phba: The HBA that this queue is being created on.
9316 * @entry_size: The size of each queue entry for this queue.
9317 * @entry count: The number of entries that this queue will handle.
9318 *
9319 * This function allocates a queue structure and the DMAable memory used for
9320 * the host resident queue. This function must be called before creating the
9321 * queue on the HBA.
9322 **/
9323struct lpfc_queue *
9324lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9325 uint32_t entry_count)
9326{
9327 struct lpfc_queue *queue;
9328 struct lpfc_dmabuf *dmabuf;
9329 int x, total_qe_count;
9330 void *dma_pointer;
9331
9332
9333 queue = kzalloc(sizeof(struct lpfc_queue) +
9334 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9335 if (!queue)
9336 return NULL;
9337 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9338 INIT_LIST_HEAD(&queue->list);
9339 INIT_LIST_HEAD(&queue->page_list);
9340 INIT_LIST_HEAD(&queue->child_list);
9341 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9342 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9343 if (!dmabuf)
9344 goto out_fail;
9345 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9346 PAGE_SIZE, &dmabuf->phys,
9347 GFP_KERNEL);
9348 if (!dmabuf->virt) {
9349 kfree(dmabuf);
9350 goto out_fail;
9351 }
9352 dmabuf->buffer_tag = x;
9353 list_add_tail(&dmabuf->list, &queue->page_list);
9354 /* initialize queue's entry array */
9355 dma_pointer = dmabuf->virt;
9356 for (; total_qe_count < entry_count &&
9357 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9358 total_qe_count++, dma_pointer += entry_size) {
9359 queue->qe[total_qe_count].address = dma_pointer;
9360 }
9361 }
9362 queue->entry_size = entry_size;
9363 queue->entry_count = entry_count;
9364 queue->phba = phba;
9365
9366 return queue;
9367out_fail:
9368 lpfc_sli4_queue_free(queue);
9369 return NULL;
9370}
9371
9372/**
9373 * lpfc_eq_create - Create an Event Queue on the HBA
9374 * @phba: HBA structure that indicates port to create a queue on.
9375 * @eq: The queue structure to use to create the event queue.
9376 * @imax: The maximum interrupt per second limit.
9377 *
9378 * This function creates an event queue, as detailed in @eq, on a port,
9379 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9380 *
9381 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9382 * is used to get the entry count and entry size that are necessary to
9383 * determine the number of pages to allocate and use for this queue. This
9384 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9385 * event queue. This function is asynchronous and will wait for the mailbox
9386 * command to finish before continuing.
9387 *
9388 * On success this function will return a zero. If unable to allocate enough
9389 * memory this function will return ENOMEM. If the queue create mailbox command
9390 * fails this function will return ENXIO.
9391 **/
9392uint32_t
9393lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9394{
9395 struct lpfc_mbx_eq_create *eq_create;
9396 LPFC_MBOXQ_t *mbox;
9397 int rc, length, status = 0;
9398 struct lpfc_dmabuf *dmabuf;
9399 uint32_t shdr_status, shdr_add_status;
9400 union lpfc_sli4_cfg_shdr *shdr;
9401 uint16_t dmult;
9402
9403 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9404 if (!mbox)
9405 return -ENOMEM;
9406 length = (sizeof(struct lpfc_mbx_eq_create) -
9407 sizeof(struct lpfc_sli4_cfg_mhdr));
9408 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9409 LPFC_MBOX_OPCODE_EQ_CREATE,
9410 length, LPFC_SLI4_MBX_EMBED);
9411 eq_create = &mbox->u.mqe.un.eq_create;
9412 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9413 eq->page_count);
9414 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9415 LPFC_EQE_SIZE);
9416 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9417 /* Calculate delay multiper from maximum interrupt per second */
9418 dmult = LPFC_DMULT_CONST/imax - 1;
9419 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9420 dmult);
9421 switch (eq->entry_count) {
9422 default:
9423 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9424 "0360 Unsupported EQ count. (%d)\n",
9425 eq->entry_count);
9426 if (eq->entry_count < 256)
9427 return -EINVAL;
9428 /* otherwise default to smallest count (drop through) */
9429 case 256:
9430 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9431 LPFC_EQ_CNT_256);
9432 break;
9433 case 512:
9434 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9435 LPFC_EQ_CNT_512);
9436 break;
9437 case 1024:
9438 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9439 LPFC_EQ_CNT_1024);
9440 break;
9441 case 2048:
9442 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9443 LPFC_EQ_CNT_2048);
9444 break;
9445 case 4096:
9446 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9447 LPFC_EQ_CNT_4096);
9448 break;
9449 }
9450 list_for_each_entry(dmabuf, &eq->page_list, list) {
9451 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9452 putPaddrLow(dmabuf->phys);
9453 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9454 putPaddrHigh(dmabuf->phys);
9455 }
9456 mbox->vport = phba->pport;
9457 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9458 mbox->context1 = NULL;
9459 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9460 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9461 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9462 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9463 if (shdr_status || shdr_add_status || rc) {
9464 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9465 "2500 EQ_CREATE mailbox failed with "
9466 "status x%x add_status x%x, mbx status x%x\n",
9467 shdr_status, shdr_add_status, rc);
9468 status = -ENXIO;
9469 }
9470 eq->type = LPFC_EQ;
9471 eq->subtype = LPFC_NONE;
9472 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9473 if (eq->queue_id == 0xFFFF)
9474 status = -ENXIO;
9475 eq->host_index = 0;
9476 eq->hba_index = 0;
9477
9478 if (rc != MBX_TIMEOUT)
9479 mempool_free(mbox, phba->mbox_mem_pool);
9480 return status;
9481}
9482
9483/**
9484 * lpfc_cq_create - Create a Completion Queue on the HBA
9485 * @phba: HBA structure that indicates port to create a queue on.
9486 * @cq: The queue structure to use to create the completion queue.
9487 * @eq: The event queue to bind this completion queue to.
9488 *
9489 * This function creates a completion queue, as detailed in @wq, on a port,
9490 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9491 *
9492 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9493 * is used to get the entry count and entry size that are necessary to
9494 * determine the number of pages to allocate and use for this queue. The @eq
9495 * is used to indicate which event queue to bind this completion queue to. This
9496 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9497 * completion queue. This function is asynchronous and will wait for the mailbox
9498 * command to finish before continuing.
9499 *
9500 * On success this function will return a zero. If unable to allocate enough
9501 * memory this function will return ENOMEM. If the queue create mailbox command
9502 * fails this function will return ENXIO.
9503 **/
9504uint32_t
9505lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9506 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9507{
9508 struct lpfc_mbx_cq_create *cq_create;
9509 struct lpfc_dmabuf *dmabuf;
9510 LPFC_MBOXQ_t *mbox;
9511 int rc, length, status = 0;
9512 uint32_t shdr_status, shdr_add_status;
9513 union lpfc_sli4_cfg_shdr *shdr;
9514
9515 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9516 if (!mbox)
9517 return -ENOMEM;
9518 length = (sizeof(struct lpfc_mbx_cq_create) -
9519 sizeof(struct lpfc_sli4_cfg_mhdr));
9520 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9521 LPFC_MBOX_OPCODE_CQ_CREATE,
9522 length, LPFC_SLI4_MBX_EMBED);
9523 cq_create = &mbox->u.mqe.un.cq_create;
9524 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9525 cq->page_count);
9526 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9527 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9528 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9529 switch (cq->entry_count) {
9530 default:
9531 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9532 "0361 Unsupported CQ count. (%d)\n",
9533 cq->entry_count);
9534 if (cq->entry_count < 256)
9535 return -EINVAL;
9536 /* otherwise default to smallest count (drop through) */
9537 case 256:
9538 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9539 LPFC_CQ_CNT_256);
9540 break;
9541 case 512:
9542 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9543 LPFC_CQ_CNT_512);
9544 break;
9545 case 1024:
9546 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9547 LPFC_CQ_CNT_1024);
9548 break;
9549 }
9550 list_for_each_entry(dmabuf, &cq->page_list, list) {
9551 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9552 putPaddrLow(dmabuf->phys);
9553 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9554 putPaddrHigh(dmabuf->phys);
9555 }
9556 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9557
9558 /* The IOCTL status is embedded in the mailbox subheader. */
9559 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9560 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9561 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9562 if (shdr_status || shdr_add_status || rc) {
9563 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9564 "2501 CQ_CREATE mailbox failed with "
9565 "status x%x add_status x%x, mbx status x%x\n",
9566 shdr_status, shdr_add_status, rc);
9567 status = -ENXIO;
9568 goto out;
9569 }
9570 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9571 if (cq->queue_id == 0xFFFF) {
9572 status = -ENXIO;
9573 goto out;
9574 }
9575 /* link the cq onto the parent eq child list */
9576 list_add_tail(&cq->list, &eq->child_list);
9577 /* Set up completion queue's type and subtype */
9578 cq->type = type;
9579 cq->subtype = subtype;
9580 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9581 cq->host_index = 0;
9582 cq->hba_index = 0;
9583out:
9584
9585 if (rc != MBX_TIMEOUT)
9586 mempool_free(mbox, phba->mbox_mem_pool);
9587 return status;
9588}
9589
9590/**
James Smart04c68492009-05-22 14:52:52 -04009591 * lpfc_mq_create - Create a mailbox Queue on the HBA
9592 * @phba: HBA structure that indicates port to create a queue on.
9593 * @mq: The queue structure to use to create the mailbox queue.
9594 *
9595 * This function creates a mailbox queue, as detailed in @mq, on a port,
9596 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9597 *
9598 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9599 * is used to get the entry count and entry size that are necessary to
9600 * determine the number of pages to allocate and use for this queue. This
9601 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9602 * mailbox queue. This function is asynchronous and will wait for the mailbox
9603 * command to finish before continuing.
9604 *
9605 * On success this function will return a zero. If unable to allocate enough
9606 * memory this function will return ENOMEM. If the queue create mailbox command
9607 * fails this function will return ENXIO.
9608 **/
9609uint32_t
9610lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9611 struct lpfc_queue *cq, uint32_t subtype)
9612{
9613 struct lpfc_mbx_mq_create *mq_create;
9614 struct lpfc_dmabuf *dmabuf;
9615 LPFC_MBOXQ_t *mbox;
9616 int rc, length, status = 0;
9617 uint32_t shdr_status, shdr_add_status;
9618 union lpfc_sli4_cfg_shdr *shdr;
9619
9620 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9621 if (!mbox)
9622 return -ENOMEM;
9623 length = (sizeof(struct lpfc_mbx_mq_create) -
9624 sizeof(struct lpfc_sli4_cfg_mhdr));
9625 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9626 LPFC_MBOX_OPCODE_MQ_CREATE,
9627 length, LPFC_SLI4_MBX_EMBED);
9628 mq_create = &mbox->u.mqe.un.mq_create;
9629 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9630 mq->page_count);
9631 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9632 cq->queue_id);
9633 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9634 switch (mq->entry_count) {
9635 default:
9636 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9637 "0362 Unsupported MQ count. (%d)\n",
9638 mq->entry_count);
9639 if (mq->entry_count < 16)
9640 return -EINVAL;
9641 /* otherwise default to smallest count (drop through) */
9642 case 16:
9643 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9644 LPFC_MQ_CNT_16);
9645 break;
9646 case 32:
9647 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9648 LPFC_MQ_CNT_32);
9649 break;
9650 case 64:
9651 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9652 LPFC_MQ_CNT_64);
9653 break;
9654 case 128:
9655 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9656 LPFC_MQ_CNT_128);
9657 break;
9658 }
9659 list_for_each_entry(dmabuf, &mq->page_list, list) {
9660 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9661 putPaddrLow(dmabuf->phys);
9662 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9663 putPaddrHigh(dmabuf->phys);
9664 }
9665 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9666 /* The IOCTL status is embedded in the mailbox subheader. */
9667 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9668 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9669 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9670 if (shdr_status || shdr_add_status || rc) {
9671 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9672 "2502 MQ_CREATE mailbox failed with "
9673 "status x%x add_status x%x, mbx status x%x\n",
9674 shdr_status, shdr_add_status, rc);
9675 status = -ENXIO;
9676 goto out;
9677 }
9678 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9679 if (mq->queue_id == 0xFFFF) {
9680 status = -ENXIO;
9681 goto out;
9682 }
9683 mq->type = LPFC_MQ;
9684 mq->subtype = subtype;
9685 mq->host_index = 0;
9686 mq->hba_index = 0;
9687
9688 /* link the mq onto the parent cq child list */
9689 list_add_tail(&mq->list, &cq->child_list);
9690out:
9691 if (rc != MBX_TIMEOUT)
9692 mempool_free(mbox, phba->mbox_mem_pool);
9693 return status;
9694}
9695
9696/**
James Smart4f774512009-05-22 14:52:35 -04009697 * lpfc_wq_create - Create a Work Queue on the HBA
9698 * @phba: HBA structure that indicates port to create a queue on.
9699 * @wq: The queue structure to use to create the work queue.
9700 * @cq: The completion queue to bind this work queue to.
9701 * @subtype: The subtype of the work queue indicating its functionality.
9702 *
9703 * This function creates a work queue, as detailed in @wq, on a port, described
9704 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9705 *
9706 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9707 * is used to get the entry count and entry size that are necessary to
9708 * determine the number of pages to allocate and use for this queue. The @cq
9709 * is used to indicate which completion queue to bind this work queue to. This
9710 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9711 * work queue. This function is asynchronous and will wait for the mailbox
9712 * command to finish before continuing.
9713 *
9714 * On success this function will return a zero. If unable to allocate enough
9715 * memory this function will return ENOMEM. If the queue create mailbox command
9716 * fails this function will return ENXIO.
9717 **/
9718uint32_t
9719lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9720 struct lpfc_queue *cq, uint32_t subtype)
9721{
9722 struct lpfc_mbx_wq_create *wq_create;
9723 struct lpfc_dmabuf *dmabuf;
9724 LPFC_MBOXQ_t *mbox;
9725 int rc, length, status = 0;
9726 uint32_t shdr_status, shdr_add_status;
9727 union lpfc_sli4_cfg_shdr *shdr;
9728
9729 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9730 if (!mbox)
9731 return -ENOMEM;
9732 length = (sizeof(struct lpfc_mbx_wq_create) -
9733 sizeof(struct lpfc_sli4_cfg_mhdr));
9734 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9735 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9736 length, LPFC_SLI4_MBX_EMBED);
9737 wq_create = &mbox->u.mqe.un.wq_create;
9738 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9739 wq->page_count);
9740 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9741 cq->queue_id);
9742 list_for_each_entry(dmabuf, &wq->page_list, list) {
9743 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9744 putPaddrLow(dmabuf->phys);
9745 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9746 putPaddrHigh(dmabuf->phys);
9747 }
9748 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9749 /* The IOCTL status is embedded in the mailbox subheader. */
9750 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9751 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9752 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9753 if (shdr_status || shdr_add_status || rc) {
9754 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9755 "2503 WQ_CREATE mailbox failed with "
9756 "status x%x add_status x%x, mbx status x%x\n",
9757 shdr_status, shdr_add_status, rc);
9758 status = -ENXIO;
9759 goto out;
9760 }
9761 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9762 if (wq->queue_id == 0xFFFF) {
9763 status = -ENXIO;
9764 goto out;
9765 }
9766 wq->type = LPFC_WQ;
9767 wq->subtype = subtype;
9768 wq->host_index = 0;
9769 wq->hba_index = 0;
9770
9771 /* link the wq onto the parent cq child list */
9772 list_add_tail(&wq->list, &cq->child_list);
9773out:
9774 if (rc == MBX_TIMEOUT)
9775 mempool_free(mbox, phba->mbox_mem_pool);
9776 return status;
9777}
9778
9779/**
9780 * lpfc_rq_create - Create a Receive Queue on the HBA
9781 * @phba: HBA structure that indicates port to create a queue on.
9782 * @hrq: The queue structure to use to create the header receive queue.
9783 * @drq: The queue structure to use to create the data receive queue.
9784 * @cq: The completion queue to bind this work queue to.
9785 *
9786 * This function creates a receive buffer queue pair , as detailed in @hrq and
9787 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9788 * to the HBA.
9789 *
9790 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9791 * struct is used to get the entry count that is necessary to determine the
9792 * number of pages to use for this queue. The @cq is used to indicate which
9793 * completion queue to bind received buffers that are posted to these queues to.
9794 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9795 * receive queue pair. This function is asynchronous and will wait for the
9796 * mailbox command to finish before continuing.
9797 *
9798 * On success this function will return a zero. If unable to allocate enough
9799 * memory this function will return ENOMEM. If the queue create mailbox command
9800 * fails this function will return ENXIO.
9801 **/
9802uint32_t
9803lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9804 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9805{
9806 struct lpfc_mbx_rq_create *rq_create;
9807 struct lpfc_dmabuf *dmabuf;
9808 LPFC_MBOXQ_t *mbox;
9809 int rc, length, status = 0;
9810 uint32_t shdr_status, shdr_add_status;
9811 union lpfc_sli4_cfg_shdr *shdr;
9812
9813 if (hrq->entry_count != drq->entry_count)
9814 return -EINVAL;
9815 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9816 if (!mbox)
9817 return -ENOMEM;
9818 length = (sizeof(struct lpfc_mbx_rq_create) -
9819 sizeof(struct lpfc_sli4_cfg_mhdr));
9820 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9821 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9822 length, LPFC_SLI4_MBX_EMBED);
9823 rq_create = &mbox->u.mqe.un.rq_create;
9824 switch (hrq->entry_count) {
9825 default:
9826 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9827 "2535 Unsupported RQ count. (%d)\n",
9828 hrq->entry_count);
9829 if (hrq->entry_count < 512)
9830 return -EINVAL;
9831 /* otherwise default to smallest count (drop through) */
9832 case 512:
9833 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9834 LPFC_RQ_RING_SIZE_512);
9835 break;
9836 case 1024:
9837 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9838 LPFC_RQ_RING_SIZE_1024);
9839 break;
9840 case 2048:
9841 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9842 LPFC_RQ_RING_SIZE_2048);
9843 break;
9844 case 4096:
9845 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9846 LPFC_RQ_RING_SIZE_4096);
9847 break;
9848 }
9849 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9850 cq->queue_id);
9851 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9852 hrq->page_count);
9853 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9854 LPFC_HDR_BUF_SIZE);
9855 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9856 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9857 putPaddrLow(dmabuf->phys);
9858 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9859 putPaddrHigh(dmabuf->phys);
9860 }
9861 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9862 /* The IOCTL status is embedded in the mailbox subheader. */
9863 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9864 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9865 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9866 if (shdr_status || shdr_add_status || rc) {
9867 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9868 "2504 RQ_CREATE mailbox failed with "
9869 "status x%x add_status x%x, mbx status x%x\n",
9870 shdr_status, shdr_add_status, rc);
9871 status = -ENXIO;
9872 goto out;
9873 }
9874 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9875 if (hrq->queue_id == 0xFFFF) {
9876 status = -ENXIO;
9877 goto out;
9878 }
9879 hrq->type = LPFC_HRQ;
9880 hrq->subtype = subtype;
9881 hrq->host_index = 0;
9882 hrq->hba_index = 0;
9883
9884 /* now create the data queue */
9885 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9886 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9887 length, LPFC_SLI4_MBX_EMBED);
9888 switch (drq->entry_count) {
9889 default:
9890 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9891 "2536 Unsupported RQ count. (%d)\n",
9892 drq->entry_count);
9893 if (drq->entry_count < 512)
9894 return -EINVAL;
9895 /* otherwise default to smallest count (drop through) */
9896 case 512:
9897 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9898 LPFC_RQ_RING_SIZE_512);
9899 break;
9900 case 1024:
9901 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9902 LPFC_RQ_RING_SIZE_1024);
9903 break;
9904 case 2048:
9905 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9906 LPFC_RQ_RING_SIZE_2048);
9907 break;
9908 case 4096:
9909 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9910 LPFC_RQ_RING_SIZE_4096);
9911 break;
9912 }
9913 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9914 cq->queue_id);
9915 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9916 drq->page_count);
9917 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9918 LPFC_DATA_BUF_SIZE);
9919 list_for_each_entry(dmabuf, &drq->page_list, list) {
9920 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9921 putPaddrLow(dmabuf->phys);
9922 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9923 putPaddrHigh(dmabuf->phys);
9924 }
9925 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9926 /* The IOCTL status is embedded in the mailbox subheader. */
9927 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9928 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9929 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9930 if (shdr_status || shdr_add_status || rc) {
9931 status = -ENXIO;
9932 goto out;
9933 }
9934 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9935 if (drq->queue_id == 0xFFFF) {
9936 status = -ENXIO;
9937 goto out;
9938 }
9939 drq->type = LPFC_DRQ;
9940 drq->subtype = subtype;
9941 drq->host_index = 0;
9942 drq->hba_index = 0;
9943
9944 /* link the header and data RQs onto the parent cq child list */
9945 list_add_tail(&hrq->list, &cq->child_list);
9946 list_add_tail(&drq->list, &cq->child_list);
9947
9948out:
9949 if (rc != MBX_TIMEOUT)
9950 mempool_free(mbox, phba->mbox_mem_pool);
9951 return status;
9952}
9953
9954/**
9955 * lpfc_eq_destroy - Destroy an event Queue on the HBA
9956 * @eq: The queue structure associated with the queue to destroy.
9957 *
9958 * This function destroys a queue, as detailed in @eq by sending an mailbox
9959 * command, specific to the type of queue, to the HBA.
9960 *
9961 * The @eq struct is used to get the queue ID of the queue to destroy.
9962 *
9963 * On success this function will return a zero. If the queue destroy mailbox
9964 * command fails this function will return ENXIO.
9965 **/
9966uint32_t
9967lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
9968{
9969 LPFC_MBOXQ_t *mbox;
9970 int rc, length, status = 0;
9971 uint32_t shdr_status, shdr_add_status;
9972 union lpfc_sli4_cfg_shdr *shdr;
9973
9974 if (!eq)
9975 return -ENODEV;
9976 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
9977 if (!mbox)
9978 return -ENOMEM;
9979 length = (sizeof(struct lpfc_mbx_eq_destroy) -
9980 sizeof(struct lpfc_sli4_cfg_mhdr));
9981 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9982 LPFC_MBOX_OPCODE_EQ_DESTROY,
9983 length, LPFC_SLI4_MBX_EMBED);
9984 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
9985 eq->queue_id);
9986 mbox->vport = eq->phba->pport;
9987 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9988
9989 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
9990 /* The IOCTL status is embedded in the mailbox subheader. */
9991 shdr = (union lpfc_sli4_cfg_shdr *)
9992 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
9993 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9994 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9995 if (shdr_status || shdr_add_status || rc) {
9996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9997 "2505 EQ_DESTROY mailbox failed with "
9998 "status x%x add_status x%x, mbx status x%x\n",
9999 shdr_status, shdr_add_status, rc);
10000 status = -ENXIO;
10001 }
10002
10003 /* Remove eq from any list */
10004 list_del_init(&eq->list);
10005 if (rc != MBX_TIMEOUT)
10006 mempool_free(mbox, eq->phba->mbox_mem_pool);
10007 return status;
10008}
10009
10010/**
10011 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10012 * @cq: The queue structure associated with the queue to destroy.
10013 *
10014 * This function destroys a queue, as detailed in @cq by sending an mailbox
10015 * command, specific to the type of queue, to the HBA.
10016 *
10017 * The @cq struct is used to get the queue ID of the queue to destroy.
10018 *
10019 * On success this function will return a zero. If the queue destroy mailbox
10020 * command fails this function will return ENXIO.
10021 **/
10022uint32_t
10023lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10024{
10025 LPFC_MBOXQ_t *mbox;
10026 int rc, length, status = 0;
10027 uint32_t shdr_status, shdr_add_status;
10028 union lpfc_sli4_cfg_shdr *shdr;
10029
10030 if (!cq)
10031 return -ENODEV;
10032 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10033 if (!mbox)
10034 return -ENOMEM;
10035 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10036 sizeof(struct lpfc_sli4_cfg_mhdr));
10037 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10038 LPFC_MBOX_OPCODE_CQ_DESTROY,
10039 length, LPFC_SLI4_MBX_EMBED);
10040 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10041 cq->queue_id);
10042 mbox->vport = cq->phba->pport;
10043 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10044 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10045 /* The IOCTL status is embedded in the mailbox subheader. */
10046 shdr = (union lpfc_sli4_cfg_shdr *)
10047 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10048 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10049 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10050 if (shdr_status || shdr_add_status || rc) {
10051 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10052 "2506 CQ_DESTROY mailbox failed with "
10053 "status x%x add_status x%x, mbx status x%x\n",
10054 shdr_status, shdr_add_status, rc);
10055 status = -ENXIO;
10056 }
10057 /* Remove cq from any list */
10058 list_del_init(&cq->list);
10059 if (rc != MBX_TIMEOUT)
10060 mempool_free(mbox, cq->phba->mbox_mem_pool);
10061 return status;
10062}
10063
10064/**
James Smart04c68492009-05-22 14:52:52 -040010065 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10066 * @qm: The queue structure associated with the queue to destroy.
10067 *
10068 * This function destroys a queue, as detailed in @mq by sending an mailbox
10069 * command, specific to the type of queue, to the HBA.
10070 *
10071 * The @mq struct is used to get the queue ID of the queue to destroy.
10072 *
10073 * On success this function will return a zero. If the queue destroy mailbox
10074 * command fails this function will return ENXIO.
10075 **/
10076uint32_t
10077lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10078{
10079 LPFC_MBOXQ_t *mbox;
10080 int rc, length, status = 0;
10081 uint32_t shdr_status, shdr_add_status;
10082 union lpfc_sli4_cfg_shdr *shdr;
10083
10084 if (!mq)
10085 return -ENODEV;
10086 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10087 if (!mbox)
10088 return -ENOMEM;
10089 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10090 sizeof(struct lpfc_sli4_cfg_mhdr));
10091 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10092 LPFC_MBOX_OPCODE_MQ_DESTROY,
10093 length, LPFC_SLI4_MBX_EMBED);
10094 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10095 mq->queue_id);
10096 mbox->vport = mq->phba->pport;
10097 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10098 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10099 /* The IOCTL status is embedded in the mailbox subheader. */
10100 shdr = (union lpfc_sli4_cfg_shdr *)
10101 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10102 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10103 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10104 if (shdr_status || shdr_add_status || rc) {
10105 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10106 "2507 MQ_DESTROY mailbox failed with "
10107 "status x%x add_status x%x, mbx status x%x\n",
10108 shdr_status, shdr_add_status, rc);
10109 status = -ENXIO;
10110 }
10111 /* Remove mq from any list */
10112 list_del_init(&mq->list);
10113 if (rc != MBX_TIMEOUT)
10114 mempool_free(mbox, mq->phba->mbox_mem_pool);
10115 return status;
10116}
10117
10118/**
James Smart4f774512009-05-22 14:52:35 -040010119 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10120 * @wq: The queue structure associated with the queue to destroy.
10121 *
10122 * This function destroys a queue, as detailed in @wq by sending an mailbox
10123 * command, specific to the type of queue, to the HBA.
10124 *
10125 * The @wq struct is used to get the queue ID of the queue to destroy.
10126 *
10127 * On success this function will return a zero. If the queue destroy mailbox
10128 * command fails this function will return ENXIO.
10129 **/
10130uint32_t
10131lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10132{
10133 LPFC_MBOXQ_t *mbox;
10134 int rc, length, status = 0;
10135 uint32_t shdr_status, shdr_add_status;
10136 union lpfc_sli4_cfg_shdr *shdr;
10137
10138 if (!wq)
10139 return -ENODEV;
10140 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10141 if (!mbox)
10142 return -ENOMEM;
10143 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10144 sizeof(struct lpfc_sli4_cfg_mhdr));
10145 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10146 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10147 length, LPFC_SLI4_MBX_EMBED);
10148 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10149 wq->queue_id);
10150 mbox->vport = wq->phba->pport;
10151 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10152 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10153 shdr = (union lpfc_sli4_cfg_shdr *)
10154 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10155 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10156 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10157 if (shdr_status || shdr_add_status || rc) {
10158 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10159 "2508 WQ_DESTROY mailbox failed with "
10160 "status x%x add_status x%x, mbx status x%x\n",
10161 shdr_status, shdr_add_status, rc);
10162 status = -ENXIO;
10163 }
10164 /* Remove wq from any list */
10165 list_del_init(&wq->list);
10166 if (rc != MBX_TIMEOUT)
10167 mempool_free(mbox, wq->phba->mbox_mem_pool);
10168 return status;
10169}
10170
10171/**
10172 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10173 * @rq: The queue structure associated with the queue to destroy.
10174 *
10175 * This function destroys a queue, as detailed in @rq by sending an mailbox
10176 * command, specific to the type of queue, to the HBA.
10177 *
10178 * The @rq struct is used to get the queue ID of the queue to destroy.
10179 *
10180 * On success this function will return a zero. If the queue destroy mailbox
10181 * command fails this function will return ENXIO.
10182 **/
10183uint32_t
10184lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10185 struct lpfc_queue *drq)
10186{
10187 LPFC_MBOXQ_t *mbox;
10188 int rc, length, status = 0;
10189 uint32_t shdr_status, shdr_add_status;
10190 union lpfc_sli4_cfg_shdr *shdr;
10191
10192 if (!hrq || !drq)
10193 return -ENODEV;
10194 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10195 if (!mbox)
10196 return -ENOMEM;
10197 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10198 sizeof(struct mbox_header));
10199 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10200 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10201 length, LPFC_SLI4_MBX_EMBED);
10202 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10203 hrq->queue_id);
10204 mbox->vport = hrq->phba->pport;
10205 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10206 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10207 /* The IOCTL status is embedded in the mailbox subheader. */
10208 shdr = (union lpfc_sli4_cfg_shdr *)
10209 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10210 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10211 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10212 if (shdr_status || shdr_add_status || rc) {
10213 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10214 "2509 RQ_DESTROY mailbox failed with "
10215 "status x%x add_status x%x, mbx status x%x\n",
10216 shdr_status, shdr_add_status, rc);
10217 if (rc != MBX_TIMEOUT)
10218 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10219 return -ENXIO;
10220 }
10221 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10222 drq->queue_id);
10223 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10224 shdr = (union lpfc_sli4_cfg_shdr *)
10225 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10226 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10227 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10228 if (shdr_status || shdr_add_status || rc) {
10229 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10230 "2510 RQ_DESTROY mailbox failed with "
10231 "status x%x add_status x%x, mbx status x%x\n",
10232 shdr_status, shdr_add_status, rc);
10233 status = -ENXIO;
10234 }
10235 list_del_init(&hrq->list);
10236 list_del_init(&drq->list);
10237 if (rc != MBX_TIMEOUT)
10238 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10239 return status;
10240}
10241
10242/**
10243 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10244 * @phba: The virtual port for which this call being executed.
10245 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10246 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10247 * @xritag: the xritag that ties this io to the SGL pages.
10248 *
10249 * This routine will post the sgl pages for the IO that has the xritag
10250 * that is in the iocbq structure. The xritag is assigned during iocbq
10251 * creation and persists for as long as the driver is loaded.
10252 * if the caller has fewer than 256 scatter gather segments to map then
10253 * pdma_phys_addr1 should be 0.
10254 * If the caller needs to map more than 256 scatter gather segment then
10255 * pdma_phys_addr1 should be a valid physical address.
10256 * physical address for SGLs must be 64 byte aligned.
10257 * If you are going to map 2 SGL's then the first one must have 256 entries
10258 * the second sgl can have between 1 and 256 entries.
10259 *
10260 * Return codes:
10261 * 0 - Success
10262 * -ENXIO, -ENOMEM - Failure
10263 **/
10264int
10265lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10266 dma_addr_t pdma_phys_addr0,
10267 dma_addr_t pdma_phys_addr1,
10268 uint16_t xritag)
10269{
10270 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10271 LPFC_MBOXQ_t *mbox;
10272 int rc;
10273 uint32_t shdr_status, shdr_add_status;
10274 union lpfc_sli4_cfg_shdr *shdr;
10275
10276 if (xritag == NO_XRI) {
10277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10278 "0364 Invalid param:\n");
10279 return -EINVAL;
10280 }
10281
10282 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10283 if (!mbox)
10284 return -ENOMEM;
10285
10286 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10287 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10288 sizeof(struct lpfc_mbx_post_sgl_pages) -
10289 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10290
10291 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10292 &mbox->u.mqe.un.post_sgl_pages;
10293 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10294 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10295
10296 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10297 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10298 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10299 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10300
10301 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10302 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10303 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10304 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10305 if (!phba->sli4_hba.intr_enable)
10306 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10307 else
10308 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10309 /* The IOCTL status is embedded in the mailbox subheader. */
10310 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10311 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10312 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10313 if (rc != MBX_TIMEOUT)
10314 mempool_free(mbox, phba->mbox_mem_pool);
10315 if (shdr_status || shdr_add_status || rc) {
10316 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10317 "2511 POST_SGL mailbox failed with "
10318 "status x%x add_status x%x, mbx status x%x\n",
10319 shdr_status, shdr_add_status, rc);
10320 rc = -ENXIO;
10321 }
10322 return 0;
10323}
10324/**
10325 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10326 * @phba: The virtual port for which this call being executed.
10327 *
10328 * This routine will remove all of the sgl pages registered with the hba.
10329 *
10330 * Return codes:
10331 * 0 - Success
10332 * -ENXIO, -ENOMEM - Failure
10333 **/
10334int
10335lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10336{
10337 LPFC_MBOXQ_t *mbox;
10338 int rc;
10339 uint32_t shdr_status, shdr_add_status;
10340 union lpfc_sli4_cfg_shdr *shdr;
10341
10342 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10343 if (!mbox)
10344 return -ENOMEM;
10345
10346 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10347 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10348 LPFC_SLI4_MBX_EMBED);
10349 if (!phba->sli4_hba.intr_enable)
10350 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10351 else
10352 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10353 /* The IOCTL status is embedded in the mailbox subheader. */
10354 shdr = (union lpfc_sli4_cfg_shdr *)
10355 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10356 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10357 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10358 if (rc != MBX_TIMEOUT)
10359 mempool_free(mbox, phba->mbox_mem_pool);
10360 if (shdr_status || shdr_add_status || rc) {
10361 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10362 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10363 "status x%x add_status x%x, mbx status x%x\n",
10364 shdr_status, shdr_add_status, rc);
10365 rc = -ENXIO;
10366 }
10367 return rc;
10368}
10369
10370/**
10371 * lpfc_sli4_next_xritag - Get an xritag for the io
10372 * @phba: Pointer to HBA context object.
10373 *
10374 * This function gets an xritag for the iocb. If there is no unused xritag
10375 * it will return 0xffff.
10376 * The function returns the allocated xritag if successful, else returns zero.
10377 * Zero is not a valid xritag.
10378 * The caller is not required to hold any lock.
10379 **/
10380uint16_t
10381lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10382{
10383 uint16_t xritag;
10384
10385 spin_lock_irq(&phba->hbalock);
10386 xritag = phba->sli4_hba.next_xri;
10387 if ((xritag != (uint16_t) -1) && xritag <
10388 (phba->sli4_hba.max_cfg_param.max_xri
10389 + phba->sli4_hba.max_cfg_param.xri_base)) {
10390 phba->sli4_hba.next_xri++;
10391 phba->sli4_hba.max_cfg_param.xri_used++;
10392 spin_unlock_irq(&phba->hbalock);
10393 return xritag;
10394 }
10395 spin_unlock_irq(&phba->hbalock);
10396
10397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10398 "2004 Failed to allocate XRI.last XRITAG is %d"
10399 " Max XRI is %d, Used XRI is %d\n",
10400 phba->sli4_hba.next_xri,
10401 phba->sli4_hba.max_cfg_param.max_xri,
10402 phba->sli4_hba.max_cfg_param.xri_used);
10403 return -1;
10404}
10405
10406/**
10407 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10408 * @phba: pointer to lpfc hba data structure.
10409 *
10410 * This routine is invoked to post a block of driver's sgl pages to the
10411 * HBA using non-embedded mailbox command. No Lock is held. This routine
10412 * is only called when the driver is loading and after all IO has been
10413 * stopped.
10414 **/
10415int
10416lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10417{
10418 struct lpfc_sglq *sglq_entry;
10419 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10420 struct sgl_page_pairs *sgl_pg_pairs;
10421 void *viraddr;
10422 LPFC_MBOXQ_t *mbox;
10423 uint32_t reqlen, alloclen, pg_pairs;
10424 uint32_t mbox_tmo;
10425 uint16_t xritag_start = 0;
10426 int els_xri_cnt, rc = 0;
10427 uint32_t shdr_status, shdr_add_status;
10428 union lpfc_sli4_cfg_shdr *shdr;
10429
10430 /* The number of sgls to be posted */
10431 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10432
10433 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10434 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10435 if (reqlen > PAGE_SIZE) {
10436 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10437 "2559 Block sgl registration required DMA "
10438 "size (%d) great than a page\n", reqlen);
10439 return -ENOMEM;
10440 }
10441 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10442 if (!mbox) {
10443 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10444 "2560 Failed to allocate mbox cmd memory\n");
10445 return -ENOMEM;
10446 }
10447
10448 /* Allocate DMA memory and set up the non-embedded mailbox command */
10449 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10450 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10451 LPFC_SLI4_MBX_NEMBED);
10452
10453 if (alloclen < reqlen) {
10454 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10455 "0285 Allocated DMA memory size (%d) is "
10456 "less than the requested DMA memory "
10457 "size (%d)\n", alloclen, reqlen);
10458 lpfc_sli4_mbox_cmd_free(phba, mbox);
10459 return -ENOMEM;
10460 }
10461
10462 /* Get the first SGE entry from the non-embedded DMA memory */
10463 if (unlikely(!mbox->sge_array)) {
10464 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10465 "2525 Failed to get the non-embedded SGE "
10466 "virtual address\n");
10467 lpfc_sli4_mbox_cmd_free(phba, mbox);
10468 return -ENOMEM;
10469 }
10470 viraddr = mbox->sge_array->addr[0];
10471
10472 /* Set up the SGL pages in the non-embedded DMA pages */
10473 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10474 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10475
10476 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10477 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10478 /* Set up the sge entry */
10479 sgl_pg_pairs->sgl_pg0_addr_lo =
10480 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10481 sgl_pg_pairs->sgl_pg0_addr_hi =
10482 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10483 sgl_pg_pairs->sgl_pg1_addr_lo =
10484 cpu_to_le32(putPaddrLow(0));
10485 sgl_pg_pairs->sgl_pg1_addr_hi =
10486 cpu_to_le32(putPaddrHigh(0));
10487 /* Keep the first xritag on the list */
10488 if (pg_pairs == 0)
10489 xritag_start = sglq_entry->sli4_xritag;
10490 sgl_pg_pairs++;
10491 }
10492 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10493 pg_pairs = (pg_pairs > 0) ? (pg_pairs - 1) : pg_pairs;
10494 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10495 /* Perform endian conversion if necessary */
10496 sgl->word0 = cpu_to_le32(sgl->word0);
10497
10498 if (!phba->sli4_hba.intr_enable)
10499 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10500 else {
10501 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10502 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10503 }
10504 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10505 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10506 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10507 if (rc != MBX_TIMEOUT)
10508 lpfc_sli4_mbox_cmd_free(phba, mbox);
10509 if (shdr_status || shdr_add_status || rc) {
10510 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10511 "2513 POST_SGL_BLOCK mailbox command failed "
10512 "status x%x add_status x%x mbx status x%x\n",
10513 shdr_status, shdr_add_status, rc);
10514 rc = -ENXIO;
10515 }
10516 return rc;
10517}
10518
10519/**
10520 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10521 * @phba: pointer to lpfc hba data structure.
10522 * @sblist: pointer to scsi buffer list.
10523 * @count: number of scsi buffers on the list.
10524 *
10525 * This routine is invoked to post a block of @count scsi sgl pages from a
10526 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10527 * No Lock is held.
10528 *
10529 **/
10530int
10531lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10532 int cnt)
10533{
10534 struct lpfc_scsi_buf *psb;
10535 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10536 struct sgl_page_pairs *sgl_pg_pairs;
10537 void *viraddr;
10538 LPFC_MBOXQ_t *mbox;
10539 uint32_t reqlen, alloclen, pg_pairs;
10540 uint32_t mbox_tmo;
10541 uint16_t xritag_start = 0;
10542 int rc = 0;
10543 uint32_t shdr_status, shdr_add_status;
10544 dma_addr_t pdma_phys_bpl1;
10545 union lpfc_sli4_cfg_shdr *shdr;
10546
10547 /* Calculate the requested length of the dma memory */
10548 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10549 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10550 if (reqlen > PAGE_SIZE) {
10551 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10552 "0217 Block sgl registration required DMA "
10553 "size (%d) great than a page\n", reqlen);
10554 return -ENOMEM;
10555 }
10556 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10557 if (!mbox) {
10558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10559 "0283 Failed to allocate mbox cmd memory\n");
10560 return -ENOMEM;
10561 }
10562
10563 /* Allocate DMA memory and set up the non-embedded mailbox command */
10564 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10565 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10566 LPFC_SLI4_MBX_NEMBED);
10567
10568 if (alloclen < reqlen) {
10569 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10570 "2561 Allocated DMA memory size (%d) is "
10571 "less than the requested DMA memory "
10572 "size (%d)\n", alloclen, reqlen);
10573 lpfc_sli4_mbox_cmd_free(phba, mbox);
10574 return -ENOMEM;
10575 }
10576
10577 /* Get the first SGE entry from the non-embedded DMA memory */
10578 if (unlikely(!mbox->sge_array)) {
10579 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10580 "2565 Failed to get the non-embedded SGE "
10581 "virtual address\n");
10582 lpfc_sli4_mbox_cmd_free(phba, mbox);
10583 return -ENOMEM;
10584 }
10585 viraddr = mbox->sge_array->addr[0];
10586
10587 /* Set up the SGL pages in the non-embedded DMA pages */
10588 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10589 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10590
10591 pg_pairs = 0;
10592 list_for_each_entry(psb, sblist, list) {
10593 /* Set up the sge entry */
10594 sgl_pg_pairs->sgl_pg0_addr_lo =
10595 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10596 sgl_pg_pairs->sgl_pg0_addr_hi =
10597 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10598 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10599 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10600 else
10601 pdma_phys_bpl1 = 0;
10602 sgl_pg_pairs->sgl_pg1_addr_lo =
10603 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10604 sgl_pg_pairs->sgl_pg1_addr_hi =
10605 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10606 /* Keep the first xritag on the list */
10607 if (pg_pairs == 0)
10608 xritag_start = psb->cur_iocbq.sli4_xritag;
10609 sgl_pg_pairs++;
10610 pg_pairs++;
10611 }
10612 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10613 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10614 /* Perform endian conversion if necessary */
10615 sgl->word0 = cpu_to_le32(sgl->word0);
10616
10617 if (!phba->sli4_hba.intr_enable)
10618 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10619 else {
10620 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10621 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10622 }
10623 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10624 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10625 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10626 if (rc != MBX_TIMEOUT)
10627 lpfc_sli4_mbox_cmd_free(phba, mbox);
10628 if (shdr_status || shdr_add_status || rc) {
10629 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10630 "2564 POST_SGL_BLOCK mailbox command failed "
10631 "status x%x add_status x%x mbx status x%x\n",
10632 shdr_status, shdr_add_status, rc);
10633 rc = -ENXIO;
10634 }
10635 return rc;
10636}
10637
10638/**
10639 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10640 * @phba: pointer to lpfc_hba struct that the frame was received on
10641 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10642 *
10643 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10644 * valid type of frame that the LPFC driver will handle. This function will
10645 * return a zero if the frame is a valid frame or a non zero value when the
10646 * frame does not pass the check.
10647 **/
10648static int
10649lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10650{
10651 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10652 char *type_names[] = FC_TYPE_NAMES_INIT;
10653 struct fc_vft_header *fc_vft_hdr;
10654
10655 switch (fc_hdr->fh_r_ctl) {
10656 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10657 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10658 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10659 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10660 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10661 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10662 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10663 case FC_RCTL_DD_CMD_STATUS: /* command status */
10664 case FC_RCTL_ELS_REQ: /* extended link services request */
10665 case FC_RCTL_ELS_REP: /* extended link services reply */
10666 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10667 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10668 case FC_RCTL_BA_NOP: /* basic link service NOP */
10669 case FC_RCTL_BA_ABTS: /* basic link service abort */
10670 case FC_RCTL_BA_RMC: /* remove connection */
10671 case FC_RCTL_BA_ACC: /* basic accept */
10672 case FC_RCTL_BA_RJT: /* basic reject */
10673 case FC_RCTL_BA_PRMT:
10674 case FC_RCTL_ACK_1: /* acknowledge_1 */
10675 case FC_RCTL_ACK_0: /* acknowledge_0 */
10676 case FC_RCTL_P_RJT: /* port reject */
10677 case FC_RCTL_F_RJT: /* fabric reject */
10678 case FC_RCTL_P_BSY: /* port busy */
10679 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10680 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10681 case FC_RCTL_LCR: /* link credit reset */
10682 case FC_RCTL_END: /* end */
10683 break;
10684 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10685 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10686 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10687 return lpfc_fc_frame_check(phba, fc_hdr);
10688 default:
10689 goto drop;
10690 }
10691 switch (fc_hdr->fh_type) {
10692 case FC_TYPE_BLS:
10693 case FC_TYPE_ELS:
10694 case FC_TYPE_FCP:
10695 case FC_TYPE_CT:
10696 break;
10697 case FC_TYPE_IP:
10698 case FC_TYPE_ILS:
10699 default:
10700 goto drop;
10701 }
10702 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10703 "2538 Received frame rctl:%s type:%s\n",
10704 rctl_names[fc_hdr->fh_r_ctl],
10705 type_names[fc_hdr->fh_type]);
10706 return 0;
10707drop:
10708 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10709 "2539 Dropped frame rctl:%s type:%s\n",
10710 rctl_names[fc_hdr->fh_r_ctl],
10711 type_names[fc_hdr->fh_type]);
10712 return 1;
10713}
10714
10715/**
10716 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10717 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10718 *
10719 * This function processes the FC header to retrieve the VFI from the VF
10720 * header, if one exists. This function will return the VFI if one exists
10721 * or 0 if no VSAN Header exists.
10722 **/
10723static uint32_t
10724lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10725{
10726 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10727
10728 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10729 return 0;
10730 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10731}
10732
10733/**
10734 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10735 * @phba: Pointer to the HBA structure to search for the vport on
10736 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10737 * @fcfi: The FC Fabric ID that the frame came from
10738 *
10739 * This function searches the @phba for a vport that matches the content of the
10740 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10741 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10742 * returns the matching vport pointer or NULL if unable to match frame to a
10743 * vport.
10744 **/
10745static struct lpfc_vport *
10746lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10747 uint16_t fcfi)
10748{
10749 struct lpfc_vport **vports;
10750 struct lpfc_vport *vport = NULL;
10751 int i;
10752 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10753 fc_hdr->fh_d_id[1] << 8 |
10754 fc_hdr->fh_d_id[2]);
10755
10756 vports = lpfc_create_vport_work_array(phba);
10757 if (vports != NULL)
10758 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10759 if (phba->fcf.fcfi == fcfi &&
10760 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10761 vports[i]->fc_myDID == did) {
10762 vport = vports[i];
10763 break;
10764 }
10765 }
10766 lpfc_destroy_vport_work_array(phba, vports);
10767 return vport;
10768}
10769
10770/**
10771 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10772 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10773 *
10774 * This function searches through the existing incomplete sequences that have
10775 * been sent to this @vport. If the frame matches one of the incomplete
10776 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10777 * make up that sequence. If no sequence is found that matches this frame then
10778 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10779 * This function returns a pointer to the first dmabuf in the sequence list that
10780 * the frame was linked to.
10781 **/
10782static struct hbq_dmabuf *
10783lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10784{
10785 struct fc_frame_header *new_hdr;
10786 struct fc_frame_header *temp_hdr;
10787 struct lpfc_dmabuf *d_buf;
10788 struct lpfc_dmabuf *h_buf;
10789 struct hbq_dmabuf *seq_dmabuf = NULL;
10790 struct hbq_dmabuf *temp_dmabuf = NULL;
10791
10792 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10793 /* Use the hdr_buf to find the sequence that this frame belongs to */
10794 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10795 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10796 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10797 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10798 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10799 continue;
10800 /* found a pending sequence that matches this frame */
10801 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10802 break;
10803 }
10804 if (!seq_dmabuf) {
10805 /*
10806 * This indicates first frame received for this sequence.
10807 * Queue the buffer on the vport's rcv_buffer_list.
10808 */
10809 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10810 return dmabuf;
10811 }
10812 temp_hdr = seq_dmabuf->hbuf.virt;
10813 if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
10814 list_add(&seq_dmabuf->dbuf.list, &dmabuf->dbuf.list);
10815 return dmabuf;
10816 }
10817 /* find the correct place in the sequence to insert this frame */
10818 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10819 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10820 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10821 /*
10822 * If the frame's sequence count is greater than the frame on
10823 * the list then insert the frame right after this frame
10824 */
10825 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10826 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10827 return seq_dmabuf;
10828 }
10829 }
10830 return NULL;
10831}
10832
10833/**
10834 * lpfc_seq_complete - Indicates if a sequence is complete
10835 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10836 *
10837 * This function checks the sequence, starting with the frame described by
10838 * @dmabuf, to see if all the frames associated with this sequence are present.
10839 * the frames associated with this sequence are linked to the @dmabuf using the
10840 * dbuf list. This function looks for two major things. 1) That the first frame
10841 * has a sequence count of zero. 2) There is a frame with last frame of sequence
10842 * set. 3) That there are no holes in the sequence count. The function will
10843 * return 1 when the sequence is complete, otherwise it will return 0.
10844 **/
10845static int
10846lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
10847{
10848 struct fc_frame_header *hdr;
10849 struct lpfc_dmabuf *d_buf;
10850 struct hbq_dmabuf *seq_dmabuf;
10851 uint32_t fctl;
10852 int seq_count = 0;
10853
10854 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10855 /* make sure first fame of sequence has a sequence count of zero */
10856 if (hdr->fh_seq_cnt != seq_count)
10857 return 0;
10858 fctl = (hdr->fh_f_ctl[0] << 16 |
10859 hdr->fh_f_ctl[1] << 8 |
10860 hdr->fh_f_ctl[2]);
10861 /* If last frame of sequence we can return success. */
10862 if (fctl & FC_FC_END_SEQ)
10863 return 1;
10864 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
10865 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10866 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10867 /* If there is a hole in the sequence count then fail. */
10868 if (++seq_count != hdr->fh_seq_cnt)
10869 return 0;
10870 fctl = (hdr->fh_f_ctl[0] << 16 |
10871 hdr->fh_f_ctl[1] << 8 |
10872 hdr->fh_f_ctl[2]);
10873 /* If last frame of sequence we can return success. */
10874 if (fctl & FC_FC_END_SEQ)
10875 return 1;
10876 }
10877 return 0;
10878}
10879
10880/**
10881 * lpfc_prep_seq - Prep sequence for ULP processing
10882 * @vport: Pointer to the vport on which this sequence was received
10883 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10884 *
10885 * This function takes a sequence, described by a list of frames, and creates
10886 * a list of iocbq structures to describe the sequence. This iocbq list will be
10887 * used to issue to the generic unsolicited sequence handler. This routine
10888 * returns a pointer to the first iocbq in the list. If the function is unable
10889 * to allocate an iocbq then it throw out the received frames that were not
10890 * able to be described and return a pointer to the first iocbq. If unable to
10891 * allocate any iocbqs (including the first) this function will return NULL.
10892 **/
10893static struct lpfc_iocbq *
10894lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
10895{
10896 struct lpfc_dmabuf *d_buf, *n_buf;
10897 struct lpfc_iocbq *first_iocbq, *iocbq;
10898 struct fc_frame_header *fc_hdr;
10899 uint32_t sid;
10900
10901 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10902 /* remove from receive buffer list */
10903 list_del_init(&seq_dmabuf->hbuf.list);
10904 /* get the Remote Port's SID */
10905 sid = (fc_hdr->fh_s_id[0] << 16 |
10906 fc_hdr->fh_s_id[1] << 8 |
10907 fc_hdr->fh_s_id[2]);
10908 /* Get an iocbq struct to fill in. */
10909 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
10910 if (first_iocbq) {
10911 /* Initialize the first IOCB. */
10912 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
10913 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
10914 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
10915 first_iocbq->iocb.unsli3.rcvsli3.vpi =
10916 vport->vpi + vport->phba->vpi_base;
10917 /* put the first buffer into the first IOCBq */
10918 first_iocbq->context2 = &seq_dmabuf->dbuf;
10919 first_iocbq->context3 = NULL;
10920 first_iocbq->iocb.ulpBdeCount = 1;
10921 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
10922 LPFC_DATA_BUF_SIZE;
10923 first_iocbq->iocb.un.rcvels.remoteID = sid;
10924 }
10925 iocbq = first_iocbq;
10926 /*
10927 * Each IOCBq can have two Buffers assigned, so go through the list
10928 * of buffers for this sequence and save two buffers in each IOCBq
10929 */
10930 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
10931 if (!iocbq) {
10932 lpfc_in_buf_free(vport->phba, d_buf);
10933 continue;
10934 }
10935 if (!iocbq->context3) {
10936 iocbq->context3 = d_buf;
10937 iocbq->iocb.ulpBdeCount++;
10938 iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
10939 LPFC_DATA_BUF_SIZE;
10940 } else {
10941 iocbq = lpfc_sli_get_iocbq(vport->phba);
10942 if (!iocbq) {
10943 if (first_iocbq) {
10944 first_iocbq->iocb.ulpStatus =
10945 IOSTAT_FCP_RSP_ERROR;
10946 first_iocbq->iocb.un.ulpWord[4] =
10947 IOERR_NO_RESOURCES;
10948 }
10949 lpfc_in_buf_free(vport->phba, d_buf);
10950 continue;
10951 }
10952 iocbq->context2 = d_buf;
10953 iocbq->context3 = NULL;
10954 iocbq->iocb.ulpBdeCount = 1;
10955 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
10956 LPFC_DATA_BUF_SIZE;
10957 iocbq->iocb.un.rcvels.remoteID = sid;
10958 list_add_tail(&iocbq->list, &first_iocbq->list);
10959 }
10960 }
10961 return first_iocbq;
10962}
10963
10964/**
10965 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
10966 * @phba: Pointer to HBA context object.
10967 *
10968 * This function is called with no lock held. This function processes all
10969 * the received buffers and gives it to upper layers when a received buffer
10970 * indicates that it is the final frame in the sequence. The interrupt
10971 * service routine processes received buffers at interrupt contexts and adds
10972 * received dma buffers to the rb_pend_list queue and signals the worker thread.
10973 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
10974 * appropriate receive function when the final frame in a sequence is received.
10975 **/
10976int
10977lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba)
10978{
10979 LIST_HEAD(cmplq);
10980 struct hbq_dmabuf *dmabuf, *seq_dmabuf;
10981 struct fc_frame_header *fc_hdr;
10982 struct lpfc_vport *vport;
10983 uint32_t fcfi;
10984 struct lpfc_iocbq *iocbq;
10985
10986 /* Clear hba flag and get all received buffers into the cmplq */
10987 spin_lock_irq(&phba->hbalock);
10988 phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
10989 list_splice_init(&phba->rb_pend_list, &cmplq);
10990 spin_unlock_irq(&phba->hbalock);
10991
10992 /* Process each received buffer */
10993 while ((dmabuf = lpfc_sli_hbqbuf_get(&cmplq)) != NULL) {
10994 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10995 /* check to see if this a valid type of frame */
10996 if (lpfc_fc_frame_check(phba, fc_hdr)) {
10997 lpfc_in_buf_free(phba, &dmabuf->dbuf);
10998 continue;
10999 }
11000 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->rcqe);
11001 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11002 if (!vport) {
11003 /* throw out the frame */
11004 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11005 continue;
11006 }
11007 /* Link this frame */
11008 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11009 if (!seq_dmabuf) {
11010 /* unable to add frame to vport - throw it out */
11011 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11012 continue;
11013 }
11014 /* If not last frame in sequence continue processing frames. */
11015 if (!lpfc_seq_complete(seq_dmabuf)) {
11016 /*
11017 * When saving off frames post a new one and mark this
11018 * frame to be freed when it is finished.
11019 **/
11020 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11021 dmabuf->tag = -1;
11022 continue;
11023 }
11024 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11025 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11026 if (!lpfc_complete_unsol_iocb(phba,
11027 &phba->sli.ring[LPFC_ELS_RING],
11028 iocbq, fc_hdr->fh_r_ctl,
11029 fc_hdr->fh_type))
11030 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11031 "2540 Ring %d handler: unexpected Rctl "
11032 "x%x Type x%x received\n",
11033 LPFC_ELS_RING,
11034 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11035 };
11036 return 0;
11037}
James Smart6fb120a2009-05-22 14:52:59 -040011038
11039/**
11040 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11041 * @phba: pointer to lpfc hba data structure.
11042 *
11043 * This routine is invoked to post rpi header templates to the
11044 * HBA consistent with the SLI-4 interface spec. This routine
11045 * posts a PAGE_SIZE memory region to the port to hold up to
11046 * PAGE_SIZE modulo 64 rpi context headers.
11047 *
11048 * This routine does not require any locks. It's usage is expected
11049 * to be driver load or reset recovery when the driver is
11050 * sequential.
11051 *
11052 * Return codes
11053 * 0 - sucessful
11054 * EIO - The mailbox failed to complete successfully.
11055 * When this error occurs, the driver is not guaranteed
11056 * to have any rpi regions posted to the device and
11057 * must either attempt to repost the regions or take a
11058 * fatal error.
11059 **/
11060int
11061lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11062{
11063 struct lpfc_rpi_hdr *rpi_page;
11064 uint32_t rc = 0;
11065
11066 /* Post all rpi memory regions to the port. */
11067 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11068 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11069 if (rc != MBX_SUCCESS) {
11070 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11071 "2008 Error %d posting all rpi "
11072 "headers\n", rc);
11073 rc = -EIO;
11074 break;
11075 }
11076 }
11077
11078 return rc;
11079}
11080
11081/**
11082 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11083 * @phba: pointer to lpfc hba data structure.
11084 * @rpi_page: pointer to the rpi memory region.
11085 *
11086 * This routine is invoked to post a single rpi header to the
11087 * HBA consistent with the SLI-4 interface spec. This memory region
11088 * maps up to 64 rpi context regions.
11089 *
11090 * Return codes
11091 * 0 - sucessful
11092 * ENOMEM - No available memory
11093 * EIO - The mailbox failed to complete successfully.
11094 **/
11095int
11096lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11097{
11098 LPFC_MBOXQ_t *mboxq;
11099 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11100 uint32_t rc = 0;
11101 uint32_t mbox_tmo;
11102 uint32_t shdr_status, shdr_add_status;
11103 union lpfc_sli4_cfg_shdr *shdr;
11104
11105 /* The port is notified of the header region via a mailbox command. */
11106 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11107 if (!mboxq) {
11108 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11109 "2001 Unable to allocate memory for issuing "
11110 "SLI_CONFIG_SPECIAL mailbox command\n");
11111 return -ENOMEM;
11112 }
11113
11114 /* Post all rpi memory regions to the port. */
11115 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11116 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11117 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11118 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11119 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11120 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11121 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11122 hdr_tmpl, rpi_page->page_count);
11123 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11124 rpi_page->start_rpi);
11125 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11126 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011127 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011128 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11129 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11130 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11131 if (rc != MBX_TIMEOUT)
11132 mempool_free(mboxq, phba->mbox_mem_pool);
11133 if (shdr_status || shdr_add_status || rc) {
11134 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11135 "2514 POST_RPI_HDR mailbox failed with "
11136 "status x%x add_status x%x, mbx status x%x\n",
11137 shdr_status, shdr_add_status, rc);
11138 rc = -ENXIO;
11139 }
11140 return rc;
11141}
11142
11143/**
11144 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11145 * @phba: pointer to lpfc hba data structure.
11146 *
11147 * This routine is invoked to post rpi header templates to the
11148 * HBA consistent with the SLI-4 interface spec. This routine
11149 * posts a PAGE_SIZE memory region to the port to hold up to
11150 * PAGE_SIZE modulo 64 rpi context headers.
11151 *
11152 * Returns
11153 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
11154 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11155 **/
11156int
11157lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11158{
11159 int rpi;
11160 uint16_t max_rpi, rpi_base, rpi_limit;
11161 uint16_t rpi_remaining;
11162 struct lpfc_rpi_hdr *rpi_hdr;
11163
11164 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11165 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11166 rpi_limit = phba->sli4_hba.next_rpi;
11167
11168 /*
11169 * The valid rpi range is not guaranteed to be zero-based. Start
11170 * the search at the rpi_base as reported by the port.
11171 */
11172 spin_lock_irq(&phba->hbalock);
11173 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11174 if (rpi >= rpi_limit || rpi < rpi_base)
11175 rpi = LPFC_RPI_ALLOC_ERROR;
11176 else {
11177 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11178 phba->sli4_hba.max_cfg_param.rpi_used++;
11179 phba->sli4_hba.rpi_count++;
11180 }
11181
11182 /*
11183 * Don't try to allocate more rpi header regions if the device limit
11184 * on available rpis max has been exhausted.
11185 */
11186 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11187 (phba->sli4_hba.rpi_count >= max_rpi)) {
11188 spin_unlock_irq(&phba->hbalock);
11189 return rpi;
11190 }
11191
11192 /*
11193 * If the driver is running low on rpi resources, allocate another
11194 * page now. Note that the next_rpi value is used because
11195 * it represents how many are actually in use whereas max_rpi notes
11196 * how many are supported max by the device.
11197 */
11198 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11199 phba->sli4_hba.rpi_count;
11200 spin_unlock_irq(&phba->hbalock);
11201 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11202 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11203 if (!rpi_hdr) {
11204 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11205 "2002 Error Could not grow rpi "
11206 "count\n");
11207 } else {
11208 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11209 }
11210 }
11211
11212 return rpi;
11213}
11214
11215/**
11216 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11217 * @phba: pointer to lpfc hba data structure.
11218 *
11219 * This routine is invoked to release an rpi to the pool of
11220 * available rpis maintained by the driver.
11221 **/
11222void
11223lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11224{
11225 spin_lock_irq(&phba->hbalock);
11226 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11227 phba->sli4_hba.rpi_count--;
11228 phba->sli4_hba.max_cfg_param.rpi_used--;
11229 spin_unlock_irq(&phba->hbalock);
11230}
11231
11232/**
11233 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11234 * @phba: pointer to lpfc hba data structure.
11235 *
11236 * This routine is invoked to remove the memory region that
11237 * provided rpi via a bitmask.
11238 **/
11239void
11240lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11241{
11242 kfree(phba->sli4_hba.rpi_bmask);
11243}
11244
11245/**
11246 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11247 * @phba: pointer to lpfc hba data structure.
11248 *
11249 * This routine is invoked to remove the memory region that
11250 * provided rpi via a bitmask.
11251 **/
11252int
11253lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11254{
11255 LPFC_MBOXQ_t *mboxq;
11256 struct lpfc_hba *phba = ndlp->phba;
11257 int rc;
11258
11259 /* The port is notified of the header region via a mailbox command. */
11260 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11261 if (!mboxq)
11262 return -ENOMEM;
11263
11264 /* Post all rpi memory regions to the port. */
11265 lpfc_resume_rpi(mboxq, ndlp);
11266 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11267 if (rc == MBX_NOT_FINISHED) {
11268 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11269 "2010 Resume RPI Mailbox failed "
11270 "status %d, mbxStatus x%x\n", rc,
11271 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11272 mempool_free(mboxq, phba->mbox_mem_pool);
11273 return -EIO;
11274 }
11275 return 0;
11276}
11277
11278/**
11279 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11280 * @phba: pointer to lpfc hba data structure.
11281 * @vpi: vpi value to activate with the port.
11282 *
11283 * This routine is invoked to activate a vpi with the
11284 * port when the host intends to use vports with a
11285 * nonzero vpi.
11286 *
11287 * Returns:
11288 * 0 success
11289 * -Evalue otherwise
11290 **/
11291int
11292lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11293{
11294 LPFC_MBOXQ_t *mboxq;
11295 int rc = 0;
11296 uint32_t mbox_tmo;
11297
11298 if (vpi == 0)
11299 return -EINVAL;
11300 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11301 if (!mboxq)
11302 return -ENOMEM;
11303 lpfc_init_vpi(mboxq, vpi);
11304 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11305 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11306 if (rc != MBX_TIMEOUT)
11307 mempool_free(mboxq, phba->mbox_mem_pool);
11308 if (rc != MBX_SUCCESS) {
11309 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11310 "2022 INIT VPI Mailbox failed "
11311 "status %d, mbxStatus x%x\n", rc,
11312 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11313 rc = -EIO;
11314 }
11315 return rc;
11316}
11317
11318/**
11319 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11320 * @phba: pointer to lpfc hba data structure.
11321 * @mboxq: Pointer to mailbox object.
11322 *
11323 * This routine is invoked to manually add a single FCF record. The caller
11324 * must pass a completely initialized FCF_Record. This routine takes
11325 * care of the nonembedded mailbox operations.
11326 **/
11327static void
11328lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11329{
11330 void *virt_addr;
11331 union lpfc_sli4_cfg_shdr *shdr;
11332 uint32_t shdr_status, shdr_add_status;
11333
11334 virt_addr = mboxq->sge_array->addr[0];
11335 /* The IOCTL status is embedded in the mailbox subheader. */
11336 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11337 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11338 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11339
11340 if ((shdr_status || shdr_add_status) &&
11341 (shdr_status != STATUS_FCF_IN_USE))
11342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11343 "2558 ADD_FCF_RECORD mailbox failed with "
11344 "status x%x add_status x%x\n",
11345 shdr_status, shdr_add_status);
11346
11347 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11348}
11349
11350/**
11351 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11352 * @phba: pointer to lpfc hba data structure.
11353 * @fcf_record: pointer to the initialized fcf record to add.
11354 *
11355 * This routine is invoked to manually add a single FCF record. The caller
11356 * must pass a completely initialized FCF_Record. This routine takes
11357 * care of the nonembedded mailbox operations.
11358 **/
11359int
11360lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11361{
11362 int rc = 0;
11363 LPFC_MBOXQ_t *mboxq;
11364 uint8_t *bytep;
11365 void *virt_addr;
11366 dma_addr_t phys_addr;
11367 struct lpfc_mbx_sge sge;
11368 uint32_t alloc_len, req_len;
11369 uint32_t fcfindex;
11370
11371 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11372 if (!mboxq) {
11373 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11374 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11375 return -ENOMEM;
11376 }
11377
11378 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11379 sizeof(uint32_t);
11380
11381 /* Allocate DMA memory and set up the non-embedded mailbox command */
11382 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11383 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11384 req_len, LPFC_SLI4_MBX_NEMBED);
11385 if (alloc_len < req_len) {
11386 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11387 "2523 Allocated DMA memory size (x%x) is "
11388 "less than the requested DMA memory "
11389 "size (x%x)\n", alloc_len, req_len);
11390 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11391 return -ENOMEM;
11392 }
11393
11394 /*
11395 * Get the first SGE entry from the non-embedded DMA memory. This
11396 * routine only uses a single SGE.
11397 */
11398 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11399 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11400 if (unlikely(!mboxq->sge_array)) {
11401 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11402 "2526 Failed to get the non-embedded SGE "
11403 "virtual address\n");
11404 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11405 return -ENOMEM;
11406 }
11407 virt_addr = mboxq->sge_array->addr[0];
11408 /*
11409 * Configure the FCF record for FCFI 0. This is the driver's
11410 * hardcoded default and gets used in nonFIP mode.
11411 */
11412 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11413 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11414 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11415
11416 /*
11417 * Copy the fcf_index and the FCF Record Data. The data starts after
11418 * the FCoE header plus word10. The data copy needs to be endian
11419 * correct.
11420 */
11421 bytep += sizeof(uint32_t);
11422 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11423 mboxq->vport = phba->pport;
11424 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11425 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11426 if (rc == MBX_NOT_FINISHED) {
11427 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11428 "2515 ADD_FCF_RECORD mailbox failed with "
11429 "status 0x%x\n", rc);
11430 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11431 rc = -EIO;
11432 } else
11433 rc = 0;
11434
11435 return rc;
11436}
11437
11438/**
11439 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11440 * @phba: pointer to lpfc hba data structure.
11441 * @fcf_record: pointer to the fcf record to write the default data.
11442 * @fcf_index: FCF table entry index.
11443 *
11444 * This routine is invoked to build the driver's default FCF record. The
11445 * values used are hardcoded. This routine handles memory initialization.
11446 *
11447 **/
11448void
11449lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11450 struct fcf_record *fcf_record,
11451 uint16_t fcf_index)
11452{
11453 memset(fcf_record, 0, sizeof(struct fcf_record));
11454 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11455 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11456 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11457 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11458 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11459 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11460 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11461 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11462 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11463 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11464 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11465 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11466 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011467 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011468 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11469 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11470 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11471 /* Set the VLAN bit map */
11472 if (phba->valid_vlan) {
11473 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11474 = 1 << (phba->vlan_id % 8);
11475 }
11476}
11477
11478/**
11479 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11480 * @phba: pointer to lpfc hba data structure.
11481 * @fcf_index: FCF table entry offset.
11482 *
11483 * This routine is invoked to read up to @fcf_num of FCF record from the
11484 * device starting with the given @fcf_index.
11485 **/
11486int
11487lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11488{
11489 int rc = 0, error;
11490 LPFC_MBOXQ_t *mboxq;
11491 void *virt_addr;
11492 dma_addr_t phys_addr;
11493 uint8_t *bytep;
11494 struct lpfc_mbx_sge sge;
11495 uint32_t alloc_len, req_len;
11496 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11497
11498 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11499 if (!mboxq) {
11500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11501 "2000 Failed to allocate mbox for "
11502 "READ_FCF cmd\n");
11503 return -ENOMEM;
11504 }
11505
11506 req_len = sizeof(struct fcf_record) +
11507 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11508
11509 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11510 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11511 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11512 LPFC_SLI4_MBX_NEMBED);
11513
11514 if (alloc_len < req_len) {
11515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11516 "0291 Allocated DMA memory size (x%x) is "
11517 "less than the requested DMA memory "
11518 "size (x%x)\n", alloc_len, req_len);
11519 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11520 return -ENOMEM;
11521 }
11522
11523 /* Get the first SGE entry from the non-embedded DMA memory. This
11524 * routine only uses a single SGE.
11525 */
11526 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11527 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11528 if (unlikely(!mboxq->sge_array)) {
11529 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11530 "2527 Failed to get the non-embedded SGE "
11531 "virtual address\n");
11532 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11533 return -ENOMEM;
11534 }
11535 virt_addr = mboxq->sge_array->addr[0];
11536 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11537
11538 /* Set up command fields */
11539 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11540 /* Perform necessary endian conversion */
11541 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11542 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11543 mboxq->vport = phba->pport;
11544 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11545 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11546 if (rc == MBX_NOT_FINISHED) {
11547 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11548 error = -EIO;
11549 } else
11550 error = 0;
11551 return error;
11552}