blob: 15032f130f22e0e0d20de0f924e417342fbd40d2 [file] [log] [blame]
Vikas Chaudharya3559432011-07-25 13:48:51 -05001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2011 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7
8#include "ql4_def.h"
9#include "ql4_glbl.h"
10#include "ql4_bsg.h"
11
12static int
13qla4xxx_read_flash(struct bsg_job *bsg_job)
14{
15 struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
16 struct scsi_qla_host *ha = to_qla_host(host);
17 struct iscsi_bsg_reply *bsg_reply = bsg_job->reply;
18 struct iscsi_bsg_request *bsg_req = bsg_job->request;
Vikas Chaudharya3559432011-07-25 13:48:51 -050019 uint32_t offset = 0;
20 uint32_t length = 0;
21 dma_addr_t flash_dma;
22 uint8_t *flash = NULL;
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070023 int rval = -EINVAL;
Vikas Chaudharya3559432011-07-25 13:48:51 -050024
25 bsg_reply->reply_payload_rcv_len = 0;
26
27 if (unlikely(pci_channel_offline(ha->pdev)))
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070028 goto leave;
Vikas Chaudharya3559432011-07-25 13:48:51 -050029
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070030 if (ql4xxx_reset_active(ha)) {
31 ql4_printk(KERN_ERR, ha, "%s: reset active\n", __func__);
32 rval = -EBUSY;
33 goto leave;
Vikas Chaudharya3559432011-07-25 13:48:51 -050034 }
35
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070036 if (ha->flash_state != QLFLASH_WAITING) {
37 ql4_printk(KERN_ERR, ha, "%s: another flash operation "
38 "active\n", __func__);
39 rval = -EBUSY;
40 goto leave;
41 }
42
43 ha->flash_state = QLFLASH_READING;
Vikas Chaudharya3559432011-07-25 13:48:51 -050044 offset = bsg_req->rqst_data.h_vendor.vendor_cmd[1];
45 length = bsg_job->reply_payload.payload_len;
46
47 flash = dma_alloc_coherent(&ha->pdev->dev, length, &flash_dma,
48 GFP_KERNEL);
49 if (!flash) {
50 ql4_printk(KERN_ERR, ha, "%s: dma alloc failed for flash "
51 "data\n", __func__);
52 rval = -ENOMEM;
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070053 goto leave;
Vikas Chaudharya3559432011-07-25 13:48:51 -050054 }
55
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070056 rval = qla4xxx_get_flash(ha, flash_dma, offset, length);
57 if (rval) {
58 ql4_printk(KERN_ERR, ha, "%s: get flash failed\n", __func__);
59 bsg_reply->result = DID_ERROR << 16;
60 rval = -EIO;
61 } else {
62 bsg_reply->reply_payload_rcv_len =
63 sg_copy_from_buffer(bsg_job->reply_payload.sg_list,
64 bsg_job->reply_payload.sg_cnt,
65 flash, length);
66 bsg_reply->result = DID_OK << 16;
Vikas Chaudharya3559432011-07-25 13:48:51 -050067 }
68
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070069 bsg_job_done(bsg_job, bsg_reply->result,
70 bsg_reply->reply_payload_rcv_len);
71 dma_free_coherent(&ha->pdev->dev, length, flash, flash_dma);
72leave:
Vikas Chaudharya3559432011-07-25 13:48:51 -050073 ha->flash_state = QLFLASH_WAITING;
Vikas Chaudharya3559432011-07-25 13:48:51 -050074 return rval;
75}
76
77static int
78qla4xxx_update_flash(struct bsg_job *bsg_job)
79{
80 struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
81 struct scsi_qla_host *ha = to_qla_host(host);
82 struct iscsi_bsg_reply *bsg_reply = bsg_job->reply;
83 struct iscsi_bsg_request *bsg_req = bsg_job->request;
Vikas Chaudharya3559432011-07-25 13:48:51 -050084 uint32_t length = 0;
85 uint32_t offset = 0;
86 uint32_t options = 0;
87 dma_addr_t flash_dma;
88 uint8_t *flash = NULL;
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070089 int rval = -EINVAL;
Vikas Chaudharya3559432011-07-25 13:48:51 -050090
91 bsg_reply->reply_payload_rcv_len = 0;
92
93 if (unlikely(pci_channel_offline(ha->pdev)))
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070094 goto leave;
Vikas Chaudharya3559432011-07-25 13:48:51 -050095
Harish Zunjarraoef7830b2011-08-01 03:26:14 -070096 if (ql4xxx_reset_active(ha)) {
97 ql4_printk(KERN_ERR, ha, "%s: reset active\n", __func__);
98 rval = -EBUSY;
99 goto leave;
Vikas Chaudharya3559432011-07-25 13:48:51 -0500100 }
101
Harish Zunjarraoef7830b2011-08-01 03:26:14 -0700102 if (ha->flash_state != QLFLASH_WAITING) {
103 ql4_printk(KERN_ERR, ha, "%s: another flash operation "
104 "active\n", __func__);
105 rval = -EBUSY;
106 goto leave;
107 }
108
109 ha->flash_state = QLFLASH_WRITING;
Vikas Chaudharya3559432011-07-25 13:48:51 -0500110 length = bsg_job->request_payload.payload_len;
111 offset = bsg_req->rqst_data.h_vendor.vendor_cmd[1];
112 options = bsg_req->rqst_data.h_vendor.vendor_cmd[2];
113
114 flash = dma_alloc_coherent(&ha->pdev->dev, length, &flash_dma,
115 GFP_KERNEL);
116 if (!flash) {
117 ql4_printk(KERN_ERR, ha, "%s: dma alloc failed for flash "
118 "data\n", __func__);
119 rval = -ENOMEM;
Harish Zunjarraoef7830b2011-08-01 03:26:14 -0700120 goto leave;
Vikas Chaudharya3559432011-07-25 13:48:51 -0500121 }
122
Vikas Chaudharya3559432011-07-25 13:48:51 -0500123 sg_copy_to_buffer(bsg_job->request_payload.sg_list,
124 bsg_job->request_payload.sg_cnt, flash, length);
125
Harish Zunjarraoef7830b2011-08-01 03:26:14 -0700126 rval = qla4xxx_set_flash(ha, flash_dma, offset, length, options);
127 if (rval) {
128 ql4_printk(KERN_ERR, ha, "%s: set flash failed\n", __func__);
129 bsg_reply->result = DID_ERROR << 16;
130 rval = -EIO;
131 } else
132 bsg_reply->result = DID_OK << 16;
Vikas Chaudharya3559432011-07-25 13:48:51 -0500133
Harish Zunjarraoef7830b2011-08-01 03:26:14 -0700134 bsg_job_done(bsg_job, bsg_reply->result,
135 bsg_reply->reply_payload_rcv_len);
136 dma_free_coherent(&ha->pdev->dev, length, flash, flash_dma);
137leave:
Vikas Chaudharya3559432011-07-25 13:48:51 -0500138 ha->flash_state = QLFLASH_WAITING;
Vikas Chaudharya3559432011-07-25 13:48:51 -0500139 return rval;
140}
141
142/**
143 * qla4xxx_process_vendor_specific - handle vendor specific bsg request
144 * @job: iscsi_bsg_job to handle
145 **/
146int qla4xxx_process_vendor_specific(struct bsg_job *bsg_job)
147{
148 struct iscsi_bsg_reply *bsg_reply = bsg_job->reply;
149 struct iscsi_bsg_request *bsg_req = bsg_job->request;
150 struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
151 struct scsi_qla_host *ha = to_qla_host(host);
152
153 switch (bsg_req->rqst_data.h_vendor.vendor_cmd[0]) {
154 case QLISCSI_VND_READ_FLASH:
155 return qla4xxx_read_flash(bsg_job);
156
157 case QLISCSI_VND_UPDATE_FLASH:
158 return qla4xxx_update_flash(bsg_job);
159
160 default:
161 ql4_printk(KERN_ERR, ha, "%s: invalid BSG vendor command: "
162 "0x%x\n", __func__, bsg_req->msgcode);
163 bsg_reply->result = (DID_ERROR << 16);
164 bsg_reply->reply_payload_rcv_len = 0;
165 bsg_job_done(bsg_job, bsg_reply->result,
166 bsg_reply->reply_payload_rcv_len);
167 return -ENOSYS;
168 }
169}
170
171/**
172 * qla4xxx_bsg_request - handle bsg request from ISCSI transport
173 * @job: iscsi_bsg_job to handle
174 */
175int qla4xxx_bsg_request(struct bsg_job *bsg_job)
176{
177 struct iscsi_bsg_request *bsg_req = bsg_job->request;
178 struct Scsi_Host *host = iscsi_job_to_shost(bsg_job);
179 struct scsi_qla_host *ha = to_qla_host(host);
180
181 switch (bsg_req->msgcode) {
182 case ISCSI_BSG_HST_VENDOR:
183 return qla4xxx_process_vendor_specific(bsg_job);
184
185 default:
186 ql4_printk(KERN_ERR, ha, "%s: invalid BSG command: 0x%x\n",
187 __func__, bsg_req->msgcode);
188 }
189
190 return -ENOSYS;
191}