GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / scsi / qla2xxx / qla_bsg.c
1 /*
2  * QLogic Fibre Channel HBA Driver
3  * Copyright (c)  2003-2014 QLogic Corporation
4  *
5  * See LICENSE.qla2xxx for copyright and licensing details.
6  */
7 #include "qla_def.h"
8
9 #include <linux/kthread.h>
10 #include <linux/vmalloc.h>
11 #include <linux/delay.h>
12
13 /* BSG support for ELS/CT pass through */
14 void
15 qla2x00_bsg_job_done(void *data, void *ptr, int res)
16 {
17         srb_t *sp = (srb_t *)ptr;
18         struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
19         struct fc_bsg_job *bsg_job = sp->u.bsg_job;
20
21         bsg_job->reply->result = res;
22         bsg_job->job_done(bsg_job);
23         sp->free(vha, sp);
24 }
25
26 void
27 qla2x00_bsg_sp_free(void *data, void *ptr)
28 {
29         srb_t *sp = (srb_t *)ptr;
30         struct scsi_qla_host *vha = sp->fcport->vha;
31         struct fc_bsg_job *bsg_job = sp->u.bsg_job;
32         struct qla_hw_data *ha = vha->hw;
33         struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
34
35         if (sp->type == SRB_FXIOCB_BCMD) {
36                 piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
37                     &bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
38
39                 if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
40                         dma_unmap_sg(&ha->pdev->dev,
41                             bsg_job->request_payload.sg_list,
42                             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
43
44                 if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
45                         dma_unmap_sg(&ha->pdev->dev,
46                             bsg_job->reply_payload.sg_list,
47                             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
48         } else {
49                 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
50                     bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
51
52                 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
53                     bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
54         }
55
56         if (sp->type == SRB_CT_CMD ||
57             sp->type == SRB_FXIOCB_BCMD ||
58             sp->type == SRB_ELS_CMD_HST)
59                 kfree(sp->fcport);
60         qla2x00_rel_sp(vha, sp);
61 }
62
63 int
64 qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
65         struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
66 {
67         int i, ret, num_valid;
68         uint8_t *bcode;
69         struct qla_fcp_prio_entry *pri_entry;
70         uint32_t *bcode_val_ptr, bcode_val;
71
72         ret = 1;
73         num_valid = 0;
74         bcode = (uint8_t *)pri_cfg;
75         bcode_val_ptr = (uint32_t *)pri_cfg;
76         bcode_val = (uint32_t)(*bcode_val_ptr);
77
78         if (bcode_val == 0xFFFFFFFF) {
79                 /* No FCP Priority config data in flash */
80                 ql_dbg(ql_dbg_user, vha, 0x7051,
81                     "No FCP Priority config data.\n");
82                 return 0;
83         }
84
85         if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
86                         bcode[3] != 'S') {
87                 /* Invalid FCP priority data header*/
88                 ql_dbg(ql_dbg_user, vha, 0x7052,
89                     "Invalid FCP Priority data header. bcode=0x%x.\n",
90                     bcode_val);
91                 return 0;
92         }
93         if (flag != 1)
94                 return ret;
95
96         pri_entry = &pri_cfg->entry[0];
97         for (i = 0; i < pri_cfg->num_entries; i++) {
98                 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
99                         num_valid++;
100                 pri_entry++;
101         }
102
103         if (num_valid == 0) {
104                 /* No valid FCP priority data entries */
105                 ql_dbg(ql_dbg_user, vha, 0x7053,
106                     "No valid FCP Priority data entries.\n");
107                 ret = 0;
108         } else {
109                 /* FCP priority data is valid */
110                 ql_dbg(ql_dbg_user, vha, 0x7054,
111                     "Valid FCP priority data. num entries = %d.\n",
112                     num_valid);
113         }
114
115         return ret;
116 }
117
118 static int
119 qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
120 {
121         struct Scsi_Host *host = bsg_job->shost;
122         scsi_qla_host_t *vha = shost_priv(host);
123         struct qla_hw_data *ha = vha->hw;
124         int ret = 0;
125         uint32_t len;
126         uint32_t oper;
127
128         if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_P3P_TYPE(ha))) {
129                 ret = -EINVAL;
130                 goto exit_fcp_prio_cfg;
131         }
132
133         /* Get the sub command */
134         oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
135
136         /* Only set config is allowed if config memory is not allocated */
137         if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
138                 ret = -EINVAL;
139                 goto exit_fcp_prio_cfg;
140         }
141         switch (oper) {
142         case QLFC_FCP_PRIO_DISABLE:
143                 if (ha->flags.fcp_prio_enabled) {
144                         ha->flags.fcp_prio_enabled = 0;
145                         ha->fcp_prio_cfg->attributes &=
146                                 ~FCP_PRIO_ATTR_ENABLE;
147                         qla24xx_update_all_fcp_prio(vha);
148                         bsg_job->reply->result = DID_OK;
149                 } else {
150                         ret = -EINVAL;
151                         bsg_job->reply->result = (DID_ERROR << 16);
152                         goto exit_fcp_prio_cfg;
153                 }
154                 break;
155
156         case QLFC_FCP_PRIO_ENABLE:
157                 if (!ha->flags.fcp_prio_enabled) {
158                         if (ha->fcp_prio_cfg) {
159                                 ha->flags.fcp_prio_enabled = 1;
160                                 ha->fcp_prio_cfg->attributes |=
161                                     FCP_PRIO_ATTR_ENABLE;
162                                 qla24xx_update_all_fcp_prio(vha);
163                                 bsg_job->reply->result = DID_OK;
164                         } else {
165                                 ret = -EINVAL;
166                                 bsg_job->reply->result = (DID_ERROR << 16);
167                                 goto exit_fcp_prio_cfg;
168                         }
169                 }
170                 break;
171
172         case QLFC_FCP_PRIO_GET_CONFIG:
173                 len = bsg_job->reply_payload.payload_len;
174                 if (!len || len > FCP_PRIO_CFG_SIZE) {
175                         ret = -EINVAL;
176                         bsg_job->reply->result = (DID_ERROR << 16);
177                         goto exit_fcp_prio_cfg;
178                 }
179
180                 bsg_job->reply->result = DID_OK;
181                 bsg_job->reply->reply_payload_rcv_len =
182                         sg_copy_from_buffer(
183                         bsg_job->reply_payload.sg_list,
184                         bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
185                         len);
186
187                 break;
188
189         case QLFC_FCP_PRIO_SET_CONFIG:
190                 len = bsg_job->request_payload.payload_len;
191                 if (!len || len > FCP_PRIO_CFG_SIZE) {
192                         bsg_job->reply->result = (DID_ERROR << 16);
193                         ret = -EINVAL;
194                         goto exit_fcp_prio_cfg;
195                 }
196
197                 if (!ha->fcp_prio_cfg) {
198                         ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
199                         if (!ha->fcp_prio_cfg) {
200                                 ql_log(ql_log_warn, vha, 0x7050,
201                                     "Unable to allocate memory for fcp prio "
202                                     "config data (%x).\n", FCP_PRIO_CFG_SIZE);
203                                 bsg_job->reply->result = (DID_ERROR << 16);
204                                 ret = -ENOMEM;
205                                 goto exit_fcp_prio_cfg;
206                         }
207                 }
208
209                 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
210                 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
211                 bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
212                         FCP_PRIO_CFG_SIZE);
213
214                 /* validate fcp priority data */
215
216                 if (!qla24xx_fcp_prio_cfg_valid(vha,
217                     (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
218                         bsg_job->reply->result = (DID_ERROR << 16);
219                         ret = -EINVAL;
220                         /* If buffer was invalidatic int
221                          * fcp_prio_cfg is of no use
222                          */
223                         vfree(ha->fcp_prio_cfg);
224                         ha->fcp_prio_cfg = NULL;
225                         goto exit_fcp_prio_cfg;
226                 }
227
228                 ha->flags.fcp_prio_enabled = 0;
229                 if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
230                         ha->flags.fcp_prio_enabled = 1;
231                 qla24xx_update_all_fcp_prio(vha);
232                 bsg_job->reply->result = DID_OK;
233                 break;
234         default:
235                 ret = -EINVAL;
236                 break;
237         }
238 exit_fcp_prio_cfg:
239         if (!ret)
240                 bsg_job->job_done(bsg_job);
241         return ret;
242 }
243
244 static int
245 qla2x00_process_els(struct fc_bsg_job *bsg_job)
246 {
247         struct fc_rport *rport;
248         fc_port_t *fcport = NULL;
249         struct Scsi_Host *host;
250         scsi_qla_host_t *vha;
251         struct qla_hw_data *ha;
252         srb_t *sp;
253         const char *type;
254         int req_sg_cnt, rsp_sg_cnt;
255         int rval =  (DID_ERROR << 16);
256         uint16_t nextlid = 0;
257
258         if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
259                 rport = bsg_job->rport;
260                 fcport = *(fc_port_t **) rport->dd_data;
261                 host = rport_to_shost(rport);
262                 vha = shost_priv(host);
263                 ha = vha->hw;
264                 type = "FC_BSG_RPT_ELS";
265         } else {
266                 host = bsg_job->shost;
267                 vha = shost_priv(host);
268                 ha = vha->hw;
269                 type = "FC_BSG_HST_ELS_NOLOGIN";
270         }
271
272         if (!vha->flags.online) {
273                 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
274                 rval = -EIO;
275                 goto done;
276         }
277
278         /* pass through is supported only for ISP 4Gb or higher */
279         if (!IS_FWI2_CAPABLE(ha)) {
280                 ql_dbg(ql_dbg_user, vha, 0x7001,
281                     "ELS passthru not supported for ISP23xx based adapters.\n");
282                 rval = -EPERM;
283                 goto done;
284         }
285
286         /*  Multiple SG's are not supported for ELS requests */
287         if (bsg_job->request_payload.sg_cnt > 1 ||
288                 bsg_job->reply_payload.sg_cnt > 1) {
289                 ql_dbg(ql_dbg_user, vha, 0x7002,
290                     "Multiple SG's are not suppored for ELS requests, "
291                     "request_sg_cnt=%x reply_sg_cnt=%x.\n",
292                     bsg_job->request_payload.sg_cnt,
293                     bsg_job->reply_payload.sg_cnt);
294                 rval = -EPERM;
295                 goto done;
296         }
297
298         /* ELS request for rport */
299         if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
300                 /* make sure the rport is logged in,
301                  * if not perform fabric login
302                  */
303                 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
304                         ql_dbg(ql_dbg_user, vha, 0x7003,
305                             "Failed to login port %06X for ELS passthru.\n",
306                             fcport->d_id.b24);
307                         rval = -EIO;
308                         goto done;
309                 }
310         } else {
311                 /* Allocate a dummy fcport structure, since functions
312                  * preparing the IOCB and mailbox command retrieves port
313                  * specific information from fcport structure. For Host based
314                  * ELS commands there will be no fcport structure allocated
315                  */
316                 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
317                 if (!fcport) {
318                         rval = -ENOMEM;
319                         goto done;
320                 }
321
322                 /* Initialize all required  fields of fcport */
323                 fcport->vha = vha;
324                 fcport->d_id.b.al_pa =
325                         bsg_job->request->rqst_data.h_els.port_id[0];
326                 fcport->d_id.b.area =
327                         bsg_job->request->rqst_data.h_els.port_id[1];
328                 fcport->d_id.b.domain =
329                         bsg_job->request->rqst_data.h_els.port_id[2];
330                 fcport->loop_id =
331                         (fcport->d_id.b.al_pa == 0xFD) ?
332                         NPH_FABRIC_CONTROLLER : NPH_F_PORT;
333         }
334
335         req_sg_cnt =
336                 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
337                 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
338         if (!req_sg_cnt) {
339                 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
340                     bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
341                 rval = -ENOMEM;
342                 goto done_free_fcport;
343         }
344
345         rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
346                 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
347         if (!rsp_sg_cnt) {
348                 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
349                     bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
350                 rval = -ENOMEM;
351                 goto done_free_fcport;
352         }
353
354         if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
355                 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
356                 ql_log(ql_log_warn, vha, 0x7008,
357                     "dma mapping resulted in different sg counts, "
358                     "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
359                     "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
360                     req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
361                 rval = -EAGAIN;
362                 goto done_unmap_sg;
363         }
364
365         /* Alloc SRB structure */
366         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
367         if (!sp) {
368                 rval = -ENOMEM;
369                 goto done_unmap_sg;
370         }
371
372         sp->type =
373                 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
374                 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
375         sp->name =
376                 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
377                 "bsg_els_rpt" : "bsg_els_hst");
378         sp->u.bsg_job = bsg_job;
379         sp->free = qla2x00_bsg_sp_free;
380         sp->done = qla2x00_bsg_job_done;
381
382         ql_dbg(ql_dbg_user, vha, 0x700a,
383             "bsg rqst type: %s els type: %x - loop-id=%x "
384             "portid=%-2x%02x%02x.\n", type,
385             bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
386             fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
387
388         rval = qla2x00_start_sp(sp);
389         if (rval != QLA_SUCCESS) {
390                 ql_log(ql_log_warn, vha, 0x700e,
391                     "qla2x00_start_sp failed = %d\n", rval);
392                 qla2x00_rel_sp(vha, sp);
393                 rval = -EIO;
394                 goto done_unmap_sg;
395         }
396         return rval;
397
398 done_unmap_sg:
399         dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
400                 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
401         dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
402                 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
403         goto done_free_fcport;
404
405 done_free_fcport:
406         if (bsg_job->request->msgcode == FC_BSG_RPT_ELS)
407                 kfree(fcport);
408 done:
409         return rval;
410 }
411
412 static inline uint16_t
413 qla24xx_calc_ct_iocbs(uint16_t dsds)
414 {
415         uint16_t iocbs;
416
417         iocbs = 1;
418         if (dsds > 2) {
419                 iocbs += (dsds - 2) / 5;
420                 if ((dsds - 2) % 5)
421                         iocbs++;
422         }
423         return iocbs;
424 }
425
426 static int
427 qla2x00_process_ct(struct fc_bsg_job *bsg_job)
428 {
429         srb_t *sp;
430         struct Scsi_Host *host = bsg_job->shost;
431         scsi_qla_host_t *vha = shost_priv(host);
432         struct qla_hw_data *ha = vha->hw;
433         int rval = (DID_ERROR << 16);
434         int req_sg_cnt, rsp_sg_cnt;
435         uint16_t loop_id;
436         struct fc_port *fcport;
437         char  *type = "FC_BSG_HST_CT";
438
439         req_sg_cnt =
440                 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
441                         bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
442         if (!req_sg_cnt) {
443                 ql_log(ql_log_warn, vha, 0x700f,
444                     "dma_map_sg return %d for request\n", req_sg_cnt);
445                 rval = -ENOMEM;
446                 goto done;
447         }
448
449         rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
450                 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
451         if (!rsp_sg_cnt) {
452                 ql_log(ql_log_warn, vha, 0x7010,
453                     "dma_map_sg return %d for reply\n", rsp_sg_cnt);
454                 rval = -ENOMEM;
455                 goto done;
456         }
457
458         if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
459             (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
460                 ql_log(ql_log_warn, vha, 0x7011,
461                     "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
462                     "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
463                     req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
464                 rval = -EAGAIN;
465                 goto done_unmap_sg;
466         }
467
468         if (!vha->flags.online) {
469                 ql_log(ql_log_warn, vha, 0x7012,
470                     "Host is not online.\n");
471                 rval = -EIO;
472                 goto done_unmap_sg;
473         }
474
475         loop_id =
476                 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
477                         >> 24;
478         switch (loop_id) {
479         case 0xFC:
480                 loop_id = cpu_to_le16(NPH_SNS);
481                 break;
482         case 0xFA:
483                 loop_id = vha->mgmt_svr_loop_id;
484                 break;
485         default:
486                 ql_dbg(ql_dbg_user, vha, 0x7013,
487                     "Unknown loop id: %x.\n", loop_id);
488                 rval = -EINVAL;
489                 goto done_unmap_sg;
490         }
491
492         /* Allocate a dummy fcport structure, since functions preparing the
493          * IOCB and mailbox command retrieves port specific information
494          * from fcport structure. For Host based ELS commands there will be
495          * no fcport structure allocated
496          */
497         fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
498         if (!fcport) {
499                 ql_log(ql_log_warn, vha, 0x7014,
500                     "Failed to allocate fcport.\n");
501                 rval = -ENOMEM;
502                 goto done_unmap_sg;
503         }
504
505         /* Initialize all required  fields of fcport */
506         fcport->vha = vha;
507         fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
508         fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
509         fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
510         fcport->loop_id = loop_id;
511
512         /* Alloc SRB structure */
513         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
514         if (!sp) {
515                 ql_log(ql_log_warn, vha, 0x7015,
516                     "qla2x00_get_sp failed.\n");
517                 rval = -ENOMEM;
518                 goto done_free_fcport;
519         }
520
521         sp->type = SRB_CT_CMD;
522         sp->name = "bsg_ct";
523         sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
524         sp->u.bsg_job = bsg_job;
525         sp->free = qla2x00_bsg_sp_free;
526         sp->done = qla2x00_bsg_job_done;
527
528         ql_dbg(ql_dbg_user, vha, 0x7016,
529             "bsg rqst type: %s else type: %x - "
530             "loop-id=%x portid=%02x%02x%02x.\n", type,
531             (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
532             fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
533             fcport->d_id.b.al_pa);
534
535         rval = qla2x00_start_sp(sp);
536         if (rval != QLA_SUCCESS) {
537                 ql_log(ql_log_warn, vha, 0x7017,
538                     "qla2x00_start_sp failed=%d.\n", rval);
539                 qla2x00_rel_sp(vha, sp);
540                 rval = -EIO;
541                 goto done_free_fcport;
542         }
543         return rval;
544
545 done_free_fcport:
546         kfree(fcport);
547 done_unmap_sg:
548         dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
549                 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
550         dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
551                 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
552 done:
553         return rval;
554 }
555
556 /* Disable loopback mode */
557 static inline int
558 qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
559                             int wait, int wait2)
560 {
561         int ret = 0;
562         int rval = 0;
563         uint16_t new_config[4];
564         struct qla_hw_data *ha = vha->hw;
565
566         if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
567                 goto done_reset_internal;
568
569         memset(new_config, 0 , sizeof(new_config));
570         if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
571             ENABLE_INTERNAL_LOOPBACK ||
572             (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
573             ENABLE_EXTERNAL_LOOPBACK) {
574                 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
575                 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
576                     (new_config[0] & INTERNAL_LOOPBACK_MASK));
577                 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
578
579                 ha->notify_dcbx_comp = wait;
580                 ha->notify_lb_portup_comp = wait2;
581
582                 ret = qla81xx_set_port_config(vha, new_config);
583                 if (ret != QLA_SUCCESS) {
584                         ql_log(ql_log_warn, vha, 0x7025,
585                             "Set port config failed.\n");
586                         ha->notify_dcbx_comp = 0;
587                         ha->notify_lb_portup_comp = 0;
588                         rval = -EINVAL;
589                         goto done_reset_internal;
590                 }
591
592                 /* Wait for DCBX complete event */
593                 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
594                         (DCBX_COMP_TIMEOUT * HZ))) {
595                         ql_dbg(ql_dbg_user, vha, 0x7026,
596                             "DCBX completion not received.\n");
597                         ha->notify_dcbx_comp = 0;
598                         ha->notify_lb_portup_comp = 0;
599                         rval = -EINVAL;
600                         goto done_reset_internal;
601                 } else
602                         ql_dbg(ql_dbg_user, vha, 0x7027,
603                             "DCBX completion received.\n");
604
605                 if (wait2 &&
606                     !wait_for_completion_timeout(&ha->lb_portup_comp,
607                     (LB_PORTUP_COMP_TIMEOUT * HZ))) {
608                         ql_dbg(ql_dbg_user, vha, 0x70c5,
609                             "Port up completion not received.\n");
610                         ha->notify_lb_portup_comp = 0;
611                         rval = -EINVAL;
612                         goto done_reset_internal;
613                 } else
614                         ql_dbg(ql_dbg_user, vha, 0x70c6,
615                             "Port up completion received.\n");
616
617                 ha->notify_dcbx_comp = 0;
618                 ha->notify_lb_portup_comp = 0;
619         }
620 done_reset_internal:
621         return rval;
622 }
623
624 /*
625  * Set the port configuration to enable the internal or external loopback
626  * depending on the loopback mode.
627  */
628 static inline int
629 qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
630         uint16_t *new_config, uint16_t mode)
631 {
632         int ret = 0;
633         int rval = 0;
634         unsigned long rem_tmo = 0, current_tmo = 0;
635         struct qla_hw_data *ha = vha->hw;
636
637         if (!IS_QLA81XX(ha) && !IS_QLA8031(ha) && !IS_QLA8044(ha))
638                 goto done_set_internal;
639
640         if (mode == INTERNAL_LOOPBACK)
641                 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
642         else if (mode == EXTERNAL_LOOPBACK)
643                 new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
644         ql_dbg(ql_dbg_user, vha, 0x70be,
645              "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
646
647         memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
648
649         ha->notify_dcbx_comp = 1;
650         ret = qla81xx_set_port_config(vha, new_config);
651         if (ret != QLA_SUCCESS) {
652                 ql_log(ql_log_warn, vha, 0x7021,
653                     "set port config failed.\n");
654                 ha->notify_dcbx_comp = 0;
655                 rval = -EINVAL;
656                 goto done_set_internal;
657         }
658
659         /* Wait for DCBX complete event */
660         current_tmo = DCBX_COMP_TIMEOUT * HZ;
661         while (1) {
662                 rem_tmo = wait_for_completion_timeout(&ha->dcbx_comp,
663                     current_tmo);
664                 if (!ha->idc_extend_tmo || rem_tmo) {
665                         ha->idc_extend_tmo = 0;
666                         break;
667                 }
668                 current_tmo = ha->idc_extend_tmo * HZ;
669                 ha->idc_extend_tmo = 0;
670         }
671
672         if (!rem_tmo) {
673                 ql_dbg(ql_dbg_user, vha, 0x7022,
674                     "DCBX completion not received.\n");
675                 ret = qla81xx_reset_loopback_mode(vha, new_config, 0, 0);
676                 /*
677                  * If the reset of the loopback mode doesn't work take a FCoE
678                  * dump and reset the chip.
679                  */
680                 if (ret) {
681                         ha->isp_ops->fw_dump(vha, 0);
682                         set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
683                 }
684                 rval = -EINVAL;
685         } else {
686                 if (ha->flags.idc_compl_status) {
687                         ql_dbg(ql_dbg_user, vha, 0x70c3,
688                             "Bad status in IDC Completion AEN\n");
689                         rval = -EINVAL;
690                         ha->flags.idc_compl_status = 0;
691                 } else
692                         ql_dbg(ql_dbg_user, vha, 0x7023,
693                             "DCBX completion received.\n");
694         }
695
696         ha->notify_dcbx_comp = 0;
697         ha->idc_extend_tmo = 0;
698
699 done_set_internal:
700         return rval;
701 }
702
703 static int
704 qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
705 {
706         struct Scsi_Host *host = bsg_job->shost;
707         scsi_qla_host_t *vha = shost_priv(host);
708         struct qla_hw_data *ha = vha->hw;
709         int rval;
710         uint8_t command_sent;
711         char *type;
712         struct msg_echo_lb elreq;
713         uint16_t response[MAILBOX_REGISTER_COUNT];
714         uint16_t config[4], new_config[4];
715         uint8_t *fw_sts_ptr;
716         uint8_t *req_data = NULL;
717         dma_addr_t req_data_dma;
718         uint32_t req_data_len;
719         uint8_t *rsp_data = NULL;
720         dma_addr_t rsp_data_dma;
721         uint32_t rsp_data_len;
722
723         if (!vha->flags.online) {
724                 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
725                 return -EIO;
726         }
727
728         memset(&elreq, 0, sizeof(elreq));
729
730         elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
731                 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
732                 DMA_TO_DEVICE);
733
734         if (!elreq.req_sg_cnt) {
735                 ql_log(ql_log_warn, vha, 0x701a,
736                     "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
737                 return -ENOMEM;
738         }
739
740         elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
741                 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
742                 DMA_FROM_DEVICE);
743
744         if (!elreq.rsp_sg_cnt) {
745                 ql_log(ql_log_warn, vha, 0x701b,
746                     "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
747                 rval = -ENOMEM;
748                 goto done_unmap_req_sg;
749         }
750
751         if ((elreq.req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
752                 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
753                 ql_log(ql_log_warn, vha, 0x701c,
754                     "dma mapping resulted in different sg counts, "
755                     "request_sg_cnt: %x dma_request_sg_cnt: %x "
756                     "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
757                     bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
758                     bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
759                 rval = -EAGAIN;
760                 goto done_unmap_sg;
761         }
762         req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
763         req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
764                 &req_data_dma, GFP_KERNEL);
765         if (!req_data) {
766                 ql_log(ql_log_warn, vha, 0x701d,
767                     "dma alloc failed for req_data.\n");
768                 rval = -ENOMEM;
769                 goto done_unmap_sg;
770         }
771
772         rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
773                 &rsp_data_dma, GFP_KERNEL);
774         if (!rsp_data) {
775                 ql_log(ql_log_warn, vha, 0x7004,
776                     "dma alloc failed for rsp_data.\n");
777                 rval = -ENOMEM;
778                 goto done_free_dma_req;
779         }
780
781         /* Copy the request buffer in req_data now */
782         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
783                 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
784
785         elreq.send_dma = req_data_dma;
786         elreq.rcv_dma = rsp_data_dma;
787         elreq.transfer_size = req_data_len;
788
789         elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
790         elreq.iteration_count =
791             bsg_job->request->rqst_data.h_vendor.vendor_cmd[2];
792
793         if (atomic_read(&vha->loop_state) == LOOP_READY &&
794             (ha->current_topology == ISP_CFG_F ||
795             (le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE &&
796              req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
797             elreq.options == EXTERNAL_LOOPBACK) {
798                 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
799                 ql_dbg(ql_dbg_user, vha, 0x701e,
800                     "BSG request type: %s.\n", type);
801                 command_sent = INT_DEF_LB_ECHO_CMD;
802                 rval = qla2x00_echo_test(vha, &elreq, response);
803         } else {
804                 if (IS_QLA81XX(ha) || IS_QLA8031(ha) || IS_QLA8044(ha)) {
805                         memset(config, 0, sizeof(config));
806                         memset(new_config, 0, sizeof(new_config));
807
808                         if (qla81xx_get_port_config(vha, config)) {
809                                 ql_log(ql_log_warn, vha, 0x701f,
810                                     "Get port config failed.\n");
811                                 rval = -EPERM;
812                                 goto done_free_dma_rsp;
813                         }
814
815                         if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
816                                 ql_dbg(ql_dbg_user, vha, 0x70c4,
817                                     "Loopback operation already in "
818                                     "progress.\n");
819                                 rval = -EAGAIN;
820                                 goto done_free_dma_rsp;
821                         }
822
823                         ql_dbg(ql_dbg_user, vha, 0x70c0,
824                             "elreq.options=%04x\n", elreq.options);
825
826                         if (elreq.options == EXTERNAL_LOOPBACK)
827                                 if (IS_QLA8031(ha) || IS_QLA8044(ha))
828                                         rval = qla81xx_set_loopback_mode(vha,
829                                             config, new_config, elreq.options);
830                                 else
831                                         rval = qla81xx_reset_loopback_mode(vha,
832                                             config, 1, 0);
833                         else
834                                 rval = qla81xx_set_loopback_mode(vha, config,
835                                     new_config, elreq.options);
836
837                         if (rval) {
838                                 rval = -EPERM;
839                                 goto done_free_dma_rsp;
840                         }
841
842                         type = "FC_BSG_HST_VENDOR_LOOPBACK";
843                         ql_dbg(ql_dbg_user, vha, 0x7028,
844                             "BSG request type: %s.\n", type);
845
846                         command_sent = INT_DEF_LB_LOOPBACK_CMD;
847                         rval = qla2x00_loopback_test(vha, &elreq, response);
848
849                         if (response[0] == MBS_COMMAND_ERROR &&
850                                         response[1] == MBS_LB_RESET) {
851                                 ql_log(ql_log_warn, vha, 0x7029,
852                                     "MBX command error, Aborting ISP.\n");
853                                 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
854                                 qla2xxx_wake_dpc(vha);
855                                 qla2x00_wait_for_chip_reset(vha);
856                                 /* Also reset the MPI */
857                                 if (IS_QLA81XX(ha)) {
858                                         if (qla81xx_restart_mpi_firmware(vha) !=
859                                             QLA_SUCCESS) {
860                                                 ql_log(ql_log_warn, vha, 0x702a,
861                                                     "MPI reset failed.\n");
862                                         }
863                                 }
864
865                                 rval = -EIO;
866                                 goto done_free_dma_rsp;
867                         }
868
869                         if (new_config[0]) {
870                                 int ret;
871
872                                 /* Revert back to original port config
873                                  * Also clear internal loopback
874                                  */
875                                 ret = qla81xx_reset_loopback_mode(vha,
876                                     new_config, 0, 1);
877                                 if (ret) {
878                                         /*
879                                          * If the reset of the loopback mode
880                                          * doesn't work take FCoE dump and then
881                                          * reset the chip.
882                                          */
883                                         ha->isp_ops->fw_dump(vha, 0);
884                                         set_bit(ISP_ABORT_NEEDED,
885                                             &vha->dpc_flags);
886                                 }
887
888                         }
889
890                 } else {
891                         type = "FC_BSG_HST_VENDOR_LOOPBACK";
892                         ql_dbg(ql_dbg_user, vha, 0x702b,
893                             "BSG request type: %s.\n", type);
894                         command_sent = INT_DEF_LB_LOOPBACK_CMD;
895                         rval = qla2x00_loopback_test(vha, &elreq, response);
896                 }
897         }
898
899         if (rval) {
900                 ql_log(ql_log_warn, vha, 0x702c,
901                     "Vendor request %s failed.\n", type);
902
903                 rval = 0;
904                 bsg_job->reply->result = (DID_ERROR << 16);
905                 bsg_job->reply->reply_payload_rcv_len = 0;
906         } else {
907                 ql_dbg(ql_dbg_user, vha, 0x702d,
908                     "Vendor request %s completed.\n", type);
909                 bsg_job->reply->result = (DID_OK << 16);
910                 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
911                         bsg_job->reply_payload.sg_cnt, rsp_data,
912                         rsp_data_len);
913         }
914
915         bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
916             sizeof(response) + sizeof(uint8_t);
917         fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
918             sizeof(struct fc_bsg_reply);
919         memcpy(fw_sts_ptr, response, sizeof(response));
920         fw_sts_ptr += sizeof(response);
921         *fw_sts_ptr = command_sent;
922
923 done_free_dma_rsp:
924         dma_free_coherent(&ha->pdev->dev, rsp_data_len,
925                 rsp_data, rsp_data_dma);
926 done_free_dma_req:
927         dma_free_coherent(&ha->pdev->dev, req_data_len,
928                 req_data, req_data_dma);
929 done_unmap_sg:
930         dma_unmap_sg(&ha->pdev->dev,
931             bsg_job->reply_payload.sg_list,
932             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
933 done_unmap_req_sg:
934         dma_unmap_sg(&ha->pdev->dev,
935             bsg_job->request_payload.sg_list,
936             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
937         if (!rval)
938                 bsg_job->job_done(bsg_job);
939         return rval;
940 }
941
942 static int
943 qla84xx_reset(struct fc_bsg_job *bsg_job)
944 {
945         struct Scsi_Host *host = bsg_job->shost;
946         scsi_qla_host_t *vha = shost_priv(host);
947         struct qla_hw_data *ha = vha->hw;
948         int rval = 0;
949         uint32_t flag;
950
951         if (!IS_QLA84XX(ha)) {
952                 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
953                 return -EINVAL;
954         }
955
956         flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
957
958         rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
959
960         if (rval) {
961                 ql_log(ql_log_warn, vha, 0x7030,
962                     "Vendor request 84xx reset failed.\n");
963                 rval = (DID_ERROR << 16);
964
965         } else {
966                 ql_dbg(ql_dbg_user, vha, 0x7031,
967                     "Vendor request 84xx reset completed.\n");
968                 bsg_job->reply->result = DID_OK;
969                 bsg_job->job_done(bsg_job);
970         }
971
972         return rval;
973 }
974
975 static int
976 qla84xx_updatefw(struct fc_bsg_job *bsg_job)
977 {
978         struct Scsi_Host *host = bsg_job->shost;
979         scsi_qla_host_t *vha = shost_priv(host);
980         struct qla_hw_data *ha = vha->hw;
981         struct verify_chip_entry_84xx *mn = NULL;
982         dma_addr_t mn_dma, fw_dma;
983         void *fw_buf = NULL;
984         int rval = 0;
985         uint32_t sg_cnt;
986         uint32_t data_len;
987         uint16_t options;
988         uint32_t flag;
989         uint32_t fw_ver;
990
991         if (!IS_QLA84XX(ha)) {
992                 ql_dbg(ql_dbg_user, vha, 0x7032,
993                     "Not 84xx, exiting.\n");
994                 return -EINVAL;
995         }
996
997         sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
998                 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
999         if (!sg_cnt) {
1000                 ql_log(ql_log_warn, vha, 0x7033,
1001                     "dma_map_sg returned %d for request.\n", sg_cnt);
1002                 return -ENOMEM;
1003         }
1004
1005         if (sg_cnt != bsg_job->request_payload.sg_cnt) {
1006                 ql_log(ql_log_warn, vha, 0x7034,
1007                     "DMA mapping resulted in different sg counts, "
1008                     "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1009                     bsg_job->request_payload.sg_cnt, sg_cnt);
1010                 rval = -EAGAIN;
1011                 goto done_unmap_sg;
1012         }
1013
1014         data_len = bsg_job->request_payload.payload_len;
1015         fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
1016                 &fw_dma, GFP_KERNEL);
1017         if (!fw_buf) {
1018                 ql_log(ql_log_warn, vha, 0x7035,
1019                     "DMA alloc failed for fw_buf.\n");
1020                 rval = -ENOMEM;
1021                 goto done_unmap_sg;
1022         }
1023
1024         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1025                 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
1026
1027         mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1028         if (!mn) {
1029                 ql_log(ql_log_warn, vha, 0x7036,
1030                     "DMA alloc failed for fw buffer.\n");
1031                 rval = -ENOMEM;
1032                 goto done_free_fw_buf;
1033         }
1034
1035         flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1036         fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
1037
1038         memset(mn, 0, sizeof(struct access_chip_84xx));
1039         mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
1040         mn->entry_count = 1;
1041
1042         options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
1043         if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
1044                 options |= VCO_DIAG_FW;
1045
1046         mn->options = cpu_to_le16(options);
1047         mn->fw_ver =  cpu_to_le32(fw_ver);
1048         mn->fw_size =  cpu_to_le32(data_len);
1049         mn->fw_seq_size =  cpu_to_le32(data_len);
1050         mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
1051         mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
1052         mn->dseg_length = cpu_to_le32(data_len);
1053         mn->data_seg_cnt = cpu_to_le16(1);
1054
1055         rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
1056
1057         if (rval) {
1058                 ql_log(ql_log_warn, vha, 0x7037,
1059                     "Vendor request 84xx updatefw failed.\n");
1060
1061                 rval = (DID_ERROR << 16);
1062         } else {
1063                 ql_dbg(ql_dbg_user, vha, 0x7038,
1064                     "Vendor request 84xx updatefw completed.\n");
1065
1066                 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1067                 bsg_job->reply->result = DID_OK;
1068         }
1069
1070         dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1071
1072 done_free_fw_buf:
1073         dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
1074
1075 done_unmap_sg:
1076         dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1077                 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1078
1079         if (!rval)
1080                 bsg_job->job_done(bsg_job);
1081         return rval;
1082 }
1083
1084 static int
1085 qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1086 {
1087         struct Scsi_Host *host = bsg_job->shost;
1088         scsi_qla_host_t *vha = shost_priv(host);
1089         struct qla_hw_data *ha = vha->hw;
1090         struct access_chip_84xx *mn = NULL;
1091         dma_addr_t mn_dma, mgmt_dma;
1092         void *mgmt_b = NULL;
1093         int rval = 0;
1094         struct qla_bsg_a84_mgmt *ql84_mgmt;
1095         uint32_t sg_cnt;
1096         uint32_t data_len = 0;
1097         uint32_t dma_direction = DMA_NONE;
1098
1099         if (!IS_QLA84XX(ha)) {
1100                 ql_log(ql_log_warn, vha, 0x703a,
1101                     "Not 84xx, exiting.\n");
1102                 return -EINVAL;
1103         }
1104
1105         mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1106         if (!mn) {
1107                 ql_log(ql_log_warn, vha, 0x703c,
1108                     "DMA alloc failed for fw buffer.\n");
1109                 return -ENOMEM;
1110         }
1111
1112         memset(mn, 0, sizeof(struct access_chip_84xx));
1113         mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1114         mn->entry_count = 1;
1115         ql84_mgmt = (void *)bsg_job->request + sizeof(struct fc_bsg_request);
1116         switch (ql84_mgmt->mgmt.cmd) {
1117         case QLA84_MGMT_READ_MEM:
1118         case QLA84_MGMT_GET_INFO:
1119                 sg_cnt = dma_map_sg(&ha->pdev->dev,
1120                         bsg_job->reply_payload.sg_list,
1121                         bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1122                 if (!sg_cnt) {
1123                         ql_log(ql_log_warn, vha, 0x703d,
1124                             "dma_map_sg returned %d for reply.\n", sg_cnt);
1125                         rval = -ENOMEM;
1126                         goto exit_mgmt;
1127                 }
1128
1129                 dma_direction = DMA_FROM_DEVICE;
1130
1131                 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
1132                         ql_log(ql_log_warn, vha, 0x703e,
1133                             "DMA mapping resulted in different sg counts, "
1134                             "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1135                             bsg_job->reply_payload.sg_cnt, sg_cnt);
1136                         rval = -EAGAIN;
1137                         goto done_unmap_sg;
1138                 }
1139
1140                 data_len = bsg_job->reply_payload.payload_len;
1141
1142                 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1143                     &mgmt_dma, GFP_KERNEL);
1144                 if (!mgmt_b) {
1145                         ql_log(ql_log_warn, vha, 0x703f,
1146                             "DMA alloc failed for mgmt_b.\n");
1147                         rval = -ENOMEM;
1148                         goto done_unmap_sg;
1149                 }
1150
1151                 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1152                         mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1153                         mn->parameter1 =
1154                                 cpu_to_le32(
1155                                 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1156
1157                 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1158                         mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1159                         mn->parameter1 =
1160                                 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1161
1162                         mn->parameter2 =
1163                                 cpu_to_le32(
1164                                 ql84_mgmt->mgmt.mgmtp.u.info.context);
1165                 }
1166                 break;
1167
1168         case QLA84_MGMT_WRITE_MEM:
1169                 sg_cnt = dma_map_sg(&ha->pdev->dev,
1170                         bsg_job->request_payload.sg_list,
1171                         bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1172
1173                 if (!sg_cnt) {
1174                         ql_log(ql_log_warn, vha, 0x7040,
1175                             "dma_map_sg returned %d.\n", sg_cnt);
1176                         rval = -ENOMEM;
1177                         goto exit_mgmt;
1178                 }
1179
1180                 dma_direction = DMA_TO_DEVICE;
1181
1182                 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
1183                         ql_log(ql_log_warn, vha, 0x7041,
1184                             "DMA mapping resulted in different sg counts, "
1185                             "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1186                             bsg_job->request_payload.sg_cnt, sg_cnt);
1187                         rval = -EAGAIN;
1188                         goto done_unmap_sg;
1189                 }
1190
1191                 data_len = bsg_job->request_payload.payload_len;
1192                 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1193                         &mgmt_dma, GFP_KERNEL);
1194                 if (!mgmt_b) {
1195                         ql_log(ql_log_warn, vha, 0x7042,
1196                             "DMA alloc failed for mgmt_b.\n");
1197                         rval = -ENOMEM;
1198                         goto done_unmap_sg;
1199                 }
1200
1201                 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1202                         bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1203
1204                 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1205                 mn->parameter1 =
1206                         cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1207                 break;
1208
1209         case QLA84_MGMT_CHNG_CONFIG:
1210                 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1211                 mn->parameter1 =
1212                         cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1213
1214                 mn->parameter2 =
1215                         cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1216
1217                 mn->parameter3 =
1218                         cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1219                 break;
1220
1221         default:
1222                 rval = -EIO;
1223                 goto exit_mgmt;
1224         }
1225
1226         if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1227                 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1228                 mn->dseg_count = cpu_to_le16(1);
1229                 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1230                 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1231                 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1232         }
1233
1234         rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1235
1236         if (rval) {
1237                 ql_log(ql_log_warn, vha, 0x7043,
1238                     "Vendor request 84xx mgmt failed.\n");
1239
1240                 rval = (DID_ERROR << 16);
1241
1242         } else {
1243                 ql_dbg(ql_dbg_user, vha, 0x7044,
1244                     "Vendor request 84xx mgmt completed.\n");
1245
1246                 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1247                 bsg_job->reply->result = DID_OK;
1248
1249                 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1250                         (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1251                         bsg_job->reply->reply_payload_rcv_len =
1252                                 bsg_job->reply_payload.payload_len;
1253
1254                         sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1255                                 bsg_job->reply_payload.sg_cnt, mgmt_b,
1256                                 data_len);
1257                 }
1258         }
1259
1260 done_unmap_sg:
1261         if (mgmt_b)
1262                 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1263
1264         if (dma_direction == DMA_TO_DEVICE)
1265                 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1266                         bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1267         else if (dma_direction == DMA_FROM_DEVICE)
1268                 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1269                         bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1270
1271 exit_mgmt:
1272         dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1273
1274         if (!rval)
1275                 bsg_job->job_done(bsg_job);
1276         return rval;
1277 }
1278
1279 static int
1280 qla24xx_iidma(struct fc_bsg_job *bsg_job)
1281 {
1282         struct Scsi_Host *host = bsg_job->shost;
1283         scsi_qla_host_t *vha = shost_priv(host);
1284         int rval = 0;
1285         struct qla_port_param *port_param = NULL;
1286         fc_port_t *fcport = NULL;
1287         int found = 0;
1288         uint16_t mb[MAILBOX_REGISTER_COUNT];
1289         uint8_t *rsp_ptr = NULL;
1290
1291         if (!IS_IIDMA_CAPABLE(vha->hw)) {
1292                 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
1293                 return -EINVAL;
1294         }
1295
1296         port_param = (void *)bsg_job->request + sizeof(struct fc_bsg_request);
1297         if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
1298                 ql_log(ql_log_warn, vha, 0x7048,
1299                     "Invalid destination type.\n");
1300                 return -EINVAL;
1301         }
1302
1303         list_for_each_entry(fcport, &vha->vp_fcports, list) {
1304                 if (fcport->port_type != FCT_TARGET)
1305                         continue;
1306
1307                 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1308                         fcport->port_name, sizeof(fcport->port_name)))
1309                         continue;
1310
1311                 found = 1;
1312                 break;
1313         }
1314
1315         if (!found) {
1316                 ql_log(ql_log_warn, vha, 0x7049,
1317                     "Failed to find port.\n");
1318                 return -EINVAL;
1319         }
1320
1321         if (atomic_read(&fcport->state) != FCS_ONLINE) {
1322                 ql_log(ql_log_warn, vha, 0x704a,
1323                     "Port is not online.\n");
1324                 return -EINVAL;
1325         }
1326
1327         if (fcport->flags & FCF_LOGIN_NEEDED) {
1328                 ql_log(ql_log_warn, vha, 0x704b,
1329                     "Remote port not logged in flags = 0x%x.\n", fcport->flags);
1330                 return -EINVAL;
1331         }
1332
1333         if (port_param->mode)
1334                 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1335                         port_param->speed, mb);
1336         else
1337                 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1338                         &port_param->speed, mb);
1339
1340         if (rval) {
1341                 ql_log(ql_log_warn, vha, 0x704c,
1342                     "iIDMA cmd failed for %8phN -- "
1343                     "%04x %x %04x %04x.\n", fcport->port_name,
1344                     rval, fcport->fp_speed, mb[0], mb[1]);
1345                 rval = (DID_ERROR << 16);
1346         } else {
1347                 if (!port_param->mode) {
1348                         bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1349                                 sizeof(struct qla_port_param);
1350
1351                         rsp_ptr = ((uint8_t *)bsg_job->reply) +
1352                                 sizeof(struct fc_bsg_reply);
1353
1354                         memcpy(rsp_ptr, port_param,
1355                                 sizeof(struct qla_port_param));
1356                 }
1357
1358                 bsg_job->reply->result = DID_OK;
1359                 bsg_job->job_done(bsg_job);
1360         }
1361
1362         return rval;
1363 }
1364
1365 static int
1366 qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
1367         uint8_t is_update)
1368 {
1369         uint32_t start = 0;
1370         int valid = 0;
1371         struct qla_hw_data *ha = vha->hw;
1372
1373         if (unlikely(pci_channel_offline(ha->pdev)))
1374                 return -EINVAL;
1375
1376         start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1377         if (start > ha->optrom_size) {
1378                 ql_log(ql_log_warn, vha, 0x7055,
1379                     "start %d > optrom_size %d.\n", start, ha->optrom_size);
1380                 return -EINVAL;
1381         }
1382
1383         if (ha->optrom_state != QLA_SWAITING) {
1384                 ql_log(ql_log_info, vha, 0x7056,
1385                     "optrom_state %d.\n", ha->optrom_state);
1386                 return -EBUSY;
1387         }
1388
1389         ha->optrom_region_start = start;
1390         ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
1391         if (is_update) {
1392                 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1393                         valid = 1;
1394                 else if (start == (ha->flt_region_boot * 4) ||
1395                     start == (ha->flt_region_fw * 4))
1396                         valid = 1;
1397                 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
1398                     IS_CNA_CAPABLE(ha) || IS_QLA2031(ha) || IS_QLA27XX(ha))
1399                         valid = 1;
1400                 if (!valid) {
1401                         ql_log(ql_log_warn, vha, 0x7058,
1402                             "Invalid start region 0x%x/0x%x.\n", start,
1403                             bsg_job->request_payload.payload_len);
1404                         return -EINVAL;
1405                 }
1406
1407                 ha->optrom_region_size = start +
1408                     bsg_job->request_payload.payload_len > ha->optrom_size ?
1409                     ha->optrom_size - start :
1410                     bsg_job->request_payload.payload_len;
1411                 ha->optrom_state = QLA_SWRITING;
1412         } else {
1413                 ha->optrom_region_size = start +
1414                     bsg_job->reply_payload.payload_len > ha->optrom_size ?
1415                     ha->optrom_size - start :
1416                     bsg_job->reply_payload.payload_len;
1417                 ha->optrom_state = QLA_SREADING;
1418         }
1419
1420         ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1421         if (!ha->optrom_buffer) {
1422                 ql_log(ql_log_warn, vha, 0x7059,
1423                     "Read: Unable to allocate memory for optrom retrieval "
1424                     "(%x)\n", ha->optrom_region_size);
1425
1426                 ha->optrom_state = QLA_SWAITING;
1427                 return -ENOMEM;
1428         }
1429
1430         memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1431         return 0;
1432 }
1433
1434 static int
1435 qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1436 {
1437         struct Scsi_Host *host = bsg_job->shost;
1438         scsi_qla_host_t *vha = shost_priv(host);
1439         struct qla_hw_data *ha = vha->hw;
1440         int rval = 0;
1441
1442         if (ha->flags.nic_core_reset_hdlr_active)
1443                 return -EBUSY;
1444
1445         mutex_lock(&ha->optrom_mutex);
1446         rval = qla2x00_optrom_setup(bsg_job, vha, 0);
1447         if (rval) {
1448                 mutex_unlock(&ha->optrom_mutex);
1449                 return rval;
1450         }
1451
1452         ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1453             ha->optrom_region_start, ha->optrom_region_size);
1454
1455         sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1456             bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1457             ha->optrom_region_size);
1458
1459         bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1460         bsg_job->reply->result = DID_OK;
1461         vfree(ha->optrom_buffer);
1462         ha->optrom_buffer = NULL;
1463         ha->optrom_state = QLA_SWAITING;
1464         mutex_unlock(&ha->optrom_mutex);
1465         bsg_job->job_done(bsg_job);
1466         return rval;
1467 }
1468
1469 static int
1470 qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1471 {
1472         struct Scsi_Host *host = bsg_job->shost;
1473         scsi_qla_host_t *vha = shost_priv(host);
1474         struct qla_hw_data *ha = vha->hw;
1475         int rval = 0;
1476
1477         mutex_lock(&ha->optrom_mutex);
1478         rval = qla2x00_optrom_setup(bsg_job, vha, 1);
1479         if (rval) {
1480                 mutex_unlock(&ha->optrom_mutex);
1481                 return rval;
1482         }
1483
1484         /* Set the isp82xx_no_md_cap not to capture minidump */
1485         ha->flags.isp82xx_no_md_cap = 1;
1486
1487         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1488             bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1489             ha->optrom_region_size);
1490
1491         ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1492             ha->optrom_region_start, ha->optrom_region_size);
1493
1494         bsg_job->reply->result = DID_OK;
1495         vfree(ha->optrom_buffer);
1496         ha->optrom_buffer = NULL;
1497         ha->optrom_state = QLA_SWAITING;
1498         mutex_unlock(&ha->optrom_mutex);
1499         bsg_job->job_done(bsg_job);
1500         return rval;
1501 }
1502
1503 static int
1504 qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1505 {
1506         struct Scsi_Host *host = bsg_job->shost;
1507         scsi_qla_host_t *vha = shost_priv(host);
1508         struct qla_hw_data *ha = vha->hw;
1509         int rval = 0;
1510         uint8_t bsg[DMA_POOL_SIZE];
1511         struct qla_image_version_list *list = (void *)bsg;
1512         struct qla_image_version *image;
1513         uint32_t count;
1514         dma_addr_t sfp_dma;
1515         void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1516         if (!sfp) {
1517                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1518                     EXT_STATUS_NO_MEMORY;
1519                 goto done;
1520         }
1521
1522         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1523             bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1524
1525         image = list->version;
1526         count = list->count;
1527         while (count--) {
1528                 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1529                 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1530                     image->field_address.device, image->field_address.offset,
1531                     sizeof(image->field_info), image->field_address.option);
1532                 if (rval) {
1533                         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1534                             EXT_STATUS_MAILBOX;
1535                         goto dealloc;
1536                 }
1537                 image++;
1538         }
1539
1540         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1541
1542 dealloc:
1543         dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1544
1545 done:
1546         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1547         bsg_job->reply->result = DID_OK << 16;
1548         bsg_job->job_done(bsg_job);
1549
1550         return 0;
1551 }
1552
1553 static int
1554 qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1555 {
1556         struct Scsi_Host *host = bsg_job->shost;
1557         scsi_qla_host_t *vha = shost_priv(host);
1558         struct qla_hw_data *ha = vha->hw;
1559         int rval = 0;
1560         uint8_t bsg[DMA_POOL_SIZE];
1561         struct qla_status_reg *sr = (void *)bsg;
1562         dma_addr_t sfp_dma;
1563         uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1564         if (!sfp) {
1565                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1566                     EXT_STATUS_NO_MEMORY;
1567                 goto done;
1568         }
1569
1570         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1571             bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1572
1573         rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1574             sr->field_address.device, sr->field_address.offset,
1575             sizeof(sr->status_reg), sr->field_address.option);
1576         sr->status_reg = *sfp;
1577
1578         if (rval) {
1579                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1580                     EXT_STATUS_MAILBOX;
1581                 goto dealloc;
1582         }
1583
1584         sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1585             bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1586
1587         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1588
1589 dealloc:
1590         dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1591
1592 done:
1593         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1594         bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1595         bsg_job->reply->result = DID_OK << 16;
1596         bsg_job->job_done(bsg_job);
1597
1598         return 0;
1599 }
1600
1601 static int
1602 qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1603 {
1604         struct Scsi_Host *host = bsg_job->shost;
1605         scsi_qla_host_t *vha = shost_priv(host);
1606         struct qla_hw_data *ha = vha->hw;
1607         int rval = 0;
1608         uint8_t bsg[DMA_POOL_SIZE];
1609         struct qla_status_reg *sr = (void *)bsg;
1610         dma_addr_t sfp_dma;
1611         uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1612         if (!sfp) {
1613                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1614                     EXT_STATUS_NO_MEMORY;
1615                 goto done;
1616         }
1617
1618         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1619             bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1620
1621         *sfp = sr->status_reg;
1622         rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1623             sr->field_address.device, sr->field_address.offset,
1624             sizeof(sr->status_reg), sr->field_address.option);
1625
1626         if (rval) {
1627                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1628                     EXT_STATUS_MAILBOX;
1629                 goto dealloc;
1630         }
1631
1632         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1633
1634 dealloc:
1635         dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1636
1637 done:
1638         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1639         bsg_job->reply->result = DID_OK << 16;
1640         bsg_job->job_done(bsg_job);
1641
1642         return 0;
1643 }
1644
1645 static int
1646 qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1647 {
1648         struct Scsi_Host *host = bsg_job->shost;
1649         scsi_qla_host_t *vha = shost_priv(host);
1650         struct qla_hw_data *ha = vha->hw;
1651         int rval = 0;
1652         uint8_t bsg[DMA_POOL_SIZE];
1653         struct qla_i2c_access *i2c = (void *)bsg;
1654         dma_addr_t sfp_dma;
1655         uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1656         if (!sfp) {
1657                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1658                     EXT_STATUS_NO_MEMORY;
1659                 goto done;
1660         }
1661
1662         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1663             bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1664
1665         memcpy(sfp, i2c->buffer, i2c->length);
1666         rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1667             i2c->device, i2c->offset, i2c->length, i2c->option);
1668
1669         if (rval) {
1670                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1671                     EXT_STATUS_MAILBOX;
1672                 goto dealloc;
1673         }
1674
1675         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1676
1677 dealloc:
1678         dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1679
1680 done:
1681         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1682         bsg_job->reply->result = DID_OK << 16;
1683         bsg_job->job_done(bsg_job);
1684
1685         return 0;
1686 }
1687
1688 static int
1689 qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1690 {
1691         struct Scsi_Host *host = bsg_job->shost;
1692         scsi_qla_host_t *vha = shost_priv(host);
1693         struct qla_hw_data *ha = vha->hw;
1694         int rval = 0;
1695         uint8_t bsg[DMA_POOL_SIZE];
1696         struct qla_i2c_access *i2c = (void *)bsg;
1697         dma_addr_t sfp_dma;
1698         uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1699         if (!sfp) {
1700                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1701                     EXT_STATUS_NO_MEMORY;
1702                 goto done;
1703         }
1704
1705         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1706             bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1707
1708         rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1709                 i2c->device, i2c->offset, i2c->length, i2c->option);
1710
1711         if (rval) {
1712                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1713                     EXT_STATUS_MAILBOX;
1714                 goto dealloc;
1715         }
1716
1717         memcpy(i2c->buffer, sfp, i2c->length);
1718         sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1719             bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1720
1721         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1722
1723 dealloc:
1724         dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1725
1726 done:
1727         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1728         bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1729         bsg_job->reply->result = DID_OK << 16;
1730         bsg_job->job_done(bsg_job);
1731
1732         return 0;
1733 }
1734
1735 static int
1736 qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1737 {
1738         struct Scsi_Host *host = bsg_job->shost;
1739         scsi_qla_host_t *vha = shost_priv(host);
1740         struct qla_hw_data *ha = vha->hw;
1741         uint32_t rval = EXT_STATUS_OK;
1742         uint16_t req_sg_cnt = 0;
1743         uint16_t rsp_sg_cnt = 0;
1744         uint16_t nextlid = 0;
1745         uint32_t tot_dsds;
1746         srb_t *sp = NULL;
1747         uint32_t req_data_len;
1748         uint32_t rsp_data_len;
1749
1750         /* Check the type of the adapter */
1751         if (!IS_BIDI_CAPABLE(ha)) {
1752                 ql_log(ql_log_warn, vha, 0x70a0,
1753                         "This adapter is not supported\n");
1754                 rval = EXT_STATUS_NOT_SUPPORTED;
1755                 goto done;
1756         }
1757
1758         if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1759                 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1760                 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1761                 rval =  EXT_STATUS_BUSY;
1762                 goto done;
1763         }
1764
1765         /* Check if host is online */
1766         if (!vha->flags.online) {
1767                 ql_log(ql_log_warn, vha, 0x70a1,
1768                         "Host is not online\n");
1769                 rval = EXT_STATUS_DEVICE_OFFLINE;
1770                 goto done;
1771         }
1772
1773         /* Check if cable is plugged in or not */
1774         if (vha->device_flags & DFLG_NO_CABLE) {
1775                 ql_log(ql_log_warn, vha, 0x70a2,
1776                         "Cable is unplugged...\n");
1777                 rval = EXT_STATUS_INVALID_CFG;
1778                 goto done;
1779         }
1780
1781         /* Check if the switch is connected or not */
1782         if (ha->current_topology != ISP_CFG_F) {
1783                 ql_log(ql_log_warn, vha, 0x70a3,
1784                         "Host is not connected to the switch\n");
1785                 rval = EXT_STATUS_INVALID_CFG;
1786                 goto done;
1787         }
1788
1789         /* Check if operating mode is P2P */
1790         if (ha->operating_mode != P2P) {
1791                 ql_log(ql_log_warn, vha, 0x70a4,
1792                     "Host is operating mode is not P2p\n");
1793                 rval = EXT_STATUS_INVALID_CFG;
1794                 goto done;
1795         }
1796
1797         mutex_lock(&ha->selflogin_lock);
1798         if (vha->self_login_loop_id == 0) {
1799                 /* Initialize all required  fields of fcport */
1800                 vha->bidir_fcport.vha = vha;
1801                 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1802                 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1803                 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1804                 vha->bidir_fcport.loop_id = vha->loop_id;
1805
1806                 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1807                         ql_log(ql_log_warn, vha, 0x70a7,
1808                             "Failed to login port %06X for bidirectional IOCB\n",
1809                             vha->bidir_fcport.d_id.b24);
1810                         mutex_unlock(&ha->selflogin_lock);
1811                         rval = EXT_STATUS_MAILBOX;
1812                         goto done;
1813                 }
1814                 vha->self_login_loop_id = nextlid - 1;
1815
1816         }
1817         /* Assign the self login loop id to fcport */
1818         mutex_unlock(&ha->selflogin_lock);
1819
1820         vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1821
1822         req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1823                 bsg_job->request_payload.sg_list,
1824                 bsg_job->request_payload.sg_cnt,
1825                 DMA_TO_DEVICE);
1826
1827         if (!req_sg_cnt) {
1828                 rval = EXT_STATUS_NO_MEMORY;
1829                 goto done;
1830         }
1831
1832         rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1833                 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1834                 DMA_FROM_DEVICE);
1835
1836         if (!rsp_sg_cnt) {
1837                 rval = EXT_STATUS_NO_MEMORY;
1838                 goto done_unmap_req_sg;
1839         }
1840
1841         if ((req_sg_cnt !=  bsg_job->request_payload.sg_cnt) ||
1842                 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1843                 ql_dbg(ql_dbg_user, vha, 0x70a9,
1844                     "Dma mapping resulted in different sg counts "
1845                     "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1846                     "%x dma_reply_sg_cnt: %x]\n",
1847                     bsg_job->request_payload.sg_cnt, req_sg_cnt,
1848                     bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1849                 rval = EXT_STATUS_NO_MEMORY;
1850                 goto done_unmap_sg;
1851         }
1852
1853         req_data_len = bsg_job->request_payload.payload_len;
1854         rsp_data_len = bsg_job->reply_payload.payload_len;
1855
1856         if (req_data_len != rsp_data_len) {
1857                 rval = EXT_STATUS_BUSY;
1858                 ql_log(ql_log_warn, vha, 0x70aa,
1859                     "req_data_len != rsp_data_len\n");
1860                 goto done_unmap_sg;
1861         }
1862
1863         /* Alloc SRB structure */
1864         sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1865         if (!sp) {
1866                 ql_dbg(ql_dbg_user, vha, 0x70ac,
1867                     "Alloc SRB structure failed\n");
1868                 rval = EXT_STATUS_NO_MEMORY;
1869                 goto done_unmap_sg;
1870         }
1871
1872         /*Populate srb->ctx with bidir ctx*/
1873         sp->u.bsg_job = bsg_job;
1874         sp->free = qla2x00_bsg_sp_free;
1875         sp->type = SRB_BIDI_CMD;
1876         sp->done = qla2x00_bsg_job_done;
1877
1878         /* Add the read and write sg count */
1879         tot_dsds = rsp_sg_cnt + req_sg_cnt;
1880
1881         rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1882         if (rval != EXT_STATUS_OK)
1883                 goto done_free_srb;
1884         /* the bsg request  will be completed in the interrupt handler */
1885         return rval;
1886
1887 done_free_srb:
1888         mempool_free(sp, ha->srb_mempool);
1889 done_unmap_sg:
1890         dma_unmap_sg(&ha->pdev->dev,
1891             bsg_job->reply_payload.sg_list,
1892             bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1893 done_unmap_req_sg:
1894         dma_unmap_sg(&ha->pdev->dev,
1895             bsg_job->request_payload.sg_list,
1896             bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1897 done:
1898
1899         /* Return an error vendor specific response
1900          * and complete the bsg request
1901          */
1902         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1903         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1904         bsg_job->reply->reply_payload_rcv_len = 0;
1905         bsg_job->reply->result = (DID_OK) << 16;
1906         bsg_job->job_done(bsg_job);
1907         /* Always return success, vendor rsp carries correct status */
1908         return 0;
1909 }
1910
1911 static int
1912 qlafx00_mgmt_cmd(struct fc_bsg_job *bsg_job)
1913 {
1914         struct Scsi_Host *host = bsg_job->shost;
1915         scsi_qla_host_t *vha = shost_priv(host);
1916         struct qla_hw_data *ha = vha->hw;
1917         int rval = (DID_ERROR << 16);
1918         struct qla_mt_iocb_rqst_fx00 *piocb_rqst;
1919         srb_t *sp;
1920         int req_sg_cnt = 0, rsp_sg_cnt = 0;
1921         struct fc_port *fcport;
1922         char  *type = "FC_BSG_HST_FX_MGMT";
1923
1924         /* Copy the IOCB specific information */
1925         piocb_rqst = (struct qla_mt_iocb_rqst_fx00 *)
1926             &bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1927
1928         /* Dump the vendor information */
1929         ql_dump_buffer(ql_dbg_user + ql_dbg_verbose , vha, 0x70cf,
1930             (uint8_t *)piocb_rqst, sizeof(struct qla_mt_iocb_rqst_fx00));
1931
1932         if (!vha->flags.online) {
1933                 ql_log(ql_log_warn, vha, 0x70d0,
1934                     "Host is not online.\n");
1935                 rval = -EIO;
1936                 goto done;
1937         }
1938
1939         if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID) {
1940                 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1941                     bsg_job->request_payload.sg_list,
1942                     bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1943                 if (!req_sg_cnt) {
1944                         ql_log(ql_log_warn, vha, 0x70c7,
1945                             "dma_map_sg return %d for request\n", req_sg_cnt);
1946                         rval = -ENOMEM;
1947                         goto done;
1948                 }
1949         }
1950
1951         if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID) {
1952                 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1953                     bsg_job->reply_payload.sg_list,
1954                     bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1955                 if (!rsp_sg_cnt) {
1956                         ql_log(ql_log_warn, vha, 0x70c8,
1957                             "dma_map_sg return %d for reply\n", rsp_sg_cnt);
1958                         rval = -ENOMEM;
1959                         goto done_unmap_req_sg;
1960                 }
1961         }
1962
1963         ql_dbg(ql_dbg_user, vha, 0x70c9,
1964             "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
1965             "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
1966             req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1967
1968         /* Allocate a dummy fcport structure, since functions preparing the
1969          * IOCB and mailbox command retrieves port specific information
1970          * from fcport structure. For Host based ELS commands there will be
1971          * no fcport structure allocated
1972          */
1973         fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
1974         if (!fcport) {
1975                 ql_log(ql_log_warn, vha, 0x70ca,
1976                     "Failed to allocate fcport.\n");
1977                 rval = -ENOMEM;
1978                 goto done_unmap_rsp_sg;
1979         }
1980
1981         /* Alloc SRB structure */
1982         sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
1983         if (!sp) {
1984                 ql_log(ql_log_warn, vha, 0x70cb,
1985                     "qla2x00_get_sp failed.\n");
1986                 rval = -ENOMEM;
1987                 goto done_free_fcport;
1988         }
1989
1990         /* Initialize all required  fields of fcport */
1991         fcport->vha = vha;
1992         fcport->loop_id = piocb_rqst->dataword;
1993
1994         sp->type = SRB_FXIOCB_BCMD;
1995         sp->name = "bsg_fx_mgmt";
1996         sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
1997         sp->u.bsg_job = bsg_job;
1998         sp->free = qla2x00_bsg_sp_free;
1999         sp->done = qla2x00_bsg_job_done;
2000
2001         ql_dbg(ql_dbg_user, vha, 0x70cc,
2002             "bsg rqst type: %s fx_mgmt_type: %x id=%x\n",
2003             type, piocb_rqst->func_type, fcport->loop_id);
2004
2005         rval = qla2x00_start_sp(sp);
2006         if (rval != QLA_SUCCESS) {
2007                 ql_log(ql_log_warn, vha, 0x70cd,
2008                     "qla2x00_start_sp failed=%d.\n", rval);
2009                 mempool_free(sp, ha->srb_mempool);
2010                 rval = -EIO;
2011                 goto done_free_fcport;
2012         }
2013         return rval;
2014
2015 done_free_fcport:
2016         kfree(fcport);
2017
2018 done_unmap_rsp_sg:
2019         if (piocb_rqst->flags & SRB_FXDISC_RESP_DMA_VALID)
2020                 dma_unmap_sg(&ha->pdev->dev,
2021                     bsg_job->reply_payload.sg_list,
2022                     bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
2023 done_unmap_req_sg:
2024         if (piocb_rqst->flags & SRB_FXDISC_REQ_DMA_VALID)
2025                 dma_unmap_sg(&ha->pdev->dev,
2026                     bsg_job->request_payload.sg_list,
2027                     bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
2028
2029 done:
2030         return rval;
2031 }
2032
2033 static int
2034 qla26xx_serdes_op(struct fc_bsg_job *bsg_job)
2035 {
2036         struct Scsi_Host *host = bsg_job->shost;
2037         scsi_qla_host_t *vha = shost_priv(host);
2038         int rval = 0;
2039         struct qla_serdes_reg sr;
2040
2041         memset(&sr, 0, sizeof(sr));
2042
2043         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2044             bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
2045
2046         switch (sr.cmd) {
2047         case INT_SC_SERDES_WRITE_REG:
2048                 rval = qla2x00_write_serdes_word(vha, sr.addr, sr.val);
2049                 bsg_job->reply->reply_payload_rcv_len = 0;
2050                 break;
2051         case INT_SC_SERDES_READ_REG:
2052                 rval = qla2x00_read_serdes_word(vha, sr.addr, &sr.val);
2053                 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2054                     bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
2055                 bsg_job->reply->reply_payload_rcv_len = sizeof(sr);
2056                 break;
2057         default:
2058                 ql_dbg(ql_dbg_user, vha, 0x708c,
2059                     "Unknown serdes cmd %x.\n", sr.cmd);
2060                 rval = -EINVAL;
2061                 break;
2062         }
2063
2064         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2065             rval ? EXT_STATUS_MAILBOX : 0;
2066
2067         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2068         bsg_job->reply->result = DID_OK << 16;
2069         bsg_job->job_done(bsg_job);
2070         return 0;
2071 }
2072
2073 static int
2074 qla8044_serdes_op(struct fc_bsg_job *bsg_job)
2075 {
2076         struct Scsi_Host *host = bsg_job->shost;
2077         scsi_qla_host_t *vha = shost_priv(host);
2078         int rval = 0;
2079         struct qla_serdes_reg_ex sr;
2080
2081         memset(&sr, 0, sizeof(sr));
2082
2083         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2084             bsg_job->request_payload.sg_cnt, &sr, sizeof(sr));
2085
2086         switch (sr.cmd) {
2087         case INT_SC_SERDES_WRITE_REG:
2088                 rval = qla8044_write_serdes_word(vha, sr.addr, sr.val);
2089                 bsg_job->reply->reply_payload_rcv_len = 0;
2090                 break;
2091         case INT_SC_SERDES_READ_REG:
2092                 rval = qla8044_read_serdes_word(vha, sr.addr, &sr.val);
2093                 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2094                     bsg_job->reply_payload.sg_cnt, &sr, sizeof(sr));
2095                 bsg_job->reply->reply_payload_rcv_len = sizeof(sr);
2096                 break;
2097         default:
2098                 ql_dbg(ql_dbg_user, vha, 0x70cf,
2099                     "Unknown serdes cmd %x.\n", sr.cmd);
2100                 rval = -EINVAL;
2101                 break;
2102         }
2103
2104         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2105             rval ? EXT_STATUS_MAILBOX : 0;
2106
2107         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2108         bsg_job->reply->result = DID_OK << 16;
2109         bsg_job->job_done(bsg_job);
2110         return 0;
2111 }
2112
2113 static int
2114 qla27xx_get_flash_upd_cap(struct fc_bsg_job *bsg_job)
2115 {
2116         struct Scsi_Host *host = bsg_job->shost;
2117         scsi_qla_host_t *vha = shost_priv(host);
2118         struct qla_hw_data *ha = vha->hw;
2119         struct qla_flash_update_caps cap;
2120
2121         if (!(IS_QLA27XX(ha)))
2122                 return -EPERM;
2123
2124         memset(&cap, 0, sizeof(cap));
2125         cap.capabilities = (uint64_t)ha->fw_attributes_ext[1] << 48 |
2126                            (uint64_t)ha->fw_attributes_ext[0] << 32 |
2127                            (uint64_t)ha->fw_attributes_h << 16 |
2128                            (uint64_t)ha->fw_attributes;
2129
2130         sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2131             bsg_job->reply_payload.sg_cnt, &cap, sizeof(cap));
2132         bsg_job->reply->reply_payload_rcv_len = sizeof(cap);
2133
2134         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2135             EXT_STATUS_OK;
2136
2137         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2138         bsg_job->reply->result = DID_OK << 16;
2139         bsg_job->job_done(bsg_job);
2140         return 0;
2141 }
2142
2143 static int
2144 qla27xx_set_flash_upd_cap(struct fc_bsg_job *bsg_job)
2145 {
2146         struct Scsi_Host *host = bsg_job->shost;
2147         scsi_qla_host_t *vha = shost_priv(host);
2148         struct qla_hw_data *ha = vha->hw;
2149         uint64_t online_fw_attr = 0;
2150         struct qla_flash_update_caps cap;
2151
2152         if (!(IS_QLA27XX(ha)))
2153                 return -EPERM;
2154
2155         memset(&cap, 0, sizeof(cap));
2156         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2157             bsg_job->request_payload.sg_cnt, &cap, sizeof(cap));
2158
2159         online_fw_attr = (uint64_t)ha->fw_attributes_ext[1] << 48 |
2160                          (uint64_t)ha->fw_attributes_ext[0] << 32 |
2161                          (uint64_t)ha->fw_attributes_h << 16 |
2162                          (uint64_t)ha->fw_attributes;
2163
2164         if (online_fw_attr != cap.capabilities) {
2165                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2166                     EXT_STATUS_INVALID_PARAM;
2167                 return -EINVAL;
2168         }
2169
2170         if (cap.outage_duration < MAX_LOOP_TIMEOUT)  {
2171                 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2172                     EXT_STATUS_INVALID_PARAM;
2173                 return -EINVAL;
2174         }
2175
2176         bsg_job->reply->reply_payload_rcv_len = 0;
2177
2178         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2179             EXT_STATUS_OK;
2180
2181         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2182         bsg_job->reply->result = DID_OK << 16;
2183         bsg_job->job_done(bsg_job);
2184         return 0;
2185 }
2186
2187 static int
2188 qla27xx_get_bbcr_data(struct fc_bsg_job *bsg_job)
2189 {
2190         struct Scsi_Host *host = bsg_job->shost;
2191         scsi_qla_host_t *vha = shost_priv(host);
2192         struct qla_hw_data *ha = vha->hw;
2193         struct qla_bbcr_data bbcr;
2194         uint16_t loop_id, topo, sw_cap;
2195         uint8_t domain, area, al_pa, state;
2196         int rval;
2197
2198         if (!(IS_QLA27XX(ha)))
2199                 return -EPERM;
2200
2201         memset(&bbcr, 0, sizeof(bbcr));
2202
2203         if (vha->flags.bbcr_enable)
2204                 bbcr.status = QLA_BBCR_STATUS_ENABLED;
2205         else
2206                 bbcr.status = QLA_BBCR_STATUS_DISABLED;
2207
2208         if (bbcr.status == QLA_BBCR_STATUS_ENABLED) {
2209                 rval = qla2x00_get_adapter_id(vha, &loop_id, &al_pa,
2210                         &area, &domain, &topo, &sw_cap);
2211                 if (rval != QLA_SUCCESS) {
2212                         bbcr.status = QLA_BBCR_STATUS_UNKNOWN;
2213                         bbcr.state = QLA_BBCR_STATE_OFFLINE;
2214                         bbcr.mbx1 = loop_id;
2215                         goto done;
2216                 }
2217
2218                 state = (vha->bbcr >> 12) & 0x1;
2219
2220                 if (state) {
2221                         bbcr.state = QLA_BBCR_STATE_OFFLINE;
2222                         bbcr.offline_reason_code = QLA_BBCR_REASON_LOGIN_REJECT;
2223                 } else {
2224                         bbcr.state = QLA_BBCR_STATE_ONLINE;
2225                         bbcr.negotiated_bbscn = (vha->bbcr >> 8) & 0xf;
2226                 }
2227
2228                 bbcr.configured_bbscn = vha->bbcr & 0xf;
2229         }
2230
2231 done:
2232         sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2233                 bsg_job->reply_payload.sg_cnt, &bbcr, sizeof(bbcr));
2234         bsg_job->reply->reply_payload_rcv_len = sizeof(bbcr);
2235
2236         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = EXT_STATUS_OK;
2237
2238         bsg_job->reply_len = sizeof(struct fc_bsg_reply);
2239         bsg_job->reply->result = DID_OK << 16;
2240         bsg_job->job_done(bsg_job);
2241         return 0;
2242 }
2243
2244 static int
2245 qla2x00_get_priv_stats(struct fc_bsg_job *bsg_job)
2246 {
2247         struct Scsi_Host *host = bsg_job->shost;
2248         scsi_qla_host_t *vha = shost_priv(host);
2249         struct qla_hw_data *ha = vha->hw;
2250         struct scsi_qla_host *base_vha = pci_get_drvdata(ha->pdev);
2251         struct link_statistics *stats = NULL;
2252         dma_addr_t stats_dma;
2253         int rval;
2254         uint32_t *cmd = bsg_job->request->rqst_data.h_vendor.vendor_cmd;
2255         uint options = cmd[0] == QL_VND_GET_PRIV_STATS_EX ? cmd[1] : 0;
2256
2257         if (test_bit(UNLOADING, &vha->dpc_flags))
2258                 return -ENODEV;
2259
2260         if (unlikely(pci_channel_offline(ha->pdev)))
2261                 return -ENODEV;
2262
2263         if (qla2x00_reset_active(vha))
2264                 return -EBUSY;
2265
2266         if (!IS_FWI2_CAPABLE(ha))
2267                 return -EPERM;
2268
2269         stats = dma_alloc_coherent(&ha->pdev->dev,
2270                 sizeof(*stats), &stats_dma, GFP_KERNEL);
2271         if (!stats) {
2272                 ql_log(ql_log_warn, vha, 0x70e2,
2273                     "Failed to allocate memory for stats.\n");
2274                 return -ENOMEM;
2275         }
2276
2277         memset(stats, 0, sizeof(*stats));
2278
2279         rval = qla24xx_get_isp_stats(base_vha, stats, stats_dma, options);
2280
2281         if (rval == QLA_SUCCESS) {
2282                 ql_dump_buffer(ql_dbg_user + ql_dbg_verbose, vha, 0x70e3,
2283                     (uint8_t *)stats, sizeof(*stats));
2284                 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2285                         bsg_job->reply_payload.sg_cnt, stats, sizeof(*stats));
2286         }
2287
2288         bsg_job->reply->reply_payload_rcv_len = sizeof(*stats);
2289         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2290             rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
2291
2292         bsg_job->reply_len = sizeof(*bsg_job->reply);
2293         bsg_job->reply->result = DID_OK << 16;
2294         bsg_job->job_done(bsg_job);
2295
2296         dma_free_coherent(&ha->pdev->dev, sizeof(*stats),
2297                 stats, stats_dma);
2298
2299         return 0;
2300 }
2301
2302 static int
2303 qla2x00_do_dport_diagnostics(struct fc_bsg_job *bsg_job)
2304 {
2305         struct Scsi_Host *host = bsg_job->shost;
2306         scsi_qla_host_t *vha = shost_priv(host);
2307         int rval;
2308         struct qla_dport_diag *dd;
2309
2310         if (!IS_QLA83XX(vha->hw) && !IS_QLA27XX(vha->hw))
2311                 return -EPERM;
2312
2313         dd = kmalloc(sizeof(*dd), GFP_KERNEL);
2314         if (!dd) {
2315                 ql_log(ql_log_warn, vha, 0x70db,
2316                     "Failed to allocate memory for dport.\n");
2317                 return -ENOMEM;
2318         }
2319
2320         sg_copy_to_buffer(bsg_job->request_payload.sg_list,
2321             bsg_job->request_payload.sg_cnt, dd, sizeof(*dd));
2322
2323         rval = qla26xx_dport_diagnostics(
2324             vha, dd->buf, sizeof(dd->buf), dd->options);
2325         if (rval == QLA_SUCCESS) {
2326                 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
2327                     bsg_job->reply_payload.sg_cnt, dd, sizeof(*dd));
2328         }
2329
2330         bsg_job->reply->reply_payload_rcv_len = sizeof(*dd);
2331         bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
2332             rval ? EXT_STATUS_MAILBOX : EXT_STATUS_OK;
2333
2334         bsg_job->reply_len = sizeof(*bsg_job->reply);
2335         bsg_job->reply->result = DID_OK << 16;
2336         bsg_job->job_done(bsg_job);
2337
2338         kfree(dd);
2339
2340         return 0;
2341 }
2342
2343 static int
2344 qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
2345 {
2346         switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
2347         case QL_VND_LOOPBACK:
2348                 return qla2x00_process_loopback(bsg_job);
2349
2350         case QL_VND_A84_RESET:
2351                 return qla84xx_reset(bsg_job);
2352
2353         case QL_VND_A84_UPDATE_FW:
2354                 return qla84xx_updatefw(bsg_job);
2355
2356         case QL_VND_A84_MGMT_CMD:
2357                 return qla84xx_mgmt_cmd(bsg_job);
2358
2359         case QL_VND_IIDMA:
2360                 return qla24xx_iidma(bsg_job);
2361
2362         case QL_VND_FCP_PRIO_CFG_CMD:
2363                 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
2364
2365         case QL_VND_READ_FLASH:
2366                 return qla2x00_read_optrom(bsg_job);
2367
2368         case QL_VND_UPDATE_FLASH:
2369                 return qla2x00_update_optrom(bsg_job);
2370
2371         case QL_VND_SET_FRU_VERSION:
2372                 return qla2x00_update_fru_versions(bsg_job);
2373
2374         case QL_VND_READ_FRU_STATUS:
2375                 return qla2x00_read_fru_status(bsg_job);
2376
2377         case QL_VND_WRITE_FRU_STATUS:
2378                 return qla2x00_write_fru_status(bsg_job);
2379
2380         case QL_VND_WRITE_I2C:
2381                 return qla2x00_write_i2c(bsg_job);
2382
2383         case QL_VND_READ_I2C:
2384                 return qla2x00_read_i2c(bsg_job);
2385
2386         case QL_VND_DIAG_IO_CMD:
2387                 return qla24xx_process_bidir_cmd(bsg_job);
2388
2389         case QL_VND_FX00_MGMT_CMD:
2390                 return qlafx00_mgmt_cmd(bsg_job);
2391
2392         case QL_VND_SERDES_OP:
2393                 return qla26xx_serdes_op(bsg_job);
2394
2395         case QL_VND_SERDES_OP_EX:
2396                 return qla8044_serdes_op(bsg_job);
2397
2398         case QL_VND_GET_FLASH_UPDATE_CAPS:
2399                 return qla27xx_get_flash_upd_cap(bsg_job);
2400
2401         case QL_VND_SET_FLASH_UPDATE_CAPS:
2402                 return qla27xx_set_flash_upd_cap(bsg_job);
2403
2404         case QL_VND_GET_BBCR_DATA:
2405                 return qla27xx_get_bbcr_data(bsg_job);
2406
2407         case QL_VND_GET_PRIV_STATS:
2408         case QL_VND_GET_PRIV_STATS_EX:
2409                 return qla2x00_get_priv_stats(bsg_job);
2410
2411         case QL_VND_DPORT_DIAGNOSTICS:
2412                 return qla2x00_do_dport_diagnostics(bsg_job);
2413
2414         default:
2415                 return -ENOSYS;
2416         }
2417 }
2418
2419 int
2420 qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
2421 {
2422         int ret = -EINVAL;
2423         struct fc_rport *rport;
2424         struct Scsi_Host *host;
2425         scsi_qla_host_t *vha;
2426
2427         /* In case no data transferred. */
2428         bsg_job->reply->reply_payload_rcv_len = 0;
2429
2430         if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
2431                 rport = bsg_job->rport;
2432                 host = rport_to_shost(rport);
2433                 vha = shost_priv(host);
2434         } else {
2435                 host = bsg_job->shost;
2436                 vha = shost_priv(host);
2437         }
2438
2439         if (qla2x00_reset_active(vha)) {
2440                 ql_dbg(ql_dbg_user, vha, 0x709f,
2441                     "BSG: ISP abort active/needed -- cmd=%d.\n",
2442                     bsg_job->request->msgcode);
2443                 return -EBUSY;
2444         }
2445
2446         ql_dbg(ql_dbg_user, vha, 0x7000,
2447             "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
2448
2449         switch (bsg_job->request->msgcode) {
2450         case FC_BSG_RPT_ELS:
2451         case FC_BSG_HST_ELS_NOLOGIN:
2452                 ret = qla2x00_process_els(bsg_job);
2453                 break;
2454         case FC_BSG_HST_CT:
2455                 ret = qla2x00_process_ct(bsg_job);
2456                 break;
2457         case FC_BSG_HST_VENDOR:
2458                 ret = qla2x00_process_vendor_specific(bsg_job);
2459                 break;
2460         case FC_BSG_HST_ADD_RPORT:
2461         case FC_BSG_HST_DEL_RPORT:
2462         case FC_BSG_RPT_CT:
2463         default:
2464                 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
2465                 break;
2466         }
2467         return ret;
2468 }
2469
2470 int
2471 qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
2472 {
2473         scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
2474         struct qla_hw_data *ha = vha->hw;
2475         srb_t *sp;
2476         int cnt, que;
2477         unsigned long flags;
2478         struct req_que *req;
2479
2480         /* find the bsg job from the active list of commands */
2481         spin_lock_irqsave(&ha->hardware_lock, flags);
2482         for (que = 0; que < ha->max_req_queues; que++) {
2483                 req = ha->req_q_map[que];
2484                 if (!req)
2485                         continue;
2486
2487                 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
2488                         sp = req->outstanding_cmds[cnt];
2489                         if (sp) {
2490                                 if (((sp->type == SRB_CT_CMD) ||
2491                                         (sp->type == SRB_ELS_CMD_HST) ||
2492                                         (sp->type == SRB_FXIOCB_BCMD))
2493                                         && (sp->u.bsg_job == bsg_job)) {
2494                                         req->outstanding_cmds[cnt] = NULL;
2495                                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2496                                         if (ha->isp_ops->abort_command(sp)) {
2497                                                 ql_log(ql_log_warn, vha, 0x7089,
2498                                                     "mbx abort_command "
2499                                                     "failed.\n");
2500                                                 bsg_job->req->errors =
2501                                                 bsg_job->reply->result = -EIO;
2502                                         } else {
2503                                                 ql_dbg(ql_dbg_user, vha, 0x708a,
2504                                                     "mbx abort_command "
2505                                                     "success.\n");
2506                                                 bsg_job->req->errors =
2507                                                 bsg_job->reply->result = 0;
2508                                         }
2509                                         spin_lock_irqsave(&ha->hardware_lock, flags);
2510                                         goto done;
2511                                 }
2512                         }
2513                 }
2514         }
2515         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2516         ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
2517         bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
2518         return 0;
2519
2520 done:
2521         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2522         sp->free(vha, sp);
2523         return 0;
2524 }