OSDN Git Service

smb3: Add defines for new information level, FileIdInformation
[tomoyo/tomoyo-test1.git] / drivers / scsi / esas2r / esas2r_main.c
1 /*
2  *  linux/drivers/scsi/esas2r/esas2r_main.c
3  *      For use with ATTO ExpressSAS R6xx SAS/SATA RAID controllers
4  *
5  *  Copyright (c) 2001-2013 ATTO Technology, Inc.
6  *  (mailto:linuxdrivers@attotech.com)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * NO WARRANTY
19  * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20  * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21  * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23  * solely responsible for determining the appropriateness of using and
24  * distributing the Program and assumes all risks associated with its
25  * exercise of rights under this Agreement, including but not limited to
26  * the risks and costs of program errors, damage to or loss of data,
27  * programs or equipment, and unavailability or interruption of operations.
28  *
29  * DISCLAIMER OF LIABILITY
30  * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35  * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36  * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
41  * USA.
42  */
43
44 #include "esas2r.h"
45
46 MODULE_DESCRIPTION(ESAS2R_DRVR_NAME ": " ESAS2R_LONGNAME " driver");
47 MODULE_AUTHOR("ATTO Technology, Inc.");
48 MODULE_LICENSE("GPL");
49 MODULE_VERSION(ESAS2R_VERSION_STR);
50
51 /* global definitions */
52
53 static int found_adapters;
54 struct esas2r_adapter *esas2r_adapters[MAX_ADAPTERS];
55
56 #define ESAS2R_VDA_EVENT_PORT1       54414
57 #define ESAS2R_VDA_EVENT_PORT2       54415
58 #define ESAS2R_VDA_EVENT_SOCK_COUNT  2
59
60 static struct esas2r_adapter *esas2r_adapter_from_kobj(struct kobject *kobj)
61 {
62         struct device *dev = container_of(kobj, struct device, kobj);
63         struct Scsi_Host *host = class_to_shost(dev);
64
65         return (struct esas2r_adapter *)host->hostdata;
66 }
67
68 static ssize_t read_fw(struct file *file, struct kobject *kobj,
69                        struct bin_attribute *attr,
70                        char *buf, loff_t off, size_t count)
71 {
72         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
73
74         return esas2r_read_fw(a, buf, off, count);
75 }
76
77 static ssize_t write_fw(struct file *file, struct kobject *kobj,
78                         struct bin_attribute *attr,
79                         char *buf, loff_t off, size_t count)
80 {
81         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
82
83         return esas2r_write_fw(a, buf, off, count);
84 }
85
86 static ssize_t read_fs(struct file *file, struct kobject *kobj,
87                        struct bin_attribute *attr,
88                        char *buf, loff_t off, size_t count)
89 {
90         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
91
92         return esas2r_read_fs(a, buf, off, count);
93 }
94
95 static ssize_t write_fs(struct file *file, struct kobject *kobj,
96                         struct bin_attribute *attr,
97                         char *buf, loff_t off, size_t count)
98 {
99         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
100         int length = min(sizeof(struct esas2r_ioctl_fs), count);
101         int result = 0;
102
103         result = esas2r_write_fs(a, buf, off, count);
104
105         if (result < 0)
106                 result = 0;
107
108         return length;
109 }
110
111 static ssize_t read_vda(struct file *file, struct kobject *kobj,
112                         struct bin_attribute *attr,
113                         char *buf, loff_t off, size_t count)
114 {
115         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
116
117         return esas2r_read_vda(a, buf, off, count);
118 }
119
120 static ssize_t write_vda(struct file *file, struct kobject *kobj,
121                          struct bin_attribute *attr,
122                          char *buf, loff_t off, size_t count)
123 {
124         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
125
126         return esas2r_write_vda(a, buf, off, count);
127 }
128
129 static ssize_t read_live_nvram(struct file *file, struct kobject *kobj,
130                                struct bin_attribute *attr,
131                                char *buf, loff_t off, size_t count)
132 {
133         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
134         int length = min_t(size_t, sizeof(struct esas2r_sas_nvram), PAGE_SIZE);
135
136         memcpy(buf, a->nvram, length);
137         return length;
138 }
139
140 static ssize_t write_live_nvram(struct file *file, struct kobject *kobj,
141                                 struct bin_attribute *attr,
142                                 char *buf, loff_t off, size_t count)
143 {
144         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
145         struct esas2r_request *rq;
146         int result = -EFAULT;
147
148         rq = esas2r_alloc_request(a);
149         if (rq == NULL)
150                 return -ENOMEM;
151
152         if (esas2r_write_params(a, rq, (struct esas2r_sas_nvram *)buf))
153                 result = count;
154
155         esas2r_free_request(a, rq);
156
157         return result;
158 }
159
160 static ssize_t read_default_nvram(struct file *file, struct kobject *kobj,
161                                   struct bin_attribute *attr,
162                                   char *buf, loff_t off, size_t count)
163 {
164         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
165
166         esas2r_nvram_get_defaults(a, (struct esas2r_sas_nvram *)buf);
167
168         return sizeof(struct esas2r_sas_nvram);
169 }
170
171 static ssize_t read_hw(struct file *file, struct kobject *kobj,
172                        struct bin_attribute *attr,
173                        char *buf, loff_t off, size_t count)
174 {
175         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
176         int length = min_t(size_t, sizeof(struct atto_ioctl), PAGE_SIZE);
177
178         if (!a->local_atto_ioctl)
179                 return -ENOMEM;
180
181         if (handle_hba_ioctl(a, a->local_atto_ioctl) != IOCTL_SUCCESS)
182                 return -ENOMEM;
183
184         memcpy(buf, a->local_atto_ioctl, length);
185
186         return length;
187 }
188
189 static ssize_t write_hw(struct file *file, struct kobject *kobj,
190                         struct bin_attribute *attr,
191                         char *buf, loff_t off, size_t count)
192 {
193         struct esas2r_adapter *a = esas2r_adapter_from_kobj(kobj);
194         int length = min(sizeof(struct atto_ioctl), count);
195
196         if (!a->local_atto_ioctl) {
197                 a->local_atto_ioctl = kmalloc(sizeof(struct atto_ioctl),
198                                               GFP_KERNEL);
199                 if (a->local_atto_ioctl == NULL) {
200                         esas2r_log(ESAS2R_LOG_WARN,
201                                    "write_hw kzalloc failed for %zu bytes",
202                                    sizeof(struct atto_ioctl));
203                         return -ENOMEM;
204                 }
205         }
206
207         memset(a->local_atto_ioctl, 0, sizeof(struct atto_ioctl));
208         memcpy(a->local_atto_ioctl, buf, length);
209
210         return length;
211 }
212
213 #define ESAS2R_RW_BIN_ATTR(_name) \
214         struct bin_attribute bin_attr_ ## _name = { \
215                 .attr   = \
216                 { .name = __stringify(_name), .mode  = S_IRUSR | S_IWUSR }, \
217                 .size   = 0, \
218                 .read   = read_ ## _name, \
219                 .write  = write_ ## _name }
220
221 ESAS2R_RW_BIN_ATTR(fw);
222 ESAS2R_RW_BIN_ATTR(fs);
223 ESAS2R_RW_BIN_ATTR(vda);
224 ESAS2R_RW_BIN_ATTR(hw);
225 ESAS2R_RW_BIN_ATTR(live_nvram);
226
227 struct bin_attribute bin_attr_default_nvram = {
228         .attr   = { .name = "default_nvram", .mode = S_IRUGO },
229         .size   = 0,
230         .read   = read_default_nvram,
231         .write  = NULL
232 };
233
234 static struct scsi_host_template driver_template = {
235         .module                         = THIS_MODULE,
236         .show_info                      = esas2r_show_info,
237         .name                           = ESAS2R_LONGNAME,
238         .info                           = esas2r_info,
239         .ioctl                          = esas2r_ioctl,
240         .queuecommand                   = esas2r_queuecommand,
241         .eh_abort_handler               = esas2r_eh_abort,
242         .eh_device_reset_handler        = esas2r_device_reset,
243         .eh_bus_reset_handler           = esas2r_bus_reset,
244         .eh_host_reset_handler          = esas2r_host_reset,
245         .eh_target_reset_handler        = esas2r_target_reset,
246         .can_queue                      = 128,
247         .this_id                        = -1,
248         .sg_tablesize                   = SG_CHUNK_SIZE,
249         .cmd_per_lun                    =
250                 ESAS2R_DEFAULT_CMD_PER_LUN,
251         .present                        = 0,
252         .unchecked_isa_dma              = 0,
253         .emulated                       = 0,
254         .proc_name                      = ESAS2R_DRVR_NAME,
255         .change_queue_depth             = scsi_change_queue_depth,
256         .max_sectors                    = 0xFFFF,
257 };
258
259 int sgl_page_size = 512;
260 module_param(sgl_page_size, int, 0);
261 MODULE_PARM_DESC(sgl_page_size,
262                  "Scatter/gather list (SGL) page size in number of S/G "
263                  "entries.  If your application is doing a lot of very large "
264                  "transfers, you may want to increase the SGL page size.  "
265                  "Default 512.");
266
267 int num_sg_lists = 1024;
268 module_param(num_sg_lists, int, 0);
269 MODULE_PARM_DESC(num_sg_lists,
270                  "Number of scatter/gather lists.  Default 1024.");
271
272 int sg_tablesize = SG_CHUNK_SIZE;
273 module_param(sg_tablesize, int, 0);
274 MODULE_PARM_DESC(sg_tablesize,
275                  "Maximum number of entries in a scatter/gather table.");
276
277 int num_requests = 256;
278 module_param(num_requests, int, 0);
279 MODULE_PARM_DESC(num_requests,
280                  "Number of requests.  Default 256.");
281
282 int num_ae_requests = 4;
283 module_param(num_ae_requests, int, 0);
284 MODULE_PARM_DESC(num_ae_requests,
285                  "Number of VDA asynchronous event requests.  Default 4.");
286
287 int cmd_per_lun = ESAS2R_DEFAULT_CMD_PER_LUN;
288 module_param(cmd_per_lun, int, 0);
289 MODULE_PARM_DESC(cmd_per_lun,
290                  "Maximum number of commands per LUN.  Default "
291                  DEFINED_NUM_TO_STR(ESAS2R_DEFAULT_CMD_PER_LUN) ".");
292
293 int can_queue = 128;
294 module_param(can_queue, int, 0);
295 MODULE_PARM_DESC(can_queue,
296                  "Maximum number of commands per adapter.  Default 128.");
297
298 int esas2r_max_sectors = 0xFFFF;
299 module_param(esas2r_max_sectors, int, 0);
300 MODULE_PARM_DESC(esas2r_max_sectors,
301                  "Maximum number of disk sectors in a single data transfer.  "
302                  "Default 65535 (largest possible setting).");
303
304 int interrupt_mode = 1;
305 module_param(interrupt_mode, int, 0);
306 MODULE_PARM_DESC(interrupt_mode,
307                  "Defines the interrupt mode to use.  0 for legacy"
308                  ", 1 for MSI.  Default is MSI (1).");
309
310 static const struct pci_device_id
311         esas2r_pci_table[] = {
312         { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x0049,
313           0,
314           0, 0 },
315         { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004A,
316           0,
317           0, 0 },
318         { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004B,
319           0,
320           0, 0 },
321         { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004C,
322           0,
323           0, 0 },
324         { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004D,
325           0,
326           0, 0 },
327         { ATTO_VENDOR_ID, 0x0049,         ATTO_VENDOR_ID, 0x004E,
328           0,
329           0, 0 },
330         { 0,              0,              0,              0,
331           0,
332           0, 0 }
333 };
334
335 MODULE_DEVICE_TABLE(pci, esas2r_pci_table);
336
337 static int
338 esas2r_probe(struct pci_dev *pcid, const struct pci_device_id *id);
339
340 static void
341 esas2r_remove(struct pci_dev *pcid);
342
343 static struct pci_driver
344         esas2r_pci_driver = {
345         .name           = ESAS2R_DRVR_NAME,
346         .id_table       = esas2r_pci_table,
347         .probe          = esas2r_probe,
348         .remove         = esas2r_remove,
349         .suspend        = esas2r_suspend,
350         .resume         = esas2r_resume,
351 };
352
353 static int esas2r_probe(struct pci_dev *pcid,
354                         const struct pci_device_id *id)
355 {
356         struct Scsi_Host *host = NULL;
357         struct esas2r_adapter *a;
358         int err;
359
360         size_t host_alloc_size = sizeof(struct esas2r_adapter)
361                                  + ((num_requests) +
362                                     1) * sizeof(struct esas2r_request);
363
364         esas2r_log_dev(ESAS2R_LOG_DEBG, &(pcid->dev),
365                        "esas2r_probe() 0x%02x 0x%02x 0x%02x 0x%02x",
366                        pcid->vendor,
367                        pcid->device,
368                        pcid->subsystem_vendor,
369                        pcid->subsystem_device);
370
371         esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
372                        "before pci_enable_device() "
373                        "enable_cnt: %d",
374                        pcid->enable_cnt.counter);
375
376         err = pci_enable_device(pcid);
377         if (err != 0) {
378                 esas2r_log_dev(ESAS2R_LOG_CRIT, &(pcid->dev),
379                                "pci_enable_device() FAIL (%d)",
380                                err);
381                 return -ENODEV;
382         }
383
384         esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
385                        "pci_enable_device() OK");
386         esas2r_log_dev(ESAS2R_LOG_INFO, &(pcid->dev),
387                        "after pci_enable_device() enable_cnt: %d",
388                        pcid->enable_cnt.counter);
389
390         host = scsi_host_alloc(&driver_template, host_alloc_size);
391         if (host == NULL) {
392                 esas2r_log(ESAS2R_LOG_CRIT, "scsi_host_alloc() FAIL");
393                 return -ENODEV;
394         }
395
396         memset(host->hostdata, 0, host_alloc_size);
397
398         a = (struct esas2r_adapter *)host->hostdata;
399
400         esas2r_log(ESAS2R_LOG_INFO, "scsi_host_alloc() OK host: %p", host);
401
402         /* override max LUN and max target id */
403
404         host->max_id = ESAS2R_MAX_ID + 1;
405         host->max_lun = 255;
406
407         /* we can handle 16-byte CDbs */
408
409         host->max_cmd_len = 16;
410
411         host->can_queue = can_queue;
412         host->cmd_per_lun = cmd_per_lun;
413         host->this_id = host->max_id + 1;
414         host->max_channel = 0;
415         host->unique_id = found_adapters;
416         host->sg_tablesize = sg_tablesize;
417         host->max_sectors = esas2r_max_sectors;
418
419         /* set to bus master for BIOses that don't do it for us */
420
421         esas2r_log(ESAS2R_LOG_INFO, "pci_set_master() called");
422
423         pci_set_master(pcid);
424
425         if (!esas2r_init_adapter(host, pcid, found_adapters)) {
426                 esas2r_log(ESAS2R_LOG_CRIT,
427                            "unable to initialize device at PCI bus %x:%x",
428                            pcid->bus->number,
429                            pcid->devfn);
430
431                 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
432                                "scsi_host_put() called");
433
434                 scsi_host_put(host);
435
436                 return 0;
437
438         }
439
440         esas2r_log(ESAS2R_LOG_INFO, "pci_set_drvdata(%p, %p) called", pcid,
441                    host->hostdata);
442
443         pci_set_drvdata(pcid, host);
444
445         esas2r_log(ESAS2R_LOG_INFO, "scsi_add_host() called");
446
447         err = scsi_add_host(host, &pcid->dev);
448
449         if (err) {
450                 esas2r_log(ESAS2R_LOG_CRIT, "scsi_add_host returned %d", err);
451                 esas2r_log_dev(ESAS2R_LOG_CRIT, &(host->shost_gendev),
452                                "scsi_add_host() FAIL");
453
454                 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
455                                "scsi_host_put() called");
456
457                 scsi_host_put(host);
458
459                 esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
460                                "pci_set_drvdata(%p, NULL) called",
461                                pcid);
462
463                 pci_set_drvdata(pcid, NULL);
464
465                 return -ENODEV;
466         }
467
468
469         esas2r_fw_event_on(a);
470
471         esas2r_log_dev(ESAS2R_LOG_INFO, &(host->shost_gendev),
472                        "scsi_scan_host() called");
473
474         scsi_scan_host(host);
475
476         /* Add sysfs binary files */
477         if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fw))
478                 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
479                                "Failed to create sysfs binary file: fw");
480         else
481                 a->sysfs_fw_created = 1;
482
483         if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_fs))
484                 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
485                                "Failed to create sysfs binary file: fs");
486         else
487                 a->sysfs_fs_created = 1;
488
489         if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_vda))
490                 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
491                                "Failed to create sysfs binary file: vda");
492         else
493                 a->sysfs_vda_created = 1;
494
495         if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_hw))
496                 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
497                                "Failed to create sysfs binary file: hw");
498         else
499                 a->sysfs_hw_created = 1;
500
501         if (sysfs_create_bin_file(&host->shost_dev.kobj, &bin_attr_live_nvram))
502                 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
503                                "Failed to create sysfs binary file: live_nvram");
504         else
505                 a->sysfs_live_nvram_created = 1;
506
507         if (sysfs_create_bin_file(&host->shost_dev.kobj,
508                                   &bin_attr_default_nvram))
509                 esas2r_log_dev(ESAS2R_LOG_WARN, &(host->shost_gendev),
510                                "Failed to create sysfs binary file: default_nvram");
511         else
512                 a->sysfs_default_nvram_created = 1;
513
514         found_adapters++;
515
516         return 0;
517 }
518
519 static void esas2r_remove(struct pci_dev *pdev)
520 {
521         struct Scsi_Host *host = pci_get_drvdata(pdev);
522         struct esas2r_adapter *a = (struct esas2r_adapter *)host->hostdata;
523
524         esas2r_log_dev(ESAS2R_LOG_INFO, &(pdev->dev),
525                        "esas2r_remove(%p) called; "
526                        "host:%p", pdev,
527                        host);
528
529         esas2r_kill_adapter(a->index);
530         found_adapters--;
531 }
532
533 static int __init esas2r_init(void)
534 {
535         int i;
536
537         esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
538
539         /* verify valid parameters */
540
541         if (can_queue < 1) {
542                 esas2r_log(ESAS2R_LOG_WARN,
543                            "warning: can_queue must be at least 1, value "
544                            "forced.");
545                 can_queue = 1;
546         } else if (can_queue > 2048) {
547                 esas2r_log(ESAS2R_LOG_WARN,
548                            "warning: can_queue must be no larger than 2048, "
549                            "value forced.");
550                 can_queue = 2048;
551         }
552
553         if (cmd_per_lun < 1) {
554                 esas2r_log(ESAS2R_LOG_WARN,
555                            "warning: cmd_per_lun must be at least 1, value "
556                            "forced.");
557                 cmd_per_lun = 1;
558         } else if (cmd_per_lun > 2048) {
559                 esas2r_log(ESAS2R_LOG_WARN,
560                            "warning: cmd_per_lun must be no larger than "
561                            "2048, value forced.");
562                 cmd_per_lun = 2048;
563         }
564
565         if (sg_tablesize < 32) {
566                 esas2r_log(ESAS2R_LOG_WARN,
567                            "warning: sg_tablesize must be at least 32, "
568                            "value forced.");
569                 sg_tablesize = 32;
570         }
571
572         if (esas2r_max_sectors < 1) {
573                 esas2r_log(ESAS2R_LOG_WARN,
574                            "warning: esas2r_max_sectors must be at least "
575                            "1, value forced.");
576                 esas2r_max_sectors = 1;
577         } else if (esas2r_max_sectors > 0xffff) {
578                 esas2r_log(ESAS2R_LOG_WARN,
579                            "warning: esas2r_max_sectors must be no larger "
580                            "than 0xffff, value forced.");
581                 esas2r_max_sectors = 0xffff;
582         }
583
584         sgl_page_size &= ~(ESAS2R_SGL_ALIGN - 1);
585
586         if (sgl_page_size < SGL_PG_SZ_MIN)
587                 sgl_page_size = SGL_PG_SZ_MIN;
588         else if (sgl_page_size > SGL_PG_SZ_MAX)
589                 sgl_page_size = SGL_PG_SZ_MAX;
590
591         if (num_sg_lists < NUM_SGL_MIN)
592                 num_sg_lists = NUM_SGL_MIN;
593         else if (num_sg_lists > NUM_SGL_MAX)
594                 num_sg_lists = NUM_SGL_MAX;
595
596         if (num_requests < NUM_REQ_MIN)
597                 num_requests = NUM_REQ_MIN;
598         else if (num_requests > NUM_REQ_MAX)
599                 num_requests = NUM_REQ_MAX;
600
601         if (num_ae_requests < NUM_AE_MIN)
602                 num_ae_requests = NUM_AE_MIN;
603         else if (num_ae_requests > NUM_AE_MAX)
604                 num_ae_requests = NUM_AE_MAX;
605
606         /* set up other globals */
607
608         for (i = 0; i < MAX_ADAPTERS; i++)
609                 esas2r_adapters[i] = NULL;
610
611         return pci_register_driver(&esas2r_pci_driver);
612 }
613
614 /* Handle ioctl calls to "/proc/scsi/esas2r/ATTOnode" */
615 static const struct file_operations esas2r_proc_fops = {
616         .compat_ioctl   = compat_ptr_ioctl,
617         .unlocked_ioctl = esas2r_proc_ioctl,
618 };
619
620 static struct Scsi_Host *esas2r_proc_host;
621 static int esas2r_proc_major;
622
623 long esas2r_proc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
624 {
625         return esas2r_ioctl_handler(esas2r_proc_host->hostdata,
626                                     cmd, (void __user *)arg);
627 }
628
629 static void __exit esas2r_exit(void)
630 {
631         esas2r_log(ESAS2R_LOG_INFO, "%s called", __func__);
632
633         if (esas2r_proc_major > 0) {
634                 esas2r_log(ESAS2R_LOG_INFO, "unregister proc");
635
636                 remove_proc_entry(ATTONODE_NAME,
637                                   esas2r_proc_host->hostt->proc_dir);
638                 unregister_chrdev(esas2r_proc_major, ESAS2R_DRVR_NAME);
639
640                 esas2r_proc_major = 0;
641         }
642
643         esas2r_log(ESAS2R_LOG_INFO, "pci_unregister_driver() called");
644
645         pci_unregister_driver(&esas2r_pci_driver);
646 }
647
648 int esas2r_show_info(struct seq_file *m, struct Scsi_Host *sh)
649 {
650         struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
651
652         struct esas2r_target *t;
653         int dev_count = 0;
654
655         esas2r_log(ESAS2R_LOG_DEBG, "esas2r_show_info (%p,%d)", m, sh->host_no);
656
657         seq_printf(m, ESAS2R_LONGNAME "\n"
658                    "Driver version: "ESAS2R_VERSION_STR "\n"
659                    "Flash version: %s\n"
660                    "Firmware version: %s\n"
661                    "Copyright "ESAS2R_COPYRIGHT_YEARS "\n"
662                    "http://www.attotech.com\n"
663                    "\n",
664                    a->flash_rev,
665                    a->fw_rev[0] ? a->fw_rev : "(none)");
666
667
668         seq_printf(m, "Adapter information:\n"
669                    "--------------------\n"
670                    "Model: %s\n"
671                    "SAS address: %02X%02X%02X%02X:%02X%02X%02X%02X\n",
672                    esas2r_get_model_name(a),
673                    a->nvram->sas_addr[0],
674                    a->nvram->sas_addr[1],
675                    a->nvram->sas_addr[2],
676                    a->nvram->sas_addr[3],
677                    a->nvram->sas_addr[4],
678                    a->nvram->sas_addr[5],
679                    a->nvram->sas_addr[6],
680                    a->nvram->sas_addr[7]);
681
682         seq_puts(m, "\n"
683                    "Discovered devices:\n"
684                    "\n"
685                    "   #  Target ID\n"
686                    "---------------\n");
687
688         for (t = a->targetdb; t < a->targetdb_end; t++)
689                 if (t->buffered_target_state == TS_PRESENT) {
690                         seq_printf(m, " %3d   %3d\n",
691                                    ++dev_count,
692                                    (u16)(uintptr_t)(t - a->targetdb));
693                 }
694
695         if (dev_count == 0)
696                 seq_puts(m, "none\n");
697
698         seq_putc(m, '\n');
699         return 0;
700
701 }
702
703 const char *esas2r_info(struct Scsi_Host *sh)
704 {
705         struct esas2r_adapter *a = (struct esas2r_adapter *)sh->hostdata;
706         static char esas2r_info_str[512];
707
708         esas2r_log_dev(ESAS2R_LOG_INFO, &(sh->shost_gendev),
709                        "esas2r_info() called");
710
711         /*
712          * if we haven't done so already, register as a char driver
713          * and stick a node under "/proc/scsi/esas2r/ATTOnode"
714          */
715
716         if (esas2r_proc_major <= 0) {
717                 esas2r_proc_host = sh;
718
719                 esas2r_proc_major = register_chrdev(0, ESAS2R_DRVR_NAME,
720                                                     &esas2r_proc_fops);
721
722                 esas2r_log_dev(ESAS2R_LOG_DEBG, &(sh->shost_gendev),
723                                "register_chrdev (major %d)",
724                                esas2r_proc_major);
725
726                 if (esas2r_proc_major > 0) {
727                         struct proc_dir_entry *pde;
728
729                         pde = proc_create(ATTONODE_NAME, 0,
730                                           sh->hostt->proc_dir,
731                                           &esas2r_proc_fops);
732
733                         if (!pde) {
734                                 esas2r_log_dev(ESAS2R_LOG_WARN,
735                                                &(sh->shost_gendev),
736                                                "failed to create_proc_entry");
737                                 esas2r_proc_major = -1;
738                         }
739                 }
740         }
741
742         sprintf(esas2r_info_str,
743                 ESAS2R_LONGNAME " (bus 0x%02X, device 0x%02X, IRQ 0x%02X)"
744                 " driver version: "ESAS2R_VERSION_STR "  firmware version: "
745                 "%s\n",
746                 a->pcid->bus->number, a->pcid->devfn, a->pcid->irq,
747                 a->fw_rev[0] ? a->fw_rev : "(none)");
748
749         return esas2r_info_str;
750 }
751
752 /* Callback for building a request scatter/gather list */
753 static u32 get_physaddr_from_sgc(struct esas2r_sg_context *sgc, u64 *addr)
754 {
755         u32 len;
756
757         if (likely(sgc->cur_offset == sgc->exp_offset)) {
758                 /*
759                  * the normal case: caller used all bytes from previous call, so
760                  * expected offset is the same as the current offset.
761                  */
762
763                 if (sgc->sgel_count < sgc->num_sgel) {
764                         /* retrieve next segment, except for first time */
765                         if (sgc->exp_offset > (u8 *)0) {
766                                 /* advance current segment */
767                                 sgc->cur_sgel = sg_next(sgc->cur_sgel);
768                                 ++(sgc->sgel_count);
769                         }
770
771
772                         len = sg_dma_len(sgc->cur_sgel);
773                         (*addr) = sg_dma_address(sgc->cur_sgel);
774
775                         /* save the total # bytes returned to caller so far */
776                         sgc->exp_offset += len;
777
778                 } else {
779                         len = 0;
780                 }
781         } else if (sgc->cur_offset < sgc->exp_offset) {
782                 /*
783                  * caller did not use all bytes from previous call. need to
784                  * compute the address based on current segment.
785                  */
786
787                 len = sg_dma_len(sgc->cur_sgel);
788                 (*addr) = sg_dma_address(sgc->cur_sgel);
789
790                 sgc->exp_offset -= len;
791
792                 /* calculate PA based on prev segment address and offsets */
793                 *addr = *addr +
794                         (sgc->cur_offset - sgc->exp_offset);
795
796                 sgc->exp_offset += len;
797
798                 /* re-calculate length based on offset */
799                 len = lower_32_bits(
800                         sgc->exp_offset - sgc->cur_offset);
801         } else {   /* if ( sgc->cur_offset > sgc->exp_offset ) */
802                    /*
803                     * we don't expect the caller to skip ahead.
804                     * cur_offset will never exceed the len we return
805                     */
806                 len = 0;
807         }
808
809         return len;
810 }
811
812 int esas2r_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
813 {
814         struct esas2r_adapter *a =
815                 (struct esas2r_adapter *)cmd->device->host->hostdata;
816         struct esas2r_request *rq;
817         struct esas2r_sg_context sgc;
818         unsigned bufflen;
819
820         /* Assume success, if it fails we will fix the result later. */
821         cmd->result = DID_OK << 16;
822
823         if (unlikely(test_bit(AF_DEGRADED_MODE, &a->flags))) {
824                 cmd->result = DID_NO_CONNECT << 16;
825                 cmd->scsi_done(cmd);
826                 return 0;
827         }
828
829         rq = esas2r_alloc_request(a);
830         if (unlikely(rq == NULL)) {
831                 esas2r_debug("esas2r_alloc_request failed");
832                 return SCSI_MLQUEUE_HOST_BUSY;
833         }
834
835         rq->cmd = cmd;
836         bufflen = scsi_bufflen(cmd);
837
838         if (likely(bufflen != 0)) {
839                 if (cmd->sc_data_direction == DMA_TO_DEVICE)
840                         rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_WRD);
841                 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
842                         rq->vrq->scsi.flags |= cpu_to_le32(FCP_CMND_RDD);
843         }
844
845         memcpy(rq->vrq->scsi.cdb, cmd->cmnd, cmd->cmd_len);
846         rq->vrq->scsi.length = cpu_to_le32(bufflen);
847         rq->target_id = cmd->device->id;
848         rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
849         rq->sense_buf = cmd->sense_buffer;
850         rq->sense_len = SCSI_SENSE_BUFFERSIZE;
851
852         esas2r_sgc_init(&sgc, a, rq, NULL);
853
854         sgc.length = bufflen;
855         sgc.cur_offset = NULL;
856
857         sgc.cur_sgel = scsi_sglist(cmd);
858         sgc.exp_offset = NULL;
859         sgc.num_sgel = scsi_dma_map(cmd);
860         sgc.sgel_count = 0;
861
862         if (unlikely(sgc.num_sgel < 0)) {
863                 esas2r_free_request(a, rq);
864                 return SCSI_MLQUEUE_HOST_BUSY;
865         }
866
867         sgc.get_phys_addr = (PGETPHYSADDR)get_physaddr_from_sgc;
868
869         if (unlikely(!esas2r_build_sg_list(a, rq, &sgc))) {
870                 scsi_dma_unmap(cmd);
871                 esas2r_free_request(a, rq);
872                 return SCSI_MLQUEUE_HOST_BUSY;
873         }
874
875         esas2r_debug("start request %p to %d:%d\n", rq, (int)cmd->device->id,
876                      (int)cmd->device->lun);
877
878         esas2r_start_request(a, rq);
879
880         return 0;
881 }
882
883 static void complete_task_management_request(struct esas2r_adapter *a,
884                                              struct esas2r_request *rq)
885 {
886         (*rq->task_management_status_ptr) = rq->req_stat;
887         esas2r_free_request(a, rq);
888 }
889
890 /**
891  * Searches the specified queue for the specified queue for the command
892  * to abort.
893  *
894  * @param [in] a
895  * @param [in] abort_request
896  * @param [in] cmd
897  * t
898  * @return 0 on failure, 1 if command was not found, 2 if command was found
899  */
900 static int esas2r_check_active_queue(struct esas2r_adapter *a,
901                                      struct esas2r_request **abort_request,
902                                      struct scsi_cmnd *cmd,
903                                      struct list_head *queue)
904 {
905         bool found = false;
906         struct esas2r_request *ar = *abort_request;
907         struct esas2r_request *rq;
908         struct list_head *element, *next;
909
910         list_for_each_safe(element, next, queue) {
911
912                 rq = list_entry(element, struct esas2r_request, req_list);
913
914                 if (rq->cmd == cmd) {
915
916                         /* Found the request.  See what to do with it. */
917                         if (queue == &a->active_list) {
918                                 /*
919                                  * We are searching the active queue, which
920                                  * means that we need to send an abort request
921                                  * to the firmware.
922                                  */
923                                 ar = esas2r_alloc_request(a);
924                                 if (ar == NULL) {
925                                         esas2r_log_dev(ESAS2R_LOG_WARN,
926                                                        &(a->host->shost_gendev),
927                                                        "unable to allocate an abort request for cmd %p",
928                                                        cmd);
929                                         return 0; /* Failure */
930                                 }
931
932                                 /*
933                                  * Task management request must be formatted
934                                  * with a lock held.
935                                  */
936                                 ar->sense_len = 0;
937                                 ar->vrq->scsi.length = 0;
938                                 ar->target_id = rq->target_id;
939                                 ar->vrq->scsi.flags |= cpu_to_le32(
940                                         (u8)le32_to_cpu(rq->vrq->scsi.flags));
941
942                                 memset(ar->vrq->scsi.cdb, 0,
943                                        sizeof(ar->vrq->scsi.cdb));
944
945                                 ar->vrq->scsi.flags |= cpu_to_le32(
946                                         FCP_CMND_TRM);
947                                 ar->vrq->scsi.u.abort_handle =
948                                         rq->vrq->scsi.handle;
949                         } else {
950                                 /*
951                                  * The request is pending but not active on
952                                  * the firmware.  Just free it now and we'll
953                                  * report the successful abort below.
954                                  */
955                                 list_del_init(&rq->req_list);
956                                 esas2r_free_request(a, rq);
957                         }
958
959                         found = true;
960                         break;
961                 }
962
963         }
964
965         if (!found)
966                 return 1;       /* Not found */
967
968         return 2;               /* found */
969
970
971 }
972
973 int esas2r_eh_abort(struct scsi_cmnd *cmd)
974 {
975         struct esas2r_adapter *a =
976                 (struct esas2r_adapter *)cmd->device->host->hostdata;
977         struct esas2r_request *abort_request = NULL;
978         unsigned long flags;
979         struct list_head *queue;
980         int result;
981
982         esas2r_log(ESAS2R_LOG_INFO, "eh_abort (%p)", cmd);
983
984         if (test_bit(AF_DEGRADED_MODE, &a->flags)) {
985                 cmd->result = DID_ABORT << 16;
986
987                 scsi_set_resid(cmd, 0);
988
989                 cmd->scsi_done(cmd);
990
991                 return SUCCESS;
992         }
993
994         spin_lock_irqsave(&a->queue_lock, flags);
995
996         /*
997          * Run through the defer and active queues looking for the request
998          * to abort.
999          */
1000
1001         queue = &a->defer_list;
1002
1003 check_active_queue:
1004
1005         result = esas2r_check_active_queue(a, &abort_request, cmd, queue);
1006
1007         if (!result) {
1008                 spin_unlock_irqrestore(&a->queue_lock, flags);
1009                 return FAILED;
1010         } else if (result == 2 && (queue == &a->defer_list)) {
1011                 queue = &a->active_list;
1012                 goto check_active_queue;
1013         }
1014
1015         spin_unlock_irqrestore(&a->queue_lock, flags);
1016
1017         if (abort_request) {
1018                 u8 task_management_status = RS_PENDING;
1019
1020                 /*
1021                  * the request is already active, so we need to tell
1022                  * the firmware to abort it and wait for the response.
1023                  */
1024
1025                 abort_request->comp_cb = complete_task_management_request;
1026                 abort_request->task_management_status_ptr =
1027                         &task_management_status;
1028
1029                 esas2r_start_request(a, abort_request);
1030
1031                 if (atomic_read(&a->disable_cnt) == 0)
1032                         esas2r_do_deferred_processes(a);
1033
1034                 while (task_management_status == RS_PENDING)
1035                         msleep(10);
1036
1037                 /*
1038                  * Once we get here, the original request will have been
1039                  * completed by the firmware and the abort request will have
1040                  * been cleaned up.  we're done!
1041                  */
1042
1043                 return SUCCESS;
1044         }
1045
1046         /*
1047          * If we get here, either we found the inactive request and
1048          * freed it, or we didn't find it at all.  Either way, success!
1049          */
1050
1051         cmd->result = DID_ABORT << 16;
1052
1053         scsi_set_resid(cmd, 0);
1054
1055         cmd->scsi_done(cmd);
1056
1057         return SUCCESS;
1058 }
1059
1060 static int esas2r_host_bus_reset(struct scsi_cmnd *cmd, bool host_reset)
1061 {
1062         struct esas2r_adapter *a =
1063                 (struct esas2r_adapter *)cmd->device->host->hostdata;
1064
1065         if (test_bit(AF_DEGRADED_MODE, &a->flags))
1066                 return FAILED;
1067
1068         if (host_reset)
1069                 esas2r_reset_adapter(a);
1070         else
1071                 esas2r_reset_bus(a);
1072
1073         /* above call sets the AF_OS_RESET flag.  wait for it to clear. */
1074
1075         while (test_bit(AF_OS_RESET, &a->flags)) {
1076                 msleep(10);
1077
1078                 if (test_bit(AF_DEGRADED_MODE, &a->flags))
1079                         return FAILED;
1080         }
1081
1082         if (test_bit(AF_DEGRADED_MODE, &a->flags))
1083                 return FAILED;
1084
1085         return SUCCESS;
1086 }
1087
1088 int esas2r_host_reset(struct scsi_cmnd *cmd)
1089 {
1090         esas2r_log(ESAS2R_LOG_INFO, "host_reset (%p)", cmd);
1091
1092         return esas2r_host_bus_reset(cmd, true);
1093 }
1094
1095 int esas2r_bus_reset(struct scsi_cmnd *cmd)
1096 {
1097         esas2r_log(ESAS2R_LOG_INFO, "bus_reset (%p)", cmd);
1098
1099         return esas2r_host_bus_reset(cmd, false);
1100 }
1101
1102 static int esas2r_dev_targ_reset(struct scsi_cmnd *cmd, bool target_reset)
1103 {
1104         struct esas2r_adapter *a =
1105                 (struct esas2r_adapter *)cmd->device->host->hostdata;
1106         struct esas2r_request *rq;
1107         u8 task_management_status = RS_PENDING;
1108         bool completed;
1109
1110         if (test_bit(AF_DEGRADED_MODE, &a->flags))
1111                 return FAILED;
1112
1113 retry:
1114         rq = esas2r_alloc_request(a);
1115         if (rq == NULL) {
1116                 if (target_reset) {
1117                         esas2r_log(ESAS2R_LOG_CRIT,
1118                                    "unable to allocate a request for a "
1119                                    "target reset (%d)!",
1120                                    cmd->device->id);
1121                 } else {
1122                         esas2r_log(ESAS2R_LOG_CRIT,
1123                                    "unable to allocate a request for a "
1124                                    "device reset (%d:%llu)!",
1125                                    cmd->device->id,
1126                                    cmd->device->lun);
1127                 }
1128
1129
1130                 return FAILED;
1131         }
1132
1133         rq->target_id = cmd->device->id;
1134         rq->vrq->scsi.flags |= cpu_to_le32(cmd->device->lun);
1135         rq->req_stat = RS_PENDING;
1136
1137         rq->comp_cb = complete_task_management_request;
1138         rq->task_management_status_ptr = &task_management_status;
1139
1140         if (target_reset) {
1141                 esas2r_debug("issuing target reset (%p) to id %d", rq,
1142                              cmd->device->id);
1143                 completed = esas2r_send_task_mgmt(a, rq, 0x20);
1144         } else {
1145                 esas2r_debug("issuing device reset (%p) to id %d lun %d", rq,
1146                              cmd->device->id, cmd->device->lun);
1147                 completed = esas2r_send_task_mgmt(a, rq, 0x10);
1148         }
1149
1150         if (completed) {
1151                 /* Task management cmd completed right away, need to free it. */
1152
1153                 esas2r_free_request(a, rq);
1154         } else {
1155                 /*
1156                  * Wait for firmware to complete the request.  Completion
1157                  * callback will free it.
1158                  */
1159                 while (task_management_status == RS_PENDING)
1160                         msleep(10);
1161         }
1162
1163         if (test_bit(AF_DEGRADED_MODE, &a->flags))
1164                 return FAILED;
1165
1166         if (task_management_status == RS_BUSY) {
1167                 /*
1168                  * Busy, probably because we are flashing.  Wait a bit and
1169                  * try again.
1170                  */
1171                 msleep(100);
1172                 goto retry;
1173         }
1174
1175         return SUCCESS;
1176 }
1177
1178 int esas2r_device_reset(struct scsi_cmnd *cmd)
1179 {
1180         esas2r_log(ESAS2R_LOG_INFO, "device_reset (%p)", cmd);
1181
1182         return esas2r_dev_targ_reset(cmd, false);
1183
1184 }
1185
1186 int esas2r_target_reset(struct scsi_cmnd *cmd)
1187 {
1188         esas2r_log(ESAS2R_LOG_INFO, "target_reset (%p)", cmd);
1189
1190         return esas2r_dev_targ_reset(cmd, true);
1191 }
1192
1193 void esas2r_log_request_failure(struct esas2r_adapter *a,
1194                                 struct esas2r_request *rq)
1195 {
1196         u8 reqstatus = rq->req_stat;
1197
1198         if (reqstatus == RS_SUCCESS)
1199                 return;
1200
1201         if (rq->vrq->scsi.function == VDA_FUNC_SCSI) {
1202                 if (reqstatus == RS_SCSI_ERROR) {
1203                         if (rq->func_rsp.scsi_rsp.sense_len >= 13) {
1204                                 esas2r_log(ESAS2R_LOG_WARN,
1205                                            "request failure - SCSI error %x ASC:%x ASCQ:%x CDB:%x",
1206                                            rq->sense_buf[2], rq->sense_buf[12],
1207                                            rq->sense_buf[13],
1208                                            rq->vrq->scsi.cdb[0]);
1209                         } else {
1210                                 esas2r_log(ESAS2R_LOG_WARN,
1211                                            "request failure - SCSI error CDB:%x\n",
1212                                            rq->vrq->scsi.cdb[0]);
1213                         }
1214                 } else if ((rq->vrq->scsi.cdb[0] != INQUIRY
1215                             && rq->vrq->scsi.cdb[0] != REPORT_LUNS)
1216                            || (reqstatus != RS_SEL
1217                                && reqstatus != RS_SEL2)) {
1218                         if ((reqstatus == RS_UNDERRUN) &&
1219                             (rq->vrq->scsi.cdb[0] == INQUIRY)) {
1220                                 /* Don't log inquiry underruns */
1221                         } else {
1222                                 esas2r_log(ESAS2R_LOG_WARN,
1223                                            "request failure - cdb:%x reqstatus:%d target:%d",
1224                                            rq->vrq->scsi.cdb[0], reqstatus,
1225                                            rq->target_id);
1226                         }
1227                 }
1228         }
1229 }
1230
1231 void esas2r_wait_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1232 {
1233         u32 starttime;
1234         u32 timeout;
1235
1236         starttime = jiffies_to_msecs(jiffies);
1237         timeout = rq->timeout ? rq->timeout : 5000;
1238
1239         while (true) {
1240                 esas2r_polled_interrupt(a);
1241
1242                 if (rq->req_stat != RS_STARTED)
1243                         break;
1244
1245                 schedule_timeout_interruptible(msecs_to_jiffies(100));
1246
1247                 if ((jiffies_to_msecs(jiffies) - starttime) > timeout) {
1248                         esas2r_hdebug("request TMO");
1249                         esas2r_bugon();
1250
1251                         rq->req_stat = RS_TIMEOUT;
1252
1253                         esas2r_local_reset_adapter(a);
1254                         return;
1255                 }
1256         }
1257 }
1258
1259 u32 esas2r_map_data_window(struct esas2r_adapter *a, u32 addr_lo)
1260 {
1261         u32 offset = addr_lo & (MW_DATA_WINDOW_SIZE - 1);
1262         u32 base = addr_lo & -(signed int)MW_DATA_WINDOW_SIZE;
1263
1264         if (a->window_base != base) {
1265                 esas2r_write_register_dword(a, MVR_PCI_WIN1_REMAP,
1266                                             base | MVRPW1R_ENABLE);
1267                 esas2r_flush_register_dword(a, MVR_PCI_WIN1_REMAP);
1268                 a->window_base = base;
1269         }
1270
1271         return offset;
1272 }
1273
1274 /* Read a block of data from chip memory */
1275 bool esas2r_read_mem_block(struct esas2r_adapter *a,
1276                            void *to,
1277                            u32 from,
1278                            u32 size)
1279 {
1280         u8 *end = (u8 *)to;
1281
1282         while (size) {
1283                 u32 len;
1284                 u32 offset;
1285                 u32 iatvr;
1286
1287                 iatvr = (from & -(signed int)MW_DATA_WINDOW_SIZE);
1288
1289                 esas2r_map_data_window(a, iatvr);
1290
1291                 offset = from & (MW_DATA_WINDOW_SIZE - 1);
1292                 len = size;
1293
1294                 if (len > MW_DATA_WINDOW_SIZE - offset)
1295                         len = MW_DATA_WINDOW_SIZE - offset;
1296
1297                 from += len;
1298                 size -= len;
1299
1300                 while (len--) {
1301                         *end++ = esas2r_read_data_byte(a, offset);
1302                         offset++;
1303                 }
1304         }
1305
1306         return true;
1307 }
1308
1309 void esas2r_nuxi_mgt_data(u8 function, void *data)
1310 {
1311         struct atto_vda_grp_info *g;
1312         struct atto_vda_devinfo *d;
1313         struct atto_vdapart_info *p;
1314         struct atto_vda_dh_info *h;
1315         struct atto_vda_metrics_info *m;
1316         struct atto_vda_schedule_info *s;
1317         struct atto_vda_buzzer_info *b;
1318         u8 i;
1319
1320         switch (function) {
1321         case VDAMGT_BUZZER_INFO:
1322         case VDAMGT_BUZZER_SET:
1323
1324                 b = (struct atto_vda_buzzer_info *)data;
1325
1326                 b->duration = le32_to_cpu(b->duration);
1327                 break;
1328
1329         case VDAMGT_SCHEDULE_INFO:
1330         case VDAMGT_SCHEDULE_EVENT:
1331
1332                 s = (struct atto_vda_schedule_info *)data;
1333
1334                 s->id = le32_to_cpu(s->id);
1335
1336                 break;
1337
1338         case VDAMGT_DEV_INFO:
1339         case VDAMGT_DEV_CLEAN:
1340         case VDAMGT_DEV_PT_INFO:
1341         case VDAMGT_DEV_FEATURES:
1342         case VDAMGT_DEV_PT_FEATURES:
1343         case VDAMGT_DEV_OPERATION:
1344
1345                 d = (struct atto_vda_devinfo *)data;
1346
1347                 d->capacity = le64_to_cpu(d->capacity);
1348                 d->block_size = le32_to_cpu(d->block_size);
1349                 d->ses_dev_index = le16_to_cpu(d->ses_dev_index);
1350                 d->target_id = le16_to_cpu(d->target_id);
1351                 d->lun = le16_to_cpu(d->lun);
1352                 d->features = le16_to_cpu(d->features);
1353                 break;
1354
1355         case VDAMGT_GRP_INFO:
1356         case VDAMGT_GRP_CREATE:
1357         case VDAMGT_GRP_DELETE:
1358         case VDAMGT_ADD_STORAGE:
1359         case VDAMGT_MEMBER_ADD:
1360         case VDAMGT_GRP_COMMIT:
1361         case VDAMGT_GRP_REBUILD:
1362         case VDAMGT_GRP_COMMIT_INIT:
1363         case VDAMGT_QUICK_RAID:
1364         case VDAMGT_GRP_FEATURES:
1365         case VDAMGT_GRP_COMMIT_INIT_AUTOMAP:
1366         case VDAMGT_QUICK_RAID_INIT_AUTOMAP:
1367         case VDAMGT_SPARE_LIST:
1368         case VDAMGT_SPARE_ADD:
1369         case VDAMGT_SPARE_REMOVE:
1370         case VDAMGT_LOCAL_SPARE_ADD:
1371         case VDAMGT_GRP_OPERATION:
1372
1373                 g = (struct atto_vda_grp_info *)data;
1374
1375                 g->capacity = le64_to_cpu(g->capacity);
1376                 g->block_size = le32_to_cpu(g->block_size);
1377                 g->interleave = le32_to_cpu(g->interleave);
1378                 g->features = le16_to_cpu(g->features);
1379
1380                 for (i = 0; i < 32; i++)
1381                         g->members[i] = le16_to_cpu(g->members[i]);
1382
1383                 break;
1384
1385         case VDAMGT_PART_INFO:
1386         case VDAMGT_PART_MAP:
1387         case VDAMGT_PART_UNMAP:
1388         case VDAMGT_PART_AUTOMAP:
1389         case VDAMGT_PART_SPLIT:
1390         case VDAMGT_PART_MERGE:
1391
1392                 p = (struct atto_vdapart_info *)data;
1393
1394                 p->part_size = le64_to_cpu(p->part_size);
1395                 p->start_lba = le32_to_cpu(p->start_lba);
1396                 p->block_size = le32_to_cpu(p->block_size);
1397                 p->target_id = le16_to_cpu(p->target_id);
1398                 break;
1399
1400         case VDAMGT_DEV_HEALTH_REQ:
1401
1402                 h = (struct atto_vda_dh_info *)data;
1403
1404                 h->med_defect_cnt = le32_to_cpu(h->med_defect_cnt);
1405                 h->info_exc_cnt = le32_to_cpu(h->info_exc_cnt);
1406                 break;
1407
1408         case VDAMGT_DEV_METRICS:
1409
1410                 m = (struct atto_vda_metrics_info *)data;
1411
1412                 for (i = 0; i < 32; i++)
1413                         m->dev_indexes[i] = le16_to_cpu(m->dev_indexes[i]);
1414
1415                 break;
1416
1417         default:
1418                 break;
1419         }
1420 }
1421
1422 void esas2r_nuxi_cfg_data(u8 function, void *data)
1423 {
1424         struct atto_vda_cfg_init *ci;
1425
1426         switch (function) {
1427         case VDA_CFG_INIT:
1428         case VDA_CFG_GET_INIT:
1429         case VDA_CFG_GET_INIT2:
1430
1431                 ci = (struct atto_vda_cfg_init *)data;
1432
1433                 ci->date_time.year = le16_to_cpu(ci->date_time.year);
1434                 ci->sgl_page_size = le32_to_cpu(ci->sgl_page_size);
1435                 ci->vda_version = le32_to_cpu(ci->vda_version);
1436                 ci->epoch_time = le32_to_cpu(ci->epoch_time);
1437                 ci->ioctl_tunnel = le32_to_cpu(ci->ioctl_tunnel);
1438                 ci->num_targets_backend = le32_to_cpu(ci->num_targets_backend);
1439                 break;
1440
1441         default:
1442                 break;
1443         }
1444 }
1445
1446 void esas2r_nuxi_ae_data(union atto_vda_ae *ae)
1447 {
1448         struct atto_vda_ae_raid *r = &ae->raid;
1449         struct atto_vda_ae_lu *l = &ae->lu;
1450
1451         switch (ae->hdr.bytype) {
1452         case VDAAE_HDR_TYPE_RAID:
1453
1454                 r->dwflags = le32_to_cpu(r->dwflags);
1455                 break;
1456
1457         case VDAAE_HDR_TYPE_LU:
1458
1459                 l->dwevent = le32_to_cpu(l->dwevent);
1460                 l->wphys_target_id = le16_to_cpu(l->wphys_target_id);
1461                 l->id.tgtlun.wtarget_id = le16_to_cpu(l->id.tgtlun.wtarget_id);
1462
1463                 if (l->hdr.bylength >= offsetof(struct atto_vda_ae_lu, id)
1464                     + sizeof(struct atto_vda_ae_lu_tgt_lun_raid)) {
1465                         l->id.tgtlun_raid.dwinterleave
1466                                 = le32_to_cpu(l->id.tgtlun_raid.dwinterleave);
1467                         l->id.tgtlun_raid.dwblock_size
1468                                 = le32_to_cpu(l->id.tgtlun_raid.dwblock_size);
1469                 }
1470
1471                 break;
1472
1473         case VDAAE_HDR_TYPE_DISK:
1474         default:
1475                 break;
1476         }
1477 }
1478
1479 void esas2r_free_request(struct esas2r_adapter *a, struct esas2r_request *rq)
1480 {
1481         unsigned long flags;
1482
1483         esas2r_rq_destroy_request(rq, a);
1484         spin_lock_irqsave(&a->request_lock, flags);
1485         list_add(&rq->comp_list, &a->avail_request);
1486         spin_unlock_irqrestore(&a->request_lock, flags);
1487 }
1488
1489 struct esas2r_request *esas2r_alloc_request(struct esas2r_adapter *a)
1490 {
1491         struct esas2r_request *rq;
1492         unsigned long flags;
1493
1494         spin_lock_irqsave(&a->request_lock, flags);
1495
1496         if (unlikely(list_empty(&a->avail_request))) {
1497                 spin_unlock_irqrestore(&a->request_lock, flags);
1498                 return NULL;
1499         }
1500
1501         rq = list_first_entry(&a->avail_request, struct esas2r_request,
1502                               comp_list);
1503         list_del(&rq->comp_list);
1504         spin_unlock_irqrestore(&a->request_lock, flags);
1505         esas2r_rq_init_request(rq, a);
1506
1507         return rq;
1508
1509 }
1510
1511 void esas2r_complete_request_cb(struct esas2r_adapter *a,
1512                                 struct esas2r_request *rq)
1513 {
1514         esas2r_debug("completing request %p\n", rq);
1515
1516         scsi_dma_unmap(rq->cmd);
1517
1518         if (unlikely(rq->req_stat != RS_SUCCESS)) {
1519                 esas2r_debug("[%x STATUS %x:%x (%x)]", rq->target_id,
1520                              rq->req_stat,
1521                              rq->func_rsp.scsi_rsp.scsi_stat,
1522                              rq->cmd);
1523
1524                 rq->cmd->result =
1525                         ((esas2r_req_status_to_error(rq->req_stat) << 16)
1526                          | (rq->func_rsp.scsi_rsp.scsi_stat & STATUS_MASK));
1527
1528                 if (rq->req_stat == RS_UNDERRUN)
1529                         scsi_set_resid(rq->cmd,
1530                                        le32_to_cpu(rq->func_rsp.scsi_rsp.
1531                                                    residual_length));
1532                 else
1533                         scsi_set_resid(rq->cmd, 0);
1534         }
1535
1536         rq->cmd->scsi_done(rq->cmd);
1537
1538         esas2r_free_request(a, rq);
1539 }
1540
1541 /* Run tasklet to handle stuff outside of interrupt context. */
1542 void esas2r_adapter_tasklet(unsigned long context)
1543 {
1544         struct esas2r_adapter *a = (struct esas2r_adapter *)context;
1545
1546         if (unlikely(test_bit(AF2_TIMER_TICK, &a->flags2))) {
1547                 clear_bit(AF2_TIMER_TICK, &a->flags2);
1548                 esas2r_timer_tick(a);
1549         }
1550
1551         if (likely(test_bit(AF2_INT_PENDING, &a->flags2))) {
1552                 clear_bit(AF2_INT_PENDING, &a->flags2);
1553                 esas2r_adapter_interrupt(a);
1554         }
1555
1556         if (esas2r_is_tasklet_pending(a))
1557                 esas2r_do_tasklet_tasks(a);
1558
1559         if (esas2r_is_tasklet_pending(a)
1560             || (test_bit(AF2_INT_PENDING, &a->flags2))
1561             || (test_bit(AF2_TIMER_TICK, &a->flags2))) {
1562                 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1563                 esas2r_schedule_tasklet(a);
1564         } else {
1565                 clear_bit(AF_TASKLET_SCHEDULED, &a->flags);
1566         }
1567 }
1568
1569 static void esas2r_timer_callback(struct timer_list *t);
1570
1571 void esas2r_kickoff_timer(struct esas2r_adapter *a)
1572 {
1573         timer_setup(&a->timer, esas2r_timer_callback, 0);
1574
1575         a->timer.expires = jiffies +
1576                            msecs_to_jiffies(100);
1577
1578         add_timer(&a->timer);
1579 }
1580
1581 static void esas2r_timer_callback(struct timer_list *t)
1582 {
1583         struct esas2r_adapter *a = from_timer(a, t, timer);
1584
1585         set_bit(AF2_TIMER_TICK, &a->flags2);
1586
1587         esas2r_schedule_tasklet(a);
1588
1589         esas2r_kickoff_timer(a);
1590 }
1591
1592 /*
1593  * Firmware events need to be handled outside of interrupt context
1594  * so we schedule a delayed_work to handle them.
1595  */
1596
1597 static void
1598 esas2r_free_fw_event(struct esas2r_fw_event_work *fw_event)
1599 {
1600         unsigned long flags;
1601         struct esas2r_adapter *a = fw_event->a;
1602
1603         spin_lock_irqsave(&a->fw_event_lock, flags);
1604         list_del(&fw_event->list);
1605         kfree(fw_event);
1606         spin_unlock_irqrestore(&a->fw_event_lock, flags);
1607 }
1608
1609 void
1610 esas2r_fw_event_off(struct esas2r_adapter *a)
1611 {
1612         unsigned long flags;
1613
1614         spin_lock_irqsave(&a->fw_event_lock, flags);
1615         a->fw_events_off = 1;
1616         spin_unlock_irqrestore(&a->fw_event_lock, flags);
1617 }
1618
1619 void
1620 esas2r_fw_event_on(struct esas2r_adapter *a)
1621 {
1622         unsigned long flags;
1623
1624         spin_lock_irqsave(&a->fw_event_lock, flags);
1625         a->fw_events_off = 0;
1626         spin_unlock_irqrestore(&a->fw_event_lock, flags);
1627 }
1628
1629 static void esas2r_add_device(struct esas2r_adapter *a, u16 target_id)
1630 {
1631         int ret;
1632         struct scsi_device *scsi_dev;
1633
1634         scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1635
1636         if (scsi_dev) {
1637                 esas2r_log_dev(
1638                         ESAS2R_LOG_WARN,
1639                         &(scsi_dev->
1640                           sdev_gendev),
1641                         "scsi device already exists at id %d", target_id);
1642
1643                 scsi_device_put(scsi_dev);
1644         } else {
1645                 esas2r_log_dev(
1646                         ESAS2R_LOG_INFO,
1647                         &(a->host->
1648                           shost_gendev),
1649                         "scsi_add_device() called for 0:%d:0",
1650                         target_id);
1651
1652                 ret = scsi_add_device(a->host, 0, target_id, 0);
1653                 if (ret) {
1654                         esas2r_log_dev(
1655                                 ESAS2R_LOG_CRIT,
1656                                 &(a->host->
1657                                   shost_gendev),
1658                                 "scsi_add_device failed with %d for id %d",
1659                                 ret, target_id);
1660                 }
1661         }
1662 }
1663
1664 static void esas2r_remove_device(struct esas2r_adapter *a, u16 target_id)
1665 {
1666         struct scsi_device *scsi_dev;
1667
1668         scsi_dev = scsi_device_lookup(a->host, 0, target_id, 0);
1669
1670         if (scsi_dev) {
1671                 scsi_device_set_state(scsi_dev, SDEV_OFFLINE);
1672
1673                 esas2r_log_dev(
1674                         ESAS2R_LOG_INFO,
1675                         &(scsi_dev->
1676                           sdev_gendev),
1677                         "scsi_remove_device() called for 0:%d:0",
1678                         target_id);
1679
1680                 scsi_remove_device(scsi_dev);
1681
1682                 esas2r_log_dev(
1683                         ESAS2R_LOG_INFO,
1684                         &(scsi_dev->
1685                           sdev_gendev),
1686                         "scsi_device_put() called");
1687
1688                 scsi_device_put(scsi_dev);
1689         } else {
1690                 esas2r_log_dev(
1691                         ESAS2R_LOG_WARN,
1692                         &(a->host->shost_gendev),
1693                         "no target found at id %d",
1694                         target_id);
1695         }
1696 }
1697
1698 /*
1699  * Sends a firmware asynchronous event to anyone who happens to be
1700  * listening on the defined ATTO VDA event ports.
1701  */
1702 static void esas2r_send_ae_event(struct esas2r_fw_event_work *fw_event)
1703 {
1704         struct esas2r_vda_ae *ae = (struct esas2r_vda_ae *)fw_event->data;
1705         char *type;
1706
1707         switch (ae->vda_ae.hdr.bytype) {
1708         case VDAAE_HDR_TYPE_RAID:
1709                 type = "RAID group state change";
1710                 break;
1711
1712         case VDAAE_HDR_TYPE_LU:
1713                 type = "Mapped destination LU change";
1714                 break;
1715
1716         case VDAAE_HDR_TYPE_DISK:
1717                 type = "Physical disk inventory change";
1718                 break;
1719
1720         case VDAAE_HDR_TYPE_RESET:
1721                 type = "Firmware reset";
1722                 break;
1723
1724         case VDAAE_HDR_TYPE_LOG_INFO:
1725                 type = "Event Log message (INFO level)";
1726                 break;
1727
1728         case VDAAE_HDR_TYPE_LOG_WARN:
1729                 type = "Event Log message (WARN level)";
1730                 break;
1731
1732         case VDAAE_HDR_TYPE_LOG_CRIT:
1733                 type = "Event Log message (CRIT level)";
1734                 break;
1735
1736         case VDAAE_HDR_TYPE_LOG_FAIL:
1737                 type = "Event Log message (FAIL level)";
1738                 break;
1739
1740         case VDAAE_HDR_TYPE_NVC:
1741                 type = "NVCache change";
1742                 break;
1743
1744         case VDAAE_HDR_TYPE_TLG_INFO:
1745                 type = "Time stamped log message (INFO level)";
1746                 break;
1747
1748         case VDAAE_HDR_TYPE_TLG_WARN:
1749                 type = "Time stamped log message (WARN level)";
1750                 break;
1751
1752         case VDAAE_HDR_TYPE_TLG_CRIT:
1753                 type = "Time stamped log message (CRIT level)";
1754                 break;
1755
1756         case VDAAE_HDR_TYPE_PWRMGT:
1757                 type = "Power management";
1758                 break;
1759
1760         case VDAAE_HDR_TYPE_MUTE:
1761                 type = "Mute button pressed";
1762                 break;
1763
1764         case VDAAE_HDR_TYPE_DEV:
1765                 type = "Device attribute change";
1766                 break;
1767
1768         default:
1769                 type = "Unknown";
1770                 break;
1771         }
1772
1773         esas2r_log(ESAS2R_LOG_WARN,
1774                    "An async event of type \"%s\" was received from the firmware.  The event contents are:",
1775                    type);
1776         esas2r_log_hexdump(ESAS2R_LOG_WARN, &ae->vda_ae,
1777                            ae->vda_ae.hdr.bylength);
1778
1779 }
1780
1781 static void
1782 esas2r_firmware_event_work(struct work_struct *work)
1783 {
1784         struct esas2r_fw_event_work *fw_event =
1785                 container_of(work, struct esas2r_fw_event_work, work.work);
1786
1787         struct esas2r_adapter *a = fw_event->a;
1788
1789         u16 target_id = *(u16 *)&fw_event->data[0];
1790
1791         if (a->fw_events_off)
1792                 goto done;
1793
1794         switch (fw_event->type) {
1795         case fw_event_null:
1796                 break; /* do nothing */
1797
1798         case fw_event_lun_change:
1799                 esas2r_remove_device(a, target_id);
1800                 esas2r_add_device(a, target_id);
1801                 break;
1802
1803         case fw_event_present:
1804                 esas2r_add_device(a, target_id);
1805                 break;
1806
1807         case fw_event_not_present:
1808                 esas2r_remove_device(a, target_id);
1809                 break;
1810
1811         case fw_event_vda_ae:
1812                 esas2r_send_ae_event(fw_event);
1813                 break;
1814         }
1815
1816 done:
1817         esas2r_free_fw_event(fw_event);
1818 }
1819
1820 void esas2r_queue_fw_event(struct esas2r_adapter *a,
1821                            enum fw_event_type type,
1822                            void *data,
1823                            int data_sz)
1824 {
1825         struct esas2r_fw_event_work *fw_event;
1826         unsigned long flags;
1827
1828         fw_event = kzalloc(sizeof(struct esas2r_fw_event_work), GFP_ATOMIC);
1829         if (!fw_event) {
1830                 esas2r_log(ESAS2R_LOG_WARN,
1831                            "esas2r_queue_fw_event failed to alloc");
1832                 return;
1833         }
1834
1835         if (type == fw_event_vda_ae) {
1836                 struct esas2r_vda_ae *ae =
1837                         (struct esas2r_vda_ae *)fw_event->data;
1838
1839                 ae->signature = ESAS2R_VDA_EVENT_SIG;
1840                 ae->bus_number = a->pcid->bus->number;
1841                 ae->devfn = a->pcid->devfn;
1842                 memcpy(&ae->vda_ae, data, sizeof(ae->vda_ae));
1843         } else {
1844                 memcpy(fw_event->data, data, data_sz);
1845         }
1846
1847         fw_event->type = type;
1848         fw_event->a = a;
1849
1850         spin_lock_irqsave(&a->fw_event_lock, flags);
1851         list_add_tail(&fw_event->list, &a->fw_event_list);
1852         INIT_DELAYED_WORK(&fw_event->work, esas2r_firmware_event_work);
1853         queue_delayed_work_on(
1854                 smp_processor_id(), a->fw_event_q, &fw_event->work,
1855                 msecs_to_jiffies(1));
1856         spin_unlock_irqrestore(&a->fw_event_lock, flags);
1857 }
1858
1859 void esas2r_target_state_changed(struct esas2r_adapter *a, u16 targ_id,
1860                                  u8 state)
1861 {
1862         if (state == TS_LUN_CHANGE)
1863                 esas2r_queue_fw_event(a, fw_event_lun_change, &targ_id,
1864                                       sizeof(targ_id));
1865         else if (state == TS_PRESENT)
1866                 esas2r_queue_fw_event(a, fw_event_present, &targ_id,
1867                                       sizeof(targ_id));
1868         else if (state == TS_NOT_PRESENT)
1869                 esas2r_queue_fw_event(a, fw_event_not_present, &targ_id,
1870                                       sizeof(targ_id));
1871 }
1872
1873 /* Translate status to a Linux SCSI mid-layer error code */
1874 int esas2r_req_status_to_error(u8 req_stat)
1875 {
1876         switch (req_stat) {
1877         case RS_OVERRUN:
1878         case RS_UNDERRUN:
1879         case RS_SUCCESS:
1880         /*
1881          * NOTE: SCSI mid-layer wants a good status for a SCSI error, because
1882          *       it will check the scsi_stat value in the completion anyway.
1883          */
1884         case RS_SCSI_ERROR:
1885                 return DID_OK;
1886
1887         case RS_SEL:
1888         case RS_SEL2:
1889                 return DID_NO_CONNECT;
1890
1891         case RS_RESET:
1892                 return DID_RESET;
1893
1894         case RS_ABORTED:
1895                 return DID_ABORT;
1896
1897         case RS_BUSY:
1898                 return DID_BUS_BUSY;
1899         }
1900
1901         /* everything else is just an error. */
1902
1903         return DID_ERROR;
1904 }
1905
1906 module_init(esas2r_init);
1907 module_exit(esas2r_exit);