Merge with master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / drivers / scsi / qla2xxx / qla_attr.c
1 /*
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
21 #include <linux/vmalloc.h>
22 #include <scsi/scsi_transport_fc.h>
23
24 /* SYSFS attributes --------------------------------------------------------- */
25
26 static ssize_t
27 qla2x00_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
45 static ssize_t
46 qla2x00_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);
96                         if (IS_QLA2100(ha) || IS_QLA2200(ha))
97                                 qla2100_ascii_fw_dump(ha);
98                         else
99                                 qla2300_ascii_fw_dump(ha);
100                         ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
101                 }
102                 break;
103         }
104         return (count);
105 }
106
107 static struct bin_attribute sysfs_fw_dump_attr = {
108         .attr = {
109                 .name = "fw_dump",
110                 .mode = S_IRUSR | S_IWUSR,
111                 .owner = THIS_MODULE,
112         },
113         .size = 0,
114         .read = qla2x00_sysfs_read_fw_dump,
115         .write = qla2x00_sysfs_write_fw_dump,
116 };
117
118 static ssize_t
119 qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf, loff_t off,
120     size_t count)
121 {
122         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
123             struct device, kobj)));
124         uint16_t        *witer;
125         unsigned long   flags;
126         uint16_t        cnt;
127
128         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
129                 return 0;
130
131         /* Read NVRAM. */
132         spin_lock_irqsave(&ha->hardware_lock, flags);
133         qla2x00_lock_nvram_access(ha);
134         witer = (uint16_t *)buf;
135         for (cnt = 0; cnt < count / 2; cnt++) {
136                 *witer = cpu_to_le16(qla2x00_get_nvram_word(ha,
137                     cnt+ha->nvram_base));
138                 witer++;
139         }
140         qla2x00_unlock_nvram_access(ha);
141         spin_unlock_irqrestore(&ha->hardware_lock, flags);
142
143         return (count);
144 }
145
146 static ssize_t
147 qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf, loff_t off,
148     size_t count)
149 {
150         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
151             struct device, kobj)));
152         uint8_t         *iter;
153         uint16_t        *witer;
154         unsigned long   flags;
155         uint16_t        cnt;
156         uint8_t         chksum;
157
158         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
159                 return 0;
160
161         /* Checksum NVRAM. */
162         iter = (uint8_t *)buf;
163         chksum = 0;
164         for (cnt = 0; cnt < count - 1; cnt++)
165                 chksum += *iter++;
166         chksum = ~chksum + 1;
167         *iter = chksum;
168
169         /* Write NVRAM. */
170         spin_lock_irqsave(&ha->hardware_lock, flags);
171         qla2x00_lock_nvram_access(ha);
172         qla2x00_release_nvram_protection(ha);
173         witer = (uint16_t *)buf;
174         for (cnt = 0; cnt < count / 2; cnt++) {
175                 qla2x00_write_nvram_word(ha, cnt+ha->nvram_base,
176                     cpu_to_le16(*witer));
177                 witer++;
178         }
179         qla2x00_unlock_nvram_access(ha);
180         spin_unlock_irqrestore(&ha->hardware_lock, flags);
181
182         return (count);
183 }
184
185 static struct bin_attribute sysfs_nvram_attr = {
186         .attr = {
187                 .name = "nvram",
188                 .mode = S_IRUSR | S_IWUSR,
189                 .owner = THIS_MODULE,
190         },
191         .size = sizeof(nvram_t),
192         .read = qla2x00_sysfs_read_nvram,
193         .write = qla2x00_sysfs_write_nvram,
194 };
195
196 void
197 qla2x00_alloc_sysfs_attr(scsi_qla_host_t *ha)
198 {
199         struct Scsi_Host *host = ha->host;
200
201         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
202         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
203 }
204
205 void
206 qla2x00_free_sysfs_attr(scsi_qla_host_t *ha)
207 {
208         struct Scsi_Host *host = ha->host;
209
210         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
211         sysfs_remove_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
212 }
213
214 /* Host attributes. */
215
216 static void
217 qla2x00_get_host_port_id(struct Scsi_Host *shost)
218 {
219         scsi_qla_host_t *ha = to_qla_host(shost);
220
221         fc_host_port_id(shost) = ha->d_id.b.domain << 16 |
222             ha->d_id.b.area << 8 | ha->d_id.b.al_pa;
223 }
224
225 static void
226 qla2x00_get_starget_node_name(struct scsi_target *starget)
227 {
228         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
229         scsi_qla_host_t *ha = to_qla_host(host);
230         fc_port_t *fcport;
231         uint64_t node_name = 0;
232
233         list_for_each_entry(fcport, &ha->fcports, list) {
234                 if (starget->id == fcport->os_target_id) {
235                         node_name = *(uint64_t *)fcport->node_name;
236                         break;
237                 }
238         }
239
240         fc_starget_node_name(starget) = be64_to_cpu(node_name);
241 }
242
243 static void
244 qla2x00_get_starget_port_name(struct scsi_target *starget)
245 {
246         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
247         scsi_qla_host_t *ha = to_qla_host(host);
248         fc_port_t *fcport;
249         uint64_t port_name = 0;
250
251         list_for_each_entry(fcport, &ha->fcports, list) {
252                 if (starget->id == fcport->os_target_id) {
253                         port_name = *(uint64_t *)fcport->port_name;
254                         break;
255                 }
256         }
257
258         fc_starget_port_name(starget) = be64_to_cpu(port_name);
259 }
260
261 static void
262 qla2x00_get_starget_port_id(struct scsi_target *starget)
263 {
264         struct Scsi_Host *host = dev_to_shost(starget->dev.parent);
265         scsi_qla_host_t *ha = to_qla_host(host);
266         fc_port_t *fcport;
267         uint32_t port_id = ~0U;
268
269         list_for_each_entry(fcport, &ha->fcports, list) {
270                 if (starget->id == fcport->os_target_id) {
271                         port_id = fcport->d_id.b.domain << 16 |
272                             fcport->d_id.b.area << 8 | fcport->d_id.b.al_pa;
273                         break;
274                 }
275         }
276
277         fc_starget_port_id(starget) = port_id;
278 }
279
280 static void
281 qla2x00_get_rport_loss_tmo(struct fc_rport *rport)
282 {
283         struct Scsi_Host *host = rport_to_shost(rport);
284         scsi_qla_host_t *ha = to_qla_host(host);
285
286         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
287 }
288
289 static void
290 qla2x00_set_rport_loss_tmo(struct fc_rport *rport, uint32_t timeout)
291 {
292         struct Scsi_Host *host = rport_to_shost(rport);
293         scsi_qla_host_t *ha = to_qla_host(host);
294
295         if (timeout)
296                 ha->port_down_retry_count = timeout;
297         else
298                 ha->port_down_retry_count = 1;
299
300         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
301 }
302
303 struct fc_function_template qla2xxx_transport_functions = {
304
305         .show_host_node_name = 1,
306         .show_host_port_name = 1,
307         .get_host_port_id = qla2x00_get_host_port_id,
308         .show_host_port_id = 1,
309
310         .dd_fcrport_size = sizeof(struct fc_port *),
311
312         .get_starget_node_name = qla2x00_get_starget_node_name,
313         .show_starget_node_name = 1,
314         .get_starget_port_name = qla2x00_get_starget_port_name,
315         .show_starget_port_name = 1,
316         .get_starget_port_id  = qla2x00_get_starget_port_id,
317         .show_starget_port_id = 1,
318
319         .get_rport_dev_loss_tmo = qla2x00_get_rport_loss_tmo,
320         .set_rport_dev_loss_tmo = qla2x00_set_rport_loss_tmo,
321         .show_rport_dev_loss_tmo = 1,
322
323 };
324
325 void
326 qla2x00_init_host_attr(scsi_qla_host_t *ha)
327 {
328         fc_host_node_name(ha->host) =
329             be64_to_cpu(*(uint64_t *)ha->init_cb->node_name);
330         fc_host_port_name(ha->host) =
331             be64_to_cpu(*(uint64_t *)ha->init_cb->port_name);
332 }