xref: /freebsd/sys/dev/smartpqi/smartpqi_event.c (revision 0d48d6d7bba77d6c3736ce56ea5667bfbde89705)
1 /*-
2  * Copyright 2016-2026 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 
27 #include"smartpqi_includes.h"
28 
29 /*
30  * Function to rescan the devices connected to adapter.
31  */
32 int
pqisrc_rescan_devices(pqisrc_softstate_t * softs)33 pqisrc_rescan_devices(pqisrc_softstate_t *softs)
34 {
35 	int ret;
36 
37 	DBG_FUNC("IN\n");
38 
39 	os_sema_lock(&softs->scan_lock);
40 
41 	ret = pqisrc_scan_devices(softs);
42 
43 	os_sema_unlock(&softs->scan_lock);
44 
45 	DBG_FUNC("OUT\n");
46 
47 	return ret;
48 }
49 
50 void
pqisrc_wait_for_rescan_complete(pqisrc_softstate_t * softs)51 pqisrc_wait_for_rescan_complete(pqisrc_softstate_t *softs)
52 {
53 	os_sema_lock(&softs->scan_lock);
54 	os_sema_unlock(&softs->scan_lock);
55 }
56 
57 /*
58  * Subroutine to acknowledge the events processed by the driver to the adapter.
59  */
60 static void
pqisrc_acknowledge_event(pqisrc_softstate_t * softs,struct pqi_event const * event)61 pqisrc_acknowledge_event(pqisrc_softstate_t *softs,
62 	struct pqi_event const *event)
63 {
64 
65 	int ret;
66 	pqi_event_acknowledge_request_t request;
67 	ib_queue_t *ib_q = &softs->op_raid_ib_q[0];
68 	int tmo = PQISRC_EVENT_ACK_RESP_TIMEOUT;
69 	memset(&request,0,sizeof(request));
70 
71 	DBG_FUNC("IN\n");
72 
73 	request.header.iu_type = PQI_REQUEST_IU_ACKNOWLEDGE_VENDOR_EVENT;
74 	request.header.iu_length = (sizeof(pqi_event_acknowledge_request_t) -
75 								PQI_REQUEST_HEADER_LENGTH);
76 	request.event_type = event->event_type;
77 	request.event_id   = event->event_id;
78 	request.additional_event_id = event->additional_event_id;
79 
80 	/* Submit Event Acknowledge */
81 	ret = pqisrc_submit_cmnd(softs, ib_q, &request);
82 	if (ret != PQI_STATUS_SUCCESS) {
83 		DBG_ERR("Unable to submit acknowledge command\n");
84 		goto out;
85 	}
86 
87 	/*
88 	 * We have to special-case this type of request because the firmware
89 	 * does not generate an interrupt when this type of request completes.
90 	 * Therefore, we have to poll until we see that the firmware has
91 	 * consumed the request before we move on.
92 	 */
93 
94 	COND_WAIT(((ib_q->pi_local) == *(ib_q->ci_virt_addr)), tmo);
95 		if (tmo <= 0) {
96 			DBG_ERR("wait for event acknowledge timed out\n");
97 			DBG_ERR("tmo : %d\n",tmo);
98  		}
99 
100 out:
101 	DBG_FUNC("OUT\n");
102 }
103 
104 /*
105  * Acknowledge processed events to the adapter.
106  */
107 void
pqisrc_ack_all_events(void * arg1)108 pqisrc_ack_all_events(void *arg1)
109 {
110 	int i;
111 	struct pqi_event *pending_event;
112 	pqisrc_softstate_t *softs = (pqisrc_softstate_t*)arg1;
113 
114 	DBG_FUNC(" IN\n");
115 
116 
117 	pending_event = &softs->pending_events[0];
118 	for (i = 0; i < PQI_NUM_SUPPORTED_EVENTS; i++) {
119 		if (pending_event->pending == true) {
120 			pending_event->pending = false;
121 			pqisrc_acknowledge_event(softs, pending_event);
122 		}
123 		pending_event++;
124 	}
125 
126 	/* Rescan devices except for heartbeat event */
127 	if ((pqisrc_rescan_devices(softs)) != PQI_STATUS_SUCCESS) {
128 			DBG_ERR(" Failed to Re-Scan devices\n ");
129 	}
130 	DBG_FUNC(" OUT\n");
131 
132 }
133 
134 /*
135  * Get event index from event type to validate the type of event.
136  */
137 static int
pqisrc_event_type_to_event_index(unsigned event_type)138 pqisrc_event_type_to_event_index(unsigned event_type)
139 {
140 	int index;
141 
142 	switch (event_type) {
143 	case PQI_EVENT_TYPE_HOTPLUG:
144 		index = PQI_EVENT_HOTPLUG;
145 		break;
146 	case PQI_EVENT_TYPE_HARDWARE:
147 		index = PQI_EVENT_HARDWARE;
148 		break;
149 	case PQI_EVENT_TYPE_PHYSICAL_DEVICE:
150 		index = PQI_EVENT_PHYSICAL_DEVICE;
151 		break;
152 	case PQI_EVENT_TYPE_LOGICAL_DEVICE:
153 		index = PQI_EVENT_LOGICAL_DEVICE;
154 		break;
155 	case PQI_EVENT_TYPE_AIO_STATE_CHANGE:
156 		index = PQI_EVENT_AIO_STATE_CHANGE;
157 		break;
158 	case PQI_EVENT_TYPE_AIO_CONFIG_CHANGE:
159 		index = PQI_EVENT_AIO_CONFIG_CHANGE;
160 		break;
161 	default:
162 		index = -1;
163 		break;
164 	}
165 
166 	return index;
167 }
168 
169 /*
170  * Function used to process the events supported by the adapter.
171  */
172 int
pqisrc_process_event_intr_src(pqisrc_softstate_t * softs,int obq_id)173 pqisrc_process_event_intr_src(pqisrc_softstate_t *softs,int obq_id)
174 {
175 	uint32_t obq_pi,obq_ci;
176 	pqi_event_response_t response;
177 	ob_queue_t *event_q;
178 	struct pqi_event *pending_event;
179 	boolean_t  need_delayed_work = false;
180 
181 	DBG_FUNC(" IN\n");
182 
183 	event_q = &softs->event_q;
184 	obq_ci = event_q->ci_local;
185 	obq_pi = *(event_q->pi_virt_addr);
186 
187 	while(1) {
188 		int event_index;
189 		DBG_INFO("Event queue_id : %d, ci : %u, pi : %u\n",obq_id, obq_ci, obq_pi);
190 		if (obq_pi == obq_ci)
191 			break;
192 
193 		need_delayed_work = true;
194 
195 		/* Copy the response */
196 		memcpy(&response, event_q->array_virt_addr + (obq_ci * event_q->elem_size),
197 					sizeof(pqi_event_response_t));
198 		DBG_INIT("event iu_type=0x%x event_type=0x%x\n",
199 			response.header.iu_type, response.event_type);
200 
201 		event_index = pqisrc_event_type_to_event_index(response.event_type);
202 		if ( event_index == PQI_EVENT_LOGICAL_DEVICE) {
203 			softs->ld_rescan = true;
204 		}
205 
206 		if (event_index >= 0) {
207 			static const char *event_names[] = {
208 				[PQI_EVENT_HOTPLUG]           = "hotplug",
209 				[PQI_EVENT_HARDWARE]          = "hardware",
210 				[PQI_EVENT_PHYSICAL_DEVICE]   = "physical device",
211 				[PQI_EVENT_LOGICAL_DEVICE]    = "logical device",
212 				[PQI_EVENT_AIO_STATE_CHANGE]  = "AIO state change",
213 				[PQI_EVENT_AIO_CONFIG_CHANGE] = "AIO config change",
214 			};
215 			device_printf(softs->os_specific.pqi_dev,
216 			    "event: %s (type=0x%x)\n",
217 			    event_names[event_index], response.event_type);
218 			if(response.request_acknowledge) {
219 				pending_event = &softs->pending_events[event_index];
220 				pending_event->pending = true;
221 				pending_event->event_type = response.event_type;
222 				pending_event->event_id = response.event_id;
223 				pending_event->additional_event_id = response.additional_event_id;
224 			}
225 		}
226 
227 	obq_ci = (obq_ci + 1) % event_q->num_elem;
228 	}
229     /* Update CI */
230 	event_q->ci_local = obq_ci;
231 	PCI_MEM_PUT32(softs, event_q->ci_register_abs,
232         event_q->ci_register_offset, event_q->ci_local);
233 
234 	/*Adding events to the task queue for acknowledging*/
235 	if (need_delayed_work == true) {
236 		os_eventtaskqueue_enqueue(softs);
237 	}
238 
239 	DBG_FUNC("OUT\n");
240 	return PQI_STATUS_SUCCESS;
241 
242 
243 }
244 
245 /*
246  * Function used to build and send the vendor general request
247  * Used for configuring PQI feature bits between firmware and driver
248  */
249 int
pqisrc_build_send_vendor_request(pqisrc_softstate_t * softs,struct pqi_vendor_general_request * request)250 pqisrc_build_send_vendor_request(pqisrc_softstate_t *softs,
251 	struct pqi_vendor_general_request *request)
252 {
253 	int ret = PQI_STATUS_SUCCESS;
254 	ib_queue_t *op_ib_q = &softs->op_raid_ib_q[PQI_DEFAULT_IB_QUEUE];
255 	ob_queue_t const *ob_q = &softs->op_ob_q[PQI_DEFAULT_IB_QUEUE];
256 
257 	rcb_t *rcb = NULL;
258 
259 	/* Get the tag */
260 	request->request_id = pqisrc_get_tag(&softs->taglist);
261 	if (INVALID_ELEM == request->request_id) {
262 		DBG_ERR("Tag not available\n");
263 		ret = PQI_STATUS_FAILURE;
264 		goto err_notag;
265 	}
266 
267 	request->response_id = ob_q->q_id;
268 
269 	rcb = &softs->rcb[request->request_id];
270 
271 	rcb->req_pending = true;
272 	rcb->tag = request->request_id;
273 
274 	ret = pqisrc_submit_cmnd(softs, op_ib_q, request);
275 
276 	if (ret != PQI_STATUS_SUCCESS) {
277 		DBG_ERR("Unable to submit command\n");
278 		goto err_out;
279 	}
280 
281 	ret = pqisrc_wait_on_condition(softs, rcb, PQISRC_CMD_TIMEOUT);
282 	if (ret != PQI_STATUS_SUCCESS) {
283 		DBG_ERR("Management request timed out!\n");
284 		goto err_out;
285 	}
286 
287 	ret = rcb->status;
288 
289 err_out:
290 	os_reset_rcb(rcb);
291 	pqisrc_put_tag(&softs->taglist, request->request_id);
292 err_notag:
293 	DBG_FUNC("OUT \n");
294 	return ret;
295 }
296 
297 /*
298  * Function used to send a general management request to adapter.
299  */
300 int
pqisrc_submit_management_req(pqisrc_softstate_t * softs,pqi_event_config_request_t * request)301 pqisrc_submit_management_req(pqisrc_softstate_t *softs,
302                                       pqi_event_config_request_t *request)
303 {
304 	int ret = PQI_STATUS_SUCCESS;
305 	ib_queue_t *op_ib_q = &softs->op_raid_ib_q[0];
306 	rcb_t *rcb = NULL;
307 
308 	DBG_FUNC(" IN\n");
309 
310 	/* Get the tag */
311 	request->request_id = pqisrc_get_tag(&softs->taglist);
312 	if (INVALID_ELEM == request->request_id) {
313 		DBG_ERR("Tag not available\n");
314 		ret = PQI_STATUS_FAILURE;
315 		goto err_out;
316 	}
317 
318 	rcb = &softs->rcb[request->request_id];
319 	rcb->req_pending = true;
320 	rcb->tag = request->request_id;
321 
322 	/* Submit command on operational raid ib queue */
323 	ret = pqisrc_submit_cmnd(softs, op_ib_q, request);
324 	if (ret != PQI_STATUS_SUCCESS) {
325 		DBG_ERR("  Unable to submit command\n");
326 		goto err_cmd;
327 	}
328 
329 	ret = pqisrc_wait_on_condition(softs, rcb, PQISRC_CMD_TIMEOUT);
330 
331 	if (ret != PQI_STATUS_SUCCESS) {
332 		DBG_ERR("Management request timed out !!\n");
333 		goto err_cmd;
334 	}
335 
336 	os_reset_rcb(rcb);
337  	pqisrc_put_tag(&softs->taglist,request->request_id);
338 	DBG_FUNC("OUT\n");
339 	return ret;
340 
341 err_cmd:
342 	os_reset_rcb(rcb);
343 	pqisrc_put_tag(&softs->taglist,request->request_id);
344 err_out:
345 	DBG_FUNC(" failed OUT : %d\n", ret);
346 	return ret;
347 }
348 
349 /*
350  * Build and send the general management request.
351  */
352 static int
pqi_event_configure(pqisrc_softstate_t * softs,pqi_event_config_request_t * request,dma_mem_t const * buff)353 pqi_event_configure(pqisrc_softstate_t *softs ,
354                               pqi_event_config_request_t *request,
355                               dma_mem_t const *buff)
356 {
357         int ret = PQI_STATUS_SUCCESS;
358 
359 	DBG_FUNC(" IN\n");
360 
361 	request->header.comp_feature = 0x00;
362 	request->header.iu_length = sizeof(pqi_event_config_request_t) -
363 		    PQI_REQUEST_HEADER_LENGTH; /* excluding IU header length */
364 
365 	/*Op OQ id where response to be delivered */
366 	request->response_queue_id = softs->op_ob_q[0].q_id;
367 	request->buffer_length 	   = buff->size;
368 	request->sg_desc.addr 	   = buff->dma_addr;
369 	request->sg_desc.length    = buff->size;
370 	request->sg_desc.zero 	   = 0;
371 	request->sg_desc.type 	   = SGL_DESCRIPTOR_CODE_LAST_ALTERNATIVE_SGL_SEGMENT;
372 
373 	/* submit management req IU*/
374 	ret = pqisrc_submit_management_req(softs,request);
375 	if(ret)
376 		goto err_out;
377 
378 
379 	DBG_FUNC(" OUT\n");
380 	return ret;
381 
382 err_out:
383 	DBG_FUNC("Failed OUT\n");
384 	return ret;
385 }
386 
387 /*
388  * Prepare REPORT EVENT CONFIGURATION IU to request that
389  * event configuration information be reported.
390  */
391 int
pqisrc_report_event_config(pqisrc_softstate_t * softs)392 pqisrc_report_event_config(pqisrc_softstate_t *softs)
393 {
394 
395 	int ret,i ;
396 	pqi_event_config_request_t request;
397 	pqi_event_config_t  *event_config_p ;
398 	dma_mem_t  buf_report_event ;
399 	/*bytes to be allocated for report event config data-in buffer */
400 	uint32_t alloc_size = sizeof(pqi_event_config_t) ;
401 	memset(&request, 0 , sizeof(request));
402 
403 	DBG_FUNC(" IN\n");
404 
405 	memset(&buf_report_event, 0, sizeof(struct dma_mem));
406 	os_strlcpy(buf_report_event.tag, "pqi_report_event_buf", sizeof(buf_report_event.tag)); ;
407 	buf_report_event.size 	= alloc_size;
408 	buf_report_event.align 	= PQISRC_DEFAULT_DMA_ALIGN;
409 
410 	/* allocate memory */
411 	ret = os_dma_mem_alloc(softs, &buf_report_event);
412 	if (ret) {
413 		DBG_ERR("Failed to Allocate report event config buffer : %d\n", ret);
414 		goto err_out;
415 	}
416 	DBG_INFO("buf_report_event.dma_addr	= %p \n",(void*)buf_report_event.dma_addr);
417 	DBG_INFO("buf_report_event.virt_addr 	= %p \n",(void*)buf_report_event.virt_addr);
418 
419 	request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
420 
421 	/* Event configuration */
422 	ret=pqi_event_configure(softs,&request,&buf_report_event);
423 	if(ret)
424 		goto free_mem;
425 
426 
427 	event_config_p = (pqi_event_config_t*)buf_report_event.virt_addr;
428 	softs->event_config.num_event_descriptors = MIN(event_config_p->num_event_descriptors,
429 		                                            PQI_MAX_EVENT_DESCRIPTORS) ;
430 
431        for (i=0; i < softs->event_config.num_event_descriptors; i++) {
432 		softs->event_config.descriptors[i].event_type =
433 					event_config_p->descriptors[i].event_type;
434 	}
435         /* free the allocated memory*/
436 	os_dma_mem_free(softs, &buf_report_event);
437 
438 	DBG_FUNC(" OUT\n");
439 	return ret;
440 
441 free_mem:
442 	os_dma_mem_free(softs, &buf_report_event);
443 err_out:
444 	DBG_FUNC("Failed OUT\n");
445 	return PQI_STATUS_FAILURE;
446 }
447 
448 /*
449  * Prepare SET EVENT CONFIGURATION IU to request that
450  * event configuration parameters be set.
451  */
452 int
pqisrc_set_event_config(pqisrc_softstate_t * softs)453 pqisrc_set_event_config(pqisrc_softstate_t *softs)
454 {
455 
456 	int ret,i;
457 	pqi_event_config_request_t request;
458 	pqi_event_config_t *event_config_p;
459 	dma_mem_t buf_set_event;
460 	/*bytes to be allocated for set event config data-out buffer */
461 	uint32_t alloc_size = sizeof(pqi_event_config_t);
462 	memset(&request, 0 , sizeof(request));
463 
464 	DBG_FUNC(" IN\n");
465 
466  	memset(&buf_set_event, 0, sizeof(struct dma_mem));
467 	os_strlcpy(buf_set_event.tag, "pqi_set_event_buf", sizeof(buf_set_event.tag));
468 	buf_set_event.size 	= alloc_size;
469 	buf_set_event.align 	= PQISRC_DEFAULT_DMA_ALIGN;
470 
471 	/* allocate memory */
472 	ret = os_dma_mem_alloc(softs, &buf_set_event);
473 	if (ret) {
474 		DBG_ERR("Failed to Allocate set event config buffer : %d\n", ret);
475 		goto err_out;
476 	}
477 
478 	DBG_INFO("buf_set_event.dma_addr  	= %p\n",(void*)buf_set_event.dma_addr);
479 	DBG_INFO("buf_set_event.virt_addr 	= %p\n",(void*)buf_set_event.virt_addr);
480 
481 	request.header.iu_type = PQI_REQUEST_IU_SET_EVENT_CONFIG;
482 	request.iu_specific.global_event_oq_id = softs->event_q.q_id;
483 
484 	/*pointer to data-out buffer*/
485 
486 	event_config_p = (pqi_event_config_t *)buf_set_event.virt_addr;
487 
488 	event_config_p->num_event_descriptors = softs->event_config.num_event_descriptors;
489 
490 
491 	for (i=0; i < softs->event_config.num_event_descriptors; i++) {
492 		event_config_p->descriptors[i].event_type =
493 					softs->event_config.descriptors[i].event_type;
494 		if( pqisrc_event_type_to_event_index(event_config_p->descriptors[i].event_type) != -1)
495 			event_config_p->descriptors[i].oq_id = softs->event_q.q_id;
496 		else
497 			event_config_p->descriptors[i].oq_id = 0; /* Not supported this event. */
498 
499 
500 	}
501         /* Event configuration */
502 	ret = pqi_event_configure(softs,&request,&buf_set_event);
503 		if(ret)
504 			goto free_mem;
505 
506 	os_dma_mem_free(softs, &buf_set_event);
507 
508 	DBG_FUNC(" OUT\n");
509 	return ret;
510 
511 free_mem:
512 	os_dma_mem_free(softs, &buf_set_event);
513 err_out:
514 	DBG_FUNC("Failed OUT\n");
515 	return PQI_STATUS_FAILURE;
516 
517 }
518