blob: 05520be511b6a7a94508c6e4e61ee12213bd738e [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;
746 goto done_free_dma_req;
747 }
748
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400749 ql_dbg(ql_dbg_user, vha, 0x70c0,
750 "elreq.options=%04x\n", elreq.options);
751
752 if (elreq.options == EXTERNAL_LOOPBACK)
753 if (IS_QLA8031(ha))
754 rval = qla81xx_set_loopback_mode(vha,
755 config, new_config, elreq.options);
756 else
757 rval = qla81xx_reset_loopback_mode(vha,
758 config, 1);
759 else
760 rval = qla81xx_set_loopback_mode(vha, config,
761 new_config, elreq.options);
762
763 if (rval) {
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400764 rval = -EPERM;
765 goto done_free_dma_req;
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700766 }
767
768 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700769 ql_dbg(ql_dbg_user, vha, 0x7028,
770 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700771
772 command_sent = INT_DEF_LB_LOOPBACK_CMD;
773 rval = qla2x00_loopback_test(vha, &elreq, response);
774
Joe Carnuccio4052bd52010-12-21 16:00:17 -0800775 if (new_config[0]) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700776 /* Revert back to original port config
777 * Also clear internal loopback
778 */
Chad Dupuis8fcd6b82012-08-22 14:21:06 -0400779 qla81xx_reset_loopback_mode(vha,
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700780 new_config, 0);
781 }
782
783 if (response[0] == MBS_COMMAND_ERROR &&
784 response[1] == MBS_LB_RESET) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700785 ql_log(ql_log_warn, vha, 0x7029,
786 "MBX command error, Aborting ISP.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700787 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
788 qla2xxx_wake_dpc(vha);
789 qla2x00_wait_for_chip_reset(vha);
790 /* Also reset the MPI */
791 if (qla81xx_restart_mpi_firmware(vha) !=
792 QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700793 ql_log(ql_log_warn, vha, 0x702a,
794 "MPI reset failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700795 }
796
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700797 rval = -EIO;
798 goto done_free_dma_req;
799 }
800 } else {
801 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700802 ql_dbg(ql_dbg_user, vha, 0x702b,
803 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700804 command_sent = INT_DEF_LB_LOOPBACK_CMD;
805 rval = qla2x00_loopback_test(vha, &elreq, response);
806 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700807 }
808
809 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700810 ql_log(ql_log_warn, vha, 0x702c,
811 "Vendor request %s failed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700812
Giridhar Malavali6e980162010-03-19 17:03:58 -0700813 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700814 bsg_job->reply->result = (DID_ERROR << 16);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500815 bsg_job->reply->reply_payload_rcv_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700816 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700817 ql_dbg(ql_dbg_user, vha, 0x702d,
818 "Vendor request %s completed.\n", type);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500819 bsg_job->reply->result = (DID_OK << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700820 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
821 bsg_job->reply_payload.sg_cnt, rsp_data,
822 rsp_data_len);
823 }
Armen Baloyan63ea9232012-11-21 02:39:53 -0500824
825 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
826 sizeof(response) + sizeof(uint8_t);
827 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
828 sizeof(struct fc_bsg_reply);
829 memcpy(fw_sts_ptr, response, sizeof(response));
830 fw_sts_ptr += sizeof(response);
831 *fw_sts_ptr = command_sent;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700832
833 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
834 rsp_data, rsp_data_dma);
835done_free_dma_req:
836 dma_free_coherent(&ha->pdev->dev, req_data_len,
837 req_data, req_data_dma);
838done_unmap_sg:
839 dma_unmap_sg(&ha->pdev->dev,
840 bsg_job->reply_payload.sg_list,
841 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
842done_unmap_req_sg:
843 dma_unmap_sg(&ha->pdev->dev,
844 bsg_job->request_payload.sg_list,
845 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Armen Baloyan63ea9232012-11-21 02:39:53 -0500846 if (!rval)
847 bsg_job->job_done(bsg_job);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700848 return rval;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700849}
850
851static int
852qla84xx_reset(struct fc_bsg_job *bsg_job)
853{
854 struct Scsi_Host *host = bsg_job->shost;
855 scsi_qla_host_t *vha = shost_priv(host);
856 struct qla_hw_data *ha = vha->hw;
857 int rval = 0;
858 uint32_t flag;
859
Giridhar Malavali6e980162010-03-19 17:03:58 -0700860 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700861 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700862 return -EINVAL;
863 }
864
865 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
866
867 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
868
869 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700870 ql_log(ql_log_warn, vha, 0x7030,
871 "Vendor request 84xx reset failed.\n");
Armen Baloyan63ea9232012-11-21 02:39:53 -0500872 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700873
874 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700875 ql_dbg(ql_dbg_user, vha, 0x7031,
876 "Vendor request 84xx reset completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700877 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -0500878 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700879 }
880
Giridhar Malavali6e980162010-03-19 17:03:58 -0700881 return rval;
882}
883
884static int
885qla84xx_updatefw(struct fc_bsg_job *bsg_job)
886{
887 struct Scsi_Host *host = bsg_job->shost;
888 scsi_qla_host_t *vha = shost_priv(host);
889 struct qla_hw_data *ha = vha->hw;
890 struct verify_chip_entry_84xx *mn = NULL;
891 dma_addr_t mn_dma, fw_dma;
892 void *fw_buf = NULL;
893 int rval = 0;
894 uint32_t sg_cnt;
895 uint32_t data_len;
896 uint16_t options;
897 uint32_t flag;
898 uint32_t fw_ver;
899
Giridhar Malavali6e980162010-03-19 17:03:58 -0700900 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700901 ql_dbg(ql_dbg_user, vha, 0x7032,
902 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700903 return -EINVAL;
904 }
905
906 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
907 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700908 if (!sg_cnt) {
909 ql_log(ql_log_warn, vha, 0x7033,
910 "dma_map_sg returned %d for request.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700911 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700912 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700913
914 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700915 ql_log(ql_log_warn, vha, 0x7034,
916 "DMA mapping resulted in different sg counts, "
917 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
918 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700919 rval = -EAGAIN;
920 goto done_unmap_sg;
921 }
922
923 data_len = bsg_job->request_payload.payload_len;
924 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
925 &fw_dma, GFP_KERNEL);
926 if (!fw_buf) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700927 ql_log(ql_log_warn, vha, 0x7035,
928 "DMA alloc failed for fw_buf.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700929 rval = -ENOMEM;
930 goto done_unmap_sg;
931 }
932
933 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
934 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
935
936 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
937 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700938 ql_log(ql_log_warn, vha, 0x7036,
939 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700940 rval = -ENOMEM;
941 goto done_free_fw_buf;
942 }
943
944 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
945 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
946
947 memset(mn, 0, sizeof(struct access_chip_84xx));
948 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
949 mn->entry_count = 1;
950
951 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
952 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
953 options |= VCO_DIAG_FW;
954
955 mn->options = cpu_to_le16(options);
956 mn->fw_ver = cpu_to_le32(fw_ver);
957 mn->fw_size = cpu_to_le32(data_len);
958 mn->fw_seq_size = cpu_to_le32(data_len);
959 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
960 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
961 mn->dseg_length = cpu_to_le32(data_len);
962 mn->data_seg_cnt = cpu_to_le16(1);
963
964 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
965
966 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700967 ql_log(ql_log_warn, vha, 0x7037,
968 "Vendor request 84xx updatefw failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700969
Armen Baloyan63ea9232012-11-21 02:39:53 -0500970 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700971 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700972 ql_dbg(ql_dbg_user, vha, 0x7038,
973 "Vendor request 84xx updatefw completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700974
975 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
976 bsg_job->reply->result = DID_OK;
977 }
978
Giridhar Malavali6e980162010-03-19 17:03:58 -0700979 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
980
981done_free_fw_buf:
982 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
983
984done_unmap_sg:
985 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
986 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
987
Armen Baloyan63ea9232012-11-21 02:39:53 -0500988 if (!rval)
989 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700990 return rval;
991}
992
993static int
994qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
995{
996 struct Scsi_Host *host = bsg_job->shost;
997 scsi_qla_host_t *vha = shost_priv(host);
998 struct qla_hw_data *ha = vha->hw;
999 struct access_chip_84xx *mn = NULL;
1000 dma_addr_t mn_dma, mgmt_dma;
1001 void *mgmt_b = NULL;
1002 int rval = 0;
1003 struct qla_bsg_a84_mgmt *ql84_mgmt;
1004 uint32_t sg_cnt;
Harish Zunjarraod5459082010-03-19 17:04:00 -07001005 uint32_t data_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001006 uint32_t dma_direction = DMA_NONE;
1007
Giridhar Malavali6e980162010-03-19 17:03:58 -07001008 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001009 ql_log(ql_log_warn, vha, 0x703a,
1010 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001011 return -EINVAL;
1012 }
1013
1014 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1015 sizeof(struct fc_bsg_request));
1016 if (!ql84_mgmt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001017 ql_log(ql_log_warn, vha, 0x703b,
1018 "MGMT header not provided, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001019 return -EINVAL;
1020 }
1021
1022 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1023 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001024 ql_log(ql_log_warn, vha, 0x703c,
1025 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001026 return -ENOMEM;
1027 }
1028
1029 memset(mn, 0, sizeof(struct access_chip_84xx));
1030 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1031 mn->entry_count = 1;
1032
1033 switch (ql84_mgmt->mgmt.cmd) {
1034 case QLA84_MGMT_READ_MEM:
1035 case QLA84_MGMT_GET_INFO:
1036 sg_cnt = dma_map_sg(&ha->pdev->dev,
1037 bsg_job->reply_payload.sg_list,
1038 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1039 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001040 ql_log(ql_log_warn, vha, 0x703d,
1041 "dma_map_sg returned %d for reply.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001042 rval = -ENOMEM;
1043 goto exit_mgmt;
1044 }
1045
1046 dma_direction = DMA_FROM_DEVICE;
1047
1048 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001049 ql_log(ql_log_warn, vha, 0x703e,
1050 "DMA mapping resulted in different sg counts, "
1051 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1052 bsg_job->reply_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001053 rval = -EAGAIN;
1054 goto done_unmap_sg;
1055 }
1056
1057 data_len = bsg_job->reply_payload.payload_len;
1058
1059 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1060 &mgmt_dma, GFP_KERNEL);
1061 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001062 ql_log(ql_log_warn, vha, 0x703f,
1063 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001064 rval = -ENOMEM;
1065 goto done_unmap_sg;
1066 }
1067
1068 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1069 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1070 mn->parameter1 =
1071 cpu_to_le32(
1072 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1073
1074 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1075 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1076 mn->parameter1 =
1077 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1078
1079 mn->parameter2 =
1080 cpu_to_le32(
1081 ql84_mgmt->mgmt.mgmtp.u.info.context);
1082 }
1083 break;
1084
1085 case QLA84_MGMT_WRITE_MEM:
1086 sg_cnt = dma_map_sg(&ha->pdev->dev,
1087 bsg_job->request_payload.sg_list,
1088 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1089
1090 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001091 ql_log(ql_log_warn, vha, 0x7040,
1092 "dma_map_sg returned %d.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001093 rval = -ENOMEM;
1094 goto exit_mgmt;
1095 }
1096
1097 dma_direction = DMA_TO_DEVICE;
1098
1099 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001100 ql_log(ql_log_warn, vha, 0x7041,
1101 "DMA mapping resulted in different sg counts, "
1102 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1103 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001104 rval = -EAGAIN;
1105 goto done_unmap_sg;
1106 }
1107
1108 data_len = bsg_job->request_payload.payload_len;
1109 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1110 &mgmt_dma, GFP_KERNEL);
1111 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001112 ql_log(ql_log_warn, vha, 0x7042,
1113 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001114 rval = -ENOMEM;
1115 goto done_unmap_sg;
1116 }
1117
1118 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1119 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1120
1121 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1122 mn->parameter1 =
1123 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1124 break;
1125
1126 case QLA84_MGMT_CHNG_CONFIG:
1127 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1128 mn->parameter1 =
1129 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1130
1131 mn->parameter2 =
1132 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1133
1134 mn->parameter3 =
1135 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1136 break;
1137
1138 default:
1139 rval = -EIO;
1140 goto exit_mgmt;
1141 }
1142
1143 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1144 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1145 mn->dseg_count = cpu_to_le16(1);
1146 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1147 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1148 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1149 }
1150
1151 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1152
1153 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001154 ql_log(ql_log_warn, vha, 0x7043,
1155 "Vendor request 84xx mgmt failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001156
Armen Baloyan63ea9232012-11-21 02:39:53 -05001157 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001158
1159 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001160 ql_dbg(ql_dbg_user, vha, 0x7044,
1161 "Vendor request 84xx mgmt completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001162
1163 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1164 bsg_job->reply->result = DID_OK;
1165
1166 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1167 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1168 bsg_job->reply->reply_payload_rcv_len =
1169 bsg_job->reply_payload.payload_len;
1170
1171 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001172 bsg_job->reply_payload.sg_cnt, mgmt_b,
1173 data_len);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001174 }
1175 }
1176
Giridhar Malavali6e980162010-03-19 17:03:58 -07001177done_unmap_sg:
Harish Zunjarraod5459082010-03-19 17:04:00 -07001178 if (mgmt_b)
1179 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1180
Giridhar Malavali6e980162010-03-19 17:03:58 -07001181 if (dma_direction == DMA_TO_DEVICE)
1182 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1183 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1184 else if (dma_direction == DMA_FROM_DEVICE)
1185 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1186 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1187
1188exit_mgmt:
1189 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1190
Armen Baloyan63ea9232012-11-21 02:39:53 -05001191 if (!rval)
1192 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001193 return rval;
1194}
1195
1196static int
1197qla24xx_iidma(struct fc_bsg_job *bsg_job)
1198{
1199 struct Scsi_Host *host = bsg_job->shost;
1200 scsi_qla_host_t *vha = shost_priv(host);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001201 int rval = 0;
1202 struct qla_port_param *port_param = NULL;
1203 fc_port_t *fcport = NULL;
1204 uint16_t mb[MAILBOX_REGISTER_COUNT];
1205 uint8_t *rsp_ptr = NULL;
1206
Giridhar Malavali6e980162010-03-19 17:03:58 -07001207 if (!IS_IIDMA_CAPABLE(vha->hw)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001208 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001209 return -EINVAL;
1210 }
1211
1212 port_param = (struct qla_port_param *)((char *)bsg_job->request +
1213 sizeof(struct fc_bsg_request));
1214 if (!port_param) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001215 ql_log(ql_log_warn, vha, 0x7047,
1216 "port_param header not provided.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001217 return -EINVAL;
1218 }
1219
1220 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001221 ql_log(ql_log_warn, vha, 0x7048,
1222 "Invalid destination type.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001223 return -EINVAL;
1224 }
1225
1226 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1227 if (fcport->port_type != FCT_TARGET)
1228 continue;
1229
1230 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1231 fcport->port_name, sizeof(fcport->port_name)))
1232 continue;
1233 break;
1234 }
1235
1236 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001237 ql_log(ql_log_warn, vha, 0x7049,
1238 "Failed to find port.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001239 return -EINVAL;
1240 }
1241
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07001242 if (atomic_read(&fcport->state) != FCS_ONLINE) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001243 ql_log(ql_log_warn, vha, 0x704a,
1244 "Port is not online.\n");
Madhuranath Iyengar17cf2c52010-07-23 15:28:22 +05001245 return -EINVAL;
1246 }
1247
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001248 if (fcport->flags & FCF_LOGIN_NEEDED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001249 ql_log(ql_log_warn, vha, 0x704b,
1250 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001251 return -EINVAL;
1252 }
1253
Giridhar Malavali6e980162010-03-19 17:03:58 -07001254 if (port_param->mode)
1255 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1256 port_param->speed, mb);
1257 else
1258 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1259 &port_param->speed, mb);
1260
1261 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001262 ql_log(ql_log_warn, vha, 0x704c,
1263 "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
1264 "%04x %x %04x %04x.\n", fcport->port_name[0],
1265 fcport->port_name[1], fcport->port_name[2],
1266 fcport->port_name[3], fcport->port_name[4],
1267 fcport->port_name[5], fcport->port_name[6],
1268 fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
Armen Baloyan63ea9232012-11-21 02:39:53 -05001269 rval = (DID_ERROR << 16);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001270 } else {
1271 if (!port_param->mode) {
1272 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1273 sizeof(struct qla_port_param);
1274
1275 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1276 sizeof(struct fc_bsg_reply);
1277
1278 memcpy(rsp_ptr, port_param,
1279 sizeof(struct qla_port_param));
1280 }
1281
1282 bsg_job->reply->result = DID_OK;
Armen Baloyan63ea9232012-11-21 02:39:53 -05001283 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001284 }
1285
Giridhar Malavali6e980162010-03-19 17:03:58 -07001286 return rval;
1287}
1288
1289static int
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001290qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001291 uint8_t is_update)
1292{
1293 uint32_t start = 0;
1294 int valid = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001295 struct qla_hw_data *ha = vha->hw;
Harish Zunjarraof19af162010-10-15 11:27:43 -07001296
Harish Zunjarraof19af162010-10-15 11:27:43 -07001297 if (unlikely(pci_channel_offline(ha->pdev)))
1298 return -EINVAL;
1299
1300 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001301 if (start > ha->optrom_size) {
1302 ql_log(ql_log_warn, vha, 0x7055,
1303 "start %d > optrom_size %d.\n", start, ha->optrom_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001304 return -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001305 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001306
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001307 if (ha->optrom_state != QLA_SWAITING) {
1308 ql_log(ql_log_info, vha, 0x7056,
1309 "optrom_state %d.\n", ha->optrom_state);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001310 return -EBUSY;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001311 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001312
1313 ha->optrom_region_start = start;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001314 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001315 if (is_update) {
1316 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1317 valid = 1;
1318 else if (start == (ha->flt_region_boot * 4) ||
1319 start == (ha->flt_region_fw * 4))
1320 valid = 1;
1321 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001322 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
Harish Zunjarraof19af162010-10-15 11:27:43 -07001323 valid = 1;
1324 if (!valid) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001325 ql_log(ql_log_warn, vha, 0x7058,
1326 "Invalid start region 0x%x/0x%x.\n", start,
1327 bsg_job->request_payload.payload_len);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001328 return -EINVAL;
1329 }
1330
1331 ha->optrom_region_size = start +
1332 bsg_job->request_payload.payload_len > ha->optrom_size ?
1333 ha->optrom_size - start :
1334 bsg_job->request_payload.payload_len;
1335 ha->optrom_state = QLA_SWRITING;
1336 } else {
1337 ha->optrom_region_size = start +
1338 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1339 ha->optrom_size - start :
1340 bsg_job->reply_payload.payload_len;
1341 ha->optrom_state = QLA_SREADING;
1342 }
1343
1344 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1345 if (!ha->optrom_buffer) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001346 ql_log(ql_log_warn, vha, 0x7059,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001347 "Read: Unable to allocate memory for optrom retrieval "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001348 "(%x)\n", ha->optrom_region_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001349
1350 ha->optrom_state = QLA_SWAITING;
1351 return -ENOMEM;
1352 }
1353
1354 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1355 return 0;
1356}
1357
1358static int
1359qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1360{
1361 struct Scsi_Host *host = bsg_job->shost;
1362 scsi_qla_host_t *vha = shost_priv(host);
1363 struct qla_hw_data *ha = vha->hw;
1364 int rval = 0;
1365
Santosh Vernekar7d613ac2012-08-22 14:21:03 -04001366 if (ha->flags.nic_core_reset_hdlr_active)
Giridhar Malavalia49393f2012-04-25 07:26:14 -07001367 return -EBUSY;
1368
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001369 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001370 if (rval)
1371 return rval;
1372
1373 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1374 ha->optrom_region_start, ha->optrom_region_size);
1375
1376 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1377 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1378 ha->optrom_region_size);
1379
1380 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1381 bsg_job->reply->result = DID_OK;
1382 vfree(ha->optrom_buffer);
1383 ha->optrom_buffer = NULL;
1384 ha->optrom_state = QLA_SWAITING;
1385 bsg_job->job_done(bsg_job);
1386 return rval;
1387}
1388
1389static int
1390qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1391{
1392 struct Scsi_Host *host = bsg_job->shost;
1393 scsi_qla_host_t *vha = shost_priv(host);
1394 struct qla_hw_data *ha = vha->hw;
1395 int rval = 0;
1396
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001397 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001398 if (rval)
1399 return rval;
1400
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04001401 /* Set the isp82xx_no_md_cap not to capture minidump */
1402 ha->flags.isp82xx_no_md_cap = 1;
1403
Harish Zunjarraof19af162010-10-15 11:27:43 -07001404 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1405 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1406 ha->optrom_region_size);
1407
1408 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1409 ha->optrom_region_start, ha->optrom_region_size);
1410
1411 bsg_job->reply->result = DID_OK;
1412 vfree(ha->optrom_buffer);
1413 ha->optrom_buffer = NULL;
1414 ha->optrom_state = QLA_SWAITING;
1415 bsg_job->job_done(bsg_job);
1416 return rval;
1417}
1418
1419static int
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001420qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1421{
1422 struct Scsi_Host *host = bsg_job->shost;
1423 scsi_qla_host_t *vha = shost_priv(host);
1424 struct qla_hw_data *ha = vha->hw;
1425 int rval = 0;
1426 uint8_t bsg[DMA_POOL_SIZE];
1427 struct qla_image_version_list *list = (void *)bsg;
1428 struct qla_image_version *image;
1429 uint32_t count;
1430 dma_addr_t sfp_dma;
1431 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1432 if (!sfp) {
1433 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1434 EXT_STATUS_NO_MEMORY;
1435 goto done;
1436 }
1437
1438 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1439 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1440
1441 image = list->version;
1442 count = list->count;
1443 while (count--) {
1444 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1445 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1446 image->field_address.device, image->field_address.offset,
1447 sizeof(image->field_info), image->field_address.option);
1448 if (rval) {
1449 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1450 EXT_STATUS_MAILBOX;
1451 goto dealloc;
1452 }
1453 image++;
1454 }
1455
1456 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1457
1458dealloc:
1459 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1460
1461done:
1462 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1463 bsg_job->reply->result = DID_OK << 16;
1464 bsg_job->job_done(bsg_job);
1465
1466 return 0;
1467}
1468
1469static int
1470qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1471{
1472 struct Scsi_Host *host = bsg_job->shost;
1473 scsi_qla_host_t *vha = shost_priv(host);
1474 struct qla_hw_data *ha = vha->hw;
1475 int rval = 0;
1476 uint8_t bsg[DMA_POOL_SIZE];
1477 struct qla_status_reg *sr = (void *)bsg;
1478 dma_addr_t sfp_dma;
1479 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1480 if (!sfp) {
1481 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1482 EXT_STATUS_NO_MEMORY;
1483 goto done;
1484 }
1485
1486 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1487 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1488
1489 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1490 sr->field_address.device, sr->field_address.offset,
1491 sizeof(sr->status_reg), sr->field_address.option);
1492 sr->status_reg = *sfp;
1493
1494 if (rval) {
1495 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1496 EXT_STATUS_MAILBOX;
1497 goto dealloc;
1498 }
1499
1500 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1501 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1502
1503 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1504
1505dealloc:
1506 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1507
1508done:
1509 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1510 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1511 bsg_job->reply->result = DID_OK << 16;
1512 bsg_job->job_done(bsg_job);
1513
1514 return 0;
1515}
1516
1517static int
1518qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1519{
1520 struct Scsi_Host *host = bsg_job->shost;
1521 scsi_qla_host_t *vha = shost_priv(host);
1522 struct qla_hw_data *ha = vha->hw;
1523 int rval = 0;
1524 uint8_t bsg[DMA_POOL_SIZE];
1525 struct qla_status_reg *sr = (void *)bsg;
1526 dma_addr_t sfp_dma;
1527 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1528 if (!sfp) {
1529 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1530 EXT_STATUS_NO_MEMORY;
1531 goto done;
1532 }
1533
1534 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1535 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1536
1537 *sfp = sr->status_reg;
1538 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1539 sr->field_address.device, sr->field_address.offset,
1540 sizeof(sr->status_reg), sr->field_address.option);
1541
1542 if (rval) {
1543 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1544 EXT_STATUS_MAILBOX;
1545 goto dealloc;
1546 }
1547
1548 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1549
1550dealloc:
1551 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1552
1553done:
1554 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1555 bsg_job->reply->result = DID_OK << 16;
1556 bsg_job->job_done(bsg_job);
1557
1558 return 0;
1559}
1560
1561static int
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001562qla2x00_write_i2c(struct fc_bsg_job *bsg_job)
1563{
1564 struct Scsi_Host *host = bsg_job->shost;
1565 scsi_qla_host_t *vha = shost_priv(host);
1566 struct qla_hw_data *ha = vha->hw;
1567 int rval = 0;
1568 uint8_t bsg[DMA_POOL_SIZE];
1569 struct qla_i2c_access *i2c = (void *)bsg;
1570 dma_addr_t sfp_dma;
1571 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1572 if (!sfp) {
1573 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1574 EXT_STATUS_NO_MEMORY;
1575 goto done;
1576 }
1577
1578 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1579 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1580
1581 memcpy(sfp, i2c->buffer, i2c->length);
1582 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1583 i2c->device, i2c->offset, i2c->length, i2c->option);
1584
1585 if (rval) {
1586 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1587 EXT_STATUS_MAILBOX;
1588 goto dealloc;
1589 }
1590
1591 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1592
1593dealloc:
1594 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1595
1596done:
1597 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1598 bsg_job->reply->result = DID_OK << 16;
1599 bsg_job->job_done(bsg_job);
1600
1601 return 0;
1602}
1603
1604static int
1605qla2x00_read_i2c(struct fc_bsg_job *bsg_job)
1606{
1607 struct Scsi_Host *host = bsg_job->shost;
1608 scsi_qla_host_t *vha = shost_priv(host);
1609 struct qla_hw_data *ha = vha->hw;
1610 int rval = 0;
1611 uint8_t bsg[DMA_POOL_SIZE];
1612 struct qla_i2c_access *i2c = (void *)bsg;
1613 dma_addr_t sfp_dma;
1614 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1615 if (!sfp) {
1616 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1617 EXT_STATUS_NO_MEMORY;
1618 goto done;
1619 }
1620
1621 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1622 bsg_job->request_payload.sg_cnt, i2c, sizeof(*i2c));
1623
1624 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1625 i2c->device, i2c->offset, i2c->length, i2c->option);
1626
1627 if (rval) {
1628 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1629 EXT_STATUS_MAILBOX;
1630 goto dealloc;
1631 }
1632
1633 memcpy(i2c->buffer, sfp, i2c->length);
1634 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1635 bsg_job->reply_payload.sg_cnt, i2c, sizeof(*i2c));
1636
1637 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1638
1639dealloc:
1640 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1641
1642done:
1643 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1644 bsg_job->reply->reply_payload_rcv_len = sizeof(*i2c);
1645 bsg_job->reply->result = DID_OK << 16;
1646 bsg_job->job_done(bsg_job);
1647
1648 return 0;
1649}
1650
1651static int
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001652qla24xx_process_bidir_cmd(struct fc_bsg_job *bsg_job)
1653{
1654 struct Scsi_Host *host = bsg_job->shost;
1655 scsi_qla_host_t *vha = shost_priv(host);
1656 struct qla_hw_data *ha = vha->hw;
1657 uint16_t thread_id;
1658 uint32_t rval = EXT_STATUS_OK;
1659 uint16_t req_sg_cnt = 0;
1660 uint16_t rsp_sg_cnt = 0;
1661 uint16_t nextlid = 0;
1662 uint32_t tot_dsds;
1663 srb_t *sp = NULL;
1664 uint32_t req_data_len = 0;
1665 uint32_t rsp_data_len = 0;
1666
1667 /* Check the type of the adapter */
1668 if (!IS_BIDI_CAPABLE(ha)) {
1669 ql_log(ql_log_warn, vha, 0x70a0,
1670 "This adapter is not supported\n");
1671 rval = EXT_STATUS_NOT_SUPPORTED;
1672 goto done;
1673 }
1674
1675 if (test_bit(ISP_ABORT_NEEDED, &vha->dpc_flags) ||
1676 test_bit(ABORT_ISP_ACTIVE, &vha->dpc_flags) ||
1677 test_bit(ISP_ABORT_RETRY, &vha->dpc_flags)) {
1678 rval = EXT_STATUS_BUSY;
1679 goto done;
1680 }
1681
1682 /* Check if host is online */
1683 if (!vha->flags.online) {
1684 ql_log(ql_log_warn, vha, 0x70a1,
1685 "Host is not online\n");
1686 rval = EXT_STATUS_DEVICE_OFFLINE;
1687 goto done;
1688 }
1689
1690 /* Check if cable is plugged in or not */
1691 if (vha->device_flags & DFLG_NO_CABLE) {
1692 ql_log(ql_log_warn, vha, 0x70a2,
1693 "Cable is unplugged...\n");
1694 rval = EXT_STATUS_INVALID_CFG;
1695 goto done;
1696 }
1697
1698 /* Check if the switch is connected or not */
1699 if (ha->current_topology != ISP_CFG_F) {
1700 ql_log(ql_log_warn, vha, 0x70a3,
1701 "Host is not connected to the switch\n");
1702 rval = EXT_STATUS_INVALID_CFG;
1703 goto done;
1704 }
1705
1706 /* Check if operating mode is P2P */
1707 if (ha->operating_mode != P2P) {
1708 ql_log(ql_log_warn, vha, 0x70a4,
1709 "Host is operating mode is not P2p\n");
1710 rval = EXT_STATUS_INVALID_CFG;
1711 goto done;
1712 }
1713
1714 thread_id = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
1715
1716 mutex_lock(&ha->selflogin_lock);
1717 if (vha->self_login_loop_id == 0) {
1718 /* Initialize all required fields of fcport */
1719 vha->bidir_fcport.vha = vha;
1720 vha->bidir_fcport.d_id.b.al_pa = vha->d_id.b.al_pa;
1721 vha->bidir_fcport.d_id.b.area = vha->d_id.b.area;
1722 vha->bidir_fcport.d_id.b.domain = vha->d_id.b.domain;
1723 vha->bidir_fcport.loop_id = vha->loop_id;
1724
1725 if (qla2x00_fabric_login(vha, &(vha->bidir_fcport), &nextlid)) {
1726 ql_log(ql_log_warn, vha, 0x70a7,
1727 "Failed to login port %06X for bidirectional IOCB\n",
1728 vha->bidir_fcport.d_id.b24);
1729 mutex_unlock(&ha->selflogin_lock);
1730 rval = EXT_STATUS_MAILBOX;
1731 goto done;
1732 }
1733 vha->self_login_loop_id = nextlid - 1;
1734
1735 }
1736 /* Assign the self login loop id to fcport */
1737 mutex_unlock(&ha->selflogin_lock);
1738
1739 vha->bidir_fcport.loop_id = vha->self_login_loop_id;
1740
1741 req_sg_cnt = dma_map_sg(&ha->pdev->dev,
1742 bsg_job->request_payload.sg_list,
1743 bsg_job->request_payload.sg_cnt,
1744 DMA_TO_DEVICE);
1745
1746 if (!req_sg_cnt) {
1747 rval = EXT_STATUS_NO_MEMORY;
1748 goto done;
1749 }
1750
1751 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
1752 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
1753 DMA_FROM_DEVICE);
1754
1755 if (!rsp_sg_cnt) {
1756 rval = EXT_STATUS_NO_MEMORY;
1757 goto done_unmap_req_sg;
1758 }
1759
1760 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
1761 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
1762 ql_dbg(ql_dbg_user, vha, 0x70a9,
1763 "Dma mapping resulted in different sg counts "
1764 "[request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt: "
1765 "%x dma_reply_sg_cnt: %x]\n",
1766 bsg_job->request_payload.sg_cnt, req_sg_cnt,
1767 bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
1768 rval = EXT_STATUS_NO_MEMORY;
1769 goto done_unmap_sg;
1770 }
1771
1772 if (req_data_len != rsp_data_len) {
1773 rval = EXT_STATUS_BUSY;
1774 ql_log(ql_log_warn, vha, 0x70aa,
1775 "req_data_len != rsp_data_len\n");
1776 goto done_unmap_sg;
1777 }
1778
1779 req_data_len = bsg_job->request_payload.payload_len;
1780 rsp_data_len = bsg_job->reply_payload.payload_len;
1781
1782
1783 /* Alloc SRB structure */
1784 sp = qla2x00_get_sp(vha, &(vha->bidir_fcport), GFP_KERNEL);
1785 if (!sp) {
1786 ql_dbg(ql_dbg_user, vha, 0x70ac,
1787 "Alloc SRB structure failed\n");
1788 rval = EXT_STATUS_NO_MEMORY;
1789 goto done_unmap_sg;
1790 }
1791
1792 /*Populate srb->ctx with bidir ctx*/
1793 sp->u.bsg_job = bsg_job;
1794 sp->free = qla2x00_bsg_sp_free;
1795 sp->type = SRB_BIDI_CMD;
1796 sp->done = qla2x00_bsg_job_done;
1797
1798 /* Add the read and write sg count */
1799 tot_dsds = rsp_sg_cnt + req_sg_cnt;
1800
1801 rval = qla2x00_start_bidir(sp, vha, tot_dsds);
1802 if (rval != EXT_STATUS_OK)
1803 goto done_free_srb;
1804 /* the bsg request will be completed in the interrupt handler */
1805 return rval;
1806
1807done_free_srb:
1808 mempool_free(sp, ha->srb_mempool);
1809done_unmap_sg:
1810 dma_unmap_sg(&ha->pdev->dev,
1811 bsg_job->reply_payload.sg_list,
1812 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1813done_unmap_req_sg:
1814 dma_unmap_sg(&ha->pdev->dev,
1815 bsg_job->request_payload.sg_list,
1816 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1817done:
1818
1819 /* Return an error vendor specific response
1820 * and complete the bsg request
1821 */
1822 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = rval;
1823 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1824 bsg_job->reply->reply_payload_rcv_len = 0;
1825 bsg_job->reply->result = (DID_OK) << 16;
1826 bsg_job->job_done(bsg_job);
1827 /* Always retrun success, vendor rsp carries correct status */
1828 return 0;
1829}
1830
1831static int
Giridhar Malavali6e980162010-03-19 17:03:58 -07001832qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
1833{
1834 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
1835 case QL_VND_LOOPBACK:
1836 return qla2x00_process_loopback(bsg_job);
1837
1838 case QL_VND_A84_RESET:
1839 return qla84xx_reset(bsg_job);
1840
1841 case QL_VND_A84_UPDATE_FW:
1842 return qla84xx_updatefw(bsg_job);
1843
1844 case QL_VND_A84_MGMT_CMD:
1845 return qla84xx_mgmt_cmd(bsg_job);
1846
1847 case QL_VND_IIDMA:
1848 return qla24xx_iidma(bsg_job);
1849
Sarang Radke09ff7012010-03-19 17:03:59 -07001850 case QL_VND_FCP_PRIO_CFG_CMD:
1851 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
1852
Harish Zunjarraof19af162010-10-15 11:27:43 -07001853 case QL_VND_READ_FLASH:
1854 return qla2x00_read_optrom(bsg_job);
1855
1856 case QL_VND_UPDATE_FLASH:
1857 return qla2x00_update_optrom(bsg_job);
1858
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001859 case QL_VND_SET_FRU_VERSION:
1860 return qla2x00_update_fru_versions(bsg_job);
1861
1862 case QL_VND_READ_FRU_STATUS:
1863 return qla2x00_read_fru_status(bsg_job);
1864
1865 case QL_VND_WRITE_FRU_STATUS:
1866 return qla2x00_write_fru_status(bsg_job);
1867
Joe Carnuccio9ebb5d92012-08-22 14:20:56 -04001868 case QL_VND_WRITE_I2C:
1869 return qla2x00_write_i2c(bsg_job);
1870
1871 case QL_VND_READ_I2C:
1872 return qla2x00_read_i2c(bsg_job);
1873
Saurav Kashyapa9b6f722012-08-22 14:21:01 -04001874 case QL_VND_DIAG_IO_CMD:
1875 return qla24xx_process_bidir_cmd(bsg_job);
1876
Giridhar Malavali6e980162010-03-19 17:03:58 -07001877 default:
Giridhar Malavali6e980162010-03-19 17:03:58 -07001878 return -ENOSYS;
1879 }
1880}
1881
1882int
1883qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
1884{
1885 int ret = -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001886 struct fc_rport *rport;
1887 fc_port_t *fcport = NULL;
1888 struct Scsi_Host *host;
1889 scsi_qla_host_t *vha;
1890
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08001891 /* In case no data transferred. */
1892 bsg_job->reply->reply_payload_rcv_len = 0;
1893
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001894 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1895 rport = bsg_job->rport;
1896 fcport = *(fc_port_t **) rport->dd_data;
1897 host = rport_to_shost(rport);
1898 vha = shost_priv(host);
1899 } else {
1900 host = bsg_job->shost;
1901 vha = shost_priv(host);
1902 }
1903
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001904 if (qla2x00_reset_active(vha)) {
1905 ql_dbg(ql_dbg_user, vha, 0x709f,
1906 "BSG: ISP abort active/needed -- cmd=%d.\n",
1907 bsg_job->request->msgcode);
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001908 return -EBUSY;
1909 }
1910
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001911 ql_dbg(ql_dbg_user, vha, 0x7000,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001912 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001913
1914 switch (bsg_job->request->msgcode) {
1915 case FC_BSG_RPT_ELS:
1916 case FC_BSG_HST_ELS_NOLOGIN:
1917 ret = qla2x00_process_els(bsg_job);
1918 break;
1919 case FC_BSG_HST_CT:
1920 ret = qla2x00_process_ct(bsg_job);
1921 break;
1922 case FC_BSG_HST_VENDOR:
1923 ret = qla2x00_process_vendor_specific(bsg_job);
1924 break;
1925 case FC_BSG_HST_ADD_RPORT:
1926 case FC_BSG_HST_DEL_RPORT:
1927 case FC_BSG_RPT_CT:
1928 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001929 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001930 break;
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001931 }
Giridhar Malavali6e980162010-03-19 17:03:58 -07001932 return ret;
1933}
1934
1935int
1936qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
1937{
1938 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
1939 struct qla_hw_data *ha = vha->hw;
1940 srb_t *sp;
1941 int cnt, que;
1942 unsigned long flags;
1943 struct req_que *req;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001944
1945 /* find the bsg job from the active list of commands */
1946 spin_lock_irqsave(&ha->hardware_lock, flags);
1947 for (que = 0; que < ha->max_req_queues; que++) {
1948 req = ha->req_q_map[que];
1949 if (!req)
1950 continue;
1951
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001952 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Giridhar Malavali6e980162010-03-19 17:03:58 -07001953 sp = req->outstanding_cmds[cnt];
Giridhar Malavali6e980162010-03-19 17:03:58 -07001954 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001955 if (((sp->type == SRB_CT_CMD) ||
1956 (sp->type == SRB_ELS_CMD_HST))
1957 && (sp->u.bsg_job == bsg_job)) {
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001958 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001959 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001960 ql_log(ql_log_warn, vha, 0x7089,
1961 "mbx abort_command "
1962 "failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001963 bsg_job->req->errors =
1964 bsg_job->reply->result = -EIO;
1965 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001966 ql_dbg(ql_dbg_user, vha, 0x708a,
1967 "mbx abort_command "
1968 "success.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001969 bsg_job->req->errors =
1970 bsg_job->reply->result = 0;
1971 }
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001972 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001973 goto done;
1974 }
1975 }
1976 }
1977 }
1978 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001979 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001980 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
1981 return 0;
1982
1983done:
1984 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1985 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
1986 kfree(sp->fcport);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001987 mempool_free(sp, ha->srb_mempool);
1988 return 0;
1989}