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