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, ioc_info(ioc, "%s: enter\n", __func__));
76 
77 	sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
78 	    sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T) + 4;
79 	mpi_reply = kzalloc(sz, GFP_KERNEL);
80 	if (!mpi_reply)
81 		goto out;
82 	mpi_reply->Event = cpu_to_le16(MPI3_EVENT_DIAGNOSTIC_TRIGGER_FIRED);
83 	event_data_sz = (sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T) + 4) / 4;
84 	mpi_reply->EventDataLength = cpu_to_le16(event_data_sz);
85 	memcpy(&mpi_reply->EventData, event_data,
86 	    sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
87 	dTriggerDiagPrintk(ioc,
88 			   ioc_info(ioc, "%s: add to driver event log\n",
89 				    __func__));
90 	mpt3sas_ctl_add_to_event_log(ioc, mpi_reply);
91 	kfree(mpi_reply);
92  out:
93 
94 	/* clearing the diag_trigger_active flag */
95 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
96 	dTriggerDiagPrintk(ioc,
97 			   ioc_info(ioc, "%s: clearing diag_trigger_active flag\n",
98 				    __func__));
99 	ioc->diag_trigger_active = 0;
100 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
101 
102 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: exit\n",
103 					 __func__));
104 }
105 
106 /**
107  * mpt3sas_process_trigger_data - process the event data for the trigger
108  * @ioc: per adapter object
109  * @event_data: ?
110  */
111 void
112 mpt3sas_process_trigger_data(struct MPT3SAS_ADAPTER *ioc,
113 	struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
114 {
115 	u8 issue_reset = 0;
116 	u32 *trig_data = (u32 *)&event_data->u.master;
117 
118 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: enter\n", __func__));
119 
120 	/* release the diag buffer trace */
121 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
122 	    MPT3_DIAG_BUFFER_IS_RELEASED) == 0) {
123 		/*
124 		 * add a log message so that user knows which event caused
125 		 * the release
126 		 */
127 		ioc_info(ioc,
128 		    "%s: Releasing the trace buffer. Trigger_Type 0x%08x, Data[0] 0x%08x, Data[1] 0x%08x\n",
129 		    __func__, event_data->trigger_type,
130 		    trig_data[0], trig_data[1]);
131 		mpt3sas_send_diag_release(ioc, MPI2_DIAG_BUF_TYPE_TRACE,
132 		    &issue_reset);
133 	}
134 
135 	_mpt3sas_raise_sigio(ioc, event_data);
136 
137 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: exit\n",
138 					 __func__));
139 }
140 
141 /**
142  * mpt3sas_trigger_master - Master trigger handler
143  * @ioc: per adapter object
144  * @trigger_bitmask:
145  *
146  */
147 void
148 mpt3sas_trigger_master(struct MPT3SAS_ADAPTER *ioc, u32 trigger_bitmask)
149 {
150 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
151 	unsigned long flags;
152 	u8 found_match = 0;
153 
154 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
155 
156 	if (trigger_bitmask & MASTER_TRIGGER_FW_FAULT ||
157 	    trigger_bitmask & MASTER_TRIGGER_ADAPTER_RESET)
158 		goto by_pass_checks;
159 
160 	/* check to see if trace buffers are currently registered */
161 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
162 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
163 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
164 		return;
165 	}
166 
167 	/* check to see if trace buffers are currently released */
168 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
169 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
170 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
171 		return;
172 	}
173 
174  by_pass_checks:
175 
176 	dTriggerDiagPrintk(ioc,
177 			   ioc_info(ioc, "%s: enter - trigger_bitmask = 0x%08x\n",
178 				    __func__, trigger_bitmask));
179 
180 	/* don't send trigger if an trigger is currently active */
181 	if (ioc->diag_trigger_active) {
182 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
183 		goto out;
184 	}
185 
186 	/* check for the trigger condition */
187 	if (ioc->diag_trigger_master.MasterData & trigger_bitmask) {
188 		found_match = 1;
189 		ioc->diag_trigger_active = 1;
190 		dTriggerDiagPrintk(ioc,
191 				   ioc_info(ioc, "%s: setting diag_trigger_active flag\n",
192 					    __func__));
193 	}
194 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
195 
196 	if (!found_match)
197 		goto out;
198 
199 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
200 	event_data.trigger_type = MPT3SAS_TRIGGER_MASTER;
201 	event_data.u.master.MasterData = trigger_bitmask;
202 
203 	if (trigger_bitmask & MASTER_TRIGGER_FW_FAULT ||
204 	    trigger_bitmask & MASTER_TRIGGER_ADAPTER_RESET)
205 		_mpt3sas_raise_sigio(ioc, &event_data);
206 	else
207 		mpt3sas_send_trigger_data_event(ioc, &event_data);
208 
209  out:
210 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: exit\n",
211 					 __func__));
212 }
213 
214 /**
215  * mpt3sas_trigger_event - Event trigger handler
216  * @ioc: per adapter object
217  * @event: ?
218  * @log_entry_qualifier: ?
219  *
220  */
221 void
222 mpt3sas_trigger_event(struct MPT3SAS_ADAPTER *ioc, u16 event,
223 	u16 log_entry_qualifier)
224 {
225 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
226 	struct SL_WH_EVENT_TRIGGER_T *event_trigger;
227 	int i;
228 	unsigned long flags;
229 	u8 found_match;
230 
231 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
232 
233 	/* check to see if trace buffers are currently registered */
234 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
235 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
236 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
237 		return;
238 	}
239 
240 	/* check to see if trace buffers are currently released */
241 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
242 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
243 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
244 		return;
245 	}
246 
247 	dTriggerDiagPrintk(ioc,
248 			   ioc_info(ioc, "%s: enter - event = 0x%04x, log_entry_qualifier = 0x%04x\n",
249 				    __func__, event, log_entry_qualifier));
250 
251 	/* don't send trigger if an trigger is currently active */
252 	if (ioc->diag_trigger_active) {
253 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
254 		goto out;
255 	}
256 
257 	/* check for the trigger condition */
258 	event_trigger = ioc->diag_trigger_event.EventTriggerEntry;
259 	for (i = 0 , found_match = 0; i < ioc->diag_trigger_event.ValidEntries
260 	    && !found_match; i++, event_trigger++) {
261 		if (event_trigger->EventValue != event)
262 			continue;
263 		if (event == MPI2_EVENT_LOG_ENTRY_ADDED) {
264 			if (event_trigger->LogEntryQualifier ==
265 			    log_entry_qualifier)
266 				found_match = 1;
267 			continue;
268 		}
269 		found_match = 1;
270 		ioc->diag_trigger_active = 1;
271 		dTriggerDiagPrintk(ioc,
272 				   ioc_info(ioc, "%s: setting diag_trigger_active flag\n",
273 					    __func__));
274 	}
275 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
276 
277 	if (!found_match)
278 		goto out;
279 
280 	dTriggerDiagPrintk(ioc,
281 			   ioc_info(ioc, "%s: setting diag_trigger_active flag\n",
282 				    __func__));
283 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
284 	event_data.trigger_type = MPT3SAS_TRIGGER_EVENT;
285 	event_data.u.event.EventValue = event;
286 	event_data.u.event.LogEntryQualifier = log_entry_qualifier;
287 	mpt3sas_send_trigger_data_event(ioc, &event_data);
288  out:
289 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: exit\n",
290 					 __func__));
291 }
292 
293 /**
294  * mpt3sas_trigger_scsi - SCSI trigger handler
295  * @ioc: per adapter object
296  * @sense_key: ?
297  * @asc: ?
298  * @ascq: ?
299  *
300  */
301 void
302 mpt3sas_trigger_scsi(struct MPT3SAS_ADAPTER *ioc, u8 sense_key, u8 asc,
303 	u8 ascq)
304 {
305 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
306 	struct SL_WH_SCSI_TRIGGER_T *scsi_trigger;
307 	int i;
308 	unsigned long flags;
309 	u8 found_match;
310 
311 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
312 
313 	/* check to see if trace buffers are currently registered */
314 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
315 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
316 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
317 		return;
318 	}
319 
320 	/* check to see if trace buffers are currently released */
321 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
322 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
323 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
324 		return;
325 	}
326 
327 	dTriggerDiagPrintk(ioc,
328 			   ioc_info(ioc, "%s: enter - sense_key = 0x%02x, asc = 0x%02x, ascq = 0x%02x\n",
329 				    __func__, sense_key, asc, ascq));
330 
331 	/* don't send trigger if an trigger is currently active */
332 	if (ioc->diag_trigger_active) {
333 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
334 		goto out;
335 	}
336 
337 	/* check for the trigger condition */
338 	scsi_trigger = ioc->diag_trigger_scsi.SCSITriggerEntry;
339 	for (i = 0 , found_match = 0; i < ioc->diag_trigger_scsi.ValidEntries
340 	    && !found_match; i++, scsi_trigger++) {
341 		if (scsi_trigger->SenseKey != sense_key)
342 			continue;
343 		if (!(scsi_trigger->ASC == 0xFF || scsi_trigger->ASC == asc))
344 			continue;
345 		if (!(scsi_trigger->ASCQ == 0xFF || scsi_trigger->ASCQ == ascq))
346 			continue;
347 		found_match = 1;
348 		ioc->diag_trigger_active = 1;
349 	}
350 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
351 
352 	if (!found_match)
353 		goto out;
354 
355 	dTriggerDiagPrintk(ioc,
356 			   ioc_info(ioc, "%s: setting diag_trigger_active flag\n",
357 				    __func__));
358 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
359 	event_data.trigger_type = MPT3SAS_TRIGGER_SCSI;
360 	event_data.u.scsi.SenseKey = sense_key;
361 	event_data.u.scsi.ASC = asc;
362 	event_data.u.scsi.ASCQ = ascq;
363 	mpt3sas_send_trigger_data_event(ioc, &event_data);
364  out:
365 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: exit\n",
366 					 __func__));
367 }
368 
369 /**
370  * mpt3sas_trigger_mpi - MPI trigger handler
371  * @ioc: per adapter object
372  * @ioc_status: ?
373  * @loginfo: ?
374  *
375  */
376 void
377 mpt3sas_trigger_mpi(struct MPT3SAS_ADAPTER *ioc, u16 ioc_status, u32 loginfo)
378 {
379 	struct SL_WH_TRIGGERS_EVENT_DATA_T event_data;
380 	struct SL_WH_MPI_TRIGGER_T *mpi_trigger;
381 	int i;
382 	unsigned long flags;
383 	u8 found_match;
384 
385 	spin_lock_irqsave(&ioc->diag_trigger_lock, flags);
386 
387 	/* check to see if trace buffers are currently registered */
388 	if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
389 	    MPT3_DIAG_BUFFER_IS_REGISTERED) == 0) {
390 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
391 		return;
392 	}
393 
394 	/* check to see if trace buffers are currently released */
395 	if (ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
396 	    MPT3_DIAG_BUFFER_IS_RELEASED) {
397 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
398 		return;
399 	}
400 
401 	dTriggerDiagPrintk(ioc,
402 			   ioc_info(ioc, "%s: enter - ioc_status = 0x%04x, loginfo = 0x%08x\n",
403 				    __func__, ioc_status, loginfo));
404 
405 	/* don't send trigger if an trigger is currently active */
406 	if (ioc->diag_trigger_active) {
407 		spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
408 		goto out;
409 	}
410 
411 	/* check for the trigger condition */
412 	mpi_trigger = ioc->diag_trigger_mpi.MPITriggerEntry;
413 	for (i = 0 , found_match = 0; i < ioc->diag_trigger_mpi.ValidEntries
414 	    && !found_match; i++, mpi_trigger++) {
415 		if (mpi_trigger->IOCStatus != ioc_status)
416 			continue;
417 		if (!(mpi_trigger->IocLogInfo == 0xFFFFFFFF ||
418 		    mpi_trigger->IocLogInfo == loginfo))
419 			continue;
420 		found_match = 1;
421 		ioc->diag_trigger_active = 1;
422 	}
423 	spin_unlock_irqrestore(&ioc->diag_trigger_lock, flags);
424 
425 	if (!found_match)
426 		goto out;
427 
428 	dTriggerDiagPrintk(ioc,
429 			   ioc_info(ioc, "%s: setting diag_trigger_active flag\n",
430 				    __func__));
431 	memset(&event_data, 0, sizeof(struct SL_WH_TRIGGERS_EVENT_DATA_T));
432 	event_data.trigger_type = MPT3SAS_TRIGGER_MPI;
433 	event_data.u.mpi.IOCStatus = ioc_status;
434 	event_data.u.mpi.IocLogInfo = loginfo;
435 	mpt3sas_send_trigger_data_event(ioc, &event_data);
436  out:
437 	dTriggerDiagPrintk(ioc, ioc_info(ioc, "%s: exit\n",
438 					 __func__));
439 }
440