blob: cf9081c262d7a92d6950cf88efd9c6feab896f64 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
Jamie Wellnitz41415862006-02-28 19:25:27 -05004 * Copyright (C) 2004-2006 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#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
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -040031#include <scsi/scsi.h>
dea31012005-04-17 16:05:31 -050032#include <scsi/scsi_device.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi_transport_fc.h>
35
36#include "lpfc_hw.h"
37#include "lpfc_sli.h"
38#include "lpfc_disc.h"
39#include "lpfc_scsi.h"
40#include "lpfc.h"
41#include "lpfc_logmsg.h"
42#include "lpfc_crtn.h"
43#include "lpfc_version.h"
44
Jamie Wellnitz74b72a52006-02-28 22:33:04 -050045static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *, int);
dea31012005-04-17 16:05:31 -050046static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
47static int lpfc_post_rcv_buf(struct lpfc_hba *);
48
49static struct scsi_transport_template *lpfc_transport_template = NULL;
50static DEFINE_IDR(lpfc_hba_index);
51
52/************************************************************************/
53/* */
54/* lpfc_config_port_prep */
55/* This routine will do LPFC initialization prior to the */
56/* CONFIG_PORT mailbox command. This will be initialized */
57/* as a SLI layer callback routine. */
58/* This routine returns 0 on success or -ERESTART if it wants */
59/* the SLI layer to reset the HBA and try again. Any */
60/* other return value indicates an error. */
61/* */
62/************************************************************************/
63int
64lpfc_config_port_prep(struct lpfc_hba * phba)
65{
66 lpfc_vpd_t *vp = &phba->vpd;
67 int i = 0, rc;
68 LPFC_MBOXQ_t *pmb;
69 MAILBOX_t *mb;
70 char *lpfc_vpd_data = NULL;
71 uint16_t offset = 0;
72 static char licensed[56] =
73 "key unlock for use with gnu public licensed code only\0";
James Smart65a29c12006-07-06 15:50:50 -040074 static int init_key = 1;
dea31012005-04-17 16:05:31 -050075
76 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
77 if (!pmb) {
78 phba->hba_state = LPFC_HBA_ERROR;
79 return -ENOMEM;
80 }
81
82 mb = &pmb->mb;
83 phba->hba_state = LPFC_INIT_MBX_CMDS;
84
85 if (lpfc_is_LC_HBA(phba->pcidev->device)) {
James Smart65a29c12006-07-06 15:50:50 -040086 if (init_key) {
87 uint32_t *ptext = (uint32_t *) licensed;
dea31012005-04-17 16:05:31 -050088
James Smart65a29c12006-07-06 15:50:50 -040089 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
90 *ptext = cpu_to_be32(*ptext);
91 init_key = 0;
92 }
dea31012005-04-17 16:05:31 -050093
94 lpfc_read_nv(phba, pmb);
95 memset((char*)mb->un.varRDnvp.rsvd3, 0,
96 sizeof (mb->un.varRDnvp.rsvd3));
97 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
98 sizeof (licensed));
99
100 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
101
102 if (rc != MBX_SUCCESS) {
103 lpfc_printf_log(phba,
104 KERN_ERR,
105 LOG_MBOX,
106 "%d:0324 Config Port initialization "
107 "error, mbxCmd x%x READ_NVPARM, "
108 "mbxStatus x%x\n",
109 phba->brd_no,
110 mb->mbxCommand, mb->mbxStatus);
111 mempool_free(pmb, phba->mbox_mem_pool);
112 return -ERESTART;
113 }
114 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
115 sizeof (mb->un.varRDnvp.nodename));
116 }
117
118 /* Setup and issue mailbox READ REV command */
119 lpfc_read_rev(phba, pmb);
120 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
121 if (rc != MBX_SUCCESS) {
122 lpfc_printf_log(phba,
123 KERN_ERR,
124 LOG_INIT,
125 "%d:0439 Adapter failed to init, mbxCmd x%x "
126 "READ_REV, mbxStatus x%x\n",
127 phba->brd_no,
128 mb->mbxCommand, mb->mbxStatus);
129 mempool_free( pmb, phba->mbox_mem_pool);
130 return -ERESTART;
131 }
132
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500133 /*
134 * The value of rr must be 1 since the driver set the cv field to 1.
135 * This setting requires the FW to set all revision fields.
dea31012005-04-17 16:05:31 -0500136 */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500137 if (mb->un.varRdRev.rr == 0) {
dea31012005-04-17 16:05:31 -0500138 vp->rev.rBit = 0;
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500139 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
140 "%d:0440 Adapter failed to init, READ_REV has "
141 "missing revision information.\n",
142 phba->brd_no);
dea31012005-04-17 16:05:31 -0500143 mempool_free(pmb, phba->mbox_mem_pool);
144 return -ERESTART;
dea31012005-04-17 16:05:31 -0500145 }
146
147 /* Save information as VPD data */
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500148 vp->rev.rBit = 1;
149 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
150 memcpy(vp->rev.sli1FwName, (char*) mb->un.varRdRev.sli1FwName, 16);
151 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
152 memcpy(vp->rev.sli2FwName, (char *) mb->un.varRdRev.sli2FwName, 16);
dea31012005-04-17 16:05:31 -0500153 vp->rev.biuRev = mb->un.varRdRev.biuRev;
154 vp->rev.smRev = mb->un.varRdRev.smRev;
155 vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
156 vp->rev.endecRev = mb->un.varRdRev.endecRev;
157 vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
158 vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
159 vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
160 vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
161 vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
162 vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
163
164 if (lpfc_is_LC_HBA(phba->pcidev->device))
165 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
166 sizeof (phba->RandomData));
167
dea31012005-04-17 16:05:31 -0500168 /* Get adapter VPD information */
169 pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
170 if (!pmb->context2)
171 goto out_free_mbox;
172 lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
173 if (!lpfc_vpd_data)
174 goto out_free_context2;
175
176 do {
177 lpfc_dump_mem(phba, pmb, offset);
178 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
179
180 if (rc != MBX_SUCCESS) {
181 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
182 "%d:0441 VPD not present on adapter, "
183 "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
184 phba->brd_no,
185 mb->mbxCommand, mb->mbxStatus);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500186 mb->un.varDmp.word_cnt = 0;
dea31012005-04-17 16:05:31 -0500187 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500188 if (mb->un.varDmp.word_cnt > DMP_VPD_SIZE - offset)
189 mb->un.varDmp.word_cnt = DMP_VPD_SIZE - offset;
dea31012005-04-17 16:05:31 -0500190 lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
191 mb->un.varDmp.word_cnt);
192 offset += mb->un.varDmp.word_cnt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500193 } while (mb->un.varDmp.word_cnt && offset < DMP_VPD_SIZE);
194 lpfc_parse_vpd(phba, lpfc_vpd_data, offset);
dea31012005-04-17 16:05:31 -0500195
196 kfree(lpfc_vpd_data);
197out_free_context2:
198 kfree(pmb->context2);
199out_free_mbox:
200 mempool_free(pmb, phba->mbox_mem_pool);
201 return 0;
202}
203
204/************************************************************************/
205/* */
206/* lpfc_config_port_post */
207/* This routine will do LPFC initialization after the */
208/* CONFIG_PORT mailbox command. This will be initialized */
209/* as a SLI layer callback routine. */
210/* This routine returns 0 on success. Any other return value */
211/* indicates an error. */
212/* */
213/************************************************************************/
214int
215lpfc_config_port_post(struct lpfc_hba * phba)
216{
217 LPFC_MBOXQ_t *pmb;
218 MAILBOX_t *mb;
219 struct lpfc_dmabuf *mp;
220 struct lpfc_sli *psli = &phba->sli;
221 uint32_t status, timeout;
222 int i, j, rc;
223
224 pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
225 if (!pmb) {
226 phba->hba_state = LPFC_HBA_ERROR;
227 return -ENOMEM;
228 }
229 mb = &pmb->mb;
230
231 lpfc_config_link(phba, pmb);
232 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
233 if (rc != MBX_SUCCESS) {
234 lpfc_printf_log(phba,
235 KERN_ERR,
236 LOG_INIT,
237 "%d:0447 Adapter failed init, mbxCmd x%x "
238 "CONFIG_LINK mbxStatus x%x\n",
239 phba->brd_no,
240 mb->mbxCommand, mb->mbxStatus);
241 phba->hba_state = LPFC_HBA_ERROR;
242 mempool_free( pmb, phba->mbox_mem_pool);
243 return -EIO;
244 }
245
246 /* Get login parameters for NID. */
247 lpfc_read_sparam(phba, pmb);
248 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
249 lpfc_printf_log(phba,
250 KERN_ERR,
251 LOG_INIT,
252 "%d:0448 Adapter failed init, mbxCmd x%x "
253 "READ_SPARM mbxStatus x%x\n",
254 phba->brd_no,
255 mb->mbxCommand, mb->mbxStatus);
256 phba->hba_state = LPFC_HBA_ERROR;
257 mp = (struct lpfc_dmabuf *) pmb->context1;
258 mempool_free( pmb, phba->mbox_mem_pool);
259 lpfc_mbuf_free(phba, mp->virt, mp->phys);
260 kfree(mp);
261 return -EIO;
262 }
263
264 mp = (struct lpfc_dmabuf *) pmb->context1;
265
266 memcpy(&phba->fc_sparam, mp->virt, sizeof (struct serv_parm));
267 lpfc_mbuf_free(phba, mp->virt, mp->phys);
268 kfree(mp);
269 pmb->context1 = NULL;
270
James Smarta12e07b2006-12-02 13:35:30 -0500271 if (phba->cfg_soft_wwnn)
272 u64_to_wwn(phba->cfg_soft_wwnn, phba->fc_sparam.nodeName.u.wwn);
James Smartc3f28af2006-08-18 17:47:18 -0400273 if (phba->cfg_soft_wwpn)
274 u64_to_wwn(phba->cfg_soft_wwpn, phba->fc_sparam.portName.u.wwn);
dea31012005-04-17 16:05:31 -0500275 memcpy(&phba->fc_nodename, &phba->fc_sparam.nodeName,
276 sizeof (struct lpfc_name));
277 memcpy(&phba->fc_portname, &phba->fc_sparam.portName,
278 sizeof (struct lpfc_name));
279 /* If no serial number in VPD data, use low 6 bytes of WWNN */
280 /* This should be consolidated into parse_vpd ? - mr */
281 if (phba->SerialNumber[0] == 0) {
282 uint8_t *outptr;
283
Andrew Morton68ce1eb2005-09-21 09:46:54 -0700284 outptr = &phba->fc_nodename.u.s.IEEE[0];
dea31012005-04-17 16:05:31 -0500285 for (i = 0; i < 12; i++) {
286 status = *outptr++;
287 j = ((status & 0xf0) >> 4);
288 if (j <= 9)
289 phba->SerialNumber[i] =
290 (char)((uint8_t) 0x30 + (uint8_t) j);
291 else
292 phba->SerialNumber[i] =
293 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
294 i++;
295 j = (status & 0xf);
296 if (j <= 9)
297 phba->SerialNumber[i] =
298 (char)((uint8_t) 0x30 + (uint8_t) j);
299 else
300 phba->SerialNumber[i] =
301 (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
302 }
303 }
304
dea31012005-04-17 16:05:31 -0500305 lpfc_read_config(phba, pmb);
306 if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
307 lpfc_printf_log(phba,
308 KERN_ERR,
309 LOG_INIT,
310 "%d:0453 Adapter failed to init, mbxCmd x%x "
311 "READ_CONFIG, mbxStatus x%x\n",
312 phba->brd_no,
313 mb->mbxCommand, mb->mbxStatus);
314 phba->hba_state = LPFC_HBA_ERROR;
315 mempool_free( pmb, phba->mbox_mem_pool);
316 return -EIO;
317 }
318
319 /* Reset the DFT_HBA_Q_DEPTH to the max xri */
320 if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
321 phba->cfg_hba_queue_depth =
322 mb->un.varRdConfig.max_xri + 1;
323
324 phba->lmt = mb->un.varRdConfig.lmt;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500325
326 /* Get the default values for Model Name and Description */
327 lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
328
329 if ((phba->cfg_link_speed > LINK_SPEED_10G)
330 || ((phba->cfg_link_speed == LINK_SPEED_1G)
331 && !(phba->lmt & LMT_1Gb))
332 || ((phba->cfg_link_speed == LINK_SPEED_2G)
333 && !(phba->lmt & LMT_2Gb))
334 || ((phba->cfg_link_speed == LINK_SPEED_4G)
335 && !(phba->lmt & LMT_4Gb))
336 || ((phba->cfg_link_speed == LINK_SPEED_8G)
337 && !(phba->lmt & LMT_8Gb))
338 || ((phba->cfg_link_speed == LINK_SPEED_10G)
339 && !(phba->lmt & LMT_10Gb))) {
340 /* Reset link speed to auto */
dea31012005-04-17 16:05:31 -0500341 lpfc_printf_log(phba,
342 KERN_WARNING,
343 LOG_LINK_EVENT,
344 "%d:1302 Invalid speed for this board: "
345 "Reset link speed to auto: x%x\n",
346 phba->brd_no,
347 phba->cfg_link_speed);
348 phba->cfg_link_speed = LINK_SPEED_AUTO;
349 }
350
351 phba->hba_state = LPFC_LINK_DOWN;
352
353 /* Only process IOCBs on ring 0 till hba_state is READY */
James Smarta4bc3372006-12-02 13:34:16 -0500354 if (psli->ring[psli->extra_ring].cmdringaddr)
355 psli->ring[psli->extra_ring].flag |= LPFC_STOP_IOCB_EVENT;
dea31012005-04-17 16:05:31 -0500356 if (psli->ring[psli->fcp_ring].cmdringaddr)
357 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
358 if (psli->ring[psli->next_ring].cmdringaddr)
359 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
360
361 /* Post receive buffers for desired rings */
362 lpfc_post_rcv_buf(phba);
363
364 /* Enable appropriate host interrupts */
365 spin_lock_irq(phba->host->host_lock);
366 status = readl(phba->HCregaddr);
367 status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
368 if (psli->num_rings > 0)
369 status |= HC_R0INT_ENA;
370 if (psli->num_rings > 1)
371 status |= HC_R1INT_ENA;
372 if (psli->num_rings > 2)
373 status |= HC_R2INT_ENA;
374 if (psli->num_rings > 3)
375 status |= HC_R3INT_ENA;
376
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500377 if ((phba->cfg_poll & ENABLE_FCP_RING_POLLING) &&
378 (phba->cfg_poll & DISABLE_FCP_RING_INT))
379 status &= ~(HC_R0INT_ENA << LPFC_FCP_RING);
380
dea31012005-04-17 16:05:31 -0500381 writel(status, phba->HCregaddr);
382 readl(phba->HCregaddr); /* flush */
383 spin_unlock_irq(phba->host->host_lock);
384
385 /*
386 * Setup the ring 0 (els) timeout handler
387 */
388 timeout = phba->fc_ratov << 1;
389 phba->els_tmofunc.expires = jiffies + HZ * timeout;
390 add_timer(&phba->els_tmofunc);
391
392 lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
393 pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
James Smart8aee9182006-08-31 12:27:57 -0400394 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT);
395 if (rc != MBX_SUCCESS) {
dea31012005-04-17 16:05:31 -0500396 lpfc_printf_log(phba,
397 KERN_ERR,
398 LOG_INIT,
399 "%d:0454 Adapter failed to init, mbxCmd x%x "
400 "INIT_LINK, mbxStatus x%x\n",
401 phba->brd_no,
402 mb->mbxCommand, mb->mbxStatus);
403
404 /* Clear all interrupt enable conditions */
405 writel(0, phba->HCregaddr);
406 readl(phba->HCregaddr); /* flush */
407 /* Clear all pending interrupts */
408 writel(0xffffffff, phba->HAregaddr);
409 readl(phba->HAregaddr); /* flush */
410
411 phba->hba_state = LPFC_HBA_ERROR;
James Smart8aee9182006-08-31 12:27:57 -0400412 if (rc != MBX_BUSY)
413 mempool_free(pmb, phba->mbox_mem_pool);
dea31012005-04-17 16:05:31 -0500414 return -EIO;
415 }
416 /* MBOX buffer will be freed in mbox compl */
417
James Smartce8b3ce2006-07-06 15:50:36 -0400418 return (0);
419}
420
421static int
422lpfc_discovery_wait(struct lpfc_hba *phba)
423{
424 int i = 0;
425
dea31012005-04-17 16:05:31 -0500426 while ((phba->hba_state != LPFC_HBA_READY) ||
427 (phba->num_disc_nodes) || (phba->fc_prli_sent) ||
428 ((phba->fc_map_cnt == 0) && (i<2)) ||
James Smartce8b3ce2006-07-06 15:50:36 -0400429 (phba->sli.sli_flag & LPFC_SLI_MBOX_ACTIVE)) {
dea31012005-04-17 16:05:31 -0500430 /* Check every second for 30 retries. */
431 i++;
432 if (i > 30) {
James Smartce8b3ce2006-07-06 15:50:36 -0400433 return -ETIMEDOUT;
dea31012005-04-17 16:05:31 -0500434 }
435 if ((i >= 15) && (phba->hba_state <= LPFC_LINK_DOWN)) {
436 /* The link is down. Set linkdown timeout */
James Smartce8b3ce2006-07-06 15:50:36 -0400437 return -ETIMEDOUT;
dea31012005-04-17 16:05:31 -0500438 }
439
440 /* Delay for 1 second to give discovery time to complete. */
441 msleep(1000);
442
443 }
444
James Smartce8b3ce2006-07-06 15:50:36 -0400445 return 0;
dea31012005-04-17 16:05:31 -0500446}
447
448/************************************************************************/
449/* */
450/* lpfc_hba_down_prep */
451/* This routine will do LPFC uninitialization before the */
452/* HBA is reset when bringing down the SLI Layer. This will be */
453/* initialized as a SLI layer callback routine. */
454/* This routine returns 0 on success. Any other return value */
455/* indicates an error. */
456/* */
457/************************************************************************/
458int
459lpfc_hba_down_prep(struct lpfc_hba * phba)
460{
461 /* Disable interrupts */
462 writel(0, phba->HCregaddr);
463 readl(phba->HCregaddr); /* flush */
464
465 /* Cleanup potential discovery resources */
466 lpfc_els_flush_rscn(phba);
467 lpfc_els_flush_cmd(phba);
468 lpfc_disc_flush_list(phba);
469
470 return (0);
471}
472
473/************************************************************************/
474/* */
Jamie Wellnitz41415862006-02-28 19:25:27 -0500475/* lpfc_hba_down_post */
476/* This routine will do uninitialization after the HBA is reset */
477/* when bringing down the SLI Layer. */
478/* This routine returns 0 on success. Any other return value */
479/* indicates an error. */
480/* */
481/************************************************************************/
482int
483lpfc_hba_down_post(struct lpfc_hba * phba)
484{
485 struct lpfc_sli *psli = &phba->sli;
486 struct lpfc_sli_ring *pring;
487 struct lpfc_dmabuf *mp, *next_mp;
488 int i;
489
490 /* Cleanup preposted buffers on the ELS ring */
491 pring = &psli->ring[LPFC_ELS_RING];
492 list_for_each_entry_safe(mp, next_mp, &pring->postbufq, list) {
493 list_del(&mp->list);
494 pring->postbufq_cnt--;
495 lpfc_mbuf_free(phba, mp->virt, mp->phys);
496 kfree(mp);
497 }
498
499 for (i = 0; i < psli->num_rings; i++) {
500 pring = &psli->ring[i];
501 lpfc_sli_abort_iocb_ring(phba, pring);
502 }
503
504 return 0;
505}
506
507/************************************************************************/
508/* */
dea31012005-04-17 16:05:31 -0500509/* lpfc_handle_eratt */
510/* This routine will handle processing a Host Attention */
511/* Error Status event. This will be initialized */
512/* as a SLI layer callback routine. */
513/* */
514/************************************************************************/
515void
516lpfc_handle_eratt(struct lpfc_hba * phba)
517{
518 struct lpfc_sli *psli = &phba->sli;
519 struct lpfc_sli_ring *pring;
James Smartd2873e42006-08-18 17:46:43 -0400520 uint32_t event_data;
dea31012005-04-17 16:05:31 -0500521
dea31012005-04-17 16:05:31 -0500522 if (phba->work_hs & HS_FFER6) {
523 /* Re-establishing Link */
524 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
525 "%d:1301 Re-establishing Link "
526 "Data: x%x x%x x%x\n",
527 phba->brd_no, phba->work_hs,
528 phba->work_status[0], phba->work_status[1]);
529 spin_lock_irq(phba->host->host_lock);
530 phba->fc_flag |= FC_ESTABLISH_LINK;
James Smart92908312006-03-07 15:04:13 -0500531 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
dea31012005-04-17 16:05:31 -0500532 spin_unlock_irq(phba->host->host_lock);
533
534 /*
535 * Firmware stops when it triggled erratt with HS_FFER6.
536 * That could cause the I/Os dropped by the firmware.
537 * Error iocb (I/O) on txcmplq and let the SCSI layer
538 * retry it after re-establishing link.
539 */
540 pring = &psli->ring[psli->fcp_ring];
541 lpfc_sli_abort_iocb_ring(phba, pring);
542
543
544 /*
545 * There was a firmware error. Take the hba offline and then
546 * attempt to restart it.
547 */
548 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500549 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -0500550 if (lpfc_online(phba) == 0) { /* Initialize the HBA */
551 mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
552 return;
553 }
554 } else {
555 /* The if clause above forces this code path when the status
556 * failure is a value other than FFER6. Do not call the offline
557 * twice. This is the adapter hardware error path.
558 */
559 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
560 "%d:0457 Adapter Hardware Error "
561 "Data: x%x x%x x%x\n",
562 phba->brd_no, phba->work_hs,
563 phba->work_status[0], phba->work_status[1]);
564
James Smartd2873e42006-08-18 17:46:43 -0400565 event_data = FC_REG_DUMP_EVENT;
566 fc_host_post_vendor_event(phba->host, fc_get_event_number(),
567 sizeof(event_data), (char *) &event_data,
568 SCSI_NL_VID_TYPE_PCI | PCI_VENDOR_ID_EMULEX);
569
James Smart92908312006-03-07 15:04:13 -0500570 psli->sli_flag &= ~LPFC_SLI2_ACTIVE;
dea31012005-04-17 16:05:31 -0500571 lpfc_offline(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -0500572 phba->hba_state = LPFC_HBA_ERROR;
573 lpfc_hba_down_post(phba);
dea31012005-04-17 16:05:31 -0500574 }
575}
576
577/************************************************************************/
578/* */
579/* lpfc_handle_latt */
580/* This routine will handle processing a Host Attention */
581/* Link Status event. This will be initialized */
582/* as a SLI layer callback routine. */
583/* */
584/************************************************************************/
585void
586lpfc_handle_latt(struct lpfc_hba * phba)
587{
588 struct lpfc_sli *psli = &phba->sli;
589 LPFC_MBOXQ_t *pmb;
590 volatile uint32_t control;
591 struct lpfc_dmabuf *mp;
592 int rc = -ENOMEM;
593
594 pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
595 if (!pmb)
596 goto lpfc_handle_latt_err_exit;
597
598 mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
599 if (!mp)
600 goto lpfc_handle_latt_free_pmb;
601
602 mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
603 if (!mp->virt)
604 goto lpfc_handle_latt_free_mp;
605
606 rc = -EIO;
607
James.Smart@Emulex.Com6281bfe2005-11-28 11:41:33 -0500608 /* Cleanup any outstanding ELS commands */
609 lpfc_els_flush_cmd(phba);
dea31012005-04-17 16:05:31 -0500610
611 psli->slistat.link_event++;
612 lpfc_read_la(phba, pmb, mp);
613 pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
614 rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
615 if (rc == MBX_NOT_FINISHED)
James Smart14691152006-12-02 13:34:28 -0500616 goto lpfc_handle_latt_free_mbuf;
dea31012005-04-17 16:05:31 -0500617
618 /* Clear Link Attention in HA REG */
619 spin_lock_irq(phba->host->host_lock);
620 writel(HA_LATT, phba->HAregaddr);
621 readl(phba->HAregaddr); /* flush */
622 spin_unlock_irq(phba->host->host_lock);
623
624 return;
625
James Smart14691152006-12-02 13:34:28 -0500626lpfc_handle_latt_free_mbuf:
627 lpfc_mbuf_free(phba, mp->virt, mp->phys);
dea31012005-04-17 16:05:31 -0500628lpfc_handle_latt_free_mp:
629 kfree(mp);
630lpfc_handle_latt_free_pmb:
631 kfree(pmb);
632lpfc_handle_latt_err_exit:
633 /* Enable Link attention interrupts */
634 spin_lock_irq(phba->host->host_lock);
635 psli->sli_flag |= LPFC_PROCESS_LA;
636 control = readl(phba->HCregaddr);
637 control |= HC_LAINT_ENA;
638 writel(control, phba->HCregaddr);
639 readl(phba->HCregaddr); /* flush */
640
641 /* Clear Link Attention in HA REG */
642 writel(HA_LATT, phba->HAregaddr);
643 readl(phba->HAregaddr); /* flush */
644 spin_unlock_irq(phba->host->host_lock);
645 lpfc_linkdown(phba);
646 phba->hba_state = LPFC_HBA_ERROR;
647
648 /* The other case is an error from issue_mbox */
649 if (rc == -ENOMEM)
650 lpfc_printf_log(phba,
651 KERN_WARNING,
652 LOG_MBOX,
653 "%d:0300 READ_LA: no buffers\n",
654 phba->brd_no);
655
656 return;
657}
658
659/************************************************************************/
660/* */
661/* lpfc_parse_vpd */
662/* This routine will parse the VPD data */
663/* */
664/************************************************************************/
665static int
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500666lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd, int len)
dea31012005-04-17 16:05:31 -0500667{
668 uint8_t lenlo, lenhi;
669 uint32_t Length;
670 int i, j;
671 int finished = 0;
672 int index = 0;
673
674 if (!vpd)
675 return 0;
676
677 /* Vital Product */
678 lpfc_printf_log(phba,
679 KERN_INFO,
680 LOG_INIT,
681 "%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
682 phba->brd_no,
683 (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
684 (uint32_t) vpd[3]);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500685 while (!finished && (index < (len - 4))) {
dea31012005-04-17 16:05:31 -0500686 switch (vpd[index]) {
687 case 0x82:
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500688 case 0x91:
dea31012005-04-17 16:05:31 -0500689 index += 1;
690 lenlo = vpd[index];
691 index += 1;
692 lenhi = vpd[index];
693 index += 1;
694 i = ((((unsigned short)lenhi) << 8) + lenlo);
695 index += i;
696 break;
697 case 0x90:
698 index += 1;
699 lenlo = vpd[index];
700 index += 1;
701 lenhi = vpd[index];
702 index += 1;
703 Length = ((((unsigned short)lenhi) << 8) + lenlo);
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500704 if (Length > len - index)
705 Length = len - index;
dea31012005-04-17 16:05:31 -0500706 while (Length > 0) {
707 /* Look for Serial Number */
708 if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
709 index += 2;
710 i = vpd[index];
711 index += 1;
712 j = 0;
713 Length -= (3+i);
714 while(i--) {
715 phba->SerialNumber[j++] = vpd[index++];
716 if (j == 31)
717 break;
718 }
719 phba->SerialNumber[j] = 0;
720 continue;
721 }
722 else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
723 phba->vpd_flag |= VPD_MODEL_DESC;
724 index += 2;
725 i = vpd[index];
726 index += 1;
727 j = 0;
728 Length -= (3+i);
729 while(i--) {
730 phba->ModelDesc[j++] = vpd[index++];
731 if (j == 255)
732 break;
733 }
734 phba->ModelDesc[j] = 0;
735 continue;
736 }
737 else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
738 phba->vpd_flag |= VPD_MODEL_NAME;
739 index += 2;
740 i = vpd[index];
741 index += 1;
742 j = 0;
743 Length -= (3+i);
744 while(i--) {
745 phba->ModelName[j++] = vpd[index++];
746 if (j == 79)
747 break;
748 }
749 phba->ModelName[j] = 0;
750 continue;
751 }
752 else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
753 phba->vpd_flag |= VPD_PROGRAM_TYPE;
754 index += 2;
755 i = vpd[index];
756 index += 1;
757 j = 0;
758 Length -= (3+i);
759 while(i--) {
760 phba->ProgramType[j++] = vpd[index++];
761 if (j == 255)
762 break;
763 }
764 phba->ProgramType[j] = 0;
765 continue;
766 }
767 else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
768 phba->vpd_flag |= VPD_PORT;
769 index += 2;
770 i = vpd[index];
771 index += 1;
772 j = 0;
773 Length -= (3+i);
774 while(i--) {
775 phba->Port[j++] = vpd[index++];
776 if (j == 19)
777 break;
778 }
779 phba->Port[j] = 0;
780 continue;
781 }
782 else {
783 index += 2;
784 i = vpd[index];
785 index += 1;
786 index += i;
787 Length -= (3 + i);
788 }
789 }
790 finished = 0;
791 break;
792 case 0x78:
793 finished = 1;
794 break;
795 default:
796 index ++;
797 break;
798 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500799 }
dea31012005-04-17 16:05:31 -0500800
801 return(1);
802}
803
804static void
805lpfc_get_hba_model_desc(struct lpfc_hba * phba, uint8_t * mdp, uint8_t * descp)
806{
807 lpfc_vpd_t *vp;
James.Smart@Emulex.Comfefcb2b2005-11-28 15:08:56 -0500808 uint16_t dev_id = phba->pcidev->device;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500809 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500810 struct {
811 char * name;
812 int max_speed;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500813 char * bus;
James Smart18a3b592006-12-02 13:35:08 -0500814 } m = {"<Unknown>", 0, ""};
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500815
816 if (mdp && mdp[0] != '\0'
817 && descp && descp[0] != '\0')
818 return;
819
820 if (phba->lmt & LMT_10Gb)
821 max_speed = 10;
822 else if (phba->lmt & LMT_8Gb)
823 max_speed = 8;
824 else if (phba->lmt & LMT_4Gb)
825 max_speed = 4;
826 else if (phba->lmt & LMT_2Gb)
827 max_speed = 2;
828 else
829 max_speed = 1;
dea31012005-04-17 16:05:31 -0500830
831 vp = &phba->vpd;
dea31012005-04-17 16:05:31 -0500832
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500833 switch (dev_id) {
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400834 case PCI_DEVICE_ID_FIREFLY:
James Smart18a3b592006-12-02 13:35:08 -0500835 m = (typeof(m)){"LP6000", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400836 break;
dea31012005-04-17 16:05:31 -0500837 case PCI_DEVICE_ID_SUPERFLY:
838 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
James Smart18a3b592006-12-02 13:35:08 -0500839 m = (typeof(m)){"LP7000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500840 else
James Smart18a3b592006-12-02 13:35:08 -0500841 m = (typeof(m)){"LP7000E", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500842 break;
843 case PCI_DEVICE_ID_DRAGONFLY:
James Smart18a3b592006-12-02 13:35:08 -0500844 m = (typeof(m)){"LP8000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500845 break;
846 case PCI_DEVICE_ID_CENTAUR:
847 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
James Smart18a3b592006-12-02 13:35:08 -0500848 m = (typeof(m)){"LP9002", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500849 else
James Smart18a3b592006-12-02 13:35:08 -0500850 m = (typeof(m)){"LP9000", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500851 break;
852 case PCI_DEVICE_ID_RFLY:
James Smart18a3b592006-12-02 13:35:08 -0500853 m = (typeof(m)){"LP952", max_speed, "PCI"};
dea31012005-04-17 16:05:31 -0500854 break;
855 case PCI_DEVICE_ID_PEGASUS:
James Smart18a3b592006-12-02 13:35:08 -0500856 m = (typeof(m)){"LP9802", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500857 break;
858 case PCI_DEVICE_ID_THOR:
James Smart18a3b592006-12-02 13:35:08 -0500859 m = (typeof(m)){"LP10000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500860 break;
861 case PCI_DEVICE_ID_VIPER:
James Smart18a3b592006-12-02 13:35:08 -0500862 m = (typeof(m)){"LPX1000", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500863 break;
864 case PCI_DEVICE_ID_PFLY:
James Smart18a3b592006-12-02 13:35:08 -0500865 m = (typeof(m)){"LP982", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500866 break;
867 case PCI_DEVICE_ID_TFLY:
James Smart18a3b592006-12-02 13:35:08 -0500868 m = (typeof(m)){"LP1050", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500869 break;
870 case PCI_DEVICE_ID_HELIOS:
James Smart18a3b592006-12-02 13:35:08 -0500871 m = (typeof(m)){"LP11000", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500872 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500873 case PCI_DEVICE_ID_HELIOS_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500874 m = (typeof(m)){"LP11000-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500875 break;
876 case PCI_DEVICE_ID_HELIOS_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500877 m = (typeof(m)){"LP11002-SP", max_speed, "PCI-X2"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500878 break;
879 case PCI_DEVICE_ID_NEPTUNE:
James Smart18a3b592006-12-02 13:35:08 -0500880 m = (typeof(m)){"LPe1000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500881 break;
882 case PCI_DEVICE_ID_NEPTUNE_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500883 m = (typeof(m)){"LPe1000-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500884 break;
885 case PCI_DEVICE_ID_NEPTUNE_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500886 m = (typeof(m)){"LPe1002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500887 break;
dea31012005-04-17 16:05:31 -0500888 case PCI_DEVICE_ID_BMID:
James Smart18a3b592006-12-02 13:35:08 -0500889 m = (typeof(m)){"LP1150", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500890 break;
891 case PCI_DEVICE_ID_BSMB:
James Smart18a3b592006-12-02 13:35:08 -0500892 m = (typeof(m)){"LP111", max_speed, "PCI-X2"};
dea31012005-04-17 16:05:31 -0500893 break;
894 case PCI_DEVICE_ID_ZEPHYR:
James Smart18a3b592006-12-02 13:35:08 -0500895 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -0500896 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500897 case PCI_DEVICE_ID_ZEPHYR_SCSP:
James Smart18a3b592006-12-02 13:35:08 -0500898 m = (typeof(m)){"LPe11000", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500899 break;
900 case PCI_DEVICE_ID_ZEPHYR_DCSP:
James Smart18a3b592006-12-02 13:35:08 -0500901 m = (typeof(m)){"LPe11002-SP", max_speed, "PCIe"};
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500902 break;
dea31012005-04-17 16:05:31 -0500903 case PCI_DEVICE_ID_ZMID:
James Smart18a3b592006-12-02 13:35:08 -0500904 m = (typeof(m)){"LPe1150", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -0500905 break;
906 case PCI_DEVICE_ID_ZSMB:
James Smart18a3b592006-12-02 13:35:08 -0500907 m = (typeof(m)){"LPe111", max_speed, "PCIe"};
dea31012005-04-17 16:05:31 -0500908 break;
909 case PCI_DEVICE_ID_LP101:
James Smart18a3b592006-12-02 13:35:08 -0500910 m = (typeof(m)){"LP101", max_speed, "PCI-X"};
dea31012005-04-17 16:05:31 -0500911 break;
912 case PCI_DEVICE_ID_LP10000S:
James Smart18a3b592006-12-02 13:35:08 -0500913 m = (typeof(m)){"LP10000-S", max_speed, "PCI"};
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -0400914 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500915 case PCI_DEVICE_ID_LP11000S:
James Smart18a3b592006-12-02 13:35:08 -0500916 m = (typeof(m)){"LP11000-S", max_speed,
917 "PCI-X2"};
918 break;
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500919 case PCI_DEVICE_ID_LPE11000S:
James Smart18a3b592006-12-02 13:35:08 -0500920 m = (typeof(m)){"LPe11000-S", max_speed,
921 "PCIe"};
James.Smart@Emulex.Com5cc36b32005-11-28 11:42:19 -0500922 break;
923 default:
Randy Dunlap041976f2006-06-25 01:58:51 -0700924 m = (typeof(m)){ NULL };
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -0500925 break;
dea31012005-04-17 16:05:31 -0500926 }
Jamie Wellnitz74b72a52006-02-28 22:33:04 -0500927
928 if (mdp && mdp[0] == '\0')
929 snprintf(mdp, 79,"%s", m.name);
930 if (descp && descp[0] == '\0')
931 snprintf(descp, 255,
James Smart18a3b592006-12-02 13:35:08 -0500932 "Emulex %s %dGb %s Fibre Channel Adapter",
933 m.name, m.max_speed, m.bus);
dea31012005-04-17 16:05:31 -0500934}
935
936/**************************************************/
937/* lpfc_post_buffer */
938/* */
939/* This routine will post count buffers to the */
940/* ring with the QUE_RING_BUF_CN command. This */
941/* allows 3 buffers / command to be posted. */
942/* Returns the number of buffers NOT posted. */
943/**************************************************/
944int
945lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
946 int type)
947{
948 IOCB_t *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400949 struct lpfc_iocbq *iocb;
dea31012005-04-17 16:05:31 -0500950 struct lpfc_dmabuf *mp1, *mp2;
951
952 cnt += pring->missbufcnt;
953
954 /* While there are buffers to post */
955 while (cnt > 0) {
956 /* Allocate buffer for command iocb */
957 spin_lock_irq(phba->host->host_lock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400958 iocb = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -0500959 spin_unlock_irq(phba->host->host_lock);
960 if (iocb == NULL) {
961 pring->missbufcnt = cnt;
962 return cnt;
963 }
dea31012005-04-17 16:05:31 -0500964 icmd = &iocb->iocb;
965
966 /* 2 buffers can be posted per command */
967 /* Allocate buffer to post */
968 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
969 if (mp1)
970 mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
971 &mp1->phys);
972 if (mp1 == 0 || mp1->virt == 0) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800973 kfree(mp1);
dea31012005-04-17 16:05:31 -0500974 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -0500975 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -0500976 spin_unlock_irq(phba->host->host_lock);
977 pring->missbufcnt = cnt;
978 return cnt;
979 }
980
981 INIT_LIST_HEAD(&mp1->list);
982 /* Allocate buffer to post */
983 if (cnt > 1) {
984 mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
985 if (mp2)
986 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
987 &mp2->phys);
988 if (mp2 == 0 || mp2->virt == 0) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800989 kfree(mp2);
dea31012005-04-17 16:05:31 -0500990 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
991 kfree(mp1);
992 spin_lock_irq(phba->host->host_lock);
James Bottomley604a3e32005-10-29 10:28:33 -0500993 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -0500994 spin_unlock_irq(phba->host->host_lock);
995 pring->missbufcnt = cnt;
996 return cnt;
997 }
998
999 INIT_LIST_HEAD(&mp2->list);
1000 } else {
1001 mp2 = NULL;
1002 }
1003
1004 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
1005 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
1006 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
1007 icmd->ulpBdeCount = 1;
1008 cnt--;
1009 if (mp2) {
1010 icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
1011 icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
1012 icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
1013 cnt--;
1014 icmd->ulpBdeCount = 2;
1015 }
1016
1017 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
1018 icmd->ulpLe = 1;
1019
1020 spin_lock_irq(phba->host->host_lock);
1021 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
1022 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
1023 kfree(mp1);
1024 cnt++;
1025 if (mp2) {
1026 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
1027 kfree(mp2);
1028 cnt++;
1029 }
James Bottomley604a3e32005-10-29 10:28:33 -05001030 lpfc_sli_release_iocbq(phba, iocb);
dea31012005-04-17 16:05:31 -05001031 pring->missbufcnt = cnt;
1032 spin_unlock_irq(phba->host->host_lock);
1033 return cnt;
1034 }
1035 spin_unlock_irq(phba->host->host_lock);
1036 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
1037 if (mp2) {
1038 lpfc_sli_ringpostbuf_put(phba, pring, mp2);
1039 }
1040 }
1041 pring->missbufcnt = 0;
1042 return 0;
1043}
1044
1045/************************************************************************/
1046/* */
1047/* lpfc_post_rcv_buf */
1048/* This routine post initial rcv buffers to the configured rings */
1049/* */
1050/************************************************************************/
1051static int
1052lpfc_post_rcv_buf(struct lpfc_hba * phba)
1053{
1054 struct lpfc_sli *psli = &phba->sli;
1055
1056 /* Ring 0, ELS / CT buffers */
1057 lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
1058 /* Ring 2 - FCP no buffers needed */
1059
1060 return 0;
1061}
1062
1063#define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
1064
1065/************************************************************************/
1066/* */
1067/* lpfc_sha_init */
1068/* */
1069/************************************************************************/
1070static void
1071lpfc_sha_init(uint32_t * HashResultPointer)
1072{
1073 HashResultPointer[0] = 0x67452301;
1074 HashResultPointer[1] = 0xEFCDAB89;
1075 HashResultPointer[2] = 0x98BADCFE;
1076 HashResultPointer[3] = 0x10325476;
1077 HashResultPointer[4] = 0xC3D2E1F0;
1078}
1079
1080/************************************************************************/
1081/* */
1082/* lpfc_sha_iterate */
1083/* */
1084/************************************************************************/
1085static void
1086lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1087{
1088 int t;
1089 uint32_t TEMP;
1090 uint32_t A, B, C, D, E;
1091 t = 16;
1092 do {
1093 HashWorkingPointer[t] =
1094 S(1,
1095 HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1096 8] ^
1097 HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1098 } while (++t <= 79);
1099 t = 0;
1100 A = HashResultPointer[0];
1101 B = HashResultPointer[1];
1102 C = HashResultPointer[2];
1103 D = HashResultPointer[3];
1104 E = HashResultPointer[4];
1105
1106 do {
1107 if (t < 20) {
1108 TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1109 } else if (t < 40) {
1110 TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1111 } else if (t < 60) {
1112 TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1113 } else {
1114 TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1115 }
1116 TEMP += S(5, A) + E + HashWorkingPointer[t];
1117 E = D;
1118 D = C;
1119 C = S(30, B);
1120 B = A;
1121 A = TEMP;
1122 } while (++t <= 79);
1123
1124 HashResultPointer[0] += A;
1125 HashResultPointer[1] += B;
1126 HashResultPointer[2] += C;
1127 HashResultPointer[3] += D;
1128 HashResultPointer[4] += E;
1129
1130}
1131
1132/************************************************************************/
1133/* */
1134/* lpfc_challenge_key */
1135/* */
1136/************************************************************************/
1137static void
1138lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1139{
1140 *HashWorking = (*RandomChallenge ^ *HashWorking);
1141}
1142
1143/************************************************************************/
1144/* */
1145/* lpfc_hba_init */
1146/* */
1147/************************************************************************/
1148void
1149lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1150{
1151 int t;
1152 uint32_t *HashWorking;
1153 uint32_t *pwwnn = phba->wwnn;
1154
1155 HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
1156 if (!HashWorking)
1157 return;
1158
1159 memset(HashWorking, 0, (80 * sizeof(uint32_t)));
1160 HashWorking[0] = HashWorking[78] = *pwwnn++;
1161 HashWorking[1] = HashWorking[79] = *pwwnn;
1162
1163 for (t = 0; t < 7; t++)
1164 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1165
1166 lpfc_sha_init(hbainit);
1167 lpfc_sha_iterate(hbainit, HashWorking);
1168 kfree(HashWorking);
1169}
1170
1171static void
1172lpfc_cleanup(struct lpfc_hba * phba, uint32_t save_bind)
1173{
1174 struct lpfc_nodelist *ndlp, *next_ndlp;
1175
1176 /* clean up phba - lpfc specific */
1177 lpfc_can_disctmo(phba);
1178 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpunmap_list,
1179 nlp_listp) {
1180 lpfc_nlp_remove(phba, ndlp);
1181 }
1182
1183 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpmap_list,
1184 nlp_listp) {
1185 lpfc_nlp_remove(phba, ndlp);
1186 }
1187
1188 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_unused_list,
1189 nlp_listp) {
1190 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1191 }
1192
1193 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
1194 nlp_listp) {
1195 lpfc_nlp_remove(phba, ndlp);
1196 }
1197
1198 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1199 nlp_listp) {
1200 lpfc_nlp_remove(phba, ndlp);
1201 }
1202
1203 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_reglogin_list,
1204 nlp_listp) {
1205 lpfc_nlp_remove(phba, ndlp);
1206 }
1207
1208 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_prli_list,
1209 nlp_listp) {
1210 lpfc_nlp_remove(phba, ndlp);
1211 }
1212
1213 list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1214 nlp_listp) {
1215 lpfc_nlp_remove(phba, ndlp);
1216 }
1217
1218 INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1219 INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1220 INIT_LIST_HEAD(&phba->fc_unused_list);
1221 INIT_LIST_HEAD(&phba->fc_plogi_list);
1222 INIT_LIST_HEAD(&phba->fc_adisc_list);
1223 INIT_LIST_HEAD(&phba->fc_reglogin_list);
1224 INIT_LIST_HEAD(&phba->fc_prli_list);
1225 INIT_LIST_HEAD(&phba->fc_npr_list);
1226
1227 phba->fc_map_cnt = 0;
1228 phba->fc_unmap_cnt = 0;
1229 phba->fc_plogi_cnt = 0;
1230 phba->fc_adisc_cnt = 0;
1231 phba->fc_reglogin_cnt = 0;
1232 phba->fc_prli_cnt = 0;
1233 phba->fc_npr_cnt = 0;
1234 phba->fc_unused_cnt= 0;
1235 return;
1236}
1237
1238static void
1239lpfc_establish_link_tmo(unsigned long ptr)
1240{
1241 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1242 unsigned long iflag;
1243
1244
1245 /* Re-establishing Link, timer expired */
1246 lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1247 "%d:1300 Re-establishing Link, timer expired "
1248 "Data: x%x x%x\n",
1249 phba->brd_no, phba->fc_flag, phba->hba_state);
1250 spin_lock_irqsave(phba->host->host_lock, iflag);
1251 phba->fc_flag &= ~FC_ESTABLISH_LINK;
1252 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1253}
1254
1255static int
1256lpfc_stop_timer(struct lpfc_hba * phba)
1257{
1258 struct lpfc_sli *psli = &phba->sli;
1259
1260 /* Instead of a timer, this has been converted to a
1261 * deferred procedding list.
1262 */
1263 while (!list_empty(&phba->freebufList)) {
1264
1265 struct lpfc_dmabuf *mp = NULL;
1266
1267 list_remove_head((&phba->freebufList), mp,
1268 struct lpfc_dmabuf, list);
1269 if (mp) {
1270 lpfc_mbuf_free(phba, mp->virt, mp->phys);
1271 kfree(mp);
1272 }
1273 }
1274
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001275 del_timer_sync(&phba->fcp_poll_timer);
dea31012005-04-17 16:05:31 -05001276 del_timer_sync(&phba->fc_estabtmo);
1277 del_timer_sync(&phba->fc_disctmo);
1278 del_timer_sync(&phba->fc_fdmitmo);
1279 del_timer_sync(&phba->els_tmofunc);
1280 psli = &phba->sli;
1281 del_timer_sync(&psli->mbox_tmo);
1282 return(1);
1283}
1284
1285int
1286lpfc_online(struct lpfc_hba * phba)
1287{
1288 if (!phba)
1289 return 0;
1290
1291 if (!(phba->fc_flag & FC_OFFLINE_MODE))
1292 return 0;
1293
1294 lpfc_printf_log(phba,
1295 KERN_WARNING,
1296 LOG_INIT,
1297 "%d:0458 Bring Adapter online\n",
1298 phba->brd_no);
1299
1300 if (!lpfc_sli_queue_setup(phba))
1301 return 1;
1302
1303 if (lpfc_sli_hba_setup(phba)) /* Initialize the HBA */
1304 return 1;
1305
1306 spin_lock_irq(phba->host->host_lock);
1307 phba->fc_flag &= ~FC_OFFLINE_MODE;
1308 spin_unlock_irq(phba->host->host_lock);
1309
dea31012005-04-17 16:05:31 -05001310 return 0;
1311}
1312
1313int
1314lpfc_offline(struct lpfc_hba * phba)
1315{
1316 struct lpfc_sli_ring *pring;
1317 struct lpfc_sli *psli;
1318 unsigned long iflag;
James Smart688a8862006-07-06 15:49:56 -04001319 int i;
1320 int cnt = 0;
dea31012005-04-17 16:05:31 -05001321
1322 if (!phba)
1323 return 0;
1324
1325 if (phba->fc_flag & FC_OFFLINE_MODE)
1326 return 0;
1327
dea31012005-04-17 16:05:31 -05001328 psli = &phba->sli;
dea31012005-04-17 16:05:31 -05001329
1330 lpfc_linkdown(phba);
James Smartb4c02652006-07-06 15:50:43 -04001331 lpfc_sli_flush_mbox_queue(phba);
dea31012005-04-17 16:05:31 -05001332
James Smart688a8862006-07-06 15:49:56 -04001333 for (i = 0; i < psli->num_rings; i++) {
1334 pring = &psli->ring[i];
1335 /* The linkdown event takes 30 seconds to timeout. */
1336 while (pring->txcmplq_cnt) {
1337 mdelay(10);
1338 if (cnt++ > 3000) {
1339 lpfc_printf_log(phba,
1340 KERN_WARNING, LOG_INIT,
1341 "%d:0466 Outstanding IO when "
1342 "bringing Adapter offline\n",
1343 phba->brd_no);
1344 break;
1345 }
1346 }
dea31012005-04-17 16:05:31 -05001347 }
1348
James Smart688a8862006-07-06 15:49:56 -04001349
dea31012005-04-17 16:05:31 -05001350 /* stop all timers associated with this hba */
1351 lpfc_stop_timer(phba);
1352 phba->work_hba_events = 0;
James Smart33ccf8d2006-08-17 11:57:58 -04001353 phba->work_ha = 0;
dea31012005-04-17 16:05:31 -05001354
1355 lpfc_printf_log(phba,
1356 KERN_WARNING,
1357 LOG_INIT,
1358 "%d:0460 Bring Adapter offline\n",
1359 phba->brd_no);
1360
1361 /* Bring down the SLI Layer and cleanup. The HBA is offline
1362 now. */
1363 lpfc_sli_hba_down(phba);
1364 lpfc_cleanup(phba, 1);
1365 spin_lock_irqsave(phba->host->host_lock, iflag);
1366 phba->fc_flag |= FC_OFFLINE_MODE;
1367 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1368 return 0;
1369}
1370
1371/******************************************************************************
1372* Function name: lpfc_scsi_free
1373*
1374* Description: Called from lpfc_pci_remove_one free internal driver resources
1375*
1376******************************************************************************/
1377static int
1378lpfc_scsi_free(struct lpfc_hba * phba)
1379{
1380 struct lpfc_scsi_buf *sb, *sb_next;
1381 struct lpfc_iocbq *io, *io_next;
1382
1383 spin_lock_irq(phba->host->host_lock);
1384 /* Release all the lpfc_scsi_bufs maintained by this host. */
1385 list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1386 list_del(&sb->list);
1387 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1388 sb->dma_handle);
1389 kfree(sb);
1390 phba->total_scsi_bufs--;
1391 }
1392
1393 /* Release all the lpfc_iocbq entries maintained by this host. */
1394 list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1395 list_del(&io->list);
1396 kfree(io);
1397 phba->total_iocbq_bufs--;
1398 }
1399
1400 spin_unlock_irq(phba->host->host_lock);
1401
1402 return 0;
1403}
1404
1405
1406static int __devinit
1407lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1408{
1409 struct Scsi_Host *host;
1410 struct lpfc_hba *phba;
1411 struct lpfc_sli *psli;
1412 struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
1413 unsigned long bar0map_len, bar2map_len;
1414 int error = -ENODEV, retval;
1415 int i;
James Bottomley604a3e32005-10-29 10:28:33 -05001416 uint16_t iotag;
dea31012005-04-17 16:05:31 -05001417
1418 if (pci_enable_device(pdev))
1419 goto out;
1420 if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1421 goto out_disable_device;
1422
James.Smart@Emulex.Comf888ba32005-08-10 15:03:01 -04001423 host = scsi_host_alloc(&lpfc_template, sizeof (struct lpfc_hba));
dea31012005-04-17 16:05:31 -05001424 if (!host)
1425 goto out_release_regions;
1426
1427 phba = (struct lpfc_hba*)host->hostdata;
1428 memset(phba, 0, sizeof (struct lpfc_hba));
dea31012005-04-17 16:05:31 -05001429 phba->host = host;
1430
1431 phba->fc_flag |= FC_LOADING;
1432 phba->pcidev = pdev;
1433
1434 /* Assign an unused board number */
1435 if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1436 goto out_put_host;
1437
1438 error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
1439 if (error)
1440 goto out_put_host;
1441
1442 host->unique_id = phba->brd_no;
dea31012005-04-17 16:05:31 -05001443 INIT_LIST_HEAD(&phba->ctrspbuflist);
1444 INIT_LIST_HEAD(&phba->rnidrspbuflist);
1445 INIT_LIST_HEAD(&phba->freebufList);
1446
1447 /* Initialize timers used by driver */
1448 init_timer(&phba->fc_estabtmo);
1449 phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1450 phba->fc_estabtmo.data = (unsigned long)phba;
1451 init_timer(&phba->fc_disctmo);
1452 phba->fc_disctmo.function = lpfc_disc_timeout;
1453 phba->fc_disctmo.data = (unsigned long)phba;
1454
1455 init_timer(&phba->fc_fdmitmo);
1456 phba->fc_fdmitmo.function = lpfc_fdmi_tmo;
1457 phba->fc_fdmitmo.data = (unsigned long)phba;
1458 init_timer(&phba->els_tmofunc);
1459 phba->els_tmofunc.function = lpfc_els_timeout;
1460 phba->els_tmofunc.data = (unsigned long)phba;
1461 psli = &phba->sli;
1462 init_timer(&psli->mbox_tmo);
1463 psli->mbox_tmo.function = lpfc_mbox_timeout;
1464 psli->mbox_tmo.data = (unsigned long)phba;
1465
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001466 init_timer(&phba->fcp_poll_timer);
1467 phba->fcp_poll_timer.function = lpfc_poll_timeout;
1468 phba->fcp_poll_timer.data = (unsigned long)phba;
1469
dea31012005-04-17 16:05:31 -05001470 /*
1471 * Get all the module params for configuring this host and then
1472 * establish the host parameters.
1473 */
1474 lpfc_get_cfgparam(phba);
1475
1476 host->max_id = LPFC_MAX_TARGET;
1477 host->max_lun = phba->cfg_max_luns;
1478 host->this_id = -1;
1479
1480 /* Initialize all internally managed lists. */
1481 INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1482 INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1483 INIT_LIST_HEAD(&phba->fc_unused_list);
1484 INIT_LIST_HEAD(&phba->fc_plogi_list);
1485 INIT_LIST_HEAD(&phba->fc_adisc_list);
1486 INIT_LIST_HEAD(&phba->fc_reglogin_list);
1487 INIT_LIST_HEAD(&phba->fc_prli_list);
1488 INIT_LIST_HEAD(&phba->fc_npr_list);
1489
1490
1491 pci_set_master(pdev);
1492 retval = pci_set_mwi(pdev);
1493 if (retval)
1494 dev_printk(KERN_WARNING, &pdev->dev,
1495 "Warning: pci_set_mwi returned %d\n", retval);
1496
1497 if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1498 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1499 goto out_idr_remove;
1500
1501 /*
1502 * Get the bus address of Bar0 and Bar2 and the number of bytes
1503 * required by each mapping.
1504 */
1505 phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1506 bar0map_len = pci_resource_len(phba->pcidev, 0);
1507
1508 phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1509 bar2map_len = pci_resource_len(phba->pcidev, 2);
1510
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001511 /* Map HBA SLIM to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001512 phba->slim_memmap_p = ioremap(phba->pci_bar0_map, bar0map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001513 if (!phba->slim_memmap_p) {
1514 error = -ENODEV;
1515 dev_printk(KERN_ERR, &pdev->dev,
1516 "ioremap failed for SLIM memory.\n");
1517 goto out_idr_remove;
1518 }
1519
1520 /* Map HBA Control Registers to a kernel virtual address. */
dea31012005-04-17 16:05:31 -05001521 phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001522 if (!phba->ctrl_regs_memmap_p) {
1523 error = -ENODEV;
1524 dev_printk(KERN_ERR, &pdev->dev,
1525 "ioremap failed for HBA control registers.\n");
1526 goto out_iounmap_slim;
1527 }
dea31012005-04-17 16:05:31 -05001528
1529 /* Allocate memory for SLI-2 structures */
1530 phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1531 &phba->slim2p_mapping, GFP_KERNEL);
1532 if (!phba->slim2p)
1533 goto out_iounmap;
1534
James.Smart@Emulex.Comf91b3922005-10-28 20:29:28 -04001535 memset(phba->slim2p, 0, SLI2_SLIM_SIZE);
dea31012005-04-17 16:05:31 -05001536
1537 /* Initialize the SLI Layer to run with lpfc HBAs. */
1538 lpfc_sli_setup(phba);
1539 lpfc_sli_queue_setup(phba);
1540
1541 error = lpfc_mem_alloc(phba);
1542 if (error)
1543 goto out_free_slim;
1544
1545 /* Initialize and populate the iocb list per host. */
1546 INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1547 for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
1548 iocbq_entry = kmalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
1549 if (iocbq_entry == NULL) {
1550 printk(KERN_ERR "%s: only allocated %d iocbs of "
1551 "expected %d count. Unloading driver.\n",
1552 __FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1553 error = -ENOMEM;
1554 goto out_free_iocbq;
1555 }
1556
1557 memset(iocbq_entry, 0, sizeof(struct lpfc_iocbq));
James Bottomley604a3e32005-10-29 10:28:33 -05001558 iotag = lpfc_sli_next_iotag(phba, iocbq_entry);
1559 if (iotag == 0) {
1560 kfree (iocbq_entry);
1561 printk(KERN_ERR "%s: failed to allocate IOTAG. "
1562 "Unloading driver.\n",
1563 __FUNCTION__);
1564 error = -ENOMEM;
1565 goto out_free_iocbq;
1566 }
dea31012005-04-17 16:05:31 -05001567 spin_lock_irq(phba->host->host_lock);
1568 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1569 phba->total_iocbq_bufs++;
1570 spin_unlock_irq(phba->host->host_lock);
1571 }
1572
1573 /* Initialize HBA structure */
1574 phba->fc_edtov = FF_DEF_EDTOV;
1575 phba->fc_ratov = FF_DEF_RATOV;
1576 phba->fc_altov = FF_DEF_ALTOV;
1577 phba->fc_arbtov = FF_DEF_ARBTOV;
1578
1579 INIT_LIST_HEAD(&phba->work_list);
1580 phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
1581 phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
1582
1583 /* Startup the kernel thread for this host adapter. */
1584 phba->worker_thread = kthread_run(lpfc_do_work, phba,
1585 "lpfc_worker_%d", phba->brd_no);
1586 if (IS_ERR(phba->worker_thread)) {
1587 error = PTR_ERR(phba->worker_thread);
1588 goto out_free_iocbq;
1589 }
1590
James Smart8f6d98d2006-08-01 07:34:00 -04001591 /*
1592 * Set initial can_queue value since 0 is no longer supported and
1593 * scsi_add_host will fail. This will be adjusted later based on the
1594 * max xri value determined in hba setup.
1595 */
dea31012005-04-17 16:05:31 -05001596 host->can_queue = phba->cfg_hba_queue_depth - 10;
1597
1598 /* Tell the midlayer we support 16 byte commands */
1599 host->max_cmd_len = 16;
1600
1601 /* Initialize the list of scsi buffers used by driver for scsi IO. */
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001602 spin_lock_init(&phba->scsi_buf_list_lock);
dea31012005-04-17 16:05:31 -05001603 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1604
1605 host->transportt = lpfc_transport_template;
dea31012005-04-17 16:05:31 -05001606 pci_set_drvdata(pdev, host);
1607 error = scsi_add_host(host, &pdev->dev);
1608 if (error)
1609 goto out_kthread_stop;
1610
1611 error = lpfc_alloc_sysfs_attr(phba);
1612 if (error)
James Smartdefbcf12006-04-16 22:26:50 -04001613 goto out_remove_host;
dea31012005-04-17 16:05:31 -05001614
James Smart4ff43242006-12-02 13:34:56 -05001615 if (phba->cfg_use_msi) {
1616 error = pci_enable_msi(phba->pcidev);
1617 if (error)
1618 lpfc_printf_log(phba, KERN_INFO, LOG_INIT, "%d:0452 "
1619 "Enable MSI failed, continuing with "
1620 "IRQ\n", phba->brd_no);
1621 }
1622
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001623 error = request_irq(phba->pcidev->irq, lpfc_intr_handler, IRQF_SHARED,
dea31012005-04-17 16:05:31 -05001624 LPFC_DRIVER_NAME, phba);
1625 if (error) {
1626 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1627 "%d:0451 Enable interrupt handler failed\n",
1628 phba->brd_no);
1629 goto out_free_sysfs_attr;
1630 }
1631 phba->MBslimaddr = phba->slim_memmap_p;
1632 phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
1633 phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
1634 phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
1635 phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
1636
1637 error = lpfc_sli_hba_setup(phba);
James Smartdefbcf12006-04-16 22:26:50 -04001638 if (error) {
1639 error = -ENODEV;
dea31012005-04-17 16:05:31 -05001640 goto out_free_irq;
James Smartdefbcf12006-04-16 22:26:50 -04001641 }
dea31012005-04-17 16:05:31 -05001642
James Smart8f6d98d2006-08-01 07:34:00 -04001643 /*
1644 * hba setup may have changed the hba_queue_depth so we need to adjust
1645 * the value of can_queue.
1646 */
1647 host->can_queue = phba->cfg_hba_queue_depth - 10;
1648
James Smartce8b3ce2006-07-06 15:50:36 -04001649 lpfc_discovery_wait(phba);
1650
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001651 if (phba->cfg_poll & DISABLE_FCP_RING_INT) {
1652 spin_lock_irq(phba->host->host_lock);
1653 lpfc_poll_start_timer(phba);
1654 spin_unlock_irq(phba->host->host_lock);
1655 }
1656
dea31012005-04-17 16:05:31 -05001657 /*
1658 * set fixed host attributes
1659 * Must done after lpfc_sli_hba_setup()
1660 */
1661
Andrew Morton68ce1eb2005-09-21 09:46:54 -07001662 fc_host_node_name(host) = wwn_to_u64(phba->fc_nodename.u.wwn);
1663 fc_host_port_name(host) = wwn_to_u64(phba->fc_portname.u.wwn);
dea31012005-04-17 16:05:31 -05001664 fc_host_supported_classes(host) = FC_COS_CLASS3;
1665
1666 memset(fc_host_supported_fc4s(host), 0,
1667 sizeof(fc_host_supported_fc4s(host)));
1668 fc_host_supported_fc4s(host)[2] = 1;
1669 fc_host_supported_fc4s(host)[7] = 1;
1670
1671 lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(host));
1672
1673 fc_host_supported_speeds(host) = 0;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001674 if (phba->lmt & LMT_10Gb)
dea31012005-04-17 16:05:31 -05001675 fc_host_supported_speeds(host) |= FC_PORTSPEED_10GBIT;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001676 if (phba->lmt & LMT_4Gb)
dea31012005-04-17 16:05:31 -05001677 fc_host_supported_speeds(host) |= FC_PORTSPEED_4GBIT;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001678 if (phba->lmt & LMT_2Gb)
dea31012005-04-17 16:05:31 -05001679 fc_host_supported_speeds(host) |= FC_PORTSPEED_2GBIT;
Jamie Wellnitz74b72a52006-02-28 22:33:04 -05001680 if (phba->lmt & LMT_1Gb)
1681 fc_host_supported_speeds(host) |= FC_PORTSPEED_1GBIT;
dea31012005-04-17 16:05:31 -05001682
1683 fc_host_maxframe_size(host) =
1684 ((((uint32_t) phba->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1685 (uint32_t) phba->fc_sparam.cmn.bbRcvSizeLsb);
1686
1687 /* This value is also unchanging */
1688 memset(fc_host_active_fc4s(host), 0,
1689 sizeof(fc_host_active_fc4s(host)));
1690 fc_host_active_fc4s(host)[2] = 1;
1691 fc_host_active_fc4s(host)[7] = 1;
1692
1693 spin_lock_irq(phba->host->host_lock);
1694 phba->fc_flag &= ~FC_LOADING;
1695 spin_unlock_irq(phba->host->host_lock);
1696 return 0;
1697
1698out_free_irq:
1699 lpfc_stop_timer(phba);
1700 phba->work_hba_events = 0;
1701 free_irq(phba->pcidev->irq, phba);
James Smart4ff43242006-12-02 13:34:56 -05001702 pci_disable_msi(phba->pcidev);
dea31012005-04-17 16:05:31 -05001703out_free_sysfs_attr:
1704 lpfc_free_sysfs_attr(phba);
James Smartdefbcf12006-04-16 22:26:50 -04001705out_remove_host:
1706 fc_remove_host(phba->host);
1707 scsi_remove_host(phba->host);
dea31012005-04-17 16:05:31 -05001708out_kthread_stop:
1709 kthread_stop(phba->worker_thread);
1710out_free_iocbq:
1711 list_for_each_entry_safe(iocbq_entry, iocbq_next,
1712 &phba->lpfc_iocb_list, list) {
1713 spin_lock_irq(phba->host->host_lock);
1714 kfree(iocbq_entry);
1715 phba->total_iocbq_bufs--;
1716 spin_unlock_irq(phba->host->host_lock);
1717 }
1718 lpfc_mem_free(phba);
1719out_free_slim:
1720 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
1721 phba->slim2p_mapping);
1722out_iounmap:
1723 iounmap(phba->ctrl_regs_memmap_p);
Jamie Wellnitz901a9202006-02-28 19:25:19 -05001724out_iounmap_slim:
dea31012005-04-17 16:05:31 -05001725 iounmap(phba->slim_memmap_p);
1726out_idr_remove:
1727 idr_remove(&lpfc_hba_index, phba->brd_no);
1728out_put_host:
James Smartdefbcf12006-04-16 22:26:50 -04001729 phba->host = NULL;
dea31012005-04-17 16:05:31 -05001730 scsi_host_put(host);
1731out_release_regions:
1732 pci_release_regions(pdev);
1733out_disable_device:
1734 pci_disable_device(pdev);
1735out:
James Smartdefbcf12006-04-16 22:26:50 -04001736 pci_set_drvdata(pdev, NULL);
dea31012005-04-17 16:05:31 -05001737 return error;
1738}
1739
1740static void __devexit
1741lpfc_pci_remove_one(struct pci_dev *pdev)
1742{
1743 struct Scsi_Host *host = pci_get_drvdata(pdev);
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -05001744 struct lpfc_hba *phba = (struct lpfc_hba *)host->hostdata;
dea31012005-04-17 16:05:31 -05001745 unsigned long iflag;
1746
1747 lpfc_free_sysfs_attr(phba);
1748
1749 spin_lock_irqsave(phba->host->host_lock, iflag);
1750 phba->fc_flag |= FC_UNLOADING;
1751
1752 spin_unlock_irqrestore(phba->host->host_lock, iflag);
1753
1754 fc_remove_host(phba->host);
1755 scsi_remove_host(phba->host);
1756
1757 kthread_stop(phba->worker_thread);
1758
1759 /*
1760 * Bring down the SLI Layer. This step disable all interrupts,
1761 * clears the rings, discards all mailbox commands, and resets
1762 * the HBA.
1763 */
1764 lpfc_sli_hba_down(phba);
Jamie Wellnitz41415862006-02-28 19:25:27 -05001765 lpfc_sli_brdrestart(phba);
dea31012005-04-17 16:05:31 -05001766
1767 /* Release the irq reservation */
1768 free_irq(phba->pcidev->irq, phba);
James Smart4ff43242006-12-02 13:34:56 -05001769 pci_disable_msi(phba->pcidev);
dea31012005-04-17 16:05:31 -05001770
1771 lpfc_cleanup(phba, 0);
1772 lpfc_stop_timer(phba);
1773 phba->work_hba_events = 0;
1774
1775 /*
1776 * Call scsi_free before mem_free since scsi bufs are released to their
1777 * corresponding pools here.
1778 */
1779 lpfc_scsi_free(phba);
1780 lpfc_mem_free(phba);
1781
1782 /* Free resources associated with SLI2 interface */
1783 dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
1784 phba->slim2p, phba->slim2p_mapping);
1785
1786 /* unmap adapter SLIM and Control Registers */
1787 iounmap(phba->ctrl_regs_memmap_p);
1788 iounmap(phba->slim_memmap_p);
1789
1790 pci_release_regions(phba->pcidev);
1791 pci_disable_device(phba->pcidev);
1792
1793 idr_remove(&lpfc_hba_index, phba->brd_no);
1794 scsi_host_put(phba->host);
1795
1796 pci_set_drvdata(pdev, NULL);
1797}
1798
1799static struct pci_device_id lpfc_id_table[] = {
1800 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
1801 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Com06325e72005-06-25 10:34:22 -04001802 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
1803 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05001804 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
1805 PCI_ANY_ID, PCI_ANY_ID, },
1806 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
1807 PCI_ANY_ID, PCI_ANY_ID, },
1808 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
1809 PCI_ANY_ID, PCI_ANY_ID, },
1810 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
1811 PCI_ANY_ID, PCI_ANY_ID, },
1812 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
1813 PCI_ANY_ID, PCI_ANY_ID, },
1814 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
1815 PCI_ANY_ID, PCI_ANY_ID, },
1816 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
1817 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001818 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE,
1819 PCI_ANY_ID, PCI_ANY_ID, },
1820 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_SCSP,
1821 PCI_ANY_ID, PCI_ANY_ID, },
1822 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_NEPTUNE_DCSP,
1823 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05001824 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
1825 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001826 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_SCSP,
1827 PCI_ANY_ID, PCI_ANY_ID, },
1828 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS_DCSP,
1829 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05001830 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
1831 PCI_ANY_ID, PCI_ANY_ID, },
1832 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
1833 PCI_ANY_ID, PCI_ANY_ID, },
1834 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
1835 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001836 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_SCSP,
1837 PCI_ANY_ID, PCI_ANY_ID, },
1838 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR_DCSP,
1839 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05001840 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
1841 PCI_ANY_ID, PCI_ANY_ID, },
1842 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
1843 PCI_ANY_ID, PCI_ANY_ID, },
1844 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
1845 PCI_ANY_ID, PCI_ANY_ID, },
1846 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
1847 PCI_ANY_ID, PCI_ANY_ID, },
1848 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
1849 PCI_ANY_ID, PCI_ANY_ID, },
James.Smart@Emulex.Come4adb202005-11-28 11:42:12 -05001850 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP11000S,
1851 PCI_ANY_ID, PCI_ANY_ID, },
1852 {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LPE11000S,
1853 PCI_ANY_ID, PCI_ANY_ID, },
dea31012005-04-17 16:05:31 -05001854 { 0 }
1855};
1856
1857MODULE_DEVICE_TABLE(pci, lpfc_id_table);
1858
1859static struct pci_driver lpfc_driver = {
1860 .name = LPFC_DRIVER_NAME,
1861 .id_table = lpfc_id_table,
1862 .probe = lpfc_pci_probe_one,
1863 .remove = __devexit_p(lpfc_pci_remove_one),
1864};
1865
1866static int __init
1867lpfc_init(void)
1868{
1869 int error = 0;
1870
1871 printk(LPFC_MODULE_DESC "\n");
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04001872 printk(LPFC_COPYRIGHT "\n");
dea31012005-04-17 16:05:31 -05001873
1874 lpfc_transport_template =
1875 fc_attach_transport(&lpfc_transport_functions);
1876 if (!lpfc_transport_template)
1877 return -ENOMEM;
1878 error = pci_register_driver(&lpfc_driver);
1879 if (error)
1880 fc_release_transport(lpfc_transport_template);
1881
1882 return error;
1883}
1884
1885static void __exit
1886lpfc_exit(void)
1887{
1888 pci_unregister_driver(&lpfc_driver);
1889 fc_release_transport(lpfc_transport_template);
1890}
1891
1892module_init(lpfc_init);
1893module_exit(lpfc_exit);
1894MODULE_LICENSE("GPL");
1895MODULE_DESCRIPTION(LPFC_MODULE_DESC);
1896MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
1897MODULE_VERSION("0:" LPFC_DRIVER_VERSION);