1/*-
2 * Copyright 2016-2023 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 */
32int
33pqisrc_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
50void
51pqisrc_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 */
60static void
61pqisrc_acknowledge_event(pqisrc_softstate_t *softs,
62	struct pqi_event *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
100out:
101	DBG_FUNC("OUT\n");
102}
103
104/*
105 * Acknowledge processed events to the adapter.
106 */
107void
108pqisrc_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 */
137static int
138pqisrc_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 */
172int
173pqisrc_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			if(response.request_acknowledge) {
208				pending_event = &softs->pending_events[event_index];
209				pending_event->pending = true;
210				pending_event->event_type = response.event_type;
211				pending_event->event_id = response.event_id;
212				pending_event->additional_event_id = response.additional_event_id;
213			}
214		}
215
216	obq_ci = (obq_ci + 1) % event_q->num_elem;
217	}
218    /* Update CI */
219	event_q->ci_local = obq_ci;
220	PCI_MEM_PUT32(softs, event_q->ci_register_abs,
221        event_q->ci_register_offset, event_q->ci_local);
222
223	/*Adding events to the task queue for acknowledging*/
224	if (need_delayed_work == true) {
225		os_eventtaskqueue_enqueue(softs);
226	}
227
228	DBG_FUNC("OUT");
229	return PQI_STATUS_SUCCESS;
230
231
232}
233
234/*
235 * Function used to build and send the vendor general request
236 * Used for configuring PQI feature bits between firmware and driver
237 */
238int
239pqisrc_build_send_vendor_request(pqisrc_softstate_t *softs,
240	struct pqi_vendor_general_request *request)
241{
242	int ret = PQI_STATUS_SUCCESS;
243	ib_queue_t *op_ib_q = &softs->op_raid_ib_q[PQI_DEFAULT_IB_QUEUE];
244	ob_queue_t *ob_q = &softs->op_ob_q[PQI_DEFAULT_IB_QUEUE];
245
246	rcb_t *rcb = NULL;
247
248	/* Get the tag */
249	request->request_id = pqisrc_get_tag(&softs->taglist);
250	if (INVALID_ELEM == request->request_id) {
251		DBG_ERR("Tag not available\n");
252		ret = PQI_STATUS_FAILURE;
253		goto err_notag;
254	}
255
256	request->response_id = ob_q->q_id;
257
258	rcb = &softs->rcb[request->request_id];
259
260	rcb->req_pending = true;
261	rcb->tag = request->request_id;
262
263	ret = pqisrc_submit_cmnd(softs, op_ib_q, request);
264
265	if (ret != PQI_STATUS_SUCCESS) {
266		DBG_ERR("Unable to submit command\n");
267		goto err_out;
268	}
269
270	ret = pqisrc_wait_on_condition(softs, rcb, PQISRC_CMD_TIMEOUT);
271	if (ret != PQI_STATUS_SUCCESS) {
272		DBG_ERR("Management request timed out!\n");
273		goto err_out;
274	}
275
276	ret = rcb->status;
277
278err_out:
279	os_reset_rcb(rcb);
280	pqisrc_put_tag(&softs->taglist, request->request_id);
281err_notag:
282	DBG_FUNC("OUT \n");
283	return ret;
284}
285
286/*
287 * Function used to send a general management request to adapter.
288 */
289int
290pqisrc_submit_management_req(pqisrc_softstate_t *softs,
291                                      pqi_event_config_request_t *request)
292{
293	int ret = PQI_STATUS_SUCCESS;
294	ib_queue_t *op_ib_q = &softs->op_raid_ib_q[0];
295	rcb_t *rcb = NULL;
296
297	DBG_FUNC(" IN\n");
298
299	/* Get the tag */
300	request->request_id = pqisrc_get_tag(&softs->taglist);
301	if (INVALID_ELEM == request->request_id) {
302		DBG_ERR("Tag not available\n");
303		ret = PQI_STATUS_FAILURE;
304		goto err_out;
305	}
306
307	rcb = &softs->rcb[request->request_id];
308	rcb->req_pending = true;
309	rcb->tag = request->request_id;
310
311	/* Submit command on operational raid ib queue */
312	ret = pqisrc_submit_cmnd(softs, op_ib_q, request);
313	if (ret != PQI_STATUS_SUCCESS) {
314		DBG_ERR("  Unable to submit command\n");
315		goto err_cmd;
316	}
317
318	ret = pqisrc_wait_on_condition(softs, rcb, PQISRC_CMD_TIMEOUT);
319
320	if (ret != PQI_STATUS_SUCCESS) {
321		DBG_ERR("Management request timed out !!\n");
322		goto err_cmd;
323	}
324
325	os_reset_rcb(rcb);
326 	pqisrc_put_tag(&softs->taglist,request->request_id);
327	DBG_FUNC("OUT\n");
328	return ret;
329
330err_cmd:
331	os_reset_rcb(rcb);
332	pqisrc_put_tag(&softs->taglist,request->request_id);
333err_out:
334	DBG_FUNC(" failed OUT : %d\n", ret);
335	return ret;
336}
337
338/*
339 * Build and send the general management request.
340 */
341static int
342pqi_event_configure(pqisrc_softstate_t *softs ,
343                              pqi_event_config_request_t *request,
344                              dma_mem_t *buff)
345{
346        int ret = PQI_STATUS_SUCCESS;
347
348	DBG_FUNC(" IN\n");
349
350	request->header.comp_feature = 0x00;
351	request->header.iu_length = sizeof(pqi_event_config_request_t) -
352		    PQI_REQUEST_HEADER_LENGTH; /* excluding IU header length */
353
354	/*Op OQ id where response to be delivered */
355	request->response_queue_id = softs->op_ob_q[0].q_id;
356	request->buffer_length 	   = buff->size;
357	request->sg_desc.addr 	   = buff->dma_addr;
358	request->sg_desc.length    = buff->size;
359	request->sg_desc.zero 	   = 0;
360	request->sg_desc.type 	   = SGL_DESCRIPTOR_CODE_LAST_ALTERNATIVE_SGL_SEGMENT;
361
362	/* submit management req IU*/
363	ret = pqisrc_submit_management_req(softs,request);
364	if(ret)
365		goto err_out;
366
367
368	DBG_FUNC(" OUT\n");
369	return ret;
370
371err_out:
372	DBG_FUNC("Failed OUT\n");
373	return ret;
374}
375
376/*
377 * Prepare REPORT EVENT CONFIGURATION IU to request that
378 * event configuration information be reported.
379 */
380int
381pqisrc_report_event_config(pqisrc_softstate_t *softs)
382{
383
384	int ret,i ;
385	pqi_event_config_request_t request;
386	pqi_event_config_t  *event_config_p ;
387	dma_mem_t  buf_report_event ;
388	/*bytes to be allocaed for report event config data-in buffer */
389	uint32_t alloc_size = sizeof(pqi_event_config_t) ;
390	memset(&request, 0 , sizeof(request));
391
392	DBG_FUNC(" IN\n");
393
394	memset(&buf_report_event, 0, sizeof(struct dma_mem));
395	os_strlcpy(buf_report_event.tag, "pqi_report_event_buf", sizeof(buf_report_event.tag)); ;
396	buf_report_event.size 	= alloc_size;
397	buf_report_event.align 	= PQISRC_DEFAULT_DMA_ALIGN;
398
399	/* allocate memory */
400	ret = os_dma_mem_alloc(softs, &buf_report_event);
401	if (ret) {
402		DBG_ERR("Failed to Allocate report event config buffer : %d\n", ret);
403		goto err_out;
404	}
405	DBG_INFO("buf_report_event.dma_addr	= %p \n",(void*)buf_report_event.dma_addr);
406	DBG_INFO("buf_report_event.virt_addr 	= %p \n",(void*)buf_report_event.virt_addr);
407
408	request.header.iu_type = PQI_REQUEST_IU_REPORT_VENDOR_EVENT_CONFIG;
409
410	/* Event configuration */
411	ret=pqi_event_configure(softs,&request,&buf_report_event);
412	if(ret)
413		goto free_mem;
414
415
416	event_config_p = (pqi_event_config_t*)buf_report_event.virt_addr;
417	softs->event_config.num_event_descriptors = MIN(event_config_p->num_event_descriptors,
418		                                            PQI_MAX_EVENT_DESCRIPTORS) ;
419
420        for (i=0; i < softs->event_config.num_event_descriptors ;i++){
421		softs->event_config.descriptors[i].event_type =
422					event_config_p->descriptors[i].event_type;
423	}
424        /* free the allocated memory*/
425	os_dma_mem_free(softs, &buf_report_event);
426
427	DBG_FUNC(" OUT\n");
428	return ret;
429
430free_mem:
431	os_dma_mem_free(softs, &buf_report_event);
432err_out:
433	DBG_FUNC("Failed OUT\n");
434	return PQI_STATUS_FAILURE;
435}
436
437/*
438 * Prepare SET EVENT CONFIGURATION IU to request that
439 * event configuration parameters be set.
440 */
441int
442pqisrc_set_event_config(pqisrc_softstate_t *softs)
443{
444
445	int ret,i;
446	pqi_event_config_request_t request;
447	pqi_event_config_t *event_config_p;
448	dma_mem_t buf_set_event;
449	/*bytes to be allocaed for set event config data-out buffer */
450	uint32_t alloc_size = sizeof(pqi_event_config_t);
451	memset(&request, 0 , sizeof(request));
452
453	DBG_FUNC(" IN\n");
454
455 	memset(&buf_set_event, 0, sizeof(struct dma_mem));
456	os_strlcpy(buf_set_event.tag, "pqi_set_event_buf", sizeof(buf_set_event.tag));
457	buf_set_event.size 	= alloc_size;
458	buf_set_event.align 	= PQISRC_DEFAULT_DMA_ALIGN;
459
460	/* allocate memory */
461	ret = os_dma_mem_alloc(softs, &buf_set_event);
462	if (ret) {
463		DBG_ERR("Failed to Allocate set event config buffer : %d\n", ret);
464		goto err_out;
465	}
466
467	DBG_INFO("buf_set_event.dma_addr  	= %p\n",(void*)buf_set_event.dma_addr);
468	DBG_INFO("buf_set_event.virt_addr 	= %p\n",(void*)buf_set_event.virt_addr);
469
470	request.header.iu_type = PQI_REQUEST_IU_SET_EVENT_CONFIG;
471	request.iu_specific.global_event_oq_id = softs->event_q.q_id;
472
473	/*pointer to data-out buffer*/
474
475	event_config_p = (pqi_event_config_t *)buf_set_event.virt_addr;
476
477	event_config_p->num_event_descriptors = softs->event_config.num_event_descriptors;
478
479
480	for (i=0; i < softs->event_config.num_event_descriptors ; i++){
481		event_config_p->descriptors[i].event_type =
482					softs->event_config.descriptors[i].event_type;
483		if( pqisrc_event_type_to_event_index(event_config_p->descriptors[i].event_type) != -1)
484			event_config_p->descriptors[i].oq_id = softs->event_q.q_id;
485		else
486			event_config_p->descriptors[i].oq_id = 0; /* Not supported this event. */
487
488
489	}
490        /* Event configuration */
491	ret = pqi_event_configure(softs,&request,&buf_set_event);
492		if(ret)
493			goto free_mem;
494
495	os_dma_mem_free(softs, &buf_set_event);
496
497	DBG_FUNC(" OUT\n");
498	return ret;
499
500free_mem:
501	os_dma_mem_free(softs, &buf_set_event);
502err_out:
503	DBG_FUNC("Failed OUT\n");
504	return PQI_STATUS_FAILURE;
505
506}
507