1/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c)  2003-2013 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7
8#include "ql4_def.h"
9#include "ql4_glbl.h"
10#include "ql4_dbg.h"
11
12static ssize_t
13qla4_8xxx_sysfs_read_fw_dump(struct file *filep, struct kobject *kobj,
14			     struct bin_attribute *ba, char *buf, loff_t off,
15			     size_t count)
16{
17	struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
18					       struct device, kobj)));
19
20	if (is_qla40XX(ha))
21		return -EINVAL;
22
23	if (!test_bit(AF_82XX_DUMP_READING, &ha->flags))
24		return 0;
25
26	return memory_read_from_buffer(buf, count, &off, ha->fw_dump,
27				       ha->fw_dump_size);
28}
29
30static ssize_t
31qla4_8xxx_sysfs_write_fw_dump(struct file *filep, struct kobject *kobj,
32			      struct bin_attribute *ba, char *buf, loff_t off,
33			      size_t count)
34{
35	struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
36					       struct device, kobj)));
37	uint32_t dev_state;
38	long reading;
39	int ret = 0;
40
41	if (is_qla40XX(ha))
42		return -EINVAL;
43
44	if (off != 0)
45		return ret;
46
47	buf[1] = 0;
48	ret = kstrtol(buf, 10, &reading);
49	if (ret) {
50		ql4_printk(KERN_ERR, ha, "%s: Invalid input. Return err %d\n",
51			   __func__, ret);
52		return ret;
53	}
54
55	switch (reading) {
56	case 0:
57		/* clear dump collection flags */
58		if (test_and_clear_bit(AF_82XX_DUMP_READING, &ha->flags)) {
59			clear_bit(AF_82XX_FW_DUMPED, &ha->flags);
60			/* Reload minidump template */
61			qla4xxx_alloc_fw_dump(ha);
62			DEBUG2(ql4_printk(KERN_INFO, ha,
63					  "Firmware template reloaded\n"));
64		}
65		break;
66	case 1:
67		/* Set flag to read dump */
68		if (test_bit(AF_82XX_FW_DUMPED, &ha->flags) &&
69		    !test_bit(AF_82XX_DUMP_READING, &ha->flags)) {
70			set_bit(AF_82XX_DUMP_READING, &ha->flags);
71			DEBUG2(ql4_printk(KERN_INFO, ha,
72					  "Raw firmware dump ready for read on (%ld).\n",
73					  ha->host_no));
74		}
75		break;
76	case 2:
77		/* Reset HBA and collect FW dump */
78		ha->isp_ops->idc_lock(ha);
79		dev_state = qla4_8xxx_rd_direct(ha, QLA8XXX_CRB_DEV_STATE);
80		if (dev_state == QLA8XXX_DEV_READY) {
81			ql4_printk(KERN_INFO, ha, "%s: Setting Need reset\n",
82				   __func__);
83			qla4_8xxx_wr_direct(ha, QLA8XXX_CRB_DEV_STATE,
84					    QLA8XXX_DEV_NEED_RESET);
85			if (is_qla8022(ha) ||
86			    ((is_qla8032(ha) || is_qla8042(ha)) &&
87			     qla4_83xx_can_perform_reset(ha))) {
88				set_bit(AF_8XXX_RST_OWNER, &ha->flags);
89				set_bit(AF_FW_RECOVERY, &ha->flags);
90				ql4_printk(KERN_INFO, ha, "%s: Reset owner is 0x%x\n",
91					   __func__, ha->func_num);
92			}
93		} else
94			ql4_printk(KERN_INFO, ha,
95				   "%s: Reset not performed as device state is 0x%x\n",
96				   __func__, dev_state);
97
98		ha->isp_ops->idc_unlock(ha);
99		break;
100	default:
101		/* do nothing */
102		break;
103	}
104
105	return count;
106}
107
108static struct bin_attribute sysfs_fw_dump_attr = {
109	.attr = {
110		.name = "fw_dump",
111		.mode = S_IRUSR | S_IWUSR,
112	},
113	.size = 0,
114	.read = qla4_8xxx_sysfs_read_fw_dump,
115	.write = qla4_8xxx_sysfs_write_fw_dump,
116};
117
118static struct sysfs_entry {
119	char *name;
120	struct bin_attribute *attr;
121} bin_file_entries[] = {
122	{ "fw_dump", &sysfs_fw_dump_attr },
123	{ NULL },
124};
125
126void qla4_8xxx_alloc_sysfs_attr(struct scsi_qla_host *ha)
127{
128	struct Scsi_Host *host = ha->host;
129	struct sysfs_entry *iter;
130	int ret;
131
132	for (iter = bin_file_entries; iter->name; iter++) {
133		ret = sysfs_create_bin_file(&host->shost_gendev.kobj,
134					    iter->attr);
135		if (ret)
136			ql4_printk(KERN_ERR, ha,
137				   "Unable to create sysfs %s binary attribute (%d).\n",
138				   iter->name, ret);
139	}
140}
141
142void qla4_8xxx_free_sysfs_attr(struct scsi_qla_host *ha)
143{
144	struct Scsi_Host *host = ha->host;
145	struct sysfs_entry *iter;
146
147	for (iter = bin_file_entries; iter->name; iter++)
148		sysfs_remove_bin_file(&host->shost_gendev.kobj,
149				      iter->attr);
150}
151
152/* Scsi_Host attributes. */
153static ssize_t
154qla4xxx_fw_version_show(struct device *dev,
155			struct device_attribute *attr, char *buf)
156{
157	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
158
159	if (is_qla80XX(ha))
160		return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d (%x)\n",
161				ha->fw_info.fw_major, ha->fw_info.fw_minor,
162				ha->fw_info.fw_patch, ha->fw_info.fw_build);
163	else
164		return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d.%02d\n",
165				ha->fw_info.fw_major, ha->fw_info.fw_minor,
166				ha->fw_info.fw_patch, ha->fw_info.fw_build);
167}
168
169static ssize_t
170qla4xxx_serial_num_show(struct device *dev, struct device_attribute *attr,
171			char *buf)
172{
173	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
174	return snprintf(buf, PAGE_SIZE, "%s\n", ha->serial_number);
175}
176
177static ssize_t
178qla4xxx_iscsi_version_show(struct device *dev, struct device_attribute *attr,
179			   char *buf)
180{
181	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
182	return snprintf(buf, PAGE_SIZE, "%d.%02d\n", ha->fw_info.iscsi_major,
183			ha->fw_info.iscsi_minor);
184}
185
186static ssize_t
187qla4xxx_optrom_version_show(struct device *dev, struct device_attribute *attr,
188			    char *buf)
189{
190	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
191	return snprintf(buf, PAGE_SIZE, "%d.%02d.%02d.%02d\n",
192			ha->fw_info.bootload_major, ha->fw_info.bootload_minor,
193			ha->fw_info.bootload_patch, ha->fw_info.bootload_build);
194}
195
196static ssize_t
197qla4xxx_board_id_show(struct device *dev, struct device_attribute *attr,
198		      char *buf)
199{
200	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
201	return snprintf(buf, PAGE_SIZE, "0x%08X\n", ha->board_id);
202}
203
204static ssize_t
205qla4xxx_fw_state_show(struct device *dev, struct device_attribute *attr,
206		      char *buf)
207{
208	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
209
210	qla4xxx_get_firmware_state(ha);
211	return snprintf(buf, PAGE_SIZE, "0x%08X%8X\n", ha->firmware_state,
212			ha->addl_fw_state);
213}
214
215static ssize_t
216qla4xxx_phy_port_cnt_show(struct device *dev, struct device_attribute *attr,
217		      char *buf)
218{
219	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
220
221	if (is_qla40XX(ha))
222		return -ENOSYS;
223
224	return snprintf(buf, PAGE_SIZE, "0x%04X\n", ha->phy_port_cnt);
225}
226
227static ssize_t
228qla4xxx_phy_port_num_show(struct device *dev, struct device_attribute *attr,
229		      char *buf)
230{
231	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
232
233	if (is_qla40XX(ha))
234		return -ENOSYS;
235
236	return snprintf(buf, PAGE_SIZE, "0x%04X\n", ha->phy_port_num);
237}
238
239static ssize_t
240qla4xxx_iscsi_func_cnt_show(struct device *dev, struct device_attribute *attr,
241		      char *buf)
242{
243	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
244
245	if (is_qla40XX(ha))
246		return -ENOSYS;
247
248	return snprintf(buf, PAGE_SIZE, "0x%04X\n", ha->iscsi_pci_func_cnt);
249}
250
251static ssize_t
252qla4xxx_hba_model_show(struct device *dev, struct device_attribute *attr,
253		       char *buf)
254{
255	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
256
257	return snprintf(buf, PAGE_SIZE, "%s\n", ha->model_name);
258}
259
260static ssize_t
261qla4xxx_fw_timestamp_show(struct device *dev, struct device_attribute *attr,
262			  char *buf)
263{
264	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
265	return snprintf(buf, PAGE_SIZE, "%s %s\n", ha->fw_info.fw_build_date,
266			ha->fw_info.fw_build_time);
267}
268
269static ssize_t
270qla4xxx_fw_build_user_show(struct device *dev, struct device_attribute *attr,
271			   char *buf)
272{
273	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
274	return snprintf(buf, PAGE_SIZE, "%s\n", ha->fw_info.fw_build_user);
275}
276
277static ssize_t
278qla4xxx_fw_ext_timestamp_show(struct device *dev, struct device_attribute *attr,
279			      char *buf)
280{
281	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
282	return snprintf(buf, PAGE_SIZE, "%s\n", ha->fw_info.extended_timestamp);
283}
284
285static ssize_t
286qla4xxx_fw_load_src_show(struct device *dev, struct device_attribute *attr,
287			 char *buf)
288{
289	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
290	char *load_src = NULL;
291
292	switch (ha->fw_info.fw_load_source) {
293	case 1:
294		load_src = "Flash Primary";
295		break;
296	case 2:
297		load_src = "Flash Secondary";
298		break;
299	case 3:
300		load_src = "Host Download";
301		break;
302	}
303
304	return snprintf(buf, PAGE_SIZE, "%s\n", load_src);
305}
306
307static ssize_t
308qla4xxx_fw_uptime_show(struct device *dev, struct device_attribute *attr,
309		       char *buf)
310{
311	struct scsi_qla_host *ha = to_qla_host(class_to_shost(dev));
312	qla4xxx_about_firmware(ha);
313	return snprintf(buf, PAGE_SIZE, "%u.%u secs\n", ha->fw_uptime_secs,
314			ha->fw_uptime_msecs);
315}
316
317static DEVICE_ATTR(fw_version, S_IRUGO, qla4xxx_fw_version_show, NULL);
318static DEVICE_ATTR(serial_num, S_IRUGO, qla4xxx_serial_num_show, NULL);
319static DEVICE_ATTR(iscsi_version, S_IRUGO, qla4xxx_iscsi_version_show, NULL);
320static DEVICE_ATTR(optrom_version, S_IRUGO, qla4xxx_optrom_version_show, NULL);
321static DEVICE_ATTR(board_id, S_IRUGO, qla4xxx_board_id_show, NULL);
322static DEVICE_ATTR(fw_state, S_IRUGO, qla4xxx_fw_state_show, NULL);
323static DEVICE_ATTR(phy_port_cnt, S_IRUGO, qla4xxx_phy_port_cnt_show, NULL);
324static DEVICE_ATTR(phy_port_num, S_IRUGO, qla4xxx_phy_port_num_show, NULL);
325static DEVICE_ATTR(iscsi_func_cnt, S_IRUGO, qla4xxx_iscsi_func_cnt_show, NULL);
326static DEVICE_ATTR(hba_model, S_IRUGO, qla4xxx_hba_model_show, NULL);
327static DEVICE_ATTR(fw_timestamp, S_IRUGO, qla4xxx_fw_timestamp_show, NULL);
328static DEVICE_ATTR(fw_build_user, S_IRUGO, qla4xxx_fw_build_user_show, NULL);
329static DEVICE_ATTR(fw_ext_timestamp, S_IRUGO, qla4xxx_fw_ext_timestamp_show,
330		   NULL);
331static DEVICE_ATTR(fw_load_src, S_IRUGO, qla4xxx_fw_load_src_show, NULL);
332static DEVICE_ATTR(fw_uptime, S_IRUGO, qla4xxx_fw_uptime_show, NULL);
333
334struct device_attribute *qla4xxx_host_attrs[] = {
335	&dev_attr_fw_version,
336	&dev_attr_serial_num,
337	&dev_attr_iscsi_version,
338	&dev_attr_optrom_version,
339	&dev_attr_board_id,
340	&dev_attr_fw_state,
341	&dev_attr_phy_port_cnt,
342	&dev_attr_phy_port_num,
343	&dev_attr_iscsi_func_cnt,
344	&dev_attr_hba_model,
345	&dev_attr_fw_timestamp,
346	&dev_attr_fw_build_user,
347	&dev_attr_fw_ext_timestamp,
348	&dev_attr_fw_load_src,
349	&dev_attr_fw_uptime,
350	NULL,
351};
352