blob: e8d3e4732a846b3f37499ee3bb9cbdd18993f689 [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 *);
James Smart6669f9b2009-10-02 15:16:45 -040062static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
63 struct hbq_dmabuf *);
James Smart4f774512009-05-22 14:52:35 -040064static IOCB_t *
65lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
66{
67 return &iocbq->iocb;
68}
69
70/**
71 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
72 * @q: The Work Queue to operate on.
73 * @wqe: The work Queue Entry to put on the Work queue.
74 *
75 * This routine will copy the contents of @wqe to the next available entry on
76 * the @q. This function will then ring the Work Queue Doorbell to signal the
77 * HBA to start processing the Work Queue Entry. This function returns 0 if
78 * successful. If no entries are available on @q then this function will return
79 * -ENOMEM.
80 * The caller is expected to hold the hbalock when calling this routine.
81 **/
82static uint32_t
83lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
84{
85 union lpfc_wqe *temp_wqe = q->qe[q->host_index].wqe;
86 struct lpfc_register doorbell;
87 uint32_t host_index;
88
89 /* If the host has not yet processed the next entry then we are done */
90 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
91 return -ENOMEM;
92 /* set consumption flag every once in a while */
93 if (!((q->host_index + 1) % LPFC_RELEASE_NOTIFICATION_INTERVAL))
94 bf_set(lpfc_wqe_gen_wqec, &wqe->generic, 1);
95
96 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
97
98 /* Update the host index before invoking device */
99 host_index = q->host_index;
100 q->host_index = ((q->host_index + 1) % q->entry_count);
101
102 /* Ring Doorbell */
103 doorbell.word0 = 0;
104 bf_set(lpfc_wq_doorbell_num_posted, &doorbell, 1);
105 bf_set(lpfc_wq_doorbell_index, &doorbell, host_index);
106 bf_set(lpfc_wq_doorbell_id, &doorbell, q->queue_id);
107 writel(doorbell.word0, q->phba->sli4_hba.WQDBregaddr);
108 readl(q->phba->sli4_hba.WQDBregaddr); /* Flush */
109
110 return 0;
111}
112
113/**
114 * lpfc_sli4_wq_release - Updates internal hba index for WQ
115 * @q: The Work Queue to operate on.
116 * @index: The index to advance the hba index to.
117 *
118 * This routine will update the HBA index of a queue to reflect consumption of
119 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
120 * an entry the host calls this function to update the queue's internal
121 * pointers. This routine returns the number of entries that were consumed by
122 * the HBA.
123 **/
124static uint32_t
125lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
126{
127 uint32_t released = 0;
128
129 if (q->hba_index == index)
130 return 0;
131 do {
132 q->hba_index = ((q->hba_index + 1) % q->entry_count);
133 released++;
134 } while (q->hba_index != index);
135 return released;
136}
137
138/**
139 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
140 * @q: The Mailbox Queue to operate on.
141 * @wqe: The Mailbox Queue Entry to put on the Work queue.
142 *
143 * This routine will copy the contents of @mqe to the next available entry on
144 * the @q. This function will then ring the Work Queue Doorbell to signal the
145 * HBA to start processing the Work Queue Entry. This function returns 0 if
146 * successful. If no entries are available on @q then this function will return
147 * -ENOMEM.
148 * The caller is expected to hold the hbalock when calling this routine.
149 **/
150static uint32_t
151lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
152{
153 struct lpfc_mqe *temp_mqe = q->qe[q->host_index].mqe;
154 struct lpfc_register doorbell;
155 uint32_t host_index;
156
157 /* If the host has not yet processed the next entry then we are done */
158 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
159 return -ENOMEM;
160 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
161 /* Save off the mailbox pointer for completion */
162 q->phba->mbox = (MAILBOX_t *)temp_mqe;
163
164 /* Update the host index before invoking device */
165 host_index = q->host_index;
166 q->host_index = ((q->host_index + 1) % q->entry_count);
167
168 /* Ring Doorbell */
169 doorbell.word0 = 0;
170 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
171 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
172 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
173 readl(q->phba->sli4_hba.MQDBregaddr); /* Flush */
174 return 0;
175}
176
177/**
178 * lpfc_sli4_mq_release - Updates internal hba index for MQ
179 * @q: The Mailbox Queue to operate on.
180 *
181 * This routine will update the HBA index of a queue to reflect consumption of
182 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
183 * an entry the host calls this function to update the queue's internal
184 * pointers. This routine returns the number of entries that were consumed by
185 * the HBA.
186 **/
187static uint32_t
188lpfc_sli4_mq_release(struct lpfc_queue *q)
189{
190 /* Clear the mailbox pointer for completion */
191 q->phba->mbox = NULL;
192 q->hba_index = ((q->hba_index + 1) % q->entry_count);
193 return 1;
194}
195
196/**
197 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
198 * @q: The Event Queue to get the first valid EQE from
199 *
200 * This routine will get the first valid Event Queue Entry from @q, update
201 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
202 * the Queue (no more work to do), or the Queue is full of EQEs that have been
203 * processed, but not popped back to the HBA then this routine will return NULL.
204 **/
205static struct lpfc_eqe *
206lpfc_sli4_eq_get(struct lpfc_queue *q)
207{
208 struct lpfc_eqe *eqe = q->qe[q->hba_index].eqe;
209
210 /* If the next EQE is not valid then we are done */
211 if (!bf_get(lpfc_eqe_valid, eqe))
212 return NULL;
213 /* If the host has not yet processed the next entry then we are done */
214 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
215 return NULL;
216
217 q->hba_index = ((q->hba_index + 1) % q->entry_count);
218 return eqe;
219}
220
221/**
222 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
223 * @q: The Event Queue that the host has completed processing for.
224 * @arm: Indicates whether the host wants to arms this CQ.
225 *
226 * This routine will mark all Event Queue Entries on @q, from the last
227 * known completed entry to the last entry that was processed, as completed
228 * by clearing the valid bit for each completion queue entry. Then it will
229 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
230 * The internal host index in the @q will be updated by this routine to indicate
231 * that the host has finished processing the entries. The @arm parameter
232 * indicates that the queue should be rearmed when ringing the doorbell.
233 *
234 * This function will return the number of EQEs that were popped.
235 **/
236uint32_t
237lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
238{
239 uint32_t released = 0;
240 struct lpfc_eqe *temp_eqe;
241 struct lpfc_register doorbell;
242
243 /* while there are valid entries */
244 while (q->hba_index != q->host_index) {
245 temp_eqe = q->qe[q->host_index].eqe;
246 bf_set(lpfc_eqe_valid, temp_eqe, 0);
247 released++;
248 q->host_index = ((q->host_index + 1) % q->entry_count);
249 }
250 if (unlikely(released == 0 && !arm))
251 return 0;
252
253 /* ring doorbell for number popped */
254 doorbell.word0 = 0;
255 if (arm) {
256 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
257 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
258 }
259 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
260 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
261 bf_set(lpfc_eqcq_doorbell_eqid, &doorbell, q->queue_id);
262 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
263 return released;
264}
265
266/**
267 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
268 * @q: The Completion Queue to get the first valid CQE from
269 *
270 * This routine will get the first valid Completion Queue Entry from @q, update
271 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
272 * the Queue (no more work to do), or the Queue is full of CQEs that have been
273 * processed, but not popped back to the HBA then this routine will return NULL.
274 **/
275static struct lpfc_cqe *
276lpfc_sli4_cq_get(struct lpfc_queue *q)
277{
278 struct lpfc_cqe *cqe;
279
280 /* If the next CQE is not valid then we are done */
281 if (!bf_get(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
282 return NULL;
283 /* If the host has not yet processed the next entry then we are done */
284 if (((q->hba_index + 1) % q->entry_count) == q->host_index)
285 return NULL;
286
287 cqe = q->qe[q->hba_index].cqe;
288 q->hba_index = ((q->hba_index + 1) % q->entry_count);
289 return cqe;
290}
291
292/**
293 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
294 * @q: The Completion Queue that the host has completed processing for.
295 * @arm: Indicates whether the host wants to arms this CQ.
296 *
297 * This routine will mark all Completion queue entries on @q, from the last
298 * known completed entry to the last entry that was processed, as completed
299 * by clearing the valid bit for each completion queue entry. Then it will
300 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
301 * The internal host index in the @q will be updated by this routine to indicate
302 * that the host has finished processing the entries. The @arm parameter
303 * indicates that the queue should be rearmed when ringing the doorbell.
304 *
305 * This function will return the number of CQEs that were released.
306 **/
307uint32_t
308lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
309{
310 uint32_t released = 0;
311 struct lpfc_cqe *temp_qe;
312 struct lpfc_register doorbell;
313
314 /* while there are valid entries */
315 while (q->hba_index != q->host_index) {
316 temp_qe = q->qe[q->host_index].cqe;
317 bf_set(lpfc_cqe_valid, temp_qe, 0);
318 released++;
319 q->host_index = ((q->host_index + 1) % q->entry_count);
320 }
321 if (unlikely(released == 0 && !arm))
322 return 0;
323
324 /* ring doorbell for number popped */
325 doorbell.word0 = 0;
326 if (arm)
327 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
328 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
329 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
330 bf_set(lpfc_eqcq_doorbell_cqid, &doorbell, q->queue_id);
331 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
332 return released;
333}
334
335/**
336 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
337 * @q: The Header Receive Queue to operate on.
338 * @wqe: The Receive Queue Entry to put on the Receive queue.
339 *
340 * This routine will copy the contents of @wqe to the next available entry on
341 * the @q. This function will then ring the Receive Queue Doorbell to signal the
342 * HBA to start processing the Receive Queue Entry. This function returns the
343 * index that the rqe was copied to if successful. If no entries are available
344 * on @q then this function will return -ENOMEM.
345 * The caller is expected to hold the hbalock when calling this routine.
346 **/
347static int
348lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
349 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
350{
351 struct lpfc_rqe *temp_hrqe = hq->qe[hq->host_index].rqe;
352 struct lpfc_rqe *temp_drqe = dq->qe[dq->host_index].rqe;
353 struct lpfc_register doorbell;
354 int put_index = hq->host_index;
355
356 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
357 return -EINVAL;
358 if (hq->host_index != dq->host_index)
359 return -EINVAL;
360 /* If the host has not yet processed the next entry then we are done */
361 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
362 return -EBUSY;
363 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
364 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
365
366 /* Update the host index to point to the next slot */
367 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
368 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
369
370 /* Ring The Header Receive Queue Doorbell */
371 if (!(hq->host_index % LPFC_RQ_POST_BATCH)) {
372 doorbell.word0 = 0;
373 bf_set(lpfc_rq_doorbell_num_posted, &doorbell,
374 LPFC_RQ_POST_BATCH);
375 bf_set(lpfc_rq_doorbell_id, &doorbell, hq->queue_id);
376 writel(doorbell.word0, hq->phba->sli4_hba.RQDBregaddr);
377 }
378 return put_index;
379}
380
381/**
382 * lpfc_sli4_rq_release - Updates internal hba index for RQ
383 * @q: The Header Receive Queue to operate on.
384 *
385 * This routine will update the HBA index of a queue to reflect consumption of
386 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
387 * consumed an entry the host calls this function to update the queue's
388 * internal pointers. This routine returns the number of entries that were
389 * consumed by the HBA.
390 **/
391static uint32_t
392lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
393{
394 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
395 return 0;
396 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
397 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
398 return 1;
399}
400
James Smarte59058c2008-08-24 21:49:00 -0400401/**
James Smart3621a712009-04-06 18:47:14 -0400402 * lpfc_cmd_iocb - Get next command iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400403 * @phba: Pointer to HBA context object.
404 * @pring: Pointer to driver SLI ring object.
405 *
406 * This function returns pointer to next command iocb entry
407 * in the command ring. The caller must hold hbalock to prevent
408 * other threads consume the next command iocb.
409 * SLI-2/SLI-3 provide different sized iocbs.
410 **/
James Smarted957682007-06-17 19:56:37 -0500411static inline IOCB_t *
412lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
413{
414 return (IOCB_t *) (((char *) pring->cmdringaddr) +
415 pring->cmdidx * phba->iocb_cmd_size);
416}
417
James Smarte59058c2008-08-24 21:49:00 -0400418/**
James Smart3621a712009-04-06 18:47:14 -0400419 * lpfc_resp_iocb - Get next response iocb entry in the ring
James Smarte59058c2008-08-24 21:49:00 -0400420 * @phba: Pointer to HBA context object.
421 * @pring: Pointer to driver SLI ring object.
422 *
423 * This function returns pointer to next response iocb entry
424 * in the response ring. The caller must hold hbalock to make sure
425 * that no other thread consume the next response iocb.
426 * SLI-2/SLI-3 provide different sized iocbs.
427 **/
James Smarted957682007-06-17 19:56:37 -0500428static inline IOCB_t *
429lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
430{
431 return (IOCB_t *) (((char *) pring->rspringaddr) +
432 pring->rspidx * phba->iocb_rsp_size);
433}
434
James Smarte59058c2008-08-24 21:49:00 -0400435/**
James Smart3621a712009-04-06 18:47:14 -0400436 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400437 * @phba: Pointer to HBA context object.
438 *
439 * This function is called with hbalock held. This function
440 * allocates a new driver iocb object from the iocb pool. If the
441 * allocation is successful, it returns pointer to the newly
442 * allocated iocb object else it returns NULL.
443 **/
James Smart2e0fef82007-06-17 19:56:36 -0500444static struct lpfc_iocbq *
445__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400446{
447 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
448 struct lpfc_iocbq * iocbq = NULL;
449
450 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
451 return iocbq;
452}
453
James Smarte59058c2008-08-24 21:49:00 -0400454/**
James Smartda0436e2009-05-22 14:51:39 -0400455 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
456 * @phba: Pointer to HBA context object.
457 * @xritag: XRI value.
458 *
459 * This function clears the sglq pointer from the array of acive
460 * sglq's. The xritag that is passed in is used to index into the
461 * array. Before the xritag can be used it needs to be adjusted
462 * by subtracting the xribase.
463 *
464 * Returns sglq ponter = success, NULL = Failure.
465 **/
466static struct lpfc_sglq *
467__lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
468{
469 uint16_t adj_xri;
470 struct lpfc_sglq *sglq;
471 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
472 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
473 return NULL;
474 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
475 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = NULL;
476 return sglq;
477}
478
479/**
480 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
481 * @phba: Pointer to HBA context object.
482 * @xritag: XRI value.
483 *
484 * This function returns the sglq pointer from the array of acive
485 * sglq's. The xritag that is passed in is used to index into the
486 * array. Before the xritag can be used it needs to be adjusted
487 * by subtracting the xribase.
488 *
489 * Returns sglq ponter = success, NULL = Failure.
490 **/
491static struct lpfc_sglq *
492__lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
493{
494 uint16_t adj_xri;
495 struct lpfc_sglq *sglq;
496 adj_xri = xritag - phba->sli4_hba.max_cfg_param.xri_base;
497 if (adj_xri > phba->sli4_hba.max_cfg_param.max_xri)
498 return NULL;
499 sglq = phba->sli4_hba.lpfc_sglq_active_list[adj_xri];
500 return sglq;
501}
502
503/**
504 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
505 * @phba: Pointer to HBA context object.
506 *
507 * This function is called with hbalock held. This function
508 * Gets a new driver sglq object from the sglq list. If the
509 * list is not empty then it is successful, it returns pointer to the newly
510 * allocated sglq object else it returns NULL.
511 **/
512static struct lpfc_sglq *
513__lpfc_sli_get_sglq(struct lpfc_hba *phba)
514{
515 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
516 struct lpfc_sglq *sglq = NULL;
517 uint16_t adj_xri;
518 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
519 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
520 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
521 return sglq;
522}
523
524/**
James Smart3621a712009-04-06 18:47:14 -0400525 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400526 * @phba: Pointer to HBA context object.
527 *
528 * This function is called with no lock held. This function
529 * allocates a new driver iocb object from the iocb pool. If the
530 * allocation is successful, it returns pointer to the newly
531 * allocated iocb object else it returns NULL.
532 **/
James Smart2e0fef82007-06-17 19:56:36 -0500533struct lpfc_iocbq *
534lpfc_sli_get_iocbq(struct lpfc_hba *phba)
James Bottomley604a3e32005-10-29 10:28:33 -0500535{
James Smart2e0fef82007-06-17 19:56:36 -0500536 struct lpfc_iocbq * iocbq = NULL;
537 unsigned long iflags;
538
539 spin_lock_irqsave(&phba->hbalock, iflags);
540 iocbq = __lpfc_sli_get_iocbq(phba);
541 spin_unlock_irqrestore(&phba->hbalock, iflags);
542 return iocbq;
543}
544
James Smarte59058c2008-08-24 21:49:00 -0400545/**
James Smart4f774512009-05-22 14:52:35 -0400546 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
547 * @phba: Pointer to HBA context object.
548 * @iocbq: Pointer to driver iocb object.
549 *
550 * This function is called with hbalock held to release driver
551 * iocb object to the iocb pool. The iotag in the iocb object
552 * does not change for each use of the iocb object. This function
553 * clears all other fields of the iocb object when it is freed.
554 * The sqlq structure that holds the xritag and phys and virtual
555 * mappings for the scatter gather list is retrieved from the
556 * active array of sglq. The get of the sglq pointer also clears
557 * the entry in the array. If the status of the IO indiactes that
558 * this IO was aborted then the sglq entry it put on the
559 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
560 * IO has good status or fails for any other reason then the sglq
561 * entry is added to the free list (lpfc_sgl_list).
562 **/
563static void
564__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
565{
566 struct lpfc_sglq *sglq;
567 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
568 unsigned long iflag;
569
570 if (iocbq->sli4_xritag == NO_XRI)
571 sglq = NULL;
572 else
573 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
574 if (sglq) {
575 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
James Smart6669f9b2009-10-02 15:16:45 -0400576 && ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
James Smart4f774512009-05-22 14:52:35 -0400577 && (iocbq->iocb.un.ulpWord[4]
James Smart6669f9b2009-10-02 15:16:45 -0400578 == IOERR_ABORT_REQUESTED))) {
James Smart4f774512009-05-22 14:52:35 -0400579 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
580 iflag);
581 list_add(&sglq->list,
582 &phba->sli4_hba.lpfc_abts_els_sgl_list);
583 spin_unlock_irqrestore(
584 &phba->sli4_hba.abts_sgl_list_lock, iflag);
585 } else
586 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
587 }
588
589
590 /*
591 * Clean all volatile data fields, preserve iotag and node struct.
592 */
593 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
594 iocbq->sli4_xritag = NO_XRI;
595 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
596}
597
598/**
James Smart3772a992009-05-22 14:50:54 -0400599 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
600 * @phba: Pointer to HBA context object.
601 * @iocbq: Pointer to driver iocb object.
602 *
603 * This function is called with hbalock held to release driver
604 * iocb object to the iocb pool. The iotag in the iocb object
605 * does not change for each use of the iocb object. This function
606 * clears all other fields of the iocb object when it is freed.
607 **/
608static void
609__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
610{
611 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
612
613 /*
614 * Clean all volatile data fields, preserve iotag and node struct.
615 */
616 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
617 iocbq->sli4_xritag = NO_XRI;
618 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
619}
620
621/**
James Smart3621a712009-04-06 18:47:14 -0400622 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400623 * @phba: Pointer to HBA context object.
624 * @iocbq: Pointer to driver iocb object.
625 *
626 * This function is called with hbalock held to release driver
627 * iocb object to the iocb pool. The iotag in the iocb object
628 * does not change for each use of the iocb object. This function
629 * clears all other fields of the iocb object when it is freed.
630 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +0100631static void
James Smart2e0fef82007-06-17 19:56:36 -0500632__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
633{
James Smart3772a992009-05-22 14:50:54 -0400634 phba->__lpfc_sli_release_iocbq(phba, iocbq);
James Bottomley604a3e32005-10-29 10:28:33 -0500635}
636
James Smarte59058c2008-08-24 21:49:00 -0400637/**
James Smart3621a712009-04-06 18:47:14 -0400638 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
James Smarte59058c2008-08-24 21:49:00 -0400639 * @phba: Pointer to HBA context object.
640 * @iocbq: Pointer to driver iocb object.
641 *
642 * This function is called with no lock held to release the iocb to
643 * iocb pool.
644 **/
James Smart2e0fef82007-06-17 19:56:36 -0500645void
646lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
647{
648 unsigned long iflags;
649
650 /*
651 * Clean all volatile data fields, preserve iotag and node struct.
652 */
653 spin_lock_irqsave(&phba->hbalock, iflags);
654 __lpfc_sli_release_iocbq(phba, iocbq);
655 spin_unlock_irqrestore(&phba->hbalock, iflags);
656}
657
James Smarte59058c2008-08-24 21:49:00 -0400658/**
James Smarta257bf92009-04-06 18:48:10 -0400659 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
660 * @phba: Pointer to HBA context object.
661 * @iocblist: List of IOCBs.
662 * @ulpstatus: ULP status in IOCB command field.
663 * @ulpWord4: ULP word-4 in IOCB command field.
664 *
665 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
666 * on the list by invoking the complete callback function associated with the
667 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
668 * fields.
669 **/
670void
671lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
672 uint32_t ulpstatus, uint32_t ulpWord4)
673{
674 struct lpfc_iocbq *piocb;
675
676 while (!list_empty(iocblist)) {
677 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
678
679 if (!piocb->iocb_cmpl)
680 lpfc_sli_release_iocbq(phba, piocb);
681 else {
682 piocb->iocb.ulpStatus = ulpstatus;
683 piocb->iocb.un.ulpWord[4] = ulpWord4;
684 (piocb->iocb_cmpl) (phba, piocb, piocb);
685 }
686 }
687 return;
688}
689
690/**
James Smart3621a712009-04-06 18:47:14 -0400691 * lpfc_sli_iocb_cmd_type - Get the iocb type
692 * @iocb_cmnd: iocb command code.
James Smarte59058c2008-08-24 21:49:00 -0400693 *
694 * This function is called by ring event handler function to get the iocb type.
695 * This function translates the iocb command to an iocb command type used to
696 * decide the final disposition of each completed IOCB.
697 * The function returns
698 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
699 * LPFC_SOL_IOCB if it is a solicited iocb completion
700 * LPFC_ABORT_IOCB if it is an abort iocb
701 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
702 *
703 * The caller is not required to hold any lock.
704 **/
dea31012005-04-17 16:05:31 -0500705static lpfc_iocb_type
706lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
707{
708 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
709
710 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
711 return 0;
712
713 switch (iocb_cmnd) {
714 case CMD_XMIT_SEQUENCE_CR:
715 case CMD_XMIT_SEQUENCE_CX:
716 case CMD_XMIT_BCAST_CN:
717 case CMD_XMIT_BCAST_CX:
718 case CMD_ELS_REQUEST_CR:
719 case CMD_ELS_REQUEST_CX:
720 case CMD_CREATE_XRI_CR:
721 case CMD_CREATE_XRI_CX:
722 case CMD_GET_RPI_CN:
723 case CMD_XMIT_ELS_RSP_CX:
724 case CMD_GET_RPI_CR:
725 case CMD_FCP_IWRITE_CR:
726 case CMD_FCP_IWRITE_CX:
727 case CMD_FCP_IREAD_CR:
728 case CMD_FCP_IREAD_CX:
729 case CMD_FCP_ICMND_CR:
730 case CMD_FCP_ICMND_CX:
James Smartf5603512006-12-02 13:35:43 -0500731 case CMD_FCP_TSEND_CX:
732 case CMD_FCP_TRSP_CX:
733 case CMD_FCP_TRECEIVE_CX:
734 case CMD_FCP_AUTO_TRSP_CX:
dea31012005-04-17 16:05:31 -0500735 case CMD_ADAPTER_MSG:
736 case CMD_ADAPTER_DUMP:
737 case CMD_XMIT_SEQUENCE64_CR:
738 case CMD_XMIT_SEQUENCE64_CX:
739 case CMD_XMIT_BCAST64_CN:
740 case CMD_XMIT_BCAST64_CX:
741 case CMD_ELS_REQUEST64_CR:
742 case CMD_ELS_REQUEST64_CX:
743 case CMD_FCP_IWRITE64_CR:
744 case CMD_FCP_IWRITE64_CX:
745 case CMD_FCP_IREAD64_CR:
746 case CMD_FCP_IREAD64_CX:
747 case CMD_FCP_ICMND64_CR:
748 case CMD_FCP_ICMND64_CX:
James Smartf5603512006-12-02 13:35:43 -0500749 case CMD_FCP_TSEND64_CX:
750 case CMD_FCP_TRSP64_CX:
751 case CMD_FCP_TRECEIVE64_CX:
dea31012005-04-17 16:05:31 -0500752 case CMD_GEN_REQUEST64_CR:
753 case CMD_GEN_REQUEST64_CX:
754 case CMD_XMIT_ELS_RSP64_CX:
James Smartda0436e2009-05-22 14:51:39 -0400755 case DSSCMD_IWRITE64_CR:
756 case DSSCMD_IWRITE64_CX:
757 case DSSCMD_IREAD64_CR:
758 case DSSCMD_IREAD64_CX:
759 case DSSCMD_INVALIDATE_DEK:
760 case DSSCMD_SET_KEK:
761 case DSSCMD_GET_KEK_ID:
762 case DSSCMD_GEN_XFER:
dea31012005-04-17 16:05:31 -0500763 type = LPFC_SOL_IOCB;
764 break;
765 case CMD_ABORT_XRI_CN:
766 case CMD_ABORT_XRI_CX:
767 case CMD_CLOSE_XRI_CN:
768 case CMD_CLOSE_XRI_CX:
769 case CMD_XRI_ABORTED_CX:
770 case CMD_ABORT_MXRI64_CN:
James Smart6669f9b2009-10-02 15:16:45 -0400771 case CMD_XMIT_BLS_RSP64_CX:
dea31012005-04-17 16:05:31 -0500772 type = LPFC_ABORT_IOCB;
773 break;
774 case CMD_RCV_SEQUENCE_CX:
775 case CMD_RCV_ELS_REQ_CX:
776 case CMD_RCV_SEQUENCE64_CX:
777 case CMD_RCV_ELS_REQ64_CX:
James Smart57127f12007-10-27 13:37:05 -0400778 case CMD_ASYNC_STATUS:
James Smarted957682007-06-17 19:56:37 -0500779 case CMD_IOCB_RCV_SEQ64_CX:
780 case CMD_IOCB_RCV_ELS64_CX:
781 case CMD_IOCB_RCV_CONT64_CX:
James Smart3163f722008-02-08 18:50:25 -0500782 case CMD_IOCB_RET_XRI64_CX:
dea31012005-04-17 16:05:31 -0500783 type = LPFC_UNSOL_IOCB;
784 break;
James Smart3163f722008-02-08 18:50:25 -0500785 case CMD_IOCB_XMIT_MSEQ64_CR:
786 case CMD_IOCB_XMIT_MSEQ64_CX:
787 case CMD_IOCB_RCV_SEQ_LIST64_CX:
788 case CMD_IOCB_RCV_ELS_LIST64_CX:
789 case CMD_IOCB_CLOSE_EXTENDED_CN:
790 case CMD_IOCB_ABORT_EXTENDED_CN:
791 case CMD_IOCB_RET_HBQE64_CN:
792 case CMD_IOCB_FCP_IBIDIR64_CR:
793 case CMD_IOCB_FCP_IBIDIR64_CX:
794 case CMD_IOCB_FCP_ITASKMGT64_CX:
795 case CMD_IOCB_LOGENTRY_CN:
796 case CMD_IOCB_LOGENTRY_ASYNC_CN:
797 printk("%s - Unhandled SLI-3 Command x%x\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -0700798 __func__, iocb_cmnd);
James Smart3163f722008-02-08 18:50:25 -0500799 type = LPFC_UNKNOWN_IOCB;
800 break;
dea31012005-04-17 16:05:31 -0500801 default:
802 type = LPFC_UNKNOWN_IOCB;
803 break;
804 }
805
806 return type;
807}
808
James Smarte59058c2008-08-24 21:49:00 -0400809/**
James Smart3621a712009-04-06 18:47:14 -0400810 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
James Smarte59058c2008-08-24 21:49:00 -0400811 * @phba: Pointer to HBA context object.
812 *
813 * This function is called from SLI initialization code
814 * to configure every ring of the HBA's SLI interface. The
815 * caller is not required to hold any lock. This function issues
816 * a config_ring mailbox command for each ring.
817 * This function returns zero if successful else returns a negative
818 * error code.
819 **/
dea31012005-04-17 16:05:31 -0500820static int
James Smarted957682007-06-17 19:56:37 -0500821lpfc_sli_ring_map(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -0500822{
823 struct lpfc_sli *psli = &phba->sli;
James Smarted957682007-06-17 19:56:37 -0500824 LPFC_MBOXQ_t *pmb;
825 MAILBOX_t *pmbox;
826 int i, rc, ret = 0;
dea31012005-04-17 16:05:31 -0500827
James Smarted957682007-06-17 19:56:37 -0500828 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
829 if (!pmb)
830 return -ENOMEM;
James Smart04c68492009-05-22 14:52:52 -0400831 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -0500832 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -0500833 for (i = 0; i < psli->num_rings; i++) {
dea31012005-04-17 16:05:31 -0500834 lpfc_config_ring(phba, i, pmb);
835 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
836 if (rc != MBX_SUCCESS) {
James Smart92d7f7b2007-06-17 19:56:38 -0500837 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -0400838 "0446 Adapter failed to init (%d), "
dea31012005-04-17 16:05:31 -0500839 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
840 "ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400841 rc, pmbox->mbxCommand,
842 pmbox->mbxStatus, i);
James Smart2e0fef82007-06-17 19:56:36 -0500843 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -0500844 ret = -ENXIO;
845 break;
dea31012005-04-17 16:05:31 -0500846 }
847 }
James Smarted957682007-06-17 19:56:37 -0500848 mempool_free(pmb, phba->mbox_mem_pool);
849 return ret;
dea31012005-04-17 16:05:31 -0500850}
851
James Smarte59058c2008-08-24 21:49:00 -0400852/**
James Smart3621a712009-04-06 18:47:14 -0400853 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
James Smarte59058c2008-08-24 21:49:00 -0400854 * @phba: Pointer to HBA context object.
855 * @pring: Pointer to driver SLI ring object.
856 * @piocb: Pointer to the driver iocb object.
857 *
858 * This function is called with hbalock held. The function adds the
859 * new iocb to txcmplq of the given ring. This function always returns
860 * 0. If this function is called for ELS ring, this function checks if
861 * there is a vport associated with the ELS command. This function also
862 * starts els_tmofunc timer if this is an ELS command.
863 **/
dea31012005-04-17 16:05:31 -0500864static int
James Smart2e0fef82007-06-17 19:56:36 -0500865lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
866 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -0500867{
dea31012005-04-17 16:05:31 -0500868 list_add_tail(&piocb->list, &pring->txcmplq);
869 pring->txcmplq_cnt++;
James Smart92d7f7b2007-06-17 19:56:38 -0500870 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
871 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
872 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
873 if (!piocb->vport)
874 BUG();
875 else
876 mod_timer(&piocb->vport->els_tmofunc,
877 jiffies + HZ * (phba->fc_ratov << 1));
878 }
879
dea31012005-04-17 16:05:31 -0500880
James Smart2e0fef82007-06-17 19:56:36 -0500881 return 0;
dea31012005-04-17 16:05:31 -0500882}
883
James Smarte59058c2008-08-24 21:49:00 -0400884/**
James Smart3621a712009-04-06 18:47:14 -0400885 * lpfc_sli_ringtx_get - Get first element of the txq
James Smarte59058c2008-08-24 21:49:00 -0400886 * @phba: Pointer to HBA context object.
887 * @pring: Pointer to driver SLI ring object.
888 *
889 * This function is called with hbalock held to get next
890 * iocb in txq of the given ring. If there is any iocb in
891 * the txq, the function returns first iocb in the list after
892 * removing the iocb from the list, else it returns NULL.
893 **/
dea31012005-04-17 16:05:31 -0500894static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -0500895lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -0500896{
dea31012005-04-17 16:05:31 -0500897 struct lpfc_iocbq *cmd_iocb;
898
James Smart858c9f62007-06-17 19:56:39 -0500899 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
900 if (cmd_iocb != NULL)
dea31012005-04-17 16:05:31 -0500901 pring->txq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -0500902 return cmd_iocb;
dea31012005-04-17 16:05:31 -0500903}
904
James Smarte59058c2008-08-24 21:49:00 -0400905/**
James Smart3621a712009-04-06 18:47:14 -0400906 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
James Smarte59058c2008-08-24 21:49:00 -0400907 * @phba: Pointer to HBA context object.
908 * @pring: Pointer to driver SLI ring object.
909 *
910 * This function is called with hbalock held and the caller must post the
911 * iocb without releasing the lock. If the caller releases the lock,
912 * iocb slot returned by the function is not guaranteed to be available.
913 * The function returns pointer to the next available iocb slot if there
914 * is available slot in the ring, else it returns NULL.
915 * If the get index of the ring is ahead of the put index, the function
916 * will post an error attention event to the worker thread to take the
917 * HBA to offline state.
918 **/
dea31012005-04-17 16:05:31 -0500919static IOCB_t *
920lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
921{
James Smart34b02dc2008-08-24 21:49:55 -0400922 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -0500923 uint32_t max_cmd_idx = pring->numCiocb;
dea31012005-04-17 16:05:31 -0500924 if ((pring->next_cmdidx == pring->cmdidx) &&
925 (++pring->next_cmdidx >= max_cmd_idx))
926 pring->next_cmdidx = 0;
927
928 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
929
930 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
931
932 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
933 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -0400934 "0315 Ring %d issue: portCmdGet %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200935 "is bigger than cmd ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -0400936 pring->ringno,
dea31012005-04-17 16:05:31 -0500937 pring->local_getidx, max_cmd_idx);
938
James Smart2e0fef82007-06-17 19:56:36 -0500939 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -0500940 /*
941 * All error attention handlers are posted to
942 * worker thread
943 */
944 phba->work_ha |= HA_ERATT;
945 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -0500946
James Smart5e9d9b82008-06-14 22:52:53 -0400947 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -0500948
949 return NULL;
950 }
951
952 if (pring->local_getidx == pring->next_cmdidx)
953 return NULL;
954 }
955
James Smarted957682007-06-17 19:56:37 -0500956 return lpfc_cmd_iocb(phba, pring);
dea31012005-04-17 16:05:31 -0500957}
958
James Smarte59058c2008-08-24 21:49:00 -0400959/**
James Smart3621a712009-04-06 18:47:14 -0400960 * lpfc_sli_next_iotag - Get an iotag for the iocb
James Smarte59058c2008-08-24 21:49:00 -0400961 * @phba: Pointer to HBA context object.
962 * @iocbq: Pointer to driver iocb object.
963 *
964 * This function gets an iotag for the iocb. If there is no unused iotag and
965 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
966 * array and assigns a new iotag.
967 * The function returns the allocated iotag if successful, else returns zero.
968 * Zero is not a valid iotag.
969 * The caller is not required to hold any lock.
970 **/
James Bottomley604a3e32005-10-29 10:28:33 -0500971uint16_t
James Smart2e0fef82007-06-17 19:56:36 -0500972lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea31012005-04-17 16:05:31 -0500973{
James Smart2e0fef82007-06-17 19:56:36 -0500974 struct lpfc_iocbq **new_arr;
975 struct lpfc_iocbq **old_arr;
James Bottomley604a3e32005-10-29 10:28:33 -0500976 size_t new_len;
977 struct lpfc_sli *psli = &phba->sli;
978 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500979
James Smart2e0fef82007-06-17 19:56:36 -0500980 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500981 iotag = psli->last_iotag;
982 if(++iotag < psli->iocbq_lookup_len) {
983 psli->last_iotag = iotag;
984 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -0500985 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500986 iocbq->iotag = iotag;
987 return iotag;
James Smart2e0fef82007-06-17 19:56:36 -0500988 } else if (psli->iocbq_lookup_len < (0xffff
James Bottomley604a3e32005-10-29 10:28:33 -0500989 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
990 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
James Smart2e0fef82007-06-17 19:56:36 -0500991 spin_unlock_irq(&phba->hbalock);
992 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
James Bottomley604a3e32005-10-29 10:28:33 -0500993 GFP_KERNEL);
994 if (new_arr) {
James Smart2e0fef82007-06-17 19:56:36 -0500995 spin_lock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -0500996 old_arr = psli->iocbq_lookup;
997 if (new_len <= psli->iocbq_lookup_len) {
998 /* highly unprobable case */
999 kfree(new_arr);
1000 iotag = psli->last_iotag;
1001 if(++iotag < psli->iocbq_lookup_len) {
1002 psli->last_iotag = iotag;
1003 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001004 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001005 iocbq->iotag = iotag;
1006 return iotag;
1007 }
James Smart2e0fef82007-06-17 19:56:36 -05001008 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001009 return 0;
1010 }
1011 if (psli->iocbq_lookup)
1012 memcpy(new_arr, old_arr,
1013 ((psli->last_iotag + 1) *
James Smart311464e2007-08-02 11:10:37 -04001014 sizeof (struct lpfc_iocbq *)));
James Bottomley604a3e32005-10-29 10:28:33 -05001015 psli->iocbq_lookup = new_arr;
1016 psli->iocbq_lookup_len = new_len;
1017 psli->last_iotag = iotag;
1018 psli->iocbq_lookup[iotag] = iocbq;
James Smart2e0fef82007-06-17 19:56:36 -05001019 spin_unlock_irq(&phba->hbalock);
James Bottomley604a3e32005-10-29 10:28:33 -05001020 iocbq->iotag = iotag;
1021 kfree(old_arr);
1022 return iotag;
1023 }
James Smart8f6d98d2006-08-01 07:34:00 -04001024 } else
James Smart2e0fef82007-06-17 19:56:36 -05001025 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05001026
James Bottomley604a3e32005-10-29 10:28:33 -05001027 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001028 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1029 psli->last_iotag);
dea31012005-04-17 16:05:31 -05001030
James Bottomley604a3e32005-10-29 10:28:33 -05001031 return 0;
dea31012005-04-17 16:05:31 -05001032}
1033
James Smarte59058c2008-08-24 21:49:00 -04001034/**
James Smart3621a712009-04-06 18:47:14 -04001035 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
James Smarte59058c2008-08-24 21:49:00 -04001036 * @phba: Pointer to HBA context object.
1037 * @pring: Pointer to driver SLI ring object.
1038 * @iocb: Pointer to iocb slot in the ring.
1039 * @nextiocb: Pointer to driver iocb object which need to be
1040 * posted to firmware.
1041 *
1042 * This function is called with hbalock held to post a new iocb to
1043 * the firmware. This function copies the new iocb to ring iocb slot and
1044 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1045 * a completion call back for this iocb else the function will free the
1046 * iocb object.
1047 **/
dea31012005-04-17 16:05:31 -05001048static void
1049lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1050 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1051{
1052 /*
James Bottomley604a3e32005-10-29 10:28:33 -05001053 * Set up an iotag
dea31012005-04-17 16:05:31 -05001054 */
James Bottomley604a3e32005-10-29 10:28:33 -05001055 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea31012005-04-17 16:05:31 -05001056
James Smarte2a0a9d2008-12-04 22:40:02 -05001057
James Smarta58cbd52007-08-02 11:09:43 -04001058 if (pring->ringno == LPFC_ELS_RING) {
1059 lpfc_debugfs_slow_ring_trc(phba,
1060 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1061 *(((uint32_t *) &nextiocb->iocb) + 4),
1062 *(((uint32_t *) &nextiocb->iocb) + 6),
1063 *(((uint32_t *) &nextiocb->iocb) + 7));
1064 }
1065
dea31012005-04-17 16:05:31 -05001066 /*
1067 * Issue iocb command to adapter
1068 */
James Smart92d7f7b2007-06-17 19:56:38 -05001069 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea31012005-04-17 16:05:31 -05001070 wmb();
1071 pring->stats.iocb_cmd++;
1072
1073 /*
1074 * If there is no completion routine to call, we can release the
1075 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1076 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1077 */
1078 if (nextiocb->iocb_cmpl)
1079 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
James Bottomley604a3e32005-10-29 10:28:33 -05001080 else
James Smart2e0fef82007-06-17 19:56:36 -05001081 __lpfc_sli_release_iocbq(phba, nextiocb);
dea31012005-04-17 16:05:31 -05001082
1083 /*
1084 * Let the HBA know what IOCB slot will be the next one the
1085 * driver will put a command into.
1086 */
1087 pring->cmdidx = pring->next_cmdidx;
James Smarted957682007-06-17 19:56:37 -05001088 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea31012005-04-17 16:05:31 -05001089}
1090
James Smarte59058c2008-08-24 21:49:00 -04001091/**
James Smart3621a712009-04-06 18:47:14 -04001092 * lpfc_sli_update_full_ring - Update the chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001093 * @phba: Pointer to HBA context object.
1094 * @pring: Pointer to driver SLI ring object.
1095 *
1096 * The caller is not required to hold any lock for calling this function.
1097 * This function updates the chip attention bits for the ring to inform firmware
1098 * that there are pending work to be done for this ring and requests an
1099 * interrupt when there is space available in the ring. This function is
1100 * called when the driver is unable to post more iocbs to the ring due
1101 * to unavailability of space in the ring.
1102 **/
dea31012005-04-17 16:05:31 -05001103static void
James Smart2e0fef82007-06-17 19:56:36 -05001104lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001105{
1106 int ringno = pring->ringno;
1107
1108 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1109
1110 wmb();
1111
1112 /*
1113 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1114 * The HBA will tell us when an IOCB entry is available.
1115 */
1116 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1117 readl(phba->CAregaddr); /* flush */
1118
1119 pring->stats.iocb_cmd_full++;
1120}
1121
James Smarte59058c2008-08-24 21:49:00 -04001122/**
James Smart3621a712009-04-06 18:47:14 -04001123 * lpfc_sli_update_ring - Update chip attention register
James Smarte59058c2008-08-24 21:49:00 -04001124 * @phba: Pointer to HBA context object.
1125 * @pring: Pointer to driver SLI ring object.
1126 *
1127 * This function updates the chip attention register bit for the
1128 * given ring to inform HBA that there is more work to be done
1129 * in this ring. The caller is not required to hold any lock.
1130 **/
dea31012005-04-17 16:05:31 -05001131static void
James Smart2e0fef82007-06-17 19:56:36 -05001132lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001133{
1134 int ringno = pring->ringno;
1135
1136 /*
1137 * Tell the HBA that there is work to do in this ring.
1138 */
James Smart34b02dc2008-08-24 21:49:55 -04001139 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1140 wmb();
1141 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1142 readl(phba->CAregaddr); /* flush */
1143 }
dea31012005-04-17 16:05:31 -05001144}
1145
James Smarte59058c2008-08-24 21:49:00 -04001146/**
James Smart3621a712009-04-06 18:47:14 -04001147 * lpfc_sli_resume_iocb - Process iocbs in the txq
James Smarte59058c2008-08-24 21:49:00 -04001148 * @phba: Pointer to HBA context object.
1149 * @pring: Pointer to driver SLI ring object.
1150 *
1151 * This function is called with hbalock held to post pending iocbs
1152 * in the txq to the firmware. This function is called when driver
1153 * detects space available in the ring.
1154 **/
dea31012005-04-17 16:05:31 -05001155static void
James Smart2e0fef82007-06-17 19:56:36 -05001156lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea31012005-04-17 16:05:31 -05001157{
1158 IOCB_t *iocb;
1159 struct lpfc_iocbq *nextiocb;
1160
1161 /*
1162 * Check to see if:
1163 * (a) there is anything on the txq to send
1164 * (b) link is up
1165 * (c) link attention events can be processed (fcp ring only)
1166 * (d) IOCB processing is not blocked by the outstanding mbox command.
1167 */
1168 if (pring->txq_cnt &&
James Smart2e0fef82007-06-17 19:56:36 -05001169 lpfc_is_link_up(phba) &&
dea31012005-04-17 16:05:31 -05001170 (pring->ringno != phba->sli.fcp_ring ||
James Smart0b727fe2007-10-27 13:37:25 -04001171 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea31012005-04-17 16:05:31 -05001172
1173 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1174 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1175 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1176
1177 if (iocb)
1178 lpfc_sli_update_ring(phba, pring);
1179 else
1180 lpfc_sli_update_full_ring(phba, pring);
1181 }
1182
1183 return;
1184}
1185
James Smarte59058c2008-08-24 21:49:00 -04001186/**
James Smart3621a712009-04-06 18:47:14 -04001187 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001188 * @phba: Pointer to HBA context object.
1189 * @hbqno: HBQ number.
1190 *
1191 * This function is called with hbalock held to get the next
1192 * available slot for the given HBQ. If there is free slot
1193 * available for the HBQ it will return pointer to the next available
1194 * HBQ entry else it will return NULL.
1195 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001196static struct lpfc_hbq_entry *
James Smarted957682007-06-17 19:56:37 -05001197lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1198{
1199 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1200
1201 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1202 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1203 hbqp->next_hbqPutIdx = 0;
1204
1205 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
James Smart92d7f7b2007-06-17 19:56:38 -05001206 uint32_t raw_index = phba->hbq_get[hbqno];
James Smarted957682007-06-17 19:56:37 -05001207 uint32_t getidx = le32_to_cpu(raw_index);
1208
1209 hbqp->local_hbqGetIdx = getidx;
1210
1211 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1212 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05001213 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001214 "1802 HBQ %d: local_hbqGetIdx "
James Smarted957682007-06-17 19:56:37 -05001215 "%u is > than hbqp->entry_count %u\n",
James Smarte8b62012007-08-02 11:10:09 -04001216 hbqno, hbqp->local_hbqGetIdx,
James Smarted957682007-06-17 19:56:37 -05001217 hbqp->entry_count);
1218
1219 phba->link_state = LPFC_HBA_ERROR;
1220 return NULL;
1221 }
1222
1223 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1224 return NULL;
1225 }
1226
James Smart51ef4c22007-08-02 11:10:31 -04001227 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1228 hbqp->hbqPutIdx;
James Smarted957682007-06-17 19:56:37 -05001229}
1230
James Smarte59058c2008-08-24 21:49:00 -04001231/**
James Smart3621a712009-04-06 18:47:14 -04001232 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
James Smarte59058c2008-08-24 21:49:00 -04001233 * @phba: Pointer to HBA context object.
1234 *
1235 * This function is called with no lock held to free all the
1236 * hbq buffers while uninitializing the SLI interface. It also
1237 * frees the HBQ buffers returned by the firmware but not yet
1238 * processed by the upper layers.
1239 **/
James Smarted957682007-06-17 19:56:37 -05001240void
1241lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1242{
James Smart92d7f7b2007-06-17 19:56:38 -05001243 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1244 struct hbq_dmabuf *hbq_buf;
James Smart3163f722008-02-08 18:50:25 -05001245 unsigned long flags;
James Smart51ef4c22007-08-02 11:10:31 -04001246 int i, hbq_count;
James Smart3163f722008-02-08 18:50:25 -05001247 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001248
James Smart51ef4c22007-08-02 11:10:31 -04001249 hbq_count = lpfc_sli_hbq_count();
James Smarted957682007-06-17 19:56:37 -05001250 /* Return all memory used by all HBQs */
James Smart3163f722008-02-08 18:50:25 -05001251 spin_lock_irqsave(&phba->hbalock, flags);
James Smart51ef4c22007-08-02 11:10:31 -04001252 for (i = 0; i < hbq_count; ++i) {
1253 list_for_each_entry_safe(dmabuf, next_dmabuf,
1254 &phba->hbqs[i].hbq_buffer_list, list) {
1255 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1256 list_del(&hbq_buf->dbuf.list);
1257 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1258 }
James Smarta8adb832007-10-27 13:37:53 -04001259 phba->hbqs[i].buffer_count = 0;
James Smarted957682007-06-17 19:56:37 -05001260 }
James Smart3163f722008-02-08 18:50:25 -05001261 /* Return all HBQ buffer that are in-fly */
James Smart3772a992009-05-22 14:50:54 -04001262 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1263 list) {
James Smart3163f722008-02-08 18:50:25 -05001264 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1265 list_del(&hbq_buf->dbuf.list);
1266 if (hbq_buf->tag == -1) {
1267 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1268 (phba, hbq_buf);
1269 } else {
1270 hbqno = hbq_buf->tag >> 16;
1271 if (hbqno >= LPFC_MAX_HBQS)
1272 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1273 (phba, hbq_buf);
1274 else
1275 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1276 hbq_buf);
1277 }
1278 }
1279
1280 /* Mark the HBQs not in use */
1281 phba->hbq_in_use = 0;
1282 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smarted957682007-06-17 19:56:37 -05001283}
1284
James Smarte59058c2008-08-24 21:49:00 -04001285/**
James Smart3621a712009-04-06 18:47:14 -04001286 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001287 * @phba: Pointer to HBA context object.
1288 * @hbqno: HBQ number.
1289 * @hbq_buf: Pointer to HBQ buffer.
1290 *
1291 * This function is called with the hbalock held to post a
1292 * hbq buffer to the firmware. If the function finds an empty
1293 * slot in the HBQ, it will post the buffer. The function will return
1294 * pointer to the hbq entry if it successfully post the buffer
1295 * else it will return NULL.
1296 **/
James Smart3772a992009-05-22 14:50:54 -04001297static int
James Smarted957682007-06-17 19:56:37 -05001298lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
James Smart92d7f7b2007-06-17 19:56:38 -05001299 struct hbq_dmabuf *hbq_buf)
James Smarted957682007-06-17 19:56:37 -05001300{
James Smart3772a992009-05-22 14:50:54 -04001301 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1302}
1303
1304/**
1305 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1306 * @phba: Pointer to HBA context object.
1307 * @hbqno: HBQ number.
1308 * @hbq_buf: Pointer to HBQ buffer.
1309 *
1310 * This function is called with the hbalock held to post a hbq buffer to the
1311 * firmware. If the function finds an empty slot in the HBQ, it will post the
1312 * buffer and place it on the hbq_buffer_list. The function will return zero if
1313 * it successfully post the buffer else it will return an error.
1314 **/
1315static int
1316lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1317 struct hbq_dmabuf *hbq_buf)
1318{
James Smarted957682007-06-17 19:56:37 -05001319 struct lpfc_hbq_entry *hbqe;
James Smart92d7f7b2007-06-17 19:56:38 -05001320 dma_addr_t physaddr = hbq_buf->dbuf.phys;
James Smarted957682007-06-17 19:56:37 -05001321
1322 /* Get next HBQ entry slot to use */
1323 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1324 if (hbqe) {
1325 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1326
James Smart92d7f7b2007-06-17 19:56:38 -05001327 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1328 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
James Smart51ef4c22007-08-02 11:10:31 -04001329 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
James Smarted957682007-06-17 19:56:37 -05001330 hbqe->bde.tus.f.bdeFlags = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001331 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1332 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1333 /* Sync SLIM */
James Smarted957682007-06-17 19:56:37 -05001334 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1335 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
James Smart92d7f7b2007-06-17 19:56:38 -05001336 /* flush */
James Smarted957682007-06-17 19:56:37 -05001337 readl(phba->hbq_put + hbqno);
James Smart51ef4c22007-08-02 11:10:31 -04001338 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
James Smart3772a992009-05-22 14:50:54 -04001339 return 0;
1340 } else
1341 return -ENOMEM;
James Smarted957682007-06-17 19:56:37 -05001342}
1343
James Smart4f774512009-05-22 14:52:35 -04001344/**
1345 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1346 * @phba: Pointer to HBA context object.
1347 * @hbqno: HBQ number.
1348 * @hbq_buf: Pointer to HBQ buffer.
1349 *
1350 * This function is called with the hbalock held to post an RQE to the SLI4
1351 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1352 * the hbq_buffer_list and return zero, otherwise it will return an error.
1353 **/
1354static int
1355lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1356 struct hbq_dmabuf *hbq_buf)
1357{
1358 int rc;
1359 struct lpfc_rqe hrqe;
1360 struct lpfc_rqe drqe;
1361
1362 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1363 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1364 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1365 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1366 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1367 &hrqe, &drqe);
1368 if (rc < 0)
1369 return rc;
1370 hbq_buf->tag = rc;
1371 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1372 return 0;
1373}
1374
James Smarte59058c2008-08-24 21:49:00 -04001375/* HBQ for ELS and CT traffic. */
James Smart92d7f7b2007-06-17 19:56:38 -05001376static struct lpfc_hbq_init lpfc_els_hbq = {
1377 .rn = 1,
1378 .entry_count = 200,
1379 .mask_count = 0,
1380 .profile = 0,
James Smart51ef4c22007-08-02 11:10:31 -04001381 .ring_mask = (1 << LPFC_ELS_RING),
James Smart92d7f7b2007-06-17 19:56:38 -05001382 .buffer_count = 0,
James Smarta257bf92009-04-06 18:48:10 -04001383 .init_count = 40,
1384 .add_count = 40,
James Smart92d7f7b2007-06-17 19:56:38 -05001385};
James Smarted957682007-06-17 19:56:37 -05001386
James Smarte59058c2008-08-24 21:49:00 -04001387/* HBQ for the extra ring if needed */
James Smart51ef4c22007-08-02 11:10:31 -04001388static struct lpfc_hbq_init lpfc_extra_hbq = {
1389 .rn = 1,
1390 .entry_count = 200,
1391 .mask_count = 0,
1392 .profile = 0,
1393 .ring_mask = (1 << LPFC_EXTRA_RING),
1394 .buffer_count = 0,
1395 .init_count = 0,
1396 .add_count = 5,
1397};
1398
James Smarte59058c2008-08-24 21:49:00 -04001399/* Array of HBQs */
James Smart78b2d852007-08-02 11:10:21 -04001400struct lpfc_hbq_init *lpfc_hbq_defs[] = {
James Smart92d7f7b2007-06-17 19:56:38 -05001401 &lpfc_els_hbq,
James Smart51ef4c22007-08-02 11:10:31 -04001402 &lpfc_extra_hbq,
James Smart92d7f7b2007-06-17 19:56:38 -05001403};
1404
James Smarte59058c2008-08-24 21:49:00 -04001405/**
James Smart3621a712009-04-06 18:47:14 -04001406 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
James Smarte59058c2008-08-24 21:49:00 -04001407 * @phba: Pointer to HBA context object.
1408 * @hbqno: HBQ number.
1409 * @count: Number of HBQ buffers to be posted.
1410 *
James Smartd7c255b2008-08-24 21:50:00 -04001411 * This function is called with no lock held to post more hbq buffers to the
1412 * given HBQ. The function returns the number of HBQ buffers successfully
1413 * posted.
James Smarte59058c2008-08-24 21:49:00 -04001414 **/
James Smart311464e2007-08-02 11:10:37 -04001415static int
James Smart92d7f7b2007-06-17 19:56:38 -05001416lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1417{
James Smartd7c255b2008-08-24 21:50:00 -04001418 uint32_t i, posted = 0;
James Smart3163f722008-02-08 18:50:25 -05001419 unsigned long flags;
James Smart92d7f7b2007-06-17 19:56:38 -05001420 struct hbq_dmabuf *hbq_buffer;
James Smartd7c255b2008-08-24 21:50:00 -04001421 LIST_HEAD(hbq_buf_list);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001422 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
James Smart51ef4c22007-08-02 11:10:31 -04001423 return 0;
James Smart51ef4c22007-08-02 11:10:31 -04001424
James Smartd7c255b2008-08-24 21:50:00 -04001425 if ((phba->hbqs[hbqno].buffer_count + count) >
1426 lpfc_hbq_defs[hbqno]->entry_count)
1427 count = lpfc_hbq_defs[hbqno]->entry_count -
1428 phba->hbqs[hbqno].buffer_count;
1429 if (!count)
1430 return 0;
1431 /* Allocate HBQ entries */
1432 for (i = 0; i < count; i++) {
1433 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1434 if (!hbq_buffer)
1435 break;
1436 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1437 }
James Smart3163f722008-02-08 18:50:25 -05001438 /* Check whether HBQ is still in use */
1439 spin_lock_irqsave(&phba->hbalock, flags);
Matthew Wilcoxeafe1df2008-02-21 05:44:33 -07001440 if (!phba->hbq_in_use)
James Smartd7c255b2008-08-24 21:50:00 -04001441 goto err;
1442 while (!list_empty(&hbq_buf_list)) {
1443 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1444 dbuf.list);
1445 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1446 (hbqno << 16));
James Smart3772a992009-05-22 14:50:54 -04001447 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
James Smarta8adb832007-10-27 13:37:53 -04001448 phba->hbqs[hbqno].buffer_count++;
James Smartd7c255b2008-08-24 21:50:00 -04001449 posted++;
1450 } else
James Smart51ef4c22007-08-02 11:10:31 -04001451 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smart92d7f7b2007-06-17 19:56:38 -05001452 }
James Smart3163f722008-02-08 18:50:25 -05001453 spin_unlock_irqrestore(&phba->hbalock, flags);
James Smartd7c255b2008-08-24 21:50:00 -04001454 return posted;
1455err:
1456 spin_unlock_irqrestore(&phba->hbalock, flags);
1457 while (!list_empty(&hbq_buf_list)) {
1458 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1459 dbuf.list);
1460 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1461 }
James Smart92d7f7b2007-06-17 19:56:38 -05001462 return 0;
James Smarted957682007-06-17 19:56:37 -05001463}
1464
James Smarte59058c2008-08-24 21:49:00 -04001465/**
James Smart3621a712009-04-06 18:47:14 -04001466 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
James Smarte59058c2008-08-24 21:49:00 -04001467 * @phba: Pointer to HBA context object.
1468 * @qno: HBQ number.
1469 *
1470 * This function posts more buffers to the HBQ. This function
James Smartd7c255b2008-08-24 21:50:00 -04001471 * is called with no lock held. The function returns the number of HBQ entries
1472 * successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001473 **/
James Smarted957682007-06-17 19:56:37 -05001474int
James Smart92d7f7b2007-06-17 19:56:38 -05001475lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001476{
James Smart92d7f7b2007-06-17 19:56:38 -05001477 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1478 lpfc_hbq_defs[qno]->add_count));
James Smarted957682007-06-17 19:56:37 -05001479}
1480
James Smarte59058c2008-08-24 21:49:00 -04001481/**
James Smart3621a712009-04-06 18:47:14 -04001482 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
James Smarte59058c2008-08-24 21:49:00 -04001483 * @phba: Pointer to HBA context object.
1484 * @qno: HBQ queue number.
1485 *
1486 * This function is called from SLI initialization code path with
1487 * no lock held to post initial HBQ buffers to firmware. The
James Smartd7c255b2008-08-24 21:50:00 -04001488 * function returns the number of HBQ entries successfully allocated.
James Smarte59058c2008-08-24 21:49:00 -04001489 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001490static int
James Smart92d7f7b2007-06-17 19:56:38 -05001491lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
James Smarted957682007-06-17 19:56:37 -05001492{
James Smart92d7f7b2007-06-17 19:56:38 -05001493 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1494 lpfc_hbq_defs[qno]->init_count));
James Smarted957682007-06-17 19:56:37 -05001495}
1496
James Smarte59058c2008-08-24 21:49:00 -04001497/**
James Smart3772a992009-05-22 14:50:54 -04001498 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1499 * @phba: Pointer to HBA context object.
1500 * @hbqno: HBQ number.
1501 *
1502 * This function removes the first hbq buffer on an hbq list and returns a
1503 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1504 **/
1505static struct hbq_dmabuf *
1506lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1507{
1508 struct lpfc_dmabuf *d_buf;
1509
1510 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1511 if (!d_buf)
1512 return NULL;
1513 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1514}
1515
1516/**
James Smart3621a712009-04-06 18:47:14 -04001517 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
James Smarte59058c2008-08-24 21:49:00 -04001518 * @phba: Pointer to HBA context object.
1519 * @tag: Tag of the hbq buffer.
1520 *
1521 * This function is called with hbalock held. This function searches
1522 * for the hbq buffer associated with the given tag in the hbq buffer
1523 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1524 * it returns NULL.
1525 **/
Adrian Bunka6ababd2007-11-05 18:07:33 +01001526static struct hbq_dmabuf *
James Smarted957682007-06-17 19:56:37 -05001527lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
1528{
James Smart92d7f7b2007-06-17 19:56:38 -05001529 struct lpfc_dmabuf *d_buf;
1530 struct hbq_dmabuf *hbq_buf;
James Smart51ef4c22007-08-02 11:10:31 -04001531 uint32_t hbqno;
James Smarted957682007-06-17 19:56:37 -05001532
James Smart51ef4c22007-08-02 11:10:31 -04001533 hbqno = tag >> 16;
Jesper Juhla0a74e452007-08-09 20:47:15 +02001534 if (hbqno >= LPFC_MAX_HBQS)
James Smart51ef4c22007-08-02 11:10:31 -04001535 return NULL;
1536
James Smart3772a992009-05-22 14:50:54 -04001537 spin_lock_irq(&phba->hbalock);
James Smart51ef4c22007-08-02 11:10:31 -04001538 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
James Smart92d7f7b2007-06-17 19:56:38 -05001539 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
James Smart51ef4c22007-08-02 11:10:31 -04001540 if (hbq_buf->tag == tag) {
James Smart3772a992009-05-22 14:50:54 -04001541 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001542 return hbq_buf;
James Smarted957682007-06-17 19:56:37 -05001543 }
1544 }
James Smart3772a992009-05-22 14:50:54 -04001545 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05001546 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04001547 "1803 Bad hbq tag. Data: x%x x%x\n",
James Smarta8adb832007-10-27 13:37:53 -04001548 tag, phba->hbqs[tag >> 16].buffer_count);
James Smart92d7f7b2007-06-17 19:56:38 -05001549 return NULL;
James Smarted957682007-06-17 19:56:37 -05001550}
1551
James Smarte59058c2008-08-24 21:49:00 -04001552/**
James Smart3621a712009-04-06 18:47:14 -04001553 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
James Smarte59058c2008-08-24 21:49:00 -04001554 * @phba: Pointer to HBA context object.
1555 * @hbq_buffer: Pointer to HBQ buffer.
1556 *
1557 * This function is called with hbalock. This function gives back
1558 * the hbq buffer to firmware. If the HBQ does not have space to
1559 * post the buffer, it will free the buffer.
1560 **/
James Smarted957682007-06-17 19:56:37 -05001561void
James Smart51ef4c22007-08-02 11:10:31 -04001562lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
James Smarted957682007-06-17 19:56:37 -05001563{
1564 uint32_t hbqno;
1565
James Smart51ef4c22007-08-02 11:10:31 -04001566 if (hbq_buffer) {
1567 hbqno = hbq_buffer->tag >> 16;
James Smart3772a992009-05-22 14:50:54 -04001568 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
James Smart51ef4c22007-08-02 11:10:31 -04001569 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
James Smarted957682007-06-17 19:56:37 -05001570 }
1571}
1572
James Smarte59058c2008-08-24 21:49:00 -04001573/**
James Smart3621a712009-04-06 18:47:14 -04001574 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
James Smarte59058c2008-08-24 21:49:00 -04001575 * @mbxCommand: mailbox command code.
1576 *
1577 * This function is called by the mailbox event handler function to verify
1578 * that the completed mailbox command is a legitimate mailbox command. If the
1579 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1580 * and the mailbox event handler will take the HBA offline.
1581 **/
dea31012005-04-17 16:05:31 -05001582static int
1583lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1584{
1585 uint8_t ret;
1586
1587 switch (mbxCommand) {
1588 case MBX_LOAD_SM:
1589 case MBX_READ_NV:
1590 case MBX_WRITE_NV:
James Smarta8adb832007-10-27 13:37:53 -04001591 case MBX_WRITE_VPARMS:
dea31012005-04-17 16:05:31 -05001592 case MBX_RUN_BIU_DIAG:
1593 case MBX_INIT_LINK:
1594 case MBX_DOWN_LINK:
1595 case MBX_CONFIG_LINK:
1596 case MBX_CONFIG_RING:
1597 case MBX_RESET_RING:
1598 case MBX_READ_CONFIG:
1599 case MBX_READ_RCONFIG:
1600 case MBX_READ_SPARM:
1601 case MBX_READ_STATUS:
1602 case MBX_READ_RPI:
1603 case MBX_READ_XRI:
1604 case MBX_READ_REV:
1605 case MBX_READ_LNK_STAT:
1606 case MBX_REG_LOGIN:
1607 case MBX_UNREG_LOGIN:
1608 case MBX_READ_LA:
1609 case MBX_CLEAR_LA:
1610 case MBX_DUMP_MEMORY:
1611 case MBX_DUMP_CONTEXT:
1612 case MBX_RUN_DIAGS:
1613 case MBX_RESTART:
1614 case MBX_UPDATE_CFG:
1615 case MBX_DOWN_LOAD:
1616 case MBX_DEL_LD_ENTRY:
1617 case MBX_RUN_PROGRAM:
1618 case MBX_SET_MASK:
James Smart09372822008-01-11 01:52:54 -05001619 case MBX_SET_VARIABLE:
dea31012005-04-17 16:05:31 -05001620 case MBX_UNREG_D_ID:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001621 case MBX_KILL_BOARD:
dea31012005-04-17 16:05:31 -05001622 case MBX_CONFIG_FARP:
Jamie Wellnitz41415862006-02-28 19:25:27 -05001623 case MBX_BEACON:
dea31012005-04-17 16:05:31 -05001624 case MBX_LOAD_AREA:
1625 case MBX_RUN_BIU_DIAG64:
1626 case MBX_CONFIG_PORT:
1627 case MBX_READ_SPARM64:
1628 case MBX_READ_RPI64:
1629 case MBX_REG_LOGIN64:
1630 case MBX_READ_LA64:
James Smart09372822008-01-11 01:52:54 -05001631 case MBX_WRITE_WWN:
dea31012005-04-17 16:05:31 -05001632 case MBX_SET_DEBUG:
1633 case MBX_LOAD_EXP_ROM:
James Smart57127f12007-10-27 13:37:05 -04001634 case MBX_ASYNCEVT_ENABLE:
James Smart92d7f7b2007-06-17 19:56:38 -05001635 case MBX_REG_VPI:
1636 case MBX_UNREG_VPI:
James Smart858c9f62007-06-17 19:56:39 -05001637 case MBX_HEARTBEAT:
James Smart84774a42008-08-24 21:50:06 -04001638 case MBX_PORT_CAPABILITIES:
1639 case MBX_PORT_IOV_CONTROL:
James Smart04c68492009-05-22 14:52:52 -04001640 case MBX_SLI4_CONFIG:
1641 case MBX_SLI4_REQ_FTRS:
1642 case MBX_REG_FCFI:
1643 case MBX_UNREG_FCFI:
1644 case MBX_REG_VFI:
1645 case MBX_UNREG_VFI:
1646 case MBX_INIT_VPI:
1647 case MBX_INIT_VFI:
1648 case MBX_RESUME_RPI:
dea31012005-04-17 16:05:31 -05001649 ret = mbxCommand;
1650 break;
1651 default:
1652 ret = MBX_SHUTDOWN;
1653 break;
1654 }
James Smart2e0fef82007-06-17 19:56:36 -05001655 return ret;
dea31012005-04-17 16:05:31 -05001656}
James Smarte59058c2008-08-24 21:49:00 -04001657
1658/**
James Smart3621a712009-04-06 18:47:14 -04001659 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001660 * @phba: Pointer to HBA context object.
1661 * @pmboxq: Pointer to mailbox command.
1662 *
1663 * This is completion handler function for mailbox commands issued from
1664 * lpfc_sli_issue_mbox_wait function. This function is called by the
1665 * mailbox event handler function with no lock held. This function
1666 * will wake up thread waiting on the wait queue pointed by context1
1667 * of the mailbox.
1668 **/
James Smart04c68492009-05-22 14:52:52 -04001669void
James Smart2e0fef82007-06-17 19:56:36 -05001670lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea31012005-04-17 16:05:31 -05001671{
1672 wait_queue_head_t *pdone_q;
James Smart858c9f62007-06-17 19:56:39 -05001673 unsigned long drvr_flag;
dea31012005-04-17 16:05:31 -05001674
1675 /*
1676 * If pdone_q is empty, the driver thread gave up waiting and
1677 * continued running.
1678 */
James Smart7054a602007-04-25 09:52:34 -04001679 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
James Smart858c9f62007-06-17 19:56:39 -05001680 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001681 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1682 if (pdone_q)
1683 wake_up_interruptible(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05001684 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05001685 return;
1686}
1687
James Smarte59058c2008-08-24 21:49:00 -04001688
1689/**
James Smart3621a712009-04-06 18:47:14 -04001690 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
James Smarte59058c2008-08-24 21:49:00 -04001691 * @phba: Pointer to HBA context object.
1692 * @pmb: Pointer to mailbox object.
1693 *
1694 * This function is the default mailbox completion handler. It
1695 * frees the memory resources associated with the completed mailbox
1696 * command. If the completed command is a REG_LOGIN mailbox command,
1697 * this function will issue a UREG_LOGIN to re-claim the RPI.
1698 **/
dea31012005-04-17 16:05:31 -05001699void
James Smart2e0fef82007-06-17 19:56:36 -05001700lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea31012005-04-17 16:05:31 -05001701{
1702 struct lpfc_dmabuf *mp;
James Smart04c68492009-05-22 14:52:52 -04001703 uint16_t rpi, vpi;
James Smart7054a602007-04-25 09:52:34 -04001704 int rc;
1705
dea31012005-04-17 16:05:31 -05001706 mp = (struct lpfc_dmabuf *) (pmb->context1);
James Smart7054a602007-04-25 09:52:34 -04001707
dea31012005-04-17 16:05:31 -05001708 if (mp) {
1709 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1710 kfree(mp);
1711 }
James Smart7054a602007-04-25 09:52:34 -04001712
James Smart04c68492009-05-22 14:52:52 -04001713 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1714 (phba->sli_rev == LPFC_SLI_REV4))
1715 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1716
James Smart7054a602007-04-25 09:52:34 -04001717 /*
1718 * If a REG_LOGIN succeeded after node is destroyed or node
1719 * is in re-discovery driver need to cleanup the RPI.
1720 */
James Smart2e0fef82007-06-17 19:56:36 -05001721 if (!(phba->pport->load_flag & FC_UNLOADING) &&
James Smart04c68492009-05-22 14:52:52 -04001722 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1723 !pmb->u.mb.mbxStatus) {
1724 rpi = pmb->u.mb.un.varWords[0];
1725 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1726 lpfc_unreg_login(phba, vpi, rpi, pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001727 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart7054a602007-04-25 09:52:34 -04001728 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1729 if (rc != MBX_NOT_FINISHED)
1730 return;
1731 }
1732
James Smart04c68492009-05-22 14:52:52 -04001733 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1734 lpfc_sli4_mbox_cmd_free(phba, pmb);
1735 else
1736 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -05001737}
1738
James Smarte59058c2008-08-24 21:49:00 -04001739/**
James Smart3621a712009-04-06 18:47:14 -04001740 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
James Smarte59058c2008-08-24 21:49:00 -04001741 * @phba: Pointer to HBA context object.
1742 *
1743 * This function is called with no lock held. This function processes all
1744 * the completed mailbox commands and gives it to upper layers. The interrupt
1745 * service routine processes mailbox completion interrupt and adds completed
1746 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1747 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1748 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1749 * function returns the mailbox commands to the upper layer by calling the
1750 * completion handler function of each mailbox.
1751 **/
dea31012005-04-17 16:05:31 -05001752int
James Smart2e0fef82007-06-17 19:56:36 -05001753lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05001754{
James Smart92d7f7b2007-06-17 19:56:38 -05001755 MAILBOX_t *pmbox;
dea31012005-04-17 16:05:31 -05001756 LPFC_MBOXQ_t *pmb;
James Smart92d7f7b2007-06-17 19:56:38 -05001757 int rc;
1758 LIST_HEAD(cmplq);
dea31012005-04-17 16:05:31 -05001759
1760 phba->sli.slistat.mbox_event++;
1761
James Smart92d7f7b2007-06-17 19:56:38 -05001762 /* Get all completed mailboxe buffers into the cmplq */
1763 spin_lock_irq(&phba->hbalock);
1764 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1765 spin_unlock_irq(&phba->hbalock);
1766
dea31012005-04-17 16:05:31 -05001767 /* Get a Mailbox buffer to setup mailbox commands for callback */
James Smart92d7f7b2007-06-17 19:56:38 -05001768 do {
1769 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1770 if (pmb == NULL)
1771 break;
1772
James Smart04c68492009-05-22 14:52:52 -04001773 pmbox = &pmb->u.mb;
dea31012005-04-17 16:05:31 -05001774
James Smart858c9f62007-06-17 19:56:39 -05001775 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1776 if (pmb->vport) {
1777 lpfc_debugfs_disc_trc(pmb->vport,
1778 LPFC_DISC_TRC_MBOX_VPORT,
1779 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1780 (uint32_t)pmbox->mbxCommand,
1781 pmbox->un.varWords[0],
1782 pmbox->un.varWords[1]);
1783 }
1784 else {
1785 lpfc_debugfs_disc_trc(phba->pport,
1786 LPFC_DISC_TRC_MBOX,
1787 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1788 (uint32_t)pmbox->mbxCommand,
1789 pmbox->un.varWords[0],
1790 pmbox->un.varWords[1]);
1791 }
1792 }
1793
dea31012005-04-17 16:05:31 -05001794 /*
1795 * It is a fatal error if unknown mbox command completion.
1796 */
1797 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1798 MBX_SHUTDOWN) {
dea31012005-04-17 16:05:31 -05001799 /* Unknow mailbox command compl */
James Smart92d7f7b2007-06-17 19:56:38 -05001800 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001801 "(%d):0323 Unknown Mailbox command "
James Smart04c68492009-05-22 14:52:52 -04001802 "x%x (x%x) Cmpl\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001803 pmb->vport ? pmb->vport->vpi : 0,
James Smart04c68492009-05-22 14:52:52 -04001804 pmbox->mbxCommand,
1805 lpfc_sli4_mbox_opcode_get(phba, pmb));
James Smart2e0fef82007-06-17 19:56:36 -05001806 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05001807 phba->work_hs = HS_FFER3;
1808 lpfc_handle_eratt(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05001809 continue;
dea31012005-04-17 16:05:31 -05001810 }
1811
dea31012005-04-17 16:05:31 -05001812 if (pmbox->mbxStatus) {
1813 phba->sli.slistat.mbox_stat_err++;
1814 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1815 /* Mbox cmd cmpl error - RETRYing */
James Smart92d7f7b2007-06-17 19:56:38 -05001816 lpfc_printf_log(phba, KERN_INFO,
1817 LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04001818 "(%d):0305 Mbox cmd cmpl "
James Smart92d7f7b2007-06-17 19:56:38 -05001819 "error - RETRYing Data: x%x "
James Smart04c68492009-05-22 14:52:52 -04001820 "(x%x) x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001821 pmb->vport ? pmb->vport->vpi :0,
1822 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001823 lpfc_sli4_mbox_opcode_get(phba,
1824 pmb),
James Smart92d7f7b2007-06-17 19:56:38 -05001825 pmbox->mbxStatus,
1826 pmbox->un.varWords[0],
1827 pmb->vport->port_state);
dea31012005-04-17 16:05:31 -05001828 pmbox->mbxStatus = 0;
1829 pmbox->mbxOwner = OWN_HOST;
dea31012005-04-17 16:05:31 -05001830 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
James Smart04c68492009-05-22 14:52:52 -04001831 if (rc != MBX_NOT_FINISHED)
James Smart92d7f7b2007-06-17 19:56:38 -05001832 continue;
dea31012005-04-17 16:05:31 -05001833 }
1834 }
1835
1836 /* Mailbox cmd <cmd> Cmpl <cmpl> */
James Smart92d7f7b2007-06-17 19:56:38 -05001837 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smart04c68492009-05-22 14:52:52 -04001838 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea31012005-04-17 16:05:31 -05001839 "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 -05001840 pmb->vport ? pmb->vport->vpi : 0,
dea31012005-04-17 16:05:31 -05001841 pmbox->mbxCommand,
James Smart04c68492009-05-22 14:52:52 -04001842 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea31012005-04-17 16:05:31 -05001843 pmb->mbox_cmpl,
1844 *((uint32_t *) pmbox),
1845 pmbox->un.varWords[0],
1846 pmbox->un.varWords[1],
1847 pmbox->un.varWords[2],
1848 pmbox->un.varWords[3],
1849 pmbox->un.varWords[4],
1850 pmbox->un.varWords[5],
1851 pmbox->un.varWords[6],
1852 pmbox->un.varWords[7]);
1853
James Smart92d7f7b2007-06-17 19:56:38 -05001854 if (pmb->mbox_cmpl)
dea31012005-04-17 16:05:31 -05001855 pmb->mbox_cmpl(phba,pmb);
James Smart92d7f7b2007-06-17 19:56:38 -05001856 } while (1);
James Smart2e0fef82007-06-17 19:56:36 -05001857 return 0;
dea31012005-04-17 16:05:31 -05001858}
James Smart92d7f7b2007-06-17 19:56:38 -05001859
James Smarte59058c2008-08-24 21:49:00 -04001860/**
James Smart3621a712009-04-06 18:47:14 -04001861 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
James Smarte59058c2008-08-24 21:49:00 -04001862 * @phba: Pointer to HBA context object.
1863 * @pring: Pointer to driver SLI ring object.
1864 * @tag: buffer tag.
1865 *
1866 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1867 * is set in the tag the buffer is posted for a particular exchange,
1868 * the function will return the buffer without replacing the buffer.
1869 * If the buffer is for unsolicited ELS or CT traffic, this function
1870 * returns the buffer and also posts another buffer to the firmware.
1871 **/
James Smart76bb24e2007-10-27 13:38:00 -04001872static struct lpfc_dmabuf *
1873lpfc_sli_get_buff(struct lpfc_hba *phba,
James Smart9f1e1b52008-12-04 22:39:40 -05001874 struct lpfc_sli_ring *pring,
1875 uint32_t tag)
James Smart76bb24e2007-10-27 13:38:00 -04001876{
James Smart9f1e1b52008-12-04 22:39:40 -05001877 struct hbq_dmabuf *hbq_entry;
1878
James Smart76bb24e2007-10-27 13:38:00 -04001879 if (tag & QUE_BUFTAG_BIT)
1880 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
James Smart9f1e1b52008-12-04 22:39:40 -05001881 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1882 if (!hbq_entry)
1883 return NULL;
1884 return &hbq_entry->dbuf;
James Smart76bb24e2007-10-27 13:38:00 -04001885}
James Smart57127f12007-10-27 13:37:05 -04001886
James Smart3772a992009-05-22 14:50:54 -04001887/**
1888 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1889 * @phba: Pointer to HBA context object.
1890 * @pring: Pointer to driver SLI ring object.
1891 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1892 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1893 * @fch_type: the type for the first frame of the sequence.
1894 *
1895 * This function is called with no lock held. This function uses the r_ctl and
1896 * type of the received sequence to find the correct callback function to call
1897 * to process the sequence.
1898 **/
1899static int
1900lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1901 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1902 uint32_t fch_type)
1903{
1904 int i;
1905
1906 /* unSolicited Responses */
1907 if (pring->prt[0].profile) {
1908 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1909 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1910 saveq);
1911 return 1;
1912 }
1913 /* We must search, based on rctl / type
1914 for the right routine */
1915 for (i = 0; i < pring->num_mask; i++) {
1916 if ((pring->prt[i].rctl == fch_r_ctl) &&
1917 (pring->prt[i].type == fch_type)) {
1918 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1919 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1920 (phba, pring, saveq);
1921 return 1;
1922 }
1923 }
1924 return 0;
1925}
James Smarte59058c2008-08-24 21:49:00 -04001926
1927/**
James Smart3621a712009-04-06 18:47:14 -04001928 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
James Smarte59058c2008-08-24 21:49:00 -04001929 * @phba: Pointer to HBA context object.
1930 * @pring: Pointer to driver SLI ring object.
1931 * @saveq: Pointer to the unsolicited iocb.
1932 *
1933 * This function is called with no lock held by the ring event handler
1934 * when there is an unsolicited iocb posted to the response ring by the
1935 * firmware. This function gets the buffer associated with the iocbs
1936 * and calls the event handler for the ring. This function handles both
1937 * qring buffers and hbq buffers.
1938 * When the function returns 1 the caller can free the iocb object otherwise
1939 * upper layer functions will free the iocb objects.
1940 **/
dea31012005-04-17 16:05:31 -05001941static int
1942lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1943 struct lpfc_iocbq *saveq)
1944{
1945 IOCB_t * irsp;
1946 WORD5 * w5p;
1947 uint32_t Rctl, Type;
James Smart3772a992009-05-22 14:50:54 -04001948 uint32_t match;
James Smart76bb24e2007-10-27 13:38:00 -04001949 struct lpfc_iocbq *iocbq;
James Smart3163f722008-02-08 18:50:25 -05001950 struct lpfc_dmabuf *dmzbuf;
dea31012005-04-17 16:05:31 -05001951
1952 match = 0;
1953 irsp = &(saveq->iocb);
James Smart57127f12007-10-27 13:37:05 -04001954
1955 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1956 if (pring->lpfc_sli_rcv_async_status)
1957 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1958 else
1959 lpfc_printf_log(phba,
1960 KERN_WARNING,
1961 LOG_SLI,
1962 "0316 Ring %d handler: unexpected "
1963 "ASYNC_STATUS iocb received evt_code "
1964 "0x%x\n",
1965 pring->ringno,
1966 irsp->un.asyncstat.evt_code);
1967 return 1;
1968 }
1969
James Smart3163f722008-02-08 18:50:25 -05001970 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1971 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1972 if (irsp->ulpBdeCount > 0) {
1973 dmzbuf = lpfc_sli_get_buff(phba, pring,
1974 irsp->un.ulpWord[3]);
1975 lpfc_in_buf_free(phba, dmzbuf);
1976 }
1977
1978 if (irsp->ulpBdeCount > 1) {
1979 dmzbuf = lpfc_sli_get_buff(phba, pring,
1980 irsp->unsli3.sli3Words[3]);
1981 lpfc_in_buf_free(phba, dmzbuf);
1982 }
1983
1984 if (irsp->ulpBdeCount > 2) {
1985 dmzbuf = lpfc_sli_get_buff(phba, pring,
1986 irsp->unsli3.sli3Words[7]);
1987 lpfc_in_buf_free(phba, dmzbuf);
1988 }
1989
1990 return 1;
1991 }
1992
James Smart92d7f7b2007-06-17 19:56:38 -05001993 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
James Smart76bb24e2007-10-27 13:38:00 -04001994 if (irsp->ulpBdeCount != 0) {
1995 saveq->context2 = lpfc_sli_get_buff(phba, pring,
James Smart92d7f7b2007-06-17 19:56:38 -05001996 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04001997 if (!saveq->context2)
1998 lpfc_printf_log(phba,
1999 KERN_ERR,
2000 LOG_SLI,
2001 "0341 Ring %d Cannot find buffer for "
2002 "an unsolicited iocb. tag 0x%x\n",
2003 pring->ringno,
2004 irsp->un.ulpWord[3]);
James Smart76bb24e2007-10-27 13:38:00 -04002005 }
2006 if (irsp->ulpBdeCount == 2) {
2007 saveq->context3 = lpfc_sli_get_buff(phba, pring,
James Smart51ef4c22007-08-02 11:10:31 -04002008 irsp->unsli3.sli3Words[7]);
James Smart76bb24e2007-10-27 13:38:00 -04002009 if (!saveq->context3)
2010 lpfc_printf_log(phba,
2011 KERN_ERR,
2012 LOG_SLI,
2013 "0342 Ring %d Cannot find buffer for an"
2014 " unsolicited iocb. tag 0x%x\n",
2015 pring->ringno,
2016 irsp->unsli3.sli3Words[7]);
2017 }
2018 list_for_each_entry(iocbq, &saveq->list, list) {
James Smart76bb24e2007-10-27 13:38:00 -04002019 irsp = &(iocbq->iocb);
James Smart76bb24e2007-10-27 13:38:00 -04002020 if (irsp->ulpBdeCount != 0) {
2021 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2022 irsp->un.ulpWord[3]);
James Smart9c2face2008-01-11 01:53:18 -05002023 if (!iocbq->context2)
James Smart76bb24e2007-10-27 13:38:00 -04002024 lpfc_printf_log(phba,
2025 KERN_ERR,
2026 LOG_SLI,
2027 "0343 Ring %d Cannot find "
2028 "buffer for an unsolicited iocb"
2029 ". tag 0x%x\n", pring->ringno,
2030 irsp->un.ulpWord[3]);
2031 }
2032 if (irsp->ulpBdeCount == 2) {
2033 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2034 irsp->unsli3.sli3Words[7]);
James Smart9c2face2008-01-11 01:53:18 -05002035 if (!iocbq->context3)
James Smart76bb24e2007-10-27 13:38:00 -04002036 lpfc_printf_log(phba,
2037 KERN_ERR,
2038 LOG_SLI,
2039 "0344 Ring %d Cannot find "
2040 "buffer for an unsolicited "
2041 "iocb. tag 0x%x\n",
2042 pring->ringno,
2043 irsp->unsli3.sli3Words[7]);
2044 }
2045 }
James Smart92d7f7b2007-06-17 19:56:38 -05002046 }
James Smart9c2face2008-01-11 01:53:18 -05002047 if (irsp->ulpBdeCount != 0 &&
2048 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2049 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2050 int found = 0;
2051
2052 /* search continue save q for same XRI */
2053 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2054 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2055 list_add_tail(&saveq->list, &iocbq->list);
2056 found = 1;
2057 break;
2058 }
2059 }
2060 if (!found)
2061 list_add_tail(&saveq->clist,
2062 &pring->iocb_continue_saveq);
2063 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2064 list_del_init(&iocbq->clist);
2065 saveq = iocbq;
2066 irsp = &(saveq->iocb);
2067 } else
2068 return 0;
2069 }
2070 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2071 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2072 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2073 Rctl = FC_ELS_REQ;
2074 Type = FC_ELS_DATA;
2075 } else {
2076 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2077 Rctl = w5p->hcsw.Rctl;
2078 Type = w5p->hcsw.Type;
2079
2080 /* Firmware Workaround */
2081 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2082 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2083 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2084 Rctl = FC_ELS_REQ;
2085 Type = FC_ELS_DATA;
2086 w5p->hcsw.Rctl = Rctl;
2087 w5p->hcsw.Type = Type;
2088 }
2089 }
James Smart92d7f7b2007-06-17 19:56:38 -05002090
James Smart3772a992009-05-22 14:50:54 -04002091 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
James Smart92d7f7b2007-06-17 19:56:38 -05002092 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002093 "0313 Ring %d handler: unexpected Rctl x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05002094 "Type x%x received\n",
James Smarte8b62012007-08-02 11:10:09 -04002095 pring->ringno, Rctl, Type);
James Smart3772a992009-05-22 14:50:54 -04002096
James Smart92d7f7b2007-06-17 19:56:38 -05002097 return 1;
dea31012005-04-17 16:05:31 -05002098}
2099
James Smarte59058c2008-08-24 21:49:00 -04002100/**
James Smart3621a712009-04-06 18:47:14 -04002101 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
James Smarte59058c2008-08-24 21:49:00 -04002102 * @phba: Pointer to HBA context object.
2103 * @pring: Pointer to driver SLI ring object.
2104 * @prspiocb: Pointer to response iocb object.
2105 *
2106 * This function looks up the iocb_lookup table to get the command iocb
2107 * corresponding to the given response iocb using the iotag of the
2108 * response iocb. This function is called with the hbalock held.
2109 * This function returns the command iocb object if it finds the command
2110 * iocb else returns NULL.
2111 **/
dea31012005-04-17 16:05:31 -05002112static struct lpfc_iocbq *
James Smart2e0fef82007-06-17 19:56:36 -05002113lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2114 struct lpfc_sli_ring *pring,
2115 struct lpfc_iocbq *prspiocb)
dea31012005-04-17 16:05:31 -05002116{
dea31012005-04-17 16:05:31 -05002117 struct lpfc_iocbq *cmd_iocb = NULL;
2118 uint16_t iotag;
2119
James Bottomley604a3e32005-10-29 10:28:33 -05002120 iotag = prspiocb->iocb.ulpIoTag;
dea31012005-04-17 16:05:31 -05002121
James Bottomley604a3e32005-10-29 10:28:33 -05002122 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2123 cmd_iocb = phba->sli.iocbq_lookup[iotag];
James Smart92d7f7b2007-06-17 19:56:38 -05002124 list_del_init(&cmd_iocb->list);
James Bottomley604a3e32005-10-29 10:28:33 -05002125 pring->txcmplq_cnt--;
2126 return cmd_iocb;
dea31012005-04-17 16:05:31 -05002127 }
2128
dea31012005-04-17 16:05:31 -05002129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002130 "0317 iotag x%x is out off "
James Bottomley604a3e32005-10-29 10:28:33 -05002131 "range: max iotag x%x wd0 x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002132 iotag, phba->sli.last_iotag,
James Bottomley604a3e32005-10-29 10:28:33 -05002133 *(((uint32_t *) &prspiocb->iocb) + 7));
dea31012005-04-17 16:05:31 -05002134 return NULL;
2135}
2136
James Smarte59058c2008-08-24 21:49:00 -04002137/**
James Smart3772a992009-05-22 14:50:54 -04002138 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2139 * @phba: Pointer to HBA context object.
2140 * @pring: Pointer to driver SLI ring object.
2141 * @iotag: IOCB tag.
2142 *
2143 * This function looks up the iocb_lookup table to get the command iocb
2144 * corresponding to the given iotag. This function is called with the
2145 * hbalock held.
2146 * This function returns the command iocb object if it finds the command
2147 * iocb else returns NULL.
2148 **/
2149static struct lpfc_iocbq *
2150lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2151 struct lpfc_sli_ring *pring, uint16_t iotag)
2152{
2153 struct lpfc_iocbq *cmd_iocb;
2154
2155 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2156 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2157 list_del_init(&cmd_iocb->list);
2158 pring->txcmplq_cnt--;
2159 return cmd_iocb;
2160 }
2161
2162 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2163 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2164 iotag, phba->sli.last_iotag);
2165 return NULL;
2166}
2167
2168/**
James Smart3621a712009-04-06 18:47:14 -04002169 * lpfc_sli_process_sol_iocb - process solicited iocb completion
James Smarte59058c2008-08-24 21:49:00 -04002170 * @phba: Pointer to HBA context object.
2171 * @pring: Pointer to driver SLI ring object.
2172 * @saveq: Pointer to the response iocb to be processed.
2173 *
2174 * This function is called by the ring event handler for non-fcp
2175 * rings when there is a new response iocb in the response ring.
2176 * The caller is not required to hold any locks. This function
2177 * gets the command iocb associated with the response iocb and
2178 * calls the completion handler for the command iocb. If there
2179 * is no completion handler, the function will free the resources
2180 * associated with command iocb. If the response iocb is for
2181 * an already aborted command iocb, the status of the completion
2182 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2183 * This function always returns 1.
2184 **/
dea31012005-04-17 16:05:31 -05002185static int
James Smart2e0fef82007-06-17 19:56:36 -05002186lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea31012005-04-17 16:05:31 -05002187 struct lpfc_iocbq *saveq)
2188{
James Smart2e0fef82007-06-17 19:56:36 -05002189 struct lpfc_iocbq *cmdiocbp;
dea31012005-04-17 16:05:31 -05002190 int rc = 1;
2191 unsigned long iflag;
2192
2193 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
James Smart2e0fef82007-06-17 19:56:36 -05002194 spin_lock_irqsave(&phba->hbalock, iflag);
James Bottomley604a3e32005-10-29 10:28:33 -05002195 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
James Smart2e0fef82007-06-17 19:56:36 -05002196 spin_unlock_irqrestore(&phba->hbalock, iflag);
2197
dea31012005-04-17 16:05:31 -05002198 if (cmdiocbp) {
2199 if (cmdiocbp->iocb_cmpl) {
2200 /*
James Smartea2151b2008-09-07 11:52:10 -04002201 * If an ELS command failed send an event to mgmt
2202 * application.
2203 */
2204 if (saveq->iocb.ulpStatus &&
2205 (pring->ringno == LPFC_ELS_RING) &&
2206 (cmdiocbp->iocb.ulpCommand ==
2207 CMD_ELS_REQUEST64_CR))
2208 lpfc_send_els_failure_event(phba,
2209 cmdiocbp, saveq);
2210
2211 /*
dea31012005-04-17 16:05:31 -05002212 * Post all ELS completions to the worker thread.
2213 * All other are passed to the completion callback.
2214 */
2215 if (pring->ringno == LPFC_ELS_RING) {
James Smart07951072007-04-25 09:51:38 -04002216 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2217 cmdiocbp->iocb_flag &=
2218 ~LPFC_DRIVER_ABORTED;
2219 saveq->iocb.ulpStatus =
2220 IOSTAT_LOCAL_REJECT;
2221 saveq->iocb.un.ulpWord[4] =
2222 IOERR_SLI_ABORTED;
James Smart0ff10d42008-01-11 01:52:36 -05002223
2224 /* Firmware could still be in progress
2225 * of DMAing payload, so don't free data
2226 * buffer till after a hbeat.
2227 */
2228 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
James Smart07951072007-04-25 09:51:38 -04002229 }
dea31012005-04-17 16:05:31 -05002230 }
James Smart2e0fef82007-06-17 19:56:36 -05002231 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
James Bottomley604a3e32005-10-29 10:28:33 -05002232 } else
2233 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea31012005-04-17 16:05:31 -05002234 } else {
2235 /*
2236 * Unknown initiating command based on the response iotag.
2237 * This could be the case on the ELS ring because of
2238 * lpfc_els_abort().
2239 */
2240 if (pring->ringno != LPFC_ELS_RING) {
2241 /*
2242 * Ring <ringno> handler: unexpected completion IoTag
2243 * <IoTag>
2244 */
James Smarta257bf92009-04-06 18:48:10 -04002245 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002246 "0322 Ring %d handler: "
2247 "unexpected completion IoTag x%x "
2248 "Data: x%x x%x x%x x%x\n",
2249 pring->ringno,
2250 saveq->iocb.ulpIoTag,
2251 saveq->iocb.ulpStatus,
2252 saveq->iocb.un.ulpWord[4],
2253 saveq->iocb.ulpCommand,
2254 saveq->iocb.ulpContext);
dea31012005-04-17 16:05:31 -05002255 }
2256 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04002257
dea31012005-04-17 16:05:31 -05002258 return rc;
2259}
2260
James Smarte59058c2008-08-24 21:49:00 -04002261/**
James Smart3621a712009-04-06 18:47:14 -04002262 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
James Smarte59058c2008-08-24 21:49:00 -04002263 * @phba: Pointer to HBA context object.
2264 * @pring: Pointer to driver SLI ring object.
2265 *
2266 * This function is called from the iocb ring event handlers when
2267 * put pointer is ahead of the get pointer for a ring. This function signal
2268 * an error attention condition to the worker thread and the worker
2269 * thread will transition the HBA to offline state.
2270 **/
James Smart2e0fef82007-06-17 19:56:36 -05002271static void
2272lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002273{
James Smart34b02dc2008-08-24 21:49:55 -04002274 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002275 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002276 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002277 * rsp ring <portRspMax>
2278 */
2279 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002280 "0312 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002281 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002282 pring->ringno, le32_to_cpu(pgp->rspPutInx),
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002283 pring->numRiocb);
2284
James Smart2e0fef82007-06-17 19:56:36 -05002285 phba->link_state = LPFC_HBA_ERROR;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002286
2287 /*
2288 * All error attention handlers are posted to
2289 * worker thread
2290 */
2291 phba->work_ha |= HA_ERATT;
2292 phba->work_hs = HS_FFER3;
James Smart92d7f7b2007-06-17 19:56:38 -05002293
James Smart5e9d9b82008-06-14 22:52:53 -04002294 lpfc_worker_wake_up(phba);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002295
2296 return;
2297}
2298
James Smarte59058c2008-08-24 21:49:00 -04002299/**
James Smart3621a712009-04-06 18:47:14 -04002300 * lpfc_poll_eratt - Error attention polling timer timeout handler
James Smart93996272008-08-24 21:50:30 -04002301 * @ptr: Pointer to address of HBA context object.
2302 *
2303 * This function is invoked by the Error Attention polling timer when the
2304 * timer times out. It will check the SLI Error Attention register for
2305 * possible attention events. If so, it will post an Error Attention event
2306 * and wake up worker thread to process it. Otherwise, it will set up the
2307 * Error Attention polling timer for the next poll.
2308 **/
2309void lpfc_poll_eratt(unsigned long ptr)
2310{
2311 struct lpfc_hba *phba;
2312 uint32_t eratt = 0;
2313
2314 phba = (struct lpfc_hba *)ptr;
2315
2316 /* Check chip HA register for error event */
2317 eratt = lpfc_sli_check_eratt(phba);
2318
2319 if (eratt)
2320 /* Tell the worker thread there is work to do */
2321 lpfc_worker_wake_up(phba);
2322 else
2323 /* Restart the timer for next eratt poll */
2324 mod_timer(&phba->eratt_poll, jiffies +
2325 HZ * LPFC_ERATT_POLL_INTERVAL);
2326 return;
2327}
2328
2329/**
James Smart3621a712009-04-06 18:47:14 -04002330 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
James Smarte59058c2008-08-24 21:49:00 -04002331 * @phba: Pointer to HBA context object.
2332 *
2333 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
2334 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
2335 * is enabled.
2336 *
2337 * The caller does not hold any lock.
2338 * The function processes each response iocb in the response ring until it
2339 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2340 * LE bit set. The function will call the completion handler of the command iocb
2341 * if the response iocb indicates a completion for a command iocb or it is
2342 * an abort completion.
2343 **/
James Smart2e0fef82007-06-17 19:56:36 -05002344void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002345{
James Smart2e0fef82007-06-17 19:56:36 -05002346 struct lpfc_sli *psli = &phba->sli;
2347 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002348 IOCB_t *irsp = NULL;
2349 IOCB_t *entry = NULL;
2350 struct lpfc_iocbq *cmdiocbq = NULL;
2351 struct lpfc_iocbq rspiocbq;
James Smart34b02dc2008-08-24 21:49:55 -04002352 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002353 uint32_t status;
2354 uint32_t portRspPut, portRspMax;
2355 int type;
2356 uint32_t rsp_cmpl = 0;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002357 uint32_t ha_copy;
James Smart2e0fef82007-06-17 19:56:36 -05002358 unsigned long iflags;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002359
2360 pring->stats.iocb_event++;
2361
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002362 /*
2363 * The next available response entry should never exceed the maximum
2364 * entries. If it does, treat it as an adapter hardware error.
2365 */
2366 portRspMax = pring->numRiocb;
2367 portRspPut = le32_to_cpu(pgp->rspPutInx);
2368 if (unlikely(portRspPut >= portRspMax)) {
2369 lpfc_sli_rsp_pointers_error(phba, pring);
2370 return;
2371 }
2372
2373 rmb();
2374 while (pring->rspidx != portRspPut) {
James Smarted957682007-06-17 19:56:37 -05002375 entry = lpfc_resp_iocb(phba, pring);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002376 if (++pring->rspidx >= portRspMax)
2377 pring->rspidx = 0;
2378
2379 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2380 (uint32_t *) &rspiocbq.iocb,
James Smart92d7f7b2007-06-17 19:56:38 -05002381 phba->iocb_rsp_size);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002382 irsp = &rspiocbq.iocb;
2383 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2384 pring->stats.iocb_rsp++;
2385 rsp_cmpl++;
2386
2387 if (unlikely(irsp->ulpStatus)) {
2388 /* Rsp ring <ringno> error: IOCB */
2389 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002390 "0326 Rsp Ring %d error: IOCB Data: "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002391 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002392 pring->ringno,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002393 irsp->un.ulpWord[0],
2394 irsp->un.ulpWord[1],
2395 irsp->un.ulpWord[2],
2396 irsp->un.ulpWord[3],
2397 irsp->un.ulpWord[4],
2398 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002399 *(uint32_t *)&irsp->un1,
2400 *((uint32_t *)&irsp->un1 + 1));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002401 }
2402
2403 switch (type) {
2404 case LPFC_ABORT_IOCB:
2405 case LPFC_SOL_IOCB:
2406 /*
2407 * Idle exchange closed via ABTS from port. No iocb
2408 * resources need to be recovered.
2409 */
2410 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002411 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002412 "0314 IOCB cmd 0x%x "
2413 "processed. Skipping "
2414 "completion",
James Smartdca94792006-08-01 07:34:08 -04002415 irsp->ulpCommand);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002416 break;
2417 }
2418
James Smart2e0fef82007-06-17 19:56:36 -05002419 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002420 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2421 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002422 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002423 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2424 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2425 &rspiocbq);
2426 }
2427 break;
2428 default:
2429 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2430 char adaptermsg[LPFC_MAX_ADPTMSG];
2431 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2432 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2433 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002434 dev_warn(&((phba->pcidev)->dev),
2435 "lpfc%d: %s\n",
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002436 phba->brd_no, adaptermsg);
2437 } else {
2438 /* Unknown IOCB command */
2439 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002440 "0321 Unknown IOCB command "
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002441 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002442 type, irsp->ulpCommand,
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002443 irsp->ulpStatus,
2444 irsp->ulpIoTag,
2445 irsp->ulpContext);
2446 }
2447 break;
2448 }
2449
2450 /*
2451 * The response IOCB has been processed. Update the ring
2452 * pointer in SLIM. If the port response put pointer has not
2453 * been updated, sync the pgp->rspPutInx and fetch the new port
2454 * response put pointer.
2455 */
James Smarted957682007-06-17 19:56:37 -05002456 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002457
2458 if (pring->rspidx == portRspPut)
2459 portRspPut = le32_to_cpu(pgp->rspPutInx);
2460 }
2461
2462 ha_copy = readl(phba->HAregaddr);
2463 ha_copy >>= (LPFC_FCP_RING * 4);
2464
2465 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
James Smart2e0fef82007-06-17 19:56:36 -05002466 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002467 pring->stats.iocb_rsp_full++;
2468 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
2469 writel(status, phba->CAregaddr);
2470 readl(phba->CAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05002471 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002472 }
2473 if ((ha_copy & HA_R0CE_RSP) &&
2474 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
James Smart2e0fef82007-06-17 19:56:36 -05002475 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002476 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2477 pring->stats.iocb_cmd_empty++;
2478
2479 /* Force update of the local copy of cmdGetInx */
2480 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2481 lpfc_sli_resume_iocb(phba, pring);
2482
2483 if ((pring->lpfc_sli_cmd_available))
2484 (pring->lpfc_sli_cmd_available) (phba, pring);
2485
James Smart2e0fef82007-06-17 19:56:36 -05002486 spin_unlock_irqrestore(&phba->hbalock, iflags);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002487 }
2488
2489 return;
2490}
2491
James Smarte59058c2008-08-24 21:49:00 -04002492/**
James Smart3621a712009-04-06 18:47:14 -04002493 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
James Smarte59058c2008-08-24 21:49:00 -04002494 * @phba: Pointer to HBA context object.
2495 * @pring: Pointer to driver SLI ring object.
2496 * @mask: Host attention register mask for this ring.
2497 *
2498 * This function is called from the interrupt context when there is a ring
2499 * event for the fcp ring. The caller does not hold any lock.
2500 * The function processes each response iocb in the response ring until it
2501 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2502 * LE bit set. The function will call the completion handler of the command iocb
2503 * if the response iocb indicates a completion for a command iocb or it is
2504 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2505 * function if this is an unsolicited iocb.
dea31012005-04-17 16:05:31 -05002506 * This routine presumes LPFC_FCP_RING handling and doesn't bother
James Smarte59058c2008-08-24 21:49:00 -04002507 * to check it explicitly. This function always returns 1.
2508 **/
dea31012005-04-17 16:05:31 -05002509static int
James Smart2e0fef82007-06-17 19:56:36 -05002510lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2511 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002512{
James Smart34b02dc2008-08-24 21:49:55 -04002513 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea31012005-04-17 16:05:31 -05002514 IOCB_t *irsp = NULL;
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002515 IOCB_t *entry = NULL;
dea31012005-04-17 16:05:31 -05002516 struct lpfc_iocbq *cmdiocbq = NULL;
2517 struct lpfc_iocbq rspiocbq;
dea31012005-04-17 16:05:31 -05002518 uint32_t status;
2519 uint32_t portRspPut, portRspMax;
2520 int rc = 1;
2521 lpfc_iocb_type type;
2522 unsigned long iflag;
2523 uint32_t rsp_cmpl = 0;
dea31012005-04-17 16:05:31 -05002524
James Smart2e0fef82007-06-17 19:56:36 -05002525 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002526 pring->stats.iocb_event++;
2527
dea31012005-04-17 16:05:31 -05002528 /*
2529 * The next available response entry should never exceed the maximum
2530 * entries. If it does, treat it as an adapter hardware error.
2531 */
2532 portRspMax = pring->numRiocb;
2533 portRspPut = le32_to_cpu(pgp->rspPutInx);
2534 if (unlikely(portRspPut >= portRspMax)) {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002535 lpfc_sli_rsp_pointers_error(phba, pring);
James Smart2e0fef82007-06-17 19:56:36 -05002536 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002537 return 1;
2538 }
2539
2540 rmb();
2541 while (pring->rspidx != portRspPut) {
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002542 /*
2543 * Fetch an entry off the ring and copy it into a local data
2544 * structure. The copy involves a byte-swap since the
2545 * network byte order and pci byte orders are different.
2546 */
James Smarted957682007-06-17 19:56:37 -05002547 entry = lpfc_resp_iocb(phba, pring);
James Smart858c9f62007-06-17 19:56:39 -05002548 phba->last_completion_time = jiffies;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05002549
2550 if (++pring->rspidx >= portRspMax)
2551 pring->rspidx = 0;
2552
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002553 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2554 (uint32_t *) &rspiocbq.iocb,
James Smarted957682007-06-17 19:56:37 -05002555 phba->iocb_rsp_size);
James Smarta4bc3372006-12-02 13:34:16 -05002556 INIT_LIST_HEAD(&(rspiocbq.list));
James.Smart@Emulex.Com87f6eaf2005-06-25 10:34:13 -04002557 irsp = &rspiocbq.iocb;
2558
dea31012005-04-17 16:05:31 -05002559 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2560 pring->stats.iocb_rsp++;
2561 rsp_cmpl++;
2562
2563 if (unlikely(irsp->ulpStatus)) {
James Smart92d7f7b2007-06-17 19:56:38 -05002564 /*
2565 * If resource errors reported from HBA, reduce
2566 * queuedepths of the SCSI device.
2567 */
2568 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2569 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2570 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04002571 phba->lpfc_rampdown_queue_depth(phba);
James Smart92d7f7b2007-06-17 19:56:38 -05002572 spin_lock_irqsave(&phba->hbalock, iflag);
2573 }
2574
dea31012005-04-17 16:05:31 -05002575 /* Rsp ring <ringno> error: IOCB */
2576 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002577 "0336 Rsp Ring %d error: IOCB Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05002578 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002579 pring->ringno,
James Smart92d7f7b2007-06-17 19:56:38 -05002580 irsp->un.ulpWord[0],
2581 irsp->un.ulpWord[1],
2582 irsp->un.ulpWord[2],
2583 irsp->un.ulpWord[3],
2584 irsp->un.ulpWord[4],
2585 irsp->un.ulpWord[5],
James Smartd7c255b2008-08-24 21:50:00 -04002586 *(uint32_t *)&irsp->un1,
2587 *((uint32_t *)&irsp->un1 + 1));
dea31012005-04-17 16:05:31 -05002588 }
2589
2590 switch (type) {
2591 case LPFC_ABORT_IOCB:
2592 case LPFC_SOL_IOCB:
2593 /*
2594 * Idle exchange closed via ABTS from port. No iocb
2595 * resources need to be recovered.
2596 */
2597 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
James Smartdca94792006-08-01 07:34:08 -04002598 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002599 "0333 IOCB cmd 0x%x"
James Smartdca94792006-08-01 07:34:08 -04002600 " processed. Skipping"
James Smart92d7f7b2007-06-17 19:56:38 -05002601 " completion\n",
James Smartdca94792006-08-01 07:34:08 -04002602 irsp->ulpCommand);
dea31012005-04-17 16:05:31 -05002603 break;
2604 }
2605
James Bottomley604a3e32005-10-29 10:28:33 -05002606 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2607 &rspiocbq);
dea31012005-04-17 16:05:31 -05002608 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002609 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2610 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2611 &rspiocbq);
2612 } else {
James Smart2e0fef82007-06-17 19:56:36 -05002613 spin_unlock_irqrestore(&phba->hbalock,
2614 iflag);
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002615 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2616 &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002617 spin_lock_irqsave(&phba->hbalock,
Jamie Wellnitzb8086082006-02-28 22:33:12 -05002618 iflag);
2619 }
dea31012005-04-17 16:05:31 -05002620 }
2621 break;
James Smarta4bc3372006-12-02 13:34:16 -05002622 case LPFC_UNSOL_IOCB:
James Smart2e0fef82007-06-17 19:56:36 -05002623 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002624 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
James Smart2e0fef82007-06-17 19:56:36 -05002625 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta4bc3372006-12-02 13:34:16 -05002626 break;
dea31012005-04-17 16:05:31 -05002627 default:
2628 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2629 char adaptermsg[LPFC_MAX_ADPTMSG];
2630 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2631 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2632 MAX_MSG_DATA);
Joe Perches898eb712007-10-18 03:06:30 -07002633 dev_warn(&((phba->pcidev)->dev),
2634 "lpfc%d: %s\n",
dea31012005-04-17 16:05:31 -05002635 phba->brd_no, adaptermsg);
2636 } else {
2637 /* Unknown IOCB command */
2638 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002639 "0334 Unknown IOCB command "
James Smart92d7f7b2007-06-17 19:56:38 -05002640 "Data: x%x, x%x x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04002641 type, irsp->ulpCommand,
James Smart92d7f7b2007-06-17 19:56:38 -05002642 irsp->ulpStatus,
2643 irsp->ulpIoTag,
2644 irsp->ulpContext);
dea31012005-04-17 16:05:31 -05002645 }
2646 break;
2647 }
2648
2649 /*
2650 * The response IOCB has been processed. Update the ring
2651 * pointer in SLIM. If the port response put pointer has not
2652 * been updated, sync the pgp->rspPutInx and fetch the new port
2653 * response put pointer.
2654 */
James Smarted957682007-06-17 19:56:37 -05002655 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002656
2657 if (pring->rspidx == portRspPut)
2658 portRspPut = le32_to_cpu(pgp->rspPutInx);
2659 }
2660
2661 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2662 pring->stats.iocb_rsp_full++;
2663 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2664 writel(status, phba->CAregaddr);
2665 readl(phba->CAregaddr);
2666 }
2667 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2668 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2669 pring->stats.iocb_cmd_empty++;
2670
2671 /* Force update of the local copy of cmdGetInx */
2672 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2673 lpfc_sli_resume_iocb(phba, pring);
2674
2675 if ((pring->lpfc_sli_cmd_available))
2676 (pring->lpfc_sli_cmd_available) (phba, pring);
2677
2678 }
2679
James Smart2e0fef82007-06-17 19:56:36 -05002680 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002681 return rc;
2682}
2683
James Smarte59058c2008-08-24 21:49:00 -04002684/**
James Smart3772a992009-05-22 14:50:54 -04002685 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2686 * @phba: Pointer to HBA context object.
2687 * @pring: Pointer to driver SLI ring object.
2688 * @rspiocbp: Pointer to driver response IOCB object.
2689 *
2690 * This function is called from the worker thread when there is a slow-path
2691 * response IOCB to process. This function chains all the response iocbs until
2692 * seeing the iocb with the LE bit set. The function will call
2693 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2694 * completion of a command iocb. The function will call the
2695 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2696 * The function frees the resources or calls the completion handler if this
2697 * iocb is an abort completion. The function returns NULL when the response
2698 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2699 * this function shall chain the iocb on to the iocb_continueq and return the
2700 * response iocb passed in.
2701 **/
2702static struct lpfc_iocbq *
2703lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2704 struct lpfc_iocbq *rspiocbp)
2705{
2706 struct lpfc_iocbq *saveq;
2707 struct lpfc_iocbq *cmdiocbp;
2708 struct lpfc_iocbq *next_iocb;
2709 IOCB_t *irsp = NULL;
2710 uint32_t free_saveq;
2711 uint8_t iocb_cmd_type;
2712 lpfc_iocb_type type;
2713 unsigned long iflag;
2714 int rc;
2715
2716 spin_lock_irqsave(&phba->hbalock, iflag);
2717 /* First add the response iocb to the countinueq list */
2718 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2719 pring->iocb_continueq_cnt++;
2720
2721 /* Now, determine whetehr the list is completed for processing */
2722 irsp = &rspiocbp->iocb;
2723 if (irsp->ulpLe) {
2724 /*
2725 * By default, the driver expects to free all resources
2726 * associated with this iocb completion.
2727 */
2728 free_saveq = 1;
2729 saveq = list_get_first(&pring->iocb_continueq,
2730 struct lpfc_iocbq, list);
2731 irsp = &(saveq->iocb);
2732 list_del_init(&pring->iocb_continueq);
2733 pring->iocb_continueq_cnt = 0;
2734
2735 pring->stats.iocb_rsp++;
2736
2737 /*
2738 * If resource errors reported from HBA, reduce
2739 * queuedepths of the SCSI device.
2740 */
2741 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2742 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2743 spin_unlock_irqrestore(&phba->hbalock, iflag);
2744 phba->lpfc_rampdown_queue_depth(phba);
2745 spin_lock_irqsave(&phba->hbalock, iflag);
2746 }
2747
2748 if (irsp->ulpStatus) {
2749 /* Rsp ring <ringno> error: IOCB */
2750 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2751 "0328 Rsp Ring %d error: "
2752 "IOCB Data: "
2753 "x%x x%x x%x x%x "
2754 "x%x x%x x%x x%x "
2755 "x%x x%x x%x x%x "
2756 "x%x x%x x%x x%x\n",
2757 pring->ringno,
2758 irsp->un.ulpWord[0],
2759 irsp->un.ulpWord[1],
2760 irsp->un.ulpWord[2],
2761 irsp->un.ulpWord[3],
2762 irsp->un.ulpWord[4],
2763 irsp->un.ulpWord[5],
2764 *(((uint32_t *) irsp) + 6),
2765 *(((uint32_t *) irsp) + 7),
2766 *(((uint32_t *) irsp) + 8),
2767 *(((uint32_t *) irsp) + 9),
2768 *(((uint32_t *) irsp) + 10),
2769 *(((uint32_t *) irsp) + 11),
2770 *(((uint32_t *) irsp) + 12),
2771 *(((uint32_t *) irsp) + 13),
2772 *(((uint32_t *) irsp) + 14),
2773 *(((uint32_t *) irsp) + 15));
2774 }
2775
2776 /*
2777 * Fetch the IOCB command type and call the correct completion
2778 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2779 * get freed back to the lpfc_iocb_list by the discovery
2780 * kernel thread.
2781 */
2782 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2783 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2784 switch (type) {
2785 case LPFC_SOL_IOCB:
2786 spin_unlock_irqrestore(&phba->hbalock, iflag);
2787 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2788 spin_lock_irqsave(&phba->hbalock, iflag);
2789 break;
2790
2791 case LPFC_UNSOL_IOCB:
2792 spin_unlock_irqrestore(&phba->hbalock, iflag);
2793 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2794 spin_lock_irqsave(&phba->hbalock, iflag);
2795 if (!rc)
2796 free_saveq = 0;
2797 break;
2798
2799 case LPFC_ABORT_IOCB:
2800 cmdiocbp = NULL;
2801 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2802 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2803 saveq);
2804 if (cmdiocbp) {
2805 /* Call the specified completion routine */
2806 if (cmdiocbp->iocb_cmpl) {
2807 spin_unlock_irqrestore(&phba->hbalock,
2808 iflag);
2809 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2810 saveq);
2811 spin_lock_irqsave(&phba->hbalock,
2812 iflag);
2813 } else
2814 __lpfc_sli_release_iocbq(phba,
2815 cmdiocbp);
2816 }
2817 break;
2818
2819 case LPFC_UNKNOWN_IOCB:
2820 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2821 char adaptermsg[LPFC_MAX_ADPTMSG];
2822 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2823 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2824 MAX_MSG_DATA);
2825 dev_warn(&((phba->pcidev)->dev),
2826 "lpfc%d: %s\n",
2827 phba->brd_no, adaptermsg);
2828 } else {
2829 /* Unknown IOCB command */
2830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2831 "0335 Unknown IOCB "
2832 "command Data: x%x "
2833 "x%x x%x x%x\n",
2834 irsp->ulpCommand,
2835 irsp->ulpStatus,
2836 irsp->ulpIoTag,
2837 irsp->ulpContext);
2838 }
2839 break;
2840 }
2841
2842 if (free_saveq) {
2843 list_for_each_entry_safe(rspiocbp, next_iocb,
2844 &saveq->list, list) {
2845 list_del(&rspiocbp->list);
2846 __lpfc_sli_release_iocbq(phba, rspiocbp);
2847 }
2848 __lpfc_sli_release_iocbq(phba, saveq);
2849 }
2850 rspiocbp = NULL;
2851 }
2852 spin_unlock_irqrestore(&phba->hbalock, iflag);
2853 return rspiocbp;
2854}
2855
2856/**
2857 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
James Smarte59058c2008-08-24 21:49:00 -04002858 * @phba: Pointer to HBA context object.
2859 * @pring: Pointer to driver SLI ring object.
2860 * @mask: Host attention register mask for this ring.
2861 *
James Smart3772a992009-05-22 14:50:54 -04002862 * This routine wraps the actual slow_ring event process routine from the
2863 * API jump table function pointer from the lpfc_hba struct.
James Smarte59058c2008-08-24 21:49:00 -04002864 **/
James Smart3772a992009-05-22 14:50:54 -04002865void
James Smart2e0fef82007-06-17 19:56:36 -05002866lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2867 struct lpfc_sli_ring *pring, uint32_t mask)
dea31012005-04-17 16:05:31 -05002868{
James Smart3772a992009-05-22 14:50:54 -04002869 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2870}
2871
2872/**
2873 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2874 * @phba: Pointer to HBA context object.
2875 * @pring: Pointer to driver SLI ring object.
2876 * @mask: Host attention register mask for this ring.
2877 *
2878 * This function is called from the worker thread when there is a ring event
2879 * for non-fcp rings. The caller does not hold any lock. The function will
2880 * remove each response iocb in the response ring and calls the handle
2881 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2882 **/
2883static void
2884lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2885 struct lpfc_sli_ring *pring, uint32_t mask)
2886{
James Smart34b02dc2008-08-24 21:49:55 -04002887 struct lpfc_pgp *pgp;
dea31012005-04-17 16:05:31 -05002888 IOCB_t *entry;
2889 IOCB_t *irsp = NULL;
2890 struct lpfc_iocbq *rspiocbp = NULL;
dea31012005-04-17 16:05:31 -05002891 uint32_t portRspPut, portRspMax;
dea31012005-04-17 16:05:31 -05002892 unsigned long iflag;
James Smart3772a992009-05-22 14:50:54 -04002893 uint32_t status;
dea31012005-04-17 16:05:31 -05002894
James Smart34b02dc2008-08-24 21:49:55 -04002895 pgp = &phba->port_gp[pring->ringno];
James Smart2e0fef82007-06-17 19:56:36 -05002896 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002897 pring->stats.iocb_event++;
2898
dea31012005-04-17 16:05:31 -05002899 /*
2900 * The next available response entry should never exceed the maximum
2901 * entries. If it does, treat it as an adapter hardware error.
2902 */
2903 portRspMax = pring->numRiocb;
2904 portRspPut = le32_to_cpu(pgp->rspPutInx);
2905 if (portRspPut >= portRspMax) {
2906 /*
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002907 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea31012005-04-17 16:05:31 -05002908 * rsp ring <portRspMax>
2909 */
James Smarted957682007-06-17 19:56:37 -05002910 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04002911 "0303 Ring %d handler: portRspPut %d "
Frederik Schwarzer025dfda2008-10-16 19:02:37 +02002912 "is bigger than rsp ring %d\n",
James Smarte8b62012007-08-02 11:10:09 -04002913 pring->ringno, portRspPut, portRspMax);
dea31012005-04-17 16:05:31 -05002914
James Smart2e0fef82007-06-17 19:56:36 -05002915 phba->link_state = LPFC_HBA_ERROR;
2916 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002917
2918 phba->work_hs = HS_FFER3;
2919 lpfc_handle_eratt(phba);
2920
James Smart3772a992009-05-22 14:50:54 -04002921 return;
dea31012005-04-17 16:05:31 -05002922 }
2923
2924 rmb();
dea31012005-04-17 16:05:31 -05002925 while (pring->rspidx != portRspPut) {
2926 /*
2927 * Build a completion list and call the appropriate handler.
2928 * The process is to get the next available response iocb, get
2929 * a free iocb from the list, copy the response data into the
2930 * free iocb, insert to the continuation list, and update the
2931 * next response index to slim. This process makes response
2932 * iocb's in the ring available to DMA as fast as possible but
2933 * pays a penalty for a copy operation. Since the iocb is
2934 * only 32 bytes, this penalty is considered small relative to
2935 * the PCI reads for register values and a slim write. When
2936 * the ulpLe field is set, the entire Command has been
2937 * received.
2938 */
James Smarted957682007-06-17 19:56:37 -05002939 entry = lpfc_resp_iocb(phba, pring);
2940
James Smart858c9f62007-06-17 19:56:39 -05002941 phba->last_completion_time = jiffies;
James Smart2e0fef82007-06-17 19:56:36 -05002942 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05002943 if (rspiocbp == NULL) {
2944 printk(KERN_ERR "%s: out of buffers! Failing "
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07002945 "completion.\n", __func__);
dea31012005-04-17 16:05:31 -05002946 break;
2947 }
2948
James Smarted957682007-06-17 19:56:37 -05002949 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2950 phba->iocb_rsp_size);
dea31012005-04-17 16:05:31 -05002951 irsp = &rspiocbp->iocb;
2952
2953 if (++pring->rspidx >= portRspMax)
2954 pring->rspidx = 0;
2955
James Smarta58cbd52007-08-02 11:09:43 -04002956 if (pring->ringno == LPFC_ELS_RING) {
2957 lpfc_debugfs_slow_ring_trc(phba,
2958 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2959 *(((uint32_t *) irsp) + 4),
2960 *(((uint32_t *) irsp) + 6),
2961 *(((uint32_t *) irsp) + 7));
2962 }
2963
James Smarted957682007-06-17 19:56:37 -05002964 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea31012005-04-17 16:05:31 -05002965
James Smart3772a992009-05-22 14:50:54 -04002966 spin_unlock_irqrestore(&phba->hbalock, iflag);
2967 /* Handle the response IOCB */
2968 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2969 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05002970
2971 /*
2972 * If the port response put pointer has not been updated, sync
2973 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2974 * response put pointer.
2975 */
2976 if (pring->rspidx == portRspPut) {
2977 portRspPut = le32_to_cpu(pgp->rspPutInx);
2978 }
2979 } /* while (pring->rspidx != portRspPut) */
2980
James Smart92d7f7b2007-06-17 19:56:38 -05002981 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea31012005-04-17 16:05:31 -05002982 /* At least one response entry has been freed */
2983 pring->stats.iocb_rsp_full++;
2984 /* SET RxRE_RSP in Chip Att register */
2985 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2986 writel(status, phba->CAregaddr);
2987 readl(phba->CAregaddr); /* flush */
2988 }
2989 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2990 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2991 pring->stats.iocb_cmd_empty++;
2992
2993 /* Force update of the local copy of cmdGetInx */
2994 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2995 lpfc_sli_resume_iocb(phba, pring);
2996
2997 if ((pring->lpfc_sli_cmd_available))
2998 (pring->lpfc_sli_cmd_available) (phba, pring);
2999
3000 }
3001
James Smart2e0fef82007-06-17 19:56:36 -05003002 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart3772a992009-05-22 14:50:54 -04003003 return;
dea31012005-04-17 16:05:31 -05003004}
3005
James Smarte59058c2008-08-24 21:49:00 -04003006/**
James Smart4f774512009-05-22 14:52:35 -04003007 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3008 * @phba: Pointer to HBA context object.
3009 * @pring: Pointer to driver SLI ring object.
3010 * @mask: Host attention register mask for this ring.
3011 *
3012 * This function is called from the worker thread when there is a pending
3013 * ELS response iocb on the driver internal slow-path response iocb worker
3014 * queue. The caller does not hold any lock. The function will remove each
3015 * response iocb from the response worker queue and calls the handle
3016 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3017 **/
3018static void
3019lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3020 struct lpfc_sli_ring *pring, uint32_t mask)
3021{
3022 struct lpfc_iocbq *irspiocbq;
James Smart4d9ab992009-10-02 15:16:39 -04003023 struct hbq_dmabuf *dmabuf;
3024 struct lpfc_cq_event *cq_event;
James Smart4f774512009-05-22 14:52:35 -04003025 unsigned long iflag;
3026
3027 while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
3028 /* Get the response iocb from the head of work queue */
3029 spin_lock_irqsave(&phba->hbalock, iflag);
3030 list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
James Smart4d9ab992009-10-02 15:16:39 -04003031 cq_event, struct lpfc_cq_event, list);
James Smart4f774512009-05-22 14:52:35 -04003032 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart4d9ab992009-10-02 15:16:39 -04003033
3034 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3035 case CQE_CODE_COMPL_WQE:
3036 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3037 cq_event);
3038 lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
3039 break;
3040 case CQE_CODE_RECEIVE:
3041 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3042 cq_event);
3043 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3044 break;
3045 default:
3046 break;
3047 }
James Smart4f774512009-05-22 14:52:35 -04003048 }
3049}
3050
3051/**
James Smart3621a712009-04-06 18:47:14 -04003052 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
James Smarte59058c2008-08-24 21:49:00 -04003053 * @phba: Pointer to HBA context object.
3054 * @pring: Pointer to driver SLI ring object.
3055 *
3056 * This function aborts all iocbs in the given ring and frees all the iocb
3057 * objects in txq. This function issues an abort iocb for all the iocb commands
3058 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3059 * the return of this function. The caller is not required to hold any locks.
3060 **/
James Smart2e0fef82007-06-17 19:56:36 -05003061void
dea31012005-04-17 16:05:31 -05003062lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3063{
James Smart2534ba72007-04-25 09:52:20 -04003064 LIST_HEAD(completions);
dea31012005-04-17 16:05:31 -05003065 struct lpfc_iocbq *iocb, *next_iocb;
dea31012005-04-17 16:05:31 -05003066
James Smart92d7f7b2007-06-17 19:56:38 -05003067 if (pring->ringno == LPFC_ELS_RING) {
3068 lpfc_fabric_abort_hba(phba);
3069 }
3070
dea31012005-04-17 16:05:31 -05003071 /* Error everything on txq and txcmplq
3072 * First do the txq.
3073 */
James Smart2e0fef82007-06-17 19:56:36 -05003074 spin_lock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003075 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05003076 pring->txq_cnt = 0;
dea31012005-04-17 16:05:31 -05003077
3078 /* Next issue ABTS for everything on the txcmplq */
James Smart2534ba72007-04-25 09:52:20 -04003079 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3080 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3081
James Smart2e0fef82007-06-17 19:56:36 -05003082 spin_unlock_irq(&phba->hbalock);
James Smart2534ba72007-04-25 09:52:20 -04003083
James Smarta257bf92009-04-06 18:48:10 -04003084 /* Cancel all the IOCBs from the completions list */
3085 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3086 IOERR_SLI_ABORTED);
dea31012005-04-17 16:05:31 -05003087}
3088
James Smarte59058c2008-08-24 21:49:00 -04003089/**
James Smart3621a712009-04-06 18:47:14 -04003090 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
James Smarta8e497d2008-08-24 21:50:11 -04003091 * @phba: Pointer to HBA context object.
3092 *
3093 * This function flushes all iocbs in the fcp ring and frees all the iocb
3094 * objects in txq and txcmplq. This function will not issue abort iocbs
3095 * for all the iocb commands in txcmplq, they will just be returned with
3096 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3097 * slot has been permanently disabled.
3098 **/
3099void
3100lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3101{
3102 LIST_HEAD(txq);
3103 LIST_HEAD(txcmplq);
James Smarta8e497d2008-08-24 21:50:11 -04003104 struct lpfc_sli *psli = &phba->sli;
3105 struct lpfc_sli_ring *pring;
3106
3107 /* Currently, only one fcp ring */
3108 pring = &psli->ring[psli->fcp_ring];
3109
3110 spin_lock_irq(&phba->hbalock);
3111 /* Retrieve everything on txq */
3112 list_splice_init(&pring->txq, &txq);
3113 pring->txq_cnt = 0;
3114
3115 /* Retrieve everything on the txcmplq */
3116 list_splice_init(&pring->txcmplq, &txcmplq);
3117 pring->txcmplq_cnt = 0;
3118 spin_unlock_irq(&phba->hbalock);
3119
3120 /* Flush the txq */
James Smarta257bf92009-04-06 18:48:10 -04003121 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3122 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003123
3124 /* Flush the txcmpq */
James Smarta257bf92009-04-06 18:48:10 -04003125 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3126 IOERR_SLI_DOWN);
James Smarta8e497d2008-08-24 21:50:11 -04003127}
3128
3129/**
James Smart3772a992009-05-22 14:50:54 -04003130 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
James Smarte59058c2008-08-24 21:49:00 -04003131 * @phba: Pointer to HBA context object.
3132 * @mask: Bit mask to be checked.
3133 *
3134 * This function reads the host status register and compares
3135 * with the provided bit mask to check if HBA completed
3136 * the restart. This function will wait in a loop for the
3137 * HBA to complete restart. If the HBA does not restart within
3138 * 15 iterations, the function will reset the HBA again. The
3139 * function returns 1 when HBA fail to restart otherwise returns
3140 * zero.
3141 **/
James Smart3772a992009-05-22 14:50:54 -04003142static int
3143lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea31012005-04-17 16:05:31 -05003144{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003145 uint32_t status;
3146 int i = 0;
3147 int retval = 0;
dea31012005-04-17 16:05:31 -05003148
Jamie Wellnitz41415862006-02-28 19:25:27 -05003149 /* Read the HBA Host Status Register */
3150 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003151
Jamie Wellnitz41415862006-02-28 19:25:27 -05003152 /*
3153 * Check status register every 100ms for 5 retries, then every
3154 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3155 * every 2.5 sec for 4.
3156 * Break our of the loop if errors occurred during init.
3157 */
3158 while (((status & mask) != mask) &&
3159 !(status & HS_FFERM) &&
3160 i++ < 20) {
dea31012005-04-17 16:05:31 -05003161
Jamie Wellnitz41415862006-02-28 19:25:27 -05003162 if (i <= 5)
3163 msleep(10);
3164 else if (i <= 10)
3165 msleep(500);
3166 else
3167 msleep(2500);
dea31012005-04-17 16:05:31 -05003168
Jamie Wellnitz41415862006-02-28 19:25:27 -05003169 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003170 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003171 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003172 lpfc_sli_brdrestart(phba);
3173 }
3174 /* Read the HBA Host Status Register */
3175 status = readl(phba->HSregaddr);
dea31012005-04-17 16:05:31 -05003176 }
dea31012005-04-17 16:05:31 -05003177
Jamie Wellnitz41415862006-02-28 19:25:27 -05003178 /* Check to see if any errors occurred during init */
3179 if ((status & HS_FFERM) || (i >= 20)) {
James Smart2e0fef82007-06-17 19:56:36 -05003180 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003181 retval = 1;
3182 }
dea31012005-04-17 16:05:31 -05003183
Jamie Wellnitz41415862006-02-28 19:25:27 -05003184 return retval;
dea31012005-04-17 16:05:31 -05003185}
3186
James Smartda0436e2009-05-22 14:51:39 -04003187/**
3188 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3189 * @phba: Pointer to HBA context object.
3190 * @mask: Bit mask to be checked.
3191 *
3192 * This function checks the host status register to check if HBA is
3193 * ready. This function will wait in a loop for the HBA to be ready
3194 * If the HBA is not ready , the function will will reset the HBA PCI
3195 * function again. The function returns 1 when HBA fail to be ready
3196 * otherwise returns zero.
3197 **/
3198static int
3199lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3200{
3201 uint32_t status;
3202 int retval = 0;
3203
3204 /* Read the HBA Host Status Register */
3205 status = lpfc_sli4_post_status_check(phba);
3206
3207 if (status) {
3208 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3209 lpfc_sli_brdrestart(phba);
3210 status = lpfc_sli4_post_status_check(phba);
3211 }
3212
3213 /* Check to see if any errors occurred during init */
3214 if (status) {
3215 phba->link_state = LPFC_HBA_ERROR;
3216 retval = 1;
3217 } else
3218 phba->sli4_hba.intr_enable = 0;
3219
3220 return retval;
3221}
3222
3223/**
3224 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3225 * @phba: Pointer to HBA context object.
3226 * @mask: Bit mask to be checked.
3227 *
3228 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3229 * from the API jump table function pointer from the lpfc_hba struct.
3230 **/
3231int
3232lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3233{
3234 return phba->lpfc_sli_brdready(phba, mask);
3235}
3236
James Smart92908312006-03-07 15:04:13 -05003237#define BARRIER_TEST_PATTERN (0xdeadbeef)
3238
James Smarte59058c2008-08-24 21:49:00 -04003239/**
James Smart3621a712009-04-06 18:47:14 -04003240 * lpfc_reset_barrier - Make HBA ready for HBA reset
James Smarte59058c2008-08-24 21:49:00 -04003241 * @phba: Pointer to HBA context object.
3242 *
3243 * This function is called before resetting an HBA. This
3244 * function requests HBA to quiesce DMAs before a reset.
3245 **/
James Smart2e0fef82007-06-17 19:56:36 -05003246void lpfc_reset_barrier(struct lpfc_hba *phba)
James Smart92908312006-03-07 15:04:13 -05003247{
James Smart65a29c12006-07-06 15:50:50 -04003248 uint32_t __iomem *resp_buf;
3249 uint32_t __iomem *mbox_buf;
James Smart92908312006-03-07 15:04:13 -05003250 volatile uint32_t mbox;
3251 uint32_t hc_copy;
3252 int i;
3253 uint8_t hdrtype;
3254
3255 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3256 if (hdrtype != 0x80 ||
3257 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3258 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3259 return;
3260
3261 /*
3262 * Tell the other part of the chip to suspend temporarily all
3263 * its DMA activity.
3264 */
James Smart65a29c12006-07-06 15:50:50 -04003265 resp_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003266
3267 /* Disable the error attention */
3268 hc_copy = readl(phba->HCregaddr);
3269 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3270 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003271 phba->link_flag |= LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003272
3273 if (readl(phba->HAregaddr) & HA_ERATT) {
3274 /* Clear Chip error bit */
3275 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003276 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003277 }
3278
3279 mbox = 0;
3280 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3281 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3282
3283 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
James Smart65a29c12006-07-06 15:50:50 -04003284 mbox_buf = phba->MBslimaddr;
James Smart92908312006-03-07 15:04:13 -05003285 writel(mbox, mbox_buf);
3286
3287 for (i = 0;
3288 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3289 mdelay(1);
3290
3291 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
James Smartf4b4c682009-05-22 14:53:12 -04003292 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
James Smart2e0fef82007-06-17 19:56:36 -05003293 phba->pport->stopped)
James Smart92908312006-03-07 15:04:13 -05003294 goto restore_hc;
3295 else
3296 goto clear_errat;
3297 }
3298
3299 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3300 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3301 mdelay(1);
3302
3303clear_errat:
3304
3305 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3306 mdelay(1);
3307
3308 if (readl(phba->HAregaddr) & HA_ERATT) {
3309 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003310 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003311 }
3312
3313restore_hc:
James Smart2e0fef82007-06-17 19:56:36 -05003314 phba->link_flag &= ~LS_IGNORE_ERATT;
James Smart92908312006-03-07 15:04:13 -05003315 writel(hc_copy, phba->HCregaddr);
3316 readl(phba->HCregaddr); /* flush */
3317}
3318
James Smarte59058c2008-08-24 21:49:00 -04003319/**
James Smart3621a712009-04-06 18:47:14 -04003320 * lpfc_sli_brdkill - Issue a kill_board mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003321 * @phba: Pointer to HBA context object.
3322 *
3323 * This function issues a kill_board mailbox command and waits for
3324 * the error attention interrupt. This function is called for stopping
3325 * the firmware processing. The caller is not required to hold any
3326 * locks. This function calls lpfc_hba_down_post function to free
3327 * any pending commands after the kill. The function will return 1 when it
3328 * fails to kill the board else will return 0.
3329 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003330int
James Smart2e0fef82007-06-17 19:56:36 -05003331lpfc_sli_brdkill(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05003332{
Jamie Wellnitz41415862006-02-28 19:25:27 -05003333 struct lpfc_sli *psli;
3334 LPFC_MBOXQ_t *pmb;
3335 uint32_t status;
3336 uint32_t ha_copy;
3337 int retval;
3338 int i = 0;
3339
3340 psli = &phba->sli;
3341
3342 /* Kill HBA */
James Smarted957682007-06-17 19:56:37 -05003343 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003344 "0329 Kill HBA Data: x%x x%x\n",
3345 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003346
James Smart98c9ea52007-10-27 13:37:33 -04003347 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3348 if (!pmb)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003349 return 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003350
3351 /* Disable the error attention */
James Smart2e0fef82007-06-17 19:56:36 -05003352 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003353 status = readl(phba->HCregaddr);
3354 status &= ~HC_ERINT_ENA;
3355 writel(status, phba->HCregaddr);
3356 readl(phba->HCregaddr); /* flush */
James Smart2e0fef82007-06-17 19:56:36 -05003357 phba->link_flag |= LS_IGNORE_ERATT;
3358 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003359
3360 lpfc_kill_board(phba, pmb);
3361 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3362 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3363
3364 if (retval != MBX_SUCCESS) {
3365 if (retval != MBX_BUSY)
3366 mempool_free(pmb, phba->mbox_mem_pool);
James Smart2e0fef82007-06-17 19:56:36 -05003367 spin_lock_irq(&phba->hbalock);
3368 phba->link_flag &= ~LS_IGNORE_ERATT;
3369 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003370 return 1;
3371 }
3372
James Smartf4b4c682009-05-22 14:53:12 -04003373 spin_lock_irq(&phba->hbalock);
3374 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3375 spin_unlock_irq(&phba->hbalock);
James Smart92908312006-03-07 15:04:13 -05003376
Jamie Wellnitz41415862006-02-28 19:25:27 -05003377 mempool_free(pmb, phba->mbox_mem_pool);
3378
3379 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3380 * attention every 100ms for 3 seconds. If we don't get ERATT after
3381 * 3 seconds we still set HBA_ERROR state because the status of the
3382 * board is now undefined.
3383 */
3384 ha_copy = readl(phba->HAregaddr);
3385
3386 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3387 mdelay(100);
3388 ha_copy = readl(phba->HAregaddr);
3389 }
3390
3391 del_timer_sync(&psli->mbox_tmo);
James Smart92908312006-03-07 15:04:13 -05003392 if (ha_copy & HA_ERATT) {
3393 writel(HA_ERATT, phba->HAregaddr);
James Smart2e0fef82007-06-17 19:56:36 -05003394 phba->pport->stopped = 1;
James Smart92908312006-03-07 15:04:13 -05003395 }
James Smart2e0fef82007-06-17 19:56:36 -05003396 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003397 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart04c68492009-05-22 14:52:52 -04003398 psli->mbox_active = NULL;
James Smart2e0fef82007-06-17 19:56:36 -05003399 phba->link_flag &= ~LS_IGNORE_ERATT;
3400 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003401
Jamie Wellnitz41415862006-02-28 19:25:27 -05003402 lpfc_hba_down_post(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003403 phba->link_state = LPFC_HBA_ERROR;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003404
James Smart2e0fef82007-06-17 19:56:36 -05003405 return ha_copy & HA_ERATT ? 0 : 1;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003406}
3407
James Smarte59058c2008-08-24 21:49:00 -04003408/**
James Smart3772a992009-05-22 14:50:54 -04003409 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
James Smarte59058c2008-08-24 21:49:00 -04003410 * @phba: Pointer to HBA context object.
3411 *
3412 * This function resets the HBA by writing HC_INITFF to the control
3413 * register. After the HBA resets, this function resets all the iocb ring
3414 * indices. This function disables PCI layer parity checking during
3415 * the reset.
3416 * This function returns 0 always.
3417 * The caller is not required to hold any locks.
3418 **/
Jamie Wellnitz41415862006-02-28 19:25:27 -05003419int
James Smart2e0fef82007-06-17 19:56:36 -05003420lpfc_sli_brdreset(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003421{
3422 struct lpfc_sli *psli;
dea31012005-04-17 16:05:31 -05003423 struct lpfc_sli_ring *pring;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003424 uint16_t cfg_value;
dea31012005-04-17 16:05:31 -05003425 int i;
dea31012005-04-17 16:05:31 -05003426
Jamie Wellnitz41415862006-02-28 19:25:27 -05003427 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05003428
Jamie Wellnitz41415862006-02-28 19:25:27 -05003429 /* Reset HBA */
3430 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003431 "0325 Reset HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003432 phba->pport->port_state, psli->sli_flag);
dea31012005-04-17 16:05:31 -05003433
3434 /* perform board reset */
3435 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003436 phba->link_events = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003437 phba->pport->fc_myDID = 0;
3438 phba->pport->fc_prevDID = 0;
dea31012005-04-17 16:05:31 -05003439
Jamie Wellnitz41415862006-02-28 19:25:27 -05003440 /* Turn off parity checking and serr during the physical reset */
3441 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3442 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3443 (cfg_value &
3444 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3445
James Smart3772a992009-05-22 14:50:54 -04003446 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3447
Jamie Wellnitz41415862006-02-28 19:25:27 -05003448 /* Now toggle INITFF bit in the Host Control Register */
3449 writel(HC_INITFF, phba->HCregaddr);
3450 mdelay(1);
3451 readl(phba->HCregaddr); /* flush */
3452 writel(0, phba->HCregaddr);
3453 readl(phba->HCregaddr); /* flush */
3454
3455 /* Restore PCI cmd register */
3456 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea31012005-04-17 16:05:31 -05003457
3458 /* Initialize relevant SLI info */
Jamie Wellnitz41415862006-02-28 19:25:27 -05003459 for (i = 0; i < psli->num_rings; i++) {
3460 pring = &psli->ring[i];
dea31012005-04-17 16:05:31 -05003461 pring->flag = 0;
3462 pring->rspidx = 0;
3463 pring->next_cmdidx = 0;
3464 pring->local_getidx = 0;
3465 pring->cmdidx = 0;
3466 pring->missbufcnt = 0;
3467 }
dea31012005-04-17 16:05:31 -05003468
James Smart2e0fef82007-06-17 19:56:36 -05003469 phba->link_state = LPFC_WARM_START;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003470 return 0;
3471}
3472
James Smarte59058c2008-08-24 21:49:00 -04003473/**
James Smartda0436e2009-05-22 14:51:39 -04003474 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3475 * @phba: Pointer to HBA context object.
3476 *
3477 * This function resets a SLI4 HBA. This function disables PCI layer parity
3478 * checking during resets the device. The caller is not required to hold
3479 * any locks.
3480 *
3481 * This function returns 0 always.
3482 **/
3483int
3484lpfc_sli4_brdreset(struct lpfc_hba *phba)
3485{
3486 struct lpfc_sli *psli = &phba->sli;
3487 uint16_t cfg_value;
3488 uint8_t qindx;
3489
3490 /* Reset HBA */
3491 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3492 "0295 Reset HBA Data: x%x x%x\n",
3493 phba->pport->port_state, psli->sli_flag);
3494
3495 /* perform board reset */
3496 phba->fc_eventTag = 0;
James Smart4d9ab992009-10-02 15:16:39 -04003497 phba->link_events = 0;
James Smartda0436e2009-05-22 14:51:39 -04003498 phba->pport->fc_myDID = 0;
3499 phba->pport->fc_prevDID = 0;
3500
3501 /* Turn off parity checking and serr during the physical reset */
3502 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3503 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3504 (cfg_value &
3505 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3506
3507 spin_lock_irq(&phba->hbalock);
3508 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3509 phba->fcf.fcf_flag = 0;
3510 /* Clean up the child queue list for the CQs */
3511 list_del_init(&phba->sli4_hba.mbx_wq->list);
3512 list_del_init(&phba->sli4_hba.els_wq->list);
3513 list_del_init(&phba->sli4_hba.hdr_rq->list);
3514 list_del_init(&phba->sli4_hba.dat_rq->list);
3515 list_del_init(&phba->sli4_hba.mbx_cq->list);
3516 list_del_init(&phba->sli4_hba.els_cq->list);
James Smartda0436e2009-05-22 14:51:39 -04003517 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3518 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3519 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3520 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3521 spin_unlock_irq(&phba->hbalock);
3522
3523 /* Now physically reset the device */
3524 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3525 "0389 Performing PCI function reset!\n");
3526 /* Perform FCoE PCI function reset */
3527 lpfc_pci_function_reset(phba);
3528
3529 return 0;
3530}
3531
3532/**
3533 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
James Smarte59058c2008-08-24 21:49:00 -04003534 * @phba: Pointer to HBA context object.
3535 *
3536 * This function is called in the SLI initialization code path to
3537 * restart the HBA. The caller is not required to hold any lock.
3538 * This function writes MBX_RESTART mailbox command to the SLIM and
3539 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3540 * function to free any pending commands. The function enables
3541 * POST only during the first initialization. The function returns zero.
3542 * The function does not guarantee completion of MBX_RESTART mailbox
3543 * command before the return of this function.
3544 **/
James Smartda0436e2009-05-22 14:51:39 -04003545static int
3546lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003547{
3548 MAILBOX_t *mb;
3549 struct lpfc_sli *psli;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003550 volatile uint32_t word0;
3551 void __iomem *to_slim;
3552
James Smart2e0fef82007-06-17 19:56:36 -05003553 spin_lock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003554
3555 psli = &phba->sli;
3556
3557 /* Restart HBA */
3558 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04003559 "0337 Restart HBA Data: x%x x%x\n",
James Smart2e0fef82007-06-17 19:56:36 -05003560 phba->pport->port_state, psli->sli_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003561
3562 word0 = 0;
3563 mb = (MAILBOX_t *) &word0;
3564 mb->mbxCommand = MBX_RESTART;
3565 mb->mbxHc = 1;
3566
James Smart92908312006-03-07 15:04:13 -05003567 lpfc_reset_barrier(phba);
3568
Jamie Wellnitz41415862006-02-28 19:25:27 -05003569 to_slim = phba->MBslimaddr;
3570 writel(*(uint32_t *) mb, to_slim);
3571 readl(to_slim); /* flush */
3572
3573 /* Only skip post after fc_ffinit is completed */
James Smarteaf15d52008-12-04 22:39:29 -05003574 if (phba->pport->port_state)
Jamie Wellnitz41415862006-02-28 19:25:27 -05003575 word0 = 1; /* This is really setting up word1 */
James Smarteaf15d52008-12-04 22:39:29 -05003576 else
Jamie Wellnitz41415862006-02-28 19:25:27 -05003577 word0 = 0; /* This is really setting up word1 */
James Smart65a29c12006-07-06 15:50:50 -04003578 to_slim = phba->MBslimaddr + sizeof (uint32_t);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003579 writel(*(uint32_t *) mb, to_slim);
3580 readl(to_slim); /* flush */
3581
3582 lpfc_sli_brdreset(phba);
James Smart2e0fef82007-06-17 19:56:36 -05003583 phba->pport->stopped = 0;
3584 phba->link_state = LPFC_INIT_START;
James Smartda0436e2009-05-22 14:51:39 -04003585 phba->hba_flag = 0;
James Smart2e0fef82007-06-17 19:56:36 -05003586 spin_unlock_irq(&phba->hbalock);
Jamie Wellnitz41415862006-02-28 19:25:27 -05003587
James Smart64ba8812006-08-02 15:24:34 -04003588 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3589 psli->stats_start = get_seconds();
3590
James Smarteaf15d52008-12-04 22:39:29 -05003591 /* Give the INITFF and Post time to settle. */
3592 mdelay(100);
dea31012005-04-17 16:05:31 -05003593
Jamie Wellnitz41415862006-02-28 19:25:27 -05003594 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -05003595
3596 return 0;
3597}
3598
James Smarte59058c2008-08-24 21:49:00 -04003599/**
James Smartda0436e2009-05-22 14:51:39 -04003600 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3601 * @phba: Pointer to HBA context object.
3602 *
3603 * This function is called in the SLI initialization code path to restart
3604 * a SLI4 HBA. The caller is not required to hold any lock.
3605 * At the end of the function, it calls lpfc_hba_down_post function to
3606 * free any pending commands.
3607 **/
3608static int
3609lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3610{
3611 struct lpfc_sli *psli = &phba->sli;
3612
3613
3614 /* Restart HBA */
3615 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3616 "0296 Restart HBA Data: x%x x%x\n",
3617 phba->pport->port_state, psli->sli_flag);
3618
3619 lpfc_sli4_brdreset(phba);
3620
3621 spin_lock_irq(&phba->hbalock);
3622 phba->pport->stopped = 0;
3623 phba->link_state = LPFC_INIT_START;
3624 phba->hba_flag = 0;
3625 spin_unlock_irq(&phba->hbalock);
3626
3627 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3628 psli->stats_start = get_seconds();
3629
3630 lpfc_hba_down_post(phba);
3631
3632 return 0;
3633}
3634
3635/**
3636 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3637 * @phba: Pointer to HBA context object.
3638 *
3639 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3640 * API jump table function pointer from the lpfc_hba struct.
3641**/
3642int
3643lpfc_sli_brdrestart(struct lpfc_hba *phba)
3644{
3645 return phba->lpfc_sli_brdrestart(phba);
3646}
3647
3648/**
James Smart3621a712009-04-06 18:47:14 -04003649 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
James Smarte59058c2008-08-24 21:49:00 -04003650 * @phba: Pointer to HBA context object.
3651 *
3652 * This function is called after a HBA restart to wait for successful
3653 * restart of the HBA. Successful restart of the HBA is indicated by
3654 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3655 * iteration, the function will restart the HBA again. The function returns
3656 * zero if HBA successfully restarted else returns negative error code.
3657 **/
dea31012005-04-17 16:05:31 -05003658static int
3659lpfc_sli_chipset_init(struct lpfc_hba *phba)
3660{
3661 uint32_t status, i = 0;
3662
3663 /* Read the HBA Host Status Register */
3664 status = readl(phba->HSregaddr);
3665
3666 /* Check status register to see what current state is */
3667 i = 0;
3668 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3669
3670 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3671 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3672 * 4.
3673 */
3674 if (i++ >= 20) {
3675 /* Adapter failed to init, timeout, status reg
3676 <status> */
James Smarted957682007-06-17 19:56:37 -05003677 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003678 "0436 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003679 "timeout, status reg x%x, "
3680 "FW Data: A8 x%x AC x%x\n", status,
3681 readl(phba->MBslimaddr + 0xa8),
3682 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003683 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003684 return -ETIMEDOUT;
3685 }
3686
3687 /* Check to see if any errors occurred during init */
3688 if (status & HS_FFERM) {
3689 /* ERROR: During chipset initialization */
3690 /* Adapter failed to init, chipset, status reg
3691 <status> */
James Smarted957682007-06-17 19:56:37 -05003692 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003693 "0437 Adapter failed to init, "
James Smart09372822008-01-11 01:52:54 -05003694 "chipset, status reg x%x, "
3695 "FW Data: A8 x%x AC x%x\n", status,
3696 readl(phba->MBslimaddr + 0xa8),
3697 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003698 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003699 return -EIO;
3700 }
3701
3702 if (i <= 5) {
3703 msleep(10);
3704 } else if (i <= 10) {
3705 msleep(500);
3706 } else {
3707 msleep(2500);
3708 }
3709
3710 if (i == 15) {
James Smart2e0fef82007-06-17 19:56:36 -05003711 /* Do post */
James Smart92d7f7b2007-06-17 19:56:38 -05003712 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003713 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003714 }
3715 /* Read the HBA Host Status Register */
3716 status = readl(phba->HSregaddr);
3717 }
3718
3719 /* Check to see if any errors occurred during init */
3720 if (status & HS_FFERM) {
3721 /* ERROR: During chipset initialization */
3722 /* Adapter failed to init, chipset, status reg <status> */
James Smarted957682007-06-17 19:56:37 -05003723 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003724 "0438 Adapter failed to init, chipset, "
James Smart09372822008-01-11 01:52:54 -05003725 "status reg x%x, "
3726 "FW Data: A8 x%x AC x%x\n", status,
3727 readl(phba->MBslimaddr + 0xa8),
3728 readl(phba->MBslimaddr + 0xac));
James Smart2e0fef82007-06-17 19:56:36 -05003729 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003730 return -EIO;
3731 }
3732
3733 /* Clear all interrupt enable conditions */
3734 writel(0, phba->HCregaddr);
3735 readl(phba->HCregaddr); /* flush */
3736
3737 /* setup host attn register */
3738 writel(0xffffffff, phba->HAregaddr);
3739 readl(phba->HAregaddr); /* flush */
3740 return 0;
3741}
3742
James Smarte59058c2008-08-24 21:49:00 -04003743/**
James Smart3621a712009-04-06 18:47:14 -04003744 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
James Smarte59058c2008-08-24 21:49:00 -04003745 *
3746 * This function calculates and returns the number of HBQs required to be
3747 * configured.
3748 **/
James Smart78b2d852007-08-02 11:10:21 -04003749int
James Smarted957682007-06-17 19:56:37 -05003750lpfc_sli_hbq_count(void)
3751{
James Smart92d7f7b2007-06-17 19:56:38 -05003752 return ARRAY_SIZE(lpfc_hbq_defs);
James Smarted957682007-06-17 19:56:37 -05003753}
3754
James Smarte59058c2008-08-24 21:49:00 -04003755/**
James Smart3621a712009-04-06 18:47:14 -04003756 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003757 *
3758 * This function adds the number of hbq entries in every HBQ to get
3759 * the total number of hbq entries required for the HBA and returns
3760 * the total count.
3761 **/
James Smarted957682007-06-17 19:56:37 -05003762static int
3763lpfc_sli_hbq_entry_count(void)
3764{
3765 int hbq_count = lpfc_sli_hbq_count();
3766 int count = 0;
3767 int i;
3768
3769 for (i = 0; i < hbq_count; ++i)
James Smart92d7f7b2007-06-17 19:56:38 -05003770 count += lpfc_hbq_defs[i]->entry_count;
James Smarted957682007-06-17 19:56:37 -05003771 return count;
3772}
3773
James Smarte59058c2008-08-24 21:49:00 -04003774/**
James Smart3621a712009-04-06 18:47:14 -04003775 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
James Smarte59058c2008-08-24 21:49:00 -04003776 *
3777 * This function calculates amount of memory required for all hbq entries
3778 * to be configured and returns the total memory required.
3779 **/
dea31012005-04-17 16:05:31 -05003780int
James Smarted957682007-06-17 19:56:37 -05003781lpfc_sli_hbq_size(void)
3782{
3783 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3784}
3785
James Smarte59058c2008-08-24 21:49:00 -04003786/**
James Smart3621a712009-04-06 18:47:14 -04003787 * lpfc_sli_hbq_setup - configure and initialize HBQs
James Smarte59058c2008-08-24 21:49:00 -04003788 * @phba: Pointer to HBA context object.
3789 *
3790 * This function is called during the SLI initialization to configure
3791 * all the HBQs and post buffers to the HBQ. The caller is not
3792 * required to hold any locks. This function will return zero if successful
3793 * else it will return negative error code.
3794 **/
James Smarted957682007-06-17 19:56:37 -05003795static int
3796lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3797{
3798 int hbq_count = lpfc_sli_hbq_count();
3799 LPFC_MBOXQ_t *pmb;
3800 MAILBOX_t *pmbox;
3801 uint32_t hbqno;
3802 uint32_t hbq_entry_index;
James Smarted957682007-06-17 19:56:37 -05003803
James Smart92d7f7b2007-06-17 19:56:38 -05003804 /* Get a Mailbox buffer to setup mailbox
3805 * commands for HBA initialization
3806 */
James Smarted957682007-06-17 19:56:37 -05003807 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3808
3809 if (!pmb)
3810 return -ENOMEM;
3811
James Smart04c68492009-05-22 14:52:52 -04003812 pmbox = &pmb->u.mb;
James Smarted957682007-06-17 19:56:37 -05003813
3814 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3815 phba->link_state = LPFC_INIT_MBX_CMDS;
James Smart3163f722008-02-08 18:50:25 -05003816 phba->hbq_in_use = 1;
James Smarted957682007-06-17 19:56:37 -05003817
3818 hbq_entry_index = 0;
3819 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3820 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3821 phba->hbqs[hbqno].hbqPutIdx = 0;
3822 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3823 phba->hbqs[hbqno].entry_count =
James Smart92d7f7b2007-06-17 19:56:38 -05003824 lpfc_hbq_defs[hbqno]->entry_count;
James Smart51ef4c22007-08-02 11:10:31 -04003825 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3826 hbq_entry_index, pmb);
James Smarted957682007-06-17 19:56:37 -05003827 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3828
3829 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3830 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3831 mbxStatus <status>, ring <num> */
3832
3833 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05003834 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04003835 "1805 Adapter failed to init. "
James Smarted957682007-06-17 19:56:37 -05003836 "Data: x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04003837 pmbox->mbxCommand,
James Smarted957682007-06-17 19:56:37 -05003838 pmbox->mbxStatus, hbqno);
3839
3840 phba->link_state = LPFC_HBA_ERROR;
3841 mempool_free(pmb, phba->mbox_mem_pool);
James Smarted957682007-06-17 19:56:37 -05003842 return ENXIO;
3843 }
3844 }
3845 phba->hbq_count = hbq_count;
3846
James Smarted957682007-06-17 19:56:37 -05003847 mempool_free(pmb, phba->mbox_mem_pool);
3848
James Smart92d7f7b2007-06-17 19:56:38 -05003849 /* Initially populate or replenish the HBQs */
James Smartd7c255b2008-08-24 21:50:00 -04003850 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3851 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
James Smarted957682007-06-17 19:56:37 -05003852 return 0;
3853}
3854
James Smarte59058c2008-08-24 21:49:00 -04003855/**
James Smart4f774512009-05-22 14:52:35 -04003856 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3857 * @phba: Pointer to HBA context object.
3858 *
3859 * This function is called during the SLI initialization to configure
3860 * all the HBQs and post buffers to the HBQ. The caller is not
3861 * required to hold any locks. This function will return zero if successful
3862 * else it will return negative error code.
3863 **/
3864static int
3865lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3866{
3867 phba->hbq_in_use = 1;
3868 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3869 phba->hbq_count = 1;
3870 /* Initially populate or replenish the HBQs */
3871 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3872 return 0;
3873}
3874
3875/**
James Smart3621a712009-04-06 18:47:14 -04003876 * lpfc_sli_config_port - Issue config port mailbox command
James Smarte59058c2008-08-24 21:49:00 -04003877 * @phba: Pointer to HBA context object.
3878 * @sli_mode: sli mode - 2/3
3879 *
3880 * This function is called by the sli intialization code path
3881 * to issue config_port mailbox command. This function restarts the
3882 * HBA firmware and issues a config_port mailbox command to configure
3883 * the SLI interface in the sli mode specified by sli_mode
3884 * variable. The caller is not required to hold any locks.
3885 * The function returns 0 if successful, else returns negative error
3886 * code.
3887 **/
James Smart93996272008-08-24 21:50:30 -04003888int
3889lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea31012005-04-17 16:05:31 -05003890{
3891 LPFC_MBOXQ_t *pmb;
3892 uint32_t resetcount = 0, rc = 0, done = 0;
3893
3894 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3895 if (!pmb) {
James Smart2e0fef82007-06-17 19:56:36 -05003896 phba->link_state = LPFC_HBA_ERROR;
dea31012005-04-17 16:05:31 -05003897 return -ENOMEM;
3898 }
3899
James Smarted957682007-06-17 19:56:37 -05003900 phba->sli_rev = sli_mode;
dea31012005-04-17 16:05:31 -05003901 while (resetcount < 2 && !done) {
James Smart2e0fef82007-06-17 19:56:36 -05003902 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003903 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003904 spin_unlock_irq(&phba->hbalock);
James Smart92d7f7b2007-06-17 19:56:38 -05003905 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
Jamie Wellnitz41415862006-02-28 19:25:27 -05003906 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05003907 rc = lpfc_sli_chipset_init(phba);
3908 if (rc)
3909 break;
3910
James Smart2e0fef82007-06-17 19:56:36 -05003911 spin_lock_irq(&phba->hbalock);
James Smart1c067a42006-08-01 07:33:52 -04003912 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003913 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05003914 resetcount++;
3915
James Smarted957682007-06-17 19:56:37 -05003916 /* Call pre CONFIG_PORT mailbox command initialization. A
3917 * value of 0 means the call was successful. Any other
3918 * nonzero value is a failure, but if ERESTART is returned,
3919 * the driver may reset the HBA and try again.
3920 */
dea31012005-04-17 16:05:31 -05003921 rc = lpfc_config_port_prep(phba);
3922 if (rc == -ERESTART) {
James Smarted957682007-06-17 19:56:37 -05003923 phba->link_state = LPFC_LINK_UNKNOWN;
dea31012005-04-17 16:05:31 -05003924 continue;
James Smart34b02dc2008-08-24 21:49:55 -04003925 } else if (rc)
dea31012005-04-17 16:05:31 -05003926 break;
James Smart2e0fef82007-06-17 19:56:36 -05003927 phba->link_state = LPFC_INIT_MBX_CMDS;
dea31012005-04-17 16:05:31 -05003928 lpfc_config_port(phba, pmb);
3929 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
James Smart34b02dc2008-08-24 21:49:55 -04003930 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3931 LPFC_SLI3_HBQ_ENABLED |
3932 LPFC_SLI3_CRP_ENABLED |
James Smarte2a0a9d2008-12-04 22:40:02 -05003933 LPFC_SLI3_INB_ENABLED |
3934 LPFC_SLI3_BG_ENABLED);
James Smarted957682007-06-17 19:56:37 -05003935 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -05003936 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04003937 "0442 Adapter failed to init, mbxCmd x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05003938 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
James Smart04c68492009-05-22 14:52:52 -04003939 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
James Smart2e0fef82007-06-17 19:56:36 -05003940 spin_lock_irq(&phba->hbalock);
James Smart04c68492009-05-22 14:52:52 -04003941 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05003942 spin_unlock_irq(&phba->hbalock);
3943 rc = -ENXIO;
James Smart04c68492009-05-22 14:52:52 -04003944 } else {
3945 /* Allow asynchronous mailbox command to go through */
3946 spin_lock_irq(&phba->hbalock);
3947 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3948 spin_unlock_irq(&phba->hbalock);
James Smarted957682007-06-17 19:56:37 -05003949 done = 1;
James Smart04c68492009-05-22 14:52:52 -04003950 }
dea31012005-04-17 16:05:31 -05003951 }
James Smarted957682007-06-17 19:56:37 -05003952 if (!done) {
3953 rc = -EINVAL;
3954 goto do_prep_failed;
3955 }
James Smart04c68492009-05-22 14:52:52 -04003956 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3957 if (!pmb->u.mb.un.varCfgPort.cMA) {
James Smart34b02dc2008-08-24 21:49:55 -04003958 rc = -ENXIO;
3959 goto do_prep_failed;
3960 }
James Smart04c68492009-05-22 14:52:52 -04003961 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
James Smart34b02dc2008-08-24 21:49:55 -04003962 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003963 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3964 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3965 phba->max_vpi : phba->max_vports;
3966
James Smart34b02dc2008-08-24 21:49:55 -04003967 } else
3968 phba->max_vpi = 0;
James Smart04c68492009-05-22 14:52:52 -04003969 if (pmb->u.mb.un.varCfgPort.gdss)
3970 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3971 if (pmb->u.mb.un.varCfgPort.gerbm)
James Smart34b02dc2008-08-24 21:49:55 -04003972 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003973 if (pmb->u.mb.un.varCfgPort.gcrp)
James Smart34b02dc2008-08-24 21:49:55 -04003974 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
James Smart04c68492009-05-22 14:52:52 -04003975 if (pmb->u.mb.un.varCfgPort.ginb) {
James Smart34b02dc2008-08-24 21:49:55 -04003976 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
James Smart8f34f4c2008-12-04 22:39:23 -05003977 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003978 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3979 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3980 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3981 phba->inb_last_counter =
3982 phba->mbox->us.s3_inb_pgp.counter;
3983 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003984 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
James Smart34b02dc2008-08-24 21:49:55 -04003985 phba->port_gp = phba->mbox->us.s3_pgp.port;
3986 phba->inb_ha_copy = NULL;
3987 phba->inb_counter = NULL;
3988 }
James Smarte2a0a9d2008-12-04 22:40:02 -05003989
3990 if (phba->cfg_enable_bg) {
James Smart04c68492009-05-22 14:52:52 -04003991 if (pmb->u.mb.un.varCfgPort.gbg)
James Smarte2a0a9d2008-12-04 22:40:02 -05003992 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3993 else
3994 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3995 "0443 Adapter did not grant "
3996 "BlockGuard\n");
3997 }
James Smart34b02dc2008-08-24 21:49:55 -04003998 } else {
James Smart8f34f4c2008-12-04 22:39:23 -05003999 phba->hbq_get = NULL;
James Smart34b02dc2008-08-24 21:49:55 -04004000 phba->port_gp = phba->mbox->us.s2.port;
4001 phba->inb_ha_copy = NULL;
4002 phba->inb_counter = NULL;
James Smartd7c255b2008-08-24 21:50:00 -04004003 phba->max_vpi = 0;
James Smarted957682007-06-17 19:56:37 -05004004 }
James Smart92d7f7b2007-06-17 19:56:38 -05004005do_prep_failed:
James Smarted957682007-06-17 19:56:37 -05004006 mempool_free(pmb, phba->mbox_mem_pool);
4007 return rc;
4008}
4009
James Smarte59058c2008-08-24 21:49:00 -04004010
4011/**
James Smart3621a712009-04-06 18:47:14 -04004012 * lpfc_sli_hba_setup - SLI intialization function
James Smarte59058c2008-08-24 21:49:00 -04004013 * @phba: Pointer to HBA context object.
4014 *
4015 * This function is the main SLI intialization function. This function
4016 * is called by the HBA intialization code, HBA reset code and HBA
4017 * error attention handler code. Caller is not required to hold any
4018 * locks. This function issues config_port mailbox command to configure
4019 * the SLI, setup iocb rings and HBQ rings. In the end the function
4020 * calls the config_port_post function to issue init_link mailbox
4021 * command and to start the discovery. The function will return zero
4022 * if successful, else it will return negative error code.
4023 **/
James Smarted957682007-06-17 19:56:37 -05004024int
4025lpfc_sli_hba_setup(struct lpfc_hba *phba)
4026{
4027 uint32_t rc;
James Smart92d7f7b2007-06-17 19:56:38 -05004028 int mode = 3;
James Smarted957682007-06-17 19:56:37 -05004029
4030 switch (lpfc_sli_mode) {
4031 case 2:
James Smart78b2d852007-08-02 11:10:21 -04004032 if (phba->cfg_enable_npiv) {
James Smart92d7f7b2007-06-17 19:56:38 -05004033 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004034 "1824 NPIV enabled: Override lpfc_sli_mode "
James Smart92d7f7b2007-06-17 19:56:38 -05004035 "parameter (%d) to auto (0).\n",
James Smarte8b62012007-08-02 11:10:09 -04004036 lpfc_sli_mode);
James Smart92d7f7b2007-06-17 19:56:38 -05004037 break;
4038 }
James Smarted957682007-06-17 19:56:37 -05004039 mode = 2;
4040 break;
4041 case 0:
4042 case 3:
4043 break;
4044 default:
James Smart92d7f7b2007-06-17 19:56:38 -05004045 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004046 "1819 Unrecognized lpfc_sli_mode "
4047 "parameter: %d.\n", lpfc_sli_mode);
James Smarted957682007-06-17 19:56:37 -05004048
4049 break;
4050 }
4051
James Smart93996272008-08-24 21:50:30 -04004052 rc = lpfc_sli_config_port(phba, mode);
4053
James Smarted957682007-06-17 19:56:37 -05004054 if (rc && lpfc_sli_mode == 3)
James Smart92d7f7b2007-06-17 19:56:38 -05004055 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004056 "1820 Unable to select SLI-3. "
4057 "Not supported by adapter.\n");
James Smarted957682007-06-17 19:56:37 -05004058 if (rc && mode != 2)
James Smart93996272008-08-24 21:50:30 -04004059 rc = lpfc_sli_config_port(phba, 2);
James Smarted957682007-06-17 19:56:37 -05004060 if (rc)
dea31012005-04-17 16:05:31 -05004061 goto lpfc_sli_hba_setup_error;
4062
James Smarted957682007-06-17 19:56:37 -05004063 if (phba->sli_rev == 3) {
4064 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4065 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
James Smarted957682007-06-17 19:56:37 -05004066 } else {
4067 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4068 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
James Smart92d7f7b2007-06-17 19:56:38 -05004069 phba->sli3_options = 0;
James Smarted957682007-06-17 19:56:37 -05004070 }
4071
4072 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004073 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4074 phba->sli_rev, phba->max_vpi);
James Smarted957682007-06-17 19:56:37 -05004075 rc = lpfc_sli_ring_map(phba);
dea31012005-04-17 16:05:31 -05004076
4077 if (rc)
4078 goto lpfc_sli_hba_setup_error;
4079
James Smart93996272008-08-24 21:50:30 -04004080 /* Init HBQs */
James Smarted957682007-06-17 19:56:37 -05004081 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4082 rc = lpfc_sli_hbq_setup(phba);
4083 if (rc)
4084 goto lpfc_sli_hba_setup_error;
4085 }
James Smart04c68492009-05-22 14:52:52 -04004086 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004087 phba->sli.sli_flag |= LPFC_PROCESS_LA;
James Smart04c68492009-05-22 14:52:52 -04004088 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004089
4090 rc = lpfc_config_port_post(phba);
4091 if (rc)
4092 goto lpfc_sli_hba_setup_error;
4093
James Smarted957682007-06-17 19:56:37 -05004094 return rc;
4095
James Smart92d7f7b2007-06-17 19:56:38 -05004096lpfc_sli_hba_setup_error:
James Smart2e0fef82007-06-17 19:56:36 -05004097 phba->link_state = LPFC_HBA_ERROR;
James Smarted957682007-06-17 19:56:37 -05004098 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04004099 "0445 Firmware initialization failed\n");
dea31012005-04-17 16:05:31 -05004100 return rc;
4101}
4102
James Smartda0436e2009-05-22 14:51:39 -04004103/**
4104 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4105 * @phba: Pointer to HBA context object.
4106 * @mboxq: mailbox pointer.
4107 * This function issue a dump mailbox command to read config region
4108 * 23 and parse the records in the region and populate driver
4109 * data structure.
4110 **/
4111static int
4112lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4113 LPFC_MBOXQ_t *mboxq)
4114{
4115 struct lpfc_dmabuf *mp;
4116 struct lpfc_mqe *mqe;
4117 uint32_t data_length;
4118 int rc;
4119
4120 /* Program the default value of vlan_id and fc_map */
4121 phba->valid_vlan = 0;
4122 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4123 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4124 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4125
4126 mqe = &mboxq->u.mqe;
4127 if (lpfc_dump_fcoe_param(phba, mboxq))
4128 return -ENOMEM;
4129
4130 mp = (struct lpfc_dmabuf *) mboxq->context1;
4131 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4132
4133 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4134 "(%d):2571 Mailbox cmd x%x Status x%x "
4135 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4136 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4137 "CQ: x%x x%x x%x x%x\n",
4138 mboxq->vport ? mboxq->vport->vpi : 0,
4139 bf_get(lpfc_mqe_command, mqe),
4140 bf_get(lpfc_mqe_status, mqe),
4141 mqe->un.mb_words[0], mqe->un.mb_words[1],
4142 mqe->un.mb_words[2], mqe->un.mb_words[3],
4143 mqe->un.mb_words[4], mqe->un.mb_words[5],
4144 mqe->un.mb_words[6], mqe->un.mb_words[7],
4145 mqe->un.mb_words[8], mqe->un.mb_words[9],
4146 mqe->un.mb_words[10], mqe->un.mb_words[11],
4147 mqe->un.mb_words[12], mqe->un.mb_words[13],
4148 mqe->un.mb_words[14], mqe->un.mb_words[15],
4149 mqe->un.mb_words[16], mqe->un.mb_words[50],
4150 mboxq->mcqe.word0,
4151 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4152 mboxq->mcqe.trailer);
4153
4154 if (rc) {
4155 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4156 kfree(mp);
4157 return -EIO;
4158 }
4159 data_length = mqe->un.mb_words[5];
James Smarta0c87cb2009-07-19 10:01:10 -04004160 if (data_length > DMP_RGN23_SIZE) {
James Smartd11e31d2009-06-10 17:23:06 -04004161 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4162 kfree(mp);
James Smartda0436e2009-05-22 14:51:39 -04004163 return -EIO;
James Smartd11e31d2009-06-10 17:23:06 -04004164 }
James Smartda0436e2009-05-22 14:51:39 -04004165
4166 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4167 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4168 kfree(mp);
4169 return 0;
4170}
4171
4172/**
4173 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4174 * @phba: pointer to lpfc hba data structure.
4175 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4176 * @vpd: pointer to the memory to hold resulting port vpd data.
4177 * @vpd_size: On input, the number of bytes allocated to @vpd.
4178 * On output, the number of data bytes in @vpd.
4179 *
4180 * This routine executes a READ_REV SLI4 mailbox command. In
4181 * addition, this routine gets the port vpd data.
4182 *
4183 * Return codes
4184 * 0 - sucessful
4185 * ENOMEM - could not allocated memory.
4186 **/
4187static int
4188lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4189 uint8_t *vpd, uint32_t *vpd_size)
4190{
4191 int rc = 0;
4192 uint32_t dma_size;
4193 struct lpfc_dmabuf *dmabuf;
4194 struct lpfc_mqe *mqe;
4195
4196 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4197 if (!dmabuf)
4198 return -ENOMEM;
4199
4200 /*
4201 * Get a DMA buffer for the vpd data resulting from the READ_REV
4202 * mailbox command.
4203 */
4204 dma_size = *vpd_size;
4205 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4206 dma_size,
4207 &dmabuf->phys,
4208 GFP_KERNEL);
4209 if (!dmabuf->virt) {
4210 kfree(dmabuf);
4211 return -ENOMEM;
4212 }
4213 memset(dmabuf->virt, 0, dma_size);
4214
4215 /*
4216 * The SLI4 implementation of READ_REV conflicts at word1,
4217 * bits 31:16 and SLI4 adds vpd functionality not present
4218 * in SLI3. This code corrects the conflicts.
4219 */
4220 lpfc_read_rev(phba, mboxq);
4221 mqe = &mboxq->u.mqe;
4222 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4223 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4224 mqe->un.read_rev.word1 &= 0x0000FFFF;
4225 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4226 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4227
4228 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4229 if (rc) {
4230 dma_free_coherent(&phba->pcidev->dev, dma_size,
4231 dmabuf->virt, dmabuf->phys);
4232 return -EIO;
4233 }
4234
James Smartda0436e2009-05-22 14:51:39 -04004235 /*
4236 * The available vpd length cannot be bigger than the
4237 * DMA buffer passed to the port. Catch the less than
4238 * case and update the caller's size.
4239 */
4240 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4241 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4242
4243 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4244 dma_free_coherent(&phba->pcidev->dev, dma_size,
4245 dmabuf->virt, dmabuf->phys);
4246 kfree(dmabuf);
4247 return 0;
4248}
4249
4250/**
4251 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4252 * @phba: pointer to lpfc hba data structure.
4253 *
4254 * This routine is called to explicitly arm the SLI4 device's completion and
4255 * event queues
4256 **/
4257static void
4258lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4259{
4260 uint8_t fcp_eqidx;
4261
4262 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4263 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
James Smartda0436e2009-05-22 14:51:39 -04004264 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4265 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4266 LPFC_QUEUE_REARM);
4267 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4268 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4269 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4270 LPFC_QUEUE_REARM);
4271}
4272
4273/**
4274 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4275 * @phba: Pointer to HBA context object.
4276 *
4277 * This function is the main SLI4 device intialization PCI function. This
4278 * function is called by the HBA intialization code, HBA reset code and
4279 * HBA error attention handler code. Caller is not required to hold any
4280 * locks.
4281 **/
4282int
4283lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4284{
4285 int rc;
4286 LPFC_MBOXQ_t *mboxq;
4287 struct lpfc_mqe *mqe;
4288 uint8_t *vpd;
4289 uint32_t vpd_size;
4290 uint32_t ftr_rsp = 0;
4291 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4292 struct lpfc_vport *vport = phba->pport;
4293 struct lpfc_dmabuf *mp;
4294
4295 /* Perform a PCI function reset to start from clean */
4296 rc = lpfc_pci_function_reset(phba);
4297 if (unlikely(rc))
4298 return -ENODEV;
4299
4300 /* Check the HBA Host Status Register for readyness */
4301 rc = lpfc_sli4_post_status_check(phba);
4302 if (unlikely(rc))
4303 return -ENODEV;
4304 else {
4305 spin_lock_irq(&phba->hbalock);
4306 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4307 spin_unlock_irq(&phba->hbalock);
4308 }
4309
4310 /*
4311 * Allocate a single mailbox container for initializing the
4312 * port.
4313 */
4314 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4315 if (!mboxq)
4316 return -ENOMEM;
4317
4318 /*
4319 * Continue initialization with default values even if driver failed
4320 * to read FCoE param config regions
4321 */
4322 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
James Smarte4e74272009-07-19 10:01:38 -04004324 "2570 Failed to read FCoE parameters\n");
James Smartda0436e2009-05-22 14:51:39 -04004325
4326 /* Issue READ_REV to collect vpd and FW information. */
4327 vpd_size = PAGE_SIZE;
4328 vpd = kzalloc(vpd_size, GFP_KERNEL);
4329 if (!vpd) {
4330 rc = -ENOMEM;
4331 goto out_free_mbox;
4332 }
4333
4334 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4335 if (unlikely(rc))
4336 goto out_free_vpd;
4337
4338 mqe = &mboxq->u.mqe;
James Smartf1126682009-06-10 17:22:44 -04004339 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4340 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4341 phba->hba_flag |= HBA_FCOE_SUPPORT;
4342 if (phba->sli_rev != LPFC_SLI_REV4 ||
4343 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
James Smartda0436e2009-05-22 14:51:39 -04004344 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4345 "0376 READ_REV Error. SLI Level %d "
4346 "FCoE enabled %d\n",
James Smartf1126682009-06-10 17:22:44 -04004347 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
James Smartda0436e2009-05-22 14:51:39 -04004348 rc = -EIO;
4349 goto out_free_vpd;
4350 }
James Smartda0436e2009-05-22 14:51:39 -04004351 /*
4352 * Evaluate the read rev and vpd data. Populate the driver
4353 * state with the results. If this routine fails, the failure
4354 * is not fatal as the driver will use generic values.
4355 */
4356 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4357 if (unlikely(!rc)) {
4358 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4359 "0377 Error %d parsing vpd. "
4360 "Using defaults.\n", rc);
4361 rc = 0;
4362 }
4363
James Smartf1126682009-06-10 17:22:44 -04004364 /* Save information as VPD data */
4365 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4366 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4367 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4368 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4369 &mqe->un.read_rev);
4370 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4371 &mqe->un.read_rev);
4372 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4373 &mqe->un.read_rev);
4374 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4375 &mqe->un.read_rev);
4376 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4377 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4378 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4379 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4380 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4381 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4382 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4383 "(%d):0380 READ_REV Status x%x "
4384 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4385 mboxq->vport ? mboxq->vport->vpi : 0,
4386 bf_get(lpfc_mqe_status, mqe),
4387 phba->vpd.rev.opFwName,
4388 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4389 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
James Smartda0436e2009-05-22 14:51:39 -04004390
4391 /*
4392 * Discover the port's supported feature set and match it against the
4393 * hosts requests.
4394 */
4395 lpfc_request_features(phba, mboxq);
4396 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4397 if (unlikely(rc)) {
4398 rc = -EIO;
4399 goto out_free_vpd;
4400 }
4401
4402 /*
4403 * The port must support FCP initiator mode as this is the
4404 * only mode running in the host.
4405 */
4406 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4407 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4408 "0378 No support for fcpi mode.\n");
4409 ftr_rsp++;
4410 }
4411
4412 /*
4413 * If the port cannot support the host's requested features
4414 * then turn off the global config parameters to disable the
4415 * feature in the driver. This is not a fatal error.
4416 */
4417 if ((phba->cfg_enable_bg) &&
4418 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4419 ftr_rsp++;
4420
4421 if (phba->max_vpi && phba->cfg_enable_npiv &&
4422 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4423 ftr_rsp++;
4424
4425 if (ftr_rsp) {
4426 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4427 "0379 Feature Mismatch Data: x%08x %08x "
4428 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4429 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4430 phba->cfg_enable_npiv, phba->max_vpi);
4431 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4432 phba->cfg_enable_bg = 0;
4433 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4434 phba->cfg_enable_npiv = 0;
4435 }
4436
4437 /* These SLI3 features are assumed in SLI4 */
4438 spin_lock_irq(&phba->hbalock);
4439 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4440 spin_unlock_irq(&phba->hbalock);
4441
4442 /* Read the port's service parameters. */
4443 lpfc_read_sparam(phba, mboxq, vport->vpi);
4444 mboxq->vport = vport;
4445 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4446 mp = (struct lpfc_dmabuf *) mboxq->context1;
4447 if (rc == MBX_SUCCESS) {
4448 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4449 rc = 0;
4450 }
4451
4452 /*
4453 * This memory was allocated by the lpfc_read_sparam routine. Release
4454 * it to the mbuf pool.
4455 */
4456 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4457 kfree(mp);
4458 mboxq->context1 = NULL;
4459 if (unlikely(rc)) {
4460 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4461 "0382 READ_SPARAM command failed "
4462 "status %d, mbxStatus x%x\n",
4463 rc, bf_get(lpfc_mqe_status, mqe));
4464 phba->link_state = LPFC_HBA_ERROR;
4465 rc = -EIO;
4466 goto out_free_vpd;
4467 }
4468
4469 if (phba->cfg_soft_wwnn)
4470 u64_to_wwn(phba->cfg_soft_wwnn,
4471 vport->fc_sparam.nodeName.u.wwn);
4472 if (phba->cfg_soft_wwpn)
4473 u64_to_wwn(phba->cfg_soft_wwpn,
4474 vport->fc_sparam.portName.u.wwn);
4475 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4476 sizeof(struct lpfc_name));
4477 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4478 sizeof(struct lpfc_name));
4479
4480 /* Update the fc_host data structures with new wwn. */
4481 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4482 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4483
4484 /* Register SGL pool to the device using non-embedded mailbox command */
4485 rc = lpfc_sli4_post_sgl_list(phba);
4486 if (unlikely(rc)) {
4487 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4488 "0582 Error %d during sgl post operation", rc);
4489 rc = -ENODEV;
4490 goto out_free_vpd;
4491 }
4492
4493 /* Register SCSI SGL pool to the device */
4494 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4495 if (unlikely(rc)) {
4496 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4497 "0383 Error %d during scsi sgl post opeation",
4498 rc);
4499 /* Some Scsi buffers were moved to the abort scsi list */
4500 /* A pci function reset will repost them */
4501 rc = -ENODEV;
4502 goto out_free_vpd;
4503 }
4504
4505 /* Post the rpi header region to the device. */
4506 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4507 if (unlikely(rc)) {
4508 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4509 "0393 Error %d during rpi post operation\n",
4510 rc);
4511 rc = -ENODEV;
4512 goto out_free_vpd;
4513 }
James Smart0c287582009-06-10 17:22:56 -04004514 if (phba->cfg_enable_fip)
4515 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
4516 else
4517 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
James Smartda0436e2009-05-22 14:51:39 -04004518
4519 /* Set up all the queues to the device */
4520 rc = lpfc_sli4_queue_setup(phba);
4521 if (unlikely(rc)) {
4522 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4523 "0381 Error %d during queue setup.\n ", rc);
4524 goto out_stop_timers;
4525 }
4526
4527 /* Arm the CQs and then EQs on device */
4528 lpfc_sli4_arm_cqeq_intr(phba);
4529
4530 /* Indicate device interrupt mode */
4531 phba->sli4_hba.intr_enable = 1;
4532
4533 /* Allow asynchronous mailbox command to go through */
4534 spin_lock_irq(&phba->hbalock);
4535 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4536 spin_unlock_irq(&phba->hbalock);
4537
4538 /* Post receive buffers to the device */
4539 lpfc_sli4_rb_setup(phba);
4540
4541 /* Start the ELS watchdog timer */
James Smart8fa38512009-07-19 10:01:03 -04004542 mod_timer(&vport->els_tmofunc,
4543 jiffies + HZ * (phba->fc_ratov * 2));
James Smartda0436e2009-05-22 14:51:39 -04004544
4545 /* Start heart beat timer */
4546 mod_timer(&phba->hb_tmofunc,
4547 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4548 phba->hb_outstanding = 0;
4549 phba->last_completion_time = jiffies;
4550
4551 /* Start error attention (ERATT) polling timer */
4552 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4553
4554 /*
4555 * The port is ready, set the host's link state to LINK_DOWN
4556 * in preparation for link interrupts.
4557 */
4558 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4559 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4560 lpfc_set_loopback_flag(phba);
4561 /* Change driver state to LPFC_LINK_DOWN right before init link */
4562 spin_lock_irq(&phba->hbalock);
4563 phba->link_state = LPFC_LINK_DOWN;
4564 spin_unlock_irq(&phba->hbalock);
4565 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4566 if (unlikely(rc != MBX_NOT_FINISHED)) {
4567 kfree(vpd);
4568 return 0;
4569 } else
4570 rc = -EIO;
4571
4572 /* Unset all the queues set up in this routine when error out */
4573 if (rc)
4574 lpfc_sli4_queue_unset(phba);
4575
4576out_stop_timers:
4577 if (rc)
4578 lpfc_stop_hba_timers(phba);
4579out_free_vpd:
4580 kfree(vpd);
4581out_free_mbox:
4582 mempool_free(mboxq, phba->mbox_mem_pool);
4583 return rc;
4584}
James Smarte59058c2008-08-24 21:49:00 -04004585
4586/**
James Smart3621a712009-04-06 18:47:14 -04004587 * lpfc_mbox_timeout - Timeout call back function for mbox timer
James Smarte59058c2008-08-24 21:49:00 -04004588 * @ptr: context object - pointer to hba structure.
dea31012005-04-17 16:05:31 -05004589 *
James Smarte59058c2008-08-24 21:49:00 -04004590 * This is the callback function for mailbox timer. The mailbox
4591 * timer is armed when a new mailbox command is issued and the timer
4592 * is deleted when the mailbox complete. The function is called by
4593 * the kernel timer code when a mailbox does not complete within
4594 * expected time. This function wakes up the worker thread to
4595 * process the mailbox timeout and returns. All the processing is
4596 * done by the worker thread function lpfc_mbox_timeout_handler.
4597 **/
dea31012005-04-17 16:05:31 -05004598void
4599lpfc_mbox_timeout(unsigned long ptr)
4600{
James Smart92d7f7b2007-06-17 19:56:38 -05004601 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
dea31012005-04-17 16:05:31 -05004602 unsigned long iflag;
James Smart2e0fef82007-06-17 19:56:36 -05004603 uint32_t tmo_posted;
dea31012005-04-17 16:05:31 -05004604
James Smart2e0fef82007-06-17 19:56:36 -05004605 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05004606 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
James Smart2e0fef82007-06-17 19:56:36 -05004607 if (!tmo_posted)
4608 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4609 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4610
James Smart5e9d9b82008-06-14 22:52:53 -04004611 if (!tmo_posted)
4612 lpfc_worker_wake_up(phba);
4613 return;
dea31012005-04-17 16:05:31 -05004614}
4615
James Smarte59058c2008-08-24 21:49:00 -04004616
4617/**
James Smart3621a712009-04-06 18:47:14 -04004618 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
James Smarte59058c2008-08-24 21:49:00 -04004619 * @phba: Pointer to HBA context object.
4620 *
4621 * This function is called from worker thread when a mailbox command times out.
4622 * The caller is not required to hold any locks. This function will reset the
4623 * HBA and recover all the pending commands.
4624 **/
dea31012005-04-17 16:05:31 -05004625void
4626lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4627{
James Smart2e0fef82007-06-17 19:56:36 -05004628 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04004629 MAILBOX_t *mb = &pmbox->u.mb;
James Smart1dcb58e2007-04-25 09:51:30 -04004630 struct lpfc_sli *psli = &phba->sli;
4631 struct lpfc_sli_ring *pring;
dea31012005-04-17 16:05:31 -05004632
James Smarta257bf92009-04-06 18:48:10 -04004633 /* Check the pmbox pointer first. There is a race condition
4634 * between the mbox timeout handler getting executed in the
4635 * worklist and the mailbox actually completing. When this
4636 * race condition occurs, the mbox_active will be NULL.
4637 */
4638 spin_lock_irq(&phba->hbalock);
4639 if (pmbox == NULL) {
4640 lpfc_printf_log(phba, KERN_WARNING,
4641 LOG_MBOX | LOG_SLI,
4642 "0353 Active Mailbox cleared - mailbox timeout "
4643 "exiting\n");
4644 spin_unlock_irq(&phba->hbalock);
4645 return;
4646 }
4647
dea31012005-04-17 16:05:31 -05004648 /* Mbox cmd <mbxCommand> timeout */
James Smarted957682007-06-17 19:56:37 -05004649 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004650 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004651 mb->mbxCommand,
4652 phba->pport->port_state,
4653 phba->sli.sli_flag,
4654 phba->sli.mbox_active);
James Smarta257bf92009-04-06 18:48:10 -04004655 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05004656
James Smart1dcb58e2007-04-25 09:51:30 -04004657 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4658 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4659 * it to fail all oustanding SCSI IO.
4660 */
James Smart2e0fef82007-06-17 19:56:36 -05004661 spin_lock_irq(&phba->pport->work_port_lock);
4662 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4663 spin_unlock_irq(&phba->pport->work_port_lock);
4664 spin_lock_irq(&phba->hbalock);
4665 phba->link_state = LPFC_LINK_UNKNOWN;
James Smartf4b4c682009-05-22 14:53:12 -04004666 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004667 spin_unlock_irq(&phba->hbalock);
James Smart1dcb58e2007-04-25 09:51:30 -04004668
4669 pring = &psli->ring[psli->fcp_ring];
4670 lpfc_sli_abort_iocb_ring(phba, pring);
4671
4672 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04004673 "0345 Resetting board due to mailbox timeout\n");
James Smart3772a992009-05-22 14:50:54 -04004674
4675 /* Reset the HBA device */
4676 lpfc_reset_hba(phba);
dea31012005-04-17 16:05:31 -05004677}
4678
James Smarte59058c2008-08-24 21:49:00 -04004679/**
James Smart3772a992009-05-22 14:50:54 -04004680 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
James Smarte59058c2008-08-24 21:49:00 -04004681 * @phba: Pointer to HBA context object.
4682 * @pmbox: Pointer to mailbox object.
4683 * @flag: Flag indicating how the mailbox need to be processed.
4684 *
4685 * This function is called by discovery code and HBA management code
James Smart3772a992009-05-22 14:50:54 -04004686 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4687 * function gets the hbalock to protect the data structures.
James Smarte59058c2008-08-24 21:49:00 -04004688 * The mailbox command can be submitted in polling mode, in which case
4689 * this function will wait in a polling loop for the completion of the
4690 * mailbox.
4691 * If the mailbox is submitted in no_wait mode (not polling) the
4692 * function will submit the command and returns immediately without waiting
4693 * for the mailbox completion. The no_wait is supported only when HBA
4694 * is in SLI2/SLI3 mode - interrupts are enabled.
4695 * The SLI interface allows only one mailbox pending at a time. If the
4696 * mailbox is issued in polling mode and there is already a mailbox
4697 * pending, then the function will return an error. If the mailbox is issued
4698 * in NO_WAIT mode and there is a mailbox pending already, the function
4699 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4700 * The sli layer owns the mailbox object until the completion of mailbox
4701 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4702 * return codes the caller owns the mailbox command after the return of
4703 * the function.
4704 **/
James Smart3772a992009-05-22 14:50:54 -04004705static int
4706lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4707 uint32_t flag)
dea31012005-04-17 16:05:31 -05004708{
dea31012005-04-17 16:05:31 -05004709 MAILBOX_t *mb;
James Smart2e0fef82007-06-17 19:56:36 -05004710 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05004711 uint32_t status, evtctr;
4712 uint32_t ha_copy;
4713 int i;
James Smart09372822008-01-11 01:52:54 -05004714 unsigned long timeout;
dea31012005-04-17 16:05:31 -05004715 unsigned long drvr_flag = 0;
James Smart34b02dc2008-08-24 21:49:55 -04004716 uint32_t word0, ldata;
dea31012005-04-17 16:05:31 -05004717 void __iomem *to_slim;
James Smart58da1ff2008-04-07 10:15:56 -04004718 int processing_queue = 0;
4719
4720 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4721 if (!pmbox) {
James Smart8568a4d2009-07-19 10:01:16 -04004722 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart58da1ff2008-04-07 10:15:56 -04004723 /* processing mbox queue from intr_handler */
James Smart3772a992009-05-22 14:50:54 -04004724 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4725 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4726 return MBX_SUCCESS;
4727 }
James Smart58da1ff2008-04-07 10:15:56 -04004728 processing_queue = 1;
James Smart58da1ff2008-04-07 10:15:56 -04004729 pmbox = lpfc_mbox_get(phba);
4730 if (!pmbox) {
4731 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4732 return MBX_SUCCESS;
4733 }
4734 }
dea31012005-04-17 16:05:31 -05004735
James Smarted957682007-06-17 19:56:37 -05004736 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
James Smart92d7f7b2007-06-17 19:56:38 -05004737 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
James Smarted957682007-06-17 19:56:37 -05004738 if(!pmbox->vport) {
James Smart58da1ff2008-04-07 10:15:56 -04004739 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smarted957682007-06-17 19:56:37 -05004740 lpfc_printf_log(phba, KERN_ERR,
James Smart92d7f7b2007-06-17 19:56:38 -05004741 LOG_MBOX | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04004742 "1806 Mbox x%x failed. No vport\n",
James Smart3772a992009-05-22 14:50:54 -04004743 pmbox->u.mb.mbxCommand);
James Smarted957682007-06-17 19:56:37 -05004744 dump_stack();
James Smart58da1ff2008-04-07 10:15:56 -04004745 goto out_not_finished;
James Smarted957682007-06-17 19:56:37 -05004746 }
4747 }
4748
Linas Vepstas8d63f372007-02-14 14:28:36 -06004749 /* If the PCI channel is in offline state, do not post mbox. */
James Smart58da1ff2008-04-07 10:15:56 -04004750 if (unlikely(pci_channel_offline(phba->pcidev))) {
4751 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4752 goto out_not_finished;
4753 }
Linas Vepstas8d63f372007-02-14 14:28:36 -06004754
James Smarta257bf92009-04-06 18:48:10 -04004755 /* If HBA has a deferred error attention, fail the iocb. */
4756 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4757 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4758 goto out_not_finished;
4759 }
4760
dea31012005-04-17 16:05:31 -05004761 psli = &phba->sli;
James Smart92d7f7b2007-06-17 19:56:38 -05004762
James Smart3772a992009-05-22 14:50:54 -04004763 mb = &pmbox->u.mb;
dea31012005-04-17 16:05:31 -05004764 status = MBX_SUCCESS;
4765
James Smart2e0fef82007-06-17 19:56:36 -05004766 if (phba->link_state == LPFC_HBA_ERROR) {
4767 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
Jamie Wellnitz41415862006-02-28 19:25:27 -05004768
4769 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004770 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4771 "(%d):0311 Mailbox command x%x cannot "
4772 "issue Data: x%x x%x\n",
4773 pmbox->vport ? pmbox->vport->vpi : 0,
4774 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004775 goto out_not_finished;
Jamie Wellnitz41415862006-02-28 19:25:27 -05004776 }
4777
James Smart92908312006-03-07 15:04:13 -05004778 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4779 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
James Smart2e0fef82007-06-17 19:56:36 -05004780 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
James Smart3772a992009-05-22 14:50:54 -04004781 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4782 "(%d):2528 Mailbox command x%x cannot "
4783 "issue Data: x%x x%x\n",
4784 pmbox->vport ? pmbox->vport->vpi : 0,
4785 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004786 goto out_not_finished;
James Smart92908312006-03-07 15:04:13 -05004787 }
4788
dea31012005-04-17 16:05:31 -05004789 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4790 /* Polling for a mbox command when another one is already active
4791 * is not allowed in SLI. Also, the driver must have established
4792 * SLI2 mode to queue and process multiple mbox commands.
4793 */
4794
4795 if (flag & MBX_POLL) {
James Smart2e0fef82007-06-17 19:56:36 -05004796 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004797
4798 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004799 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4800 "(%d):2529 Mailbox command x%x "
4801 "cannot issue Data: x%x x%x\n",
4802 pmbox->vport ? pmbox->vport->vpi : 0,
4803 pmbox->u.mb.mbxCommand,
4804 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004805 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004806 }
4807
James Smart3772a992009-05-22 14:50:54 -04004808 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
James Smart2e0fef82007-06-17 19:56:36 -05004809 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004810 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004811 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4812 "(%d):2530 Mailbox command x%x "
4813 "cannot issue Data: x%x x%x\n",
4814 pmbox->vport ? pmbox->vport->vpi : 0,
4815 pmbox->u.mb.mbxCommand,
4816 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004817 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004818 }
4819
dea31012005-04-17 16:05:31 -05004820 /* Another mailbox command is still being processed, queue this
4821 * command to be processed later.
4822 */
4823 lpfc_mbox_put(phba, pmbox);
4824
4825 /* Mbox cmd issue - BUSY */
James Smarted957682007-06-17 19:56:37 -05004826 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004827 "(%d):0308 Mbox cmd issue - BUSY Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05004828 "x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05004829 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4830 mb->mbxCommand, phba->pport->port_state,
4831 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004832
4833 psli->slistat.mbox_busy++;
James Smart2e0fef82007-06-17 19:56:36 -05004834 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004835
James Smart858c9f62007-06-17 19:56:39 -05004836 if (pmbox->vport) {
4837 lpfc_debugfs_disc_trc(pmbox->vport,
4838 LPFC_DISC_TRC_MBOX_VPORT,
4839 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4840 (uint32_t)mb->mbxCommand,
4841 mb->un.varWords[0], mb->un.varWords[1]);
4842 }
4843 else {
4844 lpfc_debugfs_disc_trc(phba->pport,
4845 LPFC_DISC_TRC_MBOX,
4846 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4847 (uint32_t)mb->mbxCommand,
4848 mb->un.varWords[0], mb->un.varWords[1]);
4849 }
4850
James Smart2e0fef82007-06-17 19:56:36 -05004851 return MBX_BUSY;
dea31012005-04-17 16:05:31 -05004852 }
4853
dea31012005-04-17 16:05:31 -05004854 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4855
4856 /* If we are not polling, we MUST be in SLI2 mode */
4857 if (flag != MBX_POLL) {
James Smart3772a992009-05-22 14:50:54 -04004858 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
Jamie Wellnitz41415862006-02-28 19:25:27 -05004859 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea31012005-04-17 16:05:31 -05004860 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004861 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea31012005-04-17 16:05:31 -05004862 /* Mbox command <mbxCommand> cannot issue */
James Smart3772a992009-05-22 14:50:54 -04004863 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4864 "(%d):2531 Mailbox command x%x "
4865 "cannot issue Data: x%x x%x\n",
4866 pmbox->vport ? pmbox->vport->vpi : 0,
4867 pmbox->u.mb.mbxCommand,
4868 psli->sli_flag, flag);
James Smart58da1ff2008-04-07 10:15:56 -04004869 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004870 }
4871 /* timeout active mbox command */
James Smarta309a6b2006-08-01 07:33:43 -04004872 mod_timer(&psli->mbox_tmo, (jiffies +
4873 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea31012005-04-17 16:05:31 -05004874 }
4875
4876 /* Mailbox cmd <cmd> issue */
James Smarted957682007-06-17 19:56:37 -05004877 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04004878 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
James Smart92d7f7b2007-06-17 19:56:38 -05004879 "x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04004880 pmbox->vport ? pmbox->vport->vpi : 0,
James Smart92d7f7b2007-06-17 19:56:38 -05004881 mb->mbxCommand, phba->pport->port_state,
4882 psli->sli_flag, flag);
dea31012005-04-17 16:05:31 -05004883
James Smart858c9f62007-06-17 19:56:39 -05004884 if (mb->mbxCommand != MBX_HEARTBEAT) {
4885 if (pmbox->vport) {
4886 lpfc_debugfs_disc_trc(pmbox->vport,
4887 LPFC_DISC_TRC_MBOX_VPORT,
4888 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4889 (uint32_t)mb->mbxCommand,
4890 mb->un.varWords[0], mb->un.varWords[1]);
4891 }
4892 else {
4893 lpfc_debugfs_disc_trc(phba->pport,
4894 LPFC_DISC_TRC_MBOX,
4895 "MBOX Send: cmd:x%x mb:x%x x%x",
4896 (uint32_t)mb->mbxCommand,
4897 mb->un.varWords[0], mb->un.varWords[1]);
4898 }
4899 }
4900
dea31012005-04-17 16:05:31 -05004901 psli->slistat.mbox_cmd++;
4902 evtctr = psli->slistat.mbox_event;
4903
4904 /* next set own bit for the adapter and copy over command word */
4905 mb->mbxOwner = OWN_CHIP;
4906
James Smart3772a992009-05-22 14:50:54 -04004907 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004908 /* First copy command data to host SLIM area */
James Smart34b02dc2008-08-24 21:49:55 -04004909 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004910 } else {
James Smart92908312006-03-07 15:04:13 -05004911 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea31012005-04-17 16:05:31 -05004912 /* copy command data into host mbox for cmpl */
James Smart34b02dc2008-08-24 21:49:55 -04004913 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05004914 }
4915
4916 /* First copy mbox command data to HBA SLIM, skip past first
4917 word */
4918 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4919 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4920 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4921
4922 /* Next copy over first word, with mbxOwner set */
James Smart34b02dc2008-08-24 21:49:55 -04004923 ldata = *((uint32_t *)mb);
dea31012005-04-17 16:05:31 -05004924 to_slim = phba->MBslimaddr;
4925 writel(ldata, to_slim);
4926 readl(to_slim); /* flush */
4927
4928 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4929 /* switch over to host mailbox */
James Smart3772a992009-05-22 14:50:54 -04004930 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05004931 }
4932 }
4933
4934 wmb();
dea31012005-04-17 16:05:31 -05004935
4936 switch (flag) {
4937 case MBX_NOWAIT:
James Smart09372822008-01-11 01:52:54 -05004938 /* Set up reference to mailbox command */
dea31012005-04-17 16:05:31 -05004939 psli->mbox_active = pmbox;
James Smart09372822008-01-11 01:52:54 -05004940 /* Interrupt board to do it */
4941 writel(CA_MBATT, phba->CAregaddr);
4942 readl(phba->CAregaddr); /* flush */
4943 /* Don't wait for it to finish, just return */
dea31012005-04-17 16:05:31 -05004944 break;
4945
4946 case MBX_POLL:
James Smart09372822008-01-11 01:52:54 -05004947 /* Set up null reference to mailbox command */
dea31012005-04-17 16:05:31 -05004948 psli->mbox_active = NULL;
James Smart09372822008-01-11 01:52:54 -05004949 /* Interrupt board to do it */
4950 writel(CA_MBATT, phba->CAregaddr);
4951 readl(phba->CAregaddr); /* flush */
4952
James Smart3772a992009-05-22 14:50:54 -04004953 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004954 /* First read mbox status word */
James Smart34b02dc2008-08-24 21:49:55 -04004955 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004956 word0 = le32_to_cpu(word0);
4957 } else {
4958 /* First read mbox status word */
4959 word0 = readl(phba->MBslimaddr);
4960 }
4961
4962 /* Read the HBA Host Attention Register */
4963 ha_copy = readl(phba->HAregaddr);
James Smart09372822008-01-11 01:52:54 -05004964 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4965 mb->mbxCommand) *
4966 1000) + jiffies;
4967 i = 0;
dea31012005-04-17 16:05:31 -05004968 /* Wait for command to complete */
Jamie Wellnitz41415862006-02-28 19:25:27 -05004969 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4970 (!(ha_copy & HA_MBATT) &&
James Smart2e0fef82007-06-17 19:56:36 -05004971 (phba->link_state > LPFC_WARM_START))) {
James Smart09372822008-01-11 01:52:54 -05004972 if (time_after(jiffies, timeout)) {
dea31012005-04-17 16:05:31 -05004973 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
James Smart2e0fef82007-06-17 19:56:36 -05004974 spin_unlock_irqrestore(&phba->hbalock,
dea31012005-04-17 16:05:31 -05004975 drvr_flag);
James Smart58da1ff2008-04-07 10:15:56 -04004976 goto out_not_finished;
dea31012005-04-17 16:05:31 -05004977 }
4978
4979 /* Check if we took a mbox interrupt while we were
4980 polling */
4981 if (((word0 & OWN_CHIP) != OWN_CHIP)
4982 && (evtctr != psli->slistat.mbox_event))
4983 break;
4984
James Smart09372822008-01-11 01:52:54 -05004985 if (i++ > 10) {
4986 spin_unlock_irqrestore(&phba->hbalock,
4987 drvr_flag);
4988 msleep(1);
4989 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4990 }
dea31012005-04-17 16:05:31 -05004991
James Smart3772a992009-05-22 14:50:54 -04004992 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05004993 /* First copy command data */
James Smart34b02dc2008-08-24 21:49:55 -04004994 word0 = *((uint32_t *)phba->mbox);
dea31012005-04-17 16:05:31 -05004995 word0 = le32_to_cpu(word0);
4996 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4997 MAILBOX_t *slimmb;
James Smart34b02dc2008-08-24 21:49:55 -04004998 uint32_t slimword0;
dea31012005-04-17 16:05:31 -05004999 /* Check real SLIM for any errors */
5000 slimword0 = readl(phba->MBslimaddr);
5001 slimmb = (MAILBOX_t *) & slimword0;
5002 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5003 && slimmb->mbxStatus) {
5004 psli->sli_flag &=
James Smart3772a992009-05-22 14:50:54 -04005005 ~LPFC_SLI_ACTIVE;
dea31012005-04-17 16:05:31 -05005006 word0 = slimword0;
5007 }
5008 }
5009 } else {
5010 /* First copy command data */
5011 word0 = readl(phba->MBslimaddr);
5012 }
5013 /* Read the HBA Host Attention Register */
5014 ha_copy = readl(phba->HAregaddr);
5015 }
5016
James Smart3772a992009-05-22 14:50:54 -04005017 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea31012005-04-17 16:05:31 -05005018 /* copy results back to user */
James Smart34b02dc2008-08-24 21:49:55 -04005019 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea31012005-04-17 16:05:31 -05005020 } else {
5021 /* First copy command data */
5022 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5023 MAILBOX_CMD_SIZE);
5024 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5025 pmbox->context2) {
James Smart92d7f7b2007-06-17 19:56:38 -05005026 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea31012005-04-17 16:05:31 -05005027 phba->MBslimaddr + DMP_RSP_OFFSET,
5028 mb->un.varDmp.word_cnt);
5029 }
5030 }
5031
5032 writel(HA_MBATT, phba->HAregaddr);
5033 readl(phba->HAregaddr); /* flush */
5034
5035 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5036 status = mb->mbxStatus;
5037 }
5038
James Smart2e0fef82007-06-17 19:56:36 -05005039 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5040 return status;
James Smart58da1ff2008-04-07 10:15:56 -04005041
5042out_not_finished:
5043 if (processing_queue) {
James Smartda0436e2009-05-22 14:51:39 -04005044 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
James Smart58da1ff2008-04-07 10:15:56 -04005045 lpfc_mbox_cmpl_put(phba, pmbox);
5046 }
5047 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05005048}
5049
James Smarte59058c2008-08-24 21:49:00 -04005050/**
James Smartf1126682009-06-10 17:22:44 -04005051 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5052 * @phba: Pointer to HBA context object.
5053 *
5054 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5055 * the driver internal pending mailbox queue. It will then try to wait out the
5056 * possible outstanding mailbox command before return.
5057 *
5058 * Returns:
5059 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5060 * the outstanding mailbox command timed out.
5061 **/
5062static int
5063lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5064{
5065 struct lpfc_sli *psli = &phba->sli;
5066 uint8_t actcmd = MBX_HEARTBEAT;
5067 int rc = 0;
5068 unsigned long timeout;
5069
5070 /* Mark the asynchronous mailbox command posting as blocked */
5071 spin_lock_irq(&phba->hbalock);
5072 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5073 if (phba->sli.mbox_active)
5074 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5075 spin_unlock_irq(&phba->hbalock);
5076 /* Determine how long we might wait for the active mailbox
5077 * command to be gracefully completed by firmware.
5078 */
5079 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5080 jiffies;
5081 /* Wait for the outstnading mailbox command to complete */
5082 while (phba->sli.mbox_active) {
5083 /* Check active mailbox complete status every 2ms */
5084 msleep(2);
5085 if (time_after(jiffies, timeout)) {
5086 /* Timeout, marked the outstanding cmd not complete */
5087 rc = 1;
5088 break;
5089 }
5090 }
5091
5092 /* Can not cleanly block async mailbox command, fails it */
5093 if (rc) {
5094 spin_lock_irq(&phba->hbalock);
5095 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5096 spin_unlock_irq(&phba->hbalock);
5097 }
5098 return rc;
5099}
5100
5101/**
5102 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5103 * @phba: Pointer to HBA context object.
5104 *
5105 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5106 * commands from the driver internal pending mailbox queue. It makes sure
5107 * that there is no outstanding mailbox command before resuming posting
5108 * asynchronous mailbox commands. If, for any reason, there is outstanding
5109 * mailbox command, it will try to wait it out before resuming asynchronous
5110 * mailbox command posting.
5111 **/
5112static void
5113lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5114{
5115 struct lpfc_sli *psli = &phba->sli;
5116
5117 spin_lock_irq(&phba->hbalock);
5118 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5119 /* Asynchronous mailbox posting is not blocked, do nothing */
5120 spin_unlock_irq(&phba->hbalock);
5121 return;
5122 }
5123
5124 /* Outstanding synchronous mailbox command is guaranteed to be done,
5125 * successful or timeout, after timing-out the outstanding mailbox
5126 * command shall always be removed, so just unblock posting async
5127 * mailbox command and resume
5128 */
5129 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5130 spin_unlock_irq(&phba->hbalock);
5131
5132 /* wake up worker thread to post asynchronlous mailbox command */
5133 lpfc_worker_wake_up(phba);
5134}
5135
5136/**
James Smartda0436e2009-05-22 14:51:39 -04005137 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5138 * @phba: Pointer to HBA context object.
5139 * @mboxq: Pointer to mailbox object.
5140 *
5141 * The function posts a mailbox to the port. The mailbox is expected
5142 * to be comletely filled in and ready for the port to operate on it.
5143 * This routine executes a synchronous completion operation on the
5144 * mailbox by polling for its completion.
5145 *
5146 * The caller must not be holding any locks when calling this routine.
5147 *
5148 * Returns:
5149 * MBX_SUCCESS - mailbox posted successfully
5150 * Any of the MBX error values.
5151 **/
5152static int
5153lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5154{
5155 int rc = MBX_SUCCESS;
5156 unsigned long iflag;
5157 uint32_t db_ready;
5158 uint32_t mcqe_status;
5159 uint32_t mbx_cmnd;
5160 unsigned long timeout;
5161 struct lpfc_sli *psli = &phba->sli;
5162 struct lpfc_mqe *mb = &mboxq->u.mqe;
5163 struct lpfc_bmbx_create *mbox_rgn;
5164 struct dma_address *dma_address;
5165 struct lpfc_register bmbx_reg;
5166
5167 /*
5168 * Only one mailbox can be active to the bootstrap mailbox region
5169 * at a time and there is no queueing provided.
5170 */
5171 spin_lock_irqsave(&phba->hbalock, iflag);
5172 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5173 spin_unlock_irqrestore(&phba->hbalock, iflag);
5174 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5175 "(%d):2532 Mailbox command x%x (x%x) "
5176 "cannot issue Data: x%x x%x\n",
5177 mboxq->vport ? mboxq->vport->vpi : 0,
5178 mboxq->u.mb.mbxCommand,
5179 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5180 psli->sli_flag, MBX_POLL);
5181 return MBXERR_ERROR;
5182 }
5183 /* The server grabs the token and owns it until release */
5184 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5185 phba->sli.mbox_active = mboxq;
5186 spin_unlock_irqrestore(&phba->hbalock, iflag);
5187
5188 /*
5189 * Initialize the bootstrap memory region to avoid stale data areas
5190 * in the mailbox post. Then copy the caller's mailbox contents to
5191 * the bmbx mailbox region.
5192 */
5193 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5194 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5195 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5196 sizeof(struct lpfc_mqe));
5197
5198 /* Post the high mailbox dma address to the port and wait for ready. */
5199 dma_address = &phba->sli4_hba.bmbx.dma_address;
5200 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5201
5202 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5203 * 1000) + jiffies;
5204 do {
5205 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5206 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5207 if (!db_ready)
5208 msleep(2);
5209
5210 if (time_after(jiffies, timeout)) {
5211 rc = MBXERR_ERROR;
5212 goto exit;
5213 }
5214 } while (!db_ready);
5215
5216 /* Post the low mailbox dma address to the port. */
5217 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5218 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5219 * 1000) + jiffies;
5220 do {
5221 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5222 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5223 if (!db_ready)
5224 msleep(2);
5225
5226 if (time_after(jiffies, timeout)) {
5227 rc = MBXERR_ERROR;
5228 goto exit;
5229 }
5230 } while (!db_ready);
5231
5232 /*
5233 * Read the CQ to ensure the mailbox has completed.
5234 * If so, update the mailbox status so that the upper layers
5235 * can complete the request normally.
5236 */
5237 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5238 sizeof(struct lpfc_mqe));
5239 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5240 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5241 sizeof(struct lpfc_mcqe));
5242 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5243
5244 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5245 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5246 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5247 rc = MBXERR_ERROR;
5248 }
5249
5250 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5251 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5252 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5253 " x%x x%x CQ: x%x x%x x%x x%x\n",
5254 mboxq->vport ? mboxq->vport->vpi : 0,
5255 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5256 bf_get(lpfc_mqe_status, mb),
5257 mb->un.mb_words[0], mb->un.mb_words[1],
5258 mb->un.mb_words[2], mb->un.mb_words[3],
5259 mb->un.mb_words[4], mb->un.mb_words[5],
5260 mb->un.mb_words[6], mb->un.mb_words[7],
5261 mb->un.mb_words[8], mb->un.mb_words[9],
5262 mb->un.mb_words[10], mb->un.mb_words[11],
5263 mb->un.mb_words[12], mboxq->mcqe.word0,
5264 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5265 mboxq->mcqe.trailer);
5266exit:
5267 /* We are holding the token, no needed for lock when release */
5268 spin_lock_irqsave(&phba->hbalock, iflag);
5269 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5270 phba->sli.mbox_active = NULL;
5271 spin_unlock_irqrestore(&phba->hbalock, iflag);
5272 return rc;
5273}
5274
5275/**
5276 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5277 * @phba: Pointer to HBA context object.
5278 * @pmbox: Pointer to mailbox object.
5279 * @flag: Flag indicating how the mailbox need to be processed.
5280 *
5281 * This function is called by discovery code and HBA management code to submit
5282 * a mailbox command to firmware with SLI-4 interface spec.
5283 *
5284 * Return codes the caller owns the mailbox command after the return of the
5285 * function.
5286 **/
5287static int
5288lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5289 uint32_t flag)
5290{
5291 struct lpfc_sli *psli = &phba->sli;
5292 unsigned long iflags;
5293 int rc;
5294
James Smart8fa38512009-07-19 10:01:03 -04005295 rc = lpfc_mbox_dev_check(phba);
5296 if (unlikely(rc)) {
5297 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5298 "(%d):2544 Mailbox command x%x (x%x) "
5299 "cannot issue Data: x%x x%x\n",
5300 mboxq->vport ? mboxq->vport->vpi : 0,
5301 mboxq->u.mb.mbxCommand,
5302 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5303 psli->sli_flag, flag);
5304 goto out_not_finished;
5305 }
5306
James Smartda0436e2009-05-22 14:51:39 -04005307 /* Detect polling mode and jump to a handler */
5308 if (!phba->sli4_hba.intr_enable) {
5309 if (flag == MBX_POLL)
5310 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5311 else
5312 rc = -EIO;
5313 if (rc != MBX_SUCCESS)
5314 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5315 "(%d):2541 Mailbox command x%x "
5316 "(x%x) cannot issue Data: x%x x%x\n",
5317 mboxq->vport ? mboxq->vport->vpi : 0,
5318 mboxq->u.mb.mbxCommand,
5319 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5320 psli->sli_flag, flag);
5321 return rc;
5322 } else if (flag == MBX_POLL) {
James Smartf1126682009-06-10 17:22:44 -04005323 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5324 "(%d):2542 Try to issue mailbox command "
5325 "x%x (x%x) synchronously ahead of async"
5326 "mailbox command queue: x%x x%x\n",
James Smartda0436e2009-05-22 14:51:39 -04005327 mboxq->vport ? mboxq->vport->vpi : 0,
5328 mboxq->u.mb.mbxCommand,
5329 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5330 psli->sli_flag, flag);
James Smartf1126682009-06-10 17:22:44 -04005331 /* Try to block the asynchronous mailbox posting */
5332 rc = lpfc_sli4_async_mbox_block(phba);
5333 if (!rc) {
5334 /* Successfully blocked, now issue sync mbox cmd */
5335 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5336 if (rc != MBX_SUCCESS)
5337 lpfc_printf_log(phba, KERN_ERR,
5338 LOG_MBOX | LOG_SLI,
5339 "(%d):2597 Mailbox command "
5340 "x%x (x%x) cannot issue "
5341 "Data: x%x x%x\n",
5342 mboxq->vport ?
5343 mboxq->vport->vpi : 0,
5344 mboxq->u.mb.mbxCommand,
5345 lpfc_sli4_mbox_opcode_get(phba,
5346 mboxq),
5347 psli->sli_flag, flag);
5348 /* Unblock the async mailbox posting afterward */
5349 lpfc_sli4_async_mbox_unblock(phba);
5350 }
5351 return rc;
James Smartda0436e2009-05-22 14:51:39 -04005352 }
5353
5354 /* Now, interrupt mode asynchrous mailbox command */
5355 rc = lpfc_mbox_cmd_check(phba, mboxq);
5356 if (rc) {
5357 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5358 "(%d):2543 Mailbox command x%x (x%x) "
5359 "cannot issue Data: x%x x%x\n",
5360 mboxq->vport ? mboxq->vport->vpi : 0,
5361 mboxq->u.mb.mbxCommand,
5362 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5363 psli->sli_flag, flag);
5364 goto out_not_finished;
5365 }
James Smartda0436e2009-05-22 14:51:39 -04005366
5367 /* Put the mailbox command to the driver internal FIFO */
5368 psli->slistat.mbox_busy++;
5369 spin_lock_irqsave(&phba->hbalock, iflags);
5370 lpfc_mbox_put(phba, mboxq);
5371 spin_unlock_irqrestore(&phba->hbalock, iflags);
5372 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5373 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5374 "x%x (x%x) x%x x%x x%x\n",
5375 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5376 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5377 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5378 phba->pport->port_state,
5379 psli->sli_flag, MBX_NOWAIT);
5380 /* Wake up worker thread to transport mailbox command from head */
5381 lpfc_worker_wake_up(phba);
5382
5383 return MBX_BUSY;
5384
5385out_not_finished:
5386 return MBX_NOT_FINISHED;
5387}
5388
5389/**
5390 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5391 * @phba: Pointer to HBA context object.
5392 *
5393 * This function is called by worker thread to send a mailbox command to
5394 * SLI4 HBA firmware.
5395 *
5396 **/
5397int
5398lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5399{
5400 struct lpfc_sli *psli = &phba->sli;
5401 LPFC_MBOXQ_t *mboxq;
5402 int rc = MBX_SUCCESS;
5403 unsigned long iflags;
5404 struct lpfc_mqe *mqe;
5405 uint32_t mbx_cmnd;
5406
5407 /* Check interrupt mode before post async mailbox command */
5408 if (unlikely(!phba->sli4_hba.intr_enable))
5409 return MBX_NOT_FINISHED;
5410
5411 /* Check for mailbox command service token */
5412 spin_lock_irqsave(&phba->hbalock, iflags);
5413 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5414 spin_unlock_irqrestore(&phba->hbalock, iflags);
5415 return MBX_NOT_FINISHED;
5416 }
5417 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5418 spin_unlock_irqrestore(&phba->hbalock, iflags);
5419 return MBX_NOT_FINISHED;
5420 }
5421 if (unlikely(phba->sli.mbox_active)) {
5422 spin_unlock_irqrestore(&phba->hbalock, iflags);
5423 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5424 "0384 There is pending active mailbox cmd\n");
5425 return MBX_NOT_FINISHED;
5426 }
5427 /* Take the mailbox command service token */
5428 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5429
5430 /* Get the next mailbox command from head of queue */
5431 mboxq = lpfc_mbox_get(phba);
5432
5433 /* If no more mailbox command waiting for post, we're done */
5434 if (!mboxq) {
5435 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5436 spin_unlock_irqrestore(&phba->hbalock, iflags);
5437 return MBX_SUCCESS;
5438 }
5439 phba->sli.mbox_active = mboxq;
5440 spin_unlock_irqrestore(&phba->hbalock, iflags);
5441
5442 /* Check device readiness for posting mailbox command */
5443 rc = lpfc_mbox_dev_check(phba);
5444 if (unlikely(rc))
5445 /* Driver clean routine will clean up pending mailbox */
5446 goto out_not_finished;
5447
5448 /* Prepare the mbox command to be posted */
5449 mqe = &mboxq->u.mqe;
5450 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5451
5452 /* Start timer for the mbox_tmo and log some mailbox post messages */
5453 mod_timer(&psli->mbox_tmo, (jiffies +
5454 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5455
5456 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5457 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5458 "x%x x%x\n",
5459 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5460 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5461 phba->pport->port_state, psli->sli_flag);
5462
5463 if (mbx_cmnd != MBX_HEARTBEAT) {
5464 if (mboxq->vport) {
5465 lpfc_debugfs_disc_trc(mboxq->vport,
5466 LPFC_DISC_TRC_MBOX_VPORT,
5467 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5468 mbx_cmnd, mqe->un.mb_words[0],
5469 mqe->un.mb_words[1]);
5470 } else {
5471 lpfc_debugfs_disc_trc(phba->pport,
5472 LPFC_DISC_TRC_MBOX,
5473 "MBOX Send: cmd:x%x mb:x%x x%x",
5474 mbx_cmnd, mqe->un.mb_words[0],
5475 mqe->un.mb_words[1]);
5476 }
5477 }
5478 psli->slistat.mbox_cmd++;
5479
5480 /* Post the mailbox command to the port */
5481 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5482 if (rc != MBX_SUCCESS) {
5483 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5484 "(%d):2533 Mailbox command x%x (x%x) "
5485 "cannot issue Data: x%x x%x\n",
5486 mboxq->vport ? mboxq->vport->vpi : 0,
5487 mboxq->u.mb.mbxCommand,
5488 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5489 psli->sli_flag, MBX_NOWAIT);
5490 goto out_not_finished;
5491 }
5492
5493 return rc;
5494
5495out_not_finished:
5496 spin_lock_irqsave(&phba->hbalock, iflags);
5497 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5498 __lpfc_mbox_cmpl_put(phba, mboxq);
5499 /* Release the token */
5500 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5501 phba->sli.mbox_active = NULL;
5502 spin_unlock_irqrestore(&phba->hbalock, iflags);
5503
5504 return MBX_NOT_FINISHED;
5505}
5506
5507/**
5508 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5509 * @phba: Pointer to HBA context object.
5510 * @pmbox: Pointer to mailbox object.
5511 * @flag: Flag indicating how the mailbox need to be processed.
5512 *
5513 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5514 * the API jump table function pointer from the lpfc_hba struct.
5515 *
5516 * Return codes the caller owns the mailbox command after the return of the
5517 * function.
5518 **/
5519int
5520lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5521{
5522 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5523}
5524
5525/**
5526 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5527 * @phba: The hba struct for which this call is being executed.
5528 * @dev_grp: The HBA PCI-Device group number.
5529 *
5530 * This routine sets up the mbox interface API function jump table in @phba
5531 * struct.
5532 * Returns: 0 - success, -ENODEV - failure.
5533 **/
5534int
5535lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5536{
5537
5538 switch (dev_grp) {
5539 case LPFC_PCI_DEV_LP:
5540 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5541 phba->lpfc_sli_handle_slow_ring_event =
5542 lpfc_sli_handle_slow_ring_event_s3;
5543 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5544 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5545 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5546 break;
5547 case LPFC_PCI_DEV_OC:
5548 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5549 phba->lpfc_sli_handle_slow_ring_event =
5550 lpfc_sli_handle_slow_ring_event_s4;
5551 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5552 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5553 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5554 break;
5555 default:
5556 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5557 "1420 Invalid HBA PCI-device group: 0x%x\n",
5558 dev_grp);
5559 return -ENODEV;
5560 break;
5561 }
5562 return 0;
5563}
5564
5565/**
James Smart3621a712009-04-06 18:47:14 -04005566 * __lpfc_sli_ringtx_put - Add an iocb to the txq
James Smarte59058c2008-08-24 21:49:00 -04005567 * @phba: Pointer to HBA context object.
5568 * @pring: Pointer to driver SLI ring object.
5569 * @piocb: Pointer to address of newly added command iocb.
5570 *
5571 * This function is called with hbalock held to add a command
5572 * iocb to the txq when SLI layer cannot submit the command iocb
5573 * to the ring.
5574 **/
James Smart858c9f62007-06-17 19:56:39 -05005575static void
James Smart92d7f7b2007-06-17 19:56:38 -05005576__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005577 struct lpfc_iocbq *piocb)
dea31012005-04-17 16:05:31 -05005578{
5579 /* Insert the caller's iocb in the txq tail for later processing. */
5580 list_add_tail(&piocb->list, &pring->txq);
5581 pring->txq_cnt++;
dea31012005-04-17 16:05:31 -05005582}
5583
James Smarte59058c2008-08-24 21:49:00 -04005584/**
James Smart3621a712009-04-06 18:47:14 -04005585 * lpfc_sli_next_iocb - Get the next iocb in the txq
James Smarte59058c2008-08-24 21:49:00 -04005586 * @phba: Pointer to HBA context object.
5587 * @pring: Pointer to driver SLI ring object.
5588 * @piocb: Pointer to address of newly added command iocb.
5589 *
5590 * This function is called with hbalock held before a new
5591 * iocb is submitted to the firmware. This function checks
5592 * txq to flush the iocbs in txq to Firmware before
5593 * submitting new iocbs to the Firmware.
5594 * If there are iocbs in the txq which need to be submitted
5595 * to firmware, lpfc_sli_next_iocb returns the first element
5596 * of the txq after dequeuing it from txq.
5597 * If there is no iocb in the txq then the function will return
5598 * *piocb and *piocb is set to NULL. Caller needs to check
5599 * *piocb to find if there are more commands in the txq.
5600 **/
dea31012005-04-17 16:05:31 -05005601static struct lpfc_iocbq *
5602lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
James Smart2e0fef82007-06-17 19:56:36 -05005603 struct lpfc_iocbq **piocb)
dea31012005-04-17 16:05:31 -05005604{
5605 struct lpfc_iocbq * nextiocb;
5606
5607 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5608 if (!nextiocb) {
5609 nextiocb = *piocb;
5610 *piocb = NULL;
5611 }
5612
5613 return nextiocb;
5614}
5615
James Smarte59058c2008-08-24 21:49:00 -04005616/**
James Smart3772a992009-05-22 14:50:54 -04005617 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04005618 * @phba: Pointer to HBA context object.
James Smart3772a992009-05-22 14:50:54 -04005619 * @ring_number: SLI ring number to issue iocb on.
James Smarte59058c2008-08-24 21:49:00 -04005620 * @piocb: Pointer to command iocb.
5621 * @flag: Flag indicating if this command can be put into txq.
5622 *
James Smart3772a992009-05-22 14:50:54 -04005623 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5624 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5625 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5626 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5627 * this function allows only iocbs for posting buffers. This function finds
5628 * next available slot in the command ring and posts the command to the
5629 * available slot and writes the port attention register to request HBA start
5630 * processing new iocb. If there is no slot available in the ring and
5631 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5632 * the function returns IOCB_BUSY.
James Smarte59058c2008-08-24 21:49:00 -04005633 *
James Smart3772a992009-05-22 14:50:54 -04005634 * This function is called with hbalock held. The function will return success
5635 * after it successfully submit the iocb to firmware or after adding to the
5636 * txq.
James Smarte59058c2008-08-24 21:49:00 -04005637 **/
James Smart98c9ea52007-10-27 13:37:33 -04005638static int
James Smart3772a992009-05-22 14:50:54 -04005639__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea31012005-04-17 16:05:31 -05005640 struct lpfc_iocbq *piocb, uint32_t flag)
5641{
5642 struct lpfc_iocbq *nextiocb;
5643 IOCB_t *iocb;
James Smart3772a992009-05-22 14:50:54 -04005644 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea31012005-04-17 16:05:31 -05005645
James Smart92d7f7b2007-06-17 19:56:38 -05005646 if (piocb->iocb_cmpl && (!piocb->vport) &&
5647 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5648 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5649 lpfc_printf_log(phba, KERN_ERR,
5650 LOG_SLI | LOG_VPORT,
James Smarte8b62012007-08-02 11:10:09 -04005651 "1807 IOCB x%x failed. No vport\n",
James Smart92d7f7b2007-06-17 19:56:38 -05005652 piocb->iocb.ulpCommand);
5653 dump_stack();
5654 return IOCB_ERROR;
5655 }
5656
5657
Linas Vepstas8d63f372007-02-14 14:28:36 -06005658 /* If the PCI channel is in offline state, do not post iocbs. */
5659 if (unlikely(pci_channel_offline(phba->pcidev)))
5660 return IOCB_ERROR;
5661
James Smarta257bf92009-04-06 18:48:10 -04005662 /* If HBA has a deferred error attention, fail the iocb. */
5663 if (unlikely(phba->hba_flag & DEFER_ERATT))
5664 return IOCB_ERROR;
5665
dea31012005-04-17 16:05:31 -05005666 /*
5667 * We should never get an IOCB if we are in a < LINK_DOWN state
5668 */
James Smart2e0fef82007-06-17 19:56:36 -05005669 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea31012005-04-17 16:05:31 -05005670 return IOCB_ERROR;
5671
5672 /*
5673 * Check to see if we are blocking IOCB processing because of a
James Smart0b727fe2007-10-27 13:37:25 -04005674 * outstanding event.
dea31012005-04-17 16:05:31 -05005675 */
James Smart0b727fe2007-10-27 13:37:25 -04005676 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea31012005-04-17 16:05:31 -05005677 goto iocb_busy;
5678
James Smart2e0fef82007-06-17 19:56:36 -05005679 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea31012005-04-17 16:05:31 -05005680 /*
James Smart2680eea2007-04-25 09:52:55 -04005681 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea31012005-04-17 16:05:31 -05005682 * can be issued if the link is not up.
5683 */
5684 switch (piocb->iocb.ulpCommand) {
James Smart84774a42008-08-24 21:50:06 -04005685 case CMD_GEN_REQUEST64_CR:
5686 case CMD_GEN_REQUEST64_CX:
5687 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5688 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
5689 FC_FCP_CMND) ||
5690 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5691 MENLO_TRANSPORT_TYPE))
5692
5693 goto iocb_busy;
5694 break;
dea31012005-04-17 16:05:31 -05005695 case CMD_QUE_RING_BUF_CN:
5696 case CMD_QUE_RING_BUF64_CN:
dea31012005-04-17 16:05:31 -05005697 /*
5698 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5699 * completion, iocb_cmpl MUST be 0.
5700 */
5701 if (piocb->iocb_cmpl)
5702 piocb->iocb_cmpl = NULL;
5703 /*FALLTHROUGH*/
5704 case CMD_CREATE_XRI_CR:
James Smart2680eea2007-04-25 09:52:55 -04005705 case CMD_CLOSE_XRI_CN:
5706 case CMD_CLOSE_XRI_CX:
dea31012005-04-17 16:05:31 -05005707 break;
5708 default:
5709 goto iocb_busy;
5710 }
5711
5712 /*
5713 * For FCP commands, we must be in a state where we can process link
5714 * attention events.
5715 */
5716 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
James Smart92d7f7b2007-06-17 19:56:38 -05005717 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea31012005-04-17 16:05:31 -05005718 goto iocb_busy;
James Smart92d7f7b2007-06-17 19:56:38 -05005719 }
dea31012005-04-17 16:05:31 -05005720
dea31012005-04-17 16:05:31 -05005721 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5722 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5723 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5724
5725 if (iocb)
5726 lpfc_sli_update_ring(phba, pring);
5727 else
5728 lpfc_sli_update_full_ring(phba, pring);
5729
5730 if (!piocb)
5731 return IOCB_SUCCESS;
5732
5733 goto out_busy;
5734
5735 iocb_busy:
5736 pring->stats.iocb_cmd_delay++;
5737
5738 out_busy:
5739
5740 if (!(flag & SLI_IOCB_RET_IOCB)) {
James Smart92d7f7b2007-06-17 19:56:38 -05005741 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea31012005-04-17 16:05:31 -05005742 return IOCB_SUCCESS;
5743 }
5744
5745 return IOCB_BUSY;
5746}
5747
James Smart3772a992009-05-22 14:50:54 -04005748/**
James Smart4f774512009-05-22 14:52:35 -04005749 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5750 * @phba: Pointer to HBA context object.
5751 * @piocb: Pointer to command iocb.
5752 * @sglq: Pointer to the scatter gather queue object.
5753 *
5754 * This routine converts the bpl or bde that is in the IOCB
5755 * to a sgl list for the sli4 hardware. The physical address
5756 * of the bpl/bde is converted back to a virtual address.
5757 * If the IOCB contains a BPL then the list of BDE's is
5758 * converted to sli4_sge's. If the IOCB contains a single
5759 * BDE then it is converted to a single sli_sge.
5760 * The IOCB is still in cpu endianess so the contents of
5761 * the bpl can be used without byte swapping.
5762 *
5763 * Returns valid XRI = Success, NO_XRI = Failure.
5764**/
5765static uint16_t
5766lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5767 struct lpfc_sglq *sglq)
5768{
5769 uint16_t xritag = NO_XRI;
5770 struct ulp_bde64 *bpl = NULL;
5771 struct ulp_bde64 bde;
5772 struct sli4_sge *sgl = NULL;
5773 IOCB_t *icmd;
5774 int numBdes = 0;
5775 int i = 0;
5776
5777 if (!piocbq || !sglq)
5778 return xritag;
5779
5780 sgl = (struct sli4_sge *)sglq->sgl;
5781 icmd = &piocbq->iocb;
5782 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5783 numBdes = icmd->un.genreq64.bdl.bdeSize /
5784 sizeof(struct ulp_bde64);
5785 /* The addrHigh and addrLow fields within the IOCB
5786 * have not been byteswapped yet so there is no
5787 * need to swap them back.
5788 */
5789 bpl = (struct ulp_bde64 *)
5790 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5791
5792 if (!bpl)
5793 return xritag;
5794
5795 for (i = 0; i < numBdes; i++) {
5796 /* Should already be byte swapped. */
5797 sgl->addr_hi = bpl->addrHigh;
5798 sgl->addr_lo = bpl->addrLow;
5799 /* swap the size field back to the cpu so we
5800 * can assign it to the sgl.
5801 */
5802 bde.tus.w = le32_to_cpu(bpl->tus.w);
5803 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5804 if ((i+1) == numBdes)
5805 bf_set(lpfc_sli4_sge_last, sgl, 1);
5806 else
5807 bf_set(lpfc_sli4_sge_last, sgl, 0);
5808 sgl->word2 = cpu_to_le32(sgl->word2);
5809 sgl->word3 = cpu_to_le32(sgl->word3);
5810 bpl++;
5811 sgl++;
5812 }
5813 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5814 /* The addrHigh and addrLow fields of the BDE have not
5815 * been byteswapped yet so they need to be swapped
5816 * before putting them in the sgl.
5817 */
5818 sgl->addr_hi =
5819 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5820 sgl->addr_lo =
5821 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5822 bf_set(lpfc_sli4_sge_len, sgl,
5823 icmd->un.genreq64.bdl.bdeSize);
5824 bf_set(lpfc_sli4_sge_last, sgl, 1);
5825 sgl->word2 = cpu_to_le32(sgl->word2);
5826 sgl->word3 = cpu_to_le32(sgl->word3);
5827 }
5828 return sglq->sli4_xritag;
5829}
5830
5831/**
5832 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
5833 * @phba: Pointer to HBA context object.
James Smart4f774512009-05-22 14:52:35 -04005834 *
5835 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
James Smart8fa38512009-07-19 10:01:03 -04005836 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5837 * held.
James Smart4f774512009-05-22 14:52:35 -04005838 *
5839 * Return: index into SLI4 fast-path FCP queue index.
5840 **/
5841static uint32_t
James Smart8fa38512009-07-19 10:01:03 -04005842lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
James Smart4f774512009-05-22 14:52:35 -04005843{
James Smart8fa38512009-07-19 10:01:03 -04005844 ++phba->fcp_qidx;
5845 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5846 phba->fcp_qidx = 0;
James Smart4f774512009-05-22 14:52:35 -04005847
James Smart8fa38512009-07-19 10:01:03 -04005848 return phba->fcp_qidx;
James Smart4f774512009-05-22 14:52:35 -04005849}
5850
5851/**
5852 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
5853 * @phba: Pointer to HBA context object.
5854 * @piocb: Pointer to command iocb.
5855 * @wqe: Pointer to the work queue entry.
5856 *
5857 * This routine converts the iocb command to its Work Queue Entry
5858 * equivalent. The wqe pointer should not have any fields set when
5859 * this routine is called because it will memcpy over them.
5860 * This routine does not set the CQ_ID or the WQEC bits in the
5861 * wqe.
5862 *
5863 * Returns: 0 = Success, IOCB_ERROR = Failure.
5864 **/
5865static int
5866lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5867 union lpfc_wqe *wqe)
5868{
5869 uint32_t payload_len = 0;
5870 uint8_t ct = 0;
5871 uint32_t fip;
5872 uint32_t abort_tag;
5873 uint8_t command_type = ELS_COMMAND_NON_FIP;
5874 uint8_t cmnd;
5875 uint16_t xritag;
5876 struct ulp_bde64 *bpl = NULL;
5877
5878 fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
5879 /* The fcp commands will set command type */
James Smart0c287582009-06-10 17:22:56 -04005880 if (iocbq->iocb_flag & LPFC_IO_FCP)
James Smart4f774512009-05-22 14:52:35 -04005881 command_type = FCP_COMMAND;
James Smart0c287582009-06-10 17:22:56 -04005882 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
5883 command_type = ELS_COMMAND_FIP;
5884 else
5885 command_type = ELS_COMMAND_NON_FIP;
5886
James Smart4f774512009-05-22 14:52:35 -04005887 /* Some of the fields are in the right position already */
5888 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5889 abort_tag = (uint32_t) iocbq->iotag;
5890 xritag = iocbq->sli4_xritag;
5891 wqe->words[7] = 0; /* The ct field has moved so reset */
5892 /* words0-2 bpl convert bde */
5893 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5894 bpl = (struct ulp_bde64 *)
5895 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5896 if (!bpl)
5897 return IOCB_ERROR;
5898
5899 /* Should already be byte swapped. */
5900 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5901 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5902 /* swap the size field back to the cpu so we
5903 * can assign it to the sgl.
5904 */
5905 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5906 payload_len = wqe->generic.bde.tus.f.bdeSize;
5907 } else
5908 payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
5909
5910 iocbq->iocb.ulpIoTag = iocbq->iotag;
5911 cmnd = iocbq->iocb.ulpCommand;
5912
5913 switch (iocbq->iocb.ulpCommand) {
5914 case CMD_ELS_REQUEST64_CR:
5915 if (!iocbq->iocb.ulpLe) {
5916 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5917 "2007 Only Limited Edition cmd Format"
5918 " supported 0x%x\n",
5919 iocbq->iocb.ulpCommand);
5920 return IOCB_ERROR;
5921 }
5922 wqe->els_req.payload_len = payload_len;
5923 /* Els_reguest64 has a TMO */
5924 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5925 iocbq->iocb.ulpTimeout);
5926 /* Need a VF for word 4 set the vf bit*/
5927 bf_set(els_req64_vf, &wqe->els_req, 0);
5928 /* And a VFID for word 12 */
5929 bf_set(els_req64_vfid, &wqe->els_req, 0);
5930 /*
5931 * Set ct field to 3, indicates that the context_tag field
5932 * contains the FCFI and remote N_Port_ID is
5933 * in word 5.
5934 */
5935
5936 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5937 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5938 iocbq->iocb.ulpContext);
5939
James Smart4f774512009-05-22 14:52:35 -04005940 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5941 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5942 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5943 break;
5944 case CMD_XMIT_SEQUENCE64_CR:
5945 /* word3 iocb=io_tag32 wqe=payload_offset */
5946 /* payload offset used for multilpe outstanding
5947 * sequences on the same exchange
5948 */
5949 wqe->words[3] = 0;
5950 /* word4 relative_offset memcpy */
5951 /* word5 r_ctl/df_ctl memcpy */
5952 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5953 wqe->xmit_sequence.xmit_len = payload_len;
5954 break;
5955 case CMD_XMIT_BCAST64_CN:
5956 /* word3 iocb=iotag32 wqe=payload_len */
5957 wqe->words[3] = 0; /* no definition for this in wqe */
5958 /* word4 iocb=rsvd wqe=rsvd */
5959 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5960 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5961 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5962 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5963 break;
5964 case CMD_FCP_IWRITE64_CR:
5965 command_type = FCP_COMMAND_DATA_OUT;
5966 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5967 * confusing.
5968 * word3 is payload_len: byte offset to the sgl entry for the
5969 * fcp_command.
5970 * word4 is total xfer len, same as the IOCB->ulpParameter.
5971 * word5 is initial xfer len 0 = wait for xfer-ready
5972 */
5973
5974 /* Always wait for xfer-ready before sending data */
5975 wqe->fcp_iwrite.initial_xfer_len = 0;
5976 /* word 4 (xfer length) should have been set on the memcpy */
5977
5978 /* allow write to fall through to read */
5979 case CMD_FCP_IREAD64_CR:
5980 /* FCP_CMD is always the 1st sgl entry */
5981 wqe->fcp_iread.payload_len =
5982 payload_len + sizeof(struct fcp_rsp);
5983
5984 /* word 4 (xfer length) should have been set on the memcpy */
5985
5986 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5987 iocbq->iocb.ulpFCP2Rcvy);
5988 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5989 /* The XC bit and the XS bit are similar. The driver never
5990 * tracked whether or not the exchange was previouslly open.
5991 * XC = Exchange create, 0 is create. 1 is already open.
5992 * XS = link cmd: 1 do not close the exchange after command.
5993 * XS = 0 close exchange when command completes.
5994 * The only time we would not set the XC bit is when the XS bit
5995 * is set and we are sending our 2nd or greater command on
5996 * this exchange.
5997 */
James Smart4f774512009-05-22 14:52:35 -04005998 /* Always open the exchange */
5999 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6000
6001 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6002 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
James Smartf1126682009-06-10 17:22:44 -04006003 break;
6004 case CMD_FCP_ICMND64_CR:
6005 /* Always open the exchange */
6006 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6007
6008 wqe->words[4] = 0;
6009 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6010 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
James Smart4f774512009-05-22 14:52:35 -04006011 break;
6012 case CMD_GEN_REQUEST64_CR:
6013 /* word3 command length is described as byte offset to the
6014 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6015 * sgl[0] = cmnd
6016 * sgl[1] = rsp.
6017 *
6018 */
6019 wqe->gen_req.command_len = payload_len;
6020 /* Word4 parameter copied in the memcpy */
6021 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6022 /* word6 context tag copied in memcpy */
6023 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6024 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6025 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6026 "2015 Invalid CT %x command 0x%x\n",
6027 ct, iocbq->iocb.ulpCommand);
6028 return IOCB_ERROR;
6029 }
6030 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6031 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6032 iocbq->iocb.ulpTimeout);
6033
6034 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6035 command_type = OTHER_COMMAND;
6036 break;
6037 case CMD_XMIT_ELS_RSP64_CX:
6038 /* words0-2 BDE memcpy */
6039 /* word3 iocb=iotag32 wqe=rsvd */
6040 wqe->words[3] = 0;
6041 /* word4 iocb=did wge=rsvd. */
6042 wqe->words[4] = 0;
6043 /* word5 iocb=rsvd wge=did */
6044 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6045 iocbq->iocb.un.elsreq64.remoteID);
6046
6047 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6048 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6049
6050 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6051 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6052 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6053 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6054 iocbq->vport->vpi + phba->vpi_base);
6055 command_type = OTHER_COMMAND;
6056 break;
6057 case CMD_CLOSE_XRI_CN:
6058 case CMD_ABORT_XRI_CN:
6059 case CMD_ABORT_XRI_CX:
6060 /* words 0-2 memcpy should be 0 rserved */
6061 /* port will send abts */
6062 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6063 /*
6064 * The link is down so the fw does not need to send abts
6065 * on the wire.
6066 */
6067 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6068 else
6069 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6070 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6071 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6072 wqe->words[5] = 0;
6073 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6074 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6075 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6076 wqe->generic.abort_tag = abort_tag;
6077 /*
6078 * The abort handler will send us CMD_ABORT_XRI_CN or
6079 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6080 */
6081 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6082 cmnd = CMD_ABORT_XRI_CX;
6083 command_type = OTHER_COMMAND;
6084 xritag = 0;
6085 break;
James Smart6669f9b2009-10-02 15:16:45 -04006086 case CMD_XMIT_BLS_RSP64_CX:
6087 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6088 * we re-construct this WQE here based on information in
6089 * iocbq from scratch.
6090 */
6091 memset(wqe, 0, sizeof(union lpfc_wqe));
6092 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6093 iocbq->iocb.un.ulpWord[3]);
6094 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6095 iocbq->sli4_xritag);
6096 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6097 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6098 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6099 iocbq->iocb.ulpContext);
6100 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6101 command_type = OTHER_COMMAND;
6102 break;
James Smart4f774512009-05-22 14:52:35 -04006103 case CMD_XRI_ABORTED_CX:
6104 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6105 /* words0-2 are all 0's no bde */
6106 /* word3 and word4 are rsvrd */
6107 wqe->words[3] = 0;
6108 wqe->words[4] = 0;
6109 /* word5 iocb=rsvd wge=did */
6110 /* There is no remote port id in the IOCB? */
6111 /* Let this fall through and fail */
6112 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6113 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6114 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6115 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6116 default:
6117 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6118 "2014 Invalid command 0x%x\n",
6119 iocbq->iocb.ulpCommand);
6120 return IOCB_ERROR;
6121 break;
6122
6123 }
6124 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6125 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6126 wqe->generic.abort_tag = abort_tag;
6127 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6128 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6129 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6130 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6131
6132 return 0;
6133}
6134
6135/**
6136 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6137 * @phba: Pointer to HBA context object.
6138 * @ring_number: SLI ring number to issue iocb on.
6139 * @piocb: Pointer to command iocb.
6140 * @flag: Flag indicating if this command can be put into txq.
6141 *
6142 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6143 * an iocb command to an HBA with SLI-4 interface spec.
6144 *
6145 * This function is called with hbalock held. The function will return success
6146 * after it successfully submit the iocb to firmware or after adding to the
6147 * txq.
6148 **/
6149static int
6150__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6151 struct lpfc_iocbq *piocb, uint32_t flag)
6152{
6153 struct lpfc_sglq *sglq;
6154 uint16_t xritag;
6155 union lpfc_wqe wqe;
6156 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6157 uint32_t fcp_wqidx;
6158
6159 if (piocb->sli4_xritag == NO_XRI) {
6160 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
James Smart6669f9b2009-10-02 15:16:45 -04006161 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
James Smart4f774512009-05-22 14:52:35 -04006162 sglq = NULL;
6163 else {
6164 sglq = __lpfc_sli_get_sglq(phba);
6165 if (!sglq)
6166 return IOCB_ERROR;
6167 piocb->sli4_xritag = sglq->sli4_xritag;
6168 }
6169 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6170 sglq = NULL; /* These IO's already have an XRI and
6171 * a mapped sgl.
6172 */
6173 } else {
6174 /* This is a continuation of a commandi,(CX) so this
6175 * sglq is on the active list
6176 */
6177 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6178 if (!sglq)
6179 return IOCB_ERROR;
6180 }
6181
6182 if (sglq) {
6183 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6184 if (xritag != sglq->sli4_xritag)
6185 return IOCB_ERROR;
6186 }
6187
6188 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6189 return IOCB_ERROR;
6190
6191 if (piocb->iocb_flag & LPFC_IO_FCP) {
James Smart8fa38512009-07-19 10:01:03 -04006192 fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
James Smart4f774512009-05-22 14:52:35 -04006193 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
6194 return IOCB_ERROR;
6195 } else {
6196 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6197 return IOCB_ERROR;
6198 }
6199 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6200
6201 return 0;
6202}
6203
6204/**
James Smart3772a992009-05-22 14:50:54 -04006205 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6206 *
6207 * This routine wraps the actual lockless version for issusing IOCB function
6208 * pointer from the lpfc_hba struct.
6209 *
6210 * Return codes:
6211 * IOCB_ERROR - Error
6212 * IOCB_SUCCESS - Success
6213 * IOCB_BUSY - Busy
6214 **/
6215static inline int
6216__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6217 struct lpfc_iocbq *piocb, uint32_t flag)
6218{
6219 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6220}
6221
6222/**
6223 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6224 * @phba: The hba struct for which this call is being executed.
6225 * @dev_grp: The HBA PCI-Device group number.
6226 *
6227 * This routine sets up the SLI interface API function jump table in @phba
6228 * struct.
6229 * Returns: 0 - success, -ENODEV - failure.
6230 **/
6231int
6232lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6233{
6234
6235 switch (dev_grp) {
6236 case LPFC_PCI_DEV_LP:
6237 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6238 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6239 break;
James Smart4f774512009-05-22 14:52:35 -04006240 case LPFC_PCI_DEV_OC:
6241 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6242 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6243 break;
James Smart3772a992009-05-22 14:50:54 -04006244 default:
6245 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6246 "1419 Invalid HBA PCI-device group: 0x%x\n",
6247 dev_grp);
6248 return -ENODEV;
6249 break;
6250 }
6251 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6252 return 0;
6253}
James Smart92d7f7b2007-06-17 19:56:38 -05006254
James Smarte59058c2008-08-24 21:49:00 -04006255/**
James Smart3621a712009-04-06 18:47:14 -04006256 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
James Smarte59058c2008-08-24 21:49:00 -04006257 * @phba: Pointer to HBA context object.
6258 * @pring: Pointer to driver SLI ring object.
6259 * @piocb: Pointer to command iocb.
6260 * @flag: Flag indicating if this command can be put into txq.
6261 *
6262 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6263 * function. This function gets the hbalock and calls
6264 * __lpfc_sli_issue_iocb function and will return the error returned
6265 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6266 * functions which do not hold hbalock.
6267 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006268int
James Smart3772a992009-05-22 14:50:54 -04006269lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
James Smart92d7f7b2007-06-17 19:56:38 -05006270 struct lpfc_iocbq *piocb, uint32_t flag)
6271{
6272 unsigned long iflags;
6273 int rc;
6274
6275 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart3772a992009-05-22 14:50:54 -04006276 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
James Smart92d7f7b2007-06-17 19:56:38 -05006277 spin_unlock_irqrestore(&phba->hbalock, iflags);
6278
6279 return rc;
6280}
6281
James Smarte59058c2008-08-24 21:49:00 -04006282/**
James Smart3621a712009-04-06 18:47:14 -04006283 * lpfc_extra_ring_setup - Extra ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006284 * @phba: Pointer to HBA context object.
6285 *
6286 * This function is called while driver attaches with the
6287 * HBA to setup the extra ring. The extra ring is used
6288 * only when driver needs to support target mode functionality
6289 * or IP over FC functionalities.
6290 *
6291 * This function is called with no lock held.
6292 **/
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006293static int
6294lpfc_extra_ring_setup( struct lpfc_hba *phba)
6295{
6296 struct lpfc_sli *psli;
6297 struct lpfc_sli_ring *pring;
6298
6299 psli = &phba->sli;
6300
6301 /* Adjust cmd/rsp ring iocb entries more evenly */
James Smarta4bc3372006-12-02 13:34:16 -05006302
6303 /* Take some away from the FCP ring */
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006304 pring = &psli->ring[psli->fcp_ring];
6305 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6306 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6307 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6308 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6309
James Smarta4bc3372006-12-02 13:34:16 -05006310 /* and give them to the extra ring */
6311 pring = &psli->ring[psli->extra_ring];
6312
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006313 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6314 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6315 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6316 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6317
6318 /* Setup default profile for this ring */
6319 pring->iotag_max = 4096;
6320 pring->num_mask = 1;
6321 pring->prt[0].profile = 0; /* Mask 0 */
James Smarta4bc3372006-12-02 13:34:16 -05006322 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6323 pring->prt[0].type = phba->cfg_multi_ring_type;
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006324 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6325 return 0;
6326}
6327
James Smarte59058c2008-08-24 21:49:00 -04006328/**
James Smart3621a712009-04-06 18:47:14 -04006329 * lpfc_sli_async_event_handler - ASYNC iocb handler function
James Smarte59058c2008-08-24 21:49:00 -04006330 * @phba: Pointer to HBA context object.
6331 * @pring: Pointer to driver SLI ring object.
6332 * @iocbq: Pointer to iocb object.
6333 *
6334 * This function is called by the slow ring event handler
6335 * function when there is an ASYNC event iocb in the ring.
6336 * This function is called with no lock held.
6337 * Currently this function handles only temperature related
6338 * ASYNC events. The function decodes the temperature sensor
6339 * event message and posts events for the management applications.
6340 **/
James Smart98c9ea52007-10-27 13:37:33 -04006341static void
James Smart57127f12007-10-27 13:37:05 -04006342lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6343 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6344{
6345 IOCB_t *icmd;
6346 uint16_t evt_code;
6347 uint16_t temp;
6348 struct temp_event temp_event_data;
6349 struct Scsi_Host *shost;
James Smarta257bf92009-04-06 18:48:10 -04006350 uint32_t *iocb_w;
James Smart57127f12007-10-27 13:37:05 -04006351
6352 icmd = &iocbq->iocb;
6353 evt_code = icmd->un.asyncstat.evt_code;
6354 temp = icmd->ulpContext;
6355
6356 if ((evt_code != ASYNC_TEMP_WARN) &&
6357 (evt_code != ASYNC_TEMP_SAFE)) {
James Smarta257bf92009-04-06 18:48:10 -04006358 iocb_w = (uint32_t *) icmd;
James Smart57127f12007-10-27 13:37:05 -04006359 lpfc_printf_log(phba,
6360 KERN_ERR,
6361 LOG_SLI,
James Smart76bb24e2007-10-27 13:38:00 -04006362 "0346 Ring %d handler: unexpected ASYNC_STATUS"
James Smarte4e74272009-07-19 10:01:38 -04006363 " evt_code 0x%x\n"
James Smarta257bf92009-04-06 18:48:10 -04006364 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6365 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6366 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6367 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
James Smart57127f12007-10-27 13:37:05 -04006368 pring->ringno,
James Smarta257bf92009-04-06 18:48:10 -04006369 icmd->un.asyncstat.evt_code,
6370 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6371 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6372 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6373 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6374
James Smart57127f12007-10-27 13:37:05 -04006375 return;
6376 }
6377 temp_event_data.data = (uint32_t)temp;
6378 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6379 if (evt_code == ASYNC_TEMP_WARN) {
6380 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6381 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006382 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006383 LOG_TEMP,
James Smart76bb24e2007-10-27 13:38:00 -04006384 "0347 Adapter is very hot, please take "
James Smart57127f12007-10-27 13:37:05 -04006385 "corrective action. temperature : %d Celsius\n",
6386 temp);
6387 }
6388 if (evt_code == ASYNC_TEMP_SAFE) {
6389 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6390 lpfc_printf_log(phba,
James Smart09372822008-01-11 01:52:54 -05006391 KERN_ERR,
James Smart57127f12007-10-27 13:37:05 -04006392 LOG_TEMP,
6393 "0340 Adapter temperature is OK now. "
6394 "temperature : %d Celsius\n",
6395 temp);
6396 }
6397
6398 /* Send temperature change event to applications */
6399 shost = lpfc_shost_from_vport(phba->pport);
6400 fc_host_post_vendor_event(shost, fc_get_event_number(),
6401 sizeof(temp_event_data), (char *) &temp_event_data,
James Smartddcc50f2008-12-04 22:38:46 -05006402 LPFC_NL_VENDOR_ID);
James Smart57127f12007-10-27 13:37:05 -04006403
6404}
6405
6406
James Smarte59058c2008-08-24 21:49:00 -04006407/**
James Smart3621a712009-04-06 18:47:14 -04006408 * lpfc_sli_setup - SLI ring setup function
James Smarte59058c2008-08-24 21:49:00 -04006409 * @phba: Pointer to HBA context object.
6410 *
6411 * lpfc_sli_setup sets up rings of the SLI interface with
6412 * number of iocbs per ring and iotags. This function is
6413 * called while driver attach to the HBA and before the
6414 * interrupts are enabled. So there is no need for locking.
6415 *
6416 * This function always returns 0.
6417 **/
dea31012005-04-17 16:05:31 -05006418int
6419lpfc_sli_setup(struct lpfc_hba *phba)
6420{
James Smarted957682007-06-17 19:56:37 -05006421 int i, totiocbsize = 0;
dea31012005-04-17 16:05:31 -05006422 struct lpfc_sli *psli = &phba->sli;
6423 struct lpfc_sli_ring *pring;
6424
6425 psli->num_rings = MAX_CONFIGURED_RINGS;
6426 psli->sli_flag = 0;
6427 psli->fcp_ring = LPFC_FCP_RING;
6428 psli->next_ring = LPFC_FCP_NEXT_RING;
James Smarta4bc3372006-12-02 13:34:16 -05006429 psli->extra_ring = LPFC_EXTRA_RING;
dea31012005-04-17 16:05:31 -05006430
James Bottomley604a3e32005-10-29 10:28:33 -05006431 psli->iocbq_lookup = NULL;
6432 psli->iocbq_lookup_len = 0;
6433 psli->last_iotag = 0;
6434
dea31012005-04-17 16:05:31 -05006435 for (i = 0; i < psli->num_rings; i++) {
6436 pring = &psli->ring[i];
6437 switch (i) {
6438 case LPFC_FCP_RING: /* ring 0 - FCP */
6439 /* numCiocb and numRiocb are used in config_port */
6440 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6441 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6442 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6443 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6444 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6445 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006446 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006447 SLI3_IOCB_CMD_SIZE :
6448 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006449 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006450 SLI3_IOCB_RSP_SIZE :
6451 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006452 pring->iotag_ctr = 0;
6453 pring->iotag_max =
James Smart92d7f7b2007-06-17 19:56:38 -05006454 (phba->cfg_hba_queue_depth * 2);
dea31012005-04-17 16:05:31 -05006455 pring->fast_iotag = pring->iotag_max;
6456 pring->num_mask = 0;
6457 break;
James Smarta4bc3372006-12-02 13:34:16 -05006458 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea31012005-04-17 16:05:31 -05006459 /* numCiocb and numRiocb are used in config_port */
6460 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6461 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006462 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006463 SLI3_IOCB_CMD_SIZE :
6464 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006465 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006466 SLI3_IOCB_RSP_SIZE :
6467 SLI2_IOCB_RSP_SIZE;
James Smart2e0fef82007-06-17 19:56:36 -05006468 pring->iotag_max = phba->cfg_hba_queue_depth;
dea31012005-04-17 16:05:31 -05006469 pring->num_mask = 0;
6470 break;
6471 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6472 /* numCiocb and numRiocb are used in config_port */
6473 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6474 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
James Smarted957682007-06-17 19:56:37 -05006475 pring->sizeCiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006476 SLI3_IOCB_CMD_SIZE :
6477 SLI2_IOCB_CMD_SIZE;
James Smarted957682007-06-17 19:56:37 -05006478 pring->sizeRiocb = (phba->sli_rev == 3) ?
James Smart92d7f7b2007-06-17 19:56:38 -05006479 SLI3_IOCB_RSP_SIZE :
6480 SLI2_IOCB_RSP_SIZE;
dea31012005-04-17 16:05:31 -05006481 pring->fast_iotag = 0;
6482 pring->iotag_ctr = 0;
6483 pring->iotag_max = 4096;
James Smart57127f12007-10-27 13:37:05 -04006484 pring->lpfc_sli_rcv_async_status =
6485 lpfc_sli_async_event_handler;
James Smart6669f9b2009-10-02 15:16:45 -04006486 pring->num_mask = LPFC_MAX_RING_MASK;
dea31012005-04-17 16:05:31 -05006487 pring->prt[0].profile = 0; /* Mask 0 */
6488 pring->prt[0].rctl = FC_ELS_REQ;
6489 pring->prt[0].type = FC_ELS_DATA;
6490 pring->prt[0].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006491 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006492 pring->prt[1].profile = 0; /* Mask 1 */
6493 pring->prt[1].rctl = FC_ELS_RSP;
6494 pring->prt[1].type = FC_ELS_DATA;
6495 pring->prt[1].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006496 lpfc_els_unsol_event;
dea31012005-04-17 16:05:31 -05006497 pring->prt[2].profile = 0; /* Mask 2 */
6498 /* NameServer Inquiry */
6499 pring->prt[2].rctl = FC_UNSOL_CTL;
6500 /* NameServer */
6501 pring->prt[2].type = FC_COMMON_TRANSPORT_ULP;
6502 pring->prt[2].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006503 lpfc_ct_unsol_event;
dea31012005-04-17 16:05:31 -05006504 pring->prt[3].profile = 0; /* Mask 3 */
6505 /* NameServer response */
6506 pring->prt[3].rctl = FC_SOL_CTL;
6507 /* NameServer */
6508 pring->prt[3].type = FC_COMMON_TRANSPORT_ULP;
6509 pring->prt[3].lpfc_sli_rcv_unsol_event =
James Smart92d7f7b2007-06-17 19:56:38 -05006510 lpfc_ct_unsol_event;
James Smart6669f9b2009-10-02 15:16:45 -04006511 /* abort unsolicited sequence */
6512 pring->prt[4].profile = 0; /* Mask 4 */
6513 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6514 pring->prt[4].type = FC_TYPE_BLS;
6515 pring->prt[4].lpfc_sli_rcv_unsol_event =
6516 lpfc_sli4_ct_abort_unsol_event;
dea31012005-04-17 16:05:31 -05006517 break;
6518 }
James Smarted957682007-06-17 19:56:37 -05006519 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
James Smart92d7f7b2007-06-17 19:56:38 -05006520 (pring->numRiocb * pring->sizeRiocb);
dea31012005-04-17 16:05:31 -05006521 }
James Smarted957682007-06-17 19:56:37 -05006522 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea31012005-04-17 16:05:31 -05006523 /* Too many cmd / rsp ring entries in SLI2 SLIM */
James Smarte8b62012007-08-02 11:10:09 -04006524 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6525 "SLI2 SLIM Data: x%x x%lx\n",
6526 phba->brd_no, totiocbsize,
6527 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea31012005-04-17 16:05:31 -05006528 }
Jamie Wellnitzcf5bf972006-02-28 22:33:08 -05006529 if (phba->cfg_multi_ring_support == 2)
6530 lpfc_extra_ring_setup(phba);
dea31012005-04-17 16:05:31 -05006531
6532 return 0;
6533}
6534
James Smarte59058c2008-08-24 21:49:00 -04006535/**
James Smart3621a712009-04-06 18:47:14 -04006536 * lpfc_sli_queue_setup - Queue initialization function
James Smarte59058c2008-08-24 21:49:00 -04006537 * @phba: Pointer to HBA context object.
6538 *
6539 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6540 * ring. This function also initializes ring indices of each ring.
6541 * This function is called during the initialization of the SLI
6542 * interface of an HBA.
6543 * This function is called with no lock held and always returns
6544 * 1.
6545 **/
dea31012005-04-17 16:05:31 -05006546int
James Smart2e0fef82007-06-17 19:56:36 -05006547lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006548{
6549 struct lpfc_sli *psli;
6550 struct lpfc_sli_ring *pring;
James Bottomley604a3e32005-10-29 10:28:33 -05006551 int i;
dea31012005-04-17 16:05:31 -05006552
6553 psli = &phba->sli;
James Smart2e0fef82007-06-17 19:56:36 -05006554 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006555 INIT_LIST_HEAD(&psli->mboxq);
James Smart92d7f7b2007-06-17 19:56:38 -05006556 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea31012005-04-17 16:05:31 -05006557 /* Initialize list headers for txq and txcmplq as double linked lists */
6558 for (i = 0; i < psli->num_rings; i++) {
6559 pring = &psli->ring[i];
6560 pring->ringno = i;
6561 pring->next_cmdidx = 0;
6562 pring->local_getidx = 0;
6563 pring->cmdidx = 0;
6564 INIT_LIST_HEAD(&pring->txq);
6565 INIT_LIST_HEAD(&pring->txcmplq);
6566 INIT_LIST_HEAD(&pring->iocb_continueq);
James Smart9c2face2008-01-11 01:53:18 -05006567 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea31012005-04-17 16:05:31 -05006568 INIT_LIST_HEAD(&pring->postbufq);
dea31012005-04-17 16:05:31 -05006569 }
James Smart2e0fef82007-06-17 19:56:36 -05006570 spin_unlock_irq(&phba->hbalock);
6571 return 1;
dea31012005-04-17 16:05:31 -05006572}
6573
James Smarte59058c2008-08-24 21:49:00 -04006574/**
James Smart04c68492009-05-22 14:52:52 -04006575 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6576 * @phba: Pointer to HBA context object.
6577 *
6578 * This routine flushes the mailbox command subsystem. It will unconditionally
6579 * flush all the mailbox commands in the three possible stages in the mailbox
6580 * command sub-system: pending mailbox command queue; the outstanding mailbox
6581 * command; and completed mailbox command queue. It is caller's responsibility
6582 * to make sure that the driver is in the proper state to flush the mailbox
6583 * command sub-system. Namely, the posting of mailbox commands into the
6584 * pending mailbox command queue from the various clients must be stopped;
6585 * either the HBA is in a state that it will never works on the outstanding
6586 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6587 * mailbox command has been completed.
6588 **/
6589static void
6590lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6591{
6592 LIST_HEAD(completions);
6593 struct lpfc_sli *psli = &phba->sli;
6594 LPFC_MBOXQ_t *pmb;
6595 unsigned long iflag;
6596
6597 /* Flush all the mailbox commands in the mbox system */
6598 spin_lock_irqsave(&phba->hbalock, iflag);
6599 /* The pending mailbox command queue */
6600 list_splice_init(&phba->sli.mboxq, &completions);
6601 /* The outstanding active mailbox command */
6602 if (psli->mbox_active) {
6603 list_add_tail(&psli->mbox_active->list, &completions);
6604 psli->mbox_active = NULL;
6605 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6606 }
6607 /* The completed mailbox command queue */
6608 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6609 spin_unlock_irqrestore(&phba->hbalock, iflag);
6610
6611 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6612 while (!list_empty(&completions)) {
6613 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6614 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6615 if (pmb->mbox_cmpl)
6616 pmb->mbox_cmpl(phba, pmb);
6617 }
6618}
6619
6620/**
James Smart3621a712009-04-06 18:47:14 -04006621 * lpfc_sli_host_down - Vport cleanup function
James Smarte59058c2008-08-24 21:49:00 -04006622 * @vport: Pointer to virtual port object.
6623 *
6624 * lpfc_sli_host_down is called to clean up the resources
6625 * associated with a vport before destroying virtual
6626 * port data structures.
6627 * This function does following operations:
6628 * - Free discovery resources associated with this virtual
6629 * port.
6630 * - Free iocbs associated with this virtual port in
6631 * the txq.
6632 * - Send abort for all iocb commands associated with this
6633 * vport in txcmplq.
6634 *
6635 * This function is called with no lock held and always returns 1.
6636 **/
dea31012005-04-17 16:05:31 -05006637int
James Smart92d7f7b2007-06-17 19:56:38 -05006638lpfc_sli_host_down(struct lpfc_vport *vport)
6639{
James Smart858c9f62007-06-17 19:56:39 -05006640 LIST_HEAD(completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006641 struct lpfc_hba *phba = vport->phba;
6642 struct lpfc_sli *psli = &phba->sli;
6643 struct lpfc_sli_ring *pring;
6644 struct lpfc_iocbq *iocb, *next_iocb;
James Smart92d7f7b2007-06-17 19:56:38 -05006645 int i;
6646 unsigned long flags = 0;
6647 uint16_t prev_pring_flag;
6648
6649 lpfc_cleanup_discovery_resources(vport);
6650
6651 spin_lock_irqsave(&phba->hbalock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006652 for (i = 0; i < psli->num_rings; i++) {
6653 pring = &psli->ring[i];
6654 prev_pring_flag = pring->flag;
James Smart5e9d9b82008-06-14 22:52:53 -04006655 /* Only slow rings */
6656 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006657 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006658 /* Set the lpfc data pending flag */
6659 set_bit(LPFC_DATA_READY, &phba->data_flags);
6660 }
James Smart92d7f7b2007-06-17 19:56:38 -05006661 /*
6662 * Error everything on the txq since these iocbs have not been
6663 * given to the FW yet.
6664 */
James Smart92d7f7b2007-06-17 19:56:38 -05006665 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6666 if (iocb->vport != vport)
6667 continue;
James Smart858c9f62007-06-17 19:56:39 -05006668 list_move_tail(&iocb->list, &completions);
James Smart92d7f7b2007-06-17 19:56:38 -05006669 pring->txq_cnt--;
James Smart92d7f7b2007-06-17 19:56:38 -05006670 }
6671
6672 /* Next issue ABTS for everything on the txcmplq */
6673 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6674 list) {
6675 if (iocb->vport != vport)
6676 continue;
6677 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6678 }
6679
6680 pring->flag = prev_pring_flag;
6681 }
6682
6683 spin_unlock_irqrestore(&phba->hbalock, flags);
6684
James Smarta257bf92009-04-06 18:48:10 -04006685 /* Cancel all the IOCBs from the completions list */
6686 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6687 IOERR_SLI_DOWN);
James Smart92d7f7b2007-06-17 19:56:38 -05006688 return 1;
6689}
6690
James Smarte59058c2008-08-24 21:49:00 -04006691/**
James Smart3621a712009-04-06 18:47:14 -04006692 * lpfc_sli_hba_down - Resource cleanup function for the HBA
James Smarte59058c2008-08-24 21:49:00 -04006693 * @phba: Pointer to HBA context object.
6694 *
6695 * This function cleans up all iocb, buffers, mailbox commands
6696 * while shutting down the HBA. This function is called with no
6697 * lock held and always returns 1.
6698 * This function does the following to cleanup driver resources:
6699 * - Free discovery resources for each virtual port
6700 * - Cleanup any pending fabric iocbs
6701 * - Iterate through the iocb txq and free each entry
6702 * in the list.
6703 * - Free up any buffer posted to the HBA
6704 * - Free mailbox commands in the mailbox queue.
6705 **/
James Smart92d7f7b2007-06-17 19:56:38 -05006706int
James Smart2e0fef82007-06-17 19:56:36 -05006707lpfc_sli_hba_down(struct lpfc_hba *phba)
dea31012005-04-17 16:05:31 -05006708{
James Smart2534ba72007-04-25 09:52:20 -04006709 LIST_HEAD(completions);
James Smart2e0fef82007-06-17 19:56:36 -05006710 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -05006711 struct lpfc_sli_ring *pring;
James Smart0ff10d42008-01-11 01:52:36 -05006712 struct lpfc_dmabuf *buf_ptr;
dea31012005-04-17 16:05:31 -05006713 unsigned long flags = 0;
James Smart04c68492009-05-22 14:52:52 -04006714 int i;
6715
6716 /* Shutdown the mailbox command sub-system */
6717 lpfc_sli_mbox_sys_shutdown(phba);
dea31012005-04-17 16:05:31 -05006718
dea31012005-04-17 16:05:31 -05006719 lpfc_hba_down_prep(phba);
6720
James Smart92d7f7b2007-06-17 19:56:38 -05006721 lpfc_fabric_abort_hba(phba);
6722
James Smart2e0fef82007-06-17 19:56:36 -05006723 spin_lock_irqsave(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006724 for (i = 0; i < psli->num_rings; i++) {
6725 pring = &psli->ring[i];
James Smart5e9d9b82008-06-14 22:52:53 -04006726 /* Only slow rings */
6727 if (pring->ringno == LPFC_ELS_RING) {
James Smart858c9f62007-06-17 19:56:39 -05006728 pring->flag |= LPFC_DEFERRED_RING_EVENT;
James Smart5e9d9b82008-06-14 22:52:53 -04006729 /* Set the lpfc data pending flag */
6730 set_bit(LPFC_DATA_READY, &phba->data_flags);
6731 }
dea31012005-04-17 16:05:31 -05006732
6733 /*
6734 * Error everything on the txq since these iocbs have not been
6735 * given to the FW yet.
6736 */
James Smart2534ba72007-04-25 09:52:20 -04006737 list_splice_init(&pring->txq, &completions);
dea31012005-04-17 16:05:31 -05006738 pring->txq_cnt = 0;
6739
dea31012005-04-17 16:05:31 -05006740 }
James Smart2e0fef82007-06-17 19:56:36 -05006741 spin_unlock_irqrestore(&phba->hbalock, flags);
dea31012005-04-17 16:05:31 -05006742
James Smarta257bf92009-04-06 18:48:10 -04006743 /* Cancel all the IOCBs from the completions list */
6744 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6745 IOERR_SLI_DOWN);
James Smart2534ba72007-04-25 09:52:20 -04006746
James Smart0ff10d42008-01-11 01:52:36 -05006747 spin_lock_irqsave(&phba->hbalock, flags);
6748 list_splice_init(&phba->elsbuf, &completions);
6749 phba->elsbuf_cnt = 0;
6750 phba->elsbuf_prev_cnt = 0;
6751 spin_unlock_irqrestore(&phba->hbalock, flags);
6752
6753 while (!list_empty(&completions)) {
6754 list_remove_head(&completions, buf_ptr,
6755 struct lpfc_dmabuf, list);
6756 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6757 kfree(buf_ptr);
6758 }
6759
dea31012005-04-17 16:05:31 -05006760 /* Return any active mbox cmds */
6761 del_timer_sync(&psli->mbox_tmo);
James Smart92d7f7b2007-06-17 19:56:38 -05006762
James Smartda0436e2009-05-22 14:51:39 -04006763 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006764 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
James Smartda0436e2009-05-22 14:51:39 -04006765 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
James Smart92d7f7b2007-06-17 19:56:38 -05006766
James Smartda0436e2009-05-22 14:51:39 -04006767 return 1;
6768}
James Smart92d7f7b2007-06-17 19:56:38 -05006769
James Smartda0436e2009-05-22 14:51:39 -04006770/**
6771 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6772 * @phba: Pointer to HBA context object.
6773 *
6774 * This function cleans up all queues, iocb, buffers, mailbox commands while
6775 * shutting down the SLI4 HBA FCoE function. This function is called with no
6776 * lock held and always returns 1.
6777 *
6778 * This function does the following to cleanup driver FCoE function resources:
6779 * - Free discovery resources for each virtual port
6780 * - Cleanup any pending fabric iocbs
6781 * - Iterate through the iocb txq and free each entry in the list.
6782 * - Free up any buffer posted to the HBA.
6783 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6784 * - Free mailbox commands in the mailbox queue.
6785 **/
6786int
6787lpfc_sli4_hba_down(struct lpfc_hba *phba)
6788{
6789 /* Stop the SLI4 device port */
6790 lpfc_stop_port(phba);
6791
6792 /* Tear down the queues in the HBA */
6793 lpfc_sli4_queue_unset(phba);
6794
6795 /* unregister default FCFI from the HBA */
6796 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
6797
dea31012005-04-17 16:05:31 -05006798 return 1;
6799}
6800
James Smarte59058c2008-08-24 21:49:00 -04006801/**
James Smart3621a712009-04-06 18:47:14 -04006802 * lpfc_sli_pcimem_bcopy - SLI memory copy function
James Smarte59058c2008-08-24 21:49:00 -04006803 * @srcp: Source memory pointer.
6804 * @destp: Destination memory pointer.
6805 * @cnt: Number of words required to be copied.
6806 *
6807 * This function is used for copying data between driver memory
6808 * and the SLI memory. This function also changes the endianness
6809 * of each word if native endianness is different from SLI
6810 * endianness. This function can be called with or without
6811 * lock.
6812 **/
dea31012005-04-17 16:05:31 -05006813void
6814lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6815{
6816 uint32_t *src = srcp;
6817 uint32_t *dest = destp;
6818 uint32_t ldata;
6819 int i;
6820
6821 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6822 ldata = *src;
6823 ldata = le32_to_cpu(ldata);
6824 *dest = ldata;
6825 src++;
6826 dest++;
6827 }
6828}
6829
James Smarte59058c2008-08-24 21:49:00 -04006830
6831/**
James Smarta0c87cb2009-07-19 10:01:10 -04006832 * lpfc_sli_bemem_bcopy - SLI memory copy function
6833 * @srcp: Source memory pointer.
6834 * @destp: Destination memory pointer.
6835 * @cnt: Number of words required to be copied.
6836 *
6837 * This function is used for copying data between a data structure
6838 * with big endian representation to local endianness.
6839 * This function can be called with or without lock.
6840 **/
6841void
6842lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6843{
6844 uint32_t *src = srcp;
6845 uint32_t *dest = destp;
6846 uint32_t ldata;
6847 int i;
6848
6849 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6850 ldata = *src;
6851 ldata = be32_to_cpu(ldata);
6852 *dest = ldata;
6853 src++;
6854 dest++;
6855 }
6856}
6857
6858/**
James Smart3621a712009-04-06 18:47:14 -04006859 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
James Smarte59058c2008-08-24 21:49:00 -04006860 * @phba: Pointer to HBA context object.
6861 * @pring: Pointer to driver SLI ring object.
6862 * @mp: Pointer to driver buffer object.
6863 *
6864 * This function is called with no lock held.
6865 * It always return zero after adding the buffer to the postbufq
6866 * buffer list.
6867 **/
dea31012005-04-17 16:05:31 -05006868int
James Smart2e0fef82007-06-17 19:56:36 -05006869lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6870 struct lpfc_dmabuf *mp)
dea31012005-04-17 16:05:31 -05006871{
6872 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6873 later */
James Smart2e0fef82007-06-17 19:56:36 -05006874 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006875 list_add_tail(&mp->list, &pring->postbufq);
dea31012005-04-17 16:05:31 -05006876 pring->postbufq_cnt++;
James Smart2e0fef82007-06-17 19:56:36 -05006877 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006878 return 0;
6879}
6880
James Smarte59058c2008-08-24 21:49:00 -04006881/**
James Smart3621a712009-04-06 18:47:14 -04006882 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
James Smarte59058c2008-08-24 21:49:00 -04006883 * @phba: Pointer to HBA context object.
6884 *
6885 * When HBQ is enabled, buffers are searched based on tags. This function
6886 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6887 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6888 * does not conflict with tags of buffer posted for unsolicited events.
6889 * The function returns the allocated tag. The function is called with
6890 * no locks held.
6891 **/
James Smart76bb24e2007-10-27 13:38:00 -04006892uint32_t
6893lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6894{
6895 spin_lock_irq(&phba->hbalock);
6896 phba->buffer_tag_count++;
6897 /*
6898 * Always set the QUE_BUFTAG_BIT to distiguish between
6899 * a tag assigned by HBQ.
6900 */
6901 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6902 spin_unlock_irq(&phba->hbalock);
6903 return phba->buffer_tag_count;
6904}
6905
James Smarte59058c2008-08-24 21:49:00 -04006906/**
James Smart3621a712009-04-06 18:47:14 -04006907 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
James Smarte59058c2008-08-24 21:49:00 -04006908 * @phba: Pointer to HBA context object.
6909 * @pring: Pointer to driver SLI ring object.
6910 * @tag: Buffer tag.
6911 *
6912 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6913 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6914 * iocb is posted to the response ring with the tag of the buffer.
6915 * This function searches the pring->postbufq list using the tag
6916 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6917 * iocb. If the buffer is found then lpfc_dmabuf object of the
6918 * buffer is returned to the caller else NULL is returned.
6919 * This function is called with no lock held.
6920 **/
James Smart76bb24e2007-10-27 13:38:00 -04006921struct lpfc_dmabuf *
6922lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6923 uint32_t tag)
6924{
6925 struct lpfc_dmabuf *mp, *next_mp;
6926 struct list_head *slp = &pring->postbufq;
6927
6928 /* Search postbufq, from the begining, looking for a match on tag */
6929 spin_lock_irq(&phba->hbalock);
6930 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6931 if (mp->buffer_tag == tag) {
6932 list_del_init(&mp->list);
6933 pring->postbufq_cnt--;
6934 spin_unlock_irq(&phba->hbalock);
6935 return mp;
6936 }
6937 }
6938
6939 spin_unlock_irq(&phba->hbalock);
6940 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smartd7c255b2008-08-24 21:50:00 -04006941 "0402 Cannot find virtual addr for buffer tag on "
James Smart76bb24e2007-10-27 13:38:00 -04006942 "ring %d Data x%lx x%p x%p x%x\n",
6943 pring->ringno, (unsigned long) tag,
6944 slp->next, slp->prev, pring->postbufq_cnt);
6945
6946 return NULL;
6947}
dea31012005-04-17 16:05:31 -05006948
James Smarte59058c2008-08-24 21:49:00 -04006949/**
James Smart3621a712009-04-06 18:47:14 -04006950 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
James Smarte59058c2008-08-24 21:49:00 -04006951 * @phba: Pointer to HBA context object.
6952 * @pring: Pointer to driver SLI ring object.
6953 * @phys: DMA address of the buffer.
6954 *
6955 * This function searches the buffer list using the dma_address
6956 * of unsolicited event to find the driver's lpfc_dmabuf object
6957 * corresponding to the dma_address. The function returns the
6958 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6959 * This function is called by the ct and els unsolicited event
6960 * handlers to get the buffer associated with the unsolicited
6961 * event.
6962 *
6963 * This function is called with no lock held.
6964 **/
dea31012005-04-17 16:05:31 -05006965struct lpfc_dmabuf *
6966lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6967 dma_addr_t phys)
6968{
6969 struct lpfc_dmabuf *mp, *next_mp;
6970 struct list_head *slp = &pring->postbufq;
6971
6972 /* Search postbufq, from the begining, looking for a match on phys */
James Smart2e0fef82007-06-17 19:56:36 -05006973 spin_lock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006974 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6975 if (mp->phys == phys) {
6976 list_del_init(&mp->list);
6977 pring->postbufq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05006978 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006979 return mp;
6980 }
6981 }
6982
James Smart2e0fef82007-06-17 19:56:36 -05006983 spin_unlock_irq(&phba->hbalock);
dea31012005-04-17 16:05:31 -05006984 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
James Smarte8b62012007-08-02 11:10:09 -04006985 "0410 Cannot find virtual addr for mapped buf on "
dea31012005-04-17 16:05:31 -05006986 "ring %d Data x%llx x%p x%p x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04006987 pring->ringno, (unsigned long long)phys,
dea31012005-04-17 16:05:31 -05006988 slp->next, slp->prev, pring->postbufq_cnt);
6989 return NULL;
6990}
6991
James Smarte59058c2008-08-24 21:49:00 -04006992/**
James Smart3621a712009-04-06 18:47:14 -04006993 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
James Smarte59058c2008-08-24 21:49:00 -04006994 * @phba: Pointer to HBA context object.
6995 * @cmdiocb: Pointer to driver command iocb object.
6996 * @rspiocb: Pointer to driver response iocb object.
6997 *
6998 * This function is the completion handler for the abort iocbs for
6999 * ELS commands. This function is called from the ELS ring event
7000 * handler with no lock held. This function frees memory resources
7001 * associated with the abort iocb.
7002 **/
dea31012005-04-17 16:05:31 -05007003static void
James Smart2e0fef82007-06-17 19:56:36 -05007004lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7005 struct lpfc_iocbq *rspiocb)
dea31012005-04-17 16:05:31 -05007006{
James Smart2e0fef82007-06-17 19:56:36 -05007007 IOCB_t *irsp = &rspiocb->iocb;
James Smart2680eea2007-04-25 09:52:55 -04007008 uint16_t abort_iotag, abort_context;
James Smart92d7f7b2007-06-17 19:56:38 -05007009 struct lpfc_iocbq *abort_iocb;
James Smart2680eea2007-04-25 09:52:55 -04007010 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7011
7012 abort_iocb = NULL;
James Smart2680eea2007-04-25 09:52:55 -04007013
7014 if (irsp->ulpStatus) {
7015 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7016 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7017
James Smart2e0fef82007-06-17 19:56:36 -05007018 spin_lock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007019 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
7020 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
7021
James Smart92d7f7b2007-06-17 19:56:38 -05007022 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007023 "0327 Cannot abort els iocb %p "
James Smart92d7f7b2007-06-17 19:56:38 -05007024 "with tag %x context %x, abort status %x, "
7025 "abort code %x\n",
James Smarte8b62012007-08-02 11:10:09 -04007026 abort_iocb, abort_iotag, abort_context,
7027 irsp->ulpStatus, irsp->un.ulpWord[4]);
James Smart2680eea2007-04-25 09:52:55 -04007028
7029 /*
James Smart58da1ff2008-04-07 10:15:56 -04007030 * If the iocb is not found in Firmware queue the iocb
7031 * might have completed already. Do not free it again.
7032 */
James Smart9b379602008-04-07 10:16:00 -04007033 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
James Smart58da1ff2008-04-07 10:15:56 -04007034 spin_unlock_irq(&phba->hbalock);
7035 lpfc_sli_release_iocbq(phba, cmdiocb);
7036 return;
7037 }
7038 /*
James Smart2680eea2007-04-25 09:52:55 -04007039 * make sure we have the right iocbq before taking it
7040 * off the txcmplq and try to call completion routine.
7041 */
James Smart2e0fef82007-06-17 19:56:36 -05007042 if (!abort_iocb ||
7043 abort_iocb->iocb.ulpContext != abort_context ||
7044 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7045 spin_unlock_irq(&phba->hbalock);
7046 else {
James Smart92d7f7b2007-06-17 19:56:38 -05007047 list_del_init(&abort_iocb->list);
James Smart2680eea2007-04-25 09:52:55 -04007048 pring->txcmplq_cnt--;
James Smart2e0fef82007-06-17 19:56:36 -05007049 spin_unlock_irq(&phba->hbalock);
James Smart2680eea2007-04-25 09:52:55 -04007050
James Smart0ff10d42008-01-11 01:52:36 -05007051 /* Firmware could still be in progress of DMAing
7052 * payload, so don't free data buffer till after
7053 * a hbeat.
7054 */
7055 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7056
James Smart92d7f7b2007-06-17 19:56:38 -05007057 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7058 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7059 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
7060 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
James Smart2680eea2007-04-25 09:52:55 -04007061 }
7062 }
7063
James Bottomley604a3e32005-10-29 10:28:33 -05007064 lpfc_sli_release_iocbq(phba, cmdiocb);
dea31012005-04-17 16:05:31 -05007065 return;
7066}
7067
James Smarte59058c2008-08-24 21:49:00 -04007068/**
James Smart3621a712009-04-06 18:47:14 -04007069 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
James Smarte59058c2008-08-24 21:49:00 -04007070 * @phba: Pointer to HBA context object.
7071 * @cmdiocb: Pointer to driver command iocb object.
7072 * @rspiocb: Pointer to driver response iocb object.
7073 *
7074 * The function is called from SLI ring event handler with no
7075 * lock held. This function is the completion handler for ELS commands
7076 * which are aborted. The function frees memory resources used for
7077 * the aborted ELS commands.
7078 **/
James Smart92d7f7b2007-06-17 19:56:38 -05007079static void
7080lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7081 struct lpfc_iocbq *rspiocb)
7082{
7083 IOCB_t *irsp = &rspiocb->iocb;
7084
7085 /* ELS cmd tag <ulpIoTag> completes */
7086 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
James Smartd7c255b2008-08-24 21:50:00 -04007087 "0139 Ignoring ELS cmd tag x%x completion Data: "
James Smart92d7f7b2007-06-17 19:56:38 -05007088 "x%x x%x x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007089 irsp->ulpIoTag, irsp->ulpStatus,
James Smart92d7f7b2007-06-17 19:56:38 -05007090 irsp->un.ulpWord[4], irsp->ulpTimeout);
James Smart858c9f62007-06-17 19:56:39 -05007091 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7092 lpfc_ct_free_iocb(phba, cmdiocb);
7093 else
7094 lpfc_els_free_iocb(phba, cmdiocb);
James Smart92d7f7b2007-06-17 19:56:38 -05007095 return;
7096}
7097
James Smarte59058c2008-08-24 21:49:00 -04007098/**
James Smart3621a712009-04-06 18:47:14 -04007099 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
James Smarte59058c2008-08-24 21:49:00 -04007100 * @phba: Pointer to HBA context object.
7101 * @pring: Pointer to driver SLI ring object.
7102 * @cmdiocb: Pointer to driver command iocb object.
7103 *
7104 * This function issues an abort iocb for the provided command
7105 * iocb. This function is called with hbalock held.
7106 * The function returns 0 when it fails due to memory allocation
7107 * failure or when the command iocb is an abort request.
7108 **/
dea31012005-04-17 16:05:31 -05007109int
James Smart2e0fef82007-06-17 19:56:36 -05007110lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7111 struct lpfc_iocbq *cmdiocb)
dea31012005-04-17 16:05:31 -05007112{
James Smart2e0fef82007-06-17 19:56:36 -05007113 struct lpfc_vport *vport = cmdiocb->vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007114 struct lpfc_iocbq *abtsiocbp;
dea31012005-04-17 16:05:31 -05007115 IOCB_t *icmd = NULL;
7116 IOCB_t *iabt = NULL;
James Smart07951072007-04-25 09:51:38 -04007117 int retval = IOCB_ERROR;
7118
James Smart92d7f7b2007-06-17 19:56:38 -05007119 /*
7120 * There are certain command types we don't want to abort. And we
7121 * don't want to abort commands that are already in the process of
7122 * being aborted.
James Smart07951072007-04-25 09:51:38 -04007123 */
7124 icmd = &cmdiocb->iocb;
James Smart2e0fef82007-06-17 19:56:36 -05007125 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
James Smart92d7f7b2007-06-17 19:56:38 -05007126 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7127 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
James Smart07951072007-04-25 09:51:38 -04007128 return 0;
7129
James Smart858c9f62007-06-17 19:56:39 -05007130 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7131 * callback so that nothing happens when it finishes.
James Smart07951072007-04-25 09:51:38 -04007132 */
James Smart858c9f62007-06-17 19:56:39 -05007133 if ((vport->load_flag & FC_UNLOADING) &&
7134 (pring->ringno == LPFC_ELS_RING)) {
James Smart92d7f7b2007-06-17 19:56:38 -05007135 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7136 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7137 else
7138 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
James Smart07951072007-04-25 09:51:38 -04007139 goto abort_iotag_exit;
James Smart92d7f7b2007-06-17 19:56:38 -05007140 }
dea31012005-04-17 16:05:31 -05007141
7142 /* issue ABTS for this IOCB based on iotag */
James Smart92d7f7b2007-06-17 19:56:38 -05007143 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007144 if (abtsiocbp == NULL)
7145 return 0;
dea31012005-04-17 16:05:31 -05007146
James Smart07951072007-04-25 09:51:38 -04007147 /* This signals the response to set the correct status
7148 * before calling the completion handler.
7149 */
7150 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7151
dea31012005-04-17 16:05:31 -05007152 iabt = &abtsiocbp->iocb;
James Smart07951072007-04-25 09:51:38 -04007153 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7154 iabt->un.acxri.abortContextTag = icmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007155 if (phba->sli_rev == LPFC_SLI_REV4)
7156 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7157 else
7158 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007159 iabt->ulpLe = 1;
James Smart07951072007-04-25 09:51:38 -04007160 iabt->ulpClass = icmd->ulpClass;
dea31012005-04-17 16:05:31 -05007161
James Smart2e0fef82007-06-17 19:56:36 -05007162 if (phba->link_state >= LPFC_LINK_UP)
James Smart07951072007-04-25 09:51:38 -04007163 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7164 else
7165 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
7166
7167 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
James Smart5b8bd0c2007-04-25 09:52:49 -04007168
James Smarte8b62012007-08-02 11:10:09 -04007169 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7170 "0339 Abort xri x%x, original iotag x%x, "
7171 "abort cmd iotag x%x\n",
7172 iabt->un.acxri.abortContextTag,
7173 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
James Smartda0436e2009-05-22 14:51:39 -04007174 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
James Smart07951072007-04-25 09:51:38 -04007175
James Smartd7c255b2008-08-24 21:50:00 -04007176 if (retval)
7177 __lpfc_sli_release_iocbq(phba, abtsiocbp);
James Smart07951072007-04-25 09:51:38 -04007178abort_iotag_exit:
James Smart2e0fef82007-06-17 19:56:36 -05007179 /*
7180 * Caller to this routine should check for IOCB_ERROR
7181 * and handle it properly. This routine no longer removes
7182 * iocb off txcmplq and call compl in case of IOCB_ERROR.
James Smart07951072007-04-25 09:51:38 -04007183 */
James Smart2e0fef82007-06-17 19:56:36 -05007184 return retval;
dea31012005-04-17 16:05:31 -05007185}
7186
James Smarte59058c2008-08-24 21:49:00 -04007187/**
James Smart3621a712009-04-06 18:47:14 -04007188 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
James Smarte59058c2008-08-24 21:49:00 -04007189 * @iocbq: Pointer to driver iocb object.
7190 * @vport: Pointer to driver virtual port object.
7191 * @tgt_id: SCSI ID of the target.
7192 * @lun_id: LUN ID of the scsi device.
7193 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7194 *
James Smart3621a712009-04-06 18:47:14 -04007195 * This function acts as an iocb filter for functions which abort or count
James Smarte59058c2008-08-24 21:49:00 -04007196 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7197 * 0 if the filtering criteria is met for the given iocb and will return
7198 * 1 if the filtering criteria is not met.
7199 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7200 * given iocb is for the SCSI device specified by vport, tgt_id and
7201 * lun_id parameter.
7202 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7203 * given iocb is for the SCSI target specified by vport and tgt_id
7204 * parameters.
7205 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7206 * given iocb is for the SCSI host associated with the given vport.
7207 * This function is called with no locks held.
7208 **/
dea31012005-04-17 16:05:31 -05007209static int
James Smart51ef4c22007-08-02 11:10:31 -04007210lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7211 uint16_t tgt_id, uint64_t lun_id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007212 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007213{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007214 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05007215 int rc = 1;
7216
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007217 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7218 return rc;
7219
James Smart51ef4c22007-08-02 11:10:31 -04007220 if (iocbq->vport != vport)
7221 return rc;
7222
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007223 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007224
James Smart495a7142008-06-14 22:52:59 -04007225 if (lpfc_cmd->pCmd == NULL)
dea31012005-04-17 16:05:31 -05007226 return rc;
7227
7228 switch (ctx_cmd) {
7229 case LPFC_CTX_LUN:
James Smart495a7142008-06-14 22:52:59 -04007230 if ((lpfc_cmd->rdata->pnode) &&
7231 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7232 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea31012005-04-17 16:05:31 -05007233 rc = 0;
7234 break;
7235 case LPFC_CTX_TGT:
James Smart495a7142008-06-14 22:52:59 -04007236 if ((lpfc_cmd->rdata->pnode) &&
7237 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea31012005-04-17 16:05:31 -05007238 rc = 0;
7239 break;
dea31012005-04-17 16:05:31 -05007240 case LPFC_CTX_HOST:
7241 rc = 0;
7242 break;
7243 default:
7244 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
Harvey Harrisoncadbd4a2008-07-03 23:47:27 -07007245 __func__, ctx_cmd);
dea31012005-04-17 16:05:31 -05007246 break;
7247 }
7248
7249 return rc;
7250}
7251
James Smarte59058c2008-08-24 21:49:00 -04007252/**
James Smart3621a712009-04-06 18:47:14 -04007253 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
James Smarte59058c2008-08-24 21:49:00 -04007254 * @vport: Pointer to virtual port.
7255 * @tgt_id: SCSI ID of the target.
7256 * @lun_id: LUN ID of the scsi device.
7257 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7258 *
7259 * This function returns number of FCP commands pending for the vport.
7260 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7261 * commands pending on the vport associated with SCSI device specified
7262 * by tgt_id and lun_id parameters.
7263 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7264 * commands pending on the vport associated with SCSI target specified
7265 * by tgt_id parameter.
7266 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7267 * commands pending on the vport.
7268 * This function returns the number of iocbs which satisfy the filter.
7269 * This function is called without any lock held.
7270 **/
dea31012005-04-17 16:05:31 -05007271int
James Smart51ef4c22007-08-02 11:10:31 -04007272lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7273 lpfc_ctx_cmd ctx_cmd)
dea31012005-04-17 16:05:31 -05007274{
James Smart51ef4c22007-08-02 11:10:31 -04007275 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007276 struct lpfc_iocbq *iocbq;
7277 int sum, i;
dea31012005-04-17 16:05:31 -05007278
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007279 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7280 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007281
James Smart51ef4c22007-08-02 11:10:31 -04007282 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7283 ctx_cmd) == 0)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007284 sum++;
dea31012005-04-17 16:05:31 -05007285 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007286
dea31012005-04-17 16:05:31 -05007287 return sum;
7288}
7289
James Smarte59058c2008-08-24 21:49:00 -04007290/**
James Smart3621a712009-04-06 18:47:14 -04007291 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
James Smarte59058c2008-08-24 21:49:00 -04007292 * @phba: Pointer to HBA context object
7293 * @cmdiocb: Pointer to command iocb object.
7294 * @rspiocb: Pointer to response iocb object.
7295 *
7296 * This function is called when an aborted FCP iocb completes. This
7297 * function is called by the ring event handler with no lock held.
7298 * This function frees the iocb.
7299 **/
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007300void
James Smart2e0fef82007-06-17 19:56:36 -05007301lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7302 struct lpfc_iocbq *rspiocb)
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007303{
James Bottomley604a3e32005-10-29 10:28:33 -05007304 lpfc_sli_release_iocbq(phba, cmdiocb);
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007305 return;
7306}
7307
James Smarte59058c2008-08-24 21:49:00 -04007308/**
James Smart3621a712009-04-06 18:47:14 -04007309 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
James Smarte59058c2008-08-24 21:49:00 -04007310 * @vport: Pointer to virtual port.
7311 * @pring: Pointer to driver SLI ring object.
7312 * @tgt_id: SCSI ID of the target.
7313 * @lun_id: LUN ID of the scsi device.
7314 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7315 *
7316 * This function sends an abort command for every SCSI command
7317 * associated with the given virtual port pending on the ring
7318 * filtered by lpfc_sli_validate_fcp_iocb function.
7319 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7320 * FCP iocbs associated with lun specified by tgt_id and lun_id
7321 * parameters
7322 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7323 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7324 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7325 * FCP iocbs associated with virtual port.
7326 * This function returns number of iocbs it failed to abort.
7327 * This function is called with no locks held.
7328 **/
dea31012005-04-17 16:05:31 -05007329int
James Smart51ef4c22007-08-02 11:10:31 -04007330lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7331 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea31012005-04-17 16:05:31 -05007332{
James Smart51ef4c22007-08-02 11:10:31 -04007333 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007334 struct lpfc_iocbq *iocbq;
7335 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05007336 IOCB_t *cmd = NULL;
dea31012005-04-17 16:05:31 -05007337 int errcnt = 0, ret_val = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007338 int i;
dea31012005-04-17 16:05:31 -05007339
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007340 for (i = 1; i <= phba->sli.last_iotag; i++) {
7341 iocbq = phba->sli.iocbq_lookup[i];
dea31012005-04-17 16:05:31 -05007342
James Smart51ef4c22007-08-02 11:10:31 -04007343 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
James Smart2e0fef82007-06-17 19:56:36 -05007344 abort_cmd) != 0)
dea31012005-04-17 16:05:31 -05007345 continue;
7346
7347 /* issue ABTS for this IOCB based on iotag */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007348 abtsiocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05007349 if (abtsiocb == NULL) {
7350 errcnt++;
7351 continue;
7352 }
dea31012005-04-17 16:05:31 -05007353
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04007354 cmd = &iocbq->iocb;
dea31012005-04-17 16:05:31 -05007355 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7356 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
James Smartda0436e2009-05-22 14:51:39 -04007357 if (phba->sli_rev == LPFC_SLI_REV4)
7358 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7359 else
7360 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05007361 abtsiocb->iocb.ulpLe = 1;
7362 abtsiocb->iocb.ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05007363 abtsiocb->vport = phba->pport;
dea31012005-04-17 16:05:31 -05007364
James Smart2e0fef82007-06-17 19:56:36 -05007365 if (lpfc_is_link_up(phba))
dea31012005-04-17 16:05:31 -05007366 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7367 else
7368 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7369
James.Smart@Emulex.Com5eb95af2005-06-25 10:34:30 -04007370 /* Setup callback routine and issue the command. */
7371 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smartda0436e2009-05-22 14:51:39 -04007372 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7373 abtsiocb, 0);
dea31012005-04-17 16:05:31 -05007374 if (ret_val == IOCB_ERROR) {
James Bottomley604a3e32005-10-29 10:28:33 -05007375 lpfc_sli_release_iocbq(phba, abtsiocb);
dea31012005-04-17 16:05:31 -05007376 errcnt++;
7377 continue;
7378 }
7379 }
7380
7381 return errcnt;
7382}
7383
James Smarte59058c2008-08-24 21:49:00 -04007384/**
James Smart3621a712009-04-06 18:47:14 -04007385 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
James Smarte59058c2008-08-24 21:49:00 -04007386 * @phba: Pointer to HBA context object.
7387 * @cmdiocbq: Pointer to command iocb.
7388 * @rspiocbq: Pointer to response iocb.
7389 *
7390 * This function is the completion handler for iocbs issued using
7391 * lpfc_sli_issue_iocb_wait function. This function is called by the
7392 * ring event handler function without any lock held. This function
7393 * can be called from both worker thread context and interrupt
7394 * context. This function also can be called from other thread which
7395 * cleans up the SLI layer objects.
7396 * This function copy the contents of the response iocb to the
7397 * response iocb memory object provided by the caller of
7398 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7399 * sleeps for the iocb completion.
7400 **/
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007401static void
7402lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7403 struct lpfc_iocbq *cmdiocbq,
7404 struct lpfc_iocbq *rspiocbq)
dea31012005-04-17 16:05:31 -05007405{
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007406 wait_queue_head_t *pdone_q;
7407 unsigned long iflags;
dea31012005-04-17 16:05:31 -05007408
James Smart2e0fef82007-06-17 19:56:36 -05007409 spin_lock_irqsave(&phba->hbalock, iflags);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007410 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7411 if (cmdiocbq->context2 && rspiocbq)
7412 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7413 &rspiocbq->iocb, sizeof(IOCB_t));
7414
7415 pdone_q = cmdiocbq->context_un.wait_queue;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007416 if (pdone_q)
7417 wake_up(pdone_q);
James Smart858c9f62007-06-17 19:56:39 -05007418 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea31012005-04-17 16:05:31 -05007419 return;
7420}
7421
James Smarte59058c2008-08-24 21:49:00 -04007422/**
James Smartd11e31d2009-06-10 17:23:06 -04007423 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7424 * @phba: Pointer to HBA context object..
7425 * @piocbq: Pointer to command iocb.
7426 * @flag: Flag to test.
7427 *
7428 * This routine grabs the hbalock and then test the iocb_flag to
7429 * see if the passed in flag is set.
7430 * Returns:
7431 * 1 if flag is set.
7432 * 0 if flag is not set.
7433 **/
7434static int
7435lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7436 struct lpfc_iocbq *piocbq, uint32_t flag)
7437{
7438 unsigned long iflags;
7439 int ret;
7440
7441 spin_lock_irqsave(&phba->hbalock, iflags);
7442 ret = piocbq->iocb_flag & flag;
7443 spin_unlock_irqrestore(&phba->hbalock, iflags);
7444 return ret;
7445
7446}
7447
7448/**
James Smart3621a712009-04-06 18:47:14 -04007449 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
James Smarte59058c2008-08-24 21:49:00 -04007450 * @phba: Pointer to HBA context object..
7451 * @pring: Pointer to sli ring.
7452 * @piocb: Pointer to command iocb.
7453 * @prspiocbq: Pointer to response iocb.
7454 * @timeout: Timeout in number of seconds.
7455 *
7456 * This function issues the iocb to firmware and waits for the
7457 * iocb to complete. If the iocb command is not
7458 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7459 * Caller should not free the iocb resources if this function
7460 * returns IOCB_TIMEDOUT.
7461 * The function waits for the iocb completion using an
7462 * non-interruptible wait.
7463 * This function will sleep while waiting for iocb completion.
7464 * So, this function should not be called from any context which
7465 * does not allow sleeping. Due to the same reason, this function
7466 * cannot be called with interrupt disabled.
7467 * This function assumes that the iocb completions occur while
7468 * this function sleep. So, this function cannot be called from
7469 * the thread which process iocb completion for this ring.
7470 * This function clears the iocb_flag of the iocb object before
7471 * issuing the iocb and the iocb completion handler sets this
7472 * flag and wakes this thread when the iocb completes.
7473 * The contents of the response iocb will be copied to prspiocbq
7474 * by the completion handler when the command completes.
7475 * This function returns IOCB_SUCCESS when success.
7476 * This function is called with no lock held.
7477 **/
dea31012005-04-17 16:05:31 -05007478int
James Smart2e0fef82007-06-17 19:56:36 -05007479lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
James Smartda0436e2009-05-22 14:51:39 -04007480 uint32_t ring_number,
James Smart2e0fef82007-06-17 19:56:36 -05007481 struct lpfc_iocbq *piocb,
7482 struct lpfc_iocbq *prspiocbq,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007483 uint32_t timeout)
dea31012005-04-17 16:05:31 -05007484{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007485 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007486 long timeleft, timeout_req = 0;
7487 int retval = IOCB_SUCCESS;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007488 uint32_t creg_val;
dea31012005-04-17 16:05:31 -05007489
7490 /*
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007491 * If the caller has provided a response iocbq buffer, then context2
7492 * is NULL or its an error.
dea31012005-04-17 16:05:31 -05007493 */
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007494 if (prspiocbq) {
7495 if (piocb->context2)
7496 return IOCB_ERROR;
7497 piocb->context2 = prspiocbq;
dea31012005-04-17 16:05:31 -05007498 }
7499
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007500 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7501 piocb->context_un.wait_queue = &done_q;
7502 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea31012005-04-17 16:05:31 -05007503
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007504 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7505 creg_val = readl(phba->HCregaddr);
7506 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7507 writel(creg_val, phba->HCregaddr);
7508 readl(phba->HCregaddr); /* flush */
7509 }
7510
James Smartda0436e2009-05-22 14:51:39 -04007511 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007512 if (retval == IOCB_SUCCESS) {
7513 timeout_req = timeout * HZ;
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007514 timeleft = wait_event_timeout(done_q,
James Smartd11e31d2009-06-10 17:23:06 -04007515 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007516 timeout_req);
dea31012005-04-17 16:05:31 -05007517
James Smart7054a602007-04-25 09:52:34 -04007518 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7519 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007520 "0331 IOCB wake signaled\n");
James Smart7054a602007-04-25 09:52:34 -04007521 } else if (timeleft == 0) {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007522 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007523 "0338 IOCB wait timeout error - no "
7524 "wake response Data x%x\n", timeout);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007525 retval = IOCB_TIMEDOUT;
James Smart7054a602007-04-25 09:52:34 -04007526 } else {
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007527 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04007528 "0330 IOCB wake NOT set, "
7529 "Data x%x x%lx\n",
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007530 timeout, (timeleft / jiffies));
7531 retval = IOCB_TIMEDOUT;
dea31012005-04-17 16:05:31 -05007532 }
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007533 } else {
7534 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04007535 "0332 IOCB wait issue failed, Data x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04007536 retval);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007537 retval = IOCB_ERROR;
dea31012005-04-17 16:05:31 -05007538 }
7539
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05007540 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7541 creg_val = readl(phba->HCregaddr);
7542 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7543 writel(creg_val, phba->HCregaddr);
7544 readl(phba->HCregaddr); /* flush */
7545 }
7546
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007547 if (prspiocbq)
7548 piocb->context2 = NULL;
7549
7550 piocb->context_un.wait_queue = NULL;
7551 piocb->iocb_cmpl = NULL;
dea31012005-04-17 16:05:31 -05007552 return retval;
7553}
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04007554
James Smarte59058c2008-08-24 21:49:00 -04007555/**
James Smart3621a712009-04-06 18:47:14 -04007556 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
James Smarte59058c2008-08-24 21:49:00 -04007557 * @phba: Pointer to HBA context object.
7558 * @pmboxq: Pointer to driver mailbox object.
7559 * @timeout: Timeout in number of seconds.
7560 *
7561 * This function issues the mailbox to firmware and waits for the
7562 * mailbox command to complete. If the mailbox command is not
7563 * completed within timeout seconds, it returns MBX_TIMEOUT.
7564 * The function waits for the mailbox completion using an
7565 * interruptible wait. If the thread is woken up due to a
7566 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7567 * should not free the mailbox resources, if this function returns
7568 * MBX_TIMEOUT.
7569 * This function will sleep while waiting for mailbox completion.
7570 * So, this function should not be called from any context which
7571 * does not allow sleeping. Due to the same reason, this function
7572 * cannot be called with interrupt disabled.
7573 * This function assumes that the mailbox completion occurs while
7574 * this function sleep. So, this function cannot be called from
7575 * the worker thread which processes mailbox completion.
7576 * This function is called in the context of HBA management
7577 * applications.
7578 * This function returns MBX_SUCCESS when successful.
7579 * This function is called with no lock held.
7580 **/
dea31012005-04-17 16:05:31 -05007581int
James Smart2e0fef82007-06-17 19:56:36 -05007582lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea31012005-04-17 16:05:31 -05007583 uint32_t timeout)
7584{
Peter Zijlstra7259f0d2006-10-29 22:46:36 -08007585 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea31012005-04-17 16:05:31 -05007586 int retval;
James Smart858c9f62007-06-17 19:56:39 -05007587 unsigned long flag;
dea31012005-04-17 16:05:31 -05007588
7589 /* The caller must leave context1 empty. */
James Smart98c9ea52007-10-27 13:37:33 -04007590 if (pmboxq->context1)
James Smart2e0fef82007-06-17 19:56:36 -05007591 return MBX_NOT_FINISHED;
dea31012005-04-17 16:05:31 -05007592
James Smart495a7142008-06-14 22:52:59 -04007593 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea31012005-04-17 16:05:31 -05007594 /* setup wake call as IOCB callback */
7595 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7596 /* setup context field to pass wait_queue pointer to wake function */
7597 pmboxq->context1 = &done_q;
7598
dea31012005-04-17 16:05:31 -05007599 /* now issue the command */
7600 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7601
7602 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -04007603 wait_event_interruptible_timeout(done_q,
7604 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7605 timeout * HZ);
7606
James Smart858c9f62007-06-17 19:56:39 -05007607 spin_lock_irqsave(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007608 pmboxq->context1 = NULL;
James Smart7054a602007-04-25 09:52:34 -04007609 /*
7610 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7611 * else do not free the resources.
7612 */
7613 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea31012005-04-17 16:05:31 -05007614 retval = MBX_SUCCESS;
James Smart858c9f62007-06-17 19:56:39 -05007615 else {
James Smart7054a602007-04-25 09:52:34 -04007616 retval = MBX_TIMEOUT;
James Smart858c9f62007-06-17 19:56:39 -05007617 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7618 }
7619 spin_unlock_irqrestore(&phba->hbalock, flag);
dea31012005-04-17 16:05:31 -05007620 }
7621
dea31012005-04-17 16:05:31 -05007622 return retval;
7623}
7624
James Smarte59058c2008-08-24 21:49:00 -04007625/**
James Smart3772a992009-05-22 14:50:54 -04007626 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
James Smarte59058c2008-08-24 21:49:00 -04007627 * @phba: Pointer to HBA context.
7628 *
James Smart3772a992009-05-22 14:50:54 -04007629 * This function is called to shutdown the driver's mailbox sub-system.
7630 * It first marks the mailbox sub-system is in a block state to prevent
7631 * the asynchronous mailbox command from issued off the pending mailbox
7632 * command queue. If the mailbox command sub-system shutdown is due to
7633 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7634 * the mailbox sub-system flush routine to forcefully bring down the
7635 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7636 * as with offline or HBA function reset), this routine will wait for the
7637 * outstanding mailbox command to complete before invoking the mailbox
7638 * sub-system flush routine to gracefully bring down mailbox sub-system.
James Smarte59058c2008-08-24 21:49:00 -04007639 **/
James Smart3772a992009-05-22 14:50:54 -04007640void
7641lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
James Smartb4c02652006-07-06 15:50:43 -04007642{
James Smart3772a992009-05-22 14:50:54 -04007643 struct lpfc_sli *psli = &phba->sli;
7644 uint8_t actcmd = MBX_HEARTBEAT;
7645 unsigned long timeout;
7646
7647 spin_lock_irq(&phba->hbalock);
7648 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7649 spin_unlock_irq(&phba->hbalock);
7650
7651 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7652 spin_lock_irq(&phba->hbalock);
7653 if (phba->sli.mbox_active)
7654 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
7655 spin_unlock_irq(&phba->hbalock);
7656 /* Determine how long we might wait for the active mailbox
7657 * command to be gracefully completed by firmware.
7658 */
7659 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7660 1000) + jiffies;
7661 while (phba->sli.mbox_active) {
7662 /* Check active mailbox complete status every 2ms */
7663 msleep(2);
7664 if (time_after(jiffies, timeout))
7665 /* Timeout, let the mailbox flush routine to
7666 * forcefully release active mailbox command
7667 */
7668 break;
7669 }
7670 }
7671 lpfc_sli_mbox_sys_flush(phba);
7672}
7673
7674/**
7675 * lpfc_sli_eratt_read - read sli-3 error attention events
7676 * @phba: Pointer to HBA context.
7677 *
7678 * This function is called to read the SLI3 device error attention registers
7679 * for possible error attention events. The caller must hold the hostlock
7680 * with spin_lock_irq().
7681 *
7682 * This fucntion returns 1 when there is Error Attention in the Host Attention
7683 * Register and returns 0 otherwise.
7684 **/
7685static int
7686lpfc_sli_eratt_read(struct lpfc_hba *phba)
7687{
James Smarted957682007-06-17 19:56:37 -05007688 uint32_t ha_copy;
James Smartb4c02652006-07-06 15:50:43 -04007689
James Smart3772a992009-05-22 14:50:54 -04007690 /* Read chip Host Attention (HA) register */
7691 ha_copy = readl(phba->HAregaddr);
7692 if (ha_copy & HA_ERATT) {
7693 /* Read host status register to retrieve error event */
7694 lpfc_sli_read_hs(phba);
James Smartb4c02652006-07-06 15:50:43 -04007695
James Smart3772a992009-05-22 14:50:54 -04007696 /* Check if there is a deferred error condition is active */
7697 if ((HS_FFER1 & phba->work_hs) &&
7698 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7699 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
James Smart3772a992009-05-22 14:50:54 -04007700 phba->hba_flag |= DEFER_ERATT;
James Smart3772a992009-05-22 14:50:54 -04007701 /* Clear all interrupt enable conditions */
7702 writel(0, phba->HCregaddr);
7703 readl(phba->HCregaddr);
7704 }
7705
7706 /* Set the driver HA work bitmap */
James Smart3772a992009-05-22 14:50:54 -04007707 phba->work_ha |= HA_ERATT;
7708 /* Indicate polling handles this ERATT */
7709 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04007710 return 1;
James Smartb4c02652006-07-06 15:50:43 -04007711 }
James Smart3772a992009-05-22 14:50:54 -04007712 return 0;
James Smartb4c02652006-07-06 15:50:43 -04007713}
7714
James Smarte59058c2008-08-24 21:49:00 -04007715/**
James Smartda0436e2009-05-22 14:51:39 -04007716 * lpfc_sli4_eratt_read - read sli-4 error attention events
7717 * @phba: Pointer to HBA context.
7718 *
7719 * This function is called to read the SLI4 device error attention registers
7720 * for possible error attention events. The caller must hold the hostlock
7721 * with spin_lock_irq().
7722 *
7723 * This fucntion returns 1 when there is Error Attention in the Host Attention
7724 * Register and returns 0 otherwise.
7725 **/
7726static int
7727lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7728{
7729 uint32_t uerr_sta_hi, uerr_sta_lo;
7730 uint32_t onlnreg0, onlnreg1;
7731
7732 /* For now, use the SLI4 device internal unrecoverable error
7733 * registers for error attention. This can be changed later.
7734 */
7735 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7736 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7737 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7738 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7739 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7740 if (uerr_sta_lo || uerr_sta_hi) {
7741 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7742 "1423 HBA Unrecoverable error: "
7743 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7744 "online0_reg=0x%x, online1_reg=0x%x\n",
7745 uerr_sta_lo, uerr_sta_hi,
7746 onlnreg0, onlnreg1);
James Smartda0436e2009-05-22 14:51:39 -04007747 phba->work_status[0] = uerr_sta_lo;
7748 phba->work_status[1] = uerr_sta_hi;
James Smartda0436e2009-05-22 14:51:39 -04007749 /* Set the driver HA work bitmap */
7750 phba->work_ha |= HA_ERATT;
7751 /* Indicate polling handles this ERATT */
7752 phba->hba_flag |= HBA_ERATT_HANDLED;
James Smartda0436e2009-05-22 14:51:39 -04007753 return 1;
7754 }
7755 }
7756 return 0;
7757}
7758
7759/**
James Smart3621a712009-04-06 18:47:14 -04007760 * lpfc_sli_check_eratt - check error attention events
James Smart93996272008-08-24 21:50:30 -04007761 * @phba: Pointer to HBA context.
7762 *
James Smart3772a992009-05-22 14:50:54 -04007763 * This function is called from timer soft interrupt context to check HBA's
James Smart93996272008-08-24 21:50:30 -04007764 * error attention register bit for error attention events.
7765 *
7766 * This fucntion returns 1 when there is Error Attention in the Host Attention
7767 * Register and returns 0 otherwise.
7768 **/
7769int
7770lpfc_sli_check_eratt(struct lpfc_hba *phba)
7771{
7772 uint32_t ha_copy;
7773
7774 /* If somebody is waiting to handle an eratt, don't process it
7775 * here. The brdkill function will do this.
7776 */
7777 if (phba->link_flag & LS_IGNORE_ERATT)
7778 return 0;
7779
7780 /* Check if interrupt handler handles this ERATT */
7781 spin_lock_irq(&phba->hbalock);
7782 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7783 /* Interrupt handler has handled ERATT */
7784 spin_unlock_irq(&phba->hbalock);
7785 return 0;
7786 }
7787
James Smarta257bf92009-04-06 18:48:10 -04007788 /*
7789 * If there is deferred error attention, do not check for error
7790 * attention
7791 */
7792 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7793 spin_unlock_irq(&phba->hbalock);
7794 return 0;
7795 }
7796
James Smart3772a992009-05-22 14:50:54 -04007797 /* If PCI channel is offline, don't process it */
7798 if (unlikely(pci_channel_offline(phba->pcidev))) {
James Smart93996272008-08-24 21:50:30 -04007799 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007800 return 0;
7801 }
7802
7803 switch (phba->sli_rev) {
7804 case LPFC_SLI_REV2:
7805 case LPFC_SLI_REV3:
7806 /* Read chip Host Attention (HA) register */
7807 ha_copy = lpfc_sli_eratt_read(phba);
7808 break;
James Smartda0436e2009-05-22 14:51:39 -04007809 case LPFC_SLI_REV4:
7810 /* Read devcie Uncoverable Error (UERR) registers */
7811 ha_copy = lpfc_sli4_eratt_read(phba);
7812 break;
James Smart3772a992009-05-22 14:50:54 -04007813 default:
7814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7815 "0299 Invalid SLI revision (%d)\n",
7816 phba->sli_rev);
7817 ha_copy = 0;
7818 break;
James Smart93996272008-08-24 21:50:30 -04007819 }
7820 spin_unlock_irq(&phba->hbalock);
James Smart3772a992009-05-22 14:50:54 -04007821
7822 return ha_copy;
7823}
7824
7825/**
7826 * lpfc_intr_state_check - Check device state for interrupt handling
7827 * @phba: Pointer to HBA context.
7828 *
7829 * This inline routine checks whether a device or its PCI slot is in a state
7830 * that the interrupt should be handled.
7831 *
7832 * This function returns 0 if the device or the PCI slot is in a state that
7833 * interrupt should be handled, otherwise -EIO.
7834 */
7835static inline int
7836lpfc_intr_state_check(struct lpfc_hba *phba)
7837{
7838 /* If the pci channel is offline, ignore all the interrupts */
7839 if (unlikely(pci_channel_offline(phba->pcidev)))
7840 return -EIO;
7841
7842 /* Update device level interrupt statistics */
7843 phba->sli.slistat.sli_intr++;
7844
7845 /* Ignore all interrupts during initialization. */
7846 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7847 return -EIO;
7848
James Smart93996272008-08-24 21:50:30 -04007849 return 0;
7850}
7851
7852/**
James Smart3772a992009-05-22 14:50:54 -04007853 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
James Smarte59058c2008-08-24 21:49:00 -04007854 * @irq: Interrupt number.
7855 * @dev_id: The device context pointer.
7856 *
James Smart93996272008-08-24 21:50:30 -04007857 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04007858 * service routine when device with SLI-3 interface spec is enabled with
7859 * MSI-X multi-message interrupt mode and there are slow-path events in
7860 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7861 * interrupt mode, this function is called as part of the device-level
7862 * interrupt handler. When the PCI slot is in error recovery or the HBA
7863 * is undergoing initialization, the interrupt handler will not process
7864 * the interrupt. The link attention and ELS ring attention events are
7865 * handled by the worker thread. The interrupt handler signals the worker
7866 * thread and returns for these events. This function is called without
7867 * any lock held. It gets the hbalock to access and update SLI data
James Smart93996272008-08-24 21:50:30 -04007868 * structures.
7869 *
7870 * This function returns IRQ_HANDLED when interrupt is handled else it
7871 * returns IRQ_NONE.
James Smarte59058c2008-08-24 21:49:00 -04007872 **/
dea31012005-04-17 16:05:31 -05007873irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04007874lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea31012005-04-17 16:05:31 -05007875{
James Smart2e0fef82007-06-17 19:56:36 -05007876 struct lpfc_hba *phba;
dea31012005-04-17 16:05:31 -05007877 uint32_t ha_copy;
7878 uint32_t work_ha_copy;
7879 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05007880 unsigned long iflag;
dea31012005-04-17 16:05:31 -05007881 uint32_t control;
7882
James Smart92d7f7b2007-06-17 19:56:38 -05007883 MAILBOX_t *mbox, *pmbox;
James Smart858c9f62007-06-17 19:56:39 -05007884 struct lpfc_vport *vport;
7885 struct lpfc_nodelist *ndlp;
7886 struct lpfc_dmabuf *mp;
James Smart92d7f7b2007-06-17 19:56:38 -05007887 LPFC_MBOXQ_t *pmb;
7888 int rc;
7889
dea31012005-04-17 16:05:31 -05007890 /*
7891 * Get the driver's phba structure from the dev_id and
7892 * assume the HBA is not interrupting.
7893 */
James Smart93996272008-08-24 21:50:30 -04007894 phba = (struct lpfc_hba *)dev_id;
dea31012005-04-17 16:05:31 -05007895
7896 if (unlikely(!phba))
7897 return IRQ_NONE;
7898
dea31012005-04-17 16:05:31 -05007899 /*
James Smart93996272008-08-24 21:50:30 -04007900 * Stuff needs to be attented to when this function is invoked as an
7901 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05007902 */
James Smart93996272008-08-24 21:50:30 -04007903 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04007904 /* Check device state for handling interrupt */
7905 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04007906 return IRQ_NONE;
7907 /* Need to read HA REG for slow-path events */
James Smart5b75da22008-12-04 22:39:35 -05007908 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart34b02dc2008-08-24 21:49:55 -04007909 ha_copy = readl(phba->HAregaddr);
James Smart93996272008-08-24 21:50:30 -04007910 /* If somebody is waiting to handle an eratt don't process it
7911 * here. The brdkill function will do this.
7912 */
7913 if (phba->link_flag & LS_IGNORE_ERATT)
7914 ha_copy &= ~HA_ERATT;
7915 /* Check the need for handling ERATT in interrupt handler */
7916 if (ha_copy & HA_ERATT) {
7917 if (phba->hba_flag & HBA_ERATT_HANDLED)
7918 /* ERATT polling has handled ERATT */
7919 ha_copy &= ~HA_ERATT;
7920 else
7921 /* Indicate interrupt handler handles ERATT */
7922 phba->hba_flag |= HBA_ERATT_HANDLED;
7923 }
James Smarta257bf92009-04-06 18:48:10 -04007924
7925 /*
7926 * If there is deferred error attention, do not check for any
7927 * interrupt.
7928 */
7929 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04007930 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04007931 return IRQ_NONE;
7932 }
7933
James Smart93996272008-08-24 21:50:30 -04007934 /* Clear up only attention source related to slow-path */
7935 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7936 phba->HAregaddr);
7937 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007938 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04007939 } else
7940 ha_copy = phba->ha_copy;
dea31012005-04-17 16:05:31 -05007941
dea31012005-04-17 16:05:31 -05007942 work_ha_copy = ha_copy & phba->work_ha_mask;
7943
James Smart93996272008-08-24 21:50:30 -04007944 if (work_ha_copy) {
dea31012005-04-17 16:05:31 -05007945 if (work_ha_copy & HA_LATT) {
7946 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7947 /*
7948 * Turn off Link Attention interrupts
7949 * until CLEAR_LA done
7950 */
James Smart5b75da22008-12-04 22:39:35 -05007951 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007952 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7953 control = readl(phba->HCregaddr);
7954 control &= ~HC_LAINT_ENA;
7955 writel(control, phba->HCregaddr);
7956 readl(phba->HCregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05007957 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05007958 }
7959 else
7960 work_ha_copy &= ~HA_LATT;
7961 }
7962
James Smart93996272008-08-24 21:50:30 -04007963 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
James Smart858c9f62007-06-17 19:56:39 -05007964 /*
7965 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7966 * the only slow ring.
7967 */
7968 status = (work_ha_copy &
7969 (HA_RXMASK << (4*LPFC_ELS_RING)));
7970 status >>= (4*LPFC_ELS_RING);
7971 if (status & HA_RXMASK) {
James Smart5b75da22008-12-04 22:39:35 -05007972 spin_lock_irqsave(&phba->hbalock, iflag);
James Smart858c9f62007-06-17 19:56:39 -05007973 control = readl(phba->HCregaddr);
James Smarta58cbd52007-08-02 11:09:43 -04007974
7975 lpfc_debugfs_slow_ring_trc(phba,
7976 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
7977 control, status,
7978 (uint32_t)phba->sli.slistat.sli_intr);
7979
James Smart858c9f62007-06-17 19:56:39 -05007980 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
James Smarta58cbd52007-08-02 11:09:43 -04007981 lpfc_debugfs_slow_ring_trc(phba,
7982 "ISR Disable ring:"
7983 "pwork:x%x hawork:x%x wait:x%x",
7984 phba->work_ha, work_ha_copy,
7985 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007986 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04007987
James Smart858c9f62007-06-17 19:56:39 -05007988 control &=
7989 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea31012005-04-17 16:05:31 -05007990 writel(control, phba->HCregaddr);
7991 readl(phba->HCregaddr); /* flush */
dea31012005-04-17 16:05:31 -05007992 }
James Smarta58cbd52007-08-02 11:09:43 -04007993 else {
7994 lpfc_debugfs_slow_ring_trc(phba,
7995 "ISR slow ring: pwork:"
7996 "x%x hawork:x%x wait:x%x",
7997 phba->work_ha, work_ha_copy,
7998 (uint32_t)((unsigned long)
James Smart5e9d9b82008-06-14 22:52:53 -04007999 &phba->work_waitq));
James Smarta58cbd52007-08-02 11:09:43 -04008000 }
James Smart5b75da22008-12-04 22:39:35 -05008001 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008002 }
8003 }
James Smart5b75da22008-12-04 22:39:35 -05008004 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008005 if (work_ha_copy & HA_ERATT) {
James Smart93996272008-08-24 21:50:30 -04008006 lpfc_sli_read_hs(phba);
James Smarta257bf92009-04-06 18:48:10 -04008007 /*
8008 * Check if there is a deferred error condition
8009 * is active
8010 */
8011 if ((HS_FFER1 & phba->work_hs) &&
8012 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8013 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8014 phba->hba_flag |= DEFER_ERATT;
8015 /* Clear all interrupt enable conditions */
8016 writel(0, phba->HCregaddr);
8017 readl(phba->HCregaddr);
8018 }
8019 }
8020
James Smart93996272008-08-24 21:50:30 -04008021 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
James Smart92d7f7b2007-06-17 19:56:38 -05008022 pmb = phba->sli.mbox_active;
James Smart04c68492009-05-22 14:52:52 -04008023 pmbox = &pmb->u.mb;
James Smart34b02dc2008-08-24 21:49:55 -04008024 mbox = phba->mbox;
James Smart858c9f62007-06-17 19:56:39 -05008025 vport = pmb->vport;
James Smart92d7f7b2007-06-17 19:56:38 -05008026
8027 /* First check out the status word */
8028 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8029 if (pmbox->mbxOwner != OWN_HOST) {
James Smart5b75da22008-12-04 22:39:35 -05008030 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart92d7f7b2007-06-17 19:56:38 -05008031 /*
8032 * Stray Mailbox Interrupt, mbxCommand <cmd>
8033 * mbxStatus <status>
8034 */
James Smart09372822008-01-11 01:52:54 -05008035 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
James Smart92d7f7b2007-06-17 19:56:38 -05008036 LOG_SLI,
James Smarte8b62012007-08-02 11:10:09 -04008037 "(%d):0304 Stray Mailbox "
James Smart92d7f7b2007-06-17 19:56:38 -05008038 "Interrupt mbxCommand x%x "
8039 "mbxStatus x%x\n",
James Smarte8b62012007-08-02 11:10:09 -04008040 (vport ? vport->vpi : 0),
James Smart92d7f7b2007-06-17 19:56:38 -05008041 pmbox->mbxCommand,
8042 pmbox->mbxStatus);
James Smart09372822008-01-11 01:52:54 -05008043 /* clear mailbox attention bit */
8044 work_ha_copy &= ~HA_MBATT;
8045 } else {
James Smart97eab632008-04-07 10:16:05 -04008046 phba->sli.mbox_active = NULL;
James Smart5b75da22008-12-04 22:39:35 -05008047 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart09372822008-01-11 01:52:54 -05008048 phba->last_completion_time = jiffies;
8049 del_timer(&phba->sli.mbox_tmo);
James Smart09372822008-01-11 01:52:54 -05008050 if (pmb->mbox_cmpl) {
8051 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8052 MAILBOX_CMD_SIZE);
James Smart858c9f62007-06-17 19:56:39 -05008053 }
James Smart09372822008-01-11 01:52:54 -05008054 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8055 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8056
8057 lpfc_debugfs_disc_trc(vport,
8058 LPFC_DISC_TRC_MBOX_VPORT,
8059 "MBOX dflt rpi: : "
8060 "status:x%x rpi:x%x",
8061 (uint32_t)pmbox->mbxStatus,
8062 pmbox->un.varWords[0], 0);
8063
8064 if (!pmbox->mbxStatus) {
8065 mp = (struct lpfc_dmabuf *)
8066 (pmb->context1);
8067 ndlp = (struct lpfc_nodelist *)
8068 pmb->context2;
8069
8070 /* Reg_LOGIN of dflt RPI was
8071 * successful. new lets get
8072 * rid of the RPI using the
8073 * same mbox buffer.
8074 */
8075 lpfc_unreg_login(phba,
8076 vport->vpi,
8077 pmbox->un.varWords[0],
8078 pmb);
8079 pmb->mbox_cmpl =
8080 lpfc_mbx_cmpl_dflt_rpi;
8081 pmb->context1 = mp;
8082 pmb->context2 = ndlp;
8083 pmb->vport = vport;
James Smart58da1ff2008-04-07 10:15:56 -04008084 rc = lpfc_sli_issue_mbox(phba,
8085 pmb,
8086 MBX_NOWAIT);
8087 if (rc != MBX_BUSY)
8088 lpfc_printf_log(phba,
8089 KERN_ERR,
8090 LOG_MBOX | LOG_SLI,
James Smartd7c255b2008-08-24 21:50:00 -04008091 "0350 rc should have"
James Smart58da1ff2008-04-07 10:15:56 -04008092 "been MBX_BUSY");
James Smart3772a992009-05-22 14:50:54 -04008093 if (rc != MBX_NOT_FINISHED)
8094 goto send_current_mbox;
James Smart09372822008-01-11 01:52:54 -05008095 }
8096 }
James Smart5b75da22008-12-04 22:39:35 -05008097 spin_lock_irqsave(
8098 &phba->pport->work_port_lock,
8099 iflag);
James Smart09372822008-01-11 01:52:54 -05008100 phba->pport->work_port_events &=
8101 ~WORKER_MBOX_TMO;
James Smart5b75da22008-12-04 22:39:35 -05008102 spin_unlock_irqrestore(
8103 &phba->pport->work_port_lock,
8104 iflag);
James Smart09372822008-01-11 01:52:54 -05008105 lpfc_mbox_cmpl_put(phba, pmb);
James Smart858c9f62007-06-17 19:56:39 -05008106 }
James Smart97eab632008-04-07 10:16:05 -04008107 } else
James Smart5b75da22008-12-04 22:39:35 -05008108 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008109
James Smart92d7f7b2007-06-17 19:56:38 -05008110 if ((work_ha_copy & HA_MBATT) &&
8111 (phba->sli.mbox_active == NULL)) {
James Smart858c9f62007-06-17 19:56:39 -05008112send_current_mbox:
James Smart92d7f7b2007-06-17 19:56:38 -05008113 /* Process next mailbox command if there is one */
James Smart58da1ff2008-04-07 10:15:56 -04008114 do {
8115 rc = lpfc_sli_issue_mbox(phba, NULL,
8116 MBX_NOWAIT);
8117 } while (rc == MBX_NOT_FINISHED);
8118 if (rc != MBX_SUCCESS)
8119 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8120 LOG_SLI, "0349 rc should be "
8121 "MBX_SUCCESS");
James Smart92d7f7b2007-06-17 19:56:38 -05008122 }
8123
James Smart5b75da22008-12-04 22:39:35 -05008124 spin_lock_irqsave(&phba->hbalock, iflag);
dea31012005-04-17 16:05:31 -05008125 phba->work_ha |= work_ha_copy;
James Smart5b75da22008-12-04 22:39:35 -05008126 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart5e9d9b82008-06-14 22:52:53 -04008127 lpfc_worker_wake_up(phba);
dea31012005-04-17 16:05:31 -05008128 }
James Smart93996272008-08-24 21:50:30 -04008129 return IRQ_HANDLED;
dea31012005-04-17 16:05:31 -05008130
James Smart3772a992009-05-22 14:50:54 -04008131} /* lpfc_sli_sp_intr_handler */
James Smart93996272008-08-24 21:50:30 -04008132
8133/**
James Smart3772a992009-05-22 14:50:54 -04008134 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
James Smart93996272008-08-24 21:50:30 -04008135 * @irq: Interrupt number.
8136 * @dev_id: The device context pointer.
8137 *
8138 * This function is directly called from the PCI layer as an interrupt
James Smart3772a992009-05-22 14:50:54 -04008139 * service routine when device with SLI-3 interface spec is enabled with
8140 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8141 * ring event in the HBA. However, when the device is enabled with either
8142 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8143 * device-level interrupt handler. When the PCI slot is in error recovery
8144 * or the HBA is undergoing initialization, the interrupt handler will not
8145 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8146 * the intrrupt context. This function is called without any lock held.
8147 * It gets the hbalock to access and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008148 *
8149 * This function returns IRQ_HANDLED when interrupt is handled else it
8150 * returns IRQ_NONE.
8151 **/
8152irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008153lpfc_sli_fp_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008154{
8155 struct lpfc_hba *phba;
8156 uint32_t ha_copy;
8157 unsigned long status;
James Smart5b75da22008-12-04 22:39:35 -05008158 unsigned long iflag;
James Smart93996272008-08-24 21:50:30 -04008159
8160 /* Get the driver's phba structure from the dev_id and
8161 * assume the HBA is not interrupting.
8162 */
8163 phba = (struct lpfc_hba *) dev_id;
8164
8165 if (unlikely(!phba))
8166 return IRQ_NONE;
dea31012005-04-17 16:05:31 -05008167
8168 /*
James Smart93996272008-08-24 21:50:30 -04008169 * Stuff needs to be attented to when this function is invoked as an
8170 * individual interrupt handler in MSI-X multi-message interrupt mode
dea31012005-04-17 16:05:31 -05008171 */
James Smart93996272008-08-24 21:50:30 -04008172 if (phba->intr_type == MSIX) {
James Smart3772a992009-05-22 14:50:54 -04008173 /* Check device state for handling interrupt */
8174 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008175 return IRQ_NONE;
8176 /* Need to read HA REG for FCP ring and other ring events */
8177 ha_copy = readl(phba->HAregaddr);
8178 /* Clear up only attention source related to fast-path */
James Smart5b75da22008-12-04 22:39:35 -05008179 spin_lock_irqsave(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008180 /*
8181 * If there is deferred error attention, do not check for
8182 * any interrupt.
8183 */
8184 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
James Smart3772a992009-05-22 14:50:54 -04008185 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smarta257bf92009-04-06 18:48:10 -04008186 return IRQ_NONE;
8187 }
James Smart93996272008-08-24 21:50:30 -04008188 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8189 phba->HAregaddr);
8190 readl(phba->HAregaddr); /* flush */
James Smart5b75da22008-12-04 22:39:35 -05008191 spin_unlock_irqrestore(&phba->hbalock, iflag);
James Smart93996272008-08-24 21:50:30 -04008192 } else
8193 ha_copy = phba->ha_copy;
8194
8195 /*
8196 * Process all events on FCP ring. Take the optimized path for FCP IO.
8197 */
8198 ha_copy &= ~(phba->work_ha_mask);
8199
8200 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea31012005-04-17 16:05:31 -05008201 status >>= (4*LPFC_FCP_RING);
James Smart858c9f62007-06-17 19:56:39 -05008202 if (status & HA_RXMASK)
dea31012005-04-17 16:05:31 -05008203 lpfc_sli_handle_fast_ring_event(phba,
8204 &phba->sli.ring[LPFC_FCP_RING],
8205 status);
James Smarta4bc3372006-12-02 13:34:16 -05008206
8207 if (phba->cfg_multi_ring_support == 2) {
8208 /*
James Smart93996272008-08-24 21:50:30 -04008209 * Process all events on extra ring. Take the optimized path
8210 * for extra ring IO.
James Smarta4bc3372006-12-02 13:34:16 -05008211 */
James Smart93996272008-08-24 21:50:30 -04008212 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
James Smarta4bc3372006-12-02 13:34:16 -05008213 status >>= (4*LPFC_EXTRA_RING);
James Smart858c9f62007-06-17 19:56:39 -05008214 if (status & HA_RXMASK) {
James Smarta4bc3372006-12-02 13:34:16 -05008215 lpfc_sli_handle_fast_ring_event(phba,
8216 &phba->sli.ring[LPFC_EXTRA_RING],
8217 status);
8218 }
8219 }
dea31012005-04-17 16:05:31 -05008220 return IRQ_HANDLED;
James Smart3772a992009-05-22 14:50:54 -04008221} /* lpfc_sli_fp_intr_handler */
dea31012005-04-17 16:05:31 -05008222
James Smart93996272008-08-24 21:50:30 -04008223/**
James Smart3772a992009-05-22 14:50:54 -04008224 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
James Smart93996272008-08-24 21:50:30 -04008225 * @irq: Interrupt number.
8226 * @dev_id: The device context pointer.
8227 *
James Smart3772a992009-05-22 14:50:54 -04008228 * This function is the HBA device-level interrupt handler to device with
8229 * SLI-3 interface spec, called from the PCI layer when either MSI or
8230 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8231 * requires driver attention. This function invokes the slow-path interrupt
8232 * attention handling function and fast-path interrupt attention handling
8233 * function in turn to process the relevant HBA attention events. This
8234 * function is called without any lock held. It gets the hbalock to access
8235 * and update SLI data structures.
James Smart93996272008-08-24 21:50:30 -04008236 *
8237 * This function returns IRQ_HANDLED when interrupt is handled, else it
8238 * returns IRQ_NONE.
8239 **/
8240irqreturn_t
James Smart3772a992009-05-22 14:50:54 -04008241lpfc_sli_intr_handler(int irq, void *dev_id)
James Smart93996272008-08-24 21:50:30 -04008242{
8243 struct lpfc_hba *phba;
8244 irqreturn_t sp_irq_rc, fp_irq_rc;
8245 unsigned long status1, status2;
8246
8247 /*
8248 * Get the driver's phba structure from the dev_id and
8249 * assume the HBA is not interrupting.
8250 */
8251 phba = (struct lpfc_hba *) dev_id;
8252
8253 if (unlikely(!phba))
8254 return IRQ_NONE;
8255
James Smart3772a992009-05-22 14:50:54 -04008256 /* Check device state for handling interrupt */
8257 if (lpfc_intr_state_check(phba))
James Smart93996272008-08-24 21:50:30 -04008258 return IRQ_NONE;
8259
8260 spin_lock(&phba->hbalock);
8261 phba->ha_copy = readl(phba->HAregaddr);
8262 if (unlikely(!phba->ha_copy)) {
8263 spin_unlock(&phba->hbalock);
8264 return IRQ_NONE;
8265 } else if (phba->ha_copy & HA_ERATT) {
8266 if (phba->hba_flag & HBA_ERATT_HANDLED)
8267 /* ERATT polling has handled ERATT */
8268 phba->ha_copy &= ~HA_ERATT;
8269 else
8270 /* Indicate interrupt handler handles ERATT */
8271 phba->hba_flag |= HBA_ERATT_HANDLED;
8272 }
8273
James Smarta257bf92009-04-06 18:48:10 -04008274 /*
8275 * If there is deferred error attention, do not check for any interrupt.
8276 */
8277 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8278 spin_unlock_irq(&phba->hbalock);
8279 return IRQ_NONE;
8280 }
8281
James Smart93996272008-08-24 21:50:30 -04008282 /* Clear attention sources except link and error attentions */
8283 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8284 readl(phba->HAregaddr); /* flush */
8285 spin_unlock(&phba->hbalock);
8286
8287 /*
8288 * Invokes slow-path host attention interrupt handling as appropriate.
8289 */
8290
8291 /* status of events with mailbox and link attention */
8292 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8293
8294 /* status of events with ELS ring */
8295 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8296 status2 >>= (4*LPFC_ELS_RING);
8297
8298 if (status1 || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008299 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008300 else
8301 sp_irq_rc = IRQ_NONE;
8302
8303 /*
8304 * Invoke fast-path host attention interrupt handling as appropriate.
8305 */
8306
8307 /* status of events with FCP ring */
8308 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8309 status1 >>= (4*LPFC_FCP_RING);
8310
8311 /* status of events with extra ring */
8312 if (phba->cfg_multi_ring_support == 2) {
8313 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8314 status2 >>= (4*LPFC_EXTRA_RING);
8315 } else
8316 status2 = 0;
8317
8318 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
James Smart3772a992009-05-22 14:50:54 -04008319 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
James Smart93996272008-08-24 21:50:30 -04008320 else
8321 fp_irq_rc = IRQ_NONE;
8322
8323 /* Return device-level interrupt handling status */
8324 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
James Smart3772a992009-05-22 14:50:54 -04008325} /* lpfc_sli_intr_handler */
James Smart4f774512009-05-22 14:52:35 -04008326
8327/**
8328 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8329 * @phba: pointer to lpfc hba data structure.
8330 *
8331 * This routine is invoked by the worker thread to process all the pending
8332 * SLI4 FCP abort XRI events.
8333 **/
8334void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8335{
8336 struct lpfc_cq_event *cq_event;
8337
8338 /* First, declare the fcp xri abort event has been handled */
8339 spin_lock_irq(&phba->hbalock);
8340 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8341 spin_unlock_irq(&phba->hbalock);
8342 /* Now, handle all the fcp xri abort events */
8343 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8344 /* Get the first event from the head of the event queue */
8345 spin_lock_irq(&phba->hbalock);
8346 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8347 cq_event, struct lpfc_cq_event, list);
8348 spin_unlock_irq(&phba->hbalock);
8349 /* Notify aborted XRI for FCP work queue */
8350 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8351 /* Free the event processed back to the free pool */
8352 lpfc_sli4_cq_event_release(phba, cq_event);
8353 }
8354}
8355
8356/**
8357 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8358 * @phba: pointer to lpfc hba data structure.
8359 *
8360 * This routine is invoked by the worker thread to process all the pending
8361 * SLI4 els abort xri events.
8362 **/
8363void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8364{
8365 struct lpfc_cq_event *cq_event;
8366
8367 /* First, declare the els xri abort event has been handled */
8368 spin_lock_irq(&phba->hbalock);
8369 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8370 spin_unlock_irq(&phba->hbalock);
8371 /* Now, handle all the els xri abort events */
8372 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8373 /* Get the first event from the head of the event queue */
8374 spin_lock_irq(&phba->hbalock);
8375 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8376 cq_event, struct lpfc_cq_event, list);
8377 spin_unlock_irq(&phba->hbalock);
8378 /* Notify aborted XRI for ELS work queue */
8379 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8380 /* Free the event processed back to the free pool */
8381 lpfc_sli4_cq_event_release(phba, cq_event);
8382 }
8383}
8384
8385static void
8386lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8387 struct lpfc_iocbq *pIocbOut,
8388 struct lpfc_wcqe_complete *wcqe)
8389{
8390 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8391
8392 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8393 sizeof(struct lpfc_iocbq) - offset);
James Smart4d9ab992009-10-02 15:16:39 -04008394 pIocbIn->cq_event.cqe.wcqe_cmpl = *wcqe;
James Smart4f774512009-05-22 14:52:35 -04008395 /* Map WCQE parameters into irspiocb parameters */
8396 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8397 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8398 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8399 pIocbIn->iocb.un.fcpi.fcpi_parm =
8400 pIocbOut->iocb.un.fcpi.fcpi_parm -
8401 wcqe->total_data_placed;
8402 else
8403 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8404 else
8405 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
James Smart4f774512009-05-22 14:52:35 -04008406}
8407
8408/**
James Smart04c68492009-05-22 14:52:52 -04008409 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8410 * @phba: Pointer to HBA context object.
8411 * @cqe: Pointer to mailbox completion queue entry.
8412 *
8413 * This routine process a mailbox completion queue entry with asynchrous
8414 * event.
8415 *
8416 * Return: true if work posted to worker thread, otherwise false.
8417 **/
8418static bool
8419lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8420{
8421 struct lpfc_cq_event *cq_event;
8422 unsigned long iflags;
8423
8424 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8425 "0392 Async Event: word0:x%x, word1:x%x, "
8426 "word2:x%x, word3:x%x\n", mcqe->word0,
8427 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8428
8429 /* Allocate a new internal CQ_EVENT entry */
8430 cq_event = lpfc_sli4_cq_event_alloc(phba);
8431 if (!cq_event) {
8432 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8433 "0394 Failed to allocate CQ_EVENT entry\n");
8434 return false;
8435 }
8436
8437 /* Move the CQE into an asynchronous event entry */
8438 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8439 spin_lock_irqsave(&phba->hbalock, iflags);
8440 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8441 /* Set the async event flag */
8442 phba->hba_flag |= ASYNC_EVENT;
8443 spin_unlock_irqrestore(&phba->hbalock, iflags);
8444
8445 return true;
8446}
8447
8448/**
8449 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8450 * @phba: Pointer to HBA context object.
8451 * @cqe: Pointer to mailbox completion queue entry.
8452 *
8453 * This routine process a mailbox completion queue entry with mailbox
8454 * completion event.
8455 *
8456 * Return: true if work posted to worker thread, otherwise false.
8457 **/
8458static bool
8459lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8460{
8461 uint32_t mcqe_status;
8462 MAILBOX_t *mbox, *pmbox;
8463 struct lpfc_mqe *mqe;
8464 struct lpfc_vport *vport;
8465 struct lpfc_nodelist *ndlp;
8466 struct lpfc_dmabuf *mp;
8467 unsigned long iflags;
8468 LPFC_MBOXQ_t *pmb;
8469 bool workposted = false;
8470 int rc;
8471
8472 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8473 if (!bf_get(lpfc_trailer_completed, mcqe))
8474 goto out_no_mqe_complete;
8475
8476 /* Get the reference to the active mbox command */
8477 spin_lock_irqsave(&phba->hbalock, iflags);
8478 pmb = phba->sli.mbox_active;
8479 if (unlikely(!pmb)) {
8480 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8481 "1832 No pending MBOX command to handle\n");
8482 spin_unlock_irqrestore(&phba->hbalock, iflags);
8483 goto out_no_mqe_complete;
8484 }
8485 spin_unlock_irqrestore(&phba->hbalock, iflags);
8486 mqe = &pmb->u.mqe;
8487 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8488 mbox = phba->mbox;
8489 vport = pmb->vport;
8490
8491 /* Reset heartbeat timer */
8492 phba->last_completion_time = jiffies;
8493 del_timer(&phba->sli.mbox_tmo);
8494
8495 /* Move mbox data to caller's mailbox region, do endian swapping */
8496 if (pmb->mbox_cmpl && mbox)
8497 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8498 /* Set the mailbox status with SLI4 range 0x4000 */
8499 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8500 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8501 bf_set(lpfc_mqe_status, mqe,
8502 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8503
8504 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8505 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8506 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8507 "MBOX dflt rpi: status:x%x rpi:x%x",
8508 mcqe_status,
8509 pmbox->un.varWords[0], 0);
8510 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8511 mp = (struct lpfc_dmabuf *)(pmb->context1);
8512 ndlp = (struct lpfc_nodelist *)pmb->context2;
8513 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8514 * RID of the PPI using the same mbox buffer.
8515 */
8516 lpfc_unreg_login(phba, vport->vpi,
8517 pmbox->un.varWords[0], pmb);
8518 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8519 pmb->context1 = mp;
8520 pmb->context2 = ndlp;
8521 pmb->vport = vport;
8522 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8523 if (rc != MBX_BUSY)
8524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8525 LOG_SLI, "0385 rc should "
8526 "have been MBX_BUSY\n");
8527 if (rc != MBX_NOT_FINISHED)
8528 goto send_current_mbox;
8529 }
8530 }
8531 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8532 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8533 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8534
8535 /* There is mailbox completion work to do */
8536 spin_lock_irqsave(&phba->hbalock, iflags);
8537 __lpfc_mbox_cmpl_put(phba, pmb);
8538 phba->work_ha |= HA_MBATT;
8539 spin_unlock_irqrestore(&phba->hbalock, iflags);
8540 workposted = true;
8541
8542send_current_mbox:
8543 spin_lock_irqsave(&phba->hbalock, iflags);
8544 /* Release the mailbox command posting token */
8545 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8546 /* Setting active mailbox pointer need to be in sync to flag clear */
8547 phba->sli.mbox_active = NULL;
8548 spin_unlock_irqrestore(&phba->hbalock, iflags);
8549 /* Wake up worker thread to post the next pending mailbox command */
8550 lpfc_worker_wake_up(phba);
8551out_no_mqe_complete:
8552 if (bf_get(lpfc_trailer_consumed, mcqe))
8553 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8554 return workposted;
8555}
8556
8557/**
8558 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8559 * @phba: Pointer to HBA context object.
8560 * @cqe: Pointer to mailbox completion queue entry.
8561 *
8562 * This routine process a mailbox completion queue entry, it invokes the
8563 * proper mailbox complete handling or asynchrous event handling routine
8564 * according to the MCQE's async bit.
8565 *
8566 * Return: true if work posted to worker thread, otherwise false.
8567 **/
8568static bool
8569lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8570{
8571 struct lpfc_mcqe mcqe;
8572 bool workposted;
8573
8574 /* Copy the mailbox MCQE and convert endian order as needed */
8575 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8576
8577 /* Invoke the proper event handling routine */
8578 if (!bf_get(lpfc_trailer_async, &mcqe))
8579 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8580 else
8581 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8582 return workposted;
8583}
8584
8585/**
James Smart4f774512009-05-22 14:52:35 -04008586 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8587 * @phba: Pointer to HBA context object.
8588 * @wcqe: Pointer to work-queue completion queue entry.
8589 *
8590 * This routine handles an ELS work-queue completion event.
8591 *
8592 * Return: true if work posted to worker thread, otherwise false.
8593 **/
8594static bool
8595lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8596 struct lpfc_wcqe_complete *wcqe)
8597{
8598 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8599 struct lpfc_iocbq *cmdiocbq;
8600 struct lpfc_iocbq *irspiocbq;
8601 unsigned long iflags;
8602 bool workposted = false;
8603
8604 spin_lock_irqsave(&phba->hbalock, iflags);
8605 pring->stats.iocb_event++;
8606 /* Look up the ELS command IOCB and create pseudo response IOCB */
8607 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8608 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8609 spin_unlock_irqrestore(&phba->hbalock, iflags);
8610
8611 if (unlikely(!cmdiocbq)) {
8612 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8613 "0386 ELS complete with no corresponding "
8614 "cmdiocb: iotag (%d)\n",
8615 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8616 return workposted;
8617 }
8618
8619 /* Fake the irspiocbq and copy necessary response information */
8620 irspiocbq = lpfc_sli_get_iocbq(phba);
8621 if (!irspiocbq) {
8622 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8623 "0387 Failed to allocate an iocbq\n");
8624 return workposted;
8625 }
8626 lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8627
8628 /* Add the irspiocb to the response IOCB work list */
8629 spin_lock_irqsave(&phba->hbalock, iflags);
James Smart4d9ab992009-10-02 15:16:39 -04008630 list_add_tail(&irspiocbq->cq_event.list,
8631 &phba->sli4_hba.sp_rspiocb_work_queue);
James Smart4f774512009-05-22 14:52:35 -04008632 /* Indicate ELS ring attention */
8633 phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
8634 spin_unlock_irqrestore(&phba->hbalock, iflags);
8635 workposted = true;
8636
8637 return workposted;
8638}
8639
8640/**
8641 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8642 * @phba: Pointer to HBA context object.
8643 * @wcqe: Pointer to work-queue completion queue entry.
8644 *
8645 * This routine handles slow-path WQ entry comsumed event by invoking the
8646 * proper WQ release routine to the slow-path WQ.
8647 **/
8648static void
8649lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8650 struct lpfc_wcqe_release *wcqe)
8651{
8652 /* Check for the slow-path ELS work queue */
8653 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8654 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8655 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8656 else
8657 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8658 "2579 Slow-path wqe consume event carries "
8659 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8660 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8661 phba->sli4_hba.els_wq->queue_id);
8662}
8663
8664/**
8665 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8666 * @phba: Pointer to HBA context object.
8667 * @cq: Pointer to a WQ completion queue.
8668 * @wcqe: Pointer to work-queue completion queue entry.
8669 *
8670 * This routine handles an XRI abort event.
8671 *
8672 * Return: true if work posted to worker thread, otherwise false.
8673 **/
8674static bool
8675lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8676 struct lpfc_queue *cq,
8677 struct sli4_wcqe_xri_aborted *wcqe)
8678{
8679 bool workposted = false;
8680 struct lpfc_cq_event *cq_event;
8681 unsigned long iflags;
8682
8683 /* Allocate a new internal CQ_EVENT entry */
8684 cq_event = lpfc_sli4_cq_event_alloc(phba);
8685 if (!cq_event) {
8686 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8687 "0602 Failed to allocate CQ_EVENT entry\n");
8688 return false;
8689 }
8690
8691 /* Move the CQE into the proper xri abort event list */
8692 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8693 switch (cq->subtype) {
8694 case LPFC_FCP:
8695 spin_lock_irqsave(&phba->hbalock, iflags);
8696 list_add_tail(&cq_event->list,
8697 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8698 /* Set the fcp xri abort event flag */
8699 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8700 spin_unlock_irqrestore(&phba->hbalock, iflags);
8701 workposted = true;
8702 break;
8703 case LPFC_ELS:
8704 spin_lock_irqsave(&phba->hbalock, iflags);
8705 list_add_tail(&cq_event->list,
8706 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8707 /* Set the els xri abort event flag */
8708 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8709 spin_unlock_irqrestore(&phba->hbalock, iflags);
8710 workposted = true;
8711 break;
8712 default:
8713 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8714 "0603 Invalid work queue CQE subtype (x%x)\n",
8715 cq->subtype);
8716 workposted = false;
8717 break;
8718 }
8719 return workposted;
8720}
8721
8722/**
James Smart4d9ab992009-10-02 15:16:39 -04008723 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
James Smart4f774512009-05-22 14:52:35 -04008724 * @phba: Pointer to HBA context object.
James Smart4d9ab992009-10-02 15:16:39 -04008725 * @rcqe: Pointer to receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008726 *
James Smart4d9ab992009-10-02 15:16:39 -04008727 * This routine process a receive-queue completion queue entry.
James Smart4f774512009-05-22 14:52:35 -04008728 *
8729 * Return: true if work posted to worker thread, otherwise false.
8730 **/
8731static bool
James Smart4d9ab992009-10-02 15:16:39 -04008732lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
8733{
8734 bool workposted = false;
8735 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8736 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8737 struct hbq_dmabuf *dma_buf;
8738 uint32_t status;
8739 unsigned long iflags;
8740
8741 lpfc_sli4_rq_release(hrq, drq);
8742 if (bf_get(lpfc_rcqe_code, rcqe) != CQE_CODE_RECEIVE)
8743 goto out;
8744 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
8745 goto out;
8746
8747 status = bf_get(lpfc_rcqe_status, rcqe);
8748 switch (status) {
8749 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8750 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8751 "2537 Receive Frame Truncated!!\n");
8752 case FC_STATUS_RQ_SUCCESS:
8753 spin_lock_irqsave(&phba->hbalock, iflags);
8754 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8755 if (!dma_buf) {
8756 spin_unlock_irqrestore(&phba->hbalock, iflags);
8757 goto out;
8758 }
8759 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
8760 /* save off the frame for the word thread to process */
8761 list_add_tail(&dma_buf->cq_event.list,
8762 &phba->sli4_hba.sp_rspiocb_work_queue);
8763 /* Frame received */
8764 phba->hba_flag |= HBA_RECEIVE_BUFFER;
8765 spin_unlock_irqrestore(&phba->hbalock, iflags);
8766 workposted = true;
8767 break;
8768 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8769 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8770 /* Post more buffers if possible */
8771 spin_lock_irqsave(&phba->hbalock, iflags);
8772 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8773 spin_unlock_irqrestore(&phba->hbalock, iflags);
8774 workposted = true;
8775 break;
8776 }
8777out:
8778 return workposted;
8779
8780}
8781
8782/**
8783 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8784 * @phba: Pointer to HBA context object.
8785 * @cq: Pointer to the completion queue.
8786 * @wcqe: Pointer to a completion queue entry.
8787 *
8788 * This routine process a slow-path work-queue or recieve queue completion queue
8789 * entry.
8790 *
8791 * Return: true if work posted to worker thread, otherwise false.
8792 **/
8793static bool
8794lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
James Smart4f774512009-05-22 14:52:35 -04008795 struct lpfc_cqe *cqe)
8796{
8797 struct lpfc_wcqe_complete wcqe;
8798 bool workposted = false;
8799
8800 /* Copy the work queue CQE and convert endian order if needed */
8801 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8802
8803 /* Check and process for different type of WCQE and dispatch */
8804 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8805 case CQE_CODE_COMPL_WQE:
8806 /* Process the WQ complete event */
8807 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8808 (struct lpfc_wcqe_complete *)&wcqe);
8809 break;
8810 case CQE_CODE_RELEASE_WQE:
8811 /* Process the WQ release event */
8812 lpfc_sli4_sp_handle_rel_wcqe(phba,
8813 (struct lpfc_wcqe_release *)&wcqe);
8814 break;
8815 case CQE_CODE_XRI_ABORTED:
8816 /* Process the WQ XRI abort event */
8817 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8818 (struct sli4_wcqe_xri_aborted *)&wcqe);
8819 break;
James Smart4d9ab992009-10-02 15:16:39 -04008820 case CQE_CODE_RECEIVE:
8821 /* Process the RQ event */
8822 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8823 (struct lpfc_rcqe *)&wcqe);
8824 break;
James Smart4f774512009-05-22 14:52:35 -04008825 default:
8826 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8827 "0388 Not a valid WCQE code: x%x\n",
8828 bf_get(lpfc_wcqe_c_code, &wcqe));
8829 break;
8830 }
8831 return workposted;
8832}
8833
8834/**
James Smart4f774512009-05-22 14:52:35 -04008835 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8836 * @phba: Pointer to HBA context object.
8837 * @eqe: Pointer to fast-path event queue entry.
8838 *
8839 * This routine process a event queue entry from the slow-path event queue.
8840 * It will check the MajorCode and MinorCode to determine this is for a
8841 * completion event on a completion queue, if not, an error shall be logged
8842 * and just return. Otherwise, it will get to the corresponding completion
8843 * queue and process all the entries on that completion queue, rearm the
8844 * completion queue, and then return.
8845 *
8846 **/
8847static void
8848lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8849{
8850 struct lpfc_queue *cq = NULL, *childq, *speq;
8851 struct lpfc_cqe *cqe;
8852 bool workposted = false;
8853 int ecount = 0;
8854 uint16_t cqid;
8855
8856 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8857 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8859 "0359 Not a valid slow-path completion "
8860 "event: majorcode=x%x, minorcode=x%x\n",
8861 bf_get(lpfc_eqe_major_code, eqe),
8862 bf_get(lpfc_eqe_minor_code, eqe));
8863 return;
8864 }
8865
8866 /* Get the reference to the corresponding CQ */
8867 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8868
8869 /* Search for completion queue pointer matching this cqid */
8870 speq = phba->sli4_hba.sp_eq;
8871 list_for_each_entry(childq, &speq->child_list, list) {
8872 if (childq->queue_id == cqid) {
8873 cq = childq;
8874 break;
8875 }
8876 }
8877 if (unlikely(!cq)) {
8878 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8879 "0365 Slow-path CQ identifier (%d) does "
8880 "not exist\n", cqid);
8881 return;
8882 }
8883
8884 /* Process all the entries to the CQ */
8885 switch (cq->type) {
8886 case LPFC_MCQ:
8887 while ((cqe = lpfc_sli4_cq_get(cq))) {
8888 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8889 if (!(++ecount % LPFC_GET_QE_REL_INT))
8890 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8891 }
8892 break;
8893 case LPFC_WCQ:
8894 while ((cqe = lpfc_sli4_cq_get(cq))) {
James Smart4d9ab992009-10-02 15:16:39 -04008895 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
James Smart4f774512009-05-22 14:52:35 -04008896 if (!(++ecount % LPFC_GET_QE_REL_INT))
8897 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8898 }
8899 break;
8900 default:
8901 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8902 "0370 Invalid completion queue type (%d)\n",
8903 cq->type);
8904 return;
8905 }
8906
8907 /* Catch the no cq entry condition, log an error */
8908 if (unlikely(ecount == 0))
8909 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8910 "0371 No entry from the CQ: identifier "
8911 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8912
8913 /* In any case, flash and re-arm the RCQ */
8914 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8915
8916 /* wake up worker thread if there are works to be done */
8917 if (workposted)
8918 lpfc_worker_wake_up(phba);
8919}
8920
8921/**
8922 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8923 * @eqe: Pointer to fast-path completion queue entry.
8924 *
8925 * This routine process a fast-path work queue completion entry from fast-path
8926 * event queue for FCP command response completion.
8927 **/
8928static void
8929lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8930 struct lpfc_wcqe_complete *wcqe)
8931{
8932 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8933 struct lpfc_iocbq *cmdiocbq;
8934 struct lpfc_iocbq irspiocbq;
8935 unsigned long iflags;
8936
8937 spin_lock_irqsave(&phba->hbalock, iflags);
8938 pring->stats.iocb_event++;
8939 spin_unlock_irqrestore(&phba->hbalock, iflags);
8940
8941 /* Check for response status */
8942 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8943 /* If resource errors reported from HBA, reduce queue
8944 * depth of the SCSI device.
8945 */
8946 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8947 IOSTAT_LOCAL_REJECT) &&
8948 (wcqe->parameter == IOERR_NO_RESOURCES)) {
8949 phba->lpfc_rampdown_queue_depth(phba);
8950 }
8951 /* Log the error status */
8952 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8953 "0373 FCP complete error: status=x%x, "
8954 "hw_status=x%x, total_data_specified=%d, "
8955 "parameter=x%x, word3=x%x\n",
8956 bf_get(lpfc_wcqe_c_status, wcqe),
8957 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8958 wcqe->total_data_placed, wcqe->parameter,
8959 wcqe->word3);
8960 }
8961
8962 /* Look up the FCP command IOCB and create pseudo response IOCB */
8963 spin_lock_irqsave(&phba->hbalock, iflags);
8964 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8965 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8966 spin_unlock_irqrestore(&phba->hbalock, iflags);
8967 if (unlikely(!cmdiocbq)) {
8968 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8969 "0374 FCP complete with no corresponding "
8970 "cmdiocb: iotag (%d)\n",
8971 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8972 return;
8973 }
8974 if (unlikely(!cmdiocbq->iocb_cmpl)) {
8975 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8976 "0375 FCP cmdiocb not callback function "
8977 "iotag: (%d)\n",
8978 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8979 return;
8980 }
8981
8982 /* Fake the irspiocb and copy necessary response information */
8983 lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8984
8985 /* Pass the cmd_iocb and the rsp state to the upper layer */
8986 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8987}
8988
8989/**
8990 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8991 * @phba: Pointer to HBA context object.
8992 * @cq: Pointer to completion queue.
8993 * @wcqe: Pointer to work-queue completion queue entry.
8994 *
8995 * This routine handles an fast-path WQ entry comsumed event by invoking the
8996 * proper WQ release routine to the slow-path WQ.
8997 **/
8998static void
8999lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9000 struct lpfc_wcqe_release *wcqe)
9001{
9002 struct lpfc_queue *childwq;
9003 bool wqid_matched = false;
9004 uint16_t fcp_wqid;
9005
9006 /* Check for fast-path FCP work queue release */
9007 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9008 list_for_each_entry(childwq, &cq->child_list, list) {
9009 if (childwq->queue_id == fcp_wqid) {
9010 lpfc_sli4_wq_release(childwq,
9011 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9012 wqid_matched = true;
9013 break;
9014 }
9015 }
9016 /* Report warning log message if no match found */
9017 if (wqid_matched != true)
9018 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9019 "2580 Fast-path wqe consume event carries "
9020 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9021}
9022
9023/**
9024 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9025 * @cq: Pointer to the completion queue.
9026 * @eqe: Pointer to fast-path completion queue entry.
9027 *
9028 * This routine process a fast-path work queue completion entry from fast-path
9029 * event queue for FCP command response completion.
9030 **/
9031static int
9032lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9033 struct lpfc_cqe *cqe)
9034{
9035 struct lpfc_wcqe_release wcqe;
9036 bool workposted = false;
9037
9038 /* Copy the work queue CQE and convert endian order if needed */
9039 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9040
9041 /* Check and process for different type of WCQE and dispatch */
9042 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9043 case CQE_CODE_COMPL_WQE:
9044 /* Process the WQ complete event */
9045 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9046 (struct lpfc_wcqe_complete *)&wcqe);
9047 break;
9048 case CQE_CODE_RELEASE_WQE:
9049 /* Process the WQ release event */
9050 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9051 (struct lpfc_wcqe_release *)&wcqe);
9052 break;
9053 case CQE_CODE_XRI_ABORTED:
9054 /* Process the WQ XRI abort event */
9055 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9056 (struct sli4_wcqe_xri_aborted *)&wcqe);
9057 break;
9058 default:
9059 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9060 "0144 Not a valid WCQE code: x%x\n",
9061 bf_get(lpfc_wcqe_c_code, &wcqe));
9062 break;
9063 }
9064 return workposted;
9065}
9066
9067/**
9068 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9069 * @phba: Pointer to HBA context object.
9070 * @eqe: Pointer to fast-path event queue entry.
9071 *
9072 * This routine process a event queue entry from the fast-path event queue.
9073 * It will check the MajorCode and MinorCode to determine this is for a
9074 * completion event on a completion queue, if not, an error shall be logged
9075 * and just return. Otherwise, it will get to the corresponding completion
9076 * queue and process all the entries on the completion queue, rearm the
9077 * completion queue, and then return.
9078 **/
9079static void
9080lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9081 uint32_t fcp_cqidx)
9082{
9083 struct lpfc_queue *cq;
9084 struct lpfc_cqe *cqe;
9085 bool workposted = false;
9086 uint16_t cqid;
9087 int ecount = 0;
9088
9089 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9090 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9091 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9092 "0366 Not a valid fast-path completion "
9093 "event: majorcode=x%x, minorcode=x%x\n",
9094 bf_get(lpfc_eqe_major_code, eqe),
9095 bf_get(lpfc_eqe_minor_code, eqe));
9096 return;
9097 }
9098
9099 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9100 if (unlikely(!cq)) {
9101 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9102 "0367 Fast-path completion queue does not "
9103 "exist\n");
9104 return;
9105 }
9106
9107 /* Get the reference to the corresponding CQ */
9108 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9109 if (unlikely(cqid != cq->queue_id)) {
9110 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9111 "0368 Miss-matched fast-path completion "
9112 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9113 cqid, cq->queue_id);
9114 return;
9115 }
9116
9117 /* Process all the entries to the CQ */
9118 while ((cqe = lpfc_sli4_cq_get(cq))) {
9119 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9120 if (!(++ecount % LPFC_GET_QE_REL_INT))
9121 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9122 }
9123
9124 /* Catch the no cq entry condition */
9125 if (unlikely(ecount == 0))
9126 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9127 "0369 No entry from fast-path completion "
9128 "queue fcpcqid=%d\n", cq->queue_id);
9129
9130 /* In any case, flash and re-arm the CQ */
9131 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9132
9133 /* wake up worker thread if there are works to be done */
9134 if (workposted)
9135 lpfc_worker_wake_up(phba);
9136}
9137
9138static void
9139lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9140{
9141 struct lpfc_eqe *eqe;
9142
9143 /* walk all the EQ entries and drop on the floor */
9144 while ((eqe = lpfc_sli4_eq_get(eq)))
9145 ;
9146
9147 /* Clear and re-arm the EQ */
9148 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9149}
9150
9151/**
9152 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9153 * @irq: Interrupt number.
9154 * @dev_id: The device context pointer.
9155 *
9156 * This function is directly called from the PCI layer as an interrupt
9157 * service routine when device with SLI-4 interface spec is enabled with
9158 * MSI-X multi-message interrupt mode and there are slow-path events in
9159 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9160 * interrupt mode, this function is called as part of the device-level
9161 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9162 * undergoing initialization, the interrupt handler will not process the
9163 * interrupt. The link attention and ELS ring attention events are handled
9164 * by the worker thread. The interrupt handler signals the worker thread
9165 * and returns for these events. This function is called without any lock
9166 * held. It gets the hbalock to access and update SLI data structures.
9167 *
9168 * This function returns IRQ_HANDLED when interrupt is handled else it
9169 * returns IRQ_NONE.
9170 **/
9171irqreturn_t
9172lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9173{
9174 struct lpfc_hba *phba;
9175 struct lpfc_queue *speq;
9176 struct lpfc_eqe *eqe;
9177 unsigned long iflag;
9178 int ecount = 0;
9179
9180 /*
9181 * Get the driver's phba structure from the dev_id
9182 */
9183 phba = (struct lpfc_hba *)dev_id;
9184
9185 if (unlikely(!phba))
9186 return IRQ_NONE;
9187
9188 /* Get to the EQ struct associated with this vector */
9189 speq = phba->sli4_hba.sp_eq;
9190
9191 /* Check device state for handling interrupt */
9192 if (unlikely(lpfc_intr_state_check(phba))) {
9193 /* Check again for link_state with lock held */
9194 spin_lock_irqsave(&phba->hbalock, iflag);
9195 if (phba->link_state < LPFC_LINK_DOWN)
9196 /* Flush, clear interrupt, and rearm the EQ */
9197 lpfc_sli4_eq_flush(phba, speq);
9198 spin_unlock_irqrestore(&phba->hbalock, iflag);
9199 return IRQ_NONE;
9200 }
9201
9202 /*
9203 * Process all the event on FCP slow-path EQ
9204 */
9205 while ((eqe = lpfc_sli4_eq_get(speq))) {
9206 lpfc_sli4_sp_handle_eqe(phba, eqe);
9207 if (!(++ecount % LPFC_GET_QE_REL_INT))
9208 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9209 }
9210
9211 /* Always clear and re-arm the slow-path EQ */
9212 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9213
9214 /* Catch the no cq entry condition */
9215 if (unlikely(ecount == 0)) {
9216 if (phba->intr_type == MSIX)
9217 /* MSI-X treated interrupt served as no EQ share INT */
9218 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9219 "0357 MSI-X interrupt with no EQE\n");
9220 else
9221 /* Non MSI-X treated on interrupt as EQ share INT */
9222 return IRQ_NONE;
9223 }
9224
9225 return IRQ_HANDLED;
9226} /* lpfc_sli4_sp_intr_handler */
9227
9228/**
9229 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9230 * @irq: Interrupt number.
9231 * @dev_id: The device context pointer.
9232 *
9233 * This function is directly called from the PCI layer as an interrupt
9234 * service routine when device with SLI-4 interface spec is enabled with
9235 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9236 * ring event in the HBA. However, when the device is enabled with either
9237 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9238 * device-level interrupt handler. When the PCI slot is in error recovery
9239 * or the HBA is undergoing initialization, the interrupt handler will not
9240 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9241 * the intrrupt context. This function is called without any lock held.
9242 * It gets the hbalock to access and update SLI data structures. Note that,
9243 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9244 * equal to that of FCP CQ index.
9245 *
9246 * This function returns IRQ_HANDLED when interrupt is handled else it
9247 * returns IRQ_NONE.
9248 **/
9249irqreturn_t
9250lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9251{
9252 struct lpfc_hba *phba;
9253 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9254 struct lpfc_queue *fpeq;
9255 struct lpfc_eqe *eqe;
9256 unsigned long iflag;
9257 int ecount = 0;
9258 uint32_t fcp_eqidx;
9259
9260 /* Get the driver's phba structure from the dev_id */
9261 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9262 phba = fcp_eq_hdl->phba;
9263 fcp_eqidx = fcp_eq_hdl->idx;
9264
9265 if (unlikely(!phba))
9266 return IRQ_NONE;
9267
9268 /* Get to the EQ struct associated with this vector */
9269 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9270
9271 /* Check device state for handling interrupt */
9272 if (unlikely(lpfc_intr_state_check(phba))) {
9273 /* Check again for link_state with lock held */
9274 spin_lock_irqsave(&phba->hbalock, iflag);
9275 if (phba->link_state < LPFC_LINK_DOWN)
9276 /* Flush, clear interrupt, and rearm the EQ */
9277 lpfc_sli4_eq_flush(phba, fpeq);
9278 spin_unlock_irqrestore(&phba->hbalock, iflag);
9279 return IRQ_NONE;
9280 }
9281
9282 /*
9283 * Process all the event on FCP fast-path EQ
9284 */
9285 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9286 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9287 if (!(++ecount % LPFC_GET_QE_REL_INT))
9288 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9289 }
9290
9291 /* Always clear and re-arm the fast-path EQ */
9292 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9293
9294 if (unlikely(ecount == 0)) {
9295 if (phba->intr_type == MSIX)
9296 /* MSI-X treated interrupt served as no EQ share INT */
9297 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9298 "0358 MSI-X interrupt with no EQE\n");
9299 else
9300 /* Non MSI-X treated on interrupt as EQ share INT */
9301 return IRQ_NONE;
9302 }
9303
9304 return IRQ_HANDLED;
9305} /* lpfc_sli4_fp_intr_handler */
9306
9307/**
9308 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9309 * @irq: Interrupt number.
9310 * @dev_id: The device context pointer.
9311 *
9312 * This function is the device-level interrupt handler to device with SLI-4
9313 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9314 * interrupt mode is enabled and there is an event in the HBA which requires
9315 * driver attention. This function invokes the slow-path interrupt attention
9316 * handling function and fast-path interrupt attention handling function in
9317 * turn to process the relevant HBA attention events. This function is called
9318 * without any lock held. It gets the hbalock to access and update SLI data
9319 * structures.
9320 *
9321 * This function returns IRQ_HANDLED when interrupt is handled, else it
9322 * returns IRQ_NONE.
9323 **/
9324irqreturn_t
9325lpfc_sli4_intr_handler(int irq, void *dev_id)
9326{
9327 struct lpfc_hba *phba;
9328 irqreturn_t sp_irq_rc, fp_irq_rc;
9329 bool fp_handled = false;
9330 uint32_t fcp_eqidx;
9331
9332 /* Get the driver's phba structure from the dev_id */
9333 phba = (struct lpfc_hba *)dev_id;
9334
9335 if (unlikely(!phba))
9336 return IRQ_NONE;
9337
9338 /*
9339 * Invokes slow-path host attention interrupt handling as appropriate.
9340 */
9341 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9342
9343 /*
9344 * Invoke fast-path host attention interrupt handling as appropriate.
9345 */
9346 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9347 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9348 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9349 if (fp_irq_rc == IRQ_HANDLED)
9350 fp_handled |= true;
9351 }
9352
9353 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9354} /* lpfc_sli4_intr_handler */
9355
9356/**
9357 * lpfc_sli4_queue_free - free a queue structure and associated memory
9358 * @queue: The queue structure to free.
9359 *
9360 * This function frees a queue structure and the DMAable memeory used for
9361 * the host resident queue. This function must be called after destroying the
9362 * queue on the HBA.
9363 **/
9364void
9365lpfc_sli4_queue_free(struct lpfc_queue *queue)
9366{
9367 struct lpfc_dmabuf *dmabuf;
9368
9369 if (!queue)
9370 return;
9371
9372 while (!list_empty(&queue->page_list)) {
9373 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9374 list);
9375 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9376 dmabuf->virt, dmabuf->phys);
9377 kfree(dmabuf);
9378 }
9379 kfree(queue);
9380 return;
9381}
9382
9383/**
9384 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9385 * @phba: The HBA that this queue is being created on.
9386 * @entry_size: The size of each queue entry for this queue.
9387 * @entry count: The number of entries that this queue will handle.
9388 *
9389 * This function allocates a queue structure and the DMAable memory used for
9390 * the host resident queue. This function must be called before creating the
9391 * queue on the HBA.
9392 **/
9393struct lpfc_queue *
9394lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9395 uint32_t entry_count)
9396{
9397 struct lpfc_queue *queue;
9398 struct lpfc_dmabuf *dmabuf;
9399 int x, total_qe_count;
9400 void *dma_pointer;
9401
9402
9403 queue = kzalloc(sizeof(struct lpfc_queue) +
9404 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9405 if (!queue)
9406 return NULL;
9407 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9408 INIT_LIST_HEAD(&queue->list);
9409 INIT_LIST_HEAD(&queue->page_list);
9410 INIT_LIST_HEAD(&queue->child_list);
9411 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9412 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9413 if (!dmabuf)
9414 goto out_fail;
9415 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9416 PAGE_SIZE, &dmabuf->phys,
9417 GFP_KERNEL);
9418 if (!dmabuf->virt) {
9419 kfree(dmabuf);
9420 goto out_fail;
9421 }
James Smartd11e31d2009-06-10 17:23:06 -04009422 memset(dmabuf->virt, 0, PAGE_SIZE);
James Smart4f774512009-05-22 14:52:35 -04009423 dmabuf->buffer_tag = x;
9424 list_add_tail(&dmabuf->list, &queue->page_list);
9425 /* initialize queue's entry array */
9426 dma_pointer = dmabuf->virt;
9427 for (; total_qe_count < entry_count &&
9428 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9429 total_qe_count++, dma_pointer += entry_size) {
9430 queue->qe[total_qe_count].address = dma_pointer;
9431 }
9432 }
9433 queue->entry_size = entry_size;
9434 queue->entry_count = entry_count;
9435 queue->phba = phba;
9436
9437 return queue;
9438out_fail:
9439 lpfc_sli4_queue_free(queue);
9440 return NULL;
9441}
9442
9443/**
9444 * lpfc_eq_create - Create an Event Queue on the HBA
9445 * @phba: HBA structure that indicates port to create a queue on.
9446 * @eq: The queue structure to use to create the event queue.
9447 * @imax: The maximum interrupt per second limit.
9448 *
9449 * This function creates an event queue, as detailed in @eq, on a port,
9450 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9451 *
9452 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9453 * is used to get the entry count and entry size that are necessary to
9454 * determine the number of pages to allocate and use for this queue. This
9455 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9456 * event queue. This function is asynchronous and will wait for the mailbox
9457 * command to finish before continuing.
9458 *
9459 * On success this function will return a zero. If unable to allocate enough
9460 * memory this function will return ENOMEM. If the queue create mailbox command
9461 * fails this function will return ENXIO.
9462 **/
9463uint32_t
9464lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9465{
9466 struct lpfc_mbx_eq_create *eq_create;
9467 LPFC_MBOXQ_t *mbox;
9468 int rc, length, status = 0;
9469 struct lpfc_dmabuf *dmabuf;
9470 uint32_t shdr_status, shdr_add_status;
9471 union lpfc_sli4_cfg_shdr *shdr;
9472 uint16_t dmult;
9473
9474 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9475 if (!mbox)
9476 return -ENOMEM;
9477 length = (sizeof(struct lpfc_mbx_eq_create) -
9478 sizeof(struct lpfc_sli4_cfg_mhdr));
9479 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9480 LPFC_MBOX_OPCODE_EQ_CREATE,
9481 length, LPFC_SLI4_MBX_EMBED);
9482 eq_create = &mbox->u.mqe.un.eq_create;
9483 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9484 eq->page_count);
9485 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9486 LPFC_EQE_SIZE);
9487 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9488 /* Calculate delay multiper from maximum interrupt per second */
9489 dmult = LPFC_DMULT_CONST/imax - 1;
9490 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9491 dmult);
9492 switch (eq->entry_count) {
9493 default:
9494 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9495 "0360 Unsupported EQ count. (%d)\n",
9496 eq->entry_count);
9497 if (eq->entry_count < 256)
9498 return -EINVAL;
9499 /* otherwise default to smallest count (drop through) */
9500 case 256:
9501 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9502 LPFC_EQ_CNT_256);
9503 break;
9504 case 512:
9505 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9506 LPFC_EQ_CNT_512);
9507 break;
9508 case 1024:
9509 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9510 LPFC_EQ_CNT_1024);
9511 break;
9512 case 2048:
9513 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9514 LPFC_EQ_CNT_2048);
9515 break;
9516 case 4096:
9517 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9518 LPFC_EQ_CNT_4096);
9519 break;
9520 }
9521 list_for_each_entry(dmabuf, &eq->page_list, list) {
9522 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9523 putPaddrLow(dmabuf->phys);
9524 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9525 putPaddrHigh(dmabuf->phys);
9526 }
9527 mbox->vport = phba->pport;
9528 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9529 mbox->context1 = NULL;
9530 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9531 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9532 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9533 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9534 if (shdr_status || shdr_add_status || rc) {
9535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9536 "2500 EQ_CREATE mailbox failed with "
9537 "status x%x add_status x%x, mbx status x%x\n",
9538 shdr_status, shdr_add_status, rc);
9539 status = -ENXIO;
9540 }
9541 eq->type = LPFC_EQ;
9542 eq->subtype = LPFC_NONE;
9543 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9544 if (eq->queue_id == 0xFFFF)
9545 status = -ENXIO;
9546 eq->host_index = 0;
9547 eq->hba_index = 0;
9548
James Smart8fa38512009-07-19 10:01:03 -04009549 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009550 return status;
9551}
9552
9553/**
9554 * lpfc_cq_create - Create a Completion Queue on the HBA
9555 * @phba: HBA structure that indicates port to create a queue on.
9556 * @cq: The queue structure to use to create the completion queue.
9557 * @eq: The event queue to bind this completion queue to.
9558 *
9559 * This function creates a completion queue, as detailed in @wq, on a port,
9560 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9561 *
9562 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9563 * is used to get the entry count and entry size that are necessary to
9564 * determine the number of pages to allocate and use for this queue. The @eq
9565 * is used to indicate which event queue to bind this completion queue to. This
9566 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9567 * completion queue. This function is asynchronous and will wait for the mailbox
9568 * command to finish before continuing.
9569 *
9570 * On success this function will return a zero. If unable to allocate enough
9571 * memory this function will return ENOMEM. If the queue create mailbox command
9572 * fails this function will return ENXIO.
9573 **/
9574uint32_t
9575lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9576 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9577{
9578 struct lpfc_mbx_cq_create *cq_create;
9579 struct lpfc_dmabuf *dmabuf;
9580 LPFC_MBOXQ_t *mbox;
9581 int rc, length, status = 0;
9582 uint32_t shdr_status, shdr_add_status;
9583 union lpfc_sli4_cfg_shdr *shdr;
9584
9585 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9586 if (!mbox)
9587 return -ENOMEM;
9588 length = (sizeof(struct lpfc_mbx_cq_create) -
9589 sizeof(struct lpfc_sli4_cfg_mhdr));
9590 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9591 LPFC_MBOX_OPCODE_CQ_CREATE,
9592 length, LPFC_SLI4_MBX_EMBED);
9593 cq_create = &mbox->u.mqe.un.cq_create;
9594 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9595 cq->page_count);
9596 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9597 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9598 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9599 switch (cq->entry_count) {
9600 default:
9601 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9602 "0361 Unsupported CQ count. (%d)\n",
9603 cq->entry_count);
9604 if (cq->entry_count < 256)
9605 return -EINVAL;
9606 /* otherwise default to smallest count (drop through) */
9607 case 256:
9608 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9609 LPFC_CQ_CNT_256);
9610 break;
9611 case 512:
9612 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9613 LPFC_CQ_CNT_512);
9614 break;
9615 case 1024:
9616 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9617 LPFC_CQ_CNT_1024);
9618 break;
9619 }
9620 list_for_each_entry(dmabuf, &cq->page_list, list) {
9621 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9622 putPaddrLow(dmabuf->phys);
9623 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9624 putPaddrHigh(dmabuf->phys);
9625 }
9626 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9627
9628 /* The IOCTL status is embedded in the mailbox subheader. */
9629 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9630 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9631 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9632 if (shdr_status || shdr_add_status || rc) {
9633 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9634 "2501 CQ_CREATE mailbox failed with "
9635 "status x%x add_status x%x, mbx status x%x\n",
9636 shdr_status, shdr_add_status, rc);
9637 status = -ENXIO;
9638 goto out;
9639 }
9640 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9641 if (cq->queue_id == 0xFFFF) {
9642 status = -ENXIO;
9643 goto out;
9644 }
9645 /* link the cq onto the parent eq child list */
9646 list_add_tail(&cq->list, &eq->child_list);
9647 /* Set up completion queue's type and subtype */
9648 cq->type = type;
9649 cq->subtype = subtype;
9650 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9651 cq->host_index = 0;
9652 cq->hba_index = 0;
James Smart4f774512009-05-22 14:52:35 -04009653
James Smart8fa38512009-07-19 10:01:03 -04009654out:
9655 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009656 return status;
9657}
9658
9659/**
James Smart04c68492009-05-22 14:52:52 -04009660 * lpfc_mq_create - Create a mailbox Queue on the HBA
9661 * @phba: HBA structure that indicates port to create a queue on.
9662 * @mq: The queue structure to use to create the mailbox queue.
9663 *
9664 * This function creates a mailbox queue, as detailed in @mq, on a port,
9665 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9666 *
9667 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9668 * is used to get the entry count and entry size that are necessary to
9669 * determine the number of pages to allocate and use for this queue. This
9670 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9671 * mailbox queue. This function is asynchronous and will wait for the mailbox
9672 * command to finish before continuing.
9673 *
9674 * On success this function will return a zero. If unable to allocate enough
9675 * memory this function will return ENOMEM. If the queue create mailbox command
9676 * fails this function will return ENXIO.
9677 **/
9678uint32_t
9679lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9680 struct lpfc_queue *cq, uint32_t subtype)
9681{
9682 struct lpfc_mbx_mq_create *mq_create;
9683 struct lpfc_dmabuf *dmabuf;
9684 LPFC_MBOXQ_t *mbox;
9685 int rc, length, status = 0;
9686 uint32_t shdr_status, shdr_add_status;
9687 union lpfc_sli4_cfg_shdr *shdr;
9688
9689 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9690 if (!mbox)
9691 return -ENOMEM;
9692 length = (sizeof(struct lpfc_mbx_mq_create) -
9693 sizeof(struct lpfc_sli4_cfg_mhdr));
9694 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9695 LPFC_MBOX_OPCODE_MQ_CREATE,
9696 length, LPFC_SLI4_MBX_EMBED);
9697 mq_create = &mbox->u.mqe.un.mq_create;
9698 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9699 mq->page_count);
9700 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9701 cq->queue_id);
9702 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9703 switch (mq->entry_count) {
9704 default:
9705 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9706 "0362 Unsupported MQ count. (%d)\n",
9707 mq->entry_count);
9708 if (mq->entry_count < 16)
9709 return -EINVAL;
9710 /* otherwise default to smallest count (drop through) */
9711 case 16:
9712 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9713 LPFC_MQ_CNT_16);
9714 break;
9715 case 32:
9716 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9717 LPFC_MQ_CNT_32);
9718 break;
9719 case 64:
9720 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9721 LPFC_MQ_CNT_64);
9722 break;
9723 case 128:
9724 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9725 LPFC_MQ_CNT_128);
9726 break;
9727 }
9728 list_for_each_entry(dmabuf, &mq->page_list, list) {
9729 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9730 putPaddrLow(dmabuf->phys);
9731 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9732 putPaddrHigh(dmabuf->phys);
9733 }
9734 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9735 /* The IOCTL status is embedded in the mailbox subheader. */
9736 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9737 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9738 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9739 if (shdr_status || shdr_add_status || rc) {
9740 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9741 "2502 MQ_CREATE mailbox failed with "
9742 "status x%x add_status x%x, mbx status x%x\n",
9743 shdr_status, shdr_add_status, rc);
9744 status = -ENXIO;
9745 goto out;
9746 }
9747 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9748 if (mq->queue_id == 0xFFFF) {
9749 status = -ENXIO;
9750 goto out;
9751 }
9752 mq->type = LPFC_MQ;
9753 mq->subtype = subtype;
9754 mq->host_index = 0;
9755 mq->hba_index = 0;
9756
9757 /* link the mq onto the parent cq child list */
9758 list_add_tail(&mq->list, &cq->child_list);
9759out:
James Smart8fa38512009-07-19 10:01:03 -04009760 mempool_free(mbox, phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -04009761 return status;
9762}
9763
9764/**
James Smart4f774512009-05-22 14:52:35 -04009765 * lpfc_wq_create - Create a Work Queue on the HBA
9766 * @phba: HBA structure that indicates port to create a queue on.
9767 * @wq: The queue structure to use to create the work queue.
9768 * @cq: The completion queue to bind this work queue to.
9769 * @subtype: The subtype of the work queue indicating its functionality.
9770 *
9771 * This function creates a work queue, as detailed in @wq, on a port, described
9772 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9773 *
9774 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9775 * is used to get the entry count and entry size that are necessary to
9776 * determine the number of pages to allocate and use for this queue. The @cq
9777 * is used to indicate which completion queue to bind this work queue to. This
9778 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9779 * work queue. This function is asynchronous and will wait for the mailbox
9780 * command to finish before continuing.
9781 *
9782 * On success this function will return a zero. If unable to allocate enough
9783 * memory this function will return ENOMEM. If the queue create mailbox command
9784 * fails this function will return ENXIO.
9785 **/
9786uint32_t
9787lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9788 struct lpfc_queue *cq, uint32_t subtype)
9789{
9790 struct lpfc_mbx_wq_create *wq_create;
9791 struct lpfc_dmabuf *dmabuf;
9792 LPFC_MBOXQ_t *mbox;
9793 int rc, length, status = 0;
9794 uint32_t shdr_status, shdr_add_status;
9795 union lpfc_sli4_cfg_shdr *shdr;
9796
9797 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9798 if (!mbox)
9799 return -ENOMEM;
9800 length = (sizeof(struct lpfc_mbx_wq_create) -
9801 sizeof(struct lpfc_sli4_cfg_mhdr));
9802 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9803 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9804 length, LPFC_SLI4_MBX_EMBED);
9805 wq_create = &mbox->u.mqe.un.wq_create;
9806 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9807 wq->page_count);
9808 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9809 cq->queue_id);
9810 list_for_each_entry(dmabuf, &wq->page_list, list) {
9811 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9812 putPaddrLow(dmabuf->phys);
9813 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9814 putPaddrHigh(dmabuf->phys);
9815 }
9816 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9817 /* The IOCTL status is embedded in the mailbox subheader. */
9818 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9819 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9820 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9821 if (shdr_status || shdr_add_status || rc) {
9822 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9823 "2503 WQ_CREATE mailbox failed with "
9824 "status x%x add_status x%x, mbx status x%x\n",
9825 shdr_status, shdr_add_status, rc);
9826 status = -ENXIO;
9827 goto out;
9828 }
9829 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9830 if (wq->queue_id == 0xFFFF) {
9831 status = -ENXIO;
9832 goto out;
9833 }
9834 wq->type = LPFC_WQ;
9835 wq->subtype = subtype;
9836 wq->host_index = 0;
9837 wq->hba_index = 0;
9838
9839 /* link the wq onto the parent cq child list */
9840 list_add_tail(&wq->list, &cq->child_list);
9841out:
James Smart8fa38512009-07-19 10:01:03 -04009842 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -04009843 return status;
9844}
9845
9846/**
9847 * lpfc_rq_create - Create a Receive Queue on the HBA
9848 * @phba: HBA structure that indicates port to create a queue on.
9849 * @hrq: The queue structure to use to create the header receive queue.
9850 * @drq: The queue structure to use to create the data receive queue.
9851 * @cq: The completion queue to bind this work queue to.
9852 *
9853 * This function creates a receive buffer queue pair , as detailed in @hrq and
9854 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9855 * to the HBA.
9856 *
9857 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9858 * struct is used to get the entry count that is necessary to determine the
9859 * number of pages to use for this queue. The @cq is used to indicate which
9860 * completion queue to bind received buffers that are posted to these queues to.
9861 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9862 * receive queue pair. This function is asynchronous and will wait for the
9863 * mailbox command to finish before continuing.
9864 *
9865 * On success this function will return a zero. If unable to allocate enough
9866 * memory this function will return ENOMEM. If the queue create mailbox command
9867 * fails this function will return ENXIO.
9868 **/
9869uint32_t
9870lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9871 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9872{
9873 struct lpfc_mbx_rq_create *rq_create;
9874 struct lpfc_dmabuf *dmabuf;
9875 LPFC_MBOXQ_t *mbox;
9876 int rc, length, status = 0;
9877 uint32_t shdr_status, shdr_add_status;
9878 union lpfc_sli4_cfg_shdr *shdr;
9879
9880 if (hrq->entry_count != drq->entry_count)
9881 return -EINVAL;
9882 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9883 if (!mbox)
9884 return -ENOMEM;
9885 length = (sizeof(struct lpfc_mbx_rq_create) -
9886 sizeof(struct lpfc_sli4_cfg_mhdr));
9887 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9888 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9889 length, LPFC_SLI4_MBX_EMBED);
9890 rq_create = &mbox->u.mqe.un.rq_create;
9891 switch (hrq->entry_count) {
9892 default:
9893 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9894 "2535 Unsupported RQ count. (%d)\n",
9895 hrq->entry_count);
9896 if (hrq->entry_count < 512)
9897 return -EINVAL;
9898 /* otherwise default to smallest count (drop through) */
9899 case 512:
9900 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9901 LPFC_RQ_RING_SIZE_512);
9902 break;
9903 case 1024:
9904 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9905 LPFC_RQ_RING_SIZE_1024);
9906 break;
9907 case 2048:
9908 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9909 LPFC_RQ_RING_SIZE_2048);
9910 break;
9911 case 4096:
9912 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9913 LPFC_RQ_RING_SIZE_4096);
9914 break;
9915 }
9916 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9917 cq->queue_id);
9918 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9919 hrq->page_count);
9920 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9921 LPFC_HDR_BUF_SIZE);
9922 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9923 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9924 putPaddrLow(dmabuf->phys);
9925 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9926 putPaddrHigh(dmabuf->phys);
9927 }
9928 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9929 /* The IOCTL status is embedded in the mailbox subheader. */
9930 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9931 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9932 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9933 if (shdr_status || shdr_add_status || rc) {
9934 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9935 "2504 RQ_CREATE mailbox failed with "
9936 "status x%x add_status x%x, mbx status x%x\n",
9937 shdr_status, shdr_add_status, rc);
9938 status = -ENXIO;
9939 goto out;
9940 }
9941 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9942 if (hrq->queue_id == 0xFFFF) {
9943 status = -ENXIO;
9944 goto out;
9945 }
9946 hrq->type = LPFC_HRQ;
9947 hrq->subtype = subtype;
9948 hrq->host_index = 0;
9949 hrq->hba_index = 0;
9950
9951 /* now create the data queue */
9952 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9953 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9954 length, LPFC_SLI4_MBX_EMBED);
9955 switch (drq->entry_count) {
9956 default:
9957 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9958 "2536 Unsupported RQ count. (%d)\n",
9959 drq->entry_count);
9960 if (drq->entry_count < 512)
9961 return -EINVAL;
9962 /* otherwise default to smallest count (drop through) */
9963 case 512:
9964 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9965 LPFC_RQ_RING_SIZE_512);
9966 break;
9967 case 1024:
9968 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9969 LPFC_RQ_RING_SIZE_1024);
9970 break;
9971 case 2048:
9972 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9973 LPFC_RQ_RING_SIZE_2048);
9974 break;
9975 case 4096:
9976 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9977 LPFC_RQ_RING_SIZE_4096);
9978 break;
9979 }
9980 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9981 cq->queue_id);
9982 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9983 drq->page_count);
9984 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9985 LPFC_DATA_BUF_SIZE);
9986 list_for_each_entry(dmabuf, &drq->page_list, list) {
9987 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9988 putPaddrLow(dmabuf->phys);
9989 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9990 putPaddrHigh(dmabuf->phys);
9991 }
9992 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9993 /* The IOCTL status is embedded in the mailbox subheader. */
9994 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9995 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9996 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9997 if (shdr_status || shdr_add_status || rc) {
9998 status = -ENXIO;
9999 goto out;
10000 }
10001 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10002 if (drq->queue_id == 0xFFFF) {
10003 status = -ENXIO;
10004 goto out;
10005 }
10006 drq->type = LPFC_DRQ;
10007 drq->subtype = subtype;
10008 drq->host_index = 0;
10009 drq->hba_index = 0;
10010
10011 /* link the header and data RQs onto the parent cq child list */
10012 list_add_tail(&hrq->list, &cq->child_list);
10013 list_add_tail(&drq->list, &cq->child_list);
10014
10015out:
James Smart8fa38512009-07-19 10:01:03 -040010016 mempool_free(mbox, phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010017 return status;
10018}
10019
10020/**
10021 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10022 * @eq: The queue structure associated with the queue to destroy.
10023 *
10024 * This function destroys a queue, as detailed in @eq by sending an mailbox
10025 * command, specific to the type of queue, to the HBA.
10026 *
10027 * The @eq struct is used to get the queue ID of the queue to destroy.
10028 *
10029 * On success this function will return a zero. If the queue destroy mailbox
10030 * command fails this function will return ENXIO.
10031 **/
10032uint32_t
10033lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10034{
10035 LPFC_MBOXQ_t *mbox;
10036 int rc, length, status = 0;
10037 uint32_t shdr_status, shdr_add_status;
10038 union lpfc_sli4_cfg_shdr *shdr;
10039
10040 if (!eq)
10041 return -ENODEV;
10042 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10043 if (!mbox)
10044 return -ENOMEM;
10045 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10046 sizeof(struct lpfc_sli4_cfg_mhdr));
10047 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10048 LPFC_MBOX_OPCODE_EQ_DESTROY,
10049 length, LPFC_SLI4_MBX_EMBED);
10050 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10051 eq->queue_id);
10052 mbox->vport = eq->phba->pport;
10053 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10054
10055 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10056 /* The IOCTL status is embedded in the mailbox subheader. */
10057 shdr = (union lpfc_sli4_cfg_shdr *)
10058 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10059 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10060 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10061 if (shdr_status || shdr_add_status || rc) {
10062 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10063 "2505 EQ_DESTROY mailbox failed with "
10064 "status x%x add_status x%x, mbx status x%x\n",
10065 shdr_status, shdr_add_status, rc);
10066 status = -ENXIO;
10067 }
10068
10069 /* Remove eq from any list */
10070 list_del_init(&eq->list);
James Smart8fa38512009-07-19 10:01:03 -040010071 mempool_free(mbox, eq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010072 return status;
10073}
10074
10075/**
10076 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10077 * @cq: The queue structure associated with the queue to destroy.
10078 *
10079 * This function destroys a queue, as detailed in @cq by sending an mailbox
10080 * command, specific to the type of queue, to the HBA.
10081 *
10082 * The @cq struct is used to get the queue ID of the queue to destroy.
10083 *
10084 * On success this function will return a zero. If the queue destroy mailbox
10085 * command fails this function will return ENXIO.
10086 **/
10087uint32_t
10088lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10089{
10090 LPFC_MBOXQ_t *mbox;
10091 int rc, length, status = 0;
10092 uint32_t shdr_status, shdr_add_status;
10093 union lpfc_sli4_cfg_shdr *shdr;
10094
10095 if (!cq)
10096 return -ENODEV;
10097 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10098 if (!mbox)
10099 return -ENOMEM;
10100 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10101 sizeof(struct lpfc_sli4_cfg_mhdr));
10102 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10103 LPFC_MBOX_OPCODE_CQ_DESTROY,
10104 length, LPFC_SLI4_MBX_EMBED);
10105 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10106 cq->queue_id);
10107 mbox->vport = cq->phba->pport;
10108 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10109 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10110 /* The IOCTL status is embedded in the mailbox subheader. */
10111 shdr = (union lpfc_sli4_cfg_shdr *)
10112 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10113 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10114 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10115 if (shdr_status || shdr_add_status || rc) {
10116 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10117 "2506 CQ_DESTROY mailbox failed with "
10118 "status x%x add_status x%x, mbx status x%x\n",
10119 shdr_status, shdr_add_status, rc);
10120 status = -ENXIO;
10121 }
10122 /* Remove cq from any list */
10123 list_del_init(&cq->list);
James Smart8fa38512009-07-19 10:01:03 -040010124 mempool_free(mbox, cq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010125 return status;
10126}
10127
10128/**
James Smart04c68492009-05-22 14:52:52 -040010129 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10130 * @qm: The queue structure associated with the queue to destroy.
10131 *
10132 * This function destroys a queue, as detailed in @mq by sending an mailbox
10133 * command, specific to the type of queue, to the HBA.
10134 *
10135 * The @mq struct is used to get the queue ID of the queue to destroy.
10136 *
10137 * On success this function will return a zero. If the queue destroy mailbox
10138 * command fails this function will return ENXIO.
10139 **/
10140uint32_t
10141lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10142{
10143 LPFC_MBOXQ_t *mbox;
10144 int rc, length, status = 0;
10145 uint32_t shdr_status, shdr_add_status;
10146 union lpfc_sli4_cfg_shdr *shdr;
10147
10148 if (!mq)
10149 return -ENODEV;
10150 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10151 if (!mbox)
10152 return -ENOMEM;
10153 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10154 sizeof(struct lpfc_sli4_cfg_mhdr));
10155 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10156 LPFC_MBOX_OPCODE_MQ_DESTROY,
10157 length, LPFC_SLI4_MBX_EMBED);
10158 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10159 mq->queue_id);
10160 mbox->vport = mq->phba->pport;
10161 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10162 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10163 /* The IOCTL status is embedded in the mailbox subheader. */
10164 shdr = (union lpfc_sli4_cfg_shdr *)
10165 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10166 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10167 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10168 if (shdr_status || shdr_add_status || rc) {
10169 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10170 "2507 MQ_DESTROY mailbox failed with "
10171 "status x%x add_status x%x, mbx status x%x\n",
10172 shdr_status, shdr_add_status, rc);
10173 status = -ENXIO;
10174 }
10175 /* Remove mq from any list */
10176 list_del_init(&mq->list);
James Smart8fa38512009-07-19 10:01:03 -040010177 mempool_free(mbox, mq->phba->mbox_mem_pool);
James Smart04c68492009-05-22 14:52:52 -040010178 return status;
10179}
10180
10181/**
James Smart4f774512009-05-22 14:52:35 -040010182 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10183 * @wq: The queue structure associated with the queue to destroy.
10184 *
10185 * This function destroys a queue, as detailed in @wq by sending an mailbox
10186 * command, specific to the type of queue, to the HBA.
10187 *
10188 * The @wq struct is used to get the queue ID of the queue to destroy.
10189 *
10190 * On success this function will return a zero. If the queue destroy mailbox
10191 * command fails this function will return ENXIO.
10192 **/
10193uint32_t
10194lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10195{
10196 LPFC_MBOXQ_t *mbox;
10197 int rc, length, status = 0;
10198 uint32_t shdr_status, shdr_add_status;
10199 union lpfc_sli4_cfg_shdr *shdr;
10200
10201 if (!wq)
10202 return -ENODEV;
10203 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10204 if (!mbox)
10205 return -ENOMEM;
10206 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10207 sizeof(struct lpfc_sli4_cfg_mhdr));
10208 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10209 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10210 length, LPFC_SLI4_MBX_EMBED);
10211 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10212 wq->queue_id);
10213 mbox->vport = wq->phba->pport;
10214 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10215 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10216 shdr = (union lpfc_sli4_cfg_shdr *)
10217 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10218 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10219 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10220 if (shdr_status || shdr_add_status || rc) {
10221 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10222 "2508 WQ_DESTROY mailbox failed with "
10223 "status x%x add_status x%x, mbx status x%x\n",
10224 shdr_status, shdr_add_status, rc);
10225 status = -ENXIO;
10226 }
10227 /* Remove wq from any list */
10228 list_del_init(&wq->list);
James Smart8fa38512009-07-19 10:01:03 -040010229 mempool_free(mbox, wq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010230 return status;
10231}
10232
10233/**
10234 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10235 * @rq: The queue structure associated with the queue to destroy.
10236 *
10237 * This function destroys a queue, as detailed in @rq by sending an mailbox
10238 * command, specific to the type of queue, to the HBA.
10239 *
10240 * The @rq struct is used to get the queue ID of the queue to destroy.
10241 *
10242 * On success this function will return a zero. If the queue destroy mailbox
10243 * command fails this function will return ENXIO.
10244 **/
10245uint32_t
10246lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10247 struct lpfc_queue *drq)
10248{
10249 LPFC_MBOXQ_t *mbox;
10250 int rc, length, status = 0;
10251 uint32_t shdr_status, shdr_add_status;
10252 union lpfc_sli4_cfg_shdr *shdr;
10253
10254 if (!hrq || !drq)
10255 return -ENODEV;
10256 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10257 if (!mbox)
10258 return -ENOMEM;
10259 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10260 sizeof(struct mbox_header));
10261 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10262 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10263 length, LPFC_SLI4_MBX_EMBED);
10264 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10265 hrq->queue_id);
10266 mbox->vport = hrq->phba->pport;
10267 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10268 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10269 /* The IOCTL status is embedded in the mailbox subheader. */
10270 shdr = (union lpfc_sli4_cfg_shdr *)
10271 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10272 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10273 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10274 if (shdr_status || shdr_add_status || rc) {
10275 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10276 "2509 RQ_DESTROY mailbox failed with "
10277 "status x%x add_status x%x, mbx status x%x\n",
10278 shdr_status, shdr_add_status, rc);
10279 if (rc != MBX_TIMEOUT)
10280 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10281 return -ENXIO;
10282 }
10283 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10284 drq->queue_id);
10285 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10286 shdr = (union lpfc_sli4_cfg_shdr *)
10287 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10288 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10289 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10290 if (shdr_status || shdr_add_status || rc) {
10291 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10292 "2510 RQ_DESTROY mailbox failed with "
10293 "status x%x add_status x%x, mbx status x%x\n",
10294 shdr_status, shdr_add_status, rc);
10295 status = -ENXIO;
10296 }
10297 list_del_init(&hrq->list);
10298 list_del_init(&drq->list);
James Smart8fa38512009-07-19 10:01:03 -040010299 mempool_free(mbox, hrq->phba->mbox_mem_pool);
James Smart4f774512009-05-22 14:52:35 -040010300 return status;
10301}
10302
10303/**
10304 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10305 * @phba: The virtual port for which this call being executed.
10306 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10307 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10308 * @xritag: the xritag that ties this io to the SGL pages.
10309 *
10310 * This routine will post the sgl pages for the IO that has the xritag
10311 * that is in the iocbq structure. The xritag is assigned during iocbq
10312 * creation and persists for as long as the driver is loaded.
10313 * if the caller has fewer than 256 scatter gather segments to map then
10314 * pdma_phys_addr1 should be 0.
10315 * If the caller needs to map more than 256 scatter gather segment then
10316 * pdma_phys_addr1 should be a valid physical address.
10317 * physical address for SGLs must be 64 byte aligned.
10318 * If you are going to map 2 SGL's then the first one must have 256 entries
10319 * the second sgl can have between 1 and 256 entries.
10320 *
10321 * Return codes:
10322 * 0 - Success
10323 * -ENXIO, -ENOMEM - Failure
10324 **/
10325int
10326lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10327 dma_addr_t pdma_phys_addr0,
10328 dma_addr_t pdma_phys_addr1,
10329 uint16_t xritag)
10330{
10331 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10332 LPFC_MBOXQ_t *mbox;
10333 int rc;
10334 uint32_t shdr_status, shdr_add_status;
10335 union lpfc_sli4_cfg_shdr *shdr;
10336
10337 if (xritag == NO_XRI) {
10338 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10339 "0364 Invalid param:\n");
10340 return -EINVAL;
10341 }
10342
10343 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10344 if (!mbox)
10345 return -ENOMEM;
10346
10347 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10348 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10349 sizeof(struct lpfc_mbx_post_sgl_pages) -
10350 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10351
10352 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10353 &mbox->u.mqe.un.post_sgl_pages;
10354 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10355 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10356
10357 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10358 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10359 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10360 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10361
10362 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10363 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10364 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10365 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10366 if (!phba->sli4_hba.intr_enable)
10367 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10368 else
10369 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10370 /* The IOCTL status is embedded in the mailbox subheader. */
10371 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10372 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10373 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10374 if (rc != MBX_TIMEOUT)
10375 mempool_free(mbox, phba->mbox_mem_pool);
10376 if (shdr_status || shdr_add_status || rc) {
10377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10378 "2511 POST_SGL mailbox failed with "
10379 "status x%x add_status x%x, mbx status x%x\n",
10380 shdr_status, shdr_add_status, rc);
10381 rc = -ENXIO;
10382 }
10383 return 0;
10384}
10385/**
10386 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10387 * @phba: The virtual port for which this call being executed.
10388 *
10389 * This routine will remove all of the sgl pages registered with the hba.
10390 *
10391 * Return codes:
10392 * 0 - Success
10393 * -ENXIO, -ENOMEM - Failure
10394 **/
10395int
10396lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10397{
10398 LPFC_MBOXQ_t *mbox;
10399 int rc;
10400 uint32_t shdr_status, shdr_add_status;
10401 union lpfc_sli4_cfg_shdr *shdr;
10402
10403 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10404 if (!mbox)
10405 return -ENOMEM;
10406
10407 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10408 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10409 LPFC_SLI4_MBX_EMBED);
10410 if (!phba->sli4_hba.intr_enable)
10411 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10412 else
10413 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10414 /* The IOCTL status is embedded in the mailbox subheader. */
10415 shdr = (union lpfc_sli4_cfg_shdr *)
10416 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10417 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10418 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10419 if (rc != MBX_TIMEOUT)
10420 mempool_free(mbox, phba->mbox_mem_pool);
10421 if (shdr_status || shdr_add_status || rc) {
10422 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10423 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10424 "status x%x add_status x%x, mbx status x%x\n",
10425 shdr_status, shdr_add_status, rc);
10426 rc = -ENXIO;
10427 }
10428 return rc;
10429}
10430
10431/**
10432 * lpfc_sli4_next_xritag - Get an xritag for the io
10433 * @phba: Pointer to HBA context object.
10434 *
10435 * This function gets an xritag for the iocb. If there is no unused xritag
10436 * it will return 0xffff.
10437 * The function returns the allocated xritag if successful, else returns zero.
10438 * Zero is not a valid xritag.
10439 * The caller is not required to hold any lock.
10440 **/
10441uint16_t
10442lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10443{
10444 uint16_t xritag;
10445
10446 spin_lock_irq(&phba->hbalock);
10447 xritag = phba->sli4_hba.next_xri;
10448 if ((xritag != (uint16_t) -1) && xritag <
10449 (phba->sli4_hba.max_cfg_param.max_xri
10450 + phba->sli4_hba.max_cfg_param.xri_base)) {
10451 phba->sli4_hba.next_xri++;
10452 phba->sli4_hba.max_cfg_param.xri_used++;
10453 spin_unlock_irq(&phba->hbalock);
10454 return xritag;
10455 }
10456 spin_unlock_irq(&phba->hbalock);
10457
10458 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10459 "2004 Failed to allocate XRI.last XRITAG is %d"
10460 " Max XRI is %d, Used XRI is %d\n",
10461 phba->sli4_hba.next_xri,
10462 phba->sli4_hba.max_cfg_param.max_xri,
10463 phba->sli4_hba.max_cfg_param.xri_used);
10464 return -1;
10465}
10466
10467/**
10468 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10469 * @phba: pointer to lpfc hba data structure.
10470 *
10471 * This routine is invoked to post a block of driver's sgl pages to the
10472 * HBA using non-embedded mailbox command. No Lock is held. This routine
10473 * is only called when the driver is loading and after all IO has been
10474 * stopped.
10475 **/
10476int
10477lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10478{
10479 struct lpfc_sglq *sglq_entry;
10480 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10481 struct sgl_page_pairs *sgl_pg_pairs;
10482 void *viraddr;
10483 LPFC_MBOXQ_t *mbox;
10484 uint32_t reqlen, alloclen, pg_pairs;
10485 uint32_t mbox_tmo;
10486 uint16_t xritag_start = 0;
10487 int els_xri_cnt, rc = 0;
10488 uint32_t shdr_status, shdr_add_status;
10489 union lpfc_sli4_cfg_shdr *shdr;
10490
10491 /* The number of sgls to be posted */
10492 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10493
10494 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10495 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10496 if (reqlen > PAGE_SIZE) {
10497 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10498 "2559 Block sgl registration required DMA "
10499 "size (%d) great than a page\n", reqlen);
10500 return -ENOMEM;
10501 }
10502 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10503 if (!mbox) {
10504 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10505 "2560 Failed to allocate mbox cmd memory\n");
10506 return -ENOMEM;
10507 }
10508
10509 /* Allocate DMA memory and set up the non-embedded mailbox command */
10510 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10511 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10512 LPFC_SLI4_MBX_NEMBED);
10513
10514 if (alloclen < reqlen) {
10515 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10516 "0285 Allocated DMA memory size (%d) is "
10517 "less than the requested DMA memory "
10518 "size (%d)\n", alloclen, reqlen);
10519 lpfc_sli4_mbox_cmd_free(phba, mbox);
10520 return -ENOMEM;
10521 }
10522
10523 /* Get the first SGE entry from the non-embedded DMA memory */
10524 if (unlikely(!mbox->sge_array)) {
10525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10526 "2525 Failed to get the non-embedded SGE "
10527 "virtual address\n");
10528 lpfc_sli4_mbox_cmd_free(phba, mbox);
10529 return -ENOMEM;
10530 }
10531 viraddr = mbox->sge_array->addr[0];
10532
10533 /* Set up the SGL pages in the non-embedded DMA pages */
10534 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10535 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10536
10537 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10538 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10539 /* Set up the sge entry */
10540 sgl_pg_pairs->sgl_pg0_addr_lo =
10541 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10542 sgl_pg_pairs->sgl_pg0_addr_hi =
10543 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10544 sgl_pg_pairs->sgl_pg1_addr_lo =
10545 cpu_to_le32(putPaddrLow(0));
10546 sgl_pg_pairs->sgl_pg1_addr_hi =
10547 cpu_to_le32(putPaddrHigh(0));
10548 /* Keep the first xritag on the list */
10549 if (pg_pairs == 0)
10550 xritag_start = sglq_entry->sli4_xritag;
10551 sgl_pg_pairs++;
10552 }
10553 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10554 pg_pairs = (pg_pairs > 0) ? (pg_pairs - 1) : pg_pairs;
10555 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10556 /* Perform endian conversion if necessary */
10557 sgl->word0 = cpu_to_le32(sgl->word0);
10558
10559 if (!phba->sli4_hba.intr_enable)
10560 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10561 else {
10562 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10563 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10564 }
10565 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10566 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10567 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10568 if (rc != MBX_TIMEOUT)
10569 lpfc_sli4_mbox_cmd_free(phba, mbox);
10570 if (shdr_status || shdr_add_status || rc) {
10571 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10572 "2513 POST_SGL_BLOCK mailbox command failed "
10573 "status x%x add_status x%x mbx status x%x\n",
10574 shdr_status, shdr_add_status, rc);
10575 rc = -ENXIO;
10576 }
10577 return rc;
10578}
10579
10580/**
10581 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10582 * @phba: pointer to lpfc hba data structure.
10583 * @sblist: pointer to scsi buffer list.
10584 * @count: number of scsi buffers on the list.
10585 *
10586 * This routine is invoked to post a block of @count scsi sgl pages from a
10587 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10588 * No Lock is held.
10589 *
10590 **/
10591int
10592lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10593 int cnt)
10594{
10595 struct lpfc_scsi_buf *psb;
10596 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10597 struct sgl_page_pairs *sgl_pg_pairs;
10598 void *viraddr;
10599 LPFC_MBOXQ_t *mbox;
10600 uint32_t reqlen, alloclen, pg_pairs;
10601 uint32_t mbox_tmo;
10602 uint16_t xritag_start = 0;
10603 int rc = 0;
10604 uint32_t shdr_status, shdr_add_status;
10605 dma_addr_t pdma_phys_bpl1;
10606 union lpfc_sli4_cfg_shdr *shdr;
10607
10608 /* Calculate the requested length of the dma memory */
10609 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10610 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10611 if (reqlen > PAGE_SIZE) {
10612 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10613 "0217 Block sgl registration required DMA "
10614 "size (%d) great than a page\n", reqlen);
10615 return -ENOMEM;
10616 }
10617 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10618 if (!mbox) {
10619 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10620 "0283 Failed to allocate mbox cmd memory\n");
10621 return -ENOMEM;
10622 }
10623
10624 /* Allocate DMA memory and set up the non-embedded mailbox command */
10625 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10626 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10627 LPFC_SLI4_MBX_NEMBED);
10628
10629 if (alloclen < reqlen) {
10630 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10631 "2561 Allocated DMA memory size (%d) is "
10632 "less than the requested DMA memory "
10633 "size (%d)\n", alloclen, reqlen);
10634 lpfc_sli4_mbox_cmd_free(phba, mbox);
10635 return -ENOMEM;
10636 }
10637
10638 /* Get the first SGE entry from the non-embedded DMA memory */
10639 if (unlikely(!mbox->sge_array)) {
10640 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
10641 "2565 Failed to get the non-embedded SGE "
10642 "virtual address\n");
10643 lpfc_sli4_mbox_cmd_free(phba, mbox);
10644 return -ENOMEM;
10645 }
10646 viraddr = mbox->sge_array->addr[0];
10647
10648 /* Set up the SGL pages in the non-embedded DMA pages */
10649 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10650 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10651
10652 pg_pairs = 0;
10653 list_for_each_entry(psb, sblist, list) {
10654 /* Set up the sge entry */
10655 sgl_pg_pairs->sgl_pg0_addr_lo =
10656 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10657 sgl_pg_pairs->sgl_pg0_addr_hi =
10658 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10659 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10660 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10661 else
10662 pdma_phys_bpl1 = 0;
10663 sgl_pg_pairs->sgl_pg1_addr_lo =
10664 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10665 sgl_pg_pairs->sgl_pg1_addr_hi =
10666 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10667 /* Keep the first xritag on the list */
10668 if (pg_pairs == 0)
10669 xritag_start = psb->cur_iocbq.sli4_xritag;
10670 sgl_pg_pairs++;
10671 pg_pairs++;
10672 }
10673 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10674 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10675 /* Perform endian conversion if necessary */
10676 sgl->word0 = cpu_to_le32(sgl->word0);
10677
10678 if (!phba->sli4_hba.intr_enable)
10679 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10680 else {
10681 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10682 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10683 }
10684 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10685 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10686 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10687 if (rc != MBX_TIMEOUT)
10688 lpfc_sli4_mbox_cmd_free(phba, mbox);
10689 if (shdr_status || shdr_add_status || rc) {
10690 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10691 "2564 POST_SGL_BLOCK mailbox command failed "
10692 "status x%x add_status x%x mbx status x%x\n",
10693 shdr_status, shdr_add_status, rc);
10694 rc = -ENXIO;
10695 }
10696 return rc;
10697}
10698
10699/**
10700 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10701 * @phba: pointer to lpfc_hba struct that the frame was received on
10702 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10703 *
10704 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10705 * valid type of frame that the LPFC driver will handle. This function will
10706 * return a zero if the frame is a valid frame or a non zero value when the
10707 * frame does not pass the check.
10708 **/
10709static int
10710lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10711{
10712 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10713 char *type_names[] = FC_TYPE_NAMES_INIT;
10714 struct fc_vft_header *fc_vft_hdr;
10715
10716 switch (fc_hdr->fh_r_ctl) {
10717 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10718 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10719 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10720 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10721 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10722 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10723 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10724 case FC_RCTL_DD_CMD_STATUS: /* command status */
10725 case FC_RCTL_ELS_REQ: /* extended link services request */
10726 case FC_RCTL_ELS_REP: /* extended link services reply */
10727 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10728 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10729 case FC_RCTL_BA_NOP: /* basic link service NOP */
10730 case FC_RCTL_BA_ABTS: /* basic link service abort */
10731 case FC_RCTL_BA_RMC: /* remove connection */
10732 case FC_RCTL_BA_ACC: /* basic accept */
10733 case FC_RCTL_BA_RJT: /* basic reject */
10734 case FC_RCTL_BA_PRMT:
10735 case FC_RCTL_ACK_1: /* acknowledge_1 */
10736 case FC_RCTL_ACK_0: /* acknowledge_0 */
10737 case FC_RCTL_P_RJT: /* port reject */
10738 case FC_RCTL_F_RJT: /* fabric reject */
10739 case FC_RCTL_P_BSY: /* port busy */
10740 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10741 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10742 case FC_RCTL_LCR: /* link credit reset */
10743 case FC_RCTL_END: /* end */
10744 break;
10745 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10746 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10747 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10748 return lpfc_fc_frame_check(phba, fc_hdr);
10749 default:
10750 goto drop;
10751 }
10752 switch (fc_hdr->fh_type) {
10753 case FC_TYPE_BLS:
10754 case FC_TYPE_ELS:
10755 case FC_TYPE_FCP:
10756 case FC_TYPE_CT:
10757 break;
10758 case FC_TYPE_IP:
10759 case FC_TYPE_ILS:
10760 default:
10761 goto drop;
10762 }
10763 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10764 "2538 Received frame rctl:%s type:%s\n",
10765 rctl_names[fc_hdr->fh_r_ctl],
10766 type_names[fc_hdr->fh_type]);
10767 return 0;
10768drop:
10769 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10770 "2539 Dropped frame rctl:%s type:%s\n",
10771 rctl_names[fc_hdr->fh_r_ctl],
10772 type_names[fc_hdr->fh_type]);
10773 return 1;
10774}
10775
10776/**
10777 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10778 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10779 *
10780 * This function processes the FC header to retrieve the VFI from the VF
10781 * header, if one exists. This function will return the VFI if one exists
10782 * or 0 if no VSAN Header exists.
10783 **/
10784static uint32_t
10785lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10786{
10787 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10788
10789 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10790 return 0;
10791 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10792}
10793
10794/**
10795 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10796 * @phba: Pointer to the HBA structure to search for the vport on
10797 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10798 * @fcfi: The FC Fabric ID that the frame came from
10799 *
10800 * This function searches the @phba for a vport that matches the content of the
10801 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10802 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10803 * returns the matching vport pointer or NULL if unable to match frame to a
10804 * vport.
10805 **/
10806static struct lpfc_vport *
10807lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10808 uint16_t fcfi)
10809{
10810 struct lpfc_vport **vports;
10811 struct lpfc_vport *vport = NULL;
10812 int i;
10813 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10814 fc_hdr->fh_d_id[1] << 8 |
10815 fc_hdr->fh_d_id[2]);
10816
10817 vports = lpfc_create_vport_work_array(phba);
10818 if (vports != NULL)
10819 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10820 if (phba->fcf.fcfi == fcfi &&
10821 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10822 vports[i]->fc_myDID == did) {
10823 vport = vports[i];
10824 break;
10825 }
10826 }
10827 lpfc_destroy_vport_work_array(phba, vports);
10828 return vport;
10829}
10830
10831/**
10832 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10833 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10834 *
10835 * This function searches through the existing incomplete sequences that have
10836 * been sent to this @vport. If the frame matches one of the incomplete
10837 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10838 * make up that sequence. If no sequence is found that matches this frame then
10839 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10840 * This function returns a pointer to the first dmabuf in the sequence list that
10841 * the frame was linked to.
10842 **/
10843static struct hbq_dmabuf *
10844lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10845{
10846 struct fc_frame_header *new_hdr;
10847 struct fc_frame_header *temp_hdr;
10848 struct lpfc_dmabuf *d_buf;
10849 struct lpfc_dmabuf *h_buf;
10850 struct hbq_dmabuf *seq_dmabuf = NULL;
10851 struct hbq_dmabuf *temp_dmabuf = NULL;
10852
James Smart4d9ab992009-10-02 15:16:39 -040010853 INIT_LIST_HEAD(&dmabuf->dbuf.list);
James Smart4f774512009-05-22 14:52:35 -040010854 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10855 /* Use the hdr_buf to find the sequence that this frame belongs to */
10856 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10857 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10858 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10859 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10860 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10861 continue;
10862 /* found a pending sequence that matches this frame */
10863 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10864 break;
10865 }
10866 if (!seq_dmabuf) {
10867 /*
10868 * This indicates first frame received for this sequence.
10869 * Queue the buffer on the vport's rcv_buffer_list.
10870 */
10871 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10872 return dmabuf;
10873 }
10874 temp_hdr = seq_dmabuf->hbuf.virt;
10875 if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
James Smart4d9ab992009-10-02 15:16:39 -040010876 list_del_init(&seq_dmabuf->hbuf.list);
10877 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10878 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
James Smart4f774512009-05-22 14:52:35 -040010879 return dmabuf;
10880 }
10881 /* find the correct place in the sequence to insert this frame */
10882 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10883 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10884 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10885 /*
10886 * If the frame's sequence count is greater than the frame on
10887 * the list then insert the frame right after this frame
10888 */
10889 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10890 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10891 return seq_dmabuf;
10892 }
10893 }
10894 return NULL;
10895}
10896
10897/**
James Smart6669f9b2009-10-02 15:16:45 -040010898 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
10899 * @vport: pointer to a vitural port
10900 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10901 *
10902 * This function tries to abort from the partially assembed sequence, described
10903 * by the information from basic abbort @dmabuf. It checks to see whether such
10904 * partially assembled sequence held by the driver. If so, it shall free up all
10905 * the frames from the partially assembled sequence.
10906 *
10907 * Return
10908 * true -- if there is matching partially assembled sequence present and all
10909 * the frames freed with the sequence;
10910 * false -- if there is no matching partially assembled sequence present so
10911 * nothing got aborted in the lower layer driver
10912 **/
10913static bool
10914lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
10915 struct hbq_dmabuf *dmabuf)
10916{
10917 struct fc_frame_header *new_hdr;
10918 struct fc_frame_header *temp_hdr;
10919 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
10920 struct hbq_dmabuf *seq_dmabuf = NULL;
10921
10922 /* Use the hdr_buf to find the sequence that matches this frame */
10923 INIT_LIST_HEAD(&dmabuf->dbuf.list);
10924 INIT_LIST_HEAD(&dmabuf->hbuf.list);
10925 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10926 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10927 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10928 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10929 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10930 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10931 continue;
10932 /* found a pending sequence that matches this frame */
10933 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10934 break;
10935 }
10936
10937 /* Free up all the frames from the partially assembled sequence */
10938 if (seq_dmabuf) {
10939 list_for_each_entry_safe(d_buf, n_buf,
10940 &seq_dmabuf->dbuf.list, list) {
10941 list_del_init(&d_buf->list);
10942 lpfc_in_buf_free(vport->phba, d_buf);
10943 }
10944 return true;
10945 }
10946 return false;
10947}
10948
10949/**
10950 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
10951 * @phba: Pointer to HBA context object.
10952 * @cmd_iocbq: pointer to the command iocbq structure.
10953 * @rsp_iocbq: pointer to the response iocbq structure.
10954 *
10955 * This function handles the sequence abort accept iocb command complete
10956 * event. It properly releases the memory allocated to the sequence abort
10957 * accept iocb.
10958 **/
10959static void
10960lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
10961 struct lpfc_iocbq *cmd_iocbq,
10962 struct lpfc_iocbq *rsp_iocbq)
10963{
10964 if (cmd_iocbq)
10965 lpfc_sli_release_iocbq(phba, cmd_iocbq);
10966}
10967
10968/**
10969 * lpfc_sli4_seq_abort_acc - Accept sequence abort
10970 * @phba: Pointer to HBA context object.
10971 * @fc_hdr: pointer to a FC frame header.
10972 *
10973 * This function sends a basic accept to a previous unsol sequence abort
10974 * event after aborting the sequence handling.
10975 **/
10976static void
10977lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
10978 struct fc_frame_header *fc_hdr)
10979{
10980 struct lpfc_iocbq *ctiocb = NULL;
10981 struct lpfc_nodelist *ndlp;
10982 uint16_t oxid;
10983 uint32_t sid;
10984 IOCB_t *icmd;
10985
10986 if (!lpfc_is_link_up(phba))
10987 return;
10988
10989 sid = sli4_sid_from_fc_hdr(fc_hdr);
10990 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
10991
10992 ndlp = lpfc_findnode_did(phba->pport, sid);
10993 if (!ndlp) {
10994 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10995 "1268 Find ndlp returned NULL for oxid:x%x "
10996 "SID:x%x\n", oxid, sid);
10997 return;
10998 }
10999
11000 /* Allocate buffer for acc iocb */
11001 ctiocb = lpfc_sli_get_iocbq(phba);
11002 if (!ctiocb)
11003 return;
11004
11005 icmd = &ctiocb->iocb;
11006 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
11007 icmd->un.xseq64.bdl.bdeSize = 0;
11008 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
11009 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
11010 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
11011
11012 /* Fill in the rest of iocb fields */
11013 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
11014 icmd->ulpBdeCount = 0;
11015 icmd->ulpLe = 1;
11016 icmd->ulpClass = CLASS3;
11017 icmd->ulpContext = ndlp->nlp_rpi;
11018 icmd->un.ulpWord[3] = oxid;
11019
11020 ctiocb->sli4_xritag = NO_XRI;
11021 ctiocb->iocb_cmpl = NULL;
11022 ctiocb->vport = phba->pport;
11023 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11024
11025 /* Xmit CT abts accept on exchange <xid> */
11026 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11027 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11028 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11029 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11030}
11031
11032/**
11033 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11034 * @vport: Pointer to the vport on which this sequence was received
11035 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11036 *
11037 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11038 * receive sequence is only partially assembed by the driver, it shall abort
11039 * the partially assembled frames for the sequence. Otherwise, if the
11040 * unsolicited receive sequence has been completely assembled and passed to
11041 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11042 * unsolicited sequence has been aborted. After that, it will issue a basic
11043 * accept to accept the abort.
11044 **/
11045void
11046lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11047 struct hbq_dmabuf *dmabuf)
11048{
11049 struct lpfc_hba *phba = vport->phba;
11050 struct fc_frame_header fc_hdr;
11051 bool abts_par;
11052
11053 /* Try to abort partially assembled seq */
11054 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11055
11056 /* Make a copy of fc_hdr before the dmabuf being released */
11057 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11058
11059 /* Send abort to ULP if partially seq abort failed */
11060 if (abts_par == false)
11061 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11062 else
11063 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11064 /* Send basic accept (BA_ACC) to the abort requester */
11065 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11066}
11067
11068/**
James Smart4f774512009-05-22 14:52:35 -040011069 * lpfc_seq_complete - Indicates if a sequence is complete
11070 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11071 *
11072 * This function checks the sequence, starting with the frame described by
11073 * @dmabuf, to see if all the frames associated with this sequence are present.
11074 * the frames associated with this sequence are linked to the @dmabuf using the
11075 * dbuf list. This function looks for two major things. 1) That the first frame
11076 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11077 * set. 3) That there are no holes in the sequence count. The function will
11078 * return 1 when the sequence is complete, otherwise it will return 0.
11079 **/
11080static int
11081lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11082{
11083 struct fc_frame_header *hdr;
11084 struct lpfc_dmabuf *d_buf;
11085 struct hbq_dmabuf *seq_dmabuf;
11086 uint32_t fctl;
11087 int seq_count = 0;
11088
11089 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11090 /* make sure first fame of sequence has a sequence count of zero */
11091 if (hdr->fh_seq_cnt != seq_count)
11092 return 0;
11093 fctl = (hdr->fh_f_ctl[0] << 16 |
11094 hdr->fh_f_ctl[1] << 8 |
11095 hdr->fh_f_ctl[2]);
11096 /* If last frame of sequence we can return success. */
11097 if (fctl & FC_FC_END_SEQ)
11098 return 1;
11099 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11100 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11101 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11102 /* If there is a hole in the sequence count then fail. */
11103 if (++seq_count != hdr->fh_seq_cnt)
11104 return 0;
11105 fctl = (hdr->fh_f_ctl[0] << 16 |
11106 hdr->fh_f_ctl[1] << 8 |
11107 hdr->fh_f_ctl[2]);
11108 /* If last frame of sequence we can return success. */
11109 if (fctl & FC_FC_END_SEQ)
11110 return 1;
11111 }
11112 return 0;
11113}
11114
11115/**
11116 * lpfc_prep_seq - Prep sequence for ULP processing
11117 * @vport: Pointer to the vport on which this sequence was received
11118 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11119 *
11120 * This function takes a sequence, described by a list of frames, and creates
11121 * a list of iocbq structures to describe the sequence. This iocbq list will be
11122 * used to issue to the generic unsolicited sequence handler. This routine
11123 * returns a pointer to the first iocbq in the list. If the function is unable
11124 * to allocate an iocbq then it throw out the received frames that were not
11125 * able to be described and return a pointer to the first iocbq. If unable to
11126 * allocate any iocbqs (including the first) this function will return NULL.
11127 **/
11128static struct lpfc_iocbq *
11129lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11130{
11131 struct lpfc_dmabuf *d_buf, *n_buf;
11132 struct lpfc_iocbq *first_iocbq, *iocbq;
11133 struct fc_frame_header *fc_hdr;
11134 uint32_t sid;
11135
11136 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11137 /* remove from receive buffer list */
11138 list_del_init(&seq_dmabuf->hbuf.list);
11139 /* get the Remote Port's SID */
James Smart6669f9b2009-10-02 15:16:45 -040011140 sid = sli4_sid_from_fc_hdr(fc_hdr);
James Smart4f774512009-05-22 14:52:35 -040011141 /* Get an iocbq struct to fill in. */
11142 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11143 if (first_iocbq) {
11144 /* Initialize the first IOCB. */
James Smart8fa38512009-07-19 10:01:03 -040011145 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
James Smart4f774512009-05-22 14:52:35 -040011146 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11147 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11148 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11149 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11150 vport->vpi + vport->phba->vpi_base;
11151 /* put the first buffer into the first IOCBq */
11152 first_iocbq->context2 = &seq_dmabuf->dbuf;
11153 first_iocbq->context3 = NULL;
11154 first_iocbq->iocb.ulpBdeCount = 1;
11155 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11156 LPFC_DATA_BUF_SIZE;
11157 first_iocbq->iocb.un.rcvels.remoteID = sid;
James Smart8fa38512009-07-19 10:01:03 -040011158 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011159 bf_get(lpfc_rcqe_length,
11160 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011161 }
11162 iocbq = first_iocbq;
11163 /*
11164 * Each IOCBq can have two Buffers assigned, so go through the list
11165 * of buffers for this sequence and save two buffers in each IOCBq
11166 */
11167 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11168 if (!iocbq) {
11169 lpfc_in_buf_free(vport->phba, d_buf);
11170 continue;
11171 }
11172 if (!iocbq->context3) {
11173 iocbq->context3 = d_buf;
11174 iocbq->iocb.ulpBdeCount++;
11175 iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
11176 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011177 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011178 bf_get(lpfc_rcqe_length,
11179 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011180 } else {
11181 iocbq = lpfc_sli_get_iocbq(vport->phba);
11182 if (!iocbq) {
11183 if (first_iocbq) {
11184 first_iocbq->iocb.ulpStatus =
11185 IOSTAT_FCP_RSP_ERROR;
11186 first_iocbq->iocb.un.ulpWord[4] =
11187 IOERR_NO_RESOURCES;
11188 }
11189 lpfc_in_buf_free(vport->phba, d_buf);
11190 continue;
11191 }
11192 iocbq->context2 = d_buf;
11193 iocbq->context3 = NULL;
11194 iocbq->iocb.ulpBdeCount = 1;
11195 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11196 LPFC_DATA_BUF_SIZE;
James Smart8fa38512009-07-19 10:01:03 -040011197 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
James Smart4d9ab992009-10-02 15:16:39 -040011198 bf_get(lpfc_rcqe_length,
11199 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
James Smart4f774512009-05-22 14:52:35 -040011200 iocbq->iocb.un.rcvels.remoteID = sid;
11201 list_add_tail(&iocbq->list, &first_iocbq->list);
11202 }
11203 }
11204 return first_iocbq;
11205}
11206
James Smart6669f9b2009-10-02 15:16:45 -040011207static void
11208lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11209 struct hbq_dmabuf *seq_dmabuf)
11210{
11211 struct fc_frame_header *fc_hdr;
11212 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11213 struct lpfc_hba *phba = vport->phba;
11214
11215 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11216 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11217 if (!iocbq) {
11218 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11219 "2707 Ring %d handler: Failed to allocate "
11220 "iocb Rctl x%x Type x%x received\n",
11221 LPFC_ELS_RING,
11222 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11223 return;
11224 }
11225 if (!lpfc_complete_unsol_iocb(phba,
11226 &phba->sli.ring[LPFC_ELS_RING],
11227 iocbq, fc_hdr->fh_r_ctl,
11228 fc_hdr->fh_type))
11229 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11230 "2540 Ring %d handler: unexpected Rctl "
11231 "x%x Type x%x received\n",
11232 LPFC_ELS_RING,
11233 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11234
11235 /* Free iocb created in lpfc_prep_seq */
11236 list_for_each_entry_safe(curr_iocb, next_iocb,
11237 &iocbq->list, list) {
11238 list_del_init(&curr_iocb->list);
11239 lpfc_sli_release_iocbq(phba, curr_iocb);
11240 }
11241 lpfc_sli_release_iocbq(phba, iocbq);
11242}
11243
James Smart4f774512009-05-22 14:52:35 -040011244/**
11245 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11246 * @phba: Pointer to HBA context object.
11247 *
11248 * This function is called with no lock held. This function processes all
11249 * the received buffers and gives it to upper layers when a received buffer
11250 * indicates that it is the final frame in the sequence. The interrupt
11251 * service routine processes received buffers at interrupt contexts and adds
11252 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11253 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11254 * appropriate receive function when the final frame in a sequence is received.
11255 **/
James Smart4d9ab992009-10-02 15:16:39 -040011256void
11257lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11258 struct hbq_dmabuf *dmabuf)
James Smart4f774512009-05-22 14:52:35 -040011259{
James Smart4d9ab992009-10-02 15:16:39 -040011260 struct hbq_dmabuf *seq_dmabuf;
James Smart4f774512009-05-22 14:52:35 -040011261 struct fc_frame_header *fc_hdr;
11262 struct lpfc_vport *vport;
11263 uint32_t fcfi;
James Smart4f774512009-05-22 14:52:35 -040011264
11265 /* Clear hba flag and get all received buffers into the cmplq */
11266 spin_lock_irq(&phba->hbalock);
11267 phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
James Smart4f774512009-05-22 14:52:35 -040011268 spin_unlock_irq(&phba->hbalock);
11269
11270 /* Process each received buffer */
James Smart4d9ab992009-10-02 15:16:39 -040011271 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11272 /* check to see if this a valid type of frame */
11273 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11274 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11275 return;
11276 }
11277 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11278 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11279 if (!vport) {
11280 /* throw out the frame */
11281 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11282 return;
11283 }
James Smart6669f9b2009-10-02 15:16:45 -040011284 /* Handle the basic abort sequence (BA_ABTS) event */
11285 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11286 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11287 return;
11288 }
11289
James Smart4d9ab992009-10-02 15:16:39 -040011290 /* Link this frame */
11291 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11292 if (!seq_dmabuf) {
11293 /* unable to add frame to vport - throw it out */
11294 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11295 return;
11296 }
11297 /* If not last frame in sequence continue processing frames. */
11298 if (!lpfc_seq_complete(seq_dmabuf)) {
11299 /*
11300 * When saving off frames post a new one and mark this
11301 * frame to be freed when it is finished.
11302 **/
11303 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11304 dmabuf->tag = -1;
11305 return;
11306 }
James Smart6669f9b2009-10-02 15:16:45 -040011307 /* Send the complete sequence to the upper layer protocol */
11308 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
James Smart4f774512009-05-22 14:52:35 -040011309}
James Smart6fb120a2009-05-22 14:52:59 -040011310
11311/**
11312 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11313 * @phba: pointer to lpfc hba data structure.
11314 *
11315 * This routine is invoked to post rpi header templates to the
11316 * HBA consistent with the SLI-4 interface spec. This routine
11317 * posts a PAGE_SIZE memory region to the port to hold up to
11318 * PAGE_SIZE modulo 64 rpi context headers.
11319 *
11320 * This routine does not require any locks. It's usage is expected
11321 * to be driver load or reset recovery when the driver is
11322 * sequential.
11323 *
11324 * Return codes
11325 * 0 - sucessful
11326 * EIO - The mailbox failed to complete successfully.
11327 * When this error occurs, the driver is not guaranteed
11328 * to have any rpi regions posted to the device and
11329 * must either attempt to repost the regions or take a
11330 * fatal error.
11331 **/
11332int
11333lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11334{
11335 struct lpfc_rpi_hdr *rpi_page;
11336 uint32_t rc = 0;
11337
11338 /* Post all rpi memory regions to the port. */
11339 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11340 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11341 if (rc != MBX_SUCCESS) {
11342 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11343 "2008 Error %d posting all rpi "
11344 "headers\n", rc);
11345 rc = -EIO;
11346 break;
11347 }
11348 }
11349
11350 return rc;
11351}
11352
11353/**
11354 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11355 * @phba: pointer to lpfc hba data structure.
11356 * @rpi_page: pointer to the rpi memory region.
11357 *
11358 * This routine is invoked to post a single rpi header to the
11359 * HBA consistent with the SLI-4 interface spec. This memory region
11360 * maps up to 64 rpi context regions.
11361 *
11362 * Return codes
11363 * 0 - sucessful
11364 * ENOMEM - No available memory
11365 * EIO - The mailbox failed to complete successfully.
11366 **/
11367int
11368lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11369{
11370 LPFC_MBOXQ_t *mboxq;
11371 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11372 uint32_t rc = 0;
11373 uint32_t mbox_tmo;
11374 uint32_t shdr_status, shdr_add_status;
11375 union lpfc_sli4_cfg_shdr *shdr;
11376
11377 /* The port is notified of the header region via a mailbox command. */
11378 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11379 if (!mboxq) {
11380 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11381 "2001 Unable to allocate memory for issuing "
11382 "SLI_CONFIG_SPECIAL mailbox command\n");
11383 return -ENOMEM;
11384 }
11385
11386 /* Post all rpi memory regions to the port. */
11387 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11388 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11389 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11390 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11391 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11392 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11393 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11394 hdr_tmpl, rpi_page->page_count);
11395 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11396 rpi_page->start_rpi);
11397 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11398 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
James Smartf1126682009-06-10 17:22:44 -040011399 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
James Smart6fb120a2009-05-22 14:52:59 -040011400 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11401 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11402 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11403 if (rc != MBX_TIMEOUT)
11404 mempool_free(mboxq, phba->mbox_mem_pool);
11405 if (shdr_status || shdr_add_status || rc) {
11406 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11407 "2514 POST_RPI_HDR mailbox failed with "
11408 "status x%x add_status x%x, mbx status x%x\n",
11409 shdr_status, shdr_add_status, rc);
11410 rc = -ENXIO;
11411 }
11412 return rc;
11413}
11414
11415/**
11416 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11417 * @phba: pointer to lpfc hba data structure.
11418 *
11419 * This routine is invoked to post rpi header templates to the
11420 * HBA consistent with the SLI-4 interface spec. This routine
11421 * posts a PAGE_SIZE memory region to the port to hold up to
11422 * PAGE_SIZE modulo 64 rpi context headers.
11423 *
11424 * Returns
11425 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
11426 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11427 **/
11428int
11429lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11430{
11431 int rpi;
11432 uint16_t max_rpi, rpi_base, rpi_limit;
11433 uint16_t rpi_remaining;
11434 struct lpfc_rpi_hdr *rpi_hdr;
11435
11436 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11437 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11438 rpi_limit = phba->sli4_hba.next_rpi;
11439
11440 /*
11441 * The valid rpi range is not guaranteed to be zero-based. Start
11442 * the search at the rpi_base as reported by the port.
11443 */
11444 spin_lock_irq(&phba->hbalock);
11445 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11446 if (rpi >= rpi_limit || rpi < rpi_base)
11447 rpi = LPFC_RPI_ALLOC_ERROR;
11448 else {
11449 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11450 phba->sli4_hba.max_cfg_param.rpi_used++;
11451 phba->sli4_hba.rpi_count++;
11452 }
11453
11454 /*
11455 * Don't try to allocate more rpi header regions if the device limit
11456 * on available rpis max has been exhausted.
11457 */
11458 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11459 (phba->sli4_hba.rpi_count >= max_rpi)) {
11460 spin_unlock_irq(&phba->hbalock);
11461 return rpi;
11462 }
11463
11464 /*
11465 * If the driver is running low on rpi resources, allocate another
11466 * page now. Note that the next_rpi value is used because
11467 * it represents how many are actually in use whereas max_rpi notes
11468 * how many are supported max by the device.
11469 */
11470 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11471 phba->sli4_hba.rpi_count;
11472 spin_unlock_irq(&phba->hbalock);
11473 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11474 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11475 if (!rpi_hdr) {
11476 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11477 "2002 Error Could not grow rpi "
11478 "count\n");
11479 } else {
11480 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11481 }
11482 }
11483
11484 return rpi;
11485}
11486
11487/**
11488 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11489 * @phba: pointer to lpfc hba data structure.
11490 *
11491 * This routine is invoked to release an rpi to the pool of
11492 * available rpis maintained by the driver.
11493 **/
11494void
11495lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11496{
11497 spin_lock_irq(&phba->hbalock);
11498 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11499 phba->sli4_hba.rpi_count--;
11500 phba->sli4_hba.max_cfg_param.rpi_used--;
11501 spin_unlock_irq(&phba->hbalock);
11502}
11503
11504/**
11505 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11506 * @phba: pointer to lpfc hba data structure.
11507 *
11508 * This routine is invoked to remove the memory region that
11509 * provided rpi via a bitmask.
11510 **/
11511void
11512lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11513{
11514 kfree(phba->sli4_hba.rpi_bmask);
11515}
11516
11517/**
11518 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11519 * @phba: pointer to lpfc hba data structure.
11520 *
11521 * This routine is invoked to remove the memory region that
11522 * provided rpi via a bitmask.
11523 **/
11524int
11525lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11526{
11527 LPFC_MBOXQ_t *mboxq;
11528 struct lpfc_hba *phba = ndlp->phba;
11529 int rc;
11530
11531 /* The port is notified of the header region via a mailbox command. */
11532 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11533 if (!mboxq)
11534 return -ENOMEM;
11535
11536 /* Post all rpi memory regions to the port. */
11537 lpfc_resume_rpi(mboxq, ndlp);
11538 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11539 if (rc == MBX_NOT_FINISHED) {
11540 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11541 "2010 Resume RPI Mailbox failed "
11542 "status %d, mbxStatus x%x\n", rc,
11543 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11544 mempool_free(mboxq, phba->mbox_mem_pool);
11545 return -EIO;
11546 }
11547 return 0;
11548}
11549
11550/**
11551 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11552 * @phba: pointer to lpfc hba data structure.
11553 * @vpi: vpi value to activate with the port.
11554 *
11555 * This routine is invoked to activate a vpi with the
11556 * port when the host intends to use vports with a
11557 * nonzero vpi.
11558 *
11559 * Returns:
11560 * 0 success
11561 * -Evalue otherwise
11562 **/
11563int
11564lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11565{
11566 LPFC_MBOXQ_t *mboxq;
11567 int rc = 0;
11568 uint32_t mbox_tmo;
11569
11570 if (vpi == 0)
11571 return -EINVAL;
11572 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11573 if (!mboxq)
11574 return -ENOMEM;
James Smart1c6834a2009-07-19 10:01:26 -040011575 lpfc_init_vpi(phba, mboxq, vpi);
James Smart6fb120a2009-05-22 14:52:59 -040011576 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11577 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
11578 if (rc != MBX_TIMEOUT)
11579 mempool_free(mboxq, phba->mbox_mem_pool);
11580 if (rc != MBX_SUCCESS) {
11581 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11582 "2022 INIT VPI Mailbox failed "
11583 "status %d, mbxStatus x%x\n", rc,
11584 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11585 rc = -EIO;
11586 }
11587 return rc;
11588}
11589
11590/**
11591 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11592 * @phba: pointer to lpfc hba data structure.
11593 * @mboxq: Pointer to mailbox object.
11594 *
11595 * This routine is invoked to manually add a single FCF record. The caller
11596 * must pass a completely initialized FCF_Record. This routine takes
11597 * care of the nonembedded mailbox operations.
11598 **/
11599static void
11600lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11601{
11602 void *virt_addr;
11603 union lpfc_sli4_cfg_shdr *shdr;
11604 uint32_t shdr_status, shdr_add_status;
11605
11606 virt_addr = mboxq->sge_array->addr[0];
11607 /* The IOCTL status is embedded in the mailbox subheader. */
11608 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11609 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11610 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11611
11612 if ((shdr_status || shdr_add_status) &&
11613 (shdr_status != STATUS_FCF_IN_USE))
11614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11615 "2558 ADD_FCF_RECORD mailbox failed with "
11616 "status x%x add_status x%x\n",
11617 shdr_status, shdr_add_status);
11618
11619 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11620}
11621
11622/**
11623 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11624 * @phba: pointer to lpfc hba data structure.
11625 * @fcf_record: pointer to the initialized fcf record to add.
11626 *
11627 * This routine is invoked to manually add a single FCF record. The caller
11628 * must pass a completely initialized FCF_Record. This routine takes
11629 * care of the nonembedded mailbox operations.
11630 **/
11631int
11632lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11633{
11634 int rc = 0;
11635 LPFC_MBOXQ_t *mboxq;
11636 uint8_t *bytep;
11637 void *virt_addr;
11638 dma_addr_t phys_addr;
11639 struct lpfc_mbx_sge sge;
11640 uint32_t alloc_len, req_len;
11641 uint32_t fcfindex;
11642
11643 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11644 if (!mboxq) {
11645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11646 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11647 return -ENOMEM;
11648 }
11649
11650 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11651 sizeof(uint32_t);
11652
11653 /* Allocate DMA memory and set up the non-embedded mailbox command */
11654 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11655 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11656 req_len, LPFC_SLI4_MBX_NEMBED);
11657 if (alloc_len < req_len) {
11658 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11659 "2523 Allocated DMA memory size (x%x) is "
11660 "less than the requested DMA memory "
11661 "size (x%x)\n", alloc_len, req_len);
11662 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11663 return -ENOMEM;
11664 }
11665
11666 /*
11667 * Get the first SGE entry from the non-embedded DMA memory. This
11668 * routine only uses a single SGE.
11669 */
11670 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11671 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11672 if (unlikely(!mboxq->sge_array)) {
11673 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11674 "2526 Failed to get the non-embedded SGE "
11675 "virtual address\n");
11676 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11677 return -ENOMEM;
11678 }
11679 virt_addr = mboxq->sge_array->addr[0];
11680 /*
11681 * Configure the FCF record for FCFI 0. This is the driver's
11682 * hardcoded default and gets used in nonFIP mode.
11683 */
11684 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11685 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11686 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11687
11688 /*
11689 * Copy the fcf_index and the FCF Record Data. The data starts after
11690 * the FCoE header plus word10. The data copy needs to be endian
11691 * correct.
11692 */
11693 bytep += sizeof(uint32_t);
11694 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11695 mboxq->vport = phba->pport;
11696 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11697 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11698 if (rc == MBX_NOT_FINISHED) {
11699 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11700 "2515 ADD_FCF_RECORD mailbox failed with "
11701 "status 0x%x\n", rc);
11702 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11703 rc = -EIO;
11704 } else
11705 rc = 0;
11706
11707 return rc;
11708}
11709
11710/**
11711 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11712 * @phba: pointer to lpfc hba data structure.
11713 * @fcf_record: pointer to the fcf record to write the default data.
11714 * @fcf_index: FCF table entry index.
11715 *
11716 * This routine is invoked to build the driver's default FCF record. The
11717 * values used are hardcoded. This routine handles memory initialization.
11718 *
11719 **/
11720void
11721lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11722 struct fcf_record *fcf_record,
11723 uint16_t fcf_index)
11724{
11725 memset(fcf_record, 0, sizeof(struct fcf_record));
11726 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11727 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11728 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11729 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11730 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11731 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11732 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11733 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11734 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11735 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11736 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11737 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11738 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
James Smart0c287582009-06-10 17:22:56 -040011739 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
James Smart6fb120a2009-05-22 14:52:59 -040011740 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11741 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11742 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11743 /* Set the VLAN bit map */
11744 if (phba->valid_vlan) {
11745 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11746 = 1 << (phba->vlan_id % 8);
11747 }
11748}
11749
11750/**
11751 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11752 * @phba: pointer to lpfc hba data structure.
11753 * @fcf_index: FCF table entry offset.
11754 *
11755 * This routine is invoked to read up to @fcf_num of FCF record from the
11756 * device starting with the given @fcf_index.
11757 **/
11758int
11759lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11760{
11761 int rc = 0, error;
11762 LPFC_MBOXQ_t *mboxq;
11763 void *virt_addr;
11764 dma_addr_t phys_addr;
11765 uint8_t *bytep;
11766 struct lpfc_mbx_sge sge;
11767 uint32_t alloc_len, req_len;
11768 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11769
James Smart32b97932009-07-19 10:01:21 -040011770 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
James Smart6fb120a2009-05-22 14:52:59 -040011771 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11772 if (!mboxq) {
11773 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11774 "2000 Failed to allocate mbox for "
11775 "READ_FCF cmd\n");
James Smart4d9ab992009-10-02 15:16:39 -040011776 error = -ENOMEM;
11777 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011778 }
11779
11780 req_len = sizeof(struct fcf_record) +
11781 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11782
11783 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11784 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11785 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11786 LPFC_SLI4_MBX_NEMBED);
11787
11788 if (alloc_len < req_len) {
11789 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11790 "0291 Allocated DMA memory size (x%x) is "
11791 "less than the requested DMA memory "
11792 "size (x%x)\n", alloc_len, req_len);
James Smart4d9ab992009-10-02 15:16:39 -040011793 error = -ENOMEM;
11794 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011795 }
11796
11797 /* Get the first SGE entry from the non-embedded DMA memory. This
11798 * routine only uses a single SGE.
11799 */
11800 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11801 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
11802 if (unlikely(!mboxq->sge_array)) {
11803 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11804 "2527 Failed to get the non-embedded SGE "
11805 "virtual address\n");
James Smart4d9ab992009-10-02 15:16:39 -040011806 error = -ENOMEM;
11807 goto fail_fcfscan;
James Smart6fb120a2009-05-22 14:52:59 -040011808 }
11809 virt_addr = mboxq->sge_array->addr[0];
11810 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11811
11812 /* Set up command fields */
11813 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11814 /* Perform necessary endian conversion */
11815 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11816 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11817 mboxq->vport = phba->pport;
11818 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11819 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11820 if (rc == MBX_NOT_FINISHED) {
James Smart6fb120a2009-05-22 14:52:59 -040011821 error = -EIO;
James Smart32b97932009-07-19 10:01:21 -040011822 } else {
11823 spin_lock_irq(&phba->hbalock);
11824 phba->hba_flag |= FCF_DISC_INPROGRESS;
11825 spin_unlock_irq(&phba->hbalock);
James Smart6fb120a2009-05-22 14:52:59 -040011826 error = 0;
James Smart32b97932009-07-19 10:01:21 -040011827 }
James Smart4d9ab992009-10-02 15:16:39 -040011828fail_fcfscan:
11829 if (error) {
11830 if (mboxq)
11831 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11832 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11833 spin_lock_irq(&phba->hbalock);
11834 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11835 spin_unlock_irq(&phba->hbalock);
11836 }
James Smart6fb120a2009-05-22 14:52:59 -040011837 return error;
11838}
James Smarta0c87cb2009-07-19 10:01:10 -040011839
11840/**
11841 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
11842 * @phba: pointer to lpfc hba data structure.
11843 *
11844 * This function read region 23 and parse TLV for port status to
11845 * decide if the user disaled the port. If the TLV indicates the
11846 * port is disabled, the hba_flag is set accordingly.
11847 **/
11848void
11849lpfc_sli_read_link_ste(struct lpfc_hba *phba)
11850{
11851 LPFC_MBOXQ_t *pmb = NULL;
11852 MAILBOX_t *mb;
11853 uint8_t *rgn23_data = NULL;
11854 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
11855 int rc;
11856
11857 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11858 if (!pmb) {
11859 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11860 "2600 lpfc_sli_read_serdes_param failed to"
11861 " allocate mailbox memory\n");
11862 goto out;
11863 }
11864 mb = &pmb->u.mb;
11865
11866 /* Get adapter Region 23 data */
11867 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
11868 if (!rgn23_data)
11869 goto out;
11870
11871 do {
11872 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
11873 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
11874
11875 if (rc != MBX_SUCCESS) {
11876 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11877 "2601 lpfc_sli_read_link_ste failed to"
11878 " read config region 23 rc 0x%x Status 0x%x\n",
11879 rc, mb->mbxStatus);
11880 mb->un.varDmp.word_cnt = 0;
11881 }
11882 /*
11883 * dump mem may return a zero when finished or we got a
11884 * mailbox error, either way we are done.
11885 */
11886 if (mb->un.varDmp.word_cnt == 0)
11887 break;
11888 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
11889 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
11890
11891 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
11892 rgn23_data + offset,
11893 mb->un.varDmp.word_cnt);
11894 offset += mb->un.varDmp.word_cnt;
11895 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
11896
11897 data_size = offset;
11898 offset = 0;
11899
11900 if (!data_size)
11901 goto out;
11902
11903 /* Check the region signature first */
11904 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
11905 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11906 "2619 Config region 23 has bad signature\n");
11907 goto out;
11908 }
11909 offset += 4;
11910
11911 /* Check the data structure version */
11912 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
11913 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11914 "2620 Config region 23 has bad version\n");
11915 goto out;
11916 }
11917 offset += 4;
11918
11919 /* Parse TLV entries in the region */
11920 while (offset < data_size) {
11921 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
11922 break;
11923 /*
11924 * If the TLV is not driver specific TLV or driver id is
11925 * not linux driver id, skip the record.
11926 */
11927 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
11928 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
11929 (rgn23_data[offset + 3] != 0)) {
11930 offset += rgn23_data[offset + 1] * 4 + 4;
11931 continue;
11932 }
11933
11934 /* Driver found a driver specific TLV in the config region */
11935 sub_tlv_len = rgn23_data[offset + 1] * 4;
11936 offset += 4;
11937 tlv_offset = 0;
11938
11939 /*
11940 * Search for configured port state sub-TLV.
11941 */
11942 while ((offset < data_size) &&
11943 (tlv_offset < sub_tlv_len)) {
11944 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
11945 offset += 4;
11946 tlv_offset += 4;
11947 break;
11948 }
11949 if (rgn23_data[offset] != PORT_STE_TYPE) {
11950 offset += rgn23_data[offset + 1] * 4 + 4;
11951 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
11952 continue;
11953 }
11954
11955 /* This HBA contains PORT_STE configured */
11956 if (!rgn23_data[offset + 2])
11957 phba->hba_flag |= LINK_DISABLED;
11958
11959 goto out;
11960 }
11961 }
11962out:
11963 if (pmb)
11964 mempool_free(pmb, phba->mbox_mem_pool);
11965 kfree(rgn23_data);
11966 return;
11967}