1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2023 Tintri by DDN, Inc. All rights reserved.
14  * Copyright 2019 RackTop Systems, Inc.
15  */
16 
17 /*
18  * Utility routines that have common usage throughout the driver.
19  */
20 #include <smartpqi.h>
21 
22 /* ---- Forward declarations for support/utility functions ---- */
23 static void reinit_io(pqi_io_request_t *io);
24 static void dump_raid(pqi_state_t *s, void *v, pqi_index_t idx);
25 static void dump_aio(void *v);
26 static void show_error_detail(pqi_state_t *s);
27 static void cmd_start_time(pqi_cmd_t *c);
28 static void cmd_finish_task(void *v);
29 
30 
31 /*
32  * Entry points for this file
33  */
34 
35 #ifdef DEBUG
36 int	pqi_shuffle_delay = 0;
37 #endif	/* DEBUG */
38 
39 static void
cmd_remove_group(pqi_cmd_t * c)40 cmd_remove_group(pqi_cmd_t *c)
41 {
42 	pqi_io_request_t	*io = c->pc_io_rqst;
43 	pqi_device_t		*d = c->pc_device;
44 
45 	/*
46 	 * This would be a good place to send a SCSI TASK MANAGEMENT
47 	 * command to cancel an individual command, but we don't
48 	 * have any documentation on the HBA to describe how that
49 	 * might be done.
50 	 */
51 	if (io != NULL) {
52 		pqi_queue_group_t	*qg = io->io_queue_group;
53 		int			path = io->io_queue_path;
54 
55 		/*
56 		 * The lock ordering is such that the driver must drop
57 		 * the device lock in order to grab the queue lock.
58 		 * We must also drop the cmd mutex to prevent possible deadlock.
59 		 */
60 		mutex_exit(&c->pc_mutex);
61 		mutex_exit(&d->pd_mutex);
62 		mutex_enter(&qg->submit_lock[path]);
63 		if (list_link_active(&io->io_list_node)) {
64 			list_remove(&qg->request_list[path], io);
65 		}
66 		mutex_exit(&qg->submit_lock[path]);
67 #ifdef DEBUG
68 		if (pqi_shuffle_delay != 0) { /* try to force deadlock error */
69 			pqi_state_t	*s = c->pc_softc;
70 			pqi_lun_reset(s, d);
71 			delay(pqi_shuffle_delay * drv_usectohz(1000000));
72 		}
73 #endif	/* DEBUG */
74 		mutex_enter(&d->pd_mutex);
75 		mutex_enter(&c->pc_mutex);
76 	}
77 }
78 
79 pqi_cmd_action_t
pqi_cmd_action_nolock(pqi_cmd_t * c,pqi_cmd_action_t a)80 pqi_cmd_action_nolock(pqi_cmd_t *c, pqi_cmd_action_t a)
81 {
82 	pqi_device_t	*d = c->pc_device;
83 	pqi_state_t	*s = c->pc_softc;
84 	struct scsi_pkt	*pkt;
85 
86 	mutex_enter(&c->pc_mutex);
87 	/*
88 	 * Don't change cmd if we are in middle of a timeout.
89 	 */
90 	if ((c->pc_flags & PQI_FLAG_TIMED_OUT) != 0) {
91 		a = PQI_CMD_FAIL;
92 		goto skipto;
93 	}
94 	c->pc_last_action = c->pc_cur_action;
95 	c->pc_cur_action = a;
96 	switch (a) {
97 	case PQI_CMD_QUEUE:
98 		list_insert_tail(&d->pd_cmd_list, c);
99 
100 		/*
101 		 * Set the start time now in case the HBA hangs. That will
102 		 * allow the timeout processing to handle these commands, which
103 		 * in theory have been started but not really started, without
104 		 * the need for special handling logic in the timeout scan.
105 		 */
106 		cmd_start_time(c);
107 		break;
108 
109 	case PQI_CMD_START:
110 		if (c->pc_last_action == PQI_CMD_FAIL) {
111 			list_remove(&d->pd_cmd_list, c);
112 
113 			pkt = CMD2PKT(c);
114 			if (pkt == NULL) {
115 				pqi_io_request_t	*io = c->pc_io_rqst;
116 
117 				io->io_status = PQI_DATA_IN_OUT_TIMEOUT;
118 				(*io->io_cb)(io, io->io_context);
119 				pqi_free_io(io);
120 				c->pc_io_rqst = NULL;
121 			} else {
122 				pqi_free_io(c->pc_io_rqst);
123 				c->pc_io_rqst = NULL;
124 				(void) ddi_taskq_dispatch(s->s_complete_taskq,
125 				    cmd_finish_task, c, 0);
126 			}
127 			a = PQI_CMD_FAIL;
128 		} else {
129 			/*
130 			 * Now that the command is actually being sent to the
131 			 * HBA reset the start so that a timeout will occur
132 			 * only after the HBA has had the command for some
133 			 * amount of time as defined by the SCSI packet.
134 			 */
135 			cmd_start_time(c);
136 		}
137 		break;
138 
139 	case PQI_CMD_FAIL:
140 		if (c->pc_last_action == PQI_CMD_START) {
141 			/*
142 			 * There's no means to cancel a command that has
143 			 * been passed to the HBA, at least none without more
144 			 * documentation. So, if the command has been passed
145 			 * to the HBA the queue slot must remain active until
146 			 * the command completes. If it fails to complete
147 			 * then it will be freed by cmd_timeout_scan() when
148 			 * the action is PQI_CMD_TIMEOUT. So, for now keep
149 			 * the action as being PQI_CMD_START.
150 			 */
151 			a = PQI_CMD_START;
152 		} else {
153 			/*
154 			 * Don't do any actual processing here to cancel and
155 			 * free the command. By leaving the pc_cur_action
156 			 * set to PQI_CMD_FAIL the command will be freed
157 			 * when pqi_start_io() calls pqi_cmd_action(). The need
158 			 * for handling the error case in this manner is due
159 			 * to a small window in pqi_start_io() where the command
160 			 * has been removed from the group queue and before
161 			 * pqi_cmd_action() is called. It would be possible
162 			 * to fix by adding an additional lock to
163 			 * pqi_io_request_t or handle the issue in this manner.
164 			 * Less locks == good.
165 			 */
166 			/*
167 			 * We could have come in here during a cmd timeout
168 			 * lock shuffle so last action might be timeout here.
169 			 */
170 			ASSERT(c->pc_last_action == PQI_CMD_TIMEOUT ||
171 			    c->pc_last_action == PQI_CMD_QUEUE);
172 		}
173 		break;
174 
175 	case PQI_CMD_TIMEOUT:
176 		list_remove(&d->pd_cmd_list, c);
177 		/*
178 		 * Set a flag to prevent this command from changing while we
179 		 * shuffle locks below.
180 		 */
181 		c->pc_flags |= PQI_FLAG_TIMED_OUT;
182 		cmd_remove_group(c);
183 
184 		/*
185 		 * When a timeout has occurred it means something has gone
186 		 * wrong with the HBA or drive.  Timed out io requests are
187 		 * marked and the cmd was marked and removed from the chain
188 		 * above so it should not have changed state when we dropped
189 		 * and re-grabbed the locks.
190 		 */
191 		ASSERT3U(c->pc_cur_action, ==, PQI_CMD_TIMEOUT);
192 
193 		c->pc_flags &= ~PQI_FLAG_TIMED_OUT;
194 		/*
195 		 * Internal commands to the driver will not have a SCSI packet
196 		 * associated.
197 		 */
198 		pkt = CMD2PKT(c);
199 		if (pkt == NULL) {
200 			pqi_io_request_t	*io = c->pc_io_rqst;
201 
202 			io->io_status = PQI_DATA_IN_OUT_TIMEOUT;
203 			(*io->io_cb)(io, io->io_context);
204 			pqi_free_io(c->pc_io_rqst);
205 			c->pc_io_rqst = NULL;
206 		} else {
207 			pqi_free_io(c->pc_io_rqst);
208 			c->pc_io_rqst = NULL;
209 			mutex_exit(&c->pc_mutex);
210 			(void) ddi_taskq_dispatch(s->s_complete_taskq,
211 			    cmd_finish_task, c, 0);
212 			return (a);
213 		}
214 		break;
215 
216 	case PQI_CMD_CMPLT:
217 		if (c->pc_last_action == PQI_CMD_TIMEOUT)
218 			break;
219 
220 		list_remove(&d->pd_cmd_list, c);
221 
222 		pqi_free_io(c->pc_io_rqst);
223 		c->pc_io_rqst = NULL;
224 		if (CMD2PKT(c) != NULL) {
225 			/*
226 			 * ddi_taskq_dispatch doesn't always start a separate
227 			 * thread. Under some conditions this will turn into
228 			 * a direct call to cmd_finish_task(). That in turn
229 			 * calls into the SCSA layer which can call
230 			 * tran_ini_pkt which will eventually try to call
231 			 * pqi_cmd_action(). So, need to drop the mutex before
232 			 * making the call to ddi_taskq_dispatch and then
233 			 * return.
234 			 */
235 			mutex_exit(&c->pc_mutex);
236 			(void) ddi_taskq_dispatch(s->s_complete_taskq,
237 			    cmd_finish_task, c, 0);
238 			return (a);
239 		}
240 		break;
241 
242 	default:
243 		cmn_err(CE_PANIC,
244 		    "%s: Unknown action request: %d", __func__, a);
245 	}
246 skipto:
247 	mutex_exit(&c->pc_mutex);
248 	return (a);
249 }
250 
251 pqi_cmd_action_t
pqi_cmd_action(pqi_cmd_t * c,pqi_cmd_action_t a)252 pqi_cmd_action(pqi_cmd_t *c, pqi_cmd_action_t a)
253 {
254 	pqi_device_t		*d = c->pc_device;
255 	pqi_cmd_action_t	rval;
256 
257 	mutex_enter(&d->pd_mutex);
258 	rval = pqi_cmd_action_nolock(c, a);
259 	mutex_exit(&d->pd_mutex);
260 
261 	return (rval);
262 }
263 
264 boolean_t
pqi_is_offline(pqi_state_t * s)265 pqi_is_offline(pqi_state_t *s)
266 {
267 	return (s->s_offline);
268 }
269 
270 /*
271  * pqi_alloc_io -- return next available slot.
272  */
273 pqi_io_request_t *
pqi_alloc_io(pqi_state_t * s)274 pqi_alloc_io(pqi_state_t *s)
275 {
276 	pqi_io_request_t	*io	= NULL;
277 	uint16_t		loop;
278 	uint16_t		i;
279 
280 	mutex_enter(&s->s_io_mutex);
281 	i = s->s_next_io_slot; /* just a hint */
282 	s->s_io_need++;
283 	for (;;) {
284 		for (loop = 0; loop < s->s_max_io_slots; loop++) {
285 			/*
286 			 * Controller offline can only occur if the HBA is going
287 			 * through reset due to firmware hang.
288 			 */
289 			if (pqi_is_offline(s)) {
290 				mutex_exit(&s->s_io_mutex);
291 				return (NULL);
292 			}
293 			io = &s->s_io_rqst_pool[i];
294 			i = (i + 1) % s->s_max_io_slots;
295 			if (io->io_refcount == 0) {
296 				io->io_refcount = 1;
297 				break;
298 			}
299 		}
300 		if (loop != s->s_max_io_slots)
301 			break;
302 
303 		s->s_io_had2wait++;
304 		s->s_io_wait_cnt++;
305 		if (cv_wait_sig(&s->s_io_condvar, &s->s_io_mutex) == 0) {
306 			s->s_io_sig++;
307 			io = NULL;
308 			break;
309 		}
310 		i = s->s_next_io_slot; /* just a hint */
311 	}
312 	s->s_next_io_slot = i;
313 	mutex_exit(&s->s_io_mutex);
314 
315 	if (io != NULL)
316 		reinit_io(io);
317 	return (io);
318 }
319 
320 void
pqi_free_io(pqi_io_request_t * io)321 pqi_free_io(pqi_io_request_t *io)
322 {
323 	pqi_state_t	*s = io->io_softc;
324 
325 	mutex_enter(&s->s_io_mutex);
326 	ASSERT(io->io_refcount == 1);
327 	io->io_refcount = 0;
328 	reinit_io(io);
329 	if (s->s_io_wait_cnt != 0) {
330 		s->s_io_wait_cnt--;
331 		cv_signal(&s->s_io_condvar);
332 	}
333 	mutex_exit(&s->s_io_mutex);
334 }
335 
336 
337 /*
338  * Time out an in progress i/o.
339  * If the i/o has been serviced then return false (can't timeout),
340  * otherwise increment the generation counter and return true.
341  */
342 boolean_t
pqi_timeout_io(pqi_io_request_t * io)343 pqi_timeout_io(pqi_io_request_t *io)
344 {
345 	mutex_enter(&io->io_lock);
346 	if (io->io_serviced) {
347 		/*
348 		 * Can't timeout this io, it's already been serviced.
349 		 */
350 		mutex_exit(&io->io_lock);
351 		return (B_FALSE);
352 	}
353 	io->io_gen = (io->io_gen + 1) % PQI_NGENS;
354 	mutex_exit(&io->io_lock);
355 	return (B_TRUE);
356 }
357 
358 /*
359  * Check if an i/o is serviceable (generation counter matches).
360  * If so, mark it as serviced and return true.
361  * Otherwise, return false.
362  */
363 boolean_t
pqi_service_io(pqi_io_request_t * io,uint8_t generation)364 pqi_service_io(pqi_io_request_t *io, uint8_t generation)
365 {
366 	mutex_enter(&io->io_lock);
367 	if (io->io_gen != generation) {
368 		/*
369 		 * Can't service this io, it's already been timed out.
370 		 */
371 		mutex_exit(&io->io_lock);
372 		return (B_FALSE);
373 	}
374 	io->io_serviced = B_TRUE;
375 	mutex_exit(&io->io_lock);
376 	return (B_TRUE);
377 }
378 
379 void
pqi_dump_io(pqi_io_request_t * io)380 pqi_dump_io(pqi_io_request_t *io)
381 {
382 	pqi_iu_header_t	*hdr = io->io_iu;
383 	pqi_state_t	*s;
384 
385 	if (io->io_cmd != NULL) {
386 		s = io->io_cmd->pc_softc;
387 	} else {
388 		/*
389 		 * Early on, during driver attach, commands are run without
390 		 * a pqi_cmd_t structure associated. These io requests are
391 		 * low level operations direct to the HBA. So, grab a
392 		 * reference to the first and only instance through the
393 		 * DDI interface. Even though there might be multiple HBA's
394 		 * grabbing the first is okay since dump_raid() only references
395 		 * the debug level which will be the same for all the
396 		 * controllers.
397 		 */
398 		s = ddi_get_soft_state(pqi_state, 0);
399 	}
400 
401 	if (hdr->iu_type == PQI_REQUEST_IU_AIO_PATH_IO) {
402 		dump_aio(io->io_iu);
403 	} else if (hdr->iu_type == PQI_REQUEST_IU_RAID_PATH_IO) {
404 		dump_raid(s, io->io_iu, io->io_pi);
405 	}
406 }
407 
408 static uint_t supported_event_types[] = {
409 	PQI_EVENT_TYPE_HOTPLUG,
410 	PQI_EVENT_TYPE_HARDWARE,
411 	PQI_EVENT_TYPE_PHYSICAL_DEVICE,
412 	PQI_EVENT_TYPE_LOGICAL_DEVICE,
413 	PQI_EVENT_TYPE_AIO_STATE_CHANGE,
414 	PQI_EVENT_TYPE_AIO_CONFIG_CHANGE,
415 	PQI_EVENT_TYPE_HEARTBEAT
416 };
417 
418 int
pqi_map_event(uint8_t event)419 pqi_map_event(uint8_t event)
420 {
421 	int i;
422 
423 	for (i = 0; i < sizeof (supported_event_types) / sizeof (uint_t); i++)
424 		if (supported_event_types[i] == event)
425 			return (i);
426 	return (-1);
427 }
428 
429 boolean_t
pqi_supported_event(uint8_t event)430 pqi_supported_event(uint8_t event)
431 {
432 	return (pqi_map_event(event) == -1 ? B_FALSE : B_TRUE);
433 }
434 
435 char *
pqi_event_to_str(uint8_t event)436 pqi_event_to_str(uint8_t event)
437 {
438 	switch (event) {
439 	case PQI_EVENT_TYPE_HOTPLUG: return ("Hotplug");
440 	case PQI_EVENT_TYPE_HARDWARE: return ("Hardware");
441 	case PQI_EVENT_TYPE_PHYSICAL_DEVICE:
442 		return ("Physical Device");
443 	case PQI_EVENT_TYPE_LOGICAL_DEVICE: return ("logical Device");
444 	case PQI_EVENT_TYPE_AIO_STATE_CHANGE:
445 		return ("AIO State Change");
446 	case PQI_EVENT_TYPE_AIO_CONFIG_CHANGE:
447 		return ("AIO Config Change");
448 	case PQI_EVENT_TYPE_HEARTBEAT: return ("Heartbeat");
449 	default: return ("Unsupported Event Type");
450 	}
451 }
452 
453 char *
bool_to_str(int v)454 bool_to_str(int v)
455 {
456 	return (v ? "T" : "f");
457 }
458 
459 char *
dtype_to_str(int t)460 dtype_to_str(int t)
461 {
462 	switch (t) {
463 	case DTYPE_DIRECT: return ("Direct");
464 	case DTYPE_SEQUENTIAL: return ("Sequential");
465 	case DTYPE_ESI: return ("ESI");
466 	case DTYPE_ARRAY_CTRL: return ("RAID");
467 	default: return ("Ughknown");
468 	}
469 }
470 
471 static ddi_dma_attr_t single_dma_attrs = {
472 	.dma_attr_version =	DMA_ATTR_V0,
473 	.dma_attr_addr_lo =	0x0ull,
474 	.dma_attr_addr_hi =	0xffffffffffffffffull,
475 	.dma_attr_count_max =	0x7ffffull,
476 	.dma_attr_align =	4096,
477 	.dma_attr_burstsizes =	0x78,
478 	.dma_attr_minxfer =	1,
479 	.dma_attr_maxxfer =	0x007ffffull,
480 	.dma_attr_seg =		0xffffffffull,
481 	.dma_attr_sgllen =	1,
482 	.dma_attr_granular =	512,
483 	.dma_attr_flags =	0,
484 };
485 
486 pqi_dma_overhead_t *
pqi_alloc_single(pqi_state_t * s,size_t len)487 pqi_alloc_single(pqi_state_t *s, size_t len)
488 {
489 	pqi_dma_overhead_t	*d;
490 	ddi_dma_cookie_t	cookie;
491 
492 	d = kmem_zalloc(sizeof (*d), KM_SLEEP);
493 	d->len_to_alloc = len;
494 
495 	if (ddi_dma_alloc_handle(s->s_dip, &single_dma_attrs,
496 	    DDI_DMA_SLEEP, 0, &d->handle) != DDI_SUCCESS)
497 		goto error_out;
498 
499 	if (ddi_dma_mem_alloc(d->handle, len, &s->s_reg_acc_attr,
500 	    DDI_DMA_CONSISTENT, DDI_DMA_SLEEP, 0,
501 	    &d->alloc_memory, &len, &d->acc) != DDI_SUCCESS)
502 		goto error_out;
503 
504 	bzero(d->alloc_memory, len);
505 	if (ddi_dma_addr_bind_handle(d->handle, NULL, d->alloc_memory, len,
506 	    DDI_DMA_RDWR, DDI_DMA_SLEEP, 0, &cookie, &d->cookie_count) !=
507 	    DDI_SUCCESS)
508 		goto error_out;
509 
510 	d->dma_addr = cookie.dmac_laddress;
511 	if (d->cookie_count != 1)
512 		ddi_dma_nextcookie(d->handle, &d->second);
513 
514 	return (d);
515 
516 error_out:
517 	pqi_free_single(s, d);
518 	return (NULL);
519 }
520 
521 void
pqi_free_single(pqi_state_t * s __unused,pqi_dma_overhead_t * d)522 pqi_free_single(pqi_state_t *s __unused, pqi_dma_overhead_t *d)
523 {
524 	(void) ddi_dma_unbind_handle(d->handle);
525 	if (d->alloc_memory != NULL)
526 		ddi_dma_mem_free(&d->acc);
527 	if (d->handle != NULL)
528 		ddi_dma_free_handle(&d->handle);
529 	kmem_free(d, sizeof (*d));
530 }
531 
532 void
pqi_show_dev_state(pqi_state_t * s)533 pqi_show_dev_state(pqi_state_t *s)
534 {
535 	uint32_t dev_status = G32(s, pqi_registers.device_status);
536 
537 	switch (dev_status & 0xf) {
538 	case 0:
539 		cmn_err(CE_NOTE, "Power_On_And_Reset");
540 		break;
541 
542 	case 1:
543 		cmn_err(CE_NOTE, "PQI_Status_Available");
544 		break;
545 
546 	case 2:
547 		cmn_err(CE_NOTE, "All_Registers_Ready");
548 		break;
549 
550 	case 3:
551 		cmn_err(CE_NOTE,
552 		    "Adminstrator_Queue_Pair_Ready");
553 		break;
554 
555 	case 4:
556 		cmn_err(CE_NOTE, "Error: %s %s",
557 		    dev_status & 0x100 ? "(OP OQ Error)" : "",
558 		    dev_status & 0x200 ? "(OP IQ Error)" : "");
559 		show_error_detail(s);
560 		break;
561 
562 	default:
563 		cmn_err(CE_WARN, "Unknown HBA status: 0x%x", dev_status);
564 		break;
565 	}
566 }
567 
568 char *
cdb_to_str(uint8_t scsi_cmd)569 cdb_to_str(uint8_t scsi_cmd)
570 {
571 	switch (scsi_cmd) {
572 	case SCMD_INQUIRY: return ("Inquiry");
573 	case SCMD_TEST_UNIT_READY: return ("TestUnitReady");
574 	case SCMD_READ: return ("Read");
575 	case SCMD_READ_G1: return ("Read G1");
576 	case SCMD_RESERVE: return ("Reserve");
577 	case SCMD_RELEASE: return ("Release");
578 	case SCMD_WRITE: return ("Write");
579 	case SCMD_WRITE_G1: return ("Write G1");
580 	case SCMD_START_STOP: return ("StartStop");
581 	case SCMD_READ_CAPACITY: return ("ReadCap");
582 	case SCMD_MODE_SENSE: return ("ModeSense");
583 	case SCMD_MODE_SELECT: return ("ModeSelect");
584 	case SCMD_SVC_ACTION_IN_G4: return ("ActionInG4");
585 	case SCMD_MAINTENANCE_IN: return ("MaintenanceIn");
586 	case SCMD_GDIAG: return ("ReceiveDiag");
587 	case SCMD_SDIAG: return ("SendDiag");
588 	case SCMD_LOG_SENSE_G1: return ("LogSenseG1");
589 	case SCMD_PERSISTENT_RESERVE_IN: return ("PgrReserveIn");
590 	case SCMD_PERSISTENT_RESERVE_OUT: return ("PgrReserveOut");
591 	case BMIC_READ: return ("BMIC Read");
592 	case BMIC_WRITE: return ("BMIC Write");
593 	case CISS_REPORT_LOG: return ("CISS Report Logical");
594 	case CISS_REPORT_PHYS: return ("CISS Report Physical");
595 	default: return ("unmapped");
596 	}
597 }
598 
599 char *
io_status_to_str(int val)600 io_status_to_str(int val)
601 {
602 	switch (val) {
603 	case PQI_DATA_IN_OUT_GOOD: return ("Good");
604 	case PQI_DATA_IN_OUT_UNDERFLOW: return ("Underflow");
605 	case PQI_DATA_IN_OUT_ERROR: return ("ERROR");
606 	case PQI_DATA_IN_OUT_PROTOCOL_ERROR: return ("Protocol Error");
607 	case PQI_DATA_IN_OUT_HARDWARE_ERROR: return ("Hardware Error");
608 	default: return ("UNHANDLED");
609 	}
610 }
611 
612 char *
scsi_status_to_str(uint8_t val)613 scsi_status_to_str(uint8_t val)
614 {
615 	switch (val) {
616 	case STATUS_GOOD: return ("Good");
617 	case STATUS_CHECK: return ("Check");
618 	case STATUS_MET: return ("Met");
619 	case STATUS_BUSY: return ("Busy");
620 	case STATUS_INTERMEDIATE: return ("Intermediate");
621 	case STATUS_RESERVATION_CONFLICT: return ("Reservation Conflict");
622 	case STATUS_TERMINATED: return ("Terminated");
623 	case STATUS_QFULL: return ("QFull");
624 	case STATUS_ACA_ACTIVE: return ("ACA Active");
625 	case STATUS_TASK_ABORT: return ("Task Abort");
626 	default: return ("Illegal Status");
627 	}
628 }
629 
630 char *
iu_type_to_str(int val)631 iu_type_to_str(int val)
632 {
633 	switch (val) {
634 	case PQI_RESPONSE_IU_RAID_PATH_IO_SUCCESS: return ("Success");
635 	case PQI_RESPONSE_IU_AIO_PATH_IO_SUCCESS: return ("AIO Success");
636 	case PQI_RESPONSE_IU_GENERAL_MANAGEMENT: return ("General");
637 	case PQI_RESPONSE_IU_RAID_PATH_IO_ERROR: return ("IO Error");
638 	case PQI_RESPONSE_IU_AIO_PATH_IO_ERROR: return ("AIO IO Error");
639 	case PQI_RESPONSE_IU_AIO_PATH_DISABLED: return ("AIO Path Disabled");
640 	default: return ("UNHANDLED");
641 	}
642 }
643 
644 void
pqi_free_mem_len(mem_len_pair_t * m)645 pqi_free_mem_len(mem_len_pair_t *m)
646 {
647 	kmem_free(m->mem, m->len);
648 }
649 
650 mem_len_pair_t
pqi_alloc_mem_len(int len)651 pqi_alloc_mem_len(int len)
652 {
653 	mem_len_pair_t m;
654 	m.len = len;
655 	m.mem = kmem_alloc(m.len, KM_SLEEP);
656 	*m.mem = '\0';
657 	return (m);
658 }
659 
660 /*
661  * []------------------------------------------------------------------[]
662  * | Support/utility functions for main functions above			|
663  * []------------------------------------------------------------------[]
664  */
665 
666 typedef struct qual {
667 	int	q_val;
668 	char	*q_str;
669 } qual_t;
670 
671 typedef struct code_qual {
672 	int	cq_code;
673 	qual_t	*cq_list;
674 } code_qual_t;
675 
676 /*
677  * These messages come from pqi2r01 spec section 5.6 table 18.
678  */
679 static qual_t pair0[] = { {0, "No error"}, {0, NULL} };
680 static qual_t pair1[] = { {0, "Error detected during initialization"},
681 	{ 0, NULL } };
682 static qual_t pair2[] = { {1, "Invalid PD Function"},
683 	{2, "Invalid paramter for PD function"},
684 	{0, NULL } };
685 static qual_t pair3[] = { {0, "Error creating admin queue pair"},
686 	{ 1, "Error deleting admin queue pair"},
687 	{ 0, NULL} };
688 static qual_t pair4[] = { {1, "Invalid IU type in general" },
689 	{2, "Invalid IU length in general admin request"},
690 	{0, NULL} };
691 static qual_t pair5[] = { {1, "Internal error" },
692 	{2, "OQ spanning conflict"},
693 	{0, NULL} };
694 static qual_t pair6[] = { {1, "Error completing PQI soft reset"},
695 	{2, "Error completing PQI firmware reset"},
696 	{3, "Error completing PQI hardware reset"},
697 	{0, NULL} };
698 static code_qual_t cq_table[] = {
699 	{ 0, pair0 },
700 	{ 1, pair1 },
701 	{ 2, pair2 },
702 	{ 3, pair3 },
703 	{ 4, pair4 },
704 	{ 5, pair5 },
705 	{ 6, pair6 },
706 	{ 0, NULL },
707 };
708 
709 /*
710  * cmd_finish_task -- taskq to complete command processing
711  *
712  * Under high load the driver will run out of IO slots which causes command
713  * requests to pause until a slot is free. Calls to pkt_comp below can circle
714  * through the SCSI layer and back into the driver to start another command
715  * request and therefore possibly pause. If cmd_finish_task() was called on
716  * the interrupt thread a hang condition could occur because IO slots wouldn't
717  * be processed and then freed. So, this portion of the command completion
718  * is run on a taskq.
719  */
720 static void
cmd_finish_task(void * v)721 cmd_finish_task(void *v)
722 {
723 	pqi_cmd_t	*c = v;
724 	struct scsi_pkt	*pkt = CMD2PKT(c);
725 
726 	if (c->pc_poll)
727 		sema_v(c->pc_poll);
728 
729 	if (pkt != NULL && (pkt->pkt_flags & FLAG_NOINTR) == 0 &&
730 	    (pkt->pkt_comp != NULL))
731 		(*pkt->pkt_comp)(pkt);
732 }
733 
734 static void
cmd_start_time(pqi_cmd_t * c)735 cmd_start_time(pqi_cmd_t *c)
736 {
737 	c->pc_start_time = gethrtime();
738 	if (CMD2PKT(c) != NULL) {
739 		c->pc_expiration = c->pc_start_time +
740 		    ((hrtime_t)c->pc_pkt->pkt_time * NANOSEC);
741 	} else {
742 		c->pc_expiration = c->pc_start_time + 5 * NANOSEC;
743 	}
744 }
745 
746 static void
show_error_detail(pqi_state_t * s)747 show_error_detail(pqi_state_t *s)
748 {
749 	uint32_t error_reg = G32(s, pqi_registers.device_error);
750 	uint8_t		code, qualifier;
751 	qual_t		*p;
752 	code_qual_t	*cq;
753 
754 	code = error_reg & 0xff;
755 	qualifier = (error_reg >> 8) & 0xff;
756 
757 	for (cq = cq_table; cq->cq_list != NULL; cq++) {
758 		if (cq->cq_code == code) {
759 			for (p = cq->cq_list; p->q_str != NULL; p++) {
760 				if (p->q_val == qualifier) {
761 					cmn_err(CE_NOTE,
762 					    "[code=%x,qual=%x]: %s",
763 					    code, qualifier, p->q_str);
764 					return;
765 				}
766 			}
767 		}
768 	}
769 	cmn_err(CE_NOTE, "Undefined code(%x)/qualifier(%x)",
770 	    code, qualifier);
771 }
772 
773 static void
pqi_catch_release(pqi_io_request_t * io __unused,void * v __unused)774 pqi_catch_release(pqi_io_request_t *io __unused, void *v __unused)
775 {
776 	/*
777 	 * This call can occur if the software times out a command because
778 	 * the HBA hasn't responded in the default amount of time, 10 seconds,
779 	 * and then the HBA responds. It's occurred a few times during testing
780 	 * so catch and ignore.
781 	 */
782 	cmn_err(CE_NOTE, "%s: caught", __func__);
783 }
784 
785 static void
reinit_io(pqi_io_request_t * io)786 reinit_io(pqi_io_request_t *io)
787 {
788 	io->io_cb = pqi_catch_release;
789 	io->io_status = 0;
790 	io->io_serviced = B_FALSE;
791 	io->io_error_info = NULL;
792 	io->io_raid_bypass = B_FALSE;
793 	io->io_context = NULL;
794 	io->io_cmd = NULL;
795 }
796 
797 mem_len_pair_t
build_cdb_str(uint8_t * cdb)798 build_cdb_str(uint8_t *cdb)
799 {
800 	mem_len_pair_t m = pqi_alloc_mem_len(64);
801 
802 	m.mem[0] = '\0';
803 
804 	switch (cdb[0]) {
805 	case SCMD_INQUIRY:
806 		MEMP("%s", cdb_to_str(cdb[0]));
807 		if ((cdb[1] & 0x1) != 0)
808 			MEMP(".vpd=%x", cdb[2]);
809 		else if (cdb[2])
810 			MEMP("Illegal CDB");
811 		MEMP(".len=%x", cdb[3] << 8 | cdb[4]);
812 		break;
813 
814 	case SCMD_READ:
815 		MEMP("%s.lba=%x.len=%x", cdb_to_str(cdb[0]),
816 		    (cdb[1] & 0x1f) << 16 | cdb[2] << 8 | cdb[3],
817 		    cdb[4]);
818 		break;
819 
820 	case SCMD_MODE_SENSE:
821 		MEMP("%s.dbd=%s.pc=%x.page_code=%x.subpage=%x."
822 		    "len=%x", cdb_to_str(cdb[0]),
823 		    bool_to_str(cdb[1] & 8), cdb[2] >> 6 & 0x3,
824 		    cdb[2] & 0x3f, cdb[3], cdb[4]);
825 		break;
826 
827 	case SCMD_START_STOP:
828 		MEMP("%s.immed=%s.power=%x.start=%s",
829 		    cdb_to_str(cdb[0]), bool_to_str(cdb[1] & 1),
830 		    (cdb[4] >> 4) & 0xf, bool_to_str(cdb[4] & 1));
831 		break;
832 
833 	case SCMD_SVC_ACTION_IN_G4:
834 	case SCMD_READ_CAPACITY:
835 	case SCMD_TEST_UNIT_READY:
836 	default:
837 		MEMP("%s (%x)", cdb_to_str(cdb[0]), cdb[0]);
838 		break;
839 	}
840 	return (m);
841 }
842 
843 mem_len_pair_t
mem_to_arraystr(uint8_t * ptr,size_t len)844 mem_to_arraystr(uint8_t *ptr, size_t len)
845 {
846 	mem_len_pair_t	m	= pqi_alloc_mem_len(len * 3 + 20);
847 	int		i;
848 
849 	m.mem[0] = '\0';
850 	MEMP("{ ");
851 	for (i = 0; i < len; i++) {
852 		MEMP("%02x ", *ptr++ & 0xff);
853 	}
854 	MEMP(" }");
855 
856 	return (m);
857 }
858 
859 static char lun_str[64];
860 static char *
lun_to_str(uint8_t * lun)861 lun_to_str(uint8_t *lun)
862 {
863 	int	i;
864 	lun_str[0] = '\0';
865 	for (i = 0; i < 8; i++)
866 		(void) snprintf(lun_str + strlen(lun_str),
867 		    sizeof (lun_str) - strlen(lun_str), "%02x.", *lun++);
868 	return (lun_str);
869 }
870 
871 static char *
dir_to_str(int dir)872 dir_to_str(int dir)
873 {
874 	switch (dir) {
875 	case SOP_NO_DIRECTION_FLAG: return ("NoDir");
876 	case SOP_WRITE_FLAG: return ("Write");
877 	case SOP_READ_FLAG: return ("Read");
878 	case SOP_BIDIRECTIONAL: return ("RW");
879 	default: return ("Oops");
880 	}
881 }
882 
883 static char *
flags_to_str(uint32_t flag)884 flags_to_str(uint32_t flag)
885 {
886 	switch (flag) {
887 	case CISS_SG_LAST: return ("Last");
888 	case CISS_SG_CHAIN: return ("Chain");
889 	case CISS_SG_NORMAL: return ("Norm");
890 	default: return ("Ooops");
891 	}
892 }
893 
894 /* ---- Only for use in dump_raid and dump_aio ---- */
895 #define	SCRATCH_PRINT(args...) (void)snprintf(scratch + strlen(scratch), \
896     len - strlen(scratch), args)
897 
898 static void
dump_raid(pqi_state_t * s,void * v,pqi_index_t idx)899 dump_raid(pqi_state_t *s, void *v, pqi_index_t idx)
900 {
901 	int			i;
902 	int			len	= 512;
903 	caddr_t			scratch;
904 	pqi_raid_path_request_t	*rqst = v;
905 	mem_len_pair_t		cdb_data;
906 	caddr_t			raw = v;
907 
908 	scratch = kmem_alloc(len, KM_SLEEP);
909 	scratch[0] = '\0';
910 
911 	if (s->s_debug_level & DBG_LVL_RAW_RQST) {
912 		SCRATCH_PRINT("RAW RQST: ");
913 		for (i = 0; i < sizeof (*rqst); i++)
914 			SCRATCH_PRINT("%02x:", *raw++ & 0xff);
915 		cmn_err(CE_NOTE, "%s", scratch);
916 		scratch[0] = '\0';
917 	}
918 
919 	if (s->s_debug_level & DBG_LVL_CDB) {
920 		cdb_data = build_cdb_str(rqst->rp_cdb);
921 		SCRATCH_PRINT("cdb(%s),", cdb_data.mem);
922 		pqi_free_mem_len(&cdb_data);
923 	}
924 
925 	ASSERT0(rqst->header.reserved);
926 	ASSERT0(rqst->reserved1);
927 	ASSERT0(rqst->reserved2);
928 	ASSERT0(rqst->reserved3);
929 	ASSERT0(rqst->reserved4);
930 	ASSERT0(rqst->reserved5);
931 
932 	if (s->s_debug_level & DBG_LVL_RQST) {
933 		SCRATCH_PRINT("pi=%x,h(type=%x,len=%x,id=%x)", idx,
934 		    rqst->header.iu_type, rqst->header.iu_length,
935 		    rqst->header.iu_id);
936 		SCRATCH_PRINT("rqst_id=%x,nexus_id=%x,len=%x,lun=(%s),"
937 		    "proto=%x,dir=%s,partial=%s,",
938 		    rqst->rp_id, rqst->rp_nexus_id, rqst->rp_data_len,
939 		    lun_to_str(rqst->rp_lun), rqst->protocol_specific,
940 		    dir_to_str(rqst->rp_data_dir),
941 		    bool_to_str(rqst->rp_partial));
942 		SCRATCH_PRINT("fence=%s,error_idx=%x,task_attr=%x,"
943 		    "priority=%x,additional=%x,sg=(",
944 		    bool_to_str(rqst->rp_fence), rqst->rp_error_index,
945 		    rqst->rp_task_attr,
946 		    rqst->rp_pri, rqst->rp_additional_cdb);
947 		for (i = 0; i < PQI_MAX_EMBEDDED_SG_DESCRIPTORS; i++) {
948 			SCRATCH_PRINT("%lx:%x:%s,",
949 			    (long unsigned int)rqst->rp_sglist[i].sg_addr,
950 			    rqst->rp_sglist[i].sg_len,
951 			    flags_to_str(rqst->rp_sglist[i].sg_flags));
952 		}
953 		SCRATCH_PRINT(")");
954 	}
955 
956 	cmn_err(CE_NOTE, "%s", scratch);
957 	kmem_free(scratch, len);
958 }
959 
960 static void
dump_aio(void * v)961 dump_aio(void *v)
962 {
963 	pqi_aio_path_request_t	*rqst	= v;
964 	int			i;
965 	int			len	= 512;
966 	caddr_t			scratch;
967 	mem_len_pair_t		cdb_data;
968 
969 	scratch = kmem_alloc(len, KM_SLEEP);
970 	scratch[0] = '\0';
971 
972 	cdb_data = build_cdb_str(rqst->cdb);
973 	SCRATCH_PRINT("cdb(%s)", cdb_data.mem);
974 	pqi_free_mem_len(&cdb_data);
975 
976 	SCRATCH_PRINT("h(type=%x,len=%x,id=%x)",
977 	    rqst->header.iu_type, rqst->header.iu_length,
978 	    rqst->header.iu_id);
979 	SCRATCH_PRINT("rqst_id=%x,nexus_id=%x,len=%x,lun=(%s),dir=%s,"
980 	    "partial=%s,",
981 	    rqst->request_id, rqst->nexus_id, rqst->buffer_length,
982 	    lun_to_str(rqst->lun_number),
983 	    dir_to_str(rqst->data_direction), bool_to_str(rqst->partial));
984 	SCRATCH_PRINT("fence=%s,error_idx=%x,task_attr=%x,priority=%x,"
985 	    "num_sg=%x,cdb_len=%x,sg=(",
986 	    bool_to_str(rqst->fence), rqst->error_index, rqst->task_attribute,
987 	    rqst->command_priority, rqst->num_sg_descriptors, rqst->cdb_length);
988 	for (i = 0; i < PQI_MAX_EMBEDDED_SG_DESCRIPTORS; i++) {
989 		SCRATCH_PRINT("%lx:%x:%s,",
990 		    (long unsigned int)rqst->ap_sglist[i].sg_addr,
991 		    rqst->ap_sglist[i].sg_len,
992 		    flags_to_str(rqst->ap_sglist[i].sg_flags));
993 	}
994 	SCRATCH_PRINT(")");
995 
996 	cmn_err(CE_NOTE, "%s", scratch);
997 	kmem_free(scratch, len);
998 }
999