[SCSI] lpfc: Add LP6000 PCI ID
[powerpc.git] / drivers / scsi / lpfc / lpfc_init.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Enterprise Fibre Channel Host Bus Adapters.                     *
4  * Refer to the README file included with this package for         *
5  * driver version and adapter support.                             *
6  * Copyright (C) 2004 Emulex Corporation.                          *
7  * www.emulex.com                                                  *
8  *                                                                 *
9  * This program is free software; you can redistribute it and/or   *
10  * modify it under the terms of the GNU General Public License     *
11  * as published by the Free Software Foundation; either version 2  *
12  * of the License, or (at your option) any later version.          *
13  *                                                                 *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of  *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   *
17  * GNU General Public License for more details, a copy of which    *
18  * can be found in the file COPYING included with this package.    *
19  *******************************************************************/
20
21 /*
22  * $Id: lpfc_init.c 1.233 2005/04/13 11:59:09EDT sf_support Exp  $
23  */
24
25 #include <linux/blkdev.h>
26 #include <linux/delay.h>
27 #include <linux/dma-mapping.h>
28 #include <linux/idr.h>
29 #include <linux/interrupt.h>
30 #include <linux/kthread.h>
31 #include <linux/pci.h>
32 #include <linux/spinlock.h>
33
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_host.h>
36 #include <scsi/scsi_transport_fc.h>
37
38 #include "lpfc_hw.h"
39 #include "lpfc_sli.h"
40 #include "lpfc_disc.h"
41 #include "lpfc_scsi.h"
42 #include "lpfc.h"
43 #include "lpfc_logmsg.h"
44 #include "lpfc_crtn.h"
45 #include "lpfc_version.h"
46
47 static int lpfc_parse_vpd(struct lpfc_hba *, uint8_t *);
48 static void lpfc_get_hba_model_desc(struct lpfc_hba *, uint8_t *, uint8_t *);
49 static int lpfc_post_rcv_buf(struct lpfc_hba *);
50
51 static struct scsi_transport_template *lpfc_transport_template = NULL;
52 static DEFINE_IDR(lpfc_hba_index);
53
54 /************************************************************************/
55 /*                                                                      */
56 /*    lpfc_config_port_prep                                             */
57 /*    This routine will do LPFC initialization prior to the             */
58 /*    CONFIG_PORT mailbox command. This will be initialized             */
59 /*    as a SLI layer callback routine.                                  */
60 /*    This routine returns 0 on success or -ERESTART if it wants        */
61 /*    the SLI layer to reset the HBA and try again. Any                 */
62 /*    other return value indicates an error.                            */
63 /*                                                                      */
64 /************************************************************************/
65 int
66 lpfc_config_port_prep(struct lpfc_hba * phba)
67 {
68         lpfc_vpd_t *vp = &phba->vpd;
69         int i = 0, rc;
70         LPFC_MBOXQ_t *pmb;
71         MAILBOX_t *mb;
72         char *lpfc_vpd_data = NULL;
73         uint16_t offset = 0;
74         static char licensed[56] =
75                     "key unlock for use with gnu public licensed code only\0";
76
77         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
78         if (!pmb) {
79                 phba->hba_state = LPFC_HBA_ERROR;
80                 return -ENOMEM;
81         }
82
83         mb = &pmb->mb;
84         phba->hba_state = LPFC_INIT_MBX_CMDS;
85
86         if (lpfc_is_LC_HBA(phba->pcidev->device)) {
87                 uint32_t *ptext = (uint32_t *) licensed;
88
89                 for (i = 0; i < 56; i += sizeof (uint32_t), ptext++)
90                         *ptext = cpu_to_be32(*ptext);
91
92                 lpfc_read_nv(phba, pmb);
93                 memset((char*)mb->un.varRDnvp.rsvd3, 0,
94                         sizeof (mb->un.varRDnvp.rsvd3));
95                 memcpy((char*)mb->un.varRDnvp.rsvd3, licensed,
96                          sizeof (licensed));
97
98                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
99
100                 if (rc != MBX_SUCCESS) {
101                         lpfc_printf_log(phba,
102                                         KERN_ERR,
103                                         LOG_MBOX,
104                                         "%d:0324 Config Port initialization "
105                                         "error, mbxCmd x%x READ_NVPARM, "
106                                         "mbxStatus x%x\n",
107                                         phba->brd_no,
108                                         mb->mbxCommand, mb->mbxStatus);
109                         mempool_free(pmb, phba->mbox_mem_pool);
110                         return -ERESTART;
111                 }
112                 memcpy(phba->wwnn, (char *)mb->un.varRDnvp.nodename,
113                        sizeof (mb->un.varRDnvp.nodename));
114         }
115
116         /* Setup and issue mailbox READ REV command */
117         lpfc_read_rev(phba, pmb);
118         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
119         if (rc != MBX_SUCCESS) {
120                 lpfc_printf_log(phba,
121                                 KERN_ERR,
122                                 LOG_INIT,
123                                 "%d:0439 Adapter failed to init, mbxCmd x%x "
124                                 "READ_REV, mbxStatus x%x\n",
125                                 phba->brd_no,
126                                 mb->mbxCommand, mb->mbxStatus);
127                 mempool_free( pmb, phba->mbox_mem_pool);
128                 return -ERESTART;
129         }
130
131         /* The HBA's current state is provided by the ProgType and rr fields.
132          * Read and check the value of these fields before continuing to config
133          * this port.
134          */
135         if (mb->un.varRdRev.rr == 0 || mb->un.varRdRev.un.b.ProgType != 2) {
136                 /* Old firmware */
137                 vp->rev.rBit = 0;
138                 lpfc_printf_log(phba,
139                                 KERN_ERR,
140                                 LOG_INIT,
141                                 "%d:0440 Adapter failed to init, mbxCmd x%x "
142                                 "READ_REV detected outdated firmware"
143                                 "Data: x%x\n",
144                                 phba->brd_no,
145                                 mb->mbxCommand, 0);
146                 mempool_free(pmb, phba->mbox_mem_pool);
147                 return -ERESTART;
148         } else {
149                 vp->rev.rBit = 1;
150                 vp->rev.sli1FwRev = mb->un.varRdRev.sli1FwRev;
151                 memcpy(vp->rev.sli1FwName,
152                         (char*)mb->un.varRdRev.sli1FwName, 16);
153                 vp->rev.sli2FwRev = mb->un.varRdRev.sli2FwRev;
154                 memcpy(vp->rev.sli2FwName,
155                                         (char *)mb->un.varRdRev.sli2FwName, 16);
156         }
157
158         /* Save information as VPD data */
159         vp->rev.biuRev = mb->un.varRdRev.biuRev;
160         vp->rev.smRev = mb->un.varRdRev.smRev;
161         vp->rev.smFwRev = mb->un.varRdRev.un.smFwRev;
162         vp->rev.endecRev = mb->un.varRdRev.endecRev;
163         vp->rev.fcphHigh = mb->un.varRdRev.fcphHigh;
164         vp->rev.fcphLow = mb->un.varRdRev.fcphLow;
165         vp->rev.feaLevelHigh = mb->un.varRdRev.feaLevelHigh;
166         vp->rev.feaLevelLow = mb->un.varRdRev.feaLevelLow;
167         vp->rev.postKernRev = mb->un.varRdRev.postKernRev;
168         vp->rev.opFwRev = mb->un.varRdRev.opFwRev;
169
170         if (lpfc_is_LC_HBA(phba->pcidev->device))
171                 memcpy(phba->RandomData, (char *)&mb->un.varWords[24],
172                                                 sizeof (phba->RandomData));
173
174         /* Get the default values for Model Name and Description */
175         lpfc_get_hba_model_desc(phba, phba->ModelName, phba->ModelDesc);
176
177         /* Get adapter VPD information */
178         pmb->context2 = kmalloc(DMP_RSP_SIZE, GFP_KERNEL);
179         if (!pmb->context2)
180                 goto out_free_mbox;
181         lpfc_vpd_data = kmalloc(DMP_VPD_SIZE, GFP_KERNEL);
182         if (!lpfc_vpd_data)
183                 goto out_free_context2;
184
185         do {
186                 lpfc_dump_mem(phba, pmb, offset);
187                 rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
188
189                 if (rc != MBX_SUCCESS) {
190                         lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
191                                         "%d:0441 VPD not present on adapter, "
192                                         "mbxCmd x%x DUMP VPD, mbxStatus x%x\n",
193                                         phba->brd_no,
194                                         mb->mbxCommand, mb->mbxStatus);
195                         kfree(lpfc_vpd_data);
196                         lpfc_vpd_data = NULL;
197                         break;
198                 }
199
200                 lpfc_sli_pcimem_bcopy(pmb->context2, lpfc_vpd_data + offset,
201                                                         mb->un.varDmp.word_cnt);
202                 offset += mb->un.varDmp.word_cnt;
203         } while (mb->un.varDmp.word_cnt);
204         lpfc_parse_vpd(phba, lpfc_vpd_data);
205
206         kfree(lpfc_vpd_data);
207 out_free_context2:
208         kfree(pmb->context2);
209 out_free_mbox:
210         mempool_free(pmb, phba->mbox_mem_pool);
211         return 0;
212 }
213
214 /************************************************************************/
215 /*                                                                      */
216 /*    lpfc_config_port_post                                             */
217 /*    This routine will do LPFC initialization after the                */
218 /*    CONFIG_PORT mailbox command. This will be initialized             */
219 /*    as a SLI layer callback routine.                                  */
220 /*    This routine returns 0 on success. Any other return value         */
221 /*    indicates an error.                                               */
222 /*                                                                      */
223 /************************************************************************/
224 int
225 lpfc_config_port_post(struct lpfc_hba * phba)
226 {
227         LPFC_MBOXQ_t *pmb;
228         MAILBOX_t *mb;
229         struct lpfc_dmabuf *mp;
230         struct lpfc_sli *psli = &phba->sli;
231         uint32_t status, timeout;
232         int i, j, rc;
233
234         pmb = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
235         if (!pmb) {
236                 phba->hba_state = LPFC_HBA_ERROR;
237                 return -ENOMEM;
238         }
239         mb = &pmb->mb;
240
241         lpfc_config_link(phba, pmb);
242         rc = lpfc_sli_issue_mbox(phba, pmb, MBX_POLL);
243         if (rc != MBX_SUCCESS) {
244                 lpfc_printf_log(phba,
245                                 KERN_ERR,
246                                 LOG_INIT,
247                                 "%d:0447 Adapter failed init, mbxCmd x%x "
248                                 "CONFIG_LINK mbxStatus x%x\n",
249                                 phba->brd_no,
250                                 mb->mbxCommand, mb->mbxStatus);
251                 phba->hba_state = LPFC_HBA_ERROR;
252                 mempool_free( pmb, phba->mbox_mem_pool);
253                 return -EIO;
254         }
255
256         /* Get login parameters for NID.  */
257         lpfc_read_sparam(phba, pmb);
258         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
259                 lpfc_printf_log(phba,
260                                 KERN_ERR,
261                                 LOG_INIT,
262                                 "%d:0448 Adapter failed init, mbxCmd x%x "
263                                 "READ_SPARM mbxStatus x%x\n",
264                                 phba->brd_no,
265                                 mb->mbxCommand, mb->mbxStatus);
266                 phba->hba_state = LPFC_HBA_ERROR;
267                 mp = (struct lpfc_dmabuf *) pmb->context1;
268                 mempool_free( pmb, phba->mbox_mem_pool);
269                 lpfc_mbuf_free(phba, mp->virt, mp->phys);
270                 kfree(mp);
271                 return -EIO;
272         }
273
274         mp = (struct lpfc_dmabuf *) pmb->context1;
275
276         memcpy(&phba->fc_sparam, mp->virt, sizeof (struct serv_parm));
277         lpfc_mbuf_free(phba, mp->virt, mp->phys);
278         kfree(mp);
279         pmb->context1 = NULL;
280
281         memcpy(&phba->fc_nodename, &phba->fc_sparam.nodeName,
282                sizeof (struct lpfc_name));
283         memcpy(&phba->fc_portname, &phba->fc_sparam.portName,
284                sizeof (struct lpfc_name));
285         /* If no serial number in VPD data, use low 6 bytes of WWNN */
286         /* This should be consolidated into parse_vpd ? - mr */
287         if (phba->SerialNumber[0] == 0) {
288                 uint8_t *outptr;
289
290                 outptr = (uint8_t *) & phba->fc_nodename.IEEE[0];
291                 for (i = 0; i < 12; i++) {
292                         status = *outptr++;
293                         j = ((status & 0xf0) >> 4);
294                         if (j <= 9)
295                                 phba->SerialNumber[i] =
296                                     (char)((uint8_t) 0x30 + (uint8_t) j);
297                         else
298                                 phba->SerialNumber[i] =
299                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
300                         i++;
301                         j = (status & 0xf);
302                         if (j <= 9)
303                                 phba->SerialNumber[i] =
304                                     (char)((uint8_t) 0x30 + (uint8_t) j);
305                         else
306                                 phba->SerialNumber[i] =
307                                     (char)((uint8_t) 0x61 + (uint8_t) (j - 10));
308                 }
309         }
310
311         /* This should turn on DELAYED ABTS for ELS timeouts */
312         lpfc_set_slim(phba, pmb, 0x052198, 0x1);
313         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
314                 phba->hba_state = LPFC_HBA_ERROR;
315                 mempool_free( pmb, phba->mbox_mem_pool);
316                 return -EIO;
317         }
318
319
320         lpfc_read_config(phba, pmb);
321         if (lpfc_sli_issue_mbox(phba, pmb, MBX_POLL) != MBX_SUCCESS) {
322                 lpfc_printf_log(phba,
323                                 KERN_ERR,
324                                 LOG_INIT,
325                                 "%d:0453 Adapter failed to init, mbxCmd x%x "
326                                 "READ_CONFIG, mbxStatus x%x\n",
327                                 phba->brd_no,
328                                 mb->mbxCommand, mb->mbxStatus);
329                 phba->hba_state = LPFC_HBA_ERROR;
330                 mempool_free( pmb, phba->mbox_mem_pool);
331                 return -EIO;
332         }
333
334         /* Reset the DFT_HBA_Q_DEPTH to the max xri  */
335         if (phba->cfg_hba_queue_depth > (mb->un.varRdConfig.max_xri+1))
336                 phba->cfg_hba_queue_depth =
337                         mb->un.varRdConfig.max_xri + 1;
338
339         phba->lmt = mb->un.varRdConfig.lmt;
340         /* HBA is not 4GB capable, or HBA is not 2GB capable,
341         don't let link speed ask for it */
342         if ((((phba->lmt & LMT_4250_10bit) != LMT_4250_10bit) &&
343                 (phba->cfg_link_speed > LINK_SPEED_2G)) ||
344                 (((phba->lmt & LMT_2125_10bit) != LMT_2125_10bit) &&
345                 (phba->cfg_link_speed > LINK_SPEED_1G))) {
346                 /* Reset link speed to auto. 1G/2GB HBA cfg'd for 4G */
347                 lpfc_printf_log(phba,
348                         KERN_WARNING,
349                         LOG_LINK_EVENT,
350                         "%d:1302 Invalid speed for this board: "
351                         "Reset link speed to auto: x%x\n",
352                         phba->brd_no,
353                         phba->cfg_link_speed);
354                         phba->cfg_link_speed = LINK_SPEED_AUTO;
355         }
356
357         phba->hba_state = LPFC_LINK_DOWN;
358
359         /* Only process IOCBs on ring 0 till hba_state is READY */
360         if (psli->ring[psli->ip_ring].cmdringaddr)
361                 psli->ring[psli->ip_ring].flag |= LPFC_STOP_IOCB_EVENT;
362         if (psli->ring[psli->fcp_ring].cmdringaddr)
363                 psli->ring[psli->fcp_ring].flag |= LPFC_STOP_IOCB_EVENT;
364         if (psli->ring[psli->next_ring].cmdringaddr)
365                 psli->ring[psli->next_ring].flag |= LPFC_STOP_IOCB_EVENT;
366
367         /* Post receive buffers for desired rings */
368         lpfc_post_rcv_buf(phba);
369
370         /* Enable appropriate host interrupts */
371         spin_lock_irq(phba->host->host_lock);
372         status = readl(phba->HCregaddr);
373         status |= HC_MBINT_ENA | HC_ERINT_ENA | HC_LAINT_ENA;
374         if (psli->num_rings > 0)
375                 status |= HC_R0INT_ENA;
376         if (psli->num_rings > 1)
377                 status |= HC_R1INT_ENA;
378         if (psli->num_rings > 2)
379                 status |= HC_R2INT_ENA;
380         if (psli->num_rings > 3)
381                 status |= HC_R3INT_ENA;
382
383         writel(status, phba->HCregaddr);
384         readl(phba->HCregaddr); /* flush */
385         spin_unlock_irq(phba->host->host_lock);
386
387         /*
388          * Setup the ring 0 (els)  timeout handler
389          */
390         timeout = phba->fc_ratov << 1;
391         phba->els_tmofunc.expires = jiffies + HZ * timeout;
392         add_timer(&phba->els_tmofunc);
393
394         lpfc_init_link(phba, pmb, phba->cfg_topology, phba->cfg_link_speed);
395         pmb->mbox_cmpl = lpfc_sli_def_mbox_cmpl;
396         if (lpfc_sli_issue_mbox(phba, pmb, MBX_NOWAIT) != MBX_SUCCESS) {
397                 lpfc_printf_log(phba,
398                                 KERN_ERR,
399                                 LOG_INIT,
400                                 "%d:0454 Adapter failed to init, mbxCmd x%x "
401                                 "INIT_LINK, mbxStatus x%x\n",
402                                 phba->brd_no,
403                                 mb->mbxCommand, mb->mbxStatus);
404
405                 /* Clear all interrupt enable conditions */
406                 writel(0, phba->HCregaddr);
407                 readl(phba->HCregaddr); /* flush */
408                 /* Clear all pending interrupts */
409                 writel(0xffffffff, phba->HAregaddr);
410                 readl(phba->HAregaddr); /* flush */
411
412                 phba->hba_state = LPFC_HBA_ERROR;
413                 mempool_free(pmb, phba->mbox_mem_pool);
414                 return -EIO;
415         }
416         /* MBOX buffer will be freed in mbox compl */
417
418         i = 0;
419         while ((phba->hba_state != LPFC_HBA_READY) ||
420                (phba->num_disc_nodes) || (phba->fc_prli_sent) ||
421                ((phba->fc_map_cnt == 0) && (i<2)) ||
422                (psli->sli_flag & LPFC_SLI_MBOX_ACTIVE)) {
423                 /* Check every second for 30 retries. */
424                 i++;
425                 if (i > 30) {
426                         break;
427                 }
428                 if ((i >= 15) && (phba->hba_state <= LPFC_LINK_DOWN)) {
429                         /* The link is down.  Set linkdown timeout */
430                         break;
431                 }
432
433                 /* Delay for 1 second to give discovery time to complete. */
434                 msleep(1000);
435
436         }
437
438         /* Since num_disc_nodes keys off of PLOGI, delay a bit to let
439          * any potential PRLIs to flush thru the SLI sub-system.
440          */
441         msleep(50);
442
443         return (0);
444 }
445
446 /************************************************************************/
447 /*                                                                      */
448 /*    lpfc_hba_down_prep                                                */
449 /*    This routine will do LPFC uninitialization before the             */
450 /*    HBA is reset when bringing down the SLI Layer. This will be       */
451 /*    initialized as a SLI layer callback routine.                      */
452 /*    This routine returns 0 on success. Any other return value         */
453 /*    indicates an error.                                               */
454 /*                                                                      */
455 /************************************************************************/
456 int
457 lpfc_hba_down_prep(struct lpfc_hba * phba)
458 {
459         /* Disable interrupts */
460         writel(0, phba->HCregaddr);
461         readl(phba->HCregaddr); /* flush */
462
463         /* Cleanup potential discovery resources */
464         lpfc_els_flush_rscn(phba);
465         lpfc_els_flush_cmd(phba);
466         lpfc_disc_flush_list(phba);
467
468         return (0);
469 }
470
471 /************************************************************************/
472 /*                                                                      */
473 /*    lpfc_handle_eratt                                                 */
474 /*    This routine will handle processing a Host Attention              */
475 /*    Error Status event. This will be initialized                      */
476 /*    as a SLI layer callback routine.                                  */
477 /*                                                                      */
478 /************************************************************************/
479 void
480 lpfc_handle_eratt(struct lpfc_hba * phba)
481 {
482         struct lpfc_sli *psli = &phba->sli;
483         struct lpfc_sli_ring  *pring;
484
485         /*
486          * If a reset is sent to the HBA restore PCI configuration registers.
487          */
488         if ( phba->hba_state == LPFC_INIT_START ) {
489                 mdelay(1);
490                 readl(phba->HCregaddr); /* flush */
491                 writel(0, phba->HCregaddr);
492                 readl(phba->HCregaddr); /* flush */
493
494                 /* Restore PCI cmd register */
495                 pci_write_config_word(phba->pcidev,
496                                       PCI_COMMAND, phba->pci_cfg_value);
497         }
498
499         if (phba->work_hs & HS_FFER6) {
500                 /* Re-establishing Link */
501                 lpfc_printf_log(phba, KERN_INFO, LOG_LINK_EVENT,
502                                 "%d:1301 Re-establishing Link "
503                                 "Data: x%x x%x x%x\n",
504                                 phba->brd_no, phba->work_hs,
505                                 phba->work_status[0], phba->work_status[1]);
506                 spin_lock_irq(phba->host->host_lock);
507                 phba->fc_flag |= FC_ESTABLISH_LINK;
508                 spin_unlock_irq(phba->host->host_lock);
509
510                 /*
511                 * Firmware stops when it triggled erratt with HS_FFER6.
512                 * That could cause the I/Os dropped by the firmware.
513                 * Error iocb (I/O) on txcmplq and let the SCSI layer
514                 * retry it after re-establishing link.
515                 */
516                 pring = &psli->ring[psli->fcp_ring];
517                 lpfc_sli_abort_iocb_ring(phba, pring);
518
519
520                 /*
521                  * There was a firmware error.  Take the hba offline and then
522                  * attempt to restart it.
523                  */
524                 lpfc_offline(phba);
525                 if (lpfc_online(phba) == 0) {   /* Initialize the HBA */
526                         mod_timer(&phba->fc_estabtmo, jiffies + HZ * 60);
527                         return;
528                 }
529         } else {
530                 /* The if clause above forces this code path when the status
531                  * failure is a value other than FFER6.  Do not call the offline
532                  *  twice. This is the adapter hardware error path.
533                  */
534                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
535                                 "%d:0457 Adapter Hardware Error "
536                                 "Data: x%x x%x x%x\n",
537                                 phba->brd_no, phba->work_hs,
538                                 phba->work_status[0], phba->work_status[1]);
539
540                 lpfc_offline(phba);
541
542                 /*
543                  * Restart all traffic to this host.  Since the fc_transport
544                  * block functions (future) were not called in lpfc_offline,
545                  * don't call them here.
546                  */
547                 scsi_unblock_requests(phba->host);
548         }
549 }
550
551 /************************************************************************/
552 /*                                                                      */
553 /*    lpfc_handle_latt                                                  */
554 /*    This routine will handle processing a Host Attention              */
555 /*    Link Status event. This will be initialized                       */
556 /*    as a SLI layer callback routine.                                  */
557 /*                                                                      */
558 /************************************************************************/
559 void
560 lpfc_handle_latt(struct lpfc_hba * phba)
561 {
562         struct lpfc_sli *psli = &phba->sli;
563         LPFC_MBOXQ_t *pmb;
564         volatile uint32_t control;
565         struct lpfc_dmabuf *mp;
566         int rc = -ENOMEM;
567
568         pmb = (LPFC_MBOXQ_t *)mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
569         if (!pmb)
570                 goto lpfc_handle_latt_err_exit;
571
572         mp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
573         if (!mp)
574                 goto lpfc_handle_latt_free_pmb;
575
576         mp->virt = lpfc_mbuf_alloc(phba, 0, &mp->phys);
577         if (!mp->virt)
578                 goto lpfc_handle_latt_free_mp;
579
580         rc = -EIO;
581
582
583         psli->slistat.link_event++;
584         lpfc_read_la(phba, pmb, mp);
585         pmb->mbox_cmpl = lpfc_mbx_cmpl_read_la;
586         rc = lpfc_sli_issue_mbox (phba, pmb, (MBX_NOWAIT | MBX_STOP_IOCB));
587         if (rc == MBX_NOT_FINISHED)
588                 goto lpfc_handle_latt_free_mp;
589
590         /* Clear Link Attention in HA REG */
591         spin_lock_irq(phba->host->host_lock);
592         writel(HA_LATT, phba->HAregaddr);
593         readl(phba->HAregaddr); /* flush */
594         spin_unlock_irq(phba->host->host_lock);
595
596         return;
597
598 lpfc_handle_latt_free_mp:
599         kfree(mp);
600 lpfc_handle_latt_free_pmb:
601         kfree(pmb);
602 lpfc_handle_latt_err_exit:
603         /* Enable Link attention interrupts */
604         spin_lock_irq(phba->host->host_lock);
605         psli->sli_flag |= LPFC_PROCESS_LA;
606         control = readl(phba->HCregaddr);
607         control |= HC_LAINT_ENA;
608         writel(control, phba->HCregaddr);
609         readl(phba->HCregaddr); /* flush */
610
611         /* Clear Link Attention in HA REG */
612         writel(HA_LATT, phba->HAregaddr);
613         readl(phba->HAregaddr); /* flush */
614         spin_unlock_irq(phba->host->host_lock);
615         lpfc_linkdown(phba);
616         phba->hba_state = LPFC_HBA_ERROR;
617
618         /* The other case is an error from issue_mbox */
619         if (rc == -ENOMEM)
620                 lpfc_printf_log(phba,
621                                 KERN_WARNING,
622                                 LOG_MBOX,
623                                 "%d:0300 READ_LA: no buffers\n",
624                                 phba->brd_no);
625
626         return;
627 }
628
629 /************************************************************************/
630 /*                                                                      */
631 /*   lpfc_parse_vpd                                                     */
632 /*   This routine will parse the VPD data                               */
633 /*                                                                      */
634 /************************************************************************/
635 static int
636 lpfc_parse_vpd(struct lpfc_hba * phba, uint8_t * vpd)
637 {
638         uint8_t lenlo, lenhi;
639         uint32_t Length;
640         int i, j;
641         int finished = 0;
642         int index = 0;
643
644         if (!vpd)
645                 return 0;
646
647         /* Vital Product */
648         lpfc_printf_log(phba,
649                         KERN_INFO,
650                         LOG_INIT,
651                         "%d:0455 Vital Product Data: x%x x%x x%x x%x\n",
652                         phba->brd_no,
653                         (uint32_t) vpd[0], (uint32_t) vpd[1], (uint32_t) vpd[2],
654                         (uint32_t) vpd[3]);
655         do {
656                 switch (vpd[index]) {
657                 case 0x82:
658                         index += 1;
659                         lenlo = vpd[index];
660                         index += 1;
661                         lenhi = vpd[index];
662                         index += 1;
663                         i = ((((unsigned short)lenhi) << 8) + lenlo);
664                         index += i;
665                         break;
666                 case 0x90:
667                         index += 1;
668                         lenlo = vpd[index];
669                         index += 1;
670                         lenhi = vpd[index];
671                         index += 1;
672                         Length = ((((unsigned short)lenhi) << 8) + lenlo);
673
674                         while (Length > 0) {
675                         /* Look for Serial Number */
676                         if ((vpd[index] == 'S') && (vpd[index+1] == 'N')) {
677                                 index += 2;
678                                 i = vpd[index];
679                                 index += 1;
680                                 j = 0;
681                                 Length -= (3+i);
682                                 while(i--) {
683                                         phba->SerialNumber[j++] = vpd[index++];
684                                         if (j == 31)
685                                                 break;
686                                 }
687                                 phba->SerialNumber[j] = 0;
688                                 continue;
689                         }
690                         else if ((vpd[index] == 'V') && (vpd[index+1] == '1')) {
691                                 phba->vpd_flag |= VPD_MODEL_DESC;
692                                 index += 2;
693                                 i = vpd[index];
694                                 index += 1;
695                                 j = 0;
696                                 Length -= (3+i);
697                                 while(i--) {
698                                         phba->ModelDesc[j++] = vpd[index++];
699                                         if (j == 255)
700                                                 break;
701                                 }
702                                 phba->ModelDesc[j] = 0;
703                                 continue;
704                         }
705                         else if ((vpd[index] == 'V') && (vpd[index+1] == '2')) {
706                                 phba->vpd_flag |= VPD_MODEL_NAME;
707                                 index += 2;
708                                 i = vpd[index];
709                                 index += 1;
710                                 j = 0;
711                                 Length -= (3+i);
712                                 while(i--) {
713                                         phba->ModelName[j++] = vpd[index++];
714                                         if (j == 79)
715                                                 break;
716                                 }
717                                 phba->ModelName[j] = 0;
718                                 continue;
719                         }
720                         else if ((vpd[index] == 'V') && (vpd[index+1] == '3')) {
721                                 phba->vpd_flag |= VPD_PROGRAM_TYPE;
722                                 index += 2;
723                                 i = vpd[index];
724                                 index += 1;
725                                 j = 0;
726                                 Length -= (3+i);
727                                 while(i--) {
728                                         phba->ProgramType[j++] = vpd[index++];
729                                         if (j == 255)
730                                                 break;
731                                 }
732                                 phba->ProgramType[j] = 0;
733                                 continue;
734                         }
735                         else if ((vpd[index] == 'V') && (vpd[index+1] == '4')) {
736                                 phba->vpd_flag |= VPD_PORT;
737                                 index += 2;
738                                 i = vpd[index];
739                                 index += 1;
740                                 j = 0;
741                                 Length -= (3+i);
742                                 while(i--) {
743                                 phba->Port[j++] = vpd[index++];
744                                 if (j == 19)
745                                         break;
746                                 }
747                                 phba->Port[j] = 0;
748                                 continue;
749                         }
750                         else {
751                                 index += 2;
752                                 i = vpd[index];
753                                 index += 1;
754                                 index += i;
755                                 Length -= (3 + i);
756                         }
757                 }
758                 finished = 0;
759                 break;
760                 case 0x78:
761                         finished = 1;
762                         break;
763                 default:
764                         index ++;
765                         break;
766                 }
767         } while (!finished && (index < 108));
768
769         return(1);
770 }
771
772 static void
773 lpfc_get_hba_model_desc(struct lpfc_hba * phba, uint8_t * mdp, uint8_t * descp)
774 {
775         lpfc_vpd_t *vp;
776         uint32_t id;
777         char str[16];
778
779         vp = &phba->vpd;
780         pci_read_config_dword(phba->pcidev, PCI_VENDOR_ID, &id);
781
782         switch ((id >> 16) & 0xffff) {
783         case PCI_DEVICE_ID_FIREFLY:
784                 strcpy(str, "LP6000 1");
785                 break;
786         case PCI_DEVICE_ID_SUPERFLY:
787                 if (vp->rev.biuRev >= 1 && vp->rev.biuRev <= 3)
788                         strcpy(str, "LP7000 1");
789                 else
790                         strcpy(str, "LP7000E 1");
791                 break;
792         case PCI_DEVICE_ID_DRAGONFLY:
793                 strcpy(str, "LP8000 1");
794                 break;
795         case PCI_DEVICE_ID_CENTAUR:
796                 if (FC_JEDEC_ID(vp->rev.biuRev) == CENTAUR_2G_JEDEC_ID)
797                         strcpy(str, "LP9002 2");
798                 else
799                         strcpy(str, "LP9000 1");
800                 break;
801         case PCI_DEVICE_ID_RFLY:
802                 strcpy(str, "LP952 2");
803                 break;
804         case PCI_DEVICE_ID_PEGASUS:
805                 strcpy(str, "LP9802 2");
806                 break;
807         case PCI_DEVICE_ID_THOR:
808                 strcpy(str, "LP10000 2");
809                 break;
810         case PCI_DEVICE_ID_VIPER:
811                 strcpy(str, "LPX1000 10");
812                 break;
813         case PCI_DEVICE_ID_PFLY:
814                 strcpy(str, "LP982 2");
815                 break;
816         case PCI_DEVICE_ID_TFLY:
817                 strcpy(str, "LP1050 2");
818                 break;
819         case PCI_DEVICE_ID_HELIOS:
820                 strcpy(str, "LP11000 4");
821                 break;
822         case PCI_DEVICE_ID_BMID:
823                 strcpy(str, "LP1150 4");
824                 break;
825         case PCI_DEVICE_ID_BSMB:
826                 strcpy(str, "LP111 4");
827                 break;
828         case PCI_DEVICE_ID_ZEPHYR:
829                 strcpy(str, "LP11000e 4");
830                 break;
831         case PCI_DEVICE_ID_ZMID:
832                 strcpy(str, "LP1150e 4");
833                 break;
834         case PCI_DEVICE_ID_ZSMB:
835                 strcpy(str, "LP111e 4");
836                 break;
837         case PCI_DEVICE_ID_LP101:
838                 strcpy(str, "LP101 2");
839                 break;
840         case PCI_DEVICE_ID_LP10000S:
841                 strcpy(str, "LP10000-S 2");
842                 break;
843         default:
844                 memset(str, 0, 16);
845                 break;
846         }
847         if (mdp)
848                 sscanf(str, "%s", mdp);
849         if (descp)
850                 sprintf(descp, "Emulex LightPulse %s Gigabit PCI Fibre "
851                         "Channel Adapter", str);
852 }
853
854 /**************************************************/
855 /*   lpfc_post_buffer                             */
856 /*                                                */
857 /*   This routine will post count buffers to the  */
858 /*   ring with the QUE_RING_BUF_CN command. This  */
859 /*   allows 3 buffers / command to be posted.     */
860 /*   Returns the number of buffers NOT posted.    */
861 /**************************************************/
862 int
863 lpfc_post_buffer(struct lpfc_hba * phba, struct lpfc_sli_ring * pring, int cnt,
864                  int type)
865 {
866         IOCB_t *icmd;
867         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
868         struct lpfc_iocbq *iocb = NULL;
869         struct lpfc_dmabuf *mp1, *mp2;
870
871         cnt += pring->missbufcnt;
872
873         /* While there are buffers to post */
874         while (cnt > 0) {
875                 /* Allocate buffer for  command iocb */
876                 spin_lock_irq(phba->host->host_lock);
877                 list_remove_head(lpfc_iocb_list, iocb, struct lpfc_iocbq, list);
878                 spin_unlock_irq(phba->host->host_lock);
879                 if (iocb == NULL) {
880                         pring->missbufcnt = cnt;
881                         return cnt;
882                 }
883                 memset(iocb, 0, sizeof (struct lpfc_iocbq));
884                 icmd = &iocb->iocb;
885
886                 /* 2 buffers can be posted per command */
887                 /* Allocate buffer to post */
888                 mp1 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
889                 if (mp1)
890                     mp1->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
891                                                 &mp1->phys);
892                 if (mp1 == 0 || mp1->virt == 0) {
893                         if (mp1)
894                                 kfree(mp1);
895                         spin_lock_irq(phba->host->host_lock);
896                         list_add_tail(&iocb->list, lpfc_iocb_list);
897                         spin_unlock_irq(phba->host->host_lock);
898                         pring->missbufcnt = cnt;
899                         return cnt;
900                 }
901
902                 INIT_LIST_HEAD(&mp1->list);
903                 /* Allocate buffer to post */
904                 if (cnt > 1) {
905                         mp2 = kmalloc(sizeof (struct lpfc_dmabuf), GFP_KERNEL);
906                         if (mp2)
907                                 mp2->virt = lpfc_mbuf_alloc(phba, MEM_PRI,
908                                                             &mp2->phys);
909                         if (mp2 == 0 || mp2->virt == 0) {
910                                 if (mp2)
911                                         kfree(mp2);
912                                 lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
913                                 kfree(mp1);
914                                 spin_lock_irq(phba->host->host_lock);
915                                 list_add_tail(&iocb->list, lpfc_iocb_list);
916                                 spin_unlock_irq(phba->host->host_lock);
917                                 pring->missbufcnt = cnt;
918                                 return cnt;
919                         }
920
921                         INIT_LIST_HEAD(&mp2->list);
922                 } else {
923                         mp2 = NULL;
924                 }
925
926                 icmd->un.cont64[0].addrHigh = putPaddrHigh(mp1->phys);
927                 icmd->un.cont64[0].addrLow = putPaddrLow(mp1->phys);
928                 icmd->un.cont64[0].tus.f.bdeSize = FCELSSIZE;
929                 icmd->ulpBdeCount = 1;
930                 cnt--;
931                 if (mp2) {
932                         icmd->un.cont64[1].addrHigh = putPaddrHigh(mp2->phys);
933                         icmd->un.cont64[1].addrLow = putPaddrLow(mp2->phys);
934                         icmd->un.cont64[1].tus.f.bdeSize = FCELSSIZE;
935                         cnt--;
936                         icmd->ulpBdeCount = 2;
937                 }
938
939                 icmd->ulpCommand = CMD_QUE_RING_BUF64_CN;
940                 icmd->ulpLe = 1;
941
942                 spin_lock_irq(phba->host->host_lock);
943                 if (lpfc_sli_issue_iocb(phba, pring, iocb, 0) == IOCB_ERROR) {
944                         lpfc_mbuf_free(phba, mp1->virt, mp1->phys);
945                         kfree(mp1);
946                         cnt++;
947                         if (mp2) {
948                                 lpfc_mbuf_free(phba, mp2->virt, mp2->phys);
949                                 kfree(mp2);
950                                 cnt++;
951                         }
952                         list_add_tail(&iocb->list, lpfc_iocb_list);
953                         pring->missbufcnt = cnt;
954                         spin_unlock_irq(phba->host->host_lock);
955                         return cnt;
956                 }
957                 spin_unlock_irq(phba->host->host_lock);
958                 lpfc_sli_ringpostbuf_put(phba, pring, mp1);
959                 if (mp2) {
960                         lpfc_sli_ringpostbuf_put(phba, pring, mp2);
961                 }
962         }
963         pring->missbufcnt = 0;
964         return 0;
965 }
966
967 /************************************************************************/
968 /*                                                                      */
969 /*   lpfc_post_rcv_buf                                                  */
970 /*   This routine post initial rcv buffers to the configured rings      */
971 /*                                                                      */
972 /************************************************************************/
973 static int
974 lpfc_post_rcv_buf(struct lpfc_hba * phba)
975 {
976         struct lpfc_sli *psli = &phba->sli;
977
978         /* Ring 0, ELS / CT buffers */
979         lpfc_post_buffer(phba, &psli->ring[LPFC_ELS_RING], LPFC_BUF_RING0, 1);
980         /* Ring 2 - FCP no buffers needed */
981
982         return 0;
983 }
984
985 #define S(N,V) (((V)<<(N))|((V)>>(32-(N))))
986
987 /************************************************************************/
988 /*                                                                      */
989 /*   lpfc_sha_init                                                      */
990 /*                                                                      */
991 /************************************************************************/
992 static void
993 lpfc_sha_init(uint32_t * HashResultPointer)
994 {
995         HashResultPointer[0] = 0x67452301;
996         HashResultPointer[1] = 0xEFCDAB89;
997         HashResultPointer[2] = 0x98BADCFE;
998         HashResultPointer[3] = 0x10325476;
999         HashResultPointer[4] = 0xC3D2E1F0;
1000 }
1001
1002 /************************************************************************/
1003 /*                                                                      */
1004 /*   lpfc_sha_iterate                                                   */
1005 /*                                                                      */
1006 /************************************************************************/
1007 static void
1008 lpfc_sha_iterate(uint32_t * HashResultPointer, uint32_t * HashWorkingPointer)
1009 {
1010         int t;
1011         uint32_t TEMP;
1012         uint32_t A, B, C, D, E;
1013         t = 16;
1014         do {
1015                 HashWorkingPointer[t] =
1016                     S(1,
1017                       HashWorkingPointer[t - 3] ^ HashWorkingPointer[t -
1018                                                                      8] ^
1019                       HashWorkingPointer[t - 14] ^ HashWorkingPointer[t - 16]);
1020         } while (++t <= 79);
1021         t = 0;
1022         A = HashResultPointer[0];
1023         B = HashResultPointer[1];
1024         C = HashResultPointer[2];
1025         D = HashResultPointer[3];
1026         E = HashResultPointer[4];
1027
1028         do {
1029                 if (t < 20) {
1030                         TEMP = ((B & C) | ((~B) & D)) + 0x5A827999;
1031                 } else if (t < 40) {
1032                         TEMP = (B ^ C ^ D) + 0x6ED9EBA1;
1033                 } else if (t < 60) {
1034                         TEMP = ((B & C) | (B & D) | (C & D)) + 0x8F1BBCDC;
1035                 } else {
1036                         TEMP = (B ^ C ^ D) + 0xCA62C1D6;
1037                 }
1038                 TEMP += S(5, A) + E + HashWorkingPointer[t];
1039                 E = D;
1040                 D = C;
1041                 C = S(30, B);
1042                 B = A;
1043                 A = TEMP;
1044         } while (++t <= 79);
1045
1046         HashResultPointer[0] += A;
1047         HashResultPointer[1] += B;
1048         HashResultPointer[2] += C;
1049         HashResultPointer[3] += D;
1050         HashResultPointer[4] += E;
1051
1052 }
1053
1054 /************************************************************************/
1055 /*                                                                      */
1056 /*   lpfc_challenge_key                                                 */
1057 /*                                                                      */
1058 /************************************************************************/
1059 static void
1060 lpfc_challenge_key(uint32_t * RandomChallenge, uint32_t * HashWorking)
1061 {
1062         *HashWorking = (*RandomChallenge ^ *HashWorking);
1063 }
1064
1065 /************************************************************************/
1066 /*                                                                      */
1067 /*   lpfc_hba_init                                                      */
1068 /*                                                                      */
1069 /************************************************************************/
1070 void
1071 lpfc_hba_init(struct lpfc_hba *phba, uint32_t *hbainit)
1072 {
1073         int t;
1074         uint32_t *HashWorking;
1075         uint32_t *pwwnn = phba->wwnn;
1076
1077         HashWorking = kmalloc(80 * sizeof(uint32_t), GFP_KERNEL);
1078         if (!HashWorking)
1079                 return;
1080
1081         memset(HashWorking, 0, (80 * sizeof(uint32_t)));
1082         HashWorking[0] = HashWorking[78] = *pwwnn++;
1083         HashWorking[1] = HashWorking[79] = *pwwnn;
1084
1085         for (t = 0; t < 7; t++)
1086                 lpfc_challenge_key(phba->RandomData + t, HashWorking + t);
1087
1088         lpfc_sha_init(hbainit);
1089         lpfc_sha_iterate(hbainit, HashWorking);
1090         kfree(HashWorking);
1091 }
1092
1093 static void
1094 lpfc_cleanup(struct lpfc_hba * phba, uint32_t save_bind)
1095 {
1096         struct lpfc_nodelist *ndlp, *next_ndlp;
1097
1098         /* clean up phba - lpfc specific */
1099         lpfc_can_disctmo(phba);
1100         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpunmap_list,
1101                                 nlp_listp) {
1102                 lpfc_nlp_remove(phba, ndlp);
1103         }
1104
1105         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_nlpmap_list,
1106                                  nlp_listp) {
1107                 lpfc_nlp_remove(phba, ndlp);
1108         }
1109
1110         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_unused_list,
1111                                 nlp_listp) {
1112                 lpfc_nlp_list(phba, ndlp, NLP_NO_LIST);
1113         }
1114
1115         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_plogi_list,
1116                                 nlp_listp) {
1117                 lpfc_nlp_remove(phba, ndlp);
1118         }
1119
1120         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_adisc_list,
1121                                 nlp_listp) {
1122                 lpfc_nlp_remove(phba, ndlp);
1123         }
1124
1125         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_reglogin_list,
1126                                 nlp_listp) {
1127                 lpfc_nlp_remove(phba, ndlp);
1128         }
1129
1130         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_prli_list,
1131                                 nlp_listp) {
1132                 lpfc_nlp_remove(phba, ndlp);
1133         }
1134
1135         list_for_each_entry_safe(ndlp, next_ndlp, &phba->fc_npr_list,
1136                                 nlp_listp) {
1137                 lpfc_nlp_remove(phba, ndlp);
1138         }
1139
1140         INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1141         INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1142         INIT_LIST_HEAD(&phba->fc_unused_list);
1143         INIT_LIST_HEAD(&phba->fc_plogi_list);
1144         INIT_LIST_HEAD(&phba->fc_adisc_list);
1145         INIT_LIST_HEAD(&phba->fc_reglogin_list);
1146         INIT_LIST_HEAD(&phba->fc_prli_list);
1147         INIT_LIST_HEAD(&phba->fc_npr_list);
1148
1149         phba->fc_map_cnt   = 0;
1150         phba->fc_unmap_cnt = 0;
1151         phba->fc_plogi_cnt = 0;
1152         phba->fc_adisc_cnt = 0;
1153         phba->fc_reglogin_cnt = 0;
1154         phba->fc_prli_cnt  = 0;
1155         phba->fc_npr_cnt   = 0;
1156         phba->fc_unused_cnt= 0;
1157         return;
1158 }
1159
1160 static void
1161 lpfc_establish_link_tmo(unsigned long ptr)
1162 {
1163         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
1164         unsigned long iflag;
1165
1166
1167         /* Re-establishing Link, timer expired */
1168         lpfc_printf_log(phba, KERN_ERR, LOG_LINK_EVENT,
1169                         "%d:1300 Re-establishing Link, timer expired "
1170                         "Data: x%x x%x\n",
1171                         phba->brd_no, phba->fc_flag, phba->hba_state);
1172         spin_lock_irqsave(phba->host->host_lock, iflag);
1173         phba->fc_flag &= ~FC_ESTABLISH_LINK;
1174         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1175 }
1176
1177 static int
1178 lpfc_stop_timer(struct lpfc_hba * phba)
1179 {
1180         struct lpfc_sli *psli = &phba->sli;
1181
1182         /* Instead of a timer, this has been converted to a
1183          * deferred procedding list.
1184          */
1185         while (!list_empty(&phba->freebufList)) {
1186
1187                 struct lpfc_dmabuf *mp = NULL;
1188
1189                 list_remove_head((&phba->freebufList), mp,
1190                                  struct lpfc_dmabuf, list);
1191                 if (mp) {
1192                         lpfc_mbuf_free(phba, mp->virt, mp->phys);
1193                         kfree(mp);
1194                 }
1195         }
1196
1197         del_timer_sync(&phba->fc_estabtmo);
1198         del_timer_sync(&phba->fc_disctmo);
1199         del_timer_sync(&phba->fc_fdmitmo);
1200         del_timer_sync(&phba->els_tmofunc);
1201         psli = &phba->sli;
1202         del_timer_sync(&psli->mbox_tmo);
1203         return(1);
1204 }
1205
1206 int
1207 lpfc_online(struct lpfc_hba * phba)
1208 {
1209         if (!phba)
1210                 return 0;
1211
1212         if (!(phba->fc_flag & FC_OFFLINE_MODE))
1213                 return 0;
1214
1215         lpfc_printf_log(phba,
1216                        KERN_WARNING,
1217                        LOG_INIT,
1218                        "%d:0458 Bring Adapter online\n",
1219                        phba->brd_no);
1220
1221         if (!lpfc_sli_queue_setup(phba))
1222                 return 1;
1223
1224         if (lpfc_sli_hba_setup(phba))   /* Initialize the HBA */
1225                 return 1;
1226
1227         spin_lock_irq(phba->host->host_lock);
1228         phba->fc_flag &= ~FC_OFFLINE_MODE;
1229         spin_unlock_irq(phba->host->host_lock);
1230
1231         /*
1232          * Restart all traffic to this host.  Since the fc_transport block
1233          * functions (future) were not called in lpfc_offline, don't call them
1234          * here.
1235          */
1236         scsi_unblock_requests(phba->host);
1237         return 0;
1238 }
1239
1240 int
1241 lpfc_offline(struct lpfc_hba * phba)
1242 {
1243         struct lpfc_sli_ring *pring;
1244         struct lpfc_sli *psli;
1245         unsigned long iflag;
1246         int i = 0;
1247
1248         if (!phba)
1249                 return 0;
1250
1251         if (phba->fc_flag & FC_OFFLINE_MODE)
1252                 return 0;
1253
1254         /*
1255          * Don't call the fc_transport block api (future).  The device is
1256          * going offline and causing a timer to fire in the midlayer is
1257          * unproductive.  Just block all new requests until the driver
1258          * comes back online.
1259          */
1260         scsi_block_requests(phba->host);
1261         psli = &phba->sli;
1262         pring = &psli->ring[psli->fcp_ring];
1263
1264         lpfc_linkdown(phba);
1265
1266         /* The linkdown event takes 30 seconds to timeout. */
1267         while (pring->txcmplq_cnt) {
1268                 mdelay(10);
1269                 if (i++ > 3000)
1270                         break;
1271         }
1272
1273         /* stop all timers associated with this hba */
1274         lpfc_stop_timer(phba);
1275         phba->work_hba_events = 0;
1276
1277         lpfc_printf_log(phba,
1278                        KERN_WARNING,
1279                        LOG_INIT,
1280                        "%d:0460 Bring Adapter offline\n",
1281                        phba->brd_no);
1282
1283         /* Bring down the SLI Layer and cleanup.  The HBA is offline
1284            now.  */
1285         lpfc_sli_hba_down(phba);
1286         lpfc_cleanup(phba, 1);
1287         spin_lock_irqsave(phba->host->host_lock, iflag);
1288         phba->fc_flag |= FC_OFFLINE_MODE;
1289         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1290         return 0;
1291 }
1292
1293 /******************************************************************************
1294 * Function name: lpfc_scsi_free
1295 *
1296 * Description: Called from lpfc_pci_remove_one free internal driver resources
1297 *
1298 ******************************************************************************/
1299 static int
1300 lpfc_scsi_free(struct lpfc_hba * phba)
1301 {
1302         struct lpfc_scsi_buf *sb, *sb_next;
1303         struct lpfc_iocbq *io, *io_next;
1304
1305         spin_lock_irq(phba->host->host_lock);
1306         /* Release all the lpfc_scsi_bufs maintained by this host. */
1307         list_for_each_entry_safe(sb, sb_next, &phba->lpfc_scsi_buf_list, list) {
1308                 list_del(&sb->list);
1309                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool, sb->data,
1310                                                                 sb->dma_handle);
1311                 kfree(sb);
1312                 phba->total_scsi_bufs--;
1313         }
1314
1315         /* Release all the lpfc_iocbq entries maintained by this host. */
1316         list_for_each_entry_safe(io, io_next, &phba->lpfc_iocb_list, list) {
1317                 list_del(&io->list);
1318                 kfree(io);
1319                 phba->total_iocbq_bufs--;
1320         }
1321
1322         spin_unlock_irq(phba->host->host_lock);
1323
1324         return 0;
1325 }
1326
1327
1328 static int __devinit
1329 lpfc_pci_probe_one(struct pci_dev *pdev, const struct pci_device_id *pid)
1330 {
1331         struct Scsi_Host *host;
1332         struct lpfc_hba  *phba;
1333         struct lpfc_sli  *psli;
1334         struct lpfc_iocbq *iocbq_entry = NULL, *iocbq_next = NULL;
1335         unsigned long bar0map_len, bar2map_len;
1336         int error = -ENODEV, retval;
1337         int i;
1338         u64 wwname;
1339
1340         if (pci_enable_device(pdev))
1341                 goto out;
1342         if (pci_request_regions(pdev, LPFC_DRIVER_NAME))
1343                 goto out_disable_device;
1344
1345         host = scsi_host_alloc(&lpfc_template,
1346                         sizeof (struct lpfc_hba) + sizeof (unsigned long));
1347         if (!host)
1348                 goto out_release_regions;
1349
1350         phba = (struct lpfc_hba*)host->hostdata;
1351         memset(phba, 0, sizeof (struct lpfc_hba));
1352         phba->link_stats = (void *)&phba[1];
1353         phba->host = host;
1354
1355         phba->fc_flag |= FC_LOADING;
1356         phba->pcidev = pdev;
1357
1358         /* Assign an unused board number */
1359         if (!idr_pre_get(&lpfc_hba_index, GFP_KERNEL))
1360                 goto out_put_host;
1361
1362         error = idr_get_new(&lpfc_hba_index, NULL, &phba->brd_no);
1363         if (error)
1364                 goto out_put_host;
1365
1366         host->unique_id = phba->brd_no;
1367
1368         INIT_LIST_HEAD(&phba->ctrspbuflist);
1369         INIT_LIST_HEAD(&phba->rnidrspbuflist);
1370         INIT_LIST_HEAD(&phba->freebufList);
1371
1372         /* Initialize timers used by driver */
1373         init_timer(&phba->fc_estabtmo);
1374         phba->fc_estabtmo.function = lpfc_establish_link_tmo;
1375         phba->fc_estabtmo.data = (unsigned long)phba;
1376         init_timer(&phba->fc_disctmo);
1377         phba->fc_disctmo.function = lpfc_disc_timeout;
1378         phba->fc_disctmo.data = (unsigned long)phba;
1379
1380         init_timer(&phba->fc_fdmitmo);
1381         phba->fc_fdmitmo.function = lpfc_fdmi_tmo;
1382         phba->fc_fdmitmo.data = (unsigned long)phba;
1383         init_timer(&phba->els_tmofunc);
1384         phba->els_tmofunc.function = lpfc_els_timeout;
1385         phba->els_tmofunc.data = (unsigned long)phba;
1386         psli = &phba->sli;
1387         init_timer(&psli->mbox_tmo);
1388         psli->mbox_tmo.function = lpfc_mbox_timeout;
1389         psli->mbox_tmo.data = (unsigned long)phba;
1390
1391         /*
1392          * Get all the module params for configuring this host and then
1393          * establish the host parameters.
1394          */
1395         lpfc_get_cfgparam(phba);
1396
1397         host->max_id = LPFC_MAX_TARGET;
1398         host->max_lun = phba->cfg_max_luns;
1399         host->this_id = -1;
1400
1401         /* Initialize all internally managed lists. */
1402         INIT_LIST_HEAD(&phba->fc_nlpmap_list);
1403         INIT_LIST_HEAD(&phba->fc_nlpunmap_list);
1404         INIT_LIST_HEAD(&phba->fc_unused_list);
1405         INIT_LIST_HEAD(&phba->fc_plogi_list);
1406         INIT_LIST_HEAD(&phba->fc_adisc_list);
1407         INIT_LIST_HEAD(&phba->fc_reglogin_list);
1408         INIT_LIST_HEAD(&phba->fc_prli_list);
1409         INIT_LIST_HEAD(&phba->fc_npr_list);
1410
1411
1412         pci_set_master(pdev);
1413         retval = pci_set_mwi(pdev);
1414         if (retval)
1415                 dev_printk(KERN_WARNING, &pdev->dev,
1416                            "Warning: pci_set_mwi returned %d\n", retval);
1417
1418         if (pci_set_dma_mask(phba->pcidev, DMA_64BIT_MASK) != 0)
1419                 if (pci_set_dma_mask(phba->pcidev, DMA_32BIT_MASK) != 0)
1420                         goto out_idr_remove;
1421
1422         /*
1423          * Get the bus address of Bar0 and Bar2 and the number of bytes
1424          * required by each mapping.
1425          */
1426         phba->pci_bar0_map = pci_resource_start(phba->pcidev, 0);
1427         bar0map_len        = pci_resource_len(phba->pcidev, 0);
1428
1429         phba->pci_bar2_map = pci_resource_start(phba->pcidev, 2);
1430         bar2map_len        = pci_resource_len(phba->pcidev, 2);
1431
1432         /* Map HBA SLIM and Control Registers to a kernel virtual address. */
1433         phba->slim_memmap_p      = ioremap(phba->pci_bar0_map, bar0map_len);
1434         phba->ctrl_regs_memmap_p = ioremap(phba->pci_bar2_map, bar2map_len);
1435
1436         /* Allocate memory for SLI-2 structures */
1437         phba->slim2p = dma_alloc_coherent(&phba->pcidev->dev, SLI2_SLIM_SIZE,
1438                                           &phba->slim2p_mapping, GFP_KERNEL);
1439         if (!phba->slim2p)
1440                 goto out_iounmap;
1441
1442
1443         /* Initialize the SLI Layer to run with lpfc HBAs. */
1444         lpfc_sli_setup(phba);
1445         lpfc_sli_queue_setup(phba);
1446
1447         error = lpfc_mem_alloc(phba);
1448         if (error)
1449                 goto out_free_slim;
1450
1451         /* Initialize and populate the iocb list per host.  */
1452         INIT_LIST_HEAD(&phba->lpfc_iocb_list);
1453         for (i = 0; i < LPFC_IOCB_LIST_CNT; i++) {
1454                 iocbq_entry = kmalloc(sizeof(struct lpfc_iocbq), GFP_KERNEL);
1455                 if (iocbq_entry == NULL) {
1456                         printk(KERN_ERR "%s: only allocated %d iocbs of "
1457                                 "expected %d count. Unloading driver.\n",
1458                                 __FUNCTION__, i, LPFC_IOCB_LIST_CNT);
1459                         error = -ENOMEM;
1460                         goto out_free_iocbq;
1461                 }
1462
1463                 memset(iocbq_entry, 0, sizeof(struct lpfc_iocbq));
1464                 spin_lock_irq(phba->host->host_lock);
1465                 list_add(&iocbq_entry->list, &phba->lpfc_iocb_list);
1466                 phba->total_iocbq_bufs++;
1467                 spin_unlock_irq(phba->host->host_lock);
1468         }
1469
1470         /* Initialize HBA structure */
1471         phba->fc_edtov = FF_DEF_EDTOV;
1472         phba->fc_ratov = FF_DEF_RATOV;
1473         phba->fc_altov = FF_DEF_ALTOV;
1474         phba->fc_arbtov = FF_DEF_ARBTOV;
1475
1476         INIT_LIST_HEAD(&phba->work_list);
1477         phba->work_ha_mask = (HA_ERATT|HA_MBATT|HA_LATT);
1478         phba->work_ha_mask |= (HA_RXMASK << (LPFC_ELS_RING * 4));
1479
1480         /* Startup the kernel thread for this host adapter. */
1481         phba->worker_thread = kthread_run(lpfc_do_work, phba,
1482                                        "lpfc_worker_%d", phba->brd_no);
1483         if (IS_ERR(phba->worker_thread)) {
1484                 error = PTR_ERR(phba->worker_thread);
1485                 goto out_free_iocbq;
1486         }
1487
1488         /* We can rely on a queue depth attribute only after SLI HBA setup */
1489         host->can_queue = phba->cfg_hba_queue_depth - 10;
1490
1491         /* Tell the midlayer we support 16 byte commands */
1492         host->max_cmd_len = 16;
1493
1494         /* Initialize the list of scsi buffers used by driver for scsi IO. */
1495         INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list);
1496
1497         host->transportt = lpfc_transport_template;
1498         host->hostdata[0] = (unsigned long)phba;
1499         pci_set_drvdata(pdev, host);
1500         error = scsi_add_host(host, &pdev->dev);
1501         if (error)
1502                 goto out_kthread_stop;
1503
1504         error = lpfc_alloc_sysfs_attr(phba);
1505         if (error)
1506                 goto out_kthread_stop;
1507
1508         error = request_irq(phba->pcidev->irq, lpfc_intr_handler, SA_SHIRQ,
1509                                                         LPFC_DRIVER_NAME, phba);
1510         if (error) {
1511                 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1512                         "%d:0451 Enable interrupt handler failed\n",
1513                         phba->brd_no);
1514                 goto out_free_sysfs_attr;
1515         }
1516         phba->MBslimaddr = phba->slim_memmap_p;
1517         phba->HAregaddr = phba->ctrl_regs_memmap_p + HA_REG_OFFSET;
1518         phba->CAregaddr = phba->ctrl_regs_memmap_p + CA_REG_OFFSET;
1519         phba->HSregaddr = phba->ctrl_regs_memmap_p + HS_REG_OFFSET;
1520         phba->HCregaddr = phba->ctrl_regs_memmap_p + HC_REG_OFFSET;
1521
1522         error = lpfc_sli_hba_setup(phba);
1523         if (error)
1524                 goto out_free_irq;
1525
1526         /*
1527          * set fixed host attributes
1528          * Must done after lpfc_sli_hba_setup()
1529          */
1530
1531         memcpy(&wwname, &phba->fc_nodename, sizeof(u64));
1532         fc_host_node_name(host) = be64_to_cpu(wwname);
1533         memcpy(&wwname, &phba->fc_portname, sizeof(u64));
1534         fc_host_port_name(host) = be64_to_cpu(wwname);
1535         fc_host_supported_classes(host) = FC_COS_CLASS3;
1536
1537         memset(fc_host_supported_fc4s(host), 0,
1538                 sizeof(fc_host_supported_fc4s(host)));
1539         fc_host_supported_fc4s(host)[2] = 1;
1540         fc_host_supported_fc4s(host)[7] = 1;
1541
1542         lpfc_get_hba_sym_node_name(phba, fc_host_symbolic_name(host));
1543
1544         fc_host_supported_speeds(host) = 0;
1545         switch (FC_JEDEC_ID(phba->vpd.rev.biuRev)) {
1546         case VIPER_JEDEC_ID:
1547                 fc_host_supported_speeds(host) |= FC_PORTSPEED_10GBIT;
1548                 break;
1549         case HELIOS_JEDEC_ID:
1550                 fc_host_supported_speeds(host) |= FC_PORTSPEED_4GBIT;
1551                 /* Fall through */
1552         case CENTAUR_2G_JEDEC_ID:
1553         case PEGASUS_JEDEC_ID:
1554         case THOR_JEDEC_ID:
1555                 fc_host_supported_speeds(host) |= FC_PORTSPEED_2GBIT;
1556                 /* Fall through */
1557         default:
1558                 fc_host_supported_speeds(host) = FC_PORTSPEED_1GBIT;
1559         }
1560
1561         fc_host_maxframe_size(host) =
1562                 ((((uint32_t) phba->fc_sparam.cmn.bbRcvSizeMsb & 0x0F) << 8) |
1563                  (uint32_t) phba->fc_sparam.cmn.bbRcvSizeLsb);
1564
1565         /* This value is also unchanging */
1566         memset(fc_host_active_fc4s(host), 0,
1567                 sizeof(fc_host_active_fc4s(host)));
1568         fc_host_active_fc4s(host)[2] = 1;
1569         fc_host_active_fc4s(host)[7] = 1;
1570
1571         spin_lock_irq(phba->host->host_lock);
1572         phba->fc_flag &= ~FC_LOADING;
1573         spin_unlock_irq(phba->host->host_lock);
1574         return 0;
1575
1576 out_free_irq:
1577         lpfc_stop_timer(phba);
1578         phba->work_hba_events = 0;
1579         free_irq(phba->pcidev->irq, phba);
1580 out_free_sysfs_attr:
1581         lpfc_free_sysfs_attr(phba);
1582 out_kthread_stop:
1583         kthread_stop(phba->worker_thread);
1584 out_free_iocbq:
1585         list_for_each_entry_safe(iocbq_entry, iocbq_next,
1586                                                 &phba->lpfc_iocb_list, list) {
1587                 spin_lock_irq(phba->host->host_lock);
1588                 kfree(iocbq_entry);
1589                 phba->total_iocbq_bufs--;
1590                 spin_unlock_irq(phba->host->host_lock);
1591         }
1592         lpfc_mem_free(phba);
1593 out_free_slim:
1594         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE, phba->slim2p,
1595                                                         phba->slim2p_mapping);
1596 out_iounmap:
1597         iounmap(phba->ctrl_regs_memmap_p);
1598         iounmap(phba->slim_memmap_p);
1599 out_idr_remove:
1600         idr_remove(&lpfc_hba_index, phba->brd_no);
1601 out_put_host:
1602         scsi_host_put(host);
1603 out_release_regions:
1604         pci_release_regions(pdev);
1605 out_disable_device:
1606         pci_disable_device(pdev);
1607 out:
1608         return error;
1609 }
1610
1611 static void __devexit
1612 lpfc_pci_remove_one(struct pci_dev *pdev)
1613 {
1614         struct Scsi_Host   *host = pci_get_drvdata(pdev);
1615         struct lpfc_hba    *phba = (struct lpfc_hba *)host->hostdata[0];
1616         unsigned long iflag;
1617
1618         lpfc_free_sysfs_attr(phba);
1619
1620         spin_lock_irqsave(phba->host->host_lock, iflag);
1621         phba->fc_flag |= FC_UNLOADING;
1622
1623         spin_unlock_irqrestore(phba->host->host_lock, iflag);
1624
1625         fc_remove_host(phba->host);
1626         scsi_remove_host(phba->host);
1627
1628         kthread_stop(phba->worker_thread);
1629
1630         /*
1631          * Bring down the SLI Layer. This step disable all interrupts,
1632          * clears the rings, discards all mailbox commands, and resets
1633          * the HBA.
1634          */
1635         lpfc_sli_hba_down(phba);
1636
1637         /* Release the irq reservation */
1638         free_irq(phba->pcidev->irq, phba);
1639
1640         lpfc_cleanup(phba, 0);
1641         lpfc_stop_timer(phba);
1642         phba->work_hba_events = 0;
1643
1644         /*
1645          * Call scsi_free before mem_free since scsi bufs are released to their
1646          * corresponding pools here.
1647          */
1648         lpfc_scsi_free(phba);
1649         lpfc_mem_free(phba);
1650
1651         /* Free resources associated with SLI2 interface */
1652         dma_free_coherent(&pdev->dev, SLI2_SLIM_SIZE,
1653                           phba->slim2p, phba->slim2p_mapping);
1654
1655         /* unmap adapter SLIM and Control Registers */
1656         iounmap(phba->ctrl_regs_memmap_p);
1657         iounmap(phba->slim_memmap_p);
1658
1659         pci_release_regions(phba->pcidev);
1660         pci_disable_device(phba->pcidev);
1661
1662         idr_remove(&lpfc_hba_index, phba->brd_no);
1663         scsi_host_put(phba->host);
1664
1665         pci_set_drvdata(pdev, NULL);
1666 }
1667
1668 static struct pci_device_id lpfc_id_table[] = {
1669         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_VIPER,
1670                 PCI_ANY_ID, PCI_ANY_ID, },
1671         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_FIREFLY,
1672                 PCI_ANY_ID, PCI_ANY_ID, },
1673         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_THOR,
1674                 PCI_ANY_ID, PCI_ANY_ID, },
1675         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PEGASUS,
1676                 PCI_ANY_ID, PCI_ANY_ID, },
1677         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_CENTAUR,
1678                 PCI_ANY_ID, PCI_ANY_ID, },
1679         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_DRAGONFLY,
1680                 PCI_ANY_ID, PCI_ANY_ID, },
1681         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_SUPERFLY,
1682                 PCI_ANY_ID, PCI_ANY_ID, },
1683         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_RFLY,
1684                 PCI_ANY_ID, PCI_ANY_ID, },
1685         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_PFLY,
1686                 PCI_ANY_ID, PCI_ANY_ID, },
1687         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_HELIOS,
1688                 PCI_ANY_ID, PCI_ANY_ID, },
1689         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BMID,
1690                 PCI_ANY_ID, PCI_ANY_ID, },
1691         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_BSMB,
1692                 PCI_ANY_ID, PCI_ANY_ID, },
1693         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZEPHYR,
1694                 PCI_ANY_ID, PCI_ANY_ID, },
1695         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZMID,
1696                 PCI_ANY_ID, PCI_ANY_ID, },
1697         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_ZSMB,
1698                 PCI_ANY_ID, PCI_ANY_ID, },
1699         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_TFLY,
1700                 PCI_ANY_ID, PCI_ANY_ID, },
1701         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP101,
1702                 PCI_ANY_ID, PCI_ANY_ID, },
1703         {PCI_VENDOR_ID_EMULEX, PCI_DEVICE_ID_LP10000S,
1704                 PCI_ANY_ID, PCI_ANY_ID, },
1705         { 0 }
1706 };
1707
1708 MODULE_DEVICE_TABLE(pci, lpfc_id_table);
1709
1710 static struct pci_driver lpfc_driver = {
1711         .name           = LPFC_DRIVER_NAME,
1712         .id_table       = lpfc_id_table,
1713         .probe          = lpfc_pci_probe_one,
1714         .remove         = __devexit_p(lpfc_pci_remove_one),
1715 };
1716
1717 static int __init
1718 lpfc_init(void)
1719 {
1720         int error = 0;
1721
1722         printk(LPFC_MODULE_DESC "\n");
1723
1724         lpfc_transport_template =
1725                                 fc_attach_transport(&lpfc_transport_functions);
1726         if (!lpfc_transport_template)
1727                 return -ENOMEM;
1728         error = pci_register_driver(&lpfc_driver);
1729         if (error)
1730                 fc_release_transport(lpfc_transport_template);
1731
1732         return error;
1733 }
1734
1735 static void __exit
1736 lpfc_exit(void)
1737 {
1738         pci_unregister_driver(&lpfc_driver);
1739         fc_release_transport(lpfc_transport_template);
1740 }
1741
1742 module_init(lpfc_init);
1743 module_exit(lpfc_exit);
1744 MODULE_LICENSE("GPL");
1745 MODULE_DESCRIPTION(LPFC_MODULE_DESC);
1746 MODULE_AUTHOR("Emulex Corporation - tech.support@emulex.com");
1747 MODULE_VERSION("0:" LPFC_DRIVER_VERSION);