1e4f5a11James Kremer/*
2e4f5a11James Kremer * CDDL HEADER START
3e4f5a11James Kremer *
4e4f5a11James Kremer * The contents of this file are subject to the terms of the
5e4f5a11James Kremer * Common Development and Distribution License (the "License").
6e4f5a11James Kremer * You may not use this file except in compliance with the License.
7e4f5a11James Kremer *
8e4f5a11James Kremer * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9e4f5a11James Kremer * or http://www.opensolaris.org/os/licensing.
10e4f5a11James Kremer * See the License for the specific language governing permissions
11e4f5a11James Kremer * and limitations under the License.
12e4f5a11James Kremer *
13e4f5a11James Kremer * When distributing Covered Code, include this CDDL HEADER in each
14e4f5a11James Kremer * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15e4f5a11James Kremer * If applicable, add the following below this CDDL HEADER, with the
16e4f5a11James Kremer * fields enclosed by brackets "[]" replaced with your own identifying
17e4f5a11James Kremer * information: Portions Copyright [yyyy] [name of copyright owner]
18e4f5a11James Kremer *
19e4f5a11James Kremer * CDDL HEADER END
20e4f5a11James Kremer */
21e4f5a11James Kremer/*
22e4f5a11James Kremer * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23e4f5a11James Kremer */
24e4f5a11James Kremer
25e4f5a11James Kremer/*
26e4f5a11James Kremer * SES Log reader library
27e4f5a11James Kremer *
28e4f5a11James Kremer * This library is responsible for accessing the SES log at the target address,
29e4f5a11James Kremer * formatting and returning any log entries found.
30e4f5a11James Kremer *
31e4f5a11James Kremer * The data will be returned in an nvlist_t structure allocated here.
32e4f5a11James Kremer */
33e4f5a11James Kremer
34e4f5a11James Kremer#include <assert.h>
35e4f5a11James Kremer#include <errno.h>
36e4f5a11James Kremer#include <fcntl.h>
37e4f5a11James Kremer#include <sys/param.h>
38e4f5a11James Kremer#include <libseslog.h>
39e4f5a11James Kremer#include <stdlib.h>
40e4f5a11James Kremer#include <string.h>
41e4f5a11James Kremer#include <sys/stat.h>
42e4f5a11James Kremer#include <unistd.h>
43e4f5a11James Kremer#include <dirent.h>
44e4f5a11James Kremer#include <sys/scsi/generic/commands.h>
45e4f5a11James Kremer#include <sys/scsi/generic/status.h>
465cf8276Todd McKenney#include <sys/scsi/impl/commands.h>
47e4f5a11James Kremer
48e4f5a11James Kremer/*
49e4f5a11James Kremer * open the device with given device name
50e4f5a11James Kremer */
51e4f5a11James Kremerstatic int
52e4f5a11James Kremeropen_device(const char *device_name)
53e4f5a11James Kremer{
54e4f5a11James Kremer	int oflags = O_NONBLOCK | O_RDWR;
55e4f5a11James Kremer	int fd;
56e4f5a11James Kremer
57e4f5a11James Kremer	fd = open(device_name, oflags);
58e4f5a11James Kremer	if (fd < 0)
59e4f5a11James Kremer		fd = -errno;
60e4f5a11James Kremer	return (fd);
61e4f5a11James Kremer}
62e4f5a11James Kremer
63e4f5a11James Kremer/*
64e4f5a11James Kremer * Initialize scsi struct
65e4f5a11James Kremer */
66e4f5a11James Kremerstatic void
67e4f5a11James Kremerconstruct_scsi_pt_obj(struct uscsi_cmd *uscsi)
68e4f5a11James Kremer{
69e4f5a11James Kremer	(void) memset(uscsi, 0, sizeof (struct uscsi_cmd));
70e4f5a11James Kremer	uscsi->uscsi_timeout = DEF_PT_TIMEOUT;
71e4f5a11James Kremer	uscsi->uscsi_flags = USCSI_READ | USCSI_ISOLATE | USCSI_RQENABLE;
72e4f5a11James Kremer}
73e4f5a11James Kremer
74e4f5a11James Kremer/*
75e4f5a11James Kremer * set control cdb of scsi structure
76e4f5a11James Kremer */
77e4f5a11James Kremerstatic void
78e4f5a11James Kremerset_scsi_pt_cdb(struct uscsi_cmd *uscsi, const unsigned char *cdb,
79e4f5a11James Kremer    int cdb_len)
80e4f5a11James Kremer{
81e4f5a11James Kremer	uscsi->uscsi_cdb = (char *)cdb;
82e4f5a11James Kremer	uscsi->uscsi_cdblen = cdb_len;
83e4f5a11James Kremer}
84e4f5a11James Kremer
85e4f5a11James Kremer/*
86e4f5a11James Kremer * initialize sense data
87e4f5a11James Kremer */
88e4f5a11James Kremerstatic void
89e4f5a11James Kremerset_scsi_pt_sense(struct uscsi_cmd *uscsi, unsigned char *sense,
90e4f5a11James Kremer    int max_sense_len)
91e4f5a11James Kremer{
92e4f5a11James Kremer	(void) memset(sense, 0, max_sense_len);
93e4f5a11James Kremer	uscsi->uscsi_rqbuf = (char *)sense;
94e4f5a11James Kremer	uscsi->uscsi_rqlen = max_sense_len;
95e4f5a11James Kremer}
96e4f5a11James Kremer
97e4f5a11James Kremer/*
98e4f5a11James Kremer * Initialize data going to device
99e4f5a11James Kremer */
100e4f5a11James Kremerstatic void
101e4f5a11James Kremerset_scsi_pt_data_in(struct uscsi_cmd *uscsi, unsigned char *dxferp,
102e4f5a11James Kremer		    int dxfer_len)
103e4f5a11James Kremer{
104e4f5a11James Kremer	if (dxfer_len > 0) {
105e4f5a11James Kremer		uscsi->uscsi_bufaddr = (char *)dxferp;
106e4f5a11James Kremer		uscsi->uscsi_buflen = dxfer_len;
107e4f5a11James Kremer		uscsi->uscsi_flags = USCSI_READ | USCSI_ISOLATE |
108e4f5a11James Kremer		    USCSI_RQENABLE;
109e4f5a11James Kremer	}
110e4f5a11James Kremer}
111e4f5a11James Kremer
112e4f5a11James Kremer/*
113e4f5a11James Kremer * Executes SCSI command(or at least forwards it to lower layers).
114e4f5a11James Kremer */
115e4f5a11James Kremerstatic int
116e4f5a11James Kremerdo_scsi_pt(struct uscsi_cmd *uscsi, int fd, int time_secs)
117e4f5a11James Kremer{
118e4f5a11James Kremer	if (time_secs > 0)
119e4f5a11James Kremer		uscsi->uscsi_timeout = time_secs;
120e4f5a11James Kremer
121e4f5a11James Kremer	if (ioctl(fd, USCSICMD, uscsi)) {
122e4f5a11James Kremer		/* Took an error */
123e4f5a11James Kremer		return (errno);
124e4f5a11James Kremer	}
125e4f5a11James Kremer	return (0);
126e4f5a11James Kremer}
127e4f5a11James Kremer
128e4f5a11James Kremer
129e4f5a11James Kremer/*
130e4f5a11James Kremer * Read log from device
131e4f5a11James Kremer * Invokes a SCSI LOG SENSE command.
132e4f5a11James Kremer * Return:
133e4f5a11James Kremer * 0 -> success
134e4f5a11James Kremer * SG_LIB_CAT_INVALID_OP -> Log Sense not supported,
135e4f5a11James Kremer * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb,
136e4f5a11James Kremer * SG_LIB_CAT_NOT_READY -> device not ready,
137e4f5a11James Kremer * -1 -> other failure
138e4f5a11James Kremer */
139e4f5a11James Kremer
140e4f5a11James Kremerstatic int
141e4f5a11James Kremerread_log(int sg_fd, unsigned char *resp, int mx_resp_len)
142e4f5a11James Kremer{
143e4f5a11James Kremer	int res, ret;
144e4f5a11James Kremer	unsigned char logsCmdBlk[CDB_GROUP1] =
145e4f5a11James Kremer	    {SCMD_LOG_SENSE_G1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
146e4f5a11James Kremer	unsigned char sense_b[SENSE_BUFF_LEN];
147e4f5a11James Kremer	struct uscsi_cmd uscsi;
148e4f5a11James Kremer
149e4f5a11James Kremer	if (mx_resp_len > 0xffff) {
150e4f5a11James Kremer		return (-1);
151e4f5a11James Kremer	}
152e4f5a11James Kremer	logsCmdBlk[1] = 0;
153e4f5a11James Kremer	/* pc = 1, pg_code = 0x7 (logs page) */
154e4f5a11James Kremer	/* (((pc << 6) & 0xc0) | (pg_code & 0x3f)) = 0x47; */
155e4f5a11James Kremer	logsCmdBlk[2] = 0x47;
156e4f5a11James Kremer	/* pc = 1 current values */
157e4f5a11James Kremer	logsCmdBlk[3] = 0; /* No subpage code */
158e4f5a11James Kremer	logsCmdBlk[5] = 0; /* Want all logs starting from 0 */
159e4f5a11James Kremer	logsCmdBlk[6] = 0;
160e4f5a11James Kremer	logsCmdBlk[7] = (unsigned char) ((mx_resp_len >> 8) & 0xff);
161e4f5a11James Kremer	logsCmdBlk[8] = (unsigned char) (mx_resp_len & 0xff);
162e4f5a11James Kremer
163e4f5a11James Kremer	construct_scsi_pt_obj(&uscsi);
164e4f5a11James Kremer
165e4f5a11James Kremer	set_scsi_pt_cdb(&uscsi, logsCmdBlk, sizeof (logsCmdBlk));
166e4f5a11James Kremer	set_scsi_pt_sense(&uscsi, sense_b, sizeof (sense_b));
167e4f5a11James Kremer	set_scsi_pt_data_in(&uscsi, resp, mx_resp_len);
168e4f5a11James Kremer	res = do_scsi_pt(&uscsi, sg_fd, DEF_PT_TIMEOUT);
169e4f5a11James Kremer	if (res) {
170e4f5a11James Kremer		ret = res;
171e4f5a11James Kremer	} else {
172e4f5a11James Kremer		ret = uscsi.uscsi_status;
173e4f5a11James Kremer	}
174e4f5a11James Kremer	return (ret);
175e4f5a11James Kremer}
176e4f5a11James Kremer
177e4f5a11James Kremer/*
178e4f5a11James Kremer * Save the logs by walking through the entries in the response buffer.
179e4f5a11James Kremer *
180e4f5a11James Kremer * resp buffer looks like:
181e4f5a11James Kremer *
182e4f5a11James Kremer * +=====-========-========-========-========-========-========-========-=====+
183e4f5a11James Kremer * |  Bit|   7    |   6    |   5    |   4    |   3    |   2    |   1    |   0 |
184e4f5a11James Kremer * |Byte |        |        |        |        |        |        |        |     |
185e4f5a11James Kremer * |=====+====================================================================|
186e4f5a11James Kremer * | 0   |  reserved       |     page code                                    |
187e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
188e4f5a11James Kremer * | 1   |                   Reserved                                         |
189e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
190e4f5a11James Kremer * | 2   |(MSB)                           Page Length(n-3)                    |
191e4f5a11James Kremer * | --  |                                                                    |
192e4f5a11James Kremer * | 3   |                                                            (LSB)   |
193e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
194e4f5a11James Kremer * | 4   |                           Log Parameter (First)(Length X)          |
195e4f5a11James Kremer * | --  |                                                                    |
196e4f5a11James Kremer * | x+3 |                                                                    |
197e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
198e4f5a11James Kremer * |n-y+1|                           Log Parameter (Last)(Length y)           |
199e4f5a11James Kremer * | --  |                                                                    |
200e4f5a11James Kremer * | n   |                                                                    |
201e4f5a11James Kremer * +==========================================================================+
202e4f5a11James Kremer *
203e4f5a11James Kremer * Log parameter field looks like:
204e4f5a11James Kremer *
205e4f5a11James Kremer * +=====-========-========-========-========-========-========-========-=====+
206e4f5a11James Kremer * |  Bit|   7    |   6    |   5    |   4    |   3    |   2    |   1    |   0 |
207e4f5a11James Kremer * |Byte |        |        |        |        |        |        |        |     |
208e4f5a11James Kremer * |=====+====================================================================|
209e4f5a11James Kremer * | 0   |(MSB)                           Parameter Code                      |
210e4f5a11James Kremer * | --  |                                                                    |
211e4f5a11James Kremer * | 1   |                                                            (LSB)   |
212e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
213e4f5a11James Kremer * | 2   | DU     |  DS    |  TSD    | ETC   |         TMC     |  LBIN  | LP  |
214e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
2155cf8276Todd McKenney * | 3   |                          Parameter Length(n-3)                     |
216e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
217e4f5a11James Kremer * | 4   |                           Parameter Values                         |
218e4f5a11James Kremer * | --  |                                                                    |
219e4f5a11James Kremer * | n	 |                                                                    |
220e4f5a11James Kremer * |-----+--------------------------------------------------------------------|
221e4f5a11James Kremer */
222e4f5a11James Kremer
223e4f5a11James Kremerstatic int
2245cf8276Todd McKenneysave_logs(unsigned char *resp, ses_log_call_t *data)
225e4f5a11James Kremer{
2265cf8276Todd McKenney	int k;
2275cf8276Todd McKenney	int param_code; 	/* Parameter code */
2285cf8276Todd McKenney	int param_len = 0; 	/* Paramter length */
2295cf8276Todd McKenney	unsigned char *log_param_ptr; 	/* Log parameter pointer */
230e4f5a11James Kremer	unsigned char *log_str_ptr; /* ptr to ascii str returend by expander */
231e4f5a11James Kremer
2325cf8276Todd McKenney	char log_code[ENTRY_MAX_SIZE];
2335cf8276Todd McKenney	char log_level[ENTRY_MAX_SIZE];
234e4f5a11James Kremer	nvlist_t *entry;
235e4f5a11James Kremer	char entry_num[15];
236e4f5a11James Kremer	int match_found = 0;
2375cf8276Todd McKenney	char save_buffer[MAX_LOG_ENTRY_SZ];
238e4f5a11James Kremer	char entry_added = 0;
2395cf8276Todd McKenney	int all_log_data_len;
240e4f5a11James Kremer
2415cf8276Todd McKenney	/*
2425cf8276Todd McKenney	 * Bytes 2 and 3 of response buffer contain the page length of
2435cf8276Todd McKenney	 * the log entries returned.
2445cf8276Todd McKenney	 */
2455cf8276Todd McKenney	all_log_data_len = SCSI_READ16(&resp[2]);
246e4f5a11James Kremer
2475cf8276Todd McKenney	/*
2485cf8276Todd McKenney	 * Initialize log parameter pointer to point to first log entry.
2495cf8276Todd McKenney	 * The resp includes 4 bytes of header info and then log entries
2505cf8276Todd McKenney	 */
2515cf8276Todd McKenney	log_param_ptr = &resp[0] + 4;
252e4f5a11James Kremer
2535cf8276Todd McKenney	/*
2545cf8276Todd McKenney	 * If multiple heads are reading the logs, it is possible that we
2555cf8276Todd McKenney	 * could be re-reading some of the same log entries plus some
2565cf8276Todd McKenney	 * new additional entries. Check to see if any entries in this read
2575cf8276Todd McKenney	 * contain the same log entry as the last entry we read last time.
2585cf8276Todd McKenney	 */
2595cf8276Todd McKenney	if (data->last_log_entry != NULL &&
2605cf8276Todd McKenney	    (strlen(data->last_log_entry) == SES_LOG_VALID_LOG_SIZE)) {
2615cf8276Todd McKenney		/*
2625cf8276Todd McKenney		 * We have a valid log entry from a previous read log
2635cf8276Todd McKenney		 * operation.
2645cf8276Todd McKenney		 */
265e4f5a11James Kremer
266e4f5a11James Kremer
267e4f5a11James Kremer		/*
2685cf8276Todd McKenney		 * Start walking each log entry in response buffer looking for
269e4f5a11James Kremer		 * a duplicate entry.
270e4f5a11James Kremer		 */
2715cf8276Todd McKenney		for (k = 0; k < all_log_data_len; k += param_len) {
2725cf8276Todd McKenney			/*
2735cf8276Todd McKenney			 * Calculate log entry length
2745cf8276Todd McKenney			 * Log param ptr [3] contains the log length minus the
2755cf8276Todd McKenney			 * header info which is 4 bytes so add that in.
2765cf8276Todd McKenney			 */
2775cf8276Todd McKenney			param_len = log_param_ptr[3] + 4;
2785cf8276Todd McKenney
2795cf8276Todd McKenney			if (param_len <= 4) {
280e4f5a11James Kremer				/*
2815cf8276Todd McKenney				 * Only header information in this entry
2825cf8276Todd McKenney				 * process next log entry
283e4f5a11James Kremer				 */
2845cf8276Todd McKenney				log_param_ptr += param_len;
2855cf8276Todd McKenney				continue;
286e4f5a11James Kremer			}
2875cf8276Todd McKenney
2885cf8276Todd McKenney
2895cf8276Todd McKenney			/*
2905cf8276Todd McKenney			 * initialize log_str_ptr to point to string info
2915cf8276Todd McKenney			 * returned by expander
2925cf8276Todd McKenney			 * first 4 bytes of log parameter contains
2935cf8276Todd McKenney			 * 2 bytes of parameter code, 1 byte of Control data
2945cf8276Todd McKenney			 * and 1 byte for parameter length. Log string begins
2955cf8276Todd McKenney			 * after that so add 4 to log param ptr.
2965cf8276Todd McKenney			 */
2975cf8276Todd McKenney			log_str_ptr = log_param_ptr + 4;
2985cf8276Todd McKenney
299e4f5a11James Kremer			/*
3005cf8276Todd McKenney			 * Check to see if this is the
3015cf8276Todd McKenney			 * same line
302e4f5a11James Kremer			 */
3035cf8276Todd McKenney			if (strncmp((char *)log_str_ptr, data->last_log_entry,
3045cf8276Todd McKenney			    SES_LOG_VALID_LOG_SIZE) == 0) {
3055cf8276Todd McKenney				/* Found an exact match */
3065cf8276Todd McKenney				log_param_ptr += param_len;
3075cf8276Todd McKenney				k += param_len;
3085cf8276Todd McKenney				match_found = 1;
3095cf8276Todd McKenney				break;
310e4f5a11James Kremer			}
3115cf8276Todd McKenney			log_param_ptr += param_len;
312e4f5a11James Kremer		}
313e4f5a11James Kremer	}
314e4f5a11James Kremer	if (!match_found) {
3155cf8276Todd McKenney		log_param_ptr = &resp[0] + 4;
3165cf8276Todd McKenney		k = 0;
3175cf8276Todd McKenney	}
3185cf8276Todd McKenney	if (k == all_log_data_len) {
3195cf8276Todd McKenney		/*
3205cf8276Todd McKenney		 * Either there was no log data or we have
3215cf8276Todd McKenney		 * already read these log entries.
3225cf8276Todd McKenney		 * Just return.
3235cf8276Todd McKenney		 */
3245cf8276Todd McKenney		return (0);
3255cf8276Todd McKenney	}
3265cf8276Todd McKenney
3275cf8276Todd McKenney	/* Grab memory to return logs with */
3285cf8276Todd McKenney	if (nvlist_alloc(&data->log_data, NV_UNIQUE_NAME, 0) != 0) {
3295cf8276Todd McKenney		/* Couldn't alloc memory for nvlist */
3305cf8276Todd McKenney		return (SES_LOG_FAILED_NVLIST_CREATE);
331e4f5a11James Kremer	}
332e4f5a11James Kremer
3335cf8276Todd McKenney	(void) memset(log_code,		0, sizeof (log_code));
3345cf8276Todd McKenney	(void) memset(save_buffer,	0, sizeof (save_buffer));
3355cf8276Todd McKenney	(void) memset(log_level,	0, sizeof (log_level));
3365cf8276Todd McKenney
3375cf8276Todd McKenney	/*
3385cf8276Todd McKenney	 * Start saving new log entries
3395cf8276Todd McKenney	 * Walk the log data adding any new entries
3405cf8276Todd McKenney	 */
3415cf8276Todd McKenney
3425cf8276Todd McKenney	for (; k < all_log_data_len; k += param_len) {
3435cf8276Todd McKenney		/*
3445cf8276Todd McKenney		 * Calculate log entry length
3455cf8276Todd McKenney		 * Log ptr [3] contains the log length minus the header info
3465cf8276Todd McKenney		 * which is 4 bytes so add that in
3475cf8276Todd McKenney		 */
3485cf8276Todd McKenney		param_len = log_param_ptr[3] + 4;
3495cf8276Todd McKenney
3505cf8276Todd McKenney		if (param_len <= 4) {
3515cf8276Todd McKenney			/* Only header information in this entry */
3525cf8276Todd McKenney			/* process next log entry */
3535cf8276Todd McKenney			log_param_ptr += param_len;
3545cf8276Todd McKenney			continue;
355e4f5a11James Kremer		}
3565cf8276Todd McKenney
357e4f5a11James Kremer		/*
3585cf8276Todd McKenney		 * initialize log_str_ptr to point to string info of the log
3595cf8276Todd McKenney		 * entry. First 4 bytes of log entry contains param code,
3605cf8276Todd McKenney		 * control byte, and length. Log string starts after that.
361e4f5a11James Kremer		 */
3625cf8276Todd McKenney		log_str_ptr = log_param_ptr + 4;
363e4f5a11James Kremer
364e4f5a11James Kremer		/*
365e4f5a11James Kremer		 * Format of log str is as follows
366e4f5a11James Kremer		 * "%8x %8x %8x %8x %8x %8x %8x %8x",
367e4f5a11James Kremer		 * log_entry.log_word0, log_entry.ts_u, log_entry.ts_l,
368e4f5a11James Kremer		 * log_entry.seq_num, log_entry.log_code, log_entry.log_word2,
369e4f5a11James Kremer		 * log_entry.log_word3, log_entry.log_word4
370e4f5a11James Kremer		 * following example has extra spaces removed to fit in 80 char
371e4f5a11James Kremer		 * 40004 0 42d5f5fe 185b 630002 fd0800 50800207 e482813
372e4f5a11James Kremer		 */
373e4f5a11James Kremer
3745cf8276Todd McKenney		(void) strncpy(save_buffer,
3755cf8276Todd McKenney		    (const char *)log_str_ptr,
3765cf8276Todd McKenney		    SES_LOG_VALID_LOG_SIZE);
377e4f5a11James Kremer
3785cf8276Todd McKenney		(void) strncpy(log_code,
3795cf8276Todd McKenney		    (const char *)log_str_ptr+SES_LOG_CODE_START,
3805cf8276Todd McKenney		    SES_LOG_SPECIFIC_ENTRY_SIZE);
3815cf8276Todd McKenney
3825cf8276Todd McKenney		(void) strncpy(log_level,
3835cf8276Todd McKenney		    (const char *) log_str_ptr +
3845cf8276Todd McKenney		    SES_LOG_LEVEL_START, 1);
3855cf8276Todd McKenney
3865cf8276Todd McKenney
3875cf8276Todd McKenney		/* Add this entry to the nvlist log data */
3885cf8276Todd McKenney		if (nvlist_alloc(&entry, NV_UNIQUE_NAME, 0) != 0) {
3895cf8276Todd McKenney			/* Couldn't alloc space, return error */
3905cf8276Todd McKenney			return (SES_LOG_FAILED_NV_UNIQUE);
3915cf8276Todd McKenney		}
3925cf8276Todd McKenney
3935cf8276Todd McKenney
3945cf8276Todd McKenney		if (nvlist_add_string(entry, ENTRY_LOG, save_buffer) != 0) {
3955cf8276Todd McKenney			/* Error adding string, return error */
3965cf8276Todd McKenney			nvlist_free(entry);
3975cf8276Todd McKenney			return (SES_LOG_FAILED_NV_LOG);
398e4f5a11James Kremer		}
3995cf8276Todd McKenney
4005cf8276Todd McKenney		if (nvlist_add_string(entry, ENTRY_CODE, log_code) != 0) {
4015cf8276Todd McKenney			/* Error adding string, return error */
4025cf8276Todd McKenney			nvlist_free(entry);
4035cf8276Todd McKenney			return (SES_LOG_FAILED_NV_CODE);
4045cf8276Todd McKenney		}
4055cf8276Todd McKenney		if (nvlist_add_string(entry, ENTRY_SEVERITY, log_level) != 0) {
4065cf8276Todd McKenney			/* Error adding srtring, return error */
4075cf8276Todd McKenney			nvlist_free(entry);
4085cf8276Todd McKenney			return (SES_LOG_FAILED_NV_SEV);
4095cf8276Todd McKenney		}
4105cf8276Todd McKenney
4115cf8276Todd McKenney		param_code = SCSI_READ16(&log_param_ptr[0]);
4125cf8276Todd McKenney
4135cf8276Todd McKenney		(void) snprintf(entry_num, sizeof (entry_num),
4145cf8276Todd McKenney		    "%s%d", ENTRY_PREFIX, param_code);
4155cf8276Todd McKenney
4165cf8276Todd McKenney		if (nvlist_add_nvlist(data->log_data, entry_num, entry) != 0) {
4175cf8276Todd McKenney			/* Error adding nvlist, return error */
4185cf8276Todd McKenney			nvlist_free(entry);
4195cf8276Todd McKenney			return (SES_LOG_FAILED_NV_ENTRY);
4205cf8276Todd McKenney		}
4215cf8276Todd McKenney		nvlist_free(entry);
4225cf8276Todd McKenney
4235cf8276Todd McKenney		entry_added = 1;
4245cf8276Todd McKenney		(data->number_log_entries)++;
4255cf8276Todd McKenney
4265cf8276Todd McKenney		log_param_ptr += param_len;
427e4f5a11James Kremer
428e4f5a11James Kremer	}
429e4f5a11James Kremer	if (entry_added) {
430e4f5a11James Kremer		/* Update the last log entry string with last one read */
4315cf8276Todd McKenney		(void) strncpy(data->last_log_entry, save_buffer, MAXNAMELEN);
432e4f5a11James Kremer	}
433e4f5a11James Kremer	return (0);
434e4f5a11James Kremer}
435e4f5a11James Kremer
436e4f5a11James Kremer
437e4f5a11James Kremer
438e4f5a11James Kremer/* Setup struct to send command to device */
439e4f5a11James Kremerstatic void
440e4f5a11James Kremerset_scsi_pt_data_out(struct uscsi_cmd *uscsi, const unsigned char *dxferp,
441e4f5a11James Kremer    int dxfer_len)
442e4f5a11James Kremer{
443e4f5a11James Kremer	if (dxfer_len > 0) {
444e4f5a11James Kremer		uscsi->uscsi_bufaddr = (char *)dxferp;
445e4f5a11James Kremer		uscsi->uscsi_buflen = dxfer_len;
446e4f5a11James Kremer		uscsi->uscsi_flags = USCSI_WRITE | USCSI_ISOLATE |
447e4f5a11James Kremer		    USCSI_RQENABLE;
448e4f5a11James Kremer	}
449e4f5a11James Kremer}
450e4f5a11James Kremer
451e4f5a11James Kremer/*
452e4f5a11James Kremer * Invokes a SCSI MODE SENSE(10) command.
453e4f5a11James Kremer * Return:
454e4f5a11James Kremer * 0 for success
455e4f5a11James Kremer * SG_LIB_CAT_INVALID_OP -> invalid opcode
456e4f5a11James Kremer * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb
457e4f5a11James Kremer * SG_LIB_CAT_NOT_READY -> device not ready
458e4f5a11James Kremer * -1 -> other failure
459e4f5a11James Kremer */
460e4f5a11James Kremer
461e4f5a11James Kremerstatic int
462e4f5a11James Kremersg_ll_mode_sense10(int sg_fd, void * resp, int mx_resp_len)
463e4f5a11James Kremer{
464e4f5a11James Kremer	int res, ret;
465e4f5a11James Kremer	unsigned char modesCmdBlk[MODE_SENSE10_CMDLEN] =
466e4f5a11James Kremer	    {SCMD_MODE_SENSE_G1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
467e4f5a11James Kremer	unsigned char sense_b[SENSE_BUFF_LEN];
468e4f5a11James Kremer	struct uscsi_cmd uscsi;
469e4f5a11James Kremer
470e4f5a11James Kremer	modesCmdBlk[1] = 0;
471e4f5a11James Kremer	modesCmdBlk[2] = 0; /* page code 0 vendor specific */
472e4f5a11James Kremer	modesCmdBlk[3] = 0;
473e4f5a11James Kremer	modesCmdBlk[7] = (unsigned char) ((mx_resp_len >> 8) & 0xff);
474e4f5a11James Kremer	modesCmdBlk[8] = (unsigned char) (mx_resp_len & 0xff);
475e4f5a11James Kremer
476e4f5a11James Kremer	construct_scsi_pt_obj(&uscsi);
477e4f5a11James Kremer	set_scsi_pt_cdb(&uscsi, modesCmdBlk, sizeof (modesCmdBlk));
478e4f5a11James Kremer	set_scsi_pt_sense(&uscsi, sense_b, sizeof (sense_b));
479e4f5a11James Kremer	set_scsi_pt_data_in(&uscsi, (unsigned char *) resp, mx_resp_len);
480e4f5a11James Kremer	res = do_scsi_pt(&uscsi, sg_fd, DEF_PT_TIMEOUT);
481e4f5a11James Kremer	if (res) {
482e4f5a11James Kremer		ret = res;
483e4f5a11James Kremer	} else {
484e4f5a11James Kremer		ret = uscsi.uscsi_status;
485e4f5a11James Kremer	}
486e4f5a11James Kremer	return (ret);
487e4f5a11James Kremer}
488e4f5a11James Kremer
489e4f5a11James Kremer/*
490e4f5a11James Kremer * Invokes a SCSI MODE SELECT(10) command.
491e4f5a11James Kremer * Return:
492e4f5a11James Kremer * 0 for success.
493e4f5a11James Kremer * SG_LIB_CAT_INVALID_OP for invalid opcode
494e4f5a11James Kremer * SG_LIB_CAT_ILLEGAL_REQ -> bad field in cdb,
495e4f5a11James Kremer * SG_LIB_CAT_NOT_READY -> device not ready,
496e4f5a11James Kremer * -1 -> other failure
497e4f5a11James Kremer */
498e4f5a11James Kremerstatic int
499e4f5a11James Kremersg_ll_mode_select10(int sg_fd, void * paramp, int param_len)
500e4f5a11James Kremer{
501e4f5a11James Kremer	int res, ret;
502e4f5a11James Kremer	unsigned char modesCmdBlk[MODE_SELECT10_CMDLEN] =
503e4f5a11James Kremer	    {SCMD_MODE_SELECT_G1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
504e4f5a11James Kremer	unsigned char sense_b[SENSE_BUFF_LEN];
505e4f5a11James Kremer	struct uscsi_cmd uscsi;
506e4f5a11James Kremer
507e4f5a11James Kremer
508e4f5a11James Kremer	modesCmdBlk[1] = 0;
509e4f5a11James Kremer	/*
5105cf8276Todd McKenney	 * modesCmdBlk 2 equal 0 PC 0 return current page code 0 return
511e4f5a11James Kremer	 * vendor specific
512e4f5a11James Kremer	 */
513e4f5a11James Kremer
514e4f5a11James Kremer	modesCmdBlk[7] = (unsigned char)((param_len >> 8) & 0xff);
515e4f5a11James Kremer	modesCmdBlk[8] = (unsigned char)(param_len & 0xff);
516e4f5a11James Kremer
517e4f5a11James Kremer	construct_scsi_pt_obj(&uscsi);
518e4f5a11James Kremer
519e4f5a11James Kremer	set_scsi_pt_cdb(&uscsi, modesCmdBlk, sizeof (modesCmdBlk));
520e4f5a11James Kremer	set_scsi_pt_sense(&uscsi, sense_b, sizeof (sense_b));
521e4f5a11James Kremer	set_scsi_pt_data_out(&uscsi, (unsigned char *) paramp, param_len);
522e4f5a11James Kremer	res = do_scsi_pt(&uscsi, sg_fd, DEF_PT_TIMEOUT);
523e4f5a11James Kremer	if (res) {
524e4f5a11James Kremer		ret = res;
525e4f5a11James Kremer	} else {
526e4f5a11James Kremer		ret = uscsi.uscsi_status;
527e4f5a11James Kremer	}
528e4f5a11James Kremer	return (ret);
529e4f5a11James Kremer}
530e4f5a11James Kremer
531e4f5a11James Kremer
532e4f5a11James Kremer
533e4f5a11James Kremer/*
534e4f5a11James Kremer * MODE SENSE 10 commands yield a response that has block descriptors followed
535e4f5a11James Kremer * by mode pages. In most cases users are interested in the first mode page.
536e4f5a11James Kremer * This function returns the(byte) offset of the start of the first mode page.
537e4f5a11James Kremer * Returns >= 0 is successful or -1 if failure. If there is a failure
538e4f5a11James Kremer * a message is written to err_buff.
539e4f5a11James Kremer */
540e4f5a11James Kremer
541e4f5a11James Kremer/*
542e4f5a11James Kremer * return data looks like:
543e4f5a11James Kremer * Table 92 - Mode parameter header(10)
544e4f5a11James Kremer * Bit
545e4f5a11James Kremer * Byte
546e4f5a11James Kremer *	7 	6 	5 	4 	3 	2 	1 	0
547e4f5a11James Kremer *	----------------------------------------------------------
548e4f5a11James Kremer * 0	MSB Data length
549e4f5a11James Kremer * 1	LSB Data length
550e4f5a11James Kremer *	----------------------------------------------------------
551e4f5a11James Kremer * 2	Medium type
552e4f5a11James Kremer *	----------------------------------------------------------
553e4f5a11James Kremer * 3 	Device-specific parameter
554e4f5a11James Kremer *	----------------------------------------------------------
555e4f5a11James Kremer * 4 	Reserved
556e4f5a11James Kremer *	----------------------------------------------------------
557e4f5a11James Kremer * 5	Reserved
558e4f5a11James Kremer *	----------------------------------------------------------
559e4f5a11James Kremer * 6	MSB block descriptor length
560e4f5a11James Kremer * 7	LSB block descriptor length
561e4f5a11James Kremer *	----------------------------------------------------------
562e4f5a11James Kremer *	block desciptors....
563e4f5a11James Kremer *	-----------------------
564e4f5a11James Kremer *	mode sense page:
565e4f5a11James Kremer *	0 : ps Reserved : page Code
566e4f5a11James Kremer *	1 : Page Length(n-1)
567e4f5a11James Kremer *	2-N  Mode parameters
568e4f5a11James Kremer */
569e4f5a11James Kremerstatic int
5705cf8276Todd McKenneysg_mode_page_offset(const unsigned char *resp, int resp_len)
571e4f5a11James Kremer{
572e4f5a11James Kremer	int bd_len;
573e4f5a11James Kremer	int calc_len;
574e4f5a11James Kremer	int offset;
575e4f5a11James Kremer
576e4f5a11James Kremer	if ((NULL == resp) || (resp_len < 8)) {
577e4f5a11James Kremer		/* Too short of a response buffer */
578e4f5a11James Kremer		return (-1);
579e4f5a11James Kremer	}
580e4f5a11James Kremer
581e4f5a11James Kremer	calc_len = (resp[0] << 8) + resp[1] + 2;
582e4f5a11James Kremer	bd_len = (resp[6] << 8) + resp[7];
583e4f5a11James Kremer
584e4f5a11James Kremer	/* LongLBA doesn't change this calculation */
585e4f5a11James Kremer	offset = bd_len + MODE10_RESP_HDR_LEN;
586e4f5a11James Kremer
587e4f5a11James Kremer	if ((offset + 2) > resp_len) {
5885cf8276Todd McKenney		/* Given response length to small */
589e4f5a11James Kremer		offset = -1;
590e4f5a11James Kremer	} else if ((offset + 2) > calc_len) {
5915cf8276Todd McKenney		/* Calculated response length too small */
592e4f5a11James Kremer		offset = -1;
593e4f5a11James Kremer	}
594e4f5a11James Kremer	return (offset);
595e4f5a11James Kremer}
596e4f5a11James Kremer
597e4f5a11James Kremer/*
598e4f5a11James Kremer * Clear logs
599e4f5a11James Kremer */
600e4f5a11James Kremerstatic int
6015cf8276Todd McKenneyclear_log(int sg_fd, ses_log_call_t *data)
602e4f5a11James Kremer{
603e4f5a11James Kremer
604e4f5a11James Kremer	int res, alloc_len, off;
605e4f5a11James Kremer	int md_len;
606e4f5a11James Kremer	int read_in_len = 0;
6075cf8276Todd McKenney	unsigned char ref_md[MAX_ALLOC_LEN];
608e4f5a11James Kremer	struct log_clear_control_struct clear_data;
609e4f5a11James Kremer	long myhostid;
610e4f5a11James Kremer	int error = 0;
6115cf8276Todd McKenney	long poll_time;
6125cf8276Todd McKenney	char seq_num_str[10];
6135cf8276Todd McKenney	unsigned long seq_num = 0;
614e4f5a11James Kremer
615e4f5a11James Kremer	(void) memset(&clear_data, 0, sizeof (clear_data));
616e4f5a11James Kremer
617e4f5a11James Kremer	clear_data.pageControls = 0x40;
618e4f5a11James Kremer	clear_data.subpage_code = 0;
619e4f5a11James Kremer	clear_data.page_lengthLower = 0x16;
620e4f5a11James Kremer
621e4f5a11James Kremer	myhostid = gethostid();
622e4f5a11James Kremer	/* 0 -> 11 are memset to 0 */
623e4f5a11James Kremer	clear_data.host_id[12] = (myhostid & 0xff000000) >> 24;
624e4f5a11James Kremer	clear_data.host_id[13] = (myhostid & 0xff0000) >> 16;
625e4f5a11James Kremer	clear_data.host_id[14] = (myhostid & 0xff00) >> 8;
626e4f5a11James Kremer	clear_data.host_id[15] = myhostid & 0xff;
627e4f5a11James Kremer
6285cf8276Todd McKenney	/*
6295cf8276Todd McKenney	 * convert nanosecond time to seconds
6305cf8276Todd McKenney	 */
6315cf8276Todd McKenney	poll_time = data->poll_time / 1000000000;
6325cf8276Todd McKenney	/* Add 5 minutes to poll time to allow for data retrieval time */
633e4f5a11James Kremer	poll_time = poll_time + 300;
634e4f5a11James Kremer	clear_data.timeout[0] = (poll_time & 0xff00) >> 8;
635e4f5a11James Kremer	clear_data.timeout[1] = poll_time & 0xff;
636e4f5a11James Kremer
6375cf8276Todd McKenney	/*
6385cf8276Todd McKenney	 * retrieve the last read sequence number from the last
6395cf8276Todd McKenney	 * log entry read.
6405cf8276Todd McKenney	 */
6415cf8276Todd McKenney	if (data->last_log_entry != NULL &&
6425cf8276Todd McKenney	    (strlen(data->last_log_entry) == SES_LOG_VALID_LOG_SIZE)) {
6435cf8276Todd McKenney		/*
6445cf8276Todd McKenney		 * We have a valid log entry from a previous read log
6455cf8276Todd McKenney		 * operation.
6465cf8276Todd McKenney		 */
6475cf8276Todd McKenney		(void) strncpy(seq_num_str,
6485cf8276Todd McKenney		    (const char *) data->last_log_entry +
6495cf8276Todd McKenney		    SES_LOG_SEQ_NUM_START, 8);
6505cf8276Todd McKenney		seq_num = strtoul(seq_num_str, 0, 16);
6515cf8276Todd McKenney	}
652e4f5a11James Kremer	clear_data.seq_clear[0] = (seq_num & 0xff000000) >> 24;
653e4f5a11James Kremer	clear_data.seq_clear[1] = (seq_num & 0xff0000) >> 16;
654e4f5a11James Kremer	clear_data.seq_clear[2] = (seq_num & 0xff00) >> 8;
655e4f5a11James Kremer	clear_data.seq_clear[3] = (seq_num & 0xff);
656e4f5a11James Kremer
657e4f5a11James Kremer	read_in_len = sizeof (clear_data);
658e4f5a11James Kremer
659e4f5a11James Kremer
660e4f5a11James Kremer	/* do MODE SENSE to fetch current values */
6615cf8276Todd McKenney	(void) memset(ref_md, 0, MAX_ALLOC_LEN);
6625cf8276Todd McKenney	alloc_len = MAX_ALLOC_LEN;
663e4f5a11James Kremer
664e4f5a11James Kremer
665e4f5a11James Kremer	res = sg_ll_mode_sense10(sg_fd, ref_md, alloc_len);
666e4f5a11James Kremer	if (0 != res) {
667e4f5a11James Kremer		/* Error during mode sense */
668e4f5a11James Kremer		error = SES_LOG_FAILED_MODE_SENSE;
669e4f5a11James Kremer		return (error);
670e4f5a11James Kremer	}
671e4f5a11James Kremer
672e4f5a11James Kremer	/* Setup mode Select to clear logs */
6735cf8276Todd McKenney	off = sg_mode_page_offset(ref_md, alloc_len);
674e4f5a11James Kremer	if (off < 0) {
675e4f5a11James Kremer		/* Mode page offset error */
676e4f5a11James Kremer		error =  SES_LOG_FAILED_MODE_SENSE_OFFSET;
677e4f5a11James Kremer		return (error);
678e4f5a11James Kremer	}
679e4f5a11James Kremer	md_len = (ref_md[0] << 8) + ref_md[1] + 2;
680e4f5a11James Kremer
681e4f5a11James Kremer	ref_md[0] = 0;
682e4f5a11James Kremer	ref_md[1] = 0;
683e4f5a11James Kremer
684e4f5a11James Kremer	if (md_len > alloc_len) {
685e4f5a11James Kremer		/* Data length to large */
686e4f5a11James Kremer		error = SES_LOG_FAILED_BAD_DATA_LEN;
687e4f5a11James Kremer		return (error);
688e4f5a11James Kremer	}
689e4f5a11James Kremer
690e4f5a11James Kremer	if ((md_len - off) != read_in_len) {
691e4f5a11James Kremer		/* Content length not correct */
692e4f5a11James Kremer		error = SES_LOG_FAILED_BAD_CONTENT_LEN;
693e4f5a11James Kremer		return (error);
694e4f5a11James Kremer	}
695e4f5a11James Kremer
696e4f5a11James Kremer	if ((clear_data.pageControls & 0x40) != (ref_md[off] & 0x40)) {
697e4f5a11James Kremer		/* reference model doesn't have use subpage format bit set */
698e4f5a11James Kremer		/* Even though it should have */
699e4f5a11James Kremer		/* don't send the command */
7005cf8276Todd McKenney		error = SES_LOG_FAILED_FORMAT_PAGE_ERR;
701e4f5a11James Kremer		return (error);
702e4f5a11James Kremer	}
703e4f5a11James Kremer
7045cf8276Todd McKenney	(void) memcpy(ref_md + off, (const void *) &clear_data,
705e4f5a11James Kremer	    sizeof (clear_data));
706e4f5a11James Kremer
707e4f5a11James Kremer	res = sg_ll_mode_select10(sg_fd, ref_md, md_len);
708e4f5a11James Kremer	if (res != 0) {
709e4f5a11James Kremer		error = SES_LOG_FAILED_MODE_SELECT;
710e4f5a11James Kremer		return (error);
711e4f5a11James Kremer	}
712e4f5a11James Kremer
713e4f5a11James Kremer	return (error);
714e4f5a11James Kremer}
715e4f5a11James Kremer/*
716e4f5a11James Kremer * Gather data from given device.
717e4f5a11James Kremer */
718e4f5a11James Kremerstatic int
7195cf8276Todd McKenneygather_data(char *device_name, ses_log_call_t *data)
720e4f5a11James Kremer{
721e4f5a11James Kremer	int sg_fd;
7225cf8276Todd McKenney	int resp_len, res;
7235cf8276Todd McKenney	unsigned char rsp_buff[MAX_ALLOC_LEN];
724e4f5a11James Kremer	int error;
725e4f5a11James Kremer
726e4f5a11James Kremer	/* Open device */
727e4f5a11James Kremer	if ((sg_fd = open_device(device_name)) < 0) {
728e4f5a11James Kremer		/* Failed to open device */
729e4f5a11James Kremer		return (SES_LOG_FAILED_TO_OPEN_DEVICE);
730e4f5a11James Kremer	}
731e4f5a11James Kremer
732e4f5a11James Kremer	/* Read the logs */
733e4f5a11James Kremer	(void) memset(rsp_buff, 0, sizeof (rsp_buff));
734e4f5a11James Kremer	resp_len = 0x8000; /* Maximum size available to read */
735e4f5a11James Kremer	res = read_log(sg_fd, rsp_buff, resp_len);
736e4f5a11James Kremer
7375cf8276Todd McKenney	if (res != 0) {
738e4f5a11James Kremer		/* Some sort of Error during read of logs */
7395cf8276Todd McKenney		(void) close(sg_fd);
740e4f5a11James Kremer		return (SES_LOG_FAILED_TO_READ_DEVICE);
741e4f5a11James Kremer	}
742e4f5a11James Kremer
743e4f5a11James Kremer	/* Save the logs */
7445cf8276Todd McKenney	error = save_logs(rsp_buff, data);
745e4f5a11James Kremer	if (error != 0) {
7465cf8276Todd McKenney		(void) close(sg_fd);
747e4f5a11James Kremer		return (error);
748e4f5a11James Kremer	}
7495cf8276Todd McKenney	/* Clear the logs */
7505cf8276Todd McKenney	error = clear_log(sg_fd, data);
751e4f5a11James Kremer
752e4f5a11James Kremer	(void) close(sg_fd);
753e4f5a11James Kremer
754e4f5a11James Kremer	return (error);
755e4f5a11James Kremer}
756e4f5a11James Kremer
757e4f5a11James Kremer/*
758e4f5a11James Kremer * Access the SES target identified by the indicated path.  Read the logs
759e4f5a11James Kremer * and return them in a nvlist.
760e4f5a11James Kremer */
761e4f5a11James Kremerint
7625cf8276Todd McKenneyaccess_ses_log(ses_log_call_t *data)
763e4f5a11James Kremer{
764e4f5a11James Kremer	char real_path[MAXPATHLEN];
765e4f5a11James Kremer	struct stat buffer;
766e4f5a11James Kremer	int error;
767e4f5a11James Kremer
7685cf8276Todd McKenney	/* Initialize return data */
7695cf8276Todd McKenney	data->log_data = NULL;
7705cf8276Todd McKenney	data->number_log_entries = 0;
7715cf8276Todd McKenney
772e4f5a11James Kremer	if (data->target_path == NULL) {
773e4f5a11James Kremer		/* NULL Target path, return error */
774e4f5a11James Kremer		return (SES_LOG_FAILED_NULL_TARGET_PATH);
775e4f5a11James Kremer	}
776e4f5a11James Kremer
777e4f5a11James Kremer	/* Try to find a valid path */
778e4f5a11James Kremer	(void) snprintf(real_path, sizeof (real_path), "/devices%s:ses",
779e4f5a11James Kremer	    data->target_path);
780e4f5a11James Kremer
781e4f5a11James Kremer	if (stat(real_path, &buffer) != 0) {
782e4f5a11James Kremer
783e4f5a11James Kremer		(void) snprintf(real_path, sizeof (real_path), "/devices%s:0",
784e4f5a11James Kremer		    data->target_path);
785e4f5a11James Kremer		if (stat(real_path, &buffer) != 0) {
786e4f5a11James Kremer			/* Couldn't find a path that exists */
787e4f5a11James Kremer			return (SES_LOG_FAILED_BAD_TARGET_PATH);
788e4f5a11James Kremer		}
789e4f5a11James Kremer	}
790e4f5a11James Kremer
7915cf8276Todd McKenney	error = gather_data(real_path, data);
792e4f5a11James Kremer
793e4f5a11James Kremer	/* Update the size of log entries being returned */
794e4f5a11James Kremer	data->size_of_log_entries =
795e4f5a11James Kremer	    data->number_log_entries * SES_LOG_VALID_LOG_SIZE;
796e4f5a11James Kremer
797e4f5a11James Kremer	return (error);
798e4f5a11James Kremer}
799