[SCSI] lpfc 8.3.5: fix sysfs parameters, vport creation and other bugs and update...
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_sli.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
d8e93df1 4 * Copyright (C) 2004-2009 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/blkdev.h>
23#include <linux/pci.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26
91886523 27#include <scsi/scsi.h>
dea3101e 28#include <scsi/scsi_cmnd.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_host.h>
f888ba3c 31#include <scsi/scsi_transport_fc.h>
da0436e9 32#include <scsi/fc/fc_fs.h>
dea3101e 33
da0436e9 34#include "lpfc_hw4.h"
dea3101e 35#include "lpfc_hw.h"
36#include "lpfc_sli.h"
da0436e9 37#include "lpfc_sli4.h"
ea2151b4 38#include "lpfc_nl.h"
dea3101e 39#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"
858c9f6c 45#include "lpfc_debugfs.h"
04c68496 46#include "lpfc_vport.h"
dea3101e 47
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
4f774513
JS
56
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 *);
6669f9bb
JS
62static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
63 struct hbq_dmabuf *);
4f774513
JS
64static 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
e59058c4 401/**
3621a710 402 * lpfc_cmd_iocb - Get next command iocb entry in the ring
e59058c4
JS
403 * @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 **/
ed957684
JS
411static 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
e59058c4 418/**
3621a710 419 * lpfc_resp_iocb - Get next response iocb entry in the ring
e59058c4
JS
420 * @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 **/
ed957684
JS
428static 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
e59058c4 435/**
3621a710 436 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
437 * @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 **/
2e0fef85
JS
444static struct lpfc_iocbq *
445__lpfc_sli_get_iocbq(struct lpfc_hba *phba)
0bd4ca25
JSEC
446{
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
da0436e9
JS
454/**
455 * __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);
6a9c52cf
JS
519 if (!sglq)
520 return NULL;
da0436e9
JS
521 adj_xri = sglq->sli4_xritag - phba->sli4_hba.max_cfg_param.xri_base;
522 phba->sli4_hba.lpfc_sglq_active_list[adj_xri] = sglq;
523 return sglq;
524}
525
e59058c4 526/**
3621a710 527 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
e59058c4
JS
528 * @phba: Pointer to HBA context object.
529 *
530 * This function is called with no lock held. This function
531 * allocates a new driver iocb object from the iocb pool. If the
532 * allocation is successful, it returns pointer to the newly
533 * allocated iocb object else it returns NULL.
534 **/
2e0fef85
JS
535struct lpfc_iocbq *
536lpfc_sli_get_iocbq(struct lpfc_hba *phba)
537{
538 struct lpfc_iocbq * iocbq = NULL;
539 unsigned long iflags;
540
541 spin_lock_irqsave(&phba->hbalock, iflags);
542 iocbq = __lpfc_sli_get_iocbq(phba);
543 spin_unlock_irqrestore(&phba->hbalock, iflags);
544 return iocbq;
545}
546
4f774513
JS
547/**
548 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
549 * @phba: Pointer to HBA context object.
550 * @iocbq: Pointer to driver iocb object.
551 *
552 * This function is called with hbalock held to release driver
553 * iocb object to the iocb pool. The iotag in the iocb object
554 * does not change for each use of the iocb object. This function
555 * clears all other fields of the iocb object when it is freed.
556 * The sqlq structure that holds the xritag and phys and virtual
557 * mappings for the scatter gather list is retrieved from the
558 * active array of sglq. The get of the sglq pointer also clears
559 * the entry in the array. If the status of the IO indiactes that
560 * this IO was aborted then the sglq entry it put on the
561 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
562 * IO has good status or fails for any other reason then the sglq
563 * entry is added to the free list (lpfc_sgl_list).
564 **/
565static void
566__lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
567{
568 struct lpfc_sglq *sglq;
569 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
570 unsigned long iflag;
571
572 if (iocbq->sli4_xritag == NO_XRI)
573 sglq = NULL;
574 else
575 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_xritag);
576 if (sglq) {
577 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED
6669f9bb 578 && ((iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
4f774513 579 && (iocbq->iocb.un.ulpWord[4]
6669f9bb 580 == IOERR_ABORT_REQUESTED))) {
4f774513
JS
581 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
582 iflag);
583 list_add(&sglq->list,
584 &phba->sli4_hba.lpfc_abts_els_sgl_list);
585 spin_unlock_irqrestore(
586 &phba->sli4_hba.abts_sgl_list_lock, iflag);
587 } else
588 list_add(&sglq->list, &phba->sli4_hba.lpfc_sgl_list);
589 }
590
591
592 /*
593 * Clean all volatile data fields, preserve iotag and node struct.
594 */
595 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
596 iocbq->sli4_xritag = NO_XRI;
597 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
598}
599
e59058c4 600/**
3772a991 601 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
e59058c4
JS
602 * @phba: Pointer to HBA context object.
603 * @iocbq: Pointer to driver iocb object.
604 *
605 * This function is called with hbalock held to release driver
606 * iocb object to the iocb pool. The iotag in the iocb object
607 * does not change for each use of the iocb object. This function
608 * clears all other fields of the iocb object when it is freed.
609 **/
a6ababd2 610static void
3772a991 611__lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
604a3e30 612{
2e0fef85 613 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
604a3e30
JB
614
615 /*
616 * Clean all volatile data fields, preserve iotag and node struct.
617 */
618 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
3772a991 619 iocbq->sli4_xritag = NO_XRI;
604a3e30
JB
620 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
621}
622
3772a991
JS
623/**
624 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
625 * @phba: Pointer to HBA context object.
626 * @iocbq: Pointer to driver iocb object.
627 *
628 * This function is called with hbalock held to release driver
629 * iocb object to the iocb pool. The iotag in the iocb object
630 * does not change for each use of the iocb object. This function
631 * clears all other fields of the iocb object when it is freed.
632 **/
633static void
634__lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
635{
636 phba->__lpfc_sli_release_iocbq(phba, iocbq);
637}
638
e59058c4 639/**
3621a710 640 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
e59058c4
JS
641 * @phba: Pointer to HBA context object.
642 * @iocbq: Pointer to driver iocb object.
643 *
644 * This function is called with no lock held to release the iocb to
645 * iocb pool.
646 **/
2e0fef85
JS
647void
648lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
649{
650 unsigned long iflags;
651
652 /*
653 * Clean all volatile data fields, preserve iotag and node struct.
654 */
655 spin_lock_irqsave(&phba->hbalock, iflags);
656 __lpfc_sli_release_iocbq(phba, iocbq);
657 spin_unlock_irqrestore(&phba->hbalock, iflags);
658}
659
a257bf90
JS
660/**
661 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
662 * @phba: Pointer to HBA context object.
663 * @iocblist: List of IOCBs.
664 * @ulpstatus: ULP status in IOCB command field.
665 * @ulpWord4: ULP word-4 in IOCB command field.
666 *
667 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
668 * on the list by invoking the complete callback function associated with the
669 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
670 * fields.
671 **/
672void
673lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
674 uint32_t ulpstatus, uint32_t ulpWord4)
675{
676 struct lpfc_iocbq *piocb;
677
678 while (!list_empty(iocblist)) {
679 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
680
681 if (!piocb->iocb_cmpl)
682 lpfc_sli_release_iocbq(phba, piocb);
683 else {
684 piocb->iocb.ulpStatus = ulpstatus;
685 piocb->iocb.un.ulpWord[4] = ulpWord4;
686 (piocb->iocb_cmpl) (phba, piocb, piocb);
687 }
688 }
689 return;
690}
691
e59058c4 692/**
3621a710
JS
693 * lpfc_sli_iocb_cmd_type - Get the iocb type
694 * @iocb_cmnd: iocb command code.
e59058c4
JS
695 *
696 * This function is called by ring event handler function to get the iocb type.
697 * This function translates the iocb command to an iocb command type used to
698 * decide the final disposition of each completed IOCB.
699 * The function returns
700 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
701 * LPFC_SOL_IOCB if it is a solicited iocb completion
702 * LPFC_ABORT_IOCB if it is an abort iocb
703 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
704 *
705 * The caller is not required to hold any lock.
706 **/
dea3101e 707static lpfc_iocb_type
708lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
709{
710 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
711
712 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
713 return 0;
714
715 switch (iocb_cmnd) {
716 case CMD_XMIT_SEQUENCE_CR:
717 case CMD_XMIT_SEQUENCE_CX:
718 case CMD_XMIT_BCAST_CN:
719 case CMD_XMIT_BCAST_CX:
720 case CMD_ELS_REQUEST_CR:
721 case CMD_ELS_REQUEST_CX:
722 case CMD_CREATE_XRI_CR:
723 case CMD_CREATE_XRI_CX:
724 case CMD_GET_RPI_CN:
725 case CMD_XMIT_ELS_RSP_CX:
726 case CMD_GET_RPI_CR:
727 case CMD_FCP_IWRITE_CR:
728 case CMD_FCP_IWRITE_CX:
729 case CMD_FCP_IREAD_CR:
730 case CMD_FCP_IREAD_CX:
731 case CMD_FCP_ICMND_CR:
732 case CMD_FCP_ICMND_CX:
f5603511
JS
733 case CMD_FCP_TSEND_CX:
734 case CMD_FCP_TRSP_CX:
735 case CMD_FCP_TRECEIVE_CX:
736 case CMD_FCP_AUTO_TRSP_CX:
dea3101e 737 case CMD_ADAPTER_MSG:
738 case CMD_ADAPTER_DUMP:
739 case CMD_XMIT_SEQUENCE64_CR:
740 case CMD_XMIT_SEQUENCE64_CX:
741 case CMD_XMIT_BCAST64_CN:
742 case CMD_XMIT_BCAST64_CX:
743 case CMD_ELS_REQUEST64_CR:
744 case CMD_ELS_REQUEST64_CX:
745 case CMD_FCP_IWRITE64_CR:
746 case CMD_FCP_IWRITE64_CX:
747 case CMD_FCP_IREAD64_CR:
748 case CMD_FCP_IREAD64_CX:
749 case CMD_FCP_ICMND64_CR:
750 case CMD_FCP_ICMND64_CX:
f5603511
JS
751 case CMD_FCP_TSEND64_CX:
752 case CMD_FCP_TRSP64_CX:
753 case CMD_FCP_TRECEIVE64_CX:
dea3101e 754 case CMD_GEN_REQUEST64_CR:
755 case CMD_GEN_REQUEST64_CX:
756 case CMD_XMIT_ELS_RSP64_CX:
da0436e9
JS
757 case DSSCMD_IWRITE64_CR:
758 case DSSCMD_IWRITE64_CX:
759 case DSSCMD_IREAD64_CR:
760 case DSSCMD_IREAD64_CX:
761 case DSSCMD_INVALIDATE_DEK:
762 case DSSCMD_SET_KEK:
763 case DSSCMD_GET_KEK_ID:
764 case DSSCMD_GEN_XFER:
dea3101e 765 type = LPFC_SOL_IOCB;
766 break;
767 case CMD_ABORT_XRI_CN:
768 case CMD_ABORT_XRI_CX:
769 case CMD_CLOSE_XRI_CN:
770 case CMD_CLOSE_XRI_CX:
771 case CMD_XRI_ABORTED_CX:
772 case CMD_ABORT_MXRI64_CN:
6669f9bb 773 case CMD_XMIT_BLS_RSP64_CX:
dea3101e 774 type = LPFC_ABORT_IOCB;
775 break;
776 case CMD_RCV_SEQUENCE_CX:
777 case CMD_RCV_ELS_REQ_CX:
778 case CMD_RCV_SEQUENCE64_CX:
779 case CMD_RCV_ELS_REQ64_CX:
57127f15 780 case CMD_ASYNC_STATUS:
ed957684
JS
781 case CMD_IOCB_RCV_SEQ64_CX:
782 case CMD_IOCB_RCV_ELS64_CX:
783 case CMD_IOCB_RCV_CONT64_CX:
3163f725 784 case CMD_IOCB_RET_XRI64_CX:
dea3101e 785 type = LPFC_UNSOL_IOCB;
786 break;
3163f725
JS
787 case CMD_IOCB_XMIT_MSEQ64_CR:
788 case CMD_IOCB_XMIT_MSEQ64_CX:
789 case CMD_IOCB_RCV_SEQ_LIST64_CX:
790 case CMD_IOCB_RCV_ELS_LIST64_CX:
791 case CMD_IOCB_CLOSE_EXTENDED_CN:
792 case CMD_IOCB_ABORT_EXTENDED_CN:
793 case CMD_IOCB_RET_HBQE64_CN:
794 case CMD_IOCB_FCP_IBIDIR64_CR:
795 case CMD_IOCB_FCP_IBIDIR64_CX:
796 case CMD_IOCB_FCP_ITASKMGT64_CX:
797 case CMD_IOCB_LOGENTRY_CN:
798 case CMD_IOCB_LOGENTRY_ASYNC_CN:
799 printk("%s - Unhandled SLI-3 Command x%x\n",
cadbd4a5 800 __func__, iocb_cmnd);
3163f725
JS
801 type = LPFC_UNKNOWN_IOCB;
802 break;
dea3101e 803 default:
804 type = LPFC_UNKNOWN_IOCB;
805 break;
806 }
807
808 return type;
809}
810
e59058c4 811/**
3621a710 812 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
e59058c4
JS
813 * @phba: Pointer to HBA context object.
814 *
815 * This function is called from SLI initialization code
816 * to configure every ring of the HBA's SLI interface. The
817 * caller is not required to hold any lock. This function issues
818 * a config_ring mailbox command for each ring.
819 * This function returns zero if successful else returns a negative
820 * error code.
821 **/
dea3101e 822static int
ed957684 823lpfc_sli_ring_map(struct lpfc_hba *phba)
dea3101e 824{
825 struct lpfc_sli *psli = &phba->sli;
ed957684
JS
826 LPFC_MBOXQ_t *pmb;
827 MAILBOX_t *pmbox;
828 int i, rc, ret = 0;
dea3101e 829
ed957684
JS
830 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
831 if (!pmb)
832 return -ENOMEM;
04c68496 833 pmbox = &pmb->u.mb;
ed957684 834 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 835 for (i = 0; i < psli->num_rings; i++) {
dea3101e 836 lpfc_config_ring(phba, i, pmb);
837 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
838 if (rc != MBX_SUCCESS) {
92d7f7b0 839 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 840 "0446 Adapter failed to init (%d), "
dea3101e 841 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
842 "ring %d\n",
e8b62011
JS
843 rc, pmbox->mbxCommand,
844 pmbox->mbxStatus, i);
2e0fef85 845 phba->link_state = LPFC_HBA_ERROR;
ed957684
JS
846 ret = -ENXIO;
847 break;
dea3101e 848 }
849 }
ed957684
JS
850 mempool_free(pmb, phba->mbox_mem_pool);
851 return ret;
dea3101e 852}
853
e59058c4 854/**
3621a710 855 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
e59058c4
JS
856 * @phba: Pointer to HBA context object.
857 * @pring: Pointer to driver SLI ring object.
858 * @piocb: Pointer to the driver iocb object.
859 *
860 * This function is called with hbalock held. The function adds the
861 * new iocb to txcmplq of the given ring. This function always returns
862 * 0. If this function is called for ELS ring, this function checks if
863 * there is a vport associated with the ELS command. This function also
864 * starts els_tmofunc timer if this is an ELS command.
865 **/
dea3101e 866static int
2e0fef85
JS
867lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
868 struct lpfc_iocbq *piocb)
dea3101e 869{
dea3101e 870 list_add_tail(&piocb->list, &pring->txcmplq);
871 pring->txcmplq_cnt++;
92d7f7b0
JS
872 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
873 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
874 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
875 if (!piocb->vport)
876 BUG();
877 else
878 mod_timer(&piocb->vport->els_tmofunc,
879 jiffies + HZ * (phba->fc_ratov << 1));
880 }
881
dea3101e 882
2e0fef85 883 return 0;
dea3101e 884}
885
e59058c4 886/**
3621a710 887 * lpfc_sli_ringtx_get - Get first element of the txq
e59058c4
JS
888 * @phba: Pointer to HBA context object.
889 * @pring: Pointer to driver SLI ring object.
890 *
891 * This function is called with hbalock held to get next
892 * iocb in txq of the given ring. If there is any iocb in
893 * the txq, the function returns first iocb in the list after
894 * removing the iocb from the list, else it returns NULL.
895 **/
dea3101e 896static struct lpfc_iocbq *
2e0fef85 897lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 898{
dea3101e 899 struct lpfc_iocbq *cmd_iocb;
900
858c9f6c
JS
901 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
902 if (cmd_iocb != NULL)
dea3101e 903 pring->txq_cnt--;
2e0fef85 904 return cmd_iocb;
dea3101e 905}
906
e59058c4 907/**
3621a710 908 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
e59058c4
JS
909 * @phba: Pointer to HBA context object.
910 * @pring: Pointer to driver SLI ring object.
911 *
912 * This function is called with hbalock held and the caller must post the
913 * iocb without releasing the lock. If the caller releases the lock,
914 * iocb slot returned by the function is not guaranteed to be available.
915 * The function returns pointer to the next available iocb slot if there
916 * is available slot in the ring, else it returns NULL.
917 * If the get index of the ring is ahead of the put index, the function
918 * will post an error attention event to the worker thread to take the
919 * HBA to offline state.
920 **/
dea3101e 921static IOCB_t *
922lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
923{
34b02dcd 924 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 925 uint32_t max_cmd_idx = pring->numCiocb;
dea3101e 926 if ((pring->next_cmdidx == pring->cmdidx) &&
927 (++pring->next_cmdidx >= max_cmd_idx))
928 pring->next_cmdidx = 0;
929
930 if (unlikely(pring->local_getidx == pring->next_cmdidx)) {
931
932 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
933
934 if (unlikely(pring->local_getidx >= max_cmd_idx)) {
935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 936 "0315 Ring %d issue: portCmdGet %d "
025dfdaf 937 "is bigger than cmd ring %d\n",
e8b62011 938 pring->ringno,
dea3101e 939 pring->local_getidx, max_cmd_idx);
940
2e0fef85 941 phba->link_state = LPFC_HBA_ERROR;
dea3101e 942 /*
943 * All error attention handlers are posted to
944 * worker thread
945 */
946 phba->work_ha |= HA_ERATT;
947 phba->work_hs = HS_FFER3;
92d7f7b0 948
5e9d9b82 949 lpfc_worker_wake_up(phba);
dea3101e 950
951 return NULL;
952 }
953
954 if (pring->local_getidx == pring->next_cmdidx)
955 return NULL;
956 }
957
ed957684 958 return lpfc_cmd_iocb(phba, pring);
dea3101e 959}
960
e59058c4 961/**
3621a710 962 * lpfc_sli_next_iotag - Get an iotag for the iocb
e59058c4
JS
963 * @phba: Pointer to HBA context object.
964 * @iocbq: Pointer to driver iocb object.
965 *
966 * This function gets an iotag for the iocb. If there is no unused iotag and
967 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
968 * array and assigns a new iotag.
969 * The function returns the allocated iotag if successful, else returns zero.
970 * Zero is not a valid iotag.
971 * The caller is not required to hold any lock.
972 **/
604a3e30 973uint16_t
2e0fef85 974lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
dea3101e 975{
2e0fef85
JS
976 struct lpfc_iocbq **new_arr;
977 struct lpfc_iocbq **old_arr;
604a3e30
JB
978 size_t new_len;
979 struct lpfc_sli *psli = &phba->sli;
980 uint16_t iotag;
dea3101e 981
2e0fef85 982 spin_lock_irq(&phba->hbalock);
604a3e30
JB
983 iotag = psli->last_iotag;
984 if(++iotag < psli->iocbq_lookup_len) {
985 psli->last_iotag = iotag;
986 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 987 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
988 iocbq->iotag = iotag;
989 return iotag;
2e0fef85 990 } else if (psli->iocbq_lookup_len < (0xffff
604a3e30
JB
991 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
992 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
2e0fef85
JS
993 spin_unlock_irq(&phba->hbalock);
994 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
604a3e30
JB
995 GFP_KERNEL);
996 if (new_arr) {
2e0fef85 997 spin_lock_irq(&phba->hbalock);
604a3e30
JB
998 old_arr = psli->iocbq_lookup;
999 if (new_len <= psli->iocbq_lookup_len) {
1000 /* highly unprobable case */
1001 kfree(new_arr);
1002 iotag = psli->last_iotag;
1003 if(++iotag < psli->iocbq_lookup_len) {
1004 psli->last_iotag = iotag;
1005 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1006 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1007 iocbq->iotag = iotag;
1008 return iotag;
1009 }
2e0fef85 1010 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1011 return 0;
1012 }
1013 if (psli->iocbq_lookup)
1014 memcpy(new_arr, old_arr,
1015 ((psli->last_iotag + 1) *
311464ec 1016 sizeof (struct lpfc_iocbq *)));
604a3e30
JB
1017 psli->iocbq_lookup = new_arr;
1018 psli->iocbq_lookup_len = new_len;
1019 psli->last_iotag = iotag;
1020 psli->iocbq_lookup[iotag] = iocbq;
2e0fef85 1021 spin_unlock_irq(&phba->hbalock);
604a3e30
JB
1022 iocbq->iotag = iotag;
1023 kfree(old_arr);
1024 return iotag;
1025 }
8f6d98d2 1026 } else
2e0fef85 1027 spin_unlock_irq(&phba->hbalock);
dea3101e 1028
604a3e30 1029 lpfc_printf_log(phba, KERN_ERR,LOG_SLI,
e8b62011
JS
1030 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1031 psli->last_iotag);
dea3101e 1032
604a3e30 1033 return 0;
dea3101e 1034}
1035
e59058c4 1036/**
3621a710 1037 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
e59058c4
JS
1038 * @phba: Pointer to HBA context object.
1039 * @pring: Pointer to driver SLI ring object.
1040 * @iocb: Pointer to iocb slot in the ring.
1041 * @nextiocb: Pointer to driver iocb object which need to be
1042 * posted to firmware.
1043 *
1044 * This function is called with hbalock held to post a new iocb to
1045 * the firmware. This function copies the new iocb to ring iocb slot and
1046 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1047 * a completion call back for this iocb else the function will free the
1048 * iocb object.
1049 **/
dea3101e 1050static void
1051lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1052 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1053{
1054 /*
604a3e30 1055 * Set up an iotag
dea3101e 1056 */
604a3e30 1057 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
dea3101e 1058
e2a0a9d6 1059
a58cbd52
JS
1060 if (pring->ringno == LPFC_ELS_RING) {
1061 lpfc_debugfs_slow_ring_trc(phba,
1062 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1063 *(((uint32_t *) &nextiocb->iocb) + 4),
1064 *(((uint32_t *) &nextiocb->iocb) + 6),
1065 *(((uint32_t *) &nextiocb->iocb) + 7));
1066 }
1067
dea3101e 1068 /*
1069 * Issue iocb command to adapter
1070 */
92d7f7b0 1071 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
dea3101e 1072 wmb();
1073 pring->stats.iocb_cmd++;
1074
1075 /*
1076 * If there is no completion routine to call, we can release the
1077 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1078 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1079 */
1080 if (nextiocb->iocb_cmpl)
1081 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
604a3e30 1082 else
2e0fef85 1083 __lpfc_sli_release_iocbq(phba, nextiocb);
dea3101e 1084
1085 /*
1086 * Let the HBA know what IOCB slot will be the next one the
1087 * driver will put a command into.
1088 */
1089 pring->cmdidx = pring->next_cmdidx;
ed957684 1090 writel(pring->cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
dea3101e 1091}
1092
e59058c4 1093/**
3621a710 1094 * lpfc_sli_update_full_ring - Update the chip attention register
e59058c4
JS
1095 * @phba: Pointer to HBA context object.
1096 * @pring: Pointer to driver SLI ring object.
1097 *
1098 * The caller is not required to hold any lock for calling this function.
1099 * This function updates the chip attention bits for the ring to inform firmware
1100 * that there are pending work to be done for this ring and requests an
1101 * interrupt when there is space available in the ring. This function is
1102 * called when the driver is unable to post more iocbs to the ring due
1103 * to unavailability of space in the ring.
1104 **/
dea3101e 1105static void
2e0fef85 1106lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1107{
1108 int ringno = pring->ringno;
1109
1110 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1111
1112 wmb();
1113
1114 /*
1115 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1116 * The HBA will tell us when an IOCB entry is available.
1117 */
1118 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1119 readl(phba->CAregaddr); /* flush */
1120
1121 pring->stats.iocb_cmd_full++;
1122}
1123
e59058c4 1124/**
3621a710 1125 * lpfc_sli_update_ring - Update chip attention register
e59058c4
JS
1126 * @phba: Pointer to HBA context object.
1127 * @pring: Pointer to driver SLI ring object.
1128 *
1129 * This function updates the chip attention register bit for the
1130 * given ring to inform HBA that there is more work to be done
1131 * in this ring. The caller is not required to hold any lock.
1132 **/
dea3101e 1133static void
2e0fef85 1134lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1135{
1136 int ringno = pring->ringno;
1137
1138 /*
1139 * Tell the HBA that there is work to do in this ring.
1140 */
34b02dcd
JS
1141 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1142 wmb();
1143 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1144 readl(phba->CAregaddr); /* flush */
1145 }
dea3101e 1146}
1147
e59058c4 1148/**
3621a710 1149 * lpfc_sli_resume_iocb - Process iocbs in the txq
e59058c4
JS
1150 * @phba: Pointer to HBA context object.
1151 * @pring: Pointer to driver SLI ring object.
1152 *
1153 * This function is called with hbalock held to post pending iocbs
1154 * in the txq to the firmware. This function is called when driver
1155 * detects space available in the ring.
1156 **/
dea3101e 1157static void
2e0fef85 1158lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
dea3101e 1159{
1160 IOCB_t *iocb;
1161 struct lpfc_iocbq *nextiocb;
1162
1163 /*
1164 * Check to see if:
1165 * (a) there is anything on the txq to send
1166 * (b) link is up
1167 * (c) link attention events can be processed (fcp ring only)
1168 * (d) IOCB processing is not blocked by the outstanding mbox command.
1169 */
1170 if (pring->txq_cnt &&
2e0fef85 1171 lpfc_is_link_up(phba) &&
dea3101e 1172 (pring->ringno != phba->sli.fcp_ring ||
0b727fea 1173 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
dea3101e 1174
1175 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1176 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1177 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1178
1179 if (iocb)
1180 lpfc_sli_update_ring(phba, pring);
1181 else
1182 lpfc_sli_update_full_ring(phba, pring);
1183 }
1184
1185 return;
1186}
1187
e59058c4 1188/**
3621a710 1189 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
e59058c4
JS
1190 * @phba: Pointer to HBA context object.
1191 * @hbqno: HBQ number.
1192 *
1193 * This function is called with hbalock held to get the next
1194 * available slot for the given HBQ. If there is free slot
1195 * available for the HBQ it will return pointer to the next available
1196 * HBQ entry else it will return NULL.
1197 **/
a6ababd2 1198static struct lpfc_hbq_entry *
ed957684
JS
1199lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1200{
1201 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1202
1203 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1204 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1205 hbqp->next_hbqPutIdx = 0;
1206
1207 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
92d7f7b0 1208 uint32_t raw_index = phba->hbq_get[hbqno];
ed957684
JS
1209 uint32_t getidx = le32_to_cpu(raw_index);
1210
1211 hbqp->local_hbqGetIdx = getidx;
1212
1213 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1214 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 1215 LOG_SLI | LOG_VPORT,
e8b62011 1216 "1802 HBQ %d: local_hbqGetIdx "
ed957684 1217 "%u is > than hbqp->entry_count %u\n",
e8b62011 1218 hbqno, hbqp->local_hbqGetIdx,
ed957684
JS
1219 hbqp->entry_count);
1220
1221 phba->link_state = LPFC_HBA_ERROR;
1222 return NULL;
1223 }
1224
1225 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1226 return NULL;
1227 }
1228
51ef4c26
JS
1229 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1230 hbqp->hbqPutIdx;
ed957684
JS
1231}
1232
e59058c4 1233/**
3621a710 1234 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
e59058c4
JS
1235 * @phba: Pointer to HBA context object.
1236 *
1237 * This function is called with no lock held to free all the
1238 * hbq buffers while uninitializing the SLI interface. It also
1239 * frees the HBQ buffers returned by the firmware but not yet
1240 * processed by the upper layers.
1241 **/
ed957684
JS
1242void
1243lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1244{
92d7f7b0
JS
1245 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1246 struct hbq_dmabuf *hbq_buf;
3163f725 1247 unsigned long flags;
51ef4c26 1248 int i, hbq_count;
3163f725 1249 uint32_t hbqno;
ed957684 1250
51ef4c26 1251 hbq_count = lpfc_sli_hbq_count();
ed957684 1252 /* Return all memory used by all HBQs */
3163f725 1253 spin_lock_irqsave(&phba->hbalock, flags);
51ef4c26
JS
1254 for (i = 0; i < hbq_count; ++i) {
1255 list_for_each_entry_safe(dmabuf, next_dmabuf,
1256 &phba->hbqs[i].hbq_buffer_list, list) {
1257 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1258 list_del(&hbq_buf->dbuf.list);
1259 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1260 }
a8adb832 1261 phba->hbqs[i].buffer_count = 0;
ed957684 1262 }
3163f725 1263 /* Return all HBQ buffer that are in-fly */
3772a991
JS
1264 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1265 list) {
3163f725
JS
1266 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1267 list_del(&hbq_buf->dbuf.list);
1268 if (hbq_buf->tag == -1) {
1269 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1270 (phba, hbq_buf);
1271 } else {
1272 hbqno = hbq_buf->tag >> 16;
1273 if (hbqno >= LPFC_MAX_HBQS)
1274 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1275 (phba, hbq_buf);
1276 else
1277 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1278 hbq_buf);
1279 }
1280 }
1281
1282 /* Mark the HBQs not in use */
1283 phba->hbq_in_use = 0;
1284 spin_unlock_irqrestore(&phba->hbalock, flags);
ed957684
JS
1285}
1286
e59058c4 1287/**
3621a710 1288 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
e59058c4
JS
1289 * @phba: Pointer to HBA context object.
1290 * @hbqno: HBQ number.
1291 * @hbq_buf: Pointer to HBQ buffer.
1292 *
1293 * This function is called with the hbalock held to post a
1294 * hbq buffer to the firmware. If the function finds an empty
1295 * slot in the HBQ, it will post the buffer. The function will return
1296 * pointer to the hbq entry if it successfully post the buffer
1297 * else it will return NULL.
1298 **/
3772a991 1299static int
ed957684 1300lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
92d7f7b0 1301 struct hbq_dmabuf *hbq_buf)
3772a991
JS
1302{
1303 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1304}
1305
1306/**
1307 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1308 * @phba: Pointer to HBA context object.
1309 * @hbqno: HBQ number.
1310 * @hbq_buf: Pointer to HBQ buffer.
1311 *
1312 * This function is called with the hbalock held to post a hbq buffer to the
1313 * firmware. If the function finds an empty slot in the HBQ, it will post the
1314 * buffer and place it on the hbq_buffer_list. The function will return zero if
1315 * it successfully post the buffer else it will return an error.
1316 **/
1317static int
1318lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1319 struct hbq_dmabuf *hbq_buf)
ed957684
JS
1320{
1321 struct lpfc_hbq_entry *hbqe;
92d7f7b0 1322 dma_addr_t physaddr = hbq_buf->dbuf.phys;
ed957684
JS
1323
1324 /* Get next HBQ entry slot to use */
1325 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1326 if (hbqe) {
1327 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1328
92d7f7b0
JS
1329 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1330 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
51ef4c26 1331 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
ed957684 1332 hbqe->bde.tus.f.bdeFlags = 0;
92d7f7b0
JS
1333 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1334 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1335 /* Sync SLIM */
ed957684
JS
1336 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1337 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
92d7f7b0 1338 /* flush */
ed957684 1339 readl(phba->hbq_put + hbqno);
51ef4c26 1340 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
3772a991
JS
1341 return 0;
1342 } else
1343 return -ENOMEM;
ed957684
JS
1344}
1345
4f774513
JS
1346/**
1347 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1348 * @phba: Pointer to HBA context object.
1349 * @hbqno: HBQ number.
1350 * @hbq_buf: Pointer to HBQ buffer.
1351 *
1352 * This function is called with the hbalock held to post an RQE to the SLI4
1353 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1354 * the hbq_buffer_list and return zero, otherwise it will return an error.
1355 **/
1356static int
1357lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1358 struct hbq_dmabuf *hbq_buf)
1359{
1360 int rc;
1361 struct lpfc_rqe hrqe;
1362 struct lpfc_rqe drqe;
1363
1364 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1365 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1366 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1367 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1368 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1369 &hrqe, &drqe);
1370 if (rc < 0)
1371 return rc;
1372 hbq_buf->tag = rc;
1373 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1374 return 0;
1375}
1376
e59058c4 1377/* HBQ for ELS and CT traffic. */
92d7f7b0
JS
1378static struct lpfc_hbq_init lpfc_els_hbq = {
1379 .rn = 1,
1380 .entry_count = 200,
1381 .mask_count = 0,
1382 .profile = 0,
51ef4c26 1383 .ring_mask = (1 << LPFC_ELS_RING),
92d7f7b0 1384 .buffer_count = 0,
a257bf90
JS
1385 .init_count = 40,
1386 .add_count = 40,
92d7f7b0 1387};
ed957684 1388
e59058c4 1389/* HBQ for the extra ring if needed */
51ef4c26
JS
1390static struct lpfc_hbq_init lpfc_extra_hbq = {
1391 .rn = 1,
1392 .entry_count = 200,
1393 .mask_count = 0,
1394 .profile = 0,
1395 .ring_mask = (1 << LPFC_EXTRA_RING),
1396 .buffer_count = 0,
1397 .init_count = 0,
1398 .add_count = 5,
1399};
1400
e59058c4 1401/* Array of HBQs */
78b2d852 1402struct lpfc_hbq_init *lpfc_hbq_defs[] = {
92d7f7b0 1403 &lpfc_els_hbq,
51ef4c26 1404 &lpfc_extra_hbq,
92d7f7b0 1405};
ed957684 1406
e59058c4 1407/**
3621a710 1408 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
e59058c4
JS
1409 * @phba: Pointer to HBA context object.
1410 * @hbqno: HBQ number.
1411 * @count: Number of HBQ buffers to be posted.
1412 *
d7c255b2
JS
1413 * This function is called with no lock held to post more hbq buffers to the
1414 * given HBQ. The function returns the number of HBQ buffers successfully
1415 * posted.
e59058c4 1416 **/
311464ec 1417static int
92d7f7b0 1418lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
ed957684 1419{
d7c255b2 1420 uint32_t i, posted = 0;
3163f725 1421 unsigned long flags;
92d7f7b0 1422 struct hbq_dmabuf *hbq_buffer;
d7c255b2 1423 LIST_HEAD(hbq_buf_list);
eafe1df9 1424 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
51ef4c26 1425 return 0;
51ef4c26 1426
d7c255b2
JS
1427 if ((phba->hbqs[hbqno].buffer_count + count) >
1428 lpfc_hbq_defs[hbqno]->entry_count)
1429 count = lpfc_hbq_defs[hbqno]->entry_count -
1430 phba->hbqs[hbqno].buffer_count;
1431 if (!count)
1432 return 0;
1433 /* Allocate HBQ entries */
1434 for (i = 0; i < count; i++) {
1435 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1436 if (!hbq_buffer)
1437 break;
1438 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1439 }
3163f725
JS
1440 /* Check whether HBQ is still in use */
1441 spin_lock_irqsave(&phba->hbalock, flags);
eafe1df9 1442 if (!phba->hbq_in_use)
d7c255b2
JS
1443 goto err;
1444 while (!list_empty(&hbq_buf_list)) {
1445 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1446 dbuf.list);
1447 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1448 (hbqno << 16));
3772a991 1449 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
a8adb832 1450 phba->hbqs[hbqno].buffer_count++;
d7c255b2
JS
1451 posted++;
1452 } else
51ef4c26 1453 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684 1454 }
3163f725 1455 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1456 return posted;
1457err:
eafe1df9 1458 spin_unlock_irqrestore(&phba->hbalock, flags);
d7c255b2
JS
1459 while (!list_empty(&hbq_buf_list)) {
1460 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1461 dbuf.list);
1462 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1463 }
1464 return 0;
ed957684
JS
1465}
1466
e59058c4 1467/**
3621a710 1468 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
e59058c4
JS
1469 * @phba: Pointer to HBA context object.
1470 * @qno: HBQ number.
1471 *
1472 * This function posts more buffers to the HBQ. This function
d7c255b2
JS
1473 * is called with no lock held. The function returns the number of HBQ entries
1474 * successfully allocated.
e59058c4 1475 **/
92d7f7b0
JS
1476int
1477lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
ed957684 1478{
92d7f7b0
JS
1479 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1480 lpfc_hbq_defs[qno]->add_count));
1481}
ed957684 1482
e59058c4 1483/**
3621a710 1484 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
e59058c4
JS
1485 * @phba: Pointer to HBA context object.
1486 * @qno: HBQ queue number.
1487 *
1488 * This function is called from SLI initialization code path with
1489 * no lock held to post initial HBQ buffers to firmware. The
d7c255b2 1490 * function returns the number of HBQ entries successfully allocated.
e59058c4 1491 **/
a6ababd2 1492static int
92d7f7b0
JS
1493lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1494{
1495 return(lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1496 lpfc_hbq_defs[qno]->init_count));
ed957684
JS
1497}
1498
3772a991
JS
1499/**
1500 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1501 * @phba: Pointer to HBA context object.
1502 * @hbqno: HBQ number.
1503 *
1504 * This function removes the first hbq buffer on an hbq list and returns a
1505 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1506 **/
1507static struct hbq_dmabuf *
1508lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1509{
1510 struct lpfc_dmabuf *d_buf;
1511
1512 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1513 if (!d_buf)
1514 return NULL;
1515 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1516}
1517
e59058c4 1518/**
3621a710 1519 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
e59058c4
JS
1520 * @phba: Pointer to HBA context object.
1521 * @tag: Tag of the hbq buffer.
1522 *
1523 * This function is called with hbalock held. This function searches
1524 * for the hbq buffer associated with the given tag in the hbq buffer
1525 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1526 * it returns NULL.
1527 **/
a6ababd2 1528static struct hbq_dmabuf *
92d7f7b0 1529lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
ed957684 1530{
92d7f7b0
JS
1531 struct lpfc_dmabuf *d_buf;
1532 struct hbq_dmabuf *hbq_buf;
51ef4c26
JS
1533 uint32_t hbqno;
1534
1535 hbqno = tag >> 16;
a0a74e45 1536 if (hbqno >= LPFC_MAX_HBQS)
51ef4c26 1537 return NULL;
ed957684 1538
3772a991 1539 spin_lock_irq(&phba->hbalock);
51ef4c26 1540 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
92d7f7b0 1541 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
51ef4c26 1542 if (hbq_buf->tag == tag) {
3772a991 1543 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1544 return hbq_buf;
ed957684
JS
1545 }
1546 }
3772a991 1547 spin_unlock_irq(&phba->hbalock);
92d7f7b0 1548 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
e8b62011 1549 "1803 Bad hbq tag. Data: x%x x%x\n",
a8adb832 1550 tag, phba->hbqs[tag >> 16].buffer_count);
92d7f7b0 1551 return NULL;
ed957684
JS
1552}
1553
e59058c4 1554/**
3621a710 1555 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
e59058c4
JS
1556 * @phba: Pointer to HBA context object.
1557 * @hbq_buffer: Pointer to HBQ buffer.
1558 *
1559 * This function is called with hbalock. This function gives back
1560 * the hbq buffer to firmware. If the HBQ does not have space to
1561 * post the buffer, it will free the buffer.
1562 **/
ed957684 1563void
51ef4c26 1564lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
ed957684
JS
1565{
1566 uint32_t hbqno;
1567
51ef4c26
JS
1568 if (hbq_buffer) {
1569 hbqno = hbq_buffer->tag >> 16;
3772a991 1570 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
51ef4c26 1571 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
ed957684
JS
1572 }
1573}
1574
e59058c4 1575/**
3621a710 1576 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
e59058c4
JS
1577 * @mbxCommand: mailbox command code.
1578 *
1579 * This function is called by the mailbox event handler function to verify
1580 * that the completed mailbox command is a legitimate mailbox command. If the
1581 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
1582 * and the mailbox event handler will take the HBA offline.
1583 **/
dea3101e 1584static int
1585lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
1586{
1587 uint8_t ret;
1588
1589 switch (mbxCommand) {
1590 case MBX_LOAD_SM:
1591 case MBX_READ_NV:
1592 case MBX_WRITE_NV:
a8adb832 1593 case MBX_WRITE_VPARMS:
dea3101e 1594 case MBX_RUN_BIU_DIAG:
1595 case MBX_INIT_LINK:
1596 case MBX_DOWN_LINK:
1597 case MBX_CONFIG_LINK:
1598 case MBX_CONFIG_RING:
1599 case MBX_RESET_RING:
1600 case MBX_READ_CONFIG:
1601 case MBX_READ_RCONFIG:
1602 case MBX_READ_SPARM:
1603 case MBX_READ_STATUS:
1604 case MBX_READ_RPI:
1605 case MBX_READ_XRI:
1606 case MBX_READ_REV:
1607 case MBX_READ_LNK_STAT:
1608 case MBX_REG_LOGIN:
1609 case MBX_UNREG_LOGIN:
1610 case MBX_READ_LA:
1611 case MBX_CLEAR_LA:
1612 case MBX_DUMP_MEMORY:
1613 case MBX_DUMP_CONTEXT:
1614 case MBX_RUN_DIAGS:
1615 case MBX_RESTART:
1616 case MBX_UPDATE_CFG:
1617 case MBX_DOWN_LOAD:
1618 case MBX_DEL_LD_ENTRY:
1619 case MBX_RUN_PROGRAM:
1620 case MBX_SET_MASK:
09372820 1621 case MBX_SET_VARIABLE:
dea3101e 1622 case MBX_UNREG_D_ID:
41415862 1623 case MBX_KILL_BOARD:
dea3101e 1624 case MBX_CONFIG_FARP:
41415862 1625 case MBX_BEACON:
dea3101e 1626 case MBX_LOAD_AREA:
1627 case MBX_RUN_BIU_DIAG64:
1628 case MBX_CONFIG_PORT:
1629 case MBX_READ_SPARM64:
1630 case MBX_READ_RPI64:
1631 case MBX_REG_LOGIN64:
1632 case MBX_READ_LA64:
09372820 1633 case MBX_WRITE_WWN:
dea3101e 1634 case MBX_SET_DEBUG:
1635 case MBX_LOAD_EXP_ROM:
57127f15 1636 case MBX_ASYNCEVT_ENABLE:
92d7f7b0
JS
1637 case MBX_REG_VPI:
1638 case MBX_UNREG_VPI:
858c9f6c 1639 case MBX_HEARTBEAT:
84774a4d
JS
1640 case MBX_PORT_CAPABILITIES:
1641 case MBX_PORT_IOV_CONTROL:
04c68496
JS
1642 case MBX_SLI4_CONFIG:
1643 case MBX_SLI4_REQ_FTRS:
1644 case MBX_REG_FCFI:
1645 case MBX_UNREG_FCFI:
1646 case MBX_REG_VFI:
1647 case MBX_UNREG_VFI:
1648 case MBX_INIT_VPI:
1649 case MBX_INIT_VFI:
1650 case MBX_RESUME_RPI:
dea3101e 1651 ret = mbxCommand;
1652 break;
1653 default:
1654 ret = MBX_SHUTDOWN;
1655 break;
1656 }
2e0fef85 1657 return ret;
dea3101e 1658}
e59058c4
JS
1659
1660/**
3621a710 1661 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
e59058c4
JS
1662 * @phba: Pointer to HBA context object.
1663 * @pmboxq: Pointer to mailbox command.
1664 *
1665 * This is completion handler function for mailbox commands issued from
1666 * lpfc_sli_issue_mbox_wait function. This function is called by the
1667 * mailbox event handler function with no lock held. This function
1668 * will wake up thread waiting on the wait queue pointed by context1
1669 * of the mailbox.
1670 **/
04c68496 1671void
2e0fef85 1672lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
dea3101e 1673{
1674 wait_queue_head_t *pdone_q;
858c9f6c 1675 unsigned long drvr_flag;
dea3101e 1676
1677 /*
1678 * If pdone_q is empty, the driver thread gave up waiting and
1679 * continued running.
1680 */
7054a606 1681 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
858c9f6c 1682 spin_lock_irqsave(&phba->hbalock, drvr_flag);
dea3101e 1683 pdone_q = (wait_queue_head_t *) pmboxq->context1;
1684 if (pdone_q)
1685 wake_up_interruptible(pdone_q);
858c9f6c 1686 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 1687 return;
1688}
1689
e59058c4
JS
1690
1691/**
3621a710 1692 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
e59058c4
JS
1693 * @phba: Pointer to HBA context object.
1694 * @pmb: Pointer to mailbox object.
1695 *
1696 * This function is the default mailbox completion handler. It
1697 * frees the memory resources associated with the completed mailbox
1698 * command. If the completed command is a REG_LOGIN mailbox command,
1699 * this function will issue a UREG_LOGIN to re-claim the RPI.
1700 **/
dea3101e 1701void
2e0fef85 1702lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
dea3101e 1703{
1704 struct lpfc_dmabuf *mp;
04c68496 1705 uint16_t rpi, vpi;
7054a606
JS
1706 int rc;
1707
dea3101e 1708 mp = (struct lpfc_dmabuf *) (pmb->context1);
7054a606 1709
dea3101e 1710 if (mp) {
1711 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1712 kfree(mp);
1713 }
7054a606 1714
04c68496
JS
1715 if ((pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) &&
1716 (phba->sli_rev == LPFC_SLI_REV4))
1717 lpfc_sli4_free_rpi(phba, pmb->u.mb.un.varUnregLogin.rpi);
1718
7054a606
JS
1719 /*
1720 * If a REG_LOGIN succeeded after node is destroyed or node
1721 * is in re-discovery driver need to cleanup the RPI.
1722 */
2e0fef85 1723 if (!(phba->pport->load_flag & FC_UNLOADING) &&
04c68496
JS
1724 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
1725 !pmb->u.mb.mbxStatus) {
1726 rpi = pmb->u.mb.un.varWords[0];
1727 vpi = pmb->u.mb.un.varRegLogin.vpi - phba->vpi_base;
1728 lpfc_unreg_login(phba, vpi, rpi, pmb);
92d7f7b0 1729 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7054a606
JS
1730 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
1731 if (rc != MBX_NOT_FINISHED)
1732 return;
1733 }
1734
04c68496
JS
1735 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
1736 lpfc_sli4_mbox_cmd_free(phba, pmb);
1737 else
1738 mempool_free(pmb, phba->mbox_mem_pool);
dea3101e 1739}
1740
e59058c4 1741/**
3621a710 1742 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
e59058c4
JS
1743 * @phba: Pointer to HBA context object.
1744 *
1745 * This function is called with no lock held. This function processes all
1746 * the completed mailbox commands and gives it to upper layers. The interrupt
1747 * service routine processes mailbox completion interrupt and adds completed
1748 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
1749 * Worker thread call lpfc_sli_handle_mb_event, which will return the
1750 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
1751 * function returns the mailbox commands to the upper layer by calling the
1752 * completion handler function of each mailbox.
1753 **/
dea3101e 1754int
2e0fef85 1755lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
dea3101e 1756{
92d7f7b0 1757 MAILBOX_t *pmbox;
dea3101e 1758 LPFC_MBOXQ_t *pmb;
92d7f7b0
JS
1759 int rc;
1760 LIST_HEAD(cmplq);
dea3101e 1761
1762 phba->sli.slistat.mbox_event++;
1763
92d7f7b0
JS
1764 /* Get all completed mailboxe buffers into the cmplq */
1765 spin_lock_irq(&phba->hbalock);
1766 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
1767 spin_unlock_irq(&phba->hbalock);
dea3101e 1768
92d7f7b0
JS
1769 /* Get a Mailbox buffer to setup mailbox commands for callback */
1770 do {
1771 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
1772 if (pmb == NULL)
1773 break;
2e0fef85 1774
04c68496 1775 pmbox = &pmb->u.mb;
dea3101e 1776
858c9f6c
JS
1777 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
1778 if (pmb->vport) {
1779 lpfc_debugfs_disc_trc(pmb->vport,
1780 LPFC_DISC_TRC_MBOX_VPORT,
1781 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
1782 (uint32_t)pmbox->mbxCommand,
1783 pmbox->un.varWords[0],
1784 pmbox->un.varWords[1]);
1785 }
1786 else {
1787 lpfc_debugfs_disc_trc(phba->pport,
1788 LPFC_DISC_TRC_MBOX,
1789 "MBOX cmpl: cmd:x%x mb:x%x x%x",
1790 (uint32_t)pmbox->mbxCommand,
1791 pmbox->un.varWords[0],
1792 pmbox->un.varWords[1]);
1793 }
1794 }
1795
dea3101e 1796 /*
1797 * It is a fatal error if unknown mbox command completion.
1798 */
1799 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
1800 MBX_SHUTDOWN) {
dea3101e 1801 /* Unknow mailbox command compl */
92d7f7b0 1802 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
e8b62011 1803 "(%d):0323 Unknown Mailbox command "
04c68496 1804 "x%x (x%x) Cmpl\n",
92d7f7b0 1805 pmb->vport ? pmb->vport->vpi : 0,
04c68496
JS
1806 pmbox->mbxCommand,
1807 lpfc_sli4_mbox_opcode_get(phba, pmb));
2e0fef85 1808 phba->link_state = LPFC_HBA_ERROR;
dea3101e 1809 phba->work_hs = HS_FFER3;
1810 lpfc_handle_eratt(phba);
92d7f7b0 1811 continue;
dea3101e 1812 }
1813
dea3101e 1814 if (pmbox->mbxStatus) {
1815 phba->sli.slistat.mbox_stat_err++;
1816 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
1817 /* Mbox cmd cmpl error - RETRYing */
92d7f7b0
JS
1818 lpfc_printf_log(phba, KERN_INFO,
1819 LOG_MBOX | LOG_SLI,
e8b62011 1820 "(%d):0305 Mbox cmd cmpl "
92d7f7b0 1821 "error - RETRYing Data: x%x "
04c68496 1822 "(x%x) x%x x%x x%x\n",
92d7f7b0
JS
1823 pmb->vport ? pmb->vport->vpi :0,
1824 pmbox->mbxCommand,
04c68496
JS
1825 lpfc_sli4_mbox_opcode_get(phba,
1826 pmb),
92d7f7b0
JS
1827 pmbox->mbxStatus,
1828 pmbox->un.varWords[0],
1829 pmb->vport->port_state);
dea3101e 1830 pmbox->mbxStatus = 0;
1831 pmbox->mbxOwner = OWN_HOST;
dea3101e 1832 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
04c68496 1833 if (rc != MBX_NOT_FINISHED)
92d7f7b0 1834 continue;
dea3101e 1835 }
1836 }
1837
1838 /* Mailbox cmd <cmd> Cmpl <cmpl> */
92d7f7b0 1839 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
04c68496 1840 "(%d):0307 Mailbox cmd x%x (x%x) Cmpl x%p "
dea3101e 1841 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
92d7f7b0 1842 pmb->vport ? pmb->vport->vpi : 0,
dea3101e 1843 pmbox->mbxCommand,
04c68496 1844 lpfc_sli4_mbox_opcode_get(phba, pmb),
dea3101e 1845 pmb->mbox_cmpl,
1846 *((uint32_t *) pmbox),
1847 pmbox->un.varWords[0],
1848 pmbox->un.varWords[1],
1849 pmbox->un.varWords[2],
1850 pmbox->un.varWords[3],
1851 pmbox->un.varWords[4],
1852 pmbox->un.varWords[5],
1853 pmbox->un.varWords[6],
1854 pmbox->un.varWords[7]);
1855
92d7f7b0 1856 if (pmb->mbox_cmpl)
dea3101e 1857 pmb->mbox_cmpl(phba,pmb);
92d7f7b0
JS
1858 } while (1);
1859 return 0;
1860}
dea3101e 1861
e59058c4 1862/**
3621a710 1863 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
e59058c4
JS
1864 * @phba: Pointer to HBA context object.
1865 * @pring: Pointer to driver SLI ring object.
1866 * @tag: buffer tag.
1867 *
1868 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
1869 * is set in the tag the buffer is posted for a particular exchange,
1870 * the function will return the buffer without replacing the buffer.
1871 * If the buffer is for unsolicited ELS or CT traffic, this function
1872 * returns the buffer and also posts another buffer to the firmware.
1873 **/
76bb24ef
JS
1874static struct lpfc_dmabuf *
1875lpfc_sli_get_buff(struct lpfc_hba *phba,
9f1e1b50
JS
1876 struct lpfc_sli_ring *pring,
1877 uint32_t tag)
76bb24ef 1878{
9f1e1b50
JS
1879 struct hbq_dmabuf *hbq_entry;
1880
76bb24ef
JS
1881 if (tag & QUE_BUFTAG_BIT)
1882 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
9f1e1b50
JS
1883 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
1884 if (!hbq_entry)
1885 return NULL;
1886 return &hbq_entry->dbuf;
76bb24ef 1887}
57127f15 1888
3772a991
JS
1889/**
1890 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
1891 * @phba: Pointer to HBA context object.
1892 * @pring: Pointer to driver SLI ring object.
1893 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
1894 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
1895 * @fch_type: the type for the first frame of the sequence.
1896 *
1897 * This function is called with no lock held. This function uses the r_ctl and
1898 * type of the received sequence to find the correct callback function to call
1899 * to process the sequence.
1900 **/
1901static int
1902lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1903 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
1904 uint32_t fch_type)
1905{
1906 int i;
1907
1908 /* unSolicited Responses */
1909 if (pring->prt[0].profile) {
1910 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
1911 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
1912 saveq);
1913 return 1;
1914 }
1915 /* We must search, based on rctl / type
1916 for the right routine */
1917 for (i = 0; i < pring->num_mask; i++) {
1918 if ((pring->prt[i].rctl == fch_r_ctl) &&
1919 (pring->prt[i].type == fch_type)) {
1920 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
1921 (pring->prt[i].lpfc_sli_rcv_unsol_event)
1922 (phba, pring, saveq);
1923 return 1;
1924 }
1925 }
1926 return 0;
1927}
e59058c4
JS
1928
1929/**
3621a710 1930 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
e59058c4
JS
1931 * @phba: Pointer to HBA context object.
1932 * @pring: Pointer to driver SLI ring object.
1933 * @saveq: Pointer to the unsolicited iocb.
1934 *
1935 * This function is called with no lock held by the ring event handler
1936 * when there is an unsolicited iocb posted to the response ring by the
1937 * firmware. This function gets the buffer associated with the iocbs
1938 * and calls the event handler for the ring. This function handles both
1939 * qring buffers and hbq buffers.
1940 * When the function returns 1 the caller can free the iocb object otherwise
1941 * upper layer functions will free the iocb objects.
1942 **/
dea3101e 1943static int
1944lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1945 struct lpfc_iocbq *saveq)
1946{
1947 IOCB_t * irsp;
1948 WORD5 * w5p;
1949 uint32_t Rctl, Type;
3772a991 1950 uint32_t match;
76bb24ef 1951 struct lpfc_iocbq *iocbq;
3163f725 1952 struct lpfc_dmabuf *dmzbuf;
dea3101e 1953
1954 match = 0;
1955 irsp = &(saveq->iocb);
57127f15
JS
1956
1957 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
1958 if (pring->lpfc_sli_rcv_async_status)
1959 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
1960 else
1961 lpfc_printf_log(phba,
1962 KERN_WARNING,
1963 LOG_SLI,
1964 "0316 Ring %d handler: unexpected "
1965 "ASYNC_STATUS iocb received evt_code "
1966 "0x%x\n",
1967 pring->ringno,
1968 irsp->un.asyncstat.evt_code);
1969 return 1;
1970 }
1971
3163f725
JS
1972 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
1973 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
1974 if (irsp->ulpBdeCount > 0) {
1975 dmzbuf = lpfc_sli_get_buff(phba, pring,
1976 irsp->un.ulpWord[3]);
1977 lpfc_in_buf_free(phba, dmzbuf);
1978 }
1979
1980 if (irsp->ulpBdeCount > 1) {
1981 dmzbuf = lpfc_sli_get_buff(phba, pring,
1982 irsp->unsli3.sli3Words[3]);
1983 lpfc_in_buf_free(phba, dmzbuf);
1984 }
1985
1986 if (irsp->ulpBdeCount > 2) {
1987 dmzbuf = lpfc_sli_get_buff(phba, pring,
1988 irsp->unsli3.sli3Words[7]);
1989 lpfc_in_buf_free(phba, dmzbuf);
1990 }
1991
1992 return 1;
1993 }
1994
92d7f7b0 1995 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
76bb24ef
JS
1996 if (irsp->ulpBdeCount != 0) {
1997 saveq->context2 = lpfc_sli_get_buff(phba, pring,
1998 irsp->un.ulpWord[3]);
1999 if (!saveq->context2)
2000 lpfc_printf_log(phba,
2001 KERN_ERR,
2002 LOG_SLI,
2003 "0341 Ring %d Cannot find buffer for "
2004 "an unsolicited iocb. tag 0x%x\n",
2005 pring->ringno,
2006 irsp->un.ulpWord[3]);
76bb24ef
JS
2007 }
2008 if (irsp->ulpBdeCount == 2) {
2009 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2010 irsp->unsli3.sli3Words[7]);
2011 if (!saveq->context3)
2012 lpfc_printf_log(phba,
2013 KERN_ERR,
2014 LOG_SLI,
2015 "0342 Ring %d Cannot find buffer for an"
2016 " unsolicited iocb. tag 0x%x\n",
2017 pring->ringno,
2018 irsp->unsli3.sli3Words[7]);
2019 }
2020 list_for_each_entry(iocbq, &saveq->list, list) {
76bb24ef 2021 irsp = &(iocbq->iocb);
76bb24ef
JS
2022 if (irsp->ulpBdeCount != 0) {
2023 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2024 irsp->un.ulpWord[3]);
9c2face6 2025 if (!iocbq->context2)
76bb24ef
JS
2026 lpfc_printf_log(phba,
2027 KERN_ERR,
2028 LOG_SLI,
2029 "0343 Ring %d Cannot find "
2030 "buffer for an unsolicited iocb"
2031 ". tag 0x%x\n", pring->ringno,
92d7f7b0 2032 irsp->un.ulpWord[3]);
76bb24ef
JS
2033 }
2034 if (irsp->ulpBdeCount == 2) {
2035 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
51ef4c26 2036 irsp->unsli3.sli3Words[7]);
9c2face6 2037 if (!iocbq->context3)
76bb24ef
JS
2038 lpfc_printf_log(phba,
2039 KERN_ERR,
2040 LOG_SLI,
2041 "0344 Ring %d Cannot find "
2042 "buffer for an unsolicited "
2043 "iocb. tag 0x%x\n",
2044 pring->ringno,
2045 irsp->unsli3.sli3Words[7]);
2046 }
2047 }
92d7f7b0 2048 }
9c2face6
JS
2049 if (irsp->ulpBdeCount != 0 &&
2050 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2051 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2052 int found = 0;
2053
2054 /* search continue save q for same XRI */
2055 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2056 if (iocbq->iocb.ulpContext == saveq->iocb.ulpContext) {
2057 list_add_tail(&saveq->list, &iocbq->list);
2058 found = 1;
2059 break;
2060 }
2061 }
2062 if (!found)
2063 list_add_tail(&saveq->clist,
2064 &pring->iocb_continue_saveq);
2065 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2066 list_del_init(&iocbq->clist);
2067 saveq = iocbq;
2068 irsp = &(saveq->iocb);
2069 } else
2070 return 0;
2071 }
2072 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2073 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2074 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
6a9c52cf
JS
2075 Rctl = FC_RCTL_ELS_REQ;
2076 Type = FC_TYPE_ELS;
9c2face6
JS
2077 } else {
2078 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2079 Rctl = w5p->hcsw.Rctl;
2080 Type = w5p->hcsw.Type;
2081
2082 /* Firmware Workaround */
2083 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2084 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2085 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
6a9c52cf
JS
2086 Rctl = FC_RCTL_ELS_REQ;
2087 Type = FC_TYPE_ELS;
9c2face6
JS
2088 w5p->hcsw.Rctl = Rctl;
2089 w5p->hcsw.Type = Type;
2090 }
2091 }
92d7f7b0 2092
3772a991 2093 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
92d7f7b0 2094 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2095 "0313 Ring %d handler: unexpected Rctl x%x "
92d7f7b0 2096 "Type x%x received\n",
e8b62011 2097 pring->ringno, Rctl, Type);
3772a991 2098
92d7f7b0 2099 return 1;
dea3101e 2100}
2101
e59058c4 2102/**
3621a710 2103 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
e59058c4
JS
2104 * @phba: Pointer to HBA context object.
2105 * @pring: Pointer to driver SLI ring object.
2106 * @prspiocb: Pointer to response iocb object.
2107 *
2108 * This function looks up the iocb_lookup table to get the command iocb
2109 * corresponding to the given response iocb using the iotag of the
2110 * response iocb. This function is called with the hbalock held.
2111 * This function returns the command iocb object if it finds the command
2112 * iocb else returns NULL.
2113 **/
dea3101e 2114static struct lpfc_iocbq *
2e0fef85
JS
2115lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2116 struct lpfc_sli_ring *pring,
2117 struct lpfc_iocbq *prspiocb)
dea3101e 2118{
dea3101e 2119 struct lpfc_iocbq *cmd_iocb = NULL;
2120 uint16_t iotag;
2121
604a3e30
JB
2122 iotag = prspiocb->iocb.ulpIoTag;
2123
2124 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2125 cmd_iocb = phba->sli.iocbq_lookup[iotag];
92d7f7b0 2126 list_del_init(&cmd_iocb->list);
604a3e30
JB
2127 pring->txcmplq_cnt--;
2128 return cmd_iocb;
dea3101e 2129 }
2130
dea3101e 2131 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2132 "0317 iotag x%x is out off "
604a3e30 2133 "range: max iotag x%x wd0 x%x\n",
e8b62011 2134 iotag, phba->sli.last_iotag,
604a3e30 2135 *(((uint32_t *) &prspiocb->iocb) + 7));
dea3101e 2136 return NULL;
2137}
2138
3772a991
JS
2139/**
2140 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2141 * @phba: Pointer to HBA context object.
2142 * @pring: Pointer to driver SLI ring object.
2143 * @iotag: IOCB tag.
2144 *
2145 * This function looks up the iocb_lookup table to get the command iocb
2146 * corresponding to the given iotag. This function is called with the
2147 * hbalock held.
2148 * This function returns the command iocb object if it finds the command
2149 * iocb else returns NULL.
2150 **/
2151static struct lpfc_iocbq *
2152lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2153 struct lpfc_sli_ring *pring, uint16_t iotag)
2154{
2155 struct lpfc_iocbq *cmd_iocb;
2156
2157 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2158 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2159 list_del_init(&cmd_iocb->list);
2160 pring->txcmplq_cnt--;
2161 return cmd_iocb;
2162 }
2163
2164 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2165 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2166 iotag, phba->sli.last_iotag);
2167 return NULL;
2168}
2169
e59058c4 2170/**
3621a710 2171 * lpfc_sli_process_sol_iocb - process solicited iocb completion
e59058c4
JS
2172 * @phba: Pointer to HBA context object.
2173 * @pring: Pointer to driver SLI ring object.
2174 * @saveq: Pointer to the response iocb to be processed.
2175 *
2176 * This function is called by the ring event handler for non-fcp
2177 * rings when there is a new response iocb in the response ring.
2178 * The caller is not required to hold any locks. This function
2179 * gets the command iocb associated with the response iocb and
2180 * calls the completion handler for the command iocb. If there
2181 * is no completion handler, the function will free the resources
2182 * associated with command iocb. If the response iocb is for
2183 * an already aborted command iocb, the status of the completion
2184 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2185 * This function always returns 1.
2186 **/
dea3101e 2187static int
2e0fef85 2188lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
dea3101e 2189 struct lpfc_iocbq *saveq)
2190{
2e0fef85 2191 struct lpfc_iocbq *cmdiocbp;
dea3101e 2192 int rc = 1;
2193 unsigned long iflag;
2194
2195 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2e0fef85 2196 spin_lock_irqsave(&phba->hbalock, iflag);
604a3e30 2197 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2e0fef85
JS
2198 spin_unlock_irqrestore(&phba->hbalock, iflag);
2199
dea3101e 2200 if (cmdiocbp) {
2201 if (cmdiocbp->iocb_cmpl) {
ea2151b4
JS
2202 /*
2203 * If an ELS command failed send an event to mgmt
2204 * application.
2205 */
2206 if (saveq->iocb.ulpStatus &&
2207 (pring->ringno == LPFC_ELS_RING) &&
2208 (cmdiocbp->iocb.ulpCommand ==
2209 CMD_ELS_REQUEST64_CR))
2210 lpfc_send_els_failure_event(phba,
2211 cmdiocbp, saveq);
2212
dea3101e 2213 /*
2214 * Post all ELS completions to the worker thread.
2215 * All other are passed to the completion callback.
2216 */
2217 if (pring->ringno == LPFC_ELS_RING) {
07951076
JS
2218 if (cmdiocbp->iocb_flag & LPFC_DRIVER_ABORTED) {
2219 cmdiocbp->iocb_flag &=
2220 ~LPFC_DRIVER_ABORTED;
2221 saveq->iocb.ulpStatus =
2222 IOSTAT_LOCAL_REJECT;
2223 saveq->iocb.un.ulpWord[4] =
2224 IOERR_SLI_ABORTED;
0ff10d46
JS
2225
2226 /* Firmware could still be in progress
2227 * of DMAing payload, so don't free data
2228 * buffer till after a hbeat.
2229 */
2230 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
07951076 2231 }
dea3101e 2232 }
2e0fef85 2233 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
604a3e30
JB
2234 } else
2235 lpfc_sli_release_iocbq(phba, cmdiocbp);
dea3101e 2236 } else {
2237 /*
2238 * Unknown initiating command based on the response iotag.
2239 * This could be the case on the ELS ring because of
2240 * lpfc_els_abort().
2241 */
2242 if (pring->ringno != LPFC_ELS_RING) {
2243 /*
2244 * Ring <ringno> handler: unexpected completion IoTag
2245 * <IoTag>
2246 */
a257bf90 2247 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011
JS
2248 "0322 Ring %d handler: "
2249 "unexpected completion IoTag x%x "
2250 "Data: x%x x%x x%x x%x\n",
2251 pring->ringno,
2252 saveq->iocb.ulpIoTag,
2253 saveq->iocb.ulpStatus,
2254 saveq->iocb.un.ulpWord[4],
2255 saveq->iocb.ulpCommand,
2256 saveq->iocb.ulpContext);
dea3101e 2257 }
2258 }
68876920 2259
dea3101e 2260 return rc;
2261}
2262
e59058c4 2263/**
3621a710 2264 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
e59058c4
JS
2265 * @phba: Pointer to HBA context object.
2266 * @pring: Pointer to driver SLI ring object.
2267 *
2268 * This function is called from the iocb ring event handlers when
2269 * put pointer is ahead of the get pointer for a ring. This function signal
2270 * an error attention condition to the worker thread and the worker
2271 * thread will transition the HBA to offline state.
2272 **/
2e0fef85
JS
2273static void
2274lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
875fbdfe 2275{
34b02dcd 2276 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe 2277 /*
025dfdaf 2278 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
875fbdfe
JSEC
2279 * rsp ring <portRspMax>
2280 */
2281 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2282 "0312 Ring %d handler: portRspPut %d "
025dfdaf 2283 "is bigger than rsp ring %d\n",
e8b62011 2284 pring->ringno, le32_to_cpu(pgp->rspPutInx),
875fbdfe
JSEC
2285 pring->numRiocb);
2286
2e0fef85 2287 phba->link_state = LPFC_HBA_ERROR;
875fbdfe
JSEC
2288
2289 /*
2290 * All error attention handlers are posted to
2291 * worker thread
2292 */
2293 phba->work_ha |= HA_ERATT;
2294 phba->work_hs = HS_FFER3;
92d7f7b0 2295
5e9d9b82 2296 lpfc_worker_wake_up(phba);
875fbdfe
JSEC
2297
2298 return;
2299}
2300
9399627f 2301/**
3621a710 2302 * lpfc_poll_eratt - Error attention polling timer timeout handler
9399627f
JS
2303 * @ptr: Pointer to address of HBA context object.
2304 *
2305 * This function is invoked by the Error Attention polling timer when the
2306 * timer times out. It will check the SLI Error Attention register for
2307 * possible attention events. If so, it will post an Error Attention event
2308 * and wake up worker thread to process it. Otherwise, it will set up the
2309 * Error Attention polling timer for the next poll.
2310 **/
2311void lpfc_poll_eratt(unsigned long ptr)
2312{
2313 struct lpfc_hba *phba;
2314 uint32_t eratt = 0;
2315
2316 phba = (struct lpfc_hba *)ptr;
2317
2318 /* Check chip HA register for error event */
2319 eratt = lpfc_sli_check_eratt(phba);
2320
2321 if (eratt)
2322 /* Tell the worker thread there is work to do */
2323 lpfc_worker_wake_up(phba);
2324 else
2325 /* Restart the timer for next eratt poll */
2326 mod_timer(&phba->eratt_poll, jiffies +
2327 HZ * LPFC_ERATT_POLL_INTERVAL);
2328 return;
2329}
2330
e59058c4 2331/**
3621a710 2332 * lpfc_sli_poll_fcp_ring - Handle FCP ring completion in polling mode
e59058c4
JS
2333 * @phba: Pointer to HBA context object.
2334 *
2335 * This function is called from lpfc_queuecommand, lpfc_poll_timeout,
2336 * lpfc_abort_handler and lpfc_slave_configure when FCP_RING_POLLING
2337 * is enabled.
2338 *
2339 * The caller does not hold any lock.
2340 * The function processes each response iocb in the response ring until it
2341 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2342 * LE bit set. The function will call the completion handler of the command iocb
2343 * if the response iocb indicates a completion for a command iocb or it is
2344 * an abort completion.
2345 **/
2e0fef85 2346void lpfc_sli_poll_fcp_ring(struct lpfc_hba *phba)
875fbdfe 2347{
2e0fef85
JS
2348 struct lpfc_sli *psli = &phba->sli;
2349 struct lpfc_sli_ring *pring = &psli->ring[LPFC_FCP_RING];
875fbdfe
JSEC
2350 IOCB_t *irsp = NULL;
2351 IOCB_t *entry = NULL;
2352 struct lpfc_iocbq *cmdiocbq = NULL;
2353 struct lpfc_iocbq rspiocbq;
34b02dcd 2354 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
875fbdfe
JSEC
2355 uint32_t status;
2356 uint32_t portRspPut, portRspMax;
2357 int type;
2358 uint32_t rsp_cmpl = 0;
875fbdfe 2359 uint32_t ha_copy;
2e0fef85 2360 unsigned long iflags;
875fbdfe
JSEC
2361
2362 pring->stats.iocb_event++;
2363
875fbdfe
JSEC
2364 /*
2365 * The next available response entry should never exceed the maximum
2366 * entries. If it does, treat it as an adapter hardware error.
2367 */
2368 portRspMax = pring->numRiocb;
2369 portRspPut = le32_to_cpu(pgp->rspPutInx);
2370 if (unlikely(portRspPut >= portRspMax)) {
2371 lpfc_sli_rsp_pointers_error(phba, pring);
2372 return;
2373 }
2374
2375 rmb();
2376 while (pring->rspidx != portRspPut) {
ed957684 2377 entry = lpfc_resp_iocb(phba, pring);
875fbdfe
JSEC
2378 if (++pring->rspidx >= portRspMax)
2379 pring->rspidx = 0;
2380
2381 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2382 (uint32_t *) &rspiocbq.iocb,
92d7f7b0 2383 phba->iocb_rsp_size);
875fbdfe
JSEC
2384 irsp = &rspiocbq.iocb;
2385 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2386 pring->stats.iocb_rsp++;
2387 rsp_cmpl++;
2388
2389 if (unlikely(irsp->ulpStatus)) {
2390 /* Rsp ring <ringno> error: IOCB */
2391 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2392 "0326 Rsp Ring %d error: IOCB Data: "
875fbdfe 2393 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2394 pring->ringno,
875fbdfe
JSEC
2395 irsp->un.ulpWord[0],
2396 irsp->un.ulpWord[1],
2397 irsp->un.ulpWord[2],
2398 irsp->un.ulpWord[3],
2399 irsp->un.ulpWord[4],
2400 irsp->un.ulpWord[5],
d7c255b2
JS
2401 *(uint32_t *)&irsp->un1,
2402 *((uint32_t *)&irsp->un1 + 1));
875fbdfe
JSEC
2403 }
2404
2405 switch (type) {
2406 case LPFC_ABORT_IOCB:
2407 case LPFC_SOL_IOCB:
2408 /*
2409 * Idle exchange closed via ABTS from port. No iocb
2410 * resources need to be recovered.
2411 */
2412 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2413 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
2414 "0314 IOCB cmd 0x%x "
2415 "processed. Skipping "
2416 "completion",
dca9479b 2417 irsp->ulpCommand);
875fbdfe
JSEC
2418 break;
2419 }
2420
2e0fef85 2421 spin_lock_irqsave(&phba->hbalock, iflags);
875fbdfe
JSEC
2422 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2423 &rspiocbq);
2e0fef85 2424 spin_unlock_irqrestore(&phba->hbalock, iflags);
875fbdfe
JSEC
2425 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
2426 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2427 &rspiocbq);
2428 }
2429 break;
2430 default:
2431 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2432 char adaptermsg[LPFC_MAX_ADPTMSG];
2433 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2434 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2435 MAX_MSG_DATA);
898eb71c
JP
2436 dev_warn(&((phba->pcidev)->dev),
2437 "lpfc%d: %s\n",
875fbdfe
JSEC
2438 phba->brd_no, adaptermsg);
2439 } else {
2440 /* Unknown IOCB command */
2441 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2442 "0321 Unknown IOCB command "
875fbdfe 2443 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2444 type, irsp->ulpCommand,
875fbdfe
JSEC
2445 irsp->ulpStatus,
2446 irsp->ulpIoTag,
2447 irsp->ulpContext);
2448 }
2449 break;
2450 }
2451
2452 /*
2453 * The response IOCB has been processed. Update the ring
2454 * pointer in SLIM. If the port response put pointer has not
2455 * been updated, sync the pgp->rspPutInx and fetch the new port
2456 * response put pointer.
2457 */
ed957684 2458 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
875fbdfe
JSEC
2459
2460 if (pring->rspidx == portRspPut)
2461 portRspPut = le32_to_cpu(pgp->rspPutInx);
2462 }
2463
2464 ha_copy = readl(phba->HAregaddr);
2465 ha_copy >>= (LPFC_FCP_RING * 4);
2466
2467 if ((rsp_cmpl > 0) && (ha_copy & HA_R0RE_REQ)) {
2e0fef85 2468 spin_lock_irqsave(&phba->hbalock, iflags);
875fbdfe
JSEC
2469 pring->stats.iocb_rsp_full++;
2470 status = ((CA_R0ATT | CA_R0RE_RSP) << (LPFC_FCP_RING * 4));
2471 writel(status, phba->CAregaddr);
2472 readl(phba->CAregaddr);
2e0fef85 2473 spin_unlock_irqrestore(&phba->hbalock, iflags);
875fbdfe
JSEC
2474 }
2475 if ((ha_copy & HA_R0CE_RSP) &&
2476 (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2e0fef85 2477 spin_lock_irqsave(&phba->hbalock, iflags);
875fbdfe
JSEC
2478 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2479 pring->stats.iocb_cmd_empty++;
2480
2481 /* Force update of the local copy of cmdGetInx */
2482 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2483 lpfc_sli_resume_iocb(phba, pring);
2484
2485 if ((pring->lpfc_sli_cmd_available))
2486 (pring->lpfc_sli_cmd_available) (phba, pring);
2487
2e0fef85 2488 spin_unlock_irqrestore(&phba->hbalock, iflags);
875fbdfe
JSEC
2489 }
2490
2491 return;
2492}
2493
e59058c4 2494/**
3621a710 2495 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
e59058c4
JS
2496 * @phba: Pointer to HBA context object.
2497 * @pring: Pointer to driver SLI ring object.
2498 * @mask: Host attention register mask for this ring.
2499 *
2500 * This function is called from the interrupt context when there is a ring
2501 * event for the fcp ring. The caller does not hold any lock.
2502 * The function processes each response iocb in the response ring until it
2503 * finds an iocb with LE bit set and chains all the iocbs upto the iocb with
2504 * LE bit set. The function will call the completion handler of the command iocb
2505 * if the response iocb indicates a completion for a command iocb or it is
2506 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2507 * function if this is an unsolicited iocb.
dea3101e 2508 * This routine presumes LPFC_FCP_RING handling and doesn't bother
e59058c4
JS
2509 * to check it explicitly. This function always returns 1.
2510 **/
dea3101e 2511static int
2e0fef85
JS
2512lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2513 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2514{
34b02dcd 2515 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
dea3101e 2516 IOCB_t *irsp = NULL;
87f6eaff 2517 IOCB_t *entry = NULL;
dea3101e 2518 struct lpfc_iocbq *cmdiocbq = NULL;
2519 struct lpfc_iocbq rspiocbq;
dea3101e 2520 uint32_t status;
2521 uint32_t portRspPut, portRspMax;
2522 int rc = 1;
2523 lpfc_iocb_type type;
2524 unsigned long iflag;
2525 uint32_t rsp_cmpl = 0;
dea3101e 2526
2e0fef85 2527 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2528 pring->stats.iocb_event++;
2529
dea3101e 2530 /*
2531 * The next available response entry should never exceed the maximum
2532 * entries. If it does, treat it as an adapter hardware error.
2533 */
2534 portRspMax = pring->numRiocb;
2535 portRspPut = le32_to_cpu(pgp->rspPutInx);
2536 if (unlikely(portRspPut >= portRspMax)) {
875fbdfe 2537 lpfc_sli_rsp_pointers_error(phba, pring);
2e0fef85 2538 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2539 return 1;
2540 }
2541
2542 rmb();
2543 while (pring->rspidx != portRspPut) {
87f6eaff
JSEC
2544 /*
2545 * Fetch an entry off the ring and copy it into a local data
2546 * structure. The copy involves a byte-swap since the
2547 * network byte order and pci byte orders are different.
2548 */
ed957684 2549 entry = lpfc_resp_iocb(phba, pring);
858c9f6c 2550 phba->last_completion_time = jiffies;
875fbdfe
JSEC
2551
2552 if (++pring->rspidx >= portRspMax)
2553 pring->rspidx = 0;
2554
87f6eaff
JSEC
2555 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2556 (uint32_t *) &rspiocbq.iocb,
ed957684 2557 phba->iocb_rsp_size);
a4bc3379 2558 INIT_LIST_HEAD(&(rspiocbq.list));
87f6eaff
JSEC
2559 irsp = &rspiocbq.iocb;
2560
dea3101e 2561 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2562 pring->stats.iocb_rsp++;
2563 rsp_cmpl++;
2564
2565 if (unlikely(irsp->ulpStatus)) {
92d7f7b0
JS
2566 /*
2567 * If resource errors reported from HBA, reduce
2568 * queuedepths of the SCSI device.
2569 */
2570 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2571 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2572 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 2573 phba->lpfc_rampdown_queue_depth(phba);
92d7f7b0
JS
2574 spin_lock_irqsave(&phba->hbalock, iflag);
2575 }
2576
dea3101e 2577 /* Rsp ring <ringno> error: IOCB */
2578 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
e8b62011 2579 "0336 Rsp Ring %d error: IOCB Data: "
92d7f7b0 2580 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
e8b62011 2581 pring->ringno,
92d7f7b0
JS
2582 irsp->un.ulpWord[0],
2583 irsp->un.ulpWord[1],
2584 irsp->un.ulpWord[2],
2585 irsp->un.ulpWord[3],
2586 irsp->un.ulpWord[4],
2587 irsp->un.ulpWord[5],
d7c255b2
JS
2588 *(uint32_t *)&irsp->un1,
2589 *((uint32_t *)&irsp->un1 + 1));
dea3101e 2590 }
2591
2592 switch (type) {
2593 case LPFC_ABORT_IOCB:
2594 case LPFC_SOL_IOCB:
2595 /*
2596 * Idle exchange closed via ABTS from port. No iocb
2597 * resources need to be recovered.
2598 */
2599 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
dca9479b 2600 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 2601 "0333 IOCB cmd 0x%x"
dca9479b 2602 " processed. Skipping"
92d7f7b0 2603 " completion\n",
dca9479b 2604 irsp->ulpCommand);
dea3101e 2605 break;
2606 }
2607
604a3e30
JB
2608 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
2609 &rspiocbq);
dea3101e 2610 if ((cmdiocbq) && (cmdiocbq->iocb_cmpl)) {
b808608b
JW
2611 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2612 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2613 &rspiocbq);
2614 } else {
2e0fef85
JS
2615 spin_unlock_irqrestore(&phba->hbalock,
2616 iflag);
b808608b
JW
2617 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
2618 &rspiocbq);
2e0fef85 2619 spin_lock_irqsave(&phba->hbalock,
b808608b
JW
2620 iflag);
2621 }
dea3101e 2622 }
2623 break;
a4bc3379 2624 case LPFC_UNSOL_IOCB:
2e0fef85 2625 spin_unlock_irqrestore(&phba->hbalock, iflag);
a4bc3379 2626 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
2e0fef85 2627 spin_lock_irqsave(&phba->hbalock, iflag);
a4bc3379 2628 break;
dea3101e 2629 default:
2630 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2631 char adaptermsg[LPFC_MAX_ADPTMSG];
2632 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2633 memcpy(&adaptermsg[0], (uint8_t *) irsp,
2634 MAX_MSG_DATA);
898eb71c
JP
2635 dev_warn(&((phba->pcidev)->dev),
2636 "lpfc%d: %s\n",
dea3101e 2637 phba->brd_no, adaptermsg);
2638 } else {
2639 /* Unknown IOCB command */
2640 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2641 "0334 Unknown IOCB command "
92d7f7b0 2642 "Data: x%x, x%x x%x x%x x%x\n",
e8b62011 2643 type, irsp->ulpCommand,
92d7f7b0
JS
2644 irsp->ulpStatus,
2645 irsp->ulpIoTag,
2646 irsp->ulpContext);
dea3101e 2647 }
2648 break;
2649 }
2650
2651 /*
2652 * The response IOCB has been processed. Update the ring
2653 * pointer in SLIM. If the port response put pointer has not
2654 * been updated, sync the pgp->rspPutInx and fetch the new port
2655 * response put pointer.
2656 */
ed957684 2657 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2658
2659 if (pring->rspidx == portRspPut)
2660 portRspPut = le32_to_cpu(pgp->rspPutInx);
2661 }
2662
2663 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
2664 pring->stats.iocb_rsp_full++;
2665 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2666 writel(status, phba->CAregaddr);
2667 readl(phba->CAregaddr);
2668 }
2669 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2670 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2671 pring->stats.iocb_cmd_empty++;
2672
2673 /* Force update of the local copy of cmdGetInx */
2674 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2675 lpfc_sli_resume_iocb(phba, pring);
2676
2677 if ((pring->lpfc_sli_cmd_available))
2678 (pring->lpfc_sli_cmd_available) (phba, pring);
2679
2680 }
2681
2e0fef85 2682 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2683 return rc;
2684}
2685
e59058c4 2686/**
3772a991
JS
2687 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
2688 * @phba: Pointer to HBA context object.
2689 * @pring: Pointer to driver SLI ring object.
2690 * @rspiocbp: Pointer to driver response IOCB object.
2691 *
2692 * This function is called from the worker thread when there is a slow-path
2693 * response IOCB to process. This function chains all the response iocbs until
2694 * seeing the iocb with the LE bit set. The function will call
2695 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
2696 * completion of a command iocb. The function will call the
2697 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
2698 * The function frees the resources or calls the completion handler if this
2699 * iocb is an abort completion. The function returns NULL when the response
2700 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
2701 * this function shall chain the iocb on to the iocb_continueq and return the
2702 * response iocb passed in.
2703 **/
2704static struct lpfc_iocbq *
2705lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2706 struct lpfc_iocbq *rspiocbp)
2707{
2708 struct lpfc_iocbq *saveq;
2709 struct lpfc_iocbq *cmdiocbp;
2710 struct lpfc_iocbq *next_iocb;
2711 IOCB_t *irsp = NULL;
2712 uint32_t free_saveq;
2713 uint8_t iocb_cmd_type;
2714 lpfc_iocb_type type;
2715 unsigned long iflag;
2716 int rc;
2717
2718 spin_lock_irqsave(&phba->hbalock, iflag);
2719 /* First add the response iocb to the countinueq list */
2720 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
2721 pring->iocb_continueq_cnt++;
2722
2723 /* Now, determine whetehr the list is completed for processing */
2724 irsp = &rspiocbp->iocb;
2725 if (irsp->ulpLe) {
2726 /*
2727 * By default, the driver expects to free all resources
2728 * associated with this iocb completion.
2729 */
2730 free_saveq = 1;
2731 saveq = list_get_first(&pring->iocb_continueq,
2732 struct lpfc_iocbq, list);
2733 irsp = &(saveq->iocb);
2734 list_del_init(&pring->iocb_continueq);
2735 pring->iocb_continueq_cnt = 0;
2736
2737 pring->stats.iocb_rsp++;
2738
2739 /*
2740 * If resource errors reported from HBA, reduce
2741 * queuedepths of the SCSI device.
2742 */
2743 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2744 (irsp->un.ulpWord[4] == IOERR_NO_RESOURCES)) {
2745 spin_unlock_irqrestore(&phba->hbalock, iflag);
2746 phba->lpfc_rampdown_queue_depth(phba);
2747 spin_lock_irqsave(&phba->hbalock, iflag);
2748 }
2749
2750 if (irsp->ulpStatus) {
2751 /* Rsp ring <ringno> error: IOCB */
2752 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2753 "0328 Rsp Ring %d error: "
2754 "IOCB Data: "
2755 "x%x x%x x%x x%x "
2756 "x%x x%x x%x x%x "
2757 "x%x x%x x%x x%x "
2758 "x%x x%x x%x x%x\n",
2759 pring->ringno,
2760 irsp->un.ulpWord[0],
2761 irsp->un.ulpWord[1],
2762 irsp->un.ulpWord[2],
2763 irsp->un.ulpWord[3],
2764 irsp->un.ulpWord[4],
2765 irsp->un.ulpWord[5],
2766 *(((uint32_t *) irsp) + 6),
2767 *(((uint32_t *) irsp) + 7),
2768 *(((uint32_t *) irsp) + 8),
2769 *(((uint32_t *) irsp) + 9),
2770 *(((uint32_t *) irsp) + 10),
2771 *(((uint32_t *) irsp) + 11),
2772 *(((uint32_t *) irsp) + 12),
2773 *(((uint32_t *) irsp) + 13),
2774 *(((uint32_t *) irsp) + 14),
2775 *(((uint32_t *) irsp) + 15));
2776 }
2777
2778 /*
2779 * Fetch the IOCB command type and call the correct completion
2780 * routine. Solicited and Unsolicited IOCBs on the ELS ring
2781 * get freed back to the lpfc_iocb_list by the discovery
2782 * kernel thread.
2783 */
2784 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
2785 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
2786 switch (type) {
2787 case LPFC_SOL_IOCB:
2788 spin_unlock_irqrestore(&phba->hbalock, iflag);
2789 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
2790 spin_lock_irqsave(&phba->hbalock, iflag);
2791 break;
2792
2793 case LPFC_UNSOL_IOCB:
2794 spin_unlock_irqrestore(&phba->hbalock, iflag);
2795 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
2796 spin_lock_irqsave(&phba->hbalock, iflag);
2797 if (!rc)
2798 free_saveq = 0;
2799 break;
2800
2801 case LPFC_ABORT_IOCB:
2802 cmdiocbp = NULL;
2803 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
2804 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
2805 saveq);
2806 if (cmdiocbp) {
2807 /* Call the specified completion routine */
2808 if (cmdiocbp->iocb_cmpl) {
2809 spin_unlock_irqrestore(&phba->hbalock,
2810 iflag);
2811 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
2812 saveq);
2813 spin_lock_irqsave(&phba->hbalock,
2814 iflag);
2815 } else
2816 __lpfc_sli_release_iocbq(phba,
2817 cmdiocbp);
2818 }
2819 break;
2820
2821 case LPFC_UNKNOWN_IOCB:
2822 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
2823 char adaptermsg[LPFC_MAX_ADPTMSG];
2824 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
2825 memcpy(&adaptermsg[0], (uint8_t *)irsp,
2826 MAX_MSG_DATA);
2827 dev_warn(&((phba->pcidev)->dev),
2828 "lpfc%d: %s\n",
2829 phba->brd_no, adaptermsg);
2830 } else {
2831 /* Unknown IOCB command */
2832 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2833 "0335 Unknown IOCB "
2834 "command Data: x%x "
2835 "x%x x%x x%x\n",
2836 irsp->ulpCommand,
2837 irsp->ulpStatus,
2838 irsp->ulpIoTag,
2839 irsp->ulpContext);
2840 }
2841 break;
2842 }
2843
2844 if (free_saveq) {
2845 list_for_each_entry_safe(rspiocbp, next_iocb,
2846 &saveq->list, list) {
2847 list_del(&rspiocbp->list);
2848 __lpfc_sli_release_iocbq(phba, rspiocbp);
2849 }
2850 __lpfc_sli_release_iocbq(phba, saveq);
2851 }
2852 rspiocbp = NULL;
2853 }
2854 spin_unlock_irqrestore(&phba->hbalock, iflag);
2855 return rspiocbp;
2856}
2857
2858/**
2859 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
e59058c4
JS
2860 * @phba: Pointer to HBA context object.
2861 * @pring: Pointer to driver SLI ring object.
2862 * @mask: Host attention register mask for this ring.
2863 *
3772a991
JS
2864 * This routine wraps the actual slow_ring event process routine from the
2865 * API jump table function pointer from the lpfc_hba struct.
e59058c4 2866 **/
3772a991 2867void
2e0fef85
JS
2868lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
2869 struct lpfc_sli_ring *pring, uint32_t mask)
3772a991
JS
2870{
2871 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
2872}
2873
2874/**
2875 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
2876 * @phba: Pointer to HBA context object.
2877 * @pring: Pointer to driver SLI ring object.
2878 * @mask: Host attention register mask for this ring.
2879 *
2880 * This function is called from the worker thread when there is a ring event
2881 * for non-fcp rings. The caller does not hold any lock. The function will
2882 * remove each response iocb in the response ring and calls the handle
2883 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
2884 **/
2885static void
2886lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
2887 struct lpfc_sli_ring *pring, uint32_t mask)
dea3101e 2888{
34b02dcd 2889 struct lpfc_pgp *pgp;
dea3101e 2890 IOCB_t *entry;
2891 IOCB_t *irsp = NULL;
2892 struct lpfc_iocbq *rspiocbp = NULL;
dea3101e 2893 uint32_t portRspPut, portRspMax;
dea3101e 2894 unsigned long iflag;
3772a991 2895 uint32_t status;
dea3101e 2896
34b02dcd 2897 pgp = &phba->port_gp[pring->ringno];
2e0fef85 2898 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2899 pring->stats.iocb_event++;
2900
dea3101e 2901 /*
2902 * The next available response entry should never exceed the maximum
2903 * entries. If it does, treat it as an adapter hardware error.
2904 */
2905 portRspMax = pring->numRiocb;
2906 portRspPut = le32_to_cpu(pgp->rspPutInx);
2907 if (portRspPut >= portRspMax) {
2908 /*
025dfdaf 2909 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
dea3101e 2910 * rsp ring <portRspMax>
2911 */
ed957684 2912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011 2913 "0303 Ring %d handler: portRspPut %d "
025dfdaf 2914 "is bigger than rsp ring %d\n",
e8b62011 2915 pring->ringno, portRspPut, portRspMax);
dea3101e 2916
2e0fef85
JS
2917 phba->link_state = LPFC_HBA_ERROR;
2918 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 2919
2920 phba->work_hs = HS_FFER3;
2921 lpfc_handle_eratt(phba);
2922
3772a991 2923 return;
dea3101e 2924 }
2925
2926 rmb();
dea3101e 2927 while (pring->rspidx != portRspPut) {
2928 /*
2929 * Build a completion list and call the appropriate handler.
2930 * The process is to get the next available response iocb, get
2931 * a free iocb from the list, copy the response data into the
2932 * free iocb, insert to the continuation list, and update the
2933 * next response index to slim. This process makes response
2934 * iocb's in the ring available to DMA as fast as possible but
2935 * pays a penalty for a copy operation. Since the iocb is
2936 * only 32 bytes, this penalty is considered small relative to
2937 * the PCI reads for register values and a slim write. When
2938 * the ulpLe field is set, the entire Command has been
2939 * received.
2940 */
ed957684
JS
2941 entry = lpfc_resp_iocb(phba, pring);
2942
858c9f6c 2943 phba->last_completion_time = jiffies;
2e0fef85 2944 rspiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 2945 if (rspiocbp == NULL) {
2946 printk(KERN_ERR "%s: out of buffers! Failing "
cadbd4a5 2947 "completion.\n", __func__);
dea3101e 2948 break;
2949 }
2950
ed957684
JS
2951 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
2952 phba->iocb_rsp_size);
dea3101e 2953 irsp = &rspiocbp->iocb;
2954
2955 if (++pring->rspidx >= portRspMax)
2956 pring->rspidx = 0;
2957
a58cbd52
JS
2958 if (pring->ringno == LPFC_ELS_RING) {
2959 lpfc_debugfs_slow_ring_trc(phba,
2960 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
2961 *(((uint32_t *) irsp) + 4),
2962 *(((uint32_t *) irsp) + 6),
2963 *(((uint32_t *) irsp) + 7));
2964 }
2965
ed957684 2966 writel(pring->rspidx, &phba->host_gp[pring->ringno].rspGetInx);
dea3101e 2967
3772a991
JS
2968 spin_unlock_irqrestore(&phba->hbalock, iflag);
2969 /* Handle the response IOCB */
2970 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
2971 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 2972
2973 /*
2974 * If the port response put pointer has not been updated, sync
2975 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
2976 * response put pointer.
2977 */
2978 if (pring->rspidx == portRspPut) {
2979 portRspPut = le32_to_cpu(pgp->rspPutInx);
2980 }
2981 } /* while (pring->rspidx != portRspPut) */
2982
92d7f7b0 2983 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
dea3101e 2984 /* At least one response entry has been freed */
2985 pring->stats.iocb_rsp_full++;
2986 /* SET RxRE_RSP in Chip Att register */
2987 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
2988 writel(status, phba->CAregaddr);
2989 readl(phba->CAregaddr); /* flush */
2990 }
2991 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
2992 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
2993 pring->stats.iocb_cmd_empty++;
2994
2995 /* Force update of the local copy of cmdGetInx */
2996 pring->local_getidx = le32_to_cpu(pgp->cmdGetInx);
2997 lpfc_sli_resume_iocb(phba, pring);
2998
2999 if ((pring->lpfc_sli_cmd_available))
3000 (pring->lpfc_sli_cmd_available) (phba, pring);
3001
3002 }
3003
2e0fef85 3004 spin_unlock_irqrestore(&phba->hbalock, iflag);
3772a991 3005 return;
dea3101e 3006}
3007
4f774513
JS
3008/**
3009 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3010 * @phba: Pointer to HBA context object.
3011 * @pring: Pointer to driver SLI ring object.
3012 * @mask: Host attention register mask for this ring.
3013 *
3014 * This function is called from the worker thread when there is a pending
3015 * ELS response iocb on the driver internal slow-path response iocb worker
3016 * queue. The caller does not hold any lock. The function will remove each
3017 * response iocb from the response worker queue and calls the handle
3018 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3019 **/
3020static void
3021lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3022 struct lpfc_sli_ring *pring, uint32_t mask)
3023{
3024 struct lpfc_iocbq *irspiocbq;
4d9ab994
JS
3025 struct hbq_dmabuf *dmabuf;
3026 struct lpfc_cq_event *cq_event;
4f774513
JS
3027 unsigned long iflag;
3028
3029 while (!list_empty(&phba->sli4_hba.sp_rspiocb_work_queue)) {
3030 /* Get the response iocb from the head of work queue */
3031 spin_lock_irqsave(&phba->hbalock, iflag);
3032 list_remove_head(&phba->sli4_hba.sp_rspiocb_work_queue,
4d9ab994 3033 cq_event, struct lpfc_cq_event, list);
4f774513 3034 spin_unlock_irqrestore(&phba->hbalock, iflag);
4d9ab994
JS
3035
3036 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3037 case CQE_CODE_COMPL_WQE:
3038 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3039 cq_event);
3040 lpfc_sli_sp_handle_rspiocb(phba, pring, irspiocbq);
3041 break;
3042 case CQE_CODE_RECEIVE:
3043 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3044 cq_event);
3045 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3046 break;
3047 default:
3048 break;
3049 }
4f774513
JS
3050 }
3051}
3052
e59058c4 3053/**
3621a710 3054 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
e59058c4
JS
3055 * @phba: Pointer to HBA context object.
3056 * @pring: Pointer to driver SLI ring object.
3057 *
3058 * This function aborts all iocbs in the given ring and frees all the iocb
3059 * objects in txq. This function issues an abort iocb for all the iocb commands
3060 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3061 * the return of this function. The caller is not required to hold any locks.
3062 **/
2e0fef85 3063void
dea3101e 3064lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3065{
2534ba75 3066 LIST_HEAD(completions);
dea3101e 3067 struct lpfc_iocbq *iocb, *next_iocb;
dea3101e 3068
92d7f7b0
JS
3069 if (pring->ringno == LPFC_ELS_RING) {
3070 lpfc_fabric_abort_hba(phba);
3071 }
3072
dea3101e 3073 /* Error everything on txq and txcmplq
3074 * First do the txq.
3075 */
2e0fef85 3076 spin_lock_irq(&phba->hbalock);
2534ba75 3077 list_splice_init(&pring->txq, &completions);
dea3101e 3078 pring->txq_cnt = 0;
dea3101e 3079
3080 /* Next issue ABTS for everything on the txcmplq */
2534ba75
JS
3081 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3082 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
dea3101e 3083
2e0fef85 3084 spin_unlock_irq(&phba->hbalock);
dea3101e 3085
a257bf90
JS
3086 /* Cancel all the IOCBs from the completions list */
3087 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3088 IOERR_SLI_ABORTED);
dea3101e 3089}
3090
a8e497d5 3091/**
3621a710 3092 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
a8e497d5
JS
3093 * @phba: Pointer to HBA context object.
3094 *
3095 * This function flushes all iocbs in the fcp ring and frees all the iocb
3096 * objects in txq and txcmplq. This function will not issue abort iocbs
3097 * for all the iocb commands in txcmplq, they will just be returned with
3098 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3099 * slot has been permanently disabled.
3100 **/
3101void
3102lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3103{
3104 LIST_HEAD(txq);
3105 LIST_HEAD(txcmplq);
a8e497d5
JS
3106 struct lpfc_sli *psli = &phba->sli;
3107 struct lpfc_sli_ring *pring;
3108
3109 /* Currently, only one fcp ring */
3110 pring = &psli->ring[psli->fcp_ring];
3111
3112 spin_lock_irq(&phba->hbalock);
3113 /* Retrieve everything on txq */
3114 list_splice_init(&pring->txq, &txq);
3115 pring->txq_cnt = 0;
3116
3117 /* Retrieve everything on the txcmplq */
3118 list_splice_init(&pring->txcmplq, &txcmplq);
3119 pring->txcmplq_cnt = 0;
3120 spin_unlock_irq(&phba->hbalock);
3121
3122 /* Flush the txq */
a257bf90
JS
3123 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3124 IOERR_SLI_DOWN);
a8e497d5
JS
3125
3126 /* Flush the txcmpq */
a257bf90
JS
3127 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3128 IOERR_SLI_DOWN);
a8e497d5
JS
3129}
3130
e59058c4 3131/**
3772a991 3132 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
e59058c4
JS
3133 * @phba: Pointer to HBA context object.
3134 * @mask: Bit mask to be checked.
3135 *
3136 * This function reads the host status register and compares
3137 * with the provided bit mask to check if HBA completed
3138 * the restart. This function will wait in a loop for the
3139 * HBA to complete restart. If the HBA does not restart within
3140 * 15 iterations, the function will reset the HBA again. The
3141 * function returns 1 when HBA fail to restart otherwise returns
3142 * zero.
3143 **/
3772a991
JS
3144static int
3145lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
dea3101e 3146{
41415862
JW
3147 uint32_t status;
3148 int i = 0;
3149 int retval = 0;
dea3101e 3150
41415862
JW
3151 /* Read the HBA Host Status Register */
3152 status = readl(phba->HSregaddr);
dea3101e 3153
41415862
JW
3154 /*
3155 * Check status register every 100ms for 5 retries, then every
3156 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3157 * every 2.5 sec for 4.
3158 * Break our of the loop if errors occurred during init.
3159 */
3160 while (((status & mask) != mask) &&
3161 !(status & HS_FFERM) &&
3162 i++ < 20) {
dea3101e 3163
41415862
JW
3164 if (i <= 5)
3165 msleep(10);
3166 else if (i <= 10)
3167 msleep(500);
3168 else
3169 msleep(2500);
dea3101e 3170
41415862 3171 if (i == 15) {
2e0fef85 3172 /* Do post */
92d7f7b0 3173 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862
JW
3174 lpfc_sli_brdrestart(phba);
3175 }
3176 /* Read the HBA Host Status Register */
3177 status = readl(phba->HSregaddr);
3178 }
dea3101e 3179
41415862
JW
3180 /* Check to see if any errors occurred during init */
3181 if ((status & HS_FFERM) || (i >= 20)) {
2e0fef85 3182 phba->link_state = LPFC_HBA_ERROR;
41415862 3183 retval = 1;
dea3101e 3184 }
dea3101e 3185
41415862
JW
3186 return retval;
3187}
dea3101e 3188
da0436e9
JS
3189/**
3190 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3191 * @phba: Pointer to HBA context object.
3192 * @mask: Bit mask to be checked.
3193 *
3194 * This function checks the host status register to check if HBA is
3195 * ready. This function will wait in a loop for the HBA to be ready
3196 * If the HBA is not ready , the function will will reset the HBA PCI
3197 * function again. The function returns 1 when HBA fail to be ready
3198 * otherwise returns zero.
3199 **/
3200static int
3201lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3202{
3203 uint32_t status;
3204 int retval = 0;
3205
3206 /* Read the HBA Host Status Register */
3207 status = lpfc_sli4_post_status_check(phba);
3208
3209 if (status) {
3210 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3211 lpfc_sli_brdrestart(phba);
3212 status = lpfc_sli4_post_status_check(phba);
3213 }
3214
3215 /* Check to see if any errors occurred during init */
3216 if (status) {
3217 phba->link_state = LPFC_HBA_ERROR;
3218 retval = 1;
3219 } else
3220 phba->sli4_hba.intr_enable = 0;
3221
3222 return retval;
3223}
3224
3225/**
3226 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3227 * @phba: Pointer to HBA context object.
3228 * @mask: Bit mask to be checked.
3229 *
3230 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3231 * from the API jump table function pointer from the lpfc_hba struct.
3232 **/
3233int
3234lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3235{
3236 return phba->lpfc_sli_brdready(phba, mask);
3237}
3238
9290831f
JS
3239#define BARRIER_TEST_PATTERN (0xdeadbeef)
3240
e59058c4 3241/**
3621a710 3242 * lpfc_reset_barrier - Make HBA ready for HBA reset
e59058c4
JS
3243 * @phba: Pointer to HBA context object.
3244 *
3245 * This function is called before resetting an HBA. This
3246 * function requests HBA to quiesce DMAs before a reset.
3247 **/
2e0fef85 3248void lpfc_reset_barrier(struct lpfc_hba *phba)
9290831f 3249{
65a29c16
JS
3250 uint32_t __iomem *resp_buf;
3251 uint32_t __iomem *mbox_buf;
9290831f
JS
3252 volatile uint32_t mbox;
3253 uint32_t hc_copy;
3254 int i;
3255 uint8_t hdrtype;
3256
3257 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3258 if (hdrtype != 0x80 ||
3259 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3260 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3261 return;
3262
3263 /*
3264 * Tell the other part of the chip to suspend temporarily all
3265 * its DMA activity.
3266 */
65a29c16 3267 resp_buf = phba->MBslimaddr;
9290831f
JS
3268
3269 /* Disable the error attention */
3270 hc_copy = readl(phba->HCregaddr);
3271 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3272 readl(phba->HCregaddr); /* flush */
2e0fef85 3273 phba->link_flag |= LS_IGNORE_ERATT;
9290831f
JS
3274
3275 if (readl(phba->HAregaddr) & HA_ERATT) {
3276 /* Clear Chip error bit */
3277 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3278 phba->pport->stopped = 1;
9290831f
JS
3279 }
3280
3281 mbox = 0;
3282 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3283 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3284
3285 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
65a29c16 3286 mbox_buf = phba->MBslimaddr;
9290831f
JS
3287 writel(mbox, mbox_buf);
3288
3289 for (i = 0;
3290 readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN) && i < 50; i++)
3291 mdelay(1);
3292
3293 if (readl(resp_buf + 1) != ~(BARRIER_TEST_PATTERN)) {
f4b4c68f 3294 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
2e0fef85 3295 phba->pport->stopped)
9290831f
JS
3296 goto restore_hc;
3297 else
3298 goto clear_errat;
3299 }
3300
3301 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3302 for (i = 0; readl(resp_buf) != mbox && i < 500; i++)
3303 mdelay(1);
3304
3305clear_errat:
3306
3307 while (!(readl(phba->HAregaddr) & HA_ERATT) && ++i < 500)
3308 mdelay(1);
3309
3310 if (readl(phba->HAregaddr) & HA_ERATT) {
3311 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3312 phba->pport->stopped = 1;
9290831f
JS
3313 }
3314
3315restore_hc:
2e0fef85 3316 phba->link_flag &= ~LS_IGNORE_ERATT;
9290831f
JS
3317 writel(hc_copy, phba->HCregaddr);
3318 readl(phba->HCregaddr); /* flush */
3319}
3320
e59058c4 3321/**
3621a710 3322 * lpfc_sli_brdkill - Issue a kill_board mailbox command
e59058c4
JS
3323 * @phba: Pointer to HBA context object.
3324 *
3325 * This function issues a kill_board mailbox command and waits for
3326 * the error attention interrupt. This function is called for stopping
3327 * the firmware processing. The caller is not required to hold any
3328 * locks. This function calls lpfc_hba_down_post function to free
3329 * any pending commands after the kill. The function will return 1 when it
3330 * fails to kill the board else will return 0.
3331 **/
41415862 3332int
2e0fef85 3333lpfc_sli_brdkill(struct lpfc_hba *phba)
41415862
JW
3334{
3335 struct lpfc_sli *psli;
3336 LPFC_MBOXQ_t *pmb;
3337 uint32_t status;
3338 uint32_t ha_copy;
3339 int retval;
3340 int i = 0;
dea3101e 3341
41415862 3342 psli = &phba->sli;
dea3101e 3343
41415862 3344 /* Kill HBA */
ed957684 3345 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011
JS
3346 "0329 Kill HBA Data: x%x x%x\n",
3347 phba->pport->port_state, psli->sli_flag);
41415862 3348
98c9ea5c
JS
3349 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3350 if (!pmb)
41415862 3351 return 1;
41415862
JW
3352
3353 /* Disable the error attention */
2e0fef85 3354 spin_lock_irq(&phba->hbalock);
41415862
JW
3355 status = readl(phba->HCregaddr);
3356 status &= ~HC_ERINT_ENA;
3357 writel(status, phba->HCregaddr);
3358 readl(phba->HCregaddr); /* flush */
2e0fef85
JS
3359 phba->link_flag |= LS_IGNORE_ERATT;
3360 spin_unlock_irq(&phba->hbalock);
41415862
JW
3361
3362 lpfc_kill_board(phba, pmb);
3363 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3364 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3365
3366 if (retval != MBX_SUCCESS) {
3367 if (retval != MBX_BUSY)
3368 mempool_free(pmb, phba->mbox_mem_pool);
2e0fef85
JS
3369 spin_lock_irq(&phba->hbalock);
3370 phba->link_flag &= ~LS_IGNORE_ERATT;
3371 spin_unlock_irq(&phba->hbalock);
41415862
JW
3372 return 1;
3373 }
3374
f4b4c68f
JS
3375 spin_lock_irq(&phba->hbalock);
3376 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3377 spin_unlock_irq(&phba->hbalock);
9290831f 3378
41415862
JW
3379 mempool_free(pmb, phba->mbox_mem_pool);
3380
3381 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3382 * attention every 100ms for 3 seconds. If we don't get ERATT after
3383 * 3 seconds we still set HBA_ERROR state because the status of the
3384 * board is now undefined.
3385 */
3386 ha_copy = readl(phba->HAregaddr);
3387
3388 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3389 mdelay(100);
3390 ha_copy = readl(phba->HAregaddr);
3391 }
3392
3393 del_timer_sync(&psli->mbox_tmo);
9290831f
JS
3394 if (ha_copy & HA_ERATT) {
3395 writel(HA_ERATT, phba->HAregaddr);
2e0fef85 3396 phba->pport->stopped = 1;
9290831f 3397 }
2e0fef85 3398 spin_lock_irq(&phba->hbalock);
41415862 3399 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
04c68496 3400 psli->mbox_active = NULL;
2e0fef85
JS
3401 phba->link_flag &= ~LS_IGNORE_ERATT;
3402 spin_unlock_irq(&phba->hbalock);
41415862 3403
41415862 3404 lpfc_hba_down_post(phba);
2e0fef85 3405 phba->link_state = LPFC_HBA_ERROR;
41415862 3406
2e0fef85 3407 return ha_copy & HA_ERATT ? 0 : 1;
dea3101e 3408}
3409
e59058c4 3410/**
3772a991 3411 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
e59058c4
JS
3412 * @phba: Pointer to HBA context object.
3413 *
3414 * This function resets the HBA by writing HC_INITFF to the control
3415 * register. After the HBA resets, this function resets all the iocb ring
3416 * indices. This function disables PCI layer parity checking during
3417 * the reset.
3418 * This function returns 0 always.
3419 * The caller is not required to hold any locks.
3420 **/
41415862 3421int
2e0fef85 3422lpfc_sli_brdreset(struct lpfc_hba *phba)
dea3101e 3423{
41415862 3424 struct lpfc_sli *psli;
dea3101e 3425 struct lpfc_sli_ring *pring;
41415862 3426 uint16_t cfg_value;
dea3101e 3427 int i;
dea3101e 3428
41415862 3429 psli = &phba->sli;
dea3101e 3430
41415862
JW
3431 /* Reset HBA */
3432 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3433 "0325 Reset HBA Data: x%x x%x\n",
2e0fef85 3434 phba->pport->port_state, psli->sli_flag);
dea3101e 3435
3436 /* perform board reset */
3437 phba->fc_eventTag = 0;
4d9ab994 3438 phba->link_events = 0;
2e0fef85
JS
3439 phba->pport->fc_myDID = 0;
3440 phba->pport->fc_prevDID = 0;
dea3101e 3441
41415862
JW
3442 /* Turn off parity checking and serr during the physical reset */
3443 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3444 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3445 (cfg_value &
3446 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3447
3772a991
JS
3448 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3449
41415862
JW
3450 /* Now toggle INITFF bit in the Host Control Register */
3451 writel(HC_INITFF, phba->HCregaddr);
3452 mdelay(1);
3453 readl(phba->HCregaddr); /* flush */
3454 writel(0, phba->HCregaddr);
3455 readl(phba->HCregaddr); /* flush */
3456
3457 /* Restore PCI cmd register */
3458 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
dea3101e 3459
3460 /* Initialize relevant SLI info */
41415862
JW
3461 for (i = 0; i < psli->num_rings; i++) {
3462 pring = &psli->ring[i];
dea3101e 3463 pring->flag = 0;
3464 pring->rspidx = 0;
3465 pring->next_cmdidx = 0;
3466 pring->local_getidx = 0;
3467 pring->cmdidx = 0;
3468 pring->missbufcnt = 0;
3469 }
dea3101e 3470
2e0fef85 3471 phba->link_state = LPFC_WARM_START;
41415862
JW
3472 return 0;
3473}
3474
e59058c4 3475/**
da0436e9
JS
3476 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3477 * @phba: Pointer to HBA context object.
3478 *
3479 * This function resets a SLI4 HBA. This function disables PCI layer parity
3480 * checking during resets the device. The caller is not required to hold
3481 * any locks.
3482 *
3483 * This function returns 0 always.
3484 **/
3485int
3486lpfc_sli4_brdreset(struct lpfc_hba *phba)
3487{
3488 struct lpfc_sli *psli = &phba->sli;
3489 uint16_t cfg_value;
3490 uint8_t qindx;
3491
3492 /* Reset HBA */
3493 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3494 "0295 Reset HBA Data: x%x x%x\n",
3495 phba->pport->port_state, psli->sli_flag);
3496
3497 /* perform board reset */
3498 phba->fc_eventTag = 0;
4d9ab994 3499 phba->link_events = 0;
da0436e9
JS
3500 phba->pport->fc_myDID = 0;
3501 phba->pport->fc_prevDID = 0;
3502
3503 /* Turn off parity checking and serr during the physical reset */
3504 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3505 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3506 (cfg_value &
3507 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3508
3509 spin_lock_irq(&phba->hbalock);
3510 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3511 phba->fcf.fcf_flag = 0;
3512 /* Clean up the child queue list for the CQs */
3513 list_del_init(&phba->sli4_hba.mbx_wq->list);
3514 list_del_init(&phba->sli4_hba.els_wq->list);
3515 list_del_init(&phba->sli4_hba.hdr_rq->list);
3516 list_del_init(&phba->sli4_hba.dat_rq->list);
3517 list_del_init(&phba->sli4_hba.mbx_cq->list);
3518 list_del_init(&phba->sli4_hba.els_cq->list);
da0436e9
JS
3519 for (qindx = 0; qindx < phba->cfg_fcp_wq_count; qindx++)
3520 list_del_init(&phba->sli4_hba.fcp_wq[qindx]->list);
3521 for (qindx = 0; qindx < phba->cfg_fcp_eq_count; qindx++)
3522 list_del_init(&phba->sli4_hba.fcp_cq[qindx]->list);
3523 spin_unlock_irq(&phba->hbalock);
3524
3525 /* Now physically reset the device */
3526 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3527 "0389 Performing PCI function reset!\n");
3528 /* Perform FCoE PCI function reset */
3529 lpfc_pci_function_reset(phba);
3530
3531 return 0;
3532}
3533
3534/**
3535 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
e59058c4
JS
3536 * @phba: Pointer to HBA context object.
3537 *
3538 * This function is called in the SLI initialization code path to
3539 * restart the HBA. The caller is not required to hold any lock.
3540 * This function writes MBX_RESTART mailbox command to the SLIM and
3541 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
3542 * function to free any pending commands. The function enables
3543 * POST only during the first initialization. The function returns zero.
3544 * The function does not guarantee completion of MBX_RESTART mailbox
3545 * command before the return of this function.
3546 **/
da0436e9
JS
3547static int
3548lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
41415862
JW
3549{
3550 MAILBOX_t *mb;
3551 struct lpfc_sli *psli;
41415862
JW
3552 volatile uint32_t word0;
3553 void __iomem *to_slim;
3554
2e0fef85 3555 spin_lock_irq(&phba->hbalock);
41415862
JW
3556
3557 psli = &phba->sli;
3558
3559 /* Restart HBA */
3560 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 3561 "0337 Restart HBA Data: x%x x%x\n",
2e0fef85 3562 phba->pport->port_state, psli->sli_flag);
41415862
JW
3563
3564 word0 = 0;
3565 mb = (MAILBOX_t *) &word0;
3566 mb->mbxCommand = MBX_RESTART;
3567 mb->mbxHc = 1;
3568
9290831f
JS
3569 lpfc_reset_barrier(phba);
3570
41415862
JW
3571 to_slim = phba->MBslimaddr;
3572 writel(*(uint32_t *) mb, to_slim);
3573 readl(to_slim); /* flush */
3574
3575 /* Only skip post after fc_ffinit is completed */
eaf15d5b 3576 if (phba->pport->port_state)
41415862 3577 word0 = 1; /* This is really setting up word1 */
eaf15d5b 3578 else
41415862 3579 word0 = 0; /* This is really setting up word1 */
65a29c16 3580 to_slim = phba->MBslimaddr + sizeof (uint32_t);
41415862
JW
3581 writel(*(uint32_t *) mb, to_slim);
3582 readl(to_slim); /* flush */
dea3101e 3583
41415862 3584 lpfc_sli_brdreset(phba);
2e0fef85
JS
3585 phba->pport->stopped = 0;
3586 phba->link_state = LPFC_INIT_START;
da0436e9 3587 phba->hba_flag = 0;
2e0fef85 3588 spin_unlock_irq(&phba->hbalock);
41415862 3589
64ba8818
JS
3590 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3591 psli->stats_start = get_seconds();
3592
eaf15d5b
JS
3593 /* Give the INITFF and Post time to settle. */
3594 mdelay(100);
41415862
JW
3595
3596 lpfc_hba_down_post(phba);
dea3101e 3597
3598 return 0;
3599}
3600
da0436e9
JS
3601/**
3602 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
3603 * @phba: Pointer to HBA context object.
3604 *
3605 * This function is called in the SLI initialization code path to restart
3606 * a SLI4 HBA. The caller is not required to hold any lock.
3607 * At the end of the function, it calls lpfc_hba_down_post function to
3608 * free any pending commands.
3609 **/
3610static int
3611lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
3612{
3613 struct lpfc_sli *psli = &phba->sli;
3614
3615
3616 /* Restart HBA */
3617 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3618 "0296 Restart HBA Data: x%x x%x\n",
3619 phba->pport->port_state, psli->sli_flag);
3620
3621 lpfc_sli4_brdreset(phba);
3622
3623 spin_lock_irq(&phba->hbalock);
3624 phba->pport->stopped = 0;
3625 phba->link_state = LPFC_INIT_START;
3626 phba->hba_flag = 0;
3627 spin_unlock_irq(&phba->hbalock);
3628
3629 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
3630 psli->stats_start = get_seconds();
3631
3632 lpfc_hba_down_post(phba);
3633
3634 return 0;
3635}
3636
3637/**
3638 * lpfc_sli_brdrestart - Wrapper func for restarting hba
3639 * @phba: Pointer to HBA context object.
3640 *
3641 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
3642 * API jump table function pointer from the lpfc_hba struct.
3643**/
3644int
3645lpfc_sli_brdrestart(struct lpfc_hba *phba)
3646{
3647 return phba->lpfc_sli_brdrestart(phba);
3648}
3649
e59058c4 3650/**
3621a710 3651 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
e59058c4
JS
3652 * @phba: Pointer to HBA context object.
3653 *
3654 * This function is called after a HBA restart to wait for successful
3655 * restart of the HBA. Successful restart of the HBA is indicated by
3656 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
3657 * iteration, the function will restart the HBA again. The function returns
3658 * zero if HBA successfully restarted else returns negative error code.
3659 **/
dea3101e 3660static int
3661lpfc_sli_chipset_init(struct lpfc_hba *phba)
3662{
3663 uint32_t status, i = 0;
3664
3665 /* Read the HBA Host Status Register */
3666 status = readl(phba->HSregaddr);
3667
3668 /* Check status register to see what current state is */
3669 i = 0;
3670 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
3671
3672 /* Check every 100ms for 5 retries, then every 500ms for 5, then
3673 * every 2.5 sec for 5, then reset board and every 2.5 sec for
3674 * 4.
3675 */
3676 if (i++ >= 20) {
3677 /* Adapter failed to init, timeout, status reg
3678 <status> */
ed957684 3679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3680 "0436 Adapter failed to init, "
09372820
JS
3681 "timeout, status reg x%x, "
3682 "FW Data: A8 x%x AC x%x\n", status,
3683 readl(phba->MBslimaddr + 0xa8),
3684 readl(phba->MBslimaddr + 0xac));
2e0fef85 3685 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3686 return -ETIMEDOUT;
3687 }
3688
3689 /* Check to see if any errors occurred during init */
3690 if (status & HS_FFERM) {
3691 /* ERROR: During chipset initialization */
3692 /* Adapter failed to init, chipset, status reg
3693 <status> */
ed957684 3694 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3695 "0437 Adapter failed to init, "
09372820
JS
3696 "chipset, status reg x%x, "
3697 "FW Data: A8 x%x AC x%x\n", status,
3698 readl(phba->MBslimaddr + 0xa8),
3699 readl(phba->MBslimaddr + 0xac));
2e0fef85 3700 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3701 return -EIO;
3702 }
3703
3704 if (i <= 5) {
3705 msleep(10);
3706 } else if (i <= 10) {
3707 msleep(500);
3708 } else {
3709 msleep(2500);
3710 }
3711
3712 if (i == 15) {
2e0fef85 3713 /* Do post */
92d7f7b0 3714 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3715 lpfc_sli_brdrestart(phba);
dea3101e 3716 }
3717 /* Read the HBA Host Status Register */
3718 status = readl(phba->HSregaddr);
3719 }
3720
3721 /* Check to see if any errors occurred during init */
3722 if (status & HS_FFERM) {
3723 /* ERROR: During chipset initialization */
3724 /* Adapter failed to init, chipset, status reg <status> */
ed957684 3725 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3726 "0438 Adapter failed to init, chipset, "
09372820
JS
3727 "status reg x%x, "
3728 "FW Data: A8 x%x AC x%x\n", status,
3729 readl(phba->MBslimaddr + 0xa8),
3730 readl(phba->MBslimaddr + 0xac));
2e0fef85 3731 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3732 return -EIO;
3733 }
3734
3735 /* Clear all interrupt enable conditions */
3736 writel(0, phba->HCregaddr);
3737 readl(phba->HCregaddr); /* flush */
3738
3739 /* setup host attn register */
3740 writel(0xffffffff, phba->HAregaddr);
3741 readl(phba->HAregaddr); /* flush */
3742 return 0;
3743}
3744
e59058c4 3745/**
3621a710 3746 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
e59058c4
JS
3747 *
3748 * This function calculates and returns the number of HBQs required to be
3749 * configured.
3750 **/
78b2d852 3751int
ed957684
JS
3752lpfc_sli_hbq_count(void)
3753{
92d7f7b0 3754 return ARRAY_SIZE(lpfc_hbq_defs);
ed957684
JS
3755}
3756
e59058c4 3757/**
3621a710 3758 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
e59058c4
JS
3759 *
3760 * This function adds the number of hbq entries in every HBQ to get
3761 * the total number of hbq entries required for the HBA and returns
3762 * the total count.
3763 **/
ed957684
JS
3764static int
3765lpfc_sli_hbq_entry_count(void)
3766{
3767 int hbq_count = lpfc_sli_hbq_count();
3768 int count = 0;
3769 int i;
3770
3771 for (i = 0; i < hbq_count; ++i)
92d7f7b0 3772 count += lpfc_hbq_defs[i]->entry_count;
ed957684
JS
3773 return count;
3774}
3775
e59058c4 3776/**
3621a710 3777 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
e59058c4
JS
3778 *
3779 * This function calculates amount of memory required for all hbq entries
3780 * to be configured and returns the total memory required.
3781 **/
dea3101e 3782int
ed957684
JS
3783lpfc_sli_hbq_size(void)
3784{
3785 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
3786}
3787
e59058c4 3788/**
3621a710 3789 * lpfc_sli_hbq_setup - configure and initialize HBQs
e59058c4
JS
3790 * @phba: Pointer to HBA context object.
3791 *
3792 * This function is called during the SLI initialization to configure
3793 * all the HBQs and post buffers to the HBQ. The caller is not
3794 * required to hold any locks. This function will return zero if successful
3795 * else it will return negative error code.
3796 **/
ed957684
JS
3797static int
3798lpfc_sli_hbq_setup(struct lpfc_hba *phba)
3799{
3800 int hbq_count = lpfc_sli_hbq_count();
3801 LPFC_MBOXQ_t *pmb;
3802 MAILBOX_t *pmbox;
3803 uint32_t hbqno;
3804 uint32_t hbq_entry_index;
ed957684 3805
92d7f7b0
JS
3806 /* Get a Mailbox buffer to setup mailbox
3807 * commands for HBA initialization
3808 */
ed957684
JS
3809 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3810
3811 if (!pmb)
3812 return -ENOMEM;
3813
04c68496 3814 pmbox = &pmb->u.mb;
ed957684
JS
3815
3816 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
3817 phba->link_state = LPFC_INIT_MBX_CMDS;
3163f725 3818 phba->hbq_in_use = 1;
ed957684
JS
3819
3820 hbq_entry_index = 0;
3821 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
3822 phba->hbqs[hbqno].next_hbqPutIdx = 0;
3823 phba->hbqs[hbqno].hbqPutIdx = 0;
3824 phba->hbqs[hbqno].local_hbqGetIdx = 0;
3825 phba->hbqs[hbqno].entry_count =
92d7f7b0 3826 lpfc_hbq_defs[hbqno]->entry_count;
51ef4c26
JS
3827 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
3828 hbq_entry_index, pmb);
ed957684
JS
3829 hbq_entry_index += phba->hbqs[hbqno].entry_count;
3830
3831 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
3832 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
3833 mbxStatus <status>, ring <num> */
3834
3835 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 3836 LOG_SLI | LOG_VPORT,
e8b62011 3837 "1805 Adapter failed to init. "
ed957684 3838 "Data: x%x x%x x%x\n",
e8b62011 3839 pmbox->mbxCommand,
ed957684
JS
3840 pmbox->mbxStatus, hbqno);
3841
3842 phba->link_state = LPFC_HBA_ERROR;
3843 mempool_free(pmb, phba->mbox_mem_pool);
ed957684
JS
3844 return ENXIO;
3845 }
3846 }
3847 phba->hbq_count = hbq_count;
3848
ed957684
JS
3849 mempool_free(pmb, phba->mbox_mem_pool);
3850
92d7f7b0 3851 /* Initially populate or replenish the HBQs */
d7c255b2
JS
3852 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
3853 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
ed957684
JS
3854 return 0;
3855}
3856
4f774513
JS
3857/**
3858 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
3859 * @phba: Pointer to HBA context object.
3860 *
3861 * This function is called during the SLI initialization to configure
3862 * all the HBQs and post buffers to the HBQ. The caller is not
3863 * required to hold any locks. This function will return zero if successful
3864 * else it will return negative error code.
3865 **/
3866static int
3867lpfc_sli4_rb_setup(struct lpfc_hba *phba)
3868{
3869 phba->hbq_in_use = 1;
3870 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
3871 phba->hbq_count = 1;
3872 /* Initially populate or replenish the HBQs */
3873 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
3874 return 0;
3875}
3876
e59058c4 3877/**
3621a710 3878 * lpfc_sli_config_port - Issue config port mailbox command
e59058c4
JS
3879 * @phba: Pointer to HBA context object.
3880 * @sli_mode: sli mode - 2/3
3881 *
3882 * This function is called by the sli intialization code path
3883 * to issue config_port mailbox command. This function restarts the
3884 * HBA firmware and issues a config_port mailbox command to configure
3885 * the SLI interface in the sli mode specified by sli_mode
3886 * variable. The caller is not required to hold any locks.
3887 * The function returns 0 if successful, else returns negative error
3888 * code.
3889 **/
9399627f
JS
3890int
3891lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
dea3101e 3892{
3893 LPFC_MBOXQ_t *pmb;
3894 uint32_t resetcount = 0, rc = 0, done = 0;
3895
3896 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3897 if (!pmb) {
2e0fef85 3898 phba->link_state = LPFC_HBA_ERROR;
dea3101e 3899 return -ENOMEM;
3900 }
3901
ed957684 3902 phba->sli_rev = sli_mode;
dea3101e 3903 while (resetcount < 2 && !done) {
2e0fef85 3904 spin_lock_irq(&phba->hbalock);
1c067a42 3905 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3906 spin_unlock_irq(&phba->hbalock);
92d7f7b0 3907 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
41415862 3908 lpfc_sli_brdrestart(phba);
dea3101e 3909 rc = lpfc_sli_chipset_init(phba);
3910 if (rc)
3911 break;
3912
2e0fef85 3913 spin_lock_irq(&phba->hbalock);
1c067a42 3914 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 3915 spin_unlock_irq(&phba->hbalock);
dea3101e 3916 resetcount++;
3917
ed957684
JS
3918 /* Call pre CONFIG_PORT mailbox command initialization. A
3919 * value of 0 means the call was successful. Any other
3920 * nonzero value is a failure, but if ERESTART is returned,
3921 * the driver may reset the HBA and try again.
3922 */
dea3101e 3923 rc = lpfc_config_port_prep(phba);
3924 if (rc == -ERESTART) {
ed957684 3925 phba->link_state = LPFC_LINK_UNKNOWN;
dea3101e 3926 continue;
34b02dcd 3927 } else if (rc)
dea3101e 3928 break;
2e0fef85 3929 phba->link_state = LPFC_INIT_MBX_CMDS;
dea3101e 3930 lpfc_config_port(phba, pmb);
3931 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
34b02dcd
JS
3932 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
3933 LPFC_SLI3_HBQ_ENABLED |
3934 LPFC_SLI3_CRP_ENABLED |
e2a0a9d6
JS
3935 LPFC_SLI3_INB_ENABLED |
3936 LPFC_SLI3_BG_ENABLED);
ed957684 3937 if (rc != MBX_SUCCESS) {
dea3101e 3938 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 3939 "0442 Adapter failed to init, mbxCmd x%x "
92d7f7b0 3940 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
04c68496 3941 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
2e0fef85 3942 spin_lock_irq(&phba->hbalock);
04c68496 3943 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
2e0fef85
JS
3944 spin_unlock_irq(&phba->hbalock);
3945 rc = -ENXIO;
04c68496
JS
3946 } else {
3947 /* Allow asynchronous mailbox command to go through */
3948 spin_lock_irq(&phba->hbalock);
3949 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
3950 spin_unlock_irq(&phba->hbalock);
ed957684 3951 done = 1;
04c68496 3952 }
dea3101e 3953 }
ed957684
JS
3954 if (!done) {
3955 rc = -EINVAL;
3956 goto do_prep_failed;
3957 }
04c68496
JS
3958 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
3959 if (!pmb->u.mb.un.varCfgPort.cMA) {
34b02dcd
JS
3960 rc = -ENXIO;
3961 goto do_prep_failed;
3962 }
04c68496 3963 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
34b02dcd 3964 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
04c68496
JS
3965 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
3966 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
3967 phba->max_vpi : phba->max_vports;
3968
34b02dcd
JS
3969 } else
3970 phba->max_vpi = 0;
04c68496
JS
3971 if (pmb->u.mb.un.varCfgPort.gdss)
3972 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
3973 if (pmb->u.mb.un.varCfgPort.gerbm)
34b02dcd 3974 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
04c68496 3975 if (pmb->u.mb.un.varCfgPort.gcrp)
34b02dcd 3976 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
04c68496 3977 if (pmb->u.mb.un.varCfgPort.ginb) {
34b02dcd 3978 phba->sli3_options |= LPFC_SLI3_INB_ENABLED;
8f34f4ce 3979 phba->hbq_get = phba->mbox->us.s3_inb_pgp.hbq_get;
34b02dcd
JS
3980 phba->port_gp = phba->mbox->us.s3_inb_pgp.port;
3981 phba->inb_ha_copy = &phba->mbox->us.s3_inb_pgp.ha_copy;
3982 phba->inb_counter = &phba->mbox->us.s3_inb_pgp.counter;
3983 phba->inb_last_counter =
3984 phba->mbox->us.s3_inb_pgp.counter;
3985 } else {
8f34f4ce 3986 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
34b02dcd
JS
3987 phba->port_gp = phba->mbox->us.s3_pgp.port;
3988 phba->inb_ha_copy = NULL;
3989 phba->inb_counter = NULL;
3990 }
e2a0a9d6
JS
3991
3992 if (phba->cfg_enable_bg) {
04c68496 3993 if (pmb->u.mb.un.varCfgPort.gbg)
e2a0a9d6
JS
3994 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
3995 else
3996 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3997 "0443 Adapter did not grant "
3998 "BlockGuard\n");
3999 }
34b02dcd 4000 } else {
8f34f4ce 4001 phba->hbq_get = NULL;
34b02dcd
JS
4002 phba->port_gp = phba->mbox->us.s2.port;
4003 phba->inb_ha_copy = NULL;
4004 phba->inb_counter = NULL;
d7c255b2 4005 phba->max_vpi = 0;
ed957684 4006 }
92d7f7b0 4007do_prep_failed:
ed957684
JS
4008 mempool_free(pmb, phba->mbox_mem_pool);
4009 return rc;
4010}
4011
e59058c4
JS
4012
4013/**
3621a710 4014 * lpfc_sli_hba_setup - SLI intialization function
e59058c4
JS
4015 * @phba: Pointer to HBA context object.
4016 *
4017 * This function is the main SLI intialization function. This function
4018 * is called by the HBA intialization code, HBA reset code and HBA
4019 * error attention handler code. Caller is not required to hold any
4020 * locks. This function issues config_port mailbox command to configure
4021 * the SLI, setup iocb rings and HBQ rings. In the end the function
4022 * calls the config_port_post function to issue init_link mailbox
4023 * command and to start the discovery. The function will return zero
4024 * if successful, else it will return negative error code.
4025 **/
ed957684
JS
4026int
4027lpfc_sli_hba_setup(struct lpfc_hba *phba)
4028{
4029 uint32_t rc;
92d7f7b0 4030 int mode = 3;
ed957684
JS
4031
4032 switch (lpfc_sli_mode) {
4033 case 2:
78b2d852 4034 if (phba->cfg_enable_npiv) {
92d7f7b0 4035 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011 4036 "1824 NPIV enabled: Override lpfc_sli_mode "
92d7f7b0 4037 "parameter (%d) to auto (0).\n",
e8b62011 4038 lpfc_sli_mode);
92d7f7b0
JS
4039 break;
4040 }
ed957684
JS
4041 mode = 2;
4042 break;
4043 case 0:
4044 case 3:
4045 break;
4046 default:
92d7f7b0 4047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4048 "1819 Unrecognized lpfc_sli_mode "
4049 "parameter: %d.\n", lpfc_sli_mode);
ed957684
JS
4050
4051 break;
4052 }
4053
9399627f
JS
4054 rc = lpfc_sli_config_port(phba, mode);
4055
ed957684 4056 if (rc && lpfc_sli_mode == 3)
92d7f7b0 4057 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
e8b62011
JS
4058 "1820 Unable to select SLI-3. "
4059 "Not supported by adapter.\n");
ed957684 4060 if (rc && mode != 2)
9399627f 4061 rc = lpfc_sli_config_port(phba, 2);
ed957684 4062 if (rc)
dea3101e 4063 goto lpfc_sli_hba_setup_error;
4064
ed957684
JS
4065 if (phba->sli_rev == 3) {
4066 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4067 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
ed957684
JS
4068 } else {
4069 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4070 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
92d7f7b0 4071 phba->sli3_options = 0;
ed957684
JS
4072 }
4073
4074 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011
JS
4075 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4076 phba->sli_rev, phba->max_vpi);
ed957684 4077 rc = lpfc_sli_ring_map(phba);
dea3101e 4078
4079 if (rc)
4080 goto lpfc_sli_hba_setup_error;
4081
9399627f 4082 /* Init HBQs */
ed957684
JS
4083 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4084 rc = lpfc_sli_hbq_setup(phba);
4085 if (rc)
4086 goto lpfc_sli_hba_setup_error;
4087 }
04c68496 4088 spin_lock_irq(&phba->hbalock);
dea3101e 4089 phba->sli.sli_flag |= LPFC_PROCESS_LA;
04c68496 4090 spin_unlock_irq(&phba->hbalock);
dea3101e 4091
4092 rc = lpfc_config_port_post(phba);
4093 if (rc)
4094 goto lpfc_sli_hba_setup_error;
4095
ed957684
JS
4096 return rc;
4097
92d7f7b0 4098lpfc_sli_hba_setup_error:
2e0fef85 4099 phba->link_state = LPFC_HBA_ERROR;
ed957684 4100 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
e8b62011 4101 "0445 Firmware initialization failed\n");
dea3101e 4102 return rc;
4103}
4104
e59058c4 4105/**
da0436e9
JS
4106 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4107 * @phba: Pointer to HBA context object.
4108 * @mboxq: mailbox pointer.
4109 * This function issue a dump mailbox command to read config region
4110 * 23 and parse the records in the region and populate driver
4111 * data structure.
e59058c4 4112 **/
da0436e9
JS
4113static int
4114lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba,
4115 LPFC_MBOXQ_t *mboxq)
dea3101e 4116{
da0436e9
JS
4117 struct lpfc_dmabuf *mp;
4118 struct lpfc_mqe *mqe;
4119 uint32_t data_length;
4120 int rc;
dea3101e 4121
da0436e9
JS
4122 /* Program the default value of vlan_id and fc_map */
4123 phba->valid_vlan = 0;
4124 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4125 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4126 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
2e0fef85 4127
da0436e9
JS
4128 mqe = &mboxq->u.mqe;
4129 if (lpfc_dump_fcoe_param(phba, mboxq))
4130 return -ENOMEM;
4131
4132 mp = (struct lpfc_dmabuf *) mboxq->context1;
4133 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4134
4135 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4136 "(%d):2571 Mailbox cmd x%x Status x%x "
4137 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4138 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4139 "CQ: x%x x%x x%x x%x\n",
4140 mboxq->vport ? mboxq->vport->vpi : 0,
4141 bf_get(lpfc_mqe_command, mqe),
4142 bf_get(lpfc_mqe_status, mqe),
4143 mqe->un.mb_words[0], mqe->un.mb_words[1],
4144 mqe->un.mb_words[2], mqe->un.mb_words[3],
4145 mqe->un.mb_words[4], mqe->un.mb_words[5],
4146 mqe->un.mb_words[6], mqe->un.mb_words[7],
4147 mqe->un.mb_words[8], mqe->un.mb_words[9],
4148 mqe->un.mb_words[10], mqe->un.mb_words[11],
4149 mqe->un.mb_words[12], mqe->un.mb_words[13],
4150 mqe->un.mb_words[14], mqe->un.mb_words[15],
4151 mqe->un.mb_words[16], mqe->un.mb_words[50],
4152 mboxq->mcqe.word0,
4153 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4154 mboxq->mcqe.trailer);
4155
4156 if (rc) {
4157 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4158 kfree(mp);
4159 return -EIO;
4160 }
4161 data_length = mqe->un.mb_words[5];
a0c87cbd 4162 if (data_length > DMP_RGN23_SIZE) {
d11e31dd
JS
4163 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4164 kfree(mp);
da0436e9 4165 return -EIO;
d11e31dd 4166 }
dea3101e 4167
da0436e9
JS
4168 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4169 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4170 kfree(mp);
4171 return 0;
4172}
e59058c4
JS
4173
4174/**
da0436e9
JS
4175 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4176 * @phba: pointer to lpfc hba data structure.
4177 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4178 * @vpd: pointer to the memory to hold resulting port vpd data.
4179 * @vpd_size: On input, the number of bytes allocated to @vpd.
4180 * On output, the number of data bytes in @vpd.
e59058c4 4181 *
da0436e9
JS
4182 * This routine executes a READ_REV SLI4 mailbox command. In
4183 * addition, this routine gets the port vpd data.
4184 *
4185 * Return codes
4186 * 0 - sucessful
4187 * ENOMEM - could not allocated memory.
e59058c4 4188 **/
da0436e9
JS
4189static int
4190lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4191 uint8_t *vpd, uint32_t *vpd_size)
dea3101e 4192{
da0436e9
JS
4193 int rc = 0;
4194 uint32_t dma_size;
4195 struct lpfc_dmabuf *dmabuf;
4196 struct lpfc_mqe *mqe;
dea3101e 4197
da0436e9
JS
4198 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4199 if (!dmabuf)
4200 return -ENOMEM;
4201
4202 /*
4203 * Get a DMA buffer for the vpd data resulting from the READ_REV
4204 * mailbox command.
a257bf90 4205 */
da0436e9
JS
4206 dma_size = *vpd_size;
4207 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4208 dma_size,
4209 &dmabuf->phys,
4210 GFP_KERNEL);
4211 if (!dmabuf->virt) {
4212 kfree(dmabuf);
4213 return -ENOMEM;
a257bf90 4214 }
da0436e9 4215 memset(dmabuf->virt, 0, dma_size);
a257bf90 4216
da0436e9
JS
4217 /*
4218 * The SLI4 implementation of READ_REV conflicts at word1,
4219 * bits 31:16 and SLI4 adds vpd functionality not present
4220 * in SLI3. This code corrects the conflicts.
1dcb58e5 4221 */
da0436e9
JS
4222 lpfc_read_rev(phba, mboxq);
4223 mqe = &mboxq->u.mqe;
4224 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4225 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4226 mqe->un.read_rev.word1 &= 0x0000FFFF;
4227 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4228 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4229
4230 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4231 if (rc) {
4232 dma_free_coherent(&phba->pcidev->dev, dma_size,
4233 dmabuf->virt, dmabuf->phys);
4234 return -EIO;
4235 }
1dcb58e5 4236
da0436e9
JS
4237 /*
4238 * The available vpd length cannot be bigger than the
4239 * DMA buffer passed to the port. Catch the less than
4240 * case and update the caller's size.
4241 */
4242 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4243 *vpd_size = mqe->un.read_rev.avail_vpd_len;
3772a991 4244
da0436e9
JS
4245 lpfc_sli_pcimem_bcopy(dmabuf->virt, vpd, *vpd_size);
4246 dma_free_coherent(&phba->pcidev->dev, dma_size,
4247 dmabuf->virt, dmabuf->phys);
4248 kfree(dmabuf);
4249 return 0;
dea3101e 4250}
4251
e59058c4 4252/**
da0436e9
JS
4253 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4254 * @phba: pointer to lpfc hba data structure.
e59058c4 4255 *
da0436e9
JS
4256 * This routine is called to explicitly arm the SLI4 device's completion and
4257 * event queues
4258 **/
4259static void
4260lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4261{
4262 uint8_t fcp_eqidx;
4263
4264 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4265 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
da0436e9
JS
4266 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4267 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4268 LPFC_QUEUE_REARM);
4269 lpfc_sli4_eq_release(phba->sli4_hba.sp_eq, LPFC_QUEUE_REARM);
4270 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++)
4271 lpfc_sli4_eq_release(phba->sli4_hba.fp_eq[fcp_eqidx],
4272 LPFC_QUEUE_REARM);
4273}
4274
4275/**
4276 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
4277 * @phba: Pointer to HBA context object.
4278 *
4279 * This function is the main SLI4 device intialization PCI function. This
4280 * function is called by the HBA intialization code, HBA reset code and
4281 * HBA error attention handler code. Caller is not required to hold any
4282 * locks.
4283 **/
4284int
4285lpfc_sli4_hba_setup(struct lpfc_hba *phba)
4286{
4287 int rc;
4288 LPFC_MBOXQ_t *mboxq;
4289 struct lpfc_mqe *mqe;
4290 uint8_t *vpd;
4291 uint32_t vpd_size;
4292 uint32_t ftr_rsp = 0;
4293 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
4294 struct lpfc_vport *vport = phba->pport;
4295 struct lpfc_dmabuf *mp;
4296
4297 /* Perform a PCI function reset to start from clean */
4298 rc = lpfc_pci_function_reset(phba);
4299 if (unlikely(rc))
4300 return -ENODEV;
4301
4302 /* Check the HBA Host Status Register for readyness */
4303 rc = lpfc_sli4_post_status_check(phba);
4304 if (unlikely(rc))
4305 return -ENODEV;
4306 else {
4307 spin_lock_irq(&phba->hbalock);
4308 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
4309 spin_unlock_irq(&phba->hbalock);
4310 }
4311
4312 /*
4313 * Allocate a single mailbox container for initializing the
4314 * port.
4315 */
4316 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4317 if (!mboxq)
4318 return -ENOMEM;
4319
4320 /*
4321 * Continue initialization with default values even if driver failed
4322 * to read FCoE param config regions
4323 */
4324 if (lpfc_sli4_read_fcoe_params(phba, mboxq))
4325 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
e4e74273 4326 "2570 Failed to read FCoE parameters\n");
da0436e9
JS
4327
4328 /* Issue READ_REV to collect vpd and FW information. */
4329 vpd_size = PAGE_SIZE;
4330 vpd = kzalloc(vpd_size, GFP_KERNEL);
4331 if (!vpd) {
4332 rc = -ENOMEM;
4333 goto out_free_mbox;
4334 }
4335
4336 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
4337 if (unlikely(rc))
4338 goto out_free_vpd;
4339
4340 mqe = &mboxq->u.mqe;
f1126688
JS
4341 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
4342 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
4343 phba->hba_flag |= HBA_FCOE_SUPPORT;
4344 if (phba->sli_rev != LPFC_SLI_REV4 ||
4345 !(phba->hba_flag & HBA_FCOE_SUPPORT)) {
da0436e9
JS
4346 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4347 "0376 READ_REV Error. SLI Level %d "
4348 "FCoE enabled %d\n",
f1126688 4349 phba->sli_rev, phba->hba_flag & HBA_FCOE_SUPPORT);
da0436e9
JS
4350 rc = -EIO;
4351 goto out_free_vpd;
4352 }
da0436e9
JS
4353 /*
4354 * Evaluate the read rev and vpd data. Populate the driver
4355 * state with the results. If this routine fails, the failure
4356 * is not fatal as the driver will use generic values.
4357 */
4358 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
4359 if (unlikely(!rc)) {
4360 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4361 "0377 Error %d parsing vpd. "
4362 "Using defaults.\n", rc);
4363 rc = 0;
4364 }
4365
f1126688
JS
4366 /* Save information as VPD data */
4367 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
4368 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
4369 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
4370 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
4371 &mqe->un.read_rev);
4372 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
4373 &mqe->un.read_rev);
4374 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
4375 &mqe->un.read_rev);
4376 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
4377 &mqe->un.read_rev);
4378 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
4379 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
4380 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
4381 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
4382 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
4383 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
4384 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4385 "(%d):0380 READ_REV Status x%x "
4386 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
4387 mboxq->vport ? mboxq->vport->vpi : 0,
4388 bf_get(lpfc_mqe_status, mqe),
4389 phba->vpd.rev.opFwName,
4390 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
4391 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
da0436e9
JS
4392
4393 /*
4394 * Discover the port's supported feature set and match it against the
4395 * hosts requests.
4396 */
4397 lpfc_request_features(phba, mboxq);
4398 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4399 if (unlikely(rc)) {
4400 rc = -EIO;
4401 goto out_free_vpd;
4402 }
4403
4404 /*
4405 * The port must support FCP initiator mode as this is the
4406 * only mode running in the host.
4407 */
4408 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
4409 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4410 "0378 No support for fcpi mode.\n");
4411 ftr_rsp++;
4412 }
4413
4414 /*
4415 * If the port cannot support the host's requested features
4416 * then turn off the global config parameters to disable the
4417 * feature in the driver. This is not a fatal error.
4418 */
4419 if ((phba->cfg_enable_bg) &&
4420 !(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4421 ftr_rsp++;
4422
4423 if (phba->max_vpi && phba->cfg_enable_npiv &&
4424 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4425 ftr_rsp++;
4426
4427 if (ftr_rsp) {
4428 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
4429 "0379 Feature Mismatch Data: x%08x %08x "
4430 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
4431 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
4432 phba->cfg_enable_npiv, phba->max_vpi);
4433 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
4434 phba->cfg_enable_bg = 0;
4435 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
4436 phba->cfg_enable_npiv = 0;
4437 }
4438
4439 /* These SLI3 features are assumed in SLI4 */
4440 spin_lock_irq(&phba->hbalock);
4441 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
4442 spin_unlock_irq(&phba->hbalock);
4443
4444 /* Read the port's service parameters. */
4445 lpfc_read_sparam(phba, mboxq, vport->vpi);
4446 mboxq->vport = vport;
4447 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4448 mp = (struct lpfc_dmabuf *) mboxq->context1;
4449 if (rc == MBX_SUCCESS) {
4450 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
4451 rc = 0;
4452 }
4453
4454 /*
4455 * This memory was allocated by the lpfc_read_sparam routine. Release
4456 * it to the mbuf pool.
4457 */
4458 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4459 kfree(mp);
4460 mboxq->context1 = NULL;
4461 if (unlikely(rc)) {
4462 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4463 "0382 READ_SPARAM command failed "
4464 "status %d, mbxStatus x%x\n",
4465 rc, bf_get(lpfc_mqe_status, mqe));
4466 phba->link_state = LPFC_HBA_ERROR;
4467 rc = -EIO;
4468 goto out_free_vpd;
4469 }
4470
4471 if (phba->cfg_soft_wwnn)
4472 u64_to_wwn(phba->cfg_soft_wwnn,
4473 vport->fc_sparam.nodeName.u.wwn);
4474 if (phba->cfg_soft_wwpn)
4475 u64_to_wwn(phba->cfg_soft_wwpn,
4476 vport->fc_sparam.portName.u.wwn);
4477 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
4478 sizeof(struct lpfc_name));
4479 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
4480 sizeof(struct lpfc_name));
4481
4482 /* Update the fc_host data structures with new wwn. */
4483 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
4484 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
4485
4486 /* Register SGL pool to the device using non-embedded mailbox command */
4487 rc = lpfc_sli4_post_sgl_list(phba);
4488 if (unlikely(rc)) {
4489 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4490 "0582 Error %d during sgl post operation\n",
4491 rc);
da0436e9
JS
4492 rc = -ENODEV;
4493 goto out_free_vpd;
4494 }
4495
4496 /* Register SCSI SGL pool to the device */
4497 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
4498 if (unlikely(rc)) {
4499 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6a9c52cf
JS
4500 "0383 Error %d during scsi sgl post "
4501 "operation\n", rc);
da0436e9
JS
4502 /* Some Scsi buffers were moved to the abort scsi list */
4503 /* A pci function reset will repost them */
4504 rc = -ENODEV;
4505 goto out_free_vpd;
4506 }
4507
4508 /* Post the rpi header region to the device. */
4509 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
4510 if (unlikely(rc)) {
4511 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4512 "0393 Error %d during rpi post operation\n",
4513 rc);
4514 rc = -ENODEV;
4515 goto out_free_vpd;
4516 }
0c287589
JS
4517 if (phba->cfg_enable_fip)
4518 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 1);
4519 else
4520 bf_set(lpfc_fip_flag, &phba->sli4_hba.sli4_flags, 0);
da0436e9
JS
4521
4522 /* Set up all the queues to the device */
4523 rc = lpfc_sli4_queue_setup(phba);
4524 if (unlikely(rc)) {
4525 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4526 "0381 Error %d during queue setup.\n ", rc);
4527 goto out_stop_timers;
4528 }
4529
4530 /* Arm the CQs and then EQs on device */
4531 lpfc_sli4_arm_cqeq_intr(phba);
4532
4533 /* Indicate device interrupt mode */
4534 phba->sli4_hba.intr_enable = 1;
4535
4536 /* Allow asynchronous mailbox command to go through */
4537 spin_lock_irq(&phba->hbalock);
4538 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4539 spin_unlock_irq(&phba->hbalock);
4540
4541 /* Post receive buffers to the device */
4542 lpfc_sli4_rb_setup(phba);
4543
4544 /* Start the ELS watchdog timer */
8fa38513
JS
4545 mod_timer(&vport->els_tmofunc,
4546 jiffies + HZ * (phba->fc_ratov * 2));
da0436e9
JS
4547
4548 /* Start heart beat timer */
4549 mod_timer(&phba->hb_tmofunc,
4550 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
4551 phba->hb_outstanding = 0;
4552 phba->last_completion_time = jiffies;
4553
4554 /* Start error attention (ERATT) polling timer */
4555 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
4556
4557 /*
4558 * The port is ready, set the host's link state to LINK_DOWN
4559 * in preparation for link interrupts.
4560 */
4561 lpfc_init_link(phba, mboxq, phba->cfg_topology, phba->cfg_link_speed);
4562 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
4563 lpfc_set_loopback_flag(phba);
4564 /* Change driver state to LPFC_LINK_DOWN right before init link */
4565 spin_lock_irq(&phba->hbalock);
4566 phba->link_state = LPFC_LINK_DOWN;
4567 spin_unlock_irq(&phba->hbalock);
4568 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
4569 if (unlikely(rc != MBX_NOT_FINISHED)) {
4570 kfree(vpd);
4571 return 0;
4572 } else
4573 rc = -EIO;
4574
4575 /* Unset all the queues set up in this routine when error out */
4576 if (rc)
4577 lpfc_sli4_queue_unset(phba);
4578
4579out_stop_timers:
4580 if (rc)
4581 lpfc_stop_hba_timers(phba);
4582out_free_vpd:
4583 kfree(vpd);
4584out_free_mbox:
4585 mempool_free(mboxq, phba->mbox_mem_pool);
4586 return rc;
4587}
4588
4589/**
4590 * lpfc_mbox_timeout - Timeout call back function for mbox timer
4591 * @ptr: context object - pointer to hba structure.
4592 *
4593 * This is the callback function for mailbox timer. The mailbox
4594 * timer is armed when a new mailbox command is issued and the timer
4595 * is deleted when the mailbox complete. The function is called by
4596 * the kernel timer code when a mailbox does not complete within
4597 * expected time. This function wakes up the worker thread to
4598 * process the mailbox timeout and returns. All the processing is
4599 * done by the worker thread function lpfc_mbox_timeout_handler.
4600 **/
4601void
4602lpfc_mbox_timeout(unsigned long ptr)
4603{
4604 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4605 unsigned long iflag;
4606 uint32_t tmo_posted;
4607
4608 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
4609 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
4610 if (!tmo_posted)
4611 phba->pport->work_port_events |= WORKER_MBOX_TMO;
4612 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
4613
4614 if (!tmo_posted)
4615 lpfc_worker_wake_up(phba);
4616 return;
4617}
4618
4619
4620/**
4621 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
4622 * @phba: Pointer to HBA context object.
4623 *
4624 * This function is called from worker thread when a mailbox command times out.
4625 * The caller is not required to hold any locks. This function will reset the
4626 * HBA and recover all the pending commands.
4627 **/
4628void
4629lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
4630{
4631 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
04c68496 4632 MAILBOX_t *mb = &pmbox->u.mb;
da0436e9
JS
4633 struct lpfc_sli *psli = &phba->sli;
4634 struct lpfc_sli_ring *pring;
4635
4636 /* Check the pmbox pointer first. There is a race condition
4637 * between the mbox timeout handler getting executed in the
4638 * worklist and the mailbox actually completing. When this
4639 * race condition occurs, the mbox_active will be NULL.
4640 */
4641 spin_lock_irq(&phba->hbalock);
4642 if (pmbox == NULL) {
4643 lpfc_printf_log(phba, KERN_WARNING,
4644 LOG_MBOX | LOG_SLI,
4645 "0353 Active Mailbox cleared - mailbox timeout "
4646 "exiting\n");
4647 spin_unlock_irq(&phba->hbalock);
4648 return;
4649 }
4650
4651 /* Mbox cmd <mbxCommand> timeout */
4652 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4653 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
4654 mb->mbxCommand,
4655 phba->pport->port_state,
4656 phba->sli.sli_flag,
4657 phba->sli.mbox_active);
4658 spin_unlock_irq(&phba->hbalock);
4659
4660 /* Setting state unknown so lpfc_sli_abort_iocb_ring
4661 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
4662 * it to fail all oustanding SCSI IO.
4663 */
4664 spin_lock_irq(&phba->pport->work_port_lock);
4665 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
4666 spin_unlock_irq(&phba->pport->work_port_lock);
4667 spin_lock_irq(&phba->hbalock);
4668 phba->link_state = LPFC_LINK_UNKNOWN;
f4b4c68f 4669 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
da0436e9
JS
4670 spin_unlock_irq(&phba->hbalock);
4671
4672 pring = &psli->ring[psli->fcp_ring];
4673 lpfc_sli_abort_iocb_ring(phba, pring);
4674
4675 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4676 "0345 Resetting board due to mailbox timeout\n");
4677
4678 /* Reset the HBA device */
4679 lpfc_reset_hba(phba);
4680}
4681
4682/**
4683 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
4684 * @phba: Pointer to HBA context object.
4685 * @pmbox: Pointer to mailbox object.
4686 * @flag: Flag indicating how the mailbox need to be processed.
4687 *
4688 * This function is called by discovery code and HBA management code
4689 * to submit a mailbox command to firmware with SLI-3 interface spec. This
4690 * function gets the hbalock to protect the data structures.
4691 * The mailbox command can be submitted in polling mode, in which case
4692 * this function will wait in a polling loop for the completion of the
4693 * mailbox.
4694 * If the mailbox is submitted in no_wait mode (not polling) the
4695 * function will submit the command and returns immediately without waiting
4696 * for the mailbox completion. The no_wait is supported only when HBA
4697 * is in SLI2/SLI3 mode - interrupts are enabled.
4698 * The SLI interface allows only one mailbox pending at a time. If the
4699 * mailbox is issued in polling mode and there is already a mailbox
4700 * pending, then the function will return an error. If the mailbox is issued
4701 * in NO_WAIT mode and there is a mailbox pending already, the function
4702 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
4703 * The sli layer owns the mailbox object until the completion of mailbox
4704 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
4705 * return codes the caller owns the mailbox command after the return of
4706 * the function.
e59058c4 4707 **/
3772a991
JS
4708static int
4709lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
4710 uint32_t flag)
dea3101e 4711{
dea3101e 4712 MAILBOX_t *mb;
2e0fef85 4713 struct lpfc_sli *psli = &phba->sli;
dea3101e 4714 uint32_t status, evtctr;
4715 uint32_t ha_copy;
4716 int i;
09372820 4717 unsigned long timeout;
dea3101e 4718 unsigned long drvr_flag = 0;
34b02dcd 4719 uint32_t word0, ldata;
dea3101e 4720 void __iomem *to_slim;
58da1ffb
JS
4721 int processing_queue = 0;
4722
4723 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4724 if (!pmbox) {
8568a4d2 4725 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
58da1ffb 4726 /* processing mbox queue from intr_handler */
3772a991
JS
4727 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
4728 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4729 return MBX_SUCCESS;
4730 }
58da1ffb 4731 processing_queue = 1;
58da1ffb
JS
4732 pmbox = lpfc_mbox_get(phba);
4733 if (!pmbox) {
4734 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4735 return MBX_SUCCESS;
4736 }
4737 }
dea3101e 4738
ed957684 4739 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
92d7f7b0 4740 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
ed957684 4741 if(!pmbox->vport) {
58da1ffb 4742 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
ed957684 4743 lpfc_printf_log(phba, KERN_ERR,
92d7f7b0 4744 LOG_MBOX | LOG_VPORT,
e8b62011 4745 "1806 Mbox x%x failed. No vport\n",
3772a991 4746 pmbox->u.mb.mbxCommand);
ed957684 4747 dump_stack();
58da1ffb 4748 goto out_not_finished;
ed957684
JS
4749 }
4750 }
4751
8d63f375 4752 /* If the PCI channel is in offline state, do not post mbox. */
58da1ffb
JS
4753 if (unlikely(pci_channel_offline(phba->pcidev))) {
4754 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4755 goto out_not_finished;
4756 }
8d63f375 4757
a257bf90
JS
4758 /* If HBA has a deferred error attention, fail the iocb. */
4759 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
4760 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
4761 goto out_not_finished;
4762 }
4763
dea3101e 4764 psli = &phba->sli;
92d7f7b0 4765
3772a991 4766 mb = &pmbox->u.mb;
dea3101e 4767 status = MBX_SUCCESS;
4768
2e0fef85
JS
4769 if (phba->link_state == LPFC_HBA_ERROR) {
4770 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
41415862
JW
4771
4772 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4773 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4774 "(%d):0311 Mailbox command x%x cannot "
4775 "issue Data: x%x x%x\n",
4776 pmbox->vport ? pmbox->vport->vpi : 0,
4777 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4778 goto out_not_finished;
41415862
JW
4779 }
4780
9290831f
JS
4781 if (mb->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT &&
4782 !(readl(phba->HCregaddr) & HC_MBINT_ENA)) {
2e0fef85 4783 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
3772a991
JS
4784 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4785 "(%d):2528 Mailbox command x%x cannot "
4786 "issue Data: x%x x%x\n",
4787 pmbox->vport ? pmbox->vport->vpi : 0,
4788 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
58da1ffb 4789 goto out_not_finished;
9290831f
JS
4790 }
4791
dea3101e 4792 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
4793 /* Polling for a mbox command when another one is already active
4794 * is not allowed in SLI. Also, the driver must have established
4795 * SLI2 mode to queue and process multiple mbox commands.
4796 */
4797
4798 if (flag & MBX_POLL) {
2e0fef85 4799 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4800
4801 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4802 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4803 "(%d):2529 Mailbox command x%x "
4804 "cannot issue Data: x%x x%x\n",
4805 pmbox->vport ? pmbox->vport->vpi : 0,
4806 pmbox->u.mb.mbxCommand,
4807 psli->sli_flag, flag);
58da1ffb 4808 goto out_not_finished;
dea3101e 4809 }
4810
3772a991 4811 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
2e0fef85 4812 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4813 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4814 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4815 "(%d):2530 Mailbox command x%x "
4816 "cannot issue Data: x%x x%x\n",
4817 pmbox->vport ? pmbox->vport->vpi : 0,
4818 pmbox->u.mb.mbxCommand,
4819 psli->sli_flag, flag);
58da1ffb 4820 goto out_not_finished;
dea3101e 4821 }
4822
dea3101e 4823 /* Another mailbox command is still being processed, queue this
4824 * command to be processed later.
4825 */
4826 lpfc_mbox_put(phba, pmbox);
4827
4828 /* Mbox cmd issue - BUSY */
ed957684 4829 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4830 "(%d):0308 Mbox cmd issue - BUSY Data: "
92d7f7b0 4831 "x%x x%x x%x x%x\n",
92d7f7b0
JS
4832 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
4833 mb->mbxCommand, phba->pport->port_state,
4834 psli->sli_flag, flag);
dea3101e 4835
4836 psli->slistat.mbox_busy++;
2e0fef85 4837 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4838
858c9f6c
JS
4839 if (pmbox->vport) {
4840 lpfc_debugfs_disc_trc(pmbox->vport,
4841 LPFC_DISC_TRC_MBOX_VPORT,
4842 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
4843 (uint32_t)mb->mbxCommand,
4844 mb->un.varWords[0], mb->un.varWords[1]);
4845 }
4846 else {
4847 lpfc_debugfs_disc_trc(phba->pport,
4848 LPFC_DISC_TRC_MBOX,
4849 "MBOX Bsy: cmd:x%x mb:x%x x%x",
4850 (uint32_t)mb->mbxCommand,
4851 mb->un.varWords[0], mb->un.varWords[1]);
4852 }
4853
2e0fef85 4854 return MBX_BUSY;
dea3101e 4855 }
4856
dea3101e 4857 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4858
4859 /* If we are not polling, we MUST be in SLI2 mode */
4860 if (flag != MBX_POLL) {
3772a991 4861 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
41415862 4862 (mb->mbxCommand != MBX_KILL_BOARD)) {
dea3101e 4863 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4864 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
dea3101e 4865 /* Mbox command <mbxCommand> cannot issue */
3772a991
JS
4866 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
4867 "(%d):2531 Mailbox command x%x "
4868 "cannot issue Data: x%x x%x\n",
4869 pmbox->vport ? pmbox->vport->vpi : 0,
4870 pmbox->u.mb.mbxCommand,
4871 psli->sli_flag, flag);
58da1ffb 4872 goto out_not_finished;
dea3101e 4873 }
4874 /* timeout active mbox command */
a309a6b6
JS
4875 mod_timer(&psli->mbox_tmo, (jiffies +
4876 (HZ * lpfc_mbox_tmo_val(phba, mb->mbxCommand))));
dea3101e 4877 }
4878
4879 /* Mailbox cmd <cmd> issue */
ed957684 4880 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
e8b62011 4881 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
92d7f7b0 4882 "x%x\n",
e8b62011 4883 pmbox->vport ? pmbox->vport->vpi : 0,
92d7f7b0
JS
4884 mb->mbxCommand, phba->pport->port_state,
4885 psli->sli_flag, flag);
dea3101e 4886
858c9f6c
JS
4887 if (mb->mbxCommand != MBX_HEARTBEAT) {
4888 if (pmbox->vport) {
4889 lpfc_debugfs_disc_trc(pmbox->vport,
4890 LPFC_DISC_TRC_MBOX_VPORT,
4891 "MBOX Send vport: cmd:x%x mb:x%x x%x",
4892 (uint32_t)mb->mbxCommand,
4893 mb->un.varWords[0], mb->un.varWords[1]);
4894 }
4895 else {
4896 lpfc_debugfs_disc_trc(phba->pport,
4897 LPFC_DISC_TRC_MBOX,
4898 "MBOX Send: cmd:x%x mb:x%x x%x",
4899 (uint32_t)mb->mbxCommand,
4900 mb->un.varWords[0], mb->un.varWords[1]);
4901 }
4902 }
4903
dea3101e 4904 psli->slistat.mbox_cmd++;
4905 evtctr = psli->slistat.mbox_event;
4906
4907 /* next set own bit for the adapter and copy over command word */
4908 mb->mbxOwner = OWN_CHIP;
4909
3772a991 4910 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4911 /* First copy command data to host SLIM area */
34b02dcd 4912 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4913 } else {
9290831f 4914 if (mb->mbxCommand == MBX_CONFIG_PORT) {
dea3101e 4915 /* copy command data into host mbox for cmpl */
34b02dcd 4916 lpfc_sli_pcimem_bcopy(mb, phba->mbox, MAILBOX_CMD_SIZE);
dea3101e 4917 }
4918
4919 /* First copy mbox command data to HBA SLIM, skip past first
4920 word */
4921 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4922 lpfc_memcpy_to_slim(to_slim, &mb->un.varWords[0],
4923 MAILBOX_CMD_SIZE - sizeof (uint32_t));
4924
4925 /* Next copy over first word, with mbxOwner set */
34b02dcd 4926 ldata = *((uint32_t *)mb);
dea3101e 4927 to_slim = phba->MBslimaddr;
4928 writel(ldata, to_slim);
4929 readl(to_slim); /* flush */
4930
4931 if (mb->mbxCommand == MBX_CONFIG_PORT) {
4932 /* switch over to host mailbox */
3772a991 4933 psli->sli_flag |= LPFC_SLI_ACTIVE;
dea3101e 4934 }
4935 }
4936
4937 wmb();
dea3101e 4938
4939 switch (flag) {
4940 case MBX_NOWAIT:
09372820 4941 /* Set up reference to mailbox command */
dea3101e 4942 psli->mbox_active = pmbox;
09372820
JS
4943 /* Interrupt board to do it */
4944 writel(CA_MBATT, phba->CAregaddr);
4945 readl(phba->CAregaddr); /* flush */
4946 /* Don't wait for it to finish, just return */
dea3101e 4947 break;
4948
4949 case MBX_POLL:
09372820 4950 /* Set up null reference to mailbox command */
dea3101e 4951 psli->mbox_active = NULL;
09372820
JS
4952 /* Interrupt board to do it */
4953 writel(CA_MBATT, phba->CAregaddr);
4954 readl(phba->CAregaddr); /* flush */
4955
3772a991 4956 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4957 /* First read mbox status word */
34b02dcd 4958 word0 = *((uint32_t *)phba->mbox);
dea3101e 4959 word0 = le32_to_cpu(word0);
4960 } else {
4961 /* First read mbox status word */
4962 word0 = readl(phba->MBslimaddr);
4963 }
4964
4965 /* Read the HBA Host Attention Register */
4966 ha_copy = readl(phba->HAregaddr);
09372820
JS
4967 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
4968 mb->mbxCommand) *
4969 1000) + jiffies;
4970 i = 0;
dea3101e 4971 /* Wait for command to complete */
41415862
JW
4972 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
4973 (!(ha_copy & HA_MBATT) &&
2e0fef85 4974 (phba->link_state > LPFC_WARM_START))) {
09372820 4975 if (time_after(jiffies, timeout)) {
dea3101e 4976 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
2e0fef85 4977 spin_unlock_irqrestore(&phba->hbalock,
dea3101e 4978 drvr_flag);
58da1ffb 4979 goto out_not_finished;
dea3101e 4980 }
4981
4982 /* Check if we took a mbox interrupt while we were
4983 polling */
4984 if (((word0 & OWN_CHIP) != OWN_CHIP)
4985 && (evtctr != psli->slistat.mbox_event))
4986 break;
4987
09372820
JS
4988 if (i++ > 10) {
4989 spin_unlock_irqrestore(&phba->hbalock,
4990 drvr_flag);
4991 msleep(1);
4992 spin_lock_irqsave(&phba->hbalock, drvr_flag);
4993 }
dea3101e 4994
3772a991 4995 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 4996 /* First copy command data */
34b02dcd 4997 word0 = *((uint32_t *)phba->mbox);
dea3101e 4998 word0 = le32_to_cpu(word0);
4999 if (mb->mbxCommand == MBX_CONFIG_PORT) {
5000 MAILBOX_t *slimmb;
34b02dcd 5001 uint32_t slimword0;
dea3101e 5002 /* Check real SLIM for any errors */
5003 slimword0 = readl(phba->MBslimaddr);
5004 slimmb = (MAILBOX_t *) & slimword0;
5005 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
5006 && slimmb->mbxStatus) {
5007 psli->sli_flag &=
3772a991 5008 ~LPFC_SLI_ACTIVE;
dea3101e 5009 word0 = slimword0;
5010 }
5011 }
5012 } else {
5013 /* First copy command data */
5014 word0 = readl(phba->MBslimaddr);
5015 }
5016 /* Read the HBA Host Attention Register */
5017 ha_copy = readl(phba->HAregaddr);
5018 }
5019
3772a991 5020 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
dea3101e 5021 /* copy results back to user */
34b02dcd 5022 lpfc_sli_pcimem_bcopy(phba->mbox, mb, MAILBOX_CMD_SIZE);
dea3101e 5023 } else {
5024 /* First copy command data */
5025 lpfc_memcpy_from_slim(mb, phba->MBslimaddr,
5026 MAILBOX_CMD_SIZE);
5027 if ((mb->mbxCommand == MBX_DUMP_MEMORY) &&
5028 pmbox->context2) {
92d7f7b0 5029 lpfc_memcpy_from_slim((void *)pmbox->context2,
dea3101e 5030 phba->MBslimaddr + DMP_RSP_OFFSET,
5031 mb->un.varDmp.word_cnt);
5032 }
5033 }
5034
5035 writel(HA_MBATT, phba->HAregaddr);
5036 readl(phba->HAregaddr); /* flush */
5037
5038 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5039 status = mb->mbxStatus;
5040 }
5041
2e0fef85
JS
5042 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
5043 return status;
58da1ffb
JS
5044
5045out_not_finished:
5046 if (processing_queue) {
da0436e9 5047 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
58da1ffb
JS
5048 lpfc_mbox_cmpl_put(phba, pmbox);
5049 }
5050 return MBX_NOT_FINISHED;
dea3101e 5051}
5052
f1126688
JS
5053/**
5054 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
5055 * @phba: Pointer to HBA context object.
5056 *
5057 * The function blocks the posting of SLI4 asynchronous mailbox commands from
5058 * the driver internal pending mailbox queue. It will then try to wait out the
5059 * possible outstanding mailbox command before return.
5060 *
5061 * Returns:
5062 * 0 - the outstanding mailbox command completed; otherwise, the wait for
5063 * the outstanding mailbox command timed out.
5064 **/
5065static int
5066lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
5067{
5068 struct lpfc_sli *psli = &phba->sli;
5069 uint8_t actcmd = MBX_HEARTBEAT;
5070 int rc = 0;
5071 unsigned long timeout;
5072
5073 /* Mark the asynchronous mailbox command posting as blocked */
5074 spin_lock_irq(&phba->hbalock);
5075 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
5076 if (phba->sli.mbox_active)
5077 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
5078 spin_unlock_irq(&phba->hbalock);
5079 /* Determine how long we might wait for the active mailbox
5080 * command to be gracefully completed by firmware.
5081 */
5082 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) * 1000) +
5083 jiffies;
5084 /* Wait for the outstnading mailbox command to complete */
5085 while (phba->sli.mbox_active) {
5086 /* Check active mailbox complete status every 2ms */
5087 msleep(2);
5088 if (time_after(jiffies, timeout)) {
5089 /* Timeout, marked the outstanding cmd not complete */
5090 rc = 1;
5091 break;
5092 }
5093 }
5094
5095 /* Can not cleanly block async mailbox command, fails it */
5096 if (rc) {
5097 spin_lock_irq(&phba->hbalock);
5098 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5099 spin_unlock_irq(&phba->hbalock);
5100 }
5101 return rc;
5102}
5103
5104/**
5105 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
5106 * @phba: Pointer to HBA context object.
5107 *
5108 * The function unblocks and resume posting of SLI4 asynchronous mailbox
5109 * commands from the driver internal pending mailbox queue. It makes sure
5110 * that there is no outstanding mailbox command before resuming posting
5111 * asynchronous mailbox commands. If, for any reason, there is outstanding
5112 * mailbox command, it will try to wait it out before resuming asynchronous
5113 * mailbox command posting.
5114 **/
5115static void
5116lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
5117{
5118 struct lpfc_sli *psli = &phba->sli;
5119
5120 spin_lock_irq(&phba->hbalock);
5121 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5122 /* Asynchronous mailbox posting is not blocked, do nothing */
5123 spin_unlock_irq(&phba->hbalock);
5124 return;
5125 }
5126
5127 /* Outstanding synchronous mailbox command is guaranteed to be done,
5128 * successful or timeout, after timing-out the outstanding mailbox
5129 * command shall always be removed, so just unblock posting async
5130 * mailbox command and resume
5131 */
5132 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
5133 spin_unlock_irq(&phba->hbalock);
5134
5135 /* wake up worker thread to post asynchronlous mailbox command */
5136 lpfc_worker_wake_up(phba);
5137}
5138
da0436e9
JS
5139/**
5140 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
5141 * @phba: Pointer to HBA context object.
5142 * @mboxq: Pointer to mailbox object.
5143 *
5144 * The function posts a mailbox to the port. The mailbox is expected
5145 * to be comletely filled in and ready for the port to operate on it.
5146 * This routine executes a synchronous completion operation on the
5147 * mailbox by polling for its completion.
5148 *
5149 * The caller must not be holding any locks when calling this routine.
5150 *
5151 * Returns:
5152 * MBX_SUCCESS - mailbox posted successfully
5153 * Any of the MBX error values.
5154 **/
5155static int
5156lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
5157{
5158 int rc = MBX_SUCCESS;
5159 unsigned long iflag;
5160 uint32_t db_ready;
5161 uint32_t mcqe_status;
5162 uint32_t mbx_cmnd;
5163 unsigned long timeout;
5164 struct lpfc_sli *psli = &phba->sli;
5165 struct lpfc_mqe *mb = &mboxq->u.mqe;
5166 struct lpfc_bmbx_create *mbox_rgn;
5167 struct dma_address *dma_address;
5168 struct lpfc_register bmbx_reg;
5169
5170 /*
5171 * Only one mailbox can be active to the bootstrap mailbox region
5172 * at a time and there is no queueing provided.
5173 */
5174 spin_lock_irqsave(&phba->hbalock, iflag);
5175 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5176 spin_unlock_irqrestore(&phba->hbalock, iflag);
5177 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5178 "(%d):2532 Mailbox command x%x (x%x) "
5179 "cannot issue Data: x%x x%x\n",
5180 mboxq->vport ? mboxq->vport->vpi : 0,
5181 mboxq->u.mb.mbxCommand,
5182 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5183 psli->sli_flag, MBX_POLL);
5184 return MBXERR_ERROR;
5185 }
5186 /* The server grabs the token and owns it until release */
5187 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5188 phba->sli.mbox_active = mboxq;
5189 spin_unlock_irqrestore(&phba->hbalock, iflag);
5190
5191 /*
5192 * Initialize the bootstrap memory region to avoid stale data areas
5193 * in the mailbox post. Then copy the caller's mailbox contents to
5194 * the bmbx mailbox region.
5195 */
5196 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
5197 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
5198 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
5199 sizeof(struct lpfc_mqe));
5200
5201 /* Post the high mailbox dma address to the port and wait for ready. */
5202 dma_address = &phba->sli4_hba.bmbx.dma_address;
5203 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
5204
5205 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5206 * 1000) + jiffies;
5207 do {
5208 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5209 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5210 if (!db_ready)
5211 msleep(2);
5212
5213 if (time_after(jiffies, timeout)) {
5214 rc = MBXERR_ERROR;
5215 goto exit;
5216 }
5217 } while (!db_ready);
5218
5219 /* Post the low mailbox dma address to the port. */
5220 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
5221 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mbx_cmnd)
5222 * 1000) + jiffies;
5223 do {
5224 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
5225 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
5226 if (!db_ready)
5227 msleep(2);
5228
5229 if (time_after(jiffies, timeout)) {
5230 rc = MBXERR_ERROR;
5231 goto exit;
5232 }
5233 } while (!db_ready);
5234
5235 /*
5236 * Read the CQ to ensure the mailbox has completed.
5237 * If so, update the mailbox status so that the upper layers
5238 * can complete the request normally.
5239 */
5240 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
5241 sizeof(struct lpfc_mqe));
5242 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
5243 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
5244 sizeof(struct lpfc_mcqe));
5245 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
5246
5247 /* Prefix the mailbox status with range x4000 to note SLI4 status. */
5248 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
5249 bf_set(lpfc_mqe_status, mb, LPFC_MBX_ERROR_RANGE | mcqe_status);
5250 rc = MBXERR_ERROR;
5251 }
5252
5253 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5254 "(%d):0356 Mailbox cmd x%x (x%x) Status x%x "
5255 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
5256 " x%x x%x CQ: x%x x%x x%x x%x\n",
5257 mboxq->vport ? mboxq->vport->vpi : 0,
5258 mbx_cmnd, lpfc_sli4_mbox_opcode_get(phba, mboxq),
5259 bf_get(lpfc_mqe_status, mb),
5260 mb->un.mb_words[0], mb->un.mb_words[1],
5261 mb->un.mb_words[2], mb->un.mb_words[3],
5262 mb->un.mb_words[4], mb->un.mb_words[5],
5263 mb->un.mb_words[6], mb->un.mb_words[7],
5264 mb->un.mb_words[8], mb->un.mb_words[9],
5265 mb->un.mb_words[10], mb->un.mb_words[11],
5266 mb->un.mb_words[12], mboxq->mcqe.word0,
5267 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
5268 mboxq->mcqe.trailer);
5269exit:
5270 /* We are holding the token, no needed for lock when release */
5271 spin_lock_irqsave(&phba->hbalock, iflag);
5272 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5273 phba->sli.mbox_active = NULL;
5274 spin_unlock_irqrestore(&phba->hbalock, iflag);
5275 return rc;
5276}
5277
5278/**
5279 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
5280 * @phba: Pointer to HBA context object.
5281 * @pmbox: Pointer to mailbox object.
5282 * @flag: Flag indicating how the mailbox need to be processed.
5283 *
5284 * This function is called by discovery code and HBA management code to submit
5285 * a mailbox command to firmware with SLI-4 interface spec.
5286 *
5287 * Return codes the caller owns the mailbox command after the return of the
5288 * function.
5289 **/
5290static int
5291lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
5292 uint32_t flag)
5293{
5294 struct lpfc_sli *psli = &phba->sli;
5295 unsigned long iflags;
5296 int rc;
5297
8fa38513
JS
5298 rc = lpfc_mbox_dev_check(phba);
5299 if (unlikely(rc)) {
5300 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5301 "(%d):2544 Mailbox command x%x (x%x) "
5302 "cannot issue Data: x%x x%x\n",
5303 mboxq->vport ? mboxq->vport->vpi : 0,
5304 mboxq->u.mb.mbxCommand,
5305 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5306 psli->sli_flag, flag);
5307 goto out_not_finished;
5308 }
5309
da0436e9
JS
5310 /* Detect polling mode and jump to a handler */
5311 if (!phba->sli4_hba.intr_enable) {
5312 if (flag == MBX_POLL)
5313 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5314 else
5315 rc = -EIO;
5316 if (rc != MBX_SUCCESS)
5317 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5318 "(%d):2541 Mailbox command x%x "
5319 "(x%x) cannot issue Data: x%x x%x\n",
5320 mboxq->vport ? mboxq->vport->vpi : 0,
5321 mboxq->u.mb.mbxCommand,
5322 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5323 psli->sli_flag, flag);
5324 return rc;
5325 } else if (flag == MBX_POLL) {
f1126688
JS
5326 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
5327 "(%d):2542 Try to issue mailbox command "
5328 "x%x (x%x) synchronously ahead of async"
5329 "mailbox command queue: x%x x%x\n",
da0436e9
JS
5330 mboxq->vport ? mboxq->vport->vpi : 0,
5331 mboxq->u.mb.mbxCommand,
5332 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5333 psli->sli_flag, flag);
f1126688
JS
5334 /* Try to block the asynchronous mailbox posting */
5335 rc = lpfc_sli4_async_mbox_block(phba);
5336 if (!rc) {
5337 /* Successfully blocked, now issue sync mbox cmd */
5338 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
5339 if (rc != MBX_SUCCESS)
5340 lpfc_printf_log(phba, KERN_ERR,
5341 LOG_MBOX | LOG_SLI,
5342 "(%d):2597 Mailbox command "
5343 "x%x (x%x) cannot issue "
5344 "Data: x%x x%x\n",
5345 mboxq->vport ?
5346 mboxq->vport->vpi : 0,
5347 mboxq->u.mb.mbxCommand,
5348 lpfc_sli4_mbox_opcode_get(phba,
5349 mboxq),
5350 psli->sli_flag, flag);
5351 /* Unblock the async mailbox posting afterward */
5352 lpfc_sli4_async_mbox_unblock(phba);
5353 }
5354 return rc;
da0436e9
JS
5355 }
5356
5357 /* Now, interrupt mode asynchrous mailbox command */
5358 rc = lpfc_mbox_cmd_check(phba, mboxq);
5359 if (rc) {
5360 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5361 "(%d):2543 Mailbox command x%x (x%x) "
5362 "cannot issue Data: x%x x%x\n",
5363 mboxq->vport ? mboxq->vport->vpi : 0,
5364 mboxq->u.mb.mbxCommand,
5365 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5366 psli->sli_flag, flag);
5367 goto out_not_finished;
5368 }
da0436e9
JS
5369
5370 /* Put the mailbox command to the driver internal FIFO */
5371 psli->slistat.mbox_busy++;
5372 spin_lock_irqsave(&phba->hbalock, iflags);
5373 lpfc_mbox_put(phba, mboxq);
5374 spin_unlock_irqrestore(&phba->hbalock, iflags);
5375 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5376 "(%d):0354 Mbox cmd issue - Enqueue Data: "
5377 "x%x (x%x) x%x x%x x%x\n",
5378 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
5379 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5380 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5381 phba->pport->port_state,
5382 psli->sli_flag, MBX_NOWAIT);
5383 /* Wake up worker thread to transport mailbox command from head */
5384 lpfc_worker_wake_up(phba);
5385
5386 return MBX_BUSY;
5387
5388out_not_finished:
5389 return MBX_NOT_FINISHED;
5390}
5391
5392/**
5393 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
5394 * @phba: Pointer to HBA context object.
5395 *
5396 * This function is called by worker thread to send a mailbox command to
5397 * SLI4 HBA firmware.
5398 *
5399 **/
5400int
5401lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
5402{
5403 struct lpfc_sli *psli = &phba->sli;
5404 LPFC_MBOXQ_t *mboxq;
5405 int rc = MBX_SUCCESS;
5406 unsigned long iflags;
5407 struct lpfc_mqe *mqe;
5408 uint32_t mbx_cmnd;
5409
5410 /* Check interrupt mode before post async mailbox command */
5411 if (unlikely(!phba->sli4_hba.intr_enable))
5412 return MBX_NOT_FINISHED;
5413
5414 /* Check for mailbox command service token */
5415 spin_lock_irqsave(&phba->hbalock, iflags);
5416 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
5417 spin_unlock_irqrestore(&phba->hbalock, iflags);
5418 return MBX_NOT_FINISHED;
5419 }
5420 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
5421 spin_unlock_irqrestore(&phba->hbalock, iflags);
5422 return MBX_NOT_FINISHED;
5423 }
5424 if (unlikely(phba->sli.mbox_active)) {
5425 spin_unlock_irqrestore(&phba->hbalock, iflags);
5426 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5427 "0384 There is pending active mailbox cmd\n");
5428 return MBX_NOT_FINISHED;
5429 }
5430 /* Take the mailbox command service token */
5431 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
5432
5433 /* Get the next mailbox command from head of queue */
5434 mboxq = lpfc_mbox_get(phba);
5435
5436 /* If no more mailbox command waiting for post, we're done */
5437 if (!mboxq) {
5438 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5439 spin_unlock_irqrestore(&phba->hbalock, iflags);
5440 return MBX_SUCCESS;
5441 }
5442 phba->sli.mbox_active = mboxq;
5443 spin_unlock_irqrestore(&phba->hbalock, iflags);
5444
5445 /* Check device readiness for posting mailbox command */
5446 rc = lpfc_mbox_dev_check(phba);
5447 if (unlikely(rc))
5448 /* Driver clean routine will clean up pending mailbox */
5449 goto out_not_finished;
5450
5451 /* Prepare the mbox command to be posted */
5452 mqe = &mboxq->u.mqe;
5453 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
5454
5455 /* Start timer for the mbox_tmo and log some mailbox post messages */
5456 mod_timer(&psli->mbox_tmo, (jiffies +
5457 (HZ * lpfc_mbox_tmo_val(phba, mbx_cmnd))));
5458
5459 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
5460 "(%d):0355 Mailbox cmd x%x (x%x) issue Data: "
5461 "x%x x%x\n",
5462 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
5463 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5464 phba->pport->port_state, psli->sli_flag);
5465
5466 if (mbx_cmnd != MBX_HEARTBEAT) {
5467 if (mboxq->vport) {
5468 lpfc_debugfs_disc_trc(mboxq->vport,
5469 LPFC_DISC_TRC_MBOX_VPORT,
5470 "MBOX Send vport: cmd:x%x mb:x%x x%x",
5471 mbx_cmnd, mqe->un.mb_words[0],
5472 mqe->un.mb_words[1]);
5473 } else {
5474 lpfc_debugfs_disc_trc(phba->pport,
5475 LPFC_DISC_TRC_MBOX,
5476 "MBOX Send: cmd:x%x mb:x%x x%x",
5477 mbx_cmnd, mqe->un.mb_words[0],
5478 mqe->un.mb_words[1]);
5479 }
5480 }
5481 psli->slistat.mbox_cmd++;
5482
5483 /* Post the mailbox command to the port */
5484 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
5485 if (rc != MBX_SUCCESS) {
5486 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5487 "(%d):2533 Mailbox command x%x (x%x) "
5488 "cannot issue Data: x%x x%x\n",
5489 mboxq->vport ? mboxq->vport->vpi : 0,
5490 mboxq->u.mb.mbxCommand,
5491 lpfc_sli4_mbox_opcode_get(phba, mboxq),
5492 psli->sli_flag, MBX_NOWAIT);
5493 goto out_not_finished;
5494 }
5495
5496 return rc;
5497
5498out_not_finished:
5499 spin_lock_irqsave(&phba->hbalock, iflags);
5500 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
5501 __lpfc_mbox_cmpl_put(phba, mboxq);
5502 /* Release the token */
5503 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
5504 phba->sli.mbox_active = NULL;
5505 spin_unlock_irqrestore(&phba->hbalock, iflags);
5506
5507 return MBX_NOT_FINISHED;
5508}
5509
5510/**
5511 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
5512 * @phba: Pointer to HBA context object.
5513 * @pmbox: Pointer to mailbox object.
5514 * @flag: Flag indicating how the mailbox need to be processed.
5515 *
5516 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
5517 * the API jump table function pointer from the lpfc_hba struct.
5518 *
5519 * Return codes the caller owns the mailbox command after the return of the
5520 * function.
5521 **/
5522int
5523lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
5524{
5525 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
5526}
5527
5528/**
5529 * lpfc_mbox_api_table_setup - Set up mbox api fucntion jump table
5530 * @phba: The hba struct for which this call is being executed.
5531 * @dev_grp: The HBA PCI-Device group number.
5532 *
5533 * This routine sets up the mbox interface API function jump table in @phba
5534 * struct.
5535 * Returns: 0 - success, -ENODEV - failure.
5536 **/
5537int
5538lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
5539{
5540
5541 switch (dev_grp) {
5542 case LPFC_PCI_DEV_LP:
5543 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
5544 phba->lpfc_sli_handle_slow_ring_event =
5545 lpfc_sli_handle_slow_ring_event_s3;
5546 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
5547 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
5548 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
5549 break;
5550 case LPFC_PCI_DEV_OC:
5551 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
5552 phba->lpfc_sli_handle_slow_ring_event =
5553 lpfc_sli_handle_slow_ring_event_s4;
5554 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
5555 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
5556 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
5557 break;
5558 default:
5559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5560 "1420 Invalid HBA PCI-device group: 0x%x\n",
5561 dev_grp);
5562 return -ENODEV;
5563 break;
5564 }
5565 return 0;
5566}
5567
e59058c4 5568/**
3621a710 5569 * __lpfc_sli_ringtx_put - Add an iocb to the txq
e59058c4
JS
5570 * @phba: Pointer to HBA context object.
5571 * @pring: Pointer to driver SLI ring object.
5572 * @piocb: Pointer to address of newly added command iocb.
5573 *
5574 * This function is called with hbalock held to add a command
5575 * iocb to the txq when SLI layer cannot submit the command iocb
5576 * to the ring.
5577 **/
858c9f6c 5578static void
92d7f7b0 5579__lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5580 struct lpfc_iocbq *piocb)
dea3101e 5581{
5582 /* Insert the caller's iocb in the txq tail for later processing. */
5583 list_add_tail(&piocb->list, &pring->txq);
5584 pring->txq_cnt++;
dea3101e 5585}
5586
e59058c4 5587/**
3621a710 5588 * lpfc_sli_next_iocb - Get the next iocb in the txq
e59058c4
JS
5589 * @phba: Pointer to HBA context object.
5590 * @pring: Pointer to driver SLI ring object.
5591 * @piocb: Pointer to address of newly added command iocb.
5592 *
5593 * This function is called with hbalock held before a new
5594 * iocb is submitted to the firmware. This function checks
5595 * txq to flush the iocbs in txq to Firmware before
5596 * submitting new iocbs to the Firmware.
5597 * If there are iocbs in the txq which need to be submitted
5598 * to firmware, lpfc_sli_next_iocb returns the first element
5599 * of the txq after dequeuing it from txq.
5600 * If there is no iocb in the txq then the function will return
5601 * *piocb and *piocb is set to NULL. Caller needs to check
5602 * *piocb to find if there are more commands in the txq.
5603 **/
dea3101e 5604static struct lpfc_iocbq *
5605lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2e0fef85 5606 struct lpfc_iocbq **piocb)
dea3101e 5607{
5608 struct lpfc_iocbq * nextiocb;
5609
5610 nextiocb = lpfc_sli_ringtx_get(phba, pring);
5611 if (!nextiocb) {
5612 nextiocb = *piocb;
5613 *piocb = NULL;
5614 }
5615
5616 return nextiocb;
5617}
5618
e59058c4 5619/**
3772a991 5620 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
e59058c4 5621 * @phba: Pointer to HBA context object.
3772a991 5622 * @ring_number: SLI ring number to issue iocb on.
e59058c4
JS
5623 * @piocb: Pointer to command iocb.
5624 * @flag: Flag indicating if this command can be put into txq.
5625 *
3772a991
JS
5626 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
5627 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
5628 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
5629 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
5630 * this function allows only iocbs for posting buffers. This function finds
5631 * next available slot in the command ring and posts the command to the
5632 * available slot and writes the port attention register to request HBA start
5633 * processing new iocb. If there is no slot available in the ring and
5634 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
5635 * the function returns IOCB_BUSY.
e59058c4 5636 *
3772a991
JS
5637 * This function is called with hbalock held. The function will return success
5638 * after it successfully submit the iocb to firmware or after adding to the
5639 * txq.
e59058c4 5640 **/
98c9ea5c 5641static int
3772a991 5642__lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
dea3101e 5643 struct lpfc_iocbq *piocb, uint32_t flag)
5644{
5645 struct lpfc_iocbq *nextiocb;
5646 IOCB_t *iocb;
3772a991 5647 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
dea3101e 5648
92d7f7b0
JS
5649 if (piocb->iocb_cmpl && (!piocb->vport) &&
5650 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
5651 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
5652 lpfc_printf_log(phba, KERN_ERR,
5653 LOG_SLI | LOG_VPORT,
e8b62011 5654 "1807 IOCB x%x failed. No vport\n",
92d7f7b0
JS
5655 piocb->iocb.ulpCommand);
5656 dump_stack();
5657 return IOCB_ERROR;
5658 }
5659
5660
8d63f375
LV
5661 /* If the PCI channel is in offline state, do not post iocbs. */
5662 if (unlikely(pci_channel_offline(phba->pcidev)))
5663 return IOCB_ERROR;
5664
a257bf90
JS
5665 /* If HBA has a deferred error attention, fail the iocb. */
5666 if (unlikely(phba->hba_flag & DEFER_ERATT))
5667 return IOCB_ERROR;
5668
dea3101e 5669 /*
5670 * We should never get an IOCB if we are in a < LINK_DOWN state
5671 */
2e0fef85 5672 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
dea3101e 5673 return IOCB_ERROR;
5674
5675 /*
5676 * Check to see if we are blocking IOCB processing because of a
0b727fea 5677 * outstanding event.
dea3101e 5678 */
0b727fea 5679 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
dea3101e 5680 goto iocb_busy;
5681
2e0fef85 5682 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
dea3101e 5683 /*
2680eeaa 5684 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
dea3101e 5685 * can be issued if the link is not up.
5686 */
5687 switch (piocb->iocb.ulpCommand) {
84774a4d
JS
5688 case CMD_GEN_REQUEST64_CR:
5689 case CMD_GEN_REQUEST64_CX:
5690 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
5691 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
6a9c52cf 5692 FC_RCTL_DD_UNSOL_CMD) ||
84774a4d
JS
5693 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
5694 MENLO_TRANSPORT_TYPE))
5695
5696 goto iocb_busy;
5697 break;
dea3101e 5698 case CMD_QUE_RING_BUF_CN:
5699 case CMD_QUE_RING_BUF64_CN:
dea3101e 5700 /*
5701 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
5702 * completion, iocb_cmpl MUST be 0.
5703 */
5704 if (piocb->iocb_cmpl)
5705 piocb->iocb_cmpl = NULL;
5706 /*FALLTHROUGH*/
5707 case CMD_CREATE_XRI_CR:
2680eeaa
JS
5708 case CMD_CLOSE_XRI_CN:
5709 case CMD_CLOSE_XRI_CX:
dea3101e 5710 break;
5711 default:
5712 goto iocb_busy;
5713 }
5714
5715 /*
5716 * For FCP commands, we must be in a state where we can process link
5717 * attention events.
5718 */
5719 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
92d7f7b0 5720 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
dea3101e 5721 goto iocb_busy;
92d7f7b0 5722 }
dea3101e 5723
dea3101e 5724 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
5725 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
5726 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
5727
5728 if (iocb)
5729 lpfc_sli_update_ring(phba, pring);
5730 else
5731 lpfc_sli_update_full_ring(phba, pring);
5732
5733 if (!piocb)
5734 return IOCB_SUCCESS;
5735
5736 goto out_busy;
5737
5738 iocb_busy:
5739 pring->stats.iocb_cmd_delay++;
5740
5741 out_busy:
5742
5743 if (!(flag & SLI_IOCB_RET_IOCB)) {
92d7f7b0 5744 __lpfc_sli_ringtx_put(phba, pring, piocb);
dea3101e 5745 return IOCB_SUCCESS;
5746 }
5747
5748 return IOCB_BUSY;
5749}
5750
3772a991 5751/**
4f774513
JS
5752 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
5753 * @phba: Pointer to HBA context object.
5754 * @piocb: Pointer to command iocb.
5755 * @sglq: Pointer to the scatter gather queue object.
5756 *
5757 * This routine converts the bpl or bde that is in the IOCB
5758 * to a sgl list for the sli4 hardware. The physical address
5759 * of the bpl/bde is converted back to a virtual address.
5760 * If the IOCB contains a BPL then the list of BDE's is
5761 * converted to sli4_sge's. If the IOCB contains a single
5762 * BDE then it is converted to a single sli_sge.
5763 * The IOCB is still in cpu endianess so the contents of
5764 * the bpl can be used without byte swapping.
5765 *
5766 * Returns valid XRI = Success, NO_XRI = Failure.
5767**/
5768static uint16_t
5769lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
5770 struct lpfc_sglq *sglq)
3772a991 5771{
4f774513
JS
5772 uint16_t xritag = NO_XRI;
5773 struct ulp_bde64 *bpl = NULL;
5774 struct ulp_bde64 bde;
5775 struct sli4_sge *sgl = NULL;
5776 IOCB_t *icmd;
5777 int numBdes = 0;
5778 int i = 0;
3772a991 5779
4f774513
JS
5780 if (!piocbq || !sglq)
5781 return xritag;
5782
5783 sgl = (struct sli4_sge *)sglq->sgl;
5784 icmd = &piocbq->iocb;
5785 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5786 numBdes = icmd->un.genreq64.bdl.bdeSize /
5787 sizeof(struct ulp_bde64);
5788 /* The addrHigh and addrLow fields within the IOCB
5789 * have not been byteswapped yet so there is no
5790 * need to swap them back.
5791 */
5792 bpl = (struct ulp_bde64 *)
5793 ((struct lpfc_dmabuf *)piocbq->context3)->virt;
5794
5795 if (!bpl)
5796 return xritag;
5797
5798 for (i = 0; i < numBdes; i++) {
5799 /* Should already be byte swapped. */
5800 sgl->addr_hi = bpl->addrHigh;
5801 sgl->addr_lo = bpl->addrLow;
5802 /* swap the size field back to the cpu so we
5803 * can assign it to the sgl.
5804 */
5805 bde.tus.w = le32_to_cpu(bpl->tus.w);
5806 bf_set(lpfc_sli4_sge_len, sgl, bde.tus.f.bdeSize);
5807 if ((i+1) == numBdes)
5808 bf_set(lpfc_sli4_sge_last, sgl, 1);
5809 else
5810 bf_set(lpfc_sli4_sge_last, sgl, 0);
5811 sgl->word2 = cpu_to_le32(sgl->word2);
5812 sgl->word3 = cpu_to_le32(sgl->word3);
5813 bpl++;
5814 sgl++;
5815 }
5816 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
5817 /* The addrHigh and addrLow fields of the BDE have not
5818 * been byteswapped yet so they need to be swapped
5819 * before putting them in the sgl.
5820 */
5821 sgl->addr_hi =
5822 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
5823 sgl->addr_lo =
5824 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
5825 bf_set(lpfc_sli4_sge_len, sgl,
5826 icmd->un.genreq64.bdl.bdeSize);
5827 bf_set(lpfc_sli4_sge_last, sgl, 1);
5828 sgl->word2 = cpu_to_le32(sgl->word2);
5829 sgl->word3 = cpu_to_le32(sgl->word3);
5830 }
5831 return sglq->sli4_xritag;
3772a991 5832}
92d7f7b0 5833
e59058c4 5834/**
4f774513 5835 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
e59058c4 5836 * @phba: Pointer to HBA context object.
e59058c4 5837 *
4f774513 5838 * This routine performs a round robin SCSI command to SLI4 FCP WQ index
8fa38513
JS
5839 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
5840 * held.
4f774513
JS
5841 *
5842 * Return: index into SLI4 fast-path FCP queue index.
e59058c4 5843 **/
4f774513 5844static uint32_t
8fa38513 5845lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
92d7f7b0 5846{
8fa38513
JS
5847 ++phba->fcp_qidx;
5848 if (phba->fcp_qidx >= phba->cfg_fcp_wq_count)
5849 phba->fcp_qidx = 0;
92d7f7b0 5850
8fa38513 5851 return phba->fcp_qidx;
92d7f7b0
JS
5852}
5853
e59058c4 5854/**
4f774513 5855 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
e59058c4 5856 * @phba: Pointer to HBA context object.
4f774513
JS
5857 * @piocb: Pointer to command iocb.
5858 * @wqe: Pointer to the work queue entry.
e59058c4 5859 *
4f774513
JS
5860 * This routine converts the iocb command to its Work Queue Entry
5861 * equivalent. The wqe pointer should not have any fields set when
5862 * this routine is called because it will memcpy over them.
5863 * This routine does not set the CQ_ID or the WQEC bits in the
5864 * wqe.
e59058c4 5865 *
4f774513 5866 * Returns: 0 = Success, IOCB_ERROR = Failure.
e59058c4 5867 **/
cf5bf97e 5868static int
4f774513
JS
5869lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
5870 union lpfc_wqe *wqe)
cf5bf97e 5871{
4f774513
JS
5872 uint32_t payload_len = 0;
5873 uint8_t ct = 0;
5874 uint32_t fip;
5875 uint32_t abort_tag;
5876 uint8_t command_type = ELS_COMMAND_NON_FIP;
5877 uint8_t cmnd;
5878 uint16_t xritag;
5879 struct ulp_bde64 *bpl = NULL;
5880
5881 fip = bf_get(lpfc_fip_flag, &phba->sli4_hba.sli4_flags);
5882 /* The fcp commands will set command type */
0c287589 5883 if (iocbq->iocb_flag & LPFC_IO_FCP)
4f774513 5884 command_type = FCP_COMMAND;
0c287589
JS
5885 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS))
5886 command_type = ELS_COMMAND_FIP;
5887 else
5888 command_type = ELS_COMMAND_NON_FIP;
5889
4f774513
JS
5890 /* Some of the fields are in the right position already */
5891 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
5892 abort_tag = (uint32_t) iocbq->iotag;
5893 xritag = iocbq->sli4_xritag;
5894 wqe->words[7] = 0; /* The ct field has moved so reset */
5895 /* words0-2 bpl convert bde */
5896 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
5897 bpl = (struct ulp_bde64 *)
5898 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
5899 if (!bpl)
5900 return IOCB_ERROR;
cf5bf97e 5901
4f774513
JS
5902 /* Should already be byte swapped. */
5903 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
5904 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
5905 /* swap the size field back to the cpu so we
5906 * can assign it to the sgl.
5907 */
5908 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
5909 payload_len = wqe->generic.bde.tus.f.bdeSize;
5910 } else
5911 payload_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
cf5bf97e 5912
4f774513
JS
5913 iocbq->iocb.ulpIoTag = iocbq->iotag;
5914 cmnd = iocbq->iocb.ulpCommand;
a4bc3379 5915
4f774513
JS
5916 switch (iocbq->iocb.ulpCommand) {
5917 case CMD_ELS_REQUEST64_CR:
5918 if (!iocbq->iocb.ulpLe) {
5919 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5920 "2007 Only Limited Edition cmd Format"
5921 " supported 0x%x\n",
5922 iocbq->iocb.ulpCommand);
5923 return IOCB_ERROR;
5924 }
5925 wqe->els_req.payload_len = payload_len;
5926 /* Els_reguest64 has a TMO */
5927 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
5928 iocbq->iocb.ulpTimeout);
5929 /* Need a VF for word 4 set the vf bit*/
5930 bf_set(els_req64_vf, &wqe->els_req, 0);
5931 /* And a VFID for word 12 */
5932 bf_set(els_req64_vfid, &wqe->els_req, 0);
5933 /*
5934 * Set ct field to 3, indicates that the context_tag field
5935 * contains the FCFI and remote N_Port_ID is
5936 * in word 5.
5937 */
5938
5939 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
5940 bf_set(lpfc_wqe_gen_context, &wqe->generic,
5941 iocbq->iocb.ulpContext);
5942
4f774513
JS
5943 bf_set(lpfc_wqe_gen_ct, &wqe->generic, ct);
5944 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5945 /* CCP CCPE PV PRI in word10 were set in the memcpy */
5946 break;
5947 case CMD_XMIT_SEQUENCE64_CR:
5948 /* word3 iocb=io_tag32 wqe=payload_offset */
5949 /* payload offset used for multilpe outstanding
5950 * sequences on the same exchange
5951 */
5952 wqe->words[3] = 0;
5953 /* word4 relative_offset memcpy */
5954 /* word5 r_ctl/df_ctl memcpy */
5955 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
5956 wqe->xmit_sequence.xmit_len = payload_len;
5957 break;
5958 case CMD_XMIT_BCAST64_CN:
5959 /* word3 iocb=iotag32 wqe=payload_len */
5960 wqe->words[3] = 0; /* no definition for this in wqe */
5961 /* word4 iocb=rsvd wqe=rsvd */
5962 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
5963 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
5964 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
5965 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
5966 break;
5967 case CMD_FCP_IWRITE64_CR:
5968 command_type = FCP_COMMAND_DATA_OUT;
5969 /* The struct for wqe fcp_iwrite has 3 fields that are somewhat
5970 * confusing.
5971 * word3 is payload_len: byte offset to the sgl entry for the
5972 * fcp_command.
5973 * word4 is total xfer len, same as the IOCB->ulpParameter.
5974 * word5 is initial xfer len 0 = wait for xfer-ready
5975 */
5976
5977 /* Always wait for xfer-ready before sending data */
5978 wqe->fcp_iwrite.initial_xfer_len = 0;
5979 /* word 4 (xfer length) should have been set on the memcpy */
5980
5981 /* allow write to fall through to read */
5982 case CMD_FCP_IREAD64_CR:
5983 /* FCP_CMD is always the 1st sgl entry */
5984 wqe->fcp_iread.payload_len =
5985 payload_len + sizeof(struct fcp_rsp);
5986
5987 /* word 4 (xfer length) should have been set on the memcpy */
5988
5989 bf_set(lpfc_wqe_gen_erp, &wqe->generic,
5990 iocbq->iocb.ulpFCP2Rcvy);
5991 bf_set(lpfc_wqe_gen_lnk, &wqe->generic, iocbq->iocb.ulpXS);
5992 /* The XC bit and the XS bit are similar. The driver never
5993 * tracked whether or not the exchange was previouslly open.
5994 * XC = Exchange create, 0 is create. 1 is already open.
5995 * XS = link cmd: 1 do not close the exchange after command.
5996 * XS = 0 close exchange when command completes.
5997 * The only time we would not set the XC bit is when the XS bit
5998 * is set and we are sending our 2nd or greater command on
5999 * this exchange.
6000 */
f1126688
JS
6001 /* Always open the exchange */
6002 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
4f774513 6003
f1126688
JS
6004 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
6005 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6006 break;
4f774513
JS
6007 case CMD_FCP_ICMND64_CR:
6008 /* Always open the exchange */
6009 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
6010
f1126688 6011 wqe->words[4] = 0;
4f774513 6012 wqe->words[10] &= 0xffff0000; /* zero out ebde count */
f1126688 6013 bf_set(lpfc_wqe_gen_pu, &wqe->generic, 0);
4f774513
JS
6014 break;
6015 case CMD_GEN_REQUEST64_CR:
6016 /* word3 command length is described as byte offset to the
6017 * rsp_data. Would always be 16, sizeof(struct sli4_sge)
6018 * sgl[0] = cmnd
6019 * sgl[1] = rsp.
6020 *
6021 */
6022 wqe->gen_req.command_len = payload_len;
6023 /* Word4 parameter copied in the memcpy */
6024 /* Word5 [rctl, type, df_ctl, la] copied in memcpy */
6025 /* word6 context tag copied in memcpy */
6026 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
6027 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
6028 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6029 "2015 Invalid CT %x command 0x%x\n",
6030 ct, iocbq->iocb.ulpCommand);
6031 return IOCB_ERROR;
6032 }
6033 bf_set(lpfc_wqe_gen_ct, &wqe->generic, 0);
6034 bf_set(wqe_tmo, &wqe->gen_req.wqe_com,
6035 iocbq->iocb.ulpTimeout);
6036
6037 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6038 command_type = OTHER_COMMAND;
6039 break;
6040 case CMD_XMIT_ELS_RSP64_CX:
6041 /* words0-2 BDE memcpy */
6042 /* word3 iocb=iotag32 wqe=rsvd */
6043 wqe->words[3] = 0;
6044 /* word4 iocb=did wge=rsvd. */
6045 wqe->words[4] = 0;
6046 /* word5 iocb=rsvd wge=did */
6047 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
6048 iocbq->iocb.un.elsreq64.remoteID);
6049
6050 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6051 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6052
6053 bf_set(lpfc_wqe_gen_pu, &wqe->generic, iocbq->iocb.ulpPU);
6054 bf_set(wqe_rcvoxid, &wqe->generic, iocbq->iocb.ulpContext);
6055 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
6056 bf_set(lpfc_wqe_gen_context, &wqe->generic,
6057 iocbq->vport->vpi + phba->vpi_base);
6058 command_type = OTHER_COMMAND;
6059 break;
6060 case CMD_CLOSE_XRI_CN:
6061 case CMD_ABORT_XRI_CN:
6062 case CMD_ABORT_XRI_CX:
6063 /* words 0-2 memcpy should be 0 rserved */
6064 /* port will send abts */
6065 if (iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
6066 /*
6067 * The link is down so the fw does not need to send abts
6068 * on the wire.
6069 */
6070 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
6071 else
6072 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
6073 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
6074 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6075 wqe->words[5] = 0;
6076 bf_set(lpfc_wqe_gen_ct, &wqe->generic,
6077 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
6078 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
6079 wqe->generic.abort_tag = abort_tag;
6080 /*
6081 * The abort handler will send us CMD_ABORT_XRI_CN or
6082 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
6083 */
6084 bf_set(lpfc_wqe_gen_command, &wqe->generic, CMD_ABORT_XRI_CX);
6085 cmnd = CMD_ABORT_XRI_CX;
6086 command_type = OTHER_COMMAND;
6087 xritag = 0;
6088 break;
6669f9bb
JS
6089 case CMD_XMIT_BLS_RSP64_CX:
6090 /* As BLS ABTS-ACC WQE is very different from other WQEs,
6091 * we re-construct this WQE here based on information in
6092 * iocbq from scratch.
6093 */
6094 memset(wqe, 0, sizeof(union lpfc_wqe));
6095 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
6096 iocbq->iocb.un.ulpWord[3]);
6097 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
6098 iocbq->sli4_xritag);
6099 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
6100 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
6101 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
6102 iocbq->iocb.ulpContext);
6103 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
6104 command_type = OTHER_COMMAND;
6105 break;
4f774513
JS
6106 case CMD_XRI_ABORTED_CX:
6107 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
6108 /* words0-2 are all 0's no bde */
6109 /* word3 and word4 are rsvrd */
6110 wqe->words[3] = 0;
6111 wqe->words[4] = 0;
6112 /* word5 iocb=rsvd wge=did */
6113 /* There is no remote port id in the IOCB? */
6114 /* Let this fall through and fail */
6115 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
6116 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
6117 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
6118 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
6119 default:
6120 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6121 "2014 Invalid command 0x%x\n",
6122 iocbq->iocb.ulpCommand);
6123 return IOCB_ERROR;
6124 break;
6125
6126 }
6127 bf_set(lpfc_wqe_gen_xri, &wqe->generic, xritag);
6128 bf_set(lpfc_wqe_gen_request_tag, &wqe->generic, iocbq->iotag);
6129 wqe->generic.abort_tag = abort_tag;
6130 bf_set(lpfc_wqe_gen_cmd_type, &wqe->generic, command_type);
6131 bf_set(lpfc_wqe_gen_command, &wqe->generic, cmnd);
6132 bf_set(lpfc_wqe_gen_class, &wqe->generic, iocbq->iocb.ulpClass);
6133 bf_set(lpfc_wqe_gen_cq_id, &wqe->generic, LPFC_WQE_CQ_ID_DEFAULT);
6134
6135 return 0;
6136}
6137
6138/**
6139 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
6140 * @phba: Pointer to HBA context object.
6141 * @ring_number: SLI ring number to issue iocb on.
6142 * @piocb: Pointer to command iocb.
6143 * @flag: Flag indicating if this command can be put into txq.
6144 *
6145 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
6146 * an iocb command to an HBA with SLI-4 interface spec.
6147 *
6148 * This function is called with hbalock held. The function will return success
6149 * after it successfully submit the iocb to firmware or after adding to the
6150 * txq.
6151 **/
6152static int
6153__lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
6154 struct lpfc_iocbq *piocb, uint32_t flag)
6155{
6156 struct lpfc_sglq *sglq;
6157 uint16_t xritag;
6158 union lpfc_wqe wqe;
6159 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
6160 uint32_t fcp_wqidx;
6161
6162 if (piocb->sli4_xritag == NO_XRI) {
6163 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
6669f9bb 6164 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
4f774513
JS
6165 sglq = NULL;
6166 else {
6167 sglq = __lpfc_sli_get_sglq(phba);
6168 if (!sglq)
6169 return IOCB_ERROR;
6170 piocb->sli4_xritag = sglq->sli4_xritag;
6171 }
6172 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
6173 sglq = NULL; /* These IO's already have an XRI and
6174 * a mapped sgl.
6175 */
6176 } else {
6177 /* This is a continuation of a commandi,(CX) so this
6178 * sglq is on the active list
6179 */
6180 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_xritag);
6181 if (!sglq)
6182 return IOCB_ERROR;
6183 }
6184
6185 if (sglq) {
6186 xritag = lpfc_sli4_bpl2sgl(phba, piocb, sglq);
6187 if (xritag != sglq->sli4_xritag)
6188 return IOCB_ERROR;
6189 }
6190
6191 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
6192 return IOCB_ERROR;
6193
6194 if (piocb->iocb_flag & LPFC_IO_FCP) {
8fa38513 6195 fcp_wqidx = lpfc_sli4_scmd_to_wqidx_distr(phba);
4f774513
JS
6196 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[fcp_wqidx], &wqe))
6197 return IOCB_ERROR;
6198 } else {
6199 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
6200 return IOCB_ERROR;
6201 }
6202 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
6203
6204 return 0;
6205}
6206
6207/**
6208 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
6209 *
6210 * This routine wraps the actual lockless version for issusing IOCB function
6211 * pointer from the lpfc_hba struct.
6212 *
6213 * Return codes:
6214 * IOCB_ERROR - Error
6215 * IOCB_SUCCESS - Success
6216 * IOCB_BUSY - Busy
6217 **/
6218static inline int
6219__lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6220 struct lpfc_iocbq *piocb, uint32_t flag)
6221{
6222 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6223}
6224
6225/**
6226 * lpfc_sli_api_table_setup - Set up sli api fucntion jump table
6227 * @phba: The hba struct for which this call is being executed.
6228 * @dev_grp: The HBA PCI-Device group number.
6229 *
6230 * This routine sets up the SLI interface API function jump table in @phba
6231 * struct.
6232 * Returns: 0 - success, -ENODEV - failure.
6233 **/
6234int
6235lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
6236{
6237
6238 switch (dev_grp) {
6239 case LPFC_PCI_DEV_LP:
6240 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
6241 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
6242 break;
6243 case LPFC_PCI_DEV_OC:
6244 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
6245 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
6246 break;
6247 default:
6248 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6249 "1419 Invalid HBA PCI-device group: 0x%x\n",
6250 dev_grp);
6251 return -ENODEV;
6252 break;
6253 }
6254 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
6255 return 0;
6256}
6257
6258/**
6259 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
6260 * @phba: Pointer to HBA context object.
6261 * @pring: Pointer to driver SLI ring object.
6262 * @piocb: Pointer to command iocb.
6263 * @flag: Flag indicating if this command can be put into txq.
6264 *
6265 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
6266 * function. This function gets the hbalock and calls
6267 * __lpfc_sli_issue_iocb function and will return the error returned
6268 * by __lpfc_sli_issue_iocb function. This wrapper is used by
6269 * functions which do not hold hbalock.
6270 **/
6271int
6272lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
6273 struct lpfc_iocbq *piocb, uint32_t flag)
6274{
6275 unsigned long iflags;
6276 int rc;
6277
6278 spin_lock_irqsave(&phba->hbalock, iflags);
6279 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
6280 spin_unlock_irqrestore(&phba->hbalock, iflags);
6281
6282 return rc;
6283}
6284
6285/**
6286 * lpfc_extra_ring_setup - Extra ring setup function
6287 * @phba: Pointer to HBA context object.
6288 *
6289 * This function is called while driver attaches with the
6290 * HBA to setup the extra ring. The extra ring is used
6291 * only when driver needs to support target mode functionality
6292 * or IP over FC functionalities.
6293 *
6294 * This function is called with no lock held.
6295 **/
6296static int
6297lpfc_extra_ring_setup( struct lpfc_hba *phba)
6298{
6299 struct lpfc_sli *psli;
6300 struct lpfc_sli_ring *pring;
6301
6302 psli = &phba->sli;
6303
6304 /* Adjust cmd/rsp ring iocb entries more evenly */
6305
6306 /* Take some away from the FCP ring */
6307 pring = &psli->ring[psli->fcp_ring];
6308 pring->numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6309 pring->numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
cf5bf97e
JW
6310 pring->numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6311 pring->numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6312
a4bc3379
JS
6313 /* and give them to the extra ring */
6314 pring = &psli->ring[psli->extra_ring];
6315
cf5bf97e
JW
6316 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6317 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6318 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6319 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
6320
6321 /* Setup default profile for this ring */
6322 pring->iotag_max = 4096;
6323 pring->num_mask = 1;
6324 pring->prt[0].profile = 0; /* Mask 0 */
a4bc3379
JS
6325 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
6326 pring->prt[0].type = phba->cfg_multi_ring_type;
cf5bf97e
JW
6327 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
6328 return 0;
6329}
6330
e59058c4 6331/**
3621a710 6332 * lpfc_sli_async_event_handler - ASYNC iocb handler function
e59058c4
JS
6333 * @phba: Pointer to HBA context object.
6334 * @pring: Pointer to driver SLI ring object.
6335 * @iocbq: Pointer to iocb object.
6336 *
6337 * This function is called by the slow ring event handler
6338 * function when there is an ASYNC event iocb in the ring.
6339 * This function is called with no lock held.
6340 * Currently this function handles only temperature related
6341 * ASYNC events. The function decodes the temperature sensor
6342 * event message and posts events for the management applications.
6343 **/
98c9ea5c 6344static void
57127f15
JS
6345lpfc_sli_async_event_handler(struct lpfc_hba * phba,
6346 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
6347{
6348 IOCB_t *icmd;
6349 uint16_t evt_code;
6350 uint16_t temp;
6351 struct temp_event temp_event_data;
6352 struct Scsi_Host *shost;
a257bf90 6353 uint32_t *iocb_w;
57127f15
JS
6354
6355 icmd = &iocbq->iocb;
6356 evt_code = icmd->un.asyncstat.evt_code;
6357 temp = icmd->ulpContext;
6358
6359 if ((evt_code != ASYNC_TEMP_WARN) &&
6360 (evt_code != ASYNC_TEMP_SAFE)) {
a257bf90 6361 iocb_w = (uint32_t *) icmd;
57127f15
JS
6362 lpfc_printf_log(phba,
6363 KERN_ERR,
6364 LOG_SLI,
76bb24ef 6365 "0346 Ring %d handler: unexpected ASYNC_STATUS"
e4e74273 6366 " evt_code 0x%x\n"
a257bf90
JS
6367 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
6368 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
6369 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
6370 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
57127f15 6371 pring->ringno,
a257bf90
JS
6372 icmd->un.asyncstat.evt_code,
6373 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
6374 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
6375 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
6376 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
6377
57127f15
JS
6378 return;
6379 }
6380 temp_event_data.data = (uint32_t)temp;
6381 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
6382 if (evt_code == ASYNC_TEMP_WARN) {
6383 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
6384 lpfc_printf_log(phba,
09372820 6385 KERN_ERR,
57127f15 6386 LOG_TEMP,
76bb24ef 6387 "0347 Adapter is very hot, please take "
57127f15
JS
6388 "corrective action. temperature : %d Celsius\n",
6389 temp);
6390 }
6391 if (evt_code == ASYNC_TEMP_SAFE) {
6392 temp_event_data.event_code = LPFC_NORMAL_TEMP;
6393 lpfc_printf_log(phba,
09372820 6394 KERN_ERR,
57127f15
JS
6395 LOG_TEMP,
6396 "0340 Adapter temperature is OK now. "
6397 "temperature : %d Celsius\n",
6398 temp);
6399 }
6400
6401 /* Send temperature change event to applications */
6402 shost = lpfc_shost_from_vport(phba->pport);
6403 fc_host_post_vendor_event(shost, fc_get_event_number(),
6404 sizeof(temp_event_data), (char *) &temp_event_data,
ddcc50f0 6405 LPFC_NL_VENDOR_ID);
57127f15
JS
6406
6407}
6408
6409
e59058c4 6410/**
3621a710 6411 * lpfc_sli_setup - SLI ring setup function
e59058c4
JS
6412 * @phba: Pointer to HBA context object.
6413 *
6414 * lpfc_sli_setup sets up rings of the SLI interface with
6415 * number of iocbs per ring and iotags. This function is
6416 * called while driver attach to the HBA and before the
6417 * interrupts are enabled. So there is no need for locking.
6418 *
6419 * This function always returns 0.
6420 **/
dea3101e 6421int
6422lpfc_sli_setup(struct lpfc_hba *phba)
6423{
ed957684 6424 int i, totiocbsize = 0;
dea3101e 6425 struct lpfc_sli *psli = &phba->sli;
6426 struct lpfc_sli_ring *pring;
6427
6428 psli->num_rings = MAX_CONFIGURED_RINGS;
6429 psli->sli_flag = 0;
6430 psli->fcp_ring = LPFC_FCP_RING;
6431 psli->next_ring = LPFC_FCP_NEXT_RING;
a4bc3379 6432 psli->extra_ring = LPFC_EXTRA_RING;
dea3101e 6433
604a3e30
JB
6434 psli->iocbq_lookup = NULL;
6435 psli->iocbq_lookup_len = 0;
6436 psli->last_iotag = 0;
6437
dea3101e 6438 for (i = 0; i < psli->num_rings; i++) {
6439 pring = &psli->ring[i];
6440 switch (i) {
6441 case LPFC_FCP_RING: /* ring 0 - FCP */
6442 /* numCiocb and numRiocb are used in config_port */
6443 pring->numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
6444 pring->numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
6445 pring->numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
6446 pring->numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
6447 pring->numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
6448 pring->numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
ed957684 6449 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6450 SLI3_IOCB_CMD_SIZE :
6451 SLI2_IOCB_CMD_SIZE;
ed957684 6452 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6453 SLI3_IOCB_RSP_SIZE :
6454 SLI2_IOCB_RSP_SIZE;
dea3101e 6455 pring->iotag_ctr = 0;
6456 pring->iotag_max =
92d7f7b0 6457 (phba->cfg_hba_queue_depth * 2);
dea3101e 6458 pring->fast_iotag = pring->iotag_max;
6459 pring->num_mask = 0;
6460 break;
a4bc3379 6461 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
dea3101e 6462 /* numCiocb and numRiocb are used in config_port */
6463 pring->numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
6464 pring->numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
ed957684 6465 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6466 SLI3_IOCB_CMD_SIZE :
6467 SLI2_IOCB_CMD_SIZE;
ed957684 6468 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6469 SLI3_IOCB_RSP_SIZE :
6470 SLI2_IOCB_RSP_SIZE;
2e0fef85 6471 pring->iotag_max = phba->cfg_hba_queue_depth;
dea3101e 6472 pring->num_mask = 0;
6473 break;
6474 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
6475 /* numCiocb and numRiocb are used in config_port */
6476 pring->numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
6477 pring->numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
ed957684 6478 pring->sizeCiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6479 SLI3_IOCB_CMD_SIZE :
6480 SLI2_IOCB_CMD_SIZE;
ed957684 6481 pring->sizeRiocb = (phba->sli_rev == 3) ?
92d7f7b0
JS
6482 SLI3_IOCB_RSP_SIZE :
6483 SLI2_IOCB_RSP_SIZE;
dea3101e 6484 pring->fast_iotag = 0;
6485 pring->iotag_ctr = 0;
6486 pring->iotag_max = 4096;
57127f15
JS
6487 pring->lpfc_sli_rcv_async_status =
6488 lpfc_sli_async_event_handler;
6669f9bb 6489 pring->num_mask = LPFC_MAX_RING_MASK;
dea3101e 6490 pring->prt[0].profile = 0; /* Mask 0 */
6a9c52cf
JS
6491 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
6492 pring->prt[0].type = FC_TYPE_ELS;
dea3101e 6493 pring->prt[0].lpfc_sli_rcv_unsol_event =
92d7f7b0 6494 lpfc_els_unsol_event;
dea3101e 6495 pring->prt[1].profile = 0; /* Mask 1 */
6a9c52cf
JS
6496 pring->prt[1].rctl = FC_RCTL_ELS_REP;
6497 pring->prt[1].type = FC_TYPE_ELS;
dea3101e 6498 pring->prt[1].lpfc_sli_rcv_unsol_event =
92d7f7b0 6499 lpfc_els_unsol_event;
dea3101e 6500 pring->prt[2].profile = 0; /* Mask 2 */
6501 /* NameServer Inquiry */
6a9c52cf 6502 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
dea3101e 6503 /* NameServer */
6a9c52cf 6504 pring->prt[2].type = FC_TYPE_CT;
dea3101e 6505 pring->prt[2].lpfc_sli_rcv_unsol_event =
92d7f7b0 6506 lpfc_ct_unsol_event;
dea3101e 6507 pring->prt[3].profile = 0; /* Mask 3 */
6508 /* NameServer response */
6a9c52cf 6509 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
dea3101e 6510 /* NameServer */
6a9c52cf 6511 pring->prt[3].type = FC_TYPE_CT;
dea3101e 6512 pring->prt[3].lpfc_sli_rcv_unsol_event =
92d7f7b0 6513 lpfc_ct_unsol_event;
6669f9bb
JS
6514 /* abort unsolicited sequence */
6515 pring->prt[4].profile = 0; /* Mask 4 */
6516 pring->prt[4].rctl = FC_RCTL_BA_ABTS;
6517 pring->prt[4].type = FC_TYPE_BLS;
6518 pring->prt[4].lpfc_sli_rcv_unsol_event =
6519 lpfc_sli4_ct_abort_unsol_event;
dea3101e 6520 break;
6521 }
ed957684 6522 totiocbsize += (pring->numCiocb * pring->sizeCiocb) +
92d7f7b0 6523 (pring->numRiocb * pring->sizeRiocb);
dea3101e 6524 }
ed957684 6525 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
dea3101e 6526 /* Too many cmd / rsp ring entries in SLI2 SLIM */
e8b62011
JS
6527 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
6528 "SLI2 SLIM Data: x%x x%lx\n",
6529 phba->brd_no, totiocbsize,
6530 (unsigned long) MAX_SLIM_IOCB_SIZE);
dea3101e 6531 }
cf5bf97e
JW
6532 if (phba->cfg_multi_ring_support == 2)
6533 lpfc_extra_ring_setup(phba);
dea3101e 6534
6535 return 0;
6536}
6537
e59058c4 6538/**
3621a710 6539 * lpfc_sli_queue_setup - Queue initialization function
e59058c4
JS
6540 * @phba: Pointer to HBA context object.
6541 *
6542 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
6543 * ring. This function also initializes ring indices of each ring.
6544 * This function is called during the initialization of the SLI
6545 * interface of an HBA.
6546 * This function is called with no lock held and always returns
6547 * 1.
6548 **/
dea3101e 6549int
2e0fef85 6550lpfc_sli_queue_setup(struct lpfc_hba *phba)
dea3101e 6551{
6552 struct lpfc_sli *psli;
6553 struct lpfc_sli_ring *pring;
604a3e30 6554 int i;
dea3101e 6555
6556 psli = &phba->sli;
2e0fef85 6557 spin_lock_irq(&phba->hbalock);
dea3101e 6558 INIT_LIST_HEAD(&psli->mboxq);
92d7f7b0 6559 INIT_LIST_HEAD(&psli->mboxq_cmpl);
dea3101e 6560 /* Initialize list headers for txq and txcmplq as double linked lists */
6561 for (i = 0; i < psli->num_rings; i++) {
6562 pring = &psli->ring[i];
6563 pring->ringno = i;
6564 pring->next_cmdidx = 0;
6565 pring->local_getidx = 0;
6566 pring->cmdidx = 0;
6567 INIT_LIST_HEAD(&pring->txq);
6568 INIT_LIST_HEAD(&pring->txcmplq);
6569 INIT_LIST_HEAD(&pring->iocb_continueq);
9c2face6 6570 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
dea3101e 6571 INIT_LIST_HEAD(&pring->postbufq);
dea3101e 6572 }
2e0fef85
JS
6573 spin_unlock_irq(&phba->hbalock);
6574 return 1;
dea3101e 6575}
6576
04c68496
JS
6577/**
6578 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
6579 * @phba: Pointer to HBA context object.
6580 *
6581 * This routine flushes the mailbox command subsystem. It will unconditionally
6582 * flush all the mailbox commands in the three possible stages in the mailbox
6583 * command sub-system: pending mailbox command queue; the outstanding mailbox
6584 * command; and completed mailbox command queue. It is caller's responsibility
6585 * to make sure that the driver is in the proper state to flush the mailbox
6586 * command sub-system. Namely, the posting of mailbox commands into the
6587 * pending mailbox command queue from the various clients must be stopped;
6588 * either the HBA is in a state that it will never works on the outstanding
6589 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
6590 * mailbox command has been completed.
6591 **/
6592static void
6593lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
6594{
6595 LIST_HEAD(completions);
6596 struct lpfc_sli *psli = &phba->sli;
6597 LPFC_MBOXQ_t *pmb;
6598 unsigned long iflag;
6599
6600 /* Flush all the mailbox commands in the mbox system */
6601 spin_lock_irqsave(&phba->hbalock, iflag);
6602 /* The pending mailbox command queue */
6603 list_splice_init(&phba->sli.mboxq, &completions);
6604 /* The outstanding active mailbox command */
6605 if (psli->mbox_active) {
6606 list_add_tail(&psli->mbox_active->list, &completions);
6607 psli->mbox_active = NULL;
6608 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6609 }
6610 /* The completed mailbox command queue */
6611 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
6612 spin_unlock_irqrestore(&phba->hbalock, iflag);
6613
6614 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
6615 while (!list_empty(&completions)) {
6616 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
6617 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
6618 if (pmb->mbox_cmpl)
6619 pmb->mbox_cmpl(phba, pmb);
6620 }
6621}
6622
e59058c4 6623/**
3621a710 6624 * lpfc_sli_host_down - Vport cleanup function
e59058c4
JS
6625 * @vport: Pointer to virtual port object.
6626 *
6627 * lpfc_sli_host_down is called to clean up the resources
6628 * associated with a vport before destroying virtual
6629 * port data structures.
6630 * This function does following operations:
6631 * - Free discovery resources associated with this virtual
6632 * port.
6633 * - Free iocbs associated with this virtual port in
6634 * the txq.
6635 * - Send abort for all iocb commands associated with this
6636 * vport in txcmplq.
6637 *
6638 * This function is called with no lock held and always returns 1.
6639 **/
92d7f7b0
JS
6640int
6641lpfc_sli_host_down(struct lpfc_vport *vport)
6642{
858c9f6c 6643 LIST_HEAD(completions);
92d7f7b0
JS
6644 struct lpfc_hba *phba = vport->phba;
6645 struct lpfc_sli *psli = &phba->sli;
6646 struct lpfc_sli_ring *pring;
6647 struct lpfc_iocbq *iocb, *next_iocb;
92d7f7b0
JS
6648 int i;
6649 unsigned long flags = 0;
6650 uint16_t prev_pring_flag;
6651
6652 lpfc_cleanup_discovery_resources(vport);
6653
6654 spin_lock_irqsave(&phba->hbalock, flags);
92d7f7b0
JS
6655 for (i = 0; i < psli->num_rings; i++) {
6656 pring = &psli->ring[i];
6657 prev_pring_flag = pring->flag;
5e9d9b82
JS
6658 /* Only slow rings */
6659 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6660 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6661 /* Set the lpfc data pending flag */
6662 set_bit(LPFC_DATA_READY, &phba->data_flags);
6663 }
92d7f7b0
JS
6664 /*
6665 * Error everything on the txq since these iocbs have not been
6666 * given to the FW yet.
6667 */
92d7f7b0
JS
6668 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
6669 if (iocb->vport != vport)
6670 continue;
858c9f6c 6671 list_move_tail(&iocb->list, &completions);
92d7f7b0 6672 pring->txq_cnt--;
92d7f7b0
JS
6673 }
6674
6675 /* Next issue ABTS for everything on the txcmplq */
6676 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
6677 list) {
6678 if (iocb->vport != vport)
6679 continue;
6680 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
6681 }
6682
6683 pring->flag = prev_pring_flag;
6684 }
6685
6686 spin_unlock_irqrestore(&phba->hbalock, flags);
6687
a257bf90
JS
6688 /* Cancel all the IOCBs from the completions list */
6689 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6690 IOERR_SLI_DOWN);
92d7f7b0
JS
6691 return 1;
6692}
6693
e59058c4 6694/**
3621a710 6695 * lpfc_sli_hba_down - Resource cleanup function for the HBA
e59058c4
JS
6696 * @phba: Pointer to HBA context object.
6697 *
6698 * This function cleans up all iocb, buffers, mailbox commands
6699 * while shutting down the HBA. This function is called with no
6700 * lock held and always returns 1.
6701 * This function does the following to cleanup driver resources:
6702 * - Free discovery resources for each virtual port
6703 * - Cleanup any pending fabric iocbs
6704 * - Iterate through the iocb txq and free each entry
6705 * in the list.
6706 * - Free up any buffer posted to the HBA
6707 * - Free mailbox commands in the mailbox queue.
6708 **/
dea3101e 6709int
2e0fef85 6710lpfc_sli_hba_down(struct lpfc_hba *phba)
dea3101e 6711{
2534ba75 6712 LIST_HEAD(completions);
2e0fef85 6713 struct lpfc_sli *psli = &phba->sli;
dea3101e 6714 struct lpfc_sli_ring *pring;
0ff10d46 6715 struct lpfc_dmabuf *buf_ptr;
dea3101e 6716 unsigned long flags = 0;
04c68496
JS
6717 int i;
6718
6719 /* Shutdown the mailbox command sub-system */
6720 lpfc_sli_mbox_sys_shutdown(phba);
dea3101e 6721
dea3101e 6722 lpfc_hba_down_prep(phba);
6723
92d7f7b0
JS
6724 lpfc_fabric_abort_hba(phba);
6725
2e0fef85 6726 spin_lock_irqsave(&phba->hbalock, flags);
dea3101e 6727 for (i = 0; i < psli->num_rings; i++) {
6728 pring = &psli->ring[i];
5e9d9b82
JS
6729 /* Only slow rings */
6730 if (pring->ringno == LPFC_ELS_RING) {
858c9f6c 6731 pring->flag |= LPFC_DEFERRED_RING_EVENT;
5e9d9b82
JS
6732 /* Set the lpfc data pending flag */
6733 set_bit(LPFC_DATA_READY, &phba->data_flags);
6734 }
dea3101e 6735
6736 /*
6737 * Error everything on the txq since these iocbs have not been
6738 * given to the FW yet.
6739 */
2534ba75 6740 list_splice_init(&pring->txq, &completions);
dea3101e 6741 pring->txq_cnt = 0;
6742
2534ba75 6743 }
2e0fef85 6744 spin_unlock_irqrestore(&phba->hbalock, flags);
dea3101e 6745
a257bf90
JS
6746 /* Cancel all the IOCBs from the completions list */
6747 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
6748 IOERR_SLI_DOWN);
dea3101e 6749
0ff10d46
JS
6750 spin_lock_irqsave(&phba->hbalock, flags);
6751 list_splice_init(&phba->elsbuf, &completions);
6752 phba->elsbuf_cnt = 0;
6753 phba->elsbuf_prev_cnt = 0;
6754 spin_unlock_irqrestore(&phba->hbalock, flags);
6755
6756 while (!list_empty(&completions)) {
6757 list_remove_head(&completions, buf_ptr,
6758 struct lpfc_dmabuf, list);
6759 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
6760 kfree(buf_ptr);
6761 }
6762
dea3101e 6763 /* Return any active mbox cmds */
6764 del_timer_sync(&psli->mbox_tmo);
2e0fef85 6765
da0436e9 6766 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
2e0fef85 6767 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
da0436e9 6768 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
2e0fef85 6769
da0436e9
JS
6770 return 1;
6771}
6772
6773/**
6774 * lpfc_sli4_hba_down - PCI function resource cleanup for the SLI4 HBA
6775 * @phba: Pointer to HBA context object.
6776 *
6777 * This function cleans up all queues, iocb, buffers, mailbox commands while
6778 * shutting down the SLI4 HBA FCoE function. This function is called with no
6779 * lock held and always returns 1.
6780 *
6781 * This function does the following to cleanup driver FCoE function resources:
6782 * - Free discovery resources for each virtual port
6783 * - Cleanup any pending fabric iocbs
6784 * - Iterate through the iocb txq and free each entry in the list.
6785 * - Free up any buffer posted to the HBA.
6786 * - Clean up all the queue entries: WQ, RQ, MQ, EQ, CQ, etc.
6787 * - Free mailbox commands in the mailbox queue.
6788 **/
6789int
6790lpfc_sli4_hba_down(struct lpfc_hba *phba)
6791{
6792 /* Stop the SLI4 device port */
6793 lpfc_stop_port(phba);
6794
6795 /* Tear down the queues in the HBA */
6796 lpfc_sli4_queue_unset(phba);
6797
6798 /* unregister default FCFI from the HBA */
6799 lpfc_sli4_fcfi_unreg(phba, phba->fcf.fcfi);
92d7f7b0 6800
dea3101e 6801 return 1;
6802}
6803
e59058c4 6804/**
3621a710 6805 * lpfc_sli_pcimem_bcopy - SLI memory copy function
e59058c4
JS
6806 * @srcp: Source memory pointer.
6807 * @destp: Destination memory pointer.
6808 * @cnt: Number of words required to be copied.
6809 *
6810 * This function is used for copying data between driver memory
6811 * and the SLI memory. This function also changes the endianness
6812 * of each word if native endianness is different from SLI
6813 * endianness. This function can be called with or without
6814 * lock.
6815 **/
dea3101e 6816void
6817lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
6818{
6819 uint32_t *src = srcp;
6820 uint32_t *dest = destp;
6821 uint32_t ldata;
6822 int i;
6823
6824 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
6825 ldata = *src;
6826 ldata = le32_to_cpu(ldata);
6827 *dest = ldata;
6828 src++;
6829 dest++;
6830 }
6831}
6832
e59058c4 6833
a0c87cbd
JS
6834/**
6835 * lpfc_sli_bemem_bcopy - SLI memory copy function
6836 * @srcp: Source memory pointer.
6837 * @destp: Destination memory pointer.
6838 * @cnt: Number of words required to be copied.
6839 *
6840 * This function is used for copying data between a data structure
6841 * with big endian representation to local endianness.
6842 * This function can be called with or without lock.
6843 **/
6844void
6845lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
6846{
6847 uint32_t *src = srcp;
6848 uint32_t *dest = destp;
6849 uint32_t ldata;
6850 int i;
6851
6852 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
6853 ldata = *src;
6854 ldata = be32_to_cpu(ldata);
6855 *dest = ldata;
6856 src++;
6857 dest++;
6858 }
6859}
6860
e59058c4 6861/**
3621a710 6862 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
e59058c4
JS
6863 * @phba: Pointer to HBA context object.
6864 * @pring: Pointer to driver SLI ring object.
6865 * @mp: Pointer to driver buffer object.
6866 *
6867 * This function is called with no lock held.
6868 * It always return zero after adding the buffer to the postbufq
6869 * buffer list.
6870 **/
dea3101e 6871int
2e0fef85
JS
6872lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6873 struct lpfc_dmabuf *mp)
dea3101e 6874{
6875 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
6876 later */
2e0fef85 6877 spin_lock_irq(&phba->hbalock);
dea3101e 6878 list_add_tail(&mp->list, &pring->postbufq);
dea3101e 6879 pring->postbufq_cnt++;
2e0fef85 6880 spin_unlock_irq(&phba->hbalock);
dea3101e 6881 return 0;
6882}
6883
e59058c4 6884/**
3621a710 6885 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
e59058c4
JS
6886 * @phba: Pointer to HBA context object.
6887 *
6888 * When HBQ is enabled, buffers are searched based on tags. This function
6889 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
6890 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
6891 * does not conflict with tags of buffer posted for unsolicited events.
6892 * The function returns the allocated tag. The function is called with
6893 * no locks held.
6894 **/
76bb24ef
JS
6895uint32_t
6896lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
6897{
6898 spin_lock_irq(&phba->hbalock);
6899 phba->buffer_tag_count++;
6900 /*
6901 * Always set the QUE_BUFTAG_BIT to distiguish between
6902 * a tag assigned by HBQ.
6903 */
6904 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
6905 spin_unlock_irq(&phba->hbalock);
6906 return phba->buffer_tag_count;
6907}
6908
e59058c4 6909/**
3621a710 6910 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
e59058c4
JS
6911 * @phba: Pointer to HBA context object.
6912 * @pring: Pointer to driver SLI ring object.
6913 * @tag: Buffer tag.
6914 *
6915 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
6916 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
6917 * iocb is posted to the response ring with the tag of the buffer.
6918 * This function searches the pring->postbufq list using the tag
6919 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
6920 * iocb. If the buffer is found then lpfc_dmabuf object of the
6921 * buffer is returned to the caller else NULL is returned.
6922 * This function is called with no lock held.
6923 **/
76bb24ef
JS
6924struct lpfc_dmabuf *
6925lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6926 uint32_t tag)
6927{
6928 struct lpfc_dmabuf *mp, *next_mp;
6929 struct list_head *slp = &pring->postbufq;
6930
6931 /* Search postbufq, from the begining, looking for a match on tag */
6932 spin_lock_irq(&phba->hbalock);
6933 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6934 if (mp->buffer_tag == tag) {
6935 list_del_init(&mp->list);
6936 pring->postbufq_cnt--;
6937 spin_unlock_irq(&phba->hbalock);
6938 return mp;
6939 }
6940 }
6941
6942 spin_unlock_irq(&phba->hbalock);
6943 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
d7c255b2 6944 "0402 Cannot find virtual addr for buffer tag on "
76bb24ef
JS
6945 "ring %d Data x%lx x%p x%p x%x\n",
6946 pring->ringno, (unsigned long) tag,
6947 slp->next, slp->prev, pring->postbufq_cnt);
6948
6949 return NULL;
6950}
dea3101e 6951
e59058c4 6952/**
3621a710 6953 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
e59058c4
JS
6954 * @phba: Pointer to HBA context object.
6955 * @pring: Pointer to driver SLI ring object.
6956 * @phys: DMA address of the buffer.
6957 *
6958 * This function searches the buffer list using the dma_address
6959 * of unsolicited event to find the driver's lpfc_dmabuf object
6960 * corresponding to the dma_address. The function returns the
6961 * lpfc_dmabuf object if a buffer is found else it returns NULL.
6962 * This function is called by the ct and els unsolicited event
6963 * handlers to get the buffer associated with the unsolicited
6964 * event.
6965 *
6966 * This function is called with no lock held.
6967 **/
dea3101e 6968struct lpfc_dmabuf *
6969lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
6970 dma_addr_t phys)
6971{
6972 struct lpfc_dmabuf *mp, *next_mp;
6973 struct list_head *slp = &pring->postbufq;
6974
6975 /* Search postbufq, from the begining, looking for a match on phys */
2e0fef85 6976 spin_lock_irq(&phba->hbalock);
dea3101e 6977 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
6978 if (mp->phys == phys) {
6979 list_del_init(&mp->list);
6980 pring->postbufq_cnt--;
2e0fef85 6981 spin_unlock_irq(&phba->hbalock);
dea3101e 6982 return mp;
6983 }
6984 }
6985
2e0fef85 6986 spin_unlock_irq(&phba->hbalock);
dea3101e 6987 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
e8b62011 6988 "0410 Cannot find virtual addr for mapped buf on "
dea3101e 6989 "ring %d Data x%llx x%p x%p x%x\n",
e8b62011 6990 pring->ringno, (unsigned long long)phys,
dea3101e 6991 slp->next, slp->prev, pring->postbufq_cnt);
6992 return NULL;
6993}
6994
e59058c4 6995/**
3621a710 6996 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
e59058c4
JS
6997 * @phba: Pointer to HBA context object.
6998 * @cmdiocb: Pointer to driver command iocb object.
6999 * @rspiocb: Pointer to driver response iocb object.
7000 *
7001 * This function is the completion handler for the abort iocbs for
7002 * ELS commands. This function is called from the ELS ring event
7003 * handler with no lock held. This function frees memory resources
7004 * associated with the abort iocb.
7005 **/
dea3101e 7006static void
2e0fef85
JS
7007lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7008 struct lpfc_iocbq *rspiocb)
dea3101e 7009{
2e0fef85 7010 IOCB_t *irsp = &rspiocb->iocb;
2680eeaa 7011 uint16_t abort_iotag, abort_context;
92d7f7b0 7012 struct lpfc_iocbq *abort_iocb;
2680eeaa
JS
7013 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
7014
7015 abort_iocb = NULL;
2680eeaa
JS
7016
7017 if (irsp->ulpStatus) {
7018 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
7019 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
7020
2e0fef85 7021 spin_lock_irq(&phba->hbalock);
2680eeaa
JS
7022 if (abort_iotag != 0 && abort_iotag <= phba->sli.last_iotag)
7023 abort_iocb = phba->sli.iocbq_lookup[abort_iotag];
7024
92d7f7b0 7025 lpfc_printf_log(phba, KERN_INFO, LOG_ELS | LOG_SLI,
e8b62011 7026 "0327 Cannot abort els iocb %p "
92d7f7b0
JS
7027 "with tag %x context %x, abort status %x, "
7028 "abort code %x\n",
e8b62011
JS
7029 abort_iocb, abort_iotag, abort_context,
7030 irsp->ulpStatus, irsp->un.ulpWord[4]);
2680eeaa 7031
58da1ffb
JS
7032 /*
7033 * If the iocb is not found in Firmware queue the iocb
7034 * might have completed already. Do not free it again.
7035 */
9b379605 7036 if (irsp->ulpStatus == IOSTAT_LOCAL_REJECT) {
58da1ffb
JS
7037 spin_unlock_irq(&phba->hbalock);
7038 lpfc_sli_release_iocbq(phba, cmdiocb);
7039 return;
7040 }
2680eeaa
JS
7041 /*
7042 * make sure we have the right iocbq before taking it
7043 * off the txcmplq and try to call completion routine.
7044 */
2e0fef85
JS
7045 if (!abort_iocb ||
7046 abort_iocb->iocb.ulpContext != abort_context ||
7047 (abort_iocb->iocb_flag & LPFC_DRIVER_ABORTED) == 0)
7048 spin_unlock_irq(&phba->hbalock);
7049 else {
92d7f7b0 7050 list_del_init(&abort_iocb->list);
2680eeaa 7051 pring->txcmplq_cnt--;
2e0fef85 7052 spin_unlock_irq(&phba->hbalock);
2680eeaa 7053
0ff10d46
JS
7054 /* Firmware could still be in progress of DMAing
7055 * payload, so don't free data buffer till after
7056 * a hbeat.
7057 */
7058 abort_iocb->iocb_flag |= LPFC_DELAY_MEM_FREE;
7059
92d7f7b0
JS
7060 abort_iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
7061 abort_iocb->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
7062 abort_iocb->iocb.un.ulpWord[4] = IOERR_SLI_ABORTED;
7063 (abort_iocb->iocb_cmpl)(phba, abort_iocb, abort_iocb);
2680eeaa
JS
7064 }
7065 }
7066
604a3e30 7067 lpfc_sli_release_iocbq(phba, cmdiocb);
dea3101e 7068 return;
7069}
7070
e59058c4 7071/**
3621a710 7072 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
e59058c4
JS
7073 * @phba: Pointer to HBA context object.
7074 * @cmdiocb: Pointer to driver command iocb object.
7075 * @rspiocb: Pointer to driver response iocb object.
7076 *
7077 * The function is called from SLI ring event handler with no
7078 * lock held. This function is the completion handler for ELS commands
7079 * which are aborted. The function frees memory resources used for
7080 * the aborted ELS commands.
7081 **/
92d7f7b0
JS
7082static void
7083lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7084 struct lpfc_iocbq *rspiocb)
7085{
7086 IOCB_t *irsp = &rspiocb->iocb;
7087
7088 /* ELS cmd tag <ulpIoTag> completes */
7089 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
d7c255b2 7090 "0139 Ignoring ELS cmd tag x%x completion Data: "
92d7f7b0 7091 "x%x x%x x%x\n",
e8b62011 7092 irsp->ulpIoTag, irsp->ulpStatus,
92d7f7b0 7093 irsp->un.ulpWord[4], irsp->ulpTimeout);
858c9f6c
JS
7094 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
7095 lpfc_ct_free_iocb(phba, cmdiocb);
7096 else
7097 lpfc_els_free_iocb(phba, cmdiocb);
92d7f7b0
JS
7098 return;
7099}
7100
e59058c4 7101/**
3621a710 7102 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
e59058c4
JS
7103 * @phba: Pointer to HBA context object.
7104 * @pring: Pointer to driver SLI ring object.
7105 * @cmdiocb: Pointer to driver command iocb object.
7106 *
7107 * This function issues an abort iocb for the provided command
7108 * iocb. This function is called with hbalock held.
7109 * The function returns 0 when it fails due to memory allocation
7110 * failure or when the command iocb is an abort request.
7111 **/
dea3101e 7112int
2e0fef85
JS
7113lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7114 struct lpfc_iocbq *cmdiocb)
dea3101e 7115{
2e0fef85 7116 struct lpfc_vport *vport = cmdiocb->vport;
0bd4ca25 7117 struct lpfc_iocbq *abtsiocbp;
dea3101e 7118 IOCB_t *icmd = NULL;
7119 IOCB_t *iabt = NULL;
07951076
JS
7120 int retval = IOCB_ERROR;
7121
92d7f7b0
JS
7122 /*
7123 * There are certain command types we don't want to abort. And we
7124 * don't want to abort commands that are already in the process of
7125 * being aborted.
07951076
JS
7126 */
7127 icmd = &cmdiocb->iocb;
2e0fef85 7128 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
92d7f7b0
JS
7129 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
7130 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
07951076
JS
7131 return 0;
7132
858c9f6c
JS
7133 /* If we're unloading, don't abort iocb on the ELS ring, but change the
7134 * callback so that nothing happens when it finishes.
07951076 7135 */
858c9f6c
JS
7136 if ((vport->load_flag & FC_UNLOADING) &&
7137 (pring->ringno == LPFC_ELS_RING)) {
92d7f7b0
JS
7138 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
7139 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
7140 else
7141 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
07951076 7142 goto abort_iotag_exit;
92d7f7b0 7143 }
dea3101e 7144
7145 /* issue ABTS for this IOCB based on iotag */
92d7f7b0 7146 abtsiocbp = __lpfc_sli_get_iocbq(phba);
dea3101e 7147 if (abtsiocbp == NULL)
7148 return 0;
dea3101e 7149
07951076
JS
7150 /* This signals the response to set the correct status
7151 * before calling the completion handler.
7152 */
7153 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
7154
dea3101e 7155 iabt = &abtsiocbp->iocb;
07951076
JS
7156 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
7157 iabt->un.acxri.abortContextTag = icmd->ulpContext;
da0436e9
JS
7158 if (phba->sli_rev == LPFC_SLI_REV4)
7159 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
7160 else
7161 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
07951076
JS
7162 iabt->ulpLe = 1;
7163 iabt->ulpClass = icmd->ulpClass;
dea3101e 7164
2e0fef85 7165 if (phba->link_state >= LPFC_LINK_UP)
07951076
JS
7166 iabt->ulpCommand = CMD_ABORT_XRI_CN;
7167 else
7168 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 7169
07951076 7170 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
5b8bd0c9 7171
e8b62011
JS
7172 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
7173 "0339 Abort xri x%x, original iotag x%x, "
7174 "abort cmd iotag x%x\n",
7175 iabt->un.acxri.abortContextTag,
7176 iabt->un.acxri.abortIoTag, abtsiocbp->iotag);
da0436e9 7177 retval = __lpfc_sli_issue_iocb(phba, pring->ringno, abtsiocbp, 0);
dea3101e 7178
d7c255b2
JS
7179 if (retval)
7180 __lpfc_sli_release_iocbq(phba, abtsiocbp);
07951076 7181abort_iotag_exit:
2e0fef85
JS
7182 /*
7183 * Caller to this routine should check for IOCB_ERROR
7184 * and handle it properly. This routine no longer removes
7185 * iocb off txcmplq and call compl in case of IOCB_ERROR.
07951076 7186 */
2e0fef85 7187 return retval;
dea3101e 7188}
7189
e59058c4 7190/**
3621a710 7191 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
e59058c4
JS
7192 * @iocbq: Pointer to driver iocb object.
7193 * @vport: Pointer to driver virtual port object.
7194 * @tgt_id: SCSI ID of the target.
7195 * @lun_id: LUN ID of the scsi device.
7196 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
7197 *
3621a710 7198 * This function acts as an iocb filter for functions which abort or count
e59058c4
JS
7199 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
7200 * 0 if the filtering criteria is met for the given iocb and will return
7201 * 1 if the filtering criteria is not met.
7202 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
7203 * given iocb is for the SCSI device specified by vport, tgt_id and
7204 * lun_id parameter.
7205 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
7206 * given iocb is for the SCSI target specified by vport and tgt_id
7207 * parameters.
7208 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
7209 * given iocb is for the SCSI host associated with the given vport.
7210 * This function is called with no locks held.
7211 **/
dea3101e 7212static int
51ef4c26
JS
7213lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
7214 uint16_t tgt_id, uint64_t lun_id,
0bd4ca25 7215 lpfc_ctx_cmd ctx_cmd)
dea3101e 7216{
0bd4ca25 7217 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 7218 int rc = 1;
7219
0bd4ca25
JSEC
7220 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
7221 return rc;
7222
51ef4c26
JS
7223 if (iocbq->vport != vport)
7224 return rc;
7225
0bd4ca25 7226 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
0bd4ca25 7227
495a714c 7228 if (lpfc_cmd->pCmd == NULL)
dea3101e 7229 return rc;
7230
7231 switch (ctx_cmd) {
7232 case LPFC_CTX_LUN:
495a714c
JS
7233 if ((lpfc_cmd->rdata->pnode) &&
7234 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
7235 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
dea3101e 7236 rc = 0;
7237 break;
7238 case LPFC_CTX_TGT:
495a714c
JS
7239 if ((lpfc_cmd->rdata->pnode) &&
7240 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
dea3101e 7241 rc = 0;
7242 break;
dea3101e 7243 case LPFC_CTX_HOST:
7244 rc = 0;
7245 break;
7246 default:
7247 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
cadbd4a5 7248 __func__, ctx_cmd);
dea3101e 7249 break;
7250 }
7251
7252 return rc;
7253}
7254
e59058c4 7255/**
3621a710 7256 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
e59058c4
JS
7257 * @vport: Pointer to virtual port.
7258 * @tgt_id: SCSI ID of the target.
7259 * @lun_id: LUN ID of the scsi device.
7260 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7261 *
7262 * This function returns number of FCP commands pending for the vport.
7263 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
7264 * commands pending on the vport associated with SCSI device specified
7265 * by tgt_id and lun_id parameters.
7266 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
7267 * commands pending on the vport associated with SCSI target specified
7268 * by tgt_id parameter.
7269 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
7270 * commands pending on the vport.
7271 * This function returns the number of iocbs which satisfy the filter.
7272 * This function is called without any lock held.
7273 **/
dea3101e 7274int
51ef4c26
JS
7275lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
7276 lpfc_ctx_cmd ctx_cmd)
dea3101e 7277{
51ef4c26 7278 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7279 struct lpfc_iocbq *iocbq;
7280 int sum, i;
dea3101e 7281
0bd4ca25
JSEC
7282 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
7283 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7284
51ef4c26
JS
7285 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
7286 ctx_cmd) == 0)
0bd4ca25 7287 sum++;
dea3101e 7288 }
0bd4ca25 7289
dea3101e 7290 return sum;
7291}
7292
e59058c4 7293/**
3621a710 7294 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
e59058c4
JS
7295 * @phba: Pointer to HBA context object
7296 * @cmdiocb: Pointer to command iocb object.
7297 * @rspiocb: Pointer to response iocb object.
7298 *
7299 * This function is called when an aborted FCP iocb completes. This
7300 * function is called by the ring event handler with no lock held.
7301 * This function frees the iocb.
7302 **/
5eb95af0 7303void
2e0fef85
JS
7304lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
7305 struct lpfc_iocbq *rspiocb)
5eb95af0 7306{
604a3e30 7307 lpfc_sli_release_iocbq(phba, cmdiocb);
5eb95af0
JSEC
7308 return;
7309}
7310
e59058c4 7311/**
3621a710 7312 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
e59058c4
JS
7313 * @vport: Pointer to virtual port.
7314 * @pring: Pointer to driver SLI ring object.
7315 * @tgt_id: SCSI ID of the target.
7316 * @lun_id: LUN ID of the scsi device.
7317 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
7318 *
7319 * This function sends an abort command for every SCSI command
7320 * associated with the given virtual port pending on the ring
7321 * filtered by lpfc_sli_validate_fcp_iocb function.
7322 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
7323 * FCP iocbs associated with lun specified by tgt_id and lun_id
7324 * parameters
7325 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
7326 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
7327 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
7328 * FCP iocbs associated with virtual port.
7329 * This function returns number of iocbs it failed to abort.
7330 * This function is called with no locks held.
7331 **/
dea3101e 7332int
51ef4c26
JS
7333lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
7334 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
dea3101e 7335{
51ef4c26 7336 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
7337 struct lpfc_iocbq *iocbq;
7338 struct lpfc_iocbq *abtsiocb;
dea3101e 7339 IOCB_t *cmd = NULL;
dea3101e 7340 int errcnt = 0, ret_val = 0;
0bd4ca25 7341 int i;
dea3101e 7342
0bd4ca25
JSEC
7343 for (i = 1; i <= phba->sli.last_iotag; i++) {
7344 iocbq = phba->sli.iocbq_lookup[i];
dea3101e 7345
51ef4c26 7346 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
2e0fef85 7347 abort_cmd) != 0)
dea3101e 7348 continue;
7349
7350 /* issue ABTS for this IOCB based on iotag */
0bd4ca25 7351 abtsiocb = lpfc_sli_get_iocbq(phba);
dea3101e 7352 if (abtsiocb == NULL) {
7353 errcnt++;
7354 continue;
7355 }
dea3101e 7356
0bd4ca25 7357 cmd = &iocbq->iocb;
dea3101e 7358 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
7359 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
da0436e9
JS
7360 if (phba->sli_rev == LPFC_SLI_REV4)
7361 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
7362 else
7363 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 7364 abtsiocb->iocb.ulpLe = 1;
7365 abtsiocb->iocb.ulpClass = cmd->ulpClass;
2e0fef85 7366 abtsiocb->vport = phba->pport;
dea3101e 7367
2e0fef85 7368 if (lpfc_is_link_up(phba))
dea3101e 7369 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
7370 else
7371 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
7372
5eb95af0
JSEC
7373 /* Setup callback routine and issue the command. */
7374 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
da0436e9
JS
7375 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
7376 abtsiocb, 0);
dea3101e 7377 if (ret_val == IOCB_ERROR) {
604a3e30 7378 lpfc_sli_release_iocbq(phba, abtsiocb);
dea3101e 7379 errcnt++;
7380 continue;
7381 }
7382 }
7383
7384 return errcnt;
7385}
7386
e59058c4 7387/**
3621a710 7388 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
e59058c4
JS
7389 * @phba: Pointer to HBA context object.
7390 * @cmdiocbq: Pointer to command iocb.
7391 * @rspiocbq: Pointer to response iocb.
7392 *
7393 * This function is the completion handler for iocbs issued using
7394 * lpfc_sli_issue_iocb_wait function. This function is called by the
7395 * ring event handler function without any lock held. This function
7396 * can be called from both worker thread context and interrupt
7397 * context. This function also can be called from other thread which
7398 * cleans up the SLI layer objects.
7399 * This function copy the contents of the response iocb to the
7400 * response iocb memory object provided by the caller of
7401 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
7402 * sleeps for the iocb completion.
7403 **/
68876920
JSEC
7404static void
7405lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
7406 struct lpfc_iocbq *cmdiocbq,
7407 struct lpfc_iocbq *rspiocbq)
dea3101e 7408{
68876920
JSEC
7409 wait_queue_head_t *pdone_q;
7410 unsigned long iflags;
dea3101e 7411
2e0fef85 7412 spin_lock_irqsave(&phba->hbalock, iflags);
68876920
JSEC
7413 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
7414 if (cmdiocbq->context2 && rspiocbq)
7415 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
7416 &rspiocbq->iocb, sizeof(IOCB_t));
7417
7418 pdone_q = cmdiocbq->context_un.wait_queue;
68876920
JSEC
7419 if (pdone_q)
7420 wake_up(pdone_q);
858c9f6c 7421 spin_unlock_irqrestore(&phba->hbalock, iflags);
dea3101e 7422 return;
7423}
7424
d11e31dd
JS
7425/**
7426 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
7427 * @phba: Pointer to HBA context object..
7428 * @piocbq: Pointer to command iocb.
7429 * @flag: Flag to test.
7430 *
7431 * This routine grabs the hbalock and then test the iocb_flag to
7432 * see if the passed in flag is set.
7433 * Returns:
7434 * 1 if flag is set.
7435 * 0 if flag is not set.
7436 **/
7437static int
7438lpfc_chk_iocb_flg(struct lpfc_hba *phba,
7439 struct lpfc_iocbq *piocbq, uint32_t flag)
7440{
7441 unsigned long iflags;
7442 int ret;
7443
7444 spin_lock_irqsave(&phba->hbalock, iflags);
7445 ret = piocbq->iocb_flag & flag;
7446 spin_unlock_irqrestore(&phba->hbalock, iflags);
7447 return ret;
7448
7449}
7450
e59058c4 7451/**
3621a710 7452 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
e59058c4
JS
7453 * @phba: Pointer to HBA context object..
7454 * @pring: Pointer to sli ring.
7455 * @piocb: Pointer to command iocb.
7456 * @prspiocbq: Pointer to response iocb.
7457 * @timeout: Timeout in number of seconds.
7458 *
7459 * This function issues the iocb to firmware and waits for the
7460 * iocb to complete. If the iocb command is not
7461 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
7462 * Caller should not free the iocb resources if this function
7463 * returns IOCB_TIMEDOUT.
7464 * The function waits for the iocb completion using an
7465 * non-interruptible wait.
7466 * This function will sleep while waiting for iocb completion.
7467 * So, this function should not be called from any context which
7468 * does not allow sleeping. Due to the same reason, this function
7469 * cannot be called with interrupt disabled.
7470 * This function assumes that the iocb completions occur while
7471 * this function sleep. So, this function cannot be called from
7472 * the thread which process iocb completion for this ring.
7473 * This function clears the iocb_flag of the iocb object before
7474 * issuing the iocb and the iocb completion handler sets this
7475 * flag and wakes this thread when the iocb completes.
7476 * The contents of the response iocb will be copied to prspiocbq
7477 * by the completion handler when the command completes.
7478 * This function returns IOCB_SUCCESS when success.
7479 * This function is called with no lock held.
7480 **/
dea3101e 7481int
2e0fef85 7482lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
da0436e9 7483 uint32_t ring_number,
2e0fef85
JS
7484 struct lpfc_iocbq *piocb,
7485 struct lpfc_iocbq *prspiocbq,
68876920 7486 uint32_t timeout)
dea3101e 7487{
7259f0d0 7488 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
68876920
JSEC
7489 long timeleft, timeout_req = 0;
7490 int retval = IOCB_SUCCESS;
875fbdfe 7491 uint32_t creg_val;
dea3101e 7492
7493 /*
68876920
JSEC
7494 * If the caller has provided a response iocbq buffer, then context2
7495 * is NULL or its an error.
dea3101e 7496 */
68876920
JSEC
7497 if (prspiocbq) {
7498 if (piocb->context2)
7499 return IOCB_ERROR;
7500 piocb->context2 = prspiocbq;
dea3101e 7501 }
7502
68876920
JSEC
7503 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
7504 piocb->context_un.wait_queue = &done_q;
7505 piocb->iocb_flag &= ~LPFC_IO_WAKE;
dea3101e 7506
875fbdfe
JSEC
7507 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7508 creg_val = readl(phba->HCregaddr);
7509 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
7510 writel(creg_val, phba->HCregaddr);
7511 readl(phba->HCregaddr); /* flush */
7512 }
7513
da0436e9 7514 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb, 0);
68876920
JSEC
7515 if (retval == IOCB_SUCCESS) {
7516 timeout_req = timeout * HZ;
68876920 7517 timeleft = wait_event_timeout(done_q,
d11e31dd 7518 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
68876920 7519 timeout_req);
dea3101e 7520
7054a606
JS
7521 if (piocb->iocb_flag & LPFC_IO_WAKE) {
7522 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
e8b62011 7523 "0331 IOCB wake signaled\n");
7054a606 7524 } else if (timeleft == 0) {
68876920 7525 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7526 "0338 IOCB wait timeout error - no "
7527 "wake response Data x%x\n", timeout);
68876920 7528 retval = IOCB_TIMEDOUT;
7054a606 7529 } else {
68876920 7530 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
e8b62011
JS
7531 "0330 IOCB wake NOT set, "
7532 "Data x%x x%lx\n",
68876920
JSEC
7533 timeout, (timeleft / jiffies));
7534 retval = IOCB_TIMEDOUT;
dea3101e 7535 }
68876920
JSEC
7536 } else {
7537 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
d7c255b2 7538 "0332 IOCB wait issue failed, Data x%x\n",
e8b62011 7539 retval);
68876920 7540 retval = IOCB_ERROR;
dea3101e 7541 }
7542
875fbdfe
JSEC
7543 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
7544 creg_val = readl(phba->HCregaddr);
7545 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
7546 writel(creg_val, phba->HCregaddr);
7547 readl(phba->HCregaddr); /* flush */
7548 }
7549
68876920
JSEC
7550 if (prspiocbq)
7551 piocb->context2 = NULL;
7552
7553 piocb->context_un.wait_queue = NULL;
7554 piocb->iocb_cmpl = NULL;
dea3101e 7555 return retval;
7556}
68876920 7557
e59058c4 7558/**
3621a710 7559 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
e59058c4
JS
7560 * @phba: Pointer to HBA context object.
7561 * @pmboxq: Pointer to driver mailbox object.
7562 * @timeout: Timeout in number of seconds.
7563 *
7564 * This function issues the mailbox to firmware and waits for the
7565 * mailbox command to complete. If the mailbox command is not
7566 * completed within timeout seconds, it returns MBX_TIMEOUT.
7567 * The function waits for the mailbox completion using an
7568 * interruptible wait. If the thread is woken up due to a
7569 * signal, MBX_TIMEOUT error is returned to the caller. Caller
7570 * should not free the mailbox resources, if this function returns
7571 * MBX_TIMEOUT.
7572 * This function will sleep while waiting for mailbox completion.
7573 * So, this function should not be called from any context which
7574 * does not allow sleeping. Due to the same reason, this function
7575 * cannot be called with interrupt disabled.
7576 * This function assumes that the mailbox completion occurs while
7577 * this function sleep. So, this function cannot be called from
7578 * the worker thread which processes mailbox completion.
7579 * This function is called in the context of HBA management
7580 * applications.
7581 * This function returns MBX_SUCCESS when successful.
7582 * This function is called with no lock held.
7583 **/
dea3101e 7584int
2e0fef85 7585lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
dea3101e 7586 uint32_t timeout)
7587{
7259f0d0 7588 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
dea3101e 7589 int retval;
858c9f6c 7590 unsigned long flag;
dea3101e 7591
7592 /* The caller must leave context1 empty. */
98c9ea5c 7593 if (pmboxq->context1)
2e0fef85 7594 return MBX_NOT_FINISHED;
dea3101e 7595
495a714c 7596 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
dea3101e 7597 /* setup wake call as IOCB callback */
7598 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
7599 /* setup context field to pass wait_queue pointer to wake function */
7600 pmboxq->context1 = &done_q;
7601
dea3101e 7602 /* now issue the command */
7603 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
7604
7605 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
7054a606
JS
7606 wait_event_interruptible_timeout(done_q,
7607 pmboxq->mbox_flag & LPFC_MBX_WAKE,
7608 timeout * HZ);
7609
858c9f6c 7610 spin_lock_irqsave(&phba->hbalock, flag);
dea3101e 7611 pmboxq->context1 = NULL;
7054a606
JS
7612 /*
7613 * if LPFC_MBX_WAKE flag is set the mailbox is completed
7614 * else do not free the resources.
7615 */
7616 if (pmboxq->mbox_flag & LPFC_MBX_WAKE)
dea3101e 7617 retval = MBX_SUCCESS;
858c9f6c 7618 else {
7054a606 7619 retval = MBX_TIMEOUT;
858c9f6c
JS
7620 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
7621 }
7622 spin_unlock_irqrestore(&phba->hbalock, flag);
dea3101e 7623 }
7624
dea3101e 7625 return retval;
7626}
7627
e59058c4 7628/**
3772a991 7629 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
e59058c4
JS
7630 * @phba: Pointer to HBA context.
7631 *
3772a991
JS
7632 * This function is called to shutdown the driver's mailbox sub-system.
7633 * It first marks the mailbox sub-system is in a block state to prevent
7634 * the asynchronous mailbox command from issued off the pending mailbox
7635 * command queue. If the mailbox command sub-system shutdown is due to
7636 * HBA error conditions such as EEH or ERATT, this routine shall invoke
7637 * the mailbox sub-system flush routine to forcefully bring down the
7638 * mailbox sub-system. Otherwise, if it is due to normal condition (such
7639 * as with offline or HBA function reset), this routine will wait for the
7640 * outstanding mailbox command to complete before invoking the mailbox
7641 * sub-system flush routine to gracefully bring down mailbox sub-system.
e59058c4 7642 **/
3772a991
JS
7643void
7644lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba)
b4c02652 7645{
3772a991
JS
7646 struct lpfc_sli *psli = &phba->sli;
7647 uint8_t actcmd = MBX_HEARTBEAT;
7648 unsigned long timeout;
b4c02652 7649
3772a991
JS
7650 spin_lock_irq(&phba->hbalock);
7651 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7652 spin_unlock_irq(&phba->hbalock);
b4c02652 7653
3772a991 7654 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
ed957684 7655 spin_lock_irq(&phba->hbalock);
3772a991
JS
7656 if (phba->sli.mbox_active)
7657 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
ed957684 7658 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7659 /* Determine how long we might wait for the active mailbox
7660 * command to be gracefully completed by firmware.
7661 */
7662 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, actcmd) *
7663 1000) + jiffies;
7664 while (phba->sli.mbox_active) {
7665 /* Check active mailbox complete status every 2ms */
7666 msleep(2);
7667 if (time_after(jiffies, timeout))
7668 /* Timeout, let the mailbox flush routine to
7669 * forcefully release active mailbox command
7670 */
7671 break;
7672 }
7673 }
7674 lpfc_sli_mbox_sys_flush(phba);
7675}
ed957684 7676
3772a991
JS
7677/**
7678 * lpfc_sli_eratt_read - read sli-3 error attention events
7679 * @phba: Pointer to HBA context.
7680 *
7681 * This function is called to read the SLI3 device error attention registers
7682 * for possible error attention events. The caller must hold the hostlock
7683 * with spin_lock_irq().
7684 *
7685 * This fucntion returns 1 when there is Error Attention in the Host Attention
7686 * Register and returns 0 otherwise.
7687 **/
7688static int
7689lpfc_sli_eratt_read(struct lpfc_hba *phba)
7690{
7691 uint32_t ha_copy;
b4c02652 7692
3772a991
JS
7693 /* Read chip Host Attention (HA) register */
7694 ha_copy = readl(phba->HAregaddr);
7695 if (ha_copy & HA_ERATT) {
7696 /* Read host status register to retrieve error event */
7697 lpfc_sli_read_hs(phba);
b4c02652 7698
3772a991
JS
7699 /* Check if there is a deferred error condition is active */
7700 if ((HS_FFER1 & phba->work_hs) &&
7701 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
7702 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
3772a991 7703 phba->hba_flag |= DEFER_ERATT;
3772a991
JS
7704 /* Clear all interrupt enable conditions */
7705 writel(0, phba->HCregaddr);
7706 readl(phba->HCregaddr);
7707 }
7708
7709 /* Set the driver HA work bitmap */
3772a991
JS
7710 phba->work_ha |= HA_ERATT;
7711 /* Indicate polling handles this ERATT */
7712 phba->hba_flag |= HBA_ERATT_HANDLED;
3772a991
JS
7713 return 1;
7714 }
7715 return 0;
b4c02652
JS
7716}
7717
da0436e9
JS
7718/**
7719 * lpfc_sli4_eratt_read - read sli-4 error attention events
7720 * @phba: Pointer to HBA context.
7721 *
7722 * This function is called to read the SLI4 device error attention registers
7723 * for possible error attention events. The caller must hold the hostlock
7724 * with spin_lock_irq().
7725 *
7726 * This fucntion returns 1 when there is Error Attention in the Host Attention
7727 * Register and returns 0 otherwise.
7728 **/
7729static int
7730lpfc_sli4_eratt_read(struct lpfc_hba *phba)
7731{
7732 uint32_t uerr_sta_hi, uerr_sta_lo;
7733 uint32_t onlnreg0, onlnreg1;
7734
7735 /* For now, use the SLI4 device internal unrecoverable error
7736 * registers for error attention. This can be changed later.
7737 */
7738 onlnreg0 = readl(phba->sli4_hba.ONLINE0regaddr);
7739 onlnreg1 = readl(phba->sli4_hba.ONLINE1regaddr);
7740 if ((onlnreg0 != LPFC_ONLINE_NERR) || (onlnreg1 != LPFC_ONLINE_NERR)) {
7741 uerr_sta_lo = readl(phba->sli4_hba.UERRLOregaddr);
7742 uerr_sta_hi = readl(phba->sli4_hba.UERRHIregaddr);
7743 if (uerr_sta_lo || uerr_sta_hi) {
7744 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7745 "1423 HBA Unrecoverable error: "
7746 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
7747 "online0_reg=0x%x, online1_reg=0x%x\n",
7748 uerr_sta_lo, uerr_sta_hi,
7749 onlnreg0, onlnreg1);
da0436e9
JS
7750 phba->work_status[0] = uerr_sta_lo;
7751 phba->work_status[1] = uerr_sta_hi;
da0436e9
JS
7752 /* Set the driver HA work bitmap */
7753 phba->work_ha |= HA_ERATT;
7754 /* Indicate polling handles this ERATT */
7755 phba->hba_flag |= HBA_ERATT_HANDLED;
da0436e9
JS
7756 return 1;
7757 }
7758 }
7759 return 0;
7760}
7761
e59058c4 7762/**
3621a710 7763 * lpfc_sli_check_eratt - check error attention events
9399627f
JS
7764 * @phba: Pointer to HBA context.
7765 *
3772a991 7766 * This function is called from timer soft interrupt context to check HBA's
9399627f
JS
7767 * error attention register bit for error attention events.
7768 *
7769 * This fucntion returns 1 when there is Error Attention in the Host Attention
7770 * Register and returns 0 otherwise.
7771 **/
7772int
7773lpfc_sli_check_eratt(struct lpfc_hba *phba)
7774{
7775 uint32_t ha_copy;
7776
7777 /* If somebody is waiting to handle an eratt, don't process it
7778 * here. The brdkill function will do this.
7779 */
7780 if (phba->link_flag & LS_IGNORE_ERATT)
7781 return 0;
7782
7783 /* Check if interrupt handler handles this ERATT */
7784 spin_lock_irq(&phba->hbalock);
7785 if (phba->hba_flag & HBA_ERATT_HANDLED) {
7786 /* Interrupt handler has handled ERATT */
7787 spin_unlock_irq(&phba->hbalock);
7788 return 0;
7789 }
7790
a257bf90
JS
7791 /*
7792 * If there is deferred error attention, do not check for error
7793 * attention
7794 */
7795 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7796 spin_unlock_irq(&phba->hbalock);
7797 return 0;
7798 }
7799
3772a991
JS
7800 /* If PCI channel is offline, don't process it */
7801 if (unlikely(pci_channel_offline(phba->pcidev))) {
9399627f 7802 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7803 return 0;
7804 }
7805
7806 switch (phba->sli_rev) {
7807 case LPFC_SLI_REV2:
7808 case LPFC_SLI_REV3:
7809 /* Read chip Host Attention (HA) register */
7810 ha_copy = lpfc_sli_eratt_read(phba);
7811 break;
da0436e9
JS
7812 case LPFC_SLI_REV4:
7813 /* Read devcie Uncoverable Error (UERR) registers */
7814 ha_copy = lpfc_sli4_eratt_read(phba);
7815 break;
3772a991
JS
7816 default:
7817 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7818 "0299 Invalid SLI revision (%d)\n",
7819 phba->sli_rev);
7820 ha_copy = 0;
7821 break;
9399627f
JS
7822 }
7823 spin_unlock_irq(&phba->hbalock);
3772a991
JS
7824
7825 return ha_copy;
7826}
7827
7828/**
7829 * lpfc_intr_state_check - Check device state for interrupt handling
7830 * @phba: Pointer to HBA context.
7831 *
7832 * This inline routine checks whether a device or its PCI slot is in a state
7833 * that the interrupt should be handled.
7834 *
7835 * This function returns 0 if the device or the PCI slot is in a state that
7836 * interrupt should be handled, otherwise -EIO.
7837 */
7838static inline int
7839lpfc_intr_state_check(struct lpfc_hba *phba)
7840{
7841 /* If the pci channel is offline, ignore all the interrupts */
7842 if (unlikely(pci_channel_offline(phba->pcidev)))
7843 return -EIO;
7844
7845 /* Update device level interrupt statistics */
7846 phba->sli.slistat.sli_intr++;
7847
7848 /* Ignore all interrupts during initialization. */
7849 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7850 return -EIO;
7851
9399627f
JS
7852 return 0;
7853}
7854
7855/**
3772a991 7856 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
e59058c4
JS
7857 * @irq: Interrupt number.
7858 * @dev_id: The device context pointer.
7859 *
9399627f 7860 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
7861 * service routine when device with SLI-3 interface spec is enabled with
7862 * MSI-X multi-message interrupt mode and there are slow-path events in
7863 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
7864 * interrupt mode, this function is called as part of the device-level
7865 * interrupt handler. When the PCI slot is in error recovery or the HBA
7866 * is undergoing initialization, the interrupt handler will not process
7867 * the interrupt. The link attention and ELS ring attention events are
7868 * handled by the worker thread. The interrupt handler signals the worker
7869 * thread and returns for these events. This function is called without
7870 * any lock held. It gets the hbalock to access and update SLI data
9399627f
JS
7871 * structures.
7872 *
7873 * This function returns IRQ_HANDLED when interrupt is handled else it
7874 * returns IRQ_NONE.
e59058c4 7875 **/
dea3101e 7876irqreturn_t
3772a991 7877lpfc_sli_sp_intr_handler(int irq, void *dev_id)
dea3101e 7878{
2e0fef85 7879 struct lpfc_hba *phba;
dea3101e 7880 uint32_t ha_copy;
7881 uint32_t work_ha_copy;
7882 unsigned long status;
5b75da2f 7883 unsigned long iflag;
dea3101e 7884 uint32_t control;
7885
92d7f7b0 7886 MAILBOX_t *mbox, *pmbox;
858c9f6c
JS
7887 struct lpfc_vport *vport;
7888 struct lpfc_nodelist *ndlp;
7889 struct lpfc_dmabuf *mp;
92d7f7b0
JS
7890 LPFC_MBOXQ_t *pmb;
7891 int rc;
7892
dea3101e 7893 /*
7894 * Get the driver's phba structure from the dev_id and
7895 * assume the HBA is not interrupting.
7896 */
9399627f 7897 phba = (struct lpfc_hba *)dev_id;
dea3101e 7898
7899 if (unlikely(!phba))
7900 return IRQ_NONE;
7901
dea3101e 7902 /*
9399627f
JS
7903 * Stuff needs to be attented to when this function is invoked as an
7904 * individual interrupt handler in MSI-X multi-message interrupt mode
dea3101e 7905 */
9399627f 7906 if (phba->intr_type == MSIX) {
3772a991
JS
7907 /* Check device state for handling interrupt */
7908 if (lpfc_intr_state_check(phba))
9399627f
JS
7909 return IRQ_NONE;
7910 /* Need to read HA REG for slow-path events */
5b75da2f 7911 spin_lock_irqsave(&phba->hbalock, iflag);
34b02dcd 7912 ha_copy = readl(phba->HAregaddr);
9399627f
JS
7913 /* If somebody is waiting to handle an eratt don't process it
7914 * here. The brdkill function will do this.
7915 */
7916 if (phba->link_flag & LS_IGNORE_ERATT)
7917 ha_copy &= ~HA_ERATT;
7918 /* Check the need for handling ERATT in interrupt handler */
7919 if (ha_copy & HA_ERATT) {
7920 if (phba->hba_flag & HBA_ERATT_HANDLED)
7921 /* ERATT polling has handled ERATT */
7922 ha_copy &= ~HA_ERATT;
7923 else
7924 /* Indicate interrupt handler handles ERATT */
7925 phba->hba_flag |= HBA_ERATT_HANDLED;
7926 }
a257bf90
JS
7927
7928 /*
7929 * If there is deferred error attention, do not check for any
7930 * interrupt.
7931 */
7932 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 7933 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
7934 return IRQ_NONE;
7935 }
7936
9399627f
JS
7937 /* Clear up only attention source related to slow-path */
7938 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
7939 phba->HAregaddr);
7940 readl(phba->HAregaddr); /* flush */
5b75da2f 7941 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
7942 } else
7943 ha_copy = phba->ha_copy;
dea3101e 7944
dea3101e 7945 work_ha_copy = ha_copy & phba->work_ha_mask;
7946
9399627f 7947 if (work_ha_copy) {
dea3101e 7948 if (work_ha_copy & HA_LATT) {
7949 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
7950 /*
7951 * Turn off Link Attention interrupts
7952 * until CLEAR_LA done
7953 */
5b75da2f 7954 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 7955 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
7956 control = readl(phba->HCregaddr);
7957 control &= ~HC_LAINT_ENA;
7958 writel(control, phba->HCregaddr);
7959 readl(phba->HCregaddr); /* flush */
5b75da2f 7960 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 7961 }
7962 else
7963 work_ha_copy &= ~HA_LATT;
7964 }
7965
9399627f 7966 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
858c9f6c
JS
7967 /*
7968 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
7969 * the only slow ring.
7970 */
7971 status = (work_ha_copy &
7972 (HA_RXMASK << (4*LPFC_ELS_RING)));
7973 status >>= (4*LPFC_ELS_RING);
7974 if (status & HA_RXMASK) {
5b75da2f 7975 spin_lock_irqsave(&phba->hbalock, iflag);
858c9f6c 7976 control = readl(phba->HCregaddr);
a58cbd52
JS
7977
7978 lpfc_debugfs_slow_ring_trc(phba,
7979 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
7980 control, status,
7981 (uint32_t)phba->sli.slistat.sli_intr);
7982
858c9f6c 7983 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
a58cbd52
JS
7984 lpfc_debugfs_slow_ring_trc(phba,
7985 "ISR Disable ring:"
7986 "pwork:x%x hawork:x%x wait:x%x",
7987 phba->work_ha, work_ha_copy,
7988 (uint32_t)((unsigned long)
5e9d9b82 7989 &phba->work_waitq));
a58cbd52 7990
858c9f6c
JS
7991 control &=
7992 ~(HC_R0INT_ENA << LPFC_ELS_RING);
dea3101e 7993 writel(control, phba->HCregaddr);
7994 readl(phba->HCregaddr); /* flush */
dea3101e 7995 }
a58cbd52
JS
7996 else {
7997 lpfc_debugfs_slow_ring_trc(phba,
7998 "ISR slow ring: pwork:"
7999 "x%x hawork:x%x wait:x%x",
8000 phba->work_ha, work_ha_copy,
8001 (uint32_t)((unsigned long)
5e9d9b82 8002 &phba->work_waitq));
a58cbd52 8003 }
5b75da2f 8004 spin_unlock_irqrestore(&phba->hbalock, iflag);
dea3101e 8005 }
8006 }
5b75da2f 8007 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90 8008 if (work_ha_copy & HA_ERATT) {
9399627f 8009 lpfc_sli_read_hs(phba);
a257bf90
JS
8010 /*
8011 * Check if there is a deferred error condition
8012 * is active
8013 */
8014 if ((HS_FFER1 & phba->work_hs) &&
8015 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
8016 HS_FFER6 | HS_FFER7) & phba->work_hs)) {
8017 phba->hba_flag |= DEFER_ERATT;
8018 /* Clear all interrupt enable conditions */
8019 writel(0, phba->HCregaddr);
8020 readl(phba->HCregaddr);
8021 }
8022 }
8023
9399627f 8024 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
92d7f7b0 8025 pmb = phba->sli.mbox_active;
04c68496 8026 pmbox = &pmb->u.mb;
34b02dcd 8027 mbox = phba->mbox;
858c9f6c 8028 vport = pmb->vport;
92d7f7b0
JS
8029
8030 /* First check out the status word */
8031 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
8032 if (pmbox->mbxOwner != OWN_HOST) {
5b75da2f 8033 spin_unlock_irqrestore(&phba->hbalock, iflag);
92d7f7b0
JS
8034 /*
8035 * Stray Mailbox Interrupt, mbxCommand <cmd>
8036 * mbxStatus <status>
8037 */
09372820 8038 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
92d7f7b0 8039 LOG_SLI,
e8b62011 8040 "(%d):0304 Stray Mailbox "
92d7f7b0
JS
8041 "Interrupt mbxCommand x%x "
8042 "mbxStatus x%x\n",
e8b62011 8043 (vport ? vport->vpi : 0),
92d7f7b0
JS
8044 pmbox->mbxCommand,
8045 pmbox->mbxStatus);
09372820
JS
8046 /* clear mailbox attention bit */
8047 work_ha_copy &= ~HA_MBATT;
8048 } else {
97eab634 8049 phba->sli.mbox_active = NULL;
5b75da2f 8050 spin_unlock_irqrestore(&phba->hbalock, iflag);
09372820
JS
8051 phba->last_completion_time = jiffies;
8052 del_timer(&phba->sli.mbox_tmo);
09372820
JS
8053 if (pmb->mbox_cmpl) {
8054 lpfc_sli_pcimem_bcopy(mbox, pmbox,
8055 MAILBOX_CMD_SIZE);
8056 }
8057 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8058 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8059
8060 lpfc_debugfs_disc_trc(vport,
8061 LPFC_DISC_TRC_MBOX_VPORT,
8062 "MBOX dflt rpi: : "
8063 "status:x%x rpi:x%x",
8064 (uint32_t)pmbox->mbxStatus,
8065 pmbox->un.varWords[0], 0);
8066
8067 if (!pmbox->mbxStatus) {
8068 mp = (struct lpfc_dmabuf *)
8069 (pmb->context1);
8070 ndlp = (struct lpfc_nodelist *)
8071 pmb->context2;
8072
8073 /* Reg_LOGIN of dflt RPI was
8074 * successful. new lets get
8075 * rid of the RPI using the
8076 * same mbox buffer.
8077 */
8078 lpfc_unreg_login(phba,
8079 vport->vpi,
8080 pmbox->un.varWords[0],
8081 pmb);
8082 pmb->mbox_cmpl =
8083 lpfc_mbx_cmpl_dflt_rpi;
8084 pmb->context1 = mp;
8085 pmb->context2 = ndlp;
8086 pmb->vport = vport;
58da1ffb
JS
8087 rc = lpfc_sli_issue_mbox(phba,
8088 pmb,
8089 MBX_NOWAIT);
8090 if (rc != MBX_BUSY)
8091 lpfc_printf_log(phba,
8092 KERN_ERR,
8093 LOG_MBOX | LOG_SLI,
d7c255b2 8094 "0350 rc should have"
6a9c52cf 8095 "been MBX_BUSY\n");
3772a991
JS
8096 if (rc != MBX_NOT_FINISHED)
8097 goto send_current_mbox;
09372820 8098 }
858c9f6c 8099 }
5b75da2f
JS
8100 spin_lock_irqsave(
8101 &phba->pport->work_port_lock,
8102 iflag);
09372820
JS
8103 phba->pport->work_port_events &=
8104 ~WORKER_MBOX_TMO;
5b75da2f
JS
8105 spin_unlock_irqrestore(
8106 &phba->pport->work_port_lock,
8107 iflag);
09372820 8108 lpfc_mbox_cmpl_put(phba, pmb);
858c9f6c 8109 }
97eab634 8110 } else
5b75da2f 8111 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f 8112
92d7f7b0
JS
8113 if ((work_ha_copy & HA_MBATT) &&
8114 (phba->sli.mbox_active == NULL)) {
858c9f6c 8115send_current_mbox:
92d7f7b0 8116 /* Process next mailbox command if there is one */
58da1ffb
JS
8117 do {
8118 rc = lpfc_sli_issue_mbox(phba, NULL,
8119 MBX_NOWAIT);
8120 } while (rc == MBX_NOT_FINISHED);
8121 if (rc != MBX_SUCCESS)
8122 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8123 LOG_SLI, "0349 rc should be "
6a9c52cf 8124 "MBX_SUCCESS\n");
92d7f7b0
JS
8125 }
8126
5b75da2f 8127 spin_lock_irqsave(&phba->hbalock, iflag);
dea3101e 8128 phba->work_ha |= work_ha_copy;
5b75da2f 8129 spin_unlock_irqrestore(&phba->hbalock, iflag);
5e9d9b82 8130 lpfc_worker_wake_up(phba);
dea3101e 8131 }
9399627f 8132 return IRQ_HANDLED;
dea3101e 8133
3772a991 8134} /* lpfc_sli_sp_intr_handler */
9399627f
JS
8135
8136/**
3772a991 8137 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
9399627f
JS
8138 * @irq: Interrupt number.
8139 * @dev_id: The device context pointer.
8140 *
8141 * This function is directly called from the PCI layer as an interrupt
3772a991
JS
8142 * service routine when device with SLI-3 interface spec is enabled with
8143 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
8144 * ring event in the HBA. However, when the device is enabled with either
8145 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
8146 * device-level interrupt handler. When the PCI slot is in error recovery
8147 * or the HBA is undergoing initialization, the interrupt handler will not
8148 * process the interrupt. The SCSI FCP fast-path ring event are handled in
8149 * the intrrupt context. This function is called without any lock held.
8150 * It gets the hbalock to access and update SLI data structures.
9399627f
JS
8151 *
8152 * This function returns IRQ_HANDLED when interrupt is handled else it
8153 * returns IRQ_NONE.
8154 **/
8155irqreturn_t
3772a991 8156lpfc_sli_fp_intr_handler(int irq, void *dev_id)
9399627f
JS
8157{
8158 struct lpfc_hba *phba;
8159 uint32_t ha_copy;
8160 unsigned long status;
5b75da2f 8161 unsigned long iflag;
9399627f
JS
8162
8163 /* Get the driver's phba structure from the dev_id and
8164 * assume the HBA is not interrupting.
8165 */
8166 phba = (struct lpfc_hba *) dev_id;
8167
8168 if (unlikely(!phba))
8169 return IRQ_NONE;
8170
8171 /*
8172 * Stuff needs to be attented to when this function is invoked as an
8173 * individual interrupt handler in MSI-X multi-message interrupt mode
8174 */
8175 if (phba->intr_type == MSIX) {
3772a991
JS
8176 /* Check device state for handling interrupt */
8177 if (lpfc_intr_state_check(phba))
9399627f
JS
8178 return IRQ_NONE;
8179 /* Need to read HA REG for FCP ring and other ring events */
8180 ha_copy = readl(phba->HAregaddr);
8181 /* Clear up only attention source related to fast-path */
5b75da2f 8182 spin_lock_irqsave(&phba->hbalock, iflag);
a257bf90
JS
8183 /*
8184 * If there is deferred error attention, do not check for
8185 * any interrupt.
8186 */
8187 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
3772a991 8188 spin_unlock_irqrestore(&phba->hbalock, iflag);
a257bf90
JS
8189 return IRQ_NONE;
8190 }
9399627f
JS
8191 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
8192 phba->HAregaddr);
8193 readl(phba->HAregaddr); /* flush */
5b75da2f 8194 spin_unlock_irqrestore(&phba->hbalock, iflag);
9399627f
JS
8195 } else
8196 ha_copy = phba->ha_copy;
dea3101e 8197
8198 /*
9399627f 8199 * Process all events on FCP ring. Take the optimized path for FCP IO.
dea3101e 8200 */
9399627f
JS
8201 ha_copy &= ~(phba->work_ha_mask);
8202
8203 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
dea3101e 8204 status >>= (4*LPFC_FCP_RING);
858c9f6c 8205 if (status & HA_RXMASK)
dea3101e 8206 lpfc_sli_handle_fast_ring_event(phba,
8207 &phba->sli.ring[LPFC_FCP_RING],
8208 status);
a4bc3379
JS
8209
8210 if (phba->cfg_multi_ring_support == 2) {
8211 /*
9399627f
JS
8212 * Process all events on extra ring. Take the optimized path
8213 * for extra ring IO.
a4bc3379 8214 */
9399627f 8215 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
a4bc3379 8216 status >>= (4*LPFC_EXTRA_RING);
858c9f6c 8217 if (status & HA_RXMASK) {
a4bc3379
JS
8218 lpfc_sli_handle_fast_ring_event(phba,
8219 &phba->sli.ring[LPFC_EXTRA_RING],
8220 status);
8221 }
8222 }
dea3101e 8223 return IRQ_HANDLED;
3772a991 8224} /* lpfc_sli_fp_intr_handler */
9399627f
JS
8225
8226/**
3772a991 8227 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
9399627f
JS
8228 * @irq: Interrupt number.
8229 * @dev_id: The device context pointer.
8230 *
3772a991
JS
8231 * This function is the HBA device-level interrupt handler to device with
8232 * SLI-3 interface spec, called from the PCI layer when either MSI or
8233 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
8234 * requires driver attention. This function invokes the slow-path interrupt
8235 * attention handling function and fast-path interrupt attention handling
8236 * function in turn to process the relevant HBA attention events. This
8237 * function is called without any lock held. It gets the hbalock to access
8238 * and update SLI data structures.
9399627f
JS
8239 *
8240 * This function returns IRQ_HANDLED when interrupt is handled, else it
8241 * returns IRQ_NONE.
8242 **/
8243irqreturn_t
3772a991 8244lpfc_sli_intr_handler(int irq, void *dev_id)
9399627f
JS
8245{
8246 struct lpfc_hba *phba;
8247 irqreturn_t sp_irq_rc, fp_irq_rc;
8248 unsigned long status1, status2;
8249
8250 /*
8251 * Get the driver's phba structure from the dev_id and
8252 * assume the HBA is not interrupting.
8253 */
8254 phba = (struct lpfc_hba *) dev_id;
8255
8256 if (unlikely(!phba))
8257 return IRQ_NONE;
8258
3772a991
JS
8259 /* Check device state for handling interrupt */
8260 if (lpfc_intr_state_check(phba))
9399627f
JS
8261 return IRQ_NONE;
8262
8263 spin_lock(&phba->hbalock);
8264 phba->ha_copy = readl(phba->HAregaddr);
8265 if (unlikely(!phba->ha_copy)) {
8266 spin_unlock(&phba->hbalock);
8267 return IRQ_NONE;
8268 } else if (phba->ha_copy & HA_ERATT) {
8269 if (phba->hba_flag & HBA_ERATT_HANDLED)
8270 /* ERATT polling has handled ERATT */
8271 phba->ha_copy &= ~HA_ERATT;
8272 else
8273 /* Indicate interrupt handler handles ERATT */
8274 phba->hba_flag |= HBA_ERATT_HANDLED;
8275 }
8276
a257bf90
JS
8277 /*
8278 * If there is deferred error attention, do not check for any interrupt.
8279 */
8280 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
8281 spin_unlock_irq(&phba->hbalock);
8282 return IRQ_NONE;
8283 }
8284
9399627f
JS
8285 /* Clear attention sources except link and error attentions */
8286 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
8287 readl(phba->HAregaddr); /* flush */
8288 spin_unlock(&phba->hbalock);
8289
8290 /*
8291 * Invokes slow-path host attention interrupt handling as appropriate.
8292 */
8293
8294 /* status of events with mailbox and link attention */
8295 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
8296
8297 /* status of events with ELS ring */
8298 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
8299 status2 >>= (4*LPFC_ELS_RING);
8300
8301 if (status1 || (status2 & HA_RXMASK))
3772a991 8302 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
9399627f
JS
8303 else
8304 sp_irq_rc = IRQ_NONE;
8305
8306 /*
8307 * Invoke fast-path host attention interrupt handling as appropriate.
8308 */
8309
8310 /* status of events with FCP ring */
8311 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
8312 status1 >>= (4*LPFC_FCP_RING);
8313
8314 /* status of events with extra ring */
8315 if (phba->cfg_multi_ring_support == 2) {
8316 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
8317 status2 >>= (4*LPFC_EXTRA_RING);
8318 } else
8319 status2 = 0;
8320
8321 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
3772a991 8322 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
9399627f
JS
8323 else
8324 fp_irq_rc = IRQ_NONE;
dea3101e 8325
9399627f
JS
8326 /* Return device-level interrupt handling status */
8327 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
3772a991 8328} /* lpfc_sli_intr_handler */
4f774513
JS
8329
8330/**
8331 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
8332 * @phba: pointer to lpfc hba data structure.
8333 *
8334 * This routine is invoked by the worker thread to process all the pending
8335 * SLI4 FCP abort XRI events.
8336 **/
8337void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
8338{
8339 struct lpfc_cq_event *cq_event;
8340
8341 /* First, declare the fcp xri abort event has been handled */
8342 spin_lock_irq(&phba->hbalock);
8343 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
8344 spin_unlock_irq(&phba->hbalock);
8345 /* Now, handle all the fcp xri abort events */
8346 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
8347 /* Get the first event from the head of the event queue */
8348 spin_lock_irq(&phba->hbalock);
8349 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
8350 cq_event, struct lpfc_cq_event, list);
8351 spin_unlock_irq(&phba->hbalock);
8352 /* Notify aborted XRI for FCP work queue */
8353 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8354 /* Free the event processed back to the free pool */
8355 lpfc_sli4_cq_event_release(phba, cq_event);
8356 }
8357}
8358
8359/**
8360 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
8361 * @phba: pointer to lpfc hba data structure.
8362 *
8363 * This routine is invoked by the worker thread to process all the pending
8364 * SLI4 els abort xri events.
8365 **/
8366void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
8367{
8368 struct lpfc_cq_event *cq_event;
8369
8370 /* First, declare the els xri abort event has been handled */
8371 spin_lock_irq(&phba->hbalock);
8372 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
8373 spin_unlock_irq(&phba->hbalock);
8374 /* Now, handle all the els xri abort events */
8375 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
8376 /* Get the first event from the head of the event queue */
8377 spin_lock_irq(&phba->hbalock);
8378 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
8379 cq_event, struct lpfc_cq_event, list);
8380 spin_unlock_irq(&phba->hbalock);
8381 /* Notify aborted XRI for ELS work queue */
8382 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
8383 /* Free the event processed back to the free pool */
8384 lpfc_sli4_cq_event_release(phba, cq_event);
8385 }
8386}
8387
8388static void
8389lpfc_sli4_iocb_param_transfer(struct lpfc_iocbq *pIocbIn,
8390 struct lpfc_iocbq *pIocbOut,
8391 struct lpfc_wcqe_complete *wcqe)
8392{
8393 size_t offset = offsetof(struct lpfc_iocbq, iocb);
8394
8395 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
8396 sizeof(struct lpfc_iocbq) - offset);
4d9ab994 8397 pIocbIn->cq_event.cqe.wcqe_cmpl = *wcqe;
4f774513
JS
8398 /* Map WCQE parameters into irspiocb parameters */
8399 pIocbIn->iocb.ulpStatus = bf_get(lpfc_wcqe_c_status, wcqe);
8400 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
8401 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
8402 pIocbIn->iocb.un.fcpi.fcpi_parm =
8403 pIocbOut->iocb.un.fcpi.fcpi_parm -
8404 wcqe->total_data_placed;
8405 else
8406 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
8407 else
8408 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
4f774513
JS
8409}
8410
04c68496
JS
8411/**
8412 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
8413 * @phba: Pointer to HBA context object.
8414 * @cqe: Pointer to mailbox completion queue entry.
8415 *
8416 * This routine process a mailbox completion queue entry with asynchrous
8417 * event.
8418 *
8419 * Return: true if work posted to worker thread, otherwise false.
8420 **/
8421static bool
8422lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8423{
8424 struct lpfc_cq_event *cq_event;
8425 unsigned long iflags;
8426
8427 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8428 "0392 Async Event: word0:x%x, word1:x%x, "
8429 "word2:x%x, word3:x%x\n", mcqe->word0,
8430 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
8431
8432 /* Allocate a new internal CQ_EVENT entry */
8433 cq_event = lpfc_sli4_cq_event_alloc(phba);
8434 if (!cq_event) {
8435 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8436 "0394 Failed to allocate CQ_EVENT entry\n");
8437 return false;
8438 }
8439
8440 /* Move the CQE into an asynchronous event entry */
8441 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
8442 spin_lock_irqsave(&phba->hbalock, iflags);
8443 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
8444 /* Set the async event flag */
8445 phba->hba_flag |= ASYNC_EVENT;
8446 spin_unlock_irqrestore(&phba->hbalock, iflags);
8447
8448 return true;
8449}
8450
8451/**
8452 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
8453 * @phba: Pointer to HBA context object.
8454 * @cqe: Pointer to mailbox completion queue entry.
8455 *
8456 * This routine process a mailbox completion queue entry with mailbox
8457 * completion event.
8458 *
8459 * Return: true if work posted to worker thread, otherwise false.
8460 **/
8461static bool
8462lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
8463{
8464 uint32_t mcqe_status;
8465 MAILBOX_t *mbox, *pmbox;
8466 struct lpfc_mqe *mqe;
8467 struct lpfc_vport *vport;
8468 struct lpfc_nodelist *ndlp;
8469 struct lpfc_dmabuf *mp;
8470 unsigned long iflags;
8471 LPFC_MBOXQ_t *pmb;
8472 bool workposted = false;
8473 int rc;
8474
8475 /* If not a mailbox complete MCQE, out by checking mailbox consume */
8476 if (!bf_get(lpfc_trailer_completed, mcqe))
8477 goto out_no_mqe_complete;
8478
8479 /* Get the reference to the active mbox command */
8480 spin_lock_irqsave(&phba->hbalock, iflags);
8481 pmb = phba->sli.mbox_active;
8482 if (unlikely(!pmb)) {
8483 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
8484 "1832 No pending MBOX command to handle\n");
8485 spin_unlock_irqrestore(&phba->hbalock, iflags);
8486 goto out_no_mqe_complete;
8487 }
8488 spin_unlock_irqrestore(&phba->hbalock, iflags);
8489 mqe = &pmb->u.mqe;
8490 pmbox = (MAILBOX_t *)&pmb->u.mqe;
8491 mbox = phba->mbox;
8492 vport = pmb->vport;
8493
8494 /* Reset heartbeat timer */
8495 phba->last_completion_time = jiffies;
8496 del_timer(&phba->sli.mbox_tmo);
8497
8498 /* Move mbox data to caller's mailbox region, do endian swapping */
8499 if (pmb->mbox_cmpl && mbox)
8500 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
8501 /* Set the mailbox status with SLI4 range 0x4000 */
8502 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
8503 if (mcqe_status != MB_CQE_STATUS_SUCCESS)
8504 bf_set(lpfc_mqe_status, mqe,
8505 (LPFC_MBX_ERROR_RANGE | mcqe_status));
8506
8507 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
8508 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
8509 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
8510 "MBOX dflt rpi: status:x%x rpi:x%x",
8511 mcqe_status,
8512 pmbox->un.varWords[0], 0);
8513 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
8514 mp = (struct lpfc_dmabuf *)(pmb->context1);
8515 ndlp = (struct lpfc_nodelist *)pmb->context2;
8516 /* Reg_LOGIN of dflt RPI was successful. Now lets get
8517 * RID of the PPI using the same mbox buffer.
8518 */
8519 lpfc_unreg_login(phba, vport->vpi,
8520 pmbox->un.varWords[0], pmb);
8521 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
8522 pmb->context1 = mp;
8523 pmb->context2 = ndlp;
8524 pmb->vport = vport;
8525 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
8526 if (rc != MBX_BUSY)
8527 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
8528 LOG_SLI, "0385 rc should "
8529 "have been MBX_BUSY\n");
8530 if (rc != MBX_NOT_FINISHED)
8531 goto send_current_mbox;
8532 }
8533 }
8534 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
8535 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
8536 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
8537
8538 /* There is mailbox completion work to do */
8539 spin_lock_irqsave(&phba->hbalock, iflags);
8540 __lpfc_mbox_cmpl_put(phba, pmb);
8541 phba->work_ha |= HA_MBATT;
8542 spin_unlock_irqrestore(&phba->hbalock, iflags);
8543 workposted = true;
8544
8545send_current_mbox:
8546 spin_lock_irqsave(&phba->hbalock, iflags);
8547 /* Release the mailbox command posting token */
8548 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8549 /* Setting active mailbox pointer need to be in sync to flag clear */
8550 phba->sli.mbox_active = NULL;
8551 spin_unlock_irqrestore(&phba->hbalock, iflags);
8552 /* Wake up worker thread to post the next pending mailbox command */
8553 lpfc_worker_wake_up(phba);
8554out_no_mqe_complete:
8555 if (bf_get(lpfc_trailer_consumed, mcqe))
8556 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
8557 return workposted;
8558}
8559
8560/**
8561 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
8562 * @phba: Pointer to HBA context object.
8563 * @cqe: Pointer to mailbox completion queue entry.
8564 *
8565 * This routine process a mailbox completion queue entry, it invokes the
8566 * proper mailbox complete handling or asynchrous event handling routine
8567 * according to the MCQE's async bit.
8568 *
8569 * Return: true if work posted to worker thread, otherwise false.
8570 **/
8571static bool
8572lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
8573{
8574 struct lpfc_mcqe mcqe;
8575 bool workposted;
8576
8577 /* Copy the mailbox MCQE and convert endian order as needed */
8578 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
8579
8580 /* Invoke the proper event handling routine */
8581 if (!bf_get(lpfc_trailer_async, &mcqe))
8582 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
8583 else
8584 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
8585 return workposted;
8586}
8587
4f774513
JS
8588/**
8589 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
8590 * @phba: Pointer to HBA context object.
8591 * @wcqe: Pointer to work-queue completion queue entry.
8592 *
8593 * This routine handles an ELS work-queue completion event.
8594 *
8595 * Return: true if work posted to worker thread, otherwise false.
8596 **/
8597static bool
8598lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba,
8599 struct lpfc_wcqe_complete *wcqe)
8600{
8601 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
8602 struct lpfc_iocbq *cmdiocbq;
8603 struct lpfc_iocbq *irspiocbq;
8604 unsigned long iflags;
8605 bool workposted = false;
8606
8607 spin_lock_irqsave(&phba->hbalock, iflags);
8608 pring->stats.iocb_event++;
8609 /* Look up the ELS command IOCB and create pseudo response IOCB */
8610 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8611 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8612 spin_unlock_irqrestore(&phba->hbalock, iflags);
8613
8614 if (unlikely(!cmdiocbq)) {
8615 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8616 "0386 ELS complete with no corresponding "
8617 "cmdiocb: iotag (%d)\n",
8618 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8619 return workposted;
8620 }
8621
8622 /* Fake the irspiocbq and copy necessary response information */
8623 irspiocbq = lpfc_sli_get_iocbq(phba);
8624 if (!irspiocbq) {
8625 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8626 "0387 Failed to allocate an iocbq\n");
8627 return workposted;
8628 }
8629 lpfc_sli4_iocb_param_transfer(irspiocbq, cmdiocbq, wcqe);
8630
8631 /* Add the irspiocb to the response IOCB work list */
8632 spin_lock_irqsave(&phba->hbalock, iflags);
4d9ab994
JS
8633 list_add_tail(&irspiocbq->cq_event.list,
8634 &phba->sli4_hba.sp_rspiocb_work_queue);
4f774513
JS
8635 /* Indicate ELS ring attention */
8636 phba->work_ha |= (HA_R0ATT << (4*LPFC_ELS_RING));
8637 spin_unlock_irqrestore(&phba->hbalock, iflags);
8638 workposted = true;
8639
8640 return workposted;
8641}
8642
8643/**
8644 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
8645 * @phba: Pointer to HBA context object.
8646 * @wcqe: Pointer to work-queue completion queue entry.
8647 *
8648 * This routine handles slow-path WQ entry comsumed event by invoking the
8649 * proper WQ release routine to the slow-path WQ.
8650 **/
8651static void
8652lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
8653 struct lpfc_wcqe_release *wcqe)
8654{
8655 /* Check for the slow-path ELS work queue */
8656 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
8657 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
8658 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
8659 else
8660 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8661 "2579 Slow-path wqe consume event carries "
8662 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
8663 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
8664 phba->sli4_hba.els_wq->queue_id);
8665}
8666
8667/**
8668 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
8669 * @phba: Pointer to HBA context object.
8670 * @cq: Pointer to a WQ completion queue.
8671 * @wcqe: Pointer to work-queue completion queue entry.
8672 *
8673 * This routine handles an XRI abort event.
8674 *
8675 * Return: true if work posted to worker thread, otherwise false.
8676 **/
8677static bool
8678lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
8679 struct lpfc_queue *cq,
8680 struct sli4_wcqe_xri_aborted *wcqe)
8681{
8682 bool workposted = false;
8683 struct lpfc_cq_event *cq_event;
8684 unsigned long iflags;
8685
8686 /* Allocate a new internal CQ_EVENT entry */
8687 cq_event = lpfc_sli4_cq_event_alloc(phba);
8688 if (!cq_event) {
8689 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8690 "0602 Failed to allocate CQ_EVENT entry\n");
8691 return false;
8692 }
8693
8694 /* Move the CQE into the proper xri abort event list */
8695 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
8696 switch (cq->subtype) {
8697 case LPFC_FCP:
8698 spin_lock_irqsave(&phba->hbalock, iflags);
8699 list_add_tail(&cq_event->list,
8700 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
8701 /* Set the fcp xri abort event flag */
8702 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
8703 spin_unlock_irqrestore(&phba->hbalock, iflags);
8704 workposted = true;
8705 break;
8706 case LPFC_ELS:
8707 spin_lock_irqsave(&phba->hbalock, iflags);
8708 list_add_tail(&cq_event->list,
8709 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
8710 /* Set the els xri abort event flag */
8711 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
8712 spin_unlock_irqrestore(&phba->hbalock, iflags);
8713 workposted = true;
8714 break;
8715 default:
8716 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8717 "0603 Invalid work queue CQE subtype (x%x)\n",
8718 cq->subtype);
8719 workposted = false;
8720 break;
8721 }
8722 return workposted;
8723}
8724
4f774513
JS
8725/**
8726 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
8727 * @phba: Pointer to HBA context object.
8728 * @rcqe: Pointer to receive-queue completion queue entry.
8729 *
8730 * This routine process a receive-queue completion queue entry.
8731 *
8732 * Return: true if work posted to worker thread, otherwise false.
8733 **/
8734static bool
4d9ab994 8735lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
4f774513 8736{
4f774513
JS
8737 bool workposted = false;
8738 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
8739 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
8740 struct hbq_dmabuf *dma_buf;
8741 uint32_t status;
8742 unsigned long iflags;
8743
4f774513 8744 lpfc_sli4_rq_release(hrq, drq);
4d9ab994 8745 if (bf_get(lpfc_rcqe_code, rcqe) != CQE_CODE_RECEIVE)
4f774513 8746 goto out;
4d9ab994 8747 if (bf_get(lpfc_rcqe_rq_id, rcqe) != hrq->queue_id)
4f774513
JS
8748 goto out;
8749
4d9ab994 8750 status = bf_get(lpfc_rcqe_status, rcqe);
4f774513
JS
8751 switch (status) {
8752 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
8753 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8754 "2537 Receive Frame Truncated!!\n");
8755 case FC_STATUS_RQ_SUCCESS:
8756 spin_lock_irqsave(&phba->hbalock, iflags);
8757 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
8758 if (!dma_buf) {
8759 spin_unlock_irqrestore(&phba->hbalock, iflags);
8760 goto out;
8761 }
4d9ab994 8762 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
4f774513 8763 /* save off the frame for the word thread to process */
4d9ab994
JS
8764 list_add_tail(&dma_buf->cq_event.list,
8765 &phba->sli4_hba.sp_rspiocb_work_queue);
4f774513
JS
8766 /* Frame received */
8767 phba->hba_flag |= HBA_RECEIVE_BUFFER;
8768 spin_unlock_irqrestore(&phba->hbalock, iflags);
8769 workposted = true;
8770 break;
8771 case FC_STATUS_INSUFF_BUF_NEED_BUF:
8772 case FC_STATUS_INSUFF_BUF_FRM_DISC:
8773 /* Post more buffers if possible */
8774 spin_lock_irqsave(&phba->hbalock, iflags);
8775 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
8776 spin_unlock_irqrestore(&phba->hbalock, iflags);
8777 workposted = true;
8778 break;
8779 }
8780out:
8781 return workposted;
8782
8783}
8784
4d9ab994
JS
8785/**
8786 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
8787 * @phba: Pointer to HBA context object.
8788 * @cq: Pointer to the completion queue.
8789 * @wcqe: Pointer to a completion queue entry.
8790 *
8791 * This routine process a slow-path work-queue or recieve queue completion queue
8792 * entry.
8793 *
8794 * Return: true if work posted to worker thread, otherwise false.
8795 **/
8796static bool
8797lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
8798 struct lpfc_cqe *cqe)
8799{
8800 struct lpfc_wcqe_complete wcqe;
8801 bool workposted = false;
8802
8803 /* Copy the work queue CQE and convert endian order if needed */
8804 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
8805
8806 /* Check and process for different type of WCQE and dispatch */
8807 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
8808 case CQE_CODE_COMPL_WQE:
8809 /* Process the WQ complete event */
8810 workposted = lpfc_sli4_sp_handle_els_wcqe(phba,
8811 (struct lpfc_wcqe_complete *)&wcqe);
8812 break;
8813 case CQE_CODE_RELEASE_WQE:
8814 /* Process the WQ release event */
8815 lpfc_sli4_sp_handle_rel_wcqe(phba,
8816 (struct lpfc_wcqe_release *)&wcqe);
8817 break;
8818 case CQE_CODE_XRI_ABORTED:
8819 /* Process the WQ XRI abort event */
8820 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
8821 (struct sli4_wcqe_xri_aborted *)&wcqe);
8822 break;
8823 case CQE_CODE_RECEIVE:
8824 /* Process the RQ event */
8825 workposted = lpfc_sli4_sp_handle_rcqe(phba,
8826 (struct lpfc_rcqe *)&wcqe);
8827 break;
8828 default:
8829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8830 "0388 Not a valid WCQE code: x%x\n",
8831 bf_get(lpfc_wcqe_c_code, &wcqe));
8832 break;
8833 }
8834 return workposted;
8835}
8836
4f774513
JS
8837/**
8838 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
8839 * @phba: Pointer to HBA context object.
8840 * @eqe: Pointer to fast-path event queue entry.
8841 *
8842 * This routine process a event queue entry from the slow-path event queue.
8843 * It will check the MajorCode and MinorCode to determine this is for a
8844 * completion event on a completion queue, if not, an error shall be logged
8845 * and just return. Otherwise, it will get to the corresponding completion
8846 * queue and process all the entries on that completion queue, rearm the
8847 * completion queue, and then return.
8848 *
8849 **/
8850static void
8851lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
8852{
8853 struct lpfc_queue *cq = NULL, *childq, *speq;
8854 struct lpfc_cqe *cqe;
8855 bool workposted = false;
8856 int ecount = 0;
8857 uint16_t cqid;
8858
8859 if (bf_get(lpfc_eqe_major_code, eqe) != 0 ||
8860 bf_get(lpfc_eqe_minor_code, eqe) != 0) {
8861 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8862 "0359 Not a valid slow-path completion "
8863 "event: majorcode=x%x, minorcode=x%x\n",
8864 bf_get(lpfc_eqe_major_code, eqe),
8865 bf_get(lpfc_eqe_minor_code, eqe));
8866 return;
8867 }
8868
8869 /* Get the reference to the corresponding CQ */
8870 cqid = bf_get(lpfc_eqe_resource_id, eqe);
8871
8872 /* Search for completion queue pointer matching this cqid */
8873 speq = phba->sli4_hba.sp_eq;
8874 list_for_each_entry(childq, &speq->child_list, list) {
8875 if (childq->queue_id == cqid) {
8876 cq = childq;
8877 break;
8878 }
8879 }
8880 if (unlikely(!cq)) {
8881 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8882 "0365 Slow-path CQ identifier (%d) does "
8883 "not exist\n", cqid);
8884 return;
8885 }
8886
8887 /* Process all the entries to the CQ */
8888 switch (cq->type) {
8889 case LPFC_MCQ:
8890 while ((cqe = lpfc_sli4_cq_get(cq))) {
8891 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
8892 if (!(++ecount % LPFC_GET_QE_REL_INT))
8893 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8894 }
8895 break;
8896 case LPFC_WCQ:
8897 while ((cqe = lpfc_sli4_cq_get(cq))) {
4d9ab994 8898 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq, cqe);
4f774513
JS
8899 if (!(++ecount % LPFC_GET_QE_REL_INT))
8900 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
8901 }
8902 break;
8903 default:
8904 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8905 "0370 Invalid completion queue type (%d)\n",
8906 cq->type);
8907 return;
8908 }
8909
8910 /* Catch the no cq entry condition, log an error */
8911 if (unlikely(ecount == 0))
8912 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8913 "0371 No entry from the CQ: identifier "
8914 "(x%x), type (%d)\n", cq->queue_id, cq->type);
8915
8916 /* In any case, flash and re-arm the RCQ */
8917 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
8918
8919 /* wake up worker thread if there are works to be done */
8920 if (workposted)
8921 lpfc_worker_wake_up(phba);
8922}
8923
8924/**
8925 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
8926 * @eqe: Pointer to fast-path completion queue entry.
8927 *
8928 * This routine process a fast-path work queue completion entry from fast-path
8929 * event queue for FCP command response completion.
8930 **/
8931static void
8932lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba,
8933 struct lpfc_wcqe_complete *wcqe)
8934{
8935 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_FCP_RING];
8936 struct lpfc_iocbq *cmdiocbq;
8937 struct lpfc_iocbq irspiocbq;
8938 unsigned long iflags;
8939
8940 spin_lock_irqsave(&phba->hbalock, iflags);
8941 pring->stats.iocb_event++;
8942 spin_unlock_irqrestore(&phba->hbalock, iflags);
8943
8944 /* Check for response status */
8945 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
8946 /* If resource errors reported from HBA, reduce queue
8947 * depth of the SCSI device.
8948 */
8949 if ((bf_get(lpfc_wcqe_c_status, wcqe) ==
8950 IOSTAT_LOCAL_REJECT) &&
8951 (wcqe->parameter == IOERR_NO_RESOURCES)) {
8952 phba->lpfc_rampdown_queue_depth(phba);
8953 }
8954 /* Log the error status */
8955 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8956 "0373 FCP complete error: status=x%x, "
8957 "hw_status=x%x, total_data_specified=%d, "
8958 "parameter=x%x, word3=x%x\n",
8959 bf_get(lpfc_wcqe_c_status, wcqe),
8960 bf_get(lpfc_wcqe_c_hw_status, wcqe),
8961 wcqe->total_data_placed, wcqe->parameter,
8962 wcqe->word3);
8963 }
8964
8965 /* Look up the FCP command IOCB and create pseudo response IOCB */
8966 spin_lock_irqsave(&phba->hbalock, iflags);
8967 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
8968 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8969 spin_unlock_irqrestore(&phba->hbalock, iflags);
8970 if (unlikely(!cmdiocbq)) {
8971 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8972 "0374 FCP complete with no corresponding "
8973 "cmdiocb: iotag (%d)\n",
8974 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8975 return;
8976 }
8977 if (unlikely(!cmdiocbq->iocb_cmpl)) {
8978 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8979 "0375 FCP cmdiocb not callback function "
8980 "iotag: (%d)\n",
8981 bf_get(lpfc_wcqe_c_request_tag, wcqe));
8982 return;
8983 }
8984
8985 /* Fake the irspiocb and copy necessary response information */
8986 lpfc_sli4_iocb_param_transfer(&irspiocbq, cmdiocbq, wcqe);
8987
8988 /* Pass the cmd_iocb and the rsp state to the upper layer */
8989 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
8990}
8991
8992/**
8993 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
8994 * @phba: Pointer to HBA context object.
8995 * @cq: Pointer to completion queue.
8996 * @wcqe: Pointer to work-queue completion queue entry.
8997 *
8998 * This routine handles an fast-path WQ entry comsumed event by invoking the
8999 * proper WQ release routine to the slow-path WQ.
9000 **/
9001static void
9002lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9003 struct lpfc_wcqe_release *wcqe)
9004{
9005 struct lpfc_queue *childwq;
9006 bool wqid_matched = false;
9007 uint16_t fcp_wqid;
9008
9009 /* Check for fast-path FCP work queue release */
9010 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
9011 list_for_each_entry(childwq, &cq->child_list, list) {
9012 if (childwq->queue_id == fcp_wqid) {
9013 lpfc_sli4_wq_release(childwq,
9014 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
9015 wqid_matched = true;
9016 break;
9017 }
9018 }
9019 /* Report warning log message if no match found */
9020 if (wqid_matched != true)
9021 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9022 "2580 Fast-path wqe consume event carries "
9023 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
9024}
9025
9026/**
9027 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
9028 * @cq: Pointer to the completion queue.
9029 * @eqe: Pointer to fast-path completion queue entry.
9030 *
9031 * This routine process a fast-path work queue completion entry from fast-path
9032 * event queue for FCP command response completion.
9033 **/
9034static int
9035lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
9036 struct lpfc_cqe *cqe)
9037{
9038 struct lpfc_wcqe_release wcqe;
9039 bool workposted = false;
9040
9041 /* Copy the work queue CQE and convert endian order if needed */
9042 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
9043
9044 /* Check and process for different type of WCQE and dispatch */
9045 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
9046 case CQE_CODE_COMPL_WQE:
9047 /* Process the WQ complete event */
9048 lpfc_sli4_fp_handle_fcp_wcqe(phba,
9049 (struct lpfc_wcqe_complete *)&wcqe);
9050 break;
9051 case CQE_CODE_RELEASE_WQE:
9052 /* Process the WQ release event */
9053 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
9054 (struct lpfc_wcqe_release *)&wcqe);
9055 break;
9056 case CQE_CODE_XRI_ABORTED:
9057 /* Process the WQ XRI abort event */
9058 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
9059 (struct sli4_wcqe_xri_aborted *)&wcqe);
9060 break;
9061 default:
9062 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9063 "0144 Not a valid WCQE code: x%x\n",
9064 bf_get(lpfc_wcqe_c_code, &wcqe));
9065 break;
9066 }
9067 return workposted;
9068}
9069
9070/**
9071 * lpfc_sli4_fp_handle_eqe - Process a fast-path event queue entry
9072 * @phba: Pointer to HBA context object.
9073 * @eqe: Pointer to fast-path event queue entry.
9074 *
9075 * This routine process a event queue entry from the fast-path event queue.
9076 * It will check the MajorCode and MinorCode to determine this is for a
9077 * completion event on a completion queue, if not, an error shall be logged
9078 * and just return. Otherwise, it will get to the corresponding completion
9079 * queue and process all the entries on the completion queue, rearm the
9080 * completion queue, and then return.
9081 **/
9082static void
9083lpfc_sli4_fp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
9084 uint32_t fcp_cqidx)
9085{
9086 struct lpfc_queue *cq;
9087 struct lpfc_cqe *cqe;
9088 bool workposted = false;
9089 uint16_t cqid;
9090 int ecount = 0;
9091
9092 if (unlikely(bf_get(lpfc_eqe_major_code, eqe) != 0) ||
9093 unlikely(bf_get(lpfc_eqe_minor_code, eqe) != 0)) {
9094 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9095 "0366 Not a valid fast-path completion "
9096 "event: majorcode=x%x, minorcode=x%x\n",
9097 bf_get(lpfc_eqe_major_code, eqe),
9098 bf_get(lpfc_eqe_minor_code, eqe));
9099 return;
9100 }
9101
9102 cq = phba->sli4_hba.fcp_cq[fcp_cqidx];
9103 if (unlikely(!cq)) {
9104 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9105 "0367 Fast-path completion queue does not "
9106 "exist\n");
9107 return;
9108 }
9109
9110 /* Get the reference to the corresponding CQ */
9111 cqid = bf_get(lpfc_eqe_resource_id, eqe);
9112 if (unlikely(cqid != cq->queue_id)) {
9113 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9114 "0368 Miss-matched fast-path completion "
9115 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
9116 cqid, cq->queue_id);
9117 return;
9118 }
9119
9120 /* Process all the entries to the CQ */
9121 while ((cqe = lpfc_sli4_cq_get(cq))) {
9122 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
9123 if (!(++ecount % LPFC_GET_QE_REL_INT))
9124 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
9125 }
9126
9127 /* Catch the no cq entry condition */
9128 if (unlikely(ecount == 0))
9129 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9130 "0369 No entry from fast-path completion "
9131 "queue fcpcqid=%d\n", cq->queue_id);
9132
9133 /* In any case, flash and re-arm the CQ */
9134 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
9135
9136 /* wake up worker thread if there are works to be done */
9137 if (workposted)
9138 lpfc_worker_wake_up(phba);
9139}
9140
9141static void
9142lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
9143{
9144 struct lpfc_eqe *eqe;
9145
9146 /* walk all the EQ entries and drop on the floor */
9147 while ((eqe = lpfc_sli4_eq_get(eq)))
9148 ;
9149
9150 /* Clear and re-arm the EQ */
9151 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
9152}
9153
9154/**
9155 * lpfc_sli4_sp_intr_handler - Slow-path interrupt handler to SLI-4 device
9156 * @irq: Interrupt number.
9157 * @dev_id: The device context pointer.
9158 *
9159 * This function is directly called from the PCI layer as an interrupt
9160 * service routine when device with SLI-4 interface spec is enabled with
9161 * MSI-X multi-message interrupt mode and there are slow-path events in
9162 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
9163 * interrupt mode, this function is called as part of the device-level
9164 * interrupt handler. When the PCI slot is in error recovery or the HBA is
9165 * undergoing initialization, the interrupt handler will not process the
9166 * interrupt. The link attention and ELS ring attention events are handled
9167 * by the worker thread. The interrupt handler signals the worker thread
9168 * and returns for these events. This function is called without any lock
9169 * held. It gets the hbalock to access and update SLI data structures.
9170 *
9171 * This function returns IRQ_HANDLED when interrupt is handled else it
9172 * returns IRQ_NONE.
9173 **/
9174irqreturn_t
9175lpfc_sli4_sp_intr_handler(int irq, void *dev_id)
9176{
9177 struct lpfc_hba *phba;
9178 struct lpfc_queue *speq;
9179 struct lpfc_eqe *eqe;
9180 unsigned long iflag;
9181 int ecount = 0;
9182
9183 /*
9184 * Get the driver's phba structure from the dev_id
9185 */
9186 phba = (struct lpfc_hba *)dev_id;
9187
9188 if (unlikely(!phba))
9189 return IRQ_NONE;
9190
9191 /* Get to the EQ struct associated with this vector */
9192 speq = phba->sli4_hba.sp_eq;
9193
9194 /* Check device state for handling interrupt */
9195 if (unlikely(lpfc_intr_state_check(phba))) {
9196 /* Check again for link_state with lock held */
9197 spin_lock_irqsave(&phba->hbalock, iflag);
9198 if (phba->link_state < LPFC_LINK_DOWN)
9199 /* Flush, clear interrupt, and rearm the EQ */
9200 lpfc_sli4_eq_flush(phba, speq);
9201 spin_unlock_irqrestore(&phba->hbalock, iflag);
9202 return IRQ_NONE;
9203 }
9204
9205 /*
9206 * Process all the event on FCP slow-path EQ
9207 */
9208 while ((eqe = lpfc_sli4_eq_get(speq))) {
9209 lpfc_sli4_sp_handle_eqe(phba, eqe);
9210 if (!(++ecount % LPFC_GET_QE_REL_INT))
9211 lpfc_sli4_eq_release(speq, LPFC_QUEUE_NOARM);
9212 }
9213
9214 /* Always clear and re-arm the slow-path EQ */
9215 lpfc_sli4_eq_release(speq, LPFC_QUEUE_REARM);
9216
9217 /* Catch the no cq entry condition */
9218 if (unlikely(ecount == 0)) {
9219 if (phba->intr_type == MSIX)
9220 /* MSI-X treated interrupt served as no EQ share INT */
9221 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9222 "0357 MSI-X interrupt with no EQE\n");
9223 else
9224 /* Non MSI-X treated on interrupt as EQ share INT */
9225 return IRQ_NONE;
9226 }
9227
9228 return IRQ_HANDLED;
9229} /* lpfc_sli4_sp_intr_handler */
9230
9231/**
9232 * lpfc_sli4_fp_intr_handler - Fast-path interrupt handler to SLI-4 device
9233 * @irq: Interrupt number.
9234 * @dev_id: The device context pointer.
9235 *
9236 * This function is directly called from the PCI layer as an interrupt
9237 * service routine when device with SLI-4 interface spec is enabled with
9238 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
9239 * ring event in the HBA. However, when the device is enabled with either
9240 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
9241 * device-level interrupt handler. When the PCI slot is in error recovery
9242 * or the HBA is undergoing initialization, the interrupt handler will not
9243 * process the interrupt. The SCSI FCP fast-path ring event are handled in
9244 * the intrrupt context. This function is called without any lock held.
9245 * It gets the hbalock to access and update SLI data structures. Note that,
9246 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
9247 * equal to that of FCP CQ index.
9248 *
9249 * This function returns IRQ_HANDLED when interrupt is handled else it
9250 * returns IRQ_NONE.
9251 **/
9252irqreturn_t
9253lpfc_sli4_fp_intr_handler(int irq, void *dev_id)
9254{
9255 struct lpfc_hba *phba;
9256 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9257 struct lpfc_queue *fpeq;
9258 struct lpfc_eqe *eqe;
9259 unsigned long iflag;
9260 int ecount = 0;
9261 uint32_t fcp_eqidx;
9262
9263 /* Get the driver's phba structure from the dev_id */
9264 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
9265 phba = fcp_eq_hdl->phba;
9266 fcp_eqidx = fcp_eq_hdl->idx;
9267
9268 if (unlikely(!phba))
9269 return IRQ_NONE;
9270
9271 /* Get to the EQ struct associated with this vector */
9272 fpeq = phba->sli4_hba.fp_eq[fcp_eqidx];
9273
9274 /* Check device state for handling interrupt */
9275 if (unlikely(lpfc_intr_state_check(phba))) {
9276 /* Check again for link_state with lock held */
9277 spin_lock_irqsave(&phba->hbalock, iflag);
9278 if (phba->link_state < LPFC_LINK_DOWN)
9279 /* Flush, clear interrupt, and rearm the EQ */
9280 lpfc_sli4_eq_flush(phba, fpeq);
9281 spin_unlock_irqrestore(&phba->hbalock, iflag);
9282 return IRQ_NONE;
9283 }
9284
9285 /*
9286 * Process all the event on FCP fast-path EQ
9287 */
9288 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9289 lpfc_sli4_fp_handle_eqe(phba, eqe, fcp_eqidx);
9290 if (!(++ecount % LPFC_GET_QE_REL_INT))
9291 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
9292 }
9293
9294 /* Always clear and re-arm the fast-path EQ */
9295 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
9296
9297 if (unlikely(ecount == 0)) {
9298 if (phba->intr_type == MSIX)
9299 /* MSI-X treated interrupt served as no EQ share INT */
9300 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9301 "0358 MSI-X interrupt with no EQE\n");
9302 else
9303 /* Non MSI-X treated on interrupt as EQ share INT */
9304 return IRQ_NONE;
9305 }
9306
9307 return IRQ_HANDLED;
9308} /* lpfc_sli4_fp_intr_handler */
9309
9310/**
9311 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
9312 * @irq: Interrupt number.
9313 * @dev_id: The device context pointer.
9314 *
9315 * This function is the device-level interrupt handler to device with SLI-4
9316 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
9317 * interrupt mode is enabled and there is an event in the HBA which requires
9318 * driver attention. This function invokes the slow-path interrupt attention
9319 * handling function and fast-path interrupt attention handling function in
9320 * turn to process the relevant HBA attention events. This function is called
9321 * without any lock held. It gets the hbalock to access and update SLI data
9322 * structures.
9323 *
9324 * This function returns IRQ_HANDLED when interrupt is handled, else it
9325 * returns IRQ_NONE.
9326 **/
9327irqreturn_t
9328lpfc_sli4_intr_handler(int irq, void *dev_id)
9329{
9330 struct lpfc_hba *phba;
9331 irqreturn_t sp_irq_rc, fp_irq_rc;
9332 bool fp_handled = false;
9333 uint32_t fcp_eqidx;
9334
9335 /* Get the driver's phba structure from the dev_id */
9336 phba = (struct lpfc_hba *)dev_id;
9337
9338 if (unlikely(!phba))
9339 return IRQ_NONE;
9340
9341 /*
9342 * Invokes slow-path host attention interrupt handling as appropriate.
9343 */
9344 sp_irq_rc = lpfc_sli4_sp_intr_handler(irq, dev_id);
9345
9346 /*
9347 * Invoke fast-path host attention interrupt handling as appropriate.
9348 */
9349 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
9350 fp_irq_rc = lpfc_sli4_fp_intr_handler(irq,
9351 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
9352 if (fp_irq_rc == IRQ_HANDLED)
9353 fp_handled |= true;
9354 }
9355
9356 return (fp_handled == true) ? IRQ_HANDLED : sp_irq_rc;
9357} /* lpfc_sli4_intr_handler */
9358
9359/**
9360 * lpfc_sli4_queue_free - free a queue structure and associated memory
9361 * @queue: The queue structure to free.
9362 *
9363 * This function frees a queue structure and the DMAable memeory used for
9364 * the host resident queue. This function must be called after destroying the
9365 * queue on the HBA.
9366 **/
9367void
9368lpfc_sli4_queue_free(struct lpfc_queue *queue)
9369{
9370 struct lpfc_dmabuf *dmabuf;
9371
9372 if (!queue)
9373 return;
9374
9375 while (!list_empty(&queue->page_list)) {
9376 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
9377 list);
9378 dma_free_coherent(&queue->phba->pcidev->dev, PAGE_SIZE,
9379 dmabuf->virt, dmabuf->phys);
9380 kfree(dmabuf);
9381 }
9382 kfree(queue);
9383 return;
9384}
9385
9386/**
9387 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
9388 * @phba: The HBA that this queue is being created on.
9389 * @entry_size: The size of each queue entry for this queue.
9390 * @entry count: The number of entries that this queue will handle.
9391 *
9392 * This function allocates a queue structure and the DMAable memory used for
9393 * the host resident queue. This function must be called before creating the
9394 * queue on the HBA.
9395 **/
9396struct lpfc_queue *
9397lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
9398 uint32_t entry_count)
9399{
9400 struct lpfc_queue *queue;
9401 struct lpfc_dmabuf *dmabuf;
9402 int x, total_qe_count;
9403 void *dma_pointer;
9404
9405
9406 queue = kzalloc(sizeof(struct lpfc_queue) +
9407 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
9408 if (!queue)
9409 return NULL;
9410 queue->page_count = (PAGE_ALIGN(entry_size * entry_count))/PAGE_SIZE;
9411 INIT_LIST_HEAD(&queue->list);
9412 INIT_LIST_HEAD(&queue->page_list);
9413 INIT_LIST_HEAD(&queue->child_list);
9414 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
9415 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
9416 if (!dmabuf)
9417 goto out_fail;
9418 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
9419 PAGE_SIZE, &dmabuf->phys,
9420 GFP_KERNEL);
9421 if (!dmabuf->virt) {
9422 kfree(dmabuf);
9423 goto out_fail;
9424 }
d11e31dd 9425 memset(dmabuf->virt, 0, PAGE_SIZE);
4f774513
JS
9426 dmabuf->buffer_tag = x;
9427 list_add_tail(&dmabuf->list, &queue->page_list);
9428 /* initialize queue's entry array */
9429 dma_pointer = dmabuf->virt;
9430 for (; total_qe_count < entry_count &&
9431 dma_pointer < (PAGE_SIZE + dmabuf->virt);
9432 total_qe_count++, dma_pointer += entry_size) {
9433 queue->qe[total_qe_count].address = dma_pointer;
9434 }
9435 }
9436 queue->entry_size = entry_size;
9437 queue->entry_count = entry_count;
9438 queue->phba = phba;
9439
9440 return queue;
9441out_fail:
9442 lpfc_sli4_queue_free(queue);
9443 return NULL;
9444}
9445
9446/**
9447 * lpfc_eq_create - Create an Event Queue on the HBA
9448 * @phba: HBA structure that indicates port to create a queue on.
9449 * @eq: The queue structure to use to create the event queue.
9450 * @imax: The maximum interrupt per second limit.
9451 *
9452 * This function creates an event queue, as detailed in @eq, on a port,
9453 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
9454 *
9455 * The @phba struct is used to send mailbox command to HBA. The @eq struct
9456 * is used to get the entry count and entry size that are necessary to
9457 * determine the number of pages to allocate and use for this queue. This
9458 * function will send the EQ_CREATE mailbox command to the HBA to setup the
9459 * event queue. This function is asynchronous and will wait for the mailbox
9460 * command to finish before continuing.
9461 *
9462 * On success this function will return a zero. If unable to allocate enough
9463 * memory this function will return ENOMEM. If the queue create mailbox command
9464 * fails this function will return ENXIO.
9465 **/
9466uint32_t
9467lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint16_t imax)
9468{
9469 struct lpfc_mbx_eq_create *eq_create;
9470 LPFC_MBOXQ_t *mbox;
9471 int rc, length, status = 0;
9472 struct lpfc_dmabuf *dmabuf;
9473 uint32_t shdr_status, shdr_add_status;
9474 union lpfc_sli4_cfg_shdr *shdr;
9475 uint16_t dmult;
9476
9477 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9478 if (!mbox)
9479 return -ENOMEM;
9480 length = (sizeof(struct lpfc_mbx_eq_create) -
9481 sizeof(struct lpfc_sli4_cfg_mhdr));
9482 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9483 LPFC_MBOX_OPCODE_EQ_CREATE,
9484 length, LPFC_SLI4_MBX_EMBED);
9485 eq_create = &mbox->u.mqe.un.eq_create;
9486 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
9487 eq->page_count);
9488 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
9489 LPFC_EQE_SIZE);
9490 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
9491 /* Calculate delay multiper from maximum interrupt per second */
9492 dmult = LPFC_DMULT_CONST/imax - 1;
9493 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
9494 dmult);
9495 switch (eq->entry_count) {
9496 default:
9497 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9498 "0360 Unsupported EQ count. (%d)\n",
9499 eq->entry_count);
9500 if (eq->entry_count < 256)
9501 return -EINVAL;
9502 /* otherwise default to smallest count (drop through) */
9503 case 256:
9504 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9505 LPFC_EQ_CNT_256);
9506 break;
9507 case 512:
9508 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9509 LPFC_EQ_CNT_512);
9510 break;
9511 case 1024:
9512 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9513 LPFC_EQ_CNT_1024);
9514 break;
9515 case 2048:
9516 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9517 LPFC_EQ_CNT_2048);
9518 break;
9519 case 4096:
9520 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
9521 LPFC_EQ_CNT_4096);
9522 break;
9523 }
9524 list_for_each_entry(dmabuf, &eq->page_list, list) {
9525 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9526 putPaddrLow(dmabuf->phys);
9527 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9528 putPaddrHigh(dmabuf->phys);
9529 }
9530 mbox->vport = phba->pport;
9531 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
9532 mbox->context1 = NULL;
9533 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9534 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
9535 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9536 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9537 if (shdr_status || shdr_add_status || rc) {
9538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9539 "2500 EQ_CREATE mailbox failed with "
9540 "status x%x add_status x%x, mbx status x%x\n",
9541 shdr_status, shdr_add_status, rc);
9542 status = -ENXIO;
9543 }
9544 eq->type = LPFC_EQ;
9545 eq->subtype = LPFC_NONE;
9546 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
9547 if (eq->queue_id == 0xFFFF)
9548 status = -ENXIO;
9549 eq->host_index = 0;
9550 eq->hba_index = 0;
9551
8fa38513 9552 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9553 return status;
9554}
9555
9556/**
9557 * lpfc_cq_create - Create a Completion Queue on the HBA
9558 * @phba: HBA structure that indicates port to create a queue on.
9559 * @cq: The queue structure to use to create the completion queue.
9560 * @eq: The event queue to bind this completion queue to.
9561 *
9562 * This function creates a completion queue, as detailed in @wq, on a port,
9563 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
9564 *
9565 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9566 * is used to get the entry count and entry size that are necessary to
9567 * determine the number of pages to allocate and use for this queue. The @eq
9568 * is used to indicate which event queue to bind this completion queue to. This
9569 * function will send the CQ_CREATE mailbox command to the HBA to setup the
9570 * completion queue. This function is asynchronous and will wait for the mailbox
9571 * command to finish before continuing.
9572 *
9573 * On success this function will return a zero. If unable to allocate enough
9574 * memory this function will return ENOMEM. If the queue create mailbox command
9575 * fails this function will return ENXIO.
9576 **/
9577uint32_t
9578lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
9579 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
9580{
9581 struct lpfc_mbx_cq_create *cq_create;
9582 struct lpfc_dmabuf *dmabuf;
9583 LPFC_MBOXQ_t *mbox;
9584 int rc, length, status = 0;
9585 uint32_t shdr_status, shdr_add_status;
9586 union lpfc_sli4_cfg_shdr *shdr;
9587
9588 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9589 if (!mbox)
9590 return -ENOMEM;
9591 length = (sizeof(struct lpfc_mbx_cq_create) -
9592 sizeof(struct lpfc_sli4_cfg_mhdr));
9593 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9594 LPFC_MBOX_OPCODE_CQ_CREATE,
9595 length, LPFC_SLI4_MBX_EMBED);
9596 cq_create = &mbox->u.mqe.un.cq_create;
9597 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
9598 cq->page_count);
9599 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
9600 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
9601 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context, eq->queue_id);
9602 switch (cq->entry_count) {
9603 default:
9604 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9605 "0361 Unsupported CQ count. (%d)\n",
9606 cq->entry_count);
9607 if (cq->entry_count < 256)
9608 return -EINVAL;
9609 /* otherwise default to smallest count (drop through) */
9610 case 256:
9611 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9612 LPFC_CQ_CNT_256);
9613 break;
9614 case 512:
9615 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9616 LPFC_CQ_CNT_512);
9617 break;
9618 case 1024:
9619 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
9620 LPFC_CQ_CNT_1024);
9621 break;
9622 }
9623 list_for_each_entry(dmabuf, &cq->page_list, list) {
9624 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9625 putPaddrLow(dmabuf->phys);
9626 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9627 putPaddrHigh(dmabuf->phys);
9628 }
9629 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9630
9631 /* The IOCTL status is embedded in the mailbox subheader. */
9632 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
9633 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9634 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9635 if (shdr_status || shdr_add_status || rc) {
9636 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9637 "2501 CQ_CREATE mailbox failed with "
9638 "status x%x add_status x%x, mbx status x%x\n",
9639 shdr_status, shdr_add_status, rc);
9640 status = -ENXIO;
9641 goto out;
9642 }
9643 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9644 if (cq->queue_id == 0xFFFF) {
9645 status = -ENXIO;
9646 goto out;
9647 }
9648 /* link the cq onto the parent eq child list */
9649 list_add_tail(&cq->list, &eq->child_list);
9650 /* Set up completion queue's type and subtype */
9651 cq->type = type;
9652 cq->subtype = subtype;
9653 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
9654 cq->host_index = 0;
9655 cq->hba_index = 0;
4f774513 9656
8fa38513
JS
9657out:
9658 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9659 return status;
9660}
9661
04c68496
JS
9662/**
9663 * lpfc_mq_create - Create a mailbox Queue on the HBA
9664 * @phba: HBA structure that indicates port to create a queue on.
9665 * @mq: The queue structure to use to create the mailbox queue.
9666 *
9667 * This function creates a mailbox queue, as detailed in @mq, on a port,
9668 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
9669 *
9670 * The @phba struct is used to send mailbox command to HBA. The @cq struct
9671 * is used to get the entry count and entry size that are necessary to
9672 * determine the number of pages to allocate and use for this queue. This
9673 * function will send the MQ_CREATE mailbox command to the HBA to setup the
9674 * mailbox queue. This function is asynchronous and will wait for the mailbox
9675 * command to finish before continuing.
9676 *
9677 * On success this function will return a zero. If unable to allocate enough
9678 * memory this function will return ENOMEM. If the queue create mailbox command
9679 * fails this function will return ENXIO.
9680 **/
9681uint32_t
9682lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
9683 struct lpfc_queue *cq, uint32_t subtype)
9684{
9685 struct lpfc_mbx_mq_create *mq_create;
9686 struct lpfc_dmabuf *dmabuf;
9687 LPFC_MBOXQ_t *mbox;
9688 int rc, length, status = 0;
9689 uint32_t shdr_status, shdr_add_status;
9690 union lpfc_sli4_cfg_shdr *shdr;
9691
9692 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9693 if (!mbox)
9694 return -ENOMEM;
9695 length = (sizeof(struct lpfc_mbx_mq_create) -
9696 sizeof(struct lpfc_sli4_cfg_mhdr));
9697 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
9698 LPFC_MBOX_OPCODE_MQ_CREATE,
9699 length, LPFC_SLI4_MBX_EMBED);
9700 mq_create = &mbox->u.mqe.un.mq_create;
9701 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
9702 mq->page_count);
9703 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
9704 cq->queue_id);
9705 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
9706 switch (mq->entry_count) {
9707 default:
9708 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9709 "0362 Unsupported MQ count. (%d)\n",
9710 mq->entry_count);
9711 if (mq->entry_count < 16)
9712 return -EINVAL;
9713 /* otherwise default to smallest count (drop through) */
9714 case 16:
9715 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9716 LPFC_MQ_CNT_16);
9717 break;
9718 case 32:
9719 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9720 LPFC_MQ_CNT_32);
9721 break;
9722 case 64:
9723 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9724 LPFC_MQ_CNT_64);
9725 break;
9726 case 128:
9727 bf_set(lpfc_mq_context_count, &mq_create->u.request.context,
9728 LPFC_MQ_CNT_128);
9729 break;
9730 }
9731 list_for_each_entry(dmabuf, &mq->page_list, list) {
9732 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9733 putPaddrLow(dmabuf->phys);
9734 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9735 putPaddrHigh(dmabuf->phys);
9736 }
9737 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9738 /* The IOCTL status is embedded in the mailbox subheader. */
9739 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
9740 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9741 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9742 if (shdr_status || shdr_add_status || rc) {
9743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9744 "2502 MQ_CREATE mailbox failed with "
9745 "status x%x add_status x%x, mbx status x%x\n",
9746 shdr_status, shdr_add_status, rc);
9747 status = -ENXIO;
9748 goto out;
9749 }
9750 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id, &mq_create->u.response);
9751 if (mq->queue_id == 0xFFFF) {
9752 status = -ENXIO;
9753 goto out;
9754 }
9755 mq->type = LPFC_MQ;
9756 mq->subtype = subtype;
9757 mq->host_index = 0;
9758 mq->hba_index = 0;
9759
9760 /* link the mq onto the parent cq child list */
9761 list_add_tail(&mq->list, &cq->child_list);
9762out:
8fa38513 9763 mempool_free(mbox, phba->mbox_mem_pool);
04c68496
JS
9764 return status;
9765}
9766
4f774513
JS
9767/**
9768 * lpfc_wq_create - Create a Work Queue on the HBA
9769 * @phba: HBA structure that indicates port to create a queue on.
9770 * @wq: The queue structure to use to create the work queue.
9771 * @cq: The completion queue to bind this work queue to.
9772 * @subtype: The subtype of the work queue indicating its functionality.
9773 *
9774 * This function creates a work queue, as detailed in @wq, on a port, described
9775 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
9776 *
9777 * The @phba struct is used to send mailbox command to HBA. The @wq struct
9778 * is used to get the entry count and entry size that are necessary to
9779 * determine the number of pages to allocate and use for this queue. The @cq
9780 * is used to indicate which completion queue to bind this work queue to. This
9781 * function will send the WQ_CREATE mailbox command to the HBA to setup the
9782 * work queue. This function is asynchronous and will wait for the mailbox
9783 * command to finish before continuing.
9784 *
9785 * On success this function will return a zero. If unable to allocate enough
9786 * memory this function will return ENOMEM. If the queue create mailbox command
9787 * fails this function will return ENXIO.
9788 **/
9789uint32_t
9790lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
9791 struct lpfc_queue *cq, uint32_t subtype)
9792{
9793 struct lpfc_mbx_wq_create *wq_create;
9794 struct lpfc_dmabuf *dmabuf;
9795 LPFC_MBOXQ_t *mbox;
9796 int rc, length, status = 0;
9797 uint32_t shdr_status, shdr_add_status;
9798 union lpfc_sli4_cfg_shdr *shdr;
9799
9800 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9801 if (!mbox)
9802 return -ENOMEM;
9803 length = (sizeof(struct lpfc_mbx_wq_create) -
9804 sizeof(struct lpfc_sli4_cfg_mhdr));
9805 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9806 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
9807 length, LPFC_SLI4_MBX_EMBED);
9808 wq_create = &mbox->u.mqe.un.wq_create;
9809 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
9810 wq->page_count);
9811 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
9812 cq->queue_id);
9813 list_for_each_entry(dmabuf, &wq->page_list, list) {
9814 wq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9815 putPaddrLow(dmabuf->phys);
9816 wq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9817 putPaddrHigh(dmabuf->phys);
9818 }
9819 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9820 /* The IOCTL status is embedded in the mailbox subheader. */
9821 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
9822 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9823 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9824 if (shdr_status || shdr_add_status || rc) {
9825 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9826 "2503 WQ_CREATE mailbox failed with "
9827 "status x%x add_status x%x, mbx status x%x\n",
9828 shdr_status, shdr_add_status, rc);
9829 status = -ENXIO;
9830 goto out;
9831 }
9832 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
9833 if (wq->queue_id == 0xFFFF) {
9834 status = -ENXIO;
9835 goto out;
9836 }
9837 wq->type = LPFC_WQ;
9838 wq->subtype = subtype;
9839 wq->host_index = 0;
9840 wq->hba_index = 0;
9841
9842 /* link the wq onto the parent cq child list */
9843 list_add_tail(&wq->list, &cq->child_list);
9844out:
8fa38513 9845 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
9846 return status;
9847}
9848
9849/**
9850 * lpfc_rq_create - Create a Receive Queue on the HBA
9851 * @phba: HBA structure that indicates port to create a queue on.
9852 * @hrq: The queue structure to use to create the header receive queue.
9853 * @drq: The queue structure to use to create the data receive queue.
9854 * @cq: The completion queue to bind this work queue to.
9855 *
9856 * This function creates a receive buffer queue pair , as detailed in @hrq and
9857 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
9858 * to the HBA.
9859 *
9860 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
9861 * struct is used to get the entry count that is necessary to determine the
9862 * number of pages to use for this queue. The @cq is used to indicate which
9863 * completion queue to bind received buffers that are posted to these queues to.
9864 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
9865 * receive queue pair. This function is asynchronous and will wait for the
9866 * mailbox command to finish before continuing.
9867 *
9868 * On success this function will return a zero. If unable to allocate enough
9869 * memory this function will return ENOMEM. If the queue create mailbox command
9870 * fails this function will return ENXIO.
9871 **/
9872uint32_t
9873lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
9874 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
9875{
9876 struct lpfc_mbx_rq_create *rq_create;
9877 struct lpfc_dmabuf *dmabuf;
9878 LPFC_MBOXQ_t *mbox;
9879 int rc, length, status = 0;
9880 uint32_t shdr_status, shdr_add_status;
9881 union lpfc_sli4_cfg_shdr *shdr;
9882
9883 if (hrq->entry_count != drq->entry_count)
9884 return -EINVAL;
9885 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
9886 if (!mbox)
9887 return -ENOMEM;
9888 length = (sizeof(struct lpfc_mbx_rq_create) -
9889 sizeof(struct lpfc_sli4_cfg_mhdr));
9890 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9891 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9892 length, LPFC_SLI4_MBX_EMBED);
9893 rq_create = &mbox->u.mqe.un.rq_create;
9894 switch (hrq->entry_count) {
9895 default:
9896 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9897 "2535 Unsupported RQ count. (%d)\n",
9898 hrq->entry_count);
9899 if (hrq->entry_count < 512)
9900 return -EINVAL;
9901 /* otherwise default to smallest count (drop through) */
9902 case 512:
9903 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9904 LPFC_RQ_RING_SIZE_512);
9905 break;
9906 case 1024:
9907 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9908 LPFC_RQ_RING_SIZE_1024);
9909 break;
9910 case 2048:
9911 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9912 LPFC_RQ_RING_SIZE_2048);
9913 break;
9914 case 4096:
9915 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9916 LPFC_RQ_RING_SIZE_4096);
9917 break;
9918 }
9919 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9920 cq->queue_id);
9921 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9922 hrq->page_count);
9923 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9924 LPFC_HDR_BUF_SIZE);
9925 list_for_each_entry(dmabuf, &hrq->page_list, list) {
9926 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9927 putPaddrLow(dmabuf->phys);
9928 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9929 putPaddrHigh(dmabuf->phys);
9930 }
9931 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9932 /* The IOCTL status is embedded in the mailbox subheader. */
9933 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9934 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9935 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
9936 if (shdr_status || shdr_add_status || rc) {
9937 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9938 "2504 RQ_CREATE mailbox failed with "
9939 "status x%x add_status x%x, mbx status x%x\n",
9940 shdr_status, shdr_add_status, rc);
9941 status = -ENXIO;
9942 goto out;
9943 }
9944 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
9945 if (hrq->queue_id == 0xFFFF) {
9946 status = -ENXIO;
9947 goto out;
9948 }
9949 hrq->type = LPFC_HRQ;
9950 hrq->subtype = subtype;
9951 hrq->host_index = 0;
9952 hrq->hba_index = 0;
9953
9954 /* now create the data queue */
9955 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
9956 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
9957 length, LPFC_SLI4_MBX_EMBED);
9958 switch (drq->entry_count) {
9959 default:
9960 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9961 "2536 Unsupported RQ count. (%d)\n",
9962 drq->entry_count);
9963 if (drq->entry_count < 512)
9964 return -EINVAL;
9965 /* otherwise default to smallest count (drop through) */
9966 case 512:
9967 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9968 LPFC_RQ_RING_SIZE_512);
9969 break;
9970 case 1024:
9971 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9972 LPFC_RQ_RING_SIZE_1024);
9973 break;
9974 case 2048:
9975 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9976 LPFC_RQ_RING_SIZE_2048);
9977 break;
9978 case 4096:
9979 bf_set(lpfc_rq_context_rq_size, &rq_create->u.request.context,
9980 LPFC_RQ_RING_SIZE_4096);
9981 break;
9982 }
9983 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
9984 cq->queue_id);
9985 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
9986 drq->page_count);
9987 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
9988 LPFC_DATA_BUF_SIZE);
9989 list_for_each_entry(dmabuf, &drq->page_list, list) {
9990 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
9991 putPaddrLow(dmabuf->phys);
9992 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
9993 putPaddrHigh(dmabuf->phys);
9994 }
9995 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
9996 /* The IOCTL status is embedded in the mailbox subheader. */
9997 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
9998 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
9999 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10000 if (shdr_status || shdr_add_status || rc) {
10001 status = -ENXIO;
10002 goto out;
10003 }
10004 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
10005 if (drq->queue_id == 0xFFFF) {
10006 status = -ENXIO;
10007 goto out;
10008 }
10009 drq->type = LPFC_DRQ;
10010 drq->subtype = subtype;
10011 drq->host_index = 0;
10012 drq->hba_index = 0;
10013
10014 /* link the header and data RQs onto the parent cq child list */
10015 list_add_tail(&hrq->list, &cq->child_list);
10016 list_add_tail(&drq->list, &cq->child_list);
10017
10018out:
8fa38513 10019 mempool_free(mbox, phba->mbox_mem_pool);
4f774513
JS
10020 return status;
10021}
10022
10023/**
10024 * lpfc_eq_destroy - Destroy an event Queue on the HBA
10025 * @eq: The queue structure associated with the queue to destroy.
10026 *
10027 * This function destroys a queue, as detailed in @eq by sending an mailbox
10028 * command, specific to the type of queue, to the HBA.
10029 *
10030 * The @eq struct is used to get the queue ID of the queue to destroy.
10031 *
10032 * On success this function will return a zero. If the queue destroy mailbox
10033 * command fails this function will return ENXIO.
10034 **/
10035uint32_t
10036lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
10037{
10038 LPFC_MBOXQ_t *mbox;
10039 int rc, length, status = 0;
10040 uint32_t shdr_status, shdr_add_status;
10041 union lpfc_sli4_cfg_shdr *shdr;
10042
10043 if (!eq)
10044 return -ENODEV;
10045 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
10046 if (!mbox)
10047 return -ENOMEM;
10048 length = (sizeof(struct lpfc_mbx_eq_destroy) -
10049 sizeof(struct lpfc_sli4_cfg_mhdr));
10050 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10051 LPFC_MBOX_OPCODE_EQ_DESTROY,
10052 length, LPFC_SLI4_MBX_EMBED);
10053 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
10054 eq->queue_id);
10055 mbox->vport = eq->phba->pport;
10056 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10057
10058 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
10059 /* The IOCTL status is embedded in the mailbox subheader. */
10060 shdr = (union lpfc_sli4_cfg_shdr *)
10061 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
10062 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10063 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10064 if (shdr_status || shdr_add_status || rc) {
10065 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10066 "2505 EQ_DESTROY mailbox failed with "
10067 "status x%x add_status x%x, mbx status x%x\n",
10068 shdr_status, shdr_add_status, rc);
10069 status = -ENXIO;
10070 }
10071
10072 /* Remove eq from any list */
10073 list_del_init(&eq->list);
8fa38513 10074 mempool_free(mbox, eq->phba->mbox_mem_pool);
4f774513
JS
10075 return status;
10076}
10077
10078/**
10079 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
10080 * @cq: The queue structure associated with the queue to destroy.
10081 *
10082 * This function destroys a queue, as detailed in @cq by sending an mailbox
10083 * command, specific to the type of queue, to the HBA.
10084 *
10085 * The @cq struct is used to get the queue ID of the queue to destroy.
10086 *
10087 * On success this function will return a zero. If the queue destroy mailbox
10088 * command fails this function will return ENXIO.
10089 **/
10090uint32_t
10091lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
10092{
10093 LPFC_MBOXQ_t *mbox;
10094 int rc, length, status = 0;
10095 uint32_t shdr_status, shdr_add_status;
10096 union lpfc_sli4_cfg_shdr *shdr;
10097
10098 if (!cq)
10099 return -ENODEV;
10100 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
10101 if (!mbox)
10102 return -ENOMEM;
10103 length = (sizeof(struct lpfc_mbx_cq_destroy) -
10104 sizeof(struct lpfc_sli4_cfg_mhdr));
10105 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10106 LPFC_MBOX_OPCODE_CQ_DESTROY,
10107 length, LPFC_SLI4_MBX_EMBED);
10108 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
10109 cq->queue_id);
10110 mbox->vport = cq->phba->pport;
10111 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10112 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
10113 /* The IOCTL status is embedded in the mailbox subheader. */
10114 shdr = (union lpfc_sli4_cfg_shdr *)
10115 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
10116 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10117 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10118 if (shdr_status || shdr_add_status || rc) {
10119 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10120 "2506 CQ_DESTROY mailbox failed with "
10121 "status x%x add_status x%x, mbx status x%x\n",
10122 shdr_status, shdr_add_status, rc);
10123 status = -ENXIO;
10124 }
10125 /* Remove cq from any list */
10126 list_del_init(&cq->list);
8fa38513 10127 mempool_free(mbox, cq->phba->mbox_mem_pool);
4f774513
JS
10128 return status;
10129}
10130
04c68496
JS
10131/**
10132 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
10133 * @qm: The queue structure associated with the queue to destroy.
10134 *
10135 * This function destroys a queue, as detailed in @mq by sending an mailbox
10136 * command, specific to the type of queue, to the HBA.
10137 *
10138 * The @mq struct is used to get the queue ID of the queue to destroy.
10139 *
10140 * On success this function will return a zero. If the queue destroy mailbox
10141 * command fails this function will return ENXIO.
10142 **/
10143uint32_t
10144lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
10145{
10146 LPFC_MBOXQ_t *mbox;
10147 int rc, length, status = 0;
10148 uint32_t shdr_status, shdr_add_status;
10149 union lpfc_sli4_cfg_shdr *shdr;
10150
10151 if (!mq)
10152 return -ENODEV;
10153 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
10154 if (!mbox)
10155 return -ENOMEM;
10156 length = (sizeof(struct lpfc_mbx_mq_destroy) -
10157 sizeof(struct lpfc_sli4_cfg_mhdr));
10158 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
10159 LPFC_MBOX_OPCODE_MQ_DESTROY,
10160 length, LPFC_SLI4_MBX_EMBED);
10161 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
10162 mq->queue_id);
10163 mbox->vport = mq->phba->pport;
10164 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10165 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
10166 /* The IOCTL status is embedded in the mailbox subheader. */
10167 shdr = (union lpfc_sli4_cfg_shdr *)
10168 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
10169 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10170 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10171 if (shdr_status || shdr_add_status || rc) {
10172 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10173 "2507 MQ_DESTROY mailbox failed with "
10174 "status x%x add_status x%x, mbx status x%x\n",
10175 shdr_status, shdr_add_status, rc);
10176 status = -ENXIO;
10177 }
10178 /* Remove mq from any list */
10179 list_del_init(&mq->list);
8fa38513 10180 mempool_free(mbox, mq->phba->mbox_mem_pool);
04c68496
JS
10181 return status;
10182}
10183
4f774513
JS
10184/**
10185 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
10186 * @wq: The queue structure associated with the queue to destroy.
10187 *
10188 * This function destroys a queue, as detailed in @wq by sending an mailbox
10189 * command, specific to the type of queue, to the HBA.
10190 *
10191 * The @wq struct is used to get the queue ID of the queue to destroy.
10192 *
10193 * On success this function will return a zero. If the queue destroy mailbox
10194 * command fails this function will return ENXIO.
10195 **/
10196uint32_t
10197lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
10198{
10199 LPFC_MBOXQ_t *mbox;
10200 int rc, length, status = 0;
10201 uint32_t shdr_status, shdr_add_status;
10202 union lpfc_sli4_cfg_shdr *shdr;
10203
10204 if (!wq)
10205 return -ENODEV;
10206 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
10207 if (!mbox)
10208 return -ENOMEM;
10209 length = (sizeof(struct lpfc_mbx_wq_destroy) -
10210 sizeof(struct lpfc_sli4_cfg_mhdr));
10211 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10212 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
10213 length, LPFC_SLI4_MBX_EMBED);
10214 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
10215 wq->queue_id);
10216 mbox->vport = wq->phba->pport;
10217 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10218 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
10219 shdr = (union lpfc_sli4_cfg_shdr *)
10220 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
10221 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10222 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10223 if (shdr_status || shdr_add_status || rc) {
10224 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10225 "2508 WQ_DESTROY mailbox failed with "
10226 "status x%x add_status x%x, mbx status x%x\n",
10227 shdr_status, shdr_add_status, rc);
10228 status = -ENXIO;
10229 }
10230 /* Remove wq from any list */
10231 list_del_init(&wq->list);
8fa38513 10232 mempool_free(mbox, wq->phba->mbox_mem_pool);
4f774513
JS
10233 return status;
10234}
10235
10236/**
10237 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
10238 * @rq: The queue structure associated with the queue to destroy.
10239 *
10240 * This function destroys a queue, as detailed in @rq by sending an mailbox
10241 * command, specific to the type of queue, to the HBA.
10242 *
10243 * The @rq struct is used to get the queue ID of the queue to destroy.
10244 *
10245 * On success this function will return a zero. If the queue destroy mailbox
10246 * command fails this function will return ENXIO.
10247 **/
10248uint32_t
10249lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
10250 struct lpfc_queue *drq)
10251{
10252 LPFC_MBOXQ_t *mbox;
10253 int rc, length, status = 0;
10254 uint32_t shdr_status, shdr_add_status;
10255 union lpfc_sli4_cfg_shdr *shdr;
10256
10257 if (!hrq || !drq)
10258 return -ENODEV;
10259 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
10260 if (!mbox)
10261 return -ENOMEM;
10262 length = (sizeof(struct lpfc_mbx_rq_destroy) -
10263 sizeof(struct mbox_header));
10264 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10265 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
10266 length, LPFC_SLI4_MBX_EMBED);
10267 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10268 hrq->queue_id);
10269 mbox->vport = hrq->phba->pport;
10270 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10271 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
10272 /* The IOCTL status is embedded in the mailbox subheader. */
10273 shdr = (union lpfc_sli4_cfg_shdr *)
10274 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10275 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10276 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10277 if (shdr_status || shdr_add_status || rc) {
10278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10279 "2509 RQ_DESTROY mailbox failed with "
10280 "status x%x add_status x%x, mbx status x%x\n",
10281 shdr_status, shdr_add_status, rc);
10282 if (rc != MBX_TIMEOUT)
10283 mempool_free(mbox, hrq->phba->mbox_mem_pool);
10284 return -ENXIO;
10285 }
10286 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
10287 drq->queue_id);
10288 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
10289 shdr = (union lpfc_sli4_cfg_shdr *)
10290 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
10291 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10292 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10293 if (shdr_status || shdr_add_status || rc) {
10294 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10295 "2510 RQ_DESTROY mailbox failed with "
10296 "status x%x add_status x%x, mbx status x%x\n",
10297 shdr_status, shdr_add_status, rc);
10298 status = -ENXIO;
10299 }
10300 list_del_init(&hrq->list);
10301 list_del_init(&drq->list);
8fa38513 10302 mempool_free(mbox, hrq->phba->mbox_mem_pool);
4f774513
JS
10303 return status;
10304}
10305
10306/**
10307 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
10308 * @phba: The virtual port for which this call being executed.
10309 * @pdma_phys_addr0: Physical address of the 1st SGL page.
10310 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
10311 * @xritag: the xritag that ties this io to the SGL pages.
10312 *
10313 * This routine will post the sgl pages for the IO that has the xritag
10314 * that is in the iocbq structure. The xritag is assigned during iocbq
10315 * creation and persists for as long as the driver is loaded.
10316 * if the caller has fewer than 256 scatter gather segments to map then
10317 * pdma_phys_addr1 should be 0.
10318 * If the caller needs to map more than 256 scatter gather segment then
10319 * pdma_phys_addr1 should be a valid physical address.
10320 * physical address for SGLs must be 64 byte aligned.
10321 * If you are going to map 2 SGL's then the first one must have 256 entries
10322 * the second sgl can have between 1 and 256 entries.
10323 *
10324 * Return codes:
10325 * 0 - Success
10326 * -ENXIO, -ENOMEM - Failure
10327 **/
10328int
10329lpfc_sli4_post_sgl(struct lpfc_hba *phba,
10330 dma_addr_t pdma_phys_addr0,
10331 dma_addr_t pdma_phys_addr1,
10332 uint16_t xritag)
10333{
10334 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
10335 LPFC_MBOXQ_t *mbox;
10336 int rc;
10337 uint32_t shdr_status, shdr_add_status;
10338 union lpfc_sli4_cfg_shdr *shdr;
10339
10340 if (xritag == NO_XRI) {
10341 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10342 "0364 Invalid param:\n");
10343 return -EINVAL;
10344 }
10345
10346 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10347 if (!mbox)
10348 return -ENOMEM;
10349
10350 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10351 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
10352 sizeof(struct lpfc_mbx_post_sgl_pages) -
10353 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
10354
10355 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
10356 &mbox->u.mqe.un.post_sgl_pages;
10357 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
10358 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
10359
10360 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
10361 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
10362 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
10363 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
10364
10365 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
10366 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
10367 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
10368 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
10369 if (!phba->sli4_hba.intr_enable)
10370 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10371 else
10372 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10373 /* The IOCTL status is embedded in the mailbox subheader. */
10374 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
10375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10377 if (rc != MBX_TIMEOUT)
10378 mempool_free(mbox, phba->mbox_mem_pool);
10379 if (shdr_status || shdr_add_status || rc) {
10380 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10381 "2511 POST_SGL mailbox failed with "
10382 "status x%x add_status x%x, mbx status x%x\n",
10383 shdr_status, shdr_add_status, rc);
10384 rc = -ENXIO;
10385 }
10386 return 0;
10387}
10388/**
10389 * lpfc_sli4_remove_all_sgl_pages - Post scatter gather list for an XRI to HBA
10390 * @phba: The virtual port for which this call being executed.
10391 *
10392 * This routine will remove all of the sgl pages registered with the hba.
10393 *
10394 * Return codes:
10395 * 0 - Success
10396 * -ENXIO, -ENOMEM - Failure
10397 **/
10398int
10399lpfc_sli4_remove_all_sgl_pages(struct lpfc_hba *phba)
10400{
10401 LPFC_MBOXQ_t *mbox;
10402 int rc;
10403 uint32_t shdr_status, shdr_add_status;
10404 union lpfc_sli4_cfg_shdr *shdr;
10405
10406 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10407 if (!mbox)
10408 return -ENOMEM;
10409
10410 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10411 LPFC_MBOX_OPCODE_FCOE_REMOVE_SGL_PAGES, 0,
10412 LPFC_SLI4_MBX_EMBED);
10413 if (!phba->sli4_hba.intr_enable)
10414 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10415 else
10416 rc = lpfc_sli_issue_mbox_wait(phba, mbox, LPFC_MBOX_TMO);
10417 /* The IOCTL status is embedded in the mailbox subheader. */
10418 shdr = (union lpfc_sli4_cfg_shdr *)
10419 &mbox->u.mqe.un.sli4_config.header.cfg_shdr;
10420 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10421 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10422 if (rc != MBX_TIMEOUT)
10423 mempool_free(mbox, phba->mbox_mem_pool);
10424 if (shdr_status || shdr_add_status || rc) {
10425 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10426 "2512 REMOVE_ALL_SGL_PAGES mailbox failed with "
10427 "status x%x add_status x%x, mbx status x%x\n",
10428 shdr_status, shdr_add_status, rc);
10429 rc = -ENXIO;
10430 }
10431 return rc;
10432}
10433
10434/**
10435 * lpfc_sli4_next_xritag - Get an xritag for the io
10436 * @phba: Pointer to HBA context object.
10437 *
10438 * This function gets an xritag for the iocb. If there is no unused xritag
10439 * it will return 0xffff.
10440 * The function returns the allocated xritag if successful, else returns zero.
10441 * Zero is not a valid xritag.
10442 * The caller is not required to hold any lock.
10443 **/
10444uint16_t
10445lpfc_sli4_next_xritag(struct lpfc_hba *phba)
10446{
10447 uint16_t xritag;
10448
10449 spin_lock_irq(&phba->hbalock);
10450 xritag = phba->sli4_hba.next_xri;
10451 if ((xritag != (uint16_t) -1) && xritag <
10452 (phba->sli4_hba.max_cfg_param.max_xri
10453 + phba->sli4_hba.max_cfg_param.xri_base)) {
10454 phba->sli4_hba.next_xri++;
10455 phba->sli4_hba.max_cfg_param.xri_used++;
10456 spin_unlock_irq(&phba->hbalock);
10457 return xritag;
10458 }
10459 spin_unlock_irq(&phba->hbalock);
6a9c52cf 10460 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4f774513
JS
10461 "2004 Failed to allocate XRI.last XRITAG is %d"
10462 " Max XRI is %d, Used XRI is %d\n",
10463 phba->sli4_hba.next_xri,
10464 phba->sli4_hba.max_cfg_param.max_xri,
10465 phba->sli4_hba.max_cfg_param.xri_used);
10466 return -1;
10467}
10468
10469/**
10470 * lpfc_sli4_post_sgl_list - post a block of sgl list to the firmware.
10471 * @phba: pointer to lpfc hba data structure.
10472 *
10473 * This routine is invoked to post a block of driver's sgl pages to the
10474 * HBA using non-embedded mailbox command. No Lock is held. This routine
10475 * is only called when the driver is loading and after all IO has been
10476 * stopped.
10477 **/
10478int
10479lpfc_sli4_post_sgl_list(struct lpfc_hba *phba)
10480{
10481 struct lpfc_sglq *sglq_entry;
10482 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10483 struct sgl_page_pairs *sgl_pg_pairs;
10484 void *viraddr;
10485 LPFC_MBOXQ_t *mbox;
10486 uint32_t reqlen, alloclen, pg_pairs;
10487 uint32_t mbox_tmo;
10488 uint16_t xritag_start = 0;
10489 int els_xri_cnt, rc = 0;
10490 uint32_t shdr_status, shdr_add_status;
10491 union lpfc_sli4_cfg_shdr *shdr;
10492
10493 /* The number of sgls to be posted */
10494 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
10495
10496 reqlen = els_xri_cnt * sizeof(struct sgl_page_pairs) +
10497 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10498 if (reqlen > PAGE_SIZE) {
10499 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10500 "2559 Block sgl registration required DMA "
10501 "size (%d) great than a page\n", reqlen);
10502 return -ENOMEM;
10503 }
10504 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10505 if (!mbox) {
10506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10507 "2560 Failed to allocate mbox cmd memory\n");
10508 return -ENOMEM;
10509 }
10510
10511 /* Allocate DMA memory and set up the non-embedded mailbox command */
10512 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10513 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10514 LPFC_SLI4_MBX_NEMBED);
10515
10516 if (alloclen < reqlen) {
10517 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10518 "0285 Allocated DMA memory size (%d) is "
10519 "less than the requested DMA memory "
10520 "size (%d)\n", alloclen, reqlen);
10521 lpfc_sli4_mbox_cmd_free(phba, mbox);
10522 return -ENOMEM;
10523 }
4f774513 10524 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10525 viraddr = mbox->sge_array->addr[0];
10526
10527 /* Set up the SGL pages in the non-embedded DMA pages */
10528 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10529 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10530
10531 for (pg_pairs = 0; pg_pairs < els_xri_cnt; pg_pairs++) {
10532 sglq_entry = phba->sli4_hba.lpfc_els_sgl_array[pg_pairs];
10533 /* Set up the sge entry */
10534 sgl_pg_pairs->sgl_pg0_addr_lo =
10535 cpu_to_le32(putPaddrLow(sglq_entry->phys));
10536 sgl_pg_pairs->sgl_pg0_addr_hi =
10537 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
10538 sgl_pg_pairs->sgl_pg1_addr_lo =
10539 cpu_to_le32(putPaddrLow(0));
10540 sgl_pg_pairs->sgl_pg1_addr_hi =
10541 cpu_to_le32(putPaddrHigh(0));
10542 /* Keep the first xritag on the list */
10543 if (pg_pairs == 0)
10544 xritag_start = sglq_entry->sli4_xritag;
10545 sgl_pg_pairs++;
10546 }
10547 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
6a9c52cf 10548 bf_set(lpfc_post_sgl_pages_xricnt, sgl, els_xri_cnt);
4f774513
JS
10549 /* Perform endian conversion if necessary */
10550 sgl->word0 = cpu_to_le32(sgl->word0);
10551
10552 if (!phba->sli4_hba.intr_enable)
10553 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10554 else {
10555 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10556 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10557 }
10558 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10559 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10560 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10561 if (rc != MBX_TIMEOUT)
10562 lpfc_sli4_mbox_cmd_free(phba, mbox);
10563 if (shdr_status || shdr_add_status || rc) {
10564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10565 "2513 POST_SGL_BLOCK mailbox command failed "
10566 "status x%x add_status x%x mbx status x%x\n",
10567 shdr_status, shdr_add_status, rc);
10568 rc = -ENXIO;
10569 }
10570 return rc;
10571}
10572
10573/**
10574 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
10575 * @phba: pointer to lpfc hba data structure.
10576 * @sblist: pointer to scsi buffer list.
10577 * @count: number of scsi buffers on the list.
10578 *
10579 * This routine is invoked to post a block of @count scsi sgl pages from a
10580 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
10581 * No Lock is held.
10582 *
10583 **/
10584int
10585lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba, struct list_head *sblist,
10586 int cnt)
10587{
10588 struct lpfc_scsi_buf *psb;
10589 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
10590 struct sgl_page_pairs *sgl_pg_pairs;
10591 void *viraddr;
10592 LPFC_MBOXQ_t *mbox;
10593 uint32_t reqlen, alloclen, pg_pairs;
10594 uint32_t mbox_tmo;
10595 uint16_t xritag_start = 0;
10596 int rc = 0;
10597 uint32_t shdr_status, shdr_add_status;
10598 dma_addr_t pdma_phys_bpl1;
10599 union lpfc_sli4_cfg_shdr *shdr;
10600
10601 /* Calculate the requested length of the dma memory */
10602 reqlen = cnt * sizeof(struct sgl_page_pairs) +
10603 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
10604 if (reqlen > PAGE_SIZE) {
10605 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
10606 "0217 Block sgl registration required DMA "
10607 "size (%d) great than a page\n", reqlen);
10608 return -ENOMEM;
10609 }
10610 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
10611 if (!mbox) {
10612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10613 "0283 Failed to allocate mbox cmd memory\n");
10614 return -ENOMEM;
10615 }
10616
10617 /* Allocate DMA memory and set up the non-embedded mailbox command */
10618 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
10619 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
10620 LPFC_SLI4_MBX_NEMBED);
10621
10622 if (alloclen < reqlen) {
10623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10624 "2561 Allocated DMA memory size (%d) is "
10625 "less than the requested DMA memory "
10626 "size (%d)\n", alloclen, reqlen);
10627 lpfc_sli4_mbox_cmd_free(phba, mbox);
10628 return -ENOMEM;
10629 }
4f774513 10630 /* Get the first SGE entry from the non-embedded DMA memory */
4f774513
JS
10631 viraddr = mbox->sge_array->addr[0];
10632
10633 /* Set up the SGL pages in the non-embedded DMA pages */
10634 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
10635 sgl_pg_pairs = &sgl->sgl_pg_pairs;
10636
10637 pg_pairs = 0;
10638 list_for_each_entry(psb, sblist, list) {
10639 /* Set up the sge entry */
10640 sgl_pg_pairs->sgl_pg0_addr_lo =
10641 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
10642 sgl_pg_pairs->sgl_pg0_addr_hi =
10643 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
10644 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
10645 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
10646 else
10647 pdma_phys_bpl1 = 0;
10648 sgl_pg_pairs->sgl_pg1_addr_lo =
10649 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
10650 sgl_pg_pairs->sgl_pg1_addr_hi =
10651 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
10652 /* Keep the first xritag on the list */
10653 if (pg_pairs == 0)
10654 xritag_start = psb->cur_iocbq.sli4_xritag;
10655 sgl_pg_pairs++;
10656 pg_pairs++;
10657 }
10658 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
10659 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
10660 /* Perform endian conversion if necessary */
10661 sgl->word0 = cpu_to_le32(sgl->word0);
10662
10663 if (!phba->sli4_hba.intr_enable)
10664 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
10665 else {
10666 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
10667 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
10668 }
10669 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
10670 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
10671 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
10672 if (rc != MBX_TIMEOUT)
10673 lpfc_sli4_mbox_cmd_free(phba, mbox);
10674 if (shdr_status || shdr_add_status || rc) {
10675 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10676 "2564 POST_SGL_BLOCK mailbox command failed "
10677 "status x%x add_status x%x mbx status x%x\n",
10678 shdr_status, shdr_add_status, rc);
10679 rc = -ENXIO;
10680 }
10681 return rc;
10682}
10683
10684/**
10685 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
10686 * @phba: pointer to lpfc_hba struct that the frame was received on
10687 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10688 *
10689 * This function checks the fields in the @fc_hdr to see if the FC frame is a
10690 * valid type of frame that the LPFC driver will handle. This function will
10691 * return a zero if the frame is a valid frame or a non zero value when the
10692 * frame does not pass the check.
10693 **/
10694static int
10695lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
10696{
10697 char *rctl_names[] = FC_RCTL_NAMES_INIT;
10698 char *type_names[] = FC_TYPE_NAMES_INIT;
10699 struct fc_vft_header *fc_vft_hdr;
10700
10701 switch (fc_hdr->fh_r_ctl) {
10702 case FC_RCTL_DD_UNCAT: /* uncategorized information */
10703 case FC_RCTL_DD_SOL_DATA: /* solicited data */
10704 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
10705 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
10706 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
10707 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
10708 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
10709 case FC_RCTL_DD_CMD_STATUS: /* command status */
10710 case FC_RCTL_ELS_REQ: /* extended link services request */
10711 case FC_RCTL_ELS_REP: /* extended link services reply */
10712 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
10713 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
10714 case FC_RCTL_BA_NOP: /* basic link service NOP */
10715 case FC_RCTL_BA_ABTS: /* basic link service abort */
10716 case FC_RCTL_BA_RMC: /* remove connection */
10717 case FC_RCTL_BA_ACC: /* basic accept */
10718 case FC_RCTL_BA_RJT: /* basic reject */
10719 case FC_RCTL_BA_PRMT:
10720 case FC_RCTL_ACK_1: /* acknowledge_1 */
10721 case FC_RCTL_ACK_0: /* acknowledge_0 */
10722 case FC_RCTL_P_RJT: /* port reject */
10723 case FC_RCTL_F_RJT: /* fabric reject */
10724 case FC_RCTL_P_BSY: /* port busy */
10725 case FC_RCTL_F_BSY: /* fabric busy to data frame */
10726 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
10727 case FC_RCTL_LCR: /* link credit reset */
10728 case FC_RCTL_END: /* end */
10729 break;
10730 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
10731 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10732 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
10733 return lpfc_fc_frame_check(phba, fc_hdr);
10734 default:
10735 goto drop;
10736 }
10737 switch (fc_hdr->fh_type) {
10738 case FC_TYPE_BLS:
10739 case FC_TYPE_ELS:
10740 case FC_TYPE_FCP:
10741 case FC_TYPE_CT:
10742 break;
10743 case FC_TYPE_IP:
10744 case FC_TYPE_ILS:
10745 default:
10746 goto drop;
10747 }
10748 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
10749 "2538 Received frame rctl:%s type:%s\n",
10750 rctl_names[fc_hdr->fh_r_ctl],
10751 type_names[fc_hdr->fh_type]);
10752 return 0;
10753drop:
10754 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10755 "2539 Dropped frame rctl:%s type:%s\n",
10756 rctl_names[fc_hdr->fh_r_ctl],
10757 type_names[fc_hdr->fh_type]);
10758 return 1;
10759}
10760
10761/**
10762 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
10763 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10764 *
10765 * This function processes the FC header to retrieve the VFI from the VF
10766 * header, if one exists. This function will return the VFI if one exists
10767 * or 0 if no VSAN Header exists.
10768 **/
10769static uint32_t
10770lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
10771{
10772 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
10773
10774 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
10775 return 0;
10776 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
10777}
10778
10779/**
10780 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
10781 * @phba: Pointer to the HBA structure to search for the vport on
10782 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
10783 * @fcfi: The FC Fabric ID that the frame came from
10784 *
10785 * This function searches the @phba for a vport that matches the content of the
10786 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
10787 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
10788 * returns the matching vport pointer or NULL if unable to match frame to a
10789 * vport.
10790 **/
10791static struct lpfc_vport *
10792lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
10793 uint16_t fcfi)
10794{
10795 struct lpfc_vport **vports;
10796 struct lpfc_vport *vport = NULL;
10797 int i;
10798 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
10799 fc_hdr->fh_d_id[1] << 8 |
10800 fc_hdr->fh_d_id[2]);
10801
10802 vports = lpfc_create_vport_work_array(phba);
10803 if (vports != NULL)
10804 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
10805 if (phba->fcf.fcfi == fcfi &&
10806 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
10807 vports[i]->fc_myDID == did) {
10808 vport = vports[i];
10809 break;
10810 }
10811 }
10812 lpfc_destroy_vport_work_array(phba, vports);
10813 return vport;
10814}
10815
10816/**
10817 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
10818 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
10819 *
10820 * This function searches through the existing incomplete sequences that have
10821 * been sent to this @vport. If the frame matches one of the incomplete
10822 * sequences then the dbuf in the @dmabuf is added to the list of frames that
10823 * make up that sequence. If no sequence is found that matches this frame then
10824 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
10825 * This function returns a pointer to the first dmabuf in the sequence list that
10826 * the frame was linked to.
10827 **/
10828static struct hbq_dmabuf *
10829lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
10830{
10831 struct fc_frame_header *new_hdr;
10832 struct fc_frame_header *temp_hdr;
10833 struct lpfc_dmabuf *d_buf;
10834 struct lpfc_dmabuf *h_buf;
10835 struct hbq_dmabuf *seq_dmabuf = NULL;
10836 struct hbq_dmabuf *temp_dmabuf = NULL;
10837
4d9ab994 10838 INIT_LIST_HEAD(&dmabuf->dbuf.list);
4f774513
JS
10839 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10840 /* Use the hdr_buf to find the sequence that this frame belongs to */
10841 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10842 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10843 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10844 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10845 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10846 continue;
10847 /* found a pending sequence that matches this frame */
10848 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10849 break;
10850 }
10851 if (!seq_dmabuf) {
10852 /*
10853 * This indicates first frame received for this sequence.
10854 * Queue the buffer on the vport's rcv_buffer_list.
10855 */
10856 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10857 return dmabuf;
10858 }
10859 temp_hdr = seq_dmabuf->hbuf.virt;
10860 if (new_hdr->fh_seq_cnt < temp_hdr->fh_seq_cnt) {
4d9ab994
JS
10861 list_del_init(&seq_dmabuf->hbuf.list);
10862 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
10863 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
4f774513
JS
10864 return dmabuf;
10865 }
10866 /* find the correct place in the sequence to insert this frame */
10867 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
10868 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
10869 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
10870 /*
10871 * If the frame's sequence count is greater than the frame on
10872 * the list then insert the frame right after this frame
10873 */
10874 if (new_hdr->fh_seq_cnt > temp_hdr->fh_seq_cnt) {
10875 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
10876 return seq_dmabuf;
10877 }
10878 }
10879 return NULL;
10880}
10881
6669f9bb
JS
10882/**
10883 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
10884 * @vport: pointer to a vitural port
10885 * @dmabuf: pointer to a dmabuf that describes the FC sequence
10886 *
10887 * This function tries to abort from the partially assembed sequence, described
10888 * by the information from basic abbort @dmabuf. It checks to see whether such
10889 * partially assembled sequence held by the driver. If so, it shall free up all
10890 * the frames from the partially assembled sequence.
10891 *
10892 * Return
10893 * true -- if there is matching partially assembled sequence present and all
10894 * the frames freed with the sequence;
10895 * false -- if there is no matching partially assembled sequence present so
10896 * nothing got aborted in the lower layer driver
10897 **/
10898static bool
10899lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
10900 struct hbq_dmabuf *dmabuf)
10901{
10902 struct fc_frame_header *new_hdr;
10903 struct fc_frame_header *temp_hdr;
10904 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
10905 struct hbq_dmabuf *seq_dmabuf = NULL;
10906
10907 /* Use the hdr_buf to find the sequence that matches this frame */
10908 INIT_LIST_HEAD(&dmabuf->dbuf.list);
10909 INIT_LIST_HEAD(&dmabuf->hbuf.list);
10910 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
10911 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
10912 temp_hdr = (struct fc_frame_header *)h_buf->virt;
10913 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
10914 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
10915 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
10916 continue;
10917 /* found a pending sequence that matches this frame */
10918 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
10919 break;
10920 }
10921
10922 /* Free up all the frames from the partially assembled sequence */
10923 if (seq_dmabuf) {
10924 list_for_each_entry_safe(d_buf, n_buf,
10925 &seq_dmabuf->dbuf.list, list) {
10926 list_del_init(&d_buf->list);
10927 lpfc_in_buf_free(vport->phba, d_buf);
10928 }
10929 return true;
10930 }
10931 return false;
10932}
10933
10934/**
10935 * lpfc_sli4_seq_abort_acc_cmpl - Accept seq abort iocb complete handler
10936 * @phba: Pointer to HBA context object.
10937 * @cmd_iocbq: pointer to the command iocbq structure.
10938 * @rsp_iocbq: pointer to the response iocbq structure.
10939 *
10940 * This function handles the sequence abort accept iocb command complete
10941 * event. It properly releases the memory allocated to the sequence abort
10942 * accept iocb.
10943 **/
10944static void
10945lpfc_sli4_seq_abort_acc_cmpl(struct lpfc_hba *phba,
10946 struct lpfc_iocbq *cmd_iocbq,
10947 struct lpfc_iocbq *rsp_iocbq)
10948{
10949 if (cmd_iocbq)
10950 lpfc_sli_release_iocbq(phba, cmd_iocbq);
10951}
10952
10953/**
10954 * lpfc_sli4_seq_abort_acc - Accept sequence abort
10955 * @phba: Pointer to HBA context object.
10956 * @fc_hdr: pointer to a FC frame header.
10957 *
10958 * This function sends a basic accept to a previous unsol sequence abort
10959 * event after aborting the sequence handling.
10960 **/
10961static void
10962lpfc_sli4_seq_abort_acc(struct lpfc_hba *phba,
10963 struct fc_frame_header *fc_hdr)
10964{
10965 struct lpfc_iocbq *ctiocb = NULL;
10966 struct lpfc_nodelist *ndlp;
10967 uint16_t oxid;
10968 uint32_t sid;
10969 IOCB_t *icmd;
10970
10971 if (!lpfc_is_link_up(phba))
10972 return;
10973
10974 sid = sli4_sid_from_fc_hdr(fc_hdr);
10975 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
10976
10977 ndlp = lpfc_findnode_did(phba->pport, sid);
10978 if (!ndlp) {
10979 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
10980 "1268 Find ndlp returned NULL for oxid:x%x "
10981 "SID:x%x\n", oxid, sid);
10982 return;
10983 }
10984
10985 /* Allocate buffer for acc iocb */
10986 ctiocb = lpfc_sli_get_iocbq(phba);
10987 if (!ctiocb)
10988 return;
10989
10990 icmd = &ctiocb->iocb;
10991 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
10992 icmd->un.xseq64.bdl.bdeSize = 0;
10993 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
10994 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
10995 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
10996
10997 /* Fill in the rest of iocb fields */
10998 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
10999 icmd->ulpBdeCount = 0;
11000 icmd->ulpLe = 1;
11001 icmd->ulpClass = CLASS3;
11002 icmd->ulpContext = ndlp->nlp_rpi;
11003 icmd->un.ulpWord[3] = oxid;
11004
11005 ctiocb->sli4_xritag = NO_XRI;
11006 ctiocb->iocb_cmpl = NULL;
11007 ctiocb->vport = phba->pport;
11008 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_acc_cmpl;
11009
11010 /* Xmit CT abts accept on exchange <xid> */
11011 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
11012 "1200 Xmit CT ABTS ACC on exchange x%x Data: x%x\n",
11013 CMD_XMIT_BLS_RSP64_CX, phba->link_state);
11014 lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
11015}
11016
11017/**
11018 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
11019 * @vport: Pointer to the vport on which this sequence was received
11020 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11021 *
11022 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
11023 * receive sequence is only partially assembed by the driver, it shall abort
11024 * the partially assembled frames for the sequence. Otherwise, if the
11025 * unsolicited receive sequence has been completely assembled and passed to
11026 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
11027 * unsolicited sequence has been aborted. After that, it will issue a basic
11028 * accept to accept the abort.
11029 **/
11030void
11031lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
11032 struct hbq_dmabuf *dmabuf)
11033{
11034 struct lpfc_hba *phba = vport->phba;
11035 struct fc_frame_header fc_hdr;
11036 bool abts_par;
11037
11038 /* Try to abort partially assembled seq */
11039 abts_par = lpfc_sli4_abort_partial_seq(vport, dmabuf);
11040
11041 /* Make a copy of fc_hdr before the dmabuf being released */
11042 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
11043
11044 /* Send abort to ULP if partially seq abort failed */
11045 if (abts_par == false)
11046 lpfc_sli4_send_seq_to_ulp(vport, dmabuf);
11047 else
11048 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11049 /* Send basic accept (BA_ACC) to the abort requester */
11050 lpfc_sli4_seq_abort_acc(phba, &fc_hdr);
11051}
11052
4f774513
JS
11053/**
11054 * lpfc_seq_complete - Indicates if a sequence is complete
11055 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11056 *
11057 * This function checks the sequence, starting with the frame described by
11058 * @dmabuf, to see if all the frames associated with this sequence are present.
11059 * the frames associated with this sequence are linked to the @dmabuf using the
11060 * dbuf list. This function looks for two major things. 1) That the first frame
11061 * has a sequence count of zero. 2) There is a frame with last frame of sequence
11062 * set. 3) That there are no holes in the sequence count. The function will
11063 * return 1 when the sequence is complete, otherwise it will return 0.
11064 **/
11065static int
11066lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
11067{
11068 struct fc_frame_header *hdr;
11069 struct lpfc_dmabuf *d_buf;
11070 struct hbq_dmabuf *seq_dmabuf;
11071 uint32_t fctl;
11072 int seq_count = 0;
11073
11074 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11075 /* make sure first fame of sequence has a sequence count of zero */
11076 if (hdr->fh_seq_cnt != seq_count)
11077 return 0;
11078 fctl = (hdr->fh_f_ctl[0] << 16 |
11079 hdr->fh_f_ctl[1] << 8 |
11080 hdr->fh_f_ctl[2]);
11081 /* If last frame of sequence we can return success. */
11082 if (fctl & FC_FC_END_SEQ)
11083 return 1;
11084 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
11085 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
11086 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11087 /* If there is a hole in the sequence count then fail. */
11088 if (++seq_count != hdr->fh_seq_cnt)
11089 return 0;
11090 fctl = (hdr->fh_f_ctl[0] << 16 |
11091 hdr->fh_f_ctl[1] << 8 |
11092 hdr->fh_f_ctl[2]);
11093 /* If last frame of sequence we can return success. */
11094 if (fctl & FC_FC_END_SEQ)
11095 return 1;
11096 }
11097 return 0;
11098}
11099
11100/**
11101 * lpfc_prep_seq - Prep sequence for ULP processing
11102 * @vport: Pointer to the vport on which this sequence was received
11103 * @dmabuf: pointer to a dmabuf that describes the FC sequence
11104 *
11105 * This function takes a sequence, described by a list of frames, and creates
11106 * a list of iocbq structures to describe the sequence. This iocbq list will be
11107 * used to issue to the generic unsolicited sequence handler. This routine
11108 * returns a pointer to the first iocbq in the list. If the function is unable
11109 * to allocate an iocbq then it throw out the received frames that were not
11110 * able to be described and return a pointer to the first iocbq. If unable to
11111 * allocate any iocbqs (including the first) this function will return NULL.
11112 **/
11113static struct lpfc_iocbq *
11114lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
11115{
11116 struct lpfc_dmabuf *d_buf, *n_buf;
11117 struct lpfc_iocbq *first_iocbq, *iocbq;
11118 struct fc_frame_header *fc_hdr;
11119 uint32_t sid;
11120
11121 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11122 /* remove from receive buffer list */
11123 list_del_init(&seq_dmabuf->hbuf.list);
11124 /* get the Remote Port's SID */
6669f9bb 11125 sid = sli4_sid_from_fc_hdr(fc_hdr);
4f774513
JS
11126 /* Get an iocbq struct to fill in. */
11127 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
11128 if (first_iocbq) {
11129 /* Initialize the first IOCB. */
8fa38513 11130 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
4f774513
JS
11131 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
11132 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
11133 first_iocbq->iocb.ulpContext = be16_to_cpu(fc_hdr->fh_ox_id);
11134 first_iocbq->iocb.unsli3.rcvsli3.vpi =
11135 vport->vpi + vport->phba->vpi_base;
11136 /* put the first buffer into the first IOCBq */
11137 first_iocbq->context2 = &seq_dmabuf->dbuf;
11138 first_iocbq->context3 = NULL;
11139 first_iocbq->iocb.ulpBdeCount = 1;
11140 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11141 LPFC_DATA_BUF_SIZE;
11142 first_iocbq->iocb.un.rcvels.remoteID = sid;
8fa38513 11143 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11144 bf_get(lpfc_rcqe_length,
11145 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11146 }
11147 iocbq = first_iocbq;
11148 /*
11149 * Each IOCBq can have two Buffers assigned, so go through the list
11150 * of buffers for this sequence and save two buffers in each IOCBq
11151 */
11152 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
11153 if (!iocbq) {
11154 lpfc_in_buf_free(vport->phba, d_buf);
11155 continue;
11156 }
11157 if (!iocbq->context3) {
11158 iocbq->context3 = d_buf;
11159 iocbq->iocb.ulpBdeCount++;
11160 iocbq->iocb.unsli3.rcvsli3.bde2.tus.f.bdeSize =
11161 LPFC_DATA_BUF_SIZE;
8fa38513 11162 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11163 bf_get(lpfc_rcqe_length,
11164 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11165 } else {
11166 iocbq = lpfc_sli_get_iocbq(vport->phba);
11167 if (!iocbq) {
11168 if (first_iocbq) {
11169 first_iocbq->iocb.ulpStatus =
11170 IOSTAT_FCP_RSP_ERROR;
11171 first_iocbq->iocb.un.ulpWord[4] =
11172 IOERR_NO_RESOURCES;
11173 }
11174 lpfc_in_buf_free(vport->phba, d_buf);
11175 continue;
11176 }
11177 iocbq->context2 = d_buf;
11178 iocbq->context3 = NULL;
11179 iocbq->iocb.ulpBdeCount = 1;
11180 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
11181 LPFC_DATA_BUF_SIZE;
8fa38513 11182 first_iocbq->iocb.unsli3.rcvsli3.acc_len +=
4d9ab994
JS
11183 bf_get(lpfc_rcqe_length,
11184 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
4f774513
JS
11185 iocbq->iocb.un.rcvels.remoteID = sid;
11186 list_add_tail(&iocbq->list, &first_iocbq->list);
11187 }
11188 }
11189 return first_iocbq;
11190}
11191
6669f9bb
JS
11192static void
11193lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
11194 struct hbq_dmabuf *seq_dmabuf)
11195{
11196 struct fc_frame_header *fc_hdr;
11197 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
11198 struct lpfc_hba *phba = vport->phba;
11199
11200 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
11201 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
11202 if (!iocbq) {
11203 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11204 "2707 Ring %d handler: Failed to allocate "
11205 "iocb Rctl x%x Type x%x received\n",
11206 LPFC_ELS_RING,
11207 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11208 return;
11209 }
11210 if (!lpfc_complete_unsol_iocb(phba,
11211 &phba->sli.ring[LPFC_ELS_RING],
11212 iocbq, fc_hdr->fh_r_ctl,
11213 fc_hdr->fh_type))
11214 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11215 "2540 Ring %d handler: unexpected Rctl "
11216 "x%x Type x%x received\n",
11217 LPFC_ELS_RING,
11218 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
11219
11220 /* Free iocb created in lpfc_prep_seq */
11221 list_for_each_entry_safe(curr_iocb, next_iocb,
11222 &iocbq->list, list) {
11223 list_del_init(&curr_iocb->list);
11224 lpfc_sli_release_iocbq(phba, curr_iocb);
11225 }
11226 lpfc_sli_release_iocbq(phba, iocbq);
11227}
11228
4f774513
JS
11229/**
11230 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
11231 * @phba: Pointer to HBA context object.
11232 *
11233 * This function is called with no lock held. This function processes all
11234 * the received buffers and gives it to upper layers when a received buffer
11235 * indicates that it is the final frame in the sequence. The interrupt
11236 * service routine processes received buffers at interrupt contexts and adds
11237 * received dma buffers to the rb_pend_list queue and signals the worker thread.
11238 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
11239 * appropriate receive function when the final frame in a sequence is received.
11240 **/
4d9ab994
JS
11241void
11242lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
11243 struct hbq_dmabuf *dmabuf)
4f774513 11244{
4d9ab994 11245 struct hbq_dmabuf *seq_dmabuf;
4f774513
JS
11246 struct fc_frame_header *fc_hdr;
11247 struct lpfc_vport *vport;
11248 uint32_t fcfi;
4f774513
JS
11249
11250 /* Clear hba flag and get all received buffers into the cmplq */
11251 spin_lock_irq(&phba->hbalock);
11252 phba->hba_flag &= ~HBA_RECEIVE_BUFFER;
4f774513
JS
11253 spin_unlock_irq(&phba->hbalock);
11254
11255 /* Process each received buffer */
4d9ab994
JS
11256 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
11257 /* check to see if this a valid type of frame */
11258 if (lpfc_fc_frame_check(phba, fc_hdr)) {
11259 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11260 return;
11261 }
11262 fcfi = bf_get(lpfc_rcqe_fcf_id, &dmabuf->cq_event.cqe.rcqe_cmpl);
11263 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
11264 if (!vport) {
11265 /* throw out the frame */
11266 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11267 return;
11268 }
6669f9bb
JS
11269 /* Handle the basic abort sequence (BA_ABTS) event */
11270 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
11271 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
11272 return;
11273 }
11274
4d9ab994
JS
11275 /* Link this frame */
11276 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
11277 if (!seq_dmabuf) {
11278 /* unable to add frame to vport - throw it out */
11279 lpfc_in_buf_free(phba, &dmabuf->dbuf);
11280 return;
11281 }
11282 /* If not last frame in sequence continue processing frames. */
11283 if (!lpfc_seq_complete(seq_dmabuf)) {
11284 /*
11285 * When saving off frames post a new one and mark this
11286 * frame to be freed when it is finished.
11287 **/
11288 lpfc_sli_hbqbuf_fill_hbqs(phba, LPFC_ELS_HBQ, 1);
11289 dmabuf->tag = -1;
11290 return;
11291 }
6669f9bb
JS
11292 /* Send the complete sequence to the upper layer protocol */
11293 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
4f774513 11294}
6fb120a7
JS
11295
11296/**
11297 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
11298 * @phba: pointer to lpfc hba data structure.
11299 *
11300 * This routine is invoked to post rpi header templates to the
11301 * HBA consistent with the SLI-4 interface spec. This routine
11302 * posts a PAGE_SIZE memory region to the port to hold up to
11303 * PAGE_SIZE modulo 64 rpi context headers.
11304 *
11305 * This routine does not require any locks. It's usage is expected
11306 * to be driver load or reset recovery when the driver is
11307 * sequential.
11308 *
11309 * Return codes
11310 * 0 - sucessful
11311 * EIO - The mailbox failed to complete successfully.
11312 * When this error occurs, the driver is not guaranteed
11313 * to have any rpi regions posted to the device and
11314 * must either attempt to repost the regions or take a
11315 * fatal error.
11316 **/
11317int
11318lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
11319{
11320 struct lpfc_rpi_hdr *rpi_page;
11321 uint32_t rc = 0;
11322
11323 /* Post all rpi memory regions to the port. */
11324 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
11325 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
11326 if (rc != MBX_SUCCESS) {
11327 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11328 "2008 Error %d posting all rpi "
11329 "headers\n", rc);
11330 rc = -EIO;
11331 break;
11332 }
11333 }
11334
11335 return rc;
11336}
11337
11338/**
11339 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
11340 * @phba: pointer to lpfc hba data structure.
11341 * @rpi_page: pointer to the rpi memory region.
11342 *
11343 * This routine is invoked to post a single rpi header to the
11344 * HBA consistent with the SLI-4 interface spec. This memory region
11345 * maps up to 64 rpi context regions.
11346 *
11347 * Return codes
11348 * 0 - sucessful
11349 * ENOMEM - No available memory
11350 * EIO - The mailbox failed to complete successfully.
11351 **/
11352int
11353lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
11354{
11355 LPFC_MBOXQ_t *mboxq;
11356 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
11357 uint32_t rc = 0;
11358 uint32_t mbox_tmo;
11359 uint32_t shdr_status, shdr_add_status;
11360 union lpfc_sli4_cfg_shdr *shdr;
11361
11362 /* The port is notified of the header region via a mailbox command. */
11363 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11364 if (!mboxq) {
11365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11366 "2001 Unable to allocate memory for issuing "
11367 "SLI_CONFIG_SPECIAL mailbox command\n");
11368 return -ENOMEM;
11369 }
11370
11371 /* Post all rpi memory regions to the port. */
11372 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
11373 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_SLI4_CONFIG);
11374 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11375 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
11376 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
11377 sizeof(struct mbox_header), LPFC_SLI4_MBX_EMBED);
11378 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
11379 hdr_tmpl, rpi_page->page_count);
11380 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
11381 rpi_page->start_rpi);
11382 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
11383 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
f1126688 11384 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6fb120a7
JS
11385 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
11386 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11387 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11388 if (rc != MBX_TIMEOUT)
11389 mempool_free(mboxq, phba->mbox_mem_pool);
11390 if (shdr_status || shdr_add_status || rc) {
11391 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11392 "2514 POST_RPI_HDR mailbox failed with "
11393 "status x%x add_status x%x, mbx status x%x\n",
11394 shdr_status, shdr_add_status, rc);
11395 rc = -ENXIO;
11396 }
11397 return rc;
11398}
11399
11400/**
11401 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
11402 * @phba: pointer to lpfc hba data structure.
11403 *
11404 * This routine is invoked to post rpi header templates to the
11405 * HBA consistent with the SLI-4 interface spec. This routine
11406 * posts a PAGE_SIZE memory region to the port to hold up to
11407 * PAGE_SIZE modulo 64 rpi context headers.
11408 *
11409 * Returns
11410 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if sucessful
11411 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
11412 **/
11413int
11414lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
11415{
11416 int rpi;
11417 uint16_t max_rpi, rpi_base, rpi_limit;
11418 uint16_t rpi_remaining;
11419 struct lpfc_rpi_hdr *rpi_hdr;
11420
11421 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
11422 rpi_base = phba->sli4_hba.max_cfg_param.rpi_base;
11423 rpi_limit = phba->sli4_hba.next_rpi;
11424
11425 /*
11426 * The valid rpi range is not guaranteed to be zero-based. Start
11427 * the search at the rpi_base as reported by the port.
11428 */
11429 spin_lock_irq(&phba->hbalock);
11430 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, rpi_base);
11431 if (rpi >= rpi_limit || rpi < rpi_base)
11432 rpi = LPFC_RPI_ALLOC_ERROR;
11433 else {
11434 set_bit(rpi, phba->sli4_hba.rpi_bmask);
11435 phba->sli4_hba.max_cfg_param.rpi_used++;
11436 phba->sli4_hba.rpi_count++;
11437 }
11438
11439 /*
11440 * Don't try to allocate more rpi header regions if the device limit
11441 * on available rpis max has been exhausted.
11442 */
11443 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
11444 (phba->sli4_hba.rpi_count >= max_rpi)) {
11445 spin_unlock_irq(&phba->hbalock);
11446 return rpi;
11447 }
11448
11449 /*
11450 * If the driver is running low on rpi resources, allocate another
11451 * page now. Note that the next_rpi value is used because
11452 * it represents how many are actually in use whereas max_rpi notes
11453 * how many are supported max by the device.
11454 */
11455 rpi_remaining = phba->sli4_hba.next_rpi - rpi_base -
11456 phba->sli4_hba.rpi_count;
11457 spin_unlock_irq(&phba->hbalock);
11458 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
11459 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
11460 if (!rpi_hdr) {
11461 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11462 "2002 Error Could not grow rpi "
11463 "count\n");
11464 } else {
11465 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
11466 }
11467 }
11468
11469 return rpi;
11470}
11471
11472/**
11473 * lpfc_sli4_free_rpi - Release an rpi for reuse.
11474 * @phba: pointer to lpfc hba data structure.
11475 *
11476 * This routine is invoked to release an rpi to the pool of
11477 * available rpis maintained by the driver.
11478 **/
11479void
11480lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
11481{
11482 spin_lock_irq(&phba->hbalock);
11483 clear_bit(rpi, phba->sli4_hba.rpi_bmask);
11484 phba->sli4_hba.rpi_count--;
11485 phba->sli4_hba.max_cfg_param.rpi_used--;
11486 spin_unlock_irq(&phba->hbalock);
11487}
11488
11489/**
11490 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
11491 * @phba: pointer to lpfc hba data structure.
11492 *
11493 * This routine is invoked to remove the memory region that
11494 * provided rpi via a bitmask.
11495 **/
11496void
11497lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
11498{
11499 kfree(phba->sli4_hba.rpi_bmask);
11500}
11501
11502/**
11503 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
11504 * @phba: pointer to lpfc hba data structure.
11505 *
11506 * This routine is invoked to remove the memory region that
11507 * provided rpi via a bitmask.
11508 **/
11509int
11510lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp)
11511{
11512 LPFC_MBOXQ_t *mboxq;
11513 struct lpfc_hba *phba = ndlp->phba;
11514 int rc;
11515
11516 /* The port is notified of the header region via a mailbox command. */
11517 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11518 if (!mboxq)
11519 return -ENOMEM;
11520
11521 /* Post all rpi memory regions to the port. */
11522 lpfc_resume_rpi(mboxq, ndlp);
11523 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11524 if (rc == MBX_NOT_FINISHED) {
11525 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11526 "2010 Resume RPI Mailbox failed "
11527 "status %d, mbxStatus x%x\n", rc,
11528 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
11529 mempool_free(mboxq, phba->mbox_mem_pool);
11530 return -EIO;
11531 }
11532 return 0;
11533}
11534
11535/**
11536 * lpfc_sli4_init_vpi - Initialize a vpi with the port
11537 * @phba: pointer to lpfc hba data structure.
11538 * @vpi: vpi value to activate with the port.
11539 *
11540 * This routine is invoked to activate a vpi with the
11541 * port when the host intends to use vports with a
11542 * nonzero vpi.
11543 *
11544 * Returns:
11545 * 0 success
11546 * -Evalue otherwise
11547 **/
11548int
11549lpfc_sli4_init_vpi(struct lpfc_hba *phba, uint16_t vpi)
11550{
11551 LPFC_MBOXQ_t *mboxq;
11552 int rc = 0;
6a9c52cf 11553 int retval = MBX_SUCCESS;
6fb120a7
JS
11554 uint32_t mbox_tmo;
11555
11556 if (vpi == 0)
11557 return -EINVAL;
11558 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11559 if (!mboxq)
11560 return -ENOMEM;
1c6834a7 11561 lpfc_init_vpi(phba, mboxq, vpi);
6fb120a7
JS
11562 mbox_tmo = lpfc_mbox_tmo_val(phba, MBX_INIT_VPI);
11563 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
6fb120a7
JS
11564 if (rc != MBX_SUCCESS) {
11565 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11566 "2022 INIT VPI Mailbox failed "
11567 "status %d, mbxStatus x%x\n", rc,
11568 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
6a9c52cf 11569 retval = -EIO;
6fb120a7 11570 }
6a9c52cf
JS
11571 if (rc != MBX_TIMEOUT)
11572 mempool_free(mboxq, phba->mbox_mem_pool);
11573
11574 return retval;
6fb120a7
JS
11575}
11576
11577/**
11578 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
11579 * @phba: pointer to lpfc hba data structure.
11580 * @mboxq: Pointer to mailbox object.
11581 *
11582 * This routine is invoked to manually add a single FCF record. The caller
11583 * must pass a completely initialized FCF_Record. This routine takes
11584 * care of the nonembedded mailbox operations.
11585 **/
11586static void
11587lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
11588{
11589 void *virt_addr;
11590 union lpfc_sli4_cfg_shdr *shdr;
11591 uint32_t shdr_status, shdr_add_status;
11592
11593 virt_addr = mboxq->sge_array->addr[0];
11594 /* The IOCTL status is embedded in the mailbox subheader. */
11595 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
11596 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
11597 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
11598
11599 if ((shdr_status || shdr_add_status) &&
11600 (shdr_status != STATUS_FCF_IN_USE))
11601 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11602 "2558 ADD_FCF_RECORD mailbox failed with "
11603 "status x%x add_status x%x\n",
11604 shdr_status, shdr_add_status);
11605
11606 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11607}
11608
11609/**
11610 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
11611 * @phba: pointer to lpfc hba data structure.
11612 * @fcf_record: pointer to the initialized fcf record to add.
11613 *
11614 * This routine is invoked to manually add a single FCF record. The caller
11615 * must pass a completely initialized FCF_Record. This routine takes
11616 * care of the nonembedded mailbox operations.
11617 **/
11618int
11619lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
11620{
11621 int rc = 0;
11622 LPFC_MBOXQ_t *mboxq;
11623 uint8_t *bytep;
11624 void *virt_addr;
11625 dma_addr_t phys_addr;
11626 struct lpfc_mbx_sge sge;
11627 uint32_t alloc_len, req_len;
11628 uint32_t fcfindex;
11629
11630 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11631 if (!mboxq) {
11632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11633 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
11634 return -ENOMEM;
11635 }
11636
11637 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
11638 sizeof(uint32_t);
11639
11640 /* Allocate DMA memory and set up the non-embedded mailbox command */
11641 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11642 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
11643 req_len, LPFC_SLI4_MBX_NEMBED);
11644 if (alloc_len < req_len) {
11645 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11646 "2523 Allocated DMA memory size (x%x) is "
11647 "less than the requested DMA memory "
11648 "size (x%x)\n", alloc_len, req_len);
11649 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11650 return -ENOMEM;
11651 }
11652
11653 /*
11654 * Get the first SGE entry from the non-embedded DMA memory. This
11655 * routine only uses a single SGE.
11656 */
11657 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11658 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
11659 virt_addr = mboxq->sge_array->addr[0];
11660 /*
11661 * Configure the FCF record for FCFI 0. This is the driver's
11662 * hardcoded default and gets used in nonFIP mode.
11663 */
11664 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
11665 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11666 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
11667
11668 /*
11669 * Copy the fcf_index and the FCF Record Data. The data starts after
11670 * the FCoE header plus word10. The data copy needs to be endian
11671 * correct.
11672 */
11673 bytep += sizeof(uint32_t);
11674 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
11675 mboxq->vport = phba->pport;
11676 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
11677 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11678 if (rc == MBX_NOT_FINISHED) {
11679 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11680 "2515 ADD_FCF_RECORD mailbox failed with "
11681 "status 0x%x\n", rc);
11682 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11683 rc = -EIO;
11684 } else
11685 rc = 0;
11686
11687 return rc;
11688}
11689
11690/**
11691 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
11692 * @phba: pointer to lpfc hba data structure.
11693 * @fcf_record: pointer to the fcf record to write the default data.
11694 * @fcf_index: FCF table entry index.
11695 *
11696 * This routine is invoked to build the driver's default FCF record. The
11697 * values used are hardcoded. This routine handles memory initialization.
11698 *
11699 **/
11700void
11701lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
11702 struct fcf_record *fcf_record,
11703 uint16_t fcf_index)
11704{
11705 memset(fcf_record, 0, sizeof(struct fcf_record));
11706 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
11707 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
11708 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
11709 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
11710 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
11711 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
11712 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
11713 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
11714 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
11715 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
11716 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
11717 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
11718 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
0c287589 11719 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
6fb120a7
JS
11720 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
11721 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
11722 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
11723 /* Set the VLAN bit map */
11724 if (phba->valid_vlan) {
11725 fcf_record->vlan_bitmap[phba->vlan_id / 8]
11726 = 1 << (phba->vlan_id % 8);
11727 }
11728}
11729
11730/**
11731 * lpfc_sli4_read_fcf_record - Read the driver's default FCF Record.
11732 * @phba: pointer to lpfc hba data structure.
11733 * @fcf_index: FCF table entry offset.
11734 *
11735 * This routine is invoked to read up to @fcf_num of FCF record from the
11736 * device starting with the given @fcf_index.
11737 **/
11738int
11739lpfc_sli4_read_fcf_record(struct lpfc_hba *phba, uint16_t fcf_index)
11740{
11741 int rc = 0, error;
11742 LPFC_MBOXQ_t *mboxq;
11743 void *virt_addr;
11744 dma_addr_t phys_addr;
11745 uint8_t *bytep;
11746 struct lpfc_mbx_sge sge;
11747 uint32_t alloc_len, req_len;
11748 struct lpfc_mbx_read_fcf_tbl *read_fcf;
11749
32b9793f 11750 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
6fb120a7
JS
11751 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11752 if (!mboxq) {
11753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11754 "2000 Failed to allocate mbox for "
11755 "READ_FCF cmd\n");
4d9ab994
JS
11756 error = -ENOMEM;
11757 goto fail_fcfscan;
6fb120a7
JS
11758 }
11759
11760 req_len = sizeof(struct fcf_record) +
11761 sizeof(union lpfc_sli4_cfg_shdr) + 2 * sizeof(uint32_t);
11762
11763 /* Set up READ_FCF SLI4_CONFIG mailbox-ioctl command */
11764 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
11765 LPFC_MBOX_OPCODE_FCOE_READ_FCF_TABLE, req_len,
11766 LPFC_SLI4_MBX_NEMBED);
11767
11768 if (alloc_len < req_len) {
11769 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11770 "0291 Allocated DMA memory size (x%x) is "
11771 "less than the requested DMA memory "
11772 "size (x%x)\n", alloc_len, req_len);
4d9ab994
JS
11773 error = -ENOMEM;
11774 goto fail_fcfscan;
6fb120a7
JS
11775 }
11776
11777 /* Get the first SGE entry from the non-embedded DMA memory. This
11778 * routine only uses a single SGE.
11779 */
11780 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
11781 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
6fb120a7
JS
11782 virt_addr = mboxq->sge_array->addr[0];
11783 read_fcf = (struct lpfc_mbx_read_fcf_tbl *)virt_addr;
11784
11785 /* Set up command fields */
11786 bf_set(lpfc_mbx_read_fcf_tbl_indx, &read_fcf->u.request, fcf_index);
11787 /* Perform necessary endian conversion */
11788 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
11789 lpfc_sli_pcimem_bcopy(bytep, bytep, sizeof(uint32_t));
11790 mboxq->vport = phba->pport;
11791 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_record;
11792 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
11793 if (rc == MBX_NOT_FINISHED) {
6fb120a7 11794 error = -EIO;
32b9793f
JS
11795 } else {
11796 spin_lock_irq(&phba->hbalock);
11797 phba->hba_flag |= FCF_DISC_INPROGRESS;
11798 spin_unlock_irq(&phba->hbalock);
6fb120a7 11799 error = 0;
32b9793f 11800 }
4d9ab994
JS
11801fail_fcfscan:
11802 if (error) {
11803 if (mboxq)
11804 lpfc_sli4_mbox_cmd_free(phba, mboxq);
11805 /* FCF scan failed, clear FCF_DISC_INPROGRESS flag */
11806 spin_lock_irq(&phba->hbalock);
11807 phba->hba_flag &= ~FCF_DISC_INPROGRESS;
11808 spin_unlock_irq(&phba->hbalock);
11809 }
6fb120a7
JS
11810 return error;
11811}
a0c87cbd
JS
11812
11813/**
11814 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
11815 * @phba: pointer to lpfc hba data structure.
11816 *
11817 * This function read region 23 and parse TLV for port status to
11818 * decide if the user disaled the port. If the TLV indicates the
11819 * port is disabled, the hba_flag is set accordingly.
11820 **/
11821void
11822lpfc_sli_read_link_ste(struct lpfc_hba *phba)
11823{
11824 LPFC_MBOXQ_t *pmb = NULL;
11825 MAILBOX_t *mb;
11826 uint8_t *rgn23_data = NULL;
11827 uint32_t offset = 0, data_size, sub_tlv_len, tlv_offset;
11828 int rc;
11829
11830 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
11831 if (!pmb) {
11832 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11833 "2600 lpfc_sli_read_serdes_param failed to"
11834 " allocate mailbox memory\n");
11835 goto out;
11836 }
11837 mb = &pmb->u.mb;
11838
11839 /* Get adapter Region 23 data */
11840 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
11841 if (!rgn23_data)
11842 goto out;
11843
11844 do {
11845 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
11846 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
11847
11848 if (rc != MBX_SUCCESS) {
11849 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
11850 "2601 lpfc_sli_read_link_ste failed to"
11851 " read config region 23 rc 0x%x Status 0x%x\n",
11852 rc, mb->mbxStatus);
11853 mb->un.varDmp.word_cnt = 0;
11854 }
11855 /*
11856 * dump mem may return a zero when finished or we got a
11857 * mailbox error, either way we are done.
11858 */
11859 if (mb->un.varDmp.word_cnt == 0)
11860 break;
11861 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
11862 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
11863
11864 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
11865 rgn23_data + offset,
11866 mb->un.varDmp.word_cnt);
11867 offset += mb->un.varDmp.word_cnt;
11868 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
11869
11870 data_size = offset;
11871 offset = 0;
11872
11873 if (!data_size)
11874 goto out;
11875
11876 /* Check the region signature first */
11877 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
11878 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11879 "2619 Config region 23 has bad signature\n");
11880 goto out;
11881 }
11882 offset += 4;
11883
11884 /* Check the data structure version */
11885 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
11886 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11887 "2620 Config region 23 has bad version\n");
11888 goto out;
11889 }
11890 offset += 4;
11891
11892 /* Parse TLV entries in the region */
11893 while (offset < data_size) {
11894 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
11895 break;
11896 /*
11897 * If the TLV is not driver specific TLV or driver id is
11898 * not linux driver id, skip the record.
11899 */
11900 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
11901 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
11902 (rgn23_data[offset + 3] != 0)) {
11903 offset += rgn23_data[offset + 1] * 4 + 4;
11904 continue;
11905 }
11906
11907 /* Driver found a driver specific TLV in the config region */
11908 sub_tlv_len = rgn23_data[offset + 1] * 4;
11909 offset += 4;
11910 tlv_offset = 0;
11911
11912 /*
11913 * Search for configured port state sub-TLV.
11914 */
11915 while ((offset < data_size) &&
11916 (tlv_offset < sub_tlv_len)) {
11917 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
11918 offset += 4;
11919 tlv_offset += 4;
11920 break;
11921 }
11922 if (rgn23_data[offset] != PORT_STE_TYPE) {
11923 offset += rgn23_data[offset + 1] * 4 + 4;
11924 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
11925 continue;
11926 }
11927
11928 /* This HBA contains PORT_STE configured */
11929 if (!rgn23_data[offset + 2])
11930 phba->hba_flag |= LINK_DISABLED;
11931
11932 goto out;
11933 }
11934 }
11935out:
11936 if (pmb)
11937 mempool_free(pmb, phba->mbox_mem_pool);
11938 kfree(rgn23_data);
11939 return;
11940}
This page took 1.206112 seconds and 5 git commands to generate.