blob: 5b49766e0ae76d814314072a9c4bca51f4ac4215 [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;
30 struct scsi_qla_host *vha = (scsi_qla_host_t *)data;
31 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);
43 mempool_free(sp, vha->hw->srb_mempool);
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);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700371 mempool_free(sp, ha->srb_mempool);
372 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);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700518 mempool_free(sp, ha->srb_mempool);
519 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}
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400534/*
535 * Set the port configuration to enable the internal or external loopback
536 * depending on the loopback mode.
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700537 */
538static inline int
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400539qla81xx_set_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
540 uint16_t *new_config, uint16_t mode)
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700541{
542 int ret = 0;
543 int rval = 0;
544 struct qla_hw_data *ha = vha->hw;
545
Nigel Kirklandf863f602012-05-15 14:34:19 -0400546 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700547 goto done_set_internal;
548
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400549 if (mode == INTERNAL_LOOPBACK)
550 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
551 else if (mode == EXTERNAL_LOOPBACK)
552 new_config[0] = config[0] | (ENABLE_EXTERNAL_LOOPBACK << 1);
553 ql_dbg(ql_dbg_user, vha, 0x70be,
554 "new_config[0]=%02x\n", (new_config[0] & INTERNAL_LOOPBACK_MASK));
555
556 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700557
558 ha->notify_dcbx_comp = 1;
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, 0x7021,
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_set_internal;
566 }
567
568 /* Wait for DCBX complete event */
569 if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700570 ql_dbg(ql_dbg_user, vha, 0x7022,
571 "State change notification not received.\n");
Chad Dupuisbf5b8ad2012-08-22 14:21:24 -0400572 rval = -EINVAL;
573 } else {
574 if (ha->flags.idc_compl_status) {
575 ql_dbg(ql_dbg_user, vha, 0x70c3,
576 "Bad status in IDC Completion AEN\n");
577 rval = -EINVAL;
578 ha->flags.idc_compl_status = 0;
579 } else
580 ql_dbg(ql_dbg_user, vha, 0x7023,
581 "State change received.\n");
582 }
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700583
584 ha->notify_dcbx_comp = 0;
585
586done_set_internal:
587 return rval;
588}
589
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400590/* Disable loopback mode */
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700591static inline int
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400592qla81xx_reset_loopback_mode(scsi_qla_host_t *vha, uint16_t *config,
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700593 int wait)
594{
595 int ret = 0;
596 int rval = 0;
597 uint16_t new_config[4];
598 struct qla_hw_data *ha = vha->hw;
599
Nigel Kirklandf863f602012-05-15 14:34:19 -0400600 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700601 goto done_reset_internal;
602
603 memset(new_config, 0 , sizeof(new_config));
604 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400605 ENABLE_INTERNAL_LOOPBACK ||
606 (config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
607 ENABLE_EXTERNAL_LOOPBACK) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700608 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400609 ql_dbg(ql_dbg_user, vha, 0x70bf, "new_config[0]=%02x\n",
610 (new_config[0] & INTERNAL_LOOPBACK_MASK));
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700611 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
612
613 ha->notify_dcbx_comp = wait;
614 ret = qla81xx_set_port_config(vha, new_config);
615 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700616 ql_log(ql_log_warn, vha, 0x7025,
617 "Set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700618 ha->notify_dcbx_comp = 0;
619 rval = -EINVAL;
620 goto done_reset_internal;
621 }
622
623 /* Wait for DCBX complete event */
624 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
625 (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700626 ql_dbg(ql_dbg_user, vha, 0x7026,
627 "State change notification not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700628 ha->notify_dcbx_comp = 0;
629 rval = -EINVAL;
630 goto done_reset_internal;
631 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700632 ql_dbg(ql_dbg_user, vha, 0x7027,
633 "State change received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700634
635 ha->notify_dcbx_comp = 0;
636 }
637done_reset_internal:
638 return rval;
639}
640
Giridhar Malavali6e980162010-03-19 17:03:58 -0700641static int
642qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
643{
644 struct Scsi_Host *host = bsg_job->shost;
645 scsi_qla_host_t *vha = shost_priv(host);
646 struct qla_hw_data *ha = vha->hw;
647 int rval;
648 uint8_t command_sent;
649 char *type;
650 struct msg_echo_lb elreq;
651 uint16_t response[MAILBOX_REGISTER_COUNT];
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700652 uint16_t config[4], new_config[4];
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700653 uint8_t *fw_sts_ptr;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700654 uint8_t *req_data = NULL;
655 dma_addr_t req_data_dma;
656 uint32_t req_data_len;
657 uint8_t *rsp_data = NULL;
658 dma_addr_t rsp_data_dma;
659 uint32_t rsp_data_len;
660
Giridhar Malavali6e980162010-03-19 17:03:58 -0700661 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700662 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700663 return -EIO;
664 }
665
666 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
667 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
668 DMA_TO_DEVICE);
669
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700670 if (!elreq.req_sg_cnt) {
671 ql_log(ql_log_warn, vha, 0x701a,
672 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700673 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700674 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700675
676 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
677 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
678 DMA_FROM_DEVICE);
679
680 if (!elreq.rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700681 ql_log(ql_log_warn, vha, 0x701b,
682 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700683 rval = -ENOMEM;
684 goto done_unmap_req_sg;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700685 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700686
687 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
688 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700689 ql_log(ql_log_warn, vha, 0x701c,
690 "dma mapping resulted in different sg counts, "
691 "request_sg_cnt: %x dma_request_sg_cnt: %x "
692 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
693 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
694 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700695 rval = -EAGAIN;
696 goto done_unmap_sg;
697 }
698 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
699 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
700 &req_data_dma, GFP_KERNEL);
701 if (!req_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700702 ql_log(ql_log_warn, vha, 0x701d,
703 "dma alloc failed for req_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700704 rval = -ENOMEM;
705 goto done_unmap_sg;
706 }
707
708 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
709 &rsp_data_dma, GFP_KERNEL);
710 if (!rsp_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700711 ql_log(ql_log_warn, vha, 0x7004,
712 "dma alloc failed for rsp_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700713 rval = -ENOMEM;
714 goto done_free_dma_req;
715 }
716
717 /* Copy the request buffer in req_data now */
718 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
719 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
720
721 elreq.send_dma = req_data_dma;
722 elreq.rcv_dma = rsp_data_dma;
723 elreq.transfer_size = req_data_len;
724
725 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
726
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400727 if (atomic_read(&vha->loop_state) == LOOP_READY &&
728 (ha->current_topology == ISP_CFG_F ||
Nigel Kirklandf863f602012-05-15 14:34:19 -0400729 ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700730 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
731 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
732 elreq.options == EXTERNAL_LOOPBACK) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700733 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700734 ql_dbg(ql_dbg_user, vha, 0x701e,
735 "BSG request type: %s.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700736 command_sent = INT_DEF_LB_ECHO_CMD;
737 rval = qla2x00_echo_test(vha, &elreq, response);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700738 } else {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800739 if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700740 memset(config, 0, sizeof(config));
741 memset(new_config, 0, sizeof(new_config));
742 if (qla81xx_get_port_config(vha, config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700743 ql_log(ql_log_warn, vha, 0x701f,
744 "Get port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700745 rval = -EPERM;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500746 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700747 }
748
Chad Dupuis1bcc46c2013-02-08 01:57:46 -0500749 if ((config[0] & INTERNAL_LOOPBACK_MASK) != 0) {
750 ql_dbg(ql_dbg_user, vha, 0x70c4,
751 "Loopback operation already in "
752 "progress.\n");
753 rval = -EAGAIN;
754 goto done_free_dma_rsp;
755 }
756
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400757 ql_dbg(ql_dbg_user, vha, 0x70c0,
758 "elreq.options=%04x\n", elreq.options);
759
760 if (elreq.options == EXTERNAL_LOOPBACK)
761 if (IS_QLA8031(ha))
762 rval = qla81xx_set_loopback_mode(vha,
763 config, new_config, elreq.options);
764 else
765 rval = qla81xx_reset_loopback_mode(vha,
766 config, 1);
767 else
768 rval = qla81xx_set_loopback_mode(vha, config,
769 new_config, elreq.options);
770
771 if (rval) {
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400772 rval = -EPERM;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500773 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700774 }
775
776 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700777 ql_dbg(ql_dbg_user, vha, 0x7028,
778 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700779
780 command_sent = INT_DEF_LB_LOOPBACK_CMD;
781 rval = qla2x00_loopback_test(vha, &elreq, response);
782
Joe Carnuccio4052bd52010-12-21 16:00:17 -0800783 if (new_config[0]) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700784 /* Revert back to original port config
785 * Also clear internal loopback
786 */
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400787 qla81xx_reset_loopback_mode(vha,
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700788 new_config, 0);
789 }
790
791 if (response[0] == MBS_COMMAND_ERROR &&
792 response[1] == MBS_LB_RESET) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700793 ql_log(ql_log_warn, vha, 0x7029,
794 "MBX command error, Aborting ISP.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700795 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
796 qla2xxx_wake_dpc(vha);
797 qla2x00_wait_for_chip_reset(vha);
798 /* Also reset the MPI */
799 if (qla81xx_restart_mpi_firmware(vha) !=
800 QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700801 ql_log(ql_log_warn, vha, 0x702a,
802 "MPI reset failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700803 }
804
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700805 rval = -EIO;
Steve Hodgson9bceab42012-11-21 02:39:56 -0500806 goto done_free_dma_rsp;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700807 }
808 } else {
809 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700810 ql_dbg(ql_dbg_user, vha, 0x702b,
811 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700812 command_sent = INT_DEF_LB_LOOPBACK_CMD;
813 rval = qla2x00_loopback_test(vha, &elreq, response);
814 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700815 }
816
817 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700818 ql_log(ql_log_warn, vha, 0x702c,
819 "Vendor request %s failed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700820
Giridhar Malavali6e980162010-03-19 17:03:58 -0700821 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700822 bsg_job->reply->result = (DID_ERROR << 16);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500823 bsg_job->reply->reply_payload_rcv_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700824 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700825 ql_dbg(ql_dbg_user, vha, 0x702d,
826 "Vendor request %s completed.\n", type);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500827 bsg_job->reply->result = (DID_OK << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700828 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
829 bsg_job->reply_payload.sg_cnt, rsp_data,
830 rsp_data_len);
831 }
Armen Baloyan63ea9232012-11-21 02:39:53 -0500832
833 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
834 sizeof(response) + sizeof(uint8_t);
835 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
836 sizeof(struct fc_bsg_reply);
837 memcpy(fw_sts_ptr, response, sizeof(response));
838 fw_sts_ptr += sizeof(response);
839 *fw_sts_ptr = command_sent;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700840
Steve Hodgson9bceab42012-11-21 02:39:56 -0500841done_free_dma_rsp:
Giridhar Malavali6e980162010-03-19 17:03:58 -0700842 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
843 rsp_data, rsp_data_dma);
844done_free_dma_req:
845 dma_free_coherent(&ha->pdev->dev, req_data_len,
846 req_data, req_data_dma);
847done_unmap_sg:
848 dma_unmap_sg(&ha->pdev->dev,
849 bsg_job->reply_payload.sg_list,
850 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
851done_unmap_req_sg:
852 dma_unmap_sg(&ha->pdev->dev,
853 bsg_job->request_payload.sg_list,
854 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500855 if (!rval)
856 bsg_job->job_done(bsg_job);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700857 return rval;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700858}
859
860static int
861qla84xx_reset(struct fc_bsg_job *bsg_job)
862{
863 struct Scsi_Host *host = bsg_job->shost;
864 scsi_qla_host_t *vha = shost_priv(host);
865 struct qla_hw_data *ha = vha->hw;
866 int rval = 0;
867 uint32_t flag;
868
Giridhar Malavali6e980162010-03-19 17:03:58 -0700869 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700870 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700871 return -EINVAL;
872 }
873
874 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
875
876 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
877
878 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700879 ql_log(ql_log_warn, vha, 0x7030,
880 "Vendor request 84xx reset failed.\n");
Armen Baloyan63ea9232012-11-21 02:39:53 -0500881 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700882
883 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700884 ql_dbg(ql_dbg_user, vha, 0x7031,
885 "Vendor request 84xx reset completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700886 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -0500887 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700888 }
889
Giridhar Malavali6e980162010-03-19 17:03:58 -0700890 return rval;
891}
892
893static int
894qla84xx_updatefw(struct fc_bsg_job *bsg_job)
895{
896 struct Scsi_Host *host = bsg_job->shost;
897 scsi_qla_host_t *vha = shost_priv(host);
898 struct qla_hw_data *ha = vha->hw;
899 struct verify_chip_entry_84xx *mn = NULL;
900 dma_addr_t mn_dma, fw_dma;
901 void *fw_buf = NULL;
902 int rval = 0;
903 uint32_t sg_cnt;
904 uint32_t data_len;
905 uint16_t options;
906 uint32_t flag;
907 uint32_t fw_ver;
908
Giridhar Malavali6e980162010-03-19 17:03:58 -0700909 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700910 ql_dbg(ql_dbg_user, vha, 0x7032,
911 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700912 return -EINVAL;
913 }
914
915 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
916 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700917 if (!sg_cnt) {
918 ql_log(ql_log_warn, vha, 0x7033,
919 "dma_map_sg returned %d for request.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700920 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700921 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700922
923 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700924 ql_log(ql_log_warn, vha, 0x7034,
925 "DMA mapping resulted in different sg counts, "
926 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
927 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700928 rval = -EAGAIN;
929 goto done_unmap_sg;
930 }
931
932 data_len = bsg_job->request_payload.payload_len;
933 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
934 &fw_dma, GFP_KERNEL);
935 if (!fw_buf) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700936 ql_log(ql_log_warn, vha, 0x7035,
937 "DMA alloc failed for fw_buf.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700938 rval = -ENOMEM;
939 goto done_unmap_sg;
940 }
941
942 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
943 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
944
945 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
946 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700947 ql_log(ql_log_warn, vha, 0x7036,
948 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700949 rval = -ENOMEM;
950 goto done_free_fw_buf;
951 }
952
953 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
954 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
955
956 memset(mn, 0, sizeof(struct access_chip_84xx));
957 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
958 mn->entry_count = 1;
959
960 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
961 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
962 options |= VCO_DIAG_FW;
963
964 mn->options = cpu_to_le16(options);
965 mn->fw_ver = cpu_to_le32(fw_ver);
966 mn->fw_size = cpu_to_le32(data_len);
967 mn->fw_seq_size = cpu_to_le32(data_len);
968 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
969 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
970 mn->dseg_length = cpu_to_le32(data_len);
971 mn->data_seg_cnt = cpu_to_le16(1);
972
973 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
974
975 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700976 ql_log(ql_log_warn, vha, 0x7037,
977 "Vendor request 84xx updatefw failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700978
Armen Baloyan63ea9232012-11-21 02:39:53 -0500979 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700980 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700981 ql_dbg(ql_dbg_user, vha, 0x7038,
982 "Vendor request 84xx updatefw completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700983
984 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
985 bsg_job->reply->result = DID_OK;
986 }
987
Giridhar Malavali6e980162010-03-19 17:03:58 -0700988 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
989
990done_free_fw_buf:
991 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
992
993done_unmap_sg:
994 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
995 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
996
Armen Baloyan63ea9232012-11-21 02:39:53 -0500997 if (!rval)
998 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700999 return rval;
1000}
1001
1002static int
1003qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1004{
1005 struct Scsi_Host *host = bsg_job->shost;
1006 scsi_qla_host_t *vha = shost_priv(host);
1007 struct qla_hw_data *ha = vha->hw;
1008 struct access_chip_84xx *mn = NULL;
1009 dma_addr_t mn_dma, mgmt_dma;
1010 void *mgmt_b = NULL;
1011 int rval = 0;
1012 struct qla_bsg_a84_mgmt *ql84_mgmt;
1013 uint32_t sg_cnt;
Harish Zunjarraod5459082010-03-19 17:04:00 -07001014 uint32_t data_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001015 uint32_t dma_direction = DMA_NONE;
1016
Giridhar Malavali6e980162010-03-19 17:03:58 -07001017 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001018 ql_log(ql_log_warn, vha, 0x703a,
1019 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001020 return -EINVAL;
1021 }
1022
1023 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1024 sizeof(struct fc_bsg_request));
1025 if (!ql84_mgmt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001026 ql_log(ql_log_warn, vha, 0x703b,
1027 "MGMT header not provided, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001028 return -EINVAL;
1029 }
1030
1031 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1032 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001033 ql_log(ql_log_warn, vha, 0x703c,
1034 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001035 return -ENOMEM;
1036 }
1037
1038 memset(mn, 0, sizeof(struct access_chip_84xx));
1039 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1040 mn->entry_count = 1;
1041
1042 switch (ql84_mgmt->mgmt.cmd) {
1043 case QLA84_MGMT_READ_MEM:
1044 case QLA84_MGMT_GET_INFO:
1045 sg_cnt = dma_map_sg(&ha->pdev->dev,
1046 bsg_job->reply_payload.sg_list,
1047 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1048 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001049 ql_log(ql_log_warn, vha, 0x703d,
1050 "dma_map_sg returned %d for reply.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001051 rval = -ENOMEM;
1052 goto exit_mgmt;
1053 }
1054
1055 dma_direction = DMA_FROM_DEVICE;
1056
1057 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001058 ql_log(ql_log_warn, vha, 0x703e,
1059 "DMA mapping resulted in different sg counts, "
1060 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1061 bsg_job->reply_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001062 rval = -EAGAIN;
1063 goto done_unmap_sg;
1064 }
1065
1066 data_len = bsg_job->reply_payload.payload_len;
1067
1068 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1069 &mgmt_dma, GFP_KERNEL);
1070 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001071 ql_log(ql_log_warn, vha, 0x703f,
1072 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001073 rval = -ENOMEM;
1074 goto done_unmap_sg;
1075 }
1076
1077 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1078 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1079 mn->parameter1 =
1080 cpu_to_le32(
1081 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1082
1083 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1084 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1085 mn->parameter1 =
1086 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1087
1088 mn->parameter2 =
1089 cpu_to_le32(
1090 ql84_mgmt->mgmt.mgmtp.u.info.context);
1091 }
1092 break;
1093
1094 case QLA84_MGMT_WRITE_MEM:
1095 sg_cnt = dma_map_sg(&ha->pdev->dev,
1096 bsg_job->request_payload.sg_list,
1097 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1098
1099 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001100 ql_log(ql_log_warn, vha, 0x7040,
1101 "dma_map_sg returned %d.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001102 rval = -ENOMEM;
1103 goto exit_mgmt;
1104 }
1105
1106 dma_direction = DMA_TO_DEVICE;
1107
1108 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001109 ql_log(ql_log_warn, vha, 0x7041,
1110 "DMA mapping resulted in different sg counts, "
1111 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1112 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001113 rval = -EAGAIN;
1114 goto done_unmap_sg;
1115 }
1116
1117 data_len = bsg_job->request_payload.payload_len;
1118 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1119 &mgmt_dma, GFP_KERNEL);
1120 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001121 ql_log(ql_log_warn, vha, 0x7042,
1122 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001123 rval = -ENOMEM;
1124 goto done_unmap_sg;
1125 }
1126
1127 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1128 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1129
1130 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1131 mn->parameter1 =
1132 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1133 break;
1134
1135 case QLA84_MGMT_CHNG_CONFIG:
1136 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1137 mn->parameter1 =
1138 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1139
1140 mn->parameter2 =
1141 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1142
1143 mn->parameter3 =
1144 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1145 break;
1146
1147 default:
1148 rval = -EIO;
1149 goto exit_mgmt;
1150 }
1151
1152 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1153 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1154 mn->dseg_count = cpu_to_le16(1);
1155 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1156 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1157 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1158 }
1159
1160 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1161
1162 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001163 ql_log(ql_log_warn, vha, 0x7043,
1164 "Vendor request 84xx mgmt failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001165
Armen Baloyan63ea9232012-11-21 02:39:53 -05001166 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001167
1168 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001169 ql_dbg(ql_dbg_user, vha, 0x7044,
1170 "Vendor request 84xx mgmt completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001171
1172 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1173 bsg_job->reply->result = DID_OK;
1174
1175 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1176 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1177 bsg_job->reply->reply_payload_rcv_len =
1178 bsg_job->reply_payload.payload_len;
1179
1180 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001181 bsg_job->reply_payload.sg_cnt, mgmt_b,
1182 data_len);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001183 }
1184 }
1185
Giridhar Malavali6e980162010-03-19 17:03:58 -07001186done_unmap_sg:
Harish Zunjarraod5459082010-03-19 17:04:00 -07001187 if (mgmt_b)
1188 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1189
Giridhar Malavali6e980162010-03-19 17:03:58 -07001190 if (dma_direction == DMA_TO_DEVICE)
1191 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1192 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1193 else if (dma_direction == DMA_FROM_DEVICE)
1194 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1195 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1196
1197exit_mgmt:
1198 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1199
Armen Baloyan63ea9232012-11-21 02:39:53 -05001200 if (!rval)
1201 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001202 return rval;
1203}
1204
1205static int
1206qla24xx_iidma(struct fc_bsg_job *bsg_job)
1207{
1208 struct Scsi_Host *host = bsg_job->shost;
1209 scsi_qla_host_t *vha = shost_priv(host);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001210 int rval = 0;
1211 struct qla_port_param *port_param = NULL;
1212 fc_port_t *fcport = NULL;
1213 uint16_t mb[MAILBOX_REGISTER_COUNT];
1214 uint8_t *rsp_ptr = NULL;
1215
Giridhar Malavali6e980162010-03-19 17:03:58 -07001216 if (!IS_IIDMA_CAPABLE(vha->hw)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001217 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001218 return -EINVAL;
1219 }
1220
1221 port_param = (struct qla_port_param *)((char *)bsg_job->request +
1222 sizeof(struct fc_bsg_request));
1223 if (!port_param) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001224 ql_log(ql_log_warn, vha, 0x7047,
1225 "port_param header not provided.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001226 return -EINVAL;
1227 }
1228
1229 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001230 ql_log(ql_log_warn, vha, 0x7048,
1231 "Invalid destination type.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001232 return -EINVAL;
1233 }
1234
1235 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1236 if (fcport->port_type != FCT_TARGET)
1237 continue;
1238
1239 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1240 fcport->port_name, sizeof(fcport->port_name)))
1241 continue;
1242 break;
1243 }
1244
1245 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001246 ql_log(ql_log_warn, vha, 0x7049,
1247 "Failed to find port.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001248 return -EINVAL;
1249 }
1250
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07001251 if (atomic_read(&fcport->state) != FCS_ONLINE) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001252 ql_log(ql_log_warn, vha, 0x704a,
1253 "Port is not online.\n");
Madhuranath Iyengar17cf2c52010-07-23 15:28:22 +05001254 return -EINVAL;
1255 }
1256
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001257 if (fcport->flags & FCF_LOGIN_NEEDED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001258 ql_log(ql_log_warn, vha, 0x704b,
1259 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001260 return -EINVAL;
1261 }
1262
Giridhar Malavali6e980162010-03-19 17:03:58 -07001263 if (port_param->mode)
1264 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1265 port_param->speed, mb);
1266 else
1267 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1268 &port_param->speed, mb);
1269
1270 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001271 ql_log(ql_log_warn, vha, 0x704c,
1272 "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
1273 "%04x %x %04x %04x.\n", fcport->port_name[0],
1274 fcport->port_name[1], fcport->port_name[2],
1275 fcport->port_name[3], fcport->port_name[4],
1276 fcport->port_name[5], fcport->port_name[6],
1277 fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
Armen Baloyan63ea9232012-11-21 02:39:53 -05001278 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001279 } else {
1280 if (!port_param->mode) {
1281 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1282 sizeof(struct qla_port_param);
1283
1284 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1285 sizeof(struct fc_bsg_reply);
1286
1287 memcpy(rsp_ptr, port_param,
1288 sizeof(struct qla_port_param));
1289 }
1290
1291 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -05001292 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001293 }
1294
Giridhar Malavali6e980162010-03-19 17:03:58 -07001295 return rval;
1296}
1297
1298static int
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001299qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001300 uint8_t is_update)
1301{
1302 uint32_t start = 0;
1303 int valid = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001304 struct qla_hw_data *ha = vha->hw;
Harish Zunjarraof19af162010-10-15 11:27:43 -07001305
Harish Zunjarraof19af162010-10-15 11:27:43 -07001306 if (unlikely(pci_channel_offline(ha->pdev)))
1307 return -EINVAL;
1308
1309 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001310 if (start > ha->optrom_size) {
1311 ql_log(ql_log_warn, vha, 0x7055,
1312 "start %d > optrom_size %d.\n", start, ha->optrom_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001313 return -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001314 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001315
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001316 if (ha->optrom_state != QLA_SWAITING) {
1317 ql_log(ql_log_info, vha, 0x7056,
1318 "optrom_state %d.\n", ha->optrom_state);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001319 return -EBUSY;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001320 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001321
1322 ha->optrom_region_start = start;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001323 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001324 if (is_update) {
1325 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1326 valid = 1;
1327 else if (start == (ha->flt_region_boot * 4) ||
1328 start == (ha->flt_region_fw * 4))
1329 valid = 1;
1330 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001331 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
Harish Zunjarraof19af162010-10-15 11:27:43 -07001332 valid = 1;
1333 if (!valid) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001334 ql_log(ql_log_warn, vha, 0x7058,
1335 "Invalid start region 0x%x/0x%x.\n", start,
1336 bsg_job->request_payload.payload_len);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001337 return -EINVAL;
1338 }
1339
1340 ha->optrom_region_size = start +
1341 bsg_job->request_payload.payload_len > ha->optrom_size ?
1342 ha->optrom_size - start :
1343 bsg_job->request_payload.payload_len;
1344 ha->optrom_state = QLA_SWRITING;
1345 } else {
1346 ha->optrom_region_size = start +
1347 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1348 ha->optrom_size - start :
1349 bsg_job->reply_payload.payload_len;
1350 ha->optrom_state = QLA_SREADING;
1351 }
1352
1353 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1354 if (!ha->optrom_buffer) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001355 ql_log(ql_log_warn, vha, 0x7059,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001356 "Read: Unable to allocate memory for optrom retrieval "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001357 "(%x)\n", ha->optrom_region_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001358
1359 ha->optrom_state = QLA_SWAITING;
1360 return -ENOMEM;
1361 }
1362
1363 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1364 return 0;
1365}
1366
1367static int
1368qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1369{
1370 struct Scsi_Host *host = bsg_job->shost;
1371 scsi_qla_host_t *vha = shost_priv(host);
1372 struct qla_hw_data *ha = vha->hw;
1373 int rval = 0;
1374
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001375 if (ha->flags.nic_core_reset_hdlr_active)
Giridhar Malavalia49393f2012-04-25 07:26:14 -07001376 return -EBUSY;
1377
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001378 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001379 if (rval)
1380 return rval;
1381
1382 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1383 ha->optrom_region_start, ha->optrom_region_size);
1384
1385 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1386 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1387 ha->optrom_region_size);
1388
1389 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1390 bsg_job->reply->result = DID_OK;
1391 vfree(ha->optrom_buffer);
1392 ha->optrom_buffer = NULL;
1393 ha->optrom_state = QLA_SWAITING;
1394 bsg_job->job_done(bsg_job);
1395 return rval;
1396}
1397
1398static int
1399qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1400{
1401 struct Scsi_Host *host = bsg_job->shost;
1402 scsi_qla_host_t *vha = shost_priv(host);
1403 struct qla_hw_data *ha = vha->hw;
1404 int rval = 0;
1405
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001406 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001407 if (rval)
1408 return rval;
1409
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04001410 /* Set the isp82xx_no_md_cap not to capture minidump */
1411 ha->flags.isp82xx_no_md_cap = 1;
1412
Harish Zunjarraof19af162010-10-15 11:27:43 -07001413 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1414 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1415 ha->optrom_region_size);
1416
1417 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1418 ha->optrom_region_start, ha->optrom_region_size);
1419
1420 bsg_job->reply->result = DID_OK;
1421 vfree(ha->optrom_buffer);
1422 ha->optrom_buffer = NULL;
1423 ha->optrom_state = QLA_SWAITING;
1424 bsg_job->job_done(bsg_job);
1425 return rval;
1426}
1427
1428static int
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001429qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1430{
1431 struct Scsi_Host *host = bsg_job->shost;
1432 scsi_qla_host_t *vha = shost_priv(host);
1433 struct qla_hw_data *ha = vha->hw;
1434 int rval = 0;
1435 uint8_t bsg[DMA_POOL_SIZE];
1436 struct qla_image_version_list *list = (void *)bsg;
1437 struct qla_image_version *image;
1438 uint32_t count;
1439 dma_addr_t sfp_dma;
1440 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1441 if (!sfp) {
1442 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1443 EXT_STATUS_NO_MEMORY;
1444 goto done;
1445 }
1446
1447 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1448 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1449
1450 image = list->version;
1451 count = list->count;
1452 while (count--) {
1453 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1454 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1455 image->field_address.device, image->field_address.offset,
1456 sizeof(image->field_info), image->field_address.option);
1457 if (rval) {
1458 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1459 EXT_STATUS_MAILBOX;
1460 goto dealloc;
1461 }
1462 image++;
1463 }
1464
1465 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1466
1467dealloc:
1468 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1469
1470done:
1471 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1472 bsg_job->reply->result = DID_OK << 16;
1473 bsg_job->job_done(bsg_job);
1474
1475 return 0;
1476}
1477
1478static int
1479qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1480{
1481 struct Scsi_Host *host = bsg_job->shost;
1482 scsi_qla_host_t *vha = shost_priv(host);
1483 struct qla_hw_data *ha = vha->hw;
1484 int rval = 0;
1485 uint8_t bsg[DMA_POOL_SIZE];
1486 struct qla_status_reg *sr = (void *)bsg;
1487 dma_addr_t sfp_dma;
1488 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1489 if (!sfp) {
1490 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1491 EXT_STATUS_NO_MEMORY;
1492 goto done;
1493 }
1494
1495 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1496 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1497
1498 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1499 sr->field_address.device, sr->field_address.offset,
1500 sizeof(sr->status_reg), sr->field_address.option);
1501 sr->status_reg = *sfp;
1502
1503 if (rval) {
1504 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1505 EXT_STATUS_MAILBOX;
1506 goto dealloc;
1507 }
1508
1509 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1510 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1511
1512 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1513
1514dealloc:
1515 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1516
1517done:
1518 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1519 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1520 bsg_job->reply->result = DID_OK << 16;
1521 bsg_job->job_done(bsg_job);
1522
1523 return 0;
1524}
1525
1526static int
1527qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1528{
1529 struct Scsi_Host *host = bsg_job->shost;
1530 scsi_qla_host_t *vha = shost_priv(host);
1531 struct qla_hw_data *ha = vha->hw;
1532 int rval = 0;
1533 uint8_t bsg[DMA_POOL_SIZE];
1534 struct qla_status_reg *sr = (void *)bsg;
1535 dma_addr_t sfp_dma;
1536 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1537 if (!sfp) {
1538 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1539 EXT_STATUS_NO_MEMORY;
1540 goto done;
1541 }
1542
1543 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1544 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1545
1546 *sfp = sr->status_reg;
1547 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1548 sr->field_address.device, sr->field_address.offset,
1549 sizeof(sr->status_reg), sr->field_address.option);
1550
1551 if (rval) {
1552 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1553 EXT_STATUS_MAILBOX;
1554 goto dealloc;
1555 }
1556
1557 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1558
1559dealloc:
1560 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1561
1562done:
1563 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1564 bsg_job->reply->result = DID_OK << 16;
1565 bsg_job->job_done(bsg_job);
1566
1567 return 0;
1568}
1569
1570static int
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001571qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1572{
1573 struct Scsi_Host *host = bsg_job->shost;
1574 scsi_qla_host_t *vha = shost_priv(host);
1575 struct qla_hw_data *ha = vha->hw;
1576 int rval = 0;
1577 uint8_t bsg[DMA_POOL_SIZE];
1578 struct qla_i2c_access *i2c = (void *)bsg;
1579 dma_addr_t sfp_dma;
1580 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1581 if (!sfp) {
1582 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1583 EXT_STATUS_NO_MEMORY;
1584 goto done;
1585 }
1586
1587 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1588 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1589
1590 memcpy(sfp, i2c->buffer, i2c->length);
1591 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1592 i2c->device, i2c->offset, i2c->length, i2c->option);
1593
1594 if (rval) {
1595 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1596 EXT_STATUS_MAILBOX;
1597 goto dealloc;
1598 }
1599
1600 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1601
1602dealloc:
1603 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1604
1605done:
1606 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1607 bsg_job->reply->result = DID_OK << 16;
1608 bsg_job->job_done(bsg_job);
1609
1610 return 0;
1611}
1612
1613static int
1614qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1615{
1616 struct Scsi_Host *host = bsg_job->shost;
1617 scsi_qla_host_t *vha = shost_priv(host);
1618 struct qla_hw_data *ha = vha->hw;
1619 int rval = 0;
1620 uint8_t bsg[DMA_POOL_SIZE];
1621 struct qla_i2c_access *i2c = (void *)bsg;
1622 dma_addr_t sfp_dma;
1623 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1624 if (!sfp) {
1625 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1626 EXT_STATUS_NO_MEMORY;
1627 goto done;
1628 }
1629
1630 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1631 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1632
1633 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1634 i2c->device, i2c->offset, i2c->length, i2c->option);
1635
1636 if (rval) {
1637 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1638 EXT_STATUS_MAILBOX;
1639 goto dealloc;
1640 }
1641
1642 memcpy(i2c->buffer, sfp, i2c->length);
1643 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1644 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1645
1646 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1647
1648dealloc:
1649 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1650
1651done:
1652 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1653 bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1654 bsg_job->reply->result = DID_OK << 16;
1655 bsg_job->job_done(bsg_job);
1656
1657 return 0;
1658}
1659
1660static int
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001661qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1662{
1663 struct Scsi_Host *host = bsg_job->shost;
1664 scsi_qla_host_t *vha = shost_priv(host);
1665 struct qla_hw_data *ha = vha->hw;
1666 uint16_t thread_id;
1667 uint32_t rval = EXT_STATUS_OK;
1668 uint16_t req_sg_cnt = 0;
1669 uint16_t rsp_sg_cnt = 0;
1670 uint16_t nextlid = 0;
1671 uint32_t tot_dsds;
1672 srb_t *sp = NULL;
1673 uint32_t req_data_len = 0;
1674 uint32_t rsp_data_len = 0;
1675
1676 /* Check the type of the adapter */
1677 if (!IS_BIDI_CAPABLE(ha)) {
1678 ql_log(ql_log_warn, vha, 0x70a0,
1679 "This adapter is not supported\n");
1680 rval = EXT_STATUS_NOT_SUPPORTED;
1681 goto done;
1682 }
1683
1684 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1685 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1686 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1687 rval = EXT_STATUS_BUSY;
1688 goto done;
1689 }
1690
1691 /* Check if host is online */
1692 if (!vha->flags.online) {
1693 ql_log(ql_log_warn, vha, 0x70a1,
1694 "Host is not online\n");
1695 rval = EXT_STATUS_DEVICE_OFFLINE;
1696 goto done;
1697 }
1698
1699 /* Check if cable is plugged in or not */
1700 if (vha->device_flags & DFLG_NO_CABLE) {
1701 ql_log(ql_log_warn, vha, 0x70a2,
1702 "Cable is unplugged...\n");
1703 rval = EXT_STATUS_INVALID_CFG;
1704 goto done;
1705 }
1706
1707 /* Check if the switch is connected or not */
1708 if (ha->current_topology != ISP_CFG_F) {
1709 ql_log(ql_log_warn, vha, 0x70a3,
1710 "Host is not connected to the switch\n");
1711 rval = EXT_STATUS_INVALID_CFG;
1712 goto done;
1713 }
1714
1715 /* Check if operating mode is P2P */
1716 if (ha->operating_mode != P2P) {
1717 ql_log(ql_log_warn, vha, 0x70a4,
1718 "Host is operating mode is not P2p\n");
1719 rval = EXT_STATUS_INVALID_CFG;
1720 goto done;
1721 }
1722
1723 thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1724
1725 mutex_lock(&ha->selflogin_lock);
1726 if (vha->self_login_loop_id == 0) {
1727 /* Initialize all required fields of fcport */
1728 vha->bidir_fcport.vha = vha;
1729 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1730 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1731 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1732 vha->bidir_fcport.loop_id = vha->loop_id;
1733
1734 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1735 ql_log(ql_log_warn, vha, 0x70a7,
1736 "Failed to login port %06X for bidirectional IOCB\n",
1737 vha->bidir_fcport.d_id.b24);
1738 mutex_unlock(&ha->selflogin_lock);
1739 rval = EXT_STATUS_MAILBOX;
1740 goto done;
1741 }
1742 vha->self_login_loop_id = nextlid - 1;
1743
1744 }
1745 /* Assign the self login loop id to fcport */
1746 mutex_unlock(&ha->selflogin_lock);
1747
1748 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1749
1750 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1751 bsg_job->request_payload.sg_list,
1752 bsg_job->request_payload.sg_cnt,
1753 DMA_TO_DEVICE);
1754
1755 if (!req_sg_cnt) {
1756 rval = EXT_STATUS_NO_MEMORY;
1757 goto done;
1758 }
1759
1760 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1761 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1762 DMA_FROM_DEVICE);
1763
1764 if (!rsp_sg_cnt) {
1765 rval = EXT_STATUS_NO_MEMORY;
1766 goto done_unmap_req_sg;
1767 }
1768
1769 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1770 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1771 ql_dbg(ql_dbg_user, vha, 0x70a9,
1772 "Dma mapping resulted in different sg counts "
1773 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1774 "%x dma_reply_sg_cnt: %x]\n",
1775 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1776 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1777 rval = EXT_STATUS_NO_MEMORY;
1778 goto done_unmap_sg;
1779 }
1780
1781 if (req_data_len != rsp_data_len) {
1782 rval = EXT_STATUS_BUSY;
1783 ql_log(ql_log_warn, vha, 0x70aa,
1784 "req_data_len != rsp_data_len\n");
1785 goto done_unmap_sg;
1786 }
1787
1788 req_data_len = bsg_job->request_payload.payload_len;
1789 rsp_data_len = bsg_job->reply_payload.payload_len;
1790
1791
1792 /* Alloc SRB structure */
1793 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1794 if (!sp) {
1795 ql_dbg(ql_dbg_user, vha, 0x70ac,
1796 "Alloc SRB structure failed\n");
1797 rval = EXT_STATUS_NO_MEMORY;
1798 goto done_unmap_sg;
1799 }
1800
1801 /*Populate srb->ctx with bidir ctx*/
1802 sp->u.bsg_job = bsg_job;
1803 sp->free = qla2x00_bsg_sp_free;
1804 sp->type = SRB_BIDI_CMD;
1805 sp->done = qla2x00_bsg_job_done;
1806
1807 /* Add the read and write sg count */
1808 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1809
1810 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1811 if (rval != EXT_STATUS_OK)
1812 goto done_free_srb;
1813 /* the bsg request will be completed in the interrupt handler */
1814 return rval;
1815
1816done_free_srb:
1817 mempool_free(sp, ha->srb_mempool);
1818done_unmap_sg:
1819 dma_unmap_sg(&ha->pdev->dev,
1820 bsg_job->reply_payload.sg_list,
1821 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1822done_unmap_req_sg:
1823 dma_unmap_sg(&ha->pdev->dev,
1824 bsg_job->request_payload.sg_list,
1825 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1826done:
1827
1828 /* Return an error vendor specific response
1829 * and complete the bsg request
1830 */
1831 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1832 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1833 bsg_job->reply->reply_payload_rcv_len = 0;
1834 bsg_job->reply->result = (DID_OK) << 16;
1835 bsg_job->job_done(bsg_job);
1836 /* Always retrun success, vendor rsp carries correct status */
1837 return 0;
1838}
1839
1840static int
Giridhar Malavali6e980162010-03-19 17:03:58 -07001841qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
1842{
1843 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
1844 case QL_VND_LOOPBACK:
1845 return qla2x00_process_loopback(bsg_job);
1846
1847 case QL_VND_A84_RESET:
1848 return qla84xx_reset(bsg_job);
1849
1850 case QL_VND_A84_UPDATE_FW:
1851 return qla84xx_updatefw(bsg_job);
1852
1853 case QL_VND_A84_MGMT_CMD:
1854 return qla84xx_mgmt_cmd(bsg_job);
1855
1856 case QL_VND_IIDMA:
1857 return qla24xx_iidma(bsg_job);
1858
Sarang Radke09ff7012010-03-19 17:03:59 -07001859 case QL_VND_FCP_PRIO_CFG_CMD:
1860 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
1861
Harish Zunjarraof19af162010-10-15 11:27:43 -07001862 case QL_VND_READ_FLASH:
1863 return qla2x00_read_optrom(bsg_job);
1864
1865 case QL_VND_UPDATE_FLASH:
1866 return qla2x00_update_optrom(bsg_job);
1867
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001868 case QL_VND_SET_FRU_VERSION:
1869 return qla2x00_update_fru_versions(bsg_job);
1870
1871 case QL_VND_READ_FRU_STATUS:
1872 return qla2x00_read_fru_status(bsg_job);
1873
1874 case QL_VND_WRITE_FRU_STATUS:
1875 return qla2x00_write_fru_status(bsg_job);
1876
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001877 case QL_VND_WRITE_I2C:
1878 return qla2x00_write_i2c(bsg_job);
1879
1880 case QL_VND_READ_I2C:
1881 return qla2x00_read_i2c(bsg_job);
1882
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001883 case QL_VND_DIAG_IO_CMD:
1884 return qla24xx_process_bidir_cmd(bsg_job);
1885
Giridhar Malavali6e980162010-03-19 17:03:58 -07001886 default:
Giridhar Malavali6e980162010-03-19 17:03:58 -07001887 return -ENOSYS;
1888 }
1889}
1890
1891int
1892qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
1893{
1894 int ret = -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001895 struct fc_rport *rport;
1896 fc_port_t *fcport = NULL;
1897 struct Scsi_Host *host;
1898 scsi_qla_host_t *vha;
1899
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08001900 /* In case no data transferred. */
1901 bsg_job->reply->reply_payload_rcv_len = 0;
1902
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001903 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1904 rport = bsg_job->rport;
1905 fcport = *(fc_port_t **) rport->dd_data;
1906 host = rport_to_shost(rport);
1907 vha = shost_priv(host);
1908 } else {
1909 host = bsg_job->shost;
1910 vha = shost_priv(host);
1911 }
1912
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001913 if (qla2x00_reset_active(vha)) {
1914 ql_dbg(ql_dbg_user, vha, 0x709f,
1915 "BSG: ISP abort active/needed -- cmd=%d.\n",
1916 bsg_job->request->msgcode);
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001917 return -EBUSY;
1918 }
1919
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001920 ql_dbg(ql_dbg_user, vha, 0x7000,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001921 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001922
1923 switch (bsg_job->request->msgcode) {
1924 case FC_BSG_RPT_ELS:
1925 case FC_BSG_HST_ELS_NOLOGIN:
1926 ret = qla2x00_process_els(bsg_job);
1927 break;
1928 case FC_BSG_HST_CT:
1929 ret = qla2x00_process_ct(bsg_job);
1930 break;
1931 case FC_BSG_HST_VENDOR:
1932 ret = qla2x00_process_vendor_specific(bsg_job);
1933 break;
1934 case FC_BSG_HST_ADD_RPORT:
1935 case FC_BSG_HST_DEL_RPORT:
1936 case FC_BSG_RPT_CT:
1937 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001938 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001939 break;
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001940 }
Giridhar Malavali6e980162010-03-19 17:03:58 -07001941 return ret;
1942}
1943
1944int
1945qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
1946{
1947 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
1948 struct qla_hw_data *ha = vha->hw;
1949 srb_t *sp;
1950 int cnt, que;
1951 unsigned long flags;
1952 struct req_que *req;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001953
1954 /* find the bsg job from the active list of commands */
1955 spin_lock_irqsave(&ha->hardware_lock, flags);
1956 for (que = 0; que < ha->max_req_queues; que++) {
1957 req = ha->req_q_map[que];
1958 if (!req)
1959 continue;
1960
Chad Dupuis8d93f552013-01-30 03:34:37 -05001961 for (cnt = 1; cnt < req->num_outstanding_cmds; cnt++) {
Giridhar Malavali6e980162010-03-19 17:03:58 -07001962 sp = req->outstanding_cmds[cnt];
Giridhar Malavali6e980162010-03-19 17:03:58 -07001963 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001964 if (((sp->type == SRB_CT_CMD) ||
1965 (sp->type == SRB_ELS_CMD_HST))
1966 && (sp->u.bsg_job == bsg_job)) {
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001967 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001968 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001969 ql_log(ql_log_warn, vha, 0x7089,
1970 "mbx abort_command "
1971 "failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001972 bsg_job->req->errors =
1973 bsg_job->reply->result = -EIO;
1974 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001975 ql_dbg(ql_dbg_user, vha, 0x708a,
1976 "mbx abort_command "
1977 "success.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001978 bsg_job->req->errors =
1979 bsg_job->reply->result = 0;
1980 }
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001981 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001982 goto done;
1983 }
1984 }
1985 }
1986 }
1987 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001988 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001989 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
1990 return 0;
1991
1992done:
1993 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1994 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
1995 kfree(sp->fcport);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001996 mempool_free(sp, ha->srb_mempool);
1997 return 0;
1998}