blob: 8d884d8e18be3149ff9f11157a4ae415fe987580 [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;
James Smart4d9ab992009-10-02 15:16:39 -04003021 struct hbq_dmabuf *dmabuf;
3022 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003023 unsigned long iflag;
3024
3025 while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
3026 /* Get the response iocb from the head of work queue */
3027 spin_lock_irqsave(&phba->hbalock, iflag);
3028 list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
James Smart4d9ab992009-10-02 15:16:39 -04003029 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003030 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003031
3032 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3033 case CQE_CODE_COMPL_WQE:
3034 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3035 cq_event);
3036 lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
3037 break;
3038 case CQE_CODE_RECEIVE:
3039 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3040 cq_event);
3041 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3042 break;
3043 default:
3044 break;
3045 }
James Smart4f774512009-05-22 14:52:35 -04003046 }
3047}
3048
3049/**
James Smart3621a712009-04-06 18:47:14 -04003050 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003051 * @phba: Pointer to HBA context object.
3052 * @pring: Pointer to driver SLI ring object.
3053 *
3054 * This function aborts all iocbs in the given ring and frees all the iocb
3055 * objects in txq. This function issues an abort iocb for all the iocb commands
3056 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3057 * the return of this function. The caller is not required to hold any locks.
3058 **/
James Smart2e0fef82007-06-17 19:56:36 -05003059void
dea31012005-04-17 16:05:31 -05003060lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3061{
James Smart2534ba72007-04-25 09:52:20 -04003062 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003063 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003064
James Smart92d7f7b2007-06-17 19:56:38 -05003065 if (pring->ringno == LPFC_ELS_RING) {
3066 lpfc_fabric_abort_hba(phba);
3067 }
3068
dea31012005-04-17 16:05:31 -05003069 /* Error everything on txq and txcmplq
3070 * First do the txq.
3071 */
James Smart2e0fef82007-06-17 19:56:36 -05003072 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003073 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003074 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003075
3076 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003077 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3078 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3079
James Smart2e0fef82007-06-17 19:56:36 -05003080 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003081
James Smarta257bf92009-04-06 18:48:10 -04003082 /* Cancel all the IOCBs from the completions list */
3083 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3084 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003085}
3086
James Smarte59058c2008-08-24 21:49:00 -04003087/**
James Smart3621a712009-04-06 18:47:14 -04003088 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003089 * @phba: Pointer to HBA context object.
3090 *
3091 * This function flushes all iocbs in the fcp ring and frees all the iocb
3092 * objects in txq and txcmplq. This function will not issue abort iocbs
3093 * for all the iocb commands in txcmplq, they will just be returned with
3094 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3095 * slot has been permanently disabled.
3096 **/
3097void
3098lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3099{
3100 LIST_HEAD(txq);
3101 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003102 struct lpfc_sli *psli = &phba->sli;
3103 struct lpfc_sli_ring *pring;
3104
3105 /* Currently, only one fcp ring */
3106 pring = &psli->ring[psli->fcp_ring];
3107
3108 spin_lock_irq(&phba->hbalock);
3109 /* Retrieve everything on txq */
3110 list_splice_init(&pring->txq, &txq);
3111 pring->txq_cnt = 0;
3112
3113 /* Retrieve everything on the txcmplq */
3114 list_splice_init(&pring->txcmplq, &txcmplq);
3115 pring->txcmplq_cnt = 0;
3116 spin_unlock_irq(&phba->hbalock);
3117
3118 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003119 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3120 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003121
3122 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003123 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3124 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003125}
3126
3127/**
James Smart3772a992009-05-22 14:50:54 -04003128 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003129 * @phba: Pointer to HBA context object.
3130 * @mask: Bit mask to be checked.
3131 *
3132 * This function reads the host status register and compares
3133 * with the provided bit mask to check if HBA completed
3134 * the restart. This function will wait in a loop for the
3135 * HBA to complete restart. If the HBA does not restart within
3136 * 15 iterations, the function will reset the HBA again. The
3137 * function returns 1 when HBA fail to restart otherwise returns
3138 * zero.
3139 **/
James Smart3772a992009-05-22 14:50:54 -04003140static int
3141lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003142{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003143 uint32_t status;
3144 int i = 0;
3145 int retval = 0;
dea31012005-04-17 16:05:31 -05003146
Jamie Wellnitz41415862006-02-28 19:25:27 -05003147 /* Read the HBA Host Status Register */
3148 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003149
Jamie Wellnitz41415862006-02-28 19:25:27 -05003150 /*
3151 * Check status register every 100ms for 5 retries, then every
3152 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3153 * every 2.5 sec for 4.
3154 * Break our of the loop if errors occurred during init.
3155 */
3156 while (((status & mask) != mask) &&
3157 !(status & HS_FFERM) &&
3158 i++ < 20) {
dea31012005-04-17 16:05:31 -05003159
Jamie Wellnitz41415862006-02-28 19:25:27 -05003160 if (i <= 5)
3161 msleep(10);
3162 else if (i <= 10)
3163 msleep(500);
3164 else
3165 msleep(2500);
dea31012005-04-17 16:05:31 -05003166
Jamie Wellnitz41415862006-02-28 19:25:27 -05003167 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003168 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003169 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003170 lpfc_sli_brdrestart(phba);
3171 }
3172 /* Read the HBA Host Status Register */
3173 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003174 }
dea31012005-04-17 16:05:31 -05003175
Jamie Wellnitz41415862006-02-28 19:25:27 -05003176 /* Check to see if any errors occurred during init */
3177 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05003178 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003179 retval = 1;
3180 }
dea31012005-04-17 16:05:31 -05003181
Jamie Wellnitz41415862006-02-28 19:25:27 -05003182 return retval;
dea31012005-04-17 16:05:31 -05003183}
3184
James Smartda0436e2009-05-22 14:51:39 -04003185/**
3186 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3187 * @phba: Pointer to HBA context object.
3188 * @mask: Bit mask to be checked.
3189 *
3190 * This function checks the host status register to check if HBA is
3191 * ready. This function will wait in a loop for the HBA to be ready
3192 * If the HBA is not ready , the function will will reset the HBA PCI
3193 * function again. The function returns 1 when HBA fail to be ready
3194 * otherwise returns zero.
3195 **/
3196static int
3197lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3198{
3199 uint32_t status;
3200 int retval = 0;
3201
3202 /* Read the HBA Host Status Register */
3203 status = lpfc_sli4_post_status_check(phba);
3204
3205 if (status) {
3206 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3207 lpfc_sli_brdrestart(phba);
3208 status = lpfc_sli4_post_status_check(phba);
3209 }
3210
3211 /* Check to see if any errors occurred during init */
3212 if (status) {
3213 phba->link_state = LPFC_HBA_ERROR;
3214 retval = 1;
3215 } else
3216 phba->sli4_hba.intr_enable = 0;
3217
3218 return retval;
3219}
3220
3221/**
3222 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3223 * @phba: Pointer to HBA context object.
3224 * @mask: Bit mask to be checked.
3225 *
3226 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3227 * from the API jump table function pointer from the lpfc_hba struct.
3228 **/
3229int
3230lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3231{
3232 return phba->lpfc_sli_brdready(phba, mask);
3233}
3234
James Smart92908312006-03-07 15:04:13 -05003235#define BARRIER_TEST_PATTERN (0xdeadbeef)
3236
James Smarte59058c2008-08-24 21:49:00 -04003237/**
James Smart3621a712009-04-06 18:47:14 -04003238 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003239 * @phba: Pointer to HBA context object.
3240 *
3241 * This function is called before resetting an HBA. This
3242 * function requests HBA to quiesce DMAs before a reset.
3243 **/
James Smart2e0fef82007-06-17 19:56:36 -05003244void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003245{
James Smart65a29c12006-07-06 15:50:50 -04003246 uint32_t __iomem *resp_buf;
3247 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003248 volatile uint32_t mbox;
3249 uint32_t hc_copy;
3250 int i;
3251 uint8_t hdrtype;
3252
3253 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3254 if (hdrtype != 0x80 ||
3255 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3256 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3257 return;
3258
3259 /*
3260 * Tell the other part of the chip to suspend temporarily all
3261 * its DMA activity.
3262 */
James Smart65a29c12006-07-06 15:50:50 -04003263 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003264
3265 /* Disable the error attention */
3266 hc_copy = readl(phba->HCregaddr);
3267 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3268 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003269 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003270
3271 if (readl(phba->HAregaddr) & HA_ERATT) {
3272 /* Clear Chip error bit */
3273 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003274 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003275 }
3276
3277 mbox = 0;
3278 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3279 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3280
3281 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003282 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003283 writel(mbox, mbox_buf);
3284
3285 for (i = 0;
3286 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3287 mdelay(1);
3288
3289 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003290 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003291 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003292 goto restore_hc;
3293 else
3294 goto clear_errat;
3295 }
3296
3297 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3298 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3299 mdelay(1);
3300
3301clear_errat:
3302
3303 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3304 mdelay(1);
3305
3306 if (readl(phba->HAregaddr) & HA_ERATT) {
3307 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003308 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003309 }
3310
3311restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003312 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003313 writel(hc_copy, phba->HCregaddr);
3314 readl(phba->HCregaddr); /* flush */
3315}
3316
James Smarte59058c2008-08-24 21:49:00 -04003317/**
James Smart3621a712009-04-06 18:47:14 -04003318 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003319 * @phba: Pointer to HBA context object.
3320 *
3321 * This function issues a kill_board mailbox command and waits for
3322 * the error attention interrupt. This function is called for stopping
3323 * the firmware processing. The caller is not required to hold any
3324 * locks. This function calls lpfc_hba_down_post function to free
3325 * any pending commands after the kill. The function will return 1 when it
3326 * fails to kill the board else will return 0.
3327 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003328int
James Smart2e0fef82007-06-17 19:56:36 -05003329lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003330{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003331 struct lpfc_sli *psli;
3332 LPFC_MBOXQ_t *pmb;
3333 uint32_t status;
3334 uint32_t ha_copy;
3335 int retval;
3336 int i = 0;
3337
3338 psli = &phba->sli;
3339
3340 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003341 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003342 "0329 Kill HBA Data: x%x x%x\n",
3343 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003344
James Smart98c9ea52007-10-27 13:37:33 -04003345 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3346 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003347 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003348
3349 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003350 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003351 status = readl(phba->HCregaddr);
3352 status &= ~HC_ERINT_ENA;
3353 writel(status, phba->HCregaddr);
3354 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003355 phba->link_flag |= LS_IGNORE_ERATT;
3356 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003357
3358 lpfc_kill_board(phba, pmb);
3359 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3360 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3361
3362 if (retval != MBX_SUCCESS) {
3363 if (retval != MBX_BUSY)
3364 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003365 spin_lock_irq(&phba->hbalock);
3366 phba->link_flag &= ~LS_IGNORE_ERATT;
3367 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003368 return 1;
3369 }
3370
James Smartf4b4c682009-05-22 14:53:12 -04003371 spin_lock_irq(&phba->hbalock);
3372 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3373 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003374
Jamie Wellnitz41415862006-02-28 19:25:27 -05003375 mempool_free(pmb, phba->mbox_mem_pool);
3376
3377 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3378 * attention every 100ms for 3 seconds. If we don't get ERATT after
3379 * 3 seconds we still set HBA_ERROR state because the status of the
3380 * board is now undefined.
3381 */
3382 ha_copy = readl(phba->HAregaddr);
3383
3384 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3385 mdelay(100);
3386 ha_copy = readl(phba->HAregaddr);
3387 }
3388
3389 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003390 if (ha_copy & HA_ERATT) {
3391 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003392 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003393 }
James Smart2e0fef82007-06-17 19:56:36 -05003394 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003395 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003396 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003397 phba->link_flag &= ~LS_IGNORE_ERATT;
3398 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003399
Jamie Wellnitz41415862006-02-28 19:25:27 -05003400 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003401 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003402
James Smart2e0fef82007-06-17 19:56:36 -05003403 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003404}
3405
James Smarte59058c2008-08-24 21:49:00 -04003406/**
James Smart3772a992009-05-22 14:50:54 -04003407 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003408 * @phba: Pointer to HBA context object.
3409 *
3410 * This function resets the HBA by writing HC_INITFF to the control
3411 * register. After the HBA resets, this function resets all the iocb ring
3412 * indices. This function disables PCI layer parity checking during
3413 * the reset.
3414 * This function returns 0 always.
3415 * The caller is not required to hold any locks.
3416 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003417int
James Smart2e0fef82007-06-17 19:56:36 -05003418lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003419{
3420 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003421 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003422 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003423 int i;
dea31012005-04-17 16:05:31 -05003424
Jamie Wellnitz41415862006-02-28 19:25:27 -05003425 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003426
Jamie Wellnitz41415862006-02-28 19:25:27 -05003427 /* Reset HBA */
3428 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003429 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003430 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003431
3432 /* perform board reset */
3433 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003434 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003435 phba->pport->fc_myDID = 0;
3436 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003437
Jamie Wellnitz41415862006-02-28 19:25:27 -05003438 /* Turn off parity checking and serr during the physical reset */
3439 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3440 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3441 (cfg_value &
3442 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3443
James Smart3772a992009-05-22 14:50:54 -04003444 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3445
Jamie Wellnitz41415862006-02-28 19:25:27 -05003446 /* Now toggle INITFF bit in the Host Control Register */
3447 writel(HC_INITFF, phba->HCregaddr);
3448 mdelay(1);
3449 readl(phba->HCregaddr); /* flush */
3450 writel(0, phba->HCregaddr);
3451 readl(phba->HCregaddr); /* flush */
3452
3453 /* Restore PCI cmd register */
3454 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003455
3456 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003457 for (i = 0; i < psli->num_rings; i++) {
3458 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003459 pring->flag = 0;
3460 pring->rspidx = 0;
3461 pring->next_cmdidx = 0;
3462 pring->local_getidx = 0;
3463 pring->cmdidx = 0;
3464 pring->missbufcnt = 0;
3465 }
dea31012005-04-17 16:05:31 -05003466
James Smart2e0fef82007-06-17 19:56:36 -05003467 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003468 return 0;
3469}
3470
James Smarte59058c2008-08-24 21:49:00 -04003471/**
James Smartda0436e2009-05-22 14:51:39 -04003472 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3473 * @phba: Pointer to HBA context object.
3474 *
3475 * This function resets a SLI4 HBA. This function disables PCI layer parity
3476 * checking during resets the device. The caller is not required to hold
3477 * any locks.
3478 *
3479 * This function returns 0 always.
3480 **/
3481int
3482lpfc_sli4_brdreset(struct lpfc_hba *phba)
3483{
3484 struct lpfc_sli *psli = &phba->sli;
3485 uint16_t cfg_value;
3486 uint8_t qindx;
3487
3488 /* Reset HBA */
3489 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3490 "0295 Reset HBA Data: x%x x%x\n",
3491 phba->pport->port_state, psli->sli_flag);
3492
3493 /* perform board reset */
3494 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003495 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003496 phba->pport->fc_myDID = 0;
3497 phba->pport->fc_prevDID = 0;
3498
3499 /* Turn off parity checking and serr during the physical reset */
3500 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3501 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3502 (cfg_value &
3503 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3504
3505 spin_lock_irq(&phba->hbalock);
3506 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3507 phba->fcf.fcf_flag = 0;
3508 /* Clean up the child queue list for the CQs */
3509 list_del_init(&phba->sli4_hba.mbx_wq->list);
3510 list_del_init(&phba->sli4_hba.els_wq->list);
3511 list_del_init(&phba->sli4_hba.hdr_rq->list);
3512 list_del_init(&phba->sli4_hba.dat_rq->list);
3513 list_del_init(&phba->sli4_hba.mbx_cq->list);
3514 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003515 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3516 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3517 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3518 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3519 spin_unlock_irq(&phba->hbalock);
3520
3521 /* Now physically reset the device */
3522 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3523 "0389 Performing PCI function reset!\n");
3524 /* Perform FCoE PCI function reset */
3525 lpfc_pci_function_reset(phba);
3526
3527 return 0;
3528}
3529
3530/**
3531 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003532 * @phba: Pointer to HBA context object.
3533 *
3534 * This function is called in the SLI initialization code path to
3535 * restart the HBA. The caller is not required to hold any lock.
3536 * This function writes MBX_RESTART mailbox command to the SLIM and
3537 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3538 * function to free any pending commands. The function enables
3539 * POST only during the first initialization. The function returns zero.
3540 * The function does not guarantee completion of MBX_RESTART mailbox
3541 * command before the return of this function.
3542 **/
James Smartda0436e2009-05-22 14:51:39 -04003543static int
3544lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003545{
3546 MAILBOX_t *mb;
3547 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003548 volatile uint32_t word0;
3549 void __iomem *to_slim;
3550
James Smart2e0fef82007-06-17 19:56:36 -05003551 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003552
3553 psli = &phba->sli;
3554
3555 /* Restart HBA */
3556 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003557 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003558 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003559
3560 word0 = 0;
3561 mb = (MAILBOX_t *) &word0;
3562 mb->mbxCommand = MBX_RESTART;
3563 mb->mbxHc = 1;
3564
James Smart92908312006-03-07 15:04:13 -05003565 lpfc_reset_barrier(phba);
3566
Jamie Wellnitz41415862006-02-28 19:25:27 -05003567 to_slim = phba->MBslimaddr;
3568 writel(*(uint32_t *) mb, to_slim);
3569 readl(to_slim); /* flush */
3570
3571 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003572 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003573 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003574 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003575 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003576 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003577 writel(*(uint32_t *) mb, to_slim);
3578 readl(to_slim); /* flush */
3579
3580 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003581 phba->pport->stopped = 0;
3582 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003583 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003584 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003585
James Smart64ba8812006-08-02 15:24:34 -04003586 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3587 psli->stats_start = get_seconds();
3588
James Smarteaf15d52008-12-04 22:39:29 -05003589 /* Give the INITFF and Post time to settle. */
3590 mdelay(100);
dea31012005-04-17 16:05:31 -05003591
Jamie Wellnitz41415862006-02-28 19:25:27 -05003592 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003593
3594 return 0;
3595}
3596
James Smarte59058c2008-08-24 21:49:00 -04003597/**
James Smartda0436e2009-05-22 14:51:39 -04003598 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3599 * @phba: Pointer to HBA context object.
3600 *
3601 * This function is called in the SLI initialization code path to restart
3602 * a SLI4 HBA. The caller is not required to hold any lock.
3603 * At the end of the function, it calls lpfc_hba_down_post function to
3604 * free any pending commands.
3605 **/
3606static int
3607lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3608{
3609 struct lpfc_sli *psli = &phba->sli;
3610
3611
3612 /* Restart HBA */
3613 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3614 "0296 Restart HBA Data: x%x x%x\n",
3615 phba->pport->port_state, psli->sli_flag);
3616
3617 lpfc_sli4_brdreset(phba);
3618
3619 spin_lock_irq(&phba->hbalock);
3620 phba->pport->stopped = 0;
3621 phba->link_state = LPFC_INIT_START;
3622 phba->hba_flag = 0;
3623 spin_unlock_irq(&phba->hbalock);
3624
3625 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3626 psli->stats_start = get_seconds();
3627
3628 lpfc_hba_down_post(phba);
3629
3630 return 0;
3631}
3632
3633/**
3634 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3635 * @phba: Pointer to HBA context object.
3636 *
3637 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3638 * API jump table function pointer from the lpfc_hba struct.
3639**/
3640int
3641lpfc_sli_brdrestart(struct lpfc_hba *phba)
3642{
3643 return phba->lpfc_sli_brdrestart(phba);
3644}
3645
3646/**
James Smart3621a712009-04-06 18:47:14 -04003647 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003648 * @phba: Pointer to HBA context object.
3649 *
3650 * This function is called after a HBA restart to wait for successful
3651 * restart of the HBA. Successful restart of the HBA is indicated by
3652 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3653 * iteration, the function will restart the HBA again. The function returns
3654 * zero if HBA successfully restarted else returns negative error code.
3655 **/
dea31012005-04-17 16:05:31 -05003656static int
3657lpfc_sli_chipset_init(struct lpfc_hba *phba)
3658{
3659 uint32_t status, i = 0;
3660
3661 /* Read the HBA Host Status Register */
3662 status = readl(phba->HSregaddr);
3663
3664 /* Check status register to see what current state is */
3665 i = 0;
3666 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3667
3668 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3669 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3670 * 4.
3671 */
3672 if (i++ >= 20) {
3673 /* Adapter failed to init, timeout, status reg
3674 <status> */
James Smarted957682007-06-17 19:56:37 -05003675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003676 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003677 "timeout, status reg x%x, "
3678 "FW Data: A8 x%x AC x%x\n", status,
3679 readl(phba->MBslimaddr + 0xa8),
3680 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003681 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003682 return -ETIMEDOUT;
3683 }
3684
3685 /* Check to see if any errors occurred during init */
3686 if (status & HS_FFERM) {
3687 /* ERROR: During chipset initialization */
3688 /* Adapter failed to init, chipset, status reg
3689 <status> */
James Smarted957682007-06-17 19:56:37 -05003690 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003691 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003692 "chipset, status reg x%x, "
3693 "FW Data: A8 x%x AC x%x\n", status,
3694 readl(phba->MBslimaddr + 0xa8),
3695 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003696 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003697 return -EIO;
3698 }
3699
3700 if (i <= 5) {
3701 msleep(10);
3702 } else if (i <= 10) {
3703 msleep(500);
3704 } else {
3705 msleep(2500);
3706 }
3707
3708 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003709 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003710 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003711 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003712 }
3713 /* Read the HBA Host Status Register */
3714 status = readl(phba->HSregaddr);
3715 }
3716
3717 /* Check to see if any errors occurred during init */
3718 if (status & HS_FFERM) {
3719 /* ERROR: During chipset initialization */
3720 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003721 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003722 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003723 "status reg x%x, "
3724 "FW Data: A8 x%x AC x%x\n", status,
3725 readl(phba->MBslimaddr + 0xa8),
3726 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003727 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003728 return -EIO;
3729 }
3730
3731 /* Clear all interrupt enable conditions */
3732 writel(0, phba->HCregaddr);
3733 readl(phba->HCregaddr); /* flush */
3734
3735 /* setup host attn register */
3736 writel(0xffffffff, phba->HAregaddr);
3737 readl(phba->HAregaddr); /* flush */
3738 return 0;
3739}
3740
James Smarte59058c2008-08-24 21:49:00 -04003741/**
James Smart3621a712009-04-06 18:47:14 -04003742 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003743 *
3744 * This function calculates and returns the number of HBQs required to be
3745 * configured.
3746 **/
James Smart78b2d852007-08-02 11:10:21 -04003747int
James Smarted957682007-06-17 19:56:37 -05003748lpfc_sli_hbq_count(void)
3749{
James Smart92d7f7b2007-06-17 19:56:38 -05003750 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003751}
3752
James Smarte59058c2008-08-24 21:49:00 -04003753/**
James Smart3621a712009-04-06 18:47:14 -04003754 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003755 *
3756 * This function adds the number of hbq entries in every HBQ to get
3757 * the total number of hbq entries required for the HBA and returns
3758 * the total count.
3759 **/
James Smarted957682007-06-17 19:56:37 -05003760static int
3761lpfc_sli_hbq_entry_count(void)
3762{
3763 int hbq_count = lpfc_sli_hbq_count();
3764 int count = 0;
3765 int i;
3766
3767 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003768 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003769 return count;
3770}
3771
James Smarte59058c2008-08-24 21:49:00 -04003772/**
James Smart3621a712009-04-06 18:47:14 -04003773 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003774 *
3775 * This function calculates amount of memory required for all hbq entries
3776 * to be configured and returns the total memory required.
3777 **/
dea31012005-04-17 16:05:31 -05003778int
James Smarted957682007-06-17 19:56:37 -05003779lpfc_sli_hbq_size(void)
3780{
3781 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3782}
3783
James Smarte59058c2008-08-24 21:49:00 -04003784/**
James Smart3621a712009-04-06 18:47:14 -04003785 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003786 * @phba: Pointer to HBA context object.
3787 *
3788 * This function is called during the SLI initialization to configure
3789 * all the HBQs and post buffers to the HBQ. The caller is not
3790 * required to hold any locks. This function will return zero if successful
3791 * else it will return negative error code.
3792 **/
James Smarted957682007-06-17 19:56:37 -05003793static int
3794lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3795{
3796 int hbq_count = lpfc_sli_hbq_count();
3797 LPFC_MBOXQ_t *pmb;
3798 MAILBOX_t *pmbox;
3799 uint32_t hbqno;
3800 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003801
James Smart92d7f7b2007-06-17 19:56:38 -05003802 /* Get a Mailbox buffer to setup mailbox
3803 * commands for HBA initialization
3804 */
James Smarted957682007-06-17 19:56:37 -05003805 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3806
3807 if (!pmb)
3808 return -ENOMEM;
3809
James Smart04c68492009-05-22 14:52:52 -04003810 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003811
3812 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3813 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003814 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003815
3816 hbq_entry_index = 0;
3817 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3818 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3819 phba->hbqs[hbqno].hbqPutIdx = 0;
3820 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3821 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003822 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003823 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3824 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003825 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3826
3827 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3828 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3829 mbxStatus <status>, ring <num> */
3830
3831 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003832 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003833 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003834 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003835 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003836 pmbox->mbxStatus, hbqno);
3837
3838 phba->link_state = LPFC_HBA_ERROR;
3839 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003840 return ENXIO;
3841 }
3842 }
3843 phba->hbq_count = hbq_count;
3844
James Smarted957682007-06-17 19:56:37 -05003845 mempool_free(pmb, phba->mbox_mem_pool);
3846
James Smart92d7f7b2007-06-17 19:56:38 -05003847 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003848 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3849 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003850 return 0;
3851}
3852
James Smarte59058c2008-08-24 21:49:00 -04003853/**
James Smart4f774512009-05-22 14:52:35 -04003854 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3855 * @phba: Pointer to HBA context object.
3856 *
3857 * This function is called during the SLI initialization to configure
3858 * all the HBQs and post buffers to the HBQ. The caller is not
3859 * required to hold any locks. This function will return zero if successful
3860 * else it will return negative error code.
3861 **/
3862static int
3863lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3864{
3865 phba->hbq_in_use = 1;
3866 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3867 phba->hbq_count = 1;
3868 /* Initially populate or replenish the HBQs */
3869 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3870 return 0;
3871}
3872
3873/**
James Smart3621a712009-04-06 18:47:14 -04003874 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003875 * @phba: Pointer to HBA context object.
3876 * @sli_mode: sli mode - 2/3
3877 *
3878 * This function is called by the sli intialization code path
3879 * to issue config_port mailbox command. This function restarts the
3880 * HBA firmware and issues a config_port mailbox command to configure
3881 * the SLI interface in the sli mode specified by sli_mode
3882 * variable. The caller is not required to hold any locks.
3883 * The function returns 0 if successful, else returns negative error
3884 * code.
3885 **/
James Smart93996272008-08-24 21:50:30 -04003886int
3887lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003888{
3889 LPFC_MBOXQ_t *pmb;
3890 uint32_t resetcount = 0, rc = 0, done = 0;
3891
3892 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3893 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003894 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003895 return -ENOMEM;
3896 }
3897
James Smarted957682007-06-17 19:56:37 -05003898 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003899 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003900 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003901 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003902 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003903 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003904 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003905 rc = lpfc_sli_chipset_init(phba);
3906 if (rc)
3907 break;
3908
James Smart2e0fef82007-06-17 19:56:36 -05003909 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003910 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003911 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003912 resetcount++;
3913
James Smarted957682007-06-17 19:56:37 -05003914 /* Call pre CONFIG_PORT mailbox command initialization. A
3915 * value of 0 means the call was successful. Any other
3916 * nonzero value is a failure, but if ERESTART is returned,
3917 * the driver may reset the HBA and try again.
3918 */
dea31012005-04-17 16:05:31 -05003919 rc = lpfc_config_port_prep(phba);
3920 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003921 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003922 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003923 } else if (rc)
dea31012005-04-17 16:05:31 -05003924 break;
James Smart2e0fef82007-06-17 19:56:36 -05003925 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003926 lpfc_config_port(phba, pmb);
3927 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003928 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3929 LPFC_SLI3_HBQ_ENABLED |
3930 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003931 LPFC_SLI3_INB_ENABLED |
3932 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003933 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003935 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003936 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003937 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003938 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003939 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003940 spin_unlock_irq(&phba->hbalock);
3941 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003942 } else {
3943 /* Allow asynchronous mailbox command to go through */
3944 spin_lock_irq(&phba->hbalock);
3945 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3946 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003947 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003948 }
dea31012005-04-17 16:05:31 -05003949 }
James Smarted957682007-06-17 19:56:37 -05003950 if (!done) {
3951 rc = -EINVAL;
3952 goto do_prep_failed;
3953 }
James Smart04c68492009-05-22 14:52:52 -04003954 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3955 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003956 rc = -ENXIO;
3957 goto do_prep_failed;
3958 }
James Smart04c68492009-05-22 14:52:52 -04003959 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003960 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003961 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3962 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3963 phba->max_vpi : phba->max_vports;
3964
James Smart34b02dc2008-08-24 21:49:55 -04003965 } else
3966 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003967 if (pmb->u.mb.un.varCfgPort.gdss)
3968 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3969 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003970 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003971 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003972 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003973 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003974 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003975 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003976 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3977 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3978 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3979 phba->inb_last_counter =
3980 phba->mbox->us.s3_inb_pgp.counter;
3981 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003982 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003983 phba->port_gp = phba->mbox->us.s3_pgp.port;
3984 phba->inb_ha_copy = NULL;
3985 phba->inb_counter = NULL;
3986 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003987
3988 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003989 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003990 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3991 else
3992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3993 "0443 Adapter did not grant "
3994 "BlockGuard\n");
3995 }
James Smart34b02dc2008-08-24 21:49:55 -04003996 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003997 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04003998 phba->port_gp = phba->mbox->us.s2.port;
3999 phba->inb_ha_copy = NULL;
4000 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04004001 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004002 }
James Smart92d7f7b2007-06-17 19:56:38 -05004003do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004004 mempool_free(pmb, phba->mbox_mem_pool);
4005 return rc;
4006}
4007
James Smarte59058c2008-08-24 21:49:00 -04004008
4009/**
James Smart3621a712009-04-06 18:47:14 -04004010 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004011 * @phba: Pointer to HBA context object.
4012 *
4013 * This function is the main SLI intialization function. This function
4014 * is called by the HBA intialization code, HBA reset code and HBA
4015 * error attention handler code. Caller is not required to hold any
4016 * locks. This function issues config_port mailbox command to configure
4017 * the SLI, setup iocb rings and HBQ rings. In the end the function
4018 * calls the config_port_post function to issue init_link mailbox
4019 * command and to start the discovery. The function will return zero
4020 * if successful, else it will return negative error code.
4021 **/
James Smarted957682007-06-17 19:56:37 -05004022int
4023lpfc_sli_hba_setup(struct lpfc_hba *phba)
4024{
4025 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004026 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004027
4028 switch (lpfc_sli_mode) {
4029 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004030 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004032 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004033 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004034 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004035 break;
4036 }
James Smarted957682007-06-17 19:56:37 -05004037 mode = 2;
4038 break;
4039 case 0:
4040 case 3:
4041 break;
4042 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004043 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004044 "1819 Unrecognized lpfc_sli_mode "
4045 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004046
4047 break;
4048 }
4049
James Smart93996272008-08-24 21:50:30 -04004050 rc = lpfc_sli_config_port(phba, mode);
4051
James Smarted957682007-06-17 19:56:37 -05004052 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004054 "1820 Unable to select SLI-3. "
4055 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004056 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004057 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004058 if (rc)
dea31012005-04-17 16:05:31 -05004059 goto lpfc_sli_hba_setup_error;
4060
James Smarted957682007-06-17 19:56:37 -05004061 if (phba->sli_rev == 3) {
4062 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4063 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004064 } else {
4065 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4066 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004067 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004068 }
4069
4070 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004071 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4072 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004073 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004074
4075 if (rc)
4076 goto lpfc_sli_hba_setup_error;
4077
James Smart93996272008-08-24 21:50:30 -04004078 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004079 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4080 rc = lpfc_sli_hbq_setup(phba);
4081 if (rc)
4082 goto lpfc_sli_hba_setup_error;
4083 }
James Smart04c68492009-05-22 14:52:52 -04004084 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004085 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004086 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004087
4088 rc = lpfc_config_port_post(phba);
4089 if (rc)
4090 goto lpfc_sli_hba_setup_error;
4091
James Smarted957682007-06-17 19:56:37 -05004092 return rc;
4093
James Smart92d7f7b2007-06-17 19:56:38 -05004094lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004095 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05004096 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004097 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004098 return rc;
4099}
4100
James Smartda0436e2009-05-22 14:51:39 -04004101/**
4102 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4103 * @phba: Pointer to HBA context object.
4104 * @mboxq: mailbox pointer.
4105 * This function issue a dump mailbox command to read config region
4106 * 23 and parse the records in the region and populate driver
4107 * data structure.
4108 **/
4109static int
4110lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4111 LPFC_MBOXQ_t *mboxq)
4112{
4113 struct lpfc_dmabuf *mp;
4114 struct lpfc_mqe *mqe;
4115 uint32_t data_length;
4116 int rc;
4117
4118 /* Program the default value of vlan_id and fc_map */
4119 phba->valid_vlan = 0;
4120 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4121 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4122 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4123
4124 mqe = &mboxq->u.mqe;
4125 if (lpfc_dump_fcoe_param(phba, mboxq))
4126 return -ENOMEM;
4127
4128 mp = (struct lpfc_dmabuf *) mboxq->context1;
4129 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4130
4131 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4132 "(%d):2571 Mailbox cmd x%x Status x%x "
4133 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4134 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4135 "CQ: x%x x%x x%x x%x\n",
4136 mboxq->vport ? mboxq->vport->vpi : 0,
4137 bf_get(lpfc_mqe_command, mqe),
4138 bf_get(lpfc_mqe_status, mqe),
4139 mqe->un.mb_words[0], mqe->un.mb_words[1],
4140 mqe->un.mb_words[2], mqe->un.mb_words[3],
4141 mqe->un.mb_words[4], mqe->un.mb_words[5],
4142 mqe->un.mb_words[6], mqe->un.mb_words[7],
4143 mqe->un.mb_words[8], mqe->un.mb_words[9],
4144 mqe->un.mb_words[10], mqe->un.mb_words[11],
4145 mqe->un.mb_words[12], mqe->un.mb_words[13],
4146 mqe->un.mb_words[14], mqe->un.mb_words[15],
4147 mqe->un.mb_words[16], mqe->un.mb_words[50],
4148 mboxq->mcqe.word0,
4149 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4150 mboxq->mcqe.trailer);
4151
4152 if (rc) {
4153 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4154 kfree(mp);
4155 return -EIO;
4156 }
4157 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004158 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004159 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4160 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004161 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004162 }
James Smartda0436e2009-05-22 14:51:39 -04004163
4164 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4165 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4166 kfree(mp);
4167 return 0;
4168}
4169
4170/**
4171 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4172 * @phba: pointer to lpfc hba data structure.
4173 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4174 * @vpd: pointer to the memory to hold resulting port vpd data.
4175 * @vpd_size: On input, the number of bytes allocated to @vpd.
4176 * On output, the number of data bytes in @vpd.
4177 *
4178 * This routine executes a READ_REV SLI4 mailbox command. In
4179 * addition, this routine gets the port vpd data.
4180 *
4181 * Return codes
4182 * 0 - sucessful
4183 * ENOMEM - could not allocated memory.
4184 **/
4185static int
4186lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4187 uint8_t *vpd, uint32_t *vpd_size)
4188{
4189 int rc = 0;
4190 uint32_t dma_size;
4191 struct lpfc_dmabuf *dmabuf;
4192 struct lpfc_mqe *mqe;
4193
4194 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4195 if (!dmabuf)
4196 return -ENOMEM;
4197
4198 /*
4199 * Get a DMA buffer for the vpd data resulting from the READ_REV
4200 * mailbox command.
4201 */
4202 dma_size = *vpd_size;
4203 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4204 dma_size,
4205 &dmabuf->phys,
4206 GFP_KERNEL);
4207 if (!dmabuf->virt) {
4208 kfree(dmabuf);
4209 return -ENOMEM;
4210 }
4211 memset(dmabuf->virt, 0, dma_size);
4212
4213 /*
4214 * The SLI4 implementation of READ_REV conflicts at word1,
4215 * bits 31:16 and SLI4 adds vpd functionality not present
4216 * in SLI3. This code corrects the conflicts.
4217 */
4218 lpfc_read_rev(phba, mboxq);
4219 mqe = &mboxq->u.mqe;
4220 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4221 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4222 mqe->un.read_rev.word1 &= 0x0000FFFF;
4223 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4224 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4225
4226 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4227 if (rc) {
4228 dma_free_coherent(&phba->pcidev->dev, dma_size,
4229 dmabuf->virt, dmabuf->phys);
4230 return -EIO;
4231 }
4232
James Smartda0436e2009-05-22 14:51:39 -04004233 /*
4234 * The available vpd length cannot be bigger than the
4235 * DMA buffer passed to the port. Catch the less than
4236 * case and update the caller's size.
4237 */
4238 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4239 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4240
4241 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4242 dma_free_coherent(&phba->pcidev->dev, dma_size,
4243 dmabuf->virt, dmabuf->phys);
4244 kfree(dmabuf);
4245 return 0;
4246}
4247
4248/**
4249 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4250 * @phba: pointer to lpfc hba data structure.
4251 *
4252 * This routine is called to explicitly arm the SLI4 device's completion and
4253 * event queues
4254 **/
4255static void
4256lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4257{
4258 uint8_t fcp_eqidx;
4259
4260 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4261 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004262 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4263 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4264 LPFC_QUEUE_REARM);
4265 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4266 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4267 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4268 LPFC_QUEUE_REARM);
4269}
4270
4271/**
4272 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4273 * @phba: Pointer to HBA context object.
4274 *
4275 * This function is the main SLI4 device intialization PCI function. This
4276 * function is called by the HBA intialization code, HBA reset code and
4277 * HBA error attention handler code. Caller is not required to hold any
4278 * locks.
4279 **/
4280int
4281lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4282{
4283 int rc;
4284 LPFC_MBOXQ_t *mboxq;
4285 struct lpfc_mqe *mqe;
4286 uint8_t *vpd;
4287 uint32_t vpd_size;
4288 uint32_t ftr_rsp = 0;
4289 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4290 struct lpfc_vport *vport = phba->pport;
4291 struct lpfc_dmabuf *mp;
4292
4293 /* Perform a PCI function reset to start from clean */
4294 rc = lpfc_pci_function_reset(phba);
4295 if (unlikely(rc))
4296 return -ENODEV;
4297
4298 /* Check the HBA Host Status Register for readyness */
4299 rc = lpfc_sli4_post_status_check(phba);
4300 if (unlikely(rc))
4301 return -ENODEV;
4302 else {
4303 spin_lock_irq(&phba->hbalock);
4304 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4305 spin_unlock_irq(&phba->hbalock);
4306 }
4307
4308 /*
4309 * Allocate a single mailbox container for initializing the
4310 * port.
4311 */
4312 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4313 if (!mboxq)
4314 return -ENOMEM;
4315
4316 /*
4317 * Continue initialization with default values even if driver failed
4318 * to read FCoE param config regions
4319 */
4320 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4321 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004322 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004323
4324 /* Issue READ_REV to collect vpd and FW information. */
4325 vpd_size = PAGE_SIZE;
4326 vpd = kzalloc(vpd_size, GFP_KERNEL);
4327 if (!vpd) {
4328 rc = -ENOMEM;
4329 goto out_free_mbox;
4330 }
4331
4332 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4333 if (unlikely(rc))
4334 goto out_free_vpd;
4335
4336 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004337 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4338 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4339 phba->hba_flag |= HBA_FCOE_SUPPORT;
4340 if (phba->sli_rev != LPFC_SLI_REV4 ||
4341 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004342 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4343 "0376 READ_REV Error. SLI Level %d "
4344 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004345 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004346 rc = -EIO;
4347 goto out_free_vpd;
4348 }
James Smartda0436e2009-05-22 14:51:39 -04004349 /*
4350 * Evaluate the read rev and vpd data. Populate the driver
4351 * state with the results. If this routine fails, the failure
4352 * is not fatal as the driver will use generic values.
4353 */
4354 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4355 if (unlikely(!rc)) {
4356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4357 "0377 Error %d parsing vpd. "
4358 "Using defaults.\n", rc);
4359 rc = 0;
4360 }
4361
James Smartf1126682009-06-10 17:22:44 -04004362 /* Save information as VPD data */
4363 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4364 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4365 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4366 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4367 &mqe->un.read_rev);
4368 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4369 &mqe->un.read_rev);
4370 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4371 &mqe->un.read_rev);
4372 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4373 &mqe->un.read_rev);
4374 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4375 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4376 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4377 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4378 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4379 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4380 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4381 "(%d):0380 READ_REV Status x%x "
4382 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4383 mboxq->vport ? mboxq->vport->vpi : 0,
4384 bf_get(lpfc_mqe_status, mqe),
4385 phba->vpd.rev.opFwName,
4386 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4387 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004388
4389 /*
4390 * Discover the port's supported feature set and match it against the
4391 * hosts requests.
4392 */
4393 lpfc_request_features(phba, mboxq);
4394 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4395 if (unlikely(rc)) {
4396 rc = -EIO;
4397 goto out_free_vpd;
4398 }
4399
4400 /*
4401 * The port must support FCP initiator mode as this is the
4402 * only mode running in the host.
4403 */
4404 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4405 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4406 "0378 No support for fcpi mode.\n");
4407 ftr_rsp++;
4408 }
4409
4410 /*
4411 * If the port cannot support the host's requested features
4412 * then turn off the global config parameters to disable the
4413 * feature in the driver. This is not a fatal error.
4414 */
4415 if ((phba->cfg_enable_bg) &&
4416 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4417 ftr_rsp++;
4418
4419 if (phba->max_vpi && phba->cfg_enable_npiv &&
4420 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4421 ftr_rsp++;
4422
4423 if (ftr_rsp) {
4424 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4425 "0379 Feature Mismatch Data: x%08x %08x "
4426 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4427 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4428 phba->cfg_enable_npiv, phba->max_vpi);
4429 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4430 phba->cfg_enable_bg = 0;
4431 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4432 phba->cfg_enable_npiv = 0;
4433 }
4434
4435 /* These SLI3 features are assumed in SLI4 */
4436 spin_lock_irq(&phba->hbalock);
4437 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4438 spin_unlock_irq(&phba->hbalock);
4439
4440 /* Read the port's service parameters. */
4441 lpfc_read_sparam(phba, mboxq, vport->vpi);
4442 mboxq->vport = vport;
4443 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4444 mp = (struct lpfc_dmabuf *) mboxq->context1;
4445 if (rc == MBX_SUCCESS) {
4446 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4447 rc = 0;
4448 }
4449
4450 /*
4451 * This memory was allocated by the lpfc_read_sparam routine. Release
4452 * it to the mbuf pool.
4453 */
4454 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4455 kfree(mp);
4456 mboxq->context1 = NULL;
4457 if (unlikely(rc)) {
4458 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4459 "0382 READ_SPARAM command failed "
4460 "status %d, mbxStatus x%x\n",
4461 rc, bf_get(lpfc_mqe_status, mqe));
4462 phba->link_state = LPFC_HBA_ERROR;
4463 rc = -EIO;
4464 goto out_free_vpd;
4465 }
4466
4467 if (phba->cfg_soft_wwnn)
4468 u64_to_wwn(phba->cfg_soft_wwnn,
4469 vport->fc_sparam.nodeName.u.wwn);
4470 if (phba->cfg_soft_wwpn)
4471 u64_to_wwn(phba->cfg_soft_wwpn,
4472 vport->fc_sparam.portName.u.wwn);
4473 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4474 sizeof(struct lpfc_name));
4475 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4476 sizeof(struct lpfc_name));
4477
4478 /* Update the fc_host data structures with new wwn. */
4479 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4480 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4481
4482 /* Register SGL pool to the device using non-embedded mailbox command */
4483 rc = lpfc_sli4_post_sgl_list(phba);
4484 if (unlikely(rc)) {
4485 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4486 "0582 Error %d during sgl post operation", rc);
4487 rc = -ENODEV;
4488 goto out_free_vpd;
4489 }
4490
4491 /* Register SCSI SGL pool to the device */
4492 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4493 if (unlikely(rc)) {
4494 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4495 "0383 Error %d during scsi sgl post opeation",
4496 rc);
4497 /* Some Scsi buffers were moved to the abort scsi list */
4498 /* A pci function reset will repost them */
4499 rc = -ENODEV;
4500 goto out_free_vpd;
4501 }
4502
4503 /* Post the rpi header region to the device. */
4504 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4505 if (unlikely(rc)) {
4506 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4507 "0393 Error %d during rpi post operation\n",
4508 rc);
4509 rc = -ENODEV;
4510 goto out_free_vpd;
4511 }
James Smart0c287582009-06-10 17:22:56 -04004512 if (phba->cfg_enable_fip)
4513 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
4514 else
4515 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
James Smartda0436e2009-05-22 14:51:39 -04004516
4517 /* Set up all the queues to the device */
4518 rc = lpfc_sli4_queue_setup(phba);
4519 if (unlikely(rc)) {
4520 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4521 "0381 Error %d during queue setup.\n ", rc);
4522 goto out_stop_timers;
4523 }
4524
4525 /* Arm the CQs and then EQs on device */
4526 lpfc_sli4_arm_cqeq_intr(phba);
4527
4528 /* Indicate device interrupt mode */
4529 phba->sli4_hba.intr_enable = 1;
4530
4531 /* Allow asynchronous mailbox command to go through */
4532 spin_lock_irq(&phba->hbalock);
4533 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4534 spin_unlock_irq(&phba->hbalock);
4535
4536 /* Post receive buffers to the device */
4537 lpfc_sli4_rb_setup(phba);
4538
4539 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004540 mod_timer(&vport->els_tmofunc,
4541 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004542
4543 /* Start heart beat timer */
4544 mod_timer(&phba->hb_tmofunc,
4545 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4546 phba->hb_outstanding = 0;
4547 phba->last_completion_time = jiffies;
4548
4549 /* Start error attention (ERATT) polling timer */
4550 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4551
4552 /*
4553 * The port is ready, set the host's link state to LINK_DOWN
4554 * in preparation for link interrupts.
4555 */
4556 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4557 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4558 lpfc_set_loopback_flag(phba);
4559 /* Change driver state to LPFC_LINK_DOWN right before init link */
4560 spin_lock_irq(&phba->hbalock);
4561 phba->link_state = LPFC_LINK_DOWN;
4562 spin_unlock_irq(&phba->hbalock);
4563 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4564 if (unlikely(rc != MBX_NOT_FINISHED)) {
4565 kfree(vpd);
4566 return 0;
4567 } else
4568 rc = -EIO;
4569
4570 /* Unset all the queues set up in this routine when error out */
4571 if (rc)
4572 lpfc_sli4_queue_unset(phba);
4573
4574out_stop_timers:
4575 if (rc)
4576 lpfc_stop_hba_timers(phba);
4577out_free_vpd:
4578 kfree(vpd);
4579out_free_mbox:
4580 mempool_free(mboxq, phba->mbox_mem_pool);
4581 return rc;
4582}
James Smarte59058c2008-08-24 21:49:00 -04004583
4584/**
James Smart3621a712009-04-06 18:47:14 -04004585 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004586 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004587 *
James Smarte59058c2008-08-24 21:49:00 -04004588 * This is the callback function for mailbox timer. The mailbox
4589 * timer is armed when a new mailbox command is issued and the timer
4590 * is deleted when the mailbox complete. The function is called by
4591 * the kernel timer code when a mailbox does not complete within
4592 * expected time. This function wakes up the worker thread to
4593 * process the mailbox timeout and returns. All the processing is
4594 * done by the worker thread function lpfc_mbox_timeout_handler.
4595 **/
dea31012005-04-17 16:05:31 -05004596void
4597lpfc_mbox_timeout(unsigned long ptr)
4598{
James Smart92d7f7b2007-06-17 19:56:38 -05004599 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004600 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004601 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004602
James Smart2e0fef82007-06-17 19:56:36 -05004603 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004604 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004605 if (!tmo_posted)
4606 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4607 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4608
James Smart5e9d9b82008-06-14 22:52:53 -04004609 if (!tmo_posted)
4610 lpfc_worker_wake_up(phba);
4611 return;
dea31012005-04-17 16:05:31 -05004612}
4613
James Smarte59058c2008-08-24 21:49:00 -04004614
4615/**
James Smart3621a712009-04-06 18:47:14 -04004616 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004617 * @phba: Pointer to HBA context object.
4618 *
4619 * This function is called from worker thread when a mailbox command times out.
4620 * The caller is not required to hold any locks. This function will reset the
4621 * HBA and recover all the pending commands.
4622 **/
dea31012005-04-17 16:05:31 -05004623void
4624lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4625{
James Smart2e0fef82007-06-17 19:56:36 -05004626 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004627 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004628 struct lpfc_sli *psli = &phba->sli;
4629 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004630
James Smarta257bf92009-04-06 18:48:10 -04004631 /* Check the pmbox pointer first. There is a race condition
4632 * between the mbox timeout handler getting executed in the
4633 * worklist and the mailbox actually completing. When this
4634 * race condition occurs, the mbox_active will be NULL.
4635 */
4636 spin_lock_irq(&phba->hbalock);
4637 if (pmbox == NULL) {
4638 lpfc_printf_log(phba, KERN_WARNING,
4639 LOG_MBOX | LOG_SLI,
4640 "0353 Active Mailbox cleared - mailbox timeout "
4641 "exiting\n");
4642 spin_unlock_irq(&phba->hbalock);
4643 return;
4644 }
4645
dea31012005-04-17 16:05:31 -05004646 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004647 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004648 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004649 mb->mbxCommand,
4650 phba->pport->port_state,
4651 phba->sli.sli_flag,
4652 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004653 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004654
James Smart1dcb58e2007-04-25 09:51:30 -04004655 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4656 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4657 * it to fail all oustanding SCSI IO.
4658 */
James Smart2e0fef82007-06-17 19:56:36 -05004659 spin_lock_irq(&phba->pport->work_port_lock);
4660 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4661 spin_unlock_irq(&phba->pport->work_port_lock);
4662 spin_lock_irq(&phba->hbalock);
4663 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004664 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004665 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004666
4667 pring = &psli->ring[psli->fcp_ring];
4668 lpfc_sli_abort_iocb_ring(phba, pring);
4669
4670 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004671 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004672
4673 /* Reset the HBA device */
4674 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004675}
4676
James Smarte59058c2008-08-24 21:49:00 -04004677/**
James Smart3772a992009-05-22 14:50:54 -04004678 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004679 * @phba: Pointer to HBA context object.
4680 * @pmbox: Pointer to mailbox object.
4681 * @flag: Flag indicating how the mailbox need to be processed.
4682 *
4683 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004684 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4685 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004686 * The mailbox command can be submitted in polling mode, in which case
4687 * this function will wait in a polling loop for the completion of the
4688 * mailbox.
4689 * If the mailbox is submitted in no_wait mode (not polling) the
4690 * function will submit the command and returns immediately without waiting
4691 * for the mailbox completion. The no_wait is supported only when HBA
4692 * is in SLI2/SLI3 mode - interrupts are enabled.
4693 * The SLI interface allows only one mailbox pending at a time. If the
4694 * mailbox is issued in polling mode and there is already a mailbox
4695 * pending, then the function will return an error. If the mailbox is issued
4696 * in NO_WAIT mode and there is a mailbox pending already, the function
4697 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4698 * The sli layer owns the mailbox object until the completion of mailbox
4699 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4700 * return codes the caller owns the mailbox command after the return of
4701 * the function.
4702 **/
James Smart3772a992009-05-22 14:50:54 -04004703static int
4704lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4705 uint32_t flag)
dea31012005-04-17 16:05:31 -05004706{
dea31012005-04-17 16:05:31 -05004707 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004708 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004709 uint32_t status, evtctr;
4710 uint32_t ha_copy;
4711 int i;
James Smart09372822008-01-11 01:52:54 -05004712 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004713 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004714 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004715 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004716 int processing_queue = 0;
4717
4718 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4719 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004720 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004721 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004722 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4723 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4724 return MBX_SUCCESS;
4725 }
James Smart58da1ff2008-04-07 10:15:56 -04004726 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004727 pmbox = lpfc_mbox_get(phba);
4728 if (!pmbox) {
4729 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4730 return MBX_SUCCESS;
4731 }
4732 }
dea31012005-04-17 16:05:31 -05004733
James Smarted957682007-06-17 19:56:37 -05004734 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004735 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004736 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004737 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004738 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004739 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004740 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004741 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004742 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004743 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004744 }
4745 }
4746
Linas Vepstas8d63f372007-02-14 14:28:36 -06004747 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004748 if (unlikely(pci_channel_offline(phba->pcidev))) {
4749 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4750 goto out_not_finished;
4751 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004752
James Smarta257bf92009-04-06 18:48:10 -04004753 /* If HBA has a deferred error attention, fail the iocb. */
4754 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4755 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4756 goto out_not_finished;
4757 }
4758
dea31012005-04-17 16:05:31 -05004759 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004760
James Smart3772a992009-05-22 14:50:54 -04004761 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004762 status = MBX_SUCCESS;
4763
James Smart2e0fef82007-06-17 19:56:36 -05004764 if (phba->link_state == LPFC_HBA_ERROR) {
4765 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004766
4767 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004768 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4769 "(%d):0311 Mailbox command x%x cannot "
4770 "issue Data: x%x x%x\n",
4771 pmbox->vport ? pmbox->vport->vpi : 0,
4772 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004773 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004774 }
4775
James Smart92908312006-03-07 15:04:13 -05004776 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4777 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004778 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004779 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4780 "(%d):2528 Mailbox command x%x cannot "
4781 "issue Data: x%x x%x\n",
4782 pmbox->vport ? pmbox->vport->vpi : 0,
4783 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004784 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004785 }
4786
dea31012005-04-17 16:05:31 -05004787 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4788 /* Polling for a mbox command when another one is already active
4789 * is not allowed in SLI. Also, the driver must have established
4790 * SLI2 mode to queue and process multiple mbox commands.
4791 */
4792
4793 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004794 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004795
4796 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004797 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4798 "(%d):2529 Mailbox command x%x "
4799 "cannot issue Data: x%x x%x\n",
4800 pmbox->vport ? pmbox->vport->vpi : 0,
4801 pmbox->u.mb.mbxCommand,
4802 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004803 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004804 }
4805
James Smart3772a992009-05-22 14:50:54 -04004806 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004807 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004808 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004809 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4810 "(%d):2530 Mailbox command x%x "
4811 "cannot issue Data: x%x x%x\n",
4812 pmbox->vport ? pmbox->vport->vpi : 0,
4813 pmbox->u.mb.mbxCommand,
4814 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004815 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004816 }
4817
dea31012005-04-17 16:05:31 -05004818 /* Another mailbox command is still being processed, queue this
4819 * command to be processed later.
4820 */
4821 lpfc_mbox_put(phba, pmbox);
4822
4823 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004824 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004825 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004826 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004827 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4828 mb->mbxCommand, phba->pport->port_state,
4829 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004830
4831 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004832 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004833
James Smart858c9f62007-06-17 19:56:39 -05004834 if (pmbox->vport) {
4835 lpfc_debugfs_disc_trc(pmbox->vport,
4836 LPFC_DISC_TRC_MBOX_VPORT,
4837 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4838 (uint32_t)mb->mbxCommand,
4839 mb->un.varWords[0], mb->un.varWords[1]);
4840 }
4841 else {
4842 lpfc_debugfs_disc_trc(phba->pport,
4843 LPFC_DISC_TRC_MBOX,
4844 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4845 (uint32_t)mb->mbxCommand,
4846 mb->un.varWords[0], mb->un.varWords[1]);
4847 }
4848
James Smart2e0fef82007-06-17 19:56:36 -05004849 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004850 }
4851
dea31012005-04-17 16:05:31 -05004852 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4853
4854 /* If we are not polling, we MUST be in SLI2 mode */
4855 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004856 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004857 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004858 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004859 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004860 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004861 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4862 "(%d):2531 Mailbox command x%x "
4863 "cannot issue Data: x%x x%x\n",
4864 pmbox->vport ? pmbox->vport->vpi : 0,
4865 pmbox->u.mb.mbxCommand,
4866 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004867 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004868 }
4869 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004870 mod_timer(&psli->mbox_tmo, (jiffies +
4871 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004872 }
4873
4874 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004875 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004876 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004877 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004878 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004879 mb->mbxCommand, phba->pport->port_state,
4880 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004881
James Smart858c9f62007-06-17 19:56:39 -05004882 if (mb->mbxCommand != MBX_HEARTBEAT) {
4883 if (pmbox->vport) {
4884 lpfc_debugfs_disc_trc(pmbox->vport,
4885 LPFC_DISC_TRC_MBOX_VPORT,
4886 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4887 (uint32_t)mb->mbxCommand,
4888 mb->un.varWords[0], mb->un.varWords[1]);
4889 }
4890 else {
4891 lpfc_debugfs_disc_trc(phba->pport,
4892 LPFC_DISC_TRC_MBOX,
4893 "MBOX Send: cmd:x%x mb:x%x x%x",
4894 (uint32_t)mb->mbxCommand,
4895 mb->un.varWords[0], mb->un.varWords[1]);
4896 }
4897 }
4898
dea31012005-04-17 16:05:31 -05004899 psli->slistat.mbox_cmd++;
4900 evtctr = psli->slistat.mbox_event;
4901
4902 /* next set own bit for the adapter and copy over command word */
4903 mb->mbxOwner = OWN_CHIP;
4904
James Smart3772a992009-05-22 14:50:54 -04004905 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004906 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004907 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004908 } else {
James Smart92908312006-03-07 15:04:13 -05004909 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004910 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004911 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004912 }
4913
4914 /* First copy mbox command data to HBA SLIM, skip past first
4915 word */
4916 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4917 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4918 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4919
4920 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004921 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004922 to_slim = phba->MBslimaddr;
4923 writel(ldata, to_slim);
4924 readl(to_slim); /* flush */
4925
4926 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4927 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004928 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004929 }
4930 }
4931
4932 wmb();
dea31012005-04-17 16:05:31 -05004933
4934 switch (flag) {
4935 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004936 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004937 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004938 /* Interrupt board to do it */
4939 writel(CA_MBATT, phba->CAregaddr);
4940 readl(phba->CAregaddr); /* flush */
4941 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004942 break;
4943
4944 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004945 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004946 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004947 /* Interrupt board to do it */
4948 writel(CA_MBATT, phba->CAregaddr);
4949 readl(phba->CAregaddr); /* flush */
4950
James Smart3772a992009-05-22 14:50:54 -04004951 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004952 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004953 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004954 word0 = le32_to_cpu(word0);
4955 } else {
4956 /* First read mbox status word */
4957 word0 = readl(phba->MBslimaddr);
4958 }
4959
4960 /* Read the HBA Host Attention Register */
4961 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004962 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4963 mb->mbxCommand) *
4964 1000) + jiffies;
4965 i = 0;
dea31012005-04-17 16:05:31 -05004966 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004967 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4968 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004969 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004970 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004971 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004972 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004973 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004974 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004975 }
4976
4977 /* Check if we took a mbox interrupt while we were
4978 polling */
4979 if (((word0 & OWN_CHIP) != OWN_CHIP)
4980 && (evtctr != psli->slistat.mbox_event))
4981 break;
4982
James Smart09372822008-01-11 01:52:54 -05004983 if (i++ > 10) {
4984 spin_unlock_irqrestore(&phba->hbalock,
4985 drvr_flag);
4986 msleep(1);
4987 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4988 }
dea31012005-04-17 16:05:31 -05004989
James Smart3772a992009-05-22 14:50:54 -04004990 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004991 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004992 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004993 word0 = le32_to_cpu(word0);
4994 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4995 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004996 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004997 /* Check real SLIM for any errors */
4998 slimword0 = readl(phba->MBslimaddr);
4999 slimmb = (MAILBOX_t *) & slimword0;
5000 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5001 && slimmb->mbxStatus) {
5002 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005003 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005004 word0 = slimword0;
5005 }
5006 }
5007 } else {
5008 /* First copy command data */
5009 word0 = readl(phba->MBslimaddr);
5010 }
5011 /* Read the HBA Host Attention Register */
5012 ha_copy = readl(phba->HAregaddr);
5013 }
5014
James Smart3772a992009-05-22 14:50:54 -04005015 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005016 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005017 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005018 } else {
5019 /* First copy command data */
5020 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5021 MAILBOX_CMD_SIZE);
5022 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5023 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05005024 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05005025 phba->MBslimaddr + DMP_RSP_OFFSET,
5026 mb->un.varDmp.word_cnt);
5027 }
5028 }
5029
5030 writel(HA_MBATT, phba->HAregaddr);
5031 readl(phba->HAregaddr); /* flush */
5032
5033 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5034 status = mb->mbxStatus;
5035 }
5036
James Smart2e0fef82007-06-17 19:56:36 -05005037 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5038 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005039
5040out_not_finished:
5041 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005042 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005043 lpfc_mbox_cmpl_put(phba, pmbox);
5044 }
5045 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005046}
5047
James Smarte59058c2008-08-24 21:49:00 -04005048/**
James Smartf1126682009-06-10 17:22:44 -04005049 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5050 * @phba: Pointer to HBA context object.
5051 *
5052 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5053 * the driver internal pending mailbox queue. It will then try to wait out the
5054 * possible outstanding mailbox command before return.
5055 *
5056 * Returns:
5057 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5058 * the outstanding mailbox command timed out.
5059 **/
5060static int
5061lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5062{
5063 struct lpfc_sli *psli = &phba->sli;
5064 uint8_t actcmd = MBX_HEARTBEAT;
5065 int rc = 0;
5066 unsigned long timeout;
5067
5068 /* Mark the asynchronous mailbox command posting as blocked */
5069 spin_lock_irq(&phba->hbalock);
5070 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5071 if (phba->sli.mbox_active)
5072 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5073 spin_unlock_irq(&phba->hbalock);
5074 /* Determine how long we might wait for the active mailbox
5075 * command to be gracefully completed by firmware.
5076 */
5077 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5078 jiffies;
5079 /* Wait for the outstnading mailbox command to complete */
5080 while (phba->sli.mbox_active) {
5081 /* Check active mailbox complete status every 2ms */
5082 msleep(2);
5083 if (time_after(jiffies, timeout)) {
5084 /* Timeout, marked the outstanding cmd not complete */
5085 rc = 1;
5086 break;
5087 }
5088 }
5089
5090 /* Can not cleanly block async mailbox command, fails it */
5091 if (rc) {
5092 spin_lock_irq(&phba->hbalock);
5093 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5094 spin_unlock_irq(&phba->hbalock);
5095 }
5096 return rc;
5097}
5098
5099/**
5100 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5101 * @phba: Pointer to HBA context object.
5102 *
5103 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5104 * commands from the driver internal pending mailbox queue. It makes sure
5105 * that there is no outstanding mailbox command before resuming posting
5106 * asynchronous mailbox commands. If, for any reason, there is outstanding
5107 * mailbox command, it will try to wait it out before resuming asynchronous
5108 * mailbox command posting.
5109 **/
5110static void
5111lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5112{
5113 struct lpfc_sli *psli = &phba->sli;
5114
5115 spin_lock_irq(&phba->hbalock);
5116 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5117 /* Asynchronous mailbox posting is not blocked, do nothing */
5118 spin_unlock_irq(&phba->hbalock);
5119 return;
5120 }
5121
5122 /* Outstanding synchronous mailbox command is guaranteed to be done,
5123 * successful or timeout, after timing-out the outstanding mailbox
5124 * command shall always be removed, so just unblock posting async
5125 * mailbox command and resume
5126 */
5127 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5128 spin_unlock_irq(&phba->hbalock);
5129
5130 /* wake up worker thread to post asynchronlous mailbox command */
5131 lpfc_worker_wake_up(phba);
5132}
5133
5134/**
James Smartda0436e2009-05-22 14:51:39 -04005135 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5136 * @phba: Pointer to HBA context object.
5137 * @mboxq: Pointer to mailbox object.
5138 *
5139 * The function posts a mailbox to the port. The mailbox is expected
5140 * to be comletely filled in and ready for the port to operate on it.
5141 * This routine executes a synchronous completion operation on the
5142 * mailbox by polling for its completion.
5143 *
5144 * The caller must not be holding any locks when calling this routine.
5145 *
5146 * Returns:
5147 * MBX_SUCCESS - mailbox posted successfully
5148 * Any of the MBX error values.
5149 **/
5150static int
5151lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5152{
5153 int rc = MBX_SUCCESS;
5154 unsigned long iflag;
5155 uint32_t db_ready;
5156 uint32_t mcqe_status;
5157 uint32_t mbx_cmnd;
5158 unsigned long timeout;
5159 struct lpfc_sli *psli = &phba->sli;
5160 struct lpfc_mqe *mb = &mboxq->u.mqe;
5161 struct lpfc_bmbx_create *mbox_rgn;
5162 struct dma_address *dma_address;
5163 struct lpfc_register bmbx_reg;
5164
5165 /*
5166 * Only one mailbox can be active to the bootstrap mailbox region
5167 * at a time and there is no queueing provided.
5168 */
5169 spin_lock_irqsave(&phba->hbalock, iflag);
5170 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5171 spin_unlock_irqrestore(&phba->hbalock, iflag);
5172 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5173 "(%d):2532 Mailbox command x%x (x%x) "
5174 "cannot issue Data: x%x x%x\n",
5175 mboxq->vport ? mboxq->vport->vpi : 0,
5176 mboxq->u.mb.mbxCommand,
5177 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5178 psli->sli_flag, MBX_POLL);
5179 return MBXERR_ERROR;
5180 }
5181 /* The server grabs the token and owns it until release */
5182 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5183 phba->sli.mbox_active = mboxq;
5184 spin_unlock_irqrestore(&phba->hbalock, iflag);
5185
5186 /*
5187 * Initialize the bootstrap memory region to avoid stale data areas
5188 * in the mailbox post. Then copy the caller's mailbox contents to
5189 * the bmbx mailbox region.
5190 */
5191 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5192 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5193 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5194 sizeof(struct lpfc_mqe));
5195
5196 /* Post the high mailbox dma address to the port and wait for ready. */
5197 dma_address = &phba->sli4_hba.bmbx.dma_address;
5198 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5199
5200 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5201 * 1000) + jiffies;
5202 do {
5203 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5204 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5205 if (!db_ready)
5206 msleep(2);
5207
5208 if (time_after(jiffies, timeout)) {
5209 rc = MBXERR_ERROR;
5210 goto exit;
5211 }
5212 } while (!db_ready);
5213
5214 /* Post the low mailbox dma address to the port. */
5215 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5216 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5217 * 1000) + jiffies;
5218 do {
5219 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5220 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5221 if (!db_ready)
5222 msleep(2);
5223
5224 if (time_after(jiffies, timeout)) {
5225 rc = MBXERR_ERROR;
5226 goto exit;
5227 }
5228 } while (!db_ready);
5229
5230 /*
5231 * Read the CQ to ensure the mailbox has completed.
5232 * If so, update the mailbox status so that the upper layers
5233 * can complete the request normally.
5234 */
5235 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5236 sizeof(struct lpfc_mqe));
5237 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5238 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5239 sizeof(struct lpfc_mcqe));
5240 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5241
5242 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5243 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5244 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5245 rc = MBXERR_ERROR;
5246 }
5247
5248 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5249 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5250 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5251 " x%x x%x CQ: x%x x%x x%x x%x\n",
5252 mboxq->vport ? mboxq->vport->vpi : 0,
5253 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5254 bf_get(lpfc_mqe_status, mb),
5255 mb->un.mb_words[0], mb->un.mb_words[1],
5256 mb->un.mb_words[2], mb->un.mb_words[3],
5257 mb->un.mb_words[4], mb->un.mb_words[5],
5258 mb->un.mb_words[6], mb->un.mb_words[7],
5259 mb->un.mb_words[8], mb->un.mb_words[9],
5260 mb->un.mb_words[10], mb->un.mb_words[11],
5261 mb->un.mb_words[12], mboxq->mcqe.word0,
5262 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5263 mboxq->mcqe.trailer);
5264exit:
5265 /* We are holding the token, no needed for lock when release */
5266 spin_lock_irqsave(&phba->hbalock, iflag);
5267 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5268 phba->sli.mbox_active = NULL;
5269 spin_unlock_irqrestore(&phba->hbalock, iflag);
5270 return rc;
5271}
5272
5273/**
5274 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5275 * @phba: Pointer to HBA context object.
5276 * @pmbox: Pointer to mailbox object.
5277 * @flag: Flag indicating how the mailbox need to be processed.
5278 *
5279 * This function is called by discovery code and HBA management code to submit
5280 * a mailbox command to firmware with SLI-4 interface spec.
5281 *
5282 * Return codes the caller owns the mailbox command after the return of the
5283 * function.
5284 **/
5285static int
5286lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5287 uint32_t flag)
5288{
5289 struct lpfc_sli *psli = &phba->sli;
5290 unsigned long iflags;
5291 int rc;
5292
James Smart8fa38512009-07-19 10:01:03 -04005293 rc = lpfc_mbox_dev_check(phba);
5294 if (unlikely(rc)) {
5295 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5296 "(%d):2544 Mailbox command x%x (x%x) "
5297 "cannot issue Data: x%x x%x\n",
5298 mboxq->vport ? mboxq->vport->vpi : 0,
5299 mboxq->u.mb.mbxCommand,
5300 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5301 psli->sli_flag, flag);
5302 goto out_not_finished;
5303 }
5304
James Smartda0436e2009-05-22 14:51:39 -04005305 /* Detect polling mode and jump to a handler */
5306 if (!phba->sli4_hba.intr_enable) {
5307 if (flag == MBX_POLL)
5308 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5309 else
5310 rc = -EIO;
5311 if (rc != MBX_SUCCESS)
5312 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5313 "(%d):2541 Mailbox command x%x "
5314 "(x%x) cannot issue Data: x%x x%x\n",
5315 mboxq->vport ? mboxq->vport->vpi : 0,
5316 mboxq->u.mb.mbxCommand,
5317 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5318 psli->sli_flag, flag);
5319 return rc;
5320 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005321 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5322 "(%d):2542 Try to issue mailbox command "
5323 "x%x (x%x) synchronously ahead of async"
5324 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005325 mboxq->vport ? mboxq->vport->vpi : 0,
5326 mboxq->u.mb.mbxCommand,
5327 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5328 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005329 /* Try to block the asynchronous mailbox posting */
5330 rc = lpfc_sli4_async_mbox_block(phba);
5331 if (!rc) {
5332 /* Successfully blocked, now issue sync mbox cmd */
5333 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5334 if (rc != MBX_SUCCESS)
5335 lpfc_printf_log(phba, KERN_ERR,
5336 LOG_MBOX | LOG_SLI,
5337 "(%d):2597 Mailbox command "
5338 "x%x (x%x) cannot issue "
5339 "Data: x%x x%x\n",
5340 mboxq->vport ?
5341 mboxq->vport->vpi : 0,
5342 mboxq->u.mb.mbxCommand,
5343 lpfc_sli4_mbox_opcode_get(phba,
5344 mboxq),
5345 psli->sli_flag, flag);
5346 /* Unblock the async mailbox posting afterward */
5347 lpfc_sli4_async_mbox_unblock(phba);
5348 }
5349 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005350 }
5351
5352 /* Now, interrupt mode asynchrous mailbox command */
5353 rc = lpfc_mbox_cmd_check(phba, mboxq);
5354 if (rc) {
5355 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5356 "(%d):2543 Mailbox command x%x (x%x) "
5357 "cannot issue Data: x%x x%x\n",
5358 mboxq->vport ? mboxq->vport->vpi : 0,
5359 mboxq->u.mb.mbxCommand,
5360 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5361 psli->sli_flag, flag);
5362 goto out_not_finished;
5363 }
James Smartda0436e2009-05-22 14:51:39 -04005364
5365 /* Put the mailbox command to the driver internal FIFO */
5366 psli->slistat.mbox_busy++;
5367 spin_lock_irqsave(&phba->hbalock, iflags);
5368 lpfc_mbox_put(phba, mboxq);
5369 spin_unlock_irqrestore(&phba->hbalock, iflags);
5370 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5371 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5372 "x%x (x%x) x%x x%x x%x\n",
5373 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5374 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5375 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5376 phba->pport->port_state,
5377 psli->sli_flag, MBX_NOWAIT);
5378 /* Wake up worker thread to transport mailbox command from head */
5379 lpfc_worker_wake_up(phba);
5380
5381 return MBX_BUSY;
5382
5383out_not_finished:
5384 return MBX_NOT_FINISHED;
5385}
5386
5387/**
5388 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5389 * @phba: Pointer to HBA context object.
5390 *
5391 * This function is called by worker thread to send a mailbox command to
5392 * SLI4 HBA firmware.
5393 *
5394 **/
5395int
5396lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5397{
5398 struct lpfc_sli *psli = &phba->sli;
5399 LPFC_MBOXQ_t *mboxq;
5400 int rc = MBX_SUCCESS;
5401 unsigned long iflags;
5402 struct lpfc_mqe *mqe;
5403 uint32_t mbx_cmnd;
5404
5405 /* Check interrupt mode before post async mailbox command */
5406 if (unlikely(!phba->sli4_hba.intr_enable))
5407 return MBX_NOT_FINISHED;
5408
5409 /* Check for mailbox command service token */
5410 spin_lock_irqsave(&phba->hbalock, iflags);
5411 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5412 spin_unlock_irqrestore(&phba->hbalock, iflags);
5413 return MBX_NOT_FINISHED;
5414 }
5415 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5416 spin_unlock_irqrestore(&phba->hbalock, iflags);
5417 return MBX_NOT_FINISHED;
5418 }
5419 if (unlikely(phba->sli.mbox_active)) {
5420 spin_unlock_irqrestore(&phba->hbalock, iflags);
5421 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5422 "0384 There is pending active mailbox cmd\n");
5423 return MBX_NOT_FINISHED;
5424 }
5425 /* Take the mailbox command service token */
5426 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5427
5428 /* Get the next mailbox command from head of queue */
5429 mboxq = lpfc_mbox_get(phba);
5430
5431 /* If no more mailbox command waiting for post, we're done */
5432 if (!mboxq) {
5433 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5434 spin_unlock_irqrestore(&phba->hbalock, iflags);
5435 return MBX_SUCCESS;
5436 }
5437 phba->sli.mbox_active = mboxq;
5438 spin_unlock_irqrestore(&phba->hbalock, iflags);
5439
5440 /* Check device readiness for posting mailbox command */
5441 rc = lpfc_mbox_dev_check(phba);
5442 if (unlikely(rc))
5443 /* Driver clean routine will clean up pending mailbox */
5444 goto out_not_finished;
5445
5446 /* Prepare the mbox command to be posted */
5447 mqe = &mboxq->u.mqe;
5448 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5449
5450 /* Start timer for the mbox_tmo and log some mailbox post messages */
5451 mod_timer(&psli->mbox_tmo, (jiffies +
5452 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5453
5454 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5455 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5456 "x%x x%x\n",
5457 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5458 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5459 phba->pport->port_state, psli->sli_flag);
5460
5461 if (mbx_cmnd != MBX_HEARTBEAT) {
5462 if (mboxq->vport) {
5463 lpfc_debugfs_disc_trc(mboxq->vport,
5464 LPFC_DISC_TRC_MBOX_VPORT,
5465 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5466 mbx_cmnd, mqe->un.mb_words[0],
5467 mqe->un.mb_words[1]);
5468 } else {
5469 lpfc_debugfs_disc_trc(phba->pport,
5470 LPFC_DISC_TRC_MBOX,
5471 "MBOX Send: cmd:x%x mb:x%x x%x",
5472 mbx_cmnd, mqe->un.mb_words[0],
5473 mqe->un.mb_words[1]);
5474 }
5475 }
5476 psli->slistat.mbox_cmd++;
5477
5478 /* Post the mailbox command to the port */
5479 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5480 if (rc != MBX_SUCCESS) {
5481 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5482 "(%d):2533 Mailbox command x%x (x%x) "
5483 "cannot issue Data: x%x x%x\n",
5484 mboxq->vport ? mboxq->vport->vpi : 0,
5485 mboxq->u.mb.mbxCommand,
5486 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5487 psli->sli_flag, MBX_NOWAIT);
5488 goto out_not_finished;
5489 }
5490
5491 return rc;
5492
5493out_not_finished:
5494 spin_lock_irqsave(&phba->hbalock, iflags);
5495 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5496 __lpfc_mbox_cmpl_put(phba, mboxq);
5497 /* Release the token */
5498 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5499 phba->sli.mbox_active = NULL;
5500 spin_unlock_irqrestore(&phba->hbalock, iflags);
5501
5502 return MBX_NOT_FINISHED;
5503}
5504
5505/**
5506 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5507 * @phba: Pointer to HBA context object.
5508 * @pmbox: Pointer to mailbox object.
5509 * @flag: Flag indicating how the mailbox need to be processed.
5510 *
5511 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5512 * the API jump table function pointer from the lpfc_hba struct.
5513 *
5514 * Return codes the caller owns the mailbox command after the return of the
5515 * function.
5516 **/
5517int
5518lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5519{
5520 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5521}
5522
5523/**
5524 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5525 * @phba: The hba struct for which this call is being executed.
5526 * @dev_grp: The HBA PCI-Device group number.
5527 *
5528 * This routine sets up the mbox interface API function jump table in @phba
5529 * struct.
5530 * Returns: 0 - success, -ENODEV - failure.
5531 **/
5532int
5533lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5534{
5535
5536 switch (dev_grp) {
5537 case LPFC_PCI_DEV_LP:
5538 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5539 phba->lpfc_sli_handle_slow_ring_event =
5540 lpfc_sli_handle_slow_ring_event_s3;
5541 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5542 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5543 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5544 break;
5545 case LPFC_PCI_DEV_OC:
5546 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5547 phba->lpfc_sli_handle_slow_ring_event =
5548 lpfc_sli_handle_slow_ring_event_s4;
5549 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5550 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5551 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5552 break;
5553 default:
5554 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5555 "1420 Invalid HBA PCI-device group: 0x%x\n",
5556 dev_grp);
5557 return -ENODEV;
5558 break;
5559 }
5560 return 0;
5561}
5562
5563/**
James Smart3621a712009-04-06 18:47:14 -04005564 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005565 * @phba: Pointer to HBA context object.
5566 * @pring: Pointer to driver SLI ring object.
5567 * @piocb: Pointer to address of newly added command iocb.
5568 *
5569 * This function is called with hbalock held to add a command
5570 * iocb to the txq when SLI layer cannot submit the command iocb
5571 * to the ring.
5572 **/
James Smart858c9f62007-06-17 19:56:39 -05005573static void
James Smart92d7f7b2007-06-17 19:56:38 -05005574__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005575 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005576{
5577 /* Insert the caller's iocb in the txq tail for later processing. */
5578 list_add_tail(&piocb->list, &pring->txq);
5579 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005580}
5581
James Smarte59058c2008-08-24 21:49:00 -04005582/**
James Smart3621a712009-04-06 18:47:14 -04005583 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005584 * @phba: Pointer to HBA context object.
5585 * @pring: Pointer to driver SLI ring object.
5586 * @piocb: Pointer to address of newly added command iocb.
5587 *
5588 * This function is called with hbalock held before a new
5589 * iocb is submitted to the firmware. This function checks
5590 * txq to flush the iocbs in txq to Firmware before
5591 * submitting new iocbs to the Firmware.
5592 * If there are iocbs in the txq which need to be submitted
5593 * to firmware, lpfc_sli_next_iocb returns the first element
5594 * of the txq after dequeuing it from txq.
5595 * If there is no iocb in the txq then the function will return
5596 * *piocb and *piocb is set to NULL. Caller needs to check
5597 * *piocb to find if there are more commands in the txq.
5598 **/
dea31012005-04-17 16:05:31 -05005599static struct lpfc_iocbq *
5600lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005601 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005602{
5603 struct lpfc_iocbq * nextiocb;
5604
5605 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5606 if (!nextiocb) {
5607 nextiocb = *piocb;
5608 *piocb = NULL;
5609 }
5610
5611 return nextiocb;
5612}
5613
James Smarte59058c2008-08-24 21:49:00 -04005614/**
James Smart3772a992009-05-22 14:50:54 -04005615 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005616 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005617 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005618 * @piocb: Pointer to command iocb.
5619 * @flag: Flag indicating if this command can be put into txq.
5620 *
James Smart3772a992009-05-22 14:50:54 -04005621 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5622 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5623 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5624 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5625 * this function allows only iocbs for posting buffers. This function finds
5626 * next available slot in the command ring and posts the command to the
5627 * available slot and writes the port attention register to request HBA start
5628 * processing new iocb. If there is no slot available in the ring and
5629 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5630 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005631 *
James Smart3772a992009-05-22 14:50:54 -04005632 * This function is called with hbalock held. The function will return success
5633 * after it successfully submit the iocb to firmware or after adding to the
5634 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005635 **/
James Smart98c9ea52007-10-27 13:37:33 -04005636static int
James Smart3772a992009-05-22 14:50:54 -04005637__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005638 struct lpfc_iocbq *piocb, uint32_t flag)
5639{
5640 struct lpfc_iocbq *nextiocb;
5641 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005642 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005643
James Smart92d7f7b2007-06-17 19:56:38 -05005644 if (piocb->iocb_cmpl && (!piocb->vport) &&
5645 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5646 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5647 lpfc_printf_log(phba, KERN_ERR,
5648 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005649 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005650 piocb->iocb.ulpCommand);
5651 dump_stack();
5652 return IOCB_ERROR;
5653 }
5654
5655
Linas Vepstas8d63f372007-02-14 14:28:36 -06005656 /* If the PCI channel is in offline state, do not post iocbs. */
5657 if (unlikely(pci_channel_offline(phba->pcidev)))
5658 return IOCB_ERROR;
5659
James Smarta257bf92009-04-06 18:48:10 -04005660 /* If HBA has a deferred error attention, fail the iocb. */
5661 if (unlikely(phba->hba_flag & DEFER_ERATT))
5662 return IOCB_ERROR;
5663
dea31012005-04-17 16:05:31 -05005664 /*
5665 * We should never get an IOCB if we are in a < LINK_DOWN state
5666 */
James Smart2e0fef82007-06-17 19:56:36 -05005667 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005668 return IOCB_ERROR;
5669
5670 /*
5671 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005672 * outstanding event.
dea31012005-04-17 16:05:31 -05005673 */
James Smart0b727fe2007-10-27 13:37:25 -04005674 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005675 goto iocb_busy;
5676
James Smart2e0fef82007-06-17 19:56:36 -05005677 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005678 /*
James Smart2680eea2007-04-25 09:52:55 -04005679 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005680 * can be issued if the link is not up.
5681 */
5682 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005683 case CMD_GEN_REQUEST64_CR:
5684 case CMD_GEN_REQUEST64_CX:
5685 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5686 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5687 FC_FCP_CMND) ||
5688 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5689 MENLO_TRANSPORT_TYPE))
5690
5691 goto iocb_busy;
5692 break;
dea31012005-04-17 16:05:31 -05005693 case CMD_QUE_RING_BUF_CN:
5694 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005695 /*
5696 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5697 * completion, iocb_cmpl MUST be 0.
5698 */
5699 if (piocb->iocb_cmpl)
5700 piocb->iocb_cmpl = NULL;
5701 /*FALLTHROUGH*/
5702 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005703 case CMD_CLOSE_XRI_CN:
5704 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005705 break;
5706 default:
5707 goto iocb_busy;
5708 }
5709
5710 /*
5711 * For FCP commands, we must be in a state where we can process link
5712 * attention events.
5713 */
5714 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005715 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005716 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005717 }
dea31012005-04-17 16:05:31 -05005718
dea31012005-04-17 16:05:31 -05005719 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5720 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5721 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5722
5723 if (iocb)
5724 lpfc_sli_update_ring(phba, pring);
5725 else
5726 lpfc_sli_update_full_ring(phba, pring);
5727
5728 if (!piocb)
5729 return IOCB_SUCCESS;
5730
5731 goto out_busy;
5732
5733 iocb_busy:
5734 pring->stats.iocb_cmd_delay++;
5735
5736 out_busy:
5737
5738 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005739 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005740 return IOCB_SUCCESS;
5741 }
5742
5743 return IOCB_BUSY;
5744}
5745
James Smart3772a992009-05-22 14:50:54 -04005746/**
James Smart4f774512009-05-22 14:52:35 -04005747 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5748 * @phba: Pointer to HBA context object.
5749 * @piocb: Pointer to command iocb.
5750 * @sglq: Pointer to the scatter gather queue object.
5751 *
5752 * This routine converts the bpl or bde that is in the IOCB
5753 * to a sgl list for the sli4 hardware. The physical address
5754 * of the bpl/bde is converted back to a virtual address.
5755 * If the IOCB contains a BPL then the list of BDE's is
5756 * converted to sli4_sge's. If the IOCB contains a single
5757 * BDE then it is converted to a single sli_sge.
5758 * The IOCB is still in cpu endianess so the contents of
5759 * the bpl can be used without byte swapping.
5760 *
5761 * Returns valid XRI = Success, NO_XRI = Failure.
5762**/
5763static uint16_t
5764lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5765 struct lpfc_sglq *sglq)
5766{
5767 uint16_t xritag = NO_XRI;
5768 struct ulp_bde64 *bpl = NULL;
5769 struct ulp_bde64 bde;
5770 struct sli4_sge *sgl = NULL;
5771 IOCB_t *icmd;
5772 int numBdes = 0;
5773 int i = 0;
5774
5775 if (!piocbq || !sglq)
5776 return xritag;
5777
5778 sgl = (struct sli4_sge *)sglq->sgl;
5779 icmd = &piocbq->iocb;
5780 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5781 numBdes = icmd->un.genreq64.bdl.bdeSize /
5782 sizeof(struct ulp_bde64);
5783 /* The addrHigh and addrLow fields within the IOCB
5784 * have not been byteswapped yet so there is no
5785 * need to swap them back.
5786 */
5787 bpl = (struct ulp_bde64 *)
5788 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5789
5790 if (!bpl)
5791 return xritag;
5792
5793 for (i = 0; i < numBdes; i++) {
5794 /* Should already be byte swapped. */
5795 sgl->addr_hi = bpl->addrHigh;
5796 sgl->addr_lo = bpl->addrLow;
5797 /* swap the size field back to the cpu so we
5798 * can assign it to the sgl.
5799 */
5800 bde.tus.w = le32_to_cpu(bpl->tus.w);
5801 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5802 if ((i+1) == numBdes)
5803 bf_set(lpfc_sli4_sge_last, sgl, 1);
5804 else
5805 bf_set(lpfc_sli4_sge_last, sgl, 0);
5806 sgl->word2 = cpu_to_le32(sgl->word2);
5807 sgl->word3 = cpu_to_le32(sgl->word3);
5808 bpl++;
5809 sgl++;
5810 }
5811 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5812 /* The addrHigh and addrLow fields of the BDE have not
5813 * been byteswapped yet so they need to be swapped
5814 * before putting them in the sgl.
5815 */
5816 sgl->addr_hi =
5817 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5818 sgl->addr_lo =
5819 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5820 bf_set(lpfc_sli4_sge_len, sgl,
5821 icmd->un.genreq64.bdl.bdeSize);
5822 bf_set(lpfc_sli4_sge_last, sgl, 1);
5823 sgl->word2 = cpu_to_le32(sgl->word2);
5824 sgl->word3 = cpu_to_le32(sgl->word3);
5825 }
5826 return sglq->sli4_xritag;
5827}
5828
5829/**
5830 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5831 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005832 *
5833 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005834 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5835 * held.
James Smart4f774512009-05-22 14:52:35 -04005836 *
5837 * Return: index into SLI4 fast-path FCP queue index.
5838 **/
5839static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005840lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005841{
James Smart8fa38512009-07-19 10:01:03 -04005842 ++phba->fcp_qidx;
5843 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5844 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005845
James Smart8fa38512009-07-19 10:01:03 -04005846 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005847}
5848
5849/**
5850 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5851 * @phba: Pointer to HBA context object.
5852 * @piocb: Pointer to command iocb.
5853 * @wqe: Pointer to the work queue entry.
5854 *
5855 * This routine converts the iocb command to its Work Queue Entry
5856 * equivalent. The wqe pointer should not have any fields set when
5857 * this routine is called because it will memcpy over them.
5858 * This routine does not set the CQ_ID or the WQEC bits in the
5859 * wqe.
5860 *
5861 * Returns: 0 = Success, IOCB_ERROR = Failure.
5862 **/
5863static int
5864lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5865 union lpfc_wqe *wqe)
5866{
5867 uint32_t payload_len = 0;
5868 uint8_t ct = 0;
5869 uint32_t fip;
5870 uint32_t abort_tag;
5871 uint8_t command_type = ELS_COMMAND_NON_FIP;
5872 uint8_t cmnd;
5873 uint16_t xritag;
5874 struct ulp_bde64 *bpl = NULL;
5875
5876 fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
5877 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005878 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005879 command_type = FCP_COMMAND;
James Smart0c287582009-06-10 17:22:56 -04005880 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
5881 command_type = ELS_COMMAND_FIP;
5882 else
5883 command_type = ELS_COMMAND_NON_FIP;
5884
James Smart4f774512009-05-22 14:52:35 -04005885 /* Some of the fields are in the right position already */
5886 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5887 abort_tag = (uint32_t) iocbq->iotag;
5888 xritag = iocbq->sli4_xritag;
5889 wqe->words[7] = 0; /* The ct field has moved so reset */
5890 /* words0-2 bpl convert bde */
5891 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5892 bpl = (struct ulp_bde64 *)
5893 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5894 if (!bpl)
5895 return IOCB_ERROR;
5896
5897 /* Should already be byte swapped. */
5898 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5899 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5900 /* swap the size field back to the cpu so we
5901 * can assign it to the sgl.
5902 */
5903 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5904 payload_len = wqe->generic.bde.tus.f.bdeSize;
5905 } else
5906 payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5907
5908 iocbq->iocb.ulpIoTag = iocbq->iotag;
5909 cmnd = iocbq->iocb.ulpCommand;
5910
5911 switch (iocbq->iocb.ulpCommand) {
5912 case CMD_ELS_REQUEST64_CR:
5913 if (!iocbq->iocb.ulpLe) {
5914 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5915 "2007 Only Limited Edition cmd Format"
5916 " supported 0x%x\n",
5917 iocbq->iocb.ulpCommand);
5918 return IOCB_ERROR;
5919 }
5920 wqe->els_req.payload_len = payload_len;
5921 /* Els_reguest64 has a TMO */
5922 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5923 iocbq->iocb.ulpTimeout);
5924 /* Need a VF for word 4 set the vf bit*/
5925 bf_set(els_req64_vf, &wqe->els_req, 0);
5926 /* And a VFID for word 12 */
5927 bf_set(els_req64_vfid, &wqe->els_req, 0);
5928 /*
5929 * Set ct field to 3, indicates that the context_tag field
5930 * contains the FCFI and remote N_Port_ID is
5931 * in word 5.
5932 */
5933
5934 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5935 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5936 iocbq->iocb.ulpContext);
5937
James Smart4f774512009-05-22 14:52:35 -04005938 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5939 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5940 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5941 break;
5942 case CMD_XMIT_SEQUENCE64_CR:
5943 /* word3 iocb=io_tag32 wqe=payload_offset */
5944 /* payload offset used for multilpe outstanding
5945 * sequences on the same exchange
5946 */
5947 wqe->words[3] = 0;
5948 /* word4 relative_offset memcpy */
5949 /* word5 r_ctl/df_ctl memcpy */
5950 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5951 wqe->xmit_sequence.xmit_len = payload_len;
5952 break;
5953 case CMD_XMIT_BCAST64_CN:
5954 /* word3 iocb=iotag32 wqe=payload_len */
5955 wqe->words[3] = 0; /* no definition for this in wqe */
5956 /* word4 iocb=rsvd wqe=rsvd */
5957 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5958 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5959 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5960 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5961 break;
5962 case CMD_FCP_IWRITE64_CR:
5963 command_type = FCP_COMMAND_DATA_OUT;
5964 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5965 * confusing.
5966 * word3 is payload_len: byte offset to the sgl entry for the
5967 * fcp_command.
5968 * word4 is total xfer len, same as the IOCB->ulpParameter.
5969 * word5 is initial xfer len 0 = wait for xfer-ready
5970 */
5971
5972 /* Always wait for xfer-ready before sending data */
5973 wqe->fcp_iwrite.initial_xfer_len = 0;
5974 /* word 4 (xfer length) should have been set on the memcpy */
5975
5976 /* allow write to fall through to read */
5977 case CMD_FCP_IREAD64_CR:
5978 /* FCP_CMD is always the 1st sgl entry */
5979 wqe->fcp_iread.payload_len =
5980 payload_len + sizeof(struct fcp_rsp);
5981
5982 /* word 4 (xfer length) should have been set on the memcpy */
5983
5984 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5985 iocbq->iocb.ulpFCP2Rcvy);
5986 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5987 /* The XC bit and the XS bit are similar. The driver never
5988 * tracked whether or not the exchange was previouslly open.
5989 * XC = Exchange create, 0 is create. 1 is already open.
5990 * XS = link cmd: 1 do not close the exchange after command.
5991 * XS = 0 close exchange when command completes.
5992 * The only time we would not set the XC bit is when the XS bit
5993 * is set and we are sending our 2nd or greater command on
5994 * this exchange.
5995 */
James Smart4f774512009-05-22 14:52:35 -04005996 /* Always open the exchange */
5997 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
5998
5999 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6000 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006001 break;
6002 case CMD_FCP_ICMND64_CR:
6003 /* Always open the exchange */
6004 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6005
6006 wqe->words[4] = 0;
6007 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6008 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006009 break;
6010 case CMD_GEN_REQUEST64_CR:
6011 /* word3 command length is described as byte offset to the
6012 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6013 * sgl[0] = cmnd
6014 * sgl[1] = rsp.
6015 *
6016 */
6017 wqe->gen_req.command_len = payload_len;
6018 /* Word4 parameter copied in the memcpy */
6019 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6020 /* word6 context tag copied in memcpy */
6021 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6022 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6023 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6024 "2015 Invalid CT %x command 0x%x\n",
6025 ct, iocbq->iocb.ulpCommand);
6026 return IOCB_ERROR;
6027 }
6028 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6029 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6030 iocbq->iocb.ulpTimeout);
6031
6032 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6033 command_type = OTHER_COMMAND;
6034 break;
6035 case CMD_XMIT_ELS_RSP64_CX:
6036 /* words0-2 BDE memcpy */
6037 /* word3 iocb=iotag32 wqe=rsvd */
6038 wqe->words[3] = 0;
6039 /* word4 iocb=did wge=rsvd. */
6040 wqe->words[4] = 0;
6041 /* word5 iocb=rsvd wge=did */
6042 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6043 iocbq->iocb.un.elsreq64.remoteID);
6044
6045 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6046 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6047
6048 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6049 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6050 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6051 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6052 iocbq->vport->vpi + phba->vpi_base);
6053 command_type = OTHER_COMMAND;
6054 break;
6055 case CMD_CLOSE_XRI_CN:
6056 case CMD_ABORT_XRI_CN:
6057 case CMD_ABORT_XRI_CX:
6058 /* words 0-2 memcpy should be 0 rserved */
6059 /* port will send abts */
6060 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6061 /*
6062 * The link is down so the fw does not need to send abts
6063 * on the wire.
6064 */
6065 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6066 else
6067 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6068 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6069 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6070 wqe->words[5] = 0;
6071 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6072 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6073 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6074 wqe->generic.abort_tag = abort_tag;
6075 /*
6076 * The abort handler will send us CMD_ABORT_XRI_CN or
6077 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6078 */
6079 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6080 cmnd = CMD_ABORT_XRI_CX;
6081 command_type = OTHER_COMMAND;
6082 xritag = 0;
6083 break;
6084 case CMD_XRI_ABORTED_CX:
6085 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6086 /* words0-2 are all 0's no bde */
6087 /* word3 and word4 are rsvrd */
6088 wqe->words[3] = 0;
6089 wqe->words[4] = 0;
6090 /* word5 iocb=rsvd wge=did */
6091 /* There is no remote port id in the IOCB? */
6092 /* Let this fall through and fail */
6093 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6094 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6095 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6096 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6097 default:
6098 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6099 "2014 Invalid command 0x%x\n",
6100 iocbq->iocb.ulpCommand);
6101 return IOCB_ERROR;
6102 break;
6103
6104 }
6105 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6106 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6107 wqe->generic.abort_tag = abort_tag;
6108 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6109 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6110 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6111 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6112
6113 return 0;
6114}
6115
6116/**
6117 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6118 * @phba: Pointer to HBA context object.
6119 * @ring_number: SLI ring number to issue iocb on.
6120 * @piocb: Pointer to command iocb.
6121 * @flag: Flag indicating if this command can be put into txq.
6122 *
6123 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6124 * an iocb command to an HBA with SLI-4 interface spec.
6125 *
6126 * This function is called with hbalock held. The function will return success
6127 * after it successfully submit the iocb to firmware or after adding to the
6128 * txq.
6129 **/
6130static int
6131__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6132 struct lpfc_iocbq *piocb, uint32_t flag)
6133{
6134 struct lpfc_sglq *sglq;
6135 uint16_t xritag;
6136 union lpfc_wqe wqe;
6137 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6138 uint32_t fcp_wqidx;
6139
6140 if (piocb->sli4_xritag == NO_XRI) {
6141 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6142 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6143 sglq = NULL;
6144 else {
6145 sglq = __lpfc_sli_get_sglq(phba);
6146 if (!sglq)
6147 return IOCB_ERROR;
6148 piocb->sli4_xritag = sglq->sli4_xritag;
6149 }
6150 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6151 sglq = NULL; /* These IO's already have an XRI and
6152 * a mapped sgl.
6153 */
6154 } else {
6155 /* This is a continuation of a commandi,(CX) so this
6156 * sglq is on the active list
6157 */
6158 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6159 if (!sglq)
6160 return IOCB_ERROR;
6161 }
6162
6163 if (sglq) {
6164 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6165 if (xritag != sglq->sli4_xritag)
6166 return IOCB_ERROR;
6167 }
6168
6169 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6170 return IOCB_ERROR;
6171
6172 if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart8fa38512009-07-19 10:01:03 -04006173 fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart4f774512009-05-22 14:52:35 -04006174 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
6175 return IOCB_ERROR;
6176 } else {
6177 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6178 return IOCB_ERROR;
6179 }
6180 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6181
6182 return 0;
6183}
6184
6185/**
James Smart3772a992009-05-22 14:50:54 -04006186 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6187 *
6188 * This routine wraps the actual lockless version for issusing IOCB function
6189 * pointer from the lpfc_hba struct.
6190 *
6191 * Return codes:
6192 * IOCB_ERROR - Error
6193 * IOCB_SUCCESS - Success
6194 * IOCB_BUSY - Busy
6195 **/
6196static inline int
6197__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6198 struct lpfc_iocbq *piocb, uint32_t flag)
6199{
6200 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6201}
6202
6203/**
6204 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6205 * @phba: The hba struct for which this call is being executed.
6206 * @dev_grp: The HBA PCI-Device group number.
6207 *
6208 * This routine sets up the SLI interface API function jump table in @phba
6209 * struct.
6210 * Returns: 0 - success, -ENODEV - failure.
6211 **/
6212int
6213lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6214{
6215
6216 switch (dev_grp) {
6217 case LPFC_PCI_DEV_LP:
6218 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6219 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6220 break;
James Smart4f774512009-05-22 14:52:35 -04006221 case LPFC_PCI_DEV_OC:
6222 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6223 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6224 break;
James Smart3772a992009-05-22 14:50:54 -04006225 default:
6226 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6227 "1419 Invalid HBA PCI-device group: 0x%x\n",
6228 dev_grp);
6229 return -ENODEV;
6230 break;
6231 }
6232 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6233 return 0;
6234}
James Smart92d7f7b2007-06-17 19:56:38 -05006235
James Smarte59058c2008-08-24 21:49:00 -04006236/**
James Smart3621a712009-04-06 18:47:14 -04006237 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006238 * @phba: Pointer to HBA context object.
6239 * @pring: Pointer to driver SLI ring object.
6240 * @piocb: Pointer to command iocb.
6241 * @flag: Flag indicating if this command can be put into txq.
6242 *
6243 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6244 * function. This function gets the hbalock and calls
6245 * __lpfc_sli_issue_iocb function and will return the error returned
6246 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6247 * functions which do not hold hbalock.
6248 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006249int
James Smart3772a992009-05-22 14:50:54 -04006250lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006251 struct lpfc_iocbq *piocb, uint32_t flag)
6252{
6253 unsigned long iflags;
6254 int rc;
6255
6256 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006257 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006258 spin_unlock_irqrestore(&phba->hbalock, iflags);
6259
6260 return rc;
6261}
6262
James Smarte59058c2008-08-24 21:49:00 -04006263/**
James Smart3621a712009-04-06 18:47:14 -04006264 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006265 * @phba: Pointer to HBA context object.
6266 *
6267 * This function is called while driver attaches with the
6268 * HBA to setup the extra ring. The extra ring is used
6269 * only when driver needs to support target mode functionality
6270 * or IP over FC functionalities.
6271 *
6272 * This function is called with no lock held.
6273 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006274static int
6275lpfc_extra_ring_setup( struct lpfc_hba *phba)
6276{
6277 struct lpfc_sli *psli;
6278 struct lpfc_sli_ring *pring;
6279
6280 psli = &phba->sli;
6281
6282 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006283
6284 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006285 pring = &psli->ring[psli->fcp_ring];
6286 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6287 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6288 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6289 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6290
James Smarta4bc3372006-12-02 13:34:16 -05006291 /* and give them to the extra ring */
6292 pring = &psli->ring[psli->extra_ring];
6293
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006294 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6295 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6296 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6297 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6298
6299 /* Setup default profile for this ring */
6300 pring->iotag_max = 4096;
6301 pring->num_mask = 1;
6302 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006303 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6304 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006305 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6306 return 0;
6307}
6308
James Smarte59058c2008-08-24 21:49:00 -04006309/**
James Smart3621a712009-04-06 18:47:14 -04006310 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006311 * @phba: Pointer to HBA context object.
6312 * @pring: Pointer to driver SLI ring object.
6313 * @iocbq: Pointer to iocb object.
6314 *
6315 * This function is called by the slow ring event handler
6316 * function when there is an ASYNC event iocb in the ring.
6317 * This function is called with no lock held.
6318 * Currently this function handles only temperature related
6319 * ASYNC events. The function decodes the temperature sensor
6320 * event message and posts events for the management applications.
6321 **/
James Smart98c9ea52007-10-27 13:37:33 -04006322static void
James Smart57127f12007-10-27 13:37:05 -04006323lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6324 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6325{
6326 IOCB_t *icmd;
6327 uint16_t evt_code;
6328 uint16_t temp;
6329 struct temp_event temp_event_data;
6330 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006331 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006332
6333 icmd = &iocbq->iocb;
6334 evt_code = icmd->un.asyncstat.evt_code;
6335 temp = icmd->ulpContext;
6336
6337 if ((evt_code != ASYNC_TEMP_WARN) &&
6338 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006339 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006340 lpfc_printf_log(phba,
6341 KERN_ERR,
6342 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006343 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006344 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006345 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6346 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6347 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6348 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006349 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006350 icmd->un.asyncstat.evt_code,
6351 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6352 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6353 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6354 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6355
James Smart57127f12007-10-27 13:37:05 -04006356 return;
6357 }
6358 temp_event_data.data = (uint32_t)temp;
6359 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6360 if (evt_code == ASYNC_TEMP_WARN) {
6361 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6362 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006363 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006364 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006365 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006366 "corrective action. temperature : %d Celsius\n",
6367 temp);
6368 }
6369 if (evt_code == ASYNC_TEMP_SAFE) {
6370 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6371 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006372 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006373 LOG_TEMP,
6374 "0340 Adapter temperature is OK now. "
6375 "temperature : %d Celsius\n",
6376 temp);
6377 }
6378
6379 /* Send temperature change event to applications */
6380 shost = lpfc_shost_from_vport(phba->pport);
6381 fc_host_post_vendor_event(shost, fc_get_event_number(),
6382 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006383 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006384
6385}
6386
6387
James Smarte59058c2008-08-24 21:49:00 -04006388/**
James Smart3621a712009-04-06 18:47:14 -04006389 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006390 * @phba: Pointer to HBA context object.
6391 *
6392 * lpfc_sli_setup sets up rings of the SLI interface with
6393 * number of iocbs per ring and iotags. This function is
6394 * called while driver attach to the HBA and before the
6395 * interrupts are enabled. So there is no need for locking.
6396 *
6397 * This function always returns 0.
6398 **/
dea31012005-04-17 16:05:31 -05006399int
6400lpfc_sli_setup(struct lpfc_hba *phba)
6401{
James Smarted957682007-06-17 19:56:37 -05006402 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006403 struct lpfc_sli *psli = &phba->sli;
6404 struct lpfc_sli_ring *pring;
6405
6406 psli->num_rings = MAX_CONFIGURED_RINGS;
6407 psli->sli_flag = 0;
6408 psli->fcp_ring = LPFC_FCP_RING;
6409 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006410 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006411
James Bottomley604a3e32005-10-29 10:28:33 -05006412 psli->iocbq_lookup = NULL;
6413 psli->iocbq_lookup_len = 0;
6414 psli->last_iotag = 0;
6415
dea31012005-04-17 16:05:31 -05006416 for (i = 0; i < psli->num_rings; i++) {
6417 pring = &psli->ring[i];
6418 switch (i) {
6419 case LPFC_FCP_RING: /* ring 0 - FCP */
6420 /* numCiocb and numRiocb are used in config_port */
6421 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6422 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6423 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6424 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6425 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6426 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006427 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006428 SLI3_IOCB_CMD_SIZE :
6429 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006430 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006431 SLI3_IOCB_RSP_SIZE :
6432 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006433 pring->iotag_ctr = 0;
6434 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006435 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006436 pring->fast_iotag = pring->iotag_max;
6437 pring->num_mask = 0;
6438 break;
James Smarta4bc3372006-12-02 13:34:16 -05006439 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006440 /* numCiocb and numRiocb are used in config_port */
6441 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6442 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006443 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006444 SLI3_IOCB_CMD_SIZE :
6445 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006446 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006447 SLI3_IOCB_RSP_SIZE :
6448 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006449 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006450 pring->num_mask = 0;
6451 break;
6452 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6453 /* numCiocb and numRiocb are used in config_port */
6454 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6455 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006456 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006457 SLI3_IOCB_CMD_SIZE :
6458 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006459 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006460 SLI3_IOCB_RSP_SIZE :
6461 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006462 pring->fast_iotag = 0;
6463 pring->iotag_ctr = 0;
6464 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006465 pring->lpfc_sli_rcv_async_status =
6466 lpfc_sli_async_event_handler;
dea31012005-04-17 16:05:31 -05006467 pring->num_mask = 4;
6468 pring->prt[0].profile = 0; /* Mask 0 */
6469 pring->prt[0].rctl = FC_ELS_REQ;
6470 pring->prt[0].type = FC_ELS_DATA;
6471 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006472 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006473 pring->prt[1].profile = 0; /* Mask 1 */
6474 pring->prt[1].rctl = FC_ELS_RSP;
6475 pring->prt[1].type = FC_ELS_DATA;
6476 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006477 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006478 pring->prt[2].profile = 0; /* Mask 2 */
6479 /* NameServer Inquiry */
6480 pring->prt[2].rctl = FC_UNSOL_CTL;
6481 /* NameServer */
6482 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
6483 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006484 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006485 pring->prt[3].profile = 0; /* Mask 3 */
6486 /* NameServer response */
6487 pring->prt[3].rctl = FC_SOL_CTL;
6488 /* NameServer */
6489 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
6490 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006491 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006492 break;
6493 }
James Smarted957682007-06-17 19:56:37 -05006494 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006495 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006496 }
James Smarted957682007-06-17 19:56:37 -05006497 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006498 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006499 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6500 "SLI2 SLIM Data: x%x x%lx\n",
6501 phba->brd_no, totiocbsize,
6502 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006503 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006504 if (phba->cfg_multi_ring_support == 2)
6505 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006506
6507 return 0;
6508}
6509
James Smarte59058c2008-08-24 21:49:00 -04006510/**
James Smart3621a712009-04-06 18:47:14 -04006511 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006512 * @phba: Pointer to HBA context object.
6513 *
6514 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6515 * ring. This function also initializes ring indices of each ring.
6516 * This function is called during the initialization of the SLI
6517 * interface of an HBA.
6518 * This function is called with no lock held and always returns
6519 * 1.
6520 **/
dea31012005-04-17 16:05:31 -05006521int
James Smart2e0fef82007-06-17 19:56:36 -05006522lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006523{
6524 struct lpfc_sli *psli;
6525 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006526 int i;
dea31012005-04-17 16:05:31 -05006527
6528 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006529 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006530 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006531 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006532 /* Initialize list headers for txq and txcmplq as double linked lists */
6533 for (i = 0; i < psli->num_rings; i++) {
6534 pring = &psli->ring[i];
6535 pring->ringno = i;
6536 pring->next_cmdidx = 0;
6537 pring->local_getidx = 0;
6538 pring->cmdidx = 0;
6539 INIT_LIST_HEAD(&pring->txq);
6540 INIT_LIST_HEAD(&pring->txcmplq);
6541 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006542 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006543 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006544 }
James Smart2e0fef82007-06-17 19:56:36 -05006545 spin_unlock_irq(&phba->hbalock);
6546 return 1;
dea31012005-04-17 16:05:31 -05006547}
6548
James Smarte59058c2008-08-24 21:49:00 -04006549/**
James Smart04c68492009-05-22 14:52:52 -04006550 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6551 * @phba: Pointer to HBA context object.
6552 *
6553 * This routine flushes the mailbox command subsystem. It will unconditionally
6554 * flush all the mailbox commands in the three possible stages in the mailbox
6555 * command sub-system: pending mailbox command queue; the outstanding mailbox
6556 * command; and completed mailbox command queue. It is caller's responsibility
6557 * to make sure that the driver is in the proper state to flush the mailbox
6558 * command sub-system. Namely, the posting of mailbox commands into the
6559 * pending mailbox command queue from the various clients must be stopped;
6560 * either the HBA is in a state that it will never works on the outstanding
6561 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6562 * mailbox command has been completed.
6563 **/
6564static void
6565lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6566{
6567 LIST_HEAD(completions);
6568 struct lpfc_sli *psli = &phba->sli;
6569 LPFC_MBOXQ_t *pmb;
6570 unsigned long iflag;
6571
6572 /* Flush all the mailbox commands in the mbox system */
6573 spin_lock_irqsave(&phba->hbalock, iflag);
6574 /* The pending mailbox command queue */
6575 list_splice_init(&phba->sli.mboxq, &completions);
6576 /* The outstanding active mailbox command */
6577 if (psli->mbox_active) {
6578 list_add_tail(&psli->mbox_active->list, &completions);
6579 psli->mbox_active = NULL;
6580 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6581 }
6582 /* The completed mailbox command queue */
6583 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6584 spin_unlock_irqrestore(&phba->hbalock, iflag);
6585
6586 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6587 while (!list_empty(&completions)) {
6588 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6589 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6590 if (pmb->mbox_cmpl)
6591 pmb->mbox_cmpl(phba, pmb);
6592 }
6593}
6594
6595/**
James Smart3621a712009-04-06 18:47:14 -04006596 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006597 * @vport: Pointer to virtual port object.
6598 *
6599 * lpfc_sli_host_down is called to clean up the resources
6600 * associated with a vport before destroying virtual
6601 * port data structures.
6602 * This function does following operations:
6603 * - Free discovery resources associated with this virtual
6604 * port.
6605 * - Free iocbs associated with this virtual port in
6606 * the txq.
6607 * - Send abort for all iocb commands associated with this
6608 * vport in txcmplq.
6609 *
6610 * This function is called with no lock held and always returns 1.
6611 **/
dea31012005-04-17 16:05:31 -05006612int
James Smart92d7f7b2007-06-17 19:56:38 -05006613lpfc_sli_host_down(struct lpfc_vport *vport)
6614{
James Smart858c9f62007-06-17 19:56:39 -05006615 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006616 struct lpfc_hba *phba = vport->phba;
6617 struct lpfc_sli *psli = &phba->sli;
6618 struct lpfc_sli_ring *pring;
6619 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006620 int i;
6621 unsigned long flags = 0;
6622 uint16_t prev_pring_flag;
6623
6624 lpfc_cleanup_discovery_resources(vport);
6625
6626 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006627 for (i = 0; i < psli->num_rings; i++) {
6628 pring = &psli->ring[i];
6629 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006630 /* Only slow rings */
6631 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006632 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006633 /* Set the lpfc data pending flag */
6634 set_bit(LPFC_DATA_READY, &phba->data_flags);
6635 }
James Smart92d7f7b2007-06-17 19:56:38 -05006636 /*
6637 * Error everything on the txq since these iocbs have not been
6638 * given to the FW yet.
6639 */
James Smart92d7f7b2007-06-17 19:56:38 -05006640 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6641 if (iocb->vport != vport)
6642 continue;
James Smart858c9f62007-06-17 19:56:39 -05006643 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006644 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006645 }
6646
6647 /* Next issue ABTS for everything on the txcmplq */
6648 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6649 list) {
6650 if (iocb->vport != vport)
6651 continue;
6652 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6653 }
6654
6655 pring->flag = prev_pring_flag;
6656 }
6657
6658 spin_unlock_irqrestore(&phba->hbalock, flags);
6659
James Smarta257bf92009-04-06 18:48:10 -04006660 /* Cancel all the IOCBs from the completions list */
6661 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6662 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006663 return 1;
6664}
6665
James Smarte59058c2008-08-24 21:49:00 -04006666/**
James Smart3621a712009-04-06 18:47:14 -04006667 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006668 * @phba: Pointer to HBA context object.
6669 *
6670 * This function cleans up all iocb, buffers, mailbox commands
6671 * while shutting down the HBA. This function is called with no
6672 * lock held and always returns 1.
6673 * This function does the following to cleanup driver resources:
6674 * - Free discovery resources for each virtual port
6675 * - Cleanup any pending fabric iocbs
6676 * - Iterate through the iocb txq and free each entry
6677 * in the list.
6678 * - Free up any buffer posted to the HBA
6679 * - Free mailbox commands in the mailbox queue.
6680 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006681int
James Smart2e0fef82007-06-17 19:56:36 -05006682lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006683{
James Smart2534ba72007-04-25 09:52:20 -04006684 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006685 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006686 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006687 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006688 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006689 int i;
6690
6691 /* Shutdown the mailbox command sub-system */
6692 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006693
dea31012005-04-17 16:05:31 -05006694 lpfc_hba_down_prep(phba);
6695
James Smart92d7f7b2007-06-17 19:56:38 -05006696 lpfc_fabric_abort_hba(phba);
6697
James Smart2e0fef82007-06-17 19:56:36 -05006698 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006699 for (i = 0; i < psli->num_rings; i++) {
6700 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006701 /* Only slow rings */
6702 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006703 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006704 /* Set the lpfc data pending flag */
6705 set_bit(LPFC_DATA_READY, &phba->data_flags);
6706 }
dea31012005-04-17 16:05:31 -05006707
6708 /*
6709 * Error everything on the txq since these iocbs have not been
6710 * given to the FW yet.
6711 */
James Smart2534ba72007-04-25 09:52:20 -04006712 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006713 pring->txq_cnt = 0;
6714
dea31012005-04-17 16:05:31 -05006715 }
James Smart2e0fef82007-06-17 19:56:36 -05006716 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006717
James Smarta257bf92009-04-06 18:48:10 -04006718 /* Cancel all the IOCBs from the completions list */
6719 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6720 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006721
James Smart0ff10d42008-01-11 01:52:36 -05006722 spin_lock_irqsave(&phba->hbalock, flags);
6723 list_splice_init(&phba->elsbuf, &completions);
6724 phba->elsbuf_cnt = 0;
6725 phba->elsbuf_prev_cnt = 0;
6726 spin_unlock_irqrestore(&phba->hbalock, flags);
6727
6728 while (!list_empty(&completions)) {
6729 list_remove_head(&completions, buf_ptr,
6730 struct lpfc_dmabuf, list);
6731 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6732 kfree(buf_ptr);
6733 }
6734
dea31012005-04-17 16:05:31 -05006735 /* Return any active mbox cmds */
6736 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006737
James Smartda0436e2009-05-22 14:51:39 -04006738 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006739 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006740 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006741
James Smartda0436e2009-05-22 14:51:39 -04006742 return 1;
6743}
James Smart92d7f7b2007-06-17 19:56:38 -05006744
James Smartda0436e2009-05-22 14:51:39 -04006745/**
6746 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6747 * @phba: Pointer to HBA context object.
6748 *
6749 * This function cleans up all queues, iocb, buffers, mailbox commands while
6750 * shutting down the SLI4 HBA FCoE function. This function is called with no
6751 * lock held and always returns 1.
6752 *
6753 * This function does the following to cleanup driver FCoE function resources:
6754 * - Free discovery resources for each virtual port
6755 * - Cleanup any pending fabric iocbs
6756 * - Iterate through the iocb txq and free each entry in the list.
6757 * - Free up any buffer posted to the HBA.
6758 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6759 * - Free mailbox commands in the mailbox queue.
6760 **/
6761int
6762lpfc_sli4_hba_down(struct lpfc_hba *phba)
6763{
6764 /* Stop the SLI4 device port */
6765 lpfc_stop_port(phba);
6766
6767 /* Tear down the queues in the HBA */
6768 lpfc_sli4_queue_unset(phba);
6769
6770 /* unregister default FCFI from the HBA */
6771 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6772
dea31012005-04-17 16:05:31 -05006773 return 1;
6774}
6775
James Smarte59058c2008-08-24 21:49:00 -04006776/**
James Smart3621a712009-04-06 18:47:14 -04006777 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006778 * @srcp: Source memory pointer.
6779 * @destp: Destination memory pointer.
6780 * @cnt: Number of words required to be copied.
6781 *
6782 * This function is used for copying data between driver memory
6783 * and the SLI memory. This function also changes the endianness
6784 * of each word if native endianness is different from SLI
6785 * endianness. This function can be called with or without
6786 * lock.
6787 **/
dea31012005-04-17 16:05:31 -05006788void
6789lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6790{
6791 uint32_t *src = srcp;
6792 uint32_t *dest = destp;
6793 uint32_t ldata;
6794 int i;
6795
6796 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6797 ldata = *src;
6798 ldata = le32_to_cpu(ldata);
6799 *dest = ldata;
6800 src++;
6801 dest++;
6802 }
6803}
6804
James Smarte59058c2008-08-24 21:49:00 -04006805
6806/**
James Smarta0c87cb2009-07-19 10:01:10 -04006807 * lpfc_sli_bemem_bcopy - SLI memory copy function
6808 * @srcp: Source memory pointer.
6809 * @destp: Destination memory pointer.
6810 * @cnt: Number of words required to be copied.
6811 *
6812 * This function is used for copying data between a data structure
6813 * with big endian representation to local endianness.
6814 * This function can be called with or without lock.
6815 **/
6816void
6817lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6818{
6819 uint32_t *src = srcp;
6820 uint32_t *dest = destp;
6821 uint32_t ldata;
6822 int i;
6823
6824 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6825 ldata = *src;
6826 ldata = be32_to_cpu(ldata);
6827 *dest = ldata;
6828 src++;
6829 dest++;
6830 }
6831}
6832
6833/**
James Smart3621a712009-04-06 18:47:14 -04006834 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006835 * @phba: Pointer to HBA context object.
6836 * @pring: Pointer to driver SLI ring object.
6837 * @mp: Pointer to driver buffer object.
6838 *
6839 * This function is called with no lock held.
6840 * It always return zero after adding the buffer to the postbufq
6841 * buffer list.
6842 **/
dea31012005-04-17 16:05:31 -05006843int
James Smart2e0fef82007-06-17 19:56:36 -05006844lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6845 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006846{
6847 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6848 later */
James Smart2e0fef82007-06-17 19:56:36 -05006849 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006850 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006851 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006852 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006853 return 0;
6854}
6855
James Smarte59058c2008-08-24 21:49:00 -04006856/**
James Smart3621a712009-04-06 18:47:14 -04006857 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006858 * @phba: Pointer to HBA context object.
6859 *
6860 * When HBQ is enabled, buffers are searched based on tags. This function
6861 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6862 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6863 * does not conflict with tags of buffer posted for unsolicited events.
6864 * The function returns the allocated tag. The function is called with
6865 * no locks held.
6866 **/
James Smart76bb24e2007-10-27 13:38:00 -04006867uint32_t
6868lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6869{
6870 spin_lock_irq(&phba->hbalock);
6871 phba->buffer_tag_count++;
6872 /*
6873 * Always set the QUE_BUFTAG_BIT to distiguish between
6874 * a tag assigned by HBQ.
6875 */
6876 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6877 spin_unlock_irq(&phba->hbalock);
6878 return phba->buffer_tag_count;
6879}
6880
James Smarte59058c2008-08-24 21:49:00 -04006881/**
James Smart3621a712009-04-06 18:47:14 -04006882 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006883 * @phba: Pointer to HBA context object.
6884 * @pring: Pointer to driver SLI ring object.
6885 * @tag: Buffer tag.
6886 *
6887 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6888 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6889 * iocb is posted to the response ring with the tag of the buffer.
6890 * This function searches the pring->postbufq list using the tag
6891 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6892 * iocb. If the buffer is found then lpfc_dmabuf object of the
6893 * buffer is returned to the caller else NULL is returned.
6894 * This function is called with no lock held.
6895 **/
James Smart76bb24e2007-10-27 13:38:00 -04006896struct lpfc_dmabuf *
6897lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6898 uint32_t tag)
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 tag */
6904 spin_lock_irq(&phba->hbalock);
6905 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6906 if (mp->buffer_tag == tag) {
6907 list_del_init(&mp->list);
6908 pring->postbufq_cnt--;
6909 spin_unlock_irq(&phba->hbalock);
6910 return mp;
6911 }
6912 }
6913
6914 spin_unlock_irq(&phba->hbalock);
6915 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006916 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006917 "ring %d Data x%lx x%p x%p x%x\n",
6918 pring->ringno, (unsigned long) tag,
6919 slp->next, slp->prev, pring->postbufq_cnt);
6920
6921 return NULL;
6922}
dea31012005-04-17 16:05:31 -05006923
James Smarte59058c2008-08-24 21:49:00 -04006924/**
James Smart3621a712009-04-06 18:47:14 -04006925 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006926 * @phba: Pointer to HBA context object.
6927 * @pring: Pointer to driver SLI ring object.
6928 * @phys: DMA address of the buffer.
6929 *
6930 * This function searches the buffer list using the dma_address
6931 * of unsolicited event to find the driver's lpfc_dmabuf object
6932 * corresponding to the dma_address. The function returns the
6933 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6934 * This function is called by the ct and els unsolicited event
6935 * handlers to get the buffer associated with the unsolicited
6936 * event.
6937 *
6938 * This function is called with no lock held.
6939 **/
dea31012005-04-17 16:05:31 -05006940struct lpfc_dmabuf *
6941lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6942 dma_addr_t phys)
6943{
6944 struct lpfc_dmabuf *mp, *next_mp;
6945 struct list_head *slp = &pring->postbufq;
6946
6947 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05006948 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006949 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6950 if (mp->phys == phys) {
6951 list_del_init(&mp->list);
6952 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006953 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006954 return mp;
6955 }
6956 }
6957
James Smart2e0fef82007-06-17 19:56:36 -05006958 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006959 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04006960 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05006961 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006962 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05006963 slp->next, slp->prev, pring->postbufq_cnt);
6964 return NULL;
6965}
6966
James Smarte59058c2008-08-24 21:49:00 -04006967/**
James Smart3621a712009-04-06 18:47:14 -04006968 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04006969 * @phba: Pointer to HBA context object.
6970 * @cmdiocb: Pointer to driver command iocb object.
6971 * @rspiocb: Pointer to driver response iocb object.
6972 *
6973 * This function is the completion handler for the abort iocbs for
6974 * ELS commands. This function is called from the ELS ring event
6975 * handler with no lock held. This function frees memory resources
6976 * associated with the abort iocb.
6977 **/
dea31012005-04-17 16:05:31 -05006978static void
James Smart2e0fef82007-06-17 19:56:36 -05006979lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
6980 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05006981{
James Smart2e0fef82007-06-17 19:56:36 -05006982 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04006983 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05006984 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04006985 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
6986
6987 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04006988
6989 if (irsp->ulpStatus) {
6990 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
6991 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
6992
James Smart2e0fef82007-06-17 19:56:36 -05006993 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04006994 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
6995 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
6996
James Smart92d7f7b2007-06-17 19:56:38 -05006997 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04006998 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05006999 "with tag %x context %x, abort status %x, "
7000 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007001 abort_iocb, abort_iotag, abort_context,
7002 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007003
7004 /*
James Smart58da1ff2008-04-07 10:15:56 -04007005 * If the iocb is not found in Firmware queue the iocb
7006 * might have completed already. Do not free it again.
7007 */
James Smart9b379602008-04-07 10:16:00 -04007008 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04007009 spin_unlock_irq(&phba->hbalock);
7010 lpfc_sli_release_iocbq(phba, cmdiocb);
7011 return;
7012 }
7013 /*
James Smart2680eea2007-04-25 09:52:55 -04007014 * make sure we have the right iocbq before taking it
7015 * off the txcmplq and try to call completion routine.
7016 */
James Smart2e0fef82007-06-17 19:56:36 -05007017 if (!abort_iocb ||
7018 abort_iocb->iocb.ulpContext != abort_context ||
7019 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7020 spin_unlock_irq(&phba->hbalock);
7021 else {
James Smart92d7f7b2007-06-17 19:56:38 -05007022 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007023 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007024 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007025
James Smart0ff10d42008-01-11 01:52:36 -05007026 /* Firmware could still be in progress of DMAing
7027 * payload, so don't free data buffer till after
7028 * a hbeat.
7029 */
7030 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7031
James Smart92d7f7b2007-06-17 19:56:38 -05007032 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7033 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7034 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
7035 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007036 }
7037 }
7038
James Bottomley604a3e32005-10-29 10:28:33 -05007039 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007040 return;
7041}
7042
James Smarte59058c2008-08-24 21:49:00 -04007043/**
James Smart3621a712009-04-06 18:47:14 -04007044 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007045 * @phba: Pointer to HBA context object.
7046 * @cmdiocb: Pointer to driver command iocb object.
7047 * @rspiocb: Pointer to driver response iocb object.
7048 *
7049 * The function is called from SLI ring event handler with no
7050 * lock held. This function is the completion handler for ELS commands
7051 * which are aborted. The function frees memory resources used for
7052 * the aborted ELS commands.
7053 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007054static void
7055lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7056 struct lpfc_iocbq *rspiocb)
7057{
7058 IOCB_t *irsp = &rspiocb->iocb;
7059
7060 /* ELS cmd tag <ulpIoTag> completes */
7061 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007062 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007063 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007064 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007065 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007066 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7067 lpfc_ct_free_iocb(phba, cmdiocb);
7068 else
7069 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007070 return;
7071}
7072
James Smarte59058c2008-08-24 21:49:00 -04007073/**
James Smart3621a712009-04-06 18:47:14 -04007074 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007075 * @phba: Pointer to HBA context object.
7076 * @pring: Pointer to driver SLI ring object.
7077 * @cmdiocb: Pointer to driver command iocb object.
7078 *
7079 * This function issues an abort iocb for the provided command
7080 * iocb. This function is called with hbalock held.
7081 * The function returns 0 when it fails due to memory allocation
7082 * failure or when the command iocb is an abort request.
7083 **/
dea31012005-04-17 16:05:31 -05007084int
James Smart2e0fef82007-06-17 19:56:36 -05007085lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7086 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007087{
James Smart2e0fef82007-06-17 19:56:36 -05007088 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007089 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007090 IOCB_t *icmd = NULL;
7091 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007092 int retval = IOCB_ERROR;
7093
James Smart92d7f7b2007-06-17 19:56:38 -05007094 /*
7095 * There are certain command types we don't want to abort. And we
7096 * don't want to abort commands that are already in the process of
7097 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007098 */
7099 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007100 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007101 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7102 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007103 return 0;
7104
James Smart858c9f62007-06-17 19:56:39 -05007105 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7106 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007107 */
James Smart858c9f62007-06-17 19:56:39 -05007108 if ((vport->load_flag & FC_UNLOADING) &&
7109 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007110 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7111 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7112 else
7113 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007114 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007115 }
dea31012005-04-17 16:05:31 -05007116
7117 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007118 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007119 if (abtsiocbp == NULL)
7120 return 0;
dea31012005-04-17 16:05:31 -05007121
James Smart07951072007-04-25 09:51:38 -04007122 /* This signals the response to set the correct status
7123 * before calling the completion handler.
7124 */
7125 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7126
dea31012005-04-17 16:05:31 -05007127 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007128 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7129 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007130 if (phba->sli_rev == LPFC_SLI_REV4)
7131 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7132 else
7133 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007134 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007135 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007136
James Smart2e0fef82007-06-17 19:56:36 -05007137 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007138 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7139 else
7140 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7141
7142 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007143
James Smarte8b62012007-08-02 11:10:09 -04007144 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7145 "0339 Abort xri x%x, original iotag x%x, "
7146 "abort cmd iotag x%x\n",
7147 iabt->un.acxri.abortContextTag,
7148 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007149 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007150
James Smartd7c255b2008-08-24 21:50:00 -04007151 if (retval)
7152 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007153abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007154 /*
7155 * Caller to this routine should check for IOCB_ERROR
7156 * and handle it properly. This routine no longer removes
7157 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007158 */
James Smart2e0fef82007-06-17 19:56:36 -05007159 return retval;
dea31012005-04-17 16:05:31 -05007160}
7161
James Smarte59058c2008-08-24 21:49:00 -04007162/**
James Smart3621a712009-04-06 18:47:14 -04007163 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007164 * @iocbq: Pointer to driver iocb object.
7165 * @vport: Pointer to driver virtual port object.
7166 * @tgt_id: SCSI ID of the target.
7167 * @lun_id: LUN ID of the scsi device.
7168 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7169 *
James Smart3621a712009-04-06 18:47:14 -04007170 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007171 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7172 * 0 if the filtering criteria is met for the given iocb and will return
7173 * 1 if the filtering criteria is not met.
7174 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7175 * given iocb is for the SCSI device specified by vport, tgt_id and
7176 * lun_id parameter.
7177 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7178 * given iocb is for the SCSI target specified by vport and tgt_id
7179 * parameters.
7180 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7181 * given iocb is for the SCSI host associated with the given vport.
7182 * This function is called with no locks held.
7183 **/
dea31012005-04-17 16:05:31 -05007184static int
James Smart51ef4c22007-08-02 11:10:31 -04007185lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7186 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007187 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007188{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007189 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007190 int rc = 1;
7191
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007192 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7193 return rc;
7194
James Smart51ef4c22007-08-02 11:10:31 -04007195 if (iocbq->vport != vport)
7196 return rc;
7197
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007198 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007199
James Smart495a7142008-06-14 22:52:59 -04007200 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007201 return rc;
7202
7203 switch (ctx_cmd) {
7204 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007205 if ((lpfc_cmd->rdata->pnode) &&
7206 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7207 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007208 rc = 0;
7209 break;
7210 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007211 if ((lpfc_cmd->rdata->pnode) &&
7212 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007213 rc = 0;
7214 break;
dea31012005-04-17 16:05:31 -05007215 case LPFC_CTX_HOST:
7216 rc = 0;
7217 break;
7218 default:
7219 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007220 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007221 break;
7222 }
7223
7224 return rc;
7225}
7226
James Smarte59058c2008-08-24 21:49:00 -04007227/**
James Smart3621a712009-04-06 18:47:14 -04007228 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007229 * @vport: Pointer to virtual port.
7230 * @tgt_id: SCSI ID of the target.
7231 * @lun_id: LUN ID of the scsi device.
7232 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7233 *
7234 * This function returns number of FCP commands pending for the vport.
7235 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7236 * commands pending on the vport associated with SCSI device specified
7237 * by tgt_id and lun_id parameters.
7238 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7239 * commands pending on the vport associated with SCSI target specified
7240 * by tgt_id parameter.
7241 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7242 * commands pending on the vport.
7243 * This function returns the number of iocbs which satisfy the filter.
7244 * This function is called without any lock held.
7245 **/
dea31012005-04-17 16:05:31 -05007246int
James Smart51ef4c22007-08-02 11:10:31 -04007247lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7248 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007249{
James Smart51ef4c22007-08-02 11:10:31 -04007250 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007251 struct lpfc_iocbq *iocbq;
7252 int sum, i;
dea31012005-04-17 16:05:31 -05007253
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007254 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7255 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007256
James Smart51ef4c22007-08-02 11:10:31 -04007257 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7258 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007259 sum++;
dea31012005-04-17 16:05:31 -05007260 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007261
dea31012005-04-17 16:05:31 -05007262 return sum;
7263}
7264
James Smarte59058c2008-08-24 21:49:00 -04007265/**
James Smart3621a712009-04-06 18:47:14 -04007266 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007267 * @phba: Pointer to HBA context object
7268 * @cmdiocb: Pointer to command iocb object.
7269 * @rspiocb: Pointer to response iocb object.
7270 *
7271 * This function is called when an aborted FCP iocb completes. This
7272 * function is called by the ring event handler with no lock held.
7273 * This function frees the iocb.
7274 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007275void
James Smart2e0fef82007-06-17 19:56:36 -05007276lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7277 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007278{
James Bottomley604a3e32005-10-29 10:28:33 -05007279 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007280 return;
7281}
7282
James Smarte59058c2008-08-24 21:49:00 -04007283/**
James Smart3621a712009-04-06 18:47:14 -04007284 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007285 * @vport: Pointer to virtual port.
7286 * @pring: Pointer to driver SLI ring object.
7287 * @tgt_id: SCSI ID of the target.
7288 * @lun_id: LUN ID of the scsi device.
7289 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7290 *
7291 * This function sends an abort command for every SCSI command
7292 * associated with the given virtual port pending on the ring
7293 * filtered by lpfc_sli_validate_fcp_iocb function.
7294 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7295 * FCP iocbs associated with lun specified by tgt_id and lun_id
7296 * parameters
7297 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7298 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7299 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7300 * FCP iocbs associated with virtual port.
7301 * This function returns number of iocbs it failed to abort.
7302 * This function is called with no locks held.
7303 **/
dea31012005-04-17 16:05:31 -05007304int
James Smart51ef4c22007-08-02 11:10:31 -04007305lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7306 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007307{
James Smart51ef4c22007-08-02 11:10:31 -04007308 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007309 struct lpfc_iocbq *iocbq;
7310 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007311 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007312 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007313 int i;
dea31012005-04-17 16:05:31 -05007314
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007315 for (i = 1; i <= phba->sli.last_iotag; i++) {
7316 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007317
James Smart51ef4c22007-08-02 11:10:31 -04007318 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007319 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007320 continue;
7321
7322 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007323 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007324 if (abtsiocb == NULL) {
7325 errcnt++;
7326 continue;
7327 }
dea31012005-04-17 16:05:31 -05007328
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007329 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007330 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7331 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007332 if (phba->sli_rev == LPFC_SLI_REV4)
7333 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7334 else
7335 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007336 abtsiocb->iocb.ulpLe = 1;
7337 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007338 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007339
James Smart2e0fef82007-06-17 19:56:36 -05007340 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007341 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7342 else
7343 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7344
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007345 /* Setup callback routine and issue the command. */
7346 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007347 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7348 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007349 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007350 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007351 errcnt++;
7352 continue;
7353 }
7354 }
7355
7356 return errcnt;
7357}
7358
James Smarte59058c2008-08-24 21:49:00 -04007359/**
James Smart3621a712009-04-06 18:47:14 -04007360 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007361 * @phba: Pointer to HBA context object.
7362 * @cmdiocbq: Pointer to command iocb.
7363 * @rspiocbq: Pointer to response iocb.
7364 *
7365 * This function is the completion handler for iocbs issued using
7366 * lpfc_sli_issue_iocb_wait function. This function is called by the
7367 * ring event handler function without any lock held. This function
7368 * can be called from both worker thread context and interrupt
7369 * context. This function also can be called from other thread which
7370 * cleans up the SLI layer objects.
7371 * This function copy the contents of the response iocb to the
7372 * response iocb memory object provided by the caller of
7373 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7374 * sleeps for the iocb completion.
7375 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007376static void
7377lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7378 struct lpfc_iocbq *cmdiocbq,
7379 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007380{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007381 wait_queue_head_t *pdone_q;
7382 unsigned long iflags;
dea31012005-04-17 16:05:31 -05007383
James Smart2e0fef82007-06-17 19:56:36 -05007384 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007385 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7386 if (cmdiocbq->context2 && rspiocbq)
7387 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7388 &rspiocbq->iocb, sizeof(IOCB_t));
7389
7390 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007391 if (pdone_q)
7392 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007393 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007394 return;
7395}
7396
James Smarte59058c2008-08-24 21:49:00 -04007397/**
James Smartd11e31d2009-06-10 17:23:06 -04007398 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7399 * @phba: Pointer to HBA context object..
7400 * @piocbq: Pointer to command iocb.
7401 * @flag: Flag to test.
7402 *
7403 * This routine grabs the hbalock and then test the iocb_flag to
7404 * see if the passed in flag is set.
7405 * Returns:
7406 * 1 if flag is set.
7407 * 0 if flag is not set.
7408 **/
7409static int
7410lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7411 struct lpfc_iocbq *piocbq, uint32_t flag)
7412{
7413 unsigned long iflags;
7414 int ret;
7415
7416 spin_lock_irqsave(&phba->hbalock, iflags);
7417 ret = piocbq->iocb_flag & flag;
7418 spin_unlock_irqrestore(&phba->hbalock, iflags);
7419 return ret;
7420
7421}
7422
7423/**
James Smart3621a712009-04-06 18:47:14 -04007424 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007425 * @phba: Pointer to HBA context object..
7426 * @pring: Pointer to sli ring.
7427 * @piocb: Pointer to command iocb.
7428 * @prspiocbq: Pointer to response iocb.
7429 * @timeout: Timeout in number of seconds.
7430 *
7431 * This function issues the iocb to firmware and waits for the
7432 * iocb to complete. If the iocb command is not
7433 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7434 * Caller should not free the iocb resources if this function
7435 * returns IOCB_TIMEDOUT.
7436 * The function waits for the iocb completion using an
7437 * non-interruptible wait.
7438 * This function will sleep while waiting for iocb completion.
7439 * So, this function should not be called from any context which
7440 * does not allow sleeping. Due to the same reason, this function
7441 * cannot be called with interrupt disabled.
7442 * This function assumes that the iocb completions occur while
7443 * this function sleep. So, this function cannot be called from
7444 * the thread which process iocb completion for this ring.
7445 * This function clears the iocb_flag of the iocb object before
7446 * issuing the iocb and the iocb completion handler sets this
7447 * flag and wakes this thread when the iocb completes.
7448 * The contents of the response iocb will be copied to prspiocbq
7449 * by the completion handler when the command completes.
7450 * This function returns IOCB_SUCCESS when success.
7451 * This function is called with no lock held.
7452 **/
dea31012005-04-17 16:05:31 -05007453int
James Smart2e0fef82007-06-17 19:56:36 -05007454lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007455 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007456 struct lpfc_iocbq *piocb,
7457 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007458 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007459{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007460 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007461 long timeleft, timeout_req = 0;
7462 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007463 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007464
7465 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007466 * If the caller has provided a response iocbq buffer, then context2
7467 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007468 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007469 if (prspiocbq) {
7470 if (piocb->context2)
7471 return IOCB_ERROR;
7472 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007473 }
7474
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007475 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7476 piocb->context_un.wait_queue = &done_q;
7477 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007478
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007479 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7480 creg_val = readl(phba->HCregaddr);
7481 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7482 writel(creg_val, phba->HCregaddr);
7483 readl(phba->HCregaddr); /* flush */
7484 }
7485
James Smartda0436e2009-05-22 14:51:39 -04007486 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007487 if (retval == IOCB_SUCCESS) {
7488 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007489 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007490 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007491 timeout_req);
dea31012005-04-17 16:05:31 -05007492
James Smart7054a602007-04-25 09:52:34 -04007493 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7494 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007495 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007496 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007497 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007498 "0338 IOCB wait timeout error - no "
7499 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007500 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007501 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007502 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007503 "0330 IOCB wake NOT set, "
7504 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007505 timeout, (timeleft / jiffies));
7506 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007507 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007508 } else {
7509 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007510 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007511 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007512 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007513 }
7514
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007515 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7516 creg_val = readl(phba->HCregaddr);
7517 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7518 writel(creg_val, phba->HCregaddr);
7519 readl(phba->HCregaddr); /* flush */
7520 }
7521
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007522 if (prspiocbq)
7523 piocb->context2 = NULL;
7524
7525 piocb->context_un.wait_queue = NULL;
7526 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007527 return retval;
7528}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007529
James Smarte59058c2008-08-24 21:49:00 -04007530/**
James Smart3621a712009-04-06 18:47:14 -04007531 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007532 * @phba: Pointer to HBA context object.
7533 * @pmboxq: Pointer to driver mailbox object.
7534 * @timeout: Timeout in number of seconds.
7535 *
7536 * This function issues the mailbox to firmware and waits for the
7537 * mailbox command to complete. If the mailbox command is not
7538 * completed within timeout seconds, it returns MBX_TIMEOUT.
7539 * The function waits for the mailbox completion using an
7540 * interruptible wait. If the thread is woken up due to a
7541 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7542 * should not free the mailbox resources, if this function returns
7543 * MBX_TIMEOUT.
7544 * This function will sleep while waiting for mailbox completion.
7545 * So, this function should not be called from any context which
7546 * does not allow sleeping. Due to the same reason, this function
7547 * cannot be called with interrupt disabled.
7548 * This function assumes that the mailbox completion occurs while
7549 * this function sleep. So, this function cannot be called from
7550 * the worker thread which processes mailbox completion.
7551 * This function is called in the context of HBA management
7552 * applications.
7553 * This function returns MBX_SUCCESS when successful.
7554 * This function is called with no lock held.
7555 **/
dea31012005-04-17 16:05:31 -05007556int
James Smart2e0fef82007-06-17 19:56:36 -05007557lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007558 uint32_t timeout)
7559{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007560 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007561 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007562 unsigned long flag;
dea31012005-04-17 16:05:31 -05007563
7564 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007565 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007566 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007567
James Smart495a7142008-06-14 22:52:59 -04007568 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007569 /* setup wake call as IOCB callback */
7570 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7571 /* setup context field to pass wait_queue pointer to wake function */
7572 pmboxq->context1 = &done_q;
7573
dea31012005-04-17 16:05:31 -05007574 /* now issue the command */
7575 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7576
7577 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007578 wait_event_interruptible_timeout(done_q,
7579 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7580 timeout * HZ);
7581
James Smart858c9f62007-06-17 19:56:39 -05007582 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007583 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007584 /*
7585 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7586 * else do not free the resources.
7587 */
7588 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007589 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007590 else {
James Smart7054a602007-04-25 09:52:34 -04007591 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007592 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7593 }
7594 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007595 }
7596
dea31012005-04-17 16:05:31 -05007597 return retval;
7598}
7599
James Smarte59058c2008-08-24 21:49:00 -04007600/**
James Smart3772a992009-05-22 14:50:54 -04007601 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007602 * @phba: Pointer to HBA context.
7603 *
James Smart3772a992009-05-22 14:50:54 -04007604 * This function is called to shutdown the driver's mailbox sub-system.
7605 * It first marks the mailbox sub-system is in a block state to prevent
7606 * the asynchronous mailbox command from issued off the pending mailbox
7607 * command queue. If the mailbox command sub-system shutdown is due to
7608 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7609 * the mailbox sub-system flush routine to forcefully bring down the
7610 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7611 * as with offline or HBA function reset), this routine will wait for the
7612 * outstanding mailbox command to complete before invoking the mailbox
7613 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007614 **/
James Smart3772a992009-05-22 14:50:54 -04007615void
7616lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007617{
James Smart3772a992009-05-22 14:50:54 -04007618 struct lpfc_sli *psli = &phba->sli;
7619 uint8_t actcmd = MBX_HEARTBEAT;
7620 unsigned long timeout;
7621
7622 spin_lock_irq(&phba->hbalock);
7623 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7624 spin_unlock_irq(&phba->hbalock);
7625
7626 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7627 spin_lock_irq(&phba->hbalock);
7628 if (phba->sli.mbox_active)
7629 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7630 spin_unlock_irq(&phba->hbalock);
7631 /* Determine how long we might wait for the active mailbox
7632 * command to be gracefully completed by firmware.
7633 */
7634 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7635 1000) + jiffies;
7636 while (phba->sli.mbox_active) {
7637 /* Check active mailbox complete status every 2ms */
7638 msleep(2);
7639 if (time_after(jiffies, timeout))
7640 /* Timeout, let the mailbox flush routine to
7641 * forcefully release active mailbox command
7642 */
7643 break;
7644 }
7645 }
7646 lpfc_sli_mbox_sys_flush(phba);
7647}
7648
7649/**
7650 * lpfc_sli_eratt_read - read sli-3 error attention events
7651 * @phba: Pointer to HBA context.
7652 *
7653 * This function is called to read the SLI3 device error attention registers
7654 * for possible error attention events. The caller must hold the hostlock
7655 * with spin_lock_irq().
7656 *
7657 * This fucntion returns 1 when there is Error Attention in the Host Attention
7658 * Register and returns 0 otherwise.
7659 **/
7660static int
7661lpfc_sli_eratt_read(struct lpfc_hba *phba)
7662{
James Smarted957682007-06-17 19:56:37 -05007663 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007664
James Smart3772a992009-05-22 14:50:54 -04007665 /* Read chip Host Attention (HA) register */
7666 ha_copy = readl(phba->HAregaddr);
7667 if (ha_copy & HA_ERATT) {
7668 /* Read host status register to retrieve error event */
7669 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007670
James Smart3772a992009-05-22 14:50:54 -04007671 /* Check if there is a deferred error condition is active */
7672 if ((HS_FFER1 & phba->work_hs) &&
7673 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7674 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007675 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007676 /* Clear all interrupt enable conditions */
7677 writel(0, phba->HCregaddr);
7678 readl(phba->HCregaddr);
7679 }
7680
7681 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007682 phba->work_ha |= HA_ERATT;
7683 /* Indicate polling handles this ERATT */
7684 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007685 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007686 }
James Smart3772a992009-05-22 14:50:54 -04007687 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007688}
7689
James Smarte59058c2008-08-24 21:49:00 -04007690/**
James Smartda0436e2009-05-22 14:51:39 -04007691 * lpfc_sli4_eratt_read - read sli-4 error attention events
7692 * @phba: Pointer to HBA context.
7693 *
7694 * This function is called to read the SLI4 device error attention registers
7695 * for possible error attention events. The caller must hold the hostlock
7696 * with spin_lock_irq().
7697 *
7698 * This fucntion returns 1 when there is Error Attention in the Host Attention
7699 * Register and returns 0 otherwise.
7700 **/
7701static int
7702lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7703{
7704 uint32_t uerr_sta_hi, uerr_sta_lo;
7705 uint32_t onlnreg0, onlnreg1;
7706
7707 /* For now, use the SLI4 device internal unrecoverable error
7708 * registers for error attention. This can be changed later.
7709 */
7710 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7711 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7712 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7713 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7714 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7715 if (uerr_sta_lo || uerr_sta_hi) {
7716 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7717 "1423 HBA Unrecoverable error: "
7718 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7719 "online0_reg=0x%x, online1_reg=0x%x\n",
7720 uerr_sta_lo, uerr_sta_hi,
7721 onlnreg0, onlnreg1);
James Smartda0436e2009-05-22 14:51:39 -04007722 phba->work_status[0] = uerr_sta_lo;
7723 phba->work_status[1] = uerr_sta_hi;
James Smartda0436e2009-05-22 14:51:39 -04007724 /* Set the driver HA work bitmap */
7725 phba->work_ha |= HA_ERATT;
7726 /* Indicate polling handles this ERATT */
7727 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smartda0436e2009-05-22 14:51:39 -04007728 return 1;
7729 }
7730 }
7731 return 0;
7732}
7733
7734/**
James Smart3621a712009-04-06 18:47:14 -04007735 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007736 * @phba: Pointer to HBA context.
7737 *
James Smart3772a992009-05-22 14:50:54 -04007738 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007739 * error attention register bit for error attention events.
7740 *
7741 * This fucntion returns 1 when there is Error Attention in the Host Attention
7742 * Register and returns 0 otherwise.
7743 **/
7744int
7745lpfc_sli_check_eratt(struct lpfc_hba *phba)
7746{
7747 uint32_t ha_copy;
7748
7749 /* If somebody is waiting to handle an eratt, don't process it
7750 * here. The brdkill function will do this.
7751 */
7752 if (phba->link_flag & LS_IGNORE_ERATT)
7753 return 0;
7754
7755 /* Check if interrupt handler handles this ERATT */
7756 spin_lock_irq(&phba->hbalock);
7757 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7758 /* Interrupt handler has handled ERATT */
7759 spin_unlock_irq(&phba->hbalock);
7760 return 0;
7761 }
7762
James Smarta257bf92009-04-06 18:48:10 -04007763 /*
7764 * If there is deferred error attention, do not check for error
7765 * attention
7766 */
7767 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7768 spin_unlock_irq(&phba->hbalock);
7769 return 0;
7770 }
7771
James Smart3772a992009-05-22 14:50:54 -04007772 /* If PCI channel is offline, don't process it */
7773 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007774 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007775 return 0;
7776 }
7777
7778 switch (phba->sli_rev) {
7779 case LPFC_SLI_REV2:
7780 case LPFC_SLI_REV3:
7781 /* Read chip Host Attention (HA) register */
7782 ha_copy = lpfc_sli_eratt_read(phba);
7783 break;
James Smartda0436e2009-05-22 14:51:39 -04007784 case LPFC_SLI_REV4:
7785 /* Read devcie Uncoverable Error (UERR) registers */
7786 ha_copy = lpfc_sli4_eratt_read(phba);
7787 break;
James Smart3772a992009-05-22 14:50:54 -04007788 default:
7789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7790 "0299 Invalid SLI revision (%d)\n",
7791 phba->sli_rev);
7792 ha_copy = 0;
7793 break;
James Smart93996272008-08-24 21:50:30 -04007794 }
7795 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007796
7797 return ha_copy;
7798}
7799
7800/**
7801 * lpfc_intr_state_check - Check device state for interrupt handling
7802 * @phba: Pointer to HBA context.
7803 *
7804 * This inline routine checks whether a device or its PCI slot is in a state
7805 * that the interrupt should be handled.
7806 *
7807 * This function returns 0 if the device or the PCI slot is in a state that
7808 * interrupt should be handled, otherwise -EIO.
7809 */
7810static inline int
7811lpfc_intr_state_check(struct lpfc_hba *phba)
7812{
7813 /* If the pci channel is offline, ignore all the interrupts */
7814 if (unlikely(pci_channel_offline(phba->pcidev)))
7815 return -EIO;
7816
7817 /* Update device level interrupt statistics */
7818 phba->sli.slistat.sli_intr++;
7819
7820 /* Ignore all interrupts during initialization. */
7821 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7822 return -EIO;
7823
James Smart93996272008-08-24 21:50:30 -04007824 return 0;
7825}
7826
7827/**
James Smart3772a992009-05-22 14:50:54 -04007828 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007829 * @irq: Interrupt number.
7830 * @dev_id: The device context pointer.
7831 *
James Smart93996272008-08-24 21:50:30 -04007832 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007833 * service routine when device with SLI-3 interface spec is enabled with
7834 * MSI-X multi-message interrupt mode and there are slow-path events in
7835 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7836 * interrupt mode, this function is called as part of the device-level
7837 * interrupt handler. When the PCI slot is in error recovery or the HBA
7838 * is undergoing initialization, the interrupt handler will not process
7839 * the interrupt. The link attention and ELS ring attention events are
7840 * handled by the worker thread. The interrupt handler signals the worker
7841 * thread and returns for these events. This function is called without
7842 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007843 * structures.
7844 *
7845 * This function returns IRQ_HANDLED when interrupt is handled else it
7846 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007847 **/
dea31012005-04-17 16:05:31 -05007848irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007849lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007850{
James Smart2e0fef82007-06-17 19:56:36 -05007851 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05007852 uint32_t ha_copy;
7853 uint32_t work_ha_copy;
7854 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007855 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007856 uint32_t control;
7857
James Smart92d7f7b2007-06-17 19:56:38 -05007858 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007859 struct lpfc_vport *vport;
7860 struct lpfc_nodelist *ndlp;
7861 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007862 LPFC_MBOXQ_t *pmb;
7863 int rc;
7864
dea31012005-04-17 16:05:31 -05007865 /*
7866 * Get the driver's phba structure from the dev_id and
7867 * assume the HBA is not interrupting.
7868 */
James Smart93996272008-08-24 21:50:30 -04007869 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007870
7871 if (unlikely(!phba))
7872 return IRQ_NONE;
7873
dea31012005-04-17 16:05:31 -05007874 /*
James Smart93996272008-08-24 21:50:30 -04007875 * Stuff needs to be attented to when this function is invoked as an
7876 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007877 */
James Smart93996272008-08-24 21:50:30 -04007878 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007879 /* Check device state for handling interrupt */
7880 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007881 return IRQ_NONE;
7882 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007883 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007884 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007885 /* If somebody is waiting to handle an eratt don't process it
7886 * here. The brdkill function will do this.
7887 */
7888 if (phba->link_flag & LS_IGNORE_ERATT)
7889 ha_copy &= ~HA_ERATT;
7890 /* Check the need for handling ERATT in interrupt handler */
7891 if (ha_copy & HA_ERATT) {
7892 if (phba->hba_flag & HBA_ERATT_HANDLED)
7893 /* ERATT polling has handled ERATT */
7894 ha_copy &= ~HA_ERATT;
7895 else
7896 /* Indicate interrupt handler handles ERATT */
7897 phba->hba_flag |= HBA_ERATT_HANDLED;
7898 }
James Smarta257bf92009-04-06 18:48:10 -04007899
7900 /*
7901 * If there is deferred error attention, do not check for any
7902 * interrupt.
7903 */
7904 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007905 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007906 return IRQ_NONE;
7907 }
7908
James Smart93996272008-08-24 21:50:30 -04007909 /* Clear up only attention source related to slow-path */
7910 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7911 phba->HAregaddr);
7912 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007913 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007914 } else
7915 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05007916
dea31012005-04-17 16:05:31 -05007917 work_ha_copy = ha_copy & phba->work_ha_mask;
7918
James Smart93996272008-08-24 21:50:30 -04007919 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05007920 if (work_ha_copy & HA_LATT) {
7921 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7922 /*
7923 * Turn off Link Attention interrupts
7924 * until CLEAR_LA done
7925 */
James Smart5b75da22008-12-04 22:39:35 -05007926 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007927 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7928 control = readl(phba->HCregaddr);
7929 control &= ~HC_LAINT_ENA;
7930 writel(control, phba->HCregaddr);
7931 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007932 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007933 }
7934 else
7935 work_ha_copy &= ~HA_LATT;
7936 }
7937
James Smart93996272008-08-24 21:50:30 -04007938 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05007939 /*
7940 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7941 * the only slow ring.
7942 */
7943 status = (work_ha_copy &
7944 (HA_RXMASK << (4*LPFC_ELS_RING)));
7945 status >>= (4*LPFC_ELS_RING);
7946 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05007947 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05007948 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04007949
7950 lpfc_debugfs_slow_ring_trc(phba,
7951 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
7952 control, status,
7953 (uint32_t)phba->sli.slistat.sli_intr);
7954
James Smart858c9f62007-06-17 19:56:39 -05007955 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04007956 lpfc_debugfs_slow_ring_trc(phba,
7957 "ISR Disable ring:"
7958 "pwork:x%x hawork:x%x wait:x%x",
7959 phba->work_ha, work_ha_copy,
7960 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007961 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007962
James Smart858c9f62007-06-17 19:56:39 -05007963 control &=
7964 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05007965 writel(control, phba->HCregaddr);
7966 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05007967 }
James Smarta58cbd52007-08-02 11:09:43 -04007968 else {
7969 lpfc_debugfs_slow_ring_trc(phba,
7970 "ISR slow ring: pwork:"
7971 "x%x hawork:x%x wait:x%x",
7972 phba->work_ha, work_ha_copy,
7973 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007974 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007975 }
James Smart5b75da22008-12-04 22:39:35 -05007976 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007977 }
7978 }
James Smart5b75da22008-12-04 22:39:35 -05007979 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007980 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04007981 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04007982 /*
7983 * Check if there is a deferred error condition
7984 * is active
7985 */
7986 if ((HS_FFER1 & phba->work_hs) &&
7987 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7988 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
7989 phba->hba_flag |= DEFER_ERATT;
7990 /* Clear all interrupt enable conditions */
7991 writel(0, phba->HCregaddr);
7992 readl(phba->HCregaddr);
7993 }
7994 }
7995
James Smart93996272008-08-24 21:50:30 -04007996 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007997 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04007998 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04007999 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008000 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008001
8002 /* First check out the status word */
8003 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8004 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008005 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008006 /*
8007 * Stray Mailbox Interrupt, mbxCommand <cmd>
8008 * mbxStatus <status>
8009 */
James Smart09372822008-01-11 01:52:54 -05008010 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008011 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008012 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008013 "Interrupt mbxCommand x%x "
8014 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008015 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008016 pmbox->mbxCommand,
8017 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008018 /* clear mailbox attention bit */
8019 work_ha_copy &= ~HA_MBATT;
8020 } else {
James Smart97eab632008-04-07 10:16:05 -04008021 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008022 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008023 phba->last_completion_time = jiffies;
8024 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008025 if (pmb->mbox_cmpl) {
8026 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8027 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008028 }
James Smart09372822008-01-11 01:52:54 -05008029 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8030 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8031
8032 lpfc_debugfs_disc_trc(vport,
8033 LPFC_DISC_TRC_MBOX_VPORT,
8034 "MBOX dflt rpi: : "
8035 "status:x%x rpi:x%x",
8036 (uint32_t)pmbox->mbxStatus,
8037 pmbox->un.varWords[0], 0);
8038
8039 if (!pmbox->mbxStatus) {
8040 mp = (struct lpfc_dmabuf *)
8041 (pmb->context1);
8042 ndlp = (struct lpfc_nodelist *)
8043 pmb->context2;
8044
8045 /* Reg_LOGIN of dflt RPI was
8046 * successful. new lets get
8047 * rid of the RPI using the
8048 * same mbox buffer.
8049 */
8050 lpfc_unreg_login(phba,
8051 vport->vpi,
8052 pmbox->un.varWords[0],
8053 pmb);
8054 pmb->mbox_cmpl =
8055 lpfc_mbx_cmpl_dflt_rpi;
8056 pmb->context1 = mp;
8057 pmb->context2 = ndlp;
8058 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008059 rc = lpfc_sli_issue_mbox(phba,
8060 pmb,
8061 MBX_NOWAIT);
8062 if (rc != MBX_BUSY)
8063 lpfc_printf_log(phba,
8064 KERN_ERR,
8065 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008066 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04008067 "been MBX_BUSY");
James Smart3772a992009-05-22 14:50:54 -04008068 if (rc != MBX_NOT_FINISHED)
8069 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008070 }
8071 }
James Smart5b75da22008-12-04 22:39:35 -05008072 spin_lock_irqsave(
8073 &phba->pport->work_port_lock,
8074 iflag);
James Smart09372822008-01-11 01:52:54 -05008075 phba->pport->work_port_events &=
8076 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008077 spin_unlock_irqrestore(
8078 &phba->pport->work_port_lock,
8079 iflag);
James Smart09372822008-01-11 01:52:54 -05008080 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008081 }
James Smart97eab632008-04-07 10:16:05 -04008082 } else
James Smart5b75da22008-12-04 22:39:35 -05008083 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008084
James Smart92d7f7b2007-06-17 19:56:38 -05008085 if ((work_ha_copy & HA_MBATT) &&
8086 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008087send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008088 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008089 do {
8090 rc = lpfc_sli_issue_mbox(phba, NULL,
8091 MBX_NOWAIT);
8092 } while (rc == MBX_NOT_FINISHED);
8093 if (rc != MBX_SUCCESS)
8094 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8095 LOG_SLI, "0349 rc should be "
8096 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05008097 }
8098
James Smart5b75da22008-12-04 22:39:35 -05008099 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008100 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008101 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008102 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008103 }
James Smart93996272008-08-24 21:50:30 -04008104 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008105
James Smart3772a992009-05-22 14:50:54 -04008106} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008107
8108/**
James Smart3772a992009-05-22 14:50:54 -04008109 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008110 * @irq: Interrupt number.
8111 * @dev_id: The device context pointer.
8112 *
8113 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008114 * service routine when device with SLI-3 interface spec is enabled with
8115 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8116 * ring event in the HBA. However, when the device is enabled with either
8117 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8118 * device-level interrupt handler. When the PCI slot is in error recovery
8119 * or the HBA is undergoing initialization, the interrupt handler will not
8120 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8121 * the intrrupt context. This function is called without any lock held.
8122 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008123 *
8124 * This function returns IRQ_HANDLED when interrupt is handled else it
8125 * returns IRQ_NONE.
8126 **/
8127irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008128lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008129{
8130 struct lpfc_hba *phba;
8131 uint32_t ha_copy;
8132 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008133 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008134
8135 /* Get the driver's phba structure from the dev_id and
8136 * assume the HBA is not interrupting.
8137 */
8138 phba = (struct lpfc_hba *) dev_id;
8139
8140 if (unlikely(!phba))
8141 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008142
8143 /*
James Smart93996272008-08-24 21:50:30 -04008144 * Stuff needs to be attented to when this function is invoked as an
8145 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008146 */
James Smart93996272008-08-24 21:50:30 -04008147 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008148 /* Check device state for handling interrupt */
8149 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008150 return IRQ_NONE;
8151 /* Need to read HA REG for FCP ring and other ring events */
8152 ha_copy = readl(phba->HAregaddr);
8153 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008154 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008155 /*
8156 * If there is deferred error attention, do not check for
8157 * any interrupt.
8158 */
8159 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008160 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008161 return IRQ_NONE;
8162 }
James Smart93996272008-08-24 21:50:30 -04008163 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8164 phba->HAregaddr);
8165 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008166 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008167 } else
8168 ha_copy = phba->ha_copy;
8169
8170 /*
8171 * Process all events on FCP ring. Take the optimized path for FCP IO.
8172 */
8173 ha_copy &= ~(phba->work_ha_mask);
8174
8175 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008176 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008177 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008178 lpfc_sli_handle_fast_ring_event(phba,
8179 &phba->sli.ring[LPFC_FCP_RING],
8180 status);
James Smarta4bc3372006-12-02 13:34:16 -05008181
8182 if (phba->cfg_multi_ring_support == 2) {
8183 /*
James Smart93996272008-08-24 21:50:30 -04008184 * Process all events on extra ring. Take the optimized path
8185 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008186 */
James Smart93996272008-08-24 21:50:30 -04008187 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008188 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008189 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008190 lpfc_sli_handle_fast_ring_event(phba,
8191 &phba->sli.ring[LPFC_EXTRA_RING],
8192 status);
8193 }
8194 }
dea31012005-04-17 16:05:31 -05008195 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008196} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008197
James Smart93996272008-08-24 21:50:30 -04008198/**
James Smart3772a992009-05-22 14:50:54 -04008199 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008200 * @irq: Interrupt number.
8201 * @dev_id: The device context pointer.
8202 *
James Smart3772a992009-05-22 14:50:54 -04008203 * This function is the HBA device-level interrupt handler to device with
8204 * SLI-3 interface spec, called from the PCI layer when either MSI or
8205 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8206 * requires driver attention. This function invokes the slow-path interrupt
8207 * attention handling function and fast-path interrupt attention handling
8208 * function in turn to process the relevant HBA attention events. This
8209 * function is called without any lock held. It gets the hbalock to access
8210 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008211 *
8212 * This function returns IRQ_HANDLED when interrupt is handled, else it
8213 * returns IRQ_NONE.
8214 **/
8215irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008216lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008217{
8218 struct lpfc_hba *phba;
8219 irqreturn_t sp_irq_rc, fp_irq_rc;
8220 unsigned long status1, status2;
8221
8222 /*
8223 * Get the driver's phba structure from the dev_id and
8224 * assume the HBA is not interrupting.
8225 */
8226 phba = (struct lpfc_hba *) dev_id;
8227
8228 if (unlikely(!phba))
8229 return IRQ_NONE;
8230
James Smart3772a992009-05-22 14:50:54 -04008231 /* Check device state for handling interrupt */
8232 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008233 return IRQ_NONE;
8234
8235 spin_lock(&phba->hbalock);
8236 phba->ha_copy = readl(phba->HAregaddr);
8237 if (unlikely(!phba->ha_copy)) {
8238 spin_unlock(&phba->hbalock);
8239 return IRQ_NONE;
8240 } else if (phba->ha_copy & HA_ERATT) {
8241 if (phba->hba_flag & HBA_ERATT_HANDLED)
8242 /* ERATT polling has handled ERATT */
8243 phba->ha_copy &= ~HA_ERATT;
8244 else
8245 /* Indicate interrupt handler handles ERATT */
8246 phba->hba_flag |= HBA_ERATT_HANDLED;
8247 }
8248
James Smarta257bf92009-04-06 18:48:10 -04008249 /*
8250 * If there is deferred error attention, do not check for any interrupt.
8251 */
8252 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8253 spin_unlock_irq(&phba->hbalock);
8254 return IRQ_NONE;
8255 }
8256
James Smart93996272008-08-24 21:50:30 -04008257 /* Clear attention sources except link and error attentions */
8258 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8259 readl(phba->HAregaddr); /* flush */
8260 spin_unlock(&phba->hbalock);
8261
8262 /*
8263 * Invokes slow-path host attention interrupt handling as appropriate.
8264 */
8265
8266 /* status of events with mailbox and link attention */
8267 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8268
8269 /* status of events with ELS ring */
8270 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8271 status2 >>= (4*LPFC_ELS_RING);
8272
8273 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008274 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008275 else
8276 sp_irq_rc = IRQ_NONE;
8277
8278 /*
8279 * Invoke fast-path host attention interrupt handling as appropriate.
8280 */
8281
8282 /* status of events with FCP ring */
8283 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8284 status1 >>= (4*LPFC_FCP_RING);
8285
8286 /* status of events with extra ring */
8287 if (phba->cfg_multi_ring_support == 2) {
8288 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8289 status2 >>= (4*LPFC_EXTRA_RING);
8290 } else
8291 status2 = 0;
8292
8293 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008294 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008295 else
8296 fp_irq_rc = IRQ_NONE;
8297
8298 /* Return device-level interrupt handling status */
8299 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008300} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008301
8302/**
8303 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8304 * @phba: pointer to lpfc hba data structure.
8305 *
8306 * This routine is invoked by the worker thread to process all the pending
8307 * SLI4 FCP abort XRI events.
8308 **/
8309void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8310{
8311 struct lpfc_cq_event *cq_event;
8312
8313 /* First, declare the fcp xri abort event has been handled */
8314 spin_lock_irq(&phba->hbalock);
8315 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8316 spin_unlock_irq(&phba->hbalock);
8317 /* Now, handle all the fcp xri abort events */
8318 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8319 /* Get the first event from the head of the event queue */
8320 spin_lock_irq(&phba->hbalock);
8321 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8322 cq_event, struct lpfc_cq_event, list);
8323 spin_unlock_irq(&phba->hbalock);
8324 /* Notify aborted XRI for FCP work queue */
8325 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8326 /* Free the event processed back to the free pool */
8327 lpfc_sli4_cq_event_release(phba, cq_event);
8328 }
8329}
8330
8331/**
8332 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8333 * @phba: pointer to lpfc hba data structure.
8334 *
8335 * This routine is invoked by the worker thread to process all the pending
8336 * SLI4 els abort xri events.
8337 **/
8338void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8339{
8340 struct lpfc_cq_event *cq_event;
8341
8342 /* First, declare the els xri abort event has been handled */
8343 spin_lock_irq(&phba->hbalock);
8344 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8345 spin_unlock_irq(&phba->hbalock);
8346 /* Now, handle all the els xri abort events */
8347 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8348 /* Get the first event from the head of the event queue */
8349 spin_lock_irq(&phba->hbalock);
8350 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8351 cq_event, struct lpfc_cq_event, list);
8352 spin_unlock_irq(&phba->hbalock);
8353 /* Notify aborted XRI for ELS work queue */
8354 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8355 /* Free the event processed back to the free pool */
8356 lpfc_sli4_cq_event_release(phba, cq_event);
8357 }
8358}
8359
8360static void
8361lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8362 struct lpfc_iocbq *pIocbOut,
8363 struct lpfc_wcqe_complete *wcqe)
8364{
8365 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8366
8367 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8368 sizeof(struct lpfc_iocbq) - offset);
James Smart4d9ab992009-10-02 15:16:39 -04008369 pIocbIn->cq_event.cqe.wcqe_cmpl = *wcqe;
James Smart4f774512009-05-22 14:52:35 -04008370 /* Map WCQE parameters into irspiocb parameters */
8371 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8372 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8373 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8374 pIocbIn->iocb.un.fcpi.fcpi_parm =
8375 pIocbOut->iocb.un.fcpi.fcpi_parm -
8376 wcqe->total_data_placed;
8377 else
8378 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8379 else
8380 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart4f774512009-05-22 14:52:35 -04008381}
8382
8383/**
James Smart04c68492009-05-22 14:52:52 -04008384 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8385 * @phba: Pointer to HBA context object.
8386 * @cqe: Pointer to mailbox completion queue entry.
8387 *
8388 * This routine process a mailbox completion queue entry with asynchrous
8389 * event.
8390 *
8391 * Return: true if work posted to worker thread, otherwise false.
8392 **/
8393static bool
8394lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8395{
8396 struct lpfc_cq_event *cq_event;
8397 unsigned long iflags;
8398
8399 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8400 "0392 Async Event: word0:x%x, word1:x%x, "
8401 "word2:x%x, word3:x%x\n", mcqe->word0,
8402 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8403
8404 /* Allocate a new internal CQ_EVENT entry */
8405 cq_event = lpfc_sli4_cq_event_alloc(phba);
8406 if (!cq_event) {
8407 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8408 "0394 Failed to allocate CQ_EVENT entry\n");
8409 return false;
8410 }
8411
8412 /* Move the CQE into an asynchronous event entry */
8413 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8414 spin_lock_irqsave(&phba->hbalock, iflags);
8415 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8416 /* Set the async event flag */
8417 phba->hba_flag |= ASYNC_EVENT;
8418 spin_unlock_irqrestore(&phba->hbalock, iflags);
8419
8420 return true;
8421}
8422
8423/**
8424 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8425 * @phba: Pointer to HBA context object.
8426 * @cqe: Pointer to mailbox completion queue entry.
8427 *
8428 * This routine process a mailbox completion queue entry with mailbox
8429 * completion event.
8430 *
8431 * Return: true if work posted to worker thread, otherwise false.
8432 **/
8433static bool
8434lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8435{
8436 uint32_t mcqe_status;
8437 MAILBOX_t *mbox, *pmbox;
8438 struct lpfc_mqe *mqe;
8439 struct lpfc_vport *vport;
8440 struct lpfc_nodelist *ndlp;
8441 struct lpfc_dmabuf *mp;
8442 unsigned long iflags;
8443 LPFC_MBOXQ_t *pmb;
8444 bool workposted = false;
8445 int rc;
8446
8447 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8448 if (!bf_get(lpfc_trailer_completed, mcqe))
8449 goto out_no_mqe_complete;
8450
8451 /* Get the reference to the active mbox command */
8452 spin_lock_irqsave(&phba->hbalock, iflags);
8453 pmb = phba->sli.mbox_active;
8454 if (unlikely(!pmb)) {
8455 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8456 "1832 No pending MBOX command to handle\n");
8457 spin_unlock_irqrestore(&phba->hbalock, iflags);
8458 goto out_no_mqe_complete;
8459 }
8460 spin_unlock_irqrestore(&phba->hbalock, iflags);
8461 mqe = &pmb->u.mqe;
8462 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8463 mbox = phba->mbox;
8464 vport = pmb->vport;
8465
8466 /* Reset heartbeat timer */
8467 phba->last_completion_time = jiffies;
8468 del_timer(&phba->sli.mbox_tmo);
8469
8470 /* Move mbox data to caller's mailbox region, do endian swapping */
8471 if (pmb->mbox_cmpl && mbox)
8472 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8473 /* Set the mailbox status with SLI4 range 0x4000 */
8474 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8475 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8476 bf_set(lpfc_mqe_status, mqe,
8477 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8478
8479 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8480 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8481 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8482 "MBOX dflt rpi: status:x%x rpi:x%x",
8483 mcqe_status,
8484 pmbox->un.varWords[0], 0);
8485 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8486 mp = (struct lpfc_dmabuf *)(pmb->context1);
8487 ndlp = (struct lpfc_nodelist *)pmb->context2;
8488 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8489 * RID of the PPI using the same mbox buffer.
8490 */
8491 lpfc_unreg_login(phba, vport->vpi,
8492 pmbox->un.varWords[0], pmb);
8493 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8494 pmb->context1 = mp;
8495 pmb->context2 = ndlp;
8496 pmb->vport = vport;
8497 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8498 if (rc != MBX_BUSY)
8499 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8500 LOG_SLI, "0385 rc should "
8501 "have been MBX_BUSY\n");
8502 if (rc != MBX_NOT_FINISHED)
8503 goto send_current_mbox;
8504 }
8505 }
8506 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8507 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8508 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8509
8510 /* There is mailbox completion work to do */
8511 spin_lock_irqsave(&phba->hbalock, iflags);
8512 __lpfc_mbox_cmpl_put(phba, pmb);
8513 phba->work_ha |= HA_MBATT;
8514 spin_unlock_irqrestore(&phba->hbalock, iflags);
8515 workposted = true;
8516
8517send_current_mbox:
8518 spin_lock_irqsave(&phba->hbalock, iflags);
8519 /* Release the mailbox command posting token */
8520 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8521 /* Setting active mailbox pointer need to be in sync to flag clear */
8522 phba->sli.mbox_active = NULL;
8523 spin_unlock_irqrestore(&phba->hbalock, iflags);
8524 /* Wake up worker thread to post the next pending mailbox command */
8525 lpfc_worker_wake_up(phba);
8526out_no_mqe_complete:
8527 if (bf_get(lpfc_trailer_consumed, mcqe))
8528 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8529 return workposted;
8530}
8531
8532/**
8533 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8534 * @phba: Pointer to HBA context object.
8535 * @cqe: Pointer to mailbox completion queue entry.
8536 *
8537 * This routine process a mailbox completion queue entry, it invokes the
8538 * proper mailbox complete handling or asynchrous event handling routine
8539 * according to the MCQE's async bit.
8540 *
8541 * Return: true if work posted to worker thread, otherwise false.
8542 **/
8543static bool
8544lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8545{
8546 struct lpfc_mcqe mcqe;
8547 bool workposted;
8548
8549 /* Copy the mailbox MCQE and convert endian order as needed */
8550 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8551
8552 /* Invoke the proper event handling routine */
8553 if (!bf_get(lpfc_trailer_async, &mcqe))
8554 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8555 else
8556 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8557 return workposted;
8558}
8559
8560/**
James Smart4f774512009-05-22 14:52:35 -04008561 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8562 * @phba: Pointer to HBA context object.
8563 * @wcqe: Pointer to work-queue completion queue entry.
8564 *
8565 * This routine handles an ELS work-queue completion event.
8566 *
8567 * Return: true if work posted to worker thread, otherwise false.
8568 **/
8569static bool
8570lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8571 struct lpfc_wcqe_complete *wcqe)
8572{
8573 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8574 struct lpfc_iocbq *cmdiocbq;
8575 struct lpfc_iocbq *irspiocbq;
8576 unsigned long iflags;
8577 bool workposted = false;
8578
8579 spin_lock_irqsave(&phba->hbalock, iflags);
8580 pring->stats.iocb_event++;
8581 /* Look up the ELS command IOCB and create pseudo response IOCB */
8582 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8583 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8584 spin_unlock_irqrestore(&phba->hbalock, iflags);
8585
8586 if (unlikely(!cmdiocbq)) {
8587 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8588 "0386 ELS complete with no corresponding "
8589 "cmdiocb: iotag (%d)\n",
8590 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8591 return workposted;
8592 }
8593
8594 /* Fake the irspiocbq and copy necessary response information */
8595 irspiocbq = lpfc_sli_get_iocbq(phba);
8596 if (!irspiocbq) {
8597 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8598 "0387 Failed to allocate an iocbq\n");
8599 return workposted;
8600 }
8601 lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8602
8603 /* Add the irspiocb to the response IOCB work list */
8604 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008605 list_add_tail(&irspiocbq->cq_event.list,
8606 &phba->sli4_hba.sp_rspiocb_work_queue);
James Smart4f774512009-05-22 14:52:35 -04008607 /* Indicate ELS ring attention */
8608 phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
8609 spin_unlock_irqrestore(&phba->hbalock, iflags);
8610 workposted = true;
8611
8612 return workposted;
8613}
8614
8615/**
8616 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8617 * @phba: Pointer to HBA context object.
8618 * @wcqe: Pointer to work-queue completion queue entry.
8619 *
8620 * This routine handles slow-path WQ entry comsumed event by invoking the
8621 * proper WQ release routine to the slow-path WQ.
8622 **/
8623static void
8624lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8625 struct lpfc_wcqe_release *wcqe)
8626{
8627 /* Check for the slow-path ELS work queue */
8628 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8629 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8630 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8631 else
8632 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8633 "2579 Slow-path wqe consume event carries "
8634 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8635 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8636 phba->sli4_hba.els_wq->queue_id);
8637}
8638
8639/**
8640 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8641 * @phba: Pointer to HBA context object.
8642 * @cq: Pointer to a WQ completion queue.
8643 * @wcqe: Pointer to work-queue completion queue entry.
8644 *
8645 * This routine handles an XRI abort event.
8646 *
8647 * Return: true if work posted to worker thread, otherwise false.
8648 **/
8649static bool
8650lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8651 struct lpfc_queue *cq,
8652 struct sli4_wcqe_xri_aborted *wcqe)
8653{
8654 bool workposted = false;
8655 struct lpfc_cq_event *cq_event;
8656 unsigned long iflags;
8657
8658 /* Allocate a new internal CQ_EVENT entry */
8659 cq_event = lpfc_sli4_cq_event_alloc(phba);
8660 if (!cq_event) {
8661 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8662 "0602 Failed to allocate CQ_EVENT entry\n");
8663 return false;
8664 }
8665
8666 /* Move the CQE into the proper xri abort event list */
8667 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8668 switch (cq->subtype) {
8669 case LPFC_FCP:
8670 spin_lock_irqsave(&phba->hbalock, iflags);
8671 list_add_tail(&cq_event->list,
8672 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8673 /* Set the fcp xri abort event flag */
8674 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8675 spin_unlock_irqrestore(&phba->hbalock, iflags);
8676 workposted = true;
8677 break;
8678 case LPFC_ELS:
8679 spin_lock_irqsave(&phba->hbalock, iflags);
8680 list_add_tail(&cq_event->list,
8681 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8682 /* Set the els xri abort event flag */
8683 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8684 spin_unlock_irqrestore(&phba->hbalock, iflags);
8685 workposted = true;
8686 break;
8687 default:
8688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8689 "0603 Invalid work queue CQE subtype (x%x)\n",
8690 cq->subtype);
8691 workposted = false;
8692 break;
8693 }
8694 return workposted;
8695}
8696
8697/**
James Smart4d9ab992009-10-02 15:16:39 -04008698 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008699 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008700 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008701 *
James Smart4d9ab992009-10-02 15:16:39 -04008702 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008703 *
8704 * Return: true if work posted to worker thread, otherwise false.
8705 **/
8706static bool
James Smart4d9ab992009-10-02 15:16:39 -04008707lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8708{
8709 bool workposted = false;
8710 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8711 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8712 struct hbq_dmabuf *dma_buf;
8713 uint32_t status;
8714 unsigned long iflags;
8715
8716 lpfc_sli4_rq_release(hrq, drq);
8717 if (bf_get(lpfc_rcqe_code, rcqe) != CQE_CODE_RECEIVE)
8718 goto out;
8719 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8720 goto out;
8721
8722 status = bf_get(lpfc_rcqe_status, rcqe);
8723 switch (status) {
8724 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8725 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8726 "2537 Receive Frame Truncated!!\n");
8727 case FC_STATUS_RQ_SUCCESS:
8728 spin_lock_irqsave(&phba->hbalock, iflags);
8729 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8730 if (!dma_buf) {
8731 spin_unlock_irqrestore(&phba->hbalock, iflags);
8732 goto out;
8733 }
8734 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8735 /* save off the frame for the word thread to process */
8736 list_add_tail(&dma_buf->cq_event.list,
8737 &phba->sli4_hba.sp_rspiocb_work_queue);
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_cqe - Process a slow path completion queue entry
8759 * @phba: Pointer to HBA context object.
8760 * @cq: Pointer to the completion queue.
8761 * @wcqe: Pointer to a completion queue entry.
8762 *
8763 * This routine process a slow-path work-queue or recieve queue completion queue
8764 * entry.
8765 *
8766 * Return: true if work posted to worker thread, otherwise false.
8767 **/
8768static bool
8769lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008770 struct lpfc_cqe *cqe)
8771{
8772 struct lpfc_wcqe_complete wcqe;
8773 bool workposted = false;
8774
8775 /* Copy the work queue CQE and convert endian order if needed */
8776 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8777
8778 /* Check and process for different type of WCQE and dispatch */
8779 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8780 case CQE_CODE_COMPL_WQE:
8781 /* Process the WQ complete event */
8782 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8783 (struct lpfc_wcqe_complete *)&wcqe);
8784 break;
8785 case CQE_CODE_RELEASE_WQE:
8786 /* Process the WQ release event */
8787 lpfc_sli4_sp_handle_rel_wcqe(phba,
8788 (struct lpfc_wcqe_release *)&wcqe);
8789 break;
8790 case CQE_CODE_XRI_ABORTED:
8791 /* Process the WQ XRI abort event */
8792 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8793 (struct sli4_wcqe_xri_aborted *)&wcqe);
8794 break;
James Smart4d9ab992009-10-02 15:16:39 -04008795 case CQE_CODE_RECEIVE:
8796 /* Process the RQ event */
8797 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8798 (struct lpfc_rcqe *)&wcqe);
8799 break;
James Smart4f774512009-05-22 14:52:35 -04008800 default:
8801 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8802 "0388 Not a valid WCQE code: x%x\n",
8803 bf_get(lpfc_wcqe_c_code, &wcqe));
8804 break;
8805 }
8806 return workposted;
8807}
8808
8809/**
James Smart4f774512009-05-22 14:52:35 -04008810 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8811 * @phba: Pointer to HBA context object.
8812 * @eqe: Pointer to fast-path event queue entry.
8813 *
8814 * This routine process a event queue entry from the slow-path event queue.
8815 * It will check the MajorCode and MinorCode to determine this is for a
8816 * completion event on a completion queue, if not, an error shall be logged
8817 * and just return. Otherwise, it will get to the corresponding completion
8818 * queue and process all the entries on that completion queue, rearm the
8819 * completion queue, and then return.
8820 *
8821 **/
8822static void
8823lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8824{
8825 struct lpfc_queue *cq = NULL, *childq, *speq;
8826 struct lpfc_cqe *cqe;
8827 bool workposted = false;
8828 int ecount = 0;
8829 uint16_t cqid;
8830
8831 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8832 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8833 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8834 "0359 Not a valid slow-path completion "
8835 "event: majorcode=x%x, minorcode=x%x\n",
8836 bf_get(lpfc_eqe_major_code, eqe),
8837 bf_get(lpfc_eqe_minor_code, eqe));
8838 return;
8839 }
8840
8841 /* Get the reference to the corresponding CQ */
8842 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8843
8844 /* Search for completion queue pointer matching this cqid */
8845 speq = phba->sli4_hba.sp_eq;
8846 list_for_each_entry(childq, &speq->child_list, list) {
8847 if (childq->queue_id == cqid) {
8848 cq = childq;
8849 break;
8850 }
8851 }
8852 if (unlikely(!cq)) {
8853 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8854 "0365 Slow-path CQ identifier (%d) does "
8855 "not exist\n", cqid);
8856 return;
8857 }
8858
8859 /* Process all the entries to the CQ */
8860 switch (cq->type) {
8861 case LPFC_MCQ:
8862 while ((cqe = lpfc_sli4_cq_get(cq))) {
8863 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8864 if (!(++ecount % LPFC_GET_QE_REL_INT))
8865 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8866 }
8867 break;
8868 case LPFC_WCQ:
8869 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04008870 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04008871 if (!(++ecount % LPFC_GET_QE_REL_INT))
8872 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8873 }
8874 break;
8875 default:
8876 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8877 "0370 Invalid completion queue type (%d)\n",
8878 cq->type);
8879 return;
8880 }
8881
8882 /* Catch the no cq entry condition, log an error */
8883 if (unlikely(ecount == 0))
8884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8885 "0371 No entry from the CQ: identifier "
8886 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8887
8888 /* In any case, flash and re-arm the RCQ */
8889 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8890
8891 /* wake up worker thread if there are works to be done */
8892 if (workposted)
8893 lpfc_worker_wake_up(phba);
8894}
8895
8896/**
8897 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8898 * @eqe: Pointer to fast-path completion queue entry.
8899 *
8900 * This routine process a fast-path work queue completion entry from fast-path
8901 * event queue for FCP command response completion.
8902 **/
8903static void
8904lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8905 struct lpfc_wcqe_complete *wcqe)
8906{
8907 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8908 struct lpfc_iocbq *cmdiocbq;
8909 struct lpfc_iocbq irspiocbq;
8910 unsigned long iflags;
8911
8912 spin_lock_irqsave(&phba->hbalock, iflags);
8913 pring->stats.iocb_event++;
8914 spin_unlock_irqrestore(&phba->hbalock, iflags);
8915
8916 /* Check for response status */
8917 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8918 /* If resource errors reported from HBA, reduce queue
8919 * depth of the SCSI device.
8920 */
8921 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8922 IOSTAT_LOCAL_REJECT) &&
8923 (wcqe->parameter == IOERR_NO_RESOURCES)) {
8924 phba->lpfc_rampdown_queue_depth(phba);
8925 }
8926 /* Log the error status */
8927 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8928 "0373 FCP complete error: status=x%x, "
8929 "hw_status=x%x, total_data_specified=%d, "
8930 "parameter=x%x, word3=x%x\n",
8931 bf_get(lpfc_wcqe_c_status, wcqe),
8932 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8933 wcqe->total_data_placed, wcqe->parameter,
8934 wcqe->word3);
8935 }
8936
8937 /* Look up the FCP command IOCB and create pseudo response IOCB */
8938 spin_lock_irqsave(&phba->hbalock, iflags);
8939 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8940 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8941 spin_unlock_irqrestore(&phba->hbalock, iflags);
8942 if (unlikely(!cmdiocbq)) {
8943 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8944 "0374 FCP complete with no corresponding "
8945 "cmdiocb: iotag (%d)\n",
8946 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8947 return;
8948 }
8949 if (unlikely(!cmdiocbq->iocb_cmpl)) {
8950 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8951 "0375 FCP cmdiocb not callback function "
8952 "iotag: (%d)\n",
8953 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8954 return;
8955 }
8956
8957 /* Fake the irspiocb and copy necessary response information */
8958 lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8959
8960 /* Pass the cmd_iocb and the rsp state to the upper layer */
8961 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8962}
8963
8964/**
8965 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8966 * @phba: Pointer to HBA context object.
8967 * @cq: Pointer to completion queue.
8968 * @wcqe: Pointer to work-queue completion queue entry.
8969 *
8970 * This routine handles an fast-path WQ entry comsumed event by invoking the
8971 * proper WQ release routine to the slow-path WQ.
8972 **/
8973static void
8974lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8975 struct lpfc_wcqe_release *wcqe)
8976{
8977 struct lpfc_queue *childwq;
8978 bool wqid_matched = false;
8979 uint16_t fcp_wqid;
8980
8981 /* Check for fast-path FCP work queue release */
8982 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
8983 list_for_each_entry(childwq, &cq->child_list, list) {
8984 if (childwq->queue_id == fcp_wqid) {
8985 lpfc_sli4_wq_release(childwq,
8986 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8987 wqid_matched = true;
8988 break;
8989 }
8990 }
8991 /* Report warning log message if no match found */
8992 if (wqid_matched != true)
8993 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8994 "2580 Fast-path wqe consume event carries "
8995 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
8996}
8997
8998/**
8999 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9000 * @cq: Pointer to the completion queue.
9001 * @eqe: Pointer to fast-path completion queue entry.
9002 *
9003 * This routine process a fast-path work queue completion entry from fast-path
9004 * event queue for FCP command response completion.
9005 **/
9006static int
9007lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9008 struct lpfc_cqe *cqe)
9009{
9010 struct lpfc_wcqe_release wcqe;
9011 bool workposted = false;
9012
9013 /* Copy the work queue CQE and convert endian order if needed */
9014 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9015
9016 /* Check and process for different type of WCQE and dispatch */
9017 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9018 case CQE_CODE_COMPL_WQE:
9019 /* Process the WQ complete event */
9020 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9021 (struct lpfc_wcqe_complete *)&wcqe);
9022 break;
9023 case CQE_CODE_RELEASE_WQE:
9024 /* Process the WQ release event */
9025 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9026 (struct lpfc_wcqe_release *)&wcqe);
9027 break;
9028 case CQE_CODE_XRI_ABORTED:
9029 /* Process the WQ XRI abort event */
9030 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9031 (struct sli4_wcqe_xri_aborted *)&wcqe);
9032 break;
9033 default:
9034 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9035 "0144 Not a valid WCQE code: x%x\n",
9036 bf_get(lpfc_wcqe_c_code, &wcqe));
9037 break;
9038 }
9039 return workposted;
9040}
9041
9042/**
9043 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9044 * @phba: Pointer to HBA context object.
9045 * @eqe: Pointer to fast-path event queue entry.
9046 *
9047 * This routine process a event queue entry from the fast-path event queue.
9048 * It will check the MajorCode and MinorCode to determine this is for a
9049 * completion event on a completion queue, if not, an error shall be logged
9050 * and just return. Otherwise, it will get to the corresponding completion
9051 * queue and process all the entries on the completion queue, rearm the
9052 * completion queue, and then return.
9053 **/
9054static void
9055lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9056 uint32_t fcp_cqidx)
9057{
9058 struct lpfc_queue *cq;
9059 struct lpfc_cqe *cqe;
9060 bool workposted = false;
9061 uint16_t cqid;
9062 int ecount = 0;
9063
9064 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9065 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9067 "0366 Not a valid fast-path completion "
9068 "event: majorcode=x%x, minorcode=x%x\n",
9069 bf_get(lpfc_eqe_major_code, eqe),
9070 bf_get(lpfc_eqe_minor_code, eqe));
9071 return;
9072 }
9073
9074 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9075 if (unlikely(!cq)) {
9076 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9077 "0367 Fast-path completion queue does not "
9078 "exist\n");
9079 return;
9080 }
9081
9082 /* Get the reference to the corresponding CQ */
9083 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9084 if (unlikely(cqid != cq->queue_id)) {
9085 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9086 "0368 Miss-matched fast-path completion "
9087 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9088 cqid, cq->queue_id);
9089 return;
9090 }
9091
9092 /* Process all the entries to the CQ */
9093 while ((cqe = lpfc_sli4_cq_get(cq))) {
9094 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9095 if (!(++ecount % LPFC_GET_QE_REL_INT))
9096 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9097 }
9098
9099 /* Catch the no cq entry condition */
9100 if (unlikely(ecount == 0))
9101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9102 "0369 No entry from fast-path completion "
9103 "queue fcpcqid=%d\n", cq->queue_id);
9104
9105 /* In any case, flash and re-arm the CQ */
9106 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9107
9108 /* wake up worker thread if there are works to be done */
9109 if (workposted)
9110 lpfc_worker_wake_up(phba);
9111}
9112
9113static void
9114lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9115{
9116 struct lpfc_eqe *eqe;
9117
9118 /* walk all the EQ entries and drop on the floor */
9119 while ((eqe = lpfc_sli4_eq_get(eq)))
9120 ;
9121
9122 /* Clear and re-arm the EQ */
9123 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9124}
9125
9126/**
9127 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9128 * @irq: Interrupt number.
9129 * @dev_id: The device context pointer.
9130 *
9131 * This function is directly called from the PCI layer as an interrupt
9132 * service routine when device with SLI-4 interface spec is enabled with
9133 * MSI-X multi-message interrupt mode and there are slow-path events in
9134 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9135 * interrupt mode, this function is called as part of the device-level
9136 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9137 * undergoing initialization, the interrupt handler will not process the
9138 * interrupt. The link attention and ELS ring attention events are handled
9139 * by the worker thread. The interrupt handler signals the worker thread
9140 * and returns for these events. This function is called without any lock
9141 * held. It gets the hbalock to access and update SLI data structures.
9142 *
9143 * This function returns IRQ_HANDLED when interrupt is handled else it
9144 * returns IRQ_NONE.
9145 **/
9146irqreturn_t
9147lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9148{
9149 struct lpfc_hba *phba;
9150 struct lpfc_queue *speq;
9151 struct lpfc_eqe *eqe;
9152 unsigned long iflag;
9153 int ecount = 0;
9154
9155 /*
9156 * Get the driver's phba structure from the dev_id
9157 */
9158 phba = (struct lpfc_hba *)dev_id;
9159
9160 if (unlikely(!phba))
9161 return IRQ_NONE;
9162
9163 /* Get to the EQ struct associated with this vector */
9164 speq = phba->sli4_hba.sp_eq;
9165
9166 /* Check device state for handling interrupt */
9167 if (unlikely(lpfc_intr_state_check(phba))) {
9168 /* Check again for link_state with lock held */
9169 spin_lock_irqsave(&phba->hbalock, iflag);
9170 if (phba->link_state < LPFC_LINK_DOWN)
9171 /* Flush, clear interrupt, and rearm the EQ */
9172 lpfc_sli4_eq_flush(phba, speq);
9173 spin_unlock_irqrestore(&phba->hbalock, iflag);
9174 return IRQ_NONE;
9175 }
9176
9177 /*
9178 * Process all the event on FCP slow-path EQ
9179 */
9180 while ((eqe = lpfc_sli4_eq_get(speq))) {
9181 lpfc_sli4_sp_handle_eqe(phba, eqe);
9182 if (!(++ecount % LPFC_GET_QE_REL_INT))
9183 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9184 }
9185
9186 /* Always clear and re-arm the slow-path EQ */
9187 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9188
9189 /* Catch the no cq entry condition */
9190 if (unlikely(ecount == 0)) {
9191 if (phba->intr_type == MSIX)
9192 /* MSI-X treated interrupt served as no EQ share INT */
9193 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9194 "0357 MSI-X interrupt with no EQE\n");
9195 else
9196 /* Non MSI-X treated on interrupt as EQ share INT */
9197 return IRQ_NONE;
9198 }
9199
9200 return IRQ_HANDLED;
9201} /* lpfc_sli4_sp_intr_handler */
9202
9203/**
9204 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9205 * @irq: Interrupt number.
9206 * @dev_id: The device context pointer.
9207 *
9208 * This function is directly called from the PCI layer as an interrupt
9209 * service routine when device with SLI-4 interface spec is enabled with
9210 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9211 * ring event in the HBA. However, when the device is enabled with either
9212 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9213 * device-level interrupt handler. When the PCI slot is in error recovery
9214 * or the HBA is undergoing initialization, the interrupt handler will not
9215 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9216 * the intrrupt context. This function is called without any lock held.
9217 * It gets the hbalock to access and update SLI data structures. Note that,
9218 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9219 * equal to that of FCP CQ index.
9220 *
9221 * This function returns IRQ_HANDLED when interrupt is handled else it
9222 * returns IRQ_NONE.
9223 **/
9224irqreturn_t
9225lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9226{
9227 struct lpfc_hba *phba;
9228 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9229 struct lpfc_queue *fpeq;
9230 struct lpfc_eqe *eqe;
9231 unsigned long iflag;
9232 int ecount = 0;
9233 uint32_t fcp_eqidx;
9234
9235 /* Get the driver's phba structure from the dev_id */
9236 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9237 phba = fcp_eq_hdl->phba;
9238 fcp_eqidx = fcp_eq_hdl->idx;
9239
9240 if (unlikely(!phba))
9241 return IRQ_NONE;
9242
9243 /* Get to the EQ struct associated with this vector */
9244 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9245
9246 /* Check device state for handling interrupt */
9247 if (unlikely(lpfc_intr_state_check(phba))) {
9248 /* Check again for link_state with lock held */
9249 spin_lock_irqsave(&phba->hbalock, iflag);
9250 if (phba->link_state < LPFC_LINK_DOWN)
9251 /* Flush, clear interrupt, and rearm the EQ */
9252 lpfc_sli4_eq_flush(phba, fpeq);
9253 spin_unlock_irqrestore(&phba->hbalock, iflag);
9254 return IRQ_NONE;
9255 }
9256
9257 /*
9258 * Process all the event on FCP fast-path EQ
9259 */
9260 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9261 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9262 if (!(++ecount % LPFC_GET_QE_REL_INT))
9263 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9264 }
9265
9266 /* Always clear and re-arm the fast-path EQ */
9267 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9268
9269 if (unlikely(ecount == 0)) {
9270 if (phba->intr_type == MSIX)
9271 /* MSI-X treated interrupt served as no EQ share INT */
9272 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9273 "0358 MSI-X interrupt with no EQE\n");
9274 else
9275 /* Non MSI-X treated on interrupt as EQ share INT */
9276 return IRQ_NONE;
9277 }
9278
9279 return IRQ_HANDLED;
9280} /* lpfc_sli4_fp_intr_handler */
9281
9282/**
9283 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9284 * @irq: Interrupt number.
9285 * @dev_id: The device context pointer.
9286 *
9287 * This function is the device-level interrupt handler to device with SLI-4
9288 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9289 * interrupt mode is enabled and there is an event in the HBA which requires
9290 * driver attention. This function invokes the slow-path interrupt attention
9291 * handling function and fast-path interrupt attention handling function in
9292 * turn to process the relevant HBA attention events. This function is called
9293 * without any lock held. It gets the hbalock to access and update SLI data
9294 * structures.
9295 *
9296 * This function returns IRQ_HANDLED when interrupt is handled, else it
9297 * returns IRQ_NONE.
9298 **/
9299irqreturn_t
9300lpfc_sli4_intr_handler(int irq, void *dev_id)
9301{
9302 struct lpfc_hba *phba;
9303 irqreturn_t sp_irq_rc, fp_irq_rc;
9304 bool fp_handled = false;
9305 uint32_t fcp_eqidx;
9306
9307 /* Get the driver's phba structure from the dev_id */
9308 phba = (struct lpfc_hba *)dev_id;
9309
9310 if (unlikely(!phba))
9311 return IRQ_NONE;
9312
9313 /*
9314 * Invokes slow-path host attention interrupt handling as appropriate.
9315 */
9316 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9317
9318 /*
9319 * Invoke fast-path host attention interrupt handling as appropriate.
9320 */
9321 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9322 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9323 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9324 if (fp_irq_rc == IRQ_HANDLED)
9325 fp_handled |= true;
9326 }
9327
9328 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9329} /* lpfc_sli4_intr_handler */
9330
9331/**
9332 * lpfc_sli4_queue_free - free a queue structure and associated memory
9333 * @queue: The queue structure to free.
9334 *
9335 * This function frees a queue structure and the DMAable memeory used for
9336 * the host resident queue. This function must be called after destroying the
9337 * queue on the HBA.
9338 **/
9339void
9340lpfc_sli4_queue_free(struct lpfc_queue *queue)
9341{
9342 struct lpfc_dmabuf *dmabuf;
9343
9344 if (!queue)
9345 return;
9346
9347 while (!list_empty(&queue->page_list)) {
9348 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9349 list);
9350 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9351 dmabuf->virt, dmabuf->phys);
9352 kfree(dmabuf);
9353 }
9354 kfree(queue);
9355 return;
9356}
9357
9358/**
9359 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9360 * @phba: The HBA that this queue is being created on.
9361 * @entry_size: The size of each queue entry for this queue.
9362 * @entry count: The number of entries that this queue will handle.
9363 *
9364 * This function allocates a queue structure and the DMAable memory used for
9365 * the host resident queue. This function must be called before creating the
9366 * queue on the HBA.
9367 **/
9368struct lpfc_queue *
9369lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9370 uint32_t entry_count)
9371{
9372 struct lpfc_queue *queue;
9373 struct lpfc_dmabuf *dmabuf;
9374 int x, total_qe_count;
9375 void *dma_pointer;
9376
9377
9378 queue = kzalloc(sizeof(struct lpfc_queue) +
9379 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9380 if (!queue)
9381 return NULL;
9382 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9383 INIT_LIST_HEAD(&queue->list);
9384 INIT_LIST_HEAD(&queue->page_list);
9385 INIT_LIST_HEAD(&queue->child_list);
9386 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9387 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9388 if (!dmabuf)
9389 goto out_fail;
9390 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9391 PAGE_SIZE, &dmabuf->phys,
9392 GFP_KERNEL);
9393 if (!dmabuf->virt) {
9394 kfree(dmabuf);
9395 goto out_fail;
9396 }
James Smartd11e31d2009-06-10 17:23:06 -04009397 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009398 dmabuf->buffer_tag = x;
9399 list_add_tail(&dmabuf->list, &queue->page_list);
9400 /* initialize queue's entry array */
9401 dma_pointer = dmabuf->virt;
9402 for (; total_qe_count < entry_count &&
9403 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9404 total_qe_count++, dma_pointer += entry_size) {
9405 queue->qe[total_qe_count].address = dma_pointer;
9406 }
9407 }
9408 queue->entry_size = entry_size;
9409 queue->entry_count = entry_count;
9410 queue->phba = phba;
9411
9412 return queue;
9413out_fail:
9414 lpfc_sli4_queue_free(queue);
9415 return NULL;
9416}
9417
9418/**
9419 * lpfc_eq_create - Create an Event Queue on the HBA
9420 * @phba: HBA structure that indicates port to create a queue on.
9421 * @eq: The queue structure to use to create the event queue.
9422 * @imax: The maximum interrupt per second limit.
9423 *
9424 * This function creates an event queue, as detailed in @eq, on a port,
9425 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9426 *
9427 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9428 * is used to get the entry count and entry size that are necessary to
9429 * determine the number of pages to allocate and use for this queue. This
9430 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9431 * event queue. This function is asynchronous and will wait for the mailbox
9432 * command to finish before continuing.
9433 *
9434 * On success this function will return a zero. If unable to allocate enough
9435 * memory this function will return ENOMEM. If the queue create mailbox command
9436 * fails this function will return ENXIO.
9437 **/
9438uint32_t
9439lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9440{
9441 struct lpfc_mbx_eq_create *eq_create;
9442 LPFC_MBOXQ_t *mbox;
9443 int rc, length, status = 0;
9444 struct lpfc_dmabuf *dmabuf;
9445 uint32_t shdr_status, shdr_add_status;
9446 union lpfc_sli4_cfg_shdr *shdr;
9447 uint16_t dmult;
9448
9449 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9450 if (!mbox)
9451 return -ENOMEM;
9452 length = (sizeof(struct lpfc_mbx_eq_create) -
9453 sizeof(struct lpfc_sli4_cfg_mhdr));
9454 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9455 LPFC_MBOX_OPCODE_EQ_CREATE,
9456 length, LPFC_SLI4_MBX_EMBED);
9457 eq_create = &mbox->u.mqe.un.eq_create;
9458 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9459 eq->page_count);
9460 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9461 LPFC_EQE_SIZE);
9462 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9463 /* Calculate delay multiper from maximum interrupt per second */
9464 dmult = LPFC_DMULT_CONST/imax - 1;
9465 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9466 dmult);
9467 switch (eq->entry_count) {
9468 default:
9469 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9470 "0360 Unsupported EQ count. (%d)\n",
9471 eq->entry_count);
9472 if (eq->entry_count < 256)
9473 return -EINVAL;
9474 /* otherwise default to smallest count (drop through) */
9475 case 256:
9476 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9477 LPFC_EQ_CNT_256);
9478 break;
9479 case 512:
9480 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9481 LPFC_EQ_CNT_512);
9482 break;
9483 case 1024:
9484 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9485 LPFC_EQ_CNT_1024);
9486 break;
9487 case 2048:
9488 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9489 LPFC_EQ_CNT_2048);
9490 break;
9491 case 4096:
9492 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9493 LPFC_EQ_CNT_4096);
9494 break;
9495 }
9496 list_for_each_entry(dmabuf, &eq->page_list, list) {
9497 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9498 putPaddrLow(dmabuf->phys);
9499 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9500 putPaddrHigh(dmabuf->phys);
9501 }
9502 mbox->vport = phba->pport;
9503 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9504 mbox->context1 = NULL;
9505 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9506 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9507 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9508 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9509 if (shdr_status || shdr_add_status || rc) {
9510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9511 "2500 EQ_CREATE mailbox failed with "
9512 "status x%x add_status x%x, mbx status x%x\n",
9513 shdr_status, shdr_add_status, rc);
9514 status = -ENXIO;
9515 }
9516 eq->type = LPFC_EQ;
9517 eq->subtype = LPFC_NONE;
9518 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9519 if (eq->queue_id == 0xFFFF)
9520 status = -ENXIO;
9521 eq->host_index = 0;
9522 eq->hba_index = 0;
9523
James Smart8fa38512009-07-19 10:01:03 -04009524 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009525 return status;
9526}
9527
9528/**
9529 * lpfc_cq_create - Create a Completion Queue on the HBA
9530 * @phba: HBA structure that indicates port to create a queue on.
9531 * @cq: The queue structure to use to create the completion queue.
9532 * @eq: The event queue to bind this completion queue to.
9533 *
9534 * This function creates a completion queue, as detailed in @wq, on a port,
9535 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9536 *
9537 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9538 * is used to get the entry count and entry size that are necessary to
9539 * determine the number of pages to allocate and use for this queue. The @eq
9540 * is used to indicate which event queue to bind this completion queue to. This
9541 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9542 * completion queue. This function is asynchronous and will wait for the mailbox
9543 * command to finish before continuing.
9544 *
9545 * On success this function will return a zero. If unable to allocate enough
9546 * memory this function will return ENOMEM. If the queue create mailbox command
9547 * fails this function will return ENXIO.
9548 **/
9549uint32_t
9550lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9551 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9552{
9553 struct lpfc_mbx_cq_create *cq_create;
9554 struct lpfc_dmabuf *dmabuf;
9555 LPFC_MBOXQ_t *mbox;
9556 int rc, length, status = 0;
9557 uint32_t shdr_status, shdr_add_status;
9558 union lpfc_sli4_cfg_shdr *shdr;
9559
9560 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9561 if (!mbox)
9562 return -ENOMEM;
9563 length = (sizeof(struct lpfc_mbx_cq_create) -
9564 sizeof(struct lpfc_sli4_cfg_mhdr));
9565 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9566 LPFC_MBOX_OPCODE_CQ_CREATE,
9567 length, LPFC_SLI4_MBX_EMBED);
9568 cq_create = &mbox->u.mqe.un.cq_create;
9569 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9570 cq->page_count);
9571 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9572 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9573 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9574 switch (cq->entry_count) {
9575 default:
9576 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9577 "0361 Unsupported CQ count. (%d)\n",
9578 cq->entry_count);
9579 if (cq->entry_count < 256)
9580 return -EINVAL;
9581 /* otherwise default to smallest count (drop through) */
9582 case 256:
9583 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9584 LPFC_CQ_CNT_256);
9585 break;
9586 case 512:
9587 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9588 LPFC_CQ_CNT_512);
9589 break;
9590 case 1024:
9591 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9592 LPFC_CQ_CNT_1024);
9593 break;
9594 }
9595 list_for_each_entry(dmabuf, &cq->page_list, list) {
9596 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9597 putPaddrLow(dmabuf->phys);
9598 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9599 putPaddrHigh(dmabuf->phys);
9600 }
9601 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9602
9603 /* The IOCTL status is embedded in the mailbox subheader. */
9604 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9605 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9606 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9607 if (shdr_status || shdr_add_status || rc) {
9608 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9609 "2501 CQ_CREATE mailbox failed with "
9610 "status x%x add_status x%x, mbx status x%x\n",
9611 shdr_status, shdr_add_status, rc);
9612 status = -ENXIO;
9613 goto out;
9614 }
9615 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9616 if (cq->queue_id == 0xFFFF) {
9617 status = -ENXIO;
9618 goto out;
9619 }
9620 /* link the cq onto the parent eq child list */
9621 list_add_tail(&cq->list, &eq->child_list);
9622 /* Set up completion queue's type and subtype */
9623 cq->type = type;
9624 cq->subtype = subtype;
9625 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9626 cq->host_index = 0;
9627 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009628
James Smart8fa38512009-07-19 10:01:03 -04009629out:
9630 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009631 return status;
9632}
9633
9634/**
James Smart04c68492009-05-22 14:52:52 -04009635 * lpfc_mq_create - Create a mailbox Queue on the HBA
9636 * @phba: HBA structure that indicates port to create a queue on.
9637 * @mq: The queue structure to use to create the mailbox queue.
9638 *
9639 * This function creates a mailbox queue, as detailed in @mq, on a port,
9640 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9641 *
9642 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9643 * is used to get the entry count and entry size that are necessary to
9644 * determine the number of pages to allocate and use for this queue. This
9645 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9646 * mailbox queue. This function is asynchronous and will wait for the mailbox
9647 * command to finish before continuing.
9648 *
9649 * On success this function will return a zero. If unable to allocate enough
9650 * memory this function will return ENOMEM. If the queue create mailbox command
9651 * fails this function will return ENXIO.
9652 **/
9653uint32_t
9654lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9655 struct lpfc_queue *cq, uint32_t subtype)
9656{
9657 struct lpfc_mbx_mq_create *mq_create;
9658 struct lpfc_dmabuf *dmabuf;
9659 LPFC_MBOXQ_t *mbox;
9660 int rc, length, status = 0;
9661 uint32_t shdr_status, shdr_add_status;
9662 union lpfc_sli4_cfg_shdr *shdr;
9663
9664 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9665 if (!mbox)
9666 return -ENOMEM;
9667 length = (sizeof(struct lpfc_mbx_mq_create) -
9668 sizeof(struct lpfc_sli4_cfg_mhdr));
9669 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9670 LPFC_MBOX_OPCODE_MQ_CREATE,
9671 length, LPFC_SLI4_MBX_EMBED);
9672 mq_create = &mbox->u.mqe.un.mq_create;
9673 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9674 mq->page_count);
9675 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9676 cq->queue_id);
9677 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9678 switch (mq->entry_count) {
9679 default:
9680 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9681 "0362 Unsupported MQ count. (%d)\n",
9682 mq->entry_count);
9683 if (mq->entry_count < 16)
9684 return -EINVAL;
9685 /* otherwise default to smallest count (drop through) */
9686 case 16:
9687 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9688 LPFC_MQ_CNT_16);
9689 break;
9690 case 32:
9691 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9692 LPFC_MQ_CNT_32);
9693 break;
9694 case 64:
9695 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9696 LPFC_MQ_CNT_64);
9697 break;
9698 case 128:
9699 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9700 LPFC_MQ_CNT_128);
9701 break;
9702 }
9703 list_for_each_entry(dmabuf, &mq->page_list, list) {
9704 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9705 putPaddrLow(dmabuf->phys);
9706 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9707 putPaddrHigh(dmabuf->phys);
9708 }
9709 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9710 /* The IOCTL status is embedded in the mailbox subheader. */
9711 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9712 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9713 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9714 if (shdr_status || shdr_add_status || rc) {
9715 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9716 "2502 MQ_CREATE mailbox failed with "
9717 "status x%x add_status x%x, mbx status x%x\n",
9718 shdr_status, shdr_add_status, rc);
9719 status = -ENXIO;
9720 goto out;
9721 }
9722 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9723 if (mq->queue_id == 0xFFFF) {
9724 status = -ENXIO;
9725 goto out;
9726 }
9727 mq->type = LPFC_MQ;
9728 mq->subtype = subtype;
9729 mq->host_index = 0;
9730 mq->hba_index = 0;
9731
9732 /* link the mq onto the parent cq child list */
9733 list_add_tail(&mq->list, &cq->child_list);
9734out:
James Smart8fa38512009-07-19 10:01:03 -04009735 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009736 return status;
9737}
9738
9739/**
James Smart4f774512009-05-22 14:52:35 -04009740 * lpfc_wq_create - Create a Work Queue on the HBA
9741 * @phba: HBA structure that indicates port to create a queue on.
9742 * @wq: The queue structure to use to create the work queue.
9743 * @cq: The completion queue to bind this work queue to.
9744 * @subtype: The subtype of the work queue indicating its functionality.
9745 *
9746 * This function creates a work queue, as detailed in @wq, on a port, described
9747 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9748 *
9749 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9750 * is used to get the entry count and entry size that are necessary to
9751 * determine the number of pages to allocate and use for this queue. The @cq
9752 * is used to indicate which completion queue to bind this work queue to. This
9753 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9754 * work queue. This function is asynchronous and will wait for the mailbox
9755 * command to finish before continuing.
9756 *
9757 * On success this function will return a zero. If unable to allocate enough
9758 * memory this function will return ENOMEM. If the queue create mailbox command
9759 * fails this function will return ENXIO.
9760 **/
9761uint32_t
9762lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9763 struct lpfc_queue *cq, uint32_t subtype)
9764{
9765 struct lpfc_mbx_wq_create *wq_create;
9766 struct lpfc_dmabuf *dmabuf;
9767 LPFC_MBOXQ_t *mbox;
9768 int rc, length, status = 0;
9769 uint32_t shdr_status, shdr_add_status;
9770 union lpfc_sli4_cfg_shdr *shdr;
9771
9772 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9773 if (!mbox)
9774 return -ENOMEM;
9775 length = (sizeof(struct lpfc_mbx_wq_create) -
9776 sizeof(struct lpfc_sli4_cfg_mhdr));
9777 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9778 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9779 length, LPFC_SLI4_MBX_EMBED);
9780 wq_create = &mbox->u.mqe.un.wq_create;
9781 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9782 wq->page_count);
9783 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9784 cq->queue_id);
9785 list_for_each_entry(dmabuf, &wq->page_list, list) {
9786 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9787 putPaddrLow(dmabuf->phys);
9788 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9789 putPaddrHigh(dmabuf->phys);
9790 }
9791 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9792 /* The IOCTL status is embedded in the mailbox subheader. */
9793 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9794 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9795 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9796 if (shdr_status || shdr_add_status || rc) {
9797 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9798 "2503 WQ_CREATE mailbox failed with "
9799 "status x%x add_status x%x, mbx status x%x\n",
9800 shdr_status, shdr_add_status, rc);
9801 status = -ENXIO;
9802 goto out;
9803 }
9804 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9805 if (wq->queue_id == 0xFFFF) {
9806 status = -ENXIO;
9807 goto out;
9808 }
9809 wq->type = LPFC_WQ;
9810 wq->subtype = subtype;
9811 wq->host_index = 0;
9812 wq->hba_index = 0;
9813
9814 /* link the wq onto the parent cq child list */
9815 list_add_tail(&wq->list, &cq->child_list);
9816out:
James Smart8fa38512009-07-19 10:01:03 -04009817 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009818 return status;
9819}
9820
9821/**
9822 * lpfc_rq_create - Create a Receive Queue on the HBA
9823 * @phba: HBA structure that indicates port to create a queue on.
9824 * @hrq: The queue structure to use to create the header receive queue.
9825 * @drq: The queue structure to use to create the data receive queue.
9826 * @cq: The completion queue to bind this work queue to.
9827 *
9828 * This function creates a receive buffer queue pair , as detailed in @hrq and
9829 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9830 * to the HBA.
9831 *
9832 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9833 * struct is used to get the entry count that is necessary to determine the
9834 * number of pages to use for this queue. The @cq is used to indicate which
9835 * completion queue to bind received buffers that are posted to these queues to.
9836 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9837 * receive queue pair. This function is asynchronous and will wait for the
9838 * mailbox command to finish before continuing.
9839 *
9840 * On success this function will return a zero. If unable to allocate enough
9841 * memory this function will return ENOMEM. If the queue create mailbox command
9842 * fails this function will return ENXIO.
9843 **/
9844uint32_t
9845lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9846 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9847{
9848 struct lpfc_mbx_rq_create *rq_create;
9849 struct lpfc_dmabuf *dmabuf;
9850 LPFC_MBOXQ_t *mbox;
9851 int rc, length, status = 0;
9852 uint32_t shdr_status, shdr_add_status;
9853 union lpfc_sli4_cfg_shdr *shdr;
9854
9855 if (hrq->entry_count != drq->entry_count)
9856 return -EINVAL;
9857 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9858 if (!mbox)
9859 return -ENOMEM;
9860 length = (sizeof(struct lpfc_mbx_rq_create) -
9861 sizeof(struct lpfc_sli4_cfg_mhdr));
9862 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9863 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9864 length, LPFC_SLI4_MBX_EMBED);
9865 rq_create = &mbox->u.mqe.un.rq_create;
9866 switch (hrq->entry_count) {
9867 default:
9868 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9869 "2535 Unsupported RQ count. (%d)\n",
9870 hrq->entry_count);
9871 if (hrq->entry_count < 512)
9872 return -EINVAL;
9873 /* otherwise default to smallest count (drop through) */
9874 case 512:
9875 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9876 LPFC_RQ_RING_SIZE_512);
9877 break;
9878 case 1024:
9879 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9880 LPFC_RQ_RING_SIZE_1024);
9881 break;
9882 case 2048:
9883 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9884 LPFC_RQ_RING_SIZE_2048);
9885 break;
9886 case 4096:
9887 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9888 LPFC_RQ_RING_SIZE_4096);
9889 break;
9890 }
9891 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9892 cq->queue_id);
9893 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9894 hrq->page_count);
9895 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9896 LPFC_HDR_BUF_SIZE);
9897 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9898 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9899 putPaddrLow(dmabuf->phys);
9900 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9901 putPaddrHigh(dmabuf->phys);
9902 }
9903 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9904 /* The IOCTL status is embedded in the mailbox subheader. */
9905 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9906 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9907 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9908 if (shdr_status || shdr_add_status || rc) {
9909 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9910 "2504 RQ_CREATE mailbox failed with "
9911 "status x%x add_status x%x, mbx status x%x\n",
9912 shdr_status, shdr_add_status, rc);
9913 status = -ENXIO;
9914 goto out;
9915 }
9916 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9917 if (hrq->queue_id == 0xFFFF) {
9918 status = -ENXIO;
9919 goto out;
9920 }
9921 hrq->type = LPFC_HRQ;
9922 hrq->subtype = subtype;
9923 hrq->host_index = 0;
9924 hrq->hba_index = 0;
9925
9926 /* now create the data queue */
9927 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9928 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9929 length, LPFC_SLI4_MBX_EMBED);
9930 switch (drq->entry_count) {
9931 default:
9932 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9933 "2536 Unsupported RQ count. (%d)\n",
9934 drq->entry_count);
9935 if (drq->entry_count < 512)
9936 return -EINVAL;
9937 /* otherwise default to smallest count (drop through) */
9938 case 512:
9939 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9940 LPFC_RQ_RING_SIZE_512);
9941 break;
9942 case 1024:
9943 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9944 LPFC_RQ_RING_SIZE_1024);
9945 break;
9946 case 2048:
9947 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9948 LPFC_RQ_RING_SIZE_2048);
9949 break;
9950 case 4096:
9951 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9952 LPFC_RQ_RING_SIZE_4096);
9953 break;
9954 }
9955 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9956 cq->queue_id);
9957 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9958 drq->page_count);
9959 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9960 LPFC_DATA_BUF_SIZE);
9961 list_for_each_entry(dmabuf, &drq->page_list, list) {
9962 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9963 putPaddrLow(dmabuf->phys);
9964 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9965 putPaddrHigh(dmabuf->phys);
9966 }
9967 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9968 /* The IOCTL status is embedded in the mailbox subheader. */
9969 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9970 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9971 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9972 if (shdr_status || shdr_add_status || rc) {
9973 status = -ENXIO;
9974 goto out;
9975 }
9976 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9977 if (drq->queue_id == 0xFFFF) {
9978 status = -ENXIO;
9979 goto out;
9980 }
9981 drq->type = LPFC_DRQ;
9982 drq->subtype = subtype;
9983 drq->host_index = 0;
9984 drq->hba_index = 0;
9985
9986 /* link the header and data RQs onto the parent cq child list */
9987 list_add_tail(&hrq->list, &cq->child_list);
9988 list_add_tail(&drq->list, &cq->child_list);
9989
9990out:
James Smart8fa38512009-07-19 10:01:03 -04009991 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009992 return status;
9993}
9994
9995/**
9996 * lpfc_eq_destroy - Destroy an event Queue on the HBA
9997 * @eq: The queue structure associated with the queue to destroy.
9998 *
9999 * This function destroys a queue, as detailed in @eq by sending an mailbox
10000 * command, specific to the type of queue, to the HBA.
10001 *
10002 * The @eq struct is used to get the queue ID of the queue to destroy.
10003 *
10004 * On success this function will return a zero. If the queue destroy mailbox
10005 * command fails this function will return ENXIO.
10006 **/
10007uint32_t
10008lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10009{
10010 LPFC_MBOXQ_t *mbox;
10011 int rc, length, status = 0;
10012 uint32_t shdr_status, shdr_add_status;
10013 union lpfc_sli4_cfg_shdr *shdr;
10014
10015 if (!eq)
10016 return -ENODEV;
10017 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10018 if (!mbox)
10019 return -ENOMEM;
10020 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10021 sizeof(struct lpfc_sli4_cfg_mhdr));
10022 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10023 LPFC_MBOX_OPCODE_EQ_DESTROY,
10024 length, LPFC_SLI4_MBX_EMBED);
10025 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10026 eq->queue_id);
10027 mbox->vport = eq->phba->pport;
10028 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10029
10030 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10031 /* The IOCTL status is embedded in the mailbox subheader. */
10032 shdr = (union lpfc_sli4_cfg_shdr *)
10033 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10034 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10035 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10036 if (shdr_status || shdr_add_status || rc) {
10037 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10038 "2505 EQ_DESTROY mailbox failed with "
10039 "status x%x add_status x%x, mbx status x%x\n",
10040 shdr_status, shdr_add_status, rc);
10041 status = -ENXIO;
10042 }
10043
10044 /* Remove eq from any list */
10045 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010046 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010047 return status;
10048}
10049
10050/**
10051 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10052 * @cq: The queue structure associated with the queue to destroy.
10053 *
10054 * This function destroys a queue, as detailed in @cq by sending an mailbox
10055 * command, specific to the type of queue, to the HBA.
10056 *
10057 * The @cq struct is used to get the queue ID of the queue to destroy.
10058 *
10059 * On success this function will return a zero. If the queue destroy mailbox
10060 * command fails this function will return ENXIO.
10061 **/
10062uint32_t
10063lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10064{
10065 LPFC_MBOXQ_t *mbox;
10066 int rc, length, status = 0;
10067 uint32_t shdr_status, shdr_add_status;
10068 union lpfc_sli4_cfg_shdr *shdr;
10069
10070 if (!cq)
10071 return -ENODEV;
10072 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10073 if (!mbox)
10074 return -ENOMEM;
10075 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10076 sizeof(struct lpfc_sli4_cfg_mhdr));
10077 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10078 LPFC_MBOX_OPCODE_CQ_DESTROY,
10079 length, LPFC_SLI4_MBX_EMBED);
10080 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10081 cq->queue_id);
10082 mbox->vport = cq->phba->pport;
10083 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10084 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10085 /* The IOCTL status is embedded in the mailbox subheader. */
10086 shdr = (union lpfc_sli4_cfg_shdr *)
10087 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10088 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10089 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10090 if (shdr_status || shdr_add_status || rc) {
10091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10092 "2506 CQ_DESTROY mailbox failed with "
10093 "status x%x add_status x%x, mbx status x%x\n",
10094 shdr_status, shdr_add_status, rc);
10095 status = -ENXIO;
10096 }
10097 /* Remove cq from any list */
10098 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010099 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010100 return status;
10101}
10102
10103/**
James Smart04c68492009-05-22 14:52:52 -040010104 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10105 * @qm: The queue structure associated with the queue to destroy.
10106 *
10107 * This function destroys a queue, as detailed in @mq by sending an mailbox
10108 * command, specific to the type of queue, to the HBA.
10109 *
10110 * The @mq struct is used to get the queue ID of the queue to destroy.
10111 *
10112 * On success this function will return a zero. If the queue destroy mailbox
10113 * command fails this function will return ENXIO.
10114 **/
10115uint32_t
10116lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10117{
10118 LPFC_MBOXQ_t *mbox;
10119 int rc, length, status = 0;
10120 uint32_t shdr_status, shdr_add_status;
10121 union lpfc_sli4_cfg_shdr *shdr;
10122
10123 if (!mq)
10124 return -ENODEV;
10125 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10126 if (!mbox)
10127 return -ENOMEM;
10128 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10129 sizeof(struct lpfc_sli4_cfg_mhdr));
10130 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10131 LPFC_MBOX_OPCODE_MQ_DESTROY,
10132 length, LPFC_SLI4_MBX_EMBED);
10133 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10134 mq->queue_id);
10135 mbox->vport = mq->phba->pport;
10136 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10137 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10138 /* The IOCTL status is embedded in the mailbox subheader. */
10139 shdr = (union lpfc_sli4_cfg_shdr *)
10140 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10141 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10142 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10143 if (shdr_status || shdr_add_status || rc) {
10144 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10145 "2507 MQ_DESTROY mailbox failed with "
10146 "status x%x add_status x%x, mbx status x%x\n",
10147 shdr_status, shdr_add_status, rc);
10148 status = -ENXIO;
10149 }
10150 /* Remove mq from any list */
10151 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010152 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010153 return status;
10154}
10155
10156/**
James Smart4f774512009-05-22 14:52:35 -040010157 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10158 * @wq: The queue structure associated with the queue to destroy.
10159 *
10160 * This function destroys a queue, as detailed in @wq by sending an mailbox
10161 * command, specific to the type of queue, to the HBA.
10162 *
10163 * The @wq struct is used to get the queue ID of the queue to destroy.
10164 *
10165 * On success this function will return a zero. If the queue destroy mailbox
10166 * command fails this function will return ENXIO.
10167 **/
10168uint32_t
10169lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10170{
10171 LPFC_MBOXQ_t *mbox;
10172 int rc, length, status = 0;
10173 uint32_t shdr_status, shdr_add_status;
10174 union lpfc_sli4_cfg_shdr *shdr;
10175
10176 if (!wq)
10177 return -ENODEV;
10178 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10179 if (!mbox)
10180 return -ENOMEM;
10181 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10182 sizeof(struct lpfc_sli4_cfg_mhdr));
10183 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10184 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10185 length, LPFC_SLI4_MBX_EMBED);
10186 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10187 wq->queue_id);
10188 mbox->vport = wq->phba->pport;
10189 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10190 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10191 shdr = (union lpfc_sli4_cfg_shdr *)
10192 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10193 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10194 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10195 if (shdr_status || shdr_add_status || rc) {
10196 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10197 "2508 WQ_DESTROY mailbox failed with "
10198 "status x%x add_status x%x, mbx status x%x\n",
10199 shdr_status, shdr_add_status, rc);
10200 status = -ENXIO;
10201 }
10202 /* Remove wq from any list */
10203 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010204 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010205 return status;
10206}
10207
10208/**
10209 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10210 * @rq: The queue structure associated with the queue to destroy.
10211 *
10212 * This function destroys a queue, as detailed in @rq by sending an mailbox
10213 * command, specific to the type of queue, to the HBA.
10214 *
10215 * The @rq struct is used to get the queue ID of the queue to destroy.
10216 *
10217 * On success this function will return a zero. If the queue destroy mailbox
10218 * command fails this function will return ENXIO.
10219 **/
10220uint32_t
10221lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10222 struct lpfc_queue *drq)
10223{
10224 LPFC_MBOXQ_t *mbox;
10225 int rc, length, status = 0;
10226 uint32_t shdr_status, shdr_add_status;
10227 union lpfc_sli4_cfg_shdr *shdr;
10228
10229 if (!hrq || !drq)
10230 return -ENODEV;
10231 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10232 if (!mbox)
10233 return -ENOMEM;
10234 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10235 sizeof(struct mbox_header));
10236 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10237 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10238 length, LPFC_SLI4_MBX_EMBED);
10239 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10240 hrq->queue_id);
10241 mbox->vport = hrq->phba->pport;
10242 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10243 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10244 /* The IOCTL status is embedded in the mailbox subheader. */
10245 shdr = (union lpfc_sli4_cfg_shdr *)
10246 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10247 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10248 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10249 if (shdr_status || shdr_add_status || rc) {
10250 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10251 "2509 RQ_DESTROY mailbox failed with "
10252 "status x%x add_status x%x, mbx status x%x\n",
10253 shdr_status, shdr_add_status, rc);
10254 if (rc != MBX_TIMEOUT)
10255 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10256 return -ENXIO;
10257 }
10258 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10259 drq->queue_id);
10260 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10261 shdr = (union lpfc_sli4_cfg_shdr *)
10262 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10263 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10264 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10265 if (shdr_status || shdr_add_status || rc) {
10266 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10267 "2510 RQ_DESTROY mailbox failed with "
10268 "status x%x add_status x%x, mbx status x%x\n",
10269 shdr_status, shdr_add_status, rc);
10270 status = -ENXIO;
10271 }
10272 list_del_init(&hrq->list);
10273 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010274 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010275 return status;
10276}
10277
10278/**
10279 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10280 * @phba: The virtual port for which this call being executed.
10281 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10282 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10283 * @xritag: the xritag that ties this io to the SGL pages.
10284 *
10285 * This routine will post the sgl pages for the IO that has the xritag
10286 * that is in the iocbq structure. The xritag is assigned during iocbq
10287 * creation and persists for as long as the driver is loaded.
10288 * if the caller has fewer than 256 scatter gather segments to map then
10289 * pdma_phys_addr1 should be 0.
10290 * If the caller needs to map more than 256 scatter gather segment then
10291 * pdma_phys_addr1 should be a valid physical address.
10292 * physical address for SGLs must be 64 byte aligned.
10293 * If you are going to map 2 SGL's then the first one must have 256 entries
10294 * the second sgl can have between 1 and 256 entries.
10295 *
10296 * Return codes:
10297 * 0 - Success
10298 * -ENXIO, -ENOMEM - Failure
10299 **/
10300int
10301lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10302 dma_addr_t pdma_phys_addr0,
10303 dma_addr_t pdma_phys_addr1,
10304 uint16_t xritag)
10305{
10306 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10307 LPFC_MBOXQ_t *mbox;
10308 int rc;
10309 uint32_t shdr_status, shdr_add_status;
10310 union lpfc_sli4_cfg_shdr *shdr;
10311
10312 if (xritag == NO_XRI) {
10313 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10314 "0364 Invalid param:\n");
10315 return -EINVAL;
10316 }
10317
10318 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10319 if (!mbox)
10320 return -ENOMEM;
10321
10322 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10323 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10324 sizeof(struct lpfc_mbx_post_sgl_pages) -
10325 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10326
10327 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10328 &mbox->u.mqe.un.post_sgl_pages;
10329 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10330 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10331
10332 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10333 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10334 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10335 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10336
10337 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10338 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10339 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10340 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10341 if (!phba->sli4_hba.intr_enable)
10342 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10343 else
10344 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10345 /* The IOCTL status is embedded in the mailbox subheader. */
10346 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10347 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10348 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10349 if (rc != MBX_TIMEOUT)
10350 mempool_free(mbox, phba->mbox_mem_pool);
10351 if (shdr_status || shdr_add_status || rc) {
10352 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10353 "2511 POST_SGL mailbox failed with "
10354 "status x%x add_status x%x, mbx status x%x\n",
10355 shdr_status, shdr_add_status, rc);
10356 rc = -ENXIO;
10357 }
10358 return 0;
10359}
10360/**
10361 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10362 * @phba: The virtual port for which this call being executed.
10363 *
10364 * This routine will remove all of the sgl pages registered with the hba.
10365 *
10366 * Return codes:
10367 * 0 - Success
10368 * -ENXIO, -ENOMEM - Failure
10369 **/
10370int
10371lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10372{
10373 LPFC_MBOXQ_t *mbox;
10374 int rc;
10375 uint32_t shdr_status, shdr_add_status;
10376 union lpfc_sli4_cfg_shdr *shdr;
10377
10378 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10379 if (!mbox)
10380 return -ENOMEM;
10381
10382 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10383 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10384 LPFC_SLI4_MBX_EMBED);
10385 if (!phba->sli4_hba.intr_enable)
10386 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10387 else
10388 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10389 /* The IOCTL status is embedded in the mailbox subheader. */
10390 shdr = (union lpfc_sli4_cfg_shdr *)
10391 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10392 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10393 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10394 if (rc != MBX_TIMEOUT)
10395 mempool_free(mbox, phba->mbox_mem_pool);
10396 if (shdr_status || shdr_add_status || rc) {
10397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10398 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10399 "status x%x add_status x%x, mbx status x%x\n",
10400 shdr_status, shdr_add_status, rc);
10401 rc = -ENXIO;
10402 }
10403 return rc;
10404}
10405
10406/**
10407 * lpfc_sli4_next_xritag - Get an xritag for the io
10408 * @phba: Pointer to HBA context object.
10409 *
10410 * This function gets an xritag for the iocb. If there is no unused xritag
10411 * it will return 0xffff.
10412 * The function returns the allocated xritag if successful, else returns zero.
10413 * Zero is not a valid xritag.
10414 * The caller is not required to hold any lock.
10415 **/
10416uint16_t
10417lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10418{
10419 uint16_t xritag;
10420
10421 spin_lock_irq(&phba->hbalock);
10422 xritag = phba->sli4_hba.next_xri;
10423 if ((xritag != (uint16_t) -1) && xritag <
10424 (phba->sli4_hba.max_cfg_param.max_xri
10425 + phba->sli4_hba.max_cfg_param.xri_base)) {
10426 phba->sli4_hba.next_xri++;
10427 phba->sli4_hba.max_cfg_param.xri_used++;
10428 spin_unlock_irq(&phba->hbalock);
10429 return xritag;
10430 }
10431 spin_unlock_irq(&phba->hbalock);
10432
10433 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10434 "2004 Failed to allocate XRI.last XRITAG is %d"
10435 " Max XRI is %d, Used XRI is %d\n",
10436 phba->sli4_hba.next_xri,
10437 phba->sli4_hba.max_cfg_param.max_xri,
10438 phba->sli4_hba.max_cfg_param.xri_used);
10439 return -1;
10440}
10441
10442/**
10443 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10444 * @phba: pointer to lpfc hba data structure.
10445 *
10446 * This routine is invoked to post a block of driver's sgl pages to the
10447 * HBA using non-embedded mailbox command. No Lock is held. This routine
10448 * is only called when the driver is loading and after all IO has been
10449 * stopped.
10450 **/
10451int
10452lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10453{
10454 struct lpfc_sglq *sglq_entry;
10455 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10456 struct sgl_page_pairs *sgl_pg_pairs;
10457 void *viraddr;
10458 LPFC_MBOXQ_t *mbox;
10459 uint32_t reqlen, alloclen, pg_pairs;
10460 uint32_t mbox_tmo;
10461 uint16_t xritag_start = 0;
10462 int els_xri_cnt, rc = 0;
10463 uint32_t shdr_status, shdr_add_status;
10464 union lpfc_sli4_cfg_shdr *shdr;
10465
10466 /* The number of sgls to be posted */
10467 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10468
10469 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10470 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10471 if (reqlen > PAGE_SIZE) {
10472 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10473 "2559 Block sgl registration required DMA "
10474 "size (%d) great than a page\n", reqlen);
10475 return -ENOMEM;
10476 }
10477 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10478 if (!mbox) {
10479 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10480 "2560 Failed to allocate mbox cmd memory\n");
10481 return -ENOMEM;
10482 }
10483
10484 /* Allocate DMA memory and set up the non-embedded mailbox command */
10485 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10486 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10487 LPFC_SLI4_MBX_NEMBED);
10488
10489 if (alloclen < reqlen) {
10490 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10491 "0285 Allocated DMA memory size (%d) is "
10492 "less than the requested DMA memory "
10493 "size (%d)\n", alloclen, reqlen);
10494 lpfc_sli4_mbox_cmd_free(phba, mbox);
10495 return -ENOMEM;
10496 }
10497
10498 /* Get the first SGE entry from the non-embedded DMA memory */
10499 if (unlikely(!mbox->sge_array)) {
10500 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10501 "2525 Failed to get the non-embedded SGE "
10502 "virtual address\n");
10503 lpfc_sli4_mbox_cmd_free(phba, mbox);
10504 return -ENOMEM;
10505 }
10506 viraddr = mbox->sge_array->addr[0];
10507
10508 /* Set up the SGL pages in the non-embedded DMA pages */
10509 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10510 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10511
10512 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10513 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10514 /* Set up the sge entry */
10515 sgl_pg_pairs->sgl_pg0_addr_lo =
10516 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10517 sgl_pg_pairs->sgl_pg0_addr_hi =
10518 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10519 sgl_pg_pairs->sgl_pg1_addr_lo =
10520 cpu_to_le32(putPaddrLow(0));
10521 sgl_pg_pairs->sgl_pg1_addr_hi =
10522 cpu_to_le32(putPaddrHigh(0));
10523 /* Keep the first xritag on the list */
10524 if (pg_pairs == 0)
10525 xritag_start = sglq_entry->sli4_xritag;
10526 sgl_pg_pairs++;
10527 }
10528 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10529 pg_pairs = (pg_pairs > 0) ? (pg_pairs - 1) : pg_pairs;
10530 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10531 /* Perform endian conversion if necessary */
10532 sgl->word0 = cpu_to_le32(sgl->word0);
10533
10534 if (!phba->sli4_hba.intr_enable)
10535 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10536 else {
10537 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10538 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10539 }
10540 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10541 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10542 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10543 if (rc != MBX_TIMEOUT)
10544 lpfc_sli4_mbox_cmd_free(phba, mbox);
10545 if (shdr_status || shdr_add_status || rc) {
10546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10547 "2513 POST_SGL_BLOCK mailbox command failed "
10548 "status x%x add_status x%x mbx status x%x\n",
10549 shdr_status, shdr_add_status, rc);
10550 rc = -ENXIO;
10551 }
10552 return rc;
10553}
10554
10555/**
10556 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10557 * @phba: pointer to lpfc hba data structure.
10558 * @sblist: pointer to scsi buffer list.
10559 * @count: number of scsi buffers on the list.
10560 *
10561 * This routine is invoked to post a block of @count scsi sgl pages from a
10562 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10563 * No Lock is held.
10564 *
10565 **/
10566int
10567lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10568 int cnt)
10569{
10570 struct lpfc_scsi_buf *psb;
10571 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10572 struct sgl_page_pairs *sgl_pg_pairs;
10573 void *viraddr;
10574 LPFC_MBOXQ_t *mbox;
10575 uint32_t reqlen, alloclen, pg_pairs;
10576 uint32_t mbox_tmo;
10577 uint16_t xritag_start = 0;
10578 int rc = 0;
10579 uint32_t shdr_status, shdr_add_status;
10580 dma_addr_t pdma_phys_bpl1;
10581 union lpfc_sli4_cfg_shdr *shdr;
10582
10583 /* Calculate the requested length of the dma memory */
10584 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10585 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10586 if (reqlen > PAGE_SIZE) {
10587 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10588 "0217 Block sgl registration required DMA "
10589 "size (%d) great than a page\n", reqlen);
10590 return -ENOMEM;
10591 }
10592 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10593 if (!mbox) {
10594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10595 "0283 Failed to allocate mbox cmd memory\n");
10596 return -ENOMEM;
10597 }
10598
10599 /* Allocate DMA memory and set up the non-embedded mailbox command */
10600 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10601 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10602 LPFC_SLI4_MBX_NEMBED);
10603
10604 if (alloclen < reqlen) {
10605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10606 "2561 Allocated DMA memory size (%d) is "
10607 "less than the requested DMA memory "
10608 "size (%d)\n", alloclen, reqlen);
10609 lpfc_sli4_mbox_cmd_free(phba, mbox);
10610 return -ENOMEM;
10611 }
10612
10613 /* Get the first SGE entry from the non-embedded DMA memory */
10614 if (unlikely(!mbox->sge_array)) {
10615 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10616 "2565 Failed to get the non-embedded SGE "
10617 "virtual address\n");
10618 lpfc_sli4_mbox_cmd_free(phba, mbox);
10619 return -ENOMEM;
10620 }
10621 viraddr = mbox->sge_array->addr[0];
10622
10623 /* Set up the SGL pages in the non-embedded DMA pages */
10624 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10625 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10626
10627 pg_pairs = 0;
10628 list_for_each_entry(psb, sblist, list) {
10629 /* Set up the sge entry */
10630 sgl_pg_pairs->sgl_pg0_addr_lo =
10631 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10632 sgl_pg_pairs->sgl_pg0_addr_hi =
10633 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10634 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10635 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10636 else
10637 pdma_phys_bpl1 = 0;
10638 sgl_pg_pairs->sgl_pg1_addr_lo =
10639 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10640 sgl_pg_pairs->sgl_pg1_addr_hi =
10641 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10642 /* Keep the first xritag on the list */
10643 if (pg_pairs == 0)
10644 xritag_start = psb->cur_iocbq.sli4_xritag;
10645 sgl_pg_pairs++;
10646 pg_pairs++;
10647 }
10648 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10649 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10650 /* Perform endian conversion if necessary */
10651 sgl->word0 = cpu_to_le32(sgl->word0);
10652
10653 if (!phba->sli4_hba.intr_enable)
10654 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10655 else {
10656 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10657 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10658 }
10659 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10660 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10661 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10662 if (rc != MBX_TIMEOUT)
10663 lpfc_sli4_mbox_cmd_free(phba, mbox);
10664 if (shdr_status || shdr_add_status || rc) {
10665 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10666 "2564 POST_SGL_BLOCK mailbox command failed "
10667 "status x%x add_status x%x mbx status x%x\n",
10668 shdr_status, shdr_add_status, rc);
10669 rc = -ENXIO;
10670 }
10671 return rc;
10672}
10673
10674/**
10675 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10676 * @phba: pointer to lpfc_hba struct that the frame was received on
10677 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10678 *
10679 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10680 * valid type of frame that the LPFC driver will handle. This function will
10681 * return a zero if the frame is a valid frame or a non zero value when the
10682 * frame does not pass the check.
10683 **/
10684static int
10685lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10686{
10687 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10688 char *type_names[] = FC_TYPE_NAMES_INIT;
10689 struct fc_vft_header *fc_vft_hdr;
10690
10691 switch (fc_hdr->fh_r_ctl) {
10692 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10693 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10694 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10695 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10696 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10697 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10698 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10699 case FC_RCTL_DD_CMD_STATUS: /* command status */
10700 case FC_RCTL_ELS_REQ: /* extended link services request */
10701 case FC_RCTL_ELS_REP: /* extended link services reply */
10702 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10703 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10704 case FC_RCTL_BA_NOP: /* basic link service NOP */
10705 case FC_RCTL_BA_ABTS: /* basic link service abort */
10706 case FC_RCTL_BA_RMC: /* remove connection */
10707 case FC_RCTL_BA_ACC: /* basic accept */
10708 case FC_RCTL_BA_RJT: /* basic reject */
10709 case FC_RCTL_BA_PRMT:
10710 case FC_RCTL_ACK_1: /* acknowledge_1 */
10711 case FC_RCTL_ACK_0: /* acknowledge_0 */
10712 case FC_RCTL_P_RJT: /* port reject */
10713 case FC_RCTL_F_RJT: /* fabric reject */
10714 case FC_RCTL_P_BSY: /* port busy */
10715 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10716 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10717 case FC_RCTL_LCR: /* link credit reset */
10718 case FC_RCTL_END: /* end */
10719 break;
10720 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10721 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10722 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10723 return lpfc_fc_frame_check(phba, fc_hdr);
10724 default:
10725 goto drop;
10726 }
10727 switch (fc_hdr->fh_type) {
10728 case FC_TYPE_BLS:
10729 case FC_TYPE_ELS:
10730 case FC_TYPE_FCP:
10731 case FC_TYPE_CT:
10732 break;
10733 case FC_TYPE_IP:
10734 case FC_TYPE_ILS:
10735 default:
10736 goto drop;
10737 }
10738 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10739 "2538 Received frame rctl:%s type:%s\n",
10740 rctl_names[fc_hdr->fh_r_ctl],
10741 type_names[fc_hdr->fh_type]);
10742 return 0;
10743drop:
10744 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10745 "2539 Dropped frame rctl:%s type:%s\n",
10746 rctl_names[fc_hdr->fh_r_ctl],
10747 type_names[fc_hdr->fh_type]);
10748 return 1;
10749}
10750
10751/**
10752 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10753 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10754 *
10755 * This function processes the FC header to retrieve the VFI from the VF
10756 * header, if one exists. This function will return the VFI if one exists
10757 * or 0 if no VSAN Header exists.
10758 **/
10759static uint32_t
10760lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10761{
10762 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10763
10764 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10765 return 0;
10766 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10767}
10768
10769/**
10770 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10771 * @phba: Pointer to the HBA structure to search for the vport on
10772 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10773 * @fcfi: The FC Fabric ID that the frame came from
10774 *
10775 * This function searches the @phba for a vport that matches the content of the
10776 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10777 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10778 * returns the matching vport pointer or NULL if unable to match frame to a
10779 * vport.
10780 **/
10781static struct lpfc_vport *
10782lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10783 uint16_t fcfi)
10784{
10785 struct lpfc_vport **vports;
10786 struct lpfc_vport *vport = NULL;
10787 int i;
10788 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10789 fc_hdr->fh_d_id[1] << 8 |
10790 fc_hdr->fh_d_id[2]);
10791
10792 vports = lpfc_create_vport_work_array(phba);
10793 if (vports != NULL)
10794 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10795 if (phba->fcf.fcfi == fcfi &&
10796 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10797 vports[i]->fc_myDID == did) {
10798 vport = vports[i];
10799 break;
10800 }
10801 }
10802 lpfc_destroy_vport_work_array(phba, vports);
10803 return vport;
10804}
10805
10806/**
10807 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10808 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10809 *
10810 * This function searches through the existing incomplete sequences that have
10811 * been sent to this @vport. If the frame matches one of the incomplete
10812 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10813 * make up that sequence. If no sequence is found that matches this frame then
10814 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10815 * This function returns a pointer to the first dmabuf in the sequence list that
10816 * the frame was linked to.
10817 **/
10818static struct hbq_dmabuf *
10819lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10820{
10821 struct fc_frame_header *new_hdr;
10822 struct fc_frame_header *temp_hdr;
10823 struct lpfc_dmabuf *d_buf;
10824 struct lpfc_dmabuf *h_buf;
10825 struct hbq_dmabuf *seq_dmabuf = NULL;
10826 struct hbq_dmabuf *temp_dmabuf = NULL;
10827
James Smart4d9ab992009-10-02 15:16:39 -040010828 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart4f774512009-05-22 14:52:35 -040010829 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10830 /* Use the hdr_buf to find the sequence that this frame belongs to */
10831 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10832 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10833 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10834 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10835 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10836 continue;
10837 /* found a pending sequence that matches this frame */
10838 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10839 break;
10840 }
10841 if (!seq_dmabuf) {
10842 /*
10843 * This indicates first frame received for this sequence.
10844 * Queue the buffer on the vport's rcv_buffer_list.
10845 */
10846 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10847 return dmabuf;
10848 }
10849 temp_hdr = seq_dmabuf->hbuf.virt;
10850 if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
James Smart4d9ab992009-10-02 15:16:39 -040010851 list_del_init(&seq_dmabuf->hbuf.list);
10852 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10853 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart4f774512009-05-22 14:52:35 -040010854 return dmabuf;
10855 }
10856 /* find the correct place in the sequence to insert this frame */
10857 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10858 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10859 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10860 /*
10861 * If the frame's sequence count is greater than the frame on
10862 * the list then insert the frame right after this frame
10863 */
10864 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10865 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10866 return seq_dmabuf;
10867 }
10868 }
10869 return NULL;
10870}
10871
10872/**
10873 * lpfc_seq_complete - Indicates if a sequence is complete
10874 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10875 *
10876 * This function checks the sequence, starting with the frame described by
10877 * @dmabuf, to see if all the frames associated with this sequence are present.
10878 * the frames associated with this sequence are linked to the @dmabuf using the
10879 * dbuf list. This function looks for two major things. 1) That the first frame
10880 * has a sequence count of zero. 2) There is a frame with last frame of sequence
10881 * set. 3) That there are no holes in the sequence count. The function will
10882 * return 1 when the sequence is complete, otherwise it will return 0.
10883 **/
10884static int
10885lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
10886{
10887 struct fc_frame_header *hdr;
10888 struct lpfc_dmabuf *d_buf;
10889 struct hbq_dmabuf *seq_dmabuf;
10890 uint32_t fctl;
10891 int seq_count = 0;
10892
10893 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10894 /* make sure first fame of sequence has a sequence count of zero */
10895 if (hdr->fh_seq_cnt != seq_count)
10896 return 0;
10897 fctl = (hdr->fh_f_ctl[0] << 16 |
10898 hdr->fh_f_ctl[1] << 8 |
10899 hdr->fh_f_ctl[2]);
10900 /* If last frame of sequence we can return success. */
10901 if (fctl & FC_FC_END_SEQ)
10902 return 1;
10903 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
10904 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10905 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10906 /* If there is a hole in the sequence count then fail. */
10907 if (++seq_count != hdr->fh_seq_cnt)
10908 return 0;
10909 fctl = (hdr->fh_f_ctl[0] << 16 |
10910 hdr->fh_f_ctl[1] << 8 |
10911 hdr->fh_f_ctl[2]);
10912 /* If last frame of sequence we can return success. */
10913 if (fctl & FC_FC_END_SEQ)
10914 return 1;
10915 }
10916 return 0;
10917}
10918
10919/**
10920 * lpfc_prep_seq - Prep sequence for ULP processing
10921 * @vport: Pointer to the vport on which this sequence was received
10922 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10923 *
10924 * This function takes a sequence, described by a list of frames, and creates
10925 * a list of iocbq structures to describe the sequence. This iocbq list will be
10926 * used to issue to the generic unsolicited sequence handler. This routine
10927 * returns a pointer to the first iocbq in the list. If the function is unable
10928 * to allocate an iocbq then it throw out the received frames that were not
10929 * able to be described and return a pointer to the first iocbq. If unable to
10930 * allocate any iocbqs (including the first) this function will return NULL.
10931 **/
10932static struct lpfc_iocbq *
10933lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
10934{
10935 struct lpfc_dmabuf *d_buf, *n_buf;
10936 struct lpfc_iocbq *first_iocbq, *iocbq;
10937 struct fc_frame_header *fc_hdr;
10938 uint32_t sid;
10939
10940 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
10941 /* remove from receive buffer list */
10942 list_del_init(&seq_dmabuf->hbuf.list);
10943 /* get the Remote Port's SID */
10944 sid = (fc_hdr->fh_s_id[0] << 16 |
10945 fc_hdr->fh_s_id[1] << 8 |
10946 fc_hdr->fh_s_id[2]);
10947 /* Get an iocbq struct to fill in. */
10948 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
10949 if (first_iocbq) {
10950 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040010951 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040010952 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
10953 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
10954 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
10955 first_iocbq->iocb.unsli3.rcvsli3.vpi =
10956 vport->vpi + vport->phba->vpi_base;
10957 /* put the first buffer into the first IOCBq */
10958 first_iocbq->context2 = &seq_dmabuf->dbuf;
10959 first_iocbq->context3 = NULL;
10960 first_iocbq->iocb.ulpBdeCount = 1;
10961 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
10962 LPFC_DATA_BUF_SIZE;
10963 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040010964 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040010965 bf_get(lpfc_rcqe_length,
10966 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040010967 }
10968 iocbq = first_iocbq;
10969 /*
10970 * Each IOCBq can have two Buffers assigned, so go through the list
10971 * of buffers for this sequence and save two buffers in each IOCBq
10972 */
10973 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
10974 if (!iocbq) {
10975 lpfc_in_buf_free(vport->phba, d_buf);
10976 continue;
10977 }
10978 if (!iocbq->context3) {
10979 iocbq->context3 = d_buf;
10980 iocbq->iocb.ulpBdeCount++;
10981 iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
10982 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040010983 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040010984 bf_get(lpfc_rcqe_length,
10985 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040010986 } else {
10987 iocbq = lpfc_sli_get_iocbq(vport->phba);
10988 if (!iocbq) {
10989 if (first_iocbq) {
10990 first_iocbq->iocb.ulpStatus =
10991 IOSTAT_FCP_RSP_ERROR;
10992 first_iocbq->iocb.un.ulpWord[4] =
10993 IOERR_NO_RESOURCES;
10994 }
10995 lpfc_in_buf_free(vport->phba, d_buf);
10996 continue;
10997 }
10998 iocbq->context2 = d_buf;
10999 iocbq->context3 = NULL;
11000 iocbq->iocb.ulpBdeCount = 1;
11001 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11002 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011003 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011004 bf_get(lpfc_rcqe_length,
11005 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011006 iocbq->iocb.un.rcvels.remoteID = sid;
11007 list_add_tail(&iocbq->list, &first_iocbq->list);
11008 }
11009 }
11010 return first_iocbq;
11011}
11012
11013/**
11014 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11015 * @phba: Pointer to HBA context object.
11016 *
11017 * This function is called with no lock held. This function processes all
11018 * the received buffers and gives it to upper layers when a received buffer
11019 * indicates that it is the final frame in the sequence. The interrupt
11020 * service routine processes received buffers at interrupt contexts and adds
11021 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11022 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11023 * appropriate receive function when the final frame in a sequence is received.
11024 **/
James Smart4d9ab992009-10-02 15:16:39 -040011025void
11026lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11027 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011028{
James Smart4d9ab992009-10-02 15:16:39 -040011029 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011030 struct fc_frame_header *fc_hdr;
11031 struct lpfc_vport *vport;
11032 uint32_t fcfi;
11033 struct lpfc_iocbq *iocbq;
11034
11035 /* Clear hba flag and get all received buffers into the cmplq */
11036 spin_lock_irq(&phba->hbalock);
11037 phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
James Smart4f774512009-05-22 14:52:35 -040011038 spin_unlock_irq(&phba->hbalock);
11039
11040 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011041 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11042 /* check to see if this a valid type of frame */
11043 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11044 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11045 return;
11046 }
11047 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11048 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11049 if (!vport) {
11050 /* throw out the frame */
11051 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11052 return;
11053 }
11054 /* Link this frame */
11055 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11056 if (!seq_dmabuf) {
11057 /* unable to add frame to vport - throw it out */
11058 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11059 return;
11060 }
11061 /* If not last frame in sequence continue processing frames. */
11062 if (!lpfc_seq_complete(seq_dmabuf)) {
11063 /*
11064 * When saving off frames post a new one and mark this
11065 * frame to be freed when it is finished.
11066 **/
11067 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11068 dmabuf->tag = -1;
11069 return;
11070 }
11071 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11072 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11073 if (!lpfc_complete_unsol_iocb(phba,
11074 &phba->sli.ring[LPFC_ELS_RING],
11075 iocbq, fc_hdr->fh_r_ctl,
11076 fc_hdr->fh_type))
11077 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11078 "2540 Ring %d handler: unexpected Rctl "
11079 "x%x Type x%x received\n",
11080 LPFC_ELS_RING,
11081 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
James Smart4f774512009-05-22 14:52:35 -040011082}
James Smart6fb120a2009-05-22 14:52:59 -040011083
11084/**
11085 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11086 * @phba: pointer to lpfc hba data structure.
11087 *
11088 * This routine is invoked to post rpi header templates to the
11089 * HBA consistent with the SLI-4 interface spec. This routine
11090 * posts a PAGE_SIZE memory region to the port to hold up to
11091 * PAGE_SIZE modulo 64 rpi context headers.
11092 *
11093 * This routine does not require any locks. It's usage is expected
11094 * to be driver load or reset recovery when the driver is
11095 * sequential.
11096 *
11097 * Return codes
11098 * 0 - sucessful
11099 * EIO - The mailbox failed to complete successfully.
11100 * When this error occurs, the driver is not guaranteed
11101 * to have any rpi regions posted to the device and
11102 * must either attempt to repost the regions or take a
11103 * fatal error.
11104 **/
11105int
11106lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11107{
11108 struct lpfc_rpi_hdr *rpi_page;
11109 uint32_t rc = 0;
11110
11111 /* Post all rpi memory regions to the port. */
11112 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11113 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11114 if (rc != MBX_SUCCESS) {
11115 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11116 "2008 Error %d posting all rpi "
11117 "headers\n", rc);
11118 rc = -EIO;
11119 break;
11120 }
11121 }
11122
11123 return rc;
11124}
11125
11126/**
11127 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11128 * @phba: pointer to lpfc hba data structure.
11129 * @rpi_page: pointer to the rpi memory region.
11130 *
11131 * This routine is invoked to post a single rpi header to the
11132 * HBA consistent with the SLI-4 interface spec. This memory region
11133 * maps up to 64 rpi context regions.
11134 *
11135 * Return codes
11136 * 0 - sucessful
11137 * ENOMEM - No available memory
11138 * EIO - The mailbox failed to complete successfully.
11139 **/
11140int
11141lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11142{
11143 LPFC_MBOXQ_t *mboxq;
11144 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11145 uint32_t rc = 0;
11146 uint32_t mbox_tmo;
11147 uint32_t shdr_status, shdr_add_status;
11148 union lpfc_sli4_cfg_shdr *shdr;
11149
11150 /* The port is notified of the header region via a mailbox command. */
11151 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11152 if (!mboxq) {
11153 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11154 "2001 Unable to allocate memory for issuing "
11155 "SLI_CONFIG_SPECIAL mailbox command\n");
11156 return -ENOMEM;
11157 }
11158
11159 /* Post all rpi memory regions to the port. */
11160 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11161 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11162 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11163 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11164 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11165 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11166 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11167 hdr_tmpl, rpi_page->page_count);
11168 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11169 rpi_page->start_rpi);
11170 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11171 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011172 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011173 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11174 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11175 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11176 if (rc != MBX_TIMEOUT)
11177 mempool_free(mboxq, phba->mbox_mem_pool);
11178 if (shdr_status || shdr_add_status || rc) {
11179 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11180 "2514 POST_RPI_HDR mailbox failed with "
11181 "status x%x add_status x%x, mbx status x%x\n",
11182 shdr_status, shdr_add_status, rc);
11183 rc = -ENXIO;
11184 }
11185 return rc;
11186}
11187
11188/**
11189 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11190 * @phba: pointer to lpfc hba data structure.
11191 *
11192 * This routine is invoked to post rpi header templates to the
11193 * HBA consistent with the SLI-4 interface spec. This routine
11194 * posts a PAGE_SIZE memory region to the port to hold up to
11195 * PAGE_SIZE modulo 64 rpi context headers.
11196 *
11197 * Returns
11198 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
11199 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11200 **/
11201int
11202lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11203{
11204 int rpi;
11205 uint16_t max_rpi, rpi_base, rpi_limit;
11206 uint16_t rpi_remaining;
11207 struct lpfc_rpi_hdr *rpi_hdr;
11208
11209 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11210 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11211 rpi_limit = phba->sli4_hba.next_rpi;
11212
11213 /*
11214 * The valid rpi range is not guaranteed to be zero-based. Start
11215 * the search at the rpi_base as reported by the port.
11216 */
11217 spin_lock_irq(&phba->hbalock);
11218 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11219 if (rpi >= rpi_limit || rpi < rpi_base)
11220 rpi = LPFC_RPI_ALLOC_ERROR;
11221 else {
11222 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11223 phba->sli4_hba.max_cfg_param.rpi_used++;
11224 phba->sli4_hba.rpi_count++;
11225 }
11226
11227 /*
11228 * Don't try to allocate more rpi header regions if the device limit
11229 * on available rpis max has been exhausted.
11230 */
11231 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11232 (phba->sli4_hba.rpi_count >= max_rpi)) {
11233 spin_unlock_irq(&phba->hbalock);
11234 return rpi;
11235 }
11236
11237 /*
11238 * If the driver is running low on rpi resources, allocate another
11239 * page now. Note that the next_rpi value is used because
11240 * it represents how many are actually in use whereas max_rpi notes
11241 * how many are supported max by the device.
11242 */
11243 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11244 phba->sli4_hba.rpi_count;
11245 spin_unlock_irq(&phba->hbalock);
11246 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11247 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11248 if (!rpi_hdr) {
11249 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11250 "2002 Error Could not grow rpi "
11251 "count\n");
11252 } else {
11253 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11254 }
11255 }
11256
11257 return rpi;
11258}
11259
11260/**
11261 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11262 * @phba: pointer to lpfc hba data structure.
11263 *
11264 * This routine is invoked to release an rpi to the pool of
11265 * available rpis maintained by the driver.
11266 **/
11267void
11268lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11269{
11270 spin_lock_irq(&phba->hbalock);
11271 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11272 phba->sli4_hba.rpi_count--;
11273 phba->sli4_hba.max_cfg_param.rpi_used--;
11274 spin_unlock_irq(&phba->hbalock);
11275}
11276
11277/**
11278 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11279 * @phba: pointer to lpfc hba data structure.
11280 *
11281 * This routine is invoked to remove the memory region that
11282 * provided rpi via a bitmask.
11283 **/
11284void
11285lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11286{
11287 kfree(phba->sli4_hba.rpi_bmask);
11288}
11289
11290/**
11291 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11292 * @phba: pointer to lpfc hba data structure.
11293 *
11294 * This routine is invoked to remove the memory region that
11295 * provided rpi via a bitmask.
11296 **/
11297int
11298lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11299{
11300 LPFC_MBOXQ_t *mboxq;
11301 struct lpfc_hba *phba = ndlp->phba;
11302 int rc;
11303
11304 /* The port is notified of the header region via a mailbox command. */
11305 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11306 if (!mboxq)
11307 return -ENOMEM;
11308
11309 /* Post all rpi memory regions to the port. */
11310 lpfc_resume_rpi(mboxq, ndlp);
11311 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11312 if (rc == MBX_NOT_FINISHED) {
11313 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11314 "2010 Resume RPI Mailbox failed "
11315 "status %d, mbxStatus x%x\n", rc,
11316 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11317 mempool_free(mboxq, phba->mbox_mem_pool);
11318 return -EIO;
11319 }
11320 return 0;
11321}
11322
11323/**
11324 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11325 * @phba: pointer to lpfc hba data structure.
11326 * @vpi: vpi value to activate with the port.
11327 *
11328 * This routine is invoked to activate a vpi with the
11329 * port when the host intends to use vports with a
11330 * nonzero vpi.
11331 *
11332 * Returns:
11333 * 0 success
11334 * -Evalue otherwise
11335 **/
11336int
11337lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11338{
11339 LPFC_MBOXQ_t *mboxq;
11340 int rc = 0;
11341 uint32_t mbox_tmo;
11342
11343 if (vpi == 0)
11344 return -EINVAL;
11345 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11346 if (!mboxq)
11347 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011348 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011349 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11350 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11351 if (rc != MBX_TIMEOUT)
11352 mempool_free(mboxq, phba->mbox_mem_pool);
11353 if (rc != MBX_SUCCESS) {
11354 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11355 "2022 INIT VPI Mailbox failed "
11356 "status %d, mbxStatus x%x\n", rc,
11357 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11358 rc = -EIO;
11359 }
11360 return rc;
11361}
11362
11363/**
11364 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11365 * @phba: pointer to lpfc hba data structure.
11366 * @mboxq: Pointer to mailbox object.
11367 *
11368 * This routine is invoked to manually add a single FCF record. The caller
11369 * must pass a completely initialized FCF_Record. This routine takes
11370 * care of the nonembedded mailbox operations.
11371 **/
11372static void
11373lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11374{
11375 void *virt_addr;
11376 union lpfc_sli4_cfg_shdr *shdr;
11377 uint32_t shdr_status, shdr_add_status;
11378
11379 virt_addr = mboxq->sge_array->addr[0];
11380 /* The IOCTL status is embedded in the mailbox subheader. */
11381 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11382 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11383 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11384
11385 if ((shdr_status || shdr_add_status) &&
11386 (shdr_status != STATUS_FCF_IN_USE))
11387 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11388 "2558 ADD_FCF_RECORD mailbox failed with "
11389 "status x%x add_status x%x\n",
11390 shdr_status, shdr_add_status);
11391
11392 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11393}
11394
11395/**
11396 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11397 * @phba: pointer to lpfc hba data structure.
11398 * @fcf_record: pointer to the initialized fcf record to add.
11399 *
11400 * This routine is invoked to manually add a single FCF record. The caller
11401 * must pass a completely initialized FCF_Record. This routine takes
11402 * care of the nonembedded mailbox operations.
11403 **/
11404int
11405lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11406{
11407 int rc = 0;
11408 LPFC_MBOXQ_t *mboxq;
11409 uint8_t *bytep;
11410 void *virt_addr;
11411 dma_addr_t phys_addr;
11412 struct lpfc_mbx_sge sge;
11413 uint32_t alloc_len, req_len;
11414 uint32_t fcfindex;
11415
11416 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11417 if (!mboxq) {
11418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11419 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11420 return -ENOMEM;
11421 }
11422
11423 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11424 sizeof(uint32_t);
11425
11426 /* Allocate DMA memory and set up the non-embedded mailbox command */
11427 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11428 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11429 req_len, LPFC_SLI4_MBX_NEMBED);
11430 if (alloc_len < req_len) {
11431 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11432 "2523 Allocated DMA memory size (x%x) is "
11433 "less than the requested DMA memory "
11434 "size (x%x)\n", alloc_len, req_len);
11435 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11436 return -ENOMEM;
11437 }
11438
11439 /*
11440 * Get the first SGE entry from the non-embedded DMA memory. This
11441 * routine only uses a single SGE.
11442 */
11443 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11444 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11445 if (unlikely(!mboxq->sge_array)) {
11446 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11447 "2526 Failed to get the non-embedded SGE "
11448 "virtual address\n");
11449 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11450 return -ENOMEM;
11451 }
11452 virt_addr = mboxq->sge_array->addr[0];
11453 /*
11454 * Configure the FCF record for FCFI 0. This is the driver's
11455 * hardcoded default and gets used in nonFIP mode.
11456 */
11457 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11458 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11459 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11460
11461 /*
11462 * Copy the fcf_index and the FCF Record Data. The data starts after
11463 * the FCoE header plus word10. The data copy needs to be endian
11464 * correct.
11465 */
11466 bytep += sizeof(uint32_t);
11467 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11468 mboxq->vport = phba->pport;
11469 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11470 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11471 if (rc == MBX_NOT_FINISHED) {
11472 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11473 "2515 ADD_FCF_RECORD mailbox failed with "
11474 "status 0x%x\n", rc);
11475 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11476 rc = -EIO;
11477 } else
11478 rc = 0;
11479
11480 return rc;
11481}
11482
11483/**
11484 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11485 * @phba: pointer to lpfc hba data structure.
11486 * @fcf_record: pointer to the fcf record to write the default data.
11487 * @fcf_index: FCF table entry index.
11488 *
11489 * This routine is invoked to build the driver's default FCF record. The
11490 * values used are hardcoded. This routine handles memory initialization.
11491 *
11492 **/
11493void
11494lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11495 struct fcf_record *fcf_record,
11496 uint16_t fcf_index)
11497{
11498 memset(fcf_record, 0, sizeof(struct fcf_record));
11499 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11500 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11501 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11502 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11503 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11504 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11505 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11506 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11507 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11508 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11509 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11510 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11511 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011512 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011513 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11514 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11515 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11516 /* Set the VLAN bit map */
11517 if (phba->valid_vlan) {
11518 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11519 = 1 << (phba->vlan_id % 8);
11520 }
11521}
11522
11523/**
11524 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11525 * @phba: pointer to lpfc hba data structure.
11526 * @fcf_index: FCF table entry offset.
11527 *
11528 * This routine is invoked to read up to @fcf_num of FCF record from the
11529 * device starting with the given @fcf_index.
11530 **/
11531int
11532lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11533{
11534 int rc = 0, error;
11535 LPFC_MBOXQ_t *mboxq;
11536 void *virt_addr;
11537 dma_addr_t phys_addr;
11538 uint8_t *bytep;
11539 struct lpfc_mbx_sge sge;
11540 uint32_t alloc_len, req_len;
11541 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11542
James Smart32b97932009-07-19 10:01:21 -040011543 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040011544 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11545 if (!mboxq) {
11546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11547 "2000 Failed to allocate mbox for "
11548 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040011549 error = -ENOMEM;
11550 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011551 }
11552
11553 req_len = sizeof(struct fcf_record) +
11554 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11555
11556 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11557 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11558 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11559 LPFC_SLI4_MBX_NEMBED);
11560
11561 if (alloc_len < req_len) {
11562 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11563 "0291 Allocated DMA memory size (x%x) is "
11564 "less than the requested DMA memory "
11565 "size (x%x)\n", alloc_len, req_len);
James Smart4d9ab992009-10-02 15:16:39 -040011566 error = -ENOMEM;
11567 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011568 }
11569
11570 /* Get the first SGE entry from the non-embedded DMA memory. This
11571 * routine only uses a single SGE.
11572 */
11573 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11574 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11575 if (unlikely(!mboxq->sge_array)) {
11576 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11577 "2527 Failed to get the non-embedded SGE "
11578 "virtual address\n");
James Smart4d9ab992009-10-02 15:16:39 -040011579 error = -ENOMEM;
11580 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011581 }
11582 virt_addr = mboxq->sge_array->addr[0];
11583 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11584
11585 /* Set up command fields */
11586 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11587 /* Perform necessary endian conversion */
11588 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11589 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11590 mboxq->vport = phba->pport;
11591 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11592 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11593 if (rc == MBX_NOT_FINISHED) {
James Smart6fb120a2009-05-22 14:52:59 -040011594 error = -EIO;
James Smart32b97932009-07-19 10:01:21 -040011595 } else {
11596 spin_lock_irq(&phba->hbalock);
11597 phba->hba_flag |= FCF_DISC_INPROGRESS;
11598 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040011599 error = 0;
James Smart32b97932009-07-19 10:01:21 -040011600 }
James Smart4d9ab992009-10-02 15:16:39 -040011601fail_fcfscan:
11602 if (error) {
11603 if (mboxq)
11604 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11605 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11606 spin_lock_irq(&phba->hbalock);
11607 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11608 spin_unlock_irq(&phba->hbalock);
11609 }
James Smart6fb120a2009-05-22 14:52:59 -040011610 return error;
11611}
James Smarta0c87cb2009-07-19 10:01:10 -040011612
11613/**
11614 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
11615 * @phba: pointer to lpfc hba data structure.
11616 *
11617 * This function read region 23 and parse TLV for port status to
11618 * decide if the user disaled the port. If the TLV indicates the
11619 * port is disabled, the hba_flag is set accordingly.
11620 **/
11621void
11622lpfc_sli_read_link_ste(struct lpfc_hba *phba)
11623{
11624 LPFC_MBOXQ_t *pmb = NULL;
11625 MAILBOX_t *mb;
11626 uint8_t *rgn23_data = NULL;
11627 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
11628 int rc;
11629
11630 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11631 if (!pmb) {
11632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11633 "2600 lpfc_sli_read_serdes_param failed to"
11634 " allocate mailbox memory\n");
11635 goto out;
11636 }
11637 mb = &pmb->u.mb;
11638
11639 /* Get adapter Region 23 data */
11640 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
11641 if (!rgn23_data)
11642 goto out;
11643
11644 do {
11645 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
11646 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
11647
11648 if (rc != MBX_SUCCESS) {
11649 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11650 "2601 lpfc_sli_read_link_ste failed to"
11651 " read config region 23 rc 0x%x Status 0x%x\n",
11652 rc, mb->mbxStatus);
11653 mb->un.varDmp.word_cnt = 0;
11654 }
11655 /*
11656 * dump mem may return a zero when finished or we got a
11657 * mailbox error, either way we are done.
11658 */
11659 if (mb->un.varDmp.word_cnt == 0)
11660 break;
11661 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
11662 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
11663
11664 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
11665 rgn23_data + offset,
11666 mb->un.varDmp.word_cnt);
11667 offset += mb->un.varDmp.word_cnt;
11668 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
11669
11670 data_size = offset;
11671 offset = 0;
11672
11673 if (!data_size)
11674 goto out;
11675
11676 /* Check the region signature first */
11677 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
11678 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11679 "2619 Config region 23 has bad signature\n");
11680 goto out;
11681 }
11682 offset += 4;
11683
11684 /* Check the data structure version */
11685 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
11686 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11687 "2620 Config region 23 has bad version\n");
11688 goto out;
11689 }
11690 offset += 4;
11691
11692 /* Parse TLV entries in the region */
11693 while (offset < data_size) {
11694 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
11695 break;
11696 /*
11697 * If the TLV is not driver specific TLV or driver id is
11698 * not linux driver id, skip the record.
11699 */
11700 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
11701 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
11702 (rgn23_data[offset + 3] != 0)) {
11703 offset += rgn23_data[offset + 1] * 4 + 4;
11704 continue;
11705 }
11706
11707 /* Driver found a driver specific TLV in the config region */
11708 sub_tlv_len = rgn23_data[offset + 1] * 4;
11709 offset += 4;
11710 tlv_offset = 0;
11711
11712 /*
11713 * Search for configured port state sub-TLV.
11714 */
11715 while ((offset < data_size) &&
11716 (tlv_offset < sub_tlv_len)) {
11717 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
11718 offset += 4;
11719 tlv_offset += 4;
11720 break;
11721 }
11722 if (rgn23_data[offset] != PORT_STE_TYPE) {
11723 offset += rgn23_data[offset + 1] * 4 + 4;
11724 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
11725 continue;
11726 }
11727
11728 /* This HBA contains PORT_STE configured */
11729 if (!rgn23_data[offset + 2])
11730 phba->hba_flag |= LINK_DISABLED;
11731
11732 goto out;
11733 }
11734 }
11735out:
11736 if (pmb)
11737 mempool_free(pmb, phba->mbox_mem_pool);
11738 kfree(rgn23_data);
11739 return;
11740}