Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[GitHub/mt8127/android_kernel_alcatel_ttab.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-2012 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
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_cmnd.h>
30 #include <scsi/scsi_device.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_transport_fc.h>
33 #include <scsi/fc/fc_fs.h>
34 #include <linux/aer.h>
35
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_logmsg.h"
46 #include "lpfc_compat.h"
47 #include "lpfc_debugfs.h"
48 #include "lpfc_vport.h"
49
50 /* There are only four IOCB completion types. */
51 typedef enum _lpfc_iocb_type {
52 LPFC_UNKNOWN_IOCB,
53 LPFC_UNSOL_IOCB,
54 LPFC_SOL_IOCB,
55 LPFC_ABORT_IOCB
56 } lpfc_iocb_type;
57
58
59 /* Provide function prototypes local to this module. */
60 static int lpfc_sli_issue_mbox_s4(struct lpfc_hba *, LPFC_MBOXQ_t *,
61 uint32_t);
62 static int lpfc_sli4_read_rev(struct lpfc_hba *, LPFC_MBOXQ_t *,
63 uint8_t *, uint32_t *);
64 static struct lpfc_iocbq *lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *,
65 struct lpfc_iocbq *);
66 static void lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *,
67 struct hbq_dmabuf *);
68 static int lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *, struct lpfc_queue *,
69 struct lpfc_cqe *);
70 static int lpfc_sli4_post_els_sgl_list(struct lpfc_hba *, struct list_head *,
71 int);
72 static void lpfc_sli4_hba_handle_eqe(struct lpfc_hba *, struct lpfc_eqe *,
73 uint32_t);
74
75 static IOCB_t *
76 lpfc_get_iocb_from_iocbq(struct lpfc_iocbq *iocbq)
77 {
78 return &iocbq->iocb;
79 }
80
81 /**
82 * lpfc_sli4_wq_put - Put a Work Queue Entry on an Work Queue
83 * @q: The Work Queue to operate on.
84 * @wqe: The work Queue Entry to put on the Work queue.
85 *
86 * This routine will copy the contents of @wqe to the next available entry on
87 * the @q. This function will then ring the Work Queue Doorbell to signal the
88 * HBA to start processing the Work Queue Entry. This function returns 0 if
89 * successful. If no entries are available on @q then this function will return
90 * -ENOMEM.
91 * The caller is expected to hold the hbalock when calling this routine.
92 **/
93 static uint32_t
94 lpfc_sli4_wq_put(struct lpfc_queue *q, union lpfc_wqe *wqe)
95 {
96 union lpfc_wqe *temp_wqe;
97 struct lpfc_register doorbell;
98 uint32_t host_index;
99 uint32_t idx;
100
101 /* sanity check on queue memory */
102 if (unlikely(!q))
103 return -ENOMEM;
104 temp_wqe = q->qe[q->host_index].wqe;
105
106 /* If the host has not yet processed the next entry then we are done */
107 idx = ((q->host_index + 1) % q->entry_count);
108 if (idx == q->hba_index) {
109 q->WQ_overflow++;
110 return -ENOMEM;
111 }
112 q->WQ_posted++;
113 /* set consumption flag every once in a while */
114 if (!((q->host_index + 1) % q->entry_repost))
115 bf_set(wqe_wqec, &wqe->generic.wqe_com, 1);
116 if (q->phba->sli3_options & LPFC_SLI4_PHWQ_ENABLED)
117 bf_set(wqe_wqid, &wqe->generic.wqe_com, q->queue_id);
118 lpfc_sli_pcimem_bcopy(wqe, temp_wqe, q->entry_size);
119
120 /* Update the host index before invoking device */
121 host_index = q->host_index;
122
123 q->host_index = idx;
124
125 /* Ring Doorbell */
126 doorbell.word0 = 0;
127 if (q->db_format == LPFC_DB_LIST_FORMAT) {
128 bf_set(lpfc_wq_db_list_fm_num_posted, &doorbell, 1);
129 bf_set(lpfc_wq_db_list_fm_index, &doorbell, host_index);
130 bf_set(lpfc_wq_db_list_fm_id, &doorbell, q->queue_id);
131 } else if (q->db_format == LPFC_DB_RING_FORMAT) {
132 bf_set(lpfc_wq_db_ring_fm_num_posted, &doorbell, 1);
133 bf_set(lpfc_wq_db_ring_fm_id, &doorbell, q->queue_id);
134 } else {
135 return -EINVAL;
136 }
137 writel(doorbell.word0, q->db_regaddr);
138
139 return 0;
140 }
141
142 /**
143 * lpfc_sli4_wq_release - Updates internal hba index for WQ
144 * @q: The Work Queue to operate on.
145 * @index: The index to advance the hba index to.
146 *
147 * This routine will update the HBA index of a queue to reflect consumption of
148 * Work Queue Entries by the HBA. When the HBA indicates that it has consumed
149 * an entry the host calls this function to update the queue's internal
150 * pointers. This routine returns the number of entries that were consumed by
151 * the HBA.
152 **/
153 static uint32_t
154 lpfc_sli4_wq_release(struct lpfc_queue *q, uint32_t index)
155 {
156 uint32_t released = 0;
157
158 /* sanity check on queue memory */
159 if (unlikely(!q))
160 return 0;
161
162 if (q->hba_index == index)
163 return 0;
164 do {
165 q->hba_index = ((q->hba_index + 1) % q->entry_count);
166 released++;
167 } while (q->hba_index != index);
168 return released;
169 }
170
171 /**
172 * lpfc_sli4_mq_put - Put a Mailbox Queue Entry on an Mailbox Queue
173 * @q: The Mailbox Queue to operate on.
174 * @wqe: The Mailbox Queue Entry to put on the Work queue.
175 *
176 * This routine will copy the contents of @mqe to the next available entry on
177 * the @q. This function will then ring the Work Queue Doorbell to signal the
178 * HBA to start processing the Work Queue Entry. This function returns 0 if
179 * successful. If no entries are available on @q then this function will return
180 * -ENOMEM.
181 * The caller is expected to hold the hbalock when calling this routine.
182 **/
183 static uint32_t
184 lpfc_sli4_mq_put(struct lpfc_queue *q, struct lpfc_mqe *mqe)
185 {
186 struct lpfc_mqe *temp_mqe;
187 struct lpfc_register doorbell;
188 uint32_t host_index;
189
190 /* sanity check on queue memory */
191 if (unlikely(!q))
192 return -ENOMEM;
193 temp_mqe = q->qe[q->host_index].mqe;
194
195 /* If the host has not yet processed the next entry then we are done */
196 if (((q->host_index + 1) % q->entry_count) == q->hba_index)
197 return -ENOMEM;
198 lpfc_sli_pcimem_bcopy(mqe, temp_mqe, q->entry_size);
199 /* Save off the mailbox pointer for completion */
200 q->phba->mbox = (MAILBOX_t *)temp_mqe;
201
202 /* Update the host index before invoking device */
203 host_index = q->host_index;
204 q->host_index = ((q->host_index + 1) % q->entry_count);
205
206 /* Ring Doorbell */
207 doorbell.word0 = 0;
208 bf_set(lpfc_mq_doorbell_num_posted, &doorbell, 1);
209 bf_set(lpfc_mq_doorbell_id, &doorbell, q->queue_id);
210 writel(doorbell.word0, q->phba->sli4_hba.MQDBregaddr);
211 return 0;
212 }
213
214 /**
215 * lpfc_sli4_mq_release - Updates internal hba index for MQ
216 * @q: The Mailbox Queue to operate on.
217 *
218 * This routine will update the HBA index of a queue to reflect consumption of
219 * a Mailbox Queue Entry by the HBA. When the HBA indicates that it has consumed
220 * an entry the host calls this function to update the queue's internal
221 * pointers. This routine returns the number of entries that were consumed by
222 * the HBA.
223 **/
224 static uint32_t
225 lpfc_sli4_mq_release(struct lpfc_queue *q)
226 {
227 /* sanity check on queue memory */
228 if (unlikely(!q))
229 return 0;
230
231 /* Clear the mailbox pointer for completion */
232 q->phba->mbox = NULL;
233 q->hba_index = ((q->hba_index + 1) % q->entry_count);
234 return 1;
235 }
236
237 /**
238 * lpfc_sli4_eq_get - Gets the next valid EQE from a EQ
239 * @q: The Event Queue to get the first valid EQE from
240 *
241 * This routine will get the first valid Event Queue Entry from @q, update
242 * the queue's internal hba index, and return the EQE. If no valid EQEs are in
243 * the Queue (no more work to do), or the Queue is full of EQEs that have been
244 * processed, but not popped back to the HBA then this routine will return NULL.
245 **/
246 static struct lpfc_eqe *
247 lpfc_sli4_eq_get(struct lpfc_queue *q)
248 {
249 struct lpfc_eqe *eqe;
250 uint32_t idx;
251
252 /* sanity check on queue memory */
253 if (unlikely(!q))
254 return NULL;
255 eqe = q->qe[q->hba_index].eqe;
256
257 /* If the next EQE is not valid then we are done */
258 if (!bf_get_le32(lpfc_eqe_valid, eqe))
259 return NULL;
260 /* If the host has not yet processed the next entry then we are done */
261 idx = ((q->hba_index + 1) % q->entry_count);
262 if (idx == q->host_index)
263 return NULL;
264
265 q->hba_index = idx;
266 return eqe;
267 }
268
269 /**
270 * lpfc_sli4_eq_clr_intr - Turn off interrupts from this EQ
271 * @q: The Event Queue to disable interrupts
272 *
273 **/
274 static inline void
275 lpfc_sli4_eq_clr_intr(struct lpfc_queue *q)
276 {
277 struct lpfc_register doorbell;
278
279 doorbell.word0 = 0;
280 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
281 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
282 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
283 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
284 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
285 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
286 }
287
288 /**
289 * lpfc_sli4_eq_release - Indicates the host has finished processing an EQ
290 * @q: The Event Queue that the host has completed processing for.
291 * @arm: Indicates whether the host wants to arms this CQ.
292 *
293 * This routine will mark all Event Queue Entries on @q, from the last
294 * known completed entry to the last entry that was processed, as completed
295 * by clearing the valid bit for each completion queue entry. Then it will
296 * notify the HBA, by ringing the doorbell, that the EQEs have been processed.
297 * The internal host index in the @q will be updated by this routine to indicate
298 * that the host has finished processing the entries. The @arm parameter
299 * indicates that the queue should be rearmed when ringing the doorbell.
300 *
301 * This function will return the number of EQEs that were popped.
302 **/
303 uint32_t
304 lpfc_sli4_eq_release(struct lpfc_queue *q, bool arm)
305 {
306 uint32_t released = 0;
307 struct lpfc_eqe *temp_eqe;
308 struct lpfc_register doorbell;
309
310 /* sanity check on queue memory */
311 if (unlikely(!q))
312 return 0;
313
314 /* while there are valid entries */
315 while (q->hba_index != q->host_index) {
316 temp_eqe = q->qe[q->host_index].eqe;
317 bf_set_le32(lpfc_eqe_valid, temp_eqe, 0);
318 released++;
319 q->host_index = ((q->host_index + 1) % q->entry_count);
320 }
321 if (unlikely(released == 0 && !arm))
322 return 0;
323
324 /* ring doorbell for number popped */
325 doorbell.word0 = 0;
326 if (arm) {
327 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
328 bf_set(lpfc_eqcq_doorbell_eqci, &doorbell, 1);
329 }
330 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
331 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_EVENT);
332 bf_set(lpfc_eqcq_doorbell_eqid_hi, &doorbell,
333 (q->queue_id >> LPFC_EQID_HI_FIELD_SHIFT));
334 bf_set(lpfc_eqcq_doorbell_eqid_lo, &doorbell, q->queue_id);
335 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
336 /* PCI read to flush PCI pipeline on re-arming for INTx mode */
337 if ((q->phba->intr_type == INTx) && (arm == LPFC_QUEUE_REARM))
338 readl(q->phba->sli4_hba.EQCQDBregaddr);
339 return released;
340 }
341
342 /**
343 * lpfc_sli4_cq_get - Gets the next valid CQE from a CQ
344 * @q: The Completion Queue to get the first valid CQE from
345 *
346 * This routine will get the first valid Completion Queue Entry from @q, update
347 * the queue's internal hba index, and return the CQE. If no valid CQEs are in
348 * the Queue (no more work to do), or the Queue is full of CQEs that have been
349 * processed, but not popped back to the HBA then this routine will return NULL.
350 **/
351 static struct lpfc_cqe *
352 lpfc_sli4_cq_get(struct lpfc_queue *q)
353 {
354 struct lpfc_cqe *cqe;
355 uint32_t idx;
356
357 /* sanity check on queue memory */
358 if (unlikely(!q))
359 return NULL;
360
361 /* If the next CQE is not valid then we are done */
362 if (!bf_get_le32(lpfc_cqe_valid, q->qe[q->hba_index].cqe))
363 return NULL;
364 /* If the host has not yet processed the next entry then we are done */
365 idx = ((q->hba_index + 1) % q->entry_count);
366 if (idx == q->host_index)
367 return NULL;
368
369 cqe = q->qe[q->hba_index].cqe;
370 q->hba_index = idx;
371 return cqe;
372 }
373
374 /**
375 * lpfc_sli4_cq_release - Indicates the host has finished processing a CQ
376 * @q: The Completion Queue that the host has completed processing for.
377 * @arm: Indicates whether the host wants to arms this CQ.
378 *
379 * This routine will mark all Completion queue entries on @q, from the last
380 * known completed entry to the last entry that was processed, as completed
381 * by clearing the valid bit for each completion queue entry. Then it will
382 * notify the HBA, by ringing the doorbell, that the CQEs have been processed.
383 * The internal host index in the @q will be updated by this routine to indicate
384 * that the host has finished processing the entries. The @arm parameter
385 * indicates that the queue should be rearmed when ringing the doorbell.
386 *
387 * This function will return the number of CQEs that were released.
388 **/
389 uint32_t
390 lpfc_sli4_cq_release(struct lpfc_queue *q, bool arm)
391 {
392 uint32_t released = 0;
393 struct lpfc_cqe *temp_qe;
394 struct lpfc_register doorbell;
395
396 /* sanity check on queue memory */
397 if (unlikely(!q))
398 return 0;
399 /* while there are valid entries */
400 while (q->hba_index != q->host_index) {
401 temp_qe = q->qe[q->host_index].cqe;
402 bf_set_le32(lpfc_cqe_valid, temp_qe, 0);
403 released++;
404 q->host_index = ((q->host_index + 1) % q->entry_count);
405 }
406 if (unlikely(released == 0 && !arm))
407 return 0;
408
409 /* ring doorbell for number popped */
410 doorbell.word0 = 0;
411 if (arm)
412 bf_set(lpfc_eqcq_doorbell_arm, &doorbell, 1);
413 bf_set(lpfc_eqcq_doorbell_num_released, &doorbell, released);
414 bf_set(lpfc_eqcq_doorbell_qt, &doorbell, LPFC_QUEUE_TYPE_COMPLETION);
415 bf_set(lpfc_eqcq_doorbell_cqid_hi, &doorbell,
416 (q->queue_id >> LPFC_CQID_HI_FIELD_SHIFT));
417 bf_set(lpfc_eqcq_doorbell_cqid_lo, &doorbell, q->queue_id);
418 writel(doorbell.word0, q->phba->sli4_hba.EQCQDBregaddr);
419 return released;
420 }
421
422 /**
423 * lpfc_sli4_rq_put - Put a Receive Buffer Queue Entry on a Receive Queue
424 * @q: The Header Receive Queue to operate on.
425 * @wqe: The Receive Queue Entry to put on the Receive queue.
426 *
427 * This routine will copy the contents of @wqe to the next available entry on
428 * the @q. This function will then ring the Receive Queue Doorbell to signal the
429 * HBA to start processing the Receive Queue Entry. This function returns the
430 * index that the rqe was copied to if successful. If no entries are available
431 * on @q then this function will return -ENOMEM.
432 * The caller is expected to hold the hbalock when calling this routine.
433 **/
434 static int
435 lpfc_sli4_rq_put(struct lpfc_queue *hq, struct lpfc_queue *dq,
436 struct lpfc_rqe *hrqe, struct lpfc_rqe *drqe)
437 {
438 struct lpfc_rqe *temp_hrqe;
439 struct lpfc_rqe *temp_drqe;
440 struct lpfc_register doorbell;
441 int put_index;
442
443 /* sanity check on queue memory */
444 if (unlikely(!hq) || unlikely(!dq))
445 return -ENOMEM;
446 put_index = hq->host_index;
447 temp_hrqe = hq->qe[hq->host_index].rqe;
448 temp_drqe = dq->qe[dq->host_index].rqe;
449
450 if (hq->type != LPFC_HRQ || dq->type != LPFC_DRQ)
451 return -EINVAL;
452 if (hq->host_index != dq->host_index)
453 return -EINVAL;
454 /* If the host has not yet processed the next entry then we are done */
455 if (((hq->host_index + 1) % hq->entry_count) == hq->hba_index)
456 return -EBUSY;
457 lpfc_sli_pcimem_bcopy(hrqe, temp_hrqe, hq->entry_size);
458 lpfc_sli_pcimem_bcopy(drqe, temp_drqe, dq->entry_size);
459
460 /* Update the host index to point to the next slot */
461 hq->host_index = ((hq->host_index + 1) % hq->entry_count);
462 dq->host_index = ((dq->host_index + 1) % dq->entry_count);
463
464 /* Ring The Header Receive Queue Doorbell */
465 if (!(hq->host_index % hq->entry_repost)) {
466 doorbell.word0 = 0;
467 if (hq->db_format == LPFC_DB_RING_FORMAT) {
468 bf_set(lpfc_rq_db_ring_fm_num_posted, &doorbell,
469 hq->entry_repost);
470 bf_set(lpfc_rq_db_ring_fm_id, &doorbell, hq->queue_id);
471 } else if (hq->db_format == LPFC_DB_LIST_FORMAT) {
472 bf_set(lpfc_rq_db_list_fm_num_posted, &doorbell,
473 hq->entry_repost);
474 bf_set(lpfc_rq_db_list_fm_index, &doorbell,
475 hq->host_index);
476 bf_set(lpfc_rq_db_list_fm_id, &doorbell, hq->queue_id);
477 } else {
478 return -EINVAL;
479 }
480 writel(doorbell.word0, hq->db_regaddr);
481 }
482 return put_index;
483 }
484
485 /**
486 * lpfc_sli4_rq_release - Updates internal hba index for RQ
487 * @q: The Header Receive Queue to operate on.
488 *
489 * This routine will update the HBA index of a queue to reflect consumption of
490 * one Receive Queue Entry by the HBA. When the HBA indicates that it has
491 * consumed an entry the host calls this function to update the queue's
492 * internal pointers. This routine returns the number of entries that were
493 * consumed by the HBA.
494 **/
495 static uint32_t
496 lpfc_sli4_rq_release(struct lpfc_queue *hq, struct lpfc_queue *dq)
497 {
498 /* sanity check on queue memory */
499 if (unlikely(!hq) || unlikely(!dq))
500 return 0;
501
502 if ((hq->type != LPFC_HRQ) || (dq->type != LPFC_DRQ))
503 return 0;
504 hq->hba_index = ((hq->hba_index + 1) % hq->entry_count);
505 dq->hba_index = ((dq->hba_index + 1) % dq->entry_count);
506 return 1;
507 }
508
509 /**
510 * lpfc_cmd_iocb - Get next command iocb entry in the ring
511 * @phba: Pointer to HBA context object.
512 * @pring: Pointer to driver SLI ring object.
513 *
514 * This function returns pointer to next command iocb entry
515 * in the command ring. The caller must hold hbalock to prevent
516 * other threads consume the next command iocb.
517 * SLI-2/SLI-3 provide different sized iocbs.
518 **/
519 static inline IOCB_t *
520 lpfc_cmd_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
521 {
522 return (IOCB_t *) (((char *) pring->sli.sli3.cmdringaddr) +
523 pring->sli.sli3.cmdidx * phba->iocb_cmd_size);
524 }
525
526 /**
527 * lpfc_resp_iocb - Get next response iocb entry in the ring
528 * @phba: Pointer to HBA context object.
529 * @pring: Pointer to driver SLI ring object.
530 *
531 * This function returns pointer to next response iocb entry
532 * in the response ring. The caller must hold hbalock to make sure
533 * that no other thread consume the next response iocb.
534 * SLI-2/SLI-3 provide different sized iocbs.
535 **/
536 static inline IOCB_t *
537 lpfc_resp_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
538 {
539 return (IOCB_t *) (((char *) pring->sli.sli3.rspringaddr) +
540 pring->sli.sli3.rspidx * phba->iocb_rsp_size);
541 }
542
543 /**
544 * __lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
545 * @phba: Pointer to HBA context object.
546 *
547 * This function is called with hbalock held. This function
548 * allocates a new driver iocb object from the iocb pool. If the
549 * allocation is successful, it returns pointer to the newly
550 * allocated iocb object else it returns NULL.
551 **/
552 struct lpfc_iocbq *
553 __lpfc_sli_get_iocbq(struct lpfc_hba *phba)
554 {
555 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
556 struct lpfc_iocbq * iocbq = NULL;
557
558 list_remove_head(lpfc_iocb_list, iocbq, struct lpfc_iocbq, list);
559 if (iocbq)
560 phba->iocb_cnt++;
561 if (phba->iocb_cnt > phba->iocb_max)
562 phba->iocb_max = phba->iocb_cnt;
563 return iocbq;
564 }
565
566 /**
567 * __lpfc_clear_active_sglq - Remove the active sglq for this XRI.
568 * @phba: Pointer to HBA context object.
569 * @xritag: XRI value.
570 *
571 * This function clears the sglq pointer from the array of acive
572 * sglq's. The xritag that is passed in is used to index into the
573 * array. Before the xritag can be used it needs to be adjusted
574 * by subtracting the xribase.
575 *
576 * Returns sglq ponter = success, NULL = Failure.
577 **/
578 static struct lpfc_sglq *
579 __lpfc_clear_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
580 {
581 struct lpfc_sglq *sglq;
582
583 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
584 phba->sli4_hba.lpfc_sglq_active_list[xritag] = NULL;
585 return sglq;
586 }
587
588 /**
589 * __lpfc_get_active_sglq - Get the active sglq for this XRI.
590 * @phba: Pointer to HBA context object.
591 * @xritag: XRI value.
592 *
593 * This function returns the sglq pointer from the array of acive
594 * sglq's. The xritag that is passed in is used to index into the
595 * array. Before the xritag can be used it needs to be adjusted
596 * by subtracting the xribase.
597 *
598 * Returns sglq ponter = success, NULL = Failure.
599 **/
600 struct lpfc_sglq *
601 __lpfc_get_active_sglq(struct lpfc_hba *phba, uint16_t xritag)
602 {
603 struct lpfc_sglq *sglq;
604
605 sglq = phba->sli4_hba.lpfc_sglq_active_list[xritag];
606 return sglq;
607 }
608
609 /**
610 * lpfc_clr_rrq_active - Clears RRQ active bit in xri_bitmap.
611 * @phba: Pointer to HBA context object.
612 * @xritag: xri used in this exchange.
613 * @rrq: The RRQ to be cleared.
614 *
615 **/
616 void
617 lpfc_clr_rrq_active(struct lpfc_hba *phba,
618 uint16_t xritag,
619 struct lpfc_node_rrq *rrq)
620 {
621 struct lpfc_nodelist *ndlp = NULL;
622
623 if ((rrq->vport) && NLP_CHK_NODE_ACT(rrq->ndlp))
624 ndlp = lpfc_findnode_did(rrq->vport, rrq->nlp_DID);
625
626 /* The target DID could have been swapped (cable swap)
627 * we should use the ndlp from the findnode if it is
628 * available.
629 */
630 if ((!ndlp) && rrq->ndlp)
631 ndlp = rrq->ndlp;
632
633 if (!ndlp)
634 goto out;
635
636 if (test_and_clear_bit(xritag, ndlp->active_rrqs.xri_bitmap)) {
637 rrq->send_rrq = 0;
638 rrq->xritag = 0;
639 rrq->rrq_stop_time = 0;
640 }
641 out:
642 mempool_free(rrq, phba->rrq_pool);
643 }
644
645 /**
646 * lpfc_handle_rrq_active - Checks if RRQ has waithed RATOV.
647 * @phba: Pointer to HBA context object.
648 *
649 * This function is called with hbalock held. This function
650 * Checks if stop_time (ratov from setting rrq active) has
651 * been reached, if it has and the send_rrq flag is set then
652 * it will call lpfc_send_rrq. If the send_rrq flag is not set
653 * then it will just call the routine to clear the rrq and
654 * free the rrq resource.
655 * The timer is set to the next rrq that is going to expire before
656 * leaving the routine.
657 *
658 **/
659 void
660 lpfc_handle_rrq_active(struct lpfc_hba *phba)
661 {
662 struct lpfc_node_rrq *rrq;
663 struct lpfc_node_rrq *nextrrq;
664 unsigned long next_time;
665 unsigned long iflags;
666 LIST_HEAD(send_rrq);
667
668 spin_lock_irqsave(&phba->hbalock, iflags);
669 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
670 next_time = jiffies + HZ * (phba->fc_ratov + 1);
671 list_for_each_entry_safe(rrq, nextrrq,
672 &phba->active_rrq_list, list) {
673 if (time_after(jiffies, rrq->rrq_stop_time))
674 list_move(&rrq->list, &send_rrq);
675 else if (time_before(rrq->rrq_stop_time, next_time))
676 next_time = rrq->rrq_stop_time;
677 }
678 spin_unlock_irqrestore(&phba->hbalock, iflags);
679 if (!list_empty(&phba->active_rrq_list))
680 mod_timer(&phba->rrq_tmr, next_time);
681 list_for_each_entry_safe(rrq, nextrrq, &send_rrq, list) {
682 list_del(&rrq->list);
683 if (!rrq->send_rrq)
684 /* this call will free the rrq */
685 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
686 else if (lpfc_send_rrq(phba, rrq)) {
687 /* if we send the rrq then the completion handler
688 * will clear the bit in the xribitmap.
689 */
690 lpfc_clr_rrq_active(phba, rrq->xritag,
691 rrq);
692 }
693 }
694 }
695
696 /**
697 * lpfc_get_active_rrq - Get the active RRQ for this exchange.
698 * @vport: Pointer to vport context object.
699 * @xri: The xri used in the exchange.
700 * @did: The targets DID for this exchange.
701 *
702 * returns NULL = rrq not found in the phba->active_rrq_list.
703 * rrq = rrq for this xri and target.
704 **/
705 struct lpfc_node_rrq *
706 lpfc_get_active_rrq(struct lpfc_vport *vport, uint16_t xri, uint32_t did)
707 {
708 struct lpfc_hba *phba = vport->phba;
709 struct lpfc_node_rrq *rrq;
710 struct lpfc_node_rrq *nextrrq;
711 unsigned long iflags;
712
713 if (phba->sli_rev != LPFC_SLI_REV4)
714 return NULL;
715 spin_lock_irqsave(&phba->hbalock, iflags);
716 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list) {
717 if (rrq->vport == vport && rrq->xritag == xri &&
718 rrq->nlp_DID == did){
719 list_del(&rrq->list);
720 spin_unlock_irqrestore(&phba->hbalock, iflags);
721 return rrq;
722 }
723 }
724 spin_unlock_irqrestore(&phba->hbalock, iflags);
725 return NULL;
726 }
727
728 /**
729 * lpfc_cleanup_vports_rrqs - Remove and clear the active RRQ for this vport.
730 * @vport: Pointer to vport context object.
731 * @ndlp: Pointer to the lpfc_node_list structure.
732 * If ndlp is NULL Remove all active RRQs for this vport from the
733 * phba->active_rrq_list and clear the rrq.
734 * If ndlp is not NULL then only remove rrqs for this vport & this ndlp.
735 **/
736 void
737 lpfc_cleanup_vports_rrqs(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
738
739 {
740 struct lpfc_hba *phba = vport->phba;
741 struct lpfc_node_rrq *rrq;
742 struct lpfc_node_rrq *nextrrq;
743 unsigned long iflags;
744 LIST_HEAD(rrq_list);
745
746 if (phba->sli_rev != LPFC_SLI_REV4)
747 return;
748 if (!ndlp) {
749 lpfc_sli4_vport_delete_els_xri_aborted(vport);
750 lpfc_sli4_vport_delete_fcp_xri_aborted(vport);
751 }
752 spin_lock_irqsave(&phba->hbalock, iflags);
753 list_for_each_entry_safe(rrq, nextrrq, &phba->active_rrq_list, list)
754 if ((rrq->vport == vport) && (!ndlp || rrq->ndlp == ndlp))
755 list_move(&rrq->list, &rrq_list);
756 spin_unlock_irqrestore(&phba->hbalock, iflags);
757
758 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
759 list_del(&rrq->list);
760 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
761 }
762 }
763
764 /**
765 * lpfc_cleanup_wt_rrqs - Remove all rrq's from the active list.
766 * @phba: Pointer to HBA context object.
767 *
768 * Remove all rrqs from the phba->active_rrq_list and free them by
769 * calling __lpfc_clr_active_rrq
770 *
771 **/
772 void
773 lpfc_cleanup_wt_rrqs(struct lpfc_hba *phba)
774 {
775 struct lpfc_node_rrq *rrq;
776 struct lpfc_node_rrq *nextrrq;
777 unsigned long next_time;
778 unsigned long iflags;
779 LIST_HEAD(rrq_list);
780
781 if (phba->sli_rev != LPFC_SLI_REV4)
782 return;
783 spin_lock_irqsave(&phba->hbalock, iflags);
784 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
785 next_time = jiffies + HZ * (phba->fc_ratov * 2);
786 list_splice_init(&phba->active_rrq_list, &rrq_list);
787 spin_unlock_irqrestore(&phba->hbalock, iflags);
788
789 list_for_each_entry_safe(rrq, nextrrq, &rrq_list, list) {
790 list_del(&rrq->list);
791 lpfc_clr_rrq_active(phba, rrq->xritag, rrq);
792 }
793 if (!list_empty(&phba->active_rrq_list))
794 mod_timer(&phba->rrq_tmr, next_time);
795 }
796
797
798 /**
799 * lpfc_test_rrq_active - Test RRQ bit in xri_bitmap.
800 * @phba: Pointer to HBA context object.
801 * @ndlp: Targets nodelist pointer for this exchange.
802 * @xritag the xri in the bitmap to test.
803 *
804 * This function is called with hbalock held. This function
805 * returns 0 = rrq not active for this xri
806 * 1 = rrq is valid for this xri.
807 **/
808 int
809 lpfc_test_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
810 uint16_t xritag)
811 {
812 if (!ndlp)
813 return 0;
814 if (test_bit(xritag, ndlp->active_rrqs.xri_bitmap))
815 return 1;
816 else
817 return 0;
818 }
819
820 /**
821 * lpfc_set_rrq_active - set RRQ active bit in xri_bitmap.
822 * @phba: Pointer to HBA context object.
823 * @ndlp: nodelist pointer for this target.
824 * @xritag: xri used in this exchange.
825 * @rxid: Remote Exchange ID.
826 * @send_rrq: Flag used to determine if we should send rrq els cmd.
827 *
828 * This function takes the hbalock.
829 * The active bit is always set in the active rrq xri_bitmap even
830 * if there is no slot avaiable for the other rrq information.
831 *
832 * returns 0 rrq actived for this xri
833 * < 0 No memory or invalid ndlp.
834 **/
835 int
836 lpfc_set_rrq_active(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
837 uint16_t xritag, uint16_t rxid, uint16_t send_rrq)
838 {
839 unsigned long iflags;
840 struct lpfc_node_rrq *rrq;
841 int empty;
842
843 if (!ndlp)
844 return -EINVAL;
845
846 if (!phba->cfg_enable_rrq)
847 return -EINVAL;
848
849 spin_lock_irqsave(&phba->hbalock, iflags);
850 if (phba->pport->load_flag & FC_UNLOADING) {
851 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
852 goto out;
853 }
854
855 /*
856 * set the active bit even if there is no mem available.
857 */
858 if (NLP_CHK_FREE_REQ(ndlp))
859 goto out;
860
861 if (ndlp->vport && (ndlp->vport->load_flag & FC_UNLOADING))
862 goto out;
863
864 if (test_and_set_bit(xritag, ndlp->active_rrqs.xri_bitmap))
865 goto out;
866
867 spin_unlock_irqrestore(&phba->hbalock, iflags);
868 rrq = mempool_alloc(phba->rrq_pool, GFP_KERNEL);
869 if (!rrq) {
870 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
871 "3155 Unable to allocate RRQ xri:0x%x rxid:0x%x"
872 " DID:0x%x Send:%d\n",
873 xritag, rxid, ndlp->nlp_DID, send_rrq);
874 return -EINVAL;
875 }
876 if (phba->cfg_enable_rrq == 1)
877 rrq->send_rrq = send_rrq;
878 else
879 rrq->send_rrq = 0;
880 rrq->xritag = xritag;
881 rrq->rrq_stop_time = jiffies + HZ * (phba->fc_ratov + 1);
882 rrq->ndlp = ndlp;
883 rrq->nlp_DID = ndlp->nlp_DID;
884 rrq->vport = ndlp->vport;
885 rrq->rxid = rxid;
886 spin_lock_irqsave(&phba->hbalock, iflags);
887 empty = list_empty(&phba->active_rrq_list);
888 list_add_tail(&rrq->list, &phba->active_rrq_list);
889 phba->hba_flag |= HBA_RRQ_ACTIVE;
890 if (empty)
891 lpfc_worker_wake_up(phba);
892 spin_unlock_irqrestore(&phba->hbalock, iflags);
893 return 0;
894 out:
895 spin_unlock_irqrestore(&phba->hbalock, iflags);
896 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
897 "2921 Can't set rrq active xri:0x%x rxid:0x%x"
898 " DID:0x%x Send:%d\n",
899 xritag, rxid, ndlp->nlp_DID, send_rrq);
900 return -EINVAL;
901 }
902
903 /**
904 * __lpfc_sli_get_sglq - Allocates an iocb object from sgl pool
905 * @phba: Pointer to HBA context object.
906 * @piocb: Pointer to the iocbq.
907 *
908 * This function is called with hbalock held. This function
909 * gets a new driver sglq object from the sglq list. If the
910 * list is not empty then it is successful, it returns pointer to the newly
911 * allocated sglq object else it returns NULL.
912 **/
913 static struct lpfc_sglq *
914 __lpfc_sli_get_sglq(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq)
915 {
916 struct list_head *lpfc_sgl_list = &phba->sli4_hba.lpfc_sgl_list;
917 struct lpfc_sglq *sglq = NULL;
918 struct lpfc_sglq *start_sglq = NULL;
919 struct lpfc_scsi_buf *lpfc_cmd;
920 struct lpfc_nodelist *ndlp;
921 int found = 0;
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.ulpCommand == CMD_ELS_REQUEST64_CR) &&
930 (piocbq->iocb_flag & LPFC_IO_LIBDFC))
931 ndlp = piocbq->context_un.ndlp;
932 else
933 ndlp = piocbq->context1;
934
935 list_remove_head(lpfc_sgl_list, sglq, struct lpfc_sglq, list);
936 start_sglq = sglq;
937 while (!found) {
938 if (!sglq)
939 return NULL;
940 if (lpfc_test_rrq_active(phba, ndlp, sglq->sli4_lxritag)) {
941 /* This xri has an rrq outstanding for this DID.
942 * put it back in the list and get another xri.
943 */
944 list_add_tail(&sglq->list, lpfc_sgl_list);
945 sglq = NULL;
946 list_remove_head(lpfc_sgl_list, sglq,
947 struct lpfc_sglq, list);
948 if (sglq == start_sglq) {
949 sglq = NULL;
950 break;
951 } else
952 continue;
953 }
954 sglq->ndlp = ndlp;
955 found = 1;
956 phba->sli4_hba.lpfc_sglq_active_list[sglq->sli4_lxritag] = sglq;
957 sglq->state = SGL_ALLOCATED;
958 }
959 return sglq;
960 }
961
962 /**
963 * lpfc_sli_get_iocbq - Allocates an iocb object from iocb pool
964 * @phba: Pointer to HBA context object.
965 *
966 * This function is called with no lock held. This function
967 * allocates a new driver iocb object from the iocb pool. If the
968 * allocation is successful, it returns pointer to the newly
969 * allocated iocb object else it returns NULL.
970 **/
971 struct lpfc_iocbq *
972 lpfc_sli_get_iocbq(struct lpfc_hba *phba)
973 {
974 struct lpfc_iocbq * iocbq = NULL;
975 unsigned long iflags;
976
977 spin_lock_irqsave(&phba->hbalock, iflags);
978 iocbq = __lpfc_sli_get_iocbq(phba);
979 spin_unlock_irqrestore(&phba->hbalock, iflags);
980 return iocbq;
981 }
982
983 /**
984 * __lpfc_sli_release_iocbq_s4 - Release iocb to the iocb pool
985 * @phba: Pointer to HBA context object.
986 * @iocbq: Pointer to driver iocb object.
987 *
988 * This function is called with hbalock held to release driver
989 * iocb object to the iocb pool. The iotag in the iocb object
990 * does not change for each use of the iocb object. This function
991 * clears all other fields of the iocb object when it is freed.
992 * The sqlq structure that holds the xritag and phys and virtual
993 * mappings for the scatter gather list is retrieved from the
994 * active array of sglq. The get of the sglq pointer also clears
995 * the entry in the array. If the status of the IO indiactes that
996 * this IO was aborted then the sglq entry it put on the
997 * lpfc_abts_els_sgl_list until the CQ_ABORTED_XRI is received. If the
998 * IO has good status or fails for any other reason then the sglq
999 * entry is added to the free list (lpfc_sgl_list).
1000 **/
1001 static void
1002 __lpfc_sli_release_iocbq_s4(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1003 {
1004 struct lpfc_sglq *sglq;
1005 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1006 unsigned long iflag = 0;
1007 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
1008
1009 if (iocbq->sli4_xritag == NO_XRI)
1010 sglq = NULL;
1011 else
1012 sglq = __lpfc_clear_active_sglq(phba, iocbq->sli4_lxritag);
1013
1014 /*
1015 ** This should have been removed from the txcmplq before calling
1016 ** iocbq_release. The normal completion
1017 ** path should have already done the list_del_init.
1018 */
1019 if (unlikely(!list_empty(&iocbq->list))) {
1020 if (iocbq->iocb_flag & LPFC_IO_ON_TXCMPLQ)
1021 iocbq->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
1022 list_del_init(&iocbq->list);
1023 }
1024
1025
1026 if (sglq) {
1027 if ((iocbq->iocb_flag & LPFC_EXCHANGE_BUSY) &&
1028 (sglq->state != SGL_XRI_ABORTED)) {
1029 spin_lock_irqsave(&phba->sli4_hba.abts_sgl_list_lock,
1030 iflag);
1031 list_add(&sglq->list,
1032 &phba->sli4_hba.lpfc_abts_els_sgl_list);
1033 spin_unlock_irqrestore(
1034 &phba->sli4_hba.abts_sgl_list_lock, iflag);
1035 } else {
1036 sglq->state = SGL_FREED;
1037 sglq->ndlp = NULL;
1038 list_add_tail(&sglq->list,
1039 &phba->sli4_hba.lpfc_sgl_list);
1040
1041 /* Check if TXQ queue needs to be serviced */
1042 if (!list_empty(&pring->txq))
1043 lpfc_worker_wake_up(phba);
1044 }
1045 }
1046
1047
1048 /*
1049 * Clean all volatile data fields, preserve iotag and node struct.
1050 */
1051 memset((char *)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1052 iocbq->sli4_lxritag = NO_XRI;
1053 iocbq->sli4_xritag = NO_XRI;
1054 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1055 }
1056
1057
1058 /**
1059 * __lpfc_sli_release_iocbq_s3 - Release iocb to the iocb pool
1060 * @phba: Pointer to HBA context object.
1061 * @iocbq: Pointer to driver iocb object.
1062 *
1063 * This function is called with hbalock held to release driver
1064 * iocb object to the iocb pool. The iotag in the iocb object
1065 * does not change for each use of the iocb object. This function
1066 * clears all other fields of the iocb object when it is freed.
1067 **/
1068 static void
1069 __lpfc_sli_release_iocbq_s3(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1070 {
1071 size_t start_clean = offsetof(struct lpfc_iocbq, iocb);
1072
1073 /*
1074 ** This should have been removed from the txcmplq before calling
1075 ** iocbq_release. The normal completion
1076 ** path should have already done the list_del_init.
1077 */
1078 if (unlikely(!list_empty(&iocbq->list)))
1079 list_del_init(&iocbq->list);
1080
1081 /*
1082 * Clean all volatile data fields, preserve iotag and node struct.
1083 */
1084 memset((char*)iocbq + start_clean, 0, sizeof(*iocbq) - start_clean);
1085 iocbq->sli4_xritag = NO_XRI;
1086 list_add_tail(&iocbq->list, &phba->lpfc_iocb_list);
1087 }
1088
1089 /**
1090 * __lpfc_sli_release_iocbq - Release iocb to the iocb pool
1091 * @phba: Pointer to HBA context object.
1092 * @iocbq: Pointer to driver iocb object.
1093 *
1094 * This function is called with hbalock held to release driver
1095 * iocb object to the iocb pool. The iotag in the iocb object
1096 * does not change for each use of the iocb object. This function
1097 * clears all other fields of the iocb object when it is freed.
1098 **/
1099 static void
1100 __lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1101 {
1102 phba->__lpfc_sli_release_iocbq(phba, iocbq);
1103 phba->iocb_cnt--;
1104 }
1105
1106 /**
1107 * lpfc_sli_release_iocbq - Release iocb to the iocb pool
1108 * @phba: Pointer to HBA context object.
1109 * @iocbq: Pointer to driver iocb object.
1110 *
1111 * This function is called with no lock held to release the iocb to
1112 * iocb pool.
1113 **/
1114 void
1115 lpfc_sli_release_iocbq(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq)
1116 {
1117 unsigned long iflags;
1118
1119 /*
1120 * Clean all volatile data fields, preserve iotag and node struct.
1121 */
1122 spin_lock_irqsave(&phba->hbalock, iflags);
1123 __lpfc_sli_release_iocbq(phba, iocbq);
1124 spin_unlock_irqrestore(&phba->hbalock, iflags);
1125 }
1126
1127 /**
1128 * lpfc_sli_cancel_iocbs - Cancel all iocbs from a list.
1129 * @phba: Pointer to HBA context object.
1130 * @iocblist: List of IOCBs.
1131 * @ulpstatus: ULP status in IOCB command field.
1132 * @ulpWord4: ULP word-4 in IOCB command field.
1133 *
1134 * This function is called with a list of IOCBs to cancel. It cancels the IOCB
1135 * on the list by invoking the complete callback function associated with the
1136 * IOCB with the provided @ulpstatus and @ulpword4 set to the IOCB commond
1137 * fields.
1138 **/
1139 void
1140 lpfc_sli_cancel_iocbs(struct lpfc_hba *phba, struct list_head *iocblist,
1141 uint32_t ulpstatus, uint32_t ulpWord4)
1142 {
1143 struct lpfc_iocbq *piocb;
1144
1145 while (!list_empty(iocblist)) {
1146 list_remove_head(iocblist, piocb, struct lpfc_iocbq, list);
1147 if (!piocb->iocb_cmpl)
1148 lpfc_sli_release_iocbq(phba, piocb);
1149 else {
1150 piocb->iocb.ulpStatus = ulpstatus;
1151 piocb->iocb.un.ulpWord[4] = ulpWord4;
1152 (piocb->iocb_cmpl) (phba, piocb, piocb);
1153 }
1154 }
1155 return;
1156 }
1157
1158 /**
1159 * lpfc_sli_iocb_cmd_type - Get the iocb type
1160 * @iocb_cmnd: iocb command code.
1161 *
1162 * This function is called by ring event handler function to get the iocb type.
1163 * This function translates the iocb command to an iocb command type used to
1164 * decide the final disposition of each completed IOCB.
1165 * The function returns
1166 * LPFC_UNKNOWN_IOCB if it is an unsupported iocb
1167 * LPFC_SOL_IOCB if it is a solicited iocb completion
1168 * LPFC_ABORT_IOCB if it is an abort iocb
1169 * LPFC_UNSOL_IOCB if it is an unsolicited iocb
1170 *
1171 * The caller is not required to hold any lock.
1172 **/
1173 static lpfc_iocb_type
1174 lpfc_sli_iocb_cmd_type(uint8_t iocb_cmnd)
1175 {
1176 lpfc_iocb_type type = LPFC_UNKNOWN_IOCB;
1177
1178 if (iocb_cmnd > CMD_MAX_IOCB_CMD)
1179 return 0;
1180
1181 switch (iocb_cmnd) {
1182 case CMD_XMIT_SEQUENCE_CR:
1183 case CMD_XMIT_SEQUENCE_CX:
1184 case CMD_XMIT_BCAST_CN:
1185 case CMD_XMIT_BCAST_CX:
1186 case CMD_ELS_REQUEST_CR:
1187 case CMD_ELS_REQUEST_CX:
1188 case CMD_CREATE_XRI_CR:
1189 case CMD_CREATE_XRI_CX:
1190 case CMD_GET_RPI_CN:
1191 case CMD_XMIT_ELS_RSP_CX:
1192 case CMD_GET_RPI_CR:
1193 case CMD_FCP_IWRITE_CR:
1194 case CMD_FCP_IWRITE_CX:
1195 case CMD_FCP_IREAD_CR:
1196 case CMD_FCP_IREAD_CX:
1197 case CMD_FCP_ICMND_CR:
1198 case CMD_FCP_ICMND_CX:
1199 case CMD_FCP_TSEND_CX:
1200 case CMD_FCP_TRSP_CX:
1201 case CMD_FCP_TRECEIVE_CX:
1202 case CMD_FCP_AUTO_TRSP_CX:
1203 case CMD_ADAPTER_MSG:
1204 case CMD_ADAPTER_DUMP:
1205 case CMD_XMIT_SEQUENCE64_CR:
1206 case CMD_XMIT_SEQUENCE64_CX:
1207 case CMD_XMIT_BCAST64_CN:
1208 case CMD_XMIT_BCAST64_CX:
1209 case CMD_ELS_REQUEST64_CR:
1210 case CMD_ELS_REQUEST64_CX:
1211 case CMD_FCP_IWRITE64_CR:
1212 case CMD_FCP_IWRITE64_CX:
1213 case CMD_FCP_IREAD64_CR:
1214 case CMD_FCP_IREAD64_CX:
1215 case CMD_FCP_ICMND64_CR:
1216 case CMD_FCP_ICMND64_CX:
1217 case CMD_FCP_TSEND64_CX:
1218 case CMD_FCP_TRSP64_CX:
1219 case CMD_FCP_TRECEIVE64_CX:
1220 case CMD_GEN_REQUEST64_CR:
1221 case CMD_GEN_REQUEST64_CX:
1222 case CMD_XMIT_ELS_RSP64_CX:
1223 case DSSCMD_IWRITE64_CR:
1224 case DSSCMD_IWRITE64_CX:
1225 case DSSCMD_IREAD64_CR:
1226 case DSSCMD_IREAD64_CX:
1227 type = LPFC_SOL_IOCB;
1228 break;
1229 case CMD_ABORT_XRI_CN:
1230 case CMD_ABORT_XRI_CX:
1231 case CMD_CLOSE_XRI_CN:
1232 case CMD_CLOSE_XRI_CX:
1233 case CMD_XRI_ABORTED_CX:
1234 case CMD_ABORT_MXRI64_CN:
1235 case CMD_XMIT_BLS_RSP64_CX:
1236 type = LPFC_ABORT_IOCB;
1237 break;
1238 case CMD_RCV_SEQUENCE_CX:
1239 case CMD_RCV_ELS_REQ_CX:
1240 case CMD_RCV_SEQUENCE64_CX:
1241 case CMD_RCV_ELS_REQ64_CX:
1242 case CMD_ASYNC_STATUS:
1243 case CMD_IOCB_RCV_SEQ64_CX:
1244 case CMD_IOCB_RCV_ELS64_CX:
1245 case CMD_IOCB_RCV_CONT64_CX:
1246 case CMD_IOCB_RET_XRI64_CX:
1247 type = LPFC_UNSOL_IOCB;
1248 break;
1249 case CMD_IOCB_XMIT_MSEQ64_CR:
1250 case CMD_IOCB_XMIT_MSEQ64_CX:
1251 case CMD_IOCB_RCV_SEQ_LIST64_CX:
1252 case CMD_IOCB_RCV_ELS_LIST64_CX:
1253 case CMD_IOCB_CLOSE_EXTENDED_CN:
1254 case CMD_IOCB_ABORT_EXTENDED_CN:
1255 case CMD_IOCB_RET_HBQE64_CN:
1256 case CMD_IOCB_FCP_IBIDIR64_CR:
1257 case CMD_IOCB_FCP_IBIDIR64_CX:
1258 case CMD_IOCB_FCP_ITASKMGT64_CX:
1259 case CMD_IOCB_LOGENTRY_CN:
1260 case CMD_IOCB_LOGENTRY_ASYNC_CN:
1261 printk("%s - Unhandled SLI-3 Command x%x\n",
1262 __func__, iocb_cmnd);
1263 type = LPFC_UNKNOWN_IOCB;
1264 break;
1265 default:
1266 type = LPFC_UNKNOWN_IOCB;
1267 break;
1268 }
1269
1270 return type;
1271 }
1272
1273 /**
1274 * lpfc_sli_ring_map - Issue config_ring mbox for all rings
1275 * @phba: Pointer to HBA context object.
1276 *
1277 * This function is called from SLI initialization code
1278 * to configure every ring of the HBA's SLI interface. The
1279 * caller is not required to hold any lock. This function issues
1280 * a config_ring mailbox command for each ring.
1281 * This function returns zero if successful else returns a negative
1282 * error code.
1283 **/
1284 static int
1285 lpfc_sli_ring_map(struct lpfc_hba *phba)
1286 {
1287 struct lpfc_sli *psli = &phba->sli;
1288 LPFC_MBOXQ_t *pmb;
1289 MAILBOX_t *pmbox;
1290 int i, rc, ret = 0;
1291
1292 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1293 if (!pmb)
1294 return -ENOMEM;
1295 pmbox = &pmb->u.mb;
1296 phba->link_state = LPFC_INIT_MBX_CMDS;
1297 for (i = 0; i < psli->num_rings; i++) {
1298 lpfc_config_ring(phba, i, pmb);
1299 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
1300 if (rc != MBX_SUCCESS) {
1301 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1302 "0446 Adapter failed to init (%d), "
1303 "mbxCmd x%x CFG_RING, mbxStatus x%x, "
1304 "ring %d\n",
1305 rc, pmbox->mbxCommand,
1306 pmbox->mbxStatus, i);
1307 phba->link_state = LPFC_HBA_ERROR;
1308 ret = -ENXIO;
1309 break;
1310 }
1311 }
1312 mempool_free(pmb, phba->mbox_mem_pool);
1313 return ret;
1314 }
1315
1316 /**
1317 * lpfc_sli_ringtxcmpl_put - Adds new iocb to the txcmplq
1318 * @phba: Pointer to HBA context object.
1319 * @pring: Pointer to driver SLI ring object.
1320 * @piocb: Pointer to the driver iocb object.
1321 *
1322 * This function is called with hbalock held. The function adds the
1323 * new iocb to txcmplq of the given ring. This function always returns
1324 * 0. If this function is called for ELS ring, this function checks if
1325 * there is a vport associated with the ELS command. This function also
1326 * starts els_tmofunc timer if this is an ELS command.
1327 **/
1328 static int
1329 lpfc_sli_ringtxcmpl_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1330 struct lpfc_iocbq *piocb)
1331 {
1332 list_add_tail(&piocb->list, &pring->txcmplq);
1333 piocb->iocb_flag |= LPFC_IO_ON_TXCMPLQ;
1334
1335 if ((unlikely(pring->ringno == LPFC_ELS_RING)) &&
1336 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
1337 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
1338 if (!piocb->vport)
1339 BUG();
1340 else
1341 mod_timer(&piocb->vport->els_tmofunc,
1342 jiffies + HZ * (phba->fc_ratov << 1));
1343 }
1344
1345
1346 return 0;
1347 }
1348
1349 /**
1350 * lpfc_sli_ringtx_get - Get first element of the txq
1351 * @phba: Pointer to HBA context object.
1352 * @pring: Pointer to driver SLI ring object.
1353 *
1354 * This function is called with hbalock held to get next
1355 * iocb in txq of the given ring. If there is any iocb in
1356 * the txq, the function returns first iocb in the list after
1357 * removing the iocb from the list, else it returns NULL.
1358 **/
1359 struct lpfc_iocbq *
1360 lpfc_sli_ringtx_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1361 {
1362 struct lpfc_iocbq *cmd_iocb;
1363
1364 list_remove_head((&pring->txq), cmd_iocb, struct lpfc_iocbq, list);
1365 return cmd_iocb;
1366 }
1367
1368 /**
1369 * lpfc_sli_next_iocb_slot - Get next iocb slot in the ring
1370 * @phba: Pointer to HBA context object.
1371 * @pring: Pointer to driver SLI ring object.
1372 *
1373 * This function is called with hbalock held and the caller must post the
1374 * iocb without releasing the lock. If the caller releases the lock,
1375 * iocb slot returned by the function is not guaranteed to be available.
1376 * The function returns pointer to the next available iocb slot if there
1377 * is available slot in the ring, else it returns NULL.
1378 * If the get index of the ring is ahead of the put index, the function
1379 * will post an error attention event to the worker thread to take the
1380 * HBA to offline state.
1381 **/
1382 static IOCB_t *
1383 lpfc_sli_next_iocb_slot (struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1384 {
1385 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
1386 uint32_t max_cmd_idx = pring->sli.sli3.numCiocb;
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 /*
1518 * Set up an iotag
1519 */
1520 nextiocb->iocb.ulpIoTag = (nextiocb->iocb_cmpl) ? nextiocb->iotag : 0;
1521
1522
1523 if (pring->ringno == LPFC_ELS_RING) {
1524 lpfc_debugfs_slow_ring_trc(phba,
1525 "IOCB cmd ring: wd4:x%08x wd6:x%08x wd7:x%08x",
1526 *(((uint32_t *) &nextiocb->iocb) + 4),
1527 *(((uint32_t *) &nextiocb->iocb) + 6),
1528 *(((uint32_t *) &nextiocb->iocb) + 7));
1529 }
1530
1531 /*
1532 * Issue iocb command to adapter
1533 */
1534 lpfc_sli_pcimem_bcopy(&nextiocb->iocb, iocb, phba->iocb_cmd_size);
1535 wmb();
1536 pring->stats.iocb_cmd++;
1537
1538 /*
1539 * If there is no completion routine to call, we can release the
1540 * IOCB buffer back right now. For IOCBs, like QUE_RING_BUF,
1541 * that have no rsp ring completion, iocb_cmpl MUST be NULL.
1542 */
1543 if (nextiocb->iocb_cmpl)
1544 lpfc_sli_ringtxcmpl_put(phba, pring, nextiocb);
1545 else
1546 __lpfc_sli_release_iocbq(phba, nextiocb);
1547
1548 /*
1549 * Let the HBA know what IOCB slot will be the next one the
1550 * driver will put a command into.
1551 */
1552 pring->sli.sli3.cmdidx = pring->sli.sli3.next_cmdidx;
1553 writel(pring->sli.sli3.cmdidx, &phba->host_gp[pring->ringno].cmdPutInx);
1554 }
1555
1556 /**
1557 * lpfc_sli_update_full_ring - Update the chip attention register
1558 * @phba: Pointer to HBA context object.
1559 * @pring: Pointer to driver SLI ring object.
1560 *
1561 * The caller is not required to hold any lock for calling this function.
1562 * This function updates the chip attention bits for the ring to inform firmware
1563 * that there are pending work to be done for this ring and requests an
1564 * interrupt when there is space available in the ring. This function is
1565 * called when the driver is unable to post more iocbs to the ring due
1566 * to unavailability of space in the ring.
1567 **/
1568 static void
1569 lpfc_sli_update_full_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1570 {
1571 int ringno = pring->ringno;
1572
1573 pring->flag |= LPFC_CALL_RING_AVAILABLE;
1574
1575 wmb();
1576
1577 /*
1578 * Set ring 'ringno' to SET R0CE_REQ in Chip Att register.
1579 * The HBA will tell us when an IOCB entry is available.
1580 */
1581 writel((CA_R0ATT|CA_R0CE_REQ) << (ringno*4), phba->CAregaddr);
1582 readl(phba->CAregaddr); /* flush */
1583
1584 pring->stats.iocb_cmd_full++;
1585 }
1586
1587 /**
1588 * lpfc_sli_update_ring - Update chip attention register
1589 * @phba: Pointer to HBA context object.
1590 * @pring: Pointer to driver SLI ring object.
1591 *
1592 * This function updates the chip attention register bit for the
1593 * given ring to inform HBA that there is more work to be done
1594 * in this ring. The caller is not required to hold any lock.
1595 **/
1596 static void
1597 lpfc_sli_update_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1598 {
1599 int ringno = pring->ringno;
1600
1601 /*
1602 * Tell the HBA that there is work to do in this ring.
1603 */
1604 if (!(phba->sli3_options & LPFC_SLI3_CRP_ENABLED)) {
1605 wmb();
1606 writel(CA_R0ATT << (ringno * 4), phba->CAregaddr);
1607 readl(phba->CAregaddr); /* flush */
1608 }
1609 }
1610
1611 /**
1612 * lpfc_sli_resume_iocb - Process iocbs in the txq
1613 * @phba: Pointer to HBA context object.
1614 * @pring: Pointer to driver SLI ring object.
1615 *
1616 * This function is called with hbalock held to post pending iocbs
1617 * in the txq to the firmware. This function is called when driver
1618 * detects space available in the ring.
1619 **/
1620 static void
1621 lpfc_sli_resume_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
1622 {
1623 IOCB_t *iocb;
1624 struct lpfc_iocbq *nextiocb;
1625
1626 /*
1627 * Check to see if:
1628 * (a) there is anything on the txq to send
1629 * (b) link is up
1630 * (c) link attention events can be processed (fcp ring only)
1631 * (d) IOCB processing is not blocked by the outstanding mbox command.
1632 */
1633
1634 if (lpfc_is_link_up(phba) &&
1635 (!list_empty(&pring->txq)) &&
1636 (pring->ringno != phba->sli.fcp_ring ||
1637 phba->sli.sli_flag & LPFC_PROCESS_LA)) {
1638
1639 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
1640 (nextiocb = lpfc_sli_ringtx_get(phba, pring)))
1641 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
1642
1643 if (iocb)
1644 lpfc_sli_update_ring(phba, pring);
1645 else
1646 lpfc_sli_update_full_ring(phba, pring);
1647 }
1648
1649 return;
1650 }
1651
1652 /**
1653 * lpfc_sli_next_hbq_slot - Get next hbq entry for the HBQ
1654 * @phba: Pointer to HBA context object.
1655 * @hbqno: HBQ number.
1656 *
1657 * This function is called with hbalock held to get the next
1658 * available slot for the given HBQ. If there is free slot
1659 * available for the HBQ it will return pointer to the next available
1660 * HBQ entry else it will return NULL.
1661 **/
1662 static struct lpfc_hbq_entry *
1663 lpfc_sli_next_hbq_slot(struct lpfc_hba *phba, uint32_t hbqno)
1664 {
1665 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1666
1667 if (hbqp->next_hbqPutIdx == hbqp->hbqPutIdx &&
1668 ++hbqp->next_hbqPutIdx >= hbqp->entry_count)
1669 hbqp->next_hbqPutIdx = 0;
1670
1671 if (unlikely(hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)) {
1672 uint32_t raw_index = phba->hbq_get[hbqno];
1673 uint32_t getidx = le32_to_cpu(raw_index);
1674
1675 hbqp->local_hbqGetIdx = getidx;
1676
1677 if (unlikely(hbqp->local_hbqGetIdx >= hbqp->entry_count)) {
1678 lpfc_printf_log(phba, KERN_ERR,
1679 LOG_SLI | LOG_VPORT,
1680 "1802 HBQ %d: local_hbqGetIdx "
1681 "%u is > than hbqp->entry_count %u\n",
1682 hbqno, hbqp->local_hbqGetIdx,
1683 hbqp->entry_count);
1684
1685 phba->link_state = LPFC_HBA_ERROR;
1686 return NULL;
1687 }
1688
1689 if (hbqp->local_hbqGetIdx == hbqp->next_hbqPutIdx)
1690 return NULL;
1691 }
1692
1693 return (struct lpfc_hbq_entry *) phba->hbqs[hbqno].hbq_virt +
1694 hbqp->hbqPutIdx;
1695 }
1696
1697 /**
1698 * lpfc_sli_hbqbuf_free_all - Free all the hbq buffers
1699 * @phba: Pointer to HBA context object.
1700 *
1701 * This function is called with no lock held to free all the
1702 * hbq buffers while uninitializing the SLI interface. It also
1703 * frees the HBQ buffers returned by the firmware but not yet
1704 * processed by the upper layers.
1705 **/
1706 void
1707 lpfc_sli_hbqbuf_free_all(struct lpfc_hba *phba)
1708 {
1709 struct lpfc_dmabuf *dmabuf, *next_dmabuf;
1710 struct hbq_dmabuf *hbq_buf;
1711 unsigned long flags;
1712 int i, hbq_count;
1713 uint32_t hbqno;
1714
1715 hbq_count = lpfc_sli_hbq_count();
1716 /* Return all memory used by all HBQs */
1717 spin_lock_irqsave(&phba->hbalock, flags);
1718 for (i = 0; i < hbq_count; ++i) {
1719 list_for_each_entry_safe(dmabuf, next_dmabuf,
1720 &phba->hbqs[i].hbq_buffer_list, list) {
1721 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1722 list_del(&hbq_buf->dbuf.list);
1723 (phba->hbqs[i].hbq_free_buffer)(phba, hbq_buf);
1724 }
1725 phba->hbqs[i].buffer_count = 0;
1726 }
1727 /* Return all HBQ buffer that are in-fly */
1728 list_for_each_entry_safe(dmabuf, next_dmabuf, &phba->rb_pend_list,
1729 list) {
1730 hbq_buf = container_of(dmabuf, struct hbq_dmabuf, dbuf);
1731 list_del(&hbq_buf->dbuf.list);
1732 if (hbq_buf->tag == -1) {
1733 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1734 (phba, hbq_buf);
1735 } else {
1736 hbqno = hbq_buf->tag >> 16;
1737 if (hbqno >= LPFC_MAX_HBQS)
1738 (phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer)
1739 (phba, hbq_buf);
1740 else
1741 (phba->hbqs[hbqno].hbq_free_buffer)(phba,
1742 hbq_buf);
1743 }
1744 }
1745
1746 /* Mark the HBQs not in use */
1747 phba->hbq_in_use = 0;
1748 spin_unlock_irqrestore(&phba->hbalock, flags);
1749 }
1750
1751 /**
1752 * lpfc_sli_hbq_to_firmware - Post the hbq buffer to firmware
1753 * @phba: Pointer to HBA context object.
1754 * @hbqno: HBQ number.
1755 * @hbq_buf: Pointer to HBQ buffer.
1756 *
1757 * This function is called with the hbalock held to post a
1758 * hbq buffer to the firmware. If the function finds an empty
1759 * slot in the HBQ, it will post the buffer. The function will return
1760 * pointer to the hbq entry if it successfully post the buffer
1761 * else it will return NULL.
1762 **/
1763 static int
1764 lpfc_sli_hbq_to_firmware(struct lpfc_hba *phba, uint32_t hbqno,
1765 struct hbq_dmabuf *hbq_buf)
1766 {
1767 return phba->lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buf);
1768 }
1769
1770 /**
1771 * lpfc_sli_hbq_to_firmware_s3 - Post the hbq buffer to SLI3 firmware
1772 * @phba: Pointer to HBA context object.
1773 * @hbqno: HBQ number.
1774 * @hbq_buf: Pointer to HBQ buffer.
1775 *
1776 * This function is called with the hbalock held to post a hbq buffer to the
1777 * firmware. If the function finds an empty slot in the HBQ, it will post the
1778 * buffer and place it on the hbq_buffer_list. The function will return zero if
1779 * it successfully post the buffer else it will return an error.
1780 **/
1781 static int
1782 lpfc_sli_hbq_to_firmware_s3(struct lpfc_hba *phba, uint32_t hbqno,
1783 struct hbq_dmabuf *hbq_buf)
1784 {
1785 struct lpfc_hbq_entry *hbqe;
1786 dma_addr_t physaddr = hbq_buf->dbuf.phys;
1787
1788 /* Get next HBQ entry slot to use */
1789 hbqe = lpfc_sli_next_hbq_slot(phba, hbqno);
1790 if (hbqe) {
1791 struct hbq_s *hbqp = &phba->hbqs[hbqno];
1792
1793 hbqe->bde.addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1794 hbqe->bde.addrLow = le32_to_cpu(putPaddrLow(physaddr));
1795 hbqe->bde.tus.f.bdeSize = hbq_buf->size;
1796 hbqe->bde.tus.f.bdeFlags = 0;
1797 hbqe->bde.tus.w = le32_to_cpu(hbqe->bde.tus.w);
1798 hbqe->buffer_tag = le32_to_cpu(hbq_buf->tag);
1799 /* Sync SLIM */
1800 hbqp->hbqPutIdx = hbqp->next_hbqPutIdx;
1801 writel(hbqp->hbqPutIdx, phba->hbq_put + hbqno);
1802 /* flush */
1803 readl(phba->hbq_put + hbqno);
1804 list_add_tail(&hbq_buf->dbuf.list, &hbqp->hbq_buffer_list);
1805 return 0;
1806 } else
1807 return -ENOMEM;
1808 }
1809
1810 /**
1811 * lpfc_sli_hbq_to_firmware_s4 - Post the hbq buffer to SLI4 firmware
1812 * @phba: Pointer to HBA context object.
1813 * @hbqno: HBQ number.
1814 * @hbq_buf: Pointer to HBQ buffer.
1815 *
1816 * This function is called with the hbalock held to post an RQE to the SLI4
1817 * firmware. If able to post the RQE to the RQ it will queue the hbq entry to
1818 * the hbq_buffer_list and return zero, otherwise it will return an error.
1819 **/
1820 static int
1821 lpfc_sli_hbq_to_firmware_s4(struct lpfc_hba *phba, uint32_t hbqno,
1822 struct hbq_dmabuf *hbq_buf)
1823 {
1824 int rc;
1825 struct lpfc_rqe hrqe;
1826 struct lpfc_rqe drqe;
1827
1828 hrqe.address_lo = putPaddrLow(hbq_buf->hbuf.phys);
1829 hrqe.address_hi = putPaddrHigh(hbq_buf->hbuf.phys);
1830 drqe.address_lo = putPaddrLow(hbq_buf->dbuf.phys);
1831 drqe.address_hi = putPaddrHigh(hbq_buf->dbuf.phys);
1832 rc = lpfc_sli4_rq_put(phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
1833 &hrqe, &drqe);
1834 if (rc < 0)
1835 return rc;
1836 hbq_buf->tag = rc;
1837 list_add_tail(&hbq_buf->dbuf.list, &phba->hbqs[hbqno].hbq_buffer_list);
1838 return 0;
1839 }
1840
1841 /* HBQ for ELS and CT traffic. */
1842 static struct lpfc_hbq_init lpfc_els_hbq = {
1843 .rn = 1,
1844 .entry_count = 256,
1845 .mask_count = 0,
1846 .profile = 0,
1847 .ring_mask = (1 << LPFC_ELS_RING),
1848 .buffer_count = 0,
1849 .init_count = 40,
1850 .add_count = 40,
1851 };
1852
1853 /* HBQ for the extra ring if needed */
1854 static struct lpfc_hbq_init lpfc_extra_hbq = {
1855 .rn = 1,
1856 .entry_count = 200,
1857 .mask_count = 0,
1858 .profile = 0,
1859 .ring_mask = (1 << LPFC_EXTRA_RING),
1860 .buffer_count = 0,
1861 .init_count = 0,
1862 .add_count = 5,
1863 };
1864
1865 /* Array of HBQs */
1866 struct lpfc_hbq_init *lpfc_hbq_defs[] = {
1867 &lpfc_els_hbq,
1868 &lpfc_extra_hbq,
1869 };
1870
1871 /**
1872 * lpfc_sli_hbqbuf_fill_hbqs - Post more hbq buffers to HBQ
1873 * @phba: Pointer to HBA context object.
1874 * @hbqno: HBQ number.
1875 * @count: Number of HBQ buffers to be posted.
1876 *
1877 * This function is called with no lock held to post more hbq buffers to the
1878 * given HBQ. The function returns the number of HBQ buffers successfully
1879 * posted.
1880 **/
1881 static int
1882 lpfc_sli_hbqbuf_fill_hbqs(struct lpfc_hba *phba, uint32_t hbqno, uint32_t count)
1883 {
1884 uint32_t i, posted = 0;
1885 unsigned long flags;
1886 struct hbq_dmabuf *hbq_buffer;
1887 LIST_HEAD(hbq_buf_list);
1888 if (!phba->hbqs[hbqno].hbq_alloc_buffer)
1889 return 0;
1890
1891 if ((phba->hbqs[hbqno].buffer_count + count) >
1892 lpfc_hbq_defs[hbqno]->entry_count)
1893 count = lpfc_hbq_defs[hbqno]->entry_count -
1894 phba->hbqs[hbqno].buffer_count;
1895 if (!count)
1896 return 0;
1897 /* Allocate HBQ entries */
1898 for (i = 0; i < count; i++) {
1899 hbq_buffer = (phba->hbqs[hbqno].hbq_alloc_buffer)(phba);
1900 if (!hbq_buffer)
1901 break;
1902 list_add_tail(&hbq_buffer->dbuf.list, &hbq_buf_list);
1903 }
1904 /* Check whether HBQ is still in use */
1905 spin_lock_irqsave(&phba->hbalock, flags);
1906 if (!phba->hbq_in_use)
1907 goto err;
1908 while (!list_empty(&hbq_buf_list)) {
1909 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1910 dbuf.list);
1911 hbq_buffer->tag = (phba->hbqs[hbqno].buffer_count |
1912 (hbqno << 16));
1913 if (!lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer)) {
1914 phba->hbqs[hbqno].buffer_count++;
1915 posted++;
1916 } else
1917 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1918 }
1919 spin_unlock_irqrestore(&phba->hbalock, flags);
1920 return posted;
1921 err:
1922 spin_unlock_irqrestore(&phba->hbalock, flags);
1923 while (!list_empty(&hbq_buf_list)) {
1924 list_remove_head(&hbq_buf_list, hbq_buffer, struct hbq_dmabuf,
1925 dbuf.list);
1926 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
1927 }
1928 return 0;
1929 }
1930
1931 /**
1932 * lpfc_sli_hbqbuf_add_hbqs - Post more HBQ buffers to firmware
1933 * @phba: Pointer to HBA context object.
1934 * @qno: HBQ number.
1935 *
1936 * This function posts more buffers to the HBQ. This function
1937 * is called with no lock held. The function returns the number of HBQ entries
1938 * successfully allocated.
1939 **/
1940 int
1941 lpfc_sli_hbqbuf_add_hbqs(struct lpfc_hba *phba, uint32_t qno)
1942 {
1943 if (phba->sli_rev == LPFC_SLI_REV4)
1944 return 0;
1945 else
1946 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1947 lpfc_hbq_defs[qno]->add_count);
1948 }
1949
1950 /**
1951 * lpfc_sli_hbqbuf_init_hbqs - Post initial buffers to the HBQ
1952 * @phba: Pointer to HBA context object.
1953 * @qno: HBQ queue number.
1954 *
1955 * This function is called from SLI initialization code path with
1956 * no lock held to post initial HBQ buffers to firmware. The
1957 * function returns the number of HBQ entries successfully allocated.
1958 **/
1959 static int
1960 lpfc_sli_hbqbuf_init_hbqs(struct lpfc_hba *phba, uint32_t qno)
1961 {
1962 if (phba->sli_rev == LPFC_SLI_REV4)
1963 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1964 lpfc_hbq_defs[qno]->entry_count);
1965 else
1966 return lpfc_sli_hbqbuf_fill_hbqs(phba, qno,
1967 lpfc_hbq_defs[qno]->init_count);
1968 }
1969
1970 /**
1971 * lpfc_sli_hbqbuf_get - Remove the first hbq off of an hbq list
1972 * @phba: Pointer to HBA context object.
1973 * @hbqno: HBQ number.
1974 *
1975 * This function removes the first hbq buffer on an hbq list and returns a
1976 * pointer to that buffer. If it finds no buffers on the list it returns NULL.
1977 **/
1978 static struct hbq_dmabuf *
1979 lpfc_sli_hbqbuf_get(struct list_head *rb_list)
1980 {
1981 struct lpfc_dmabuf *d_buf;
1982
1983 list_remove_head(rb_list, d_buf, struct lpfc_dmabuf, list);
1984 if (!d_buf)
1985 return NULL;
1986 return container_of(d_buf, struct hbq_dmabuf, dbuf);
1987 }
1988
1989 /**
1990 * lpfc_sli_hbqbuf_find - Find the hbq buffer associated with a tag
1991 * @phba: Pointer to HBA context object.
1992 * @tag: Tag of the hbq buffer.
1993 *
1994 * This function is called with hbalock held. This function searches
1995 * for the hbq buffer associated with the given tag in the hbq buffer
1996 * list. If it finds the hbq buffer, it returns the hbq_buffer other wise
1997 * it returns NULL.
1998 **/
1999 static struct hbq_dmabuf *
2000 lpfc_sli_hbqbuf_find(struct lpfc_hba *phba, uint32_t tag)
2001 {
2002 struct lpfc_dmabuf *d_buf;
2003 struct hbq_dmabuf *hbq_buf;
2004 uint32_t hbqno;
2005
2006 hbqno = tag >> 16;
2007 if (hbqno >= LPFC_MAX_HBQS)
2008 return NULL;
2009
2010 spin_lock_irq(&phba->hbalock);
2011 list_for_each_entry(d_buf, &phba->hbqs[hbqno].hbq_buffer_list, list) {
2012 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2013 if (hbq_buf->tag == tag) {
2014 spin_unlock_irq(&phba->hbalock);
2015 return hbq_buf;
2016 }
2017 }
2018 spin_unlock_irq(&phba->hbalock);
2019 lpfc_printf_log(phba, KERN_ERR, LOG_SLI | LOG_VPORT,
2020 "1803 Bad hbq tag. Data: x%x x%x\n",
2021 tag, phba->hbqs[tag >> 16].buffer_count);
2022 return NULL;
2023 }
2024
2025 /**
2026 * lpfc_sli_free_hbq - Give back the hbq buffer to firmware
2027 * @phba: Pointer to HBA context object.
2028 * @hbq_buffer: Pointer to HBQ buffer.
2029 *
2030 * This function is called with hbalock. This function gives back
2031 * the hbq buffer to firmware. If the HBQ does not have space to
2032 * post the buffer, it will free the buffer.
2033 **/
2034 void
2035 lpfc_sli_free_hbq(struct lpfc_hba *phba, struct hbq_dmabuf *hbq_buffer)
2036 {
2037 uint32_t hbqno;
2038
2039 if (hbq_buffer) {
2040 hbqno = hbq_buffer->tag >> 16;
2041 if (lpfc_sli_hbq_to_firmware(phba, hbqno, hbq_buffer))
2042 (phba->hbqs[hbqno].hbq_free_buffer)(phba, hbq_buffer);
2043 }
2044 }
2045
2046 /**
2047 * lpfc_sli_chk_mbx_command - Check if the mailbox is a legitimate mailbox
2048 * @mbxCommand: mailbox command code.
2049 *
2050 * This function is called by the mailbox event handler function to verify
2051 * that the completed mailbox command is a legitimate mailbox command. If the
2052 * completed mailbox is not known to the function, it will return MBX_SHUTDOWN
2053 * and the mailbox event handler will take the HBA offline.
2054 **/
2055 static int
2056 lpfc_sli_chk_mbx_command(uint8_t mbxCommand)
2057 {
2058 uint8_t ret;
2059
2060 switch (mbxCommand) {
2061 case MBX_LOAD_SM:
2062 case MBX_READ_NV:
2063 case MBX_WRITE_NV:
2064 case MBX_WRITE_VPARMS:
2065 case MBX_RUN_BIU_DIAG:
2066 case MBX_INIT_LINK:
2067 case MBX_DOWN_LINK:
2068 case MBX_CONFIG_LINK:
2069 case MBX_CONFIG_RING:
2070 case MBX_RESET_RING:
2071 case MBX_READ_CONFIG:
2072 case MBX_READ_RCONFIG:
2073 case MBX_READ_SPARM:
2074 case MBX_READ_STATUS:
2075 case MBX_READ_RPI:
2076 case MBX_READ_XRI:
2077 case MBX_READ_REV:
2078 case MBX_READ_LNK_STAT:
2079 case MBX_REG_LOGIN:
2080 case MBX_UNREG_LOGIN:
2081 case MBX_CLEAR_LA:
2082 case MBX_DUMP_MEMORY:
2083 case MBX_DUMP_CONTEXT:
2084 case MBX_RUN_DIAGS:
2085 case MBX_RESTART:
2086 case MBX_UPDATE_CFG:
2087 case MBX_DOWN_LOAD:
2088 case MBX_DEL_LD_ENTRY:
2089 case MBX_RUN_PROGRAM:
2090 case MBX_SET_MASK:
2091 case MBX_SET_VARIABLE:
2092 case MBX_UNREG_D_ID:
2093 case MBX_KILL_BOARD:
2094 case MBX_CONFIG_FARP:
2095 case MBX_BEACON:
2096 case MBX_LOAD_AREA:
2097 case MBX_RUN_BIU_DIAG64:
2098 case MBX_CONFIG_PORT:
2099 case MBX_READ_SPARM64:
2100 case MBX_READ_RPI64:
2101 case MBX_REG_LOGIN64:
2102 case MBX_READ_TOPOLOGY:
2103 case MBX_WRITE_WWN:
2104 case MBX_SET_DEBUG:
2105 case MBX_LOAD_EXP_ROM:
2106 case MBX_ASYNCEVT_ENABLE:
2107 case MBX_REG_VPI:
2108 case MBX_UNREG_VPI:
2109 case MBX_HEARTBEAT:
2110 case MBX_PORT_CAPABILITIES:
2111 case MBX_PORT_IOV_CONTROL:
2112 case MBX_SLI4_CONFIG:
2113 case MBX_SLI4_REQ_FTRS:
2114 case MBX_REG_FCFI:
2115 case MBX_UNREG_FCFI:
2116 case MBX_REG_VFI:
2117 case MBX_UNREG_VFI:
2118 case MBX_INIT_VPI:
2119 case MBX_INIT_VFI:
2120 case MBX_RESUME_RPI:
2121 case MBX_READ_EVENT_LOG_STATUS:
2122 case MBX_READ_EVENT_LOG:
2123 case MBX_SECURITY_MGMT:
2124 case MBX_AUTH_PORT:
2125 case MBX_ACCESS_VDATA:
2126 ret = mbxCommand;
2127 break;
2128 default:
2129 ret = MBX_SHUTDOWN;
2130 break;
2131 }
2132 return ret;
2133 }
2134
2135 /**
2136 * lpfc_sli_wake_mbox_wait - lpfc_sli_issue_mbox_wait mbox completion handler
2137 * @phba: Pointer to HBA context object.
2138 * @pmboxq: Pointer to mailbox command.
2139 *
2140 * This is completion handler function for mailbox commands issued from
2141 * lpfc_sli_issue_mbox_wait function. This function is called by the
2142 * mailbox event handler function with no lock held. This function
2143 * will wake up thread waiting on the wait queue pointed by context1
2144 * of the mailbox.
2145 **/
2146 void
2147 lpfc_sli_wake_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
2148 {
2149 wait_queue_head_t *pdone_q;
2150 unsigned long drvr_flag;
2151
2152 /*
2153 * If pdone_q is empty, the driver thread gave up waiting and
2154 * continued running.
2155 */
2156 pmboxq->mbox_flag |= LPFC_MBX_WAKE;
2157 spin_lock_irqsave(&phba->hbalock, drvr_flag);
2158 pdone_q = (wait_queue_head_t *) pmboxq->context1;
2159 if (pdone_q)
2160 wake_up_interruptible(pdone_q);
2161 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
2162 return;
2163 }
2164
2165
2166 /**
2167 * lpfc_sli_def_mbox_cmpl - Default mailbox completion handler
2168 * @phba: Pointer to HBA context object.
2169 * @pmb: Pointer to mailbox object.
2170 *
2171 * This function is the default mailbox completion handler. It
2172 * frees the memory resources associated with the completed mailbox
2173 * command. If the completed command is a REG_LOGIN mailbox command,
2174 * this function will issue a UREG_LOGIN to re-claim the RPI.
2175 **/
2176 void
2177 lpfc_sli_def_mbox_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
2178 {
2179 struct lpfc_vport *vport = pmb->vport;
2180 struct lpfc_dmabuf *mp;
2181 struct lpfc_nodelist *ndlp;
2182 struct Scsi_Host *shost;
2183 uint16_t rpi, vpi;
2184 int rc;
2185
2186 mp = (struct lpfc_dmabuf *) (pmb->context1);
2187
2188 if (mp) {
2189 lpfc_mbuf_free(phba, mp->virt, mp->phys);
2190 kfree(mp);
2191 }
2192
2193 /*
2194 * If a REG_LOGIN succeeded after node is destroyed or node
2195 * is in re-discovery driver need to cleanup the RPI.
2196 */
2197 if (!(phba->pport->load_flag & FC_UNLOADING) &&
2198 pmb->u.mb.mbxCommand == MBX_REG_LOGIN64 &&
2199 !pmb->u.mb.mbxStatus) {
2200 rpi = pmb->u.mb.un.varWords[0];
2201 vpi = pmb->u.mb.un.varRegLogin.vpi;
2202 lpfc_unreg_login(phba, vpi, rpi, pmb);
2203 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
2204 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2205 if (rc != MBX_NOT_FINISHED)
2206 return;
2207 }
2208
2209 if ((pmb->u.mb.mbxCommand == MBX_REG_VPI) &&
2210 !(phba->pport->load_flag & FC_UNLOADING) &&
2211 !pmb->u.mb.mbxStatus) {
2212 shost = lpfc_shost_from_vport(vport);
2213 spin_lock_irq(shost->host_lock);
2214 vport->vpi_state |= LPFC_VPI_REGISTERED;
2215 vport->fc_flag &= ~FC_VPORT_NEEDS_REG_VPI;
2216 spin_unlock_irq(shost->host_lock);
2217 }
2218
2219 if (pmb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
2220 ndlp = (struct lpfc_nodelist *)pmb->context2;
2221 lpfc_nlp_put(ndlp);
2222 pmb->context2 = NULL;
2223 }
2224
2225 /* Check security permission status on INIT_LINK mailbox command */
2226 if ((pmb->u.mb.mbxCommand == MBX_INIT_LINK) &&
2227 (pmb->u.mb.mbxStatus == MBXERR_SEC_NO_PERMISSION))
2228 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2229 "2860 SLI authentication is required "
2230 "for INIT_LINK but has not done yet\n");
2231
2232 if (bf_get(lpfc_mqe_command, &pmb->u.mqe) == MBX_SLI4_CONFIG)
2233 lpfc_sli4_mbox_cmd_free(phba, pmb);
2234 else
2235 mempool_free(pmb, phba->mbox_mem_pool);
2236 }
2237
2238 /**
2239 * lpfc_sli_handle_mb_event - Handle mailbox completions from firmware
2240 * @phba: Pointer to HBA context object.
2241 *
2242 * This function is called with no lock held. This function processes all
2243 * the completed mailbox commands and gives it to upper layers. The interrupt
2244 * service routine processes mailbox completion interrupt and adds completed
2245 * mailbox commands to the mboxq_cmpl queue and signals the worker thread.
2246 * Worker thread call lpfc_sli_handle_mb_event, which will return the
2247 * completed mailbox commands in mboxq_cmpl queue to the upper layers. This
2248 * function returns the mailbox commands to the upper layer by calling the
2249 * completion handler function of each mailbox.
2250 **/
2251 int
2252 lpfc_sli_handle_mb_event(struct lpfc_hba *phba)
2253 {
2254 MAILBOX_t *pmbox;
2255 LPFC_MBOXQ_t *pmb;
2256 int rc;
2257 LIST_HEAD(cmplq);
2258
2259 phba->sli.slistat.mbox_event++;
2260
2261 /* Get all completed mailboxe buffers into the cmplq */
2262 spin_lock_irq(&phba->hbalock);
2263 list_splice_init(&phba->sli.mboxq_cmpl, &cmplq);
2264 spin_unlock_irq(&phba->hbalock);
2265
2266 /* Get a Mailbox buffer to setup mailbox commands for callback */
2267 do {
2268 list_remove_head(&cmplq, pmb, LPFC_MBOXQ_t, list);
2269 if (pmb == NULL)
2270 break;
2271
2272 pmbox = &pmb->u.mb;
2273
2274 if (pmbox->mbxCommand != MBX_HEARTBEAT) {
2275 if (pmb->vport) {
2276 lpfc_debugfs_disc_trc(pmb->vport,
2277 LPFC_DISC_TRC_MBOX_VPORT,
2278 "MBOX cmpl vport: cmd:x%x mb:x%x x%x",
2279 (uint32_t)pmbox->mbxCommand,
2280 pmbox->un.varWords[0],
2281 pmbox->un.varWords[1]);
2282 }
2283 else {
2284 lpfc_debugfs_disc_trc(phba->pport,
2285 LPFC_DISC_TRC_MBOX,
2286 "MBOX cmpl: cmd:x%x mb:x%x x%x",
2287 (uint32_t)pmbox->mbxCommand,
2288 pmbox->un.varWords[0],
2289 pmbox->un.varWords[1]);
2290 }
2291 }
2292
2293 /*
2294 * It is a fatal error if unknown mbox command completion.
2295 */
2296 if (lpfc_sli_chk_mbx_command(pmbox->mbxCommand) ==
2297 MBX_SHUTDOWN) {
2298 /* Unknown mailbox command compl */
2299 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
2300 "(%d):0323 Unknown Mailbox command "
2301 "x%x (x%x/x%x) Cmpl\n",
2302 pmb->vport ? pmb->vport->vpi : 0,
2303 pmbox->mbxCommand,
2304 lpfc_sli_config_mbox_subsys_get(phba,
2305 pmb),
2306 lpfc_sli_config_mbox_opcode_get(phba,
2307 pmb));
2308 phba->link_state = LPFC_HBA_ERROR;
2309 phba->work_hs = HS_FFER3;
2310 lpfc_handle_eratt(phba);
2311 continue;
2312 }
2313
2314 if (pmbox->mbxStatus) {
2315 phba->sli.slistat.mbox_stat_err++;
2316 if (pmbox->mbxStatus == MBXERR_NO_RESOURCES) {
2317 /* Mbox cmd cmpl error - RETRYing */
2318 lpfc_printf_log(phba, KERN_INFO,
2319 LOG_MBOX | LOG_SLI,
2320 "(%d):0305 Mbox cmd cmpl "
2321 "error - RETRYing Data: x%x "
2322 "(x%x/x%x) x%x x%x x%x\n",
2323 pmb->vport ? pmb->vport->vpi : 0,
2324 pmbox->mbxCommand,
2325 lpfc_sli_config_mbox_subsys_get(phba,
2326 pmb),
2327 lpfc_sli_config_mbox_opcode_get(phba,
2328 pmb),
2329 pmbox->mbxStatus,
2330 pmbox->un.varWords[0],
2331 pmb->vport->port_state);
2332 pmbox->mbxStatus = 0;
2333 pmbox->mbxOwner = OWN_HOST;
2334 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
2335 if (rc != MBX_NOT_FINISHED)
2336 continue;
2337 }
2338 }
2339
2340 /* Mailbox cmd <cmd> Cmpl <cmpl> */
2341 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
2342 "(%d):0307 Mailbox cmd x%x (x%x/x%x) Cmpl x%p "
2343 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x\n",
2344 pmb->vport ? pmb->vport->vpi : 0,
2345 pmbox->mbxCommand,
2346 lpfc_sli_config_mbox_subsys_get(phba, pmb),
2347 lpfc_sli_config_mbox_opcode_get(phba, pmb),
2348 pmb->mbox_cmpl,
2349 *((uint32_t *) pmbox),
2350 pmbox->un.varWords[0],
2351 pmbox->un.varWords[1],
2352 pmbox->un.varWords[2],
2353 pmbox->un.varWords[3],
2354 pmbox->un.varWords[4],
2355 pmbox->un.varWords[5],
2356 pmbox->un.varWords[6],
2357 pmbox->un.varWords[7]);
2358
2359 if (pmb->mbox_cmpl)
2360 pmb->mbox_cmpl(phba,pmb);
2361 } while (1);
2362 return 0;
2363 }
2364
2365 /**
2366 * lpfc_sli_get_buff - Get the buffer associated with the buffer tag
2367 * @phba: Pointer to HBA context object.
2368 * @pring: Pointer to driver SLI ring object.
2369 * @tag: buffer tag.
2370 *
2371 * This function is called with no lock held. When QUE_BUFTAG_BIT bit
2372 * is set in the tag the buffer is posted for a particular exchange,
2373 * the function will return the buffer without replacing the buffer.
2374 * If the buffer is for unsolicited ELS or CT traffic, this function
2375 * returns the buffer and also posts another buffer to the firmware.
2376 **/
2377 static struct lpfc_dmabuf *
2378 lpfc_sli_get_buff(struct lpfc_hba *phba,
2379 struct lpfc_sli_ring *pring,
2380 uint32_t tag)
2381 {
2382 struct hbq_dmabuf *hbq_entry;
2383
2384 if (tag & QUE_BUFTAG_BIT)
2385 return lpfc_sli_ring_taggedbuf_get(phba, pring, tag);
2386 hbq_entry = lpfc_sli_hbqbuf_find(phba, tag);
2387 if (!hbq_entry)
2388 return NULL;
2389 return &hbq_entry->dbuf;
2390 }
2391
2392 /**
2393 * lpfc_complete_unsol_iocb - Complete an unsolicited sequence
2394 * @phba: Pointer to HBA context object.
2395 * @pring: Pointer to driver SLI ring object.
2396 * @saveq: Pointer to the iocbq struct representing the sequence starting frame.
2397 * @fch_r_ctl: the r_ctl for the first frame of the sequence.
2398 * @fch_type: the type for the first frame of the sequence.
2399 *
2400 * This function is called with no lock held. This function uses the r_ctl and
2401 * type of the received sequence to find the correct callback function to call
2402 * to process the sequence.
2403 **/
2404 static int
2405 lpfc_complete_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2406 struct lpfc_iocbq *saveq, uint32_t fch_r_ctl,
2407 uint32_t fch_type)
2408 {
2409 int i;
2410
2411 /* unSolicited Responses */
2412 if (pring->prt[0].profile) {
2413 if (pring->prt[0].lpfc_sli_rcv_unsol_event)
2414 (pring->prt[0].lpfc_sli_rcv_unsol_event) (phba, pring,
2415 saveq);
2416 return 1;
2417 }
2418 /* We must search, based on rctl / type
2419 for the right routine */
2420 for (i = 0; i < pring->num_mask; i++) {
2421 if ((pring->prt[i].rctl == fch_r_ctl) &&
2422 (pring->prt[i].type == fch_type)) {
2423 if (pring->prt[i].lpfc_sli_rcv_unsol_event)
2424 (pring->prt[i].lpfc_sli_rcv_unsol_event)
2425 (phba, pring, saveq);
2426 return 1;
2427 }
2428 }
2429 return 0;
2430 }
2431
2432 /**
2433 * lpfc_sli_process_unsol_iocb - Unsolicited iocb handler
2434 * @phba: Pointer to HBA context object.
2435 * @pring: Pointer to driver SLI ring object.
2436 * @saveq: Pointer to the unsolicited iocb.
2437 *
2438 * This function is called with no lock held by the ring event handler
2439 * when there is an unsolicited iocb posted to the response ring by the
2440 * firmware. This function gets the buffer associated with the iocbs
2441 * and calls the event handler for the ring. This function handles both
2442 * qring buffers and hbq buffers.
2443 * When the function returns 1 the caller can free the iocb object otherwise
2444 * upper layer functions will free the iocb objects.
2445 **/
2446 static int
2447 lpfc_sli_process_unsol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2448 struct lpfc_iocbq *saveq)
2449 {
2450 IOCB_t * irsp;
2451 WORD5 * w5p;
2452 uint32_t Rctl, Type;
2453 uint32_t match;
2454 struct lpfc_iocbq *iocbq;
2455 struct lpfc_dmabuf *dmzbuf;
2456
2457 match = 0;
2458 irsp = &(saveq->iocb);
2459
2460 if (irsp->ulpCommand == CMD_ASYNC_STATUS) {
2461 if (pring->lpfc_sli_rcv_async_status)
2462 pring->lpfc_sli_rcv_async_status(phba, pring, saveq);
2463 else
2464 lpfc_printf_log(phba,
2465 KERN_WARNING,
2466 LOG_SLI,
2467 "0316 Ring %d handler: unexpected "
2468 "ASYNC_STATUS iocb received evt_code "
2469 "0x%x\n",
2470 pring->ringno,
2471 irsp->un.asyncstat.evt_code);
2472 return 1;
2473 }
2474
2475 if ((irsp->ulpCommand == CMD_IOCB_RET_XRI64_CX) &&
2476 (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)) {
2477 if (irsp->ulpBdeCount > 0) {
2478 dmzbuf = lpfc_sli_get_buff(phba, pring,
2479 irsp->un.ulpWord[3]);
2480 lpfc_in_buf_free(phba, dmzbuf);
2481 }
2482
2483 if (irsp->ulpBdeCount > 1) {
2484 dmzbuf = lpfc_sli_get_buff(phba, pring,
2485 irsp->unsli3.sli3Words[3]);
2486 lpfc_in_buf_free(phba, dmzbuf);
2487 }
2488
2489 if (irsp->ulpBdeCount > 2) {
2490 dmzbuf = lpfc_sli_get_buff(phba, pring,
2491 irsp->unsli3.sli3Words[7]);
2492 lpfc_in_buf_free(phba, dmzbuf);
2493 }
2494
2495 return 1;
2496 }
2497
2498 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
2499 if (irsp->ulpBdeCount != 0) {
2500 saveq->context2 = lpfc_sli_get_buff(phba, pring,
2501 irsp->un.ulpWord[3]);
2502 if (!saveq->context2)
2503 lpfc_printf_log(phba,
2504 KERN_ERR,
2505 LOG_SLI,
2506 "0341 Ring %d Cannot find buffer for "
2507 "an unsolicited iocb. tag 0x%x\n",
2508 pring->ringno,
2509 irsp->un.ulpWord[3]);
2510 }
2511 if (irsp->ulpBdeCount == 2) {
2512 saveq->context3 = lpfc_sli_get_buff(phba, pring,
2513 irsp->unsli3.sli3Words[7]);
2514 if (!saveq->context3)
2515 lpfc_printf_log(phba,
2516 KERN_ERR,
2517 LOG_SLI,
2518 "0342 Ring %d Cannot find buffer for an"
2519 " unsolicited iocb. tag 0x%x\n",
2520 pring->ringno,
2521 irsp->unsli3.sli3Words[7]);
2522 }
2523 list_for_each_entry(iocbq, &saveq->list, list) {
2524 irsp = &(iocbq->iocb);
2525 if (irsp->ulpBdeCount != 0) {
2526 iocbq->context2 = lpfc_sli_get_buff(phba, pring,
2527 irsp->un.ulpWord[3]);
2528 if (!iocbq->context2)
2529 lpfc_printf_log(phba,
2530 KERN_ERR,
2531 LOG_SLI,
2532 "0343 Ring %d Cannot find "
2533 "buffer for an unsolicited iocb"
2534 ". tag 0x%x\n", pring->ringno,
2535 irsp->un.ulpWord[3]);
2536 }
2537 if (irsp->ulpBdeCount == 2) {
2538 iocbq->context3 = lpfc_sli_get_buff(phba, pring,
2539 irsp->unsli3.sli3Words[7]);
2540 if (!iocbq->context3)
2541 lpfc_printf_log(phba,
2542 KERN_ERR,
2543 LOG_SLI,
2544 "0344 Ring %d Cannot find "
2545 "buffer for an unsolicited "
2546 "iocb. tag 0x%x\n",
2547 pring->ringno,
2548 irsp->unsli3.sli3Words[7]);
2549 }
2550 }
2551 }
2552 if (irsp->ulpBdeCount != 0 &&
2553 (irsp->ulpCommand == CMD_IOCB_RCV_CONT64_CX ||
2554 irsp->ulpStatus == IOSTAT_INTERMED_RSP)) {
2555 int found = 0;
2556
2557 /* search continue save q for same XRI */
2558 list_for_each_entry(iocbq, &pring->iocb_continue_saveq, clist) {
2559 if (iocbq->iocb.unsli3.rcvsli3.ox_id ==
2560 saveq->iocb.unsli3.rcvsli3.ox_id) {
2561 list_add_tail(&saveq->list, &iocbq->list);
2562 found = 1;
2563 break;
2564 }
2565 }
2566 if (!found)
2567 list_add_tail(&saveq->clist,
2568 &pring->iocb_continue_saveq);
2569 if (saveq->iocb.ulpStatus != IOSTAT_INTERMED_RSP) {
2570 list_del_init(&iocbq->clist);
2571 saveq = iocbq;
2572 irsp = &(saveq->iocb);
2573 } else
2574 return 0;
2575 }
2576 if ((irsp->ulpCommand == CMD_RCV_ELS_REQ64_CX) ||
2577 (irsp->ulpCommand == CMD_RCV_ELS_REQ_CX) ||
2578 (irsp->ulpCommand == CMD_IOCB_RCV_ELS64_CX)) {
2579 Rctl = FC_RCTL_ELS_REQ;
2580 Type = FC_TYPE_ELS;
2581 } else {
2582 w5p = (WORD5 *)&(saveq->iocb.un.ulpWord[5]);
2583 Rctl = w5p->hcsw.Rctl;
2584 Type = w5p->hcsw.Type;
2585
2586 /* Firmware Workaround */
2587 if ((Rctl == 0) && (pring->ringno == LPFC_ELS_RING) &&
2588 (irsp->ulpCommand == CMD_RCV_SEQUENCE64_CX ||
2589 irsp->ulpCommand == CMD_IOCB_RCV_SEQ64_CX)) {
2590 Rctl = FC_RCTL_ELS_REQ;
2591 Type = FC_TYPE_ELS;
2592 w5p->hcsw.Rctl = Rctl;
2593 w5p->hcsw.Type = Type;
2594 }
2595 }
2596
2597 if (!lpfc_complete_unsol_iocb(phba, pring, saveq, Rctl, Type))
2598 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2599 "0313 Ring %d handler: unexpected Rctl x%x "
2600 "Type x%x received\n",
2601 pring->ringno, Rctl, Type);
2602
2603 return 1;
2604 }
2605
2606 /**
2607 * lpfc_sli_iocbq_lookup - Find command iocb for the given response iocb
2608 * @phba: Pointer to HBA context object.
2609 * @pring: Pointer to driver SLI ring object.
2610 * @prspiocb: Pointer to response iocb object.
2611 *
2612 * This function looks up the iocb_lookup table to get the command iocb
2613 * corresponding to the given response iocb using the iotag of the
2614 * response iocb. This function is called with the hbalock held.
2615 * This function returns the command iocb object if it finds the command
2616 * iocb else returns NULL.
2617 **/
2618 static struct lpfc_iocbq *
2619 lpfc_sli_iocbq_lookup(struct lpfc_hba *phba,
2620 struct lpfc_sli_ring *pring,
2621 struct lpfc_iocbq *prspiocb)
2622 {
2623 struct lpfc_iocbq *cmd_iocb = NULL;
2624 uint16_t iotag;
2625
2626 iotag = prspiocb->iocb.ulpIoTag;
2627
2628 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2629 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2630 list_del_init(&cmd_iocb->list);
2631 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2632 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2633 }
2634 return cmd_iocb;
2635 }
2636
2637 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2638 "0317 iotag x%x is out off "
2639 "range: max iotag x%x wd0 x%x\n",
2640 iotag, phba->sli.last_iotag,
2641 *(((uint32_t *) &prspiocb->iocb) + 7));
2642 return NULL;
2643 }
2644
2645 /**
2646 * lpfc_sli_iocbq_lookup_by_tag - Find command iocb for the iotag
2647 * @phba: Pointer to HBA context object.
2648 * @pring: Pointer to driver SLI ring object.
2649 * @iotag: IOCB tag.
2650 *
2651 * This function looks up the iocb_lookup table to get the command iocb
2652 * corresponding to the given iotag. This function is called with the
2653 * hbalock held.
2654 * This function returns the command iocb object if it finds the command
2655 * iocb else returns NULL.
2656 **/
2657 static struct lpfc_iocbq *
2658 lpfc_sli_iocbq_lookup_by_tag(struct lpfc_hba *phba,
2659 struct lpfc_sli_ring *pring, uint16_t iotag)
2660 {
2661 struct lpfc_iocbq *cmd_iocb;
2662
2663 if (iotag != 0 && iotag <= phba->sli.last_iotag) {
2664 cmd_iocb = phba->sli.iocbq_lookup[iotag];
2665 if (cmd_iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ) {
2666 /* remove from txcmpl queue list */
2667 list_del_init(&cmd_iocb->list);
2668 cmd_iocb->iocb_flag &= ~LPFC_IO_ON_TXCMPLQ;
2669 return cmd_iocb;
2670 }
2671 }
2672 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2673 "0372 iotag x%x is out off range: max iotag (x%x)\n",
2674 iotag, phba->sli.last_iotag);
2675 return NULL;
2676 }
2677
2678 /**
2679 * lpfc_sli_process_sol_iocb - process solicited iocb completion
2680 * @phba: Pointer to HBA context object.
2681 * @pring: Pointer to driver SLI ring object.
2682 * @saveq: Pointer to the response iocb to be processed.
2683 *
2684 * This function is called by the ring event handler for non-fcp
2685 * rings when there is a new response iocb in the response ring.
2686 * The caller is not required to hold any locks. This function
2687 * gets the command iocb associated with the response iocb and
2688 * calls the completion handler for the command iocb. If there
2689 * is no completion handler, the function will free the resources
2690 * associated with command iocb. If the response iocb is for
2691 * an already aborted command iocb, the status of the completion
2692 * is changed to IOSTAT_LOCAL_REJECT/IOERR_SLI_ABORTED.
2693 * This function always returns 1.
2694 **/
2695 static int
2696 lpfc_sli_process_sol_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2697 struct lpfc_iocbq *saveq)
2698 {
2699 struct lpfc_iocbq *cmdiocbp;
2700 int rc = 1;
2701 unsigned long iflag;
2702
2703 /* Based on the iotag field, get the cmd IOCB from the txcmplq */
2704 spin_lock_irqsave(&phba->hbalock, iflag);
2705 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring, saveq);
2706 spin_unlock_irqrestore(&phba->hbalock, iflag);
2707
2708 if (cmdiocbp) {
2709 if (cmdiocbp->iocb_cmpl) {
2710 /*
2711 * If an ELS command failed send an event to mgmt
2712 * application.
2713 */
2714 if (saveq->iocb.ulpStatus &&
2715 (pring->ringno == LPFC_ELS_RING) &&
2716 (cmdiocbp->iocb.ulpCommand ==
2717 CMD_ELS_REQUEST64_CR))
2718 lpfc_send_els_failure_event(phba,
2719 cmdiocbp, saveq);
2720
2721 /*
2722 * Post all ELS completions to the worker thread.
2723 * All other are passed to the completion callback.
2724 */
2725 if (pring->ringno == LPFC_ELS_RING) {
2726 if ((phba->sli_rev < LPFC_SLI_REV4) &&
2727 (cmdiocbp->iocb_flag &
2728 LPFC_DRIVER_ABORTED)) {
2729 spin_lock_irqsave(&phba->hbalock,
2730 iflag);
2731 cmdiocbp->iocb_flag &=
2732 ~LPFC_DRIVER_ABORTED;
2733 spin_unlock_irqrestore(&phba->hbalock,
2734 iflag);
2735 saveq->iocb.ulpStatus =
2736 IOSTAT_LOCAL_REJECT;
2737 saveq->iocb.un.ulpWord[4] =
2738 IOERR_SLI_ABORTED;
2739
2740 /* Firmware could still be in progress
2741 * of DMAing payload, so don't free data
2742 * buffer till after a hbeat.
2743 */
2744 spin_lock_irqsave(&phba->hbalock,
2745 iflag);
2746 saveq->iocb_flag |= LPFC_DELAY_MEM_FREE;
2747 spin_unlock_irqrestore(&phba->hbalock,
2748 iflag);
2749 }
2750 if (phba->sli_rev == LPFC_SLI_REV4) {
2751 if (saveq->iocb_flag &
2752 LPFC_EXCHANGE_BUSY) {
2753 /* Set cmdiocb flag for the
2754 * exchange busy so sgl (xri)
2755 * will not be released until
2756 * the abort xri is received
2757 * from hba.
2758 */
2759 spin_lock_irqsave(
2760 &phba->hbalock, iflag);
2761 cmdiocbp->iocb_flag |=
2762 LPFC_EXCHANGE_BUSY;
2763 spin_unlock_irqrestore(
2764 &phba->hbalock, iflag);
2765 }
2766 if (cmdiocbp->iocb_flag &
2767 LPFC_DRIVER_ABORTED) {
2768 /*
2769 * Clear LPFC_DRIVER_ABORTED
2770 * bit in case it was driver
2771 * initiated abort.
2772 */
2773 spin_lock_irqsave(
2774 &phba->hbalock, iflag);
2775 cmdiocbp->iocb_flag &=
2776 ~LPFC_DRIVER_ABORTED;
2777 spin_unlock_irqrestore(
2778 &phba->hbalock, iflag);
2779 cmdiocbp->iocb.ulpStatus =
2780 IOSTAT_LOCAL_REJECT;
2781 cmdiocbp->iocb.un.ulpWord[4] =
2782 IOERR_ABORT_REQUESTED;
2783 /*
2784 * For SLI4, irsiocb contains
2785 * NO_XRI in sli_xritag, it
2786 * shall not affect releasing
2787 * sgl (xri) process.
2788 */
2789 saveq->iocb.ulpStatus =
2790 IOSTAT_LOCAL_REJECT;
2791 saveq->iocb.un.ulpWord[4] =
2792 IOERR_SLI_ABORTED;
2793 spin_lock_irqsave(
2794 &phba->hbalock, iflag);
2795 saveq->iocb_flag |=
2796 LPFC_DELAY_MEM_FREE;
2797 spin_unlock_irqrestore(
2798 &phba->hbalock, iflag);
2799 }
2800 }
2801 }
2802 (cmdiocbp->iocb_cmpl) (phba, cmdiocbp, saveq);
2803 } else
2804 lpfc_sli_release_iocbq(phba, cmdiocbp);
2805 } else {
2806 /*
2807 * Unknown initiating command based on the response iotag.
2808 * This could be the case on the ELS ring because of
2809 * lpfc_els_abort().
2810 */
2811 if (pring->ringno != LPFC_ELS_RING) {
2812 /*
2813 * Ring <ringno> handler: unexpected completion IoTag
2814 * <IoTag>
2815 */
2816 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
2817 "0322 Ring %d handler: "
2818 "unexpected completion IoTag x%x "
2819 "Data: x%x x%x x%x x%x\n",
2820 pring->ringno,
2821 saveq->iocb.ulpIoTag,
2822 saveq->iocb.ulpStatus,
2823 saveq->iocb.un.ulpWord[4],
2824 saveq->iocb.ulpCommand,
2825 saveq->iocb.ulpContext);
2826 }
2827 }
2828
2829 return rc;
2830 }
2831
2832 /**
2833 * lpfc_sli_rsp_pointers_error - Response ring pointer error handler
2834 * @phba: Pointer to HBA context object.
2835 * @pring: Pointer to driver SLI ring object.
2836 *
2837 * This function is called from the iocb ring event handlers when
2838 * put pointer is ahead of the get pointer for a ring. This function signal
2839 * an error attention condition to the worker thread and the worker
2840 * thread will transition the HBA to offline state.
2841 **/
2842 static void
2843 lpfc_sli_rsp_pointers_error(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
2844 {
2845 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2846 /*
2847 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
2848 * rsp ring <portRspMax>
2849 */
2850 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2851 "0312 Ring %d handler: portRspPut %d "
2852 "is bigger than rsp ring %d\n",
2853 pring->ringno, le32_to_cpu(pgp->rspPutInx),
2854 pring->sli.sli3.numRiocb);
2855
2856 phba->link_state = LPFC_HBA_ERROR;
2857
2858 /*
2859 * All error attention handlers are posted to
2860 * worker thread
2861 */
2862 phba->work_ha |= HA_ERATT;
2863 phba->work_hs = HS_FFER3;
2864
2865 lpfc_worker_wake_up(phba);
2866
2867 return;
2868 }
2869
2870 /**
2871 * lpfc_poll_eratt - Error attention polling timer timeout handler
2872 * @ptr: Pointer to address of HBA context object.
2873 *
2874 * This function is invoked by the Error Attention polling timer when the
2875 * timer times out. It will check the SLI Error Attention register for
2876 * possible attention events. If so, it will post an Error Attention event
2877 * and wake up worker thread to process it. Otherwise, it will set up the
2878 * Error Attention polling timer for the next poll.
2879 **/
2880 void lpfc_poll_eratt(unsigned long ptr)
2881 {
2882 struct lpfc_hba *phba;
2883 uint32_t eratt = 0, rem;
2884 uint64_t sli_intr, cnt;
2885
2886 phba = (struct lpfc_hba *)ptr;
2887
2888 /* Here we will also keep track of interrupts per sec of the hba */
2889 sli_intr = phba->sli.slistat.sli_intr;
2890
2891 if (phba->sli.slistat.sli_prev_intr > sli_intr)
2892 cnt = (((uint64_t)(-1) - phba->sli.slistat.sli_prev_intr) +
2893 sli_intr);
2894 else
2895 cnt = (sli_intr - phba->sli.slistat.sli_prev_intr);
2896
2897 /* 64-bit integer division not supporte on 32-bit x86 - use do_div */
2898 rem = do_div(cnt, LPFC_ERATT_POLL_INTERVAL);
2899 phba->sli.slistat.sli_ips = cnt;
2900
2901 phba->sli.slistat.sli_prev_intr = sli_intr;
2902
2903 /* Check chip HA register for error event */
2904 eratt = lpfc_sli_check_eratt(phba);
2905
2906 if (eratt)
2907 /* Tell the worker thread there is work to do */
2908 lpfc_worker_wake_up(phba);
2909 else
2910 /* Restart the timer for next eratt poll */
2911 mod_timer(&phba->eratt_poll, jiffies +
2912 HZ * LPFC_ERATT_POLL_INTERVAL);
2913 return;
2914 }
2915
2916
2917 /**
2918 * lpfc_sli_handle_fast_ring_event - Handle ring events on FCP ring
2919 * @phba: Pointer to HBA context object.
2920 * @pring: Pointer to driver SLI ring object.
2921 * @mask: Host attention register mask for this ring.
2922 *
2923 * This function is called from the interrupt context when there is a ring
2924 * event for the fcp ring. The caller does not hold any lock.
2925 * The function processes each response iocb in the response ring until it
2926 * finds an iocb with LE bit set and chains all the iocbs up to the iocb with
2927 * LE bit set. The function will call the completion handler of the command iocb
2928 * if the response iocb indicates a completion for a command iocb or it is
2929 * an abort completion. The function will call lpfc_sli_process_unsol_iocb
2930 * function if this is an unsolicited iocb.
2931 * This routine presumes LPFC_FCP_RING handling and doesn't bother
2932 * to check it explicitly.
2933 */
2934 int
2935 lpfc_sli_handle_fast_ring_event(struct lpfc_hba *phba,
2936 struct lpfc_sli_ring *pring, uint32_t mask)
2937 {
2938 struct lpfc_pgp *pgp = &phba->port_gp[pring->ringno];
2939 IOCB_t *irsp = NULL;
2940 IOCB_t *entry = NULL;
2941 struct lpfc_iocbq *cmdiocbq = NULL;
2942 struct lpfc_iocbq rspiocbq;
2943 uint32_t status;
2944 uint32_t portRspPut, portRspMax;
2945 int rc = 1;
2946 lpfc_iocb_type type;
2947 unsigned long iflag;
2948 uint32_t rsp_cmpl = 0;
2949
2950 spin_lock_irqsave(&phba->hbalock, iflag);
2951 pring->stats.iocb_event++;
2952
2953 /*
2954 * The next available response entry should never exceed the maximum
2955 * entries. If it does, treat it as an adapter hardware error.
2956 */
2957 portRspMax = pring->sli.sli3.numRiocb;
2958 portRspPut = le32_to_cpu(pgp->rspPutInx);
2959 if (unlikely(portRspPut >= portRspMax)) {
2960 lpfc_sli_rsp_pointers_error(phba, pring);
2961 spin_unlock_irqrestore(&phba->hbalock, iflag);
2962 return 1;
2963 }
2964 if (phba->fcp_ring_in_use) {
2965 spin_unlock_irqrestore(&phba->hbalock, iflag);
2966 return 1;
2967 } else
2968 phba->fcp_ring_in_use = 1;
2969
2970 rmb();
2971 while (pring->sli.sli3.rspidx != portRspPut) {
2972 /*
2973 * Fetch an entry off the ring and copy it into a local data
2974 * structure. The copy involves a byte-swap since the
2975 * network byte order and pci byte orders are different.
2976 */
2977 entry = lpfc_resp_iocb(phba, pring);
2978 phba->last_completion_time = jiffies;
2979
2980 if (++pring->sli.sli3.rspidx >= portRspMax)
2981 pring->sli.sli3.rspidx = 0;
2982
2983 lpfc_sli_pcimem_bcopy((uint32_t *) entry,
2984 (uint32_t *) &rspiocbq.iocb,
2985 phba->iocb_rsp_size);
2986 INIT_LIST_HEAD(&(rspiocbq.list));
2987 irsp = &rspiocbq.iocb;
2988
2989 type = lpfc_sli_iocb_cmd_type(irsp->ulpCommand & CMD_IOCB_MASK);
2990 pring->stats.iocb_rsp++;
2991 rsp_cmpl++;
2992
2993 if (unlikely(irsp->ulpStatus)) {
2994 /*
2995 * If resource errors reported from HBA, reduce
2996 * queuedepths of the SCSI device.
2997 */
2998 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
2999 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3000 IOERR_NO_RESOURCES)) {
3001 spin_unlock_irqrestore(&phba->hbalock, iflag);
3002 phba->lpfc_rampdown_queue_depth(phba);
3003 spin_lock_irqsave(&phba->hbalock, iflag);
3004 }
3005
3006 /* Rsp ring <ringno> error: IOCB */
3007 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3008 "0336 Rsp Ring %d error: IOCB Data: "
3009 "x%x x%x x%x x%x x%x x%x x%x x%x\n",
3010 pring->ringno,
3011 irsp->un.ulpWord[0],
3012 irsp->un.ulpWord[1],
3013 irsp->un.ulpWord[2],
3014 irsp->un.ulpWord[3],
3015 irsp->un.ulpWord[4],
3016 irsp->un.ulpWord[5],
3017 *(uint32_t *)&irsp->un1,
3018 *((uint32_t *)&irsp->un1 + 1));
3019 }
3020
3021 switch (type) {
3022 case LPFC_ABORT_IOCB:
3023 case LPFC_SOL_IOCB:
3024 /*
3025 * Idle exchange closed via ABTS from port. No iocb
3026 * resources need to be recovered.
3027 */
3028 if (unlikely(irsp->ulpCommand == CMD_XRI_ABORTED_CX)) {
3029 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3030 "0333 IOCB cmd 0x%x"
3031 " processed. Skipping"
3032 " completion\n",
3033 irsp->ulpCommand);
3034 break;
3035 }
3036
3037 cmdiocbq = lpfc_sli_iocbq_lookup(phba, pring,
3038 &rspiocbq);
3039 if (unlikely(!cmdiocbq))
3040 break;
3041 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED)
3042 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
3043 if (cmdiocbq->iocb_cmpl) {
3044 spin_unlock_irqrestore(&phba->hbalock, iflag);
3045 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq,
3046 &rspiocbq);
3047 spin_lock_irqsave(&phba->hbalock, iflag);
3048 }
3049 break;
3050 case LPFC_UNSOL_IOCB:
3051 spin_unlock_irqrestore(&phba->hbalock, iflag);
3052 lpfc_sli_process_unsol_iocb(phba, pring, &rspiocbq);
3053 spin_lock_irqsave(&phba->hbalock, iflag);
3054 break;
3055 default:
3056 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3057 char adaptermsg[LPFC_MAX_ADPTMSG];
3058 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3059 memcpy(&adaptermsg[0], (uint8_t *) irsp,
3060 MAX_MSG_DATA);
3061 dev_warn(&((phba->pcidev)->dev),
3062 "lpfc%d: %s\n",
3063 phba->brd_no, adaptermsg);
3064 } else {
3065 /* Unknown IOCB command */
3066 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3067 "0334 Unknown IOCB command "
3068 "Data: x%x, x%x x%x x%x x%x\n",
3069 type, irsp->ulpCommand,
3070 irsp->ulpStatus,
3071 irsp->ulpIoTag,
3072 irsp->ulpContext);
3073 }
3074 break;
3075 }
3076
3077 /*
3078 * The response IOCB has been processed. Update the ring
3079 * pointer in SLIM. If the port response put pointer has not
3080 * been updated, sync the pgp->rspPutInx and fetch the new port
3081 * response put pointer.
3082 */
3083 writel(pring->sli.sli3.rspidx,
3084 &phba->host_gp[pring->ringno].rspGetInx);
3085
3086 if (pring->sli.sli3.rspidx == portRspPut)
3087 portRspPut = le32_to_cpu(pgp->rspPutInx);
3088 }
3089
3090 if ((rsp_cmpl > 0) && (mask & HA_R0RE_REQ)) {
3091 pring->stats.iocb_rsp_full++;
3092 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3093 writel(status, phba->CAregaddr);
3094 readl(phba->CAregaddr);
3095 }
3096 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3097 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3098 pring->stats.iocb_cmd_empty++;
3099
3100 /* Force update of the local copy of cmdGetInx */
3101 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3102 lpfc_sli_resume_iocb(phba, pring);
3103
3104 if ((pring->lpfc_sli_cmd_available))
3105 (pring->lpfc_sli_cmd_available) (phba, pring);
3106
3107 }
3108
3109 phba->fcp_ring_in_use = 0;
3110 spin_unlock_irqrestore(&phba->hbalock, iflag);
3111 return rc;
3112 }
3113
3114 /**
3115 * lpfc_sli_sp_handle_rspiocb - Handle slow-path response iocb
3116 * @phba: Pointer to HBA context object.
3117 * @pring: Pointer to driver SLI ring object.
3118 * @rspiocbp: Pointer to driver response IOCB object.
3119 *
3120 * This function is called from the worker thread when there is a slow-path
3121 * response IOCB to process. This function chains all the response iocbs until
3122 * seeing the iocb with the LE bit set. The function will call
3123 * lpfc_sli_process_sol_iocb function if the response iocb indicates a
3124 * completion of a command iocb. The function will call the
3125 * lpfc_sli_process_unsol_iocb function if this is an unsolicited iocb.
3126 * The function frees the resources or calls the completion handler if this
3127 * iocb is an abort completion. The function returns NULL when the response
3128 * iocb has the LE bit set and all the chained iocbs are processed, otherwise
3129 * this function shall chain the iocb on to the iocb_continueq and return the
3130 * response iocb passed in.
3131 **/
3132 static struct lpfc_iocbq *
3133 lpfc_sli_sp_handle_rspiocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
3134 struct lpfc_iocbq *rspiocbp)
3135 {
3136 struct lpfc_iocbq *saveq;
3137 struct lpfc_iocbq *cmdiocbp;
3138 struct lpfc_iocbq *next_iocb;
3139 IOCB_t *irsp = NULL;
3140 uint32_t free_saveq;
3141 uint8_t iocb_cmd_type;
3142 lpfc_iocb_type type;
3143 unsigned long iflag;
3144 int rc;
3145
3146 spin_lock_irqsave(&phba->hbalock, iflag);
3147 /* First add the response iocb to the countinueq list */
3148 list_add_tail(&rspiocbp->list, &(pring->iocb_continueq));
3149 pring->iocb_continueq_cnt++;
3150
3151 /* Now, determine whether the list is completed for processing */
3152 irsp = &rspiocbp->iocb;
3153 if (irsp->ulpLe) {
3154 /*
3155 * By default, the driver expects to free all resources
3156 * associated with this iocb completion.
3157 */
3158 free_saveq = 1;
3159 saveq = list_get_first(&pring->iocb_continueq,
3160 struct lpfc_iocbq, list);
3161 irsp = &(saveq->iocb);
3162 list_del_init(&pring->iocb_continueq);
3163 pring->iocb_continueq_cnt = 0;
3164
3165 pring->stats.iocb_rsp++;
3166
3167 /*
3168 * If resource errors reported from HBA, reduce
3169 * queuedepths of the SCSI device.
3170 */
3171 if ((irsp->ulpStatus == IOSTAT_LOCAL_REJECT) &&
3172 ((irsp->un.ulpWord[4] & IOERR_PARAM_MASK) ==
3173 IOERR_NO_RESOURCES)) {
3174 spin_unlock_irqrestore(&phba->hbalock, iflag);
3175 phba->lpfc_rampdown_queue_depth(phba);
3176 spin_lock_irqsave(&phba->hbalock, iflag);
3177 }
3178
3179 if (irsp->ulpStatus) {
3180 /* Rsp ring <ringno> error: IOCB */
3181 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
3182 "0328 Rsp Ring %d error: "
3183 "IOCB Data: "
3184 "x%x x%x x%x x%x "
3185 "x%x x%x x%x x%x "
3186 "x%x x%x x%x x%x "
3187 "x%x x%x x%x x%x\n",
3188 pring->ringno,
3189 irsp->un.ulpWord[0],
3190 irsp->un.ulpWord[1],
3191 irsp->un.ulpWord[2],
3192 irsp->un.ulpWord[3],
3193 irsp->un.ulpWord[4],
3194 irsp->un.ulpWord[5],
3195 *(((uint32_t *) irsp) + 6),
3196 *(((uint32_t *) irsp) + 7),
3197 *(((uint32_t *) irsp) + 8),
3198 *(((uint32_t *) irsp) + 9),
3199 *(((uint32_t *) irsp) + 10),
3200 *(((uint32_t *) irsp) + 11),
3201 *(((uint32_t *) irsp) + 12),
3202 *(((uint32_t *) irsp) + 13),
3203 *(((uint32_t *) irsp) + 14),
3204 *(((uint32_t *) irsp) + 15));
3205 }
3206
3207 /*
3208 * Fetch the IOCB command type and call the correct completion
3209 * routine. Solicited and Unsolicited IOCBs on the ELS ring
3210 * get freed back to the lpfc_iocb_list by the discovery
3211 * kernel thread.
3212 */
3213 iocb_cmd_type = irsp->ulpCommand & CMD_IOCB_MASK;
3214 type = lpfc_sli_iocb_cmd_type(iocb_cmd_type);
3215 switch (type) {
3216 case LPFC_SOL_IOCB:
3217 spin_unlock_irqrestore(&phba->hbalock, iflag);
3218 rc = lpfc_sli_process_sol_iocb(phba, pring, saveq);
3219 spin_lock_irqsave(&phba->hbalock, iflag);
3220 break;
3221
3222 case LPFC_UNSOL_IOCB:
3223 spin_unlock_irqrestore(&phba->hbalock, iflag);
3224 rc = lpfc_sli_process_unsol_iocb(phba, pring, saveq);
3225 spin_lock_irqsave(&phba->hbalock, iflag);
3226 if (!rc)
3227 free_saveq = 0;
3228 break;
3229
3230 case LPFC_ABORT_IOCB:
3231 cmdiocbp = NULL;
3232 if (irsp->ulpCommand != CMD_XRI_ABORTED_CX)
3233 cmdiocbp = lpfc_sli_iocbq_lookup(phba, pring,
3234 saveq);
3235 if (cmdiocbp) {
3236 /* Call the specified completion routine */
3237 if (cmdiocbp->iocb_cmpl) {
3238 spin_unlock_irqrestore(&phba->hbalock,
3239 iflag);
3240 (cmdiocbp->iocb_cmpl)(phba, cmdiocbp,
3241 saveq);
3242 spin_lock_irqsave(&phba->hbalock,
3243 iflag);
3244 } else
3245 __lpfc_sli_release_iocbq(phba,
3246 cmdiocbp);
3247 }
3248 break;
3249
3250 case LPFC_UNKNOWN_IOCB:
3251 if (irsp->ulpCommand == CMD_ADAPTER_MSG) {
3252 char adaptermsg[LPFC_MAX_ADPTMSG];
3253 memset(adaptermsg, 0, LPFC_MAX_ADPTMSG);
3254 memcpy(&adaptermsg[0], (uint8_t *)irsp,
3255 MAX_MSG_DATA);
3256 dev_warn(&((phba->pcidev)->dev),
3257 "lpfc%d: %s\n",
3258 phba->brd_no, adaptermsg);
3259 } else {
3260 /* Unknown IOCB command */
3261 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3262 "0335 Unknown IOCB "
3263 "command Data: x%x "
3264 "x%x x%x x%x\n",
3265 irsp->ulpCommand,
3266 irsp->ulpStatus,
3267 irsp->ulpIoTag,
3268 irsp->ulpContext);
3269 }
3270 break;
3271 }
3272
3273 if (free_saveq) {
3274 list_for_each_entry_safe(rspiocbp, next_iocb,
3275 &saveq->list, list) {
3276 list_del(&rspiocbp->list);
3277 __lpfc_sli_release_iocbq(phba, rspiocbp);
3278 }
3279 __lpfc_sli_release_iocbq(phba, saveq);
3280 }
3281 rspiocbp = NULL;
3282 }
3283 spin_unlock_irqrestore(&phba->hbalock, iflag);
3284 return rspiocbp;
3285 }
3286
3287 /**
3288 * lpfc_sli_handle_slow_ring_event - Wrapper func for handling slow-path iocbs
3289 * @phba: Pointer to HBA context object.
3290 * @pring: Pointer to driver SLI ring object.
3291 * @mask: Host attention register mask for this ring.
3292 *
3293 * This routine wraps the actual slow_ring event process routine from the
3294 * API jump table function pointer from the lpfc_hba struct.
3295 **/
3296 void
3297 lpfc_sli_handle_slow_ring_event(struct lpfc_hba *phba,
3298 struct lpfc_sli_ring *pring, uint32_t mask)
3299 {
3300 phba->lpfc_sli_handle_slow_ring_event(phba, pring, mask);
3301 }
3302
3303 /**
3304 * lpfc_sli_handle_slow_ring_event_s3 - Handle SLI3 ring event for non-FCP rings
3305 * @phba: Pointer to HBA context object.
3306 * @pring: Pointer to driver SLI ring object.
3307 * @mask: Host attention register mask for this ring.
3308 *
3309 * This function is called from the worker thread when there is a ring event
3310 * for non-fcp rings. The caller does not hold any lock. The function will
3311 * remove each response iocb in the response ring and calls the handle
3312 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3313 **/
3314 static void
3315 lpfc_sli_handle_slow_ring_event_s3(struct lpfc_hba *phba,
3316 struct lpfc_sli_ring *pring, uint32_t mask)
3317 {
3318 struct lpfc_pgp *pgp;
3319 IOCB_t *entry;
3320 IOCB_t *irsp = NULL;
3321 struct lpfc_iocbq *rspiocbp = NULL;
3322 uint32_t portRspPut, portRspMax;
3323 unsigned long iflag;
3324 uint32_t status;
3325
3326 pgp = &phba->port_gp[pring->ringno];
3327 spin_lock_irqsave(&phba->hbalock, iflag);
3328 pring->stats.iocb_event++;
3329
3330 /*
3331 * The next available response entry should never exceed the maximum
3332 * entries. If it does, treat it as an adapter hardware error.
3333 */
3334 portRspMax = pring->sli.sli3.numRiocb;
3335 portRspPut = le32_to_cpu(pgp->rspPutInx);
3336 if (portRspPut >= portRspMax) {
3337 /*
3338 * Ring <ringno> handler: portRspPut <portRspPut> is bigger than
3339 * rsp ring <portRspMax>
3340 */
3341 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3342 "0303 Ring %d handler: portRspPut %d "
3343 "is bigger than rsp ring %d\n",
3344 pring->ringno, portRspPut, portRspMax);
3345
3346 phba->link_state = LPFC_HBA_ERROR;
3347 spin_unlock_irqrestore(&phba->hbalock, iflag);
3348
3349 phba->work_hs = HS_FFER3;
3350 lpfc_handle_eratt(phba);
3351
3352 return;
3353 }
3354
3355 rmb();
3356 while (pring->sli.sli3.rspidx != portRspPut) {
3357 /*
3358 * Build a completion list and call the appropriate handler.
3359 * The process is to get the next available response iocb, get
3360 * a free iocb from the list, copy the response data into the
3361 * free iocb, insert to the continuation list, and update the
3362 * next response index to slim. This process makes response
3363 * iocb's in the ring available to DMA as fast as possible but
3364 * pays a penalty for a copy operation. Since the iocb is
3365 * only 32 bytes, this penalty is considered small relative to
3366 * the PCI reads for register values and a slim write. When
3367 * the ulpLe field is set, the entire Command has been
3368 * received.
3369 */
3370 entry = lpfc_resp_iocb(phba, pring);
3371
3372 phba->last_completion_time = jiffies;
3373 rspiocbp = __lpfc_sli_get_iocbq(phba);
3374 if (rspiocbp == NULL) {
3375 printk(KERN_ERR "%s: out of buffers! Failing "
3376 "completion.\n", __func__);
3377 break;
3378 }
3379
3380 lpfc_sli_pcimem_bcopy(entry, &rspiocbp->iocb,
3381 phba->iocb_rsp_size);
3382 irsp = &rspiocbp->iocb;
3383
3384 if (++pring->sli.sli3.rspidx >= portRspMax)
3385 pring->sli.sli3.rspidx = 0;
3386
3387 if (pring->ringno == LPFC_ELS_RING) {
3388 lpfc_debugfs_slow_ring_trc(phba,
3389 "IOCB rsp ring: wd4:x%08x wd6:x%08x wd7:x%08x",
3390 *(((uint32_t *) irsp) + 4),
3391 *(((uint32_t *) irsp) + 6),
3392 *(((uint32_t *) irsp) + 7));
3393 }
3394
3395 writel(pring->sli.sli3.rspidx,
3396 &phba->host_gp[pring->ringno].rspGetInx);
3397
3398 spin_unlock_irqrestore(&phba->hbalock, iflag);
3399 /* Handle the response IOCB */
3400 rspiocbp = lpfc_sli_sp_handle_rspiocb(phba, pring, rspiocbp);
3401 spin_lock_irqsave(&phba->hbalock, iflag);
3402
3403 /*
3404 * If the port response put pointer has not been updated, sync
3405 * the pgp->rspPutInx in the MAILBOX_tand fetch the new port
3406 * response put pointer.
3407 */
3408 if (pring->sli.sli3.rspidx == portRspPut) {
3409 portRspPut = le32_to_cpu(pgp->rspPutInx);
3410 }
3411 } /* while (pring->sli.sli3.rspidx != portRspPut) */
3412
3413 if ((rspiocbp != NULL) && (mask & HA_R0RE_REQ)) {
3414 /* At least one response entry has been freed */
3415 pring->stats.iocb_rsp_full++;
3416 /* SET RxRE_RSP in Chip Att register */
3417 status = ((CA_R0ATT | CA_R0RE_RSP) << (pring->ringno * 4));
3418 writel(status, phba->CAregaddr);
3419 readl(phba->CAregaddr); /* flush */
3420 }
3421 if ((mask & HA_R0CE_RSP) && (pring->flag & LPFC_CALL_RING_AVAILABLE)) {
3422 pring->flag &= ~LPFC_CALL_RING_AVAILABLE;
3423 pring->stats.iocb_cmd_empty++;
3424
3425 /* Force update of the local copy of cmdGetInx */
3426 pring->sli.sli3.local_getidx = le32_to_cpu(pgp->cmdGetInx);
3427 lpfc_sli_resume_iocb(phba, pring);
3428
3429 if ((pring->lpfc_sli_cmd_available))
3430 (pring->lpfc_sli_cmd_available) (phba, pring);
3431
3432 }
3433
3434 spin_unlock_irqrestore(&phba->hbalock, iflag);
3435 return;
3436 }
3437
3438 /**
3439 * lpfc_sli_handle_slow_ring_event_s4 - Handle SLI4 slow-path els events
3440 * @phba: Pointer to HBA context object.
3441 * @pring: Pointer to driver SLI ring object.
3442 * @mask: Host attention register mask for this ring.
3443 *
3444 * This function is called from the worker thread when there is a pending
3445 * ELS response iocb on the driver internal slow-path response iocb worker
3446 * queue. The caller does not hold any lock. The function will remove each
3447 * response iocb from the response worker queue and calls the handle
3448 * response iocb routine (lpfc_sli_sp_handle_rspiocb) to process it.
3449 **/
3450 static void
3451 lpfc_sli_handle_slow_ring_event_s4(struct lpfc_hba *phba,
3452 struct lpfc_sli_ring *pring, uint32_t mask)
3453 {
3454 struct lpfc_iocbq *irspiocbq;
3455 struct hbq_dmabuf *dmabuf;
3456 struct lpfc_cq_event *cq_event;
3457 unsigned long iflag;
3458
3459 spin_lock_irqsave(&phba->hbalock, iflag);
3460 phba->hba_flag &= ~HBA_SP_QUEUE_EVT;
3461 spin_unlock_irqrestore(&phba->hbalock, iflag);
3462 while (!list_empty(&phba->sli4_hba.sp_queue_event)) {
3463 /* Get the response iocb from the head of work queue */
3464 spin_lock_irqsave(&phba->hbalock, iflag);
3465 list_remove_head(&phba->sli4_hba.sp_queue_event,
3466 cq_event, struct lpfc_cq_event, list);
3467 spin_unlock_irqrestore(&phba->hbalock, iflag);
3468
3469 switch (bf_get(lpfc_wcqe_c_code, &cq_event->cqe.wcqe_cmpl)) {
3470 case CQE_CODE_COMPL_WQE:
3471 irspiocbq = container_of(cq_event, struct lpfc_iocbq,
3472 cq_event);
3473 /* Translate ELS WCQE to response IOCBQ */
3474 irspiocbq = lpfc_sli4_els_wcqe_to_rspiocbq(phba,
3475 irspiocbq);
3476 if (irspiocbq)
3477 lpfc_sli_sp_handle_rspiocb(phba, pring,
3478 irspiocbq);
3479 break;
3480 case CQE_CODE_RECEIVE:
3481 case CQE_CODE_RECEIVE_V1:
3482 dmabuf = container_of(cq_event, struct hbq_dmabuf,
3483 cq_event);
3484 lpfc_sli4_handle_received_buffer(phba, dmabuf);
3485 break;
3486 default:
3487 break;
3488 }
3489 }
3490 }
3491
3492 /**
3493 * lpfc_sli_abort_iocb_ring - Abort all iocbs in the ring
3494 * @phba: Pointer to HBA context object.
3495 * @pring: Pointer to driver SLI ring object.
3496 *
3497 * This function aborts all iocbs in the given ring and frees all the iocb
3498 * objects in txq. This function issues an abort iocb for all the iocb commands
3499 * in txcmplq. The iocbs in the txcmplq is not guaranteed to complete before
3500 * the return of this function. The caller is not required to hold any locks.
3501 **/
3502 void
3503 lpfc_sli_abort_iocb_ring(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
3504 {
3505 LIST_HEAD(completions);
3506 struct lpfc_iocbq *iocb, *next_iocb;
3507
3508 if (pring->ringno == LPFC_ELS_RING) {
3509 lpfc_fabric_abort_hba(phba);
3510 }
3511
3512 /* Error everything on txq and txcmplq
3513 * First do the txq.
3514 */
3515 spin_lock_irq(&phba->hbalock);
3516 list_splice_init(&pring->txq, &completions);
3517
3518 /* Next issue ABTS for everything on the txcmplq */
3519 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
3520 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
3521
3522 spin_unlock_irq(&phba->hbalock);
3523
3524 /* Cancel all the IOCBs from the completions list */
3525 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
3526 IOERR_SLI_ABORTED);
3527 }
3528
3529 /**
3530 * lpfc_sli_flush_fcp_rings - flush all iocbs in the fcp ring
3531 * @phba: Pointer to HBA context object.
3532 *
3533 * This function flushes all iocbs in the fcp ring and frees all the iocb
3534 * objects in txq and txcmplq. This function will not issue abort iocbs
3535 * for all the iocb commands in txcmplq, they will just be returned with
3536 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
3537 * slot has been permanently disabled.
3538 **/
3539 void
3540 lpfc_sli_flush_fcp_rings(struct lpfc_hba *phba)
3541 {
3542 LIST_HEAD(txq);
3543 LIST_HEAD(txcmplq);
3544 struct lpfc_sli *psli = &phba->sli;
3545 struct lpfc_sli_ring *pring;
3546
3547 /* Currently, only one fcp ring */
3548 pring = &psli->ring[psli->fcp_ring];
3549
3550 spin_lock_irq(&phba->hbalock);
3551 /* Retrieve everything on txq */
3552 list_splice_init(&pring->txq, &txq);
3553
3554 /* Retrieve everything on the txcmplq */
3555 list_splice_init(&pring->txcmplq, &txcmplq);
3556
3557 /* Indicate the I/O queues are flushed */
3558 phba->hba_flag |= HBA_FCP_IOQ_FLUSH;
3559 spin_unlock_irq(&phba->hbalock);
3560
3561 /* Flush the txq */
3562 lpfc_sli_cancel_iocbs(phba, &txq, IOSTAT_LOCAL_REJECT,
3563 IOERR_SLI_DOWN);
3564
3565 /* Flush the txcmpq */
3566 lpfc_sli_cancel_iocbs(phba, &txcmplq, IOSTAT_LOCAL_REJECT,
3567 IOERR_SLI_DOWN);
3568 }
3569
3570 /**
3571 * lpfc_sli_brdready_s3 - Check for sli3 host ready status
3572 * @phba: Pointer to HBA context object.
3573 * @mask: Bit mask to be checked.
3574 *
3575 * This function reads the host status register and compares
3576 * with the provided bit mask to check if HBA completed
3577 * the restart. This function will wait in a loop for the
3578 * HBA to complete restart. If the HBA does not restart within
3579 * 15 iterations, the function will reset the HBA again. The
3580 * function returns 1 when HBA fail to restart otherwise returns
3581 * zero.
3582 **/
3583 static int
3584 lpfc_sli_brdready_s3(struct lpfc_hba *phba, uint32_t mask)
3585 {
3586 uint32_t status;
3587 int i = 0;
3588 int retval = 0;
3589
3590 /* Read the HBA Host Status Register */
3591 if (lpfc_readl(phba->HSregaddr, &status))
3592 return 1;
3593
3594 /*
3595 * Check status register every 100ms for 5 retries, then every
3596 * 500ms for 5, then every 2.5 sec for 5, then reset board and
3597 * every 2.5 sec for 4.
3598 * Break our of the loop if errors occurred during init.
3599 */
3600 while (((status & mask) != mask) &&
3601 !(status & HS_FFERM) &&
3602 i++ < 20) {
3603
3604 if (i <= 5)
3605 msleep(10);
3606 else if (i <= 10)
3607 msleep(500);
3608 else
3609 msleep(2500);
3610
3611 if (i == 15) {
3612 /* Do post */
3613 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3614 lpfc_sli_brdrestart(phba);
3615 }
3616 /* Read the HBA Host Status Register */
3617 if (lpfc_readl(phba->HSregaddr, &status)) {
3618 retval = 1;
3619 break;
3620 }
3621 }
3622
3623 /* Check to see if any errors occurred during init */
3624 if ((status & HS_FFERM) || (i >= 20)) {
3625 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3626 "2751 Adapter failed to restart, "
3627 "status reg x%x, FW Data: A8 x%x AC x%x\n",
3628 status,
3629 readl(phba->MBslimaddr + 0xa8),
3630 readl(phba->MBslimaddr + 0xac));
3631 phba->link_state = LPFC_HBA_ERROR;
3632 retval = 1;
3633 }
3634
3635 return retval;
3636 }
3637
3638 /**
3639 * lpfc_sli_brdready_s4 - Check for sli4 host ready status
3640 * @phba: Pointer to HBA context object.
3641 * @mask: Bit mask to be checked.
3642 *
3643 * This function checks the host status register to check if HBA is
3644 * ready. This function will wait in a loop for the HBA to be ready
3645 * If the HBA is not ready , the function will will reset the HBA PCI
3646 * function again. The function returns 1 when HBA fail to be ready
3647 * otherwise returns zero.
3648 **/
3649 static int
3650 lpfc_sli_brdready_s4(struct lpfc_hba *phba, uint32_t mask)
3651 {
3652 uint32_t status;
3653 int retval = 0;
3654
3655 /* Read the HBA Host Status Register */
3656 status = lpfc_sli4_post_status_check(phba);
3657
3658 if (status) {
3659 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
3660 lpfc_sli_brdrestart(phba);
3661 status = lpfc_sli4_post_status_check(phba);
3662 }
3663
3664 /* Check to see if any errors occurred during init */
3665 if (status) {
3666 phba->link_state = LPFC_HBA_ERROR;
3667 retval = 1;
3668 } else
3669 phba->sli4_hba.intr_enable = 0;
3670
3671 return retval;
3672 }
3673
3674 /**
3675 * lpfc_sli_brdready - Wrapper func for checking the hba readyness
3676 * @phba: Pointer to HBA context object.
3677 * @mask: Bit mask to be checked.
3678 *
3679 * This routine wraps the actual SLI3 or SLI4 hba readyness check routine
3680 * from the API jump table function pointer from the lpfc_hba struct.
3681 **/
3682 int
3683 lpfc_sli_brdready(struct lpfc_hba *phba, uint32_t mask)
3684 {
3685 return phba->lpfc_sli_brdready(phba, mask);
3686 }
3687
3688 #define BARRIER_TEST_PATTERN (0xdeadbeef)
3689
3690 /**
3691 * lpfc_reset_barrier - Make HBA ready for HBA reset
3692 * @phba: Pointer to HBA context object.
3693 *
3694 * This function is called before resetting an HBA. This function is called
3695 * with hbalock held and requests HBA to quiesce DMAs before a reset.
3696 **/
3697 void lpfc_reset_barrier(struct lpfc_hba *phba)
3698 {
3699 uint32_t __iomem *resp_buf;
3700 uint32_t __iomem *mbox_buf;
3701 volatile uint32_t mbox;
3702 uint32_t hc_copy, ha_copy, resp_data;
3703 int i;
3704 uint8_t hdrtype;
3705
3706 pci_read_config_byte(phba->pcidev, PCI_HEADER_TYPE, &hdrtype);
3707 if (hdrtype != 0x80 ||
3708 (FC_JEDEC_ID(phba->vpd.rev.biuRev) != HELIOS_JEDEC_ID &&
3709 FC_JEDEC_ID(phba->vpd.rev.biuRev) != THOR_JEDEC_ID))
3710 return;
3711
3712 /*
3713 * Tell the other part of the chip to suspend temporarily all
3714 * its DMA activity.
3715 */
3716 resp_buf = phba->MBslimaddr;
3717
3718 /* Disable the error attention */
3719 if (lpfc_readl(phba->HCregaddr, &hc_copy))
3720 return;
3721 writel((hc_copy & ~HC_ERINT_ENA), phba->HCregaddr);
3722 readl(phba->HCregaddr); /* flush */
3723 phba->link_flag |= LS_IGNORE_ERATT;
3724
3725 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3726 return;
3727 if (ha_copy & HA_ERATT) {
3728 /* Clear Chip error bit */
3729 writel(HA_ERATT, phba->HAregaddr);
3730 phba->pport->stopped = 1;
3731 }
3732
3733 mbox = 0;
3734 ((MAILBOX_t *)&mbox)->mbxCommand = MBX_KILL_BOARD;
3735 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_CHIP;
3736
3737 writel(BARRIER_TEST_PATTERN, (resp_buf + 1));
3738 mbox_buf = phba->MBslimaddr;
3739 writel(mbox, mbox_buf);
3740
3741 for (i = 0; i < 50; i++) {
3742 if (lpfc_readl((resp_buf + 1), &resp_data))
3743 return;
3744 if (resp_data != ~(BARRIER_TEST_PATTERN))
3745 mdelay(1);
3746 else
3747 break;
3748 }
3749 resp_data = 0;
3750 if (lpfc_readl((resp_buf + 1), &resp_data))
3751 return;
3752 if (resp_data != ~(BARRIER_TEST_PATTERN)) {
3753 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE ||
3754 phba->pport->stopped)
3755 goto restore_hc;
3756 else
3757 goto clear_errat;
3758 }
3759
3760 ((MAILBOX_t *)&mbox)->mbxOwner = OWN_HOST;
3761 resp_data = 0;
3762 for (i = 0; i < 500; i++) {
3763 if (lpfc_readl(resp_buf, &resp_data))
3764 return;
3765 if (resp_data != mbox)
3766 mdelay(1);
3767 else
3768 break;
3769 }
3770
3771 clear_errat:
3772
3773 while (++i < 500) {
3774 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3775 return;
3776 if (!(ha_copy & HA_ERATT))
3777 mdelay(1);
3778 else
3779 break;
3780 }
3781
3782 if (readl(phba->HAregaddr) & HA_ERATT) {
3783 writel(HA_ERATT, phba->HAregaddr);
3784 phba->pport->stopped = 1;
3785 }
3786
3787 restore_hc:
3788 phba->link_flag &= ~LS_IGNORE_ERATT;
3789 writel(hc_copy, phba->HCregaddr);
3790 readl(phba->HCregaddr); /* flush */
3791 }
3792
3793 /**
3794 * lpfc_sli_brdkill - Issue a kill_board mailbox command
3795 * @phba: Pointer to HBA context object.
3796 *
3797 * This function issues a kill_board mailbox command and waits for
3798 * the error attention interrupt. This function is called for stopping
3799 * the firmware processing. The caller is not required to hold any
3800 * locks. This function calls lpfc_hba_down_post function to free
3801 * any pending commands after the kill. The function will return 1 when it
3802 * fails to kill the board else will return 0.
3803 **/
3804 int
3805 lpfc_sli_brdkill(struct lpfc_hba *phba)
3806 {
3807 struct lpfc_sli *psli;
3808 LPFC_MBOXQ_t *pmb;
3809 uint32_t status;
3810 uint32_t ha_copy;
3811 int retval;
3812 int i = 0;
3813
3814 psli = &phba->sli;
3815
3816 /* Kill HBA */
3817 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3818 "0329 Kill HBA Data: x%x x%x\n",
3819 phba->pport->port_state, psli->sli_flag);
3820
3821 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3822 if (!pmb)
3823 return 1;
3824
3825 /* Disable the error attention */
3826 spin_lock_irq(&phba->hbalock);
3827 if (lpfc_readl(phba->HCregaddr, &status)) {
3828 spin_unlock_irq(&phba->hbalock);
3829 mempool_free(pmb, phba->mbox_mem_pool);
3830 return 1;
3831 }
3832 status &= ~HC_ERINT_ENA;
3833 writel(status, phba->HCregaddr);
3834 readl(phba->HCregaddr); /* flush */
3835 phba->link_flag |= LS_IGNORE_ERATT;
3836 spin_unlock_irq(&phba->hbalock);
3837
3838 lpfc_kill_board(phba, pmb);
3839 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
3840 retval = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3841
3842 if (retval != MBX_SUCCESS) {
3843 if (retval != MBX_BUSY)
3844 mempool_free(pmb, phba->mbox_mem_pool);
3845 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3846 "2752 KILL_BOARD command failed retval %d\n",
3847 retval);
3848 spin_lock_irq(&phba->hbalock);
3849 phba->link_flag &= ~LS_IGNORE_ERATT;
3850 spin_unlock_irq(&phba->hbalock);
3851 return 1;
3852 }
3853
3854 spin_lock_irq(&phba->hbalock);
3855 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
3856 spin_unlock_irq(&phba->hbalock);
3857
3858 mempool_free(pmb, phba->mbox_mem_pool);
3859
3860 /* There is no completion for a KILL_BOARD mbox cmd. Check for an error
3861 * attention every 100ms for 3 seconds. If we don't get ERATT after
3862 * 3 seconds we still set HBA_ERROR state because the status of the
3863 * board is now undefined.
3864 */
3865 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3866 return 1;
3867 while ((i++ < 30) && !(ha_copy & HA_ERATT)) {
3868 mdelay(100);
3869 if (lpfc_readl(phba->HAregaddr, &ha_copy))
3870 return 1;
3871 }
3872
3873 del_timer_sync(&psli->mbox_tmo);
3874 if (ha_copy & HA_ERATT) {
3875 writel(HA_ERATT, phba->HAregaddr);
3876 phba->pport->stopped = 1;
3877 }
3878 spin_lock_irq(&phba->hbalock);
3879 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
3880 psli->mbox_active = NULL;
3881 phba->link_flag &= ~LS_IGNORE_ERATT;
3882 spin_unlock_irq(&phba->hbalock);
3883
3884 lpfc_hba_down_post(phba);
3885 phba->link_state = LPFC_HBA_ERROR;
3886
3887 return ha_copy & HA_ERATT ? 0 : 1;
3888 }
3889
3890 /**
3891 * lpfc_sli_brdreset - Reset a sli-2 or sli-3 HBA
3892 * @phba: Pointer to HBA context object.
3893 *
3894 * This function resets the HBA by writing HC_INITFF to the control
3895 * register. After the HBA resets, this function resets all the iocb ring
3896 * indices. This function disables PCI layer parity checking during
3897 * the reset.
3898 * This function returns 0 always.
3899 * The caller is not required to hold any locks.
3900 **/
3901 int
3902 lpfc_sli_brdreset(struct lpfc_hba *phba)
3903 {
3904 struct lpfc_sli *psli;
3905 struct lpfc_sli_ring *pring;
3906 uint16_t cfg_value;
3907 int i;
3908
3909 psli = &phba->sli;
3910
3911 /* Reset HBA */
3912 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3913 "0325 Reset HBA Data: x%x x%x\n",
3914 phba->pport->port_state, psli->sli_flag);
3915
3916 /* perform board reset */
3917 phba->fc_eventTag = 0;
3918 phba->link_events = 0;
3919 phba->pport->fc_myDID = 0;
3920 phba->pport->fc_prevDID = 0;
3921
3922 /* Turn off parity checking and serr during the physical reset */
3923 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3924 pci_write_config_word(phba->pcidev, PCI_COMMAND,
3925 (cfg_value &
3926 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3927
3928 psli->sli_flag &= ~(LPFC_SLI_ACTIVE | LPFC_PROCESS_LA);
3929
3930 /* Now toggle INITFF bit in the Host Control Register */
3931 writel(HC_INITFF, phba->HCregaddr);
3932 mdelay(1);
3933 readl(phba->HCregaddr); /* flush */
3934 writel(0, phba->HCregaddr);
3935 readl(phba->HCregaddr); /* flush */
3936
3937 /* Restore PCI cmd register */
3938 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
3939
3940 /* Initialize relevant SLI info */
3941 for (i = 0; i < psli->num_rings; i++) {
3942 pring = &psli->ring[i];
3943 pring->flag = 0;
3944 pring->sli.sli3.rspidx = 0;
3945 pring->sli.sli3.next_cmdidx = 0;
3946 pring->sli.sli3.local_getidx = 0;
3947 pring->sli.sli3.cmdidx = 0;
3948 pring->missbufcnt = 0;
3949 }
3950
3951 phba->link_state = LPFC_WARM_START;
3952 return 0;
3953 }
3954
3955 /**
3956 * lpfc_sli4_brdreset - Reset a sli-4 HBA
3957 * @phba: Pointer to HBA context object.
3958 *
3959 * This function resets a SLI4 HBA. This function disables PCI layer parity
3960 * checking during resets the device. The caller is not required to hold
3961 * any locks.
3962 *
3963 * This function returns 0 always.
3964 **/
3965 int
3966 lpfc_sli4_brdreset(struct lpfc_hba *phba)
3967 {
3968 struct lpfc_sli *psli = &phba->sli;
3969 uint16_t cfg_value;
3970 int rc;
3971
3972 /* Reset HBA */
3973 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3974 "0295 Reset HBA Data: x%x x%x\n",
3975 phba->pport->port_state, psli->sli_flag);
3976
3977 /* perform board reset */
3978 phba->fc_eventTag = 0;
3979 phba->link_events = 0;
3980 phba->pport->fc_myDID = 0;
3981 phba->pport->fc_prevDID = 0;
3982
3983 spin_lock_irq(&phba->hbalock);
3984 psli->sli_flag &= ~(LPFC_PROCESS_LA);
3985 phba->fcf.fcf_flag = 0;
3986 spin_unlock_irq(&phba->hbalock);
3987
3988 /* Now physically reset the device */
3989 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3990 "0389 Performing PCI function reset!\n");
3991
3992 /* Turn off parity checking and serr during the physical reset */
3993 pci_read_config_word(phba->pcidev, PCI_COMMAND, &cfg_value);
3994 pci_write_config_word(phba->pcidev, PCI_COMMAND, (cfg_value &
3995 ~(PCI_COMMAND_PARITY | PCI_COMMAND_SERR)));
3996
3997 /* Perform FCoE PCI function reset before freeing queue memory */
3998 rc = lpfc_pci_function_reset(phba);
3999 lpfc_sli4_queue_destroy(phba);
4000
4001 /* Restore PCI cmd register */
4002 pci_write_config_word(phba->pcidev, PCI_COMMAND, cfg_value);
4003
4004 return rc;
4005 }
4006
4007 /**
4008 * lpfc_sli_brdrestart_s3 - Restart a sli-3 hba
4009 * @phba: Pointer to HBA context object.
4010 *
4011 * This function is called in the SLI initialization code path to
4012 * restart the HBA. The caller is not required to hold any lock.
4013 * This function writes MBX_RESTART mailbox command to the SLIM and
4014 * resets the HBA. At the end of the function, it calls lpfc_hba_down_post
4015 * function to free any pending commands. The function enables
4016 * POST only during the first initialization. The function returns zero.
4017 * The function does not guarantee completion of MBX_RESTART mailbox
4018 * command before the return of this function.
4019 **/
4020 static int
4021 lpfc_sli_brdrestart_s3(struct lpfc_hba *phba)
4022 {
4023 MAILBOX_t *mb;
4024 struct lpfc_sli *psli;
4025 volatile uint32_t word0;
4026 void __iomem *to_slim;
4027 uint32_t hba_aer_enabled;
4028
4029 spin_lock_irq(&phba->hbalock);
4030
4031 /* Take PCIe device Advanced Error Reporting (AER) state */
4032 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4033
4034 psli = &phba->sli;
4035
4036 /* Restart HBA */
4037 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4038 "0337 Restart HBA Data: x%x x%x\n",
4039 phba->pport->port_state, psli->sli_flag);
4040
4041 word0 = 0;
4042 mb = (MAILBOX_t *) &word0;
4043 mb->mbxCommand = MBX_RESTART;
4044 mb->mbxHc = 1;
4045
4046 lpfc_reset_barrier(phba);
4047
4048 to_slim = phba->MBslimaddr;
4049 writel(*(uint32_t *) mb, to_slim);
4050 readl(to_slim); /* flush */
4051
4052 /* Only skip post after fc_ffinit is completed */
4053 if (phba->pport->port_state)
4054 word0 = 1; /* This is really setting up word1 */
4055 else
4056 word0 = 0; /* This is really setting up word1 */
4057 to_slim = phba->MBslimaddr + sizeof (uint32_t);
4058 writel(*(uint32_t *) mb, to_slim);
4059 readl(to_slim); /* flush */
4060
4061 lpfc_sli_brdreset(phba);
4062 phba->pport->stopped = 0;
4063 phba->link_state = LPFC_INIT_START;
4064 phba->hba_flag = 0;
4065 spin_unlock_irq(&phba->hbalock);
4066
4067 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4068 psli->stats_start = get_seconds();
4069
4070 /* Give the INITFF and Post time to settle. */
4071 mdelay(100);
4072
4073 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4074 if (hba_aer_enabled)
4075 pci_disable_pcie_error_reporting(phba->pcidev);
4076
4077 lpfc_hba_down_post(phba);
4078
4079 return 0;
4080 }
4081
4082 /**
4083 * lpfc_sli_brdrestart_s4 - Restart the sli-4 hba
4084 * @phba: Pointer to HBA context object.
4085 *
4086 * This function is called in the SLI initialization code path to restart
4087 * a SLI4 HBA. The caller is not required to hold any lock.
4088 * At the end of the function, it calls lpfc_hba_down_post function to
4089 * free any pending commands.
4090 **/
4091 static int
4092 lpfc_sli_brdrestart_s4(struct lpfc_hba *phba)
4093 {
4094 struct lpfc_sli *psli = &phba->sli;
4095 uint32_t hba_aer_enabled;
4096 int rc;
4097
4098 /* Restart HBA */
4099 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4100 "0296 Restart HBA Data: x%x x%x\n",
4101 phba->pport->port_state, psli->sli_flag);
4102
4103 /* Take PCIe device Advanced Error Reporting (AER) state */
4104 hba_aer_enabled = phba->hba_flag & HBA_AER_ENABLED;
4105
4106 rc = lpfc_sli4_brdreset(phba);
4107
4108 spin_lock_irq(&phba->hbalock);
4109 phba->pport->stopped = 0;
4110 phba->link_state = LPFC_INIT_START;
4111 phba->hba_flag = 0;
4112 spin_unlock_irq(&phba->hbalock);
4113
4114 memset(&psli->lnk_stat_offsets, 0, sizeof(psli->lnk_stat_offsets));
4115 psli->stats_start = get_seconds();
4116
4117 /* Reset HBA AER if it was enabled, note hba_flag was reset above */
4118 if (hba_aer_enabled)
4119 pci_disable_pcie_error_reporting(phba->pcidev);
4120
4121 lpfc_hba_down_post(phba);
4122
4123 return rc;
4124 }
4125
4126 /**
4127 * lpfc_sli_brdrestart - Wrapper func for restarting hba
4128 * @phba: Pointer to HBA context object.
4129 *
4130 * This routine wraps the actual SLI3 or SLI4 hba restart routine from the
4131 * API jump table function pointer from the lpfc_hba struct.
4132 **/
4133 int
4134 lpfc_sli_brdrestart(struct lpfc_hba *phba)
4135 {
4136 return phba->lpfc_sli_brdrestart(phba);
4137 }
4138
4139 /**
4140 * lpfc_sli_chipset_init - Wait for the restart of the HBA after a restart
4141 * @phba: Pointer to HBA context object.
4142 *
4143 * This function is called after a HBA restart to wait for successful
4144 * restart of the HBA. Successful restart of the HBA is indicated by
4145 * HS_FFRDY and HS_MBRDY bits. If the HBA fails to restart even after 15
4146 * iteration, the function will restart the HBA again. The function returns
4147 * zero if HBA successfully restarted else returns negative error code.
4148 **/
4149 static int
4150 lpfc_sli_chipset_init(struct lpfc_hba *phba)
4151 {
4152 uint32_t status, i = 0;
4153
4154 /* Read the HBA Host Status Register */
4155 if (lpfc_readl(phba->HSregaddr, &status))
4156 return -EIO;
4157
4158 /* Check status register to see what current state is */
4159 i = 0;
4160 while ((status & (HS_FFRDY | HS_MBRDY)) != (HS_FFRDY | HS_MBRDY)) {
4161
4162 /* Check every 10ms for 10 retries, then every 100ms for 90
4163 * retries, then every 1 sec for 50 retires for a total of
4164 * ~60 seconds before reset the board again and check every
4165 * 1 sec for 50 retries. The up to 60 seconds before the
4166 * board ready is required by the Falcon FIPS zeroization
4167 * complete, and any reset the board in between shall cause
4168 * restart of zeroization, further delay the board ready.
4169 */
4170 if (i++ >= 200) {
4171 /* Adapter failed to init, timeout, status reg
4172 <status> */
4173 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4174 "0436 Adapter failed to init, "
4175 "timeout, status reg x%x, "
4176 "FW Data: A8 x%x AC x%x\n", status,
4177 readl(phba->MBslimaddr + 0xa8),
4178 readl(phba->MBslimaddr + 0xac));
4179 phba->link_state = LPFC_HBA_ERROR;
4180 return -ETIMEDOUT;
4181 }
4182
4183 /* Check to see if any errors occurred during init */
4184 if (status & HS_FFERM) {
4185 /* ERROR: During chipset initialization */
4186 /* Adapter failed to init, chipset, status reg
4187 <status> */
4188 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4189 "0437 Adapter failed to init, "
4190 "chipset, status reg x%x, "
4191 "FW Data: A8 x%x AC x%x\n", status,
4192 readl(phba->MBslimaddr + 0xa8),
4193 readl(phba->MBslimaddr + 0xac));
4194 phba->link_state = LPFC_HBA_ERROR;
4195 return -EIO;
4196 }
4197
4198 if (i <= 10)
4199 msleep(10);
4200 else if (i <= 100)
4201 msleep(100);
4202 else
4203 msleep(1000);
4204
4205 if (i == 150) {
4206 /* Do post */
4207 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4208 lpfc_sli_brdrestart(phba);
4209 }
4210 /* Read the HBA Host Status Register */
4211 if (lpfc_readl(phba->HSregaddr, &status))
4212 return -EIO;
4213 }
4214
4215 /* Check to see if any errors occurred during init */
4216 if (status & HS_FFERM) {
4217 /* ERROR: During chipset initialization */
4218 /* Adapter failed to init, chipset, status reg <status> */
4219 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4220 "0438 Adapter failed to init, chipset, "
4221 "status reg x%x, "
4222 "FW Data: A8 x%x AC x%x\n", status,
4223 readl(phba->MBslimaddr + 0xa8),
4224 readl(phba->MBslimaddr + 0xac));
4225 phba->link_state = LPFC_HBA_ERROR;
4226 return -EIO;
4227 }
4228
4229 /* Clear all interrupt enable conditions */
4230 writel(0, phba->HCregaddr);
4231 readl(phba->HCregaddr); /* flush */
4232
4233 /* setup host attn register */
4234 writel(0xffffffff, phba->HAregaddr);
4235 readl(phba->HAregaddr); /* flush */
4236 return 0;
4237 }
4238
4239 /**
4240 * lpfc_sli_hbq_count - Get the number of HBQs to be configured
4241 *
4242 * This function calculates and returns the number of HBQs required to be
4243 * configured.
4244 **/
4245 int
4246 lpfc_sli_hbq_count(void)
4247 {
4248 return ARRAY_SIZE(lpfc_hbq_defs);
4249 }
4250
4251 /**
4252 * lpfc_sli_hbq_entry_count - Calculate total number of hbq entries
4253 *
4254 * This function adds the number of hbq entries in every HBQ to get
4255 * the total number of hbq entries required for the HBA and returns
4256 * the total count.
4257 **/
4258 static int
4259 lpfc_sli_hbq_entry_count(void)
4260 {
4261 int hbq_count = lpfc_sli_hbq_count();
4262 int count = 0;
4263 int i;
4264
4265 for (i = 0; i < hbq_count; ++i)
4266 count += lpfc_hbq_defs[i]->entry_count;
4267 return count;
4268 }
4269
4270 /**
4271 * lpfc_sli_hbq_size - Calculate memory required for all hbq entries
4272 *
4273 * This function calculates amount of memory required for all hbq entries
4274 * to be configured and returns the total memory required.
4275 **/
4276 int
4277 lpfc_sli_hbq_size(void)
4278 {
4279 return lpfc_sli_hbq_entry_count() * sizeof(struct lpfc_hbq_entry);
4280 }
4281
4282 /**
4283 * lpfc_sli_hbq_setup - configure and initialize HBQs
4284 * @phba: Pointer to HBA context object.
4285 *
4286 * This function is called during the SLI initialization to configure
4287 * all the HBQs and post buffers to the HBQ. The caller is not
4288 * required to hold any locks. This function will return zero if successful
4289 * else it will return negative error code.
4290 **/
4291 static int
4292 lpfc_sli_hbq_setup(struct lpfc_hba *phba)
4293 {
4294 int hbq_count = lpfc_sli_hbq_count();
4295 LPFC_MBOXQ_t *pmb;
4296 MAILBOX_t *pmbox;
4297 uint32_t hbqno;
4298 uint32_t hbq_entry_index;
4299
4300 /* Get a Mailbox buffer to setup mailbox
4301 * commands for HBA initialization
4302 */
4303 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4304
4305 if (!pmb)
4306 return -ENOMEM;
4307
4308 pmbox = &pmb->u.mb;
4309
4310 /* Initialize the struct lpfc_sli_hbq structure for each hbq */
4311 phba->link_state = LPFC_INIT_MBX_CMDS;
4312 phba->hbq_in_use = 1;
4313
4314 hbq_entry_index = 0;
4315 for (hbqno = 0; hbqno < hbq_count; ++hbqno) {
4316 phba->hbqs[hbqno].next_hbqPutIdx = 0;
4317 phba->hbqs[hbqno].hbqPutIdx = 0;
4318 phba->hbqs[hbqno].local_hbqGetIdx = 0;
4319 phba->hbqs[hbqno].entry_count =
4320 lpfc_hbq_defs[hbqno]->entry_count;
4321 lpfc_config_hbq(phba, hbqno, lpfc_hbq_defs[hbqno],
4322 hbq_entry_index, pmb);
4323 hbq_entry_index += phba->hbqs[hbqno].entry_count;
4324
4325 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
4326 /* Adapter failed to init, mbxCmd <cmd> CFG_RING,
4327 mbxStatus <status>, ring <num> */
4328
4329 lpfc_printf_log(phba, KERN_ERR,
4330 LOG_SLI | LOG_VPORT,
4331 "1805 Adapter failed to init. "
4332 "Data: x%x x%x x%x\n",
4333 pmbox->mbxCommand,
4334 pmbox->mbxStatus, hbqno);
4335
4336 phba->link_state = LPFC_HBA_ERROR;
4337 mempool_free(pmb, phba->mbox_mem_pool);
4338 return -ENXIO;
4339 }
4340 }
4341 phba->hbq_count = hbq_count;
4342
4343 mempool_free(pmb, phba->mbox_mem_pool);
4344
4345 /* Initially populate or replenish the HBQs */
4346 for (hbqno = 0; hbqno < hbq_count; ++hbqno)
4347 lpfc_sli_hbqbuf_init_hbqs(phba, hbqno);
4348 return 0;
4349 }
4350
4351 /**
4352 * lpfc_sli4_rb_setup - Initialize and post RBs to HBA
4353 * @phba: Pointer to HBA context object.
4354 *
4355 * This function is called during the SLI initialization to configure
4356 * all the HBQs and post buffers to the HBQ. The caller is not
4357 * required to hold any locks. This function will return zero if successful
4358 * else it will return negative error code.
4359 **/
4360 static int
4361 lpfc_sli4_rb_setup(struct lpfc_hba *phba)
4362 {
4363 phba->hbq_in_use = 1;
4364 phba->hbqs[0].entry_count = lpfc_hbq_defs[0]->entry_count;
4365 phba->hbq_count = 1;
4366 /* Initially populate or replenish the HBQs */
4367 lpfc_sli_hbqbuf_init_hbqs(phba, 0);
4368 return 0;
4369 }
4370
4371 /**
4372 * lpfc_sli_config_port - Issue config port mailbox command
4373 * @phba: Pointer to HBA context object.
4374 * @sli_mode: sli mode - 2/3
4375 *
4376 * This function is called by the sli intialization code path
4377 * to issue config_port mailbox command. This function restarts the
4378 * HBA firmware and issues a config_port mailbox command to configure
4379 * the SLI interface in the sli mode specified by sli_mode
4380 * variable. The caller is not required to hold any locks.
4381 * The function returns 0 if successful, else returns negative error
4382 * code.
4383 **/
4384 int
4385 lpfc_sli_config_port(struct lpfc_hba *phba, int sli_mode)
4386 {
4387 LPFC_MBOXQ_t *pmb;
4388 uint32_t resetcount = 0, rc = 0, done = 0;
4389
4390 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4391 if (!pmb) {
4392 phba->link_state = LPFC_HBA_ERROR;
4393 return -ENOMEM;
4394 }
4395
4396 phba->sli_rev = sli_mode;
4397 while (resetcount < 2 && !done) {
4398 spin_lock_irq(&phba->hbalock);
4399 phba->sli.sli_flag |= LPFC_SLI_MBOX_ACTIVE;
4400 spin_unlock_irq(&phba->hbalock);
4401 phba->pport->port_state = LPFC_VPORT_UNKNOWN;
4402 lpfc_sli_brdrestart(phba);
4403 rc = lpfc_sli_chipset_init(phba);
4404 if (rc)
4405 break;
4406
4407 spin_lock_irq(&phba->hbalock);
4408 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
4409 spin_unlock_irq(&phba->hbalock);
4410 resetcount++;
4411
4412 /* Call pre CONFIG_PORT mailbox command initialization. A
4413 * value of 0 means the call was successful. Any other
4414 * nonzero value is a failure, but if ERESTART is returned,
4415 * the driver may reset the HBA and try again.
4416 */
4417 rc = lpfc_config_port_prep(phba);
4418 if (rc == -ERESTART) {
4419 phba->link_state = LPFC_LINK_UNKNOWN;
4420 continue;
4421 } else if (rc)
4422 break;
4423
4424 phba->link_state = LPFC_INIT_MBX_CMDS;
4425 lpfc_config_port(phba, pmb);
4426 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
4427 phba->sli3_options &= ~(LPFC_SLI3_NPIV_ENABLED |
4428 LPFC_SLI3_HBQ_ENABLED |
4429 LPFC_SLI3_CRP_ENABLED |
4430 LPFC_SLI3_BG_ENABLED |
4431 LPFC_SLI3_DSS_ENABLED);
4432 if (rc != MBX_SUCCESS) {
4433 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4434 "0442 Adapter failed to init, mbxCmd x%x "
4435 "CONFIG_PORT, mbxStatus x%x Data: x%x\n",
4436 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus, 0);
4437 spin_lock_irq(&phba->hbalock);
4438 phba->sli.sli_flag &= ~LPFC_SLI_ACTIVE;
4439 spin_unlock_irq(&phba->hbalock);
4440 rc = -ENXIO;
4441 } else {
4442 /* Allow asynchronous mailbox command to go through */
4443 spin_lock_irq(&phba->hbalock);
4444 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
4445 spin_unlock_irq(&phba->hbalock);
4446 done = 1;
4447
4448 if ((pmb->u.mb.un.varCfgPort.casabt == 1) &&
4449 (pmb->u.mb.un.varCfgPort.gasabt == 0))
4450 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4451 "3110 Port did not grant ASABT\n");
4452 }
4453 }
4454 if (!done) {
4455 rc = -EINVAL;
4456 goto do_prep_failed;
4457 }
4458 if (pmb->u.mb.un.varCfgPort.sli_mode == 3) {
4459 if (!pmb->u.mb.un.varCfgPort.cMA) {
4460 rc = -ENXIO;
4461 goto do_prep_failed;
4462 }
4463 if (phba->max_vpi && pmb->u.mb.un.varCfgPort.gmv) {
4464 phba->sli3_options |= LPFC_SLI3_NPIV_ENABLED;
4465 phba->max_vpi = pmb->u.mb.un.varCfgPort.max_vpi;
4466 phba->max_vports = (phba->max_vpi > phba->max_vports) ?
4467 phba->max_vpi : phba->max_vports;
4468
4469 } else
4470 phba->max_vpi = 0;
4471 phba->fips_level = 0;
4472 phba->fips_spec_rev = 0;
4473 if (pmb->u.mb.un.varCfgPort.gdss) {
4474 phba->sli3_options |= LPFC_SLI3_DSS_ENABLED;
4475 phba->fips_level = pmb->u.mb.un.varCfgPort.fips_level;
4476 phba->fips_spec_rev = pmb->u.mb.un.varCfgPort.fips_rev;
4477 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4478 "2850 Security Crypto Active. FIPS x%d "
4479 "(Spec Rev: x%d)",
4480 phba->fips_level, phba->fips_spec_rev);
4481 }
4482 if (pmb->u.mb.un.varCfgPort.sec_err) {
4483 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4484 "2856 Config Port Security Crypto "
4485 "Error: x%x ",
4486 pmb->u.mb.un.varCfgPort.sec_err);
4487 }
4488 if (pmb->u.mb.un.varCfgPort.gerbm)
4489 phba->sli3_options |= LPFC_SLI3_HBQ_ENABLED;
4490 if (pmb->u.mb.un.varCfgPort.gcrp)
4491 phba->sli3_options |= LPFC_SLI3_CRP_ENABLED;
4492
4493 phba->hbq_get = phba->mbox->us.s3_pgp.hbq_get;
4494 phba->port_gp = phba->mbox->us.s3_pgp.port;
4495
4496 if (phba->cfg_enable_bg) {
4497 if (pmb->u.mb.un.varCfgPort.gbg)
4498 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
4499 else
4500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4501 "0443 Adapter did not grant "
4502 "BlockGuard\n");
4503 }
4504 } else {
4505 phba->hbq_get = NULL;
4506 phba->port_gp = phba->mbox->us.s2.port;
4507 phba->max_vpi = 0;
4508 }
4509 do_prep_failed:
4510 mempool_free(pmb, phba->mbox_mem_pool);
4511 return rc;
4512 }
4513
4514
4515 /**
4516 * lpfc_sli_hba_setup - SLI intialization function
4517 * @phba: Pointer to HBA context object.
4518 *
4519 * This function is the main SLI intialization function. This function
4520 * is called by the HBA intialization code, HBA reset code and HBA
4521 * error attention handler code. Caller is not required to hold any
4522 * locks. This function issues config_port mailbox command to configure
4523 * the SLI, setup iocb rings and HBQ rings. In the end the function
4524 * calls the config_port_post function to issue init_link mailbox
4525 * command and to start the discovery. The function will return zero
4526 * if successful, else it will return negative error code.
4527 **/
4528 int
4529 lpfc_sli_hba_setup(struct lpfc_hba *phba)
4530 {
4531 uint32_t rc;
4532 int mode = 3, i;
4533 int longs;
4534
4535 switch (lpfc_sli_mode) {
4536 case 2:
4537 if (phba->cfg_enable_npiv) {
4538 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4539 "1824 NPIV enabled: Override lpfc_sli_mode "
4540 "parameter (%d) to auto (0).\n",
4541 lpfc_sli_mode);
4542 break;
4543 }
4544 mode = 2;
4545 break;
4546 case 0:
4547 case 3:
4548 break;
4549 default:
4550 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4551 "1819 Unrecognized lpfc_sli_mode "
4552 "parameter: %d.\n", lpfc_sli_mode);
4553
4554 break;
4555 }
4556
4557 rc = lpfc_sli_config_port(phba, mode);
4558
4559 if (rc && lpfc_sli_mode == 3)
4560 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_VPORT,
4561 "1820 Unable to select SLI-3. "
4562 "Not supported by adapter.\n");
4563 if (rc && mode != 2)
4564 rc = lpfc_sli_config_port(phba, 2);
4565 if (rc)
4566 goto lpfc_sli_hba_setup_error;
4567
4568 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
4569 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
4570 rc = pci_enable_pcie_error_reporting(phba->pcidev);
4571 if (!rc) {
4572 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4573 "2709 This device supports "
4574 "Advanced Error Reporting (AER)\n");
4575 spin_lock_irq(&phba->hbalock);
4576 phba->hba_flag |= HBA_AER_ENABLED;
4577 spin_unlock_irq(&phba->hbalock);
4578 } else {
4579 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4580 "2708 This device does not support "
4581 "Advanced Error Reporting (AER)\n");
4582 phba->cfg_aer_support = 0;
4583 }
4584 }
4585
4586 if (phba->sli_rev == 3) {
4587 phba->iocb_cmd_size = SLI3_IOCB_CMD_SIZE;
4588 phba->iocb_rsp_size = SLI3_IOCB_RSP_SIZE;
4589 } else {
4590 phba->iocb_cmd_size = SLI2_IOCB_CMD_SIZE;
4591 phba->iocb_rsp_size = SLI2_IOCB_RSP_SIZE;
4592 phba->sli3_options = 0;
4593 }
4594
4595 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4596 "0444 Firmware in SLI %x mode. Max_vpi %d\n",
4597 phba->sli_rev, phba->max_vpi);
4598 rc = lpfc_sli_ring_map(phba);
4599
4600 if (rc)
4601 goto lpfc_sli_hba_setup_error;
4602
4603 /* Initialize VPIs. */
4604 if (phba->sli_rev == LPFC_SLI_REV3) {
4605 /*
4606 * The VPI bitmask and physical ID array are allocated
4607 * and initialized once only - at driver load. A port
4608 * reset doesn't need to reinitialize this memory.
4609 */
4610 if ((phba->vpi_bmask == NULL) && (phba->vpi_ids == NULL)) {
4611 longs = (phba->max_vpi + BITS_PER_LONG) / BITS_PER_LONG;
4612 phba->vpi_bmask = kzalloc(longs * sizeof(unsigned long),
4613 GFP_KERNEL);
4614 if (!phba->vpi_bmask) {
4615 rc = -ENOMEM;
4616 goto lpfc_sli_hba_setup_error;
4617 }
4618
4619 phba->vpi_ids = kzalloc(
4620 (phba->max_vpi+1) * sizeof(uint16_t),
4621 GFP_KERNEL);
4622 if (!phba->vpi_ids) {
4623 kfree(phba->vpi_bmask);
4624 rc = -ENOMEM;
4625 goto lpfc_sli_hba_setup_error;
4626 }
4627 for (i = 0; i < phba->max_vpi; i++)
4628 phba->vpi_ids[i] = i;
4629 }
4630 }
4631
4632 /* Init HBQs */
4633 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED) {
4634 rc = lpfc_sli_hbq_setup(phba);
4635 if (rc)
4636 goto lpfc_sli_hba_setup_error;
4637 }
4638 spin_lock_irq(&phba->hbalock);
4639 phba->sli.sli_flag |= LPFC_PROCESS_LA;
4640 spin_unlock_irq(&phba->hbalock);
4641
4642 rc = lpfc_config_port_post(phba);
4643 if (rc)
4644 goto lpfc_sli_hba_setup_error;
4645
4646 return rc;
4647
4648 lpfc_sli_hba_setup_error:
4649 phba->link_state = LPFC_HBA_ERROR;
4650 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4651 "0445 Firmware initialization failed\n");
4652 return rc;
4653 }
4654
4655 /**
4656 * lpfc_sli4_read_fcoe_params - Read fcoe params from conf region
4657 * @phba: Pointer to HBA context object.
4658 * @mboxq: mailbox pointer.
4659 * This function issue a dump mailbox command to read config region
4660 * 23 and parse the records in the region and populate driver
4661 * data structure.
4662 **/
4663 static int
4664 lpfc_sli4_read_fcoe_params(struct lpfc_hba *phba)
4665 {
4666 LPFC_MBOXQ_t *mboxq;
4667 struct lpfc_dmabuf *mp;
4668 struct lpfc_mqe *mqe;
4669 uint32_t data_length;
4670 int rc;
4671
4672 /* Program the default value of vlan_id and fc_map */
4673 phba->valid_vlan = 0;
4674 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4675 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4676 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4677
4678 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4679 if (!mboxq)
4680 return -ENOMEM;
4681
4682 mqe = &mboxq->u.mqe;
4683 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq)) {
4684 rc = -ENOMEM;
4685 goto out_free_mboxq;
4686 }
4687
4688 mp = (struct lpfc_dmabuf *) mboxq->context1;
4689 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4690
4691 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
4692 "(%d):2571 Mailbox cmd x%x Status x%x "
4693 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4694 "x%x x%x x%x x%x x%x x%x x%x x%x x%x "
4695 "CQ: x%x x%x x%x x%x\n",
4696 mboxq->vport ? mboxq->vport->vpi : 0,
4697 bf_get(lpfc_mqe_command, mqe),
4698 bf_get(lpfc_mqe_status, mqe),
4699 mqe->un.mb_words[0], mqe->un.mb_words[1],
4700 mqe->un.mb_words[2], mqe->un.mb_words[3],
4701 mqe->un.mb_words[4], mqe->un.mb_words[5],
4702 mqe->un.mb_words[6], mqe->un.mb_words[7],
4703 mqe->un.mb_words[8], mqe->un.mb_words[9],
4704 mqe->un.mb_words[10], mqe->un.mb_words[11],
4705 mqe->un.mb_words[12], mqe->un.mb_words[13],
4706 mqe->un.mb_words[14], mqe->un.mb_words[15],
4707 mqe->un.mb_words[16], mqe->un.mb_words[50],
4708 mboxq->mcqe.word0,
4709 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
4710 mboxq->mcqe.trailer);
4711
4712 if (rc) {
4713 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4714 kfree(mp);
4715 rc = -EIO;
4716 goto out_free_mboxq;
4717 }
4718 data_length = mqe->un.mb_words[5];
4719 if (data_length > DMP_RGN23_SIZE) {
4720 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4721 kfree(mp);
4722 rc = -EIO;
4723 goto out_free_mboxq;
4724 }
4725
4726 lpfc_parse_fcoe_conf(phba, mp->virt, data_length);
4727 lpfc_mbuf_free(phba, mp->virt, mp->phys);
4728 kfree(mp);
4729 rc = 0;
4730
4731 out_free_mboxq:
4732 mempool_free(mboxq, phba->mbox_mem_pool);
4733 return rc;
4734 }
4735
4736 /**
4737 * lpfc_sli4_read_rev - Issue READ_REV and collect vpd data
4738 * @phba: pointer to lpfc hba data structure.
4739 * @mboxq: pointer to the LPFC_MBOXQ_t structure.
4740 * @vpd: pointer to the memory to hold resulting port vpd data.
4741 * @vpd_size: On input, the number of bytes allocated to @vpd.
4742 * On output, the number of data bytes in @vpd.
4743 *
4744 * This routine executes a READ_REV SLI4 mailbox command. In
4745 * addition, this routine gets the port vpd data.
4746 *
4747 * Return codes
4748 * 0 - successful
4749 * -ENOMEM - could not allocated memory.
4750 **/
4751 static int
4752 lpfc_sli4_read_rev(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
4753 uint8_t *vpd, uint32_t *vpd_size)
4754 {
4755 int rc = 0;
4756 uint32_t dma_size;
4757 struct lpfc_dmabuf *dmabuf;
4758 struct lpfc_mqe *mqe;
4759
4760 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
4761 if (!dmabuf)
4762 return -ENOMEM;
4763
4764 /*
4765 * Get a DMA buffer for the vpd data resulting from the READ_REV
4766 * mailbox command.
4767 */
4768 dma_size = *vpd_size;
4769 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
4770 dma_size,
4771 &dmabuf->phys,
4772 GFP_KERNEL);
4773 if (!dmabuf->virt) {
4774 kfree(dmabuf);
4775 return -ENOMEM;
4776 }
4777 memset(dmabuf->virt, 0, dma_size);
4778
4779 /*
4780 * The SLI4 implementation of READ_REV conflicts at word1,
4781 * bits 31:16 and SLI4 adds vpd functionality not present
4782 * in SLI3. This code corrects the conflicts.
4783 */
4784 lpfc_read_rev(phba, mboxq);
4785 mqe = &mboxq->u.mqe;
4786 mqe->un.read_rev.vpd_paddr_high = putPaddrHigh(dmabuf->phys);
4787 mqe->un.read_rev.vpd_paddr_low = putPaddrLow(dmabuf->phys);
4788 mqe->un.read_rev.word1 &= 0x0000FFFF;
4789 bf_set(lpfc_mbx_rd_rev_vpd, &mqe->un.read_rev, 1);
4790 bf_set(lpfc_mbx_rd_rev_avail_len, &mqe->un.read_rev, dma_size);
4791
4792 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4793 if (rc) {
4794 dma_free_coherent(&phba->pcidev->dev, dma_size,
4795 dmabuf->virt, dmabuf->phys);
4796 kfree(dmabuf);
4797 return -EIO;
4798 }
4799
4800 /*
4801 * The available vpd length cannot be bigger than the
4802 * DMA buffer passed to the port. Catch the less than
4803 * case and update the caller's size.
4804 */
4805 if (mqe->un.read_rev.avail_vpd_len < *vpd_size)
4806 *vpd_size = mqe->un.read_rev.avail_vpd_len;
4807
4808 memcpy(vpd, dmabuf->virt, *vpd_size);
4809
4810 dma_free_coherent(&phba->pcidev->dev, dma_size,
4811 dmabuf->virt, dmabuf->phys);
4812 kfree(dmabuf);
4813 return 0;
4814 }
4815
4816 /**
4817 * lpfc_sli4_retrieve_pport_name - Retrieve SLI4 device physical port name
4818 * @phba: pointer to lpfc hba data structure.
4819 *
4820 * This routine retrieves SLI4 device physical port name this PCI function
4821 * is attached to.
4822 *
4823 * Return codes
4824 * 0 - successful
4825 * otherwise - failed to retrieve physical port name
4826 **/
4827 static int
4828 lpfc_sli4_retrieve_pport_name(struct lpfc_hba *phba)
4829 {
4830 LPFC_MBOXQ_t *mboxq;
4831 struct lpfc_mbx_get_cntl_attributes *mbx_cntl_attr;
4832 struct lpfc_controller_attribute *cntl_attr;
4833 struct lpfc_mbx_get_port_name *get_port_name;
4834 void *virtaddr = NULL;
4835 uint32_t alloclen, reqlen;
4836 uint32_t shdr_status, shdr_add_status;
4837 union lpfc_sli4_cfg_shdr *shdr;
4838 char cport_name = 0;
4839 int rc;
4840
4841 /* We assume nothing at this point */
4842 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4843 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_NON;
4844
4845 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
4846 if (!mboxq)
4847 return -ENOMEM;
4848 /* obtain link type and link number via READ_CONFIG */
4849 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_INVAL;
4850 lpfc_sli4_read_config(phba);
4851 if (phba->sli4_hba.lnk_info.lnk_dv == LPFC_LNK_DAT_VAL)
4852 goto retrieve_ppname;
4853
4854 /* obtain link type and link number via COMMON_GET_CNTL_ATTRIBUTES */
4855 reqlen = sizeof(struct lpfc_mbx_get_cntl_attributes);
4856 alloclen = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4857 LPFC_MBOX_OPCODE_GET_CNTL_ATTRIBUTES, reqlen,
4858 LPFC_SLI4_MBX_NEMBED);
4859 if (alloclen < reqlen) {
4860 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4861 "3084 Allocated DMA memory size (%d) is "
4862 "less than the requested DMA memory size "
4863 "(%d)\n", alloclen, reqlen);
4864 rc = -ENOMEM;
4865 goto out_free_mboxq;
4866 }
4867 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4868 virtaddr = mboxq->sge_array->addr[0];
4869 mbx_cntl_attr = (struct lpfc_mbx_get_cntl_attributes *)virtaddr;
4870 shdr = &mbx_cntl_attr->cfg_shdr;
4871 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4872 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4873 if (shdr_status || shdr_add_status || rc) {
4874 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4875 "3085 Mailbox x%x (x%x/x%x) failed, "
4876 "rc:x%x, status:x%x, add_status:x%x\n",
4877 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4878 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4879 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4880 rc, shdr_status, shdr_add_status);
4881 rc = -ENXIO;
4882 goto out_free_mboxq;
4883 }
4884 cntl_attr = &mbx_cntl_attr->cntl_attr;
4885 phba->sli4_hba.lnk_info.lnk_dv = LPFC_LNK_DAT_VAL;
4886 phba->sli4_hba.lnk_info.lnk_tp =
4887 bf_get(lpfc_cntl_attr_lnk_type, cntl_attr);
4888 phba->sli4_hba.lnk_info.lnk_no =
4889 bf_get(lpfc_cntl_attr_lnk_numb, cntl_attr);
4890 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4891 "3086 lnk_type:%d, lnk_numb:%d\n",
4892 phba->sli4_hba.lnk_info.lnk_tp,
4893 phba->sli4_hba.lnk_info.lnk_no);
4894
4895 retrieve_ppname:
4896 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
4897 LPFC_MBOX_OPCODE_GET_PORT_NAME,
4898 sizeof(struct lpfc_mbx_get_port_name) -
4899 sizeof(struct lpfc_sli4_cfg_mhdr),
4900 LPFC_SLI4_MBX_EMBED);
4901 get_port_name = &mboxq->u.mqe.un.get_port_name;
4902 shdr = (union lpfc_sli4_cfg_shdr *)&get_port_name->header.cfg_shdr;
4903 bf_set(lpfc_mbox_hdr_version, &shdr->request, LPFC_OPCODE_VERSION_1);
4904 bf_set(lpfc_mbx_get_port_name_lnk_type, &get_port_name->u.request,
4905 phba->sli4_hba.lnk_info.lnk_tp);
4906 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4907 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
4908 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
4909 if (shdr_status || shdr_add_status || rc) {
4910 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
4911 "3087 Mailbox x%x (x%x/x%x) failed: "
4912 "rc:x%x, status:x%x, add_status:x%x\n",
4913 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
4914 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
4915 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
4916 rc, shdr_status, shdr_add_status);
4917 rc = -ENXIO;
4918 goto out_free_mboxq;
4919 }
4920 switch (phba->sli4_hba.lnk_info.lnk_no) {
4921 case LPFC_LINK_NUMBER_0:
4922 cport_name = bf_get(lpfc_mbx_get_port_name_name0,
4923 &get_port_name->u.response);
4924 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4925 break;
4926 case LPFC_LINK_NUMBER_1:
4927 cport_name = bf_get(lpfc_mbx_get_port_name_name1,
4928 &get_port_name->u.response);
4929 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4930 break;
4931 case LPFC_LINK_NUMBER_2:
4932 cport_name = bf_get(lpfc_mbx_get_port_name_name2,
4933 &get_port_name->u.response);
4934 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4935 break;
4936 case LPFC_LINK_NUMBER_3:
4937 cport_name = bf_get(lpfc_mbx_get_port_name_name3,
4938 &get_port_name->u.response);
4939 phba->sli4_hba.pport_name_sta = LPFC_SLI4_PPNAME_GET;
4940 break;
4941 default:
4942 break;
4943 }
4944
4945 if (phba->sli4_hba.pport_name_sta == LPFC_SLI4_PPNAME_GET) {
4946 phba->Port[0] = cport_name;
4947 phba->Port[1] = '\0';
4948 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4949 "3091 SLI get port name: %s\n", phba->Port);
4950 }
4951
4952 out_free_mboxq:
4953 if (rc != MBX_TIMEOUT) {
4954 if (bf_get(lpfc_mqe_command, &mboxq->u.mqe) == MBX_SLI4_CONFIG)
4955 lpfc_sli4_mbox_cmd_free(phba, mboxq);
4956 else
4957 mempool_free(mboxq, phba->mbox_mem_pool);
4958 }
4959 return rc;
4960 }
4961
4962 /**
4963 * lpfc_sli4_arm_cqeq_intr - Arm sli-4 device completion and event queues
4964 * @phba: pointer to lpfc hba data structure.
4965 *
4966 * This routine is called to explicitly arm the SLI4 device's completion and
4967 * event queues
4968 **/
4969 static void
4970 lpfc_sli4_arm_cqeq_intr(struct lpfc_hba *phba)
4971 {
4972 int fcp_eqidx;
4973
4974 lpfc_sli4_cq_release(phba->sli4_hba.mbx_cq, LPFC_QUEUE_REARM);
4975 lpfc_sli4_cq_release(phba->sli4_hba.els_cq, LPFC_QUEUE_REARM);
4976 fcp_eqidx = 0;
4977 if (phba->sli4_hba.fcp_cq) {
4978 do {
4979 lpfc_sli4_cq_release(phba->sli4_hba.fcp_cq[fcp_eqidx],
4980 LPFC_QUEUE_REARM);
4981 } while (++fcp_eqidx < phba->cfg_fcp_io_channel);
4982 }
4983 if (phba->sli4_hba.hba_eq) {
4984 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel;
4985 fcp_eqidx++)
4986 lpfc_sli4_eq_release(phba->sli4_hba.hba_eq[fcp_eqidx],
4987 LPFC_QUEUE_REARM);
4988 }
4989 }
4990
4991 /**
4992 * lpfc_sli4_get_avail_extnt_rsrc - Get available resource extent count.
4993 * @phba: Pointer to HBA context object.
4994 * @type: The resource extent type.
4995 * @extnt_count: buffer to hold port available extent count.
4996 * @extnt_size: buffer to hold element count per extent.
4997 *
4998 * This function calls the port and retrievs the number of available
4999 * extents and their size for a particular extent type.
5000 *
5001 * Returns: 0 if successful. Nonzero otherwise.
5002 **/
5003 int
5004 lpfc_sli4_get_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type,
5005 uint16_t *extnt_count, uint16_t *extnt_size)
5006 {
5007 int rc = 0;
5008 uint32_t length;
5009 uint32_t mbox_tmo;
5010 struct lpfc_mbx_get_rsrc_extent_info *rsrc_info;
5011 LPFC_MBOXQ_t *mbox;
5012
5013 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5014 if (!mbox)
5015 return -ENOMEM;
5016
5017 /* Find out how many extents are available for this resource type */
5018 length = (sizeof(struct lpfc_mbx_get_rsrc_extent_info) -
5019 sizeof(struct lpfc_sli4_cfg_mhdr));
5020 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5021 LPFC_MBOX_OPCODE_GET_RSRC_EXTENT_INFO,
5022 length, LPFC_SLI4_MBX_EMBED);
5023
5024 /* Send an extents count of 0 - the GET doesn't use it. */
5025 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5026 LPFC_SLI4_MBX_EMBED);
5027 if (unlikely(rc)) {
5028 rc = -EIO;
5029 goto err_exit;
5030 }
5031
5032 if (!phba->sli4_hba.intr_enable)
5033 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5034 else {
5035 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5036 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5037 }
5038 if (unlikely(rc)) {
5039 rc = -EIO;
5040 goto err_exit;
5041 }
5042
5043 rsrc_info = &mbox->u.mqe.un.rsrc_extent_info;
5044 if (bf_get(lpfc_mbox_hdr_status,
5045 &rsrc_info->header.cfg_shdr.response)) {
5046 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5047 "2930 Failed to get resource extents "
5048 "Status 0x%x Add'l Status 0x%x\n",
5049 bf_get(lpfc_mbox_hdr_status,
5050 &rsrc_info->header.cfg_shdr.response),
5051 bf_get(lpfc_mbox_hdr_add_status,
5052 &rsrc_info->header.cfg_shdr.response));
5053 rc = -EIO;
5054 goto err_exit;
5055 }
5056
5057 *extnt_count = bf_get(lpfc_mbx_get_rsrc_extent_info_cnt,
5058 &rsrc_info->u.rsp);
5059 *extnt_size = bf_get(lpfc_mbx_get_rsrc_extent_info_size,
5060 &rsrc_info->u.rsp);
5061
5062 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
5063 "3162 Retrieved extents type-%d from port: count:%d, "
5064 "size:%d\n", type, *extnt_count, *extnt_size);
5065
5066 err_exit:
5067 mempool_free(mbox, phba->mbox_mem_pool);
5068 return rc;
5069 }
5070
5071 /**
5072 * lpfc_sli4_chk_avail_extnt_rsrc - Check for available SLI4 resource extents.
5073 * @phba: Pointer to HBA context object.
5074 * @type: The extent type to check.
5075 *
5076 * This function reads the current available extents from the port and checks
5077 * if the extent count or extent size has changed since the last access.
5078 * Callers use this routine post port reset to understand if there is a
5079 * extent reprovisioning requirement.
5080 *
5081 * Returns:
5082 * -Error: error indicates problem.
5083 * 1: Extent count or size has changed.
5084 * 0: No changes.
5085 **/
5086 static int
5087 lpfc_sli4_chk_avail_extnt_rsrc(struct lpfc_hba *phba, uint16_t type)
5088 {
5089 uint16_t curr_ext_cnt, rsrc_ext_cnt;
5090 uint16_t size_diff, rsrc_ext_size;
5091 int rc = 0;
5092 struct lpfc_rsrc_blks *rsrc_entry;
5093 struct list_head *rsrc_blk_list = NULL;
5094
5095 size_diff = 0;
5096 curr_ext_cnt = 0;
5097 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5098 &rsrc_ext_cnt,
5099 &rsrc_ext_size);
5100 if (unlikely(rc))
5101 return -EIO;
5102
5103 switch (type) {
5104 case LPFC_RSC_TYPE_FCOE_RPI:
5105 rsrc_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5106 break;
5107 case LPFC_RSC_TYPE_FCOE_VPI:
5108 rsrc_blk_list = &phba->lpfc_vpi_blk_list;
5109 break;
5110 case LPFC_RSC_TYPE_FCOE_XRI:
5111 rsrc_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5112 break;
5113 case LPFC_RSC_TYPE_FCOE_VFI:
5114 rsrc_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5115 break;
5116 default:
5117 break;
5118 }
5119
5120 list_for_each_entry(rsrc_entry, rsrc_blk_list, list) {
5121 curr_ext_cnt++;
5122 if (rsrc_entry->rsrc_size != rsrc_ext_size)
5123 size_diff++;
5124 }
5125
5126 if (curr_ext_cnt != rsrc_ext_cnt || size_diff != 0)
5127 rc = 1;
5128
5129 return rc;
5130 }
5131
5132 /**
5133 * lpfc_sli4_cfg_post_extnts -
5134 * @phba: Pointer to HBA context object.
5135 * @extnt_cnt - number of available extents.
5136 * @type - the extent type (rpi, xri, vfi, vpi).
5137 * @emb - buffer to hold either MBX_EMBED or MBX_NEMBED operation.
5138 * @mbox - pointer to the caller's allocated mailbox structure.
5139 *
5140 * This function executes the extents allocation request. It also
5141 * takes care of the amount of memory needed to allocate or get the
5142 * allocated extents. It is the caller's responsibility to evaluate
5143 * the response.
5144 *
5145 * Returns:
5146 * -Error: Error value describes the condition found.
5147 * 0: if successful
5148 **/
5149 static int
5150 lpfc_sli4_cfg_post_extnts(struct lpfc_hba *phba, uint16_t extnt_cnt,
5151 uint16_t type, bool *emb, LPFC_MBOXQ_t *mbox)
5152 {
5153 int rc = 0;
5154 uint32_t req_len;
5155 uint32_t emb_len;
5156 uint32_t alloc_len, mbox_tmo;
5157
5158 /* Calculate the total requested length of the dma memory */
5159 req_len = extnt_cnt * sizeof(uint16_t);
5160
5161 /*
5162 * Calculate the size of an embedded mailbox. The uint32_t
5163 * accounts for extents-specific word.
5164 */
5165 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5166 sizeof(uint32_t);
5167
5168 /*
5169 * Presume the allocation and response will fit into an embedded
5170 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5171 */
5172 *emb = LPFC_SLI4_MBX_EMBED;
5173 if (req_len > emb_len) {
5174 req_len = extnt_cnt * sizeof(uint16_t) +
5175 sizeof(union lpfc_sli4_cfg_shdr) +
5176 sizeof(uint32_t);
5177 *emb = LPFC_SLI4_MBX_NEMBED;
5178 }
5179
5180 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5181 LPFC_MBOX_OPCODE_ALLOC_RSRC_EXTENT,
5182 req_len, *emb);
5183 if (alloc_len < req_len) {
5184 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5185 "2982 Allocated DMA memory size (x%x) is "
5186 "less than the requested DMA memory "
5187 "size (x%x)\n", alloc_len, req_len);
5188 return -ENOMEM;
5189 }
5190 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, extnt_cnt, type, *emb);
5191 if (unlikely(rc))
5192 return -EIO;
5193
5194 if (!phba->sli4_hba.intr_enable)
5195 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5196 else {
5197 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5198 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5199 }
5200
5201 if (unlikely(rc))
5202 rc = -EIO;
5203 return rc;
5204 }
5205
5206 /**
5207 * lpfc_sli4_alloc_extent - Allocate an SLI4 resource extent.
5208 * @phba: Pointer to HBA context object.
5209 * @type: The resource extent type to allocate.
5210 *
5211 * This function allocates the number of elements for the specified
5212 * resource type.
5213 **/
5214 static int
5215 lpfc_sli4_alloc_extent(struct lpfc_hba *phba, uint16_t type)
5216 {
5217 bool emb = false;
5218 uint16_t rsrc_id_cnt, rsrc_cnt, rsrc_size;
5219 uint16_t rsrc_id, rsrc_start, j, k;
5220 uint16_t *ids;
5221 int i, rc;
5222 unsigned long longs;
5223 unsigned long *bmask;
5224 struct lpfc_rsrc_blks *rsrc_blks;
5225 LPFC_MBOXQ_t *mbox;
5226 uint32_t length;
5227 struct lpfc_id_range *id_array = NULL;
5228 void *virtaddr = NULL;
5229 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5230 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5231 struct list_head *ext_blk_list;
5232
5233 rc = lpfc_sli4_get_avail_extnt_rsrc(phba, type,
5234 &rsrc_cnt,
5235 &rsrc_size);
5236 if (unlikely(rc))
5237 return -EIO;
5238
5239 if ((rsrc_cnt == 0) || (rsrc_size == 0)) {
5240 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5241 "3009 No available Resource Extents "
5242 "for resource type 0x%x: Count: 0x%x, "
5243 "Size 0x%x\n", type, rsrc_cnt,
5244 rsrc_size);
5245 return -ENOMEM;
5246 }
5247
5248 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_INIT | LOG_SLI,
5249 "2903 Post resource extents type-0x%x: "
5250 "count:%d, size %d\n", type, rsrc_cnt, rsrc_size);
5251
5252 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5253 if (!mbox)
5254 return -ENOMEM;
5255
5256 rc = lpfc_sli4_cfg_post_extnts(phba, rsrc_cnt, type, &emb, mbox);
5257 if (unlikely(rc)) {
5258 rc = -EIO;
5259 goto err_exit;
5260 }
5261
5262 /*
5263 * Figure out where the response is located. Then get local pointers
5264 * to the response data. The port does not guarantee to respond to
5265 * all extents counts request so update the local variable with the
5266 * allocated count from the port.
5267 */
5268 if (emb == LPFC_SLI4_MBX_EMBED) {
5269 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5270 id_array = &rsrc_ext->u.rsp.id[0];
5271 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5272 } else {
5273 virtaddr = mbox->sge_array->addr[0];
5274 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5275 rsrc_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5276 id_array = &n_rsrc->id;
5277 }
5278
5279 longs = ((rsrc_cnt * rsrc_size) + BITS_PER_LONG - 1) / BITS_PER_LONG;
5280 rsrc_id_cnt = rsrc_cnt * rsrc_size;
5281
5282 /*
5283 * Based on the resource size and count, correct the base and max
5284 * resource values.
5285 */
5286 length = sizeof(struct lpfc_rsrc_blks);
5287 switch (type) {
5288 case LPFC_RSC_TYPE_FCOE_RPI:
5289 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5290 sizeof(unsigned long),
5291 GFP_KERNEL);
5292 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5293 rc = -ENOMEM;
5294 goto err_exit;
5295 }
5296 phba->sli4_hba.rpi_ids = kzalloc(rsrc_id_cnt *
5297 sizeof(uint16_t),
5298 GFP_KERNEL);
5299 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5300 kfree(phba->sli4_hba.rpi_bmask);
5301 rc = -ENOMEM;
5302 goto err_exit;
5303 }
5304
5305 /*
5306 * The next_rpi was initialized with the maximum available
5307 * count but the port may allocate a smaller number. Catch
5308 * that case and update the next_rpi.
5309 */
5310 phba->sli4_hba.next_rpi = rsrc_id_cnt;
5311
5312 /* Initialize local ptrs for common extent processing later. */
5313 bmask = phba->sli4_hba.rpi_bmask;
5314 ids = phba->sli4_hba.rpi_ids;
5315 ext_blk_list = &phba->sli4_hba.lpfc_rpi_blk_list;
5316 break;
5317 case LPFC_RSC_TYPE_FCOE_VPI:
5318 phba->vpi_bmask = kzalloc(longs *
5319 sizeof(unsigned long),
5320 GFP_KERNEL);
5321 if (unlikely(!phba->vpi_bmask)) {
5322 rc = -ENOMEM;
5323 goto err_exit;
5324 }
5325 phba->vpi_ids = kzalloc(rsrc_id_cnt *
5326 sizeof(uint16_t),
5327 GFP_KERNEL);
5328 if (unlikely(!phba->vpi_ids)) {
5329 kfree(phba->vpi_bmask);
5330 rc = -ENOMEM;
5331 goto err_exit;
5332 }
5333
5334 /* Initialize local ptrs for common extent processing later. */
5335 bmask = phba->vpi_bmask;
5336 ids = phba->vpi_ids;
5337 ext_blk_list = &phba->lpfc_vpi_blk_list;
5338 break;
5339 case LPFC_RSC_TYPE_FCOE_XRI:
5340 phba->sli4_hba.xri_bmask = kzalloc(longs *
5341 sizeof(unsigned long),
5342 GFP_KERNEL);
5343 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5344 rc = -ENOMEM;
5345 goto err_exit;
5346 }
5347 phba->sli4_hba.max_cfg_param.xri_used = 0;
5348 phba->sli4_hba.xri_ids = kzalloc(rsrc_id_cnt *
5349 sizeof(uint16_t),
5350 GFP_KERNEL);
5351 if (unlikely(!phba->sli4_hba.xri_ids)) {
5352 kfree(phba->sli4_hba.xri_bmask);
5353 rc = -ENOMEM;
5354 goto err_exit;
5355 }
5356
5357 /* Initialize local ptrs for common extent processing later. */
5358 bmask = phba->sli4_hba.xri_bmask;
5359 ids = phba->sli4_hba.xri_ids;
5360 ext_blk_list = &phba->sli4_hba.lpfc_xri_blk_list;
5361 break;
5362 case LPFC_RSC_TYPE_FCOE_VFI:
5363 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5364 sizeof(unsigned long),
5365 GFP_KERNEL);
5366 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5367 rc = -ENOMEM;
5368 goto err_exit;
5369 }
5370 phba->sli4_hba.vfi_ids = kzalloc(rsrc_id_cnt *
5371 sizeof(uint16_t),
5372 GFP_KERNEL);
5373 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5374 kfree(phba->sli4_hba.vfi_bmask);
5375 rc = -ENOMEM;
5376 goto err_exit;
5377 }
5378
5379 /* Initialize local ptrs for common extent processing later. */
5380 bmask = phba->sli4_hba.vfi_bmask;
5381 ids = phba->sli4_hba.vfi_ids;
5382 ext_blk_list = &phba->sli4_hba.lpfc_vfi_blk_list;
5383 break;
5384 default:
5385 /* Unsupported Opcode. Fail call. */
5386 id_array = NULL;
5387 bmask = NULL;
5388 ids = NULL;
5389 ext_blk_list = NULL;
5390 goto err_exit;
5391 }
5392
5393 /*
5394 * Complete initializing the extent configuration with the
5395 * allocated ids assigned to this function. The bitmask serves
5396 * as an index into the array and manages the available ids. The
5397 * array just stores the ids communicated to the port via the wqes.
5398 */
5399 for (i = 0, j = 0, k = 0; i < rsrc_cnt; i++) {
5400 if ((i % 2) == 0)
5401 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_0,
5402 &id_array[k]);
5403 else
5404 rsrc_id = bf_get(lpfc_mbx_rsrc_id_word4_1,
5405 &id_array[k]);
5406
5407 rsrc_blks = kzalloc(length, GFP_KERNEL);
5408 if (unlikely(!rsrc_blks)) {
5409 rc = -ENOMEM;
5410 kfree(bmask);
5411 kfree(ids);
5412 goto err_exit;
5413 }
5414 rsrc_blks->rsrc_start = rsrc_id;
5415 rsrc_blks->rsrc_size = rsrc_size;
5416 list_add_tail(&rsrc_blks->list, ext_blk_list);
5417 rsrc_start = rsrc_id;
5418 if ((type == LPFC_RSC_TYPE_FCOE_XRI) && (j == 0))
5419 phba->sli4_hba.scsi_xri_start = rsrc_start +
5420 lpfc_sli4_get_els_iocb_cnt(phba);
5421
5422 while (rsrc_id < (rsrc_start + rsrc_size)) {
5423 ids[j] = rsrc_id;
5424 rsrc_id++;
5425 j++;
5426 }
5427 /* Entire word processed. Get next word.*/
5428 if ((i % 2) == 1)
5429 k++;
5430 }
5431 err_exit:
5432 lpfc_sli4_mbox_cmd_free(phba, mbox);
5433 return rc;
5434 }
5435
5436 /**
5437 * lpfc_sli4_dealloc_extent - Deallocate an SLI4 resource extent.
5438 * @phba: Pointer to HBA context object.
5439 * @type: the extent's type.
5440 *
5441 * This function deallocates all extents of a particular resource type.
5442 * SLI4 does not allow for deallocating a particular extent range. It
5443 * is the caller's responsibility to release all kernel memory resources.
5444 **/
5445 static int
5446 lpfc_sli4_dealloc_extent(struct lpfc_hba *phba, uint16_t type)
5447 {
5448 int rc;
5449 uint32_t length, mbox_tmo = 0;
5450 LPFC_MBOXQ_t *mbox;
5451 struct lpfc_mbx_dealloc_rsrc_extents *dealloc_rsrc;
5452 struct lpfc_rsrc_blks *rsrc_blk, *rsrc_blk_next;
5453
5454 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5455 if (!mbox)
5456 return -ENOMEM;
5457
5458 /*
5459 * This function sends an embedded mailbox because it only sends the
5460 * the resource type. All extents of this type are released by the
5461 * port.
5462 */
5463 length = (sizeof(struct lpfc_mbx_dealloc_rsrc_extents) -
5464 sizeof(struct lpfc_sli4_cfg_mhdr));
5465 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5466 LPFC_MBOX_OPCODE_DEALLOC_RSRC_EXTENT,
5467 length, LPFC_SLI4_MBX_EMBED);
5468
5469 /* Send an extents count of 0 - the dealloc doesn't use it. */
5470 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, 0, type,
5471 LPFC_SLI4_MBX_EMBED);
5472 if (unlikely(rc)) {
5473 rc = -EIO;
5474 goto out_free_mbox;
5475 }
5476 if (!phba->sli4_hba.intr_enable)
5477 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5478 else {
5479 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5480 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5481 }
5482 if (unlikely(rc)) {
5483 rc = -EIO;
5484 goto out_free_mbox;
5485 }
5486
5487 dealloc_rsrc = &mbox->u.mqe.un.dealloc_rsrc_extents;
5488 if (bf_get(lpfc_mbox_hdr_status,
5489 &dealloc_rsrc->header.cfg_shdr.response)) {
5490 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5491 "2919 Failed to release resource extents "
5492 "for type %d - Status 0x%x Add'l Status 0x%x. "
5493 "Resource memory not released.\n",
5494 type,
5495 bf_get(lpfc_mbox_hdr_status,
5496 &dealloc_rsrc->header.cfg_shdr.response),
5497 bf_get(lpfc_mbox_hdr_add_status,
5498 &dealloc_rsrc->header.cfg_shdr.response));
5499 rc = -EIO;
5500 goto out_free_mbox;
5501 }
5502
5503 /* Release kernel memory resources for the specific type. */
5504 switch (type) {
5505 case LPFC_RSC_TYPE_FCOE_VPI:
5506 kfree(phba->vpi_bmask);
5507 kfree(phba->vpi_ids);
5508 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5509 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5510 &phba->lpfc_vpi_blk_list, list) {
5511 list_del_init(&rsrc_blk->list);
5512 kfree(rsrc_blk);
5513 }
5514 break;
5515 case LPFC_RSC_TYPE_FCOE_XRI:
5516 kfree(phba->sli4_hba.xri_bmask);
5517 kfree(phba->sli4_hba.xri_ids);
5518 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5519 &phba->sli4_hba.lpfc_xri_blk_list, list) {
5520 list_del_init(&rsrc_blk->list);
5521 kfree(rsrc_blk);
5522 }
5523 break;
5524 case LPFC_RSC_TYPE_FCOE_VFI:
5525 kfree(phba->sli4_hba.vfi_bmask);
5526 kfree(phba->sli4_hba.vfi_ids);
5527 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5528 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5529 &phba->sli4_hba.lpfc_vfi_blk_list, list) {
5530 list_del_init(&rsrc_blk->list);
5531 kfree(rsrc_blk);
5532 }
5533 break;
5534 case LPFC_RSC_TYPE_FCOE_RPI:
5535 /* RPI bitmask and physical id array are cleaned up earlier. */
5536 list_for_each_entry_safe(rsrc_blk, rsrc_blk_next,
5537 &phba->sli4_hba.lpfc_rpi_blk_list, list) {
5538 list_del_init(&rsrc_blk->list);
5539 kfree(rsrc_blk);
5540 }
5541 break;
5542 default:
5543 break;
5544 }
5545
5546 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5547
5548 out_free_mbox:
5549 mempool_free(mbox, phba->mbox_mem_pool);
5550 return rc;
5551 }
5552
5553 /**
5554 * lpfc_sli4_alloc_resource_identifiers - Allocate all SLI4 resource extents.
5555 * @phba: Pointer to HBA context object.
5556 *
5557 * This function allocates all SLI4 resource identifiers.
5558 **/
5559 int
5560 lpfc_sli4_alloc_resource_identifiers(struct lpfc_hba *phba)
5561 {
5562 int i, rc, error = 0;
5563 uint16_t count, base;
5564 unsigned long longs;
5565
5566 if (!phba->sli4_hba.rpi_hdrs_in_use)
5567 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5568 if (phba->sli4_hba.extents_in_use) {
5569 /*
5570 * The port supports resource extents. The XRI, VPI, VFI, RPI
5571 * resource extent count must be read and allocated before
5572 * provisioning the resource id arrays.
5573 */
5574 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5575 LPFC_IDX_RSRC_RDY) {
5576 /*
5577 * Extent-based resources are set - the driver could
5578 * be in a port reset. Figure out if any corrective
5579 * actions need to be taken.
5580 */
5581 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5582 LPFC_RSC_TYPE_FCOE_VFI);
5583 if (rc != 0)
5584 error++;
5585 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5586 LPFC_RSC_TYPE_FCOE_VPI);
5587 if (rc != 0)
5588 error++;
5589 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5590 LPFC_RSC_TYPE_FCOE_XRI);
5591 if (rc != 0)
5592 error++;
5593 rc = lpfc_sli4_chk_avail_extnt_rsrc(phba,
5594 LPFC_RSC_TYPE_FCOE_RPI);
5595 if (rc != 0)
5596 error++;
5597
5598 /*
5599 * It's possible that the number of resources
5600 * provided to this port instance changed between
5601 * resets. Detect this condition and reallocate
5602 * resources. Otherwise, there is no action.
5603 */
5604 if (error) {
5605 lpfc_printf_log(phba, KERN_INFO,
5606 LOG_MBOX | LOG_INIT,
5607 "2931 Detected extent resource "
5608 "change. Reallocating all "
5609 "extents.\n");
5610 rc = lpfc_sli4_dealloc_extent(phba,
5611 LPFC_RSC_TYPE_FCOE_VFI);
5612 rc = lpfc_sli4_dealloc_extent(phba,
5613 LPFC_RSC_TYPE_FCOE_VPI);
5614 rc = lpfc_sli4_dealloc_extent(phba,
5615 LPFC_RSC_TYPE_FCOE_XRI);
5616 rc = lpfc_sli4_dealloc_extent(phba,
5617 LPFC_RSC_TYPE_FCOE_RPI);
5618 } else
5619 return 0;
5620 }
5621
5622 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5623 if (unlikely(rc))
5624 goto err_exit;
5625
5626 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5627 if (unlikely(rc))
5628 goto err_exit;
5629
5630 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5631 if (unlikely(rc))
5632 goto err_exit;
5633
5634 rc = lpfc_sli4_alloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5635 if (unlikely(rc))
5636 goto err_exit;
5637 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5638 LPFC_IDX_RSRC_RDY);
5639 return rc;
5640 } else {
5641 /*
5642 * The port does not support resource extents. The XRI, VPI,
5643 * VFI, RPI resource ids were determined from READ_CONFIG.
5644 * Just allocate the bitmasks and provision the resource id
5645 * arrays. If a port reset is active, the resources don't
5646 * need any action - just exit.
5647 */
5648 if (bf_get(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags) ==
5649 LPFC_IDX_RSRC_RDY) {
5650 lpfc_sli4_dealloc_resource_identifiers(phba);
5651 lpfc_sli4_remove_rpis(phba);
5652 }
5653 /* RPIs. */
5654 count = phba->sli4_hba.max_cfg_param.max_rpi;
5655 if (count <= 0) {
5656 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5657 "3279 Invalid provisioning of "
5658 "rpi:%d\n", count);
5659 rc = -EINVAL;
5660 goto err_exit;
5661 }
5662 base = phba->sli4_hba.max_cfg_param.rpi_base;
5663 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5664 phba->sli4_hba.rpi_bmask = kzalloc(longs *
5665 sizeof(unsigned long),
5666 GFP_KERNEL);
5667 if (unlikely(!phba->sli4_hba.rpi_bmask)) {
5668 rc = -ENOMEM;
5669 goto err_exit;
5670 }
5671 phba->sli4_hba.rpi_ids = kzalloc(count *
5672 sizeof(uint16_t),
5673 GFP_KERNEL);
5674 if (unlikely(!phba->sli4_hba.rpi_ids)) {
5675 rc = -ENOMEM;
5676 goto free_rpi_bmask;
5677 }
5678
5679 for (i = 0; i < count; i++)
5680 phba->sli4_hba.rpi_ids[i] = base + i;
5681
5682 /* VPIs. */
5683 count = phba->sli4_hba.max_cfg_param.max_vpi;
5684 if (count <= 0) {
5685 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5686 "3280 Invalid provisioning of "
5687 "vpi:%d\n", count);
5688 rc = -EINVAL;
5689 goto free_rpi_ids;
5690 }
5691 base = phba->sli4_hba.max_cfg_param.vpi_base;
5692 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5693 phba->vpi_bmask = kzalloc(longs *
5694 sizeof(unsigned long),
5695 GFP_KERNEL);
5696 if (unlikely(!phba->vpi_bmask)) {
5697 rc = -ENOMEM;
5698 goto free_rpi_ids;
5699 }
5700 phba->vpi_ids = kzalloc(count *
5701 sizeof(uint16_t),
5702 GFP_KERNEL);
5703 if (unlikely(!phba->vpi_ids)) {
5704 rc = -ENOMEM;
5705 goto free_vpi_bmask;
5706 }
5707
5708 for (i = 0; i < count; i++)
5709 phba->vpi_ids[i] = base + i;
5710
5711 /* XRIs. */
5712 count = phba->sli4_hba.max_cfg_param.max_xri;
5713 if (count <= 0) {
5714 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5715 "3281 Invalid provisioning of "
5716 "xri:%d\n", count);
5717 rc = -EINVAL;
5718 goto free_vpi_ids;
5719 }
5720 base = phba->sli4_hba.max_cfg_param.xri_base;
5721 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5722 phba->sli4_hba.xri_bmask = kzalloc(longs *
5723 sizeof(unsigned long),
5724 GFP_KERNEL);
5725 if (unlikely(!phba->sli4_hba.xri_bmask)) {
5726 rc = -ENOMEM;
5727 goto free_vpi_ids;
5728 }
5729 phba->sli4_hba.max_cfg_param.xri_used = 0;
5730 phba->sli4_hba.xri_ids = kzalloc(count *
5731 sizeof(uint16_t),
5732 GFP_KERNEL);
5733 if (unlikely(!phba->sli4_hba.xri_ids)) {
5734 rc = -ENOMEM;
5735 goto free_xri_bmask;
5736 }
5737
5738 for (i = 0; i < count; i++)
5739 phba->sli4_hba.xri_ids[i] = base + i;
5740
5741 /* VFIs. */
5742 count = phba->sli4_hba.max_cfg_param.max_vfi;
5743 if (count <= 0) {
5744 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5745 "3282 Invalid provisioning of "
5746 "vfi:%d\n", count);
5747 rc = -EINVAL;
5748 goto free_xri_ids;
5749 }
5750 base = phba->sli4_hba.max_cfg_param.vfi_base;
5751 longs = (count + BITS_PER_LONG - 1) / BITS_PER_LONG;
5752 phba->sli4_hba.vfi_bmask = kzalloc(longs *
5753 sizeof(unsigned long),
5754 GFP_KERNEL);
5755 if (unlikely(!phba->sli4_hba.vfi_bmask)) {
5756 rc = -ENOMEM;
5757 goto free_xri_ids;
5758 }
5759 phba->sli4_hba.vfi_ids = kzalloc(count *
5760 sizeof(uint16_t),
5761 GFP_KERNEL);
5762 if (unlikely(!phba->sli4_hba.vfi_ids)) {
5763 rc = -ENOMEM;
5764 goto free_vfi_bmask;
5765 }
5766
5767 for (i = 0; i < count; i++)
5768 phba->sli4_hba.vfi_ids[i] = base + i;
5769
5770 /*
5771 * Mark all resources ready. An HBA reset doesn't need
5772 * to reset the initialization.
5773 */
5774 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags,
5775 LPFC_IDX_RSRC_RDY);
5776 return 0;
5777 }
5778
5779 free_vfi_bmask:
5780 kfree(phba->sli4_hba.vfi_bmask);
5781 free_xri_ids:
5782 kfree(phba->sli4_hba.xri_ids);
5783 free_xri_bmask:
5784 kfree(phba->sli4_hba.xri_bmask);
5785 free_vpi_ids:
5786 kfree(phba->vpi_ids);
5787 free_vpi_bmask:
5788 kfree(phba->vpi_bmask);
5789 free_rpi_ids:
5790 kfree(phba->sli4_hba.rpi_ids);
5791 free_rpi_bmask:
5792 kfree(phba->sli4_hba.rpi_bmask);
5793 err_exit:
5794 return rc;
5795 }
5796
5797 /**
5798 * lpfc_sli4_dealloc_resource_identifiers - Deallocate all SLI4 resource extents.
5799 * @phba: Pointer to HBA context object.
5800 *
5801 * This function allocates the number of elements for the specified
5802 * resource type.
5803 **/
5804 int
5805 lpfc_sli4_dealloc_resource_identifiers(struct lpfc_hba *phba)
5806 {
5807 if (phba->sli4_hba.extents_in_use) {
5808 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VPI);
5809 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_RPI);
5810 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_XRI);
5811 lpfc_sli4_dealloc_extent(phba, LPFC_RSC_TYPE_FCOE_VFI);
5812 } else {
5813 kfree(phba->vpi_bmask);
5814 kfree(phba->vpi_ids);
5815 bf_set(lpfc_vpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5816 kfree(phba->sli4_hba.xri_bmask);
5817 kfree(phba->sli4_hba.xri_ids);
5818 kfree(phba->sli4_hba.vfi_bmask);
5819 kfree(phba->sli4_hba.vfi_ids);
5820 bf_set(lpfc_vfi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5821 bf_set(lpfc_idx_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
5822 }
5823
5824 return 0;
5825 }
5826
5827 /**
5828 * lpfc_sli4_get_allocated_extnts - Get the port's allocated extents.
5829 * @phba: Pointer to HBA context object.
5830 * @type: The resource extent type.
5831 * @extnt_count: buffer to hold port extent count response
5832 * @extnt_size: buffer to hold port extent size response.
5833 *
5834 * This function calls the port to read the host allocated extents
5835 * for a particular type.
5836 **/
5837 int
5838 lpfc_sli4_get_allocated_extnts(struct lpfc_hba *phba, uint16_t type,
5839 uint16_t *extnt_cnt, uint16_t *extnt_size)
5840 {
5841 bool emb;
5842 int rc = 0;
5843 uint16_t curr_blks = 0;
5844 uint32_t req_len, emb_len;
5845 uint32_t alloc_len, mbox_tmo;
5846 struct list_head *blk_list_head;
5847 struct lpfc_rsrc_blks *rsrc_blk;
5848 LPFC_MBOXQ_t *mbox;
5849 void *virtaddr = NULL;
5850 struct lpfc_mbx_nembed_rsrc_extent *n_rsrc;
5851 struct lpfc_mbx_alloc_rsrc_extents *rsrc_ext;
5852 union lpfc_sli4_cfg_shdr *shdr;
5853
5854 switch (type) {
5855 case LPFC_RSC_TYPE_FCOE_VPI:
5856 blk_list_head = &phba->lpfc_vpi_blk_list;
5857 break;
5858 case LPFC_RSC_TYPE_FCOE_XRI:
5859 blk_list_head = &phba->sli4_hba.lpfc_xri_blk_list;
5860 break;
5861 case LPFC_RSC_TYPE_FCOE_VFI:
5862 blk_list_head = &phba->sli4_hba.lpfc_vfi_blk_list;
5863 break;
5864 case LPFC_RSC_TYPE_FCOE_RPI:
5865 blk_list_head = &phba->sli4_hba.lpfc_rpi_blk_list;
5866 break;
5867 default:
5868 return -EIO;
5869 }
5870
5871 /* Count the number of extents currently allocatd for this type. */
5872 list_for_each_entry(rsrc_blk, blk_list_head, list) {
5873 if (curr_blks == 0) {
5874 /*
5875 * The GET_ALLOCATED mailbox does not return the size,
5876 * just the count. The size should be just the size
5877 * stored in the current allocated block and all sizes
5878 * for an extent type are the same so set the return
5879 * value now.
5880 */
5881 *extnt_size = rsrc_blk->rsrc_size;
5882 }
5883 curr_blks++;
5884 }
5885
5886 /* Calculate the total requested length of the dma memory. */
5887 req_len = curr_blks * sizeof(uint16_t);
5888
5889 /*
5890 * Calculate the size of an embedded mailbox. The uint32_t
5891 * accounts for extents-specific word.
5892 */
5893 emb_len = sizeof(MAILBOX_t) - sizeof(struct mbox_header) -
5894 sizeof(uint32_t);
5895
5896 /*
5897 * Presume the allocation and response will fit into an embedded
5898 * mailbox. If not true, reconfigure to a non-embedded mailbox.
5899 */
5900 emb = LPFC_SLI4_MBX_EMBED;
5901 req_len = emb_len;
5902 if (req_len > emb_len) {
5903 req_len = curr_blks * sizeof(uint16_t) +
5904 sizeof(union lpfc_sli4_cfg_shdr) +
5905 sizeof(uint32_t);
5906 emb = LPFC_SLI4_MBX_NEMBED;
5907 }
5908
5909 mbox = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5910 if (!mbox)
5911 return -ENOMEM;
5912 memset(mbox, 0, sizeof(LPFC_MBOXQ_t));
5913
5914 alloc_len = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
5915 LPFC_MBOX_OPCODE_GET_ALLOC_RSRC_EXTENT,
5916 req_len, emb);
5917 if (alloc_len < req_len) {
5918 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5919 "2983 Allocated DMA memory size (x%x) is "
5920 "less than the requested DMA memory "
5921 "size (x%x)\n", alloc_len, req_len);
5922 rc = -ENOMEM;
5923 goto err_exit;
5924 }
5925 rc = lpfc_sli4_mbox_rsrc_extent(phba, mbox, curr_blks, type, emb);
5926 if (unlikely(rc)) {
5927 rc = -EIO;
5928 goto err_exit;
5929 }
5930
5931 if (!phba->sli4_hba.intr_enable)
5932 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
5933 else {
5934 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
5935 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
5936 }
5937
5938 if (unlikely(rc)) {
5939 rc = -EIO;
5940 goto err_exit;
5941 }
5942
5943 /*
5944 * Figure out where the response is located. Then get local pointers
5945 * to the response data. The port does not guarantee to respond to
5946 * all extents counts request so update the local variable with the
5947 * allocated count from the port.
5948 */
5949 if (emb == LPFC_SLI4_MBX_EMBED) {
5950 rsrc_ext = &mbox->u.mqe.un.alloc_rsrc_extents;
5951 shdr = &rsrc_ext->header.cfg_shdr;
5952 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, &rsrc_ext->u.rsp);
5953 } else {
5954 virtaddr = mbox->sge_array->addr[0];
5955 n_rsrc = (struct lpfc_mbx_nembed_rsrc_extent *) virtaddr;
5956 shdr = &n_rsrc->cfg_shdr;
5957 *extnt_cnt = bf_get(lpfc_mbx_rsrc_cnt, n_rsrc);
5958 }
5959
5960 if (bf_get(lpfc_mbox_hdr_status, &shdr->response)) {
5961 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_INIT,
5962 "2984 Failed to read allocated resources "
5963 "for type %d - Status 0x%x Add'l Status 0x%x.\n",
5964 type,
5965 bf_get(lpfc_mbox_hdr_status, &shdr->response),
5966 bf_get(lpfc_mbox_hdr_add_status, &shdr->response));
5967 rc = -EIO;
5968 goto err_exit;
5969 }
5970 err_exit:
5971 lpfc_sli4_mbox_cmd_free(phba, mbox);
5972 return rc;
5973 }
5974
5975 /**
5976 * lpfc_sli4_repost_els_sgl_list - Repsot the els buffers sgl pages as block
5977 * @phba: pointer to lpfc hba data structure.
5978 *
5979 * This routine walks the list of els buffers that have been allocated and
5980 * repost them to the port by using SGL block post. This is needed after a
5981 * pci_function_reset/warm_start or start. It attempts to construct blocks
5982 * of els buffer sgls which contains contiguous xris and uses the non-embedded
5983 * SGL block post mailbox commands to post them to the port. For single els
5984 * buffer sgl with non-contiguous xri, if any, it shall use embedded SGL post
5985 * mailbox command for posting.
5986 *
5987 * Returns: 0 = success, non-zero failure.
5988 **/
5989 static int
5990 lpfc_sli4_repost_els_sgl_list(struct lpfc_hba *phba)
5991 {
5992 struct lpfc_sglq *sglq_entry = NULL;
5993 struct lpfc_sglq *sglq_entry_next = NULL;
5994 struct lpfc_sglq *sglq_entry_first = NULL;
5995 int status, post_cnt = 0, num_posted = 0, block_cnt = 0;
5996 int last_xritag = NO_XRI;
5997 LIST_HEAD(prep_sgl_list);
5998 LIST_HEAD(blck_sgl_list);
5999 LIST_HEAD(allc_sgl_list);
6000 LIST_HEAD(post_sgl_list);
6001 LIST_HEAD(free_sgl_list);
6002
6003 spin_lock_irq(&phba->hbalock);
6004 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &allc_sgl_list);
6005 spin_unlock_irq(&phba->hbalock);
6006
6007 list_for_each_entry_safe(sglq_entry, sglq_entry_next,
6008 &allc_sgl_list, list) {
6009 list_del_init(&sglq_entry->list);
6010 block_cnt++;
6011 if ((last_xritag != NO_XRI) &&
6012 (sglq_entry->sli4_xritag != last_xritag + 1)) {
6013 /* a hole in xri block, form a sgl posting block */
6014 list_splice_init(&prep_sgl_list, &blck_sgl_list);
6015 post_cnt = block_cnt - 1;
6016 /* prepare list for next posting block */
6017 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6018 block_cnt = 1;
6019 } else {
6020 /* prepare list for next posting block */
6021 list_add_tail(&sglq_entry->list, &prep_sgl_list);
6022 /* enough sgls for non-embed sgl mbox command */
6023 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
6024 list_splice_init(&prep_sgl_list,
6025 &blck_sgl_list);
6026 post_cnt = block_cnt;
6027 block_cnt = 0;
6028 }
6029 }
6030 num_posted++;
6031
6032 /* keep track of last sgl's xritag */
6033 last_xritag = sglq_entry->sli4_xritag;
6034
6035 /* end of repost sgl list condition for els buffers */
6036 if (num_posted == phba->sli4_hba.els_xri_cnt) {
6037 if (post_cnt == 0) {
6038 list_splice_init(&prep_sgl_list,
6039 &blck_sgl_list);
6040 post_cnt = block_cnt;
6041 } else if (block_cnt == 1) {
6042 status = lpfc_sli4_post_sgl(phba,
6043 sglq_entry->phys, 0,
6044 sglq_entry->sli4_xritag);
6045 if (!status) {
6046 /* successful, put sgl to posted list */
6047 list_add_tail(&sglq_entry->list,
6048 &post_sgl_list);
6049 } else {
6050 /* Failure, put sgl to free list */
6051 lpfc_printf_log(phba, KERN_WARNING,
6052 LOG_SLI,
6053 "3159 Failed to post els "
6054 "sgl, xritag:x%x\n",
6055 sglq_entry->sli4_xritag);
6056 list_add_tail(&sglq_entry->list,
6057 &free_sgl_list);
6058 spin_lock_irq(&phba->hbalock);
6059 phba->sli4_hba.els_xri_cnt--;
6060 spin_unlock_irq(&phba->hbalock);
6061 }
6062 }
6063 }
6064
6065 /* continue until a nembed page worth of sgls */
6066 if (post_cnt == 0)
6067 continue;
6068
6069 /* post the els buffer list sgls as a block */
6070 status = lpfc_sli4_post_els_sgl_list(phba, &blck_sgl_list,
6071 post_cnt);
6072
6073 if (!status) {
6074 /* success, put sgl list to posted sgl list */
6075 list_splice_init(&blck_sgl_list, &post_sgl_list);
6076 } else {
6077 /* Failure, put sgl list to free sgl list */
6078 sglq_entry_first = list_first_entry(&blck_sgl_list,
6079 struct lpfc_sglq,
6080 list);
6081 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
6082 "3160 Failed to post els sgl-list, "
6083 "xritag:x%x-x%x\n",
6084 sglq_entry_first->sli4_xritag,
6085 (sglq_entry_first->sli4_xritag +
6086 post_cnt - 1));
6087 list_splice_init(&blck_sgl_list, &free_sgl_list);
6088 spin_lock_irq(&phba->hbalock);
6089 phba->sli4_hba.els_xri_cnt -= post_cnt;
6090 spin_unlock_irq(&phba->hbalock);
6091 }
6092
6093 /* don't reset xirtag due to hole in xri block */
6094 if (block_cnt == 0)
6095 last_xritag = NO_XRI;
6096
6097 /* reset els sgl post count for next round of posting */
6098 post_cnt = 0;
6099 }
6100
6101 /* free the els sgls failed to post */
6102 lpfc_free_sgl_list(phba, &free_sgl_list);
6103
6104 /* push els sgls posted to the availble list */
6105 if (!list_empty(&post_sgl_list)) {
6106 spin_lock_irq(&phba->hbalock);
6107 list_splice_init(&post_sgl_list,
6108 &phba->sli4_hba.lpfc_sgl_list);
6109 spin_unlock_irq(&phba->hbalock);
6110 } else {
6111 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6112 "3161 Failure to post els sgl to port.\n");
6113 return -EIO;
6114 }
6115 return 0;
6116 }
6117
6118 /**
6119 * lpfc_sli4_hba_setup - SLI4 device intialization PCI function
6120 * @phba: Pointer to HBA context object.
6121 *
6122 * This function is the main SLI4 device intialization PCI function. This
6123 * function is called by the HBA intialization code, HBA reset code and
6124 * HBA error attention handler code. Caller is not required to hold any
6125 * locks.
6126 **/
6127 int
6128 lpfc_sli4_hba_setup(struct lpfc_hba *phba)
6129 {
6130 int rc;
6131 LPFC_MBOXQ_t *mboxq;
6132 struct lpfc_mqe *mqe;
6133 uint8_t *vpd;
6134 uint32_t vpd_size;
6135 uint32_t ftr_rsp = 0;
6136 struct Scsi_Host *shost = lpfc_shost_from_vport(phba->pport);
6137 struct lpfc_vport *vport = phba->pport;
6138 struct lpfc_dmabuf *mp;
6139
6140 /* Perform a PCI function reset to start from clean */
6141 rc = lpfc_pci_function_reset(phba);
6142 if (unlikely(rc))
6143 return -ENODEV;
6144
6145 /* Check the HBA Host Status Register for readyness */
6146 rc = lpfc_sli4_post_status_check(phba);
6147 if (unlikely(rc))
6148 return -ENODEV;
6149 else {
6150 spin_lock_irq(&phba->hbalock);
6151 phba->sli.sli_flag |= LPFC_SLI_ACTIVE;
6152 spin_unlock_irq(&phba->hbalock);
6153 }
6154
6155 /*
6156 * Allocate a single mailbox container for initializing the
6157 * port.
6158 */
6159 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
6160 if (!mboxq)
6161 return -ENOMEM;
6162
6163 /* Issue READ_REV to collect vpd and FW information. */
6164 vpd_size = SLI4_PAGE_SIZE;
6165 vpd = kzalloc(vpd_size, GFP_KERNEL);
6166 if (!vpd) {
6167 rc = -ENOMEM;
6168 goto out_free_mbox;
6169 }
6170
6171 rc = lpfc_sli4_read_rev(phba, mboxq, vpd, &vpd_size);
6172 if (unlikely(rc)) {
6173 kfree(vpd);
6174 goto out_free_mbox;
6175 }
6176 mqe = &mboxq->u.mqe;
6177 phba->sli_rev = bf_get(lpfc_mbx_rd_rev_sli_lvl, &mqe->un.read_rev);
6178 if (bf_get(lpfc_mbx_rd_rev_fcoe, &mqe->un.read_rev))
6179 phba->hba_flag |= HBA_FCOE_MODE;
6180 else
6181 phba->hba_flag &= ~HBA_FCOE_MODE;
6182
6183 if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
6184 LPFC_DCBX_CEE_MODE)
6185 phba->hba_flag |= HBA_FIP_SUPPORT;
6186 else
6187 phba->hba_flag &= ~HBA_FIP_SUPPORT;
6188
6189 phba->hba_flag &= ~HBA_FCP_IOQ_FLUSH;
6190
6191 if (phba->sli_rev != LPFC_SLI_REV4) {
6192 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6193 "0376 READ_REV Error. SLI Level %d "
6194 "FCoE enabled %d\n",
6195 phba->sli_rev, phba->hba_flag & HBA_FCOE_MODE);
6196 rc = -EIO;
6197 kfree(vpd);
6198 goto out_free_mbox;
6199 }
6200
6201 /*
6202 * Continue initialization with default values even if driver failed
6203 * to read FCoE param config regions, only read parameters if the
6204 * board is FCoE
6205 */
6206 if (phba->hba_flag & HBA_FCOE_MODE &&
6207 lpfc_sli4_read_fcoe_params(phba))
6208 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_INIT,
6209 "2570 Failed to read FCoE parameters\n");
6210
6211 /*
6212 * Retrieve sli4 device physical port name, failure of doing it
6213 * is considered as non-fatal.
6214 */
6215 rc = lpfc_sli4_retrieve_pport_name(phba);
6216 if (!rc)
6217 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6218 "3080 Successful retrieving SLI4 device "
6219 "physical port name: %s.\n", phba->Port);
6220
6221 /*
6222 * Evaluate the read rev and vpd data. Populate the driver
6223 * state with the results. If this routine fails, the failure
6224 * is not fatal as the driver will use generic values.
6225 */
6226 rc = lpfc_parse_vpd(phba, vpd, vpd_size);
6227 if (unlikely(!rc)) {
6228 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6229 "0377 Error %d parsing vpd. "
6230 "Using defaults.\n", rc);
6231 rc = 0;
6232 }
6233 kfree(vpd);
6234
6235 /* Save information as VPD data */
6236 phba->vpd.rev.biuRev = mqe->un.read_rev.first_hw_rev;
6237 phba->vpd.rev.smRev = mqe->un.read_rev.second_hw_rev;
6238 phba->vpd.rev.endecRev = mqe->un.read_rev.third_hw_rev;
6239 phba->vpd.rev.fcphHigh = bf_get(lpfc_mbx_rd_rev_fcph_high,
6240 &mqe->un.read_rev);
6241 phba->vpd.rev.fcphLow = bf_get(lpfc_mbx_rd_rev_fcph_low,
6242 &mqe->un.read_rev);
6243 phba->vpd.rev.feaLevelHigh = bf_get(lpfc_mbx_rd_rev_ftr_lvl_high,
6244 &mqe->un.read_rev);
6245 phba->vpd.rev.feaLevelLow = bf_get(lpfc_mbx_rd_rev_ftr_lvl_low,
6246 &mqe->un.read_rev);
6247 phba->vpd.rev.sli1FwRev = mqe->un.read_rev.fw_id_rev;
6248 memcpy(phba->vpd.rev.sli1FwName, mqe->un.read_rev.fw_name, 16);
6249 phba->vpd.rev.sli2FwRev = mqe->un.read_rev.ulp_fw_id_rev;
6250 memcpy(phba->vpd.rev.sli2FwName, mqe->un.read_rev.ulp_fw_name, 16);
6251 phba->vpd.rev.opFwRev = mqe->un.read_rev.fw_id_rev;
6252 memcpy(phba->vpd.rev.opFwName, mqe->un.read_rev.fw_name, 16);
6253 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6254 "(%d):0380 READ_REV Status x%x "
6255 "fw_rev:%s fcphHi:%x fcphLo:%x flHi:%x flLo:%x\n",
6256 mboxq->vport ? mboxq->vport->vpi : 0,
6257 bf_get(lpfc_mqe_status, mqe),
6258 phba->vpd.rev.opFwName,
6259 phba->vpd.rev.fcphHigh, phba->vpd.rev.fcphLow,
6260 phba->vpd.rev.feaLevelHigh, phba->vpd.rev.feaLevelLow);
6261
6262 /*
6263 * Discover the port's supported feature set and match it against the
6264 * hosts requests.
6265 */
6266 lpfc_request_features(phba, mboxq);
6267 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6268 if (unlikely(rc)) {
6269 rc = -EIO;
6270 goto out_free_mbox;
6271 }
6272
6273 /*
6274 * The port must support FCP initiator mode as this is the
6275 * only mode running in the host.
6276 */
6277 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_fcpi, &mqe->un.req_ftrs))) {
6278 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6279 "0378 No support for fcpi mode.\n");
6280 ftr_rsp++;
6281 }
6282 if (bf_get(lpfc_mbx_rq_ftr_rsp_perfh, &mqe->un.req_ftrs))
6283 phba->sli3_options |= LPFC_SLI4_PERFH_ENABLED;
6284 else
6285 phba->sli3_options &= ~LPFC_SLI4_PERFH_ENABLED;
6286 /*
6287 * If the port cannot support the host's requested features
6288 * then turn off the global config parameters to disable the
6289 * feature in the driver. This is not a fatal error.
6290 */
6291 phba->sli3_options &= ~LPFC_SLI3_BG_ENABLED;
6292 if (phba->cfg_enable_bg) {
6293 if (bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs))
6294 phba->sli3_options |= LPFC_SLI3_BG_ENABLED;
6295 else
6296 ftr_rsp++;
6297 }
6298
6299 if (phba->max_vpi && phba->cfg_enable_npiv &&
6300 !(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6301 ftr_rsp++;
6302
6303 if (ftr_rsp) {
6304 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
6305 "0379 Feature Mismatch Data: x%08x %08x "
6306 "x%x x%x x%x\n", mqe->un.req_ftrs.word2,
6307 mqe->un.req_ftrs.word3, phba->cfg_enable_bg,
6308 phba->cfg_enable_npiv, phba->max_vpi);
6309 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_dif, &mqe->un.req_ftrs)))
6310 phba->cfg_enable_bg = 0;
6311 if (!(bf_get(lpfc_mbx_rq_ftr_rsp_npiv, &mqe->un.req_ftrs)))
6312 phba->cfg_enable_npiv = 0;
6313 }
6314
6315 /* These SLI3 features are assumed in SLI4 */
6316 spin_lock_irq(&phba->hbalock);
6317 phba->sli3_options |= (LPFC_SLI3_NPIV_ENABLED | LPFC_SLI3_HBQ_ENABLED);
6318 spin_unlock_irq(&phba->hbalock);
6319
6320 /*
6321 * Allocate all resources (xri,rpi,vpi,vfi) now. Subsequent
6322 * calls depends on these resources to complete port setup.
6323 */
6324 rc = lpfc_sli4_alloc_resource_identifiers(phba);
6325 if (rc) {
6326 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6327 "2920 Failed to alloc Resource IDs "
6328 "rc = x%x\n", rc);
6329 goto out_free_mbox;
6330 }
6331
6332 /* Read the port's service parameters. */
6333 rc = lpfc_read_sparam(phba, mboxq, vport->vpi);
6334 if (rc) {
6335 phba->link_state = LPFC_HBA_ERROR;
6336 rc = -ENOMEM;
6337 goto out_free_mbox;
6338 }
6339
6340 mboxq->vport = vport;
6341 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6342 mp = (struct lpfc_dmabuf *) mboxq->context1;
6343 if (rc == MBX_SUCCESS) {
6344 memcpy(&vport->fc_sparam, mp->virt, sizeof(struct serv_parm));
6345 rc = 0;
6346 }
6347
6348 /*
6349 * This memory was allocated by the lpfc_read_sparam routine. Release
6350 * it to the mbuf pool.
6351 */
6352 lpfc_mbuf_free(phba, mp->virt, mp->phys);
6353 kfree(mp);
6354 mboxq->context1 = NULL;
6355 if (unlikely(rc)) {
6356 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6357 "0382 READ_SPARAM command failed "
6358 "status %d, mbxStatus x%x\n",
6359 rc, bf_get(lpfc_mqe_status, mqe));
6360 phba->link_state = LPFC_HBA_ERROR;
6361 rc = -EIO;
6362 goto out_free_mbox;
6363 }
6364
6365 lpfc_update_vport_wwn(vport);
6366
6367 /* Update the fc_host data structures with new wwn. */
6368 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
6369 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
6370
6371 /* update host els and scsi xri-sgl sizes and mappings */
6372 rc = lpfc_sli4_xri_sgl_update(phba);
6373 if (unlikely(rc)) {
6374 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6375 "1400 Failed to update xri-sgl size and "
6376 "mapping: %d\n", rc);
6377 goto out_free_mbox;
6378 }
6379
6380 /* register the els sgl pool to the port */
6381 rc = lpfc_sli4_repost_els_sgl_list(phba);
6382 if (unlikely(rc)) {
6383 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6384 "0582 Error %d during els sgl post "
6385 "operation\n", rc);
6386 rc = -ENODEV;
6387 goto out_free_mbox;
6388 }
6389
6390 /* register the allocated scsi sgl pool to the port */
6391 rc = lpfc_sli4_repost_scsi_sgl_list(phba);
6392 if (unlikely(rc)) {
6393 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6394 "0383 Error %d during scsi sgl post "
6395 "operation\n", rc);
6396 /* Some Scsi buffers were moved to the abort scsi list */
6397 /* A pci function reset will repost them */
6398 rc = -ENODEV;
6399 goto out_free_mbox;
6400 }
6401
6402 /* Post the rpi header region to the device. */
6403 rc = lpfc_sli4_post_all_rpi_hdrs(phba);
6404 if (unlikely(rc)) {
6405 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6406 "0393 Error %d during rpi post operation\n",
6407 rc);
6408 rc = -ENODEV;
6409 goto out_free_mbox;
6410 }
6411 lpfc_sli4_node_prep(phba);
6412
6413 /* Create all the SLI4 queues */
6414 rc = lpfc_sli4_queue_create(phba);
6415 if (rc) {
6416 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6417 "3089 Failed to allocate queues\n");
6418 rc = -ENODEV;
6419 goto out_stop_timers;
6420 }
6421 /* Set up all the queues to the device */
6422 rc = lpfc_sli4_queue_setup(phba);
6423 if (unlikely(rc)) {
6424 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6425 "0381 Error %d during queue setup.\n ", rc);
6426 goto out_destroy_queue;
6427 }
6428
6429 /* Arm the CQs and then EQs on device */
6430 lpfc_sli4_arm_cqeq_intr(phba);
6431
6432 /* Indicate device interrupt mode */
6433 phba->sli4_hba.intr_enable = 1;
6434
6435 /* Allow asynchronous mailbox command to go through */
6436 spin_lock_irq(&phba->hbalock);
6437 phba->sli.sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
6438 spin_unlock_irq(&phba->hbalock);
6439
6440 /* Post receive buffers to the device */
6441 lpfc_sli4_rb_setup(phba);
6442
6443 /* Reset HBA FCF states after HBA reset */
6444 phba->fcf.fcf_flag = 0;
6445 phba->fcf.current_rec.flag = 0;
6446
6447 /* Start the ELS watchdog timer */
6448 mod_timer(&vport->els_tmofunc,
6449 jiffies + HZ * (phba->fc_ratov * 2));
6450
6451 /* Start heart beat timer */
6452 mod_timer(&phba->hb_tmofunc,
6453 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
6454 phba->hb_outstanding = 0;
6455 phba->last_completion_time = jiffies;
6456
6457 /* Start error attention (ERATT) polling timer */
6458 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
6459
6460 /* Enable PCIe device Advanced Error Reporting (AER) if configured */
6461 if (phba->cfg_aer_support == 1 && !(phba->hba_flag & HBA_AER_ENABLED)) {
6462 rc = pci_enable_pcie_error_reporting(phba->pcidev);
6463 if (!rc) {
6464 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6465 "2829 This device supports "
6466 "Advanced Error Reporting (AER)\n");
6467 spin_lock_irq(&phba->hbalock);
6468 phba->hba_flag |= HBA_AER_ENABLED;
6469 spin_unlock_irq(&phba->hbalock);
6470 } else {
6471 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6472 "2830 This device does not support "
6473 "Advanced Error Reporting (AER)\n");
6474 phba->cfg_aer_support = 0;
6475 }
6476 rc = 0;
6477 }
6478
6479 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
6480 /*
6481 * The FC Port needs to register FCFI (index 0)
6482 */
6483 lpfc_reg_fcfi(phba, mboxq);
6484 mboxq->vport = phba->pport;
6485 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6486 if (rc != MBX_SUCCESS)
6487 goto out_unset_queue;
6488 rc = 0;
6489 phba->fcf.fcfi = bf_get(lpfc_reg_fcfi_fcfi,
6490 &mboxq->u.mqe.un.reg_fcfi);
6491
6492 /* Check if the port is configured to be disabled */
6493 lpfc_sli_read_link_ste(phba);
6494 }
6495
6496 /*
6497 * The port is ready, set the host's link state to LINK_DOWN
6498 * in preparation for link interrupts.
6499 */
6500 spin_lock_irq(&phba->hbalock);
6501 phba->link_state = LPFC_LINK_DOWN;
6502 spin_unlock_irq(&phba->hbalock);
6503 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
6504 (phba->hba_flag & LINK_DISABLED)) {
6505 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6506 "3103 Adapter Link is disabled.\n");
6507 lpfc_down_link(phba, mboxq);
6508 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6509 if (rc != MBX_SUCCESS) {
6510 lpfc_printf_log(phba, KERN_ERR, LOG_INIT | LOG_SLI,
6511 "3104 Adapter failed to issue "
6512 "DOWN_LINK mbox cmd, rc:x%x\n", rc);
6513 goto out_unset_queue;
6514 }
6515 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
6516 /* don't perform init_link on SLI4 FC port loopback test */
6517 if (!(phba->link_flag & LS_LOOPBACK_MODE)) {
6518 rc = phba->lpfc_hba_init_link(phba, MBX_NOWAIT);
6519 if (rc)
6520 goto out_unset_queue;
6521 }
6522 }
6523 mempool_free(mboxq, phba->mbox_mem_pool);
6524 return rc;
6525 out_unset_queue:
6526 /* Unset all the queues set up in this routine when error out */
6527 lpfc_sli4_queue_unset(phba);
6528 out_destroy_queue:
6529 lpfc_sli4_queue_destroy(phba);
6530 out_stop_timers:
6531 lpfc_stop_hba_timers(phba);
6532 out_free_mbox:
6533 mempool_free(mboxq, phba->mbox_mem_pool);
6534 return rc;
6535 }
6536
6537 /**
6538 * lpfc_mbox_timeout - Timeout call back function for mbox timer
6539 * @ptr: context object - pointer to hba structure.
6540 *
6541 * This is the callback function for mailbox timer. The mailbox
6542 * timer is armed when a new mailbox command is issued and the timer
6543 * is deleted when the mailbox complete. The function is called by
6544 * the kernel timer code when a mailbox does not complete within
6545 * expected time. This function wakes up the worker thread to
6546 * process the mailbox timeout and returns. All the processing is
6547 * done by the worker thread function lpfc_mbox_timeout_handler.
6548 **/
6549 void
6550 lpfc_mbox_timeout(unsigned long ptr)
6551 {
6552 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
6553 unsigned long iflag;
6554 uint32_t tmo_posted;
6555
6556 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
6557 tmo_posted = phba->pport->work_port_events & WORKER_MBOX_TMO;
6558 if (!tmo_posted)
6559 phba->pport->work_port_events |= WORKER_MBOX_TMO;
6560 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
6561
6562 if (!tmo_posted)
6563 lpfc_worker_wake_up(phba);
6564 return;
6565 }
6566
6567
6568 /**
6569 * lpfc_mbox_timeout_handler - Worker thread function to handle mailbox timeout
6570 * @phba: Pointer to HBA context object.
6571 *
6572 * This function is called from worker thread when a mailbox command times out.
6573 * The caller is not required to hold any locks. This function will reset the
6574 * HBA and recover all the pending commands.
6575 **/
6576 void
6577 lpfc_mbox_timeout_handler(struct lpfc_hba *phba)
6578 {
6579 LPFC_MBOXQ_t *pmbox = phba->sli.mbox_active;
6580 MAILBOX_t *mb = &pmbox->u.mb;
6581 struct lpfc_sli *psli = &phba->sli;
6582 struct lpfc_sli_ring *pring;
6583
6584 /* Check the pmbox pointer first. There is a race condition
6585 * between the mbox timeout handler getting executed in the
6586 * worklist and the mailbox actually completing. When this
6587 * race condition occurs, the mbox_active will be NULL.
6588 */
6589 spin_lock_irq(&phba->hbalock);
6590 if (pmbox == NULL) {
6591 lpfc_printf_log(phba, KERN_WARNING,
6592 LOG_MBOX | LOG_SLI,
6593 "0353 Active Mailbox cleared - mailbox timeout "
6594 "exiting\n");
6595 spin_unlock_irq(&phba->hbalock);
6596 return;
6597 }
6598
6599 /* Mbox cmd <mbxCommand> timeout */
6600 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6601 "0310 Mailbox command x%x timeout Data: x%x x%x x%p\n",
6602 mb->mbxCommand,
6603 phba->pport->port_state,
6604 phba->sli.sli_flag,
6605 phba->sli.mbox_active);
6606 spin_unlock_irq(&phba->hbalock);
6607
6608 /* Setting state unknown so lpfc_sli_abort_iocb_ring
6609 * would get IOCB_ERROR from lpfc_sli_issue_iocb, allowing
6610 * it to fail all outstanding SCSI IO.
6611 */
6612 spin_lock_irq(&phba->pport->work_port_lock);
6613 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
6614 spin_unlock_irq(&phba->pport->work_port_lock);
6615 spin_lock_irq(&phba->hbalock);
6616 phba->link_state = LPFC_LINK_UNKNOWN;
6617 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
6618 spin_unlock_irq(&phba->hbalock);
6619
6620 pring = &psli->ring[psli->fcp_ring];
6621 lpfc_sli_abort_iocb_ring(phba, pring);
6622
6623 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6624 "0345 Resetting board due to mailbox timeout\n");
6625
6626 /* Reset the HBA device */
6627 lpfc_reset_hba(phba);
6628 }
6629
6630 /**
6631 * lpfc_sli_issue_mbox_s3 - Issue an SLI3 mailbox command to firmware
6632 * @phba: Pointer to HBA context object.
6633 * @pmbox: Pointer to mailbox object.
6634 * @flag: Flag indicating how the mailbox need to be processed.
6635 *
6636 * This function is called by discovery code and HBA management code
6637 * to submit a mailbox command to firmware with SLI-3 interface spec. This
6638 * function gets the hbalock to protect the data structures.
6639 * The mailbox command can be submitted in polling mode, in which case
6640 * this function will wait in a polling loop for the completion of the
6641 * mailbox.
6642 * If the mailbox is submitted in no_wait mode (not polling) the
6643 * function will submit the command and returns immediately without waiting
6644 * for the mailbox completion. The no_wait is supported only when HBA
6645 * is in SLI2/SLI3 mode - interrupts are enabled.
6646 * The SLI interface allows only one mailbox pending at a time. If the
6647 * mailbox is issued in polling mode and there is already a mailbox
6648 * pending, then the function will return an error. If the mailbox is issued
6649 * in NO_WAIT mode and there is a mailbox pending already, the function
6650 * will return MBX_BUSY after queuing the mailbox into mailbox queue.
6651 * The sli layer owns the mailbox object until the completion of mailbox
6652 * command if this function return MBX_BUSY or MBX_SUCCESS. For all other
6653 * return codes the caller owns the mailbox command after the return of
6654 * the function.
6655 **/
6656 static int
6657 lpfc_sli_issue_mbox_s3(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox,
6658 uint32_t flag)
6659 {
6660 MAILBOX_t *mbx;
6661 struct lpfc_sli *psli = &phba->sli;
6662 uint32_t status, evtctr;
6663 uint32_t ha_copy, hc_copy;
6664 int i;
6665 unsigned long timeout;
6666 unsigned long drvr_flag = 0;
6667 uint32_t word0, ldata;
6668 void __iomem *to_slim;
6669 int processing_queue = 0;
6670
6671 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6672 if (!pmbox) {
6673 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6674 /* processing mbox queue from intr_handler */
6675 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
6676 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6677 return MBX_SUCCESS;
6678 }
6679 processing_queue = 1;
6680 pmbox = lpfc_mbox_get(phba);
6681 if (!pmbox) {
6682 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6683 return MBX_SUCCESS;
6684 }
6685 }
6686
6687 if (pmbox->mbox_cmpl && pmbox->mbox_cmpl != lpfc_sli_def_mbox_cmpl &&
6688 pmbox->mbox_cmpl != lpfc_sli_wake_mbox_wait) {
6689 if(!pmbox->vport) {
6690 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6691 lpfc_printf_log(phba, KERN_ERR,
6692 LOG_MBOX | LOG_VPORT,
6693 "1806 Mbox x%x failed. No vport\n",
6694 pmbox->u.mb.mbxCommand);
6695 dump_stack();
6696 goto out_not_finished;
6697 }
6698 }
6699
6700 /* If the PCI channel is in offline state, do not post mbox. */
6701 if (unlikely(pci_channel_offline(phba->pcidev))) {
6702 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6703 goto out_not_finished;
6704 }
6705
6706 /* If HBA has a deferred error attention, fail the iocb. */
6707 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
6708 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6709 goto out_not_finished;
6710 }
6711
6712 psli = &phba->sli;
6713
6714 mbx = &pmbox->u.mb;
6715 status = MBX_SUCCESS;
6716
6717 if (phba->link_state == LPFC_HBA_ERROR) {
6718 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6719
6720 /* Mbox command <mbxCommand> cannot issue */
6721 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6722 "(%d):0311 Mailbox command x%x cannot "
6723 "issue Data: x%x x%x\n",
6724 pmbox->vport ? pmbox->vport->vpi : 0,
6725 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6726 goto out_not_finished;
6727 }
6728
6729 if (mbx->mbxCommand != MBX_KILL_BOARD && flag & MBX_NOWAIT) {
6730 if (lpfc_readl(phba->HCregaddr, &hc_copy) ||
6731 !(hc_copy & HC_MBINT_ENA)) {
6732 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6733 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6734 "(%d):2528 Mailbox command x%x cannot "
6735 "issue Data: x%x x%x\n",
6736 pmbox->vport ? pmbox->vport->vpi : 0,
6737 pmbox->u.mb.mbxCommand, psli->sli_flag, flag);
6738 goto out_not_finished;
6739 }
6740 }
6741
6742 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
6743 /* Polling for a mbox command when another one is already active
6744 * is not allowed in SLI. Also, the driver must have established
6745 * SLI2 mode to queue and process multiple mbox commands.
6746 */
6747
6748 if (flag & MBX_POLL) {
6749 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6750
6751 /* Mbox command <mbxCommand> cannot issue */
6752 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6753 "(%d):2529 Mailbox command x%x "
6754 "cannot issue Data: x%x x%x\n",
6755 pmbox->vport ? pmbox->vport->vpi : 0,
6756 pmbox->u.mb.mbxCommand,
6757 psli->sli_flag, flag);
6758 goto out_not_finished;
6759 }
6760
6761 if (!(psli->sli_flag & LPFC_SLI_ACTIVE)) {
6762 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6763 /* Mbox command <mbxCommand> cannot issue */
6764 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6765 "(%d):2530 Mailbox command x%x "
6766 "cannot issue Data: x%x x%x\n",
6767 pmbox->vport ? pmbox->vport->vpi : 0,
6768 pmbox->u.mb.mbxCommand,
6769 psli->sli_flag, flag);
6770 goto out_not_finished;
6771 }
6772
6773 /* Another mailbox command is still being processed, queue this
6774 * command to be processed later.
6775 */
6776 lpfc_mbox_put(phba, pmbox);
6777
6778 /* Mbox cmd issue - BUSY */
6779 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6780 "(%d):0308 Mbox cmd issue - BUSY Data: "
6781 "x%x x%x x%x x%x\n",
6782 pmbox->vport ? pmbox->vport->vpi : 0xffffff,
6783 mbx->mbxCommand, phba->pport->port_state,
6784 psli->sli_flag, flag);
6785
6786 psli->slistat.mbox_busy++;
6787 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6788
6789 if (pmbox->vport) {
6790 lpfc_debugfs_disc_trc(pmbox->vport,
6791 LPFC_DISC_TRC_MBOX_VPORT,
6792 "MBOX Bsy vport: cmd:x%x mb:x%x x%x",
6793 (uint32_t)mbx->mbxCommand,
6794 mbx->un.varWords[0], mbx->un.varWords[1]);
6795 }
6796 else {
6797 lpfc_debugfs_disc_trc(phba->pport,
6798 LPFC_DISC_TRC_MBOX,
6799 "MBOX Bsy: cmd:x%x mb:x%x x%x",
6800 (uint32_t)mbx->mbxCommand,
6801 mbx->un.varWords[0], mbx->un.varWords[1]);
6802 }
6803
6804 return MBX_BUSY;
6805 }
6806
6807 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
6808
6809 /* If we are not polling, we MUST be in SLI2 mode */
6810 if (flag != MBX_POLL) {
6811 if (!(psli->sli_flag & LPFC_SLI_ACTIVE) &&
6812 (mbx->mbxCommand != MBX_KILL_BOARD)) {
6813 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6814 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
6815 /* Mbox command <mbxCommand> cannot issue */
6816 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
6817 "(%d):2531 Mailbox command x%x "
6818 "cannot issue Data: x%x x%x\n",
6819 pmbox->vport ? pmbox->vport->vpi : 0,
6820 pmbox->u.mb.mbxCommand,
6821 psli->sli_flag, flag);
6822 goto out_not_finished;
6823 }
6824 /* timeout active mbox command */
6825 mod_timer(&psli->mbox_tmo, (jiffies +
6826 (HZ * lpfc_mbox_tmo_val(phba, pmbox))));
6827 }
6828
6829 /* Mailbox cmd <cmd> issue */
6830 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
6831 "(%d):0309 Mailbox cmd x%x issue Data: x%x x%x "
6832 "x%x\n",
6833 pmbox->vport ? pmbox->vport->vpi : 0,
6834 mbx->mbxCommand, phba->pport->port_state,
6835 psli->sli_flag, flag);
6836
6837 if (mbx->mbxCommand != MBX_HEARTBEAT) {
6838 if (pmbox->vport) {
6839 lpfc_debugfs_disc_trc(pmbox->vport,
6840 LPFC_DISC_TRC_MBOX_VPORT,
6841 "MBOX Send vport: cmd:x%x mb:x%x x%x",
6842 (uint32_t)mbx->mbxCommand,
6843 mbx->un.varWords[0], mbx->un.varWords[1]);
6844 }
6845 else {
6846 lpfc_debugfs_disc_trc(phba->pport,
6847 LPFC_DISC_TRC_MBOX,
6848 "MBOX Send: cmd:x%x mb:x%x x%x",
6849 (uint32_t)mbx->mbxCommand,
6850 mbx->un.varWords[0], mbx->un.varWords[1]);
6851 }
6852 }
6853
6854 psli->slistat.mbox_cmd++;
6855 evtctr = psli->slistat.mbox_event;
6856
6857 /* next set own bit for the adapter and copy over command word */
6858 mbx->mbxOwner = OWN_CHIP;
6859
6860 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6861 /* Populate mbox extension offset word. */
6862 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len) {
6863 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
6864 = (uint8_t *)phba->mbox_ext
6865 - (uint8_t *)phba->mbox;
6866 }
6867
6868 /* Copy the mailbox extension data */
6869 if (pmbox->in_ext_byte_len && pmbox->context2) {
6870 lpfc_sli_pcimem_bcopy(pmbox->context2,
6871 (uint8_t *)phba->mbox_ext,
6872 pmbox->in_ext_byte_len);
6873 }
6874 /* Copy command data to host SLIM area */
6875 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
6876 } else {
6877 /* Populate mbox extension offset word. */
6878 if (pmbox->in_ext_byte_len || pmbox->out_ext_byte_len)
6879 *(((uint32_t *)mbx) + pmbox->mbox_offset_word)
6880 = MAILBOX_HBA_EXT_OFFSET;
6881
6882 /* Copy the mailbox extension data */
6883 if (pmbox->in_ext_byte_len && pmbox->context2) {
6884 lpfc_memcpy_to_slim(phba->MBslimaddr +
6885 MAILBOX_HBA_EXT_OFFSET,
6886 pmbox->context2, pmbox->in_ext_byte_len);
6887
6888 }
6889 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
6890 /* copy command data into host mbox for cmpl */
6891 lpfc_sli_pcimem_bcopy(mbx, phba->mbox, MAILBOX_CMD_SIZE);
6892 }
6893
6894 /* First copy mbox command data to HBA SLIM, skip past first
6895 word */
6896 to_slim = phba->MBslimaddr + sizeof (uint32_t);
6897 lpfc_memcpy_to_slim(to_slim, &mbx->un.varWords[0],
6898 MAILBOX_CMD_SIZE - sizeof (uint32_t));
6899
6900 /* Next copy over first word, with mbxOwner set */
6901 ldata = *((uint32_t *)mbx);
6902 to_slim = phba->MBslimaddr;
6903 writel(ldata, to_slim);
6904 readl(to_slim); /* flush */
6905
6906 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
6907 /* switch over to host mailbox */
6908 psli->sli_flag |= LPFC_SLI_ACTIVE;
6909 }
6910 }
6911
6912 wmb();
6913
6914 switch (flag) {
6915 case MBX_NOWAIT:
6916 /* Set up reference to mailbox command */
6917 psli->mbox_active = pmbox;
6918 /* Interrupt board to do it */
6919 writel(CA_MBATT, phba->CAregaddr);
6920 readl(phba->CAregaddr); /* flush */
6921 /* Don't wait for it to finish, just return */
6922 break;
6923
6924 case MBX_POLL:
6925 /* Set up null reference to mailbox command */
6926 psli->mbox_active = NULL;
6927 /* Interrupt board to do it */
6928 writel(CA_MBATT, phba->CAregaddr);
6929 readl(phba->CAregaddr); /* flush */
6930
6931 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6932 /* First read mbox status word */
6933 word0 = *((uint32_t *)phba->mbox);
6934 word0 = le32_to_cpu(word0);
6935 } else {
6936 /* First read mbox status word */
6937 if (lpfc_readl(phba->MBslimaddr, &word0)) {
6938 spin_unlock_irqrestore(&phba->hbalock,
6939 drvr_flag);
6940 goto out_not_finished;
6941 }
6942 }
6943
6944 /* Read the HBA Host Attention Register */
6945 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
6946 spin_unlock_irqrestore(&phba->hbalock,
6947 drvr_flag);
6948 goto out_not_finished;
6949 }
6950 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, pmbox) *
6951 1000) + jiffies;
6952 i = 0;
6953 /* Wait for command to complete */
6954 while (((word0 & OWN_CHIP) == OWN_CHIP) ||
6955 (!(ha_copy & HA_MBATT) &&
6956 (phba->link_state > LPFC_WARM_START))) {
6957 if (time_after(jiffies, timeout)) {
6958 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
6959 spin_unlock_irqrestore(&phba->hbalock,
6960 drvr_flag);
6961 goto out_not_finished;
6962 }
6963
6964 /* Check if we took a mbox interrupt while we were
6965 polling */
6966 if (((word0 & OWN_CHIP) != OWN_CHIP)
6967 && (evtctr != psli->slistat.mbox_event))
6968 break;
6969
6970 if (i++ > 10) {
6971 spin_unlock_irqrestore(&phba->hbalock,
6972 drvr_flag);
6973 msleep(1);
6974 spin_lock_irqsave(&phba->hbalock, drvr_flag);
6975 }
6976
6977 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
6978 /* First copy command data */
6979 word0 = *((uint32_t *)phba->mbox);
6980 word0 = le32_to_cpu(word0);
6981 if (mbx->mbxCommand == MBX_CONFIG_PORT) {
6982 MAILBOX_t *slimmb;
6983 uint32_t slimword0;
6984 /* Check real SLIM for any errors */
6985 slimword0 = readl(phba->MBslimaddr);
6986 slimmb = (MAILBOX_t *) & slimword0;
6987 if (((slimword0 & OWN_CHIP) != OWN_CHIP)
6988 && slimmb->mbxStatus) {
6989 psli->sli_flag &=
6990 ~LPFC_SLI_ACTIVE;
6991 word0 = slimword0;
6992 }
6993 }
6994 } else {
6995 /* First copy command data */
6996 word0 = readl(phba->MBslimaddr);
6997 }
6998 /* Read the HBA Host Attention Register */
6999 if (lpfc_readl(phba->HAregaddr, &ha_copy)) {
7000 spin_unlock_irqrestore(&phba->hbalock,
7001 drvr_flag);
7002 goto out_not_finished;
7003 }
7004 }
7005
7006 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
7007 /* copy results back to user */
7008 lpfc_sli_pcimem_bcopy(phba->mbox, mbx, MAILBOX_CMD_SIZE);
7009 /* Copy the mailbox extension data */
7010 if (pmbox->out_ext_byte_len && pmbox->context2) {
7011 lpfc_sli_pcimem_bcopy(phba->mbox_ext,
7012 pmbox->context2,
7013 pmbox->out_ext_byte_len);
7014 }
7015 } else {
7016 /* First copy command data */
7017 lpfc_memcpy_from_slim(mbx, phba->MBslimaddr,
7018 MAILBOX_CMD_SIZE);
7019 /* Copy the mailbox extension data */
7020 if (pmbox->out_ext_byte_len && pmbox->context2) {
7021 lpfc_memcpy_from_slim(pmbox->context2,
7022 phba->MBslimaddr +
7023 MAILBOX_HBA_EXT_OFFSET,
7024 pmbox->out_ext_byte_len);
7025 }
7026 }
7027
7028 writel(HA_MBATT, phba->HAregaddr);
7029 readl(phba->HAregaddr); /* flush */
7030
7031 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7032 status = mbx->mbxStatus;
7033 }
7034
7035 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
7036 return status;
7037
7038 out_not_finished:
7039 if (processing_queue) {
7040 pmbox->u.mb.mbxStatus = MBX_NOT_FINISHED;
7041 lpfc_mbox_cmpl_put(phba, pmbox);
7042 }
7043 return MBX_NOT_FINISHED;
7044 }
7045
7046 /**
7047 * lpfc_sli4_async_mbox_block - Block posting SLI4 asynchronous mailbox command
7048 * @phba: Pointer to HBA context object.
7049 *
7050 * The function blocks the posting of SLI4 asynchronous mailbox commands from
7051 * the driver internal pending mailbox queue. It will then try to wait out the
7052 * possible outstanding mailbox command before return.
7053 *
7054 * Returns:
7055 * 0 - the outstanding mailbox command completed; otherwise, the wait for
7056 * the outstanding mailbox command timed out.
7057 **/
7058 static int
7059 lpfc_sli4_async_mbox_block(struct lpfc_hba *phba)
7060 {
7061 struct lpfc_sli *psli = &phba->sli;
7062 int rc = 0;
7063 unsigned long timeout = 0;
7064
7065 /* Mark the asynchronous mailbox command posting as blocked */
7066 spin_lock_irq(&phba->hbalock);
7067 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
7068 /* Determine how long we might wait for the active mailbox
7069 * command to be gracefully completed by firmware.
7070 */
7071 if (phba->sli.mbox_active)
7072 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
7073 phba->sli.mbox_active) *
7074 1000) + jiffies;
7075 spin_unlock_irq(&phba->hbalock);
7076
7077 /* Wait for the outstnading mailbox command to complete */
7078 while (phba->sli.mbox_active) {
7079 /* Check active mailbox complete status every 2ms */
7080 msleep(2);
7081 if (time_after(jiffies, timeout)) {
7082 /* Timeout, marked the outstanding cmd not complete */
7083 rc = 1;
7084 break;
7085 }
7086 }
7087
7088 /* Can not cleanly block async mailbox command, fails it */
7089 if (rc) {
7090 spin_lock_irq(&phba->hbalock);
7091 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7092 spin_unlock_irq(&phba->hbalock);
7093 }
7094 return rc;
7095 }
7096
7097 /**
7098 * lpfc_sli4_async_mbox_unblock - Block posting SLI4 async mailbox command
7099 * @phba: Pointer to HBA context object.
7100 *
7101 * The function unblocks and resume posting of SLI4 asynchronous mailbox
7102 * commands from the driver internal pending mailbox queue. It makes sure
7103 * that there is no outstanding mailbox command before resuming posting
7104 * asynchronous mailbox commands. If, for any reason, there is outstanding
7105 * mailbox command, it will try to wait it out before resuming asynchronous
7106 * mailbox command posting.
7107 **/
7108 static void
7109 lpfc_sli4_async_mbox_unblock(struct lpfc_hba *phba)
7110 {
7111 struct lpfc_sli *psli = &phba->sli;
7112
7113 spin_lock_irq(&phba->hbalock);
7114 if (!(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7115 /* Asynchronous mailbox posting is not blocked, do nothing */
7116 spin_unlock_irq(&phba->hbalock);
7117 return;
7118 }
7119
7120 /* Outstanding synchronous mailbox command is guaranteed to be done,
7121 * successful or timeout, after timing-out the outstanding mailbox
7122 * command shall always be removed, so just unblock posting async
7123 * mailbox command and resume
7124 */
7125 psli->sli_flag &= ~LPFC_SLI_ASYNC_MBX_BLK;
7126 spin_unlock_irq(&phba->hbalock);
7127
7128 /* wake up worker thread to post asynchronlous mailbox command */
7129 lpfc_worker_wake_up(phba);
7130 }
7131
7132 /**
7133 * lpfc_sli4_wait_bmbx_ready - Wait for bootstrap mailbox register ready
7134 * @phba: Pointer to HBA context object.
7135 * @mboxq: Pointer to mailbox object.
7136 *
7137 * The function waits for the bootstrap mailbox register ready bit from
7138 * port for twice the regular mailbox command timeout value.
7139 *
7140 * 0 - no timeout on waiting for bootstrap mailbox register ready.
7141 * MBXERR_ERROR - wait for bootstrap mailbox register timed out.
7142 **/
7143 static int
7144 lpfc_sli4_wait_bmbx_ready(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7145 {
7146 uint32_t db_ready;
7147 unsigned long timeout;
7148 struct lpfc_register bmbx_reg;
7149
7150 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba, mboxq)
7151 * 1000) + jiffies;
7152
7153 do {
7154 bmbx_reg.word0 = readl(phba->sli4_hba.BMBXregaddr);
7155 db_ready = bf_get(lpfc_bmbx_rdy, &bmbx_reg);
7156 if (!db_ready)
7157 msleep(2);
7158
7159 if (time_after(jiffies, timeout))
7160 return MBXERR_ERROR;
7161 } while (!db_ready);
7162
7163 return 0;
7164 }
7165
7166 /**
7167 * lpfc_sli4_post_sync_mbox - Post an SLI4 mailbox to the bootstrap mailbox
7168 * @phba: Pointer to HBA context object.
7169 * @mboxq: Pointer to mailbox object.
7170 *
7171 * The function posts a mailbox to the port. The mailbox is expected
7172 * to be comletely filled in and ready for the port to operate on it.
7173 * This routine executes a synchronous completion operation on the
7174 * mailbox by polling for its completion.
7175 *
7176 * The caller must not be holding any locks when calling this routine.
7177 *
7178 * Returns:
7179 * MBX_SUCCESS - mailbox posted successfully
7180 * Any of the MBX error values.
7181 **/
7182 static int
7183 lpfc_sli4_post_sync_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
7184 {
7185 int rc = MBX_SUCCESS;
7186 unsigned long iflag;
7187 uint32_t mcqe_status;
7188 uint32_t mbx_cmnd;
7189 struct lpfc_sli *psli = &phba->sli;
7190 struct lpfc_mqe *mb = &mboxq->u.mqe;
7191 struct lpfc_bmbx_create *mbox_rgn;
7192 struct dma_address *dma_address;
7193
7194 /*
7195 * Only one mailbox can be active to the bootstrap mailbox region
7196 * at a time and there is no queueing provided.
7197 */
7198 spin_lock_irqsave(&phba->hbalock, iflag);
7199 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7200 spin_unlock_irqrestore(&phba->hbalock, iflag);
7201 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7202 "(%d):2532 Mailbox command x%x (x%x/x%x) "
7203 "cannot issue Data: x%x x%x\n",
7204 mboxq->vport ? mboxq->vport->vpi : 0,
7205 mboxq->u.mb.mbxCommand,
7206 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7207 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7208 psli->sli_flag, MBX_POLL);
7209 return MBXERR_ERROR;
7210 }
7211 /* The server grabs the token and owns it until release */
7212 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7213 phba->sli.mbox_active = mboxq;
7214 spin_unlock_irqrestore(&phba->hbalock, iflag);
7215
7216 /* wait for bootstrap mbox register for readyness */
7217 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7218 if (rc)
7219 goto exit;
7220
7221 /*
7222 * Initialize the bootstrap memory region to avoid stale data areas
7223 * in the mailbox post. Then copy the caller's mailbox contents to
7224 * the bmbx mailbox region.
7225 */
7226 mbx_cmnd = bf_get(lpfc_mqe_command, mb);
7227 memset(phba->sli4_hba.bmbx.avirt, 0, sizeof(struct lpfc_bmbx_create));
7228 lpfc_sli_pcimem_bcopy(mb, phba->sli4_hba.bmbx.avirt,
7229 sizeof(struct lpfc_mqe));
7230
7231 /* Post the high mailbox dma address to the port and wait for ready. */
7232 dma_address = &phba->sli4_hba.bmbx.dma_address;
7233 writel(dma_address->addr_hi, phba->sli4_hba.BMBXregaddr);
7234
7235 /* wait for bootstrap mbox register for hi-address write done */
7236 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7237 if (rc)
7238 goto exit;
7239
7240 /* Post the low mailbox dma address to the port. */
7241 writel(dma_address->addr_lo, phba->sli4_hba.BMBXregaddr);
7242
7243 /* wait for bootstrap mbox register for low address write done */
7244 rc = lpfc_sli4_wait_bmbx_ready(phba, mboxq);
7245 if (rc)
7246 goto exit;
7247
7248 /*
7249 * Read the CQ to ensure the mailbox has completed.
7250 * If so, update the mailbox status so that the upper layers
7251 * can complete the request normally.
7252 */
7253 lpfc_sli_pcimem_bcopy(phba->sli4_hba.bmbx.avirt, mb,
7254 sizeof(struct lpfc_mqe));
7255 mbox_rgn = (struct lpfc_bmbx_create *) phba->sli4_hba.bmbx.avirt;
7256 lpfc_sli_pcimem_bcopy(&mbox_rgn->mcqe, &mboxq->mcqe,
7257 sizeof(struct lpfc_mcqe));
7258 mcqe_status = bf_get(lpfc_mcqe_status, &mbox_rgn->mcqe);
7259 /*
7260 * When the CQE status indicates a failure and the mailbox status
7261 * indicates success then copy the CQE status into the mailbox status
7262 * (and prefix it with x4000).
7263 */
7264 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
7265 if (bf_get(lpfc_mqe_status, mb) == MBX_SUCCESS)
7266 bf_set(lpfc_mqe_status, mb,
7267 (LPFC_MBX_ERROR_RANGE | mcqe_status));
7268 rc = MBXERR_ERROR;
7269 } else
7270 lpfc_sli4_swap_str(phba, mboxq);
7271
7272 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7273 "(%d):0356 Mailbox cmd x%x (x%x/x%x) Status x%x "
7274 "Data: x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x x%x"
7275 " x%x x%x CQ: x%x x%x x%x x%x\n",
7276 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7277 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7278 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7279 bf_get(lpfc_mqe_status, mb),
7280 mb->un.mb_words[0], mb->un.mb_words[1],
7281 mb->un.mb_words[2], mb->un.mb_words[3],
7282 mb->un.mb_words[4], mb->un.mb_words[5],
7283 mb->un.mb_words[6], mb->un.mb_words[7],
7284 mb->un.mb_words[8], mb->un.mb_words[9],
7285 mb->un.mb_words[10], mb->un.mb_words[11],
7286 mb->un.mb_words[12], mboxq->mcqe.word0,
7287 mboxq->mcqe.mcqe_tag0, mboxq->mcqe.mcqe_tag1,
7288 mboxq->mcqe.trailer);
7289 exit:
7290 /* We are holding the token, no needed for lock when release */
7291 spin_lock_irqsave(&phba->hbalock, iflag);
7292 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7293 phba->sli.mbox_active = NULL;
7294 spin_unlock_irqrestore(&phba->hbalock, iflag);
7295 return rc;
7296 }
7297
7298 /**
7299 * lpfc_sli_issue_mbox_s4 - Issue an SLI4 mailbox command to firmware
7300 * @phba: Pointer to HBA context object.
7301 * @pmbox: Pointer to mailbox object.
7302 * @flag: Flag indicating how the mailbox need to be processed.
7303 *
7304 * This function is called by discovery code and HBA management code to submit
7305 * a mailbox command to firmware with SLI-4 interface spec.
7306 *
7307 * Return codes the caller owns the mailbox command after the return of the
7308 * function.
7309 **/
7310 static int
7311 lpfc_sli_issue_mbox_s4(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq,
7312 uint32_t flag)
7313 {
7314 struct lpfc_sli *psli = &phba->sli;
7315 unsigned long iflags;
7316 int rc;
7317
7318 /* dump from issue mailbox command if setup */
7319 lpfc_idiag_mbxacc_dump_issue_mbox(phba, &mboxq->u.mb);
7320
7321 rc = lpfc_mbox_dev_check(phba);
7322 if (unlikely(rc)) {
7323 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7324 "(%d):2544 Mailbox command x%x (x%x/x%x) "
7325 "cannot issue Data: x%x x%x\n",
7326 mboxq->vport ? mboxq->vport->vpi : 0,
7327 mboxq->u.mb.mbxCommand,
7328 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7329 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7330 psli->sli_flag, flag);
7331 goto out_not_finished;
7332 }
7333
7334 /* Detect polling mode and jump to a handler */
7335 if (!phba->sli4_hba.intr_enable) {
7336 if (flag == MBX_POLL)
7337 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7338 else
7339 rc = -EIO;
7340 if (rc != MBX_SUCCESS)
7341 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7342 "(%d):2541 Mailbox command x%x "
7343 "(x%x/x%x) failure: "
7344 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7345 "Data: x%x x%x\n,",
7346 mboxq->vport ? mboxq->vport->vpi : 0,
7347 mboxq->u.mb.mbxCommand,
7348 lpfc_sli_config_mbox_subsys_get(phba,
7349 mboxq),
7350 lpfc_sli_config_mbox_opcode_get(phba,
7351 mboxq),
7352 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7353 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7354 bf_get(lpfc_mcqe_ext_status,
7355 &mboxq->mcqe),
7356 psli->sli_flag, flag);
7357 return rc;
7358 } else if (flag == MBX_POLL) {
7359 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX | LOG_SLI,
7360 "(%d):2542 Try to issue mailbox command "
7361 "x%x (x%x/x%x) synchronously ahead of async"
7362 "mailbox command queue: x%x x%x\n",
7363 mboxq->vport ? mboxq->vport->vpi : 0,
7364 mboxq->u.mb.mbxCommand,
7365 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7366 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7367 psli->sli_flag, flag);
7368 /* Try to block the asynchronous mailbox posting */
7369 rc = lpfc_sli4_async_mbox_block(phba);
7370 if (!rc) {
7371 /* Successfully blocked, now issue sync mbox cmd */
7372 rc = lpfc_sli4_post_sync_mbox(phba, mboxq);
7373 if (rc != MBX_SUCCESS)
7374 lpfc_printf_log(phba, KERN_WARNING,
7375 LOG_MBOX | LOG_SLI,
7376 "(%d):2597 Sync Mailbox command "
7377 "x%x (x%x/x%x) failure: "
7378 "mqe_sta: x%x mcqe_sta: x%x/x%x "
7379 "Data: x%x x%x\n,",
7380 mboxq->vport ? mboxq->vport->vpi : 0,
7381 mboxq->u.mb.mbxCommand,
7382 lpfc_sli_config_mbox_subsys_get(phba,
7383 mboxq),
7384 lpfc_sli_config_mbox_opcode_get(phba,
7385 mboxq),
7386 bf_get(lpfc_mqe_status, &mboxq->u.mqe),
7387 bf_get(lpfc_mcqe_status, &mboxq->mcqe),
7388 bf_get(lpfc_mcqe_ext_status,
7389 &mboxq->mcqe),
7390 psli->sli_flag, flag);
7391 /* Unblock the async mailbox posting afterward */
7392 lpfc_sli4_async_mbox_unblock(phba);
7393 }
7394 return rc;
7395 }
7396
7397 /* Now, interrupt mode asynchrous mailbox command */
7398 rc = lpfc_mbox_cmd_check(phba, mboxq);
7399 if (rc) {
7400 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7401 "(%d):2543 Mailbox command x%x (x%x/x%x) "
7402 "cannot issue Data: x%x x%x\n",
7403 mboxq->vport ? mboxq->vport->vpi : 0,
7404 mboxq->u.mb.mbxCommand,
7405 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7406 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7407 psli->sli_flag, flag);
7408 goto out_not_finished;
7409 }
7410
7411 /* Put the mailbox command to the driver internal FIFO */
7412 psli->slistat.mbox_busy++;
7413 spin_lock_irqsave(&phba->hbalock, iflags);
7414 lpfc_mbox_put(phba, mboxq);
7415 spin_unlock_irqrestore(&phba->hbalock, iflags);
7416 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7417 "(%d):0354 Mbox cmd issue - Enqueue Data: "
7418 "x%x (x%x/x%x) x%x x%x x%x\n",
7419 mboxq->vport ? mboxq->vport->vpi : 0xffffff,
7420 bf_get(lpfc_mqe_command, &mboxq->u.mqe),
7421 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7422 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7423 phba->pport->port_state,
7424 psli->sli_flag, MBX_NOWAIT);
7425 /* Wake up worker thread to transport mailbox command from head */
7426 lpfc_worker_wake_up(phba);
7427
7428 return MBX_BUSY;
7429
7430 out_not_finished:
7431 return MBX_NOT_FINISHED;
7432 }
7433
7434 /**
7435 * lpfc_sli4_post_async_mbox - Post an SLI4 mailbox command to device
7436 * @phba: Pointer to HBA context object.
7437 *
7438 * This function is called by worker thread to send a mailbox command to
7439 * SLI4 HBA firmware.
7440 *
7441 **/
7442 int
7443 lpfc_sli4_post_async_mbox(struct lpfc_hba *phba)
7444 {
7445 struct lpfc_sli *psli = &phba->sli;
7446 LPFC_MBOXQ_t *mboxq;
7447 int rc = MBX_SUCCESS;
7448 unsigned long iflags;
7449 struct lpfc_mqe *mqe;
7450 uint32_t mbx_cmnd;
7451
7452 /* Check interrupt mode before post async mailbox command */
7453 if (unlikely(!phba->sli4_hba.intr_enable))
7454 return MBX_NOT_FINISHED;
7455
7456 /* Check for mailbox command service token */
7457 spin_lock_irqsave(&phba->hbalock, iflags);
7458 if (unlikely(psli->sli_flag & LPFC_SLI_ASYNC_MBX_BLK)) {
7459 spin_unlock_irqrestore(&phba->hbalock, iflags);
7460 return MBX_NOT_FINISHED;
7461 }
7462 if (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE) {
7463 spin_unlock_irqrestore(&phba->hbalock, iflags);
7464 return MBX_NOT_FINISHED;
7465 }
7466 if (unlikely(phba->sli.mbox_active)) {
7467 spin_unlock_irqrestore(&phba->hbalock, iflags);
7468 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7469 "0384 There is pending active mailbox cmd\n");
7470 return MBX_NOT_FINISHED;
7471 }
7472 /* Take the mailbox command service token */
7473 psli->sli_flag |= LPFC_SLI_MBOX_ACTIVE;
7474
7475 /* Get the next mailbox command from head of queue */
7476 mboxq = lpfc_mbox_get(phba);
7477
7478 /* If no more mailbox command waiting for post, we're done */
7479 if (!mboxq) {
7480 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7481 spin_unlock_irqrestore(&phba->hbalock, iflags);
7482 return MBX_SUCCESS;
7483 }
7484 phba->sli.mbox_active = mboxq;
7485 spin_unlock_irqrestore(&phba->hbalock, iflags);
7486
7487 /* Check device readiness for posting mailbox command */
7488 rc = lpfc_mbox_dev_check(phba);
7489 if (unlikely(rc))
7490 /* Driver clean routine will clean up pending mailbox */
7491 goto out_not_finished;
7492
7493 /* Prepare the mbox command to be posted */
7494 mqe = &mboxq->u.mqe;
7495 mbx_cmnd = bf_get(lpfc_mqe_command, mqe);
7496
7497 /* Start timer for the mbox_tmo and log some mailbox post messages */
7498 mod_timer(&psli->mbox_tmo, (jiffies +
7499 (HZ * lpfc_mbox_tmo_val(phba, mboxq))));
7500
7501 lpfc_printf_log(phba, KERN_INFO, LOG_MBOX | LOG_SLI,
7502 "(%d):0355 Mailbox cmd x%x (x%x/x%x) issue Data: "
7503 "x%x x%x\n",
7504 mboxq->vport ? mboxq->vport->vpi : 0, mbx_cmnd,
7505 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7506 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7507 phba->pport->port_state, psli->sli_flag);
7508
7509 if (mbx_cmnd != MBX_HEARTBEAT) {
7510 if (mboxq->vport) {
7511 lpfc_debugfs_disc_trc(mboxq->vport,
7512 LPFC_DISC_TRC_MBOX_VPORT,
7513 "MBOX Send vport: cmd:x%x mb:x%x x%x",
7514 mbx_cmnd, mqe->un.mb_words[0],
7515 mqe->un.mb_words[1]);
7516 } else {
7517 lpfc_debugfs_disc_trc(phba->pport,
7518 LPFC_DISC_TRC_MBOX,
7519 "MBOX Send: cmd:x%x mb:x%x x%x",
7520 mbx_cmnd, mqe->un.mb_words[0],
7521 mqe->un.mb_words[1]);
7522 }
7523 }
7524 psli->slistat.mbox_cmd++;
7525
7526 /* Post the mailbox command to the port */
7527 rc = lpfc_sli4_mq_put(phba->sli4_hba.mbx_wq, mqe);
7528 if (rc != MBX_SUCCESS) {
7529 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
7530 "(%d):2533 Mailbox command x%x (x%x/x%x) "
7531 "cannot issue Data: x%x x%x\n",
7532 mboxq->vport ? mboxq->vport->vpi : 0,
7533 mboxq->u.mb.mbxCommand,
7534 lpfc_sli_config_mbox_subsys_get(phba, mboxq),
7535 lpfc_sli_config_mbox_opcode_get(phba, mboxq),
7536 psli->sli_flag, MBX_NOWAIT);
7537 goto out_not_finished;
7538 }
7539
7540 return rc;
7541
7542 out_not_finished:
7543 spin_lock_irqsave(&phba->hbalock, iflags);
7544 if (phba->sli.mbox_active) {
7545 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
7546 __lpfc_mbox_cmpl_put(phba, mboxq);
7547 /* Release the token */
7548 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
7549 phba->sli.mbox_active = NULL;
7550 }
7551 spin_unlock_irqrestore(&phba->hbalock, iflags);
7552
7553 return MBX_NOT_FINISHED;
7554 }
7555
7556 /**
7557 * lpfc_sli_issue_mbox - Wrapper func for issuing mailbox command
7558 * @phba: Pointer to HBA context object.
7559 * @pmbox: Pointer to mailbox object.
7560 * @flag: Flag indicating how the mailbox need to be processed.
7561 *
7562 * This routine wraps the actual SLI3 or SLI4 mailbox issuing routine from
7563 * the API jump table function pointer from the lpfc_hba struct.
7564 *
7565 * Return codes the caller owns the mailbox command after the return of the
7566 * function.
7567 **/
7568 int
7569 lpfc_sli_issue_mbox(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmbox, uint32_t flag)
7570 {
7571 return phba->lpfc_sli_issue_mbox(phba, pmbox, flag);
7572 }
7573
7574 /**
7575 * lpfc_mbox_api_table_setup - Set up mbox api function jump table
7576 * @phba: The hba struct for which this call is being executed.
7577 * @dev_grp: The HBA PCI-Device group number.
7578 *
7579 * This routine sets up the mbox interface API function jump table in @phba
7580 * struct.
7581 * Returns: 0 - success, -ENODEV - failure.
7582 **/
7583 int
7584 lpfc_mbox_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
7585 {
7586
7587 switch (dev_grp) {
7588 case LPFC_PCI_DEV_LP:
7589 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s3;
7590 phba->lpfc_sli_handle_slow_ring_event =
7591 lpfc_sli_handle_slow_ring_event_s3;
7592 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s3;
7593 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s3;
7594 phba->lpfc_sli_brdready = lpfc_sli_brdready_s3;
7595 break;
7596 case LPFC_PCI_DEV_OC:
7597 phba->lpfc_sli_issue_mbox = lpfc_sli_issue_mbox_s4;
7598 phba->lpfc_sli_handle_slow_ring_event =
7599 lpfc_sli_handle_slow_ring_event_s4;
7600 phba->lpfc_sli_hbq_to_firmware = lpfc_sli_hbq_to_firmware_s4;
7601 phba->lpfc_sli_brdrestart = lpfc_sli_brdrestart_s4;
7602 phba->lpfc_sli_brdready = lpfc_sli_brdready_s4;
7603 break;
7604 default:
7605 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7606 "1420 Invalid HBA PCI-device group: 0x%x\n",
7607 dev_grp);
7608 return -ENODEV;
7609 break;
7610 }
7611 return 0;
7612 }
7613
7614 /**
7615 * __lpfc_sli_ringtx_put - Add an iocb to the txq
7616 * @phba: Pointer to HBA context object.
7617 * @pring: Pointer to driver SLI ring object.
7618 * @piocb: Pointer to address of newly added command iocb.
7619 *
7620 * This function is called with hbalock held to add a command
7621 * iocb to the txq when SLI layer cannot submit the command iocb
7622 * to the ring.
7623 **/
7624 void
7625 __lpfc_sli_ringtx_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7626 struct lpfc_iocbq *piocb)
7627 {
7628 /* Insert the caller's iocb in the txq tail for later processing. */
7629 list_add_tail(&piocb->list, &pring->txq);
7630 }
7631
7632 /**
7633 * lpfc_sli_next_iocb - Get the next iocb in the txq
7634 * @phba: Pointer to HBA context object.
7635 * @pring: Pointer to driver SLI ring object.
7636 * @piocb: Pointer to address of newly added command iocb.
7637 *
7638 * This function is called with hbalock held before a new
7639 * iocb is submitted to the firmware. This function checks
7640 * txq to flush the iocbs in txq to Firmware before
7641 * submitting new iocbs to the Firmware.
7642 * If there are iocbs in the txq which need to be submitted
7643 * to firmware, lpfc_sli_next_iocb returns the first element
7644 * of the txq after dequeuing it from txq.
7645 * If there is no iocb in the txq then the function will return
7646 * *piocb and *piocb is set to NULL. Caller needs to check
7647 * *piocb to find if there are more commands in the txq.
7648 **/
7649 static struct lpfc_iocbq *
7650 lpfc_sli_next_iocb(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
7651 struct lpfc_iocbq **piocb)
7652 {
7653 struct lpfc_iocbq * nextiocb;
7654
7655 nextiocb = lpfc_sli_ringtx_get(phba, pring);
7656 if (!nextiocb) {
7657 nextiocb = *piocb;
7658 *piocb = NULL;
7659 }
7660
7661 return nextiocb;
7662 }
7663
7664 /**
7665 * __lpfc_sli_issue_iocb_s3 - SLI3 device lockless ver of lpfc_sli_issue_iocb
7666 * @phba: Pointer to HBA context object.
7667 * @ring_number: SLI ring number to issue iocb on.
7668 * @piocb: Pointer to command iocb.
7669 * @flag: Flag indicating if this command can be put into txq.
7670 *
7671 * __lpfc_sli_issue_iocb_s3 is used by other functions in the driver to issue
7672 * an iocb command to an HBA with SLI-3 interface spec. If the PCI slot is
7673 * recovering from error state, if HBA is resetting or if LPFC_STOP_IOCB_EVENT
7674 * flag is turned on, the function returns IOCB_ERROR. When the link is down,
7675 * this function allows only iocbs for posting buffers. This function finds
7676 * next available slot in the command ring and posts the command to the
7677 * available slot and writes the port attention register to request HBA start
7678 * processing new iocb. If there is no slot available in the ring and
7679 * flag & SLI_IOCB_RET_IOCB is set, the new iocb is added to the txq, otherwise
7680 * the function returns IOCB_BUSY.
7681 *
7682 * This function is called with hbalock held. The function will return success
7683 * after it successfully submit the iocb to firmware or after adding to the
7684 * txq.
7685 **/
7686 static int
7687 __lpfc_sli_issue_iocb_s3(struct lpfc_hba *phba, uint32_t ring_number,
7688 struct lpfc_iocbq *piocb, uint32_t flag)
7689 {
7690 struct lpfc_iocbq *nextiocb;
7691 IOCB_t *iocb;
7692 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
7693
7694 if (piocb->iocb_cmpl && (!piocb->vport) &&
7695 (piocb->iocb.ulpCommand != CMD_ABORT_XRI_CN) &&
7696 (piocb->iocb.ulpCommand != CMD_CLOSE_XRI_CN)) {
7697 lpfc_printf_log(phba, KERN_ERR,
7698 LOG_SLI | LOG_VPORT,
7699 "1807 IOCB x%x failed. No vport\n",
7700 piocb->iocb.ulpCommand);
7701 dump_stack();
7702 return IOCB_ERROR;
7703 }
7704
7705
7706 /* If the PCI channel is in offline state, do not post iocbs. */
7707 if (unlikely(pci_channel_offline(phba->pcidev)))
7708 return IOCB_ERROR;
7709
7710 /* If HBA has a deferred error attention, fail the iocb. */
7711 if (unlikely(phba->hba_flag & DEFER_ERATT))
7712 return IOCB_ERROR;
7713
7714 /*
7715 * We should never get an IOCB if we are in a < LINK_DOWN state
7716 */
7717 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
7718 return IOCB_ERROR;
7719
7720 /*
7721 * Check to see if we are blocking IOCB processing because of a
7722 * outstanding event.
7723 */
7724 if (unlikely(pring->flag & LPFC_STOP_IOCB_EVENT))
7725 goto iocb_busy;
7726
7727 if (unlikely(phba->link_state == LPFC_LINK_DOWN)) {
7728 /*
7729 * Only CREATE_XRI, CLOSE_XRI, and QUE_RING_BUF
7730 * can be issued if the link is not up.
7731 */
7732 switch (piocb->iocb.ulpCommand) {
7733 case CMD_GEN_REQUEST64_CR:
7734 case CMD_GEN_REQUEST64_CX:
7735 if (!(phba->sli.sli_flag & LPFC_MENLO_MAINT) ||
7736 (piocb->iocb.un.genreq64.w5.hcsw.Rctl !=
7737 FC_RCTL_DD_UNSOL_CMD) ||
7738 (piocb->iocb.un.genreq64.w5.hcsw.Type !=
7739 MENLO_TRANSPORT_TYPE))
7740
7741 goto iocb_busy;
7742 break;
7743 case CMD_QUE_RING_BUF_CN:
7744 case CMD_QUE_RING_BUF64_CN:
7745 /*
7746 * For IOCBs, like QUE_RING_BUF, that have no rsp ring
7747 * completion, iocb_cmpl MUST be 0.
7748 */
7749 if (piocb->iocb_cmpl)
7750 piocb->iocb_cmpl = NULL;
7751 /*FALLTHROUGH*/
7752 case CMD_CREATE_XRI_CR:
7753 case CMD_CLOSE_XRI_CN:
7754 case CMD_CLOSE_XRI_CX:
7755 break;
7756 default:
7757 goto iocb_busy;
7758 }
7759
7760 /*
7761 * For FCP commands, we must be in a state where we can process link
7762 * attention events.
7763 */
7764 } else if (unlikely(pring->ringno == phba->sli.fcp_ring &&
7765 !(phba->sli.sli_flag & LPFC_PROCESS_LA))) {
7766 goto iocb_busy;
7767 }
7768
7769 while ((iocb = lpfc_sli_next_iocb_slot(phba, pring)) &&
7770 (nextiocb = lpfc_sli_next_iocb(phba, pring, &piocb)))
7771 lpfc_sli_submit_iocb(phba, pring, iocb, nextiocb);
7772
7773 if (iocb)
7774 lpfc_sli_update_ring(phba, pring);
7775 else
7776 lpfc_sli_update_full_ring(phba, pring);
7777
7778 if (!piocb)
7779 return IOCB_SUCCESS;
7780
7781 goto out_busy;
7782
7783 iocb_busy:
7784 pring->stats.iocb_cmd_delay++;
7785
7786 out_busy:
7787
7788 if (!(flag & SLI_IOCB_RET_IOCB)) {
7789 __lpfc_sli_ringtx_put(phba, pring, piocb);
7790 return IOCB_SUCCESS;
7791 }
7792
7793 return IOCB_BUSY;
7794 }
7795
7796 /**
7797 * lpfc_sli4_bpl2sgl - Convert the bpl/bde to a sgl.
7798 * @phba: Pointer to HBA context object.
7799 * @piocb: Pointer to command iocb.
7800 * @sglq: Pointer to the scatter gather queue object.
7801 *
7802 * This routine converts the bpl or bde that is in the IOCB
7803 * to a sgl list for the sli4 hardware. The physical address
7804 * of the bpl/bde is converted back to a virtual address.
7805 * If the IOCB contains a BPL then the list of BDE's is
7806 * converted to sli4_sge's. If the IOCB contains a single
7807 * BDE then it is converted to a single sli_sge.
7808 * The IOCB is still in cpu endianess so the contents of
7809 * the bpl can be used without byte swapping.
7810 *
7811 * Returns valid XRI = Success, NO_XRI = Failure.
7812 **/
7813 static uint16_t
7814 lpfc_sli4_bpl2sgl(struct lpfc_hba *phba, struct lpfc_iocbq *piocbq,
7815 struct lpfc_sglq *sglq)
7816 {
7817 uint16_t xritag = NO_XRI;
7818 struct ulp_bde64 *bpl = NULL;
7819 struct ulp_bde64 bde;
7820 struct sli4_sge *sgl = NULL;
7821 struct lpfc_dmabuf *dmabuf;
7822 IOCB_t *icmd;
7823 int numBdes = 0;
7824 int i = 0;
7825 uint32_t offset = 0; /* accumulated offset in the sg request list */
7826 int inbound = 0; /* number of sg reply entries inbound from firmware */
7827
7828 if (!piocbq || !sglq)
7829 return xritag;
7830
7831 sgl = (struct sli4_sge *)sglq->sgl;
7832 icmd = &piocbq->iocb;
7833 if (icmd->ulpCommand == CMD_XMIT_BLS_RSP64_CX)
7834 return sglq->sli4_xritag;
7835 if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7836 numBdes = icmd->un.genreq64.bdl.bdeSize /
7837 sizeof(struct ulp_bde64);
7838 /* The addrHigh and addrLow fields within the IOCB
7839 * have not been byteswapped yet so there is no
7840 * need to swap them back.
7841 */
7842 if (piocbq->context3)
7843 dmabuf = (struct lpfc_dmabuf *)piocbq->context3;
7844 else
7845 return xritag;
7846
7847 bpl = (struct ulp_bde64 *)dmabuf->virt;
7848 if (!bpl)
7849 return xritag;
7850
7851 for (i = 0; i < numBdes; i++) {
7852 /* Should already be byte swapped. */
7853 sgl->addr_hi = bpl->addrHigh;
7854 sgl->addr_lo = bpl->addrLow;
7855
7856 sgl->word2 = le32_to_cpu(sgl->word2);
7857 if ((i+1) == numBdes)
7858 bf_set(lpfc_sli4_sge_last, sgl, 1);
7859 else
7860 bf_set(lpfc_sli4_sge_last, sgl, 0);
7861 /* swap the size field back to the cpu so we
7862 * can assign it to the sgl.
7863 */
7864 bde.tus.w = le32_to_cpu(bpl->tus.w);
7865 sgl->sge_len = cpu_to_le32(bde.tus.f.bdeSize);
7866 /* The offsets in the sgl need to be accumulated
7867 * separately for the request and reply lists.
7868 * The request is always first, the reply follows.
7869 */
7870 if (piocbq->iocb.ulpCommand == CMD_GEN_REQUEST64_CR) {
7871 /* add up the reply sg entries */
7872 if (bpl->tus.f.bdeFlags == BUFF_TYPE_BDE_64I)
7873 inbound++;
7874 /* first inbound? reset the offset */
7875 if (inbound == 1)
7876 offset = 0;
7877 bf_set(lpfc_sli4_sge_offset, sgl, offset);
7878 bf_set(lpfc_sli4_sge_type, sgl,
7879 LPFC_SGE_TYPE_DATA);
7880 offset += bde.tus.f.bdeSize;
7881 }
7882 sgl->word2 = cpu_to_le32(sgl->word2);
7883 bpl++;
7884 sgl++;
7885 }
7886 } else if (icmd->un.genreq64.bdl.bdeFlags == BUFF_TYPE_BDE_64) {
7887 /* The addrHigh and addrLow fields of the BDE have not
7888 * been byteswapped yet so they need to be swapped
7889 * before putting them in the sgl.
7890 */
7891 sgl->addr_hi =
7892 cpu_to_le32(icmd->un.genreq64.bdl.addrHigh);
7893 sgl->addr_lo =
7894 cpu_to_le32(icmd->un.genreq64.bdl.addrLow);
7895 sgl->word2 = le32_to_cpu(sgl->word2);
7896 bf_set(lpfc_sli4_sge_last, sgl, 1);
7897 sgl->word2 = cpu_to_le32(sgl->word2);
7898 sgl->sge_len =
7899 cpu_to_le32(icmd->un.genreq64.bdl.bdeSize);
7900 }
7901 return sglq->sli4_xritag;
7902 }
7903
7904 /**
7905 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
7906 * @phba: Pointer to HBA context object.
7907 *
7908 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
7909 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
7910 * held.
7911 *
7912 * Return: index into SLI4 fast-path FCP queue index.
7913 **/
7914 static inline uint32_t
7915 lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba)
7916 {
7917 int i;
7918
7919 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU)
7920 i = smp_processor_id();
7921 else
7922 i = atomic_add_return(1, &phba->fcp_qidx);
7923
7924 i = (i % phba->cfg_fcp_io_channel);
7925 return i;
7926 }
7927
7928 /**
7929 * lpfc_sli_iocb2wqe - Convert the IOCB to a work queue entry.
7930 * @phba: Pointer to HBA context object.
7931 * @piocb: Pointer to command iocb.
7932 * @wqe: Pointer to the work queue entry.
7933 *
7934 * This routine converts the iocb command to its Work Queue Entry
7935 * equivalent. The wqe pointer should not have any fields set when
7936 * this routine is called because it will memcpy over them.
7937 * This routine does not set the CQ_ID or the WQEC bits in the
7938 * wqe.
7939 *
7940 * Returns: 0 = Success, IOCB_ERROR = Failure.
7941 **/
7942 static int
7943 lpfc_sli4_iocb2wqe(struct lpfc_hba *phba, struct lpfc_iocbq *iocbq,
7944 union lpfc_wqe *wqe)
7945 {
7946 uint32_t xmit_len = 0, total_len = 0;
7947 uint8_t ct = 0;
7948 uint32_t fip;
7949 uint32_t abort_tag;
7950 uint8_t command_type = ELS_COMMAND_NON_FIP;
7951 uint8_t cmnd;
7952 uint16_t xritag;
7953 uint16_t abrt_iotag;
7954 struct lpfc_iocbq *abrtiocbq;
7955 struct ulp_bde64 *bpl = NULL;
7956 uint32_t els_id = LPFC_ELS_ID_DEFAULT;
7957 int numBdes, i;
7958 struct ulp_bde64 bde;
7959 struct lpfc_nodelist *ndlp;
7960 uint32_t *pcmd;
7961 uint32_t if_type;
7962
7963 fip = phba->hba_flag & HBA_FIP_SUPPORT;
7964 /* The fcp commands will set command type */
7965 if (iocbq->iocb_flag & LPFC_IO_FCP)
7966 command_type = FCP_COMMAND;
7967 else if (fip && (iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK))
7968 command_type = ELS_COMMAND_FIP;
7969 else
7970 command_type = ELS_COMMAND_NON_FIP;
7971
7972 /* Some of the fields are in the right position already */
7973 memcpy(wqe, &iocbq->iocb, sizeof(union lpfc_wqe));
7974 abort_tag = (uint32_t) iocbq->iotag;
7975 xritag = iocbq->sli4_xritag;
7976 wqe->generic.wqe_com.word7 = 0; /* The ct field has moved so reset */
7977 /* words0-2 bpl convert bde */
7978 if (iocbq->iocb.un.genreq64.bdl.bdeFlags == BUFF_TYPE_BLP_64) {
7979 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
7980 sizeof(struct ulp_bde64);
7981 bpl = (struct ulp_bde64 *)
7982 ((struct lpfc_dmabuf *)iocbq->context3)->virt;
7983 if (!bpl)
7984 return IOCB_ERROR;
7985
7986 /* Should already be byte swapped. */
7987 wqe->generic.bde.addrHigh = le32_to_cpu(bpl->addrHigh);
7988 wqe->generic.bde.addrLow = le32_to_cpu(bpl->addrLow);
7989 /* swap the size field back to the cpu so we
7990 * can assign it to the sgl.
7991 */
7992 wqe->generic.bde.tus.w = le32_to_cpu(bpl->tus.w);
7993 xmit_len = wqe->generic.bde.tus.f.bdeSize;
7994 total_len = 0;
7995 for (i = 0; i < numBdes; i++) {
7996 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
7997 total_len += bde.tus.f.bdeSize;
7998 }
7999 } else
8000 xmit_len = iocbq->iocb.un.fcpi64.bdl.bdeSize;
8001
8002 iocbq->iocb.ulpIoTag = iocbq->iotag;
8003 cmnd = iocbq->iocb.ulpCommand;
8004
8005 switch (iocbq->iocb.ulpCommand) {
8006 case CMD_ELS_REQUEST64_CR:
8007 if (iocbq->iocb_flag & LPFC_IO_LIBDFC)
8008 ndlp = iocbq->context_un.ndlp;
8009 else
8010 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8011 if (!iocbq->iocb.ulpLe) {
8012 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8013 "2007 Only Limited Edition cmd Format"
8014 " supported 0x%x\n",
8015 iocbq->iocb.ulpCommand);
8016 return IOCB_ERROR;
8017 }
8018
8019 wqe->els_req.payload_len = xmit_len;
8020 /* Els_reguest64 has a TMO */
8021 bf_set(wqe_tmo, &wqe->els_req.wqe_com,
8022 iocbq->iocb.ulpTimeout);
8023 /* Need a VF for word 4 set the vf bit*/
8024 bf_set(els_req64_vf, &wqe->els_req, 0);
8025 /* And a VFID for word 12 */
8026 bf_set(els_req64_vfid, &wqe->els_req, 0);
8027 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8028 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8029 iocbq->iocb.ulpContext);
8030 bf_set(wqe_ct, &wqe->els_req.wqe_com, ct);
8031 bf_set(wqe_pu, &wqe->els_req.wqe_com, 0);
8032 /* CCP CCPE PV PRI in word10 were set in the memcpy */
8033 if (command_type == ELS_COMMAND_FIP)
8034 els_id = ((iocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK)
8035 >> LPFC_FIP_ELS_ID_SHIFT);
8036 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8037 iocbq->context2)->virt);
8038 if_type = bf_get(lpfc_sli_intf_if_type,
8039 &phba->sli4_hba.sli_intf);
8040 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8041 if (pcmd && (*pcmd == ELS_CMD_FLOGI ||
8042 *pcmd == ELS_CMD_SCR ||
8043 *pcmd == ELS_CMD_FDISC ||
8044 *pcmd == ELS_CMD_LOGO ||
8045 *pcmd == ELS_CMD_PLOGI)) {
8046 bf_set(els_req64_sp, &wqe->els_req, 1);
8047 bf_set(els_req64_sid, &wqe->els_req,
8048 iocbq->vport->fc_myDID);
8049 if ((*pcmd == ELS_CMD_FLOGI) &&
8050 !(phba->fc_topology ==
8051 LPFC_TOPOLOGY_LOOP))
8052 bf_set(els_req64_sid, &wqe->els_req, 0);
8053 bf_set(wqe_ct, &wqe->els_req.wqe_com, 1);
8054 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8055 phba->vpi_ids[iocbq->vport->vpi]);
8056 } else if (pcmd && iocbq->context1) {
8057 bf_set(wqe_ct, &wqe->els_req.wqe_com, 0);
8058 bf_set(wqe_ctxt_tag, &wqe->els_req.wqe_com,
8059 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8060 }
8061 }
8062 bf_set(wqe_temp_rpi, &wqe->els_req.wqe_com,
8063 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8064 bf_set(wqe_els_id, &wqe->els_req.wqe_com, els_id);
8065 bf_set(wqe_dbde, &wqe->els_req.wqe_com, 1);
8066 bf_set(wqe_iod, &wqe->els_req.wqe_com, LPFC_WQE_IOD_READ);
8067 bf_set(wqe_qosd, &wqe->els_req.wqe_com, 1);
8068 bf_set(wqe_lenloc, &wqe->els_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8069 bf_set(wqe_ebde_cnt, &wqe->els_req.wqe_com, 0);
8070 break;
8071 case CMD_XMIT_SEQUENCE64_CX:
8072 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
8073 iocbq->iocb.un.ulpWord[3]);
8074 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com,
8075 iocbq->iocb.unsli3.rcvsli3.ox_id);
8076 /* The entire sequence is transmitted for this IOCB */
8077 xmit_len = total_len;
8078 cmnd = CMD_XMIT_SEQUENCE64_CR;
8079 if (phba->link_flag & LS_LOOPBACK_MODE)
8080 bf_set(wqe_xo, &wqe->xmit_sequence.wge_ctl, 1);
8081 case CMD_XMIT_SEQUENCE64_CR:
8082 /* word3 iocb=io_tag32 wqe=reserved */
8083 wqe->xmit_sequence.rsvd3 = 0;
8084 /* word4 relative_offset memcpy */
8085 /* word5 r_ctl/df_ctl memcpy */
8086 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
8087 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
8088 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com,
8089 LPFC_WQE_IOD_WRITE);
8090 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
8091 LPFC_WQE_LENLOC_WORD12);
8092 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
8093 wqe->xmit_sequence.xmit_len = xmit_len;
8094 command_type = OTHER_COMMAND;
8095 break;
8096 case CMD_XMIT_BCAST64_CN:
8097 /* word3 iocb=iotag32 wqe=seq_payload_len */
8098 wqe->xmit_bcast64.seq_payload_len = xmit_len;
8099 /* word4 iocb=rsvd wqe=rsvd */
8100 /* word5 iocb=rctl/type/df_ctl wqe=rctl/type/df_ctl memcpy */
8101 /* word6 iocb=ctxt_tag/io_tag wqe=ctxt_tag/xri */
8102 bf_set(wqe_ct, &wqe->xmit_bcast64.wqe_com,
8103 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8104 bf_set(wqe_dbde, &wqe->xmit_bcast64.wqe_com, 1);
8105 bf_set(wqe_iod, &wqe->xmit_bcast64.wqe_com, LPFC_WQE_IOD_WRITE);
8106 bf_set(wqe_lenloc, &wqe->xmit_bcast64.wqe_com,
8107 LPFC_WQE_LENLOC_WORD3);
8108 bf_set(wqe_ebde_cnt, &wqe->xmit_bcast64.wqe_com, 0);
8109 break;
8110 case CMD_FCP_IWRITE64_CR:
8111 command_type = FCP_COMMAND_DATA_OUT;
8112 /* word3 iocb=iotag wqe=payload_offset_len */
8113 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8114 wqe->fcp_iwrite.payload_offset_len =
8115 xmit_len + sizeof(struct fcp_rsp);
8116 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8117 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8118 bf_set(wqe_erp, &wqe->fcp_iwrite.wqe_com,
8119 iocbq->iocb.ulpFCP2Rcvy);
8120 bf_set(wqe_lnk, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpXS);
8121 /* Always open the exchange */
8122 bf_set(wqe_xc, &wqe->fcp_iwrite.wqe_com, 0);
8123 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
8124 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
8125 LPFC_WQE_LENLOC_WORD4);
8126 bf_set(wqe_ebde_cnt, &wqe->fcp_iwrite.wqe_com, 0);
8127 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, iocbq->iocb.ulpPU);
8128 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 1);
8129 break;
8130 case CMD_FCP_IREAD64_CR:
8131 /* word3 iocb=iotag wqe=payload_offset_len */
8132 /* Add the FCP_CMD and FCP_RSP sizes to get the offset */
8133 wqe->fcp_iread.payload_offset_len =
8134 xmit_len + sizeof(struct fcp_rsp);
8135 /* word4 iocb=parameter wqe=total_xfer_length memcpy */
8136 /* word5 iocb=initial_xfer_len wqe=initial_xfer_len memcpy */
8137 bf_set(wqe_erp, &wqe->fcp_iread.wqe_com,
8138 iocbq->iocb.ulpFCP2Rcvy);
8139 bf_set(wqe_lnk, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpXS);
8140 /* Always open the exchange */
8141 bf_set(wqe_xc, &wqe->fcp_iread.wqe_com, 0);
8142 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
8143 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
8144 LPFC_WQE_LENLOC_WORD4);
8145 bf_set(wqe_ebde_cnt, &wqe->fcp_iread.wqe_com, 0);
8146 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, iocbq->iocb.ulpPU);
8147 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 1);
8148 break;
8149 case CMD_FCP_ICMND64_CR:
8150 /* word3 iocb=IO_TAG wqe=reserved */
8151 wqe->fcp_icmd.rsrvd3 = 0;
8152 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
8153 /* Always open the exchange */
8154 bf_set(wqe_xc, &wqe->fcp_icmd.wqe_com, 0);
8155 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 1);
8156 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
8157 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
8158 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
8159 LPFC_WQE_LENLOC_NONE);
8160 bf_set(wqe_ebde_cnt, &wqe->fcp_icmd.wqe_com, 0);
8161 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
8162 iocbq->iocb.ulpFCP2Rcvy);
8163 break;
8164 case CMD_GEN_REQUEST64_CR:
8165 /* For this command calculate the xmit length of the
8166 * request bde.
8167 */
8168 xmit_len = 0;
8169 numBdes = iocbq->iocb.un.genreq64.bdl.bdeSize /
8170 sizeof(struct ulp_bde64);
8171 for (i = 0; i < numBdes; i++) {
8172 bde.tus.w = le32_to_cpu(bpl[i].tus.w);
8173 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
8174 break;
8175 xmit_len += bde.tus.f.bdeSize;
8176 }
8177 /* word3 iocb=IO_TAG wqe=request_payload_len */
8178 wqe->gen_req.request_payload_len = xmit_len;
8179 /* word4 iocb=parameter wqe=relative_offset memcpy */
8180 /* word5 [rctl, type, df_ctl, la] copied in memcpy */
8181 /* word6 context tag copied in memcpy */
8182 if (iocbq->iocb.ulpCt_h || iocbq->iocb.ulpCt_l) {
8183 ct = ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l);
8184 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8185 "2015 Invalid CT %x command 0x%x\n",
8186 ct, iocbq->iocb.ulpCommand);
8187 return IOCB_ERROR;
8188 }
8189 bf_set(wqe_ct, &wqe->gen_req.wqe_com, 0);
8190 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, iocbq->iocb.ulpTimeout);
8191 bf_set(wqe_pu, &wqe->gen_req.wqe_com, iocbq->iocb.ulpPU);
8192 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
8193 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
8194 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
8195 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
8196 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
8197 command_type = OTHER_COMMAND;
8198 break;
8199 case CMD_XMIT_ELS_RSP64_CX:
8200 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8201 /* words0-2 BDE memcpy */
8202 /* word3 iocb=iotag32 wqe=response_payload_len */
8203 wqe->xmit_els_rsp.response_payload_len = xmit_len;
8204 /* word4 */
8205 wqe->xmit_els_rsp.word4 = 0;
8206 /* word5 iocb=rsvd wge=did */
8207 bf_set(wqe_els_did, &wqe->xmit_els_rsp.wqe_dest,
8208 iocbq->iocb.un.xseq64.xmit_els_remoteID);
8209
8210 if_type = bf_get(lpfc_sli_intf_if_type,
8211 &phba->sli4_hba.sli_intf);
8212 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
8213 if (iocbq->vport->fc_flag & FC_PT2PT) {
8214 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8215 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8216 iocbq->vport->fc_myDID);
8217 if (iocbq->vport->fc_myDID == Fabric_DID) {
8218 bf_set(wqe_els_did,
8219 &wqe->xmit_els_rsp.wqe_dest, 0);
8220 }
8221 }
8222 }
8223 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com,
8224 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8225 bf_set(wqe_pu, &wqe->xmit_els_rsp.wqe_com, iocbq->iocb.ulpPU);
8226 bf_set(wqe_rcvoxid, &wqe->xmit_els_rsp.wqe_com,
8227 iocbq->iocb.unsli3.rcvsli3.ox_id);
8228 if (!iocbq->iocb.ulpCt_h && iocbq->iocb.ulpCt_l)
8229 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8230 phba->vpi_ids[iocbq->vport->vpi]);
8231 bf_set(wqe_dbde, &wqe->xmit_els_rsp.wqe_com, 1);
8232 bf_set(wqe_iod, &wqe->xmit_els_rsp.wqe_com, LPFC_WQE_IOD_WRITE);
8233 bf_set(wqe_qosd, &wqe->xmit_els_rsp.wqe_com, 1);
8234 bf_set(wqe_lenloc, &wqe->xmit_els_rsp.wqe_com,
8235 LPFC_WQE_LENLOC_WORD3);
8236 bf_set(wqe_ebde_cnt, &wqe->xmit_els_rsp.wqe_com, 0);
8237 bf_set(wqe_rsp_temp_rpi, &wqe->xmit_els_rsp,
8238 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
8239 pcmd = (uint32_t *) (((struct lpfc_dmabuf *)
8240 iocbq->context2)->virt);
8241 if (phba->fc_topology == LPFC_TOPOLOGY_LOOP) {
8242 bf_set(els_rsp64_sp, &wqe->xmit_els_rsp, 1);
8243 bf_set(els_rsp64_sid, &wqe->xmit_els_rsp,
8244 iocbq->vport->fc_myDID);
8245 bf_set(wqe_ct, &wqe->xmit_els_rsp.wqe_com, 1);
8246 bf_set(wqe_ctxt_tag, &wqe->xmit_els_rsp.wqe_com,
8247 phba->vpi_ids[phba->pport->vpi]);
8248 }
8249 command_type = OTHER_COMMAND;
8250 break;
8251 case CMD_CLOSE_XRI_CN:
8252 case CMD_ABORT_XRI_CN:
8253 case CMD_ABORT_XRI_CX:
8254 /* words 0-2 memcpy should be 0 rserved */
8255 /* port will send abts */
8256 abrt_iotag = iocbq->iocb.un.acxri.abortContextTag;
8257 if (abrt_iotag != 0 && abrt_iotag <= phba->sli.last_iotag) {
8258 abrtiocbq = phba->sli.iocbq_lookup[abrt_iotag];
8259 fip = abrtiocbq->iocb_flag & LPFC_FIP_ELS_ID_MASK;
8260 } else
8261 fip = 0;
8262
8263 if ((iocbq->iocb.ulpCommand == CMD_CLOSE_XRI_CN) || fip)
8264 /*
8265 * The link is down, or the command was ELS_FIP
8266 * so the fw does not need to send abts
8267 * on the wire.
8268 */
8269 bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
8270 else
8271 bf_set(abort_cmd_ia, &wqe->abort_cmd, 0);
8272 bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
8273 /* word5 iocb=CONTEXT_TAG|IO_TAG wqe=reserved */
8274 wqe->abort_cmd.rsrvd5 = 0;
8275 bf_set(wqe_ct, &wqe->abort_cmd.wqe_com,
8276 ((iocbq->iocb.ulpCt_h << 1) | iocbq->iocb.ulpCt_l));
8277 abort_tag = iocbq->iocb.un.acxri.abortIoTag;
8278 /*
8279 * The abort handler will send us CMD_ABORT_XRI_CN or
8280 * CMD_CLOSE_XRI_CN and the fw only accepts CMD_ABORT_XRI_CX
8281 */
8282 bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
8283 bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
8284 bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com,
8285 LPFC_WQE_LENLOC_NONE);
8286 cmnd = CMD_ABORT_XRI_CX;
8287 command_type = OTHER_COMMAND;
8288 xritag = 0;
8289 break;
8290 case CMD_XMIT_BLS_RSP64_CX:
8291 ndlp = (struct lpfc_nodelist *)iocbq->context1;
8292 /* As BLS ABTS RSP WQE is very different from other WQEs,
8293 * we re-construct this WQE here based on information in
8294 * iocbq from scratch.
8295 */
8296 memset(wqe, 0, sizeof(union lpfc_wqe));
8297 /* OX_ID is invariable to who sent ABTS to CT exchange */
8298 bf_set(xmit_bls_rsp64_oxid, &wqe->xmit_bls_rsp,
8299 bf_get(lpfc_abts_oxid, &iocbq->iocb.un.bls_rsp));
8300 if (bf_get(lpfc_abts_orig, &iocbq->iocb.un.bls_rsp) ==
8301 LPFC_ABTS_UNSOL_INT) {
8302 /* ABTS sent by initiator to CT exchange, the
8303 * RX_ID field will be filled with the newly
8304 * allocated responder XRI.
8305 */
8306 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8307 iocbq->sli4_xritag);
8308 } else {
8309 /* ABTS sent by responder to CT exchange, the
8310 * RX_ID field will be filled with the responder
8311 * RX_ID from ABTS.
8312 */
8313 bf_set(xmit_bls_rsp64_rxid, &wqe->xmit_bls_rsp,
8314 bf_get(lpfc_abts_rxid, &iocbq->iocb.un.bls_rsp));
8315 }
8316 bf_set(xmit_bls_rsp64_seqcnthi, &wqe->xmit_bls_rsp, 0xffff);
8317 bf_set(wqe_xmit_bls_pt, &wqe->xmit_bls_rsp.wqe_dest, 0x1);
8318
8319 /* Use CT=VPI */
8320 bf_set(wqe_els_did, &wqe->xmit_bls_rsp.wqe_dest,
8321 ndlp->nlp_DID);
8322 bf_set(xmit_bls_rsp64_temprpi, &wqe->xmit_bls_rsp,
8323 iocbq->iocb.ulpContext);
8324 bf_set(wqe_ct, &wqe->xmit_bls_rsp.wqe_com, 1);
8325 bf_set(wqe_ctxt_tag, &wqe->xmit_bls_rsp.wqe_com,
8326 phba->vpi_ids[phba->pport->vpi]);
8327 bf_set(wqe_qosd, &wqe->xmit_bls_rsp.wqe_com, 1);
8328 bf_set(wqe_lenloc, &wqe->xmit_bls_rsp.wqe_com,
8329 LPFC_WQE_LENLOC_NONE);
8330 /* Overwrite the pre-set comnd type with OTHER_COMMAND */
8331 command_type = OTHER_COMMAND;
8332 if (iocbq->iocb.un.xseq64.w5.hcsw.Rctl == FC_RCTL_BA_RJT) {
8333 bf_set(xmit_bls_rsp64_rjt_vspec, &wqe->xmit_bls_rsp,
8334 bf_get(lpfc_vndr_code, &iocbq->iocb.un.bls_rsp));
8335 bf_set(xmit_bls_rsp64_rjt_expc, &wqe->xmit_bls_rsp,
8336 bf_get(lpfc_rsn_expln, &iocbq->iocb.un.bls_rsp));
8337 bf_set(xmit_bls_rsp64_rjt_rsnc, &wqe->xmit_bls_rsp,
8338 bf_get(lpfc_rsn_code, &iocbq->iocb.un.bls_rsp));
8339 }
8340
8341 break;
8342 case CMD_XRI_ABORTED_CX:
8343 case CMD_CREATE_XRI_CR: /* Do we expect to use this? */
8344 case CMD_IOCB_FCP_IBIDIR64_CR: /* bidirectional xfer */
8345 case CMD_FCP_TSEND64_CX: /* Target mode send xfer-ready */
8346 case CMD_FCP_TRSP64_CX: /* Target mode rcv */
8347 case CMD_FCP_AUTO_TRSP_CX: /* Auto target rsp */
8348 default:
8349 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8350 "2014 Invalid command 0x%x\n",
8351 iocbq->iocb.ulpCommand);
8352 return IOCB_ERROR;
8353 break;
8354 }
8355
8356 if (iocbq->iocb_flag & LPFC_IO_DIF_PASS)
8357 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
8358 else if (iocbq->iocb_flag & LPFC_IO_DIF_STRIP)
8359 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
8360 else if (iocbq->iocb_flag & LPFC_IO_DIF_INSERT)
8361 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
8362 iocbq->iocb_flag &= ~(LPFC_IO_DIF_PASS | LPFC_IO_DIF_STRIP |
8363 LPFC_IO_DIF_INSERT);
8364 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, xritag);
8365 bf_set(wqe_reqtag, &wqe->generic.wqe_com, iocbq->iotag);
8366 wqe->generic.wqe_com.abort_tag = abort_tag;
8367 bf_set(wqe_cmd_type, &wqe->generic.wqe_com, command_type);
8368 bf_set(wqe_cmnd, &wqe->generic.wqe_com, cmnd);
8369 bf_set(wqe_class, &wqe->generic.wqe_com, iocbq->iocb.ulpClass);
8370 bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
8371 return 0;
8372 }
8373
8374 /**
8375 * __lpfc_sli_issue_iocb_s4 - SLI4 device lockless ver of lpfc_sli_issue_iocb
8376 * @phba: Pointer to HBA context object.
8377 * @ring_number: SLI ring number to issue iocb on.
8378 * @piocb: Pointer to command iocb.
8379 * @flag: Flag indicating if this command can be put into txq.
8380 *
8381 * __lpfc_sli_issue_iocb_s4 is used by other functions in the driver to issue
8382 * an iocb command to an HBA with SLI-4 interface spec.
8383 *
8384 * This function is called with hbalock held. The function will return success
8385 * after it successfully submit the iocb to firmware or after adding to the
8386 * txq.
8387 **/
8388 static int
8389 __lpfc_sli_issue_iocb_s4(struct lpfc_hba *phba, uint32_t ring_number,
8390 struct lpfc_iocbq *piocb, uint32_t flag)
8391 {
8392 struct lpfc_sglq *sglq;
8393 union lpfc_wqe wqe;
8394 struct lpfc_sli_ring *pring = &phba->sli.ring[ring_number];
8395
8396 if (piocb->sli4_xritag == NO_XRI) {
8397 if (piocb->iocb.ulpCommand == CMD_ABORT_XRI_CN ||
8398 piocb->iocb.ulpCommand == CMD_CLOSE_XRI_CN)
8399 sglq = NULL;
8400 else {
8401 if (!list_empty(&pring->txq)) {
8402 if (!(flag & SLI_IOCB_RET_IOCB)) {
8403 __lpfc_sli_ringtx_put(phba,
8404 pring, piocb);
8405 return IOCB_SUCCESS;
8406 } else {
8407 return IOCB_BUSY;
8408 }
8409 } else {
8410 sglq = __lpfc_sli_get_sglq(phba, piocb);
8411 if (!sglq) {
8412 if (!(flag & SLI_IOCB_RET_IOCB)) {
8413 __lpfc_sli_ringtx_put(phba,
8414 pring,
8415 piocb);
8416 return IOCB_SUCCESS;
8417 } else
8418 return IOCB_BUSY;
8419 }
8420 }
8421 }
8422 } else if (piocb->iocb_flag & LPFC_IO_FCP) {
8423 /* These IO's already have an XRI and a mapped sgl. */
8424 sglq = NULL;
8425 } else {
8426 /*
8427 * This is a continuation of a commandi,(CX) so this
8428 * sglq is on the active list
8429 */
8430 sglq = __lpfc_get_active_sglq(phba, piocb->sli4_lxritag);
8431 if (!sglq)
8432 return IOCB_ERROR;
8433 }
8434
8435 if (sglq) {
8436 piocb->sli4_lxritag = sglq->sli4_lxritag;
8437 piocb->sli4_xritag = sglq->sli4_xritag;
8438 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocb, sglq))
8439 return IOCB_ERROR;
8440 }
8441
8442 if (lpfc_sli4_iocb2wqe(phba, piocb, &wqe))
8443 return IOCB_ERROR;
8444
8445 if ((piocb->iocb_flag & LPFC_IO_FCP) ||
8446 (piocb->iocb_flag & LPFC_USE_FCPWQIDX)) {
8447 if (lpfc_sli4_wq_put(phba->sli4_hba.fcp_wq[piocb->fcp_wqidx],
8448 &wqe))
8449 return IOCB_ERROR;
8450 } else {
8451 if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
8452 return IOCB_ERROR;
8453 }
8454 lpfc_sli_ringtxcmpl_put(phba, pring, piocb);
8455
8456 return 0;
8457 }
8458
8459 /**
8460 * __lpfc_sli_issue_iocb - Wrapper func of lockless version for issuing iocb
8461 *
8462 * This routine wraps the actual lockless version for issusing IOCB function
8463 * pointer from the lpfc_hba struct.
8464 *
8465 * Return codes:
8466 * IOCB_ERROR - Error
8467 * IOCB_SUCCESS - Success
8468 * IOCB_BUSY - Busy
8469 **/
8470 int
8471 __lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8472 struct lpfc_iocbq *piocb, uint32_t flag)
8473 {
8474 return phba->__lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8475 }
8476
8477 /**
8478 * lpfc_sli_api_table_setup - Set up sli api function jump table
8479 * @phba: The hba struct for which this call is being executed.
8480 * @dev_grp: The HBA PCI-Device group number.
8481 *
8482 * This routine sets up the SLI interface API function jump table in @phba
8483 * struct.
8484 * Returns: 0 - success, -ENODEV - failure.
8485 **/
8486 int
8487 lpfc_sli_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
8488 {
8489
8490 switch (dev_grp) {
8491 case LPFC_PCI_DEV_LP:
8492 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s3;
8493 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s3;
8494 break;
8495 case LPFC_PCI_DEV_OC:
8496 phba->__lpfc_sli_issue_iocb = __lpfc_sli_issue_iocb_s4;
8497 phba->__lpfc_sli_release_iocbq = __lpfc_sli_release_iocbq_s4;
8498 break;
8499 default:
8500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8501 "1419 Invalid HBA PCI-device group: 0x%x\n",
8502 dev_grp);
8503 return -ENODEV;
8504 break;
8505 }
8506 phba->lpfc_get_iocb_from_iocbq = lpfc_get_iocb_from_iocbq;
8507 return 0;
8508 }
8509
8510 /**
8511 * lpfc_sli_issue_iocb - Wrapper function for __lpfc_sli_issue_iocb
8512 * @phba: Pointer to HBA context object.
8513 * @pring: Pointer to driver SLI ring object.
8514 * @piocb: Pointer to command iocb.
8515 * @flag: Flag indicating if this command can be put into txq.
8516 *
8517 * lpfc_sli_issue_iocb is a wrapper around __lpfc_sli_issue_iocb
8518 * function. This function gets the hbalock and calls
8519 * __lpfc_sli_issue_iocb function and will return the error returned
8520 * by __lpfc_sli_issue_iocb function. This wrapper is used by
8521 * functions which do not hold hbalock.
8522 **/
8523 int
8524 lpfc_sli_issue_iocb(struct lpfc_hba *phba, uint32_t ring_number,
8525 struct lpfc_iocbq *piocb, uint32_t flag)
8526 {
8527 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
8528 struct lpfc_sli_ring *pring;
8529 struct lpfc_queue *fpeq;
8530 struct lpfc_eqe *eqe;
8531 unsigned long iflags;
8532 int rc, idx;
8533
8534 if (phba->sli_rev == LPFC_SLI_REV4) {
8535 if (piocb->iocb_flag & LPFC_IO_FCP) {
8536 if (unlikely(!phba->sli4_hba.fcp_wq))
8537 return IOCB_ERROR;
8538 idx = lpfc_sli4_scmd_to_wqidx_distr(phba);
8539 piocb->fcp_wqidx = idx;
8540 ring_number = MAX_SLI3_CONFIGURED_RINGS + idx;
8541
8542 pring = &phba->sli.ring[ring_number];
8543 spin_lock_irqsave(&pring->ring_lock, iflags);
8544 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8545 flag);
8546 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8547
8548 if (lpfc_fcp_look_ahead) {
8549 fcp_eq_hdl = &phba->sli4_hba.fcp_eq_hdl[idx];
8550
8551 if (atomic_dec_and_test(&fcp_eq_hdl->
8552 fcp_eq_in_use)) {
8553
8554 /* Get associated EQ with this index */
8555 fpeq = phba->sli4_hba.hba_eq[idx];
8556
8557 /* Turn off interrupts from this EQ */
8558 lpfc_sli4_eq_clr_intr(fpeq);
8559
8560 /*
8561 * Process all the events on FCP EQ
8562 */
8563 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
8564 lpfc_sli4_hba_handle_eqe(phba,
8565 eqe, idx);
8566 fpeq->EQ_processed++;
8567 }
8568
8569 /* Always clear and re-arm the EQ */
8570 lpfc_sli4_eq_release(fpeq,
8571 LPFC_QUEUE_REARM);
8572 }
8573 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
8574 }
8575 } else {
8576 pring = &phba->sli.ring[ring_number];
8577 spin_lock_irqsave(&pring->ring_lock, iflags);
8578 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb,
8579 flag);
8580 spin_unlock_irqrestore(&pring->ring_lock, iflags);
8581
8582 }
8583 } else {
8584 /* For now, SLI2/3 will still use hbalock */
8585 spin_lock_irqsave(&phba->hbalock, iflags);
8586 rc = __lpfc_sli_issue_iocb(phba, ring_number, piocb, flag);
8587 spin_unlock_irqrestore(&phba->hbalock, iflags);
8588 }
8589 return rc;
8590 }
8591
8592 /**
8593 * lpfc_extra_ring_setup - Extra ring setup function
8594 * @phba: Pointer to HBA context object.
8595 *
8596 * This function is called while driver attaches with the
8597 * HBA to setup the extra ring. The extra ring is used
8598 * only when driver needs to support target mode functionality
8599 * or IP over FC functionalities.
8600 *
8601 * This function is called with no lock held.
8602 **/
8603 static int
8604 lpfc_extra_ring_setup( struct lpfc_hba *phba)
8605 {
8606 struct lpfc_sli *psli;
8607 struct lpfc_sli_ring *pring;
8608
8609 psli = &phba->sli;
8610
8611 /* Adjust cmd/rsp ring iocb entries more evenly */
8612
8613 /* Take some away from the FCP ring */
8614 pring = &psli->ring[psli->fcp_ring];
8615 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8616 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8617 pring->sli.sli3.numCiocb -= SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8618 pring->sli.sli3.numRiocb -= SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8619
8620 /* and give them to the extra ring */
8621 pring = &psli->ring[psli->extra_ring];
8622
8623 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8624 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8625 pring->sli.sli3.numCiocb += SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8626 pring->sli.sli3.numRiocb += SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8627
8628 /* Setup default profile for this ring */
8629 pring->iotag_max = 4096;
8630 pring->num_mask = 1;
8631 pring->prt[0].profile = 0; /* Mask 0 */
8632 pring->prt[0].rctl = phba->cfg_multi_ring_rctl;
8633 pring->prt[0].type = phba->cfg_multi_ring_type;
8634 pring->prt[0].lpfc_sli_rcv_unsol_event = NULL;
8635 return 0;
8636 }
8637
8638 /* lpfc_sli_abts_err_handler - handle a failed ABTS request from an SLI3 port.
8639 * @phba: Pointer to HBA context object.
8640 * @iocbq: Pointer to iocb object.
8641 *
8642 * The async_event handler calls this routine when it receives
8643 * an ASYNC_STATUS_CN event from the port. The port generates
8644 * this event when an Abort Sequence request to an rport fails
8645 * twice in succession. The abort could be originated by the
8646 * driver or by the port. The ABTS could have been for an ELS
8647 * or FCP IO. The port only generates this event when an ABTS
8648 * fails to complete after one retry.
8649 */
8650 static void
8651 lpfc_sli_abts_err_handler(struct lpfc_hba *phba,
8652 struct lpfc_iocbq *iocbq)
8653 {
8654 struct lpfc_nodelist *ndlp = NULL;
8655 uint16_t rpi = 0, vpi = 0;
8656 struct lpfc_vport *vport = NULL;
8657
8658 /* The rpi in the ulpContext is vport-sensitive. */
8659 vpi = iocbq->iocb.un.asyncstat.sub_ctxt_tag;
8660 rpi = iocbq->iocb.ulpContext;
8661
8662 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8663 "3092 Port generated ABTS async event "
8664 "on vpi %d rpi %d status 0x%x\n",
8665 vpi, rpi, iocbq->iocb.ulpStatus);
8666
8667 vport = lpfc_find_vport_by_vpid(phba, vpi);
8668 if (!vport)
8669 goto err_exit;
8670 ndlp = lpfc_findnode_rpi(vport, rpi);
8671 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
8672 goto err_exit;
8673
8674 if (iocbq->iocb.ulpStatus == IOSTAT_LOCAL_REJECT)
8675 lpfc_sli_abts_recover_port(vport, ndlp);
8676 return;
8677
8678 err_exit:
8679 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8680 "3095 Event Context not found, no "
8681 "action on vpi %d rpi %d status 0x%x, reason 0x%x\n",
8682 iocbq->iocb.ulpContext, iocbq->iocb.ulpStatus,
8683 vpi, rpi);
8684 }
8685
8686 /* lpfc_sli4_abts_err_handler - handle a failed ABTS request from an SLI4 port.
8687 * @phba: pointer to HBA context object.
8688 * @ndlp: nodelist pointer for the impacted rport.
8689 * @axri: pointer to the wcqe containing the failed exchange.
8690 *
8691 * The driver calls this routine when it receives an ABORT_XRI_FCP CQE from the
8692 * port. The port generates this event when an abort exchange request to an
8693 * rport fails twice in succession with no reply. The abort could be originated
8694 * by the driver or by the port. The ABTS could have been for an ELS or FCP IO.
8695 */
8696 void
8697 lpfc_sli4_abts_err_handler(struct lpfc_hba *phba,
8698 struct lpfc_nodelist *ndlp,
8699 struct sli4_wcqe_xri_aborted *axri)
8700 {
8701 struct lpfc_vport *vport;
8702 uint32_t ext_status = 0;
8703
8704 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
8705 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
8706 "3115 Node Context not found, driver "
8707 "ignoring abts err event\n");
8708 return;
8709 }
8710
8711 vport = ndlp->vport;
8712 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
8713 "3116 Port generated FCP XRI ABORT event on "
8714 "vpi %d rpi %d xri x%x status 0x%x parameter x%x\n",
8715 ndlp->vport->vpi, ndlp->nlp_rpi,
8716 bf_get(lpfc_wcqe_xa_xri, axri),
8717 bf_get(lpfc_wcqe_xa_status, axri),
8718 axri->parameter);
8719
8720 /*
8721 * Catch the ABTS protocol failure case. Older OCe FW releases returned
8722 * LOCAL_REJECT and 0 for a failed ABTS exchange and later OCe and
8723 * LPe FW releases returned LOCAL_REJECT and SEQUENCE_TIMEOUT.
8724 */
8725 ext_status = axri->parameter & IOERR_PARAM_MASK;
8726 if ((bf_get(lpfc_wcqe_xa_status, axri) == IOSTAT_LOCAL_REJECT) &&
8727 ((ext_status == IOERR_SEQUENCE_TIMEOUT) || (ext_status == 0)))
8728 lpfc_sli_abts_recover_port(vport, ndlp);
8729 }
8730
8731 /**
8732 * lpfc_sli_async_event_handler - ASYNC iocb handler function
8733 * @phba: Pointer to HBA context object.
8734 * @pring: Pointer to driver SLI ring object.
8735 * @iocbq: Pointer to iocb object.
8736 *
8737 * This function is called by the slow ring event handler
8738 * function when there is an ASYNC event iocb in the ring.
8739 * This function is called with no lock held.
8740 * Currently this function handles only temperature related
8741 * ASYNC events. The function decodes the temperature sensor
8742 * event message and posts events for the management applications.
8743 **/
8744 static void
8745 lpfc_sli_async_event_handler(struct lpfc_hba * phba,
8746 struct lpfc_sli_ring * pring, struct lpfc_iocbq * iocbq)
8747 {
8748 IOCB_t *icmd;
8749 uint16_t evt_code;
8750 struct temp_event temp_event_data;
8751 struct Scsi_Host *shost;
8752 uint32_t *iocb_w;
8753
8754 icmd = &iocbq->iocb;
8755 evt_code = icmd->un.asyncstat.evt_code;
8756
8757 switch (evt_code) {
8758 case ASYNC_TEMP_WARN:
8759 case ASYNC_TEMP_SAFE:
8760 temp_event_data.data = (uint32_t) icmd->ulpContext;
8761 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
8762 if (evt_code == ASYNC_TEMP_WARN) {
8763 temp_event_data.event_code = LPFC_THRESHOLD_TEMP;
8764 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8765 "0347 Adapter is very hot, please take "
8766 "corrective action. temperature : %d Celsius\n",
8767 (uint32_t) icmd->ulpContext);
8768 } else {
8769 temp_event_data.event_code = LPFC_NORMAL_TEMP;
8770 lpfc_printf_log(phba, KERN_ERR, LOG_TEMP,
8771 "0340 Adapter temperature is OK now. "
8772 "temperature : %d Celsius\n",
8773 (uint32_t) icmd->ulpContext);
8774 }
8775
8776 /* Send temperature change event to applications */
8777 shost = lpfc_shost_from_vport(phba->pport);
8778 fc_host_post_vendor_event(shost, fc_get_event_number(),
8779 sizeof(temp_event_data), (char *) &temp_event_data,
8780 LPFC_NL_VENDOR_ID);
8781 break;
8782 case ASYNC_STATUS_CN:
8783 lpfc_sli_abts_err_handler(phba, iocbq);
8784 break;
8785 default:
8786 iocb_w = (uint32_t *) icmd;
8787 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
8788 "0346 Ring %d handler: unexpected ASYNC_STATUS"
8789 " evt_code 0x%x\n"
8790 "W0 0x%08x W1 0x%08x W2 0x%08x W3 0x%08x\n"
8791 "W4 0x%08x W5 0x%08x W6 0x%08x W7 0x%08x\n"
8792 "W8 0x%08x W9 0x%08x W10 0x%08x W11 0x%08x\n"
8793 "W12 0x%08x W13 0x%08x W14 0x%08x W15 0x%08x\n",
8794 pring->ringno, icmd->un.asyncstat.evt_code,
8795 iocb_w[0], iocb_w[1], iocb_w[2], iocb_w[3],
8796 iocb_w[4], iocb_w[5], iocb_w[6], iocb_w[7],
8797 iocb_w[8], iocb_w[9], iocb_w[10], iocb_w[11],
8798 iocb_w[12], iocb_w[13], iocb_w[14], iocb_w[15]);
8799
8800 break;
8801 }
8802 }
8803
8804
8805 /**
8806 * lpfc_sli_setup - SLI ring setup function
8807 * @phba: Pointer to HBA context object.
8808 *
8809 * lpfc_sli_setup sets up rings of the SLI interface with
8810 * number of iocbs per ring and iotags. This function is
8811 * called while driver attach to the HBA and before the
8812 * interrupts are enabled. So there is no need for locking.
8813 *
8814 * This function always returns 0.
8815 **/
8816 int
8817 lpfc_sli_setup(struct lpfc_hba *phba)
8818 {
8819 int i, totiocbsize = 0;
8820 struct lpfc_sli *psli = &phba->sli;
8821 struct lpfc_sli_ring *pring;
8822
8823 psli->num_rings = MAX_SLI3_CONFIGURED_RINGS;
8824 if (phba->sli_rev == LPFC_SLI_REV4)
8825 psli->num_rings += phba->cfg_fcp_io_channel;
8826 psli->sli_flag = 0;
8827 psli->fcp_ring = LPFC_FCP_RING;
8828 psli->next_ring = LPFC_FCP_NEXT_RING;
8829 psli->extra_ring = LPFC_EXTRA_RING;
8830
8831 psli->iocbq_lookup = NULL;
8832 psli->iocbq_lookup_len = 0;
8833 psli->last_iotag = 0;
8834
8835 for (i = 0; i < psli->num_rings; i++) {
8836 pring = &psli->ring[i];
8837 switch (i) {
8838 case LPFC_FCP_RING: /* ring 0 - FCP */
8839 /* numCiocb and numRiocb are used in config_port */
8840 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R0_ENTRIES;
8841 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R0_ENTRIES;
8842 pring->sli.sli3.numCiocb +=
8843 SLI2_IOCB_CMD_R1XTRA_ENTRIES;
8844 pring->sli.sli3.numRiocb +=
8845 SLI2_IOCB_RSP_R1XTRA_ENTRIES;
8846 pring->sli.sli3.numCiocb +=
8847 SLI2_IOCB_CMD_R3XTRA_ENTRIES;
8848 pring->sli.sli3.numRiocb +=
8849 SLI2_IOCB_RSP_R3XTRA_ENTRIES;
8850 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8851 SLI3_IOCB_CMD_SIZE :
8852 SLI2_IOCB_CMD_SIZE;
8853 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8854 SLI3_IOCB_RSP_SIZE :
8855 SLI2_IOCB_RSP_SIZE;
8856 pring->iotag_ctr = 0;
8857 pring->iotag_max =
8858 (phba->cfg_hba_queue_depth * 2);
8859 pring->fast_iotag = pring->iotag_max;
8860 pring->num_mask = 0;
8861 break;
8862 case LPFC_EXTRA_RING: /* ring 1 - EXTRA */
8863 /* numCiocb and numRiocb are used in config_port */
8864 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R1_ENTRIES;
8865 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R1_ENTRIES;
8866 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8867 SLI3_IOCB_CMD_SIZE :
8868 SLI2_IOCB_CMD_SIZE;
8869 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8870 SLI3_IOCB_RSP_SIZE :
8871 SLI2_IOCB_RSP_SIZE;
8872 pring->iotag_max = phba->cfg_hba_queue_depth;
8873 pring->num_mask = 0;
8874 break;
8875 case LPFC_ELS_RING: /* ring 2 - ELS / CT */
8876 /* numCiocb and numRiocb are used in config_port */
8877 pring->sli.sli3.numCiocb = SLI2_IOCB_CMD_R2_ENTRIES;
8878 pring->sli.sli3.numRiocb = SLI2_IOCB_RSP_R2_ENTRIES;
8879 pring->sli.sli3.sizeCiocb = (phba->sli_rev == 3) ?
8880 SLI3_IOCB_CMD_SIZE :
8881 SLI2_IOCB_CMD_SIZE;
8882 pring->sli.sli3.sizeRiocb = (phba->sli_rev == 3) ?
8883 SLI3_IOCB_RSP_SIZE :
8884 SLI2_IOCB_RSP_SIZE;
8885 pring->fast_iotag = 0;
8886 pring->iotag_ctr = 0;
8887 pring->iotag_max = 4096;
8888 pring->lpfc_sli_rcv_async_status =
8889 lpfc_sli_async_event_handler;
8890 pring->num_mask = LPFC_MAX_RING_MASK;
8891 pring->prt[0].profile = 0; /* Mask 0 */
8892 pring->prt[0].rctl = FC_RCTL_ELS_REQ;
8893 pring->prt[0].type = FC_TYPE_ELS;
8894 pring->prt[0].lpfc_sli_rcv_unsol_event =
8895 lpfc_els_unsol_event;
8896 pring->prt[1].profile = 0; /* Mask 1 */
8897 pring->prt[1].rctl = FC_RCTL_ELS_REP;
8898 pring->prt[1].type = FC_TYPE_ELS;
8899 pring->prt[1].lpfc_sli_rcv_unsol_event =
8900 lpfc_els_unsol_event;
8901 pring->prt[2].profile = 0; /* Mask 2 */
8902 /* NameServer Inquiry */
8903 pring->prt[2].rctl = FC_RCTL_DD_UNSOL_CTL;
8904 /* NameServer */
8905 pring->prt[2].type = FC_TYPE_CT;
8906 pring->prt[2].lpfc_sli_rcv_unsol_event =
8907 lpfc_ct_unsol_event;
8908 pring->prt[3].profile = 0; /* Mask 3 */
8909 /* NameServer response */
8910 pring->prt[3].rctl = FC_RCTL_DD_SOL_CTL;
8911 /* NameServer */
8912 pring->prt[3].type = FC_TYPE_CT;
8913 pring->prt[3].lpfc_sli_rcv_unsol_event =
8914 lpfc_ct_unsol_event;
8915 break;
8916 }
8917 totiocbsize += (pring->sli.sli3.numCiocb *
8918 pring->sli.sli3.sizeCiocb) +
8919 (pring->sli.sli3.numRiocb * pring->sli.sli3.sizeRiocb);
8920 }
8921 if (totiocbsize > MAX_SLIM_IOCB_SIZE) {
8922 /* Too many cmd / rsp ring entries in SLI2 SLIM */
8923 printk(KERN_ERR "%d:0462 Too many cmd / rsp ring entries in "
8924 "SLI2 SLIM Data: x%x x%lx\n",
8925 phba->brd_no, totiocbsize,
8926 (unsigned long) MAX_SLIM_IOCB_SIZE);
8927 }
8928 if (phba->cfg_multi_ring_support == 2)
8929 lpfc_extra_ring_setup(phba);
8930
8931 return 0;
8932 }
8933
8934 /**
8935 * lpfc_sli_queue_setup - Queue initialization function
8936 * @phba: Pointer to HBA context object.
8937 *
8938 * lpfc_sli_queue_setup sets up mailbox queues and iocb queues for each
8939 * ring. This function also initializes ring indices of each ring.
8940 * This function is called during the initialization of the SLI
8941 * interface of an HBA.
8942 * This function is called with no lock held and always returns
8943 * 1.
8944 **/
8945 int
8946 lpfc_sli_queue_setup(struct lpfc_hba *phba)
8947 {
8948 struct lpfc_sli *psli;
8949 struct lpfc_sli_ring *pring;
8950 int i;
8951
8952 psli = &phba->sli;
8953 spin_lock_irq(&phba->hbalock);
8954 INIT_LIST_HEAD(&psli->mboxq);
8955 INIT_LIST_HEAD(&psli->mboxq_cmpl);
8956 /* Initialize list headers for txq and txcmplq as double linked lists */
8957 for (i = 0; i < psli->num_rings; i++) {
8958 pring = &psli->ring[i];
8959 pring->ringno = i;
8960 pring->sli.sli3.next_cmdidx = 0;
8961 pring->sli.sli3.local_getidx = 0;
8962 pring->sli.sli3.cmdidx = 0;
8963 INIT_LIST_HEAD(&pring->txq);
8964 INIT_LIST_HEAD(&pring->txcmplq);
8965 INIT_LIST_HEAD(&pring->iocb_continueq);
8966 INIT_LIST_HEAD(&pring->iocb_continue_saveq);
8967 INIT_LIST_HEAD(&pring->postbufq);
8968 spin_lock_init(&pring->ring_lock);
8969 }
8970 spin_unlock_irq(&phba->hbalock);
8971 return 1;
8972 }
8973
8974 /**
8975 * lpfc_sli_mbox_sys_flush - Flush mailbox command sub-system
8976 * @phba: Pointer to HBA context object.
8977 *
8978 * This routine flushes the mailbox command subsystem. It will unconditionally
8979 * flush all the mailbox commands in the three possible stages in the mailbox
8980 * command sub-system: pending mailbox command queue; the outstanding mailbox
8981 * command; and completed mailbox command queue. It is caller's responsibility
8982 * to make sure that the driver is in the proper state to flush the mailbox
8983 * command sub-system. Namely, the posting of mailbox commands into the
8984 * pending mailbox command queue from the various clients must be stopped;
8985 * either the HBA is in a state that it will never works on the outstanding
8986 * mailbox command (such as in EEH or ERATT conditions) or the outstanding
8987 * mailbox command has been completed.
8988 **/
8989 static void
8990 lpfc_sli_mbox_sys_flush(struct lpfc_hba *phba)
8991 {
8992 LIST_HEAD(completions);
8993 struct lpfc_sli *psli = &phba->sli;
8994 LPFC_MBOXQ_t *pmb;
8995 unsigned long iflag;
8996
8997 /* Flush all the mailbox commands in the mbox system */
8998 spin_lock_irqsave(&phba->hbalock, iflag);
8999 /* The pending mailbox command queue */
9000 list_splice_init(&phba->sli.mboxq, &completions);
9001 /* The outstanding active mailbox command */
9002 if (psli->mbox_active) {
9003 list_add_tail(&psli->mbox_active->list, &completions);
9004 psli->mbox_active = NULL;
9005 psli->sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
9006 }
9007 /* The completed mailbox command queue */
9008 list_splice_init(&phba->sli.mboxq_cmpl, &completions);
9009 spin_unlock_irqrestore(&phba->hbalock, iflag);
9010
9011 /* Return all flushed mailbox commands with MBX_NOT_FINISHED status */
9012 while (!list_empty(&completions)) {
9013 list_remove_head(&completions, pmb, LPFC_MBOXQ_t, list);
9014 pmb->u.mb.mbxStatus = MBX_NOT_FINISHED;
9015 if (pmb->mbox_cmpl)
9016 pmb->mbox_cmpl(phba, pmb);
9017 }
9018 }
9019
9020 /**
9021 * lpfc_sli_host_down - Vport cleanup function
9022 * @vport: Pointer to virtual port object.
9023 *
9024 * lpfc_sli_host_down is called to clean up the resources
9025 * associated with a vport before destroying virtual
9026 * port data structures.
9027 * This function does following operations:
9028 * - Free discovery resources associated with this virtual
9029 * port.
9030 * - Free iocbs associated with this virtual port in
9031 * the txq.
9032 * - Send abort for all iocb commands associated with this
9033 * vport in txcmplq.
9034 *
9035 * This function is called with no lock held and always returns 1.
9036 **/
9037 int
9038 lpfc_sli_host_down(struct lpfc_vport *vport)
9039 {
9040 LIST_HEAD(completions);
9041 struct lpfc_hba *phba = vport->phba;
9042 struct lpfc_sli *psli = &phba->sli;
9043 struct lpfc_sli_ring *pring;
9044 struct lpfc_iocbq *iocb, *next_iocb;
9045 int i;
9046 unsigned long flags = 0;
9047 uint16_t prev_pring_flag;
9048
9049 lpfc_cleanup_discovery_resources(vport);
9050
9051 spin_lock_irqsave(&phba->hbalock, flags);
9052 for (i = 0; i < psli->num_rings; i++) {
9053 pring = &psli->ring[i];
9054 prev_pring_flag = pring->flag;
9055 /* Only slow rings */
9056 if (pring->ringno == LPFC_ELS_RING) {
9057 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9058 /* Set the lpfc data pending flag */
9059 set_bit(LPFC_DATA_READY, &phba->data_flags);
9060 }
9061 /*
9062 * Error everything on the txq since these iocbs have not been
9063 * given to the FW yet.
9064 */
9065 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
9066 if (iocb->vport != vport)
9067 continue;
9068 list_move_tail(&iocb->list, &completions);
9069 }
9070
9071 /* Next issue ABTS for everything on the txcmplq */
9072 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq,
9073 list) {
9074 if (iocb->vport != vport)
9075 continue;
9076 lpfc_sli_issue_abort_iotag(phba, pring, iocb);
9077 }
9078
9079 pring->flag = prev_pring_flag;
9080 }
9081
9082 spin_unlock_irqrestore(&phba->hbalock, flags);
9083
9084 /* Cancel all the IOCBs from the completions list */
9085 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9086 IOERR_SLI_DOWN);
9087 return 1;
9088 }
9089
9090 /**
9091 * lpfc_sli_hba_down - Resource cleanup function for the HBA
9092 * @phba: Pointer to HBA context object.
9093 *
9094 * This function cleans up all iocb, buffers, mailbox commands
9095 * while shutting down the HBA. This function is called with no
9096 * lock held and always returns 1.
9097 * This function does the following to cleanup driver resources:
9098 * - Free discovery resources for each virtual port
9099 * - Cleanup any pending fabric iocbs
9100 * - Iterate through the iocb txq and free each entry
9101 * in the list.
9102 * - Free up any buffer posted to the HBA
9103 * - Free mailbox commands in the mailbox queue.
9104 **/
9105 int
9106 lpfc_sli_hba_down(struct lpfc_hba *phba)
9107 {
9108 LIST_HEAD(completions);
9109 struct lpfc_sli *psli = &phba->sli;
9110 struct lpfc_sli_ring *pring;
9111 struct lpfc_dmabuf *buf_ptr;
9112 unsigned long flags = 0;
9113 int i;
9114
9115 /* Shutdown the mailbox command sub-system */
9116 lpfc_sli_mbox_sys_shutdown(phba, LPFC_MBX_WAIT);
9117
9118 lpfc_hba_down_prep(phba);
9119
9120 lpfc_fabric_abort_hba(phba);
9121
9122 spin_lock_irqsave(&phba->hbalock, flags);
9123 for (i = 0; i < psli->num_rings; i++) {
9124 pring = &psli->ring[i];
9125 /* Only slow rings */
9126 if (pring->ringno == LPFC_ELS_RING) {
9127 pring->flag |= LPFC_DEFERRED_RING_EVENT;
9128 /* Set the lpfc data pending flag */
9129 set_bit(LPFC_DATA_READY, &phba->data_flags);
9130 }
9131
9132 /*
9133 * Error everything on the txq since these iocbs have not been
9134 * given to the FW yet.
9135 */
9136 list_splice_init(&pring->txq, &completions);
9137 }
9138 spin_unlock_irqrestore(&phba->hbalock, flags);
9139
9140 /* Cancel all the IOCBs from the completions list */
9141 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9142 IOERR_SLI_DOWN);
9143
9144 spin_lock_irqsave(&phba->hbalock, flags);
9145 list_splice_init(&phba->elsbuf, &completions);
9146 phba->elsbuf_cnt = 0;
9147 phba->elsbuf_prev_cnt = 0;
9148 spin_unlock_irqrestore(&phba->hbalock, flags);
9149
9150 while (!list_empty(&completions)) {
9151 list_remove_head(&completions, buf_ptr,
9152 struct lpfc_dmabuf, list);
9153 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
9154 kfree(buf_ptr);
9155 }
9156
9157 /* Return any active mbox cmds */
9158 del_timer_sync(&psli->mbox_tmo);
9159
9160 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
9161 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
9162 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
9163
9164 return 1;
9165 }
9166
9167 /**
9168 * lpfc_sli_pcimem_bcopy - SLI memory copy function
9169 * @srcp: Source memory pointer.
9170 * @destp: Destination memory pointer.
9171 * @cnt: Number of words required to be copied.
9172 *
9173 * This function is used for copying data between driver memory
9174 * and the SLI memory. This function also changes the endianness
9175 * of each word if native endianness is different from SLI
9176 * endianness. This function can be called with or without
9177 * lock.
9178 **/
9179 void
9180 lpfc_sli_pcimem_bcopy(void *srcp, void *destp, uint32_t cnt)
9181 {
9182 uint32_t *src = srcp;
9183 uint32_t *dest = destp;
9184 uint32_t ldata;
9185 int i;
9186
9187 for (i = 0; i < (int)cnt; i += sizeof (uint32_t)) {
9188 ldata = *src;
9189 ldata = le32_to_cpu(ldata);
9190 *dest = ldata;
9191 src++;
9192 dest++;
9193 }
9194 }
9195
9196
9197 /**
9198 * lpfc_sli_bemem_bcopy - SLI memory copy function
9199 * @srcp: Source memory pointer.
9200 * @destp: Destination memory pointer.
9201 * @cnt: Number of words required to be copied.
9202 *
9203 * This function is used for copying data between a data structure
9204 * with big endian representation to local endianness.
9205 * This function can be called with or without lock.
9206 **/
9207 void
9208 lpfc_sli_bemem_bcopy(void *srcp, void *destp, uint32_t cnt)
9209 {
9210 uint32_t *src = srcp;
9211 uint32_t *dest = destp;
9212 uint32_t ldata;
9213 int i;
9214
9215 for (i = 0; i < (int)cnt; i += sizeof(uint32_t)) {
9216 ldata = *src;
9217 ldata = be32_to_cpu(ldata);
9218 *dest = ldata;
9219 src++;
9220 dest++;
9221 }
9222 }
9223
9224 /**
9225 * lpfc_sli_ringpostbuf_put - Function to add a buffer to postbufq
9226 * @phba: Pointer to HBA context object.
9227 * @pring: Pointer to driver SLI ring object.
9228 * @mp: Pointer to driver buffer object.
9229 *
9230 * This function is called with no lock held.
9231 * It always return zero after adding the buffer to the postbufq
9232 * buffer list.
9233 **/
9234 int
9235 lpfc_sli_ringpostbuf_put(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9236 struct lpfc_dmabuf *mp)
9237 {
9238 /* Stick struct lpfc_dmabuf at end of postbufq so driver can look it up
9239 later */
9240 spin_lock_irq(&phba->hbalock);
9241 list_add_tail(&mp->list, &pring->postbufq);
9242 pring->postbufq_cnt++;
9243 spin_unlock_irq(&phba->hbalock);
9244 return 0;
9245 }
9246
9247 /**
9248 * lpfc_sli_get_buffer_tag - allocates a tag for a CMD_QUE_XRI64_CX buffer
9249 * @phba: Pointer to HBA context object.
9250 *
9251 * When HBQ is enabled, buffers are searched based on tags. This function
9252 * allocates a tag for buffer posted using CMD_QUE_XRI64_CX iocb. The
9253 * tag is bit wise or-ed with QUE_BUFTAG_BIT to make sure that the tag
9254 * does not conflict with tags of buffer posted for unsolicited events.
9255 * The function returns the allocated tag. The function is called with
9256 * no locks held.
9257 **/
9258 uint32_t
9259 lpfc_sli_get_buffer_tag(struct lpfc_hba *phba)
9260 {
9261 spin_lock_irq(&phba->hbalock);
9262 phba->buffer_tag_count++;
9263 /*
9264 * Always set the QUE_BUFTAG_BIT to distiguish between
9265 * a tag assigned by HBQ.
9266 */
9267 phba->buffer_tag_count |= QUE_BUFTAG_BIT;
9268 spin_unlock_irq(&phba->hbalock);
9269 return phba->buffer_tag_count;
9270 }
9271
9272 /**
9273 * lpfc_sli_ring_taggedbuf_get - find HBQ buffer associated with given tag
9274 * @phba: Pointer to HBA context object.
9275 * @pring: Pointer to driver SLI ring object.
9276 * @tag: Buffer tag.
9277 *
9278 * Buffers posted using CMD_QUE_XRI64_CX iocb are in pring->postbufq
9279 * list. After HBA DMA data to these buffers, CMD_IOCB_RET_XRI64_CX
9280 * iocb is posted to the response ring with the tag of the buffer.
9281 * This function searches the pring->postbufq list using the tag
9282 * to find buffer associated with CMD_IOCB_RET_XRI64_CX
9283 * iocb. If the buffer is found then lpfc_dmabuf object of the
9284 * buffer is returned to the caller else NULL is returned.
9285 * This function is called with no lock held.
9286 **/
9287 struct lpfc_dmabuf *
9288 lpfc_sli_ring_taggedbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9289 uint32_t tag)
9290 {
9291 struct lpfc_dmabuf *mp, *next_mp;
9292 struct list_head *slp = &pring->postbufq;
9293
9294 /* Search postbufq, from the beginning, looking for a match on tag */
9295 spin_lock_irq(&phba->hbalock);
9296 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9297 if (mp->buffer_tag == tag) {
9298 list_del_init(&mp->list);
9299 pring->postbufq_cnt--;
9300 spin_unlock_irq(&phba->hbalock);
9301 return mp;
9302 }
9303 }
9304
9305 spin_unlock_irq(&phba->hbalock);
9306 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9307 "0402 Cannot find virtual addr for buffer tag on "
9308 "ring %d Data x%lx x%p x%p x%x\n",
9309 pring->ringno, (unsigned long) tag,
9310 slp->next, slp->prev, pring->postbufq_cnt);
9311
9312 return NULL;
9313 }
9314
9315 /**
9316 * lpfc_sli_ringpostbuf_get - search buffers for unsolicited CT and ELS events
9317 * @phba: Pointer to HBA context object.
9318 * @pring: Pointer to driver SLI ring object.
9319 * @phys: DMA address of the buffer.
9320 *
9321 * This function searches the buffer list using the dma_address
9322 * of unsolicited event to find the driver's lpfc_dmabuf object
9323 * corresponding to the dma_address. The function returns the
9324 * lpfc_dmabuf object if a buffer is found else it returns NULL.
9325 * This function is called by the ct and els unsolicited event
9326 * handlers to get the buffer associated with the unsolicited
9327 * event.
9328 *
9329 * This function is called with no lock held.
9330 **/
9331 struct lpfc_dmabuf *
9332 lpfc_sli_ringpostbuf_get(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9333 dma_addr_t phys)
9334 {
9335 struct lpfc_dmabuf *mp, *next_mp;
9336 struct list_head *slp = &pring->postbufq;
9337
9338 /* Search postbufq, from the beginning, looking for a match on phys */
9339 spin_lock_irq(&phba->hbalock);
9340 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
9341 if (mp->phys == phys) {
9342 list_del_init(&mp->list);
9343 pring->postbufq_cnt--;
9344 spin_unlock_irq(&phba->hbalock);
9345 return mp;
9346 }
9347 }
9348
9349 spin_unlock_irq(&phba->hbalock);
9350 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9351 "0410 Cannot find virtual addr for mapped buf on "
9352 "ring %d Data x%llx x%p x%p x%x\n",
9353 pring->ringno, (unsigned long long)phys,
9354 slp->next, slp->prev, pring->postbufq_cnt);
9355 return NULL;
9356 }
9357
9358 /**
9359 * lpfc_sli_abort_els_cmpl - Completion handler for the els abort iocbs
9360 * @phba: Pointer to HBA context object.
9361 * @cmdiocb: Pointer to driver command iocb object.
9362 * @rspiocb: Pointer to driver response iocb object.
9363 *
9364 * This function is the completion handler for the abort iocbs for
9365 * ELS commands. This function is called from the ELS ring event
9366 * handler with no lock held. This function frees memory resources
9367 * associated with the abort iocb.
9368 **/
9369 static void
9370 lpfc_sli_abort_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9371 struct lpfc_iocbq *rspiocb)
9372 {
9373 IOCB_t *irsp = &rspiocb->iocb;
9374 uint16_t abort_iotag, abort_context;
9375 struct lpfc_iocbq *abort_iocb = NULL;
9376
9377 if (irsp->ulpStatus) {
9378
9379 /*
9380 * Assume that the port already completed and returned, or
9381 * will return the iocb. Just Log the message.
9382 */
9383 abort_context = cmdiocb->iocb.un.acxri.abortContextTag;
9384 abort_iotag = cmdiocb->iocb.un.acxri.abortIoTag;
9385
9386 spin_lock_irq(&phba->hbalock);
9387 if (phba->sli_rev < LPFC_SLI_REV4) {
9388 if (abort_iotag != 0 &&
9389 abort_iotag <= phba->sli.last_iotag)
9390 abort_iocb =
9391 phba->sli.iocbq_lookup[abort_iotag];
9392 } else
9393 /* For sli4 the abort_tag is the XRI,
9394 * so the abort routine puts the iotag of the iocb
9395 * being aborted in the context field of the abort
9396 * IOCB.
9397 */
9398 abort_iocb = phba->sli.iocbq_lookup[abort_context];
9399
9400 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS | LOG_SLI,
9401 "0327 Cannot abort els iocb %p "
9402 "with tag %x context %x, abort status %x, "
9403 "abort code %x\n",
9404 abort_iocb, abort_iotag, abort_context,
9405 irsp->ulpStatus, irsp->un.ulpWord[4]);
9406
9407 spin_unlock_irq(&phba->hbalock);
9408 }
9409 lpfc_sli_release_iocbq(phba, cmdiocb);
9410 return;
9411 }
9412
9413 /**
9414 * lpfc_ignore_els_cmpl - Completion handler for aborted ELS command
9415 * @phba: Pointer to HBA context object.
9416 * @cmdiocb: Pointer to driver command iocb object.
9417 * @rspiocb: Pointer to driver response iocb object.
9418 *
9419 * The function is called from SLI ring event handler with no
9420 * lock held. This function is the completion handler for ELS commands
9421 * which are aborted. The function frees memory resources used for
9422 * the aborted ELS commands.
9423 **/
9424 static void
9425 lpfc_ignore_els_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9426 struct lpfc_iocbq *rspiocb)
9427 {
9428 IOCB_t *irsp = &rspiocb->iocb;
9429
9430 /* ELS cmd tag <ulpIoTag> completes */
9431 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
9432 "0139 Ignoring ELS cmd tag x%x completion Data: "
9433 "x%x x%x x%x\n",
9434 irsp->ulpIoTag, irsp->ulpStatus,
9435 irsp->un.ulpWord[4], irsp->ulpTimeout);
9436 if (cmdiocb->iocb.ulpCommand == CMD_GEN_REQUEST64_CR)
9437 lpfc_ct_free_iocb(phba, cmdiocb);
9438 else
9439 lpfc_els_free_iocb(phba, cmdiocb);
9440 return;
9441 }
9442
9443 /**
9444 * lpfc_sli_abort_iotag_issue - Issue abort for a command iocb
9445 * @phba: Pointer to HBA context object.
9446 * @pring: Pointer to driver SLI ring object.
9447 * @cmdiocb: Pointer to driver command iocb object.
9448 *
9449 * This function issues an abort iocb for the provided command iocb down to
9450 * the port. Other than the case the outstanding command iocb is an abort
9451 * request, this function issues abort out unconditionally. This function is
9452 * called with hbalock held. The function returns 0 when it fails due to
9453 * memory allocation failure or when the command iocb is an abort request.
9454 **/
9455 static int
9456 lpfc_sli_abort_iotag_issue(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9457 struct lpfc_iocbq *cmdiocb)
9458 {
9459 struct lpfc_vport *vport = cmdiocb->vport;
9460 struct lpfc_iocbq *abtsiocbp;
9461 IOCB_t *icmd = NULL;
9462 IOCB_t *iabt = NULL;
9463 int retval;
9464 unsigned long iflags;
9465
9466 /*
9467 * There are certain command types we don't want to abort. And we
9468 * don't want to abort commands that are already in the process of
9469 * being aborted.
9470 */
9471 icmd = &cmdiocb->iocb;
9472 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9473 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9474 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9475 return 0;
9476
9477 /* issue ABTS for this IOCB based on iotag */
9478 abtsiocbp = __lpfc_sli_get_iocbq(phba);
9479 if (abtsiocbp == NULL)
9480 return 0;
9481
9482 /* This signals the response to set the correct status
9483 * before calling the completion handler
9484 */
9485 cmdiocb->iocb_flag |= LPFC_DRIVER_ABORTED;
9486
9487 iabt = &abtsiocbp->iocb;
9488 iabt->un.acxri.abortType = ABORT_TYPE_ABTS;
9489 iabt->un.acxri.abortContextTag = icmd->ulpContext;
9490 if (phba->sli_rev == LPFC_SLI_REV4) {
9491 iabt->un.acxri.abortIoTag = cmdiocb->sli4_xritag;
9492 iabt->un.acxri.abortContextTag = cmdiocb->iotag;
9493 }
9494 else
9495 iabt->un.acxri.abortIoTag = icmd->ulpIoTag;
9496 iabt->ulpLe = 1;
9497 iabt->ulpClass = icmd->ulpClass;
9498
9499 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9500 abtsiocbp->fcp_wqidx = cmdiocb->fcp_wqidx;
9501 if (cmdiocb->iocb_flag & LPFC_IO_FCP)
9502 abtsiocbp->iocb_flag |= LPFC_USE_FCPWQIDX;
9503
9504 if (phba->link_state >= LPFC_LINK_UP)
9505 iabt->ulpCommand = CMD_ABORT_XRI_CN;
9506 else
9507 iabt->ulpCommand = CMD_CLOSE_XRI_CN;
9508
9509 abtsiocbp->iocb_cmpl = lpfc_sli_abort_els_cmpl;
9510
9511 lpfc_printf_vlog(vport, KERN_INFO, LOG_SLI,
9512 "0339 Abort xri x%x, original iotag x%x, "
9513 "abort cmd iotag x%x\n",
9514 iabt->un.acxri.abortIoTag,
9515 iabt->un.acxri.abortContextTag,
9516 abtsiocbp->iotag);
9517
9518 if (phba->sli_rev == LPFC_SLI_REV4) {
9519 /* Note: both hbalock and ring_lock need to be set here */
9520 spin_lock_irqsave(&pring->ring_lock, iflags);
9521 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9522 abtsiocbp, 0);
9523 spin_unlock_irqrestore(&pring->ring_lock, iflags);
9524 } else {
9525 retval = __lpfc_sli_issue_iocb(phba, pring->ringno,
9526 abtsiocbp, 0);
9527 }
9528
9529 if (retval)
9530 __lpfc_sli_release_iocbq(phba, abtsiocbp);
9531
9532 /*
9533 * Caller to this routine should check for IOCB_ERROR
9534 * and handle it properly. This routine no longer removes
9535 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9536 */
9537 return retval;
9538 }
9539
9540 /**
9541 * lpfc_sli_issue_abort_iotag - Abort function for a command iocb
9542 * @phba: Pointer to HBA context object.
9543 * @pring: Pointer to driver SLI ring object.
9544 * @cmdiocb: Pointer to driver command iocb object.
9545 *
9546 * This function issues an abort iocb for the provided command iocb. In case
9547 * of unloading, the abort iocb will not be issued to commands on the ELS
9548 * ring. Instead, the callback function shall be changed to those commands
9549 * so that nothing happens when them finishes. This function is called with
9550 * hbalock held. The function returns 0 when the command iocb is an abort
9551 * request.
9552 **/
9553 int
9554 lpfc_sli_issue_abort_iotag(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
9555 struct lpfc_iocbq *cmdiocb)
9556 {
9557 struct lpfc_vport *vport = cmdiocb->vport;
9558 int retval = IOCB_ERROR;
9559 IOCB_t *icmd = NULL;
9560
9561 /*
9562 * There are certain command types we don't want to abort. And we
9563 * don't want to abort commands that are already in the process of
9564 * being aborted.
9565 */
9566 icmd = &cmdiocb->iocb;
9567 if (icmd->ulpCommand == CMD_ABORT_XRI_CN ||
9568 icmd->ulpCommand == CMD_CLOSE_XRI_CN ||
9569 (cmdiocb->iocb_flag & LPFC_DRIVER_ABORTED) != 0)
9570 return 0;
9571
9572 /*
9573 * If we're unloading, don't abort iocb on the ELS ring, but change
9574 * the callback so that nothing happens when it finishes.
9575 */
9576 if ((vport->load_flag & FC_UNLOADING) &&
9577 (pring->ringno == LPFC_ELS_RING)) {
9578 if (cmdiocb->iocb_flag & LPFC_IO_FABRIC)
9579 cmdiocb->fabric_iocb_cmpl = lpfc_ignore_els_cmpl;
9580 else
9581 cmdiocb->iocb_cmpl = lpfc_ignore_els_cmpl;
9582 goto abort_iotag_exit;
9583 }
9584
9585 /* Now, we try to issue the abort to the cmdiocb out */
9586 retval = lpfc_sli_abort_iotag_issue(phba, pring, cmdiocb);
9587
9588 abort_iotag_exit:
9589 /*
9590 * Caller to this routine should check for IOCB_ERROR
9591 * and handle it properly. This routine no longer removes
9592 * iocb off txcmplq and call compl in case of IOCB_ERROR.
9593 */
9594 return retval;
9595 }
9596
9597 /**
9598 * lpfc_sli_iocb_ring_abort - Unconditionally abort all iocbs on an iocb ring
9599 * @phba: Pointer to HBA context object.
9600 * @pring: Pointer to driver SLI ring object.
9601 *
9602 * This function aborts all iocbs in the given ring and frees all the iocb
9603 * objects in txq. This function issues abort iocbs unconditionally for all
9604 * the iocb commands in txcmplq. The iocbs in the txcmplq is not guaranteed
9605 * to complete before the return of this function. The caller is not required
9606 * to hold any locks.
9607 **/
9608 static void
9609 lpfc_sli_iocb_ring_abort(struct lpfc_hba *phba, struct lpfc_sli_ring *pring)
9610 {
9611 LIST_HEAD(completions);
9612 struct lpfc_iocbq *iocb, *next_iocb;
9613
9614 if (pring->ringno == LPFC_ELS_RING)
9615 lpfc_fabric_abort_hba(phba);
9616
9617 spin_lock_irq(&phba->hbalock);
9618
9619 /* Take off all the iocbs on txq for cancelling */
9620 list_splice_init(&pring->txq, &completions);
9621 pring->txq_cnt = 0;
9622
9623 /* Next issue ABTS for everything on the txcmplq */
9624 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list)
9625 lpfc_sli_abort_iotag_issue(phba, pring, iocb);
9626
9627 spin_unlock_irq(&phba->hbalock);
9628
9629 /* Cancel all the IOCBs from the completions list */
9630 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
9631 IOERR_SLI_ABORTED);
9632 }
9633
9634 /**
9635 * lpfc_sli_hba_iocb_abort - Abort all iocbs to an hba.
9636 * @phba: pointer to lpfc HBA data structure.
9637 *
9638 * This routine will abort all pending and outstanding iocbs to an HBA.
9639 **/
9640 void
9641 lpfc_sli_hba_iocb_abort(struct lpfc_hba *phba)
9642 {
9643 struct lpfc_sli *psli = &phba->sli;
9644 struct lpfc_sli_ring *pring;
9645 int i;
9646
9647 for (i = 0; i < psli->num_rings; i++) {
9648 pring = &psli->ring[i];
9649 lpfc_sli_iocb_ring_abort(phba, pring);
9650 }
9651 }
9652
9653 /**
9654 * lpfc_sli_validate_fcp_iocb - find commands associated with a vport or LUN
9655 * @iocbq: Pointer to driver iocb object.
9656 * @vport: Pointer to driver virtual port object.
9657 * @tgt_id: SCSI ID of the target.
9658 * @lun_id: LUN ID of the scsi device.
9659 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST
9660 *
9661 * This function acts as an iocb filter for functions which abort or count
9662 * all FCP iocbs pending on a lun/SCSI target/SCSI host. It will return
9663 * 0 if the filtering criteria is met for the given iocb and will return
9664 * 1 if the filtering criteria is not met.
9665 * If ctx_cmd == LPFC_CTX_LUN, the function returns 0 only if the
9666 * given iocb is for the SCSI device specified by vport, tgt_id and
9667 * lun_id parameter.
9668 * If ctx_cmd == LPFC_CTX_TGT, the function returns 0 only if the
9669 * given iocb is for the SCSI target specified by vport and tgt_id
9670 * parameters.
9671 * If ctx_cmd == LPFC_CTX_HOST, the function returns 0 only if the
9672 * given iocb is for the SCSI host associated with the given vport.
9673 * This function is called with no locks held.
9674 **/
9675 static int
9676 lpfc_sli_validate_fcp_iocb(struct lpfc_iocbq *iocbq, struct lpfc_vport *vport,
9677 uint16_t tgt_id, uint64_t lun_id,
9678 lpfc_ctx_cmd ctx_cmd)
9679 {
9680 struct lpfc_scsi_buf *lpfc_cmd;
9681 int rc = 1;
9682
9683 if (!(iocbq->iocb_flag & LPFC_IO_FCP))
9684 return rc;
9685
9686 if (iocbq->vport != vport)
9687 return rc;
9688
9689 lpfc_cmd = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
9690
9691 if (lpfc_cmd->pCmd == NULL)
9692 return rc;
9693
9694 switch (ctx_cmd) {
9695 case LPFC_CTX_LUN:
9696 if ((lpfc_cmd->rdata->pnode) &&
9697 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id) &&
9698 (scsilun_to_int(&lpfc_cmd->fcp_cmnd->fcp_lun) == lun_id))
9699 rc = 0;
9700 break;
9701 case LPFC_CTX_TGT:
9702 if ((lpfc_cmd->rdata->pnode) &&
9703 (lpfc_cmd->rdata->pnode->nlp_sid == tgt_id))
9704 rc = 0;
9705 break;
9706 case LPFC_CTX_HOST:
9707 rc = 0;
9708 break;
9709 default:
9710 printk(KERN_ERR "%s: Unknown context cmd type, value %d\n",
9711 __func__, ctx_cmd);
9712 break;
9713 }
9714
9715 return rc;
9716 }
9717
9718 /**
9719 * lpfc_sli_sum_iocb - Function to count the number of FCP iocbs pending
9720 * @vport: Pointer to virtual port.
9721 * @tgt_id: SCSI ID of the target.
9722 * @lun_id: LUN ID of the scsi device.
9723 * @ctx_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9724 *
9725 * This function returns number of FCP commands pending for the vport.
9726 * When ctx_cmd == LPFC_CTX_LUN, the function returns number of FCP
9727 * commands pending on the vport associated with SCSI device specified
9728 * by tgt_id and lun_id parameters.
9729 * When ctx_cmd == LPFC_CTX_TGT, the function returns number of FCP
9730 * commands pending on the vport associated with SCSI target specified
9731 * by tgt_id parameter.
9732 * When ctx_cmd == LPFC_CTX_HOST, the function returns number of FCP
9733 * commands pending on the vport.
9734 * This function returns the number of iocbs which satisfy the filter.
9735 * This function is called without any lock held.
9736 **/
9737 int
9738 lpfc_sli_sum_iocb(struct lpfc_vport *vport, uint16_t tgt_id, uint64_t lun_id,
9739 lpfc_ctx_cmd ctx_cmd)
9740 {
9741 struct lpfc_hba *phba = vport->phba;
9742 struct lpfc_iocbq *iocbq;
9743 int sum, i;
9744
9745 for (i = 1, sum = 0; i <= phba->sli.last_iotag; i++) {
9746 iocbq = phba->sli.iocbq_lookup[i];
9747
9748 if (lpfc_sli_validate_fcp_iocb (iocbq, vport, tgt_id, lun_id,
9749 ctx_cmd) == 0)
9750 sum++;
9751 }
9752
9753 return sum;
9754 }
9755
9756 /**
9757 * lpfc_sli_abort_fcp_cmpl - Completion handler function for aborted FCP IOCBs
9758 * @phba: Pointer to HBA context object
9759 * @cmdiocb: Pointer to command iocb object.
9760 * @rspiocb: Pointer to response iocb object.
9761 *
9762 * This function is called when an aborted FCP iocb completes. This
9763 * function is called by the ring event handler with no lock held.
9764 * This function frees the iocb.
9765 **/
9766 void
9767 lpfc_sli_abort_fcp_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
9768 struct lpfc_iocbq *rspiocb)
9769 {
9770 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
9771 "3096 ABORT_XRI_CN completing on xri x%x "
9772 "original iotag x%x, abort cmd iotag x%x "
9773 "status 0x%x, reason 0x%x\n",
9774 cmdiocb->iocb.un.acxri.abortContextTag,
9775 cmdiocb->iocb.un.acxri.abortIoTag,
9776 cmdiocb->iotag, rspiocb->iocb.ulpStatus,
9777 rspiocb->iocb.un.ulpWord[4]);
9778 lpfc_sli_release_iocbq(phba, cmdiocb);
9779 return;
9780 }
9781
9782 /**
9783 * lpfc_sli_abort_iocb - issue abort for all commands on a host/target/LUN
9784 * @vport: Pointer to virtual port.
9785 * @pring: Pointer to driver SLI ring object.
9786 * @tgt_id: SCSI ID of the target.
9787 * @lun_id: LUN ID of the scsi device.
9788 * @abort_cmd: LPFC_CTX_LUN/LPFC_CTX_TGT/LPFC_CTX_HOST.
9789 *
9790 * This function sends an abort command for every SCSI command
9791 * associated with the given virtual port pending on the ring
9792 * filtered by lpfc_sli_validate_fcp_iocb function.
9793 * When abort_cmd == LPFC_CTX_LUN, the function sends abort only to the
9794 * FCP iocbs associated with lun specified by tgt_id and lun_id
9795 * parameters
9796 * When abort_cmd == LPFC_CTX_TGT, the function sends abort only to the
9797 * FCP iocbs associated with SCSI target specified by tgt_id parameter.
9798 * When abort_cmd == LPFC_CTX_HOST, the function sends abort to all
9799 * FCP iocbs associated with virtual port.
9800 * This function returns number of iocbs it failed to abort.
9801 * This function is called with no locks held.
9802 **/
9803 int
9804 lpfc_sli_abort_iocb(struct lpfc_vport *vport, struct lpfc_sli_ring *pring,
9805 uint16_t tgt_id, uint64_t lun_id, lpfc_ctx_cmd abort_cmd)
9806 {
9807 struct lpfc_hba *phba = vport->phba;
9808 struct lpfc_iocbq *iocbq;
9809 struct lpfc_iocbq *abtsiocb;
9810 IOCB_t *cmd = NULL;
9811 int errcnt = 0, ret_val = 0;
9812 int i;
9813
9814 for (i = 1; i <= phba->sli.last_iotag; i++) {
9815 iocbq = phba->sli.iocbq_lookup[i];
9816
9817 if (lpfc_sli_validate_fcp_iocb(iocbq, vport, tgt_id, lun_id,
9818 abort_cmd) != 0)
9819 continue;
9820
9821 /* issue ABTS for this IOCB based on iotag */
9822 abtsiocb = lpfc_sli_get_iocbq(phba);
9823 if (abtsiocb == NULL) {
9824 errcnt++;
9825 continue;
9826 }
9827
9828 cmd = &iocbq->iocb;
9829 abtsiocb->iocb.un.acxri.abortType = ABORT_TYPE_ABTS;
9830 abtsiocb->iocb.un.acxri.abortContextTag = cmd->ulpContext;
9831 if (phba->sli_rev == LPFC_SLI_REV4)
9832 abtsiocb->iocb.un.acxri.abortIoTag = iocbq->sli4_xritag;
9833 else
9834 abtsiocb->iocb.un.acxri.abortIoTag = cmd->ulpIoTag;
9835 abtsiocb->iocb.ulpLe = 1;
9836 abtsiocb->iocb.ulpClass = cmd->ulpClass;
9837 abtsiocb->vport = phba->pport;
9838
9839 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
9840 abtsiocb->fcp_wqidx = iocbq->fcp_wqidx;
9841 if (iocbq->iocb_flag & LPFC_IO_FCP)
9842 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
9843
9844 if (lpfc_is_link_up(phba))
9845 abtsiocb->iocb.ulpCommand = CMD_ABORT_XRI_CN;
9846 else
9847 abtsiocb->iocb.ulpCommand = CMD_CLOSE_XRI_CN;
9848
9849 /* Setup callback routine and issue the command. */
9850 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
9851 ret_val = lpfc_sli_issue_iocb(phba, pring->ringno,
9852 abtsiocb, 0);
9853 if (ret_val == IOCB_ERROR) {
9854 lpfc_sli_release_iocbq(phba, abtsiocb);
9855 errcnt++;
9856 continue;
9857 }
9858 }
9859
9860 return errcnt;
9861 }
9862
9863 /**
9864 * lpfc_sli_wake_iocb_wait - lpfc_sli_issue_iocb_wait's completion handler
9865 * @phba: Pointer to HBA context object.
9866 * @cmdiocbq: Pointer to command iocb.
9867 * @rspiocbq: Pointer to response iocb.
9868 *
9869 * This function is the completion handler for iocbs issued using
9870 * lpfc_sli_issue_iocb_wait function. This function is called by the
9871 * ring event handler function without any lock held. This function
9872 * can be called from both worker thread context and interrupt
9873 * context. This function also can be called from other thread which
9874 * cleans up the SLI layer objects.
9875 * This function copy the contents of the response iocb to the
9876 * response iocb memory object provided by the caller of
9877 * lpfc_sli_issue_iocb_wait and then wakes up the thread which
9878 * sleeps for the iocb completion.
9879 **/
9880 static void
9881 lpfc_sli_wake_iocb_wait(struct lpfc_hba *phba,
9882 struct lpfc_iocbq *cmdiocbq,
9883 struct lpfc_iocbq *rspiocbq)
9884 {
9885 wait_queue_head_t *pdone_q;
9886 unsigned long iflags;
9887 struct lpfc_scsi_buf *lpfc_cmd;
9888
9889 spin_lock_irqsave(&phba->hbalock, iflags);
9890 cmdiocbq->iocb_flag |= LPFC_IO_WAKE;
9891 if (cmdiocbq->context2 && rspiocbq)
9892 memcpy(&((struct lpfc_iocbq *)cmdiocbq->context2)->iocb,
9893 &rspiocbq->iocb, sizeof(IOCB_t));
9894
9895 /* Set the exchange busy flag for task management commands */
9896 if ((cmdiocbq->iocb_flag & LPFC_IO_FCP) &&
9897 !(cmdiocbq->iocb_flag & LPFC_IO_LIBDFC)) {
9898 lpfc_cmd = container_of(cmdiocbq, struct lpfc_scsi_buf,
9899 cur_iocbq);
9900 lpfc_cmd->exch_busy = rspiocbq->iocb_flag & LPFC_EXCHANGE_BUSY;
9901 }
9902
9903 pdone_q = cmdiocbq->context_un.wait_queue;
9904 if (pdone_q)
9905 wake_up(pdone_q);
9906 spin_unlock_irqrestore(&phba->hbalock, iflags);
9907 return;
9908 }
9909
9910 /**
9911 * lpfc_chk_iocb_flg - Test IOCB flag with lock held.
9912 * @phba: Pointer to HBA context object..
9913 * @piocbq: Pointer to command iocb.
9914 * @flag: Flag to test.
9915 *
9916 * This routine grabs the hbalock and then test the iocb_flag to
9917 * see if the passed in flag is set.
9918 * Returns:
9919 * 1 if flag is set.
9920 * 0 if flag is not set.
9921 **/
9922 static int
9923 lpfc_chk_iocb_flg(struct lpfc_hba *phba,
9924 struct lpfc_iocbq *piocbq, uint32_t flag)
9925 {
9926 unsigned long iflags;
9927 int ret;
9928
9929 spin_lock_irqsave(&phba->hbalock, iflags);
9930 ret = piocbq->iocb_flag & flag;
9931 spin_unlock_irqrestore(&phba->hbalock, iflags);
9932 return ret;
9933
9934 }
9935
9936 /**
9937 * lpfc_sli_issue_iocb_wait - Synchronous function to issue iocb commands
9938 * @phba: Pointer to HBA context object..
9939 * @pring: Pointer to sli ring.
9940 * @piocb: Pointer to command iocb.
9941 * @prspiocbq: Pointer to response iocb.
9942 * @timeout: Timeout in number of seconds.
9943 *
9944 * This function issues the iocb to firmware and waits for the
9945 * iocb to complete. If the iocb command is not
9946 * completed within timeout seconds, it returns IOCB_TIMEDOUT.
9947 * Caller should not free the iocb resources if this function
9948 * returns IOCB_TIMEDOUT.
9949 * The function waits for the iocb completion using an
9950 * non-interruptible wait.
9951 * This function will sleep while waiting for iocb completion.
9952 * So, this function should not be called from any context which
9953 * does not allow sleeping. Due to the same reason, this function
9954 * cannot be called with interrupt disabled.
9955 * This function assumes that the iocb completions occur while
9956 * this function sleep. So, this function cannot be called from
9957 * the thread which process iocb completion for this ring.
9958 * This function clears the iocb_flag of the iocb object before
9959 * issuing the iocb and the iocb completion handler sets this
9960 * flag and wakes this thread when the iocb completes.
9961 * The contents of the response iocb will be copied to prspiocbq
9962 * by the completion handler when the command completes.
9963 * This function returns IOCB_SUCCESS when success.
9964 * This function is called with no lock held.
9965 **/
9966 int
9967 lpfc_sli_issue_iocb_wait(struct lpfc_hba *phba,
9968 uint32_t ring_number,
9969 struct lpfc_iocbq *piocb,
9970 struct lpfc_iocbq *prspiocbq,
9971 uint32_t timeout)
9972 {
9973 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
9974 long timeleft, timeout_req = 0;
9975 int retval = IOCB_SUCCESS;
9976 uint32_t creg_val;
9977 struct lpfc_iocbq *iocb;
9978 int txq_cnt = 0;
9979 int txcmplq_cnt = 0;
9980 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
9981 /*
9982 * If the caller has provided a response iocbq buffer, then context2
9983 * is NULL or its an error.
9984 */
9985 if (prspiocbq) {
9986 if (piocb->context2)
9987 return IOCB_ERROR;
9988 piocb->context2 = prspiocbq;
9989 }
9990
9991 piocb->iocb_cmpl = lpfc_sli_wake_iocb_wait;
9992 piocb->context_un.wait_queue = &done_q;
9993 piocb->iocb_flag &= ~LPFC_IO_WAKE;
9994
9995 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
9996 if (lpfc_readl(phba->HCregaddr, &creg_val))
9997 return IOCB_ERROR;
9998 creg_val |= (HC_R0INT_ENA << LPFC_FCP_RING);
9999 writel(creg_val, phba->HCregaddr);
10000 readl(phba->HCregaddr); /* flush */
10001 }
10002
10003 retval = lpfc_sli_issue_iocb(phba, ring_number, piocb,
10004 SLI_IOCB_RET_IOCB);
10005 if (retval == IOCB_SUCCESS) {
10006 timeout_req = timeout * HZ;
10007 timeleft = wait_event_timeout(done_q,
10008 lpfc_chk_iocb_flg(phba, piocb, LPFC_IO_WAKE),
10009 timeout_req);
10010
10011 if (piocb->iocb_flag & LPFC_IO_WAKE) {
10012 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10013 "0331 IOCB wake signaled\n");
10014 } else if (timeleft == 0) {
10015 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10016 "0338 IOCB wait timeout error - no "
10017 "wake response Data x%x\n", timeout);
10018 retval = IOCB_TIMEDOUT;
10019 } else {
10020 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
10021 "0330 IOCB wake NOT set, "
10022 "Data x%x x%lx\n",
10023 timeout, (timeleft / jiffies));
10024 retval = IOCB_TIMEDOUT;
10025 }
10026 } else if (retval == IOCB_BUSY) {
10027 if (phba->cfg_log_verbose & LOG_SLI) {
10028 list_for_each_entry(iocb, &pring->txq, list) {
10029 txq_cnt++;
10030 }
10031 list_for_each_entry(iocb, &pring->txcmplq, list) {
10032 txcmplq_cnt++;
10033 }
10034 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10035 "2818 Max IOCBs %d txq cnt %d txcmplq cnt %d\n",
10036 phba->iocb_cnt, txq_cnt, txcmplq_cnt);
10037 }
10038 return retval;
10039 } else {
10040 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
10041 "0332 IOCB wait issue failed, Data x%x\n",
10042 retval);
10043 retval = IOCB_ERROR;
10044 }
10045
10046 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
10047 if (lpfc_readl(phba->HCregaddr, &creg_val))
10048 return IOCB_ERROR;
10049 creg_val &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
10050 writel(creg_val, phba->HCregaddr);
10051 readl(phba->HCregaddr); /* flush */
10052 }
10053
10054 if (prspiocbq)
10055 piocb->context2 = NULL;
10056
10057 piocb->context_un.wait_queue = NULL;
10058 piocb->iocb_cmpl = NULL;
10059 return retval;
10060 }
10061
10062 /**
10063 * lpfc_sli_issue_mbox_wait - Synchronous function to issue mailbox
10064 * @phba: Pointer to HBA context object.
10065 * @pmboxq: Pointer to driver mailbox object.
10066 * @timeout: Timeout in number of seconds.
10067 *
10068 * This function issues the mailbox to firmware and waits for the
10069 * mailbox command to complete. If the mailbox command is not
10070 * completed within timeout seconds, it returns MBX_TIMEOUT.
10071 * The function waits for the mailbox completion using an
10072 * interruptible wait. If the thread is woken up due to a
10073 * signal, MBX_TIMEOUT error is returned to the caller. Caller
10074 * should not free the mailbox resources, if this function returns
10075 * MBX_TIMEOUT.
10076 * This function will sleep while waiting for mailbox completion.
10077 * So, this function should not be called from any context which
10078 * does not allow sleeping. Due to the same reason, this function
10079 * cannot be called with interrupt disabled.
10080 * This function assumes that the mailbox completion occurs while
10081 * this function sleep. So, this function cannot be called from
10082 * the worker thread which processes mailbox completion.
10083 * This function is called in the context of HBA management
10084 * applications.
10085 * This function returns MBX_SUCCESS when successful.
10086 * This function is called with no lock held.
10087 **/
10088 int
10089 lpfc_sli_issue_mbox_wait(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq,
10090 uint32_t timeout)
10091 {
10092 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(done_q);
10093 int retval;
10094 unsigned long flag;
10095
10096 /* The caller must leave context1 empty. */
10097 if (pmboxq->context1)
10098 return MBX_NOT_FINISHED;
10099
10100 pmboxq->mbox_flag &= ~LPFC_MBX_WAKE;
10101 /* setup wake call as IOCB callback */
10102 pmboxq->mbox_cmpl = lpfc_sli_wake_mbox_wait;
10103 /* setup context field to pass wait_queue pointer to wake function */
10104 pmboxq->context1 = &done_q;
10105
10106 /* now issue the command */
10107 retval = lpfc_sli_issue_mbox(phba, pmboxq, MBX_NOWAIT);
10108 if (retval == MBX_BUSY || retval == MBX_SUCCESS) {
10109 wait_event_interruptible_timeout(done_q,
10110 pmboxq->mbox_flag & LPFC_MBX_WAKE,
10111 timeout * HZ);
10112
10113 spin_lock_irqsave(&phba->hbalock, flag);
10114 pmboxq->context1 = NULL;
10115 /*
10116 * if LPFC_MBX_WAKE flag is set the mailbox is completed
10117 * else do not free the resources.
10118 */
10119 if (pmboxq->mbox_flag & LPFC_MBX_WAKE) {
10120 retval = MBX_SUCCESS;
10121 lpfc_sli4_swap_str(phba, pmboxq);
10122 } else {
10123 retval = MBX_TIMEOUT;
10124 pmboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
10125 }
10126 spin_unlock_irqrestore(&phba->hbalock, flag);
10127 }
10128
10129 return retval;
10130 }
10131
10132 /**
10133 * lpfc_sli_mbox_sys_shutdown - shutdown mailbox command sub-system
10134 * @phba: Pointer to HBA context.
10135 *
10136 * This function is called to shutdown the driver's mailbox sub-system.
10137 * It first marks the mailbox sub-system is in a block state to prevent
10138 * the asynchronous mailbox command from issued off the pending mailbox
10139 * command queue. If the mailbox command sub-system shutdown is due to
10140 * HBA error conditions such as EEH or ERATT, this routine shall invoke
10141 * the mailbox sub-system flush routine to forcefully bring down the
10142 * mailbox sub-system. Otherwise, if it is due to normal condition (such
10143 * as with offline or HBA function reset), this routine will wait for the
10144 * outstanding mailbox command to complete before invoking the mailbox
10145 * sub-system flush routine to gracefully bring down mailbox sub-system.
10146 **/
10147 void
10148 lpfc_sli_mbox_sys_shutdown(struct lpfc_hba *phba, int mbx_action)
10149 {
10150 struct lpfc_sli *psli = &phba->sli;
10151 unsigned long timeout;
10152
10153 if (mbx_action == LPFC_MBX_NO_WAIT) {
10154 /* delay 100ms for port state */
10155 msleep(100);
10156 lpfc_sli_mbox_sys_flush(phba);
10157 return;
10158 }
10159 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
10160
10161 spin_lock_irq(&phba->hbalock);
10162 psli->sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
10163
10164 if (psli->sli_flag & LPFC_SLI_ACTIVE) {
10165 /* Determine how long we might wait for the active mailbox
10166 * command to be gracefully completed by firmware.
10167 */
10168 if (phba->sli.mbox_active)
10169 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
10170 phba->sli.mbox_active) *
10171 1000) + jiffies;
10172 spin_unlock_irq(&phba->hbalock);
10173
10174 while (phba->sli.mbox_active) {
10175 /* Check active mailbox complete status every 2ms */
10176 msleep(2);
10177 if (time_after(jiffies, timeout))
10178 /* Timeout, let the mailbox flush routine to
10179 * forcefully release active mailbox command
10180 */
10181 break;
10182 }
10183 } else
10184 spin_unlock_irq(&phba->hbalock);
10185
10186 lpfc_sli_mbox_sys_flush(phba);
10187 }
10188
10189 /**
10190 * lpfc_sli_eratt_read - read sli-3 error attention events
10191 * @phba: Pointer to HBA context.
10192 *
10193 * This function is called to read the SLI3 device error attention registers
10194 * for possible error attention events. The caller must hold the hostlock
10195 * with spin_lock_irq().
10196 *
10197 * This function returns 1 when there is Error Attention in the Host Attention
10198 * Register and returns 0 otherwise.
10199 **/
10200 static int
10201 lpfc_sli_eratt_read(struct lpfc_hba *phba)
10202 {
10203 uint32_t ha_copy;
10204
10205 /* Read chip Host Attention (HA) register */
10206 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10207 goto unplug_err;
10208
10209 if (ha_copy & HA_ERATT) {
10210 /* Read host status register to retrieve error event */
10211 if (lpfc_sli_read_hs(phba))
10212 goto unplug_err;
10213
10214 /* Check if there is a deferred error condition is active */
10215 if ((HS_FFER1 & phba->work_hs) &&
10216 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10217 HS_FFER6 | HS_FFER7 | HS_FFER8) & phba->work_hs)) {
10218 phba->hba_flag |= DEFER_ERATT;
10219 /* Clear all interrupt enable conditions */
10220 writel(0, phba->HCregaddr);
10221 readl(phba->HCregaddr);
10222 }
10223
10224 /* Set the driver HA work bitmap */
10225 phba->work_ha |= HA_ERATT;
10226 /* Indicate polling handles this ERATT */
10227 phba->hba_flag |= HBA_ERATT_HANDLED;
10228 return 1;
10229 }
10230 return 0;
10231
10232 unplug_err:
10233 /* Set the driver HS work bitmap */
10234 phba->work_hs |= UNPLUG_ERR;
10235 /* Set the driver HA work bitmap */
10236 phba->work_ha |= HA_ERATT;
10237 /* Indicate polling handles this ERATT */
10238 phba->hba_flag |= HBA_ERATT_HANDLED;
10239 return 1;
10240 }
10241
10242 /**
10243 * lpfc_sli4_eratt_read - read sli-4 error attention events
10244 * @phba: Pointer to HBA context.
10245 *
10246 * This function is called to read the SLI4 device error attention registers
10247 * for possible error attention events. The caller must hold the hostlock
10248 * with spin_lock_irq().
10249 *
10250 * This function returns 1 when there is Error Attention in the Host Attention
10251 * Register and returns 0 otherwise.
10252 **/
10253 static int
10254 lpfc_sli4_eratt_read(struct lpfc_hba *phba)
10255 {
10256 uint32_t uerr_sta_hi, uerr_sta_lo;
10257 uint32_t if_type, portsmphr;
10258 struct lpfc_register portstat_reg;
10259
10260 /*
10261 * For now, use the SLI4 device internal unrecoverable error
10262 * registers for error attention. This can be changed later.
10263 */
10264 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
10265 switch (if_type) {
10266 case LPFC_SLI_INTF_IF_TYPE_0:
10267 if (lpfc_readl(phba->sli4_hba.u.if_type0.UERRLOregaddr,
10268 &uerr_sta_lo) ||
10269 lpfc_readl(phba->sli4_hba.u.if_type0.UERRHIregaddr,
10270 &uerr_sta_hi)) {
10271 phba->work_hs |= UNPLUG_ERR;
10272 phba->work_ha |= HA_ERATT;
10273 phba->hba_flag |= HBA_ERATT_HANDLED;
10274 return 1;
10275 }
10276 if ((~phba->sli4_hba.ue_mask_lo & uerr_sta_lo) ||
10277 (~phba->sli4_hba.ue_mask_hi & uerr_sta_hi)) {
10278 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10279 "1423 HBA Unrecoverable error: "
10280 "uerr_lo_reg=0x%x, uerr_hi_reg=0x%x, "
10281 "ue_mask_lo_reg=0x%x, "
10282 "ue_mask_hi_reg=0x%x\n",
10283 uerr_sta_lo, uerr_sta_hi,
10284 phba->sli4_hba.ue_mask_lo,
10285 phba->sli4_hba.ue_mask_hi);
10286 phba->work_status[0] = uerr_sta_lo;
10287 phba->work_status[1] = uerr_sta_hi;
10288 phba->work_ha |= HA_ERATT;
10289 phba->hba_flag |= HBA_ERATT_HANDLED;
10290 return 1;
10291 }
10292 break;
10293 case LPFC_SLI_INTF_IF_TYPE_2:
10294 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
10295 &portstat_reg.word0) ||
10296 lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
10297 &portsmphr)){
10298 phba->work_hs |= UNPLUG_ERR;
10299 phba->work_ha |= HA_ERATT;
10300 phba->hba_flag |= HBA_ERATT_HANDLED;
10301 return 1;
10302 }
10303 if (bf_get(lpfc_sliport_status_err, &portstat_reg)) {
10304 phba->work_status[0] =
10305 readl(phba->sli4_hba.u.if_type2.ERR1regaddr);
10306 phba->work_status[1] =
10307 readl(phba->sli4_hba.u.if_type2.ERR2regaddr);
10308 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10309 "2885 Port Status Event: "
10310 "port status reg 0x%x, "
10311 "port smphr reg 0x%x, "
10312 "error 1=0x%x, error 2=0x%x\n",
10313 portstat_reg.word0,
10314 portsmphr,
10315 phba->work_status[0],
10316 phba->work_status[1]);
10317 phba->work_ha |= HA_ERATT;
10318 phba->hba_flag |= HBA_ERATT_HANDLED;
10319 return 1;
10320 }
10321 break;
10322 case LPFC_SLI_INTF_IF_TYPE_1:
10323 default:
10324 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10325 "2886 HBA Error Attention on unsupported "
10326 "if type %d.", if_type);
10327 return 1;
10328 }
10329
10330 return 0;
10331 }
10332
10333 /**
10334 * lpfc_sli_check_eratt - check error attention events
10335 * @phba: Pointer to HBA context.
10336 *
10337 * This function is called from timer soft interrupt context to check HBA's
10338 * error attention register bit for error attention events.
10339 *
10340 * This function returns 1 when there is Error Attention in the Host Attention
10341 * Register and returns 0 otherwise.
10342 **/
10343 int
10344 lpfc_sli_check_eratt(struct lpfc_hba *phba)
10345 {
10346 uint32_t ha_copy;
10347
10348 /* If somebody is waiting to handle an eratt, don't process it
10349 * here. The brdkill function will do this.
10350 */
10351 if (phba->link_flag & LS_IGNORE_ERATT)
10352 return 0;
10353
10354 /* Check if interrupt handler handles this ERATT */
10355 spin_lock_irq(&phba->hbalock);
10356 if (phba->hba_flag & HBA_ERATT_HANDLED) {
10357 /* Interrupt handler has handled ERATT */
10358 spin_unlock_irq(&phba->hbalock);
10359 return 0;
10360 }
10361
10362 /*
10363 * If there is deferred error attention, do not check for error
10364 * attention
10365 */
10366 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10367 spin_unlock_irq(&phba->hbalock);
10368 return 0;
10369 }
10370
10371 /* If PCI channel is offline, don't process it */
10372 if (unlikely(pci_channel_offline(phba->pcidev))) {
10373 spin_unlock_irq(&phba->hbalock);
10374 return 0;
10375 }
10376
10377 switch (phba->sli_rev) {
10378 case LPFC_SLI_REV2:
10379 case LPFC_SLI_REV3:
10380 /* Read chip Host Attention (HA) register */
10381 ha_copy = lpfc_sli_eratt_read(phba);
10382 break;
10383 case LPFC_SLI_REV4:
10384 /* Read device Uncoverable Error (UERR) registers */
10385 ha_copy = lpfc_sli4_eratt_read(phba);
10386 break;
10387 default:
10388 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
10389 "0299 Invalid SLI revision (%d)\n",
10390 phba->sli_rev);
10391 ha_copy = 0;
10392 break;
10393 }
10394 spin_unlock_irq(&phba->hbalock);
10395
10396 return ha_copy;
10397 }
10398
10399 /**
10400 * lpfc_intr_state_check - Check device state for interrupt handling
10401 * @phba: Pointer to HBA context.
10402 *
10403 * This inline routine checks whether a device or its PCI slot is in a state
10404 * that the interrupt should be handled.
10405 *
10406 * This function returns 0 if the device or the PCI slot is in a state that
10407 * interrupt should be handled, otherwise -EIO.
10408 */
10409 static inline int
10410 lpfc_intr_state_check(struct lpfc_hba *phba)
10411 {
10412 /* If the pci channel is offline, ignore all the interrupts */
10413 if (unlikely(pci_channel_offline(phba->pcidev)))
10414 return -EIO;
10415
10416 /* Update device level interrupt statistics */
10417 phba->sli.slistat.sli_intr++;
10418
10419 /* Ignore all interrupts during initialization. */
10420 if (unlikely(phba->link_state < LPFC_LINK_DOWN))
10421 return -EIO;
10422
10423 return 0;
10424 }
10425
10426 /**
10427 * lpfc_sli_sp_intr_handler - Slow-path interrupt handler to SLI-3 device
10428 * @irq: Interrupt number.
10429 * @dev_id: The device context pointer.
10430 *
10431 * This function is directly called from the PCI layer as an interrupt
10432 * service routine when device with SLI-3 interface spec is enabled with
10433 * MSI-X multi-message interrupt mode and there are slow-path events in
10434 * the HBA. However, when the device is enabled with either MSI or Pin-IRQ
10435 * interrupt mode, this function is called as part of the device-level
10436 * interrupt handler. When the PCI slot is in error recovery or the HBA
10437 * is undergoing initialization, the interrupt handler will not process
10438 * the interrupt. The link attention and ELS ring attention events are
10439 * handled by the worker thread. The interrupt handler signals the worker
10440 * thread and returns for these events. This function is called without
10441 * any lock held. It gets the hbalock to access and update SLI data
10442 * structures.
10443 *
10444 * This function returns IRQ_HANDLED when interrupt is handled else it
10445 * returns IRQ_NONE.
10446 **/
10447 irqreturn_t
10448 lpfc_sli_sp_intr_handler(int irq, void *dev_id)
10449 {
10450 struct lpfc_hba *phba;
10451 uint32_t ha_copy, hc_copy;
10452 uint32_t work_ha_copy;
10453 unsigned long status;
10454 unsigned long iflag;
10455 uint32_t control;
10456
10457 MAILBOX_t *mbox, *pmbox;
10458 struct lpfc_vport *vport;
10459 struct lpfc_nodelist *ndlp;
10460 struct lpfc_dmabuf *mp;
10461 LPFC_MBOXQ_t *pmb;
10462 int rc;
10463
10464 /*
10465 * Get the driver's phba structure from the dev_id and
10466 * assume the HBA is not interrupting.
10467 */
10468 phba = (struct lpfc_hba *)dev_id;
10469
10470 if (unlikely(!phba))
10471 return IRQ_NONE;
10472
10473 /*
10474 * Stuff needs to be attented to when this function is invoked as an
10475 * individual interrupt handler in MSI-X multi-message interrupt mode
10476 */
10477 if (phba->intr_type == MSIX) {
10478 /* Check device state for handling interrupt */
10479 if (lpfc_intr_state_check(phba))
10480 return IRQ_NONE;
10481 /* Need to read HA REG for slow-path events */
10482 spin_lock_irqsave(&phba->hbalock, iflag);
10483 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10484 goto unplug_error;
10485 /* If somebody is waiting to handle an eratt don't process it
10486 * here. The brdkill function will do this.
10487 */
10488 if (phba->link_flag & LS_IGNORE_ERATT)
10489 ha_copy &= ~HA_ERATT;
10490 /* Check the need for handling ERATT in interrupt handler */
10491 if (ha_copy & HA_ERATT) {
10492 if (phba->hba_flag & HBA_ERATT_HANDLED)
10493 /* ERATT polling has handled ERATT */
10494 ha_copy &= ~HA_ERATT;
10495 else
10496 /* Indicate interrupt handler handles ERATT */
10497 phba->hba_flag |= HBA_ERATT_HANDLED;
10498 }
10499
10500 /*
10501 * If there is deferred error attention, do not check for any
10502 * interrupt.
10503 */
10504 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10505 spin_unlock_irqrestore(&phba->hbalock, iflag);
10506 return IRQ_NONE;
10507 }
10508
10509 /* Clear up only attention source related to slow-path */
10510 if (lpfc_readl(phba->HCregaddr, &hc_copy))
10511 goto unplug_error;
10512
10513 writel(hc_copy & ~(HC_MBINT_ENA | HC_R2INT_ENA |
10514 HC_LAINT_ENA | HC_ERINT_ENA),
10515 phba->HCregaddr);
10516 writel((ha_copy & (HA_MBATT | HA_R2_CLR_MSK)),
10517 phba->HAregaddr);
10518 writel(hc_copy, phba->HCregaddr);
10519 readl(phba->HAregaddr); /* flush */
10520 spin_unlock_irqrestore(&phba->hbalock, iflag);
10521 } else
10522 ha_copy = phba->ha_copy;
10523
10524 work_ha_copy = ha_copy & phba->work_ha_mask;
10525
10526 if (work_ha_copy) {
10527 if (work_ha_copy & HA_LATT) {
10528 if (phba->sli.sli_flag & LPFC_PROCESS_LA) {
10529 /*
10530 * Turn off Link Attention interrupts
10531 * until CLEAR_LA done
10532 */
10533 spin_lock_irqsave(&phba->hbalock, iflag);
10534 phba->sli.sli_flag &= ~LPFC_PROCESS_LA;
10535 if (lpfc_readl(phba->HCregaddr, &control))
10536 goto unplug_error;
10537 control &= ~HC_LAINT_ENA;
10538 writel(control, phba->HCregaddr);
10539 readl(phba->HCregaddr); /* flush */
10540 spin_unlock_irqrestore(&phba->hbalock, iflag);
10541 }
10542 else
10543 work_ha_copy &= ~HA_LATT;
10544 }
10545
10546 if (work_ha_copy & ~(HA_ERATT | HA_MBATT | HA_LATT)) {
10547 /*
10548 * Turn off Slow Rings interrupts, LPFC_ELS_RING is
10549 * the only slow ring.
10550 */
10551 status = (work_ha_copy &
10552 (HA_RXMASK << (4*LPFC_ELS_RING)));
10553 status >>= (4*LPFC_ELS_RING);
10554 if (status & HA_RXMASK) {
10555 spin_lock_irqsave(&phba->hbalock, iflag);
10556 if (lpfc_readl(phba->HCregaddr, &control))
10557 goto unplug_error;
10558
10559 lpfc_debugfs_slow_ring_trc(phba,
10560 "ISR slow ring: ctl:x%x stat:x%x isrcnt:x%x",
10561 control, status,
10562 (uint32_t)phba->sli.slistat.sli_intr);
10563
10564 if (control & (HC_R0INT_ENA << LPFC_ELS_RING)) {
10565 lpfc_debugfs_slow_ring_trc(phba,
10566 "ISR Disable ring:"
10567 "pwork:x%x hawork:x%x wait:x%x",
10568 phba->work_ha, work_ha_copy,
10569 (uint32_t)((unsigned long)
10570 &phba->work_waitq));
10571
10572 control &=
10573 ~(HC_R0INT_ENA << LPFC_ELS_RING);
10574 writel(control, phba->HCregaddr);
10575 readl(phba->HCregaddr); /* flush */
10576 }
10577 else {
10578 lpfc_debugfs_slow_ring_trc(phba,
10579 "ISR slow ring: pwork:"
10580 "x%x hawork:x%x wait:x%x",
10581 phba->work_ha, work_ha_copy,
10582 (uint32_t)((unsigned long)
10583 &phba->work_waitq));
10584 }
10585 spin_unlock_irqrestore(&phba->hbalock, iflag);
10586 }
10587 }
10588 spin_lock_irqsave(&phba->hbalock, iflag);
10589 if (work_ha_copy & HA_ERATT) {
10590 if (lpfc_sli_read_hs(phba))
10591 goto unplug_error;
10592 /*
10593 * Check if there is a deferred error condition
10594 * is active
10595 */
10596 if ((HS_FFER1 & phba->work_hs) &&
10597 ((HS_FFER2 | HS_FFER3 | HS_FFER4 | HS_FFER5 |
10598 HS_FFER6 | HS_FFER7 | HS_FFER8) &
10599 phba->work_hs)) {
10600 phba->hba_flag |= DEFER_ERATT;
10601 /* Clear all interrupt enable conditions */
10602 writel(0, phba->HCregaddr);
10603 readl(phba->HCregaddr);
10604 }
10605 }
10606
10607 if ((work_ha_copy & HA_MBATT) && (phba->sli.mbox_active)) {
10608 pmb = phba->sli.mbox_active;
10609 pmbox = &pmb->u.mb;
10610 mbox = phba->mbox;
10611 vport = pmb->vport;
10612
10613 /* First check out the status word */
10614 lpfc_sli_pcimem_bcopy(mbox, pmbox, sizeof(uint32_t));
10615 if (pmbox->mbxOwner != OWN_HOST) {
10616 spin_unlock_irqrestore(&phba->hbalock, iflag);
10617 /*
10618 * Stray Mailbox Interrupt, mbxCommand <cmd>
10619 * mbxStatus <status>
10620 */
10621 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10622 LOG_SLI,
10623 "(%d):0304 Stray Mailbox "
10624 "Interrupt mbxCommand x%x "
10625 "mbxStatus x%x\n",
10626 (vport ? vport->vpi : 0),
10627 pmbox->mbxCommand,
10628 pmbox->mbxStatus);
10629 /* clear mailbox attention bit */
10630 work_ha_copy &= ~HA_MBATT;
10631 } else {
10632 phba->sli.mbox_active = NULL;
10633 spin_unlock_irqrestore(&phba->hbalock, iflag);
10634 phba->last_completion_time = jiffies;
10635 del_timer(&phba->sli.mbox_tmo);
10636 if (pmb->mbox_cmpl) {
10637 lpfc_sli_pcimem_bcopy(mbox, pmbox,
10638 MAILBOX_CMD_SIZE);
10639 if (pmb->out_ext_byte_len &&
10640 pmb->context2)
10641 lpfc_sli_pcimem_bcopy(
10642 phba->mbox_ext,
10643 pmb->context2,
10644 pmb->out_ext_byte_len);
10645 }
10646 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
10647 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
10648
10649 lpfc_debugfs_disc_trc(vport,
10650 LPFC_DISC_TRC_MBOX_VPORT,
10651 "MBOX dflt rpi: : "
10652 "status:x%x rpi:x%x",
10653 (uint32_t)pmbox->mbxStatus,
10654 pmbox->un.varWords[0], 0);
10655
10656 if (!pmbox->mbxStatus) {
10657 mp = (struct lpfc_dmabuf *)
10658 (pmb->context1);
10659 ndlp = (struct lpfc_nodelist *)
10660 pmb->context2;
10661
10662 /* Reg_LOGIN of dflt RPI was
10663 * successful. new lets get
10664 * rid of the RPI using the
10665 * same mbox buffer.
10666 */
10667 lpfc_unreg_login(phba,
10668 vport->vpi,
10669 pmbox->un.varWords[0],
10670 pmb);
10671 pmb->mbox_cmpl =
10672 lpfc_mbx_cmpl_dflt_rpi;
10673 pmb->context1 = mp;
10674 pmb->context2 = ndlp;
10675 pmb->vport = vport;
10676 rc = lpfc_sli_issue_mbox(phba,
10677 pmb,
10678 MBX_NOWAIT);
10679 if (rc != MBX_BUSY)
10680 lpfc_printf_log(phba,
10681 KERN_ERR,
10682 LOG_MBOX | LOG_SLI,
10683 "0350 rc should have"
10684 "been MBX_BUSY\n");
10685 if (rc != MBX_NOT_FINISHED)
10686 goto send_current_mbox;
10687 }
10688 }
10689 spin_lock_irqsave(
10690 &phba->pport->work_port_lock,
10691 iflag);
10692 phba->pport->work_port_events &=
10693 ~WORKER_MBOX_TMO;
10694 spin_unlock_irqrestore(
10695 &phba->pport->work_port_lock,
10696 iflag);
10697 lpfc_mbox_cmpl_put(phba, pmb);
10698 }
10699 } else
10700 spin_unlock_irqrestore(&phba->hbalock, iflag);
10701
10702 if ((work_ha_copy & HA_MBATT) &&
10703 (phba->sli.mbox_active == NULL)) {
10704 send_current_mbox:
10705 /* Process next mailbox command if there is one */
10706 do {
10707 rc = lpfc_sli_issue_mbox(phba, NULL,
10708 MBX_NOWAIT);
10709 } while (rc == MBX_NOT_FINISHED);
10710 if (rc != MBX_SUCCESS)
10711 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
10712 LOG_SLI, "0349 rc should be "
10713 "MBX_SUCCESS\n");
10714 }
10715
10716 spin_lock_irqsave(&phba->hbalock, iflag);
10717 phba->work_ha |= work_ha_copy;
10718 spin_unlock_irqrestore(&phba->hbalock, iflag);
10719 lpfc_worker_wake_up(phba);
10720 }
10721 return IRQ_HANDLED;
10722 unplug_error:
10723 spin_unlock_irqrestore(&phba->hbalock, iflag);
10724 return IRQ_HANDLED;
10725
10726 } /* lpfc_sli_sp_intr_handler */
10727
10728 /**
10729 * lpfc_sli_fp_intr_handler - Fast-path interrupt handler to SLI-3 device.
10730 * @irq: Interrupt number.
10731 * @dev_id: The device context pointer.
10732 *
10733 * This function is directly called from the PCI layer as an interrupt
10734 * service routine when device with SLI-3 interface spec is enabled with
10735 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
10736 * ring event in the HBA. However, when the device is enabled with either
10737 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
10738 * device-level interrupt handler. When the PCI slot is in error recovery
10739 * or the HBA is undergoing initialization, the interrupt handler will not
10740 * process the interrupt. The SCSI FCP fast-path ring event are handled in
10741 * the intrrupt context. This function is called without any lock held.
10742 * It gets the hbalock to access and update SLI data structures.
10743 *
10744 * This function returns IRQ_HANDLED when interrupt is handled else it
10745 * returns IRQ_NONE.
10746 **/
10747 irqreturn_t
10748 lpfc_sli_fp_intr_handler(int irq, void *dev_id)
10749 {
10750 struct lpfc_hba *phba;
10751 uint32_t ha_copy;
10752 unsigned long status;
10753 unsigned long iflag;
10754
10755 /* Get the driver's phba structure from the dev_id and
10756 * assume the HBA is not interrupting.
10757 */
10758 phba = (struct lpfc_hba *) dev_id;
10759
10760 if (unlikely(!phba))
10761 return IRQ_NONE;
10762
10763 /*
10764 * Stuff needs to be attented to when this function is invoked as an
10765 * individual interrupt handler in MSI-X multi-message interrupt mode
10766 */
10767 if (phba->intr_type == MSIX) {
10768 /* Check device state for handling interrupt */
10769 if (lpfc_intr_state_check(phba))
10770 return IRQ_NONE;
10771 /* Need to read HA REG for FCP ring and other ring events */
10772 if (lpfc_readl(phba->HAregaddr, &ha_copy))
10773 return IRQ_HANDLED;
10774 /* Clear up only attention source related to fast-path */
10775 spin_lock_irqsave(&phba->hbalock, iflag);
10776 /*
10777 * If there is deferred error attention, do not check for
10778 * any interrupt.
10779 */
10780 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10781 spin_unlock_irqrestore(&phba->hbalock, iflag);
10782 return IRQ_NONE;
10783 }
10784 writel((ha_copy & (HA_R0_CLR_MSK | HA_R1_CLR_MSK)),
10785 phba->HAregaddr);
10786 readl(phba->HAregaddr); /* flush */
10787 spin_unlock_irqrestore(&phba->hbalock, iflag);
10788 } else
10789 ha_copy = phba->ha_copy;
10790
10791 /*
10792 * Process all events on FCP ring. Take the optimized path for FCP IO.
10793 */
10794 ha_copy &= ~(phba->work_ha_mask);
10795
10796 status = (ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10797 status >>= (4*LPFC_FCP_RING);
10798 if (status & HA_RXMASK)
10799 lpfc_sli_handle_fast_ring_event(phba,
10800 &phba->sli.ring[LPFC_FCP_RING],
10801 status);
10802
10803 if (phba->cfg_multi_ring_support == 2) {
10804 /*
10805 * Process all events on extra ring. Take the optimized path
10806 * for extra ring IO.
10807 */
10808 status = (ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10809 status >>= (4*LPFC_EXTRA_RING);
10810 if (status & HA_RXMASK) {
10811 lpfc_sli_handle_fast_ring_event(phba,
10812 &phba->sli.ring[LPFC_EXTRA_RING],
10813 status);
10814 }
10815 }
10816 return IRQ_HANDLED;
10817 } /* lpfc_sli_fp_intr_handler */
10818
10819 /**
10820 * lpfc_sli_intr_handler - Device-level interrupt handler to SLI-3 device
10821 * @irq: Interrupt number.
10822 * @dev_id: The device context pointer.
10823 *
10824 * This function is the HBA device-level interrupt handler to device with
10825 * SLI-3 interface spec, called from the PCI layer when either MSI or
10826 * Pin-IRQ interrupt mode is enabled and there is an event in the HBA which
10827 * requires driver attention. This function invokes the slow-path interrupt
10828 * attention handling function and fast-path interrupt attention handling
10829 * function in turn to process the relevant HBA attention events. This
10830 * function is called without any lock held. It gets the hbalock to access
10831 * and update SLI data structures.
10832 *
10833 * This function returns IRQ_HANDLED when interrupt is handled, else it
10834 * returns IRQ_NONE.
10835 **/
10836 irqreturn_t
10837 lpfc_sli_intr_handler(int irq, void *dev_id)
10838 {
10839 struct lpfc_hba *phba;
10840 irqreturn_t sp_irq_rc, fp_irq_rc;
10841 unsigned long status1, status2;
10842 uint32_t hc_copy;
10843
10844 /*
10845 * Get the driver's phba structure from the dev_id and
10846 * assume the HBA is not interrupting.
10847 */
10848 phba = (struct lpfc_hba *) dev_id;
10849
10850 if (unlikely(!phba))
10851 return IRQ_NONE;
10852
10853 /* Check device state for handling interrupt */
10854 if (lpfc_intr_state_check(phba))
10855 return IRQ_NONE;
10856
10857 spin_lock(&phba->hbalock);
10858 if (lpfc_readl(phba->HAregaddr, &phba->ha_copy)) {
10859 spin_unlock(&phba->hbalock);
10860 return IRQ_HANDLED;
10861 }
10862
10863 if (unlikely(!phba->ha_copy)) {
10864 spin_unlock(&phba->hbalock);
10865 return IRQ_NONE;
10866 } else if (phba->ha_copy & HA_ERATT) {
10867 if (phba->hba_flag & HBA_ERATT_HANDLED)
10868 /* ERATT polling has handled ERATT */
10869 phba->ha_copy &= ~HA_ERATT;
10870 else
10871 /* Indicate interrupt handler handles ERATT */
10872 phba->hba_flag |= HBA_ERATT_HANDLED;
10873 }
10874
10875 /*
10876 * If there is deferred error attention, do not check for any interrupt.
10877 */
10878 if (unlikely(phba->hba_flag & DEFER_ERATT)) {
10879 spin_unlock(&phba->hbalock);
10880 return IRQ_NONE;
10881 }
10882
10883 /* Clear attention sources except link and error attentions */
10884 if (lpfc_readl(phba->HCregaddr, &hc_copy)) {
10885 spin_unlock(&phba->hbalock);
10886 return IRQ_HANDLED;
10887 }
10888 writel(hc_copy & ~(HC_MBINT_ENA | HC_R0INT_ENA | HC_R1INT_ENA
10889 | HC_R2INT_ENA | HC_LAINT_ENA | HC_ERINT_ENA),
10890 phba->HCregaddr);
10891 writel((phba->ha_copy & ~(HA_LATT | HA_ERATT)), phba->HAregaddr);
10892 writel(hc_copy, phba->HCregaddr);
10893 readl(phba->HAregaddr); /* flush */
10894 spin_unlock(&phba->hbalock);
10895
10896 /*
10897 * Invokes slow-path host attention interrupt handling as appropriate.
10898 */
10899
10900 /* status of events with mailbox and link attention */
10901 status1 = phba->ha_copy & (HA_MBATT | HA_LATT | HA_ERATT);
10902
10903 /* status of events with ELS ring */
10904 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_ELS_RING)));
10905 status2 >>= (4*LPFC_ELS_RING);
10906
10907 if (status1 || (status2 & HA_RXMASK))
10908 sp_irq_rc = lpfc_sli_sp_intr_handler(irq, dev_id);
10909 else
10910 sp_irq_rc = IRQ_NONE;
10911
10912 /*
10913 * Invoke fast-path host attention interrupt handling as appropriate.
10914 */
10915
10916 /* status of events with FCP ring */
10917 status1 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_FCP_RING)));
10918 status1 >>= (4*LPFC_FCP_RING);
10919
10920 /* status of events with extra ring */
10921 if (phba->cfg_multi_ring_support == 2) {
10922 status2 = (phba->ha_copy & (HA_RXMASK << (4*LPFC_EXTRA_RING)));
10923 status2 >>= (4*LPFC_EXTRA_RING);
10924 } else
10925 status2 = 0;
10926
10927 if ((status1 & HA_RXMASK) || (status2 & HA_RXMASK))
10928 fp_irq_rc = lpfc_sli_fp_intr_handler(irq, dev_id);
10929 else
10930 fp_irq_rc = IRQ_NONE;
10931
10932 /* Return device-level interrupt handling status */
10933 return (sp_irq_rc == IRQ_HANDLED) ? sp_irq_rc : fp_irq_rc;
10934 } /* lpfc_sli_intr_handler */
10935
10936 /**
10937 * lpfc_sli4_fcp_xri_abort_event_proc - Process fcp xri abort event
10938 * @phba: pointer to lpfc hba data structure.
10939 *
10940 * This routine is invoked by the worker thread to process all the pending
10941 * SLI4 FCP abort XRI events.
10942 **/
10943 void lpfc_sli4_fcp_xri_abort_event_proc(struct lpfc_hba *phba)
10944 {
10945 struct lpfc_cq_event *cq_event;
10946
10947 /* First, declare the fcp xri abort event has been handled */
10948 spin_lock_irq(&phba->hbalock);
10949 phba->hba_flag &= ~FCP_XRI_ABORT_EVENT;
10950 spin_unlock_irq(&phba->hbalock);
10951 /* Now, handle all the fcp xri abort events */
10952 while (!list_empty(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue)) {
10953 /* Get the first event from the head of the event queue */
10954 spin_lock_irq(&phba->hbalock);
10955 list_remove_head(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
10956 cq_event, struct lpfc_cq_event, list);
10957 spin_unlock_irq(&phba->hbalock);
10958 /* Notify aborted XRI for FCP work queue */
10959 lpfc_sli4_fcp_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10960 /* Free the event processed back to the free pool */
10961 lpfc_sli4_cq_event_release(phba, cq_event);
10962 }
10963 }
10964
10965 /**
10966 * lpfc_sli4_els_xri_abort_event_proc - Process els xri abort event
10967 * @phba: pointer to lpfc hba data structure.
10968 *
10969 * This routine is invoked by the worker thread to process all the pending
10970 * SLI4 els abort xri events.
10971 **/
10972 void lpfc_sli4_els_xri_abort_event_proc(struct lpfc_hba *phba)
10973 {
10974 struct lpfc_cq_event *cq_event;
10975
10976 /* First, declare the els xri abort event has been handled */
10977 spin_lock_irq(&phba->hbalock);
10978 phba->hba_flag &= ~ELS_XRI_ABORT_EVENT;
10979 spin_unlock_irq(&phba->hbalock);
10980 /* Now, handle all the els xri abort events */
10981 while (!list_empty(&phba->sli4_hba.sp_els_xri_aborted_work_queue)) {
10982 /* Get the first event from the head of the event queue */
10983 spin_lock_irq(&phba->hbalock);
10984 list_remove_head(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
10985 cq_event, struct lpfc_cq_event, list);
10986 spin_unlock_irq(&phba->hbalock);
10987 /* Notify aborted XRI for ELS work queue */
10988 lpfc_sli4_els_xri_aborted(phba, &cq_event->cqe.wcqe_axri);
10989 /* Free the event processed back to the free pool */
10990 lpfc_sli4_cq_event_release(phba, cq_event);
10991 }
10992 }
10993
10994 /**
10995 * lpfc_sli4_iocb_param_transfer - Transfer pIocbOut and cmpl status to pIocbIn
10996 * @phba: pointer to lpfc hba data structure
10997 * @pIocbIn: pointer to the rspiocbq
10998 * @pIocbOut: pointer to the cmdiocbq
10999 * @wcqe: pointer to the complete wcqe
11000 *
11001 * This routine transfers the fields of a command iocbq to a response iocbq
11002 * by copying all the IOCB fields from command iocbq and transferring the
11003 * completion status information from the complete wcqe.
11004 **/
11005 static void
11006 lpfc_sli4_iocb_param_transfer(struct lpfc_hba *phba,
11007 struct lpfc_iocbq *pIocbIn,
11008 struct lpfc_iocbq *pIocbOut,
11009 struct lpfc_wcqe_complete *wcqe)
11010 {
11011 unsigned long iflags;
11012 uint32_t status;
11013 size_t offset = offsetof(struct lpfc_iocbq, iocb);
11014
11015 memcpy((char *)pIocbIn + offset, (char *)pIocbOut + offset,
11016 sizeof(struct lpfc_iocbq) - offset);
11017 /* Map WCQE parameters into irspiocb parameters */
11018 status = bf_get(lpfc_wcqe_c_status, wcqe);
11019 pIocbIn->iocb.ulpStatus = (status & LPFC_IOCB_STATUS_MASK);
11020 if (pIocbOut->iocb_flag & LPFC_IO_FCP)
11021 if (pIocbIn->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
11022 pIocbIn->iocb.un.fcpi.fcpi_parm =
11023 pIocbOut->iocb.un.fcpi.fcpi_parm -
11024 wcqe->total_data_placed;
11025 else
11026 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11027 else {
11028 pIocbIn->iocb.un.ulpWord[4] = wcqe->parameter;
11029 pIocbIn->iocb.un.genreq64.bdl.bdeSize = wcqe->total_data_placed;
11030 }
11031
11032 /* Convert BG errors for completion status */
11033 if (status == CQE_STATUS_DI_ERROR) {
11034 pIocbIn->iocb.ulpStatus = IOSTAT_LOCAL_REJECT;
11035
11036 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
11037 pIocbIn->iocb.un.ulpWord[4] = IOERR_RX_DMA_FAILED;
11038 else
11039 pIocbIn->iocb.un.ulpWord[4] = IOERR_TX_DMA_FAILED;
11040
11041 pIocbIn->iocb.unsli3.sli3_bg.bgstat = 0;
11042 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe)) /* Guard Check failed */
11043 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11044 BGS_GUARD_ERR_MASK;
11045 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe)) /* App Tag Check failed */
11046 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11047 BGS_APPTAG_ERR_MASK;
11048 if (bf_get(lpfc_wcqe_c_bg_re, wcqe)) /* Ref Tag Check failed */
11049 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11050 BGS_REFTAG_ERR_MASK;
11051
11052 /* Check to see if there was any good data before the error */
11053 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
11054 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11055 BGS_HI_WATER_MARK_PRESENT_MASK;
11056 pIocbIn->iocb.unsli3.sli3_bg.bghm =
11057 wcqe->total_data_placed;
11058 }
11059
11060 /*
11061 * Set ALL the error bits to indicate we don't know what
11062 * type of error it is.
11063 */
11064 if (!pIocbIn->iocb.unsli3.sli3_bg.bgstat)
11065 pIocbIn->iocb.unsli3.sli3_bg.bgstat |=
11066 (BGS_REFTAG_ERR_MASK | BGS_APPTAG_ERR_MASK |
11067 BGS_GUARD_ERR_MASK);
11068 }
11069
11070 /* Pick up HBA exchange busy condition */
11071 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
11072 spin_lock_irqsave(&phba->hbalock, iflags);
11073 pIocbIn->iocb_flag |= LPFC_EXCHANGE_BUSY;
11074 spin_unlock_irqrestore(&phba->hbalock, iflags);
11075 }
11076 }
11077
11078 /**
11079 * lpfc_sli4_els_wcqe_to_rspiocbq - Get response iocbq from els wcqe
11080 * @phba: Pointer to HBA context object.
11081 * @wcqe: Pointer to work-queue completion queue entry.
11082 *
11083 * This routine handles an ELS work-queue completion event and construct
11084 * a pseudo response ELS IODBQ from the SLI4 ELS WCQE for the common
11085 * discovery engine to handle.
11086 *
11087 * Return: Pointer to the receive IOCBQ, NULL otherwise.
11088 **/
11089 static struct lpfc_iocbq *
11090 lpfc_sli4_els_wcqe_to_rspiocbq(struct lpfc_hba *phba,
11091 struct lpfc_iocbq *irspiocbq)
11092 {
11093 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
11094 struct lpfc_iocbq *cmdiocbq;
11095 struct lpfc_wcqe_complete *wcqe;
11096 unsigned long iflags;
11097
11098 wcqe = &irspiocbq->cq_event.cqe.wcqe_cmpl;
11099 spin_lock_irqsave(&pring->ring_lock, iflags);
11100 pring->stats.iocb_event++;
11101 /* Look up the ELS command IOCB and create pseudo response IOCB */
11102 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11103 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11104 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11105
11106 if (unlikely(!cmdiocbq)) {
11107 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11108 "0386 ELS complete with no corresponding "
11109 "cmdiocb: iotag (%d)\n",
11110 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11111 lpfc_sli_release_iocbq(phba, irspiocbq);
11112 return NULL;
11113 }
11114
11115 /* Fake the irspiocbq and copy necessary response information */
11116 lpfc_sli4_iocb_param_transfer(phba, irspiocbq, cmdiocbq, wcqe);
11117
11118 return irspiocbq;
11119 }
11120
11121 /**
11122 * lpfc_sli4_sp_handle_async_event - Handle an asynchroous event
11123 * @phba: Pointer to HBA context object.
11124 * @cqe: Pointer to mailbox completion queue entry.
11125 *
11126 * This routine process a mailbox completion queue entry with asynchrous
11127 * event.
11128 *
11129 * Return: true if work posted to worker thread, otherwise false.
11130 **/
11131 static bool
11132 lpfc_sli4_sp_handle_async_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11133 {
11134 struct lpfc_cq_event *cq_event;
11135 unsigned long iflags;
11136
11137 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
11138 "0392 Async Event: word0:x%x, word1:x%x, "
11139 "word2:x%x, word3:x%x\n", mcqe->word0,
11140 mcqe->mcqe_tag0, mcqe->mcqe_tag1, mcqe->trailer);
11141
11142 /* Allocate a new internal CQ_EVENT entry */
11143 cq_event = lpfc_sli4_cq_event_alloc(phba);
11144 if (!cq_event) {
11145 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11146 "0394 Failed to allocate CQ_EVENT entry\n");
11147 return false;
11148 }
11149
11150 /* Move the CQE into an asynchronous event entry */
11151 memcpy(&cq_event->cqe, mcqe, sizeof(struct lpfc_mcqe));
11152 spin_lock_irqsave(&phba->hbalock, iflags);
11153 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_asynce_work_queue);
11154 /* Set the async event flag */
11155 phba->hba_flag |= ASYNC_EVENT;
11156 spin_unlock_irqrestore(&phba->hbalock, iflags);
11157
11158 return true;
11159 }
11160
11161 /**
11162 * lpfc_sli4_sp_handle_mbox_event - Handle a mailbox completion event
11163 * @phba: Pointer to HBA context object.
11164 * @cqe: Pointer to mailbox completion queue entry.
11165 *
11166 * This routine process a mailbox completion queue entry with mailbox
11167 * completion event.
11168 *
11169 * Return: true if work posted to worker thread, otherwise false.
11170 **/
11171 static bool
11172 lpfc_sli4_sp_handle_mbox_event(struct lpfc_hba *phba, struct lpfc_mcqe *mcqe)
11173 {
11174 uint32_t mcqe_status;
11175 MAILBOX_t *mbox, *pmbox;
11176 struct lpfc_mqe *mqe;
11177 struct lpfc_vport *vport;
11178 struct lpfc_nodelist *ndlp;
11179 struct lpfc_dmabuf *mp;
11180 unsigned long iflags;
11181 LPFC_MBOXQ_t *pmb;
11182 bool workposted = false;
11183 int rc;
11184
11185 /* If not a mailbox complete MCQE, out by checking mailbox consume */
11186 if (!bf_get(lpfc_trailer_completed, mcqe))
11187 goto out_no_mqe_complete;
11188
11189 /* Get the reference to the active mbox command */
11190 spin_lock_irqsave(&phba->hbalock, iflags);
11191 pmb = phba->sli.mbox_active;
11192 if (unlikely(!pmb)) {
11193 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
11194 "1832 No pending MBOX command to handle\n");
11195 spin_unlock_irqrestore(&phba->hbalock, iflags);
11196 goto out_no_mqe_complete;
11197 }
11198 spin_unlock_irqrestore(&phba->hbalock, iflags);
11199 mqe = &pmb->u.mqe;
11200 pmbox = (MAILBOX_t *)&pmb->u.mqe;
11201 mbox = phba->mbox;
11202 vport = pmb->vport;
11203
11204 /* Reset heartbeat timer */
11205 phba->last_completion_time = jiffies;
11206 del_timer(&phba->sli.mbox_tmo);
11207
11208 /* Move mbox data to caller's mailbox region, do endian swapping */
11209 if (pmb->mbox_cmpl && mbox)
11210 lpfc_sli_pcimem_bcopy(mbox, mqe, sizeof(struct lpfc_mqe));
11211
11212 /*
11213 * For mcqe errors, conditionally move a modified error code to
11214 * the mbox so that the error will not be missed.
11215 */
11216 mcqe_status = bf_get(lpfc_mcqe_status, mcqe);
11217 if (mcqe_status != MB_CQE_STATUS_SUCCESS) {
11218 if (bf_get(lpfc_mqe_status, mqe) == MBX_SUCCESS)
11219 bf_set(lpfc_mqe_status, mqe,
11220 (LPFC_MBX_ERROR_RANGE | mcqe_status));
11221 }
11222 if (pmb->mbox_flag & LPFC_MBX_IMED_UNREG) {
11223 pmb->mbox_flag &= ~LPFC_MBX_IMED_UNREG;
11224 lpfc_debugfs_disc_trc(vport, LPFC_DISC_TRC_MBOX_VPORT,
11225 "MBOX dflt rpi: status:x%x rpi:x%x",
11226 mcqe_status,
11227 pmbox->un.varWords[0], 0);
11228 if (mcqe_status == MB_CQE_STATUS_SUCCESS) {
11229 mp = (struct lpfc_dmabuf *)(pmb->context1);
11230 ndlp = (struct lpfc_nodelist *)pmb->context2;
11231 /* Reg_LOGIN of dflt RPI was successful. Now lets get
11232 * RID of the PPI using the same mbox buffer.
11233 */
11234 lpfc_unreg_login(phba, vport->vpi,
11235 pmbox->un.varWords[0], pmb);
11236 pmb->mbox_cmpl = lpfc_mbx_cmpl_dflt_rpi;
11237 pmb->context1 = mp;
11238 pmb->context2 = ndlp;
11239 pmb->vport = vport;
11240 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
11241 if (rc != MBX_BUSY)
11242 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX |
11243 LOG_SLI, "0385 rc should "
11244 "have been MBX_BUSY\n");
11245 if (rc != MBX_NOT_FINISHED)
11246 goto send_current_mbox;
11247 }
11248 }
11249 spin_lock_irqsave(&phba->pport->work_port_lock, iflags);
11250 phba->pport->work_port_events &= ~WORKER_MBOX_TMO;
11251 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflags);
11252
11253 /* There is mailbox completion work to do */
11254 spin_lock_irqsave(&phba->hbalock, iflags);
11255 __lpfc_mbox_cmpl_put(phba, pmb);
11256 phba->work_ha |= HA_MBATT;
11257 spin_unlock_irqrestore(&phba->hbalock, iflags);
11258 workposted = true;
11259
11260 send_current_mbox:
11261 spin_lock_irqsave(&phba->hbalock, iflags);
11262 /* Release the mailbox command posting token */
11263 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
11264 /* Setting active mailbox pointer need to be in sync to flag clear */
11265 phba->sli.mbox_active = NULL;
11266 spin_unlock_irqrestore(&phba->hbalock, iflags);
11267 /* Wake up worker thread to post the next pending mailbox command */
11268 lpfc_worker_wake_up(phba);
11269 out_no_mqe_complete:
11270 if (bf_get(lpfc_trailer_consumed, mcqe))
11271 lpfc_sli4_mq_release(phba->sli4_hba.mbx_wq);
11272 return workposted;
11273 }
11274
11275 /**
11276 * lpfc_sli4_sp_handle_mcqe - Process a mailbox completion queue entry
11277 * @phba: Pointer to HBA context object.
11278 * @cqe: Pointer to mailbox completion queue entry.
11279 *
11280 * This routine process a mailbox completion queue entry, it invokes the
11281 * proper mailbox complete handling or asynchrous event handling routine
11282 * according to the MCQE's async bit.
11283 *
11284 * Return: true if work posted to worker thread, otherwise false.
11285 **/
11286 static bool
11287 lpfc_sli4_sp_handle_mcqe(struct lpfc_hba *phba, struct lpfc_cqe *cqe)
11288 {
11289 struct lpfc_mcqe mcqe;
11290 bool workposted;
11291
11292 /* Copy the mailbox MCQE and convert endian order as needed */
11293 lpfc_sli_pcimem_bcopy(cqe, &mcqe, sizeof(struct lpfc_mcqe));
11294
11295 /* Invoke the proper event handling routine */
11296 if (!bf_get(lpfc_trailer_async, &mcqe))
11297 workposted = lpfc_sli4_sp_handle_mbox_event(phba, &mcqe);
11298 else
11299 workposted = lpfc_sli4_sp_handle_async_event(phba, &mcqe);
11300 return workposted;
11301 }
11302
11303 /**
11304 * lpfc_sli4_sp_handle_els_wcqe - Handle els work-queue completion event
11305 * @phba: Pointer to HBA context object.
11306 * @cq: Pointer to associated CQ
11307 * @wcqe: Pointer to work-queue completion queue entry.
11308 *
11309 * This routine handles an ELS work-queue completion event.
11310 *
11311 * Return: true if work posted to worker thread, otherwise false.
11312 **/
11313 static bool
11314 lpfc_sli4_sp_handle_els_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11315 struct lpfc_wcqe_complete *wcqe)
11316 {
11317 struct lpfc_iocbq *irspiocbq;
11318 unsigned long iflags;
11319 struct lpfc_sli_ring *pring = cq->pring;
11320 int txq_cnt = 0;
11321 int txcmplq_cnt = 0;
11322 int fcp_txcmplq_cnt = 0;
11323
11324 /* Get an irspiocbq for later ELS response processing use */
11325 irspiocbq = lpfc_sli_get_iocbq(phba);
11326 if (!irspiocbq) {
11327 if (!list_empty(&pring->txq))
11328 txq_cnt++;
11329 if (!list_empty(&pring->txcmplq))
11330 txcmplq_cnt++;
11331 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
11332 fcp_txcmplq_cnt++;
11333 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11334 "0387 NO IOCBQ data: txq_cnt=%d iocb_cnt=%d "
11335 "fcp_txcmplq_cnt=%d, els_txcmplq_cnt=%d\n",
11336 txq_cnt, phba->iocb_cnt,
11337 fcp_txcmplq_cnt,
11338 txcmplq_cnt);
11339 return false;
11340 }
11341
11342 /* Save off the slow-path queue event for work thread to process */
11343 memcpy(&irspiocbq->cq_event.cqe.wcqe_cmpl, wcqe, sizeof(*wcqe));
11344 spin_lock_irqsave(&phba->hbalock, iflags);
11345 list_add_tail(&irspiocbq->cq_event.list,
11346 &phba->sli4_hba.sp_queue_event);
11347 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11348 spin_unlock_irqrestore(&phba->hbalock, iflags);
11349
11350 return true;
11351 }
11352
11353 /**
11354 * lpfc_sli4_sp_handle_rel_wcqe - Handle slow-path WQ entry consumed event
11355 * @phba: Pointer to HBA context object.
11356 * @wcqe: Pointer to work-queue completion queue entry.
11357 *
11358 * This routine handles slow-path WQ entry comsumed event by invoking the
11359 * proper WQ release routine to the slow-path WQ.
11360 **/
11361 static void
11362 lpfc_sli4_sp_handle_rel_wcqe(struct lpfc_hba *phba,
11363 struct lpfc_wcqe_release *wcqe)
11364 {
11365 /* sanity check on queue memory */
11366 if (unlikely(!phba->sli4_hba.els_wq))
11367 return;
11368 /* Check for the slow-path ELS work queue */
11369 if (bf_get(lpfc_wcqe_r_wq_id, wcqe) == phba->sli4_hba.els_wq->queue_id)
11370 lpfc_sli4_wq_release(phba->sli4_hba.els_wq,
11371 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11372 else
11373 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11374 "2579 Slow-path wqe consume event carries "
11375 "miss-matched qid: wcqe-qid=x%x, sp-qid=x%x\n",
11376 bf_get(lpfc_wcqe_r_wqe_index, wcqe),
11377 phba->sli4_hba.els_wq->queue_id);
11378 }
11379
11380 /**
11381 * lpfc_sli4_sp_handle_abort_xri_wcqe - Handle a xri abort event
11382 * @phba: Pointer to HBA context object.
11383 * @cq: Pointer to a WQ completion queue.
11384 * @wcqe: Pointer to work-queue completion queue entry.
11385 *
11386 * This routine handles an XRI abort event.
11387 *
11388 * Return: true if work posted to worker thread, otherwise false.
11389 **/
11390 static bool
11391 lpfc_sli4_sp_handle_abort_xri_wcqe(struct lpfc_hba *phba,
11392 struct lpfc_queue *cq,
11393 struct sli4_wcqe_xri_aborted *wcqe)
11394 {
11395 bool workposted = false;
11396 struct lpfc_cq_event *cq_event;
11397 unsigned long iflags;
11398
11399 /* Allocate a new internal CQ_EVENT entry */
11400 cq_event = lpfc_sli4_cq_event_alloc(phba);
11401 if (!cq_event) {
11402 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11403 "0602 Failed to allocate CQ_EVENT entry\n");
11404 return false;
11405 }
11406
11407 /* Move the CQE into the proper xri abort event list */
11408 memcpy(&cq_event->cqe, wcqe, sizeof(struct sli4_wcqe_xri_aborted));
11409 switch (cq->subtype) {
11410 case LPFC_FCP:
11411 spin_lock_irqsave(&phba->hbalock, iflags);
11412 list_add_tail(&cq_event->list,
11413 &phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
11414 /* Set the fcp xri abort event flag */
11415 phba->hba_flag |= FCP_XRI_ABORT_EVENT;
11416 spin_unlock_irqrestore(&phba->hbalock, iflags);
11417 workposted = true;
11418 break;
11419 case LPFC_ELS:
11420 spin_lock_irqsave(&phba->hbalock, iflags);
11421 list_add_tail(&cq_event->list,
11422 &phba->sli4_hba.sp_els_xri_aborted_work_queue);
11423 /* Set the els xri abort event flag */
11424 phba->hba_flag |= ELS_XRI_ABORT_EVENT;
11425 spin_unlock_irqrestore(&phba->hbalock, iflags);
11426 workposted = true;
11427 break;
11428 default:
11429 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11430 "0603 Invalid work queue CQE subtype (x%x)\n",
11431 cq->subtype);
11432 workposted = false;
11433 break;
11434 }
11435 return workposted;
11436 }
11437
11438 /**
11439 * lpfc_sli4_sp_handle_rcqe - Process a receive-queue completion queue entry
11440 * @phba: Pointer to HBA context object.
11441 * @rcqe: Pointer to receive-queue completion queue entry.
11442 *
11443 * This routine process a receive-queue completion queue entry.
11444 *
11445 * Return: true if work posted to worker thread, otherwise false.
11446 **/
11447 static bool
11448 lpfc_sli4_sp_handle_rcqe(struct lpfc_hba *phba, struct lpfc_rcqe *rcqe)
11449 {
11450 bool workposted = false;
11451 struct lpfc_queue *hrq = phba->sli4_hba.hdr_rq;
11452 struct lpfc_queue *drq = phba->sli4_hba.dat_rq;
11453 struct hbq_dmabuf *dma_buf;
11454 uint32_t status, rq_id;
11455 unsigned long iflags;
11456
11457 /* sanity check on queue memory */
11458 if (unlikely(!hrq) || unlikely(!drq))
11459 return workposted;
11460
11461 if (bf_get(lpfc_cqe_code, rcqe) == CQE_CODE_RECEIVE_V1)
11462 rq_id = bf_get(lpfc_rcqe_rq_id_v1, rcqe);
11463 else
11464 rq_id = bf_get(lpfc_rcqe_rq_id, rcqe);
11465 if (rq_id != hrq->queue_id)
11466 goto out;
11467
11468 status = bf_get(lpfc_rcqe_status, rcqe);
11469 switch (status) {
11470 case FC_STATUS_RQ_BUF_LEN_EXCEEDED:
11471 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11472 "2537 Receive Frame Truncated!!\n");
11473 hrq->RQ_buf_trunc++;
11474 case FC_STATUS_RQ_SUCCESS:
11475 lpfc_sli4_rq_release(hrq, drq);
11476 spin_lock_irqsave(&phba->hbalock, iflags);
11477 dma_buf = lpfc_sli_hbqbuf_get(&phba->hbqs[0].hbq_buffer_list);
11478 if (!dma_buf) {
11479 hrq->RQ_no_buf_found++;
11480 spin_unlock_irqrestore(&phba->hbalock, iflags);
11481 goto out;
11482 }
11483 hrq->RQ_rcv_buf++;
11484 memcpy(&dma_buf->cq_event.cqe.rcqe_cmpl, rcqe, sizeof(*rcqe));
11485 /* save off the frame for the word thread to process */
11486 list_add_tail(&dma_buf->cq_event.list,
11487 &phba->sli4_hba.sp_queue_event);
11488 /* Frame received */
11489 phba->hba_flag |= HBA_SP_QUEUE_EVT;
11490 spin_unlock_irqrestore(&phba->hbalock, iflags);
11491 workposted = true;
11492 break;
11493 case FC_STATUS_INSUFF_BUF_NEED_BUF:
11494 case FC_STATUS_INSUFF_BUF_FRM_DISC:
11495 hrq->RQ_no_posted_buf++;
11496 /* Post more buffers if possible */
11497 spin_lock_irqsave(&phba->hbalock, iflags);
11498 phba->hba_flag |= HBA_POST_RECEIVE_BUFFER;
11499 spin_unlock_irqrestore(&phba->hbalock, iflags);
11500 workposted = true;
11501 break;
11502 }
11503 out:
11504 return workposted;
11505 }
11506
11507 /**
11508 * lpfc_sli4_sp_handle_cqe - Process a slow path completion queue entry
11509 * @phba: Pointer to HBA context object.
11510 * @cq: Pointer to the completion queue.
11511 * @wcqe: Pointer to a completion queue entry.
11512 *
11513 * This routine process a slow-path work-queue or receive queue completion queue
11514 * entry.
11515 *
11516 * Return: true if work posted to worker thread, otherwise false.
11517 **/
11518 static bool
11519 lpfc_sli4_sp_handle_cqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11520 struct lpfc_cqe *cqe)
11521 {
11522 struct lpfc_cqe cqevt;
11523 bool workposted = false;
11524
11525 /* Copy the work queue CQE and convert endian order if needed */
11526 lpfc_sli_pcimem_bcopy(cqe, &cqevt, sizeof(struct lpfc_cqe));
11527
11528 /* Check and process for different type of WCQE and dispatch */
11529 switch (bf_get(lpfc_cqe_code, &cqevt)) {
11530 case CQE_CODE_COMPL_WQE:
11531 /* Process the WQ/RQ complete event */
11532 phba->last_completion_time = jiffies;
11533 workposted = lpfc_sli4_sp_handle_els_wcqe(phba, cq,
11534 (struct lpfc_wcqe_complete *)&cqevt);
11535 break;
11536 case CQE_CODE_RELEASE_WQE:
11537 /* Process the WQ release event */
11538 lpfc_sli4_sp_handle_rel_wcqe(phba,
11539 (struct lpfc_wcqe_release *)&cqevt);
11540 break;
11541 case CQE_CODE_XRI_ABORTED:
11542 /* Process the WQ XRI abort event */
11543 phba->last_completion_time = jiffies;
11544 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11545 (struct sli4_wcqe_xri_aborted *)&cqevt);
11546 break;
11547 case CQE_CODE_RECEIVE:
11548 case CQE_CODE_RECEIVE_V1:
11549 /* Process the RQ event */
11550 phba->last_completion_time = jiffies;
11551 workposted = lpfc_sli4_sp_handle_rcqe(phba,
11552 (struct lpfc_rcqe *)&cqevt);
11553 break;
11554 default:
11555 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11556 "0388 Not a valid WCQE code: x%x\n",
11557 bf_get(lpfc_cqe_code, &cqevt));
11558 break;
11559 }
11560 return workposted;
11561 }
11562
11563 /**
11564 * lpfc_sli4_sp_handle_eqe - Process a slow-path event queue entry
11565 * @phba: Pointer to HBA context object.
11566 * @eqe: Pointer to fast-path event queue entry.
11567 *
11568 * This routine process a event queue entry from the slow-path event queue.
11569 * It will check the MajorCode and MinorCode to determine this is for a
11570 * completion event on a completion queue, if not, an error shall be logged
11571 * and just return. Otherwise, it will get to the corresponding completion
11572 * queue and process all the entries on that completion queue, rearm the
11573 * completion queue, and then return.
11574 *
11575 **/
11576 static void
11577 lpfc_sli4_sp_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11578 struct lpfc_queue *speq)
11579 {
11580 struct lpfc_queue *cq = NULL, *childq;
11581 struct lpfc_cqe *cqe;
11582 bool workposted = false;
11583 int ecount = 0;
11584 uint16_t cqid;
11585
11586 /* Get the reference to the corresponding CQ */
11587 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11588
11589 list_for_each_entry(childq, &speq->child_list, list) {
11590 if (childq->queue_id == cqid) {
11591 cq = childq;
11592 break;
11593 }
11594 }
11595 if (unlikely(!cq)) {
11596 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11597 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11598 "0365 Slow-path CQ identifier "
11599 "(%d) does not exist\n", cqid);
11600 return;
11601 }
11602
11603 /* Process all the entries to the CQ */
11604 switch (cq->type) {
11605 case LPFC_MCQ:
11606 while ((cqe = lpfc_sli4_cq_get(cq))) {
11607 workposted |= lpfc_sli4_sp_handle_mcqe(phba, cqe);
11608 if (!(++ecount % cq->entry_repost))
11609 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11610 cq->CQ_mbox++;
11611 }
11612 break;
11613 case LPFC_WCQ:
11614 while ((cqe = lpfc_sli4_cq_get(cq))) {
11615 if (cq->subtype == LPFC_FCP)
11616 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq,
11617 cqe);
11618 else
11619 workposted |= lpfc_sli4_sp_handle_cqe(phba, cq,
11620 cqe);
11621 if (!(++ecount % cq->entry_repost))
11622 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11623 }
11624
11625 /* Track the max number of CQEs processed in 1 EQ */
11626 if (ecount > cq->CQ_max_cqe)
11627 cq->CQ_max_cqe = ecount;
11628 break;
11629 default:
11630 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11631 "0370 Invalid completion queue type (%d)\n",
11632 cq->type);
11633 return;
11634 }
11635
11636 /* Catch the no cq entry condition, log an error */
11637 if (unlikely(ecount == 0))
11638 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11639 "0371 No entry from the CQ: identifier "
11640 "(x%x), type (%d)\n", cq->queue_id, cq->type);
11641
11642 /* In any case, flash and re-arm the RCQ */
11643 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11644
11645 /* wake up worker thread if there are works to be done */
11646 if (workposted)
11647 lpfc_worker_wake_up(phba);
11648 }
11649
11650 /**
11651 * lpfc_sli4_fp_handle_fcp_wcqe - Process fast-path work queue completion entry
11652 * @phba: Pointer to HBA context object.
11653 * @cq: Pointer to associated CQ
11654 * @wcqe: Pointer to work-queue completion queue entry.
11655 *
11656 * This routine process a fast-path work queue completion entry from fast-path
11657 * event queue for FCP command response completion.
11658 **/
11659 static void
11660 lpfc_sli4_fp_handle_fcp_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11661 struct lpfc_wcqe_complete *wcqe)
11662 {
11663 struct lpfc_sli_ring *pring = cq->pring;
11664 struct lpfc_iocbq *cmdiocbq;
11665 struct lpfc_iocbq irspiocbq;
11666 unsigned long iflags;
11667
11668 /* Check for response status */
11669 if (unlikely(bf_get(lpfc_wcqe_c_status, wcqe))) {
11670 /* If resource errors reported from HBA, reduce queue
11671 * depth of the SCSI device.
11672 */
11673 if (((bf_get(lpfc_wcqe_c_status, wcqe) ==
11674 IOSTAT_LOCAL_REJECT)) &&
11675 ((wcqe->parameter & IOERR_PARAM_MASK) ==
11676 IOERR_NO_RESOURCES))
11677 phba->lpfc_rampdown_queue_depth(phba);
11678
11679 /* Log the error status */
11680 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11681 "0373 FCP complete error: status=x%x, "
11682 "hw_status=x%x, total_data_specified=%d, "
11683 "parameter=x%x, word3=x%x\n",
11684 bf_get(lpfc_wcqe_c_status, wcqe),
11685 bf_get(lpfc_wcqe_c_hw_status, wcqe),
11686 wcqe->total_data_placed, wcqe->parameter,
11687 wcqe->word3);
11688 }
11689
11690 /* Look up the FCP command IOCB and create pseudo response IOCB */
11691 spin_lock_irqsave(&pring->ring_lock, iflags);
11692 pring->stats.iocb_event++;
11693 cmdiocbq = lpfc_sli_iocbq_lookup_by_tag(phba, pring,
11694 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11695 spin_unlock_irqrestore(&pring->ring_lock, iflags);
11696 if (unlikely(!cmdiocbq)) {
11697 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11698 "0374 FCP complete with no corresponding "
11699 "cmdiocb: iotag (%d)\n",
11700 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11701 return;
11702 }
11703 if (unlikely(!cmdiocbq->iocb_cmpl)) {
11704 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11705 "0375 FCP cmdiocb not callback function "
11706 "iotag: (%d)\n",
11707 bf_get(lpfc_wcqe_c_request_tag, wcqe));
11708 return;
11709 }
11710
11711 /* Fake the irspiocb and copy necessary response information */
11712 lpfc_sli4_iocb_param_transfer(phba, &irspiocbq, cmdiocbq, wcqe);
11713
11714 if (cmdiocbq->iocb_flag & LPFC_DRIVER_ABORTED) {
11715 spin_lock_irqsave(&phba->hbalock, iflags);
11716 cmdiocbq->iocb_flag &= ~LPFC_DRIVER_ABORTED;
11717 spin_unlock_irqrestore(&phba->hbalock, iflags);
11718 }
11719
11720 /* Pass the cmd_iocb and the rsp state to the upper layer */
11721 (cmdiocbq->iocb_cmpl)(phba, cmdiocbq, &irspiocbq);
11722 }
11723
11724 /**
11725 * lpfc_sli4_fp_handle_rel_wcqe - Handle fast-path WQ entry consumed event
11726 * @phba: Pointer to HBA context object.
11727 * @cq: Pointer to completion queue.
11728 * @wcqe: Pointer to work-queue completion queue entry.
11729 *
11730 * This routine handles an fast-path WQ entry comsumed event by invoking the
11731 * proper WQ release routine to the slow-path WQ.
11732 **/
11733 static void
11734 lpfc_sli4_fp_handle_rel_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11735 struct lpfc_wcqe_release *wcqe)
11736 {
11737 struct lpfc_queue *childwq;
11738 bool wqid_matched = false;
11739 uint16_t fcp_wqid;
11740
11741 /* Check for fast-path FCP work queue release */
11742 fcp_wqid = bf_get(lpfc_wcqe_r_wq_id, wcqe);
11743 list_for_each_entry(childwq, &cq->child_list, list) {
11744 if (childwq->queue_id == fcp_wqid) {
11745 lpfc_sli4_wq_release(childwq,
11746 bf_get(lpfc_wcqe_r_wqe_index, wcqe));
11747 wqid_matched = true;
11748 break;
11749 }
11750 }
11751 /* Report warning log message if no match found */
11752 if (wqid_matched != true)
11753 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11754 "2580 Fast-path wqe consume event carries "
11755 "miss-matched qid: wcqe-qid=x%x\n", fcp_wqid);
11756 }
11757
11758 /**
11759 * lpfc_sli4_fp_handle_wcqe - Process fast-path work queue completion entry
11760 * @cq: Pointer to the completion queue.
11761 * @eqe: Pointer to fast-path completion queue entry.
11762 *
11763 * This routine process a fast-path work queue completion entry from fast-path
11764 * event queue for FCP command response completion.
11765 **/
11766 static int
11767 lpfc_sli4_fp_handle_wcqe(struct lpfc_hba *phba, struct lpfc_queue *cq,
11768 struct lpfc_cqe *cqe)
11769 {
11770 struct lpfc_wcqe_release wcqe;
11771 bool workposted = false;
11772
11773 /* Copy the work queue CQE and convert endian order if needed */
11774 lpfc_sli_pcimem_bcopy(cqe, &wcqe, sizeof(struct lpfc_cqe));
11775
11776 /* Check and process for different type of WCQE and dispatch */
11777 switch (bf_get(lpfc_wcqe_c_code, &wcqe)) {
11778 case CQE_CODE_COMPL_WQE:
11779 cq->CQ_wq++;
11780 /* Process the WQ complete event */
11781 phba->last_completion_time = jiffies;
11782 lpfc_sli4_fp_handle_fcp_wcqe(phba, cq,
11783 (struct lpfc_wcqe_complete *)&wcqe);
11784 break;
11785 case CQE_CODE_RELEASE_WQE:
11786 cq->CQ_release_wqe++;
11787 /* Process the WQ release event */
11788 lpfc_sli4_fp_handle_rel_wcqe(phba, cq,
11789 (struct lpfc_wcqe_release *)&wcqe);
11790 break;
11791 case CQE_CODE_XRI_ABORTED:
11792 cq->CQ_xri_aborted++;
11793 /* Process the WQ XRI abort event */
11794 phba->last_completion_time = jiffies;
11795 workposted = lpfc_sli4_sp_handle_abort_xri_wcqe(phba, cq,
11796 (struct sli4_wcqe_xri_aborted *)&wcqe);
11797 break;
11798 default:
11799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11800 "0144 Not a valid WCQE code: x%x\n",
11801 bf_get(lpfc_wcqe_c_code, &wcqe));
11802 break;
11803 }
11804 return workposted;
11805 }
11806
11807 /**
11808 * lpfc_sli4_hba_handle_eqe - Process a fast-path event queue entry
11809 * @phba: Pointer to HBA context object.
11810 * @eqe: Pointer to fast-path event queue entry.
11811 *
11812 * This routine process a event queue entry from the fast-path event queue.
11813 * It will check the MajorCode and MinorCode to determine this is for a
11814 * completion event on a completion queue, if not, an error shall be logged
11815 * and just return. Otherwise, it will get to the corresponding completion
11816 * queue and process all the entries on the completion queue, rearm the
11817 * completion queue, and then return.
11818 **/
11819 static void
11820 lpfc_sli4_hba_handle_eqe(struct lpfc_hba *phba, struct lpfc_eqe *eqe,
11821 uint32_t qidx)
11822 {
11823 struct lpfc_queue *cq;
11824 struct lpfc_cqe *cqe;
11825 bool workposted = false;
11826 uint16_t cqid;
11827 int ecount = 0;
11828
11829 if (unlikely(bf_get_le32(lpfc_eqe_major_code, eqe) != 0)) {
11830 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11831 "0366 Not a valid completion "
11832 "event: majorcode=x%x, minorcode=x%x\n",
11833 bf_get_le32(lpfc_eqe_major_code, eqe),
11834 bf_get_le32(lpfc_eqe_minor_code, eqe));
11835 return;
11836 }
11837
11838 /* Get the reference to the corresponding CQ */
11839 cqid = bf_get_le32(lpfc_eqe_resource_id, eqe);
11840
11841 /* Check if this is a Slow path event */
11842 if (unlikely(cqid != phba->sli4_hba.fcp_cq_map[qidx])) {
11843 lpfc_sli4_sp_handle_eqe(phba, eqe,
11844 phba->sli4_hba.hba_eq[qidx]);
11845 return;
11846 }
11847
11848 if (unlikely(!phba->sli4_hba.fcp_cq)) {
11849 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
11850 "3146 Fast-path completion queues "
11851 "does not exist\n");
11852 return;
11853 }
11854 cq = phba->sli4_hba.fcp_cq[qidx];
11855 if (unlikely(!cq)) {
11856 if (phba->sli.sli_flag & LPFC_SLI_ACTIVE)
11857 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11858 "0367 Fast-path completion queue "
11859 "(%d) does not exist\n", qidx);
11860 return;
11861 }
11862
11863 if (unlikely(cqid != cq->queue_id)) {
11864 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11865 "0368 Miss-matched fast-path completion "
11866 "queue identifier: eqcqid=%d, fcpcqid=%d\n",
11867 cqid, cq->queue_id);
11868 return;
11869 }
11870
11871 /* Process all the entries to the CQ */
11872 while ((cqe = lpfc_sli4_cq_get(cq))) {
11873 workposted |= lpfc_sli4_fp_handle_wcqe(phba, cq, cqe);
11874 if (!(++ecount % cq->entry_repost))
11875 lpfc_sli4_cq_release(cq, LPFC_QUEUE_NOARM);
11876 }
11877
11878 /* Track the max number of CQEs processed in 1 EQ */
11879 if (ecount > cq->CQ_max_cqe)
11880 cq->CQ_max_cqe = ecount;
11881
11882 /* Catch the no cq entry condition */
11883 if (unlikely(ecount == 0))
11884 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
11885 "0369 No entry from fast-path completion "
11886 "queue fcpcqid=%d\n", cq->queue_id);
11887
11888 /* In any case, flash and re-arm the CQ */
11889 lpfc_sli4_cq_release(cq, LPFC_QUEUE_REARM);
11890
11891 /* wake up worker thread if there are works to be done */
11892 if (workposted)
11893 lpfc_worker_wake_up(phba);
11894 }
11895
11896 static void
11897 lpfc_sli4_eq_flush(struct lpfc_hba *phba, struct lpfc_queue *eq)
11898 {
11899 struct lpfc_eqe *eqe;
11900
11901 /* walk all the EQ entries and drop on the floor */
11902 while ((eqe = lpfc_sli4_eq_get(eq)))
11903 ;
11904
11905 /* Clear and re-arm the EQ */
11906 lpfc_sli4_eq_release(eq, LPFC_QUEUE_REARM);
11907 }
11908
11909 /**
11910 * lpfc_sli4_hba_intr_handler - HBA interrupt handler to SLI-4 device
11911 * @irq: Interrupt number.
11912 * @dev_id: The device context pointer.
11913 *
11914 * This function is directly called from the PCI layer as an interrupt
11915 * service routine when device with SLI-4 interface spec is enabled with
11916 * MSI-X multi-message interrupt mode and there is a fast-path FCP IOCB
11917 * ring event in the HBA. However, when the device is enabled with either
11918 * MSI or Pin-IRQ interrupt mode, this function is called as part of the
11919 * device-level interrupt handler. When the PCI slot is in error recovery
11920 * or the HBA is undergoing initialization, the interrupt handler will not
11921 * process the interrupt. The SCSI FCP fast-path ring event are handled in
11922 * the intrrupt context. This function is called without any lock held.
11923 * It gets the hbalock to access and update SLI data structures. Note that,
11924 * the FCP EQ to FCP CQ are one-to-one map such that the FCP EQ index is
11925 * equal to that of FCP CQ index.
11926 *
11927 * The link attention and ELS ring attention events are handled
11928 * by the worker thread. The interrupt handler signals the worker thread
11929 * and returns for these events. This function is called without any lock
11930 * held. It gets the hbalock to access and update SLI data structures.
11931 *
11932 * This function returns IRQ_HANDLED when interrupt is handled else it
11933 * returns IRQ_NONE.
11934 **/
11935 irqreturn_t
11936 lpfc_sli4_hba_intr_handler(int irq, void *dev_id)
11937 {
11938 struct lpfc_hba *phba;
11939 struct lpfc_fcp_eq_hdl *fcp_eq_hdl;
11940 struct lpfc_queue *fpeq;
11941 struct lpfc_eqe *eqe;
11942 unsigned long iflag;
11943 int ecount = 0;
11944 int fcp_eqidx;
11945
11946 /* Get the driver's phba structure from the dev_id */
11947 fcp_eq_hdl = (struct lpfc_fcp_eq_hdl *)dev_id;
11948 phba = fcp_eq_hdl->phba;
11949 fcp_eqidx = fcp_eq_hdl->idx;
11950
11951 if (unlikely(!phba))
11952 return IRQ_NONE;
11953 if (unlikely(!phba->sli4_hba.hba_eq))
11954 return IRQ_NONE;
11955
11956 /* Get to the EQ struct associated with this vector */
11957 fpeq = phba->sli4_hba.hba_eq[fcp_eqidx];
11958 if (unlikely(!fpeq))
11959 return IRQ_NONE;
11960
11961 if (lpfc_fcp_look_ahead) {
11962 if (atomic_dec_and_test(&fcp_eq_hdl->fcp_eq_in_use))
11963 lpfc_sli4_eq_clr_intr(fpeq);
11964 else {
11965 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11966 return IRQ_NONE;
11967 }
11968 }
11969
11970 /* Check device state for handling interrupt */
11971 if (unlikely(lpfc_intr_state_check(phba))) {
11972 fpeq->EQ_badstate++;
11973 /* Check again for link_state with lock held */
11974 spin_lock_irqsave(&phba->hbalock, iflag);
11975 if (phba->link_state < LPFC_LINK_DOWN)
11976 /* Flush, clear interrupt, and rearm the EQ */
11977 lpfc_sli4_eq_flush(phba, fpeq);
11978 spin_unlock_irqrestore(&phba->hbalock, iflag);
11979 if (lpfc_fcp_look_ahead)
11980 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
11981 return IRQ_NONE;
11982 }
11983
11984 /*
11985 * Process all the event on FCP fast-path EQ
11986 */
11987 while ((eqe = lpfc_sli4_eq_get(fpeq))) {
11988 lpfc_sli4_hba_handle_eqe(phba, eqe, fcp_eqidx);
11989 if (!(++ecount % fpeq->entry_repost))
11990 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_NOARM);
11991 fpeq->EQ_processed++;
11992 }
11993
11994 /* Track the max number of EQEs processed in 1 intr */
11995 if (ecount > fpeq->EQ_max_eqe)
11996 fpeq->EQ_max_eqe = ecount;
11997
11998 /* Always clear and re-arm the fast-path EQ */
11999 lpfc_sli4_eq_release(fpeq, LPFC_QUEUE_REARM);
12000
12001 if (unlikely(ecount == 0)) {
12002 fpeq->EQ_no_entry++;
12003
12004 if (lpfc_fcp_look_ahead) {
12005 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12006 return IRQ_NONE;
12007 }
12008
12009 if (phba->intr_type == MSIX)
12010 /* MSI-X treated interrupt served as no EQ share INT */
12011 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
12012 "0358 MSI-X interrupt with no EQE\n");
12013 else
12014 /* Non MSI-X treated on interrupt as EQ share INT */
12015 return IRQ_NONE;
12016 }
12017
12018 if (lpfc_fcp_look_ahead)
12019 atomic_inc(&fcp_eq_hdl->fcp_eq_in_use);
12020 return IRQ_HANDLED;
12021 } /* lpfc_sli4_fp_intr_handler */
12022
12023 /**
12024 * lpfc_sli4_intr_handler - Device-level interrupt handler for SLI-4 device
12025 * @irq: Interrupt number.
12026 * @dev_id: The device context pointer.
12027 *
12028 * This function is the device-level interrupt handler to device with SLI-4
12029 * interface spec, called from the PCI layer when either MSI or Pin-IRQ
12030 * interrupt mode is enabled and there is an event in the HBA which requires
12031 * driver attention. This function invokes the slow-path interrupt attention
12032 * handling function and fast-path interrupt attention handling function in
12033 * turn to process the relevant HBA attention events. This function is called
12034 * without any lock held. It gets the hbalock to access and update SLI data
12035 * structures.
12036 *
12037 * This function returns IRQ_HANDLED when interrupt is handled, else it
12038 * returns IRQ_NONE.
12039 **/
12040 irqreturn_t
12041 lpfc_sli4_intr_handler(int irq, void *dev_id)
12042 {
12043 struct lpfc_hba *phba;
12044 irqreturn_t hba_irq_rc;
12045 bool hba_handled = false;
12046 int fcp_eqidx;
12047
12048 /* Get the driver's phba structure from the dev_id */
12049 phba = (struct lpfc_hba *)dev_id;
12050
12051 if (unlikely(!phba))
12052 return IRQ_NONE;
12053
12054 /*
12055 * Invoke fast-path host attention interrupt handling as appropriate.
12056 */
12057 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_io_channel; fcp_eqidx++) {
12058 hba_irq_rc = lpfc_sli4_hba_intr_handler(irq,
12059 &phba->sli4_hba.fcp_eq_hdl[fcp_eqidx]);
12060 if (hba_irq_rc == IRQ_HANDLED)
12061 hba_handled |= true;
12062 }
12063
12064 return (hba_handled == true) ? IRQ_HANDLED : IRQ_NONE;
12065 } /* lpfc_sli4_intr_handler */
12066
12067 /**
12068 * lpfc_sli4_queue_free - free a queue structure and associated memory
12069 * @queue: The queue structure to free.
12070 *
12071 * This function frees a queue structure and the DMAable memory used for
12072 * the host resident queue. This function must be called after destroying the
12073 * queue on the HBA.
12074 **/
12075 void
12076 lpfc_sli4_queue_free(struct lpfc_queue *queue)
12077 {
12078 struct lpfc_dmabuf *dmabuf;
12079
12080 if (!queue)
12081 return;
12082
12083 while (!list_empty(&queue->page_list)) {
12084 list_remove_head(&queue->page_list, dmabuf, struct lpfc_dmabuf,
12085 list);
12086 dma_free_coherent(&queue->phba->pcidev->dev, SLI4_PAGE_SIZE,
12087 dmabuf->virt, dmabuf->phys);
12088 kfree(dmabuf);
12089 }
12090 kfree(queue);
12091 return;
12092 }
12093
12094 /**
12095 * lpfc_sli4_queue_alloc - Allocate and initialize a queue structure
12096 * @phba: The HBA that this queue is being created on.
12097 * @entry_size: The size of each queue entry for this queue.
12098 * @entry count: The number of entries that this queue will handle.
12099 *
12100 * This function allocates a queue structure and the DMAable memory used for
12101 * the host resident queue. This function must be called before creating the
12102 * queue on the HBA.
12103 **/
12104 struct lpfc_queue *
12105 lpfc_sli4_queue_alloc(struct lpfc_hba *phba, uint32_t entry_size,
12106 uint32_t entry_count)
12107 {
12108 struct lpfc_queue *queue;
12109 struct lpfc_dmabuf *dmabuf;
12110 int x, total_qe_count;
12111 void *dma_pointer;
12112 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12113
12114 if (!phba->sli4_hba.pc_sli4_params.supported)
12115 hw_page_size = SLI4_PAGE_SIZE;
12116
12117 queue = kzalloc(sizeof(struct lpfc_queue) +
12118 (sizeof(union sli4_qe) * entry_count), GFP_KERNEL);
12119 if (!queue)
12120 return NULL;
12121 queue->page_count = (ALIGN(entry_size * entry_count,
12122 hw_page_size))/hw_page_size;
12123 INIT_LIST_HEAD(&queue->list);
12124 INIT_LIST_HEAD(&queue->page_list);
12125 INIT_LIST_HEAD(&queue->child_list);
12126 for (x = 0, total_qe_count = 0; x < queue->page_count; x++) {
12127 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
12128 if (!dmabuf)
12129 goto out_fail;
12130 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
12131 hw_page_size, &dmabuf->phys,
12132 GFP_KERNEL);
12133 if (!dmabuf->virt) {
12134 kfree(dmabuf);
12135 goto out_fail;
12136 }
12137 memset(dmabuf->virt, 0, hw_page_size);
12138 dmabuf->buffer_tag = x;
12139 list_add_tail(&dmabuf->list, &queue->page_list);
12140 /* initialize queue's entry array */
12141 dma_pointer = dmabuf->virt;
12142 for (; total_qe_count < entry_count &&
12143 dma_pointer < (hw_page_size + dmabuf->virt);
12144 total_qe_count++, dma_pointer += entry_size) {
12145 queue->qe[total_qe_count].address = dma_pointer;
12146 }
12147 }
12148 queue->entry_size = entry_size;
12149 queue->entry_count = entry_count;
12150
12151 /*
12152 * entry_repost is calculated based on the number of entries in the
12153 * queue. This works out except for RQs. If buffers are NOT initially
12154 * posted for every RQE, entry_repost should be adjusted accordingly.
12155 */
12156 queue->entry_repost = (entry_count >> 3);
12157 if (queue->entry_repost < LPFC_QUEUE_MIN_REPOST)
12158 queue->entry_repost = LPFC_QUEUE_MIN_REPOST;
12159 queue->phba = phba;
12160
12161 return queue;
12162 out_fail:
12163 lpfc_sli4_queue_free(queue);
12164 return NULL;
12165 }
12166
12167 /**
12168 * lpfc_dual_chute_pci_bar_map - Map pci base address register to host memory
12169 * @phba: HBA structure that indicates port to create a queue on.
12170 * @pci_barset: PCI BAR set flag.
12171 *
12172 * This function shall perform iomap of the specified PCI BAR address to host
12173 * memory address if not already done so and return it. The returned host
12174 * memory address can be NULL.
12175 */
12176 static void __iomem *
12177 lpfc_dual_chute_pci_bar_map(struct lpfc_hba *phba, uint16_t pci_barset)
12178 {
12179 struct pci_dev *pdev;
12180 unsigned long bar_map, bar_map_len;
12181
12182 if (!phba->pcidev)
12183 return NULL;
12184 else
12185 pdev = phba->pcidev;
12186
12187 switch (pci_barset) {
12188 case WQ_PCI_BAR_0_AND_1:
12189 if (!phba->pci_bar0_memmap_p) {
12190 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR0);
12191 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR0);
12192 phba->pci_bar0_memmap_p = ioremap(bar_map, bar_map_len);
12193 }
12194 return phba->pci_bar0_memmap_p;
12195 case WQ_PCI_BAR_2_AND_3:
12196 if (!phba->pci_bar2_memmap_p) {
12197 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR2);
12198 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR2);
12199 phba->pci_bar2_memmap_p = ioremap(bar_map, bar_map_len);
12200 }
12201 return phba->pci_bar2_memmap_p;
12202 case WQ_PCI_BAR_4_AND_5:
12203 if (!phba->pci_bar4_memmap_p) {
12204 bar_map = pci_resource_start(pdev, PCI_64BIT_BAR4);
12205 bar_map_len = pci_resource_len(pdev, PCI_64BIT_BAR4);
12206 phba->pci_bar4_memmap_p = ioremap(bar_map, bar_map_len);
12207 }
12208 return phba->pci_bar4_memmap_p;
12209 default:
12210 break;
12211 }
12212 return NULL;
12213 }
12214
12215 /**
12216 * lpfc_modify_fcp_eq_delay - Modify Delay Multiplier on FCP EQs
12217 * @phba: HBA structure that indicates port to create a queue on.
12218 * @startq: The starting FCP EQ to modify
12219 *
12220 * This function sends an MODIFY_EQ_DELAY mailbox command to the HBA.
12221 *
12222 * The @phba struct is used to send mailbox command to HBA. The @startq
12223 * is used to get the starting FCP EQ to change.
12224 * This function is asynchronous and will wait for the mailbox
12225 * command to finish before continuing.
12226 *
12227 * On success this function will return a zero. If unable to allocate enough
12228 * memory this function will return -ENOMEM. If the queue create mailbox command
12229 * fails this function will return -ENXIO.
12230 **/
12231 uint32_t
12232 lpfc_modify_fcp_eq_delay(struct lpfc_hba *phba, uint16_t startq)
12233 {
12234 struct lpfc_mbx_modify_eq_delay *eq_delay;
12235 LPFC_MBOXQ_t *mbox;
12236 struct lpfc_queue *eq;
12237 int cnt, rc, length, status = 0;
12238 uint32_t shdr_status, shdr_add_status;
12239 uint32_t result;
12240 int fcp_eqidx;
12241 union lpfc_sli4_cfg_shdr *shdr;
12242 uint16_t dmult;
12243
12244 if (startq >= phba->cfg_fcp_io_channel)
12245 return 0;
12246
12247 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12248 if (!mbox)
12249 return -ENOMEM;
12250 length = (sizeof(struct lpfc_mbx_modify_eq_delay) -
12251 sizeof(struct lpfc_sli4_cfg_mhdr));
12252 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12253 LPFC_MBOX_OPCODE_MODIFY_EQ_DELAY,
12254 length, LPFC_SLI4_MBX_EMBED);
12255 eq_delay = &mbox->u.mqe.un.eq_delay;
12256
12257 /* Calculate delay multiper from maximum interrupt per second */
12258 result = phba->cfg_fcp_imax / phba->cfg_fcp_io_channel;
12259 if (result > LPFC_DMULT_CONST)
12260 dmult = 0;
12261 else
12262 dmult = LPFC_DMULT_CONST/result - 1;
12263
12264 cnt = 0;
12265 for (fcp_eqidx = startq; fcp_eqidx < phba->cfg_fcp_io_channel;
12266 fcp_eqidx++) {
12267 eq = phba->sli4_hba.hba_eq[fcp_eqidx];
12268 if (!eq)
12269 continue;
12270 eq_delay->u.request.eq[cnt].eq_id = eq->queue_id;
12271 eq_delay->u.request.eq[cnt].phase = 0;
12272 eq_delay->u.request.eq[cnt].delay_multi = dmult;
12273 cnt++;
12274 if (cnt >= LPFC_MAX_EQ_DELAY)
12275 break;
12276 }
12277 eq_delay->u.request.num_eq = cnt;
12278
12279 mbox->vport = phba->pport;
12280 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12281 mbox->context1 = NULL;
12282 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12283 shdr = (union lpfc_sli4_cfg_shdr *) &eq_delay->header.cfg_shdr;
12284 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12285 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12286 if (shdr_status || shdr_add_status || rc) {
12287 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12288 "2512 MODIFY_EQ_DELAY mailbox failed with "
12289 "status x%x add_status x%x, mbx status x%x\n",
12290 shdr_status, shdr_add_status, rc);
12291 status = -ENXIO;
12292 }
12293 mempool_free(mbox, phba->mbox_mem_pool);
12294 return status;
12295 }
12296
12297 /**
12298 * lpfc_eq_create - Create an Event Queue on the HBA
12299 * @phba: HBA structure that indicates port to create a queue on.
12300 * @eq: The queue structure to use to create the event queue.
12301 * @imax: The maximum interrupt per second limit.
12302 *
12303 * This function creates an event queue, as detailed in @eq, on a port,
12304 * described by @phba by sending an EQ_CREATE mailbox command to the HBA.
12305 *
12306 * The @phba struct is used to send mailbox command to HBA. The @eq struct
12307 * is used to get the entry count and entry size that are necessary to
12308 * determine the number of pages to allocate and use for this queue. This
12309 * function will send the EQ_CREATE mailbox command to the HBA to setup the
12310 * event queue. This function is asynchronous and will wait for the mailbox
12311 * command to finish before continuing.
12312 *
12313 * On success this function will return a zero. If unable to allocate enough
12314 * memory this function will return -ENOMEM. If the queue create mailbox command
12315 * fails this function will return -ENXIO.
12316 **/
12317 uint32_t
12318 lpfc_eq_create(struct lpfc_hba *phba, struct lpfc_queue *eq, uint32_t imax)
12319 {
12320 struct lpfc_mbx_eq_create *eq_create;
12321 LPFC_MBOXQ_t *mbox;
12322 int rc, length, status = 0;
12323 struct lpfc_dmabuf *dmabuf;
12324 uint32_t shdr_status, shdr_add_status;
12325 union lpfc_sli4_cfg_shdr *shdr;
12326 uint16_t dmult;
12327 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12328
12329 /* sanity check on queue memory */
12330 if (!eq)
12331 return -ENODEV;
12332 if (!phba->sli4_hba.pc_sli4_params.supported)
12333 hw_page_size = SLI4_PAGE_SIZE;
12334
12335 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12336 if (!mbox)
12337 return -ENOMEM;
12338 length = (sizeof(struct lpfc_mbx_eq_create) -
12339 sizeof(struct lpfc_sli4_cfg_mhdr));
12340 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12341 LPFC_MBOX_OPCODE_EQ_CREATE,
12342 length, LPFC_SLI4_MBX_EMBED);
12343 eq_create = &mbox->u.mqe.un.eq_create;
12344 bf_set(lpfc_mbx_eq_create_num_pages, &eq_create->u.request,
12345 eq->page_count);
12346 bf_set(lpfc_eq_context_size, &eq_create->u.request.context,
12347 LPFC_EQE_SIZE);
12348 bf_set(lpfc_eq_context_valid, &eq_create->u.request.context, 1);
12349 /* Calculate delay multiper from maximum interrupt per second */
12350 if (imax > LPFC_DMULT_CONST)
12351 dmult = 0;
12352 else
12353 dmult = LPFC_DMULT_CONST/imax - 1;
12354 bf_set(lpfc_eq_context_delay_multi, &eq_create->u.request.context,
12355 dmult);
12356 switch (eq->entry_count) {
12357 default:
12358 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12359 "0360 Unsupported EQ count. (%d)\n",
12360 eq->entry_count);
12361 if (eq->entry_count < 256)
12362 return -EINVAL;
12363 /* otherwise default to smallest count (drop through) */
12364 case 256:
12365 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12366 LPFC_EQ_CNT_256);
12367 break;
12368 case 512:
12369 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12370 LPFC_EQ_CNT_512);
12371 break;
12372 case 1024:
12373 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12374 LPFC_EQ_CNT_1024);
12375 break;
12376 case 2048:
12377 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12378 LPFC_EQ_CNT_2048);
12379 break;
12380 case 4096:
12381 bf_set(lpfc_eq_context_count, &eq_create->u.request.context,
12382 LPFC_EQ_CNT_4096);
12383 break;
12384 }
12385 list_for_each_entry(dmabuf, &eq->page_list, list) {
12386 memset(dmabuf->virt, 0, hw_page_size);
12387 eq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12388 putPaddrLow(dmabuf->phys);
12389 eq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12390 putPaddrHigh(dmabuf->phys);
12391 }
12392 mbox->vport = phba->pport;
12393 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
12394 mbox->context1 = NULL;
12395 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12396 shdr = (union lpfc_sli4_cfg_shdr *) &eq_create->header.cfg_shdr;
12397 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12398 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12399 if (shdr_status || shdr_add_status || rc) {
12400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12401 "2500 EQ_CREATE mailbox failed with "
12402 "status x%x add_status x%x, mbx status x%x\n",
12403 shdr_status, shdr_add_status, rc);
12404 status = -ENXIO;
12405 }
12406 eq->type = LPFC_EQ;
12407 eq->subtype = LPFC_NONE;
12408 eq->queue_id = bf_get(lpfc_mbx_eq_create_q_id, &eq_create->u.response);
12409 if (eq->queue_id == 0xFFFF)
12410 status = -ENXIO;
12411 eq->host_index = 0;
12412 eq->hba_index = 0;
12413
12414 mempool_free(mbox, phba->mbox_mem_pool);
12415 return status;
12416 }
12417
12418 /**
12419 * lpfc_cq_create - Create a Completion Queue on the HBA
12420 * @phba: HBA structure that indicates port to create a queue on.
12421 * @cq: The queue structure to use to create the completion queue.
12422 * @eq: The event queue to bind this completion queue to.
12423 *
12424 * This function creates a completion queue, as detailed in @wq, on a port,
12425 * described by @phba by sending a CQ_CREATE mailbox command to the HBA.
12426 *
12427 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12428 * is used to get the entry count and entry size that are necessary to
12429 * determine the number of pages to allocate and use for this queue. The @eq
12430 * is used to indicate which event queue to bind this completion queue to. This
12431 * function will send the CQ_CREATE mailbox command to the HBA to setup the
12432 * completion queue. This function is asynchronous and will wait for the mailbox
12433 * command to finish before continuing.
12434 *
12435 * On success this function will return a zero. If unable to allocate enough
12436 * memory this function will return -ENOMEM. If the queue create mailbox command
12437 * fails this function will return -ENXIO.
12438 **/
12439 uint32_t
12440 lpfc_cq_create(struct lpfc_hba *phba, struct lpfc_queue *cq,
12441 struct lpfc_queue *eq, uint32_t type, uint32_t subtype)
12442 {
12443 struct lpfc_mbx_cq_create *cq_create;
12444 struct lpfc_dmabuf *dmabuf;
12445 LPFC_MBOXQ_t *mbox;
12446 int rc, length, status = 0;
12447 uint32_t shdr_status, shdr_add_status;
12448 union lpfc_sli4_cfg_shdr *shdr;
12449 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12450
12451 /* sanity check on queue memory */
12452 if (!cq || !eq)
12453 return -ENODEV;
12454 if (!phba->sli4_hba.pc_sli4_params.supported)
12455 hw_page_size = SLI4_PAGE_SIZE;
12456
12457 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12458 if (!mbox)
12459 return -ENOMEM;
12460 length = (sizeof(struct lpfc_mbx_cq_create) -
12461 sizeof(struct lpfc_sli4_cfg_mhdr));
12462 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12463 LPFC_MBOX_OPCODE_CQ_CREATE,
12464 length, LPFC_SLI4_MBX_EMBED);
12465 cq_create = &mbox->u.mqe.un.cq_create;
12466 shdr = (union lpfc_sli4_cfg_shdr *) &cq_create->header.cfg_shdr;
12467 bf_set(lpfc_mbx_cq_create_num_pages, &cq_create->u.request,
12468 cq->page_count);
12469 bf_set(lpfc_cq_context_event, &cq_create->u.request.context, 1);
12470 bf_set(lpfc_cq_context_valid, &cq_create->u.request.context, 1);
12471 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12472 phba->sli4_hba.pc_sli4_params.cqv);
12473 if (phba->sli4_hba.pc_sli4_params.cqv == LPFC_Q_CREATE_VERSION_2) {
12474 /* FW only supports 1. Should be PAGE_SIZE/SLI4_PAGE_SIZE */
12475 bf_set(lpfc_mbx_cq_create_page_size, &cq_create->u.request, 1);
12476 bf_set(lpfc_cq_eq_id_2, &cq_create->u.request.context,
12477 eq->queue_id);
12478 } else {
12479 bf_set(lpfc_cq_eq_id, &cq_create->u.request.context,
12480 eq->queue_id);
12481 }
12482 switch (cq->entry_count) {
12483 default:
12484 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12485 "0361 Unsupported CQ count. (%d)\n",
12486 cq->entry_count);
12487 if (cq->entry_count < 256) {
12488 status = -EINVAL;
12489 goto out;
12490 }
12491 /* otherwise default to smallest count (drop through) */
12492 case 256:
12493 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12494 LPFC_CQ_CNT_256);
12495 break;
12496 case 512:
12497 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12498 LPFC_CQ_CNT_512);
12499 break;
12500 case 1024:
12501 bf_set(lpfc_cq_context_count, &cq_create->u.request.context,
12502 LPFC_CQ_CNT_1024);
12503 break;
12504 }
12505 list_for_each_entry(dmabuf, &cq->page_list, list) {
12506 memset(dmabuf->virt, 0, hw_page_size);
12507 cq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12508 putPaddrLow(dmabuf->phys);
12509 cq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12510 putPaddrHigh(dmabuf->phys);
12511 }
12512 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12513
12514 /* The IOCTL status is embedded in the mailbox subheader. */
12515 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12516 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12517 if (shdr_status || shdr_add_status || rc) {
12518 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12519 "2501 CQ_CREATE mailbox failed with "
12520 "status x%x add_status x%x, mbx status x%x\n",
12521 shdr_status, shdr_add_status, rc);
12522 status = -ENXIO;
12523 goto out;
12524 }
12525 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12526 if (cq->queue_id == 0xFFFF) {
12527 status = -ENXIO;
12528 goto out;
12529 }
12530 /* link the cq onto the parent eq child list */
12531 list_add_tail(&cq->list, &eq->child_list);
12532 /* Set up completion queue's type and subtype */
12533 cq->type = type;
12534 cq->subtype = subtype;
12535 cq->queue_id = bf_get(lpfc_mbx_cq_create_q_id, &cq_create->u.response);
12536 cq->assoc_qid = eq->queue_id;
12537 cq->host_index = 0;
12538 cq->hba_index = 0;
12539
12540 out:
12541 mempool_free(mbox, phba->mbox_mem_pool);
12542 return status;
12543 }
12544
12545 /**
12546 * lpfc_mq_create_fb_init - Send MCC_CREATE without async events registration
12547 * @phba: HBA structure that indicates port to create a queue on.
12548 * @mq: The queue structure to use to create the mailbox queue.
12549 * @mbox: An allocated pointer to type LPFC_MBOXQ_t
12550 * @cq: The completion queue to associate with this cq.
12551 *
12552 * This function provides failback (fb) functionality when the
12553 * mq_create_ext fails on older FW generations. It's purpose is identical
12554 * to mq_create_ext otherwise.
12555 *
12556 * This routine cannot fail as all attributes were previously accessed and
12557 * initialized in mq_create_ext.
12558 **/
12559 static void
12560 lpfc_mq_create_fb_init(struct lpfc_hba *phba, struct lpfc_queue *mq,
12561 LPFC_MBOXQ_t *mbox, struct lpfc_queue *cq)
12562 {
12563 struct lpfc_mbx_mq_create *mq_create;
12564 struct lpfc_dmabuf *dmabuf;
12565 int length;
12566
12567 length = (sizeof(struct lpfc_mbx_mq_create) -
12568 sizeof(struct lpfc_sli4_cfg_mhdr));
12569 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12570 LPFC_MBOX_OPCODE_MQ_CREATE,
12571 length, LPFC_SLI4_MBX_EMBED);
12572 mq_create = &mbox->u.mqe.un.mq_create;
12573 bf_set(lpfc_mbx_mq_create_num_pages, &mq_create->u.request,
12574 mq->page_count);
12575 bf_set(lpfc_mq_context_cq_id, &mq_create->u.request.context,
12576 cq->queue_id);
12577 bf_set(lpfc_mq_context_valid, &mq_create->u.request.context, 1);
12578 switch (mq->entry_count) {
12579 case 16:
12580 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12581 LPFC_MQ_RING_SIZE_16);
12582 break;
12583 case 32:
12584 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12585 LPFC_MQ_RING_SIZE_32);
12586 break;
12587 case 64:
12588 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12589 LPFC_MQ_RING_SIZE_64);
12590 break;
12591 case 128:
12592 bf_set(lpfc_mq_context_ring_size, &mq_create->u.request.context,
12593 LPFC_MQ_RING_SIZE_128);
12594 break;
12595 }
12596 list_for_each_entry(dmabuf, &mq->page_list, list) {
12597 mq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
12598 putPaddrLow(dmabuf->phys);
12599 mq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
12600 putPaddrHigh(dmabuf->phys);
12601 }
12602 }
12603
12604 /**
12605 * lpfc_mq_create - Create a mailbox Queue on the HBA
12606 * @phba: HBA structure that indicates port to create a queue on.
12607 * @mq: The queue structure to use to create the mailbox queue.
12608 * @cq: The completion queue to associate with this cq.
12609 * @subtype: The queue's subtype.
12610 *
12611 * This function creates a mailbox queue, as detailed in @mq, on a port,
12612 * described by @phba by sending a MQ_CREATE mailbox command to the HBA.
12613 *
12614 * The @phba struct is used to send mailbox command to HBA. The @cq struct
12615 * is used to get the entry count and entry size that are necessary to
12616 * determine the number of pages to allocate and use for this queue. This
12617 * function will send the MQ_CREATE mailbox command to the HBA to setup the
12618 * mailbox queue. This function is asynchronous and will wait for the mailbox
12619 * command to finish before continuing.
12620 *
12621 * On success this function will return a zero. If unable to allocate enough
12622 * memory this function will return -ENOMEM. If the queue create mailbox command
12623 * fails this function will return -ENXIO.
12624 **/
12625 int32_t
12626 lpfc_mq_create(struct lpfc_hba *phba, struct lpfc_queue *mq,
12627 struct lpfc_queue *cq, uint32_t subtype)
12628 {
12629 struct lpfc_mbx_mq_create *mq_create;
12630 struct lpfc_mbx_mq_create_ext *mq_create_ext;
12631 struct lpfc_dmabuf *dmabuf;
12632 LPFC_MBOXQ_t *mbox;
12633 int rc, length, status = 0;
12634 uint32_t shdr_status, shdr_add_status;
12635 union lpfc_sli4_cfg_shdr *shdr;
12636 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12637
12638 /* sanity check on queue memory */
12639 if (!mq || !cq)
12640 return -ENODEV;
12641 if (!phba->sli4_hba.pc_sli4_params.supported)
12642 hw_page_size = SLI4_PAGE_SIZE;
12643
12644 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12645 if (!mbox)
12646 return -ENOMEM;
12647 length = (sizeof(struct lpfc_mbx_mq_create_ext) -
12648 sizeof(struct lpfc_sli4_cfg_mhdr));
12649 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
12650 LPFC_MBOX_OPCODE_MQ_CREATE_EXT,
12651 length, LPFC_SLI4_MBX_EMBED);
12652
12653 mq_create_ext = &mbox->u.mqe.un.mq_create_ext;
12654 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create_ext->header.cfg_shdr;
12655 bf_set(lpfc_mbx_mq_create_ext_num_pages,
12656 &mq_create_ext->u.request, mq->page_count);
12657 bf_set(lpfc_mbx_mq_create_ext_async_evt_link,
12658 &mq_create_ext->u.request, 1);
12659 bf_set(lpfc_mbx_mq_create_ext_async_evt_fip,
12660 &mq_create_ext->u.request, 1);
12661 bf_set(lpfc_mbx_mq_create_ext_async_evt_group5,
12662 &mq_create_ext->u.request, 1);
12663 bf_set(lpfc_mbx_mq_create_ext_async_evt_fc,
12664 &mq_create_ext->u.request, 1);
12665 bf_set(lpfc_mbx_mq_create_ext_async_evt_sli,
12666 &mq_create_ext->u.request, 1);
12667 bf_set(lpfc_mq_context_valid, &mq_create_ext->u.request.context, 1);
12668 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12669 phba->sli4_hba.pc_sli4_params.mqv);
12670 if (phba->sli4_hba.pc_sli4_params.mqv == LPFC_Q_CREATE_VERSION_1)
12671 bf_set(lpfc_mbx_mq_create_ext_cq_id, &mq_create_ext->u.request,
12672 cq->queue_id);
12673 else
12674 bf_set(lpfc_mq_context_cq_id, &mq_create_ext->u.request.context,
12675 cq->queue_id);
12676 switch (mq->entry_count) {
12677 default:
12678 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
12679 "0362 Unsupported MQ count. (%d)\n",
12680 mq->entry_count);
12681 if (mq->entry_count < 16) {
12682 status = -EINVAL;
12683 goto out;
12684 }
12685 /* otherwise default to smallest count (drop through) */
12686 case 16:
12687 bf_set(lpfc_mq_context_ring_size,
12688 &mq_create_ext->u.request.context,
12689 LPFC_MQ_RING_SIZE_16);
12690 break;
12691 case 32:
12692 bf_set(lpfc_mq_context_ring_size,
12693 &mq_create_ext->u.request.context,
12694 LPFC_MQ_RING_SIZE_32);
12695 break;
12696 case 64:
12697 bf_set(lpfc_mq_context_ring_size,
12698 &mq_create_ext->u.request.context,
12699 LPFC_MQ_RING_SIZE_64);
12700 break;
12701 case 128:
12702 bf_set(lpfc_mq_context_ring_size,
12703 &mq_create_ext->u.request.context,
12704 LPFC_MQ_RING_SIZE_128);
12705 break;
12706 }
12707 list_for_each_entry(dmabuf, &mq->page_list, list) {
12708 memset(dmabuf->virt, 0, hw_page_size);
12709 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_lo =
12710 putPaddrLow(dmabuf->phys);
12711 mq_create_ext->u.request.page[dmabuf->buffer_tag].addr_hi =
12712 putPaddrHigh(dmabuf->phys);
12713 }
12714 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12715 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12716 &mq_create_ext->u.response);
12717 if (rc != MBX_SUCCESS) {
12718 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12719 "2795 MQ_CREATE_EXT failed with "
12720 "status x%x. Failback to MQ_CREATE.\n",
12721 rc);
12722 lpfc_mq_create_fb_init(phba, mq, mbox, cq);
12723 mq_create = &mbox->u.mqe.un.mq_create;
12724 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12725 shdr = (union lpfc_sli4_cfg_shdr *) &mq_create->header.cfg_shdr;
12726 mq->queue_id = bf_get(lpfc_mbx_mq_create_q_id,
12727 &mq_create->u.response);
12728 }
12729
12730 /* The IOCTL status is embedded in the mailbox subheader. */
12731 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12732 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12733 if (shdr_status || shdr_add_status || rc) {
12734 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12735 "2502 MQ_CREATE mailbox failed with "
12736 "status x%x add_status x%x, mbx status x%x\n",
12737 shdr_status, shdr_add_status, rc);
12738 status = -ENXIO;
12739 goto out;
12740 }
12741 if (mq->queue_id == 0xFFFF) {
12742 status = -ENXIO;
12743 goto out;
12744 }
12745 mq->type = LPFC_MQ;
12746 mq->assoc_qid = cq->queue_id;
12747 mq->subtype = subtype;
12748 mq->host_index = 0;
12749 mq->hba_index = 0;
12750
12751 /* link the mq onto the parent cq child list */
12752 list_add_tail(&mq->list, &cq->child_list);
12753 out:
12754 mempool_free(mbox, phba->mbox_mem_pool);
12755 return status;
12756 }
12757
12758 /**
12759 * lpfc_wq_create - Create a Work Queue on the HBA
12760 * @phba: HBA structure that indicates port to create a queue on.
12761 * @wq: The queue structure to use to create the work queue.
12762 * @cq: The completion queue to bind this work queue to.
12763 * @subtype: The subtype of the work queue indicating its functionality.
12764 *
12765 * This function creates a work queue, as detailed in @wq, on a port, described
12766 * by @phba by sending a WQ_CREATE mailbox command to the HBA.
12767 *
12768 * The @phba struct is used to send mailbox command to HBA. The @wq struct
12769 * is used to get the entry count and entry size that are necessary to
12770 * determine the number of pages to allocate and use for this queue. The @cq
12771 * is used to indicate which completion queue to bind this work queue to. This
12772 * function will send the WQ_CREATE mailbox command to the HBA to setup the
12773 * work queue. This function is asynchronous and will wait for the mailbox
12774 * command to finish before continuing.
12775 *
12776 * On success this function will return a zero. If unable to allocate enough
12777 * memory this function will return -ENOMEM. If the queue create mailbox command
12778 * fails this function will return -ENXIO.
12779 **/
12780 uint32_t
12781 lpfc_wq_create(struct lpfc_hba *phba, struct lpfc_queue *wq,
12782 struct lpfc_queue *cq, uint32_t subtype)
12783 {
12784 struct lpfc_mbx_wq_create *wq_create;
12785 struct lpfc_dmabuf *dmabuf;
12786 LPFC_MBOXQ_t *mbox;
12787 int rc, length, status = 0;
12788 uint32_t shdr_status, shdr_add_status;
12789 union lpfc_sli4_cfg_shdr *shdr;
12790 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12791 struct dma_address *page;
12792 void __iomem *bar_memmap_p;
12793 uint32_t db_offset;
12794 uint16_t pci_barset;
12795
12796 /* sanity check on queue memory */
12797 if (!wq || !cq)
12798 return -ENODEV;
12799 if (!phba->sli4_hba.pc_sli4_params.supported)
12800 hw_page_size = SLI4_PAGE_SIZE;
12801
12802 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12803 if (!mbox)
12804 return -ENOMEM;
12805 length = (sizeof(struct lpfc_mbx_wq_create) -
12806 sizeof(struct lpfc_sli4_cfg_mhdr));
12807 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
12808 LPFC_MBOX_OPCODE_FCOE_WQ_CREATE,
12809 length, LPFC_SLI4_MBX_EMBED);
12810 wq_create = &mbox->u.mqe.un.wq_create;
12811 shdr = (union lpfc_sli4_cfg_shdr *) &wq_create->header.cfg_shdr;
12812 bf_set(lpfc_mbx_wq_create_num_pages, &wq_create->u.request,
12813 wq->page_count);
12814 bf_set(lpfc_mbx_wq_create_cq_id, &wq_create->u.request,
12815 cq->queue_id);
12816 bf_set(lpfc_mbox_hdr_version, &shdr->request,
12817 phba->sli4_hba.pc_sli4_params.wqv);
12818
12819 if (phba->sli4_hba.pc_sli4_params.wqv == LPFC_Q_CREATE_VERSION_1) {
12820 bf_set(lpfc_mbx_wq_create_wqe_count, &wq_create->u.request_1,
12821 wq->entry_count);
12822 switch (wq->entry_size) {
12823 default:
12824 case 64:
12825 bf_set(lpfc_mbx_wq_create_wqe_size,
12826 &wq_create->u.request_1,
12827 LPFC_WQ_WQE_SIZE_64);
12828 break;
12829 case 128:
12830 bf_set(lpfc_mbx_wq_create_wqe_size,
12831 &wq_create->u.request_1,
12832 LPFC_WQ_WQE_SIZE_128);
12833 break;
12834 }
12835 bf_set(lpfc_mbx_wq_create_page_size, &wq_create->u.request_1,
12836 (PAGE_SIZE/SLI4_PAGE_SIZE));
12837 page = wq_create->u.request_1.page;
12838 } else {
12839 page = wq_create->u.request.page;
12840 }
12841 list_for_each_entry(dmabuf, &wq->page_list, list) {
12842 memset(dmabuf->virt, 0, hw_page_size);
12843 page[dmabuf->buffer_tag].addr_lo = putPaddrLow(dmabuf->phys);
12844 page[dmabuf->buffer_tag].addr_hi = putPaddrHigh(dmabuf->phys);
12845 }
12846
12847 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
12848 bf_set(lpfc_mbx_wq_create_dua, &wq_create->u.request, 1);
12849
12850 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
12851 /* The IOCTL status is embedded in the mailbox subheader. */
12852 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
12853 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
12854 if (shdr_status || shdr_add_status || rc) {
12855 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12856 "2503 WQ_CREATE mailbox failed with "
12857 "status x%x add_status x%x, mbx status x%x\n",
12858 shdr_status, shdr_add_status, rc);
12859 status = -ENXIO;
12860 goto out;
12861 }
12862 wq->queue_id = bf_get(lpfc_mbx_wq_create_q_id, &wq_create->u.response);
12863 if (wq->queue_id == 0xFFFF) {
12864 status = -ENXIO;
12865 goto out;
12866 }
12867 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
12868 wq->db_format = bf_get(lpfc_mbx_wq_create_db_format,
12869 &wq_create->u.response);
12870 if ((wq->db_format != LPFC_DB_LIST_FORMAT) &&
12871 (wq->db_format != LPFC_DB_RING_FORMAT)) {
12872 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12873 "3265 WQ[%d] doorbell format not "
12874 "supported: x%x\n", wq->queue_id,
12875 wq->db_format);
12876 status = -EINVAL;
12877 goto out;
12878 }
12879 pci_barset = bf_get(lpfc_mbx_wq_create_bar_set,
12880 &wq_create->u.response);
12881 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
12882 if (!bar_memmap_p) {
12883 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12884 "3263 WQ[%d] failed to memmap pci "
12885 "barset:x%x\n", wq->queue_id,
12886 pci_barset);
12887 status = -ENOMEM;
12888 goto out;
12889 }
12890 db_offset = wq_create->u.response.doorbell_offset;
12891 if ((db_offset != LPFC_ULP0_WQ_DOORBELL) &&
12892 (db_offset != LPFC_ULP1_WQ_DOORBELL)) {
12893 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
12894 "3252 WQ[%d] doorbell offset not "
12895 "supported: x%x\n", wq->queue_id,
12896 db_offset);
12897 status = -EINVAL;
12898 goto out;
12899 }
12900 wq->db_regaddr = bar_memmap_p + db_offset;
12901 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
12902 "3264 WQ[%d]: barset:x%x, offset:x%x\n",
12903 wq->queue_id, pci_barset, db_offset);
12904 } else {
12905 wq->db_format = LPFC_DB_LIST_FORMAT;
12906 wq->db_regaddr = phba->sli4_hba.WQDBregaddr;
12907 }
12908 wq->type = LPFC_WQ;
12909 wq->assoc_qid = cq->queue_id;
12910 wq->subtype = subtype;
12911 wq->host_index = 0;
12912 wq->hba_index = 0;
12913 wq->entry_repost = LPFC_RELEASE_NOTIFICATION_INTERVAL;
12914
12915 /* link the wq onto the parent cq child list */
12916 list_add_tail(&wq->list, &cq->child_list);
12917 out:
12918 mempool_free(mbox, phba->mbox_mem_pool);
12919 return status;
12920 }
12921
12922 /**
12923 * lpfc_rq_adjust_repost - Adjust entry_repost for an RQ
12924 * @phba: HBA structure that indicates port to create a queue on.
12925 * @rq: The queue structure to use for the receive queue.
12926 * @qno: The associated HBQ number
12927 *
12928 *
12929 * For SLI4 we need to adjust the RQ repost value based on
12930 * the number of buffers that are initially posted to the RQ.
12931 */
12932 void
12933 lpfc_rq_adjust_repost(struct lpfc_hba *phba, struct lpfc_queue *rq, int qno)
12934 {
12935 uint32_t cnt;
12936
12937 /* sanity check on queue memory */
12938 if (!rq)
12939 return;
12940 cnt = lpfc_hbq_defs[qno]->entry_count;
12941
12942 /* Recalc repost for RQs based on buffers initially posted */
12943 cnt = (cnt >> 3);
12944 if (cnt < LPFC_QUEUE_MIN_REPOST)
12945 cnt = LPFC_QUEUE_MIN_REPOST;
12946
12947 rq->entry_repost = cnt;
12948 }
12949
12950 /**
12951 * lpfc_rq_create - Create a Receive Queue on the HBA
12952 * @phba: HBA structure that indicates port to create a queue on.
12953 * @hrq: The queue structure to use to create the header receive queue.
12954 * @drq: The queue structure to use to create the data receive queue.
12955 * @cq: The completion queue to bind this work queue to.
12956 *
12957 * This function creates a receive buffer queue pair , as detailed in @hrq and
12958 * @drq, on a port, described by @phba by sending a RQ_CREATE mailbox command
12959 * to the HBA.
12960 *
12961 * The @phba struct is used to send mailbox command to HBA. The @drq and @hrq
12962 * struct is used to get the entry count that is necessary to determine the
12963 * number of pages to use for this queue. The @cq is used to indicate which
12964 * completion queue to bind received buffers that are posted to these queues to.
12965 * This function will send the RQ_CREATE mailbox command to the HBA to setup the
12966 * receive queue pair. This function is asynchronous and will wait for the
12967 * mailbox command to finish before continuing.
12968 *
12969 * On success this function will return a zero. If unable to allocate enough
12970 * memory this function will return -ENOMEM. If the queue create mailbox command
12971 * fails this function will return -ENXIO.
12972 **/
12973 uint32_t
12974 lpfc_rq_create(struct lpfc_hba *phba, struct lpfc_queue *hrq,
12975 struct lpfc_queue *drq, struct lpfc_queue *cq, uint32_t subtype)
12976 {
12977 struct lpfc_mbx_rq_create *rq_create;
12978 struct lpfc_dmabuf *dmabuf;
12979 LPFC_MBOXQ_t *mbox;
12980 int rc, length, status = 0;
12981 uint32_t shdr_status, shdr_add_status;
12982 union lpfc_sli4_cfg_shdr *shdr;
12983 uint32_t hw_page_size = phba->sli4_hba.pc_sli4_params.if_page_sz;
12984 void __iomem *bar_memmap_p;
12985 uint32_t db_offset;
12986 uint16_t pci_barset;
12987
12988 /* sanity check on queue memory */
12989 if (!hrq || !drq || !cq)
12990 return -ENODEV;
12991 if (!phba->sli4_hba.pc_sli4_params.supported)
12992 hw_page_size = SLI4_PAGE_SIZE;
12993
12994 if (hrq->entry_count != drq->entry_count)
12995 return -EINVAL;
12996 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
12997 if (!mbox)
12998 return -ENOMEM;
12999 length = (sizeof(struct lpfc_mbx_rq_create) -
13000 sizeof(struct lpfc_sli4_cfg_mhdr));
13001 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13002 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13003 length, LPFC_SLI4_MBX_EMBED);
13004 rq_create = &mbox->u.mqe.un.rq_create;
13005 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13006 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13007 phba->sli4_hba.pc_sli4_params.rqv);
13008 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13009 bf_set(lpfc_rq_context_rqe_count_1,
13010 &rq_create->u.request.context,
13011 hrq->entry_count);
13012 rq_create->u.request.context.buffer_size = LPFC_HDR_BUF_SIZE;
13013 bf_set(lpfc_rq_context_rqe_size,
13014 &rq_create->u.request.context,
13015 LPFC_RQE_SIZE_8);
13016 bf_set(lpfc_rq_context_page_size,
13017 &rq_create->u.request.context,
13018 (PAGE_SIZE/SLI4_PAGE_SIZE));
13019 } else {
13020 switch (hrq->entry_count) {
13021 default:
13022 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13023 "2535 Unsupported RQ count. (%d)\n",
13024 hrq->entry_count);
13025 if (hrq->entry_count < 512) {
13026 status = -EINVAL;
13027 goto out;
13028 }
13029 /* otherwise default to smallest count (drop through) */
13030 case 512:
13031 bf_set(lpfc_rq_context_rqe_count,
13032 &rq_create->u.request.context,
13033 LPFC_RQ_RING_SIZE_512);
13034 break;
13035 case 1024:
13036 bf_set(lpfc_rq_context_rqe_count,
13037 &rq_create->u.request.context,
13038 LPFC_RQ_RING_SIZE_1024);
13039 break;
13040 case 2048:
13041 bf_set(lpfc_rq_context_rqe_count,
13042 &rq_create->u.request.context,
13043 LPFC_RQ_RING_SIZE_2048);
13044 break;
13045 case 4096:
13046 bf_set(lpfc_rq_context_rqe_count,
13047 &rq_create->u.request.context,
13048 LPFC_RQ_RING_SIZE_4096);
13049 break;
13050 }
13051 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13052 LPFC_HDR_BUF_SIZE);
13053 }
13054 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13055 cq->queue_id);
13056 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13057 hrq->page_count);
13058 list_for_each_entry(dmabuf, &hrq->page_list, list) {
13059 memset(dmabuf->virt, 0, hw_page_size);
13060 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13061 putPaddrLow(dmabuf->phys);
13062 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13063 putPaddrHigh(dmabuf->phys);
13064 }
13065 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13066 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13067
13068 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13069 /* The IOCTL status is embedded in the mailbox subheader. */
13070 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13071 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13072 if (shdr_status || shdr_add_status || rc) {
13073 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13074 "2504 RQ_CREATE mailbox failed with "
13075 "status x%x add_status x%x, mbx status x%x\n",
13076 shdr_status, shdr_add_status, rc);
13077 status = -ENXIO;
13078 goto out;
13079 }
13080 hrq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13081 if (hrq->queue_id == 0xFFFF) {
13082 status = -ENXIO;
13083 goto out;
13084 }
13085
13086 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE) {
13087 hrq->db_format = bf_get(lpfc_mbx_rq_create_db_format,
13088 &rq_create->u.response);
13089 if ((hrq->db_format != LPFC_DB_LIST_FORMAT) &&
13090 (hrq->db_format != LPFC_DB_RING_FORMAT)) {
13091 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13092 "3262 RQ [%d] doorbell format not "
13093 "supported: x%x\n", hrq->queue_id,
13094 hrq->db_format);
13095 status = -EINVAL;
13096 goto out;
13097 }
13098
13099 pci_barset = bf_get(lpfc_mbx_rq_create_bar_set,
13100 &rq_create->u.response);
13101 bar_memmap_p = lpfc_dual_chute_pci_bar_map(phba, pci_barset);
13102 if (!bar_memmap_p) {
13103 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13104 "3269 RQ[%d] failed to memmap pci "
13105 "barset:x%x\n", hrq->queue_id,
13106 pci_barset);
13107 status = -ENOMEM;
13108 goto out;
13109 }
13110
13111 db_offset = rq_create->u.response.doorbell_offset;
13112 if ((db_offset != LPFC_ULP0_RQ_DOORBELL) &&
13113 (db_offset != LPFC_ULP1_RQ_DOORBELL)) {
13114 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13115 "3270 RQ[%d] doorbell offset not "
13116 "supported: x%x\n", hrq->queue_id,
13117 db_offset);
13118 status = -EINVAL;
13119 goto out;
13120 }
13121 hrq->db_regaddr = bar_memmap_p + db_offset;
13122 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
13123 "3266 RQ[qid:%d]: barset:x%x, offset:x%x\n",
13124 hrq->queue_id, pci_barset, db_offset);
13125 } else {
13126 hrq->db_format = LPFC_DB_RING_FORMAT;
13127 hrq->db_regaddr = phba->sli4_hba.RQDBregaddr;
13128 }
13129 hrq->type = LPFC_HRQ;
13130 hrq->assoc_qid = cq->queue_id;
13131 hrq->subtype = subtype;
13132 hrq->host_index = 0;
13133 hrq->hba_index = 0;
13134
13135 /* now create the data queue */
13136 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13137 LPFC_MBOX_OPCODE_FCOE_RQ_CREATE,
13138 length, LPFC_SLI4_MBX_EMBED);
13139 bf_set(lpfc_mbox_hdr_version, &shdr->request,
13140 phba->sli4_hba.pc_sli4_params.rqv);
13141 if (phba->sli4_hba.pc_sli4_params.rqv == LPFC_Q_CREATE_VERSION_1) {
13142 bf_set(lpfc_rq_context_rqe_count_1,
13143 &rq_create->u.request.context, hrq->entry_count);
13144 rq_create->u.request.context.buffer_size = LPFC_DATA_BUF_SIZE;
13145 bf_set(lpfc_rq_context_rqe_size, &rq_create->u.request.context,
13146 LPFC_RQE_SIZE_8);
13147 bf_set(lpfc_rq_context_page_size, &rq_create->u.request.context,
13148 (PAGE_SIZE/SLI4_PAGE_SIZE));
13149 } else {
13150 switch (drq->entry_count) {
13151 default:
13152 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13153 "2536 Unsupported RQ count. (%d)\n",
13154 drq->entry_count);
13155 if (drq->entry_count < 512) {
13156 status = -EINVAL;
13157 goto out;
13158 }
13159 /* otherwise default to smallest count (drop through) */
13160 case 512:
13161 bf_set(lpfc_rq_context_rqe_count,
13162 &rq_create->u.request.context,
13163 LPFC_RQ_RING_SIZE_512);
13164 break;
13165 case 1024:
13166 bf_set(lpfc_rq_context_rqe_count,
13167 &rq_create->u.request.context,
13168 LPFC_RQ_RING_SIZE_1024);
13169 break;
13170 case 2048:
13171 bf_set(lpfc_rq_context_rqe_count,
13172 &rq_create->u.request.context,
13173 LPFC_RQ_RING_SIZE_2048);
13174 break;
13175 case 4096:
13176 bf_set(lpfc_rq_context_rqe_count,
13177 &rq_create->u.request.context,
13178 LPFC_RQ_RING_SIZE_4096);
13179 break;
13180 }
13181 bf_set(lpfc_rq_context_buf_size, &rq_create->u.request.context,
13182 LPFC_DATA_BUF_SIZE);
13183 }
13184 bf_set(lpfc_rq_context_cq_id, &rq_create->u.request.context,
13185 cq->queue_id);
13186 bf_set(lpfc_mbx_rq_create_num_pages, &rq_create->u.request,
13187 drq->page_count);
13188 list_for_each_entry(dmabuf, &drq->page_list, list) {
13189 rq_create->u.request.page[dmabuf->buffer_tag].addr_lo =
13190 putPaddrLow(dmabuf->phys);
13191 rq_create->u.request.page[dmabuf->buffer_tag].addr_hi =
13192 putPaddrHigh(dmabuf->phys);
13193 }
13194 if (phba->sli4_hba.fw_func_mode & LPFC_DUA_MODE)
13195 bf_set(lpfc_mbx_rq_create_dua, &rq_create->u.request, 1);
13196 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13197 /* The IOCTL status is embedded in the mailbox subheader. */
13198 shdr = (union lpfc_sli4_cfg_shdr *) &rq_create->header.cfg_shdr;
13199 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13200 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13201 if (shdr_status || shdr_add_status || rc) {
13202 status = -ENXIO;
13203 goto out;
13204 }
13205 drq->queue_id = bf_get(lpfc_mbx_rq_create_q_id, &rq_create->u.response);
13206 if (drq->queue_id == 0xFFFF) {
13207 status = -ENXIO;
13208 goto out;
13209 }
13210 drq->type = LPFC_DRQ;
13211 drq->assoc_qid = cq->queue_id;
13212 drq->subtype = subtype;
13213 drq->host_index = 0;
13214 drq->hba_index = 0;
13215
13216 /* link the header and data RQs onto the parent cq child list */
13217 list_add_tail(&hrq->list, &cq->child_list);
13218 list_add_tail(&drq->list, &cq->child_list);
13219
13220 out:
13221 mempool_free(mbox, phba->mbox_mem_pool);
13222 return status;
13223 }
13224
13225 /**
13226 * lpfc_eq_destroy - Destroy an event Queue on the HBA
13227 * @eq: The queue structure associated with the queue to destroy.
13228 *
13229 * This function destroys a queue, as detailed in @eq by sending an mailbox
13230 * command, specific to the type of queue, to the HBA.
13231 *
13232 * The @eq struct is used to get the queue ID of the queue to destroy.
13233 *
13234 * On success this function will return a zero. If the queue destroy mailbox
13235 * command fails this function will return -ENXIO.
13236 **/
13237 uint32_t
13238 lpfc_eq_destroy(struct lpfc_hba *phba, struct lpfc_queue *eq)
13239 {
13240 LPFC_MBOXQ_t *mbox;
13241 int rc, length, status = 0;
13242 uint32_t shdr_status, shdr_add_status;
13243 union lpfc_sli4_cfg_shdr *shdr;
13244
13245 /* sanity check on queue memory */
13246 if (!eq)
13247 return -ENODEV;
13248 mbox = mempool_alloc(eq->phba->mbox_mem_pool, GFP_KERNEL);
13249 if (!mbox)
13250 return -ENOMEM;
13251 length = (sizeof(struct lpfc_mbx_eq_destroy) -
13252 sizeof(struct lpfc_sli4_cfg_mhdr));
13253 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13254 LPFC_MBOX_OPCODE_EQ_DESTROY,
13255 length, LPFC_SLI4_MBX_EMBED);
13256 bf_set(lpfc_mbx_eq_destroy_q_id, &mbox->u.mqe.un.eq_destroy.u.request,
13257 eq->queue_id);
13258 mbox->vport = eq->phba->pport;
13259 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13260
13261 rc = lpfc_sli_issue_mbox(eq->phba, mbox, MBX_POLL);
13262 /* The IOCTL status is embedded in the mailbox subheader. */
13263 shdr = (union lpfc_sli4_cfg_shdr *)
13264 &mbox->u.mqe.un.eq_destroy.header.cfg_shdr;
13265 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13266 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13267 if (shdr_status || shdr_add_status || rc) {
13268 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13269 "2505 EQ_DESTROY mailbox failed with "
13270 "status x%x add_status x%x, mbx status x%x\n",
13271 shdr_status, shdr_add_status, rc);
13272 status = -ENXIO;
13273 }
13274
13275 /* Remove eq from any list */
13276 list_del_init(&eq->list);
13277 mempool_free(mbox, eq->phba->mbox_mem_pool);
13278 return status;
13279 }
13280
13281 /**
13282 * lpfc_cq_destroy - Destroy a Completion Queue on the HBA
13283 * @cq: The queue structure associated with the queue to destroy.
13284 *
13285 * This function destroys a queue, as detailed in @cq by sending an mailbox
13286 * command, specific to the type of queue, to the HBA.
13287 *
13288 * The @cq struct is used to get the queue ID of the queue to destroy.
13289 *
13290 * On success this function will return a zero. If the queue destroy mailbox
13291 * command fails this function will return -ENXIO.
13292 **/
13293 uint32_t
13294 lpfc_cq_destroy(struct lpfc_hba *phba, struct lpfc_queue *cq)
13295 {
13296 LPFC_MBOXQ_t *mbox;
13297 int rc, length, status = 0;
13298 uint32_t shdr_status, shdr_add_status;
13299 union lpfc_sli4_cfg_shdr *shdr;
13300
13301 /* sanity check on queue memory */
13302 if (!cq)
13303 return -ENODEV;
13304 mbox = mempool_alloc(cq->phba->mbox_mem_pool, GFP_KERNEL);
13305 if (!mbox)
13306 return -ENOMEM;
13307 length = (sizeof(struct lpfc_mbx_cq_destroy) -
13308 sizeof(struct lpfc_sli4_cfg_mhdr));
13309 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13310 LPFC_MBOX_OPCODE_CQ_DESTROY,
13311 length, LPFC_SLI4_MBX_EMBED);
13312 bf_set(lpfc_mbx_cq_destroy_q_id, &mbox->u.mqe.un.cq_destroy.u.request,
13313 cq->queue_id);
13314 mbox->vport = cq->phba->pport;
13315 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13316 rc = lpfc_sli_issue_mbox(cq->phba, mbox, MBX_POLL);
13317 /* The IOCTL status is embedded in the mailbox subheader. */
13318 shdr = (union lpfc_sli4_cfg_shdr *)
13319 &mbox->u.mqe.un.wq_create.header.cfg_shdr;
13320 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13321 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13322 if (shdr_status || shdr_add_status || rc) {
13323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13324 "2506 CQ_DESTROY mailbox failed with "
13325 "status x%x add_status x%x, mbx status x%x\n",
13326 shdr_status, shdr_add_status, rc);
13327 status = -ENXIO;
13328 }
13329 /* Remove cq from any list */
13330 list_del_init(&cq->list);
13331 mempool_free(mbox, cq->phba->mbox_mem_pool);
13332 return status;
13333 }
13334
13335 /**
13336 * lpfc_mq_destroy - Destroy a Mailbox Queue on the HBA
13337 * @qm: The queue structure associated with the queue to destroy.
13338 *
13339 * This function destroys a queue, as detailed in @mq by sending an mailbox
13340 * command, specific to the type of queue, to the HBA.
13341 *
13342 * The @mq struct is used to get the queue ID of the queue to destroy.
13343 *
13344 * On success this function will return a zero. If the queue destroy mailbox
13345 * command fails this function will return -ENXIO.
13346 **/
13347 uint32_t
13348 lpfc_mq_destroy(struct lpfc_hba *phba, struct lpfc_queue *mq)
13349 {
13350 LPFC_MBOXQ_t *mbox;
13351 int rc, length, status = 0;
13352 uint32_t shdr_status, shdr_add_status;
13353 union lpfc_sli4_cfg_shdr *shdr;
13354
13355 /* sanity check on queue memory */
13356 if (!mq)
13357 return -ENODEV;
13358 mbox = mempool_alloc(mq->phba->mbox_mem_pool, GFP_KERNEL);
13359 if (!mbox)
13360 return -ENOMEM;
13361 length = (sizeof(struct lpfc_mbx_mq_destroy) -
13362 sizeof(struct lpfc_sli4_cfg_mhdr));
13363 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
13364 LPFC_MBOX_OPCODE_MQ_DESTROY,
13365 length, LPFC_SLI4_MBX_EMBED);
13366 bf_set(lpfc_mbx_mq_destroy_q_id, &mbox->u.mqe.un.mq_destroy.u.request,
13367 mq->queue_id);
13368 mbox->vport = mq->phba->pport;
13369 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13370 rc = lpfc_sli_issue_mbox(mq->phba, mbox, MBX_POLL);
13371 /* The IOCTL status is embedded in the mailbox subheader. */
13372 shdr = (union lpfc_sli4_cfg_shdr *)
13373 &mbox->u.mqe.un.mq_destroy.header.cfg_shdr;
13374 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13375 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13376 if (shdr_status || shdr_add_status || rc) {
13377 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13378 "2507 MQ_DESTROY mailbox failed with "
13379 "status x%x add_status x%x, mbx status x%x\n",
13380 shdr_status, shdr_add_status, rc);
13381 status = -ENXIO;
13382 }
13383 /* Remove mq from any list */
13384 list_del_init(&mq->list);
13385 mempool_free(mbox, mq->phba->mbox_mem_pool);
13386 return status;
13387 }
13388
13389 /**
13390 * lpfc_wq_destroy - Destroy a Work Queue on the HBA
13391 * @wq: The queue structure associated with the queue to destroy.
13392 *
13393 * This function destroys a queue, as detailed in @wq by sending an mailbox
13394 * command, specific to the type of queue, to the HBA.
13395 *
13396 * The @wq struct is used to get the queue ID of the queue to destroy.
13397 *
13398 * On success this function will return a zero. If the queue destroy mailbox
13399 * command fails this function will return -ENXIO.
13400 **/
13401 uint32_t
13402 lpfc_wq_destroy(struct lpfc_hba *phba, struct lpfc_queue *wq)
13403 {
13404 LPFC_MBOXQ_t *mbox;
13405 int rc, length, status = 0;
13406 uint32_t shdr_status, shdr_add_status;
13407 union lpfc_sli4_cfg_shdr *shdr;
13408
13409 /* sanity check on queue memory */
13410 if (!wq)
13411 return -ENODEV;
13412 mbox = mempool_alloc(wq->phba->mbox_mem_pool, GFP_KERNEL);
13413 if (!mbox)
13414 return -ENOMEM;
13415 length = (sizeof(struct lpfc_mbx_wq_destroy) -
13416 sizeof(struct lpfc_sli4_cfg_mhdr));
13417 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13418 LPFC_MBOX_OPCODE_FCOE_WQ_DESTROY,
13419 length, LPFC_SLI4_MBX_EMBED);
13420 bf_set(lpfc_mbx_wq_destroy_q_id, &mbox->u.mqe.un.wq_destroy.u.request,
13421 wq->queue_id);
13422 mbox->vport = wq->phba->pport;
13423 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13424 rc = lpfc_sli_issue_mbox(wq->phba, mbox, MBX_POLL);
13425 shdr = (union lpfc_sli4_cfg_shdr *)
13426 &mbox->u.mqe.un.wq_destroy.header.cfg_shdr;
13427 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13428 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13429 if (shdr_status || shdr_add_status || rc) {
13430 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13431 "2508 WQ_DESTROY mailbox failed with "
13432 "status x%x add_status x%x, mbx status x%x\n",
13433 shdr_status, shdr_add_status, rc);
13434 status = -ENXIO;
13435 }
13436 /* Remove wq from any list */
13437 list_del_init(&wq->list);
13438 mempool_free(mbox, wq->phba->mbox_mem_pool);
13439 return status;
13440 }
13441
13442 /**
13443 * lpfc_rq_destroy - Destroy a Receive Queue on the HBA
13444 * @rq: The queue structure associated with the queue to destroy.
13445 *
13446 * This function destroys a queue, as detailed in @rq by sending an mailbox
13447 * command, specific to the type of queue, to the HBA.
13448 *
13449 * The @rq struct is used to get the queue ID of the queue to destroy.
13450 *
13451 * On success this function will return a zero. If the queue destroy mailbox
13452 * command fails this function will return -ENXIO.
13453 **/
13454 uint32_t
13455 lpfc_rq_destroy(struct lpfc_hba *phba, struct lpfc_queue *hrq,
13456 struct lpfc_queue *drq)
13457 {
13458 LPFC_MBOXQ_t *mbox;
13459 int rc, length, status = 0;
13460 uint32_t shdr_status, shdr_add_status;
13461 union lpfc_sli4_cfg_shdr *shdr;
13462
13463 /* sanity check on queue memory */
13464 if (!hrq || !drq)
13465 return -ENODEV;
13466 mbox = mempool_alloc(hrq->phba->mbox_mem_pool, GFP_KERNEL);
13467 if (!mbox)
13468 return -ENOMEM;
13469 length = (sizeof(struct lpfc_mbx_rq_destroy) -
13470 sizeof(struct lpfc_sli4_cfg_mhdr));
13471 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13472 LPFC_MBOX_OPCODE_FCOE_RQ_DESTROY,
13473 length, LPFC_SLI4_MBX_EMBED);
13474 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13475 hrq->queue_id);
13476 mbox->vport = hrq->phba->pport;
13477 mbox->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
13478 rc = lpfc_sli_issue_mbox(hrq->phba, mbox, MBX_POLL);
13479 /* The IOCTL status is embedded in the mailbox subheader. */
13480 shdr = (union lpfc_sli4_cfg_shdr *)
13481 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13482 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13483 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13484 if (shdr_status || shdr_add_status || rc) {
13485 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13486 "2509 RQ_DESTROY mailbox failed with "
13487 "status x%x add_status x%x, mbx status x%x\n",
13488 shdr_status, shdr_add_status, rc);
13489 if (rc != MBX_TIMEOUT)
13490 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13491 return -ENXIO;
13492 }
13493 bf_set(lpfc_mbx_rq_destroy_q_id, &mbox->u.mqe.un.rq_destroy.u.request,
13494 drq->queue_id);
13495 rc = lpfc_sli_issue_mbox(drq->phba, mbox, MBX_POLL);
13496 shdr = (union lpfc_sli4_cfg_shdr *)
13497 &mbox->u.mqe.un.rq_destroy.header.cfg_shdr;
13498 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13499 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13500 if (shdr_status || shdr_add_status || rc) {
13501 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13502 "2510 RQ_DESTROY mailbox failed with "
13503 "status x%x add_status x%x, mbx status x%x\n",
13504 shdr_status, shdr_add_status, rc);
13505 status = -ENXIO;
13506 }
13507 list_del_init(&hrq->list);
13508 list_del_init(&drq->list);
13509 mempool_free(mbox, hrq->phba->mbox_mem_pool);
13510 return status;
13511 }
13512
13513 /**
13514 * lpfc_sli4_post_sgl - Post scatter gather list for an XRI to HBA
13515 * @phba: The virtual port for which this call being executed.
13516 * @pdma_phys_addr0: Physical address of the 1st SGL page.
13517 * @pdma_phys_addr1: Physical address of the 2nd SGL page.
13518 * @xritag: the xritag that ties this io to the SGL pages.
13519 *
13520 * This routine will post the sgl pages for the IO that has the xritag
13521 * that is in the iocbq structure. The xritag is assigned during iocbq
13522 * creation and persists for as long as the driver is loaded.
13523 * if the caller has fewer than 256 scatter gather segments to map then
13524 * pdma_phys_addr1 should be 0.
13525 * If the caller needs to map more than 256 scatter gather segment then
13526 * pdma_phys_addr1 should be a valid physical address.
13527 * physical address for SGLs must be 64 byte aligned.
13528 * If you are going to map 2 SGL's then the first one must have 256 entries
13529 * the second sgl can have between 1 and 256 entries.
13530 *
13531 * Return codes:
13532 * 0 - Success
13533 * -ENXIO, -ENOMEM - Failure
13534 **/
13535 int
13536 lpfc_sli4_post_sgl(struct lpfc_hba *phba,
13537 dma_addr_t pdma_phys_addr0,
13538 dma_addr_t pdma_phys_addr1,
13539 uint16_t xritag)
13540 {
13541 struct lpfc_mbx_post_sgl_pages *post_sgl_pages;
13542 LPFC_MBOXQ_t *mbox;
13543 int rc;
13544 uint32_t shdr_status, shdr_add_status;
13545 uint32_t mbox_tmo;
13546 union lpfc_sli4_cfg_shdr *shdr;
13547
13548 if (xritag == NO_XRI) {
13549 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13550 "0364 Invalid param:\n");
13551 return -EINVAL;
13552 }
13553
13554 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13555 if (!mbox)
13556 return -ENOMEM;
13557
13558 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13559 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES,
13560 sizeof(struct lpfc_mbx_post_sgl_pages) -
13561 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
13562
13563 post_sgl_pages = (struct lpfc_mbx_post_sgl_pages *)
13564 &mbox->u.mqe.un.post_sgl_pages;
13565 bf_set(lpfc_post_sgl_pages_xri, post_sgl_pages, xritag);
13566 bf_set(lpfc_post_sgl_pages_xricnt, post_sgl_pages, 1);
13567
13568 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_lo =
13569 cpu_to_le32(putPaddrLow(pdma_phys_addr0));
13570 post_sgl_pages->sgl_pg_pairs[0].sgl_pg0_addr_hi =
13571 cpu_to_le32(putPaddrHigh(pdma_phys_addr0));
13572
13573 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_lo =
13574 cpu_to_le32(putPaddrLow(pdma_phys_addr1));
13575 post_sgl_pages->sgl_pg_pairs[0].sgl_pg1_addr_hi =
13576 cpu_to_le32(putPaddrHigh(pdma_phys_addr1));
13577 if (!phba->sli4_hba.intr_enable)
13578 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13579 else {
13580 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13581 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13582 }
13583 /* The IOCTL status is embedded in the mailbox subheader. */
13584 shdr = (union lpfc_sli4_cfg_shdr *) &post_sgl_pages->header.cfg_shdr;
13585 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13586 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13587 if (rc != MBX_TIMEOUT)
13588 mempool_free(mbox, phba->mbox_mem_pool);
13589 if (shdr_status || shdr_add_status || rc) {
13590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13591 "2511 POST_SGL mailbox failed with "
13592 "status x%x add_status x%x, mbx status x%x\n",
13593 shdr_status, shdr_add_status, rc);
13594 rc = -ENXIO;
13595 }
13596 return 0;
13597 }
13598
13599 /**
13600 * lpfc_sli4_alloc_xri - Get an available rpi in the device's range
13601 * @phba: pointer to lpfc hba data structure.
13602 *
13603 * This routine is invoked to post rpi header templates to the
13604 * HBA consistent with the SLI-4 interface spec. This routine
13605 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
13606 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
13607 *
13608 * Returns
13609 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
13610 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
13611 **/
13612 uint16_t
13613 lpfc_sli4_alloc_xri(struct lpfc_hba *phba)
13614 {
13615 unsigned long xri;
13616
13617 /*
13618 * Fetch the next logical xri. Because this index is logical,
13619 * the driver starts at 0 each time.
13620 */
13621 spin_lock_irq(&phba->hbalock);
13622 xri = find_next_zero_bit(phba->sli4_hba.xri_bmask,
13623 phba->sli4_hba.max_cfg_param.max_xri, 0);
13624 if (xri >= phba->sli4_hba.max_cfg_param.max_xri) {
13625 spin_unlock_irq(&phba->hbalock);
13626 return NO_XRI;
13627 } else {
13628 set_bit(xri, phba->sli4_hba.xri_bmask);
13629 phba->sli4_hba.max_cfg_param.xri_used++;
13630 }
13631 spin_unlock_irq(&phba->hbalock);
13632 return xri;
13633 }
13634
13635 /**
13636 * lpfc_sli4_free_xri - Release an xri for reuse.
13637 * @phba: pointer to lpfc hba data structure.
13638 *
13639 * This routine is invoked to release an xri to the pool of
13640 * available rpis maintained by the driver.
13641 **/
13642 void
13643 __lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13644 {
13645 if (test_and_clear_bit(xri, phba->sli4_hba.xri_bmask)) {
13646 phba->sli4_hba.max_cfg_param.xri_used--;
13647 }
13648 }
13649
13650 /**
13651 * lpfc_sli4_free_xri - Release an xri for reuse.
13652 * @phba: pointer to lpfc hba data structure.
13653 *
13654 * This routine is invoked to release an xri to the pool of
13655 * available rpis maintained by the driver.
13656 **/
13657 void
13658 lpfc_sli4_free_xri(struct lpfc_hba *phba, int xri)
13659 {
13660 spin_lock_irq(&phba->hbalock);
13661 __lpfc_sli4_free_xri(phba, xri);
13662 spin_unlock_irq(&phba->hbalock);
13663 }
13664
13665 /**
13666 * lpfc_sli4_next_xritag - Get an xritag for the io
13667 * @phba: Pointer to HBA context object.
13668 *
13669 * This function gets an xritag for the iocb. If there is no unused xritag
13670 * it will return 0xffff.
13671 * The function returns the allocated xritag if successful, else returns zero.
13672 * Zero is not a valid xritag.
13673 * The caller is not required to hold any lock.
13674 **/
13675 uint16_t
13676 lpfc_sli4_next_xritag(struct lpfc_hba *phba)
13677 {
13678 uint16_t xri_index;
13679
13680 xri_index = lpfc_sli4_alloc_xri(phba);
13681 if (xri_index == NO_XRI)
13682 lpfc_printf_log(phba, KERN_WARNING, LOG_SLI,
13683 "2004 Failed to allocate XRI.last XRITAG is %d"
13684 " Max XRI is %d, Used XRI is %d\n",
13685 xri_index,
13686 phba->sli4_hba.max_cfg_param.max_xri,
13687 phba->sli4_hba.max_cfg_param.xri_used);
13688 return xri_index;
13689 }
13690
13691 /**
13692 * lpfc_sli4_post_els_sgl_list - post a block of ELS sgls to the port.
13693 * @phba: pointer to lpfc hba data structure.
13694 * @post_sgl_list: pointer to els sgl entry list.
13695 * @count: number of els sgl entries on the list.
13696 *
13697 * This routine is invoked to post a block of driver's sgl pages to the
13698 * HBA using non-embedded mailbox command. No Lock is held. This routine
13699 * is only called when the driver is loading and after all IO has been
13700 * stopped.
13701 **/
13702 static int
13703 lpfc_sli4_post_els_sgl_list(struct lpfc_hba *phba,
13704 struct list_head *post_sgl_list,
13705 int post_cnt)
13706 {
13707 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
13708 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13709 struct sgl_page_pairs *sgl_pg_pairs;
13710 void *viraddr;
13711 LPFC_MBOXQ_t *mbox;
13712 uint32_t reqlen, alloclen, pg_pairs;
13713 uint32_t mbox_tmo;
13714 uint16_t xritag_start = 0;
13715 int rc = 0;
13716 uint32_t shdr_status, shdr_add_status;
13717 union lpfc_sli4_cfg_shdr *shdr;
13718
13719 reqlen = phba->sli4_hba.els_xri_cnt * sizeof(struct sgl_page_pairs) +
13720 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13721 if (reqlen > SLI4_PAGE_SIZE) {
13722 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13723 "2559 Block sgl registration required DMA "
13724 "size (%d) great than a page\n", reqlen);
13725 return -ENOMEM;
13726 }
13727 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13728 if (!mbox)
13729 return -ENOMEM;
13730
13731 /* Allocate DMA memory and set up the non-embedded mailbox command */
13732 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13733 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13734 LPFC_SLI4_MBX_NEMBED);
13735
13736 if (alloclen < reqlen) {
13737 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13738 "0285 Allocated DMA memory size (%d) is "
13739 "less than the requested DMA memory "
13740 "size (%d)\n", alloclen, reqlen);
13741 lpfc_sli4_mbox_cmd_free(phba, mbox);
13742 return -ENOMEM;
13743 }
13744 /* Set up the SGL pages in the non-embedded DMA pages */
13745 viraddr = mbox->sge_array->addr[0];
13746 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13747 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13748
13749 pg_pairs = 0;
13750 list_for_each_entry_safe(sglq_entry, sglq_next, post_sgl_list, list) {
13751 /* Set up the sge entry */
13752 sgl_pg_pairs->sgl_pg0_addr_lo =
13753 cpu_to_le32(putPaddrLow(sglq_entry->phys));
13754 sgl_pg_pairs->sgl_pg0_addr_hi =
13755 cpu_to_le32(putPaddrHigh(sglq_entry->phys));
13756 sgl_pg_pairs->sgl_pg1_addr_lo =
13757 cpu_to_le32(putPaddrLow(0));
13758 sgl_pg_pairs->sgl_pg1_addr_hi =
13759 cpu_to_le32(putPaddrHigh(0));
13760
13761 /* Keep the first xritag on the list */
13762 if (pg_pairs == 0)
13763 xritag_start = sglq_entry->sli4_xritag;
13764 sgl_pg_pairs++;
13765 pg_pairs++;
13766 }
13767
13768 /* Complete initialization and perform endian conversion. */
13769 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13770 bf_set(lpfc_post_sgl_pages_xricnt, sgl, phba->sli4_hba.els_xri_cnt);
13771 sgl->word0 = cpu_to_le32(sgl->word0);
13772 if (!phba->sli4_hba.intr_enable)
13773 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13774 else {
13775 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13776 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13777 }
13778 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13779 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13780 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13781 if (rc != MBX_TIMEOUT)
13782 lpfc_sli4_mbox_cmd_free(phba, mbox);
13783 if (shdr_status || shdr_add_status || rc) {
13784 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13785 "2513 POST_SGL_BLOCK mailbox command failed "
13786 "status x%x add_status x%x mbx status x%x\n",
13787 shdr_status, shdr_add_status, rc);
13788 rc = -ENXIO;
13789 }
13790 return rc;
13791 }
13792
13793 /**
13794 * lpfc_sli4_post_scsi_sgl_block - post a block of scsi sgl list to firmware
13795 * @phba: pointer to lpfc hba data structure.
13796 * @sblist: pointer to scsi buffer list.
13797 * @count: number of scsi buffers on the list.
13798 *
13799 * This routine is invoked to post a block of @count scsi sgl pages from a
13800 * SCSI buffer list @sblist to the HBA using non-embedded mailbox command.
13801 * No Lock is held.
13802 *
13803 **/
13804 int
13805 lpfc_sli4_post_scsi_sgl_block(struct lpfc_hba *phba,
13806 struct list_head *sblist,
13807 int count)
13808 {
13809 struct lpfc_scsi_buf *psb;
13810 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
13811 struct sgl_page_pairs *sgl_pg_pairs;
13812 void *viraddr;
13813 LPFC_MBOXQ_t *mbox;
13814 uint32_t reqlen, alloclen, pg_pairs;
13815 uint32_t mbox_tmo;
13816 uint16_t xritag_start = 0;
13817 int rc = 0;
13818 uint32_t shdr_status, shdr_add_status;
13819 dma_addr_t pdma_phys_bpl1;
13820 union lpfc_sli4_cfg_shdr *shdr;
13821
13822 /* Calculate the requested length of the dma memory */
13823 reqlen = count * sizeof(struct sgl_page_pairs) +
13824 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
13825 if (reqlen > SLI4_PAGE_SIZE) {
13826 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
13827 "0217 Block sgl registration required DMA "
13828 "size (%d) great than a page\n", reqlen);
13829 return -ENOMEM;
13830 }
13831 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
13832 if (!mbox) {
13833 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13834 "0283 Failed to allocate mbox cmd memory\n");
13835 return -ENOMEM;
13836 }
13837
13838 /* Allocate DMA memory and set up the non-embedded mailbox command */
13839 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
13840 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
13841 LPFC_SLI4_MBX_NEMBED);
13842
13843 if (alloclen < reqlen) {
13844 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
13845 "2561 Allocated DMA memory size (%d) is "
13846 "less than the requested DMA memory "
13847 "size (%d)\n", alloclen, reqlen);
13848 lpfc_sli4_mbox_cmd_free(phba, mbox);
13849 return -ENOMEM;
13850 }
13851
13852 /* Get the first SGE entry from the non-embedded DMA memory */
13853 viraddr = mbox->sge_array->addr[0];
13854
13855 /* Set up the SGL pages in the non-embedded DMA pages */
13856 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
13857 sgl_pg_pairs = &sgl->sgl_pg_pairs;
13858
13859 pg_pairs = 0;
13860 list_for_each_entry(psb, sblist, list) {
13861 /* Set up the sge entry */
13862 sgl_pg_pairs->sgl_pg0_addr_lo =
13863 cpu_to_le32(putPaddrLow(psb->dma_phys_bpl));
13864 sgl_pg_pairs->sgl_pg0_addr_hi =
13865 cpu_to_le32(putPaddrHigh(psb->dma_phys_bpl));
13866 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
13867 pdma_phys_bpl1 = psb->dma_phys_bpl + SGL_PAGE_SIZE;
13868 else
13869 pdma_phys_bpl1 = 0;
13870 sgl_pg_pairs->sgl_pg1_addr_lo =
13871 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
13872 sgl_pg_pairs->sgl_pg1_addr_hi =
13873 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
13874 /* Keep the first xritag on the list */
13875 if (pg_pairs == 0)
13876 xritag_start = psb->cur_iocbq.sli4_xritag;
13877 sgl_pg_pairs++;
13878 pg_pairs++;
13879 }
13880 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
13881 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
13882 /* Perform endian conversion if necessary */
13883 sgl->word0 = cpu_to_le32(sgl->word0);
13884
13885 if (!phba->sli4_hba.intr_enable)
13886 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
13887 else {
13888 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
13889 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
13890 }
13891 shdr = (union lpfc_sli4_cfg_shdr *) &sgl->cfg_shdr;
13892 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
13893 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
13894 if (rc != MBX_TIMEOUT)
13895 lpfc_sli4_mbox_cmd_free(phba, mbox);
13896 if (shdr_status || shdr_add_status || rc) {
13897 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
13898 "2564 POST_SGL_BLOCK mailbox command failed "
13899 "status x%x add_status x%x mbx status x%x\n",
13900 shdr_status, shdr_add_status, rc);
13901 rc = -ENXIO;
13902 }
13903 return rc;
13904 }
13905
13906 /**
13907 * lpfc_fc_frame_check - Check that this frame is a valid frame to handle
13908 * @phba: pointer to lpfc_hba struct that the frame was received on
13909 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13910 *
13911 * This function checks the fields in the @fc_hdr to see if the FC frame is a
13912 * valid type of frame that the LPFC driver will handle. This function will
13913 * return a zero if the frame is a valid frame or a non zero value when the
13914 * frame does not pass the check.
13915 **/
13916 static int
13917 lpfc_fc_frame_check(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr)
13918 {
13919 /* make rctl_names static to save stack space */
13920 static char *rctl_names[] = FC_RCTL_NAMES_INIT;
13921 char *type_names[] = FC_TYPE_NAMES_INIT;
13922 struct fc_vft_header *fc_vft_hdr;
13923 uint32_t *header = (uint32_t *) fc_hdr;
13924
13925 switch (fc_hdr->fh_r_ctl) {
13926 case FC_RCTL_DD_UNCAT: /* uncategorized information */
13927 case FC_RCTL_DD_SOL_DATA: /* solicited data */
13928 case FC_RCTL_DD_UNSOL_CTL: /* unsolicited control */
13929 case FC_RCTL_DD_SOL_CTL: /* solicited control or reply */
13930 case FC_RCTL_DD_UNSOL_DATA: /* unsolicited data */
13931 case FC_RCTL_DD_DATA_DESC: /* data descriptor */
13932 case FC_RCTL_DD_UNSOL_CMD: /* unsolicited command */
13933 case FC_RCTL_DD_CMD_STATUS: /* command status */
13934 case FC_RCTL_ELS_REQ: /* extended link services request */
13935 case FC_RCTL_ELS_REP: /* extended link services reply */
13936 case FC_RCTL_ELS4_REQ: /* FC-4 ELS request */
13937 case FC_RCTL_ELS4_REP: /* FC-4 ELS reply */
13938 case FC_RCTL_BA_NOP: /* basic link service NOP */
13939 case FC_RCTL_BA_ABTS: /* basic link service abort */
13940 case FC_RCTL_BA_RMC: /* remove connection */
13941 case FC_RCTL_BA_ACC: /* basic accept */
13942 case FC_RCTL_BA_RJT: /* basic reject */
13943 case FC_RCTL_BA_PRMT:
13944 case FC_RCTL_ACK_1: /* acknowledge_1 */
13945 case FC_RCTL_ACK_0: /* acknowledge_0 */
13946 case FC_RCTL_P_RJT: /* port reject */
13947 case FC_RCTL_F_RJT: /* fabric reject */
13948 case FC_RCTL_P_BSY: /* port busy */
13949 case FC_RCTL_F_BSY: /* fabric busy to data frame */
13950 case FC_RCTL_F_BSYL: /* fabric busy to link control frame */
13951 case FC_RCTL_LCR: /* link credit reset */
13952 case FC_RCTL_END: /* end */
13953 break;
13954 case FC_RCTL_VFTH: /* Virtual Fabric tagging Header */
13955 fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
13956 fc_hdr = &((struct fc_frame_header *)fc_vft_hdr)[1];
13957 return lpfc_fc_frame_check(phba, fc_hdr);
13958 default:
13959 goto drop;
13960 }
13961 switch (fc_hdr->fh_type) {
13962 case FC_TYPE_BLS:
13963 case FC_TYPE_ELS:
13964 case FC_TYPE_FCP:
13965 case FC_TYPE_CT:
13966 break;
13967 case FC_TYPE_IP:
13968 case FC_TYPE_ILS:
13969 default:
13970 goto drop;
13971 }
13972
13973 lpfc_printf_log(phba, KERN_INFO, LOG_ELS,
13974 "2538 Received frame rctl:%s type:%s "
13975 "Frame Data:%08x %08x %08x %08x %08x %08x\n",
13976 rctl_names[fc_hdr->fh_r_ctl],
13977 type_names[fc_hdr->fh_type],
13978 be32_to_cpu(header[0]), be32_to_cpu(header[1]),
13979 be32_to_cpu(header[2]), be32_to_cpu(header[3]),
13980 be32_to_cpu(header[4]), be32_to_cpu(header[5]));
13981 return 0;
13982 drop:
13983 lpfc_printf_log(phba, KERN_WARNING, LOG_ELS,
13984 "2539 Dropped frame rctl:%s type:%s\n",
13985 rctl_names[fc_hdr->fh_r_ctl],
13986 type_names[fc_hdr->fh_type]);
13987 return 1;
13988 }
13989
13990 /**
13991 * lpfc_fc_hdr_get_vfi - Get the VFI from an FC frame
13992 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
13993 *
13994 * This function processes the FC header to retrieve the VFI from the VF
13995 * header, if one exists. This function will return the VFI if one exists
13996 * or 0 if no VSAN Header exists.
13997 **/
13998 static uint32_t
13999 lpfc_fc_hdr_get_vfi(struct fc_frame_header *fc_hdr)
14000 {
14001 struct fc_vft_header *fc_vft_hdr = (struct fc_vft_header *)fc_hdr;
14002
14003 if (fc_hdr->fh_r_ctl != FC_RCTL_VFTH)
14004 return 0;
14005 return bf_get(fc_vft_hdr_vf_id, fc_vft_hdr);
14006 }
14007
14008 /**
14009 * lpfc_fc_frame_to_vport - Finds the vport that a frame is destined to
14010 * @phba: Pointer to the HBA structure to search for the vport on
14011 * @fc_hdr: A pointer to the FC Header data (In Big Endian Format)
14012 * @fcfi: The FC Fabric ID that the frame came from
14013 *
14014 * This function searches the @phba for a vport that matches the content of the
14015 * @fc_hdr passed in and the @fcfi. This function uses the @fc_hdr to fetch the
14016 * VFI, if the Virtual Fabric Tagging Header exists, and the DID. This function
14017 * returns the matching vport pointer or NULL if unable to match frame to a
14018 * vport.
14019 **/
14020 static struct lpfc_vport *
14021 lpfc_fc_frame_to_vport(struct lpfc_hba *phba, struct fc_frame_header *fc_hdr,
14022 uint16_t fcfi)
14023 {
14024 struct lpfc_vport **vports;
14025 struct lpfc_vport *vport = NULL;
14026 int i;
14027 uint32_t did = (fc_hdr->fh_d_id[0] << 16 |
14028 fc_hdr->fh_d_id[1] << 8 |
14029 fc_hdr->fh_d_id[2]);
14030
14031 if (did == Fabric_DID)
14032 return phba->pport;
14033 if ((phba->pport->fc_flag & FC_PT2PT) &&
14034 !(phba->link_state == LPFC_HBA_READY))
14035 return phba->pport;
14036
14037 vports = lpfc_create_vport_work_array(phba);
14038 if (vports != NULL)
14039 for (i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
14040 if (phba->fcf.fcfi == fcfi &&
14041 vports[i]->vfi == lpfc_fc_hdr_get_vfi(fc_hdr) &&
14042 vports[i]->fc_myDID == did) {
14043 vport = vports[i];
14044 break;
14045 }
14046 }
14047 lpfc_destroy_vport_work_array(phba, vports);
14048 return vport;
14049 }
14050
14051 /**
14052 * lpfc_update_rcv_time_stamp - Update vport's rcv seq time stamp
14053 * @vport: The vport to work on.
14054 *
14055 * This function updates the receive sequence time stamp for this vport. The
14056 * receive sequence time stamp indicates the time that the last frame of the
14057 * the sequence that has been idle for the longest amount of time was received.
14058 * the driver uses this time stamp to indicate if any received sequences have
14059 * timed out.
14060 **/
14061 void
14062 lpfc_update_rcv_time_stamp(struct lpfc_vport *vport)
14063 {
14064 struct lpfc_dmabuf *h_buf;
14065 struct hbq_dmabuf *dmabuf = NULL;
14066
14067 /* get the oldest sequence on the rcv list */
14068 h_buf = list_get_first(&vport->rcv_buffer_list,
14069 struct lpfc_dmabuf, list);
14070 if (!h_buf)
14071 return;
14072 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14073 vport->rcv_buffer_time_stamp = dmabuf->time_stamp;
14074 }
14075
14076 /**
14077 * lpfc_cleanup_rcv_buffers - Cleans up all outstanding receive sequences.
14078 * @vport: The vport that the received sequences were sent to.
14079 *
14080 * This function cleans up all outstanding received sequences. This is called
14081 * by the driver when a link event or user action invalidates all the received
14082 * sequences.
14083 **/
14084 void
14085 lpfc_cleanup_rcv_buffers(struct lpfc_vport *vport)
14086 {
14087 struct lpfc_dmabuf *h_buf, *hnext;
14088 struct lpfc_dmabuf *d_buf, *dnext;
14089 struct hbq_dmabuf *dmabuf = NULL;
14090
14091 /* start with the oldest sequence on the rcv list */
14092 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14093 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14094 list_del_init(&dmabuf->hbuf.list);
14095 list_for_each_entry_safe(d_buf, dnext,
14096 &dmabuf->dbuf.list, list) {
14097 list_del_init(&d_buf->list);
14098 lpfc_in_buf_free(vport->phba, d_buf);
14099 }
14100 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14101 }
14102 }
14103
14104 /**
14105 * lpfc_rcv_seq_check_edtov - Cleans up timed out receive sequences.
14106 * @vport: The vport that the received sequences were sent to.
14107 *
14108 * This function determines whether any received sequences have timed out by
14109 * first checking the vport's rcv_buffer_time_stamp. If this time_stamp
14110 * indicates that there is at least one timed out sequence this routine will
14111 * go through the received sequences one at a time from most inactive to most
14112 * active to determine which ones need to be cleaned up. Once it has determined
14113 * that a sequence needs to be cleaned up it will simply free up the resources
14114 * without sending an abort.
14115 **/
14116 void
14117 lpfc_rcv_seq_check_edtov(struct lpfc_vport *vport)
14118 {
14119 struct lpfc_dmabuf *h_buf, *hnext;
14120 struct lpfc_dmabuf *d_buf, *dnext;
14121 struct hbq_dmabuf *dmabuf = NULL;
14122 unsigned long timeout;
14123 int abort_count = 0;
14124
14125 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14126 vport->rcv_buffer_time_stamp);
14127 if (list_empty(&vport->rcv_buffer_list) ||
14128 time_before(jiffies, timeout))
14129 return;
14130 /* start with the oldest sequence on the rcv list */
14131 list_for_each_entry_safe(h_buf, hnext, &vport->rcv_buffer_list, list) {
14132 dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14133 timeout = (msecs_to_jiffies(vport->phba->fc_edtov) +
14134 dmabuf->time_stamp);
14135 if (time_before(jiffies, timeout))
14136 break;
14137 abort_count++;
14138 list_del_init(&dmabuf->hbuf.list);
14139 list_for_each_entry_safe(d_buf, dnext,
14140 &dmabuf->dbuf.list, list) {
14141 list_del_init(&d_buf->list);
14142 lpfc_in_buf_free(vport->phba, d_buf);
14143 }
14144 lpfc_in_buf_free(vport->phba, &dmabuf->dbuf);
14145 }
14146 if (abort_count)
14147 lpfc_update_rcv_time_stamp(vport);
14148 }
14149
14150 /**
14151 * lpfc_fc_frame_add - Adds a frame to the vport's list of received sequences
14152 * @dmabuf: pointer to a dmabuf that describes the hdr and data of the FC frame
14153 *
14154 * This function searches through the existing incomplete sequences that have
14155 * been sent to this @vport. If the frame matches one of the incomplete
14156 * sequences then the dbuf in the @dmabuf is added to the list of frames that
14157 * make up that sequence. If no sequence is found that matches this frame then
14158 * the function will add the hbuf in the @dmabuf to the @vport's rcv_buffer_list
14159 * This function returns a pointer to the first dmabuf in the sequence list that
14160 * the frame was linked to.
14161 **/
14162 static struct hbq_dmabuf *
14163 lpfc_fc_frame_add(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14164 {
14165 struct fc_frame_header *new_hdr;
14166 struct fc_frame_header *temp_hdr;
14167 struct lpfc_dmabuf *d_buf;
14168 struct lpfc_dmabuf *h_buf;
14169 struct hbq_dmabuf *seq_dmabuf = NULL;
14170 struct hbq_dmabuf *temp_dmabuf = NULL;
14171
14172 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14173 dmabuf->time_stamp = jiffies;
14174 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14175 /* Use the hdr_buf to find the sequence that this frame belongs to */
14176 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14177 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14178 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14179 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14180 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14181 continue;
14182 /* found a pending sequence that matches this frame */
14183 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14184 break;
14185 }
14186 if (!seq_dmabuf) {
14187 /*
14188 * This indicates first frame received for this sequence.
14189 * Queue the buffer on the vport's rcv_buffer_list.
14190 */
14191 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14192 lpfc_update_rcv_time_stamp(vport);
14193 return dmabuf;
14194 }
14195 temp_hdr = seq_dmabuf->hbuf.virt;
14196 if (be16_to_cpu(new_hdr->fh_seq_cnt) <
14197 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14198 list_del_init(&seq_dmabuf->hbuf.list);
14199 list_add_tail(&dmabuf->hbuf.list, &vport->rcv_buffer_list);
14200 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14201 lpfc_update_rcv_time_stamp(vport);
14202 return dmabuf;
14203 }
14204 /* move this sequence to the tail to indicate a young sequence */
14205 list_move_tail(&seq_dmabuf->hbuf.list, &vport->rcv_buffer_list);
14206 seq_dmabuf->time_stamp = jiffies;
14207 lpfc_update_rcv_time_stamp(vport);
14208 if (list_empty(&seq_dmabuf->dbuf.list)) {
14209 temp_hdr = dmabuf->hbuf.virt;
14210 list_add_tail(&dmabuf->dbuf.list, &seq_dmabuf->dbuf.list);
14211 return seq_dmabuf;
14212 }
14213 /* find the correct place in the sequence to insert this frame */
14214 list_for_each_entry_reverse(d_buf, &seq_dmabuf->dbuf.list, list) {
14215 temp_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14216 temp_hdr = (struct fc_frame_header *)temp_dmabuf->hbuf.virt;
14217 /*
14218 * If the frame's sequence count is greater than the frame on
14219 * the list then insert the frame right after this frame
14220 */
14221 if (be16_to_cpu(new_hdr->fh_seq_cnt) >
14222 be16_to_cpu(temp_hdr->fh_seq_cnt)) {
14223 list_add(&dmabuf->dbuf.list, &temp_dmabuf->dbuf.list);
14224 return seq_dmabuf;
14225 }
14226 }
14227 return NULL;
14228 }
14229
14230 /**
14231 * lpfc_sli4_abort_partial_seq - Abort partially assembled unsol sequence
14232 * @vport: pointer to a vitural port
14233 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14234 *
14235 * This function tries to abort from the partially assembed sequence, described
14236 * by the information from basic abbort @dmabuf. It checks to see whether such
14237 * partially assembled sequence held by the driver. If so, it shall free up all
14238 * the frames from the partially assembled sequence.
14239 *
14240 * Return
14241 * true -- if there is matching partially assembled sequence present and all
14242 * the frames freed with the sequence;
14243 * false -- if there is no matching partially assembled sequence present so
14244 * nothing got aborted in the lower layer driver
14245 **/
14246 static bool
14247 lpfc_sli4_abort_partial_seq(struct lpfc_vport *vport,
14248 struct hbq_dmabuf *dmabuf)
14249 {
14250 struct fc_frame_header *new_hdr;
14251 struct fc_frame_header *temp_hdr;
14252 struct lpfc_dmabuf *d_buf, *n_buf, *h_buf;
14253 struct hbq_dmabuf *seq_dmabuf = NULL;
14254
14255 /* Use the hdr_buf to find the sequence that matches this frame */
14256 INIT_LIST_HEAD(&dmabuf->dbuf.list);
14257 INIT_LIST_HEAD(&dmabuf->hbuf.list);
14258 new_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14259 list_for_each_entry(h_buf, &vport->rcv_buffer_list, list) {
14260 temp_hdr = (struct fc_frame_header *)h_buf->virt;
14261 if ((temp_hdr->fh_seq_id != new_hdr->fh_seq_id) ||
14262 (temp_hdr->fh_ox_id != new_hdr->fh_ox_id) ||
14263 (memcmp(&temp_hdr->fh_s_id, &new_hdr->fh_s_id, 3)))
14264 continue;
14265 /* found a pending sequence that matches this frame */
14266 seq_dmabuf = container_of(h_buf, struct hbq_dmabuf, hbuf);
14267 break;
14268 }
14269
14270 /* Free up all the frames from the partially assembled sequence */
14271 if (seq_dmabuf) {
14272 list_for_each_entry_safe(d_buf, n_buf,
14273 &seq_dmabuf->dbuf.list, list) {
14274 list_del_init(&d_buf->list);
14275 lpfc_in_buf_free(vport->phba, d_buf);
14276 }
14277 return true;
14278 }
14279 return false;
14280 }
14281
14282 /**
14283 * lpfc_sli4_abort_ulp_seq - Abort assembled unsol sequence from ulp
14284 * @vport: pointer to a vitural port
14285 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14286 *
14287 * This function tries to abort from the assembed sequence from upper level
14288 * protocol, described by the information from basic abbort @dmabuf. It
14289 * checks to see whether such pending context exists at upper level protocol.
14290 * If so, it shall clean up the pending context.
14291 *
14292 * Return
14293 * true -- if there is matching pending context of the sequence cleaned
14294 * at ulp;
14295 * false -- if there is no matching pending context of the sequence present
14296 * at ulp.
14297 **/
14298 static bool
14299 lpfc_sli4_abort_ulp_seq(struct lpfc_vport *vport, struct hbq_dmabuf *dmabuf)
14300 {
14301 struct lpfc_hba *phba = vport->phba;
14302 int handled;
14303
14304 /* Accepting abort at ulp with SLI4 only */
14305 if (phba->sli_rev < LPFC_SLI_REV4)
14306 return false;
14307
14308 /* Register all caring upper level protocols to attend abort */
14309 handled = lpfc_ct_handle_unsol_abort(phba, dmabuf);
14310 if (handled)
14311 return true;
14312
14313 return false;
14314 }
14315
14316 /**
14317 * lpfc_sli4_seq_abort_rsp_cmpl - BLS ABORT RSP seq abort iocb complete handler
14318 * @phba: Pointer to HBA context object.
14319 * @cmd_iocbq: pointer to the command iocbq structure.
14320 * @rsp_iocbq: pointer to the response iocbq structure.
14321 *
14322 * This function handles the sequence abort response iocb command complete
14323 * event. It properly releases the memory allocated to the sequence abort
14324 * accept iocb.
14325 **/
14326 static void
14327 lpfc_sli4_seq_abort_rsp_cmpl(struct lpfc_hba *phba,
14328 struct lpfc_iocbq *cmd_iocbq,
14329 struct lpfc_iocbq *rsp_iocbq)
14330 {
14331 struct lpfc_nodelist *ndlp;
14332
14333 if (cmd_iocbq) {
14334 ndlp = (struct lpfc_nodelist *)cmd_iocbq->context1;
14335 lpfc_nlp_put(ndlp);
14336 lpfc_nlp_not_used(ndlp);
14337 lpfc_sli_release_iocbq(phba, cmd_iocbq);
14338 }
14339
14340 /* Failure means BLS ABORT RSP did not get delivered to remote node*/
14341 if (rsp_iocbq && rsp_iocbq->iocb.ulpStatus)
14342 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14343 "3154 BLS ABORT RSP failed, data: x%x/x%x\n",
14344 rsp_iocbq->iocb.ulpStatus,
14345 rsp_iocbq->iocb.un.ulpWord[4]);
14346 }
14347
14348 /**
14349 * lpfc_sli4_xri_inrange - check xri is in range of xris owned by driver.
14350 * @phba: Pointer to HBA context object.
14351 * @xri: xri id in transaction.
14352 *
14353 * This function validates the xri maps to the known range of XRIs allocated an
14354 * used by the driver.
14355 **/
14356 uint16_t
14357 lpfc_sli4_xri_inrange(struct lpfc_hba *phba,
14358 uint16_t xri)
14359 {
14360 int i;
14361
14362 for (i = 0; i < phba->sli4_hba.max_cfg_param.max_xri; i++) {
14363 if (xri == phba->sli4_hba.xri_ids[i])
14364 return i;
14365 }
14366 return NO_XRI;
14367 }
14368
14369 /**
14370 * lpfc_sli4_seq_abort_rsp - bls rsp to sequence abort
14371 * @phba: Pointer to HBA context object.
14372 * @fc_hdr: pointer to a FC frame header.
14373 *
14374 * This function sends a basic response to a previous unsol sequence abort
14375 * event after aborting the sequence handling.
14376 **/
14377 static void
14378 lpfc_sli4_seq_abort_rsp(struct lpfc_vport *vport,
14379 struct fc_frame_header *fc_hdr, bool aborted)
14380 {
14381 struct lpfc_hba *phba = vport->phba;
14382 struct lpfc_iocbq *ctiocb = NULL;
14383 struct lpfc_nodelist *ndlp;
14384 uint16_t oxid, rxid, xri, lxri;
14385 uint32_t sid, fctl;
14386 IOCB_t *icmd;
14387 int rc;
14388
14389 if (!lpfc_is_link_up(phba))
14390 return;
14391
14392 sid = sli4_sid_from_fc_hdr(fc_hdr);
14393 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
14394 rxid = be16_to_cpu(fc_hdr->fh_rx_id);
14395
14396 ndlp = lpfc_findnode_did(vport, sid);
14397 if (!ndlp) {
14398 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
14399 if (!ndlp) {
14400 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14401 "1268 Failed to allocate ndlp for "
14402 "oxid:x%x SID:x%x\n", oxid, sid);
14403 return;
14404 }
14405 lpfc_nlp_init(vport, ndlp, sid);
14406 /* Put ndlp onto pport node list */
14407 lpfc_enqueue_node(vport, ndlp);
14408 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
14409 /* re-setup ndlp without removing from node list */
14410 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
14411 if (!ndlp) {
14412 lpfc_printf_vlog(vport, KERN_WARNING, LOG_ELS,
14413 "3275 Failed to active ndlp found "
14414 "for oxid:x%x SID:x%x\n", oxid, sid);
14415 return;
14416 }
14417 }
14418
14419 /* Allocate buffer for rsp iocb */
14420 ctiocb = lpfc_sli_get_iocbq(phba);
14421 if (!ctiocb)
14422 return;
14423
14424 /* Extract the F_CTL field from FC_HDR */
14425 fctl = sli4_fctl_from_fc_hdr(fc_hdr);
14426
14427 icmd = &ctiocb->iocb;
14428 icmd->un.xseq64.bdl.bdeSize = 0;
14429 icmd->un.xseq64.bdl.ulpIoTag32 = 0;
14430 icmd->un.xseq64.w5.hcsw.Dfctl = 0;
14431 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_ACC;
14432 icmd->un.xseq64.w5.hcsw.Type = FC_TYPE_BLS;
14433
14434 /* Fill in the rest of iocb fields */
14435 icmd->ulpCommand = CMD_XMIT_BLS_RSP64_CX;
14436 icmd->ulpBdeCount = 0;
14437 icmd->ulpLe = 1;
14438 icmd->ulpClass = CLASS3;
14439 icmd->ulpContext = phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
14440 ctiocb->context1 = lpfc_nlp_get(ndlp);
14441
14442 ctiocb->iocb_cmpl = NULL;
14443 ctiocb->vport = phba->pport;
14444 ctiocb->iocb_cmpl = lpfc_sli4_seq_abort_rsp_cmpl;
14445 ctiocb->sli4_lxritag = NO_XRI;
14446 ctiocb->sli4_xritag = NO_XRI;
14447
14448 if (fctl & FC_FC_EX_CTX)
14449 /* Exchange responder sent the abort so we
14450 * own the oxid.
14451 */
14452 xri = oxid;
14453 else
14454 xri = rxid;
14455 lxri = lpfc_sli4_xri_inrange(phba, xri);
14456 if (lxri != NO_XRI)
14457 lpfc_set_rrq_active(phba, ndlp, lxri,
14458 (xri == oxid) ? rxid : oxid, 0);
14459 /* For BA_ABTS from exchange responder, if the logical xri with
14460 * the oxid maps to the FCP XRI range, the port no longer has
14461 * that exchange context, send a BLS_RJT. Override the IOCB for
14462 * a BA_RJT.
14463 */
14464 if ((fctl & FC_FC_EX_CTX) &&
14465 (lxri > lpfc_sli4_get_els_iocb_cnt(phba))) {
14466 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14467 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14468 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14469 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14470 }
14471
14472 /* If BA_ABTS failed to abort a partially assembled receive sequence,
14473 * the driver no longer has that exchange, send a BLS_RJT. Override
14474 * the IOCB for a BA_RJT.
14475 */
14476 if (aborted == false) {
14477 icmd->un.xseq64.w5.hcsw.Rctl = FC_RCTL_BA_RJT;
14478 bf_set(lpfc_vndr_code, &icmd->un.bls_rsp, 0);
14479 bf_set(lpfc_rsn_expln, &icmd->un.bls_rsp, FC_BA_RJT_INV_XID);
14480 bf_set(lpfc_rsn_code, &icmd->un.bls_rsp, FC_BA_RJT_UNABLE);
14481 }
14482
14483 if (fctl & FC_FC_EX_CTX) {
14484 /* ABTS sent by responder to CT exchange, construction
14485 * of BA_ACC will use OX_ID from ABTS for the XRI_TAG
14486 * field and RX_ID from ABTS for RX_ID field.
14487 */
14488 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_RSP);
14489 } else {
14490 /* ABTS sent by initiator to CT exchange, construction
14491 * of BA_ACC will need to allocate a new XRI as for the
14492 * XRI_TAG field.
14493 */
14494 bf_set(lpfc_abts_orig, &icmd->un.bls_rsp, LPFC_ABTS_UNSOL_INT);
14495 }
14496 bf_set(lpfc_abts_rxid, &icmd->un.bls_rsp, rxid);
14497 bf_set(lpfc_abts_oxid, &icmd->un.bls_rsp, oxid);
14498
14499 /* Xmit CT abts response on exchange <xid> */
14500 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
14501 "1200 Send BLS cmd x%x on oxid x%x Data: x%x\n",
14502 icmd->un.xseq64.w5.hcsw.Rctl, oxid, phba->link_state);
14503
14504 rc = lpfc_sli_issue_iocb(phba, LPFC_ELS_RING, ctiocb, 0);
14505 if (rc == IOCB_ERROR) {
14506 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
14507 "2925 Failed to issue CT ABTS RSP x%x on "
14508 "xri x%x, Data x%x\n",
14509 icmd->un.xseq64.w5.hcsw.Rctl, oxid,
14510 phba->link_state);
14511 lpfc_nlp_put(ndlp);
14512 ctiocb->context1 = NULL;
14513 lpfc_sli_release_iocbq(phba, ctiocb);
14514 }
14515 }
14516
14517 /**
14518 * lpfc_sli4_handle_unsol_abort - Handle sli-4 unsolicited abort event
14519 * @vport: Pointer to the vport on which this sequence was received
14520 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14521 *
14522 * This function handles an SLI-4 unsolicited abort event. If the unsolicited
14523 * receive sequence is only partially assembed by the driver, it shall abort
14524 * the partially assembled frames for the sequence. Otherwise, if the
14525 * unsolicited receive sequence has been completely assembled and passed to
14526 * the Upper Layer Protocol (UPL), it then mark the per oxid status for the
14527 * unsolicited sequence has been aborted. After that, it will issue a basic
14528 * accept to accept the abort.
14529 **/
14530 void
14531 lpfc_sli4_handle_unsol_abort(struct lpfc_vport *vport,
14532 struct hbq_dmabuf *dmabuf)
14533 {
14534 struct lpfc_hba *phba = vport->phba;
14535 struct fc_frame_header fc_hdr;
14536 uint32_t fctl;
14537 bool aborted;
14538
14539 /* Make a copy of fc_hdr before the dmabuf being released */
14540 memcpy(&fc_hdr, dmabuf->hbuf.virt, sizeof(struct fc_frame_header));
14541 fctl = sli4_fctl_from_fc_hdr(&fc_hdr);
14542
14543 if (fctl & FC_FC_EX_CTX) {
14544 /* ABTS by responder to exchange, no cleanup needed */
14545 aborted = true;
14546 } else {
14547 /* ABTS by initiator to exchange, need to do cleanup */
14548 aborted = lpfc_sli4_abort_partial_seq(vport, dmabuf);
14549 if (aborted == false)
14550 aborted = lpfc_sli4_abort_ulp_seq(vport, dmabuf);
14551 }
14552 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14553
14554 /* Respond with BA_ACC or BA_RJT accordingly */
14555 lpfc_sli4_seq_abort_rsp(vport, &fc_hdr, aborted);
14556 }
14557
14558 /**
14559 * lpfc_seq_complete - Indicates if a sequence is complete
14560 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14561 *
14562 * This function checks the sequence, starting with the frame described by
14563 * @dmabuf, to see if all the frames associated with this sequence are present.
14564 * the frames associated with this sequence are linked to the @dmabuf using the
14565 * dbuf list. This function looks for two major things. 1) That the first frame
14566 * has a sequence count of zero. 2) There is a frame with last frame of sequence
14567 * set. 3) That there are no holes in the sequence count. The function will
14568 * return 1 when the sequence is complete, otherwise it will return 0.
14569 **/
14570 static int
14571 lpfc_seq_complete(struct hbq_dmabuf *dmabuf)
14572 {
14573 struct fc_frame_header *hdr;
14574 struct lpfc_dmabuf *d_buf;
14575 struct hbq_dmabuf *seq_dmabuf;
14576 uint32_t fctl;
14577 int seq_count = 0;
14578
14579 hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14580 /* make sure first fame of sequence has a sequence count of zero */
14581 if (hdr->fh_seq_cnt != seq_count)
14582 return 0;
14583 fctl = (hdr->fh_f_ctl[0] << 16 |
14584 hdr->fh_f_ctl[1] << 8 |
14585 hdr->fh_f_ctl[2]);
14586 /* If last frame of sequence we can return success. */
14587 if (fctl & FC_FC_END_SEQ)
14588 return 1;
14589 list_for_each_entry(d_buf, &dmabuf->dbuf.list, list) {
14590 seq_dmabuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14591 hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14592 /* If there is a hole in the sequence count then fail. */
14593 if (++seq_count != be16_to_cpu(hdr->fh_seq_cnt))
14594 return 0;
14595 fctl = (hdr->fh_f_ctl[0] << 16 |
14596 hdr->fh_f_ctl[1] << 8 |
14597 hdr->fh_f_ctl[2]);
14598 /* If last frame of sequence we can return success. */
14599 if (fctl & FC_FC_END_SEQ)
14600 return 1;
14601 }
14602 return 0;
14603 }
14604
14605 /**
14606 * lpfc_prep_seq - Prep sequence for ULP processing
14607 * @vport: Pointer to the vport on which this sequence was received
14608 * @dmabuf: pointer to a dmabuf that describes the FC sequence
14609 *
14610 * This function takes a sequence, described by a list of frames, and creates
14611 * a list of iocbq structures to describe the sequence. This iocbq list will be
14612 * used to issue to the generic unsolicited sequence handler. This routine
14613 * returns a pointer to the first iocbq in the list. If the function is unable
14614 * to allocate an iocbq then it throw out the received frames that were not
14615 * able to be described and return a pointer to the first iocbq. If unable to
14616 * allocate any iocbqs (including the first) this function will return NULL.
14617 **/
14618 static struct lpfc_iocbq *
14619 lpfc_prep_seq(struct lpfc_vport *vport, struct hbq_dmabuf *seq_dmabuf)
14620 {
14621 struct hbq_dmabuf *hbq_buf;
14622 struct lpfc_dmabuf *d_buf, *n_buf;
14623 struct lpfc_iocbq *first_iocbq, *iocbq;
14624 struct fc_frame_header *fc_hdr;
14625 uint32_t sid;
14626 uint32_t len, tot_len;
14627 struct ulp_bde64 *pbde;
14628
14629 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14630 /* remove from receive buffer list */
14631 list_del_init(&seq_dmabuf->hbuf.list);
14632 lpfc_update_rcv_time_stamp(vport);
14633 /* get the Remote Port's SID */
14634 sid = sli4_sid_from_fc_hdr(fc_hdr);
14635 tot_len = 0;
14636 /* Get an iocbq struct to fill in. */
14637 first_iocbq = lpfc_sli_get_iocbq(vport->phba);
14638 if (first_iocbq) {
14639 /* Initialize the first IOCB. */
14640 first_iocbq->iocb.unsli3.rcvsli3.acc_len = 0;
14641 first_iocbq->iocb.ulpStatus = IOSTAT_SUCCESS;
14642
14643 /* Check FC Header to see what TYPE of frame we are rcv'ing */
14644 if (sli4_type_from_fc_hdr(fc_hdr) == FC_TYPE_ELS) {
14645 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_ELS64_CX;
14646 first_iocbq->iocb.un.rcvels.parmRo =
14647 sli4_did_from_fc_hdr(fc_hdr);
14648 first_iocbq->iocb.ulpPU = PARM_NPIV_DID;
14649 } else
14650 first_iocbq->iocb.ulpCommand = CMD_IOCB_RCV_SEQ64_CX;
14651 first_iocbq->iocb.ulpContext = NO_XRI;
14652 first_iocbq->iocb.unsli3.rcvsli3.ox_id =
14653 be16_to_cpu(fc_hdr->fh_ox_id);
14654 /* iocbq is prepped for internal consumption. Physical vpi. */
14655 first_iocbq->iocb.unsli3.rcvsli3.vpi =
14656 vport->phba->vpi_ids[vport->vpi];
14657 /* put the first buffer into the first IOCBq */
14658 first_iocbq->context2 = &seq_dmabuf->dbuf;
14659 first_iocbq->context3 = NULL;
14660 first_iocbq->iocb.ulpBdeCount = 1;
14661 first_iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14662 LPFC_DATA_BUF_SIZE;
14663 first_iocbq->iocb.un.rcvels.remoteID = sid;
14664 tot_len = bf_get(lpfc_rcqe_length,
14665 &seq_dmabuf->cq_event.cqe.rcqe_cmpl);
14666 first_iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14667 }
14668 iocbq = first_iocbq;
14669 /*
14670 * Each IOCBq can have two Buffers assigned, so go through the list
14671 * of buffers for this sequence and save two buffers in each IOCBq
14672 */
14673 list_for_each_entry_safe(d_buf, n_buf, &seq_dmabuf->dbuf.list, list) {
14674 if (!iocbq) {
14675 lpfc_in_buf_free(vport->phba, d_buf);
14676 continue;
14677 }
14678 if (!iocbq->context3) {
14679 iocbq->context3 = d_buf;
14680 iocbq->iocb.ulpBdeCount++;
14681 pbde = (struct ulp_bde64 *)
14682 &iocbq->iocb.unsli3.sli3Words[4];
14683 pbde->tus.f.bdeSize = LPFC_DATA_BUF_SIZE;
14684
14685 /* We need to get the size out of the right CQE */
14686 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14687 len = bf_get(lpfc_rcqe_length,
14688 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14689 iocbq->iocb.unsli3.rcvsli3.acc_len += len;
14690 tot_len += len;
14691 } else {
14692 iocbq = lpfc_sli_get_iocbq(vport->phba);
14693 if (!iocbq) {
14694 if (first_iocbq) {
14695 first_iocbq->iocb.ulpStatus =
14696 IOSTAT_FCP_RSP_ERROR;
14697 first_iocbq->iocb.un.ulpWord[4] =
14698 IOERR_NO_RESOURCES;
14699 }
14700 lpfc_in_buf_free(vport->phba, d_buf);
14701 continue;
14702 }
14703 iocbq->context2 = d_buf;
14704 iocbq->context3 = NULL;
14705 iocbq->iocb.ulpBdeCount = 1;
14706 iocbq->iocb.un.cont64[0].tus.f.bdeSize =
14707 LPFC_DATA_BUF_SIZE;
14708
14709 /* We need to get the size out of the right CQE */
14710 hbq_buf = container_of(d_buf, struct hbq_dmabuf, dbuf);
14711 len = bf_get(lpfc_rcqe_length,
14712 &hbq_buf->cq_event.cqe.rcqe_cmpl);
14713 tot_len += len;
14714 iocbq->iocb.unsli3.rcvsli3.acc_len = tot_len;
14715
14716 iocbq->iocb.un.rcvels.remoteID = sid;
14717 list_add_tail(&iocbq->list, &first_iocbq->list);
14718 }
14719 }
14720 return first_iocbq;
14721 }
14722
14723 static void
14724 lpfc_sli4_send_seq_to_ulp(struct lpfc_vport *vport,
14725 struct hbq_dmabuf *seq_dmabuf)
14726 {
14727 struct fc_frame_header *fc_hdr;
14728 struct lpfc_iocbq *iocbq, *curr_iocb, *next_iocb;
14729 struct lpfc_hba *phba = vport->phba;
14730
14731 fc_hdr = (struct fc_frame_header *)seq_dmabuf->hbuf.virt;
14732 iocbq = lpfc_prep_seq(vport, seq_dmabuf);
14733 if (!iocbq) {
14734 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14735 "2707 Ring %d handler: Failed to allocate "
14736 "iocb Rctl x%x Type x%x received\n",
14737 LPFC_ELS_RING,
14738 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14739 return;
14740 }
14741 if (!lpfc_complete_unsol_iocb(phba,
14742 &phba->sli.ring[LPFC_ELS_RING],
14743 iocbq, fc_hdr->fh_r_ctl,
14744 fc_hdr->fh_type))
14745 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14746 "2540 Ring %d handler: unexpected Rctl "
14747 "x%x Type x%x received\n",
14748 LPFC_ELS_RING,
14749 fc_hdr->fh_r_ctl, fc_hdr->fh_type);
14750
14751 /* Free iocb created in lpfc_prep_seq */
14752 list_for_each_entry_safe(curr_iocb, next_iocb,
14753 &iocbq->list, list) {
14754 list_del_init(&curr_iocb->list);
14755 lpfc_sli_release_iocbq(phba, curr_iocb);
14756 }
14757 lpfc_sli_release_iocbq(phba, iocbq);
14758 }
14759
14760 /**
14761 * lpfc_sli4_handle_received_buffer - Handle received buffers from firmware
14762 * @phba: Pointer to HBA context object.
14763 *
14764 * This function is called with no lock held. This function processes all
14765 * the received buffers and gives it to upper layers when a received buffer
14766 * indicates that it is the final frame in the sequence. The interrupt
14767 * service routine processes received buffers at interrupt contexts and adds
14768 * received dma buffers to the rb_pend_list queue and signals the worker thread.
14769 * Worker thread calls lpfc_sli4_handle_received_buffer, which will call the
14770 * appropriate receive function when the final frame in a sequence is received.
14771 **/
14772 void
14773 lpfc_sli4_handle_received_buffer(struct lpfc_hba *phba,
14774 struct hbq_dmabuf *dmabuf)
14775 {
14776 struct hbq_dmabuf *seq_dmabuf;
14777 struct fc_frame_header *fc_hdr;
14778 struct lpfc_vport *vport;
14779 uint32_t fcfi;
14780 uint32_t did;
14781
14782 /* Process each received buffer */
14783 fc_hdr = (struct fc_frame_header *)dmabuf->hbuf.virt;
14784 /* check to see if this a valid type of frame */
14785 if (lpfc_fc_frame_check(phba, fc_hdr)) {
14786 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14787 return;
14788 }
14789 if ((bf_get(lpfc_cqe_code,
14790 &dmabuf->cq_event.cqe.rcqe_cmpl) == CQE_CODE_RECEIVE_V1))
14791 fcfi = bf_get(lpfc_rcqe_fcf_id_v1,
14792 &dmabuf->cq_event.cqe.rcqe_cmpl);
14793 else
14794 fcfi = bf_get(lpfc_rcqe_fcf_id,
14795 &dmabuf->cq_event.cqe.rcqe_cmpl);
14796
14797 vport = lpfc_fc_frame_to_vport(phba, fc_hdr, fcfi);
14798 if (!vport) {
14799 /* throw out the frame */
14800 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14801 return;
14802 }
14803
14804 /* d_id this frame is directed to */
14805 did = sli4_did_from_fc_hdr(fc_hdr);
14806
14807 /* vport is registered unless we rcv a FLOGI directed to Fabric_DID */
14808 if (!(vport->vpi_state & LPFC_VPI_REGISTERED) &&
14809 (did != Fabric_DID)) {
14810 /*
14811 * Throw out the frame if we are not pt2pt.
14812 * The pt2pt protocol allows for discovery frames
14813 * to be received without a registered VPI.
14814 */
14815 if (!(vport->fc_flag & FC_PT2PT) ||
14816 (phba->link_state == LPFC_HBA_READY)) {
14817 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14818 return;
14819 }
14820 }
14821
14822 /* Handle the basic abort sequence (BA_ABTS) event */
14823 if (fc_hdr->fh_r_ctl == FC_RCTL_BA_ABTS) {
14824 lpfc_sli4_handle_unsol_abort(vport, dmabuf);
14825 return;
14826 }
14827
14828 /* Link this frame */
14829 seq_dmabuf = lpfc_fc_frame_add(vport, dmabuf);
14830 if (!seq_dmabuf) {
14831 /* unable to add frame to vport - throw it out */
14832 lpfc_in_buf_free(phba, &dmabuf->dbuf);
14833 return;
14834 }
14835 /* If not last frame in sequence continue processing frames. */
14836 if (!lpfc_seq_complete(seq_dmabuf))
14837 return;
14838
14839 /* Send the complete sequence to the upper layer protocol */
14840 lpfc_sli4_send_seq_to_ulp(vport, seq_dmabuf);
14841 }
14842
14843 /**
14844 * lpfc_sli4_post_all_rpi_hdrs - Post the rpi header memory region to the port
14845 * @phba: pointer to lpfc hba data structure.
14846 *
14847 * This routine is invoked to post rpi header templates to the
14848 * HBA consistent with the SLI-4 interface spec. This routine
14849 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14850 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14851 *
14852 * This routine does not require any locks. It's usage is expected
14853 * to be driver load or reset recovery when the driver is
14854 * sequential.
14855 *
14856 * Return codes
14857 * 0 - successful
14858 * -EIO - The mailbox failed to complete successfully.
14859 * When this error occurs, the driver is not guaranteed
14860 * to have any rpi regions posted to the device and
14861 * must either attempt to repost the regions or take a
14862 * fatal error.
14863 **/
14864 int
14865 lpfc_sli4_post_all_rpi_hdrs(struct lpfc_hba *phba)
14866 {
14867 struct lpfc_rpi_hdr *rpi_page;
14868 uint32_t rc = 0;
14869 uint16_t lrpi = 0;
14870
14871 /* SLI4 ports that support extents do not require RPI headers. */
14872 if (!phba->sli4_hba.rpi_hdrs_in_use)
14873 goto exit;
14874 if (phba->sli4_hba.extents_in_use)
14875 return -EIO;
14876
14877 list_for_each_entry(rpi_page, &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
14878 /*
14879 * Assign the rpi headers a physical rpi only if the driver
14880 * has not initialized those resources. A port reset only
14881 * needs the headers posted.
14882 */
14883 if (bf_get(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags) !=
14884 LPFC_RPI_RSRC_RDY)
14885 rpi_page->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
14886
14887 rc = lpfc_sli4_post_rpi_hdr(phba, rpi_page);
14888 if (rc != MBX_SUCCESS) {
14889 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14890 "2008 Error %d posting all rpi "
14891 "headers\n", rc);
14892 rc = -EIO;
14893 break;
14894 }
14895 }
14896
14897 exit:
14898 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags,
14899 LPFC_RPI_RSRC_RDY);
14900 return rc;
14901 }
14902
14903 /**
14904 * lpfc_sli4_post_rpi_hdr - Post an rpi header memory region to the port
14905 * @phba: pointer to lpfc hba data structure.
14906 * @rpi_page: pointer to the rpi memory region.
14907 *
14908 * This routine is invoked to post a single rpi header to the
14909 * HBA consistent with the SLI-4 interface spec. This memory region
14910 * maps up to 64 rpi context regions.
14911 *
14912 * Return codes
14913 * 0 - successful
14914 * -ENOMEM - No available memory
14915 * -EIO - The mailbox failed to complete successfully.
14916 **/
14917 int
14918 lpfc_sli4_post_rpi_hdr(struct lpfc_hba *phba, struct lpfc_rpi_hdr *rpi_page)
14919 {
14920 LPFC_MBOXQ_t *mboxq;
14921 struct lpfc_mbx_post_hdr_tmpl *hdr_tmpl;
14922 uint32_t rc = 0;
14923 uint32_t shdr_status, shdr_add_status;
14924 union lpfc_sli4_cfg_shdr *shdr;
14925
14926 /* SLI4 ports that support extents do not require RPI headers. */
14927 if (!phba->sli4_hba.rpi_hdrs_in_use)
14928 return rc;
14929 if (phba->sli4_hba.extents_in_use)
14930 return -EIO;
14931
14932 /* The port is notified of the header region via a mailbox command. */
14933 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
14934 if (!mboxq) {
14935 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
14936 "2001 Unable to allocate memory for issuing "
14937 "SLI_CONFIG_SPECIAL mailbox command\n");
14938 return -ENOMEM;
14939 }
14940
14941 /* Post all rpi memory regions to the port. */
14942 hdr_tmpl = &mboxq->u.mqe.un.hdr_tmpl;
14943 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
14944 LPFC_MBOX_OPCODE_FCOE_POST_HDR_TEMPLATE,
14945 sizeof(struct lpfc_mbx_post_hdr_tmpl) -
14946 sizeof(struct lpfc_sli4_cfg_mhdr),
14947 LPFC_SLI4_MBX_EMBED);
14948
14949
14950 /* Post the physical rpi to the port for this rpi header. */
14951 bf_set(lpfc_mbx_post_hdr_tmpl_rpi_offset, hdr_tmpl,
14952 rpi_page->start_rpi);
14953 bf_set(lpfc_mbx_post_hdr_tmpl_page_cnt,
14954 hdr_tmpl, rpi_page->page_count);
14955
14956 hdr_tmpl->rpi_paddr_lo = putPaddrLow(rpi_page->dmabuf->phys);
14957 hdr_tmpl->rpi_paddr_hi = putPaddrHigh(rpi_page->dmabuf->phys);
14958 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
14959 shdr = (union lpfc_sli4_cfg_shdr *) &hdr_tmpl->header.cfg_shdr;
14960 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
14961 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
14962 if (rc != MBX_TIMEOUT)
14963 mempool_free(mboxq, phba->mbox_mem_pool);
14964 if (shdr_status || shdr_add_status || rc) {
14965 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
14966 "2514 POST_RPI_HDR mailbox failed with "
14967 "status x%x add_status x%x, mbx status x%x\n",
14968 shdr_status, shdr_add_status, rc);
14969 rc = -ENXIO;
14970 }
14971 return rc;
14972 }
14973
14974 /**
14975 * lpfc_sli4_alloc_rpi - Get an available rpi in the device's range
14976 * @phba: pointer to lpfc hba data structure.
14977 *
14978 * This routine is invoked to post rpi header templates to the
14979 * HBA consistent with the SLI-4 interface spec. This routine
14980 * posts a SLI4_PAGE_SIZE memory region to the port to hold up to
14981 * SLI4_PAGE_SIZE modulo 64 rpi context headers.
14982 *
14983 * Returns
14984 * A nonzero rpi defined as rpi_base <= rpi < max_rpi if successful
14985 * LPFC_RPI_ALLOC_ERROR if no rpis are available.
14986 **/
14987 int
14988 lpfc_sli4_alloc_rpi(struct lpfc_hba *phba)
14989 {
14990 unsigned long rpi;
14991 uint16_t max_rpi, rpi_limit;
14992 uint16_t rpi_remaining, lrpi = 0;
14993 struct lpfc_rpi_hdr *rpi_hdr;
14994
14995 max_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
14996 rpi_limit = phba->sli4_hba.next_rpi;
14997
14998 /*
14999 * Fetch the next logical rpi. Because this index is logical,
15000 * the driver starts at 0 each time.
15001 */
15002 spin_lock_irq(&phba->hbalock);
15003 rpi = find_next_zero_bit(phba->sli4_hba.rpi_bmask, rpi_limit, 0);
15004 if (rpi >= rpi_limit)
15005 rpi = LPFC_RPI_ALLOC_ERROR;
15006 else {
15007 set_bit(rpi, phba->sli4_hba.rpi_bmask);
15008 phba->sli4_hba.max_cfg_param.rpi_used++;
15009 phba->sli4_hba.rpi_count++;
15010 }
15011
15012 /*
15013 * Don't try to allocate more rpi header regions if the device limit
15014 * has been exhausted.
15015 */
15016 if ((rpi == LPFC_RPI_ALLOC_ERROR) &&
15017 (phba->sli4_hba.rpi_count >= max_rpi)) {
15018 spin_unlock_irq(&phba->hbalock);
15019 return rpi;
15020 }
15021
15022 /*
15023 * RPI header postings are not required for SLI4 ports capable of
15024 * extents.
15025 */
15026 if (!phba->sli4_hba.rpi_hdrs_in_use) {
15027 spin_unlock_irq(&phba->hbalock);
15028 return rpi;
15029 }
15030
15031 /*
15032 * If the driver is running low on rpi resources, allocate another
15033 * page now. Note that the next_rpi value is used because
15034 * it represents how many are actually in use whereas max_rpi notes
15035 * how many are supported max by the device.
15036 */
15037 rpi_remaining = phba->sli4_hba.next_rpi - phba->sli4_hba.rpi_count;
15038 spin_unlock_irq(&phba->hbalock);
15039 if (rpi_remaining < LPFC_RPI_LOW_WATER_MARK) {
15040 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
15041 if (!rpi_hdr) {
15042 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15043 "2002 Error Could not grow rpi "
15044 "count\n");
15045 } else {
15046 lrpi = rpi_hdr->start_rpi;
15047 rpi_hdr->start_rpi = phba->sli4_hba.rpi_ids[lrpi];
15048 lpfc_sli4_post_rpi_hdr(phba, rpi_hdr);
15049 }
15050 }
15051
15052 return rpi;
15053 }
15054
15055 /**
15056 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15057 * @phba: pointer to lpfc hba data structure.
15058 *
15059 * This routine is invoked to release an rpi to the pool of
15060 * available rpis maintained by the driver.
15061 **/
15062 void
15063 __lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15064 {
15065 if (test_and_clear_bit(rpi, phba->sli4_hba.rpi_bmask)) {
15066 phba->sli4_hba.rpi_count--;
15067 phba->sli4_hba.max_cfg_param.rpi_used--;
15068 }
15069 }
15070
15071 /**
15072 * lpfc_sli4_free_rpi - Release an rpi for reuse.
15073 * @phba: pointer to lpfc hba data structure.
15074 *
15075 * This routine is invoked to release an rpi to the pool of
15076 * available rpis maintained by the driver.
15077 **/
15078 void
15079 lpfc_sli4_free_rpi(struct lpfc_hba *phba, int rpi)
15080 {
15081 spin_lock_irq(&phba->hbalock);
15082 __lpfc_sli4_free_rpi(phba, rpi);
15083 spin_unlock_irq(&phba->hbalock);
15084 }
15085
15086 /**
15087 * lpfc_sli4_remove_rpis - Remove the rpi bitmask region
15088 * @phba: pointer to lpfc hba data structure.
15089 *
15090 * This routine is invoked to remove the memory region that
15091 * provided rpi via a bitmask.
15092 **/
15093 void
15094 lpfc_sli4_remove_rpis(struct lpfc_hba *phba)
15095 {
15096 kfree(phba->sli4_hba.rpi_bmask);
15097 kfree(phba->sli4_hba.rpi_ids);
15098 bf_set(lpfc_rpi_rsrc_rdy, &phba->sli4_hba.sli4_flags, 0);
15099 }
15100
15101 /**
15102 * lpfc_sli4_resume_rpi - Remove the rpi bitmask region
15103 * @phba: pointer to lpfc hba data structure.
15104 *
15105 * This routine is invoked to remove the memory region that
15106 * provided rpi via a bitmask.
15107 **/
15108 int
15109 lpfc_sli4_resume_rpi(struct lpfc_nodelist *ndlp,
15110 void (*cmpl)(struct lpfc_hba *, LPFC_MBOXQ_t *), void *arg)
15111 {
15112 LPFC_MBOXQ_t *mboxq;
15113 struct lpfc_hba *phba = ndlp->phba;
15114 int rc;
15115
15116 /* The port is notified of the header region via a mailbox command. */
15117 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15118 if (!mboxq)
15119 return -ENOMEM;
15120
15121 /* Post all rpi memory regions to the port. */
15122 lpfc_resume_rpi(mboxq, ndlp);
15123 if (cmpl) {
15124 mboxq->mbox_cmpl = cmpl;
15125 mboxq->context1 = arg;
15126 mboxq->context2 = ndlp;
15127 } else
15128 mboxq->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
15129 mboxq->vport = ndlp->vport;
15130 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15131 if (rc == MBX_NOT_FINISHED) {
15132 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15133 "2010 Resume RPI Mailbox failed "
15134 "status %d, mbxStatus x%x\n", rc,
15135 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15136 mempool_free(mboxq, phba->mbox_mem_pool);
15137 return -EIO;
15138 }
15139 return 0;
15140 }
15141
15142 /**
15143 * lpfc_sli4_init_vpi - Initialize a vpi with the port
15144 * @vport: Pointer to the vport for which the vpi is being initialized
15145 *
15146 * This routine is invoked to activate a vpi with the port.
15147 *
15148 * Returns:
15149 * 0 success
15150 * -Evalue otherwise
15151 **/
15152 int
15153 lpfc_sli4_init_vpi(struct lpfc_vport *vport)
15154 {
15155 LPFC_MBOXQ_t *mboxq;
15156 int rc = 0;
15157 int retval = MBX_SUCCESS;
15158 uint32_t mbox_tmo;
15159 struct lpfc_hba *phba = vport->phba;
15160 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15161 if (!mboxq)
15162 return -ENOMEM;
15163 lpfc_init_vpi(phba, mboxq, vport->vpi);
15164 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
15165 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
15166 if (rc != MBX_SUCCESS) {
15167 lpfc_printf_vlog(vport, KERN_ERR, LOG_SLI,
15168 "2022 INIT VPI Mailbox failed "
15169 "status %d, mbxStatus x%x\n", rc,
15170 bf_get(lpfc_mqe_status, &mboxq->u.mqe));
15171 retval = -EIO;
15172 }
15173 if (rc != MBX_TIMEOUT)
15174 mempool_free(mboxq, vport->phba->mbox_mem_pool);
15175
15176 return retval;
15177 }
15178
15179 /**
15180 * lpfc_mbx_cmpl_add_fcf_record - add fcf mbox completion handler.
15181 * @phba: pointer to lpfc hba data structure.
15182 * @mboxq: Pointer to mailbox object.
15183 *
15184 * This routine is invoked to manually add a single FCF record. The caller
15185 * must pass a completely initialized FCF_Record. This routine takes
15186 * care of the nonembedded mailbox operations.
15187 **/
15188 static void
15189 lpfc_mbx_cmpl_add_fcf_record(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
15190 {
15191 void *virt_addr;
15192 union lpfc_sli4_cfg_shdr *shdr;
15193 uint32_t shdr_status, shdr_add_status;
15194
15195 virt_addr = mboxq->sge_array->addr[0];
15196 /* The IOCTL status is embedded in the mailbox subheader. */
15197 shdr = (union lpfc_sli4_cfg_shdr *) virt_addr;
15198 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
15199 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
15200
15201 if ((shdr_status || shdr_add_status) &&
15202 (shdr_status != STATUS_FCF_IN_USE))
15203 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15204 "2558 ADD_FCF_RECORD mailbox failed with "
15205 "status x%x add_status x%x\n",
15206 shdr_status, shdr_add_status);
15207
15208 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15209 }
15210
15211 /**
15212 * lpfc_sli4_add_fcf_record - Manually add an FCF Record.
15213 * @phba: pointer to lpfc hba data structure.
15214 * @fcf_record: pointer to the initialized fcf record to add.
15215 *
15216 * This routine is invoked to manually add a single FCF record. The caller
15217 * must pass a completely initialized FCF_Record. This routine takes
15218 * care of the nonembedded mailbox operations.
15219 **/
15220 int
15221 lpfc_sli4_add_fcf_record(struct lpfc_hba *phba, struct fcf_record *fcf_record)
15222 {
15223 int rc = 0;
15224 LPFC_MBOXQ_t *mboxq;
15225 uint8_t *bytep;
15226 void *virt_addr;
15227 dma_addr_t phys_addr;
15228 struct lpfc_mbx_sge sge;
15229 uint32_t alloc_len, req_len;
15230 uint32_t fcfindex;
15231
15232 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15233 if (!mboxq) {
15234 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15235 "2009 Failed to allocate mbox for ADD_FCF cmd\n");
15236 return -ENOMEM;
15237 }
15238
15239 req_len = sizeof(struct fcf_record) + sizeof(union lpfc_sli4_cfg_shdr) +
15240 sizeof(uint32_t);
15241
15242 /* Allocate DMA memory and set up the non-embedded mailbox command */
15243 alloc_len = lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_FCOE,
15244 LPFC_MBOX_OPCODE_FCOE_ADD_FCF,
15245 req_len, LPFC_SLI4_MBX_NEMBED);
15246 if (alloc_len < req_len) {
15247 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15248 "2523 Allocated DMA memory size (x%x) is "
15249 "less than the requested DMA memory "
15250 "size (x%x)\n", alloc_len, req_len);
15251 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15252 return -ENOMEM;
15253 }
15254
15255 /*
15256 * Get the first SGE entry from the non-embedded DMA memory. This
15257 * routine only uses a single SGE.
15258 */
15259 lpfc_sli4_mbx_sge_get(mboxq, 0, &sge);
15260 phys_addr = getPaddr(sge.pa_hi, sge.pa_lo);
15261 virt_addr = mboxq->sge_array->addr[0];
15262 /*
15263 * Configure the FCF record for FCFI 0. This is the driver's
15264 * hardcoded default and gets used in nonFIP mode.
15265 */
15266 fcfindex = bf_get(lpfc_fcf_record_fcf_index, fcf_record);
15267 bytep = virt_addr + sizeof(union lpfc_sli4_cfg_shdr);
15268 lpfc_sli_pcimem_bcopy(&fcfindex, bytep, sizeof(uint32_t));
15269
15270 /*
15271 * Copy the fcf_index and the FCF Record Data. The data starts after
15272 * the FCoE header plus word10. The data copy needs to be endian
15273 * correct.
15274 */
15275 bytep += sizeof(uint32_t);
15276 lpfc_sli_pcimem_bcopy(fcf_record, bytep, sizeof(struct fcf_record));
15277 mboxq->vport = phba->pport;
15278 mboxq->mbox_cmpl = lpfc_mbx_cmpl_add_fcf_record;
15279 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15280 if (rc == MBX_NOT_FINISHED) {
15281 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15282 "2515 ADD_FCF_RECORD mailbox failed with "
15283 "status 0x%x\n", rc);
15284 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15285 rc = -EIO;
15286 } else
15287 rc = 0;
15288
15289 return rc;
15290 }
15291
15292 /**
15293 * lpfc_sli4_build_dflt_fcf_record - Build the driver's default FCF Record.
15294 * @phba: pointer to lpfc hba data structure.
15295 * @fcf_record: pointer to the fcf record to write the default data.
15296 * @fcf_index: FCF table entry index.
15297 *
15298 * This routine is invoked to build the driver's default FCF record. The
15299 * values used are hardcoded. This routine handles memory initialization.
15300 *
15301 **/
15302 void
15303 lpfc_sli4_build_dflt_fcf_record(struct lpfc_hba *phba,
15304 struct fcf_record *fcf_record,
15305 uint16_t fcf_index)
15306 {
15307 memset(fcf_record, 0, sizeof(struct fcf_record));
15308 fcf_record->max_rcv_size = LPFC_FCOE_MAX_RCV_SIZE;
15309 fcf_record->fka_adv_period = LPFC_FCOE_FKA_ADV_PER;
15310 fcf_record->fip_priority = LPFC_FCOE_FIP_PRIORITY;
15311 bf_set(lpfc_fcf_record_mac_0, fcf_record, phba->fc_map[0]);
15312 bf_set(lpfc_fcf_record_mac_1, fcf_record, phba->fc_map[1]);
15313 bf_set(lpfc_fcf_record_mac_2, fcf_record, phba->fc_map[2]);
15314 bf_set(lpfc_fcf_record_mac_3, fcf_record, LPFC_FCOE_FCF_MAC3);
15315 bf_set(lpfc_fcf_record_mac_4, fcf_record, LPFC_FCOE_FCF_MAC4);
15316 bf_set(lpfc_fcf_record_mac_5, fcf_record, LPFC_FCOE_FCF_MAC5);
15317 bf_set(lpfc_fcf_record_fc_map_0, fcf_record, phba->fc_map[0]);
15318 bf_set(lpfc_fcf_record_fc_map_1, fcf_record, phba->fc_map[1]);
15319 bf_set(lpfc_fcf_record_fc_map_2, fcf_record, phba->fc_map[2]);
15320 bf_set(lpfc_fcf_record_fcf_valid, fcf_record, 1);
15321 bf_set(lpfc_fcf_record_fcf_avail, fcf_record, 1);
15322 bf_set(lpfc_fcf_record_fcf_index, fcf_record, fcf_index);
15323 bf_set(lpfc_fcf_record_mac_addr_prov, fcf_record,
15324 LPFC_FCF_FPMA | LPFC_FCF_SPMA);
15325 /* Set the VLAN bit map */
15326 if (phba->valid_vlan) {
15327 fcf_record->vlan_bitmap[phba->vlan_id / 8]
15328 = 1 << (phba->vlan_id % 8);
15329 }
15330 }
15331
15332 /**
15333 * lpfc_sli4_fcf_scan_read_fcf_rec - Read hba fcf record for fcf scan.
15334 * @phba: pointer to lpfc hba data structure.
15335 * @fcf_index: FCF table entry offset.
15336 *
15337 * This routine is invoked to scan the entire FCF table by reading FCF
15338 * record and processing it one at a time starting from the @fcf_index
15339 * for initial FCF discovery or fast FCF failover rediscovery.
15340 *
15341 * Return 0 if the mailbox command is submitted successfully, none 0
15342 * otherwise.
15343 **/
15344 int
15345 lpfc_sli4_fcf_scan_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15346 {
15347 int rc = 0, error;
15348 LPFC_MBOXQ_t *mboxq;
15349
15350 phba->fcoe_eventtag_at_fcf_scan = phba->fcoe_eventtag;
15351 phba->fcoe_cvl_eventtag_attn = phba->fcoe_cvl_eventtag;
15352 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15353 if (!mboxq) {
15354 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15355 "2000 Failed to allocate mbox for "
15356 "READ_FCF cmd\n");
15357 error = -ENOMEM;
15358 goto fail_fcf_scan;
15359 }
15360 /* Construct the read FCF record mailbox command */
15361 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15362 if (rc) {
15363 error = -EINVAL;
15364 goto fail_fcf_scan;
15365 }
15366 /* Issue the mailbox command asynchronously */
15367 mboxq->vport = phba->pport;
15368 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_scan_read_fcf_rec;
15369
15370 spin_lock_irq(&phba->hbalock);
15371 phba->hba_flag |= FCF_TS_INPROG;
15372 spin_unlock_irq(&phba->hbalock);
15373
15374 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15375 if (rc == MBX_NOT_FINISHED)
15376 error = -EIO;
15377 else {
15378 /* Reset eligible FCF count for new scan */
15379 if (fcf_index == LPFC_FCOE_FCF_GET_FIRST)
15380 phba->fcf.eligible_fcf_cnt = 0;
15381 error = 0;
15382 }
15383 fail_fcf_scan:
15384 if (error) {
15385 if (mboxq)
15386 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15387 /* FCF scan failed, clear FCF_TS_INPROG flag */
15388 spin_lock_irq(&phba->hbalock);
15389 phba->hba_flag &= ~FCF_TS_INPROG;
15390 spin_unlock_irq(&phba->hbalock);
15391 }
15392 return error;
15393 }
15394
15395 /**
15396 * lpfc_sli4_fcf_rr_read_fcf_rec - Read hba fcf record for roundrobin fcf.
15397 * @phba: pointer to lpfc hba data structure.
15398 * @fcf_index: FCF table entry offset.
15399 *
15400 * This routine is invoked to read an FCF record indicated by @fcf_index
15401 * and to use it for FLOGI roundrobin FCF failover.
15402 *
15403 * Return 0 if the mailbox command is submitted successfully, none 0
15404 * otherwise.
15405 **/
15406 int
15407 lpfc_sli4_fcf_rr_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15408 {
15409 int rc = 0, error;
15410 LPFC_MBOXQ_t *mboxq;
15411
15412 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15413 if (!mboxq) {
15414 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15415 "2763 Failed to allocate mbox for "
15416 "READ_FCF cmd\n");
15417 error = -ENOMEM;
15418 goto fail_fcf_read;
15419 }
15420 /* Construct the read FCF record mailbox command */
15421 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15422 if (rc) {
15423 error = -EINVAL;
15424 goto fail_fcf_read;
15425 }
15426 /* Issue the mailbox command asynchronously */
15427 mboxq->vport = phba->pport;
15428 mboxq->mbox_cmpl = lpfc_mbx_cmpl_fcf_rr_read_fcf_rec;
15429 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15430 if (rc == MBX_NOT_FINISHED)
15431 error = -EIO;
15432 else
15433 error = 0;
15434
15435 fail_fcf_read:
15436 if (error && mboxq)
15437 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15438 return error;
15439 }
15440
15441 /**
15442 * lpfc_sli4_read_fcf_rec - Read hba fcf record for update eligible fcf bmask.
15443 * @phba: pointer to lpfc hba data structure.
15444 * @fcf_index: FCF table entry offset.
15445 *
15446 * This routine is invoked to read an FCF record indicated by @fcf_index to
15447 * determine whether it's eligible for FLOGI roundrobin failover list.
15448 *
15449 * Return 0 if the mailbox command is submitted successfully, none 0
15450 * otherwise.
15451 **/
15452 int
15453 lpfc_sli4_read_fcf_rec(struct lpfc_hba *phba, uint16_t fcf_index)
15454 {
15455 int rc = 0, error;
15456 LPFC_MBOXQ_t *mboxq;
15457
15458 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15459 if (!mboxq) {
15460 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_INIT,
15461 "2758 Failed to allocate mbox for "
15462 "READ_FCF cmd\n");
15463 error = -ENOMEM;
15464 goto fail_fcf_read;
15465 }
15466 /* Construct the read FCF record mailbox command */
15467 rc = lpfc_sli4_mbx_read_fcf_rec(phba, mboxq, fcf_index);
15468 if (rc) {
15469 error = -EINVAL;
15470 goto fail_fcf_read;
15471 }
15472 /* Issue the mailbox command asynchronously */
15473 mboxq->vport = phba->pport;
15474 mboxq->mbox_cmpl = lpfc_mbx_cmpl_read_fcf_rec;
15475 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_NOWAIT);
15476 if (rc == MBX_NOT_FINISHED)
15477 error = -EIO;
15478 else
15479 error = 0;
15480
15481 fail_fcf_read:
15482 if (error && mboxq)
15483 lpfc_sli4_mbox_cmd_free(phba, mboxq);
15484 return error;
15485 }
15486
15487 /**
15488 * lpfc_check_next_fcf_pri
15489 * phba pointer to the lpfc_hba struct for this port.
15490 * This routine is called from the lpfc_sli4_fcf_rr_next_index_get
15491 * routine when the rr_bmask is empty. The FCF indecies are put into the
15492 * rr_bmask based on their priority level. Starting from the highest priority
15493 * to the lowest. The most likely FCF candidate will be in the highest
15494 * priority group. When this routine is called it searches the fcf_pri list for
15495 * next lowest priority group and repopulates the rr_bmask with only those
15496 * fcf_indexes.
15497 * returns:
15498 * 1=success 0=failure
15499 **/
15500 int
15501 lpfc_check_next_fcf_pri_level(struct lpfc_hba *phba)
15502 {
15503 uint16_t next_fcf_pri;
15504 uint16_t last_index;
15505 struct lpfc_fcf_pri *fcf_pri;
15506 int rc;
15507 int ret = 0;
15508
15509 last_index = find_first_bit(phba->fcf.fcf_rr_bmask,
15510 LPFC_SLI4_FCF_TBL_INDX_MAX);
15511 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15512 "3060 Last IDX %d\n", last_index);
15513
15514 /* Verify the priority list has 2 or more entries */
15515 spin_lock_irq(&phba->hbalock);
15516 if (list_empty(&phba->fcf.fcf_pri_list) ||
15517 list_is_singular(&phba->fcf.fcf_pri_list)) {
15518 spin_unlock_irq(&phba->hbalock);
15519 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15520 "3061 Last IDX %d\n", last_index);
15521 return 0; /* Empty rr list */
15522 }
15523 spin_unlock_irq(&phba->hbalock);
15524
15525 next_fcf_pri = 0;
15526 /*
15527 * Clear the rr_bmask and set all of the bits that are at this
15528 * priority.
15529 */
15530 memset(phba->fcf.fcf_rr_bmask, 0,
15531 sizeof(*phba->fcf.fcf_rr_bmask));
15532 spin_lock_irq(&phba->hbalock);
15533 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15534 if (fcf_pri->fcf_rec.flag & LPFC_FCF_FLOGI_FAILED)
15535 continue;
15536 /*
15537 * the 1st priority that has not FLOGI failed
15538 * will be the highest.
15539 */
15540 if (!next_fcf_pri)
15541 next_fcf_pri = fcf_pri->fcf_rec.priority;
15542 spin_unlock_irq(&phba->hbalock);
15543 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15544 rc = lpfc_sli4_fcf_rr_index_set(phba,
15545 fcf_pri->fcf_rec.fcf_index);
15546 if (rc)
15547 return 0;
15548 }
15549 spin_lock_irq(&phba->hbalock);
15550 }
15551 /*
15552 * if next_fcf_pri was not set above and the list is not empty then
15553 * we have failed flogis on all of them. So reset flogi failed
15554 * and start at the beginning.
15555 */
15556 if (!next_fcf_pri && !list_empty(&phba->fcf.fcf_pri_list)) {
15557 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15558 fcf_pri->fcf_rec.flag &= ~LPFC_FCF_FLOGI_FAILED;
15559 /*
15560 * the 1st priority that has not FLOGI failed
15561 * will be the highest.
15562 */
15563 if (!next_fcf_pri)
15564 next_fcf_pri = fcf_pri->fcf_rec.priority;
15565 spin_unlock_irq(&phba->hbalock);
15566 if (fcf_pri->fcf_rec.priority == next_fcf_pri) {
15567 rc = lpfc_sli4_fcf_rr_index_set(phba,
15568 fcf_pri->fcf_rec.fcf_index);
15569 if (rc)
15570 return 0;
15571 }
15572 spin_lock_irq(&phba->hbalock);
15573 }
15574 } else
15575 ret = 1;
15576 spin_unlock_irq(&phba->hbalock);
15577
15578 return ret;
15579 }
15580 /**
15581 * lpfc_sli4_fcf_rr_next_index_get - Get next eligible fcf record index
15582 * @phba: pointer to lpfc hba data structure.
15583 *
15584 * This routine is to get the next eligible FCF record index in a round
15585 * robin fashion. If the next eligible FCF record index equals to the
15586 * initial roundrobin FCF record index, LPFC_FCOE_FCF_NEXT_NONE (0xFFFF)
15587 * shall be returned, otherwise, the next eligible FCF record's index
15588 * shall be returned.
15589 **/
15590 uint16_t
15591 lpfc_sli4_fcf_rr_next_index_get(struct lpfc_hba *phba)
15592 {
15593 uint16_t next_fcf_index;
15594
15595 initial_priority:
15596 /* Search start from next bit of currently registered FCF index */
15597 next_fcf_index = phba->fcf.current_rec.fcf_indx;
15598
15599 next_priority:
15600 /* Determine the next fcf index to check */
15601 next_fcf_index = (next_fcf_index + 1) % LPFC_SLI4_FCF_TBL_INDX_MAX;
15602 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15603 LPFC_SLI4_FCF_TBL_INDX_MAX,
15604 next_fcf_index);
15605
15606 /* Wrap around condition on phba->fcf.fcf_rr_bmask */
15607 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15608 /*
15609 * If we have wrapped then we need to clear the bits that
15610 * have been tested so that we can detect when we should
15611 * change the priority level.
15612 */
15613 next_fcf_index = find_next_bit(phba->fcf.fcf_rr_bmask,
15614 LPFC_SLI4_FCF_TBL_INDX_MAX, 0);
15615 }
15616
15617
15618 /* Check roundrobin failover list empty condition */
15619 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX ||
15620 next_fcf_index == phba->fcf.current_rec.fcf_indx) {
15621 /*
15622 * If next fcf index is not found check if there are lower
15623 * Priority level fcf's in the fcf_priority list.
15624 * Set up the rr_bmask with all of the avaiable fcf bits
15625 * at that level and continue the selection process.
15626 */
15627 if (lpfc_check_next_fcf_pri_level(phba))
15628 goto initial_priority;
15629 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15630 "2844 No roundrobin failover FCF available\n");
15631 if (next_fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX)
15632 return LPFC_FCOE_FCF_NEXT_NONE;
15633 else {
15634 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP,
15635 "3063 Only FCF available idx %d, flag %x\n",
15636 next_fcf_index,
15637 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag);
15638 return next_fcf_index;
15639 }
15640 }
15641
15642 if (next_fcf_index < LPFC_SLI4_FCF_TBL_INDX_MAX &&
15643 phba->fcf.fcf_pri[next_fcf_index].fcf_rec.flag &
15644 LPFC_FCF_FLOGI_FAILED)
15645 goto next_priority;
15646
15647 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15648 "2845 Get next roundrobin failover FCF (x%x)\n",
15649 next_fcf_index);
15650
15651 return next_fcf_index;
15652 }
15653
15654 /**
15655 * lpfc_sli4_fcf_rr_index_set - Set bmask with eligible fcf record index
15656 * @phba: pointer to lpfc hba data structure.
15657 *
15658 * This routine sets the FCF record index in to the eligible bmask for
15659 * roundrobin failover search. It checks to make sure that the index
15660 * does not go beyond the range of the driver allocated bmask dimension
15661 * before setting the bit.
15662 *
15663 * Returns 0 if the index bit successfully set, otherwise, it returns
15664 * -EINVAL.
15665 **/
15666 int
15667 lpfc_sli4_fcf_rr_index_set(struct lpfc_hba *phba, uint16_t fcf_index)
15668 {
15669 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15670 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15671 "2610 FCF (x%x) reached driver's book "
15672 "keeping dimension:x%x\n",
15673 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15674 return -EINVAL;
15675 }
15676 /* Set the eligible FCF record index bmask */
15677 set_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15678
15679 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15680 "2790 Set FCF (x%x) to roundrobin FCF failover "
15681 "bmask\n", fcf_index);
15682
15683 return 0;
15684 }
15685
15686 /**
15687 * lpfc_sli4_fcf_rr_index_clear - Clear bmask from eligible fcf record index
15688 * @phba: pointer to lpfc hba data structure.
15689 *
15690 * This routine clears the FCF record index from the eligible bmask for
15691 * roundrobin failover search. It checks to make sure that the index
15692 * does not go beyond the range of the driver allocated bmask dimension
15693 * before clearing the bit.
15694 **/
15695 void
15696 lpfc_sli4_fcf_rr_index_clear(struct lpfc_hba *phba, uint16_t fcf_index)
15697 {
15698 struct lpfc_fcf_pri *fcf_pri;
15699 if (fcf_index >= LPFC_SLI4_FCF_TBL_INDX_MAX) {
15700 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15701 "2762 FCF (x%x) reached driver's book "
15702 "keeping dimension:x%x\n",
15703 fcf_index, LPFC_SLI4_FCF_TBL_INDX_MAX);
15704 return;
15705 }
15706 /* Clear the eligible FCF record index bmask */
15707 spin_lock_irq(&phba->hbalock);
15708 list_for_each_entry(fcf_pri, &phba->fcf.fcf_pri_list, list) {
15709 if (fcf_pri->fcf_rec.fcf_index == fcf_index) {
15710 list_del_init(&fcf_pri->list);
15711 break;
15712 }
15713 }
15714 spin_unlock_irq(&phba->hbalock);
15715 clear_bit(fcf_index, phba->fcf.fcf_rr_bmask);
15716
15717 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15718 "2791 Clear FCF (x%x) from roundrobin failover "
15719 "bmask\n", fcf_index);
15720 }
15721
15722 /**
15723 * lpfc_mbx_cmpl_redisc_fcf_table - completion routine for rediscover FCF table
15724 * @phba: pointer to lpfc hba data structure.
15725 *
15726 * This routine is the completion routine for the rediscover FCF table mailbox
15727 * command. If the mailbox command returned failure, it will try to stop the
15728 * FCF rediscover wait timer.
15729 **/
15730 void
15731 lpfc_mbx_cmpl_redisc_fcf_table(struct lpfc_hba *phba, LPFC_MBOXQ_t *mbox)
15732 {
15733 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15734 uint32_t shdr_status, shdr_add_status;
15735
15736 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15737
15738 shdr_status = bf_get(lpfc_mbox_hdr_status,
15739 &redisc_fcf->header.cfg_shdr.response);
15740 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
15741 &redisc_fcf->header.cfg_shdr.response);
15742 if (shdr_status || shdr_add_status) {
15743 lpfc_printf_log(phba, KERN_ERR, LOG_FIP,
15744 "2746 Requesting for FCF rediscovery failed "
15745 "status x%x add_status x%x\n",
15746 shdr_status, shdr_add_status);
15747 if (phba->fcf.fcf_flag & FCF_ACVL_DISC) {
15748 spin_lock_irq(&phba->hbalock);
15749 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
15750 spin_unlock_irq(&phba->hbalock);
15751 /*
15752 * CVL event triggered FCF rediscover request failed,
15753 * last resort to re-try current registered FCF entry.
15754 */
15755 lpfc_retry_pport_discovery(phba);
15756 } else {
15757 spin_lock_irq(&phba->hbalock);
15758 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
15759 spin_unlock_irq(&phba->hbalock);
15760 /*
15761 * DEAD FCF event triggered FCF rediscover request
15762 * failed, last resort to fail over as a link down
15763 * to FCF registration.
15764 */
15765 lpfc_sli4_fcf_dead_failthrough(phba);
15766 }
15767 } else {
15768 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
15769 "2775 Start FCF rediscover quiescent timer\n");
15770 /*
15771 * Start FCF rediscovery wait timer for pending FCF
15772 * before rescan FCF record table.
15773 */
15774 lpfc_fcf_redisc_wait_start_timer(phba);
15775 }
15776
15777 mempool_free(mbox, phba->mbox_mem_pool);
15778 }
15779
15780 /**
15781 * lpfc_sli4_redisc_fcf_table - Request to rediscover entire FCF table by port.
15782 * @phba: pointer to lpfc hba data structure.
15783 *
15784 * This routine is invoked to request for rediscovery of the entire FCF table
15785 * by the port.
15786 **/
15787 int
15788 lpfc_sli4_redisc_fcf_table(struct lpfc_hba *phba)
15789 {
15790 LPFC_MBOXQ_t *mbox;
15791 struct lpfc_mbx_redisc_fcf_tbl *redisc_fcf;
15792 int rc, length;
15793
15794 /* Cancel retry delay timers to all vports before FCF rediscover */
15795 lpfc_cancel_all_vport_retry_delay_timer(phba);
15796
15797 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15798 if (!mbox) {
15799 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
15800 "2745 Failed to allocate mbox for "
15801 "requesting FCF rediscover.\n");
15802 return -ENOMEM;
15803 }
15804
15805 length = (sizeof(struct lpfc_mbx_redisc_fcf_tbl) -
15806 sizeof(struct lpfc_sli4_cfg_mhdr));
15807 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
15808 LPFC_MBOX_OPCODE_FCOE_REDISCOVER_FCF,
15809 length, LPFC_SLI4_MBX_EMBED);
15810
15811 redisc_fcf = &mbox->u.mqe.un.redisc_fcf_tbl;
15812 /* Set count to 0 for invalidating the entire FCF database */
15813 bf_set(lpfc_mbx_redisc_fcf_count, redisc_fcf, 0);
15814
15815 /* Issue the mailbox command asynchronously */
15816 mbox->vport = phba->pport;
15817 mbox->mbox_cmpl = lpfc_mbx_cmpl_redisc_fcf_table;
15818 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_NOWAIT);
15819
15820 if (rc == MBX_NOT_FINISHED) {
15821 mempool_free(mbox, phba->mbox_mem_pool);
15822 return -EIO;
15823 }
15824 return 0;
15825 }
15826
15827 /**
15828 * lpfc_sli4_fcf_dead_failthrough - Failthrough routine to fcf dead event
15829 * @phba: pointer to lpfc hba data structure.
15830 *
15831 * This function is the failover routine as a last resort to the FCF DEAD
15832 * event when driver failed to perform fast FCF failover.
15833 **/
15834 void
15835 lpfc_sli4_fcf_dead_failthrough(struct lpfc_hba *phba)
15836 {
15837 uint32_t link_state;
15838
15839 /*
15840 * Last resort as FCF DEAD event failover will treat this as
15841 * a link down, but save the link state because we don't want
15842 * it to be changed to Link Down unless it is already down.
15843 */
15844 link_state = phba->link_state;
15845 lpfc_linkdown(phba);
15846 phba->link_state = link_state;
15847
15848 /* Unregister FCF if no devices connected to it */
15849 lpfc_unregister_unused_fcf(phba);
15850 }
15851
15852 /**
15853 * lpfc_sli_get_config_region23 - Get sli3 port region 23 data.
15854 * @phba: pointer to lpfc hba data structure.
15855 * @rgn23_data: pointer to configure region 23 data.
15856 *
15857 * This function gets SLI3 port configure region 23 data through memory dump
15858 * mailbox command. When it successfully retrieves data, the size of the data
15859 * will be returned, otherwise, 0 will be returned.
15860 **/
15861 static uint32_t
15862 lpfc_sli_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15863 {
15864 LPFC_MBOXQ_t *pmb = NULL;
15865 MAILBOX_t *mb;
15866 uint32_t offset = 0;
15867 int rc;
15868
15869 if (!rgn23_data)
15870 return 0;
15871
15872 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15873 if (!pmb) {
15874 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15875 "2600 failed to allocate mailbox memory\n");
15876 return 0;
15877 }
15878 mb = &pmb->u.mb;
15879
15880 do {
15881 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_23);
15882 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
15883
15884 if (rc != MBX_SUCCESS) {
15885 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
15886 "2601 failed to read config "
15887 "region 23, rc 0x%x Status 0x%x\n",
15888 rc, mb->mbxStatus);
15889 mb->un.varDmp.word_cnt = 0;
15890 }
15891 /*
15892 * dump mem may return a zero when finished or we got a
15893 * mailbox error, either way we are done.
15894 */
15895 if (mb->un.varDmp.word_cnt == 0)
15896 break;
15897 if (mb->un.varDmp.word_cnt > DMP_RGN23_SIZE - offset)
15898 mb->un.varDmp.word_cnt = DMP_RGN23_SIZE - offset;
15899
15900 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
15901 rgn23_data + offset,
15902 mb->un.varDmp.word_cnt);
15903 offset += mb->un.varDmp.word_cnt;
15904 } while (mb->un.varDmp.word_cnt && offset < DMP_RGN23_SIZE);
15905
15906 mempool_free(pmb, phba->mbox_mem_pool);
15907 return offset;
15908 }
15909
15910 /**
15911 * lpfc_sli4_get_config_region23 - Get sli4 port region 23 data.
15912 * @phba: pointer to lpfc hba data structure.
15913 * @rgn23_data: pointer to configure region 23 data.
15914 *
15915 * This function gets SLI4 port configure region 23 data through memory dump
15916 * mailbox command. When it successfully retrieves data, the size of the data
15917 * will be returned, otherwise, 0 will be returned.
15918 **/
15919 static uint32_t
15920 lpfc_sli4_get_config_region23(struct lpfc_hba *phba, char *rgn23_data)
15921 {
15922 LPFC_MBOXQ_t *mboxq = NULL;
15923 struct lpfc_dmabuf *mp = NULL;
15924 struct lpfc_mqe *mqe;
15925 uint32_t data_length = 0;
15926 int rc;
15927
15928 if (!rgn23_data)
15929 return 0;
15930
15931 mboxq = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
15932 if (!mboxq) {
15933 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15934 "3105 failed to allocate mailbox memory\n");
15935 return 0;
15936 }
15937
15938 if (lpfc_sli4_dump_cfg_rg23(phba, mboxq))
15939 goto out;
15940 mqe = &mboxq->u.mqe;
15941 mp = (struct lpfc_dmabuf *) mboxq->context1;
15942 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
15943 if (rc)
15944 goto out;
15945 data_length = mqe->un.mb_words[5];
15946 if (data_length == 0)
15947 goto out;
15948 if (data_length > DMP_RGN23_SIZE) {
15949 data_length = 0;
15950 goto out;
15951 }
15952 lpfc_sli_pcimem_bcopy((char *)mp->virt, rgn23_data, data_length);
15953 out:
15954 mempool_free(mboxq, phba->mbox_mem_pool);
15955 if (mp) {
15956 lpfc_mbuf_free(phba, mp->virt, mp->phys);
15957 kfree(mp);
15958 }
15959 return data_length;
15960 }
15961
15962 /**
15963 * lpfc_sli_read_link_ste - Read region 23 to decide if link is disabled.
15964 * @phba: pointer to lpfc hba data structure.
15965 *
15966 * This function read region 23 and parse TLV for port status to
15967 * decide if the user disaled the port. If the TLV indicates the
15968 * port is disabled, the hba_flag is set accordingly.
15969 **/
15970 void
15971 lpfc_sli_read_link_ste(struct lpfc_hba *phba)
15972 {
15973 uint8_t *rgn23_data = NULL;
15974 uint32_t if_type, data_size, sub_tlv_len, tlv_offset;
15975 uint32_t offset = 0;
15976
15977 /* Get adapter Region 23 data */
15978 rgn23_data = kzalloc(DMP_RGN23_SIZE, GFP_KERNEL);
15979 if (!rgn23_data)
15980 goto out;
15981
15982 if (phba->sli_rev < LPFC_SLI_REV4)
15983 data_size = lpfc_sli_get_config_region23(phba, rgn23_data);
15984 else {
15985 if_type = bf_get(lpfc_sli_intf_if_type,
15986 &phba->sli4_hba.sli_intf);
15987 if (if_type == LPFC_SLI_INTF_IF_TYPE_0)
15988 goto out;
15989 data_size = lpfc_sli4_get_config_region23(phba, rgn23_data);
15990 }
15991
15992 if (!data_size)
15993 goto out;
15994
15995 /* Check the region signature first */
15996 if (memcmp(&rgn23_data[offset], LPFC_REGION23_SIGNATURE, 4)) {
15997 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
15998 "2619 Config region 23 has bad signature\n");
15999 goto out;
16000 }
16001 offset += 4;
16002
16003 /* Check the data structure version */
16004 if (rgn23_data[offset] != LPFC_REGION23_VERSION) {
16005 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16006 "2620 Config region 23 has bad version\n");
16007 goto out;
16008 }
16009 offset += 4;
16010
16011 /* Parse TLV entries in the region */
16012 while (offset < data_size) {
16013 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC)
16014 break;
16015 /*
16016 * If the TLV is not driver specific TLV or driver id is
16017 * not linux driver id, skip the record.
16018 */
16019 if ((rgn23_data[offset] != DRIVER_SPECIFIC_TYPE) ||
16020 (rgn23_data[offset + 2] != LINUX_DRIVER_ID) ||
16021 (rgn23_data[offset + 3] != 0)) {
16022 offset += rgn23_data[offset + 1] * 4 + 4;
16023 continue;
16024 }
16025
16026 /* Driver found a driver specific TLV in the config region */
16027 sub_tlv_len = rgn23_data[offset + 1] * 4;
16028 offset += 4;
16029 tlv_offset = 0;
16030
16031 /*
16032 * Search for configured port state sub-TLV.
16033 */
16034 while ((offset < data_size) &&
16035 (tlv_offset < sub_tlv_len)) {
16036 if (rgn23_data[offset] == LPFC_REGION23_LAST_REC) {
16037 offset += 4;
16038 tlv_offset += 4;
16039 break;
16040 }
16041 if (rgn23_data[offset] != PORT_STE_TYPE) {
16042 offset += rgn23_data[offset + 1] * 4 + 4;
16043 tlv_offset += rgn23_data[offset + 1] * 4 + 4;
16044 continue;
16045 }
16046
16047 /* This HBA contains PORT_STE configured */
16048 if (!rgn23_data[offset + 2])
16049 phba->hba_flag |= LINK_DISABLED;
16050
16051 goto out;
16052 }
16053 }
16054
16055 out:
16056 kfree(rgn23_data);
16057 return;
16058 }
16059
16060 /**
16061 * lpfc_wr_object - write an object to the firmware
16062 * @phba: HBA structure that indicates port to create a queue on.
16063 * @dmabuf_list: list of dmabufs to write to the port.
16064 * @size: the total byte value of the objects to write to the port.
16065 * @offset: the current offset to be used to start the transfer.
16066 *
16067 * This routine will create a wr_object mailbox command to send to the port.
16068 * the mailbox command will be constructed using the dma buffers described in
16069 * @dmabuf_list to create a list of BDEs. This routine will fill in as many
16070 * BDEs that the imbedded mailbox can support. The @offset variable will be
16071 * used to indicate the starting offset of the transfer and will also return
16072 * the offset after the write object mailbox has completed. @size is used to
16073 * determine the end of the object and whether the eof bit should be set.
16074 *
16075 * Return 0 is successful and offset will contain the the new offset to use
16076 * for the next write.
16077 * Return negative value for error cases.
16078 **/
16079 int
16080 lpfc_wr_object(struct lpfc_hba *phba, struct list_head *dmabuf_list,
16081 uint32_t size, uint32_t *offset)
16082 {
16083 struct lpfc_mbx_wr_object *wr_object;
16084 LPFC_MBOXQ_t *mbox;
16085 int rc = 0, i = 0;
16086 uint32_t shdr_status, shdr_add_status;
16087 uint32_t mbox_tmo;
16088 union lpfc_sli4_cfg_shdr *shdr;
16089 struct lpfc_dmabuf *dmabuf;
16090 uint32_t written = 0;
16091
16092 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
16093 if (!mbox)
16094 return -ENOMEM;
16095
16096 lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_COMMON,
16097 LPFC_MBOX_OPCODE_WRITE_OBJECT,
16098 sizeof(struct lpfc_mbx_wr_object) -
16099 sizeof(struct lpfc_sli4_cfg_mhdr), LPFC_SLI4_MBX_EMBED);
16100
16101 wr_object = (struct lpfc_mbx_wr_object *)&mbox->u.mqe.un.wr_object;
16102 wr_object->u.request.write_offset = *offset;
16103 sprintf((uint8_t *)wr_object->u.request.object_name, "/");
16104 wr_object->u.request.object_name[0] =
16105 cpu_to_le32(wr_object->u.request.object_name[0]);
16106 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 0);
16107 list_for_each_entry(dmabuf, dmabuf_list, list) {
16108 if (i >= LPFC_MBX_WR_CONFIG_MAX_BDE || written >= size)
16109 break;
16110 wr_object->u.request.bde[i].addrLow = putPaddrLow(dmabuf->phys);
16111 wr_object->u.request.bde[i].addrHigh =
16112 putPaddrHigh(dmabuf->phys);
16113 if (written + SLI4_PAGE_SIZE >= size) {
16114 wr_object->u.request.bde[i].tus.f.bdeSize =
16115 (size - written);
16116 written += (size - written);
16117 bf_set(lpfc_wr_object_eof, &wr_object->u.request, 1);
16118 } else {
16119 wr_object->u.request.bde[i].tus.f.bdeSize =
16120 SLI4_PAGE_SIZE;
16121 written += SLI4_PAGE_SIZE;
16122 }
16123 i++;
16124 }
16125 wr_object->u.request.bde_count = i;
16126 bf_set(lpfc_wr_object_write_length, &wr_object->u.request, written);
16127 if (!phba->sli4_hba.intr_enable)
16128 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
16129 else {
16130 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
16131 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
16132 }
16133 /* The IOCTL status is embedded in the mailbox subheader. */
16134 shdr = (union lpfc_sli4_cfg_shdr *) &wr_object->header.cfg_shdr;
16135 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
16136 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
16137 if (rc != MBX_TIMEOUT)
16138 mempool_free(mbox, phba->mbox_mem_pool);
16139 if (shdr_status || shdr_add_status || rc) {
16140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
16141 "3025 Write Object mailbox failed with "
16142 "status x%x add_status x%x, mbx status x%x\n",
16143 shdr_status, shdr_add_status, rc);
16144 rc = -ENXIO;
16145 } else
16146 *offset += wr_object->u.response.actual_write_length;
16147 return rc;
16148 }
16149
16150 /**
16151 * lpfc_cleanup_pending_mbox - Free up vport discovery mailbox commands.
16152 * @vport: pointer to vport data structure.
16153 *
16154 * This function iterate through the mailboxq and clean up all REG_LOGIN
16155 * and REG_VPI mailbox commands associated with the vport. This function
16156 * is called when driver want to restart discovery of the vport due to
16157 * a Clear Virtual Link event.
16158 **/
16159 void
16160 lpfc_cleanup_pending_mbox(struct lpfc_vport *vport)
16161 {
16162 struct lpfc_hba *phba = vport->phba;
16163 LPFC_MBOXQ_t *mb, *nextmb;
16164 struct lpfc_dmabuf *mp;
16165 struct lpfc_nodelist *ndlp;
16166 struct lpfc_nodelist *act_mbx_ndlp = NULL;
16167 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
16168 LIST_HEAD(mbox_cmd_list);
16169 uint8_t restart_loop;
16170
16171 /* Clean up internally queued mailbox commands with the vport */
16172 spin_lock_irq(&phba->hbalock);
16173 list_for_each_entry_safe(mb, nextmb, &phba->sli.mboxq, list) {
16174 if (mb->vport != vport)
16175 continue;
16176
16177 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16178 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16179 continue;
16180
16181 list_del(&mb->list);
16182 list_add_tail(&mb->list, &mbox_cmd_list);
16183 }
16184 /* Clean up active mailbox command with the vport */
16185 mb = phba->sli.mbox_active;
16186 if (mb && (mb->vport == vport)) {
16187 if ((mb->u.mb.mbxCommand == MBX_REG_LOGIN64) ||
16188 (mb->u.mb.mbxCommand == MBX_REG_VPI))
16189 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16190 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16191 act_mbx_ndlp = (struct lpfc_nodelist *)mb->context2;
16192 /* Put reference count for delayed processing */
16193 act_mbx_ndlp = lpfc_nlp_get(act_mbx_ndlp);
16194 /* Unregister the RPI when mailbox complete */
16195 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16196 }
16197 }
16198 /* Cleanup any mailbox completions which are not yet processed */
16199 do {
16200 restart_loop = 0;
16201 list_for_each_entry(mb, &phba->sli.mboxq_cmpl, list) {
16202 /*
16203 * If this mailox is already processed or it is
16204 * for another vport ignore it.
16205 */
16206 if ((mb->vport != vport) ||
16207 (mb->mbox_flag & LPFC_MBX_IMED_UNREG))
16208 continue;
16209
16210 if ((mb->u.mb.mbxCommand != MBX_REG_LOGIN64) &&
16211 (mb->u.mb.mbxCommand != MBX_REG_VPI))
16212 continue;
16213
16214 mb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
16215 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16216 ndlp = (struct lpfc_nodelist *)mb->context2;
16217 /* Unregister the RPI when mailbox complete */
16218 mb->mbox_flag |= LPFC_MBX_IMED_UNREG;
16219 restart_loop = 1;
16220 spin_unlock_irq(&phba->hbalock);
16221 spin_lock(shost->host_lock);
16222 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16223 spin_unlock(shost->host_lock);
16224 spin_lock_irq(&phba->hbalock);
16225 break;
16226 }
16227 }
16228 } while (restart_loop);
16229
16230 spin_unlock_irq(&phba->hbalock);
16231
16232 /* Release the cleaned-up mailbox commands */
16233 while (!list_empty(&mbox_cmd_list)) {
16234 list_remove_head(&mbox_cmd_list, mb, LPFC_MBOXQ_t, list);
16235 if (mb->u.mb.mbxCommand == MBX_REG_LOGIN64) {
16236 mp = (struct lpfc_dmabuf *) (mb->context1);
16237 if (mp) {
16238 __lpfc_mbuf_free(phba, mp->virt, mp->phys);
16239 kfree(mp);
16240 }
16241 ndlp = (struct lpfc_nodelist *) mb->context2;
16242 mb->context2 = NULL;
16243 if (ndlp) {
16244 spin_lock(shost->host_lock);
16245 ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16246 spin_unlock(shost->host_lock);
16247 lpfc_nlp_put(ndlp);
16248 }
16249 }
16250 mempool_free(mb, phba->mbox_mem_pool);
16251 }
16252
16253 /* Release the ndlp with the cleaned-up active mailbox command */
16254 if (act_mbx_ndlp) {
16255 spin_lock(shost->host_lock);
16256 act_mbx_ndlp->nlp_flag &= ~NLP_IGNR_REG_CMPL;
16257 spin_unlock(shost->host_lock);
16258 lpfc_nlp_put(act_mbx_ndlp);
16259 }
16260 }
16261
16262 /**
16263 * lpfc_drain_txq - Drain the txq
16264 * @phba: Pointer to HBA context object.
16265 *
16266 * This function attempt to submit IOCBs on the txq
16267 * to the adapter. For SLI4 adapters, the txq contains
16268 * ELS IOCBs that have been deferred because the there
16269 * are no SGLs. This congestion can occur with large
16270 * vport counts during node discovery.
16271 **/
16272
16273 uint32_t
16274 lpfc_drain_txq(struct lpfc_hba *phba)
16275 {
16276 LIST_HEAD(completions);
16277 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
16278 struct lpfc_iocbq *piocbq = 0;
16279 unsigned long iflags = 0;
16280 char *fail_msg = NULL;
16281 struct lpfc_sglq *sglq;
16282 union lpfc_wqe wqe;
16283 int txq_cnt = 0;
16284
16285 spin_lock_irqsave(&phba->hbalock, iflags);
16286 list_for_each_entry(piocbq, &pring->txq, list) {
16287 txq_cnt++;
16288 }
16289
16290 if (txq_cnt > pring->txq_max)
16291 pring->txq_max = txq_cnt;
16292
16293 spin_unlock_irqrestore(&phba->hbalock, iflags);
16294
16295 while (!list_empty(&pring->txq)) {
16296 spin_lock_irqsave(&phba->hbalock, iflags);
16297
16298 piocbq = lpfc_sli_ringtx_get(phba, pring);
16299 if (!piocbq) {
16300 spin_unlock_irqrestore(&phba->hbalock, iflags);
16301 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16302 "2823 txq empty and txq_cnt is %d\n ",
16303 txq_cnt);
16304 break;
16305 }
16306 sglq = __lpfc_sli_get_sglq(phba, piocbq);
16307 if (!sglq) {
16308 __lpfc_sli_ringtx_put(phba, pring, piocbq);
16309 spin_unlock_irqrestore(&phba->hbalock, iflags);
16310 break;
16311 }
16312 txq_cnt--;
16313
16314 /* The xri and iocb resources secured,
16315 * attempt to issue request
16316 */
16317 piocbq->sli4_lxritag = sglq->sli4_lxritag;
16318 piocbq->sli4_xritag = sglq->sli4_xritag;
16319 if (NO_XRI == lpfc_sli4_bpl2sgl(phba, piocbq, sglq))
16320 fail_msg = "to convert bpl to sgl";
16321 else if (lpfc_sli4_iocb2wqe(phba, piocbq, &wqe))
16322 fail_msg = "to convert iocb to wqe";
16323 else if (lpfc_sli4_wq_put(phba->sli4_hba.els_wq, &wqe))
16324 fail_msg = " - Wq is full";
16325 else
16326 lpfc_sli_ringtxcmpl_put(phba, pring, piocbq);
16327
16328 if (fail_msg) {
16329 /* Failed means we can't issue and need to cancel */
16330 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
16331 "2822 IOCB failed %s iotag 0x%x "
16332 "xri 0x%x\n",
16333 fail_msg,
16334 piocbq->iotag, piocbq->sli4_xritag);
16335 list_add_tail(&piocbq->list, &completions);
16336 }
16337 spin_unlock_irqrestore(&phba->hbalock, iflags);
16338 }
16339
16340 /* Cancel all the IOCBs that cannot be issued */
16341 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
16342 IOERR_SLI_ABORTED);
16343
16344 return txq_cnt;
16345 }