blob: 5877f29a600541d7acfaf44b8ea91fae8d74243c [file] [log] [blame]
Erich Chen1c57e862006-07-12 08:59:32 -07001/*
2*******************************************************************************
3** O.S : Linux
4** FILE NAME : arcmsr_attr.c
5** BY : Erich Chen
6** Description: attributes exported to sysfs and device host
7*******************************************************************************
8** Copyright (C) 2002 - 2005, Areca Technology Corporation All rights reserved
9**
10** Web site: www.areca.com.tw
Nick Cheng1a4f5502007-09-13 17:26:40 +080011** E-mail: support@areca.com.tw
Erich Chen1c57e862006-07-12 08:59:32 -070012**
13** This program is free software; you can redistribute it and/or modify
14** it under the terms of the GNU General Public License version 2 as
15** published by the Free Software Foundation.
16** This program is distributed in the hope that it will be useful,
17** but WITHOUT ANY WARRANTY; without even the implied warranty of
18** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19** GNU General Public License for more details.
20*******************************************************************************
21** Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions
23** are met:
24** 1. Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** 2. Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in the
28** documentation and/or other materials provided with the distribution.
29** 3. The name of the author may not be used to endorse or promote products
30** derived from this software without specific prior written permission.
31**
32** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
33** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
36** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING,BUT
37** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38** DATA, OR PROFITS; OR BUSINESS INTERRUPTION)HOWEVER CAUSED AND ON ANY
39** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40** (INCLUDING NEGLIGENCE OR OTHERWISE)ARISING IN ANY WAY OUT OF THE USE OF
41** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42*******************************************************************************
43** For history of changes, see Documentation/scsi/ChangeLog.arcmsr
44** Firmware Specification, see Documentation/scsi/arcmsr_spec.txt
45*******************************************************************************
46*/
47#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/init.h>
50#include <linux/errno.h>
51#include <linux/delay.h>
Nick Cheng1a4f5502007-09-13 17:26:40 +080052#include <linux/pci.h>
Erich Chen1c57e862006-07-12 08:59:32 -070053
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
57#include <scsi/scsi_transport.h>
58#include "arcmsr.h"
59
Tony Jonesee959b02008-02-22 00:13:36 +010060struct device_attribute *arcmsr_host_attrs[];
Erich Chen1c57e862006-07-12 08:59:32 -070061
James Bottomley685b9e02007-09-15 11:41:53 -050062static ssize_t arcmsr_sysfs_iop_message_read(struct kobject *kobj,
63 struct bin_attribute *bin,
64 char *buf, loff_t off,
65 size_t count)
Erich Chen1c57e862006-07-12 08:59:32 -070066{
Tony Jonesee959b02008-02-22 00:13:36 +010067 struct device *dev = container_of(kobj,struct device,kobj);
68 struct Scsi_Host *host = class_to_shost(dev);
Erich Chen1c57e862006-07-12 08:59:32 -070069 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -070070 uint8_t *pQbuffer,*ptmpQbuffer;
71 int32_t allxfer_len = 0;
72
73 if (!capable(CAP_SYS_ADMIN))
74 return -EACCES;
75
76 /* do message unit read. */
77 ptmpQbuffer = (uint8_t *)buf;
78 while ((acb->rqbuf_firstindex != acb->rqbuf_lastindex)
79 && (allxfer_len < 1031)) {
80 pQbuffer = &acb->rqbuffer[acb->rqbuf_firstindex];
81 memcpy(ptmpQbuffer, pQbuffer, 1);
82 acb->rqbuf_firstindex++;
83 acb->rqbuf_firstindex %= ARCMSR_MAX_QBUFFER;
84 ptmpQbuffer++;
85 allxfer_len++;
86 }
87 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
Al Viro80da1ad2007-10-29 05:08:28 +000088 struct QBUFFER __iomem *prbuffer;
89 uint8_t __iomem *iop_data;
Erich Chen1c57e862006-07-12 08:59:32 -070090 int32_t iop_len;
91
92 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
Nick Cheng1a4f5502007-09-13 17:26:40 +080093 prbuffer = arcmsr_get_iop_rqbuffer(acb);
Al Viro80da1ad2007-10-29 05:08:28 +000094 iop_data = prbuffer->data;
Erich Chen1c57e862006-07-12 08:59:32 -070095 iop_len = readl(&prbuffer->data_len);
96 while (iop_len > 0) {
97 acb->rqbuffer[acb->rqbuf_lastindex] = readb(iop_data);
98 acb->rqbuf_lastindex++;
99 acb->rqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
100 iop_data++;
101 iop_len--;
102 }
Nick Cheng1a4f5502007-09-13 17:26:40 +0800103 arcmsr_iop_message_read(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700104 }
105 return (allxfer_len);
106}
107
James Bottomley685b9e02007-09-15 11:41:53 -0500108static ssize_t arcmsr_sysfs_iop_message_write(struct kobject *kobj,
109 struct bin_attribute *bin,
110 char *buf, loff_t off,
111 size_t count)
Erich Chen1c57e862006-07-12 08:59:32 -0700112{
Tony Jonesee959b02008-02-22 00:13:36 +0100113 struct device *dev = container_of(kobj,struct device,kobj);
114 struct Scsi_Host *host = class_to_shost(dev);
Erich Chen1c57e862006-07-12 08:59:32 -0700115 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
116 int32_t my_empty_len, user_len, wqbuf_firstindex, wqbuf_lastindex;
117 uint8_t *pQbuffer, *ptmpuserbuffer;
118
119 if (!capable(CAP_SYS_ADMIN))
120 return -EACCES;
121 if (count > 1032)
122 return -EINVAL;
123 /* do message unit write. */
124 ptmpuserbuffer = (uint8_t *)buf;
125 user_len = (int32_t)count;
126 wqbuf_lastindex = acb->wqbuf_lastindex;
127 wqbuf_firstindex = acb->wqbuf_firstindex;
128 if (wqbuf_lastindex != wqbuf_firstindex) {
Nick Cheng1a4f5502007-09-13 17:26:40 +0800129 arcmsr_post_ioctldata2iop(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700130 return 0; /*need retry*/
131 } else {
132 my_empty_len = (wqbuf_firstindex-wqbuf_lastindex - 1)
133 &(ARCMSR_MAX_QBUFFER - 1);
134 if (my_empty_len >= user_len) {
135 while (user_len > 0) {
136 pQbuffer =
137 &acb->wqbuffer[acb->wqbuf_lastindex];
138 memcpy(pQbuffer, ptmpuserbuffer, 1);
139 acb->wqbuf_lastindex++;
140 acb->wqbuf_lastindex %= ARCMSR_MAX_QBUFFER;
141 ptmpuserbuffer++;
142 user_len--;
143 }
144 if (acb->acb_flags & ACB_F_MESSAGE_WQBUFFER_CLEARED) {
145 acb->acb_flags &=
146 ~ACB_F_MESSAGE_WQBUFFER_CLEARED;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800147 arcmsr_post_ioctldata2iop(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700148 }
149 return count;
150 } else {
151 return 0; /*need retry*/
152 }
153 }
154}
155
James Bottomley685b9e02007-09-15 11:41:53 -0500156static ssize_t arcmsr_sysfs_iop_message_clear(struct kobject *kobj,
157 struct bin_attribute *bin,
158 char *buf, loff_t off,
159 size_t count)
Erich Chen1c57e862006-07-12 08:59:32 -0700160{
Tony Jonesee959b02008-02-22 00:13:36 +0100161 struct device *dev = container_of(kobj,struct device,kobj);
162 struct Scsi_Host *host = class_to_shost(dev);
Erich Chen1c57e862006-07-12 08:59:32 -0700163 struct AdapterControlBlock *acb = (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700164 uint8_t *pQbuffer;
165
166 if (!capable(CAP_SYS_ADMIN))
167 return -EACCES;
168
169 if (acb->acb_flags & ACB_F_IOPDATA_OVERFLOW) {
170 acb->acb_flags &= ~ACB_F_IOPDATA_OVERFLOW;
Nick Cheng1a4f5502007-09-13 17:26:40 +0800171 arcmsr_iop_message_read(acb);
Erich Chen1c57e862006-07-12 08:59:32 -0700172 }
173 acb->acb_flags |=
174 (ACB_F_MESSAGE_WQBUFFER_CLEARED
175 | ACB_F_MESSAGE_RQBUFFER_CLEARED
176 | ACB_F_MESSAGE_WQBUFFER_READED);
177 acb->rqbuf_firstindex = 0;
178 acb->rqbuf_lastindex = 0;
179 acb->wqbuf_firstindex = 0;
180 acb->wqbuf_lastindex = 0;
181 pQbuffer = acb->rqbuffer;
182 memset(pQbuffer, 0, sizeof (struct QBUFFER));
183 pQbuffer = acb->wqbuffer;
184 memset(pQbuffer, 0, sizeof (struct QBUFFER));
185 return 1;
186}
187
188static struct bin_attribute arcmsr_sysfs_message_read_attr = {
189 .attr = {
190 .name = "mu_read",
191 .mode = S_IRUSR ,
Erich Chen1c57e862006-07-12 08:59:32 -0700192 },
193 .size = 1032,
194 .read = arcmsr_sysfs_iop_message_read,
195};
196
197static struct bin_attribute arcmsr_sysfs_message_write_attr = {
198 .attr = {
199 .name = "mu_write",
200 .mode = S_IWUSR,
Erich Chen1c57e862006-07-12 08:59:32 -0700201 },
202 .size = 1032,
203 .write = arcmsr_sysfs_iop_message_write,
204};
205
206static struct bin_attribute arcmsr_sysfs_message_clear_attr = {
207 .attr = {
208 .name = "mu_clear",
209 .mode = S_IWUSR,
Erich Chen1c57e862006-07-12 08:59:32 -0700210 },
211 .size = 1,
212 .write = arcmsr_sysfs_iop_message_clear,
213};
214
215int arcmsr_alloc_sysfs_attr(struct AdapterControlBlock *acb)
216{
217 struct Scsi_Host *host = acb->host;
218 int error;
219
Tony Jonesee959b02008-02-22 00:13:36 +0100220 error = sysfs_create_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_read_attr);
Erich Chen1c57e862006-07-12 08:59:32 -0700221 if (error) {
222 printk(KERN_ERR "arcmsr: alloc sysfs mu_read failed\n");
223 goto error_bin_file_message_read;
224 }
Tony Jonesee959b02008-02-22 00:13:36 +0100225 error = sysfs_create_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_write_attr);
Erich Chen1c57e862006-07-12 08:59:32 -0700226 if (error) {
227 printk(KERN_ERR "arcmsr: alloc sysfs mu_write failed\n");
228 goto error_bin_file_message_write;
229 }
Tony Jonesee959b02008-02-22 00:13:36 +0100230 error = sysfs_create_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_clear_attr);
Erich Chen1c57e862006-07-12 08:59:32 -0700231 if (error) {
232 printk(KERN_ERR "arcmsr: alloc sysfs mu_clear failed\n");
233 goto error_bin_file_message_clear;
234 }
235 return 0;
236error_bin_file_message_clear:
Tony Jonesee959b02008-02-22 00:13:36 +0100237 sysfs_remove_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_write_attr);
Erich Chen1c57e862006-07-12 08:59:32 -0700238error_bin_file_message_write:
Tony Jonesee959b02008-02-22 00:13:36 +0100239 sysfs_remove_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_read_attr);
Erich Chen1c57e862006-07-12 08:59:32 -0700240error_bin_file_message_read:
241 return error;
242}
243
Tony Jonesee959b02008-02-22 00:13:36 +0100244void arcmsr_free_sysfs_attr(struct AdapterControlBlock *acb)
245{
Erich Chen1c57e862006-07-12 08:59:32 -0700246 struct Scsi_Host *host = acb->host;
Erich Chen1c57e862006-07-12 08:59:32 -0700247
Tony Jonesee959b02008-02-22 00:13:36 +0100248 sysfs_remove_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_clear_attr);
249 sysfs_remove_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_write_attr);
250 sysfs_remove_bin_file(&host->shost_dev.kobj, &arcmsr_sysfs_message_read_attr);
Erich Chen1c57e862006-07-12 08:59:32 -0700251}
252
253
254static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100255arcmsr_attr_host_driver_version(struct device *dev,
256 struct device_attribute *attr, char *buf)
257{
Erich Chen1c57e862006-07-12 08:59:32 -0700258 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500259 "%s\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700260 ARCMSR_DRIVER_VERSION);
261}
262
263static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100264arcmsr_attr_host_driver_posted_cmd(struct device *dev,
265 struct device_attribute *attr, char *buf)
266{
267 struct Scsi_Host *host = class_to_shost(dev);
268 struct AdapterControlBlock *acb =
269 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700270 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500271 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700272 atomic_read(&acb->ccboutstandingcount));
273}
274
275static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100276arcmsr_attr_host_driver_reset(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct Scsi_Host *host = class_to_shost(dev);
280 struct AdapterControlBlock *acb =
281 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700282 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500283 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700284 acb->num_resets);
285}
286
287static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100288arcmsr_attr_host_driver_abort(struct device *dev,
289 struct device_attribute *attr, char *buf)
290{
291 struct Scsi_Host *host = class_to_shost(dev);
292 struct AdapterControlBlock *acb =
293 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700294 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500295 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700296 acb->num_aborts);
297}
298
299static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100300arcmsr_attr_host_fw_model(struct device *dev, struct device_attribute *attr,
301 char *buf)
302{
303 struct Scsi_Host *host = class_to_shost(dev);
304 struct AdapterControlBlock *acb =
305 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700306 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500307 "%s\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700308 acb->firm_model);
309}
310
311static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100312arcmsr_attr_host_fw_version(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315 struct Scsi_Host *host = class_to_shost(dev);
316 struct AdapterControlBlock *acb =
317 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700318
319 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500320 "%s\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700321 acb->firm_version);
322}
323
324static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100325arcmsr_attr_host_fw_request_len(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
328 struct Scsi_Host *host = class_to_shost(dev);
329 struct AdapterControlBlock *acb =
330 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700331
332 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500333 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700334 acb->firm_request_len);
335}
336
337static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100338arcmsr_attr_host_fw_numbers_queue(struct device *dev,
339 struct device_attribute *attr, char *buf)
340{
341 struct Scsi_Host *host = class_to_shost(dev);
342 struct AdapterControlBlock *acb =
343 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700344
345 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500346 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700347 acb->firm_numbers_queue);
348}
349
350static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100351arcmsr_attr_host_fw_sdram_size(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 struct Scsi_Host *host = class_to_shost(dev);
355 struct AdapterControlBlock *acb =
356 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700357
358 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500359 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700360 acb->firm_sdram_size);
361}
362
363static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100364arcmsr_attr_host_fw_hd_channels(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
367 struct Scsi_Host *host = class_to_shost(dev);
368 struct AdapterControlBlock *acb =
369 (struct AdapterControlBlock *) host->hostdata;
Erich Chen1c57e862006-07-12 08:59:32 -0700370
371 return snprintf(buf, PAGE_SIZE,
James Bottomleyd67a70a2006-07-28 17:36:46 -0500372 "%4d\n",
Erich Chen1c57e862006-07-12 08:59:32 -0700373 acb->firm_hd_channels);
374}
375
Tony Jonesee959b02008-02-22 00:13:36 +0100376static DEVICE_ATTR(host_driver_version, S_IRUGO, arcmsr_attr_host_driver_version, NULL);
377static DEVICE_ATTR(host_driver_posted_cmd, S_IRUGO, arcmsr_attr_host_driver_posted_cmd, NULL);
378static DEVICE_ATTR(host_driver_reset, S_IRUGO, arcmsr_attr_host_driver_reset, NULL);
379static DEVICE_ATTR(host_driver_abort, S_IRUGO, arcmsr_attr_host_driver_abort, NULL);
380static DEVICE_ATTR(host_fw_model, S_IRUGO, arcmsr_attr_host_fw_model, NULL);
381static DEVICE_ATTR(host_fw_version, S_IRUGO, arcmsr_attr_host_fw_version, NULL);
382static DEVICE_ATTR(host_fw_request_len, S_IRUGO, arcmsr_attr_host_fw_request_len, NULL);
383static DEVICE_ATTR(host_fw_numbers_queue, S_IRUGO, arcmsr_attr_host_fw_numbers_queue, NULL);
384static DEVICE_ATTR(host_fw_sdram_size, S_IRUGO, arcmsr_attr_host_fw_sdram_size, NULL);
385static DEVICE_ATTR(host_fw_hd_channels, S_IRUGO, arcmsr_attr_host_fw_hd_channels, NULL);
Erich Chen1c57e862006-07-12 08:59:32 -0700386
Tony Jonesee959b02008-02-22 00:13:36 +0100387struct device_attribute *arcmsr_host_attrs[] = {
388 &dev_attr_host_driver_version,
389 &dev_attr_host_driver_posted_cmd,
390 &dev_attr_host_driver_reset,
391 &dev_attr_host_driver_abort,
392 &dev_attr_host_fw_model,
393 &dev_attr_host_fw_version,
394 &dev_attr_host_fw_request_len,
395 &dev_attr_host_fw_numbers_queue,
396 &dev_attr_host_fw_sdram_size,
397 &dev_attr_host_fw_hd_channels,
Erich Chen1c57e862006-07-12 08:59:32 -0700398 NULL,
399};