1 /*
2  * This module provides common API to set Diagnostic trigger for MPT
3  * (Message Passing Technology) based controllers
4  *
5  * This code is based on drivers/scsi/mpt3sas/mpt3sas_trigger_diag.c
6  * Copyright (C) 2012-2014  LSI Corporation
7  * Copyright (C) 2013-2014 Avago Technologies
8  *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * NO WARRANTY
21  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25  * solely responsible for determining the appropriateness of using and
26  * distributing the Program and assumes all risks associated with its
27  * exercise of rights under this Agreement, including but not limited to
28  * the risks and costs of program errors, damage to or loss of data,
29  * programs or equipment, and unavailability or interruption of operations.
30 
31  * DISCLAIMER OF LIABILITY
32  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39 
40  * You should have received a copy of the GNU General Public License
41  * along with this program; if not, write to the Free Software
42  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
43  * USA.
44  */
45 
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/delay.h>
54 #include <linux/compat.h>
55 #include <linux/poll.h>
56 
57 #include <linux/io.h>
58 #include <linux/uaccess.h>
59 
60 #include "mpt3sas_base.h"
61 
62 /**
63  * _mpt3sas_raise_sigio - notifiy app
64  * @ioc: per adapter object
65  * @event_data:
66  */
67 static void
68 _mpt3sas_raise_sigio(struct MPT3SAS_ADAPTER *ioc,
69 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
70 {
71 	Mpi2EventNotificationReply_t *mpi_reply;
72 	u16 sz, event_data_sz;
73 	unsigned long flags;
74 
75 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n",
76 	    ioc->name, __func__));
77 
78 	sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
79 	    sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T) + 4;
80 	mpi_reply = kzalloc(sz, GFP_KERNEL);
81 	if (!mpi_reply)
82 		goto out;
83 	mpi_reply->Event = cpu_to_le16(MPI3_EVENT_DIAGNOSTIC_TRIGGER_FIRED);
84 	event_data_sz = (sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T) + 4) / 4;
85 	mpi_reply->EventDataLength = cpu_to_le16(event_data_sz);
86 	memcpy(&mpi_reply->EventData, event_data,
87 	    sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
88 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
89 		"%s: add to driver event log\n",
90 		ioc->name, __func__));
91 	mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
92 	kfree(mpi_reply);
93  out:
94 
95 	/* clearing the diag_trigger_active flag */
96 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
97 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
98 		"%s: clearing diag_trigger_active flag\n",
99 		ioc->name, __func__));
100 	ioc->diag_trigger_active = 0;
101 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
102 
103 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
104 	    __func__));
105 }
106 
107 /**
108  * mpt3sas_process_trigger_data - process the event data for the trigger
109  * @ioc: per adapter object
110  * @event_data:
111  */
112 void
113 mpt3sas_process_trigger_data(struct MPT3SAS_ADAPTER *ioc,
114 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
115 {
116 	u8 issue_reset = 0;
117 
118 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n",
119 	    ioc->name, __func__));
120 
121 	/* release the diag buffer trace */
122 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
123 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
124 		dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
125 		"%s: release trace diag buffer\n", ioc->name, __func__));
126 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
127 		    &issue_reset);
128 	}
129 
130 	_mpt3sas_raise_sigio(ioc, event_data);
131 
132 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
133 	    __func__));
134 }
135 
136 /**
137  * mpt3sas_trigger_master - Master trigger handler
138  * @ioc: per adapter object
139  * @trigger_bitmask:
140  *
141  */
142 void
143 mpt3sas_trigger_master(struct MPT3SAS_ADAPTER *ioc, u32 trigger_bitmask)
144 {
145 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
146 	unsigned long flags;
147 	u8 found_match = 0;
148 
149 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
150 
151 	if (trigger_bitmask & MASTER_TRIGGER_FW_FAULT ||
152 	    trigger_bitmask & MASTER_TRIGGER_ADAPTER_RESET)
153 		goto by_pass_checks;
154 
155 	/* check to see if trace buffers are currently registered */
156 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
157 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
158 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
159 		return;
160 	}
161 
162 	/* check to see if trace buffers are currently released */
163 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
164 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
165 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
166 		return;
167 	}
168 
169  by_pass_checks:
170 
171 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
172 		"%s: enter - trigger_bitmask = 0x%08x\n",
173 		ioc->name, __func__, trigger_bitmask));
174 
175 	/* don't send trigger if an trigger is currently active */
176 	if (ioc->diag_trigger_active) {
177 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
178 		goto out;
179 	}
180 
181 	/* check for the trigger condition */
182 	if (ioc->diag_trigger_master.MasterData & trigger_bitmask) {
183 		found_match = 1;
184 		ioc->diag_trigger_active = 1;
185 		dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
186 		"%s: setting diag_trigger_active flag\n",
187 		ioc->name, __func__));
188 	}
189 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
190 
191 	if (!found_match)
192 		goto out;
193 
194 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
195 	event_data.trigger_type = MPT3SAS_TRIGGER_MASTER;
196 	event_data.u.master.MasterData = trigger_bitmask;
197 
198 	if (trigger_bitmask & MASTER_TRIGGER_FW_FAULT ||
199 	    trigger_bitmask & MASTER_TRIGGER_ADAPTER_RESET)
200 		_mpt3sas_raise_sigio(ioc, &event_data);
201 	else
202 		mpt3sas_send_trigger_data_event(ioc, &event_data);
203 
204  out:
205 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
206 	    __func__));
207 }
208 
209 /**
210  * mpt3sas_trigger_event - Event trigger handler
211  * @ioc: per adapter object
212  * @event:
213  * @log_entry_qualifier:
214  *
215  */
216 void
217 mpt3sas_trigger_event(struct MPT3SAS_ADAPTER *ioc, u16 event,
218 	u16 log_entry_qualifier)
219 {
220 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
221 	struct SL_WH_EVENT_TRIGGER_T *event_trigger;
222 	int i;
223 	unsigned long flags;
224 	u8 found_match;
225 
226 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
227 
228 	/* check to see if trace buffers are currently registered */
229 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
230 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
231 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
232 		return;
233 	}
234 
235 	/* check to see if trace buffers are currently released */
236 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
237 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
238 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
239 		return;
240 	}
241 
242 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
243 		"%s: enter - event = 0x%04x, log_entry_qualifier = 0x%04x\n",
244 		ioc->name, __func__, event, log_entry_qualifier));
245 
246 	/* don't send trigger if an trigger is currently active */
247 	if (ioc->diag_trigger_active) {
248 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
249 		goto out;
250 	}
251 
252 	/* check for the trigger condition */
253 	event_trigger = ioc->diag_trigger_event.EventTriggerEntry;
254 	for (i = 0 , found_match = 0; i < ioc->diag_trigger_event.ValidEntries
255 	    && !found_match; i++, event_trigger++) {
256 		if (event_trigger->EventValue != event)
257 			continue;
258 		if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
259 			if (event_trigger->LogEntryQualifier ==
260 			    log_entry_qualifier)
261 				found_match = 1;
262 			continue;
263 		}
264 		found_match = 1;
265 		ioc->diag_trigger_active = 1;
266 		dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
267 			"%s: setting diag_trigger_active flag\n",
268 			ioc->name, __func__));
269 	}
270 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
271 
272 	if (!found_match)
273 		goto out;
274 
275 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
276 		"%s: setting diag_trigger_active flag\n",
277 		ioc->name, __func__));
278 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
279 	event_data.trigger_type = MPT3SAS_TRIGGER_EVENT;
280 	event_data.u.event.EventValue = event;
281 	event_data.u.event.LogEntryQualifier = log_entry_qualifier;
282 	mpt3sas_send_trigger_data_event(ioc, &event_data);
283  out:
284 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
285 	    __func__));
286 }
287 
288 /**
289  * mpt3sas_trigger_scsi - SCSI trigger handler
290  * @ioc: per adapter object
291  * @sense_key:
292  * @asc:
293  * @ascq:
294  *
295  */
296 void
297 mpt3sas_trigger_scsi(struct MPT3SAS_ADAPTER *ioc, u8 sense_key, u8 asc,
298 	u8 ascq)
299 {
300 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
301 	struct SL_WH_SCSI_TRIGGER_T *scsi_trigger;
302 	int i;
303 	unsigned long flags;
304 	u8 found_match;
305 
306 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
307 
308 	/* check to see if trace buffers are currently registered */
309 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
310 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
311 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
312 		return;
313 	}
314 
315 	/* check to see if trace buffers are currently released */
316 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
317 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
318 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
319 		return;
320 	}
321 
322 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
323 		"%s: enter - sense_key = 0x%02x, asc = 0x%02x, ascq = 0x%02x\n",
324 		ioc->name, __func__, sense_key, asc, ascq));
325 
326 	/* don't send trigger if an trigger is currently active */
327 	if (ioc->diag_trigger_active) {
328 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
329 		goto out;
330 	}
331 
332 	/* check for the trigger condition */
333 	scsi_trigger = ioc->diag_trigger_scsi.SCSITriggerEntry;
334 	for (i = 0 , found_match = 0; i < ioc->diag_trigger_scsi.ValidEntries
335 	    && !found_match; i++, scsi_trigger++) {
336 		if (scsi_trigger->SenseKey != sense_key)
337 			continue;
338 		if (!(scsi_trigger->ASC == 0xFF || scsi_trigger->ASC == asc))
339 			continue;
340 		if (!(scsi_trigger->ASCQ == 0xFF || scsi_trigger->ASCQ == ascq))
341 			continue;
342 		found_match = 1;
343 		ioc->diag_trigger_active = 1;
344 	}
345 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
346 
347 	if (!found_match)
348 		goto out;
349 
350 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
351 		"%s: setting diag_trigger_active flag\n",
352 		ioc->name, __func__));
353 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
354 	event_data.trigger_type = MPT3SAS_TRIGGER_SCSI;
355 	event_data.u.scsi.SenseKey = sense_key;
356 	event_data.u.scsi.ASC = asc;
357 	event_data.u.scsi.ASCQ = ascq;
358 	mpt3sas_send_trigger_data_event(ioc, &event_data);
359  out:
360 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
361 	    __func__));
362 }
363 
364 /**
365  * mpt3sas_trigger_mpi - MPI trigger handler
366  * @ioc: per adapter object
367  * @ioc_status:
368  * @loginfo:
369  *
370  */
371 void
372 mpt3sas_trigger_mpi(struct MPT3SAS_ADAPTER *ioc, u16 ioc_status, u32 loginfo)
373 {
374 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
375 	struct SL_WH_MPI_TRIGGER_T *mpi_trigger;
376 	int i;
377 	unsigned long flags;
378 	u8 found_match;
379 
380 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
381 
382 	/* check to see if trace buffers are currently registered */
383 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
384 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
385 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
386 		return;
387 	}
388 
389 	/* check to see if trace buffers are currently released */
390 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
391 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
392 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
393 		return;
394 	}
395 
396 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
397 		"%s: enter - ioc_status = 0x%04x, loginfo = 0x%08x\n",
398 		ioc->name, __func__, ioc_status, loginfo));
399 
400 	/* don't send trigger if an trigger is currently active */
401 	if (ioc->diag_trigger_active) {
402 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
403 		goto out;
404 	}
405 
406 	/* check for the trigger condition */
407 	mpi_trigger = ioc->diag_trigger_mpi.MPITriggerEntry;
408 	for (i = 0 , found_match = 0; i < ioc->diag_trigger_mpi.ValidEntries
409 	    && !found_match; i++, mpi_trigger++) {
410 		if (mpi_trigger->IOCStatus != ioc_status)
411 			continue;
412 		if (!(mpi_trigger->IocLogInfo == 0xFFFFFFFF ||
413 		    mpi_trigger->IocLogInfo == loginfo))
414 			continue;
415 		found_match = 1;
416 		ioc->diag_trigger_active = 1;
417 	}
418 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
419 
420 	if (!found_match)
421 		goto out;
422 
423 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT
424 		"%s: setting diag_trigger_active flag\n",
425 		ioc->name, __func__));
426 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
427 	event_data.trigger_type = MPT3SAS_TRIGGER_MPI;
428 	event_data.u.mpi.IOCStatus = ioc_status;
429 	event_data.u.mpi.IocLogInfo = loginfo;
430 	mpt3sas_send_trigger_data_event(ioc, &event_data);
431  out:
432 	dTriggerDiagPrintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
433 	    __func__));
434 }
435