blob: a17800e6277de4bd72254fc923f5360d70f92192 [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
2 * Copyright (c) 2005-2009 Brocade Communications Systems, Inc.
3 * All rights reserved
4 * www.brocade.com
5 *
6 * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License (GPL) Version 2 as
10 * published by the Free Software Foundation
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 */
17
18/**
19 * bfad.c Linux driver PCI interface module.
20 */
21
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070023#include <linux/module.h>
Krishna Gudipatie6714322010-03-03 17:44:02 -080024#include <linux/kthread.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070025#include "bfad_drv.h"
26#include "bfad_im.h"
27#include "bfad_tm.h"
28#include "bfad_ipfc.h"
29#include "bfad_trcmod.h"
30#include <fcb/bfa_fcb_vf.h>
31#include <fcb/bfa_fcb_rport.h>
32#include <fcb/bfa_fcb_port.h>
33#include <fcb/bfa_fcb.h>
34
35BFA_TRC_FILE(LDRV, BFAD);
Jing Huang42b426e2010-03-19 11:07:09 -070036DEFINE_MUTEX(bfad_mutex);
Jing Huang7725ccf2009-09-23 17:46:15 -070037LIST_HEAD(bfad_list);
38static int bfad_inst;
39int bfad_supported_fc4s;
40
41static char *host_name;
42static char *os_name;
43static char *os_patch;
44static int num_rports;
45static int num_ios;
46static int num_tms;
47static int num_fcxps;
48static int num_ufbufs;
49static int reqq_size;
50static int rspq_size;
51static int num_sgpgs;
52static int rport_del_timeout = BFA_FCS_RPORT_DEF_DEL_TIMEOUT;
53static int bfa_io_max_sge = BFAD_IO_MAX_SGE;
54static int log_level = BFA_LOG_WARNING;
55static int ioc_auto_recover = BFA_TRUE;
56static int ipfc_enable = BFA_FALSE;
Krishna Gudipati5b098082010-03-03 17:43:19 -080057static int fdmi_enable = BFA_TRUE;
Jing Huang7725ccf2009-09-23 17:46:15 -070058int bfa_lun_queue_depth = BFAD_LUN_QUEUE_DEPTH;
59int bfa_linkup_delay = -1;
60
61module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070062MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070063module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070064MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070065module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070066MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070067module_param(num_rports, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070068MODULE_PARM_DESC(num_rports, "Max number of rports supported per port"
69 " (physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070070module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070071MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070072module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070073MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -070074module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070075MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070076module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070077MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame buffers,"
78 " default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070079module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070080MODULE_PARM_DESC(reqq_size, "Max number of request queue elements,"
81 " default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -070082module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070083MODULE_PARM_DESC(rspq_size, "Max number of response queue elements,"
84 " default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070085module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070086MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -070087module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070088MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs,"
89 " Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -070090module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070091MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32,"
92 " Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -070093module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070094MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang7725ccf2009-09-23 17:46:15 -070095module_param(log_level, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070096MODULE_PARM_DESC(log_level, "Driver log level, default=3,"
97 " Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -070098module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070099MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1,"
100 " Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700101module_param(ipfc_enable, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700102MODULE_PARM_DESC(ipfc_enable, "Enable IPoFC, default=0, Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700103module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700104MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for boot"
105 " port. Otherwise Range[>0]");
106module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
107MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1,"
108 " Range[false:0|true:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700109
110/*
111 * Stores the module parm num_sgpgs value;
112 * used to reset for bfad next instance.
113 */
114static int num_sgpgs_parm;
115
116static bfa_status_t
117bfad_fc4_probe(struct bfad_s *bfad)
118{
119 int rc;
120
121 rc = bfad_im_probe(bfad);
122 if (rc != BFA_STATUS_OK)
123 goto ext;
124
125 bfad_tm_probe(bfad);
126
127 if (ipfc_enable)
128 bfad_ipfc_probe(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800129
130 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700131ext:
132 return rc;
133}
134
135static void
136bfad_fc4_probe_undo(struct bfad_s *bfad)
137{
138 bfad_im_probe_undo(bfad);
139 bfad_tm_probe_undo(bfad);
140 if (ipfc_enable)
141 bfad_ipfc_probe_undo(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800142 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700143}
144
145static void
146bfad_fc4_probe_post(struct bfad_s *bfad)
147{
148 if (bfad->im)
149 bfad_im_probe_post(bfad->im);
150
151 bfad_tm_probe_post(bfad);
152 if (ipfc_enable)
153 bfad_ipfc_probe_post(bfad);
154}
155
156static bfa_status_t
157bfad_fc4_port_new(struct bfad_s *bfad, struct bfad_port_s *port, int roles)
158{
159 int rc = BFA_STATUS_FAILED;
160
161 if (roles & BFA_PORT_ROLE_FCP_IM)
162 rc = bfad_im_port_new(bfad, port);
163 if (rc != BFA_STATUS_OK)
164 goto ext;
165
166 if (roles & BFA_PORT_ROLE_FCP_TM)
167 rc = bfad_tm_port_new(bfad, port);
168 if (rc != BFA_STATUS_OK)
169 goto ext;
170
171 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
172 rc = bfad_ipfc_port_new(bfad, port, port->pvb_type);
173ext:
174 return rc;
175}
176
177static void
178bfad_fc4_port_delete(struct bfad_s *bfad, struct bfad_port_s *port, int roles)
179{
180 if (roles & BFA_PORT_ROLE_FCP_IM)
181 bfad_im_port_delete(bfad, port);
182
183 if (roles & BFA_PORT_ROLE_FCP_TM)
184 bfad_tm_port_delete(bfad, port);
185
186 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
187 bfad_ipfc_port_delete(bfad, port);
188}
189
190/**
191 * BFA callbacks
192 */
193void
194bfad_hcb_comp(void *arg, bfa_status_t status)
195{
196 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
197
198 fcomp->status = status;
199 complete(&fcomp->comp);
200}
201
202/**
203 * bfa_init callback
204 */
205void
206bfa_cb_init(void *drv, bfa_status_t init_status)
207{
208 struct bfad_s *bfad = drv;
209
Krishna Gudipatie6714322010-03-03 17:44:02 -0800210 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700211 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
212
Krishna Gudipatie6714322010-03-03 17:44:02 -0800213 /* If BFAD_HAL_INIT_FAIL flag is set:
214 * Wake up the kernel thread to start
215 * the bfad operations after HAL init done
216 */
217 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
218 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
219 wake_up_process(bfad->bfad_tsk);
220 }
221 }
222
Jing Huang7725ccf2009-09-23 17:46:15 -0700223 complete(&bfad->comp);
224}
225
226
227
228/**
229 * BFA_FCS callbacks
230 */
231static struct bfad_port_s *
232bfad_get_drv_port(struct bfad_s *bfad, struct bfad_vf_s *vf_drv,
233 struct bfad_vport_s *vp_drv)
234{
Jing Huangf8ceafd2009-09-25 12:29:54 -0700235 return (vp_drv) ? (&(vp_drv)->drv_port)
236 : ((vf_drv) ? (&(vf_drv)->base_port) : (&(bfad)->pport));
Jing Huang7725ccf2009-09-23 17:46:15 -0700237}
238
239struct bfad_port_s *
240bfa_fcb_port_new(struct bfad_s *bfad, struct bfa_fcs_port_s *port,
241 enum bfa_port_role roles, struct bfad_vf_s *vf_drv,
242 struct bfad_vport_s *vp_drv)
243{
244 bfa_status_t rc;
245 struct bfad_port_s *port_drv;
246
247 if (!vp_drv && !vf_drv) {
248 port_drv = &bfad->pport;
249 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
250 } else if (!vp_drv && vf_drv) {
251 port_drv = &vf_drv->base_port;
252 port_drv->pvb_type = BFAD_PORT_VF_BASE;
253 } else if (vp_drv && !vf_drv) {
254 port_drv = &vp_drv->drv_port;
255 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
256 } else {
257 port_drv = &vp_drv->drv_port;
258 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
259 }
260
261 port_drv->fcs_port = port;
262 port_drv->roles = roles;
263 rc = bfad_fc4_port_new(bfad, port_drv, roles);
264 if (rc != BFA_STATUS_OK) {
265 bfad_fc4_port_delete(bfad, port_drv, roles);
266 port_drv = NULL;
267 }
268
269 return port_drv;
270}
271
272void
273bfa_fcb_port_delete(struct bfad_s *bfad, enum bfa_port_role roles,
274 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
275{
276 struct bfad_port_s *port_drv;
277
278 /*
279 * this will be only called from rmmod context
280 */
281 if (vp_drv && !vp_drv->comp_del) {
282 port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
283 bfa_trc(bfad, roles);
284 bfad_fc4_port_delete(bfad, port_drv, roles);
285 }
286}
287
288void
289bfa_fcb_port_online(struct bfad_s *bfad, enum bfa_port_role roles,
290 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
291{
292 struct bfad_port_s *port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
293
294 if (roles & BFA_PORT_ROLE_FCP_IM)
295 bfad_im_port_online(bfad, port_drv);
296
297 if (roles & BFA_PORT_ROLE_FCP_TM)
298 bfad_tm_port_online(bfad, port_drv);
299
300 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
301 bfad_ipfc_port_online(bfad, port_drv);
302
303 bfad->bfad_flags |= BFAD_PORT_ONLINE;
304}
305
306void
307bfa_fcb_port_offline(struct bfad_s *bfad, enum bfa_port_role roles,
308 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
309{
310 struct bfad_port_s *port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
311
312 if (roles & BFA_PORT_ROLE_FCP_IM)
313 bfad_im_port_offline(bfad, port_drv);
314
315 if (roles & BFA_PORT_ROLE_FCP_TM)
316 bfad_tm_port_offline(bfad, port_drv);
317
318 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
319 bfad_ipfc_port_offline(bfad, port_drv);
320}
321
322void
323bfa_fcb_vport_delete(struct bfad_vport_s *vport_drv)
324{
325 if (vport_drv->comp_del) {
326 complete(vport_drv->comp_del);
327 return;
328 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700329}
330
331/**
332 * FCS RPORT alloc callback, after successful PLOGI by FCS
333 */
334bfa_status_t
335bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
336 struct bfad_rport_s **rport_drv)
337{
338 bfa_status_t rc = BFA_STATUS_OK;
339
340 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
341 if (*rport_drv == NULL) {
342 rc = BFA_STATUS_ENOMEM;
343 goto ext;
344 }
345
346 *rport = &(*rport_drv)->fcs_rport;
347
348ext:
349 return rc;
350}
351
Jing Huangd9883542010-07-08 19:46:26 -0700352/**
353 * @brief
354 * FCS PBC VPORT Create
355 */
356void
357bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
358{
Jing Huang7725ccf2009-09-23 17:46:15 -0700359
Jing Huangd9883542010-07-08 19:46:26 -0700360 struct bfad_pcfg_s *pcfg;
361
362 pcfg = kzalloc(sizeof(struct bfad_pcfg_s), GFP_ATOMIC);
363 if (!pcfg) {
364 bfa_trc(bfad, 0);
365 return;
366 }
367
368 pcfg->port_cfg.roles = BFA_PORT_ROLE_FCP_IM;
369 pcfg->port_cfg.pwwn = pbc_vport.vp_pwwn;
370 pcfg->port_cfg.nwwn = pbc_vport.vp_nwwn;
371 pcfg->port_cfg.preboot_vp = BFA_TRUE;
372
373 list_add_tail(&pcfg->list_entry, &bfad->pbc_pcfg_list);
374
375 return;
376}
Jing Huang7725ccf2009-09-23 17:46:15 -0700377
378void
379bfad_hal_mem_release(struct bfad_s *bfad)
380{
381 int i;
382 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
383 struct bfa_mem_elem_s *meminfo_elem;
384
385 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
386 meminfo_elem = &hal_meminfo->meminfo[i];
387 if (meminfo_elem->kva != NULL) {
388 switch (meminfo_elem->mem_type) {
389 case BFA_MEM_TYPE_KVA:
390 vfree(meminfo_elem->kva);
391 break;
392 case BFA_MEM_TYPE_DMA:
393 dma_free_coherent(&bfad->pcidev->dev,
394 meminfo_elem->mem_len,
395 meminfo_elem->kva,
396 (dma_addr_t) meminfo_elem->dma);
397 break;
398 default:
399 bfa_assert(0);
400 break;
401 }
402 }
403 }
404
405 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
406}
407
408void
409bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
410{
411 if (num_rports > 0)
412 bfa_cfg->fwcfg.num_rports = num_rports;
413 if (num_ios > 0)
414 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
415 if (num_tms > 0)
416 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
417 if (num_fcxps > 0)
418 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
419 if (num_ufbufs > 0)
420 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
421 if (reqq_size > 0)
422 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
423 if (rspq_size > 0)
424 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
425 if (num_sgpgs > 0)
426 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
427
428 /*
429 * populate the hal values back to the driver for sysfs use.
430 * otherwise, the default values will be shown as 0 in sysfs
431 */
432 num_rports = bfa_cfg->fwcfg.num_rports;
433 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
434 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
435 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
436 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
437 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
438 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
439 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
440}
441
442bfa_status_t
443bfad_hal_mem_alloc(struct bfad_s *bfad)
444{
445 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
446 struct bfa_mem_elem_s *meminfo_elem;
447 bfa_status_t rc = BFA_STATUS_OK;
448 dma_addr_t phys_addr;
449 int retry_count = 0;
450 int reset_value = 1;
451 int min_num_sgpgs = 512;
452 void *kva;
453 int i;
454
455 bfa_cfg_get_default(&bfad->ioc_cfg);
456
457retry:
458 bfad_update_hal_cfg(&bfad->ioc_cfg);
459 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
460 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo);
461
462 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
463 meminfo_elem = &hal_meminfo->meminfo[i];
464 switch (meminfo_elem->mem_type) {
465 case BFA_MEM_TYPE_KVA:
466 kva = vmalloc(meminfo_elem->mem_len);
467 if (kva == NULL) {
468 bfad_hal_mem_release(bfad);
469 rc = BFA_STATUS_ENOMEM;
470 goto ext;
471 }
472 memset(kva, 0, meminfo_elem->mem_len);
473 meminfo_elem->kva = kva;
474 break;
475 case BFA_MEM_TYPE_DMA:
476 kva = dma_alloc_coherent(&bfad->pcidev->dev,
477 meminfo_elem->mem_len,
478 &phys_addr, GFP_KERNEL);
479 if (kva == NULL) {
480 bfad_hal_mem_release(bfad);
481 /*
482 * If we cannot allocate with default
483 * num_sgpages try with half the value.
484 */
485 if (num_sgpgs > min_num_sgpgs) {
486 printk(KERN_INFO "bfad[%d]: memory"
487 " allocation failed with"
488 " num_sgpgs: %d\n",
489 bfad->inst_no, num_sgpgs);
490 nextLowerInt(&num_sgpgs);
491 printk(KERN_INFO "bfad[%d]: trying to"
492 " allocate memory with"
493 " num_sgpgs: %d\n",
494 bfad->inst_no, num_sgpgs);
495 retry_count++;
496 goto retry;
497 } else {
498 if (num_sgpgs_parm > 0)
499 num_sgpgs = num_sgpgs_parm;
500 else {
501 reset_value =
502 (1 << retry_count);
503 num_sgpgs *= reset_value;
504 }
505 rc = BFA_STATUS_ENOMEM;
506 goto ext;
507 }
508 }
509
510 if (num_sgpgs_parm > 0)
511 num_sgpgs = num_sgpgs_parm;
512 else {
513 reset_value = (1 << retry_count);
514 num_sgpgs *= reset_value;
515 }
516
517 memset(kva, 0, meminfo_elem->mem_len);
518 meminfo_elem->kva = kva;
519 meminfo_elem->dma = phys_addr;
520 break;
521 default:
522 break;
523
524 }
525 }
526ext:
527 return rc;
528}
529
530/**
531 * Create a vport under a vf.
532 */
533bfa_status_t
534bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Jing Huangd9883542010-07-08 19:46:26 -0700535 struct bfa_port_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700536{
537 struct bfad_vport_s *vport;
Jing Huangd9883542010-07-08 19:46:26 -0700538 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700539 unsigned long flags;
540 struct completion fcomp;
541
542 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
543 if (!vport) {
544 rc = BFA_STATUS_ENOMEM;
545 goto ext;
546 }
547
548 vport->drv_port.bfad = bfad;
549 spin_lock_irqsave(&bfad->bfad_lock, flags);
Jing Huangd9883542010-07-08 19:46:26 -0700550 if (port_cfg->preboot_vp == BFA_TRUE)
551 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport,
552 &bfad->bfa_fcs, vf_id, port_cfg, vport);
553 else
554 rc = bfa_fcs_vport_create(&vport->fcs_vport,
555 &bfad->bfa_fcs, vf_id, port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700556 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
557
558 if (rc != BFA_STATUS_OK)
559 goto ext_free_vport;
560
561 if (port_cfg->roles & BFA_PORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700562 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
563 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700564 if (rc != BFA_STATUS_OK)
565 goto ext_free_fcs_vport;
566 }
567
568 spin_lock_irqsave(&bfad->bfad_lock, flags);
569 bfa_fcs_vport_start(&vport->fcs_vport);
570 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
571
572 return BFA_STATUS_OK;
573
574ext_free_fcs_vport:
575 spin_lock_irqsave(&bfad->bfad_lock, flags);
576 vport->comp_del = &fcomp;
577 init_completion(vport->comp_del);
578 bfa_fcs_vport_delete(&vport->fcs_vport);
579 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
580 wait_for_completion(vport->comp_del);
581ext_free_vport:
582 kfree(vport);
583ext:
584 return rc;
585}
586
587/**
588 * Create a vf and its base vport implicitely.
589 */
590bfa_status_t
591bfad_vf_create(struct bfad_s *bfad, u16 vf_id,
592 struct bfa_port_cfg_s *port_cfg)
593{
594 struct bfad_vf_s *vf;
595 int rc = BFA_STATUS_OK;
596
597 vf = kzalloc(sizeof(struct bfad_vf_s), GFP_KERNEL);
598 if (!vf) {
599 rc = BFA_STATUS_FAILED;
600 goto ext;
601 }
602
603 rc = bfa_fcs_vf_create(&vf->fcs_vf, &bfad->bfa_fcs, vf_id, port_cfg,
604 vf);
605 if (rc != BFA_STATUS_OK)
606 kfree(vf);
607ext:
608 return rc;
609}
610
611void
612bfad_bfa_tmo(unsigned long data)
613{
614 struct bfad_s *bfad = (struct bfad_s *)data;
615 unsigned long flags;
616 struct list_head doneq;
617
618 spin_lock_irqsave(&bfad->bfad_lock, flags);
619
620 bfa_timer_tick(&bfad->bfa);
621
622 bfa_comp_deq(&bfad->bfa, &doneq);
623 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
624
625 if (!list_empty(&doneq)) {
626 bfa_comp_process(&bfad->bfa, &doneq);
627 spin_lock_irqsave(&bfad->bfad_lock, flags);
628 bfa_comp_free(&bfad->bfa, &doneq);
629 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
630 }
631
632 mod_timer(&bfad->hal_tmo, jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
633}
634
635void
636bfad_init_timer(struct bfad_s *bfad)
637{
638 init_timer(&bfad->hal_tmo);
639 bfad->hal_tmo.function = bfad_bfa_tmo;
640 bfad->hal_tmo.data = (unsigned long)bfad;
641
642 mod_timer(&bfad->hal_tmo, jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
643}
644
645int
646bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
647{
Jing Huang7725ccf2009-09-23 17:46:15 -0700648 int rc = -ENODEV;
649
650 if (pci_enable_device(pdev)) {
651 BFA_PRINTF(BFA_ERR, "pci_enable_device fail %p\n", pdev);
652 goto out;
653 }
654
655 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
656 goto out_disable_device;
657
658 pci_set_master(pdev);
659
660
661 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
662 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
663 BFA_PRINTF(BFA_ERR, "pci_set_dma_mask fail %p\n", pdev);
664 goto out_release_region;
665 }
666
Jing Huangb3522f02010-03-19 11:06:44 -0700667 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Jing Huang7725ccf2009-09-23 17:46:15 -0700668
669 if (bfad->pci_bar0_kva == NULL) {
670 BFA_PRINTF(BFA_ERR, "Fail to map bar0\n");
671 goto out_release_region;
672 }
673
674 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
675 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
676 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
677 bfad->hal_pcidev.device_id = pdev->device;
678 bfad->pci_name = pci_name(pdev);
679
680 bfad->pci_attr.vendor_id = pdev->vendor;
681 bfad->pci_attr.device_id = pdev->device;
682 bfad->pci_attr.ssid = pdev->subsystem_device;
683 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
684 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
685
686 bfad->pcidev = pdev;
687 return 0;
688
689out_release_region:
690 pci_release_regions(pdev);
691out_disable_device:
692 pci_disable_device(pdev);
693out:
694 return rc;
695}
696
697void
698bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
699{
Jing Huang7725ccf2009-09-23 17:46:15 -0700700 pci_iounmap(pdev, bfad->pci_bar0_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700701 pci_release_regions(pdev);
702 pci_disable_device(pdev);
703 pci_set_drvdata(pdev, NULL);
704}
705
706void
707bfad_fcs_port_cfg(struct bfad_s *bfad)
708{
709 struct bfa_port_cfg_s port_cfg;
710 struct bfa_pport_attr_s attr;
711 char symname[BFA_SYMNAME_MAXLEN];
712
713 sprintf(symname, "%s-%d", BFAD_DRIVER_NAME, bfad->inst_no);
714 memcpy(port_cfg.sym_name.symname, symname, strlen(symname));
Krishna Gudipati1c8a4c32010-03-05 19:37:37 -0800715 bfa_fcport_get_attr(&bfad->bfa, &attr);
Jing Huang7725ccf2009-09-23 17:46:15 -0700716 port_cfg.nwwn = attr.nwwn;
717 port_cfg.pwwn = attr.pwwn;
718
719 bfa_fcs_cfg_base_port(&bfad->bfa_fcs, &port_cfg);
720}
721
722bfa_status_t
723bfad_drv_init(struct bfad_s *bfad)
724{
725 bfa_status_t rc;
726 unsigned long flags;
727 struct bfa_fcs_driver_info_s driver_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700728
729 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
730 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
731 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
732 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
733
734 rc = bfad_hal_mem_alloc(bfad);
735 if (rc != BFA_STATUS_OK) {
736 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
737 bfad->inst_no);
738 printk(KERN_WARNING
739 "Not enough memory to attach all Brocade HBA ports,"
740 " System may need more memory.\n");
741 goto out_hal_mem_alloc_failure;
742 }
743
744 bfa_init_log(&bfad->bfa, bfad->logmod);
745 bfa_init_trc(&bfad->bfa, bfad->trcmod);
746 bfa_init_aen(&bfad->bfa, bfad->aen);
Krishna Gudipati2993cc72010-03-05 19:36:47 -0800747 memset(bfad->file_map, 0, sizeof(bfad->file_map));
Jing Huang7725ccf2009-09-23 17:46:15 -0700748 bfa_init_plog(&bfad->bfa, &bfad->plog_buf);
749 bfa_plog_init(&bfad->plog_buf);
750 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
751 0, "Driver Attach");
752
753 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
754 &bfad->hal_pcidev);
755
756 init_completion(&bfad->comp);
757
758 /*
759 * Enable Interrupt and wait bfa_init completion
760 */
761 if (bfad_setup_intr(bfad)) {
762 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
763 bfad->inst_no);
764 goto out_setup_intr_failure;
765 }
766
767 spin_lock_irqsave(&bfad->bfad_lock, flags);
768 bfa_init(&bfad->bfa);
769 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
770
771 /*
772 * Set up interrupt handler for each vectors
773 */
774 if ((bfad->bfad_flags & BFAD_MSIX_ON)
775 && bfad_install_msix_handler(bfad)) {
776 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
Jing Huangf8ceafd2009-09-25 12:29:54 -0700777 __func__, bfad->inst_no);
Jing Huang7725ccf2009-09-23 17:46:15 -0700778 }
779
780 bfad_init_timer(bfad);
781
782 wait_for_completion(&bfad->comp);
783
784 memset(&driver_info, 0, sizeof(driver_info));
785 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
786 sizeof(driver_info.version) - 1);
787 if (host_name)
788 strncpy(driver_info.host_machine_name, host_name,
789 sizeof(driver_info.host_machine_name) - 1);
790 if (os_name)
791 strncpy(driver_info.host_os_name, os_name,
792 sizeof(driver_info.host_os_name) - 1);
793 if (os_patch)
794 strncpy(driver_info.host_os_patch, os_patch,
795 sizeof(driver_info.host_os_patch) - 1);
796
797 strncpy(driver_info.os_device_name, bfad->pci_name,
798 sizeof(driver_info.os_device_name - 1));
799
800 /*
801 * FCS INIT
802 */
803 spin_lock_irqsave(&bfad->bfad_lock, flags);
804 bfa_fcs_log_init(&bfad->bfa_fcs, bfad->logmod);
805 bfa_fcs_trc_init(&bfad->bfa_fcs, bfad->trcmod);
806 bfa_fcs_aen_init(&bfad->bfa_fcs, bfad->aen);
Krishna Gudipati82794a22010-03-03 17:43:30 -0800807 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800808
809 /* Do FCS init only when HAL init is done */
810 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
811 bfa_fcs_init(&bfad->bfa_fcs);
812 bfad->bfad_flags |= BFAD_FCS_INIT_DONE;
813 }
814
Jing Huang7725ccf2009-09-23 17:46:15 -0700815 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Krishna Gudipati5b098082010-03-03 17:43:19 -0800816 bfa_fcs_set_fdmi_param(&bfad->bfa_fcs, fdmi_enable);
Jing Huang7725ccf2009-09-23 17:46:15 -0700817 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
818
819 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
820 return BFA_STATUS_OK;
821
822out_setup_intr_failure:
823 bfa_detach(&bfad->bfa);
824 bfad_hal_mem_release(bfad);
825out_hal_mem_alloc_failure:
826 return BFA_STATUS_FAILED;
827}
828
829void
830bfad_drv_uninit(struct bfad_s *bfad)
831{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800832 unsigned long flags;
833
834 spin_lock_irqsave(&bfad->bfad_lock, flags);
835 init_completion(&bfad->comp);
836 bfa_stop(&bfad->bfa);
837 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
838 wait_for_completion(&bfad->comp);
839
Jing Huang7725ccf2009-09-23 17:46:15 -0700840 del_timer_sync(&bfad->hal_tmo);
841 bfa_isr_disable(&bfad->bfa);
842 bfa_detach(&bfad->bfa);
843 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700844 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800845
846 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700847}
848
849void
850bfad_drv_start(struct bfad_s *bfad)
851{
852 unsigned long flags;
853
854 spin_lock_irqsave(&bfad->bfad_lock, flags);
855 bfa_start(&bfad->bfa);
856 bfa_fcs_start(&bfad->bfa_fcs);
857 bfad->bfad_flags |= BFAD_HAL_START_DONE;
858 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
859
860 bfad_fc4_probe_post(bfad);
861}
862
863void
864bfad_drv_stop(struct bfad_s *bfad)
865{
866 unsigned long flags;
867
868 spin_lock_irqsave(&bfad->bfad_lock, flags);
869 init_completion(&bfad->comp);
870 bfad->pport.flags |= BFAD_PORT_DELETE;
871 bfa_fcs_exit(&bfad->bfa_fcs);
872 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
873 wait_for_completion(&bfad->comp);
874
875 spin_lock_irqsave(&bfad->bfad_lock, flags);
876 init_completion(&bfad->comp);
877 bfa_stop(&bfad->bfa);
878 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
879 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
880 wait_for_completion(&bfad->comp);
881}
882
883bfa_status_t
884bfad_cfg_pport(struct bfad_s *bfad, enum bfa_port_role role)
885{
886 int rc = BFA_STATUS_OK;
887
888 /*
889 * Allocate scsi_host for the physical port
890 */
891 if ((bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IM)
892 && (role & BFA_PORT_ROLE_FCP_IM)) {
893 if (bfad->pport.im_port == NULL) {
894 rc = BFA_STATUS_FAILED;
895 goto out;
896 }
897
Jing Huangb5042932010-03-19 11:05:39 -0700898 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
899 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700900 if (rc != BFA_STATUS_OK)
901 goto out;
902
903 bfad->pport.roles |= BFA_PORT_ROLE_FCP_IM;
904 }
905
906 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
907
908out:
909 return rc;
910}
911
912void
913bfad_uncfg_pport(struct bfad_s *bfad)
914{
915 if ((bfad->pport.roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable) {
916 bfad_ipfc_port_delete(bfad, &bfad->pport);
917 bfad->pport.roles &= ~BFA_PORT_ROLE_FCP_IPFC;
918 }
919
920 if ((bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IM)
921 && (bfad->pport.roles & BFA_PORT_ROLE_FCP_IM)) {
922 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
923 bfad_im_port_clean(bfad->pport.im_port);
924 kfree(bfad->pport.im_port);
925 bfad->pport.roles &= ~BFA_PORT_ROLE_FCP_IM;
926 }
927
928 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
929}
930
931void
932bfad_drv_log_level_set(struct bfad_s *bfad)
933{
934 if (log_level > BFA_LOG_INVALID && log_level <= BFA_LOG_LEVEL_MAX)
935 bfa_log_set_level_all(&bfad->log_data, log_level);
936}
937
Krishna Gudipatie6714322010-03-03 17:44:02 -0800938bfa_status_t
939bfad_start_ops(struct bfad_s *bfad)
940{
941 int retval;
Jing Huangd9883542010-07-08 19:46:26 -0700942 struct bfad_pcfg_s *pcfg, *pcfg_new;
Krishna Gudipatie6714322010-03-03 17:44:02 -0800943
944 /* PPORT FCS config */
945 bfad_fcs_port_cfg(bfad);
946
947 retval = bfad_cfg_pport(bfad, BFA_PORT_ROLE_FCP_IM);
948 if (retval != BFA_STATUS_OK)
949 goto out_cfg_pport_failure;
950
951 /* BFAD level FC4 (IM/TM/IPFC) specific resource allocation */
952 retval = bfad_fc4_probe(bfad);
953 if (retval != BFA_STATUS_OK) {
954 printk(KERN_WARNING "bfad_fc4_probe failed\n");
955 goto out_fc4_probe_failure;
956 }
957
958 bfad_drv_start(bfad);
959
Jing Huangd9883542010-07-08 19:46:26 -0700960 /* pbc vport creation */
961 list_for_each_entry_safe(pcfg, pcfg_new, &bfad->pbc_pcfg_list,
962 list_entry) {
963 struct fc_vport_identifiers vid;
964 struct fc_vport *fc_vport;
965
966 memset(&vid, 0, sizeof(vid));
967 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
968 vid.vport_type = FC_PORTTYPE_NPIV;
969 vid.disable = false;
970 vid.node_name = wwn_to_u64((u8 *)&pcfg->port_cfg.nwwn);
971 vid.port_name = wwn_to_u64((u8 *)&pcfg->port_cfg.pwwn);
972 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
973 if (!fc_vport)
974 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
975 " %llx\n", bfad->inst_no, vid.port_name);
976 list_del(&pcfg->list_entry);
977 kfree(pcfg);
978
979 }
980
Krishna Gudipatie6714322010-03-03 17:44:02 -0800981 /*
982 * If bfa_linkup_delay is set to -1 default; try to retrive the
983 * value using the bfad_os_get_linkup_delay(); else use the
984 * passed in module param value as the bfa_linkup_delay.
985 */
986 if (bfa_linkup_delay < 0) {
987
988 bfa_linkup_delay = bfad_os_get_linkup_delay(bfad);
989 bfad_os_rport_online_wait(bfad);
990 bfa_linkup_delay = -1;
991
992 } else {
993 bfad_os_rport_online_wait(bfad);
994 }
995
996 bfa_log(bfad->logmod, BFA_LOG_LINUX_DEVICE_CLAIMED, bfad->pci_name);
997
998 return BFA_STATUS_OK;
999
1000out_fc4_probe_failure:
1001 bfad_fc4_probe_undo(bfad);
1002 bfad_uncfg_pport(bfad);
1003out_cfg_pport_failure:
1004 return BFA_STATUS_FAILED;
1005}
1006
1007int
Jing Huangd9883542010-07-08 19:46:26 -07001008bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001009{
1010 struct bfad_s *bfad;
1011 unsigned long flags;
1012
1013 bfad = (struct bfad_s *)ptr;
1014
1015 while (!kthread_should_stop()) {
1016
1017 /* Check if the FCS init is done from bfad_drv_init;
1018 * if not done do FCS init and set the flag.
1019 */
1020 if (!(bfad->bfad_flags & BFAD_FCS_INIT_DONE)) {
1021 spin_lock_irqsave(&bfad->bfad_lock, flags);
1022 bfa_fcs_init(&bfad->bfa_fcs);
1023 bfad->bfad_flags |= BFAD_FCS_INIT_DONE;
1024 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1025 }
1026
1027 /* Start the bfad operations after HAL init done */
1028 bfad_start_ops(bfad);
1029
1030 spin_lock_irqsave(&bfad->bfad_lock, flags);
1031 bfad->bfad_tsk = NULL;
1032 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1033
1034 break;
1035 }
1036
1037 return 0;
1038}
1039
Jing Huang7725ccf2009-09-23 17:46:15 -07001040 /*
1041 * PCI_entry PCI driver entries * {
1042 */
1043
1044/**
1045 * PCI probe entry.
1046 */
1047int
1048bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1049{
1050 struct bfad_s *bfad;
1051 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001052
1053 /*
1054 * For single port cards - only claim function 0
1055 */
1056 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P)
1057 && (PCI_FUNC(pdev->devfn) != 0))
1058 return -ENODEV;
1059
1060 BFA_TRACE(BFA_INFO, "bfad_pci_probe entry");
1061
1062 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1063 if (!bfad) {
1064 error = -ENOMEM;
1065 goto out;
1066 }
1067
1068 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1069 if (!bfad->trcmod) {
1070 printk(KERN_WARNING "Error alloc trace buffer!\n");
1071 error = -ENOMEM;
1072 goto out_alloc_trace_failure;
1073 }
1074
1075 /*
1076 * LOG/TRACE INIT
1077 */
1078 bfa_trc_init(bfad->trcmod);
1079 bfa_trc(bfad, bfad_inst);
1080
1081 bfad->logmod = &bfad->log_data;
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -08001082 bfa_log_init(bfad->logmod, (char *)pci_name(pdev), bfa_os_printf);
Jing Huang7725ccf2009-09-23 17:46:15 -07001083
1084 bfad_drv_log_level_set(bfad);
1085
1086 bfad->aen = &bfad->aen_buf;
1087
1088 if (!(bfad_load_fwimg(pdev))) {
1089 printk(KERN_WARNING "bfad_load_fwimg failure!\n");
1090 kfree(bfad->trcmod);
1091 goto out_alloc_trace_failure;
1092 }
1093
1094 retval = bfad_pci_init(pdev, bfad);
1095 if (retval) {
1096 printk(KERN_WARNING "bfad_pci_init failure!\n");
1097 error = retval;
1098 goto out_pci_init_failure;
1099 }
1100
1101 mutex_lock(&bfad_mutex);
1102 bfad->inst_no = bfad_inst++;
1103 list_add_tail(&bfad->list_entry, &bfad_list);
1104 mutex_unlock(&bfad_mutex);
1105
1106 spin_lock_init(&bfad->bfad_lock);
1107 pci_set_drvdata(pdev, bfad);
1108
1109 bfad->ref_count = 0;
1110 bfad->pport.bfad = bfad;
Jing Huangd9883542010-07-08 19:46:26 -07001111 INIT_LIST_HEAD(&bfad->pbc_pcfg_list);
Jing Huang7725ccf2009-09-23 17:46:15 -07001112
Krishna Gudipatie6714322010-03-03 17:44:02 -08001113 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad, "%s",
1114 "bfad_worker");
1115 if (IS_ERR(bfad->bfad_tsk)) {
1116 printk(KERN_INFO "bfad[%d]: Kernel thread"
1117 " creation failed!\n",
1118 bfad->inst_no);
1119 goto out_kthread_create_failure;
1120 }
1121
Jing Huang7725ccf2009-09-23 17:46:15 -07001122 retval = bfad_drv_init(bfad);
1123 if (retval != BFA_STATUS_OK)
1124 goto out_drv_init_failure;
1125 if (!(bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
Krishna Gudipatie6714322010-03-03 17:44:02 -08001126 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
Jing Huang7725ccf2009-09-23 17:46:15 -07001127 printk(KERN_WARNING "bfad%d: hal init failed\n", bfad->inst_no);
1128 goto ok;
1129 }
1130
Krishna Gudipatie6714322010-03-03 17:44:02 -08001131 retval = bfad_start_ops(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001132 if (retval != BFA_STATUS_OK)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001133 goto out_start_ops_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001134
Krishna Gudipatie6714322010-03-03 17:44:02 -08001135 kthread_stop(bfad->bfad_tsk);
1136 bfad->bfad_tsk = NULL;
Jing Huang7725ccf2009-09-23 17:46:15 -07001137
Jing Huang7725ccf2009-09-23 17:46:15 -07001138ok:
1139 return 0;
1140
Krishna Gudipatie6714322010-03-03 17:44:02 -08001141out_start_ops_failure:
Jing Huang7725ccf2009-09-23 17:46:15 -07001142 bfad_drv_uninit(bfad);
1143out_drv_init_failure:
Krishna Gudipatie6714322010-03-03 17:44:02 -08001144 kthread_stop(bfad->bfad_tsk);
1145out_kthread_create_failure:
Jing Huang7725ccf2009-09-23 17:46:15 -07001146 mutex_lock(&bfad_mutex);
1147 bfad_inst--;
1148 list_del(&bfad->list_entry);
1149 mutex_unlock(&bfad_mutex);
1150 bfad_pci_uninit(pdev, bfad);
1151out_pci_init_failure:
1152 kfree(bfad->trcmod);
1153out_alloc_trace_failure:
1154 kfree(bfad);
1155out:
1156 return error;
1157}
1158
1159/**
1160 * PCI remove entry.
1161 */
1162void
1163bfad_pci_remove(struct pci_dev *pdev)
1164{
1165 struct bfad_s *bfad = pci_get_drvdata(pdev);
1166 unsigned long flags;
1167
1168 bfa_trc(bfad, bfad->inst_no);
1169
Krishna Gudipatie6714322010-03-03 17:44:02 -08001170 spin_lock_irqsave(&bfad->bfad_lock, flags);
1171 if (bfad->bfad_tsk != NULL)
1172 kthread_stop(bfad->bfad_tsk);
1173 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1174
Jing Huang7725ccf2009-09-23 17:46:15 -07001175 if ((bfad->bfad_flags & BFAD_DRV_INIT_DONE)
1176 && !(bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
1177
1178 spin_lock_irqsave(&bfad->bfad_lock, flags);
1179 init_completion(&bfad->comp);
1180 bfa_stop(&bfad->bfa);
1181 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1182 wait_for_completion(&bfad->comp);
1183
1184 bfad_remove_intr(bfad);
1185 del_timer_sync(&bfad->hal_tmo);
1186 goto hal_detach;
1187 } else if (!(bfad->bfad_flags & BFAD_DRV_INIT_DONE)) {
1188 goto remove_sysfs;
1189 }
1190
Krishna Gudipatie6714322010-03-03 17:44:02 -08001191 if (bfad->bfad_flags & BFAD_HAL_START_DONE) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001192 bfad_drv_stop(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001193 } else if (bfad->bfad_flags & BFAD_DRV_INIT_DONE) {
1194 /* Invoking bfa_stop() before bfa_detach
1195 * when HAL and DRV init are success
1196 * but HAL start did not occur.
1197 */
1198 spin_lock_irqsave(&bfad->bfad_lock, flags);
1199 init_completion(&bfad->comp);
1200 bfa_stop(&bfad->bfa);
1201 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1202 wait_for_completion(&bfad->comp);
1203 }
Jing Huang7725ccf2009-09-23 17:46:15 -07001204
1205 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001206 del_timer_sync(&bfad->hal_tmo);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001207
1208 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE)
1209 bfad_fc4_probe_undo(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001210
1211 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
1212 bfad_uncfg_pport(bfad);
1213
1214hal_detach:
1215 spin_lock_irqsave(&bfad->bfad_lock, flags);
1216 bfa_detach(&bfad->bfa);
1217 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1218 bfad_hal_mem_release(bfad);
1219remove_sysfs:
1220
1221 mutex_lock(&bfad_mutex);
1222 bfad_inst--;
1223 list_del(&bfad->list_entry);
1224 mutex_unlock(&bfad_mutex);
1225 bfad_pci_uninit(pdev, bfad);
1226
1227 kfree(bfad->trcmod);
1228 kfree(bfad);
1229}
1230
1231
1232static struct pci_device_id bfad_id_table[] = {
1233 {
1234 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1235 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1236 .subvendor = PCI_ANY_ID,
1237 .subdevice = PCI_ANY_ID,
1238 },
1239 {
1240 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1241 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1242 .subvendor = PCI_ANY_ID,
1243 .subdevice = PCI_ANY_ID,
1244 },
1245 {
1246 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1247 .device = BFA_PCI_DEVICE_ID_CT,
1248 .subvendor = PCI_ANY_ID,
1249 .subdevice = PCI_ANY_ID,
1250 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1251 .class_mask = ~0,
1252 },
Jing Huang293f82d2010-07-08 19:45:20 -07001253 {
1254 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1255 .device = BFA_PCI_DEVICE_ID_CT_FC,
1256 .subvendor = PCI_ANY_ID,
1257 .subdevice = PCI_ANY_ID,
1258 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1259 .class_mask = ~0,
1260 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001261
1262 {0, 0},
1263};
1264
1265MODULE_DEVICE_TABLE(pci, bfad_id_table);
1266
1267static struct pci_driver bfad_pci_driver = {
1268 .name = BFAD_DRIVER_NAME,
1269 .id_table = bfad_id_table,
1270 .probe = bfad_pci_probe,
1271 .remove = __devexit_p(bfad_pci_remove),
1272};
1273
1274/**
1275 * Linux driver module functions
1276 */
1277bfa_status_t
1278bfad_fc4_module_init(void)
1279{
1280 int rc;
1281
1282 rc = bfad_im_module_init();
1283 if (rc != BFA_STATUS_OK)
1284 goto ext;
1285
1286 bfad_tm_module_init();
1287 if (ipfc_enable)
1288 bfad_ipfc_module_init();
1289ext:
1290 return rc;
1291}
1292
1293void
1294bfad_fc4_module_exit(void)
1295{
1296 if (ipfc_enable)
1297 bfad_ipfc_module_exit();
1298 bfad_tm_module_exit();
1299 bfad_im_module_exit();
1300}
1301
1302/**
1303 * Driver module init.
1304 */
1305static int __init
1306bfad_init(void)
1307{
1308 int error = 0;
1309
1310 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
1311 BFAD_DRIVER_VERSION);
1312
1313 if (num_sgpgs > 0)
1314 num_sgpgs_parm = num_sgpgs;
1315
1316 error = bfad_fc4_module_init();
1317 if (error) {
1318 error = -ENOMEM;
1319 printk(KERN_WARNING "bfad_fc4_module_init failure\n");
1320 goto ext;
1321 }
1322
1323 if (!strcmp(FCPI_NAME, " fcpim"))
1324 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_IM;
1325 if (!strcmp(FCPT_NAME, " fcptm"))
1326 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_TM;
1327 if (!strcmp(IPFC_NAME, " ipfc"))
1328 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_IPFC;
1329
1330 bfa_ioc_auto_recover(ioc_auto_recover);
1331 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
1332 error = pci_register_driver(&bfad_pci_driver);
1333
1334 if (error) {
1335 printk(KERN_WARNING "bfad pci_register_driver failure\n");
1336 goto ext;
1337 }
1338
1339 return 0;
1340
1341ext:
1342 bfad_fc4_module_exit();
1343 return error;
1344}
1345
1346/**
1347 * Driver module exit.
1348 */
1349static void __exit
1350bfad_exit(void)
1351{
1352 pci_unregister_driver(&bfad_pci_driver);
1353 bfad_fc4_module_exit();
1354 bfad_free_fwimg();
1355}
1356
1357#define BFAD_PROTO_NAME FCPI_NAME FCPT_NAME IPFC_NAME
1358
1359module_init(bfad_init);
1360module_exit(bfad_exit);
1361MODULE_LICENSE("GPL");
1362MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1363MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1364MODULE_VERSION(BFAD_DRIVER_VERSION);
1365
1366