blob: 9ec8f04b16c08306955cea1df5d790f1375a720d [file] [log] [blame]
Krishna Gudipatib85daaf2011-06-13 15:55:11 -07001/*
2 * Copyright (c) 2005-2010 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#include <linux/uaccess.h>
19#include "bfad_drv.h"
20#include "bfad_im.h"
21#include "bfad_bsg.h"
22
23BFA_TRC_FILE(LDRV, BSG);
24
25/* bfad_im_bsg_get_kobject - increment the bfa refcnt */
26static void
27bfad_im_bsg_get_kobject(struct fc_bsg_job *job)
28{
29 struct Scsi_Host *shost = job->shost;
30 unsigned long flags;
31
32 spin_lock_irqsave(shost->host_lock, flags);
33 __module_get(shost->dma_dev->driver->owner);
34 spin_unlock_irqrestore(shost->host_lock, flags);
35}
36
37/* bfad_im_bsg_put_kobject - decrement the bfa refcnt */
38static void
39bfad_im_bsg_put_kobject(struct fc_bsg_job *job)
40{
41 struct Scsi_Host *shost = job->shost;
42 unsigned long flags;
43
44 spin_lock_irqsave(shost->host_lock, flags);
45 module_put(shost->dma_dev->driver->owner);
46 spin_unlock_irqrestore(shost->host_lock, flags);
47}
48
49static int
50bfad_iocmd_ioc_get_info(struct bfad_s *bfad, void *cmd)
51{
52 int i;
53 struct bfa_bsg_ioc_info_s *iocmd = (struct bfa_bsg_ioc_info_s *)cmd;
54 struct bfad_im_port_s *im_port;
55 struct bfa_port_attr_s pattr;
56 unsigned long flags;
57
58 spin_lock_irqsave(&bfad->bfad_lock, flags);
59 bfa_fcport_get_attr(&bfad->bfa, &pattr);
60 iocmd->nwwn = pattr.nwwn;
61 iocmd->pwwn = pattr.pwwn;
62 iocmd->ioc_type = bfa_get_type(&bfad->bfa);
63 iocmd->mac = bfa_get_mac(&bfad->bfa);
64 iocmd->factory_mac = bfa_get_mfg_mac(&bfad->bfa);
65 bfa_get_adapter_serial_num(&bfad->bfa, iocmd->serialnum);
66 iocmd->factorynwwn = pattr.factorynwwn;
67 iocmd->factorypwwn = pattr.factorypwwn;
68 im_port = bfad->pport.im_port;
69 iocmd->host = im_port->shost->host_no;
70 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
71
72 strcpy(iocmd->name, bfad->adapter_name);
73 strcpy(iocmd->port_name, bfad->port_name);
74 strcpy(iocmd->hwpath, bfad->pci_name);
75
76 /* set adapter hw path */
77 strcpy(iocmd->adapter_hwpath, bfad->pci_name);
78 i = strlen(iocmd->adapter_hwpath) - 1;
79 while (iocmd->adapter_hwpath[i] != '.')
80 i--;
81 iocmd->adapter_hwpath[i] = '\0';
82 iocmd->status = BFA_STATUS_OK;
83 return 0;
84}
85
86static int
87bfad_iocmd_ioc_get_attr(struct bfad_s *bfad, void *cmd)
88{
89 struct bfa_bsg_ioc_attr_s *iocmd = (struct bfa_bsg_ioc_attr_s *)cmd;
90 unsigned long flags;
91
92 spin_lock_irqsave(&bfad->bfad_lock, flags);
93 bfa_ioc_get_attr(&bfad->bfa.ioc, &iocmd->ioc_attr);
94 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
95
96 /* fill in driver attr info */
97 strcpy(iocmd->ioc_attr.driver_attr.driver, BFAD_DRIVER_NAME);
98 strncpy(iocmd->ioc_attr.driver_attr.driver_ver,
99 BFAD_DRIVER_VERSION, BFA_VERSION_LEN);
100 strcpy(iocmd->ioc_attr.driver_attr.fw_ver,
101 iocmd->ioc_attr.adapter_attr.fw_ver);
102 strcpy(iocmd->ioc_attr.driver_attr.bios_ver,
103 iocmd->ioc_attr.adapter_attr.optrom_ver);
104
105 /* copy chip rev info first otherwise it will be overwritten */
106 memcpy(bfad->pci_attr.chip_rev, iocmd->ioc_attr.pci_attr.chip_rev,
107 sizeof(bfad->pci_attr.chip_rev));
108 memcpy(&iocmd->ioc_attr.pci_attr, &bfad->pci_attr,
109 sizeof(struct bfa_ioc_pci_attr_s));
110
111 iocmd->status = BFA_STATUS_OK;
112 return 0;
113}
114
115static int
116bfad_iocmd_port_get_attr(struct bfad_s *bfad, void *cmd)
117{
118 struct bfa_bsg_port_attr_s *iocmd = (struct bfa_bsg_port_attr_s *)cmd;
119 struct bfa_lport_attr_s port_attr;
120 unsigned long flags;
121
122 spin_lock_irqsave(&bfad->bfad_lock, flags);
123 bfa_fcport_get_attr(&bfad->bfa, &iocmd->attr);
124 bfa_fcs_lport_get_attr(&bfad->bfa_fcs.fabric.bport, &port_attr);
125 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
126
127 if (iocmd->attr.topology != BFA_PORT_TOPOLOGY_NONE)
128 iocmd->attr.pid = port_attr.pid;
129 else
130 iocmd->attr.pid = 0;
131
132 iocmd->attr.port_type = port_attr.port_type;
133 iocmd->attr.loopback = port_attr.loopback;
134 iocmd->attr.authfail = port_attr.authfail;
135 strncpy(iocmd->attr.port_symname.symname,
136 port_attr.port_cfg.sym_name.symname,
137 sizeof(port_attr.port_cfg.sym_name.symname));
138
139 iocmd->status = BFA_STATUS_OK;
140 return 0;
141}
142
143static int
144bfad_iocmd_lport_get_attr(struct bfad_s *bfad, void *cmd)
145{
146 struct bfa_fcs_lport_s *fcs_port;
147 struct bfa_bsg_lport_attr_s *iocmd = (struct bfa_bsg_lport_attr_s *)cmd;
148 unsigned long flags;
149
150 spin_lock_irqsave(&bfad->bfad_lock, flags);
151 fcs_port = bfa_fcs_lookup_port(&bfad->bfa_fcs,
152 iocmd->vf_id, iocmd->pwwn);
153 if (fcs_port == NULL) {
154 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
155 iocmd->status = BFA_STATUS_UNKNOWN_LWWN;
156 goto out;
157 }
158
159 bfa_fcs_lport_get_attr(fcs_port, &iocmd->port_attr);
160 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
161 iocmd->status = BFA_STATUS_OK;
162out:
163 return 0;
164}
165
166static int
167bfad_iocmd_rport_get_addr(struct bfad_s *bfad, void *cmd)
168{
169 struct bfa_bsg_rport_scsi_addr_s *iocmd =
170 (struct bfa_bsg_rport_scsi_addr_s *)cmd;
171 struct bfa_fcs_lport_s *fcs_port;
172 struct bfa_fcs_itnim_s *fcs_itnim;
173 struct bfad_itnim_s *drv_itnim;
174 unsigned long flags;
175
176 spin_lock_irqsave(&bfad->bfad_lock, flags);
177 fcs_port = bfa_fcs_lookup_port(&bfad->bfa_fcs,
178 iocmd->vf_id, iocmd->pwwn);
179 if (fcs_port == NULL) {
180 bfa_trc(bfad, 0);
181 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
182 iocmd->status = BFA_STATUS_UNKNOWN_LWWN;
183 goto out;
184 }
185
186 fcs_itnim = bfa_fcs_itnim_lookup(fcs_port, iocmd->rpwwn);
187 if (fcs_itnim == NULL) {
188 bfa_trc(bfad, 0);
189 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
190 iocmd->status = BFA_STATUS_UNKNOWN_RWWN;
191 goto out;
192 }
193
194 drv_itnim = fcs_itnim->itnim_drv;
195
196 if (drv_itnim && drv_itnim->im_port)
197 iocmd->host = drv_itnim->im_port->shost->host_no;
198 else {
199 bfa_trc(bfad, 0);
200 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
201 iocmd->status = BFA_STATUS_UNKNOWN_RWWN;
202 goto out;
203 }
204
205 iocmd->target = drv_itnim->scsi_tgt_id;
206 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
207
208 iocmd->bus = 0;
209 iocmd->lun = 0;
210 iocmd->status = BFA_STATUS_OK;
211out:
212 return 0;
213}
214
215static int
216bfad_iocmd_fabric_get_lports(struct bfad_s *bfad, void *cmd,
217 unsigned int payload_len)
218{
219 struct bfa_bsg_fabric_get_lports_s *iocmd =
220 (struct bfa_bsg_fabric_get_lports_s *)cmd;
221 bfa_fcs_vf_t *fcs_vf;
222 uint32_t nports = iocmd->nports;
223 unsigned long flags;
224 void *iocmd_bufptr;
225
226 if (nports == 0) {
227 iocmd->status = BFA_STATUS_EINVAL;
228 goto out;
229 }
230
231 if (bfad_chk_iocmd_sz(payload_len,
232 sizeof(struct bfa_bsg_fabric_get_lports_s),
233 sizeof(wwn_t[iocmd->nports])) != BFA_STATUS_OK) {
234 iocmd->status = BFA_STATUS_VERSION_FAIL;
235 goto out;
236 }
237
238 iocmd_bufptr = (char *)iocmd +
239 sizeof(struct bfa_bsg_fabric_get_lports_s);
240
241 spin_lock_irqsave(&bfad->bfad_lock, flags);
242 fcs_vf = bfa_fcs_vf_lookup(&bfad->bfa_fcs, iocmd->vf_id);
243 if (fcs_vf == NULL) {
244 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
245 iocmd->status = BFA_STATUS_UNKNOWN_VFID;
246 goto out;
247 }
248 bfa_fcs_vf_get_ports(fcs_vf, (wwn_t *)iocmd_bufptr, &nports);
249 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
250
251 iocmd->nports = nports;
252 iocmd->status = BFA_STATUS_OK;
253out:
254 return 0;
255}
256
257static int
258bfad_iocmd_itnim_get_attr(struct bfad_s *bfad, void *cmd)
259{
260 struct bfa_bsg_itnim_attr_s *iocmd = (struct bfa_bsg_itnim_attr_s *)cmd;
261 struct bfa_fcs_lport_s *fcs_port;
262 unsigned long flags;
263
264 spin_lock_irqsave(&bfad->bfad_lock, flags);
265 fcs_port = bfa_fcs_lookup_port(&bfad->bfa_fcs,
266 iocmd->vf_id, iocmd->lpwwn);
267 if (!fcs_port)
268 iocmd->status = BFA_STATUS_UNKNOWN_LWWN;
269 else
270 iocmd->status = bfa_fcs_itnim_attr_get(fcs_port,
271 iocmd->rpwwn, &iocmd->attr);
272 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
273 return 0;
274}
275
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700276int
277bfad_iocmd_ioc_get_pcifn_cfg(struct bfad_s *bfad, void *cmd)
278{
279 struct bfa_bsg_pcifn_cfg_s *iocmd = (struct bfa_bsg_pcifn_cfg_s *)cmd;
280 struct bfad_hal_comp fcomp;
281 unsigned long flags;
282
283 init_completion(&fcomp.comp);
284 spin_lock_irqsave(&bfad->bfad_lock, flags);
285 iocmd->status = bfa_ablk_query(&bfad->bfa.modules.ablk,
286 &iocmd->pcifn_cfg,
287 bfad_hcb_comp, &fcomp);
288 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
289 if (iocmd->status != BFA_STATUS_OK)
290 goto out;
291
292 wait_for_completion(&fcomp.comp);
293 iocmd->status = fcomp.status;
294out:
295 return 0;
296}
297
298int
299bfad_iocmd_pcifn_create(struct bfad_s *bfad, void *cmd)
300{
301 struct bfa_bsg_pcifn_s *iocmd = (struct bfa_bsg_pcifn_s *)cmd;
302 struct bfad_hal_comp fcomp;
303 unsigned long flags;
304
305 init_completion(&fcomp.comp);
306 spin_lock_irqsave(&bfad->bfad_lock, flags);
307 iocmd->status = bfa_ablk_pf_create(&bfad->bfa.modules.ablk,
308 &iocmd->pcifn_id, iocmd->port,
309 iocmd->pcifn_class, iocmd->bandwidth,
310 bfad_hcb_comp, &fcomp);
311 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
312 if (iocmd->status != BFA_STATUS_OK)
313 goto out;
314
315 wait_for_completion(&fcomp.comp);
316 iocmd->status = fcomp.status;
317out:
318 return 0;
319}
320
321int
322bfad_iocmd_pcifn_delete(struct bfad_s *bfad, void *cmd)
323{
324 struct bfa_bsg_pcifn_s *iocmd = (struct bfa_bsg_pcifn_s *)cmd;
325 struct bfad_hal_comp fcomp;
326 unsigned long flags;
327
328 init_completion(&fcomp.comp);
329 spin_lock_irqsave(&bfad->bfad_lock, flags);
330 iocmd->status = bfa_ablk_pf_delete(&bfad->bfa.modules.ablk,
331 iocmd->pcifn_id,
332 bfad_hcb_comp, &fcomp);
333 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
334 if (iocmd->status != BFA_STATUS_OK)
335 goto out;
336
337 wait_for_completion(&fcomp.comp);
338 iocmd->status = fcomp.status;
339out:
340 return 0;
341}
342
343int
344bfad_iocmd_pcifn_bw(struct bfad_s *bfad, void *cmd)
345{
346 struct bfa_bsg_pcifn_s *iocmd = (struct bfa_bsg_pcifn_s *)cmd;
347 struct bfad_hal_comp fcomp;
348 unsigned long flags;
349
350 init_completion(&fcomp.comp);
351 spin_lock_irqsave(&bfad->bfad_lock, flags);
352 iocmd->status = bfa_ablk_pf_update(&bfad->bfa.modules.ablk,
353 iocmd->pcifn_id, iocmd->bandwidth,
354 bfad_hcb_comp, &fcomp);
355 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
356 bfa_trc(bfad, iocmd->status);
357 if (iocmd->status != BFA_STATUS_OK)
358 goto out;
359
360 wait_for_completion(&fcomp.comp);
361 iocmd->status = fcomp.status;
362 bfa_trc(bfad, iocmd->status);
363out:
364 return 0;
365}
366
367int
368bfad_iocmd_adapter_cfg_mode(struct bfad_s *bfad, void *cmd)
369{
370 struct bfa_bsg_adapter_cfg_mode_s *iocmd =
371 (struct bfa_bsg_adapter_cfg_mode_s *)cmd;
372 struct bfad_hal_comp fcomp;
373 unsigned long flags = 0;
374
375 init_completion(&fcomp.comp);
376 spin_lock_irqsave(&bfad->bfad_lock, flags);
377 iocmd->status = bfa_ablk_adapter_config(&bfad->bfa.modules.ablk,
378 iocmd->cfg.mode, iocmd->cfg.max_pf,
379 iocmd->cfg.max_vf, bfad_hcb_comp, &fcomp);
380 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
381 if (iocmd->status != BFA_STATUS_OK)
382 goto out;
383
384 wait_for_completion(&fcomp.comp);
385 iocmd->status = fcomp.status;
386out:
387 return 0;
388}
389
390int
391bfad_iocmd_port_cfg_mode(struct bfad_s *bfad, void *cmd)
392{
393 struct bfa_bsg_port_cfg_mode_s *iocmd =
394 (struct bfa_bsg_port_cfg_mode_s *)cmd;
395 struct bfad_hal_comp fcomp;
396 unsigned long flags = 0;
397
398 init_completion(&fcomp.comp);
399 spin_lock_irqsave(&bfad->bfad_lock, flags);
400 iocmd->status = bfa_ablk_port_config(&bfad->bfa.modules.ablk,
401 iocmd->instance, iocmd->cfg.mode,
402 iocmd->cfg.max_pf, iocmd->cfg.max_vf,
403 bfad_hcb_comp, &fcomp);
404 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
405 if (iocmd->status != BFA_STATUS_OK)
406 goto out;
407
408 wait_for_completion(&fcomp.comp);
409 iocmd->status = fcomp.status;
410out:
411 return 0;
412}
413
414int
415bfad_iocmd_ablk_optrom(struct bfad_s *bfad, unsigned int cmd, void *pcmd)
416{
417 struct bfa_bsg_gen_s *iocmd = (struct bfa_bsg_gen_s *)pcmd;
418 struct bfad_hal_comp fcomp;
419 unsigned long flags;
420
421 init_completion(&fcomp.comp);
422 spin_lock_irqsave(&bfad->bfad_lock, flags);
423 if (cmd == IOCMD_FLASH_ENABLE_OPTROM)
424 iocmd->status = bfa_ablk_optrom_en(&bfad->bfa.modules.ablk,
425 bfad_hcb_comp, &fcomp);
426 else
427 iocmd->status = bfa_ablk_optrom_dis(&bfad->bfa.modules.ablk,
428 bfad_hcb_comp, &fcomp);
429 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
430
431 if (iocmd->status != BFA_STATUS_OK)
432 goto out;
433
434 wait_for_completion(&fcomp.comp);
435 iocmd->status = fcomp.status;
436out:
437 return 0;
438}
439
Krishna Gudipatia7141342011-06-24 20:23:19 -0700440int
441bfad_iocmd_faa_enable(struct bfad_s *bfad, void *cmd)
442{
443 struct bfa_bsg_gen_s *iocmd = (struct bfa_bsg_gen_s *)cmd;
444 unsigned long flags;
445 struct bfad_hal_comp fcomp;
446
447 init_completion(&fcomp.comp);
448 iocmd->status = BFA_STATUS_OK;
449 spin_lock_irqsave(&bfad->bfad_lock, flags);
450 iocmd->status = bfa_faa_enable(&bfad->bfa, bfad_hcb_comp, &fcomp);
451 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
452
453 if (iocmd->status != BFA_STATUS_OK)
454 goto out;
455
456 wait_for_completion(&fcomp.comp);
457 iocmd->status = fcomp.status;
458out:
459 return 0;
460}
461
462int
463bfad_iocmd_faa_disable(struct bfad_s *bfad, void *cmd)
464{
465 struct bfa_bsg_gen_s *iocmd = (struct bfa_bsg_gen_s *)cmd;
466 unsigned long flags;
467 struct bfad_hal_comp fcomp;
468
469 init_completion(&fcomp.comp);
470 iocmd->status = BFA_STATUS_OK;
471 spin_lock_irqsave(&bfad->bfad_lock, flags);
472 iocmd->status = bfa_faa_disable(&bfad->bfa, bfad_hcb_comp, &fcomp);
473 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
474
475 if (iocmd->status != BFA_STATUS_OK)
476 goto out;
477
478 wait_for_completion(&fcomp.comp);
479 iocmd->status = fcomp.status;
480out:
481 return 0;
482}
483
484int
485bfad_iocmd_faa_query(struct bfad_s *bfad, void *cmd)
486{
487 struct bfa_bsg_faa_attr_s *iocmd = (struct bfa_bsg_faa_attr_s *)cmd;
488 struct bfad_hal_comp fcomp;
489 unsigned long flags;
490
491 init_completion(&fcomp.comp);
492 iocmd->status = BFA_STATUS_OK;
493 spin_lock_irqsave(&bfad->bfad_lock, flags);
494 iocmd->status = bfa_faa_query(&bfad->bfa, &iocmd->faa_attr,
495 bfad_hcb_comp, &fcomp);
496 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
497
498 if (iocmd->status != BFA_STATUS_OK)
499 goto out;
500
501 wait_for_completion(&fcomp.comp);
502 iocmd->status = fcomp.status;
503out:
504 return 0;
505}
506
Krishna Gudipatib85daaf2011-06-13 15:55:11 -0700507static int
508bfad_iocmd_handler(struct bfad_s *bfad, unsigned int cmd, void *iocmd,
509 unsigned int payload_len)
510{
511 int rc = EINVAL;
512
513 switch (cmd) {
514 case IOCMD_IOC_GET_INFO:
515 rc = bfad_iocmd_ioc_get_info(bfad, iocmd);
516 break;
517 case IOCMD_IOC_GET_ATTR:
518 rc = bfad_iocmd_ioc_get_attr(bfad, iocmd);
519 break;
520 case IOCMD_PORT_GET_ATTR:
521 rc = bfad_iocmd_port_get_attr(bfad, iocmd);
522 break;
523 case IOCMD_LPORT_GET_ATTR:
524 rc = bfad_iocmd_lport_get_attr(bfad, iocmd);
525 break;
526 case IOCMD_RPORT_GET_ADDR:
527 rc = bfad_iocmd_rport_get_addr(bfad, iocmd);
528 break;
529 case IOCMD_FABRIC_GET_LPORTS:
530 rc = bfad_iocmd_fabric_get_lports(bfad, iocmd, payload_len);
531 break;
532 case IOCMD_ITNIM_GET_ATTR:
533 rc = bfad_iocmd_itnim_get_attr(bfad, iocmd);
534 break;
Krishna Gudipati1a4d8e12011-06-24 20:22:28 -0700535 case IOCMD_IOC_PCIFN_CFG:
536 rc = bfad_iocmd_ioc_get_pcifn_cfg(bfad, iocmd);
537 break;
538 case IOCMD_PCIFN_CREATE:
539 rc = bfad_iocmd_pcifn_create(bfad, iocmd);
540 break;
541 case IOCMD_PCIFN_DELETE:
542 rc = bfad_iocmd_pcifn_delete(bfad, iocmd);
543 break;
544 case IOCMD_PCIFN_BW:
545 rc = bfad_iocmd_pcifn_bw(bfad, iocmd);
546 break;
547 case IOCMD_ADAPTER_CFG_MODE:
548 rc = bfad_iocmd_adapter_cfg_mode(bfad, iocmd);
549 break;
550 case IOCMD_PORT_CFG_MODE:
551 rc = bfad_iocmd_port_cfg_mode(bfad, iocmd);
552 break;
553 case IOCMD_FLASH_ENABLE_OPTROM:
554 case IOCMD_FLASH_DISABLE_OPTROM:
555 rc = bfad_iocmd_ablk_optrom(bfad, cmd, iocmd);
556 break;
Krishna Gudipatia7141342011-06-24 20:23:19 -0700557 case IOCMD_FAA_ENABLE:
558 rc = bfad_iocmd_faa_enable(bfad, iocmd);
559 break;
560 case IOCMD_FAA_DISABLE:
561 rc = bfad_iocmd_faa_disable(bfad, iocmd);
562 break;
563 case IOCMD_FAA_QUERY:
564 rc = bfad_iocmd_faa_query(bfad, iocmd);
565 break;
Krishna Gudipatib85daaf2011-06-13 15:55:11 -0700566 default:
567 rc = EINVAL;
568 break;
569 }
570 return -rc;
571}
572
573static int
574bfad_im_bsg_vendor_request(struct fc_bsg_job *job)
575{
576 uint32_t vendor_cmd = job->request->rqst_data.h_vendor.vendor_cmd[0];
577 struct bfad_im_port_s *im_port =
578 (struct bfad_im_port_s *) job->shost->hostdata[0];
579 struct bfad_s *bfad = im_port->bfad;
580 void *payload_kbuf;
581 int rc = -EINVAL;
582
583 /* Allocate a temp buffer to hold the passed in user space command */
584 payload_kbuf = kzalloc(job->request_payload.payload_len, GFP_KERNEL);
585 if (!payload_kbuf) {
586 rc = -ENOMEM;
587 goto out;
588 }
589
590 /* Copy the sg_list passed in to a linear buffer: holds the cmnd data */
591 sg_copy_to_buffer(job->request_payload.sg_list,
592 job->request_payload.sg_cnt, payload_kbuf,
593 job->request_payload.payload_len);
594
595 /* Invoke IOCMD handler - to handle all the vendor command requests */
596 rc = bfad_iocmd_handler(bfad, vendor_cmd, payload_kbuf,
597 job->request_payload.payload_len);
598 if (rc != BFA_STATUS_OK)
599 goto error;
600
601 /* Copy the response data to the job->reply_payload sg_list */
602 sg_copy_from_buffer(job->reply_payload.sg_list,
603 job->reply_payload.sg_cnt,
604 payload_kbuf,
605 job->reply_payload.payload_len);
606
607 /* free the command buffer */
608 kfree(payload_kbuf);
609
610 /* Fill the BSG job reply data */
611 job->reply_len = job->reply_payload.payload_len;
612 job->reply->reply_payload_rcv_len = job->reply_payload.payload_len;
613 job->reply->result = rc;
614
615 job->job_done(job);
616 return rc;
617error:
618 /* free the command buffer */
619 kfree(payload_kbuf);
620out:
621 job->reply->result = rc;
622 job->reply_len = sizeof(uint32_t);
623 job->reply->reply_payload_rcv_len = 0;
624 return rc;
625}
626
627/* FC passthru call backs */
628u64
629bfad_fcxp_get_req_sgaddr_cb(void *bfad_fcxp, int sgeid)
630{
631 struct bfad_fcxp *drv_fcxp = bfad_fcxp;
632 struct bfa_sge_s *sge;
633 u64 addr;
634
635 sge = drv_fcxp->req_sge + sgeid;
636 addr = (u64)(size_t) sge->sg_addr;
637 return addr;
638}
639
640u32
641bfad_fcxp_get_req_sglen_cb(void *bfad_fcxp, int sgeid)
642{
643 struct bfad_fcxp *drv_fcxp = bfad_fcxp;
644 struct bfa_sge_s *sge;
645
646 sge = drv_fcxp->req_sge + sgeid;
647 return sge->sg_len;
648}
649
650u64
651bfad_fcxp_get_rsp_sgaddr_cb(void *bfad_fcxp, int sgeid)
652{
653 struct bfad_fcxp *drv_fcxp = bfad_fcxp;
654 struct bfa_sge_s *sge;
655 u64 addr;
656
657 sge = drv_fcxp->rsp_sge + sgeid;
658 addr = (u64)(size_t) sge->sg_addr;
659 return addr;
660}
661
662u32
663bfad_fcxp_get_rsp_sglen_cb(void *bfad_fcxp, int sgeid)
664{
665 struct bfad_fcxp *drv_fcxp = bfad_fcxp;
666 struct bfa_sge_s *sge;
667
668 sge = drv_fcxp->rsp_sge + sgeid;
669 return sge->sg_len;
670}
671
672void
673bfad_send_fcpt_cb(void *bfad_fcxp, struct bfa_fcxp_s *fcxp, void *cbarg,
674 bfa_status_t req_status, u32 rsp_len, u32 resid_len,
675 struct fchs_s *rsp_fchs)
676{
677 struct bfad_fcxp *drv_fcxp = bfad_fcxp;
678
679 drv_fcxp->req_status = req_status;
680 drv_fcxp->rsp_len = rsp_len;
681
682 /* bfa_fcxp will be automatically freed by BFA */
683 drv_fcxp->bfa_fcxp = NULL;
684 complete(&drv_fcxp->comp);
685}
686
687struct bfad_buf_info *
688bfad_fcxp_map_sg(struct bfad_s *bfad, void *payload_kbuf,
689 uint32_t payload_len, uint32_t *num_sgles)
690{
691 struct bfad_buf_info *buf_base, *buf_info;
692 struct bfa_sge_s *sg_table;
693 int sge_num = 1;
694
695 buf_base = kzalloc((sizeof(struct bfad_buf_info) +
696 sizeof(struct bfa_sge_s)) * sge_num, GFP_KERNEL);
697 if (!buf_base)
698 return NULL;
699
700 sg_table = (struct bfa_sge_s *) (((uint8_t *)buf_base) +
701 (sizeof(struct bfad_buf_info) * sge_num));
702
703 /* Allocate dma coherent memory */
704 buf_info = buf_base;
705 buf_info->size = payload_len;
706 buf_info->virt = dma_alloc_coherent(&bfad->pcidev->dev, buf_info->size,
707 &buf_info->phys, GFP_KERNEL);
708 if (!buf_info->virt)
709 goto out_free_mem;
710
711 /* copy the linear bsg buffer to buf_info */
712 memset(buf_info->virt, 0, buf_info->size);
713 memcpy(buf_info->virt, payload_kbuf, buf_info->size);
714
715 /*
716 * Setup SG table
717 */
718 sg_table->sg_len = buf_info->size;
719 sg_table->sg_addr = (void *)(size_t) buf_info->phys;
720
721 *num_sgles = sge_num;
722
723 return buf_base;
724
725out_free_mem:
726 kfree(buf_base);
727 return NULL;
728}
729
730void
731bfad_fcxp_free_mem(struct bfad_s *bfad, struct bfad_buf_info *buf_base,
732 uint32_t num_sgles)
733{
734 int i;
735 struct bfad_buf_info *buf_info = buf_base;
736
737 if (buf_base) {
738 for (i = 0; i < num_sgles; buf_info++, i++) {
739 if (buf_info->virt != NULL)
740 dma_free_coherent(&bfad->pcidev->dev,
741 buf_info->size, buf_info->virt,
742 buf_info->phys);
743 }
744 kfree(buf_base);
745 }
746}
747
748int
749bfad_fcxp_bsg_send(struct fc_bsg_job *job, struct bfad_fcxp *drv_fcxp,
750 bfa_bsg_fcpt_t *bsg_fcpt)
751{
752 struct bfa_fcxp_s *hal_fcxp;
753 struct bfad_s *bfad = drv_fcxp->port->bfad;
754 unsigned long flags;
755 uint8_t lp_tag;
756
757 spin_lock_irqsave(&bfad->bfad_lock, flags);
758
759 /* Allocate bfa_fcxp structure */
760 hal_fcxp = bfa_fcxp_alloc(drv_fcxp, &bfad->bfa,
761 drv_fcxp->num_req_sgles,
762 drv_fcxp->num_rsp_sgles,
763 bfad_fcxp_get_req_sgaddr_cb,
764 bfad_fcxp_get_req_sglen_cb,
765 bfad_fcxp_get_rsp_sgaddr_cb,
766 bfad_fcxp_get_rsp_sglen_cb);
767 if (!hal_fcxp) {
768 bfa_trc(bfad, 0);
769 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
770 return BFA_STATUS_ENOMEM;
771 }
772
773 drv_fcxp->bfa_fcxp = hal_fcxp;
774
775 lp_tag = bfa_lps_get_tag_from_pid(&bfad->bfa, bsg_fcpt->fchs.s_id);
776
777 bfa_fcxp_send(hal_fcxp, drv_fcxp->bfa_rport, bsg_fcpt->vf_id, lp_tag,
778 bsg_fcpt->cts, bsg_fcpt->cos,
779 job->request_payload.payload_len,
780 &bsg_fcpt->fchs, bfad_send_fcpt_cb, bfad,
781 job->reply_payload.payload_len, bsg_fcpt->tsecs);
782
783 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
784
785 return BFA_STATUS_OK;
786}
787
788int
789bfad_im_bsg_els_ct_request(struct fc_bsg_job *job)
790{
791 struct bfa_bsg_data *bsg_data;
792 struct bfad_im_port_s *im_port =
793 (struct bfad_im_port_s *) job->shost->hostdata[0];
794 struct bfad_s *bfad = im_port->bfad;
795 bfa_bsg_fcpt_t *bsg_fcpt;
796 struct bfad_fcxp *drv_fcxp;
797 struct bfa_fcs_lport_s *fcs_port;
798 struct bfa_fcs_rport_s *fcs_rport;
799 uint32_t command_type = job->request->msgcode;
800 unsigned long flags;
801 struct bfad_buf_info *rsp_buf_info;
802 void *req_kbuf = NULL, *rsp_kbuf = NULL;
803 int rc = -EINVAL;
804
805 job->reply_len = sizeof(uint32_t); /* Atleast uint32_t reply_len */
806 job->reply->reply_payload_rcv_len = 0;
807
808 /* Get the payload passed in from userspace */
809 bsg_data = (struct bfa_bsg_data *) (((char *)job->request) +
810 sizeof(struct fc_bsg_request));
811 if (bsg_data == NULL)
812 goto out;
813
814 /*
815 * Allocate buffer for bsg_fcpt and do a copy_from_user op for payload
816 * buffer of size bsg_data->payload_len
817 */
818 bsg_fcpt = (struct bfa_bsg_fcpt_s *)
819 kzalloc(bsg_data->payload_len, GFP_KERNEL);
820 if (!bsg_fcpt)
821 goto out;
822
823 if (copy_from_user((uint8_t *)bsg_fcpt, bsg_data->payload,
824 bsg_data->payload_len)) {
825 kfree(bsg_fcpt);
826 goto out;
827 }
828
829 drv_fcxp = kzalloc(sizeof(struct bfad_fcxp), GFP_KERNEL);
830 if (drv_fcxp == NULL) {
831 rc = -ENOMEM;
832 goto out;
833 }
834
835 spin_lock_irqsave(&bfad->bfad_lock, flags);
836 fcs_port = bfa_fcs_lookup_port(&bfad->bfa_fcs, bsg_fcpt->vf_id,
837 bsg_fcpt->lpwwn);
838 if (fcs_port == NULL) {
839 bsg_fcpt->status = BFA_STATUS_UNKNOWN_LWWN;
840 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
841 goto out_free_mem;
842 }
843
844 /* Check if the port is online before sending FC Passthru cmd */
845 if (!bfa_fcs_lport_is_online(fcs_port)) {
846 bsg_fcpt->status = BFA_STATUS_PORT_OFFLINE;
847 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
848 goto out_free_mem;
849 }
850
851 drv_fcxp->port = fcs_port->bfad_port;
852
853 if (drv_fcxp->port->bfad == 0)
854 drv_fcxp->port->bfad = bfad;
855
856 /* Fetch the bfa_rport - if nexus needed */
857 if (command_type == FC_BSG_HST_ELS_NOLOGIN ||
858 command_type == FC_BSG_HST_CT) {
859 /* BSG HST commands: no nexus needed */
860 drv_fcxp->bfa_rport = NULL;
861
862 } else if (command_type == FC_BSG_RPT_ELS ||
863 command_type == FC_BSG_RPT_CT) {
864 /* BSG RPT commands: nexus needed */
865 fcs_rport = bfa_fcs_lport_get_rport_by_pwwn(fcs_port,
866 bsg_fcpt->dpwwn);
867 if (fcs_rport == NULL) {
868 bsg_fcpt->status = BFA_STATUS_UNKNOWN_RWWN;
869 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
870 goto out_free_mem;
871 }
872
873 drv_fcxp->bfa_rport = fcs_rport->bfa_rport;
874
875 } else { /* Unknown BSG msgcode; return -EINVAL */
876 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
877 goto out_free_mem;
878 }
879
880 spin_unlock_irqrestore(&bfad->bfad_lock, flags);
881
882 /* allocate memory for req / rsp buffers */
883 req_kbuf = kzalloc(job->request_payload.payload_len, GFP_KERNEL);
884 if (!req_kbuf) {
885 printk(KERN_INFO "bfa %s: fcpt request buffer alloc failed\n",
886 bfad->pci_name);
887 rc = -ENOMEM;
888 goto out_free_mem;
889 }
890
891 rsp_kbuf = kzalloc(job->reply_payload.payload_len, GFP_KERNEL);
892 if (!rsp_kbuf) {
893 printk(KERN_INFO "bfa %s: fcpt response buffer alloc failed\n",
894 bfad->pci_name);
895 rc = -ENOMEM;
896 goto out_free_mem;
897 }
898
899 /* map req sg - copy the sg_list passed in to the linear buffer */
900 sg_copy_to_buffer(job->request_payload.sg_list,
901 job->request_payload.sg_cnt, req_kbuf,
902 job->request_payload.payload_len);
903
904 drv_fcxp->reqbuf_info = bfad_fcxp_map_sg(bfad, req_kbuf,
905 job->request_payload.payload_len,
906 &drv_fcxp->num_req_sgles);
907 if (!drv_fcxp->reqbuf_info) {
908 printk(KERN_INFO "bfa %s: fcpt request fcxp_map_sg failed\n",
909 bfad->pci_name);
910 rc = -ENOMEM;
911 goto out_free_mem;
912 }
913
914 drv_fcxp->req_sge = (struct bfa_sge_s *)
915 (((uint8_t *)drv_fcxp->reqbuf_info) +
916 (sizeof(struct bfad_buf_info) *
917 drv_fcxp->num_req_sgles));
918
919 /* map rsp sg */
920 drv_fcxp->rspbuf_info = bfad_fcxp_map_sg(bfad, rsp_kbuf,
921 job->reply_payload.payload_len,
922 &drv_fcxp->num_rsp_sgles);
923 if (!drv_fcxp->rspbuf_info) {
924 printk(KERN_INFO "bfa %s: fcpt response fcxp_map_sg failed\n",
925 bfad->pci_name);
926 rc = -ENOMEM;
927 goto out_free_mem;
928 }
929
930 rsp_buf_info = (struct bfad_buf_info *)drv_fcxp->rspbuf_info;
931 drv_fcxp->rsp_sge = (struct bfa_sge_s *)
932 (((uint8_t *)drv_fcxp->rspbuf_info) +
933 (sizeof(struct bfad_buf_info) *
934 drv_fcxp->num_rsp_sgles));
935
936 /* fcxp send */
937 init_completion(&drv_fcxp->comp);
938 rc = bfad_fcxp_bsg_send(job, drv_fcxp, bsg_fcpt);
939 if (rc == BFA_STATUS_OK) {
940 wait_for_completion(&drv_fcxp->comp);
941 bsg_fcpt->status = drv_fcxp->req_status;
942 } else {
943 bsg_fcpt->status = rc;
944 goto out_free_mem;
945 }
946
947 /* fill the job->reply data */
948 if (drv_fcxp->req_status == BFA_STATUS_OK) {
949 job->reply_len = drv_fcxp->rsp_len;
950 job->reply->reply_payload_rcv_len = drv_fcxp->rsp_len;
951 job->reply->reply_data.ctels_reply.status = FC_CTELS_STATUS_OK;
952 } else {
953 job->reply->reply_payload_rcv_len =
954 sizeof(struct fc_bsg_ctels_reply);
955 job->reply_len = sizeof(uint32_t);
956 job->reply->reply_data.ctels_reply.status =
957 FC_CTELS_STATUS_REJECT;
958 }
959
960 /* Copy the response data to the reply_payload sg list */
961 sg_copy_from_buffer(job->reply_payload.sg_list,
962 job->reply_payload.sg_cnt,
963 (uint8_t *)rsp_buf_info->virt,
964 job->reply_payload.payload_len);
965
966out_free_mem:
967 bfad_fcxp_free_mem(bfad, drv_fcxp->rspbuf_info,
968 drv_fcxp->num_rsp_sgles);
969 bfad_fcxp_free_mem(bfad, drv_fcxp->reqbuf_info,
970 drv_fcxp->num_req_sgles);
971 kfree(req_kbuf);
972 kfree(rsp_kbuf);
973
974 /* Need a copy to user op */
975 if (copy_to_user(bsg_data->payload, (void *) bsg_fcpt,
976 bsg_data->payload_len))
977 rc = -EIO;
978
979 kfree(bsg_fcpt);
980 kfree(drv_fcxp);
981out:
982 job->reply->result = rc;
983
984 if (rc == BFA_STATUS_OK)
985 job->job_done(job);
986
987 return rc;
988}
989
990int
991bfad_im_bsg_request(struct fc_bsg_job *job)
992{
993 uint32_t rc = BFA_STATUS_OK;
994
995 /* Increment the bfa module refcnt - if bsg request is in service */
996 bfad_im_bsg_get_kobject(job);
997
998 switch (job->request->msgcode) {
999 case FC_BSG_HST_VENDOR:
1000 /* Process BSG HST Vendor requests */
1001 rc = bfad_im_bsg_vendor_request(job);
1002 break;
1003 case FC_BSG_HST_ELS_NOLOGIN:
1004 case FC_BSG_RPT_ELS:
1005 case FC_BSG_HST_CT:
1006 case FC_BSG_RPT_CT:
1007 /* Process BSG ELS/CT commands */
1008 rc = bfad_im_bsg_els_ct_request(job);
1009 break;
1010 default:
1011 job->reply->result = rc = -EINVAL;
1012 job->reply->reply_payload_rcv_len = 0;
1013 break;
1014 }
1015
1016 /* Decrement the bfa module refcnt - on completion of bsg request */
1017 bfad_im_bsg_put_kobject(job);
1018
1019 return rc;
1020}
1021
1022int
1023bfad_im_bsg_timeout(struct fc_bsg_job *job)
1024{
1025 /* Don't complete the BSG job request - return -EAGAIN
1026 * to reset bsg job timeout : for ELS/CT pass thru we
1027 * already have timer to track the request.
1028 */
1029 return -EAGAIN;
1030}