blob: fc9a2f27aaf9e7665453970f085639cd4bc50cb2 [file] [log] [blame]
adam radfordf6191062009-10-23 14:52:33 -07001/*
2 3w-sas.c -- LSI 3ware SAS/SATA-RAID Controller device driver for Linux.
3
adam radford2c9bce52016-12-09 11:08:05 -08004 Written By: Adam Radford <aradford@gmail.com>
adam radfordf6191062009-10-23 14:52:33 -07005
6 Copyright (C) 2009 LSI Corporation.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; version 2 of the License.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 NO WARRANTY
18 THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
19 CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
20 LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
21 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
22 solely responsible for determining the appropriateness of using and
23 distributing the Program and assumes all risks associated with its
24 exercise of rights under this Agreement, including but not limited to
25 the risks and costs of program errors, damage to or loss of data,
26 programs or equipment, and unavailability or interruption of operations.
27
28 DISCLAIMER OF LIABILITY
29 NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
30 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
34 USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
35 HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
36
37 You should have received a copy of the GNU General Public License
38 along with this program; if not, write to the Free Software
39 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40
41 Controllers supported by this driver:
42
43 LSI 3ware 9750 6Gb/s SAS/SATA-RAID
44
45 Bugs/Comments/Suggestions should be mailed to:
adam radford2c9bce52016-12-09 11:08:05 -080046 aradford@gmail.com
adam radfordf6191062009-10-23 14:52:33 -070047
48 History
49 -------
50 3.26.02.000 - Initial driver release.
51*/
52
53#include <linux/module.h>
54#include <linux/reboot.h>
55#include <linux/spinlock.h>
56#include <linux/interrupt.h>
57#include <linux/moduleparam.h>
58#include <linux/errno.h>
59#include <linux/types.h>
60#include <linux/delay.h>
61#include <linux/pci.h>
62#include <linux/time.h>
63#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090064#include <linux/slab.h>
adam radfordf6191062009-10-23 14:52:33 -070065#include <asm/io.h>
66#include <asm/irq.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080067#include <linux/uaccess.h>
adam radfordf6191062009-10-23 14:52:33 -070068#include <scsi/scsi.h>
69#include <scsi/scsi_host.h>
70#include <scsi/scsi_tcq.h>
71#include <scsi/scsi_cmnd.h>
72#include "3w-sas.h"
73
74/* Globals */
75#define TW_DRIVER_VERSION "3.26.02.000"
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020076static DEFINE_MUTEX(twl_chrdev_mutex);
adam radfordf6191062009-10-23 14:52:33 -070077static TW_Device_Extension *twl_device_extension_list[TW_MAX_SLOT];
78static unsigned int twl_device_extension_count;
79static int twl_major = -1;
80extern struct timezone sys_tz;
81
82/* Module parameters */
83MODULE_AUTHOR ("LSI");
84MODULE_DESCRIPTION ("LSI 3ware SAS/SATA-RAID Linux Driver");
85MODULE_LICENSE("GPL");
86MODULE_VERSION(TW_DRIVER_VERSION);
87
88static int use_msi;
89module_param(use_msi, int, S_IRUGO);
90MODULE_PARM_DESC(use_msi, "Use Message Signaled Interrupts. Default: 0");
91
92/* Function prototypes */
93static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset);
94
95/* Functions */
96
97/* This function returns AENs through sysfs */
Chris Wright2c3c8be2010-05-12 18:28:57 -070098static ssize_t twl_sysfs_aen_read(struct file *filp, struct kobject *kobj,
adam radfordf6191062009-10-23 14:52:33 -070099 struct bin_attribute *bin_attr,
100 char *outbuf, loff_t offset, size_t count)
101{
102 struct device *dev = container_of(kobj, struct device, kobj);
103 struct Scsi_Host *shost = class_to_shost(dev);
104 TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
105 unsigned long flags = 0;
106 ssize_t ret;
107
108 if (!capable(CAP_SYS_ADMIN))
109 return -EACCES;
110
111 spin_lock_irqsave(tw_dev->host->host_lock, flags);
112 ret = memory_read_from_buffer(outbuf, count, &offset, tw_dev->event_queue[0], sizeof(TW_Event) * TW_Q_LENGTH);
113 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
114
115 return ret;
116} /* End twl_sysfs_aen_read() */
117
118/* aen_read sysfs attribute initializer */
119static struct bin_attribute twl_sysfs_aen_read_attr = {
120 .attr = {
121 .name = "3ware_aen_read",
122 .mode = S_IRUSR,
Hannes Reinecke17896712021-01-13 10:04:29 +0100123 },
adam radfordf6191062009-10-23 14:52:33 -0700124 .size = 0,
125 .read = twl_sysfs_aen_read
126};
127
128/* This function returns driver compatibility info through sysfs */
Chris Wright2c3c8be2010-05-12 18:28:57 -0700129static ssize_t twl_sysfs_compat_info(struct file *filp, struct kobject *kobj,
adam radfordf6191062009-10-23 14:52:33 -0700130 struct bin_attribute *bin_attr,
131 char *outbuf, loff_t offset, size_t count)
132{
133 struct device *dev = container_of(kobj, struct device, kobj);
134 struct Scsi_Host *shost = class_to_shost(dev);
135 TW_Device_Extension *tw_dev = (TW_Device_Extension *)shost->hostdata;
136 unsigned long flags = 0;
137 ssize_t ret;
138
139 if (!capable(CAP_SYS_ADMIN))
140 return -EACCES;
141
142 spin_lock_irqsave(tw_dev->host->host_lock, flags);
143 ret = memory_read_from_buffer(outbuf, count, &offset, &tw_dev->tw_compat_info, sizeof(TW_Compatibility_Info));
144 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
145
146 return ret;
147} /* End twl_sysfs_compat_info() */
148
149/* compat_info sysfs attribute initializer */
150static struct bin_attribute twl_sysfs_compat_info_attr = {
151 .attr = {
152 .name = "3ware_compat_info",
153 .mode = S_IRUSR,
Hannes Reinecke17896712021-01-13 10:04:29 +0100154 },
adam radfordf6191062009-10-23 14:52:33 -0700155 .size = 0,
156 .read = twl_sysfs_compat_info
157};
158
159/* Show some statistics about the card */
160static ssize_t twl_show_stats(struct device *dev,
161 struct device_attribute *attr, char *buf)
162{
163 struct Scsi_Host *host = class_to_shost(dev);
164 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
165 unsigned long flags = 0;
166 ssize_t len;
167
168 spin_lock_irqsave(tw_dev->host->host_lock, flags);
169 len = snprintf(buf, PAGE_SIZE, "3w-sas Driver version: %s\n"
170 "Current commands posted: %4d\n"
171 "Max commands posted: %4d\n"
172 "Last sgl length: %4d\n"
173 "Max sgl length: %4d\n"
174 "Last sector count: %4d\n"
175 "Max sector count: %4d\n"
176 "SCSI Host Resets: %4d\n"
Hannes Reinecke17896712021-01-13 10:04:29 +0100177 "AEN's: %4d\n",
adam radfordf6191062009-10-23 14:52:33 -0700178 TW_DRIVER_VERSION,
179 tw_dev->posted_request_count,
180 tw_dev->max_posted_request_count,
181 tw_dev->sgl_entries,
182 tw_dev->max_sgl_entries,
183 tw_dev->sector_count,
184 tw_dev->max_sector_count,
185 tw_dev->num_resets,
186 tw_dev->aen_count);
187 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
188 return len;
189} /* End twl_show_stats() */
190
adam radfordf6191062009-10-23 14:52:33 -0700191/* stats sysfs attribute initializer */
192static struct device_attribute twl_host_stats_attr = {
193 .attr = {
Hannes Reinecke17896712021-01-13 10:04:29 +0100194 .name = "3ware_stats",
adam radfordf6191062009-10-23 14:52:33 -0700195 .mode = S_IRUGO,
196 },
197 .show = twl_show_stats
198};
199
200/* Host attributes initializer */
201static struct device_attribute *twl_host_attrs[] = {
202 &twl_host_stats_attr,
203 NULL,
204};
205
206/* This function will look up an AEN severity string */
207static char *twl_aen_severity_lookup(unsigned char severity_code)
208{
209 char *retval = NULL;
210
211 if ((severity_code < (unsigned char) TW_AEN_SEVERITY_ERROR) ||
212 (severity_code > (unsigned char) TW_AEN_SEVERITY_DEBUG))
213 goto out;
214
215 retval = twl_aen_severity_table[severity_code];
216out:
217 return retval;
218} /* End twl_aen_severity_lookup() */
219
220/* This function will queue an event */
221static void twl_aen_queue_event(TW_Device_Extension *tw_dev, TW_Command_Apache_Header *header)
222{
223 u32 local_time;
adam radfordf6191062009-10-23 14:52:33 -0700224 TW_Event *event;
225 unsigned short aen;
226 char host[16];
227 char *error_str;
228
229 tw_dev->aen_count++;
230
231 /* Fill out event info */
232 event = tw_dev->event_queue[tw_dev->error_index];
233
234 host[0] = '\0';
235 if (tw_dev->host)
236 sprintf(host, " scsi%d:", tw_dev->host->host_no);
237
238 aen = le16_to_cpu(header->status_block.error);
239 memset(event, 0, sizeof(TW_Event));
240
241 event->severity = TW_SEV_OUT(header->status_block.severity__reserved);
Arnd Bergmann9c88673f2017-11-10 16:58:25 +0100242 /* event->time_stamp_sec overflows in y2106 */
243 local_time = (u32)(ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
adam radfordf6191062009-10-23 14:52:33 -0700244 event->time_stamp_sec = local_time;
245 event->aen_code = aen;
246 event->retrieved = TW_AEN_NOT_RETRIEVED;
247 event->sequence_id = tw_dev->error_sequence_id;
248 tw_dev->error_sequence_id++;
249
250 /* Check for embedded error string */
251 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc)+1]);
252
253 header->err_specific_desc[sizeof(header->err_specific_desc) - 1] = '\0';
254 event->parameter_len = strlen(header->err_specific_desc);
255 memcpy(event->parameter_data, header->err_specific_desc, event->parameter_len + 1 + strlen(error_str));
256 if (event->severity != TW_AEN_SEVERITY_DEBUG)
257 printk(KERN_WARNING "3w-sas:%s AEN: %s (0x%02X:0x%04X): %s:%s.\n",
258 host,
259 twl_aen_severity_lookup(TW_SEV_OUT(header->status_block.severity__reserved)),
260 TW_MESSAGE_SOURCE_CONTROLLER_EVENT, aen, error_str,
261 header->err_specific_desc);
262 else
263 tw_dev->aen_count--;
264
265 tw_dev->error_index = (tw_dev->error_index + 1 ) % TW_Q_LENGTH;
266} /* End twl_aen_queue_event() */
267
268/* This function will attempt to post a command packet to the board */
269static int twl_post_command_packet(TW_Device_Extension *tw_dev, int request_id)
270{
271 dma_addr_t command_que_value;
272
273 command_que_value = tw_dev->command_packet_phys[request_id];
274 command_que_value += TW_COMMAND_OFFSET;
275
276 /* First write upper 4 bytes */
277 writel((u32)((u64)command_que_value >> 32), TWL_HIBQPH_REG_ADDR(tw_dev));
278 /* Then the lower 4 bytes */
279 writel((u32)(command_que_value | TWL_PULL_MODE), TWL_HIBQPL_REG_ADDR(tw_dev));
280
281 tw_dev->state[request_id] = TW_S_POSTED;
282 tw_dev->posted_request_count++;
283 if (tw_dev->posted_request_count > tw_dev->max_posted_request_count)
284 tw_dev->max_posted_request_count = tw_dev->posted_request_count;
285
286 return 0;
287} /* End twl_post_command_packet() */
288
adam radfordf6191062009-10-23 14:52:33 -0700289/* This function hands scsi cdb's to the firmware */
Nathan Chancellor09968e52018-10-18 14:55:41 -0700290static int twl_scsiop_execute_scsi(TW_Device_Extension *tw_dev, int request_id,
291 unsigned char *cdb, int use_sg,
292 TW_SG_Entry_ISO *sglistarg)
adam radfordf6191062009-10-23 14:52:33 -0700293{
294 TW_Command_Full *full_command_packet;
295 TW_Command_Apache *command_packet;
296 int i, sg_count;
297 struct scsi_cmnd *srb = NULL;
Lee Jones475bff62021-03-12 09:47:38 +0000298 struct scatterlist *sg;
adam radfordf6191062009-10-23 14:52:33 -0700299 int retval = 1;
300
Lee Jones475bff62021-03-12 09:47:38 +0000301 if (tw_dev->srb[request_id])
adam radfordf6191062009-10-23 14:52:33 -0700302 srb = tw_dev->srb[request_id];
adam radfordf6191062009-10-23 14:52:33 -0700303
304 /* Initialize command packet */
305 full_command_packet = tw_dev->command_packet_virt[request_id];
306 full_command_packet->header.header_desc.size_header = 128;
307 full_command_packet->header.status_block.error = 0;
308 full_command_packet->header.status_block.severity__reserved = 0;
309
310 command_packet = &full_command_packet->command.newcommand;
311 command_packet->status = 0;
312 command_packet->opcode__reserved = TW_OPRES_IN(0, TW_OP_EXECUTE_SCSI);
313
314 /* We forced 16 byte cdb use earlier */
315 if (!cdb)
316 memcpy(command_packet->cdb, srb->cmnd, TW_MAX_CDB_LEN);
317 else
318 memcpy(command_packet->cdb, cdb, TW_MAX_CDB_LEN);
319
320 if (srb) {
321 command_packet->unit = srb->device->id;
322 command_packet->request_id__lunl =
323 cpu_to_le16(TW_REQ_LUN_IN(srb->device->lun, request_id));
324 } else {
325 command_packet->request_id__lunl =
326 cpu_to_le16(TW_REQ_LUN_IN(0, request_id));
327 command_packet->unit = 0;
328 }
329
330 command_packet->sgl_offset = 16;
331
332 if (!sglistarg) {
333 /* Map sglist from scsi layer to cmd packet */
334 if (scsi_sg_count(srb)) {
Christoph Hellwig579d69b2015-04-23 09:48:49 +0200335 sg_count = scsi_dma_map(srb);
336 if (sg_count <= 0)
adam radfordf6191062009-10-23 14:52:33 -0700337 goto out;
338
339 scsi_for_each_sg(srb, sg, sg_count, i) {
340 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sg_dma_address(sg));
341 command_packet->sg_list[i].length = TW_CPU_TO_SGL(sg_dma_len(sg));
342 }
343 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN((srb->device->lun >> 4), scsi_sg_count(tw_dev->srb[request_id])));
344 }
345 } else {
346 /* Internal cdb post */
347 for (i = 0; i < use_sg; i++) {
348 command_packet->sg_list[i].address = TW_CPU_TO_SGL(sglistarg[i].address);
349 command_packet->sg_list[i].length = TW_CPU_TO_SGL(sglistarg[i].length);
350 }
351 command_packet->sgl_entries__lunh = cpu_to_le16(TW_REQ_LUN_IN(0, use_sg));
352 }
353
354 /* Update some stats */
355 if (srb) {
356 tw_dev->sector_count = scsi_bufflen(srb) / 512;
357 if (tw_dev->sector_count > tw_dev->max_sector_count)
358 tw_dev->max_sector_count = tw_dev->sector_count;
359 tw_dev->sgl_entries = scsi_sg_count(srb);
360 if (tw_dev->sgl_entries > tw_dev->max_sgl_entries)
361 tw_dev->max_sgl_entries = tw_dev->sgl_entries;
362 }
363
364 /* Now post the command to the board */
365 retval = twl_post_command_packet(tw_dev, request_id);
366
367out:
368 return retval;
369} /* End twl_scsiop_execute_scsi() */
370
371/* This function will read the aen queue from the isr */
372static int twl_aen_read_queue(TW_Device_Extension *tw_dev, int request_id)
373{
Nathan Chancellor09968e52018-10-18 14:55:41 -0700374 unsigned char cdb[TW_MAX_CDB_LEN];
adam radfordf6191062009-10-23 14:52:33 -0700375 TW_SG_Entry_ISO sglist[1];
376 TW_Command_Full *full_command_packet;
377 int retval = 1;
378
379 full_command_packet = tw_dev->command_packet_virt[request_id];
380 memset(full_command_packet, 0, sizeof(TW_Command_Full));
381
382 /* Initialize cdb */
383 memset(&cdb, 0, TW_MAX_CDB_LEN);
384 cdb[0] = REQUEST_SENSE; /* opcode */
385 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
386
387 /* Initialize sglist */
388 memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
389 sglist[0].length = TW_SECTOR_SIZE;
390 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
391
392 /* Mark internal command */
393 tw_dev->srb[request_id] = NULL;
394
395 /* Now post the command packet */
396 if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
397 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x2, "Post failed while reading AEN queue");
398 goto out;
399 }
400 retval = 0;
401out:
402 return retval;
403} /* End twl_aen_read_queue() */
404
405/* This function will sync firmware time with the host time */
406static void twl_aen_sync_time(TW_Device_Extension *tw_dev, int request_id)
407{
408 u32 schedulertime;
adam radfordf6191062009-10-23 14:52:33 -0700409 TW_Command_Full *full_command_packet;
410 TW_Command *command_packet;
411 TW_Param_Apache *param;
Arnd Bergmannbc8f9162017-11-10 16:58:26 +0100412 time64_t local_time;
adam radfordf6191062009-10-23 14:52:33 -0700413
414 /* Fill out the command packet */
415 full_command_packet = tw_dev->command_packet_virt[request_id];
416 memset(full_command_packet, 0, sizeof(TW_Command_Full));
417 command_packet = &full_command_packet->command.oldcommand;
418 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_SET_PARAM);
419 command_packet->request_id = request_id;
420 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
421 command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
422 command_packet->size = TW_COMMAND_SIZE;
423 command_packet->byte6_offset.parameter_count = cpu_to_le16(1);
424
425 /* Setup the param */
426 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
427 memset(param, 0, TW_SECTOR_SIZE);
428 param->table_id = cpu_to_le16(TW_TIMEKEEP_TABLE | 0x8000); /* Controller time keep table */
429 param->parameter_id = cpu_to_le16(0x3); /* SchedulerTime */
430 param->parameter_size_bytes = cpu_to_le16(4);
431
Hannes Reinecke17896712021-01-13 10:04:29 +0100432 /* Convert system time in UTC to local time seconds since last
adam radfordf6191062009-10-23 14:52:33 -0700433 Sunday 12:00AM */
Arnd Bergmannbc8f9162017-11-10 16:58:26 +0100434 local_time = (ktime_get_real_seconds() - (sys_tz.tz_minuteswest * 60));
435 div_u64_rem(local_time - (3 * 86400), 604800, &schedulertime);
436 schedulertime = cpu_to_le32(schedulertime);
adam radfordf6191062009-10-23 14:52:33 -0700437
438 memcpy(param->data, &schedulertime, sizeof(u32));
439
440 /* Mark internal command */
441 tw_dev->srb[request_id] = NULL;
442
443 /* Now post the command */
444 twl_post_command_packet(tw_dev, request_id);
445} /* End twl_aen_sync_time() */
446
447/* This function will assign an available request id */
448static void twl_get_request_id(TW_Device_Extension *tw_dev, int *request_id)
449{
450 *request_id = tw_dev->free_queue[tw_dev->free_head];
451 tw_dev->free_head = (tw_dev->free_head + 1) % TW_Q_LENGTH;
452 tw_dev->state[*request_id] = TW_S_STARTED;
453} /* End twl_get_request_id() */
454
455/* This function will free a request id */
456static void twl_free_request_id(TW_Device_Extension *tw_dev, int request_id)
457{
458 tw_dev->free_queue[tw_dev->free_tail] = request_id;
459 tw_dev->state[request_id] = TW_S_FINISHED;
460 tw_dev->free_tail = (tw_dev->free_tail + 1) % TW_Q_LENGTH;
461} /* End twl_free_request_id() */
462
463/* This function will complete an aen request from the isr */
464static int twl_aen_complete(TW_Device_Extension *tw_dev, int request_id)
465{
466 TW_Command_Full *full_command_packet;
467 TW_Command *command_packet;
468 TW_Command_Apache_Header *header;
469 unsigned short aen;
470 int retval = 1;
471
472 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
473 tw_dev->posted_request_count--;
474 aen = le16_to_cpu(header->status_block.error);
475 full_command_packet = tw_dev->command_packet_virt[request_id];
476 command_packet = &full_command_packet->command.oldcommand;
477
478 /* First check for internal completion of set param for time sync */
479 if (TW_OP_OUT(command_packet->opcode__sgloffset) == TW_OP_SET_PARAM) {
480 /* Keep reading the queue in case there are more aen's */
481 if (twl_aen_read_queue(tw_dev, request_id))
482 goto out2;
Hannes Reinecke17896712021-01-13 10:04:29 +0100483 else {
adam radfordf6191062009-10-23 14:52:33 -0700484 retval = 0;
485 goto out;
486 }
487 }
488
489 switch (aen) {
490 case TW_AEN_QUEUE_EMPTY:
491 /* Quit reading the queue if this is the last one */
492 break;
493 case TW_AEN_SYNC_TIME_WITH_HOST:
494 twl_aen_sync_time(tw_dev, request_id);
495 retval = 0;
496 goto out;
497 default:
498 twl_aen_queue_event(tw_dev, header);
499
500 /* If there are more aen's, keep reading the queue */
501 if (twl_aen_read_queue(tw_dev, request_id))
502 goto out2;
503 else {
504 retval = 0;
505 goto out;
506 }
507 }
508 retval = 0;
509out2:
510 tw_dev->state[request_id] = TW_S_COMPLETED;
511 twl_free_request_id(tw_dev, request_id);
512 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
513out:
514 return retval;
515} /* End twl_aen_complete() */
516
517/* This function will poll for a response */
518static int twl_poll_response(TW_Device_Extension *tw_dev, int request_id, int seconds)
519{
520 unsigned long before;
521 dma_addr_t mfa;
522 u32 regh, regl;
523 u32 response;
524 int retval = 1;
525 int found = 0;
526
527 before = jiffies;
528
529 while (!found) {
530 if (sizeof(dma_addr_t) > 4) {
531 regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
532 regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
533 mfa = ((u64)regh << 32) | regl;
534 } else
535 mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
536
537 response = (u32)mfa;
538
539 if (TW_RESID_OUT(response) == request_id)
540 found = 1;
541
542 if (time_after(jiffies, before + HZ * seconds))
543 goto out;
544
545 msleep(50);
546 }
547 retval = 0;
Hannes Reinecke17896712021-01-13 10:04:29 +0100548out:
adam radfordf6191062009-10-23 14:52:33 -0700549 return retval;
550} /* End twl_poll_response() */
551
552/* This function will drain the aen queue */
553static int twl_aen_drain_queue(TW_Device_Extension *tw_dev, int no_check_reset)
554{
555 int request_id = 0;
Nathan Chancellor09968e52018-10-18 14:55:41 -0700556 unsigned char cdb[TW_MAX_CDB_LEN];
adam radfordf6191062009-10-23 14:52:33 -0700557 TW_SG_Entry_ISO sglist[1];
558 int finished = 0, count = 0;
559 TW_Command_Full *full_command_packet;
560 TW_Command_Apache_Header *header;
561 unsigned short aen;
562 int first_reset = 0, queue = 0, retval = 1;
563
564 if (no_check_reset)
565 first_reset = 0;
566 else
567 first_reset = 1;
568
569 full_command_packet = tw_dev->command_packet_virt[request_id];
570 memset(full_command_packet, 0, sizeof(TW_Command_Full));
571
572 /* Initialize cdb */
573 memset(&cdb, 0, TW_MAX_CDB_LEN);
574 cdb[0] = REQUEST_SENSE; /* opcode */
575 cdb[4] = TW_ALLOCATION_LENGTH; /* allocation length */
576
577 /* Initialize sglist */
578 memset(&sglist, 0, sizeof(TW_SG_Entry_ISO));
579 sglist[0].length = TW_SECTOR_SIZE;
580 sglist[0].address = tw_dev->generic_buffer_phys[request_id];
581
582 /* Mark internal command */
583 tw_dev->srb[request_id] = NULL;
584
585 do {
586 /* Send command to the board */
587 if (twl_scsiop_execute_scsi(tw_dev, request_id, cdb, 1, sglist)) {
588 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x3, "Error posting request sense");
589 goto out;
590 }
591
592 /* Now poll for completion */
593 if (twl_poll_response(tw_dev, request_id, 30)) {
594 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x4, "No valid response while draining AEN queue");
595 tw_dev->posted_request_count--;
596 goto out;
597 }
598
599 tw_dev->posted_request_count--;
600 header = (TW_Command_Apache_Header *)tw_dev->generic_buffer_virt[request_id];
601 aen = le16_to_cpu(header->status_block.error);
602 queue = 0;
603 count++;
604
605 switch (aen) {
606 case TW_AEN_QUEUE_EMPTY:
607 if (first_reset != 1)
608 goto out;
609 else
610 finished = 1;
611 break;
612 case TW_AEN_SOFT_RESET:
613 if (first_reset == 0)
614 first_reset = 1;
615 else
616 queue = 1;
617 break;
618 case TW_AEN_SYNC_TIME_WITH_HOST:
619 break;
620 default:
621 queue = 1;
622 }
623
624 /* Now queue an event info */
625 if (queue)
626 twl_aen_queue_event(tw_dev, header);
627 } while ((finished == 0) && (count < TW_MAX_AEN_DRAIN));
628
629 if (count == TW_MAX_AEN_DRAIN)
630 goto out;
631
632 retval = 0;
633out:
634 tw_dev->state[request_id] = TW_S_INITIAL;
635 return retval;
636} /* End twl_aen_drain_queue() */
637
638/* This function will allocate memory and check if it is correctly aligned */
639static int twl_allocate_memory(TW_Device_Extension *tw_dev, int size, int which)
640{
641 int i;
642 dma_addr_t dma_handle;
643 unsigned long *cpu_addr;
644 int retval = 1;
645
Luis Chamberlain750afb02019-01-04 09:23:09 +0100646 cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev,
647 size * TW_Q_LENGTH, &dma_handle,
648 GFP_KERNEL);
adam radfordf6191062009-10-23 14:52:33 -0700649 if (!cpu_addr) {
650 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x5, "Memory allocation failed");
651 goto out;
652 }
653
adam radfordf6191062009-10-23 14:52:33 -0700654 for (i = 0; i < TW_Q_LENGTH; i++) {
655 switch(which) {
656 case 0:
657 tw_dev->command_packet_phys[i] = dma_handle+(i*size);
658 tw_dev->command_packet_virt[i] = (TW_Command_Full *)((unsigned char *)cpu_addr + (i*size));
659 break;
660 case 1:
661 tw_dev->generic_buffer_phys[i] = dma_handle+(i*size);
662 tw_dev->generic_buffer_virt[i] = (unsigned long *)((unsigned char *)cpu_addr + (i*size));
663 break;
664 case 2:
665 tw_dev->sense_buffer_phys[i] = dma_handle+(i*size);
666 tw_dev->sense_buffer_virt[i] = (TW_Command_Apache_Header *)((unsigned char *)cpu_addr + (i*size));
667 break;
668 }
669 }
670 retval = 0;
671out:
672 return retval;
673} /* End twl_allocate_memory() */
674
675/* This function will load the request id and various sgls for ioctls */
676static void twl_load_sgl(TW_Device_Extension *tw_dev, TW_Command_Full *full_command_packet, int request_id, dma_addr_t dma_handle, int length)
677{
678 TW_Command *oldcommand;
679 TW_Command_Apache *newcommand;
680 TW_SG_Entry_ISO *sgl;
681 unsigned int pae = 0;
682
683 if ((sizeof(long) < 8) && (sizeof(dma_addr_t) > 4))
684 pae = 1;
685
686 if (TW_OP_OUT(full_command_packet->command.newcommand.opcode__reserved) == TW_OP_EXECUTE_SCSI) {
687 newcommand = &full_command_packet->command.newcommand;
688 newcommand->request_id__lunl =
689 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->request_id__lunl), request_id));
690 if (length) {
691 newcommand->sg_list[0].address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
692 newcommand->sg_list[0].length = TW_CPU_TO_SGL(length);
693 }
694 newcommand->sgl_entries__lunh =
695 cpu_to_le16(TW_REQ_LUN_IN(TW_LUN_OUT(newcommand->sgl_entries__lunh), length ? 1 : 0));
696 } else {
697 oldcommand = &full_command_packet->command.oldcommand;
698 oldcommand->request_id = request_id;
699
700 if (TW_SGL_OUT(oldcommand->opcode__sgloffset)) {
701 /* Load the sg list */
702 sgl = (TW_SG_Entry_ISO *)((u32 *)oldcommand+oldcommand->size - (sizeof(TW_SG_Entry_ISO)/4) + pae + (sizeof(dma_addr_t) > 4 ? 1 : 0));
703 sgl->address = TW_CPU_TO_SGL(dma_handle + sizeof(TW_Ioctl_Buf_Apache) - 1);
704 sgl->length = TW_CPU_TO_SGL(length);
705 oldcommand->size += pae;
706 oldcommand->size += sizeof(dma_addr_t) > 4 ? 1 : 0;
707 }
708 }
709} /* End twl_load_sgl() */
710
711/* This function handles ioctl for the character device
712 This interface is used by smartmontools open source software */
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200713static long twl_chrdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
adam radfordf6191062009-10-23 14:52:33 -0700714{
715 long timeout;
716 unsigned long *cpu_addr, data_buffer_length_adjusted = 0, flags = 0;
717 dma_addr_t dma_handle;
718 int request_id = 0;
719 TW_Ioctl_Driver_Command driver_command;
Al Viro496ad9a2013-01-23 17:07:38 -0500720 struct inode *inode = file_inode(file);
adam radfordf6191062009-10-23 14:52:33 -0700721 TW_Ioctl_Buf_Apache *tw_ioctl;
722 TW_Command_Full *full_command_packet;
723 TW_Device_Extension *tw_dev = twl_device_extension_list[iminor(inode)];
724 int retval = -EFAULT;
725 void __user *argp = (void __user *)arg;
726
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200727 mutex_lock(&twl_chrdev_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200728
adam radfordf6191062009-10-23 14:52:33 -0700729 /* Only let one of these through at a time */
730 if (mutex_lock_interruptible(&tw_dev->ioctl_lock)) {
731 retval = -EINTR;
732 goto out;
733 }
734
735 /* First copy down the driver command */
736 if (copy_from_user(&driver_command, argp, sizeof(TW_Ioctl_Driver_Command)))
737 goto out2;
738
739 /* Check data buffer size */
740 if (driver_command.buffer_length > TW_MAX_SECTORS * 2048) {
741 retval = -EINVAL;
742 goto out2;
743 }
744
745 /* Hardware can only do multiple of 512 byte transfers */
746 data_buffer_length_adjusted = (driver_command.buffer_length + 511) & ~511;
747
748 /* Now allocate ioctl buf memory */
749 cpu_addr = dma_alloc_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, &dma_handle, GFP_KERNEL);
750 if (!cpu_addr) {
751 retval = -ENOMEM;
752 goto out2;
753 }
754
755 tw_ioctl = (TW_Ioctl_Buf_Apache *)cpu_addr;
756
757 /* Now copy down the entire ioctl */
758 if (copy_from_user(tw_ioctl, argp, driver_command.buffer_length + sizeof(TW_Ioctl_Buf_Apache) - 1))
759 goto out3;
760
761 /* See which ioctl we are doing */
762 switch (cmd) {
763 case TW_IOCTL_FIRMWARE_PASS_THROUGH:
764 spin_lock_irqsave(tw_dev->host->host_lock, flags);
765 twl_get_request_id(tw_dev, &request_id);
766
767 /* Flag internal command */
768 tw_dev->srb[request_id] = NULL;
769
770 /* Flag chrdev ioctl */
771 tw_dev->chrdev_request_id = request_id;
772
773 full_command_packet = (TW_Command_Full *)&tw_ioctl->firmware_command;
774
775 /* Load request id and sglist for both command types */
776 twl_load_sgl(tw_dev, full_command_packet, request_id, dma_handle, data_buffer_length_adjusted);
777
778 memcpy(tw_dev->command_packet_virt[request_id], &(tw_ioctl->firmware_command), sizeof(TW_Command_Full));
779
780 /* Now post the command packet to the controller */
781 twl_post_command_packet(tw_dev, request_id);
782 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
783
784 timeout = TW_IOCTL_CHRDEV_TIMEOUT*HZ;
785
786 /* Now wait for command to complete */
787 timeout = wait_event_timeout(tw_dev->ioctl_wqueue, tw_dev->chrdev_request_id == TW_IOCTL_CHRDEV_FREE, timeout);
788
789 /* We timed out, and didn't get an interrupt */
790 if (tw_dev->chrdev_request_id != TW_IOCTL_CHRDEV_FREE) {
791 /* Now we need to reset the board */
792 printk(KERN_WARNING "3w-sas: scsi%d: WARNING: (0x%02X:0x%04X): Character ioctl (0x%x) timed out, resetting card.\n",
793 tw_dev->host->host_no, TW_DRIVER, 0x6,
794 cmd);
795 retval = -EIO;
796 twl_reset_device_extension(tw_dev, 1);
797 goto out3;
798 }
799
800 /* Now copy in the command packet response */
801 memcpy(&(tw_ioctl->firmware_command), tw_dev->command_packet_virt[request_id], sizeof(TW_Command_Full));
Hannes Reinecke17896712021-01-13 10:04:29 +0100802
adam radfordf6191062009-10-23 14:52:33 -0700803 /* Now complete the io */
804 spin_lock_irqsave(tw_dev->host->host_lock, flags);
805 tw_dev->posted_request_count--;
806 tw_dev->state[request_id] = TW_S_COMPLETED;
807 twl_free_request_id(tw_dev, request_id);
808 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
809 break;
810 default:
811 retval = -ENOTTY;
812 goto out3;
813 }
814
815 /* Now copy the entire response to userspace */
816 if (copy_to_user(argp, tw_ioctl, sizeof(TW_Ioctl_Buf_Apache) + driver_command.buffer_length - 1) == 0)
817 retval = 0;
818out3:
819 /* Now free ioctl buf memory */
820 dma_free_coherent(&tw_dev->tw_pci_dev->dev, data_buffer_length_adjusted+sizeof(TW_Ioctl_Buf_Apache) - 1, cpu_addr, dma_handle);
821out2:
822 mutex_unlock(&tw_dev->ioctl_lock);
823out:
Arnd Bergmannc45d15d2010-06-02 14:28:52 +0200824 mutex_unlock(&twl_chrdev_mutex);
adam radfordf6191062009-10-23 14:52:33 -0700825 return retval;
826} /* End twl_chrdev_ioctl() */
827
828/* This function handles open for the character device */
829static int twl_chrdev_open(struct inode *inode, struct file *file)
830{
831 unsigned int minor_number;
832 int retval = -ENODEV;
833
834 if (!capable(CAP_SYS_ADMIN)) {
835 retval = -EACCES;
836 goto out;
837 }
838
adam radfordf6191062009-10-23 14:52:33 -0700839 minor_number = iminor(inode);
840 if (minor_number >= twl_device_extension_count)
841 goto out;
842 retval = 0;
843out:
844 return retval;
845} /* End twl_chrdev_open() */
846
847/* File operations struct for character device */
848static const struct file_operations twl_fops = {
849 .owner = THIS_MODULE,
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200850 .unlocked_ioctl = twl_chrdev_ioctl,
adam radfordf6191062009-10-23 14:52:33 -0700851 .open = twl_chrdev_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200852 .release = NULL,
853 .llseek = noop_llseek,
adam radfordf6191062009-10-23 14:52:33 -0700854};
855
856/* This function passes sense data from firmware to scsi layer */
857static int twl_fill_sense(TW_Device_Extension *tw_dev, int i, int request_id, int copy_sense, int print_host)
858{
859 TW_Command_Apache_Header *header;
860 TW_Command_Full *full_command_packet;
861 unsigned short error;
862 char *error_str;
adam radfordf6191062009-10-23 14:52:33 -0700863
864 header = tw_dev->sense_buffer_virt[i];
865 full_command_packet = tw_dev->command_packet_virt[request_id];
866
867 /* Get embedded firmware error string */
868 error_str = &(header->err_specific_desc[strlen(header->err_specific_desc) + 1]);
869
870 /* Don't print error for Logical unit not supported during rollcall */
871 error = le16_to_cpu(header->status_block.error);
872 if ((error != TW_ERROR_LOGICAL_UNIT_NOT_SUPPORTED) && (error != TW_ERROR_UNIT_OFFLINE) && (error != TW_ERROR_INVALID_FIELD_IN_CDB)) {
873 if (print_host)
874 printk(KERN_WARNING "3w-sas: scsi%d: ERROR: (0x%02X:0x%04X): %s:%s.\n",
875 tw_dev->host->host_no,
876 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
877 header->status_block.error,
Hannes Reinecke17896712021-01-13 10:04:29 +0100878 error_str,
adam radfordf6191062009-10-23 14:52:33 -0700879 header->err_specific_desc);
880 else
881 printk(KERN_WARNING "3w-sas: ERROR: (0x%02X:0x%04X): %s:%s.\n",
882 TW_MESSAGE_SOURCE_CONTROLLER_ERROR,
883 header->status_block.error,
884 error_str,
885 header->err_specific_desc);
886 }
887
888 if (copy_sense) {
889 memcpy(tw_dev->srb[request_id]->sense_buffer, header->sense_data, TW_SENSE_DATA_LENGTH);
890 tw_dev->srb[request_id]->result = (full_command_packet->command.newcommand.status << 1);
891 goto out;
892 }
893out:
Yang Li2af0bf32021-03-09 14:41:04 +0800894 return 1;
adam radfordf6191062009-10-23 14:52:33 -0700895} /* End twl_fill_sense() */
896
897/* This function will free up device extension resources */
898static void twl_free_device_extension(TW_Device_Extension *tw_dev)
899{
900 if (tw_dev->command_packet_virt[0])
Christoph Hellwigb1fa1222018-10-10 18:06:50 +0200901 dma_free_coherent(&tw_dev->tw_pci_dev->dev,
adam radfordf6191062009-10-23 14:52:33 -0700902 sizeof(TW_Command_Full)*TW_Q_LENGTH,
903 tw_dev->command_packet_virt[0],
904 tw_dev->command_packet_phys[0]);
905
906 if (tw_dev->generic_buffer_virt[0])
Christoph Hellwigb1fa1222018-10-10 18:06:50 +0200907 dma_free_coherent(&tw_dev->tw_pci_dev->dev,
adam radfordf6191062009-10-23 14:52:33 -0700908 TW_SECTOR_SIZE*TW_Q_LENGTH,
909 tw_dev->generic_buffer_virt[0],
910 tw_dev->generic_buffer_phys[0]);
911
912 if (tw_dev->sense_buffer_virt[0])
Christoph Hellwigb1fa1222018-10-10 18:06:50 +0200913 dma_free_coherent(&tw_dev->tw_pci_dev->dev,
adam radfordf6191062009-10-23 14:52:33 -0700914 sizeof(TW_Command_Apache_Header)*
915 TW_Q_LENGTH,
916 tw_dev->sense_buffer_virt[0],
917 tw_dev->sense_buffer_phys[0]);
918
919 kfree(tw_dev->event_queue[0]);
920} /* End twl_free_device_extension() */
921
922/* This function will get parameter table entries from the firmware */
923static void *twl_get_param(TW_Device_Extension *tw_dev, int request_id, int table_id, int parameter_id, int parameter_size_bytes)
924{
925 TW_Command_Full *full_command_packet;
926 TW_Command *command_packet;
927 TW_Param_Apache *param;
928 void *retval = NULL;
929
930 /* Setup the command packet */
931 full_command_packet = tw_dev->command_packet_virt[request_id];
932 memset(full_command_packet, 0, sizeof(TW_Command_Full));
933 command_packet = &full_command_packet->command.oldcommand;
934
935 command_packet->opcode__sgloffset = TW_OPSGL_IN(2, TW_OP_GET_PARAM);
Hannes Reinecke17896712021-01-13 10:04:29 +0100936 command_packet->size = TW_COMMAND_SIZE;
937 command_packet->request_id = request_id;
adam radfordf6191062009-10-23 14:52:33 -0700938 command_packet->byte6_offset.block_count = cpu_to_le16(1);
939
940 /* Now setup the param */
941 param = (TW_Param_Apache *)tw_dev->generic_buffer_virt[request_id];
942 memset(param, 0, TW_SECTOR_SIZE);
943 param->table_id = cpu_to_le16(table_id | 0x8000);
944 param->parameter_id = cpu_to_le16(parameter_id);
945 param->parameter_size_bytes = cpu_to_le16(parameter_size_bytes);
946
947 command_packet->byte8_offset.param.sgl[0].address = TW_CPU_TO_SGL(tw_dev->generic_buffer_phys[request_id]);
948 command_packet->byte8_offset.param.sgl[0].length = TW_CPU_TO_SGL(TW_SECTOR_SIZE);
949
950 /* Post the command packet to the board */
951 twl_post_command_packet(tw_dev, request_id);
952
953 /* Poll for completion */
954 if (twl_poll_response(tw_dev, request_id, 30))
955 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x7, "No valid response during get param")
956 else
957 retval = (void *)&(param->data[0]);
958
959 tw_dev->posted_request_count--;
960 tw_dev->state[request_id] = TW_S_INITIAL;
961
962 return retval;
963} /* End twl_get_param() */
964
965/* This function will send an initconnection command to controller */
966static int twl_initconnection(TW_Device_Extension *tw_dev, int message_credits,
Hannes Reinecke17896712021-01-13 10:04:29 +0100967 u32 set_features, unsigned short current_fw_srl,
968 unsigned short current_fw_arch_id,
969 unsigned short current_fw_branch,
970 unsigned short current_fw_build,
971 unsigned short *fw_on_ctlr_srl,
972 unsigned short *fw_on_ctlr_arch_id,
973 unsigned short *fw_on_ctlr_branch,
974 unsigned short *fw_on_ctlr_build,
adam radfordf6191062009-10-23 14:52:33 -0700975 u32 *init_connect_result)
976{
977 TW_Command_Full *full_command_packet;
978 TW_Initconnect *tw_initconnect;
979 int request_id = 0, retval = 1;
980
981 /* Initialize InitConnection command packet */
982 full_command_packet = tw_dev->command_packet_virt[request_id];
983 memset(full_command_packet, 0, sizeof(TW_Command_Full));
984 full_command_packet->header.header_desc.size_header = 128;
Hannes Reinecke17896712021-01-13 10:04:29 +0100985
adam radfordf6191062009-10-23 14:52:33 -0700986 tw_initconnect = (TW_Initconnect *)&full_command_packet->command.oldcommand;
987 tw_initconnect->opcode__reserved = TW_OPRES_IN(0, TW_OP_INIT_CONNECTION);
988 tw_initconnect->request_id = request_id;
989 tw_initconnect->message_credits = cpu_to_le16(message_credits);
990 tw_initconnect->features = set_features;
991
992 /* Turn on 64-bit sgl support if we need to */
993 tw_initconnect->features |= sizeof(dma_addr_t) > 4 ? 1 : 0;
994
995 tw_initconnect->features = cpu_to_le32(tw_initconnect->features);
996
997 if (set_features & TW_EXTENDED_INIT_CONNECT) {
998 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE_EXTENDED;
999 tw_initconnect->fw_srl = cpu_to_le16(current_fw_srl);
1000 tw_initconnect->fw_arch_id = cpu_to_le16(current_fw_arch_id);
1001 tw_initconnect->fw_branch = cpu_to_le16(current_fw_branch);
1002 tw_initconnect->fw_build = cpu_to_le16(current_fw_build);
Hannes Reinecke17896712021-01-13 10:04:29 +01001003 } else
adam radfordf6191062009-10-23 14:52:33 -07001004 tw_initconnect->size = TW_INIT_COMMAND_PACKET_SIZE;
1005
1006 /* Send command packet to the board */
1007 twl_post_command_packet(tw_dev, request_id);
1008
1009 /* Poll for completion */
1010 if (twl_poll_response(tw_dev, request_id, 30)) {
1011 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x8, "No valid response during init connection");
1012 } else {
1013 if (set_features & TW_EXTENDED_INIT_CONNECT) {
1014 *fw_on_ctlr_srl = le16_to_cpu(tw_initconnect->fw_srl);
1015 *fw_on_ctlr_arch_id = le16_to_cpu(tw_initconnect->fw_arch_id);
1016 *fw_on_ctlr_branch = le16_to_cpu(tw_initconnect->fw_branch);
1017 *fw_on_ctlr_build = le16_to_cpu(tw_initconnect->fw_build);
1018 *init_connect_result = le32_to_cpu(tw_initconnect->result);
1019 }
1020 retval = 0;
1021 }
1022
1023 tw_dev->posted_request_count--;
1024 tw_dev->state[request_id] = TW_S_INITIAL;
1025
1026 return retval;
1027} /* End twl_initconnection() */
1028
1029/* This function will initialize the fields of a device extension */
1030static int twl_initialize_device_extension(TW_Device_Extension *tw_dev)
1031{
1032 int i, retval = 1;
1033
1034 /* Initialize command packet buffers */
1035 if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Full), 0)) {
1036 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x9, "Command packet memory allocation failed");
1037 goto out;
1038 }
1039
1040 /* Initialize generic buffer */
1041 if (twl_allocate_memory(tw_dev, TW_SECTOR_SIZE, 1)) {
1042 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xa, "Generic memory allocation failed");
1043 goto out;
1044 }
1045
1046 /* Allocate sense buffers */
1047 if (twl_allocate_memory(tw_dev, sizeof(TW_Command_Apache_Header), 2)) {
1048 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xb, "Sense buffer allocation failed");
1049 goto out;
1050 }
1051
1052 /* Allocate event info space */
1053 tw_dev->event_queue[0] = kcalloc(TW_Q_LENGTH, sizeof(TW_Event), GFP_KERNEL);
1054 if (!tw_dev->event_queue[0]) {
1055 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xc, "Event info memory allocation failed");
1056 goto out;
1057 }
1058
1059 for (i = 0; i < TW_Q_LENGTH; i++) {
1060 tw_dev->event_queue[i] = (TW_Event *)((unsigned char *)tw_dev->event_queue[0] + (i * sizeof(TW_Event)));
1061 tw_dev->free_queue[i] = i;
1062 tw_dev->state[i] = TW_S_INITIAL;
1063 }
1064
1065 tw_dev->free_head = TW_Q_START;
1066 tw_dev->free_tail = TW_Q_START;
1067 tw_dev->error_sequence_id = 1;
1068 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1069
1070 mutex_init(&tw_dev->ioctl_lock);
1071 init_waitqueue_head(&tw_dev->ioctl_wqueue);
1072
1073 retval = 0;
1074out:
1075 return retval;
1076} /* End twl_initialize_device_extension() */
1077
adam radfordf6191062009-10-23 14:52:33 -07001078/* This function will handle attention interrupts */
1079static int twl_handle_attention_interrupt(TW_Device_Extension *tw_dev)
1080{
1081 int retval = 1;
1082 u32 request_id, doorbell;
1083
1084 /* Read doorbell status */
1085 doorbell = readl(TWL_HOBDB_REG_ADDR(tw_dev));
1086
1087 /* Check for controller errors */
1088 if (doorbell & TWL_DOORBELL_CONTROLLER_ERROR) {
1089 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xd, "Microcontroller Error: clearing");
1090 goto out;
1091 }
1092
1093 /* Check if we need to perform an AEN drain */
1094 if (doorbell & TWL_DOORBELL_ATTENTION_INTERRUPT) {
1095 if (!(test_and_set_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags))) {
1096 twl_get_request_id(tw_dev, &request_id);
1097 if (twl_aen_read_queue(tw_dev, request_id)) {
1098 tw_dev->state[request_id] = TW_S_COMPLETED;
1099 twl_free_request_id(tw_dev, request_id);
1100 clear_bit(TW_IN_ATTENTION_LOOP, &tw_dev->flags);
1101 }
1102 }
1103 }
1104
1105 retval = 0;
1106out:
1107 /* Clear doorbell interrupt */
1108 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1109
1110 /* Make sure the clear was flushed by reading it back */
1111 readl(TWL_HOBDBC_REG_ADDR(tw_dev));
1112
1113 return retval;
1114} /* End twl_handle_attention_interrupt() */
1115
1116/* Interrupt service routine */
1117static irqreturn_t twl_interrupt(int irq, void *dev_instance)
1118{
1119 TW_Device_Extension *tw_dev = (TW_Device_Extension *)dev_instance;
1120 int i, handled = 0, error = 0;
1121 dma_addr_t mfa = 0;
1122 u32 reg, regl, regh, response, request_id = 0;
1123 struct scsi_cmnd *cmd;
1124 TW_Command_Full *full_command_packet;
1125
1126 spin_lock(tw_dev->host->host_lock);
1127
1128 /* Read host interrupt status */
1129 reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1130
1131 /* Check if this is our interrupt, otherwise bail */
1132 if (!(reg & TWL_HISTATUS_VALID_INTERRUPT))
1133 goto twl_interrupt_bail;
1134
1135 handled = 1;
1136
1137 /* If we are resetting, bail */
1138 if (test_bit(TW_IN_RESET, &tw_dev->flags))
1139 goto twl_interrupt_bail;
1140
1141 /* Attention interrupt */
1142 if (reg & TWL_HISTATUS_ATTENTION_INTERRUPT) {
1143 if (twl_handle_attention_interrupt(tw_dev)) {
1144 TWL_MASK_INTERRUPTS(tw_dev);
1145 goto twl_interrupt_bail;
1146 }
1147 }
1148
1149 /* Response interrupt */
1150 while (reg & TWL_HISTATUS_RESPONSE_INTERRUPT) {
1151 if (sizeof(dma_addr_t) > 4) {
1152 regh = readl(TWL_HOBQPH_REG_ADDR(tw_dev));
1153 regl = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1154 mfa = ((u64)regh << 32) | regl;
1155 } else
1156 mfa = readl(TWL_HOBQPL_REG_ADDR(tw_dev));
1157
1158 error = 0;
1159 response = (u32)mfa;
1160
1161 /* Check for command packet error */
1162 if (!TW_NOTMFA_OUT(response)) {
1163 for (i=0;i<TW_Q_LENGTH;i++) {
1164 if (tw_dev->sense_buffer_phys[i] == mfa) {
1165 request_id = le16_to_cpu(tw_dev->sense_buffer_virt[i]->header_desc.request_id);
1166 if (tw_dev->srb[request_id] != NULL)
1167 error = twl_fill_sense(tw_dev, i, request_id, 1, 1);
1168 else {
1169 /* Skip ioctl error prints */
1170 if (request_id != tw_dev->chrdev_request_id)
1171 error = twl_fill_sense(tw_dev, i, request_id, 0, 1);
1172 else
1173 memcpy(tw_dev->command_packet_virt[request_id], tw_dev->sense_buffer_virt[i], sizeof(TW_Command_Apache_Header));
1174 }
1175
1176 /* Now re-post the sense buffer */
1177 writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1178 writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1179 break;
1180 }
1181 }
1182 } else
1183 request_id = TW_RESID_OUT(response);
1184
1185 full_command_packet = tw_dev->command_packet_virt[request_id];
1186
1187 /* Check for correct state */
1188 if (tw_dev->state[request_id] != TW_S_POSTED) {
1189 if (tw_dev->srb[request_id] != NULL) {
1190 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xe, "Received a request id that wasn't posted");
1191 TWL_MASK_INTERRUPTS(tw_dev);
1192 goto twl_interrupt_bail;
1193 }
1194 }
1195
1196 /* Check for internal command completion */
1197 if (tw_dev->srb[request_id] == NULL) {
1198 if (request_id != tw_dev->chrdev_request_id) {
1199 if (twl_aen_complete(tw_dev, request_id))
1200 TW_PRINTK(tw_dev->host, TW_DRIVER, 0xf, "Error completing AEN during attention interrupt");
1201 } else {
1202 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1203 wake_up(&tw_dev->ioctl_wqueue);
1204 }
1205 } else {
1206 cmd = tw_dev->srb[request_id];
1207
1208 if (!error)
1209 cmd->result = (DID_OK << 16);
Hannes Reinecke17896712021-01-13 10:04:29 +01001210
adam radfordf6191062009-10-23 14:52:33 -07001211 /* Report residual bytes for single sgl */
1212 if ((scsi_sg_count(cmd) <= 1) && (full_command_packet->command.newcommand.status == 0)) {
1213 if (full_command_packet->command.newcommand.sg_list[0].length < scsi_bufflen(tw_dev->srb[request_id]))
1214 scsi_set_resid(cmd, scsi_bufflen(cmd) - full_command_packet->command.newcommand.sg_list[0].length);
1215 }
1216
1217 /* Now complete the io */
Christoph Hellwig579d69b2015-04-23 09:48:49 +02001218 scsi_dma_unmap(cmd);
Bart Van Assche2adf9752021-10-07 13:28:04 -07001219 scsi_done(cmd);
adam radfordf6191062009-10-23 14:52:33 -07001220 tw_dev->state[request_id] = TW_S_COMPLETED;
1221 twl_free_request_id(tw_dev, request_id);
1222 tw_dev->posted_request_count--;
adam radfordf6191062009-10-23 14:52:33 -07001223 }
1224
1225 /* Check for another response interrupt */
1226 reg = readl(TWL_HISTAT_REG_ADDR(tw_dev));
1227 }
1228
1229twl_interrupt_bail:
1230 spin_unlock(tw_dev->host->host_lock);
1231 return IRQ_RETVAL(handled);
1232} /* End twl_interrupt() */
1233
1234/* This function will poll for a register change */
1235static int twl_poll_register(TW_Device_Extension *tw_dev, void *reg, u32 value, u32 result, int seconds)
1236{
1237 unsigned long before;
1238 int retval = 1;
1239 u32 reg_value;
1240
1241 reg_value = readl(reg);
1242 before = jiffies;
1243
Hannes Reinecke17896712021-01-13 10:04:29 +01001244 while ((reg_value & value) != result) {
adam radfordf6191062009-10-23 14:52:33 -07001245 reg_value = readl(reg);
1246 if (time_after(jiffies, before + HZ * seconds))
1247 goto out;
1248 msleep(50);
1249 }
1250 retval = 0;
1251out:
1252 return retval;
1253} /* End twl_poll_register() */
1254
1255/* This function will reset a controller */
1256static int twl_reset_sequence(TW_Device_Extension *tw_dev, int soft_reset)
1257{
1258 int retval = 1;
1259 int i = 0;
1260 u32 status = 0;
1261 unsigned short fw_on_ctlr_srl = 0, fw_on_ctlr_arch_id = 0;
1262 unsigned short fw_on_ctlr_branch = 0, fw_on_ctlr_build = 0;
1263 u32 init_connect_result = 0;
1264 int tries = 0;
1265 int do_soft_reset = soft_reset;
1266
1267 while (tries < TW_MAX_RESET_TRIES) {
1268 /* Do a soft reset if one is needed */
1269 if (do_soft_reset) {
1270 TWL_SOFT_RESET(tw_dev);
1271
1272 /* Make sure controller is in a good state */
1273 if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, 0x0, 30)) {
1274 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x10, "Controller never went non-ready during reset sequence");
1275 tries++;
1276 continue;
1277 }
1278 if (twl_poll_register(tw_dev, TWL_SCRPD3_REG_ADDR(tw_dev), TWL_CONTROLLER_READY, TWL_CONTROLLER_READY, 60)) {
1279 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x11, "Controller not ready during reset sequence");
1280 tries++;
1281 continue;
1282 }
1283 }
1284
1285 /* Initconnect */
1286 if (twl_initconnection(tw_dev, TW_INIT_MESSAGE_CREDITS,
1287 TW_EXTENDED_INIT_CONNECT, TW_CURRENT_DRIVER_SRL,
1288 TW_9750_ARCH_ID, TW_CURRENT_DRIVER_BRANCH,
1289 TW_CURRENT_DRIVER_BUILD, &fw_on_ctlr_srl,
1290 &fw_on_ctlr_arch_id, &fw_on_ctlr_branch,
1291 &fw_on_ctlr_build, &init_connect_result)) {
1292 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x12, "Initconnection failed while checking SRL");
1293 do_soft_reset = 1;
1294 tries++;
1295 continue;
1296 }
1297
1298 /* Load sense buffers */
1299 while (i < TW_Q_LENGTH) {
1300 writel((u32)((u64)tw_dev->sense_buffer_phys[i] >> 32), TWL_HOBQPH_REG_ADDR(tw_dev));
1301 writel((u32)tw_dev->sense_buffer_phys[i], TWL_HOBQPL_REG_ADDR(tw_dev));
1302
1303 /* Check status for over-run after each write */
1304 status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1305 if (!(status & TWL_STATUS_OVERRUN_SUBMIT))
1306 i++;
1307 }
1308
1309 /* Now check status */
1310 status = readl(TWL_STATUS_REG_ADDR(tw_dev));
1311 if (status) {
1312 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x13, "Bad controller status after loading sense buffers");
1313 do_soft_reset = 1;
1314 tries++;
1315 continue;
1316 }
1317
1318 /* Drain the AEN queue */
1319 if (twl_aen_drain_queue(tw_dev, soft_reset)) {
1320 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x14, "AEN drain failed during reset sequence");
1321 do_soft_reset = 1;
1322 tries++;
1323 continue;
1324 }
1325
1326 /* Load rest of compatibility struct */
1327 strncpy(tw_dev->tw_compat_info.driver_version, TW_DRIVER_VERSION, strlen(TW_DRIVER_VERSION));
1328 tw_dev->tw_compat_info.driver_srl_high = TW_CURRENT_DRIVER_SRL;
1329 tw_dev->tw_compat_info.driver_branch_high = TW_CURRENT_DRIVER_BRANCH;
1330 tw_dev->tw_compat_info.driver_build_high = TW_CURRENT_DRIVER_BUILD;
1331 tw_dev->tw_compat_info.driver_srl_low = TW_BASE_FW_SRL;
1332 tw_dev->tw_compat_info.driver_branch_low = TW_BASE_FW_BRANCH;
1333 tw_dev->tw_compat_info.driver_build_low = TW_BASE_FW_BUILD;
1334 tw_dev->tw_compat_info.fw_on_ctlr_srl = fw_on_ctlr_srl;
1335 tw_dev->tw_compat_info.fw_on_ctlr_branch = fw_on_ctlr_branch;
1336 tw_dev->tw_compat_info.fw_on_ctlr_build = fw_on_ctlr_build;
1337
1338 /* If we got here, controller is in a good state */
1339 retval = 0;
1340 goto out;
1341 }
1342out:
1343 return retval;
1344} /* End twl_reset_sequence() */
1345
1346/* This function will reset a device extension */
1347static int twl_reset_device_extension(TW_Device_Extension *tw_dev, int ioctl_reset)
1348{
1349 int i = 0, retval = 1;
1350 unsigned long flags = 0;
1351
1352 /* Block SCSI requests while we are resetting */
1353 if (ioctl_reset)
1354 scsi_block_requests(tw_dev->host);
1355
1356 set_bit(TW_IN_RESET, &tw_dev->flags);
1357 TWL_MASK_INTERRUPTS(tw_dev);
1358 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1359
1360 spin_lock_irqsave(tw_dev->host->host_lock, flags);
1361
1362 /* Abort all requests that are in progress */
1363 for (i = 0; i < TW_Q_LENGTH; i++) {
1364 if ((tw_dev->state[i] != TW_S_FINISHED) &&
1365 (tw_dev->state[i] != TW_S_INITIAL) &&
1366 (tw_dev->state[i] != TW_S_COMPLETED)) {
Christoph Hellwig579d69b2015-04-23 09:48:49 +02001367 struct scsi_cmnd *cmd = tw_dev->srb[i];
1368
1369 if (cmd) {
1370 cmd->result = (DID_RESET << 16);
1371 scsi_dma_unmap(cmd);
Bart Van Assche2adf9752021-10-07 13:28:04 -07001372 scsi_done(cmd);
adam radfordf6191062009-10-23 14:52:33 -07001373 }
1374 }
1375 }
1376
1377 /* Reset queues and counts */
1378 for (i = 0; i < TW_Q_LENGTH; i++) {
1379 tw_dev->free_queue[i] = i;
1380 tw_dev->state[i] = TW_S_INITIAL;
1381 }
1382 tw_dev->free_head = TW_Q_START;
1383 tw_dev->free_tail = TW_Q_START;
1384 tw_dev->posted_request_count = 0;
1385
1386 spin_unlock_irqrestore(tw_dev->host->host_lock, flags);
1387
1388 if (twl_reset_sequence(tw_dev, 1))
1389 goto out;
1390
1391 TWL_UNMASK_INTERRUPTS(tw_dev);
1392
1393 clear_bit(TW_IN_RESET, &tw_dev->flags);
1394 tw_dev->chrdev_request_id = TW_IOCTL_CHRDEV_FREE;
1395
1396 retval = 0;
1397out:
1398 if (ioctl_reset)
1399 scsi_unblock_requests(tw_dev->host);
1400 return retval;
1401} /* End twl_reset_device_extension() */
1402
1403/* This funciton returns unit geometry in cylinders/heads/sectors */
1404static int twl_scsi_biosparam(struct scsi_device *sdev, struct block_device *bdev, sector_t capacity, int geom[])
1405{
1406 int heads, sectors;
adam radfordf6191062009-10-23 14:52:33 -07001407
1408 if (capacity >= 0x200000) {
1409 heads = 255;
1410 sectors = 63;
1411 } else {
1412 heads = 64;
1413 sectors = 32;
1414 }
1415
1416 geom[0] = heads;
1417 geom[1] = sectors;
1418 geom[2] = sector_div(capacity, heads * sectors); /* cylinders */
1419
1420 return 0;
1421} /* End twl_scsi_biosparam() */
1422
1423/* This is the new scsi eh reset function */
1424static int twl_scsi_eh_reset(struct scsi_cmnd *SCpnt)
1425{
1426 TW_Device_Extension *tw_dev = NULL;
1427 int retval = FAILED;
1428
1429 tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1430
1431 tw_dev->num_resets++;
1432
1433 sdev_printk(KERN_WARNING, SCpnt->device,
1434 "WARNING: (0x%02X:0x%04X): Command (0x%x) timed out, resetting card.\n",
1435 TW_DRIVER, 0x2c, SCpnt->cmnd[0]);
1436
1437 /* Make sure we are not issuing an ioctl or resetting from ioctl */
1438 mutex_lock(&tw_dev->ioctl_lock);
1439
1440 /* Now reset the card and some of the device extension data */
1441 if (twl_reset_device_extension(tw_dev, 0)) {
1442 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x15, "Controller reset failed during scsi host reset");
1443 goto out;
1444 }
1445
1446 retval = SUCCESS;
1447out:
1448 mutex_unlock(&tw_dev->ioctl_lock);
1449 return retval;
1450} /* End twl_scsi_eh_reset() */
1451
1452/* This is the main scsi queue function to handle scsi opcodes */
Bart Van Asscheaf049df2021-10-07 13:46:14 -07001453static int twl_scsi_queue_lck(struct scsi_cmnd *SCpnt)
adam radfordf6191062009-10-23 14:52:33 -07001454{
Bart Van Asscheaf049df2021-10-07 13:46:14 -07001455 void (*done)(struct scsi_cmnd *) = scsi_done;
adam radfordf6191062009-10-23 14:52:33 -07001456 int request_id, retval;
1457 TW_Device_Extension *tw_dev = (TW_Device_Extension *)SCpnt->device->host->hostdata;
1458
1459 /* If we are resetting due to timed out ioctl, report as busy */
1460 if (test_bit(TW_IN_RESET, &tw_dev->flags)) {
1461 retval = SCSI_MLQUEUE_HOST_BUSY;
1462 goto out;
1463 }
1464
adam radfordf6191062009-10-23 14:52:33 -07001465 /* Get a free request id */
1466 twl_get_request_id(tw_dev, &request_id);
1467
1468 /* Save the scsi command for use by the ISR */
1469 tw_dev->srb[request_id] = SCpnt;
1470
adam radfordf6191062009-10-23 14:52:33 -07001471 retval = twl_scsiop_execute_scsi(tw_dev, request_id, NULL, 0, NULL);
1472 if (retval) {
1473 tw_dev->state[request_id] = TW_S_COMPLETED;
1474 twl_free_request_id(tw_dev, request_id);
1475 SCpnt->result = (DID_ERROR << 16);
1476 done(SCpnt);
1477 retval = 0;
1478 }
1479out:
1480 return retval;
1481} /* End twl_scsi_queue() */
1482
Jeff Garzikf2812332010-11-16 02:10:29 -05001483static DEF_SCSI_QCMD(twl_scsi_queue)
1484
adam radfordf6191062009-10-23 14:52:33 -07001485/* This function tells the controller to shut down */
1486static void __twl_shutdown(TW_Device_Extension *tw_dev)
1487{
1488 /* Disable interrupts */
1489 TWL_MASK_INTERRUPTS(tw_dev);
1490
1491 /* Free up the IRQ */
1492 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1493
1494 printk(KERN_WARNING "3w-sas: Shutting down host %d.\n", tw_dev->host->host_no);
1495
1496 /* Tell the card we are shutting down */
1497 if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1498 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x16, "Connection shutdown failed");
1499 } else {
1500 printk(KERN_WARNING "3w-sas: Shutdown complete.\n");
1501 }
1502
1503 /* Clear doorbell interrupt just before exit */
1504 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1505} /* End __twl_shutdown() */
1506
1507/* Wrapper for __twl_shutdown */
1508static void twl_shutdown(struct pci_dev *pdev)
1509{
1510 struct Scsi_Host *host = pci_get_drvdata(pdev);
1511 TW_Device_Extension *tw_dev;
1512
1513 if (!host)
1514 return;
1515
1516 tw_dev = (TW_Device_Extension *)host->hostdata;
1517
Hannes Reinecke17896712021-01-13 10:04:29 +01001518 if (tw_dev->online)
adam radfordf6191062009-10-23 14:52:33 -07001519 __twl_shutdown(tw_dev);
1520} /* End twl_shutdown() */
1521
1522/* This function configures unit settings when a unit is coming on-line */
1523static int twl_slave_configure(struct scsi_device *sdev)
1524{
1525 /* Force 60 second timeout */
1526 blk_queue_rq_timeout(sdev->request_queue, 60 * HZ);
1527
1528 return 0;
1529} /* End twl_slave_configure() */
1530
1531/* scsi_host_template initializer */
1532static struct scsi_host_template driver_template = {
1533 .module = THIS_MODULE,
1534 .name = "3w-sas",
1535 .queuecommand = twl_scsi_queue,
1536 .eh_host_reset_handler = twl_scsi_eh_reset,
1537 .bios_param = twl_scsi_biosparam,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01001538 .change_queue_depth = scsi_change_queue_depth,
adam radfordf6191062009-10-23 14:52:33 -07001539 .can_queue = TW_Q_LENGTH-2,
1540 .slave_configure = twl_slave_configure,
1541 .this_id = -1,
1542 .sg_tablesize = TW_LIBERATOR_MAX_SGL_LENGTH,
1543 .max_sectors = TW_MAX_SECTORS,
1544 .cmd_per_lun = TW_MAX_CMDS_PER_LUN,
adam radfordf6191062009-10-23 14:52:33 -07001545 .shost_attrs = twl_host_attrs,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04001546 .emulated = 1,
1547 .no_write_same = 1,
adam radfordf6191062009-10-23 14:52:33 -07001548};
1549
1550/* This function will probe and initialize a card */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001551static int twl_probe(struct pci_dev *pdev, const struct pci_device_id *dev_id)
adam radfordf6191062009-10-23 14:52:33 -07001552{
1553 struct Scsi_Host *host = NULL;
1554 TW_Device_Extension *tw_dev;
1555 int retval = -ENODEV;
1556 int *ptr_phycount, phycount=0;
1557
1558 retval = pci_enable_device(pdev);
1559 if (retval) {
1560 TW_PRINTK(host, TW_DRIVER, 0x17, "Failed to enable pci device");
1561 goto out_disable_device;
1562 }
1563
1564 pci_set_master(pdev);
1565 pci_try_set_mwi(pdev);
1566
Hannes Reinecke1feb3b022019-02-18 08:34:21 +01001567 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1568 if (retval)
1569 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1570 if (retval) {
Christoph Hellwigb1fa1222018-10-10 18:06:50 +02001571 TW_PRINTK(host, TW_DRIVER, 0x18, "Failed to set dma mask");
1572 retval = -ENODEV;
1573 goto out_disable_device;
1574 }
adam radfordf6191062009-10-23 14:52:33 -07001575
1576 host = scsi_host_alloc(&driver_template, sizeof(TW_Device_Extension));
1577 if (!host) {
1578 TW_PRINTK(host, TW_DRIVER, 0x19, "Failed to allocate memory for device extension");
1579 retval = -ENOMEM;
1580 goto out_disable_device;
1581 }
1582 tw_dev = shost_priv(host);
1583
1584 /* Save values to device extension */
1585 tw_dev->host = host;
1586 tw_dev->tw_pci_dev = pdev;
1587
1588 if (twl_initialize_device_extension(tw_dev)) {
1589 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1a, "Failed to initialize device extension");
Anton Vasilyev4dc98c12018-07-27 16:51:57 +03001590 retval = -ENOMEM;
adam radfordf6191062009-10-23 14:52:33 -07001591 goto out_free_device_extension;
1592 }
1593
1594 /* Request IO regions */
1595 retval = pci_request_regions(pdev, "3w-sas");
1596 if (retval) {
1597 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1b, "Failed to get mem region");
1598 goto out_free_device_extension;
1599 }
1600
1601 /* Save base address, use region 1 */
1602 tw_dev->base_addr = pci_iomap(pdev, 1, 0);
1603 if (!tw_dev->base_addr) {
1604 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1c, "Failed to ioremap");
Anton Vasilyev4dc98c12018-07-27 16:51:57 +03001605 retval = -ENOMEM;
adam radfordf6191062009-10-23 14:52:33 -07001606 goto out_release_mem_region;
1607 }
1608
1609 /* Disable interrupts on the card */
1610 TWL_MASK_INTERRUPTS(tw_dev);
1611
1612 /* Initialize the card */
1613 if (twl_reset_sequence(tw_dev, 0)) {
1614 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1d, "Controller reset failed during probe");
Anton Vasilyev4dc98c12018-07-27 16:51:57 +03001615 retval = -ENOMEM;
adam radfordf6191062009-10-23 14:52:33 -07001616 goto out_iounmap;
1617 }
1618
1619 /* Set host specific parameters */
1620 host->max_id = TW_MAX_UNITS;
1621 host->max_cmd_len = TW_MAX_CDB_LEN;
1622 host->max_lun = TW_MAX_LUNS;
1623 host->max_channel = 0;
1624
1625 /* Register the card with the kernel SCSI layer */
1626 retval = scsi_add_host(host, &pdev->dev);
1627 if (retval) {
1628 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1e, "scsi add host failed");
1629 goto out_iounmap;
1630 }
1631
1632 pci_set_drvdata(pdev, host);
1633
1634 printk(KERN_WARNING "3w-sas: scsi%d: Found an LSI 3ware %s Controller at 0x%llx, IRQ: %d.\n",
1635 host->host_no,
1636 (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1637 TW_PARAM_MODEL, TW_PARAM_MODEL_LENGTH),
1638 (u64)pci_resource_start(pdev, 1), pdev->irq);
1639
1640 ptr_phycount = twl_get_param(tw_dev, 2, TW_PARAM_PHY_SUMMARY_TABLE,
1641 TW_PARAM_PHYCOUNT, TW_PARAM_PHYCOUNT_LENGTH);
1642 if (ptr_phycount)
1643 phycount = le32_to_cpu(*(int *)ptr_phycount);
1644
1645 printk(KERN_WARNING "3w-sas: scsi%d: Firmware %s, BIOS %s, Phys: %d.\n",
1646 host->host_no,
1647 (char *)twl_get_param(tw_dev, 1, TW_VERSION_TABLE,
1648 TW_PARAM_FWVER, TW_PARAM_FWVER_LENGTH),
1649 (char *)twl_get_param(tw_dev, 2, TW_VERSION_TABLE,
1650 TW_PARAM_BIOSVER, TW_PARAM_BIOSVER_LENGTH),
1651 phycount);
1652
1653 /* Try to enable MSI */
1654 if (use_msi && !pci_enable_msi(pdev))
1655 set_bit(TW_USING_MSI, &tw_dev->flags);
1656
1657 /* Now setup the interrupt handler */
1658 retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1659 if (retval) {
1660 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x1f, "Error requesting IRQ");
1661 goto out_remove_host;
1662 }
1663
1664 twl_device_extension_list[twl_device_extension_count] = tw_dev;
1665 twl_device_extension_count++;
1666
1667 /* Re-enable interrupts on the card */
1668 TWL_UNMASK_INTERRUPTS(tw_dev);
Hannes Reinecke17896712021-01-13 10:04:29 +01001669
adam radfordf6191062009-10-23 14:52:33 -07001670 /* Finally, scan the host */
1671 scsi_scan_host(host);
1672
1673 /* Add sysfs binary files */
1674 if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr))
1675 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x20, "Failed to create sysfs binary file: 3ware_aen_read");
1676 if (sysfs_create_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr))
1677 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x21, "Failed to create sysfs binary file: 3ware_compat_info");
1678
1679 if (twl_major == -1) {
1680 if ((twl_major = register_chrdev (0, "twl", &twl_fops)) < 0)
1681 TW_PRINTK(host, TW_DRIVER, 0x22, "Failed to register character device");
1682 }
1683 tw_dev->online = 1;
1684 return 0;
1685
1686out_remove_host:
1687 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1688 pci_disable_msi(pdev);
1689 scsi_remove_host(host);
1690out_iounmap:
1691 iounmap(tw_dev->base_addr);
1692out_release_mem_region:
1693 pci_release_regions(pdev);
1694out_free_device_extension:
1695 twl_free_device_extension(tw_dev);
1696 scsi_host_put(host);
1697out_disable_device:
1698 pci_disable_device(pdev);
1699
1700 return retval;
1701} /* End twl_probe() */
1702
1703/* This function is called to remove a device */
1704static void twl_remove(struct pci_dev *pdev)
1705{
1706 struct Scsi_Host *host = pci_get_drvdata(pdev);
1707 TW_Device_Extension *tw_dev;
1708
1709 if (!host)
1710 return;
1711
1712 tw_dev = (TW_Device_Extension *)host->hostdata;
1713
1714 if (!tw_dev->online)
1715 return;
1716
1717 /* Remove sysfs binary files */
1718 sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_aen_read_attr);
1719 sysfs_remove_bin_file(&host->shost_dev.kobj, &twl_sysfs_compat_info_attr);
1720
1721 scsi_remove_host(tw_dev->host);
1722
1723 /* Unregister character device */
1724 if (twl_major >= 0) {
1725 unregister_chrdev(twl_major, "twl");
1726 twl_major = -1;
1727 }
1728
1729 /* Shutdown the card */
1730 __twl_shutdown(tw_dev);
1731
1732 /* Disable MSI if enabled */
1733 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1734 pci_disable_msi(pdev);
1735
1736 /* Free IO remapping */
1737 iounmap(tw_dev->base_addr);
1738
1739 /* Free up the mem region */
1740 pci_release_regions(pdev);
1741
1742 /* Free up device extension resources */
1743 twl_free_device_extension(tw_dev);
1744
1745 scsi_host_put(tw_dev->host);
1746 pci_disable_device(pdev);
1747 twl_device_extension_count--;
1748} /* End twl_remove() */
1749
adam radfordf6191062009-10-23 14:52:33 -07001750/* This function is called on PCI suspend */
Vaibhav Gupta99769d82020-11-02 22:17:25 +05301751static int __maybe_unused twl_suspend(struct device *dev)
adam radfordf6191062009-10-23 14:52:33 -07001752{
Vaibhav Gupta99769d82020-11-02 22:17:25 +05301753 struct Scsi_Host *host = dev_get_drvdata(dev);
adam radfordf6191062009-10-23 14:52:33 -07001754 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1755
1756 printk(KERN_WARNING "3w-sas: Suspending host %d.\n", tw_dev->host->host_no);
1757 /* Disable interrupts */
1758 TWL_MASK_INTERRUPTS(tw_dev);
1759
1760 free_irq(tw_dev->tw_pci_dev->irq, tw_dev);
1761
1762 /* Tell the card we are shutting down */
1763 if (twl_initconnection(tw_dev, 1, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL)) {
1764 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x23, "Connection shutdown failed during suspend");
1765 } else {
1766 printk(KERN_WARNING "3w-sas: Suspend complete.\n");
1767 }
1768
1769 /* Clear doorbell interrupt */
1770 TWL_CLEAR_DB_INTERRUPT(tw_dev);
1771
adam radfordf6191062009-10-23 14:52:33 -07001772 return 0;
1773} /* End twl_suspend() */
1774
1775/* This function is called on PCI resume */
Vaibhav Gupta99769d82020-11-02 22:17:25 +05301776static int __maybe_unused twl_resume(struct device *dev)
adam radfordf6191062009-10-23 14:52:33 -07001777{
1778 int retval = 0;
Vaibhav Gupta99769d82020-11-02 22:17:25 +05301779 struct pci_dev *pdev = to_pci_dev(dev);
adam radfordf6191062009-10-23 14:52:33 -07001780 struct Scsi_Host *host = pci_get_drvdata(pdev);
1781 TW_Device_Extension *tw_dev = (TW_Device_Extension *)host->hostdata;
1782
1783 printk(KERN_WARNING "3w-sas: Resuming host %d.\n", tw_dev->host->host_no);
adam radfordf6191062009-10-23 14:52:33 -07001784 pci_try_set_mwi(pdev);
1785
Hannes Reinecke1feb3b022019-02-18 08:34:21 +01001786 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1787 if (retval)
1788 retval = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1789 if (retval) {
Christoph Hellwigb1fa1222018-10-10 18:06:50 +02001790 TW_PRINTK(host, TW_DRIVER, 0x25, "Failed to set dma mask during resume");
1791 retval = -ENODEV;
1792 goto out_disable_device;
1793 }
adam radfordf6191062009-10-23 14:52:33 -07001794
1795 /* Initialize the card */
1796 if (twl_reset_sequence(tw_dev, 0)) {
1797 retval = -ENODEV;
1798 goto out_disable_device;
1799 }
1800
1801 /* Now setup the interrupt handler */
1802 retval = request_irq(pdev->irq, twl_interrupt, IRQF_SHARED, "3w-sas", tw_dev);
1803 if (retval) {
1804 TW_PRINTK(tw_dev->host, TW_DRIVER, 0x26, "Error requesting IRQ during resume");
1805 retval = -ENODEV;
1806 goto out_disable_device;
1807 }
1808
1809 /* Now enable MSI if enabled */
1810 if (test_bit(TW_USING_MSI, &tw_dev->flags))
1811 pci_enable_msi(pdev);
1812
1813 /* Re-enable interrupts on the card */
1814 TWL_UNMASK_INTERRUPTS(tw_dev);
1815
1816 printk(KERN_WARNING "3w-sas: Resume complete.\n");
1817 return 0;
1818
1819out_disable_device:
1820 scsi_remove_host(host);
adam radfordf6191062009-10-23 14:52:33 -07001821
1822 return retval;
1823} /* End twl_resume() */
adam radfordf6191062009-10-23 14:52:33 -07001824
1825/* PCI Devices supported by this driver */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001826static struct pci_device_id twl_pci_tbl[] = {
adam radfordf6191062009-10-23 14:52:33 -07001827 { PCI_VDEVICE(3WARE, PCI_DEVICE_ID_3WARE_9750) },
1828 { }
1829};
1830MODULE_DEVICE_TABLE(pci, twl_pci_tbl);
1831
Vaibhav Gupta99769d82020-11-02 22:17:25 +05301832static SIMPLE_DEV_PM_OPS(twl_pm_ops, twl_suspend, twl_resume);
1833
adam radfordf6191062009-10-23 14:52:33 -07001834/* pci_driver initializer */
1835static struct pci_driver twl_driver = {
1836 .name = "3w-sas",
1837 .id_table = twl_pci_tbl,
1838 .probe = twl_probe,
1839 .remove = twl_remove,
Vaibhav Gupta99769d82020-11-02 22:17:25 +05301840 .driver.pm = &twl_pm_ops,
adam radfordf6191062009-10-23 14:52:33 -07001841 .shutdown = twl_shutdown
1842};
1843
1844/* This function is called on driver initialization */
1845static int __init twl_init(void)
1846{
1847 printk(KERN_INFO "LSI 3ware SAS/SATA-RAID Controller device driver for Linux v%s.\n", TW_DRIVER_VERSION);
1848
1849 return pci_register_driver(&twl_driver);
1850} /* End twl_init() */
1851
1852/* This function is called on driver exit */
1853static void __exit twl_exit(void)
1854{
1855 pci_unregister_driver(&twl_driver);
1856} /* End twl_exit() */
1857
1858module_init(twl_init);
1859module_exit(twl_exit);
1860