[SCSI] lpfc 8.3.27: Changed worst case mailbox timeout
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_init.c
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2011 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/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/idr.h>
26 #include <linux/interrupt.h>
27 #include <linux/kthread.h>
28 #include <linux/pci.h>
29 #include <linux/spinlock.h>
30 #include <linux/ctype.h>
31 #include <linux/aer.h>
32 #include <linux/slab.h>
33 #include <linux/firmware.h>
34
35 #include <scsi/scsi.h>
36 #include <scsi/scsi_device.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport_fc.h>
39
40 #include "lpfc_hw4.h"
41 #include "lpfc_hw.h"
42 #include "lpfc_sli.h"
43 #include "lpfc_sli4.h"
44 #include "lpfc_nl.h"
45 #include "lpfc_disc.h"
46 #include "lpfc_scsi.h"
47 #include "lpfc.h"
48 #include "lpfc_logmsg.h"
49 #include "lpfc_crtn.h"
50 #include "lpfc_vport.h"
51 #include "lpfc_version.h"
52
53 char *_dump_buf_data;
54 unsigned long _dump_buf_data_order;
55 char *_dump_buf_dif;
56 unsigned long _dump_buf_dif_order;
57 spinlock_t _dump_buf_lock;
58
59 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
60 static int lpfc_post_rcv_buf(struct lpfc_hba *);
61 static int lpfc_sli4_queue_create(struct lpfc_hba *);
62 static void lpfc_sli4_queue_destroy(struct lpfc_hba *);
63 static int lpfc_create_bootstrap_mbox(struct lpfc_hba *);
64 static int lpfc_setup_endian_order(struct lpfc_hba *);
65 static int lpfc_sli4_read_config(struct lpfc_hba *);
66 static void lpfc_destroy_bootstrap_mbox(struct lpfc_hba *);
67 static void lpfc_free_sgl_list(struct lpfc_hba *);
68 static int lpfc_init_sgl_list(struct lpfc_hba *);
69 static int lpfc_init_active_sgl_array(struct lpfc_hba *);
70 static void lpfc_free_active_sgl(struct lpfc_hba *);
71 static int lpfc_hba_down_post_s3(struct lpfc_hba *phba);
72 static int lpfc_hba_down_post_s4(struct lpfc_hba *phba);
73 static int lpfc_sli4_cq_event_pool_create(struct lpfc_hba *);
74 static void lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *);
75 static void lpfc_sli4_cq_event_release_all(struct lpfc_hba *);
76
77 static struct scsi_transport_template *lpfc_transport_template = NULL;
78 static struct scsi_transport_template *lpfc_vport_transport_template = NULL;
79 static DEFINE_IDR(lpfc_hba_index);
80
81 /**
82 * lpfc_config_port_prep - Perform lpfc initialization prior to config port
83 * @phba: pointer to lpfc hba data structure.
84 *
85 * This routine will do LPFC initialization prior to issuing the CONFIG_PORT
86 * mailbox command. It retrieves the revision information from the HBA and
87 * collects the Vital Product Data (VPD) about the HBA for preparing the
88 * configuration of the HBA.
89 *
90 * Return codes:
91 * 0 - success.
92 * -ERESTART - requests the SLI layer to reset the HBA and try again.
93 * Any other value - indicates an error.
94 **/
95 int
96 lpfc_config_port_prep(struct lpfc_hba *phba)
97 {
98 lpfc_vpd_t *vp = &phba->vpd;
99 int i = 0, rc;
100 LPFC_MBOXQ_t *pmb;
101 MAILBOX_t *mb;
102 char *lpfc_vpd_data = NULL;
103 uint16_t offset = 0;
104 static char licensed[56] =
105 "key unlock for use with gnu public licensed code only\0";
106 static int init_key = 1;
107
108 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
109 if (!pmb) {
110 phba->link_state = LPFC_HBA_ERROR;
111 return -ENOMEM;
112 }
113
114 mb = &pmb->u.mb;
115 phba->link_state = LPFC_INIT_MBX_CMDS;
116
117 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
118 if (init_key) {
119 uint32_t *ptext = (uint32_t *) licensed;
120
121 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
122 *ptext = cpu_to_be32(*ptext);
123 init_key = 0;
124 }
125
126 lpfc_read_nv(phba, pmb);
127 memset((char*)mb->un.varRDnvp.rsvd3, 0,
128 sizeof (mb->un.varRDnvp.rsvd3));
129 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
130 sizeof (licensed));
131
132 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
133
134 if (rc != MBX_SUCCESS) {
135 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
136 "0324 Config Port initialization "
137 "error, mbxCmd x%x READ_NVPARM, "
138 "mbxStatus x%x\n",
139 mb->mbxCommand, mb->mbxStatus);
140 mempool_free(pmb, phba->mbox_mem_pool);
141 return -ERESTART;
142 }
143 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
144 sizeof(phba->wwnn));
145 memcpy(phba->wwpn, (char *)mb->un.varRDnvp.portname,
146 sizeof(phba->wwpn));
147 }
148
149 phba->sli3_options = 0x0;
150
151 /* Setup and issue mailbox READ REV command */
152 lpfc_read_rev(phba, pmb);
153 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
154 if (rc != MBX_SUCCESS) {
155 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
156 "0439 Adapter failed to init, mbxCmd x%x "
157 "READ_REV, mbxStatus x%x\n",
158 mb->mbxCommand, mb->mbxStatus);
159 mempool_free( pmb, phba->mbox_mem_pool);
160 return -ERESTART;
161 }
162
163
164 /*
165 * The value of rr must be 1 since the driver set the cv field to 1.
166 * This setting requires the FW to set all revision fields.
167 */
168 if (mb->un.varRdRev.rr == 0) {
169 vp->rev.rBit = 0;
170 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
171 "0440 Adapter failed to init, READ_REV has "
172 "missing revision information.\n");
173 mempool_free(pmb, phba->mbox_mem_pool);
174 return -ERESTART;
175 }
176
177 if (phba->sli_rev == 3 && !mb->un.varRdRev.v3rsp) {
178 mempool_free(pmb, phba->mbox_mem_pool);
179 return -EINVAL;
180 }
181
182 /* Save information as VPD data */
183 vp->rev.rBit = 1;
184 memcpy(&vp->sli3Feat, &mb->un.varRdRev.sli3Feat, sizeof(uint32_t));
185 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
186 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
187 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
188 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
189 vp->rev.biuRev = mb->un.varRdRev.biuRev;
190 vp->rev.smRev = mb->un.varRdRev.smRev;
191 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
192 vp->rev.endecRev = mb->un.varRdRev.endecRev;
193 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
194 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
195 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
196 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
197 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
198 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
199
200 /* If the sli feature level is less then 9, we must
201 * tear down all RPIs and VPIs on link down if NPIV
202 * is enabled.
203 */
204 if (vp->rev.feaLevelHigh < 9)
205 phba->sli3_options |= LPFC_SLI3_VPORT_TEARDOWN;
206
207 if (lpfc_is_LC_HBA(phba->pcidev->device))
208 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
209 sizeof (phba->RandomData));
210
211 /* Get adapter VPD information */
212 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
213 if (!lpfc_vpd_data)
214 goto out_free_mbox;
215 do {
216 lpfc_dump_mem(phba, pmb, offset, DMP_REGION_VPD);
217 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
218
219 if (rc != MBX_SUCCESS) {
220 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
221 "0441 VPD not present on adapter, "
222 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
223 mb->mbxCommand, mb->mbxStatus);
224 mb->un.varDmp.word_cnt = 0;
225 }
226 /* dump mem may return a zero when finished or we got a
227 * mailbox error, either way we are done.
228 */
229 if (mb->un.varDmp.word_cnt == 0)
230 break;
231 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
232 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
233 lpfc_sli_pcimem_bcopy(((uint8_t *)mb) + DMP_RSP_OFFSET,
234 lpfc_vpd_data + offset,
235 mb->un.varDmp.word_cnt);
236 offset += mb->un.varDmp.word_cnt;
237 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
238 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
239
240 kfree(lpfc_vpd_data);
241 out_free_mbox:
242 mempool_free(pmb, phba->mbox_mem_pool);
243 return 0;
244 }
245
246 /**
247 * lpfc_config_async_cmpl - Completion handler for config async event mbox cmd
248 * @phba: pointer to lpfc hba data structure.
249 * @pmboxq: pointer to the driver internal queue element for mailbox command.
250 *
251 * This is the completion handler for driver's configuring asynchronous event
252 * mailbox command to the device. If the mailbox command returns successfully,
253 * it will set internal async event support flag to 1; otherwise, it will
254 * set internal async event support flag to 0.
255 **/
256 static void
257 lpfc_config_async_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
258 {
259 if (pmboxq->u.mb.mbxStatus == MBX_SUCCESS)
260 phba->temp_sensor_support = 1;
261 else
262 phba->temp_sensor_support = 0;
263 mempool_free(pmboxq, phba->mbox_mem_pool);
264 return;
265 }
266
267 /**
268 * lpfc_dump_wakeup_param_cmpl - dump memory mailbox command completion handler
269 * @phba: pointer to lpfc hba data structure.
270 * @pmboxq: pointer to the driver internal queue element for mailbox command.
271 *
272 * This is the completion handler for dump mailbox command for getting
273 * wake up parameters. When this command complete, the response contain
274 * Option rom version of the HBA. This function translate the version number
275 * into a human readable string and store it in OptionROMVersion.
276 **/
277 static void
278 lpfc_dump_wakeup_param_cmpl(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmboxq)
279 {
280 struct prog_id *prg;
281 uint32_t prog_id_word;
282 char dist = ' ';
283 /* character array used for decoding dist type. */
284 char dist_char[] = "nabx";
285
286 if (pmboxq->u.mb.mbxStatus != MBX_SUCCESS) {
287 mempool_free(pmboxq, phba->mbox_mem_pool);
288 return;
289 }
290
291 prg = (struct prog_id *) &prog_id_word;
292
293 /* word 7 contain option rom version */
294 prog_id_word = pmboxq->u.mb.un.varWords[7];
295
296 /* Decode the Option rom version word to a readable string */
297 if (prg->dist < 4)
298 dist = dist_char[prg->dist];
299
300 if ((prg->dist == 3) && (prg->num == 0))
301 sprintf(phba->OptionROMVersion, "%d.%d%d",
302 prg->ver, prg->rev, prg->lev);
303 else
304 sprintf(phba->OptionROMVersion, "%d.%d%d%c%d",
305 prg->ver, prg->rev, prg->lev,
306 dist, prg->num);
307 mempool_free(pmboxq, phba->mbox_mem_pool);
308 return;
309 }
310
311 /**
312 * lpfc_update_vport_wwn - Updates the fc_nodename, fc_portname,
313 * cfg_soft_wwnn, cfg_soft_wwpn
314 * @vport: pointer to lpfc vport data structure.
315 *
316 *
317 * Return codes
318 * None.
319 **/
320 void
321 lpfc_update_vport_wwn(struct lpfc_vport *vport)
322 {
323 /* If the soft name exists then update it using the service params */
324 if (vport->phba->cfg_soft_wwnn)
325 u64_to_wwn(vport->phba->cfg_soft_wwnn,
326 vport->fc_sparam.nodeName.u.wwn);
327 if (vport->phba->cfg_soft_wwpn)
328 u64_to_wwn(vport->phba->cfg_soft_wwpn,
329 vport->fc_sparam.portName.u.wwn);
330
331 /*
332 * If the name is empty or there exists a soft name
333 * then copy the service params name, otherwise use the fc name
334 */
335 if (vport->fc_nodename.u.wwn[0] == 0 || vport->phba->cfg_soft_wwnn)
336 memcpy(&vport->fc_nodename, &vport->fc_sparam.nodeName,
337 sizeof(struct lpfc_name));
338 else
339 memcpy(&vport->fc_sparam.nodeName, &vport->fc_nodename,
340 sizeof(struct lpfc_name));
341
342 if (vport->fc_portname.u.wwn[0] == 0 || vport->phba->cfg_soft_wwpn)
343 memcpy(&vport->fc_portname, &vport->fc_sparam.portName,
344 sizeof(struct lpfc_name));
345 else
346 memcpy(&vport->fc_sparam.portName, &vport->fc_portname,
347 sizeof(struct lpfc_name));
348 }
349
350 /**
351 * lpfc_config_port_post - Perform lpfc initialization after config port
352 * @phba: pointer to lpfc hba data structure.
353 *
354 * This routine will do LPFC initialization after the CONFIG_PORT mailbox
355 * command call. It performs all internal resource and state setups on the
356 * port: post IOCB buffers, enable appropriate host interrupt attentions,
357 * ELS ring timers, etc.
358 *
359 * Return codes
360 * 0 - success.
361 * Any other value - error.
362 **/
363 int
364 lpfc_config_port_post(struct lpfc_hba *phba)
365 {
366 struct lpfc_vport *vport = phba->pport;
367 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
368 LPFC_MBOXQ_t *pmb;
369 MAILBOX_t *mb;
370 struct lpfc_dmabuf *mp;
371 struct lpfc_sli *psli = &phba->sli;
372 uint32_t status, timeout;
373 int i, j;
374 int rc;
375
376 spin_lock_irq(&phba->hbalock);
377 /*
378 * If the Config port completed correctly the HBA is not
379 * over heated any more.
380 */
381 if (phba->over_temp_state == HBA_OVER_TEMP)
382 phba->over_temp_state = HBA_NORMAL_TEMP;
383 spin_unlock_irq(&phba->hbalock);
384
385 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
386 if (!pmb) {
387 phba->link_state = LPFC_HBA_ERROR;
388 return -ENOMEM;
389 }
390 mb = &pmb->u.mb;
391
392 /* Get login parameters for NID. */
393 rc = lpfc_read_sparam(phba, pmb, 0);
394 if (rc) {
395 mempool_free(pmb, phba->mbox_mem_pool);
396 return -ENOMEM;
397 }
398
399 pmb->vport = vport;
400 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
401 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
402 "0448 Adapter failed init, mbxCmd x%x "
403 "READ_SPARM mbxStatus x%x\n",
404 mb->mbxCommand, mb->mbxStatus);
405 phba->link_state = LPFC_HBA_ERROR;
406 mp = (struct lpfc_dmabuf *) pmb->context1;
407 mempool_free(pmb, phba->mbox_mem_pool);
408 lpfc_mbuf_free(phba, mp->virt, mp->phys);
409 kfree(mp);
410 return -EIO;
411 }
412
413 mp = (struct lpfc_dmabuf *) pmb->context1;
414
415 memcpy(&vport->fc_sparam, mp->virt, sizeof (struct serv_parm));
416 lpfc_mbuf_free(phba, mp->virt, mp->phys);
417 kfree(mp);
418 pmb->context1 = NULL;
419 lpfc_update_vport_wwn(vport);
420
421 /* Update the fc_host data structures with new wwn. */
422 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
423 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
424 fc_host_max_npiv_vports(shost) = phba->max_vpi;
425
426 /* If no serial number in VPD data, use low 6 bytes of WWNN */
427 /* This should be consolidated into parse_vpd ? - mr */
428 if (phba->SerialNumber[0] == 0) {
429 uint8_t *outptr;
430
431 outptr = &vport->fc_nodename.u.s.IEEE[0];
432 for (i = 0; i < 12; i++) {
433 status = *outptr++;
434 j = ((status & 0xf0) >> 4);
435 if (j <= 9)
436 phba->SerialNumber[i] =
437 (char)((uint8_t) 0x30 + (uint8_t) j);
438 else
439 phba->SerialNumber[i] =
440 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
441 i++;
442 j = (status & 0xf);
443 if (j <= 9)
444 phba->SerialNumber[i] =
445 (char)((uint8_t) 0x30 + (uint8_t) j);
446 else
447 phba->SerialNumber[i] =
448 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
449 }
450 }
451
452 lpfc_read_config(phba, pmb);
453 pmb->vport = vport;
454 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
455 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
456 "0453 Adapter failed to init, mbxCmd x%x "
457 "READ_CONFIG, mbxStatus x%x\n",
458 mb->mbxCommand, mb->mbxStatus);
459 phba->link_state = LPFC_HBA_ERROR;
460 mempool_free( pmb, phba->mbox_mem_pool);
461 return -EIO;
462 }
463
464 /* Check if the port is disabled */
465 lpfc_sli_read_link_ste(phba);
466
467 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
468 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
469 phba->cfg_hba_queue_depth =
470 (mb->un.varRdConfig.max_xri + 1) -
471 lpfc_sli4_get_els_iocb_cnt(phba);
472
473 phba->lmt = mb->un.varRdConfig.lmt;
474
475 /* Get the default values for Model Name and Description */
476 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
477
478 if ((phba->cfg_link_speed > LPFC_USER_LINK_SPEED_16G)
479 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_1G)
480 && !(phba->lmt & LMT_1Gb))
481 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_2G)
482 && !(phba->lmt & LMT_2Gb))
483 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_4G)
484 && !(phba->lmt & LMT_4Gb))
485 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_8G)
486 && !(phba->lmt & LMT_8Gb))
487 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_10G)
488 && !(phba->lmt & LMT_10Gb))
489 || ((phba->cfg_link_speed == LPFC_USER_LINK_SPEED_16G)
490 && !(phba->lmt & LMT_16Gb))) {
491 /* Reset link speed to auto */
492 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
493 "1302 Invalid speed for this board: "
494 "Reset link speed to auto: x%x\n",
495 phba->cfg_link_speed);
496 phba->cfg_link_speed = LPFC_USER_LINK_SPEED_AUTO;
497 }
498
499 phba->link_state = LPFC_LINK_DOWN;
500
501 /* Only process IOCBs on ELS ring till hba_state is READY */
502 if (psli->ring[psli->extra_ring].cmdringaddr)
503 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
504 if (psli->ring[psli->fcp_ring].cmdringaddr)
505 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
506 if (psli->ring[psli->next_ring].cmdringaddr)
507 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
508
509 /* Post receive buffers for desired rings */
510 if (phba->sli_rev != 3)
511 lpfc_post_rcv_buf(phba);
512
513 /*
514 * Configure HBA MSI-X attention conditions to messages if MSI-X mode
515 */
516 if (phba->intr_type == MSIX) {
517 rc = lpfc_config_msi(phba, pmb);
518 if (rc) {
519 mempool_free(pmb, phba->mbox_mem_pool);
520 return -EIO;
521 }
522 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
523 if (rc != MBX_SUCCESS) {
524 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
525 "0352 Config MSI mailbox command "
526 "failed, mbxCmd x%x, mbxStatus x%x\n",
527 pmb->u.mb.mbxCommand,
528 pmb->u.mb.mbxStatus);
529 mempool_free(pmb, phba->mbox_mem_pool);
530 return -EIO;
531 }
532 }
533
534 spin_lock_irq(&phba->hbalock);
535 /* Initialize ERATT handling flag */
536 phba->hba_flag &= ~HBA_ERATT_HANDLED;
537
538 /* Enable appropriate host interrupts */
539 if (lpfc_readl(phba->HCregaddr, &status)) {
540 spin_unlock_irq(&phba->hbalock);
541 return -EIO;
542 }
543 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
544 if (psli->num_rings > 0)
545 status |= HC_R0INT_ENA;
546 if (psli->num_rings > 1)
547 status |= HC_R1INT_ENA;
548 if (psli->num_rings > 2)
549 status |= HC_R2INT_ENA;
550 if (psli->num_rings > 3)
551 status |= HC_R3INT_ENA;
552
553 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
554 (phba->cfg_poll & DISABLE_FCP_RING_INT))
555 status &= ~(HC_R0INT_ENA);
556
557 writel(status, phba->HCregaddr);
558 readl(phba->HCregaddr); /* flush */
559 spin_unlock_irq(&phba->hbalock);
560
561 /* Set up ring-0 (ELS) timer */
562 timeout = phba->fc_ratov * 2;
563 mod_timer(&vport->els_tmofunc, jiffies + HZ * timeout);
564 /* Set up heart beat (HB) timer */
565 mod_timer(&phba->hb_tmofunc, jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
566 phba->hb_outstanding = 0;
567 phba->last_completion_time = jiffies;
568 /* Set up error attention (ERATT) polling timer */
569 mod_timer(&phba->eratt_poll, jiffies + HZ * LPFC_ERATT_POLL_INTERVAL);
570
571 if (phba->hba_flag & LINK_DISABLED) {
572 lpfc_printf_log(phba,
573 KERN_ERR, LOG_INIT,
574 "2598 Adapter Link is disabled.\n");
575 lpfc_down_link(phba, pmb);
576 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
577 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
578 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
579 lpfc_printf_log(phba,
580 KERN_ERR, LOG_INIT,
581 "2599 Adapter failed to issue DOWN_LINK"
582 " mbox command rc 0x%x\n", rc);
583
584 mempool_free(pmb, phba->mbox_mem_pool);
585 return -EIO;
586 }
587 } else if (phba->cfg_suppress_link_up == LPFC_INITIALIZE_LINK) {
588 lpfc_init_link(phba, pmb, phba->cfg_topology,
589 phba->cfg_link_speed);
590 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
591 lpfc_set_loopback_flag(phba);
592 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
593 if (rc != MBX_SUCCESS) {
594 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
595 "0454 Adapter failed to init, mbxCmd x%x "
596 "INIT_LINK, mbxStatus x%x\n",
597 mb->mbxCommand, mb->mbxStatus);
598
599 /* Clear all interrupt enable conditions */
600 writel(0, phba->HCregaddr);
601 readl(phba->HCregaddr); /* flush */
602 /* Clear all pending interrupts */
603 writel(0xffffffff, phba->HAregaddr);
604 readl(phba->HAregaddr); /* flush */
605 phba->link_state = LPFC_HBA_ERROR;
606 if (rc != MBX_BUSY)
607 mempool_free(pmb, phba->mbox_mem_pool);
608 return -EIO;
609 }
610 }
611 /* MBOX buffer will be freed in mbox compl */
612 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
613 if (!pmb) {
614 phba->link_state = LPFC_HBA_ERROR;
615 return -ENOMEM;
616 }
617
618 lpfc_config_async(phba, pmb, LPFC_ELS_RING);
619 pmb->mbox_cmpl = lpfc_config_async_cmpl;
620 pmb->vport = phba->pport;
621 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
622
623 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
624 lpfc_printf_log(phba,
625 KERN_ERR,
626 LOG_INIT,
627 "0456 Adapter failed to issue "
628 "ASYNCEVT_ENABLE mbox status x%x\n",
629 rc);
630 mempool_free(pmb, phba->mbox_mem_pool);
631 }
632
633 /* Get Option rom version */
634 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
635 if (!pmb) {
636 phba->link_state = LPFC_HBA_ERROR;
637 return -ENOMEM;
638 }
639
640 lpfc_dump_wakeup_param(phba, pmb);
641 pmb->mbox_cmpl = lpfc_dump_wakeup_param_cmpl;
642 pmb->vport = phba->pport;
643 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
644
645 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
646 lpfc_printf_log(phba, KERN_ERR, LOG_INIT, "0435 Adapter failed "
647 "to get Option ROM version status x%x\n", rc);
648 mempool_free(pmb, phba->mbox_mem_pool);
649 }
650
651 return 0;
652 }
653
654 /**
655 * lpfc_hba_init_link - Initialize the FC link
656 * @phba: pointer to lpfc hba data structure.
657 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
658 *
659 * This routine will issue the INIT_LINK mailbox command call.
660 * It is available to other drivers through the lpfc_hba data
661 * structure for use as a delayed link up mechanism with the
662 * module parameter lpfc_suppress_link_up.
663 *
664 * Return code
665 * 0 - success
666 * Any other value - error
667 **/
668 int
669 lpfc_hba_init_link(struct lpfc_hba *phba, uint32_t flag)
670 {
671 struct lpfc_vport *vport = phba->pport;
672 LPFC_MBOXQ_t *pmb;
673 MAILBOX_t *mb;
674 int rc;
675
676 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
677 if (!pmb) {
678 phba->link_state = LPFC_HBA_ERROR;
679 return -ENOMEM;
680 }
681 mb = &pmb->u.mb;
682 pmb->vport = vport;
683
684 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
685 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
686 lpfc_set_loopback_flag(phba);
687 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
688 if ((rc != MBX_BUSY) && (rc != MBX_SUCCESS)) {
689 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
690 "0498 Adapter failed to init, mbxCmd x%x "
691 "INIT_LINK, mbxStatus x%x\n",
692 mb->mbxCommand, mb->mbxStatus);
693 if (phba->sli_rev <= LPFC_SLI_REV3) {
694 /* Clear all interrupt enable conditions */
695 writel(0, phba->HCregaddr);
696 readl(phba->HCregaddr); /* flush */
697 /* Clear all pending interrupts */
698 writel(0xffffffff, phba->HAregaddr);
699 readl(phba->HAregaddr); /* flush */
700 }
701 phba->link_state = LPFC_HBA_ERROR;
702 if (rc != MBX_BUSY || flag == MBX_POLL)
703 mempool_free(pmb, phba->mbox_mem_pool);
704 return -EIO;
705 }
706 phba->cfg_suppress_link_up = LPFC_INITIALIZE_LINK;
707 if (flag == MBX_POLL)
708 mempool_free(pmb, phba->mbox_mem_pool);
709
710 return 0;
711 }
712
713 /**
714 * lpfc_hba_down_link - this routine downs the FC link
715 * @phba: pointer to lpfc hba data structure.
716 * @flag: mailbox command issue mode - either MBX_POLL or MBX_NOWAIT
717 *
718 * This routine will issue the DOWN_LINK mailbox command call.
719 * It is available to other drivers through the lpfc_hba data
720 * structure for use to stop the link.
721 *
722 * Return code
723 * 0 - success
724 * Any other value - error
725 **/
726 int
727 lpfc_hba_down_link(struct lpfc_hba *phba, uint32_t flag)
728 {
729 LPFC_MBOXQ_t *pmb;
730 int rc;
731
732 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
733 if (!pmb) {
734 phba->link_state = LPFC_HBA_ERROR;
735 return -ENOMEM;
736 }
737
738 lpfc_printf_log(phba,
739 KERN_ERR, LOG_INIT,
740 "0491 Adapter Link is disabled.\n");
741 lpfc_down_link(phba, pmb);
742 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
743 rc = lpfc_sli_issue_mbox(phba, pmb, flag);
744 if ((rc != MBX_SUCCESS) && (rc != MBX_BUSY)) {
745 lpfc_printf_log(phba,
746 KERN_ERR, LOG_INIT,
747 "2522 Adapter failed to issue DOWN_LINK"
748 " mbox command rc 0x%x\n", rc);
749
750 mempool_free(pmb, phba->mbox_mem_pool);
751 return -EIO;
752 }
753 if (flag == MBX_POLL)
754 mempool_free(pmb, phba->mbox_mem_pool);
755
756 return 0;
757 }
758
759 /**
760 * lpfc_hba_down_prep - Perform lpfc uninitialization prior to HBA reset
761 * @phba: pointer to lpfc HBA data structure.
762 *
763 * This routine will do LPFC uninitialization before the HBA is reset when
764 * bringing down the SLI Layer.
765 *
766 * Return codes
767 * 0 - success.
768 * Any other value - error.
769 **/
770 int
771 lpfc_hba_down_prep(struct lpfc_hba *phba)
772 {
773 struct lpfc_vport **vports;
774 int i;
775
776 if (phba->sli_rev <= LPFC_SLI_REV3) {
777 /* Disable interrupts */
778 writel(0, phba->HCregaddr);
779 readl(phba->HCregaddr); /* flush */
780 }
781
782 if (phba->pport->load_flag & FC_UNLOADING)
783 lpfc_cleanup_discovery_resources(phba->pport);
784 else {
785 vports = lpfc_create_vport_work_array(phba);
786 if (vports != NULL)
787 for (i = 0; i <= phba->max_vports &&
788 vports[i] != NULL; i++)
789 lpfc_cleanup_discovery_resources(vports[i]);
790 lpfc_destroy_vport_work_array(phba, vports);
791 }
792 return 0;
793 }
794
795 /**
796 * lpfc_hba_down_post_s3 - Perform lpfc uninitialization after HBA reset
797 * @phba: pointer to lpfc HBA data structure.
798 *
799 * This routine will do uninitialization after the HBA is reset when bring
800 * down the SLI Layer.
801 *
802 * Return codes
803 * 0 - success.
804 * Any other value - error.
805 **/
806 static int
807 lpfc_hba_down_post_s3(struct lpfc_hba *phba)
808 {
809 struct lpfc_sli *psli = &phba->sli;
810 struct lpfc_sli_ring *pring;
811 struct lpfc_dmabuf *mp, *next_mp;
812 LIST_HEAD(completions);
813 int i;
814
815 if (phba->sli3_options & LPFC_SLI3_HBQ_ENABLED)
816 lpfc_sli_hbqbuf_free_all(phba);
817 else {
818 /* Cleanup preposted buffers on the ELS ring */
819 pring = &psli->ring[LPFC_ELS_RING];
820 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
821 list_del(&mp->list);
822 pring->postbufq_cnt--;
823 lpfc_mbuf_free(phba, mp->virt, mp->phys);
824 kfree(mp);
825 }
826 }
827
828 spin_lock_irq(&phba->hbalock);
829 for (i = 0; i < psli->num_rings; i++) {
830 pring = &psli->ring[i];
831
832 /* At this point in time the HBA is either reset or DOA. Either
833 * way, nothing should be on txcmplq as it will NEVER complete.
834 */
835 list_splice_init(&pring->txcmplq, &completions);
836 pring->txcmplq_cnt = 0;
837 spin_unlock_irq(&phba->hbalock);
838
839 /* Cancel all the IOCBs from the completions list */
840 lpfc_sli_cancel_iocbs(phba, &completions, IOSTAT_LOCAL_REJECT,
841 IOERR_SLI_ABORTED);
842
843 lpfc_sli_abort_iocb_ring(phba, pring);
844 spin_lock_irq(&phba->hbalock);
845 }
846 spin_unlock_irq(&phba->hbalock);
847
848 return 0;
849 }
850
851 /**
852 * lpfc_hba_down_post_s4 - Perform lpfc uninitialization after HBA reset
853 * @phba: pointer to lpfc HBA data structure.
854 *
855 * This routine will do uninitialization after the HBA is reset when bring
856 * down the SLI Layer.
857 *
858 * Return codes
859 * 0 - success.
860 * Any other value - error.
861 **/
862 static int
863 lpfc_hba_down_post_s4(struct lpfc_hba *phba)
864 {
865 struct lpfc_scsi_buf *psb, *psb_next;
866 LIST_HEAD(aborts);
867 int ret;
868 unsigned long iflag = 0;
869 struct lpfc_sglq *sglq_entry = NULL;
870
871 ret = lpfc_hba_down_post_s3(phba);
872 if (ret)
873 return ret;
874 /* At this point in time the HBA is either reset or DOA. Either
875 * way, nothing should be on lpfc_abts_els_sgl_list, it needs to be
876 * on the lpfc_sgl_list so that it can either be freed if the
877 * driver is unloading or reposted if the driver is restarting
878 * the port.
879 */
880 spin_lock_irq(&phba->hbalock); /* required for lpfc_sgl_list and */
881 /* scsl_buf_list */
882 /* abts_sgl_list_lock required because worker thread uses this
883 * list.
884 */
885 spin_lock(&phba->sli4_hba.abts_sgl_list_lock);
886 list_for_each_entry(sglq_entry,
887 &phba->sli4_hba.lpfc_abts_els_sgl_list, list)
888 sglq_entry->state = SGL_FREED;
889
890 list_splice_init(&phba->sli4_hba.lpfc_abts_els_sgl_list,
891 &phba->sli4_hba.lpfc_sgl_list);
892 spin_unlock(&phba->sli4_hba.abts_sgl_list_lock);
893 /* abts_scsi_buf_list_lock required because worker thread uses this
894 * list.
895 */
896 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
897 list_splice_init(&phba->sli4_hba.lpfc_abts_scsi_buf_list,
898 &aborts);
899 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
900 spin_unlock_irq(&phba->hbalock);
901
902 list_for_each_entry_safe(psb, psb_next, &aborts, list) {
903 psb->pCmd = NULL;
904 psb->status = IOSTAT_SUCCESS;
905 }
906 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
907 list_splice(&aborts, &phba->lpfc_scsi_buf_list);
908 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
909 return 0;
910 }
911
912 /**
913 * lpfc_hba_down_post - Wrapper func for hba down post routine
914 * @phba: pointer to lpfc HBA data structure.
915 *
916 * This routine wraps the actual SLI3 or SLI4 routine for performing
917 * uninitialization after the HBA is reset when bring down the SLI Layer.
918 *
919 * Return codes
920 * 0 - success.
921 * Any other value - error.
922 **/
923 int
924 lpfc_hba_down_post(struct lpfc_hba *phba)
925 {
926 return (*phba->lpfc_hba_down_post)(phba);
927 }
928
929 /**
930 * lpfc_hb_timeout - The HBA-timer timeout handler
931 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
932 *
933 * This is the HBA-timer timeout handler registered to the lpfc driver. When
934 * this timer fires, a HBA timeout event shall be posted to the lpfc driver
935 * work-port-events bitmap and the worker thread is notified. This timeout
936 * event will be used by the worker thread to invoke the actual timeout
937 * handler routine, lpfc_hb_timeout_handler. Any periodical operations will
938 * be performed in the timeout handler and the HBA timeout event bit shall
939 * be cleared by the worker thread after it has taken the event bitmap out.
940 **/
941 static void
942 lpfc_hb_timeout(unsigned long ptr)
943 {
944 struct lpfc_hba *phba;
945 uint32_t tmo_posted;
946 unsigned long iflag;
947
948 phba = (struct lpfc_hba *)ptr;
949
950 /* Check for heart beat timeout conditions */
951 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
952 tmo_posted = phba->pport->work_port_events & WORKER_HB_TMO;
953 if (!tmo_posted)
954 phba->pport->work_port_events |= WORKER_HB_TMO;
955 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
956
957 /* Tell the worker thread there is work to do */
958 if (!tmo_posted)
959 lpfc_worker_wake_up(phba);
960 return;
961 }
962
963 /**
964 * lpfc_rrq_timeout - The RRQ-timer timeout handler
965 * @ptr: unsigned long holds the pointer to lpfc hba data structure.
966 *
967 * This is the RRQ-timer timeout handler registered to the lpfc driver. When
968 * this timer fires, a RRQ timeout event shall be posted to the lpfc driver
969 * work-port-events bitmap and the worker thread is notified. This timeout
970 * event will be used by the worker thread to invoke the actual timeout
971 * handler routine, lpfc_rrq_handler. Any periodical operations will
972 * be performed in the timeout handler and the RRQ timeout event bit shall
973 * be cleared by the worker thread after it has taken the event bitmap out.
974 **/
975 static void
976 lpfc_rrq_timeout(unsigned long ptr)
977 {
978 struct lpfc_hba *phba;
979 unsigned long iflag;
980
981 phba = (struct lpfc_hba *)ptr;
982 spin_lock_irqsave(&phba->pport->work_port_lock, iflag);
983 phba->hba_flag |= HBA_RRQ_ACTIVE;
984 spin_unlock_irqrestore(&phba->pport->work_port_lock, iflag);
985 lpfc_worker_wake_up(phba);
986 }
987
988 /**
989 * lpfc_hb_mbox_cmpl - The lpfc heart-beat mailbox command callback function
990 * @phba: pointer to lpfc hba data structure.
991 * @pmboxq: pointer to the driver internal queue element for mailbox command.
992 *
993 * This is the callback function to the lpfc heart-beat mailbox command.
994 * If configured, the lpfc driver issues the heart-beat mailbox command to
995 * the HBA every LPFC_HB_MBOX_INTERVAL (current 5) seconds. At the time the
996 * heart-beat mailbox command is issued, the driver shall set up heart-beat
997 * timeout timer to LPFC_HB_MBOX_TIMEOUT (current 30) seconds and marks
998 * heart-beat outstanding state. Once the mailbox command comes back and
999 * no error conditions detected, the heart-beat mailbox command timer is
1000 * reset to LPFC_HB_MBOX_INTERVAL seconds and the heart-beat outstanding
1001 * state is cleared for the next heart-beat. If the timer expired with the
1002 * heart-beat outstanding state set, the driver will put the HBA offline.
1003 **/
1004 static void
1005 lpfc_hb_mbox_cmpl(struct lpfc_hba * phba, LPFC_MBOXQ_t * pmboxq)
1006 {
1007 unsigned long drvr_flag;
1008
1009 spin_lock_irqsave(&phba->hbalock, drvr_flag);
1010 phba->hb_outstanding = 0;
1011 spin_unlock_irqrestore(&phba->hbalock, drvr_flag);
1012
1013 /* Check and reset heart-beat timer is necessary */
1014 mempool_free(pmboxq, phba->mbox_mem_pool);
1015 if (!(phba->pport->fc_flag & FC_OFFLINE_MODE) &&
1016 !(phba->link_state == LPFC_HBA_ERROR) &&
1017 !(phba->pport->load_flag & FC_UNLOADING))
1018 mod_timer(&phba->hb_tmofunc,
1019 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1020 return;
1021 }
1022
1023 /**
1024 * lpfc_hb_timeout_handler - The HBA-timer timeout handler
1025 * @phba: pointer to lpfc hba data structure.
1026 *
1027 * This is the actual HBA-timer timeout handler to be invoked by the worker
1028 * thread whenever the HBA timer fired and HBA-timeout event posted. This
1029 * handler performs any periodic operations needed for the device. If such
1030 * periodic event has already been attended to either in the interrupt handler
1031 * or by processing slow-ring or fast-ring events within the HBA-timer
1032 * timeout window (LPFC_HB_MBOX_INTERVAL), this handler just simply resets
1033 * the timer for the next timeout period. If lpfc heart-beat mailbox command
1034 * is configured and there is no heart-beat mailbox command outstanding, a
1035 * heart-beat mailbox is issued and timer set properly. Otherwise, if there
1036 * has been a heart-beat mailbox command outstanding, the HBA shall be put
1037 * to offline.
1038 **/
1039 void
1040 lpfc_hb_timeout_handler(struct lpfc_hba *phba)
1041 {
1042 struct lpfc_vport **vports;
1043 LPFC_MBOXQ_t *pmboxq;
1044 struct lpfc_dmabuf *buf_ptr;
1045 int retval, i;
1046 struct lpfc_sli *psli = &phba->sli;
1047 LIST_HEAD(completions);
1048
1049 vports = lpfc_create_vport_work_array(phba);
1050 if (vports != NULL)
1051 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
1052 lpfc_rcv_seq_check_edtov(vports[i]);
1053 lpfc_destroy_vport_work_array(phba, vports);
1054
1055 if ((phba->link_state == LPFC_HBA_ERROR) ||
1056 (phba->pport->load_flag & FC_UNLOADING) ||
1057 (phba->pport->fc_flag & FC_OFFLINE_MODE))
1058 return;
1059
1060 spin_lock_irq(&phba->pport->work_port_lock);
1061
1062 if (time_after(phba->last_completion_time + LPFC_HB_MBOX_INTERVAL * HZ,
1063 jiffies)) {
1064 spin_unlock_irq(&phba->pport->work_port_lock);
1065 if (!phba->hb_outstanding)
1066 mod_timer(&phba->hb_tmofunc,
1067 jiffies + HZ * LPFC_HB_MBOX_INTERVAL);
1068 else
1069 mod_timer(&phba->hb_tmofunc,
1070 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1071 return;
1072 }
1073 spin_unlock_irq(&phba->pport->work_port_lock);
1074
1075 if (phba->elsbuf_cnt &&
1076 (phba->elsbuf_cnt == phba->elsbuf_prev_cnt)) {
1077 spin_lock_irq(&phba->hbalock);
1078 list_splice_init(&phba->elsbuf, &completions);
1079 phba->elsbuf_cnt = 0;
1080 phba->elsbuf_prev_cnt = 0;
1081 spin_unlock_irq(&phba->hbalock);
1082
1083 while (!list_empty(&completions)) {
1084 list_remove_head(&completions, buf_ptr,
1085 struct lpfc_dmabuf, list);
1086 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
1087 kfree(buf_ptr);
1088 }
1089 }
1090 phba->elsbuf_prev_cnt = phba->elsbuf_cnt;
1091
1092 /* If there is no heart beat outstanding, issue a heartbeat command */
1093 if (phba->cfg_enable_hba_heartbeat) {
1094 if (!phba->hb_outstanding) {
1095 if ((!(psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) &&
1096 (list_empty(&psli->mboxq))) {
1097 pmboxq = mempool_alloc(phba->mbox_mem_pool,
1098 GFP_KERNEL);
1099 if (!pmboxq) {
1100 mod_timer(&phba->hb_tmofunc,
1101 jiffies +
1102 HZ * LPFC_HB_MBOX_INTERVAL);
1103 return;
1104 }
1105
1106 lpfc_heart_beat(phba, pmboxq);
1107 pmboxq->mbox_cmpl = lpfc_hb_mbox_cmpl;
1108 pmboxq->vport = phba->pport;
1109 retval = lpfc_sli_issue_mbox(phba, pmboxq,
1110 MBX_NOWAIT);
1111
1112 if (retval != MBX_BUSY &&
1113 retval != MBX_SUCCESS) {
1114 mempool_free(pmboxq,
1115 phba->mbox_mem_pool);
1116 mod_timer(&phba->hb_tmofunc,
1117 jiffies +
1118 HZ * LPFC_HB_MBOX_INTERVAL);
1119 return;
1120 }
1121 phba->skipped_hb = 0;
1122 phba->hb_outstanding = 1;
1123 } else if (time_before_eq(phba->last_completion_time,
1124 phba->skipped_hb)) {
1125 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1126 "2857 Last completion time not "
1127 " updated in %d ms\n",
1128 jiffies_to_msecs(jiffies
1129 - phba->last_completion_time));
1130 } else
1131 phba->skipped_hb = jiffies;
1132
1133 mod_timer(&phba->hb_tmofunc,
1134 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1135 return;
1136 } else {
1137 /*
1138 * If heart beat timeout called with hb_outstanding set
1139 * we need to give the hb mailbox cmd a chance to
1140 * complete or TMO.
1141 */
1142 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1143 "0459 Adapter heartbeat still out"
1144 "standing:last compl time was %d ms.\n",
1145 jiffies_to_msecs(jiffies
1146 - phba->last_completion_time));
1147 mod_timer(&phba->hb_tmofunc,
1148 jiffies + HZ * LPFC_HB_MBOX_TIMEOUT);
1149 }
1150 }
1151 }
1152
1153 /**
1154 * lpfc_offline_eratt - Bring lpfc offline on hardware error attention
1155 * @phba: pointer to lpfc hba data structure.
1156 *
1157 * This routine is called to bring the HBA offline when HBA hardware error
1158 * other than Port Error 6 has been detected.
1159 **/
1160 static void
1161 lpfc_offline_eratt(struct lpfc_hba *phba)
1162 {
1163 struct lpfc_sli *psli = &phba->sli;
1164
1165 spin_lock_irq(&phba->hbalock);
1166 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1167 spin_unlock_irq(&phba->hbalock);
1168 lpfc_offline_prep(phba);
1169
1170 lpfc_offline(phba);
1171 lpfc_reset_barrier(phba);
1172 spin_lock_irq(&phba->hbalock);
1173 lpfc_sli_brdreset(phba);
1174 spin_unlock_irq(&phba->hbalock);
1175 lpfc_hba_down_post(phba);
1176 lpfc_sli_brdready(phba, HS_MBRDY);
1177 lpfc_unblock_mgmt_io(phba);
1178 phba->link_state = LPFC_HBA_ERROR;
1179 return;
1180 }
1181
1182 /**
1183 * lpfc_sli4_offline_eratt - Bring lpfc offline on SLI4 hardware error attention
1184 * @phba: pointer to lpfc hba data structure.
1185 *
1186 * This routine is called to bring a SLI4 HBA offline when HBA hardware error
1187 * other than Port Error 6 has been detected.
1188 **/
1189 static void
1190 lpfc_sli4_offline_eratt(struct lpfc_hba *phba)
1191 {
1192 lpfc_offline_prep(phba);
1193 lpfc_offline(phba);
1194 lpfc_sli4_brdreset(phba);
1195 lpfc_hba_down_post(phba);
1196 lpfc_sli4_post_status_check(phba);
1197 lpfc_unblock_mgmt_io(phba);
1198 phba->link_state = LPFC_HBA_ERROR;
1199 }
1200
1201 /**
1202 * lpfc_handle_deferred_eratt - The HBA hardware deferred error handler
1203 * @phba: pointer to lpfc hba data structure.
1204 *
1205 * This routine is invoked to handle the deferred HBA hardware error
1206 * conditions. This type of error is indicated by HBA by setting ER1
1207 * and another ER bit in the host status register. The driver will
1208 * wait until the ER1 bit clears before handling the error condition.
1209 **/
1210 static void
1211 lpfc_handle_deferred_eratt(struct lpfc_hba *phba)
1212 {
1213 uint32_t old_host_status = phba->work_hs;
1214 struct lpfc_sli_ring *pring;
1215 struct lpfc_sli *psli = &phba->sli;
1216
1217 /* If the pci channel is offline, ignore possible errors,
1218 * since we cannot communicate with the pci card anyway.
1219 */
1220 if (pci_channel_offline(phba->pcidev)) {
1221 spin_lock_irq(&phba->hbalock);
1222 phba->hba_flag &= ~DEFER_ERATT;
1223 spin_unlock_irq(&phba->hbalock);
1224 return;
1225 }
1226
1227 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1228 "0479 Deferred Adapter Hardware Error "
1229 "Data: x%x x%x x%x\n",
1230 phba->work_hs,
1231 phba->work_status[0], phba->work_status[1]);
1232
1233 spin_lock_irq(&phba->hbalock);
1234 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1235 spin_unlock_irq(&phba->hbalock);
1236
1237
1238 /*
1239 * Firmware stops when it triggred erratt. That could cause the I/Os
1240 * dropped by the firmware. Error iocb (I/O) on txcmplq and let the
1241 * SCSI layer retry it after re-establishing link.
1242 */
1243 pring = &psli->ring[psli->fcp_ring];
1244 lpfc_sli_abort_iocb_ring(phba, pring);
1245
1246 /*
1247 * There was a firmware error. Take the hba offline and then
1248 * attempt to restart it.
1249 */
1250 lpfc_offline_prep(phba);
1251 lpfc_offline(phba);
1252
1253 /* Wait for the ER1 bit to clear.*/
1254 while (phba->work_hs & HS_FFER1) {
1255 msleep(100);
1256 if (lpfc_readl(phba->HSregaddr, &phba->work_hs)) {
1257 phba->work_hs = UNPLUG_ERR ;
1258 break;
1259 }
1260 /* If driver is unloading let the worker thread continue */
1261 if (phba->pport->load_flag & FC_UNLOADING) {
1262 phba->work_hs = 0;
1263 break;
1264 }
1265 }
1266
1267 /*
1268 * This is to ptrotect against a race condition in which
1269 * first write to the host attention register clear the
1270 * host status register.
1271 */
1272 if ((!phba->work_hs) && (!(phba->pport->load_flag & FC_UNLOADING)))
1273 phba->work_hs = old_host_status & ~HS_FFER1;
1274
1275 spin_lock_irq(&phba->hbalock);
1276 phba->hba_flag &= ~DEFER_ERATT;
1277 spin_unlock_irq(&phba->hbalock);
1278 phba->work_status[0] = readl(phba->MBslimaddr + 0xa8);
1279 phba->work_status[1] = readl(phba->MBslimaddr + 0xac);
1280 }
1281
1282 static void
1283 lpfc_board_errevt_to_mgmt(struct lpfc_hba *phba)
1284 {
1285 struct lpfc_board_event_header board_event;
1286 struct Scsi_Host *shost;
1287
1288 board_event.event_type = FC_REG_BOARD_EVENT;
1289 board_event.subcategory = LPFC_EVENT_PORTINTERR;
1290 shost = lpfc_shost_from_vport(phba->pport);
1291 fc_host_post_vendor_event(shost, fc_get_event_number(),
1292 sizeof(board_event),
1293 (char *) &board_event,
1294 LPFC_NL_VENDOR_ID);
1295 }
1296
1297 /**
1298 * lpfc_handle_eratt_s3 - The SLI3 HBA hardware error handler
1299 * @phba: pointer to lpfc hba data structure.
1300 *
1301 * This routine is invoked to handle the following HBA hardware error
1302 * conditions:
1303 * 1 - HBA error attention interrupt
1304 * 2 - DMA ring index out of range
1305 * 3 - Mailbox command came back as unknown
1306 **/
1307 static void
1308 lpfc_handle_eratt_s3(struct lpfc_hba *phba)
1309 {
1310 struct lpfc_vport *vport = phba->pport;
1311 struct lpfc_sli *psli = &phba->sli;
1312 struct lpfc_sli_ring *pring;
1313 uint32_t event_data;
1314 unsigned long temperature;
1315 struct temp_event temp_event_data;
1316 struct Scsi_Host *shost;
1317
1318 /* If the pci channel is offline, ignore possible errors,
1319 * since we cannot communicate with the pci card anyway.
1320 */
1321 if (pci_channel_offline(phba->pcidev)) {
1322 spin_lock_irq(&phba->hbalock);
1323 phba->hba_flag &= ~DEFER_ERATT;
1324 spin_unlock_irq(&phba->hbalock);
1325 return;
1326 }
1327
1328 /* If resets are disabled then leave the HBA alone and return */
1329 if (!phba->cfg_enable_hba_reset)
1330 return;
1331
1332 /* Send an internal error event to mgmt application */
1333 lpfc_board_errevt_to_mgmt(phba);
1334
1335 if (phba->hba_flag & DEFER_ERATT)
1336 lpfc_handle_deferred_eratt(phba);
1337
1338 if ((phba->work_hs & HS_FFER6) || (phba->work_hs & HS_FFER8)) {
1339 if (phba->work_hs & HS_FFER6)
1340 /* Re-establishing Link */
1341 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1342 "1301 Re-establishing Link "
1343 "Data: x%x x%x x%x\n",
1344 phba->work_hs, phba->work_status[0],
1345 phba->work_status[1]);
1346 if (phba->work_hs & HS_FFER8)
1347 /* Device Zeroization */
1348 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
1349 "2861 Host Authentication device "
1350 "zeroization Data:x%x x%x x%x\n",
1351 phba->work_hs, phba->work_status[0],
1352 phba->work_status[1]);
1353
1354 spin_lock_irq(&phba->hbalock);
1355 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
1356 spin_unlock_irq(&phba->hbalock);
1357
1358 /*
1359 * Firmware stops when it triggled erratt with HS_FFER6.
1360 * That could cause the I/Os dropped by the firmware.
1361 * Error iocb (I/O) on txcmplq and let the SCSI layer
1362 * retry it after re-establishing link.
1363 */
1364 pring = &psli->ring[psli->fcp_ring];
1365 lpfc_sli_abort_iocb_ring(phba, pring);
1366
1367 /*
1368 * There was a firmware error. Take the hba offline and then
1369 * attempt to restart it.
1370 */
1371 lpfc_offline_prep(phba);
1372 lpfc_offline(phba);
1373 lpfc_sli_brdrestart(phba);
1374 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
1375 lpfc_unblock_mgmt_io(phba);
1376 return;
1377 }
1378 lpfc_unblock_mgmt_io(phba);
1379 } else if (phba->work_hs & HS_CRIT_TEMP) {
1380 temperature = readl(phba->MBslimaddr + TEMPERATURE_OFFSET);
1381 temp_event_data.event_type = FC_REG_TEMPERATURE_EVENT;
1382 temp_event_data.event_code = LPFC_CRIT_TEMP;
1383 temp_event_data.data = (uint32_t)temperature;
1384
1385 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1386 "0406 Adapter maximum temperature exceeded "
1387 "(%ld), taking this port offline "
1388 "Data: x%x x%x x%x\n",
1389 temperature, phba->work_hs,
1390 phba->work_status[0], phba->work_status[1]);
1391
1392 shost = lpfc_shost_from_vport(phba->pport);
1393 fc_host_post_vendor_event(shost, fc_get_event_number(),
1394 sizeof(temp_event_data),
1395 (char *) &temp_event_data,
1396 SCSI_NL_VID_TYPE_PCI
1397 | PCI_VENDOR_ID_EMULEX);
1398
1399 spin_lock_irq(&phba->hbalock);
1400 phba->over_temp_state = HBA_OVER_TEMP;
1401 spin_unlock_irq(&phba->hbalock);
1402 lpfc_offline_eratt(phba);
1403
1404 } else {
1405 /* The if clause above forces this code path when the status
1406 * failure is a value other than FFER6. Do not call the offline
1407 * twice. This is the adapter hardware error path.
1408 */
1409 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1410 "0457 Adapter Hardware Error "
1411 "Data: x%x x%x x%x\n",
1412 phba->work_hs,
1413 phba->work_status[0], phba->work_status[1]);
1414
1415 event_data = FC_REG_DUMP_EVENT;
1416 shost = lpfc_shost_from_vport(vport);
1417 fc_host_post_vendor_event(shost, fc_get_event_number(),
1418 sizeof(event_data), (char *) &event_data,
1419 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1420
1421 lpfc_offline_eratt(phba);
1422 }
1423 return;
1424 }
1425
1426 /**
1427 * lpfc_handle_eratt_s4 - The SLI4 HBA hardware error handler
1428 * @phba: pointer to lpfc hba data structure.
1429 *
1430 * This routine is invoked to handle the SLI4 HBA hardware error attention
1431 * conditions.
1432 **/
1433 static void
1434 lpfc_handle_eratt_s4(struct lpfc_hba *phba)
1435 {
1436 struct lpfc_vport *vport = phba->pport;
1437 uint32_t event_data;
1438 struct Scsi_Host *shost;
1439 uint32_t if_type;
1440 struct lpfc_register portstat_reg;
1441 int rc;
1442
1443 /* If the pci channel is offline, ignore possible errors, since
1444 * we cannot communicate with the pci card anyway.
1445 */
1446 if (pci_channel_offline(phba->pcidev))
1447 return;
1448 /* If resets are disabled then leave the HBA alone and return */
1449 if (!phba->cfg_enable_hba_reset)
1450 return;
1451
1452 /* Send an internal error event to mgmt application */
1453 lpfc_board_errevt_to_mgmt(phba);
1454
1455 /* For now, the actual action for SLI4 device handling is not
1456 * specified yet, just treated it as adaptor hardware failure
1457 */
1458 event_data = FC_REG_DUMP_EVENT;
1459 shost = lpfc_shost_from_vport(vport);
1460 fc_host_post_vendor_event(shost, fc_get_event_number(),
1461 sizeof(event_data), (char *) &event_data,
1462 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
1463
1464 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
1465 switch (if_type) {
1466 case LPFC_SLI_INTF_IF_TYPE_0:
1467 lpfc_sli4_offline_eratt(phba);
1468 break;
1469 case LPFC_SLI_INTF_IF_TYPE_2:
1470 portstat_reg.word0 =
1471 readl(phba->sli4_hba.u.if_type2.STATUSregaddr);
1472
1473 if (bf_get(lpfc_sliport_status_oti, &portstat_reg)) {
1474 /* TODO: Register for Overtemp async events. */
1475 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1476 "2889 Port Overtemperature event, "
1477 "taking port\n");
1478 spin_lock_irq(&phba->hbalock);
1479 phba->over_temp_state = HBA_OVER_TEMP;
1480 spin_unlock_irq(&phba->hbalock);
1481 lpfc_sli4_offline_eratt(phba);
1482 return;
1483 }
1484 /*
1485 * On error status condition, driver need to wait for port
1486 * ready before performing reset.
1487 */
1488 rc = lpfc_sli4_pdev_status_reg_wait(phba);
1489 if (!rc) {
1490 /* need reset: attempt for port recovery */
1491 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1492 "2887 Port Error: Attempting "
1493 "Port Recovery\n");
1494 lpfc_offline_prep(phba);
1495 lpfc_offline(phba);
1496 lpfc_sli_brdrestart(phba);
1497 if (lpfc_online(phba) == 0) {
1498 lpfc_unblock_mgmt_io(phba);
1499 return;
1500 }
1501 /* fall through for not able to recover */
1502 }
1503 lpfc_sli4_offline_eratt(phba);
1504 break;
1505 case LPFC_SLI_INTF_IF_TYPE_1:
1506 default:
1507 break;
1508 }
1509 }
1510
1511 /**
1512 * lpfc_handle_eratt - Wrapper func for handling hba error attention
1513 * @phba: pointer to lpfc HBA data structure.
1514 *
1515 * This routine wraps the actual SLI3 or SLI4 hba error attention handling
1516 * routine from the API jump table function pointer from the lpfc_hba struct.
1517 *
1518 * Return codes
1519 * 0 - success.
1520 * Any other value - error.
1521 **/
1522 void
1523 lpfc_handle_eratt(struct lpfc_hba *phba)
1524 {
1525 (*phba->lpfc_handle_eratt)(phba);
1526 }
1527
1528 /**
1529 * lpfc_handle_latt - The HBA link event handler
1530 * @phba: pointer to lpfc hba data structure.
1531 *
1532 * This routine is invoked from the worker thread to handle a HBA host
1533 * attention link event.
1534 **/
1535 void
1536 lpfc_handle_latt(struct lpfc_hba *phba)
1537 {
1538 struct lpfc_vport *vport = phba->pport;
1539 struct lpfc_sli *psli = &phba->sli;
1540 LPFC_MBOXQ_t *pmb;
1541 volatile uint32_t control;
1542 struct lpfc_dmabuf *mp;
1543 int rc = 0;
1544
1545 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1546 if (!pmb) {
1547 rc = 1;
1548 goto lpfc_handle_latt_err_exit;
1549 }
1550
1551 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
1552 if (!mp) {
1553 rc = 2;
1554 goto lpfc_handle_latt_free_pmb;
1555 }
1556
1557 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
1558 if (!mp->virt) {
1559 rc = 3;
1560 goto lpfc_handle_latt_free_mp;
1561 }
1562
1563 /* Cleanup any outstanding ELS commands */
1564 lpfc_els_flush_all_cmd(phba);
1565
1566 psli->slistat.link_event++;
1567 lpfc_read_topology(phba, pmb, mp);
1568 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
1569 pmb->vport = vport;
1570 /* Block ELS IOCBs until we have processed this mbox command */
1571 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
1572 rc = lpfc_sli_issue_mbox (phba, pmb, MBX_NOWAIT);
1573 if (rc == MBX_NOT_FINISHED) {
1574 rc = 4;
1575 goto lpfc_handle_latt_free_mbuf;
1576 }
1577
1578 /* Clear Link Attention in HA REG */
1579 spin_lock_irq(&phba->hbalock);
1580 writel(HA_LATT, phba->HAregaddr);
1581 readl(phba->HAregaddr); /* flush */
1582 spin_unlock_irq(&phba->hbalock);
1583
1584 return;
1585
1586 lpfc_handle_latt_free_mbuf:
1587 phba->sli.ring[LPFC_ELS_RING].flag &= ~LPFC_STOP_IOCB_EVENT;
1588 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1589 lpfc_handle_latt_free_mp:
1590 kfree(mp);
1591 lpfc_handle_latt_free_pmb:
1592 mempool_free(pmb, phba->mbox_mem_pool);
1593 lpfc_handle_latt_err_exit:
1594 /* Enable Link attention interrupts */
1595 spin_lock_irq(&phba->hbalock);
1596 psli->sli_flag |= LPFC_PROCESS_LA;
1597 control = readl(phba->HCregaddr);
1598 control |= HC_LAINT_ENA;
1599 writel(control, phba->HCregaddr);
1600 readl(phba->HCregaddr); /* flush */
1601
1602 /* Clear Link Attention in HA REG */
1603 writel(HA_LATT, phba->HAregaddr);
1604 readl(phba->HAregaddr); /* flush */
1605 spin_unlock_irq(&phba->hbalock);
1606 lpfc_linkdown(phba);
1607 phba->link_state = LPFC_HBA_ERROR;
1608
1609 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX,
1610 "0300 LATT: Cannot issue READ_LA: Data:%d\n", rc);
1611
1612 return;
1613 }
1614
1615 /**
1616 * lpfc_parse_vpd - Parse VPD (Vital Product Data)
1617 * @phba: pointer to lpfc hba data structure.
1618 * @vpd: pointer to the vital product data.
1619 * @len: length of the vital product data in bytes.
1620 *
1621 * This routine parses the Vital Product Data (VPD). The VPD is treated as
1622 * an array of characters. In this routine, the ModelName, ProgramType, and
1623 * ModelDesc, etc. fields of the phba data structure will be populated.
1624 *
1625 * Return codes
1626 * 0 - pointer to the VPD passed in is NULL
1627 * 1 - success
1628 **/
1629 int
1630 lpfc_parse_vpd(struct lpfc_hba *phba, uint8_t *vpd, int len)
1631 {
1632 uint8_t lenlo, lenhi;
1633 int Length;
1634 int i, j;
1635 int finished = 0;
1636 int index = 0;
1637
1638 if (!vpd)
1639 return 0;
1640
1641 /* Vital Product */
1642 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
1643 "0455 Vital Product Data: x%x x%x x%x x%x\n",
1644 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
1645 (uint32_t) vpd[3]);
1646 while (!finished && (index < (len - 4))) {
1647 switch (vpd[index]) {
1648 case 0x82:
1649 case 0x91:
1650 index += 1;
1651 lenlo = vpd[index];
1652 index += 1;
1653 lenhi = vpd[index];
1654 index += 1;
1655 i = ((((unsigned short)lenhi) << 8) + lenlo);
1656 index += i;
1657 break;
1658 case 0x90:
1659 index += 1;
1660 lenlo = vpd[index];
1661 index += 1;
1662 lenhi = vpd[index];
1663 index += 1;
1664 Length = ((((unsigned short)lenhi) << 8) + lenlo);
1665 if (Length > len - index)
1666 Length = len - index;
1667 while (Length > 0) {
1668 /* Look for Serial Number */
1669 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
1670 index += 2;
1671 i = vpd[index];
1672 index += 1;
1673 j = 0;
1674 Length -= (3+i);
1675 while(i--) {
1676 phba->SerialNumber[j++] = vpd[index++];
1677 if (j == 31)
1678 break;
1679 }
1680 phba->SerialNumber[j] = 0;
1681 continue;
1682 }
1683 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
1684 phba->vpd_flag |= VPD_MODEL_DESC;
1685 index += 2;
1686 i = vpd[index];
1687 index += 1;
1688 j = 0;
1689 Length -= (3+i);
1690 while(i--) {
1691 phba->ModelDesc[j++] = vpd[index++];
1692 if (j == 255)
1693 break;
1694 }
1695 phba->ModelDesc[j] = 0;
1696 continue;
1697 }
1698 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
1699 phba->vpd_flag |= VPD_MODEL_NAME;
1700 index += 2;
1701 i = vpd[index];
1702 index += 1;
1703 j = 0;
1704 Length -= (3+i);
1705 while(i--) {
1706 phba->ModelName[j++] = vpd[index++];
1707 if (j == 79)
1708 break;
1709 }
1710 phba->ModelName[j] = 0;
1711 continue;
1712 }
1713 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
1714 phba->vpd_flag |= VPD_PROGRAM_TYPE;
1715 index += 2;
1716 i = vpd[index];
1717 index += 1;
1718 j = 0;
1719 Length -= (3+i);
1720 while(i--) {
1721 phba->ProgramType[j++] = vpd[index++];
1722 if (j == 255)
1723 break;
1724 }
1725 phba->ProgramType[j] = 0;
1726 continue;
1727 }
1728 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
1729 phba->vpd_flag |= VPD_PORT;
1730 index += 2;
1731 i = vpd[index];
1732 index += 1;
1733 j = 0;
1734 Length -= (3+i);
1735 while(i--) {
1736 phba->Port[j++] = vpd[index++];
1737 if (j == 19)
1738 break;
1739 }
1740 phba->Port[j] = 0;
1741 continue;
1742 }
1743 else {
1744 index += 2;
1745 i = vpd[index];
1746 index += 1;
1747 index += i;
1748 Length -= (3 + i);
1749 }
1750 }
1751 finished = 0;
1752 break;
1753 case 0x78:
1754 finished = 1;
1755 break;
1756 default:
1757 index ++;
1758 break;
1759 }
1760 }
1761
1762 return(1);
1763 }
1764
1765 /**
1766 * lpfc_get_hba_model_desc - Retrieve HBA device model name and description
1767 * @phba: pointer to lpfc hba data structure.
1768 * @mdp: pointer to the data structure to hold the derived model name.
1769 * @descp: pointer to the data structure to hold the derived description.
1770 *
1771 * This routine retrieves HBA's description based on its registered PCI device
1772 * ID. The @descp passed into this function points to an array of 256 chars. It
1773 * shall be returned with the model name, maximum speed, and the host bus type.
1774 * The @mdp passed into this function points to an array of 80 chars. When the
1775 * function returns, the @mdp will be filled with the model name.
1776 **/
1777 static void
1778 lpfc_get_hba_model_desc(struct lpfc_hba *phba, uint8_t *mdp, uint8_t *descp)
1779 {
1780 lpfc_vpd_t *vp;
1781 uint16_t dev_id = phba->pcidev->device;
1782 int max_speed;
1783 int GE = 0;
1784 int oneConnect = 0; /* default is not a oneConnect */
1785 struct {
1786 char *name;
1787 char *bus;
1788 char *function;
1789 } m = {"<Unknown>", "", ""};
1790
1791 if (mdp && mdp[0] != '\0'
1792 && descp && descp[0] != '\0')
1793 return;
1794
1795 if (phba->lmt & LMT_16Gb)
1796 max_speed = 16;
1797 else if (phba->lmt & LMT_10Gb)
1798 max_speed = 10;
1799 else if (phba->lmt & LMT_8Gb)
1800 max_speed = 8;
1801 else if (phba->lmt & LMT_4Gb)
1802 max_speed = 4;
1803 else if (phba->lmt & LMT_2Gb)
1804 max_speed = 2;
1805 else
1806 max_speed = 1;
1807
1808 vp = &phba->vpd;
1809
1810 switch (dev_id) {
1811 case PCI_DEVICE_ID_FIREFLY:
1812 m = (typeof(m)){"LP6000", "PCI", "Fibre Channel Adapter"};
1813 break;
1814 case PCI_DEVICE_ID_SUPERFLY:
1815 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
1816 m = (typeof(m)){"LP7000", "PCI",
1817 "Fibre Channel Adapter"};
1818 else
1819 m = (typeof(m)){"LP7000E", "PCI",
1820 "Fibre Channel Adapter"};
1821 break;
1822 case PCI_DEVICE_ID_DRAGONFLY:
1823 m = (typeof(m)){"LP8000", "PCI",
1824 "Fibre Channel Adapter"};
1825 break;
1826 case PCI_DEVICE_ID_CENTAUR:
1827 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
1828 m = (typeof(m)){"LP9002", "PCI",
1829 "Fibre Channel Adapter"};
1830 else
1831 m = (typeof(m)){"LP9000", "PCI",
1832 "Fibre Channel Adapter"};
1833 break;
1834 case PCI_DEVICE_ID_RFLY:
1835 m = (typeof(m)){"LP952", "PCI",
1836 "Fibre Channel Adapter"};
1837 break;
1838 case PCI_DEVICE_ID_PEGASUS:
1839 m = (typeof(m)){"LP9802", "PCI-X",
1840 "Fibre Channel Adapter"};
1841 break;
1842 case PCI_DEVICE_ID_THOR:
1843 m = (typeof(m)){"LP10000", "PCI-X",
1844 "Fibre Channel Adapter"};
1845 break;
1846 case PCI_DEVICE_ID_VIPER:
1847 m = (typeof(m)){"LPX1000", "PCI-X",
1848 "Fibre Channel Adapter"};
1849 break;
1850 case PCI_DEVICE_ID_PFLY:
1851 m = (typeof(m)){"LP982", "PCI-X",
1852 "Fibre Channel Adapter"};
1853 break;
1854 case PCI_DEVICE_ID_TFLY:
1855 m = (typeof(m)){"LP1050", "PCI-X",
1856 "Fibre Channel Adapter"};
1857 break;
1858 case PCI_DEVICE_ID_HELIOS:
1859 m = (typeof(m)){"LP11000", "PCI-X2",
1860 "Fibre Channel Adapter"};
1861 break;
1862 case PCI_DEVICE_ID_HELIOS_SCSP:
1863 m = (typeof(m)){"LP11000-SP", "PCI-X2",
1864 "Fibre Channel Adapter"};
1865 break;
1866 case PCI_DEVICE_ID_HELIOS_DCSP:
1867 m = (typeof(m)){"LP11002-SP", "PCI-X2",
1868 "Fibre Channel Adapter"};
1869 break;
1870 case PCI_DEVICE_ID_NEPTUNE:
1871 m = (typeof(m)){"LPe1000", "PCIe", "Fibre Channel Adapter"};
1872 break;
1873 case PCI_DEVICE_ID_NEPTUNE_SCSP:
1874 m = (typeof(m)){"LPe1000-SP", "PCIe", "Fibre Channel Adapter"};
1875 break;
1876 case PCI_DEVICE_ID_NEPTUNE_DCSP:
1877 m = (typeof(m)){"LPe1002-SP", "PCIe", "Fibre Channel Adapter"};
1878 break;
1879 case PCI_DEVICE_ID_BMID:
1880 m = (typeof(m)){"LP1150", "PCI-X2", "Fibre Channel Adapter"};
1881 break;
1882 case PCI_DEVICE_ID_BSMB:
1883 m = (typeof(m)){"LP111", "PCI-X2", "Fibre Channel Adapter"};
1884 break;
1885 case PCI_DEVICE_ID_ZEPHYR:
1886 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1887 break;
1888 case PCI_DEVICE_ID_ZEPHYR_SCSP:
1889 m = (typeof(m)){"LPe11000", "PCIe", "Fibre Channel Adapter"};
1890 break;
1891 case PCI_DEVICE_ID_ZEPHYR_DCSP:
1892 m = (typeof(m)){"LP2105", "PCIe", "FCoE Adapter"};
1893 GE = 1;
1894 break;
1895 case PCI_DEVICE_ID_ZMID:
1896 m = (typeof(m)){"LPe1150", "PCIe", "Fibre Channel Adapter"};
1897 break;
1898 case PCI_DEVICE_ID_ZSMB:
1899 m = (typeof(m)){"LPe111", "PCIe", "Fibre Channel Adapter"};
1900 break;
1901 case PCI_DEVICE_ID_LP101:
1902 m = (typeof(m)){"LP101", "PCI-X", "Fibre Channel Adapter"};
1903 break;
1904 case PCI_DEVICE_ID_LP10000S:
1905 m = (typeof(m)){"LP10000-S", "PCI", "Fibre Channel Adapter"};
1906 break;
1907 case PCI_DEVICE_ID_LP11000S:
1908 m = (typeof(m)){"LP11000-S", "PCI-X2", "Fibre Channel Adapter"};
1909 break;
1910 case PCI_DEVICE_ID_LPE11000S:
1911 m = (typeof(m)){"LPe11000-S", "PCIe", "Fibre Channel Adapter"};
1912 break;
1913 case PCI_DEVICE_ID_SAT:
1914 m = (typeof(m)){"LPe12000", "PCIe", "Fibre Channel Adapter"};
1915 break;
1916 case PCI_DEVICE_ID_SAT_MID:
1917 m = (typeof(m)){"LPe1250", "PCIe", "Fibre Channel Adapter"};
1918 break;
1919 case PCI_DEVICE_ID_SAT_SMB:
1920 m = (typeof(m)){"LPe121", "PCIe", "Fibre Channel Adapter"};
1921 break;
1922 case PCI_DEVICE_ID_SAT_DCSP:
1923 m = (typeof(m)){"LPe12002-SP", "PCIe", "Fibre Channel Adapter"};
1924 break;
1925 case PCI_DEVICE_ID_SAT_SCSP:
1926 m = (typeof(m)){"LPe12000-SP", "PCIe", "Fibre Channel Adapter"};
1927 break;
1928 case PCI_DEVICE_ID_SAT_S:
1929 m = (typeof(m)){"LPe12000-S", "PCIe", "Fibre Channel Adapter"};
1930 break;
1931 case PCI_DEVICE_ID_HORNET:
1932 m = (typeof(m)){"LP21000", "PCIe", "FCoE Adapter"};
1933 GE = 1;
1934 break;
1935 case PCI_DEVICE_ID_PROTEUS_VF:
1936 m = (typeof(m)){"LPev12000", "PCIe IOV",
1937 "Fibre Channel Adapter"};
1938 break;
1939 case PCI_DEVICE_ID_PROTEUS_PF:
1940 m = (typeof(m)){"LPev12000", "PCIe IOV",
1941 "Fibre Channel Adapter"};
1942 break;
1943 case PCI_DEVICE_ID_PROTEUS_S:
1944 m = (typeof(m)){"LPemv12002-S", "PCIe IOV",
1945 "Fibre Channel Adapter"};
1946 break;
1947 case PCI_DEVICE_ID_TIGERSHARK:
1948 oneConnect = 1;
1949 m = (typeof(m)){"OCe10100", "PCIe", "FCoE"};
1950 break;
1951 case PCI_DEVICE_ID_TOMCAT:
1952 oneConnect = 1;
1953 m = (typeof(m)){"OCe11100", "PCIe", "FCoE"};
1954 break;
1955 case PCI_DEVICE_ID_FALCON:
1956 m = (typeof(m)){"LPSe12002-ML1-E", "PCIe",
1957 "EmulexSecure Fibre"};
1958 break;
1959 case PCI_DEVICE_ID_BALIUS:
1960 m = (typeof(m)){"LPVe12002", "PCIe Shared I/O",
1961 "Fibre Channel Adapter"};
1962 break;
1963 case PCI_DEVICE_ID_LANCER_FC:
1964 case PCI_DEVICE_ID_LANCER_FC_VF:
1965 m = (typeof(m)){"LPe16000", "PCIe", "Fibre Channel Adapter"};
1966 break;
1967 case PCI_DEVICE_ID_LANCER_FCOE:
1968 case PCI_DEVICE_ID_LANCER_FCOE_VF:
1969 oneConnect = 1;
1970 m = (typeof(m)){"OCe15100", "PCIe", "FCoE"};
1971 break;
1972 default:
1973 m = (typeof(m)){"Unknown", "", ""};
1974 break;
1975 }
1976
1977 if (mdp && mdp[0] == '\0')
1978 snprintf(mdp, 79,"%s", m.name);
1979 /*
1980 * oneConnect hba requires special processing, they are all initiators
1981 * and we put the port number on the end
1982 */
1983 if (descp && descp[0] == '\0') {
1984 if (oneConnect)
1985 snprintf(descp, 255,
1986 "Emulex OneConnect %s, %s Initiator, Port %s",
1987 m.name, m.function,
1988 phba->Port);
1989 else
1990 snprintf(descp, 255,
1991 "Emulex %s %d%s %s %s",
1992 m.name, max_speed, (GE) ? "GE" : "Gb",
1993 m.bus, m.function);
1994 }
1995 }
1996
1997 /**
1998 * lpfc_post_buffer - Post IOCB(s) with DMA buffer descriptor(s) to a IOCB ring
1999 * @phba: pointer to lpfc hba data structure.
2000 * @pring: pointer to a IOCB ring.
2001 * @cnt: the number of IOCBs to be posted to the IOCB ring.
2002 *
2003 * This routine posts a given number of IOCBs with the associated DMA buffer
2004 * descriptors specified by the cnt argument to the given IOCB ring.
2005 *
2006 * Return codes
2007 * The number of IOCBs NOT able to be posted to the IOCB ring.
2008 **/
2009 int
2010 lpfc_post_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring, int cnt)
2011 {
2012 IOCB_t *icmd;
2013 struct lpfc_iocbq *iocb;
2014 struct lpfc_dmabuf *mp1, *mp2;
2015
2016 cnt += pring->missbufcnt;
2017
2018 /* While there are buffers to post */
2019 while (cnt > 0) {
2020 /* Allocate buffer for command iocb */
2021 iocb = lpfc_sli_get_iocbq(phba);
2022 if (iocb == NULL) {
2023 pring->missbufcnt = cnt;
2024 return cnt;
2025 }
2026 icmd = &iocb->iocb;
2027
2028 /* 2 buffers can be posted per command */
2029 /* Allocate buffer to post */
2030 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2031 if (mp1)
2032 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI, &mp1->phys);
2033 if (!mp1 || !mp1->virt) {
2034 kfree(mp1);
2035 lpfc_sli_release_iocbq(phba, iocb);
2036 pring->missbufcnt = cnt;
2037 return cnt;
2038 }
2039
2040 INIT_LIST_HEAD(&mp1->list);
2041 /* Allocate buffer to post */
2042 if (cnt > 1) {
2043 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
2044 if (mp2)
2045 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
2046 &mp2->phys);
2047 if (!mp2 || !mp2->virt) {
2048 kfree(mp2);
2049 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2050 kfree(mp1);
2051 lpfc_sli_release_iocbq(phba, iocb);
2052 pring->missbufcnt = cnt;
2053 return cnt;
2054 }
2055
2056 INIT_LIST_HEAD(&mp2->list);
2057 } else {
2058 mp2 = NULL;
2059 }
2060
2061 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
2062 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
2063 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
2064 icmd->ulpBdeCount = 1;
2065 cnt--;
2066 if (mp2) {
2067 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
2068 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
2069 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
2070 cnt--;
2071 icmd->ulpBdeCount = 2;
2072 }
2073
2074 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
2075 icmd->ulpLe = 1;
2076
2077 if (lpfc_sli_issue_iocb(phba, pring->ringno, iocb, 0) ==
2078 IOCB_ERROR) {
2079 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
2080 kfree(mp1);
2081 cnt++;
2082 if (mp2) {
2083 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
2084 kfree(mp2);
2085 cnt++;
2086 }
2087 lpfc_sli_release_iocbq(phba, iocb);
2088 pring->missbufcnt = cnt;
2089 return cnt;
2090 }
2091 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
2092 if (mp2)
2093 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
2094 }
2095 pring->missbufcnt = 0;
2096 return 0;
2097 }
2098
2099 /**
2100 * lpfc_post_rcv_buf - Post the initial receive IOCB buffers to ELS ring
2101 * @phba: pointer to lpfc hba data structure.
2102 *
2103 * This routine posts initial receive IOCB buffers to the ELS ring. The
2104 * current number of initial IOCB buffers specified by LPFC_BUF_RING0 is
2105 * set to 64 IOCBs.
2106 *
2107 * Return codes
2108 * 0 - success (currently always success)
2109 **/
2110 static int
2111 lpfc_post_rcv_buf(struct lpfc_hba *phba)
2112 {
2113 struct lpfc_sli *psli = &phba->sli;
2114
2115 /* Ring 0, ELS / CT buffers */
2116 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0);
2117 /* Ring 2 - FCP no buffers needed */
2118
2119 return 0;
2120 }
2121
2122 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
2123
2124 /**
2125 * lpfc_sha_init - Set up initial array of hash table entries
2126 * @HashResultPointer: pointer to an array as hash table.
2127 *
2128 * This routine sets up the initial values to the array of hash table entries
2129 * for the LC HBAs.
2130 **/
2131 static void
2132 lpfc_sha_init(uint32_t * HashResultPointer)
2133 {
2134 HashResultPointer[0] = 0x67452301;
2135 HashResultPointer[1] = 0xEFCDAB89;
2136 HashResultPointer[2] = 0x98BADCFE;
2137 HashResultPointer[3] = 0x10325476;
2138 HashResultPointer[4] = 0xC3D2E1F0;
2139 }
2140
2141 /**
2142 * lpfc_sha_iterate - Iterate initial hash table with the working hash table
2143 * @HashResultPointer: pointer to an initial/result hash table.
2144 * @HashWorkingPointer: pointer to an working hash table.
2145 *
2146 * This routine iterates an initial hash table pointed by @HashResultPointer
2147 * with the values from the working hash table pointeed by @HashWorkingPointer.
2148 * The results are putting back to the initial hash table, returned through
2149 * the @HashResultPointer as the result hash table.
2150 **/
2151 static void
2152 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
2153 {
2154 int t;
2155 uint32_t TEMP;
2156 uint32_t A, B, C, D, E;
2157 t = 16;
2158 do {
2159 HashWorkingPointer[t] =
2160 S(1,
2161 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
2162 8] ^
2163 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
2164 } while (++t <= 79);
2165 t = 0;
2166 A = HashResultPointer[0];
2167 B = HashResultPointer[1];
2168 C = HashResultPointer[2];
2169 D = HashResultPointer[3];
2170 E = HashResultPointer[4];
2171
2172 do {
2173 if (t < 20) {
2174 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
2175 } else if (t < 40) {
2176 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
2177 } else if (t < 60) {
2178 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
2179 } else {
2180 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
2181 }
2182 TEMP += S(5, A) + E + HashWorkingPointer[t];
2183 E = D;
2184 D = C;
2185 C = S(30, B);
2186 B = A;
2187 A = TEMP;
2188 } while (++t <= 79);
2189
2190 HashResultPointer[0] += A;
2191 HashResultPointer[1] += B;
2192 HashResultPointer[2] += C;
2193 HashResultPointer[3] += D;
2194 HashResultPointer[4] += E;
2195
2196 }
2197
2198 /**
2199 * lpfc_challenge_key - Create challenge key based on WWPN of the HBA
2200 * @RandomChallenge: pointer to the entry of host challenge random number array.
2201 * @HashWorking: pointer to the entry of the working hash array.
2202 *
2203 * This routine calculates the working hash array referred by @HashWorking
2204 * from the challenge random numbers associated with the host, referred by
2205 * @RandomChallenge. The result is put into the entry of the working hash
2206 * array and returned by reference through @HashWorking.
2207 **/
2208 static void
2209 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
2210 {
2211 *HashWorking = (*RandomChallenge ^ *HashWorking);
2212 }
2213
2214 /**
2215 * lpfc_hba_init - Perform special handling for LC HBA initialization
2216 * @phba: pointer to lpfc hba data structure.
2217 * @hbainit: pointer to an array of unsigned 32-bit integers.
2218 *
2219 * This routine performs the special handling for LC HBA initialization.
2220 **/
2221 void
2222 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
2223 {
2224 int t;
2225 uint32_t *HashWorking;
2226 uint32_t *pwwnn = (uint32_t *) phba->wwnn;
2227
2228 HashWorking = kcalloc(80, sizeof(uint32_t), GFP_KERNEL);
2229 if (!HashWorking)
2230 return;
2231
2232 HashWorking[0] = HashWorking[78] = *pwwnn++;
2233 HashWorking[1] = HashWorking[79] = *pwwnn;
2234
2235 for (t = 0; t < 7; t++)
2236 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
2237
2238 lpfc_sha_init(hbainit);
2239 lpfc_sha_iterate(hbainit, HashWorking);
2240 kfree(HashWorking);
2241 }
2242
2243 /**
2244 * lpfc_cleanup - Performs vport cleanups before deleting a vport
2245 * @vport: pointer to a virtual N_Port data structure.
2246 *
2247 * This routine performs the necessary cleanups before deleting the @vport.
2248 * It invokes the discovery state machine to perform necessary state
2249 * transitions and to release the ndlps associated with the @vport. Note,
2250 * the physical port is treated as @vport 0.
2251 **/
2252 void
2253 lpfc_cleanup(struct lpfc_vport *vport)
2254 {
2255 struct lpfc_hba *phba = vport->phba;
2256 struct lpfc_nodelist *ndlp, *next_ndlp;
2257 int i = 0;
2258
2259 if (phba->link_state > LPFC_LINK_DOWN)
2260 lpfc_port_link_failure(vport);
2261
2262 list_for_each_entry_safe(ndlp, next_ndlp, &vport->fc_nodes, nlp_listp) {
2263 if (!NLP_CHK_NODE_ACT(ndlp)) {
2264 ndlp = lpfc_enable_node(vport, ndlp,
2265 NLP_STE_UNUSED_NODE);
2266 if (!ndlp)
2267 continue;
2268 spin_lock_irq(&phba->ndlp_lock);
2269 NLP_SET_FREE_REQ(ndlp);
2270 spin_unlock_irq(&phba->ndlp_lock);
2271 /* Trigger the release of the ndlp memory */
2272 lpfc_nlp_put(ndlp);
2273 continue;
2274 }
2275 spin_lock_irq(&phba->ndlp_lock);
2276 if (NLP_CHK_FREE_REQ(ndlp)) {
2277 /* The ndlp should not be in memory free mode already */
2278 spin_unlock_irq(&phba->ndlp_lock);
2279 continue;
2280 } else
2281 /* Indicate request for freeing ndlp memory */
2282 NLP_SET_FREE_REQ(ndlp);
2283 spin_unlock_irq(&phba->ndlp_lock);
2284
2285 if (vport->port_type != LPFC_PHYSICAL_PORT &&
2286 ndlp->nlp_DID == Fabric_DID) {
2287 /* Just free up ndlp with Fabric_DID for vports */
2288 lpfc_nlp_put(ndlp);
2289 continue;
2290 }
2291
2292 if (ndlp->nlp_type & NLP_FABRIC)
2293 lpfc_disc_state_machine(vport, ndlp, NULL,
2294 NLP_EVT_DEVICE_RECOVERY);
2295
2296 lpfc_disc_state_machine(vport, ndlp, NULL,
2297 NLP_EVT_DEVICE_RM);
2298
2299 }
2300
2301 /* At this point, ALL ndlp's should be gone
2302 * because of the previous NLP_EVT_DEVICE_RM.
2303 * Lets wait for this to happen, if needed.
2304 */
2305 while (!list_empty(&vport->fc_nodes)) {
2306 if (i++ > 3000) {
2307 lpfc_printf_vlog(vport, KERN_ERR, LOG_DISCOVERY,
2308 "0233 Nodelist not empty\n");
2309 list_for_each_entry_safe(ndlp, next_ndlp,
2310 &vport->fc_nodes, nlp_listp) {
2311 lpfc_printf_vlog(ndlp->vport, KERN_ERR,
2312 LOG_NODE,
2313 "0282 did:x%x ndlp:x%p "
2314 "usgmap:x%x refcnt:%d\n",
2315 ndlp->nlp_DID, (void *)ndlp,
2316 ndlp->nlp_usg_map,
2317 atomic_read(
2318 &ndlp->kref.refcount));
2319 }
2320 break;
2321 }
2322
2323 /* Wait for any activity on ndlps to settle */
2324 msleep(10);
2325 }
2326 lpfc_cleanup_vports_rrqs(vport, NULL);
2327 }
2328
2329 /**
2330 * lpfc_stop_vport_timers - Stop all the timers associated with a vport
2331 * @vport: pointer to a virtual N_Port data structure.
2332 *
2333 * This routine stops all the timers associated with a @vport. This function
2334 * is invoked before disabling or deleting a @vport. Note that the physical
2335 * port is treated as @vport 0.
2336 **/
2337 void
2338 lpfc_stop_vport_timers(struct lpfc_vport *vport)
2339 {
2340 del_timer_sync(&vport->els_tmofunc);
2341 del_timer_sync(&vport->fc_fdmitmo);
2342 del_timer_sync(&vport->delayed_disc_tmo);
2343 lpfc_can_disctmo(vport);
2344 return;
2345 }
2346
2347 /**
2348 * __lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2349 * @phba: pointer to lpfc hba data structure.
2350 *
2351 * This routine stops the SLI4 FCF rediscover wait timer if it's on. The
2352 * caller of this routine should already hold the host lock.
2353 **/
2354 void
2355 __lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2356 {
2357 /* Clear pending FCF rediscovery wait flag */
2358 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
2359
2360 /* Now, try to stop the timer */
2361 del_timer(&phba->fcf.redisc_wait);
2362 }
2363
2364 /**
2365 * lpfc_sli4_stop_fcf_redisc_wait_timer - Stop FCF rediscovery wait timer
2366 * @phba: pointer to lpfc hba data structure.
2367 *
2368 * This routine stops the SLI4 FCF rediscover wait timer if it's on. It
2369 * checks whether the FCF rediscovery wait timer is pending with the host
2370 * lock held before proceeding with disabling the timer and clearing the
2371 * wait timer pendig flag.
2372 **/
2373 void
2374 lpfc_sli4_stop_fcf_redisc_wait_timer(struct lpfc_hba *phba)
2375 {
2376 spin_lock_irq(&phba->hbalock);
2377 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
2378 /* FCF rediscovery timer already fired or stopped */
2379 spin_unlock_irq(&phba->hbalock);
2380 return;
2381 }
2382 __lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2383 /* Clear failover in progress flags */
2384 phba->fcf.fcf_flag &= ~(FCF_DEAD_DISC | FCF_ACVL_DISC);
2385 spin_unlock_irq(&phba->hbalock);
2386 }
2387
2388 /**
2389 * lpfc_stop_hba_timers - Stop all the timers associated with an HBA
2390 * @phba: pointer to lpfc hba data structure.
2391 *
2392 * This routine stops all the timers associated with a HBA. This function is
2393 * invoked before either putting a HBA offline or unloading the driver.
2394 **/
2395 void
2396 lpfc_stop_hba_timers(struct lpfc_hba *phba)
2397 {
2398 lpfc_stop_vport_timers(phba->pport);
2399 del_timer_sync(&phba->sli.mbox_tmo);
2400 del_timer_sync(&phba->fabric_block_timer);
2401 del_timer_sync(&phba->eratt_poll);
2402 del_timer_sync(&phba->hb_tmofunc);
2403 if (phba->sli_rev == LPFC_SLI_REV4) {
2404 del_timer_sync(&phba->rrq_tmr);
2405 phba->hba_flag &= ~HBA_RRQ_ACTIVE;
2406 }
2407 phba->hb_outstanding = 0;
2408
2409 switch (phba->pci_dev_grp) {
2410 case LPFC_PCI_DEV_LP:
2411 /* Stop any LightPulse device specific driver timers */
2412 del_timer_sync(&phba->fcp_poll_timer);
2413 break;
2414 case LPFC_PCI_DEV_OC:
2415 /* Stop any OneConnect device sepcific driver timers */
2416 lpfc_sli4_stop_fcf_redisc_wait_timer(phba);
2417 break;
2418 default:
2419 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2420 "0297 Invalid device group (x%x)\n",
2421 phba->pci_dev_grp);
2422 break;
2423 }
2424 return;
2425 }
2426
2427 /**
2428 * lpfc_block_mgmt_io - Mark a HBA's management interface as blocked
2429 * @phba: pointer to lpfc hba data structure.
2430 *
2431 * This routine marks a HBA's management interface as blocked. Once the HBA's
2432 * management interface is marked as blocked, all the user space access to
2433 * the HBA, whether they are from sysfs interface or libdfc interface will
2434 * all be blocked. The HBA is set to block the management interface when the
2435 * driver prepares the HBA interface for online or offline.
2436 **/
2437 static void
2438 lpfc_block_mgmt_io(struct lpfc_hba * phba)
2439 {
2440 unsigned long iflag;
2441 uint8_t actcmd = MBX_HEARTBEAT;
2442 unsigned long timeout;
2443
2444 timeout = msecs_to_jiffies(LPFC_MBOX_TMO * 1000) + jiffies;
2445 spin_lock_irqsave(&phba->hbalock, iflag);
2446 phba->sli.sli_flag |= LPFC_BLOCK_MGMT_IO;
2447 if (phba->sli.mbox_active) {
2448 actcmd = phba->sli.mbox_active->u.mb.mbxCommand;
2449 /* Determine how long we might wait for the active mailbox
2450 * command to be gracefully completed by firmware.
2451 */
2452 timeout = msecs_to_jiffies(lpfc_mbox_tmo_val(phba,
2453 phba->sli.mbox_active) * 1000) + jiffies;
2454 }
2455 spin_unlock_irqrestore(&phba->hbalock, iflag);
2456
2457 /* Wait for the outstnading mailbox command to complete */
2458 while (phba->sli.mbox_active) {
2459 /* Check active mailbox complete status every 2ms */
2460 msleep(2);
2461 if (time_after(jiffies, timeout)) {
2462 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2463 "2813 Mgmt IO is Blocked %x "
2464 "- mbox cmd %x still active\n",
2465 phba->sli.sli_flag, actcmd);
2466 break;
2467 }
2468 }
2469 }
2470
2471 /**
2472 * lpfc_online - Initialize and bring a HBA online
2473 * @phba: pointer to lpfc hba data structure.
2474 *
2475 * This routine initializes the HBA and brings a HBA online. During this
2476 * process, the management interface is blocked to prevent user space access
2477 * to the HBA interfering with the driver initialization.
2478 *
2479 * Return codes
2480 * 0 - successful
2481 * 1 - failed
2482 **/
2483 int
2484 lpfc_online(struct lpfc_hba *phba)
2485 {
2486 struct lpfc_vport *vport;
2487 struct lpfc_vport **vports;
2488 int i;
2489
2490 if (!phba)
2491 return 0;
2492 vport = phba->pport;
2493
2494 if (!(vport->fc_flag & FC_OFFLINE_MODE))
2495 return 0;
2496
2497 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2498 "0458 Bring Adapter online\n");
2499
2500 lpfc_block_mgmt_io(phba);
2501
2502 if (!lpfc_sli_queue_setup(phba)) {
2503 lpfc_unblock_mgmt_io(phba);
2504 return 1;
2505 }
2506
2507 if (phba->sli_rev == LPFC_SLI_REV4) {
2508 if (lpfc_sli4_hba_setup(phba)) { /* Initialize SLI4 HBA */
2509 lpfc_unblock_mgmt_io(phba);
2510 return 1;
2511 }
2512 } else {
2513 if (lpfc_sli_hba_setup(phba)) { /* Initialize SLI2/SLI3 HBA */
2514 lpfc_unblock_mgmt_io(phba);
2515 return 1;
2516 }
2517 }
2518
2519 vports = lpfc_create_vport_work_array(phba);
2520 if (vports != NULL)
2521 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2522 struct Scsi_Host *shost;
2523 shost = lpfc_shost_from_vport(vports[i]);
2524 spin_lock_irq(shost->host_lock);
2525 vports[i]->fc_flag &= ~FC_OFFLINE_MODE;
2526 if (phba->sli3_options & LPFC_SLI3_NPIV_ENABLED)
2527 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2528 if (phba->sli_rev == LPFC_SLI_REV4)
2529 vports[i]->fc_flag |= FC_VPORT_NEEDS_INIT_VPI;
2530 spin_unlock_irq(shost->host_lock);
2531 }
2532 lpfc_destroy_vport_work_array(phba, vports);
2533
2534 lpfc_unblock_mgmt_io(phba);
2535 return 0;
2536 }
2537
2538 /**
2539 * lpfc_unblock_mgmt_io - Mark a HBA's management interface to be not blocked
2540 * @phba: pointer to lpfc hba data structure.
2541 *
2542 * This routine marks a HBA's management interface as not blocked. Once the
2543 * HBA's management interface is marked as not blocked, all the user space
2544 * access to the HBA, whether they are from sysfs interface or libdfc
2545 * interface will be allowed. The HBA is set to block the management interface
2546 * when the driver prepares the HBA interface for online or offline and then
2547 * set to unblock the management interface afterwards.
2548 **/
2549 void
2550 lpfc_unblock_mgmt_io(struct lpfc_hba * phba)
2551 {
2552 unsigned long iflag;
2553
2554 spin_lock_irqsave(&phba->hbalock, iflag);
2555 phba->sli.sli_flag &= ~LPFC_BLOCK_MGMT_IO;
2556 spin_unlock_irqrestore(&phba->hbalock, iflag);
2557 }
2558
2559 /**
2560 * lpfc_offline_prep - Prepare a HBA to be brought offline
2561 * @phba: pointer to lpfc hba data structure.
2562 *
2563 * This routine is invoked to prepare a HBA to be brought offline. It performs
2564 * unregistration login to all the nodes on all vports and flushes the mailbox
2565 * queue to make it ready to be brought offline.
2566 **/
2567 void
2568 lpfc_offline_prep(struct lpfc_hba * phba)
2569 {
2570 struct lpfc_vport *vport = phba->pport;
2571 struct lpfc_nodelist *ndlp, *next_ndlp;
2572 struct lpfc_vport **vports;
2573 struct Scsi_Host *shost;
2574 int i;
2575
2576 if (vport->fc_flag & FC_OFFLINE_MODE)
2577 return;
2578
2579 lpfc_block_mgmt_io(phba);
2580
2581 lpfc_linkdown(phba);
2582
2583 /* Issue an unreg_login to all nodes on all vports */
2584 vports = lpfc_create_vport_work_array(phba);
2585 if (vports != NULL) {
2586 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2587 if (vports[i]->load_flag & FC_UNLOADING)
2588 continue;
2589 shost = lpfc_shost_from_vport(vports[i]);
2590 spin_lock_irq(shost->host_lock);
2591 vports[i]->vpi_state &= ~LPFC_VPI_REGISTERED;
2592 vports[i]->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2593 vports[i]->fc_flag &= ~FC_VFI_REGISTERED;
2594 spin_unlock_irq(shost->host_lock);
2595
2596 shost = lpfc_shost_from_vport(vports[i]);
2597 list_for_each_entry_safe(ndlp, next_ndlp,
2598 &vports[i]->fc_nodes,
2599 nlp_listp) {
2600 if (!NLP_CHK_NODE_ACT(ndlp))
2601 continue;
2602 if (ndlp->nlp_state == NLP_STE_UNUSED_NODE)
2603 continue;
2604 if (ndlp->nlp_type & NLP_FABRIC) {
2605 lpfc_disc_state_machine(vports[i], ndlp,
2606 NULL, NLP_EVT_DEVICE_RECOVERY);
2607 lpfc_disc_state_machine(vports[i], ndlp,
2608 NULL, NLP_EVT_DEVICE_RM);
2609 }
2610 spin_lock_irq(shost->host_lock);
2611 ndlp->nlp_flag &= ~NLP_NPR_ADISC;
2612 spin_unlock_irq(shost->host_lock);
2613 lpfc_unreg_rpi(vports[i], ndlp);
2614 }
2615 }
2616 }
2617 lpfc_destroy_vport_work_array(phba, vports);
2618
2619 lpfc_sli_mbox_sys_shutdown(phba);
2620 }
2621
2622 /**
2623 * lpfc_offline - Bring a HBA offline
2624 * @phba: pointer to lpfc hba data structure.
2625 *
2626 * This routine actually brings a HBA offline. It stops all the timers
2627 * associated with the HBA, brings down the SLI layer, and eventually
2628 * marks the HBA as in offline state for the upper layer protocol.
2629 **/
2630 void
2631 lpfc_offline(struct lpfc_hba *phba)
2632 {
2633 struct Scsi_Host *shost;
2634 struct lpfc_vport **vports;
2635 int i;
2636
2637 if (phba->pport->fc_flag & FC_OFFLINE_MODE)
2638 return;
2639
2640 /* stop port and all timers associated with this hba */
2641 lpfc_stop_port(phba);
2642 vports = lpfc_create_vport_work_array(phba);
2643 if (vports != NULL)
2644 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
2645 lpfc_stop_vport_timers(vports[i]);
2646 lpfc_destroy_vport_work_array(phba, vports);
2647 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
2648 "0460 Bring Adapter offline\n");
2649 /* Bring down the SLI Layer and cleanup. The HBA is offline
2650 now. */
2651 lpfc_sli_hba_down(phba);
2652 spin_lock_irq(&phba->hbalock);
2653 phba->work_ha = 0;
2654 spin_unlock_irq(&phba->hbalock);
2655 vports = lpfc_create_vport_work_array(phba);
2656 if (vports != NULL)
2657 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
2658 shost = lpfc_shost_from_vport(vports[i]);
2659 spin_lock_irq(shost->host_lock);
2660 vports[i]->work_port_events = 0;
2661 vports[i]->fc_flag |= FC_OFFLINE_MODE;
2662 spin_unlock_irq(shost->host_lock);
2663 }
2664 lpfc_destroy_vport_work_array(phba, vports);
2665 }
2666
2667 /**
2668 * lpfc_scsi_free - Free all the SCSI buffers and IOCBs from driver lists
2669 * @phba: pointer to lpfc hba data structure.
2670 *
2671 * This routine is to free all the SCSI buffers and IOCBs from the driver
2672 * list back to kernel. It is called from lpfc_pci_remove_one to free
2673 * the internal resources before the device is removed from the system.
2674 *
2675 * Return codes
2676 * 0 - successful (for now, it always returns 0)
2677 **/
2678 static int
2679 lpfc_scsi_free(struct lpfc_hba *phba)
2680 {
2681 struct lpfc_scsi_buf *sb, *sb_next;
2682 struct lpfc_iocbq *io, *io_next;
2683
2684 spin_lock_irq(&phba->hbalock);
2685 /* Release all the lpfc_scsi_bufs maintained by this host. */
2686 spin_lock(&phba->scsi_buf_list_lock);
2687 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
2688 list_del(&sb->list);
2689 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
2690 sb->dma_handle);
2691 kfree(sb);
2692 phba->total_scsi_bufs--;
2693 }
2694 spin_unlock(&phba->scsi_buf_list_lock);
2695
2696 /* Release all the lpfc_iocbq entries maintained by this host. */
2697 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
2698 list_del(&io->list);
2699 kfree(io);
2700 phba->total_iocbq_bufs--;
2701 }
2702
2703 spin_unlock_irq(&phba->hbalock);
2704 return 0;
2705 }
2706
2707 /**
2708 * lpfc_create_port - Create an FC port
2709 * @phba: pointer to lpfc hba data structure.
2710 * @instance: a unique integer ID to this FC port.
2711 * @dev: pointer to the device data structure.
2712 *
2713 * This routine creates a FC port for the upper layer protocol. The FC port
2714 * can be created on top of either a physical port or a virtual port provided
2715 * by the HBA. This routine also allocates a SCSI host data structure (shost)
2716 * and associates the FC port created before adding the shost into the SCSI
2717 * layer.
2718 *
2719 * Return codes
2720 * @vport - pointer to the virtual N_Port data structure.
2721 * NULL - port create failed.
2722 **/
2723 struct lpfc_vport *
2724 lpfc_create_port(struct lpfc_hba *phba, int instance, struct device *dev)
2725 {
2726 struct lpfc_vport *vport;
2727 struct Scsi_Host *shost;
2728 int error = 0;
2729
2730 if (dev != &phba->pcidev->dev)
2731 shost = scsi_host_alloc(&lpfc_vport_template,
2732 sizeof(struct lpfc_vport));
2733 else
2734 shost = scsi_host_alloc(&lpfc_template,
2735 sizeof(struct lpfc_vport));
2736 if (!shost)
2737 goto out;
2738
2739 vport = (struct lpfc_vport *) shost->hostdata;
2740 vport->phba = phba;
2741 vport->load_flag |= FC_LOADING;
2742 vport->fc_flag |= FC_VPORT_NEEDS_REG_VPI;
2743 vport->fc_rscn_flush = 0;
2744
2745 lpfc_get_vport_cfgparam(vport);
2746 shost->unique_id = instance;
2747 shost->max_id = LPFC_MAX_TARGET;
2748 shost->max_lun = vport->cfg_max_luns;
2749 shost->this_id = -1;
2750 shost->max_cmd_len = 16;
2751 if (phba->sli_rev == LPFC_SLI_REV4) {
2752 shost->dma_boundary =
2753 phba->sli4_hba.pc_sli4_params.sge_supp_len-1;
2754 shost->sg_tablesize = phba->cfg_sg_seg_cnt;
2755 }
2756
2757 /*
2758 * Set initial can_queue value since 0 is no longer supported and
2759 * scsi_add_host will fail. This will be adjusted later based on the
2760 * max xri value determined in hba setup.
2761 */
2762 shost->can_queue = phba->cfg_hba_queue_depth - 10;
2763 if (dev != &phba->pcidev->dev) {
2764 shost->transportt = lpfc_vport_transport_template;
2765 vport->port_type = LPFC_NPIV_PORT;
2766 } else {
2767 shost->transportt = lpfc_transport_template;
2768 vport->port_type = LPFC_PHYSICAL_PORT;
2769 }
2770
2771 /* Initialize all internally managed lists. */
2772 INIT_LIST_HEAD(&vport->fc_nodes);
2773 INIT_LIST_HEAD(&vport->rcv_buffer_list);
2774 spin_lock_init(&vport->work_port_lock);
2775
2776 init_timer(&vport->fc_disctmo);
2777 vport->fc_disctmo.function = lpfc_disc_timeout;
2778 vport->fc_disctmo.data = (unsigned long)vport;
2779
2780 init_timer(&vport->fc_fdmitmo);
2781 vport->fc_fdmitmo.function = lpfc_fdmi_tmo;
2782 vport->fc_fdmitmo.data = (unsigned long)vport;
2783
2784 init_timer(&vport->els_tmofunc);
2785 vport->els_tmofunc.function = lpfc_els_timeout;
2786 vport->els_tmofunc.data = (unsigned long)vport;
2787
2788 init_timer(&vport->delayed_disc_tmo);
2789 vport->delayed_disc_tmo.function = lpfc_delayed_disc_tmo;
2790 vport->delayed_disc_tmo.data = (unsigned long)vport;
2791
2792 error = scsi_add_host_with_dma(shost, dev, &phba->pcidev->dev);
2793 if (error)
2794 goto out_put_shost;
2795
2796 spin_lock_irq(&phba->hbalock);
2797 list_add_tail(&vport->listentry, &phba->port_list);
2798 spin_unlock_irq(&phba->hbalock);
2799 return vport;
2800
2801 out_put_shost:
2802 scsi_host_put(shost);
2803 out:
2804 return NULL;
2805 }
2806
2807 /**
2808 * destroy_port - destroy an FC port
2809 * @vport: pointer to an lpfc virtual N_Port data structure.
2810 *
2811 * This routine destroys a FC port from the upper layer protocol. All the
2812 * resources associated with the port are released.
2813 **/
2814 void
2815 destroy_port(struct lpfc_vport *vport)
2816 {
2817 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
2818 struct lpfc_hba *phba = vport->phba;
2819
2820 lpfc_debugfs_terminate(vport);
2821 fc_remove_host(shost);
2822 scsi_remove_host(shost);
2823
2824 spin_lock_irq(&phba->hbalock);
2825 list_del_init(&vport->listentry);
2826 spin_unlock_irq(&phba->hbalock);
2827
2828 lpfc_cleanup(vport);
2829 return;
2830 }
2831
2832 /**
2833 * lpfc_get_instance - Get a unique integer ID
2834 *
2835 * This routine allocates a unique integer ID from lpfc_hba_index pool. It
2836 * uses the kernel idr facility to perform the task.
2837 *
2838 * Return codes:
2839 * instance - a unique integer ID allocated as the new instance.
2840 * -1 - lpfc get instance failed.
2841 **/
2842 int
2843 lpfc_get_instance(void)
2844 {
2845 int instance = 0;
2846
2847 /* Assign an unused number */
2848 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
2849 return -1;
2850 if (idr_get_new(&lpfc_hba_index, NULL, &instance))
2851 return -1;
2852 return instance;
2853 }
2854
2855 /**
2856 * lpfc_scan_finished - method for SCSI layer to detect whether scan is done
2857 * @shost: pointer to SCSI host data structure.
2858 * @time: elapsed time of the scan in jiffies.
2859 *
2860 * This routine is called by the SCSI layer with a SCSI host to determine
2861 * whether the scan host is finished.
2862 *
2863 * Note: there is no scan_start function as adapter initialization will have
2864 * asynchronously kicked off the link initialization.
2865 *
2866 * Return codes
2867 * 0 - SCSI host scan is not over yet.
2868 * 1 - SCSI host scan is over.
2869 **/
2870 int lpfc_scan_finished(struct Scsi_Host *shost, unsigned long time)
2871 {
2872 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2873 struct lpfc_hba *phba = vport->phba;
2874 int stat = 0;
2875
2876 spin_lock_irq(shost->host_lock);
2877
2878 if (vport->load_flag & FC_UNLOADING) {
2879 stat = 1;
2880 goto finished;
2881 }
2882 if (time >= 30 * HZ) {
2883 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2884 "0461 Scanning longer than 30 "
2885 "seconds. Continuing initialization\n");
2886 stat = 1;
2887 goto finished;
2888 }
2889 if (time >= 15 * HZ && phba->link_state <= LPFC_LINK_DOWN) {
2890 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
2891 "0465 Link down longer than 15 "
2892 "seconds. Continuing initialization\n");
2893 stat = 1;
2894 goto finished;
2895 }
2896
2897 if (vport->port_state != LPFC_VPORT_READY)
2898 goto finished;
2899 if (vport->num_disc_nodes || vport->fc_prli_sent)
2900 goto finished;
2901 if (vport->fc_map_cnt == 0 && time < 2 * HZ)
2902 goto finished;
2903 if ((phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) != 0)
2904 goto finished;
2905
2906 stat = 1;
2907
2908 finished:
2909 spin_unlock_irq(shost->host_lock);
2910 return stat;
2911 }
2912
2913 /**
2914 * lpfc_host_attrib_init - Initialize SCSI host attributes on a FC port
2915 * @shost: pointer to SCSI host data structure.
2916 *
2917 * This routine initializes a given SCSI host attributes on a FC port. The
2918 * SCSI host can be either on top of a physical port or a virtual port.
2919 **/
2920 void lpfc_host_attrib_init(struct Scsi_Host *shost)
2921 {
2922 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2923 struct lpfc_hba *phba = vport->phba;
2924 /*
2925 * Set fixed host attributes. Must done after lpfc_sli_hba_setup().
2926 */
2927
2928 fc_host_node_name(shost) = wwn_to_u64(vport->fc_nodename.u.wwn);
2929 fc_host_port_name(shost) = wwn_to_u64(vport->fc_portname.u.wwn);
2930 fc_host_supported_classes(shost) = FC_COS_CLASS3;
2931
2932 memset(fc_host_supported_fc4s(shost), 0,
2933 sizeof(fc_host_supported_fc4s(shost)));
2934 fc_host_supported_fc4s(shost)[2] = 1;
2935 fc_host_supported_fc4s(shost)[7] = 1;
2936
2937 lpfc_vport_symbolic_node_name(vport, fc_host_symbolic_name(shost),
2938 sizeof fc_host_symbolic_name(shost));
2939
2940 fc_host_supported_speeds(shost) = 0;
2941 if (phba->lmt & LMT_16Gb)
2942 fc_host_supported_speeds(shost) |= FC_PORTSPEED_16GBIT;
2943 if (phba->lmt & LMT_10Gb)
2944 fc_host_supported_speeds(shost) |= FC_PORTSPEED_10GBIT;
2945 if (phba->lmt & LMT_8Gb)
2946 fc_host_supported_speeds(shost) |= FC_PORTSPEED_8GBIT;
2947 if (phba->lmt & LMT_4Gb)
2948 fc_host_supported_speeds(shost) |= FC_PORTSPEED_4GBIT;
2949 if (phba->lmt & LMT_2Gb)
2950 fc_host_supported_speeds(shost) |= FC_PORTSPEED_2GBIT;
2951 if (phba->lmt & LMT_1Gb)
2952 fc_host_supported_speeds(shost) |= FC_PORTSPEED_1GBIT;
2953
2954 fc_host_maxframe_size(shost) =
2955 (((uint32_t) vport->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
2956 (uint32_t) vport->fc_sparam.cmn.bbRcvSizeLsb;
2957
2958 fc_host_dev_loss_tmo(shost) = vport->cfg_devloss_tmo;
2959
2960 /* This value is also unchanging */
2961 memset(fc_host_active_fc4s(shost), 0,
2962 sizeof(fc_host_active_fc4s(shost)));
2963 fc_host_active_fc4s(shost)[2] = 1;
2964 fc_host_active_fc4s(shost)[7] = 1;
2965
2966 fc_host_max_npiv_vports(shost) = phba->max_vpi;
2967 spin_lock_irq(shost->host_lock);
2968 vport->load_flag &= ~FC_LOADING;
2969 spin_unlock_irq(shost->host_lock);
2970 }
2971
2972 /**
2973 * lpfc_stop_port_s3 - Stop SLI3 device port
2974 * @phba: pointer to lpfc hba data structure.
2975 *
2976 * This routine is invoked to stop an SLI3 device port, it stops the device
2977 * from generating interrupts and stops the device driver's timers for the
2978 * device.
2979 **/
2980 static void
2981 lpfc_stop_port_s3(struct lpfc_hba *phba)
2982 {
2983 /* Clear all interrupt enable conditions */
2984 writel(0, phba->HCregaddr);
2985 readl(phba->HCregaddr); /* flush */
2986 /* Clear all pending interrupts */
2987 writel(0xffffffff, phba->HAregaddr);
2988 readl(phba->HAregaddr); /* flush */
2989
2990 /* Reset some HBA SLI setup states */
2991 lpfc_stop_hba_timers(phba);
2992 phba->pport->work_port_events = 0;
2993 }
2994
2995 /**
2996 * lpfc_stop_port_s4 - Stop SLI4 device port
2997 * @phba: pointer to lpfc hba data structure.
2998 *
2999 * This routine is invoked to stop an SLI4 device port, it stops the device
3000 * from generating interrupts and stops the device driver's timers for the
3001 * device.
3002 **/
3003 static void
3004 lpfc_stop_port_s4(struct lpfc_hba *phba)
3005 {
3006 /* Reset some HBA SLI4 setup states */
3007 lpfc_stop_hba_timers(phba);
3008 phba->pport->work_port_events = 0;
3009 phba->sli4_hba.intr_enable = 0;
3010 }
3011
3012 /**
3013 * lpfc_stop_port - Wrapper function for stopping hba port
3014 * @phba: Pointer to HBA context object.
3015 *
3016 * This routine wraps the actual SLI3 or SLI4 hba stop port routine from
3017 * the API jump table function pointer from the lpfc_hba struct.
3018 **/
3019 void
3020 lpfc_stop_port(struct lpfc_hba *phba)
3021 {
3022 phba->lpfc_stop_port(phba);
3023 }
3024
3025 /**
3026 * lpfc_fcf_redisc_wait_start_timer - Start fcf rediscover wait timer
3027 * @phba: Pointer to hba for which this call is being executed.
3028 *
3029 * This routine starts the timer waiting for the FCF rediscovery to complete.
3030 **/
3031 void
3032 lpfc_fcf_redisc_wait_start_timer(struct lpfc_hba *phba)
3033 {
3034 unsigned long fcf_redisc_wait_tmo =
3035 (jiffies + msecs_to_jiffies(LPFC_FCF_REDISCOVER_WAIT_TMO));
3036 /* Start fcf rediscovery wait period timer */
3037 mod_timer(&phba->fcf.redisc_wait, fcf_redisc_wait_tmo);
3038 spin_lock_irq(&phba->hbalock);
3039 /* Allow action to new fcf asynchronous event */
3040 phba->fcf.fcf_flag &= ~(FCF_AVAILABLE | FCF_SCAN_DONE);
3041 /* Mark the FCF rediscovery pending state */
3042 phba->fcf.fcf_flag |= FCF_REDISC_PEND;
3043 spin_unlock_irq(&phba->hbalock);
3044 }
3045
3046 /**
3047 * lpfc_sli4_fcf_redisc_wait_tmo - FCF table rediscover wait timeout
3048 * @ptr: Map to lpfc_hba data structure pointer.
3049 *
3050 * This routine is invoked when waiting for FCF table rediscover has been
3051 * timed out. If new FCF record(s) has (have) been discovered during the
3052 * wait period, a new FCF event shall be added to the FCOE async event
3053 * list, and then worker thread shall be waked up for processing from the
3054 * worker thread context.
3055 **/
3056 void
3057 lpfc_sli4_fcf_redisc_wait_tmo(unsigned long ptr)
3058 {
3059 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
3060
3061 /* Don't send FCF rediscovery event if timer cancelled */
3062 spin_lock_irq(&phba->hbalock);
3063 if (!(phba->fcf.fcf_flag & FCF_REDISC_PEND)) {
3064 spin_unlock_irq(&phba->hbalock);
3065 return;
3066 }
3067 /* Clear FCF rediscovery timer pending flag */
3068 phba->fcf.fcf_flag &= ~FCF_REDISC_PEND;
3069 /* FCF rediscovery event to worker thread */
3070 phba->fcf.fcf_flag |= FCF_REDISC_EVT;
3071 spin_unlock_irq(&phba->hbalock);
3072 lpfc_printf_log(phba, KERN_INFO, LOG_FIP,
3073 "2776 FCF rediscover quiescent timer expired\n");
3074 /* wake up worker thread */
3075 lpfc_worker_wake_up(phba);
3076 }
3077
3078 /**
3079 * lpfc_sli4_parse_latt_fault - Parse sli4 link-attention link fault code
3080 * @phba: pointer to lpfc hba data structure.
3081 * @acqe_link: pointer to the async link completion queue entry.
3082 *
3083 * This routine is to parse the SLI4 link-attention link fault code and
3084 * translate it into the base driver's read link attention mailbox command
3085 * status.
3086 *
3087 * Return: Link-attention status in terms of base driver's coding.
3088 **/
3089 static uint16_t
3090 lpfc_sli4_parse_latt_fault(struct lpfc_hba *phba,
3091 struct lpfc_acqe_link *acqe_link)
3092 {
3093 uint16_t latt_fault;
3094
3095 switch (bf_get(lpfc_acqe_link_fault, acqe_link)) {
3096 case LPFC_ASYNC_LINK_FAULT_NONE:
3097 case LPFC_ASYNC_LINK_FAULT_LOCAL:
3098 case LPFC_ASYNC_LINK_FAULT_REMOTE:
3099 latt_fault = 0;
3100 break;
3101 default:
3102 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3103 "0398 Invalid link fault code: x%x\n",
3104 bf_get(lpfc_acqe_link_fault, acqe_link));
3105 latt_fault = MBXERR_ERROR;
3106 break;
3107 }
3108 return latt_fault;
3109 }
3110
3111 /**
3112 * lpfc_sli4_parse_latt_type - Parse sli4 link attention type
3113 * @phba: pointer to lpfc hba data structure.
3114 * @acqe_link: pointer to the async link completion queue entry.
3115 *
3116 * This routine is to parse the SLI4 link attention type and translate it
3117 * into the base driver's link attention type coding.
3118 *
3119 * Return: Link attention type in terms of base driver's coding.
3120 **/
3121 static uint8_t
3122 lpfc_sli4_parse_latt_type(struct lpfc_hba *phba,
3123 struct lpfc_acqe_link *acqe_link)
3124 {
3125 uint8_t att_type;
3126
3127 switch (bf_get(lpfc_acqe_link_status, acqe_link)) {
3128 case LPFC_ASYNC_LINK_STATUS_DOWN:
3129 case LPFC_ASYNC_LINK_STATUS_LOGICAL_DOWN:
3130 att_type = LPFC_ATT_LINK_DOWN;
3131 break;
3132 case LPFC_ASYNC_LINK_STATUS_UP:
3133 /* Ignore physical link up events - wait for logical link up */
3134 att_type = LPFC_ATT_RESERVED;
3135 break;
3136 case LPFC_ASYNC_LINK_STATUS_LOGICAL_UP:
3137 att_type = LPFC_ATT_LINK_UP;
3138 break;
3139 default:
3140 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3141 "0399 Invalid link attention type: x%x\n",
3142 bf_get(lpfc_acqe_link_status, acqe_link));
3143 att_type = LPFC_ATT_RESERVED;
3144 break;
3145 }
3146 return att_type;
3147 }
3148
3149 /**
3150 * lpfc_sli4_parse_latt_link_speed - Parse sli4 link-attention link speed
3151 * @phba: pointer to lpfc hba data structure.
3152 * @acqe_link: pointer to the async link completion queue entry.
3153 *
3154 * This routine is to parse the SLI4 link-attention link speed and translate
3155 * it into the base driver's link-attention link speed coding.
3156 *
3157 * Return: Link-attention link speed in terms of base driver's coding.
3158 **/
3159 static uint8_t
3160 lpfc_sli4_parse_latt_link_speed(struct lpfc_hba *phba,
3161 struct lpfc_acqe_link *acqe_link)
3162 {
3163 uint8_t link_speed;
3164
3165 switch (bf_get(lpfc_acqe_link_speed, acqe_link)) {
3166 case LPFC_ASYNC_LINK_SPEED_ZERO:
3167 case LPFC_ASYNC_LINK_SPEED_10MBPS:
3168 case LPFC_ASYNC_LINK_SPEED_100MBPS:
3169 link_speed = LPFC_LINK_SPEED_UNKNOWN;
3170 break;
3171 case LPFC_ASYNC_LINK_SPEED_1GBPS:
3172 link_speed = LPFC_LINK_SPEED_1GHZ;
3173 break;
3174 case LPFC_ASYNC_LINK_SPEED_10GBPS:
3175 link_speed = LPFC_LINK_SPEED_10GHZ;
3176 break;
3177 default:
3178 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3179 "0483 Invalid link-attention link speed: x%x\n",
3180 bf_get(lpfc_acqe_link_speed, acqe_link));
3181 link_speed = LPFC_LINK_SPEED_UNKNOWN;
3182 break;
3183 }
3184 return link_speed;
3185 }
3186
3187 /**
3188 * lpfc_sli4_async_link_evt - Process the asynchronous FCoE link event
3189 * @phba: pointer to lpfc hba data structure.
3190 * @acqe_link: pointer to the async link completion queue entry.
3191 *
3192 * This routine is to handle the SLI4 asynchronous FCoE link event.
3193 **/
3194 static void
3195 lpfc_sli4_async_link_evt(struct lpfc_hba *phba,
3196 struct lpfc_acqe_link *acqe_link)
3197 {
3198 struct lpfc_dmabuf *mp;
3199 LPFC_MBOXQ_t *pmb;
3200 MAILBOX_t *mb;
3201 struct lpfc_mbx_read_top *la;
3202 uint8_t att_type;
3203 int rc;
3204
3205 att_type = lpfc_sli4_parse_latt_type(phba, acqe_link);
3206 if (att_type != LPFC_ATT_LINK_DOWN && att_type != LPFC_ATT_LINK_UP)
3207 return;
3208 phba->fcoe_eventtag = acqe_link->event_tag;
3209 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3210 if (!pmb) {
3211 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3212 "0395 The mboxq allocation failed\n");
3213 return;
3214 }
3215 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3216 if (!mp) {
3217 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3218 "0396 The lpfc_dmabuf allocation failed\n");
3219 goto out_free_pmb;
3220 }
3221 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3222 if (!mp->virt) {
3223 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3224 "0397 The mbuf allocation failed\n");
3225 goto out_free_dmabuf;
3226 }
3227
3228 /* Cleanup any outstanding ELS commands */
3229 lpfc_els_flush_all_cmd(phba);
3230
3231 /* Block ELS IOCBs until we have done process link event */
3232 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3233
3234 /* Update link event statistics */
3235 phba->sli.slistat.link_event++;
3236
3237 /* Create lpfc_handle_latt mailbox command from link ACQE */
3238 lpfc_read_topology(phba, pmb, mp);
3239 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3240 pmb->vport = phba->pport;
3241
3242 /* Keep the link status for extra SLI4 state machine reference */
3243 phba->sli4_hba.link_state.speed =
3244 bf_get(lpfc_acqe_link_speed, acqe_link);
3245 phba->sli4_hba.link_state.duplex =
3246 bf_get(lpfc_acqe_link_duplex, acqe_link);
3247 phba->sli4_hba.link_state.status =
3248 bf_get(lpfc_acqe_link_status, acqe_link);
3249 phba->sli4_hba.link_state.type =
3250 bf_get(lpfc_acqe_link_type, acqe_link);
3251 phba->sli4_hba.link_state.number =
3252 bf_get(lpfc_acqe_link_number, acqe_link);
3253 phba->sli4_hba.link_state.fault =
3254 bf_get(lpfc_acqe_link_fault, acqe_link);
3255 phba->sli4_hba.link_state.logical_speed =
3256 bf_get(lpfc_acqe_logical_link_speed, acqe_link);
3257 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3258 "2900 Async FC/FCoE Link event - Speed:%dGBit "
3259 "duplex:x%x LA Type:x%x Port Type:%d Port Number:%d "
3260 "Logical speed:%dMbps Fault:%d\n",
3261 phba->sli4_hba.link_state.speed,
3262 phba->sli4_hba.link_state.topology,
3263 phba->sli4_hba.link_state.status,
3264 phba->sli4_hba.link_state.type,
3265 phba->sli4_hba.link_state.number,
3266 phba->sli4_hba.link_state.logical_speed * 10,
3267 phba->sli4_hba.link_state.fault);
3268 /*
3269 * For FC Mode: issue the READ_TOPOLOGY mailbox command to fetch
3270 * topology info. Note: Optional for non FC-AL ports.
3271 */
3272 if (!(phba->hba_flag & HBA_FCOE_MODE)) {
3273 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3274 if (rc == MBX_NOT_FINISHED)
3275 goto out_free_dmabuf;
3276 return;
3277 }
3278 /*
3279 * For FCoE Mode: fill in all the topology information we need and call
3280 * the READ_TOPOLOGY completion routine to continue without actually
3281 * sending the READ_TOPOLOGY mailbox command to the port.
3282 */
3283 /* Parse and translate status field */
3284 mb = &pmb->u.mb;
3285 mb->mbxStatus = lpfc_sli4_parse_latt_fault(phba, acqe_link);
3286
3287 /* Parse and translate link attention fields */
3288 la = (struct lpfc_mbx_read_top *) &pmb->u.mb.un.varReadTop;
3289 la->eventTag = acqe_link->event_tag;
3290 bf_set(lpfc_mbx_read_top_att_type, la, att_type);
3291 bf_set(lpfc_mbx_read_top_link_spd, la,
3292 lpfc_sli4_parse_latt_link_speed(phba, acqe_link));
3293
3294 /* Fake the the following irrelvant fields */
3295 bf_set(lpfc_mbx_read_top_topology, la, LPFC_TOPOLOGY_PT_PT);
3296 bf_set(lpfc_mbx_read_top_alpa_granted, la, 0);
3297 bf_set(lpfc_mbx_read_top_il, la, 0);
3298 bf_set(lpfc_mbx_read_top_pb, la, 0);
3299 bf_set(lpfc_mbx_read_top_fa, la, 0);
3300 bf_set(lpfc_mbx_read_top_mm, la, 0);
3301
3302 /* Invoke the lpfc_handle_latt mailbox command callback function */
3303 lpfc_mbx_cmpl_read_topology(phba, pmb);
3304
3305 return;
3306
3307 out_free_dmabuf:
3308 kfree(mp);
3309 out_free_pmb:
3310 mempool_free(pmb, phba->mbox_mem_pool);
3311 }
3312
3313 /**
3314 * lpfc_sli4_async_fc_evt - Process the asynchronous FC link event
3315 * @phba: pointer to lpfc hba data structure.
3316 * @acqe_fc: pointer to the async fc completion queue entry.
3317 *
3318 * This routine is to handle the SLI4 asynchronous FC event. It will simply log
3319 * that the event was received and then issue a read_topology mailbox command so
3320 * that the rest of the driver will treat it the same as SLI3.
3321 **/
3322 static void
3323 lpfc_sli4_async_fc_evt(struct lpfc_hba *phba, struct lpfc_acqe_fc_la *acqe_fc)
3324 {
3325 struct lpfc_dmabuf *mp;
3326 LPFC_MBOXQ_t *pmb;
3327 int rc;
3328
3329 if (bf_get(lpfc_trailer_type, acqe_fc) !=
3330 LPFC_FC_LA_EVENT_TYPE_FC_LINK) {
3331 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3332 "2895 Non FC link Event detected.(%d)\n",
3333 bf_get(lpfc_trailer_type, acqe_fc));
3334 return;
3335 }
3336 /* Keep the link status for extra SLI4 state machine reference */
3337 phba->sli4_hba.link_state.speed =
3338 bf_get(lpfc_acqe_fc_la_speed, acqe_fc);
3339 phba->sli4_hba.link_state.duplex = LPFC_ASYNC_LINK_DUPLEX_FULL;
3340 phba->sli4_hba.link_state.topology =
3341 bf_get(lpfc_acqe_fc_la_topology, acqe_fc);
3342 phba->sli4_hba.link_state.status =
3343 bf_get(lpfc_acqe_fc_la_att_type, acqe_fc);
3344 phba->sli4_hba.link_state.type =
3345 bf_get(lpfc_acqe_fc_la_port_type, acqe_fc);
3346 phba->sli4_hba.link_state.number =
3347 bf_get(lpfc_acqe_fc_la_port_number, acqe_fc);
3348 phba->sli4_hba.link_state.fault =
3349 bf_get(lpfc_acqe_link_fault, acqe_fc);
3350 phba->sli4_hba.link_state.logical_speed =
3351 bf_get(lpfc_acqe_fc_la_llink_spd, acqe_fc);
3352 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3353 "2896 Async FC event - Speed:%dGBaud Topology:x%x "
3354 "LA Type:x%x Port Type:%d Port Number:%d Logical speed:"
3355 "%dMbps Fault:%d\n",
3356 phba->sli4_hba.link_state.speed,
3357 phba->sli4_hba.link_state.topology,
3358 phba->sli4_hba.link_state.status,
3359 phba->sli4_hba.link_state.type,
3360 phba->sli4_hba.link_state.number,
3361 phba->sli4_hba.link_state.logical_speed * 10,
3362 phba->sli4_hba.link_state.fault);
3363 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
3364 if (!pmb) {
3365 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3366 "2897 The mboxq allocation failed\n");
3367 return;
3368 }
3369 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
3370 if (!mp) {
3371 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3372 "2898 The lpfc_dmabuf allocation failed\n");
3373 goto out_free_pmb;
3374 }
3375 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
3376 if (!mp->virt) {
3377 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3378 "2899 The mbuf allocation failed\n");
3379 goto out_free_dmabuf;
3380 }
3381
3382 /* Cleanup any outstanding ELS commands */
3383 lpfc_els_flush_all_cmd(phba);
3384
3385 /* Block ELS IOCBs until we have done process link event */
3386 phba->sli.ring[LPFC_ELS_RING].flag |= LPFC_STOP_IOCB_EVENT;
3387
3388 /* Update link event statistics */
3389 phba->sli.slistat.link_event++;
3390
3391 /* Create lpfc_handle_latt mailbox command from link ACQE */
3392 lpfc_read_topology(phba, pmb, mp);
3393 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_topology;
3394 pmb->vport = phba->pport;
3395
3396 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
3397 if (rc == MBX_NOT_FINISHED)
3398 goto out_free_dmabuf;
3399 return;
3400
3401 out_free_dmabuf:
3402 kfree(mp);
3403 out_free_pmb:
3404 mempool_free(pmb, phba->mbox_mem_pool);
3405 }
3406
3407 /**
3408 * lpfc_sli4_async_sli_evt - Process the asynchronous SLI link event
3409 * @phba: pointer to lpfc hba data structure.
3410 * @acqe_fc: pointer to the async SLI completion queue entry.
3411 *
3412 * This routine is to handle the SLI4 asynchronous SLI events.
3413 **/
3414 static void
3415 lpfc_sli4_async_sli_evt(struct lpfc_hba *phba, struct lpfc_acqe_sli *acqe_sli)
3416 {
3417 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3418 "2901 Async SLI event - Event Data1:x%08x Event Data2:"
3419 "x%08x SLI Event Type:%d",
3420 acqe_sli->event_data1, acqe_sli->event_data2,
3421 bf_get(lpfc_trailer_type, acqe_sli));
3422 return;
3423 }
3424
3425 /**
3426 * lpfc_sli4_perform_vport_cvl - Perform clear virtual link on a vport
3427 * @vport: pointer to vport data structure.
3428 *
3429 * This routine is to perform Clear Virtual Link (CVL) on a vport in
3430 * response to a CVL event.
3431 *
3432 * Return the pointer to the ndlp with the vport if successful, otherwise
3433 * return NULL.
3434 **/
3435 static struct lpfc_nodelist *
3436 lpfc_sli4_perform_vport_cvl(struct lpfc_vport *vport)
3437 {
3438 struct lpfc_nodelist *ndlp;
3439 struct Scsi_Host *shost;
3440 struct lpfc_hba *phba;
3441
3442 if (!vport)
3443 return NULL;
3444 phba = vport->phba;
3445 if (!phba)
3446 return NULL;
3447 ndlp = lpfc_findnode_did(vport, Fabric_DID);
3448 if (!ndlp) {
3449 /* Cannot find existing Fabric ndlp, so allocate a new one */
3450 ndlp = mempool_alloc(phba->nlp_mem_pool, GFP_KERNEL);
3451 if (!ndlp)
3452 return 0;
3453 lpfc_nlp_init(vport, ndlp, Fabric_DID);
3454 /* Set the node type */
3455 ndlp->nlp_type |= NLP_FABRIC;
3456 /* Put ndlp onto node list */
3457 lpfc_enqueue_node(vport, ndlp);
3458 } else if (!NLP_CHK_NODE_ACT(ndlp)) {
3459 /* re-setup ndlp without removing from node list */
3460 ndlp = lpfc_enable_node(vport, ndlp, NLP_STE_UNUSED_NODE);
3461 if (!ndlp)
3462 return 0;
3463 }
3464 if ((phba->pport->port_state < LPFC_FLOGI) &&
3465 (phba->pport->port_state != LPFC_VPORT_FAILED))
3466 return NULL;
3467 /* If virtual link is not yet instantiated ignore CVL */
3468 if ((vport != phba->pport) && (vport->port_state < LPFC_FDISC)
3469 && (vport->port_state != LPFC_VPORT_FAILED))
3470 return NULL;
3471 shost = lpfc_shost_from_vport(vport);
3472 if (!shost)
3473 return NULL;
3474 lpfc_linkdown_port(vport);
3475 lpfc_cleanup_pending_mbox(vport);
3476 spin_lock_irq(shost->host_lock);
3477 vport->fc_flag |= FC_VPORT_CVL_RCVD;
3478 spin_unlock_irq(shost->host_lock);
3479
3480 return ndlp;
3481 }
3482
3483 /**
3484 * lpfc_sli4_perform_all_vport_cvl - Perform clear virtual link on all vports
3485 * @vport: pointer to lpfc hba data structure.
3486 *
3487 * This routine is to perform Clear Virtual Link (CVL) on all vports in
3488 * response to a FCF dead event.
3489 **/
3490 static void
3491 lpfc_sli4_perform_all_vport_cvl(struct lpfc_hba *phba)
3492 {
3493 struct lpfc_vport **vports;
3494 int i;
3495
3496 vports = lpfc_create_vport_work_array(phba);
3497 if (vports)
3498 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++)
3499 lpfc_sli4_perform_vport_cvl(vports[i]);
3500 lpfc_destroy_vport_work_array(phba, vports);
3501 }
3502
3503 /**
3504 * lpfc_sli4_async_fip_evt - Process the asynchronous FCoE FIP event
3505 * @phba: pointer to lpfc hba data structure.
3506 * @acqe_link: pointer to the async fcoe completion queue entry.
3507 *
3508 * This routine is to handle the SLI4 asynchronous fcoe event.
3509 **/
3510 static void
3511 lpfc_sli4_async_fip_evt(struct lpfc_hba *phba,
3512 struct lpfc_acqe_fip *acqe_fip)
3513 {
3514 uint8_t event_type = bf_get(lpfc_trailer_type, acqe_fip);
3515 int rc;
3516 struct lpfc_vport *vport;
3517 struct lpfc_nodelist *ndlp;
3518 struct Scsi_Host *shost;
3519 int active_vlink_present;
3520 struct lpfc_vport **vports;
3521 int i;
3522
3523 phba->fc_eventTag = acqe_fip->event_tag;
3524 phba->fcoe_eventtag = acqe_fip->event_tag;
3525 switch (event_type) {
3526 case LPFC_FIP_EVENT_TYPE_NEW_FCF:
3527 case LPFC_FIP_EVENT_TYPE_FCF_PARAM_MOD:
3528 if (event_type == LPFC_FIP_EVENT_TYPE_NEW_FCF)
3529 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3530 LOG_DISCOVERY,
3531 "2546 New FCF event, evt_tag:x%x, "
3532 "index:x%x\n",
3533 acqe_fip->event_tag,
3534 acqe_fip->index);
3535 else
3536 lpfc_printf_log(phba, KERN_WARNING, LOG_FIP |
3537 LOG_DISCOVERY,
3538 "2788 FCF param modified event, "
3539 "evt_tag:x%x, index:x%x\n",
3540 acqe_fip->event_tag,
3541 acqe_fip->index);
3542 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3543 /*
3544 * During period of FCF discovery, read the FCF
3545 * table record indexed by the event to update
3546 * FCF roundrobin failover eligible FCF bmask.
3547 */
3548 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3549 LOG_DISCOVERY,
3550 "2779 Read FCF (x%x) for updating "
3551 "roundrobin FCF failover bmask\n",
3552 acqe_fip->index);
3553 rc = lpfc_sli4_read_fcf_rec(phba, acqe_fip->index);
3554 }
3555
3556 /* If the FCF discovery is in progress, do nothing. */
3557 spin_lock_irq(&phba->hbalock);
3558 if (phba->hba_flag & FCF_TS_INPROG) {
3559 spin_unlock_irq(&phba->hbalock);
3560 break;
3561 }
3562 /* If fast FCF failover rescan event is pending, do nothing */
3563 if (phba->fcf.fcf_flag & FCF_REDISC_EVT) {
3564 spin_unlock_irq(&phba->hbalock);
3565 break;
3566 }
3567
3568 /* If the FCF has been in discovered state, do nothing. */
3569 if (phba->fcf.fcf_flag & FCF_SCAN_DONE) {
3570 spin_unlock_irq(&phba->hbalock);
3571 break;
3572 }
3573 spin_unlock_irq(&phba->hbalock);
3574
3575 /* Otherwise, scan the entire FCF table and re-discover SAN */
3576 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3577 "2770 Start FCF table scan per async FCF "
3578 "event, evt_tag:x%x, index:x%x\n",
3579 acqe_fip->event_tag, acqe_fip->index);
3580 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba,
3581 LPFC_FCOE_FCF_GET_FIRST);
3582 if (rc)
3583 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3584 "2547 Issue FCF scan read FCF mailbox "
3585 "command failed (x%x)\n", rc);
3586 break;
3587
3588 case LPFC_FIP_EVENT_TYPE_FCF_TABLE_FULL:
3589 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3590 "2548 FCF Table full count 0x%x tag 0x%x\n",
3591 bf_get(lpfc_acqe_fip_fcf_count, acqe_fip),
3592 acqe_fip->event_tag);
3593 break;
3594
3595 case LPFC_FIP_EVENT_TYPE_FCF_DEAD:
3596 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3597 "2549 FCF (x%x) disconnected from network, "
3598 "tag:x%x\n", acqe_fip->index, acqe_fip->event_tag);
3599 /*
3600 * If we are in the middle of FCF failover process, clear
3601 * the corresponding FCF bit in the roundrobin bitmap.
3602 */
3603 spin_lock_irq(&phba->hbalock);
3604 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3605 spin_unlock_irq(&phba->hbalock);
3606 /* Update FLOGI FCF failover eligible FCF bmask */
3607 lpfc_sli4_fcf_rr_index_clear(phba, acqe_fip->index);
3608 break;
3609 }
3610 spin_unlock_irq(&phba->hbalock);
3611
3612 /* If the event is not for currently used fcf do nothing */
3613 if (phba->fcf.current_rec.fcf_indx != acqe_fip->index)
3614 break;
3615
3616 /*
3617 * Otherwise, request the port to rediscover the entire FCF
3618 * table for a fast recovery from case that the current FCF
3619 * is no longer valid as we are not in the middle of FCF
3620 * failover process already.
3621 */
3622 spin_lock_irq(&phba->hbalock);
3623 /* Mark the fast failover process in progress */
3624 phba->fcf.fcf_flag |= FCF_DEAD_DISC;
3625 spin_unlock_irq(&phba->hbalock);
3626
3627 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3628 "2771 Start FCF fast failover process due to "
3629 "FCF DEAD event: evt_tag:x%x, fcf_index:x%x "
3630 "\n", acqe_fip->event_tag, acqe_fip->index);
3631 rc = lpfc_sli4_redisc_fcf_table(phba);
3632 if (rc) {
3633 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3634 LOG_DISCOVERY,
3635 "2772 Issue FCF rediscover mabilbox "
3636 "command failed, fail through to FCF "
3637 "dead event\n");
3638 spin_lock_irq(&phba->hbalock);
3639 phba->fcf.fcf_flag &= ~FCF_DEAD_DISC;
3640 spin_unlock_irq(&phba->hbalock);
3641 /*
3642 * Last resort will fail over by treating this
3643 * as a link down to FCF registration.
3644 */
3645 lpfc_sli4_fcf_dead_failthrough(phba);
3646 } else {
3647 /* Reset FCF roundrobin bmask for new discovery */
3648 lpfc_sli4_clear_fcf_rr_bmask(phba);
3649 /*
3650 * Handling fast FCF failover to a DEAD FCF event is
3651 * considered equalivant to receiving CVL to all vports.
3652 */
3653 lpfc_sli4_perform_all_vport_cvl(phba);
3654 }
3655 break;
3656 case LPFC_FIP_EVENT_TYPE_CVL:
3657 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3658 "2718 Clear Virtual Link Received for VPI 0x%x"
3659 " tag 0x%x\n", acqe_fip->index, acqe_fip->event_tag);
3660
3661 vport = lpfc_find_vport_by_vpid(phba,
3662 acqe_fip->index);
3663 ndlp = lpfc_sli4_perform_vport_cvl(vport);
3664 if (!ndlp)
3665 break;
3666 active_vlink_present = 0;
3667
3668 vports = lpfc_create_vport_work_array(phba);
3669 if (vports) {
3670 for (i = 0; i <= phba->max_vports && vports[i] != NULL;
3671 i++) {
3672 if ((!(vports[i]->fc_flag &
3673 FC_VPORT_CVL_RCVD)) &&
3674 (vports[i]->port_state > LPFC_FDISC)) {
3675 active_vlink_present = 1;
3676 break;
3677 }
3678 }
3679 lpfc_destroy_vport_work_array(phba, vports);
3680 }
3681
3682 if (active_vlink_present) {
3683 /*
3684 * If there are other active VLinks present,
3685 * re-instantiate the Vlink using FDISC.
3686 */
3687 mod_timer(&ndlp->nlp_delayfunc, jiffies + HZ);
3688 shost = lpfc_shost_from_vport(vport);
3689 spin_lock_irq(shost->host_lock);
3690 ndlp->nlp_flag |= NLP_DELAY_TMO;
3691 spin_unlock_irq(shost->host_lock);
3692 ndlp->nlp_last_elscmd = ELS_CMD_FDISC;
3693 vport->port_state = LPFC_FDISC;
3694 } else {
3695 /*
3696 * Otherwise, we request port to rediscover
3697 * the entire FCF table for a fast recovery
3698 * from possible case that the current FCF
3699 * is no longer valid if we are not already
3700 * in the FCF failover process.
3701 */
3702 spin_lock_irq(&phba->hbalock);
3703 if (phba->fcf.fcf_flag & FCF_DISCOVERY) {
3704 spin_unlock_irq(&phba->hbalock);
3705 break;
3706 }
3707 /* Mark the fast failover process in progress */
3708 phba->fcf.fcf_flag |= FCF_ACVL_DISC;
3709 spin_unlock_irq(&phba->hbalock);
3710 lpfc_printf_log(phba, KERN_INFO, LOG_FIP |
3711 LOG_DISCOVERY,
3712 "2773 Start FCF failover per CVL, "
3713 "evt_tag:x%x\n", acqe_fip->event_tag);
3714 rc = lpfc_sli4_redisc_fcf_table(phba);
3715 if (rc) {
3716 lpfc_printf_log(phba, KERN_ERR, LOG_FIP |
3717 LOG_DISCOVERY,
3718 "2774 Issue FCF rediscover "
3719 "mabilbox command failed, "
3720 "through to CVL event\n");
3721 spin_lock_irq(&phba->hbalock);
3722 phba->fcf.fcf_flag &= ~FCF_ACVL_DISC;
3723 spin_unlock_irq(&phba->hbalock);
3724 /*
3725 * Last resort will be re-try on the
3726 * the current registered FCF entry.
3727 */
3728 lpfc_retry_pport_discovery(phba);
3729 } else
3730 /*
3731 * Reset FCF roundrobin bmask for new
3732 * discovery.
3733 */
3734 lpfc_sli4_clear_fcf_rr_bmask(phba);
3735 }
3736 break;
3737 default:
3738 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3739 "0288 Unknown FCoE event type 0x%x event tag "
3740 "0x%x\n", event_type, acqe_fip->event_tag);
3741 break;
3742 }
3743 }
3744
3745 /**
3746 * lpfc_sli4_async_dcbx_evt - Process the asynchronous dcbx event
3747 * @phba: pointer to lpfc hba data structure.
3748 * @acqe_link: pointer to the async dcbx completion queue entry.
3749 *
3750 * This routine is to handle the SLI4 asynchronous dcbx event.
3751 **/
3752 static void
3753 lpfc_sli4_async_dcbx_evt(struct lpfc_hba *phba,
3754 struct lpfc_acqe_dcbx *acqe_dcbx)
3755 {
3756 phba->fc_eventTag = acqe_dcbx->event_tag;
3757 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3758 "0290 The SLI4 DCBX asynchronous event is not "
3759 "handled yet\n");
3760 }
3761
3762 /**
3763 * lpfc_sli4_async_grp5_evt - Process the asynchronous group5 event
3764 * @phba: pointer to lpfc hba data structure.
3765 * @acqe_link: pointer to the async grp5 completion queue entry.
3766 *
3767 * This routine is to handle the SLI4 asynchronous grp5 event. A grp5 event
3768 * is an asynchronous notified of a logical link speed change. The Port
3769 * reports the logical link speed in units of 10Mbps.
3770 **/
3771 static void
3772 lpfc_sli4_async_grp5_evt(struct lpfc_hba *phba,
3773 struct lpfc_acqe_grp5 *acqe_grp5)
3774 {
3775 uint16_t prev_ll_spd;
3776
3777 phba->fc_eventTag = acqe_grp5->event_tag;
3778 phba->fcoe_eventtag = acqe_grp5->event_tag;
3779 prev_ll_spd = phba->sli4_hba.link_state.logical_speed;
3780 phba->sli4_hba.link_state.logical_speed =
3781 (bf_get(lpfc_acqe_grp5_llink_spd, acqe_grp5));
3782 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
3783 "2789 GRP5 Async Event: Updating logical link speed "
3784 "from %dMbps to %dMbps\n", (prev_ll_spd * 10),
3785 (phba->sli4_hba.link_state.logical_speed*10));
3786 }
3787
3788 /**
3789 * lpfc_sli4_async_event_proc - Process all the pending asynchronous event
3790 * @phba: pointer to lpfc hba data structure.
3791 *
3792 * This routine is invoked by the worker thread to process all the pending
3793 * SLI4 asynchronous events.
3794 **/
3795 void lpfc_sli4_async_event_proc(struct lpfc_hba *phba)
3796 {
3797 struct lpfc_cq_event *cq_event;
3798
3799 /* First, declare the async event has been handled */
3800 spin_lock_irq(&phba->hbalock);
3801 phba->hba_flag &= ~ASYNC_EVENT;
3802 spin_unlock_irq(&phba->hbalock);
3803 /* Now, handle all the async events */
3804 while (!list_empty(&phba->sli4_hba.sp_asynce_work_queue)) {
3805 /* Get the first event from the head of the event queue */
3806 spin_lock_irq(&phba->hbalock);
3807 list_remove_head(&phba->sli4_hba.sp_asynce_work_queue,
3808 cq_event, struct lpfc_cq_event, list);
3809 spin_unlock_irq(&phba->hbalock);
3810 /* Process the asynchronous event */
3811 switch (bf_get(lpfc_trailer_code, &cq_event->cqe.mcqe_cmpl)) {
3812 case LPFC_TRAILER_CODE_LINK:
3813 lpfc_sli4_async_link_evt(phba,
3814 &cq_event->cqe.acqe_link);
3815 break;
3816 case LPFC_TRAILER_CODE_FCOE:
3817 lpfc_sli4_async_fip_evt(phba, &cq_event->cqe.acqe_fip);
3818 break;
3819 case LPFC_TRAILER_CODE_DCBX:
3820 lpfc_sli4_async_dcbx_evt(phba,
3821 &cq_event->cqe.acqe_dcbx);
3822 break;
3823 case LPFC_TRAILER_CODE_GRP5:
3824 lpfc_sli4_async_grp5_evt(phba,
3825 &cq_event->cqe.acqe_grp5);
3826 break;
3827 case LPFC_TRAILER_CODE_FC:
3828 lpfc_sli4_async_fc_evt(phba, &cq_event->cqe.acqe_fc);
3829 break;
3830 case LPFC_TRAILER_CODE_SLI:
3831 lpfc_sli4_async_sli_evt(phba, &cq_event->cqe.acqe_sli);
3832 break;
3833 default:
3834 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
3835 "1804 Invalid asynchrous event code: "
3836 "x%x\n", bf_get(lpfc_trailer_code,
3837 &cq_event->cqe.mcqe_cmpl));
3838 break;
3839 }
3840 /* Free the completion event processed to the free pool */
3841 lpfc_sli4_cq_event_release(phba, cq_event);
3842 }
3843 }
3844
3845 /**
3846 * lpfc_sli4_fcf_redisc_event_proc - Process fcf table rediscovery event
3847 * @phba: pointer to lpfc hba data structure.
3848 *
3849 * This routine is invoked by the worker thread to process FCF table
3850 * rediscovery pending completion event.
3851 **/
3852 void lpfc_sli4_fcf_redisc_event_proc(struct lpfc_hba *phba)
3853 {
3854 int rc;
3855
3856 spin_lock_irq(&phba->hbalock);
3857 /* Clear FCF rediscovery timeout event */
3858 phba->fcf.fcf_flag &= ~FCF_REDISC_EVT;
3859 /* Clear driver fast failover FCF record flag */
3860 phba->fcf.failover_rec.flag = 0;
3861 /* Set state for FCF fast failover */
3862 phba->fcf.fcf_flag |= FCF_REDISC_FOV;
3863 spin_unlock_irq(&phba->hbalock);
3864
3865 /* Scan FCF table from the first entry to re-discover SAN */
3866 lpfc_printf_log(phba, KERN_INFO, LOG_FIP | LOG_DISCOVERY,
3867 "2777 Start post-quiescent FCF table scan\n");
3868 rc = lpfc_sli4_fcf_scan_read_fcf_rec(phba, LPFC_FCOE_FCF_GET_FIRST);
3869 if (rc)
3870 lpfc_printf_log(phba, KERN_ERR, LOG_FIP | LOG_DISCOVERY,
3871 "2747 Issue FCF scan read FCF mailbox "
3872 "command failed 0x%x\n", rc);
3873 }
3874
3875 /**
3876 * lpfc_api_table_setup - Set up per hba pci-device group func api jump table
3877 * @phba: pointer to lpfc hba data structure.
3878 * @dev_grp: The HBA PCI-Device group number.
3879 *
3880 * This routine is invoked to set up the per HBA PCI-Device group function
3881 * API jump table entries.
3882 *
3883 * Return: 0 if success, otherwise -ENODEV
3884 **/
3885 int
3886 lpfc_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
3887 {
3888 int rc;
3889
3890 /* Set up lpfc PCI-device group */
3891 phba->pci_dev_grp = dev_grp;
3892
3893 /* The LPFC_PCI_DEV_OC uses SLI4 */
3894 if (dev_grp == LPFC_PCI_DEV_OC)
3895 phba->sli_rev = LPFC_SLI_REV4;
3896
3897 /* Set up device INIT API function jump table */
3898 rc = lpfc_init_api_table_setup(phba, dev_grp);
3899 if (rc)
3900 return -ENODEV;
3901 /* Set up SCSI API function jump table */
3902 rc = lpfc_scsi_api_table_setup(phba, dev_grp);
3903 if (rc)
3904 return -ENODEV;
3905 /* Set up SLI API function jump table */
3906 rc = lpfc_sli_api_table_setup(phba, dev_grp);
3907 if (rc)
3908 return -ENODEV;
3909 /* Set up MBOX API function jump table */
3910 rc = lpfc_mbox_api_table_setup(phba, dev_grp);
3911 if (rc)
3912 return -ENODEV;
3913
3914 return 0;
3915 }
3916
3917 /**
3918 * lpfc_log_intr_mode - Log the active interrupt mode
3919 * @phba: pointer to lpfc hba data structure.
3920 * @intr_mode: active interrupt mode adopted.
3921 *
3922 * This routine it invoked to log the currently used active interrupt mode
3923 * to the device.
3924 **/
3925 static void lpfc_log_intr_mode(struct lpfc_hba *phba, uint32_t intr_mode)
3926 {
3927 switch (intr_mode) {
3928 case 0:
3929 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3930 "0470 Enable INTx interrupt mode.\n");
3931 break;
3932 case 1:
3933 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3934 "0481 Enabled MSI interrupt mode.\n");
3935 break;
3936 case 2:
3937 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
3938 "0480 Enabled MSI-X interrupt mode.\n");
3939 break;
3940 default:
3941 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3942 "0482 Illegal interrupt mode.\n");
3943 break;
3944 }
3945 return;
3946 }
3947
3948 /**
3949 * lpfc_enable_pci_dev - Enable a generic PCI device.
3950 * @phba: pointer to lpfc hba data structure.
3951 *
3952 * This routine is invoked to enable the PCI device that is common to all
3953 * PCI devices.
3954 *
3955 * Return codes
3956 * 0 - successful
3957 * other values - error
3958 **/
3959 static int
3960 lpfc_enable_pci_dev(struct lpfc_hba *phba)
3961 {
3962 struct pci_dev *pdev;
3963 int bars = 0;
3964
3965 /* Obtain PCI device reference */
3966 if (!phba->pcidev)
3967 goto out_error;
3968 else
3969 pdev = phba->pcidev;
3970 /* Select PCI BARs */
3971 bars = pci_select_bars(pdev, IORESOURCE_MEM);
3972 /* Enable PCI device */
3973 if (pci_enable_device_mem(pdev))
3974 goto out_error;
3975 /* Request PCI resource for the device */
3976 if (pci_request_selected_regions(pdev, bars, LPFC_DRIVER_NAME))
3977 goto out_disable_device;
3978 /* Set up device as PCI master and save state for EEH */
3979 pci_set_master(pdev);
3980 pci_try_set_mwi(pdev);
3981 pci_save_state(pdev);
3982
3983 /* PCIe EEH recovery on powerpc platforms needs fundamental reset */
3984 if (pci_find_capability(pdev, PCI_CAP_ID_EXP))
3985 pdev->needs_freset = 1;
3986
3987 return 0;
3988
3989 out_disable_device:
3990 pci_disable_device(pdev);
3991 out_error:
3992 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
3993 "1401 Failed to enable pci device, bars:x%x\n", bars);
3994 return -ENODEV;
3995 }
3996
3997 /**
3998 * lpfc_disable_pci_dev - Disable a generic PCI device.
3999 * @phba: pointer to lpfc hba data structure.
4000 *
4001 * This routine is invoked to disable the PCI device that is common to all
4002 * PCI devices.
4003 **/
4004 static void
4005 lpfc_disable_pci_dev(struct lpfc_hba *phba)
4006 {
4007 struct pci_dev *pdev;
4008 int bars;
4009
4010 /* Obtain PCI device reference */
4011 if (!phba->pcidev)
4012 return;
4013 else
4014 pdev = phba->pcidev;
4015 /* Select PCI BARs */
4016 bars = pci_select_bars(pdev, IORESOURCE_MEM);
4017 /* Release PCI resource and disable PCI device */
4018 pci_release_selected_regions(pdev, bars);
4019 pci_disable_device(pdev);
4020 /* Null out PCI private reference to driver */
4021 pci_set_drvdata(pdev, NULL);
4022
4023 return;
4024 }
4025
4026 /**
4027 * lpfc_reset_hba - Reset a hba
4028 * @phba: pointer to lpfc hba data structure.
4029 *
4030 * This routine is invoked to reset a hba device. It brings the HBA
4031 * offline, performs a board restart, and then brings the board back
4032 * online. The lpfc_offline calls lpfc_sli_hba_down which will clean up
4033 * on outstanding mailbox commands.
4034 **/
4035 void
4036 lpfc_reset_hba(struct lpfc_hba *phba)
4037 {
4038 /* If resets are disabled then set error state and return. */
4039 if (!phba->cfg_enable_hba_reset) {
4040 phba->link_state = LPFC_HBA_ERROR;
4041 return;
4042 }
4043 lpfc_offline_prep(phba);
4044 lpfc_offline(phba);
4045 lpfc_sli_brdrestart(phba);
4046 lpfc_online(phba);
4047 lpfc_unblock_mgmt_io(phba);
4048 }
4049
4050 /**
4051 * lpfc_sli_sriov_nr_virtfn_get - Get the number of sr-iov virtual functions
4052 * @phba: pointer to lpfc hba data structure.
4053 *
4054 * This function enables the PCI SR-IOV virtual functions to a physical
4055 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4056 * enable the number of virtual functions to the physical function. As
4057 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4058 * API call does not considered as an error condition for most of the device.
4059 **/
4060 uint16_t
4061 lpfc_sli_sriov_nr_virtfn_get(struct lpfc_hba *phba)
4062 {
4063 struct pci_dev *pdev = phba->pcidev;
4064 uint16_t nr_virtfn;
4065 int pos;
4066
4067 pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
4068 if (pos == 0)
4069 return 0;
4070
4071 pci_read_config_word(pdev, pos + PCI_SRIOV_TOTAL_VF, &nr_virtfn);
4072 return nr_virtfn;
4073 }
4074
4075 /**
4076 * lpfc_sli_probe_sriov_nr_virtfn - Enable a number of sr-iov virtual functions
4077 * @phba: pointer to lpfc hba data structure.
4078 * @nr_vfn: number of virtual functions to be enabled.
4079 *
4080 * This function enables the PCI SR-IOV virtual functions to a physical
4081 * function. It invokes the PCI SR-IOV api with the @nr_vfn provided to
4082 * enable the number of virtual functions to the physical function. As
4083 * not all devices support SR-IOV, the return code from the pci_enable_sriov()
4084 * API call does not considered as an error condition for most of the device.
4085 **/
4086 int
4087 lpfc_sli_probe_sriov_nr_virtfn(struct lpfc_hba *phba, int nr_vfn)
4088 {
4089 struct pci_dev *pdev = phba->pcidev;
4090 uint16_t max_nr_vfn;
4091 int rc;
4092
4093 max_nr_vfn = lpfc_sli_sriov_nr_virtfn_get(phba);
4094 if (nr_vfn > max_nr_vfn) {
4095 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4096 "3057 Requested vfs (%d) greater than "
4097 "supported vfs (%d)", nr_vfn, max_nr_vfn);
4098 return -EINVAL;
4099 }
4100
4101 rc = pci_enable_sriov(pdev, nr_vfn);
4102 if (rc) {
4103 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4104 "2806 Failed to enable sriov on this device "
4105 "with vfn number nr_vf:%d, rc:%d\n",
4106 nr_vfn, rc);
4107 } else
4108 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4109 "2807 Successful enable sriov on this device "
4110 "with vfn number nr_vf:%d\n", nr_vfn);
4111 return rc;
4112 }
4113
4114 /**
4115 * lpfc_sli_driver_resource_setup - Setup driver internal resources for SLI3 dev.
4116 * @phba: pointer to lpfc hba data structure.
4117 *
4118 * This routine is invoked to set up the driver internal resources specific to
4119 * support the SLI-3 HBA device it attached to.
4120 *
4121 * Return codes
4122 * 0 - successful
4123 * other values - error
4124 **/
4125 static int
4126 lpfc_sli_driver_resource_setup(struct lpfc_hba *phba)
4127 {
4128 struct lpfc_sli *psli;
4129 int rc;
4130
4131 /*
4132 * Initialize timers used by driver
4133 */
4134
4135 /* Heartbeat timer */
4136 init_timer(&phba->hb_tmofunc);
4137 phba->hb_tmofunc.function = lpfc_hb_timeout;
4138 phba->hb_tmofunc.data = (unsigned long)phba;
4139
4140 psli = &phba->sli;
4141 /* MBOX heartbeat timer */
4142 init_timer(&psli->mbox_tmo);
4143 psli->mbox_tmo.function = lpfc_mbox_timeout;
4144 psli->mbox_tmo.data = (unsigned long) phba;
4145 /* FCP polling mode timer */
4146 init_timer(&phba->fcp_poll_timer);
4147 phba->fcp_poll_timer.function = lpfc_poll_timeout;
4148 phba->fcp_poll_timer.data = (unsigned long) phba;
4149 /* Fabric block timer */
4150 init_timer(&phba->fabric_block_timer);
4151 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4152 phba->fabric_block_timer.data = (unsigned long) phba;
4153 /* EA polling mode timer */
4154 init_timer(&phba->eratt_poll);
4155 phba->eratt_poll.function = lpfc_poll_eratt;
4156 phba->eratt_poll.data = (unsigned long) phba;
4157
4158 /* Host attention work mask setup */
4159 phba->work_ha_mask = (HA_ERATT | HA_MBATT | HA_LATT);
4160 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
4161
4162 /* Get all the module params for configuring this host */
4163 lpfc_get_cfgparam(phba);
4164 if (phba->pcidev->device == PCI_DEVICE_ID_HORNET) {
4165 phba->menlo_flag |= HBA_MENLO_SUPPORT;
4166 /* check for menlo minimum sg count */
4167 if (phba->cfg_sg_seg_cnt < LPFC_DEFAULT_MENLO_SG_SEG_CNT)
4168 phba->cfg_sg_seg_cnt = LPFC_DEFAULT_MENLO_SG_SEG_CNT;
4169 }
4170
4171 /*
4172 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4173 * used to create the sg_dma_buf_pool must be dynamically calculated.
4174 * 2 segments are added since the IOCB needs a command and response bde.
4175 */
4176 phba->cfg_sg_dma_buf_size = sizeof(struct fcp_cmnd) +
4177 sizeof(struct fcp_rsp) +
4178 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct ulp_bde64));
4179
4180 if (phba->cfg_enable_bg) {
4181 phba->cfg_sg_seg_cnt = LPFC_MAX_SG_SEG_CNT;
4182 phba->cfg_sg_dma_buf_size +=
4183 phba->cfg_prot_sg_seg_cnt * sizeof(struct ulp_bde64);
4184 }
4185
4186 /* Also reinitialize the host templates with new values. */
4187 lpfc_vport_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4188 lpfc_template.sg_tablesize = phba->cfg_sg_seg_cnt;
4189
4190 phba->max_vpi = LPFC_MAX_VPI;
4191 /* This will be set to correct value after config_port mbox */
4192 phba->max_vports = 0;
4193
4194 /*
4195 * Initialize the SLI Layer to run with lpfc HBAs.
4196 */
4197 lpfc_sli_setup(phba);
4198 lpfc_sli_queue_setup(phba);
4199
4200 /* Allocate device driver memory */
4201 if (lpfc_mem_alloc(phba, BPL_ALIGN_SZ))
4202 return -ENOMEM;
4203
4204 /*
4205 * Enable sr-iov virtual functions if supported and configured
4206 * through the module parameter.
4207 */
4208 if (phba->cfg_sriov_nr_virtfn > 0) {
4209 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4210 phba->cfg_sriov_nr_virtfn);
4211 if (rc) {
4212 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4213 "2808 Requested number of SR-IOV "
4214 "virtual functions (%d) is not "
4215 "supported\n",
4216 phba->cfg_sriov_nr_virtfn);
4217 phba->cfg_sriov_nr_virtfn = 0;
4218 }
4219 }
4220
4221 return 0;
4222 }
4223
4224 /**
4225 * lpfc_sli_driver_resource_unset - Unset drvr internal resources for SLI3 dev
4226 * @phba: pointer to lpfc hba data structure.
4227 *
4228 * This routine is invoked to unset the driver internal resources set up
4229 * specific for supporting the SLI-3 HBA device it attached to.
4230 **/
4231 static void
4232 lpfc_sli_driver_resource_unset(struct lpfc_hba *phba)
4233 {
4234 /* Free device driver memory allocated */
4235 lpfc_mem_free_all(phba);
4236
4237 return;
4238 }
4239
4240 /**
4241 * lpfc_sli4_driver_resource_setup - Setup drvr internal resources for SLI4 dev
4242 * @phba: pointer to lpfc hba data structure.
4243 *
4244 * This routine is invoked to set up the driver internal resources specific to
4245 * support the SLI-4 HBA device it attached to.
4246 *
4247 * Return codes
4248 * 0 - successful
4249 * other values - error
4250 **/
4251 static int
4252 lpfc_sli4_driver_resource_setup(struct lpfc_hba *phba)
4253 {
4254 struct lpfc_sli *psli;
4255 LPFC_MBOXQ_t *mboxq;
4256 int rc, i, hbq_count, buf_size, dma_buf_size, max_buf_size;
4257 uint8_t pn_page[LPFC_MAX_SUPPORTED_PAGES] = {0};
4258 struct lpfc_mqe *mqe;
4259 int longs, sli_family;
4260
4261 /* Before proceed, wait for POST done and device ready */
4262 rc = lpfc_sli4_post_status_check(phba);
4263 if (rc)
4264 return -ENODEV;
4265
4266 /*
4267 * Initialize timers used by driver
4268 */
4269
4270 /* Heartbeat timer */
4271 init_timer(&phba->hb_tmofunc);
4272 phba->hb_tmofunc.function = lpfc_hb_timeout;
4273 phba->hb_tmofunc.data = (unsigned long)phba;
4274 init_timer(&phba->rrq_tmr);
4275 phba->rrq_tmr.function = lpfc_rrq_timeout;
4276 phba->rrq_tmr.data = (unsigned long)phba;
4277
4278 psli = &phba->sli;
4279 /* MBOX heartbeat timer */
4280 init_timer(&psli->mbox_tmo);
4281 psli->mbox_tmo.function = lpfc_mbox_timeout;
4282 psli->mbox_tmo.data = (unsigned long) phba;
4283 /* Fabric block timer */
4284 init_timer(&phba->fabric_block_timer);
4285 phba->fabric_block_timer.function = lpfc_fabric_block_timeout;
4286 phba->fabric_block_timer.data = (unsigned long) phba;
4287 /* EA polling mode timer */
4288 init_timer(&phba->eratt_poll);
4289 phba->eratt_poll.function = lpfc_poll_eratt;
4290 phba->eratt_poll.data = (unsigned long) phba;
4291 /* FCF rediscover timer */
4292 init_timer(&phba->fcf.redisc_wait);
4293 phba->fcf.redisc_wait.function = lpfc_sli4_fcf_redisc_wait_tmo;
4294 phba->fcf.redisc_wait.data = (unsigned long)phba;
4295
4296 /*
4297 * Control structure for handling external multi-buffer mailbox
4298 * command pass-through.
4299 */
4300 memset((uint8_t *)&phba->mbox_ext_buf_ctx, 0,
4301 sizeof(struct lpfc_mbox_ext_buf_ctx));
4302 INIT_LIST_HEAD(&phba->mbox_ext_buf_ctx.ext_dmabuf_list);
4303
4304 /*
4305 * We need to do a READ_CONFIG mailbox command here before
4306 * calling lpfc_get_cfgparam. For VFs this will report the
4307 * MAX_XRI, MAX_VPI, MAX_RPI, MAX_IOCB, and MAX_VFI settings.
4308 * All of the resources allocated
4309 * for this Port are tied to these values.
4310 */
4311 /* Get all the module params for configuring this host */
4312 lpfc_get_cfgparam(phba);
4313 phba->max_vpi = LPFC_MAX_VPI;
4314 /* This will be set to correct value after the read_config mbox */
4315 phba->max_vports = 0;
4316
4317 /* Program the default value of vlan_id and fc_map */
4318 phba->valid_vlan = 0;
4319 phba->fc_map[0] = LPFC_FCOE_FCF_MAP0;
4320 phba->fc_map[1] = LPFC_FCOE_FCF_MAP1;
4321 phba->fc_map[2] = LPFC_FCOE_FCF_MAP2;
4322
4323 /*
4324 * Since the sg_tablesize is module parameter, the sg_dma_buf_size
4325 * used to create the sg_dma_buf_pool must be dynamically calculated.
4326 * 2 segments are added since the IOCB needs a command and response bde.
4327 * To insure that the scsi sgl does not cross a 4k page boundary only
4328 * sgl sizes of must be a power of 2.
4329 */
4330 buf_size = (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp) +
4331 ((phba->cfg_sg_seg_cnt + 2) * sizeof(struct sli4_sge)));
4332
4333 sli_family = bf_get(lpfc_sli_intf_sli_family, &phba->sli4_hba.sli_intf);
4334 max_buf_size = LPFC_SLI4_MAX_BUF_SIZE;
4335 switch (sli_family) {
4336 case LPFC_SLI_INTF_FAMILY_BE2:
4337 case LPFC_SLI_INTF_FAMILY_BE3:
4338 /* There is a single hint for BE - 2 pages per BPL. */
4339 if (bf_get(lpfc_sli_intf_sli_hint1, &phba->sli4_hba.sli_intf) ==
4340 LPFC_SLI_INTF_SLI_HINT1_1)
4341 max_buf_size = LPFC_SLI4_FL1_MAX_BUF_SIZE;
4342 break;
4343 case LPFC_SLI_INTF_FAMILY_LNCR_A0:
4344 case LPFC_SLI_INTF_FAMILY_LNCR_B0:
4345 default:
4346 break;
4347 }
4348 for (dma_buf_size = LPFC_SLI4_MIN_BUF_SIZE;
4349 dma_buf_size < max_buf_size && buf_size > dma_buf_size;
4350 dma_buf_size = dma_buf_size << 1)
4351 ;
4352 if (dma_buf_size == max_buf_size)
4353 phba->cfg_sg_seg_cnt = (dma_buf_size -
4354 sizeof(struct fcp_cmnd) - sizeof(struct fcp_rsp) -
4355 (2 * sizeof(struct sli4_sge))) /
4356 sizeof(struct sli4_sge);
4357 phba->cfg_sg_dma_buf_size = dma_buf_size;
4358
4359 /* Initialize buffer queue management fields */
4360 hbq_count = lpfc_sli_hbq_count();
4361 for (i = 0; i < hbq_count; ++i)
4362 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
4363 INIT_LIST_HEAD(&phba->rb_pend_list);
4364 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_sli4_rb_alloc;
4365 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_sli4_rb_free;
4366
4367 /*
4368 * Initialize the SLI Layer to run with lpfc SLI4 HBAs.
4369 */
4370 /* Initialize the Abort scsi buffer list used by driver */
4371 spin_lock_init(&phba->sli4_hba.abts_scsi_buf_list_lock);
4372 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
4373 /* This abort list used by worker thread */
4374 spin_lock_init(&phba->sli4_hba.abts_sgl_list_lock);
4375
4376 /*
4377 * Initialize driver internal slow-path work queues
4378 */
4379
4380 /* Driver internel slow-path CQ Event pool */
4381 INIT_LIST_HEAD(&phba->sli4_hba.sp_cqe_event_pool);
4382 /* Response IOCB work queue list */
4383 INIT_LIST_HEAD(&phba->sli4_hba.sp_queue_event);
4384 /* Asynchronous event CQ Event work queue list */
4385 INIT_LIST_HEAD(&phba->sli4_hba.sp_asynce_work_queue);
4386 /* Fast-path XRI aborted CQ Event work queue list */
4387 INIT_LIST_HEAD(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue);
4388 /* Slow-path XRI aborted CQ Event work queue list */
4389 INIT_LIST_HEAD(&phba->sli4_hba.sp_els_xri_aborted_work_queue);
4390 /* Receive queue CQ Event work queue list */
4391 INIT_LIST_HEAD(&phba->sli4_hba.sp_unsol_work_queue);
4392
4393 /* Initialize extent block lists. */
4394 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_blk_list);
4395 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_xri_blk_list);
4396 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_vfi_blk_list);
4397 INIT_LIST_HEAD(&phba->lpfc_vpi_blk_list);
4398
4399 /* Initialize the driver internal SLI layer lists. */
4400 lpfc_sli_setup(phba);
4401 lpfc_sli_queue_setup(phba);
4402
4403 /* Allocate device driver memory */
4404 rc = lpfc_mem_alloc(phba, SGL_ALIGN_SZ);
4405 if (rc)
4406 return -ENOMEM;
4407
4408 /* IF Type 2 ports get initialized now. */
4409 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4410 LPFC_SLI_INTF_IF_TYPE_2) {
4411 rc = lpfc_pci_function_reset(phba);
4412 if (unlikely(rc))
4413 return -ENODEV;
4414 }
4415
4416 /* Create the bootstrap mailbox command */
4417 rc = lpfc_create_bootstrap_mbox(phba);
4418 if (unlikely(rc))
4419 goto out_free_mem;
4420
4421 /* Set up the host's endian order with the device. */
4422 rc = lpfc_setup_endian_order(phba);
4423 if (unlikely(rc))
4424 goto out_free_bsmbx;
4425
4426 /* Set up the hba's configuration parameters. */
4427 rc = lpfc_sli4_read_config(phba);
4428 if (unlikely(rc))
4429 goto out_free_bsmbx;
4430
4431 /* IF Type 0 ports get initialized now. */
4432 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) ==
4433 LPFC_SLI_INTF_IF_TYPE_0) {
4434 rc = lpfc_pci_function_reset(phba);
4435 if (unlikely(rc))
4436 goto out_free_bsmbx;
4437 }
4438
4439 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
4440 GFP_KERNEL);
4441 if (!mboxq) {
4442 rc = -ENOMEM;
4443 goto out_free_bsmbx;
4444 }
4445
4446 /* Get the Supported Pages if PORT_CAPABILITIES is supported by port. */
4447 lpfc_supported_pages(mboxq);
4448 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
4449 if (!rc) {
4450 mqe = &mboxq->u.mqe;
4451 memcpy(&pn_page[0], ((uint8_t *)&mqe->un.supp_pages.word3),
4452 LPFC_MAX_SUPPORTED_PAGES);
4453 for (i = 0; i < LPFC_MAX_SUPPORTED_PAGES; i++) {
4454 switch (pn_page[i]) {
4455 case LPFC_SLI4_PARAMETERS:
4456 phba->sli4_hba.pc_sli4_params.supported = 1;
4457 break;
4458 default:
4459 break;
4460 }
4461 }
4462 /* Read the port's SLI4 Parameters capabilities if supported. */
4463 if (phba->sli4_hba.pc_sli4_params.supported)
4464 rc = lpfc_pc_sli4_params_get(phba, mboxq);
4465 if (rc) {
4466 mempool_free(mboxq, phba->mbox_mem_pool);
4467 rc = -EIO;
4468 goto out_free_bsmbx;
4469 }
4470 }
4471 /*
4472 * Get sli4 parameters that override parameters from Port capabilities.
4473 * If this call fails, it isn't critical unless the SLI4 parameters come
4474 * back in conflict.
4475 */
4476 rc = lpfc_get_sli4_parameters(phba, mboxq);
4477 if (rc) {
4478 if (phba->sli4_hba.extents_in_use &&
4479 phba->sli4_hba.rpi_hdrs_in_use) {
4480 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4481 "2999 Unsupported SLI4 Parameters "
4482 "Extents and RPI headers enabled.\n");
4483 goto out_free_bsmbx;
4484 }
4485 }
4486 mempool_free(mboxq, phba->mbox_mem_pool);
4487 /* Create all the SLI4 queues */
4488 rc = lpfc_sli4_queue_create(phba);
4489 if (rc)
4490 goto out_free_bsmbx;
4491
4492 /* Create driver internal CQE event pool */
4493 rc = lpfc_sli4_cq_event_pool_create(phba);
4494 if (rc)
4495 goto out_destroy_queue;
4496
4497 /* Initialize and populate the iocb list per host */
4498 rc = lpfc_init_sgl_list(phba);
4499 if (rc) {
4500 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4501 "1400 Failed to initialize sgl list.\n");
4502 goto out_destroy_cq_event_pool;
4503 }
4504 rc = lpfc_init_active_sgl_array(phba);
4505 if (rc) {
4506 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4507 "1430 Failed to initialize sgl list.\n");
4508 goto out_free_sgl_list;
4509 }
4510 rc = lpfc_sli4_init_rpi_hdrs(phba);
4511 if (rc) {
4512 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4513 "1432 Failed to initialize rpi headers.\n");
4514 goto out_free_active_sgl;
4515 }
4516
4517 /* Allocate eligible FCF bmask memory for FCF roundrobin failover */
4518 longs = (LPFC_SLI4_FCF_TBL_INDX_MAX + BITS_PER_LONG - 1)/BITS_PER_LONG;
4519 phba->fcf.fcf_rr_bmask = kzalloc(longs * sizeof(unsigned long),
4520 GFP_KERNEL);
4521 if (!phba->fcf.fcf_rr_bmask) {
4522 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4523 "2759 Failed allocate memory for FCF round "
4524 "robin failover bmask\n");
4525 rc = -ENOMEM;
4526 goto out_remove_rpi_hdrs;
4527 }
4528
4529 phba->sli4_hba.fcp_eq_hdl = kzalloc((sizeof(struct lpfc_fcp_eq_hdl) *
4530 phba->cfg_fcp_eq_count), GFP_KERNEL);
4531 if (!phba->sli4_hba.fcp_eq_hdl) {
4532 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4533 "2572 Failed allocate memory for fast-path "
4534 "per-EQ handle array\n");
4535 rc = -ENOMEM;
4536 goto out_free_fcf_rr_bmask;
4537 }
4538
4539 phba->sli4_hba.msix_entries = kzalloc((sizeof(struct msix_entry) *
4540 phba->sli4_hba.cfg_eqn), GFP_KERNEL);
4541 if (!phba->sli4_hba.msix_entries) {
4542 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4543 "2573 Failed allocate memory for msi-x "
4544 "interrupt vector entries\n");
4545 rc = -ENOMEM;
4546 goto out_free_fcp_eq_hdl;
4547 }
4548
4549 /*
4550 * Enable sr-iov virtual functions if supported and configured
4551 * through the module parameter.
4552 */
4553 if (phba->cfg_sriov_nr_virtfn > 0) {
4554 rc = lpfc_sli_probe_sriov_nr_virtfn(phba,
4555 phba->cfg_sriov_nr_virtfn);
4556 if (rc) {
4557 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
4558 "3020 Requested number of SR-IOV "
4559 "virtual functions (%d) is not "
4560 "supported\n",
4561 phba->cfg_sriov_nr_virtfn);
4562 phba->cfg_sriov_nr_virtfn = 0;
4563 }
4564 }
4565
4566 return 0;
4567
4568 out_free_fcp_eq_hdl:
4569 kfree(phba->sli4_hba.fcp_eq_hdl);
4570 out_free_fcf_rr_bmask:
4571 kfree(phba->fcf.fcf_rr_bmask);
4572 out_remove_rpi_hdrs:
4573 lpfc_sli4_remove_rpi_hdrs(phba);
4574 out_free_active_sgl:
4575 lpfc_free_active_sgl(phba);
4576 out_free_sgl_list:
4577 lpfc_free_sgl_list(phba);
4578 out_destroy_cq_event_pool:
4579 lpfc_sli4_cq_event_pool_destroy(phba);
4580 out_destroy_queue:
4581 lpfc_sli4_queue_destroy(phba);
4582 out_free_bsmbx:
4583 lpfc_destroy_bootstrap_mbox(phba);
4584 out_free_mem:
4585 lpfc_mem_free(phba);
4586 return rc;
4587 }
4588
4589 /**
4590 * lpfc_sli4_driver_resource_unset - Unset drvr internal resources for SLI4 dev
4591 * @phba: pointer to lpfc hba data structure.
4592 *
4593 * This routine is invoked to unset the driver internal resources set up
4594 * specific for supporting the SLI-4 HBA device it attached to.
4595 **/
4596 static void
4597 lpfc_sli4_driver_resource_unset(struct lpfc_hba *phba)
4598 {
4599 struct lpfc_fcf_conn_entry *conn_entry, *next_conn_entry;
4600
4601 /* Free memory allocated for msi-x interrupt vector entries */
4602 kfree(phba->sli4_hba.msix_entries);
4603
4604 /* Free memory allocated for fast-path work queue handles */
4605 kfree(phba->sli4_hba.fcp_eq_hdl);
4606
4607 /* Free the allocated rpi headers. */
4608 lpfc_sli4_remove_rpi_hdrs(phba);
4609 lpfc_sli4_remove_rpis(phba);
4610
4611 /* Free eligible FCF index bmask */
4612 kfree(phba->fcf.fcf_rr_bmask);
4613
4614 /* Free the ELS sgl list */
4615 lpfc_free_active_sgl(phba);
4616 lpfc_free_sgl_list(phba);
4617
4618 /* Free the SCSI sgl management array */
4619 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
4620
4621 /* Free the SLI4 queues */
4622 lpfc_sli4_queue_destroy(phba);
4623
4624 /* Free the completion queue EQ event pool */
4625 lpfc_sli4_cq_event_release_all(phba);
4626 lpfc_sli4_cq_event_pool_destroy(phba);
4627
4628 /* Release resource identifiers. */
4629 lpfc_sli4_dealloc_resource_identifiers(phba);
4630
4631 /* Free the bsmbx region. */
4632 lpfc_destroy_bootstrap_mbox(phba);
4633
4634 /* Free the SLI Layer memory with SLI4 HBAs */
4635 lpfc_mem_free_all(phba);
4636
4637 /* Free the current connect table */
4638 list_for_each_entry_safe(conn_entry, next_conn_entry,
4639 &phba->fcf_conn_rec_list, list) {
4640 list_del_init(&conn_entry->list);
4641 kfree(conn_entry);
4642 }
4643
4644 return;
4645 }
4646
4647 /**
4648 * lpfc_init_api_table_setup - Set up init api function jump table
4649 * @phba: The hba struct for which this call is being executed.
4650 * @dev_grp: The HBA PCI-Device group number.
4651 *
4652 * This routine sets up the device INIT interface API function jump table
4653 * in @phba struct.
4654 *
4655 * Returns: 0 - success, -ENODEV - failure.
4656 **/
4657 int
4658 lpfc_init_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4659 {
4660 phba->lpfc_hba_init_link = lpfc_hba_init_link;
4661 phba->lpfc_hba_down_link = lpfc_hba_down_link;
4662 phba->lpfc_selective_reset = lpfc_selective_reset;
4663 switch (dev_grp) {
4664 case LPFC_PCI_DEV_LP:
4665 phba->lpfc_hba_down_post = lpfc_hba_down_post_s3;
4666 phba->lpfc_handle_eratt = lpfc_handle_eratt_s3;
4667 phba->lpfc_stop_port = lpfc_stop_port_s3;
4668 break;
4669 case LPFC_PCI_DEV_OC:
4670 phba->lpfc_hba_down_post = lpfc_hba_down_post_s4;
4671 phba->lpfc_handle_eratt = lpfc_handle_eratt_s4;
4672 phba->lpfc_stop_port = lpfc_stop_port_s4;
4673 break;
4674 default:
4675 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4676 "1431 Invalid HBA PCI-device group: 0x%x\n",
4677 dev_grp);
4678 return -ENODEV;
4679 break;
4680 }
4681 return 0;
4682 }
4683
4684 /**
4685 * lpfc_setup_driver_resource_phase1 - Phase1 etup driver internal resources.
4686 * @phba: pointer to lpfc hba data structure.
4687 *
4688 * This routine is invoked to set up the driver internal resources before the
4689 * device specific resource setup to support the HBA device it attached to.
4690 *
4691 * Return codes
4692 * 0 - successful
4693 * other values - error
4694 **/
4695 static int
4696 lpfc_setup_driver_resource_phase1(struct lpfc_hba *phba)
4697 {
4698 /*
4699 * Driver resources common to all SLI revisions
4700 */
4701 atomic_set(&phba->fast_event_count, 0);
4702 spin_lock_init(&phba->hbalock);
4703
4704 /* Initialize ndlp management spinlock */
4705 spin_lock_init(&phba->ndlp_lock);
4706
4707 INIT_LIST_HEAD(&phba->port_list);
4708 INIT_LIST_HEAD(&phba->work_list);
4709 init_waitqueue_head(&phba->wait_4_mlo_m_q);
4710
4711 /* Initialize the wait queue head for the kernel thread */
4712 init_waitqueue_head(&phba->work_waitq);
4713
4714 /* Initialize the scsi buffer list used by driver for scsi IO */
4715 spin_lock_init(&phba->scsi_buf_list_lock);
4716 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
4717
4718 /* Initialize the fabric iocb list */
4719 INIT_LIST_HEAD(&phba->fabric_iocb_list);
4720
4721 /* Initialize list to save ELS buffers */
4722 INIT_LIST_HEAD(&phba->elsbuf);
4723
4724 /* Initialize FCF connection rec list */
4725 INIT_LIST_HEAD(&phba->fcf_conn_rec_list);
4726
4727 return 0;
4728 }
4729
4730 /**
4731 * lpfc_setup_driver_resource_phase2 - Phase2 setup driver internal resources.
4732 * @phba: pointer to lpfc hba data structure.
4733 *
4734 * This routine is invoked to set up the driver internal resources after the
4735 * device specific resource setup to support the HBA device it attached to.
4736 *
4737 * Return codes
4738 * 0 - successful
4739 * other values - error
4740 **/
4741 static int
4742 lpfc_setup_driver_resource_phase2(struct lpfc_hba *phba)
4743 {
4744 int error;
4745
4746 /* Startup the kernel thread for this host adapter. */
4747 phba->worker_thread = kthread_run(lpfc_do_work, phba,
4748 "lpfc_worker_%d", phba->brd_no);
4749 if (IS_ERR(phba->worker_thread)) {
4750 error = PTR_ERR(phba->worker_thread);
4751 return error;
4752 }
4753
4754 return 0;
4755 }
4756
4757 /**
4758 * lpfc_unset_driver_resource_phase2 - Phase2 unset driver internal resources.
4759 * @phba: pointer to lpfc hba data structure.
4760 *
4761 * This routine is invoked to unset the driver internal resources set up after
4762 * the device specific resource setup for supporting the HBA device it
4763 * attached to.
4764 **/
4765 static void
4766 lpfc_unset_driver_resource_phase2(struct lpfc_hba *phba)
4767 {
4768 /* Stop kernel worker thread */
4769 kthread_stop(phba->worker_thread);
4770 }
4771
4772 /**
4773 * lpfc_free_iocb_list - Free iocb list.
4774 * @phba: pointer to lpfc hba data structure.
4775 *
4776 * This routine is invoked to free the driver's IOCB list and memory.
4777 **/
4778 static void
4779 lpfc_free_iocb_list(struct lpfc_hba *phba)
4780 {
4781 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
4782
4783 spin_lock_irq(&phba->hbalock);
4784 list_for_each_entry_safe(iocbq_entry, iocbq_next,
4785 &phba->lpfc_iocb_list, list) {
4786 list_del(&iocbq_entry->list);
4787 kfree(iocbq_entry);
4788 phba->total_iocbq_bufs--;
4789 }
4790 spin_unlock_irq(&phba->hbalock);
4791
4792 return;
4793 }
4794
4795 /**
4796 * lpfc_init_iocb_list - Allocate and initialize iocb list.
4797 * @phba: pointer to lpfc hba data structure.
4798 *
4799 * This routine is invoked to allocate and initizlize the driver's IOCB
4800 * list and set up the IOCB tag array accordingly.
4801 *
4802 * Return codes
4803 * 0 - successful
4804 * other values - error
4805 **/
4806 static int
4807 lpfc_init_iocb_list(struct lpfc_hba *phba, int iocb_count)
4808 {
4809 struct lpfc_iocbq *iocbq_entry = NULL;
4810 uint16_t iotag;
4811 int i;
4812
4813 /* Initialize and populate the iocb list per host. */
4814 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
4815 for (i = 0; i < iocb_count; i++) {
4816 iocbq_entry = kzalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
4817 if (iocbq_entry == NULL) {
4818 printk(KERN_ERR "%s: only allocated %d iocbs of "
4819 "expected %d count. Unloading driver.\n",
4820 __func__, i, LPFC_IOCB_LIST_CNT);
4821 goto out_free_iocbq;
4822 }
4823
4824 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
4825 if (iotag == 0) {
4826 kfree(iocbq_entry);
4827 printk(KERN_ERR "%s: failed to allocate IOTAG. "
4828 "Unloading driver.\n", __func__);
4829 goto out_free_iocbq;
4830 }
4831 iocbq_entry->sli4_lxritag = NO_XRI;
4832 iocbq_entry->sli4_xritag = NO_XRI;
4833
4834 spin_lock_irq(&phba->hbalock);
4835 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
4836 phba->total_iocbq_bufs++;
4837 spin_unlock_irq(&phba->hbalock);
4838 }
4839
4840 return 0;
4841
4842 out_free_iocbq:
4843 lpfc_free_iocb_list(phba);
4844
4845 return -ENOMEM;
4846 }
4847
4848 /**
4849 * lpfc_free_sgl_list - Free sgl list.
4850 * @phba: pointer to lpfc hba data structure.
4851 *
4852 * This routine is invoked to free the driver's sgl list and memory.
4853 **/
4854 static void
4855 lpfc_free_sgl_list(struct lpfc_hba *phba)
4856 {
4857 struct lpfc_sglq *sglq_entry = NULL, *sglq_next = NULL;
4858 LIST_HEAD(sglq_list);
4859
4860 spin_lock_irq(&phba->hbalock);
4861 list_splice_init(&phba->sli4_hba.lpfc_sgl_list, &sglq_list);
4862 spin_unlock_irq(&phba->hbalock);
4863
4864 list_for_each_entry_safe(sglq_entry, sglq_next,
4865 &sglq_list, list) {
4866 list_del(&sglq_entry->list);
4867 lpfc_mbuf_free(phba, sglq_entry->virt, sglq_entry->phys);
4868 kfree(sglq_entry);
4869 phba->sli4_hba.total_sglq_bufs--;
4870 }
4871 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4872 }
4873
4874 /**
4875 * lpfc_init_active_sgl_array - Allocate the buf to track active ELS XRIs.
4876 * @phba: pointer to lpfc hba data structure.
4877 *
4878 * This routine is invoked to allocate the driver's active sgl memory.
4879 * This array will hold the sglq_entry's for active IOs.
4880 **/
4881 static int
4882 lpfc_init_active_sgl_array(struct lpfc_hba *phba)
4883 {
4884 int size;
4885 size = sizeof(struct lpfc_sglq *);
4886 size *= phba->sli4_hba.max_cfg_param.max_xri;
4887
4888 phba->sli4_hba.lpfc_sglq_active_list =
4889 kzalloc(size, GFP_KERNEL);
4890 if (!phba->sli4_hba.lpfc_sglq_active_list)
4891 return -ENOMEM;
4892 return 0;
4893 }
4894
4895 /**
4896 * lpfc_free_active_sgl - Free the buf that tracks active ELS XRIs.
4897 * @phba: pointer to lpfc hba data structure.
4898 *
4899 * This routine is invoked to walk through the array of active sglq entries
4900 * and free all of the resources.
4901 * This is just a place holder for now.
4902 **/
4903 static void
4904 lpfc_free_active_sgl(struct lpfc_hba *phba)
4905 {
4906 kfree(phba->sli4_hba.lpfc_sglq_active_list);
4907 }
4908
4909 /**
4910 * lpfc_init_sgl_list - Allocate and initialize sgl list.
4911 * @phba: pointer to lpfc hba data structure.
4912 *
4913 * This routine is invoked to allocate and initizlize the driver's sgl
4914 * list and set up the sgl xritag tag array accordingly.
4915 *
4916 * Return codes
4917 * 0 - successful
4918 * other values - error
4919 **/
4920 static int
4921 lpfc_init_sgl_list(struct lpfc_hba *phba)
4922 {
4923 struct lpfc_sglq *sglq_entry = NULL;
4924 int i;
4925 int els_xri_cnt;
4926
4927 els_xri_cnt = lpfc_sli4_get_els_iocb_cnt(phba);
4928 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
4929 "2400 ELS XRI count %d.\n",
4930 els_xri_cnt);
4931 /* Initialize and populate the sglq list per host/VF. */
4932 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_sgl_list);
4933 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_abts_els_sgl_list);
4934
4935 /* Sanity check on XRI management */
4936 if (phba->sli4_hba.max_cfg_param.max_xri <= els_xri_cnt) {
4937 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4938 "2562 No room left for SCSI XRI allocation: "
4939 "max_xri=%d, els_xri=%d\n",
4940 phba->sli4_hba.max_cfg_param.max_xri,
4941 els_xri_cnt);
4942 return -ENOMEM;
4943 }
4944
4945 /* Allocate memory for the ELS XRI management array */
4946 phba->sli4_hba.lpfc_els_sgl_array =
4947 kzalloc((sizeof(struct lpfc_sglq *) * els_xri_cnt),
4948 GFP_KERNEL);
4949
4950 if (!phba->sli4_hba.lpfc_els_sgl_array) {
4951 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4952 "2401 Failed to allocate memory for ELS "
4953 "XRI management array of size %d.\n",
4954 els_xri_cnt);
4955 return -ENOMEM;
4956 }
4957
4958 /* Keep the SCSI XRI into the XRI management array */
4959 phba->sli4_hba.scsi_xri_max =
4960 phba->sli4_hba.max_cfg_param.max_xri - els_xri_cnt;
4961 phba->sli4_hba.scsi_xri_cnt = 0;
4962 phba->sli4_hba.lpfc_scsi_psb_array =
4963 kzalloc((sizeof(struct lpfc_scsi_buf *) *
4964 phba->sli4_hba.scsi_xri_max), GFP_KERNEL);
4965
4966 if (!phba->sli4_hba.lpfc_scsi_psb_array) {
4967 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
4968 "2563 Failed to allocate memory for SCSI "
4969 "XRI management array of size %d.\n",
4970 phba->sli4_hba.scsi_xri_max);
4971 kfree(phba->sli4_hba.lpfc_els_sgl_array);
4972 return -ENOMEM;
4973 }
4974
4975 for (i = 0; i < els_xri_cnt; i++) {
4976 sglq_entry = kzalloc(sizeof(struct lpfc_sglq), GFP_KERNEL);
4977 if (sglq_entry == NULL) {
4978 printk(KERN_ERR "%s: only allocated %d sgls of "
4979 "expected %d count. Unloading driver.\n",
4980 __func__, i, els_xri_cnt);
4981 goto out_free_mem;
4982 }
4983
4984 sglq_entry->buff_type = GEN_BUFF_TYPE;
4985 sglq_entry->virt = lpfc_mbuf_alloc(phba, 0, &sglq_entry->phys);
4986 if (sglq_entry->virt == NULL) {
4987 kfree(sglq_entry);
4988 printk(KERN_ERR "%s: failed to allocate mbuf.\n"
4989 "Unloading driver.\n", __func__);
4990 goto out_free_mem;
4991 }
4992 sglq_entry->sgl = sglq_entry->virt;
4993 memset(sglq_entry->sgl, 0, LPFC_BPL_SIZE);
4994
4995 /* The list order is used by later block SGL registraton */
4996 spin_lock_irq(&phba->hbalock);
4997 sglq_entry->state = SGL_FREED;
4998 list_add_tail(&sglq_entry->list, &phba->sli4_hba.lpfc_sgl_list);
4999 phba->sli4_hba.lpfc_els_sgl_array[i] = sglq_entry;
5000 phba->sli4_hba.total_sglq_bufs++;
5001 spin_unlock_irq(&phba->hbalock);
5002 }
5003 return 0;
5004
5005 out_free_mem:
5006 kfree(phba->sli4_hba.lpfc_scsi_psb_array);
5007 lpfc_free_sgl_list(phba);
5008 return -ENOMEM;
5009 }
5010
5011 /**
5012 * lpfc_sli4_init_rpi_hdrs - Post the rpi header memory region to the port
5013 * @phba: pointer to lpfc hba data structure.
5014 *
5015 * This routine is invoked to post rpi header templates to the
5016 * port for those SLI4 ports that do not support extents. This routine
5017 * posts a PAGE_SIZE memory region to the port to hold up to
5018 * PAGE_SIZE modulo 64 rpi context headers. This is an initialization routine
5019 * and should be called only when interrupts are disabled.
5020 *
5021 * Return codes
5022 * 0 - successful
5023 * -ERROR - otherwise.
5024 **/
5025 int
5026 lpfc_sli4_init_rpi_hdrs(struct lpfc_hba *phba)
5027 {
5028 int rc = 0;
5029 struct lpfc_rpi_hdr *rpi_hdr;
5030
5031 INIT_LIST_HEAD(&phba->sli4_hba.lpfc_rpi_hdr_list);
5032 /*
5033 * If the SLI4 port supports extents, posting the rpi header isn't
5034 * required. Set the expected maximum count and let the actual value
5035 * get set when extents are fully allocated.
5036 */
5037 if (!phba->sli4_hba.rpi_hdrs_in_use) {
5038 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.max_rpi;
5039 return rc;
5040 }
5041 if (phba->sli4_hba.extents_in_use)
5042 return -EIO;
5043
5044 rpi_hdr = lpfc_sli4_create_rpi_hdr(phba);
5045 if (!rpi_hdr) {
5046 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
5047 "0391 Error during rpi post operation\n");
5048 lpfc_sli4_remove_rpis(phba);
5049 rc = -ENODEV;
5050 }
5051
5052 return rc;
5053 }
5054
5055 /**
5056 * lpfc_sli4_create_rpi_hdr - Allocate an rpi header memory region
5057 * @phba: pointer to lpfc hba data structure.
5058 *
5059 * This routine is invoked to allocate a single 4KB memory region to
5060 * support rpis and stores them in the phba. This single region
5061 * provides support for up to 64 rpis. The region is used globally
5062 * by the device.
5063 *
5064 * Returns:
5065 * A valid rpi hdr on success.
5066 * A NULL pointer on any failure.
5067 **/
5068 struct lpfc_rpi_hdr *
5069 lpfc_sli4_create_rpi_hdr(struct lpfc_hba *phba)
5070 {
5071 uint16_t rpi_limit, curr_rpi_range;
5072 struct lpfc_dmabuf *dmabuf;
5073 struct lpfc_rpi_hdr *rpi_hdr;
5074 uint32_t rpi_count;
5075
5076 /*
5077 * If the SLI4 port supports extents, posting the rpi header isn't
5078 * required. Set the expected maximum count and let the actual value
5079 * get set when extents are fully allocated.
5080 */
5081 if (!phba->sli4_hba.rpi_hdrs_in_use)
5082 return NULL;
5083 if (phba->sli4_hba.extents_in_use)
5084 return NULL;
5085
5086 /* The limit on the logical index is just the max_rpi count. */
5087 rpi_limit = phba->sli4_hba.max_cfg_param.rpi_base +
5088 phba->sli4_hba.max_cfg_param.max_rpi - 1;
5089
5090 spin_lock_irq(&phba->hbalock);
5091 /*
5092 * Establish the starting RPI in this header block. The starting
5093 * rpi is normalized to a zero base because the physical rpi is
5094 * port based.
5095 */
5096 curr_rpi_range = phba->sli4_hba.next_rpi -
5097 phba->sli4_hba.max_cfg_param.rpi_base;
5098 spin_unlock_irq(&phba->hbalock);
5099
5100 /*
5101 * The port has a limited number of rpis. The increment here
5102 * is LPFC_RPI_HDR_COUNT - 1 to account for the starting value
5103 * and to allow the full max_rpi range per port.
5104 */
5105 if ((curr_rpi_range + (LPFC_RPI_HDR_COUNT - 1)) > rpi_limit)
5106 rpi_count = rpi_limit - curr_rpi_range;
5107 else
5108 rpi_count = LPFC_RPI_HDR_COUNT;
5109
5110 if (!rpi_count)
5111 return NULL;
5112 /*
5113 * First allocate the protocol header region for the port. The
5114 * port expects a 4KB DMA-mapped memory region that is 4K aligned.
5115 */
5116 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5117 if (!dmabuf)
5118 return NULL;
5119
5120 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5121 LPFC_HDR_TEMPLATE_SIZE,
5122 &dmabuf->phys,
5123 GFP_KERNEL);
5124 if (!dmabuf->virt) {
5125 rpi_hdr = NULL;
5126 goto err_free_dmabuf;
5127 }
5128
5129 memset(dmabuf->virt, 0, LPFC_HDR_TEMPLATE_SIZE);
5130 if (!IS_ALIGNED(dmabuf->phys, LPFC_HDR_TEMPLATE_SIZE)) {
5131 rpi_hdr = NULL;
5132 goto err_free_coherent;
5133 }
5134
5135 /* Save the rpi header data for cleanup later. */
5136 rpi_hdr = kzalloc(sizeof(struct lpfc_rpi_hdr), GFP_KERNEL);
5137 if (!rpi_hdr)
5138 goto err_free_coherent;
5139
5140 rpi_hdr->dmabuf = dmabuf;
5141 rpi_hdr->len = LPFC_HDR_TEMPLATE_SIZE;
5142 rpi_hdr->page_count = 1;
5143 spin_lock_irq(&phba->hbalock);
5144
5145 /* The rpi_hdr stores the logical index only. */
5146 rpi_hdr->start_rpi = curr_rpi_range;
5147 list_add_tail(&rpi_hdr->list, &phba->sli4_hba.lpfc_rpi_hdr_list);
5148
5149 /*
5150 * The next_rpi stores the next logical module-64 rpi value used
5151 * to post physical rpis in subsequent rpi postings.
5152 */
5153 phba->sli4_hba.next_rpi += rpi_count;
5154 spin_unlock_irq(&phba->hbalock);
5155 return rpi_hdr;
5156
5157 err_free_coherent:
5158 dma_free_coherent(&phba->pcidev->dev, LPFC_HDR_TEMPLATE_SIZE,
5159 dmabuf->virt, dmabuf->phys);
5160 err_free_dmabuf:
5161 kfree(dmabuf);
5162 return NULL;
5163 }
5164
5165 /**
5166 * lpfc_sli4_remove_rpi_hdrs - Remove all rpi header memory regions
5167 * @phba: pointer to lpfc hba data structure.
5168 *
5169 * This routine is invoked to remove all memory resources allocated
5170 * to support rpis for SLI4 ports not supporting extents. This routine
5171 * presumes the caller has released all rpis consumed by fabric or port
5172 * logins and is prepared to have the header pages removed.
5173 **/
5174 void
5175 lpfc_sli4_remove_rpi_hdrs(struct lpfc_hba *phba)
5176 {
5177 struct lpfc_rpi_hdr *rpi_hdr, *next_rpi_hdr;
5178
5179 if (!phba->sli4_hba.rpi_hdrs_in_use)
5180 goto exit;
5181
5182 list_for_each_entry_safe(rpi_hdr, next_rpi_hdr,
5183 &phba->sli4_hba.lpfc_rpi_hdr_list, list) {
5184 list_del(&rpi_hdr->list);
5185 dma_free_coherent(&phba->pcidev->dev, rpi_hdr->len,
5186 rpi_hdr->dmabuf->virt, rpi_hdr->dmabuf->phys);
5187 kfree(rpi_hdr->dmabuf);
5188 kfree(rpi_hdr);
5189 }
5190 exit:
5191 /* There are no rpis available to the port now. */
5192 phba->sli4_hba.next_rpi = 0;
5193 }
5194
5195 /**
5196 * lpfc_hba_alloc - Allocate driver hba data structure for a device.
5197 * @pdev: pointer to pci device data structure.
5198 *
5199 * This routine is invoked to allocate the driver hba data structure for an
5200 * HBA device. If the allocation is successful, the phba reference to the
5201 * PCI device data structure is set.
5202 *
5203 * Return codes
5204 * pointer to @phba - successful
5205 * NULL - error
5206 **/
5207 static struct lpfc_hba *
5208 lpfc_hba_alloc(struct pci_dev *pdev)
5209 {
5210 struct lpfc_hba *phba;
5211
5212 /* Allocate memory for HBA structure */
5213 phba = kzalloc(sizeof(struct lpfc_hba), GFP_KERNEL);
5214 if (!phba) {
5215 dev_err(&pdev->dev, "failed to allocate hba struct\n");
5216 return NULL;
5217 }
5218
5219 /* Set reference to PCI device in HBA structure */
5220 phba->pcidev = pdev;
5221
5222 /* Assign an unused board number */
5223 phba->brd_no = lpfc_get_instance();
5224 if (phba->brd_no < 0) {
5225 kfree(phba);
5226 return NULL;
5227 }
5228
5229 spin_lock_init(&phba->ct_ev_lock);
5230 INIT_LIST_HEAD(&phba->ct_ev_waiters);
5231
5232 return phba;
5233 }
5234
5235 /**
5236 * lpfc_hba_free - Free driver hba data structure with a device.
5237 * @phba: pointer to lpfc hba data structure.
5238 *
5239 * This routine is invoked to free the driver hba data structure with an
5240 * HBA device.
5241 **/
5242 static void
5243 lpfc_hba_free(struct lpfc_hba *phba)
5244 {
5245 /* Release the driver assigned board number */
5246 idr_remove(&lpfc_hba_index, phba->brd_no);
5247
5248 kfree(phba);
5249 return;
5250 }
5251
5252 /**
5253 * lpfc_create_shost - Create hba physical port with associated scsi host.
5254 * @phba: pointer to lpfc hba data structure.
5255 *
5256 * This routine is invoked to create HBA physical port and associate a SCSI
5257 * host with it.
5258 *
5259 * Return codes
5260 * 0 - successful
5261 * other values - error
5262 **/
5263 static int
5264 lpfc_create_shost(struct lpfc_hba *phba)
5265 {
5266 struct lpfc_vport *vport;
5267 struct Scsi_Host *shost;
5268
5269 /* Initialize HBA FC structure */
5270 phba->fc_edtov = FF_DEF_EDTOV;
5271 phba->fc_ratov = FF_DEF_RATOV;
5272 phba->fc_altov = FF_DEF_ALTOV;
5273 phba->fc_arbtov = FF_DEF_ARBTOV;
5274
5275 atomic_set(&phba->sdev_cnt, 0);
5276 vport = lpfc_create_port(phba, phba->brd_no, &phba->pcidev->dev);
5277 if (!vport)
5278 return -ENODEV;
5279
5280 shost = lpfc_shost_from_vport(vport);
5281 phba->pport = vport;
5282 lpfc_debugfs_initialize(vport);
5283 /* Put reference to SCSI host to driver's device private data */
5284 pci_set_drvdata(phba->pcidev, shost);
5285
5286 return 0;
5287 }
5288
5289 /**
5290 * lpfc_destroy_shost - Destroy hba physical port with associated scsi host.
5291 * @phba: pointer to lpfc hba data structure.
5292 *
5293 * This routine is invoked to destroy HBA physical port and the associated
5294 * SCSI host.
5295 **/
5296 static void
5297 lpfc_destroy_shost(struct lpfc_hba *phba)
5298 {
5299 struct lpfc_vport *vport = phba->pport;
5300
5301 /* Destroy physical port that associated with the SCSI host */
5302 destroy_port(vport);
5303
5304 return;
5305 }
5306
5307 /**
5308 * lpfc_setup_bg - Setup Block guard structures and debug areas.
5309 * @phba: pointer to lpfc hba data structure.
5310 * @shost: the shost to be used to detect Block guard settings.
5311 *
5312 * This routine sets up the local Block guard protocol settings for @shost.
5313 * This routine also allocates memory for debugging bg buffers.
5314 **/
5315 static void
5316 lpfc_setup_bg(struct lpfc_hba *phba, struct Scsi_Host *shost)
5317 {
5318 int pagecnt = 10;
5319 if (lpfc_prot_mask && lpfc_prot_guard) {
5320 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5321 "1478 Registering BlockGuard with the "
5322 "SCSI layer\n");
5323 scsi_host_set_prot(shost, lpfc_prot_mask);
5324 scsi_host_set_guard(shost, lpfc_prot_guard);
5325 }
5326 if (!_dump_buf_data) {
5327 while (pagecnt) {
5328 spin_lock_init(&_dump_buf_lock);
5329 _dump_buf_data =
5330 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5331 if (_dump_buf_data) {
5332 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5333 "9043 BLKGRD: allocated %d pages for "
5334 "_dump_buf_data at 0x%p\n",
5335 (1 << pagecnt), _dump_buf_data);
5336 _dump_buf_data_order = pagecnt;
5337 memset(_dump_buf_data, 0,
5338 ((1 << PAGE_SHIFT) << pagecnt));
5339 break;
5340 } else
5341 --pagecnt;
5342 }
5343 if (!_dump_buf_data_order)
5344 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5345 "9044 BLKGRD: ERROR unable to allocate "
5346 "memory for hexdump\n");
5347 } else
5348 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5349 "9045 BLKGRD: already allocated _dump_buf_data=0x%p"
5350 "\n", _dump_buf_data);
5351 if (!_dump_buf_dif) {
5352 while (pagecnt) {
5353 _dump_buf_dif =
5354 (char *) __get_free_pages(GFP_KERNEL, pagecnt);
5355 if (_dump_buf_dif) {
5356 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5357 "9046 BLKGRD: allocated %d pages for "
5358 "_dump_buf_dif at 0x%p\n",
5359 (1 << pagecnt), _dump_buf_dif);
5360 _dump_buf_dif_order = pagecnt;
5361 memset(_dump_buf_dif, 0,
5362 ((1 << PAGE_SHIFT) << pagecnt));
5363 break;
5364 } else
5365 --pagecnt;
5366 }
5367 if (!_dump_buf_dif_order)
5368 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5369 "9047 BLKGRD: ERROR unable to allocate "
5370 "memory for hexdump\n");
5371 } else
5372 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
5373 "9048 BLKGRD: already allocated _dump_buf_dif=0x%p\n",
5374 _dump_buf_dif);
5375 }
5376
5377 /**
5378 * lpfc_post_init_setup - Perform necessary device post initialization setup.
5379 * @phba: pointer to lpfc hba data structure.
5380 *
5381 * This routine is invoked to perform all the necessary post initialization
5382 * setup for the device.
5383 **/
5384 static void
5385 lpfc_post_init_setup(struct lpfc_hba *phba)
5386 {
5387 struct Scsi_Host *shost;
5388 struct lpfc_adapter_event_header adapter_event;
5389
5390 /* Get the default values for Model Name and Description */
5391 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
5392
5393 /*
5394 * hba setup may have changed the hba_queue_depth so we need to
5395 * adjust the value of can_queue.
5396 */
5397 shost = pci_get_drvdata(phba->pcidev);
5398 shost->can_queue = phba->cfg_hba_queue_depth - 10;
5399 if (phba->sli3_options & LPFC_SLI3_BG_ENABLED)
5400 lpfc_setup_bg(phba, shost);
5401
5402 lpfc_host_attrib_init(shost);
5403
5404 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
5405 spin_lock_irq(shost->host_lock);
5406 lpfc_poll_start_timer(phba);
5407 spin_unlock_irq(shost->host_lock);
5408 }
5409
5410 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5411 "0428 Perform SCSI scan\n");
5412 /* Send board arrival event to upper layer */
5413 adapter_event.event_type = FC_REG_ADAPTER_EVENT;
5414 adapter_event.subcategory = LPFC_EVENT_ARRIVAL;
5415 fc_host_post_vendor_event(shost, fc_get_event_number(),
5416 sizeof(adapter_event),
5417 (char *) &adapter_event,
5418 LPFC_NL_VENDOR_ID);
5419 return;
5420 }
5421
5422 /**
5423 * lpfc_sli_pci_mem_setup - Setup SLI3 HBA PCI memory space.
5424 * @phba: pointer to lpfc hba data structure.
5425 *
5426 * This routine is invoked to set up the PCI device memory space for device
5427 * with SLI-3 interface spec.
5428 *
5429 * Return codes
5430 * 0 - successful
5431 * other values - error
5432 **/
5433 static int
5434 lpfc_sli_pci_mem_setup(struct lpfc_hba *phba)
5435 {
5436 struct pci_dev *pdev;
5437 unsigned long bar0map_len, bar2map_len;
5438 int i, hbq_count;
5439 void *ptr;
5440 int error = -ENODEV;
5441
5442 /* Obtain PCI device reference */
5443 if (!phba->pcidev)
5444 return error;
5445 else
5446 pdev = phba->pcidev;
5447
5448 /* Set the device DMA mask size */
5449 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
5450 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
5451 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
5452 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
5453 return error;
5454 }
5455 }
5456
5457 /* Get the bus address of Bar0 and Bar2 and the number of bytes
5458 * required by each mapping.
5459 */
5460 phba->pci_bar0_map = pci_resource_start(pdev, 0);
5461 bar0map_len = pci_resource_len(pdev, 0);
5462
5463 phba->pci_bar2_map = pci_resource_start(pdev, 2);
5464 bar2map_len = pci_resource_len(pdev, 2);
5465
5466 /* Map HBA SLIM to a kernel virtual address. */
5467 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
5468 if (!phba->slim_memmap_p) {
5469 dev_printk(KERN_ERR, &pdev->dev,
5470 "ioremap failed for SLIM memory.\n");
5471 goto out;
5472 }
5473
5474 /* Map HBA Control Registers to a kernel virtual address. */
5475 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
5476 if (!phba->ctrl_regs_memmap_p) {
5477 dev_printk(KERN_ERR, &pdev->dev,
5478 "ioremap failed for HBA control registers.\n");
5479 goto out_iounmap_slim;
5480 }
5481
5482 /* Allocate memory for SLI-2 structures */
5483 phba->slim2p.virt = dma_alloc_coherent(&pdev->dev,
5484 SLI2_SLIM_SIZE,
5485 &phba->slim2p.phys,
5486 GFP_KERNEL);
5487 if (!phba->slim2p.virt)
5488 goto out_iounmap;
5489
5490 memset(phba->slim2p.virt, 0, SLI2_SLIM_SIZE);
5491 phba->mbox = phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, mbx);
5492 phba->mbox_ext = (phba->slim2p.virt +
5493 offsetof(struct lpfc_sli2_slim, mbx_ext_words));
5494 phba->pcb = (phba->slim2p.virt + offsetof(struct lpfc_sli2_slim, pcb));
5495 phba->IOCBs = (phba->slim2p.virt +
5496 offsetof(struct lpfc_sli2_slim, IOCBs));
5497
5498 phba->hbqslimp.virt = dma_alloc_coherent(&pdev->dev,
5499 lpfc_sli_hbq_size(),
5500 &phba->hbqslimp.phys,
5501 GFP_KERNEL);
5502 if (!phba->hbqslimp.virt)
5503 goto out_free_slim;
5504
5505 hbq_count = lpfc_sli_hbq_count();
5506 ptr = phba->hbqslimp.virt;
5507 for (i = 0; i < hbq_count; ++i) {
5508 phba->hbqs[i].hbq_virt = ptr;
5509 INIT_LIST_HEAD(&phba->hbqs[i].hbq_buffer_list);
5510 ptr += (lpfc_hbq_defs[i]->entry_count *
5511 sizeof(struct lpfc_hbq_entry));
5512 }
5513 phba->hbqs[LPFC_ELS_HBQ].hbq_alloc_buffer = lpfc_els_hbq_alloc;
5514 phba->hbqs[LPFC_ELS_HBQ].hbq_free_buffer = lpfc_els_hbq_free;
5515
5516 memset(phba->hbqslimp.virt, 0, lpfc_sli_hbq_size());
5517
5518 INIT_LIST_HEAD(&phba->rb_pend_list);
5519
5520 phba->MBslimaddr = phba->slim_memmap_p;
5521 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
5522 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
5523 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
5524 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
5525
5526 return 0;
5527
5528 out_free_slim:
5529 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5530 phba->slim2p.virt, phba->slim2p.phys);
5531 out_iounmap:
5532 iounmap(phba->ctrl_regs_memmap_p);
5533 out_iounmap_slim:
5534 iounmap(phba->slim_memmap_p);
5535 out:
5536 return error;
5537 }
5538
5539 /**
5540 * lpfc_sli_pci_mem_unset - Unset SLI3 HBA PCI memory space.
5541 * @phba: pointer to lpfc hba data structure.
5542 *
5543 * This routine is invoked to unset the PCI device memory space for device
5544 * with SLI-3 interface spec.
5545 **/
5546 static void
5547 lpfc_sli_pci_mem_unset(struct lpfc_hba *phba)
5548 {
5549 struct pci_dev *pdev;
5550
5551 /* Obtain PCI device reference */
5552 if (!phba->pcidev)
5553 return;
5554 else
5555 pdev = phba->pcidev;
5556
5557 /* Free coherent DMA memory allocated */
5558 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
5559 phba->hbqslimp.virt, phba->hbqslimp.phys);
5560 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
5561 phba->slim2p.virt, phba->slim2p.phys);
5562
5563 /* I/O memory unmap */
5564 iounmap(phba->ctrl_regs_memmap_p);
5565 iounmap(phba->slim_memmap_p);
5566
5567 return;
5568 }
5569
5570 /**
5571 * lpfc_sli4_post_status_check - Wait for SLI4 POST done and check status
5572 * @phba: pointer to lpfc hba data structure.
5573 *
5574 * This routine is invoked to wait for SLI4 device Power On Self Test (POST)
5575 * done and check status.
5576 *
5577 * Return 0 if successful, otherwise -ENODEV.
5578 **/
5579 int
5580 lpfc_sli4_post_status_check(struct lpfc_hba *phba)
5581 {
5582 struct lpfc_register portsmphr_reg, uerrlo_reg, uerrhi_reg;
5583 struct lpfc_register reg_data;
5584 int i, port_error = 0;
5585 uint32_t if_type;
5586
5587 memset(&portsmphr_reg, 0, sizeof(portsmphr_reg));
5588 memset(&reg_data, 0, sizeof(reg_data));
5589 if (!phba->sli4_hba.PSMPHRregaddr)
5590 return -ENODEV;
5591
5592 /* Wait up to 30 seconds for the SLI Port POST done and ready */
5593 for (i = 0; i < 3000; i++) {
5594 if (lpfc_readl(phba->sli4_hba.PSMPHRregaddr,
5595 &portsmphr_reg.word0) ||
5596 (bf_get(lpfc_port_smphr_perr, &portsmphr_reg))) {
5597 /* Port has a fatal POST error, break out */
5598 port_error = -ENODEV;
5599 break;
5600 }
5601 if (LPFC_POST_STAGE_PORT_READY ==
5602 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg))
5603 break;
5604 msleep(10);
5605 }
5606
5607 /*
5608 * If there was a port error during POST, then don't proceed with
5609 * other register reads as the data may not be valid. Just exit.
5610 */
5611 if (port_error) {
5612 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5613 "1408 Port Failed POST - portsmphr=0x%x, "
5614 "perr=x%x, sfi=x%x, nip=x%x, ipc=x%x, scr1=x%x, "
5615 "scr2=x%x, hscratch=x%x, pstatus=x%x\n",
5616 portsmphr_reg.word0,
5617 bf_get(lpfc_port_smphr_perr, &portsmphr_reg),
5618 bf_get(lpfc_port_smphr_sfi, &portsmphr_reg),
5619 bf_get(lpfc_port_smphr_nip, &portsmphr_reg),
5620 bf_get(lpfc_port_smphr_ipc, &portsmphr_reg),
5621 bf_get(lpfc_port_smphr_scr1, &portsmphr_reg),
5622 bf_get(lpfc_port_smphr_scr2, &portsmphr_reg),
5623 bf_get(lpfc_port_smphr_host_scratch, &portsmphr_reg),
5624 bf_get(lpfc_port_smphr_port_status, &portsmphr_reg));
5625 } else {
5626 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5627 "2534 Device Info: SLIFamily=0x%x, "
5628 "SLIRev=0x%x, IFType=0x%x, SLIHint_1=0x%x, "
5629 "SLIHint_2=0x%x, FT=0x%x\n",
5630 bf_get(lpfc_sli_intf_sli_family,
5631 &phba->sli4_hba.sli_intf),
5632 bf_get(lpfc_sli_intf_slirev,
5633 &phba->sli4_hba.sli_intf),
5634 bf_get(lpfc_sli_intf_if_type,
5635 &phba->sli4_hba.sli_intf),
5636 bf_get(lpfc_sli_intf_sli_hint1,
5637 &phba->sli4_hba.sli_intf),
5638 bf_get(lpfc_sli_intf_sli_hint2,
5639 &phba->sli4_hba.sli_intf),
5640 bf_get(lpfc_sli_intf_func_type,
5641 &phba->sli4_hba.sli_intf));
5642 /*
5643 * Check for other Port errors during the initialization
5644 * process. Fail the load if the port did not come up
5645 * correctly.
5646 */
5647 if_type = bf_get(lpfc_sli_intf_if_type,
5648 &phba->sli4_hba.sli_intf);
5649 switch (if_type) {
5650 case LPFC_SLI_INTF_IF_TYPE_0:
5651 phba->sli4_hba.ue_mask_lo =
5652 readl(phba->sli4_hba.u.if_type0.UEMASKLOregaddr);
5653 phba->sli4_hba.ue_mask_hi =
5654 readl(phba->sli4_hba.u.if_type0.UEMASKHIregaddr);
5655 uerrlo_reg.word0 =
5656 readl(phba->sli4_hba.u.if_type0.UERRLOregaddr);
5657 uerrhi_reg.word0 =
5658 readl(phba->sli4_hba.u.if_type0.UERRHIregaddr);
5659 if ((~phba->sli4_hba.ue_mask_lo & uerrlo_reg.word0) ||
5660 (~phba->sli4_hba.ue_mask_hi & uerrhi_reg.word0)) {
5661 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5662 "1422 Unrecoverable Error "
5663 "Detected during POST "
5664 "uerr_lo_reg=0x%x, "
5665 "uerr_hi_reg=0x%x, "
5666 "ue_mask_lo_reg=0x%x, "
5667 "ue_mask_hi_reg=0x%x\n",
5668 uerrlo_reg.word0,
5669 uerrhi_reg.word0,
5670 phba->sli4_hba.ue_mask_lo,
5671 phba->sli4_hba.ue_mask_hi);
5672 port_error = -ENODEV;
5673 }
5674 break;
5675 case LPFC_SLI_INTF_IF_TYPE_2:
5676 /* Final checks. The port status should be clean. */
5677 if (lpfc_readl(phba->sli4_hba.u.if_type2.STATUSregaddr,
5678 &reg_data.word0) ||
5679 (bf_get(lpfc_sliport_status_err, &reg_data) &&
5680 !bf_get(lpfc_sliport_status_rn, &reg_data))) {
5681 phba->work_status[0] =
5682 readl(phba->sli4_hba.u.if_type2.
5683 ERR1regaddr);
5684 phba->work_status[1] =
5685 readl(phba->sli4_hba.u.if_type2.
5686 ERR2regaddr);
5687 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5688 "2888 Port Error Detected "
5689 "during POST: "
5690 "port status reg 0x%x, "
5691 "port_smphr reg 0x%x, "
5692 "error 1=0x%x, error 2=0x%x\n",
5693 reg_data.word0,
5694 portsmphr_reg.word0,
5695 phba->work_status[0],
5696 phba->work_status[1]);
5697 port_error = -ENODEV;
5698 }
5699 break;
5700 case LPFC_SLI_INTF_IF_TYPE_1:
5701 default:
5702 break;
5703 }
5704 }
5705 return port_error;
5706 }
5707
5708 /**
5709 * lpfc_sli4_bar0_register_memmap - Set up SLI4 BAR0 register memory map.
5710 * @phba: pointer to lpfc hba data structure.
5711 * @if_type: The SLI4 interface type getting configured.
5712 *
5713 * This routine is invoked to set up SLI4 BAR0 PCI config space register
5714 * memory map.
5715 **/
5716 static void
5717 lpfc_sli4_bar0_register_memmap(struct lpfc_hba *phba, uint32_t if_type)
5718 {
5719 switch (if_type) {
5720 case LPFC_SLI_INTF_IF_TYPE_0:
5721 phba->sli4_hba.u.if_type0.UERRLOregaddr =
5722 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_LO;
5723 phba->sli4_hba.u.if_type0.UERRHIregaddr =
5724 phba->sli4_hba.conf_regs_memmap_p + LPFC_UERR_STATUS_HI;
5725 phba->sli4_hba.u.if_type0.UEMASKLOregaddr =
5726 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_LO;
5727 phba->sli4_hba.u.if_type0.UEMASKHIregaddr =
5728 phba->sli4_hba.conf_regs_memmap_p + LPFC_UE_MASK_HI;
5729 phba->sli4_hba.SLIINTFregaddr =
5730 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5731 break;
5732 case LPFC_SLI_INTF_IF_TYPE_2:
5733 phba->sli4_hba.u.if_type2.ERR1regaddr =
5734 phba->sli4_hba.conf_regs_memmap_p +
5735 LPFC_CTL_PORT_ER1_OFFSET;
5736 phba->sli4_hba.u.if_type2.ERR2regaddr =
5737 phba->sli4_hba.conf_regs_memmap_p +
5738 LPFC_CTL_PORT_ER2_OFFSET;
5739 phba->sli4_hba.u.if_type2.CTRLregaddr =
5740 phba->sli4_hba.conf_regs_memmap_p +
5741 LPFC_CTL_PORT_CTL_OFFSET;
5742 phba->sli4_hba.u.if_type2.STATUSregaddr =
5743 phba->sli4_hba.conf_regs_memmap_p +
5744 LPFC_CTL_PORT_STA_OFFSET;
5745 phba->sli4_hba.SLIINTFregaddr =
5746 phba->sli4_hba.conf_regs_memmap_p + LPFC_SLI_INTF;
5747 phba->sli4_hba.PSMPHRregaddr =
5748 phba->sli4_hba.conf_regs_memmap_p +
5749 LPFC_CTL_PORT_SEM_OFFSET;
5750 phba->sli4_hba.RQDBregaddr =
5751 phba->sli4_hba.conf_regs_memmap_p + LPFC_RQ_DOORBELL;
5752 phba->sli4_hba.WQDBregaddr =
5753 phba->sli4_hba.conf_regs_memmap_p + LPFC_WQ_DOORBELL;
5754 phba->sli4_hba.EQCQDBregaddr =
5755 phba->sli4_hba.conf_regs_memmap_p + LPFC_EQCQ_DOORBELL;
5756 phba->sli4_hba.MQDBregaddr =
5757 phba->sli4_hba.conf_regs_memmap_p + LPFC_MQ_DOORBELL;
5758 phba->sli4_hba.BMBXregaddr =
5759 phba->sli4_hba.conf_regs_memmap_p + LPFC_BMBX;
5760 break;
5761 case LPFC_SLI_INTF_IF_TYPE_1:
5762 default:
5763 dev_printk(KERN_ERR, &phba->pcidev->dev,
5764 "FATAL - unsupported SLI4 interface type - %d\n",
5765 if_type);
5766 break;
5767 }
5768 }
5769
5770 /**
5771 * lpfc_sli4_bar1_register_memmap - Set up SLI4 BAR1 register memory map.
5772 * @phba: pointer to lpfc hba data structure.
5773 *
5774 * This routine is invoked to set up SLI4 BAR1 control status register (CSR)
5775 * memory map.
5776 **/
5777 static void
5778 lpfc_sli4_bar1_register_memmap(struct lpfc_hba *phba)
5779 {
5780 phba->sli4_hba.PSMPHRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5781 LPFC_SLIPORT_IF0_SMPHR;
5782 phba->sli4_hba.ISRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5783 LPFC_HST_ISR0;
5784 phba->sli4_hba.IMRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5785 LPFC_HST_IMR0;
5786 phba->sli4_hba.ISCRregaddr = phba->sli4_hba.ctrl_regs_memmap_p +
5787 LPFC_HST_ISCR0;
5788 }
5789
5790 /**
5791 * lpfc_sli4_bar2_register_memmap - Set up SLI4 BAR2 register memory map.
5792 * @phba: pointer to lpfc hba data structure.
5793 * @vf: virtual function number
5794 *
5795 * This routine is invoked to set up SLI4 BAR2 doorbell register memory map
5796 * based on the given viftual function number, @vf.
5797 *
5798 * Return 0 if successful, otherwise -ENODEV.
5799 **/
5800 static int
5801 lpfc_sli4_bar2_register_memmap(struct lpfc_hba *phba, uint32_t vf)
5802 {
5803 if (vf > LPFC_VIR_FUNC_MAX)
5804 return -ENODEV;
5805
5806 phba->sli4_hba.RQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5807 vf * LPFC_VFR_PAGE_SIZE + LPFC_RQ_DOORBELL);
5808 phba->sli4_hba.WQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5809 vf * LPFC_VFR_PAGE_SIZE + LPFC_WQ_DOORBELL);
5810 phba->sli4_hba.EQCQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5811 vf * LPFC_VFR_PAGE_SIZE + LPFC_EQCQ_DOORBELL);
5812 phba->sli4_hba.MQDBregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5813 vf * LPFC_VFR_PAGE_SIZE + LPFC_MQ_DOORBELL);
5814 phba->sli4_hba.BMBXregaddr = (phba->sli4_hba.drbl_regs_memmap_p +
5815 vf * LPFC_VFR_PAGE_SIZE + LPFC_BMBX);
5816 return 0;
5817 }
5818
5819 /**
5820 * lpfc_create_bootstrap_mbox - Create the bootstrap mailbox
5821 * @phba: pointer to lpfc hba data structure.
5822 *
5823 * This routine is invoked to create the bootstrap mailbox
5824 * region consistent with the SLI-4 interface spec. This
5825 * routine allocates all memory necessary to communicate
5826 * mailbox commands to the port and sets up all alignment
5827 * needs. No locks are expected to be held when calling
5828 * this routine.
5829 *
5830 * Return codes
5831 * 0 - successful
5832 * -ENOMEM - could not allocated memory.
5833 **/
5834 static int
5835 lpfc_create_bootstrap_mbox(struct lpfc_hba *phba)
5836 {
5837 uint32_t bmbx_size;
5838 struct lpfc_dmabuf *dmabuf;
5839 struct dma_address *dma_address;
5840 uint32_t pa_addr;
5841 uint64_t phys_addr;
5842
5843 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
5844 if (!dmabuf)
5845 return -ENOMEM;
5846
5847 /*
5848 * The bootstrap mailbox region is comprised of 2 parts
5849 * plus an alignment restriction of 16 bytes.
5850 */
5851 bmbx_size = sizeof(struct lpfc_bmbx_create) + (LPFC_ALIGN_16_BYTE - 1);
5852 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
5853 bmbx_size,
5854 &dmabuf->phys,
5855 GFP_KERNEL);
5856 if (!dmabuf->virt) {
5857 kfree(dmabuf);
5858 return -ENOMEM;
5859 }
5860 memset(dmabuf->virt, 0, bmbx_size);
5861
5862 /*
5863 * Initialize the bootstrap mailbox pointers now so that the register
5864 * operations are simple later. The mailbox dma address is required
5865 * to be 16-byte aligned. Also align the virtual memory as each
5866 * maibox is copied into the bmbx mailbox region before issuing the
5867 * command to the port.
5868 */
5869 phba->sli4_hba.bmbx.dmabuf = dmabuf;
5870 phba->sli4_hba.bmbx.bmbx_size = bmbx_size;
5871
5872 phba->sli4_hba.bmbx.avirt = PTR_ALIGN(dmabuf->virt,
5873 LPFC_ALIGN_16_BYTE);
5874 phba->sli4_hba.bmbx.aphys = ALIGN(dmabuf->phys,
5875 LPFC_ALIGN_16_BYTE);
5876
5877 /*
5878 * Set the high and low physical addresses now. The SLI4 alignment
5879 * requirement is 16 bytes and the mailbox is posted to the port
5880 * as two 30-bit addresses. The other data is a bit marking whether
5881 * the 30-bit address is the high or low address.
5882 * Upcast bmbx aphys to 64bits so shift instruction compiles
5883 * clean on 32 bit machines.
5884 */
5885 dma_address = &phba->sli4_hba.bmbx.dma_address;
5886 phys_addr = (uint64_t)phba->sli4_hba.bmbx.aphys;
5887 pa_addr = (uint32_t) ((phys_addr >> 34) & 0x3fffffff);
5888 dma_address->addr_hi = (uint32_t) ((pa_addr << 2) |
5889 LPFC_BMBX_BIT1_ADDR_HI);
5890
5891 pa_addr = (uint32_t) ((phba->sli4_hba.bmbx.aphys >> 4) & 0x3fffffff);
5892 dma_address->addr_lo = (uint32_t) ((pa_addr << 2) |
5893 LPFC_BMBX_BIT1_ADDR_LO);
5894 return 0;
5895 }
5896
5897 /**
5898 * lpfc_destroy_bootstrap_mbox - Destroy all bootstrap mailbox resources
5899 * @phba: pointer to lpfc hba data structure.
5900 *
5901 * This routine is invoked to teardown the bootstrap mailbox
5902 * region and release all host resources. This routine requires
5903 * the caller to ensure all mailbox commands recovered, no
5904 * additional mailbox comands are sent, and interrupts are disabled
5905 * before calling this routine.
5906 *
5907 **/
5908 static void
5909 lpfc_destroy_bootstrap_mbox(struct lpfc_hba *phba)
5910 {
5911 dma_free_coherent(&phba->pcidev->dev,
5912 phba->sli4_hba.bmbx.bmbx_size,
5913 phba->sli4_hba.bmbx.dmabuf->virt,
5914 phba->sli4_hba.bmbx.dmabuf->phys);
5915
5916 kfree(phba->sli4_hba.bmbx.dmabuf);
5917 memset(&phba->sli4_hba.bmbx, 0, sizeof(struct lpfc_bmbx));
5918 }
5919
5920 /**
5921 * lpfc_sli4_read_config - Get the config parameters.
5922 * @phba: pointer to lpfc hba data structure.
5923 *
5924 * This routine is invoked to read the configuration parameters from the HBA.
5925 * The configuration parameters are used to set the base and maximum values
5926 * for RPI's XRI's VPI's VFI's and FCFIs. These values also affect the resource
5927 * allocation for the port.
5928 *
5929 * Return codes
5930 * 0 - successful
5931 * -ENOMEM - No available memory
5932 * -EIO - The mailbox failed to complete successfully.
5933 **/
5934 static int
5935 lpfc_sli4_read_config(struct lpfc_hba *phba)
5936 {
5937 LPFC_MBOXQ_t *pmb;
5938 struct lpfc_mbx_read_config *rd_config;
5939 union lpfc_sli4_cfg_shdr *shdr;
5940 uint32_t shdr_status, shdr_add_status;
5941 struct lpfc_mbx_get_func_cfg *get_func_cfg;
5942 struct lpfc_rsrc_desc_fcfcoe *desc;
5943 uint32_t desc_count;
5944 int length, i, rc = 0;
5945
5946 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
5947 if (!pmb) {
5948 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5949 "2011 Unable to allocate memory for issuing "
5950 "SLI_CONFIG_SPECIAL mailbox command\n");
5951 return -ENOMEM;
5952 }
5953
5954 lpfc_read_config(phba, pmb);
5955
5956 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
5957 if (rc != MBX_SUCCESS) {
5958 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
5959 "2012 Mailbox failed , mbxCmd x%x "
5960 "READ_CONFIG, mbxStatus x%x\n",
5961 bf_get(lpfc_mqe_command, &pmb->u.mqe),
5962 bf_get(lpfc_mqe_status, &pmb->u.mqe));
5963 rc = -EIO;
5964 } else {
5965 rd_config = &pmb->u.mqe.un.rd_config;
5966 phba->sli4_hba.extents_in_use =
5967 bf_get(lpfc_mbx_rd_conf_extnts_inuse, rd_config);
5968 phba->sli4_hba.max_cfg_param.max_xri =
5969 bf_get(lpfc_mbx_rd_conf_xri_count, rd_config);
5970 phba->sli4_hba.max_cfg_param.xri_base =
5971 bf_get(lpfc_mbx_rd_conf_xri_base, rd_config);
5972 phba->sli4_hba.max_cfg_param.max_vpi =
5973 bf_get(lpfc_mbx_rd_conf_vpi_count, rd_config);
5974 phba->sli4_hba.max_cfg_param.vpi_base =
5975 bf_get(lpfc_mbx_rd_conf_vpi_base, rd_config);
5976 phba->sli4_hba.max_cfg_param.max_rpi =
5977 bf_get(lpfc_mbx_rd_conf_rpi_count, rd_config);
5978 phba->sli4_hba.max_cfg_param.rpi_base =
5979 bf_get(lpfc_mbx_rd_conf_rpi_base, rd_config);
5980 phba->sli4_hba.max_cfg_param.max_vfi =
5981 bf_get(lpfc_mbx_rd_conf_vfi_count, rd_config);
5982 phba->sli4_hba.max_cfg_param.vfi_base =
5983 bf_get(lpfc_mbx_rd_conf_vfi_base, rd_config);
5984 phba->sli4_hba.max_cfg_param.max_fcfi =
5985 bf_get(lpfc_mbx_rd_conf_fcfi_count, rd_config);
5986 phba->sli4_hba.max_cfg_param.max_eq =
5987 bf_get(lpfc_mbx_rd_conf_eq_count, rd_config);
5988 phba->sli4_hba.max_cfg_param.max_rq =
5989 bf_get(lpfc_mbx_rd_conf_rq_count, rd_config);
5990 phba->sli4_hba.max_cfg_param.max_wq =
5991 bf_get(lpfc_mbx_rd_conf_wq_count, rd_config);
5992 phba->sli4_hba.max_cfg_param.max_cq =
5993 bf_get(lpfc_mbx_rd_conf_cq_count, rd_config);
5994 phba->lmt = bf_get(lpfc_mbx_rd_conf_lmt, rd_config);
5995 phba->sli4_hba.next_xri = phba->sli4_hba.max_cfg_param.xri_base;
5996 phba->vpi_base = phba->sli4_hba.max_cfg_param.vpi_base;
5997 phba->vfi_base = phba->sli4_hba.max_cfg_param.vfi_base;
5998 phba->sli4_hba.next_rpi = phba->sli4_hba.max_cfg_param.rpi_base;
5999 phba->max_vpi = (phba->sli4_hba.max_cfg_param.max_vpi > 0) ?
6000 (phba->sli4_hba.max_cfg_param.max_vpi - 1) : 0;
6001 phba->max_vports = phba->max_vpi;
6002 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6003 "2003 cfg params Extents? %d "
6004 "XRI(B:%d M:%d), "
6005 "VPI(B:%d M:%d) "
6006 "VFI(B:%d M:%d) "
6007 "RPI(B:%d M:%d) "
6008 "FCFI(Count:%d)\n",
6009 phba->sli4_hba.extents_in_use,
6010 phba->sli4_hba.max_cfg_param.xri_base,
6011 phba->sli4_hba.max_cfg_param.max_xri,
6012 phba->sli4_hba.max_cfg_param.vpi_base,
6013 phba->sli4_hba.max_cfg_param.max_vpi,
6014 phba->sli4_hba.max_cfg_param.vfi_base,
6015 phba->sli4_hba.max_cfg_param.max_vfi,
6016 phba->sli4_hba.max_cfg_param.rpi_base,
6017 phba->sli4_hba.max_cfg_param.max_rpi,
6018 phba->sli4_hba.max_cfg_param.max_fcfi);
6019 }
6020
6021 if (rc)
6022 goto read_cfg_out;
6023
6024 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
6025 if (phba->cfg_hba_queue_depth >
6026 (phba->sli4_hba.max_cfg_param.max_xri -
6027 lpfc_sli4_get_els_iocb_cnt(phba)))
6028 phba->cfg_hba_queue_depth =
6029 phba->sli4_hba.max_cfg_param.max_xri -
6030 lpfc_sli4_get_els_iocb_cnt(phba);
6031
6032 if (bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf) !=
6033 LPFC_SLI_INTF_IF_TYPE_2)
6034 goto read_cfg_out;
6035
6036 /* get the pf# and vf# for SLI4 if_type 2 port */
6037 length = (sizeof(struct lpfc_mbx_get_func_cfg) -
6038 sizeof(struct lpfc_sli4_cfg_mhdr));
6039 lpfc_sli4_config(phba, pmb, LPFC_MBOX_SUBSYSTEM_COMMON,
6040 LPFC_MBOX_OPCODE_GET_FUNCTION_CONFIG,
6041 length, LPFC_SLI4_MBX_EMBED);
6042
6043 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
6044 shdr = (union lpfc_sli4_cfg_shdr *)
6045 &pmb->u.mqe.un.sli4_config.header.cfg_shdr;
6046 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
6047 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
6048 if (rc || shdr_status || shdr_add_status) {
6049 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6050 "3026 Mailbox failed , mbxCmd x%x "
6051 "GET_FUNCTION_CONFIG, mbxStatus x%x\n",
6052 bf_get(lpfc_mqe_command, &pmb->u.mqe),
6053 bf_get(lpfc_mqe_status, &pmb->u.mqe));
6054 rc = -EIO;
6055 goto read_cfg_out;
6056 }
6057
6058 /* search for fc_fcoe resrouce descriptor */
6059 get_func_cfg = &pmb->u.mqe.un.get_func_cfg;
6060 desc_count = get_func_cfg->func_cfg.rsrc_desc_count;
6061
6062 for (i = 0; i < LPFC_RSRC_DESC_MAX_NUM; i++) {
6063 desc = (struct lpfc_rsrc_desc_fcfcoe *)
6064 &get_func_cfg->func_cfg.desc[i];
6065 if (LPFC_RSRC_DESC_TYPE_FCFCOE ==
6066 bf_get(lpfc_rsrc_desc_pcie_type, desc)) {
6067 phba->sli4_hba.iov.pf_number =
6068 bf_get(lpfc_rsrc_desc_fcfcoe_pfnum, desc);
6069 phba->sli4_hba.iov.vf_number =
6070 bf_get(lpfc_rsrc_desc_fcfcoe_vfnum, desc);
6071 break;
6072 }
6073 }
6074
6075 if (i < LPFC_RSRC_DESC_MAX_NUM)
6076 lpfc_printf_log(phba, KERN_INFO, LOG_SLI,
6077 "3027 GET_FUNCTION_CONFIG: pf_number:%d, "
6078 "vf_number:%d\n", phba->sli4_hba.iov.pf_number,
6079 phba->sli4_hba.iov.vf_number);
6080 else {
6081 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
6082 "3028 GET_FUNCTION_CONFIG: failed to find "
6083 "Resrouce Descriptor:x%x\n",
6084 LPFC_RSRC_DESC_TYPE_FCFCOE);
6085 rc = -EIO;
6086 }
6087
6088 read_cfg_out:
6089 mempool_free(pmb, phba->mbox_mem_pool);
6090 return rc;
6091 }
6092
6093 /**
6094 * lpfc_setup_endian_order - Write endian order to an SLI4 if_type 0 port.
6095 * @phba: pointer to lpfc hba data structure.
6096 *
6097 * This routine is invoked to setup the port-side endian order when
6098 * the port if_type is 0. This routine has no function for other
6099 * if_types.
6100 *
6101 * Return codes
6102 * 0 - successful
6103 * -ENOMEM - No available memory
6104 * -EIO - The mailbox failed to complete successfully.
6105 **/
6106 static int
6107 lpfc_setup_endian_order(struct lpfc_hba *phba)
6108 {
6109 LPFC_MBOXQ_t *mboxq;
6110 uint32_t if_type, rc = 0;
6111 uint32_t endian_mb_data[2] = {HOST_ENDIAN_LOW_WORD0,
6112 HOST_ENDIAN_HIGH_WORD1};
6113
6114 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
6115 switch (if_type) {
6116 case LPFC_SLI_INTF_IF_TYPE_0:
6117 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
6118 GFP_KERNEL);
6119 if (!mboxq) {
6120 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6121 "0492 Unable to allocate memory for "
6122 "issuing SLI_CONFIG_SPECIAL mailbox "
6123 "command\n");
6124 return -ENOMEM;
6125 }
6126
6127 /*
6128 * The SLI4_CONFIG_SPECIAL mailbox command requires the first
6129 * two words to contain special data values and no other data.
6130 */
6131 memset(mboxq, 0, sizeof(LPFC_MBOXQ_t));
6132 memcpy(&mboxq->u.mqe, &endian_mb_data, sizeof(endian_mb_data));
6133 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
6134 if (rc != MBX_SUCCESS) {
6135 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6136 "0493 SLI_CONFIG_SPECIAL mailbox "
6137 "failed with status x%x\n",
6138 rc);
6139 rc = -EIO;
6140 }
6141 mempool_free(mboxq, phba->mbox_mem_pool);
6142 break;
6143 case LPFC_SLI_INTF_IF_TYPE_2:
6144 case LPFC_SLI_INTF_IF_TYPE_1:
6145 default:
6146 break;
6147 }
6148 return rc;
6149 }
6150
6151 /**
6152 * lpfc_sli4_queue_create - Create all the SLI4 queues
6153 * @phba: pointer to lpfc hba data structure.
6154 *
6155 * This routine is invoked to allocate all the SLI4 queues for the FCoE HBA
6156 * operation. For each SLI4 queue type, the parameters such as queue entry
6157 * count (queue depth) shall be taken from the module parameter. For now,
6158 * we just use some constant number as place holder.
6159 *
6160 * Return codes
6161 * 0 - successful
6162 * -ENOMEM - No available memory
6163 * -EIO - The mailbox failed to complete successfully.
6164 **/
6165 static int
6166 lpfc_sli4_queue_create(struct lpfc_hba *phba)
6167 {
6168 struct lpfc_queue *qdesc;
6169 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6170 int cfg_fcp_wq_count;
6171 int cfg_fcp_eq_count;
6172
6173 /*
6174 * Sanity check for confiugred queue parameters against the run-time
6175 * device parameters
6176 */
6177
6178 /* Sanity check on FCP fast-path WQ parameters */
6179 cfg_fcp_wq_count = phba->cfg_fcp_wq_count;
6180 if (cfg_fcp_wq_count >
6181 (phba->sli4_hba.max_cfg_param.max_wq - LPFC_SP_WQN_DEF)) {
6182 cfg_fcp_wq_count = phba->sli4_hba.max_cfg_param.max_wq -
6183 LPFC_SP_WQN_DEF;
6184 if (cfg_fcp_wq_count < LPFC_FP_WQN_MIN) {
6185 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6186 "2581 Not enough WQs (%d) from "
6187 "the pci function for supporting "
6188 "FCP WQs (%d)\n",
6189 phba->sli4_hba.max_cfg_param.max_wq,
6190 phba->cfg_fcp_wq_count);
6191 goto out_error;
6192 }
6193 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6194 "2582 Not enough WQs (%d) from the pci "
6195 "function for supporting the requested "
6196 "FCP WQs (%d), the actual FCP WQs can "
6197 "be supported: %d\n",
6198 phba->sli4_hba.max_cfg_param.max_wq,
6199 phba->cfg_fcp_wq_count, cfg_fcp_wq_count);
6200 }
6201 /* The actual number of FCP work queues adopted */
6202 phba->cfg_fcp_wq_count = cfg_fcp_wq_count;
6203
6204 /* Sanity check on FCP fast-path EQ parameters */
6205 cfg_fcp_eq_count = phba->cfg_fcp_eq_count;
6206 if (cfg_fcp_eq_count >
6207 (phba->sli4_hba.max_cfg_param.max_eq - LPFC_SP_EQN_DEF)) {
6208 cfg_fcp_eq_count = phba->sli4_hba.max_cfg_param.max_eq -
6209 LPFC_SP_EQN_DEF;
6210 if (cfg_fcp_eq_count < LPFC_FP_EQN_MIN) {
6211 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6212 "2574 Not enough EQs (%d) from the "
6213 "pci function for supporting FCP "
6214 "EQs (%d)\n",
6215 phba->sli4_hba.max_cfg_param.max_eq,
6216 phba->cfg_fcp_eq_count);
6217 goto out_error;
6218 }
6219 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6220 "2575 Not enough EQs (%d) from the pci "
6221 "function for supporting the requested "
6222 "FCP EQs (%d), the actual FCP EQs can "
6223 "be supported: %d\n",
6224 phba->sli4_hba.max_cfg_param.max_eq,
6225 phba->cfg_fcp_eq_count, cfg_fcp_eq_count);
6226 }
6227 /* It does not make sense to have more EQs than WQs */
6228 if (cfg_fcp_eq_count > phba->cfg_fcp_wq_count) {
6229 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
6230 "2593 The FCP EQ count(%d) cannot be greater "
6231 "than the FCP WQ count(%d), limiting the "
6232 "FCP EQ count to %d\n", cfg_fcp_eq_count,
6233 phba->cfg_fcp_wq_count,
6234 phba->cfg_fcp_wq_count);
6235 cfg_fcp_eq_count = phba->cfg_fcp_wq_count;
6236 }
6237 /* The actual number of FCP event queues adopted */
6238 phba->cfg_fcp_eq_count = cfg_fcp_eq_count;
6239 /* The overall number of event queues used */
6240 phba->sli4_hba.cfg_eqn = phba->cfg_fcp_eq_count + LPFC_SP_EQN_DEF;
6241
6242 /*
6243 * Create Event Queues (EQs)
6244 */
6245
6246 /* Get EQ depth from module parameter, fake the default for now */
6247 phba->sli4_hba.eq_esize = LPFC_EQE_SIZE_4B;
6248 phba->sli4_hba.eq_ecount = LPFC_EQE_DEF_COUNT;
6249
6250 /* Create slow path event queue */
6251 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6252 phba->sli4_hba.eq_ecount);
6253 if (!qdesc) {
6254 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6255 "0496 Failed allocate slow-path EQ\n");
6256 goto out_error;
6257 }
6258 phba->sli4_hba.sp_eq = qdesc;
6259
6260 /* Create fast-path FCP Event Queue(s) */
6261 phba->sli4_hba.fp_eq = kzalloc((sizeof(struct lpfc_queue *) *
6262 phba->cfg_fcp_eq_count), GFP_KERNEL);
6263 if (!phba->sli4_hba.fp_eq) {
6264 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6265 "2576 Failed allocate memory for fast-path "
6266 "EQ record array\n");
6267 goto out_free_sp_eq;
6268 }
6269 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6270 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.eq_esize,
6271 phba->sli4_hba.eq_ecount);
6272 if (!qdesc) {
6273 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6274 "0497 Failed allocate fast-path EQ\n");
6275 goto out_free_fp_eq;
6276 }
6277 phba->sli4_hba.fp_eq[fcp_eqidx] = qdesc;
6278 }
6279
6280 /*
6281 * Create Complete Queues (CQs)
6282 */
6283
6284 /* Get CQ depth from module parameter, fake the default for now */
6285 phba->sli4_hba.cq_esize = LPFC_CQE_SIZE;
6286 phba->sli4_hba.cq_ecount = LPFC_CQE_DEF_COUNT;
6287
6288 /* Create slow-path Mailbox Command Complete Queue */
6289 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6290 phba->sli4_hba.cq_ecount);
6291 if (!qdesc) {
6292 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6293 "0500 Failed allocate slow-path mailbox CQ\n");
6294 goto out_free_fp_eq;
6295 }
6296 phba->sli4_hba.mbx_cq = qdesc;
6297
6298 /* Create slow-path ELS Complete Queue */
6299 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6300 phba->sli4_hba.cq_ecount);
6301 if (!qdesc) {
6302 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6303 "0501 Failed allocate slow-path ELS CQ\n");
6304 goto out_free_mbx_cq;
6305 }
6306 phba->sli4_hba.els_cq = qdesc;
6307
6308
6309 /* Create fast-path FCP Completion Queue(s), one-to-one with EQs */
6310 phba->sli4_hba.fcp_cq = kzalloc((sizeof(struct lpfc_queue *) *
6311 phba->cfg_fcp_eq_count), GFP_KERNEL);
6312 if (!phba->sli4_hba.fcp_cq) {
6313 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6314 "2577 Failed allocate memory for fast-path "
6315 "CQ record array\n");
6316 goto out_free_els_cq;
6317 }
6318 for (fcp_cqidx = 0; fcp_cqidx < phba->cfg_fcp_eq_count; fcp_cqidx++) {
6319 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.cq_esize,
6320 phba->sli4_hba.cq_ecount);
6321 if (!qdesc) {
6322 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6323 "0499 Failed allocate fast-path FCP "
6324 "CQ (%d)\n", fcp_cqidx);
6325 goto out_free_fcp_cq;
6326 }
6327 phba->sli4_hba.fcp_cq[fcp_cqidx] = qdesc;
6328 }
6329
6330 /* Create Mailbox Command Queue */
6331 phba->sli4_hba.mq_esize = LPFC_MQE_SIZE;
6332 phba->sli4_hba.mq_ecount = LPFC_MQE_DEF_COUNT;
6333
6334 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.mq_esize,
6335 phba->sli4_hba.mq_ecount);
6336 if (!qdesc) {
6337 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6338 "0505 Failed allocate slow-path MQ\n");
6339 goto out_free_fcp_cq;
6340 }
6341 phba->sli4_hba.mbx_wq = qdesc;
6342
6343 /*
6344 * Create all the Work Queues (WQs)
6345 */
6346 phba->sli4_hba.wq_esize = LPFC_WQE_SIZE;
6347 phba->sli4_hba.wq_ecount = LPFC_WQE_DEF_COUNT;
6348
6349 /* Create slow-path ELS Work Queue */
6350 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6351 phba->sli4_hba.wq_ecount);
6352 if (!qdesc) {
6353 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6354 "0504 Failed allocate slow-path ELS WQ\n");
6355 goto out_free_mbx_wq;
6356 }
6357 phba->sli4_hba.els_wq = qdesc;
6358
6359 /* Create fast-path FCP Work Queue(s) */
6360 phba->sli4_hba.fcp_wq = kzalloc((sizeof(struct lpfc_queue *) *
6361 phba->cfg_fcp_wq_count), GFP_KERNEL);
6362 if (!phba->sli4_hba.fcp_wq) {
6363 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6364 "2578 Failed allocate memory for fast-path "
6365 "WQ record array\n");
6366 goto out_free_els_wq;
6367 }
6368 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6369 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.wq_esize,
6370 phba->sli4_hba.wq_ecount);
6371 if (!qdesc) {
6372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6373 "0503 Failed allocate fast-path FCP "
6374 "WQ (%d)\n", fcp_wqidx);
6375 goto out_free_fcp_wq;
6376 }
6377 phba->sli4_hba.fcp_wq[fcp_wqidx] = qdesc;
6378 }
6379
6380 /*
6381 * Create Receive Queue (RQ)
6382 */
6383 phba->sli4_hba.rq_esize = LPFC_RQE_SIZE;
6384 phba->sli4_hba.rq_ecount = LPFC_RQE_DEF_COUNT;
6385
6386 /* Create Receive Queue for header */
6387 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6388 phba->sli4_hba.rq_ecount);
6389 if (!qdesc) {
6390 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6391 "0506 Failed allocate receive HRQ\n");
6392 goto out_free_fcp_wq;
6393 }
6394 phba->sli4_hba.hdr_rq = qdesc;
6395
6396 /* Create Receive Queue for data */
6397 qdesc = lpfc_sli4_queue_alloc(phba, phba->sli4_hba.rq_esize,
6398 phba->sli4_hba.rq_ecount);
6399 if (!qdesc) {
6400 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6401 "0507 Failed allocate receive DRQ\n");
6402 goto out_free_hdr_rq;
6403 }
6404 phba->sli4_hba.dat_rq = qdesc;
6405
6406 return 0;
6407
6408 out_free_hdr_rq:
6409 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6410 phba->sli4_hba.hdr_rq = NULL;
6411 out_free_fcp_wq:
6412 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--) {
6413 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_wqidx]);
6414 phba->sli4_hba.fcp_wq[fcp_wqidx] = NULL;
6415 }
6416 kfree(phba->sli4_hba.fcp_wq);
6417 out_free_els_wq:
6418 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6419 phba->sli4_hba.els_wq = NULL;
6420 out_free_mbx_wq:
6421 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6422 phba->sli4_hba.mbx_wq = NULL;
6423 out_free_fcp_cq:
6424 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--) {
6425 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_cqidx]);
6426 phba->sli4_hba.fcp_cq[fcp_cqidx] = NULL;
6427 }
6428 kfree(phba->sli4_hba.fcp_cq);
6429 out_free_els_cq:
6430 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6431 phba->sli4_hba.els_cq = NULL;
6432 out_free_mbx_cq:
6433 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6434 phba->sli4_hba.mbx_cq = NULL;
6435 out_free_fp_eq:
6436 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--) {
6437 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_eqidx]);
6438 phba->sli4_hba.fp_eq[fcp_eqidx] = NULL;
6439 }
6440 kfree(phba->sli4_hba.fp_eq);
6441 out_free_sp_eq:
6442 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6443 phba->sli4_hba.sp_eq = NULL;
6444 out_error:
6445 return -ENOMEM;
6446 }
6447
6448 /**
6449 * lpfc_sli4_queue_destroy - Destroy all the SLI4 queues
6450 * @phba: pointer to lpfc hba data structure.
6451 *
6452 * This routine is invoked to release all the SLI4 queues with the FCoE HBA
6453 * operation.
6454 *
6455 * Return codes
6456 * 0 - successful
6457 * -ENOMEM - No available memory
6458 * -EIO - The mailbox failed to complete successfully.
6459 **/
6460 static void
6461 lpfc_sli4_queue_destroy(struct lpfc_hba *phba)
6462 {
6463 int fcp_qidx;
6464
6465 /* Release mailbox command work queue */
6466 lpfc_sli4_queue_free(phba->sli4_hba.mbx_wq);
6467 phba->sli4_hba.mbx_wq = NULL;
6468
6469 /* Release ELS work queue */
6470 lpfc_sli4_queue_free(phba->sli4_hba.els_wq);
6471 phba->sli4_hba.els_wq = NULL;
6472
6473 /* Release FCP work queue */
6474 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6475 lpfc_sli4_queue_free(phba->sli4_hba.fcp_wq[fcp_qidx]);
6476 kfree(phba->sli4_hba.fcp_wq);
6477 phba->sli4_hba.fcp_wq = NULL;
6478
6479 /* Release unsolicited receive queue */
6480 lpfc_sli4_queue_free(phba->sli4_hba.hdr_rq);
6481 phba->sli4_hba.hdr_rq = NULL;
6482 lpfc_sli4_queue_free(phba->sli4_hba.dat_rq);
6483 phba->sli4_hba.dat_rq = NULL;
6484
6485 /* Release ELS complete queue */
6486 lpfc_sli4_queue_free(phba->sli4_hba.els_cq);
6487 phba->sli4_hba.els_cq = NULL;
6488
6489 /* Release mailbox command complete queue */
6490 lpfc_sli4_queue_free(phba->sli4_hba.mbx_cq);
6491 phba->sli4_hba.mbx_cq = NULL;
6492
6493 /* Release FCP response complete queue */
6494 fcp_qidx = 0;
6495 do
6496 lpfc_sli4_queue_free(phba->sli4_hba.fcp_cq[fcp_qidx]);
6497 while (++fcp_qidx < phba->cfg_fcp_eq_count);
6498 kfree(phba->sli4_hba.fcp_cq);
6499 phba->sli4_hba.fcp_cq = NULL;
6500
6501 /* Release fast-path event queue */
6502 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6503 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
6504 kfree(phba->sli4_hba.fp_eq);
6505 phba->sli4_hba.fp_eq = NULL;
6506
6507 /* Release slow-path event queue */
6508 lpfc_sli4_queue_free(phba->sli4_hba.sp_eq);
6509 phba->sli4_hba.sp_eq = NULL;
6510
6511 return;
6512 }
6513
6514 /**
6515 * lpfc_sli4_queue_setup - Set up all the SLI4 queues
6516 * @phba: pointer to lpfc hba data structure.
6517 *
6518 * This routine is invoked to set up all the SLI4 queues for the FCoE HBA
6519 * operation.
6520 *
6521 * Return codes
6522 * 0 - successful
6523 * -ENOMEM - No available memory
6524 * -EIO - The mailbox failed to complete successfully.
6525 **/
6526 int
6527 lpfc_sli4_queue_setup(struct lpfc_hba *phba)
6528 {
6529 int rc = -ENOMEM;
6530 int fcp_eqidx, fcp_cqidx, fcp_wqidx;
6531 int fcp_cq_index = 0;
6532
6533 /*
6534 * Set up Event Queues (EQs)
6535 */
6536
6537 /* Set up slow-path event queue */
6538 if (!phba->sli4_hba.sp_eq) {
6539 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6540 "0520 Slow-path EQ not allocated\n");
6541 goto out_error;
6542 }
6543 rc = lpfc_eq_create(phba, phba->sli4_hba.sp_eq,
6544 LPFC_SP_DEF_IMAX);
6545 if (rc) {
6546 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6547 "0521 Failed setup of slow-path EQ: "
6548 "rc = 0x%x\n", rc);
6549 goto out_error;
6550 }
6551 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6552 "2583 Slow-path EQ setup: queue-id=%d\n",
6553 phba->sli4_hba.sp_eq->queue_id);
6554
6555 /* Set up fast-path event queue */
6556 for (fcp_eqidx = 0; fcp_eqidx < phba->cfg_fcp_eq_count; fcp_eqidx++) {
6557 if (!phba->sli4_hba.fp_eq[fcp_eqidx]) {
6558 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6559 "0522 Fast-path EQ (%d) not "
6560 "allocated\n", fcp_eqidx);
6561 goto out_destroy_fp_eq;
6562 }
6563 rc = lpfc_eq_create(phba, phba->sli4_hba.fp_eq[fcp_eqidx],
6564 phba->cfg_fcp_imax);
6565 if (rc) {
6566 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6567 "0523 Failed setup of fast-path EQ "
6568 "(%d), rc = 0x%x\n", fcp_eqidx, rc);
6569 goto out_destroy_fp_eq;
6570 }
6571 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6572 "2584 Fast-path EQ setup: "
6573 "queue[%d]-id=%d\n", fcp_eqidx,
6574 phba->sli4_hba.fp_eq[fcp_eqidx]->queue_id);
6575 }
6576
6577 /*
6578 * Set up Complete Queues (CQs)
6579 */
6580
6581 /* Set up slow-path MBOX Complete Queue as the first CQ */
6582 if (!phba->sli4_hba.mbx_cq) {
6583 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6584 "0528 Mailbox CQ not allocated\n");
6585 goto out_destroy_fp_eq;
6586 }
6587 rc = lpfc_cq_create(phba, phba->sli4_hba.mbx_cq, phba->sli4_hba.sp_eq,
6588 LPFC_MCQ, LPFC_MBOX);
6589 if (rc) {
6590 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6591 "0529 Failed setup of slow-path mailbox CQ: "
6592 "rc = 0x%x\n", rc);
6593 goto out_destroy_fp_eq;
6594 }
6595 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6596 "2585 MBX CQ setup: cq-id=%d, parent eq-id=%d\n",
6597 phba->sli4_hba.mbx_cq->queue_id,
6598 phba->sli4_hba.sp_eq->queue_id);
6599
6600 /* Set up slow-path ELS Complete Queue */
6601 if (!phba->sli4_hba.els_cq) {
6602 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6603 "0530 ELS CQ not allocated\n");
6604 goto out_destroy_mbx_cq;
6605 }
6606 rc = lpfc_cq_create(phba, phba->sli4_hba.els_cq, phba->sli4_hba.sp_eq,
6607 LPFC_WCQ, LPFC_ELS);
6608 if (rc) {
6609 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6610 "0531 Failed setup of slow-path ELS CQ: "
6611 "rc = 0x%x\n", rc);
6612 goto out_destroy_mbx_cq;
6613 }
6614 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6615 "2586 ELS CQ setup: cq-id=%d, parent eq-id=%d\n",
6616 phba->sli4_hba.els_cq->queue_id,
6617 phba->sli4_hba.sp_eq->queue_id);
6618
6619 /* Set up fast-path FCP Response Complete Queue */
6620 fcp_cqidx = 0;
6621 do {
6622 if (!phba->sli4_hba.fcp_cq[fcp_cqidx]) {
6623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6624 "0526 Fast-path FCP CQ (%d) not "
6625 "allocated\n", fcp_cqidx);
6626 goto out_destroy_fcp_cq;
6627 }
6628 if (phba->cfg_fcp_eq_count)
6629 rc = lpfc_cq_create(phba,
6630 phba->sli4_hba.fcp_cq[fcp_cqidx],
6631 phba->sli4_hba.fp_eq[fcp_cqidx],
6632 LPFC_WCQ, LPFC_FCP);
6633 else
6634 rc = lpfc_cq_create(phba,
6635 phba->sli4_hba.fcp_cq[fcp_cqidx],
6636 phba->sli4_hba.sp_eq,
6637 LPFC_WCQ, LPFC_FCP);
6638 if (rc) {
6639 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6640 "0527 Failed setup of fast-path FCP "
6641 "CQ (%d), rc = 0x%x\n", fcp_cqidx, rc);
6642 goto out_destroy_fcp_cq;
6643 }
6644 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6645 "2588 FCP CQ setup: cq[%d]-id=%d, "
6646 "parent %seq[%d]-id=%d\n",
6647 fcp_cqidx,
6648 phba->sli4_hba.fcp_cq[fcp_cqidx]->queue_id,
6649 (phba->cfg_fcp_eq_count) ? "" : "sp_",
6650 fcp_cqidx,
6651 (phba->cfg_fcp_eq_count) ?
6652 phba->sli4_hba.fp_eq[fcp_cqidx]->queue_id :
6653 phba->sli4_hba.sp_eq->queue_id);
6654 } while (++fcp_cqidx < phba->cfg_fcp_eq_count);
6655
6656 /*
6657 * Set up all the Work Queues (WQs)
6658 */
6659
6660 /* Set up Mailbox Command Queue */
6661 if (!phba->sli4_hba.mbx_wq) {
6662 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6663 "0538 Slow-path MQ not allocated\n");
6664 goto out_destroy_fcp_cq;
6665 }
6666 rc = lpfc_mq_create(phba, phba->sli4_hba.mbx_wq,
6667 phba->sli4_hba.mbx_cq, LPFC_MBOX);
6668 if (rc) {
6669 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6670 "0539 Failed setup of slow-path MQ: "
6671 "rc = 0x%x\n", rc);
6672 goto out_destroy_fcp_cq;
6673 }
6674 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6675 "2589 MBX MQ setup: wq-id=%d, parent cq-id=%d\n",
6676 phba->sli4_hba.mbx_wq->queue_id,
6677 phba->sli4_hba.mbx_cq->queue_id);
6678
6679 /* Set up slow-path ELS Work Queue */
6680 if (!phba->sli4_hba.els_wq) {
6681 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6682 "0536 Slow-path ELS WQ not allocated\n");
6683 goto out_destroy_mbx_wq;
6684 }
6685 rc = lpfc_wq_create(phba, phba->sli4_hba.els_wq,
6686 phba->sli4_hba.els_cq, LPFC_ELS);
6687 if (rc) {
6688 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6689 "0537 Failed setup of slow-path ELS WQ: "
6690 "rc = 0x%x\n", rc);
6691 goto out_destroy_mbx_wq;
6692 }
6693 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6694 "2590 ELS WQ setup: wq-id=%d, parent cq-id=%d\n",
6695 phba->sli4_hba.els_wq->queue_id,
6696 phba->sli4_hba.els_cq->queue_id);
6697
6698 /* Set up fast-path FCP Work Queue */
6699 for (fcp_wqidx = 0; fcp_wqidx < phba->cfg_fcp_wq_count; fcp_wqidx++) {
6700 if (!phba->sli4_hba.fcp_wq[fcp_wqidx]) {
6701 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6702 "0534 Fast-path FCP WQ (%d) not "
6703 "allocated\n", fcp_wqidx);
6704 goto out_destroy_fcp_wq;
6705 }
6706 rc = lpfc_wq_create(phba, phba->sli4_hba.fcp_wq[fcp_wqidx],
6707 phba->sli4_hba.fcp_cq[fcp_cq_index],
6708 LPFC_FCP);
6709 if (rc) {
6710 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6711 "0535 Failed setup of fast-path FCP "
6712 "WQ (%d), rc = 0x%x\n", fcp_wqidx, rc);
6713 goto out_destroy_fcp_wq;
6714 }
6715 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6716 "2591 FCP WQ setup: wq[%d]-id=%d, "
6717 "parent cq[%d]-id=%d\n",
6718 fcp_wqidx,
6719 phba->sli4_hba.fcp_wq[fcp_wqidx]->queue_id,
6720 fcp_cq_index,
6721 phba->sli4_hba.fcp_cq[fcp_cq_index]->queue_id);
6722 /* Round robin FCP Work Queue's Completion Queue assignment */
6723 if (phba->cfg_fcp_eq_count)
6724 fcp_cq_index = ((fcp_cq_index + 1) %
6725 phba->cfg_fcp_eq_count);
6726 }
6727
6728 /*
6729 * Create Receive Queue (RQ)
6730 */
6731 if (!phba->sli4_hba.hdr_rq || !phba->sli4_hba.dat_rq) {
6732 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6733 "0540 Receive Queue not allocated\n");
6734 goto out_destroy_fcp_wq;
6735 }
6736
6737 lpfc_rq_adjust_repost(phba, phba->sli4_hba.hdr_rq, LPFC_ELS_HBQ);
6738 lpfc_rq_adjust_repost(phba, phba->sli4_hba.dat_rq, LPFC_ELS_HBQ);
6739
6740 rc = lpfc_rq_create(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq,
6741 phba->sli4_hba.els_cq, LPFC_USOL);
6742 if (rc) {
6743 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
6744 "0541 Failed setup of Receive Queue: "
6745 "rc = 0x%x\n", rc);
6746 goto out_destroy_fcp_wq;
6747 }
6748
6749 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
6750 "2592 USL RQ setup: hdr-rq-id=%d, dat-rq-id=%d "
6751 "parent cq-id=%d\n",
6752 phba->sli4_hba.hdr_rq->queue_id,
6753 phba->sli4_hba.dat_rq->queue_id,
6754 phba->sli4_hba.els_cq->queue_id);
6755 return 0;
6756
6757 out_destroy_fcp_wq:
6758 for (--fcp_wqidx; fcp_wqidx >= 0; fcp_wqidx--)
6759 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_wqidx]);
6760 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6761 out_destroy_mbx_wq:
6762 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6763 out_destroy_fcp_cq:
6764 for (--fcp_cqidx; fcp_cqidx >= 0; fcp_cqidx--)
6765 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_cqidx]);
6766 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6767 out_destroy_mbx_cq:
6768 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6769 out_destroy_fp_eq:
6770 for (--fcp_eqidx; fcp_eqidx >= 0; fcp_eqidx--)
6771 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_eqidx]);
6772 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6773 out_error:
6774 return rc;
6775 }
6776
6777 /**
6778 * lpfc_sli4_queue_unset - Unset all the SLI4 queues
6779 * @phba: pointer to lpfc hba data structure.
6780 *
6781 * This routine is invoked to unset all the SLI4 queues with the FCoE HBA
6782 * operation.
6783 *
6784 * Return codes
6785 * 0 - successful
6786 * -ENOMEM - No available memory
6787 * -EIO - The mailbox failed to complete successfully.
6788 **/
6789 void
6790 lpfc_sli4_queue_unset(struct lpfc_hba *phba)
6791 {
6792 int fcp_qidx;
6793
6794 /* Unset mailbox command work queue */
6795 lpfc_mq_destroy(phba, phba->sli4_hba.mbx_wq);
6796 /* Unset ELS work queue */
6797 lpfc_wq_destroy(phba, phba->sli4_hba.els_wq);
6798 /* Unset unsolicited receive queue */
6799 lpfc_rq_destroy(phba, phba->sli4_hba.hdr_rq, phba->sli4_hba.dat_rq);
6800 /* Unset FCP work queue */
6801 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_wq_count; fcp_qidx++)
6802 lpfc_wq_destroy(phba, phba->sli4_hba.fcp_wq[fcp_qidx]);
6803 /* Unset mailbox command complete queue */
6804 lpfc_cq_destroy(phba, phba->sli4_hba.mbx_cq);
6805 /* Unset ELS complete queue */
6806 lpfc_cq_destroy(phba, phba->sli4_hba.els_cq);
6807 /* Unset FCP response complete queue */
6808 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6809 lpfc_cq_destroy(phba, phba->sli4_hba.fcp_cq[fcp_qidx]);
6810 /* Unset fast-path event queue */
6811 for (fcp_qidx = 0; fcp_qidx < phba->cfg_fcp_eq_count; fcp_qidx++)
6812 lpfc_eq_destroy(phba, phba->sli4_hba.fp_eq[fcp_qidx]);
6813 /* Unset slow-path event queue */
6814 lpfc_eq_destroy(phba, phba->sli4_hba.sp_eq);
6815 }
6816
6817 /**
6818 * lpfc_sli4_cq_event_pool_create - Create completion-queue event free pool
6819 * @phba: pointer to lpfc hba data structure.
6820 *
6821 * This routine is invoked to allocate and set up a pool of completion queue
6822 * events. The body of the completion queue event is a completion queue entry
6823 * CQE. For now, this pool is used for the interrupt service routine to queue
6824 * the following HBA completion queue events for the worker thread to process:
6825 * - Mailbox asynchronous events
6826 * - Receive queue completion unsolicited events
6827 * Later, this can be used for all the slow-path events.
6828 *
6829 * Return codes
6830 * 0 - successful
6831 * -ENOMEM - No available memory
6832 **/
6833 static int
6834 lpfc_sli4_cq_event_pool_create(struct lpfc_hba *phba)
6835 {
6836 struct lpfc_cq_event *cq_event;
6837 int i;
6838
6839 for (i = 0; i < (4 * phba->sli4_hba.cq_ecount); i++) {
6840 cq_event = kmalloc(sizeof(struct lpfc_cq_event), GFP_KERNEL);
6841 if (!cq_event)
6842 goto out_pool_create_fail;
6843 list_add_tail(&cq_event->list,
6844 &phba->sli4_hba.sp_cqe_event_pool);
6845 }
6846 return 0;
6847
6848 out_pool_create_fail:
6849 lpfc_sli4_cq_event_pool_destroy(phba);
6850 return -ENOMEM;
6851 }
6852
6853 /**
6854 * lpfc_sli4_cq_event_pool_destroy - Free completion-queue event free pool
6855 * @phba: pointer to lpfc hba data structure.
6856 *
6857 * This routine is invoked to free the pool of completion queue events at
6858 * driver unload time. Note that, it is the responsibility of the driver
6859 * cleanup routine to free all the outstanding completion-queue events
6860 * allocated from this pool back into the pool before invoking this routine
6861 * to destroy the pool.
6862 **/
6863 static void
6864 lpfc_sli4_cq_event_pool_destroy(struct lpfc_hba *phba)
6865 {
6866 struct lpfc_cq_event *cq_event, *next_cq_event;
6867
6868 list_for_each_entry_safe(cq_event, next_cq_event,
6869 &phba->sli4_hba.sp_cqe_event_pool, list) {
6870 list_del(&cq_event->list);
6871 kfree(cq_event);
6872 }
6873 }
6874
6875 /**
6876 * __lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6877 * @phba: pointer to lpfc hba data structure.
6878 *
6879 * This routine is the lock free version of the API invoked to allocate a
6880 * completion-queue event from the free pool.
6881 *
6882 * Return: Pointer to the newly allocated completion-queue event if successful
6883 * NULL otherwise.
6884 **/
6885 struct lpfc_cq_event *
6886 __lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6887 {
6888 struct lpfc_cq_event *cq_event = NULL;
6889
6890 list_remove_head(&phba->sli4_hba.sp_cqe_event_pool, cq_event,
6891 struct lpfc_cq_event, list);
6892 return cq_event;
6893 }
6894
6895 /**
6896 * lpfc_sli4_cq_event_alloc - Allocate a completion-queue event from free pool
6897 * @phba: pointer to lpfc hba data structure.
6898 *
6899 * This routine is the lock version of the API invoked to allocate a
6900 * completion-queue event from the free pool.
6901 *
6902 * Return: Pointer to the newly allocated completion-queue event if successful
6903 * NULL otherwise.
6904 **/
6905 struct lpfc_cq_event *
6906 lpfc_sli4_cq_event_alloc(struct lpfc_hba *phba)
6907 {
6908 struct lpfc_cq_event *cq_event;
6909 unsigned long iflags;
6910
6911 spin_lock_irqsave(&phba->hbalock, iflags);
6912 cq_event = __lpfc_sli4_cq_event_alloc(phba);
6913 spin_unlock_irqrestore(&phba->hbalock, iflags);
6914 return cq_event;
6915 }
6916
6917 /**
6918 * __lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6919 * @phba: pointer to lpfc hba data structure.
6920 * @cq_event: pointer to the completion queue event to be freed.
6921 *
6922 * This routine is the lock free version of the API invoked to release a
6923 * completion-queue event back into the free pool.
6924 **/
6925 void
6926 __lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6927 struct lpfc_cq_event *cq_event)
6928 {
6929 list_add_tail(&cq_event->list, &phba->sli4_hba.sp_cqe_event_pool);
6930 }
6931
6932 /**
6933 * lpfc_sli4_cq_event_release - Release a completion-queue event to free pool
6934 * @phba: pointer to lpfc hba data structure.
6935 * @cq_event: pointer to the completion queue event to be freed.
6936 *
6937 * This routine is the lock version of the API invoked to release a
6938 * completion-queue event back into the free pool.
6939 **/
6940 void
6941 lpfc_sli4_cq_event_release(struct lpfc_hba *phba,
6942 struct lpfc_cq_event *cq_event)
6943 {
6944 unsigned long iflags;
6945 spin_lock_irqsave(&phba->hbalock, iflags);
6946 __lpfc_sli4_cq_event_release(phba, cq_event);
6947 spin_unlock_irqrestore(&phba->hbalock, iflags);
6948 }
6949
6950 /**
6951 * lpfc_sli4_cq_event_release_all - Release all cq events to the free pool
6952 * @phba: pointer to lpfc hba data structure.
6953 *
6954 * This routine is to free all the pending completion-queue events to the
6955 * back into the free pool for device reset.
6956 **/
6957 static void
6958 lpfc_sli4_cq_event_release_all(struct lpfc_hba *phba)
6959 {
6960 LIST_HEAD(cqelist);
6961 struct lpfc_cq_event *cqe;
6962 unsigned long iflags;
6963
6964 /* Retrieve all the pending WCQEs from pending WCQE lists */
6965 spin_lock_irqsave(&phba->hbalock, iflags);
6966 /* Pending FCP XRI abort events */
6967 list_splice_init(&phba->sli4_hba.sp_fcp_xri_aborted_work_queue,
6968 &cqelist);
6969 /* Pending ELS XRI abort events */
6970 list_splice_init(&phba->sli4_hba.sp_els_xri_aborted_work_queue,
6971 &cqelist);
6972 /* Pending asynnc events */
6973 list_splice_init(&phba->sli4_hba.sp_asynce_work_queue,
6974 &cqelist);
6975 spin_unlock_irqrestore(&phba->hbalock, iflags);
6976
6977 while (!list_empty(&cqelist)) {
6978 list_remove_head(&cqelist, cqe, struct lpfc_cq_event, list);
6979 lpfc_sli4_cq_event_release(phba, cqe);
6980 }
6981 }
6982
6983 /**
6984 * lpfc_pci_function_reset - Reset pci function.
6985 * @phba: pointer to lpfc hba data structure.
6986 *
6987 * This routine is invoked to request a PCI function reset. It will destroys
6988 * all resources assigned to the PCI function which originates this request.
6989 *
6990 * Return codes
6991 * 0 - successful
6992 * -ENOMEM - No available memory
6993 * -EIO - The mailbox failed to complete successfully.
6994 **/
6995 int
6996 lpfc_pci_function_reset(struct lpfc_hba *phba)
6997 {
6998 LPFC_MBOXQ_t *mboxq;
6999 uint32_t rc = 0, if_type;
7000 uint32_t shdr_status, shdr_add_status;
7001 uint32_t rdy_chk, num_resets = 0, reset_again = 0;
7002 union lpfc_sli4_cfg_shdr *shdr;
7003 struct lpfc_register reg_data;
7004
7005 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7006 switch (if_type) {
7007 case LPFC_SLI_INTF_IF_TYPE_0:
7008 mboxq = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool,
7009 GFP_KERNEL);
7010 if (!mboxq) {
7011 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7012 "0494 Unable to allocate memory for "
7013 "issuing SLI_FUNCTION_RESET mailbox "
7014 "command\n");
7015 return -ENOMEM;
7016 }
7017
7018 /* Setup PCI function reset mailbox-ioctl command */
7019 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7020 LPFC_MBOX_OPCODE_FUNCTION_RESET, 0,
7021 LPFC_SLI4_MBX_EMBED);
7022 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7023 shdr = (union lpfc_sli4_cfg_shdr *)
7024 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7025 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7026 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7027 &shdr->response);
7028 if (rc != MBX_TIMEOUT)
7029 mempool_free(mboxq, phba->mbox_mem_pool);
7030 if (shdr_status || shdr_add_status || rc) {
7031 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7032 "0495 SLI_FUNCTION_RESET mailbox "
7033 "failed with status x%x add_status x%x,"
7034 " mbx status x%x\n",
7035 shdr_status, shdr_add_status, rc);
7036 rc = -ENXIO;
7037 }
7038 break;
7039 case LPFC_SLI_INTF_IF_TYPE_2:
7040 for (num_resets = 0;
7041 num_resets < MAX_IF_TYPE_2_RESETS;
7042 num_resets++) {
7043 reg_data.word0 = 0;
7044 bf_set(lpfc_sliport_ctrl_end, &reg_data,
7045 LPFC_SLIPORT_LITTLE_ENDIAN);
7046 bf_set(lpfc_sliport_ctrl_ip, &reg_data,
7047 LPFC_SLIPORT_INIT_PORT);
7048 writel(reg_data.word0, phba->sli4_hba.u.if_type2.
7049 CTRLregaddr);
7050
7051 /*
7052 * Poll the Port Status Register and wait for RDY for
7053 * up to 10 seconds. If the port doesn't respond, treat
7054 * it as an error. If the port responds with RN, start
7055 * the loop again.
7056 */
7057 for (rdy_chk = 0; rdy_chk < 1000; rdy_chk++) {
7058 msleep(10);
7059 if (lpfc_readl(phba->sli4_hba.u.if_type2.
7060 STATUSregaddr, &reg_data.word0)) {
7061 rc = -ENODEV;
7062 goto out;
7063 }
7064 if (bf_get(lpfc_sliport_status_rdy, &reg_data))
7065 break;
7066 if (bf_get(lpfc_sliport_status_rn, &reg_data)) {
7067 reset_again++;
7068 break;
7069 }
7070 }
7071
7072 /*
7073 * If the port responds to the init request with
7074 * reset needed, delay for a bit and restart the loop.
7075 */
7076 if (reset_again) {
7077 msleep(10);
7078 reset_again = 0;
7079 continue;
7080 }
7081
7082 /* Detect any port errors. */
7083 if ((bf_get(lpfc_sliport_status_err, &reg_data)) ||
7084 (rdy_chk >= 1000)) {
7085 phba->work_status[0] = readl(
7086 phba->sli4_hba.u.if_type2.ERR1regaddr);
7087 phba->work_status[1] = readl(
7088 phba->sli4_hba.u.if_type2.ERR2regaddr);
7089 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7090 "2890 Port Error Detected "
7091 "during Port Reset: "
7092 "port status reg 0x%x, "
7093 "error 1=0x%x, error 2=0x%x\n",
7094 reg_data.word0,
7095 phba->work_status[0],
7096 phba->work_status[1]);
7097 rc = -ENODEV;
7098 }
7099
7100 /*
7101 * Terminate the outer loop provided the Port indicated
7102 * ready within 10 seconds.
7103 */
7104 if (rdy_chk < 1000)
7105 break;
7106 }
7107 /* delay driver action following IF_TYPE_2 function reset */
7108 msleep(100);
7109 break;
7110 case LPFC_SLI_INTF_IF_TYPE_1:
7111 default:
7112 break;
7113 }
7114
7115 out:
7116 /* Catch the not-ready port failure after a port reset. */
7117 if (num_resets >= MAX_IF_TYPE_2_RESETS)
7118 rc = -ENODEV;
7119
7120 return rc;
7121 }
7122
7123 /**
7124 * lpfc_sli4_send_nop_mbox_cmds - Send sli-4 nop mailbox commands
7125 * @phba: pointer to lpfc hba data structure.
7126 * @cnt: number of nop mailbox commands to send.
7127 *
7128 * This routine is invoked to send a number @cnt of NOP mailbox command and
7129 * wait for each command to complete.
7130 *
7131 * Return: the number of NOP mailbox command completed.
7132 **/
7133 static int
7134 lpfc_sli4_send_nop_mbox_cmds(struct lpfc_hba *phba, uint32_t cnt)
7135 {
7136 LPFC_MBOXQ_t *mboxq;
7137 int length, cmdsent;
7138 uint32_t mbox_tmo;
7139 uint32_t rc = 0;
7140 uint32_t shdr_status, shdr_add_status;
7141 union lpfc_sli4_cfg_shdr *shdr;
7142
7143 if (cnt == 0) {
7144 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7145 "2518 Requested to send 0 NOP mailbox cmd\n");
7146 return cnt;
7147 }
7148
7149 mboxq = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7150 if (!mboxq) {
7151 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7152 "2519 Unable to allocate memory for issuing "
7153 "NOP mailbox command\n");
7154 return 0;
7155 }
7156
7157 /* Set up NOP SLI4_CONFIG mailbox-ioctl command */
7158 length = (sizeof(struct lpfc_mbx_nop) -
7159 sizeof(struct lpfc_sli4_cfg_mhdr));
7160 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
7161 LPFC_MBOX_OPCODE_NOP, length, LPFC_SLI4_MBX_EMBED);
7162
7163 for (cmdsent = 0; cmdsent < cnt; cmdsent++) {
7164 if (!phba->sli4_hba.intr_enable)
7165 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
7166 else {
7167 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
7168 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
7169 }
7170 if (rc == MBX_TIMEOUT)
7171 break;
7172 /* Check return status */
7173 shdr = (union lpfc_sli4_cfg_shdr *)
7174 &mboxq->u.mqe.un.sli4_config.header.cfg_shdr;
7175 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
7176 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status,
7177 &shdr->response);
7178 if (shdr_status || shdr_add_status || rc) {
7179 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7180 "2520 NOP mailbox command failed "
7181 "status x%x add_status x%x mbx "
7182 "status x%x\n", shdr_status,
7183 shdr_add_status, rc);
7184 break;
7185 }
7186 }
7187
7188 if (rc != MBX_TIMEOUT)
7189 mempool_free(mboxq, phba->mbox_mem_pool);
7190
7191 return cmdsent;
7192 }
7193
7194 /**
7195 * lpfc_sli4_pci_mem_setup - Setup SLI4 HBA PCI memory space.
7196 * @phba: pointer to lpfc hba data structure.
7197 *
7198 * This routine is invoked to set up the PCI device memory space for device
7199 * with SLI-4 interface spec.
7200 *
7201 * Return codes
7202 * 0 - successful
7203 * other values - error
7204 **/
7205 static int
7206 lpfc_sli4_pci_mem_setup(struct lpfc_hba *phba)
7207 {
7208 struct pci_dev *pdev;
7209 unsigned long bar0map_len, bar1map_len, bar2map_len;
7210 int error = -ENODEV;
7211 uint32_t if_type;
7212
7213 /* Obtain PCI device reference */
7214 if (!phba->pcidev)
7215 return error;
7216 else
7217 pdev = phba->pcidev;
7218
7219 /* Set the device DMA mask size */
7220 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0
7221 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(64)) != 0) {
7222 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0
7223 || pci_set_consistent_dma_mask(pdev,DMA_BIT_MASK(32)) != 0) {
7224 return error;
7225 }
7226 }
7227
7228 /*
7229 * The BARs and register set definitions and offset locations are
7230 * dependent on the if_type.
7231 */
7232 if (pci_read_config_dword(pdev, LPFC_SLI_INTF,
7233 &phba->sli4_hba.sli_intf.word0)) {
7234 return error;
7235 }
7236
7237 /* There is no SLI3 failback for SLI4 devices. */
7238 if (bf_get(lpfc_sli_intf_valid, &phba->sli4_hba.sli_intf) !=
7239 LPFC_SLI_INTF_VALID) {
7240 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7241 "2894 SLI_INTF reg contents invalid "
7242 "sli_intf reg 0x%x\n",
7243 phba->sli4_hba.sli_intf.word0);
7244 return error;
7245 }
7246
7247 if_type = bf_get(lpfc_sli_intf_if_type, &phba->sli4_hba.sli_intf);
7248 /*
7249 * Get the bus address of SLI4 device Bar regions and the
7250 * number of bytes required by each mapping. The mapping of the
7251 * particular PCI BARs regions is dependent on the type of
7252 * SLI4 device.
7253 */
7254 if (pci_resource_start(pdev, 0)) {
7255 phba->pci_bar0_map = pci_resource_start(pdev, 0);
7256 bar0map_len = pci_resource_len(pdev, 0);
7257
7258 /*
7259 * Map SLI4 PCI Config Space Register base to a kernel virtual
7260 * addr
7261 */
7262 phba->sli4_hba.conf_regs_memmap_p =
7263 ioremap(phba->pci_bar0_map, bar0map_len);
7264 if (!phba->sli4_hba.conf_regs_memmap_p) {
7265 dev_printk(KERN_ERR, &pdev->dev,
7266 "ioremap failed for SLI4 PCI config "
7267 "registers.\n");
7268 goto out;
7269 }
7270 /* Set up BAR0 PCI config space register memory map */
7271 lpfc_sli4_bar0_register_memmap(phba, if_type);
7272 } else {
7273 phba->pci_bar0_map = pci_resource_start(pdev, 1);
7274 bar0map_len = pci_resource_len(pdev, 1);
7275 if (if_type == LPFC_SLI_INTF_IF_TYPE_2) {
7276 dev_printk(KERN_ERR, &pdev->dev,
7277 "FATAL - No BAR0 mapping for SLI4, if_type 2\n");
7278 goto out;
7279 }
7280 phba->sli4_hba.conf_regs_memmap_p =
7281 ioremap(phba->pci_bar0_map, bar0map_len);
7282 if (!phba->sli4_hba.conf_regs_memmap_p) {
7283 dev_printk(KERN_ERR, &pdev->dev,
7284 "ioremap failed for SLI4 PCI config "
7285 "registers.\n");
7286 goto out;
7287 }
7288 lpfc_sli4_bar0_register_memmap(phba, if_type);
7289 }
7290
7291 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7292 (pci_resource_start(pdev, 2))) {
7293 /*
7294 * Map SLI4 if type 0 HBA Control Register base to a kernel
7295 * virtual address and setup the registers.
7296 */
7297 phba->pci_bar1_map = pci_resource_start(pdev, 2);
7298 bar1map_len = pci_resource_len(pdev, 2);
7299 phba->sli4_hba.ctrl_regs_memmap_p =
7300 ioremap(phba->pci_bar1_map, bar1map_len);
7301 if (!phba->sli4_hba.ctrl_regs_memmap_p) {
7302 dev_printk(KERN_ERR, &pdev->dev,
7303 "ioremap failed for SLI4 HBA control registers.\n");
7304 goto out_iounmap_conf;
7305 }
7306 lpfc_sli4_bar1_register_memmap(phba);
7307 }
7308
7309 if ((if_type == LPFC_SLI_INTF_IF_TYPE_0) &&
7310 (pci_resource_start(pdev, 4))) {
7311 /*
7312 * Map SLI4 if type 0 HBA Doorbell Register base to a kernel
7313 * virtual address and setup the registers.
7314 */
7315 phba->pci_bar2_map = pci_resource_start(pdev, 4);
7316 bar2map_len = pci_resource_len(pdev, 4);
7317 phba->sli4_hba.drbl_regs_memmap_p =
7318 ioremap(phba->pci_bar2_map, bar2map_len);
7319 if (!phba->sli4_hba.drbl_regs_memmap_p) {
7320 dev_printk(KERN_ERR, &pdev->dev,
7321 "ioremap failed for SLI4 HBA doorbell registers.\n");
7322 goto out_iounmap_ctrl;
7323 }
7324 error = lpfc_sli4_bar2_register_memmap(phba, LPFC_VF0);
7325 if (error)
7326 goto out_iounmap_all;
7327 }
7328
7329 return 0;
7330
7331 out_iounmap_all:
7332 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7333 out_iounmap_ctrl:
7334 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7335 out_iounmap_conf:
7336 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7337 out:
7338 return error;
7339 }
7340
7341 /**
7342 * lpfc_sli4_pci_mem_unset - Unset SLI4 HBA PCI memory space.
7343 * @phba: pointer to lpfc hba data structure.
7344 *
7345 * This routine is invoked to unset the PCI device memory space for device
7346 * with SLI-4 interface spec.
7347 **/
7348 static void
7349 lpfc_sli4_pci_mem_unset(struct lpfc_hba *phba)
7350 {
7351 struct pci_dev *pdev;
7352
7353 /* Obtain PCI device reference */
7354 if (!phba->pcidev)
7355 return;
7356 else
7357 pdev = phba->pcidev;
7358
7359 /* Free coherent DMA memory allocated */
7360
7361 /* Unmap I/O memory space */
7362 iounmap(phba->sli4_hba.drbl_regs_memmap_p);
7363 iounmap(phba->sli4_hba.ctrl_regs_memmap_p);
7364 iounmap(phba->sli4_hba.conf_regs_memmap_p);
7365
7366 return;
7367 }
7368
7369 /**
7370 * lpfc_sli_enable_msix - Enable MSI-X interrupt mode on SLI-3 device
7371 * @phba: pointer to lpfc hba data structure.
7372 *
7373 * This routine is invoked to enable the MSI-X interrupt vectors to device
7374 * with SLI-3 interface specs. The kernel function pci_enable_msix() is
7375 * called to enable the MSI-X vectors. Note that pci_enable_msix(), once
7376 * invoked, enables either all or nothing, depending on the current
7377 * availability of PCI vector resources. The device driver is responsible
7378 * for calling the individual request_irq() to register each MSI-X vector
7379 * with a interrupt handler, which is done in this function. Note that
7380 * later when device is unloading, the driver should always call free_irq()
7381 * on all MSI-X vectors it has done request_irq() on before calling
7382 * pci_disable_msix(). Failure to do so results in a BUG_ON() and a device
7383 * will be left with MSI-X enabled and leaks its vectors.
7384 *
7385 * Return codes
7386 * 0 - successful
7387 * other values - error
7388 **/
7389 static int
7390 lpfc_sli_enable_msix(struct lpfc_hba *phba)
7391 {
7392 int rc, i;
7393 LPFC_MBOXQ_t *pmb;
7394
7395 /* Set up MSI-X multi-message vectors */
7396 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7397 phba->msix_entries[i].entry = i;
7398
7399 /* Configure MSI-X capability structure */
7400 rc = pci_enable_msix(phba->pcidev, phba->msix_entries,
7401 ARRAY_SIZE(phba->msix_entries));
7402 if (rc) {
7403 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7404 "0420 PCI enable MSI-X failed (%d)\n", rc);
7405 goto msi_fail_out;
7406 }
7407 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7408 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7409 "0477 MSI-X entry[%d]: vector=x%x "
7410 "message=%d\n", i,
7411 phba->msix_entries[i].vector,
7412 phba->msix_entries[i].entry);
7413 /*
7414 * Assign MSI-X vectors to interrupt handlers
7415 */
7416
7417 /* vector-0 is associated to slow-path handler */
7418 rc = request_irq(phba->msix_entries[0].vector,
7419 &lpfc_sli_sp_intr_handler, IRQF_SHARED,
7420 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7421 if (rc) {
7422 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7423 "0421 MSI-X slow-path request_irq failed "
7424 "(%d)\n", rc);
7425 goto msi_fail_out;
7426 }
7427
7428 /* vector-1 is associated to fast-path handler */
7429 rc = request_irq(phba->msix_entries[1].vector,
7430 &lpfc_sli_fp_intr_handler, IRQF_SHARED,
7431 LPFC_FP_DRIVER_HANDLER_NAME, phba);
7432
7433 if (rc) {
7434 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7435 "0429 MSI-X fast-path request_irq failed "
7436 "(%d)\n", rc);
7437 goto irq_fail_out;
7438 }
7439
7440 /*
7441 * Configure HBA MSI-X attention conditions to messages
7442 */
7443 pmb = (LPFC_MBOXQ_t *) mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
7444
7445 if (!pmb) {
7446 rc = -ENOMEM;
7447 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
7448 "0474 Unable to allocate memory for issuing "
7449 "MBOX_CONFIG_MSI command\n");
7450 goto mem_fail_out;
7451 }
7452 rc = lpfc_config_msi(phba, pmb);
7453 if (rc)
7454 goto mbx_fail_out;
7455 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
7456 if (rc != MBX_SUCCESS) {
7457 lpfc_printf_log(phba, KERN_WARNING, LOG_MBOX,
7458 "0351 Config MSI mailbox command failed, "
7459 "mbxCmd x%x, mbxStatus x%x\n",
7460 pmb->u.mb.mbxCommand, pmb->u.mb.mbxStatus);
7461 goto mbx_fail_out;
7462 }
7463
7464 /* Free memory allocated for mailbox command */
7465 mempool_free(pmb, phba->mbox_mem_pool);
7466 return rc;
7467
7468 mbx_fail_out:
7469 /* Free memory allocated for mailbox command */
7470 mempool_free(pmb, phba->mbox_mem_pool);
7471
7472 mem_fail_out:
7473 /* free the irq already requested */
7474 free_irq(phba->msix_entries[1].vector, phba);
7475
7476 irq_fail_out:
7477 /* free the irq already requested */
7478 free_irq(phba->msix_entries[0].vector, phba);
7479
7480 msi_fail_out:
7481 /* Unconfigure MSI-X capability structure */
7482 pci_disable_msix(phba->pcidev);
7483 return rc;
7484 }
7485
7486 /**
7487 * lpfc_sli_disable_msix - Disable MSI-X interrupt mode on SLI-3 device.
7488 * @phba: pointer to lpfc hba data structure.
7489 *
7490 * This routine is invoked to release the MSI-X vectors and then disable the
7491 * MSI-X interrupt mode to device with SLI-3 interface spec.
7492 **/
7493 static void
7494 lpfc_sli_disable_msix(struct lpfc_hba *phba)
7495 {
7496 int i;
7497
7498 /* Free up MSI-X multi-message vectors */
7499 for (i = 0; i < LPFC_MSIX_VECTORS; i++)
7500 free_irq(phba->msix_entries[i].vector, phba);
7501 /* Disable MSI-X */
7502 pci_disable_msix(phba->pcidev);
7503
7504 return;
7505 }
7506
7507 /**
7508 * lpfc_sli_enable_msi - Enable MSI interrupt mode on SLI-3 device.
7509 * @phba: pointer to lpfc hba data structure.
7510 *
7511 * This routine is invoked to enable the MSI interrupt mode to device with
7512 * SLI-3 interface spec. The kernel function pci_enable_msi() is called to
7513 * enable the MSI vector. The device driver is responsible for calling the
7514 * request_irq() to register MSI vector with a interrupt the handler, which
7515 * is done in this function.
7516 *
7517 * Return codes
7518 * 0 - successful
7519 * other values - error
7520 */
7521 static int
7522 lpfc_sli_enable_msi(struct lpfc_hba *phba)
7523 {
7524 int rc;
7525
7526 rc = pci_enable_msi(phba->pcidev);
7527 if (!rc)
7528 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7529 "0462 PCI enable MSI mode success.\n");
7530 else {
7531 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7532 "0471 PCI enable MSI mode failed (%d)\n", rc);
7533 return rc;
7534 }
7535
7536 rc = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7537 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7538 if (rc) {
7539 pci_disable_msi(phba->pcidev);
7540 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7541 "0478 MSI request_irq failed (%d)\n", rc);
7542 }
7543 return rc;
7544 }
7545
7546 /**
7547 * lpfc_sli_disable_msi - Disable MSI interrupt mode to SLI-3 device.
7548 * @phba: pointer to lpfc hba data structure.
7549 *
7550 * This routine is invoked to disable the MSI interrupt mode to device with
7551 * SLI-3 interface spec. The driver calls free_irq() on MSI vector it has
7552 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7553 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7554 * its vector.
7555 */
7556 static void
7557 lpfc_sli_disable_msi(struct lpfc_hba *phba)
7558 {
7559 free_irq(phba->pcidev->irq, phba);
7560 pci_disable_msi(phba->pcidev);
7561 return;
7562 }
7563
7564 /**
7565 * lpfc_sli_enable_intr - Enable device interrupt to SLI-3 device.
7566 * @phba: pointer to lpfc hba data structure.
7567 *
7568 * This routine is invoked to enable device interrupt and associate driver's
7569 * interrupt handler(s) to interrupt vector(s) to device with SLI-3 interface
7570 * spec. Depends on the interrupt mode configured to the driver, the driver
7571 * will try to fallback from the configured interrupt mode to an interrupt
7572 * mode which is supported by the platform, kernel, and device in the order
7573 * of:
7574 * MSI-X -> MSI -> IRQ.
7575 *
7576 * Return codes
7577 * 0 - successful
7578 * other values - error
7579 **/
7580 static uint32_t
7581 lpfc_sli_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7582 {
7583 uint32_t intr_mode = LPFC_INTR_ERROR;
7584 int retval;
7585
7586 if (cfg_mode == 2) {
7587 /* Need to issue conf_port mbox cmd before conf_msi mbox cmd */
7588 retval = lpfc_sli_config_port(phba, LPFC_SLI_REV3);
7589 if (!retval) {
7590 /* Now, try to enable MSI-X interrupt mode */
7591 retval = lpfc_sli_enable_msix(phba);
7592 if (!retval) {
7593 /* Indicate initialization to MSI-X mode */
7594 phba->intr_type = MSIX;
7595 intr_mode = 2;
7596 }
7597 }
7598 }
7599
7600 /* Fallback to MSI if MSI-X initialization failed */
7601 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7602 retval = lpfc_sli_enable_msi(phba);
7603 if (!retval) {
7604 /* Indicate initialization to MSI mode */
7605 phba->intr_type = MSI;
7606 intr_mode = 1;
7607 }
7608 }
7609
7610 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7611 if (phba->intr_type == NONE) {
7612 retval = request_irq(phba->pcidev->irq, lpfc_sli_intr_handler,
7613 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7614 if (!retval) {
7615 /* Indicate initialization to INTx mode */
7616 phba->intr_type = INTx;
7617 intr_mode = 0;
7618 }
7619 }
7620 return intr_mode;
7621 }
7622
7623 /**
7624 * lpfc_sli_disable_intr - Disable device interrupt to SLI-3 device.
7625 * @phba: pointer to lpfc hba data structure.
7626 *
7627 * This routine is invoked to disable device interrupt and disassociate the
7628 * driver's interrupt handler(s) from interrupt vector(s) to device with
7629 * SLI-3 interface spec. Depending on the interrupt mode, the driver will
7630 * release the interrupt vector(s) for the message signaled interrupt.
7631 **/
7632 static void
7633 lpfc_sli_disable_intr(struct lpfc_hba *phba)
7634 {
7635 /* Disable the currently initialized interrupt mode */
7636 if (phba->intr_type == MSIX)
7637 lpfc_sli_disable_msix(phba);
7638 else if (phba->intr_type == MSI)
7639 lpfc_sli_disable_msi(phba);
7640 else if (phba->intr_type == INTx)
7641 free_irq(phba->pcidev->irq, phba);
7642
7643 /* Reset interrupt management states */
7644 phba->intr_type = NONE;
7645 phba->sli.slistat.sli_intr = 0;
7646
7647 return;
7648 }
7649
7650 /**
7651 * lpfc_sli4_enable_msix - Enable MSI-X interrupt mode to SLI-4 device
7652 * @phba: pointer to lpfc hba data structure.
7653 *
7654 * This routine is invoked to enable the MSI-X interrupt vectors to device
7655 * with SLI-4 interface spec. The kernel function pci_enable_msix() is called
7656 * to enable the MSI-X vectors. Note that pci_enable_msix(), once invoked,
7657 * enables either all or nothing, depending on the current availability of
7658 * PCI vector resources. The device driver is responsible for calling the
7659 * individual request_irq() to register each MSI-X vector with a interrupt
7660 * handler, which is done in this function. Note that later when device is
7661 * unloading, the driver should always call free_irq() on all MSI-X vectors
7662 * it has done request_irq() on before calling pci_disable_msix(). Failure
7663 * to do so results in a BUG_ON() and a device will be left with MSI-X
7664 * enabled and leaks its vectors.
7665 *
7666 * Return codes
7667 * 0 - successful
7668 * other values - error
7669 **/
7670 static int
7671 lpfc_sli4_enable_msix(struct lpfc_hba *phba)
7672 {
7673 int vectors, rc, index;
7674
7675 /* Set up MSI-X multi-message vectors */
7676 for (index = 0; index < phba->sli4_hba.cfg_eqn; index++)
7677 phba->sli4_hba.msix_entries[index].entry = index;
7678
7679 /* Configure MSI-X capability structure */
7680 vectors = phba->sli4_hba.cfg_eqn;
7681 enable_msix_vectors:
7682 rc = pci_enable_msix(phba->pcidev, phba->sli4_hba.msix_entries,
7683 vectors);
7684 if (rc > 1) {
7685 vectors = rc;
7686 goto enable_msix_vectors;
7687 } else if (rc) {
7688 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7689 "0484 PCI enable MSI-X failed (%d)\n", rc);
7690 goto msi_fail_out;
7691 }
7692
7693 /* Log MSI-X vector assignment */
7694 for (index = 0; index < vectors; index++)
7695 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7696 "0489 MSI-X entry[%d]: vector=x%x "
7697 "message=%d\n", index,
7698 phba->sli4_hba.msix_entries[index].vector,
7699 phba->sli4_hba.msix_entries[index].entry);
7700 /*
7701 * Assign MSI-X vectors to interrupt handlers
7702 */
7703 if (vectors > 1)
7704 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7705 &lpfc_sli4_sp_intr_handler, IRQF_SHARED,
7706 LPFC_SP_DRIVER_HANDLER_NAME, phba);
7707 else
7708 /* All Interrupts need to be handled by one EQ */
7709 rc = request_irq(phba->sli4_hba.msix_entries[0].vector,
7710 &lpfc_sli4_intr_handler, IRQF_SHARED,
7711 LPFC_DRIVER_NAME, phba);
7712 if (rc) {
7713 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7714 "0485 MSI-X slow-path request_irq failed "
7715 "(%d)\n", rc);
7716 goto msi_fail_out;
7717 }
7718
7719 /* The rest of the vector(s) are associated to fast-path handler(s) */
7720 for (index = 1; index < vectors; index++) {
7721 phba->sli4_hba.fcp_eq_hdl[index - 1].idx = index - 1;
7722 phba->sli4_hba.fcp_eq_hdl[index - 1].phba = phba;
7723 rc = request_irq(phba->sli4_hba.msix_entries[index].vector,
7724 &lpfc_sli4_fp_intr_handler, IRQF_SHARED,
7725 LPFC_FP_DRIVER_HANDLER_NAME,
7726 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7727 if (rc) {
7728 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7729 "0486 MSI-X fast-path (%d) "
7730 "request_irq failed (%d)\n", index, rc);
7731 goto cfg_fail_out;
7732 }
7733 }
7734 phba->sli4_hba.msix_vec_nr = vectors;
7735
7736 return rc;
7737
7738 cfg_fail_out:
7739 /* free the irq already requested */
7740 for (--index; index >= 1; index--)
7741 free_irq(phba->sli4_hba.msix_entries[index - 1].vector,
7742 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7743
7744 /* free the irq already requested */
7745 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7746
7747 msi_fail_out:
7748 /* Unconfigure MSI-X capability structure */
7749 pci_disable_msix(phba->pcidev);
7750 return rc;
7751 }
7752
7753 /**
7754 * lpfc_sli4_disable_msix - Disable MSI-X interrupt mode to SLI-4 device
7755 * @phba: pointer to lpfc hba data structure.
7756 *
7757 * This routine is invoked to release the MSI-X vectors and then disable the
7758 * MSI-X interrupt mode to device with SLI-4 interface spec.
7759 **/
7760 static void
7761 lpfc_sli4_disable_msix(struct lpfc_hba *phba)
7762 {
7763 int index;
7764
7765 /* Free up MSI-X multi-message vectors */
7766 free_irq(phba->sli4_hba.msix_entries[0].vector, phba);
7767
7768 for (index = 1; index < phba->sli4_hba.msix_vec_nr; index++)
7769 free_irq(phba->sli4_hba.msix_entries[index].vector,
7770 &phba->sli4_hba.fcp_eq_hdl[index - 1]);
7771
7772 /* Disable MSI-X */
7773 pci_disable_msix(phba->pcidev);
7774
7775 return;
7776 }
7777
7778 /**
7779 * lpfc_sli4_enable_msi - Enable MSI interrupt mode to SLI-4 device
7780 * @phba: pointer to lpfc hba data structure.
7781 *
7782 * This routine is invoked to enable the MSI interrupt mode to device with
7783 * SLI-4 interface spec. The kernel function pci_enable_msi() is called
7784 * to enable the MSI vector. The device driver is responsible for calling
7785 * the request_irq() to register MSI vector with a interrupt the handler,
7786 * which is done in this function.
7787 *
7788 * Return codes
7789 * 0 - successful
7790 * other values - error
7791 **/
7792 static int
7793 lpfc_sli4_enable_msi(struct lpfc_hba *phba)
7794 {
7795 int rc, index;
7796
7797 rc = pci_enable_msi(phba->pcidev);
7798 if (!rc)
7799 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7800 "0487 PCI enable MSI mode success.\n");
7801 else {
7802 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
7803 "0488 PCI enable MSI mode failed (%d)\n", rc);
7804 return rc;
7805 }
7806
7807 rc = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7808 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7809 if (rc) {
7810 pci_disable_msi(phba->pcidev);
7811 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
7812 "0490 MSI request_irq failed (%d)\n", rc);
7813 return rc;
7814 }
7815
7816 for (index = 0; index < phba->cfg_fcp_eq_count; index++) {
7817 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7818 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7819 }
7820
7821 return 0;
7822 }
7823
7824 /**
7825 * lpfc_sli4_disable_msi - Disable MSI interrupt mode to SLI-4 device
7826 * @phba: pointer to lpfc hba data structure.
7827 *
7828 * This routine is invoked to disable the MSI interrupt mode to device with
7829 * SLI-4 interface spec. The driver calls free_irq() on MSI vector it has
7830 * done request_irq() on before calling pci_disable_msi(). Failure to do so
7831 * results in a BUG_ON() and a device will be left with MSI enabled and leaks
7832 * its vector.
7833 **/
7834 static void
7835 lpfc_sli4_disable_msi(struct lpfc_hba *phba)
7836 {
7837 free_irq(phba->pcidev->irq, phba);
7838 pci_disable_msi(phba->pcidev);
7839 return;
7840 }
7841
7842 /**
7843 * lpfc_sli4_enable_intr - Enable device interrupt to SLI-4 device
7844 * @phba: pointer to lpfc hba data structure.
7845 *
7846 * This routine is invoked to enable device interrupt and associate driver's
7847 * interrupt handler(s) to interrupt vector(s) to device with SLI-4
7848 * interface spec. Depends on the interrupt mode configured to the driver,
7849 * the driver will try to fallback from the configured interrupt mode to an
7850 * interrupt mode which is supported by the platform, kernel, and device in
7851 * the order of:
7852 * MSI-X -> MSI -> IRQ.
7853 *
7854 * Return codes
7855 * 0 - successful
7856 * other values - error
7857 **/
7858 static uint32_t
7859 lpfc_sli4_enable_intr(struct lpfc_hba *phba, uint32_t cfg_mode)
7860 {
7861 uint32_t intr_mode = LPFC_INTR_ERROR;
7862 int retval, index;
7863
7864 if (cfg_mode == 2) {
7865 /* Preparation before conf_msi mbox cmd */
7866 retval = 0;
7867 if (!retval) {
7868 /* Now, try to enable MSI-X interrupt mode */
7869 retval = lpfc_sli4_enable_msix(phba);
7870 if (!retval) {
7871 /* Indicate initialization to MSI-X mode */
7872 phba->intr_type = MSIX;
7873 intr_mode = 2;
7874 }
7875 }
7876 }
7877
7878 /* Fallback to MSI if MSI-X initialization failed */
7879 if (cfg_mode >= 1 && phba->intr_type == NONE) {
7880 retval = lpfc_sli4_enable_msi(phba);
7881 if (!retval) {
7882 /* Indicate initialization to MSI mode */
7883 phba->intr_type = MSI;
7884 intr_mode = 1;
7885 }
7886 }
7887
7888 /* Fallback to INTx if both MSI-X/MSI initalization failed */
7889 if (phba->intr_type == NONE) {
7890 retval = request_irq(phba->pcidev->irq, lpfc_sli4_intr_handler,
7891 IRQF_SHARED, LPFC_DRIVER_NAME, phba);
7892 if (!retval) {
7893 /* Indicate initialization to INTx mode */
7894 phba->intr_type = INTx;
7895 intr_mode = 0;
7896 for (index = 0; index < phba->cfg_fcp_eq_count;
7897 index++) {
7898 phba->sli4_hba.fcp_eq_hdl[index].idx = index;
7899 phba->sli4_hba.fcp_eq_hdl[index].phba = phba;
7900 }
7901 }
7902 }
7903 return intr_mode;
7904 }
7905
7906 /**
7907 * lpfc_sli4_disable_intr - Disable device interrupt to SLI-4 device
7908 * @phba: pointer to lpfc hba data structure.
7909 *
7910 * This routine is invoked to disable device interrupt and disassociate
7911 * the driver's interrupt handler(s) from interrupt vector(s) to device
7912 * with SLI-4 interface spec. Depending on the interrupt mode, the driver
7913 * will release the interrupt vector(s) for the message signaled interrupt.
7914 **/
7915 static void
7916 lpfc_sli4_disable_intr(struct lpfc_hba *phba)
7917 {
7918 /* Disable the currently initialized interrupt mode */
7919 if (phba->intr_type == MSIX)
7920 lpfc_sli4_disable_msix(phba);
7921 else if (phba->intr_type == MSI)
7922 lpfc_sli4_disable_msi(phba);
7923 else if (phba->intr_type == INTx)
7924 free_irq(phba->pcidev->irq, phba);
7925
7926 /* Reset interrupt management states */
7927 phba->intr_type = NONE;
7928 phba->sli.slistat.sli_intr = 0;
7929
7930 return;
7931 }
7932
7933 /**
7934 * lpfc_unset_hba - Unset SLI3 hba device initialization
7935 * @phba: pointer to lpfc hba data structure.
7936 *
7937 * This routine is invoked to unset the HBA device initialization steps to
7938 * a device with SLI-3 interface spec.
7939 **/
7940 static void
7941 lpfc_unset_hba(struct lpfc_hba *phba)
7942 {
7943 struct lpfc_vport *vport = phba->pport;
7944 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7945
7946 spin_lock_irq(shost->host_lock);
7947 vport->load_flag |= FC_UNLOADING;
7948 spin_unlock_irq(shost->host_lock);
7949
7950 lpfc_stop_hba_timers(phba);
7951
7952 phba->pport->work_port_events = 0;
7953
7954 lpfc_sli_hba_down(phba);
7955
7956 lpfc_sli_brdrestart(phba);
7957
7958 lpfc_sli_disable_intr(phba);
7959
7960 return;
7961 }
7962
7963 /**
7964 * lpfc_sli4_unset_hba - Unset SLI4 hba device initialization.
7965 * @phba: pointer to lpfc hba data structure.
7966 *
7967 * This routine is invoked to unset the HBA device initialization steps to
7968 * a device with SLI-4 interface spec.
7969 **/
7970 static void
7971 lpfc_sli4_unset_hba(struct lpfc_hba *phba)
7972 {
7973 struct lpfc_vport *vport = phba->pport;
7974 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
7975
7976 spin_lock_irq(shost->host_lock);
7977 vport->load_flag |= FC_UNLOADING;
7978 spin_unlock_irq(shost->host_lock);
7979
7980 phba->pport->work_port_events = 0;
7981
7982 /* Stop the SLI4 device port */
7983 lpfc_stop_port(phba);
7984
7985 lpfc_sli4_disable_intr(phba);
7986
7987 /* Reset SLI4 HBA FCoE function */
7988 lpfc_pci_function_reset(phba);
7989
7990 return;
7991 }
7992
7993 /**
7994 * lpfc_sli4_xri_exchange_busy_wait - Wait for device XRI exchange busy
7995 * @phba: Pointer to HBA context object.
7996 *
7997 * This function is called in the SLI4 code path to wait for completion
7998 * of device's XRIs exchange busy. It will check the XRI exchange busy
7999 * on outstanding FCP and ELS I/Os every 10ms for up to 10 seconds; after
8000 * that, it will check the XRI exchange busy on outstanding FCP and ELS
8001 * I/Os every 30 seconds, log error message, and wait forever. Only when
8002 * all XRI exchange busy complete, the driver unload shall proceed with
8003 * invoking the function reset ioctl mailbox command to the CNA and the
8004 * the rest of the driver unload resource release.
8005 **/
8006 static void
8007 lpfc_sli4_xri_exchange_busy_wait(struct lpfc_hba *phba)
8008 {
8009 int wait_time = 0;
8010 int fcp_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8011 int els_xri_cmpl = list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8012
8013 while (!fcp_xri_cmpl || !els_xri_cmpl) {
8014 if (wait_time > LPFC_XRI_EXCH_BUSY_WAIT_TMO) {
8015 if (!fcp_xri_cmpl)
8016 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8017 "2877 FCP XRI exchange busy "
8018 "wait time: %d seconds.\n",
8019 wait_time/1000);
8020 if (!els_xri_cmpl)
8021 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8022 "2878 ELS XRI exchange busy "
8023 "wait time: %d seconds.\n",
8024 wait_time/1000);
8025 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T2);
8026 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T2;
8027 } else {
8028 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
8029 wait_time += LPFC_XRI_EXCH_BUSY_WAIT_T1;
8030 }
8031 fcp_xri_cmpl =
8032 list_empty(&phba->sli4_hba.lpfc_abts_scsi_buf_list);
8033 els_xri_cmpl =
8034 list_empty(&phba->sli4_hba.lpfc_abts_els_sgl_list);
8035 }
8036 }
8037
8038 /**
8039 * lpfc_sli4_hba_unset - Unset the fcoe hba
8040 * @phba: Pointer to HBA context object.
8041 *
8042 * This function is called in the SLI4 code path to reset the HBA's FCoE
8043 * function. The caller is not required to hold any lock. This routine
8044 * issues PCI function reset mailbox command to reset the FCoE function.
8045 * At the end of the function, it calls lpfc_hba_down_post function to
8046 * free any pending commands.
8047 **/
8048 static void
8049 lpfc_sli4_hba_unset(struct lpfc_hba *phba)
8050 {
8051 int wait_cnt = 0;
8052 LPFC_MBOXQ_t *mboxq;
8053 struct pci_dev *pdev = phba->pcidev;
8054
8055 lpfc_stop_hba_timers(phba);
8056 phba->sli4_hba.intr_enable = 0;
8057
8058 /*
8059 * Gracefully wait out the potential current outstanding asynchronous
8060 * mailbox command.
8061 */
8062
8063 /* First, block any pending async mailbox command from posted */
8064 spin_lock_irq(&phba->hbalock);
8065 phba->sli.sli_flag |= LPFC_SLI_ASYNC_MBX_BLK;
8066 spin_unlock_irq(&phba->hbalock);
8067 /* Now, trying to wait it out if we can */
8068 while (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8069 msleep(10);
8070 if (++wait_cnt > LPFC_ACTIVE_MBOX_WAIT_CNT)
8071 break;
8072 }
8073 /* Forcefully release the outstanding mailbox command if timed out */
8074 if (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE) {
8075 spin_lock_irq(&phba->hbalock);
8076 mboxq = phba->sli.mbox_active;
8077 mboxq->u.mb.mbxStatus = MBX_NOT_FINISHED;
8078 __lpfc_mbox_cmpl_put(phba, mboxq);
8079 phba->sli.sli_flag &= ~LPFC_SLI_MBOX_ACTIVE;
8080 phba->sli.mbox_active = NULL;
8081 spin_unlock_irq(&phba->hbalock);
8082 }
8083
8084 /* Abort all iocbs associated with the hba */
8085 lpfc_sli_hba_iocb_abort(phba);
8086
8087 /* Wait for completion of device XRI exchange busy */
8088 lpfc_sli4_xri_exchange_busy_wait(phba);
8089
8090 /* Disable PCI subsystem interrupt */
8091 lpfc_sli4_disable_intr(phba);
8092
8093 /* Disable SR-IOV if enabled */
8094 if (phba->cfg_sriov_nr_virtfn)
8095 pci_disable_sriov(pdev);
8096
8097 /* Stop kthread signal shall trigger work_done one more time */
8098 kthread_stop(phba->worker_thread);
8099
8100 /* Reset SLI4 HBA FCoE function */
8101 lpfc_pci_function_reset(phba);
8102
8103 /* Stop the SLI4 device port */
8104 phba->pport->work_port_events = 0;
8105 }
8106
8107 /**
8108 * lpfc_pc_sli4_params_get - Get the SLI4_PARAMS port capabilities.
8109 * @phba: Pointer to HBA context object.
8110 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8111 *
8112 * This function is called in the SLI4 code path to read the port's
8113 * sli4 capabilities.
8114 *
8115 * This function may be be called from any context that can block-wait
8116 * for the completion. The expectation is that this routine is called
8117 * typically from probe_one or from the online routine.
8118 **/
8119 int
8120 lpfc_pc_sli4_params_get(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8121 {
8122 int rc;
8123 struct lpfc_mqe *mqe;
8124 struct lpfc_pc_sli4_params *sli4_params;
8125 uint32_t mbox_tmo;
8126
8127 rc = 0;
8128 mqe = &mboxq->u.mqe;
8129
8130 /* Read the port's SLI4 Parameters port capabilities */
8131 lpfc_pc_sli4_params(mboxq);
8132 if (!phba->sli4_hba.intr_enable)
8133 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8134 else {
8135 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8136 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8137 }
8138
8139 if (unlikely(rc))
8140 return 1;
8141
8142 sli4_params = &phba->sli4_hba.pc_sli4_params;
8143 sli4_params->if_type = bf_get(if_type, &mqe->un.sli4_params);
8144 sli4_params->sli_rev = bf_get(sli_rev, &mqe->un.sli4_params);
8145 sli4_params->sli_family = bf_get(sli_family, &mqe->un.sli4_params);
8146 sli4_params->featurelevel_1 = bf_get(featurelevel_1,
8147 &mqe->un.sli4_params);
8148 sli4_params->featurelevel_2 = bf_get(featurelevel_2,
8149 &mqe->un.sli4_params);
8150 sli4_params->proto_types = mqe->un.sli4_params.word3;
8151 sli4_params->sge_supp_len = mqe->un.sli4_params.sge_supp_len;
8152 sli4_params->if_page_sz = bf_get(if_page_sz, &mqe->un.sli4_params);
8153 sli4_params->rq_db_window = bf_get(rq_db_window, &mqe->un.sli4_params);
8154 sli4_params->loopbk_scope = bf_get(loopbk_scope, &mqe->un.sli4_params);
8155 sli4_params->eq_pages_max = bf_get(eq_pages, &mqe->un.sli4_params);
8156 sli4_params->eqe_size = bf_get(eqe_size, &mqe->un.sli4_params);
8157 sli4_params->cq_pages_max = bf_get(cq_pages, &mqe->un.sli4_params);
8158 sli4_params->cqe_size = bf_get(cqe_size, &mqe->un.sli4_params);
8159 sli4_params->mq_pages_max = bf_get(mq_pages, &mqe->un.sli4_params);
8160 sli4_params->mqe_size = bf_get(mqe_size, &mqe->un.sli4_params);
8161 sli4_params->mq_elem_cnt = bf_get(mq_elem_cnt, &mqe->un.sli4_params);
8162 sli4_params->wq_pages_max = bf_get(wq_pages, &mqe->un.sli4_params);
8163 sli4_params->wqe_size = bf_get(wqe_size, &mqe->un.sli4_params);
8164 sli4_params->rq_pages_max = bf_get(rq_pages, &mqe->un.sli4_params);
8165 sli4_params->rqe_size = bf_get(rqe_size, &mqe->un.sli4_params);
8166 sli4_params->hdr_pages_max = bf_get(hdr_pages, &mqe->un.sli4_params);
8167 sli4_params->hdr_size = bf_get(hdr_size, &mqe->un.sli4_params);
8168 sli4_params->hdr_pp_align = bf_get(hdr_pp_align, &mqe->un.sli4_params);
8169 sli4_params->sgl_pages_max = bf_get(sgl_pages, &mqe->un.sli4_params);
8170 sli4_params->sgl_pp_align = bf_get(sgl_pp_align, &mqe->un.sli4_params);
8171
8172 /* Make sure that sge_supp_len can be handled by the driver */
8173 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8174 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8175
8176 return rc;
8177 }
8178
8179 /**
8180 * lpfc_get_sli4_parameters - Get the SLI4 Config PARAMETERS.
8181 * @phba: Pointer to HBA context object.
8182 * @mboxq: Pointer to the mailboxq memory for the mailbox command response.
8183 *
8184 * This function is called in the SLI4 code path to read the port's
8185 * sli4 capabilities.
8186 *
8187 * This function may be be called from any context that can block-wait
8188 * for the completion. The expectation is that this routine is called
8189 * typically from probe_one or from the online routine.
8190 **/
8191 int
8192 lpfc_get_sli4_parameters(struct lpfc_hba *phba, LPFC_MBOXQ_t *mboxq)
8193 {
8194 int rc;
8195 struct lpfc_mqe *mqe = &mboxq->u.mqe;
8196 struct lpfc_pc_sli4_params *sli4_params;
8197 uint32_t mbox_tmo;
8198 int length;
8199 struct lpfc_sli4_parameters *mbx_sli4_parameters;
8200
8201 /*
8202 * By default, the driver assumes the SLI4 port requires RPI
8203 * header postings. The SLI4_PARAM response will correct this
8204 * assumption.
8205 */
8206 phba->sli4_hba.rpi_hdrs_in_use = 1;
8207
8208 /* Read the port's SLI4 Config Parameters */
8209 length = (sizeof(struct lpfc_mbx_get_sli4_parameters) -
8210 sizeof(struct lpfc_sli4_cfg_mhdr));
8211 lpfc_sli4_config(phba, mboxq, LPFC_MBOX_SUBSYSTEM_COMMON,
8212 LPFC_MBOX_OPCODE_GET_SLI4_PARAMETERS,
8213 length, LPFC_SLI4_MBX_EMBED);
8214 if (!phba->sli4_hba.intr_enable)
8215 rc = lpfc_sli_issue_mbox(phba, mboxq, MBX_POLL);
8216 else {
8217 mbox_tmo = lpfc_mbox_tmo_val(phba, mboxq);
8218 rc = lpfc_sli_issue_mbox_wait(phba, mboxq, mbox_tmo);
8219 }
8220 if (unlikely(rc))
8221 return rc;
8222 sli4_params = &phba->sli4_hba.pc_sli4_params;
8223 mbx_sli4_parameters = &mqe->un.get_sli4_parameters.sli4_parameters;
8224 sli4_params->if_type = bf_get(cfg_if_type, mbx_sli4_parameters);
8225 sli4_params->sli_rev = bf_get(cfg_sli_rev, mbx_sli4_parameters);
8226 sli4_params->sli_family = bf_get(cfg_sli_family, mbx_sli4_parameters);
8227 sli4_params->featurelevel_1 = bf_get(cfg_sli_hint_1,
8228 mbx_sli4_parameters);
8229 sli4_params->featurelevel_2 = bf_get(cfg_sli_hint_2,
8230 mbx_sli4_parameters);
8231 if (bf_get(cfg_phwq, mbx_sli4_parameters))
8232 phba->sli3_options |= LPFC_SLI4_PHWQ_ENABLED;
8233 else
8234 phba->sli3_options &= ~LPFC_SLI4_PHWQ_ENABLED;
8235 sli4_params->sge_supp_len = mbx_sli4_parameters->sge_supp_len;
8236 sli4_params->loopbk_scope = bf_get(loopbk_scope, mbx_sli4_parameters);
8237 sli4_params->cqv = bf_get(cfg_cqv, mbx_sli4_parameters);
8238 sli4_params->mqv = bf_get(cfg_mqv, mbx_sli4_parameters);
8239 sli4_params->wqv = bf_get(cfg_wqv, mbx_sli4_parameters);
8240 sli4_params->rqv = bf_get(cfg_rqv, mbx_sli4_parameters);
8241 sli4_params->sgl_pages_max = bf_get(cfg_sgl_page_cnt,
8242 mbx_sli4_parameters);
8243 sli4_params->sgl_pp_align = bf_get(cfg_sgl_pp_align,
8244 mbx_sli4_parameters);
8245 phba->sli4_hba.extents_in_use = bf_get(cfg_ext, mbx_sli4_parameters);
8246 phba->sli4_hba.rpi_hdrs_in_use = bf_get(cfg_hdrr, mbx_sli4_parameters);
8247
8248 /* Make sure that sge_supp_len can be handled by the driver */
8249 if (sli4_params->sge_supp_len > LPFC_MAX_SGE_SIZE)
8250 sli4_params->sge_supp_len = LPFC_MAX_SGE_SIZE;
8251
8252 return 0;
8253 }
8254
8255 /**
8256 * lpfc_pci_probe_one_s3 - PCI probe func to reg SLI-3 device to PCI subsystem.
8257 * @pdev: pointer to PCI device
8258 * @pid: pointer to PCI device identifier
8259 *
8260 * This routine is to be called to attach a device with SLI-3 interface spec
8261 * to the PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8262 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8263 * information of the device and driver to see if the driver state that it can
8264 * support this kind of device. If the match is successful, the driver core
8265 * invokes this routine. If this routine determines it can claim the HBA, it
8266 * does all the initialization that it needs to do to handle the HBA properly.
8267 *
8268 * Return code
8269 * 0 - driver can claim the device
8270 * negative value - driver can not claim the device
8271 **/
8272 static int __devinit
8273 lpfc_pci_probe_one_s3(struct pci_dev *pdev, const struct pci_device_id *pid)
8274 {
8275 struct lpfc_hba *phba;
8276 struct lpfc_vport *vport = NULL;
8277 struct Scsi_Host *shost = NULL;
8278 int error;
8279 uint32_t cfg_mode, intr_mode;
8280
8281 /* Allocate memory for HBA structure */
8282 phba = lpfc_hba_alloc(pdev);
8283 if (!phba)
8284 return -ENOMEM;
8285
8286 /* Perform generic PCI device enabling operation */
8287 error = lpfc_enable_pci_dev(phba);
8288 if (error)
8289 goto out_free_phba;
8290
8291 /* Set up SLI API function jump table for PCI-device group-0 HBAs */
8292 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_LP);
8293 if (error)
8294 goto out_disable_pci_dev;
8295
8296 /* Set up SLI-3 specific device PCI memory space */
8297 error = lpfc_sli_pci_mem_setup(phba);
8298 if (error) {
8299 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8300 "1402 Failed to set up pci memory space.\n");
8301 goto out_disable_pci_dev;
8302 }
8303
8304 /* Set up phase-1 common device driver resources */
8305 error = lpfc_setup_driver_resource_phase1(phba);
8306 if (error) {
8307 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8308 "1403 Failed to set up driver resource.\n");
8309 goto out_unset_pci_mem_s3;
8310 }
8311
8312 /* Set up SLI-3 specific device driver resources */
8313 error = lpfc_sli_driver_resource_setup(phba);
8314 if (error) {
8315 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8316 "1404 Failed to set up driver resource.\n");
8317 goto out_unset_pci_mem_s3;
8318 }
8319
8320 /* Initialize and populate the iocb list per host */
8321 error = lpfc_init_iocb_list(phba, LPFC_IOCB_LIST_CNT);
8322 if (error) {
8323 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8324 "1405 Failed to initialize iocb list.\n");
8325 goto out_unset_driver_resource_s3;
8326 }
8327
8328 /* Set up common device driver resources */
8329 error = lpfc_setup_driver_resource_phase2(phba);
8330 if (error) {
8331 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8332 "1406 Failed to set up driver resource.\n");
8333 goto out_free_iocb_list;
8334 }
8335
8336 /* Get the default values for Model Name and Description */
8337 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
8338
8339 /* Create SCSI host to the physical port */
8340 error = lpfc_create_shost(phba);
8341 if (error) {
8342 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8343 "1407 Failed to create scsi host.\n");
8344 goto out_unset_driver_resource;
8345 }
8346
8347 /* Configure sysfs attributes */
8348 vport = phba->pport;
8349 error = lpfc_alloc_sysfs_attr(vport);
8350 if (error) {
8351 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8352 "1476 Failed to allocate sysfs attr\n");
8353 goto out_destroy_shost;
8354 }
8355
8356 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
8357 /* Now, trying to enable interrupt and bring up the device */
8358 cfg_mode = phba->cfg_use_msi;
8359 while (true) {
8360 /* Put device to a known state before enabling interrupt */
8361 lpfc_stop_port(phba);
8362 /* Configure and enable interrupt */
8363 intr_mode = lpfc_sli_enable_intr(phba, cfg_mode);
8364 if (intr_mode == LPFC_INTR_ERROR) {
8365 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8366 "0431 Failed to enable interrupt.\n");
8367 error = -ENODEV;
8368 goto out_free_sysfs_attr;
8369 }
8370 /* SLI-3 HBA setup */
8371 if (lpfc_sli_hba_setup(phba)) {
8372 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8373 "1477 Failed to set up hba\n");
8374 error = -ENODEV;
8375 goto out_remove_device;
8376 }
8377
8378 /* Wait 50ms for the interrupts of previous mailbox commands */
8379 msleep(50);
8380 /* Check active interrupts on message signaled interrupts */
8381 if (intr_mode == 0 ||
8382 phba->sli.slistat.sli_intr > LPFC_MSIX_VECTORS) {
8383 /* Log the current active interrupt mode */
8384 phba->intr_mode = intr_mode;
8385 lpfc_log_intr_mode(phba, intr_mode);
8386 break;
8387 } else {
8388 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8389 "0447 Configure interrupt mode (%d) "
8390 "failed active interrupt test.\n",
8391 intr_mode);
8392 /* Disable the current interrupt mode */
8393 lpfc_sli_disable_intr(phba);
8394 /* Try next level of interrupt mode */
8395 cfg_mode = --intr_mode;
8396 }
8397 }
8398
8399 /* Perform post initialization setup */
8400 lpfc_post_init_setup(phba);
8401
8402 /* Check if there are static vports to be created. */
8403 lpfc_create_static_vport(phba);
8404
8405 return 0;
8406
8407 out_remove_device:
8408 lpfc_unset_hba(phba);
8409 out_free_sysfs_attr:
8410 lpfc_free_sysfs_attr(vport);
8411 out_destroy_shost:
8412 lpfc_destroy_shost(phba);
8413 out_unset_driver_resource:
8414 lpfc_unset_driver_resource_phase2(phba);
8415 out_free_iocb_list:
8416 lpfc_free_iocb_list(phba);
8417 out_unset_driver_resource_s3:
8418 lpfc_sli_driver_resource_unset(phba);
8419 out_unset_pci_mem_s3:
8420 lpfc_sli_pci_mem_unset(phba);
8421 out_disable_pci_dev:
8422 lpfc_disable_pci_dev(phba);
8423 if (shost)
8424 scsi_host_put(shost);
8425 out_free_phba:
8426 lpfc_hba_free(phba);
8427 return error;
8428 }
8429
8430 /**
8431 * lpfc_pci_remove_one_s3 - PCI func to unreg SLI-3 device from PCI subsystem.
8432 * @pdev: pointer to PCI device
8433 *
8434 * This routine is to be called to disattach a device with SLI-3 interface
8435 * spec from PCI subsystem. When an Emulex HBA with SLI-3 interface spec is
8436 * removed from PCI bus, it performs all the necessary cleanup for the HBA
8437 * device to be removed from the PCI subsystem properly.
8438 **/
8439 static void __devexit
8440 lpfc_pci_remove_one_s3(struct pci_dev *pdev)
8441 {
8442 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8443 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
8444 struct lpfc_vport **vports;
8445 struct lpfc_hba *phba = vport->phba;
8446 int i;
8447 int bars = pci_select_bars(pdev, IORESOURCE_MEM);
8448
8449 spin_lock_irq(&phba->hbalock);
8450 vport->load_flag |= FC_UNLOADING;
8451 spin_unlock_irq(&phba->hbalock);
8452
8453 lpfc_free_sysfs_attr(vport);
8454
8455 /* Release all the vports against this physical port */
8456 vports = lpfc_create_vport_work_array(phba);
8457 if (vports != NULL)
8458 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
8459 fc_vport_terminate(vports[i]->fc_vport);
8460 lpfc_destroy_vport_work_array(phba, vports);
8461
8462 /* Remove FC host and then SCSI host with the physical port */
8463 fc_remove_host(shost);
8464 scsi_remove_host(shost);
8465 lpfc_cleanup(vport);
8466
8467 /*
8468 * Bring down the SLI Layer. This step disable all interrupts,
8469 * clears the rings, discards all mailbox commands, and resets
8470 * the HBA.
8471 */
8472
8473 /* HBA interrupt will be disabled after this call */
8474 lpfc_sli_hba_down(phba);
8475 /* Stop kthread signal shall trigger work_done one more time */
8476 kthread_stop(phba->worker_thread);
8477 /* Final cleanup of txcmplq and reset the HBA */
8478 lpfc_sli_brdrestart(phba);
8479
8480 lpfc_stop_hba_timers(phba);
8481 spin_lock_irq(&phba->hbalock);
8482 list_del_init(&vport->listentry);
8483 spin_unlock_irq(&phba->hbalock);
8484
8485 lpfc_debugfs_terminate(vport);
8486
8487 /* Disable SR-IOV if enabled */
8488 if (phba->cfg_sriov_nr_virtfn)
8489 pci_disable_sriov(pdev);
8490
8491 /* Disable interrupt */
8492 lpfc_sli_disable_intr(phba);
8493
8494 pci_set_drvdata(pdev, NULL);
8495 scsi_host_put(shost);
8496
8497 /*
8498 * Call scsi_free before mem_free since scsi bufs are released to their
8499 * corresponding pools here.
8500 */
8501 lpfc_scsi_free(phba);
8502 lpfc_mem_free_all(phba);
8503
8504 dma_free_coherent(&pdev->dev, lpfc_sli_hbq_size(),
8505 phba->hbqslimp.virt, phba->hbqslimp.phys);
8506
8507 /* Free resources associated with SLI2 interface */
8508 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
8509 phba->slim2p.virt, phba->slim2p.phys);
8510
8511 /* unmap adapter SLIM and Control Registers */
8512 iounmap(phba->ctrl_regs_memmap_p);
8513 iounmap(phba->slim_memmap_p);
8514
8515 lpfc_hba_free(phba);
8516
8517 pci_release_selected_regions(pdev, bars);
8518 pci_disable_device(pdev);
8519 }
8520
8521 /**
8522 * lpfc_pci_suspend_one_s3 - PCI func to suspend SLI-3 device for power mgmnt
8523 * @pdev: pointer to PCI device
8524 * @msg: power management message
8525 *
8526 * This routine is to be called from the kernel's PCI subsystem to support
8527 * system Power Management (PM) to device with SLI-3 interface spec. When
8528 * PM invokes this method, it quiesces the device by stopping the driver's
8529 * worker thread for the device, turning off device's interrupt and DMA,
8530 * and bring the device offline. Note that as the driver implements the
8531 * minimum PM requirements to a power-aware driver's PM support for the
8532 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
8533 * to the suspend() method call will be treated as SUSPEND and the driver will
8534 * fully reinitialize its device during resume() method call, the driver will
8535 * set device to PCI_D3hot state in PCI config space instead of setting it
8536 * according to the @msg provided by the PM.
8537 *
8538 * Return code
8539 * 0 - driver suspended the device
8540 * Error otherwise
8541 **/
8542 static int
8543 lpfc_pci_suspend_one_s3(struct pci_dev *pdev, pm_message_t msg)
8544 {
8545 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8546 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8547
8548 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8549 "0473 PCI device Power Management suspend.\n");
8550
8551 /* Bring down the device */
8552 lpfc_offline_prep(phba);
8553 lpfc_offline(phba);
8554 kthread_stop(phba->worker_thread);
8555
8556 /* Disable interrupt from device */
8557 lpfc_sli_disable_intr(phba);
8558
8559 /* Save device state to PCI config space */
8560 pci_save_state(pdev);
8561 pci_set_power_state(pdev, PCI_D3hot);
8562
8563 return 0;
8564 }
8565
8566 /**
8567 * lpfc_pci_resume_one_s3 - PCI func to resume SLI-3 device for power mgmnt
8568 * @pdev: pointer to PCI device
8569 *
8570 * This routine is to be called from the kernel's PCI subsystem to support
8571 * system Power Management (PM) to device with SLI-3 interface spec. When PM
8572 * invokes this method, it restores the device's PCI config space state and
8573 * fully reinitializes the device and brings it online. Note that as the
8574 * driver implements the minimum PM requirements to a power-aware driver's
8575 * PM for suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE,
8576 * FREEZE) to the suspend() method call will be treated as SUSPEND and the
8577 * driver will fully reinitialize its device during resume() method call,
8578 * the device will be set to PCI_D0 directly in PCI config space before
8579 * restoring the state.
8580 *
8581 * Return code
8582 * 0 - driver suspended the device
8583 * Error otherwise
8584 **/
8585 static int
8586 lpfc_pci_resume_one_s3(struct pci_dev *pdev)
8587 {
8588 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8589 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8590 uint32_t intr_mode;
8591 int error;
8592
8593 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
8594 "0452 PCI device Power Management resume.\n");
8595
8596 /* Restore device state from PCI config space */
8597 pci_set_power_state(pdev, PCI_D0);
8598 pci_restore_state(pdev);
8599
8600 /*
8601 * As the new kernel behavior of pci_restore_state() API call clears
8602 * device saved_state flag, need to save the restored state again.
8603 */
8604 pci_save_state(pdev);
8605
8606 if (pdev->is_busmaster)
8607 pci_set_master(pdev);
8608
8609 /* Startup the kernel thread for this host adapter. */
8610 phba->worker_thread = kthread_run(lpfc_do_work, phba,
8611 "lpfc_worker_%d", phba->brd_no);
8612 if (IS_ERR(phba->worker_thread)) {
8613 error = PTR_ERR(phba->worker_thread);
8614 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8615 "0434 PM resume failed to start worker "
8616 "thread: error=x%x.\n", error);
8617 return error;
8618 }
8619
8620 /* Configure and enable interrupt */
8621 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
8622 if (intr_mode == LPFC_INTR_ERROR) {
8623 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8624 "0430 PM resume Failed to enable interrupt\n");
8625 return -EIO;
8626 } else
8627 phba->intr_mode = intr_mode;
8628
8629 /* Restart HBA and bring it online */
8630 lpfc_sli_brdrestart(phba);
8631 lpfc_online(phba);
8632
8633 /* Log the current active interrupt mode */
8634 lpfc_log_intr_mode(phba, phba->intr_mode);
8635
8636 return 0;
8637 }
8638
8639 /**
8640 * lpfc_sli_prep_dev_for_recover - Prepare SLI3 device for pci slot recover
8641 * @phba: pointer to lpfc hba data structure.
8642 *
8643 * This routine is called to prepare the SLI3 device for PCI slot recover. It
8644 * aborts all the outstanding SCSI I/Os to the pci device.
8645 **/
8646 static void
8647 lpfc_sli_prep_dev_for_recover(struct lpfc_hba *phba)
8648 {
8649 struct lpfc_sli *psli = &phba->sli;
8650 struct lpfc_sli_ring *pring;
8651
8652 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8653 "2723 PCI channel I/O abort preparing for recovery\n");
8654
8655 /*
8656 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
8657 * and let the SCSI mid-layer to retry them to recover.
8658 */
8659 pring = &psli->ring[psli->fcp_ring];
8660 lpfc_sli_abort_iocb_ring(phba, pring);
8661 }
8662
8663 /**
8664 * lpfc_sli_prep_dev_for_reset - Prepare SLI3 device for pci slot reset
8665 * @phba: pointer to lpfc hba data structure.
8666 *
8667 * This routine is called to prepare the SLI3 device for PCI slot reset. It
8668 * disables the device interrupt and pci device, and aborts the internal FCP
8669 * pending I/Os.
8670 **/
8671 static void
8672 lpfc_sli_prep_dev_for_reset(struct lpfc_hba *phba)
8673 {
8674 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8675 "2710 PCI channel disable preparing for reset\n");
8676
8677 /* Block any management I/Os to the device */
8678 lpfc_block_mgmt_io(phba);
8679
8680 /* Block all SCSI devices' I/Os on the host */
8681 lpfc_scsi_dev_block(phba);
8682
8683 /* stop all timers */
8684 lpfc_stop_hba_timers(phba);
8685
8686 /* Disable interrupt and pci device */
8687 lpfc_sli_disable_intr(phba);
8688 pci_disable_device(phba->pcidev);
8689
8690 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
8691 lpfc_sli_flush_fcp_rings(phba);
8692 }
8693
8694 /**
8695 * lpfc_sli_prep_dev_for_perm_failure - Prepare SLI3 dev for pci slot disable
8696 * @phba: pointer to lpfc hba data structure.
8697 *
8698 * This routine is called to prepare the SLI3 device for PCI slot permanently
8699 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
8700 * pending I/Os.
8701 **/
8702 static void
8703 lpfc_sli_prep_dev_for_perm_failure(struct lpfc_hba *phba)
8704 {
8705 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8706 "2711 PCI channel permanent disable for failure\n");
8707 /* Block all SCSI devices' I/Os on the host */
8708 lpfc_scsi_dev_block(phba);
8709
8710 /* stop all timers */
8711 lpfc_stop_hba_timers(phba);
8712
8713 /* Clean up all driver's outstanding SCSI I/Os */
8714 lpfc_sli_flush_fcp_rings(phba);
8715 }
8716
8717 /**
8718 * lpfc_io_error_detected_s3 - Method for handling SLI-3 device PCI I/O error
8719 * @pdev: pointer to PCI device.
8720 * @state: the current PCI connection state.
8721 *
8722 * This routine is called from the PCI subsystem for I/O error handling to
8723 * device with SLI-3 interface spec. This function is called by the PCI
8724 * subsystem after a PCI bus error affecting this device has been detected.
8725 * When this function is invoked, it will need to stop all the I/Os and
8726 * interrupt(s) to the device. Once that is done, it will return
8727 * PCI_ERS_RESULT_NEED_RESET for the PCI subsystem to perform proper recovery
8728 * as desired.
8729 *
8730 * Return codes
8731 * PCI_ERS_RESULT_CAN_RECOVER - can be recovered with reset_link
8732 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
8733 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8734 **/
8735 static pci_ers_result_t
8736 lpfc_io_error_detected_s3(struct pci_dev *pdev, pci_channel_state_t state)
8737 {
8738 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8739 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8740
8741 switch (state) {
8742 case pci_channel_io_normal:
8743 /* Non-fatal error, prepare for recovery */
8744 lpfc_sli_prep_dev_for_recover(phba);
8745 return PCI_ERS_RESULT_CAN_RECOVER;
8746 case pci_channel_io_frozen:
8747 /* Fatal error, prepare for slot reset */
8748 lpfc_sli_prep_dev_for_reset(phba);
8749 return PCI_ERS_RESULT_NEED_RESET;
8750 case pci_channel_io_perm_failure:
8751 /* Permanent failure, prepare for device down */
8752 lpfc_sli_prep_dev_for_perm_failure(phba);
8753 return PCI_ERS_RESULT_DISCONNECT;
8754 default:
8755 /* Unknown state, prepare and request slot reset */
8756 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8757 "0472 Unknown PCI error state: x%x\n", state);
8758 lpfc_sli_prep_dev_for_reset(phba);
8759 return PCI_ERS_RESULT_NEED_RESET;
8760 }
8761 }
8762
8763 /**
8764 * lpfc_io_slot_reset_s3 - Method for restarting PCI SLI-3 device from scratch.
8765 * @pdev: pointer to PCI device.
8766 *
8767 * This routine is called from the PCI subsystem for error handling to
8768 * device with SLI-3 interface spec. This is called after PCI bus has been
8769 * reset to restart the PCI card from scratch, as if from a cold-boot.
8770 * During the PCI subsystem error recovery, after driver returns
8771 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
8772 * recovery and then call this routine before calling the .resume method
8773 * to recover the device. This function will initialize the HBA device,
8774 * enable the interrupt, but it will just put the HBA to offline state
8775 * without passing any I/O traffic.
8776 *
8777 * Return codes
8778 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
8779 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
8780 */
8781 static pci_ers_result_t
8782 lpfc_io_slot_reset_s3(struct pci_dev *pdev)
8783 {
8784 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8785 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8786 struct lpfc_sli *psli = &phba->sli;
8787 uint32_t intr_mode;
8788
8789 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
8790 if (pci_enable_device_mem(pdev)) {
8791 printk(KERN_ERR "lpfc: Cannot re-enable "
8792 "PCI device after reset.\n");
8793 return PCI_ERS_RESULT_DISCONNECT;
8794 }
8795
8796 pci_restore_state(pdev);
8797
8798 /*
8799 * As the new kernel behavior of pci_restore_state() API call clears
8800 * device saved_state flag, need to save the restored state again.
8801 */
8802 pci_save_state(pdev);
8803
8804 if (pdev->is_busmaster)
8805 pci_set_master(pdev);
8806
8807 spin_lock_irq(&phba->hbalock);
8808 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
8809 spin_unlock_irq(&phba->hbalock);
8810
8811 /* Configure and enable interrupt */
8812 intr_mode = lpfc_sli_enable_intr(phba, phba->intr_mode);
8813 if (intr_mode == LPFC_INTR_ERROR) {
8814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8815 "0427 Cannot re-enable interrupt after "
8816 "slot reset.\n");
8817 return PCI_ERS_RESULT_DISCONNECT;
8818 } else
8819 phba->intr_mode = intr_mode;
8820
8821 /* Take device offline, it will perform cleanup */
8822 lpfc_offline_prep(phba);
8823 lpfc_offline(phba);
8824 lpfc_sli_brdrestart(phba);
8825
8826 /* Log the current active interrupt mode */
8827 lpfc_log_intr_mode(phba, phba->intr_mode);
8828
8829 return PCI_ERS_RESULT_RECOVERED;
8830 }
8831
8832 /**
8833 * lpfc_io_resume_s3 - Method for resuming PCI I/O operation on SLI-3 device.
8834 * @pdev: pointer to PCI device
8835 *
8836 * This routine is called from the PCI subsystem for error handling to device
8837 * with SLI-3 interface spec. It is called when kernel error recovery tells
8838 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
8839 * error recovery. After this call, traffic can start to flow from this device
8840 * again.
8841 */
8842 static void
8843 lpfc_io_resume_s3(struct pci_dev *pdev)
8844 {
8845 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8846 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
8847
8848 /* Bring device online, it will be no-op for non-fatal error resume */
8849 lpfc_online(phba);
8850
8851 /* Clean up Advanced Error Reporting (AER) if needed */
8852 if (phba->hba_flag & HBA_AER_ENABLED)
8853 pci_cleanup_aer_uncorrect_error_status(pdev);
8854 }
8855
8856 /**
8857 * lpfc_sli4_get_els_iocb_cnt - Calculate the # of ELS IOCBs to reserve
8858 * @phba: pointer to lpfc hba data structure.
8859 *
8860 * returns the number of ELS/CT IOCBs to reserve
8861 **/
8862 int
8863 lpfc_sli4_get_els_iocb_cnt(struct lpfc_hba *phba)
8864 {
8865 int max_xri = phba->sli4_hba.max_cfg_param.max_xri;
8866
8867 if (phba->sli_rev == LPFC_SLI_REV4) {
8868 if (max_xri <= 100)
8869 return 10;
8870 else if (max_xri <= 256)
8871 return 25;
8872 else if (max_xri <= 512)
8873 return 50;
8874 else if (max_xri <= 1024)
8875 return 100;
8876 else
8877 return 150;
8878 } else
8879 return 0;
8880 }
8881
8882 /**
8883 * lpfc_write_firmware - attempt to write a firmware image to the port
8884 * @phba: pointer to lpfc hba data structure.
8885 * @fw: pointer to firmware image returned from request_firmware.
8886 *
8887 * returns the number of bytes written if write is successful.
8888 * returns a negative error value if there were errors.
8889 * returns 0 if firmware matches currently active firmware on port.
8890 **/
8891 int
8892 lpfc_write_firmware(struct lpfc_hba *phba, const struct firmware *fw)
8893 {
8894 char fwrev[32];
8895 struct lpfc_grp_hdr *image = (struct lpfc_grp_hdr *)fw->data;
8896 struct list_head dma_buffer_list;
8897 int i, rc = 0;
8898 struct lpfc_dmabuf *dmabuf, *next;
8899 uint32_t offset = 0, temp_offset = 0;
8900
8901 INIT_LIST_HEAD(&dma_buffer_list);
8902 if ((be32_to_cpu(image->magic_number) != LPFC_GROUP_OJECT_MAGIC_NUM) ||
8903 (bf_get_be32(lpfc_grp_hdr_file_type, image) !=
8904 LPFC_FILE_TYPE_GROUP) ||
8905 (bf_get_be32(lpfc_grp_hdr_id, image) != LPFC_FILE_ID_GROUP) ||
8906 (be32_to_cpu(image->size) != fw->size)) {
8907 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8908 "3022 Invalid FW image found. "
8909 "Magic:%x Type:%x ID:%x\n",
8910 be32_to_cpu(image->magic_number),
8911 bf_get_be32(lpfc_grp_hdr_file_type, image),
8912 bf_get_be32(lpfc_grp_hdr_id, image));
8913 return -EINVAL;
8914 }
8915 lpfc_decode_firmware_rev(phba, fwrev, 1);
8916 if (strncmp(fwrev, image->revision, strnlen(image->revision, 16))) {
8917 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8918 "3023 Updating Firmware. Current Version:%s "
8919 "New Version:%s\n",
8920 fwrev, image->revision);
8921 for (i = 0; i < LPFC_MBX_WR_CONFIG_MAX_BDE; i++) {
8922 dmabuf = kzalloc(sizeof(struct lpfc_dmabuf),
8923 GFP_KERNEL);
8924 if (!dmabuf) {
8925 rc = -ENOMEM;
8926 goto out;
8927 }
8928 dmabuf->virt = dma_alloc_coherent(&phba->pcidev->dev,
8929 SLI4_PAGE_SIZE,
8930 &dmabuf->phys,
8931 GFP_KERNEL);
8932 if (!dmabuf->virt) {
8933 kfree(dmabuf);
8934 rc = -ENOMEM;
8935 goto out;
8936 }
8937 list_add_tail(&dmabuf->list, &dma_buffer_list);
8938 }
8939 while (offset < fw->size) {
8940 temp_offset = offset;
8941 list_for_each_entry(dmabuf, &dma_buffer_list, list) {
8942 if (temp_offset + SLI4_PAGE_SIZE > fw->size) {
8943 memcpy(dmabuf->virt,
8944 fw->data + temp_offset,
8945 fw->size - temp_offset);
8946 temp_offset = fw->size;
8947 break;
8948 }
8949 memcpy(dmabuf->virt, fw->data + temp_offset,
8950 SLI4_PAGE_SIZE);
8951 temp_offset += SLI4_PAGE_SIZE;
8952 }
8953 rc = lpfc_wr_object(phba, &dma_buffer_list,
8954 (fw->size - offset), &offset);
8955 if (rc) {
8956 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
8957 "3024 Firmware update failed. "
8958 "%d\n", rc);
8959 goto out;
8960 }
8961 }
8962 rc = offset;
8963 }
8964 out:
8965 list_for_each_entry_safe(dmabuf, next, &dma_buffer_list, list) {
8966 list_del(&dmabuf->list);
8967 dma_free_coherent(&phba->pcidev->dev, SLI4_PAGE_SIZE,
8968 dmabuf->virt, dmabuf->phys);
8969 kfree(dmabuf);
8970 }
8971 return rc;
8972 }
8973
8974 /**
8975 * lpfc_pci_probe_one_s4 - PCI probe func to reg SLI-4 device to PCI subsys
8976 * @pdev: pointer to PCI device
8977 * @pid: pointer to PCI device identifier
8978 *
8979 * This routine is called from the kernel's PCI subsystem to device with
8980 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
8981 * presented on PCI bus, the kernel PCI subsystem looks at PCI device-specific
8982 * information of the device and driver to see if the driver state that it
8983 * can support this kind of device. If the match is successful, the driver
8984 * core invokes this routine. If this routine determines it can claim the HBA,
8985 * it does all the initialization that it needs to do to handle the HBA
8986 * properly.
8987 *
8988 * Return code
8989 * 0 - driver can claim the device
8990 * negative value - driver can not claim the device
8991 **/
8992 static int __devinit
8993 lpfc_pci_probe_one_s4(struct pci_dev *pdev, const struct pci_device_id *pid)
8994 {
8995 struct lpfc_hba *phba;
8996 struct lpfc_vport *vport = NULL;
8997 struct Scsi_Host *shost = NULL;
8998 int error;
8999 uint32_t cfg_mode, intr_mode;
9000 int mcnt;
9001 int adjusted_fcp_eq_count;
9002 int fcp_qidx;
9003 const struct firmware *fw;
9004 uint8_t file_name[16];
9005
9006 /* Allocate memory for HBA structure */
9007 phba = lpfc_hba_alloc(pdev);
9008 if (!phba)
9009 return -ENOMEM;
9010
9011 /* Perform generic PCI device enabling operation */
9012 error = lpfc_enable_pci_dev(phba);
9013 if (error)
9014 goto out_free_phba;
9015
9016 /* Set up SLI API function jump table for PCI-device group-1 HBAs */
9017 error = lpfc_api_table_setup(phba, LPFC_PCI_DEV_OC);
9018 if (error)
9019 goto out_disable_pci_dev;
9020
9021 /* Set up SLI-4 specific device PCI memory space */
9022 error = lpfc_sli4_pci_mem_setup(phba);
9023 if (error) {
9024 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9025 "1410 Failed to set up pci memory space.\n");
9026 goto out_disable_pci_dev;
9027 }
9028
9029 /* Set up phase-1 common device driver resources */
9030 error = lpfc_setup_driver_resource_phase1(phba);
9031 if (error) {
9032 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9033 "1411 Failed to set up driver resource.\n");
9034 goto out_unset_pci_mem_s4;
9035 }
9036
9037 /* Set up SLI-4 Specific device driver resources */
9038 error = lpfc_sli4_driver_resource_setup(phba);
9039 if (error) {
9040 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9041 "1412 Failed to set up driver resource.\n");
9042 goto out_unset_pci_mem_s4;
9043 }
9044
9045 /* Initialize and populate the iocb list per host */
9046
9047 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9048 "2821 initialize iocb list %d.\n",
9049 phba->cfg_iocb_cnt*1024);
9050 error = lpfc_init_iocb_list(phba, phba->cfg_iocb_cnt*1024);
9051
9052 if (error) {
9053 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9054 "1413 Failed to initialize iocb list.\n");
9055 goto out_unset_driver_resource_s4;
9056 }
9057
9058 INIT_LIST_HEAD(&phba->active_rrq_list);
9059 INIT_LIST_HEAD(&phba->fcf.fcf_pri_list);
9060
9061 /* Set up common device driver resources */
9062 error = lpfc_setup_driver_resource_phase2(phba);
9063 if (error) {
9064 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9065 "1414 Failed to set up driver resource.\n");
9066 goto out_free_iocb_list;
9067 }
9068
9069 /* Get the default values for Model Name and Description */
9070 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
9071
9072 /* Create SCSI host to the physical port */
9073 error = lpfc_create_shost(phba);
9074 if (error) {
9075 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9076 "1415 Failed to create scsi host.\n");
9077 goto out_unset_driver_resource;
9078 }
9079
9080 /* Configure sysfs attributes */
9081 vport = phba->pport;
9082 error = lpfc_alloc_sysfs_attr(vport);
9083 if (error) {
9084 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9085 "1416 Failed to allocate sysfs attr\n");
9086 goto out_destroy_shost;
9087 }
9088
9089 shost = lpfc_shost_from_vport(vport); /* save shost for error cleanup */
9090 /* Now, trying to enable interrupt and bring up the device */
9091 cfg_mode = phba->cfg_use_msi;
9092 while (true) {
9093 /* Put device to a known state before enabling interrupt */
9094 lpfc_stop_port(phba);
9095 /* Configure and enable interrupt */
9096 intr_mode = lpfc_sli4_enable_intr(phba, cfg_mode);
9097 if (intr_mode == LPFC_INTR_ERROR) {
9098 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9099 "0426 Failed to enable interrupt.\n");
9100 error = -ENODEV;
9101 goto out_free_sysfs_attr;
9102 }
9103 /* Default to single EQ for non-MSI-X */
9104 if (phba->intr_type != MSIX)
9105 adjusted_fcp_eq_count = 0;
9106 else if (phba->sli4_hba.msix_vec_nr <
9107 phba->cfg_fcp_eq_count + 1)
9108 adjusted_fcp_eq_count = phba->sli4_hba.msix_vec_nr - 1;
9109 else
9110 adjusted_fcp_eq_count = phba->cfg_fcp_eq_count;
9111 /* Free unused EQs */
9112 for (fcp_qidx = adjusted_fcp_eq_count;
9113 fcp_qidx < phba->cfg_fcp_eq_count;
9114 fcp_qidx++) {
9115 lpfc_sli4_queue_free(phba->sli4_hba.fp_eq[fcp_qidx]);
9116 /* do not delete the first fcp_cq */
9117 if (fcp_qidx)
9118 lpfc_sli4_queue_free(
9119 phba->sli4_hba.fcp_cq[fcp_qidx]);
9120 }
9121 phba->cfg_fcp_eq_count = adjusted_fcp_eq_count;
9122 /* Set up SLI-4 HBA */
9123 if (lpfc_sli4_hba_setup(phba)) {
9124 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9125 "1421 Failed to set up hba\n");
9126 error = -ENODEV;
9127 goto out_disable_intr;
9128 }
9129
9130 /* Send NOP mbx cmds for non-INTx mode active interrupt test */
9131 if (intr_mode != 0)
9132 mcnt = lpfc_sli4_send_nop_mbox_cmds(phba,
9133 LPFC_ACT_INTR_CNT);
9134
9135 /* Check active interrupts received only for MSI/MSI-X */
9136 if (intr_mode == 0 ||
9137 phba->sli.slistat.sli_intr >= LPFC_ACT_INTR_CNT) {
9138 /* Log the current active interrupt mode */
9139 phba->intr_mode = intr_mode;
9140 lpfc_log_intr_mode(phba, intr_mode);
9141 break;
9142 }
9143 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9144 "0451 Configure interrupt mode (%d) "
9145 "failed active interrupt test.\n",
9146 intr_mode);
9147 /* Unset the previous SLI-4 HBA setup. */
9148 /*
9149 * TODO: Is this operation compatible with IF TYPE 2
9150 * devices? All port state is deleted and cleared.
9151 */
9152 lpfc_sli4_unset_hba(phba);
9153 /* Try next level of interrupt mode */
9154 cfg_mode = --intr_mode;
9155 }
9156
9157 /* Perform post initialization setup */
9158 lpfc_post_init_setup(phba);
9159
9160 /* check for firmware upgrade or downgrade */
9161 snprintf(file_name, 16, "%s.grp", phba->ModelName);
9162 error = request_firmware(&fw, file_name, &phba->pcidev->dev);
9163 if (!error) {
9164 lpfc_write_firmware(phba, fw);
9165 release_firmware(fw);
9166 }
9167
9168 /* Check if there are static vports to be created. */
9169 lpfc_create_static_vport(phba);
9170 return 0;
9171
9172 out_disable_intr:
9173 lpfc_sli4_disable_intr(phba);
9174 out_free_sysfs_attr:
9175 lpfc_free_sysfs_attr(vport);
9176 out_destroy_shost:
9177 lpfc_destroy_shost(phba);
9178 out_unset_driver_resource:
9179 lpfc_unset_driver_resource_phase2(phba);
9180 out_free_iocb_list:
9181 lpfc_free_iocb_list(phba);
9182 out_unset_driver_resource_s4:
9183 lpfc_sli4_driver_resource_unset(phba);
9184 out_unset_pci_mem_s4:
9185 lpfc_sli4_pci_mem_unset(phba);
9186 out_disable_pci_dev:
9187 lpfc_disable_pci_dev(phba);
9188 if (shost)
9189 scsi_host_put(shost);
9190 out_free_phba:
9191 lpfc_hba_free(phba);
9192 return error;
9193 }
9194
9195 /**
9196 * lpfc_pci_remove_one_s4 - PCI func to unreg SLI-4 device from PCI subsystem
9197 * @pdev: pointer to PCI device
9198 *
9199 * This routine is called from the kernel's PCI subsystem to device with
9200 * SLI-4 interface spec. When an Emulex HBA with SLI-4 interface spec is
9201 * removed from PCI bus, it performs all the necessary cleanup for the HBA
9202 * device to be removed from the PCI subsystem properly.
9203 **/
9204 static void __devexit
9205 lpfc_pci_remove_one_s4(struct pci_dev *pdev)
9206 {
9207 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9208 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
9209 struct lpfc_vport **vports;
9210 struct lpfc_hba *phba = vport->phba;
9211 int i;
9212
9213 /* Mark the device unloading flag */
9214 spin_lock_irq(&phba->hbalock);
9215 vport->load_flag |= FC_UNLOADING;
9216 spin_unlock_irq(&phba->hbalock);
9217
9218 /* Free the HBA sysfs attributes */
9219 lpfc_free_sysfs_attr(vport);
9220
9221 /* Release all the vports against this physical port */
9222 vports = lpfc_create_vport_work_array(phba);
9223 if (vports != NULL)
9224 for (i = 1; i <= phba->max_vports && vports[i] != NULL; i++)
9225 fc_vport_terminate(vports[i]->fc_vport);
9226 lpfc_destroy_vport_work_array(phba, vports);
9227
9228 /* Remove FC host and then SCSI host with the physical port */
9229 fc_remove_host(shost);
9230 scsi_remove_host(shost);
9231
9232 /* Perform cleanup on the physical port */
9233 lpfc_cleanup(vport);
9234
9235 /*
9236 * Bring down the SLI Layer. This step disables all interrupts,
9237 * clears the rings, discards all mailbox commands, and resets
9238 * the HBA FCoE function.
9239 */
9240 lpfc_debugfs_terminate(vport);
9241 lpfc_sli4_hba_unset(phba);
9242
9243 spin_lock_irq(&phba->hbalock);
9244 list_del_init(&vport->listentry);
9245 spin_unlock_irq(&phba->hbalock);
9246
9247 /* Perform scsi free before driver resource_unset since scsi
9248 * buffers are released to their corresponding pools here.
9249 */
9250 lpfc_scsi_free(phba);
9251 lpfc_sli4_driver_resource_unset(phba);
9252
9253 /* Unmap adapter Control and Doorbell registers */
9254 lpfc_sli4_pci_mem_unset(phba);
9255
9256 /* Release PCI resources and disable device's PCI function */
9257 scsi_host_put(shost);
9258 lpfc_disable_pci_dev(phba);
9259
9260 /* Finally, free the driver's device data structure */
9261 lpfc_hba_free(phba);
9262
9263 return;
9264 }
9265
9266 /**
9267 * lpfc_pci_suspend_one_s4 - PCI func to suspend SLI-4 device for power mgmnt
9268 * @pdev: pointer to PCI device
9269 * @msg: power management message
9270 *
9271 * This routine is called from the kernel's PCI subsystem to support system
9272 * Power Management (PM) to device with SLI-4 interface spec. When PM invokes
9273 * this method, it quiesces the device by stopping the driver's worker
9274 * thread for the device, turning off device's interrupt and DMA, and bring
9275 * the device offline. Note that as the driver implements the minimum PM
9276 * requirements to a power-aware driver's PM support for suspend/resume -- all
9277 * the possible PM messages (SUSPEND, HIBERNATE, FREEZE) to the suspend()
9278 * method call will be treated as SUSPEND and the driver will fully
9279 * reinitialize its device during resume() method call, the driver will set
9280 * device to PCI_D3hot state in PCI config space instead of setting it
9281 * according to the @msg provided by the PM.
9282 *
9283 * Return code
9284 * 0 - driver suspended the device
9285 * Error otherwise
9286 **/
9287 static int
9288 lpfc_pci_suspend_one_s4(struct pci_dev *pdev, pm_message_t msg)
9289 {
9290 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9291 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9292
9293 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9294 "2843 PCI device Power Management suspend.\n");
9295
9296 /* Bring down the device */
9297 lpfc_offline_prep(phba);
9298 lpfc_offline(phba);
9299 kthread_stop(phba->worker_thread);
9300
9301 /* Disable interrupt from device */
9302 lpfc_sli4_disable_intr(phba);
9303
9304 /* Save device state to PCI config space */
9305 pci_save_state(pdev);
9306 pci_set_power_state(pdev, PCI_D3hot);
9307
9308 return 0;
9309 }
9310
9311 /**
9312 * lpfc_pci_resume_one_s4 - PCI func to resume SLI-4 device for power mgmnt
9313 * @pdev: pointer to PCI device
9314 *
9315 * This routine is called from the kernel's PCI subsystem to support system
9316 * Power Management (PM) to device with SLI-4 interface spac. When PM invokes
9317 * this method, it restores the device's PCI config space state and fully
9318 * reinitializes the device and brings it online. Note that as the driver
9319 * implements the minimum PM requirements to a power-aware driver's PM for
9320 * suspend/resume -- all the possible PM messages (SUSPEND, HIBERNATE, FREEZE)
9321 * to the suspend() method call will be treated as SUSPEND and the driver
9322 * will fully reinitialize its device during resume() method call, the device
9323 * will be set to PCI_D0 directly in PCI config space before restoring the
9324 * state.
9325 *
9326 * Return code
9327 * 0 - driver suspended the device
9328 * Error otherwise
9329 **/
9330 static int
9331 lpfc_pci_resume_one_s4(struct pci_dev *pdev)
9332 {
9333 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9334 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9335 uint32_t intr_mode;
9336 int error;
9337
9338 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
9339 "0292 PCI device Power Management resume.\n");
9340
9341 /* Restore device state from PCI config space */
9342 pci_set_power_state(pdev, PCI_D0);
9343 pci_restore_state(pdev);
9344
9345 /*
9346 * As the new kernel behavior of pci_restore_state() API call clears
9347 * device saved_state flag, need to save the restored state again.
9348 */
9349 pci_save_state(pdev);
9350
9351 if (pdev->is_busmaster)
9352 pci_set_master(pdev);
9353
9354 /* Startup the kernel thread for this host adapter. */
9355 phba->worker_thread = kthread_run(lpfc_do_work, phba,
9356 "lpfc_worker_%d", phba->brd_no);
9357 if (IS_ERR(phba->worker_thread)) {
9358 error = PTR_ERR(phba->worker_thread);
9359 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9360 "0293 PM resume failed to start worker "
9361 "thread: error=x%x.\n", error);
9362 return error;
9363 }
9364
9365 /* Configure and enable interrupt */
9366 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9367 if (intr_mode == LPFC_INTR_ERROR) {
9368 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9369 "0294 PM resume Failed to enable interrupt\n");
9370 return -EIO;
9371 } else
9372 phba->intr_mode = intr_mode;
9373
9374 /* Restart HBA and bring it online */
9375 lpfc_sli_brdrestart(phba);
9376 lpfc_online(phba);
9377
9378 /* Log the current active interrupt mode */
9379 lpfc_log_intr_mode(phba, phba->intr_mode);
9380
9381 return 0;
9382 }
9383
9384 /**
9385 * lpfc_sli4_prep_dev_for_recover - Prepare SLI4 device for pci slot recover
9386 * @phba: pointer to lpfc hba data structure.
9387 *
9388 * This routine is called to prepare the SLI4 device for PCI slot recover. It
9389 * aborts all the outstanding SCSI I/Os to the pci device.
9390 **/
9391 static void
9392 lpfc_sli4_prep_dev_for_recover(struct lpfc_hba *phba)
9393 {
9394 struct lpfc_sli *psli = &phba->sli;
9395 struct lpfc_sli_ring *pring;
9396
9397 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9398 "2828 PCI channel I/O abort preparing for recovery\n");
9399 /*
9400 * There may be errored I/Os through HBA, abort all I/Os on txcmplq
9401 * and let the SCSI mid-layer to retry them to recover.
9402 */
9403 pring = &psli->ring[psli->fcp_ring];
9404 lpfc_sli_abort_iocb_ring(phba, pring);
9405 }
9406
9407 /**
9408 * lpfc_sli4_prep_dev_for_reset - Prepare SLI4 device for pci slot reset
9409 * @phba: pointer to lpfc hba data structure.
9410 *
9411 * This routine is called to prepare the SLI4 device for PCI slot reset. It
9412 * disables the device interrupt and pci device, and aborts the internal FCP
9413 * pending I/Os.
9414 **/
9415 static void
9416 lpfc_sli4_prep_dev_for_reset(struct lpfc_hba *phba)
9417 {
9418 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9419 "2826 PCI channel disable preparing for reset\n");
9420
9421 /* Block any management I/Os to the device */
9422 lpfc_block_mgmt_io(phba);
9423
9424 /* Block all SCSI devices' I/Os on the host */
9425 lpfc_scsi_dev_block(phba);
9426
9427 /* stop all timers */
9428 lpfc_stop_hba_timers(phba);
9429
9430 /* Disable interrupt and pci device */
9431 lpfc_sli4_disable_intr(phba);
9432 pci_disable_device(phba->pcidev);
9433
9434 /* Flush all driver's outstanding SCSI I/Os as we are to reset */
9435 lpfc_sli_flush_fcp_rings(phba);
9436 }
9437
9438 /**
9439 * lpfc_sli4_prep_dev_for_perm_failure - Prepare SLI4 dev for pci slot disable
9440 * @phba: pointer to lpfc hba data structure.
9441 *
9442 * This routine is called to prepare the SLI4 device for PCI slot permanently
9443 * disabling. It blocks the SCSI transport layer traffic and flushes the FCP
9444 * pending I/Os.
9445 **/
9446 static void
9447 lpfc_sli4_prep_dev_for_perm_failure(struct lpfc_hba *phba)
9448 {
9449 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9450 "2827 PCI channel permanent disable for failure\n");
9451
9452 /* Block all SCSI devices' I/Os on the host */
9453 lpfc_scsi_dev_block(phba);
9454
9455 /* stop all timers */
9456 lpfc_stop_hba_timers(phba);
9457
9458 /* Clean up all driver's outstanding SCSI I/Os */
9459 lpfc_sli_flush_fcp_rings(phba);
9460 }
9461
9462 /**
9463 * lpfc_io_error_detected_s4 - Method for handling PCI I/O error to SLI-4 device
9464 * @pdev: pointer to PCI device.
9465 * @state: the current PCI connection state.
9466 *
9467 * This routine is called from the PCI subsystem for error handling to device
9468 * with SLI-4 interface spec. This function is called by the PCI subsystem
9469 * after a PCI bus error affecting this device has been detected. When this
9470 * function is invoked, it will need to stop all the I/Os and interrupt(s)
9471 * to the device. Once that is done, it will return PCI_ERS_RESULT_NEED_RESET
9472 * for the PCI subsystem to perform proper recovery as desired.
9473 *
9474 * Return codes
9475 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9476 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9477 **/
9478 static pci_ers_result_t
9479 lpfc_io_error_detected_s4(struct pci_dev *pdev, pci_channel_state_t state)
9480 {
9481 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9482 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9483
9484 switch (state) {
9485 case pci_channel_io_normal:
9486 /* Non-fatal error, prepare for recovery */
9487 lpfc_sli4_prep_dev_for_recover(phba);
9488 return PCI_ERS_RESULT_CAN_RECOVER;
9489 case pci_channel_io_frozen:
9490 /* Fatal error, prepare for slot reset */
9491 lpfc_sli4_prep_dev_for_reset(phba);
9492 return PCI_ERS_RESULT_NEED_RESET;
9493 case pci_channel_io_perm_failure:
9494 /* Permanent failure, prepare for device down */
9495 lpfc_sli4_prep_dev_for_perm_failure(phba);
9496 return PCI_ERS_RESULT_DISCONNECT;
9497 default:
9498 /* Unknown state, prepare and request slot reset */
9499 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9500 "2825 Unknown PCI error state: x%x\n", state);
9501 lpfc_sli4_prep_dev_for_reset(phba);
9502 return PCI_ERS_RESULT_NEED_RESET;
9503 }
9504 }
9505
9506 /**
9507 * lpfc_io_slot_reset_s4 - Method for restart PCI SLI-4 device from scratch
9508 * @pdev: pointer to PCI device.
9509 *
9510 * This routine is called from the PCI subsystem for error handling to device
9511 * with SLI-4 interface spec. It is called after PCI bus has been reset to
9512 * restart the PCI card from scratch, as if from a cold-boot. During the
9513 * PCI subsystem error recovery, after the driver returns
9514 * PCI_ERS_RESULT_NEED_RESET, the PCI subsystem will perform proper error
9515 * recovery and then call this routine before calling the .resume method to
9516 * recover the device. This function will initialize the HBA device, enable
9517 * the interrupt, but it will just put the HBA to offline state without
9518 * passing any I/O traffic.
9519 *
9520 * Return codes
9521 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9522 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9523 */
9524 static pci_ers_result_t
9525 lpfc_io_slot_reset_s4(struct pci_dev *pdev)
9526 {
9527 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9528 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9529 struct lpfc_sli *psli = &phba->sli;
9530 uint32_t intr_mode;
9531
9532 dev_printk(KERN_INFO, &pdev->dev, "recovering from a slot reset.\n");
9533 if (pci_enable_device_mem(pdev)) {
9534 printk(KERN_ERR "lpfc: Cannot re-enable "
9535 "PCI device after reset.\n");
9536 return PCI_ERS_RESULT_DISCONNECT;
9537 }
9538
9539 pci_restore_state(pdev);
9540
9541 /*
9542 * As the new kernel behavior of pci_restore_state() API call clears
9543 * device saved_state flag, need to save the restored state again.
9544 */
9545 pci_save_state(pdev);
9546
9547 if (pdev->is_busmaster)
9548 pci_set_master(pdev);
9549
9550 spin_lock_irq(&phba->hbalock);
9551 psli->sli_flag &= ~LPFC_SLI_ACTIVE;
9552 spin_unlock_irq(&phba->hbalock);
9553
9554 /* Configure and enable interrupt */
9555 intr_mode = lpfc_sli4_enable_intr(phba, phba->intr_mode);
9556 if (intr_mode == LPFC_INTR_ERROR) {
9557 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9558 "2824 Cannot re-enable interrupt after "
9559 "slot reset.\n");
9560 return PCI_ERS_RESULT_DISCONNECT;
9561 } else
9562 phba->intr_mode = intr_mode;
9563
9564 /* Log the current active interrupt mode */
9565 lpfc_log_intr_mode(phba, phba->intr_mode);
9566
9567 return PCI_ERS_RESULT_RECOVERED;
9568 }
9569
9570 /**
9571 * lpfc_io_resume_s4 - Method for resuming PCI I/O operation to SLI-4 device
9572 * @pdev: pointer to PCI device
9573 *
9574 * This routine is called from the PCI subsystem for error handling to device
9575 * with SLI-4 interface spec. It is called when kernel error recovery tells
9576 * the lpfc driver that it is ok to resume normal PCI operation after PCI bus
9577 * error recovery. After this call, traffic can start to flow from this device
9578 * again.
9579 **/
9580 static void
9581 lpfc_io_resume_s4(struct pci_dev *pdev)
9582 {
9583 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9584 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9585
9586 /*
9587 * In case of slot reset, as function reset is performed through
9588 * mailbox command which needs DMA to be enabled, this operation
9589 * has to be moved to the io resume phase. Taking device offline
9590 * will perform the necessary cleanup.
9591 */
9592 if (!(phba->sli.sli_flag & LPFC_SLI_ACTIVE)) {
9593 /* Perform device reset */
9594 lpfc_offline_prep(phba);
9595 lpfc_offline(phba);
9596 lpfc_sli_brdrestart(phba);
9597 /* Bring the device back online */
9598 lpfc_online(phba);
9599 }
9600
9601 /* Clean up Advanced Error Reporting (AER) if needed */
9602 if (phba->hba_flag & HBA_AER_ENABLED)
9603 pci_cleanup_aer_uncorrect_error_status(pdev);
9604 }
9605
9606 /**
9607 * lpfc_pci_probe_one - lpfc PCI probe func to reg dev to PCI subsystem
9608 * @pdev: pointer to PCI device
9609 * @pid: pointer to PCI device identifier
9610 *
9611 * This routine is to be registered to the kernel's PCI subsystem. When an
9612 * Emulex HBA device is presented on PCI bus, the kernel PCI subsystem looks
9613 * at PCI device-specific information of the device and driver to see if the
9614 * driver state that it can support this kind of device. If the match is
9615 * successful, the driver core invokes this routine. This routine dispatches
9616 * the action to the proper SLI-3 or SLI-4 device probing routine, which will
9617 * do all the initialization that it needs to do to handle the HBA device
9618 * properly.
9619 *
9620 * Return code
9621 * 0 - driver can claim the device
9622 * negative value - driver can not claim the device
9623 **/
9624 static int __devinit
9625 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
9626 {
9627 int rc;
9628 struct lpfc_sli_intf intf;
9629
9630 if (pci_read_config_dword(pdev, LPFC_SLI_INTF, &intf.word0))
9631 return -ENODEV;
9632
9633 if ((bf_get(lpfc_sli_intf_valid, &intf) == LPFC_SLI_INTF_VALID) &&
9634 (bf_get(lpfc_sli_intf_slirev, &intf) == LPFC_SLI_INTF_REV_SLI4))
9635 rc = lpfc_pci_probe_one_s4(pdev, pid);
9636 else
9637 rc = lpfc_pci_probe_one_s3(pdev, pid);
9638
9639 return rc;
9640 }
9641
9642 /**
9643 * lpfc_pci_remove_one - lpfc PCI func to unreg dev from PCI subsystem
9644 * @pdev: pointer to PCI device
9645 *
9646 * This routine is to be registered to the kernel's PCI subsystem. When an
9647 * Emulex HBA is removed from PCI bus, the driver core invokes this routine.
9648 * This routine dispatches the action to the proper SLI-3 or SLI-4 device
9649 * remove routine, which will perform all the necessary cleanup for the
9650 * device to be removed from the PCI subsystem properly.
9651 **/
9652 static void __devexit
9653 lpfc_pci_remove_one(struct pci_dev *pdev)
9654 {
9655 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9656 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9657
9658 switch (phba->pci_dev_grp) {
9659 case LPFC_PCI_DEV_LP:
9660 lpfc_pci_remove_one_s3(pdev);
9661 break;
9662 case LPFC_PCI_DEV_OC:
9663 lpfc_pci_remove_one_s4(pdev);
9664 break;
9665 default:
9666 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9667 "1424 Invalid PCI device group: 0x%x\n",
9668 phba->pci_dev_grp);
9669 break;
9670 }
9671 return;
9672 }
9673
9674 /**
9675 * lpfc_pci_suspend_one - lpfc PCI func to suspend dev for power management
9676 * @pdev: pointer to PCI device
9677 * @msg: power management message
9678 *
9679 * This routine is to be registered to the kernel's PCI subsystem to support
9680 * system Power Management (PM). When PM invokes this method, it dispatches
9681 * the action to the proper SLI-3 or SLI-4 device suspend routine, which will
9682 * suspend the device.
9683 *
9684 * Return code
9685 * 0 - driver suspended the device
9686 * Error otherwise
9687 **/
9688 static int
9689 lpfc_pci_suspend_one(struct pci_dev *pdev, pm_message_t msg)
9690 {
9691 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9692 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9693 int rc = -ENODEV;
9694
9695 switch (phba->pci_dev_grp) {
9696 case LPFC_PCI_DEV_LP:
9697 rc = lpfc_pci_suspend_one_s3(pdev, msg);
9698 break;
9699 case LPFC_PCI_DEV_OC:
9700 rc = lpfc_pci_suspend_one_s4(pdev, msg);
9701 break;
9702 default:
9703 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9704 "1425 Invalid PCI device group: 0x%x\n",
9705 phba->pci_dev_grp);
9706 break;
9707 }
9708 return rc;
9709 }
9710
9711 /**
9712 * lpfc_pci_resume_one - lpfc PCI func to resume dev for power management
9713 * @pdev: pointer to PCI device
9714 *
9715 * This routine is to be registered to the kernel's PCI subsystem to support
9716 * system Power Management (PM). When PM invokes this method, it dispatches
9717 * the action to the proper SLI-3 or SLI-4 device resume routine, which will
9718 * resume the device.
9719 *
9720 * Return code
9721 * 0 - driver suspended the device
9722 * Error otherwise
9723 **/
9724 static int
9725 lpfc_pci_resume_one(struct pci_dev *pdev)
9726 {
9727 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9728 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9729 int rc = -ENODEV;
9730
9731 switch (phba->pci_dev_grp) {
9732 case LPFC_PCI_DEV_LP:
9733 rc = lpfc_pci_resume_one_s3(pdev);
9734 break;
9735 case LPFC_PCI_DEV_OC:
9736 rc = lpfc_pci_resume_one_s4(pdev);
9737 break;
9738 default:
9739 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9740 "1426 Invalid PCI device group: 0x%x\n",
9741 phba->pci_dev_grp);
9742 break;
9743 }
9744 return rc;
9745 }
9746
9747 /**
9748 * lpfc_io_error_detected - lpfc method for handling PCI I/O error
9749 * @pdev: pointer to PCI device.
9750 * @state: the current PCI connection state.
9751 *
9752 * This routine is registered to the PCI subsystem for error handling. This
9753 * function is called by the PCI subsystem after a PCI bus error affecting
9754 * this device has been detected. When this routine is invoked, it dispatches
9755 * the action to the proper SLI-3 or SLI-4 device error detected handling
9756 * routine, which will perform the proper error detected operation.
9757 *
9758 * Return codes
9759 * PCI_ERS_RESULT_NEED_RESET - need to reset before recovery
9760 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9761 **/
9762 static pci_ers_result_t
9763 lpfc_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
9764 {
9765 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9766 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9767 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9768
9769 switch (phba->pci_dev_grp) {
9770 case LPFC_PCI_DEV_LP:
9771 rc = lpfc_io_error_detected_s3(pdev, state);
9772 break;
9773 case LPFC_PCI_DEV_OC:
9774 rc = lpfc_io_error_detected_s4(pdev, state);
9775 break;
9776 default:
9777 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9778 "1427 Invalid PCI device group: 0x%x\n",
9779 phba->pci_dev_grp);
9780 break;
9781 }
9782 return rc;
9783 }
9784
9785 /**
9786 * lpfc_io_slot_reset - lpfc method for restart PCI dev from scratch
9787 * @pdev: pointer to PCI device.
9788 *
9789 * This routine is registered to the PCI subsystem for error handling. This
9790 * function is called after PCI bus has been reset to restart the PCI card
9791 * from scratch, as if from a cold-boot. When this routine is invoked, it
9792 * dispatches the action to the proper SLI-3 or SLI-4 device reset handling
9793 * routine, which will perform the proper device reset.
9794 *
9795 * Return codes
9796 * PCI_ERS_RESULT_RECOVERED - the device has been recovered
9797 * PCI_ERS_RESULT_DISCONNECT - device could not be recovered
9798 **/
9799 static pci_ers_result_t
9800 lpfc_io_slot_reset(struct pci_dev *pdev)
9801 {
9802 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9803 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9804 pci_ers_result_t rc = PCI_ERS_RESULT_DISCONNECT;
9805
9806 switch (phba->pci_dev_grp) {
9807 case LPFC_PCI_DEV_LP:
9808 rc = lpfc_io_slot_reset_s3(pdev);
9809 break;
9810 case LPFC_PCI_DEV_OC:
9811 rc = lpfc_io_slot_reset_s4(pdev);
9812 break;
9813 default:
9814 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9815 "1428 Invalid PCI device group: 0x%x\n",
9816 phba->pci_dev_grp);
9817 break;
9818 }
9819 return rc;
9820 }
9821
9822 /**
9823 * lpfc_io_resume - lpfc method for resuming PCI I/O operation
9824 * @pdev: pointer to PCI device
9825 *
9826 * This routine is registered to the PCI subsystem for error handling. It
9827 * is called when kernel error recovery tells the lpfc driver that it is
9828 * OK to resume normal PCI operation after PCI bus error recovery. When
9829 * this routine is invoked, it dispatches the action to the proper SLI-3
9830 * or SLI-4 device io_resume routine, which will resume the device operation.
9831 **/
9832 static void
9833 lpfc_io_resume(struct pci_dev *pdev)
9834 {
9835 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9836 struct lpfc_hba *phba = ((struct lpfc_vport *)shost->hostdata)->phba;
9837
9838 switch (phba->pci_dev_grp) {
9839 case LPFC_PCI_DEV_LP:
9840 lpfc_io_resume_s3(pdev);
9841 break;
9842 case LPFC_PCI_DEV_OC:
9843 lpfc_io_resume_s4(pdev);
9844 break;
9845 default:
9846 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
9847 "1429 Invalid PCI device group: 0x%x\n",
9848 phba->pci_dev_grp);
9849 break;
9850 }
9851 return;
9852 }
9853
9854 static struct pci_device_id lpfc_id_table[] = {
9855 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
9856 PCI_ANY_ID, PCI_ANY_ID, },
9857 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
9858 PCI_ANY_ID, PCI_ANY_ID, },
9859 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
9860 PCI_ANY_ID, PCI_ANY_ID, },
9861 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
9862 PCI_ANY_ID, PCI_ANY_ID, },
9863 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
9864 PCI_ANY_ID, PCI_ANY_ID, },
9865 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
9866 PCI_ANY_ID, PCI_ANY_ID, },
9867 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
9868 PCI_ANY_ID, PCI_ANY_ID, },
9869 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
9870 PCI_ANY_ID, PCI_ANY_ID, },
9871 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
9872 PCI_ANY_ID, PCI_ANY_ID, },
9873 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
9874 PCI_ANY_ID, PCI_ANY_ID, },
9875 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
9876 PCI_ANY_ID, PCI_ANY_ID, },
9877 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
9878 PCI_ANY_ID, PCI_ANY_ID, },
9879 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
9880 PCI_ANY_ID, PCI_ANY_ID, },
9881 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
9882 PCI_ANY_ID, PCI_ANY_ID, },
9883 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
9884 PCI_ANY_ID, PCI_ANY_ID, },
9885 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
9886 PCI_ANY_ID, PCI_ANY_ID, },
9887 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
9888 PCI_ANY_ID, PCI_ANY_ID, },
9889 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
9890 PCI_ANY_ID, PCI_ANY_ID, },
9891 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HORNET,
9892 PCI_ANY_ID, PCI_ANY_ID, },
9893 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
9894 PCI_ANY_ID, PCI_ANY_ID, },
9895 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
9896 PCI_ANY_ID, PCI_ANY_ID, },
9897 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
9898 PCI_ANY_ID, PCI_ANY_ID, },
9899 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
9900 PCI_ANY_ID, PCI_ANY_ID, },
9901 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
9902 PCI_ANY_ID, PCI_ANY_ID, },
9903 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
9904 PCI_ANY_ID, PCI_ANY_ID, },
9905 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
9906 PCI_ANY_ID, PCI_ANY_ID, },
9907 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
9908 PCI_ANY_ID, PCI_ANY_ID, },
9909 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
9910 PCI_ANY_ID, PCI_ANY_ID, },
9911 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT,
9912 PCI_ANY_ID, PCI_ANY_ID, },
9913 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_MID,
9914 PCI_ANY_ID, PCI_ANY_ID, },
9915 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SMB,
9916 PCI_ANY_ID, PCI_ANY_ID, },
9917 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_DCSP,
9918 PCI_ANY_ID, PCI_ANY_ID, },
9919 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_SCSP,
9920 PCI_ANY_ID, PCI_ANY_ID, },
9921 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SAT_S,
9922 PCI_ANY_ID, PCI_ANY_ID, },
9923 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_VF,
9924 PCI_ANY_ID, PCI_ANY_ID, },
9925 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_PF,
9926 PCI_ANY_ID, PCI_ANY_ID, },
9927 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PROTEUS_S,
9928 PCI_ANY_ID, PCI_ANY_ID, },
9929 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TIGERSHARK,
9930 PCI_ANY_ID, PCI_ANY_ID, },
9931 {PCI_VENDOR_ID_SERVERENGINE, PCI_DEVICE_ID_TOMCAT,
9932 PCI_ANY_ID, PCI_ANY_ID, },
9933 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FALCON,
9934 PCI_ANY_ID, PCI_ANY_ID, },
9935 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BALIUS,
9936 PCI_ANY_ID, PCI_ANY_ID, },
9937 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC,
9938 PCI_ANY_ID, PCI_ANY_ID, },
9939 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE,
9940 PCI_ANY_ID, PCI_ANY_ID, },
9941 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FC_VF,
9942 PCI_ANY_ID, PCI_ANY_ID, },
9943 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LANCER_FCOE_VF,
9944 PCI_ANY_ID, PCI_ANY_ID, },
9945 { 0 }
9946 };
9947
9948 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
9949
9950 static struct pci_error_handlers lpfc_err_handler = {
9951 .error_detected = lpfc_io_error_detected,
9952 .slot_reset = lpfc_io_slot_reset,
9953 .resume = lpfc_io_resume,
9954 };
9955
9956 static struct pci_driver lpfc_driver = {
9957 .name = LPFC_DRIVER_NAME,
9958 .id_table = lpfc_id_table,
9959 .probe = lpfc_pci_probe_one,
9960 .remove = __devexit_p(lpfc_pci_remove_one),
9961 .suspend = lpfc_pci_suspend_one,
9962 .resume = lpfc_pci_resume_one,
9963 .err_handler = &lpfc_err_handler,
9964 };
9965
9966 /**
9967 * lpfc_init - lpfc module initialization routine
9968 *
9969 * This routine is to be invoked when the lpfc module is loaded into the
9970 * kernel. The special kernel macro module_init() is used to indicate the
9971 * role of this routine to the kernel as lpfc module entry point.
9972 *
9973 * Return codes
9974 * 0 - successful
9975 * -ENOMEM - FC attach transport failed
9976 * all others - failed
9977 */
9978 static int __init
9979 lpfc_init(void)
9980 {
9981 int error = 0;
9982
9983 printk(LPFC_MODULE_DESC "\n");
9984 printk(LPFC_COPYRIGHT "\n");
9985
9986 if (lpfc_enable_npiv) {
9987 lpfc_transport_functions.vport_create = lpfc_vport_create;
9988 lpfc_transport_functions.vport_delete = lpfc_vport_delete;
9989 }
9990 lpfc_transport_template =
9991 fc_attach_transport(&lpfc_transport_functions);
9992 if (lpfc_transport_template == NULL)
9993 return -ENOMEM;
9994 if (lpfc_enable_npiv) {
9995 lpfc_vport_transport_template =
9996 fc_attach_transport(&lpfc_vport_transport_functions);
9997 if (lpfc_vport_transport_template == NULL) {
9998 fc_release_transport(lpfc_transport_template);
9999 return -ENOMEM;
10000 }
10001 }
10002 error = pci_register_driver(&lpfc_driver);
10003 if (error) {
10004 fc_release_transport(lpfc_transport_template);
10005 if (lpfc_enable_npiv)
10006 fc_release_transport(lpfc_vport_transport_template);
10007 }
10008
10009 return error;
10010 }
10011
10012 /**
10013 * lpfc_exit - lpfc module removal routine
10014 *
10015 * This routine is invoked when the lpfc module is removed from the kernel.
10016 * The special kernel macro module_exit() is used to indicate the role of
10017 * this routine to the kernel as lpfc module exit point.
10018 */
10019 static void __exit
10020 lpfc_exit(void)
10021 {
10022 pci_unregister_driver(&lpfc_driver);
10023 fc_release_transport(lpfc_transport_template);
10024 if (lpfc_enable_npiv)
10025 fc_release_transport(lpfc_vport_transport_template);
10026 if (_dump_buf_data) {
10027 printk(KERN_ERR "9062 BLKGRD: freeing %lu pages for "
10028 "_dump_buf_data at 0x%p\n",
10029 (1L << _dump_buf_data_order), _dump_buf_data);
10030 free_pages((unsigned long)_dump_buf_data, _dump_buf_data_order);
10031 }
10032
10033 if (_dump_buf_dif) {
10034 printk(KERN_ERR "9049 BLKGRD: freeing %lu pages for "
10035 "_dump_buf_dif at 0x%p\n",
10036 (1L << _dump_buf_dif_order), _dump_buf_dif);
10037 free_pages((unsigned long)_dump_buf_dif, _dump_buf_dif_order);
10038 }
10039 }
10040
10041 module_init(lpfc_init);
10042 module_exit(lpfc_exit);
10043 MODULE_LICENSE("GPL");
10044 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
10045 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
10046 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);
This page took 0.243921 seconds and 6 git commands to generate.