blob: 449ac9c2b9c145addf37deb9c2c3ba0ab94ff156 [file] [log] [blame]
Giridhar Malavali6e980162010-03-19 17:03:58 -07001/*
2 * QLogic Fibre Channel HBA Driver
Andrew Vasquez07e264b2011-03-30 11:46:23 -07003 * Copyright (c) 2003-2011 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:
222 bsg_job->job_done(bsg_job);
223 return ret;
224}
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800225
Giridhar Malavali6e980162010-03-19 17:03:58 -0700226static int
227qla2x00_process_els(struct fc_bsg_job *bsg_job)
228{
229 struct fc_rport *rport;
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500230 fc_port_t *fcport = NULL;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700231 struct Scsi_Host *host;
232 scsi_qla_host_t *vha;
233 struct qla_hw_data *ha;
234 srb_t *sp;
235 const char *type;
236 int req_sg_cnt, rsp_sg_cnt;
237 int rval = (DRIVER_ERROR << 16);
238 uint16_t nextlid = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700239
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500240 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
241 rport = bsg_job->rport;
242 fcport = *(fc_port_t **) rport->dd_data;
243 host = rport_to_shost(rport);
244 vha = shost_priv(host);
245 ha = vha->hw;
246 type = "FC_BSG_RPT_ELS";
247 } else {
248 host = bsg_job->shost;
249 vha = shost_priv(host);
250 ha = vha->hw;
251 type = "FC_BSG_HST_ELS_NOLOGIN";
252 }
253
254 /* pass through is supported only for ISP 4Gb or higher */
255 if (!IS_FWI2_CAPABLE(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700256 ql_dbg(ql_dbg_user, vha, 0x7001,
257 "ELS passthru not supported for ISP23xx based adapters.\n");
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500258 rval = -EPERM;
259 goto done;
260 }
261
Giridhar Malavali6e980162010-03-19 17:03:58 -0700262 /* Multiple SG's are not supported for ELS requests */
263 if (bsg_job->request_payload.sg_cnt > 1 ||
264 bsg_job->reply_payload.sg_cnt > 1) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700265 ql_dbg(ql_dbg_user, vha, 0x7002,
266 "Multiple SG's are not suppored for ELS requests, "
267 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
268 bsg_job->request_payload.sg_cnt,
269 bsg_job->reply_payload.sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700270 rval = -EPERM;
271 goto done;
272 }
273
274 /* ELS request for rport */
275 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700276 /* make sure the rport is logged in,
277 * if not perform fabric login
278 */
279 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700280 ql_dbg(ql_dbg_user, vha, 0x7003,
281 "Failed to login port %06X for ELS passthru.\n",
282 fcport->d_id.b24);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700283 rval = -EIO;
284 goto done;
285 }
286 } else {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700287 /* Allocate a dummy fcport structure, since functions
288 * preparing the IOCB and mailbox command retrieves port
289 * specific information from fcport structure. For Host based
290 * ELS commands there will be no fcport structure allocated
291 */
292 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
293 if (!fcport) {
294 rval = -ENOMEM;
295 goto done;
296 }
297
298 /* Initialize all required fields of fcport */
299 fcport->vha = vha;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700300 fcport->d_id.b.al_pa =
301 bsg_job->request->rqst_data.h_els.port_id[0];
302 fcport->d_id.b.area =
303 bsg_job->request->rqst_data.h_els.port_id[1];
304 fcport->d_id.b.domain =
305 bsg_job->request->rqst_data.h_els.port_id[2];
306 fcport->loop_id =
307 (fcport->d_id.b.al_pa == 0xFD) ?
308 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
309 }
310
311 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700312 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700313 rval = -EIO;
314 goto done;
315 }
316
317 req_sg_cnt =
318 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
319 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
320 if (!req_sg_cnt) {
321 rval = -ENOMEM;
322 goto done_free_fcport;
323 }
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700324
325 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
326 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700327 if (!rsp_sg_cnt) {
328 rval = -ENOMEM;
329 goto done_free_fcport;
330 }
331
332 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700333 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700334 ql_log(ql_log_warn, vha, 0x7008,
335 "dma mapping resulted in different sg counts, "
336 "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
337 "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
338 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700339 rval = -EAGAIN;
340 goto done_unmap_sg;
341 }
342
343 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800344 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700345 if (!sp) {
346 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700347 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700348 }
349
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800350 sp->type =
Giridhar Malavali6e980162010-03-19 17:03:58 -0700351 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
352 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800353 sp->name =
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700354 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
355 "bsg_els_rpt" : "bsg_els_hst");
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800356 sp->u.bsg_job = bsg_job;
357 sp->free = qla2x00_bsg_sp_free;
358 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700359
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700360 ql_dbg(ql_dbg_user, vha, 0x700a,
361 "bsg rqst type: %s els type: %x - loop-id=%x "
362 "portid=%-2x%02x%02x.\n", type,
363 bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
364 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700365
366 rval = qla2x00_start_sp(sp);
367 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700368 ql_log(ql_log_warn, vha, 0x700e,
369 "qla2x00_start_sp failed = %d\n", rval);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700370 mempool_free(sp, ha->srb_mempool);
371 rval = -EIO;
372 goto done_unmap_sg;
373 }
374 return rval;
375
376done_unmap_sg:
377 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
378 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
379 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
380 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
381 goto done_free_fcport;
382
383done_free_fcport:
384 if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
385 kfree(fcport);
386done:
387 return rval;
388}
389
Andrew Vasquez57807902011-11-18 09:03:20 -0800390inline uint16_t
391qla24xx_calc_ct_iocbs(uint16_t dsds)
392{
393 uint16_t iocbs;
394
395 iocbs = 1;
396 if (dsds > 2) {
397 iocbs += (dsds - 2) / 5;
398 if ((dsds - 2) % 5)
399 iocbs++;
400 }
401 return iocbs;
402}
403
Giridhar Malavali6e980162010-03-19 17:03:58 -0700404static int
405qla2x00_process_ct(struct fc_bsg_job *bsg_job)
406{
407 srb_t *sp;
408 struct Scsi_Host *host = bsg_job->shost;
409 scsi_qla_host_t *vha = shost_priv(host);
410 struct qla_hw_data *ha = vha->hw;
411 int rval = (DRIVER_ERROR << 16);
412 int req_sg_cnt, rsp_sg_cnt;
413 uint16_t loop_id;
414 struct fc_port *fcport;
415 char *type = "FC_BSG_HST_CT";
Giridhar Malavali6e980162010-03-19 17:03:58 -0700416
Giridhar Malavali6e980162010-03-19 17:03:58 -0700417 req_sg_cnt =
418 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
419 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700420 if (!req_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700421 ql_log(ql_log_warn, vha, 0x700f,
422 "dma_map_sg return %d for request\n", req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700423 rval = -ENOMEM;
424 goto done;
425 }
426
427 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
428 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
429 if (!rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700430 ql_log(ql_log_warn, vha, 0x7010,
431 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700432 rval = -ENOMEM;
433 goto done;
434 }
435
436 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700437 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700438 ql_log(ql_log_warn, vha, 0x7011,
439 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
440 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
441 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700442 rval = -EAGAIN;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700443 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700444 }
445
446 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700447 ql_log(ql_log_warn, vha, 0x7012,
448 "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700449 rval = -EIO;
450 goto done_unmap_sg;
451 }
452
453 loop_id =
454 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
455 >> 24;
456 switch (loop_id) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700457 case 0xFC:
458 loop_id = cpu_to_le16(NPH_SNS);
459 break;
460 case 0xFA:
461 loop_id = vha->mgmt_svr_loop_id;
462 break;
463 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700464 ql_dbg(ql_dbg_user, vha, 0x7013,
465 "Unknown loop id: %x.\n", loop_id);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700466 rval = -EINVAL;
467 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700468 }
469
470 /* Allocate a dummy fcport structure, since functions preparing the
471 * IOCB and mailbox command retrieves port specific information
472 * from fcport structure. For Host based ELS commands there will be
473 * no fcport structure allocated
474 */
475 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700476 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700477 ql_log(ql_log_warn, vha, 0x7014,
478 "Failed to allocate fcport.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700479 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700480 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700481 }
482
483 /* Initialize all required fields of fcport */
484 fcport->vha = vha;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700485 fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
486 fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
487 fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
488 fcport->loop_id = loop_id;
489
490 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800491 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700492 if (!sp) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700493 ql_log(ql_log_warn, vha, 0x7015,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800494 "qla2x00_get_sp failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700495 rval = -ENOMEM;
496 goto done_free_fcport;
497 }
498
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800499 sp->type = SRB_CT_CMD;
500 sp->name = "bsg_ct";
501 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
502 sp->u.bsg_job = bsg_job;
503 sp->free = qla2x00_bsg_sp_free;
504 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700505
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700506 ql_dbg(ql_dbg_user, vha, 0x7016,
507 "bsg rqst type: %s else type: %x - "
508 "loop-id=%x portid=%02x%02x%02x.\n", type,
509 (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
510 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
511 fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700512
513 rval = qla2x00_start_sp(sp);
514 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700515 ql_log(ql_log_warn, vha, 0x7017,
516 "qla2x00_start_sp failed=%d.\n", rval);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700517 mempool_free(sp, ha->srb_mempool);
518 rval = -EIO;
519 goto done_free_fcport;
520 }
521 return rval;
522
523done_free_fcport:
524 kfree(fcport);
525done_unmap_sg:
526 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
527 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
528 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
529 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
530done:
531 return rval;
532}
533
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700534/* Set the port configuration to enable the
535 * internal loopback on ISP81XX
536 */
537static inline int
538qla81xx_set_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
539 uint16_t *new_config)
540{
541 int ret = 0;
542 int rval = 0;
543 struct qla_hw_data *ha = vha->hw;
544
Nigel Kirklandf863f602012-05-15 14:34:19 -0400545 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700546 goto done_set_internal;
547
548 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
549 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
550
551 ha->notify_dcbx_comp = 1;
552 ret = qla81xx_set_port_config(vha, new_config);
553 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700554 ql_log(ql_log_warn, vha, 0x7021,
555 "set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700556 ha->notify_dcbx_comp = 0;
557 rval = -EINVAL;
558 goto done_set_internal;
559 }
560
561 /* Wait for DCBX complete event */
562 if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700563 ql_dbg(ql_dbg_user, vha, 0x7022,
564 "State change notification not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700565 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700566 ql_dbg(ql_dbg_user, vha, 0x7023,
567 "State change received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700568
569 ha->notify_dcbx_comp = 0;
570
571done_set_internal:
572 return rval;
573}
574
575/* Set the port configuration to disable the
576 * internal loopback on ISP81XX
577 */
578static inline int
579qla81xx_reset_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
580 int wait)
581{
582 int ret = 0;
583 int rval = 0;
584 uint16_t new_config[4];
585 struct qla_hw_data *ha = vha->hw;
586
Nigel Kirklandf863f602012-05-15 14:34:19 -0400587 if (!IS_QLA81XX(ha) && !IS_QLA8031(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700588 goto done_reset_internal;
589
590 memset(new_config, 0 , sizeof(new_config));
591 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
592 ENABLE_INTERNAL_LOOPBACK) {
593 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
594 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
595
596 ha->notify_dcbx_comp = wait;
597 ret = qla81xx_set_port_config(vha, new_config);
598 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700599 ql_log(ql_log_warn, vha, 0x7025,
600 "Set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700601 ha->notify_dcbx_comp = 0;
602 rval = -EINVAL;
603 goto done_reset_internal;
604 }
605
606 /* Wait for DCBX complete event */
607 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
608 (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700609 ql_dbg(ql_dbg_user, vha, 0x7026,
610 "State change notification not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700611 ha->notify_dcbx_comp = 0;
612 rval = -EINVAL;
613 goto done_reset_internal;
614 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700615 ql_dbg(ql_dbg_user, vha, 0x7027,
616 "State change received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700617
618 ha->notify_dcbx_comp = 0;
619 }
620done_reset_internal:
621 return rval;
622}
623
Giridhar Malavali6e980162010-03-19 17:03:58 -0700624static int
625qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
626{
627 struct Scsi_Host *host = bsg_job->shost;
628 scsi_qla_host_t *vha = shost_priv(host);
629 struct qla_hw_data *ha = vha->hw;
630 int rval;
631 uint8_t command_sent;
632 char *type;
633 struct msg_echo_lb elreq;
634 uint16_t response[MAILBOX_REGISTER_COUNT];
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700635 uint16_t config[4], new_config[4];
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700636 uint8_t *fw_sts_ptr;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700637 uint8_t *req_data = NULL;
638 dma_addr_t req_data_dma;
639 uint32_t req_data_len;
640 uint8_t *rsp_data = NULL;
641 dma_addr_t rsp_data_dma;
642 uint32_t rsp_data_len;
643
Giridhar Malavali6e980162010-03-19 17:03:58 -0700644 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700645 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700646 return -EIO;
647 }
648
649 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
650 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
651 DMA_TO_DEVICE);
652
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700653 if (!elreq.req_sg_cnt) {
654 ql_log(ql_log_warn, vha, 0x701a,
655 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700656 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700657 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700658
659 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
660 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
661 DMA_FROM_DEVICE);
662
663 if (!elreq.rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700664 ql_log(ql_log_warn, vha, 0x701b,
665 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700666 rval = -ENOMEM;
667 goto done_unmap_req_sg;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700668 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700669
670 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
671 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700672 ql_log(ql_log_warn, vha, 0x701c,
673 "dma mapping resulted in different sg counts, "
674 "request_sg_cnt: %x dma_request_sg_cnt: %x "
675 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
676 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
677 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700678 rval = -EAGAIN;
679 goto done_unmap_sg;
680 }
681 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
682 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
683 &req_data_dma, GFP_KERNEL);
684 if (!req_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700685 ql_log(ql_log_warn, vha, 0x701d,
686 "dma alloc failed for req_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700687 rval = -ENOMEM;
688 goto done_unmap_sg;
689 }
690
691 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
692 &rsp_data_dma, GFP_KERNEL);
693 if (!rsp_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700694 ql_log(ql_log_warn, vha, 0x7004,
695 "dma alloc failed for rsp_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700696 rval = -ENOMEM;
697 goto done_free_dma_req;
698 }
699
700 /* Copy the request buffer in req_data now */
701 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
702 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
703
704 elreq.send_dma = req_data_dma;
705 elreq.rcv_dma = rsp_data_dma;
706 elreq.transfer_size = req_data_len;
707
708 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
709
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700710 if ((ha->current_topology == ISP_CFG_F ||
Saurav Kashyap491118d2011-08-16 11:31:50 -0700711 (atomic_read(&vha->loop_state) == LOOP_DOWN) ||
Nigel Kirklandf863f602012-05-15 14:34:19 -0400712 ((IS_QLA81XX(ha) || IS_QLA8031(ha)) &&
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700713 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
714 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
715 elreq.options == EXTERNAL_LOOPBACK) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700716 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700717 ql_dbg(ql_dbg_user, vha, 0x701e,
718 "BSG request type: %s.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700719 command_sent = INT_DEF_LB_ECHO_CMD;
720 rval = qla2x00_echo_test(vha, &elreq, response);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700721 } else {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800722 if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700723 memset(config, 0, sizeof(config));
724 memset(new_config, 0, sizeof(new_config));
725 if (qla81xx_get_port_config(vha, config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700726 ql_log(ql_log_warn, vha, 0x701f,
727 "Get port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700728 bsg_job->reply->result = (DID_ERROR << 16);
729 rval = -EPERM;
730 goto done_free_dma_req;
731 }
732
733 if (elreq.options != EXTERNAL_LOOPBACK) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700734 ql_dbg(ql_dbg_user, vha, 0x7020,
Masanari Iida36e4d262012-02-03 22:15:25 +0900735 "Internal: current port config = %x\n",
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700736 config[0]);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700737 if (qla81xx_set_internal_loopback(vha, config,
738 new_config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700739 ql_log(ql_log_warn, vha, 0x7024,
740 "Internal loopback failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700741 bsg_job->reply->result =
742 (DID_ERROR << 16);
743 rval = -EPERM;
744 goto done_free_dma_req;
745 }
746 } else {
747 /* For external loopback to work
748 * ensure internal loopback is disabled
749 */
750 if (qla81xx_reset_internal_loopback(vha,
751 config, 1)) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700752 bsg_job->reply->result =
753 (DID_ERROR << 16);
754 rval = -EPERM;
755 goto done_free_dma_req;
756 }
757 }
758
759 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700760 ql_dbg(ql_dbg_user, vha, 0x7028,
761 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700762
763 command_sent = INT_DEF_LB_LOOPBACK_CMD;
764 rval = qla2x00_loopback_test(vha, &elreq, response);
765
Joe Carnuccio4052bd52010-12-21 16:00:17 -0800766 if (new_config[0]) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700767 /* Revert back to original port config
768 * Also clear internal loopback
769 */
770 qla81xx_reset_internal_loopback(vha,
771 new_config, 0);
772 }
773
774 if (response[0] == MBS_COMMAND_ERROR &&
775 response[1] == MBS_LB_RESET) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700776 ql_log(ql_log_warn, vha, 0x7029,
777 "MBX command error, Aborting ISP.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700778 set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
779 qla2xxx_wake_dpc(vha);
780 qla2x00_wait_for_chip_reset(vha);
781 /* Also reset the MPI */
782 if (qla81xx_restart_mpi_firmware(vha) !=
783 QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700784 ql_log(ql_log_warn, vha, 0x702a,
785 "MPI reset failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700786 }
787
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700788 bsg_job->reply->result = (DID_ERROR << 16);
789 rval = -EIO;
790 goto done_free_dma_req;
791 }
792 } else {
793 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700794 ql_dbg(ql_dbg_user, vha, 0x702b,
795 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700796 command_sent = INT_DEF_LB_LOOPBACK_CMD;
797 rval = qla2x00_loopback_test(vha, &elreq, response);
798 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700799 }
800
801 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700802 ql_log(ql_log_warn, vha, 0x702c,
803 "Vendor request %s failed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700804
805 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700806 sizeof(struct fc_bsg_reply);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700807
808 memcpy(fw_sts_ptr, response, sizeof(response));
809 fw_sts_ptr += sizeof(response);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700810 *fw_sts_ptr = command_sent;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700811 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700812 bsg_job->reply->result = (DID_ERROR << 16);
813 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700814 ql_dbg(ql_dbg_user, vha, 0x702d,
815 "Vendor request %s completed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700816
817 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
818 sizeof(response) + sizeof(uint8_t);
819 bsg_job->reply->reply_payload_rcv_len =
820 bsg_job->reply_payload.payload_len;
821 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
822 sizeof(struct fc_bsg_reply);
823 memcpy(fw_sts_ptr, response, sizeof(response));
824 fw_sts_ptr += sizeof(response);
825 *fw_sts_ptr = command_sent;
826 bsg_job->reply->result = DID_OK;
827 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
828 bsg_job->reply_payload.sg_cnt, rsp_data,
829 rsp_data_len);
830 }
831 bsg_job->job_done(bsg_job);
832
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);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700846 return rval;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700847}
848
849static int
850qla84xx_reset(struct fc_bsg_job *bsg_job)
851{
852 struct Scsi_Host *host = bsg_job->shost;
853 scsi_qla_host_t *vha = shost_priv(host);
854 struct qla_hw_data *ha = vha->hw;
855 int rval = 0;
856 uint32_t flag;
857
Giridhar Malavali6e980162010-03-19 17:03:58 -0700858 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700859 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700860 return -EINVAL;
861 }
862
863 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
864
865 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
866
867 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700868 ql_log(ql_log_warn, vha, 0x7030,
869 "Vendor request 84xx reset failed.\n");
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -0800870 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700871 bsg_job->reply->result = (DID_ERROR << 16);
872
873 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700874 ql_dbg(ql_dbg_user, vha, 0x7031,
875 "Vendor request 84xx reset completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700876 bsg_job->reply->result = DID_OK;
877 }
878
879 bsg_job->job_done(bsg_job);
880 return rval;
881}
882
883static int
884qla84xx_updatefw(struct fc_bsg_job *bsg_job)
885{
886 struct Scsi_Host *host = bsg_job->shost;
887 scsi_qla_host_t *vha = shost_priv(host);
888 struct qla_hw_data *ha = vha->hw;
889 struct verify_chip_entry_84xx *mn = NULL;
890 dma_addr_t mn_dma, fw_dma;
891 void *fw_buf = NULL;
892 int rval = 0;
893 uint32_t sg_cnt;
894 uint32_t data_len;
895 uint16_t options;
896 uint32_t flag;
897 uint32_t fw_ver;
898
Giridhar Malavali6e980162010-03-19 17:03:58 -0700899 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700900 ql_dbg(ql_dbg_user, vha, 0x7032,
901 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700902 return -EINVAL;
903 }
904
905 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
906 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700907 if (!sg_cnt) {
908 ql_log(ql_log_warn, vha, 0x7033,
909 "dma_map_sg returned %d for request.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700910 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700911 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700912
913 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700914 ql_log(ql_log_warn, vha, 0x7034,
915 "DMA mapping resulted in different sg counts, "
916 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
917 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700918 rval = -EAGAIN;
919 goto done_unmap_sg;
920 }
921
922 data_len = bsg_job->request_payload.payload_len;
923 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
924 &fw_dma, GFP_KERNEL);
925 if (!fw_buf) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700926 ql_log(ql_log_warn, vha, 0x7035,
927 "DMA alloc failed for fw_buf.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700928 rval = -ENOMEM;
929 goto done_unmap_sg;
930 }
931
932 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
933 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
934
935 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
936 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700937 ql_log(ql_log_warn, vha, 0x7036,
938 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700939 rval = -ENOMEM;
940 goto done_free_fw_buf;
941 }
942
943 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
944 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
945
946 memset(mn, 0, sizeof(struct access_chip_84xx));
947 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
948 mn->entry_count = 1;
949
950 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
951 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
952 options |= VCO_DIAG_FW;
953
954 mn->options = cpu_to_le16(options);
955 mn->fw_ver = cpu_to_le32(fw_ver);
956 mn->fw_size = cpu_to_le32(data_len);
957 mn->fw_seq_size = cpu_to_le32(data_len);
958 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
959 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
960 mn->dseg_length = cpu_to_le32(data_len);
961 mn->data_seg_cnt = cpu_to_le16(1);
962
963 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
964
965 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700966 ql_log(ql_log_warn, vha, 0x7037,
967 "Vendor request 84xx updatefw failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700968
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -0800969 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700970 bsg_job->reply->result = (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
979 bsg_job->job_done(bsg_job);
980 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
981
982done_free_fw_buf:
983 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
984
985done_unmap_sg:
986 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
987 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
988
989 return rval;
990}
991
992static int
993qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
994{
995 struct Scsi_Host *host = bsg_job->shost;
996 scsi_qla_host_t *vha = shost_priv(host);
997 struct qla_hw_data *ha = vha->hw;
998 struct access_chip_84xx *mn = NULL;
999 dma_addr_t mn_dma, mgmt_dma;
1000 void *mgmt_b = NULL;
1001 int rval = 0;
1002 struct qla_bsg_a84_mgmt *ql84_mgmt;
1003 uint32_t sg_cnt;
Harish Zunjarraod5459082010-03-19 17:04:00 -07001004 uint32_t data_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001005 uint32_t dma_direction = DMA_NONE;
1006
Giridhar Malavali6e980162010-03-19 17:03:58 -07001007 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001008 ql_log(ql_log_warn, vha, 0x703a,
1009 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001010 return -EINVAL;
1011 }
1012
1013 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1014 sizeof(struct fc_bsg_request));
1015 if (!ql84_mgmt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001016 ql_log(ql_log_warn, vha, 0x703b,
1017 "MGMT header not provided, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001018 return -EINVAL;
1019 }
1020
1021 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1022 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001023 ql_log(ql_log_warn, vha, 0x703c,
1024 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001025 return -ENOMEM;
1026 }
1027
1028 memset(mn, 0, sizeof(struct access_chip_84xx));
1029 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1030 mn->entry_count = 1;
1031
1032 switch (ql84_mgmt->mgmt.cmd) {
1033 case QLA84_MGMT_READ_MEM:
1034 case QLA84_MGMT_GET_INFO:
1035 sg_cnt = dma_map_sg(&ha->pdev->dev,
1036 bsg_job->reply_payload.sg_list,
1037 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1038 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001039 ql_log(ql_log_warn, vha, 0x703d,
1040 "dma_map_sg returned %d for reply.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001041 rval = -ENOMEM;
1042 goto exit_mgmt;
1043 }
1044
1045 dma_direction = DMA_FROM_DEVICE;
1046
1047 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001048 ql_log(ql_log_warn, vha, 0x703e,
1049 "DMA mapping resulted in different sg counts, "
1050 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1051 bsg_job->reply_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001052 rval = -EAGAIN;
1053 goto done_unmap_sg;
1054 }
1055
1056 data_len = bsg_job->reply_payload.payload_len;
1057
1058 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1059 &mgmt_dma, GFP_KERNEL);
1060 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001061 ql_log(ql_log_warn, vha, 0x703f,
1062 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001063 rval = -ENOMEM;
1064 goto done_unmap_sg;
1065 }
1066
1067 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1068 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1069 mn->parameter1 =
1070 cpu_to_le32(
1071 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1072
1073 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1074 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1075 mn->parameter1 =
1076 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1077
1078 mn->parameter2 =
1079 cpu_to_le32(
1080 ql84_mgmt->mgmt.mgmtp.u.info.context);
1081 }
1082 break;
1083
1084 case QLA84_MGMT_WRITE_MEM:
1085 sg_cnt = dma_map_sg(&ha->pdev->dev,
1086 bsg_job->request_payload.sg_list,
1087 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1088
1089 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001090 ql_log(ql_log_warn, vha, 0x7040,
1091 "dma_map_sg returned %d.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001092 rval = -ENOMEM;
1093 goto exit_mgmt;
1094 }
1095
1096 dma_direction = DMA_TO_DEVICE;
1097
1098 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001099 ql_log(ql_log_warn, vha, 0x7041,
1100 "DMA mapping resulted in different sg counts, "
1101 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1102 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001103 rval = -EAGAIN;
1104 goto done_unmap_sg;
1105 }
1106
1107 data_len = bsg_job->request_payload.payload_len;
1108 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1109 &mgmt_dma, GFP_KERNEL);
1110 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001111 ql_log(ql_log_warn, vha, 0x7042,
1112 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001113 rval = -ENOMEM;
1114 goto done_unmap_sg;
1115 }
1116
1117 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1118 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1119
1120 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1121 mn->parameter1 =
1122 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1123 break;
1124
1125 case QLA84_MGMT_CHNG_CONFIG:
1126 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1127 mn->parameter1 =
1128 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1129
1130 mn->parameter2 =
1131 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1132
1133 mn->parameter3 =
1134 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1135 break;
1136
1137 default:
1138 rval = -EIO;
1139 goto exit_mgmt;
1140 }
1141
1142 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1143 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1144 mn->dseg_count = cpu_to_le16(1);
1145 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1146 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1147 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1148 }
1149
1150 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1151
1152 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001153 ql_log(ql_log_warn, vha, 0x7043,
1154 "Vendor request 84xx mgmt failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001155
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08001156 rval = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001157 bsg_job->reply->result = (DID_ERROR << 16);
1158
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
1177 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001178
1179done_unmap_sg:
Harish Zunjarraod5459082010-03-19 17:04:00 -07001180 if (mgmt_b)
1181 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1182
Giridhar Malavali6e980162010-03-19 17:03:58 -07001183 if (dma_direction == DMA_TO_DEVICE)
1184 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1185 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1186 else if (dma_direction == DMA_FROM_DEVICE)
1187 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1188 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1189
1190exit_mgmt:
1191 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1192
1193 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]);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001269 rval = 0;
1270 bsg_job->reply->result = (DID_ERROR << 16);
1271
1272 } else {
1273 if (!port_param->mode) {
1274 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1275 sizeof(struct qla_port_param);
1276
1277 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1278 sizeof(struct fc_bsg_reply);
1279
1280 memcpy(rsp_ptr, port_param,
1281 sizeof(struct qla_port_param));
1282 }
1283
1284 bsg_job->reply->result = DID_OK;
1285 }
1286
1287 bsg_job->job_done(bsg_job);
1288 return rval;
1289}
1290
1291static int
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001292qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001293 uint8_t is_update)
1294{
1295 uint32_t start = 0;
1296 int valid = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001297 struct qla_hw_data *ha = vha->hw;
Harish Zunjarraof19af162010-10-15 11:27:43 -07001298
Harish Zunjarraof19af162010-10-15 11:27:43 -07001299 if (unlikely(pci_channel_offline(ha->pdev)))
1300 return -EINVAL;
1301
1302 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001303 if (start > ha->optrom_size) {
1304 ql_log(ql_log_warn, vha, 0x7055,
1305 "start %d > optrom_size %d.\n", start, ha->optrom_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001306 return -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001307 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001308
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001309 if (ha->optrom_state != QLA_SWAITING) {
1310 ql_log(ql_log_info, vha, 0x7056,
1311 "optrom_state %d.\n", ha->optrom_state);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001312 return -EBUSY;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001313 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001314
1315 ha->optrom_region_start = start;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001316 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001317 if (is_update) {
1318 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1319 valid = 1;
1320 else if (start == (ha->flt_region_boot * 4) ||
1321 start == (ha->flt_region_fw * 4))
1322 valid = 1;
1323 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001324 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
Harish Zunjarraof19af162010-10-15 11:27:43 -07001325 valid = 1;
1326 if (!valid) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001327 ql_log(ql_log_warn, vha, 0x7058,
1328 "Invalid start region 0x%x/0x%x.\n", start,
1329 bsg_job->request_payload.payload_len);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001330 return -EINVAL;
1331 }
1332
1333 ha->optrom_region_size = start +
1334 bsg_job->request_payload.payload_len > ha->optrom_size ?
1335 ha->optrom_size - start :
1336 bsg_job->request_payload.payload_len;
1337 ha->optrom_state = QLA_SWRITING;
1338 } else {
1339 ha->optrom_region_size = start +
1340 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1341 ha->optrom_size - start :
1342 bsg_job->reply_payload.payload_len;
1343 ha->optrom_state = QLA_SREADING;
1344 }
1345
1346 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1347 if (!ha->optrom_buffer) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001348 ql_log(ql_log_warn, vha, 0x7059,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001349 "Read: Unable to allocate memory for optrom retrieval "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001350 "(%x)\n", ha->optrom_region_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001351
1352 ha->optrom_state = QLA_SWAITING;
1353 return -ENOMEM;
1354 }
1355
1356 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1357 return 0;
1358}
1359
1360static int
1361qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1362{
1363 struct Scsi_Host *host = bsg_job->shost;
1364 scsi_qla_host_t *vha = shost_priv(host);
1365 struct qla_hw_data *ha = vha->hw;
1366 int rval = 0;
1367
Giridhar Malavalia49393f2012-04-25 07:26:14 -07001368 if (ha->flags.isp82xx_reset_hdlr_active)
1369 return -EBUSY;
1370
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001371 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001372 if (rval)
1373 return rval;
1374
1375 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1376 ha->optrom_region_start, ha->optrom_region_size);
1377
1378 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1379 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1380 ha->optrom_region_size);
1381
1382 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1383 bsg_job->reply->result = DID_OK;
1384 vfree(ha->optrom_buffer);
1385 ha->optrom_buffer = NULL;
1386 ha->optrom_state = QLA_SWAITING;
1387 bsg_job->job_done(bsg_job);
1388 return rval;
1389}
1390
1391static int
1392qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1393{
1394 struct Scsi_Host *host = bsg_job->shost;
1395 scsi_qla_host_t *vha = shost_priv(host);
1396 struct qla_hw_data *ha = vha->hw;
1397 int rval = 0;
1398
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001399 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001400 if (rval)
1401 return rval;
1402
Giridhar Malavalib6d0d9d2012-05-15 14:34:25 -04001403 /* Set the isp82xx_no_md_cap not to capture minidump */
1404 ha->flags.isp82xx_no_md_cap = 1;
1405
Harish Zunjarraof19af162010-10-15 11:27:43 -07001406 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1407 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1408 ha->optrom_region_size);
1409
1410 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1411 ha->optrom_region_start, ha->optrom_region_size);
1412
1413 bsg_job->reply->result = DID_OK;
1414 vfree(ha->optrom_buffer);
1415 ha->optrom_buffer = NULL;
1416 ha->optrom_state = QLA_SWAITING;
1417 bsg_job->job_done(bsg_job);
1418 return rval;
1419}
1420
1421static int
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001422qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1423{
1424 struct Scsi_Host *host = bsg_job->shost;
1425 scsi_qla_host_t *vha = shost_priv(host);
1426 struct qla_hw_data *ha = vha->hw;
1427 int rval = 0;
1428 uint8_t bsg[DMA_POOL_SIZE];
1429 struct qla_image_version_list *list = (void *)bsg;
1430 struct qla_image_version *image;
1431 uint32_t count;
1432 dma_addr_t sfp_dma;
1433 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1434 if (!sfp) {
1435 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1436 EXT_STATUS_NO_MEMORY;
1437 goto done;
1438 }
1439
1440 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1441 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1442
1443 image = list->version;
1444 count = list->count;
1445 while (count--) {
1446 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1447 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1448 image->field_address.device, image->field_address.offset,
1449 sizeof(image->field_info), image->field_address.option);
1450 if (rval) {
1451 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1452 EXT_STATUS_MAILBOX;
1453 goto dealloc;
1454 }
1455 image++;
1456 }
1457
1458 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1459
1460dealloc:
1461 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1462
1463done:
1464 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1465 bsg_job->reply->result = DID_OK << 16;
1466 bsg_job->job_done(bsg_job);
1467
1468 return 0;
1469}
1470
1471static int
1472qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1473{
1474 struct Scsi_Host *host = bsg_job->shost;
1475 scsi_qla_host_t *vha = shost_priv(host);
1476 struct qla_hw_data *ha = vha->hw;
1477 int rval = 0;
1478 uint8_t bsg[DMA_POOL_SIZE];
1479 struct qla_status_reg *sr = (void *)bsg;
1480 dma_addr_t sfp_dma;
1481 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1482 if (!sfp) {
1483 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1484 EXT_STATUS_NO_MEMORY;
1485 goto done;
1486 }
1487
1488 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1489 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1490
1491 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1492 sr->field_address.device, sr->field_address.offset,
1493 sizeof(sr->status_reg), sr->field_address.option);
1494 sr->status_reg = *sfp;
1495
1496 if (rval) {
1497 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1498 EXT_STATUS_MAILBOX;
1499 goto dealloc;
1500 }
1501
1502 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1503 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1504
1505 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1506
1507dealloc:
1508 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1509
1510done:
1511 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1512 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1513 bsg_job->reply->result = DID_OK << 16;
1514 bsg_job->job_done(bsg_job);
1515
1516 return 0;
1517}
1518
1519static int
1520qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1521{
1522 struct Scsi_Host *host = bsg_job->shost;
1523 scsi_qla_host_t *vha = shost_priv(host);
1524 struct qla_hw_data *ha = vha->hw;
1525 int rval = 0;
1526 uint8_t bsg[DMA_POOL_SIZE];
1527 struct qla_status_reg *sr = (void *)bsg;
1528 dma_addr_t sfp_dma;
1529 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1530 if (!sfp) {
1531 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1532 EXT_STATUS_NO_MEMORY;
1533 goto done;
1534 }
1535
1536 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1537 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1538
1539 *sfp = sr->status_reg;
1540 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1541 sr->field_address.device, sr->field_address.offset,
1542 sizeof(sr->status_reg), sr->field_address.option);
1543
1544 if (rval) {
1545 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1546 EXT_STATUS_MAILBOX;
1547 goto dealloc;
1548 }
1549
1550 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1551
1552dealloc:
1553 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1554
1555done:
1556 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1557 bsg_job->reply->result = DID_OK << 16;
1558 bsg_job->job_done(bsg_job);
1559
1560 return 0;
1561}
1562
1563static int
Giridhar Malavali6e980162010-03-19 17:03:58 -07001564qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
1565{
1566 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
1567 case QL_VND_LOOPBACK:
1568 return qla2x00_process_loopback(bsg_job);
1569
1570 case QL_VND_A84_RESET:
1571 return qla84xx_reset(bsg_job);
1572
1573 case QL_VND_A84_UPDATE_FW:
1574 return qla84xx_updatefw(bsg_job);
1575
1576 case QL_VND_A84_MGMT_CMD:
1577 return qla84xx_mgmt_cmd(bsg_job);
1578
1579 case QL_VND_IIDMA:
1580 return qla24xx_iidma(bsg_job);
1581
Sarang Radke09ff7012010-03-19 17:03:59 -07001582 case QL_VND_FCP_PRIO_CFG_CMD:
1583 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
1584
Harish Zunjarraof19af162010-10-15 11:27:43 -07001585 case QL_VND_READ_FLASH:
1586 return qla2x00_read_optrom(bsg_job);
1587
1588 case QL_VND_UPDATE_FLASH:
1589 return qla2x00_update_optrom(bsg_job);
1590
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001591 case QL_VND_SET_FRU_VERSION:
1592 return qla2x00_update_fru_versions(bsg_job);
1593
1594 case QL_VND_READ_FRU_STATUS:
1595 return qla2x00_read_fru_status(bsg_job);
1596
1597 case QL_VND_WRITE_FRU_STATUS:
1598 return qla2x00_write_fru_status(bsg_job);
1599
Giridhar Malavali6e980162010-03-19 17:03:58 -07001600 default:
1601 bsg_job->reply->result = (DID_ERROR << 16);
1602 bsg_job->job_done(bsg_job);
1603 return -ENOSYS;
1604 }
1605}
1606
1607int
1608qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
1609{
1610 int ret = -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001611 struct fc_rport *rport;
1612 fc_port_t *fcport = NULL;
1613 struct Scsi_Host *host;
1614 scsi_qla_host_t *vha;
1615
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08001616 /* In case no data transferred. */
1617 bsg_job->reply->reply_payload_rcv_len = 0;
1618
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001619 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1620 rport = bsg_job->rport;
1621 fcport = *(fc_port_t **) rport->dd_data;
1622 host = rport_to_shost(rport);
1623 vha = shost_priv(host);
1624 } else {
1625 host = bsg_job->shost;
1626 vha = shost_priv(host);
1627 }
1628
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001629 if (qla2x00_reset_active(vha)) {
1630 ql_dbg(ql_dbg_user, vha, 0x709f,
1631 "BSG: ISP abort active/needed -- cmd=%d.\n",
1632 bsg_job->request->msgcode);
1633 bsg_job->reply->result = (DID_ERROR << 16);
1634 bsg_job->job_done(bsg_job);
1635 return -EBUSY;
1636 }
1637
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001638 ql_dbg(ql_dbg_user, vha, 0x7000,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001639 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001640
1641 switch (bsg_job->request->msgcode) {
1642 case FC_BSG_RPT_ELS:
1643 case FC_BSG_HST_ELS_NOLOGIN:
1644 ret = qla2x00_process_els(bsg_job);
1645 break;
1646 case FC_BSG_HST_CT:
1647 ret = qla2x00_process_ct(bsg_job);
1648 break;
1649 case FC_BSG_HST_VENDOR:
1650 ret = qla2x00_process_vendor_specific(bsg_job);
1651 break;
1652 case FC_BSG_HST_ADD_RPORT:
1653 case FC_BSG_HST_DEL_RPORT:
1654 case FC_BSG_RPT_CT:
1655 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001656 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
Andrew Vasquezb7bfbe12012-02-09 11:15:44 -08001657 bsg_job->reply->result = ret;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001658 break;
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001659 }
Giridhar Malavali6e980162010-03-19 17:03:58 -07001660 return ret;
1661}
1662
1663int
1664qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
1665{
1666 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
1667 struct qla_hw_data *ha = vha->hw;
1668 srb_t *sp;
1669 int cnt, que;
1670 unsigned long flags;
1671 struct req_que *req;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001672
1673 /* find the bsg job from the active list of commands */
1674 spin_lock_irqsave(&ha->hardware_lock, flags);
1675 for (que = 0; que < ha->max_req_queues; que++) {
1676 req = ha->req_q_map[que];
1677 if (!req)
1678 continue;
1679
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001680 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Giridhar Malavali6e980162010-03-19 17:03:58 -07001681 sp = req->outstanding_cmds[cnt];
Giridhar Malavali6e980162010-03-19 17:03:58 -07001682 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001683 if (((sp->type == SRB_CT_CMD) ||
1684 (sp->type == SRB_ELS_CMD_HST))
1685 && (sp->u.bsg_job == bsg_job)) {
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001686 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001687 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001688 ql_log(ql_log_warn, vha, 0x7089,
1689 "mbx abort_command "
1690 "failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001691 bsg_job->req->errors =
1692 bsg_job->reply->result = -EIO;
1693 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001694 ql_dbg(ql_dbg_user, vha, 0x708a,
1695 "mbx abort_command "
1696 "success.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001697 bsg_job->req->errors =
1698 bsg_job->reply->result = 0;
1699 }
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001700 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001701 goto done;
1702 }
1703 }
1704 }
1705 }
1706 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001707 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001708 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
1709 return 0;
1710
1711done:
1712 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1713 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
1714 kfree(sp->fcport);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001715 mempool_free(sp, ha->srb_mempool);
1716 return 0;
1717}