]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/scsi/lpfc/lpfc_sli.c
f4f77c5b0c83da0520eb868b79095f836179cecf
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / lpfc / lpfc_sli.c
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
9 * This program is free software; you can redistribute it and/or *
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. *
20 *******************************************************************/
21
22 #include <linux/blkdev.h>
23 #include <linux/pci.h>
24 #include <linux/interrupt.h>
25 #include <linux/delay.h>
26 #include <linux/slab.h>
27 #include <linux/lockdep.h>
28
29 #include <scsi/scsi.h>
30 #include <scsi/scsi_cmnd.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_host.h>
33 #include <scsi/scsi_transport_fc.h>
34 #include <scsi/fc/fc_fs.h>
35 #include <linux/aer.h>
36
37 #include "lpfc_hw4.h"
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_sli4.h"
41 #include "lpfc_nl.h"
42 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_compat.h"
48 #include "lpfc_debugfs.h"
49 #include "lpfc_vport.h"
50
51 /* There are only four IOCB completion types. */
52 typedef enum _lpfc_iocb_type {
53 LPFC_UNKNOWN_IOCB,
54 LPFC_UNSOL_IOCB,
55 LPFC_SOL_IOCB,
56 LPFC_ABORT_IOCB
57 } lpfc_iocb_type;
58
59
60 /* Provide function prototypes local to this module. */
61 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
62 uint32_t);
63 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
64 uint8_t *, uint32_t *);
65 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
66 struct lpfc_iocbq *);
67 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
68 struct hbq_dmabuf *);
69 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
70 struct lpfc_cqe *);
71 static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
72 int);
73 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
74 uint32_t);
75 static bool lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba);
76 static bool lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba);
77
78 static IOCB_t *
79 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
80 {
81 return &iocbq->iocb;
82 }
83
84 /**
85 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
86 * @q: The Work Queue to operate on.
87 * @wqe: The work Queue Entry to put on the Work queue.
88 *
89 * This routine will copy the contents of @wqe to the next available entry on
90 * the @q. This function will then ring the Work Queue Doorbell to signal the
91 * HBA to start processing the Work Queue Entry. This function returns 0 if
92 * successful. If no entries are available on @q then this function will return
93 * -ENOMEM.
94 * The caller is expected to hold the hbalock when calling this routine.
95 **/
96 static uint32_t
97 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
98 {
99 union lpfc_wqe *temp_wqe;
100 struct lpfc_register doorbell;
101 uint32_t host_index;
102 uint32_t idx;
103
104 /* sanity check on queue memory */
105 if (unlikely(!q))
106 return -ENOMEM;
107 temp_wqe = q->qe[q->host_index].wqe;
108
109 /* If the host has not yet processed the next entry then we are done */
110 idx = ((q->host_index + 1) % q->entry_count);
111 if (idx == q->hba_index) {
112 q->WQ_overflow++;
113 return -ENOMEM;
114 }
115 q->WQ_posted++;
116 /* set consumption flag every once in a while */
117 if (!((q->host_index + 1) % q->entry_repost))
118 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
119 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
120 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
121 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
122
123 /* Update the host index before invoking device */
124 host_index = q->host_index;
125
126 q->host_index = idx;
127
128 /* Ring Doorbell */
129 doorbell.word0 = 0;
130 if (q->db_format == LPFC_DB_LIST_FORMAT) {
131 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
132 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
133 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
134 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
135 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
136 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
137 } else {
138 return -EINVAL;
139 }
140 writel(doorbell.word0, q->db_regaddr);
141
142 return 0;
143 }
144
145 /**
146 * lpfc_sli4_wq_release - Updates internal hba index for WQ
147 * @q: The Work Queue to operate on.
148 * @index: The index to advance the hba index to.
149 *
150 * This routine will update the HBA index of a queue to reflect consumption of
151 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
152 * an entry the host calls this function to update the queue's internal
153 * pointers. This routine returns the number of entries that were consumed by
154 * the HBA.
155 **/
156 static uint32_t
157 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
158 {
159 uint32_t released = 0;
160
161 /* sanity check on queue memory */
162 if (unlikely(!q))
163 return 0;
164
165 if (q->hba_index == index)
166 return 0;
167 do {
168 q->hba_index = ((q->hba_index + 1) % q->entry_count);
169 released++;
170 } while (q->hba_index != index);
171 return released;
172 }
173
174 /**
175 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
176 * @q: The Mailbox Queue to operate on.
177 * @wqe: The Mailbox Queue Entry to put on the Work queue.
178 *
179 * This routine will copy the contents of @mqe to the next available entry on
180 * the @q. This function will then ring the Work Queue Doorbell to signal the
181 * HBA to start processing the Work Queue Entry. This function returns 0 if
182 * successful. If no entries are available on @q then this function will return
183 * -ENOMEM.
184 * The caller is expected to hold the hbalock when calling this routine.
185 **/
186 static uint32_t
187 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
188 {
189 struct lpfc_mqe *temp_mqe;
190 struct lpfc_register doorbell;
191
192 /* sanity check on queue memory */
193 if (unlikely(!q))
194 return -ENOMEM;
195 temp_mqe = q->qe[q->host_index].mqe;
196
197 /* If the host has not yet processed the next entry then we are done */
198 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
199 return -ENOMEM;
200 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
201 /* Save off the mailbox pointer for completion */
202 q->phba->mbox = (MAILBOX_t *)temp_mqe;
203
204 /* Update the host index before invoking device */
205 q->host_index = ((q->host_index + 1) % q->entry_count);
206
207 /* Ring Doorbell */
208 doorbell.word0 = 0;
209 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
210 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
211 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
212 return 0;
213 }
214
215 /**
216 * lpfc_sli4_mq_release - Updates internal hba index for MQ
217 * @q: The Mailbox Queue to operate on.
218 *
219 * This routine will update the HBA index of a queue to reflect consumption of
220 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
221 * an entry the host calls this function to update the queue's internal
222 * pointers. This routine returns the number of entries that were consumed by
223 * the HBA.
224 **/
225 static uint32_t
226 lpfc_sli4_mq_release(struct lpfc_queue *q)
227 {
228 /* sanity check on queue memory */
229 if (unlikely(!q))
230 return 0;
231
232 /* Clear the mailbox pointer for completion */
233 q->phba->mbox = NULL;
234 q->hba_index = ((q->hba_index + 1) % q->entry_count);
235 return 1;
236 }
237
238 /**
239 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
240 * @q: The Event Queue to get the first valid EQE from
241 *
242 * This routine will get the first valid Event Queue Entry from @q, update
243 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
244 * the Queue (no more work to do), or the Queue is full of EQEs that have been
245 * processed, but not popped back to the HBA then this routine will return NULL.
246 **/
247 static struct lpfc_eqe *
248 lpfc_sli4_eq_get(struct lpfc_queue *q)
249 {
250 struct lpfc_eqe *eqe;
251 uint32_t idx;
252
253 /* sanity check on queue memory */
254 if (unlikely(!q))
255 return NULL;
256 eqe = q->qe[q->hba_index].eqe;
257
258 /* If the next EQE is not valid then we are done */
259 if (!bf_get_le32(lpfc_eqe_valid, eqe))
260 return NULL;
261 /* If the host has not yet processed the next entry then we are done */
262 idx = ((q->hba_index + 1) % q->entry_count);
263 if (idx == q->host_index)
264 return NULL;
265
266 q->hba_index = idx;
267
268 /*
269 * insert barrier for instruction interlock : data from the hardware
270 * must have the valid bit checked before it can be copied and acted
271 * upon. Given what was seen in lpfc_sli4_cq_get() of speculative
272 * instructions allowing action on content before valid bit checked,
273 * add barrier here as well. May not be needed as "content" is a
274 * single 32-bit entity here (vs multi word structure for cq's).
275 */
276 mb();
277 return eqe;
278 }
279
280 /**
281 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
282 * @q: The Event Queue to disable interrupts
283 *
284 **/
285 static inline void
286 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
287 {
288 struct lpfc_register doorbell;
289
290 doorbell.word0 = 0;
291 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
292 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
293 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
294 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
295 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
296 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
297 }
298
299 /**
300 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
301 * @q: The Event Queue that the host has completed processing for.
302 * @arm: Indicates whether the host wants to arms this CQ.
303 *
304 * This routine will mark all Event Queue Entries on @q, from the last
305 * known completed entry to the last entry that was processed, as completed
306 * by clearing the valid bit for each completion queue entry. Then it will
307 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
308 * The internal host index in the @q will be updated by this routine to indicate
309 * that the host has finished processing the entries. The @arm parameter
310 * indicates that the queue should be rearmed when ringing the doorbell.
311 *
312 * This function will return the number of EQEs that were popped.
313 **/
314 uint32_t
315 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
316 {
317 uint32_t released = 0;
318 struct lpfc_eqe *temp_eqe;
319 struct lpfc_register doorbell;
320
321 /* sanity check on queue memory */
322 if (unlikely(!q))
323 return 0;
324
325 /* while there are valid entries */
326 while (q->hba_index != q->host_index) {
327 temp_eqe = q->qe[q->host_index].eqe;
328 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
329 released++;
330 q->host_index = ((q->host_index + 1) % q->entry_count);
331 }
332 if (unlikely(released == 0 && !arm))
333 return 0;
334
335 /* ring doorbell for number popped */
336 doorbell.word0 = 0;
337 if (arm) {
338 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
339 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
340 }
341 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
342 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
343 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
344 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
345 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
346 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
347 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
348 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
349 readl(q->phba->sli4_hba.EQCQDBregaddr);
350 return released;
351 }
352
353 /**
354 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
355 * @q: The Completion Queue to get the first valid CQE from
356 *
357 * This routine will get the first valid Completion Queue Entry from @q, update
358 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
359 * the Queue (no more work to do), or the Queue is full of CQEs that have been
360 * processed, but not popped back to the HBA then this routine will return NULL.
361 **/
362 static struct lpfc_cqe *
363 lpfc_sli4_cq_get(struct lpfc_queue *q)
364 {
365 struct lpfc_cqe *cqe;
366 uint32_t idx;
367
368 /* sanity check on queue memory */
369 if (unlikely(!q))
370 return NULL;
371
372 /* If the next CQE is not valid then we are done */
373 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
374 return NULL;
375 /* If the host has not yet processed the next entry then we are done */
376 idx = ((q->hba_index + 1) % q->entry_count);
377 if (idx == q->host_index)
378 return NULL;
379
380 cqe = q->qe[q->hba_index].cqe;
381 q->hba_index = idx;
382
383 /*
384 * insert barrier for instruction interlock : data from the hardware
385 * must have the valid bit checked before it can be copied and acted
386 * upon. Speculative instructions were allowing a bcopy at the start
387 * of lpfc_sli4_fp_handle_wcqe(), which is called immediately
388 * after our return, to copy data before the valid bit check above
389 * was done. As such, some of the copied data was stale. The barrier
390 * ensures the check is before any data is copied.
391 */
392 mb();
393 return cqe;
394 }
395
396 /**
397 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
398 * @q: The Completion Queue that the host has completed processing for.
399 * @arm: Indicates whether the host wants to arms this CQ.
400 *
401 * This routine will mark all Completion queue entries on @q, from the last
402 * known completed entry to the last entry that was processed, as completed
403 * by clearing the valid bit for each completion queue entry. Then it will
404 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
405 * The internal host index in the @q will be updated by this routine to indicate
406 * that the host has finished processing the entries. The @arm parameter
407 * indicates that the queue should be rearmed when ringing the doorbell.
408 *
409 * This function will return the number of CQEs that were released.
410 **/
411 uint32_t
412 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
413 {
414 uint32_t released = 0;
415 struct lpfc_cqe *temp_qe;
416 struct lpfc_register doorbell;
417
418 /* sanity check on queue memory */
419 if (unlikely(!q))
420 return 0;
421 /* while there are valid entries */
422 while (q->hba_index != q->host_index) {
423 temp_qe = q->qe[q->host_index].cqe;
424 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
425 released++;
426 q->host_index = ((q->host_index + 1) % q->entry_count);
427 }
428 if (unlikely(released == 0 && !arm))
429 return 0;
430
431 /* ring doorbell for number popped */
432 doorbell.word0 = 0;
433 if (arm)
434 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
435 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
436 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
437 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
438 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
439 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
440 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
441 return released;
442 }
443
444 /**
445 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
446 * @q: The Header Receive Queue to operate on.
447 * @wqe: The Receive Queue Entry to put on the Receive queue.
448 *
449 * This routine will copy the contents of @wqe to the next available entry on
450 * the @q. This function will then ring the Receive Queue Doorbell to signal the
451 * HBA to start processing the Receive Queue Entry. This function returns the
452 * index that the rqe was copied to if successful. If no entries are available
453 * on @q then this function will return -ENOMEM.
454 * The caller is expected to hold the hbalock when calling this routine.
455 **/
456 static int
457 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
458 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
459 {
460 struct lpfc_rqe *temp_hrqe;
461 struct lpfc_rqe *temp_drqe;
462 struct lpfc_register doorbell;
463 int put_index;
464
465 /* sanity check on queue memory */
466 if (unlikely(!hq) || unlikely(!dq))
467 return -ENOMEM;
468 put_index = hq->host_index;
469 temp_hrqe = hq->qe[hq->host_index].rqe;
470 temp_drqe = dq->qe[dq->host_index].rqe;
471
472 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
473 return -EINVAL;
474 if (hq->host_index != dq->host_index)
475 return -EINVAL;
476 /* If the host has not yet processed the next entry then we are done */
477 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
478 return -EBUSY;
479 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
480 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
481
482 /* Update the host index to point to the next slot */
483 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
484 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
485
486 /* Ring The Header Receive Queue Doorbell */
487 if (!(hq->host_index % hq->entry_repost)) {
488 doorbell.word0 = 0;
489 if (hq->db_format == LPFC_DB_RING_FORMAT) {
490 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
491 hq->entry_repost);
492 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
493 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
494 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
495 hq->entry_repost);
496 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
497 hq->host_index);
498 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
499 } else {
500 return -EINVAL;
501 }
502 writel(doorbell.word0, hq->db_regaddr);
503 }
504 return put_index;
505 }
506
507 /**
508 * lpfc_sli4_rq_release - Updates internal hba index for RQ
509 * @q: The Header Receive Queue to operate on.
510 *
511 * This routine will update the HBA index of a queue to reflect consumption of
512 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
513 * consumed an entry the host calls this function to update the queue's
514 * internal pointers. This routine returns the number of entries that were
515 * consumed by the HBA.
516 **/
517 static uint32_t
518 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
519 {
520 /* sanity check on queue memory */
521 if (unlikely(!hq) || unlikely(!dq))
522 return 0;
523
524 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
525 return 0;
526 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
527 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
528 return 1;
529 }
530
531 /**
532 * lpfc_cmd_iocb - Get next command iocb entry in the ring
533 * @phba: Pointer to HBA context object.
534 * @pring: Pointer to driver SLI ring object.
535 *
536 * This function returns pointer to next command iocb entry
537 * in the command ring. The caller must hold hbalock to prevent
538 * other threads consume the next command iocb.
539 * SLI-2/SLI-3 provide different sized iocbs.
540 **/
541 static inline IOCB_t *
542 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
543 {
544 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
545 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
546 }
547
548 /**
549 * lpfc_resp_iocb - Get next response iocb entry in the ring
550 * @phba: Pointer to HBA context object.
551 * @pring: Pointer to driver SLI ring object.
552 *
553 * This function returns pointer to next response iocb entry
554 * in the response ring. The caller must hold hbalock to make sure
555 * that no other thread consume the next response iocb.
556 * SLI-2/SLI-3 provide different sized iocbs.
557 **/
558 static inline IOCB_t *
559 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
560 {
561 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
562 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
563 }
564
565 /**
566 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
567 * @phba: Pointer to HBA context object.
568 *
569 * This function is called with hbalock held. This function
570 * allocates a new driver iocb object from the iocb pool. If the
571 * allocation is successful, it returns pointer to the newly
572 * allocated iocb object else it returns NULL.
573 **/
574 struct lpfc_iocbq *
575 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
576 {
577 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
578 struct lpfc_iocbq * iocbq = NULL;
579
580 lockdep_assert_held(&phba->hbalock);
581
582 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
583 if (iocbq)
584 phba->iocb_cnt++;
585 if (phba->iocb_cnt > phba->iocb_max)
586 phba->iocb_max = phba->iocb_cnt;
587 return iocbq;
588 }
589
590 /**
591 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
592 * @phba: Pointer to HBA context object.
593 * @xritag: XRI value.
594 *
595 * This function clears the sglq pointer from the array of acive
596 * sglq's. The xritag that is passed in is used to index into the
597 * array. Before the xritag can be used it needs to be adjusted
598 * by subtracting the xribase.
599 *
600 * Returns sglq ponter = success, NULL = Failure.
601 **/
602 static struct lpfc_sglq *
603 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
604 {
605 struct lpfc_sglq *sglq;
606
607 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
608 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
609 return sglq;
610 }
611
612 /**
613 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
614 * @phba: Pointer to HBA context object.
615 * @xritag: XRI value.
616 *
617 * This function returns the sglq pointer from the array of acive
618 * sglq's. The xritag that is passed in is used to index into the
619 * array. Before the xritag can be used it needs to be adjusted
620 * by subtracting the xribase.
621 *
622 * Returns sglq ponter = success, NULL = Failure.
623 **/
624 struct lpfc_sglq *
625 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
626 {
627 struct lpfc_sglq *sglq;
628
629 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
630 return sglq;
631 }
632
633 /**
634 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
635 * @phba: Pointer to HBA context object.
636 * @xritag: xri used in this exchange.
637 * @rrq: The RRQ to be cleared.
638 *
639 **/
640 void
641 lpfc_clr_rrq_active(struct lpfc_hba *phba,
642 uint16_t xritag,
643 struct lpfc_node_rrq *rrq)
644 {
645 struct lpfc_nodelist *ndlp = NULL;
646
647 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
648 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
649
650 /* The target DID could have been swapped (cable swap)
651 * we should use the ndlp from the findnode if it is
652 * available.
653 */
654 if ((!ndlp) && rrq->ndlp)
655 ndlp = rrq->ndlp;
656
657 if (!ndlp)
658 goto out;
659
660 if (test_and_clear_bit(xritag, ndlp->active_rrqs_xri_bitmap)) {
661 rrq->send_rrq = 0;
662 rrq->xritag = 0;
663 rrq->rrq_stop_time = 0;
664 }
665 out:
666 mempool_free(rrq, phba->rrq_pool);
667 }
668
669 /**
670 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
671 * @phba: Pointer to HBA context object.
672 *
673 * This function is called with hbalock held. This function
674 * Checks if stop_time (ratov from setting rrq active) has
675 * been reached, if it has and the send_rrq flag is set then
676 * it will call lpfc_send_rrq. If the send_rrq flag is not set
677 * then it will just call the routine to clear the rrq and
678 * free the rrq resource.
679 * The timer is set to the next rrq that is going to expire before
680 * leaving the routine.
681 *
682 **/
683 void
684 lpfc_handle_rrq_active(struct lpfc_hba *phba)
685 {
686 struct lpfc_node_rrq *rrq;
687 struct lpfc_node_rrq *nextrrq;
688 unsigned long next_time;
689 unsigned long iflags;
690 LIST_HEAD(send_rrq);
691
692 spin_lock_irqsave(&phba->hbalock, iflags);
693 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
694 next_time = jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
695 list_for_each_entry_safe(rrq, nextrrq,
696 &phba->active_rrq_list, list) {
697 if (time_after(jiffies, rrq->rrq_stop_time))
698 list_move(&rrq->list, &send_rrq);
699 else if (time_before(rrq->rrq_stop_time, next_time))
700 next_time = rrq->rrq_stop_time;
701 }
702 spin_unlock_irqrestore(&phba->hbalock, iflags);
703 if ((!list_empty(&phba->active_rrq_list)) &&
704 (!(phba->pport->load_flag & FC_UNLOADING)))
705 mod_timer(&phba->rrq_tmr, next_time);
706 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
707 list_del(&rrq->list);
708 if (!rrq->send_rrq)
709 /* this call will free the rrq */
710 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
711 else if (lpfc_send_rrq(phba, rrq)) {
712 /* if we send the rrq then the completion handler
713 * will clear the bit in the xribitmap.
714 */
715 lpfc_clr_rrq_active(phba, rrq->xritag,
716 rrq);
717 }
718 }
719 }
720
721 /**
722 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
723 * @vport: Pointer to vport context object.
724 * @xri: The xri used in the exchange.
725 * @did: The targets DID for this exchange.
726 *
727 * returns NULL = rrq not found in the phba->active_rrq_list.
728 * rrq = rrq for this xri and target.
729 **/
730 struct lpfc_node_rrq *
731 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
732 {
733 struct lpfc_hba *phba = vport->phba;
734 struct lpfc_node_rrq *rrq;
735 struct lpfc_node_rrq *nextrrq;
736 unsigned long iflags;
737
738 if (phba->sli_rev != LPFC_SLI_REV4)
739 return NULL;
740 spin_lock_irqsave(&phba->hbalock, iflags);
741 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
742 if (rrq->vport == vport && rrq->xritag == xri &&
743 rrq->nlp_DID == did){
744 list_del(&rrq->list);
745 spin_unlock_irqrestore(&phba->hbalock, iflags);
746 return rrq;
747 }
748 }
749 spin_unlock_irqrestore(&phba->hbalock, iflags);
750 return NULL;
751 }
752
753 /**
754 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
755 * @vport: Pointer to vport context object.
756 * @ndlp: Pointer to the lpfc_node_list structure.
757 * If ndlp is NULL Remove all active RRQs for this vport from the
758 * phba->active_rrq_list and clear the rrq.
759 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
760 **/
761 void
762 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
763
764 {
765 struct lpfc_hba *phba = vport->phba;
766 struct lpfc_node_rrq *rrq;
767 struct lpfc_node_rrq *nextrrq;
768 unsigned long iflags;
769 LIST_HEAD(rrq_list);
770
771 if (phba->sli_rev != LPFC_SLI_REV4)
772 return;
773 if (!ndlp) {
774 lpfc_sli4_vport_delete_els_xri_aborted(vport);
775 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
776 }
777 spin_lock_irqsave(&phba->hbalock, iflags);
778 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
779 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
780 list_move(&rrq->list, &rrq_list);
781 spin_unlock_irqrestore(&phba->hbalock, iflags);
782
783 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
784 list_del(&rrq->list);
785 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
786 }
787 }
788
789 /**
790 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
791 * @phba: Pointer to HBA context object.
792 * @ndlp: Targets nodelist pointer for this exchange.
793 * @xritag the xri in the bitmap to test.
794 *
795 * This function is called with hbalock held. This function
796 * returns 0 = rrq not active for this xri
797 * 1 = rrq is valid for this xri.
798 **/
799 int
800 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
801 uint16_t xritag)
802 {
803 lockdep_assert_held(&phba->hbalock);
804 if (!ndlp)
805 return 0;
806 if (!ndlp->active_rrqs_xri_bitmap)
807 return 0;
808 if (test_bit(xritag, ndlp->active_rrqs_xri_bitmap))
809 return 1;
810 else
811 return 0;
812 }
813
814 /**
815 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
816 * @phba: Pointer to HBA context object.
817 * @ndlp: nodelist pointer for this target.
818 * @xritag: xri used in this exchange.
819 * @rxid: Remote Exchange ID.
820 * @send_rrq: Flag used to determine if we should send rrq els cmd.
821 *
822 * This function takes the hbalock.
823 * The active bit is always set in the active rrq xri_bitmap even
824 * if there is no slot avaiable for the other rrq information.
825 *
826 * returns 0 rrq actived for this xri
827 * < 0 No memory or invalid ndlp.
828 **/
829 int
830 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
831 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
832 {
833 unsigned long iflags;
834 struct lpfc_node_rrq *rrq;
835 int empty;
836
837 if (!ndlp)
838 return -EINVAL;
839
840 if (!phba->cfg_enable_rrq)
841 return -EINVAL;
842
843 spin_lock_irqsave(&phba->hbalock, iflags);
844 if (phba->pport->load_flag & FC_UNLOADING) {
845 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
846 goto out;
847 }
848
849 /*
850 * set the active bit even if there is no mem available.
851 */
852 if (NLP_CHK_FREE_REQ(ndlp))
853 goto out;
854
855 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
856 goto out;
857
858 if (!ndlp->active_rrqs_xri_bitmap)
859 goto out;
860
861 if (test_and_set_bit(xritag, ndlp->active_rrqs_xri_bitmap))
862 goto out;
863
864 spin_unlock_irqrestore(&phba->hbalock, iflags);
865 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
866 if (!rrq) {
867 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
868 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
869 " DID:0x%x Send:%d\n",
870 xritag, rxid, ndlp->nlp_DID, send_rrq);
871 return -EINVAL;
872 }
873 if (phba->cfg_enable_rrq == 1)
874 rrq->send_rrq = send_rrq;
875 else
876 rrq->send_rrq = 0;
877 rrq->xritag = xritag;
878 rrq->rrq_stop_time = jiffies +
879 msecs_to_jiffies(1000 * (phba->fc_ratov + 1));
880 rrq->ndlp = ndlp;
881 rrq->nlp_DID = ndlp->nlp_DID;
882 rrq->vport = ndlp->vport;
883 rrq->rxid = rxid;
884 spin_lock_irqsave(&phba->hbalock, iflags);
885 empty = list_empty(&phba->active_rrq_list);
886 list_add_tail(&rrq->list, &phba->active_rrq_list);
887 phba->hba_flag |= HBA_RRQ_ACTIVE;
888 if (empty)
889 lpfc_worker_wake_up(phba);
890 spin_unlock_irqrestore(&phba->hbalock, iflags);
891 return 0;
892 out:
893 spin_unlock_irqrestore(&phba->hbalock, iflags);
894 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
895 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
896 " DID:0x%x Send:%d\n",
897 xritag, rxid, ndlp->nlp_DID, send_rrq);
898 return -EINVAL;
899 }
900
901 /**
902 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
903 * @phba: Pointer to HBA context object.
904 * @piocb: Pointer to the iocbq.
905 *
906 * This function is called with the ring lock held. This function
907 * gets a new driver sglq object from the sglq list. If the
908 * list is not empty then it is successful, it returns pointer to the newly
909 * allocated sglq object else it returns NULL.
910 **/
911 static struct lpfc_sglq *
912 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
913 {
914 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
915 struct lpfc_sglq *sglq = NULL;
916 struct lpfc_sglq *start_sglq = NULL;
917 struct lpfc_scsi_buf *lpfc_cmd;
918 struct lpfc_nodelist *ndlp;
919 int found = 0;
920
921 lockdep_assert_held(&phba->hbalock);
922
923 if (piocbq->iocb_flag & LPFC_IO_FCP) {
924 lpfc_cmd = (struct lpfc_scsi_buf *) piocbq->context1;
925 ndlp = lpfc_cmd->rdata->pnode;
926 } else if ((piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) &&
927 !(piocbq->iocb_flag & LPFC_IO_LIBDFC)) {
928 ndlp = piocbq->context_un.ndlp;
929 } else if (piocbq->iocb_flag & LPFC_IO_LIBDFC) {
930 if (piocbq->iocb_flag & LPFC_IO_LOOPBACK)
931 ndlp = NULL;
932 else
933 ndlp = piocbq->context_un.ndlp;
934 } else {
935 ndlp = piocbq->context1;
936 }
937
938 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
939 start_sglq = sglq;
940 while (!found) {
941 if (!sglq)
942 return NULL;
943 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
944 /* This xri has an rrq outstanding for this DID.
945 * put it back in the list and get another xri.
946 */
947 list_add_tail(&sglq->list, lpfc_sgl_list);
948 sglq = NULL;
949 list_remove_head(lpfc_sgl_list, sglq,
950 struct lpfc_sglq, list);
951 if (sglq == start_sglq) {
952 sglq = NULL;
953 break;
954 } else
955 continue;
956 }
957 sglq->ndlp = ndlp;
958 found = 1;
959 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
960 sglq->state = SGL_ALLOCATED;
961 }
962 return sglq;
963 }
964
965 /**
966 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
967 * @phba: Pointer to HBA context object.
968 *
969 * This function is called with no lock held. This function
970 * allocates a new driver iocb object from the iocb pool. If the
971 * allocation is successful, it returns pointer to the newly
972 * allocated iocb object else it returns NULL.
973 **/
974 struct lpfc_iocbq *
975 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
976 {
977 struct lpfc_iocbq * iocbq = NULL;
978 unsigned long iflags;
979
980 spin_lock_irqsave(&phba->hbalock, iflags);
981 iocbq = __lpfc_sli_get_iocbq(phba);
982 spin_unlock_irqrestore(&phba->hbalock, iflags);
983 return iocbq;
984 }
985
986 /**
987 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
988 * @phba: Pointer to HBA context object.
989 * @iocbq: Pointer to driver iocb object.
990 *
991 * This function is called with hbalock held to release driver
992 * iocb object to the iocb pool. The iotag in the iocb object
993 * does not change for each use of the iocb object. This function
994 * clears all other fields of the iocb object when it is freed.
995 * The sqlq structure that holds the xritag and phys and virtual
996 * mappings for the scatter gather list is retrieved from the
997 * active array of sglq. The get of the sglq pointer also clears
998 * the entry in the array. If the status of the IO indiactes that
999 * this IO was aborted then the sglq entry it put on the
1000 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
1001 * IO has good status or fails for any other reason then the sglq
1002 * entry is added to the free list (lpfc_sgl_list).
1003 **/
1004 static void
1005 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1006 {
1007 struct lpfc_sglq *sglq;
1008 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1009 unsigned long iflag = 0;
1010 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
1011
1012 lockdep_assert_held(&phba->hbalock);
1013
1014 if (iocbq->sli4_xritag == NO_XRI)
1015 sglq = NULL;
1016 else
1017 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1018
1019
1020 if (sglq) {
1021 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1022 (sglq->state != SGL_XRI_ABORTED)) {
1023 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1024 iflag);
1025 list_add(&sglq->list,
1026 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1027 spin_unlock_irqrestore(
1028 &phba->sli4_hba.abts_sgl_list_lock, iflag);
1029 } else {
1030 spin_lock_irqsave(&pring->ring_lock, iflag);
1031 sglq->state = SGL_FREED;
1032 sglq->ndlp = NULL;
1033 list_add_tail(&sglq->list,
1034 &phba->sli4_hba.lpfc_sgl_list);
1035 spin_unlock_irqrestore(&pring->ring_lock, iflag);
1036
1037 /* Check if TXQ queue needs to be serviced */
1038 if (!list_empty(&pring->txq))
1039 lpfc_worker_wake_up(phba);
1040 }
1041 }
1042
1043
1044 /*
1045 * Clean all volatile data fields, preserve iotag and node struct.
1046 */
1047 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1048 iocbq->sli4_lxritag = NO_XRI;
1049 iocbq->sli4_xritag = NO_XRI;
1050 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1051 }
1052
1053
1054 /**
1055 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1056 * @phba: Pointer to HBA context object.
1057 * @iocbq: Pointer to driver iocb object.
1058 *
1059 * This function is called with hbalock held to release driver
1060 * iocb object to the iocb pool. The iotag in the iocb object
1061 * does not change for each use of the iocb object. This function
1062 * clears all other fields of the iocb object when it is freed.
1063 **/
1064 static void
1065 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1066 {
1067 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1068
1069 lockdep_assert_held(&phba->hbalock);
1070
1071 /*
1072 * Clean all volatile data fields, preserve iotag and node struct.
1073 */
1074 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1075 iocbq->sli4_xritag = NO_XRI;
1076 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1077 }
1078
1079 /**
1080 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1081 * @phba: Pointer to HBA context object.
1082 * @iocbq: Pointer to driver iocb object.
1083 *
1084 * This function is called with hbalock held to release driver
1085 * iocb object to the iocb pool. The iotag in the iocb object
1086 * does not change for each use of the iocb object. This function
1087 * clears all other fields of the iocb object when it is freed.
1088 **/
1089 static void
1090 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1091 {
1092 lockdep_assert_held(&phba->hbalock);
1093
1094 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1095 phba->iocb_cnt--;
1096 }
1097
1098 /**
1099 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1100 * @phba: Pointer to HBA context object.
1101 * @iocbq: Pointer to driver iocb object.
1102 *
1103 * This function is called with no lock held to release the iocb to
1104 * iocb pool.
1105 **/
1106 void
1107 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1108 {
1109 unsigned long iflags;
1110
1111 /*
1112 * Clean all volatile data fields, preserve iotag and node struct.
1113 */
1114 spin_lock_irqsave(&phba->hbalock, iflags);
1115 __lpfc_sli_release_iocbq(phba, iocbq);
1116 spin_unlock_irqrestore(&phba->hbalock, iflags);
1117 }
1118
1119 /**
1120 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1121 * @phba: Pointer to HBA context object.
1122 * @iocblist: List of IOCBs.
1123 * @ulpstatus: ULP status in IOCB command field.
1124 * @ulpWord4: ULP word-4 in IOCB command field.
1125 *
1126 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1127 * on the list by invoking the complete callback function associated with the
1128 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1129 * fields.
1130 **/
1131 void
1132 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1133 uint32_t ulpstatus, uint32_t ulpWord4)
1134 {
1135 struct lpfc_iocbq *piocb;
1136
1137 while (!list_empty(iocblist)) {
1138 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1139 if (!piocb->iocb_cmpl)
1140 lpfc_sli_release_iocbq(phba, piocb);
1141 else {
1142 piocb->iocb.ulpStatus = ulpstatus;
1143 piocb->iocb.un.ulpWord[4] = ulpWord4;
1144 (piocb->iocb_cmpl) (phba, piocb, piocb);
1145 }
1146 }
1147 return;
1148 }
1149
1150 /**
1151 * lpfc_sli_iocb_cmd_type - Get the iocb type
1152 * @iocb_cmnd: iocb command code.
1153 *
1154 * This function is called by ring event handler function to get the iocb type.
1155 * This function translates the iocb command to an iocb command type used to
1156 * decide the final disposition of each completed IOCB.
1157 * The function returns
1158 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1159 * LPFC_SOL_IOCB if it is a solicited iocb completion
1160 * LPFC_ABORT_IOCB if it is an abort iocb
1161 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1162 *
1163 * The caller is not required to hold any lock.
1164 **/
1165 static lpfc_iocb_type
1166 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1167 {
1168 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1169
1170 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1171 return 0;
1172
1173 switch (iocb_cmnd) {
1174 case CMD_XMIT_SEQUENCE_CR:
1175 case CMD_XMIT_SEQUENCE_CX:
1176 case CMD_XMIT_BCAST_CN:
1177 case CMD_XMIT_BCAST_CX:
1178 case CMD_ELS_REQUEST_CR:
1179 case CMD_ELS_REQUEST_CX:
1180 case CMD_CREATE_XRI_CR:
1181 case CMD_CREATE_XRI_CX:
1182 case CMD_GET_RPI_CN:
1183 case CMD_XMIT_ELS_RSP_CX:
1184 case CMD_GET_RPI_CR:
1185 case CMD_FCP_IWRITE_CR:
1186 case CMD_FCP_IWRITE_CX:
1187 case CMD_FCP_IREAD_CR:
1188 case CMD_FCP_IREAD_CX:
1189 case CMD_FCP_ICMND_CR:
1190 case CMD_FCP_ICMND_CX:
1191 case CMD_FCP_TSEND_CX:
1192 case CMD_FCP_TRSP_CX:
1193 case CMD_FCP_TRECEIVE_CX:
1194 case CMD_FCP_AUTO_TRSP_CX:
1195 case CMD_ADAPTER_MSG:
1196 case CMD_ADAPTER_DUMP:
1197 case CMD_XMIT_SEQUENCE64_CR:
1198 case CMD_XMIT_SEQUENCE64_CX:
1199 case CMD_XMIT_BCAST64_CN:
1200 case CMD_XMIT_BCAST64_CX:
1201 case CMD_ELS_REQUEST64_CR:
1202 case CMD_ELS_REQUEST64_CX:
1203 case CMD_FCP_IWRITE64_CR:
1204 case CMD_FCP_IWRITE64_CX:
1205 case CMD_FCP_IREAD64_CR:
1206 case CMD_FCP_IREAD64_CX:
1207 case CMD_FCP_ICMND64_CR:
1208 case CMD_FCP_ICMND64_CX:
1209 case CMD_FCP_TSEND64_CX:
1210 case CMD_FCP_TRSP64_CX:
1211 case CMD_FCP_TRECEIVE64_CX:
1212 case CMD_GEN_REQUEST64_CR:
1213 case CMD_GEN_REQUEST64_CX:
1214 case CMD_XMIT_ELS_RSP64_CX:
1215 case DSSCMD_IWRITE64_CR:
1216 case DSSCMD_IWRITE64_CX:
1217 case DSSCMD_IREAD64_CR:
1218 case DSSCMD_IREAD64_CX:
1219 type = LPFC_SOL_IOCB;
1220 break;
1221 case CMD_ABORT_XRI_CN:
1222 case CMD_ABORT_XRI_CX:
1223 case CMD_CLOSE_XRI_CN:
1224 case CMD_CLOSE_XRI_CX:
1225 case CMD_XRI_ABORTED_CX:
1226 case CMD_ABORT_MXRI64_CN:
1227 case CMD_XMIT_BLS_RSP64_CX:
1228 type = LPFC_ABORT_IOCB;
1229 break;
1230 case CMD_RCV_SEQUENCE_CX:
1231 case CMD_RCV_ELS_REQ_CX:
1232 case CMD_RCV_SEQUENCE64_CX:
1233 case CMD_RCV_ELS_REQ64_CX:
1234 case CMD_ASYNC_STATUS:
1235 case CMD_IOCB_RCV_SEQ64_CX:
1236 case CMD_IOCB_RCV_ELS64_CX:
1237 case CMD_IOCB_RCV_CONT64_CX:
1238 case CMD_IOCB_RET_XRI64_CX:
1239 type = LPFC_UNSOL_IOCB;
1240 break;
1241 case CMD_IOCB_XMIT_MSEQ64_CR:
1242 case CMD_IOCB_XMIT_MSEQ64_CX:
1243 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1244 case CMD_IOCB_RCV_ELS_LIST64_CX:
1245 case CMD_IOCB_CLOSE_EXTENDED_CN:
1246 case CMD_IOCB_ABORT_EXTENDED_CN:
1247 case CMD_IOCB_RET_HBQE64_CN:
1248 case CMD_IOCB_FCP_IBIDIR64_CR:
1249 case CMD_IOCB_FCP_IBIDIR64_CX:
1250 case CMD_IOCB_FCP_ITASKMGT64_CX:
1251 case CMD_IOCB_LOGENTRY_CN:
1252 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1253 printk("%s - Unhandled SLI-3 Command x%x\n",
1254 __func__, iocb_cmnd);
1255 type = LPFC_UNKNOWN_IOCB;
1256 break;
1257 default:
1258 type = LPFC_UNKNOWN_IOCB;
1259 break;
1260 }
1261
1262 return type;
1263 }
1264
1265 /**
1266 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1267 * @phba: Pointer to HBA context object.
1268 *
1269 * This function is called from SLI initialization code
1270 * to configure every ring of the HBA's SLI interface. The
1271 * caller is not required to hold any lock. This function issues
1272 * a config_ring mailbox command for each ring.
1273 * This function returns zero if successful else returns a negative
1274 * error code.
1275 **/
1276 static int
1277 lpfc_sli_ring_map(struct lpfc_hba *phba)
1278 {
1279 struct lpfc_sli *psli = &phba->sli;
1280 LPFC_MBOXQ_t *pmb;
1281 MAILBOX_t *pmbox;
1282 int i, rc, ret = 0;
1283
1284 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1285 if (!pmb)
1286 return -ENOMEM;
1287 pmbox = &pmb->u.mb;
1288 phba->link_state = LPFC_INIT_MBX_CMDS;
1289 for (i = 0; i < psli->num_rings; i++) {
1290 lpfc_config_ring(phba, i, pmb);
1291 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1292 if (rc != MBX_SUCCESS) {
1293 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1294 "0446 Adapter failed to init (%d), "
1295 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1296 "ring %d\n",
1297 rc, pmbox->mbxCommand,
1298 pmbox->mbxStatus, i);
1299 phba->link_state = LPFC_HBA_ERROR;
1300 ret = -ENXIO;
1301 break;
1302 }
1303 }
1304 mempool_free(pmb, phba->mbox_mem_pool);
1305 return ret;
1306 }
1307
1308 /**
1309 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1310 * @phba: Pointer to HBA context object.
1311 * @pring: Pointer to driver SLI ring object.
1312 * @piocb: Pointer to the driver iocb object.
1313 *
1314 * This function is called with hbalock held. The function adds the
1315 * new iocb to txcmplq of the given ring. This function always returns
1316 * 0. If this function is called for ELS ring, this function checks if
1317 * there is a vport associated with the ELS command. This function also
1318 * starts els_tmofunc timer if this is an ELS command.
1319 **/
1320 static int
1321 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1322 struct lpfc_iocbq *piocb)
1323 {
1324 lockdep_assert_held(&phba->hbalock);
1325
1326 BUG_ON(!piocb);
1327
1328 list_add_tail(&piocb->list, &pring->txcmplq);
1329 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1330
1331 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1332 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1333 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1334 BUG_ON(!piocb->vport);
1335 if (!(piocb->vport->load_flag & FC_UNLOADING))
1336 mod_timer(&piocb->vport->els_tmofunc,
1337 jiffies +
1338 msecs_to_jiffies(1000 * (phba->fc_ratov << 1)));
1339 }
1340
1341 return 0;
1342 }
1343
1344 /**
1345 * lpfc_sli_ringtx_get - Get first element of the txq
1346 * @phba: Pointer to HBA context object.
1347 * @pring: Pointer to driver SLI ring object.
1348 *
1349 * This function is called with hbalock held to get next
1350 * iocb in txq of the given ring. If there is any iocb in
1351 * the txq, the function returns first iocb in the list after
1352 * removing the iocb from the list, else it returns NULL.
1353 **/
1354 struct lpfc_iocbq *
1355 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1356 {
1357 struct lpfc_iocbq *cmd_iocb;
1358
1359 lockdep_assert_held(&phba->hbalock);
1360
1361 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1362 return cmd_iocb;
1363 }
1364
1365 /**
1366 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1367 * @phba: Pointer to HBA context object.
1368 * @pring: Pointer to driver SLI ring object.
1369 *
1370 * This function is called with hbalock held and the caller must post the
1371 * iocb without releasing the lock. If the caller releases the lock,
1372 * iocb slot returned by the function is not guaranteed to be available.
1373 * The function returns pointer to the next available iocb slot if there
1374 * is available slot in the ring, else it returns NULL.
1375 * If the get index of the ring is ahead of the put index, the function
1376 * will post an error attention event to the worker thread to take the
1377 * HBA to offline state.
1378 **/
1379 static IOCB_t *
1380 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1381 {
1382 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1383 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
1384
1385 lockdep_assert_held(&phba->hbalock);
1386
1387 if ((pring->sli.sli3.next_cmdidx == pring->sli.sli3.cmdidx) &&
1388 (++pring->sli.sli3.next_cmdidx >= max_cmd_idx))
1389 pring->sli.sli3.next_cmdidx = 0;
1390
1391 if (unlikely(pring->sli.sli3.local_getidx ==
1392 pring->sli.sli3.next_cmdidx)) {
1393
1394 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
1395
1396 if (unlikely(pring->sli.sli3.local_getidx >= max_cmd_idx)) {
1397 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
1398 "0315 Ring %d issue: portCmdGet %d "
1399 "is bigger than cmd ring %d\n",
1400 pring->ringno,
1401 pring->sli.sli3.local_getidx,
1402 max_cmd_idx);
1403
1404 phba->link_state = LPFC_HBA_ERROR;
1405 /*
1406 * All error attention handlers are posted to
1407 * worker thread
1408 */
1409 phba->work_ha |= HA_ERATT;
1410 phba->work_hs = HS_FFER3;
1411
1412 lpfc_worker_wake_up(phba);
1413
1414 return NULL;
1415 }
1416
1417 if (pring->sli.sli3.local_getidx == pring->sli.sli3.next_cmdidx)
1418 return NULL;
1419 }
1420
1421 return lpfc_cmd_iocb(phba, pring);
1422 }
1423
1424 /**
1425 * lpfc_sli_next_iotag - Get an iotag for the iocb
1426 * @phba: Pointer to HBA context object.
1427 * @iocbq: Pointer to driver iocb object.
1428 *
1429 * This function gets an iotag for the iocb. If there is no unused iotag and
1430 * the iocbq_lookup_len < 0xffff, this function allocates a bigger iotag_lookup
1431 * array and assigns a new iotag.
1432 * The function returns the allocated iotag if successful, else returns zero.
1433 * Zero is not a valid iotag.
1434 * The caller is not required to hold any lock.
1435 **/
1436 uint16_t
1437 lpfc_sli_next_iotag(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1438 {
1439 struct lpfc_iocbq **new_arr;
1440 struct lpfc_iocbq **old_arr;
1441 size_t new_len;
1442 struct lpfc_sli *psli = &phba->sli;
1443 uint16_t iotag;
1444
1445 spin_lock_irq(&phba->hbalock);
1446 iotag = psli->last_iotag;
1447 if(++iotag < psli->iocbq_lookup_len) {
1448 psli->last_iotag = iotag;
1449 psli->iocbq_lookup[iotag] = iocbq;
1450 spin_unlock_irq(&phba->hbalock);
1451 iocbq->iotag = iotag;
1452 return iotag;
1453 } else if (psli->iocbq_lookup_len < (0xffff
1454 - LPFC_IOCBQ_LOOKUP_INCREMENT)) {
1455 new_len = psli->iocbq_lookup_len + LPFC_IOCBQ_LOOKUP_INCREMENT;
1456 spin_unlock_irq(&phba->hbalock);
1457 new_arr = kzalloc(new_len * sizeof (struct lpfc_iocbq *),
1458 GFP_KERNEL);
1459 if (new_arr) {
1460 spin_lock_irq(&phba->hbalock);
1461 old_arr = psli->iocbq_lookup;
1462 if (new_len <= psli->iocbq_lookup_len) {
1463 /* highly unprobable case */
1464 kfree(new_arr);
1465 iotag = psli->last_iotag;
1466 if(++iotag < psli->iocbq_lookup_len) {
1467 psli->last_iotag = iotag;
1468 psli->iocbq_lookup[iotag] = iocbq;
1469 spin_unlock_irq(&phba->hbalock);
1470 iocbq->iotag = iotag;
1471 return iotag;
1472 }
1473 spin_unlock_irq(&phba->hbalock);
1474 return 0;
1475 }
1476 if (psli->iocbq_lookup)
1477 memcpy(new_arr, old_arr,
1478 ((psli->last_iotag + 1) *
1479 sizeof (struct lpfc_iocbq *)));
1480 psli->iocbq_lookup = new_arr;
1481 psli->iocbq_lookup_len = new_len;
1482 psli->last_iotag = iotag;
1483 psli->iocbq_lookup[iotag] = iocbq;
1484 spin_unlock_irq(&phba->hbalock);
1485 iocbq->iotag = iotag;
1486 kfree(old_arr);
1487 return iotag;
1488 }
1489 } else
1490 spin_unlock_irq(&phba->hbalock);
1491
1492 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
1493 "0318 Failed to allocate IOTAG.last IOTAG is %d\n",
1494 psli->last_iotag);
1495
1496 return 0;
1497 }
1498
1499 /**
1500 * lpfc_sli_submit_iocb - Submit an iocb to the firmware
1501 * @phba: Pointer to HBA context object.
1502 * @pring: Pointer to driver SLI ring object.
1503 * @iocb: Pointer to iocb slot in the ring.
1504 * @nextiocb: Pointer to driver iocb object which need to be
1505 * posted to firmware.
1506 *
1507 * This function is called with hbalock held to post a new iocb to
1508 * the firmware. This function copies the new iocb to ring iocb slot and
1509 * updates the ring pointers. It adds the new iocb to txcmplq if there is
1510 * a completion call back for this iocb else the function will free the
1511 * iocb object.
1512 **/
1513 static void
1514 lpfc_sli_submit_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1515 IOCB_t *iocb, struct lpfc_iocbq *nextiocb)
1516 {
1517 lockdep_assert_held(&phba->hbalock);
1518 /*
1519 * Set up an iotag
1520 */
1521 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1522
1523
1524 if (pring->ringno == LPFC_ELS_RING) {
1525 lpfc_debugfs_slow_ring_trc(phba,
1526 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1527 *(((uint32_t *) &nextiocb->iocb) + 4),
1528 *(((uint32_t *) &nextiocb->iocb) + 6),
1529 *(((uint32_t *) &nextiocb->iocb) + 7));
1530 }
1531
1532 /*
1533 * Issue iocb command to adapter
1534 */
1535 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1536 wmb();
1537 pring->stats.iocb_cmd++;
1538
1539 /*
1540 * If there is no completion routine to call, we can release the
1541 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1542 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1543 */
1544 if (nextiocb->iocb_cmpl)
1545 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1546 else
1547 __lpfc_sli_release_iocbq(phba, nextiocb);
1548
1549 /*
1550 * Let the HBA know what IOCB slot will be the next one the
1551 * driver will put a command into.
1552 */
1553 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1554 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1555 }
1556
1557 /**
1558 * lpfc_sli_update_full_ring - Update the chip attention register
1559 * @phba: Pointer to HBA context object.
1560 * @pring: Pointer to driver SLI ring object.
1561 *
1562 * The caller is not required to hold any lock for calling this function.
1563 * This function updates the chip attention bits for the ring to inform firmware
1564 * that there are pending work to be done for this ring and requests an
1565 * interrupt when there is space available in the ring. This function is
1566 * called when the driver is unable to post more iocbs to the ring due
1567 * to unavailability of space in the ring.
1568 **/
1569 static void
1570 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1571 {
1572 int ringno = pring->ringno;
1573
1574 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1575
1576 wmb();
1577
1578 /*
1579 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1580 * The HBA will tell us when an IOCB entry is available.
1581 */
1582 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1583 readl(phba->CAregaddr); /* flush */
1584
1585 pring->stats.iocb_cmd_full++;
1586 }
1587
1588 /**
1589 * lpfc_sli_update_ring - Update chip attention register
1590 * @phba: Pointer to HBA context object.
1591 * @pring: Pointer to driver SLI ring object.
1592 *
1593 * This function updates the chip attention register bit for the
1594 * given ring to inform HBA that there is more work to be done
1595 * in this ring. The caller is not required to hold any lock.
1596 **/
1597 static void
1598 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1599 {
1600 int ringno = pring->ringno;
1601
1602 /*
1603 * Tell the HBA that there is work to do in this ring.
1604 */
1605 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1606 wmb();
1607 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1608 readl(phba->CAregaddr); /* flush */
1609 }
1610 }
1611
1612 /**
1613 * lpfc_sli_resume_iocb - Process iocbs in the txq
1614 * @phba: Pointer to HBA context object.
1615 * @pring: Pointer to driver SLI ring object.
1616 *
1617 * This function is called with hbalock held to post pending iocbs
1618 * in the txq to the firmware. This function is called when driver
1619 * detects space available in the ring.
1620 **/
1621 static void
1622 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1623 {
1624 IOCB_t *iocb;
1625 struct lpfc_iocbq *nextiocb;
1626
1627 lockdep_assert_held(&phba->hbalock);
1628
1629 /*
1630 * Check to see if:
1631 * (a) there is anything on the txq to send
1632 * (b) link is up
1633 * (c) link attention events can be processed (fcp ring only)
1634 * (d) IOCB processing is not blocked by the outstanding mbox command.
1635 */
1636
1637 if (lpfc_is_link_up(phba) &&
1638 (!list_empty(&pring->txq)) &&
1639 (pring->ringno != phba->sli.fcp_ring ||
1640 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1641
1642 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1643 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1644 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1645
1646 if (iocb)
1647 lpfc_sli_update_ring(phba, pring);
1648 else
1649 lpfc_sli_update_full_ring(phba, pring);
1650 }
1651
1652 return;
1653 }
1654
1655 /**
1656 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1657 * @phba: Pointer to HBA context object.
1658 * @hbqno: HBQ number.
1659 *
1660 * This function is called with hbalock held to get the next
1661 * available slot for the given HBQ. If there is free slot
1662 * available for the HBQ it will return pointer to the next available
1663 * HBQ entry else it will return NULL.
1664 **/
1665 static struct lpfc_hbq_entry *
1666 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1667 {
1668 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1669
1670 lockdep_assert_held(&phba->hbalock);
1671
1672 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1673 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1674 hbqp->next_hbqPutIdx = 0;
1675
1676 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1677 uint32_t raw_index = phba->hbq_get[hbqno];
1678 uint32_t getidx = le32_to_cpu(raw_index);
1679
1680 hbqp->local_hbqGetIdx = getidx;
1681
1682 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1683 lpfc_printf_log(phba, KERN_ERR,
1684 LOG_SLI | LOG_VPORT,
1685 "1802 HBQ %d: local_hbqGetIdx "
1686 "%u is > than hbqp->entry_count %u\n",
1687 hbqno, hbqp->local_hbqGetIdx,
1688 hbqp->entry_count);
1689
1690 phba->link_state = LPFC_HBA_ERROR;
1691 return NULL;
1692 }
1693
1694 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1695 return NULL;
1696 }
1697
1698 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1699 hbqp->hbqPutIdx;
1700 }
1701
1702 /**
1703 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1704 * @phba: Pointer to HBA context object.
1705 *
1706 * This function is called with no lock held to free all the
1707 * hbq buffers while uninitializing the SLI interface. It also
1708 * frees the HBQ buffers returned by the firmware but not yet
1709 * processed by the upper layers.
1710 **/
1711 void
1712 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1713 {
1714 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1715 struct hbq_dmabuf *hbq_buf;
1716 unsigned long flags;
1717 int i, hbq_count;
1718 uint32_t hbqno;
1719
1720 hbq_count = lpfc_sli_hbq_count();
1721 /* Return all memory used by all HBQs */
1722 spin_lock_irqsave(&phba->hbalock, flags);
1723 for (i = 0; i < hbq_count; ++i) {
1724 list_for_each_entry_safe(dmabuf, next_dmabuf,
1725 &phba->hbqs[i].hbq_buffer_list, list) {
1726 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1727 list_del(&hbq_buf->dbuf.list);
1728 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1729 }
1730 phba->hbqs[i].buffer_count = 0;
1731 }
1732 /* Return all HBQ buffer that are in-fly */
1733 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1734 list) {
1735 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1736 list_del(&hbq_buf->dbuf.list);
1737 if (hbq_buf->tag == -1) {
1738 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1739 (phba, hbq_buf);
1740 } else {
1741 hbqno = hbq_buf->tag >> 16;
1742 if (hbqno >= LPFC_MAX_HBQS)
1743 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1744 (phba, hbq_buf);
1745 else
1746 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1747 hbq_buf);
1748 }
1749 }
1750
1751 /* Mark the HBQs not in use */
1752 phba->hbq_in_use = 0;
1753 spin_unlock_irqrestore(&phba->hbalock, flags);
1754 }
1755
1756 /**
1757 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1758 * @phba: Pointer to HBA context object.
1759 * @hbqno: HBQ number.
1760 * @hbq_buf: Pointer to HBQ buffer.
1761 *
1762 * This function is called with the hbalock held to post a
1763 * hbq buffer to the firmware. If the function finds an empty
1764 * slot in the HBQ, it will post the buffer. The function will return
1765 * pointer to the hbq entry if it successfully post the buffer
1766 * else it will return NULL.
1767 **/
1768 static int
1769 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1770 struct hbq_dmabuf *hbq_buf)
1771 {
1772 lockdep_assert_held(&phba->hbalock);
1773 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1774 }
1775
1776 /**
1777 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1778 * @phba: Pointer to HBA context object.
1779 * @hbqno: HBQ number.
1780 * @hbq_buf: Pointer to HBQ buffer.
1781 *
1782 * This function is called with the hbalock held to post a hbq buffer to the
1783 * firmware. If the function finds an empty slot in the HBQ, it will post the
1784 * buffer and place it on the hbq_buffer_list. The function will return zero if
1785 * it successfully post the buffer else it will return an error.
1786 **/
1787 static int
1788 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1789 struct hbq_dmabuf *hbq_buf)
1790 {
1791 struct lpfc_hbq_entry *hbqe;
1792 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1793
1794 lockdep_assert_held(&phba->hbalock);
1795 /* Get next HBQ entry slot to use */
1796 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1797 if (hbqe) {
1798 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1799
1800 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1801 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1802 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1803 hbqe->bde.tus.f.bdeFlags = 0;
1804 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1805 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1806 /* Sync SLIM */
1807 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1808 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1809 /* flush */
1810 readl(phba->hbq_put + hbqno);
1811 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1812 return 0;
1813 } else
1814 return -ENOMEM;
1815 }
1816
1817 /**
1818 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1819 * @phba: Pointer to HBA context object.
1820 * @hbqno: HBQ number.
1821 * @hbq_buf: Pointer to HBQ buffer.
1822 *
1823 * This function is called with the hbalock held to post an RQE to the SLI4
1824 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1825 * the hbq_buffer_list and return zero, otherwise it will return an error.
1826 **/
1827 static int
1828 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1829 struct hbq_dmabuf *hbq_buf)
1830 {
1831 int rc;
1832 struct lpfc_rqe hrqe;
1833 struct lpfc_rqe drqe;
1834
1835 lockdep_assert_held(&phba->hbalock);
1836 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1837 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1838 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1839 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1840 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1841 &hrqe, &drqe);
1842 if (rc < 0)
1843 return rc;
1844 hbq_buf->tag = rc;
1845 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1846 return 0;
1847 }
1848
1849 /* HBQ for ELS and CT traffic. */
1850 static struct lpfc_hbq_init lpfc_els_hbq = {
1851 .rn = 1,
1852 .entry_count = 256,
1853 .mask_count = 0,
1854 .profile = 0,
1855 .ring_mask = (1 << LPFC_ELS_RING),
1856 .buffer_count = 0,
1857 .init_count = 40,
1858 .add_count = 40,
1859 };
1860
1861 /* HBQ for the extra ring if needed */
1862 static struct lpfc_hbq_init lpfc_extra_hbq = {
1863 .rn = 1,
1864 .entry_count = 200,
1865 .mask_count = 0,
1866 .profile = 0,
1867 .ring_mask = (1 << LPFC_EXTRA_RING),
1868 .buffer_count = 0,
1869 .init_count = 0,
1870 .add_count = 5,
1871 };
1872
1873 /* Array of HBQs */
1874 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1875 &lpfc_els_hbq,
1876 &lpfc_extra_hbq,
1877 };
1878
1879 /**
1880 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1881 * @phba: Pointer to HBA context object.
1882 * @hbqno: HBQ number.
1883 * @count: Number of HBQ buffers to be posted.
1884 *
1885 * This function is called with no lock held to post more hbq buffers to the
1886 * given HBQ. The function returns the number of HBQ buffers successfully
1887 * posted.
1888 **/
1889 static int
1890 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1891 {
1892 uint32_t i, posted = 0;
1893 unsigned long flags;
1894 struct hbq_dmabuf *hbq_buffer;
1895 LIST_HEAD(hbq_buf_list);
1896 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1897 return 0;
1898
1899 if ((phba->hbqs[hbqno].buffer_count + count) >
1900 lpfc_hbq_defs[hbqno]->entry_count)
1901 count = lpfc_hbq_defs[hbqno]->entry_count -
1902 phba->hbqs[hbqno].buffer_count;
1903 if (!count)
1904 return 0;
1905 /* Allocate HBQ entries */
1906 for (i = 0; i < count; i++) {
1907 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1908 if (!hbq_buffer)
1909 break;
1910 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1911 }
1912 /* Check whether HBQ is still in use */
1913 spin_lock_irqsave(&phba->hbalock, flags);
1914 if (!phba->hbq_in_use)
1915 goto err;
1916 while (!list_empty(&hbq_buf_list)) {
1917 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1918 dbuf.list);
1919 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1920 (hbqno << 16));
1921 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1922 phba->hbqs[hbqno].buffer_count++;
1923 posted++;
1924 } else
1925 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1926 }
1927 spin_unlock_irqrestore(&phba->hbalock, flags);
1928 return posted;
1929 err:
1930 spin_unlock_irqrestore(&phba->hbalock, flags);
1931 while (!list_empty(&hbq_buf_list)) {
1932 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1933 dbuf.list);
1934 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1935 }
1936 return 0;
1937 }
1938
1939 /**
1940 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1941 * @phba: Pointer to HBA context object.
1942 * @qno: HBQ number.
1943 *
1944 * This function posts more buffers to the HBQ. This function
1945 * is called with no lock held. The function returns the number of HBQ entries
1946 * successfully allocated.
1947 **/
1948 int
1949 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1950 {
1951 if (phba->sli_rev == LPFC_SLI_REV4)
1952 return 0;
1953 else
1954 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1955 lpfc_hbq_defs[qno]->add_count);
1956 }
1957
1958 /**
1959 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1960 * @phba: Pointer to HBA context object.
1961 * @qno: HBQ queue number.
1962 *
1963 * This function is called from SLI initialization code path with
1964 * no lock held to post initial HBQ buffers to firmware. The
1965 * function returns the number of HBQ entries successfully allocated.
1966 **/
1967 static int
1968 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1969 {
1970 if (phba->sli_rev == LPFC_SLI_REV4)
1971 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1972 lpfc_hbq_defs[qno]->entry_count);
1973 else
1974 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1975 lpfc_hbq_defs[qno]->init_count);
1976 }
1977
1978 /**
1979 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1980 * @phba: Pointer to HBA context object.
1981 * @hbqno: HBQ number.
1982 *
1983 * This function removes the first hbq buffer on an hbq list and returns a
1984 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1985 **/
1986 static struct hbq_dmabuf *
1987 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1988 {
1989 struct lpfc_dmabuf *d_buf;
1990
1991 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1992 if (!d_buf)
1993 return NULL;
1994 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1995 }
1996
1997 /**
1998 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1999 * @phba: Pointer to HBA context object.
2000 * @tag: Tag of the hbq buffer.
2001 *
2002 * This function searches for the hbq buffer associated with the given tag in
2003 * the hbq buffer list. If it finds the hbq buffer, it returns the hbq_buffer
2004 * otherwise it returns NULL.
2005 **/
2006 static struct hbq_dmabuf *
2007 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2008 {
2009 struct lpfc_dmabuf *d_buf;
2010 struct hbq_dmabuf *hbq_buf;
2011 uint32_t hbqno;
2012
2013 hbqno = tag >> 16;
2014 if (hbqno >= LPFC_MAX_HBQS)
2015 return NULL;
2016
2017 spin_lock_irq(&phba->hbalock);
2018 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2019 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2020 if (hbq_buf->tag == tag) {
2021 spin_unlock_irq(&phba->hbalock);
2022 return hbq_buf;
2023 }
2024 }
2025 spin_unlock_irq(&phba->hbalock);
2026 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2027 "1803 Bad hbq tag. Data: x%x x%x\n",
2028 tag, phba->hbqs[tag >> 16].buffer_count);
2029 return NULL;
2030 }
2031
2032 /**
2033 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2034 * @phba: Pointer to HBA context object.
2035 * @hbq_buffer: Pointer to HBQ buffer.
2036 *
2037 * This function is called with hbalock. This function gives back
2038 * the hbq buffer to firmware. If the HBQ does not have space to
2039 * post the buffer, it will free the buffer.
2040 **/
2041 void
2042 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2043 {
2044 uint32_t hbqno;
2045
2046 if (hbq_buffer) {
2047 hbqno = hbq_buffer->tag >> 16;
2048 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2049 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2050 }
2051 }
2052
2053 /**
2054 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2055 * @mbxCommand: mailbox command code.
2056 *
2057 * This function is called by the mailbox event handler function to verify
2058 * that the completed mailbox command is a legitimate mailbox command. If the
2059 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2060 * and the mailbox event handler will take the HBA offline.
2061 **/
2062 static int
2063 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2064 {
2065 uint8_t ret;
2066
2067 switch (mbxCommand) {
2068 case MBX_LOAD_SM:
2069 case MBX_READ_NV:
2070 case MBX_WRITE_NV:
2071 case MBX_WRITE_VPARMS:
2072 case MBX_RUN_BIU_DIAG:
2073 case MBX_INIT_LINK:
2074 case MBX_DOWN_LINK:
2075 case MBX_CONFIG_LINK:
2076 case MBX_CONFIG_RING:
2077 case MBX_RESET_RING:
2078 case MBX_READ_CONFIG:
2079 case MBX_READ_RCONFIG:
2080 case MBX_READ_SPARM:
2081 case MBX_READ_STATUS:
2082 case MBX_READ_RPI:
2083 case MBX_READ_XRI:
2084 case MBX_READ_REV:
2085 case MBX_READ_LNK_STAT:
2086 case MBX_REG_LOGIN:
2087 case MBX_UNREG_LOGIN:
2088 case MBX_CLEAR_LA:
2089 case MBX_DUMP_MEMORY:
2090 case MBX_DUMP_CONTEXT:
2091 case MBX_RUN_DIAGS:
2092 case MBX_RESTART:
2093 case MBX_UPDATE_CFG:
2094 case MBX_DOWN_LOAD:
2095 case MBX_DEL_LD_ENTRY:
2096 case MBX_RUN_PROGRAM:
2097 case MBX_SET_MASK:
2098 case MBX_SET_VARIABLE:
2099 case MBX_UNREG_D_ID:
2100 case MBX_KILL_BOARD:
2101 case MBX_CONFIG_FARP:
2102 case MBX_BEACON:
2103 case MBX_LOAD_AREA:
2104 case MBX_RUN_BIU_DIAG64:
2105 case MBX_CONFIG_PORT:
2106 case MBX_READ_SPARM64:
2107 case MBX_READ_RPI64:
2108 case MBX_REG_LOGIN64:
2109 case MBX_READ_TOPOLOGY:
2110 case MBX_WRITE_WWN:
2111 case MBX_SET_DEBUG:
2112 case MBX_LOAD_EXP_ROM:
2113 case MBX_ASYNCEVT_ENABLE:
2114 case MBX_REG_VPI:
2115 case MBX_UNREG_VPI:
2116 case MBX_HEARTBEAT:
2117 case MBX_PORT_CAPABILITIES:
2118 case MBX_PORT_IOV_CONTROL:
2119 case MBX_SLI4_CONFIG:
2120 case MBX_SLI4_REQ_FTRS:
2121 case MBX_REG_FCFI:
2122 case MBX_UNREG_FCFI:
2123 case MBX_REG_VFI:
2124 case MBX_UNREG_VFI:
2125 case MBX_INIT_VPI:
2126 case MBX_INIT_VFI:
2127 case MBX_RESUME_RPI:
2128 case MBX_READ_EVENT_LOG_STATUS:
2129 case MBX_READ_EVENT_LOG:
2130 case MBX_SECURITY_MGMT:
2131 case MBX_AUTH_PORT:
2132 case MBX_ACCESS_VDATA:
2133 ret = mbxCommand;
2134 break;
2135 default:
2136 ret = MBX_SHUTDOWN;
2137 break;
2138 }
2139 return ret;
2140 }
2141
2142 /**
2143 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2144 * @phba: Pointer to HBA context object.
2145 * @pmboxq: Pointer to mailbox command.
2146 *
2147 * This is completion handler function for mailbox commands issued from
2148 * lpfc_sli_issue_mbox_wait function. This function is called by the
2149 * mailbox event handler function with no lock held. This function
2150 * will wake up thread waiting on the wait queue pointed by context1
2151 * of the mailbox.
2152 **/
2153 void
2154 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2155 {
2156 wait_queue_head_t *pdone_q;
2157 unsigned long drvr_flag;
2158
2159 /*
2160 * If pdone_q is empty, the driver thread gave up waiting and
2161 * continued running.
2162 */
2163 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2164 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2165 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2166 if (pdone_q)
2167 wake_up_interruptible(pdone_q);
2168 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2169 return;
2170 }
2171
2172
2173 /**
2174 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2175 * @phba: Pointer to HBA context object.
2176 * @pmb: Pointer to mailbox object.
2177 *
2178 * This function is the default mailbox completion handler. It
2179 * frees the memory resources associated with the completed mailbox
2180 * command. If the completed command is a REG_LOGIN mailbox command,
2181 * this function will issue a UREG_LOGIN to re-claim the RPI.
2182 **/
2183 void
2184 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2185 {
2186 struct lpfc_vport *vport = pmb->vport;
2187 struct lpfc_dmabuf *mp;
2188 struct lpfc_nodelist *ndlp;
2189 struct Scsi_Host *shost;
2190 uint16_t rpi, vpi;
2191 int rc;
2192
2193 mp = (struct lpfc_dmabuf *) (pmb->context1);
2194
2195 if (mp) {
2196 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2197 kfree(mp);
2198 }
2199
2200 /*
2201 * If a REG_LOGIN succeeded after node is destroyed or node
2202 * is in re-discovery driver need to cleanup the RPI.
2203 */
2204 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2205 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2206 !pmb->u.mb.mbxStatus) {
2207 rpi = pmb->u.mb.un.varWords[0];
2208 vpi = pmb->u.mb.un.varRegLogin.vpi;
2209 lpfc_unreg_login(phba, vpi, rpi, pmb);
2210 pmb->vport = vport;
2211 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2212 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2213 if (rc != MBX_NOT_FINISHED)
2214 return;
2215 }
2216
2217 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2218 !(phba->pport->load_flag & FC_UNLOADING) &&
2219 !pmb->u.mb.mbxStatus) {
2220 shost = lpfc_shost_from_vport(vport);
2221 spin_lock_irq(shost->host_lock);
2222 vport->vpi_state |= LPFC_VPI_REGISTERED;
2223 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2224 spin_unlock_irq(shost->host_lock);
2225 }
2226
2227 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2228 ndlp = (struct lpfc_nodelist *)pmb->context2;
2229 lpfc_nlp_put(ndlp);
2230 pmb->context2 = NULL;
2231 }
2232
2233 /* Check security permission status on INIT_LINK mailbox command */
2234 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2235 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2236 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2237 "2860 SLI authentication is required "
2238 "for INIT_LINK but has not done yet\n");
2239
2240 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2241 lpfc_sli4_mbox_cmd_free(phba, pmb);
2242 else
2243 mempool_free(pmb, phba->mbox_mem_pool);
2244 }
2245 /**
2246 * lpfc_sli4_unreg_rpi_cmpl_clr - mailbox completion handler
2247 * @phba: Pointer to HBA context object.
2248 * @pmb: Pointer to mailbox object.
2249 *
2250 * This function is the unreg rpi mailbox completion handler. It
2251 * frees the memory resources associated with the completed mailbox
2252 * command. An additional refrenece is put on the ndlp to prevent
2253 * lpfc_nlp_release from freeing the rpi bit in the bitmask before
2254 * the unreg mailbox command completes, this routine puts the
2255 * reference back.
2256 *
2257 **/
2258 void
2259 lpfc_sli4_unreg_rpi_cmpl_clr(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2260 {
2261 struct lpfc_vport *vport = pmb->vport;
2262 struct lpfc_nodelist *ndlp;
2263
2264 ndlp = pmb->context1;
2265 if (pmb->u.mb.mbxCommand == MBX_UNREG_LOGIN) {
2266 if (phba->sli_rev == LPFC_SLI_REV4 &&
2267 (bf_get(lpfc_sli_intf_if_type,
2268 &phba->sli4_hba.sli_intf) ==
2269 LPFC_SLI_INTF_IF_TYPE_2)) {
2270 if (ndlp) {
2271 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
2272 "0010 UNREG_LOGIN vpi:%x "
2273 "rpi:%x DID:%x map:%x %p\n",
2274 vport->vpi, ndlp->nlp_rpi,
2275 ndlp->nlp_DID,
2276 ndlp->nlp_usg_map, ndlp);
2277 ndlp->nlp_flag &= ~NLP_LOGO_ACC;
2278 lpfc_nlp_put(ndlp);
2279 }
2280 }
2281 }
2282
2283 mempool_free(pmb, phba->mbox_mem_pool);
2284 }
2285
2286 /**
2287 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2288 * @phba: Pointer to HBA context object.
2289 *
2290 * This function is called with no lock held. This function processes all
2291 * the completed mailbox commands and gives it to upper layers. The interrupt
2292 * service routine processes mailbox completion interrupt and adds completed
2293 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2294 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2295 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2296 * function returns the mailbox commands to the upper layer by calling the
2297 * completion handler function of each mailbox.
2298 **/
2299 int
2300 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2301 {
2302 MAILBOX_t *pmbox;
2303 LPFC_MBOXQ_t *pmb;
2304 int rc;
2305 LIST_HEAD(cmplq);
2306
2307 phba->sli.slistat.mbox_event++;
2308
2309 /* Get all completed mailboxe buffers into the cmplq */
2310 spin_lock_irq(&phba->hbalock);
2311 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2312 spin_unlock_irq(&phba->hbalock);
2313
2314 /* Get a Mailbox buffer to setup mailbox commands for callback */
2315 do {
2316 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2317 if (pmb == NULL)
2318 break;
2319
2320 pmbox = &pmb->u.mb;
2321
2322 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2323 if (pmb->vport) {
2324 lpfc_debugfs_disc_trc(pmb->vport,
2325 LPFC_DISC_TRC_MBOX_VPORT,
2326 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2327 (uint32_t)pmbox->mbxCommand,
2328 pmbox->un.varWords[0],
2329 pmbox->un.varWords[1]);
2330 }
2331 else {
2332 lpfc_debugfs_disc_trc(phba->pport,
2333 LPFC_DISC_TRC_MBOX,
2334 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2335 (uint32_t)pmbox->mbxCommand,
2336 pmbox->un.varWords[0],
2337 pmbox->un.varWords[1]);
2338 }
2339 }
2340
2341 /*
2342 * It is a fatal error if unknown mbox command completion.
2343 */
2344 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2345 MBX_SHUTDOWN) {
2346 /* Unknown mailbox command compl */
2347 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2348 "(%d):0323 Unknown Mailbox command "
2349 "x%x (x%x/x%x) Cmpl\n",
2350 pmb->vport ? pmb->vport->vpi : 0,
2351 pmbox->mbxCommand,
2352 lpfc_sli_config_mbox_subsys_get(phba,
2353 pmb),
2354 lpfc_sli_config_mbox_opcode_get(phba,
2355 pmb));
2356 phba->link_state = LPFC_HBA_ERROR;
2357 phba->work_hs = HS_FFER3;
2358 lpfc_handle_eratt(phba);
2359 continue;
2360 }
2361
2362 if (pmbox->mbxStatus) {
2363 phba->sli.slistat.mbox_stat_err++;
2364 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2365 /* Mbox cmd cmpl error - RETRYing */
2366 lpfc_printf_log(phba, KERN_INFO,
2367 LOG_MBOX | LOG_SLI,
2368 "(%d):0305 Mbox cmd cmpl "
2369 "error - RETRYing Data: x%x "
2370 "(x%x/x%x) x%x x%x x%x\n",
2371 pmb->vport ? pmb->vport->vpi : 0,
2372 pmbox->mbxCommand,
2373 lpfc_sli_config_mbox_subsys_get(phba,
2374 pmb),
2375 lpfc_sli_config_mbox_opcode_get(phba,
2376 pmb),
2377 pmbox->mbxStatus,
2378 pmbox->un.varWords[0],
2379 pmb->vport->port_state);
2380 pmbox->mbxStatus = 0;
2381 pmbox->mbxOwner = OWN_HOST;
2382 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2383 if (rc != MBX_NOT_FINISHED)
2384 continue;
2385 }
2386 }
2387
2388 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2389 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2390 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2391 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
2392 "x%x x%x x%x\n",
2393 pmb->vport ? pmb->vport->vpi : 0,
2394 pmbox->mbxCommand,
2395 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2396 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2397 pmb->mbox_cmpl,
2398 *((uint32_t *) pmbox),
2399 pmbox->un.varWords[0],
2400 pmbox->un.varWords[1],
2401 pmbox->un.varWords[2],
2402 pmbox->un.varWords[3],
2403 pmbox->un.varWords[4],
2404 pmbox->un.varWords[5],
2405 pmbox->un.varWords[6],
2406 pmbox->un.varWords[7],
2407 pmbox->un.varWords[8],
2408 pmbox->un.varWords[9],
2409 pmbox->un.varWords[10]);
2410
2411 if (pmb->mbox_cmpl)
2412 pmb->mbox_cmpl(phba,pmb);
2413 } while (1);
2414 return 0;
2415 }
2416
2417 /**
2418 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2419 * @phba: Pointer to HBA context object.
2420 * @pring: Pointer to driver SLI ring object.
2421 * @tag: buffer tag.
2422 *
2423 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2424 * is set in the tag the buffer is posted for a particular exchange,
2425 * the function will return the buffer without replacing the buffer.
2426 * If the buffer is for unsolicited ELS or CT traffic, this function
2427 * returns the buffer and also posts another buffer to the firmware.
2428 **/
2429 static struct lpfc_dmabuf *
2430 lpfc_sli_get_buff(struct lpfc_hba *phba,
2431 struct lpfc_sli_ring *pring,
2432 uint32_t tag)
2433 {
2434 struct hbq_dmabuf *hbq_entry;
2435
2436 if (tag & QUE_BUFTAG_BIT)
2437 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2438 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2439 if (!hbq_entry)
2440 return NULL;
2441 return &hbq_entry->dbuf;
2442 }
2443
2444 /**
2445 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2446 * @phba: Pointer to HBA context object.
2447 * @pring: Pointer to driver SLI ring object.
2448 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2449 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2450 * @fch_type: the type for the first frame of the sequence.
2451 *
2452 * This function is called with no lock held. This function uses the r_ctl and
2453 * type of the received sequence to find the correct callback function to call
2454 * to process the sequence.
2455 **/
2456 static int
2457 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2458 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2459 uint32_t fch_type)
2460 {
2461 int i;
2462
2463 /* unSolicited Responses */
2464 if (pring->prt[0].profile) {
2465 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2466 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2467 saveq);
2468 return 1;
2469 }
2470 /* We must search, based on rctl / type
2471 for the right routine */
2472 for (i = 0; i < pring->num_mask; i++) {
2473 if ((pring->prt[i].rctl == fch_r_ctl) &&
2474 (pring->prt[i].type == fch_type)) {
2475 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2476 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2477 (phba, pring, saveq);
2478 return 1;
2479 }
2480 }
2481 return 0;
2482 }
2483
2484 /**
2485 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2486 * @phba: Pointer to HBA context object.
2487 * @pring: Pointer to driver SLI ring object.
2488 * @saveq: Pointer to the unsolicited iocb.
2489 *
2490 * This function is called with no lock held by the ring event handler
2491 * when there is an unsolicited iocb posted to the response ring by the
2492 * firmware. This function gets the buffer associated with the iocbs
2493 * and calls the event handler for the ring. This function handles both
2494 * qring buffers and hbq buffers.
2495 * When the function returns 1 the caller can free the iocb object otherwise
2496 * upper layer functions will free the iocb objects.
2497 **/
2498 static int
2499 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2500 struct lpfc_iocbq *saveq)
2501 {
2502 IOCB_t * irsp;
2503 WORD5 * w5p;
2504 uint32_t Rctl, Type;
2505 struct lpfc_iocbq *iocbq;
2506 struct lpfc_dmabuf *dmzbuf;
2507
2508 irsp = &(saveq->iocb);
2509
2510 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2511 if (pring->lpfc_sli_rcv_async_status)
2512 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2513 else
2514 lpfc_printf_log(phba,
2515 KERN_WARNING,
2516 LOG_SLI,
2517 "0316 Ring %d handler: unexpected "
2518 "ASYNC_STATUS iocb received evt_code "
2519 "0x%x\n",
2520 pring->ringno,
2521 irsp->un.asyncstat.evt_code);
2522 return 1;
2523 }
2524
2525 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2526 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2527 if (irsp->ulpBdeCount > 0) {
2528 dmzbuf = lpfc_sli_get_buff(phba, pring,
2529 irsp->un.ulpWord[3]);
2530 lpfc_in_buf_free(phba, dmzbuf);
2531 }
2532
2533 if (irsp->ulpBdeCount > 1) {
2534 dmzbuf = lpfc_sli_get_buff(phba, pring,
2535 irsp->unsli3.sli3Words[3]);
2536 lpfc_in_buf_free(phba, dmzbuf);
2537 }
2538
2539 if (irsp->ulpBdeCount > 2) {
2540 dmzbuf = lpfc_sli_get_buff(phba, pring,
2541 irsp->unsli3.sli3Words[7]);
2542 lpfc_in_buf_free(phba, dmzbuf);
2543 }
2544
2545 return 1;
2546 }
2547
2548 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2549 if (irsp->ulpBdeCount != 0) {
2550 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2551 irsp->un.ulpWord[3]);
2552 if (!saveq->context2)
2553 lpfc_printf_log(phba,
2554 KERN_ERR,
2555 LOG_SLI,
2556 "0341 Ring %d Cannot find buffer for "
2557 "an unsolicited iocb. tag 0x%x\n",
2558 pring->ringno,
2559 irsp->un.ulpWord[3]);
2560 }
2561 if (irsp->ulpBdeCount == 2) {
2562 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2563 irsp->unsli3.sli3Words[7]);
2564 if (!saveq->context3)
2565 lpfc_printf_log(phba,
2566 KERN_ERR,
2567 LOG_SLI,
2568 "0342 Ring %d Cannot find buffer for an"
2569 " unsolicited iocb. tag 0x%x\n",
2570 pring->ringno,
2571 irsp->unsli3.sli3Words[7]);
2572 }
2573 list_for_each_entry(iocbq, &saveq->list, list) {
2574 irsp = &(iocbq->iocb);
2575 if (irsp->ulpBdeCount != 0) {
2576 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2577 irsp->un.ulpWord[3]);
2578 if (!iocbq->context2)
2579 lpfc_printf_log(phba,
2580 KERN_ERR,
2581 LOG_SLI,
2582 "0343 Ring %d Cannot find "
2583 "buffer for an unsolicited iocb"
2584 ". tag 0x%x\n", pring->ringno,
2585 irsp->un.ulpWord[3]);
2586 }
2587 if (irsp->ulpBdeCount == 2) {
2588 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2589 irsp->unsli3.sli3Words[7]);
2590 if (!iocbq->context3)
2591 lpfc_printf_log(phba,
2592 KERN_ERR,
2593 LOG_SLI,
2594 "0344 Ring %d Cannot find "
2595 "buffer for an unsolicited "
2596 "iocb. tag 0x%x\n",
2597 pring->ringno,
2598 irsp->unsli3.sli3Words[7]);
2599 }
2600 }
2601 }
2602 if (irsp->ulpBdeCount != 0 &&
2603 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2604 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2605 int found = 0;
2606
2607 /* search continue save q for same XRI */
2608 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2609 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2610 saveq->iocb.unsli3.rcvsli3.ox_id) {
2611 list_add_tail(&saveq->list, &iocbq->list);
2612 found = 1;
2613 break;
2614 }
2615 }
2616 if (!found)
2617 list_add_tail(&saveq->clist,
2618 &pring->iocb_continue_saveq);
2619 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2620 list_del_init(&iocbq->clist);
2621 saveq = iocbq;
2622 irsp = &(saveq->iocb);
2623 } else
2624 return 0;
2625 }
2626 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2627 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2628 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2629 Rctl = FC_RCTL_ELS_REQ;
2630 Type = FC_TYPE_ELS;
2631 } else {
2632 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2633 Rctl = w5p->hcsw.Rctl;
2634 Type = w5p->hcsw.Type;
2635
2636 /* Firmware Workaround */
2637 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2638 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2639 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2640 Rctl = FC_RCTL_ELS_REQ;
2641 Type = FC_TYPE_ELS;
2642 w5p->hcsw.Rctl = Rctl;
2643 w5p->hcsw.Type = Type;
2644 }
2645 }
2646
2647 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2648 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2649 "0313 Ring %d handler: unexpected Rctl x%x "
2650 "Type x%x received\n",
2651 pring->ringno, Rctl, Type);
2652
2653 return 1;
2654 }
2655
2656 /**
2657 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2658 * @phba: Pointer to HBA context object.
2659 * @pring: Pointer to driver SLI ring object.
2660 * @prspiocb: Pointer to response iocb object.
2661 *
2662 * This function looks up the iocb_lookup table to get the command iocb
2663 * corresponding to the given response iocb using the iotag of the
2664 * response iocb. This function is called with the hbalock held.
2665 * This function returns the command iocb object if it finds the command
2666 * iocb else returns NULL.
2667 **/
2668 static struct lpfc_iocbq *
2669 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2670 struct lpfc_sli_ring *pring,
2671 struct lpfc_iocbq *prspiocb)
2672 {
2673 struct lpfc_iocbq *cmd_iocb = NULL;
2674 uint16_t iotag;
2675 lockdep_assert_held(&phba->hbalock);
2676
2677 iotag = prspiocb->iocb.ulpIoTag;
2678
2679 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2680 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2681 list_del_init(&cmd_iocb->list);
2682 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2683 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2684 }
2685 return cmd_iocb;
2686 }
2687
2688 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2689 "0317 iotag x%x is out off "
2690 "range: max iotag x%x wd0 x%x\n",
2691 iotag, phba->sli.last_iotag,
2692 *(((uint32_t *) &prspiocb->iocb) + 7));
2693 return NULL;
2694 }
2695
2696 /**
2697 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2698 * @phba: Pointer to HBA context object.
2699 * @pring: Pointer to driver SLI ring object.
2700 * @iotag: IOCB tag.
2701 *
2702 * This function looks up the iocb_lookup table to get the command iocb
2703 * corresponding to the given iotag. This function is called with the
2704 * hbalock held.
2705 * This function returns the command iocb object if it finds the command
2706 * iocb else returns NULL.
2707 **/
2708 static struct lpfc_iocbq *
2709 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2710 struct lpfc_sli_ring *pring, uint16_t iotag)
2711 {
2712 struct lpfc_iocbq *cmd_iocb;
2713
2714 lockdep_assert_held(&phba->hbalock);
2715 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2716 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2717 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2718 /* remove from txcmpl queue list */
2719 list_del_init(&cmd_iocb->list);
2720 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2721 return cmd_iocb;
2722 }
2723 }
2724 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2725 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2726 iotag, phba->sli.last_iotag);
2727 return NULL;
2728 }
2729
2730 /**
2731 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2732 * @phba: Pointer to HBA context object.
2733 * @pring: Pointer to driver SLI ring object.
2734 * @saveq: Pointer to the response iocb to be processed.
2735 *
2736 * This function is called by the ring event handler for non-fcp
2737 * rings when there is a new response iocb in the response ring.
2738 * The caller is not required to hold any locks. This function
2739 * gets the command iocb associated with the response iocb and
2740 * calls the completion handler for the command iocb. If there
2741 * is no completion handler, the function will free the resources
2742 * associated with command iocb. If the response iocb is for
2743 * an already aborted command iocb, the status of the completion
2744 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2745 * This function always returns 1.
2746 **/
2747 static int
2748 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2749 struct lpfc_iocbq *saveq)
2750 {
2751 struct lpfc_iocbq *cmdiocbp;
2752 int rc = 1;
2753 unsigned long iflag;
2754
2755 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2756 spin_lock_irqsave(&phba->hbalock, iflag);
2757 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2758 spin_unlock_irqrestore(&phba->hbalock, iflag);
2759
2760 if (cmdiocbp) {
2761 if (cmdiocbp->iocb_cmpl) {
2762 /*
2763 * If an ELS command failed send an event to mgmt
2764 * application.
2765 */
2766 if (saveq->iocb.ulpStatus &&
2767 (pring->ringno == LPFC_ELS_RING) &&
2768 (cmdiocbp->iocb.ulpCommand ==
2769 CMD_ELS_REQUEST64_CR))
2770 lpfc_send_els_failure_event(phba,
2771 cmdiocbp, saveq);
2772
2773 /*
2774 * Post all ELS completions to the worker thread.
2775 * All other are passed to the completion callback.
2776 */
2777 if (pring->ringno == LPFC_ELS_RING) {
2778 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2779 (cmdiocbp->iocb_flag &
2780 LPFC_DRIVER_ABORTED)) {
2781 spin_lock_irqsave(&phba->hbalock,
2782 iflag);
2783 cmdiocbp->iocb_flag &=
2784 ~LPFC_DRIVER_ABORTED;
2785 spin_unlock_irqrestore(&phba->hbalock,
2786 iflag);
2787 saveq->iocb.ulpStatus =
2788 IOSTAT_LOCAL_REJECT;
2789 saveq->iocb.un.ulpWord[4] =
2790 IOERR_SLI_ABORTED;
2791
2792 /* Firmware could still be in progress
2793 * of DMAing payload, so don't free data
2794 * buffer till after a hbeat.
2795 */
2796 spin_lock_irqsave(&phba->hbalock,
2797 iflag);
2798 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2799 spin_unlock_irqrestore(&phba->hbalock,
2800 iflag);
2801 }
2802 if (phba->sli_rev == LPFC_SLI_REV4) {
2803 if (saveq->iocb_flag &
2804 LPFC_EXCHANGE_BUSY) {
2805 /* Set cmdiocb flag for the
2806 * exchange busy so sgl (xri)
2807 * will not be released until
2808 * the abort xri is received
2809 * from hba.
2810 */
2811 spin_lock_irqsave(
2812 &phba->hbalock, iflag);
2813 cmdiocbp->iocb_flag |=
2814 LPFC_EXCHANGE_BUSY;
2815 spin_unlock_irqrestore(
2816 &phba->hbalock, iflag);
2817 }
2818 if (cmdiocbp->iocb_flag &
2819 LPFC_DRIVER_ABORTED) {
2820 /*
2821 * Clear LPFC_DRIVER_ABORTED
2822 * bit in case it was driver
2823 * initiated abort.
2824 */
2825 spin_lock_irqsave(
2826 &phba->hbalock, iflag);
2827 cmdiocbp->iocb_flag &=
2828 ~LPFC_DRIVER_ABORTED;
2829 spin_unlock_irqrestore(
2830 &phba->hbalock, iflag);
2831 cmdiocbp->iocb.ulpStatus =
2832 IOSTAT_LOCAL_REJECT;
2833 cmdiocbp->iocb.un.ulpWord[4] =
2834 IOERR_ABORT_REQUESTED;
2835 /*
2836 * For SLI4, irsiocb contains
2837 * NO_XRI in sli_xritag, it
2838 * shall not affect releasing
2839 * sgl (xri) process.
2840 */
2841 saveq->iocb.ulpStatus =
2842 IOSTAT_LOCAL_REJECT;
2843 saveq->iocb.un.ulpWord[4] =
2844 IOERR_SLI_ABORTED;
2845 spin_lock_irqsave(
2846 &phba->hbalock, iflag);
2847 saveq->iocb_flag |=
2848 LPFC_DELAY_MEM_FREE;
2849 spin_unlock_irqrestore(
2850 &phba->hbalock, iflag);
2851 }
2852 }
2853 }
2854 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2855 } else
2856 lpfc_sli_release_iocbq(phba, cmdiocbp);
2857 } else {
2858 /*
2859 * Unknown initiating command based on the response iotag.
2860 * This could be the case on the ELS ring because of
2861 * lpfc_els_abort().
2862 */
2863 if (pring->ringno != LPFC_ELS_RING) {
2864 /*
2865 * Ring <ringno> handler: unexpected completion IoTag
2866 * <IoTag>
2867 */
2868 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2869 "0322 Ring %d handler: "
2870 "unexpected completion IoTag x%x "
2871 "Data: x%x x%x x%x x%x\n",
2872 pring->ringno,
2873 saveq->iocb.ulpIoTag,
2874 saveq->iocb.ulpStatus,
2875 saveq->iocb.un.ulpWord[4],
2876 saveq->iocb.ulpCommand,
2877 saveq->iocb.ulpContext);
2878 }
2879 }
2880
2881 return rc;
2882 }
2883
2884 /**
2885 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2886 * @phba: Pointer to HBA context object.
2887 * @pring: Pointer to driver SLI ring object.
2888 *
2889 * This function is called from the iocb ring event handlers when
2890 * put pointer is ahead of the get pointer for a ring. This function signal
2891 * an error attention condition to the worker thread and the worker
2892 * thread will transition the HBA to offline state.
2893 **/
2894 static void
2895 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2896 {
2897 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2898 /*
2899 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2900 * rsp ring <portRspMax>
2901 */
2902 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2903 "0312 Ring %d handler: portRspPut %d "
2904 "is bigger than rsp ring %d\n",
2905 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2906 pring->sli.sli3.numRiocb);
2907
2908 phba->link_state = LPFC_HBA_ERROR;
2909
2910 /*
2911 * All error attention handlers are posted to
2912 * worker thread
2913 */
2914 phba->work_ha |= HA_ERATT;
2915 phba->work_hs = HS_FFER3;
2916
2917 lpfc_worker_wake_up(phba);
2918
2919 return;
2920 }
2921
2922 /**
2923 * lpfc_poll_eratt - Error attention polling timer timeout handler
2924 * @ptr: Pointer to address of HBA context object.
2925 *
2926 * This function is invoked by the Error Attention polling timer when the
2927 * timer times out. It will check the SLI Error Attention register for
2928 * possible attention events. If so, it will post an Error Attention event
2929 * and wake up worker thread to process it. Otherwise, it will set up the
2930 * Error Attention polling timer for the next poll.
2931 **/
2932 void lpfc_poll_eratt(unsigned long ptr)
2933 {
2934 struct lpfc_hba *phba;
2935 uint32_t eratt = 0;
2936 uint64_t sli_intr, cnt;
2937
2938 phba = (struct lpfc_hba *)ptr;
2939
2940 /* Here we will also keep track of interrupts per sec of the hba */
2941 sli_intr = phba->sli.slistat.sli_intr;
2942
2943 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2944 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2945 sli_intr);
2946 else
2947 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2948
2949 /* 64-bit integer division not supported on 32-bit x86 - use do_div */
2950 do_div(cnt, phba->eratt_poll_interval);
2951 phba->sli.slistat.sli_ips = cnt;
2952
2953 phba->sli.slistat.sli_prev_intr = sli_intr;
2954
2955 /* Check chip HA register for error event */
2956 eratt = lpfc_sli_check_eratt(phba);
2957
2958 if (eratt)
2959 /* Tell the worker thread there is work to do */
2960 lpfc_worker_wake_up(phba);
2961 else
2962 /* Restart the timer for next eratt poll */
2963 mod_timer(&phba->eratt_poll,
2964 jiffies +
2965 msecs_to_jiffies(1000 * phba->eratt_poll_interval));
2966 return;
2967 }
2968
2969
2970 /**
2971 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2972 * @phba: Pointer to HBA context object.
2973 * @pring: Pointer to driver SLI ring object.
2974 * @mask: Host attention register mask for this ring.
2975 *
2976 * This function is called from the interrupt context when there is a ring
2977 * event for the fcp ring. The caller does not hold any lock.
2978 * The function processes each response iocb in the response ring until it
2979 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2980 * LE bit set. The function will call the completion handler of the command iocb
2981 * if the response iocb indicates a completion for a command iocb or it is
2982 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2983 * function if this is an unsolicited iocb.
2984 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2985 * to check it explicitly.
2986 */
2987 int
2988 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2989 struct lpfc_sli_ring *pring, uint32_t mask)
2990 {
2991 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2992 IOCB_t *irsp = NULL;
2993 IOCB_t *entry = NULL;
2994 struct lpfc_iocbq *cmdiocbq = NULL;
2995 struct lpfc_iocbq rspiocbq;
2996 uint32_t status;
2997 uint32_t portRspPut, portRspMax;
2998 int rc = 1;
2999 lpfc_iocb_type type;
3000 unsigned long iflag;
3001 uint32_t rsp_cmpl = 0;
3002
3003 spin_lock_irqsave(&phba->hbalock, iflag);
3004 pring->stats.iocb_event++;
3005
3006 /*
3007 * The next available response entry should never exceed the maximum
3008 * entries. If it does, treat it as an adapter hardware error.
3009 */
3010 portRspMax = pring->sli.sli3.numRiocb;
3011 portRspPut = le32_to_cpu(pgp->rspPutInx);
3012 if (unlikely(portRspPut >= portRspMax)) {
3013 lpfc_sli_rsp_pointers_error(phba, pring);
3014 spin_unlock_irqrestore(&phba->hbalock, iflag);
3015 return 1;
3016 }
3017 if (phba->fcp_ring_in_use) {
3018 spin_unlock_irqrestore(&phba->hbalock, iflag);
3019 return 1;
3020 } else
3021 phba->fcp_ring_in_use = 1;
3022
3023 rmb();
3024 while (pring->sli.sli3.rspidx != portRspPut) {
3025 /*
3026 * Fetch an entry off the ring and copy it into a local data
3027 * structure. The copy involves a byte-swap since the
3028 * network byte order and pci byte orders are different.
3029 */
3030 entry = lpfc_resp_iocb(phba, pring);
3031 phba->last_completion_time = jiffies;
3032
3033 if (++pring->sli.sli3.rspidx >= portRspMax)
3034 pring->sli.sli3.rspidx = 0;
3035
3036 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
3037 (uint32_t *) &rspiocbq.iocb,
3038 phba->iocb_rsp_size);
3039 INIT_LIST_HEAD(&(rspiocbq.list));
3040 irsp = &rspiocbq.iocb;
3041
3042 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
3043 pring->stats.iocb_rsp++;
3044 rsp_cmpl++;
3045
3046 if (unlikely(irsp->ulpStatus)) {
3047 /*
3048 * If resource errors reported from HBA, reduce
3049 * queuedepths of the SCSI device.
3050 */
3051 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3052 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3053 IOERR_NO_RESOURCES)) {
3054 spin_unlock_irqrestore(&phba->hbalock, iflag);
3055 phba->lpfc_rampdown_queue_depth(phba);
3056 spin_lock_irqsave(&phba->hbalock, iflag);
3057 }
3058
3059 /* Rsp ring <ringno> error: IOCB */
3060 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3061 "0336 Rsp Ring %d error: IOCB Data: "
3062 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3063 pring->ringno,
3064 irsp->un.ulpWord[0],
3065 irsp->un.ulpWord[1],
3066 irsp->un.ulpWord[2],
3067 irsp->un.ulpWord[3],
3068 irsp->un.ulpWord[4],
3069 irsp->un.ulpWord[5],
3070 *(uint32_t *)&irsp->un1,
3071 *((uint32_t *)&irsp->un1 + 1));
3072 }
3073
3074 switch (type) {
3075 case LPFC_ABORT_IOCB:
3076 case LPFC_SOL_IOCB:
3077 /*
3078 * Idle exchange closed via ABTS from port. No iocb
3079 * resources need to be recovered.
3080 */
3081 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3082 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3083 "0333 IOCB cmd 0x%x"
3084 " processed. Skipping"
3085 " completion\n",
3086 irsp->ulpCommand);
3087 break;
3088 }
3089
3090 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3091 &rspiocbq);
3092 if (unlikely(!cmdiocbq))
3093 break;
3094 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3095 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3096 if (cmdiocbq->iocb_cmpl) {
3097 spin_unlock_irqrestore(&phba->hbalock, iflag);
3098 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3099 &rspiocbq);
3100 spin_lock_irqsave(&phba->hbalock, iflag);
3101 }
3102 break;
3103 case LPFC_UNSOL_IOCB:
3104 spin_unlock_irqrestore(&phba->hbalock, iflag);
3105 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3106 spin_lock_irqsave(&phba->hbalock, iflag);
3107 break;
3108 default:
3109 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3110 char adaptermsg[LPFC_MAX_ADPTMSG];
3111 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3112 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3113 MAX_MSG_DATA);
3114 dev_warn(&((phba->pcidev)->dev),
3115 "lpfc%d: %s\n",
3116 phba->brd_no, adaptermsg);
3117 } else {
3118 /* Unknown IOCB command */
3119 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3120 "0334 Unknown IOCB command "
3121 "Data: x%x, x%x x%x x%x x%x\n",
3122 type, irsp->ulpCommand,
3123 irsp->ulpStatus,
3124 irsp->ulpIoTag,
3125 irsp->ulpContext);
3126 }
3127 break;
3128 }
3129
3130 /*
3131 * The response IOCB has been processed. Update the ring
3132 * pointer in SLIM. If the port response put pointer has not
3133 * been updated, sync the pgp->rspPutInx and fetch the new port
3134 * response put pointer.
3135 */
3136 writel(pring->sli.sli3.rspidx,
3137 &phba->host_gp[pring->ringno].rspGetInx);
3138
3139 if (pring->sli.sli3.rspidx == portRspPut)
3140 portRspPut = le32_to_cpu(pgp->rspPutInx);
3141 }
3142
3143 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3144 pring->stats.iocb_rsp_full++;
3145 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3146 writel(status, phba->CAregaddr);
3147 readl(phba->CAregaddr);
3148 }
3149 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3150 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3151 pring->stats.iocb_cmd_empty++;
3152
3153 /* Force update of the local copy of cmdGetInx */
3154 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3155 lpfc_sli_resume_iocb(phba, pring);
3156
3157 if ((pring->lpfc_sli_cmd_available))
3158 (pring->lpfc_sli_cmd_available) (phba, pring);
3159
3160 }
3161
3162 phba->fcp_ring_in_use = 0;
3163 spin_unlock_irqrestore(&phba->hbalock, iflag);
3164 return rc;
3165 }
3166
3167 /**
3168 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3169 * @phba: Pointer to HBA context object.
3170 * @pring: Pointer to driver SLI ring object.
3171 * @rspiocbp: Pointer to driver response IOCB object.
3172 *
3173 * This function is called from the worker thread when there is a slow-path
3174 * response IOCB to process. This function chains all the response iocbs until
3175 * seeing the iocb with the LE bit set. The function will call
3176 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3177 * completion of a command iocb. The function will call the
3178 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3179 * The function frees the resources or calls the completion handler if this
3180 * iocb is an abort completion. The function returns NULL when the response
3181 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3182 * this function shall chain the iocb on to the iocb_continueq and return the
3183 * response iocb passed in.
3184 **/
3185 static struct lpfc_iocbq *
3186 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3187 struct lpfc_iocbq *rspiocbp)
3188 {
3189 struct lpfc_iocbq *saveq;
3190 struct lpfc_iocbq *cmdiocbp;
3191 struct lpfc_iocbq *next_iocb;
3192 IOCB_t *irsp = NULL;
3193 uint32_t free_saveq;
3194 uint8_t iocb_cmd_type;
3195 lpfc_iocb_type type;
3196 unsigned long iflag;
3197 int rc;
3198
3199 spin_lock_irqsave(&phba->hbalock, iflag);
3200 /* First add the response iocb to the countinueq list */
3201 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3202 pring->iocb_continueq_cnt++;
3203
3204 /* Now, determine whether the list is completed for processing */
3205 irsp = &rspiocbp->iocb;
3206 if (irsp->ulpLe) {
3207 /*
3208 * By default, the driver expects to free all resources
3209 * associated with this iocb completion.
3210 */
3211 free_saveq = 1;
3212 saveq = list_get_first(&pring->iocb_continueq,
3213 struct lpfc_iocbq, list);
3214 irsp = &(saveq->iocb);
3215 list_del_init(&pring->iocb_continueq);
3216 pring->iocb_continueq_cnt = 0;
3217
3218 pring->stats.iocb_rsp++;
3219
3220 /*
3221 * If resource errors reported from HBA, reduce
3222 * queuedepths of the SCSI device.
3223 */
3224 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3225 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3226 IOERR_NO_RESOURCES)) {
3227 spin_unlock_irqrestore(&phba->hbalock, iflag);
3228 phba->lpfc_rampdown_queue_depth(phba);
3229 spin_lock_irqsave(&phba->hbalock, iflag);
3230 }
3231
3232 if (irsp->ulpStatus) {
3233 /* Rsp ring <ringno> error: IOCB */
3234 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3235 "0328 Rsp Ring %d error: "
3236 "IOCB Data: "
3237 "x%x x%x x%x x%x "
3238 "x%x x%x x%x x%x "
3239 "x%x x%x x%x x%x "
3240 "x%x x%x x%x x%x\n",
3241 pring->ringno,
3242 irsp->un.ulpWord[0],
3243 irsp->un.ulpWord[1],
3244 irsp->un.ulpWord[2],
3245 irsp->un.ulpWord[3],
3246 irsp->un.ulpWord[4],
3247 irsp->un.ulpWord[5],
3248 *(((uint32_t *) irsp) + 6),
3249 *(((uint32_t *) irsp) + 7),
3250 *(((uint32_t *) irsp) + 8),
3251 *(((uint32_t *) irsp) + 9),
3252 *(((uint32_t *) irsp) + 10),
3253 *(((uint32_t *) irsp) + 11),
3254 *(((uint32_t *) irsp) + 12),
3255 *(((uint32_t *) irsp) + 13),
3256 *(((uint32_t *) irsp) + 14),
3257 *(((uint32_t *) irsp) + 15));
3258 }
3259
3260 /*
3261 * Fetch the IOCB command type and call the correct completion
3262 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3263 * get freed back to the lpfc_iocb_list by the discovery
3264 * kernel thread.
3265 */
3266 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3267 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3268 switch (type) {
3269 case LPFC_SOL_IOCB:
3270 spin_unlock_irqrestore(&phba->hbalock, iflag);
3271 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3272 spin_lock_irqsave(&phba->hbalock, iflag);
3273 break;
3274
3275 case LPFC_UNSOL_IOCB:
3276 spin_unlock_irqrestore(&phba->hbalock, iflag);
3277 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3278 spin_lock_irqsave(&phba->hbalock, iflag);
3279 if (!rc)
3280 free_saveq = 0;
3281 break;
3282
3283 case LPFC_ABORT_IOCB:
3284 cmdiocbp = NULL;
3285 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3286 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3287 saveq);
3288 if (cmdiocbp) {
3289 /* Call the specified completion routine */
3290 if (cmdiocbp->iocb_cmpl) {
3291 spin_unlock_irqrestore(&phba->hbalock,
3292 iflag);
3293 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3294 saveq);
3295 spin_lock_irqsave(&phba->hbalock,
3296 iflag);
3297 } else
3298 __lpfc_sli_release_iocbq(phba,
3299 cmdiocbp);
3300 }
3301 break;
3302
3303 case LPFC_UNKNOWN_IOCB:
3304 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3305 char adaptermsg[LPFC_MAX_ADPTMSG];
3306 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3307 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3308 MAX_MSG_DATA);
3309 dev_warn(&((phba->pcidev)->dev),
3310 "lpfc%d: %s\n",
3311 phba->brd_no, adaptermsg);
3312 } else {
3313 /* Unknown IOCB command */
3314 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3315 "0335 Unknown IOCB "
3316 "command Data: x%x "
3317 "x%x x%x x%x\n",
3318 irsp->ulpCommand,
3319 irsp->ulpStatus,
3320 irsp->ulpIoTag,
3321 irsp->ulpContext);
3322 }
3323 break;
3324 }
3325
3326 if (free_saveq) {
3327 list_for_each_entry_safe(rspiocbp, next_iocb,
3328 &saveq->list, list) {
3329 list_del_init(&rspiocbp->list);
3330 __lpfc_sli_release_iocbq(phba, rspiocbp);
3331 }
3332 __lpfc_sli_release_iocbq(phba, saveq);
3333 }
3334 rspiocbp = NULL;
3335 }
3336 spin_unlock_irqrestore(&phba->hbalock, iflag);
3337 return rspiocbp;
3338 }
3339
3340 /**
3341 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3342 * @phba: Pointer to HBA context object.
3343 * @pring: Pointer to driver SLI ring object.
3344 * @mask: Host attention register mask for this ring.
3345 *
3346 * This routine wraps the actual slow_ring event process routine from the
3347 * API jump table function pointer from the lpfc_hba struct.
3348 **/
3349 void
3350 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3351 struct lpfc_sli_ring *pring, uint32_t mask)
3352 {
3353 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3354 }
3355
3356 /**
3357 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3358 * @phba: Pointer to HBA context object.
3359 * @pring: Pointer to driver SLI ring object.
3360 * @mask: Host attention register mask for this ring.
3361 *
3362 * This function is called from the worker thread when there is a ring event
3363 * for non-fcp rings. The caller does not hold any lock. The function will
3364 * remove each response iocb in the response ring and calls the handle
3365 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3366 **/
3367 static void
3368 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3369 struct lpfc_sli_ring *pring, uint32_t mask)
3370 {
3371 struct lpfc_pgp *pgp;
3372 IOCB_t *entry;
3373 IOCB_t *irsp = NULL;
3374 struct lpfc_iocbq *rspiocbp = NULL;
3375 uint32_t portRspPut, portRspMax;
3376 unsigned long iflag;
3377 uint32_t status;
3378
3379 pgp = &phba->port_gp[pring->ringno];
3380 spin_lock_irqsave(&phba->hbalock, iflag);
3381 pring->stats.iocb_event++;
3382
3383 /*
3384 * The next available response entry should never exceed the maximum
3385 * entries. If it does, treat it as an adapter hardware error.
3386 */
3387 portRspMax = pring->sli.sli3.numRiocb;
3388 portRspPut = le32_to_cpu(pgp->rspPutInx);
3389 if (portRspPut >= portRspMax) {
3390 /*
3391 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3392 * rsp ring <portRspMax>
3393 */
3394 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3395 "0303 Ring %d handler: portRspPut %d "
3396 "is bigger than rsp ring %d\n",
3397 pring->ringno, portRspPut, portRspMax);
3398
3399 phba->link_state = LPFC_HBA_ERROR;
3400 spin_unlock_irqrestore(&phba->hbalock, iflag);
3401
3402 phba->work_hs = HS_FFER3;
3403 lpfc_handle_eratt(phba);
3404
3405 return;
3406 }
3407
3408 rmb();
3409 while (pring->sli.sli3.rspidx != portRspPut) {
3410 /*
3411 * Build a completion list and call the appropriate handler.
3412 * The process is to get the next available response iocb, get
3413 * a free iocb from the list, copy the response data into the
3414 * free iocb, insert to the continuation list, and update the
3415 * next response index to slim. This process makes response
3416 * iocb's in the ring available to DMA as fast as possible but
3417 * pays a penalty for a copy operation. Since the iocb is
3418 * only 32 bytes, this penalty is considered small relative to
3419 * the PCI reads for register values and a slim write. When
3420 * the ulpLe field is set, the entire Command has been
3421 * received.
3422 */
3423 entry = lpfc_resp_iocb(phba, pring);
3424
3425 phba->last_completion_time = jiffies;
3426 rspiocbp = __lpfc_sli_get_iocbq(phba);
3427 if (rspiocbp == NULL) {
3428 printk(KERN_ERR "%s: out of buffers! Failing "
3429 "completion.\n", __func__);
3430 break;
3431 }
3432
3433 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3434 phba->iocb_rsp_size);
3435 irsp = &rspiocbp->iocb;
3436
3437 if (++pring->sli.sli3.rspidx >= portRspMax)
3438 pring->sli.sli3.rspidx = 0;
3439
3440 if (pring->ringno == LPFC_ELS_RING) {
3441 lpfc_debugfs_slow_ring_trc(phba,
3442 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3443 *(((uint32_t *) irsp) + 4),
3444 *(((uint32_t *) irsp) + 6),
3445 *(((uint32_t *) irsp) + 7));
3446 }
3447
3448 writel(pring->sli.sli3.rspidx,
3449 &phba->host_gp[pring->ringno].rspGetInx);
3450
3451 spin_unlock_irqrestore(&phba->hbalock, iflag);
3452 /* Handle the response IOCB */
3453 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3454 spin_lock_irqsave(&phba->hbalock, iflag);
3455
3456 /*
3457 * If the port response put pointer has not been updated, sync
3458 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3459 * response put pointer.
3460 */
3461 if (pring->sli.sli3.rspidx == portRspPut) {
3462 portRspPut = le32_to_cpu(pgp->rspPutInx);
3463 }
3464 } /* while (pring->sli.sli3.rspidx != portRspPut) */
3465
3466 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3467 /* At least one response entry has been freed */
3468 pring->stats.iocb_rsp_full++;
3469 /* SET RxRE_RSP in Chip Att register */
3470 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3471 writel(status, phba->CAregaddr);
3472 readl(phba->CAregaddr); /* flush */
3473 }
3474 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3475 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3476 pring->stats.iocb_cmd_empty++;
3477
3478 /* Force update of the local copy of cmdGetInx */
3479 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3480 lpfc_sli_resume_iocb(phba, pring);
3481
3482 if ((pring->lpfc_sli_cmd_available))
3483 (pring->lpfc_sli_cmd_available) (phba, pring);
3484
3485 }
3486
3487 spin_unlock_irqrestore(&phba->hbalock, iflag);
3488 return;
3489 }
3490
3491 /**
3492 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3493 * @phba: Pointer to HBA context object.
3494 * @pring: Pointer to driver SLI ring object.
3495 * @mask: Host attention register mask for this ring.
3496 *
3497 * This function is called from the worker thread when there is a pending
3498 * ELS response iocb on the driver internal slow-path response iocb worker
3499 * queue. The caller does not hold any lock. The function will remove each
3500 * response iocb from the response worker queue and calls the handle
3501 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3502 **/
3503 static void
3504 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3505 struct lpfc_sli_ring *pring, uint32_t mask)
3506 {
3507 struct lpfc_iocbq *irspiocbq;
3508 struct hbq_dmabuf *dmabuf;
3509 struct lpfc_cq_event *cq_event;
3510 unsigned long iflag;
3511
3512 spin_lock_irqsave(&phba->hbalock, iflag);
3513 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3514 spin_unlock_irqrestore(&phba->hbalock, iflag);
3515 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3516 /* Get the response iocb from the head of work queue */
3517 spin_lock_irqsave(&phba->hbalock, iflag);
3518 list_remove_head(&phba->sli4_hba.sp_queue_event,
3519 cq_event, struct lpfc_cq_event, list);
3520 spin_unlock_irqrestore(&phba->hbalock, iflag);
3521
3522 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3523 case CQE_CODE_COMPL_WQE:
3524 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3525 cq_event);
3526 /* Translate ELS WCQE to response IOCBQ */
3527 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3528 irspiocbq);
3529 if (irspiocbq)
3530 lpfc_sli_sp_handle_rspiocb(phba, pring,
3531 irspiocbq);
3532 break;
3533 case CQE_CODE_RECEIVE:
3534 case CQE_CODE_RECEIVE_V1:
3535 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3536 cq_event);
3537 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3538 break;
3539 default:
3540 break;
3541 }
3542 }
3543 }
3544
3545 /**
3546 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3547 * @phba: Pointer to HBA context object.
3548 * @pring: Pointer to driver SLI ring object.
3549 *
3550 * This function aborts all iocbs in the given ring and frees all the iocb
3551 * objects in txq. This function issues an abort iocb for all the iocb commands
3552 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3553 * the return of this function. The caller is not required to hold any locks.
3554 **/
3555 void
3556 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3557 {
3558 LIST_HEAD(completions);
3559 struct lpfc_iocbq *iocb, *next_iocb;
3560
3561 if (pring->ringno == LPFC_ELS_RING) {
3562 lpfc_fabric_abort_hba(phba);
3563 }
3564
3565 /* Error everything on txq and txcmplq
3566 * First do the txq.
3567 */
3568 if (phba->sli_rev >= LPFC_SLI_REV4) {
3569 spin_lock_irq(&pring->ring_lock);
3570 list_splice_init(&pring->txq, &completions);
3571 pring->txq_cnt = 0;
3572 spin_unlock_irq(&pring->ring_lock);
3573
3574 spin_lock_irq(&phba->hbalock);
3575 /* Next issue ABTS for everything on the txcmplq */
3576 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3577 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3578 spin_unlock_irq(&phba->hbalock);
3579 } else {
3580 spin_lock_irq(&phba->hbalock);
3581 list_splice_init(&pring->txq, &completions);
3582 pring->txq_cnt = 0;
3583
3584 /* Next issue ABTS for everything on the txcmplq */
3585 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3586 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3587 spin_unlock_irq(&phba->hbalock);
3588 }
3589
3590 /* Cancel all the IOCBs from the completions list */
3591 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3592 IOERR_SLI_ABORTED);
3593 }
3594
3595 /**
3596 * lpfc_sli_abort_fcp_rings - Abort all iocbs in all FCP rings
3597 * @phba: Pointer to HBA context object.
3598 * @pring: Pointer to driver SLI ring object.
3599 *
3600 * This function aborts all iocbs in FCP rings and frees all the iocb
3601 * objects in txq. This function issues an abort iocb for all the iocb commands
3602 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3603 * the return of this function. The caller is not required to hold any locks.
3604 **/
3605 void
3606 lpfc_sli_abort_fcp_rings(struct lpfc_hba *phba)
3607 {
3608 struct lpfc_sli *psli = &phba->sli;
3609 struct lpfc_sli_ring *pring;
3610 uint32_t i;
3611
3612 /* Look on all the FCP Rings for the iotag */
3613 if (phba->sli_rev >= LPFC_SLI_REV4) {
3614 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3615 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3616 lpfc_sli_abort_iocb_ring(phba, pring);
3617 }
3618 } else {
3619 pring = &psli->ring[psli->fcp_ring];
3620 lpfc_sli_abort_iocb_ring(phba, pring);
3621 }
3622 }
3623
3624
3625 /**
3626 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3627 * @phba: Pointer to HBA context object.
3628 *
3629 * This function flushes all iocbs in the fcp ring and frees all the iocb
3630 * objects in txq and txcmplq. This function will not issue abort iocbs
3631 * for all the iocb commands in txcmplq, they will just be returned with
3632 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3633 * slot has been permanently disabled.
3634 **/
3635 void
3636 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3637 {
3638 LIST_HEAD(txq);
3639 LIST_HEAD(txcmplq);
3640 struct lpfc_sli *psli = &phba->sli;
3641 struct lpfc_sli_ring *pring;
3642 uint32_t i;
3643
3644 spin_lock_irq(&phba->hbalock);
3645 /* Indicate the I/O queues are flushed */
3646 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3647 spin_unlock_irq(&phba->hbalock);
3648
3649 /* Look on all the FCP Rings for the iotag */
3650 if (phba->sli_rev >= LPFC_SLI_REV4) {
3651 for (i = 0; i < phba->cfg_fcp_io_channel; i++) {
3652 pring = &psli->ring[i + MAX_SLI3_CONFIGURED_RINGS];
3653
3654 spin_lock_irq(&pring->ring_lock);
3655 /* Retrieve everything on txq */
3656 list_splice_init(&pring->txq, &txq);
3657 /* Retrieve everything on the txcmplq */
3658 list_splice_init(&pring->txcmplq, &txcmplq);
3659 pring->txq_cnt = 0;
3660 pring->txcmplq_cnt = 0;
3661 spin_unlock_irq(&pring->ring_lock);
3662
3663 /* Flush the txq */
3664 lpfc_sli_cancel_iocbs(phba, &txq,
3665 IOSTAT_LOCAL_REJECT,
3666 IOERR_SLI_DOWN);
3667 /* Flush the txcmpq */
3668 lpfc_sli_cancel_iocbs(phba, &txcmplq,
3669 IOSTAT_LOCAL_REJECT,
3670 IOERR_SLI_DOWN);
3671 }
3672 } else {
3673 pring = &psli->ring[psli->fcp_ring];
3674
3675 spin_lock_irq(&phba->hbalock);
3676 /* Retrieve everything on txq */
3677 list_splice_init(&pring->txq, &txq);
3678 /* Retrieve everything on the txcmplq */
3679 list_splice_init(&pring->txcmplq, &txcmplq);
3680 pring->txq_cnt = 0;
3681 pring->txcmplq_cnt = 0;
3682 spin_unlock_irq(&phba->hbalock);
3683
3684 /* Flush the txq */
3685 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3686 IOERR_SLI_DOWN);
3687 /* Flush the txcmpq */
3688 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3689 IOERR_SLI_DOWN);
3690 }
3691 }
3692
3693 /**
3694 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3695 * @phba: Pointer to HBA context object.
3696 * @mask: Bit mask to be checked.
3697 *
3698 * This function reads the host status register and compares
3699 * with the provided bit mask to check if HBA completed
3700 * the restart. This function will wait in a loop for the
3701 * HBA to complete restart. If the HBA does not restart within
3702 * 15 iterations, the function will reset the HBA again. The
3703 * function returns 1 when HBA fail to restart otherwise returns
3704 * zero.
3705 **/
3706 static int
3707 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3708 {
3709 uint32_t status;
3710 int i = 0;
3711 int retval = 0;
3712
3713 /* Read the HBA Host Status Register */
3714 if (lpfc_readl(phba->HSregaddr, &status))
3715 return 1;
3716
3717 /*
3718 * Check status register every 100ms for 5 retries, then every
3719 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3720 * every 2.5 sec for 4.
3721 * Break our of the loop if errors occurred during init.
3722 */
3723 while (((status & mask) != mask) &&
3724 !(status & HS_FFERM) &&
3725 i++ < 20) {
3726
3727 if (i <= 5)
3728 msleep(10);
3729 else if (i <= 10)
3730 msleep(500);
3731 else
3732 msleep(2500);
3733
3734 if (i == 15) {
3735 /* Do post */
3736 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3737 lpfc_sli_brdrestart(phba);
3738 }
3739 /* Read the HBA Host Status Register */
3740 if (lpfc_readl(phba->HSregaddr, &status)) {
3741 retval = 1;
3742 break;
3743 }
3744 }
3745
3746 /* Check to see if any errors occurred during init */
3747 if ((status & HS_FFERM) || (i >= 20)) {
3748 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3749 "2751 Adapter failed to restart, "
3750 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3751 status,
3752 readl(phba->MBslimaddr + 0xa8),
3753 readl(phba->MBslimaddr + 0xac));
3754 phba->link_state = LPFC_HBA_ERROR;
3755 retval = 1;
3756 }
3757
3758 return retval;
3759 }
3760
3761 /**
3762 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3763 * @phba: Pointer to HBA context object.
3764 * @mask: Bit mask to be checked.
3765 *
3766 * This function checks the host status register to check if HBA is
3767 * ready. This function will wait in a loop for the HBA to be ready
3768 * If the HBA is not ready , the function will will reset the HBA PCI
3769 * function again. The function returns 1 when HBA fail to be ready
3770 * otherwise returns zero.
3771 **/
3772 static int
3773 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3774 {
3775 uint32_t status;
3776 int retval = 0;
3777
3778 /* Read the HBA Host Status Register */
3779 status = lpfc_sli4_post_status_check(phba);
3780
3781 if (status) {
3782 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3783 lpfc_sli_brdrestart(phba);
3784 status = lpfc_sli4_post_status_check(phba);
3785 }
3786
3787 /* Check to see if any errors occurred during init */
3788 if (status) {
3789 phba->link_state = LPFC_HBA_ERROR;
3790 retval = 1;
3791 } else
3792 phba->sli4_hba.intr_enable = 0;
3793
3794 return retval;
3795 }
3796
3797 /**
3798 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3799 * @phba: Pointer to HBA context object.
3800 * @mask: Bit mask to be checked.
3801 *
3802 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3803 * from the API jump table function pointer from the lpfc_hba struct.
3804 **/
3805 int
3806 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3807 {
3808 return phba->lpfc_sli_brdready(phba, mask);
3809 }
3810
3811 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3812
3813 /**
3814 * lpfc_reset_barrier - Make HBA ready for HBA reset
3815 * @phba: Pointer to HBA context object.
3816 *
3817 * This function is called before resetting an HBA. This function is called
3818 * with hbalock held and requests HBA to quiesce DMAs before a reset.
3819 **/
3820 void lpfc_reset_barrier(struct lpfc_hba *phba)
3821 {
3822 uint32_t __iomem *resp_buf;
3823 uint32_t __iomem *mbox_buf;
3824 volatile uint32_t mbox;
3825 uint32_t hc_copy, ha_copy, resp_data;
3826 int i;
3827 uint8_t hdrtype;
3828
3829 lockdep_assert_held(&phba->hbalock);
3830
3831 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3832 if (hdrtype != 0x80 ||
3833 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3834 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3835 return;
3836
3837 /*
3838 * Tell the other part of the chip to suspend temporarily all
3839 * its DMA activity.
3840 */
3841 resp_buf = phba->MBslimaddr;
3842
3843 /* Disable the error attention */
3844 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3845 return;
3846 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3847 readl(phba->HCregaddr); /* flush */
3848 phba->link_flag |= LS_IGNORE_ERATT;
3849
3850 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3851 return;
3852 if (ha_copy & HA_ERATT) {
3853 /* Clear Chip error bit */
3854 writel(HA_ERATT, phba->HAregaddr);
3855 phba->pport->stopped = 1;
3856 }
3857
3858 mbox = 0;
3859 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3860 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3861
3862 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3863 mbox_buf = phba->MBslimaddr;
3864 writel(mbox, mbox_buf);
3865
3866 for (i = 0; i < 50; i++) {
3867 if (lpfc_readl((resp_buf + 1), &resp_data))
3868 return;
3869 if (resp_data != ~(BARRIER_TEST_PATTERN))
3870 mdelay(1);
3871 else
3872 break;
3873 }
3874 resp_data = 0;
3875 if (lpfc_readl((resp_buf + 1), &resp_data))
3876 return;
3877 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
3878 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3879 phba->pport->stopped)
3880 goto restore_hc;
3881 else
3882 goto clear_errat;
3883 }
3884
3885 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3886 resp_data = 0;
3887 for (i = 0; i < 500; i++) {
3888 if (lpfc_readl(resp_buf, &resp_data))
3889 return;
3890 if (resp_data != mbox)
3891 mdelay(1);
3892 else
3893 break;
3894 }
3895
3896 clear_errat:
3897
3898 while (++i < 500) {
3899 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3900 return;
3901 if (!(ha_copy & HA_ERATT))
3902 mdelay(1);
3903 else
3904 break;
3905 }
3906
3907 if (readl(phba->HAregaddr) & HA_ERATT) {
3908 writel(HA_ERATT, phba->HAregaddr);
3909 phba->pport->stopped = 1;
3910 }
3911
3912 restore_hc:
3913 phba->link_flag &= ~LS_IGNORE_ERATT;
3914 writel(hc_copy, phba->HCregaddr);
3915 readl(phba->HCregaddr); /* flush */
3916 }
3917
3918 /**
3919 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3920 * @phba: Pointer to HBA context object.
3921 *
3922 * This function issues a kill_board mailbox command and waits for
3923 * the error attention interrupt. This function is called for stopping
3924 * the firmware processing. The caller is not required to hold any
3925 * locks. This function calls lpfc_hba_down_post function to free
3926 * any pending commands after the kill. The function will return 1 when it
3927 * fails to kill the board else will return 0.
3928 **/
3929 int
3930 lpfc_sli_brdkill(struct lpfc_hba *phba)
3931 {
3932 struct lpfc_sli *psli;
3933 LPFC_MBOXQ_t *pmb;
3934 uint32_t status;
3935 uint32_t ha_copy;
3936 int retval;
3937 int i = 0;
3938
3939 psli = &phba->sli;
3940
3941 /* Kill HBA */
3942 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3943 "0329 Kill HBA Data: x%x x%x\n",
3944 phba->pport->port_state, psli->sli_flag);
3945
3946 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3947 if (!pmb)
3948 return 1;
3949
3950 /* Disable the error attention */
3951 spin_lock_irq(&phba->hbalock);
3952 if (lpfc_readl(phba->HCregaddr, &status)) {
3953 spin_unlock_irq(&phba->hbalock);
3954 mempool_free(pmb, phba->mbox_mem_pool);
3955 return 1;
3956 }
3957 status &= ~HC_ERINT_ENA;
3958 writel(status, phba->HCregaddr);
3959 readl(phba->HCregaddr); /* flush */
3960 phba->link_flag |= LS_IGNORE_ERATT;
3961 spin_unlock_irq(&phba->hbalock);
3962
3963 lpfc_kill_board(phba, pmb);
3964 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3965 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3966
3967 if (retval != MBX_SUCCESS) {
3968 if (retval != MBX_BUSY)
3969 mempool_free(pmb, phba->mbox_mem_pool);
3970 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3971 "2752 KILL_BOARD command failed retval %d\n",
3972 retval);
3973 spin_lock_irq(&phba->hbalock);
3974 phba->link_flag &= ~LS_IGNORE_ERATT;
3975 spin_unlock_irq(&phba->hbalock);
3976 return 1;
3977 }
3978
3979 spin_lock_irq(&phba->hbalock);
3980 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3981 spin_unlock_irq(&phba->hbalock);
3982
3983 mempool_free(pmb, phba->mbox_mem_pool);
3984
3985 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3986 * attention every 100ms for 3 seconds. If we don't get ERATT after
3987 * 3 seconds we still set HBA_ERROR state because the status of the
3988 * board is now undefined.
3989 */
3990 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3991 return 1;
3992 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3993 mdelay(100);
3994 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3995 return 1;
3996 }
3997
3998 del_timer_sync(&psli->mbox_tmo);
3999 if (ha_copy & HA_ERATT) {
4000 writel(HA_ERATT, phba->HAregaddr);
4001 phba->pport->stopped = 1;
4002 }
4003 spin_lock_irq(&phba->hbalock);
4004 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4005 psli->mbox_active = NULL;
4006 phba->link_flag &= ~LS_IGNORE_ERATT;
4007 spin_unlock_irq(&phba->hbalock);
4008
4009 lpfc_hba_down_post(phba);
4010 phba->link_state = LPFC_HBA_ERROR;
4011
4012 return ha_copy & HA_ERATT ? 0 : 1;
4013 }
4014
4015 /**
4016 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
4017 * @phba: Pointer to HBA context object.
4018 *
4019 * This function resets the HBA by writing HC_INITFF to the control
4020 * register. After the HBA resets, this function resets all the iocb ring
4021 * indices. This function disables PCI layer parity checking during
4022 * the reset.
4023 * This function returns 0 always.
4024 * The caller is not required to hold any locks.
4025 **/
4026 int
4027 lpfc_sli_brdreset(struct lpfc_hba *phba)
4028 {
4029 struct lpfc_sli *psli;
4030 struct lpfc_sli_ring *pring;
4031 uint16_t cfg_value;
4032 int i;
4033
4034 psli = &phba->sli;
4035
4036 /* Reset HBA */
4037 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4038 "0325 Reset HBA Data: x%x x%x\n",
4039 phba->pport->port_state, psli->sli_flag);
4040
4041 /* perform board reset */
4042 phba->fc_eventTag = 0;
4043 phba->link_events = 0;
4044 phba->pport->fc_myDID = 0;
4045 phba->pport->fc_prevDID = 0;
4046
4047 /* Turn off parity checking and serr during the physical reset */
4048 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4049 pci_write_config_word(phba->pcidev, PCI_COMMAND,
4050 (cfg_value &
4051 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4052
4053 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
4054
4055 /* Now toggle INITFF bit in the Host Control Register */
4056 writel(HC_INITFF, phba->HCregaddr);
4057 mdelay(1);
4058 readl(phba->HCregaddr); /* flush */
4059 writel(0, phba->HCregaddr);
4060 readl(phba->HCregaddr); /* flush */
4061
4062 /* Restore PCI cmd register */
4063 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4064
4065 /* Initialize relevant SLI info */
4066 for (i = 0; i < psli->num_rings; i++) {
4067 pring = &psli->ring[i];
4068 pring->flag = 0;
4069 pring->sli.sli3.rspidx = 0;
4070 pring->sli.sli3.next_cmdidx = 0;
4071 pring->sli.sli3.local_getidx = 0;
4072 pring->sli.sli3.cmdidx = 0;
4073 pring->missbufcnt = 0;
4074 }
4075
4076 phba->link_state = LPFC_WARM_START;
4077 return 0;
4078 }
4079
4080 /**
4081 * lpfc_sli4_brdreset - Reset a sli-4 HBA
4082 * @phba: Pointer to HBA context object.
4083 *
4084 * This function resets a SLI4 HBA. This function disables PCI layer parity
4085 * checking during resets the device. The caller is not required to hold
4086 * any locks.
4087 *
4088 * This function returns 0 always.
4089 **/
4090 int
4091 lpfc_sli4_brdreset(struct lpfc_hba *phba)
4092 {
4093 struct lpfc_sli *psli = &phba->sli;
4094 uint16_t cfg_value;
4095 int rc = 0;
4096
4097 /* Reset HBA */
4098 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4099 "0295 Reset HBA Data: x%x x%x x%x\n",
4100 phba->pport->port_state, psli->sli_flag,
4101 phba->hba_flag);
4102
4103 /* perform board reset */
4104 phba->fc_eventTag = 0;
4105 phba->link_events = 0;
4106 phba->pport->fc_myDID = 0;
4107 phba->pport->fc_prevDID = 0;
4108
4109 spin_lock_irq(&phba->hbalock);
4110 psli->sli_flag &= ~(LPFC_PROCESS_LA);
4111 phba->fcf.fcf_flag = 0;
4112 spin_unlock_irq(&phba->hbalock);
4113
4114 /* SLI4 INTF 2: if FW dump is being taken skip INIT_PORT */
4115 if (phba->hba_flag & HBA_FW_DUMP_OP) {
4116 phba->hba_flag &= ~HBA_FW_DUMP_OP;
4117 return rc;
4118 }
4119
4120 /* Now physically reset the device */
4121 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4122 "0389 Performing PCI function reset!\n");
4123
4124 /* Turn off parity checking and serr during the physical reset */
4125 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
4126 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
4127 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
4128
4129 /* Perform FCoE PCI function reset before freeing queue memory */
4130 rc = lpfc_pci_function_reset(phba);
4131 lpfc_sli4_queue_destroy(phba);
4132
4133 /* Restore PCI cmd register */
4134 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4135
4136 return rc;
4137 }
4138
4139 /**
4140 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4141 * @phba: Pointer to HBA context object.
4142 *
4143 * This function is called in the SLI initialization code path to
4144 * restart the HBA. The caller is not required to hold any lock.
4145 * This function writes MBX_RESTART mailbox command to the SLIM and
4146 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4147 * function to free any pending commands. The function enables
4148 * POST only during the first initialization. The function returns zero.
4149 * The function does not guarantee completion of MBX_RESTART mailbox
4150 * command before the return of this function.
4151 **/
4152 static int
4153 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4154 {
4155 MAILBOX_t *mb;
4156 struct lpfc_sli *psli;
4157 volatile uint32_t word0;
4158 void __iomem *to_slim;
4159 uint32_t hba_aer_enabled;
4160
4161 spin_lock_irq(&phba->hbalock);
4162
4163 /* Take PCIe device Advanced Error Reporting (AER) state */
4164 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4165
4166 psli = &phba->sli;
4167
4168 /* Restart HBA */
4169 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4170 "0337 Restart HBA Data: x%x x%x\n",
4171 phba->pport->port_state, psli->sli_flag);
4172
4173 word0 = 0;
4174 mb = (MAILBOX_t *) &word0;
4175 mb->mbxCommand = MBX_RESTART;
4176 mb->mbxHc = 1;
4177
4178 lpfc_reset_barrier(phba);
4179
4180 to_slim = phba->MBslimaddr;
4181 writel(*(uint32_t *) mb, to_slim);
4182 readl(to_slim); /* flush */
4183
4184 /* Only skip post after fc_ffinit is completed */
4185 if (phba->pport->port_state)
4186 word0 = 1; /* This is really setting up word1 */
4187 else
4188 word0 = 0; /* This is really setting up word1 */
4189 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4190 writel(*(uint32_t *) mb, to_slim);
4191 readl(to_slim); /* flush */
4192
4193 lpfc_sli_brdreset(phba);
4194 phba->pport->stopped = 0;
4195 phba->link_state = LPFC_INIT_START;
4196 phba->hba_flag = 0;
4197 spin_unlock_irq(&phba->hbalock);
4198
4199 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4200 psli->stats_start = get_seconds();
4201
4202 /* Give the INITFF and Post time to settle. */
4203 mdelay(100);
4204
4205 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4206 if (hba_aer_enabled)
4207 pci_disable_pcie_error_reporting(phba->pcidev);
4208
4209 lpfc_hba_down_post(phba);
4210
4211 return 0;
4212 }
4213
4214 /**
4215 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4216 * @phba: Pointer to HBA context object.
4217 *
4218 * This function is called in the SLI initialization code path to restart
4219 * a SLI4 HBA. The caller is not required to hold any lock.
4220 * At the end of the function, it calls lpfc_hba_down_post function to
4221 * free any pending commands.
4222 **/
4223 static int
4224 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4225 {
4226 struct lpfc_sli *psli = &phba->sli;
4227 uint32_t hba_aer_enabled;
4228 int rc;
4229
4230 /* Restart HBA */
4231 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4232 "0296 Restart HBA Data: x%x x%x\n",
4233 phba->pport->port_state, psli->sli_flag);
4234
4235 /* Take PCIe device Advanced Error Reporting (AER) state */
4236 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4237
4238 rc = lpfc_sli4_brdreset(phba);
4239
4240 spin_lock_irq(&phba->hbalock);
4241 phba->pport->stopped = 0;
4242 phba->link_state = LPFC_INIT_START;
4243 phba->hba_flag = 0;
4244 spin_unlock_irq(&phba->hbalock);
4245
4246 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4247 psli->stats_start = get_seconds();
4248
4249 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4250 if (hba_aer_enabled)
4251 pci_disable_pcie_error_reporting(phba->pcidev);
4252
4253 lpfc_hba_down_post(phba);
4254
4255 return rc;
4256 }
4257
4258 /**
4259 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4260 * @phba: Pointer to HBA context object.
4261 *
4262 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4263 * API jump table function pointer from the lpfc_hba struct.
4264 **/
4265 int
4266 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4267 {
4268 return phba->lpfc_sli_brdrestart(phba);
4269 }
4270
4271 /**
4272 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4273 * @phba: Pointer to HBA context object.
4274 *
4275 * This function is called after a HBA restart to wait for successful
4276 * restart of the HBA. Successful restart of the HBA is indicated by
4277 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4278 * iteration, the function will restart the HBA again. The function returns
4279 * zero if HBA successfully restarted else returns negative error code.
4280 **/
4281 static int
4282 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4283 {
4284 uint32_t status, i = 0;
4285
4286 /* Read the HBA Host Status Register */
4287 if (lpfc_readl(phba->HSregaddr, &status))
4288 return -EIO;
4289
4290 /* Check status register to see what current state is */
4291 i = 0;
4292 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4293
4294 /* Check every 10ms for 10 retries, then every 100ms for 90
4295 * retries, then every 1 sec for 50 retires for a total of
4296 * ~60 seconds before reset the board again and check every
4297 * 1 sec for 50 retries. The up to 60 seconds before the
4298 * board ready is required by the Falcon FIPS zeroization
4299 * complete, and any reset the board in between shall cause
4300 * restart of zeroization, further delay the board ready.
4301 */
4302 if (i++ >= 200) {
4303 /* Adapter failed to init, timeout, status reg
4304 <status> */
4305 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4306 "0436 Adapter failed to init, "
4307 "timeout, status reg x%x, "
4308 "FW Data: A8 x%x AC x%x\n", status,
4309 readl(phba->MBslimaddr + 0xa8),
4310 readl(phba->MBslimaddr + 0xac));
4311 phba->link_state = LPFC_HBA_ERROR;
4312 return -ETIMEDOUT;
4313 }
4314
4315 /* Check to see if any errors occurred during init */
4316 if (status & HS_FFERM) {
4317 /* ERROR: During chipset initialization */
4318 /* Adapter failed to init, chipset, status reg
4319 <status> */
4320 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4321 "0437 Adapter failed to init, "
4322 "chipset, status reg x%x, "
4323 "FW Data: A8 x%x AC x%x\n", status,
4324 readl(phba->MBslimaddr + 0xa8),
4325 readl(phba->MBslimaddr + 0xac));
4326 phba->link_state = LPFC_HBA_ERROR;
4327 return -EIO;
4328 }
4329
4330 if (i <= 10)
4331 msleep(10);
4332 else if (i <= 100)
4333 msleep(100);
4334 else
4335 msleep(1000);
4336
4337 if (i == 150) {
4338 /* Do post */
4339 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4340 lpfc_sli_brdrestart(phba);
4341 }
4342 /* Read the HBA Host Status Register */
4343 if (lpfc_readl(phba->HSregaddr, &status))
4344 return -EIO;
4345 }
4346
4347 /* Check to see if any errors occurred during init */
4348 if (status & HS_FFERM) {
4349 /* ERROR: During chipset initialization */
4350 /* Adapter failed to init, chipset, status reg <status> */
4351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4352 "0438 Adapter failed to init, chipset, "
4353 "status reg x%x, "
4354 "FW Data: A8 x%x AC x%x\n", status,
4355 readl(phba->MBslimaddr + 0xa8),
4356 readl(phba->MBslimaddr + 0xac));
4357 phba->link_state = LPFC_HBA_ERROR;
4358 return -EIO;
4359 }
4360
4361 /* Clear all interrupt enable conditions */
4362 writel(0, phba->HCregaddr);
4363 readl(phba->HCregaddr); /* flush */
4364
4365 /* setup host attn register */
4366 writel(0xffffffff, phba->HAregaddr);
4367 readl(phba->HAregaddr); /* flush */
4368 return 0;
4369 }
4370
4371 /**
4372 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4373 *
4374 * This function calculates and returns the number of HBQs required to be
4375 * configured.
4376 **/
4377 int
4378 lpfc_sli_hbq_count(void)
4379 {
4380 return ARRAY_SIZE(lpfc_hbq_defs);
4381 }
4382
4383 /**
4384 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4385 *
4386 * This function adds the number of hbq entries in every HBQ to get
4387 * the total number of hbq entries required for the HBA and returns
4388 * the total count.
4389 **/
4390 static int
4391 lpfc_sli_hbq_entry_count(void)
4392 {
4393 int hbq_count = lpfc_sli_hbq_count();
4394 int count = 0;
4395 int i;
4396
4397 for (i = 0; i < hbq_count; ++i)
4398 count += lpfc_hbq_defs[i]->entry_count;
4399 return count;
4400 }
4401
4402 /**
4403 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4404 *
4405 * This function calculates amount of memory required for all hbq entries
4406 * to be configured and returns the total memory required.
4407 **/
4408 int
4409 lpfc_sli_hbq_size(void)
4410 {
4411 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4412 }
4413
4414 /**
4415 * lpfc_sli_hbq_setup - configure and initialize HBQs
4416 * @phba: Pointer to HBA context object.
4417 *
4418 * This function is called during the SLI initialization to configure
4419 * all the HBQs and post buffers to the HBQ. The caller is not
4420 * required to hold any locks. This function will return zero if successful
4421 * else it will return negative error code.
4422 **/
4423 static int
4424 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4425 {
4426 int hbq_count = lpfc_sli_hbq_count();
4427 LPFC_MBOXQ_t *pmb;
4428 MAILBOX_t *pmbox;
4429 uint32_t hbqno;
4430 uint32_t hbq_entry_index;
4431
4432 /* Get a Mailbox buffer to setup mailbox
4433 * commands for HBA initialization
4434 */
4435 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4436
4437 if (!pmb)
4438 return -ENOMEM;
4439
4440 pmbox = &pmb->u.mb;
4441
4442 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4443 phba->link_state = LPFC_INIT_MBX_CMDS;
4444 phba->hbq_in_use = 1;
4445
4446 hbq_entry_index = 0;
4447 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4448 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4449 phba->hbqs[hbqno].hbqPutIdx = 0;
4450 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4451 phba->hbqs[hbqno].entry_count =
4452 lpfc_hbq_defs[hbqno]->entry_count;
4453 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4454 hbq_entry_index, pmb);
4455 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4456
4457 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4458 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4459 mbxStatus <status>, ring <num> */
4460
4461 lpfc_printf_log(phba, KERN_ERR,
4462 LOG_SLI | LOG_VPORT,
4463 "1805 Adapter failed to init. "
4464 "Data: x%x x%x x%x\n",
4465 pmbox->mbxCommand,
4466 pmbox->mbxStatus, hbqno);
4467
4468 phba->link_state = LPFC_HBA_ERROR;
4469 mempool_free(pmb, phba->mbox_mem_pool);
4470 return -ENXIO;
4471 }
4472 }
4473 phba->hbq_count = hbq_count;
4474
4475 mempool_free(pmb, phba->mbox_mem_pool);
4476
4477 /* Initially populate or replenish the HBQs */
4478 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4479 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4480 return 0;
4481 }
4482
4483 /**
4484 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4485 * @phba: Pointer to HBA context object.
4486 *
4487 * This function is called during the SLI initialization to configure
4488 * all the HBQs and post buffers to the HBQ. The caller is not
4489 * required to hold any locks. This function will return zero if successful
4490 * else it will return negative error code.
4491 **/
4492 static int
4493 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4494 {
4495 phba->hbq_in_use = 1;
4496 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4497 phba->hbq_count = 1;
4498 /* Initially populate or replenish the HBQs */
4499 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4500 return 0;
4501 }
4502
4503 /**
4504 * lpfc_sli_config_port - Issue config port mailbox command
4505 * @phba: Pointer to HBA context object.
4506 * @sli_mode: sli mode - 2/3
4507 *
4508 * This function is called by the sli intialization code path
4509 * to issue config_port mailbox command. This function restarts the
4510 * HBA firmware and issues a config_port mailbox command to configure
4511 * the SLI interface in the sli mode specified by sli_mode
4512 * variable. The caller is not required to hold any locks.
4513 * The function returns 0 if successful, else returns negative error
4514 * code.
4515 **/
4516 int
4517 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4518 {
4519 LPFC_MBOXQ_t *pmb;
4520 uint32_t resetcount = 0, rc = 0, done = 0;
4521
4522 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4523 if (!pmb) {
4524 phba->link_state = LPFC_HBA_ERROR;
4525 return -ENOMEM;
4526 }
4527
4528 phba->sli_rev = sli_mode;
4529 while (resetcount < 2 && !done) {
4530 spin_lock_irq(&phba->hbalock);
4531 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4532 spin_unlock_irq(&phba->hbalock);
4533 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4534 lpfc_sli_brdrestart(phba);
4535 rc = lpfc_sli_chipset_init(phba);
4536 if (rc)
4537 break;
4538
4539 spin_lock_irq(&phba->hbalock);
4540 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4541 spin_unlock_irq(&phba->hbalock);
4542 resetcount++;
4543
4544 /* Call pre CONFIG_PORT mailbox command initialization. A
4545 * value of 0 means the call was successful. Any other
4546 * nonzero value is a failure, but if ERESTART is returned,
4547 * the driver may reset the HBA and try again.
4548 */
4549 rc = lpfc_config_port_prep(phba);
4550 if (rc == -ERESTART) {
4551 phba->link_state = LPFC_LINK_UNKNOWN;
4552 continue;
4553 } else if (rc)
4554 break;
4555
4556 phba->link_state = LPFC_INIT_MBX_CMDS;
4557 lpfc_config_port(phba, pmb);
4558 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4559 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4560 LPFC_SLI3_HBQ_ENABLED |
4561 LPFC_SLI3_CRP_ENABLED |
4562 LPFC_SLI3_BG_ENABLED |
4563 LPFC_SLI3_DSS_ENABLED);
4564 if (rc != MBX_SUCCESS) {
4565 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4566 "0442 Adapter failed to init, mbxCmd x%x "
4567 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4568 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4569 spin_lock_irq(&phba->hbalock);
4570 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4571 spin_unlock_irq(&phba->hbalock);
4572 rc = -ENXIO;
4573 } else {
4574 /* Allow asynchronous mailbox command to go through */
4575 spin_lock_irq(&phba->hbalock);
4576 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4577 spin_unlock_irq(&phba->hbalock);
4578 done = 1;
4579
4580 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4581 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4582 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4583 "3110 Port did not grant ASABT\n");
4584 }
4585 }
4586 if (!done) {
4587 rc = -EINVAL;
4588 goto do_prep_failed;
4589 }
4590 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4591 if (!pmb->u.mb.un.varCfgPort.cMA) {
4592 rc = -ENXIO;
4593 goto do_prep_failed;
4594 }
4595 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4596 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4597 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4598 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4599 phba->max_vpi : phba->max_vports;
4600
4601 } else
4602 phba->max_vpi = 0;
4603 phba->fips_level = 0;
4604 phba->fips_spec_rev = 0;
4605 if (pmb->u.mb.un.varCfgPort.gdss) {
4606 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4607 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4608 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4609 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4610 "2850 Security Crypto Active. FIPS x%d "
4611 "(Spec Rev: x%d)",
4612 phba->fips_level, phba->fips_spec_rev);
4613 }
4614 if (pmb->u.mb.un.varCfgPort.sec_err) {
4615 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4616 "2856 Config Port Security Crypto "
4617 "Error: x%x ",
4618 pmb->u.mb.un.varCfgPort.sec_err);
4619 }
4620 if (pmb->u.mb.un.varCfgPort.gerbm)
4621 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4622 if (pmb->u.mb.un.varCfgPort.gcrp)
4623 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4624
4625 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4626 phba->port_gp = phba->mbox->us.s3_pgp.port;
4627
4628 if (phba->cfg_enable_bg) {
4629 if (pmb->u.mb.un.varCfgPort.gbg)
4630 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4631 else
4632 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4633 "0443 Adapter did not grant "
4634 "BlockGuard\n");
4635 }
4636 } else {
4637 phba->hbq_get = NULL;
4638 phba->port_gp = phba->mbox->us.s2.port;
4639 phba->max_vpi = 0;
4640 }
4641 do_prep_failed:
4642 mempool_free(pmb, phba->mbox_mem_pool);
4643 return rc;
4644 }
4645
4646
4647 /**
4648 * lpfc_sli_hba_setup - SLI intialization function
4649 * @phba: Pointer to HBA context object.
4650 *
4651 * This function is the main SLI intialization function. This function
4652 * is called by the HBA intialization code, HBA reset code and HBA
4653 * error attention handler code. Caller is not required to hold any
4654 * locks. This function issues config_port mailbox command to configure
4655 * the SLI, setup iocb rings and HBQ rings. In the end the function
4656 * calls the config_port_post function to issue init_link mailbox
4657 * command and to start the discovery. The function will return zero
4658 * if successful, else it will return negative error code.
4659 **/
4660 int
4661 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4662 {
4663 uint32_t rc;
4664 int mode = 3, i;
4665 int longs;
4666
4667 switch (phba->cfg_sli_mode) {
4668 case 2:
4669 if (phba->cfg_enable_npiv) {
4670 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4671 "1824 NPIV enabled: Override sli_mode "
4672 "parameter (%d) to auto (0).\n",
4673 phba->cfg_sli_mode);
4674 break;
4675 }
4676 mode = 2;
4677 break;
4678 case 0:
4679 case 3:
4680 break;
4681 default:
4682 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4683 "1819 Unrecognized sli_mode parameter: %d.\n",
4684 phba->cfg_sli_mode);
4685
4686 break;
4687 }
4688 phba->fcp_embed_io = 0; /* SLI4 FC support only */
4689
4690 rc = lpfc_sli_config_port(phba, mode);
4691
4692 if (rc && phba->cfg_sli_mode == 3)
4693 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4694 "1820 Unable to select SLI-3. "
4695 "Not supported by adapter.\n");
4696 if (rc && mode != 2)
4697 rc = lpfc_sli_config_port(phba, 2);
4698 else if (rc && mode == 2)
4699 rc = lpfc_sli_config_port(phba, 3);
4700 if (rc)
4701 goto lpfc_sli_hba_setup_error;
4702
4703 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4704 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4705 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4706 if (!rc) {
4707 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4708 "2709 This device supports "
4709 "Advanced Error Reporting (AER)\n");
4710 spin_lock_irq(&phba->hbalock);
4711 phba->hba_flag |= HBA_AER_ENABLED;
4712 spin_unlock_irq(&phba->hbalock);
4713 } else {
4714 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4715 "2708 This device does not support "
4716 "Advanced Error Reporting (AER): %d\n",
4717 rc);
4718 phba->cfg_aer_support = 0;
4719 }
4720 }
4721
4722 if (phba->sli_rev == 3) {
4723 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4724 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4725 } else {
4726 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4727 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4728 phba->sli3_options = 0;
4729 }
4730
4731 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4732 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4733 phba->sli_rev, phba->max_vpi);
4734 rc = lpfc_sli_ring_map(phba);
4735
4736 if (rc)
4737 goto lpfc_sli_hba_setup_error;
4738
4739 /* Initialize VPIs. */
4740 if (phba->sli_rev == LPFC_SLI_REV3) {
4741 /*
4742 * The VPI bitmask and physical ID array are allocated
4743 * and initialized once only - at driver load. A port
4744 * reset doesn't need to reinitialize this memory.
4745 */
4746 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4747 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4748 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4749 GFP_KERNEL);
4750 if (!phba->vpi_bmask) {
4751 rc = -ENOMEM;
4752 goto lpfc_sli_hba_setup_error;
4753 }
4754
4755 phba->vpi_ids = kzalloc(
4756 (phba->max_vpi+1) * sizeof(uint16_t),
4757 GFP_KERNEL);
4758 if (!phba->vpi_ids) {
4759 kfree(phba->vpi_bmask);
4760 rc = -ENOMEM;
4761 goto lpfc_sli_hba_setup_error;
4762 }
4763 for (i = 0; i < phba->max_vpi; i++)
4764 phba->vpi_ids[i] = i;
4765 }
4766 }
4767
4768 /* Init HBQs */
4769 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4770 rc = lpfc_sli_hbq_setup(phba);
4771 if (rc)
4772 goto lpfc_sli_hba_setup_error;
4773 }
4774 spin_lock_irq(&phba->hbalock);
4775 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4776 spin_unlock_irq(&phba->hbalock);
4777
4778 rc = lpfc_config_port_post(phba);
4779 if (rc)
4780 goto lpfc_sli_hba_setup_error;
4781
4782 return rc;
4783
4784 lpfc_sli_hba_setup_error:
4785 phba->link_state = LPFC_HBA_ERROR;
4786 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4787 "0445 Firmware initialization failed\n");
4788 return rc;
4789 }
4790
4791 /**
4792 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4793 * @phba: Pointer to HBA context object.
4794 * @mboxq: mailbox pointer.
4795 * This function issue a dump mailbox command to read config region
4796 * 23 and parse the records in the region and populate driver
4797 * data structure.
4798 **/
4799 static int
4800 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4801 {
4802 LPFC_MBOXQ_t *mboxq;
4803 struct lpfc_dmabuf *mp;
4804 struct lpfc_mqe *mqe;
4805 uint32_t data_length;
4806 int rc;
4807
4808 /* Program the default value of vlan_id and fc_map */
4809 phba->valid_vlan = 0;
4810 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4811 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4812 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4813
4814 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4815 if (!mboxq)
4816 return -ENOMEM;
4817
4818 mqe = &mboxq->u.mqe;
4819 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4820 rc = -ENOMEM;
4821 goto out_free_mboxq;
4822 }
4823
4824 mp = (struct lpfc_dmabuf *) mboxq->context1;
4825 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4826
4827 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4828 "(%d):2571 Mailbox cmd x%x Status x%x "
4829 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4830 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4831 "CQ: x%x x%x x%x x%x\n",
4832 mboxq->vport ? mboxq->vport->vpi : 0,
4833 bf_get(lpfc_mqe_command, mqe),
4834 bf_get(lpfc_mqe_status, mqe),
4835 mqe->un.mb_words[0], mqe->un.mb_words[1],
4836 mqe->un.mb_words[2], mqe->un.mb_words[3],
4837 mqe->un.mb_words[4], mqe->un.mb_words[5],
4838 mqe->un.mb_words[6], mqe->un.mb_words[7],
4839 mqe->un.mb_words[8], mqe->un.mb_words[9],
4840 mqe->un.mb_words[10], mqe->un.mb_words[11],
4841 mqe->un.mb_words[12], mqe->un.mb_words[13],
4842 mqe->un.mb_words[14], mqe->un.mb_words[15],
4843 mqe->un.mb_words[16], mqe->un.mb_words[50],
4844 mboxq->mcqe.word0,
4845 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4846 mboxq->mcqe.trailer);
4847
4848 if (rc) {
4849 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4850 kfree(mp);
4851 rc = -EIO;
4852 goto out_free_mboxq;
4853 }
4854 data_length = mqe->un.mb_words[5];
4855 if (data_length > DMP_RGN23_SIZE) {
4856 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4857 kfree(mp);
4858 rc = -EIO;
4859 goto out_free_mboxq;
4860 }
4861
4862 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4863 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4864 kfree(mp);
4865 rc = 0;
4866
4867 out_free_mboxq:
4868 mempool_free(mboxq, phba->mbox_mem_pool);
4869 return rc;
4870 }
4871
4872 /**
4873 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4874 * @phba: pointer to lpfc hba data structure.
4875 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4876 * @vpd: pointer to the memory to hold resulting port vpd data.
4877 * @vpd_size: On input, the number of bytes allocated to @vpd.
4878 * On output, the number of data bytes in @vpd.
4879 *
4880 * This routine executes a READ_REV SLI4 mailbox command. In
4881 * addition, this routine gets the port vpd data.
4882 *
4883 * Return codes
4884 * 0 - successful
4885 * -ENOMEM - could not allocated memory.
4886 **/
4887 static int
4888 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4889 uint8_t *vpd, uint32_t *vpd_size)
4890 {
4891 int rc = 0;
4892 uint32_t dma_size;
4893 struct lpfc_dmabuf *dmabuf;
4894 struct lpfc_mqe *mqe;
4895
4896 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4897 if (!dmabuf)
4898 return -ENOMEM;
4899
4900 /*
4901 * Get a DMA buffer for the vpd data resulting from the READ_REV
4902 * mailbox command.
4903 */
4904 dma_size = *vpd_size;
4905 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev, dma_size,
4906 &dmabuf->phys, GFP_KERNEL);
4907 if (!dmabuf->virt) {
4908 kfree(dmabuf);
4909 return -ENOMEM;
4910 }
4911
4912 /*
4913 * The SLI4 implementation of READ_REV conflicts at word1,
4914 * bits 31:16 and SLI4 adds vpd functionality not present
4915 * in SLI3. This code corrects the conflicts.
4916 */
4917 lpfc_read_rev(phba, mboxq);
4918 mqe = &mboxq->u.mqe;
4919 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4920 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4921 mqe->un.read_rev.word1 &= 0x0000FFFF;
4922 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4923 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4924
4925 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4926 if (rc) {
4927 dma_free_coherent(&phba->pcidev->dev, dma_size,
4928 dmabuf->virt, dmabuf->phys);
4929 kfree(dmabuf);
4930 return -EIO;
4931 }
4932
4933 /*
4934 * The available vpd length cannot be bigger than the
4935 * DMA buffer passed to the port. Catch the less than
4936 * case and update the caller's size.
4937 */
4938 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4939 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4940
4941 memcpy(vpd, dmabuf->virt, *vpd_size);
4942
4943 dma_free_coherent(&phba->pcidev->dev, dma_size,
4944 dmabuf->virt, dmabuf->phys);
4945 kfree(dmabuf);
4946 return 0;
4947 }
4948
4949 /**
4950 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4951 * @phba: pointer to lpfc hba data structure.
4952 *
4953 * This routine retrieves SLI4 device physical port name this PCI function
4954 * is attached to.
4955 *
4956 * Return codes
4957 * 0 - successful
4958 * otherwise - failed to retrieve physical port name
4959 **/
4960 static int
4961 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4962 {
4963 LPFC_MBOXQ_t *mboxq;
4964 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4965 struct lpfc_controller_attribute *cntl_attr;
4966 struct lpfc_mbx_get_port_name *get_port_name;
4967 void *virtaddr = NULL;
4968 uint32_t alloclen, reqlen;
4969 uint32_t shdr_status, shdr_add_status;
4970 union lpfc_sli4_cfg_shdr *shdr;
4971 char cport_name = 0;
4972 int rc;
4973
4974 /* We assume nothing at this point */
4975 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4976 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4977
4978 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4979 if (!mboxq)
4980 return -ENOMEM;
4981 /* obtain link type and link number via READ_CONFIG */
4982 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4983 lpfc_sli4_read_config(phba);
4984 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4985 goto retrieve_ppname;
4986
4987 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4988 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4989 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4990 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4991 LPFC_SLI4_MBX_NEMBED);
4992 if (alloclen < reqlen) {
4993 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4994 "3084 Allocated DMA memory size (%d) is "
4995 "less than the requested DMA memory size "
4996 "(%d)\n", alloclen, reqlen);
4997 rc = -ENOMEM;
4998 goto out_free_mboxq;
4999 }
5000 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5001 virtaddr = mboxq->sge_array->addr[0];
5002 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
5003 shdr = &mbx_cntl_attr->cfg_shdr;
5004 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5005 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5006 if (shdr_status || shdr_add_status || rc) {
5007 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5008 "3085 Mailbox x%x (x%x/x%x) failed, "
5009 "rc:x%x, status:x%x, add_status:x%x\n",
5010 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5011 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5012 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5013 rc, shdr_status, shdr_add_status);
5014 rc = -ENXIO;
5015 goto out_free_mboxq;
5016 }
5017 cntl_attr = &mbx_cntl_attr->cntl_attr;
5018 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
5019 phba->sli4_hba.lnk_info.lnk_tp =
5020 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
5021 phba->sli4_hba.lnk_info.lnk_no =
5022 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
5023 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5024 "3086 lnk_type:%d, lnk_numb:%d\n",
5025 phba->sli4_hba.lnk_info.lnk_tp,
5026 phba->sli4_hba.lnk_info.lnk_no);
5027
5028 retrieve_ppname:
5029 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
5030 LPFC_MBOX_OPCODE_GET_PORT_NAME,
5031 sizeof(struct lpfc_mbx_get_port_name) -
5032 sizeof(struct lpfc_sli4_cfg_mhdr),
5033 LPFC_SLI4_MBX_EMBED);
5034 get_port_name = &mboxq->u.mqe.un.get_port_name;
5035 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
5036 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
5037 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
5038 phba->sli4_hba.lnk_info.lnk_tp);
5039 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
5040 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
5041 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
5042 if (shdr_status || shdr_add_status || rc) {
5043 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
5044 "3087 Mailbox x%x (x%x/x%x) failed: "
5045 "rc:x%x, status:x%x, add_status:x%x\n",
5046 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
5047 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
5048 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
5049 rc, shdr_status, shdr_add_status);
5050 rc = -ENXIO;
5051 goto out_free_mboxq;
5052 }
5053 switch (phba->sli4_hba.lnk_info.lnk_no) {
5054 case LPFC_LINK_NUMBER_0:
5055 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
5056 &get_port_name->u.response);
5057 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5058 break;
5059 case LPFC_LINK_NUMBER_1:
5060 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
5061 &get_port_name->u.response);
5062 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5063 break;
5064 case LPFC_LINK_NUMBER_2:
5065 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
5066 &get_port_name->u.response);
5067 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5068 break;
5069 case LPFC_LINK_NUMBER_3:
5070 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
5071 &get_port_name->u.response);
5072 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
5073 break;
5074 default:
5075 break;
5076 }
5077
5078 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
5079 phba->Port[0] = cport_name;
5080 phba->Port[1] = '\0';
5081 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5082 "3091 SLI get port name: %s\n", phba->Port);
5083 }
5084
5085 out_free_mboxq:
5086 if (rc != MBX_TIMEOUT) {
5087 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
5088 lpfc_sli4_mbox_cmd_free(phba, mboxq);
5089 else
5090 mempool_free(mboxq, phba->mbox_mem_pool);
5091 }
5092 return rc;
5093 }
5094
5095 /**
5096 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
5097 * @phba: pointer to lpfc hba data structure.
5098 *
5099 * This routine is called to explicitly arm the SLI4 device's completion and
5100 * event queues
5101 **/
5102 static void
5103 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
5104 {
5105 int fcp_eqidx;
5106
5107 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
5108 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
5109 fcp_eqidx = 0;
5110 if (phba->sli4_hba.fcp_cq) {
5111 do {
5112 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
5113 LPFC_QUEUE_REARM);
5114 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
5115 }
5116
5117 if (phba->cfg_fof)
5118 lpfc_sli4_cq_release(phba->sli4_hba.oas_cq, LPFC_QUEUE_REARM);
5119
5120 if (phba->sli4_hba.hba_eq) {
5121 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
5122 fcp_eqidx++)
5123 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
5124 LPFC_QUEUE_REARM);
5125 }
5126
5127 if (phba->cfg_fof)
5128 lpfc_sli4_eq_release(phba->sli4_hba.fof_eq, LPFC_QUEUE_REARM);
5129 }
5130
5131 /**
5132 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
5133 * @phba: Pointer to HBA context object.
5134 * @type: The resource extent type.
5135 * @extnt_count: buffer to hold port available extent count.
5136 * @extnt_size: buffer to hold element count per extent.
5137 *
5138 * This function calls the port and retrievs the number of available
5139 * extents and their size for a particular extent type.
5140 *
5141 * Returns: 0 if successful. Nonzero otherwise.
5142 **/
5143 int
5144 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5145 uint16_t *extnt_count, uint16_t *extnt_size)
5146 {
5147 int rc = 0;
5148 uint32_t length;
5149 uint32_t mbox_tmo;
5150 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5151 LPFC_MBOXQ_t *mbox;
5152
5153 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5154 if (!mbox)
5155 return -ENOMEM;
5156
5157 /* Find out how many extents are available for this resource type */
5158 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5159 sizeof(struct lpfc_sli4_cfg_mhdr));
5160 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5161 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5162 length, LPFC_SLI4_MBX_EMBED);
5163
5164 /* Send an extents count of 0 - the GET doesn't use it. */
5165 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5166 LPFC_SLI4_MBX_EMBED);
5167 if (unlikely(rc)) {
5168 rc = -EIO;
5169 goto err_exit;
5170 }
5171
5172 if (!phba->sli4_hba.intr_enable)
5173 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5174 else {
5175 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5176 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5177 }
5178 if (unlikely(rc)) {
5179 rc = -EIO;
5180 goto err_exit;
5181 }
5182
5183 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5184 if (bf_get(lpfc_mbox_hdr_status,
5185 &rsrc_info->header.cfg_shdr.response)) {
5186 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5187 "2930 Failed to get resource extents "
5188 "Status 0x%x Add'l Status 0x%x\n",
5189 bf_get(lpfc_mbox_hdr_status,
5190 &rsrc_info->header.cfg_shdr.response),
5191 bf_get(lpfc_mbox_hdr_add_status,
5192 &rsrc_info->header.cfg_shdr.response));
5193 rc = -EIO;
5194 goto err_exit;
5195 }
5196
5197 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5198 &rsrc_info->u.rsp);
5199 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5200 &rsrc_info->u.rsp);
5201
5202 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5203 "3162 Retrieved extents type-%d from port: count:%d, "
5204 "size:%d\n", type, *extnt_count, *extnt_size);
5205
5206 err_exit:
5207 mempool_free(mbox, phba->mbox_mem_pool);
5208 return rc;
5209 }
5210
5211 /**
5212 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5213 * @phba: Pointer to HBA context object.
5214 * @type: The extent type to check.
5215 *
5216 * This function reads the current available extents from the port and checks
5217 * if the extent count or extent size has changed since the last access.
5218 * Callers use this routine post port reset to understand if there is a
5219 * extent reprovisioning requirement.
5220 *
5221 * Returns:
5222 * -Error: error indicates problem.
5223 * 1: Extent count or size has changed.
5224 * 0: No changes.
5225 **/
5226 static int
5227 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5228 {
5229 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5230 uint16_t size_diff, rsrc_ext_size;
5231 int rc = 0;
5232 struct lpfc_rsrc_blks *rsrc_entry;
5233 struct list_head *rsrc_blk_list = NULL;
5234
5235 size_diff = 0;
5236 curr_ext_cnt = 0;
5237 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5238 &rsrc_ext_cnt,
5239 &rsrc_ext_size);
5240 if (unlikely(rc))
5241 return -EIO;
5242
5243 switch (type) {
5244 case LPFC_RSC_TYPE_FCOE_RPI:
5245 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5246 break;
5247 case LPFC_RSC_TYPE_FCOE_VPI:
5248 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5249 break;
5250 case LPFC_RSC_TYPE_FCOE_XRI:
5251 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5252 break;
5253 case LPFC_RSC_TYPE_FCOE_VFI:
5254 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5255 break;
5256 default:
5257 break;
5258 }
5259
5260 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5261 curr_ext_cnt++;
5262 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5263 size_diff++;
5264 }
5265
5266 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5267 rc = 1;
5268
5269 return rc;
5270 }
5271
5272 /**
5273 * lpfc_sli4_cfg_post_extnts -
5274 * @phba: Pointer to HBA context object.
5275 * @extnt_cnt - number of available extents.
5276 * @type - the extent type (rpi, xri, vfi, vpi).
5277 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5278 * @mbox - pointer to the caller's allocated mailbox structure.
5279 *
5280 * This function executes the extents allocation request. It also
5281 * takes care of the amount of memory needed to allocate or get the
5282 * allocated extents. It is the caller's responsibility to evaluate
5283 * the response.
5284 *
5285 * Returns:
5286 * -Error: Error value describes the condition found.
5287 * 0: if successful
5288 **/
5289 static int
5290 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5291 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5292 {
5293 int rc = 0;
5294 uint32_t req_len;
5295 uint32_t emb_len;
5296 uint32_t alloc_len, mbox_tmo;
5297
5298 /* Calculate the total requested length of the dma memory */
5299 req_len = extnt_cnt * sizeof(uint16_t);
5300
5301 /*
5302 * Calculate the size of an embedded mailbox. The uint32_t
5303 * accounts for extents-specific word.
5304 */
5305 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5306 sizeof(uint32_t);
5307
5308 /*
5309 * Presume the allocation and response will fit into an embedded
5310 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5311 */
5312 *emb = LPFC_SLI4_MBX_EMBED;
5313 if (req_len > emb_len) {
5314 req_len = extnt_cnt * sizeof(uint16_t) +
5315 sizeof(union lpfc_sli4_cfg_shdr) +
5316 sizeof(uint32_t);
5317 *emb = LPFC_SLI4_MBX_NEMBED;
5318 }
5319
5320 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5321 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5322 req_len, *emb);
5323 if (alloc_len < req_len) {
5324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5325 "2982 Allocated DMA memory size (x%x) is "
5326 "less than the requested DMA memory "
5327 "size (x%x)\n", alloc_len, req_len);
5328 return -ENOMEM;
5329 }
5330 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5331 if (unlikely(rc))
5332 return -EIO;
5333
5334 if (!phba->sli4_hba.intr_enable)
5335 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5336 else {
5337 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5338 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5339 }
5340
5341 if (unlikely(rc))
5342 rc = -EIO;
5343 return rc;
5344 }
5345
5346 /**
5347 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5348 * @phba: Pointer to HBA context object.
5349 * @type: The resource extent type to allocate.
5350 *
5351 * This function allocates the number of elements for the specified
5352 * resource type.
5353 **/
5354 static int
5355 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5356 {
5357 bool emb = false;
5358 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5359 uint16_t rsrc_id, rsrc_start, j, k;
5360 uint16_t *ids;
5361 int i, rc;
5362 unsigned long longs;
5363 unsigned long *bmask;
5364 struct lpfc_rsrc_blks *rsrc_blks;
5365 LPFC_MBOXQ_t *mbox;
5366 uint32_t length;
5367 struct lpfc_id_range *id_array = NULL;
5368 void *virtaddr = NULL;
5369 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5370 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5371 struct list_head *ext_blk_list;
5372
5373 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5374 &rsrc_cnt,
5375 &rsrc_size);
5376 if (unlikely(rc))
5377 return -EIO;
5378
5379 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5380 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5381 "3009 No available Resource Extents "
5382 "for resource type 0x%x: Count: 0x%x, "
5383 "Size 0x%x\n", type, rsrc_cnt,
5384 rsrc_size);
5385 return -ENOMEM;
5386 }
5387
5388 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5389 "2903 Post resource extents type-0x%x: "
5390 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5391
5392 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5393 if (!mbox)
5394 return -ENOMEM;
5395
5396 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5397 if (unlikely(rc)) {
5398 rc = -EIO;
5399 goto err_exit;
5400 }
5401
5402 /*
5403 * Figure out where the response is located. Then get local pointers
5404 * to the response data. The port does not guarantee to respond to
5405 * all extents counts request so update the local variable with the
5406 * allocated count from the port.
5407 */
5408 if (emb == LPFC_SLI4_MBX_EMBED) {
5409 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5410 id_array = &rsrc_ext->u.rsp.id[0];
5411 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5412 } else {
5413 virtaddr = mbox->sge_array->addr[0];
5414 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5415 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5416 id_array = &n_rsrc->id;
5417 }
5418
5419 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5420 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5421
5422 /*
5423 * Based on the resource size and count, correct the base and max
5424 * resource values.
5425 */
5426 length = sizeof(struct lpfc_rsrc_blks);
5427 switch (type) {
5428 case LPFC_RSC_TYPE_FCOE_RPI:
5429 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5430 sizeof(unsigned long),
5431 GFP_KERNEL);
5432 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5433 rc = -ENOMEM;
5434 goto err_exit;
5435 }
5436 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5437 sizeof(uint16_t),
5438 GFP_KERNEL);
5439 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5440 kfree(phba->sli4_hba.rpi_bmask);
5441 rc = -ENOMEM;
5442 goto err_exit;
5443 }
5444
5445 /*
5446 * The next_rpi was initialized with the maximum available
5447 * count but the port may allocate a smaller number. Catch
5448 * that case and update the next_rpi.
5449 */
5450 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5451
5452 /* Initialize local ptrs for common extent processing later. */
5453 bmask = phba->sli4_hba.rpi_bmask;
5454 ids = phba->sli4_hba.rpi_ids;
5455 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5456 break;
5457 case LPFC_RSC_TYPE_FCOE_VPI:
5458 phba->vpi_bmask = kzalloc(longs *
5459 sizeof(unsigned long),
5460 GFP_KERNEL);
5461 if (unlikely(!phba->vpi_bmask)) {
5462 rc = -ENOMEM;
5463 goto err_exit;
5464 }
5465 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5466 sizeof(uint16_t),
5467 GFP_KERNEL);
5468 if (unlikely(!phba->vpi_ids)) {
5469 kfree(phba->vpi_bmask);
5470 rc = -ENOMEM;
5471 goto err_exit;
5472 }
5473
5474 /* Initialize local ptrs for common extent processing later. */
5475 bmask = phba->vpi_bmask;
5476 ids = phba->vpi_ids;
5477 ext_blk_list = &phba->lpfc_vpi_blk_list;
5478 break;
5479 case LPFC_RSC_TYPE_FCOE_XRI:
5480 phba->sli4_hba.xri_bmask = kzalloc(longs *
5481 sizeof(unsigned long),
5482 GFP_KERNEL);
5483 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5484 rc = -ENOMEM;
5485 goto err_exit;
5486 }
5487 phba->sli4_hba.max_cfg_param.xri_used = 0;
5488 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5489 sizeof(uint16_t),
5490 GFP_KERNEL);
5491 if (unlikely(!phba->sli4_hba.xri_ids)) {
5492 kfree(phba->sli4_hba.xri_bmask);
5493 rc = -ENOMEM;
5494 goto err_exit;
5495 }
5496
5497 /* Initialize local ptrs for common extent processing later. */
5498 bmask = phba->sli4_hba.xri_bmask;
5499 ids = phba->sli4_hba.xri_ids;
5500 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5501 break;
5502 case LPFC_RSC_TYPE_FCOE_VFI:
5503 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5504 sizeof(unsigned long),
5505 GFP_KERNEL);
5506 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5507 rc = -ENOMEM;
5508 goto err_exit;
5509 }
5510 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5511 sizeof(uint16_t),
5512 GFP_KERNEL);
5513 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5514 kfree(phba->sli4_hba.vfi_bmask);
5515 rc = -ENOMEM;
5516 goto err_exit;
5517 }
5518
5519 /* Initialize local ptrs for common extent processing later. */
5520 bmask = phba->sli4_hba.vfi_bmask;
5521 ids = phba->sli4_hba.vfi_ids;
5522 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5523 break;
5524 default:
5525 /* Unsupported Opcode. Fail call. */
5526 id_array = NULL;
5527 bmask = NULL;
5528 ids = NULL;
5529 ext_blk_list = NULL;
5530 goto err_exit;
5531 }
5532
5533 /*
5534 * Complete initializing the extent configuration with the
5535 * allocated ids assigned to this function. The bitmask serves
5536 * as an index into the array and manages the available ids. The
5537 * array just stores the ids communicated to the port via the wqes.
5538 */
5539 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5540 if ((i % 2) == 0)
5541 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5542 &id_array[k]);
5543 else
5544 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5545 &id_array[k]);
5546
5547 rsrc_blks = kzalloc(length, GFP_KERNEL);
5548 if (unlikely(!rsrc_blks)) {
5549 rc = -ENOMEM;
5550 kfree(bmask);
5551 kfree(ids);
5552 goto err_exit;
5553 }
5554 rsrc_blks->rsrc_start = rsrc_id;
5555 rsrc_blks->rsrc_size = rsrc_size;
5556 list_add_tail(&rsrc_blks->list, ext_blk_list);
5557 rsrc_start = rsrc_id;
5558 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5559 phba->sli4_hba.scsi_xri_start = rsrc_start +
5560 lpfc_sli4_get_els_iocb_cnt(phba);
5561
5562 while (rsrc_id < (rsrc_start + rsrc_size)) {
5563 ids[j] = rsrc_id;
5564 rsrc_id++;
5565 j++;
5566 }
5567 /* Entire word processed. Get next word.*/
5568 if ((i % 2) == 1)
5569 k++;
5570 }
5571 err_exit:
5572 lpfc_sli4_mbox_cmd_free(phba, mbox);
5573 return rc;
5574 }
5575
5576 /**
5577 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5578 * @phba: Pointer to HBA context object.
5579 * @type: the extent's type.
5580 *
5581 * This function deallocates all extents of a particular resource type.
5582 * SLI4 does not allow for deallocating a particular extent range. It
5583 * is the caller's responsibility to release all kernel memory resources.
5584 **/
5585 static int
5586 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5587 {
5588 int rc;
5589 uint32_t length, mbox_tmo = 0;
5590 LPFC_MBOXQ_t *mbox;
5591 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5592 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5593
5594 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5595 if (!mbox)
5596 return -ENOMEM;
5597
5598 /*
5599 * This function sends an embedded mailbox because it only sends the
5600 * the resource type. All extents of this type are released by the
5601 * port.
5602 */
5603 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5604 sizeof(struct lpfc_sli4_cfg_mhdr));
5605 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5606 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5607 length, LPFC_SLI4_MBX_EMBED);
5608
5609 /* Send an extents count of 0 - the dealloc doesn't use it. */
5610 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5611 LPFC_SLI4_MBX_EMBED);
5612 if (unlikely(rc)) {
5613 rc = -EIO;
5614 goto out_free_mbox;
5615 }
5616 if (!phba->sli4_hba.intr_enable)
5617 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5618 else {
5619 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5620 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5621 }
5622 if (unlikely(rc)) {
5623 rc = -EIO;
5624 goto out_free_mbox;
5625 }
5626
5627 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5628 if (bf_get(lpfc_mbox_hdr_status,
5629 &dealloc_rsrc->header.cfg_shdr.response)) {
5630 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5631 "2919 Failed to release resource extents "
5632 "for type %d - Status 0x%x Add'l Status 0x%x. "
5633 "Resource memory not released.\n",
5634 type,
5635 bf_get(lpfc_mbox_hdr_status,
5636 &dealloc_rsrc->header.cfg_shdr.response),
5637 bf_get(lpfc_mbox_hdr_add_status,
5638 &dealloc_rsrc->header.cfg_shdr.response));
5639 rc = -EIO;
5640 goto out_free_mbox;
5641 }
5642
5643 /* Release kernel memory resources for the specific type. */
5644 switch (type) {
5645 case LPFC_RSC_TYPE_FCOE_VPI:
5646 kfree(phba->vpi_bmask);
5647 kfree(phba->vpi_ids);
5648 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5649 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5650 &phba->lpfc_vpi_blk_list, list) {
5651 list_del_init(&rsrc_blk->list);
5652 kfree(rsrc_blk);
5653 }
5654 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5655 break;
5656 case LPFC_RSC_TYPE_FCOE_XRI:
5657 kfree(phba->sli4_hba.xri_bmask);
5658 kfree(phba->sli4_hba.xri_ids);
5659 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5660 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5661 list_del_init(&rsrc_blk->list);
5662 kfree(rsrc_blk);
5663 }
5664 break;
5665 case LPFC_RSC_TYPE_FCOE_VFI:
5666 kfree(phba->sli4_hba.vfi_bmask);
5667 kfree(phba->sli4_hba.vfi_ids);
5668 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5669 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5670 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5671 list_del_init(&rsrc_blk->list);
5672 kfree(rsrc_blk);
5673 }
5674 break;
5675 case LPFC_RSC_TYPE_FCOE_RPI:
5676 /* RPI bitmask and physical id array are cleaned up earlier. */
5677 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5678 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5679 list_del_init(&rsrc_blk->list);
5680 kfree(rsrc_blk);
5681 }
5682 break;
5683 default:
5684 break;
5685 }
5686
5687 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5688
5689 out_free_mbox:
5690 mempool_free(mbox, phba->mbox_mem_pool);
5691 return rc;
5692 }
5693
5694 static void
5695 lpfc_set_features(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox,
5696 uint32_t feature)
5697 {
5698 uint32_t len;
5699
5700 len = sizeof(struct lpfc_mbx_set_feature) -
5701 sizeof(struct lpfc_sli4_cfg_mhdr);
5702 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5703 LPFC_MBOX_OPCODE_SET_FEATURES, len,
5704 LPFC_SLI4_MBX_EMBED);
5705
5706 switch (feature) {
5707 case LPFC_SET_UE_RECOVERY:
5708 bf_set(lpfc_mbx_set_feature_UER,
5709 &mbox->u.mqe.un.set_feature, 1);
5710 mbox->u.mqe.un.set_feature.feature = LPFC_SET_UE_RECOVERY;
5711 mbox->u.mqe.un.set_feature.param_len = 8;
5712 break;
5713 case LPFC_SET_MDS_DIAGS:
5714 bf_set(lpfc_mbx_set_feature_mds,
5715 &mbox->u.mqe.un.set_feature, 1);
5716 bf_set(lpfc_mbx_set_feature_mds_deep_loopbk,
5717 &mbox->u.mqe.un.set_feature, 0);
5718 mbox->u.mqe.un.set_feature.feature = LPFC_SET_MDS_DIAGS;
5719 mbox->u.mqe.un.set_feature.param_len = 8;
5720 break;
5721 }
5722
5723 return;
5724 }
5725
5726 /**
5727 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5728 * @phba: Pointer to HBA context object.
5729 *
5730 * This function allocates all SLI4 resource identifiers.
5731 **/
5732 int
5733 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5734 {
5735 int i, rc, error = 0;
5736 uint16_t count, base;
5737 unsigned long longs;
5738
5739 if (!phba->sli4_hba.rpi_hdrs_in_use)
5740 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5741 if (phba->sli4_hba.extents_in_use) {
5742 /*
5743 * The port supports resource extents. The XRI, VPI, VFI, RPI
5744 * resource extent count must be read and allocated before
5745 * provisioning the resource id arrays.
5746 */
5747 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5748 LPFC_IDX_RSRC_RDY) {
5749 /*
5750 * Extent-based resources are set - the driver could
5751 * be in a port reset. Figure out if any corrective
5752 * actions need to be taken.
5753 */
5754 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5755 LPFC_RSC_TYPE_FCOE_VFI);
5756 if (rc != 0)
5757 error++;
5758 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5759 LPFC_RSC_TYPE_FCOE_VPI);
5760 if (rc != 0)
5761 error++;
5762 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5763 LPFC_RSC_TYPE_FCOE_XRI);
5764 if (rc != 0)
5765 error++;
5766 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5767 LPFC_RSC_TYPE_FCOE_RPI);
5768 if (rc != 0)
5769 error++;
5770
5771 /*
5772 * It's possible that the number of resources
5773 * provided to this port instance changed between
5774 * resets. Detect this condition and reallocate
5775 * resources. Otherwise, there is no action.
5776 */
5777 if (error) {
5778 lpfc_printf_log(phba, KERN_INFO,
5779 LOG_MBOX | LOG_INIT,
5780 "2931 Detected extent resource "
5781 "change. Reallocating all "
5782 "extents.\n");
5783 rc = lpfc_sli4_dealloc_extent(phba,
5784 LPFC_RSC_TYPE_FCOE_VFI);
5785 rc = lpfc_sli4_dealloc_extent(phba,
5786 LPFC_RSC_TYPE_FCOE_VPI);
5787 rc = lpfc_sli4_dealloc_extent(phba,
5788 LPFC_RSC_TYPE_FCOE_XRI);
5789 rc = lpfc_sli4_dealloc_extent(phba,
5790 LPFC_RSC_TYPE_FCOE_RPI);
5791 } else
5792 return 0;
5793 }
5794
5795 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5796 if (unlikely(rc))
5797 goto err_exit;
5798
5799 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5800 if (unlikely(rc))
5801 goto err_exit;
5802
5803 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5804 if (unlikely(rc))
5805 goto err_exit;
5806
5807 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5808 if (unlikely(rc))
5809 goto err_exit;
5810 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5811 LPFC_IDX_RSRC_RDY);
5812 return rc;
5813 } else {
5814 /*
5815 * The port does not support resource extents. The XRI, VPI,
5816 * VFI, RPI resource ids were determined from READ_CONFIG.
5817 * Just allocate the bitmasks and provision the resource id
5818 * arrays. If a port reset is active, the resources don't
5819 * need any action - just exit.
5820 */
5821 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5822 LPFC_IDX_RSRC_RDY) {
5823 lpfc_sli4_dealloc_resource_identifiers(phba);
5824 lpfc_sli4_remove_rpis(phba);
5825 }
5826 /* RPIs. */
5827 count = phba->sli4_hba.max_cfg_param.max_rpi;
5828 if (count <= 0) {
5829 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5830 "3279 Invalid provisioning of "
5831 "rpi:%d\n", count);
5832 rc = -EINVAL;
5833 goto err_exit;
5834 }
5835 base = phba->sli4_hba.max_cfg_param.rpi_base;
5836 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5837 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5838 sizeof(unsigned long),
5839 GFP_KERNEL);
5840 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5841 rc = -ENOMEM;
5842 goto err_exit;
5843 }
5844 phba->sli4_hba.rpi_ids = kzalloc(count *
5845 sizeof(uint16_t),
5846 GFP_KERNEL);
5847 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5848 rc = -ENOMEM;
5849 goto free_rpi_bmask;
5850 }
5851
5852 for (i = 0; i < count; i++)
5853 phba->sli4_hba.rpi_ids[i] = base + i;
5854
5855 /* VPIs. */
5856 count = phba->sli4_hba.max_cfg_param.max_vpi;
5857 if (count <= 0) {
5858 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5859 "3280 Invalid provisioning of "
5860 "vpi:%d\n", count);
5861 rc = -EINVAL;
5862 goto free_rpi_ids;
5863 }
5864 base = phba->sli4_hba.max_cfg_param.vpi_base;
5865 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5866 phba->vpi_bmask = kzalloc(longs *
5867 sizeof(unsigned long),
5868 GFP_KERNEL);
5869 if (unlikely(!phba->vpi_bmask)) {
5870 rc = -ENOMEM;
5871 goto free_rpi_ids;
5872 }
5873 phba->vpi_ids = kzalloc(count *
5874 sizeof(uint16_t),
5875 GFP_KERNEL);
5876 if (unlikely(!phba->vpi_ids)) {
5877 rc = -ENOMEM;
5878 goto free_vpi_bmask;
5879 }
5880
5881 for (i = 0; i < count; i++)
5882 phba->vpi_ids[i] = base + i;
5883
5884 /* XRIs. */
5885 count = phba->sli4_hba.max_cfg_param.max_xri;
5886 if (count <= 0) {
5887 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5888 "3281 Invalid provisioning of "
5889 "xri:%d\n", count);
5890 rc = -EINVAL;
5891 goto free_vpi_ids;
5892 }
5893 base = phba->sli4_hba.max_cfg_param.xri_base;
5894 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5895 phba->sli4_hba.xri_bmask = kzalloc(longs *
5896 sizeof(unsigned long),
5897 GFP_KERNEL);
5898 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5899 rc = -ENOMEM;
5900 goto free_vpi_ids;
5901 }
5902 phba->sli4_hba.max_cfg_param.xri_used = 0;
5903 phba->sli4_hba.xri_ids = kzalloc(count *
5904 sizeof(uint16_t),
5905 GFP_KERNEL);
5906 if (unlikely(!phba->sli4_hba.xri_ids)) {
5907 rc = -ENOMEM;
5908 goto free_xri_bmask;
5909 }
5910
5911 for (i = 0; i < count; i++)
5912 phba->sli4_hba.xri_ids[i] = base + i;
5913
5914 /* VFIs. */
5915 count = phba->sli4_hba.max_cfg_param.max_vfi;
5916 if (count <= 0) {
5917 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5918 "3282 Invalid provisioning of "
5919 "vfi:%d\n", count);
5920 rc = -EINVAL;
5921 goto free_xri_ids;
5922 }
5923 base = phba->sli4_hba.max_cfg_param.vfi_base;
5924 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5925 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5926 sizeof(unsigned long),
5927 GFP_KERNEL);
5928 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5929 rc = -ENOMEM;
5930 goto free_xri_ids;
5931 }
5932 phba->sli4_hba.vfi_ids = kzalloc(count *
5933 sizeof(uint16_t),
5934 GFP_KERNEL);
5935 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5936 rc = -ENOMEM;
5937 goto free_vfi_bmask;
5938 }
5939
5940 for (i = 0; i < count; i++)
5941 phba->sli4_hba.vfi_ids[i] = base + i;
5942
5943 /*
5944 * Mark all resources ready. An HBA reset doesn't need
5945 * to reset the initialization.
5946 */
5947 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5948 LPFC_IDX_RSRC_RDY);
5949 return 0;
5950 }
5951
5952 free_vfi_bmask:
5953 kfree(phba->sli4_hba.vfi_bmask);
5954 free_xri_ids:
5955 kfree(phba->sli4_hba.xri_ids);
5956 free_xri_bmask:
5957 kfree(phba->sli4_hba.xri_bmask);
5958 free_vpi_ids:
5959 kfree(phba->vpi_ids);
5960 free_vpi_bmask:
5961 kfree(phba->vpi_bmask);
5962 free_rpi_ids:
5963 kfree(phba->sli4_hba.rpi_ids);
5964 free_rpi_bmask:
5965 kfree(phba->sli4_hba.rpi_bmask);
5966 err_exit:
5967 return rc;
5968 }
5969
5970 /**
5971 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5972 * @phba: Pointer to HBA context object.
5973 *
5974 * This function allocates the number of elements for the specified
5975 * resource type.
5976 **/
5977 int
5978 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5979 {
5980 if (phba->sli4_hba.extents_in_use) {
5981 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5982 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5983 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5984 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5985 } else {
5986 kfree(phba->vpi_bmask);
5987 phba->sli4_hba.max_cfg_param.vpi_used = 0;
5988 kfree(phba->vpi_ids);
5989 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5990 kfree(phba->sli4_hba.xri_bmask);
5991 kfree(phba->sli4_hba.xri_ids);
5992 kfree(phba->sli4_hba.vfi_bmask);
5993 kfree(phba->sli4_hba.vfi_ids);
5994 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5995 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5996 }
5997
5998 return 0;
5999 }
6000
6001 /**
6002 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
6003 * @phba: Pointer to HBA context object.
6004 * @type: The resource extent type.
6005 * @extnt_count: buffer to hold port extent count response
6006 * @extnt_size: buffer to hold port extent size response.
6007 *
6008 * This function calls the port to read the host allocated extents
6009 * for a particular type.
6010 **/
6011 int
6012 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
6013 uint16_t *extnt_cnt, uint16_t *extnt_size)
6014 {
6015 bool emb;
6016 int rc = 0;
6017 uint16_t curr_blks = 0;
6018 uint32_t req_len, emb_len;
6019 uint32_t alloc_len, mbox_tmo;
6020 struct list_head *blk_list_head;
6021 struct lpfc_rsrc_blks *rsrc_blk;
6022 LPFC_MBOXQ_t *mbox;
6023 void *virtaddr = NULL;
6024 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
6025 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
6026 union lpfc_sli4_cfg_shdr *shdr;
6027
6028 switch (type) {
6029 case LPFC_RSC_TYPE_FCOE_VPI:
6030 blk_list_head = &phba->lpfc_vpi_blk_list;
6031 break;
6032 case LPFC_RSC_TYPE_FCOE_XRI:
6033 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
6034 break;
6035 case LPFC_RSC_TYPE_FCOE_VFI:
6036 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
6037 break;
6038 case LPFC_RSC_TYPE_FCOE_RPI:
6039 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
6040 break;
6041 default:
6042 return -EIO;
6043 }
6044
6045 /* Count the number of extents currently allocatd for this type. */
6046 list_for_each_entry(rsrc_blk, blk_list_head, list) {
6047 if (curr_blks == 0) {
6048 /*
6049 * The GET_ALLOCATED mailbox does not return the size,
6050 * just the count. The size should be just the size
6051 * stored in the current allocated block and all sizes
6052 * for an extent type are the same so set the return
6053 * value now.
6054 */
6055 *extnt_size = rsrc_blk->rsrc_size;
6056 }
6057 curr_blks++;
6058 }
6059
6060 /*
6061 * Calculate the size of an embedded mailbox. The uint32_t
6062 * accounts for extents-specific word.
6063 */
6064 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
6065 sizeof(uint32_t);
6066
6067 /*
6068 * Presume the allocation and response will fit into an embedded
6069 * mailbox. If not true, reconfigure to a non-embedded mailbox.
6070 */
6071 emb = LPFC_SLI4_MBX_EMBED;
6072 req_len = emb_len;
6073 if (req_len > emb_len) {
6074 req_len = curr_blks * sizeof(uint16_t) +
6075 sizeof(union lpfc_sli4_cfg_shdr) +
6076 sizeof(uint32_t);
6077 emb = LPFC_SLI4_MBX_NEMBED;
6078 }
6079
6080 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6081 if (!mbox)
6082 return -ENOMEM;
6083 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
6084
6085 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
6086 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
6087 req_len, emb);
6088 if (alloc_len < req_len) {
6089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6090 "2983 Allocated DMA memory size (x%x) is "
6091 "less than the requested DMA memory "
6092 "size (x%x)\n", alloc_len, req_len);
6093 rc = -ENOMEM;
6094 goto err_exit;
6095 }
6096 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
6097 if (unlikely(rc)) {
6098 rc = -EIO;
6099 goto err_exit;
6100 }
6101
6102 if (!phba->sli4_hba.intr_enable)
6103 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
6104 else {
6105 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
6106 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
6107 }
6108
6109 if (unlikely(rc)) {
6110 rc = -EIO;
6111 goto err_exit;
6112 }
6113
6114 /*
6115 * Figure out where the response is located. Then get local pointers
6116 * to the response data. The port does not guarantee to respond to
6117 * all extents counts request so update the local variable with the
6118 * allocated count from the port.
6119 */
6120 if (emb == LPFC_SLI4_MBX_EMBED) {
6121 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
6122 shdr = &rsrc_ext->header.cfg_shdr;
6123 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
6124 } else {
6125 virtaddr = mbox->sge_array->addr[0];
6126 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
6127 shdr = &n_rsrc->cfg_shdr;
6128 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
6129 }
6130
6131 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
6132 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
6133 "2984 Failed to read allocated resources "
6134 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
6135 type,
6136 bf_get(lpfc_mbox_hdr_status, &shdr->response),
6137 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
6138 rc = -EIO;
6139 goto err_exit;
6140 }
6141 err_exit:
6142 lpfc_sli4_mbox_cmd_free(phba, mbox);
6143 return rc;
6144 }
6145
6146 /**
6147 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
6148 * @phba: pointer to lpfc hba data structure.
6149 *
6150 * This routine walks the list of els buffers that have been allocated and
6151 * repost them to the port by using SGL block post. This is needed after a
6152 * pci_function_reset/warm_start or start. It attempts to construct blocks
6153 * of els buffer sgls which contains contiguous xris and uses the non-embedded
6154 * SGL block post mailbox commands to post them to the port. For single els
6155 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
6156 * mailbox command for posting.
6157 *
6158 * Returns: 0 = success, non-zero failure.
6159 **/
6160 static int
6161 lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
6162 {
6163 struct lpfc_sglq *sglq_entry = NULL;
6164 struct lpfc_sglq *sglq_entry_next = NULL;
6165 struct lpfc_sglq *sglq_entry_first = NULL;
6166 int status, total_cnt, post_cnt = 0, num_posted = 0, block_cnt = 0;
6167 int last_xritag = NO_XRI;
6168 struct lpfc_sli_ring *pring;
6169 LIST_HEAD(prep_sgl_list);
6170 LIST_HEAD(blck_sgl_list);
6171 LIST_HEAD(allc_sgl_list);
6172 LIST_HEAD(post_sgl_list);
6173 LIST_HEAD(free_sgl_list);
6174
6175 pring = &phba->sli.ring[LPFC_ELS_RING];
6176 spin_lock_irq(&phba->hbalock);
6177 spin_lock(&pring->ring_lock);
6178 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
6179 spin_unlock(&pring->ring_lock);
6180 spin_unlock_irq(&phba->hbalock);
6181
6182 total_cnt = phba->sli4_hba.els_xri_cnt;
6183 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6184 &allc_sgl_list, list) {
6185 list_del_init(&sglq_entry->list);
6186 block_cnt++;
6187 if ((last_xritag != NO_XRI) &&
6188 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6189 /* a hole in xri block, form a sgl posting block */
6190 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6191 post_cnt = block_cnt - 1;
6192 /* prepare list for next posting block */
6193 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6194 block_cnt = 1;
6195 } else {
6196 /* prepare list for next posting block */
6197 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6198 /* enough sgls for non-embed sgl mbox command */
6199 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6200 list_splice_init(&prep_sgl_list,
6201 &blck_sgl_list);
6202 post_cnt = block_cnt;
6203 block_cnt = 0;
6204 }
6205 }
6206 num_posted++;
6207
6208 /* keep track of last sgl's xritag */
6209 last_xritag = sglq_entry->sli4_xritag;
6210
6211 /* end of repost sgl list condition for els buffers */
6212 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6213 if (post_cnt == 0) {
6214 list_splice_init(&prep_sgl_list,
6215 &blck_sgl_list);
6216 post_cnt = block_cnt;
6217 } else if (block_cnt == 1) {
6218 status = lpfc_sli4_post_sgl(phba,
6219 sglq_entry->phys, 0,
6220 sglq_entry->sli4_xritag);
6221 if (!status) {
6222 /* successful, put sgl to posted list */
6223 list_add_tail(&sglq_entry->list,
6224 &post_sgl_list);
6225 } else {
6226 /* Failure, put sgl to free list */
6227 lpfc_printf_log(phba, KERN_WARNING,
6228 LOG_SLI,
6229 "3159 Failed to post els "
6230 "sgl, xritag:x%x\n",
6231 sglq_entry->sli4_xritag);
6232 list_add_tail(&sglq_entry->list,
6233 &free_sgl_list);
6234 total_cnt--;
6235 }
6236 }
6237 }
6238
6239 /* continue until a nembed page worth of sgls */
6240 if (post_cnt == 0)
6241 continue;
6242
6243 /* post the els buffer list sgls as a block */
6244 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6245 post_cnt);
6246
6247 if (!status) {
6248 /* success, put sgl list to posted sgl list */
6249 list_splice_init(&blck_sgl_list, &post_sgl_list);
6250 } else {
6251 /* Failure, put sgl list to free sgl list */
6252 sglq_entry_first = list_first_entry(&blck_sgl_list,
6253 struct lpfc_sglq,
6254 list);
6255 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6256 "3160 Failed to post els sgl-list, "
6257 "xritag:x%x-x%x\n",
6258 sglq_entry_first->sli4_xritag,
6259 (sglq_entry_first->sli4_xritag +
6260 post_cnt - 1));
6261 list_splice_init(&blck_sgl_list, &free_sgl_list);
6262 total_cnt -= post_cnt;
6263 }
6264
6265 /* don't reset xirtag due to hole in xri block */
6266 if (block_cnt == 0)
6267 last_xritag = NO_XRI;
6268
6269 /* reset els sgl post count for next round of posting */
6270 post_cnt = 0;
6271 }
6272 /* update the number of XRIs posted for ELS */
6273 phba->sli4_hba.els_xri_cnt = total_cnt;
6274
6275 /* free the els sgls failed to post */
6276 lpfc_free_sgl_list(phba, &free_sgl_list);
6277
6278 /* push els sgls posted to the availble list */
6279 if (!list_empty(&post_sgl_list)) {
6280 spin_lock_irq(&phba->hbalock);
6281 spin_lock(&pring->ring_lock);
6282 list_splice_init(&post_sgl_list,
6283 &phba->sli4_hba.lpfc_sgl_list);
6284 spin_unlock(&pring->ring_lock);
6285 spin_unlock_irq(&phba->hbalock);
6286 } else {
6287 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6288 "3161 Failure to post els sgl to port.\n");
6289 return -EIO;
6290 }
6291 return 0;
6292 }
6293
6294 /**
6295 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6296 * @phba: Pointer to HBA context object.
6297 *
6298 * This function is the main SLI4 device intialization PCI function. This
6299 * function is called by the HBA intialization code, HBA reset code and
6300 * HBA error attention handler code. Caller is not required to hold any
6301 * locks.
6302 **/
6303 int
6304 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6305 {
6306 int rc;
6307 LPFC_MBOXQ_t *mboxq;
6308 struct lpfc_mqe *mqe;
6309 uint8_t *vpd;
6310 uint32_t vpd_size;
6311 uint32_t ftr_rsp = 0;
6312 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6313 struct lpfc_vport *vport = phba->pport;
6314 struct lpfc_dmabuf *mp;
6315
6316 /* Perform a PCI function reset to start from clean */
6317 rc = lpfc_pci_function_reset(phba);
6318 if (unlikely(rc))
6319 return -ENODEV;
6320
6321 /* Check the HBA Host Status Register for readyness */
6322 rc = lpfc_sli4_post_status_check(phba);
6323 if (unlikely(rc))
6324 return -ENODEV;
6325 else {
6326 spin_lock_irq(&phba->hbalock);
6327 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6328 spin_unlock_irq(&phba->hbalock);
6329 }
6330
6331 /*
6332 * Allocate a single mailbox container for initializing the
6333 * port.
6334 */
6335 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6336 if (!mboxq)
6337 return -ENOMEM;
6338
6339 /* Issue READ_REV to collect vpd and FW information. */
6340 vpd_size = SLI4_PAGE_SIZE;
6341 vpd = kzalloc(vpd_size, GFP_KERNEL);
6342 if (!vpd) {
6343 rc = -ENOMEM;
6344 goto out_free_mbox;
6345 }
6346
6347 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6348 if (unlikely(rc)) {
6349 kfree(vpd);
6350 goto out_free_mbox;
6351 }
6352
6353 mqe = &mboxq->u.mqe;
6354 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6355 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev)) {
6356 phba->hba_flag |= HBA_FCOE_MODE;
6357 phba->fcp_embed_io = 0; /* SLI4 FC support only */
6358 } else {
6359 phba->hba_flag &= ~HBA_FCOE_MODE;
6360 }
6361
6362 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6363 LPFC_DCBX_CEE_MODE)
6364 phba->hba_flag |= HBA_FIP_SUPPORT;
6365 else
6366 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6367
6368 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6369
6370 if (phba->sli_rev != LPFC_SLI_REV4) {
6371 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6372 "0376 READ_REV Error. SLI Level %d "
6373 "FCoE enabled %d\n",
6374 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6375 rc = -EIO;
6376 kfree(vpd);
6377 goto out_free_mbox;
6378 }
6379
6380 /*
6381 * Continue initialization with default values even if driver failed
6382 * to read FCoE param config regions, only read parameters if the
6383 * board is FCoE
6384 */
6385 if (phba->hba_flag & HBA_FCOE_MODE &&
6386 lpfc_sli4_read_fcoe_params(phba))
6387 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6388 "2570 Failed to read FCoE parameters\n");
6389
6390 /*
6391 * Retrieve sli4 device physical port name, failure of doing it
6392 * is considered as non-fatal.
6393 */
6394 rc = lpfc_sli4_retrieve_pport_name(phba);
6395 if (!rc)
6396 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6397 "3080 Successful retrieving SLI4 device "
6398 "physical port name: %s.\n", phba->Port);
6399
6400 /*
6401 * Evaluate the read rev and vpd data. Populate the driver
6402 * state with the results. If this routine fails, the failure
6403 * is not fatal as the driver will use generic values.
6404 */
6405 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6406 if (unlikely(!rc)) {
6407 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6408 "0377 Error %d parsing vpd. "
6409 "Using defaults.\n", rc);
6410 rc = 0;
6411 }
6412 kfree(vpd);
6413
6414 /* Save information as VPD data */
6415 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6416 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6417 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6418 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6419 &mqe->un.read_rev);
6420 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6421 &mqe->un.read_rev);
6422 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6423 &mqe->un.read_rev);
6424 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6425 &mqe->un.read_rev);
6426 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6427 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6428 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6429 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6430 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6431 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6432 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6433 "(%d):0380 READ_REV Status x%x "
6434 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6435 mboxq->vport ? mboxq->vport->vpi : 0,
6436 bf_get(lpfc_mqe_status, mqe),
6437 phba->vpd.rev.opFwName,
6438 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6439 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6440
6441 /* Reset the DFT_LUN_Q_DEPTH to (max xri >> 3) */
6442 rc = (phba->sli4_hba.max_cfg_param.max_xri >> 3);
6443 if (phba->pport->cfg_lun_queue_depth > rc) {
6444 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6445 "3362 LUN queue depth changed from %d to %d\n",
6446 phba->pport->cfg_lun_queue_depth, rc);
6447 phba->pport->cfg_lun_queue_depth = rc;
6448 }
6449
6450 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
6451 LPFC_SLI_INTF_IF_TYPE_0) {
6452 lpfc_set_features(phba, mboxq, LPFC_SET_UE_RECOVERY);
6453 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6454 if (rc == MBX_SUCCESS) {
6455 phba->hba_flag |= HBA_RECOVERABLE_UE;
6456 /* Set 1Sec interval to detect UE */
6457 phba->eratt_poll_interval = 1;
6458 phba->sli4_hba.ue_to_sr = bf_get(
6459 lpfc_mbx_set_feature_UESR,
6460 &mboxq->u.mqe.un.set_feature);
6461 phba->sli4_hba.ue_to_rp = bf_get(
6462 lpfc_mbx_set_feature_UERP,
6463 &mboxq->u.mqe.un.set_feature);
6464 }
6465 }
6466
6467 if (phba->cfg_enable_mds_diags && phba->mds_diags_support) {
6468 /* Enable MDS Diagnostics only if the SLI Port supports it */
6469 lpfc_set_features(phba, mboxq, LPFC_SET_MDS_DIAGS);
6470 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6471 if (rc != MBX_SUCCESS)
6472 phba->mds_diags_support = 0;
6473 }
6474
6475 /*
6476 * Discover the port's supported feature set and match it against the
6477 * hosts requests.
6478 */
6479 lpfc_request_features(phba, mboxq);
6480 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6481 if (unlikely(rc)) {
6482 rc = -EIO;
6483 goto out_free_mbox;
6484 }
6485
6486 /*
6487 * The port must support FCP initiator mode as this is the
6488 * only mode running in the host.
6489 */
6490 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6491 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6492 "0378 No support for fcpi mode.\n");
6493 ftr_rsp++;
6494 }
6495 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6496 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6497 else
6498 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6499 /*
6500 * If the port cannot support the host's requested features
6501 * then turn off the global config parameters to disable the
6502 * feature in the driver. This is not a fatal error.
6503 */
6504 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6505 if (phba->cfg_enable_bg) {
6506 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6507 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6508 else
6509 ftr_rsp++;
6510 }
6511
6512 if (phba->max_vpi && phba->cfg_enable_npiv &&
6513 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6514 ftr_rsp++;
6515
6516 if (ftr_rsp) {
6517 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6518 "0379 Feature Mismatch Data: x%08x %08x "
6519 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6520 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6521 phba->cfg_enable_npiv, phba->max_vpi);
6522 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6523 phba->cfg_enable_bg = 0;
6524 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6525 phba->cfg_enable_npiv = 0;
6526 }
6527
6528 /* These SLI3 features are assumed in SLI4 */
6529 spin_lock_irq(&phba->hbalock);
6530 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6531 spin_unlock_irq(&phba->hbalock);
6532
6533 /*
6534 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6535 * calls depends on these resources to complete port setup.
6536 */
6537 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6538 if (rc) {
6539 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6540 "2920 Failed to alloc Resource IDs "
6541 "rc = x%x\n", rc);
6542 goto out_free_mbox;
6543 }
6544
6545 /* Read the port's service parameters. */
6546 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6547 if (rc) {
6548 phba->link_state = LPFC_HBA_ERROR;
6549 rc = -ENOMEM;
6550 goto out_free_mbox;
6551 }
6552
6553 mboxq->vport = vport;
6554 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6555 mp = (struct lpfc_dmabuf *) mboxq->context1;
6556 if (rc == MBX_SUCCESS) {
6557 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6558 rc = 0;
6559 }
6560
6561 /*
6562 * This memory was allocated by the lpfc_read_sparam routine. Release
6563 * it to the mbuf pool.
6564 */
6565 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6566 kfree(mp);
6567 mboxq->context1 = NULL;
6568 if (unlikely(rc)) {
6569 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6570 "0382 READ_SPARAM command failed "
6571 "status %d, mbxStatus x%x\n",
6572 rc, bf_get(lpfc_mqe_status, mqe));
6573 phba->link_state = LPFC_HBA_ERROR;
6574 rc = -EIO;
6575 goto out_free_mbox;
6576 }
6577
6578 lpfc_update_vport_wwn(vport);
6579
6580 /* Update the fc_host data structures with new wwn. */
6581 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6582 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6583
6584 /* update host els and scsi xri-sgl sizes and mappings */
6585 rc = lpfc_sli4_xri_sgl_update(phba);
6586 if (unlikely(rc)) {
6587 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6588 "1400 Failed to update xri-sgl size and "
6589 "mapping: %d\n", rc);
6590 goto out_free_mbox;
6591 }
6592
6593 /* register the els sgl pool to the port */
6594 rc = lpfc_sli4_repost_els_sgl_list(phba);
6595 if (unlikely(rc)) {
6596 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6597 "0582 Error %d during els sgl post "
6598 "operation\n", rc);
6599 rc = -ENODEV;
6600 goto out_free_mbox;
6601 }
6602
6603 /* register the allocated scsi sgl pool to the port */
6604 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6605 if (unlikely(rc)) {
6606 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6607 "0383 Error %d during scsi sgl post "
6608 "operation\n", rc);
6609 /* Some Scsi buffers were moved to the abort scsi list */
6610 /* A pci function reset will repost them */
6611 rc = -ENODEV;
6612 goto out_free_mbox;
6613 }
6614
6615 /* Post the rpi header region to the device. */
6616 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6617 if (unlikely(rc)) {
6618 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6619 "0393 Error %d during rpi post operation\n",
6620 rc);
6621 rc = -ENODEV;
6622 goto out_free_mbox;
6623 }
6624 lpfc_sli4_node_prep(phba);
6625
6626 /* Create all the SLI4 queues */
6627 rc = lpfc_sli4_queue_create(phba);
6628 if (rc) {
6629 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6630 "3089 Failed to allocate queues\n");
6631 rc = -ENODEV;
6632 goto out_stop_timers;
6633 }
6634 /* Set up all the queues to the device */
6635 rc = lpfc_sli4_queue_setup(phba);
6636 if (unlikely(rc)) {
6637 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6638 "0381 Error %d during queue setup.\n ", rc);
6639 goto out_destroy_queue;
6640 }
6641
6642 /* Arm the CQs and then EQs on device */
6643 lpfc_sli4_arm_cqeq_intr(phba);
6644
6645 /* Indicate device interrupt mode */
6646 phba->sli4_hba.intr_enable = 1;
6647
6648 /* Allow asynchronous mailbox command to go through */
6649 spin_lock_irq(&phba->hbalock);
6650 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6651 spin_unlock_irq(&phba->hbalock);
6652
6653 /* Post receive buffers to the device */
6654 lpfc_sli4_rb_setup(phba);
6655
6656 /* Reset HBA FCF states after HBA reset */
6657 phba->fcf.fcf_flag = 0;
6658 phba->fcf.current_rec.flag = 0;
6659
6660 /* Start the ELS watchdog timer */
6661 mod_timer(&vport->els_tmofunc,
6662 jiffies + msecs_to_jiffies(1000 * (phba->fc_ratov * 2)));
6663
6664 /* Start heart beat timer */
6665 mod_timer(&phba->hb_tmofunc,
6666 jiffies + msecs_to_jiffies(1000 * LPFC_HB_MBOX_INTERVAL));
6667 phba->hb_outstanding = 0;
6668 phba->last_completion_time = jiffies;
6669
6670 /* Start error attention (ERATT) polling timer */
6671 mod_timer(&phba->eratt_poll,
6672 jiffies + msecs_to_jiffies(1000 * phba->eratt_poll_interval));
6673
6674 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6675 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6676 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6677 if (!rc) {
6678 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6679 "2829 This device supports "
6680 "Advanced Error Reporting (AER)\n");
6681 spin_lock_irq(&phba->hbalock);
6682 phba->hba_flag |= HBA_AER_ENABLED;
6683 spin_unlock_irq(&phba->hbalock);
6684 } else {
6685 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6686 "2830 This device does not support "
6687 "Advanced Error Reporting (AER)\n");
6688 phba->cfg_aer_support = 0;
6689 }
6690 rc = 0;
6691 }
6692
6693 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6694 /*
6695 * The FC Port needs to register FCFI (index 0)
6696 */
6697 lpfc_reg_fcfi(phba, mboxq);
6698 mboxq->vport = phba->pport;
6699 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6700 if (rc != MBX_SUCCESS)
6701 goto out_unset_queue;
6702 rc = 0;
6703 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6704 &mboxq->u.mqe.un.reg_fcfi);
6705
6706 /* Check if the port is configured to be disabled */
6707 lpfc_sli_read_link_ste(phba);
6708 }
6709
6710 /*
6711 * The port is ready, set the host's link state to LINK_DOWN
6712 * in preparation for link interrupts.
6713 */
6714 spin_lock_irq(&phba->hbalock);
6715 phba->link_state = LPFC_LINK_DOWN;
6716 spin_unlock_irq(&phba->hbalock);
6717 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6718 (phba->hba_flag & LINK_DISABLED)) {
6719 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6720 "3103 Adapter Link is disabled.\n");
6721 lpfc_down_link(phba, mboxq);
6722 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6723 if (rc != MBX_SUCCESS) {
6724 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6725 "3104 Adapter failed to issue "
6726 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6727 goto out_unset_queue;
6728 }
6729 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6730 /* don't perform init_link on SLI4 FC port loopback test */
6731 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6732 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6733 if (rc)
6734 goto out_unset_queue;
6735 }
6736 }
6737 mempool_free(mboxq, phba->mbox_mem_pool);
6738 return rc;
6739 out_unset_queue:
6740 /* Unset all the queues set up in this routine when error out */
6741 lpfc_sli4_queue_unset(phba);
6742 out_destroy_queue:
6743 lpfc_sli4_queue_destroy(phba);
6744 out_stop_timers:
6745 lpfc_stop_hba_timers(phba);
6746 out_free_mbox:
6747 mempool_free(mboxq, phba->mbox_mem_pool);
6748 return rc;
6749 }
6750
6751 /**
6752 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6753 * @ptr: context object - pointer to hba structure.
6754 *
6755 * This is the callback function for mailbox timer. The mailbox
6756 * timer is armed when a new mailbox command is issued and the timer
6757 * is deleted when the mailbox complete. The function is called by
6758 * the kernel timer code when a mailbox does not complete within
6759 * expected time. This function wakes up the worker thread to
6760 * process the mailbox timeout and returns. All the processing is
6761 * done by the worker thread function lpfc_mbox_timeout_handler.
6762 **/
6763 void
6764 lpfc_mbox_timeout(unsigned long ptr)
6765 {
6766 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6767 unsigned long iflag;
6768 uint32_t tmo_posted;
6769
6770 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6771 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6772 if (!tmo_posted)
6773 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6774 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6775
6776 if (!tmo_posted)
6777 lpfc_worker_wake_up(phba);
6778 return;
6779 }
6780
6781 /**
6782 * lpfc_sli4_mbox_completions_pending - check to see if any mailbox completions
6783 * are pending
6784 * @phba: Pointer to HBA context object.
6785 *
6786 * This function checks if any mailbox completions are present on the mailbox
6787 * completion queue.
6788 **/
6789 static bool
6790 lpfc_sli4_mbox_completions_pending(struct lpfc_hba *phba)
6791 {
6792
6793 uint32_t idx;
6794 struct lpfc_queue *mcq;
6795 struct lpfc_mcqe *mcqe;
6796 bool pending_completions = false;
6797
6798 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6799 return false;
6800
6801 /* Check for completions on mailbox completion queue */
6802
6803 mcq = phba->sli4_hba.mbx_cq;
6804 idx = mcq->hba_index;
6805 while (bf_get_le32(lpfc_cqe_valid, mcq->qe[idx].cqe)) {
6806 mcqe = (struct lpfc_mcqe *)mcq->qe[idx].cqe;
6807 if (bf_get_le32(lpfc_trailer_completed, mcqe) &&
6808 (!bf_get_le32(lpfc_trailer_async, mcqe))) {
6809 pending_completions = true;
6810 break;
6811 }
6812 idx = (idx + 1) % mcq->entry_count;
6813 if (mcq->hba_index == idx)
6814 break;
6815 }
6816 return pending_completions;
6817
6818 }
6819
6820 /**
6821 * lpfc_sli4_process_missed_mbox_completions - process mbox completions
6822 * that were missed.
6823 * @phba: Pointer to HBA context object.
6824 *
6825 * For sli4, it is possible to miss an interrupt. As such mbox completions
6826 * maybe missed causing erroneous mailbox timeouts to occur. This function
6827 * checks to see if mbox completions are on the mailbox completion queue
6828 * and will process all the completions associated with the eq for the
6829 * mailbox completion queue.
6830 **/
6831 bool
6832 lpfc_sli4_process_missed_mbox_completions(struct lpfc_hba *phba)
6833 {
6834
6835 uint32_t eqidx;
6836 struct lpfc_queue *fpeq = NULL;
6837 struct lpfc_eqe *eqe;
6838 bool mbox_pending;
6839
6840 if (unlikely(!phba) || (phba->sli_rev != LPFC_SLI_REV4))
6841 return false;
6842
6843 /* Find the eq associated with the mcq */
6844
6845 if (phba->sli4_hba.hba_eq)
6846 for (eqidx = 0; eqidx < phba->cfg_fcp_io_channel; eqidx++)
6847 if (phba->sli4_hba.hba_eq[eqidx]->queue_id ==
6848 phba->sli4_hba.mbx_cq->assoc_qid) {
6849 fpeq = phba->sli4_hba.hba_eq[eqidx];
6850 break;
6851 }
6852 if (!fpeq)
6853 return false;
6854
6855 /* Turn off interrupts from this EQ */
6856
6857 lpfc_sli4_eq_clr_intr(fpeq);
6858
6859 /* Check to see if a mbox completion is pending */
6860
6861 mbox_pending = lpfc_sli4_mbox_completions_pending(phba);
6862
6863 /*
6864 * If a mbox completion is pending, process all the events on EQ
6865 * associated with the mbox completion queue (this could include
6866 * mailbox commands, async events, els commands, receive queue data
6867 * and fcp commands)
6868 */
6869
6870 if (mbox_pending)
6871 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
6872 lpfc_sli4_hba_handle_eqe(phba, eqe, eqidx);
6873 fpeq->EQ_processed++;
6874 }
6875
6876 /* Always clear and re-arm the EQ */
6877
6878 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
6879
6880 return mbox_pending;
6881
6882 }
6883
6884 /**
6885 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6886 * @phba: Pointer to HBA context object.
6887 *
6888 * This function is called from worker thread when a mailbox command times out.
6889 * The caller is not required to hold any locks. This function will reset the
6890 * HBA and recover all the pending commands.
6891 **/
6892 void
6893 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6894 {
6895 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6896 MAILBOX_t *mb = NULL;
6897
6898 struct lpfc_sli *psli = &phba->sli;
6899
6900 /* If the mailbox completed, process the completion and return */
6901 if (lpfc_sli4_process_missed_mbox_completions(phba))
6902 return;
6903
6904 if (pmbox != NULL)
6905 mb = &pmbox->u.mb;
6906 /* Check the pmbox pointer first. There is a race condition
6907 * between the mbox timeout handler getting executed in the
6908 * worklist and the mailbox actually completing. When this
6909 * race condition occurs, the mbox_active will be NULL.
6910 */
6911 spin_lock_irq(&phba->hbalock);
6912 if (pmbox == NULL) {
6913 lpfc_printf_log(phba, KERN_WARNING,
6914 LOG_MBOX | LOG_SLI,
6915 "0353 Active Mailbox cleared - mailbox timeout "
6916 "exiting\n");
6917 spin_unlock_irq(&phba->hbalock);
6918 return;
6919 }
6920
6921 /* Mbox cmd <mbxCommand> timeout */
6922 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6923 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6924 mb->mbxCommand,
6925 phba->pport->port_state,
6926 phba->sli.sli_flag,
6927 phba->sli.mbox_active);
6928 spin_unlock_irq(&phba->hbalock);
6929
6930 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6931 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6932 * it to fail all outstanding SCSI IO.
6933 */
6934 spin_lock_irq(&phba->pport->work_port_lock);
6935 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6936 spin_unlock_irq(&phba->pport->work_port_lock);
6937 spin_lock_irq(&phba->hbalock);
6938 phba->link_state = LPFC_LINK_UNKNOWN;
6939 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6940 spin_unlock_irq(&phba->hbalock);
6941
6942 lpfc_sli_abort_fcp_rings(phba);
6943
6944 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6945 "0345 Resetting board due to mailbox timeout\n");
6946
6947 /* Reset the HBA device */
6948 lpfc_reset_hba(phba);
6949 }
6950
6951 /**
6952 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6953 * @phba: Pointer to HBA context object.
6954 * @pmbox: Pointer to mailbox object.
6955 * @flag: Flag indicating how the mailbox need to be processed.
6956 *
6957 * This function is called by discovery code and HBA management code
6958 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6959 * function gets the hbalock to protect the data structures.
6960 * The mailbox command can be submitted in polling mode, in which case
6961 * this function will wait in a polling loop for the completion of the
6962 * mailbox.
6963 * If the mailbox is submitted in no_wait mode (not polling) the
6964 * function will submit the command and returns immediately without waiting
6965 * for the mailbox completion. The no_wait is supported only when HBA
6966 * is in SLI2/SLI3 mode - interrupts are enabled.
6967 * The SLI interface allows only one mailbox pending at a time. If the
6968 * mailbox is issued in polling mode and there is already a mailbox
6969 * pending, then the function will return an error. If the mailbox is issued
6970 * in NO_WAIT mode and there is a mailbox pending already, the function
6971 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6972 * The sli layer owns the mailbox object until the completion of mailbox
6973 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6974 * return codes the caller owns the mailbox command after the return of
6975 * the function.
6976 **/
6977 static int
6978 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6979 uint32_t flag)
6980 {
6981 MAILBOX_t *mbx;
6982 struct lpfc_sli *psli = &phba->sli;
6983 uint32_t status, evtctr;
6984 uint32_t ha_copy, hc_copy;
6985 int i;
6986 unsigned long timeout;
6987 unsigned long drvr_flag = 0;
6988 uint32_t word0, ldata;
6989 void __iomem *to_slim;
6990 int processing_queue = 0;
6991
6992 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6993 if (!pmbox) {
6994 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6995 /* processing mbox queue from intr_handler */
6996 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6997 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6998 return MBX_SUCCESS;
6999 }
7000 processing_queue = 1;
7001 pmbox = lpfc_mbox_get(phba);
7002 if (!pmbox) {
7003 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7004 return MBX_SUCCESS;
7005 }
7006 }
7007
7008 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
7009 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
7010 if(!pmbox->vport) {
7011 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7012 lpfc_printf_log(phba, KERN_ERR,
7013 LOG_MBOX | LOG_VPORT,
7014 "1806 Mbox x%x failed. No vport\n",
7015 pmbox->u.mb.mbxCommand);
7016 dump_stack();
7017 goto out_not_finished;
7018 }
7019 }
7020
7021 /* If the PCI channel is in offline state, do not post mbox. */
7022 if (unlikely(pci_channel_offline(phba->pcidev))) {
7023 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7024 goto out_not_finished;
7025 }
7026
7027 /* If HBA has a deferred error attention, fail the iocb. */
7028 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
7029 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7030 goto out_not_finished;
7031 }
7032
7033 psli = &phba->sli;
7034
7035 mbx = &pmbox->u.mb;
7036 status = MBX_SUCCESS;
7037
7038 if (phba->link_state == LPFC_HBA_ERROR) {
7039 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7040
7041 /* Mbox command <mbxCommand> cannot issue */
7042 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7043 "(%d):0311 Mailbox command x%x cannot "
7044 "issue Data: x%x x%x\n",
7045 pmbox->vport ? pmbox->vport->vpi : 0,
7046 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7047 goto out_not_finished;
7048 }
7049
7050 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
7051 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
7052 !(hc_copy & HC_MBINT_ENA)) {
7053 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7054 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7055 "(%d):2528 Mailbox command x%x cannot "
7056 "issue Data: x%x x%x\n",
7057 pmbox->vport ? pmbox->vport->vpi : 0,
7058 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
7059 goto out_not_finished;
7060 }
7061 }
7062
7063 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7064 /* Polling for a mbox command when another one is already active
7065 * is not allowed in SLI. Also, the driver must have established
7066 * SLI2 mode to queue and process multiple mbox commands.
7067 */
7068
7069 if (flag & MBX_POLL) {
7070 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7071
7072 /* Mbox command <mbxCommand> cannot issue */
7073 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7074 "(%d):2529 Mailbox command x%x "
7075 "cannot issue Data: x%x x%x\n",
7076 pmbox->vport ? pmbox->vport->vpi : 0,
7077 pmbox->u.mb.mbxCommand,
7078 psli->sli_flag, flag);
7079 goto out_not_finished;
7080 }
7081
7082 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
7083 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7084 /* Mbox command <mbxCommand> cannot issue */
7085 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7086 "(%d):2530 Mailbox command x%x "
7087 "cannot issue Data: x%x x%x\n",
7088 pmbox->vport ? pmbox->vport->vpi : 0,
7089 pmbox->u.mb.mbxCommand,
7090 psli->sli_flag, flag);
7091 goto out_not_finished;
7092 }
7093
7094 /* Another mailbox command is still being processed, queue this
7095 * command to be processed later.
7096 */
7097 lpfc_mbox_put(phba, pmbox);
7098
7099 /* Mbox cmd issue - BUSY */
7100 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7101 "(%d):0308 Mbox cmd issue - BUSY Data: "
7102 "x%x x%x x%x x%x\n",
7103 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
7104 mbx->mbxCommand, phba->pport->port_state,
7105 psli->sli_flag, flag);
7106
7107 psli->slistat.mbox_busy++;
7108 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7109
7110 if (pmbox->vport) {
7111 lpfc_debugfs_disc_trc(pmbox->vport,
7112 LPFC_DISC_TRC_MBOX_VPORT,
7113 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
7114 (uint32_t)mbx->mbxCommand,
7115 mbx->un.varWords[0], mbx->un.varWords[1]);
7116 }
7117 else {
7118 lpfc_debugfs_disc_trc(phba->pport,
7119 LPFC_DISC_TRC_MBOX,
7120 "MBOX Bsy: cmd:x%x mb:x%x x%x",
7121 (uint32_t)mbx->mbxCommand,
7122 mbx->un.varWords[0], mbx->un.varWords[1]);
7123 }
7124
7125 return MBX_BUSY;
7126 }
7127
7128 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7129
7130 /* If we are not polling, we MUST be in SLI2 mode */
7131 if (flag != MBX_POLL) {
7132 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
7133 (mbx->mbxCommand != MBX_KILL_BOARD)) {
7134 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7135 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7136 /* Mbox command <mbxCommand> cannot issue */
7137 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7138 "(%d):2531 Mailbox command x%x "
7139 "cannot issue Data: x%x x%x\n",
7140 pmbox->vport ? pmbox->vport->vpi : 0,
7141 pmbox->u.mb.mbxCommand,
7142 psli->sli_flag, flag);
7143 goto out_not_finished;
7144 }
7145 /* timeout active mbox command */
7146 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7147 1000);
7148 mod_timer(&psli->mbox_tmo, jiffies + timeout);
7149 }
7150
7151 /* Mailbox cmd <cmd> issue */
7152 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7153 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
7154 "x%x\n",
7155 pmbox->vport ? pmbox->vport->vpi : 0,
7156 mbx->mbxCommand, phba->pport->port_state,
7157 psli->sli_flag, flag);
7158
7159 if (mbx->mbxCommand != MBX_HEARTBEAT) {
7160 if (pmbox->vport) {
7161 lpfc_debugfs_disc_trc(pmbox->vport,
7162 LPFC_DISC_TRC_MBOX_VPORT,
7163 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7164 (uint32_t)mbx->mbxCommand,
7165 mbx->un.varWords[0], mbx->un.varWords[1]);
7166 }
7167 else {
7168 lpfc_debugfs_disc_trc(phba->pport,
7169 LPFC_DISC_TRC_MBOX,
7170 "MBOX Send: cmd:x%x mb:x%x x%x",
7171 (uint32_t)mbx->mbxCommand,
7172 mbx->un.varWords[0], mbx->un.varWords[1]);
7173 }
7174 }
7175
7176 psli->slistat.mbox_cmd++;
7177 evtctr = psli->slistat.mbox_event;
7178
7179 /* next set own bit for the adapter and copy over command word */
7180 mbx->mbxOwner = OWN_CHIP;
7181
7182 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7183 /* Populate mbox extension offset word. */
7184 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
7185 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7186 = (uint8_t *)phba->mbox_ext
7187 - (uint8_t *)phba->mbox;
7188 }
7189
7190 /* Copy the mailbox extension data */
7191 if (pmbox->in_ext_byte_len && pmbox->context2) {
7192 lpfc_sli_pcimem_bcopy(pmbox->context2,
7193 (uint8_t *)phba->mbox_ext,
7194 pmbox->in_ext_byte_len);
7195 }
7196 /* Copy command data to host SLIM area */
7197 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7198 } else {
7199 /* Populate mbox extension offset word. */
7200 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
7201 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
7202 = MAILBOX_HBA_EXT_OFFSET;
7203
7204 /* Copy the mailbox extension data */
7205 if (pmbox->in_ext_byte_len && pmbox->context2) {
7206 lpfc_memcpy_to_slim(phba->MBslimaddr +
7207 MAILBOX_HBA_EXT_OFFSET,
7208 pmbox->context2, pmbox->in_ext_byte_len);
7209
7210 }
7211 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7212 /* copy command data into host mbox for cmpl */
7213 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
7214 }
7215
7216 /* First copy mbox command data to HBA SLIM, skip past first
7217 word */
7218 to_slim = phba->MBslimaddr + sizeof (uint32_t);
7219 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
7220 MAILBOX_CMD_SIZE - sizeof (uint32_t));
7221
7222 /* Next copy over first word, with mbxOwner set */
7223 ldata = *((uint32_t *)mbx);
7224 to_slim = phba->MBslimaddr;
7225 writel(ldata, to_slim);
7226 readl(to_slim); /* flush */
7227
7228 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7229 /* switch over to host mailbox */
7230 psli->sli_flag |= LPFC_SLI_ACTIVE;
7231 }
7232 }
7233
7234 wmb();
7235
7236 switch (flag) {
7237 case MBX_NOWAIT:
7238 /* Set up reference to mailbox command */
7239 psli->mbox_active = pmbox;
7240 /* Interrupt board to do it */
7241 writel(CA_MBATT, phba->CAregaddr);
7242 readl(phba->CAregaddr); /* flush */
7243 /* Don't wait for it to finish, just return */
7244 break;
7245
7246 case MBX_POLL:
7247 /* Set up null reference to mailbox command */
7248 psli->mbox_active = NULL;
7249 /* Interrupt board to do it */
7250 writel(CA_MBATT, phba->CAregaddr);
7251 readl(phba->CAregaddr); /* flush */
7252
7253 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7254 /* First read mbox status word */
7255 word0 = *((uint32_t *)phba->mbox);
7256 word0 = le32_to_cpu(word0);
7257 } else {
7258 /* First read mbox status word */
7259 if (lpfc_readl(phba->MBslimaddr, &word0)) {
7260 spin_unlock_irqrestore(&phba->hbalock,
7261 drvr_flag);
7262 goto out_not_finished;
7263 }
7264 }
7265
7266 /* Read the HBA Host Attention Register */
7267 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7268 spin_unlock_irqrestore(&phba->hbalock,
7269 drvr_flag);
7270 goto out_not_finished;
7271 }
7272 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
7273 1000) + jiffies;
7274 i = 0;
7275 /* Wait for command to complete */
7276 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
7277 (!(ha_copy & HA_MBATT) &&
7278 (phba->link_state > LPFC_WARM_START))) {
7279 if (time_after(jiffies, timeout)) {
7280 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7281 spin_unlock_irqrestore(&phba->hbalock,
7282 drvr_flag);
7283 goto out_not_finished;
7284 }
7285
7286 /* Check if we took a mbox interrupt while we were
7287 polling */
7288 if (((word0 & OWN_CHIP) != OWN_CHIP)
7289 && (evtctr != psli->slistat.mbox_event))
7290 break;
7291
7292 if (i++ > 10) {
7293 spin_unlock_irqrestore(&phba->hbalock,
7294 drvr_flag);
7295 msleep(1);
7296 spin_lock_irqsave(&phba->hbalock, drvr_flag);
7297 }
7298
7299 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7300 /* First copy command data */
7301 word0 = *((uint32_t *)phba->mbox);
7302 word0 = le32_to_cpu(word0);
7303 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
7304 MAILBOX_t *slimmb;
7305 uint32_t slimword0;
7306 /* Check real SLIM for any errors */
7307 slimword0 = readl(phba->MBslimaddr);
7308 slimmb = (MAILBOX_t *) & slimword0;
7309 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
7310 && slimmb->mbxStatus) {
7311 psli->sli_flag &=
7312 ~LPFC_SLI_ACTIVE;
7313 word0 = slimword0;
7314 }
7315 }
7316 } else {
7317 /* First copy command data */
7318 word0 = readl(phba->MBslimaddr);
7319 }
7320 /* Read the HBA Host Attention Register */
7321 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7322 spin_unlock_irqrestore(&phba->hbalock,
7323 drvr_flag);
7324 goto out_not_finished;
7325 }
7326 }
7327
7328 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7329 /* copy results back to user */
7330 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7331 /* Copy the mailbox extension data */
7332 if (pmbox->out_ext_byte_len && pmbox->context2) {
7333 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7334 pmbox->context2,
7335 pmbox->out_ext_byte_len);
7336 }
7337 } else {
7338 /* First copy command data */
7339 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7340 MAILBOX_CMD_SIZE);
7341 /* Copy the mailbox extension data */
7342 if (pmbox->out_ext_byte_len && pmbox->context2) {
7343 lpfc_memcpy_from_slim(pmbox->context2,
7344 phba->MBslimaddr +
7345 MAILBOX_HBA_EXT_OFFSET,
7346 pmbox->out_ext_byte_len);
7347 }
7348 }
7349
7350 writel(HA_MBATT, phba->HAregaddr);
7351 readl(phba->HAregaddr); /* flush */
7352
7353 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7354 status = mbx->mbxStatus;
7355 }
7356
7357 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7358 return status;
7359
7360 out_not_finished:
7361 if (processing_queue) {
7362 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7363 lpfc_mbox_cmpl_put(phba, pmbox);
7364 }
7365 return MBX_NOT_FINISHED;
7366 }
7367
7368 /**
7369 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7370 * @phba: Pointer to HBA context object.
7371 *
7372 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7373 * the driver internal pending mailbox queue. It will then try to wait out the
7374 * possible outstanding mailbox command before return.
7375 *
7376 * Returns:
7377 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7378 * the outstanding mailbox command timed out.
7379 **/
7380 static int
7381 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7382 {
7383 struct lpfc_sli *psli = &phba->sli;
7384 int rc = 0;
7385 unsigned long timeout = 0;
7386
7387 /* Mark the asynchronous mailbox command posting as blocked */
7388 spin_lock_irq(&phba->hbalock);
7389 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7390 /* Determine how long we might wait for the active mailbox
7391 * command to be gracefully completed by firmware.
7392 */
7393 if (phba->sli.mbox_active)
7394 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7395 phba->sli.mbox_active) *
7396 1000) + jiffies;
7397 spin_unlock_irq(&phba->hbalock);
7398
7399 /* Make sure the mailbox is really active */
7400 if (timeout)
7401 lpfc_sli4_process_missed_mbox_completions(phba);
7402
7403 /* Wait for the outstnading mailbox command to complete */
7404 while (phba->sli.mbox_active) {
7405 /* Check active mailbox complete status every 2ms */
7406 msleep(2);
7407 if (time_after(jiffies, timeout)) {
7408 /* Timeout, marked the outstanding cmd not complete */
7409 rc = 1;
7410 break;
7411 }
7412 }
7413
7414 /* Can not cleanly block async mailbox command, fails it */
7415 if (rc) {
7416 spin_lock_irq(&phba->hbalock);
7417 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7418 spin_unlock_irq(&phba->hbalock);
7419 }
7420 return rc;
7421 }
7422
7423 /**
7424 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7425 * @phba: Pointer to HBA context object.
7426 *
7427 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7428 * commands from the driver internal pending mailbox queue. It makes sure
7429 * that there is no outstanding mailbox command before resuming posting
7430 * asynchronous mailbox commands. If, for any reason, there is outstanding
7431 * mailbox command, it will try to wait it out before resuming asynchronous
7432 * mailbox command posting.
7433 **/
7434 static void
7435 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7436 {
7437 struct lpfc_sli *psli = &phba->sli;
7438
7439 spin_lock_irq(&phba->hbalock);
7440 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7441 /* Asynchronous mailbox posting is not blocked, do nothing */
7442 spin_unlock_irq(&phba->hbalock);
7443 return;
7444 }
7445
7446 /* Outstanding synchronous mailbox command is guaranteed to be done,
7447 * successful or timeout, after timing-out the outstanding mailbox
7448 * command shall always be removed, so just unblock posting async
7449 * mailbox command and resume
7450 */
7451 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7452 spin_unlock_irq(&phba->hbalock);
7453
7454 /* wake up worker thread to post asynchronlous mailbox command */
7455 lpfc_worker_wake_up(phba);
7456 }
7457
7458 /**
7459 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7460 * @phba: Pointer to HBA context object.
7461 * @mboxq: Pointer to mailbox object.
7462 *
7463 * The function waits for the bootstrap mailbox register ready bit from
7464 * port for twice the regular mailbox command timeout value.
7465 *
7466 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7467 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7468 **/
7469 static int
7470 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7471 {
7472 uint32_t db_ready;
7473 unsigned long timeout;
7474 struct lpfc_register bmbx_reg;
7475
7476 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7477 * 1000) + jiffies;
7478
7479 do {
7480 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7481 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7482 if (!db_ready)
7483 msleep(2);
7484
7485 if (time_after(jiffies, timeout))
7486 return MBXERR_ERROR;
7487 } while (!db_ready);
7488
7489 return 0;
7490 }
7491
7492 /**
7493 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7494 * @phba: Pointer to HBA context object.
7495 * @mboxq: Pointer to mailbox object.
7496 *
7497 * The function posts a mailbox to the port. The mailbox is expected
7498 * to be comletely filled in and ready for the port to operate on it.
7499 * This routine executes a synchronous completion operation on the
7500 * mailbox by polling for its completion.
7501 *
7502 * The caller must not be holding any locks when calling this routine.
7503 *
7504 * Returns:
7505 * MBX_SUCCESS - mailbox posted successfully
7506 * Any of the MBX error values.
7507 **/
7508 static int
7509 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7510 {
7511 int rc = MBX_SUCCESS;
7512 unsigned long iflag;
7513 uint32_t mcqe_status;
7514 uint32_t mbx_cmnd;
7515 struct lpfc_sli *psli = &phba->sli;
7516 struct lpfc_mqe *mb = &mboxq->u.mqe;
7517 struct lpfc_bmbx_create *mbox_rgn;
7518 struct dma_address *dma_address;
7519
7520 /*
7521 * Only one mailbox can be active to the bootstrap mailbox region
7522 * at a time and there is no queueing provided.
7523 */
7524 spin_lock_irqsave(&phba->hbalock, iflag);
7525 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7526 spin_unlock_irqrestore(&phba->hbalock, iflag);
7527 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7528 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7529 "cannot issue Data: x%x x%x\n",
7530 mboxq->vport ? mboxq->vport->vpi : 0,
7531 mboxq->u.mb.mbxCommand,
7532 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7533 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7534 psli->sli_flag, MBX_POLL);
7535 return MBXERR_ERROR;
7536 }
7537 /* The server grabs the token and owns it until release */
7538 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7539 phba->sli.mbox_active = mboxq;
7540 spin_unlock_irqrestore(&phba->hbalock, iflag);
7541
7542 /* wait for bootstrap mbox register for readyness */
7543 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7544 if (rc)
7545 goto exit;
7546
7547 /*
7548 * Initialize the bootstrap memory region to avoid stale data areas
7549 * in the mailbox post. Then copy the caller's mailbox contents to
7550 * the bmbx mailbox region.
7551 */
7552 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7553 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7554 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7555 sizeof(struct lpfc_mqe));
7556
7557 /* Post the high mailbox dma address to the port and wait for ready. */
7558 dma_address = &phba->sli4_hba.bmbx.dma_address;
7559 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7560
7561 /* wait for bootstrap mbox register for hi-address write done */
7562 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7563 if (rc)
7564 goto exit;
7565
7566 /* Post the low mailbox dma address to the port. */
7567 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7568
7569 /* wait for bootstrap mbox register for low address write done */
7570 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7571 if (rc)
7572 goto exit;
7573
7574 /*
7575 * Read the CQ to ensure the mailbox has completed.
7576 * If so, update the mailbox status so that the upper layers
7577 * can complete the request normally.
7578 */
7579 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7580 sizeof(struct lpfc_mqe));
7581 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7582 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7583 sizeof(struct lpfc_mcqe));
7584 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7585 /*
7586 * When the CQE status indicates a failure and the mailbox status
7587 * indicates success then copy the CQE status into the mailbox status
7588 * (and prefix it with x4000).
7589 */
7590 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7591 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7592 bf_set(lpfc_mqe_status, mb,
7593 (LPFC_MBX_ERROR_RANGE | mcqe_status));
7594 rc = MBXERR_ERROR;
7595 } else
7596 lpfc_sli4_swap_str(phba, mboxq);
7597
7598 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7599 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7600 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7601 " x%x x%x CQ: x%x x%x x%x x%x\n",
7602 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7603 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7604 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7605 bf_get(lpfc_mqe_status, mb),
7606 mb->un.mb_words[0], mb->un.mb_words[1],
7607 mb->un.mb_words[2], mb->un.mb_words[3],
7608 mb->un.mb_words[4], mb->un.mb_words[5],
7609 mb->un.mb_words[6], mb->un.mb_words[7],
7610 mb->un.mb_words[8], mb->un.mb_words[9],
7611 mb->un.mb_words[10], mb->un.mb_words[11],
7612 mb->un.mb_words[12], mboxq->mcqe.word0,
7613 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7614 mboxq->mcqe.trailer);
7615 exit:
7616 /* We are holding the token, no needed for lock when release */
7617 spin_lock_irqsave(&phba->hbalock, iflag);
7618 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7619 phba->sli.mbox_active = NULL;
7620 spin_unlock_irqrestore(&phba->hbalock, iflag);
7621 return rc;
7622 }
7623
7624 /**
7625 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7626 * @phba: Pointer to HBA context object.
7627 * @pmbox: Pointer to mailbox object.
7628 * @flag: Flag indicating how the mailbox need to be processed.
7629 *
7630 * This function is called by discovery code and HBA management code to submit
7631 * a mailbox command to firmware with SLI-4 interface spec.
7632 *
7633 * Return codes the caller owns the mailbox command after the return of the
7634 * function.
7635 **/
7636 static int
7637 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7638 uint32_t flag)
7639 {
7640 struct lpfc_sli *psli = &phba->sli;
7641 unsigned long iflags;
7642 int rc;
7643
7644 /* dump from issue mailbox command if setup */
7645 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7646
7647 rc = lpfc_mbox_dev_check(phba);
7648 if (unlikely(rc)) {
7649 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7650 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7651 "cannot issue Data: x%x x%x\n",
7652 mboxq->vport ? mboxq->vport->vpi : 0,
7653 mboxq->u.mb.mbxCommand,
7654 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7655 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7656 psli->sli_flag, flag);
7657 goto out_not_finished;
7658 }
7659
7660 /* Detect polling mode and jump to a handler */
7661 if (!phba->sli4_hba.intr_enable) {
7662 if (flag == MBX_POLL)
7663 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7664 else
7665 rc = -EIO;
7666 if (rc != MBX_SUCCESS)
7667 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7668 "(%d):2541 Mailbox command x%x "
7669 "(x%x/x%x) failure: "
7670 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7671 "Data: x%x x%x\n,",
7672 mboxq->vport ? mboxq->vport->vpi : 0,
7673 mboxq->u.mb.mbxCommand,
7674 lpfc_sli_config_mbox_subsys_get(phba,
7675 mboxq),
7676 lpfc_sli_config_mbox_opcode_get(phba,
7677 mboxq),
7678 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7679 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7680 bf_get(lpfc_mcqe_ext_status,
7681 &mboxq->mcqe),
7682 psli->sli_flag, flag);
7683 return rc;
7684 } else if (flag == MBX_POLL) {
7685 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7686 "(%d):2542 Try to issue mailbox command "
7687 "x%x (x%x/x%x) synchronously ahead of async"
7688 "mailbox command queue: x%x x%x\n",
7689 mboxq->vport ? mboxq->vport->vpi : 0,
7690 mboxq->u.mb.mbxCommand,
7691 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7692 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7693 psli->sli_flag, flag);
7694 /* Try to block the asynchronous mailbox posting */
7695 rc = lpfc_sli4_async_mbox_block(phba);
7696 if (!rc) {
7697 /* Successfully blocked, now issue sync mbox cmd */
7698 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7699 if (rc != MBX_SUCCESS)
7700 lpfc_printf_log(phba, KERN_WARNING,
7701 LOG_MBOX | LOG_SLI,
7702 "(%d):2597 Sync Mailbox command "
7703 "x%x (x%x/x%x) failure: "
7704 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7705 "Data: x%x x%x\n,",
7706 mboxq->vport ? mboxq->vport->vpi : 0,
7707 mboxq->u.mb.mbxCommand,
7708 lpfc_sli_config_mbox_subsys_get(phba,
7709 mboxq),
7710 lpfc_sli_config_mbox_opcode_get(phba,
7711 mboxq),
7712 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7713 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7714 bf_get(lpfc_mcqe_ext_status,
7715 &mboxq->mcqe),
7716 psli->sli_flag, flag);
7717 /* Unblock the async mailbox posting afterward */
7718 lpfc_sli4_async_mbox_unblock(phba);
7719 }
7720 return rc;
7721 }
7722
7723 /* Now, interrupt mode asynchrous mailbox command */
7724 rc = lpfc_mbox_cmd_check(phba, mboxq);
7725 if (rc) {
7726 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7727 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7728 "cannot issue Data: x%x x%x\n",
7729 mboxq->vport ? mboxq->vport->vpi : 0,
7730 mboxq->u.mb.mbxCommand,
7731 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7732 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7733 psli->sli_flag, flag);
7734 goto out_not_finished;
7735 }
7736
7737 /* Put the mailbox command to the driver internal FIFO */
7738 psli->slistat.mbox_busy++;
7739 spin_lock_irqsave(&phba->hbalock, iflags);
7740 lpfc_mbox_put(phba, mboxq);
7741 spin_unlock_irqrestore(&phba->hbalock, iflags);
7742 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7743 "(%d):0354 Mbox cmd issue - Enqueue Data: "
7744 "x%x (x%x/x%x) x%x x%x x%x\n",
7745 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7746 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7747 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7748 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7749 phba->pport->port_state,
7750 psli->sli_flag, MBX_NOWAIT);
7751 /* Wake up worker thread to transport mailbox command from head */
7752 lpfc_worker_wake_up(phba);
7753
7754 return MBX_BUSY;
7755
7756 out_not_finished:
7757 return MBX_NOT_FINISHED;
7758 }
7759
7760 /**
7761 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7762 * @phba: Pointer to HBA context object.
7763 *
7764 * This function is called by worker thread to send a mailbox command to
7765 * SLI4 HBA firmware.
7766 *
7767 **/
7768 int
7769 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7770 {
7771 struct lpfc_sli *psli = &phba->sli;
7772 LPFC_MBOXQ_t *mboxq;
7773 int rc = MBX_SUCCESS;
7774 unsigned long iflags;
7775 struct lpfc_mqe *mqe;
7776 uint32_t mbx_cmnd;
7777
7778 /* Check interrupt mode before post async mailbox command */
7779 if (unlikely(!phba->sli4_hba.intr_enable))
7780 return MBX_NOT_FINISHED;
7781
7782 /* Check for mailbox command service token */
7783 spin_lock_irqsave(&phba->hbalock, iflags);
7784 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7785 spin_unlock_irqrestore(&phba->hbalock, iflags);
7786 return MBX_NOT_FINISHED;
7787 }
7788 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7789 spin_unlock_irqrestore(&phba->hbalock, iflags);
7790 return MBX_NOT_FINISHED;
7791 }
7792 if (unlikely(phba->sli.mbox_active)) {
7793 spin_unlock_irqrestore(&phba->hbalock, iflags);
7794 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7795 "0384 There is pending active mailbox cmd\n");
7796 return MBX_NOT_FINISHED;
7797 }
7798 /* Take the mailbox command service token */
7799 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7800
7801 /* Get the next mailbox command from head of queue */
7802 mboxq = lpfc_mbox_get(phba);
7803
7804 /* If no more mailbox command waiting for post, we're done */
7805 if (!mboxq) {
7806 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7807 spin_unlock_irqrestore(&phba->hbalock, iflags);
7808 return MBX_SUCCESS;
7809 }
7810 phba->sli.mbox_active = mboxq;
7811 spin_unlock_irqrestore(&phba->hbalock, iflags);
7812
7813 /* Check device readiness for posting mailbox command */
7814 rc = lpfc_mbox_dev_check(phba);
7815 if (unlikely(rc))
7816 /* Driver clean routine will clean up pending mailbox */
7817 goto out_not_finished;
7818
7819 /* Prepare the mbox command to be posted */
7820 mqe = &mboxq->u.mqe;
7821 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7822
7823 /* Start timer for the mbox_tmo and log some mailbox post messages */
7824 mod_timer(&psli->mbox_tmo, (jiffies +
7825 msecs_to_jiffies(1000 * lpfc_mbox_tmo_val(phba, mboxq))));
7826
7827 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7828 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7829 "x%x x%x\n",
7830 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7831 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7832 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7833 phba->pport->port_state, psli->sli_flag);
7834
7835 if (mbx_cmnd != MBX_HEARTBEAT) {
7836 if (mboxq->vport) {
7837 lpfc_debugfs_disc_trc(mboxq->vport,
7838 LPFC_DISC_TRC_MBOX_VPORT,
7839 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7840 mbx_cmnd, mqe->un.mb_words[0],
7841 mqe->un.mb_words[1]);
7842 } else {
7843 lpfc_debugfs_disc_trc(phba->pport,
7844 LPFC_DISC_TRC_MBOX,
7845 "MBOX Send: cmd:x%x mb:x%x x%x",
7846 mbx_cmnd, mqe->un.mb_words[0],
7847 mqe->un.mb_words[1]);
7848 }
7849 }
7850 psli->slistat.mbox_cmd++;
7851
7852 /* Post the mailbox command to the port */
7853 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7854 if (rc != MBX_SUCCESS) {
7855 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7856 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7857 "cannot issue Data: x%x x%x\n",
7858 mboxq->vport ? mboxq->vport->vpi : 0,
7859 mboxq->u.mb.mbxCommand,
7860 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7861 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7862 psli->sli_flag, MBX_NOWAIT);
7863 goto out_not_finished;
7864 }
7865
7866 return rc;
7867
7868 out_not_finished:
7869 spin_lock_irqsave(&phba->hbalock, iflags);
7870 if (phba->sli.mbox_active) {
7871 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7872 __lpfc_mbox_cmpl_put(phba, mboxq);
7873 /* Release the token */
7874 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7875 phba->sli.mbox_active = NULL;
7876 }
7877 spin_unlock_irqrestore(&phba->hbalock, iflags);
7878
7879 return MBX_NOT_FINISHED;
7880 }
7881
7882 /**
7883 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7884 * @phba: Pointer to HBA context object.
7885 * @pmbox: Pointer to mailbox object.
7886 * @flag: Flag indicating how the mailbox need to be processed.
7887 *
7888 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7889 * the API jump table function pointer from the lpfc_hba struct.
7890 *
7891 * Return codes the caller owns the mailbox command after the return of the
7892 * function.
7893 **/
7894 int
7895 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7896 {
7897 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7898 }
7899
7900 /**
7901 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7902 * @phba: The hba struct for which this call is being executed.
7903 * @dev_grp: The HBA PCI-Device group number.
7904 *
7905 * This routine sets up the mbox interface API function jump table in @phba
7906 * struct.
7907 * Returns: 0 - success, -ENODEV - failure.
7908 **/
7909 int
7910 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7911 {
7912
7913 switch (dev_grp) {
7914 case LPFC_PCI_DEV_LP:
7915 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7916 phba->lpfc_sli_handle_slow_ring_event =
7917 lpfc_sli_handle_slow_ring_event_s3;
7918 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7919 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7920 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7921 break;
7922 case LPFC_PCI_DEV_OC:
7923 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7924 phba->lpfc_sli_handle_slow_ring_event =
7925 lpfc_sli_handle_slow_ring_event_s4;
7926 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7927 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7928 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7929 break;
7930 default:
7931 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7932 "1420 Invalid HBA PCI-device group: 0x%x\n",
7933 dev_grp);
7934 return -ENODEV;
7935 break;
7936 }
7937 return 0;
7938 }
7939
7940 /**
7941 * __lpfc_sli_ringtx_put - Add an iocb to the txq
7942 * @phba: Pointer to HBA context object.
7943 * @pring: Pointer to driver SLI ring object.
7944 * @piocb: Pointer to address of newly added command iocb.
7945 *
7946 * This function is called with hbalock held to add a command
7947 * iocb to the txq when SLI layer cannot submit the command iocb
7948 * to the ring.
7949 **/
7950 void
7951 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7952 struct lpfc_iocbq *piocb)
7953 {
7954 lockdep_assert_held(&phba->hbalock);
7955 /* Insert the caller's iocb in the txq tail for later processing. */
7956 list_add_tail(&piocb->list, &pring->txq);
7957 }
7958
7959 /**
7960 * lpfc_sli_next_iocb - Get the next iocb in the txq
7961 * @phba: Pointer to HBA context object.
7962 * @pring: Pointer to driver SLI ring object.
7963 * @piocb: Pointer to address of newly added command iocb.
7964 *
7965 * This function is called with hbalock held before a new
7966 * iocb is submitted to the firmware. This function checks
7967 * txq to flush the iocbs in txq to Firmware before
7968 * submitting new iocbs to the Firmware.
7969 * If there are iocbs in the txq which need to be submitted
7970 * to firmware, lpfc_sli_next_iocb returns the first element
7971 * of the txq after dequeuing it from txq.
7972 * If there is no iocb in the txq then the function will return
7973 * *piocb and *piocb is set to NULL. Caller needs to check
7974 * *piocb to find if there are more commands in the txq.
7975 **/
7976 static struct lpfc_iocbq *
7977 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7978 struct lpfc_iocbq **piocb)
7979 {
7980 struct lpfc_iocbq * nextiocb;
7981
7982 lockdep_assert_held(&phba->hbalock);
7983
7984 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7985 if (!nextiocb) {
7986 nextiocb = *piocb;
7987 *piocb = NULL;
7988 }
7989
7990 return nextiocb;
7991 }
7992
7993 /**
7994 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7995 * @phba: Pointer to HBA context object.
7996 * @ring_number: SLI ring number to issue iocb on.
7997 * @piocb: Pointer to command iocb.
7998 * @flag: Flag indicating if this command can be put into txq.
7999 *
8000 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
8001 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
8002 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
8003 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
8004 * this function allows only iocbs for posting buffers. This function finds
8005 * next available slot in the command ring and posts the command to the
8006 * available slot and writes the port attention register to request HBA start
8007 * processing new iocb. If there is no slot available in the ring and
8008 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
8009 * the function returns IOCB_BUSY.
8010 *
8011 * This function is called with hbalock held. The function will return success
8012 * after it successfully submit the iocb to firmware or after adding to the
8013 * txq.
8014 **/
8015 static int
8016 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
8017 struct lpfc_iocbq *piocb, uint32_t flag)
8018 {
8019 struct lpfc_iocbq *nextiocb;
8020 IOCB_t *iocb;
8021 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8022
8023 lockdep_assert_held(&phba->hbalock);
8024
8025 if (piocb->iocb_cmpl && (!piocb->vport) &&
8026 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
8027 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
8028 lpfc_printf_log(phba, KERN_ERR,
8029 LOG_SLI | LOG_VPORT,
8030 "1807 IOCB x%x failed. No vport\n",
8031 piocb->iocb.ulpCommand);
8032 dump_stack();
8033 return IOCB_ERROR;
8034 }
8035
8036
8037 /* If the PCI channel is in offline state, do not post iocbs. */
8038 if (unlikely(pci_channel_offline(phba->pcidev)))
8039 return IOCB_ERROR;
8040
8041 /* If HBA has a deferred error attention, fail the iocb. */
8042 if (unlikely(phba->hba_flag & DEFER_ERATT))
8043 return IOCB_ERROR;
8044
8045 /*
8046 * We should never get an IOCB if we are in a < LINK_DOWN state
8047 */
8048 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
8049 return IOCB_ERROR;
8050
8051 /*
8052 * Check to see if we are blocking IOCB processing because of a
8053 * outstanding event.
8054 */
8055 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
8056 goto iocb_busy;
8057
8058 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
8059 /*
8060 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
8061 * can be issued if the link is not up.
8062 */
8063 switch (piocb->iocb.ulpCommand) {
8064 case CMD_GEN_REQUEST64_CR:
8065 case CMD_GEN_REQUEST64_CX:
8066 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
8067 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
8068 FC_RCTL_DD_UNSOL_CMD) ||
8069 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
8070 MENLO_TRANSPORT_TYPE))
8071
8072 goto iocb_busy;
8073 break;
8074 case CMD_QUE_RING_BUF_CN:
8075 case CMD_QUE_RING_BUF64_CN:
8076 /*
8077 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
8078 * completion, iocb_cmpl MUST be 0.
8079 */
8080 if (piocb->iocb_cmpl)
8081 piocb->iocb_cmpl = NULL;
8082 /*FALLTHROUGH*/
8083 case CMD_CREATE_XRI_CR:
8084 case CMD_CLOSE_XRI_CN:
8085 case CMD_CLOSE_XRI_CX:
8086 break;
8087 default:
8088 goto iocb_busy;
8089 }
8090
8091 /*
8092 * For FCP commands, we must be in a state where we can process link
8093 * attention events.
8094 */
8095 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
8096 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
8097 goto iocb_busy;
8098 }
8099
8100 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
8101 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
8102 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
8103
8104 if (iocb)
8105 lpfc_sli_update_ring(phba, pring);
8106 else
8107 lpfc_sli_update_full_ring(phba, pring);
8108
8109 if (!piocb)
8110 return IOCB_SUCCESS;
8111
8112 goto out_busy;
8113
8114 iocb_busy:
8115 pring->stats.iocb_cmd_delay++;
8116
8117 out_busy:
8118
8119 if (!(flag & SLI_IOCB_RET_IOCB)) {
8120 __lpfc_sli_ringtx_put(phba, pring, piocb);
8121 return IOCB_SUCCESS;
8122 }
8123
8124 return IOCB_BUSY;
8125 }
8126
8127 /**
8128 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
8129 * @phba: Pointer to HBA context object.
8130 * @piocb: Pointer to command iocb.
8131 * @sglq: Pointer to the scatter gather queue object.
8132 *
8133 * This routine converts the bpl or bde that is in the IOCB
8134 * to a sgl list for the sli4 hardware. The physical address
8135 * of the bpl/bde is converted back to a virtual address.
8136 * If the IOCB contains a BPL then the list of BDE's is
8137 * converted to sli4_sge's. If the IOCB contains a single
8138 * BDE then it is converted to a single sli_sge.
8139 * The IOCB is still in cpu endianess so the contents of
8140 * the bpl can be used without byte swapping.
8141 *
8142 * Returns valid XRI = Success, NO_XRI = Failure.
8143 **/
8144 static uint16_t
8145 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
8146 struct lpfc_sglq *sglq)
8147 {
8148 uint16_t xritag = NO_XRI;
8149 struct ulp_bde64 *bpl = NULL;
8150 struct ulp_bde64 bde;
8151 struct sli4_sge *sgl = NULL;
8152 struct lpfc_dmabuf *dmabuf;
8153 IOCB_t *icmd;
8154 int numBdes = 0;
8155 int i = 0;
8156 uint32_t offset = 0; /* accumulated offset in the sg request list */
8157 int inbound = 0; /* number of sg reply entries inbound from firmware */
8158
8159 if (!piocbq || !sglq)
8160 return xritag;
8161
8162 sgl = (struct sli4_sge *)sglq->sgl;
8163 icmd = &piocbq->iocb;
8164 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
8165 return sglq->sli4_xritag;
8166 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8167 numBdes = icmd->un.genreq64.bdl.bdeSize /
8168 sizeof(struct ulp_bde64);
8169 /* The addrHigh and addrLow fields within the IOCB
8170 * have not been byteswapped yet so there is no
8171 * need to swap them back.
8172 */
8173 if (piocbq->context3)
8174 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
8175 else
8176 return xritag;
8177
8178 bpl = (struct ulp_bde64 *)dmabuf->virt;
8179 if (!bpl)
8180 return xritag;
8181
8182 for (i = 0; i < numBdes; i++) {
8183 /* Should already be byte swapped. */
8184 sgl->addr_hi = bpl->addrHigh;
8185 sgl->addr_lo = bpl->addrLow;
8186
8187 sgl->word2 = le32_to_cpu(sgl->word2);
8188 if ((i+1) == numBdes)
8189 bf_set(lpfc_sli4_sge_last, sgl, 1);
8190 else
8191 bf_set(lpfc_sli4_sge_last, sgl, 0);
8192 /* swap the size field back to the cpu so we
8193 * can assign it to the sgl.
8194 */
8195 bde.tus.w = le32_to_cpu(bpl->tus.w);
8196 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
8197 /* The offsets in the sgl need to be accumulated
8198 * separately for the request and reply lists.
8199 * The request is always first, the reply follows.
8200 */
8201 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
8202 /* add up the reply sg entries */
8203 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
8204 inbound++;
8205 /* first inbound? reset the offset */
8206 if (inbound == 1)
8207 offset = 0;
8208 bf_set(lpfc_sli4_sge_offset, sgl, offset);
8209 bf_set(lpfc_sli4_sge_type, sgl,
8210 LPFC_SGE_TYPE_DATA);
8211 offset += bde.tus.f.bdeSize;
8212 }
8213 sgl->word2 = cpu_to_le32(sgl->word2);
8214 bpl++;
8215 sgl++;
8216 }
8217 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
8218 /* The addrHigh and addrLow fields of the BDE have not
8219 * been byteswapped yet so they need to be swapped
8220 * before putting them in the sgl.
8221 */
8222 sgl->addr_hi =
8223 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
8224 sgl->addr_lo =
8225 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
8226 sgl->word2 = le32_to_cpu(sgl->word2);
8227 bf_set(lpfc_sli4_sge_last, sgl, 1);
8228 sgl->word2 = cpu_to_le32(sgl->word2);
8229 sgl->sge_len =
8230 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
8231 }
8232 return sglq->sli4_xritag;
8233 }
8234
8235 /**
8236 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
8237 * @phba: Pointer to HBA context object.
8238 * @piocb: Pointer to command iocb.
8239 * @wqe: Pointer to the work queue entry.
8240 *
8241 * This routine converts the iocb command to its Work Queue Entry
8242 * equivalent. The wqe pointer should not have any fields set when
8243 * this routine is called because it will memcpy over them.
8244 * This routine does not set the CQ_ID or the WQEC bits in the
8245 * wqe.
8246 *
8247 * Returns: 0 = Success, IOCB_ERROR = Failure.
8248 **/
8249 static int
8250 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
8251 union lpfc_wqe *wqe)
8252 {
8253 uint32_t xmit_len = 0, total_len = 0;
8254 uint8_t ct = 0;
8255 uint32_t fip;
8256 uint32_t abort_tag;
8257 uint8_t command_type = ELS_COMMAND_NON_FIP;
8258 uint8_t cmnd;
8259 uint16_t xritag;
8260 uint16_t abrt_iotag;
8261 struct lpfc_iocbq *abrtiocbq;
8262 struct ulp_bde64 *bpl = NULL;
8263 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
8264 int numBdes, i;
8265 struct ulp_bde64 bde;
8266 struct lpfc_nodelist *ndlp;
8267 uint32_t *pcmd;
8268 uint32_t if_type;
8269
8270 fip = phba->hba_flag & HBA_FIP_SUPPORT;
8271 /* The fcp commands will set command type */
8272 if (iocbq->iocb_flag & LPFC_IO_FCP)
8273 command_type = FCP_COMMAND;
8274 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
8275 command_type = ELS_COMMAND_FIP;
8276 else
8277 command_type = ELS_COMMAND_NON_FIP;
8278
8279 if (phba->fcp_embed_io)
8280 memset(wqe, 0, sizeof(union lpfc_wqe128));
8281 /* Some of the fields are in the right position already */
8282 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
8283 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
8284 wqe->generic.wqe_com.word10 = 0;
8285
8286 abort_tag = (uint32_t) iocbq->iotag;
8287 xritag = iocbq->sli4_xritag;
8288 /* words0-2 bpl convert bde */
8289 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
8290 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8291 sizeof(struct ulp_bde64);
8292 bpl = (struct ulp_bde64 *)
8293 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
8294 if (!bpl)
8295 return IOCB_ERROR;
8296
8297 /* Should already be byte swapped. */
8298 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
8299 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
8300 /* swap the size field back to the cpu so we
8301 * can assign it to the sgl.
8302 */
8303 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
8304 xmit_len = wqe->generic.bde.tus.f.bdeSize;
8305 total_len = 0;
8306 for (i = 0; i < numBdes; i++) {
8307 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8308 total_len += bde.tus.f.bdeSize;
8309 }
8310 } else
8311 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8312
8313 iocbq->iocb.ulpIoTag = iocbq->iotag;
8314 cmnd = iocbq->iocb.ulpCommand;
8315
8316 switch (iocbq->iocb.ulpCommand) {
8317 case CMD_ELS_REQUEST64_CR:
8318 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8319 ndlp = iocbq->context_un.ndlp;
8320 else
8321 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8322 if (!iocbq->iocb.ulpLe) {
8323 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8324 "2007 Only Limited Edition cmd Format"
8325 " supported 0x%x\n",
8326 iocbq->iocb.ulpCommand);
8327 return IOCB_ERROR;
8328 }
8329
8330 wqe->els_req.payload_len = xmit_len;
8331 /* Els_reguest64 has a TMO */
8332 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8333 iocbq->iocb.ulpTimeout);
8334 /* Need a VF for word 4 set the vf bit*/
8335 bf_set(els_req64_vf, &wqe->els_req, 0);
8336 /* And a VFID for word 12 */
8337 bf_set(els_req64_vfid, &wqe->els_req, 0);
8338 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8339 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8340 iocbq->iocb.ulpContext);
8341 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8342 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8343 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8344 if (command_type == ELS_COMMAND_FIP)
8345 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8346 >> LPFC_FIP_ELS_ID_SHIFT);
8347 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8348 iocbq->context2)->virt);
8349 if_type = bf_get(lpfc_sli_intf_if_type,
8350 &phba->sli4_hba.sli_intf);
8351 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8352 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8353 *pcmd == ELS_CMD_SCR ||
8354 *pcmd == ELS_CMD_FDISC ||
8355 *pcmd == ELS_CMD_LOGO ||
8356 *pcmd == ELS_CMD_PLOGI)) {
8357 bf_set(els_req64_sp, &wqe->els_req, 1);
8358 bf_set(els_req64_sid, &wqe->els_req,
8359 iocbq->vport->fc_myDID);
8360 if ((*pcmd == ELS_CMD_FLOGI) &&
8361 !(phba->fc_topology ==
8362 LPFC_TOPOLOGY_LOOP))
8363 bf_set(els_req64_sid, &wqe->els_req, 0);
8364 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8365 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8366 phba->vpi_ids[iocbq->vport->vpi]);
8367 } else if (pcmd && iocbq->context1) {
8368 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8369 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8370 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8371 }
8372 }
8373 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8374 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8375 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8376 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8377 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8378 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8379 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8380 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8381 wqe->els_req.max_response_payload_len = total_len - xmit_len;
8382 break;
8383 case CMD_XMIT_SEQUENCE64_CX:
8384 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8385 iocbq->iocb.un.ulpWord[3]);
8386 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8387 iocbq->iocb.unsli3.rcvsli3.ox_id);
8388 /* The entire sequence is transmitted for this IOCB */
8389 xmit_len = total_len;
8390 cmnd = CMD_XMIT_SEQUENCE64_CR;
8391 if (phba->link_flag & LS_LOOPBACK_MODE)
8392 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8393 case CMD_XMIT_SEQUENCE64_CR:
8394 /* word3 iocb=io_tag32 wqe=reserved */
8395 wqe->xmit_sequence.rsvd3 = 0;
8396 /* word4 relative_offset memcpy */
8397 /* word5 r_ctl/df_ctl memcpy */
8398 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8399 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8400 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8401 LPFC_WQE_IOD_WRITE);
8402 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8403 LPFC_WQE_LENLOC_WORD12);
8404 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8405 wqe->xmit_sequence.xmit_len = xmit_len;
8406 command_type = OTHER_COMMAND;
8407 break;
8408 case CMD_XMIT_BCAST64_CN:
8409 /* word3 iocb=iotag32 wqe=seq_payload_len */
8410 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8411 /* word4 iocb=rsvd wqe=rsvd */
8412 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8413 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8414 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8415 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8416 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8417 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8418 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8419 LPFC_WQE_LENLOC_WORD3);
8420 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8421 break;
8422 case CMD_FCP_IWRITE64_CR:
8423 command_type = FCP_COMMAND_DATA_OUT;
8424 /* word3 iocb=iotag wqe=payload_offset_len */
8425 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8426 bf_set(payload_offset_len, &wqe->fcp_iwrite,
8427 xmit_len + sizeof(struct fcp_rsp));
8428 bf_set(cmd_buff_len, &wqe->fcp_iwrite,
8429 0);
8430 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8431 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8432 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8433 iocbq->iocb.ulpFCP2Rcvy);
8434 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8435 /* Always open the exchange */
8436 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8437 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8438 LPFC_WQE_LENLOC_WORD4);
8439 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8440 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8441 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8442 bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
8443 bf_set(wqe_ccpe, &wqe->fcp_iwrite.wqe_com, 1);
8444 if (iocbq->priority) {
8445 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8446 (iocbq->priority << 1));
8447 } else {
8448 bf_set(wqe_ccp, &wqe->fcp_iwrite.wqe_com,
8449 (phba->cfg_XLanePriority << 1));
8450 }
8451 }
8452 /* Note, word 10 is already initialized to 0 */
8453
8454 if (phba->fcp_embed_io) {
8455 struct lpfc_scsi_buf *lpfc_cmd;
8456 struct sli4_sge *sgl;
8457 union lpfc_wqe128 *wqe128;
8458 struct fcp_cmnd *fcp_cmnd;
8459 uint32_t *ptr;
8460
8461 /* 128 byte wqe support here */
8462 wqe128 = (union lpfc_wqe128 *)wqe;
8463
8464 lpfc_cmd = iocbq->context1;
8465 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8466 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8467
8468 /* Word 0-2 - FCP_CMND */
8469 wqe128->generic.bde.tus.f.bdeFlags =
8470 BUFF_TYPE_BDE_IMMED;
8471 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8472 wqe128->generic.bde.addrHigh = 0;
8473 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8474
8475 bf_set(wqe_wqes, &wqe128->fcp_iwrite.wqe_com, 1);
8476
8477 /* Word 22-29 FCP CMND Payload */
8478 ptr = &wqe128->words[22];
8479 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8480 }
8481 break;
8482 case CMD_FCP_IREAD64_CR:
8483 /* word3 iocb=iotag wqe=payload_offset_len */
8484 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8485 bf_set(payload_offset_len, &wqe->fcp_iread,
8486 xmit_len + sizeof(struct fcp_rsp));
8487 bf_set(cmd_buff_len, &wqe->fcp_iread,
8488 0);
8489 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8490 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8491 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8492 iocbq->iocb.ulpFCP2Rcvy);
8493 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8494 /* Always open the exchange */
8495 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8496 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8497 LPFC_WQE_LENLOC_WORD4);
8498 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8499 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8500 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8501 bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
8502 bf_set(wqe_ccpe, &wqe->fcp_iread.wqe_com, 1);
8503 if (iocbq->priority) {
8504 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8505 (iocbq->priority << 1));
8506 } else {
8507 bf_set(wqe_ccp, &wqe->fcp_iread.wqe_com,
8508 (phba->cfg_XLanePriority << 1));
8509 }
8510 }
8511 /* Note, word 10 is already initialized to 0 */
8512
8513 if (phba->fcp_embed_io) {
8514 struct lpfc_scsi_buf *lpfc_cmd;
8515 struct sli4_sge *sgl;
8516 union lpfc_wqe128 *wqe128;
8517 struct fcp_cmnd *fcp_cmnd;
8518 uint32_t *ptr;
8519
8520 /* 128 byte wqe support here */
8521 wqe128 = (union lpfc_wqe128 *)wqe;
8522
8523 lpfc_cmd = iocbq->context1;
8524 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8525 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8526
8527 /* Word 0-2 - FCP_CMND */
8528 wqe128->generic.bde.tus.f.bdeFlags =
8529 BUFF_TYPE_BDE_IMMED;
8530 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8531 wqe128->generic.bde.addrHigh = 0;
8532 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8533
8534 bf_set(wqe_wqes, &wqe128->fcp_iread.wqe_com, 1);
8535
8536 /* Word 22-29 FCP CMND Payload */
8537 ptr = &wqe128->words[22];
8538 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8539 }
8540 break;
8541 case CMD_FCP_ICMND64_CR:
8542 /* word3 iocb=iotag wqe=payload_offset_len */
8543 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8544 bf_set(payload_offset_len, &wqe->fcp_icmd,
8545 xmit_len + sizeof(struct fcp_rsp));
8546 bf_set(cmd_buff_len, &wqe->fcp_icmd,
8547 0);
8548 /* word3 iocb=IO_TAG wqe=reserved */
8549 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8550 /* Always open the exchange */
8551 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8552 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8553 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8554 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8555 LPFC_WQE_LENLOC_NONE);
8556 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8557 iocbq->iocb.ulpFCP2Rcvy);
8558 if (iocbq->iocb_flag & LPFC_IO_OAS) {
8559 bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
8560 bf_set(wqe_ccpe, &wqe->fcp_icmd.wqe_com, 1);
8561 if (iocbq->priority) {
8562 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8563 (iocbq->priority << 1));
8564 } else {
8565 bf_set(wqe_ccp, &wqe->fcp_icmd.wqe_com,
8566 (phba->cfg_XLanePriority << 1));
8567 }
8568 }
8569 /* Note, word 10 is already initialized to 0 */
8570
8571 if (phba->fcp_embed_io) {
8572 struct lpfc_scsi_buf *lpfc_cmd;
8573 struct sli4_sge *sgl;
8574 union lpfc_wqe128 *wqe128;
8575 struct fcp_cmnd *fcp_cmnd;
8576 uint32_t *ptr;
8577
8578 /* 128 byte wqe support here */
8579 wqe128 = (union lpfc_wqe128 *)wqe;
8580
8581 lpfc_cmd = iocbq->context1;
8582 sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
8583 fcp_cmnd = lpfc_cmd->fcp_cmnd;
8584
8585 /* Word 0-2 - FCP_CMND */
8586 wqe128->generic.bde.tus.f.bdeFlags =
8587 BUFF_TYPE_BDE_IMMED;
8588 wqe128->generic.bde.tus.f.bdeSize = sgl->sge_len;
8589 wqe128->generic.bde.addrHigh = 0;
8590 wqe128->generic.bde.addrLow = 88; /* Word 22 */
8591
8592 bf_set(wqe_wqes, &wqe128->fcp_icmd.wqe_com, 1);
8593
8594 /* Word 22-29 FCP CMND Payload */
8595 ptr = &wqe128->words[22];
8596 memcpy(ptr, fcp_cmnd, sizeof(struct fcp_cmnd));
8597 }
8598 break;
8599 case CMD_GEN_REQUEST64_CR:
8600 /* For this command calculate the xmit length of the
8601 * request bde.
8602 */
8603 xmit_len = 0;
8604 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8605 sizeof(struct ulp_bde64);
8606 for (i = 0; i < numBdes; i++) {
8607 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8608 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8609 break;
8610 xmit_len += bde.tus.f.bdeSize;
8611 }
8612 /* word3 iocb=IO_TAG wqe=request_payload_len */
8613 wqe->gen_req.request_payload_len = xmit_len;
8614 /* word4 iocb=parameter wqe=relative_offset memcpy */
8615 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8616 /* word6 context tag copied in memcpy */
8617 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8618 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8619 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8620 "2015 Invalid CT %x command 0x%x\n",
8621 ct, iocbq->iocb.ulpCommand);
8622 return IOCB_ERROR;
8623 }
8624 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8625 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8626 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8627 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8628 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8629 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8630 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8631 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8632 wqe->gen_req.max_response_payload_len = total_len - xmit_len;
8633 command_type = OTHER_COMMAND;
8634 break;
8635 case CMD_XMIT_ELS_RSP64_CX:
8636 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8637 /* words0-2 BDE memcpy */
8638 /* word3 iocb=iotag32 wqe=response_payload_len */
8639 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8640 /* word4 */
8641 wqe->xmit_els_rsp.word4 = 0;
8642 /* word5 iocb=rsvd wge=did */
8643 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8644 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8645
8646 if_type = bf_get(lpfc_sli_intf_if_type,
8647 &phba->sli4_hba.sli_intf);
8648 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8649 if (iocbq->vport->fc_flag & FC_PT2PT) {
8650 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8651 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8652 iocbq->vport->fc_myDID);
8653 if (iocbq->vport->fc_myDID == Fabric_DID) {
8654 bf_set(wqe_els_did,
8655 &wqe->xmit_els_rsp.wqe_dest, 0);
8656 }
8657 }
8658 }
8659 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8660 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8661 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8662 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8663 iocbq->iocb.unsli3.rcvsli3.ox_id);
8664 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8665 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8666 phba->vpi_ids[iocbq->vport->vpi]);
8667 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8668 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8669 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8670 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8671 LPFC_WQE_LENLOC_WORD3);
8672 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
8673 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8674 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8675 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8676 iocbq->context2)->virt);
8677 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8678 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8679 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8680 iocbq->vport->fc_myDID);
8681 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8682 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8683 phba->vpi_ids[phba->pport->vpi]);
8684 }
8685 command_type = OTHER_COMMAND;
8686 break;
8687 case CMD_CLOSE_XRI_CN:
8688 case CMD_ABORT_XRI_CN:
8689 case CMD_ABORT_XRI_CX:
8690 /* words 0-2 memcpy should be 0 rserved */
8691 /* port will send abts */
8692 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8693 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8694 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8695 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8696 } else
8697 fip = 0;
8698
8699 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
8700 /*
8701 * The link is down, or the command was ELS_FIP
8702 * so the fw does not need to send abts
8703 * on the wire.
8704 */
8705 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8706 else
8707 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8708 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8709 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8710 wqe->abort_cmd.rsrvd5 = 0;
8711 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
8712 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8713 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
8714 /*
8715 * The abort handler will send us CMD_ABORT_XRI_CN or
8716 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8717 */
8718 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8719 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8720 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8721 LPFC_WQE_LENLOC_NONE);
8722 cmnd = CMD_ABORT_XRI_CX;
8723 command_type = OTHER_COMMAND;
8724 xritag = 0;
8725 break;
8726 case CMD_XMIT_BLS_RSP64_CX:
8727 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8728 /* As BLS ABTS RSP WQE is very different from other WQEs,
8729 * we re-construct this WQE here based on information in
8730 * iocbq from scratch.
8731 */
8732 memset(wqe, 0, sizeof(union lpfc_wqe));
8733 /* OX_ID is invariable to who sent ABTS to CT exchange */
8734 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
8735 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8736 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
8737 LPFC_ABTS_UNSOL_INT) {
8738 /* ABTS sent by initiator to CT exchange, the
8739 * RX_ID field will be filled with the newly
8740 * allocated responder XRI.
8741 */
8742 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8743 iocbq->sli4_xritag);
8744 } else {
8745 /* ABTS sent by responder to CT exchange, the
8746 * RX_ID field will be filled with the responder
8747 * RX_ID from ABTS.
8748 */
8749 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8750 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8751 }
8752 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8753 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8754
8755 /* Use CT=VPI */
8756 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8757 ndlp->nlp_DID);
8758 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8759 iocbq->iocb.ulpContext);
8760 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8761 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8762 phba->vpi_ids[phba->pport->vpi]);
8763 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8764 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8765 LPFC_WQE_LENLOC_NONE);
8766 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8767 command_type = OTHER_COMMAND;
8768 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8769 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8770 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8771 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8772 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8773 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8774 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8775 }
8776
8777 break;
8778 case CMD_XRI_ABORTED_CX:
8779 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8780 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8781 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8782 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8783 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8784 default:
8785 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8786 "2014 Invalid command 0x%x\n",
8787 iocbq->iocb.ulpCommand);
8788 return IOCB_ERROR;
8789 break;
8790 }
8791
8792 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8793 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8794 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8795 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8796 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8797 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8798 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8799 LPFC_IO_DIF_INSERT);
8800 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8801 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8802 wqe->generic.wqe_com.abort_tag = abort_tag;
8803 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8804 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8805 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8806 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8807 return 0;
8808 }
8809
8810 /**
8811 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8812 * @phba: Pointer to HBA context object.
8813 * @ring_number: SLI ring number to issue iocb on.
8814 * @piocb: Pointer to command iocb.
8815 * @flag: Flag indicating if this command can be put into txq.
8816 *
8817 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8818 * an iocb command to an HBA with SLI-4 interface spec.
8819 *
8820 * This function is called with hbalock held. The function will return success
8821 * after it successfully submit the iocb to firmware or after adding to the
8822 * txq.
8823 **/
8824 static int
8825 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8826 struct lpfc_iocbq *piocb, uint32_t flag)
8827 {
8828 struct lpfc_sglq *sglq;
8829 union lpfc_wqe *wqe;
8830 union lpfc_wqe128 wqe128;
8831 struct lpfc_queue *wq;
8832 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8833
8834 lockdep_assert_held(&phba->hbalock);
8835
8836 /*
8837 * The WQE can be either 64 or 128 bytes,
8838 * so allocate space on the stack assuming the largest.
8839 */
8840 wqe = (union lpfc_wqe *)&wqe128;
8841
8842 if (piocb->sli4_xritag == NO_XRI) {
8843 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8844 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8845 sglq = NULL;
8846 else {
8847 if (!list_empty(&pring->txq)) {
8848 if (!(flag & SLI_IOCB_RET_IOCB)) {
8849 __lpfc_sli_ringtx_put(phba,
8850 pring, piocb);
8851 return IOCB_SUCCESS;
8852 } else {
8853 return IOCB_BUSY;
8854 }
8855 } else {
8856 sglq = __lpfc_sli_get_sglq(phba, piocb);
8857 if (!sglq) {
8858 if (!(flag & SLI_IOCB_RET_IOCB)) {
8859 __lpfc_sli_ringtx_put(phba,
8860 pring,
8861 piocb);
8862 return IOCB_SUCCESS;
8863 } else
8864 return IOCB_BUSY;
8865 }
8866 }
8867 }
8868 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
8869 /* These IO's already have an XRI and a mapped sgl. */
8870 sglq = NULL;
8871 } else {
8872 /*
8873 * This is a continuation of a commandi,(CX) so this
8874 * sglq is on the active list
8875 */
8876 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
8877 if (!sglq)
8878 return IOCB_ERROR;
8879 }
8880
8881 if (sglq) {
8882 piocb->sli4_lxritag = sglq->sli4_lxritag;
8883 piocb->sli4_xritag = sglq->sli4_xritag;
8884 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8885 return IOCB_ERROR;
8886 }
8887
8888 if (lpfc_sli4_iocb2wqe(phba, piocb, wqe))
8889 return IOCB_ERROR;
8890
8891 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8892 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8893 if (!phba->cfg_fof || (!(piocb->iocb_flag & LPFC_IO_OAS))) {
8894 wq = phba->sli4_hba.fcp_wq[piocb->fcp_wqidx];
8895 } else {
8896 wq = phba->sli4_hba.oas_wq;
8897 }
8898 if (lpfc_sli4_wq_put(wq, wqe))
8899 return IOCB_ERROR;
8900 } else {
8901 if (unlikely(!phba->sli4_hba.els_wq))
8902 return IOCB_ERROR;
8903 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, wqe))
8904 return IOCB_ERROR;
8905 }
8906 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8907
8908 return 0;
8909 }
8910
8911 /**
8912 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8913 *
8914 * This routine wraps the actual lockless version for issusing IOCB function
8915 * pointer from the lpfc_hba struct.
8916 *
8917 * Return codes:
8918 * IOCB_ERROR - Error
8919 * IOCB_SUCCESS - Success
8920 * IOCB_BUSY - Busy
8921 **/
8922 int
8923 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8924 struct lpfc_iocbq *piocb, uint32_t flag)
8925 {
8926 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8927 }
8928
8929 /**
8930 * lpfc_sli_api_table_setup - Set up sli api function jump table
8931 * @phba: The hba struct for which this call is being executed.
8932 * @dev_grp: The HBA PCI-Device group number.
8933 *
8934 * This routine sets up the SLI interface API function jump table in @phba
8935 * struct.
8936 * Returns: 0 - success, -ENODEV - failure.
8937 **/
8938 int
8939 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8940 {
8941
8942 switch (dev_grp) {
8943 case LPFC_PCI_DEV_LP:
8944 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8945 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8946 break;
8947 case LPFC_PCI_DEV_OC:
8948 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8949 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8950 break;
8951 default:
8952 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8953 "1419 Invalid HBA PCI-device group: 0x%x\n",
8954 dev_grp);
8955 return -ENODEV;
8956 break;
8957 }
8958 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8959 return 0;
8960 }
8961
8962 /**
8963 * lpfc_sli_calc_ring - Calculates which ring to use
8964 * @phba: Pointer to HBA context object.
8965 * @ring_number: Initial ring
8966 * @piocb: Pointer to command iocb.
8967 *
8968 * For SLI4, FCP IO can deferred to one fo many WQs, based on
8969 * fcp_wqidx, thus we need to calculate the corresponding ring.
8970 * Since ABORTS must go on the same WQ of the command they are
8971 * aborting, we use command's fcp_wqidx.
8972 */
8973 static int
8974 lpfc_sli_calc_ring(struct lpfc_hba *phba, uint32_t ring_number,
8975 struct lpfc_iocbq *piocb)
8976 {
8977 if (phba->sli_rev < LPFC_SLI_REV4)
8978 return ring_number;
8979
8980 if (piocb->iocb_flag & (LPFC_IO_FCP | LPFC_USE_FCPWQIDX)) {
8981 if (!(phba->cfg_fof) ||
8982 (!(piocb->iocb_flag & LPFC_IO_FOF))) {
8983 if (unlikely(!phba->sli4_hba.fcp_wq))
8984 return LPFC_HBA_ERROR;
8985 /*
8986 * for abort iocb fcp_wqidx should already
8987 * be setup based on what work queue we used.
8988 */
8989 if (!(piocb->iocb_flag & LPFC_USE_FCPWQIDX))
8990 piocb->fcp_wqidx =
8991 lpfc_sli4_scmd_to_wqidx_distr(phba,
8992 piocb->context1);
8993 ring_number = MAX_SLI3_CONFIGURED_RINGS +
8994 piocb->fcp_wqidx;
8995 } else {
8996 if (unlikely(!phba->sli4_hba.oas_wq))
8997 return LPFC_HBA_ERROR;
8998 piocb->fcp_wqidx = 0;
8999 ring_number = LPFC_FCP_OAS_RING;
9000 }
9001 }
9002 return ring_number;
9003 }
9004
9005 /**
9006 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
9007 * @phba: Pointer to HBA context object.
9008 * @pring: Pointer to driver SLI ring object.
9009 * @piocb: Pointer to command iocb.
9010 * @flag: Flag indicating if this command can be put into txq.
9011 *
9012 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
9013 * function. This function gets the hbalock and calls
9014 * __lpfc_sli_issue_iocb function and will return the error returned
9015 * by __lpfc_sli_issue_iocb function. This wrapper is used by
9016 * functions which do not hold hbalock.
9017 **/
9018 int
9019 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
9020 struct lpfc_iocbq *piocb, uint32_t flag)
9021 {
9022 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
9023 struct lpfc_sli_ring *pring;
9024 struct lpfc_queue *fpeq;
9025 struct lpfc_eqe *eqe;
9026 unsigned long iflags;
9027 int rc, idx;
9028
9029 if (phba->sli_rev == LPFC_SLI_REV4) {
9030 ring_number = lpfc_sli_calc_ring(phba, ring_number, piocb);
9031 if (unlikely(ring_number == LPFC_HBA_ERROR))
9032 return IOCB_ERROR;
9033 idx = piocb->fcp_wqidx;
9034
9035 pring = &phba->sli.ring[ring_number];
9036 spin_lock_irqsave(&pring->ring_lock, iflags);
9037 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9038 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9039
9040 if (lpfc_fcp_look_ahead && (piocb->iocb_flag & LPFC_IO_FCP)) {
9041 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
9042
9043 if (atomic_dec_and_test(&fcp_eq_hdl->
9044 fcp_eq_in_use)) {
9045
9046 /* Get associated EQ with this index */
9047 fpeq = phba->sli4_hba.hba_eq[idx];
9048
9049 /* Turn off interrupts from this EQ */
9050 lpfc_sli4_eq_clr_intr(fpeq);
9051
9052 /*
9053 * Process all the events on FCP EQ
9054 */
9055 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
9056 lpfc_sli4_hba_handle_eqe(phba,
9057 eqe, idx);
9058 fpeq->EQ_processed++;
9059 }
9060
9061 /* Always clear and re-arm the EQ */
9062 lpfc_sli4_eq_release(fpeq,
9063 LPFC_QUEUE_REARM);
9064 }
9065 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
9066 }
9067 } else {
9068 /* For now, SLI2/3 will still use hbalock */
9069 spin_lock_irqsave(&phba->hbalock, iflags);
9070 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
9071 spin_unlock_irqrestore(&phba->hbalock, iflags);
9072 }
9073 return rc;
9074 }
9075
9076 /**
9077 * lpfc_extra_ring_setup - Extra ring setup function
9078 * @phba: Pointer to HBA context object.
9079 *
9080 * This function is called while driver attaches with the
9081 * HBA to setup the extra ring. The extra ring is used
9082 * only when driver needs to support target mode functionality
9083 * or IP over FC functionalities.
9084 *
9085 * This function is called with no lock held.
9086 **/
9087 static int
9088 lpfc_extra_ring_setup( struct lpfc_hba *phba)
9089 {
9090 struct lpfc_sli *psli;
9091 struct lpfc_sli_ring *pring;
9092
9093 psli = &phba->sli;
9094
9095 /* Adjust cmd/rsp ring iocb entries more evenly */
9096
9097 /* Take some away from the FCP ring */
9098 pring = &psli->ring[psli->fcp_ring];
9099 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9100 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9101 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9102 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9103
9104 /* and give them to the extra ring */
9105 pring = &psli->ring[psli->extra_ring];
9106
9107 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9108 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9109 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9110 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9111
9112 /* Setup default profile for this ring */
9113 pring->iotag_max = 4096;
9114 pring->num_mask = 1;
9115 pring->prt[0].profile = 0; /* Mask 0 */
9116 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
9117 pring->prt[0].type = phba->cfg_multi_ring_type;
9118 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
9119 return 0;
9120 }
9121
9122 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
9123 * @phba: Pointer to HBA context object.
9124 * @iocbq: Pointer to iocb object.
9125 *
9126 * The async_event handler calls this routine when it receives
9127 * an ASYNC_STATUS_CN event from the port. The port generates
9128 * this event when an Abort Sequence request to an rport fails
9129 * twice in succession. The abort could be originated by the
9130 * driver or by the port. The ABTS could have been for an ELS
9131 * or FCP IO. The port only generates this event when an ABTS
9132 * fails to complete after one retry.
9133 */
9134 static void
9135 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
9136 struct lpfc_iocbq *iocbq)
9137 {
9138 struct lpfc_nodelist *ndlp = NULL;
9139 uint16_t rpi = 0, vpi = 0;
9140 struct lpfc_vport *vport = NULL;
9141
9142 /* The rpi in the ulpContext is vport-sensitive. */
9143 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
9144 rpi = iocbq->iocb.ulpContext;
9145
9146 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9147 "3092 Port generated ABTS async event "
9148 "on vpi %d rpi %d status 0x%x\n",
9149 vpi, rpi, iocbq->iocb.ulpStatus);
9150
9151 vport = lpfc_find_vport_by_vpid(phba, vpi);
9152 if (!vport)
9153 goto err_exit;
9154 ndlp = lpfc_findnode_rpi(vport, rpi);
9155 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
9156 goto err_exit;
9157
9158 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
9159 lpfc_sli_abts_recover_port(vport, ndlp);
9160 return;
9161
9162 err_exit:
9163 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9164 "3095 Event Context not found, no "
9165 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
9166 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
9167 vpi, rpi);
9168 }
9169
9170 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
9171 * @phba: pointer to HBA context object.
9172 * @ndlp: nodelist pointer for the impacted rport.
9173 * @axri: pointer to the wcqe containing the failed exchange.
9174 *
9175 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
9176 * port. The port generates this event when an abort exchange request to an
9177 * rport fails twice in succession with no reply. The abort could be originated
9178 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
9179 */
9180 void
9181 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
9182 struct lpfc_nodelist *ndlp,
9183 struct sli4_wcqe_xri_aborted *axri)
9184 {
9185 struct lpfc_vport *vport;
9186 uint32_t ext_status = 0;
9187
9188 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
9189 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9190 "3115 Node Context not found, driver "
9191 "ignoring abts err event\n");
9192 return;
9193 }
9194
9195 vport = ndlp->vport;
9196 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
9197 "3116 Port generated FCP XRI ABORT event on "
9198 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
9199 ndlp->vport->vpi, phba->sli4_hba.rpi_ids[ndlp->nlp_rpi],
9200 bf_get(lpfc_wcqe_xa_xri, axri),
9201 bf_get(lpfc_wcqe_xa_status, axri),
9202 axri->parameter);
9203
9204 /*
9205 * Catch the ABTS protocol failure case. Older OCe FW releases returned
9206 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
9207 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
9208 */
9209 ext_status = axri->parameter & IOERR_PARAM_MASK;
9210 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
9211 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
9212 lpfc_sli_abts_recover_port(vport, ndlp);
9213 }
9214
9215 /**
9216 * lpfc_sli_async_event_handler - ASYNC iocb handler function
9217 * @phba: Pointer to HBA context object.
9218 * @pring: Pointer to driver SLI ring object.
9219 * @iocbq: Pointer to iocb object.
9220 *
9221 * This function is called by the slow ring event handler
9222 * function when there is an ASYNC event iocb in the ring.
9223 * This function is called with no lock held.
9224 * Currently this function handles only temperature related
9225 * ASYNC events. The function decodes the temperature sensor
9226 * event message and posts events for the management applications.
9227 **/
9228 static void
9229 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
9230 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
9231 {
9232 IOCB_t *icmd;
9233 uint16_t evt_code;
9234 struct temp_event temp_event_data;
9235 struct Scsi_Host *shost;
9236 uint32_t *iocb_w;
9237
9238 icmd = &iocbq->iocb;
9239 evt_code = icmd->un.asyncstat.evt_code;
9240
9241 switch (evt_code) {
9242 case ASYNC_TEMP_WARN:
9243 case ASYNC_TEMP_SAFE:
9244 temp_event_data.data = (uint32_t) icmd->ulpContext;
9245 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
9246 if (evt_code == ASYNC_TEMP_WARN) {
9247 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
9248 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9249 "0347 Adapter is very hot, please take "
9250 "corrective action. temperature : %d Celsius\n",
9251 (uint32_t) icmd->ulpContext);
9252 } else {
9253 temp_event_data.event_code = LPFC_NORMAL_TEMP;
9254 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
9255 "0340 Adapter temperature is OK now. "
9256 "temperature : %d Celsius\n",
9257 (uint32_t) icmd->ulpContext);
9258 }
9259
9260 /* Send temperature change event to applications */
9261 shost = lpfc_shost_from_vport(phba->pport);
9262 fc_host_post_vendor_event(shost, fc_get_event_number(),
9263 sizeof(temp_event_data), (char *) &temp_event_data,
9264 LPFC_NL_VENDOR_ID);
9265 break;
9266 case ASYNC_STATUS_CN:
9267 lpfc_sli_abts_err_handler(phba, iocbq);
9268 break;
9269 default:
9270 iocb_w = (uint32_t *) icmd;
9271 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
9272 "0346 Ring %d handler: unexpected ASYNC_STATUS"
9273 " evt_code 0x%x\n"
9274 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
9275 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
9276 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
9277 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
9278 pring->ringno, icmd->un.asyncstat.evt_code,
9279 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
9280 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
9281 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
9282 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
9283
9284 break;
9285 }
9286 }
9287
9288
9289 /**
9290 * lpfc_sli_setup - SLI ring setup function
9291 * @phba: Pointer to HBA context object.
9292 *
9293 * lpfc_sli_setup sets up rings of the SLI interface with
9294 * number of iocbs per ring and iotags. This function is
9295 * called while driver attach to the HBA and before the
9296 * interrupts are enabled. So there is no need for locking.
9297 *
9298 * This function always returns 0.
9299 **/
9300 int
9301 lpfc_sli_setup(struct lpfc_hba *phba)
9302 {
9303 int i, totiocbsize = 0;
9304 struct lpfc_sli *psli = &phba->sli;
9305 struct lpfc_sli_ring *pring;
9306
9307 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
9308 if (phba->sli_rev == LPFC_SLI_REV4)
9309 psli->num_rings += phba->cfg_fcp_io_channel;
9310 psli->sli_flag = 0;
9311 psli->fcp_ring = LPFC_FCP_RING;
9312 psli->next_ring = LPFC_FCP_NEXT_RING;
9313 psli->extra_ring = LPFC_EXTRA_RING;
9314
9315 psli->iocbq_lookup = NULL;
9316 psli->iocbq_lookup_len = 0;
9317 psli->last_iotag = 0;
9318
9319 for (i = 0; i < psli->num_rings; i++) {
9320 pring = &psli->ring[i];
9321 switch (i) {
9322 case LPFC_FCP_RING: /* ring 0 - FCP */
9323 /* numCiocb and numRiocb are used in config_port */
9324 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
9325 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
9326 pring->sli.sli3.numCiocb +=
9327 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
9328 pring->sli.sli3.numRiocb +=
9329 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
9330 pring->sli.sli3.numCiocb +=
9331 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
9332 pring->sli.sli3.numRiocb +=
9333 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
9334 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9335 SLI3_IOCB_CMD_SIZE :
9336 SLI2_IOCB_CMD_SIZE;
9337 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9338 SLI3_IOCB_RSP_SIZE :
9339 SLI2_IOCB_RSP_SIZE;
9340 pring->iotag_ctr = 0;
9341 pring->iotag_max =
9342 (phba->cfg_hba_queue_depth * 2);
9343 pring->fast_iotag = pring->iotag_max;
9344 pring->num_mask = 0;
9345 break;
9346 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
9347 /* numCiocb and numRiocb are used in config_port */
9348 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
9349 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
9350 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9351 SLI3_IOCB_CMD_SIZE :
9352 SLI2_IOCB_CMD_SIZE;
9353 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9354 SLI3_IOCB_RSP_SIZE :
9355 SLI2_IOCB_RSP_SIZE;
9356 pring->iotag_max = phba->cfg_hba_queue_depth;
9357 pring->num_mask = 0;
9358 break;
9359 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
9360 /* numCiocb and numRiocb are used in config_port */
9361 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
9362 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
9363 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
9364 SLI3_IOCB_CMD_SIZE :
9365 SLI2_IOCB_CMD_SIZE;
9366 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
9367 SLI3_IOCB_RSP_SIZE :
9368 SLI2_IOCB_RSP_SIZE;
9369 pring->fast_iotag = 0;
9370 pring->iotag_ctr = 0;
9371 pring->iotag_max = 4096;
9372 pring->lpfc_sli_rcv_async_status =
9373 lpfc_sli_async_event_handler;
9374 pring->num_mask = LPFC_MAX_RING_MASK;
9375 pring->prt[0].profile = 0; /* Mask 0 */
9376 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
9377 pring->prt[0].type = FC_TYPE_ELS;
9378 pring->prt[0].lpfc_sli_rcv_unsol_event =
9379 lpfc_els_unsol_event;
9380 pring->prt[1].profile = 0; /* Mask 1 */
9381 pring->prt[1].rctl = FC_RCTL_ELS_REP;
9382 pring->prt[1].type = FC_TYPE_ELS;
9383 pring->prt[1].lpfc_sli_rcv_unsol_event =
9384 lpfc_els_unsol_event;
9385 pring->prt[2].profile = 0; /* Mask 2 */
9386 /* NameServer Inquiry */
9387 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
9388 /* NameServer */
9389 pring->prt[2].type = FC_TYPE_CT;
9390 pring->prt[2].lpfc_sli_rcv_unsol_event =
9391 lpfc_ct_unsol_event;
9392 pring->prt[3].profile = 0; /* Mask 3 */
9393 /* NameServer response */
9394 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
9395 /* NameServer */
9396 pring->prt[3].type = FC_TYPE_CT;
9397 pring->prt[3].lpfc_sli_rcv_unsol_event =
9398 lpfc_ct_unsol_event;
9399 break;
9400 }
9401 totiocbsize += (pring->sli.sli3.numCiocb *
9402 pring->sli.sli3.sizeCiocb) +
9403 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
9404 }
9405 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
9406 /* Too many cmd / rsp ring entries in SLI2 SLIM */
9407 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
9408 "SLI2 SLIM Data: x%x x%lx\n",
9409 phba->brd_no, totiocbsize,
9410 (unsigned long) MAX_SLIM_IOCB_SIZE);
9411 }
9412 if (phba->cfg_multi_ring_support == 2)
9413 lpfc_extra_ring_setup(phba);
9414
9415 return 0;
9416 }
9417
9418 /**
9419 * lpfc_sli_queue_setup - Queue initialization function
9420 * @phba: Pointer to HBA context object.
9421 *
9422 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
9423 * ring. This function also initializes ring indices of each ring.
9424 * This function is called during the initialization of the SLI
9425 * interface of an HBA.
9426 * This function is called with no lock held and always returns
9427 * 1.
9428 **/
9429 int
9430 lpfc_sli_queue_setup(struct lpfc_hba *phba)
9431 {
9432 struct lpfc_sli *psli;
9433 struct lpfc_sli_ring *pring;
9434 int i;
9435
9436 psli = &phba->sli;
9437 spin_lock_irq(&phba->hbalock);
9438 INIT_LIST_HEAD(&psli->mboxq);
9439 INIT_LIST_HEAD(&psli->mboxq_cmpl);
9440 /* Initialize list headers for txq and txcmplq as double linked lists */
9441 for (i = 0; i < psli->num_rings; i++) {
9442 pring = &psli->ring[i];
9443 pring->ringno = i;
9444 pring->sli.sli3.next_cmdidx = 0;
9445 pring->sli.sli3.local_getidx = 0;
9446 pring->sli.sli3.cmdidx = 0;
9447 pring->flag = 0;
9448 INIT_LIST_HEAD(&pring->txq);
9449 INIT_LIST_HEAD(&pring->txcmplq);
9450 INIT_LIST_HEAD(&pring->iocb_continueq);
9451 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
9452 INIT_LIST_HEAD(&pring->postbufq);
9453 spin_lock_init(&pring->ring_lock);
9454 }
9455 spin_unlock_irq(&phba->hbalock);
9456 return 1;
9457 }
9458
9459 /**
9460 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
9461 * @phba: Pointer to HBA context object.
9462 *
9463 * This routine flushes the mailbox command subsystem. It will unconditionally
9464 * flush all the mailbox commands in the three possible stages in the mailbox
9465 * command sub-system: pending mailbox command queue; the outstanding mailbox
9466 * command; and completed mailbox command queue. It is caller's responsibility
9467 * to make sure that the driver is in the proper state to flush the mailbox
9468 * command sub-system. Namely, the posting of mailbox commands into the
9469 * pending mailbox command queue from the various clients must be stopped;
9470 * either the HBA is in a state that it will never works on the outstanding
9471 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
9472 * mailbox command has been completed.
9473 **/
9474 static void
9475 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
9476 {
9477 LIST_HEAD(completions);
9478 struct lpfc_sli *psli = &phba->sli;
9479 LPFC_MBOXQ_t *pmb;
9480 unsigned long iflag;
9481
9482 /* Flush all the mailbox commands in the mbox system */
9483 spin_lock_irqsave(&phba->hbalock, iflag);
9484 /* The pending mailbox command queue */
9485 list_splice_init(&phba->sli.mboxq, &completions);
9486 /* The outstanding active mailbox command */
9487 if (psli->mbox_active) {
9488 list_add_tail(&psli->mbox_active->list, &completions);
9489 psli->mbox_active = NULL;
9490 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9491 }
9492 /* The completed mailbox command queue */
9493 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9494 spin_unlock_irqrestore(&phba->hbalock, iflag);
9495
9496 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9497 while (!list_empty(&completions)) {
9498 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9499 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9500 if (pmb->mbox_cmpl)
9501 pmb->mbox_cmpl(phba, pmb);
9502 }
9503 }
9504
9505 /**
9506 * lpfc_sli_host_down - Vport cleanup function
9507 * @vport: Pointer to virtual port object.
9508 *
9509 * lpfc_sli_host_down is called to clean up the resources
9510 * associated with a vport before destroying virtual
9511 * port data structures.
9512 * This function does following operations:
9513 * - Free discovery resources associated with this virtual
9514 * port.
9515 * - Free iocbs associated with this virtual port in
9516 * the txq.
9517 * - Send abort for all iocb commands associated with this
9518 * vport in txcmplq.
9519 *
9520 * This function is called with no lock held and always returns 1.
9521 **/
9522 int
9523 lpfc_sli_host_down(struct lpfc_vport *vport)
9524 {
9525 LIST_HEAD(completions);
9526 struct lpfc_hba *phba = vport->phba;
9527 struct lpfc_sli *psli = &phba->sli;
9528 struct lpfc_sli_ring *pring;
9529 struct lpfc_iocbq *iocb, *next_iocb;
9530 int i;
9531 unsigned long flags = 0;
9532 uint16_t prev_pring_flag;
9533
9534 lpfc_cleanup_discovery_resources(vport);
9535
9536 spin_lock_irqsave(&phba->hbalock, flags);
9537 for (i = 0; i < psli->num_rings; i++) {
9538 pring = &psli->ring[i];
9539 prev_pring_flag = pring->flag;
9540 /* Only slow rings */
9541 if (pring->ringno == LPFC_ELS_RING) {
9542 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9543 /* Set the lpfc data pending flag */
9544 set_bit(LPFC_DATA_READY, &phba->data_flags);
9545 }
9546 /*
9547 * Error everything on the txq since these iocbs have not been
9548 * given to the FW yet.
9549 */
9550 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9551 if (iocb->vport != vport)
9552 continue;
9553 list_move_tail(&iocb->list, &completions);
9554 }
9555
9556 /* Next issue ABTS for everything on the txcmplq */
9557 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9558 list) {
9559 if (iocb->vport != vport)
9560 continue;
9561 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9562 }
9563
9564 pring->flag = prev_pring_flag;
9565 }
9566
9567 spin_unlock_irqrestore(&phba->hbalock, flags);
9568
9569 /* Cancel all the IOCBs from the completions list */
9570 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9571 IOERR_SLI_DOWN);
9572 return 1;
9573 }
9574
9575 /**
9576 * lpfc_sli_hba_down - Resource cleanup function for the HBA
9577 * @phba: Pointer to HBA context object.
9578 *
9579 * This function cleans up all iocb, buffers, mailbox commands
9580 * while shutting down the HBA. This function is called with no
9581 * lock held and always returns 1.
9582 * This function does the following to cleanup driver resources:
9583 * - Free discovery resources for each virtual port
9584 * - Cleanup any pending fabric iocbs
9585 * - Iterate through the iocb txq and free each entry
9586 * in the list.
9587 * - Free up any buffer posted to the HBA
9588 * - Free mailbox commands in the mailbox queue.
9589 **/
9590 int
9591 lpfc_sli_hba_down(struct lpfc_hba *phba)
9592 {
9593 LIST_HEAD(completions);
9594 struct lpfc_sli *psli = &phba->sli;
9595 struct lpfc_sli_ring *pring;
9596 struct lpfc_dmabuf *buf_ptr;
9597 unsigned long flags = 0;
9598 int i;
9599
9600 /* Shutdown the mailbox command sub-system */
9601 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
9602
9603 lpfc_hba_down_prep(phba);
9604
9605 lpfc_fabric_abort_hba(phba);
9606
9607 spin_lock_irqsave(&phba->hbalock, flags);
9608 for (i = 0; i < psli->num_rings; i++) {
9609 pring = &psli->ring[i];
9610 /* Only slow rings */
9611 if (pring->ringno == LPFC_ELS_RING) {
9612 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9613 /* Set the lpfc data pending flag */
9614 set_bit(LPFC_DATA_READY, &phba->data_flags);
9615 }
9616
9617 /*
9618 * Error everything on the txq since these iocbs have not been
9619 * given to the FW yet.
9620 */
9621 list_splice_init(&pring->txq, &completions);
9622 }
9623 spin_unlock_irqrestore(&phba->hbalock, flags);
9624
9625 /* Cancel all the IOCBs from the completions list */
9626 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9627 IOERR_SLI_DOWN);
9628
9629 spin_lock_irqsave(&phba->hbalock, flags);
9630 list_splice_init(&phba->elsbuf, &completions);
9631 phba->elsbuf_cnt = 0;
9632 phba->elsbuf_prev_cnt = 0;
9633 spin_unlock_irqrestore(&phba->hbalock, flags);
9634
9635 while (!list_empty(&completions)) {
9636 list_remove_head(&completions, buf_ptr,
9637 struct lpfc_dmabuf, list);
9638 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9639 kfree(buf_ptr);
9640 }
9641
9642 /* Return any active mbox cmds */
9643 del_timer_sync(&psli->mbox_tmo);
9644
9645 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
9646 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9647 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
9648
9649 return 1;
9650 }
9651
9652 /**
9653 * lpfc_sli_pcimem_bcopy - SLI memory copy function
9654 * @srcp: Source memory pointer.
9655 * @destp: Destination memory pointer.
9656 * @cnt: Number of words required to be copied.
9657 *
9658 * This function is used for copying data between driver memory
9659 * and the SLI memory. This function also changes the endianness
9660 * of each word if native endianness is different from SLI
9661 * endianness. This function can be called with or without
9662 * lock.
9663 **/
9664 void
9665 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9666 {
9667 uint32_t *src = srcp;
9668 uint32_t *dest = destp;
9669 uint32_t ldata;
9670 int i;
9671
9672 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9673 ldata = *src;
9674 ldata = le32_to_cpu(ldata);
9675 *dest = ldata;
9676 src++;
9677 dest++;
9678 }
9679 }
9680
9681
9682 /**
9683 * lpfc_sli_bemem_bcopy - SLI memory copy function
9684 * @srcp: Source memory pointer.
9685 * @destp: Destination memory pointer.
9686 * @cnt: Number of words required to be copied.
9687 *
9688 * This function is used for copying data between a data structure
9689 * with big endian representation to local endianness.
9690 * This function can be called with or without lock.
9691 **/
9692 void
9693 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9694 {
9695 uint32_t *src = srcp;
9696 uint32_t *dest = destp;
9697 uint32_t ldata;
9698 int i;
9699
9700 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9701 ldata = *src;
9702 ldata = be32_to_cpu(ldata);
9703 *dest = ldata;
9704 src++;
9705 dest++;
9706 }
9707 }
9708
9709 /**
9710 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
9711 * @phba: Pointer to HBA context object.
9712 * @pring: Pointer to driver SLI ring object.
9713 * @mp: Pointer to driver buffer object.
9714 *
9715 * This function is called with no lock held.
9716 * It always return zero after adding the buffer to the postbufq
9717 * buffer list.
9718 **/
9719 int
9720 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9721 struct lpfc_dmabuf *mp)
9722 {
9723 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9724 later */
9725 spin_lock_irq(&phba->hbalock);
9726 list_add_tail(&mp->list, &pring->postbufq);
9727 pring->postbufq_cnt++;
9728 spin_unlock_irq(&phba->hbalock);
9729 return 0;
9730 }
9731
9732 /**
9733 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
9734 * @phba: Pointer to HBA context object.
9735 *
9736 * When HBQ is enabled, buffers are searched based on tags. This function
9737 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9738 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9739 * does not conflict with tags of buffer posted for unsolicited events.
9740 * The function returns the allocated tag. The function is called with
9741 * no locks held.
9742 **/
9743 uint32_t
9744 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9745 {
9746 spin_lock_irq(&phba->hbalock);
9747 phba->buffer_tag_count++;
9748 /*
9749 * Always set the QUE_BUFTAG_BIT to distiguish between
9750 * a tag assigned by HBQ.
9751 */
9752 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9753 spin_unlock_irq(&phba->hbalock);
9754 return phba->buffer_tag_count;
9755 }
9756
9757 /**
9758 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
9759 * @phba: Pointer to HBA context object.
9760 * @pring: Pointer to driver SLI ring object.
9761 * @tag: Buffer tag.
9762 *
9763 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9764 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9765 * iocb is posted to the response ring with the tag of the buffer.
9766 * This function searches the pring->postbufq list using the tag
9767 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9768 * iocb. If the buffer is found then lpfc_dmabuf object of the
9769 * buffer is returned to the caller else NULL is returned.
9770 * This function is called with no lock held.
9771 **/
9772 struct lpfc_dmabuf *
9773 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9774 uint32_t tag)
9775 {
9776 struct lpfc_dmabuf *mp, *next_mp;
9777 struct list_head *slp = &pring->postbufq;
9778
9779 /* Search postbufq, from the beginning, looking for a match on tag */
9780 spin_lock_irq(&phba->hbalock);
9781 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9782 if (mp->buffer_tag == tag) {
9783 list_del_init(&mp->list);
9784 pring->postbufq_cnt--;
9785 spin_unlock_irq(&phba->hbalock);
9786 return mp;
9787 }
9788 }
9789
9790 spin_unlock_irq(&phba->hbalock);
9791 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9792 "0402 Cannot find virtual addr for buffer tag on "
9793 "ring %d Data x%lx x%p x%p x%x\n",
9794 pring->ringno, (unsigned long) tag,
9795 slp->next, slp->prev, pring->postbufq_cnt);
9796
9797 return NULL;
9798 }
9799
9800 /**
9801 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
9802 * @phba: Pointer to HBA context object.
9803 * @pring: Pointer to driver SLI ring object.
9804 * @phys: DMA address of the buffer.
9805 *
9806 * This function searches the buffer list using the dma_address
9807 * of unsolicited event to find the driver's lpfc_dmabuf object
9808 * corresponding to the dma_address. The function returns the
9809 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9810 * This function is called by the ct and els unsolicited event
9811 * handlers to get the buffer associated with the unsolicited
9812 * event.
9813 *
9814 * This function is called with no lock held.
9815 **/
9816 struct lpfc_dmabuf *
9817 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9818 dma_addr_t phys)
9819 {
9820 struct lpfc_dmabuf *mp, *next_mp;
9821 struct list_head *slp = &pring->postbufq;
9822
9823 /* Search postbufq, from the beginning, looking for a match on phys */
9824 spin_lock_irq(&phba->hbalock);
9825 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9826 if (mp->phys == phys) {
9827 list_del_init(&mp->list);
9828 pring->postbufq_cnt--;
9829 spin_unlock_irq(&phba->hbalock);
9830 return mp;
9831 }
9832 }
9833
9834 spin_unlock_irq(&phba->hbalock);
9835 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9836 "0410 Cannot find virtual addr for mapped buf on "
9837 "ring %d Data x%llx x%p x%p x%x\n",
9838 pring->ringno, (unsigned long long)phys,
9839 slp->next, slp->prev, pring->postbufq_cnt);
9840 return NULL;
9841 }
9842
9843 /**
9844 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9845 * @phba: Pointer to HBA context object.
9846 * @cmdiocb: Pointer to driver command iocb object.
9847 * @rspiocb: Pointer to driver response iocb object.
9848 *
9849 * This function is the completion handler for the abort iocbs for
9850 * ELS commands. This function is called from the ELS ring event
9851 * handler with no lock held. This function frees memory resources
9852 * associated with the abort iocb.
9853 **/
9854 static void
9855 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9856 struct lpfc_iocbq *rspiocb)
9857 {
9858 IOCB_t *irsp = &rspiocb->iocb;
9859 uint16_t abort_iotag, abort_context;
9860 struct lpfc_iocbq *abort_iocb = NULL;
9861
9862 if (irsp->ulpStatus) {
9863
9864 /*
9865 * Assume that the port already completed and returned, or
9866 * will return the iocb. Just Log the message.
9867 */
9868 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9869 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9870
9871 spin_lock_irq(&phba->hbalock);
9872 if (phba->sli_rev < LPFC_SLI_REV4) {
9873 if (abort_iotag != 0 &&
9874 abort_iotag <= phba->sli.last_iotag)
9875 abort_iocb =
9876 phba->sli.iocbq_lookup[abort_iotag];
9877 } else
9878 /* For sli4 the abort_tag is the XRI,
9879 * so the abort routine puts the iotag of the iocb
9880 * being aborted in the context field of the abort
9881 * IOCB.
9882 */
9883 abort_iocb = phba->sli.iocbq_lookup[abort_context];
9884
9885 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9886 "0327 Cannot abort els iocb %p "
9887 "with tag %x context %x, abort status %x, "
9888 "abort code %x\n",
9889 abort_iocb, abort_iotag, abort_context,
9890 irsp->ulpStatus, irsp->un.ulpWord[4]);
9891
9892 spin_unlock_irq(&phba->hbalock);
9893 }
9894 lpfc_sli_release_iocbq(phba, cmdiocb);
9895 return;
9896 }
9897
9898 /**
9899 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9900 * @phba: Pointer to HBA context object.
9901 * @cmdiocb: Pointer to driver command iocb object.
9902 * @rspiocb: Pointer to driver response iocb object.
9903 *
9904 * The function is called from SLI ring event handler with no
9905 * lock held. This function is the completion handler for ELS commands
9906 * which are aborted. The function frees memory resources used for
9907 * the aborted ELS commands.
9908 **/
9909 static void
9910 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9911 struct lpfc_iocbq *rspiocb)
9912 {
9913 IOCB_t *irsp = &rspiocb->iocb;
9914
9915 /* ELS cmd tag <ulpIoTag> completes */
9916 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9917 "0139 Ignoring ELS cmd tag x%x completion Data: "
9918 "x%x x%x x%x\n",
9919 irsp->ulpIoTag, irsp->ulpStatus,
9920 irsp->un.ulpWord[4], irsp->ulpTimeout);
9921 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9922 lpfc_ct_free_iocb(phba, cmdiocb);
9923 else
9924 lpfc_els_free_iocb(phba, cmdiocb);
9925 return;
9926 }
9927
9928 /**
9929 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9930 * @phba: Pointer to HBA context object.
9931 * @pring: Pointer to driver SLI ring object.
9932 * @cmdiocb: Pointer to driver command iocb object.
9933 *
9934 * This function issues an abort iocb for the provided command iocb down to
9935 * the port. Other than the case the outstanding command iocb is an abort
9936 * request, this function issues abort out unconditionally. This function is
9937 * called with hbalock held. The function returns 0 when it fails due to
9938 * memory allocation failure or when the command iocb is an abort request.
9939 **/
9940 static int
9941 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9942 struct lpfc_iocbq *cmdiocb)
9943 {
9944 struct lpfc_vport *vport = cmdiocb->vport;
9945 struct lpfc_iocbq *abtsiocbp;
9946 IOCB_t *icmd = NULL;
9947 IOCB_t *iabt = NULL;
9948 int ring_number;
9949 int retval;
9950 unsigned long iflags;
9951
9952 lockdep_assert_held(&phba->hbalock);
9953
9954 /*
9955 * There are certain command types we don't want to abort. And we
9956 * don't want to abort commands that are already in the process of
9957 * being aborted.
9958 */
9959 icmd = &cmdiocb->iocb;
9960 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9961 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9962 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9963 return 0;
9964
9965 /* issue ABTS for this IOCB based on iotag */
9966 abtsiocbp = __lpfc_sli_get_iocbq(phba);
9967 if (abtsiocbp == NULL)
9968 return 0;
9969
9970 /* This signals the response to set the correct status
9971 * before calling the completion handler
9972 */
9973 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9974
9975 iabt = &abtsiocbp->iocb;
9976 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9977 iabt->un.acxri.abortContextTag = icmd->ulpContext;
9978 if (phba->sli_rev == LPFC_SLI_REV4) {
9979 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
9980 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9981 }
9982 else
9983 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
9984 iabt->ulpLe = 1;
9985 iabt->ulpClass = icmd->ulpClass;
9986
9987 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9988 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9989 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9990 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9991 if (cmdiocb->iocb_flag & LPFC_IO_FOF)
9992 abtsiocbp->iocb_flag |= LPFC_IO_FOF;
9993
9994 if (phba->link_state >= LPFC_LINK_UP)
9995 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9996 else
9997 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9998
9999 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
10000
10001 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
10002 "0339 Abort xri x%x, original iotag x%x, "
10003 "abort cmd iotag x%x\n",
10004 iabt->un.acxri.abortIoTag,
10005 iabt->un.acxri.abortContextTag,
10006 abtsiocbp->iotag);
10007
10008 if (phba->sli_rev == LPFC_SLI_REV4) {
10009 ring_number =
10010 lpfc_sli_calc_ring(phba, pring->ringno, abtsiocbp);
10011 if (unlikely(ring_number == LPFC_HBA_ERROR))
10012 return 0;
10013 pring = &phba->sli.ring[ring_number];
10014 /* Note: both hbalock and ring_lock need to be set here */
10015 spin_lock_irqsave(&pring->ring_lock, iflags);
10016 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10017 abtsiocbp, 0);
10018 spin_unlock_irqrestore(&pring->ring_lock, iflags);
10019 } else {
10020 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
10021 abtsiocbp, 0);
10022 }
10023
10024 if (retval)
10025 __lpfc_sli_release_iocbq(phba, abtsiocbp);
10026
10027 /*
10028 * Caller to this routine should check for IOCB_ERROR
10029 * and handle it properly. This routine no longer removes
10030 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10031 */
10032 return retval;
10033 }
10034
10035 /**
10036 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
10037 * @phba: Pointer to HBA context object.
10038 * @pring: Pointer to driver SLI ring object.
10039 * @cmdiocb: Pointer to driver command iocb object.
10040 *
10041 * This function issues an abort iocb for the provided command iocb. In case
10042 * of unloading, the abort iocb will not be issued to commands on the ELS
10043 * ring. Instead, the callback function shall be changed to those commands
10044 * so that nothing happens when them finishes. This function is called with
10045 * hbalock held. The function returns 0 when the command iocb is an abort
10046 * request.
10047 **/
10048 int
10049 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
10050 struct lpfc_iocbq *cmdiocb)
10051 {
10052 struct lpfc_vport *vport = cmdiocb->vport;
10053 int retval = IOCB_ERROR;
10054 IOCB_t *icmd = NULL;
10055
10056 lockdep_assert_held(&phba->hbalock);
10057
10058 /*
10059 * There are certain command types we don't want to abort. And we
10060 * don't want to abort commands that are already in the process of
10061 * being aborted.
10062 */
10063 icmd = &cmdiocb->iocb;
10064 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
10065 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
10066 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
10067 return 0;
10068
10069 /*
10070 * If we're unloading, don't abort iocb on the ELS ring, but change
10071 * the callback so that nothing happens when it finishes.
10072 */
10073 if ((vport->load_flag & FC_UNLOADING) &&
10074 (pring->ringno == LPFC_ELS_RING)) {
10075 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
10076 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
10077 else
10078 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
10079 goto abort_iotag_exit;
10080 }
10081
10082 /* Now, we try to issue the abort to the cmdiocb out */
10083 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
10084
10085 abort_iotag_exit:
10086 /*
10087 * Caller to this routine should check for IOCB_ERROR
10088 * and handle it properly. This routine no longer removes
10089 * iocb off txcmplq and call compl in case of IOCB_ERROR.
10090 */
10091 return retval;
10092 }
10093
10094 /**
10095 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
10096 * @phba: pointer to lpfc HBA data structure.
10097 *
10098 * This routine will abort all pending and outstanding iocbs to an HBA.
10099 **/
10100 void
10101 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
10102 {
10103 struct lpfc_sli *psli = &phba->sli;
10104 struct lpfc_sli_ring *pring;
10105 int i;
10106
10107 for (i = 0; i < psli->num_rings; i++) {
10108 pring = &psli->ring[i];
10109 lpfc_sli_abort_iocb_ring(phba, pring);
10110 }
10111 }
10112
10113 /**
10114 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
10115 * @iocbq: Pointer to driver iocb object.
10116 * @vport: Pointer to driver virtual port object.
10117 * @tgt_id: SCSI ID of the target.
10118 * @lun_id: LUN ID of the scsi device.
10119 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
10120 *
10121 * This function acts as an iocb filter for functions which abort or count
10122 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
10123 * 0 if the filtering criteria is met for the given iocb and will return
10124 * 1 if the filtering criteria is not met.
10125 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
10126 * given iocb is for the SCSI device specified by vport, tgt_id and
10127 * lun_id parameter.
10128 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
10129 * given iocb is for the SCSI target specified by vport and tgt_id
10130 * parameters.
10131 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
10132 * given iocb is for the SCSI host associated with the given vport.
10133 * This function is called with no locks held.
10134 **/
10135 static int
10136 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
10137 uint16_t tgt_id, uint64_t lun_id,
10138 lpfc_ctx_cmd ctx_cmd)
10139 {
10140 struct lpfc_scsi_buf *lpfc_cmd;
10141 int rc = 1;
10142
10143 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
10144 return rc;
10145
10146 if (iocbq->vport != vport)
10147 return rc;
10148
10149 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10150
10151 if (lpfc_cmd->pCmd == NULL)
10152 return rc;
10153
10154 switch (ctx_cmd) {
10155 case LPFC_CTX_LUN:
10156 if ((lpfc_cmd->rdata->pnode) &&
10157 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
10158 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
10159 rc = 0;
10160 break;
10161 case LPFC_CTX_TGT:
10162 if ((lpfc_cmd->rdata->pnode) &&
10163 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
10164 rc = 0;
10165 break;
10166 case LPFC_CTX_HOST:
10167 rc = 0;
10168 break;
10169 default:
10170 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
10171 __func__, ctx_cmd);
10172 break;
10173 }
10174
10175 return rc;
10176 }
10177
10178 /**
10179 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
10180 * @vport: Pointer to virtual port.
10181 * @tgt_id: SCSI ID of the target.
10182 * @lun_id: LUN ID of the scsi device.
10183 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10184 *
10185 * This function returns number of FCP commands pending for the vport.
10186 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
10187 * commands pending on the vport associated with SCSI device specified
10188 * by tgt_id and lun_id parameters.
10189 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
10190 * commands pending on the vport associated with SCSI target specified
10191 * by tgt_id parameter.
10192 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
10193 * commands pending on the vport.
10194 * This function returns the number of iocbs which satisfy the filter.
10195 * This function is called without any lock held.
10196 **/
10197 int
10198 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
10199 lpfc_ctx_cmd ctx_cmd)
10200 {
10201 struct lpfc_hba *phba = vport->phba;
10202 struct lpfc_iocbq *iocbq;
10203 int sum, i;
10204
10205 spin_lock_irq(&phba->hbalock);
10206 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
10207 iocbq = phba->sli.iocbq_lookup[i];
10208
10209 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
10210 ctx_cmd) == 0)
10211 sum++;
10212 }
10213 spin_unlock_irq(&phba->hbalock);
10214
10215 return sum;
10216 }
10217
10218 /**
10219 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
10220 * @phba: Pointer to HBA context object
10221 * @cmdiocb: Pointer to command iocb object.
10222 * @rspiocb: Pointer to response iocb object.
10223 *
10224 * This function is called when an aborted FCP iocb completes. This
10225 * function is called by the ring event handler with no lock held.
10226 * This function frees the iocb.
10227 **/
10228 void
10229 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
10230 struct lpfc_iocbq *rspiocb)
10231 {
10232 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10233 "3096 ABORT_XRI_CN completing on rpi x%x "
10234 "original iotag x%x, abort cmd iotag x%x "
10235 "status 0x%x, reason 0x%x\n",
10236 cmdiocb->iocb.un.acxri.abortContextTag,
10237 cmdiocb->iocb.un.acxri.abortIoTag,
10238 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
10239 rspiocb->iocb.un.ulpWord[4]);
10240 lpfc_sli_release_iocbq(phba, cmdiocb);
10241 return;
10242 }
10243
10244 /**
10245 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
10246 * @vport: Pointer to virtual port.
10247 * @pring: Pointer to driver SLI ring object.
10248 * @tgt_id: SCSI ID of the target.
10249 * @lun_id: LUN ID of the scsi device.
10250 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10251 *
10252 * This function sends an abort command for every SCSI command
10253 * associated with the given virtual port pending on the ring
10254 * filtered by lpfc_sli_validate_fcp_iocb function.
10255 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
10256 * FCP iocbs associated with lun specified by tgt_id and lun_id
10257 * parameters
10258 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
10259 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10260 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
10261 * FCP iocbs associated with virtual port.
10262 * This function returns number of iocbs it failed to abort.
10263 * This function is called with no locks held.
10264 **/
10265 int
10266 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10267 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
10268 {
10269 struct lpfc_hba *phba = vport->phba;
10270 struct lpfc_iocbq *iocbq;
10271 struct lpfc_iocbq *abtsiocb;
10272 IOCB_t *cmd = NULL;
10273 int errcnt = 0, ret_val = 0;
10274 int i;
10275
10276 for (i = 1; i <= phba->sli.last_iotag; i++) {
10277 iocbq = phba->sli.iocbq_lookup[i];
10278
10279 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10280 abort_cmd) != 0)
10281 continue;
10282
10283 /*
10284 * If the iocbq is already being aborted, don't take a second
10285 * action, but do count it.
10286 */
10287 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10288 continue;
10289
10290 /* issue ABTS for this IOCB based on iotag */
10291 abtsiocb = lpfc_sli_get_iocbq(phba);
10292 if (abtsiocb == NULL) {
10293 errcnt++;
10294 continue;
10295 }
10296
10297 /* indicate the IO is being aborted by the driver. */
10298 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10299
10300 cmd = &iocbq->iocb;
10301 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10302 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
10303 if (phba->sli_rev == LPFC_SLI_REV4)
10304 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
10305 else
10306 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
10307 abtsiocb->iocb.ulpLe = 1;
10308 abtsiocb->iocb.ulpClass = cmd->ulpClass;
10309 abtsiocb->vport = vport;
10310
10311 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10312 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
10313 if (iocbq->iocb_flag & LPFC_IO_FCP)
10314 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
10315 if (iocbq->iocb_flag & LPFC_IO_FOF)
10316 abtsiocb->iocb_flag |= LPFC_IO_FOF;
10317
10318 if (lpfc_is_link_up(phba))
10319 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10320 else
10321 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10322
10323 /* Setup callback routine and issue the command. */
10324 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10325 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
10326 abtsiocb, 0);
10327 if (ret_val == IOCB_ERROR) {
10328 lpfc_sli_release_iocbq(phba, abtsiocb);
10329 errcnt++;
10330 continue;
10331 }
10332 }
10333
10334 return errcnt;
10335 }
10336
10337 /**
10338 * lpfc_sli_abort_taskmgmt - issue abort for all commands on a host/target/LUN
10339 * @vport: Pointer to virtual port.
10340 * @pring: Pointer to driver SLI ring object.
10341 * @tgt_id: SCSI ID of the target.
10342 * @lun_id: LUN ID of the scsi device.
10343 * @taskmgmt_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
10344 *
10345 * This function sends an abort command for every SCSI command
10346 * associated with the given virtual port pending on the ring
10347 * filtered by lpfc_sli_validate_fcp_iocb function.
10348 * When taskmgmt_cmd == LPFC_CTX_LUN, the function sends abort only to the
10349 * FCP iocbs associated with lun specified by tgt_id and lun_id
10350 * parameters
10351 * When taskmgmt_cmd == LPFC_CTX_TGT, the function sends abort only to the
10352 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
10353 * When taskmgmt_cmd == LPFC_CTX_HOST, the function sends abort to all
10354 * FCP iocbs associated with virtual port.
10355 * This function returns number of iocbs it aborted .
10356 * This function is called with no locks held right after a taskmgmt
10357 * command is sent.
10358 **/
10359 int
10360 lpfc_sli_abort_taskmgmt(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
10361 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd cmd)
10362 {
10363 struct lpfc_hba *phba = vport->phba;
10364 struct lpfc_scsi_buf *lpfc_cmd;
10365 struct lpfc_iocbq *abtsiocbq;
10366 struct lpfc_nodelist *ndlp;
10367 struct lpfc_iocbq *iocbq;
10368 IOCB_t *icmd;
10369 int sum, i, ret_val;
10370 unsigned long iflags;
10371 struct lpfc_sli_ring *pring_s4;
10372 uint32_t ring_number;
10373
10374 spin_lock_irq(&phba->hbalock);
10375
10376 /* all I/Os are in process of being flushed */
10377 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
10378 spin_unlock_irq(&phba->hbalock);
10379 return 0;
10380 }
10381 sum = 0;
10382
10383 for (i = 1; i <= phba->sli.last_iotag; i++) {
10384 iocbq = phba->sli.iocbq_lookup[i];
10385
10386 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
10387 cmd) != 0)
10388 continue;
10389
10390 /*
10391 * If the iocbq is already being aborted, don't take a second
10392 * action, but do count it.
10393 */
10394 if (iocbq->iocb_flag & LPFC_DRIVER_ABORTED)
10395 continue;
10396
10397 /* issue ABTS for this IOCB based on iotag */
10398 abtsiocbq = __lpfc_sli_get_iocbq(phba);
10399 if (abtsiocbq == NULL)
10400 continue;
10401
10402 icmd = &iocbq->iocb;
10403 abtsiocbq->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
10404 abtsiocbq->iocb.un.acxri.abortContextTag = icmd->ulpContext;
10405 if (phba->sli_rev == LPFC_SLI_REV4)
10406 abtsiocbq->iocb.un.acxri.abortIoTag =
10407 iocbq->sli4_xritag;
10408 else
10409 abtsiocbq->iocb.un.acxri.abortIoTag = icmd->ulpIoTag;
10410 abtsiocbq->iocb.ulpLe = 1;
10411 abtsiocbq->iocb.ulpClass = icmd->ulpClass;
10412 abtsiocbq->vport = vport;
10413
10414 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
10415 abtsiocbq->fcp_wqidx = iocbq->fcp_wqidx;
10416 if (iocbq->iocb_flag & LPFC_IO_FCP)
10417 abtsiocbq->iocb_flag |= LPFC_USE_FCPWQIDX;
10418 if (iocbq->iocb_flag & LPFC_IO_FOF)
10419 abtsiocbq->iocb_flag |= LPFC_IO_FOF;
10420
10421 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
10422 ndlp = lpfc_cmd->rdata->pnode;
10423
10424 if (lpfc_is_link_up(phba) &&
10425 (ndlp && ndlp->nlp_state == NLP_STE_MAPPED_NODE))
10426 abtsiocbq->iocb.ulpCommand = CMD_ABORT_XRI_CN;
10427 else
10428 abtsiocbq->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
10429
10430 /* Setup callback routine and issue the command. */
10431 abtsiocbq->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
10432
10433 /*
10434 * Indicate the IO is being aborted by the driver and set
10435 * the caller's flag into the aborted IO.
10436 */
10437 iocbq->iocb_flag |= LPFC_DRIVER_ABORTED;
10438
10439 if (phba->sli_rev == LPFC_SLI_REV4) {
10440 ring_number = MAX_SLI3_CONFIGURED_RINGS +
10441 iocbq->fcp_wqidx;
10442 pring_s4 = &phba->sli.ring[ring_number];
10443 /* Note: both hbalock and ring_lock must be set here */
10444 spin_lock_irqsave(&pring_s4->ring_lock, iflags);
10445 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
10446 abtsiocbq, 0);
10447 spin_unlock_irqrestore(&pring_s4->ring_lock, iflags);
10448 } else {
10449 ret_val = __lpfc_sli_issue_iocb(phba, pring->ringno,
10450 abtsiocbq, 0);
10451 }
10452
10453
10454 if (ret_val == IOCB_ERROR)
10455 __lpfc_sli_release_iocbq(phba, abtsiocbq);
10456 else
10457 sum++;
10458 }
10459 spin_unlock_irq(&phba->hbalock);
10460 return sum;
10461 }
10462
10463 /**
10464 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
10465 * @phba: Pointer to HBA context object.
10466 * @cmdiocbq: Pointer to command iocb.
10467 * @rspiocbq: Pointer to response iocb.
10468 *
10469 * This function is the completion handler for iocbs issued using
10470 * lpfc_sli_issue_iocb_wait function. This function is called by the
10471 * ring event handler function without any lock held. This function
10472 * can be called from both worker thread context and interrupt
10473 * context. This function also can be called from other thread which
10474 * cleans up the SLI layer objects.
10475 * This function copy the contents of the response iocb to the
10476 * response iocb memory object provided by the caller of
10477 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
10478 * sleeps for the iocb completion.
10479 **/
10480 static void
10481 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
10482 struct lpfc_iocbq *cmdiocbq,
10483 struct lpfc_iocbq *rspiocbq)
10484 {
10485 wait_queue_head_t *pdone_q;
10486 unsigned long iflags;
10487 struct lpfc_scsi_buf *lpfc_cmd;
10488
10489 spin_lock_irqsave(&phba->hbalock, iflags);
10490 if (cmdiocbq->iocb_flag & LPFC_IO_WAKE_TMO) {
10491
10492 /*
10493 * A time out has occurred for the iocb. If a time out
10494 * completion handler has been supplied, call it. Otherwise,
10495 * just free the iocbq.
10496 */
10497
10498 spin_unlock_irqrestore(&phba->hbalock, iflags);
10499 cmdiocbq->iocb_cmpl = cmdiocbq->wait_iocb_cmpl;
10500 cmdiocbq->wait_iocb_cmpl = NULL;
10501 if (cmdiocbq->iocb_cmpl)
10502 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, NULL);
10503 else
10504 lpfc_sli_release_iocbq(phba, cmdiocbq);
10505 return;
10506 }
10507
10508 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
10509 if (cmdiocbq->context2 && rspiocbq)
10510 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
10511 &rspiocbq->iocb, sizeof(IOCB_t));
10512
10513 /* Set the exchange busy flag for task management commands */
10514 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
10515 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
10516 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
10517 cur_iocbq);
10518 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
10519 }
10520
10521 pdone_q = cmdiocbq->context_un.wait_queue;
10522 if (pdone_q)
10523 wake_up(pdone_q);
10524 spin_unlock_irqrestore(&phba->hbalock, iflags);
10525 return;
10526 }
10527
10528 /**
10529 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
10530 * @phba: Pointer to HBA context object..
10531 * @piocbq: Pointer to command iocb.
10532 * @flag: Flag to test.
10533 *
10534 * This routine grabs the hbalock and then test the iocb_flag to
10535 * see if the passed in flag is set.
10536 * Returns:
10537 * 1 if flag is set.
10538 * 0 if flag is not set.
10539 **/
10540 static int
10541 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
10542 struct lpfc_iocbq *piocbq, uint32_t flag)
10543 {
10544 unsigned long iflags;
10545 int ret;
10546
10547 spin_lock_irqsave(&phba->hbalock, iflags);
10548 ret = piocbq->iocb_flag & flag;
10549 spin_unlock_irqrestore(&phba->hbalock, iflags);
10550 return ret;
10551
10552 }
10553
10554 /**
10555 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
10556 * @phba: Pointer to HBA context object..
10557 * @pring: Pointer to sli ring.
10558 * @piocb: Pointer to command iocb.
10559 * @prspiocbq: Pointer to response iocb.
10560 * @timeout: Timeout in number of seconds.
10561 *
10562 * This function issues the iocb to firmware and waits for the
10563 * iocb to complete. The iocb_cmpl field of the shall be used
10564 * to handle iocbs which time out. If the field is NULL, the
10565 * function shall free the iocbq structure. If more clean up is
10566 * needed, the caller is expected to provide a completion function
10567 * that will provide the needed clean up. If the iocb command is
10568 * not completed within timeout seconds, the function will either
10569 * free the iocbq structure (if iocb_cmpl == NULL) or execute the
10570 * completion function set in the iocb_cmpl field and then return
10571 * a status of IOCB_TIMEDOUT. The caller should not free the iocb
10572 * resources if this function returns IOCB_TIMEDOUT.
10573 * The function waits for the iocb completion using an
10574 * non-interruptible wait.
10575 * This function will sleep while waiting for iocb completion.
10576 * So, this function should not be called from any context which
10577 * does not allow sleeping. Due to the same reason, this function
10578 * cannot be called with interrupt disabled.
10579 * This function assumes that the iocb completions occur while
10580 * this function sleep. So, this function cannot be called from
10581 * the thread which process iocb completion for this ring.
10582 * This function clears the iocb_flag of the iocb object before
10583 * issuing the iocb and the iocb completion handler sets this
10584 * flag and wakes this thread when the iocb completes.
10585 * The contents of the response iocb will be copied to prspiocbq
10586 * by the completion handler when the command completes.
10587 * This function returns IOCB_SUCCESS when success.
10588 * This function is called with no lock held.
10589 **/
10590 int
10591 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
10592 uint32_t ring_number,
10593 struct lpfc_iocbq *piocb,
10594 struct lpfc_iocbq *prspiocbq,
10595 uint32_t timeout)
10596 {
10597 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10598 long timeleft, timeout_req = 0;
10599 int retval = IOCB_SUCCESS;
10600 uint32_t creg_val;
10601 struct lpfc_iocbq *iocb;
10602 int txq_cnt = 0;
10603 int txcmplq_cnt = 0;
10604 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
10605 unsigned long iflags;
10606 bool iocb_completed = true;
10607
10608 /*
10609 * If the caller has provided a response iocbq buffer, then context2
10610 * is NULL or its an error.
10611 */
10612 if (prspiocbq) {
10613 if (piocb->context2)
10614 return IOCB_ERROR;
10615 piocb->context2 = prspiocbq;
10616 }
10617
10618 piocb->wait_iocb_cmpl = piocb->iocb_cmpl;
10619 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
10620 piocb->context_un.wait_queue = &done_q;
10621 piocb->iocb_flag &= ~(LPFC_IO_WAKE | LPFC_IO_WAKE_TMO);
10622
10623 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10624 if (lpfc_readl(phba->HCregaddr, &creg_val))
10625 return IOCB_ERROR;
10626 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
10627 writel(creg_val, phba->HCregaddr);
10628 readl(phba->HCregaddr); /* flush */
10629 }
10630
10631 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10632 SLI_IOCB_RET_IOCB);
10633 if (retval == IOCB_SUCCESS) {
10634 timeout_req = msecs_to_jiffies(timeout * 1000);
10635 timeleft = wait_event_timeout(done_q,
10636 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
10637 timeout_req);
10638 spin_lock_irqsave(&phba->hbalock, iflags);
10639 if (!(piocb->iocb_flag & LPFC_IO_WAKE)) {
10640
10641 /*
10642 * IOCB timed out. Inform the wake iocb wait
10643 * completion function and set local status
10644 */
10645
10646 iocb_completed = false;
10647 piocb->iocb_flag |= LPFC_IO_WAKE_TMO;
10648 }
10649 spin_unlock_irqrestore(&phba->hbalock, iflags);
10650 if (iocb_completed) {
10651 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10652 "0331 IOCB wake signaled\n");
10653 /* Note: we are not indicating if the IOCB has a success
10654 * status or not - that's for the caller to check.
10655 * IOCB_SUCCESS means just that the command was sent and
10656 * completed. Not that it completed successfully.
10657 * */
10658 } else if (timeleft == 0) {
10659 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10660 "0338 IOCB wait timeout error - no "
10661 "wake response Data x%x\n", timeout);
10662 retval = IOCB_TIMEDOUT;
10663 } else {
10664 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10665 "0330 IOCB wake NOT set, "
10666 "Data x%x x%lx\n",
10667 timeout, (timeleft / jiffies));
10668 retval = IOCB_TIMEDOUT;
10669 }
10670 } else if (retval == IOCB_BUSY) {
10671 if (phba->cfg_log_verbose & LOG_SLI) {
10672 list_for_each_entry(iocb, &pring->txq, list) {
10673 txq_cnt++;
10674 }
10675 list_for_each_entry(iocb, &pring->txcmplq, list) {
10676 txcmplq_cnt++;
10677 }
10678 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10679 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10680 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10681 }
10682 return retval;
10683 } else {
10684 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10685 "0332 IOCB wait issue failed, Data x%x\n",
10686 retval);
10687 retval = IOCB_ERROR;
10688 }
10689
10690 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10691 if (lpfc_readl(phba->HCregaddr, &creg_val))
10692 return IOCB_ERROR;
10693 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10694 writel(creg_val, phba->HCregaddr);
10695 readl(phba->HCregaddr); /* flush */
10696 }
10697
10698 if (prspiocbq)
10699 piocb->context2 = NULL;
10700
10701 piocb->context_un.wait_queue = NULL;
10702 piocb->iocb_cmpl = NULL;
10703 return retval;
10704 }
10705
10706 /**
10707 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
10708 * @phba: Pointer to HBA context object.
10709 * @pmboxq: Pointer to driver mailbox object.
10710 * @timeout: Timeout in number of seconds.
10711 *
10712 * This function issues the mailbox to firmware and waits for the
10713 * mailbox command to complete. If the mailbox command is not
10714 * completed within timeout seconds, it returns MBX_TIMEOUT.
10715 * The function waits for the mailbox completion using an
10716 * interruptible wait. If the thread is woken up due to a
10717 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10718 * should not free the mailbox resources, if this function returns
10719 * MBX_TIMEOUT.
10720 * This function will sleep while waiting for mailbox completion.
10721 * So, this function should not be called from any context which
10722 * does not allow sleeping. Due to the same reason, this function
10723 * cannot be called with interrupt disabled.
10724 * This function assumes that the mailbox completion occurs while
10725 * this function sleep. So, this function cannot be called from
10726 * the worker thread which processes mailbox completion.
10727 * This function is called in the context of HBA management
10728 * applications.
10729 * This function returns MBX_SUCCESS when successful.
10730 * This function is called with no lock held.
10731 **/
10732 int
10733 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
10734 uint32_t timeout)
10735 {
10736 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10737 MAILBOX_t *mb = NULL;
10738 int retval;
10739 unsigned long flag;
10740
10741 /* The caller might set context1 for extended buffer */
10742 if (pmboxq->context1)
10743 mb = (MAILBOX_t *)pmboxq->context1;
10744
10745 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
10746 /* setup wake call as IOCB callback */
10747 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10748 /* setup context field to pass wait_queue pointer to wake function */
10749 pmboxq->context1 = &done_q;
10750
10751 /* now issue the command */
10752 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
10753 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
10754 wait_event_interruptible_timeout(done_q,
10755 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10756 msecs_to_jiffies(timeout * 1000));
10757
10758 spin_lock_irqsave(&phba->hbalock, flag);
10759 /* restore the possible extended buffer for free resource */
10760 pmboxq->context1 = (uint8_t *)mb;
10761 /*
10762 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10763 * else do not free the resources.
10764 */
10765 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
10766 retval = MBX_SUCCESS;
10767 } else {
10768 retval = MBX_TIMEOUT;
10769 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10770 }
10771 spin_unlock_irqrestore(&phba->hbalock, flag);
10772 } else {
10773 /* restore the possible extended buffer for free resource */
10774 pmboxq->context1 = (uint8_t *)mb;
10775 }
10776
10777 return retval;
10778 }
10779
10780 /**
10781 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
10782 * @phba: Pointer to HBA context.
10783 *
10784 * This function is called to shutdown the driver's mailbox sub-system.
10785 * It first marks the mailbox sub-system is in a block state to prevent
10786 * the asynchronous mailbox command from issued off the pending mailbox
10787 * command queue. If the mailbox command sub-system shutdown is due to
10788 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10789 * the mailbox sub-system flush routine to forcefully bring down the
10790 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10791 * as with offline or HBA function reset), this routine will wait for the
10792 * outstanding mailbox command to complete before invoking the mailbox
10793 * sub-system flush routine to gracefully bring down mailbox sub-system.
10794 **/
10795 void
10796 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
10797 {
10798 struct lpfc_sli *psli = &phba->sli;
10799 unsigned long timeout;
10800
10801 if (mbx_action == LPFC_MBX_NO_WAIT) {
10802 /* delay 100ms for port state */
10803 msleep(100);
10804 lpfc_sli_mbox_sys_flush(phba);
10805 return;
10806 }
10807 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
10808
10809 spin_lock_irq(&phba->hbalock);
10810 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10811
10812 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
10813 /* Determine how long we might wait for the active mailbox
10814 * command to be gracefully completed by firmware.
10815 */
10816 if (phba->sli.mbox_active)
10817 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10818 phba->sli.mbox_active) *
10819 1000) + jiffies;
10820 spin_unlock_irq(&phba->hbalock);
10821
10822 while (phba->sli.mbox_active) {
10823 /* Check active mailbox complete status every 2ms */
10824 msleep(2);
10825 if (time_after(jiffies, timeout))
10826 /* Timeout, let the mailbox flush routine to
10827 * forcefully release active mailbox command
10828 */
10829 break;
10830 }
10831 } else
10832 spin_unlock_irq(&phba->hbalock);
10833
10834 lpfc_sli_mbox_sys_flush(phba);
10835 }
10836
10837 /**
10838 * lpfc_sli_eratt_read - read sli-3 error attention events
10839 * @phba: Pointer to HBA context.
10840 *
10841 * This function is called to read the SLI3 device error attention registers
10842 * for possible error attention events. The caller must hold the hostlock
10843 * with spin_lock_irq().
10844 *
10845 * This function returns 1 when there is Error Attention in the Host Attention
10846 * Register and returns 0 otherwise.
10847 **/
10848 static int
10849 lpfc_sli_eratt_read(struct lpfc_hba *phba)
10850 {
10851 uint32_t ha_copy;
10852
10853 /* Read chip Host Attention (HA) register */
10854 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10855 goto unplug_err;
10856
10857 if (ha_copy & HA_ERATT) {
10858 /* Read host status register to retrieve error event */
10859 if (lpfc_sli_read_hs(phba))
10860 goto unplug_err;
10861
10862 /* Check if there is a deferred error condition is active */
10863 if ((HS_FFER1 & phba->work_hs) &&
10864 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10865 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
10866 phba->hba_flag |= DEFER_ERATT;
10867 /* Clear all interrupt enable conditions */
10868 writel(0, phba->HCregaddr);
10869 readl(phba->HCregaddr);
10870 }
10871
10872 /* Set the driver HA work bitmap */
10873 phba->work_ha |= HA_ERATT;
10874 /* Indicate polling handles this ERATT */
10875 phba->hba_flag |= HBA_ERATT_HANDLED;
10876 return 1;
10877 }
10878 return 0;
10879
10880 unplug_err:
10881 /* Set the driver HS work bitmap */
10882 phba->work_hs |= UNPLUG_ERR;
10883 /* Set the driver HA work bitmap */
10884 phba->work_ha |= HA_ERATT;
10885 /* Indicate polling handles this ERATT */
10886 phba->hba_flag |= HBA_ERATT_HANDLED;
10887 return 1;
10888 }
10889
10890 /**
10891 * lpfc_sli4_eratt_read - read sli-4 error attention events
10892 * @phba: Pointer to HBA context.
10893 *
10894 * This function is called to read the SLI4 device error attention registers
10895 * for possible error attention events. The caller must hold the hostlock
10896 * with spin_lock_irq().
10897 *
10898 * This function returns 1 when there is Error Attention in the Host Attention
10899 * Register and returns 0 otherwise.
10900 **/
10901 static int
10902 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10903 {
10904 uint32_t uerr_sta_hi, uerr_sta_lo;
10905 uint32_t if_type, portsmphr;
10906 struct lpfc_register portstat_reg;
10907
10908 /*
10909 * For now, use the SLI4 device internal unrecoverable error
10910 * registers for error attention. This can be changed later.
10911 */
10912 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10913 switch (if_type) {
10914 case LPFC_SLI_INTF_IF_TYPE_0:
10915 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10916 &uerr_sta_lo) ||
10917 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10918 &uerr_sta_hi)) {
10919 phba->work_hs |= UNPLUG_ERR;
10920 phba->work_ha |= HA_ERATT;
10921 phba->hba_flag |= HBA_ERATT_HANDLED;
10922 return 1;
10923 }
10924 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10925 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10926 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10927 "1423 HBA Unrecoverable error: "
10928 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10929 "ue_mask_lo_reg=0x%x, "
10930 "ue_mask_hi_reg=0x%x\n",
10931 uerr_sta_lo, uerr_sta_hi,
10932 phba->sli4_hba.ue_mask_lo,
10933 phba->sli4_hba.ue_mask_hi);
10934 phba->work_status[0] = uerr_sta_lo;
10935 phba->work_status[1] = uerr_sta_hi;
10936 phba->work_ha |= HA_ERATT;
10937 phba->hba_flag |= HBA_ERATT_HANDLED;
10938 return 1;
10939 }
10940 break;
10941 case LPFC_SLI_INTF_IF_TYPE_2:
10942 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10943 &portstat_reg.word0) ||
10944 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10945 &portsmphr)){
10946 phba->work_hs |= UNPLUG_ERR;
10947 phba->work_ha |= HA_ERATT;
10948 phba->hba_flag |= HBA_ERATT_HANDLED;
10949 return 1;
10950 }
10951 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10952 phba->work_status[0] =
10953 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10954 phba->work_status[1] =
10955 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10957 "2885 Port Status Event: "
10958 "port status reg 0x%x, "
10959 "port smphr reg 0x%x, "
10960 "error 1=0x%x, error 2=0x%x\n",
10961 portstat_reg.word0,
10962 portsmphr,
10963 phba->work_status[0],
10964 phba->work_status[1]);
10965 phba->work_ha |= HA_ERATT;
10966 phba->hba_flag |= HBA_ERATT_HANDLED;
10967 return 1;
10968 }
10969 break;
10970 case LPFC_SLI_INTF_IF_TYPE_1:
10971 default:
10972 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10973 "2886 HBA Error Attention on unsupported "
10974 "if type %d.", if_type);
10975 return 1;
10976 }
10977
10978 return 0;
10979 }
10980
10981 /**
10982 * lpfc_sli_check_eratt - check error attention events
10983 * @phba: Pointer to HBA context.
10984 *
10985 * This function is called from timer soft interrupt context to check HBA's
10986 * error attention register bit for error attention events.
10987 *
10988 * This function returns 1 when there is Error Attention in the Host Attention
10989 * Register and returns 0 otherwise.
10990 **/
10991 int
10992 lpfc_sli_check_eratt(struct lpfc_hba *phba)
10993 {
10994 uint32_t ha_copy;
10995
10996 /* If somebody is waiting to handle an eratt, don't process it
10997 * here. The brdkill function will do this.
10998 */
10999 if (phba->link_flag & LS_IGNORE_ERATT)
11000 return 0;
11001
11002 /* Check if interrupt handler handles this ERATT */
11003 spin_lock_irq(&phba->hbalock);
11004 if (phba->hba_flag & HBA_ERATT_HANDLED) {
11005 /* Interrupt handler has handled ERATT */
11006 spin_unlock_irq(&phba->hbalock);
11007 return 0;
11008 }
11009
11010 /*
11011 * If there is deferred error attention, do not check for error
11012 * attention
11013 */
11014 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11015 spin_unlock_irq(&phba->hbalock);
11016 return 0;
11017 }
11018
11019 /* If PCI channel is offline, don't process it */
11020 if (unlikely(pci_channel_offline(phba->pcidev))) {
11021 spin_unlock_irq(&phba->hbalock);
11022 return 0;
11023 }
11024
11025 switch (phba->sli_rev) {
11026 case LPFC_SLI_REV2:
11027 case LPFC_SLI_REV3:
11028 /* Read chip Host Attention (HA) register */
11029 ha_copy = lpfc_sli_eratt_read(phba);
11030 break;
11031 case LPFC_SLI_REV4:
11032 /* Read device Uncoverable Error (UERR) registers */
11033 ha_copy = lpfc_sli4_eratt_read(phba);
11034 break;
11035 default:
11036 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
11037 "0299 Invalid SLI revision (%d)\n",
11038 phba->sli_rev);
11039 ha_copy = 0;
11040 break;
11041 }
11042 spin_unlock_irq(&phba->hbalock);
11043
11044 return ha_copy;
11045 }
11046
11047 /**
11048 * lpfc_intr_state_check - Check device state for interrupt handling
11049 * @phba: Pointer to HBA context.
11050 *
11051 * This inline routine checks whether a device or its PCI slot is in a state
11052 * that the interrupt should be handled.
11053 *
11054 * This function returns 0 if the device or the PCI slot is in a state that
11055 * interrupt should be handled, otherwise -EIO.
11056 */
11057 static inline int
11058 lpfc_intr_state_check(struct lpfc_hba *phba)
11059 {
11060 /* If the pci channel is offline, ignore all the interrupts */
11061 if (unlikely(pci_channel_offline(phba->pcidev)))
11062 return -EIO;
11063
11064 /* Update device level interrupt statistics */
11065 phba->sli.slistat.sli_intr++;
11066
11067 /* Ignore all interrupts during initialization. */
11068 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
11069 return -EIO;
11070
11071 return 0;
11072 }
11073
11074 /**
11075 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
11076 * @irq: Interrupt number.
11077 * @dev_id: The device context pointer.
11078 *
11079 * This function is directly called from the PCI layer as an interrupt
11080 * service routine when device with SLI-3 interface spec is enabled with
11081 * MSI-X multi-message interrupt mode and there are slow-path events in
11082 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
11083 * interrupt mode, this function is called as part of the device-level
11084 * interrupt handler. When the PCI slot is in error recovery or the HBA
11085 * is undergoing initialization, the interrupt handler will not process
11086 * the interrupt. The link attention and ELS ring attention events are
11087 * handled by the worker thread. The interrupt handler signals the worker
11088 * thread and returns for these events. This function is called without
11089 * any lock held. It gets the hbalock to access and update SLI data
11090 * structures.
11091 *
11092 * This function returns IRQ_HANDLED when interrupt is handled else it
11093 * returns IRQ_NONE.
11094 **/
11095 irqreturn_t
11096 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
11097 {
11098 struct lpfc_hba *phba;
11099 uint32_t ha_copy, hc_copy;
11100 uint32_t work_ha_copy;
11101 unsigned long status;
11102 unsigned long iflag;
11103 uint32_t control;
11104
11105 MAILBOX_t *mbox, *pmbox;
11106 struct lpfc_vport *vport;
11107 struct lpfc_nodelist *ndlp;
11108 struct lpfc_dmabuf *mp;
11109 LPFC_MBOXQ_t *pmb;
11110 int rc;
11111
11112 /*
11113 * Get the driver's phba structure from the dev_id and
11114 * assume the HBA is not interrupting.
11115 */
11116 phba = (struct lpfc_hba *)dev_id;
11117
11118 if (unlikely(!phba))
11119 return IRQ_NONE;
11120
11121 /*
11122 * Stuff needs to be attented to when this function is invoked as an
11123 * individual interrupt handler in MSI-X multi-message interrupt mode
11124 */
11125 if (phba->intr_type == MSIX) {
11126 /* Check device state for handling interrupt */
11127 if (lpfc_intr_state_check(phba))
11128 return IRQ_NONE;
11129 /* Need to read HA REG for slow-path events */
11130 spin_lock_irqsave(&phba->hbalock, iflag);
11131 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11132 goto unplug_error;
11133 /* If somebody is waiting to handle an eratt don't process it
11134 * here. The brdkill function will do this.
11135 */
11136 if (phba->link_flag & LS_IGNORE_ERATT)
11137 ha_copy &= ~HA_ERATT;
11138 /* Check the need for handling ERATT in interrupt handler */
11139 if (ha_copy & HA_ERATT) {
11140 if (phba->hba_flag & HBA_ERATT_HANDLED)
11141 /* ERATT polling has handled ERATT */
11142 ha_copy &= ~HA_ERATT;
11143 else
11144 /* Indicate interrupt handler handles ERATT */
11145 phba->hba_flag |= HBA_ERATT_HANDLED;
11146 }
11147
11148 /*
11149 * If there is deferred error attention, do not check for any
11150 * interrupt.
11151 */
11152 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11153 spin_unlock_irqrestore(&phba->hbalock, iflag);
11154 return IRQ_NONE;
11155 }
11156
11157 /* Clear up only attention source related to slow-path */
11158 if (lpfc_readl(phba->HCregaddr, &hc_copy))
11159 goto unplug_error;
11160
11161 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
11162 HC_LAINT_ENA | HC_ERINT_ENA),
11163 phba->HCregaddr);
11164 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
11165 phba->HAregaddr);
11166 writel(hc_copy, phba->HCregaddr);
11167 readl(phba->HAregaddr); /* flush */
11168 spin_unlock_irqrestore(&phba->hbalock, iflag);
11169 } else
11170 ha_copy = phba->ha_copy;
11171
11172 work_ha_copy = ha_copy & phba->work_ha_mask;
11173
11174 if (work_ha_copy) {
11175 if (work_ha_copy & HA_LATT) {
11176 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
11177 /*
11178 * Turn off Link Attention interrupts
11179 * until CLEAR_LA done
11180 */
11181 spin_lock_irqsave(&phba->hbalock, iflag);
11182 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
11183 if (lpfc_readl(phba->HCregaddr, &control))
11184 goto unplug_error;
11185 control &= ~HC_LAINT_ENA;
11186 writel(control, phba->HCregaddr);
11187 readl(phba->HCregaddr); /* flush */
11188 spin_unlock_irqrestore(&phba->hbalock, iflag);
11189 }
11190 else
11191 work_ha_copy &= ~HA_LATT;
11192 }
11193
11194 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
11195 /*
11196 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
11197 * the only slow ring.
11198 */
11199 status = (work_ha_copy &
11200 (HA_RXMASK << (4*LPFC_ELS_RING)));
11201 status >>= (4*LPFC_ELS_RING);
11202 if (status & HA_RXMASK) {
11203 spin_lock_irqsave(&phba->hbalock, iflag);
11204 if (lpfc_readl(phba->HCregaddr, &control))
11205 goto unplug_error;
11206
11207 lpfc_debugfs_slow_ring_trc(phba,
11208 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
11209 control, status,
11210 (uint32_t)phba->sli.slistat.sli_intr);
11211
11212 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
11213 lpfc_debugfs_slow_ring_trc(phba,
11214 "ISR Disable ring:"
11215 "pwork:x%x hawork:x%x wait:x%x",
11216 phba->work_ha, work_ha_copy,
11217 (uint32_t)((unsigned long)
11218 &phba->work_waitq));
11219
11220 control &=
11221 ~(HC_R0INT_ENA << LPFC_ELS_RING);
11222 writel(control, phba->HCregaddr);
11223 readl(phba->HCregaddr); /* flush */
11224 }
11225 else {
11226 lpfc_debugfs_slow_ring_trc(phba,
11227 "ISR slow ring: pwork:"
11228 "x%x hawork:x%x wait:x%x",
11229 phba->work_ha, work_ha_copy,
11230 (uint32_t)((unsigned long)
11231 &phba->work_waitq));
11232 }
11233 spin_unlock_irqrestore(&phba->hbalock, iflag);
11234 }
11235 }
11236 spin_lock_irqsave(&phba->hbalock, iflag);
11237 if (work_ha_copy & HA_ERATT) {
11238 if (lpfc_sli_read_hs(phba))
11239 goto unplug_error;
11240 /*
11241 * Check if there is a deferred error condition
11242 * is active
11243 */
11244 if ((HS_FFER1 & phba->work_hs) &&
11245 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
11246 HS_FFER6 | HS_FFER7 | HS_FFER8) &
11247 phba->work_hs)) {
11248 phba->hba_flag |= DEFER_ERATT;
11249 /* Clear all interrupt enable conditions */
11250 writel(0, phba->HCregaddr);
11251 readl(phba->HCregaddr);
11252 }
11253 }
11254
11255 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
11256 pmb = phba->sli.mbox_active;
11257 pmbox = &pmb->u.mb;
11258 mbox = phba->mbox;
11259 vport = pmb->vport;
11260
11261 /* First check out the status word */
11262 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
11263 if (pmbox->mbxOwner != OWN_HOST) {
11264 spin_unlock_irqrestore(&phba->hbalock, iflag);
11265 /*
11266 * Stray Mailbox Interrupt, mbxCommand <cmd>
11267 * mbxStatus <status>
11268 */
11269 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11270 LOG_SLI,
11271 "(%d):0304 Stray Mailbox "
11272 "Interrupt mbxCommand x%x "
11273 "mbxStatus x%x\n",
11274 (vport ? vport->vpi : 0),
11275 pmbox->mbxCommand,
11276 pmbox->mbxStatus);
11277 /* clear mailbox attention bit */
11278 work_ha_copy &= ~HA_MBATT;
11279 } else {
11280 phba->sli.mbox_active = NULL;
11281 spin_unlock_irqrestore(&phba->hbalock, iflag);
11282 phba->last_completion_time = jiffies;
11283 del_timer(&phba->sli.mbox_tmo);
11284 if (pmb->mbox_cmpl) {
11285 lpfc_sli_pcimem_bcopy(mbox, pmbox,
11286 MAILBOX_CMD_SIZE);
11287 if (pmb->out_ext_byte_len &&
11288 pmb->context2)
11289 lpfc_sli_pcimem_bcopy(
11290 phba->mbox_ext,
11291 pmb->context2,
11292 pmb->out_ext_byte_len);
11293 }
11294 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11295 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11296
11297 lpfc_debugfs_disc_trc(vport,
11298 LPFC_DISC_TRC_MBOX_VPORT,
11299 "MBOX dflt rpi: : "
11300 "status:x%x rpi:x%x",
11301 (uint32_t)pmbox->mbxStatus,
11302 pmbox->un.varWords[0], 0);
11303
11304 if (!pmbox->mbxStatus) {
11305 mp = (struct lpfc_dmabuf *)
11306 (pmb->context1);
11307 ndlp = (struct lpfc_nodelist *)
11308 pmb->context2;
11309
11310 /* Reg_LOGIN of dflt RPI was
11311 * successful. new lets get
11312 * rid of the RPI using the
11313 * same mbox buffer.
11314 */
11315 lpfc_unreg_login(phba,
11316 vport->vpi,
11317 pmbox->un.varWords[0],
11318 pmb);
11319 pmb->mbox_cmpl =
11320 lpfc_mbx_cmpl_dflt_rpi;
11321 pmb->context1 = mp;
11322 pmb->context2 = ndlp;
11323 pmb->vport = vport;
11324 rc = lpfc_sli_issue_mbox(phba,
11325 pmb,
11326 MBX_NOWAIT);
11327 if (rc != MBX_BUSY)
11328 lpfc_printf_log(phba,
11329 KERN_ERR,
11330 LOG_MBOX | LOG_SLI,
11331 "0350 rc should have"
11332 "been MBX_BUSY\n");
11333 if (rc != MBX_NOT_FINISHED)
11334 goto send_current_mbox;
11335 }
11336 }
11337 spin_lock_irqsave(
11338 &phba->pport->work_port_lock,
11339 iflag);
11340 phba->pport->work_port_events &=
11341 ~WORKER_MBOX_TMO;
11342 spin_unlock_irqrestore(
11343 &phba->pport->work_port_lock,
11344 iflag);
11345 lpfc_mbox_cmpl_put(phba, pmb);
11346 }
11347 } else
11348 spin_unlock_irqrestore(&phba->hbalock, iflag);
11349
11350 if ((work_ha_copy & HA_MBATT) &&
11351 (phba->sli.mbox_active == NULL)) {
11352 send_current_mbox:
11353 /* Process next mailbox command if there is one */
11354 do {
11355 rc = lpfc_sli_issue_mbox(phba, NULL,
11356 MBX_NOWAIT);
11357 } while (rc == MBX_NOT_FINISHED);
11358 if (rc != MBX_SUCCESS)
11359 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11360 LOG_SLI, "0349 rc should be "
11361 "MBX_SUCCESS\n");
11362 }
11363
11364 spin_lock_irqsave(&phba->hbalock, iflag);
11365 phba->work_ha |= work_ha_copy;
11366 spin_unlock_irqrestore(&phba->hbalock, iflag);
11367 lpfc_worker_wake_up(phba);
11368 }
11369 return IRQ_HANDLED;
11370 unplug_error:
11371 spin_unlock_irqrestore(&phba->hbalock, iflag);
11372 return IRQ_HANDLED;
11373
11374 } /* lpfc_sli_sp_intr_handler */
11375
11376 /**
11377 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
11378 * @irq: Interrupt number.
11379 * @dev_id: The device context pointer.
11380 *
11381 * This function is directly called from the PCI layer as an interrupt
11382 * service routine when device with SLI-3 interface spec is enabled with
11383 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11384 * ring event in the HBA. However, when the device is enabled with either
11385 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11386 * device-level interrupt handler. When the PCI slot is in error recovery
11387 * or the HBA is undergoing initialization, the interrupt handler will not
11388 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11389 * the intrrupt context. This function is called without any lock held.
11390 * It gets the hbalock to access and update SLI data structures.
11391 *
11392 * This function returns IRQ_HANDLED when interrupt is handled else it
11393 * returns IRQ_NONE.
11394 **/
11395 irqreturn_t
11396 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
11397 {
11398 struct lpfc_hba *phba;
11399 uint32_t ha_copy;
11400 unsigned long status;
11401 unsigned long iflag;
11402
11403 /* Get the driver's phba structure from the dev_id and
11404 * assume the HBA is not interrupting.
11405 */
11406 phba = (struct lpfc_hba *) dev_id;
11407
11408 if (unlikely(!phba))
11409 return IRQ_NONE;
11410
11411 /*
11412 * Stuff needs to be attented to when this function is invoked as an
11413 * individual interrupt handler in MSI-X multi-message interrupt mode
11414 */
11415 if (phba->intr_type == MSIX) {
11416 /* Check device state for handling interrupt */
11417 if (lpfc_intr_state_check(phba))
11418 return IRQ_NONE;
11419 /* Need to read HA REG for FCP ring and other ring events */
11420 if (lpfc_readl(phba->HAregaddr, &ha_copy))
11421 return IRQ_HANDLED;
11422 /* Clear up only attention source related to fast-path */
11423 spin_lock_irqsave(&phba->hbalock, iflag);
11424 /*
11425 * If there is deferred error attention, do not check for
11426 * any interrupt.
11427 */
11428 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11429 spin_unlock_irqrestore(&phba->hbalock, iflag);
11430 return IRQ_NONE;
11431 }
11432 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
11433 phba->HAregaddr);
11434 readl(phba->HAregaddr); /* flush */
11435 spin_unlock_irqrestore(&phba->hbalock, iflag);
11436 } else
11437 ha_copy = phba->ha_copy;
11438
11439 /*
11440 * Process all events on FCP ring. Take the optimized path for FCP IO.
11441 */
11442 ha_copy &= ~(phba->work_ha_mask);
11443
11444 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11445 status >>= (4*LPFC_FCP_RING);
11446 if (status & HA_RXMASK)
11447 lpfc_sli_handle_fast_ring_event(phba,
11448 &phba->sli.ring[LPFC_FCP_RING],
11449 status);
11450
11451 if (phba->cfg_multi_ring_support == 2) {
11452 /*
11453 * Process all events on extra ring. Take the optimized path
11454 * for extra ring IO.
11455 */
11456 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11457 status >>= (4*LPFC_EXTRA_RING);
11458 if (status & HA_RXMASK) {
11459 lpfc_sli_handle_fast_ring_event(phba,
11460 &phba->sli.ring[LPFC_EXTRA_RING],
11461 status);
11462 }
11463 }
11464 return IRQ_HANDLED;
11465 } /* lpfc_sli_fp_intr_handler */
11466
11467 /**
11468 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
11469 * @irq: Interrupt number.
11470 * @dev_id: The device context pointer.
11471 *
11472 * This function is the HBA device-level interrupt handler to device with
11473 * SLI-3 interface spec, called from the PCI layer when either MSI or
11474 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
11475 * requires driver attention. This function invokes the slow-path interrupt
11476 * attention handling function and fast-path interrupt attention handling
11477 * function in turn to process the relevant HBA attention events. This
11478 * function is called without any lock held. It gets the hbalock to access
11479 * and update SLI data structures.
11480 *
11481 * This function returns IRQ_HANDLED when interrupt is handled, else it
11482 * returns IRQ_NONE.
11483 **/
11484 irqreturn_t
11485 lpfc_sli_intr_handler(int irq, void *dev_id)
11486 {
11487 struct lpfc_hba *phba;
11488 irqreturn_t sp_irq_rc, fp_irq_rc;
11489 unsigned long status1, status2;
11490 uint32_t hc_copy;
11491
11492 /*
11493 * Get the driver's phba structure from the dev_id and
11494 * assume the HBA is not interrupting.
11495 */
11496 phba = (struct lpfc_hba *) dev_id;
11497
11498 if (unlikely(!phba))
11499 return IRQ_NONE;
11500
11501 /* Check device state for handling interrupt */
11502 if (lpfc_intr_state_check(phba))
11503 return IRQ_NONE;
11504
11505 spin_lock(&phba->hbalock);
11506 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
11507 spin_unlock(&phba->hbalock);
11508 return IRQ_HANDLED;
11509 }
11510
11511 if (unlikely(!phba->ha_copy)) {
11512 spin_unlock(&phba->hbalock);
11513 return IRQ_NONE;
11514 } else if (phba->ha_copy & HA_ERATT) {
11515 if (phba->hba_flag & HBA_ERATT_HANDLED)
11516 /* ERATT polling has handled ERATT */
11517 phba->ha_copy &= ~HA_ERATT;
11518 else
11519 /* Indicate interrupt handler handles ERATT */
11520 phba->hba_flag |= HBA_ERATT_HANDLED;
11521 }
11522
11523 /*
11524 * If there is deferred error attention, do not check for any interrupt.
11525 */
11526 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
11527 spin_unlock(&phba->hbalock);
11528 return IRQ_NONE;
11529 }
11530
11531 /* Clear attention sources except link and error attentions */
11532 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
11533 spin_unlock(&phba->hbalock);
11534 return IRQ_HANDLED;
11535 }
11536 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
11537 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
11538 phba->HCregaddr);
11539 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
11540 writel(hc_copy, phba->HCregaddr);
11541 readl(phba->HAregaddr); /* flush */
11542 spin_unlock(&phba->hbalock);
11543
11544 /*
11545 * Invokes slow-path host attention interrupt handling as appropriate.
11546 */
11547
11548 /* status of events with mailbox and link attention */
11549 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
11550
11551 /* status of events with ELS ring */
11552 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
11553 status2 >>= (4*LPFC_ELS_RING);
11554
11555 if (status1 || (status2 & HA_RXMASK))
11556 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
11557 else
11558 sp_irq_rc = IRQ_NONE;
11559
11560 /*
11561 * Invoke fast-path host attention interrupt handling as appropriate.
11562 */
11563
11564 /* status of events with FCP ring */
11565 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
11566 status1 >>= (4*LPFC_FCP_RING);
11567
11568 /* status of events with extra ring */
11569 if (phba->cfg_multi_ring_support == 2) {
11570 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
11571 status2 >>= (4*LPFC_EXTRA_RING);
11572 } else
11573 status2 = 0;
11574
11575 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
11576 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
11577 else
11578 fp_irq_rc = IRQ_NONE;
11579
11580 /* Return device-level interrupt handling status */
11581 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
11582 } /* lpfc_sli_intr_handler */
11583
11584 /**
11585 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
11586 * @phba: pointer to lpfc hba data structure.
11587 *
11588 * This routine is invoked by the worker thread to process all the pending
11589 * SLI4 FCP abort XRI events.
11590 **/
11591 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
11592 {
11593 struct lpfc_cq_event *cq_event;
11594
11595 /* First, declare the fcp xri abort event has been handled */
11596 spin_lock_irq(&phba->hbalock);
11597 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
11598 spin_unlock_irq(&phba->hbalock);
11599 /* Now, handle all the fcp xri abort events */
11600 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
11601 /* Get the first event from the head of the event queue */
11602 spin_lock_irq(&phba->hbalock);
11603 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
11604 cq_event, struct lpfc_cq_event, list);
11605 spin_unlock_irq(&phba->hbalock);
11606 /* Notify aborted XRI for FCP work queue */
11607 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11608 /* Free the event processed back to the free pool */
11609 lpfc_sli4_cq_event_release(phba, cq_event);
11610 }
11611 }
11612
11613 /**
11614 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
11615 * @phba: pointer to lpfc hba data structure.
11616 *
11617 * This routine is invoked by the worker thread to process all the pending
11618 * SLI4 els abort xri events.
11619 **/
11620 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
11621 {
11622 struct lpfc_cq_event *cq_event;
11623
11624 /* First, declare the els xri abort event has been handled */
11625 spin_lock_irq(&phba->hbalock);
11626 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
11627 spin_unlock_irq(&phba->hbalock);
11628 /* Now, handle all the els xri abort events */
11629 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
11630 /* Get the first event from the head of the event queue */
11631 spin_lock_irq(&phba->hbalock);
11632 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
11633 cq_event, struct lpfc_cq_event, list);
11634 spin_unlock_irq(&phba->hbalock);
11635 /* Notify aborted XRI for ELS work queue */
11636 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
11637 /* Free the event processed back to the free pool */
11638 lpfc_sli4_cq_event_release(phba, cq_event);
11639 }
11640 }
11641
11642 /**
11643 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
11644 * @phba: pointer to lpfc hba data structure
11645 * @pIocbIn: pointer to the rspiocbq
11646 * @pIocbOut: pointer to the cmdiocbq
11647 * @wcqe: pointer to the complete wcqe
11648 *
11649 * This routine transfers the fields of a command iocbq to a response iocbq
11650 * by copying all the IOCB fields from command iocbq and transferring the
11651 * completion status information from the complete wcqe.
11652 **/
11653 static void
11654 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11655 struct lpfc_iocbq *pIocbIn,
11656 struct lpfc_iocbq *pIocbOut,
11657 struct lpfc_wcqe_complete *wcqe)
11658 {
11659 int numBdes, i;
11660 unsigned long iflags;
11661 uint32_t status, max_response;
11662 struct lpfc_dmabuf *dmabuf;
11663 struct ulp_bde64 *bpl, bde;
11664 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11665
11666 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11667 sizeof(struct lpfc_iocbq) - offset);
11668 /* Map WCQE parameters into irspiocb parameters */
11669 status = bf_get(lpfc_wcqe_c_status, wcqe);
11670 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
11671 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11672 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11673 pIocbIn->iocb.un.fcpi.fcpi_parm =
11674 pIocbOut->iocb.un.fcpi.fcpi_parm -
11675 wcqe->total_data_placed;
11676 else
11677 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11678 else {
11679 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11680 switch (pIocbOut->iocb.ulpCommand) {
11681 case CMD_ELS_REQUEST64_CR:
11682 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11683 bpl = (struct ulp_bde64 *)dmabuf->virt;
11684 bde.tus.w = le32_to_cpu(bpl[1].tus.w);
11685 max_response = bde.tus.f.bdeSize;
11686 break;
11687 case CMD_GEN_REQUEST64_CR:
11688 max_response = 0;
11689 if (!pIocbOut->context3)
11690 break;
11691 numBdes = pIocbOut->iocb.un.genreq64.bdl.bdeSize/
11692 sizeof(struct ulp_bde64);
11693 dmabuf = (struct lpfc_dmabuf *)pIocbOut->context3;
11694 bpl = (struct ulp_bde64 *)dmabuf->virt;
11695 for (i = 0; i < numBdes; i++) {
11696 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
11697 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
11698 max_response += bde.tus.f.bdeSize;
11699 }
11700 break;
11701 default:
11702 max_response = wcqe->total_data_placed;
11703 break;
11704 }
11705 if (max_response < wcqe->total_data_placed)
11706 pIocbIn->iocb.un.genreq64.bdl.bdeSize = max_response;
11707 else
11708 pIocbIn->iocb.un.genreq64.bdl.bdeSize =
11709 wcqe->total_data_placed;
11710 }
11711
11712 /* Convert BG errors for completion status */
11713 if (status == CQE_STATUS_DI_ERROR) {
11714 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11715
11716 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11717 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11718 else
11719 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11720
11721 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11722 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11723 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11724 BGS_GUARD_ERR_MASK;
11725 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11726 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11727 BGS_APPTAG_ERR_MASK;
11728 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11729 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11730 BGS_REFTAG_ERR_MASK;
11731
11732 /* Check to see if there was any good data before the error */
11733 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11734 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11735 BGS_HI_WATER_MARK_PRESENT_MASK;
11736 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11737 wcqe->total_data_placed;
11738 }
11739
11740 /*
11741 * Set ALL the error bits to indicate we don't know what
11742 * type of error it is.
11743 */
11744 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11745 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11746 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11747 BGS_GUARD_ERR_MASK);
11748 }
11749
11750 /* Pick up HBA exchange busy condition */
11751 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11752 spin_lock_irqsave(&phba->hbalock, iflags);
11753 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11754 spin_unlock_irqrestore(&phba->hbalock, iflags);
11755 }
11756 }
11757
11758 /**
11759 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11760 * @phba: Pointer to HBA context object.
11761 * @wcqe: Pointer to work-queue completion queue entry.
11762 *
11763 * This routine handles an ELS work-queue completion event and construct
11764 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11765 * discovery engine to handle.
11766 *
11767 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11768 **/
11769 static struct lpfc_iocbq *
11770 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11771 struct lpfc_iocbq *irspiocbq)
11772 {
11773 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11774 struct lpfc_iocbq *cmdiocbq;
11775 struct lpfc_wcqe_complete *wcqe;
11776 unsigned long iflags;
11777
11778 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
11779 spin_lock_irqsave(&pring->ring_lock, iflags);
11780 pring->stats.iocb_event++;
11781 /* Look up the ELS command IOCB and create pseudo response IOCB */
11782 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11783 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11784 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11785
11786 if (unlikely(!cmdiocbq)) {
11787 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11788 "0386 ELS complete with no corresponding "
11789 "cmdiocb: iotag (%d)\n",
11790 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11791 lpfc_sli_release_iocbq(phba, irspiocbq);
11792 return NULL;
11793 }
11794
11795 /* Fake the irspiocbq and copy necessary response information */
11796 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
11797
11798 return irspiocbq;
11799 }
11800
11801 /**
11802 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11803 * @phba: Pointer to HBA context object.
11804 * @cqe: Pointer to mailbox completion queue entry.
11805 *
11806 * This routine process a mailbox completion queue entry with asynchrous
11807 * event.
11808 *
11809 * Return: true if work posted to worker thread, otherwise false.
11810 **/
11811 static bool
11812 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11813 {
11814 struct lpfc_cq_event *cq_event;
11815 unsigned long iflags;
11816
11817 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11818 "0392 Async Event: word0:x%x, word1:x%x, "
11819 "word2:x%x, word3:x%x\n", mcqe->word0,
11820 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11821
11822 /* Allocate a new internal CQ_EVENT entry */
11823 cq_event = lpfc_sli4_cq_event_alloc(phba);
11824 if (!cq_event) {
11825 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11826 "0394 Failed to allocate CQ_EVENT entry\n");
11827 return false;
11828 }
11829
11830 /* Move the CQE into an asynchronous event entry */
11831 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11832 spin_lock_irqsave(&phba->hbalock, iflags);
11833 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11834 /* Set the async event flag */
11835 phba->hba_flag |= ASYNC_EVENT;
11836 spin_unlock_irqrestore(&phba->hbalock, iflags);
11837
11838 return true;
11839 }
11840
11841 /**
11842 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11843 * @phba: Pointer to HBA context object.
11844 * @cqe: Pointer to mailbox completion queue entry.
11845 *
11846 * This routine process a mailbox completion queue entry with mailbox
11847 * completion event.
11848 *
11849 * Return: true if work posted to worker thread, otherwise false.
11850 **/
11851 static bool
11852 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11853 {
11854 uint32_t mcqe_status;
11855 MAILBOX_t *mbox, *pmbox;
11856 struct lpfc_mqe *mqe;
11857 struct lpfc_vport *vport;
11858 struct lpfc_nodelist *ndlp;
11859 struct lpfc_dmabuf *mp;
11860 unsigned long iflags;
11861 LPFC_MBOXQ_t *pmb;
11862 bool workposted = false;
11863 int rc;
11864
11865 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11866 if (!bf_get(lpfc_trailer_completed, mcqe))
11867 goto out_no_mqe_complete;
11868
11869 /* Get the reference to the active mbox command */
11870 spin_lock_irqsave(&phba->hbalock, iflags);
11871 pmb = phba->sli.mbox_active;
11872 if (unlikely(!pmb)) {
11873 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11874 "1832 No pending MBOX command to handle\n");
11875 spin_unlock_irqrestore(&phba->hbalock, iflags);
11876 goto out_no_mqe_complete;
11877 }
11878 spin_unlock_irqrestore(&phba->hbalock, iflags);
11879 mqe = &pmb->u.mqe;
11880 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11881 mbox = phba->mbox;
11882 vport = pmb->vport;
11883
11884 /* Reset heartbeat timer */
11885 phba->last_completion_time = jiffies;
11886 del_timer(&phba->sli.mbox_tmo);
11887
11888 /* Move mbox data to caller's mailbox region, do endian swapping */
11889 if (pmb->mbox_cmpl && mbox)
11890 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
11891
11892 /*
11893 * For mcqe errors, conditionally move a modified error code to
11894 * the mbox so that the error will not be missed.
11895 */
11896 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11897 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11898 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11899 bf_set(lpfc_mqe_status, mqe,
11900 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11901 }
11902 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11903 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11904 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11905 "MBOX dflt rpi: status:x%x rpi:x%x",
11906 mcqe_status,
11907 pmbox->un.varWords[0], 0);
11908 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11909 mp = (struct lpfc_dmabuf *)(pmb->context1);
11910 ndlp = (struct lpfc_nodelist *)pmb->context2;
11911 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11912 * RID of the PPI using the same mbox buffer.
11913 */
11914 lpfc_unreg_login(phba, vport->vpi,
11915 pmbox->un.varWords[0], pmb);
11916 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11917 pmb->context1 = mp;
11918 pmb->context2 = ndlp;
11919 pmb->vport = vport;
11920 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11921 if (rc != MBX_BUSY)
11922 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11923 LOG_SLI, "0385 rc should "
11924 "have been MBX_BUSY\n");
11925 if (rc != MBX_NOT_FINISHED)
11926 goto send_current_mbox;
11927 }
11928 }
11929 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11930 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11931 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11932
11933 /* There is mailbox completion work to do */
11934 spin_lock_irqsave(&phba->hbalock, iflags);
11935 __lpfc_mbox_cmpl_put(phba, pmb);
11936 phba->work_ha |= HA_MBATT;
11937 spin_unlock_irqrestore(&phba->hbalock, iflags);
11938 workposted = true;
11939
11940 send_current_mbox:
11941 spin_lock_irqsave(&phba->hbalock, iflags);
11942 /* Release the mailbox command posting token */
11943 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11944 /* Setting active mailbox pointer need to be in sync to flag clear */
11945 phba->sli.mbox_active = NULL;
11946 spin_unlock_irqrestore(&phba->hbalock, iflags);
11947 /* Wake up worker thread to post the next pending mailbox command */
11948 lpfc_worker_wake_up(phba);
11949 out_no_mqe_complete:
11950 if (bf_get(lpfc_trailer_consumed, mcqe))
11951 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11952 return workposted;
11953 }
11954
11955 /**
11956 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11957 * @phba: Pointer to HBA context object.
11958 * @cqe: Pointer to mailbox completion queue entry.
11959 *
11960 * This routine process a mailbox completion queue entry, it invokes the
11961 * proper mailbox complete handling or asynchrous event handling routine
11962 * according to the MCQE's async bit.
11963 *
11964 * Return: true if work posted to worker thread, otherwise false.
11965 **/
11966 static bool
11967 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11968 {
11969 struct lpfc_mcqe mcqe;
11970 bool workposted;
11971
11972 /* Copy the mailbox MCQE and convert endian order as needed */
11973 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11974
11975 /* Invoke the proper event handling routine */
11976 if (!bf_get(lpfc_trailer_async, &mcqe))
11977 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11978 else
11979 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11980 return workposted;
11981 }
11982
11983 /**
11984 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11985 * @phba: Pointer to HBA context object.
11986 * @cq: Pointer to associated CQ
11987 * @wcqe: Pointer to work-queue completion queue entry.
11988 *
11989 * This routine handles an ELS work-queue completion event.
11990 *
11991 * Return: true if work posted to worker thread, otherwise false.
11992 **/
11993 static bool
11994 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11995 struct lpfc_wcqe_complete *wcqe)
11996 {
11997 struct lpfc_iocbq *irspiocbq;
11998 unsigned long iflags;
11999 struct lpfc_sli_ring *pring = cq->pring;
12000 int txq_cnt = 0;
12001 int txcmplq_cnt = 0;
12002 int fcp_txcmplq_cnt = 0;
12003
12004 /* Get an irspiocbq for later ELS response processing use */
12005 irspiocbq = lpfc_sli_get_iocbq(phba);
12006 if (!irspiocbq) {
12007 if (!list_empty(&pring->txq))
12008 txq_cnt++;
12009 if (!list_empty(&pring->txcmplq))
12010 txcmplq_cnt++;
12011 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
12012 fcp_txcmplq_cnt++;
12013 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12014 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
12015 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
12016 txq_cnt, phba->iocb_cnt,
12017 fcp_txcmplq_cnt,
12018 txcmplq_cnt);
12019 return false;
12020 }
12021
12022 /* Save off the slow-path queue event for work thread to process */
12023 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
12024 spin_lock_irqsave(&phba->hbalock, iflags);
12025 list_add_tail(&irspiocbq->cq_event.list,
12026 &phba->sli4_hba.sp_queue_event);
12027 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12028 spin_unlock_irqrestore(&phba->hbalock, iflags);
12029
12030 return true;
12031 }
12032
12033 /**
12034 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
12035 * @phba: Pointer to HBA context object.
12036 * @wcqe: Pointer to work-queue completion queue entry.
12037 *
12038 * This routine handles slow-path WQ entry comsumed event by invoking the
12039 * proper WQ release routine to the slow-path WQ.
12040 **/
12041 static void
12042 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
12043 struct lpfc_wcqe_release *wcqe)
12044 {
12045 /* sanity check on queue memory */
12046 if (unlikely(!phba->sli4_hba.els_wq))
12047 return;
12048 /* Check for the slow-path ELS work queue */
12049 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
12050 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
12051 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12052 else
12053 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12054 "2579 Slow-path wqe consume event carries "
12055 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
12056 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
12057 phba->sli4_hba.els_wq->queue_id);
12058 }
12059
12060 /**
12061 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
12062 * @phba: Pointer to HBA context object.
12063 * @cq: Pointer to a WQ completion queue.
12064 * @wcqe: Pointer to work-queue completion queue entry.
12065 *
12066 * This routine handles an XRI abort event.
12067 *
12068 * Return: true if work posted to worker thread, otherwise false.
12069 **/
12070 static bool
12071 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
12072 struct lpfc_queue *cq,
12073 struct sli4_wcqe_xri_aborted *wcqe)
12074 {
12075 bool workposted = false;
12076 struct lpfc_cq_event *cq_event;
12077 unsigned long iflags;
12078
12079 /* Allocate a new internal CQ_EVENT entry */
12080 cq_event = lpfc_sli4_cq_event_alloc(phba);
12081 if (!cq_event) {
12082 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12083 "0602 Failed to allocate CQ_EVENT entry\n");
12084 return false;
12085 }
12086
12087 /* Move the CQE into the proper xri abort event list */
12088 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
12089 switch (cq->subtype) {
12090 case LPFC_FCP:
12091 spin_lock_irqsave(&phba->hbalock, iflags);
12092 list_add_tail(&cq_event->list,
12093 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
12094 /* Set the fcp xri abort event flag */
12095 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
12096 spin_unlock_irqrestore(&phba->hbalock, iflags);
12097 workposted = true;
12098 break;
12099 case LPFC_ELS:
12100 spin_lock_irqsave(&phba->hbalock, iflags);
12101 list_add_tail(&cq_event->list,
12102 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
12103 /* Set the els xri abort event flag */
12104 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
12105 spin_unlock_irqrestore(&phba->hbalock, iflags);
12106 workposted = true;
12107 break;
12108 default:
12109 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12110 "0603 Invalid work queue CQE subtype (x%x)\n",
12111 cq->subtype);
12112 workposted = false;
12113 break;
12114 }
12115 return workposted;
12116 }
12117
12118 /**
12119 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
12120 * @phba: Pointer to HBA context object.
12121 * @rcqe: Pointer to receive-queue completion queue entry.
12122 *
12123 * This routine process a receive-queue completion queue entry.
12124 *
12125 * Return: true if work posted to worker thread, otherwise false.
12126 **/
12127 static bool
12128 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
12129 {
12130 bool workposted = false;
12131 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
12132 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
12133 struct hbq_dmabuf *dma_buf;
12134 uint32_t status, rq_id;
12135 unsigned long iflags;
12136
12137 /* sanity check on queue memory */
12138 if (unlikely(!hrq) || unlikely(!drq))
12139 return workposted;
12140
12141 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
12142 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
12143 else
12144 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
12145 if (rq_id != hrq->queue_id)
12146 goto out;
12147
12148 status = bf_get(lpfc_rcqe_status, rcqe);
12149 switch (status) {
12150 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
12151 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12152 "2537 Receive Frame Truncated!!\n");
12153 hrq->RQ_buf_trunc++;
12154 case FC_STATUS_RQ_SUCCESS:
12155 lpfc_sli4_rq_release(hrq, drq);
12156 spin_lock_irqsave(&phba->hbalock, iflags);
12157 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
12158 if (!dma_buf) {
12159 hrq->RQ_no_buf_found++;
12160 spin_unlock_irqrestore(&phba->hbalock, iflags);
12161 goto out;
12162 }
12163 hrq->RQ_rcv_buf++;
12164 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
12165 /* save off the frame for the word thread to process */
12166 list_add_tail(&dma_buf->cq_event.list,
12167 &phba->sli4_hba.sp_queue_event);
12168 /* Frame received */
12169 phba->hba_flag |= HBA_SP_QUEUE_EVT;
12170 spin_unlock_irqrestore(&phba->hbalock, iflags);
12171 workposted = true;
12172 break;
12173 case FC_STATUS_INSUFF_BUF_NEED_BUF:
12174 case FC_STATUS_INSUFF_BUF_FRM_DISC:
12175 hrq->RQ_no_posted_buf++;
12176 /* Post more buffers if possible */
12177 spin_lock_irqsave(&phba->hbalock, iflags);
12178 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
12179 spin_unlock_irqrestore(&phba->hbalock, iflags);
12180 workposted = true;
12181 break;
12182 }
12183 out:
12184 return workposted;
12185 }
12186
12187 /**
12188 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
12189 * @phba: Pointer to HBA context object.
12190 * @cq: Pointer to the completion queue.
12191 * @wcqe: Pointer to a completion queue entry.
12192 *
12193 * This routine process a slow-path work-queue or receive queue completion queue
12194 * entry.
12195 *
12196 * Return: true if work posted to worker thread, otherwise false.
12197 **/
12198 static bool
12199 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12200 struct lpfc_cqe *cqe)
12201 {
12202 struct lpfc_cqe cqevt;
12203 bool workposted = false;
12204
12205 /* Copy the work queue CQE and convert endian order if needed */
12206 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
12207
12208 /* Check and process for different type of WCQE and dispatch */
12209 switch (bf_get(lpfc_cqe_code, &cqevt)) {
12210 case CQE_CODE_COMPL_WQE:
12211 /* Process the WQ/RQ complete event */
12212 phba->last_completion_time = jiffies;
12213 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
12214 (struct lpfc_wcqe_complete *)&cqevt);
12215 break;
12216 case CQE_CODE_RELEASE_WQE:
12217 /* Process the WQ release event */
12218 lpfc_sli4_sp_handle_rel_wcqe(phba,
12219 (struct lpfc_wcqe_release *)&cqevt);
12220 break;
12221 case CQE_CODE_XRI_ABORTED:
12222 /* Process the WQ XRI abort event */
12223 phba->last_completion_time = jiffies;
12224 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12225 (struct sli4_wcqe_xri_aborted *)&cqevt);
12226 break;
12227 case CQE_CODE_RECEIVE:
12228 case CQE_CODE_RECEIVE_V1:
12229 /* Process the RQ event */
12230 phba->last_completion_time = jiffies;
12231 workposted = lpfc_sli4_sp_handle_rcqe(phba,
12232 (struct lpfc_rcqe *)&cqevt);
12233 break;
12234 default:
12235 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12236 "0388 Not a valid WCQE code: x%x\n",
12237 bf_get(lpfc_cqe_code, &cqevt));
12238 break;
12239 }
12240 return workposted;
12241 }
12242
12243 /**
12244 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
12245 * @phba: Pointer to HBA context object.
12246 * @eqe: Pointer to fast-path event queue entry.
12247 *
12248 * This routine process a event queue entry from the slow-path event queue.
12249 * It will check the MajorCode and MinorCode to determine this is for a
12250 * completion event on a completion queue, if not, an error shall be logged
12251 * and just return. Otherwise, it will get to the corresponding completion
12252 * queue and process all the entries on that completion queue, rearm the
12253 * completion queue, and then return.
12254 *
12255 **/
12256 static void
12257 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12258 struct lpfc_queue *speq)
12259 {
12260 struct lpfc_queue *cq = NULL, *childq;
12261 struct lpfc_cqe *cqe;
12262 bool workposted = false;
12263 int ecount = 0;
12264 uint16_t cqid;
12265
12266 /* Get the reference to the corresponding CQ */
12267 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12268
12269 list_for_each_entry(childq, &speq->child_list, list) {
12270 if (childq->queue_id == cqid) {
12271 cq = childq;
12272 break;
12273 }
12274 }
12275 if (unlikely(!cq)) {
12276 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12277 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12278 "0365 Slow-path CQ identifier "
12279 "(%d) does not exist\n", cqid);
12280 return;
12281 }
12282
12283 /* Process all the entries to the CQ */
12284 switch (cq->type) {
12285 case LPFC_MCQ:
12286 while ((cqe = lpfc_sli4_cq_get(cq))) {
12287 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
12288 if (!(++ecount % cq->entry_repost))
12289 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12290 cq->CQ_mbox++;
12291 }
12292 break;
12293 case LPFC_WCQ:
12294 while ((cqe = lpfc_sli4_cq_get(cq))) {
12295 if (cq->subtype == LPFC_FCP)
12296 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
12297 cqe);
12298 else
12299 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
12300 cqe);
12301 if (!(++ecount % cq->entry_repost))
12302 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12303 }
12304
12305 /* Track the max number of CQEs processed in 1 EQ */
12306 if (ecount > cq->CQ_max_cqe)
12307 cq->CQ_max_cqe = ecount;
12308 break;
12309 default:
12310 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12311 "0370 Invalid completion queue type (%d)\n",
12312 cq->type);
12313 return;
12314 }
12315
12316 /* Catch the no cq entry condition, log an error */
12317 if (unlikely(ecount == 0))
12318 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12319 "0371 No entry from the CQ: identifier "
12320 "(x%x), type (%d)\n", cq->queue_id, cq->type);
12321
12322 /* In any case, flash and re-arm the RCQ */
12323 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12324
12325 /* wake up worker thread if there are works to be done */
12326 if (workposted)
12327 lpfc_worker_wake_up(phba);
12328 }
12329
12330 /**
12331 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
12332 * @phba: Pointer to HBA context object.
12333 * @cq: Pointer to associated CQ
12334 * @wcqe: Pointer to work-queue completion queue entry.
12335 *
12336 * This routine process a fast-path work queue completion entry from fast-path
12337 * event queue for FCP command response completion.
12338 **/
12339 static void
12340 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12341 struct lpfc_wcqe_complete *wcqe)
12342 {
12343 struct lpfc_sli_ring *pring = cq->pring;
12344 struct lpfc_iocbq *cmdiocbq;
12345 struct lpfc_iocbq irspiocbq;
12346 unsigned long iflags;
12347
12348 /* Check for response status */
12349 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
12350 /* If resource errors reported from HBA, reduce queue
12351 * depth of the SCSI device.
12352 */
12353 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
12354 IOSTAT_LOCAL_REJECT)) &&
12355 ((wcqe->parameter & IOERR_PARAM_MASK) ==
12356 IOERR_NO_RESOURCES))
12357 phba->lpfc_rampdown_queue_depth(phba);
12358
12359 /* Log the error status */
12360 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12361 "0373 FCP complete error: status=x%x, "
12362 "hw_status=x%x, total_data_specified=%d, "
12363 "parameter=x%x, word3=x%x\n",
12364 bf_get(lpfc_wcqe_c_status, wcqe),
12365 bf_get(lpfc_wcqe_c_hw_status, wcqe),
12366 wcqe->total_data_placed, wcqe->parameter,
12367 wcqe->word3);
12368 }
12369
12370 /* Look up the FCP command IOCB and create pseudo response IOCB */
12371 spin_lock_irqsave(&pring->ring_lock, iflags);
12372 pring->stats.iocb_event++;
12373 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
12374 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12375 spin_unlock_irqrestore(&pring->ring_lock, iflags);
12376 if (unlikely(!cmdiocbq)) {
12377 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12378 "0374 FCP complete with no corresponding "
12379 "cmdiocb: iotag (%d)\n",
12380 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12381 return;
12382 }
12383 if (unlikely(!cmdiocbq->iocb_cmpl)) {
12384 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12385 "0375 FCP cmdiocb not callback function "
12386 "iotag: (%d)\n",
12387 bf_get(lpfc_wcqe_c_request_tag, wcqe));
12388 return;
12389 }
12390
12391 /* Fake the irspiocb and copy necessary response information */
12392 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
12393
12394 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
12395 spin_lock_irqsave(&phba->hbalock, iflags);
12396 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
12397 spin_unlock_irqrestore(&phba->hbalock, iflags);
12398 }
12399
12400 /* Pass the cmd_iocb and the rsp state to the upper layer */
12401 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
12402 }
12403
12404 /**
12405 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
12406 * @phba: Pointer to HBA context object.
12407 * @cq: Pointer to completion queue.
12408 * @wcqe: Pointer to work-queue completion queue entry.
12409 *
12410 * This routine handles an fast-path WQ entry comsumed event by invoking the
12411 * proper WQ release routine to the slow-path WQ.
12412 **/
12413 static void
12414 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12415 struct lpfc_wcqe_release *wcqe)
12416 {
12417 struct lpfc_queue *childwq;
12418 bool wqid_matched = false;
12419 uint16_t fcp_wqid;
12420
12421 /* Check for fast-path FCP work queue release */
12422 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
12423 list_for_each_entry(childwq, &cq->child_list, list) {
12424 if (childwq->queue_id == fcp_wqid) {
12425 lpfc_sli4_wq_release(childwq,
12426 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
12427 wqid_matched = true;
12428 break;
12429 }
12430 }
12431 /* Report warning log message if no match found */
12432 if (wqid_matched != true)
12433 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12434 "2580 Fast-path wqe consume event carries "
12435 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
12436 }
12437
12438 /**
12439 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
12440 * @cq: Pointer to the completion queue.
12441 * @eqe: Pointer to fast-path completion queue entry.
12442 *
12443 * This routine process a fast-path work queue completion entry from fast-path
12444 * event queue for FCP command response completion.
12445 **/
12446 static int
12447 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
12448 struct lpfc_cqe *cqe)
12449 {
12450 struct lpfc_wcqe_release wcqe;
12451 bool workposted = false;
12452
12453 /* Copy the work queue CQE and convert endian order if needed */
12454 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
12455
12456 /* Check and process for different type of WCQE and dispatch */
12457 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
12458 case CQE_CODE_COMPL_WQE:
12459 cq->CQ_wq++;
12460 /* Process the WQ complete event */
12461 phba->last_completion_time = jiffies;
12462 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
12463 (struct lpfc_wcqe_complete *)&wcqe);
12464 break;
12465 case CQE_CODE_RELEASE_WQE:
12466 cq->CQ_release_wqe++;
12467 /* Process the WQ release event */
12468 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
12469 (struct lpfc_wcqe_release *)&wcqe);
12470 break;
12471 case CQE_CODE_XRI_ABORTED:
12472 cq->CQ_xri_aborted++;
12473 /* Process the WQ XRI abort event */
12474 phba->last_completion_time = jiffies;
12475 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
12476 (struct sli4_wcqe_xri_aborted *)&wcqe);
12477 break;
12478 default:
12479 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12480 "0144 Not a valid WCQE code: x%x\n",
12481 bf_get(lpfc_wcqe_c_code, &wcqe));
12482 break;
12483 }
12484 return workposted;
12485 }
12486
12487 /**
12488 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
12489 * @phba: Pointer to HBA context object.
12490 * @eqe: Pointer to fast-path event queue entry.
12491 *
12492 * This routine process a event queue entry from the fast-path event queue.
12493 * It will check the MajorCode and MinorCode to determine this is for a
12494 * completion event on a completion queue, if not, an error shall be logged
12495 * and just return. Otherwise, it will get to the corresponding completion
12496 * queue and process all the entries on the completion queue, rearm the
12497 * completion queue, and then return.
12498 **/
12499 static void
12500 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
12501 uint32_t qidx)
12502 {
12503 struct lpfc_queue *cq;
12504 struct lpfc_cqe *cqe;
12505 bool workposted = false;
12506 uint16_t cqid;
12507 int ecount = 0;
12508
12509 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12510 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12511 "0366 Not a valid completion "
12512 "event: majorcode=x%x, minorcode=x%x\n",
12513 bf_get_le32(lpfc_eqe_major_code, eqe),
12514 bf_get_le32(lpfc_eqe_minor_code, eqe));
12515 return;
12516 }
12517
12518 /* Get the reference to the corresponding CQ */
12519 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12520
12521 /* Check if this is a Slow path event */
12522 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
12523 lpfc_sli4_sp_handle_eqe(phba, eqe,
12524 phba->sli4_hba.hba_eq[qidx]);
12525 return;
12526 }
12527
12528 if (unlikely(!phba->sli4_hba.fcp_cq)) {
12529 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12530 "3146 Fast-path completion queues "
12531 "does not exist\n");
12532 return;
12533 }
12534 cq = phba->sli4_hba.fcp_cq[qidx];
12535 if (unlikely(!cq)) {
12536 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12537 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12538 "0367 Fast-path completion queue "
12539 "(%d) does not exist\n", qidx);
12540 return;
12541 }
12542
12543 if (unlikely(cqid != cq->queue_id)) {
12544 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12545 "0368 Miss-matched fast-path completion "
12546 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
12547 cqid, cq->queue_id);
12548 return;
12549 }
12550
12551 /* Process all the entries to the CQ */
12552 while ((cqe = lpfc_sli4_cq_get(cq))) {
12553 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12554 if (!(++ecount % cq->entry_repost))
12555 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12556 }
12557
12558 /* Track the max number of CQEs processed in 1 EQ */
12559 if (ecount > cq->CQ_max_cqe)
12560 cq->CQ_max_cqe = ecount;
12561
12562 /* Catch the no cq entry condition */
12563 if (unlikely(ecount == 0))
12564 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12565 "0369 No entry from fast-path completion "
12566 "queue fcpcqid=%d\n", cq->queue_id);
12567
12568 /* In any case, flash and re-arm the CQ */
12569 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12570
12571 /* wake up worker thread if there are works to be done */
12572 if (workposted)
12573 lpfc_worker_wake_up(phba);
12574 }
12575
12576 static void
12577 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
12578 {
12579 struct lpfc_eqe *eqe;
12580
12581 /* walk all the EQ entries and drop on the floor */
12582 while ((eqe = lpfc_sli4_eq_get(eq)))
12583 ;
12584
12585 /* Clear and re-arm the EQ */
12586 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12587 }
12588
12589
12590 /**
12591 * lpfc_sli4_fof_handle_eqe - Process a Flash Optimized Fabric event queue
12592 * entry
12593 * @phba: Pointer to HBA context object.
12594 * @eqe: Pointer to fast-path event queue entry.
12595 *
12596 * This routine process a event queue entry from the Flash Optimized Fabric
12597 * event queue. It will check the MajorCode and MinorCode to determine this
12598 * is for a completion event on a completion queue, if not, an error shall be
12599 * logged and just return. Otherwise, it will get to the corresponding
12600 * completion queue and process all the entries on the completion queue, rearm
12601 * the completion queue, and then return.
12602 **/
12603 static void
12604 lpfc_sli4_fof_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe)
12605 {
12606 struct lpfc_queue *cq;
12607 struct lpfc_cqe *cqe;
12608 bool workposted = false;
12609 uint16_t cqid;
12610 int ecount = 0;
12611
12612 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
12613 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12614 "9147 Not a valid completion "
12615 "event: majorcode=x%x, minorcode=x%x\n",
12616 bf_get_le32(lpfc_eqe_major_code, eqe),
12617 bf_get_le32(lpfc_eqe_minor_code, eqe));
12618 return;
12619 }
12620
12621 /* Get the reference to the corresponding CQ */
12622 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
12623
12624 /* Next check for OAS */
12625 cq = phba->sli4_hba.oas_cq;
12626 if (unlikely(!cq)) {
12627 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
12628 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12629 "9148 OAS completion queue "
12630 "does not exist\n");
12631 return;
12632 }
12633
12634 if (unlikely(cqid != cq->queue_id)) {
12635 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12636 "9149 Miss-matched fast-path compl "
12637 "queue id: eqcqid=%d, fcpcqid=%d\n",
12638 cqid, cq->queue_id);
12639 return;
12640 }
12641
12642 /* Process all the entries to the OAS CQ */
12643 while ((cqe = lpfc_sli4_cq_get(cq))) {
12644 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
12645 if (!(++ecount % cq->entry_repost))
12646 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
12647 }
12648
12649 /* Track the max number of CQEs processed in 1 EQ */
12650 if (ecount > cq->CQ_max_cqe)
12651 cq->CQ_max_cqe = ecount;
12652
12653 /* Catch the no cq entry condition */
12654 if (unlikely(ecount == 0))
12655 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12656 "9153 No entry from fast-path completion "
12657 "queue fcpcqid=%d\n", cq->queue_id);
12658
12659 /* In any case, flash and re-arm the CQ */
12660 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
12661
12662 /* wake up worker thread if there are works to be done */
12663 if (workposted)
12664 lpfc_worker_wake_up(phba);
12665 }
12666
12667 /**
12668 * lpfc_sli4_fof_intr_handler - HBA interrupt handler to SLI-4 device
12669 * @irq: Interrupt number.
12670 * @dev_id: The device context pointer.
12671 *
12672 * This function is directly called from the PCI layer as an interrupt
12673 * service routine when device with SLI-4 interface spec is enabled with
12674 * MSI-X multi-message interrupt mode and there is a Flash Optimized Fabric
12675 * IOCB ring event in the HBA. However, when the device is enabled with either
12676 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12677 * device-level interrupt handler. When the PCI slot is in error recovery
12678 * or the HBA is undergoing initialization, the interrupt handler will not
12679 * process the interrupt. The Flash Optimized Fabric ring event are handled in
12680 * the intrrupt context. This function is called without any lock held.
12681 * It gets the hbalock to access and update SLI data structures. Note that,
12682 * the EQ to CQ are one-to-one map such that the EQ index is
12683 * equal to that of CQ index.
12684 *
12685 * This function returns IRQ_HANDLED when interrupt is handled else it
12686 * returns IRQ_NONE.
12687 **/
12688 irqreturn_t
12689 lpfc_sli4_fof_intr_handler(int irq, void *dev_id)
12690 {
12691 struct lpfc_hba *phba;
12692 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12693 struct lpfc_queue *eq;
12694 struct lpfc_eqe *eqe;
12695 unsigned long iflag;
12696 int ecount = 0;
12697
12698 /* Get the driver's phba structure from the dev_id */
12699 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12700 phba = fcp_eq_hdl->phba;
12701
12702 if (unlikely(!phba))
12703 return IRQ_NONE;
12704
12705 /* Get to the EQ struct associated with this vector */
12706 eq = phba->sli4_hba.fof_eq;
12707 if (unlikely(!eq))
12708 return IRQ_NONE;
12709
12710 /* Check device state for handling interrupt */
12711 if (unlikely(lpfc_intr_state_check(phba))) {
12712 eq->EQ_badstate++;
12713 /* Check again for link_state with lock held */
12714 spin_lock_irqsave(&phba->hbalock, iflag);
12715 if (phba->link_state < LPFC_LINK_DOWN)
12716 /* Flush, clear interrupt, and rearm the EQ */
12717 lpfc_sli4_eq_flush(phba, eq);
12718 spin_unlock_irqrestore(&phba->hbalock, iflag);
12719 return IRQ_NONE;
12720 }
12721
12722 /*
12723 * Process all the event on FCP fast-path EQ
12724 */
12725 while ((eqe = lpfc_sli4_eq_get(eq))) {
12726 lpfc_sli4_fof_handle_eqe(phba, eqe);
12727 if (!(++ecount % eq->entry_repost))
12728 lpfc_sli4_eq_release(eq, LPFC_QUEUE_NOARM);
12729 eq->EQ_processed++;
12730 }
12731
12732 /* Track the max number of EQEs processed in 1 intr */
12733 if (ecount > eq->EQ_max_eqe)
12734 eq->EQ_max_eqe = ecount;
12735
12736
12737 if (unlikely(ecount == 0)) {
12738 eq->EQ_no_entry++;
12739
12740 if (phba->intr_type == MSIX)
12741 /* MSI-X treated interrupt served as no EQ share INT */
12742 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12743 "9145 MSI-X interrupt with no EQE\n");
12744 else {
12745 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12746 "9146 ISR interrupt with no EQE\n");
12747 /* Non MSI-X treated on interrupt as EQ share INT */
12748 return IRQ_NONE;
12749 }
12750 }
12751 /* Always clear and re-arm the fast-path EQ */
12752 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
12753 return IRQ_HANDLED;
12754 }
12755
12756 /**
12757 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
12758 * @irq: Interrupt number.
12759 * @dev_id: The device context pointer.
12760 *
12761 * This function is directly called from the PCI layer as an interrupt
12762 * service routine when device with SLI-4 interface spec is enabled with
12763 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
12764 * ring event in the HBA. However, when the device is enabled with either
12765 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
12766 * device-level interrupt handler. When the PCI slot is in error recovery
12767 * or the HBA is undergoing initialization, the interrupt handler will not
12768 * process the interrupt. The SCSI FCP fast-path ring event are handled in
12769 * the intrrupt context. This function is called without any lock held.
12770 * It gets the hbalock to access and update SLI data structures. Note that,
12771 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
12772 * equal to that of FCP CQ index.
12773 *
12774 * The link attention and ELS ring attention events are handled
12775 * by the worker thread. The interrupt handler signals the worker thread
12776 * and returns for these events. This function is called without any lock
12777 * held. It gets the hbalock to access and update SLI data structures.
12778 *
12779 * This function returns IRQ_HANDLED when interrupt is handled else it
12780 * returns IRQ_NONE.
12781 **/
12782 irqreturn_t
12783 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
12784 {
12785 struct lpfc_hba *phba;
12786 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
12787 struct lpfc_queue *fpeq;
12788 struct lpfc_eqe *eqe;
12789 unsigned long iflag;
12790 int ecount = 0;
12791 int fcp_eqidx;
12792
12793 /* Get the driver's phba structure from the dev_id */
12794 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
12795 phba = fcp_eq_hdl->phba;
12796 fcp_eqidx = fcp_eq_hdl->idx;
12797
12798 if (unlikely(!phba))
12799 return IRQ_NONE;
12800 if (unlikely(!phba->sli4_hba.hba_eq))
12801 return IRQ_NONE;
12802
12803 /* Get to the EQ struct associated with this vector */
12804 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
12805 if (unlikely(!fpeq))
12806 return IRQ_NONE;
12807
12808 if (lpfc_fcp_look_ahead) {
12809 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
12810 lpfc_sli4_eq_clr_intr(fpeq);
12811 else {
12812 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12813 return IRQ_NONE;
12814 }
12815 }
12816
12817 /* Check device state for handling interrupt */
12818 if (unlikely(lpfc_intr_state_check(phba))) {
12819 fpeq->EQ_badstate++;
12820 /* Check again for link_state with lock held */
12821 spin_lock_irqsave(&phba->hbalock, iflag);
12822 if (phba->link_state < LPFC_LINK_DOWN)
12823 /* Flush, clear interrupt, and rearm the EQ */
12824 lpfc_sli4_eq_flush(phba, fpeq);
12825 spin_unlock_irqrestore(&phba->hbalock, iflag);
12826 if (lpfc_fcp_look_ahead)
12827 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12828 return IRQ_NONE;
12829 }
12830
12831 /*
12832 * Process all the event on FCP fast-path EQ
12833 */
12834 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
12835 if (eqe == NULL)
12836 break;
12837
12838 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
12839 if (!(++ecount % fpeq->entry_repost))
12840 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
12841 fpeq->EQ_processed++;
12842 }
12843
12844 /* Track the max number of EQEs processed in 1 intr */
12845 if (ecount > fpeq->EQ_max_eqe)
12846 fpeq->EQ_max_eqe = ecount;
12847
12848 /* Always clear and re-arm the fast-path EQ */
12849 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12850
12851 if (unlikely(ecount == 0)) {
12852 fpeq->EQ_no_entry++;
12853
12854 if (lpfc_fcp_look_ahead) {
12855 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12856 return IRQ_NONE;
12857 }
12858
12859 if (phba->intr_type == MSIX)
12860 /* MSI-X treated interrupt served as no EQ share INT */
12861 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12862 "0358 MSI-X interrupt with no EQE\n");
12863 else
12864 /* Non MSI-X treated on interrupt as EQ share INT */
12865 return IRQ_NONE;
12866 }
12867
12868 if (lpfc_fcp_look_ahead)
12869 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12870 return IRQ_HANDLED;
12871 } /* lpfc_sli4_fp_intr_handler */
12872
12873 /**
12874 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12875 * @irq: Interrupt number.
12876 * @dev_id: The device context pointer.
12877 *
12878 * This function is the device-level interrupt handler to device with SLI-4
12879 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12880 * interrupt mode is enabled and there is an event in the HBA which requires
12881 * driver attention. This function invokes the slow-path interrupt attention
12882 * handling function and fast-path interrupt attention handling function in
12883 * turn to process the relevant HBA attention events. This function is called
12884 * without any lock held. It gets the hbalock to access and update SLI data
12885 * structures.
12886 *
12887 * This function returns IRQ_HANDLED when interrupt is handled, else it
12888 * returns IRQ_NONE.
12889 **/
12890 irqreturn_t
12891 lpfc_sli4_intr_handler(int irq, void *dev_id)
12892 {
12893 struct lpfc_hba *phba;
12894 irqreturn_t hba_irq_rc;
12895 bool hba_handled = false;
12896 int fcp_eqidx;
12897
12898 /* Get the driver's phba structure from the dev_id */
12899 phba = (struct lpfc_hba *)dev_id;
12900
12901 if (unlikely(!phba))
12902 return IRQ_NONE;
12903
12904 /*
12905 * Invoke fast-path host attention interrupt handling as appropriate.
12906 */
12907 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12908 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
12909 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12910 if (hba_irq_rc == IRQ_HANDLED)
12911 hba_handled |= true;
12912 }
12913
12914 if (phba->cfg_fof) {
12915 hba_irq_rc = lpfc_sli4_fof_intr_handler(irq,
12916 &phba->sli4_hba.fcp_eq_hdl[0]);
12917 if (hba_irq_rc == IRQ_HANDLED)
12918 hba_handled |= true;
12919 }
12920
12921 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
12922 } /* lpfc_sli4_intr_handler */
12923
12924 /**
12925 * lpfc_sli4_queue_free - free a queue structure and associated memory
12926 * @queue: The queue structure to free.
12927 *
12928 * This function frees a queue structure and the DMAable memory used for
12929 * the host resident queue. This function must be called after destroying the
12930 * queue on the HBA.
12931 **/
12932 void
12933 lpfc_sli4_queue_free(struct lpfc_queue *queue)
12934 {
12935 struct lpfc_dmabuf *dmabuf;
12936
12937 if (!queue)
12938 return;
12939
12940 while (!list_empty(&queue->page_list)) {
12941 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12942 list);
12943 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
12944 dmabuf->virt, dmabuf->phys);
12945 kfree(dmabuf);
12946 }
12947 kfree(queue);
12948 return;
12949 }
12950
12951 /**
12952 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12953 * @phba: The HBA that this queue is being created on.
12954 * @entry_size: The size of each queue entry for this queue.
12955 * @entry count: The number of entries that this queue will handle.
12956 *
12957 * This function allocates a queue structure and the DMAable memory used for
12958 * the host resident queue. This function must be called before creating the
12959 * queue on the HBA.
12960 **/
12961 struct lpfc_queue *
12962 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12963 uint32_t entry_count)
12964 {
12965 struct lpfc_queue *queue;
12966 struct lpfc_dmabuf *dmabuf;
12967 int x, total_qe_count;
12968 void *dma_pointer;
12969 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12970
12971 if (!phba->sli4_hba.pc_sli4_params.supported)
12972 hw_page_size = SLI4_PAGE_SIZE;
12973
12974 queue = kzalloc(sizeof(struct lpfc_queue) +
12975 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12976 if (!queue)
12977 return NULL;
12978 queue->page_count = (ALIGN(entry_size * entry_count,
12979 hw_page_size))/hw_page_size;
12980 INIT_LIST_HEAD(&queue->list);
12981 INIT_LIST_HEAD(&queue->page_list);
12982 INIT_LIST_HEAD(&queue->child_list);
12983 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12984 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12985 if (!dmabuf)
12986 goto out_fail;
12987 dmabuf->virt = dma_zalloc_coherent(&phba->pcidev->dev,
12988 hw_page_size, &dmabuf->phys,
12989 GFP_KERNEL);
12990 if (!dmabuf->virt) {
12991 kfree(dmabuf);
12992 goto out_fail;
12993 }
12994 dmabuf->buffer_tag = x;
12995 list_add_tail(&dmabuf->list, &queue->page_list);
12996 /* initialize queue's entry array */
12997 dma_pointer = dmabuf->virt;
12998 for (; total_qe_count < entry_count &&
12999 dma_pointer < (hw_page_size + dmabuf->virt);
13000 total_qe_count++, dma_pointer += entry_size) {
13001 queue->qe[total_qe_count].address = dma_pointer;
13002 }
13003 }
13004 queue->entry_size = entry_size;
13005 queue->entry_count = entry_count;
13006
13007 /*
13008 * entry_repost is calculated based on the number of entries in the
13009 * queue. This works out except for RQs. If buffers are NOT initially
13010 * posted for every RQE, entry_repost should be adjusted accordingly.
13011 */
13012 queue->entry_repost = (entry_count >> 3);
13013 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
13014 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
13015 queue->phba = phba;
13016
13017 return queue;
13018 out_fail:
13019 lpfc_sli4_queue_free(queue);
13020 return NULL;
13021 }
13022
13023 /**
13024 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
13025 * @phba: HBA structure that indicates port to create a queue on.
13026 * @pci_barset: PCI BAR set flag.
13027 *
13028 * This function shall perform iomap of the specified PCI BAR address to host
13029 * memory address if not already done so and return it. The returned host
13030 * memory address can be NULL.
13031 */
13032 static void __iomem *
13033 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
13034 {
13035 if (!phba->pcidev)
13036 return NULL;
13037
13038 switch (pci_barset) {
13039 case WQ_PCI_BAR_0_AND_1:
13040 return phba->pci_bar0_memmap_p;
13041 case WQ_PCI_BAR_2_AND_3:
13042 return phba->pci_bar2_memmap_p;
13043 case WQ_PCI_BAR_4_AND_5:
13044 return phba->pci_bar4_memmap_p;
13045 default:
13046 break;
13047 }
13048 return NULL;
13049 }
13050
13051 /**
13052 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
13053 * @phba: HBA structure that indicates port to create a queue on.
13054 * @startq: The starting FCP EQ to modify
13055 *
13056 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
13057 *
13058 * The @phba struct is used to send mailbox command to HBA. The @startq
13059 * is used to get the starting FCP EQ to change.
13060 * This function is asynchronous and will wait for the mailbox
13061 * command to finish before continuing.
13062 *
13063 * On success this function will return a zero. If unable to allocate enough
13064 * memory this function will return -ENOMEM. If the queue create mailbox command
13065 * fails this function will return -ENXIO.
13066 **/
13067 int
13068 lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint32_t startq)
13069 {
13070 struct lpfc_mbx_modify_eq_delay *eq_delay;
13071 LPFC_MBOXQ_t *mbox;
13072 struct lpfc_queue *eq;
13073 int cnt, rc, length, status = 0;
13074 uint32_t shdr_status, shdr_add_status;
13075 uint32_t result;
13076 int fcp_eqidx;
13077 union lpfc_sli4_cfg_shdr *shdr;
13078 uint16_t dmult;
13079
13080 if (startq >= phba->cfg_fcp_io_channel)
13081 return 0;
13082
13083 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13084 if (!mbox)
13085 return -ENOMEM;
13086 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
13087 sizeof(struct lpfc_sli4_cfg_mhdr));
13088 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13089 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
13090 length, LPFC_SLI4_MBX_EMBED);
13091 eq_delay = &mbox->u.mqe.un.eq_delay;
13092
13093 /* Calculate delay multiper from maximum interrupt per second */
13094 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
13095 if (result > LPFC_DMULT_CONST)
13096 dmult = 0;
13097 else
13098 dmult = LPFC_DMULT_CONST/result - 1;
13099
13100 cnt = 0;
13101 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
13102 fcp_eqidx++) {
13103 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
13104 if (!eq)
13105 continue;
13106 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
13107 eq_delay->u.request.eq[cnt].phase = 0;
13108 eq_delay->u.request.eq[cnt].delay_multi = dmult;
13109 cnt++;
13110 if (cnt >= LPFC_MAX_EQ_DELAY)
13111 break;
13112 }
13113 eq_delay->u.request.num_eq = cnt;
13114
13115 mbox->vport = phba->pport;
13116 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13117 mbox->context1 = NULL;
13118 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13119 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
13120 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13121 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13122 if (shdr_status || shdr_add_status || rc) {
13123 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13124 "2512 MODIFY_EQ_DELAY mailbox failed with "
13125 "status x%x add_status x%x, mbx status x%x\n",
13126 shdr_status, shdr_add_status, rc);
13127 status = -ENXIO;
13128 }
13129 mempool_free(mbox, phba->mbox_mem_pool);
13130 return status;
13131 }
13132
13133 /**
13134 * lpfc_eq_create - Create an Event Queue on the HBA
13135 * @phba: HBA structure that indicates port to create a queue on.
13136 * @eq: The queue structure to use to create the event queue.
13137 * @imax: The maximum interrupt per second limit.
13138 *
13139 * This function creates an event queue, as detailed in @eq, on a port,
13140 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
13141 *
13142 * The @phba struct is used to send mailbox command to HBA. The @eq struct
13143 * is used to get the entry count and entry size that are necessary to
13144 * determine the number of pages to allocate and use for this queue. This
13145 * function will send the EQ_CREATE mailbox command to the HBA to setup the
13146 * event queue. This function is asynchronous and will wait for the mailbox
13147 * command to finish before continuing.
13148 *
13149 * On success this function will return a zero. If unable to allocate enough
13150 * memory this function will return -ENOMEM. If the queue create mailbox command
13151 * fails this function will return -ENXIO.
13152 **/
13153 int
13154 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
13155 {
13156 struct lpfc_mbx_eq_create *eq_create;
13157 LPFC_MBOXQ_t *mbox;
13158 int rc, length, status = 0;
13159 struct lpfc_dmabuf *dmabuf;
13160 uint32_t shdr_status, shdr_add_status;
13161 union lpfc_sli4_cfg_shdr *shdr;
13162 uint16_t dmult;
13163 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13164
13165 /* sanity check on queue memory */
13166 if (!eq)
13167 return -ENODEV;
13168 if (!phba->sli4_hba.pc_sli4_params.supported)
13169 hw_page_size = SLI4_PAGE_SIZE;
13170
13171 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13172 if (!mbox)
13173 return -ENOMEM;
13174 length = (sizeof(struct lpfc_mbx_eq_create) -
13175 sizeof(struct lpfc_sli4_cfg_mhdr));
13176 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13177 LPFC_MBOX_OPCODE_EQ_CREATE,
13178 length, LPFC_SLI4_MBX_EMBED);
13179 eq_create = &mbox->u.mqe.un.eq_create;
13180 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
13181 eq->page_count);
13182 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
13183 LPFC_EQE_SIZE);
13184 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
13185 /* don't setup delay multiplier using EQ_CREATE */
13186 dmult = 0;
13187 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
13188 dmult);
13189 switch (eq->entry_count) {
13190 default:
13191 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13192 "0360 Unsupported EQ count. (%d)\n",
13193 eq->entry_count);
13194 if (eq->entry_count < 256)
13195 return -EINVAL;
13196 /* otherwise default to smallest count (drop through) */
13197 case 256:
13198 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13199 LPFC_EQ_CNT_256);
13200 break;
13201 case 512:
13202 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13203 LPFC_EQ_CNT_512);
13204 break;
13205 case 1024:
13206 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13207 LPFC_EQ_CNT_1024);
13208 break;
13209 case 2048:
13210 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13211 LPFC_EQ_CNT_2048);
13212 break;
13213 case 4096:
13214 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
13215 LPFC_EQ_CNT_4096);
13216 break;
13217 }
13218 list_for_each_entry(dmabuf, &eq->page_list, list) {
13219 memset(dmabuf->virt, 0, hw_page_size);
13220 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13221 putPaddrLow(dmabuf->phys);
13222 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13223 putPaddrHigh(dmabuf->phys);
13224 }
13225 mbox->vport = phba->pport;
13226 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13227 mbox->context1 = NULL;
13228 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13229 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
13230 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13231 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13232 if (shdr_status || shdr_add_status || rc) {
13233 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13234 "2500 EQ_CREATE mailbox failed with "
13235 "status x%x add_status x%x, mbx status x%x\n",
13236 shdr_status, shdr_add_status, rc);
13237 status = -ENXIO;
13238 }
13239 eq->type = LPFC_EQ;
13240 eq->subtype = LPFC_NONE;
13241 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
13242 if (eq->queue_id == 0xFFFF)
13243 status = -ENXIO;
13244 eq->host_index = 0;
13245 eq->hba_index = 0;
13246
13247 mempool_free(mbox, phba->mbox_mem_pool);
13248 return status;
13249 }
13250
13251 /**
13252 * lpfc_cq_create - Create a Completion Queue on the HBA
13253 * @phba: HBA structure that indicates port to create a queue on.
13254 * @cq: The queue structure to use to create the completion queue.
13255 * @eq: The event queue to bind this completion queue to.
13256 *
13257 * This function creates a completion queue, as detailed in @wq, on a port,
13258 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
13259 *
13260 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13261 * is used to get the entry count and entry size that are necessary to
13262 * determine the number of pages to allocate and use for this queue. The @eq
13263 * is used to indicate which event queue to bind this completion queue to. This
13264 * function will send the CQ_CREATE mailbox command to the HBA to setup the
13265 * completion queue. This function is asynchronous and will wait for the mailbox
13266 * command to finish before continuing.
13267 *
13268 * On success this function will return a zero. If unable to allocate enough
13269 * memory this function will return -ENOMEM. If the queue create mailbox command
13270 * fails this function will return -ENXIO.
13271 **/
13272 int
13273 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
13274 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
13275 {
13276 struct lpfc_mbx_cq_create *cq_create;
13277 struct lpfc_dmabuf *dmabuf;
13278 LPFC_MBOXQ_t *mbox;
13279 int rc, length, status = 0;
13280 uint32_t shdr_status, shdr_add_status;
13281 union lpfc_sli4_cfg_shdr *shdr;
13282 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13283
13284 /* sanity check on queue memory */
13285 if (!cq || !eq)
13286 return -ENODEV;
13287 if (!phba->sli4_hba.pc_sli4_params.supported)
13288 hw_page_size = SLI4_PAGE_SIZE;
13289
13290 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13291 if (!mbox)
13292 return -ENOMEM;
13293 length = (sizeof(struct lpfc_mbx_cq_create) -
13294 sizeof(struct lpfc_sli4_cfg_mhdr));
13295 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13296 LPFC_MBOX_OPCODE_CQ_CREATE,
13297 length, LPFC_SLI4_MBX_EMBED);
13298 cq_create = &mbox->u.mqe.un.cq_create;
13299 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
13300 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
13301 cq->page_count);
13302 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
13303 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
13304 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13305 phba->sli4_hba.pc_sli4_params.cqv);
13306 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
13307 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
13308 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
13309 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
13310 eq->queue_id);
13311 } else {
13312 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
13313 eq->queue_id);
13314 }
13315 switch (cq->entry_count) {
13316 default:
13317 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13318 "0361 Unsupported CQ count. (%d)\n",
13319 cq->entry_count);
13320 if (cq->entry_count < 256) {
13321 status = -EINVAL;
13322 goto out;
13323 }
13324 /* otherwise default to smallest count (drop through) */
13325 case 256:
13326 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13327 LPFC_CQ_CNT_256);
13328 break;
13329 case 512:
13330 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13331 LPFC_CQ_CNT_512);
13332 break;
13333 case 1024:
13334 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
13335 LPFC_CQ_CNT_1024);
13336 break;
13337 }
13338 list_for_each_entry(dmabuf, &cq->page_list, list) {
13339 memset(dmabuf->virt, 0, hw_page_size);
13340 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13341 putPaddrLow(dmabuf->phys);
13342 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13343 putPaddrHigh(dmabuf->phys);
13344 }
13345 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13346
13347 /* The IOCTL status is embedded in the mailbox subheader. */
13348 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13349 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13350 if (shdr_status || shdr_add_status || rc) {
13351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13352 "2501 CQ_CREATE mailbox failed with "
13353 "status x%x add_status x%x, mbx status x%x\n",
13354 shdr_status, shdr_add_status, rc);
13355 status = -ENXIO;
13356 goto out;
13357 }
13358 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13359 if (cq->queue_id == 0xFFFF) {
13360 status = -ENXIO;
13361 goto out;
13362 }
13363 /* link the cq onto the parent eq child list */
13364 list_add_tail(&cq->list, &eq->child_list);
13365 /* Set up completion queue's type and subtype */
13366 cq->type = type;
13367 cq->subtype = subtype;
13368 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
13369 cq->assoc_qid = eq->queue_id;
13370 cq->host_index = 0;
13371 cq->hba_index = 0;
13372
13373 out:
13374 mempool_free(mbox, phba->mbox_mem_pool);
13375 return status;
13376 }
13377
13378 /**
13379 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
13380 * @phba: HBA structure that indicates port to create a queue on.
13381 * @mq: The queue structure to use to create the mailbox queue.
13382 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
13383 * @cq: The completion queue to associate with this cq.
13384 *
13385 * This function provides failback (fb) functionality when the
13386 * mq_create_ext fails on older FW generations. It's purpose is identical
13387 * to mq_create_ext otherwise.
13388 *
13389 * This routine cannot fail as all attributes were previously accessed and
13390 * initialized in mq_create_ext.
13391 **/
13392 static void
13393 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
13394 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
13395 {
13396 struct lpfc_mbx_mq_create *mq_create;
13397 struct lpfc_dmabuf *dmabuf;
13398 int length;
13399
13400 length = (sizeof(struct lpfc_mbx_mq_create) -
13401 sizeof(struct lpfc_sli4_cfg_mhdr));
13402 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13403 LPFC_MBOX_OPCODE_MQ_CREATE,
13404 length, LPFC_SLI4_MBX_EMBED);
13405 mq_create = &mbox->u.mqe.un.mq_create;
13406 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
13407 mq->page_count);
13408 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
13409 cq->queue_id);
13410 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
13411 switch (mq->entry_count) {
13412 case 16:
13413 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13414 LPFC_MQ_RING_SIZE_16);
13415 break;
13416 case 32:
13417 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13418 LPFC_MQ_RING_SIZE_32);
13419 break;
13420 case 64:
13421 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13422 LPFC_MQ_RING_SIZE_64);
13423 break;
13424 case 128:
13425 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
13426 LPFC_MQ_RING_SIZE_128);
13427 break;
13428 }
13429 list_for_each_entry(dmabuf, &mq->page_list, list) {
13430 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13431 putPaddrLow(dmabuf->phys);
13432 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13433 putPaddrHigh(dmabuf->phys);
13434 }
13435 }
13436
13437 /**
13438 * lpfc_mq_create - Create a mailbox Queue on the HBA
13439 * @phba: HBA structure that indicates port to create a queue on.
13440 * @mq: The queue structure to use to create the mailbox queue.
13441 * @cq: The completion queue to associate with this cq.
13442 * @subtype: The queue's subtype.
13443 *
13444 * This function creates a mailbox queue, as detailed in @mq, on a port,
13445 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
13446 *
13447 * The @phba struct is used to send mailbox command to HBA. The @cq struct
13448 * is used to get the entry count and entry size that are necessary to
13449 * determine the number of pages to allocate and use for this queue. This
13450 * function will send the MQ_CREATE mailbox command to the HBA to setup the
13451 * mailbox queue. This function is asynchronous and will wait for the mailbox
13452 * command to finish before continuing.
13453 *
13454 * On success this function will return a zero. If unable to allocate enough
13455 * memory this function will return -ENOMEM. If the queue create mailbox command
13456 * fails this function will return -ENXIO.
13457 **/
13458 int32_t
13459 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
13460 struct lpfc_queue *cq, uint32_t subtype)
13461 {
13462 struct lpfc_mbx_mq_create *mq_create;
13463 struct lpfc_mbx_mq_create_ext *mq_create_ext;
13464 struct lpfc_dmabuf *dmabuf;
13465 LPFC_MBOXQ_t *mbox;
13466 int rc, length, status = 0;
13467 uint32_t shdr_status, shdr_add_status;
13468 union lpfc_sli4_cfg_shdr *shdr;
13469 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13470
13471 /* sanity check on queue memory */
13472 if (!mq || !cq)
13473 return -ENODEV;
13474 if (!phba->sli4_hba.pc_sli4_params.supported)
13475 hw_page_size = SLI4_PAGE_SIZE;
13476
13477 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13478 if (!mbox)
13479 return -ENOMEM;
13480 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
13481 sizeof(struct lpfc_sli4_cfg_mhdr));
13482 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13483 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
13484 length, LPFC_SLI4_MBX_EMBED);
13485
13486 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
13487 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
13488 bf_set(lpfc_mbx_mq_create_ext_num_pages,
13489 &mq_create_ext->u.request, mq->page_count);
13490 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
13491 &mq_create_ext->u.request, 1);
13492 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
13493 &mq_create_ext->u.request, 1);
13494 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
13495 &mq_create_ext->u.request, 1);
13496 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
13497 &mq_create_ext->u.request, 1);
13498 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
13499 &mq_create_ext->u.request, 1);
13500 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
13501 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13502 phba->sli4_hba.pc_sli4_params.mqv);
13503 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
13504 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
13505 cq->queue_id);
13506 else
13507 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
13508 cq->queue_id);
13509 switch (mq->entry_count) {
13510 default:
13511 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13512 "0362 Unsupported MQ count. (%d)\n",
13513 mq->entry_count);
13514 if (mq->entry_count < 16) {
13515 status = -EINVAL;
13516 goto out;
13517 }
13518 /* otherwise default to smallest count (drop through) */
13519 case 16:
13520 bf_set(lpfc_mq_context_ring_size,
13521 &mq_create_ext->u.request.context,
13522 LPFC_MQ_RING_SIZE_16);
13523 break;
13524 case 32:
13525 bf_set(lpfc_mq_context_ring_size,
13526 &mq_create_ext->u.request.context,
13527 LPFC_MQ_RING_SIZE_32);
13528 break;
13529 case 64:
13530 bf_set(lpfc_mq_context_ring_size,
13531 &mq_create_ext->u.request.context,
13532 LPFC_MQ_RING_SIZE_64);
13533 break;
13534 case 128:
13535 bf_set(lpfc_mq_context_ring_size,
13536 &mq_create_ext->u.request.context,
13537 LPFC_MQ_RING_SIZE_128);
13538 break;
13539 }
13540 list_for_each_entry(dmabuf, &mq->page_list, list) {
13541 memset(dmabuf->virt, 0, hw_page_size);
13542 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
13543 putPaddrLow(dmabuf->phys);
13544 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
13545 putPaddrHigh(dmabuf->phys);
13546 }
13547 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13548 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13549 &mq_create_ext->u.response);
13550 if (rc != MBX_SUCCESS) {
13551 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13552 "2795 MQ_CREATE_EXT failed with "
13553 "status x%x. Failback to MQ_CREATE.\n",
13554 rc);
13555 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
13556 mq_create = &mbox->u.mqe.un.mq_create;
13557 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13558 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
13559 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
13560 &mq_create->u.response);
13561 }
13562
13563 /* The IOCTL status is embedded in the mailbox subheader. */
13564 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13565 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13566 if (shdr_status || shdr_add_status || rc) {
13567 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13568 "2502 MQ_CREATE mailbox failed with "
13569 "status x%x add_status x%x, mbx status x%x\n",
13570 shdr_status, shdr_add_status, rc);
13571 status = -ENXIO;
13572 goto out;
13573 }
13574 if (mq->queue_id == 0xFFFF) {
13575 status = -ENXIO;
13576 goto out;
13577 }
13578 mq->type = LPFC_MQ;
13579 mq->assoc_qid = cq->queue_id;
13580 mq->subtype = subtype;
13581 mq->host_index = 0;
13582 mq->hba_index = 0;
13583
13584 /* link the mq onto the parent cq child list */
13585 list_add_tail(&mq->list, &cq->child_list);
13586 out:
13587 mempool_free(mbox, phba->mbox_mem_pool);
13588 return status;
13589 }
13590
13591 /**
13592 * lpfc_wq_create - Create a Work Queue on the HBA
13593 * @phba: HBA structure that indicates port to create a queue on.
13594 * @wq: The queue structure to use to create the work queue.
13595 * @cq: The completion queue to bind this work queue to.
13596 * @subtype: The subtype of the work queue indicating its functionality.
13597 *
13598 * This function creates a work queue, as detailed in @wq, on a port, described
13599 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
13600 *
13601 * The @phba struct is used to send mailbox command to HBA. The @wq struct
13602 * is used to get the entry count and entry size that are necessary to
13603 * determine the number of pages to allocate and use for this queue. The @cq
13604 * is used to indicate which completion queue to bind this work queue to. This
13605 * function will send the WQ_CREATE mailbox command to the HBA to setup the
13606 * work queue. This function is asynchronous and will wait for the mailbox
13607 * command to finish before continuing.
13608 *
13609 * On success this function will return a zero. If unable to allocate enough
13610 * memory this function will return -ENOMEM. If the queue create mailbox command
13611 * fails this function will return -ENXIO.
13612 **/
13613 int
13614 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
13615 struct lpfc_queue *cq, uint32_t subtype)
13616 {
13617 struct lpfc_mbx_wq_create *wq_create;
13618 struct lpfc_dmabuf *dmabuf;
13619 LPFC_MBOXQ_t *mbox;
13620 int rc, length, status = 0;
13621 uint32_t shdr_status, shdr_add_status;
13622 union lpfc_sli4_cfg_shdr *shdr;
13623 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13624 struct dma_address *page;
13625 void __iomem *bar_memmap_p;
13626 uint32_t db_offset;
13627 uint16_t pci_barset;
13628
13629 /* sanity check on queue memory */
13630 if (!wq || !cq)
13631 return -ENODEV;
13632 if (!phba->sli4_hba.pc_sli4_params.supported)
13633 hw_page_size = SLI4_PAGE_SIZE;
13634
13635 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13636 if (!mbox)
13637 return -ENOMEM;
13638 length = (sizeof(struct lpfc_mbx_wq_create) -
13639 sizeof(struct lpfc_sli4_cfg_mhdr));
13640 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13641 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
13642 length, LPFC_SLI4_MBX_EMBED);
13643 wq_create = &mbox->u.mqe.un.wq_create;
13644 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
13645 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
13646 wq->page_count);
13647 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
13648 cq->queue_id);
13649
13650 /* wqv is the earliest version supported, NOT the latest */
13651 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13652 phba->sli4_hba.pc_sli4_params.wqv);
13653
13654 switch (phba->sli4_hba.pc_sli4_params.wqv) {
13655 case LPFC_Q_CREATE_VERSION_0:
13656 switch (wq->entry_size) {
13657 default:
13658 case 64:
13659 /* Nothing to do, version 0 ONLY supports 64 byte */
13660 page = wq_create->u.request.page;
13661 break;
13662 case 128:
13663 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13664 LPFC_WQ_SZ128_SUPPORT)) {
13665 status = -ERANGE;
13666 goto out;
13667 }
13668 /* If we get here the HBA MUST also support V1 and
13669 * we MUST use it
13670 */
13671 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13672 LPFC_Q_CREATE_VERSION_1);
13673
13674 bf_set(lpfc_mbx_wq_create_wqe_count,
13675 &wq_create->u.request_1, wq->entry_count);
13676 bf_set(lpfc_mbx_wq_create_wqe_size,
13677 &wq_create->u.request_1,
13678 LPFC_WQ_WQE_SIZE_128);
13679 bf_set(lpfc_mbx_wq_create_page_size,
13680 &wq_create->u.request_1,
13681 (PAGE_SIZE/SLI4_PAGE_SIZE));
13682 page = wq_create->u.request_1.page;
13683 break;
13684 }
13685 break;
13686 case LPFC_Q_CREATE_VERSION_1:
13687 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
13688 wq->entry_count);
13689 switch (wq->entry_size) {
13690 default:
13691 case 64:
13692 bf_set(lpfc_mbx_wq_create_wqe_size,
13693 &wq_create->u.request_1,
13694 LPFC_WQ_WQE_SIZE_64);
13695 break;
13696 case 128:
13697 if (!(phba->sli4_hba.pc_sli4_params.wqsize &
13698 LPFC_WQ_SZ128_SUPPORT)) {
13699 status = -ERANGE;
13700 goto out;
13701 }
13702 bf_set(lpfc_mbx_wq_create_wqe_size,
13703 &wq_create->u.request_1,
13704 LPFC_WQ_WQE_SIZE_128);
13705 break;
13706 }
13707 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
13708 (PAGE_SIZE/SLI4_PAGE_SIZE));
13709 page = wq_create->u.request_1.page;
13710 break;
13711 default:
13712 status = -ERANGE;
13713 goto out;
13714 }
13715
13716 list_for_each_entry(dmabuf, &wq->page_list, list) {
13717 memset(dmabuf->virt, 0, hw_page_size);
13718 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
13719 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
13720 }
13721
13722 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13723 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
13724
13725 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13726 /* The IOCTL status is embedded in the mailbox subheader. */
13727 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13728 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13729 if (shdr_status || shdr_add_status || rc) {
13730 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13731 "2503 WQ_CREATE mailbox failed with "
13732 "status x%x add_status x%x, mbx status x%x\n",
13733 shdr_status, shdr_add_status, rc);
13734 status = -ENXIO;
13735 goto out;
13736 }
13737 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
13738 if (wq->queue_id == 0xFFFF) {
13739 status = -ENXIO;
13740 goto out;
13741 }
13742 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13743 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
13744 &wq_create->u.response);
13745 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
13746 (wq->db_format != LPFC_DB_RING_FORMAT)) {
13747 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13748 "3265 WQ[%d] doorbell format not "
13749 "supported: x%x\n", wq->queue_id,
13750 wq->db_format);
13751 status = -EINVAL;
13752 goto out;
13753 }
13754 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
13755 &wq_create->u.response);
13756 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13757 if (!bar_memmap_p) {
13758 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13759 "3263 WQ[%d] failed to memmap pci "
13760 "barset:x%x\n", wq->queue_id,
13761 pci_barset);
13762 status = -ENOMEM;
13763 goto out;
13764 }
13765 db_offset = wq_create->u.response.doorbell_offset;
13766 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
13767 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
13768 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13769 "3252 WQ[%d] doorbell offset not "
13770 "supported: x%x\n", wq->queue_id,
13771 db_offset);
13772 status = -EINVAL;
13773 goto out;
13774 }
13775 wq->db_regaddr = bar_memmap_p + db_offset;
13776 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13777 "3264 WQ[%d]: barset:x%x, offset:x%x, "
13778 "format:x%x\n", wq->queue_id, pci_barset,
13779 db_offset, wq->db_format);
13780 } else {
13781 wq->db_format = LPFC_DB_LIST_FORMAT;
13782 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
13783 }
13784 wq->type = LPFC_WQ;
13785 wq->assoc_qid = cq->queue_id;
13786 wq->subtype = subtype;
13787 wq->host_index = 0;
13788 wq->hba_index = 0;
13789 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
13790
13791 /* link the wq onto the parent cq child list */
13792 list_add_tail(&wq->list, &cq->child_list);
13793 out:
13794 mempool_free(mbox, phba->mbox_mem_pool);
13795 return status;
13796 }
13797
13798 /**
13799 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
13800 * @phba: HBA structure that indicates port to create a queue on.
13801 * @rq: The queue structure to use for the receive queue.
13802 * @qno: The associated HBQ number
13803 *
13804 *
13805 * For SLI4 we need to adjust the RQ repost value based on
13806 * the number of buffers that are initially posted to the RQ.
13807 */
13808 void
13809 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
13810 {
13811 uint32_t cnt;
13812
13813 /* sanity check on queue memory */
13814 if (!rq)
13815 return;
13816 cnt = lpfc_hbq_defs[qno]->entry_count;
13817
13818 /* Recalc repost for RQs based on buffers initially posted */
13819 cnt = (cnt >> 3);
13820 if (cnt < LPFC_QUEUE_MIN_REPOST)
13821 cnt = LPFC_QUEUE_MIN_REPOST;
13822
13823 rq->entry_repost = cnt;
13824 }
13825
13826 /**
13827 * lpfc_rq_create - Create a Receive Queue on the HBA
13828 * @phba: HBA structure that indicates port to create a queue on.
13829 * @hrq: The queue structure to use to create the header receive queue.
13830 * @drq: The queue structure to use to create the data receive queue.
13831 * @cq: The completion queue to bind this work queue to.
13832 *
13833 * This function creates a receive buffer queue pair , as detailed in @hrq and
13834 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
13835 * to the HBA.
13836 *
13837 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
13838 * struct is used to get the entry count that is necessary to determine the
13839 * number of pages to use for this queue. The @cq is used to indicate which
13840 * completion queue to bind received buffers that are posted to these queues to.
13841 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
13842 * receive queue pair. This function is asynchronous and will wait for the
13843 * mailbox command to finish before continuing.
13844 *
13845 * On success this function will return a zero. If unable to allocate enough
13846 * memory this function will return -ENOMEM. If the queue create mailbox command
13847 * fails this function will return -ENXIO.
13848 **/
13849 int
13850 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13851 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
13852 {
13853 struct lpfc_mbx_rq_create *rq_create;
13854 struct lpfc_dmabuf *dmabuf;
13855 LPFC_MBOXQ_t *mbox;
13856 int rc, length, status = 0;
13857 uint32_t shdr_status, shdr_add_status;
13858 union lpfc_sli4_cfg_shdr *shdr;
13859 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
13860 void __iomem *bar_memmap_p;
13861 uint32_t db_offset;
13862 uint16_t pci_barset;
13863
13864 /* sanity check on queue memory */
13865 if (!hrq || !drq || !cq)
13866 return -ENODEV;
13867 if (!phba->sli4_hba.pc_sli4_params.supported)
13868 hw_page_size = SLI4_PAGE_SIZE;
13869
13870 if (hrq->entry_count != drq->entry_count)
13871 return -EINVAL;
13872 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13873 if (!mbox)
13874 return -ENOMEM;
13875 length = (sizeof(struct lpfc_mbx_rq_create) -
13876 sizeof(struct lpfc_sli4_cfg_mhdr));
13877 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13878 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13879 length, LPFC_SLI4_MBX_EMBED);
13880 rq_create = &mbox->u.mqe.un.rq_create;
13881 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13882 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13883 phba->sli4_hba.pc_sli4_params.rqv);
13884 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13885 bf_set(lpfc_rq_context_rqe_count_1,
13886 &rq_create->u.request.context,
13887 hrq->entry_count);
13888 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
13889 bf_set(lpfc_rq_context_rqe_size,
13890 &rq_create->u.request.context,
13891 LPFC_RQE_SIZE_8);
13892 bf_set(lpfc_rq_context_page_size,
13893 &rq_create->u.request.context,
13894 (PAGE_SIZE/SLI4_PAGE_SIZE));
13895 } else {
13896 switch (hrq->entry_count) {
13897 default:
13898 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13899 "2535 Unsupported RQ count. (%d)\n",
13900 hrq->entry_count);
13901 if (hrq->entry_count < 512) {
13902 status = -EINVAL;
13903 goto out;
13904 }
13905 /* otherwise default to smallest count (drop through) */
13906 case 512:
13907 bf_set(lpfc_rq_context_rqe_count,
13908 &rq_create->u.request.context,
13909 LPFC_RQ_RING_SIZE_512);
13910 break;
13911 case 1024:
13912 bf_set(lpfc_rq_context_rqe_count,
13913 &rq_create->u.request.context,
13914 LPFC_RQ_RING_SIZE_1024);
13915 break;
13916 case 2048:
13917 bf_set(lpfc_rq_context_rqe_count,
13918 &rq_create->u.request.context,
13919 LPFC_RQ_RING_SIZE_2048);
13920 break;
13921 case 4096:
13922 bf_set(lpfc_rq_context_rqe_count,
13923 &rq_create->u.request.context,
13924 LPFC_RQ_RING_SIZE_4096);
13925 break;
13926 }
13927 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13928 LPFC_HDR_BUF_SIZE);
13929 }
13930 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13931 cq->queue_id);
13932 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13933 hrq->page_count);
13934 list_for_each_entry(dmabuf, &hrq->page_list, list) {
13935 memset(dmabuf->virt, 0, hw_page_size);
13936 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13937 putPaddrLow(dmabuf->phys);
13938 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13939 putPaddrHigh(dmabuf->phys);
13940 }
13941 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13942 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13943
13944 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13945 /* The IOCTL status is embedded in the mailbox subheader. */
13946 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13947 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13948 if (shdr_status || shdr_add_status || rc) {
13949 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13950 "2504 RQ_CREATE mailbox failed with "
13951 "status x%x add_status x%x, mbx status x%x\n",
13952 shdr_status, shdr_add_status, rc);
13953 status = -ENXIO;
13954 goto out;
13955 }
13956 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13957 if (hrq->queue_id == 0xFFFF) {
13958 status = -ENXIO;
13959 goto out;
13960 }
13961
13962 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13963 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13964 &rq_create->u.response);
13965 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13966 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13967 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13968 "3262 RQ [%d] doorbell format not "
13969 "supported: x%x\n", hrq->queue_id,
13970 hrq->db_format);
13971 status = -EINVAL;
13972 goto out;
13973 }
13974
13975 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13976 &rq_create->u.response);
13977 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13978 if (!bar_memmap_p) {
13979 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13980 "3269 RQ[%d] failed to memmap pci "
13981 "barset:x%x\n", hrq->queue_id,
13982 pci_barset);
13983 status = -ENOMEM;
13984 goto out;
13985 }
13986
13987 db_offset = rq_create->u.response.doorbell_offset;
13988 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13989 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13990 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13991 "3270 RQ[%d] doorbell offset not "
13992 "supported: x%x\n", hrq->queue_id,
13993 db_offset);
13994 status = -EINVAL;
13995 goto out;
13996 }
13997 hrq->db_regaddr = bar_memmap_p + db_offset;
13998 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13999 "3266 RQ[qid:%d]: barset:x%x, offset:x%x, "
14000 "format:x%x\n", hrq->queue_id, pci_barset,
14001 db_offset, hrq->db_format);
14002 } else {
14003 hrq->db_format = LPFC_DB_RING_FORMAT;
14004 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
14005 }
14006 hrq->type = LPFC_HRQ;
14007 hrq->assoc_qid = cq->queue_id;
14008 hrq->subtype = subtype;
14009 hrq->host_index = 0;
14010 hrq->hba_index = 0;
14011
14012 /* now create the data queue */
14013 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14014 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
14015 length, LPFC_SLI4_MBX_EMBED);
14016 bf_set(lpfc_mbox_hdr_version, &shdr->request,
14017 phba->sli4_hba.pc_sli4_params.rqv);
14018 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
14019 bf_set(lpfc_rq_context_rqe_count_1,
14020 &rq_create->u.request.context, hrq->entry_count);
14021 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
14022 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
14023 LPFC_RQE_SIZE_8);
14024 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
14025 (PAGE_SIZE/SLI4_PAGE_SIZE));
14026 } else {
14027 switch (drq->entry_count) {
14028 default:
14029 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14030 "2536 Unsupported RQ count. (%d)\n",
14031 drq->entry_count);
14032 if (drq->entry_count < 512) {
14033 status = -EINVAL;
14034 goto out;
14035 }
14036 /* otherwise default to smallest count (drop through) */
14037 case 512:
14038 bf_set(lpfc_rq_context_rqe_count,
14039 &rq_create->u.request.context,
14040 LPFC_RQ_RING_SIZE_512);
14041 break;
14042 case 1024:
14043 bf_set(lpfc_rq_context_rqe_count,
14044 &rq_create->u.request.context,
14045 LPFC_RQ_RING_SIZE_1024);
14046 break;
14047 case 2048:
14048 bf_set(lpfc_rq_context_rqe_count,
14049 &rq_create->u.request.context,
14050 LPFC_RQ_RING_SIZE_2048);
14051 break;
14052 case 4096:
14053 bf_set(lpfc_rq_context_rqe_count,
14054 &rq_create->u.request.context,
14055 LPFC_RQ_RING_SIZE_4096);
14056 break;
14057 }
14058 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
14059 LPFC_DATA_BUF_SIZE);
14060 }
14061 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
14062 cq->queue_id);
14063 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
14064 drq->page_count);
14065 list_for_each_entry(dmabuf, &drq->page_list, list) {
14066 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
14067 putPaddrLow(dmabuf->phys);
14068 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
14069 putPaddrHigh(dmabuf->phys);
14070 }
14071 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
14072 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
14073 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14074 /* The IOCTL status is embedded in the mailbox subheader. */
14075 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
14076 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14077 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14078 if (shdr_status || shdr_add_status || rc) {
14079 status = -ENXIO;
14080 goto out;
14081 }
14082 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
14083 if (drq->queue_id == 0xFFFF) {
14084 status = -ENXIO;
14085 goto out;
14086 }
14087 drq->type = LPFC_DRQ;
14088 drq->assoc_qid = cq->queue_id;
14089 drq->subtype = subtype;
14090 drq->host_index = 0;
14091 drq->hba_index = 0;
14092
14093 /* link the header and data RQs onto the parent cq child list */
14094 list_add_tail(&hrq->list, &cq->child_list);
14095 list_add_tail(&drq->list, &cq->child_list);
14096
14097 out:
14098 mempool_free(mbox, phba->mbox_mem_pool);
14099 return status;
14100 }
14101
14102 /**
14103 * lpfc_eq_destroy - Destroy an event Queue on the HBA
14104 * @eq: The queue structure associated with the queue to destroy.
14105 *
14106 * This function destroys a queue, as detailed in @eq by sending an mailbox
14107 * command, specific to the type of queue, to the HBA.
14108 *
14109 * The @eq struct is used to get the queue ID of the queue to destroy.
14110 *
14111 * On success this function will return a zero. If the queue destroy mailbox
14112 * command fails this function will return -ENXIO.
14113 **/
14114 int
14115 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
14116 {
14117 LPFC_MBOXQ_t *mbox;
14118 int rc, length, status = 0;
14119 uint32_t shdr_status, shdr_add_status;
14120 union lpfc_sli4_cfg_shdr *shdr;
14121
14122 /* sanity check on queue memory */
14123 if (!eq)
14124 return -ENODEV;
14125 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
14126 if (!mbox)
14127 return -ENOMEM;
14128 length = (sizeof(struct lpfc_mbx_eq_destroy) -
14129 sizeof(struct lpfc_sli4_cfg_mhdr));
14130 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14131 LPFC_MBOX_OPCODE_EQ_DESTROY,
14132 length, LPFC_SLI4_MBX_EMBED);
14133 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
14134 eq->queue_id);
14135 mbox->vport = eq->phba->pport;
14136 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14137
14138 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
14139 /* The IOCTL status is embedded in the mailbox subheader. */
14140 shdr = (union lpfc_sli4_cfg_shdr *)
14141 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
14142 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14143 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14144 if (shdr_status || shdr_add_status || rc) {
14145 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14146 "2505 EQ_DESTROY mailbox failed with "
14147 "status x%x add_status x%x, mbx status x%x\n",
14148 shdr_status, shdr_add_status, rc);
14149 status = -ENXIO;
14150 }
14151
14152 /* Remove eq from any list */
14153 list_del_init(&eq->list);
14154 mempool_free(mbox, eq->phba->mbox_mem_pool);
14155 return status;
14156 }
14157
14158 /**
14159 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
14160 * @cq: The queue structure associated with the queue to destroy.
14161 *
14162 * This function destroys a queue, as detailed in @cq by sending an mailbox
14163 * command, specific to the type of queue, to the HBA.
14164 *
14165 * The @cq struct is used to get the queue ID of the queue to destroy.
14166 *
14167 * On success this function will return a zero. If the queue destroy mailbox
14168 * command fails this function will return -ENXIO.
14169 **/
14170 int
14171 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
14172 {
14173 LPFC_MBOXQ_t *mbox;
14174 int rc, length, status = 0;
14175 uint32_t shdr_status, shdr_add_status;
14176 union lpfc_sli4_cfg_shdr *shdr;
14177
14178 /* sanity check on queue memory */
14179 if (!cq)
14180 return -ENODEV;
14181 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
14182 if (!mbox)
14183 return -ENOMEM;
14184 length = (sizeof(struct lpfc_mbx_cq_destroy) -
14185 sizeof(struct lpfc_sli4_cfg_mhdr));
14186 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14187 LPFC_MBOX_OPCODE_CQ_DESTROY,
14188 length, LPFC_SLI4_MBX_EMBED);
14189 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
14190 cq->queue_id);
14191 mbox->vport = cq->phba->pport;
14192 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14193 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
14194 /* The IOCTL status is embedded in the mailbox subheader. */
14195 shdr = (union lpfc_sli4_cfg_shdr *)
14196 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
14197 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14198 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14199 if (shdr_status || shdr_add_status || rc) {
14200 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14201 "2506 CQ_DESTROY mailbox failed with "
14202 "status x%x add_status x%x, mbx status x%x\n",
14203 shdr_status, shdr_add_status, rc);
14204 status = -ENXIO;
14205 }
14206 /* Remove cq from any list */
14207 list_del_init(&cq->list);
14208 mempool_free(mbox, cq->phba->mbox_mem_pool);
14209 return status;
14210 }
14211
14212 /**
14213 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
14214 * @qm: The queue structure associated with the queue to destroy.
14215 *
14216 * This function destroys a queue, as detailed in @mq by sending an mailbox
14217 * command, specific to the type of queue, to the HBA.
14218 *
14219 * The @mq struct is used to get the queue ID of the queue to destroy.
14220 *
14221 * On success this function will return a zero. If the queue destroy mailbox
14222 * command fails this function will return -ENXIO.
14223 **/
14224 int
14225 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
14226 {
14227 LPFC_MBOXQ_t *mbox;
14228 int rc, length, status = 0;
14229 uint32_t shdr_status, shdr_add_status;
14230 union lpfc_sli4_cfg_shdr *shdr;
14231
14232 /* sanity check on queue memory */
14233 if (!mq)
14234 return -ENODEV;
14235 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
14236 if (!mbox)
14237 return -ENOMEM;
14238 length = (sizeof(struct lpfc_mbx_mq_destroy) -
14239 sizeof(struct lpfc_sli4_cfg_mhdr));
14240 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
14241 LPFC_MBOX_OPCODE_MQ_DESTROY,
14242 length, LPFC_SLI4_MBX_EMBED);
14243 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
14244 mq->queue_id);
14245 mbox->vport = mq->phba->pport;
14246 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14247 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
14248 /* The IOCTL status is embedded in the mailbox subheader. */
14249 shdr = (union lpfc_sli4_cfg_shdr *)
14250 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
14251 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14252 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14253 if (shdr_status || shdr_add_status || rc) {
14254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14255 "2507 MQ_DESTROY mailbox failed with "
14256 "status x%x add_status x%x, mbx status x%x\n",
14257 shdr_status, shdr_add_status, rc);
14258 status = -ENXIO;
14259 }
14260 /* Remove mq from any list */
14261 list_del_init(&mq->list);
14262 mempool_free(mbox, mq->phba->mbox_mem_pool);
14263 return status;
14264 }
14265
14266 /**
14267 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
14268 * @wq: The queue structure associated with the queue to destroy.
14269 *
14270 * This function destroys a queue, as detailed in @wq by sending an mailbox
14271 * command, specific to the type of queue, to the HBA.
14272 *
14273 * The @wq struct is used to get the queue ID of the queue to destroy.
14274 *
14275 * On success this function will return a zero. If the queue destroy mailbox
14276 * command fails this function will return -ENXIO.
14277 **/
14278 int
14279 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
14280 {
14281 LPFC_MBOXQ_t *mbox;
14282 int rc, length, status = 0;
14283 uint32_t shdr_status, shdr_add_status;
14284 union lpfc_sli4_cfg_shdr *shdr;
14285
14286 /* sanity check on queue memory */
14287 if (!wq)
14288 return -ENODEV;
14289 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
14290 if (!mbox)
14291 return -ENOMEM;
14292 length = (sizeof(struct lpfc_mbx_wq_destroy) -
14293 sizeof(struct lpfc_sli4_cfg_mhdr));
14294 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14295 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
14296 length, LPFC_SLI4_MBX_EMBED);
14297 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
14298 wq->queue_id);
14299 mbox->vport = wq->phba->pport;
14300 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14301 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
14302 shdr = (union lpfc_sli4_cfg_shdr *)
14303 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
14304 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14305 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14306 if (shdr_status || shdr_add_status || rc) {
14307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14308 "2508 WQ_DESTROY mailbox failed with "
14309 "status x%x add_status x%x, mbx status x%x\n",
14310 shdr_status, shdr_add_status, rc);
14311 status = -ENXIO;
14312 }
14313 /* Remove wq from any list */
14314 list_del_init(&wq->list);
14315 mempool_free(mbox, wq->phba->mbox_mem_pool);
14316 return status;
14317 }
14318
14319 /**
14320 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
14321 * @rq: The queue structure associated with the queue to destroy.
14322 *
14323 * This function destroys a queue, as detailed in @rq by sending an mailbox
14324 * command, specific to the type of queue, to the HBA.
14325 *
14326 * The @rq struct is used to get the queue ID of the queue to destroy.
14327 *
14328 * On success this function will return a zero. If the queue destroy mailbox
14329 * command fails this function will return -ENXIO.
14330 **/
14331 int
14332 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
14333 struct lpfc_queue *drq)
14334 {
14335 LPFC_MBOXQ_t *mbox;
14336 int rc, length, status = 0;
14337 uint32_t shdr_status, shdr_add_status;
14338 union lpfc_sli4_cfg_shdr *shdr;
14339
14340 /* sanity check on queue memory */
14341 if (!hrq || !drq)
14342 return -ENODEV;
14343 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
14344 if (!mbox)
14345 return -ENOMEM;
14346 length = (sizeof(struct lpfc_mbx_rq_destroy) -
14347 sizeof(struct lpfc_sli4_cfg_mhdr));
14348 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14349 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
14350 length, LPFC_SLI4_MBX_EMBED);
14351 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14352 hrq->queue_id);
14353 mbox->vport = hrq->phba->pport;
14354 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
14355 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
14356 /* The IOCTL status is embedded in the mailbox subheader. */
14357 shdr = (union lpfc_sli4_cfg_shdr *)
14358 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14359 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14360 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14361 if (shdr_status || shdr_add_status || rc) {
14362 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14363 "2509 RQ_DESTROY mailbox failed with "
14364 "status x%x add_status x%x, mbx status x%x\n",
14365 shdr_status, shdr_add_status, rc);
14366 if (rc != MBX_TIMEOUT)
14367 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14368 return -ENXIO;
14369 }
14370 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
14371 drq->queue_id);
14372 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
14373 shdr = (union lpfc_sli4_cfg_shdr *)
14374 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
14375 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14376 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14377 if (shdr_status || shdr_add_status || rc) {
14378 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14379 "2510 RQ_DESTROY mailbox failed with "
14380 "status x%x add_status x%x, mbx status x%x\n",
14381 shdr_status, shdr_add_status, rc);
14382 status = -ENXIO;
14383 }
14384 list_del_init(&hrq->list);
14385 list_del_init(&drq->list);
14386 mempool_free(mbox, hrq->phba->mbox_mem_pool);
14387 return status;
14388 }
14389
14390 /**
14391 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
14392 * @phba: The virtual port for which this call being executed.
14393 * @pdma_phys_addr0: Physical address of the 1st SGL page.
14394 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
14395 * @xritag: the xritag that ties this io to the SGL pages.
14396 *
14397 * This routine will post the sgl pages for the IO that has the xritag
14398 * that is in the iocbq structure. The xritag is assigned during iocbq
14399 * creation and persists for as long as the driver is loaded.
14400 * if the caller has fewer than 256 scatter gather segments to map then
14401 * pdma_phys_addr1 should be 0.
14402 * If the caller needs to map more than 256 scatter gather segment then
14403 * pdma_phys_addr1 should be a valid physical address.
14404 * physical address for SGLs must be 64 byte aligned.
14405 * If you are going to map 2 SGL's then the first one must have 256 entries
14406 * the second sgl can have between 1 and 256 entries.
14407 *
14408 * Return codes:
14409 * 0 - Success
14410 * -ENXIO, -ENOMEM - Failure
14411 **/
14412 int
14413 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
14414 dma_addr_t pdma_phys_addr0,
14415 dma_addr_t pdma_phys_addr1,
14416 uint16_t xritag)
14417 {
14418 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
14419 LPFC_MBOXQ_t *mbox;
14420 int rc;
14421 uint32_t shdr_status, shdr_add_status;
14422 uint32_t mbox_tmo;
14423 union lpfc_sli4_cfg_shdr *shdr;
14424
14425 if (xritag == NO_XRI) {
14426 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14427 "0364 Invalid param:\n");
14428 return -EINVAL;
14429 }
14430
14431 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14432 if (!mbox)
14433 return -ENOMEM;
14434
14435 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14436 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
14437 sizeof(struct lpfc_mbx_post_sgl_pages) -
14438 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
14439
14440 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
14441 &mbox->u.mqe.un.post_sgl_pages;
14442 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
14443 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
14444
14445 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
14446 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
14447 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
14448 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
14449
14450 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
14451 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
14452 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
14453 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
14454 if (!phba->sli4_hba.intr_enable)
14455 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14456 else {
14457 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14458 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14459 }
14460 /* The IOCTL status is embedded in the mailbox subheader. */
14461 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
14462 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14463 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14464 if (rc != MBX_TIMEOUT)
14465 mempool_free(mbox, phba->mbox_mem_pool);
14466 if (shdr_status || shdr_add_status || rc) {
14467 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14468 "2511 POST_SGL mailbox failed with "
14469 "status x%x add_status x%x, mbx status x%x\n",
14470 shdr_status, shdr_add_status, rc);
14471 }
14472 return 0;
14473 }
14474
14475 /**
14476 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
14477 * @phba: pointer to lpfc hba data structure.
14478 *
14479 * This routine is invoked to post rpi header templates to the
14480 * HBA consistent with the SLI-4 interface spec. This routine
14481 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14482 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14483 *
14484 * Returns
14485 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14486 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14487 **/
14488 static uint16_t
14489 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
14490 {
14491 unsigned long xri;
14492
14493 /*
14494 * Fetch the next logical xri. Because this index is logical,
14495 * the driver starts at 0 each time.
14496 */
14497 spin_lock_irq(&phba->hbalock);
14498 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
14499 phba->sli4_hba.max_cfg_param.max_xri, 0);
14500 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
14501 spin_unlock_irq(&phba->hbalock);
14502 return NO_XRI;
14503 } else {
14504 set_bit(xri, phba->sli4_hba.xri_bmask);
14505 phba->sli4_hba.max_cfg_param.xri_used++;
14506 }
14507 spin_unlock_irq(&phba->hbalock);
14508 return xri;
14509 }
14510
14511 /**
14512 * lpfc_sli4_free_xri - Release an xri for reuse.
14513 * @phba: pointer to lpfc hba data structure.
14514 *
14515 * This routine is invoked to release an xri to the pool of
14516 * available rpis maintained by the driver.
14517 **/
14518 static void
14519 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14520 {
14521 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
14522 phba->sli4_hba.max_cfg_param.xri_used--;
14523 }
14524 }
14525
14526 /**
14527 * lpfc_sli4_free_xri - Release an xri for reuse.
14528 * @phba: pointer to lpfc hba data structure.
14529 *
14530 * This routine is invoked to release an xri to the pool of
14531 * available rpis maintained by the driver.
14532 **/
14533 void
14534 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
14535 {
14536 spin_lock_irq(&phba->hbalock);
14537 __lpfc_sli4_free_xri(phba, xri);
14538 spin_unlock_irq(&phba->hbalock);
14539 }
14540
14541 /**
14542 * lpfc_sli4_next_xritag - Get an xritag for the io
14543 * @phba: Pointer to HBA context object.
14544 *
14545 * This function gets an xritag for the iocb. If there is no unused xritag
14546 * it will return 0xffff.
14547 * The function returns the allocated xritag if successful, else returns zero.
14548 * Zero is not a valid xritag.
14549 * The caller is not required to hold any lock.
14550 **/
14551 uint16_t
14552 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
14553 {
14554 uint16_t xri_index;
14555
14556 xri_index = lpfc_sli4_alloc_xri(phba);
14557 if (xri_index == NO_XRI)
14558 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
14559 "2004 Failed to allocate XRI.last XRITAG is %d"
14560 " Max XRI is %d, Used XRI is %d\n",
14561 xri_index,
14562 phba->sli4_hba.max_cfg_param.max_xri,
14563 phba->sli4_hba.max_cfg_param.xri_used);
14564 return xri_index;
14565 }
14566
14567 /**
14568 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
14569 * @phba: pointer to lpfc hba data structure.
14570 * @post_sgl_list: pointer to els sgl entry list.
14571 * @count: number of els sgl entries on the list.
14572 *
14573 * This routine is invoked to post a block of driver's sgl pages to the
14574 * HBA using non-embedded mailbox command. No Lock is held. This routine
14575 * is only called when the driver is loading and after all IO has been
14576 * stopped.
14577 **/
14578 static int
14579 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
14580 struct list_head *post_sgl_list,
14581 int post_cnt)
14582 {
14583 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
14584 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14585 struct sgl_page_pairs *sgl_pg_pairs;
14586 void *viraddr;
14587 LPFC_MBOXQ_t *mbox;
14588 uint32_t reqlen, alloclen, pg_pairs;
14589 uint32_t mbox_tmo;
14590 uint16_t xritag_start = 0;
14591 int rc = 0;
14592 uint32_t shdr_status, shdr_add_status;
14593 union lpfc_sli4_cfg_shdr *shdr;
14594
14595 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
14596 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
14597 if (reqlen > SLI4_PAGE_SIZE) {
14598 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14599 "2559 Block sgl registration required DMA "
14600 "size (%d) great than a page\n", reqlen);
14601 return -ENOMEM;
14602 }
14603 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14604 if (!mbox)
14605 return -ENOMEM;
14606
14607 /* Allocate DMA memory and set up the non-embedded mailbox command */
14608 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14609 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14610 LPFC_SLI4_MBX_NEMBED);
14611
14612 if (alloclen < reqlen) {
14613 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14614 "0285 Allocated DMA memory size (%d) is "
14615 "less than the requested DMA memory "
14616 "size (%d)\n", alloclen, reqlen);
14617 lpfc_sli4_mbox_cmd_free(phba, mbox);
14618 return -ENOMEM;
14619 }
14620 /* Set up the SGL pages in the non-embedded DMA pages */
14621 viraddr = mbox->sge_array->addr[0];
14622 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14623 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14624
14625 pg_pairs = 0;
14626 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
14627 /* Set up the sge entry */
14628 sgl_pg_pairs->sgl_pg0_addr_lo =
14629 cpu_to_le32(putPaddrLow(sglq_entry->phys));
14630 sgl_pg_pairs->sgl_pg0_addr_hi =
14631 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
14632 sgl_pg_pairs->sgl_pg1_addr_lo =
14633 cpu_to_le32(putPaddrLow(0));
14634 sgl_pg_pairs->sgl_pg1_addr_hi =
14635 cpu_to_le32(putPaddrHigh(0));
14636
14637 /* Keep the first xritag on the list */
14638 if (pg_pairs == 0)
14639 xritag_start = sglq_entry->sli4_xritag;
14640 sgl_pg_pairs++;
14641 pg_pairs++;
14642 }
14643
14644 /* Complete initialization and perform endian conversion. */
14645 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14646 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
14647 sgl->word0 = cpu_to_le32(sgl->word0);
14648 if (!phba->sli4_hba.intr_enable)
14649 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14650 else {
14651 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14652 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14653 }
14654 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14655 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14656 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14657 if (rc != MBX_TIMEOUT)
14658 lpfc_sli4_mbox_cmd_free(phba, mbox);
14659 if (shdr_status || shdr_add_status || rc) {
14660 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14661 "2513 POST_SGL_BLOCK mailbox command failed "
14662 "status x%x add_status x%x mbx status x%x\n",
14663 shdr_status, shdr_add_status, rc);
14664 rc = -ENXIO;
14665 }
14666 return rc;
14667 }
14668
14669 /**
14670 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
14671 * @phba: pointer to lpfc hba data structure.
14672 * @sblist: pointer to scsi buffer list.
14673 * @count: number of scsi buffers on the list.
14674 *
14675 * This routine is invoked to post a block of @count scsi sgl pages from a
14676 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
14677 * No Lock is held.
14678 *
14679 **/
14680 int
14681 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
14682 struct list_head *sblist,
14683 int count)
14684 {
14685 struct lpfc_scsi_buf *psb;
14686 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
14687 struct sgl_page_pairs *sgl_pg_pairs;
14688 void *viraddr;
14689 LPFC_MBOXQ_t *mbox;
14690 uint32_t reqlen, alloclen, pg_pairs;
14691 uint32_t mbox_tmo;
14692 uint16_t xritag_start = 0;
14693 int rc = 0;
14694 uint32_t shdr_status, shdr_add_status;
14695 dma_addr_t pdma_phys_bpl1;
14696 union lpfc_sli4_cfg_shdr *shdr;
14697
14698 /* Calculate the requested length of the dma memory */
14699 reqlen = count * sizeof(struct sgl_page_pairs) +
14700 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
14701 if (reqlen > SLI4_PAGE_SIZE) {
14702 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
14703 "0217 Block sgl registration required DMA "
14704 "size (%d) great than a page\n", reqlen);
14705 return -ENOMEM;
14706 }
14707 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14708 if (!mbox) {
14709 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14710 "0283 Failed to allocate mbox cmd memory\n");
14711 return -ENOMEM;
14712 }
14713
14714 /* Allocate DMA memory and set up the non-embedded mailbox command */
14715 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
14716 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
14717 LPFC_SLI4_MBX_NEMBED);
14718
14719 if (alloclen < reqlen) {
14720 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14721 "2561 Allocated DMA memory size (%d) is "
14722 "less than the requested DMA memory "
14723 "size (%d)\n", alloclen, reqlen);
14724 lpfc_sli4_mbox_cmd_free(phba, mbox);
14725 return -ENOMEM;
14726 }
14727
14728 /* Get the first SGE entry from the non-embedded DMA memory */
14729 viraddr = mbox->sge_array->addr[0];
14730
14731 /* Set up the SGL pages in the non-embedded DMA pages */
14732 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
14733 sgl_pg_pairs = &sgl->sgl_pg_pairs;
14734
14735 pg_pairs = 0;
14736 list_for_each_entry(psb, sblist, list) {
14737 /* Set up the sge entry */
14738 sgl_pg_pairs->sgl_pg0_addr_lo =
14739 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
14740 sgl_pg_pairs->sgl_pg0_addr_hi =
14741 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
14742 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
14743 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
14744 else
14745 pdma_phys_bpl1 = 0;
14746 sgl_pg_pairs->sgl_pg1_addr_lo =
14747 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
14748 sgl_pg_pairs->sgl_pg1_addr_hi =
14749 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
14750 /* Keep the first xritag on the list */
14751 if (pg_pairs == 0)
14752 xritag_start = psb->cur_iocbq.sli4_xritag;
14753 sgl_pg_pairs++;
14754 pg_pairs++;
14755 }
14756 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
14757 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
14758 /* Perform endian conversion if necessary */
14759 sgl->word0 = cpu_to_le32(sgl->word0);
14760
14761 if (!phba->sli4_hba.intr_enable)
14762 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
14763 else {
14764 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
14765 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
14766 }
14767 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
14768 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14769 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14770 if (rc != MBX_TIMEOUT)
14771 lpfc_sli4_mbox_cmd_free(phba, mbox);
14772 if (shdr_status || shdr_add_status || rc) {
14773 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14774 "2564 POST_SGL_BLOCK mailbox command failed "
14775 "status x%x add_status x%x mbx status x%x\n",
14776 shdr_status, shdr_add_status, rc);
14777 rc = -ENXIO;
14778 }
14779 return rc;
14780 }
14781
14782 /**
14783 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
14784 * @phba: pointer to lpfc_hba struct that the frame was received on
14785 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14786 *
14787 * This function checks the fields in the @fc_hdr to see if the FC frame is a
14788 * valid type of frame that the LPFC driver will handle. This function will
14789 * return a zero if the frame is a valid frame or a non zero value when the
14790 * frame does not pass the check.
14791 **/
14792 static int
14793 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
14794 {
14795 /* make rctl_names static to save stack space */
14796 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
14797 char *type_names[] = FC_TYPE_NAMES_INIT;
14798 struct fc_vft_header *fc_vft_hdr;
14799 uint32_t *header = (uint32_t *) fc_hdr;
14800
14801 switch (fc_hdr->fh_r_ctl) {
14802 case FC_RCTL_DD_UNCAT: /* uncategorized information */
14803 case FC_RCTL_DD_SOL_DATA: /* solicited data */
14804 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
14805 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
14806 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
14807 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
14808 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
14809 case FC_RCTL_DD_CMD_STATUS: /* command status */
14810 case FC_RCTL_ELS_REQ: /* extended link services request */
14811 case FC_RCTL_ELS_REP: /* extended link services reply */
14812 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
14813 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
14814 case FC_RCTL_BA_NOP: /* basic link service NOP */
14815 case FC_RCTL_BA_ABTS: /* basic link service abort */
14816 case FC_RCTL_BA_RMC: /* remove connection */
14817 case FC_RCTL_BA_ACC: /* basic accept */
14818 case FC_RCTL_BA_RJT: /* basic reject */
14819 case FC_RCTL_BA_PRMT:
14820 case FC_RCTL_ACK_1: /* acknowledge_1 */
14821 case FC_RCTL_ACK_0: /* acknowledge_0 */
14822 case FC_RCTL_P_RJT: /* port reject */
14823 case FC_RCTL_F_RJT: /* fabric reject */
14824 case FC_RCTL_P_BSY: /* port busy */
14825 case FC_RCTL_F_BSY: /* fabric busy to data frame */
14826 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
14827 case FC_RCTL_LCR: /* link credit reset */
14828 case FC_RCTL_END: /* end */
14829 break;
14830 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
14831 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14832 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
14833 return lpfc_fc_frame_check(phba, fc_hdr);
14834 default:
14835 goto drop;
14836 }
14837 switch (fc_hdr->fh_type) {
14838 case FC_TYPE_BLS:
14839 case FC_TYPE_ELS:
14840 case FC_TYPE_FCP:
14841 case FC_TYPE_CT:
14842 break;
14843 case FC_TYPE_IP:
14844 case FC_TYPE_ILS:
14845 default:
14846 goto drop;
14847 }
14848
14849 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
14850 "2538 Received frame rctl:%s (x%x), type:%s (x%x), "
14851 "frame Data:%08x %08x %08x %08x %08x %08x %08x\n",
14852 rctl_names[fc_hdr->fh_r_ctl], fc_hdr->fh_r_ctl,
14853 type_names[fc_hdr->fh_type], fc_hdr->fh_type,
14854 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
14855 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
14856 be32_to_cpu(header[4]), be32_to_cpu(header[5]),
14857 be32_to_cpu(header[6]));
14858 return 0;
14859 drop:
14860 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
14861 "2539 Dropped frame rctl:%s type:%s\n",
14862 rctl_names[fc_hdr->fh_r_ctl],
14863 type_names[fc_hdr->fh_type]);
14864 return 1;
14865 }
14866
14867 /**
14868 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
14869 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14870 *
14871 * This function processes the FC header to retrieve the VFI from the VF
14872 * header, if one exists. This function will return the VFI if one exists
14873 * or 0 if no VSAN Header exists.
14874 **/
14875 static uint32_t
14876 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14877 {
14878 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14879
14880 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14881 return 0;
14882 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14883 }
14884
14885 /**
14886 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14887 * @phba: Pointer to the HBA structure to search for the vport on
14888 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14889 * @fcfi: The FC Fabric ID that the frame came from
14890 *
14891 * This function searches the @phba for a vport that matches the content of the
14892 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14893 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14894 * returns the matching vport pointer or NULL if unable to match frame to a
14895 * vport.
14896 **/
14897 static struct lpfc_vport *
14898 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14899 uint16_t fcfi)
14900 {
14901 struct lpfc_vport **vports;
14902 struct lpfc_vport *vport = NULL;
14903 int i;
14904 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14905 fc_hdr->fh_d_id[1] << 8 |
14906 fc_hdr->fh_d_id[2]);
14907
14908 if (did == Fabric_DID)
14909 return phba->pport;
14910 if ((phba->pport->fc_flag & FC_PT2PT) &&
14911 !(phba->link_state == LPFC_HBA_READY))
14912 return phba->pport;
14913
14914 vports = lpfc_create_vport_work_array(phba);
14915 if (vports != NULL)
14916 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14917 if (phba->fcf.fcfi == fcfi &&
14918 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14919 vports[i]->fc_myDID == did) {
14920 vport = vports[i];
14921 break;
14922 }
14923 }
14924 lpfc_destroy_vport_work_array(phba, vports);
14925 return vport;
14926 }
14927
14928 /**
14929 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14930 * @vport: The vport to work on.
14931 *
14932 * This function updates the receive sequence time stamp for this vport. The
14933 * receive sequence time stamp indicates the time that the last frame of the
14934 * the sequence that has been idle for the longest amount of time was received.
14935 * the driver uses this time stamp to indicate if any received sequences have
14936 * timed out.
14937 **/
14938 static void
14939 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14940 {
14941 struct lpfc_dmabuf *h_buf;
14942 struct hbq_dmabuf *dmabuf = NULL;
14943
14944 /* get the oldest sequence on the rcv list */
14945 h_buf = list_get_first(&vport->rcv_buffer_list,
14946 struct lpfc_dmabuf, list);
14947 if (!h_buf)
14948 return;
14949 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14950 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14951 }
14952
14953 /**
14954 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14955 * @vport: The vport that the received sequences were sent to.
14956 *
14957 * This function cleans up all outstanding received sequences. This is called
14958 * by the driver when a link event or user action invalidates all the received
14959 * sequences.
14960 **/
14961 void
14962 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14963 {
14964 struct lpfc_dmabuf *h_buf, *hnext;
14965 struct lpfc_dmabuf *d_buf, *dnext;
14966 struct hbq_dmabuf *dmabuf = NULL;
14967
14968 /* start with the oldest sequence on the rcv list */
14969 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14970 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14971 list_del_init(&dmabuf->hbuf.list);
14972 list_for_each_entry_safe(d_buf, dnext,
14973 &dmabuf->dbuf.list, list) {
14974 list_del_init(&d_buf->list);
14975 lpfc_in_buf_free(vport->phba, d_buf);
14976 }
14977 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14978 }
14979 }
14980
14981 /**
14982 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14983 * @vport: The vport that the received sequences were sent to.
14984 *
14985 * This function determines whether any received sequences have timed out by
14986 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14987 * indicates that there is at least one timed out sequence this routine will
14988 * go through the received sequences one at a time from most inactive to most
14989 * active to determine which ones need to be cleaned up. Once it has determined
14990 * that a sequence needs to be cleaned up it will simply free up the resources
14991 * without sending an abort.
14992 **/
14993 void
14994 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14995 {
14996 struct lpfc_dmabuf *h_buf, *hnext;
14997 struct lpfc_dmabuf *d_buf, *dnext;
14998 struct hbq_dmabuf *dmabuf = NULL;
14999 unsigned long timeout;
15000 int abort_count = 0;
15001
15002 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15003 vport->rcv_buffer_time_stamp);
15004 if (list_empty(&vport->rcv_buffer_list) ||
15005 time_before(jiffies, timeout))
15006 return;
15007 /* start with the oldest sequence on the rcv list */
15008 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
15009 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15010 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
15011 dmabuf->time_stamp);
15012 if (time_before(jiffies, timeout))
15013 break;
15014 abort_count++;
15015 list_del_init(&dmabuf->hbuf.list);
15016 list_for_each_entry_safe(d_buf, dnext,
15017 &dmabuf->dbuf.list, list) {
15018 list_del_init(&d_buf->list);
15019 lpfc_in_buf_free(vport->phba, d_buf);
15020 }
15021 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
15022 }
15023 if (abort_count)
15024 lpfc_update_rcv_time_stamp(vport);
15025 }
15026
15027 /**
15028 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
15029 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
15030 *
15031 * This function searches through the existing incomplete sequences that have
15032 * been sent to this @vport. If the frame matches one of the incomplete
15033 * sequences then the dbuf in the @dmabuf is added to the list of frames that
15034 * make up that sequence. If no sequence is found that matches this frame then
15035 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
15036 * This function returns a pointer to the first dmabuf in the sequence list that
15037 * the frame was linked to.
15038 **/
15039 static struct hbq_dmabuf *
15040 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15041 {
15042 struct fc_frame_header *new_hdr;
15043 struct fc_frame_header *temp_hdr;
15044 struct lpfc_dmabuf *d_buf;
15045 struct lpfc_dmabuf *h_buf;
15046 struct hbq_dmabuf *seq_dmabuf = NULL;
15047 struct hbq_dmabuf *temp_dmabuf = NULL;
15048 uint8_t found = 0;
15049
15050 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15051 dmabuf->time_stamp = jiffies;
15052 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15053
15054 /* Use the hdr_buf to find the sequence that this frame belongs to */
15055 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15056 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15057 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15058 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15059 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15060 continue;
15061 /* found a pending sequence that matches this frame */
15062 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15063 break;
15064 }
15065 if (!seq_dmabuf) {
15066 /*
15067 * This indicates first frame received for this sequence.
15068 * Queue the buffer on the vport's rcv_buffer_list.
15069 */
15070 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15071 lpfc_update_rcv_time_stamp(vport);
15072 return dmabuf;
15073 }
15074 temp_hdr = seq_dmabuf->hbuf.virt;
15075 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
15076 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
15077 list_del_init(&seq_dmabuf->hbuf.list);
15078 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
15079 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15080 lpfc_update_rcv_time_stamp(vport);
15081 return dmabuf;
15082 }
15083 /* move this sequence to the tail to indicate a young sequence */
15084 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
15085 seq_dmabuf->time_stamp = jiffies;
15086 lpfc_update_rcv_time_stamp(vport);
15087 if (list_empty(&seq_dmabuf->dbuf.list)) {
15088 temp_hdr = dmabuf->hbuf.virt;
15089 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
15090 return seq_dmabuf;
15091 }
15092 /* find the correct place in the sequence to insert this frame */
15093 d_buf = list_entry(seq_dmabuf->dbuf.list.prev, typeof(*d_buf), list);
15094 while (!found) {
15095 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15096 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
15097 /*
15098 * If the frame's sequence count is greater than the frame on
15099 * the list then insert the frame right after this frame
15100 */
15101 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
15102 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
15103 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
15104 found = 1;
15105 break;
15106 }
15107
15108 if (&d_buf->list == &seq_dmabuf->dbuf.list)
15109 break;
15110 d_buf = list_entry(d_buf->list.prev, typeof(*d_buf), list);
15111 }
15112
15113 if (found)
15114 return seq_dmabuf;
15115 return NULL;
15116 }
15117
15118 /**
15119 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
15120 * @vport: pointer to a vitural port
15121 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15122 *
15123 * This function tries to abort from the partially assembed sequence, described
15124 * by the information from basic abbort @dmabuf. It checks to see whether such
15125 * partially assembled sequence held by the driver. If so, it shall free up all
15126 * the frames from the partially assembled sequence.
15127 *
15128 * Return
15129 * true -- if there is matching partially assembled sequence present and all
15130 * the frames freed with the sequence;
15131 * false -- if there is no matching partially assembled sequence present so
15132 * nothing got aborted in the lower layer driver
15133 **/
15134 static bool
15135 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
15136 struct hbq_dmabuf *dmabuf)
15137 {
15138 struct fc_frame_header *new_hdr;
15139 struct fc_frame_header *temp_hdr;
15140 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
15141 struct hbq_dmabuf *seq_dmabuf = NULL;
15142
15143 /* Use the hdr_buf to find the sequence that matches this frame */
15144 INIT_LIST_HEAD(&dmabuf->dbuf.list);
15145 INIT_LIST_HEAD(&dmabuf->hbuf.list);
15146 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15147 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
15148 temp_hdr = (struct fc_frame_header *)h_buf->virt;
15149 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
15150 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
15151 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
15152 continue;
15153 /* found a pending sequence that matches this frame */
15154 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
15155 break;
15156 }
15157
15158 /* Free up all the frames from the partially assembled sequence */
15159 if (seq_dmabuf) {
15160 list_for_each_entry_safe(d_buf, n_buf,
15161 &seq_dmabuf->dbuf.list, list) {
15162 list_del_init(&d_buf->list);
15163 lpfc_in_buf_free(vport->phba, d_buf);
15164 }
15165 return true;
15166 }
15167 return false;
15168 }
15169
15170 /**
15171 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
15172 * @vport: pointer to a vitural port
15173 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15174 *
15175 * This function tries to abort from the assembed sequence from upper level
15176 * protocol, described by the information from basic abbort @dmabuf. It
15177 * checks to see whether such pending context exists at upper level protocol.
15178 * If so, it shall clean up the pending context.
15179 *
15180 * Return
15181 * true -- if there is matching pending context of the sequence cleaned
15182 * at ulp;
15183 * false -- if there is no matching pending context of the sequence present
15184 * at ulp.
15185 **/
15186 static bool
15187 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
15188 {
15189 struct lpfc_hba *phba = vport->phba;
15190 int handled;
15191
15192 /* Accepting abort at ulp with SLI4 only */
15193 if (phba->sli_rev < LPFC_SLI_REV4)
15194 return false;
15195
15196 /* Register all caring upper level protocols to attend abort */
15197 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
15198 if (handled)
15199 return true;
15200
15201 return false;
15202 }
15203
15204 /**
15205 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
15206 * @phba: Pointer to HBA context object.
15207 * @cmd_iocbq: pointer to the command iocbq structure.
15208 * @rsp_iocbq: pointer to the response iocbq structure.
15209 *
15210 * This function handles the sequence abort response iocb command complete
15211 * event. It properly releases the memory allocated to the sequence abort
15212 * accept iocb.
15213 **/
15214 static void
15215 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
15216 struct lpfc_iocbq *cmd_iocbq,
15217 struct lpfc_iocbq *rsp_iocbq)
15218 {
15219 struct lpfc_nodelist *ndlp;
15220
15221 if (cmd_iocbq) {
15222 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
15223 lpfc_nlp_put(ndlp);
15224 lpfc_nlp_not_used(ndlp);
15225 lpfc_sli_release_iocbq(phba, cmd_iocbq);
15226 }
15227
15228 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
15229 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
15230 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15231 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
15232 rsp_iocbq->iocb.ulpStatus,
15233 rsp_iocbq->iocb.un.ulpWord[4]);
15234 }
15235
15236 /**
15237 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
15238 * @phba: Pointer to HBA context object.
15239 * @xri: xri id in transaction.
15240 *
15241 * This function validates the xri maps to the known range of XRIs allocated an
15242 * used by the driver.
15243 **/
15244 uint16_t
15245 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
15246 uint16_t xri)
15247 {
15248 uint16_t i;
15249
15250 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
15251 if (xri == phba->sli4_hba.xri_ids[i])
15252 return i;
15253 }
15254 return NO_XRI;
15255 }
15256
15257 /**
15258 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
15259 * @phba: Pointer to HBA context object.
15260 * @fc_hdr: pointer to a FC frame header.
15261 *
15262 * This function sends a basic response to a previous unsol sequence abort
15263 * event after aborting the sequence handling.
15264 **/
15265 static void
15266 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
15267 struct fc_frame_header *fc_hdr, bool aborted)
15268 {
15269 struct lpfc_hba *phba = vport->phba;
15270 struct lpfc_iocbq *ctiocb = NULL;
15271 struct lpfc_nodelist *ndlp;
15272 uint16_t oxid, rxid, xri, lxri;
15273 uint32_t sid, fctl;
15274 IOCB_t *icmd;
15275 int rc;
15276
15277 if (!lpfc_is_link_up(phba))
15278 return;
15279
15280 sid = sli4_sid_from_fc_hdr(fc_hdr);
15281 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
15282 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
15283
15284 ndlp = lpfc_findnode_did(vport, sid);
15285 if (!ndlp) {
15286 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
15287 if (!ndlp) {
15288 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15289 "1268 Failed to allocate ndlp for "
15290 "oxid:x%x SID:x%x\n", oxid, sid);
15291 return;
15292 }
15293 lpfc_nlp_init(vport, ndlp, sid);
15294 /* Put ndlp onto pport node list */
15295 lpfc_enqueue_node(vport, ndlp);
15296 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
15297 /* re-setup ndlp without removing from node list */
15298 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
15299 if (!ndlp) {
15300 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
15301 "3275 Failed to active ndlp found "
15302 "for oxid:x%x SID:x%x\n", oxid, sid);
15303 return;
15304 }
15305 }
15306
15307 /* Allocate buffer for rsp iocb */
15308 ctiocb = lpfc_sli_get_iocbq(phba);
15309 if (!ctiocb)
15310 return;
15311
15312 /* Extract the F_CTL field from FC_HDR */
15313 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
15314
15315 icmd = &ctiocb->iocb;
15316 icmd->un.xseq64.bdl.bdeSize = 0;
15317 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
15318 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
15319 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
15320 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
15321
15322 /* Fill in the rest of iocb fields */
15323 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
15324 icmd->ulpBdeCount = 0;
15325 icmd->ulpLe = 1;
15326 icmd->ulpClass = CLASS3;
15327 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
15328 ctiocb->context1 = lpfc_nlp_get(ndlp);
15329
15330 ctiocb->iocb_cmpl = NULL;
15331 ctiocb->vport = phba->pport;
15332 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
15333 ctiocb->sli4_lxritag = NO_XRI;
15334 ctiocb->sli4_xritag = NO_XRI;
15335
15336 if (fctl & FC_FC_EX_CTX)
15337 /* Exchange responder sent the abort so we
15338 * own the oxid.
15339 */
15340 xri = oxid;
15341 else
15342 xri = rxid;
15343 lxri = lpfc_sli4_xri_inrange(phba, xri);
15344 if (lxri != NO_XRI)
15345 lpfc_set_rrq_active(phba, ndlp, lxri,
15346 (xri == oxid) ? rxid : oxid, 0);
15347 /* For BA_ABTS from exchange responder, if the logical xri with
15348 * the oxid maps to the FCP XRI range, the port no longer has
15349 * that exchange context, send a BLS_RJT. Override the IOCB for
15350 * a BA_RJT.
15351 */
15352 if ((fctl & FC_FC_EX_CTX) &&
15353 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
15354 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15355 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15356 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15357 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15358 }
15359
15360 /* If BA_ABTS failed to abort a partially assembled receive sequence,
15361 * the driver no longer has that exchange, send a BLS_RJT. Override
15362 * the IOCB for a BA_RJT.
15363 */
15364 if (aborted == false) {
15365 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
15366 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
15367 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
15368 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
15369 }
15370
15371 if (fctl & FC_FC_EX_CTX) {
15372 /* ABTS sent by responder to CT exchange, construction
15373 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
15374 * field and RX_ID from ABTS for RX_ID field.
15375 */
15376 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
15377 } else {
15378 /* ABTS sent by initiator to CT exchange, construction
15379 * of BA_ACC will need to allocate a new XRI as for the
15380 * XRI_TAG field.
15381 */
15382 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
15383 }
15384 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
15385 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
15386
15387 /* Xmit CT abts response on exchange <xid> */
15388 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
15389 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
15390 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
15391
15392 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
15393 if (rc == IOCB_ERROR) {
15394 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
15395 "2925 Failed to issue CT ABTS RSP x%x on "
15396 "xri x%x, Data x%x\n",
15397 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
15398 phba->link_state);
15399 lpfc_nlp_put(ndlp);
15400 ctiocb->context1 = NULL;
15401 lpfc_sli_release_iocbq(phba, ctiocb);
15402 }
15403 }
15404
15405 /**
15406 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
15407 * @vport: Pointer to the vport on which this sequence was received
15408 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15409 *
15410 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
15411 * receive sequence is only partially assembed by the driver, it shall abort
15412 * the partially assembled frames for the sequence. Otherwise, if the
15413 * unsolicited receive sequence has been completely assembled and passed to
15414 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
15415 * unsolicited sequence has been aborted. After that, it will issue a basic
15416 * accept to accept the abort.
15417 **/
15418 static void
15419 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
15420 struct hbq_dmabuf *dmabuf)
15421 {
15422 struct lpfc_hba *phba = vport->phba;
15423 struct fc_frame_header fc_hdr;
15424 uint32_t fctl;
15425 bool aborted;
15426
15427 /* Make a copy of fc_hdr before the dmabuf being released */
15428 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
15429 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
15430
15431 if (fctl & FC_FC_EX_CTX) {
15432 /* ABTS by responder to exchange, no cleanup needed */
15433 aborted = true;
15434 } else {
15435 /* ABTS by initiator to exchange, need to do cleanup */
15436 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
15437 if (aborted == false)
15438 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
15439 }
15440 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15441
15442 /* Respond with BA_ACC or BA_RJT accordingly */
15443 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
15444 }
15445
15446 /**
15447 * lpfc_seq_complete - Indicates if a sequence is complete
15448 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15449 *
15450 * This function checks the sequence, starting with the frame described by
15451 * @dmabuf, to see if all the frames associated with this sequence are present.
15452 * the frames associated with this sequence are linked to the @dmabuf using the
15453 * dbuf list. This function looks for two major things. 1) That the first frame
15454 * has a sequence count of zero. 2) There is a frame with last frame of sequence
15455 * set. 3) That there are no holes in the sequence count. The function will
15456 * return 1 when the sequence is complete, otherwise it will return 0.
15457 **/
15458 static int
15459 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
15460 {
15461 struct fc_frame_header *hdr;
15462 struct lpfc_dmabuf *d_buf;
15463 struct hbq_dmabuf *seq_dmabuf;
15464 uint32_t fctl;
15465 int seq_count = 0;
15466
15467 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15468 /* make sure first fame of sequence has a sequence count of zero */
15469 if (hdr->fh_seq_cnt != seq_count)
15470 return 0;
15471 fctl = (hdr->fh_f_ctl[0] << 16 |
15472 hdr->fh_f_ctl[1] << 8 |
15473 hdr->fh_f_ctl[2]);
15474 /* If last frame of sequence we can return success. */
15475 if (fctl & FC_FC_END_SEQ)
15476 return 1;
15477 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
15478 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15479 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15480 /* If there is a hole in the sequence count then fail. */
15481 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
15482 return 0;
15483 fctl = (hdr->fh_f_ctl[0] << 16 |
15484 hdr->fh_f_ctl[1] << 8 |
15485 hdr->fh_f_ctl[2]);
15486 /* If last frame of sequence we can return success. */
15487 if (fctl & FC_FC_END_SEQ)
15488 return 1;
15489 }
15490 return 0;
15491 }
15492
15493 /**
15494 * lpfc_prep_seq - Prep sequence for ULP processing
15495 * @vport: Pointer to the vport on which this sequence was received
15496 * @dmabuf: pointer to a dmabuf that describes the FC sequence
15497 *
15498 * This function takes a sequence, described by a list of frames, and creates
15499 * a list of iocbq structures to describe the sequence. This iocbq list will be
15500 * used to issue to the generic unsolicited sequence handler. This routine
15501 * returns a pointer to the first iocbq in the list. If the function is unable
15502 * to allocate an iocbq then it throw out the received frames that were not
15503 * able to be described and return a pointer to the first iocbq. If unable to
15504 * allocate any iocbqs (including the first) this function will return NULL.
15505 **/
15506 static struct lpfc_iocbq *
15507 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
15508 {
15509 struct hbq_dmabuf *hbq_buf;
15510 struct lpfc_dmabuf *d_buf, *n_buf;
15511 struct lpfc_iocbq *first_iocbq, *iocbq;
15512 struct fc_frame_header *fc_hdr;
15513 uint32_t sid;
15514 uint32_t len, tot_len;
15515 struct ulp_bde64 *pbde;
15516
15517 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15518 /* remove from receive buffer list */
15519 list_del_init(&seq_dmabuf->hbuf.list);
15520 lpfc_update_rcv_time_stamp(vport);
15521 /* get the Remote Port's SID */
15522 sid = sli4_sid_from_fc_hdr(fc_hdr);
15523 tot_len = 0;
15524 /* Get an iocbq struct to fill in. */
15525 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
15526 if (first_iocbq) {
15527 /* Initialize the first IOCB. */
15528 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
15529 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
15530
15531 /* Check FC Header to see what TYPE of frame we are rcv'ing */
15532 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
15533 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
15534 first_iocbq->iocb.un.rcvels.parmRo =
15535 sli4_did_from_fc_hdr(fc_hdr);
15536 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
15537 } else
15538 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
15539 first_iocbq->iocb.ulpContext = NO_XRI;
15540 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
15541 be16_to_cpu(fc_hdr->fh_ox_id);
15542 /* iocbq is prepped for internal consumption. Physical vpi. */
15543 first_iocbq->iocb.unsli3.rcvsli3.vpi =
15544 vport->phba->vpi_ids[vport->vpi];
15545 /* put the first buffer into the first IOCBq */
15546 tot_len = bf_get(lpfc_rcqe_length,
15547 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
15548
15549 first_iocbq->context2 = &seq_dmabuf->dbuf;
15550 first_iocbq->context3 = NULL;
15551 first_iocbq->iocb.ulpBdeCount = 1;
15552 if (tot_len > LPFC_DATA_BUF_SIZE)
15553 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15554 LPFC_DATA_BUF_SIZE;
15555 else
15556 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize = tot_len;
15557
15558 first_iocbq->iocb.un.rcvels.remoteID = sid;
15559
15560 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15561 }
15562 iocbq = first_iocbq;
15563 /*
15564 * Each IOCBq can have two Buffers assigned, so go through the list
15565 * of buffers for this sequence and save two buffers in each IOCBq
15566 */
15567 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
15568 if (!iocbq) {
15569 lpfc_in_buf_free(vport->phba, d_buf);
15570 continue;
15571 }
15572 if (!iocbq->context3) {
15573 iocbq->context3 = d_buf;
15574 iocbq->iocb.ulpBdeCount++;
15575 /* We need to get the size out of the right CQE */
15576 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15577 len = bf_get(lpfc_rcqe_length,
15578 &hbq_buf->cq_event.cqe.rcqe_cmpl);
15579 pbde = (struct ulp_bde64 *)
15580 &iocbq->iocb.unsli3.sli3Words[4];
15581 if (len > LPFC_DATA_BUF_SIZE)
15582 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
15583 else
15584 pbde->tus.f.bdeSize = len;
15585
15586 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
15587 tot_len += len;
15588 } else {
15589 iocbq = lpfc_sli_get_iocbq(vport->phba);
15590 if (!iocbq) {
15591 if (first_iocbq) {
15592 first_iocbq->iocb.ulpStatus =
15593 IOSTAT_FCP_RSP_ERROR;
15594 first_iocbq->iocb.un.ulpWord[4] =
15595 IOERR_NO_RESOURCES;
15596 }
15597 lpfc_in_buf_free(vport->phba, d_buf);
15598 continue;
15599 }
15600 /* We need to get the size out of the right CQE */
15601 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
15602 len = bf_get(lpfc_rcqe_length,
15603 &hbq_buf->cq_event.cqe.rcqe_cmpl);
15604 iocbq->context2 = d_buf;
15605 iocbq->context3 = NULL;
15606 iocbq->iocb.ulpBdeCount = 1;
15607 if (len > LPFC_DATA_BUF_SIZE)
15608 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
15609 LPFC_DATA_BUF_SIZE;
15610 else
15611 iocbq->iocb.un.cont64[0].tus.f.bdeSize = len;
15612
15613 tot_len += len;
15614 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
15615
15616 iocbq->iocb.un.rcvels.remoteID = sid;
15617 list_add_tail(&iocbq->list, &first_iocbq->list);
15618 }
15619 }
15620 return first_iocbq;
15621 }
15622
15623 static void
15624 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
15625 struct hbq_dmabuf *seq_dmabuf)
15626 {
15627 struct fc_frame_header *fc_hdr;
15628 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
15629 struct lpfc_hba *phba = vport->phba;
15630
15631 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
15632 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
15633 if (!iocbq) {
15634 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15635 "2707 Ring %d handler: Failed to allocate "
15636 "iocb Rctl x%x Type x%x received\n",
15637 LPFC_ELS_RING,
15638 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15639 return;
15640 }
15641 if (!lpfc_complete_unsol_iocb(phba,
15642 &phba->sli.ring[LPFC_ELS_RING],
15643 iocbq, fc_hdr->fh_r_ctl,
15644 fc_hdr->fh_type))
15645 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15646 "2540 Ring %d handler: unexpected Rctl "
15647 "x%x Type x%x received\n",
15648 LPFC_ELS_RING,
15649 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
15650
15651 /* Free iocb created in lpfc_prep_seq */
15652 list_for_each_entry_safe(curr_iocb, next_iocb,
15653 &iocbq->list, list) {
15654 list_del_init(&curr_iocb->list);
15655 lpfc_sli_release_iocbq(phba, curr_iocb);
15656 }
15657 lpfc_sli_release_iocbq(phba, iocbq);
15658 }
15659
15660 /**
15661 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
15662 * @phba: Pointer to HBA context object.
15663 *
15664 * This function is called with no lock held. This function processes all
15665 * the received buffers and gives it to upper layers when a received buffer
15666 * indicates that it is the final frame in the sequence. The interrupt
15667 * service routine processes received buffers at interrupt contexts and adds
15668 * received dma buffers to the rb_pend_list queue and signals the worker thread.
15669 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
15670 * appropriate receive function when the final frame in a sequence is received.
15671 **/
15672 void
15673 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
15674 struct hbq_dmabuf *dmabuf)
15675 {
15676 struct hbq_dmabuf *seq_dmabuf;
15677 struct fc_frame_header *fc_hdr;
15678 struct lpfc_vport *vport;
15679 uint32_t fcfi;
15680 uint32_t did;
15681
15682 /* Process each received buffer */
15683 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
15684 /* check to see if this a valid type of frame */
15685 if (lpfc_fc_frame_check(phba, fc_hdr)) {
15686 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15687 return;
15688 }
15689 if ((bf_get(lpfc_cqe_code,
15690 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
15691 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
15692 &dmabuf->cq_event.cqe.rcqe_cmpl);
15693 else
15694 fcfi = bf_get(lpfc_rcqe_fcf_id,
15695 &dmabuf->cq_event.cqe.rcqe_cmpl);
15696
15697 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
15698 if (!vport) {
15699 /* throw out the frame */
15700 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15701 return;
15702 }
15703
15704 /* d_id this frame is directed to */
15705 did = sli4_did_from_fc_hdr(fc_hdr);
15706
15707 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
15708 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
15709 (did != Fabric_DID)) {
15710 /*
15711 * Throw out the frame if we are not pt2pt.
15712 * The pt2pt protocol allows for discovery frames
15713 * to be received without a registered VPI.
15714 */
15715 if (!(vport->fc_flag & FC_PT2PT) ||
15716 (phba->link_state == LPFC_HBA_READY)) {
15717 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15718 return;
15719 }
15720 }
15721
15722 /* Handle the basic abort sequence (BA_ABTS) event */
15723 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
15724 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
15725 return;
15726 }
15727
15728 /* Link this frame */
15729 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
15730 if (!seq_dmabuf) {
15731 /* unable to add frame to vport - throw it out */
15732 lpfc_in_buf_free(phba, &dmabuf->dbuf);
15733 return;
15734 }
15735 /* If not last frame in sequence continue processing frames. */
15736 if (!lpfc_seq_complete(seq_dmabuf))
15737 return;
15738
15739 /* Send the complete sequence to the upper layer protocol */
15740 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
15741 }
15742
15743 /**
15744 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
15745 * @phba: pointer to lpfc hba data structure.
15746 *
15747 * This routine is invoked to post rpi header templates to the
15748 * HBA consistent with the SLI-4 interface spec. This routine
15749 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15750 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15751 *
15752 * This routine does not require any locks. It's usage is expected
15753 * to be driver load or reset recovery when the driver is
15754 * sequential.
15755 *
15756 * Return codes
15757 * 0 - successful
15758 * -EIO - The mailbox failed to complete successfully.
15759 * When this error occurs, the driver is not guaranteed
15760 * to have any rpi regions posted to the device and
15761 * must either attempt to repost the regions or take a
15762 * fatal error.
15763 **/
15764 int
15765 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
15766 {
15767 struct lpfc_rpi_hdr *rpi_page;
15768 uint32_t rc = 0;
15769 uint16_t lrpi = 0;
15770
15771 /* SLI4 ports that support extents do not require RPI headers. */
15772 if (!phba->sli4_hba.rpi_hdrs_in_use)
15773 goto exit;
15774 if (phba->sli4_hba.extents_in_use)
15775 return -EIO;
15776
15777 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
15778 /*
15779 * Assign the rpi headers a physical rpi only if the driver
15780 * has not initialized those resources. A port reset only
15781 * needs the headers posted.
15782 */
15783 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
15784 LPFC_RPI_RSRC_RDY)
15785 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15786
15787 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
15788 if (rc != MBX_SUCCESS) {
15789 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15790 "2008 Error %d posting all rpi "
15791 "headers\n", rc);
15792 rc = -EIO;
15793 break;
15794 }
15795 }
15796
15797 exit:
15798 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
15799 LPFC_RPI_RSRC_RDY);
15800 return rc;
15801 }
15802
15803 /**
15804 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
15805 * @phba: pointer to lpfc hba data structure.
15806 * @rpi_page: pointer to the rpi memory region.
15807 *
15808 * This routine is invoked to post a single rpi header to the
15809 * HBA consistent with the SLI-4 interface spec. This memory region
15810 * maps up to 64 rpi context regions.
15811 *
15812 * Return codes
15813 * 0 - successful
15814 * -ENOMEM - No available memory
15815 * -EIO - The mailbox failed to complete successfully.
15816 **/
15817 int
15818 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
15819 {
15820 LPFC_MBOXQ_t *mboxq;
15821 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
15822 uint32_t rc = 0;
15823 uint32_t shdr_status, shdr_add_status;
15824 union lpfc_sli4_cfg_shdr *shdr;
15825
15826 /* SLI4 ports that support extents do not require RPI headers. */
15827 if (!phba->sli4_hba.rpi_hdrs_in_use)
15828 return rc;
15829 if (phba->sli4_hba.extents_in_use)
15830 return -EIO;
15831
15832 /* The port is notified of the header region via a mailbox command. */
15833 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15834 if (!mboxq) {
15835 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15836 "2001 Unable to allocate memory for issuing "
15837 "SLI_CONFIG_SPECIAL mailbox command\n");
15838 return -ENOMEM;
15839 }
15840
15841 /* Post all rpi memory regions to the port. */
15842 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
15843 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15844 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
15845 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
15846 sizeof(struct lpfc_sli4_cfg_mhdr),
15847 LPFC_SLI4_MBX_EMBED);
15848
15849
15850 /* Post the physical rpi to the port for this rpi header. */
15851 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
15852 rpi_page->start_rpi);
15853 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
15854 hdr_tmpl, rpi_page->page_count);
15855
15856 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
15857 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
15858 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15859 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
15860 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15861 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15862 if (rc != MBX_TIMEOUT)
15863 mempool_free(mboxq, phba->mbox_mem_pool);
15864 if (shdr_status || shdr_add_status || rc) {
15865 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15866 "2514 POST_RPI_HDR mailbox failed with "
15867 "status x%x add_status x%x, mbx status x%x\n",
15868 shdr_status, shdr_add_status, rc);
15869 rc = -ENXIO;
15870 }
15871 return rc;
15872 }
15873
15874 /**
15875 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
15876 * @phba: pointer to lpfc hba data structure.
15877 *
15878 * This routine is invoked to post rpi header templates to the
15879 * HBA consistent with the SLI-4 interface spec. This routine
15880 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
15881 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
15882 *
15883 * Returns
15884 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
15885 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
15886 **/
15887 int
15888 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
15889 {
15890 unsigned long rpi;
15891 uint16_t max_rpi, rpi_limit;
15892 uint16_t rpi_remaining, lrpi = 0;
15893 struct lpfc_rpi_hdr *rpi_hdr;
15894 unsigned long iflag;
15895
15896 /*
15897 * Fetch the next logical rpi. Because this index is logical,
15898 * the driver starts at 0 each time.
15899 */
15900 spin_lock_irqsave(&phba->hbalock, iflag);
15901 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
15902 rpi_limit = phba->sli4_hba.next_rpi;
15903
15904 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15905 if (rpi >= rpi_limit)
15906 rpi = LPFC_RPI_ALLOC_ERROR;
15907 else {
15908 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15909 phba->sli4_hba.max_cfg_param.rpi_used++;
15910 phba->sli4_hba.rpi_count++;
15911 }
15912 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
15913 "0001 rpi:%x max:%x lim:%x\n",
15914 (int) rpi, max_rpi, rpi_limit);
15915
15916 /*
15917 * Don't try to allocate more rpi header regions if the device limit
15918 * has been exhausted.
15919 */
15920 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15921 (phba->sli4_hba.rpi_count >= max_rpi)) {
15922 spin_unlock_irqrestore(&phba->hbalock, iflag);
15923 return rpi;
15924 }
15925
15926 /*
15927 * RPI header postings are not required for SLI4 ports capable of
15928 * extents.
15929 */
15930 if (!phba->sli4_hba.rpi_hdrs_in_use) {
15931 spin_unlock_irqrestore(&phba->hbalock, iflag);
15932 return rpi;
15933 }
15934
15935 /*
15936 * If the driver is running low on rpi resources, allocate another
15937 * page now. Note that the next_rpi value is used because
15938 * it represents how many are actually in use whereas max_rpi notes
15939 * how many are supported max by the device.
15940 */
15941 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
15942 spin_unlock_irqrestore(&phba->hbalock, iflag);
15943 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15944 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15945 if (!rpi_hdr) {
15946 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15947 "2002 Error Could not grow rpi "
15948 "count\n");
15949 } else {
15950 lrpi = rpi_hdr->start_rpi;
15951 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15952 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15953 }
15954 }
15955
15956 return rpi;
15957 }
15958
15959 /**
15960 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15961 * @phba: pointer to lpfc hba data structure.
15962 *
15963 * This routine is invoked to release an rpi to the pool of
15964 * available rpis maintained by the driver.
15965 **/
15966 static void
15967 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15968 {
15969 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15970 phba->sli4_hba.rpi_count--;
15971 phba->sli4_hba.max_cfg_param.rpi_used--;
15972 }
15973 }
15974
15975 /**
15976 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15977 * @phba: pointer to lpfc hba data structure.
15978 *
15979 * This routine is invoked to release an rpi to the pool of
15980 * available rpis maintained by the driver.
15981 **/
15982 void
15983 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15984 {
15985 spin_lock_irq(&phba->hbalock);
15986 __lpfc_sli4_free_rpi(phba, rpi);
15987 spin_unlock_irq(&phba->hbalock);
15988 }
15989
15990 /**
15991 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15992 * @phba: pointer to lpfc hba data structure.
15993 *
15994 * This routine is invoked to remove the memory region that
15995 * provided rpi via a bitmask.
15996 **/
15997 void
15998 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15999 {
16000 kfree(phba->sli4_hba.rpi_bmask);
16001 kfree(phba->sli4_hba.rpi_ids);
16002 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
16003 }
16004
16005 /**
16006 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
16007 * @phba: pointer to lpfc hba data structure.
16008 *
16009 * This routine is invoked to remove the memory region that
16010 * provided rpi via a bitmask.
16011 **/
16012 int
16013 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
16014 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
16015 {
16016 LPFC_MBOXQ_t *mboxq;
16017 struct lpfc_hba *phba = ndlp->phba;
16018 int rc;
16019
16020 /* The port is notified of the header region via a mailbox command. */
16021 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16022 if (!mboxq)
16023 return -ENOMEM;
16024
16025 /* Post all rpi memory regions to the port. */
16026 lpfc_resume_rpi(mboxq, ndlp);
16027 if (cmpl) {
16028 mboxq->mbox_cmpl = cmpl;
16029 mboxq->context1 = arg;
16030 mboxq->context2 = ndlp;
16031 } else
16032 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16033 mboxq->vport = ndlp->vport;
16034 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16035 if (rc == MBX_NOT_FINISHED) {
16036 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16037 "2010 Resume RPI Mailbox failed "
16038 "status %d, mbxStatus x%x\n", rc,
16039 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16040 mempool_free(mboxq, phba->mbox_mem_pool);
16041 return -EIO;
16042 }
16043 return 0;
16044 }
16045
16046 /**
16047 * lpfc_sli4_init_vpi - Initialize a vpi with the port
16048 * @vport: Pointer to the vport for which the vpi is being initialized
16049 *
16050 * This routine is invoked to activate a vpi with the port.
16051 *
16052 * Returns:
16053 * 0 success
16054 * -Evalue otherwise
16055 **/
16056 int
16057 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
16058 {
16059 LPFC_MBOXQ_t *mboxq;
16060 int rc = 0;
16061 int retval = MBX_SUCCESS;
16062 uint32_t mbox_tmo;
16063 struct lpfc_hba *phba = vport->phba;
16064 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16065 if (!mboxq)
16066 return -ENOMEM;
16067 lpfc_init_vpi(phba, mboxq, vport->vpi);
16068 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
16069 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
16070 if (rc != MBX_SUCCESS) {
16071 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
16072 "2022 INIT VPI Mailbox failed "
16073 "status %d, mbxStatus x%x\n", rc,
16074 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
16075 retval = -EIO;
16076 }
16077 if (rc != MBX_TIMEOUT)
16078 mempool_free(mboxq, vport->phba->mbox_mem_pool);
16079
16080 return retval;
16081 }
16082
16083 /**
16084 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
16085 * @phba: pointer to lpfc hba data structure.
16086 * @mboxq: Pointer to mailbox object.
16087 *
16088 * This routine is invoked to manually add a single FCF record. The caller
16089 * must pass a completely initialized FCF_Record. This routine takes
16090 * care of the nonembedded mailbox operations.
16091 **/
16092 static void
16093 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
16094 {
16095 void *virt_addr;
16096 union lpfc_sli4_cfg_shdr *shdr;
16097 uint32_t shdr_status, shdr_add_status;
16098
16099 virt_addr = mboxq->sge_array->addr[0];
16100 /* The IOCTL status is embedded in the mailbox subheader. */
16101 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
16102 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16103 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16104
16105 if ((shdr_status || shdr_add_status) &&
16106 (shdr_status != STATUS_FCF_IN_USE))
16107 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16108 "2558 ADD_FCF_RECORD mailbox failed with "
16109 "status x%x add_status x%x\n",
16110 shdr_status, shdr_add_status);
16111
16112 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16113 }
16114
16115 /**
16116 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
16117 * @phba: pointer to lpfc hba data structure.
16118 * @fcf_record: pointer to the initialized fcf record to add.
16119 *
16120 * This routine is invoked to manually add a single FCF record. The caller
16121 * must pass a completely initialized FCF_Record. This routine takes
16122 * care of the nonembedded mailbox operations.
16123 **/
16124 int
16125 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
16126 {
16127 int rc = 0;
16128 LPFC_MBOXQ_t *mboxq;
16129 uint8_t *bytep;
16130 void *virt_addr;
16131 struct lpfc_mbx_sge sge;
16132 uint32_t alloc_len, req_len;
16133 uint32_t fcfindex;
16134
16135 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16136 if (!mboxq) {
16137 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16138 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
16139 return -ENOMEM;
16140 }
16141
16142 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
16143 sizeof(uint32_t);
16144
16145 /* Allocate DMA memory and set up the non-embedded mailbox command */
16146 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
16147 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
16148 req_len, LPFC_SLI4_MBX_NEMBED);
16149 if (alloc_len < req_len) {
16150 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16151 "2523 Allocated DMA memory size (x%x) is "
16152 "less than the requested DMA memory "
16153 "size (x%x)\n", alloc_len, req_len);
16154 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16155 return -ENOMEM;
16156 }
16157
16158 /*
16159 * Get the first SGE entry from the non-embedded DMA memory. This
16160 * routine only uses a single SGE.
16161 */
16162 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
16163 virt_addr = mboxq->sge_array->addr[0];
16164 /*
16165 * Configure the FCF record for FCFI 0. This is the driver's
16166 * hardcoded default and gets used in nonFIP mode.
16167 */
16168 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
16169 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
16170 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
16171
16172 /*
16173 * Copy the fcf_index and the FCF Record Data. The data starts after
16174 * the FCoE header plus word10. The data copy needs to be endian
16175 * correct.
16176 */
16177 bytep += sizeof(uint32_t);
16178 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
16179 mboxq->vport = phba->pport;
16180 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
16181 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16182 if (rc == MBX_NOT_FINISHED) {
16183 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16184 "2515 ADD_FCF_RECORD mailbox failed with "
16185 "status 0x%x\n", rc);
16186 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16187 rc = -EIO;
16188 } else
16189 rc = 0;
16190
16191 return rc;
16192 }
16193
16194 /**
16195 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
16196 * @phba: pointer to lpfc hba data structure.
16197 * @fcf_record: pointer to the fcf record to write the default data.
16198 * @fcf_index: FCF table entry index.
16199 *
16200 * This routine is invoked to build the driver's default FCF record. The
16201 * values used are hardcoded. This routine handles memory initialization.
16202 *
16203 **/
16204 void
16205 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
16206 struct fcf_record *fcf_record,
16207 uint16_t fcf_index)
16208 {
16209 memset(fcf_record, 0, sizeof(struct fcf_record));
16210 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
16211 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
16212 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
16213 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
16214 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
16215 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
16216 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
16217 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
16218 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
16219 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
16220 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
16221 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
16222 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
16223 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
16224 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
16225 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
16226 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
16227 /* Set the VLAN bit map */
16228 if (phba->valid_vlan) {
16229 fcf_record->vlan_bitmap[phba->vlan_id / 8]
16230 = 1 << (phba->vlan_id % 8);
16231 }
16232 }
16233
16234 /**
16235 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
16236 * @phba: pointer to lpfc hba data structure.
16237 * @fcf_index: FCF table entry offset.
16238 *
16239 * This routine is invoked to scan the entire FCF table by reading FCF
16240 * record and processing it one at a time starting from the @fcf_index
16241 * for initial FCF discovery or fast FCF failover rediscovery.
16242 *
16243 * Return 0 if the mailbox command is submitted successfully, none 0
16244 * otherwise.
16245 **/
16246 int
16247 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16248 {
16249 int rc = 0, error;
16250 LPFC_MBOXQ_t *mboxq;
16251
16252 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
16253 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
16254 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16255 if (!mboxq) {
16256 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16257 "2000 Failed to allocate mbox for "
16258 "READ_FCF cmd\n");
16259 error = -ENOMEM;
16260 goto fail_fcf_scan;
16261 }
16262 /* Construct the read FCF record mailbox command */
16263 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16264 if (rc) {
16265 error = -EINVAL;
16266 goto fail_fcf_scan;
16267 }
16268 /* Issue the mailbox command asynchronously */
16269 mboxq->vport = phba->pport;
16270 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
16271
16272 spin_lock_irq(&phba->hbalock);
16273 phba->hba_flag |= FCF_TS_INPROG;
16274 spin_unlock_irq(&phba->hbalock);
16275
16276 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16277 if (rc == MBX_NOT_FINISHED)
16278 error = -EIO;
16279 else {
16280 /* Reset eligible FCF count for new scan */
16281 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
16282 phba->fcf.eligible_fcf_cnt = 0;
16283 error = 0;
16284 }
16285 fail_fcf_scan:
16286 if (error) {
16287 if (mboxq)
16288 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16289 /* FCF scan failed, clear FCF_TS_INPROG flag */
16290 spin_lock_irq(&phba->hbalock);
16291 phba->hba_flag &= ~FCF_TS_INPROG;
16292 spin_unlock_irq(&phba->hbalock);
16293 }
16294 return error;
16295 }
16296
16297 /**
16298 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
16299 * @phba: pointer to lpfc hba data structure.
16300 * @fcf_index: FCF table entry offset.
16301 *
16302 * This routine is invoked to read an FCF record indicated by @fcf_index
16303 * and to use it for FLOGI roundrobin FCF failover.
16304 *
16305 * Return 0 if the mailbox command is submitted successfully, none 0
16306 * otherwise.
16307 **/
16308 int
16309 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16310 {
16311 int rc = 0, error;
16312 LPFC_MBOXQ_t *mboxq;
16313
16314 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16315 if (!mboxq) {
16316 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16317 "2763 Failed to allocate mbox for "
16318 "READ_FCF cmd\n");
16319 error = -ENOMEM;
16320 goto fail_fcf_read;
16321 }
16322 /* Construct the read FCF record mailbox command */
16323 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16324 if (rc) {
16325 error = -EINVAL;
16326 goto fail_fcf_read;
16327 }
16328 /* Issue the mailbox command asynchronously */
16329 mboxq->vport = phba->pport;
16330 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
16331 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16332 if (rc == MBX_NOT_FINISHED)
16333 error = -EIO;
16334 else
16335 error = 0;
16336
16337 fail_fcf_read:
16338 if (error && mboxq)
16339 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16340 return error;
16341 }
16342
16343 /**
16344 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
16345 * @phba: pointer to lpfc hba data structure.
16346 * @fcf_index: FCF table entry offset.
16347 *
16348 * This routine is invoked to read an FCF record indicated by @fcf_index to
16349 * determine whether it's eligible for FLOGI roundrobin failover list.
16350 *
16351 * Return 0 if the mailbox command is submitted successfully, none 0
16352 * otherwise.
16353 **/
16354 int
16355 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
16356 {
16357 int rc = 0, error;
16358 LPFC_MBOXQ_t *mboxq;
16359
16360 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16361 if (!mboxq) {
16362 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
16363 "2758 Failed to allocate mbox for "
16364 "READ_FCF cmd\n");
16365 error = -ENOMEM;
16366 goto fail_fcf_read;
16367 }
16368 /* Construct the read FCF record mailbox command */
16369 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
16370 if (rc) {
16371 error = -EINVAL;
16372 goto fail_fcf_read;
16373 }
16374 /* Issue the mailbox command asynchronously */
16375 mboxq->vport = phba->pport;
16376 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
16377 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
16378 if (rc == MBX_NOT_FINISHED)
16379 error = -EIO;
16380 else
16381 error = 0;
16382
16383 fail_fcf_read:
16384 if (error && mboxq)
16385 lpfc_sli4_mbox_cmd_free(phba, mboxq);
16386 return error;
16387 }
16388
16389 /**
16390 * lpfc_check_next_fcf_pri_level
16391 * phba pointer to the lpfc_hba struct for this port.
16392 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
16393 * routine when the rr_bmask is empty. The FCF indecies are put into the
16394 * rr_bmask based on their priority level. Starting from the highest priority
16395 * to the lowest. The most likely FCF candidate will be in the highest
16396 * priority group. When this routine is called it searches the fcf_pri list for
16397 * next lowest priority group and repopulates the rr_bmask with only those
16398 * fcf_indexes.
16399 * returns:
16400 * 1=success 0=failure
16401 **/
16402 static int
16403 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
16404 {
16405 uint16_t next_fcf_pri;
16406 uint16_t last_index;
16407 struct lpfc_fcf_pri *fcf_pri;
16408 int rc;
16409 int ret = 0;
16410
16411 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
16412 LPFC_SLI4_FCF_TBL_INDX_MAX);
16413 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16414 "3060 Last IDX %d\n", last_index);
16415
16416 /* Verify the priority list has 2 or more entries */
16417 spin_lock_irq(&phba->hbalock);
16418 if (list_empty(&phba->fcf.fcf_pri_list) ||
16419 list_is_singular(&phba->fcf.fcf_pri_list)) {
16420 spin_unlock_irq(&phba->hbalock);
16421 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16422 "3061 Last IDX %d\n", last_index);
16423 return 0; /* Empty rr list */
16424 }
16425 spin_unlock_irq(&phba->hbalock);
16426
16427 next_fcf_pri = 0;
16428 /*
16429 * Clear the rr_bmask and set all of the bits that are at this
16430 * priority.
16431 */
16432 memset(phba->fcf.fcf_rr_bmask, 0,
16433 sizeof(*phba->fcf.fcf_rr_bmask));
16434 spin_lock_irq(&phba->hbalock);
16435 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16436 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
16437 continue;
16438 /*
16439 * the 1st priority that has not FLOGI failed
16440 * will be the highest.
16441 */
16442 if (!next_fcf_pri)
16443 next_fcf_pri = fcf_pri->fcf_rec.priority;
16444 spin_unlock_irq(&phba->hbalock);
16445 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16446 rc = lpfc_sli4_fcf_rr_index_set(phba,
16447 fcf_pri->fcf_rec.fcf_index);
16448 if (rc)
16449 return 0;
16450 }
16451 spin_lock_irq(&phba->hbalock);
16452 }
16453 /*
16454 * if next_fcf_pri was not set above and the list is not empty then
16455 * we have failed flogis on all of them. So reset flogi failed
16456 * and start at the beginning.
16457 */
16458 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
16459 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
16460 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
16461 /*
16462 * the 1st priority that has not FLOGI failed
16463 * will be the highest.
16464 */
16465 if (!next_fcf_pri)
16466 next_fcf_pri = fcf_pri->fcf_rec.priority;
16467 spin_unlock_irq(&phba->hbalock);
16468 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
16469 rc = lpfc_sli4_fcf_rr_index_set(phba,
16470 fcf_pri->fcf_rec.fcf_index);
16471 if (rc)
16472 return 0;
16473 }
16474 spin_lock_irq(&phba->hbalock);
16475 }
16476 } else
16477 ret = 1;
16478 spin_unlock_irq(&phba->hbalock);
16479
16480 return ret;
16481 }
16482 /**
16483 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
16484 * @phba: pointer to lpfc hba data structure.
16485 *
16486 * This routine is to get the next eligible FCF record index in a round
16487 * robin fashion. If the next eligible FCF record index equals to the
16488 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
16489 * shall be returned, otherwise, the next eligible FCF record's index
16490 * shall be returned.
16491 **/
16492 uint16_t
16493 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
16494 {
16495 uint16_t next_fcf_index;
16496
16497 initial_priority:
16498 /* Search start from next bit of currently registered FCF index */
16499 next_fcf_index = phba->fcf.current_rec.fcf_indx;
16500
16501 next_priority:
16502 /* Determine the next fcf index to check */
16503 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
16504 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16505 LPFC_SLI4_FCF_TBL_INDX_MAX,
16506 next_fcf_index);
16507
16508 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
16509 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16510 /*
16511 * If we have wrapped then we need to clear the bits that
16512 * have been tested so that we can detect when we should
16513 * change the priority level.
16514 */
16515 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
16516 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
16517 }
16518
16519
16520 /* Check roundrobin failover list empty condition */
16521 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
16522 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
16523 /*
16524 * If next fcf index is not found check if there are lower
16525 * Priority level fcf's in the fcf_priority list.
16526 * Set up the rr_bmask with all of the avaiable fcf bits
16527 * at that level and continue the selection process.
16528 */
16529 if (lpfc_check_next_fcf_pri_level(phba))
16530 goto initial_priority;
16531 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16532 "2844 No roundrobin failover FCF available\n");
16533 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
16534 return LPFC_FCOE_FCF_NEXT_NONE;
16535 else {
16536 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
16537 "3063 Only FCF available idx %d, flag %x\n",
16538 next_fcf_index,
16539 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
16540 return next_fcf_index;
16541 }
16542 }
16543
16544 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
16545 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
16546 LPFC_FCF_FLOGI_FAILED) {
16547 if (list_is_singular(&phba->fcf.fcf_pri_list))
16548 return LPFC_FCOE_FCF_NEXT_NONE;
16549
16550 goto next_priority;
16551 }
16552
16553 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16554 "2845 Get next roundrobin failover FCF (x%x)\n",
16555 next_fcf_index);
16556
16557 return next_fcf_index;
16558 }
16559
16560 /**
16561 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
16562 * @phba: pointer to lpfc hba data structure.
16563 *
16564 * This routine sets the FCF record index in to the eligible bmask for
16565 * roundrobin failover search. It checks to make sure that the index
16566 * does not go beyond the range of the driver allocated bmask dimension
16567 * before setting the bit.
16568 *
16569 * Returns 0 if the index bit successfully set, otherwise, it returns
16570 * -EINVAL.
16571 **/
16572 int
16573 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
16574 {
16575 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16576 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16577 "2610 FCF (x%x) reached driver's book "
16578 "keeping dimension:x%x\n",
16579 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16580 return -EINVAL;
16581 }
16582 /* Set the eligible FCF record index bmask */
16583 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16584
16585 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16586 "2790 Set FCF (x%x) to roundrobin FCF failover "
16587 "bmask\n", fcf_index);
16588
16589 return 0;
16590 }
16591
16592 /**
16593 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
16594 * @phba: pointer to lpfc hba data structure.
16595 *
16596 * This routine clears the FCF record index from the eligible bmask for
16597 * roundrobin failover search. It checks to make sure that the index
16598 * does not go beyond the range of the driver allocated bmask dimension
16599 * before clearing the bit.
16600 **/
16601 void
16602 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
16603 {
16604 struct lpfc_fcf_pri *fcf_pri, *fcf_pri_next;
16605 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
16606 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16607 "2762 FCF (x%x) reached driver's book "
16608 "keeping dimension:x%x\n",
16609 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
16610 return;
16611 }
16612 /* Clear the eligible FCF record index bmask */
16613 spin_lock_irq(&phba->hbalock);
16614 list_for_each_entry_safe(fcf_pri, fcf_pri_next, &phba->fcf.fcf_pri_list,
16615 list) {
16616 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
16617 list_del_init(&fcf_pri->list);
16618 break;
16619 }
16620 }
16621 spin_unlock_irq(&phba->hbalock);
16622 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
16623
16624 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16625 "2791 Clear FCF (x%x) from roundrobin failover "
16626 "bmask\n", fcf_index);
16627 }
16628
16629 /**
16630 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
16631 * @phba: pointer to lpfc hba data structure.
16632 *
16633 * This routine is the completion routine for the rediscover FCF table mailbox
16634 * command. If the mailbox command returned failure, it will try to stop the
16635 * FCF rediscover wait timer.
16636 **/
16637 static void
16638 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
16639 {
16640 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16641 uint32_t shdr_status, shdr_add_status;
16642
16643 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16644
16645 shdr_status = bf_get(lpfc_mbox_hdr_status,
16646 &redisc_fcf->header.cfg_shdr.response);
16647 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
16648 &redisc_fcf->header.cfg_shdr.response);
16649 if (shdr_status || shdr_add_status) {
16650 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
16651 "2746 Requesting for FCF rediscovery failed "
16652 "status x%x add_status x%x\n",
16653 shdr_status, shdr_add_status);
16654 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
16655 spin_lock_irq(&phba->hbalock);
16656 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
16657 spin_unlock_irq(&phba->hbalock);
16658 /*
16659 * CVL event triggered FCF rediscover request failed,
16660 * last resort to re-try current registered FCF entry.
16661 */
16662 lpfc_retry_pport_discovery(phba);
16663 } else {
16664 spin_lock_irq(&phba->hbalock);
16665 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
16666 spin_unlock_irq(&phba->hbalock);
16667 /*
16668 * DEAD FCF event triggered FCF rediscover request
16669 * failed, last resort to fail over as a link down
16670 * to FCF registration.
16671 */
16672 lpfc_sli4_fcf_dead_failthrough(phba);
16673 }
16674 } else {
16675 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
16676 "2775 Start FCF rediscover quiescent timer\n");
16677 /*
16678 * Start FCF rediscovery wait timer for pending FCF
16679 * before rescan FCF record table.
16680 */
16681 lpfc_fcf_redisc_wait_start_timer(phba);
16682 }
16683
16684 mempool_free(mbox, phba->mbox_mem_pool);
16685 }
16686
16687 /**
16688 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
16689 * @phba: pointer to lpfc hba data structure.
16690 *
16691 * This routine is invoked to request for rediscovery of the entire FCF table
16692 * by the port.
16693 **/
16694 int
16695 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
16696 {
16697 LPFC_MBOXQ_t *mbox;
16698 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
16699 int rc, length;
16700
16701 /* Cancel retry delay timers to all vports before FCF rediscover */
16702 lpfc_cancel_all_vport_retry_delay_timer(phba);
16703
16704 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16705 if (!mbox) {
16706 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16707 "2745 Failed to allocate mbox for "
16708 "requesting FCF rediscover.\n");
16709 return -ENOMEM;
16710 }
16711
16712 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
16713 sizeof(struct lpfc_sli4_cfg_mhdr));
16714 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
16715 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
16716 length, LPFC_SLI4_MBX_EMBED);
16717
16718 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
16719 /* Set count to 0 for invalidating the entire FCF database */
16720 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
16721
16722 /* Issue the mailbox command asynchronously */
16723 mbox->vport = phba->pport;
16724 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
16725 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
16726
16727 if (rc == MBX_NOT_FINISHED) {
16728 mempool_free(mbox, phba->mbox_mem_pool);
16729 return -EIO;
16730 }
16731 return 0;
16732 }
16733
16734 /**
16735 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
16736 * @phba: pointer to lpfc hba data structure.
16737 *
16738 * This function is the failover routine as a last resort to the FCF DEAD
16739 * event when driver failed to perform fast FCF failover.
16740 **/
16741 void
16742 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
16743 {
16744 uint32_t link_state;
16745
16746 /*
16747 * Last resort as FCF DEAD event failover will treat this as
16748 * a link down, but save the link state because we don't want
16749 * it to be changed to Link Down unless it is already down.
16750 */
16751 link_state = phba->link_state;
16752 lpfc_linkdown(phba);
16753 phba->link_state = link_state;
16754
16755 /* Unregister FCF if no devices connected to it */
16756 lpfc_unregister_unused_fcf(phba);
16757 }
16758
16759 /**
16760 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
16761 * @phba: pointer to lpfc hba data structure.
16762 * @rgn23_data: pointer to configure region 23 data.
16763 *
16764 * This function gets SLI3 port configure region 23 data through memory dump
16765 * mailbox command. When it successfully retrieves data, the size of the data
16766 * will be returned, otherwise, 0 will be returned.
16767 **/
16768 static uint32_t
16769 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16770 {
16771 LPFC_MBOXQ_t *pmb = NULL;
16772 MAILBOX_t *mb;
16773 uint32_t offset = 0;
16774 int rc;
16775
16776 if (!rgn23_data)
16777 return 0;
16778
16779 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16780 if (!pmb) {
16781 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16782 "2600 failed to allocate mailbox memory\n");
16783 return 0;
16784 }
16785 mb = &pmb->u.mb;
16786
16787 do {
16788 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
16789 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
16790
16791 if (rc != MBX_SUCCESS) {
16792 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
16793 "2601 failed to read config "
16794 "region 23, rc 0x%x Status 0x%x\n",
16795 rc, mb->mbxStatus);
16796 mb->un.varDmp.word_cnt = 0;
16797 }
16798 /*
16799 * dump mem may return a zero when finished or we got a
16800 * mailbox error, either way we are done.
16801 */
16802 if (mb->un.varDmp.word_cnt == 0)
16803 break;
16804 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
16805 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
16806
16807 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
16808 rgn23_data + offset,
16809 mb->un.varDmp.word_cnt);
16810 offset += mb->un.varDmp.word_cnt;
16811 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
16812
16813 mempool_free(pmb, phba->mbox_mem_pool);
16814 return offset;
16815 }
16816
16817 /**
16818 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
16819 * @phba: pointer to lpfc hba data structure.
16820 * @rgn23_data: pointer to configure region 23 data.
16821 *
16822 * This function gets SLI4 port configure region 23 data through memory dump
16823 * mailbox command. When it successfully retrieves data, the size of the data
16824 * will be returned, otherwise, 0 will be returned.
16825 **/
16826 static uint32_t
16827 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
16828 {
16829 LPFC_MBOXQ_t *mboxq = NULL;
16830 struct lpfc_dmabuf *mp = NULL;
16831 struct lpfc_mqe *mqe;
16832 uint32_t data_length = 0;
16833 int rc;
16834
16835 if (!rgn23_data)
16836 return 0;
16837
16838 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16839 if (!mboxq) {
16840 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16841 "3105 failed to allocate mailbox memory\n");
16842 return 0;
16843 }
16844
16845 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
16846 goto out;
16847 mqe = &mboxq->u.mqe;
16848 mp = (struct lpfc_dmabuf *) mboxq->context1;
16849 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
16850 if (rc)
16851 goto out;
16852 data_length = mqe->un.mb_words[5];
16853 if (data_length == 0)
16854 goto out;
16855 if (data_length > DMP_RGN23_SIZE) {
16856 data_length = 0;
16857 goto out;
16858 }
16859 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
16860 out:
16861 mempool_free(mboxq, phba->mbox_mem_pool);
16862 if (mp) {
16863 lpfc_mbuf_free(phba, mp->virt, mp->phys);
16864 kfree(mp);
16865 }
16866 return data_length;
16867 }
16868
16869 /**
16870 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
16871 * @phba: pointer to lpfc hba data structure.
16872 *
16873 * This function read region 23 and parse TLV for port status to
16874 * decide if the user disaled the port. If the TLV indicates the
16875 * port is disabled, the hba_flag is set accordingly.
16876 **/
16877 void
16878 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
16879 {
16880 uint8_t *rgn23_data = NULL;
16881 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
16882 uint32_t offset = 0;
16883
16884 /* Get adapter Region 23 data */
16885 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
16886 if (!rgn23_data)
16887 goto out;
16888
16889 if (phba->sli_rev < LPFC_SLI_REV4)
16890 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
16891 else {
16892 if_type = bf_get(lpfc_sli_intf_if_type,
16893 &phba->sli4_hba.sli_intf);
16894 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
16895 goto out;
16896 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
16897 }
16898
16899 if (!data_size)
16900 goto out;
16901
16902 /* Check the region signature first */
16903 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
16904 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16905 "2619 Config region 23 has bad signature\n");
16906 goto out;
16907 }
16908 offset += 4;
16909
16910 /* Check the data structure version */
16911 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16912 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16913 "2620 Config region 23 has bad version\n");
16914 goto out;
16915 }
16916 offset += 4;
16917
16918 /* Parse TLV entries in the region */
16919 while (offset < data_size) {
16920 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16921 break;
16922 /*
16923 * If the TLV is not driver specific TLV or driver id is
16924 * not linux driver id, skip the record.
16925 */
16926 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16927 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16928 (rgn23_data[offset + 3] != 0)) {
16929 offset += rgn23_data[offset + 1] * 4 + 4;
16930 continue;
16931 }
16932
16933 /* Driver found a driver specific TLV in the config region */
16934 sub_tlv_len = rgn23_data[offset + 1] * 4;
16935 offset += 4;
16936 tlv_offset = 0;
16937
16938 /*
16939 * Search for configured port state sub-TLV.
16940 */
16941 while ((offset < data_size) &&
16942 (tlv_offset < sub_tlv_len)) {
16943 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16944 offset += 4;
16945 tlv_offset += 4;
16946 break;
16947 }
16948 if (rgn23_data[offset] != PORT_STE_TYPE) {
16949 offset += rgn23_data[offset + 1] * 4 + 4;
16950 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16951 continue;
16952 }
16953
16954 /* This HBA contains PORT_STE configured */
16955 if (!rgn23_data[offset + 2])
16956 phba->hba_flag |= LINK_DISABLED;
16957
16958 goto out;
16959 }
16960 }
16961
16962 out:
16963 kfree(rgn23_data);
16964 return;
16965 }
16966
16967 /**
16968 * lpfc_wr_object - write an object to the firmware
16969 * @phba: HBA structure that indicates port to create a queue on.
16970 * @dmabuf_list: list of dmabufs to write to the port.
16971 * @size: the total byte value of the objects to write to the port.
16972 * @offset: the current offset to be used to start the transfer.
16973 *
16974 * This routine will create a wr_object mailbox command to send to the port.
16975 * the mailbox command will be constructed using the dma buffers described in
16976 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16977 * BDEs that the imbedded mailbox can support. The @offset variable will be
16978 * used to indicate the starting offset of the transfer and will also return
16979 * the offset after the write object mailbox has completed. @size is used to
16980 * determine the end of the object and whether the eof bit should be set.
16981 *
16982 * Return 0 is successful and offset will contain the the new offset to use
16983 * for the next write.
16984 * Return negative value for error cases.
16985 **/
16986 int
16987 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16988 uint32_t size, uint32_t *offset)
16989 {
16990 struct lpfc_mbx_wr_object *wr_object;
16991 LPFC_MBOXQ_t *mbox;
16992 int rc = 0, i = 0;
16993 uint32_t shdr_status, shdr_add_status;
16994 uint32_t mbox_tmo;
16995 union lpfc_sli4_cfg_shdr *shdr;
16996 struct lpfc_dmabuf *dmabuf;
16997 uint32_t written = 0;
16998
16999 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
17000 if (!mbox)
17001 return -ENOMEM;
17002
17003 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
17004 LPFC_MBOX_OPCODE_WRITE_OBJECT,
17005 sizeof(struct lpfc_mbx_wr_object) -
17006 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
17007
17008 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
17009 wr_object->u.request.write_offset = *offset;
17010 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
17011 wr_object->u.request.object_name[0] =
17012 cpu_to_le32(wr_object->u.request.object_name[0]);
17013 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
17014 list_for_each_entry(dmabuf, dmabuf_list, list) {
17015 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
17016 break;
17017 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
17018 wr_object->u.request.bde[i].addrHigh =
17019 putPaddrHigh(dmabuf->phys);
17020 if (written + SLI4_PAGE_SIZE >= size) {
17021 wr_object->u.request.bde[i].tus.f.bdeSize =
17022 (size - written);
17023 written += (size - written);
17024 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
17025 } else {
17026 wr_object->u.request.bde[i].tus.f.bdeSize =
17027 SLI4_PAGE_SIZE;
17028 written += SLI4_PAGE_SIZE;
17029 }
17030 i++;
17031 }
17032 wr_object->u.request.bde_count = i;
17033 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
17034 if (!phba->sli4_hba.intr_enable)
17035 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
17036 else {
17037 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
17038 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
17039 }
17040 /* The IOCTL status is embedded in the mailbox subheader. */
17041 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
17042 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
17043 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
17044 if (rc != MBX_TIMEOUT)
17045 mempool_free(mbox, phba->mbox_mem_pool);
17046 if (shdr_status || shdr_add_status || rc) {
17047 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
17048 "3025 Write Object mailbox failed with "
17049 "status x%x add_status x%x, mbx status x%x\n",
17050 shdr_status, shdr_add_status, rc);
17051 rc = -ENXIO;
17052 } else
17053 *offset += wr_object->u.response.actual_write_length;
17054 return rc;
17055 }
17056
17057 /**
17058 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
17059 * @vport: pointer to vport data structure.
17060 *
17061 * This function iterate through the mailboxq and clean up all REG_LOGIN
17062 * and REG_VPI mailbox commands associated with the vport. This function
17063 * is called when driver want to restart discovery of the vport due to
17064 * a Clear Virtual Link event.
17065 **/
17066 void
17067 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
17068 {
17069 struct lpfc_hba *phba = vport->phba;
17070 LPFC_MBOXQ_t *mb, *nextmb;
17071 struct lpfc_dmabuf *mp;
17072 struct lpfc_nodelist *ndlp;
17073 struct lpfc_nodelist *act_mbx_ndlp = NULL;
17074 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
17075 LIST_HEAD(mbox_cmd_list);
17076 uint8_t restart_loop;
17077
17078 /* Clean up internally queued mailbox commands with the vport */
17079 spin_lock_irq(&phba->hbalock);
17080 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
17081 if (mb->vport != vport)
17082 continue;
17083
17084 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17085 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17086 continue;
17087
17088 list_del(&mb->list);
17089 list_add_tail(&mb->list, &mbox_cmd_list);
17090 }
17091 /* Clean up active mailbox command with the vport */
17092 mb = phba->sli.mbox_active;
17093 if (mb && (mb->vport == vport)) {
17094 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
17095 (mb->u.mb.mbxCommand == MBX_REG_VPI))
17096 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17097 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17098 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
17099 /* Put reference count for delayed processing */
17100 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
17101 /* Unregister the RPI when mailbox complete */
17102 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17103 }
17104 }
17105 /* Cleanup any mailbox completions which are not yet processed */
17106 do {
17107 restart_loop = 0;
17108 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
17109 /*
17110 * If this mailox is already processed or it is
17111 * for another vport ignore it.
17112 */
17113 if ((mb->vport != vport) ||
17114 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
17115 continue;
17116
17117 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
17118 (mb->u.mb.mbxCommand != MBX_REG_VPI))
17119 continue;
17120
17121 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
17122 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17123 ndlp = (struct lpfc_nodelist *)mb->context2;
17124 /* Unregister the RPI when mailbox complete */
17125 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
17126 restart_loop = 1;
17127 spin_unlock_irq(&phba->hbalock);
17128 spin_lock(shost->host_lock);
17129 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17130 spin_unlock(shost->host_lock);
17131 spin_lock_irq(&phba->hbalock);
17132 break;
17133 }
17134 }
17135 } while (restart_loop);
17136
17137 spin_unlock_irq(&phba->hbalock);
17138
17139 /* Release the cleaned-up mailbox commands */
17140 while (!list_empty(&mbox_cmd_list)) {
17141 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
17142 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
17143 mp = (struct lpfc_dmabuf *) (mb->context1);
17144 if (mp) {
17145 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
17146 kfree(mp);
17147 }
17148 ndlp = (struct lpfc_nodelist *) mb->context2;
17149 mb->context2 = NULL;
17150 if (ndlp) {
17151 spin_lock(shost->host_lock);
17152 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17153 spin_unlock(shost->host_lock);
17154 lpfc_nlp_put(ndlp);
17155 }
17156 }
17157 mempool_free(mb, phba->mbox_mem_pool);
17158 }
17159
17160 /* Release the ndlp with the cleaned-up active mailbox command */
17161 if (act_mbx_ndlp) {
17162 spin_lock(shost->host_lock);
17163 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
17164 spin_unlock(shost->host_lock);
17165 lpfc_nlp_put(act_mbx_ndlp);
17166 }
17167 }
17168
17169 /**
17170 * lpfc_drain_txq - Drain the txq
17171 * @phba: Pointer to HBA context object.
17172 *
17173 * This function attempt to submit IOCBs on the txq
17174 * to the adapter. For SLI4 adapters, the txq contains
17175 * ELS IOCBs that have been deferred because the there
17176 * are no SGLs. This congestion can occur with large
17177 * vport counts during node discovery.
17178 **/
17179
17180 uint32_t
17181 lpfc_drain_txq(struct lpfc_hba *phba)
17182 {
17183 LIST_HEAD(completions);
17184 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
17185 struct lpfc_iocbq *piocbq = NULL;
17186 unsigned long iflags = 0;
17187 char *fail_msg = NULL;
17188 struct lpfc_sglq *sglq;
17189 union lpfc_wqe wqe;
17190 uint32_t txq_cnt = 0;
17191
17192 spin_lock_irqsave(&pring->ring_lock, iflags);
17193 list_for_each_entry(piocbq, &pring->txq, list) {
17194 txq_cnt++;
17195 }
17196
17197 if (txq_cnt > pring->txq_max)
17198 pring->txq_max = txq_cnt;
17199
17200 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17201
17202 while (!list_empty(&pring->txq)) {
17203 spin_lock_irqsave(&pring->ring_lock, iflags);
17204
17205 piocbq = lpfc_sli_ringtx_get(phba, pring);
17206 if (!piocbq) {
17207 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17208 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17209 "2823 txq empty and txq_cnt is %d\n ",
17210 txq_cnt);
17211 break;
17212 }
17213 sglq = __lpfc_sli_get_sglq(phba, piocbq);
17214 if (!sglq) {
17215 __lpfc_sli_ringtx_put(phba, pring, piocbq);
17216 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17217 break;
17218 }
17219 txq_cnt--;
17220
17221 /* The xri and iocb resources secured,
17222 * attempt to issue request
17223 */
17224 piocbq->sli4_lxritag = sglq->sli4_lxritag;
17225 piocbq->sli4_xritag = sglq->sli4_xritag;
17226 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
17227 fail_msg = "to convert bpl to sgl";
17228 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
17229 fail_msg = "to convert iocb to wqe";
17230 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
17231 fail_msg = " - Wq is full";
17232 else
17233 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
17234
17235 if (fail_msg) {
17236 /* Failed means we can't issue and need to cancel */
17237 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
17238 "2822 IOCB failed %s iotag 0x%x "
17239 "xri 0x%x\n",
17240 fail_msg,
17241 piocbq->iotag, piocbq->sli4_xritag);
17242 list_add_tail(&piocbq->list, &completions);
17243 }
17244 spin_unlock_irqrestore(&pring->ring_lock, iflags);
17245 }
17246
17247 /* Cancel all the IOCBs that cannot be issued */
17248 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
17249 IOERR_SLI_ABORTED);
17250
17251 return txq_cnt;
17252 }