blob: 915a29d6c7ad586c9f36e0399354498d51fa240d [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;
Jing Huangab2a9ba2010-07-08 20:02:55 -070060int bfa_debugfs_enable = 1;
Jing Huang7725ccf2009-09-23 17:46:15 -070061
62module_param(os_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070063MODULE_PARM_DESC(os_name, "OS name of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070064module_param(os_patch, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070065MODULE_PARM_DESC(os_patch, "OS patch level of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070066module_param(host_name, charp, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070067MODULE_PARM_DESC(host_name, "Hostname of the hba host machine");
Jing Huang7725ccf2009-09-23 17:46:15 -070068module_param(num_rports, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070069MODULE_PARM_DESC(num_rports, "Max number of rports supported per port"
70 " (physical/logical), default=1024");
Jing Huang7725ccf2009-09-23 17:46:15 -070071module_param(num_ios, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070072MODULE_PARM_DESC(num_ios, "Max number of ioim requests, default=2000");
Jing Huang7725ccf2009-09-23 17:46:15 -070073module_param(num_tms, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070074MODULE_PARM_DESC(num_tms, "Max number of task im requests, default=128");
Jing Huang7725ccf2009-09-23 17:46:15 -070075module_param(num_fcxps, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070076MODULE_PARM_DESC(num_fcxps, "Max number of fcxp requests, default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070077module_param(num_ufbufs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070078MODULE_PARM_DESC(num_ufbufs, "Max number of unsolicited frame buffers,"
79 " default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070080module_param(reqq_size, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070081MODULE_PARM_DESC(reqq_size, "Max number of request queue elements,"
82 " default=256");
Jing Huang7725ccf2009-09-23 17:46:15 -070083module_param(rspq_size, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070084MODULE_PARM_DESC(rspq_size, "Max number of response queue elements,"
85 " default=64");
Jing Huang7725ccf2009-09-23 17:46:15 -070086module_param(num_sgpgs, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070087MODULE_PARM_DESC(num_sgpgs, "Number of scatter/gather pages, default=2048");
Jing Huang7725ccf2009-09-23 17:46:15 -070088module_param(rport_del_timeout, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070089MODULE_PARM_DESC(rport_del_timeout, "Rport delete timeout, default=90 secs,"
90 " Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -070091module_param(bfa_lun_queue_depth, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070092MODULE_PARM_DESC(bfa_lun_queue_depth, "Lun queue depth, default=32,"
93 " Range[>0]");
Jing Huang7725ccf2009-09-23 17:46:15 -070094module_param(bfa_io_max_sge, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070095MODULE_PARM_DESC(bfa_io_max_sge, "Max io scatter/gather elements, default=255");
Jing Huang7725ccf2009-09-23 17:46:15 -070096module_param(log_level, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -070097MODULE_PARM_DESC(log_level, "Driver log level, default=3,"
98 " Range[Critical:1|Error:2|Warning:3|Info:4]");
Jing Huang7725ccf2009-09-23 17:46:15 -070099module_param(ioc_auto_recover, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700100MODULE_PARM_DESC(ioc_auto_recover, "IOC auto recovery, default=1,"
101 " Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700102module_param(ipfc_enable, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700103MODULE_PARM_DESC(ipfc_enable, "Enable IPoFC, default=0, Range[off:0|on:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700104module_param(bfa_linkup_delay, int, S_IRUGO | S_IWUSR);
Jing Huang604158ad2010-07-08 19:59:49 -0700105MODULE_PARM_DESC(bfa_linkup_delay, "Link up delay, default=30 secs for boot"
106 " port. Otherwise Range[>0]");
107module_param(fdmi_enable, int, S_IRUGO | S_IWUSR);
108MODULE_PARM_DESC(fdmi_enable, "Enables fdmi registration, default=1,"
109 " Range[false:0|true:1]");
Jing Huangab2a9ba2010-07-08 20:02:55 -0700110module_param(bfa_debugfs_enable, int, S_IRUGO | S_IWUSR);
111MODULE_PARM_DESC(bfa_debugfs_enable, "Enables debugfs feature, default=1,"
112 " Range[false:0|true:1]");
Jing Huang7725ccf2009-09-23 17:46:15 -0700113
114/*
115 * Stores the module parm num_sgpgs value;
116 * used to reset for bfad next instance.
117 */
118static int num_sgpgs_parm;
119
120static bfa_status_t
121bfad_fc4_probe(struct bfad_s *bfad)
122{
123 int rc;
124
125 rc = bfad_im_probe(bfad);
126 if (rc != BFA_STATUS_OK)
127 goto ext;
128
129 bfad_tm_probe(bfad);
130
131 if (ipfc_enable)
132 bfad_ipfc_probe(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800133
134 bfad->bfad_flags |= BFAD_FC4_PROBE_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700135ext:
136 return rc;
137}
138
139static void
140bfad_fc4_probe_undo(struct bfad_s *bfad)
141{
142 bfad_im_probe_undo(bfad);
143 bfad_tm_probe_undo(bfad);
144 if (ipfc_enable)
145 bfad_ipfc_probe_undo(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800146 bfad->bfad_flags &= ~BFAD_FC4_PROBE_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700147}
148
149static void
150bfad_fc4_probe_post(struct bfad_s *bfad)
151{
152 if (bfad->im)
153 bfad_im_probe_post(bfad->im);
154
155 bfad_tm_probe_post(bfad);
156 if (ipfc_enable)
157 bfad_ipfc_probe_post(bfad);
158}
159
160static bfa_status_t
161bfad_fc4_port_new(struct bfad_s *bfad, struct bfad_port_s *port, int roles)
162{
163 int rc = BFA_STATUS_FAILED;
164
165 if (roles & BFA_PORT_ROLE_FCP_IM)
166 rc = bfad_im_port_new(bfad, port);
167 if (rc != BFA_STATUS_OK)
168 goto ext;
169
170 if (roles & BFA_PORT_ROLE_FCP_TM)
171 rc = bfad_tm_port_new(bfad, port);
172 if (rc != BFA_STATUS_OK)
173 goto ext;
174
175 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
176 rc = bfad_ipfc_port_new(bfad, port, port->pvb_type);
177ext:
178 return rc;
179}
180
181static void
182bfad_fc4_port_delete(struct bfad_s *bfad, struct bfad_port_s *port, int roles)
183{
184 if (roles & BFA_PORT_ROLE_FCP_IM)
185 bfad_im_port_delete(bfad, port);
186
187 if (roles & BFA_PORT_ROLE_FCP_TM)
188 bfad_tm_port_delete(bfad, port);
189
190 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
191 bfad_ipfc_port_delete(bfad, port);
192}
193
194/**
195 * BFA callbacks
196 */
197void
198bfad_hcb_comp(void *arg, bfa_status_t status)
199{
200 struct bfad_hal_comp *fcomp = (struct bfad_hal_comp *)arg;
201
202 fcomp->status = status;
203 complete(&fcomp->comp);
204}
205
206/**
207 * bfa_init callback
208 */
209void
210bfa_cb_init(void *drv, bfa_status_t init_status)
211{
212 struct bfad_s *bfad = drv;
213
Krishna Gudipatie6714322010-03-03 17:44:02 -0800214 if (init_status == BFA_STATUS_OK) {
Jing Huang7725ccf2009-09-23 17:46:15 -0700215 bfad->bfad_flags |= BFAD_HAL_INIT_DONE;
216
Krishna Gudipatie6714322010-03-03 17:44:02 -0800217 /* If BFAD_HAL_INIT_FAIL flag is set:
218 * Wake up the kernel thread to start
219 * the bfad operations after HAL init done
220 */
221 if ((bfad->bfad_flags & BFAD_HAL_INIT_FAIL)) {
222 bfad->bfad_flags &= ~BFAD_HAL_INIT_FAIL;
223 wake_up_process(bfad->bfad_tsk);
224 }
225 }
226
Jing Huang7725ccf2009-09-23 17:46:15 -0700227 complete(&bfad->comp);
228}
229
230
231
232/**
233 * BFA_FCS callbacks
234 */
235static struct bfad_port_s *
236bfad_get_drv_port(struct bfad_s *bfad, struct bfad_vf_s *vf_drv,
237 struct bfad_vport_s *vp_drv)
238{
Jing Huangf8ceafd2009-09-25 12:29:54 -0700239 return (vp_drv) ? (&(vp_drv)->drv_port)
240 : ((vf_drv) ? (&(vf_drv)->base_port) : (&(bfad)->pport));
Jing Huang7725ccf2009-09-23 17:46:15 -0700241}
242
243struct bfad_port_s *
244bfa_fcb_port_new(struct bfad_s *bfad, struct bfa_fcs_port_s *port,
245 enum bfa_port_role roles, struct bfad_vf_s *vf_drv,
246 struct bfad_vport_s *vp_drv)
247{
248 bfa_status_t rc;
249 struct bfad_port_s *port_drv;
250
251 if (!vp_drv && !vf_drv) {
252 port_drv = &bfad->pport;
253 port_drv->pvb_type = BFAD_PORT_PHYS_BASE;
254 } else if (!vp_drv && vf_drv) {
255 port_drv = &vf_drv->base_port;
256 port_drv->pvb_type = BFAD_PORT_VF_BASE;
257 } else if (vp_drv && !vf_drv) {
258 port_drv = &vp_drv->drv_port;
259 port_drv->pvb_type = BFAD_PORT_PHYS_VPORT;
260 } else {
261 port_drv = &vp_drv->drv_port;
262 port_drv->pvb_type = BFAD_PORT_VF_VPORT;
263 }
264
265 port_drv->fcs_port = port;
266 port_drv->roles = roles;
267 rc = bfad_fc4_port_new(bfad, port_drv, roles);
268 if (rc != BFA_STATUS_OK) {
269 bfad_fc4_port_delete(bfad, port_drv, roles);
270 port_drv = NULL;
271 }
272
273 return port_drv;
274}
275
276void
277bfa_fcb_port_delete(struct bfad_s *bfad, enum bfa_port_role roles,
278 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
279{
280 struct bfad_port_s *port_drv;
281
282 /*
283 * this will be only called from rmmod context
284 */
285 if (vp_drv && !vp_drv->comp_del) {
286 port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
287 bfa_trc(bfad, roles);
288 bfad_fc4_port_delete(bfad, port_drv, roles);
289 }
290}
291
292void
293bfa_fcb_port_online(struct bfad_s *bfad, enum bfa_port_role roles,
294 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
295{
296 struct bfad_port_s *port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
297
298 if (roles & BFA_PORT_ROLE_FCP_IM)
299 bfad_im_port_online(bfad, port_drv);
300
301 if (roles & BFA_PORT_ROLE_FCP_TM)
302 bfad_tm_port_online(bfad, port_drv);
303
304 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
305 bfad_ipfc_port_online(bfad, port_drv);
306
307 bfad->bfad_flags |= BFAD_PORT_ONLINE;
308}
309
310void
311bfa_fcb_port_offline(struct bfad_s *bfad, enum bfa_port_role roles,
312 struct bfad_vf_s *vf_drv, struct bfad_vport_s *vp_drv)
313{
314 struct bfad_port_s *port_drv = bfad_get_drv_port(bfad, vf_drv, vp_drv);
315
316 if (roles & BFA_PORT_ROLE_FCP_IM)
317 bfad_im_port_offline(bfad, port_drv);
318
319 if (roles & BFA_PORT_ROLE_FCP_TM)
320 bfad_tm_port_offline(bfad, port_drv);
321
322 if ((roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable)
323 bfad_ipfc_port_offline(bfad, port_drv);
324}
325
326void
327bfa_fcb_vport_delete(struct bfad_vport_s *vport_drv)
328{
329 if (vport_drv->comp_del) {
330 complete(vport_drv->comp_del);
331 return;
332 }
Jing Huang7725ccf2009-09-23 17:46:15 -0700333}
334
335/**
336 * FCS RPORT alloc callback, after successful PLOGI by FCS
337 */
338bfa_status_t
339bfa_fcb_rport_alloc(struct bfad_s *bfad, struct bfa_fcs_rport_s **rport,
340 struct bfad_rport_s **rport_drv)
341{
342 bfa_status_t rc = BFA_STATUS_OK;
343
344 *rport_drv = kzalloc(sizeof(struct bfad_rport_s), GFP_ATOMIC);
345 if (*rport_drv == NULL) {
346 rc = BFA_STATUS_ENOMEM;
347 goto ext;
348 }
349
350 *rport = &(*rport_drv)->fcs_rport;
351
352ext:
353 return rc;
354}
355
Jing Huangd9883542010-07-08 19:46:26 -0700356/**
357 * @brief
358 * FCS PBC VPORT Create
359 */
360void
361bfa_fcb_pbc_vport_create(struct bfad_s *bfad, struct bfi_pbc_vport_s pbc_vport)
362{
Jing Huang7725ccf2009-09-23 17:46:15 -0700363
Jing Huangd9883542010-07-08 19:46:26 -0700364 struct bfad_pcfg_s *pcfg;
365
366 pcfg = kzalloc(sizeof(struct bfad_pcfg_s), GFP_ATOMIC);
367 if (!pcfg) {
368 bfa_trc(bfad, 0);
369 return;
370 }
371
372 pcfg->port_cfg.roles = BFA_PORT_ROLE_FCP_IM;
373 pcfg->port_cfg.pwwn = pbc_vport.vp_pwwn;
374 pcfg->port_cfg.nwwn = pbc_vport.vp_nwwn;
375 pcfg->port_cfg.preboot_vp = BFA_TRUE;
376
377 list_add_tail(&pcfg->list_entry, &bfad->pbc_pcfg_list);
378
379 return;
380}
Jing Huang7725ccf2009-09-23 17:46:15 -0700381
382void
383bfad_hal_mem_release(struct bfad_s *bfad)
384{
385 int i;
386 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
387 struct bfa_mem_elem_s *meminfo_elem;
388
389 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
390 meminfo_elem = &hal_meminfo->meminfo[i];
391 if (meminfo_elem->kva != NULL) {
392 switch (meminfo_elem->mem_type) {
393 case BFA_MEM_TYPE_KVA:
394 vfree(meminfo_elem->kva);
395 break;
396 case BFA_MEM_TYPE_DMA:
397 dma_free_coherent(&bfad->pcidev->dev,
398 meminfo_elem->mem_len,
399 meminfo_elem->kva,
400 (dma_addr_t) meminfo_elem->dma);
401 break;
402 default:
403 bfa_assert(0);
404 break;
405 }
406 }
407 }
408
409 memset(hal_meminfo, 0, sizeof(struct bfa_meminfo_s));
410}
411
412void
413bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg)
414{
415 if (num_rports > 0)
416 bfa_cfg->fwcfg.num_rports = num_rports;
417 if (num_ios > 0)
418 bfa_cfg->fwcfg.num_ioim_reqs = num_ios;
419 if (num_tms > 0)
420 bfa_cfg->fwcfg.num_tskim_reqs = num_tms;
421 if (num_fcxps > 0)
422 bfa_cfg->fwcfg.num_fcxp_reqs = num_fcxps;
423 if (num_ufbufs > 0)
424 bfa_cfg->fwcfg.num_uf_bufs = num_ufbufs;
425 if (reqq_size > 0)
426 bfa_cfg->drvcfg.num_reqq_elems = reqq_size;
427 if (rspq_size > 0)
428 bfa_cfg->drvcfg.num_rspq_elems = rspq_size;
429 if (num_sgpgs > 0)
430 bfa_cfg->drvcfg.num_sgpgs = num_sgpgs;
431
432 /*
433 * populate the hal values back to the driver for sysfs use.
434 * otherwise, the default values will be shown as 0 in sysfs
435 */
436 num_rports = bfa_cfg->fwcfg.num_rports;
437 num_ios = bfa_cfg->fwcfg.num_ioim_reqs;
438 num_tms = bfa_cfg->fwcfg.num_tskim_reqs;
439 num_fcxps = bfa_cfg->fwcfg.num_fcxp_reqs;
440 num_ufbufs = bfa_cfg->fwcfg.num_uf_bufs;
441 reqq_size = bfa_cfg->drvcfg.num_reqq_elems;
442 rspq_size = bfa_cfg->drvcfg.num_rspq_elems;
443 num_sgpgs = bfa_cfg->drvcfg.num_sgpgs;
444}
445
446bfa_status_t
447bfad_hal_mem_alloc(struct bfad_s *bfad)
448{
449 struct bfa_meminfo_s *hal_meminfo = &bfad->meminfo;
450 struct bfa_mem_elem_s *meminfo_elem;
451 bfa_status_t rc = BFA_STATUS_OK;
452 dma_addr_t phys_addr;
453 int retry_count = 0;
454 int reset_value = 1;
455 int min_num_sgpgs = 512;
456 void *kva;
457 int i;
458
459 bfa_cfg_get_default(&bfad->ioc_cfg);
460
461retry:
462 bfad_update_hal_cfg(&bfad->ioc_cfg);
463 bfad->cfg_data.ioc_queue_depth = bfad->ioc_cfg.fwcfg.num_ioim_reqs;
464 bfa_cfg_get_meminfo(&bfad->ioc_cfg, hal_meminfo);
465
466 for (i = 0; i < BFA_MEM_TYPE_MAX; i++) {
467 meminfo_elem = &hal_meminfo->meminfo[i];
468 switch (meminfo_elem->mem_type) {
469 case BFA_MEM_TYPE_KVA:
470 kva = vmalloc(meminfo_elem->mem_len);
471 if (kva == NULL) {
472 bfad_hal_mem_release(bfad);
473 rc = BFA_STATUS_ENOMEM;
474 goto ext;
475 }
476 memset(kva, 0, meminfo_elem->mem_len);
477 meminfo_elem->kva = kva;
478 break;
479 case BFA_MEM_TYPE_DMA:
480 kva = dma_alloc_coherent(&bfad->pcidev->dev,
481 meminfo_elem->mem_len,
482 &phys_addr, GFP_KERNEL);
483 if (kva == NULL) {
484 bfad_hal_mem_release(bfad);
485 /*
486 * If we cannot allocate with default
487 * num_sgpages try with half the value.
488 */
489 if (num_sgpgs > min_num_sgpgs) {
490 printk(KERN_INFO "bfad[%d]: memory"
491 " allocation failed with"
492 " num_sgpgs: %d\n",
493 bfad->inst_no, num_sgpgs);
494 nextLowerInt(&num_sgpgs);
495 printk(KERN_INFO "bfad[%d]: trying to"
496 " allocate memory with"
497 " num_sgpgs: %d\n",
498 bfad->inst_no, num_sgpgs);
499 retry_count++;
500 goto retry;
501 } else {
502 if (num_sgpgs_parm > 0)
503 num_sgpgs = num_sgpgs_parm;
504 else {
505 reset_value =
506 (1 << retry_count);
507 num_sgpgs *= reset_value;
508 }
509 rc = BFA_STATUS_ENOMEM;
510 goto ext;
511 }
512 }
513
514 if (num_sgpgs_parm > 0)
515 num_sgpgs = num_sgpgs_parm;
516 else {
517 reset_value = (1 << retry_count);
518 num_sgpgs *= reset_value;
519 }
520
521 memset(kva, 0, meminfo_elem->mem_len);
522 meminfo_elem->kva = kva;
523 meminfo_elem->dma = phys_addr;
524 break;
525 default:
526 break;
527
528 }
529 }
530ext:
531 return rc;
532}
533
534/**
535 * Create a vport under a vf.
536 */
537bfa_status_t
538bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
Jing Huangd9883542010-07-08 19:46:26 -0700539 struct bfa_port_cfg_s *port_cfg, struct device *dev)
Jing Huang7725ccf2009-09-23 17:46:15 -0700540{
541 struct bfad_vport_s *vport;
Jing Huangd9883542010-07-08 19:46:26 -0700542 int rc = BFA_STATUS_OK;
Jing Huang7725ccf2009-09-23 17:46:15 -0700543 unsigned long flags;
544 struct completion fcomp;
545
546 vport = kzalloc(sizeof(struct bfad_vport_s), GFP_KERNEL);
547 if (!vport) {
548 rc = BFA_STATUS_ENOMEM;
549 goto ext;
550 }
551
552 vport->drv_port.bfad = bfad;
553 spin_lock_irqsave(&bfad->bfad_lock, flags);
Jing Huangd9883542010-07-08 19:46:26 -0700554 if (port_cfg->preboot_vp == BFA_TRUE)
555 rc = bfa_fcs_pbc_vport_create(&vport->fcs_vport,
556 &bfad->bfa_fcs, vf_id, port_cfg, vport);
557 else
558 rc = bfa_fcs_vport_create(&vport->fcs_vport,
559 &bfad->bfa_fcs, vf_id, port_cfg, vport);
Jing Huang7725ccf2009-09-23 17:46:15 -0700560 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
561
562 if (rc != BFA_STATUS_OK)
563 goto ext_free_vport;
564
565 if (port_cfg->roles & BFA_PORT_ROLE_FCP_IM) {
Jing Huangb5042932010-03-19 11:05:39 -0700566 rc = bfad_im_scsi_host_alloc(bfad, vport->drv_port.im_port,
567 dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700568 if (rc != BFA_STATUS_OK)
569 goto ext_free_fcs_vport;
570 }
571
572 spin_lock_irqsave(&bfad->bfad_lock, flags);
573 bfa_fcs_vport_start(&vport->fcs_vport);
574 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
575
576 return BFA_STATUS_OK;
577
578ext_free_fcs_vport:
579 spin_lock_irqsave(&bfad->bfad_lock, flags);
580 vport->comp_del = &fcomp;
581 init_completion(vport->comp_del);
582 bfa_fcs_vport_delete(&vport->fcs_vport);
583 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
584 wait_for_completion(vport->comp_del);
585ext_free_vport:
586 kfree(vport);
587ext:
588 return rc;
589}
590
591/**
592 * Create a vf and its base vport implicitely.
593 */
594bfa_status_t
595bfad_vf_create(struct bfad_s *bfad, u16 vf_id,
596 struct bfa_port_cfg_s *port_cfg)
597{
598 struct bfad_vf_s *vf;
599 int rc = BFA_STATUS_OK;
600
601 vf = kzalloc(sizeof(struct bfad_vf_s), GFP_KERNEL);
602 if (!vf) {
603 rc = BFA_STATUS_FAILED;
604 goto ext;
605 }
606
607 rc = bfa_fcs_vf_create(&vf->fcs_vf, &bfad->bfa_fcs, vf_id, port_cfg,
608 vf);
609 if (rc != BFA_STATUS_OK)
610 kfree(vf);
611ext:
612 return rc;
613}
614
615void
616bfad_bfa_tmo(unsigned long data)
617{
618 struct bfad_s *bfad = (struct bfad_s *)data;
619 unsigned long flags;
620 struct list_head doneq;
621
622 spin_lock_irqsave(&bfad->bfad_lock, flags);
623
624 bfa_timer_tick(&bfad->bfa);
625
626 bfa_comp_deq(&bfad->bfa, &doneq);
627 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
628
629 if (!list_empty(&doneq)) {
630 bfa_comp_process(&bfad->bfa, &doneq);
631 spin_lock_irqsave(&bfad->bfad_lock, flags);
632 bfa_comp_free(&bfad->bfa, &doneq);
633 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
634 }
635
636 mod_timer(&bfad->hal_tmo, jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
637}
638
639void
640bfad_init_timer(struct bfad_s *bfad)
641{
642 init_timer(&bfad->hal_tmo);
643 bfad->hal_tmo.function = bfad_bfa_tmo;
644 bfad->hal_tmo.data = (unsigned long)bfad;
645
646 mod_timer(&bfad->hal_tmo, jiffies + msecs_to_jiffies(BFA_TIMER_FREQ));
647}
648
649int
650bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad)
651{
Jing Huang7725ccf2009-09-23 17:46:15 -0700652 int rc = -ENODEV;
653
654 if (pci_enable_device(pdev)) {
655 BFA_PRINTF(BFA_ERR, "pci_enable_device fail %p\n", pdev);
656 goto out;
657 }
658
659 if (pci_request_regions(pdev, BFAD_DRIVER_NAME))
660 goto out_disable_device;
661
662 pci_set_master(pdev);
663
664
665 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0)
666 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
667 BFA_PRINTF(BFA_ERR, "pci_set_dma_mask fail %p\n", pdev);
668 goto out_release_region;
669 }
670
Jing Huangb3522f02010-03-19 11:06:44 -0700671 bfad->pci_bar0_kva = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
Jing Huang7725ccf2009-09-23 17:46:15 -0700672
673 if (bfad->pci_bar0_kva == NULL) {
674 BFA_PRINTF(BFA_ERR, "Fail to map bar0\n");
675 goto out_release_region;
676 }
677
678 bfad->hal_pcidev.pci_slot = PCI_SLOT(pdev->devfn);
679 bfad->hal_pcidev.pci_func = PCI_FUNC(pdev->devfn);
680 bfad->hal_pcidev.pci_bar_kva = bfad->pci_bar0_kva;
681 bfad->hal_pcidev.device_id = pdev->device;
682 bfad->pci_name = pci_name(pdev);
683
684 bfad->pci_attr.vendor_id = pdev->vendor;
685 bfad->pci_attr.device_id = pdev->device;
686 bfad->pci_attr.ssid = pdev->subsystem_device;
687 bfad->pci_attr.ssvid = pdev->subsystem_vendor;
688 bfad->pci_attr.pcifn = PCI_FUNC(pdev->devfn);
689
690 bfad->pcidev = pdev;
691 return 0;
692
693out_release_region:
694 pci_release_regions(pdev);
695out_disable_device:
696 pci_disable_device(pdev);
697out:
698 return rc;
699}
700
701void
702bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad)
703{
Jing Huang7725ccf2009-09-23 17:46:15 -0700704 pci_iounmap(pdev, bfad->pci_bar0_kva);
Jing Huang7725ccf2009-09-23 17:46:15 -0700705 pci_release_regions(pdev);
706 pci_disable_device(pdev);
707 pci_set_drvdata(pdev, NULL);
708}
709
710void
711bfad_fcs_port_cfg(struct bfad_s *bfad)
712{
713 struct bfa_port_cfg_s port_cfg;
714 struct bfa_pport_attr_s attr;
715 char symname[BFA_SYMNAME_MAXLEN];
716
717 sprintf(symname, "%s-%d", BFAD_DRIVER_NAME, bfad->inst_no);
718 memcpy(port_cfg.sym_name.symname, symname, strlen(symname));
Krishna Gudipati1c8a4c32010-03-05 19:37:37 -0800719 bfa_fcport_get_attr(&bfad->bfa, &attr);
Jing Huang7725ccf2009-09-23 17:46:15 -0700720 port_cfg.nwwn = attr.nwwn;
721 port_cfg.pwwn = attr.pwwn;
722
723 bfa_fcs_cfg_base_port(&bfad->bfa_fcs, &port_cfg);
724}
725
726bfa_status_t
727bfad_drv_init(struct bfad_s *bfad)
728{
729 bfa_status_t rc;
730 unsigned long flags;
731 struct bfa_fcs_driver_info_s driver_info;
Jing Huang7725ccf2009-09-23 17:46:15 -0700732
733 bfad->cfg_data.rport_del_timeout = rport_del_timeout;
734 bfad->cfg_data.lun_queue_depth = bfa_lun_queue_depth;
735 bfad->cfg_data.io_max_sge = bfa_io_max_sge;
736 bfad->cfg_data.binding_method = FCP_PWWN_BINDING;
737
738 rc = bfad_hal_mem_alloc(bfad);
739 if (rc != BFA_STATUS_OK) {
740 printk(KERN_WARNING "bfad%d bfad_hal_mem_alloc failure\n",
741 bfad->inst_no);
742 printk(KERN_WARNING
743 "Not enough memory to attach all Brocade HBA ports,"
744 " System may need more memory.\n");
745 goto out_hal_mem_alloc_failure;
746 }
747
748 bfa_init_log(&bfad->bfa, bfad->logmod);
749 bfa_init_trc(&bfad->bfa, bfad->trcmod);
750 bfa_init_aen(&bfad->bfa, bfad->aen);
Krishna Gudipati2993cc72010-03-05 19:36:47 -0800751 memset(bfad->file_map, 0, sizeof(bfad->file_map));
Jing Huang7725ccf2009-09-23 17:46:15 -0700752 bfa_init_plog(&bfad->bfa, &bfad->plog_buf);
753 bfa_plog_init(&bfad->plog_buf);
754 bfa_plog_str(&bfad->plog_buf, BFA_PL_MID_DRVR, BFA_PL_EID_DRIVER_START,
755 0, "Driver Attach");
756
757 bfa_attach(&bfad->bfa, bfad, &bfad->ioc_cfg, &bfad->meminfo,
758 &bfad->hal_pcidev);
759
760 init_completion(&bfad->comp);
761
762 /*
763 * Enable Interrupt and wait bfa_init completion
764 */
765 if (bfad_setup_intr(bfad)) {
766 printk(KERN_WARNING "bfad%d: bfad_setup_intr failed\n",
767 bfad->inst_no);
768 goto out_setup_intr_failure;
769 }
770
771 spin_lock_irqsave(&bfad->bfad_lock, flags);
772 bfa_init(&bfad->bfa);
773 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
774
775 /*
776 * Set up interrupt handler for each vectors
777 */
778 if ((bfad->bfad_flags & BFAD_MSIX_ON)
779 && bfad_install_msix_handler(bfad)) {
780 printk(KERN_WARNING "%s: install_msix failed, bfad%d\n",
Jing Huangf8ceafd2009-09-25 12:29:54 -0700781 __func__, bfad->inst_no);
Jing Huang7725ccf2009-09-23 17:46:15 -0700782 }
783
784 bfad_init_timer(bfad);
785
786 wait_for_completion(&bfad->comp);
787
788 memset(&driver_info, 0, sizeof(driver_info));
789 strncpy(driver_info.version, BFAD_DRIVER_VERSION,
790 sizeof(driver_info.version) - 1);
791 if (host_name)
792 strncpy(driver_info.host_machine_name, host_name,
793 sizeof(driver_info.host_machine_name) - 1);
794 if (os_name)
795 strncpy(driver_info.host_os_name, os_name,
796 sizeof(driver_info.host_os_name) - 1);
797 if (os_patch)
798 strncpy(driver_info.host_os_patch, os_patch,
799 sizeof(driver_info.host_os_patch) - 1);
800
801 strncpy(driver_info.os_device_name, bfad->pci_name,
802 sizeof(driver_info.os_device_name - 1));
803
804 /*
805 * FCS INIT
806 */
807 spin_lock_irqsave(&bfad->bfad_lock, flags);
808 bfa_fcs_log_init(&bfad->bfa_fcs, bfad->logmod);
809 bfa_fcs_trc_init(&bfad->bfa_fcs, bfad->trcmod);
810 bfa_fcs_aen_init(&bfad->bfa_fcs, bfad->aen);
Krishna Gudipati82794a22010-03-03 17:43:30 -0800811 bfa_fcs_attach(&bfad->bfa_fcs, &bfad->bfa, bfad, BFA_FALSE);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800812
813 /* Do FCS init only when HAL init is done */
814 if ((bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
815 bfa_fcs_init(&bfad->bfa_fcs);
816 bfad->bfad_flags |= BFAD_FCS_INIT_DONE;
817 }
818
Jing Huang7725ccf2009-09-23 17:46:15 -0700819 bfa_fcs_driver_info_init(&bfad->bfa_fcs, &driver_info);
Krishna Gudipati5b098082010-03-03 17:43:19 -0800820 bfa_fcs_set_fdmi_param(&bfad->bfa_fcs, fdmi_enable);
Jing Huang7725ccf2009-09-23 17:46:15 -0700821 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
822
823 bfad->bfad_flags |= BFAD_DRV_INIT_DONE;
824 return BFA_STATUS_OK;
825
826out_setup_intr_failure:
827 bfa_detach(&bfad->bfa);
828 bfad_hal_mem_release(bfad);
829out_hal_mem_alloc_failure:
830 return BFA_STATUS_FAILED;
831}
832
833void
834bfad_drv_uninit(struct bfad_s *bfad)
835{
Krishna Gudipatie6714322010-03-03 17:44:02 -0800836 unsigned long flags;
837
838 spin_lock_irqsave(&bfad->bfad_lock, flags);
839 init_completion(&bfad->comp);
840 bfa_stop(&bfad->bfa);
841 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
842 wait_for_completion(&bfad->comp);
843
Jing Huang7725ccf2009-09-23 17:46:15 -0700844 del_timer_sync(&bfad->hal_tmo);
845 bfa_isr_disable(&bfad->bfa);
846 bfa_detach(&bfad->bfa);
847 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -0700848 bfad_hal_mem_release(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -0800849
850 bfad->bfad_flags &= ~BFAD_DRV_INIT_DONE;
Jing Huang7725ccf2009-09-23 17:46:15 -0700851}
852
853void
854bfad_drv_start(struct bfad_s *bfad)
855{
856 unsigned long flags;
857
858 spin_lock_irqsave(&bfad->bfad_lock, flags);
859 bfa_start(&bfad->bfa);
860 bfa_fcs_start(&bfad->bfa_fcs);
861 bfad->bfad_flags |= BFAD_HAL_START_DONE;
862 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
863
864 bfad_fc4_probe_post(bfad);
865}
866
867void
868bfad_drv_stop(struct bfad_s *bfad)
869{
870 unsigned long flags;
871
872 spin_lock_irqsave(&bfad->bfad_lock, flags);
873 init_completion(&bfad->comp);
874 bfad->pport.flags |= BFAD_PORT_DELETE;
875 bfa_fcs_exit(&bfad->bfa_fcs);
876 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
877 wait_for_completion(&bfad->comp);
878
879 spin_lock_irqsave(&bfad->bfad_lock, flags);
880 init_completion(&bfad->comp);
881 bfa_stop(&bfad->bfa);
882 bfad->bfad_flags &= ~BFAD_HAL_START_DONE;
883 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
884 wait_for_completion(&bfad->comp);
885}
886
887bfa_status_t
888bfad_cfg_pport(struct bfad_s *bfad, enum bfa_port_role role)
889{
890 int rc = BFA_STATUS_OK;
891
892 /*
893 * Allocate scsi_host for the physical port
894 */
895 if ((bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IM)
896 && (role & BFA_PORT_ROLE_FCP_IM)) {
897 if (bfad->pport.im_port == NULL) {
898 rc = BFA_STATUS_FAILED;
899 goto out;
900 }
901
Jing Huangb5042932010-03-19 11:05:39 -0700902 rc = bfad_im_scsi_host_alloc(bfad, bfad->pport.im_port,
903 &bfad->pcidev->dev);
Jing Huang7725ccf2009-09-23 17:46:15 -0700904 if (rc != BFA_STATUS_OK)
905 goto out;
906
907 bfad->pport.roles |= BFA_PORT_ROLE_FCP_IM;
908 }
909
Jing Huangab2a9ba2010-07-08 20:02:55 -0700910 /* Setup the debugfs node for this scsi_host */
911 if (bfa_debugfs_enable)
912 bfad_debugfs_init(&bfad->pport);
913
Jing Huang7725ccf2009-09-23 17:46:15 -0700914 bfad->bfad_flags |= BFAD_CFG_PPORT_DONE;
915
916out:
917 return rc;
918}
919
920void
921bfad_uncfg_pport(struct bfad_s *bfad)
922{
Jing Huangab2a9ba2010-07-08 20:02:55 -0700923 /* Remove the debugfs node for this scsi_host */
924 kfree(bfad->regdata);
925 bfad_debugfs_exit(&bfad->pport);
926
Jing Huang7725ccf2009-09-23 17:46:15 -0700927 if ((bfad->pport.roles & BFA_PORT_ROLE_FCP_IPFC) && ipfc_enable) {
928 bfad_ipfc_port_delete(bfad, &bfad->pport);
929 bfad->pport.roles &= ~BFA_PORT_ROLE_FCP_IPFC;
930 }
931
932 if ((bfad_supported_fc4s & BFA_PORT_ROLE_FCP_IM)
933 && (bfad->pport.roles & BFA_PORT_ROLE_FCP_IM)) {
934 bfad_im_scsi_host_free(bfad, bfad->pport.im_port);
935 bfad_im_port_clean(bfad->pport.im_port);
936 kfree(bfad->pport.im_port);
937 bfad->pport.roles &= ~BFA_PORT_ROLE_FCP_IM;
938 }
939
940 bfad->bfad_flags &= ~BFAD_CFG_PPORT_DONE;
941}
942
943void
944bfad_drv_log_level_set(struct bfad_s *bfad)
945{
946 if (log_level > BFA_LOG_INVALID && log_level <= BFA_LOG_LEVEL_MAX)
947 bfa_log_set_level_all(&bfad->log_data, log_level);
948}
949
Krishna Gudipatie6714322010-03-03 17:44:02 -0800950bfa_status_t
951bfad_start_ops(struct bfad_s *bfad)
952{
953 int retval;
Jing Huangd9883542010-07-08 19:46:26 -0700954 struct bfad_pcfg_s *pcfg, *pcfg_new;
Krishna Gudipatie6714322010-03-03 17:44:02 -0800955
956 /* PPORT FCS config */
957 bfad_fcs_port_cfg(bfad);
958
959 retval = bfad_cfg_pport(bfad, BFA_PORT_ROLE_FCP_IM);
960 if (retval != BFA_STATUS_OK)
961 goto out_cfg_pport_failure;
962
963 /* BFAD level FC4 (IM/TM/IPFC) specific resource allocation */
964 retval = bfad_fc4_probe(bfad);
965 if (retval != BFA_STATUS_OK) {
966 printk(KERN_WARNING "bfad_fc4_probe failed\n");
967 goto out_fc4_probe_failure;
968 }
969
970 bfad_drv_start(bfad);
971
Jing Huangd9883542010-07-08 19:46:26 -0700972 /* pbc vport creation */
973 list_for_each_entry_safe(pcfg, pcfg_new, &bfad->pbc_pcfg_list,
974 list_entry) {
975 struct fc_vport_identifiers vid;
976 struct fc_vport *fc_vport;
977
978 memset(&vid, 0, sizeof(vid));
979 vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
980 vid.vport_type = FC_PORTTYPE_NPIV;
981 vid.disable = false;
982 vid.node_name = wwn_to_u64((u8 *)&pcfg->port_cfg.nwwn);
983 vid.port_name = wwn_to_u64((u8 *)&pcfg->port_cfg.pwwn);
984 fc_vport = fc_vport_create(bfad->pport.im_port->shost, 0, &vid);
985 if (!fc_vport)
986 printk(KERN_WARNING "bfad%d: failed to create pbc vport"
987 " %llx\n", bfad->inst_no, vid.port_name);
988 list_del(&pcfg->list_entry);
989 kfree(pcfg);
990
991 }
992
Krishna Gudipatie6714322010-03-03 17:44:02 -0800993 /*
994 * If bfa_linkup_delay is set to -1 default; try to retrive the
995 * value using the bfad_os_get_linkup_delay(); else use the
996 * passed in module param value as the bfa_linkup_delay.
997 */
998 if (bfa_linkup_delay < 0) {
999
1000 bfa_linkup_delay = bfad_os_get_linkup_delay(bfad);
1001 bfad_os_rport_online_wait(bfad);
1002 bfa_linkup_delay = -1;
1003
1004 } else {
1005 bfad_os_rport_online_wait(bfad);
1006 }
1007
1008 bfa_log(bfad->logmod, BFA_LOG_LINUX_DEVICE_CLAIMED, bfad->pci_name);
1009
1010 return BFA_STATUS_OK;
1011
1012out_fc4_probe_failure:
1013 bfad_fc4_probe_undo(bfad);
1014 bfad_uncfg_pport(bfad);
1015out_cfg_pport_failure:
1016 return BFA_STATUS_FAILED;
1017}
1018
1019int
Jing Huangd9883542010-07-08 19:46:26 -07001020bfad_worker(void *ptr)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001021{
1022 struct bfad_s *bfad;
1023 unsigned long flags;
1024
1025 bfad = (struct bfad_s *)ptr;
1026
1027 while (!kthread_should_stop()) {
1028
1029 /* Check if the FCS init is done from bfad_drv_init;
1030 * if not done do FCS init and set the flag.
1031 */
1032 if (!(bfad->bfad_flags & BFAD_FCS_INIT_DONE)) {
1033 spin_lock_irqsave(&bfad->bfad_lock, flags);
1034 bfa_fcs_init(&bfad->bfa_fcs);
1035 bfad->bfad_flags |= BFAD_FCS_INIT_DONE;
1036 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1037 }
1038
1039 /* Start the bfad operations after HAL init done */
1040 bfad_start_ops(bfad);
1041
1042 spin_lock_irqsave(&bfad->bfad_lock, flags);
1043 bfad->bfad_tsk = NULL;
1044 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1045
1046 break;
1047 }
1048
1049 return 0;
1050}
1051
Jing Huang7725ccf2009-09-23 17:46:15 -07001052 /*
1053 * PCI_entry PCI driver entries * {
1054 */
1055
1056/**
1057 * PCI probe entry.
1058 */
1059int
1060bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid)
1061{
1062 struct bfad_s *bfad;
1063 int error = -ENODEV, retval;
Jing Huang7725ccf2009-09-23 17:46:15 -07001064
1065 /*
1066 * For single port cards - only claim function 0
1067 */
1068 if ((pdev->device == BFA_PCI_DEVICE_ID_FC_8G1P)
1069 && (PCI_FUNC(pdev->devfn) != 0))
1070 return -ENODEV;
1071
1072 BFA_TRACE(BFA_INFO, "bfad_pci_probe entry");
1073
1074 bfad = kzalloc(sizeof(struct bfad_s), GFP_KERNEL);
1075 if (!bfad) {
1076 error = -ENOMEM;
1077 goto out;
1078 }
1079
1080 bfad->trcmod = kzalloc(sizeof(struct bfa_trc_mod_s), GFP_KERNEL);
1081 if (!bfad->trcmod) {
1082 printk(KERN_WARNING "Error alloc trace buffer!\n");
1083 error = -ENOMEM;
1084 goto out_alloc_trace_failure;
1085 }
1086
1087 /*
1088 * LOG/TRACE INIT
1089 */
1090 bfa_trc_init(bfad->trcmod);
1091 bfa_trc(bfad, bfad_inst);
1092
1093 bfad->logmod = &bfad->log_data;
Krishna Gudipati0a4b1fc2010-03-05 19:37:57 -08001094 bfa_log_init(bfad->logmod, (char *)pci_name(pdev), bfa_os_printf);
Jing Huang7725ccf2009-09-23 17:46:15 -07001095
1096 bfad_drv_log_level_set(bfad);
1097
1098 bfad->aen = &bfad->aen_buf;
1099
1100 if (!(bfad_load_fwimg(pdev))) {
1101 printk(KERN_WARNING "bfad_load_fwimg failure!\n");
1102 kfree(bfad->trcmod);
1103 goto out_alloc_trace_failure;
1104 }
1105
1106 retval = bfad_pci_init(pdev, bfad);
1107 if (retval) {
1108 printk(KERN_WARNING "bfad_pci_init failure!\n");
1109 error = retval;
1110 goto out_pci_init_failure;
1111 }
1112
1113 mutex_lock(&bfad_mutex);
1114 bfad->inst_no = bfad_inst++;
1115 list_add_tail(&bfad->list_entry, &bfad_list);
1116 mutex_unlock(&bfad_mutex);
1117
1118 spin_lock_init(&bfad->bfad_lock);
1119 pci_set_drvdata(pdev, bfad);
1120
1121 bfad->ref_count = 0;
1122 bfad->pport.bfad = bfad;
Jing Huangd9883542010-07-08 19:46:26 -07001123 INIT_LIST_HEAD(&bfad->pbc_pcfg_list);
Jing Huang7725ccf2009-09-23 17:46:15 -07001124
Krishna Gudipatie6714322010-03-03 17:44:02 -08001125 bfad->bfad_tsk = kthread_create(bfad_worker, (void *) bfad, "%s",
1126 "bfad_worker");
1127 if (IS_ERR(bfad->bfad_tsk)) {
1128 printk(KERN_INFO "bfad[%d]: Kernel thread"
1129 " creation failed!\n",
1130 bfad->inst_no);
1131 goto out_kthread_create_failure;
1132 }
1133
Jing Huang7725ccf2009-09-23 17:46:15 -07001134 retval = bfad_drv_init(bfad);
1135 if (retval != BFA_STATUS_OK)
1136 goto out_drv_init_failure;
1137 if (!(bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
Krishna Gudipatie6714322010-03-03 17:44:02 -08001138 bfad->bfad_flags |= BFAD_HAL_INIT_FAIL;
Jing Huang7725ccf2009-09-23 17:46:15 -07001139 printk(KERN_WARNING "bfad%d: hal init failed\n", bfad->inst_no);
1140 goto ok;
1141 }
1142
Krishna Gudipatie6714322010-03-03 17:44:02 -08001143 retval = bfad_start_ops(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001144 if (retval != BFA_STATUS_OK)
Krishna Gudipatie6714322010-03-03 17:44:02 -08001145 goto out_start_ops_failure;
Jing Huang7725ccf2009-09-23 17:46:15 -07001146
Krishna Gudipatie6714322010-03-03 17:44:02 -08001147 kthread_stop(bfad->bfad_tsk);
1148 bfad->bfad_tsk = NULL;
Jing Huang7725ccf2009-09-23 17:46:15 -07001149
Jing Huang7725ccf2009-09-23 17:46:15 -07001150ok:
1151 return 0;
1152
Krishna Gudipatie6714322010-03-03 17:44:02 -08001153out_start_ops_failure:
Jing Huang7725ccf2009-09-23 17:46:15 -07001154 bfad_drv_uninit(bfad);
1155out_drv_init_failure:
Krishna Gudipatie6714322010-03-03 17:44:02 -08001156 kthread_stop(bfad->bfad_tsk);
1157out_kthread_create_failure:
Jing Huang7725ccf2009-09-23 17:46:15 -07001158 mutex_lock(&bfad_mutex);
1159 bfad_inst--;
1160 list_del(&bfad->list_entry);
1161 mutex_unlock(&bfad_mutex);
1162 bfad_pci_uninit(pdev, bfad);
1163out_pci_init_failure:
1164 kfree(bfad->trcmod);
1165out_alloc_trace_failure:
1166 kfree(bfad);
1167out:
1168 return error;
1169}
1170
1171/**
1172 * PCI remove entry.
1173 */
1174void
1175bfad_pci_remove(struct pci_dev *pdev)
1176{
1177 struct bfad_s *bfad = pci_get_drvdata(pdev);
1178 unsigned long flags;
1179
1180 bfa_trc(bfad, bfad->inst_no);
1181
Krishna Gudipatie6714322010-03-03 17:44:02 -08001182 spin_lock_irqsave(&bfad->bfad_lock, flags);
1183 if (bfad->bfad_tsk != NULL)
1184 kthread_stop(bfad->bfad_tsk);
1185 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1186
Jing Huang7725ccf2009-09-23 17:46:15 -07001187 if ((bfad->bfad_flags & BFAD_DRV_INIT_DONE)
1188 && !(bfad->bfad_flags & BFAD_HAL_INIT_DONE)) {
1189
1190 spin_lock_irqsave(&bfad->bfad_lock, flags);
1191 init_completion(&bfad->comp);
1192 bfa_stop(&bfad->bfa);
1193 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1194 wait_for_completion(&bfad->comp);
1195
1196 bfad_remove_intr(bfad);
1197 del_timer_sync(&bfad->hal_tmo);
1198 goto hal_detach;
1199 } else if (!(bfad->bfad_flags & BFAD_DRV_INIT_DONE)) {
1200 goto remove_sysfs;
1201 }
1202
Krishna Gudipatie6714322010-03-03 17:44:02 -08001203 if (bfad->bfad_flags & BFAD_HAL_START_DONE) {
Jing Huang7725ccf2009-09-23 17:46:15 -07001204 bfad_drv_stop(bfad);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001205 } else if (bfad->bfad_flags & BFAD_DRV_INIT_DONE) {
1206 /* Invoking bfa_stop() before bfa_detach
1207 * when HAL and DRV init are success
1208 * but HAL start did not occur.
1209 */
1210 spin_lock_irqsave(&bfad->bfad_lock, flags);
1211 init_completion(&bfad->comp);
1212 bfa_stop(&bfad->bfa);
1213 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1214 wait_for_completion(&bfad->comp);
1215 }
Jing Huang7725ccf2009-09-23 17:46:15 -07001216
1217 bfad_remove_intr(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001218 del_timer_sync(&bfad->hal_tmo);
Krishna Gudipatie6714322010-03-03 17:44:02 -08001219
1220 if (bfad->bfad_flags & BFAD_FC4_PROBE_DONE)
1221 bfad_fc4_probe_undo(bfad);
Jing Huang7725ccf2009-09-23 17:46:15 -07001222
1223 if (bfad->bfad_flags & BFAD_CFG_PPORT_DONE)
1224 bfad_uncfg_pport(bfad);
1225
1226hal_detach:
1227 spin_lock_irqsave(&bfad->bfad_lock, flags);
1228 bfa_detach(&bfad->bfa);
1229 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
1230 bfad_hal_mem_release(bfad);
1231remove_sysfs:
1232
1233 mutex_lock(&bfad_mutex);
1234 bfad_inst--;
1235 list_del(&bfad->list_entry);
1236 mutex_unlock(&bfad_mutex);
1237 bfad_pci_uninit(pdev, bfad);
1238
1239 kfree(bfad->trcmod);
1240 kfree(bfad);
1241}
1242
1243
1244static struct pci_device_id bfad_id_table[] = {
1245 {
1246 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1247 .device = BFA_PCI_DEVICE_ID_FC_8G2P,
1248 .subvendor = PCI_ANY_ID,
1249 .subdevice = PCI_ANY_ID,
1250 },
1251 {
1252 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1253 .device = BFA_PCI_DEVICE_ID_FC_8G1P,
1254 .subvendor = PCI_ANY_ID,
1255 .subdevice = PCI_ANY_ID,
1256 },
1257 {
1258 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1259 .device = BFA_PCI_DEVICE_ID_CT,
1260 .subvendor = PCI_ANY_ID,
1261 .subdevice = PCI_ANY_ID,
1262 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1263 .class_mask = ~0,
1264 },
Jing Huang293f82d2010-07-08 19:45:20 -07001265 {
1266 .vendor = BFA_PCI_VENDOR_ID_BROCADE,
1267 .device = BFA_PCI_DEVICE_ID_CT_FC,
1268 .subvendor = PCI_ANY_ID,
1269 .subdevice = PCI_ANY_ID,
1270 .class = (PCI_CLASS_SERIAL_FIBER << 8),
1271 .class_mask = ~0,
1272 },
Jing Huang7725ccf2009-09-23 17:46:15 -07001273
1274 {0, 0},
1275};
1276
1277MODULE_DEVICE_TABLE(pci, bfad_id_table);
1278
1279static struct pci_driver bfad_pci_driver = {
1280 .name = BFAD_DRIVER_NAME,
1281 .id_table = bfad_id_table,
1282 .probe = bfad_pci_probe,
1283 .remove = __devexit_p(bfad_pci_remove),
1284};
1285
1286/**
1287 * Linux driver module functions
1288 */
1289bfa_status_t
1290bfad_fc4_module_init(void)
1291{
1292 int rc;
1293
1294 rc = bfad_im_module_init();
1295 if (rc != BFA_STATUS_OK)
1296 goto ext;
1297
1298 bfad_tm_module_init();
1299 if (ipfc_enable)
1300 bfad_ipfc_module_init();
1301ext:
1302 return rc;
1303}
1304
1305void
1306bfad_fc4_module_exit(void)
1307{
1308 if (ipfc_enable)
1309 bfad_ipfc_module_exit();
1310 bfad_tm_module_exit();
1311 bfad_im_module_exit();
1312}
1313
1314/**
1315 * Driver module init.
1316 */
1317static int __init
1318bfad_init(void)
1319{
1320 int error = 0;
1321
1322 printk(KERN_INFO "Brocade BFA FC/FCOE SCSI driver - version: %s\n",
1323 BFAD_DRIVER_VERSION);
1324
1325 if (num_sgpgs > 0)
1326 num_sgpgs_parm = num_sgpgs;
1327
1328 error = bfad_fc4_module_init();
1329 if (error) {
1330 error = -ENOMEM;
1331 printk(KERN_WARNING "bfad_fc4_module_init failure\n");
1332 goto ext;
1333 }
1334
1335 if (!strcmp(FCPI_NAME, " fcpim"))
1336 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_IM;
1337 if (!strcmp(FCPT_NAME, " fcptm"))
1338 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_TM;
1339 if (!strcmp(IPFC_NAME, " ipfc"))
1340 bfad_supported_fc4s |= BFA_PORT_ROLE_FCP_IPFC;
1341
1342 bfa_ioc_auto_recover(ioc_auto_recover);
1343 bfa_fcs_rport_set_del_timeout(rport_del_timeout);
1344 error = pci_register_driver(&bfad_pci_driver);
1345
1346 if (error) {
1347 printk(KERN_WARNING "bfad pci_register_driver failure\n");
1348 goto ext;
1349 }
1350
1351 return 0;
1352
1353ext:
1354 bfad_fc4_module_exit();
1355 return error;
1356}
1357
1358/**
1359 * Driver module exit.
1360 */
1361static void __exit
1362bfad_exit(void)
1363{
1364 pci_unregister_driver(&bfad_pci_driver);
1365 bfad_fc4_module_exit();
1366 bfad_free_fwimg();
1367}
1368
1369#define BFAD_PROTO_NAME FCPI_NAME FCPT_NAME IPFC_NAME
1370
1371module_init(bfad_init);
1372module_exit(bfad_exit);
1373MODULE_LICENSE("GPL");
1374MODULE_DESCRIPTION("Brocade Fibre Channel HBA Driver" BFAD_PROTO_NAME);
1375MODULE_AUTHOR("Brocade Communications Systems, Inc.");
1376MODULE_VERSION(BFAD_DRIVER_VERSION);
1377
1378