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