blob: 30c381c3abcc508cbe5818522cec8c1197d59ea9 [file] [log] [blame]
8482e1182005-04-17 15:04:54 -05001/*
2 * QLOGIC LINUX SOFTWARE
3 *
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2005 QLogic Corporation
6 * (www.qlogic.com)
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
11 * later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 */
19#include "qla_def.h"
20
7aaef272005-04-17 16:32:42 -050021#include <linux/vmalloc.h>
8482e1182005-04-17 15:04:54 -050022#include <scsi/scsi_transport_fc.h>
23
24/* SYSFS attributes --------------------------------------------------------- */
25
26static ssize_t
27qla2x00_sysfs_read_fw_dump(struct kobject *kobj, char *buf, loff_t off,
28 size_t count)
29{
30 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
31 struct device, kobj)));
32
33 if (ha->fw_dump_reading == 0)
34 return 0;
35 if (off > ha->fw_dump_buffer_len)
36 return 0;
37 if (off + count > ha->fw_dump_buffer_len)
38 count = ha->fw_dump_buffer_len - off;
39
40 memcpy(buf, &ha->fw_dump_buffer[off], count);
41
42 return (count);
43}
44
45static ssize_t
46qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf, loff_t off,
47 size_t count)
48{
49 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
50 struct device, kobj)));
51 int reading;
52 uint32_t dump_size;
53
54 if (off != 0)
55 return (0);
56
57 reading = simple_strtol(buf, NULL, 10);
58 switch (reading) {
59 case 0:
60 if (ha->fw_dump_reading == 1) {
61 qla_printk(KERN_INFO, ha,
62 "Firmware dump cleared on (%ld).\n",
63 ha->host_no);
64
65 vfree(ha->fw_dump_buffer);
66 free_pages((unsigned long)ha->fw_dump,
67 ha->fw_dump_order);
68
69 ha->fw_dump_reading = 0;
70 ha->fw_dump_buffer = NULL;
71 ha->fw_dump = NULL;
72 }
73 break;
74 case 1:
75 if (ha->fw_dump != NULL && !ha->fw_dump_reading) {
76 ha->fw_dump_reading = 1;
77
78 dump_size = FW_DUMP_SIZE_1M;
79 if (ha->fw_memory_size < 0x20000)
80 dump_size = FW_DUMP_SIZE_128K;
81 else if (ha->fw_memory_size < 0x80000)
82 dump_size = FW_DUMP_SIZE_512K;
83 ha->fw_dump_buffer = (char *)vmalloc(dump_size);
84 if (ha->fw_dump_buffer == NULL) {
85 qla_printk(KERN_WARNING, ha,
86 "Unable to allocate memory for firmware "
87 "dump buffer (%d).\n", dump_size);
88
89 ha->fw_dump_reading = 0;
90 return (count);
91 }
92 qla_printk(KERN_INFO, ha,
93 "Firmware dump ready for read on (%ld).\n",
94 ha->host_no);
95 memset(ha->fw_dump_buffer, 0, dump_size);
Andrew Vasquezabbd8872005-07-06 10:30:05 -070096 ha->isp_ops.ascii_fw_dump(ha);
8482e1182005-04-17 15:04:54 -050097 ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
98 }
99 break;
100 }
101 return (count);
102}
103
104static struct bin_attribute sysfs_fw_dump_attr = {
105 .attr = {
106 .name = "fw_dump",
107 .mode = S_IRUSR | S_IWUSR,
108 .owner = THIS_MODULE,
109 },
110 .size = 0,
111 .read = qla2x00_sysfs_read_fw_dump,
112 .write = qla2x00_sysfs_write_fw_dump,
113};
114
115static ssize_t
116qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
117 size_t count)
118{
119 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
120 struct device, kobj)));
121 uint16_t *witer;
122 unsigned long flags;
123 uint16_t cnt;
124
125 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
126 return 0;
127
128 /* Read NVRAM. */
129 spin_lock_irqsave(&ha->hardware_lock, flags);
130 qla2x00_lock_nvram_access(ha);
131 witer = (uint16_t *)buf;
132 for (cnt = 0; cnt < count / 2; cnt++) {
133 *witer = cpu_to_le16(qla2x00_get_nvram_word(ha,
134 cnt+ha->nvram_base));
135 witer++;
136 }
137 qla2x00_unlock_nvram_access(ha);
138 spin_unlock_irqrestore(&ha->hardware_lock, flags);
139
140 return (count);
141}
142
143static ssize_t
144qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
145 size_t count)
146{
147 struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
148 struct device, kobj)));
149 uint8_t *iter;
150 uint16_t *witer;
151 unsigned long flags;
152 uint16_t cnt;
153 uint8_t chksum;
154
155 if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
156 return 0;
157
158 /* Checksum NVRAM. */
159 iter = (uint8_t *)buf;
160 chksum = 0;
161 for (cnt = 0; cnt < count - 1; cnt++)
162 chksum += *iter++;
163 chksum = ~chksum + 1;
164 *iter = chksum;
165
166 /* Write NVRAM. */
167 spin_lock_irqsave(&ha->hardware_lock, flags);
168 qla2x00_lock_nvram_access(ha);
169 qla2x00_release_nvram_protection(ha);
170 witer = (uint16_t *)buf;
171 for (cnt = 0; cnt < count / 2; cnt++) {
172 qla2x00_write_nvram_word(ha, cnt+ha->nvram_base,
173 cpu_to_le16(*witer));
174 witer++;
175 }
176 qla2x00_unlock_nvram_access(ha);
177 spin_unlock_irqrestore(&ha->hardware_lock, flags);
178
179 return (count);
180}
181
182static struct bin_attribute sysfs_nvram_attr = {
183 .attr = {
184 .name = "nvram",
185 .mode = S_IRUSR | S_IWUSR,
186 .owner = THIS_MODULE,
187 },
188 .size = sizeof(nvram_t),
189 .read = qla2x00_sysfs_read_nvram,
190 .write = qla2x00_sysfs_write_nvram,
191};
192
193void
194qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
195{
196 struct Scsi_Host *host = ha->host;
197
198 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
199 sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
200}
201
202void
203qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
204{
205 struct Scsi_Host *host = ha->host;
206
207 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
208 sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
209}
210
211/* Host attributes. */
212
213static void
214qla2x00_get_host_port_id(struct Scsi_Host *shost)
215{
216 scsi_qla_host_t *ha = to_qla_host(shost);
217
218 fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
219 ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
220}
221
222static void
223qla2x00_get_starget_node_name(struct scsi_target *starget)
224{
225 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
226 scsi_qla_host_t *ha = to_qla_host(host);
bdf79622005-04-17 15:06:53 -0500227 fc_port_t *fcport;
8482e1182005-04-17 15:04:54 -0500228 uint64_t node_name = 0;
229
bdf79622005-04-17 15:06:53 -0500230 list_for_each_entry(fcport, &ha->fcports, list) {
231 if (starget->id == fcport->os_target_id) {
232 node_name = *(uint64_t *)fcport->node_name;
233 break;
234 }
235 }
236
237 fc_starget_node_name(starget) = be64_to_cpu(node_name);
8482e1182005-04-17 15:04:54 -0500238}
239
240static void
241qla2x00_get_starget_port_name(struct scsi_target *starget)
242{
243 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
244 scsi_qla_host_t *ha = to_qla_host(host);
bdf79622005-04-17 15:06:53 -0500245 fc_port_t *fcport;
8482e1182005-04-17 15:04:54 -0500246 uint64_t port_name = 0;
247
bdf79622005-04-17 15:06:53 -0500248 list_for_each_entry(fcport, &ha->fcports, list) {
249 if (starget->id == fcport->os_target_id) {
250 port_name = *(uint64_t *)fcport->port_name;
251 break;
252 }
253 }
254
255 fc_starget_port_name(starget) = be64_to_cpu(port_name);
8482e1182005-04-17 15:04:54 -0500256}
257
258static void
259qla2x00_get_starget_port_id(struct scsi_target *starget)
260{
261 struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
262 scsi_qla_host_t *ha = to_qla_host(host);
bdf79622005-04-17 15:06:53 -0500263 fc_port_t *fcport;
264 uint32_t port_id = ~0U;
8482e1182005-04-17 15:04:54 -0500265
bdf79622005-04-17 15:06:53 -0500266 list_for_each_entry(fcport, &ha->fcports, list) {
267 if (starget->id == fcport->os_target_id) {
268 port_id = fcport->d_id.b.domain << 16 |
269 fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
270 break;
271 }
272 }
273
8482e1182005-04-17 15:04:54 -0500274 fc_starget_port_id(starget) = port_id;
275}
276
277static void
278qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
279{
bdf79622005-04-17 15:06:53 -0500280 struct Scsi_Host *host = rport_to_shost(rport);
281 scsi_qla_host_t *ha = to_qla_host(host);
8482e1182005-04-17 15:04:54 -0500282
283 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
284}
285
286static void
287qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
288{
bdf79622005-04-17 15:06:53 -0500289 struct Scsi_Host *host = rport_to_shost(rport);
290 scsi_qla_host_t *ha = to_qla_host(host);
8482e1182005-04-17 15:04:54 -0500291
292 if (timeout)
293 ha->port_down_retry_count = timeout;
294 else
295 ha->port_down_retry_count = 1;
296
297 rport->dev_loss_tmo = ha->port_down_retry_count + 5;
298}
299
Andrew Vasquez1c97a122005-04-21 16:13:36 -0400300struct fc_function_template qla2xxx_transport_functions = {
8482e1182005-04-17 15:04:54 -0500301
302 .show_host_node_name = 1,
303 .show_host_port_name = 1,
304 .get_host_port_id = qla2x00_get_host_port_id,
305 .show_host_port_id = 1,
306
bdf79622005-04-17 15:06:53 -0500307 .dd_fcrport_size = sizeof(struct fc_port *),
8482e1182005-04-17 15:04:54 -0500308
309 .get_starget_node_name = qla2x00_get_starget_node_name,
310 .show_starget_node_name = 1,
311 .get_starget_port_name = qla2x00_get_starget_port_name,
312 .show_starget_port_name = 1,
313 .get_starget_port_id = qla2x00_get_starget_port_id,
314 .show_starget_port_id = 1,
315
316 .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
317 .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
318 .show_rport_dev_loss_tmo = 1,
319
320};
321
8482e1182005-04-17 15:04:54 -0500322void
323qla2x00_init_host_attr(scsi_qla_host_t *ha)
324{
325 fc_host_node_name(ha->host) =
326 be64_to_cpu(*(uint64_t *)ha->init_cb->node_name);
327 fc_host_port_name(ha->host) =
328 be64_to_cpu(*(uint64_t *)ha->init_cb->port_name);
329}