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