1 /*-
2  * Copyright 2016-2021 Microchip Technology, Inc. and/or its subsidiaries.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 /* $FreeBSD$ */
27 
28 #include "smartpqi_includes.h"
29 
30 /*
31  * Process internal RAID response in the case of success.
32  */
33 void
34 pqisrc_process_internal_raid_response_success(pqisrc_softstate_t *softs,rcb_t *rcb)
35 {
36 	DBG_FUNC("IN");
37 
38 	rcb->status = REQUEST_SUCCESS;
39 	rcb->req_pending = false;
40 
41 	DBG_FUNC("OUT");
42 }
43 
44 /*
45  * Process internal RAID response in the case of failure.
46  */
47 void
48 pqisrc_process_internal_raid_response_error(pqisrc_softstate_t *softs,
49 				       rcb_t *rcb, uint16_t err_idx)
50 {
51 	raid_path_error_info_elem_t error_info;
52 
53 	DBG_FUNC("IN");
54 
55 	rcb->error_info = (char *) (softs->err_buf_dma_mem.virt_addr) +
56 			  (err_idx * PQI_ERROR_BUFFER_ELEMENT_LENGTH);
57 
58 	memcpy(&error_info, rcb->error_info, sizeof(error_info));
59 
60 	DBG_INFO("error_status 0x%x data_in_result 0x%x data_out_result 0x%x\n",
61 		error_info.status, error_info.data_in_result, error_info.data_out_result);
62 
63 	rcb->status = REQUEST_FAILED;
64 
65 	switch (error_info.data_out_result) {
66 	case PQI_RAID_DATA_IN_OUT_GOOD:
67 		if (error_info.status == PQI_RAID_DATA_IN_OUT_GOOD)
68 			rcb->status = REQUEST_SUCCESS;
69 		break;
70 	case PQI_RAID_DATA_IN_OUT_UNDERFLOW:
71 		if (error_info.status == PQI_RAID_DATA_IN_OUT_GOOD ||
72 				error_info.status == PQI_RAID_STATUS_CHECK_CONDITION)
73 			rcb->status = REQUEST_SUCCESS;
74 		break;
75 	}
76 
77 	rcb->req_pending = false;
78 
79 	DBG_FUNC("OUT");
80 }
81 
82 /*
83  * Process the AIO/RAID IO in the case of success.
84  */
85 void
86 pqisrc_process_io_response_success(pqisrc_softstate_t *softs, rcb_t *rcb)
87 {
88 	DBG_FUNC("IN");
89 
90 	os_io_response_success(rcb);
91 
92 	DBG_FUNC("OUT");
93 }
94 
95 static void
96 pqisrc_extract_sense_data(sense_data_u_t *sense_data, uint8_t *key, uint8_t *asc, uint8_t *ascq)
97 {
98 	if (sense_data->fixed_format.response_code == SCSI_SENSE_RESPONSE_70 ||
99 		sense_data->fixed_format.response_code == SCSI_SENSE_RESPONSE_71)
100 	{
101 		sense_data_fixed_t *fixed = &sense_data->fixed_format;
102 
103 		*key = fixed->sense_key;
104 		*asc = fixed->sense_code;
105 		*ascq = fixed->sense_qual;
106 	}
107 	else if (sense_data->descriptor_format.response_code == SCSI_SENSE_RESPONSE_72 ||
108 		sense_data->descriptor_format.response_code == SCSI_SENSE_RESPONSE_73)
109 	{
110 		sense_data_descriptor_t *desc = &sense_data->descriptor_format;
111 
112 		*key = desc->sense_key;
113 		*asc = desc->sense_code;
114 		*ascq = desc->sense_qual;
115 	}
116 	else
117 	{
118 		*key = 0xFF;
119 		*asc = 0xFF;
120 		*ascq = 0xFF;
121 	}
122 }
123 
124 static void
125 pqisrc_show_sense_data_simple(pqisrc_softstate_t *softs, rcb_t *rcb, sense_data_u_t *sense_data)
126 {
127 	uint8_t opcode = rcb->cdbp ? rcb->cdbp[0] : 0xFF;
128 	char *path = io_path_to_ascii(rcb->path);
129 	uint8_t key, asc, ascq;
130 	pqisrc_extract_sense_data(sense_data, &key, &asc, &ascq);
131 
132 	DBG_NOTE("[ERR INFO] BTL: %d:%d:%d op=0x%x path=%s K:C:Q: %x:%x:%x\n",
133 		rcb->dvp->bus, rcb->dvp->target, rcb->dvp->lun,
134 		opcode, path, key, asc, ascq);
135 }
136 
137 void
138 pqisrc_show_sense_data_full(pqisrc_softstate_t *softs, rcb_t *rcb, sense_data_u_t *sense_data)
139 {
140 	pqisrc_print_buffer(softs, "sense data", sense_data, 32, 0);
141 
142 	pqisrc_show_sense_data_simple(softs, rcb, sense_data);
143 
144 	/* add more detail here as needed */
145 }
146 
147 
148 /*
149  * Process the error info for AIO in the case of failure.
150  */
151 void
152 pqisrc_process_aio_response_error(pqisrc_softstate_t *softs,
153 		rcb_t *rcb, uint16_t err_idx)
154 {
155 	aio_path_error_info_elem_t *err_info = NULL;
156 
157 	DBG_FUNC("IN");
158 
159 	err_info = (aio_path_error_info_elem_t*)
160 			softs->err_buf_dma_mem.virt_addr +
161 			err_idx;
162 
163 	if(err_info == NULL) {
164 		DBG_ERR("err_info structure is NULL  err_idx :%x", err_idx);
165 		return;
166 	}
167 
168 	os_aio_response_error(rcb, err_info);
169 
170 	DBG_FUNC("OUT");
171 }
172 
173 /*
174  * Process the error info for RAID IO in the case of failure.
175  */
176 void
177 pqisrc_process_raid_response_error(pqisrc_softstate_t *softs,
178 		rcb_t *rcb, uint16_t err_idx)
179 {
180 	raid_path_error_info_elem_t *err_info = NULL;
181 
182 	DBG_FUNC("IN");
183 
184 	err_info = (raid_path_error_info_elem_t*)
185 			softs->err_buf_dma_mem.virt_addr +
186 			err_idx;
187 
188 	if(err_info == NULL) {
189 		DBG_ERR("err_info structure is NULL  err_idx :%x", err_idx);
190 		return;
191 	}
192 
193 	os_raid_response_error(rcb, err_info);
194 
195 	DBG_FUNC("OUT");
196 }
197 
198 /*
199  * Process the Task Management function response.
200  */
201 int
202 pqisrc_process_task_management_response(pqisrc_softstate_t *softs,
203 			pqi_tmf_resp_t *tmf_resp)
204 {
205 	int ret = REQUEST_SUCCESS;
206 	uint32_t tag = (uint32_t)tmf_resp->req_id;
207 	rcb_t *rcb = &softs->rcb[tag];
208 
209 	ASSERT(rcb->tag == tag);
210 
211 	DBG_FUNC("IN\n");
212 
213 	switch (tmf_resp->resp_code) {
214 	case SOP_TASK_MANAGEMENT_FUNCTION_COMPLETE:
215 	case SOP_TASK_MANAGEMENT_FUNCTION_SUCCEEDED:
216 		ret = REQUEST_SUCCESS;
217 		break;
218 	default:
219 		DBG_WARN("TMF Failed, Response code : 0x%x\n", tmf_resp->resp_code);
220 		ret = REQUEST_FAILED;
221 		break;
222 	}
223 
224 	rcb->status = ret;
225 	rcb->req_pending = false;
226 
227 	DBG_FUNC("OUT");
228 	return ret;
229 }
230 
231 static int
232 pqisrc_process_vendor_general_response(pqi_vendor_general_response_t *response)
233 {
234 
235 	int ret = REQUEST_SUCCESS;
236 
237 	switch(response->status) {
238 	case PQI_VENDOR_RESPONSE_IU_SUCCESS:
239 		break;
240 	case PQI_VENDOR_RESPONSE_IU_UNSUCCESS:
241 	case PQI_VENDOR_RESPONSE_IU_INVALID_PARAM:
242 	case PQI_VENDOR_RESPONSE_IU_INSUFF_RESRC:
243 		ret = REQUEST_FAILED;
244 		break;
245 	}
246 
247 	return ret;
248 }
249 
250 /*
251  * Function used to process the response from the adapter
252  * which is invoked by IRQ handler.
253  */
254 void
255 pqisrc_process_response_queue(pqisrc_softstate_t *softs, int oq_id)
256 {
257 	ob_queue_t *ob_q;
258 	struct pqi_io_response *response;
259 	uint32_t oq_pi, oq_ci;
260 	pqi_scsi_dev_t  *dvp = NULL;
261 
262 	DBG_FUNC("IN");
263 
264 	ob_q = &softs->op_ob_q[oq_id - 1]; /* zero for event Q */
265 	oq_ci = ob_q->ci_local;
266 	oq_pi = *(ob_q->pi_virt_addr);
267 
268 	DBG_INFO("ci : %d pi : %d qid : %d\n", oq_ci, oq_pi, ob_q->q_id);
269 
270 	while (1) {
271 		rcb_t *rcb = NULL;
272 		uint32_t tag = 0;
273 		uint32_t offset;
274 		boolean_t os_scsi_cmd = false;
275 
276 		if (oq_pi == oq_ci)
277 			break;
278 		/* Get the response */
279 		offset = oq_ci * ob_q->elem_size;
280 		response = (struct pqi_io_response *)(ob_q->array_virt_addr +
281 							offset);
282 		tag = response->request_id;
283 		rcb = &softs->rcb[tag];
284 		/* Make sure we are processing a valid response. */
285 		if ((rcb->tag != tag) || (rcb->req_pending == false)) {
286 			DBG_ERR("No such request pending with tag : %x", tag);
287 			oq_ci = (oq_ci + 1) % ob_q->num_elem;
288 			break;
289 		}
290 		/* Timedout request has been completed. This should not hit,
291 		 * if timeout is set as TIMEOUT_INFINITE while calling
292 		 * pqisrc_wait_on_condition(softs,rcb,timeout).
293 		 */
294 		if (rcb->timedout) {
295 			DBG_WARN("timed out request completing from firmware, driver already completed it with failure , free the tag %d\n", tag);
296 			oq_ci = (oq_ci + 1) % ob_q->num_elem;
297 			os_reset_rcb(rcb);
298 			pqisrc_put_tag(&softs->taglist, tag);
299 			break;
300 		}
301 
302 		if (IS_OS_SCSICMD(rcb)) {
303 			dvp = rcb->dvp;
304 			if (dvp)
305 				os_scsi_cmd = true;
306 			else
307 				DBG_WARN("Received IO completion for the Null device!!!\n");
308 		}
309 
310 
311 		DBG_INFO("response.header.iu_type : %x \n", response->header.iu_type);
312 
313 		switch (response->header.iu_type) {
314 		case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS:
315 		case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS:
316 			rcb->success_cmp_callback(softs, rcb);
317 			if (os_scsi_cmd)
318 				pqisrc_decrement_device_active_io(softs, dvp);
319 
320 			break;
321 		case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR:
322 		case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR:
323 			rcb->error_cmp_callback(softs, rcb, LE_16(response->error_index));
324 			if (os_scsi_cmd)
325 				pqisrc_decrement_device_active_io(softs, dvp);
326 			break;
327 		case PQI_RESPONSE_IU_GENERAL_MANAGEMENT:
328 			rcb->req_pending = false;
329 			break;
330 		case PQI_RESPONSE_IU_VENDOR_GENERAL:
331 			rcb->req_pending = false;
332 			rcb->status = pqisrc_process_vendor_general_response(
333 								(pqi_vendor_general_response_t *)response);
334 			break;
335 		case PQI_RESPONSE_IU_TASK_MANAGEMENT:
336 			rcb->status = pqisrc_process_task_management_response(softs, (void *)response);
337 			break;
338 
339 		default:
340 			DBG_ERR("Invalid Response IU 0x%x\n",response->header.iu_type);
341 			break;
342 		}
343 
344 		oq_ci = (oq_ci + 1) % ob_q->num_elem;
345 	}
346 
347 	ob_q->ci_local = oq_ci;
348 	PCI_MEM_PUT32(softs, ob_q->ci_register_abs,
349         ob_q->ci_register_offset, ob_q->ci_local );
350 	DBG_FUNC("OUT");
351 }
352