blob: be299c83e07e9c6396242d9663568a8978073918 [file] [log] [blame]
Giridhar Malavali6e980162010-03-19 17:03:58 -07001/*
2 * QLogic Fibre Channel HBA Driver
Chad Dupuis46152ce2012-08-22 14:21:08 -04003 * Copyright (c) 2003-2012 QLogic Corporation
Giridhar Malavali6e980162010-03-19 17:03:58 -07004 *
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 */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080014void
15qla2x00_bsg_job_done(void *data, void *ptr, int res)
Giridhar Malavali6e980162010-03-19 17:03:58 -070016{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080017 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
26void
27qla2x00_bsg_sp_free(void *data, void *ptr)
28{
29 srb_t *sp = (srb_t *)ptr;
Chad Dupuisb00ee7d2013-02-08 01:57:50 -050030 struct scsi_qla_host *vha = sp->fcport->vha;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080031 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali6e980162010-03-19 17:03:58 -070032 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali6e980162010-03-19 17:03:58 -070033
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080034 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
35 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Giridhar Malavali6e980162010-03-19 17:03:58 -070036
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080037 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
38 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
39
40 if (sp->type == SRB_CT_CMD ||
41 sp->type == SRB_ELS_CMD_HST)
42 kfree(sp->fcport);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -050043 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -070044}
45
Sarang Radke09ff7012010-03-19 17:03:59 -070046int
Saurav Kashyap7c3df132011-07-14 12:00:13 -070047qla24xx_fcp_prio_cfg_valid(scsi_qla_host_t *vha,
48 struct qla_fcp_prio_cfg *pri_cfg, uint8_t flag)
Sarang Radke09ff7012010-03-19 17:03:59 -070049{
50 int i, ret, num_valid;
51 uint8_t *bcode;
52 struct qla_fcp_prio_entry *pri_entry;
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050053 uint32_t *bcode_val_ptr, bcode_val;
Sarang Radke09ff7012010-03-19 17:03:59 -070054
55 ret = 1;
56 num_valid = 0;
57 bcode = (uint8_t *)pri_cfg;
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050058 bcode_val_ptr = (uint32_t *)pri_cfg;
59 bcode_val = (uint32_t)(*bcode_val_ptr);
Sarang Radke09ff7012010-03-19 17:03:59 -070060
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050061 if (bcode_val == 0xFFFFFFFF) {
62 /* No FCP Priority config data in flash */
Saurav Kashyap7c3df132011-07-14 12:00:13 -070063 ql_dbg(ql_dbg_user, vha, 0x7051,
64 "No FCP Priority config data.\n");
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050065 return 0;
66 }
67
68 if (bcode[0] != 'H' || bcode[1] != 'Q' || bcode[2] != 'O' ||
69 bcode[3] != 'S') {
70 /* Invalid FCP priority data header*/
Saurav Kashyap7c3df132011-07-14 12:00:13 -070071 ql_dbg(ql_dbg_user, vha, 0x7052,
72 "Invalid FCP Priority data header. bcode=0x%x.\n",
73 bcode_val);
Sarang Radke09ff7012010-03-19 17:03:59 -070074 return 0;
75 }
76 if (flag != 1)
77 return ret;
78
79 pri_entry = &pri_cfg->entry[0];
80 for (i = 0; i < pri_cfg->num_entries; i++) {
81 if (pri_entry->flags & FCP_PRIO_ENTRY_TAG_VALID)
82 num_valid++;
83 pri_entry++;
84 }
85
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050086 if (num_valid == 0) {
87 /* No valid FCP priority data entries */
Saurav Kashyap7c3df132011-07-14 12:00:13 -070088 ql_dbg(ql_dbg_user, vha, 0x7053,
89 "No valid FCP Priority data entries.\n");
Sarang Radke09ff7012010-03-19 17:03:59 -070090 ret = 0;
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050091 } else {
92 /* FCP priority data is valid */
Saurav Kashyap7c3df132011-07-14 12:00:13 -070093 ql_dbg(ql_dbg_user, vha, 0x7054,
94 "Valid FCP priority data. num entries = %d.\n",
95 num_valid);
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +050096 }
Sarang Radke09ff7012010-03-19 17:03:59 -070097
98 return ret;
99}
100
101static int
102qla24xx_proc_fcp_prio_cfg_cmd(struct fc_bsg_job *bsg_job)
103{
104 struct Scsi_Host *host = bsg_job->shost;
105 scsi_qla_host_t *vha = shost_priv(host);
106 struct qla_hw_data *ha = vha->hw;
107 int ret = 0;
108 uint32_t len;
109 uint32_t oper;
110
Saurav Kashyapa00f6292011-11-18 09:03:19 -0800111 if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA82XX(ha))) {
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500112 ret = -EINVAL;
113 goto exit_fcp_prio_cfg;
114 }
115
Sarang Radke09ff7012010-03-19 17:03:59 -0700116 /* Get the sub command */
117 oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
118
119 /* Only set config is allowed if config memory is not allocated */
120 if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
121 ret = -EINVAL;
122 goto exit_fcp_prio_cfg;
123 }
124 switch (oper) {
125 case QLFC_FCP_PRIO_DISABLE:
126 if (ha->flags.fcp_prio_enabled) {
127 ha->flags.fcp_prio_enabled = 0;
128 ha->fcp_prio_cfg->attributes &=
129 ~FCP_PRIO_ATTR_ENABLE;
130 qla24xx_update_all_fcp_prio(vha);
131 bsg_job->reply->result = DID_OK;
132 } else {
133 ret = -EINVAL;
134 bsg_job->reply->result = (DID_ERROR << 16);
135 goto exit_fcp_prio_cfg;
136 }
137 break;
138
139 case QLFC_FCP_PRIO_ENABLE:
140 if (!ha->flags.fcp_prio_enabled) {
141 if (ha->fcp_prio_cfg) {
142 ha->flags.fcp_prio_enabled = 1;
143 ha->fcp_prio_cfg->attributes |=
144 FCP_PRIO_ATTR_ENABLE;
145 qla24xx_update_all_fcp_prio(vha);
146 bsg_job->reply->result = DID_OK;
147 } else {
148 ret = -EINVAL;
149 bsg_job->reply->result = (DID_ERROR << 16);
150 goto exit_fcp_prio_cfg;
151 }
152 }
153 break;
154
155 case QLFC_FCP_PRIO_GET_CONFIG:
156 len = bsg_job->reply_payload.payload_len;
157 if (!len || len > FCP_PRIO_CFG_SIZE) {
158 ret = -EINVAL;
159 bsg_job->reply->result = (DID_ERROR << 16);
160 goto exit_fcp_prio_cfg;
161 }
162
163 bsg_job->reply->result = DID_OK;
164 bsg_job->reply->reply_payload_rcv_len =
165 sg_copy_from_buffer(
166 bsg_job->reply_payload.sg_list,
167 bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
168 len);
169
170 break;
171
172 case QLFC_FCP_PRIO_SET_CONFIG:
173 len = bsg_job->request_payload.payload_len;
174 if (!len || len > FCP_PRIO_CFG_SIZE) {
175 bsg_job->reply->result = (DID_ERROR << 16);
176 ret = -EINVAL;
177 goto exit_fcp_prio_cfg;
178 }
179
180 if (!ha->fcp_prio_cfg) {
181 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
182 if (!ha->fcp_prio_cfg) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700183 ql_log(ql_log_warn, vha, 0x7050,
184 "Unable to allocate memory for fcp prio "
185 "config data (%x).\n", FCP_PRIO_CFG_SIZE);
Sarang Radke09ff7012010-03-19 17:03:59 -0700186 bsg_job->reply->result = (DID_ERROR << 16);
187 ret = -ENOMEM;
188 goto exit_fcp_prio_cfg;
189 }
190 }
191
192 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
193 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
194 bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
195 FCP_PRIO_CFG_SIZE);
196
197 /* validate fcp priority data */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700198
199 if (!qla24xx_fcp_prio_cfg_valid(vha,
200 (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
Sarang Radke09ff7012010-03-19 17:03:59 -0700201 bsg_job->reply->result = (DID_ERROR << 16);
202 ret = -EINVAL;
203 /* If buffer was invalidatic int
204 * fcp_prio_cfg is of no use
205 */
206 vfree(ha->fcp_prio_cfg);
207 ha->fcp_prio_cfg = NULL;
208 goto exit_fcp_prio_cfg;
209 }
210
211 ha->flags.fcp_prio_enabled = 0;
212 if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
213 ha->flags.fcp_prio_enabled = 1;
214 qla24xx_update_all_fcp_prio(vha);
215 bsg_job->reply->result = DID_OK;
216 break;
217 default:
218 ret = -EINVAL;
219 break;
220 }
221exit_fcp_prio_cfg:
Armen Baloyan63ea9232012-11-21 02:39:53 -0500222 if (!ret)
223 bsg_job->job_done(bsg_job);
Sarang Radke09ff7012010-03-19 17:03:59 -0700224 return ret;
225}
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800226
Giridhar Malavali6e980162010-03-19 17:03:58 -0700227static int
228qla2x00_process_els(struct fc_bsg_job *bsg_job)
229{
230 struct fc_rport *rport;
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500231 fc_port_t *fcport = NULL;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700232 struct Scsi_Host *host;
233 scsi_qla_host_t *vha;
234 struct qla_hw_data *ha;
235 srb_t *sp;
236 const char *type;
237 int req_sg_cnt, rsp_sg_cnt;
238 int rval = (DRIVER_ERROR << 16);
239 uint16_t nextlid = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700240
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500241 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
242 rport = bsg_job->rport;
243 fcport = *(fc_port_t **) rport->dd_data;
244 host = rport_to_shost(rport);
245 vha = shost_priv(host);
246 ha = vha->hw;
247 type = "FC_BSG_RPT_ELS";
248 } else {
249 host = bsg_job->shost;
250 vha = shost_priv(host);
251 ha = vha->hw;
252 type = "FC_BSG_HST_ELS_NOLOGIN";
253 }
254
255 /* pass through is supported only for ISP 4Gb or higher */
256 if (!IS_FWI2_CAPABLE(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700257 ql_dbg(ql_dbg_user, vha, 0x7001,
258 "ELS passthru not supported for ISP23xx based adapters.\n");
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500259 rval = -EPERM;
260 goto done;
261 }
262
Giridhar Malavali6e980162010-03-19 17:03:58 -0700263 /* Multiple SG's are not supported for ELS requests */
264 if (bsg_job->request_payload.sg_cnt > 1 ||
265 bsg_job->reply_payload.sg_cnt > 1) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700266 ql_dbg(ql_dbg_user, vha, 0x7002,
267 "Multiple SG's are not suppored for ELS requests, "
268 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
269 bsg_job->request_payload.sg_cnt,
270 bsg_job->reply_payload.sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700271 rval = -EPERM;
272 goto done;
273 }
274
275 /* ELS request for rport */
276 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700277 /* make sure the rport is logged in,
278 * if not perform fabric login
279 */
280 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700281 ql_dbg(ql_dbg_user, vha, 0x7003,
282 "Failed to login port %06X for ELS passthru.\n",
283 fcport->d_id.b24);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700284 rval = -EIO;
285 goto done;
286 }
287 } else {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700288 /* Allocate a dummy fcport structure, since functions
289 * preparing the IOCB and mailbox command retrieves port
290 * specific information from fcport structure. For Host based
291 * ELS commands there will be no fcport structure allocated
292 */
293 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
294 if (!fcport) {
295 rval = -ENOMEM;
296 goto done;
297 }
298
299 /* Initialize all required fields of fcport */
300 fcport->vha = vha;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700301 fcport->d_id.b.al_pa =
302 bsg_job->request->rqst_data.h_els.port_id[0];
303 fcport->d_id.b.area =
304 bsg_job->request->rqst_data.h_els.port_id[1];
305 fcport->d_id.b.domain =
306 bsg_job->request->rqst_data.h_els.port_id[2];
307 fcport->loop_id =
308 (fcport->d_id.b.al_pa == 0xFD) ?
309 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
310 }
311
312 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700313 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700314 rval = -EIO;
315 goto done;
316 }
317
318 req_sg_cnt =
319 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
320 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
321 if (!req_sg_cnt) {
322 rval = -ENOMEM;
323 goto done_free_fcport;
324 }
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700325
326 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
327 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700328 if (!rsp_sg_cnt) {
329 rval = -ENOMEM;
330 goto done_free_fcport;
331 }
332
333 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700334 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700335 ql_log(ql_log_warn, vha, 0x7008,
336 "dma mapping resulted in different sg counts, "
337 "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
338 "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
339 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700340 rval = -EAGAIN;
341 goto done_unmap_sg;
342 }
343
344 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800345 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700346 if (!sp) {
347 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700348 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700349 }
350
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800351 sp->type =
Giridhar Malavali6e980162010-03-19 17:03:58 -0700352 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
353 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800354 sp->name =
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700355 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
356 "bsg_els_rpt" : "bsg_els_hst");
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800357 sp->u.bsg_job = bsg_job;
358 sp->free = qla2x00_bsg_sp_free;
359 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700360
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700361 ql_dbg(ql_dbg_user, vha, 0x700a,
362 "bsg rqst type: %s els type: %x - loop-id=%x "
363 "portid=%-2x%02x%02x.\n", type,
364 bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
365 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700366
367 rval = qla2x00_start_sp(sp);
368 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700369 ql_log(ql_log_warn, vha, 0x700e,
370 "qla2x00_start_sp failed = %d\n", rval);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -0500371 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700372 rval = -EIO;
373 goto done_unmap_sg;
374 }
375 return rval;
376
377done_unmap_sg:
378 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
379 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
380 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
381 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
382 goto done_free_fcport;
383
384done_free_fcport:
385 if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
386 kfree(fcport);
387done:
388 return rval;
389}
390
Andrew Vasquez57807902011-11-18 09:03:20 -0800391inline uint16_t
392qla24xx_calc_ct_iocbs(uint16_t dsds)
393{
394 uint16_t iocbs;
395
396 iocbs = 1;
397 if (dsds > 2) {
398 iocbs += (dsds - 2) / 5;
399 if ((dsds - 2) % 5)
400 iocbs++;
401 }
402 return iocbs;
403}
404
Giridhar Malavali6e980162010-03-19 17:03:58 -0700405static int
406qla2x00_process_ct(struct fc_bsg_job *bsg_job)
407{
408 srb_t *sp;
409 struct Scsi_Host *host = bsg_job->shost;
410 scsi_qla_host_t *vha = shost_priv(host);
411 struct qla_hw_data *ha = vha->hw;
412 int rval = (DRIVER_ERROR << 16);
413 int req_sg_cnt, rsp_sg_cnt;
414 uint16_t loop_id;
415 struct fc_port *fcport;
416 char *type = "FC_BSG_HST_CT";
Giridhar Malavali6e980162010-03-19 17:03:58 -0700417
Giridhar Malavali6e980162010-03-19 17:03:58 -0700418 req_sg_cnt =
419 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
420 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700421 if (!req_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700422 ql_log(ql_log_warn, vha, 0x700f,
423 "dma_map_sg return %d for request\n", req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700424 rval = -ENOMEM;
425 goto done;
426 }
427
428 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
429 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
430 if (!rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700431 ql_log(ql_log_warn, vha, 0x7010,
432 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700433 rval = -ENOMEM;
434 goto done;
435 }
436
437 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700438 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700439 ql_log(ql_log_warn, vha, 0x7011,
440 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
441 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
442 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700443 rval = -EAGAIN;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700444 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700445 }
446
447 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700448 ql_log(ql_log_warn, vha, 0x7012,
449 "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700450 rval = -EIO;
451 goto done_unmap_sg;
452 }
453
454 loop_id =
455 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
456 >> 24;
457 switch (loop_id) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700458 case 0xFC:
459 loop_id = cpu_to_le16(NPH_SNS);
460 break;
461 case 0xFA:
462 loop_id = vha->mgmt_svr_loop_id;
463 break;
464 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700465 ql_dbg(ql_dbg_user, vha, 0x7013,
466 "Unknown loop id: %x.\n", loop_id);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700467 rval = -EINVAL;
468 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700469 }
470
471 /* Allocate a dummy fcport structure, since functions preparing the
472 * IOCB and mailbox command retrieves port specific information
473 * from fcport structure. For Host based ELS commands there will be
474 * no fcport structure allocated
475 */
476 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700477 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700478 ql_log(ql_log_warn, vha, 0x7014,
479 "Failed to allocate fcport.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700480 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700481 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700482 }
483
484 /* Initialize all required fields of fcport */
485 fcport->vha = vha;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700486 fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
487 fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
488 fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
489 fcport->loop_id = loop_id;
490
491 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800492 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700493 if (!sp) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700494 ql_log(ql_log_warn, vha, 0x7015,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800495 "qla2x00_get_sp failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700496 rval = -ENOMEM;
497 goto done_free_fcport;
498 }
499
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800500 sp->type = SRB_CT_CMD;
501 sp->name = "bsg_ct";
502 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
503 sp->u.bsg_job = bsg_job;
504 sp->free = qla2x00_bsg_sp_free;
505 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700506
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700507 ql_dbg(ql_dbg_user, vha, 0x7016,
508 "bsg rqst type: %s else type: %x - "
509 "loop-id=%x portid=%02x%02x%02x.\n", type,
510 (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
511 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
512 fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700513
514 rval = qla2x00_start_sp(sp);
515 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700516 ql_log(ql_log_warn, vha, 0x7017,
517 "qla2x00_start_sp failed=%d.\n", rval);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -0500518 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700519 rval = -EIO;
520 goto done_free_fcport;
521 }
522 return rval;
523
524done_free_fcport:
525 kfree(fcport);
526done_unmap_sg:
527 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
528 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
529 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
530 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
531done:
532 return rval;
533}
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700534
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400535/* Disable loopback mode */
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700536static inline int
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400537qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
Chad Dupuis67b2a312013-02-08 01:57:51 -0500538 int wait)
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700539{
540 int ret = 0;
541 int rval = 0;
542 uint16_t new_config[4];
543 struct qla_hw_data *ha = vha->hw;
544
Nigel Kirklandf863f602012-05-15 14:34:19 -0400545 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700546 goto done_reset_internal;
547
548 memset(new_config, 0 , sizeof(new_config));
549 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400550 ENABLE_INTERNAL_LOOPBACK ||
551 (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
552 ENABLE_EXTERNAL_LOOPBACK) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700553 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400554 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
555 (new_config[0] & INTERNAL_LOOPBACK_MASK));
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700556 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
557
558 ha->notify_dcbx_comp = wait;
559 ret = qla81xx_set_port_config(vha, new_config);
560 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700561 ql_log(ql_log_warn, vha, 0x7025,
562 "Set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700563 ha->notify_dcbx_comp = 0;
564 rval = -EINVAL;
565 goto done_reset_internal;
566 }
567
568 /* Wait for DCBX complete event */
569 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
570 (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700571 ql_dbg(ql_dbg_user, vha, 0x7026,
572 "State change notification not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700573 ha->notify_dcbx_comp = 0;
574 rval = -EINVAL;
575 goto done_reset_internal;
576 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700577 ql_dbg(ql_dbg_user, vha, 0x7027,
578 "State change received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700579
580 ha->notify_dcbx_comp = 0;
581 }
582done_reset_internal:
583 return rval;
584}
585
Chad Dupuis67b2a312013-02-08 01:57:51 -0500586/*
587 * Set the port configuration to enable the internal or external loopback
588 * depending on the loopback mode.
589 */
590static inline int
591qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
592 uint16_t *new_config, uint16_t mode)
593{
594 int ret = 0;
595 int rval = 0;
596 struct qla_hw_data *ha = vha->hw;
597
598 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
599 goto done_set_internal;
600
601 if (mode == INTERNAL_LOOPBACK)
602 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
603 else if (mode == EXTERNAL_LOOPBACK)
604 new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
605 ql_dbg(ql_dbg_user, vha, 0x70be,
606 "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
607
608 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
609
610 ha->notify_dcbx_comp = 1;
611 ret = qla81xx_set_port_config(vha, new_config);
612 if (ret != QLA_SUCCESS) {
613 ql_log(ql_log_warn, vha, 0x7021,
614 "set port config failed.\n");
615 ha->notify_dcbx_comp = 0;
616 rval = -EINVAL;
617 goto done_set_internal;
618 }
619
620 /* Wait for DCBX complete event */
621 if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
622 ql_dbg(ql_dbg_user, vha, 0x7022,
623 "State change notification not received.\n");
624 ret = qla81xx_reset_loopback_mode(vha, new_config, 0);
625 /*
626 * If the reset of the loopback mode doesn't work take a FCoE
627 * dump and reset the chip.
628 */
629 if (ret) {
630 ha->isp_ops->fw_dump(vha, 0);
631 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
632 }
633 rval = -EINVAL;
634 } else {
635 if (ha->flags.idc_compl_status) {
636 ql_dbg(ql_dbg_user, vha, 0x70c3,
637 "Bad status in IDC Completion AEN\n");
638 rval = -EINVAL;
639 ha->flags.idc_compl_status = 0;
640 } else
641 ql_dbg(ql_dbg_user, vha, 0x7023,
642 "State change received.\n");
643 }
644
645 ha->notify_dcbx_comp = 0;
646
647done_set_internal:
648 return rval;
649}
650
Giridhar Malavali6e980162010-03-19 17:03:58 -0700651static int
652qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
653{
654 struct Scsi_Host *host = bsg_job->shost;
655 scsi_qla_host_t *vha = shost_priv(host);
656 struct qla_hw_data *ha = vha->hw;
657 int rval;
658 uint8_t command_sent;
659 char *type;
660 struct msg_echo_lb elreq;
661 uint16_t response[MAILBOX_REGISTER_COUNT];
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700662 uint16_t config[4], new_config[4];
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700663 uint8_t *fw_sts_ptr;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700664 uint8_t *req_data = NULL;
665 dma_addr_t req_data_dma;
666 uint32_t req_data_len;
667 uint8_t *rsp_data = NULL;
668 dma_addr_t rsp_data_dma;
669 uint32_t rsp_data_len;
670
Giridhar Malavali6e980162010-03-19 17:03:58 -0700671 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700672 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700673 return -EIO;
674 }
675
676 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
677 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
678 DMA_TO_DEVICE);
679
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700680 if (!elreq.req_sg_cnt) {
681 ql_log(ql_log_warn, vha, 0x701a,
682 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700683 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700684 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700685
686 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
687 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
688 DMA_FROM_DEVICE);
689
690 if (!elreq.rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700691 ql_log(ql_log_warn, vha, 0x701b,
692 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700693 rval = -ENOMEM;
694 goto done_unmap_req_sg;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700695 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700696
697 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
698 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700699 ql_log(ql_log_warn, vha, 0x701c,
700 "dma mapping resulted in different sg counts, "
701 "request_sg_cnt: %x dma_request_sg_cnt: %x "
702 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
703 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
704 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700705 rval = -EAGAIN;
706 goto done_unmap_sg;
707 }
708 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
709 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
710 &req_data_dma, GFP_KERNEL);
711 if (!req_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700712 ql_log(ql_log_warn, vha, 0x701d,
713 "dma alloc failed for req_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700714 rval = -ENOMEM;
715 goto done_unmap_sg;
716 }
717
718 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
719 &rsp_data_dma, GFP_KERNEL);
720 if (!rsp_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700721 ql_log(ql_log_warn, vha, 0x7004,
722 "dma alloc failed for rsp_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700723 rval = -ENOMEM;
724 goto done_free_dma_req;
725 }
726
727 /* Copy the request buffer in req_data now */
728 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
729 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
730
731 elreq.send_dma = req_data_dma;
732 elreq.rcv_dma = rsp_data_dma;
733 elreq.transfer_size = req_data_len;
734
735 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
736
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400737 if (atomic_read(&vha->loop_state) == LOOP_READY &&
738 (ha->current_topology == ISP_CFG_F ||
Nigel Kirklandf863f602012-05-15 14:34:19 -0400739 ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700740 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
741 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
742 elreq.options == EXTERNAL_LOOPBACK) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700743 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700744 ql_dbg(ql_dbg_user, vha, 0x701e,
745 "BSG request type: %s.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700746 command_sent = INT_DEF_LB_ECHO_CMD;
747 rval = qla2x00_echo_test(vha, &elreq, response);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700748 } else {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800749 if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700750 memset(config, 0, sizeof(config));
751 memset(new_config, 0, sizeof(new_config));
752 if (qla81xx_get_port_config(vha, config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700753 ql_log(ql_log_warn, vha, 0x701f,
754 "Get port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700755 rval = -EPERM;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500756 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700757 }
758
Chad Dupuis1bcc46c2013-02-08 01:57:46 -0500759 if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
760 ql_dbg(ql_dbg_user, vha, 0x70c4,
761 "Loopback operation already in "
762 "progress.\n");
763 rval = -EAGAIN;
764 goto done_free_dma_rsp;
765 }
766
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400767 ql_dbg(ql_dbg_user, vha, 0x70c0,
768 "elreq.options=%04x\n", elreq.options);
769
770 if (elreq.options == EXTERNAL_LOOPBACK)
771 if (IS_QLA8031(ha))
772 rval = qla81xx_set_loopback_mode(vha,
773 config, new_config, elreq.options);
774 else
775 rval = qla81xx_reset_loopback_mode(vha,
776 config, 1);
777 else
778 rval = qla81xx_set_loopback_mode(vha, config,
779 new_config, elreq.options);
780
781 if (rval) {
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400782 rval = -EPERM;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500783 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700784 }
785
786 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700787 ql_dbg(ql_dbg_user, vha, 0x7028,
788 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700789
790 command_sent = INT_DEF_LB_LOOPBACK_CMD;
791 rval = qla2x00_loopback_test(vha, &elreq, response);
792
Joe Carnuccio4052bd52010-12-21 16:00:17 -0800793 if (new_config[0]) {
Chad Dupuis67b2a312013-02-08 01:57:51 -0500794 int ret;
795
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700796 /* Revert back to original port config
797 * Also clear internal loopback
798 */
Chad Dupuis67b2a312013-02-08 01:57:51 -0500799 ret = qla81xx_reset_loopback_mode(vha,
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700800 new_config, 0);
Chad Dupuis67b2a312013-02-08 01:57:51 -0500801 if (ret) {
802 /*
803 * If the reset of the loopback mode
804 * doesn't work take FCoE dump and then
805 * reset the chip.
806 */
807 ha->isp_ops->fw_dump(vha, 0);
808 set_bit(ISP_ABORT_NEEDED,
809 &vha->dpc_flags);
810 }
811
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700812 }
813
814 if (response[0] == MBS_COMMAND_ERROR &&
815 response[1] == MBS_LB_RESET) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700816 ql_log(ql_log_warn, vha, 0x7029,
817 "MBX command error, Aborting ISP.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700818 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
819 qla2xxx_wake_dpc(vha);
820 qla2x00_wait_for_chip_reset(vha);
821 /* Also reset the MPI */
Saurav Kashyap619d5a02013-02-08 01:57:49 -0500822 if (IS_QLA81XX(ha)) {
823 if (qla81xx_restart_mpi_firmware(vha) !=
824 QLA_SUCCESS) {
825 ql_log(ql_log_warn, vha, 0x702a,
826 "MPI reset failed.\n");
827 }
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700828 }
829
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700830 rval = -EIO;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500831 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700832 }
833 } else {
834 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700835 ql_dbg(ql_dbg_user, vha, 0x702b,
836 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700837 command_sent = INT_DEF_LB_LOOPBACK_CMD;
838 rval = qla2x00_loopback_test(vha, &elreq, response);
839 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700840 }
841
842 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700843 ql_log(ql_log_warn, vha, 0x702c,
844 "Vendor request %s failed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700845
Giridhar Malavali6e980162010-03-19 17:03:58 -0700846 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700847 bsg_job->reply->result = (DID_ERROR << 16);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500848 bsg_job->reply->reply_payload_rcv_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700849 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700850 ql_dbg(ql_dbg_user, vha, 0x702d,
851 "Vendor request %s completed.\n", type);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500852 bsg_job->reply->result = (DID_OK << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700853 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
854 bsg_job->reply_payload.sg_cnt, rsp_data,
855 rsp_data_len);
856 }
Armen Baloyan63ea9232012-11-21 02:39:53 -0500857
858 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
859 sizeof(response) + sizeof(uint8_t);
860 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
861 sizeof(struct fc_bsg_reply);
862 memcpy(fw_sts_ptr, response, sizeof(response));
863 fw_sts_ptr += sizeof(response);
864 *fw_sts_ptr = command_sent;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700865
Steve Hodgson9bceab42012-11-21 02:39:56 -0500866done_free_dma_rsp:
Giridhar Malavali6e980162010-03-19 17:03:58 -0700867 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
868 rsp_data, rsp_data_dma);
869done_free_dma_req:
870 dma_free_coherent(&ha->pdev->dev, req_data_len,
871 req_data, req_data_dma);
872done_unmap_sg:
873 dma_unmap_sg(&ha->pdev->dev,
874 bsg_job->reply_payload.sg_list,
875 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
876done_unmap_req_sg:
877 dma_unmap_sg(&ha->pdev->dev,
878 bsg_job->request_payload.sg_list,
879 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500880 if (!rval)
881 bsg_job->job_done(bsg_job);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700882 return rval;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700883}
884
885static int
886qla84xx_reset(struct fc_bsg_job *bsg_job)
887{
888 struct Scsi_Host *host = bsg_job->shost;
889 scsi_qla_host_t *vha = shost_priv(host);
890 struct qla_hw_data *ha = vha->hw;
891 int rval = 0;
892 uint32_t flag;
893
Giridhar Malavali6e980162010-03-19 17:03:58 -0700894 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700895 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700896 return -EINVAL;
897 }
898
899 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
900
901 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
902
903 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700904 ql_log(ql_log_warn, vha, 0x7030,
905 "Vendor request 84xx reset failed.\n");
Armen Baloyan63ea9232012-11-21 02:39:53 -0500906 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700907
908 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700909 ql_dbg(ql_dbg_user, vha, 0x7031,
910 "Vendor request 84xx reset completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700911 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -0500912 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700913 }
914
Giridhar Malavali6e980162010-03-19 17:03:58 -0700915 return rval;
916}
917
918static int
919qla84xx_updatefw(struct fc_bsg_job *bsg_job)
920{
921 struct Scsi_Host *host = bsg_job->shost;
922 scsi_qla_host_t *vha = shost_priv(host);
923 struct qla_hw_data *ha = vha->hw;
924 struct verify_chip_entry_84xx *mn = NULL;
925 dma_addr_t mn_dma, fw_dma;
926 void *fw_buf = NULL;
927 int rval = 0;
928 uint32_t sg_cnt;
929 uint32_t data_len;
930 uint16_t options;
931 uint32_t flag;
932 uint32_t fw_ver;
933
Giridhar Malavali6e980162010-03-19 17:03:58 -0700934 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700935 ql_dbg(ql_dbg_user, vha, 0x7032,
936 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700937 return -EINVAL;
938 }
939
940 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
941 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700942 if (!sg_cnt) {
943 ql_log(ql_log_warn, vha, 0x7033,
944 "dma_map_sg returned %d for request.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700945 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700946 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700947
948 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700949 ql_log(ql_log_warn, vha, 0x7034,
950 "DMA mapping resulted in different sg counts, "
951 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
952 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700953 rval = -EAGAIN;
954 goto done_unmap_sg;
955 }
956
957 data_len = bsg_job->request_payload.payload_len;
958 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
959 &fw_dma, GFP_KERNEL);
960 if (!fw_buf) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700961 ql_log(ql_log_warn, vha, 0x7035,
962 "DMA alloc failed for fw_buf.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700963 rval = -ENOMEM;
964 goto done_unmap_sg;
965 }
966
967 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
968 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
969
970 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
971 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700972 ql_log(ql_log_warn, vha, 0x7036,
973 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700974 rval = -ENOMEM;
975 goto done_free_fw_buf;
976 }
977
978 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
979 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
980
981 memset(mn, 0, sizeof(struct access_chip_84xx));
982 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
983 mn->entry_count = 1;
984
985 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
986 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
987 options |= VCO_DIAG_FW;
988
989 mn->options = cpu_to_le16(options);
990 mn->fw_ver = cpu_to_le32(fw_ver);
991 mn->fw_size = cpu_to_le32(data_len);
992 mn->fw_seq_size = cpu_to_le32(data_len);
993 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
994 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
995 mn->dseg_length = cpu_to_le32(data_len);
996 mn->data_seg_cnt = cpu_to_le16(1);
997
998 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
999
1000 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001001 ql_log(ql_log_warn, vha, 0x7037,
1002 "Vendor request 84xx updatefw failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001003
Armen Baloyan63ea9232012-11-21 02:39:53 -05001004 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001005 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001006 ql_dbg(ql_dbg_user, vha, 0x7038,
1007 "Vendor request 84xx updatefw completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001008
1009 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1010 bsg_job->reply->result = DID_OK;
1011 }
1012
Giridhar Malavali6e980162010-03-19 17:03:58 -07001013 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1014
1015done_free_fw_buf:
1016 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
1017
1018done_unmap_sg:
1019 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1020 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1021
Armen Baloyan63ea9232012-11-21 02:39:53 -05001022 if (!rval)
1023 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001024 return rval;
1025}
1026
1027static int
1028qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1029{
1030 struct Scsi_Host *host = bsg_job->shost;
1031 scsi_qla_host_t *vha = shost_priv(host);
1032 struct qla_hw_data *ha = vha->hw;
1033 struct access_chip_84xx *mn = NULL;
1034 dma_addr_t mn_dma, mgmt_dma;
1035 void *mgmt_b = NULL;
1036 int rval = 0;
1037 struct qla_bsg_a84_mgmt *ql84_mgmt;
1038 uint32_t sg_cnt;
Harish Zunjarraod5459082010-03-19 17:04:00 -07001039 uint32_t data_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001040 uint32_t dma_direction = DMA_NONE;
1041
Giridhar Malavali6e980162010-03-19 17:03:58 -07001042 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001043 ql_log(ql_log_warn, vha, 0x703a,
1044 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001045 return -EINVAL;
1046 }
1047
1048 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1049 sizeof(struct fc_bsg_request));
1050 if (!ql84_mgmt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001051 ql_log(ql_log_warn, vha, 0x703b,
1052 "MGMT header not provided, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001053 return -EINVAL;
1054 }
1055
1056 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1057 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001058 ql_log(ql_log_warn, vha, 0x703c,
1059 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001060 return -ENOMEM;
1061 }
1062
1063 memset(mn, 0, sizeof(struct access_chip_84xx));
1064 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1065 mn->entry_count = 1;
1066
1067 switch (ql84_mgmt->mgmt.cmd) {
1068 case QLA84_MGMT_READ_MEM:
1069 case QLA84_MGMT_GET_INFO:
1070 sg_cnt = dma_map_sg(&ha->pdev->dev,
1071 bsg_job->reply_payload.sg_list,
1072 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1073 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001074 ql_log(ql_log_warn, vha, 0x703d,
1075 "dma_map_sg returned %d for reply.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001076 rval = -ENOMEM;
1077 goto exit_mgmt;
1078 }
1079
1080 dma_direction = DMA_FROM_DEVICE;
1081
1082 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001083 ql_log(ql_log_warn, vha, 0x703e,
1084 "DMA mapping resulted in different sg counts, "
1085 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1086 bsg_job->reply_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001087 rval = -EAGAIN;
1088 goto done_unmap_sg;
1089 }
1090
1091 data_len = bsg_job->reply_payload.payload_len;
1092
1093 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1094 &mgmt_dma, GFP_KERNEL);
1095 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001096 ql_log(ql_log_warn, vha, 0x703f,
1097 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001098 rval = -ENOMEM;
1099 goto done_unmap_sg;
1100 }
1101
1102 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1103 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1104 mn->parameter1 =
1105 cpu_to_le32(
1106 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1107
1108 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1109 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1110 mn->parameter1 =
1111 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1112
1113 mn->parameter2 =
1114 cpu_to_le32(
1115 ql84_mgmt->mgmt.mgmtp.u.info.context);
1116 }
1117 break;
1118
1119 case QLA84_MGMT_WRITE_MEM:
1120 sg_cnt = dma_map_sg(&ha->pdev->dev,
1121 bsg_job->request_payload.sg_list,
1122 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1123
1124 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001125 ql_log(ql_log_warn, vha, 0x7040,
1126 "dma_map_sg returned %d.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001127 rval = -ENOMEM;
1128 goto exit_mgmt;
1129 }
1130
1131 dma_direction = DMA_TO_DEVICE;
1132
1133 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001134 ql_log(ql_log_warn, vha, 0x7041,
1135 "DMA mapping resulted in different sg counts, "
1136 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1137 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001138 rval = -EAGAIN;
1139 goto done_unmap_sg;
1140 }
1141
1142 data_len = bsg_job->request_payload.payload_len;
1143 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1144 &mgmt_dma, GFP_KERNEL);
1145 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001146 ql_log(ql_log_warn, vha, 0x7042,
1147 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001148 rval = -ENOMEM;
1149 goto done_unmap_sg;
1150 }
1151
1152 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1153 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1154
1155 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1156 mn->parameter1 =
1157 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1158 break;
1159
1160 case QLA84_MGMT_CHNG_CONFIG:
1161 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1162 mn->parameter1 =
1163 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1164
1165 mn->parameter2 =
1166 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1167
1168 mn->parameter3 =
1169 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1170 break;
1171
1172 default:
1173 rval = -EIO;
1174 goto exit_mgmt;
1175 }
1176
1177 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1178 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1179 mn->dseg_count = cpu_to_le16(1);
1180 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1181 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1182 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1183 }
1184
1185 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1186
1187 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001188 ql_log(ql_log_warn, vha, 0x7043,
1189 "Vendor request 84xx mgmt failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001190
Armen Baloyan63ea9232012-11-21 02:39:53 -05001191 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001192
1193 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001194 ql_dbg(ql_dbg_user, vha, 0x7044,
1195 "Vendor request 84xx mgmt completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001196
1197 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1198 bsg_job->reply->result = DID_OK;
1199
1200 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1201 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1202 bsg_job->reply->reply_payload_rcv_len =
1203 bsg_job->reply_payload.payload_len;
1204
1205 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001206 bsg_job->reply_payload.sg_cnt, mgmt_b,
1207 data_len);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001208 }
1209 }
1210
Giridhar Malavali6e980162010-03-19 17:03:58 -07001211done_unmap_sg:
Harish Zunjarraod5459082010-03-19 17:04:00 -07001212 if (mgmt_b)
1213 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1214
Giridhar Malavali6e980162010-03-19 17:03:58 -07001215 if (dma_direction == DMA_TO_DEVICE)
1216 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1217 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1218 else if (dma_direction == DMA_FROM_DEVICE)
1219 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1220 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1221
1222exit_mgmt:
1223 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1224
Armen Baloyan63ea9232012-11-21 02:39:53 -05001225 if (!rval)
1226 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001227 return rval;
1228}
1229
1230static int
1231qla24xx_iidma(struct fc_bsg_job *bsg_job)
1232{
1233 struct Scsi_Host *host = bsg_job->shost;
1234 scsi_qla_host_t *vha = shost_priv(host);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001235 int rval = 0;
1236 struct qla_port_param *port_param = NULL;
1237 fc_port_t *fcport = NULL;
1238 uint16_t mb[MAILBOX_REGISTER_COUNT];
1239 uint8_t *rsp_ptr = NULL;
1240
Giridhar Malavali6e980162010-03-19 17:03:58 -07001241 if (!IS_IIDMA_CAPABLE(vha->hw)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001242 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001243 return -EINVAL;
1244 }
1245
1246 port_param = (struct qla_port_param *)((char *)bsg_job->request +
1247 sizeof(struct fc_bsg_request));
1248 if (!port_param) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001249 ql_log(ql_log_warn, vha, 0x7047,
1250 "port_param header not provided.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001251 return -EINVAL;
1252 }
1253
1254 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001255 ql_log(ql_log_warn, vha, 0x7048,
1256 "Invalid destination type.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001257 return -EINVAL;
1258 }
1259
1260 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1261 if (fcport->port_type != FCT_TARGET)
1262 continue;
1263
1264 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1265 fcport->port_name, sizeof(fcport->port_name)))
1266 continue;
1267 break;
1268 }
1269
1270 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001271 ql_log(ql_log_warn, vha, 0x7049,
1272 "Failed to find port.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001273 return -EINVAL;
1274 }
1275
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07001276 if (atomic_read(&fcport->state) != FCS_ONLINE) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001277 ql_log(ql_log_warn, vha, 0x704a,
1278 "Port is not online.\n");
Madhuranath Iyengar17cf2c52010-07-23 15:28:22 +05001279 return -EINVAL;
1280 }
1281
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001282 if (fcport->flags & FCF_LOGIN_NEEDED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001283 ql_log(ql_log_warn, vha, 0x704b,
1284 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001285 return -EINVAL;
1286 }
1287
Giridhar Malavali6e980162010-03-19 17:03:58 -07001288 if (port_param->mode)
1289 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1290 port_param->speed, mb);
1291 else
1292 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1293 &port_param->speed, mb);
1294
1295 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001296 ql_log(ql_log_warn, vha, 0x704c,
1297 "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
1298 "%04x %x %04x %04x.\n", fcport->port_name[0],
1299 fcport->port_name[1], fcport->port_name[2],
1300 fcport->port_name[3], fcport->port_name[4],
1301 fcport->port_name[5], fcport->port_name[6],
1302 fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
Armen Baloyan63ea9232012-11-21 02:39:53 -05001303 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001304 } else {
1305 if (!port_param->mode) {
1306 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1307 sizeof(struct qla_port_param);
1308
1309 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1310 sizeof(struct fc_bsg_reply);
1311
1312 memcpy(rsp_ptr, port_param,
1313 sizeof(struct qla_port_param));
1314 }
1315
1316 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -05001317 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001318 }
1319
Giridhar Malavali6e980162010-03-19 17:03:58 -07001320 return rval;
1321}
1322
1323static int
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001324qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001325 uint8_t is_update)
1326{
1327 uint32_t start = 0;
1328 int valid = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001329 struct qla_hw_data *ha = vha->hw;
Harish Zunjarraof19af162010-10-15 11:27:43 -07001330
Harish Zunjarraof19af162010-10-15 11:27:43 -07001331 if (unlikely(pci_channel_offline(ha->pdev)))
1332 return -EINVAL;
1333
1334 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001335 if (start > ha->optrom_size) {
1336 ql_log(ql_log_warn, vha, 0x7055,
1337 "start %d > optrom_size %d.\n", start, ha->optrom_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001338 return -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001339 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001340
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001341 if (ha->optrom_state != QLA_SWAITING) {
1342 ql_log(ql_log_info, vha, 0x7056,
1343 "optrom_state %d.\n", ha->optrom_state);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001344 return -EBUSY;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001345 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001346
1347 ha->optrom_region_start = start;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001348 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001349 if (is_update) {
1350 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1351 valid = 1;
1352 else if (start == (ha->flt_region_boot * 4) ||
1353 start == (ha->flt_region_fw * 4))
1354 valid = 1;
1355 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001356 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
Harish Zunjarraof19af162010-10-15 11:27:43 -07001357 valid = 1;
1358 if (!valid) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001359 ql_log(ql_log_warn, vha, 0x7058,
1360 "Invalid start region 0x%x/0x%x.\n", start,
1361 bsg_job->request_payload.payload_len);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001362 return -EINVAL;
1363 }
1364
1365 ha->optrom_region_size = start +
1366 bsg_job->request_payload.payload_len > ha->optrom_size ?
1367 ha->optrom_size - start :
1368 bsg_job->request_payload.payload_len;
1369 ha->optrom_state = QLA_SWRITING;
1370 } else {
1371 ha->optrom_region_size = start +
1372 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1373 ha->optrom_size - start :
1374 bsg_job->reply_payload.payload_len;
1375 ha->optrom_state = QLA_SREADING;
1376 }
1377
1378 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1379 if (!ha->optrom_buffer) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001380 ql_log(ql_log_warn, vha, 0x7059,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001381 "Read: Unable to allocate memory for optrom retrieval "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001382 "(%x)\n", ha->optrom_region_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001383
1384 ha->optrom_state = QLA_SWAITING;
1385 return -ENOMEM;
1386 }
1387
1388 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1389 return 0;
1390}
1391
1392static int
1393qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1394{
1395 struct Scsi_Host *host = bsg_job->shost;
1396 scsi_qla_host_t *vha = shost_priv(host);
1397 struct qla_hw_data *ha = vha->hw;
1398 int rval = 0;
1399
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001400 if (ha->flags.nic_core_reset_hdlr_active)
Giridhar Malavalia49393f2012-04-25 07:26:14 -07001401 return -EBUSY;
1402
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001403 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001404 if (rval)
1405 return rval;
1406
1407 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1408 ha->optrom_region_start, ha->optrom_region_size);
1409
1410 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1411 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1412 ha->optrom_region_size);
1413
1414 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1415 bsg_job->reply->result = DID_OK;
1416 vfree(ha->optrom_buffer);
1417 ha->optrom_buffer = NULL;
1418 ha->optrom_state = QLA_SWAITING;
1419 bsg_job->job_done(bsg_job);
1420 return rval;
1421}
1422
1423static int
1424qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1425{
1426 struct Scsi_Host *host = bsg_job->shost;
1427 scsi_qla_host_t *vha = shost_priv(host);
1428 struct qla_hw_data *ha = vha->hw;
1429 int rval = 0;
1430
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001431 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001432 if (rval)
1433 return rval;
1434
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04001435 /* Set the isp82xx_no_md_cap not to capture minidump */
1436 ha->flags.isp82xx_no_md_cap = 1;
1437
Harish Zunjarraof19af162010-10-15 11:27:43 -07001438 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1439 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1440 ha->optrom_region_size);
1441
1442 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1443 ha->optrom_region_start, ha->optrom_region_size);
1444
1445 bsg_job->reply->result = DID_OK;
1446 vfree(ha->optrom_buffer);
1447 ha->optrom_buffer = NULL;
1448 ha->optrom_state = QLA_SWAITING;
1449 bsg_job->job_done(bsg_job);
1450 return rval;
1451}
1452
1453static int
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001454qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1455{
1456 struct Scsi_Host *host = bsg_job->shost;
1457 scsi_qla_host_t *vha = shost_priv(host);
1458 struct qla_hw_data *ha = vha->hw;
1459 int rval = 0;
1460 uint8_t bsg[DMA_POOL_SIZE];
1461 struct qla_image_version_list *list = (void *)bsg;
1462 struct qla_image_version *image;
1463 uint32_t count;
1464 dma_addr_t sfp_dma;
1465 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1466 if (!sfp) {
1467 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1468 EXT_STATUS_NO_MEMORY;
1469 goto done;
1470 }
1471
1472 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1473 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1474
1475 image = list->version;
1476 count = list->count;
1477 while (count--) {
1478 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1479 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1480 image->field_address.device, image->field_address.offset,
1481 sizeof(image->field_info), image->field_address.option);
1482 if (rval) {
1483 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1484 EXT_STATUS_MAILBOX;
1485 goto dealloc;
1486 }
1487 image++;
1488 }
1489
1490 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1491
1492dealloc:
1493 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1494
1495done:
1496 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1497 bsg_job->reply->result = DID_OK << 16;
1498 bsg_job->job_done(bsg_job);
1499
1500 return 0;
1501}
1502
1503static int
1504qla2x00_read_fru_status(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_status_reg *sr = (void *)bsg;
1512 dma_addr_t sfp_dma;
1513 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1514 if (!sfp) {
1515 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1516 EXT_STATUS_NO_MEMORY;
1517 goto done;
1518 }
1519
1520 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1521 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1522
1523 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1524 sr->field_address.device, sr->field_address.offset,
1525 sizeof(sr->status_reg), sr->field_address.option);
1526 sr->status_reg = *sfp;
1527
1528 if (rval) {
1529 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1530 EXT_STATUS_MAILBOX;
1531 goto dealloc;
1532 }
1533
1534 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1535 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1536
1537 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1538
1539dealloc:
1540 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1541
1542done:
1543 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1544 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1545 bsg_job->reply->result = DID_OK << 16;
1546 bsg_job->job_done(bsg_job);
1547
1548 return 0;
1549}
1550
1551static int
1552qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1553{
1554 struct Scsi_Host *host = bsg_job->shost;
1555 scsi_qla_host_t *vha = shost_priv(host);
1556 struct qla_hw_data *ha = vha->hw;
1557 int rval = 0;
1558 uint8_t bsg[DMA_POOL_SIZE];
1559 struct qla_status_reg *sr = (void *)bsg;
1560 dma_addr_t sfp_dma;
1561 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1562 if (!sfp) {
1563 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1564 EXT_STATUS_NO_MEMORY;
1565 goto done;
1566 }
1567
1568 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1569 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1570
1571 *sfp = sr->status_reg;
1572 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1573 sr->field_address.device, sr->field_address.offset,
1574 sizeof(sr->status_reg), sr->field_address.option);
1575
1576 if (rval) {
1577 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1578 EXT_STATUS_MAILBOX;
1579 goto dealloc;
1580 }
1581
1582 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1583
1584dealloc:
1585 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1586
1587done:
1588 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1589 bsg_job->reply->result = DID_OK << 16;
1590 bsg_job->job_done(bsg_job);
1591
1592 return 0;
1593}
1594
1595static int
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001596qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1597{
1598 struct Scsi_Host *host = bsg_job->shost;
1599 scsi_qla_host_t *vha = shost_priv(host);
1600 struct qla_hw_data *ha = vha->hw;
1601 int rval = 0;
1602 uint8_t bsg[DMA_POOL_SIZE];
1603 struct qla_i2c_access *i2c = (void *)bsg;
1604 dma_addr_t sfp_dma;
1605 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1606 if (!sfp) {
1607 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1608 EXT_STATUS_NO_MEMORY;
1609 goto done;
1610 }
1611
1612 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1613 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1614
1615 memcpy(sfp, i2c->buffer, i2c->length);
1616 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1617 i2c->device, i2c->offset, i2c->length, i2c->option);
1618
1619 if (rval) {
1620 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1621 EXT_STATUS_MAILBOX;
1622 goto dealloc;
1623 }
1624
1625 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1626
1627dealloc:
1628 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1629
1630done:
1631 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1632 bsg_job->reply->result = DID_OK << 16;
1633 bsg_job->job_done(bsg_job);
1634
1635 return 0;
1636}
1637
1638static int
1639qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1640{
1641 struct Scsi_Host *host = bsg_job->shost;
1642 scsi_qla_host_t *vha = shost_priv(host);
1643 struct qla_hw_data *ha = vha->hw;
1644 int rval = 0;
1645 uint8_t bsg[DMA_POOL_SIZE];
1646 struct qla_i2c_access *i2c = (void *)bsg;
1647 dma_addr_t sfp_dma;
1648 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1649 if (!sfp) {
1650 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1651 EXT_STATUS_NO_MEMORY;
1652 goto done;
1653 }
1654
1655 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1656 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1657
1658 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1659 i2c->device, i2c->offset, i2c->length, i2c->option);
1660
1661 if (rval) {
1662 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1663 EXT_STATUS_MAILBOX;
1664 goto dealloc;
1665 }
1666
1667 memcpy(i2c->buffer, sfp, i2c->length);
1668 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1669 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1670
1671 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1672
1673dealloc:
1674 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1675
1676done:
1677 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1678 bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1679 bsg_job->reply->result = DID_OK << 16;
1680 bsg_job->job_done(bsg_job);
1681
1682 return 0;
1683}
1684
1685static int
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001686qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1687{
1688 struct Scsi_Host *host = bsg_job->shost;
1689 scsi_qla_host_t *vha = shost_priv(host);
1690 struct qla_hw_data *ha = vha->hw;
1691 uint16_t thread_id;
1692 uint32_t rval = EXT_STATUS_OK;
1693 uint16_t req_sg_cnt = 0;
1694 uint16_t rsp_sg_cnt = 0;
1695 uint16_t nextlid = 0;
1696 uint32_t tot_dsds;
1697 srb_t *sp = NULL;
1698 uint32_t req_data_len = 0;
1699 uint32_t rsp_data_len = 0;
1700
1701 /* Check the type of the adapter */
1702 if (!IS_BIDI_CAPABLE(ha)) {
1703 ql_log(ql_log_warn, vha, 0x70a0,
1704 "This adapter is not supported\n");
1705 rval = EXT_STATUS_NOT_SUPPORTED;
1706 goto done;
1707 }
1708
1709 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1710 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1711 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1712 rval = EXT_STATUS_BUSY;
1713 goto done;
1714 }
1715
1716 /* Check if host is online */
1717 if (!vha->flags.online) {
1718 ql_log(ql_log_warn, vha, 0x70a1,
1719 "Host is not online\n");
1720 rval = EXT_STATUS_DEVICE_OFFLINE;
1721 goto done;
1722 }
1723
1724 /* Check if cable is plugged in or not */
1725 if (vha->device_flags & DFLG_NO_CABLE) {
1726 ql_log(ql_log_warn, vha, 0x70a2,
1727 "Cable is unplugged...\n");
1728 rval = EXT_STATUS_INVALID_CFG;
1729 goto done;
1730 }
1731
1732 /* Check if the switch is connected or not */
1733 if (ha->current_topology != ISP_CFG_F) {
1734 ql_log(ql_log_warn, vha, 0x70a3,
1735 "Host is not connected to the switch\n");
1736 rval = EXT_STATUS_INVALID_CFG;
1737 goto done;
1738 }
1739
1740 /* Check if operating mode is P2P */
1741 if (ha->operating_mode != P2P) {
1742 ql_log(ql_log_warn, vha, 0x70a4,
1743 "Host is operating mode is not P2p\n");
1744 rval = EXT_STATUS_INVALID_CFG;
1745 goto done;
1746 }
1747
1748 thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1749
1750 mutex_lock(&ha->selflogin_lock);
1751 if (vha->self_login_loop_id == 0) {
1752 /* Initialize all required fields of fcport */
1753 vha->bidir_fcport.vha = vha;
1754 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1755 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1756 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1757 vha->bidir_fcport.loop_id = vha->loop_id;
1758
1759 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1760 ql_log(ql_log_warn, vha, 0x70a7,
1761 "Failed to login port %06X for bidirectional IOCB\n",
1762 vha->bidir_fcport.d_id.b24);
1763 mutex_unlock(&ha->selflogin_lock);
1764 rval = EXT_STATUS_MAILBOX;
1765 goto done;
1766 }
1767 vha->self_login_loop_id = nextlid - 1;
1768
1769 }
1770 /* Assign the self login loop id to fcport */
1771 mutex_unlock(&ha->selflogin_lock);
1772
1773 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1774
1775 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1776 bsg_job->request_payload.sg_list,
1777 bsg_job->request_payload.sg_cnt,
1778 DMA_TO_DEVICE);
1779
1780 if (!req_sg_cnt) {
1781 rval = EXT_STATUS_NO_MEMORY;
1782 goto done;
1783 }
1784
1785 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1786 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1787 DMA_FROM_DEVICE);
1788
1789 if (!rsp_sg_cnt) {
1790 rval = EXT_STATUS_NO_MEMORY;
1791 goto done_unmap_req_sg;
1792 }
1793
1794 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1795 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1796 ql_dbg(ql_dbg_user, vha, 0x70a9,
1797 "Dma mapping resulted in different sg counts "
1798 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1799 "%x dma_reply_sg_cnt: %x]\n",
1800 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1801 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1802 rval = EXT_STATUS_NO_MEMORY;
1803 goto done_unmap_sg;
1804 }
1805
1806 if (req_data_len != rsp_data_len) {
1807 rval = EXT_STATUS_BUSY;
1808 ql_log(ql_log_warn, vha, 0x70aa,
1809 "req_data_len != rsp_data_len\n");
1810 goto done_unmap_sg;
1811 }
1812
1813 req_data_len = bsg_job->request_payload.payload_len;
1814 rsp_data_len = bsg_job->reply_payload.payload_len;
1815
1816
1817 /* Alloc SRB structure */
1818 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1819 if (!sp) {
1820 ql_dbg(ql_dbg_user, vha, 0x70ac,
1821 "Alloc SRB structure failed\n");
1822 rval = EXT_STATUS_NO_MEMORY;
1823 goto done_unmap_sg;
1824 }
1825
1826 /*Populate srb->ctx with bidir ctx*/
1827 sp->u.bsg_job = bsg_job;
1828 sp->free = qla2x00_bsg_sp_free;
1829 sp->type = SRB_BIDI_CMD;
1830 sp->done = qla2x00_bsg_job_done;
1831
1832 /* Add the read and write sg count */
1833 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1834
1835 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1836 if (rval != EXT_STATUS_OK)
1837 goto done_free_srb;
1838 /* the bsg request will be completed in the interrupt handler */
1839 return rval;
1840
1841done_free_srb:
1842 mempool_free(sp, ha->srb_mempool);
1843done_unmap_sg:
1844 dma_unmap_sg(&ha->pdev->dev,
1845 bsg_job->reply_payload.sg_list,
1846 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1847done_unmap_req_sg:
1848 dma_unmap_sg(&ha->pdev->dev,
1849 bsg_job->request_payload.sg_list,
1850 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1851done:
1852
1853 /* Return an error vendor specific response
1854 * and complete the bsg request
1855 */
1856 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1857 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1858 bsg_job->reply->reply_payload_rcv_len = 0;
1859 bsg_job->reply->result = (DID_OK) << 16;
1860 bsg_job->job_done(bsg_job);
1861 /* Always retrun success, vendor rsp carries correct status */
1862 return 0;
1863}
1864
1865static int
Giridhar Malavali6e980162010-03-19 17:03:58 -07001866qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
1867{
1868 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
1869 case QL_VND_LOOPBACK:
1870 return qla2x00_process_loopback(bsg_job);
1871
1872 case QL_VND_A84_RESET:
1873 return qla84xx_reset(bsg_job);
1874
1875 case QL_VND_A84_UPDATE_FW:
1876 return qla84xx_updatefw(bsg_job);
1877
1878 case QL_VND_A84_MGMT_CMD:
1879 return qla84xx_mgmt_cmd(bsg_job);
1880
1881 case QL_VND_IIDMA:
1882 return qla24xx_iidma(bsg_job);
1883
Sarang Radke09ff7012010-03-19 17:03:59 -07001884 case QL_VND_FCP_PRIO_CFG_CMD:
1885 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
1886
Harish Zunjarraof19af162010-10-15 11:27:43 -07001887 case QL_VND_READ_FLASH:
1888 return qla2x00_read_optrom(bsg_job);
1889
1890 case QL_VND_UPDATE_FLASH:
1891 return qla2x00_update_optrom(bsg_job);
1892
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001893 case QL_VND_SET_FRU_VERSION:
1894 return qla2x00_update_fru_versions(bsg_job);
1895
1896 case QL_VND_READ_FRU_STATUS:
1897 return qla2x00_read_fru_status(bsg_job);
1898
1899 case QL_VND_WRITE_FRU_STATUS:
1900 return qla2x00_write_fru_status(bsg_job);
1901
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001902 case QL_VND_WRITE_I2C:
1903 return qla2x00_write_i2c(bsg_job);
1904
1905 case QL_VND_READ_I2C:
1906 return qla2x00_read_i2c(bsg_job);
1907
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001908 case QL_VND_DIAG_IO_CMD:
1909 return qla24xx_process_bidir_cmd(bsg_job);
1910
Giridhar Malavali6e980162010-03-19 17:03:58 -07001911 default:
Giridhar Malavali6e980162010-03-19 17:03:58 -07001912 return -ENOSYS;
1913 }
1914}
1915
1916int
1917qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
1918{
1919 int ret = -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001920 struct fc_rport *rport;
1921 fc_port_t *fcport = NULL;
1922 struct Scsi_Host *host;
1923 scsi_qla_host_t *vha;
1924
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08001925 /* In case no data transferred. */
1926 bsg_job->reply->reply_payload_rcv_len = 0;
1927
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001928 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1929 rport = bsg_job->rport;
1930 fcport = *(fc_port_t **) rport->dd_data;
1931 host = rport_to_shost(rport);
1932 vha = shost_priv(host);
1933 } else {
1934 host = bsg_job->shost;
1935 vha = shost_priv(host);
1936 }
1937
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001938 if (qla2x00_reset_active(vha)) {
1939 ql_dbg(ql_dbg_user, vha, 0x709f,
1940 "BSG: ISP abort active/needed -- cmd=%d.\n",
1941 bsg_job->request->msgcode);
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001942 return -EBUSY;
1943 }
1944
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001945 ql_dbg(ql_dbg_user, vha, 0x7000,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001946 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001947
1948 switch (bsg_job->request->msgcode) {
1949 case FC_BSG_RPT_ELS:
1950 case FC_BSG_HST_ELS_NOLOGIN:
1951 ret = qla2x00_process_els(bsg_job);
1952 break;
1953 case FC_BSG_HST_CT:
1954 ret = qla2x00_process_ct(bsg_job);
1955 break;
1956 case FC_BSG_HST_VENDOR:
1957 ret = qla2x00_process_vendor_specific(bsg_job);
1958 break;
1959 case FC_BSG_HST_ADD_RPORT:
1960 case FC_BSG_HST_DEL_RPORT:
1961 case FC_BSG_RPT_CT:
1962 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001963 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001964 break;
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001965 }
Giridhar Malavali6e980162010-03-19 17:03:58 -07001966 return ret;
1967}
1968
1969int
1970qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
1971{
1972 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
1973 struct qla_hw_data *ha = vha->hw;
1974 srb_t *sp;
1975 int cnt, que;
1976 unsigned long flags;
1977 struct req_que *req;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001978
1979 /* find the bsg job from the active list of commands */
1980 spin_lock_irqsave(&ha->hardware_lock, flags);
1981 for (que = 0; que < ha->max_req_queues; que++) {
1982 req = ha->req_q_map[que];
1983 if (!req)
1984 continue;
1985
Chad Dupuis8d93f552013-01-30 03:34:37 -05001986 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
Giridhar Malavali6e980162010-03-19 17:03:58 -07001987 sp = req->outstanding_cmds[cnt];
Giridhar Malavali6e980162010-03-19 17:03:58 -07001988 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001989 if (((sp->type == SRB_CT_CMD) ||
1990 (sp->type == SRB_ELS_CMD_HST))
1991 && (sp->u.bsg_job == bsg_job)) {
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001992 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001993 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001994 ql_log(ql_log_warn, vha, 0x7089,
1995 "mbx abort_command "
1996 "failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001997 bsg_job->req->errors =
1998 bsg_job->reply->result = -EIO;
1999 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002000 ql_dbg(ql_dbg_user, vha, 0x708a,
2001 "mbx abort_command "
2002 "success.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07002003 bsg_job->req->errors =
2004 bsg_job->reply->result = 0;
2005 }
Giridhar Malavali900a36e2010-12-21 16:00:26 -08002006 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002007 goto done;
2008 }
2009 }
2010 }
2011 }
2012 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002013 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07002014 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
2015 return 0;
2016
2017done:
2018 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2019 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
2020 kfree(sp->fcport);
Chad Dupuisb00ee7d2013-02-08 01:57:50 -05002021 qla2x00_rel_sp(vha, sp);
Giridhar Malavali6e980162010-03-19 17:03:58 -07002022 return 0;
2023}