blob: b2b664483ab469d21013d46af4eaef3ab0cf9652 [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
111 bsg_job->reply->reply_payload_rcv_len = 0;
112
Saurav Kashyapa00f6292011-11-18 09:03:19 -0800113 if (!(IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) || IS_QLA82XX(ha))) {
Madhuranath Iyengar2f0f3f42010-07-23 15:28:24 +0500114 ret = -EINVAL;
115 goto exit_fcp_prio_cfg;
116 }
117
Sarang Radke09ff7012010-03-19 17:03:59 -0700118 /* Get the sub command */
119 oper = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
120
121 /* Only set config is allowed if config memory is not allocated */
122 if (!ha->fcp_prio_cfg && (oper != QLFC_FCP_PRIO_SET_CONFIG)) {
123 ret = -EINVAL;
124 goto exit_fcp_prio_cfg;
125 }
126 switch (oper) {
127 case QLFC_FCP_PRIO_DISABLE:
128 if (ha->flags.fcp_prio_enabled) {
129 ha->flags.fcp_prio_enabled = 0;
130 ha->fcp_prio_cfg->attributes &=
131 ~FCP_PRIO_ATTR_ENABLE;
132 qla24xx_update_all_fcp_prio(vha);
133 bsg_job->reply->result = DID_OK;
134 } else {
135 ret = -EINVAL;
136 bsg_job->reply->result = (DID_ERROR << 16);
137 goto exit_fcp_prio_cfg;
138 }
139 break;
140
141 case QLFC_FCP_PRIO_ENABLE:
142 if (!ha->flags.fcp_prio_enabled) {
143 if (ha->fcp_prio_cfg) {
144 ha->flags.fcp_prio_enabled = 1;
145 ha->fcp_prio_cfg->attributes |=
146 FCP_PRIO_ATTR_ENABLE;
147 qla24xx_update_all_fcp_prio(vha);
148 bsg_job->reply->result = DID_OK;
149 } else {
150 ret = -EINVAL;
151 bsg_job->reply->result = (DID_ERROR << 16);
152 goto exit_fcp_prio_cfg;
153 }
154 }
155 break;
156
157 case QLFC_FCP_PRIO_GET_CONFIG:
158 len = bsg_job->reply_payload.payload_len;
159 if (!len || len > FCP_PRIO_CFG_SIZE) {
160 ret = -EINVAL;
161 bsg_job->reply->result = (DID_ERROR << 16);
162 goto exit_fcp_prio_cfg;
163 }
164
165 bsg_job->reply->result = DID_OK;
166 bsg_job->reply->reply_payload_rcv_len =
167 sg_copy_from_buffer(
168 bsg_job->reply_payload.sg_list,
169 bsg_job->reply_payload.sg_cnt, ha->fcp_prio_cfg,
170 len);
171
172 break;
173
174 case QLFC_FCP_PRIO_SET_CONFIG:
175 len = bsg_job->request_payload.payload_len;
176 if (!len || len > FCP_PRIO_CFG_SIZE) {
177 bsg_job->reply->result = (DID_ERROR << 16);
178 ret = -EINVAL;
179 goto exit_fcp_prio_cfg;
180 }
181
182 if (!ha->fcp_prio_cfg) {
183 ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
184 if (!ha->fcp_prio_cfg) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700185 ql_log(ql_log_warn, vha, 0x7050,
186 "Unable to allocate memory for fcp prio "
187 "config data (%x).\n", FCP_PRIO_CFG_SIZE);
Sarang Radke09ff7012010-03-19 17:03:59 -0700188 bsg_job->reply->result = (DID_ERROR << 16);
189 ret = -ENOMEM;
190 goto exit_fcp_prio_cfg;
191 }
192 }
193
194 memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
195 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
196 bsg_job->request_payload.sg_cnt, ha->fcp_prio_cfg,
197 FCP_PRIO_CFG_SIZE);
198
199 /* validate fcp priority data */
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700200
201 if (!qla24xx_fcp_prio_cfg_valid(vha,
202 (struct qla_fcp_prio_cfg *) ha->fcp_prio_cfg, 1)) {
Sarang Radke09ff7012010-03-19 17:03:59 -0700203 bsg_job->reply->result = (DID_ERROR << 16);
204 ret = -EINVAL;
205 /* If buffer was invalidatic int
206 * fcp_prio_cfg is of no use
207 */
208 vfree(ha->fcp_prio_cfg);
209 ha->fcp_prio_cfg = NULL;
210 goto exit_fcp_prio_cfg;
211 }
212
213 ha->flags.fcp_prio_enabled = 0;
214 if (ha->fcp_prio_cfg->attributes & FCP_PRIO_ATTR_ENABLE)
215 ha->flags.fcp_prio_enabled = 1;
216 qla24xx_update_all_fcp_prio(vha);
217 bsg_job->reply->result = DID_OK;
218 break;
219 default:
220 ret = -EINVAL;
221 break;
222 }
223exit_fcp_prio_cfg:
224 bsg_job->job_done(bsg_job);
225 return ret;
226}
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800227
Giridhar Malavali6e980162010-03-19 17:03:58 -0700228static int
229qla2x00_process_els(struct fc_bsg_job *bsg_job)
230{
231 struct fc_rport *rport;
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500232 fc_port_t *fcport = NULL;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700233 struct Scsi_Host *host;
234 scsi_qla_host_t *vha;
235 struct qla_hw_data *ha;
236 srb_t *sp;
237 const char *type;
238 int req_sg_cnt, rsp_sg_cnt;
239 int rval = (DRIVER_ERROR << 16);
240 uint16_t nextlid = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700241
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500242 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
243 rport = bsg_job->rport;
244 fcport = *(fc_port_t **) rport->dd_data;
245 host = rport_to_shost(rport);
246 vha = shost_priv(host);
247 ha = vha->hw;
248 type = "FC_BSG_RPT_ELS";
249 } else {
250 host = bsg_job->shost;
251 vha = shost_priv(host);
252 ha = vha->hw;
253 type = "FC_BSG_HST_ELS_NOLOGIN";
254 }
255
256 /* pass through is supported only for ISP 4Gb or higher */
257 if (!IS_FWI2_CAPABLE(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700258 ql_dbg(ql_dbg_user, vha, 0x7001,
259 "ELS passthru not supported for ISP23xx based adapters.\n");
Harish Zunjarrao08f71e02010-07-23 15:28:33 +0500260 rval = -EPERM;
261 goto done;
262 }
263
Giridhar Malavali6e980162010-03-19 17:03:58 -0700264 /* Multiple SG's are not supported for ELS requests */
265 if (bsg_job->request_payload.sg_cnt > 1 ||
266 bsg_job->reply_payload.sg_cnt > 1) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700267 ql_dbg(ql_dbg_user, vha, 0x7002,
268 "Multiple SG's are not suppored for ELS requests, "
269 "request_sg_cnt=%x reply_sg_cnt=%x.\n",
270 bsg_job->request_payload.sg_cnt,
271 bsg_job->reply_payload.sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700272 rval = -EPERM;
273 goto done;
274 }
275
276 /* ELS request for rport */
277 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700278 /* make sure the rport is logged in,
279 * if not perform fabric login
280 */
281 if (qla2x00_fabric_login(vha, fcport, &nextlid)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700282 ql_dbg(ql_dbg_user, vha, 0x7003,
283 "Failed to login port %06X for ELS passthru.\n",
284 fcport->d_id.b24);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700285 rval = -EIO;
286 goto done;
287 }
288 } else {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700289 /* Allocate a dummy fcport structure, since functions
290 * preparing the IOCB and mailbox command retrieves port
291 * specific information from fcport structure. For Host based
292 * ELS commands there will be no fcport structure allocated
293 */
294 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
295 if (!fcport) {
296 rval = -ENOMEM;
297 goto done;
298 }
299
300 /* Initialize all required fields of fcport */
301 fcport->vha = vha;
302 fcport->vp_idx = vha->vp_idx;
303 fcport->d_id.b.al_pa =
304 bsg_job->request->rqst_data.h_els.port_id[0];
305 fcport->d_id.b.area =
306 bsg_job->request->rqst_data.h_els.port_id[1];
307 fcport->d_id.b.domain =
308 bsg_job->request->rqst_data.h_els.port_id[2];
309 fcport->loop_id =
310 (fcport->d_id.b.al_pa == 0xFD) ?
311 NPH_FABRIC_CONTROLLER : NPH_F_PORT;
312 }
313
314 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700315 ql_log(ql_log_warn, vha, 0x7005, "Host not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700316 rval = -EIO;
317 goto done;
318 }
319
320 req_sg_cnt =
321 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
322 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
323 if (!req_sg_cnt) {
324 rval = -ENOMEM;
325 goto done_free_fcport;
326 }
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700327
328 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
329 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700330 if (!rsp_sg_cnt) {
331 rval = -ENOMEM;
332 goto done_free_fcport;
333 }
334
335 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700336 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700337 ql_log(ql_log_warn, vha, 0x7008,
338 "dma mapping resulted in different sg counts, "
339 "request_sg_cnt: %x dma_request_sg_cnt:%x reply_sg_cnt:%x "
340 "dma_reply_sg_cnt:%x.\n", bsg_job->request_payload.sg_cnt,
341 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700342 rval = -EAGAIN;
343 goto done_unmap_sg;
344 }
345
346 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800347 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700348 if (!sp) {
349 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700350 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700351 }
352
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800353 sp->type =
Giridhar Malavali6e980162010-03-19 17:03:58 -0700354 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
355 SRB_ELS_CMD_RPT : SRB_ELS_CMD_HST);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800356 sp->name =
Madhuranath Iyengar38222632010-05-04 15:01:29 -0700357 (bsg_job->request->msgcode == FC_BSG_RPT_ELS ?
358 "bsg_els_rpt" : "bsg_els_hst");
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800359 sp->u.bsg_job = bsg_job;
360 sp->free = qla2x00_bsg_sp_free;
361 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700362
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700363 ql_dbg(ql_dbg_user, vha, 0x700a,
364 "bsg rqst type: %s els type: %x - loop-id=%x "
365 "portid=%-2x%02x%02x.\n", type,
366 bsg_job->request->rqst_data.h_els.command_code, fcport->loop_id,
367 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700368
369 rval = qla2x00_start_sp(sp);
370 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700371 ql_log(ql_log_warn, vha, 0x700e,
372 "qla2x00_start_sp failed = %d\n", rval);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700373 mempool_free(sp, ha->srb_mempool);
374 rval = -EIO;
375 goto done_unmap_sg;
376 }
377 return rval;
378
379done_unmap_sg:
380 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
381 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
382 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
383 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
384 goto done_free_fcport;
385
386done_free_fcport:
387 if (bsg_job->request->msgcode == FC_BSG_HST_ELS_NOLOGIN)
388 kfree(fcport);
389done:
390 return rval;
391}
392
Andrew Vasquez57807902011-11-18 09:03:20 -0800393inline uint16_t
394qla24xx_calc_ct_iocbs(uint16_t dsds)
395{
396 uint16_t iocbs;
397
398 iocbs = 1;
399 if (dsds > 2) {
400 iocbs += (dsds - 2) / 5;
401 if ((dsds - 2) % 5)
402 iocbs++;
403 }
404 return iocbs;
405}
406
Giridhar Malavali6e980162010-03-19 17:03:58 -0700407static int
408qla2x00_process_ct(struct fc_bsg_job *bsg_job)
409{
410 srb_t *sp;
411 struct Scsi_Host *host = bsg_job->shost;
412 scsi_qla_host_t *vha = shost_priv(host);
413 struct qla_hw_data *ha = vha->hw;
414 int rval = (DRIVER_ERROR << 16);
415 int req_sg_cnt, rsp_sg_cnt;
416 uint16_t loop_id;
417 struct fc_port *fcport;
418 char *type = "FC_BSG_HST_CT";
Giridhar Malavali6e980162010-03-19 17:03:58 -0700419
Giridhar Malavali6e980162010-03-19 17:03:58 -0700420 req_sg_cnt =
421 dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
422 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700423 if (!req_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700424 ql_log(ql_log_warn, vha, 0x700f,
425 "dma_map_sg return %d for request\n", req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700426 rval = -ENOMEM;
427 goto done;
428 }
429
430 rsp_sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
431 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
432 if (!rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700433 ql_log(ql_log_warn, vha, 0x7010,
434 "dma_map_sg return %d for reply\n", rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700435 rval = -ENOMEM;
436 goto done;
437 }
438
439 if ((req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700440 (rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700441 ql_log(ql_log_warn, vha, 0x7011,
442 "request_sg_cnt: %x dma_request_sg_cnt: %x reply_sg_cnt:%x "
443 "dma_reply_sg_cnt: %x\n", bsg_job->request_payload.sg_cnt,
444 req_sg_cnt, bsg_job->reply_payload.sg_cnt, rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700445 rval = -EAGAIN;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700446 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700447 }
448
449 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700450 ql_log(ql_log_warn, vha, 0x7012,
451 "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700452 rval = -EIO;
453 goto done_unmap_sg;
454 }
455
456 loop_id =
457 (bsg_job->request->rqst_data.h_ct.preamble_word1 & 0xFF000000)
458 >> 24;
459 switch (loop_id) {
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700460 case 0xFC:
461 loop_id = cpu_to_le16(NPH_SNS);
462 break;
463 case 0xFA:
464 loop_id = vha->mgmt_svr_loop_id;
465 break;
466 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700467 ql_dbg(ql_dbg_user, vha, 0x7013,
468 "Unknown loop id: %x.\n", loop_id);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700469 rval = -EINVAL;
470 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700471 }
472
473 /* Allocate a dummy fcport structure, since functions preparing the
474 * IOCB and mailbox command retrieves port specific information
475 * from fcport structure. For Host based ELS commands there will be
476 * no fcport structure allocated
477 */
478 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700479 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700480 ql_log(ql_log_warn, vha, 0x7014,
481 "Failed to allocate fcport.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700482 rval = -ENOMEM;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700483 goto done_unmap_sg;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700484 }
485
486 /* Initialize all required fields of fcport */
487 fcport->vha = vha;
488 fcport->vp_idx = vha->vp_idx;
489 fcport->d_id.b.al_pa = bsg_job->request->rqst_data.h_ct.port_id[0];
490 fcport->d_id.b.area = bsg_job->request->rqst_data.h_ct.port_id[1];
491 fcport->d_id.b.domain = bsg_job->request->rqst_data.h_ct.port_id[2];
492 fcport->loop_id = loop_id;
493
494 /* Alloc SRB structure */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800495 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700496 if (!sp) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700497 ql_log(ql_log_warn, vha, 0x7015,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800498 "qla2x00_get_sp failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700499 rval = -ENOMEM;
500 goto done_free_fcport;
501 }
502
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800503 sp->type = SRB_CT_CMD;
504 sp->name = "bsg_ct";
505 sp->iocbs = qla24xx_calc_ct_iocbs(req_sg_cnt + rsp_sg_cnt);
506 sp->u.bsg_job = bsg_job;
507 sp->free = qla2x00_bsg_sp_free;
508 sp->done = qla2x00_bsg_job_done;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700509
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700510 ql_dbg(ql_dbg_user, vha, 0x7016,
511 "bsg rqst type: %s else type: %x - "
512 "loop-id=%x portid=%02x%02x%02x.\n", type,
513 (bsg_job->request->rqst_data.h_ct.preamble_word2 >> 16),
514 fcport->loop_id, fcport->d_id.b.domain, fcport->d_id.b.area,
515 fcport->d_id.b.al_pa);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700516
517 rval = qla2x00_start_sp(sp);
518 if (rval != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700519 ql_log(ql_log_warn, vha, 0x7017,
520 "qla2x00_start_sp failed=%d.\n", rval);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700521 mempool_free(sp, ha->srb_mempool);
522 rval = -EIO;
523 goto done_free_fcport;
524 }
525 return rval;
526
527done_free_fcport:
528 kfree(fcport);
529done_unmap_sg:
530 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
531 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
532 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
533 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
534done:
535 return rval;
536}
537
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700538/* Set the port configuration to enable the
539 * internal loopback on ISP81XX
540 */
541static inline int
542qla81xx_set_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
543 uint16_t *new_config)
544{
545 int ret = 0;
546 int rval = 0;
547 struct qla_hw_data *ha = vha->hw;
548
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800549 if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700550 goto done_set_internal;
551
552 new_config[0] = config[0] | (ENABLE_INTERNAL_LOOPBACK << 1);
553 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
554
555 ha->notify_dcbx_comp = 1;
556 ret = qla81xx_set_port_config(vha, new_config);
557 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700558 ql_log(ql_log_warn, vha, 0x7021,
559 "set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700560 ha->notify_dcbx_comp = 0;
561 rval = -EINVAL;
562 goto done_set_internal;
563 }
564
565 /* Wait for DCBX complete event */
566 if (!wait_for_completion_timeout(&ha->dcbx_comp, (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700567 ql_dbg(ql_dbg_user, vha, 0x7022,
568 "State change notification not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700569 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700570 ql_dbg(ql_dbg_user, vha, 0x7023,
571 "State change received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700572
573 ha->notify_dcbx_comp = 0;
574
575done_set_internal:
576 return rval;
577}
578
579/* Set the port configuration to disable the
580 * internal loopback on ISP81XX
581 */
582static inline int
583qla81xx_reset_internal_loopback(scsi_qla_host_t *vha, uint16_t *config,
584 int wait)
585{
586 int ret = 0;
587 int rval = 0;
588 uint16_t new_config[4];
589 struct qla_hw_data *ha = vha->hw;
590
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800591 if (!IS_QLA81XX(ha) && !IS_QLA83XX(ha))
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700592 goto done_reset_internal;
593
594 memset(new_config, 0 , sizeof(new_config));
595 if ((config[0] & INTERNAL_LOOPBACK_MASK) >> 1 ==
596 ENABLE_INTERNAL_LOOPBACK) {
597 new_config[0] = config[0] & ~INTERNAL_LOOPBACK_MASK;
598 memcpy(&new_config[1], &config[1], sizeof(uint16_t) * 3) ;
599
600 ha->notify_dcbx_comp = wait;
601 ret = qla81xx_set_port_config(vha, new_config);
602 if (ret != QLA_SUCCESS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700603 ql_log(ql_log_warn, vha, 0x7025,
604 "Set port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700605 ha->notify_dcbx_comp = 0;
606 rval = -EINVAL;
607 goto done_reset_internal;
608 }
609
610 /* Wait for DCBX complete event */
611 if (wait && !wait_for_completion_timeout(&ha->dcbx_comp,
612 (20 * HZ))) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700613 ql_dbg(ql_dbg_user, vha, 0x7026,
614 "State change notification not received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700615 ha->notify_dcbx_comp = 0;
616 rval = -EINVAL;
617 goto done_reset_internal;
618 } else
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700619 ql_dbg(ql_dbg_user, vha, 0x7027,
620 "State change received.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700621
622 ha->notify_dcbx_comp = 0;
623 }
624done_reset_internal:
625 return rval;
626}
627
Giridhar Malavali6e980162010-03-19 17:03:58 -0700628static int
629qla2x00_process_loopback(struct fc_bsg_job *bsg_job)
630{
631 struct Scsi_Host *host = bsg_job->shost;
632 scsi_qla_host_t *vha = shost_priv(host);
633 struct qla_hw_data *ha = vha->hw;
634 int rval;
635 uint8_t command_sent;
636 char *type;
637 struct msg_echo_lb elreq;
638 uint16_t response[MAILBOX_REGISTER_COUNT];
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700639 uint16_t config[4], new_config[4];
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700640 uint8_t *fw_sts_ptr;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700641 uint8_t *req_data = NULL;
642 dma_addr_t req_data_dma;
643 uint32_t req_data_len;
644 uint8_t *rsp_data = NULL;
645 dma_addr_t rsp_data_dma;
646 uint32_t rsp_data_len;
647
Giridhar Malavali6e980162010-03-19 17:03:58 -0700648 if (!vha->flags.online) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700649 ql_log(ql_log_warn, vha, 0x7019, "Host is not online.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700650 return -EIO;
651 }
652
653 elreq.req_sg_cnt = dma_map_sg(&ha->pdev->dev,
654 bsg_job->request_payload.sg_list, bsg_job->request_payload.sg_cnt,
655 DMA_TO_DEVICE);
656
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700657 if (!elreq.req_sg_cnt) {
658 ql_log(ql_log_warn, vha, 0x701a,
659 "dma_map_sg returned %d for request.\n", elreq.req_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700660 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700661 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700662
663 elreq.rsp_sg_cnt = dma_map_sg(&ha->pdev->dev,
664 bsg_job->reply_payload.sg_list, bsg_job->reply_payload.sg_cnt,
665 DMA_FROM_DEVICE);
666
667 if (!elreq.rsp_sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700668 ql_log(ql_log_warn, vha, 0x701b,
669 "dma_map_sg returned %d for reply.\n", elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700670 rval = -ENOMEM;
671 goto done_unmap_req_sg;
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700672 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700673
674 if ((elreq.req_sg_cnt != bsg_job->request_payload.sg_cnt) ||
675 (elreq.rsp_sg_cnt != bsg_job->reply_payload.sg_cnt)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700676 ql_log(ql_log_warn, vha, 0x701c,
677 "dma mapping resulted in different sg counts, "
678 "request_sg_cnt: %x dma_request_sg_cnt: %x "
679 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
680 bsg_job->request_payload.sg_cnt, elreq.req_sg_cnt,
681 bsg_job->reply_payload.sg_cnt, elreq.rsp_sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700682 rval = -EAGAIN;
683 goto done_unmap_sg;
684 }
685 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
686 req_data = dma_alloc_coherent(&ha->pdev->dev, req_data_len,
687 &req_data_dma, GFP_KERNEL);
688 if (!req_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700689 ql_log(ql_log_warn, vha, 0x701d,
690 "dma alloc failed for req_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700691 rval = -ENOMEM;
692 goto done_unmap_sg;
693 }
694
695 rsp_data = dma_alloc_coherent(&ha->pdev->dev, rsp_data_len,
696 &rsp_data_dma, GFP_KERNEL);
697 if (!rsp_data) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700698 ql_log(ql_log_warn, vha, 0x7004,
699 "dma alloc failed for rsp_data.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700700 rval = -ENOMEM;
701 goto done_free_dma_req;
702 }
703
704 /* Copy the request buffer in req_data now */
705 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
706 bsg_job->request_payload.sg_cnt, req_data, req_data_len);
707
708 elreq.send_dma = req_data_dma;
709 elreq.rcv_dma = rsp_data_dma;
710 elreq.transfer_size = req_data_len;
711
712 elreq.options = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
713
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700714 if ((ha->current_topology == ISP_CFG_F ||
Saurav Kashyap491118d2011-08-16 11:31:50 -0700715 (atomic_read(&vha->loop_state) == LOOP_DOWN) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800716 ((IS_QLA81XX(ha) || IS_QLA83XX(ha)) &&
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700717 le32_to_cpu(*(uint32_t *)req_data) == ELS_OPCODE_BYTE
718 && req_data_len == MAX_ELS_FRAME_PAYLOAD)) &&
719 elreq.options == EXTERNAL_LOOPBACK) {
Giridhar Malavali6e980162010-03-19 17:03:58 -0700720 type = "FC_BSG_HST_VENDOR_ECHO_DIAG";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700721 ql_dbg(ql_dbg_user, vha, 0x701e,
722 "BSG request type: %s.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700723 command_sent = INT_DEF_LB_ECHO_CMD;
724 rval = qla2x00_echo_test(vha, &elreq, response);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700725 } else {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800726 if (IS_QLA81XX(ha) || IS_QLA8031(ha)) {
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700727 memset(config, 0, sizeof(config));
728 memset(new_config, 0, sizeof(new_config));
729 if (qla81xx_get_port_config(vha, config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700730 ql_log(ql_log_warn, vha, 0x701f,
731 "Get port config failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700732 bsg_job->reply->reply_payload_rcv_len = 0;
733 bsg_job->reply->result = (DID_ERROR << 16);
734 rval = -EPERM;
735 goto done_free_dma_req;
736 }
737
738 if (elreq.options != EXTERNAL_LOOPBACK) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700739 ql_dbg(ql_dbg_user, vha, 0x7020,
740 "Internal: curent port config = %x\n",
741 config[0]);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700742 if (qla81xx_set_internal_loopback(vha, config,
743 new_config)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700744 ql_log(ql_log_warn, vha, 0x7024,
745 "Internal loopback failed.\n");
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700746 bsg_job->reply->reply_payload_rcv_len =
747 0;
748 bsg_job->reply->result =
749 (DID_ERROR << 16);
750 rval = -EPERM;
751 goto done_free_dma_req;
752 }
753 } else {
754 /* For external loopback to work
755 * ensure internal loopback is disabled
756 */
757 if (qla81xx_reset_internal_loopback(vha,
758 config, 1)) {
759 bsg_job->reply->reply_payload_rcv_len =
760 0;
761 bsg_job->reply->result =
762 (DID_ERROR << 16);
763 rval = -EPERM;
764 goto done_free_dma_req;
765 }
766 }
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 */
779 qla81xx_reset_internal_loopback(vha,
780 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
797 bsg_job->reply->reply_payload_rcv_len = 0;
798 bsg_job->reply->result = (DID_ERROR << 16);
799 rval = -EIO;
800 goto done_free_dma_req;
801 }
802 } else {
803 type = "FC_BSG_HST_VENDOR_LOOPBACK";
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700804 ql_dbg(ql_dbg_user, vha, 0x702b,
805 "BSG request type: %s.\n", type);
Sarang Radke23f2ebd2010-05-28 15:08:21 -0700806 command_sent = INT_DEF_LB_LOOPBACK_CMD;
807 rval = qla2x00_loopback_test(vha, &elreq, response);
808 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700809 }
810
811 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700812 ql_log(ql_log_warn, vha, 0x702c,
813 "Vendor request %s failed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700814
815 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700816 sizeof(struct fc_bsg_reply);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700817
818 memcpy(fw_sts_ptr, response, sizeof(response));
819 fw_sts_ptr += sizeof(response);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700820 *fw_sts_ptr = command_sent;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700821 rval = 0;
822 bsg_job->reply->reply_payload_rcv_len = 0;
823 bsg_job->reply->result = (DID_ERROR << 16);
824 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700825 ql_dbg(ql_dbg_user, vha, 0x702d,
826 "Vendor request %s completed.\n", type);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700827
828 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
829 sizeof(response) + sizeof(uint8_t);
830 bsg_job->reply->reply_payload_rcv_len =
831 bsg_job->reply_payload.payload_len;
832 fw_sts_ptr = ((uint8_t *)bsg_job->req->sense) +
833 sizeof(struct fc_bsg_reply);
834 memcpy(fw_sts_ptr, response, sizeof(response));
835 fw_sts_ptr += sizeof(response);
836 *fw_sts_ptr = command_sent;
837 bsg_job->reply->result = DID_OK;
838 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
839 bsg_job->reply_payload.sg_cnt, rsp_data,
840 rsp_data_len);
841 }
842 bsg_job->job_done(bsg_job);
843
844 dma_free_coherent(&ha->pdev->dev, rsp_data_len,
845 rsp_data, rsp_data_dma);
846done_free_dma_req:
847 dma_free_coherent(&ha->pdev->dev, req_data_len,
848 req_data, req_data_dma);
849done_unmap_sg:
850 dma_unmap_sg(&ha->pdev->dev,
851 bsg_job->reply_payload.sg_list,
852 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
853done_unmap_req_sg:
854 dma_unmap_sg(&ha->pdev->dev,
855 bsg_job->request_payload.sg_list,
856 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Andrew Vasquez6c452a42010-03-19 17:04:02 -0700857 return rval;
Giridhar Malavali6e980162010-03-19 17:03:58 -0700858}
859
860static int
861qla84xx_reset(struct fc_bsg_job *bsg_job)
862{
863 struct Scsi_Host *host = bsg_job->shost;
864 scsi_qla_host_t *vha = shost_priv(host);
865 struct qla_hw_data *ha = vha->hw;
866 int rval = 0;
867 uint32_t flag;
868
Giridhar Malavali6e980162010-03-19 17:03:58 -0700869 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700870 ql_dbg(ql_dbg_user, vha, 0x702f, "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700871 return -EINVAL;
872 }
873
874 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
875
876 rval = qla84xx_reset_chip(vha, flag == A84_ISSUE_RESET_DIAG_FW);
877
878 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700879 ql_log(ql_log_warn, vha, 0x7030,
880 "Vendor request 84xx reset failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700881 rval = bsg_job->reply->reply_payload_rcv_len = 0;
882 bsg_job->reply->result = (DID_ERROR << 16);
883
884 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700885 ql_dbg(ql_dbg_user, vha, 0x7031,
886 "Vendor request 84xx reset completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700887 bsg_job->reply->result = DID_OK;
888 }
889
890 bsg_job->job_done(bsg_job);
891 return rval;
892}
893
894static int
895qla84xx_updatefw(struct fc_bsg_job *bsg_job)
896{
897 struct Scsi_Host *host = bsg_job->shost;
898 scsi_qla_host_t *vha = shost_priv(host);
899 struct qla_hw_data *ha = vha->hw;
900 struct verify_chip_entry_84xx *mn = NULL;
901 dma_addr_t mn_dma, fw_dma;
902 void *fw_buf = NULL;
903 int rval = 0;
904 uint32_t sg_cnt;
905 uint32_t data_len;
906 uint16_t options;
907 uint32_t flag;
908 uint32_t fw_ver;
909
Giridhar Malavali6e980162010-03-19 17:03:58 -0700910 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700911 ql_dbg(ql_dbg_user, vha, 0x7032,
912 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700913 return -EINVAL;
914 }
915
916 sg_cnt = dma_map_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
917 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700918 if (!sg_cnt) {
919 ql_log(ql_log_warn, vha, 0x7033,
920 "dma_map_sg returned %d for request.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700921 return -ENOMEM;
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700922 }
Giridhar Malavali6e980162010-03-19 17:03:58 -0700923
924 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700925 ql_log(ql_log_warn, vha, 0x7034,
926 "DMA mapping resulted in different sg counts, "
927 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
928 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -0700929 rval = -EAGAIN;
930 goto done_unmap_sg;
931 }
932
933 data_len = bsg_job->request_payload.payload_len;
934 fw_buf = dma_alloc_coherent(&ha->pdev->dev, data_len,
935 &fw_dma, GFP_KERNEL);
936 if (!fw_buf) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700937 ql_log(ql_log_warn, vha, 0x7035,
938 "DMA alloc failed for fw_buf.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700939 rval = -ENOMEM;
940 goto done_unmap_sg;
941 }
942
943 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
944 bsg_job->request_payload.sg_cnt, fw_buf, data_len);
945
946 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
947 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700948 ql_log(ql_log_warn, vha, 0x7036,
949 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700950 rval = -ENOMEM;
951 goto done_free_fw_buf;
952 }
953
954 flag = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
955 fw_ver = le32_to_cpu(*((uint32_t *)((uint32_t *)fw_buf + 2)));
956
957 memset(mn, 0, sizeof(struct access_chip_84xx));
958 mn->entry_type = VERIFY_CHIP_IOCB_TYPE;
959 mn->entry_count = 1;
960
961 options = VCO_FORCE_UPDATE | VCO_END_OF_DATA;
962 if (flag == A84_ISSUE_UPDATE_DIAGFW_CMD)
963 options |= VCO_DIAG_FW;
964
965 mn->options = cpu_to_le16(options);
966 mn->fw_ver = cpu_to_le32(fw_ver);
967 mn->fw_size = cpu_to_le32(data_len);
968 mn->fw_seq_size = cpu_to_le32(data_len);
969 mn->dseg_address[0] = cpu_to_le32(LSD(fw_dma));
970 mn->dseg_address[1] = cpu_to_le32(MSD(fw_dma));
971 mn->dseg_length = cpu_to_le32(data_len);
972 mn->data_seg_cnt = cpu_to_le16(1);
973
974 rval = qla2x00_issue_iocb_timeout(vha, mn, mn_dma, 0, 120);
975
976 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700977 ql_log(ql_log_warn, vha, 0x7037,
978 "Vendor request 84xx updatefw failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700979
980 rval = bsg_job->reply->reply_payload_rcv_len = 0;
981 bsg_job->reply->result = (DID_ERROR << 16);
982
983 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700984 ql_dbg(ql_dbg_user, vha, 0x7038,
985 "Vendor request 84xx updatefw completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -0700986
987 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
988 bsg_job->reply->result = DID_OK;
989 }
990
991 bsg_job->job_done(bsg_job);
992 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
993
994done_free_fw_buf:
995 dma_free_coherent(&ha->pdev->dev, data_len, fw_buf, fw_dma);
996
997done_unmap_sg:
998 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
999 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1000
1001 return rval;
1002}
1003
1004static int
1005qla84xx_mgmt_cmd(struct fc_bsg_job *bsg_job)
1006{
1007 struct Scsi_Host *host = bsg_job->shost;
1008 scsi_qla_host_t *vha = shost_priv(host);
1009 struct qla_hw_data *ha = vha->hw;
1010 struct access_chip_84xx *mn = NULL;
1011 dma_addr_t mn_dma, mgmt_dma;
1012 void *mgmt_b = NULL;
1013 int rval = 0;
1014 struct qla_bsg_a84_mgmt *ql84_mgmt;
1015 uint32_t sg_cnt;
Harish Zunjarraod5459082010-03-19 17:04:00 -07001016 uint32_t data_len = 0;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001017 uint32_t dma_direction = DMA_NONE;
1018
Giridhar Malavali6e980162010-03-19 17:03:58 -07001019 if (!IS_QLA84XX(ha)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001020 ql_log(ql_log_warn, vha, 0x703a,
1021 "Not 84xx, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001022 return -EINVAL;
1023 }
1024
1025 ql84_mgmt = (struct qla_bsg_a84_mgmt *)((char *)bsg_job->request +
1026 sizeof(struct fc_bsg_request));
1027 if (!ql84_mgmt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001028 ql_log(ql_log_warn, vha, 0x703b,
1029 "MGMT header not provided, exiting.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001030 return -EINVAL;
1031 }
1032
1033 mn = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &mn_dma);
1034 if (!mn) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001035 ql_log(ql_log_warn, vha, 0x703c,
1036 "DMA alloc failed for fw buffer.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001037 return -ENOMEM;
1038 }
1039
1040 memset(mn, 0, sizeof(struct access_chip_84xx));
1041 mn->entry_type = ACCESS_CHIP_IOCB_TYPE;
1042 mn->entry_count = 1;
1043
1044 switch (ql84_mgmt->mgmt.cmd) {
1045 case QLA84_MGMT_READ_MEM:
1046 case QLA84_MGMT_GET_INFO:
1047 sg_cnt = dma_map_sg(&ha->pdev->dev,
1048 bsg_job->reply_payload.sg_list,
1049 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1050 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001051 ql_log(ql_log_warn, vha, 0x703d,
1052 "dma_map_sg returned %d for reply.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001053 rval = -ENOMEM;
1054 goto exit_mgmt;
1055 }
1056
1057 dma_direction = DMA_FROM_DEVICE;
1058
1059 if (sg_cnt != bsg_job->reply_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001060 ql_log(ql_log_warn, vha, 0x703e,
1061 "DMA mapping resulted in different sg counts, "
1062 "reply_sg_cnt: %x dma_reply_sg_cnt: %x.\n",
1063 bsg_job->reply_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001064 rval = -EAGAIN;
1065 goto done_unmap_sg;
1066 }
1067
1068 data_len = bsg_job->reply_payload.payload_len;
1069
1070 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1071 &mgmt_dma, GFP_KERNEL);
1072 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001073 ql_log(ql_log_warn, vha, 0x703f,
1074 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001075 rval = -ENOMEM;
1076 goto done_unmap_sg;
1077 }
1078
1079 if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) {
1080 mn->options = cpu_to_le16(ACO_DUMP_MEMORY);
1081 mn->parameter1 =
1082 cpu_to_le32(
1083 ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1084
1085 } else if (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO) {
1086 mn->options = cpu_to_le16(ACO_REQUEST_INFO);
1087 mn->parameter1 =
1088 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.info.type);
1089
1090 mn->parameter2 =
1091 cpu_to_le32(
1092 ql84_mgmt->mgmt.mgmtp.u.info.context);
1093 }
1094 break;
1095
1096 case QLA84_MGMT_WRITE_MEM:
1097 sg_cnt = dma_map_sg(&ha->pdev->dev,
1098 bsg_job->request_payload.sg_list,
1099 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1100
1101 if (!sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001102 ql_log(ql_log_warn, vha, 0x7040,
1103 "dma_map_sg returned %d.\n", sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001104 rval = -ENOMEM;
1105 goto exit_mgmt;
1106 }
1107
1108 dma_direction = DMA_TO_DEVICE;
1109
1110 if (sg_cnt != bsg_job->request_payload.sg_cnt) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001111 ql_log(ql_log_warn, vha, 0x7041,
1112 "DMA mapping resulted in different sg counts, "
1113 "request_sg_cnt: %x dma_request_sg_cnt: %x.\n",
1114 bsg_job->request_payload.sg_cnt, sg_cnt);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001115 rval = -EAGAIN;
1116 goto done_unmap_sg;
1117 }
1118
1119 data_len = bsg_job->request_payload.payload_len;
1120 mgmt_b = dma_alloc_coherent(&ha->pdev->dev, data_len,
1121 &mgmt_dma, GFP_KERNEL);
1122 if (!mgmt_b) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001123 ql_log(ql_log_warn, vha, 0x7042,
1124 "DMA alloc failed for mgmt_b.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001125 rval = -ENOMEM;
1126 goto done_unmap_sg;
1127 }
1128
1129 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1130 bsg_job->request_payload.sg_cnt, mgmt_b, data_len);
1131
1132 mn->options = cpu_to_le16(ACO_LOAD_MEMORY);
1133 mn->parameter1 =
1134 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.mem.start_addr);
1135 break;
1136
1137 case QLA84_MGMT_CHNG_CONFIG:
1138 mn->options = cpu_to_le16(ACO_CHANGE_CONFIG_PARAM);
1139 mn->parameter1 =
1140 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.id);
1141
1142 mn->parameter2 =
1143 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param0);
1144
1145 mn->parameter3 =
1146 cpu_to_le32(ql84_mgmt->mgmt.mgmtp.u.config.param1);
1147 break;
1148
1149 default:
1150 rval = -EIO;
1151 goto exit_mgmt;
1152 }
1153
1154 if (ql84_mgmt->mgmt.cmd != QLA84_MGMT_CHNG_CONFIG) {
1155 mn->total_byte_cnt = cpu_to_le32(ql84_mgmt->mgmt.len);
1156 mn->dseg_count = cpu_to_le16(1);
1157 mn->dseg_address[0] = cpu_to_le32(LSD(mgmt_dma));
1158 mn->dseg_address[1] = cpu_to_le32(MSD(mgmt_dma));
1159 mn->dseg_length = cpu_to_le32(ql84_mgmt->mgmt.len);
1160 }
1161
1162 rval = qla2x00_issue_iocb(vha, mn, mn_dma, 0);
1163
1164 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001165 ql_log(ql_log_warn, vha, 0x7043,
1166 "Vendor request 84xx mgmt failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001167
1168 rval = bsg_job->reply->reply_payload_rcv_len = 0;
1169 bsg_job->reply->result = (DID_ERROR << 16);
1170
1171 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001172 ql_dbg(ql_dbg_user, vha, 0x7044,
1173 "Vendor request 84xx mgmt completed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001174
1175 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1176 bsg_job->reply->result = DID_OK;
1177
1178 if ((ql84_mgmt->mgmt.cmd == QLA84_MGMT_READ_MEM) ||
1179 (ql84_mgmt->mgmt.cmd == QLA84_MGMT_GET_INFO)) {
1180 bsg_job->reply->reply_payload_rcv_len =
1181 bsg_job->reply_payload.payload_len;
1182
1183 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001184 bsg_job->reply_payload.sg_cnt, mgmt_b,
1185 data_len);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001186 }
1187 }
1188
1189 bsg_job->job_done(bsg_job);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001190
1191done_unmap_sg:
Harish Zunjarraod5459082010-03-19 17:04:00 -07001192 if (mgmt_b)
1193 dma_free_coherent(&ha->pdev->dev, data_len, mgmt_b, mgmt_dma);
1194
Giridhar Malavali6e980162010-03-19 17:03:58 -07001195 if (dma_direction == DMA_TO_DEVICE)
1196 dma_unmap_sg(&ha->pdev->dev, bsg_job->request_payload.sg_list,
1197 bsg_job->request_payload.sg_cnt, DMA_TO_DEVICE);
1198 else if (dma_direction == DMA_FROM_DEVICE)
1199 dma_unmap_sg(&ha->pdev->dev, bsg_job->reply_payload.sg_list,
1200 bsg_job->reply_payload.sg_cnt, DMA_FROM_DEVICE);
1201
1202exit_mgmt:
1203 dma_pool_free(ha->s_dma_pool, mn, mn_dma);
1204
1205 return rval;
1206}
1207
1208static int
1209qla24xx_iidma(struct fc_bsg_job *bsg_job)
1210{
1211 struct Scsi_Host *host = bsg_job->shost;
1212 scsi_qla_host_t *vha = shost_priv(host);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001213 int rval = 0;
1214 struct qla_port_param *port_param = NULL;
1215 fc_port_t *fcport = NULL;
1216 uint16_t mb[MAILBOX_REGISTER_COUNT];
1217 uint8_t *rsp_ptr = NULL;
1218
1219 bsg_job->reply->reply_payload_rcv_len = 0;
1220
Giridhar Malavali6e980162010-03-19 17:03:58 -07001221 if (!IS_IIDMA_CAPABLE(vha->hw)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001222 ql_log(ql_log_info, vha, 0x7046, "iiDMA not supported.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001223 return -EINVAL;
1224 }
1225
1226 port_param = (struct qla_port_param *)((char *)bsg_job->request +
1227 sizeof(struct fc_bsg_request));
1228 if (!port_param) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001229 ql_log(ql_log_warn, vha, 0x7047,
1230 "port_param header not provided.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001231 return -EINVAL;
1232 }
1233
1234 if (port_param->fc_scsi_addr.dest_type != EXT_DEF_TYPE_WWPN) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001235 ql_log(ql_log_warn, vha, 0x7048,
1236 "Invalid destination type.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001237 return -EINVAL;
1238 }
1239
1240 list_for_each_entry(fcport, &vha->vp_fcports, list) {
1241 if (fcport->port_type != FCT_TARGET)
1242 continue;
1243
1244 if (memcmp(port_param->fc_scsi_addr.dest_addr.wwpn,
1245 fcport->port_name, sizeof(fcport->port_name)))
1246 continue;
1247 break;
1248 }
1249
1250 if (!fcport) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001251 ql_log(ql_log_warn, vha, 0x7049,
1252 "Failed to find port.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001253 return -EINVAL;
1254 }
1255
Giridhar Malavalic9afb9a2010-09-03 15:20:48 -07001256 if (atomic_read(&fcport->state) != FCS_ONLINE) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001257 ql_log(ql_log_warn, vha, 0x704a,
1258 "Port is not online.\n");
Madhuranath Iyengar17cf2c52010-07-23 15:28:22 +05001259 return -EINVAL;
1260 }
1261
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001262 if (fcport->flags & FCF_LOGIN_NEEDED) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001263 ql_log(ql_log_warn, vha, 0x704b,
1264 "Remote port not logged in flags = 0x%x.\n", fcport->flags);
Madhuranath Iyengar9a15eb42010-07-23 15:28:31 +05001265 return -EINVAL;
1266 }
1267
Giridhar Malavali6e980162010-03-19 17:03:58 -07001268 if (port_param->mode)
1269 rval = qla2x00_set_idma_speed(vha, fcport->loop_id,
1270 port_param->speed, mb);
1271 else
1272 rval = qla2x00_get_idma_speed(vha, fcport->loop_id,
1273 &port_param->speed, mb);
1274
1275 if (rval) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001276 ql_log(ql_log_warn, vha, 0x704c,
1277 "iIDMA cmd failed for %02x%02x%02x%02x%02x%02x%02x%02x -- "
1278 "%04x %x %04x %04x.\n", fcport->port_name[0],
1279 fcport->port_name[1], fcport->port_name[2],
1280 fcport->port_name[3], fcport->port_name[4],
1281 fcport->port_name[5], fcport->port_name[6],
1282 fcport->port_name[7], rval, fcport->fp_speed, mb[0], mb[1]);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001283 rval = 0;
1284 bsg_job->reply->result = (DID_ERROR << 16);
1285
1286 } else {
1287 if (!port_param->mode) {
1288 bsg_job->reply_len = sizeof(struct fc_bsg_reply) +
1289 sizeof(struct qla_port_param);
1290
1291 rsp_ptr = ((uint8_t *)bsg_job->reply) +
1292 sizeof(struct fc_bsg_reply);
1293
1294 memcpy(rsp_ptr, port_param,
1295 sizeof(struct qla_port_param));
1296 }
1297
1298 bsg_job->reply->result = DID_OK;
1299 }
1300
1301 bsg_job->job_done(bsg_job);
1302 return rval;
1303}
1304
1305static int
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001306qla2x00_optrom_setup(struct fc_bsg_job *bsg_job, scsi_qla_host_t *vha,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001307 uint8_t is_update)
1308{
1309 uint32_t start = 0;
1310 int valid = 0;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001311 struct qla_hw_data *ha = vha->hw;
Harish Zunjarraof19af162010-10-15 11:27:43 -07001312
1313 bsg_job->reply->reply_payload_rcv_len = 0;
1314
1315 if (unlikely(pci_channel_offline(ha->pdev)))
1316 return -EINVAL;
1317
1318 start = bsg_job->request->rqst_data.h_vendor.vendor_cmd[1];
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001319 if (start > ha->optrom_size) {
1320 ql_log(ql_log_warn, vha, 0x7055,
1321 "start %d > optrom_size %d.\n", start, ha->optrom_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001322 return -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001323 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001324
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001325 if (ha->optrom_state != QLA_SWAITING) {
1326 ql_log(ql_log_info, vha, 0x7056,
1327 "optrom_state %d.\n", ha->optrom_state);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001328 return -EBUSY;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001329 }
Harish Zunjarraof19af162010-10-15 11:27:43 -07001330
1331 ha->optrom_region_start = start;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001332 ql_dbg(ql_dbg_user, vha, 0x7057, "is_update=%d.\n", is_update);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001333 if (is_update) {
1334 if (ha->optrom_size == OPTROM_SIZE_2300 && start == 0)
1335 valid = 1;
1336 else if (start == (ha->flt_region_boot * 4) ||
1337 start == (ha->flt_region_fw * 4))
1338 valid = 1;
1339 else if (IS_QLA24XX_TYPE(ha) || IS_QLA25XX(ha) ||
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001340 IS_CNA_CAPABLE(ha) || IS_QLA2031(ha))
Harish Zunjarraof19af162010-10-15 11:27:43 -07001341 valid = 1;
1342 if (!valid) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001343 ql_log(ql_log_warn, vha, 0x7058,
1344 "Invalid start region 0x%x/0x%x.\n", start,
1345 bsg_job->request_payload.payload_len);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001346 return -EINVAL;
1347 }
1348
1349 ha->optrom_region_size = start +
1350 bsg_job->request_payload.payload_len > ha->optrom_size ?
1351 ha->optrom_size - start :
1352 bsg_job->request_payload.payload_len;
1353 ha->optrom_state = QLA_SWRITING;
1354 } else {
1355 ha->optrom_region_size = start +
1356 bsg_job->reply_payload.payload_len > ha->optrom_size ?
1357 ha->optrom_size - start :
1358 bsg_job->reply_payload.payload_len;
1359 ha->optrom_state = QLA_SREADING;
1360 }
1361
1362 ha->optrom_buffer = vmalloc(ha->optrom_region_size);
1363 if (!ha->optrom_buffer) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001364 ql_log(ql_log_warn, vha, 0x7059,
Harish Zunjarraof19af162010-10-15 11:27:43 -07001365 "Read: Unable to allocate memory for optrom retrieval "
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001366 "(%x)\n", ha->optrom_region_size);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001367
1368 ha->optrom_state = QLA_SWAITING;
1369 return -ENOMEM;
1370 }
1371
1372 memset(ha->optrom_buffer, 0, ha->optrom_region_size);
1373 return 0;
1374}
1375
1376static int
1377qla2x00_read_optrom(struct fc_bsg_job *bsg_job)
1378{
1379 struct Scsi_Host *host = bsg_job->shost;
1380 scsi_qla_host_t *vha = shost_priv(host);
1381 struct qla_hw_data *ha = vha->hw;
1382 int rval = 0;
1383
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001384 rval = qla2x00_optrom_setup(bsg_job, vha, 0);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001385 if (rval)
1386 return rval;
1387
1388 ha->isp_ops->read_optrom(vha, ha->optrom_buffer,
1389 ha->optrom_region_start, ha->optrom_region_size);
1390
1391 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1392 bsg_job->reply_payload.sg_cnt, ha->optrom_buffer,
1393 ha->optrom_region_size);
1394
1395 bsg_job->reply->reply_payload_rcv_len = ha->optrom_region_size;
1396 bsg_job->reply->result = DID_OK;
1397 vfree(ha->optrom_buffer);
1398 ha->optrom_buffer = NULL;
1399 ha->optrom_state = QLA_SWAITING;
1400 bsg_job->job_done(bsg_job);
1401 return rval;
1402}
1403
1404static int
1405qla2x00_update_optrom(struct fc_bsg_job *bsg_job)
1406{
1407 struct Scsi_Host *host = bsg_job->shost;
1408 scsi_qla_host_t *vha = shost_priv(host);
1409 struct qla_hw_data *ha = vha->hw;
1410 int rval = 0;
1411
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001412 rval = qla2x00_optrom_setup(bsg_job, vha, 1);
Harish Zunjarraof19af162010-10-15 11:27:43 -07001413 if (rval)
1414 return rval;
1415
1416 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1417 bsg_job->request_payload.sg_cnt, ha->optrom_buffer,
1418 ha->optrom_region_size);
1419
1420 ha->isp_ops->write_optrom(vha, ha->optrom_buffer,
1421 ha->optrom_region_start, ha->optrom_region_size);
1422
1423 bsg_job->reply->result = DID_OK;
1424 vfree(ha->optrom_buffer);
1425 ha->optrom_buffer = NULL;
1426 ha->optrom_state = QLA_SWAITING;
1427 bsg_job->job_done(bsg_job);
1428 return rval;
1429}
1430
1431static int
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001432qla2x00_update_fru_versions(struct fc_bsg_job *bsg_job)
1433{
1434 struct Scsi_Host *host = bsg_job->shost;
1435 scsi_qla_host_t *vha = shost_priv(host);
1436 struct qla_hw_data *ha = vha->hw;
1437 int rval = 0;
1438 uint8_t bsg[DMA_POOL_SIZE];
1439 struct qla_image_version_list *list = (void *)bsg;
1440 struct qla_image_version *image;
1441 uint32_t count;
1442 dma_addr_t sfp_dma;
1443 void *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1444 if (!sfp) {
1445 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1446 EXT_STATUS_NO_MEMORY;
1447 goto done;
1448 }
1449
1450 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1451 bsg_job->request_payload.sg_cnt, list, sizeof(bsg));
1452
1453 image = list->version;
1454 count = list->count;
1455 while (count--) {
1456 memcpy(sfp, &image->field_info, sizeof(image->field_info));
1457 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1458 image->field_address.device, image->field_address.offset,
1459 sizeof(image->field_info), image->field_address.option);
1460 if (rval) {
1461 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1462 EXT_STATUS_MAILBOX;
1463 goto dealloc;
1464 }
1465 image++;
1466 }
1467
1468 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1469
1470dealloc:
1471 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1472
1473done:
1474 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1475 bsg_job->reply->result = DID_OK << 16;
1476 bsg_job->job_done(bsg_job);
1477
1478 return 0;
1479}
1480
1481static int
1482qla2x00_read_fru_status(struct fc_bsg_job *bsg_job)
1483{
1484 struct Scsi_Host *host = bsg_job->shost;
1485 scsi_qla_host_t *vha = shost_priv(host);
1486 struct qla_hw_data *ha = vha->hw;
1487 int rval = 0;
1488 uint8_t bsg[DMA_POOL_SIZE];
1489 struct qla_status_reg *sr = (void *)bsg;
1490 dma_addr_t sfp_dma;
1491 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1492 if (!sfp) {
1493 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1494 EXT_STATUS_NO_MEMORY;
1495 goto done;
1496 }
1497
1498 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1499 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1500
1501 rval = qla2x00_read_sfp(vha, sfp_dma, sfp,
1502 sr->field_address.device, sr->field_address.offset,
1503 sizeof(sr->status_reg), sr->field_address.option);
1504 sr->status_reg = *sfp;
1505
1506 if (rval) {
1507 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1508 EXT_STATUS_MAILBOX;
1509 goto dealloc;
1510 }
1511
1512 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
1513 bsg_job->reply_payload.sg_cnt, sr, sizeof(*sr));
1514
1515 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1516
1517dealloc:
1518 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1519
1520done:
1521 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1522 bsg_job->reply->reply_payload_rcv_len = sizeof(*sr);
1523 bsg_job->reply->result = DID_OK << 16;
1524 bsg_job->job_done(bsg_job);
1525
1526 return 0;
1527}
1528
1529static int
1530qla2x00_write_fru_status(struct fc_bsg_job *bsg_job)
1531{
1532 struct Scsi_Host *host = bsg_job->shost;
1533 scsi_qla_host_t *vha = shost_priv(host);
1534 struct qla_hw_data *ha = vha->hw;
1535 int rval = 0;
1536 uint8_t bsg[DMA_POOL_SIZE];
1537 struct qla_status_reg *sr = (void *)bsg;
1538 dma_addr_t sfp_dma;
1539 uint8_t *sfp = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL, &sfp_dma);
1540 if (!sfp) {
1541 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1542 EXT_STATUS_NO_MEMORY;
1543 goto done;
1544 }
1545
1546 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
1547 bsg_job->request_payload.sg_cnt, sr, sizeof(*sr));
1548
1549 *sfp = sr->status_reg;
1550 rval = qla2x00_write_sfp(vha, sfp_dma, sfp,
1551 sr->field_address.device, sr->field_address.offset,
1552 sizeof(sr->status_reg), sr->field_address.option);
1553
1554 if (rval) {
1555 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] =
1556 EXT_STATUS_MAILBOX;
1557 goto dealloc;
1558 }
1559
1560 bsg_job->reply->reply_data.vendor_reply.vendor_rsp[0] = 0;
1561
1562dealloc:
1563 dma_pool_free(ha->s_dma_pool, sfp, sfp_dma);
1564
1565done:
1566 bsg_job->reply_len = sizeof(struct fc_bsg_reply);
1567 bsg_job->reply->result = DID_OK << 16;
1568 bsg_job->job_done(bsg_job);
1569
1570 return 0;
1571}
1572
1573static int
Giridhar Malavali6e980162010-03-19 17:03:58 -07001574qla2x00_process_vendor_specific(struct fc_bsg_job *bsg_job)
1575{
1576 switch (bsg_job->request->rqst_data.h_vendor.vendor_cmd[0]) {
1577 case QL_VND_LOOPBACK:
1578 return qla2x00_process_loopback(bsg_job);
1579
1580 case QL_VND_A84_RESET:
1581 return qla84xx_reset(bsg_job);
1582
1583 case QL_VND_A84_UPDATE_FW:
1584 return qla84xx_updatefw(bsg_job);
1585
1586 case QL_VND_A84_MGMT_CMD:
1587 return qla84xx_mgmt_cmd(bsg_job);
1588
1589 case QL_VND_IIDMA:
1590 return qla24xx_iidma(bsg_job);
1591
Sarang Radke09ff7012010-03-19 17:03:59 -07001592 case QL_VND_FCP_PRIO_CFG_CMD:
1593 return qla24xx_proc_fcp_prio_cfg_cmd(bsg_job);
1594
Harish Zunjarraof19af162010-10-15 11:27:43 -07001595 case QL_VND_READ_FLASH:
1596 return qla2x00_read_optrom(bsg_job);
1597
1598 case QL_VND_UPDATE_FLASH:
1599 return qla2x00_update_optrom(bsg_job);
1600
Joe Carnuccio697a4bc2011-08-16 11:31:52 -07001601 case QL_VND_SET_FRU_VERSION:
1602 return qla2x00_update_fru_versions(bsg_job);
1603
1604 case QL_VND_READ_FRU_STATUS:
1605 return qla2x00_read_fru_status(bsg_job);
1606
1607 case QL_VND_WRITE_FRU_STATUS:
1608 return qla2x00_write_fru_status(bsg_job);
1609
Giridhar Malavali6e980162010-03-19 17:03:58 -07001610 default:
1611 bsg_job->reply->result = (DID_ERROR << 16);
1612 bsg_job->job_done(bsg_job);
1613 return -ENOSYS;
1614 }
1615}
1616
1617int
1618qla24xx_bsg_request(struct fc_bsg_job *bsg_job)
1619{
1620 int ret = -EINVAL;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001621 struct fc_rport *rport;
1622 fc_port_t *fcport = NULL;
1623 struct Scsi_Host *host;
1624 scsi_qla_host_t *vha;
1625
1626 if (bsg_job->request->msgcode == FC_BSG_RPT_ELS) {
1627 rport = bsg_job->rport;
1628 fcport = *(fc_port_t **) rport->dd_data;
1629 host = rport_to_shost(rport);
1630 vha = shost_priv(host);
1631 } else {
1632 host = bsg_job->shost;
1633 vha = shost_priv(host);
1634 }
1635
Andrew Vasquezd051a5aa2012-02-09 11:14:05 -08001636 if (qla2x00_reset_active(vha)) {
1637 ql_dbg(ql_dbg_user, vha, 0x709f,
1638 "BSG: ISP abort active/needed -- cmd=%d.\n",
1639 bsg_job->request->msgcode);
1640 bsg_job->reply->result = (DID_ERROR << 16);
1641 bsg_job->job_done(bsg_job);
1642 return -EBUSY;
1643 }
1644
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001645 ql_dbg(ql_dbg_user, vha, 0x7000,
Chad Dupuiscfb09192011-11-18 09:03:07 -08001646 "Entered %s msgcode=0x%x.\n", __func__, bsg_job->request->msgcode);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001647
1648 switch (bsg_job->request->msgcode) {
1649 case FC_BSG_RPT_ELS:
1650 case FC_BSG_HST_ELS_NOLOGIN:
1651 ret = qla2x00_process_els(bsg_job);
1652 break;
1653 case FC_BSG_HST_CT:
1654 ret = qla2x00_process_ct(bsg_job);
1655 break;
1656 case FC_BSG_HST_VENDOR:
1657 ret = qla2x00_process_vendor_specific(bsg_job);
1658 break;
1659 case FC_BSG_HST_ADD_RPORT:
1660 case FC_BSG_HST_DEL_RPORT:
1661 case FC_BSG_RPT_CT:
1662 default:
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001663 ql_log(ql_log_warn, vha, 0x705a, "Unsupported BSG request.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001664 break;
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001665 }
Giridhar Malavali6e980162010-03-19 17:03:58 -07001666 return ret;
1667}
1668
1669int
1670qla24xx_bsg_timeout(struct fc_bsg_job *bsg_job)
1671{
1672 scsi_qla_host_t *vha = shost_priv(bsg_job->shost);
1673 struct qla_hw_data *ha = vha->hw;
1674 srb_t *sp;
1675 int cnt, que;
1676 unsigned long flags;
1677 struct req_que *req;
Giridhar Malavali6e980162010-03-19 17:03:58 -07001678
1679 /* find the bsg job from the active list of commands */
1680 spin_lock_irqsave(&ha->hardware_lock, flags);
1681 for (que = 0; que < ha->max_req_queues; que++) {
1682 req = ha->req_q_map[que];
1683 if (!req)
1684 continue;
1685
Andrew Vasquez6c452a42010-03-19 17:04:02 -07001686 for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
Giridhar Malavali6e980162010-03-19 17:03:58 -07001687 sp = req->outstanding_cmds[cnt];
Giridhar Malavali6e980162010-03-19 17:03:58 -07001688 if (sp) {
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001689 if (((sp->type == SRB_CT_CMD) ||
1690 (sp->type == SRB_ELS_CMD_HST))
1691 && (sp->u.bsg_job == bsg_job)) {
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001692 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001693 if (ha->isp_ops->abort_command(sp)) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001694 ql_log(ql_log_warn, vha, 0x7089,
1695 "mbx abort_command "
1696 "failed.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001697 bsg_job->req->errors =
1698 bsg_job->reply->result = -EIO;
1699 } else {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001700 ql_dbg(ql_dbg_user, vha, 0x708a,
1701 "mbx abort_command "
1702 "success.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001703 bsg_job->req->errors =
1704 bsg_job->reply->result = 0;
1705 }
Giridhar Malavali900a36e2010-12-21 16:00:26 -08001706 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001707 goto done;
1708 }
1709 }
1710 }
1711 }
1712 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001713 ql_log(ql_log_info, vha, 0x708b, "SRB not found to abort.\n");
Giridhar Malavali6e980162010-03-19 17:03:58 -07001714 bsg_job->req->errors = bsg_job->reply->result = -ENXIO;
1715 return 0;
1716
1717done:
1718 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1719 if (bsg_job->request->msgcode == FC_BSG_HST_CT)
1720 kfree(sp->fcport);
Giridhar Malavali6e980162010-03-19 17:03:58 -07001721 mempool_free(sp, ha->srb_mempool);
1722 return 0;
1723}