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