blob: e94993e2eb4689ac7ddd07f838348ff9bb0ed678 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
4 *
5 * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05306 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307 * (mailto:DL-MPTFusionLinux@lsi.com)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
29
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
43 */
44
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053045#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/errno.h>
48#include <linux/init.h>
49#include <linux/slab.h>
50#include <linux/types.h>
51#include <linux/pci.h>
52#include <linux/kdev_t.h>
53#include <linux/blkdev.h>
54#include <linux/delay.h>
55#include <linux/interrupt.h>
56#include <linux/dma-mapping.h>
57#include <linux/io.h>
58#include <linux/time.h>
59#include <linux/kthread.h>
60#include <linux/aer.h>
61
62
63#include "mpt3sas_base.h"
64
65static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
66
67
68#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
69
70 /* maximum controller queue depth */
71#define MAX_HBA_QUEUE_DEPTH 30000
72#define MAX_CHAIN_DEPTH 100000
73static int max_queue_depth = -1;
74module_param(max_queue_depth, int, 0);
75MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
76
77static int max_sgl_entries = -1;
78module_param(max_sgl_entries, int, 0);
79MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
80
81static int msix_disable = -1;
82module_param(msix_disable, int, 0);
83MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
84
Sreekanth Reddy9c500062013-08-14 18:23:20 +053085static int max_msix_vectors = 8;
86module_param(max_msix_vectors, int, 0);
87MODULE_PARM_DESC(max_msix_vectors,
88 " max msix vectors - (default=8)");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053089
90static int mpt3sas_fwfault_debug;
91MODULE_PARM_DESC(mpt3sas_fwfault_debug,
92 " enable detection of firmware fault and halt firmware - (default=0)");
93
94
95/**
96 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
97 *
98 */
99static int
100_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
101{
102 int ret = param_set_int(val, kp);
103 struct MPT3SAS_ADAPTER *ioc;
104
105 if (ret)
106 return ret;
107
108 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
109 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
110 ioc->fwfault_debug = mpt3sas_fwfault_debug;
111 return 0;
112}
113module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
114 param_get_int, &mpt3sas_fwfault_debug, 0644);
115
116/**
117 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
118 * @arg: input argument, used to derive ioc
119 *
120 * Return 0 if controller is removed from pci subsystem.
121 * Return -1 for other case.
122 */
123static int mpt3sas_remove_dead_ioc_func(void *arg)
124{
125 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
126 struct pci_dev *pdev;
127
128 if ((ioc == NULL))
129 return -1;
130
131 pdev = ioc->pdev;
132 if ((pdev == NULL))
133 return -1;
Rafael J. Wysocki64cdb412014-01-10 15:27:56 +0100134 pci_stop_and_remove_bus_device_locked(pdev);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530135 return 0;
136}
137
138/**
139 * _base_fault_reset_work - workq handling ioc fault conditions
140 * @work: input argument, used to derive ioc
141 * Context: sleep.
142 *
143 * Return nothing.
144 */
145static void
146_base_fault_reset_work(struct work_struct *work)
147{
148 struct MPT3SAS_ADAPTER *ioc =
149 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
150 unsigned long flags;
151 u32 doorbell;
152 int rc;
153 struct task_struct *p;
154
155
156 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
157 if (ioc->shost_recovery)
158 goto rearm_timer;
159 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
160
161 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
162 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
163 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
164 ioc->name);
165
166 /*
167 * Call _scsih_flush_pending_cmds callback so that we flush all
168 * pending commands back to OS. This call is required to aovid
169 * deadlock at block layer. Dead IOC will fail to do diag reset,
170 * and this call is safe since dead ioc will never return any
171 * command back from HW.
172 */
173 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
174 /*
175 * Set remove_host flag early since kernel thread will
176 * take some time to execute.
177 */
178 ioc->remove_host = 1;
179 /*Remove the Dead Host */
180 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
181 "mpt3sas_dead_ioc_%d", ioc->id);
182 if (IS_ERR(p))
183 pr_err(MPT3SAS_FMT
184 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
185 ioc->name, __func__);
186 else
187 pr_err(MPT3SAS_FMT
188 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
189 ioc->name, __func__);
190 return; /* don't rearm timer */
191 }
192
193 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
194 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
195 FORCE_BIG_HAMMER);
196 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
197 __func__, (rc == 0) ? "success" : "failed");
198 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
199 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
200 mpt3sas_base_fault_info(ioc, doorbell &
201 MPI2_DOORBELL_DATA_MASK);
202 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
203 MPI2_IOC_STATE_OPERATIONAL)
204 return; /* don't rearm timer */
205 }
206
207 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
208 rearm_timer:
209 if (ioc->fault_reset_work_q)
210 queue_delayed_work(ioc->fault_reset_work_q,
211 &ioc->fault_reset_work,
212 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
213 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
214}
215
216/**
217 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
218 * @ioc: per adapter object
219 * Context: sleep.
220 *
221 * Return nothing.
222 */
223void
224mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
225{
226 unsigned long flags;
227
228 if (ioc->fault_reset_work_q)
229 return;
230
231 /* initialize fault polling */
232
233 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
234 snprintf(ioc->fault_reset_work_q_name,
235 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
236 ioc->fault_reset_work_q =
237 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
238 if (!ioc->fault_reset_work_q) {
239 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
240 ioc->name, __func__, __LINE__);
241 return;
242 }
243 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
244 if (ioc->fault_reset_work_q)
245 queue_delayed_work(ioc->fault_reset_work_q,
246 &ioc->fault_reset_work,
247 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
248 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
249}
250
251/**
252 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
253 * @ioc: per adapter object
254 * Context: sleep.
255 *
256 * Return nothing.
257 */
258void
259mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
260{
261 unsigned long flags;
262 struct workqueue_struct *wq;
263
264 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
265 wq = ioc->fault_reset_work_q;
266 ioc->fault_reset_work_q = NULL;
267 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
268 if (wq) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +0530269 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530270 flush_workqueue(wq);
271 destroy_workqueue(wq);
272 }
273}
274
275/**
276 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
277 * @ioc: per adapter object
278 * @fault_code: fault code
279 *
280 * Return nothing.
281 */
282void
283mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
284{
285 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
286 ioc->name, fault_code);
287}
288
289/**
290 * mpt3sas_halt_firmware - halt's mpt controller firmware
291 * @ioc: per adapter object
292 *
293 * For debugging timeout related issues. Writing 0xCOFFEE00
294 * to the doorbell register will halt controller firmware. With
295 * the purpose to stop both driver and firmware, the enduser can
296 * obtain a ring buffer from controller UART.
297 */
298void
299mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
300{
301 u32 doorbell;
302
303 if (!ioc->fwfault_debug)
304 return;
305
306 dump_stack();
307
308 doorbell = readl(&ioc->chip->Doorbell);
309 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
310 mpt3sas_base_fault_info(ioc , doorbell);
311 else {
312 writel(0xC0FFEE00, &ioc->chip->Doorbell);
313 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
314 ioc->name);
315 }
316
317 if (ioc->fwfault_debug == 2)
318 for (;;)
319 ;
320 else
321 panic("panic in %s\n", __func__);
322}
323
324#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
325/**
326 * _base_sas_ioc_info - verbose translation of the ioc status
327 * @ioc: per adapter object
328 * @mpi_reply: reply mf payload returned from firmware
329 * @request_hdr: request mf
330 *
331 * Return nothing.
332 */
333static void
334_base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
335 MPI2RequestHeader_t *request_hdr)
336{
337 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
338 MPI2_IOCSTATUS_MASK;
339 char *desc = NULL;
340 u16 frame_sz;
341 char *func_str = NULL;
342
343 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
344 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
345 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
346 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
347 return;
348
349 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
350 return;
351
352 switch (ioc_status) {
353
354/****************************************************************************
355* Common IOCStatus values for all replies
356****************************************************************************/
357
358 case MPI2_IOCSTATUS_INVALID_FUNCTION:
359 desc = "invalid function";
360 break;
361 case MPI2_IOCSTATUS_BUSY:
362 desc = "busy";
363 break;
364 case MPI2_IOCSTATUS_INVALID_SGL:
365 desc = "invalid sgl";
366 break;
367 case MPI2_IOCSTATUS_INTERNAL_ERROR:
368 desc = "internal error";
369 break;
370 case MPI2_IOCSTATUS_INVALID_VPID:
371 desc = "invalid vpid";
372 break;
373 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
374 desc = "insufficient resources";
375 break;
376 case MPI2_IOCSTATUS_INVALID_FIELD:
377 desc = "invalid field";
378 break;
379 case MPI2_IOCSTATUS_INVALID_STATE:
380 desc = "invalid state";
381 break;
382 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
383 desc = "op state not supported";
384 break;
385
386/****************************************************************************
387* Config IOCStatus values
388****************************************************************************/
389
390 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
391 desc = "config invalid action";
392 break;
393 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
394 desc = "config invalid type";
395 break;
396 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
397 desc = "config invalid page";
398 break;
399 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
400 desc = "config invalid data";
401 break;
402 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
403 desc = "config no defaults";
404 break;
405 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
406 desc = "config cant commit";
407 break;
408
409/****************************************************************************
410* SCSI IO Reply
411****************************************************************************/
412
413 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
414 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
415 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
416 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
417 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
418 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
419 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
420 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
421 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
422 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
423 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
424 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
425 break;
426
427/****************************************************************************
428* For use by SCSI Initiator and SCSI Target end-to-end data protection
429****************************************************************************/
430
431 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
432 desc = "eedp guard error";
433 break;
434 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
435 desc = "eedp ref tag error";
436 break;
437 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
438 desc = "eedp app tag error";
439 break;
440
441/****************************************************************************
442* SCSI Target values
443****************************************************************************/
444
445 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
446 desc = "target invalid io index";
447 break;
448 case MPI2_IOCSTATUS_TARGET_ABORTED:
449 desc = "target aborted";
450 break;
451 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
452 desc = "target no conn retryable";
453 break;
454 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
455 desc = "target no connection";
456 break;
457 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
458 desc = "target xfer count mismatch";
459 break;
460 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
461 desc = "target data offset error";
462 break;
463 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
464 desc = "target too much write data";
465 break;
466 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
467 desc = "target iu too short";
468 break;
469 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
470 desc = "target ack nak timeout";
471 break;
472 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
473 desc = "target nak received";
474 break;
475
476/****************************************************************************
477* Serial Attached SCSI values
478****************************************************************************/
479
480 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
481 desc = "smp request failed";
482 break;
483 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
484 desc = "smp data overrun";
485 break;
486
487/****************************************************************************
488* Diagnostic Buffer Post / Diagnostic Release values
489****************************************************************************/
490
491 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
492 desc = "diagnostic released";
493 break;
494 default:
495 break;
496 }
497
498 if (!desc)
499 return;
500
501 switch (request_hdr->Function) {
502 case MPI2_FUNCTION_CONFIG:
503 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
504 func_str = "config_page";
505 break;
506 case MPI2_FUNCTION_SCSI_TASK_MGMT:
507 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
508 func_str = "task_mgmt";
509 break;
510 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
511 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
512 func_str = "sas_iounit_ctl";
513 break;
514 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
515 frame_sz = sizeof(Mpi2SepRequest_t);
516 func_str = "enclosure";
517 break;
518 case MPI2_FUNCTION_IOC_INIT:
519 frame_sz = sizeof(Mpi2IOCInitRequest_t);
520 func_str = "ioc_init";
521 break;
522 case MPI2_FUNCTION_PORT_ENABLE:
523 frame_sz = sizeof(Mpi2PortEnableRequest_t);
524 func_str = "port_enable";
525 break;
526 case MPI2_FUNCTION_SMP_PASSTHROUGH:
527 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
528 func_str = "smp_passthru";
529 break;
530 default:
531 frame_sz = 32;
532 func_str = "unknown";
533 break;
534 }
535
536 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
537 ioc->name, desc, ioc_status, request_hdr, func_str);
538
539 _debug_dump_mf(request_hdr, frame_sz/4);
540}
541
542/**
543 * _base_display_event_data - verbose translation of firmware asyn events
544 * @ioc: per adapter object
545 * @mpi_reply: reply mf payload returned from firmware
546 *
547 * Return nothing.
548 */
549static void
550_base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
551 Mpi2EventNotificationReply_t *mpi_reply)
552{
553 char *desc = NULL;
554 u16 event;
555
556 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
557 return;
558
559 event = le16_to_cpu(mpi_reply->Event);
560
561 switch (event) {
562 case MPI2_EVENT_LOG_DATA:
563 desc = "Log Data";
564 break;
565 case MPI2_EVENT_STATE_CHANGE:
566 desc = "Status Change";
567 break;
568 case MPI2_EVENT_HARD_RESET_RECEIVED:
569 desc = "Hard Reset Received";
570 break;
571 case MPI2_EVENT_EVENT_CHANGE:
572 desc = "Event Change";
573 break;
574 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
575 desc = "Device Status Change";
576 break;
577 case MPI2_EVENT_IR_OPERATION_STATUS:
578 desc = "IR Operation Status";
579 break;
580 case MPI2_EVENT_SAS_DISCOVERY:
581 {
582 Mpi2EventDataSasDiscovery_t *event_data =
583 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
584 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
585 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
586 "start" : "stop");
587 if (event_data->DiscoveryStatus)
588 pr_info("discovery_status(0x%08x)",
589 le32_to_cpu(event_data->DiscoveryStatus));
590 pr_info("\n");
591 return;
592 }
593 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
594 desc = "SAS Broadcast Primitive";
595 break;
596 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
597 desc = "SAS Init Device Status Change";
598 break;
599 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
600 desc = "SAS Init Table Overflow";
601 break;
602 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
603 desc = "SAS Topology Change List";
604 break;
605 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
606 desc = "SAS Enclosure Device Status Change";
607 break;
608 case MPI2_EVENT_IR_VOLUME:
609 desc = "IR Volume";
610 break;
611 case MPI2_EVENT_IR_PHYSICAL_DISK:
612 desc = "IR Physical Disk";
613 break;
614 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
615 desc = "IR Configuration Change List";
616 break;
617 case MPI2_EVENT_LOG_ENTRY_ADDED:
618 desc = "Log Entry Added";
619 break;
620 }
621
622 if (!desc)
623 return;
624
625 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
626}
627#endif
628
629/**
630 * _base_sas_log_info - verbose translation of firmware log info
631 * @ioc: per adapter object
632 * @log_info: log info
633 *
634 * Return nothing.
635 */
636static void
637_base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
638{
639 union loginfo_type {
640 u32 loginfo;
641 struct {
642 u32 subcode:16;
643 u32 code:8;
644 u32 originator:4;
645 u32 bus_type:4;
646 } dw;
647 };
648 union loginfo_type sas_loginfo;
649 char *originator_str = NULL;
650
651 sas_loginfo.loginfo = log_info;
652 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
653 return;
654
655 /* each nexus loss loginfo */
656 if (log_info == 0x31170000)
657 return;
658
659 /* eat the loginfos associated with task aborts */
660 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
661 0x31140000 || log_info == 0x31130000))
662 return;
663
664 switch (sas_loginfo.dw.originator) {
665 case 0:
666 originator_str = "IOP";
667 break;
668 case 1:
669 originator_str = "PL";
670 break;
671 case 2:
672 originator_str = "IR";
673 break;
674 }
675
676 pr_warn(MPT3SAS_FMT
677 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
678 ioc->name, log_info,
679 originator_str, sas_loginfo.dw.code,
680 sas_loginfo.dw.subcode);
681}
682
683/**
684 * _base_display_reply_info -
685 * @ioc: per adapter object
686 * @smid: system request message index
687 * @msix_index: MSIX table index supplied by the OS
688 * @reply: reply message frame(lower 32bit addr)
689 *
690 * Return nothing.
691 */
692static void
693_base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
694 u32 reply)
695{
696 MPI2DefaultReply_t *mpi_reply;
697 u16 ioc_status;
698 u32 loginfo = 0;
699
700 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
701 if (unlikely(!mpi_reply)) {
702 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
703 ioc->name, __FILE__, __LINE__, __func__);
704 return;
705 }
706 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
707#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
708 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
709 (ioc->logging_level & MPT_DEBUG_REPLY)) {
710 _base_sas_ioc_info(ioc , mpi_reply,
711 mpt3sas_base_get_msg_frame(ioc, smid));
712 }
713#endif
714 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
715 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
716 _base_sas_log_info(ioc, loginfo);
717 }
718
719 if (ioc_status || loginfo) {
720 ioc_status &= MPI2_IOCSTATUS_MASK;
721 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
722 }
723}
724
725/**
726 * mpt3sas_base_done - base internal command completion routine
727 * @ioc: per adapter object
728 * @smid: system request message index
729 * @msix_index: MSIX table index supplied by the OS
730 * @reply: reply message frame(lower 32bit addr)
731 *
732 * Return 1 meaning mf should be freed from _base_interrupt
733 * 0 means the mf is freed from this function.
734 */
735u8
736mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
737 u32 reply)
738{
739 MPI2DefaultReply_t *mpi_reply;
740
741 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
742 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
743 return 1;
744
745 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
746 return 1;
747
748 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
749 if (mpi_reply) {
750 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
751 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
752 }
753 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
754
755 complete(&ioc->base_cmds.done);
756 return 1;
757}
758
759/**
760 * _base_async_event - main callback handler for firmware asyn events
761 * @ioc: per adapter object
762 * @msix_index: MSIX table index supplied by the OS
763 * @reply: reply message frame(lower 32bit addr)
764 *
765 * Return 1 meaning mf should be freed from _base_interrupt
766 * 0 means the mf is freed from this function.
767 */
768static u8
769_base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
770{
771 Mpi2EventNotificationReply_t *mpi_reply;
772 Mpi2EventAckRequest_t *ack_request;
773 u16 smid;
774
775 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
776 if (!mpi_reply)
777 return 1;
778 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
779 return 1;
780#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
781 _base_display_event_data(ioc, mpi_reply);
782#endif
783 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
784 goto out;
785 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
786 if (!smid) {
787 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
788 ioc->name, __func__);
789 goto out;
790 }
791
792 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
793 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
794 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
795 ack_request->Event = mpi_reply->Event;
796 ack_request->EventContext = mpi_reply->EventContext;
797 ack_request->VF_ID = 0; /* TODO */
798 ack_request->VP_ID = 0;
799 mpt3sas_base_put_smid_default(ioc, smid);
800
801 out:
802
803 /* scsih callback handler */
804 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
805
806 /* ctl callback handler */
807 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
808
809 return 1;
810}
811
812/**
813 * _base_get_cb_idx - obtain the callback index
814 * @ioc: per adapter object
815 * @smid: system request message index
816 *
817 * Return callback index.
818 */
819static u8
820_base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
821{
822 int i;
823 u8 cb_idx;
824
825 if (smid < ioc->hi_priority_smid) {
826 i = smid - 1;
827 cb_idx = ioc->scsi_lookup[i].cb_idx;
828 } else if (smid < ioc->internal_smid) {
829 i = smid - ioc->hi_priority_smid;
830 cb_idx = ioc->hpr_lookup[i].cb_idx;
831 } else if (smid <= ioc->hba_queue_depth) {
832 i = smid - ioc->internal_smid;
833 cb_idx = ioc->internal_lookup[i].cb_idx;
834 } else
835 cb_idx = 0xFF;
836 return cb_idx;
837}
838
839/**
840 * _base_mask_interrupts - disable interrupts
841 * @ioc: per adapter object
842 *
843 * Disabling ResetIRQ, Reply and Doorbell Interrupts
844 *
845 * Return nothing.
846 */
847static void
848_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
849{
850 u32 him_register;
851
852 ioc->mask_interrupts = 1;
853 him_register = readl(&ioc->chip->HostInterruptMask);
854 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
855 writel(him_register, &ioc->chip->HostInterruptMask);
856 readl(&ioc->chip->HostInterruptMask);
857}
858
859/**
860 * _base_unmask_interrupts - enable interrupts
861 * @ioc: per adapter object
862 *
863 * Enabling only Reply Interrupts
864 *
865 * Return nothing.
866 */
867static void
868_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
869{
870 u32 him_register;
871
872 him_register = readl(&ioc->chip->HostInterruptMask);
873 him_register &= ~MPI2_HIM_RIM;
874 writel(him_register, &ioc->chip->HostInterruptMask);
875 ioc->mask_interrupts = 0;
876}
877
878union reply_descriptor {
879 u64 word;
880 struct {
881 u32 low;
882 u32 high;
883 } u;
884};
885
886/**
887 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
888 * @irq: irq number (not used)
889 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
890 * @r: pt_regs pointer (not used)
891 *
892 * Return IRQ_HANDLE if processed, else IRQ_NONE.
893 */
894static irqreturn_t
895_base_interrupt(int irq, void *bus_id)
896{
897 struct adapter_reply_queue *reply_q = bus_id;
898 union reply_descriptor rd;
899 u32 completed_cmds;
900 u8 request_desript_type;
901 u16 smid;
902 u8 cb_idx;
903 u32 reply;
904 u8 msix_index = reply_q->msix_index;
905 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
906 Mpi2ReplyDescriptorsUnion_t *rpf;
907 u8 rc;
908
909 if (ioc->mask_interrupts)
910 return IRQ_NONE;
911
912 if (!atomic_add_unless(&reply_q->busy, 1, 1))
913 return IRQ_NONE;
914
915 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
916 request_desript_type = rpf->Default.ReplyFlags
917 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
918 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
919 atomic_dec(&reply_q->busy);
920 return IRQ_NONE;
921 }
922
923 completed_cmds = 0;
924 cb_idx = 0xFF;
925 do {
926 rd.word = le64_to_cpu(rpf->Words);
927 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
928 goto out;
929 reply = 0;
930 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
931 if (request_desript_type ==
932 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
933 request_desript_type ==
934 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
935 cb_idx = _base_get_cb_idx(ioc, smid);
936 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
937 (likely(mpt_callbacks[cb_idx] != NULL))) {
938 rc = mpt_callbacks[cb_idx](ioc, smid,
939 msix_index, 0);
940 if (rc)
941 mpt3sas_base_free_smid(ioc, smid);
942 }
943 } else if (request_desript_type ==
944 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
945 reply = le32_to_cpu(
946 rpf->AddressReply.ReplyFrameAddress);
947 if (reply > ioc->reply_dma_max_address ||
948 reply < ioc->reply_dma_min_address)
949 reply = 0;
950 if (smid) {
951 cb_idx = _base_get_cb_idx(ioc, smid);
952 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
953 (likely(mpt_callbacks[cb_idx] != NULL))) {
954 rc = mpt_callbacks[cb_idx](ioc, smid,
955 msix_index, reply);
956 if (reply)
957 _base_display_reply_info(ioc,
958 smid, msix_index, reply);
959 if (rc)
960 mpt3sas_base_free_smid(ioc,
961 smid);
962 }
963 } else {
964 _base_async_event(ioc, msix_index, reply);
965 }
966
967 /* reply free queue handling */
968 if (reply) {
969 ioc->reply_free_host_index =
970 (ioc->reply_free_host_index ==
971 (ioc->reply_free_queue_depth - 1)) ?
972 0 : ioc->reply_free_host_index + 1;
973 ioc->reply_free[ioc->reply_free_host_index] =
974 cpu_to_le32(reply);
975 wmb();
976 writel(ioc->reply_free_host_index,
977 &ioc->chip->ReplyFreeHostIndex);
978 }
979 }
980
981 rpf->Words = cpu_to_le64(ULLONG_MAX);
982 reply_q->reply_post_host_index =
983 (reply_q->reply_post_host_index ==
984 (ioc->reply_post_queue_depth - 1)) ? 0 :
985 reply_q->reply_post_host_index + 1;
986 request_desript_type =
987 reply_q->reply_post_free[reply_q->reply_post_host_index].
988 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
989 completed_cmds++;
990 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
991 goto out;
992 if (!reply_q->reply_post_host_index)
993 rpf = reply_q->reply_post_free;
994 else
995 rpf++;
996 } while (1);
997
998 out:
999
1000 if (!completed_cmds) {
1001 atomic_dec(&reply_q->busy);
1002 return IRQ_NONE;
1003 }
1004
1005 wmb();
1006 writel(reply_q->reply_post_host_index | (msix_index <<
1007 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
1008 atomic_dec(&reply_q->busy);
1009 return IRQ_HANDLED;
1010}
1011
1012/**
1013 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1014 * @ioc: per adapter object
1015 *
1016 */
1017static inline int
1018_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1019{
1020 return (ioc->facts.IOCCapabilities &
1021 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1022}
1023
1024/**
1025 * mpt3sas_base_flush_reply_queues - flushing the MSIX reply queues
1026 * @ioc: per adapter object
1027 * Context: ISR conext
1028 *
1029 * Called when a Task Management request has completed. We want
1030 * to flush the other reply queues so all the outstanding IO has been
1031 * completed back to OS before we process the TM completetion.
1032 *
1033 * Return nothing.
1034 */
1035void
1036mpt3sas_base_flush_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1037{
1038 struct adapter_reply_queue *reply_q;
1039
1040 /* If MSIX capability is turned off
1041 * then multi-queues are not enabled
1042 */
1043 if (!_base_is_controller_msix_enabled(ioc))
1044 return;
1045
1046 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1047 if (ioc->shost_recovery)
1048 return;
1049 /* TMs are on msix_index == 0 */
1050 if (reply_q->msix_index == 0)
1051 continue;
1052 _base_interrupt(reply_q->vector, (void *)reply_q);
1053 }
1054}
1055
1056/**
1057 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1058 * @cb_idx: callback index
1059 *
1060 * Return nothing.
1061 */
1062void
1063mpt3sas_base_release_callback_handler(u8 cb_idx)
1064{
1065 mpt_callbacks[cb_idx] = NULL;
1066}
1067
1068/**
1069 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1070 * @cb_func: callback function
1071 *
1072 * Returns cb_func.
1073 */
1074u8
1075mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1076{
1077 u8 cb_idx;
1078
1079 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1080 if (mpt_callbacks[cb_idx] == NULL)
1081 break;
1082
1083 mpt_callbacks[cb_idx] = cb_func;
1084 return cb_idx;
1085}
1086
1087/**
1088 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1089 *
1090 * Return nothing.
1091 */
1092void
1093mpt3sas_base_initialize_callback_handler(void)
1094{
1095 u8 cb_idx;
1096
1097 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1098 mpt3sas_base_release_callback_handler(cb_idx);
1099}
1100
1101
1102/**
1103 * _base_build_zero_len_sge - build zero length sg entry
1104 * @ioc: per adapter object
1105 * @paddr: virtual address for SGE
1106 *
1107 * Create a zero length scatter gather entry to insure the IOCs hardware has
1108 * something to use if the target device goes brain dead and tries
1109 * to send data even when none is asked for.
1110 *
1111 * Return nothing.
1112 */
1113static void
1114_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1115{
1116 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1117 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1118 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1119 MPI2_SGE_FLAGS_SHIFT);
1120 ioc->base_add_sg_single(paddr, flags_length, -1);
1121}
1122
1123/**
1124 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1125 * @paddr: virtual address for SGE
1126 * @flags_length: SGE flags and data transfer length
1127 * @dma_addr: Physical address
1128 *
1129 * Return nothing.
1130 */
1131static void
1132_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1133{
1134 Mpi2SGESimple32_t *sgel = paddr;
1135
1136 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1137 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1138 sgel->FlagsLength = cpu_to_le32(flags_length);
1139 sgel->Address = cpu_to_le32(dma_addr);
1140}
1141
1142
1143/**
1144 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1145 * @paddr: virtual address for SGE
1146 * @flags_length: SGE flags and data transfer length
1147 * @dma_addr: Physical address
1148 *
1149 * Return nothing.
1150 */
1151static void
1152_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1153{
1154 Mpi2SGESimple64_t *sgel = paddr;
1155
1156 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1157 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1158 sgel->FlagsLength = cpu_to_le32(flags_length);
1159 sgel->Address = cpu_to_le64(dma_addr);
1160}
1161
1162/**
1163 * _base_get_chain_buffer_tracker - obtain chain tracker
1164 * @ioc: per adapter object
1165 * @smid: smid associated to an IO request
1166 *
1167 * Returns chain tracker(from ioc->free_chain_list)
1168 */
1169static struct chain_tracker *
1170_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1171{
1172 struct chain_tracker *chain_req;
1173 unsigned long flags;
1174
1175 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1176 if (list_empty(&ioc->free_chain_list)) {
1177 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1178 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1179 "chain buffers not available\n", ioc->name));
1180 return NULL;
1181 }
1182 chain_req = list_entry(ioc->free_chain_list.next,
1183 struct chain_tracker, tracker_list);
1184 list_del_init(&chain_req->tracker_list);
1185 list_add_tail(&chain_req->tracker_list,
1186 &ioc->scsi_lookup[smid - 1].chain_list);
1187 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1188 return chain_req;
1189}
1190
1191
1192/**
1193 * _base_build_sg - build generic sg
1194 * @ioc: per adapter object
1195 * @psge: virtual address for SGE
1196 * @data_out_dma: physical address for WRITES
1197 * @data_out_sz: data xfer size for WRITES
1198 * @data_in_dma: physical address for READS
1199 * @data_in_sz: data xfer size for READS
1200 *
1201 * Return nothing.
1202 */
1203static void
1204_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1205 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1206 size_t data_in_sz)
1207{
1208 u32 sgl_flags;
1209
1210 if (!data_out_sz && !data_in_sz) {
1211 _base_build_zero_len_sge(ioc, psge);
1212 return;
1213 }
1214
1215 if (data_out_sz && data_in_sz) {
1216 /* WRITE sgel first */
1217 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1218 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1219 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1220 ioc->base_add_sg_single(psge, sgl_flags |
1221 data_out_sz, data_out_dma);
1222
1223 /* incr sgel */
1224 psge += ioc->sge_size;
1225
1226 /* READ sgel last */
1227 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1228 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1229 MPI2_SGE_FLAGS_END_OF_LIST);
1230 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1231 ioc->base_add_sg_single(psge, sgl_flags |
1232 data_in_sz, data_in_dma);
1233 } else if (data_out_sz) /* WRITE */ {
1234 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1235 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1236 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1237 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1238 ioc->base_add_sg_single(psge, sgl_flags |
1239 data_out_sz, data_out_dma);
1240 } else if (data_in_sz) /* READ */ {
1241 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1242 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1243 MPI2_SGE_FLAGS_END_OF_LIST);
1244 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1245 ioc->base_add_sg_single(psge, sgl_flags |
1246 data_in_sz, data_in_dma);
1247 }
1248}
1249
1250/* IEEE format sgls */
1251
1252/**
1253 * _base_add_sg_single_ieee - add sg element for IEEE format
1254 * @paddr: virtual address for SGE
1255 * @flags: SGE flags
1256 * @chain_offset: number of 128 byte elements from start of segment
1257 * @length: data transfer length
1258 * @dma_addr: Physical address
1259 *
1260 * Return nothing.
1261 */
1262static void
1263_base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1264 dma_addr_t dma_addr)
1265{
1266 Mpi25IeeeSgeChain64_t *sgel = paddr;
1267
1268 sgel->Flags = flags;
1269 sgel->NextChainOffset = chain_offset;
1270 sgel->Length = cpu_to_le32(length);
1271 sgel->Address = cpu_to_le64(dma_addr);
1272}
1273
1274/**
1275 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1276 * @ioc: per adapter object
1277 * @paddr: virtual address for SGE
1278 *
1279 * Create a zero length scatter gather entry to insure the IOCs hardware has
1280 * something to use if the target device goes brain dead and tries
1281 * to send data even when none is asked for.
1282 *
1283 * Return nothing.
1284 */
1285static void
1286_base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1287{
1288 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1289 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1290 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
1291 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1292}
1293
1294/**
1295 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
1296 * @ioc: per adapter object
1297 * @scmd: scsi command
1298 * @smid: system request message index
1299 * Context: none.
1300 *
1301 * The main routine that builds scatter gather table from a given
1302 * scsi request sent via the .queuecommand main handler.
1303 *
1304 * Returns 0 success, anything else error
1305 */
1306static int
1307_base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
1308 struct scsi_cmnd *scmd, u16 smid)
1309{
1310 Mpi2SCSIIORequest_t *mpi_request;
1311 dma_addr_t chain_dma;
1312 struct scatterlist *sg_scmd;
1313 void *sg_local, *chain;
1314 u32 chain_offset;
1315 u32 chain_length;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301316 int sges_left;
1317 u32 sges_in_segment;
1318 u8 simple_sgl_flags;
1319 u8 simple_sgl_flags_last;
1320 u8 chain_sgl_flags;
1321 struct chain_tracker *chain_req;
1322
1323 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1324
1325 /* init scatter gather flags */
1326 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1327 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1328 simple_sgl_flags_last = simple_sgl_flags |
1329 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1330 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1331 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1332
1333 sg_scmd = scsi_sglist(scmd);
1334 sges_left = scsi_dma_map(scmd);
1335 if (!sges_left) {
1336 sdev_printk(KERN_ERR, scmd->device,
1337 "pci_map_sg failed: request for %d bytes!\n",
1338 scsi_bufflen(scmd));
1339 return -ENOMEM;
1340 }
1341
1342 sg_local = &mpi_request->SGL;
1343 sges_in_segment = (ioc->request_sz -
1344 offsetof(Mpi2SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
1345 if (sges_left <= sges_in_segment)
1346 goto fill_in_last_segment;
1347
1348 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
1349 (offsetof(Mpi2SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
1350
1351 /* fill in main message segment when there is a chain following */
1352 while (sges_in_segment > 1) {
1353 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1354 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1355 sg_scmd = sg_next(sg_scmd);
1356 sg_local += ioc->sge_size_ieee;
1357 sges_left--;
1358 sges_in_segment--;
1359 }
1360
Wei Yongjun25ef16d2012-12-12 02:26:51 +05301361 /* initializing the pointers */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301362 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1363 if (!chain_req)
1364 return -1;
1365 chain = chain_req->chain_buffer;
1366 chain_dma = chain_req->chain_buffer_dma;
1367 do {
1368 sges_in_segment = (sges_left <=
1369 ioc->max_sges_in_chain_message) ? sges_left :
1370 ioc->max_sges_in_chain_message;
1371 chain_offset = (sges_left == sges_in_segment) ?
1372 0 : sges_in_segment;
1373 chain_length = sges_in_segment * ioc->sge_size_ieee;
1374 if (chain_offset)
1375 chain_length += ioc->sge_size_ieee;
1376 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
1377 chain_offset, chain_length, chain_dma);
1378
1379 sg_local = chain;
1380 if (!chain_offset)
1381 goto fill_in_last_segment;
1382
1383 /* fill in chain segments */
1384 while (sges_in_segment) {
1385 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1386 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1387 sg_scmd = sg_next(sg_scmd);
1388 sg_local += ioc->sge_size_ieee;
1389 sges_left--;
1390 sges_in_segment--;
1391 }
1392
1393 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1394 if (!chain_req)
1395 return -1;
1396 chain = chain_req->chain_buffer;
1397 chain_dma = chain_req->chain_buffer_dma;
1398 } while (1);
1399
1400
1401 fill_in_last_segment:
1402
1403 /* fill the last segment */
1404 while (sges_left) {
1405 if (sges_left == 1)
1406 _base_add_sg_single_ieee(sg_local,
1407 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
1408 sg_dma_address(sg_scmd));
1409 else
1410 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
1411 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1412 sg_scmd = sg_next(sg_scmd);
1413 sg_local += ioc->sge_size_ieee;
1414 sges_left--;
1415 }
1416
1417 return 0;
1418}
1419
1420/**
1421 * _base_build_sg_ieee - build generic sg for IEEE format
1422 * @ioc: per adapter object
1423 * @psge: virtual address for SGE
1424 * @data_out_dma: physical address for WRITES
1425 * @data_out_sz: data xfer size for WRITES
1426 * @data_in_dma: physical address for READS
1427 * @data_in_sz: data xfer size for READS
1428 *
1429 * Return nothing.
1430 */
1431static void
1432_base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
1433 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1434 size_t data_in_sz)
1435{
1436 u8 sgl_flags;
1437
1438 if (!data_out_sz && !data_in_sz) {
1439 _base_build_zero_len_sge_ieee(ioc, psge);
1440 return;
1441 }
1442
1443 if (data_out_sz && data_in_sz) {
1444 /* WRITE sgel first */
1445 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1446 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1447 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1448 data_out_dma);
1449
1450 /* incr sgel */
1451 psge += ioc->sge_size_ieee;
1452
1453 /* READ sgel last */
1454 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
1455 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1456 data_in_dma);
1457 } else if (data_out_sz) /* WRITE */ {
1458 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1459 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1460 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1461 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
1462 data_out_dma);
1463 } else if (data_in_sz) /* READ */ {
1464 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1465 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
1466 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
1467 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
1468 data_in_dma);
1469 }
1470}
1471
1472#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1473
1474/**
1475 * _base_config_dma_addressing - set dma addressing
1476 * @ioc: per adapter object
1477 * @pdev: PCI device struct
1478 *
1479 * Returns 0 for success, non-zero for failure.
1480 */
1481static int
1482_base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
1483{
1484 struct sysinfo s;
1485 char *desc = NULL;
1486
1487 if (sizeof(dma_addr_t) > 4) {
1488 const uint64_t required_mask =
1489 dma_get_required_mask(&pdev->dev);
1490 if ((required_mask > DMA_BIT_MASK(32)) &&
1491 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
1492 !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
1493 ioc->base_add_sg_single = &_base_add_sg_single_64;
1494 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1495 desc = "64";
1496 goto out;
1497 }
1498 }
1499
1500 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1501 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
1502 ioc->base_add_sg_single = &_base_add_sg_single_32;
1503 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1504 desc = "32";
1505 } else
1506 return -ENODEV;
1507
1508 out:
1509 si_meminfo(&s);
1510 pr_info(MPT3SAS_FMT
1511 "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
1512 ioc->name, desc, convert_to_kb(s.totalram));
1513
1514 return 0;
1515}
1516
1517/**
1518 * _base_check_enable_msix - checks MSIX capabable.
1519 * @ioc: per adapter object
1520 *
1521 * Check to see if card is capable of MSIX, and set number
1522 * of available msix vectors
1523 */
1524static int
1525_base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1526{
1527 int base;
1528 u16 message_control;
1529
1530 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1531 if (!base) {
1532 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
1533 ioc->name));
1534 return -EINVAL;
1535 }
1536
1537 /* get msix vector count */
1538
1539 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1540 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1541 if (ioc->msix_vector_count > 8)
1542 ioc->msix_vector_count = 8;
1543 dinitprintk(ioc, pr_info(MPT3SAS_FMT
1544 "msix is supported, vector_count(%d)\n",
1545 ioc->name, ioc->msix_vector_count));
1546 return 0;
1547}
1548
1549/**
1550 * _base_free_irq - free irq
1551 * @ioc: per adapter object
1552 *
1553 * Freeing respective reply_queue from the list.
1554 */
1555static void
1556_base_free_irq(struct MPT3SAS_ADAPTER *ioc)
1557{
1558 struct adapter_reply_queue *reply_q, *next;
1559
1560 if (list_empty(&ioc->reply_queue_list))
1561 return;
1562
1563 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1564 list_del(&reply_q->list);
1565 synchronize_irq(reply_q->vector);
1566 free_irq(reply_q->vector, reply_q);
1567 kfree(reply_q);
1568 }
1569}
1570
1571/**
1572 * _base_request_irq - request irq
1573 * @ioc: per adapter object
1574 * @index: msix index into vector table
1575 * @vector: irq vector
1576 *
1577 * Inserting respective reply_queue into the list.
1578 */
1579static int
1580_base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index, u32 vector)
1581{
1582 struct adapter_reply_queue *reply_q;
1583 int r;
1584
1585 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1586 if (!reply_q) {
1587 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
1588 ioc->name, (int)sizeof(struct adapter_reply_queue));
1589 return -ENOMEM;
1590 }
1591 reply_q->ioc = ioc;
1592 reply_q->msix_index = index;
1593 reply_q->vector = vector;
1594 atomic_set(&reply_q->busy, 0);
1595 if (ioc->msix_enable)
1596 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1597 MPT3SAS_DRIVER_NAME, ioc->id, index);
1598 else
1599 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1600 MPT3SAS_DRIVER_NAME, ioc->id);
1601 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1602 reply_q);
1603 if (r) {
1604 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
1605 reply_q->name, vector);
1606 kfree(reply_q);
1607 return -EBUSY;
1608 }
1609
1610 INIT_LIST_HEAD(&reply_q->list);
1611 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1612 return 0;
1613}
1614
1615/**
1616 * _base_assign_reply_queues - assigning msix index for each cpu
1617 * @ioc: per adapter object
1618 *
1619 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1620 *
1621 * It would nice if we could call irq_set_affinity, however it is not
1622 * an exported symbol
1623 */
1624static void
1625_base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
1626{
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001627 unsigned int cpu, nr_cpus, nr_msix, index = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301628
1629 if (!_base_is_controller_msix_enabled(ioc))
1630 return;
1631
1632 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1633
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001634 nr_cpus = num_online_cpus();
1635 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1636 ioc->facts.MaxMSIxVectors);
1637 if (!nr_msix)
1638 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301639
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001640 cpu = cpumask_first(cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301641
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001642 do {
1643 unsigned int i, group = nr_cpus / nr_msix;
1644
1645 if (index < nr_cpus % nr_msix)
1646 group++;
1647
1648 for (i = 0 ; i < group ; i++) {
1649 ioc->cpu_msix_table[cpu] = index;
1650 cpu = cpumask_next(cpu, cpu_online_mask);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301651 }
Martin K. Petersen91b265b2014-01-03 19:16:56 -05001652
1653 index++;
1654
1655 } while (cpu < nr_cpus);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301656}
1657
1658/**
1659 * _base_disable_msix - disables msix
1660 * @ioc: per adapter object
1661 *
1662 */
1663static void
1664_base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
1665{
1666 if (!ioc->msix_enable)
1667 return;
1668 pci_disable_msix(ioc->pdev);
1669 ioc->msix_enable = 0;
1670}
1671
1672/**
1673 * _base_enable_msix - enables msix, failback to io_apic
1674 * @ioc: per adapter object
1675 *
1676 */
1677static int
1678_base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
1679{
1680 struct msix_entry *entries, *a;
1681 int r;
1682 int i;
1683 u8 try_msix = 0;
1684
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301685 if (msix_disable == -1 || msix_disable == 0)
1686 try_msix = 1;
1687
1688 if (!try_msix)
1689 goto try_ioapic;
1690
1691 if (_base_check_enable_msix(ioc) != 0)
1692 goto try_ioapic;
1693
1694 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
1695 ioc->msix_vector_count);
1696
Sreekanth Reddy9c500062013-08-14 18:23:20 +05301697 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
1698 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
1699 ioc->cpu_count, max_msix_vectors);
1700
1701 if (max_msix_vectors > 0) {
1702 ioc->reply_queue_count = min_t(int, max_msix_vectors,
1703 ioc->reply_queue_count);
1704 ioc->msix_vector_count = ioc->reply_queue_count;
1705 }
1706
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301707 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1708 GFP_KERNEL);
1709 if (!entries) {
1710 dfailprintk(ioc, pr_info(MPT3SAS_FMT
1711 "kcalloc failed @ at %s:%d/%s() !!!\n",
1712 ioc->name, __FILE__, __LINE__, __func__));
1713 goto try_ioapic;
1714 }
1715
1716 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1717 a->entry = i;
1718
Alexander Gordeev6bfa6902014-08-18 08:01:46 +02001719 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301720 if (r) {
1721 dfailprintk(ioc, pr_info(MPT3SAS_FMT
Alexander Gordeev6bfa6902014-08-18 08:01:46 +02001722 "pci_enable_msix_exact failed (r=%d) !!!\n",
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301723 ioc->name, r));
1724 kfree(entries);
1725 goto try_ioapic;
1726 }
1727
1728 ioc->msix_enable = 1;
1729 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1730 r = _base_request_irq(ioc, i, a->vector);
1731 if (r) {
1732 _base_free_irq(ioc);
1733 _base_disable_msix(ioc);
1734 kfree(entries);
1735 goto try_ioapic;
1736 }
1737 }
1738
1739 kfree(entries);
1740 return 0;
1741
1742/* failback to io_apic interrupt routing */
1743 try_ioapic:
1744
1745 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
1746
1747 return r;
1748}
1749
1750/**
1751 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
1752 * @ioc: per adapter object
1753 *
1754 * Returns 0 for success, non-zero for failure.
1755 */
1756int
1757mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
1758{
1759 struct pci_dev *pdev = ioc->pdev;
1760 u32 memap_sz;
1761 u32 pio_sz;
1762 int i, r = 0;
1763 u64 pio_chip = 0;
1764 u64 chip_phys = 0;
1765 struct adapter_reply_queue *reply_q;
1766
1767 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
1768 ioc->name, __func__));
1769
1770 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1771 if (pci_enable_device_mem(pdev)) {
1772 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
1773 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04001774 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301775 return -ENODEV;
1776 }
1777
1778
1779 if (pci_request_selected_regions(pdev, ioc->bars,
1780 MPT3SAS_DRIVER_NAME)) {
1781 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
1782 ioc->name);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04001783 ioc->bars = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301784 r = -ENODEV;
1785 goto out_fail;
1786 }
1787
1788/* AER (Advanced Error Reporting) hooks */
1789 pci_enable_pcie_error_reporting(pdev);
1790
1791 pci_set_master(pdev);
1792
1793
1794 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1795 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
1796 ioc->name, pci_name(pdev));
1797 r = -ENODEV;
1798 goto out_fail;
1799 }
1800
1801 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1802 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
1803 if (pio_sz)
1804 continue;
1805 pio_chip = (u64)pci_resource_start(pdev, i);
1806 pio_sz = pci_resource_len(pdev, i);
1807 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1808 if (memap_sz)
1809 continue;
1810 ioc->chip_phys = pci_resource_start(pdev, i);
1811 chip_phys = (u64)ioc->chip_phys;
1812 memap_sz = pci_resource_len(pdev, i);
1813 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1814 if (ioc->chip == NULL) {
1815 pr_err(MPT3SAS_FMT "unable to map adapter memory!\n",
1816 ioc->name);
1817 r = -EINVAL;
1818 goto out_fail;
1819 }
1820 }
1821 }
1822
1823 _base_mask_interrupts(ioc);
1824 r = _base_enable_msix(ioc);
1825 if (r)
1826 goto out_fail;
1827
1828 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1829 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
1830 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1831 "IO-APIC enabled"), reply_q->vector);
1832
1833 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1834 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1835 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
1836 ioc->name, (unsigned long long)pio_chip, pio_sz);
1837
1838 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1839 pci_save_state(pdev);
1840 return 0;
1841
1842 out_fail:
1843 if (ioc->chip_phys)
1844 iounmap(ioc->chip);
1845 ioc->chip_phys = 0;
1846 pci_release_selected_regions(ioc->pdev, ioc->bars);
1847 pci_disable_pcie_error_reporting(pdev);
1848 pci_disable_device(pdev);
1849 return r;
1850}
1851
1852/**
1853 * mpt3sas_base_get_msg_frame - obtain request mf pointer
1854 * @ioc: per adapter object
1855 * @smid: system request message index(smid zero is invalid)
1856 *
1857 * Returns virt pointer to message frame.
1858 */
1859void *
1860mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1861{
1862 return (void *)(ioc->request + (smid * ioc->request_sz));
1863}
1864
1865/**
1866 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
1867 * @ioc: per adapter object
1868 * @smid: system request message index
1869 *
1870 * Returns virt pointer to sense buffer.
1871 */
1872void *
1873mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1874{
1875 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1876}
1877
1878/**
1879 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
1880 * @ioc: per adapter object
1881 * @smid: system request message index
1882 *
1883 * Returns phys pointer to the low 32bit address of the sense buffer.
1884 */
1885__le32
1886mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1887{
1888 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
1889 SCSI_SENSE_BUFFERSIZE));
1890}
1891
1892/**
1893 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
1894 * @ioc: per adapter object
1895 * @phys_addr: lower 32 physical addr of the reply
1896 *
1897 * Converts 32bit lower physical addr into a virt address.
1898 */
1899void *
1900mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
1901{
1902 if (!phys_addr)
1903 return NULL;
1904 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1905}
1906
1907/**
1908 * mpt3sas_base_get_smid - obtain a free smid from internal queue
1909 * @ioc: per adapter object
1910 * @cb_idx: callback index
1911 *
1912 * Returns smid (zero is invalid)
1913 */
1914u16
1915mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
1916{
1917 unsigned long flags;
1918 struct request_tracker *request;
1919 u16 smid;
1920
1921 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1922 if (list_empty(&ioc->internal_free_list)) {
1923 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1924 pr_err(MPT3SAS_FMT "%s: smid not available\n",
1925 ioc->name, __func__);
1926 return 0;
1927 }
1928
1929 request = list_entry(ioc->internal_free_list.next,
1930 struct request_tracker, tracker_list);
1931 request->cb_idx = cb_idx;
1932 smid = request->smid;
1933 list_del(&request->tracker_list);
1934 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1935 return smid;
1936}
1937
1938/**
1939 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1940 * @ioc: per adapter object
1941 * @cb_idx: callback index
1942 * @scmd: pointer to scsi command object
1943 *
1944 * Returns smid (zero is invalid)
1945 */
1946u16
1947mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
1948 struct scsi_cmnd *scmd)
1949{
1950 unsigned long flags;
1951 struct scsiio_tracker *request;
1952 u16 smid;
1953
1954 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1955 if (list_empty(&ioc->free_list)) {
1956 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1957 pr_err(MPT3SAS_FMT "%s: smid not available\n",
1958 ioc->name, __func__);
1959 return 0;
1960 }
1961
1962 request = list_entry(ioc->free_list.next,
1963 struct scsiio_tracker, tracker_list);
1964 request->scmd = scmd;
1965 request->cb_idx = cb_idx;
1966 smid = request->smid;
1967 list_del(&request->tracker_list);
1968 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1969 return smid;
1970}
1971
1972/**
1973 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1974 * @ioc: per adapter object
1975 * @cb_idx: callback index
1976 *
1977 * Returns smid (zero is invalid)
1978 */
1979u16
1980mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
1981{
1982 unsigned long flags;
1983 struct request_tracker *request;
1984 u16 smid;
1985
1986 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1987 if (list_empty(&ioc->hpr_free_list)) {
1988 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1989 return 0;
1990 }
1991
1992 request = list_entry(ioc->hpr_free_list.next,
1993 struct request_tracker, tracker_list);
1994 request->cb_idx = cb_idx;
1995 smid = request->smid;
1996 list_del(&request->tracker_list);
1997 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1998 return smid;
1999}
2000
2001/**
2002 * mpt3sas_base_free_smid - put smid back on free_list
2003 * @ioc: per adapter object
2004 * @smid: system request message index
2005 *
2006 * Return nothing.
2007 */
2008void
2009mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2010{
2011 unsigned long flags;
2012 int i;
2013 struct chain_tracker *chain_req, *next;
2014
2015 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2016 if (smid < ioc->hi_priority_smid) {
2017 /* scsiio queue */
2018 i = smid - 1;
2019 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
2020 list_for_each_entry_safe(chain_req, next,
2021 &ioc->scsi_lookup[i].chain_list, tracker_list) {
2022 list_del_init(&chain_req->tracker_list);
2023 list_add(&chain_req->tracker_list,
2024 &ioc->free_chain_list);
2025 }
2026 }
2027 ioc->scsi_lookup[i].cb_idx = 0xFF;
2028 ioc->scsi_lookup[i].scmd = NULL;
2029 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2030 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2031
2032 /*
2033 * See _wait_for_commands_to_complete() call with regards
2034 * to this code.
2035 */
2036 if (ioc->shost_recovery && ioc->pending_io_count) {
2037 if (ioc->pending_io_count == 1)
2038 wake_up(&ioc->reset_wq);
2039 ioc->pending_io_count--;
2040 }
2041 return;
2042 } else if (smid < ioc->internal_smid) {
2043 /* hi-priority */
2044 i = smid - ioc->hi_priority_smid;
2045 ioc->hpr_lookup[i].cb_idx = 0xFF;
2046 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2047 } else if (smid <= ioc->hba_queue_depth) {
2048 /* internal queue */
2049 i = smid - ioc->internal_smid;
2050 ioc->internal_lookup[i].cb_idx = 0xFF;
2051 list_add(&ioc->internal_lookup[i].tracker_list,
2052 &ioc->internal_free_list);
2053 }
2054 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2055}
2056
2057/**
2058 * _base_writeq - 64 bit write to MMIO
2059 * @ioc: per adapter object
2060 * @b: data payload
2061 * @addr: address in MMIO space
2062 * @writeq_lock: spin lock
2063 *
2064 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2065 * care of 32 bit environment where its not quarenteed to send the entire word
2066 * in one transfer.
2067 */
2068#if defined(writeq) && defined(CONFIG_64BIT)
2069static inline void
2070_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2071{
2072 writeq(cpu_to_le64(b), addr);
2073}
2074#else
2075static inline void
2076_base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2077{
2078 unsigned long flags;
2079 __u64 data_out = cpu_to_le64(b);
2080
2081 spin_lock_irqsave(writeq_lock, flags);
2082 writel((u32)(data_out), addr);
2083 writel((u32)(data_out >> 32), (addr + 4));
2084 spin_unlock_irqrestore(writeq_lock, flags);
2085}
2086#endif
2087
2088static inline u8
2089_base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2090{
2091 return ioc->cpu_msix_table[raw_smp_processor_id()];
2092}
2093
2094/**
2095 * mpt3sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
2096 * @ioc: per adapter object
2097 * @smid: system request message index
2098 * @handle: device handle
2099 *
2100 * Return nothing.
2101 */
2102void
2103mpt3sas_base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
2104{
2105 Mpi2RequestDescriptorUnion_t descriptor;
2106 u64 *request = (u64 *)&descriptor;
2107
2108
2109 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2110 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2111 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2112 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2113 descriptor.SCSIIO.LMID = 0;
2114 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2115 &ioc->scsi_lookup_lock);
2116}
2117
2118/**
2119 * mpt3sas_base_put_smid_fast_path - send fast path request to firmware
2120 * @ioc: per adapter object
2121 * @smid: system request message index
2122 * @handle: device handle
2123 *
2124 * Return nothing.
2125 */
2126void
2127mpt3sas_base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
2128 u16 handle)
2129{
2130 Mpi2RequestDescriptorUnion_t descriptor;
2131 u64 *request = (u64 *)&descriptor;
2132
2133 descriptor.SCSIIO.RequestFlags =
2134 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
2135 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2136 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2137 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2138 descriptor.SCSIIO.LMID = 0;
2139 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2140 &ioc->scsi_lookup_lock);
2141}
2142
2143/**
2144 * mpt3sas_base_put_smid_hi_priority - send Task Managment request to firmware
2145 * @ioc: per adapter object
2146 * @smid: system request message index
2147 *
2148 * Return nothing.
2149 */
2150void
2151mpt3sas_base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2152{
2153 Mpi2RequestDescriptorUnion_t descriptor;
2154 u64 *request = (u64 *)&descriptor;
2155
2156 descriptor.HighPriority.RequestFlags =
2157 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
2158 descriptor.HighPriority.MSIxIndex = 0;
2159 descriptor.HighPriority.SMID = cpu_to_le16(smid);
2160 descriptor.HighPriority.LMID = 0;
2161 descriptor.HighPriority.Reserved1 = 0;
2162 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2163 &ioc->scsi_lookup_lock);
2164}
2165
2166/**
2167 * mpt3sas_base_put_smid_default - Default, primarily used for config pages
2168 * @ioc: per adapter object
2169 * @smid: system request message index
2170 *
2171 * Return nothing.
2172 */
2173void
2174mpt3sas_base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2175{
2176 Mpi2RequestDescriptorUnion_t descriptor;
2177 u64 *request = (u64 *)&descriptor;
2178
2179 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2180 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
2181 descriptor.Default.SMID = cpu_to_le16(smid);
2182 descriptor.Default.LMID = 0;
2183 descriptor.Default.DescriptorTypeDependent = 0;
2184 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2185 &ioc->scsi_lookup_lock);
2186}
2187
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302188/**
2189 * _base_display_intel_branding - Display branding string
2190 * @ioc: per adapter object
2191 *
2192 * Return nothing.
2193 */
2194static void
2195_base_display_intel_branding(struct MPT3SAS_ADAPTER *ioc)
2196{
2197 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
2198 return;
2199
2200 switch (ioc->pdev->device) {
2201 case MPI25_MFGPAGE_DEVID_SAS3008:
2202 switch (ioc->pdev->subsystem_device) {
2203 case MPT3SAS_INTEL_RMS3JC080_SSDID:
2204 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2205 MPT3SAS_INTEL_RMS3JC080_BRANDING);
2206 break;
2207
2208 case MPT3SAS_INTEL_RS3GC008_SSDID:
2209 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2210 MPT3SAS_INTEL_RS3GC008_BRANDING);
2211 break;
2212 case MPT3SAS_INTEL_RS3FC044_SSDID:
2213 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2214 MPT3SAS_INTEL_RS3FC044_BRANDING);
2215 break;
2216 case MPT3SAS_INTEL_RS3UC080_SSDID:
2217 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2218 MPT3SAS_INTEL_RS3UC080_BRANDING);
2219 break;
2220 default:
2221 pr_info(MPT3SAS_FMT
2222 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2223 ioc->name, ioc->pdev->subsystem_device);
2224 break;
2225 }
2226 break;
2227 default:
2228 pr_info(MPT3SAS_FMT
2229 "Intel(R) Controller: Subsystem ID: 0x%X\n",
2230 ioc->name, ioc->pdev->subsystem_device);
2231 break;
2232 }
2233}
2234
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302235
2236
2237/**
2238 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2239 * @ioc: per adapter object
2240 *
2241 * Return nothing.
2242 */
2243static void
2244_base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
2245{
2246 int i = 0;
2247 char desc[16];
2248 u32 iounit_pg1_flags;
2249 u32 bios_version;
2250
2251 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
2252 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2253 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
2254 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2255 ioc->name, desc,
2256 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2257 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2258 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2259 ioc->facts.FWVersion.Word & 0x000000FF,
2260 ioc->pdev->revision,
2261 (bios_version & 0xFF000000) >> 24,
2262 (bios_version & 0x00FF0000) >> 16,
2263 (bios_version & 0x0000FF00) >> 8,
2264 bios_version & 0x000000FF);
2265
Sreekanth Reddy1117b312014-09-12 15:35:30 +05302266 _base_display_intel_branding(ioc);
2267
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302268 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
2269
2270 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2271 pr_info("Initiator");
2272 i++;
2273 }
2274
2275 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2276 pr_info("%sTarget", i ? "," : "");
2277 i++;
2278 }
2279
2280 i = 0;
2281 pr_info("), ");
2282 pr_info("Capabilities=(");
2283
2284 if (ioc->facts.IOCCapabilities &
2285 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2286 pr_info("Raid");
2287 i++;
2288 }
2289
2290 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2291 pr_info("%sTLR", i ? "," : "");
2292 i++;
2293 }
2294
2295 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2296 pr_info("%sMulticast", i ? "," : "");
2297 i++;
2298 }
2299
2300 if (ioc->facts.IOCCapabilities &
2301 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2302 pr_info("%sBIDI Target", i ? "," : "");
2303 i++;
2304 }
2305
2306 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2307 pr_info("%sEEDP", i ? "," : "");
2308 i++;
2309 }
2310
2311 if (ioc->facts.IOCCapabilities &
2312 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2313 pr_info("%sSnapshot Buffer", i ? "," : "");
2314 i++;
2315 }
2316
2317 if (ioc->facts.IOCCapabilities &
2318 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2319 pr_info("%sDiag Trace Buffer", i ? "," : "");
2320 i++;
2321 }
2322
2323 if (ioc->facts.IOCCapabilities &
2324 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2325 pr_info("%sDiag Extended Buffer", i ? "," : "");
2326 i++;
2327 }
2328
2329 if (ioc->facts.IOCCapabilities &
2330 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2331 pr_info("%sTask Set Full", i ? "," : "");
2332 i++;
2333 }
2334
2335 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2336 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2337 pr_info("%sNCQ", i ? "," : "");
2338 i++;
2339 }
2340
2341 pr_info(")\n");
2342}
2343
2344/**
2345 * mpt3sas_base_update_missing_delay - change the missing delay timers
2346 * @ioc: per adapter object
2347 * @device_missing_delay: amount of time till device is reported missing
2348 * @io_missing_delay: interval IO is returned when there is a missing device
2349 *
2350 * Return nothing.
2351 *
2352 * Passed on the command line, this function will modify the device missing
2353 * delay, as well as the io missing delay. This should be called at driver
2354 * load time.
2355 */
2356void
2357mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
2358 u16 device_missing_delay, u8 io_missing_delay)
2359{
2360 u16 dmd, dmd_new, dmd_orignal;
2361 u8 io_missing_delay_original;
2362 u16 sz;
2363 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2364 Mpi2ConfigReply_t mpi_reply;
2365 u8 num_phys = 0;
2366 u16 ioc_status;
2367
2368 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
2369 if (!num_phys)
2370 return;
2371
2372 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2373 sizeof(Mpi2SasIOUnit1PhyData_t));
2374 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2375 if (!sas_iounit_pg1) {
2376 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2377 ioc->name, __FILE__, __LINE__, __func__);
2378 goto out;
2379 }
2380 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2381 sas_iounit_pg1, sz))) {
2382 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2383 ioc->name, __FILE__, __LINE__, __func__);
2384 goto out;
2385 }
2386 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2387 MPI2_IOCSTATUS_MASK;
2388 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2389 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
2390 ioc->name, __FILE__, __LINE__, __func__);
2391 goto out;
2392 }
2393
2394 /* device missing delay */
2395 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2396 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2397 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2398 else
2399 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2400 dmd_orignal = dmd;
2401 if (device_missing_delay > 0x7F) {
2402 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2403 device_missing_delay;
2404 dmd = dmd / 16;
2405 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2406 } else
2407 dmd = device_missing_delay;
2408 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2409
2410 /* io missing delay */
2411 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2412 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2413
2414 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2415 sz)) {
2416 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2417 dmd_new = (dmd &
2418 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2419 else
2420 dmd_new =
2421 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2422 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
2423 ioc->name, dmd_orignal, dmd_new);
2424 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
2425 ioc->name, io_missing_delay_original,
2426 io_missing_delay);
2427 ioc->device_missing_delay = dmd_new;
2428 ioc->io_missing_delay = io_missing_delay;
2429 }
2430
2431out:
2432 kfree(sas_iounit_pg1);
2433}
2434/**
2435 * _base_static_config_pages - static start of day config pages
2436 * @ioc: per adapter object
2437 *
2438 * Return nothing.
2439 */
2440static void
2441_base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
2442{
2443 Mpi2ConfigReply_t mpi_reply;
2444 u32 iounit_pg1_flags;
2445
2446 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
2447 if (ioc->ir_firmware)
2448 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2449 &ioc->manu_pg10);
2450
2451 /*
2452 * Ensure correct T10 PI operation if vendor left EEDPTagMode
2453 * flag unset in NVDATA.
2454 */
2455 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
2456 if (ioc->manu_pg11.EEDPTagMode == 0) {
2457 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
2458 ioc->name);
2459 ioc->manu_pg11.EEDPTagMode &= ~0x3;
2460 ioc->manu_pg11.EEDPTagMode |= 0x1;
2461 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
2462 &ioc->manu_pg11);
2463 }
2464
2465 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2466 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2467 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2468 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2469 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2470 _base_display_ioc_capabilities(ioc);
2471
2472 /*
2473 * Enable task_set_full handling in iounit_pg1 when the
2474 * facts capabilities indicate that its supported.
2475 */
2476 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2477 if ((ioc->facts.IOCCapabilities &
2478 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2479 iounit_pg1_flags &=
2480 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2481 else
2482 iounit_pg1_flags |=
2483 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2484 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
2485 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2486}
2487
2488/**
2489 * _base_release_memory_pools - release memory
2490 * @ioc: per adapter object
2491 *
2492 * Free memory allocated from _base_allocate_memory_pools.
2493 *
2494 * Return nothing.
2495 */
2496static void
2497_base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
2498{
2499 int i;
2500
2501 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2502 __func__));
2503
2504 if (ioc->request) {
2505 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2506 ioc->request, ioc->request_dma);
2507 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2508 "request_pool(0x%p): free\n",
2509 ioc->name, ioc->request));
2510 ioc->request = NULL;
2511 }
2512
2513 if (ioc->sense) {
2514 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2515 if (ioc->sense_dma_pool)
2516 pci_pool_destroy(ioc->sense_dma_pool);
2517 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2518 "sense_pool(0x%p): free\n",
2519 ioc->name, ioc->sense));
2520 ioc->sense = NULL;
2521 }
2522
2523 if (ioc->reply) {
2524 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2525 if (ioc->reply_dma_pool)
2526 pci_pool_destroy(ioc->reply_dma_pool);
2527 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2528 "reply_pool(0x%p): free\n",
2529 ioc->name, ioc->reply));
2530 ioc->reply = NULL;
2531 }
2532
2533 if (ioc->reply_free) {
2534 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2535 ioc->reply_free_dma);
2536 if (ioc->reply_free_dma_pool)
2537 pci_pool_destroy(ioc->reply_free_dma_pool);
2538 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2539 "reply_free_pool(0x%p): free\n",
2540 ioc->name, ioc->reply_free));
2541 ioc->reply_free = NULL;
2542 }
2543
2544 if (ioc->reply_post_free) {
2545 pci_pool_free(ioc->reply_post_free_dma_pool,
2546 ioc->reply_post_free, ioc->reply_post_free_dma);
2547 if (ioc->reply_post_free_dma_pool)
2548 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2549 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2550 "reply_post_free_pool(0x%p): free\n", ioc->name,
2551 ioc->reply_post_free));
2552 ioc->reply_post_free = NULL;
2553 }
2554
2555 if (ioc->config_page) {
2556 dexitprintk(ioc, pr_info(MPT3SAS_FMT
2557 "config_page(0x%p): free\n", ioc->name,
2558 ioc->config_page));
2559 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2560 ioc->config_page, ioc->config_page_dma);
2561 }
2562
2563 if (ioc->scsi_lookup) {
2564 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2565 ioc->scsi_lookup = NULL;
2566 }
2567 kfree(ioc->hpr_lookup);
2568 kfree(ioc->internal_lookup);
2569 if (ioc->chain_lookup) {
2570 for (i = 0; i < ioc->chain_depth; i++) {
2571 if (ioc->chain_lookup[i].chain_buffer)
2572 pci_pool_free(ioc->chain_dma_pool,
2573 ioc->chain_lookup[i].chain_buffer,
2574 ioc->chain_lookup[i].chain_buffer_dma);
2575 }
2576 if (ioc->chain_dma_pool)
2577 pci_pool_destroy(ioc->chain_dma_pool);
2578 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2579 ioc->chain_lookup = NULL;
2580 }
2581}
2582
2583/**
2584 * _base_allocate_memory_pools - allocate start of day memory pools
2585 * @ioc: per adapter object
2586 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2587 *
2588 * Returns 0 success, anything else error
2589 */
2590static int
2591_base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
2592{
2593 struct mpt3sas_facts *facts;
2594 u16 max_sge_elements;
2595 u16 chains_needed_per_io;
2596 u32 sz, total_sz, reply_post_free_sz;
2597 u32 retry_sz;
2598 u16 max_request_credit;
2599 unsigned short sg_tablesize;
2600 u16 sge_size;
2601 int i;
2602
2603 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
2604 __func__));
2605
2606
2607 retry_sz = 0;
2608 facts = &ioc->facts;
2609
2610 /* command line tunables for max sgl entries */
2611 if (max_sgl_entries != -1)
2612 sg_tablesize = max_sgl_entries;
2613 else
2614 sg_tablesize = MPT3SAS_SG_DEPTH;
2615
2616 if (sg_tablesize < MPT3SAS_MIN_PHYS_SEGMENTS)
2617 sg_tablesize = MPT3SAS_MIN_PHYS_SEGMENTS;
2618 else if (sg_tablesize > MPT3SAS_MAX_PHYS_SEGMENTS)
2619 sg_tablesize = MPT3SAS_MAX_PHYS_SEGMENTS;
2620 ioc->shost->sg_tablesize = sg_tablesize;
2621
2622 ioc->hi_priority_depth = facts->HighPriorityCredit;
2623 ioc->internal_depth = ioc->hi_priority_depth + (5);
2624 /* command line tunables for max controller queue depth */
2625 if (max_queue_depth != -1 && max_queue_depth != 0) {
2626 max_request_credit = min_t(u16, max_queue_depth +
2627 ioc->hi_priority_depth + ioc->internal_depth,
2628 facts->RequestCredit);
2629 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2630 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2631 } else
2632 max_request_credit = min_t(u16, facts->RequestCredit,
2633 MAX_HBA_QUEUE_DEPTH);
2634
2635 ioc->hba_queue_depth = max_request_credit;
2636
2637 /* request frame size */
2638 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2639
2640 /* reply frame size */
2641 ioc->reply_sz = facts->ReplyFrameSize * 4;
2642
2643 /* calculate the max scatter element size */
2644 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
2645
2646 retry_allocation:
2647 total_sz = 0;
2648 /* calculate number of sg elements left over in the 1st frame */
2649 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2650 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
2651 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
2652
2653 /* now do the same for a chain buffer */
2654 max_sge_elements = ioc->request_sz - sge_size;
2655 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
2656
2657 /*
2658 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2659 */
2660 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2661 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2662 + 1;
2663 if (chains_needed_per_io > facts->MaxChainDepth) {
2664 chains_needed_per_io = facts->MaxChainDepth;
2665 ioc->shost->sg_tablesize = min_t(u16,
2666 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2667 * chains_needed_per_io), ioc->shost->sg_tablesize);
2668 }
2669 ioc->chains_needed_per_io = chains_needed_per_io;
2670
2671 /* reply free queue sizing - taking into account for 64 FW events */
2672 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2673
2674 /* calculate reply descriptor post queue depth */
2675 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
2676 ioc->reply_free_queue_depth + 1 ;
2677 /* align the reply post queue on the next 16 count boundary */
2678 if (ioc->reply_post_queue_depth % 16)
2679 ioc->reply_post_queue_depth += 16 -
2680 (ioc->reply_post_queue_depth % 16);
2681
2682
2683 if (ioc->reply_post_queue_depth >
2684 facts->MaxReplyDescriptorPostQueueDepth) {
2685 ioc->reply_post_queue_depth =
2686 facts->MaxReplyDescriptorPostQueueDepth -
2687 (facts->MaxReplyDescriptorPostQueueDepth % 16);
2688 ioc->hba_queue_depth =
2689 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
2690 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
2691 }
2692
2693 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
2694 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2695 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2696 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2697 ioc->chains_needed_per_io));
2698
2699 ioc->scsiio_depth = ioc->hba_queue_depth -
2700 ioc->hi_priority_depth - ioc->internal_depth;
2701
2702 /* set the scsi host can_queue depth
2703 * with some internal commands that could be outstanding
2704 */
2705 ioc->shost->can_queue = ioc->scsiio_depth;
2706 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2707 "scsi host: can_queue depth (%d)\n",
2708 ioc->name, ioc->shost->can_queue));
2709
2710
2711 /* contiguous pool for request and chains, 16 byte align, one extra "
2712 * "frame for smid=0
2713 */
2714 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2715 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
2716
2717 /* hi-priority queue */
2718 sz += (ioc->hi_priority_depth * ioc->request_sz);
2719
2720 /* internal queue */
2721 sz += (ioc->internal_depth * ioc->request_sz);
2722
2723 ioc->request_dma_sz = sz;
2724 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2725 if (!ioc->request) {
2726 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
2727 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2728 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
2729 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2730 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
2731 goto out;
2732 retry_sz += 64;
2733 ioc->hba_queue_depth = max_request_credit - retry_sz;
2734 goto retry_allocation;
2735 }
2736
2737 if (retry_sz)
2738 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
2739 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2740 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
2741 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2742
2743 /* hi-priority queue */
2744 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
2745 ioc->request_sz);
2746 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
2747 ioc->request_sz);
2748
2749 /* internal queue */
2750 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2751 ioc->request_sz);
2752 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2753 ioc->request_sz);
2754
2755 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2756 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
2757 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2758 (ioc->hba_queue_depth * ioc->request_sz)/1024));
2759
2760 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
2761 ioc->name, (unsigned long long) ioc->request_dma));
2762 total_sz += sz;
2763
2764 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
2765 ioc->scsi_lookup_pages = get_order(sz);
2766 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
2767 GFP_KERNEL, ioc->scsi_lookup_pages);
2768 if (!ioc->scsi_lookup) {
2769 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
2770 ioc->name, (int)sz);
2771 goto out;
2772 }
2773
2774 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
2775 ioc->name, ioc->request, ioc->scsiio_depth));
2776
2777 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2778 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2779 ioc->chain_pages = get_order(sz);
2780 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2781 GFP_KERNEL, ioc->chain_pages);
2782 if (!ioc->chain_lookup) {
2783 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
2784 ioc->name);
2785 goto out;
2786 }
2787 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2788 ioc->request_sz, 16, 0);
2789 if (!ioc->chain_dma_pool) {
2790 pr_err(MPT3SAS_FMT "chain_dma_pool: pci_pool_create failed\n",
2791 ioc->name);
2792 goto out;
2793 }
2794 for (i = 0; i < ioc->chain_depth; i++) {
2795 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2796 ioc->chain_dma_pool , GFP_KERNEL,
2797 &ioc->chain_lookup[i].chain_buffer_dma);
2798 if (!ioc->chain_lookup[i].chain_buffer) {
2799 ioc->chain_depth = i;
2800 goto chain_done;
2801 }
2802 total_sz += ioc->request_sz;
2803 }
2804 chain_done:
2805 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2806 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
2807 ioc->name, ioc->chain_depth, ioc->request_sz,
2808 ((ioc->chain_depth * ioc->request_sz))/1024));
2809
2810 /* initialize hi-priority queue smid's */
2811 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2812 sizeof(struct request_tracker), GFP_KERNEL);
2813 if (!ioc->hpr_lookup) {
2814 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
2815 ioc->name);
2816 goto out;
2817 }
2818 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2819 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2820 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
2821 ioc->name, ioc->hi_priority,
2822 ioc->hi_priority_depth, ioc->hi_priority_smid));
2823
2824 /* initialize internal queue smid's */
2825 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2826 sizeof(struct request_tracker), GFP_KERNEL);
2827 if (!ioc->internal_lookup) {
2828 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
2829 ioc->name);
2830 goto out;
2831 }
2832 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2833 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2834 "internal(0x%p): depth(%d), start smid(%d)\n",
2835 ioc->name, ioc->internal,
2836 ioc->internal_depth, ioc->internal_smid));
2837
2838 /* sense buffers, 4 byte align */
2839 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
2840 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2841 0);
2842 if (!ioc->sense_dma_pool) {
2843 pr_err(MPT3SAS_FMT "sense pool: pci_pool_create failed\n",
2844 ioc->name);
2845 goto out;
2846 }
2847 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2848 &ioc->sense_dma);
2849 if (!ioc->sense) {
2850 pr_err(MPT3SAS_FMT "sense pool: pci_pool_alloc failed\n",
2851 ioc->name);
2852 goto out;
2853 }
2854 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2855 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
2856 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
2857 SCSI_SENSE_BUFFERSIZE, sz/1024));
2858 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
2859 ioc->name, (unsigned long long)ioc->sense_dma));
2860 total_sz += sz;
2861
2862 /* reply pool, 4 byte align */
2863 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2864 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2865 0);
2866 if (!ioc->reply_dma_pool) {
2867 pr_err(MPT3SAS_FMT "reply pool: pci_pool_create failed\n",
2868 ioc->name);
2869 goto out;
2870 }
2871 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2872 &ioc->reply_dma);
2873 if (!ioc->reply) {
2874 pr_err(MPT3SAS_FMT "reply pool: pci_pool_alloc failed\n",
2875 ioc->name);
2876 goto out;
2877 }
2878 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2879 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
2880 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2881 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
2882 ioc->name, ioc->reply,
2883 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2884 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
2885 ioc->name, (unsigned long long)ioc->reply_dma));
2886 total_sz += sz;
2887
2888 /* reply free queue, 16 byte align */
2889 sz = ioc->reply_free_queue_depth * 4;
2890 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2891 ioc->pdev, sz, 16, 0);
2892 if (!ioc->reply_free_dma_pool) {
2893 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_create failed\n",
2894 ioc->name);
2895 goto out;
2896 }
2897 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2898 &ioc->reply_free_dma);
2899 if (!ioc->reply_free) {
2900 pr_err(MPT3SAS_FMT "reply_free pool: pci_pool_alloc failed\n",
2901 ioc->name);
2902 goto out;
2903 }
2904 memset(ioc->reply_free, 0, sz);
2905 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
2906 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2907 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2908 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2909 "reply_free_dma (0x%llx)\n",
2910 ioc->name, (unsigned long long)ioc->reply_free_dma));
2911 total_sz += sz;
2912
2913 /* reply post queue, 16 byte align */
2914 reply_post_free_sz = ioc->reply_post_queue_depth *
2915 sizeof(Mpi2DefaultReplyDescriptor_t);
2916 if (_base_is_controller_msix_enabled(ioc))
2917 sz = reply_post_free_sz * ioc->reply_queue_count;
2918 else
2919 sz = reply_post_free_sz;
2920 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2921 ioc->pdev, sz, 16, 0);
2922 if (!ioc->reply_post_free_dma_pool) {
2923 pr_err(MPT3SAS_FMT
2924 "reply_post_free pool: pci_pool_create failed\n",
2925 ioc->name);
2926 goto out;
2927 }
2928 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2929 GFP_KERNEL, &ioc->reply_post_free_dma);
2930 if (!ioc->reply_post_free) {
2931 pr_err(MPT3SAS_FMT
2932 "reply_post_free pool: pci_pool_alloc failed\n",
2933 ioc->name);
2934 goto out;
2935 }
2936 memset(ioc->reply_post_free, 0, sz);
2937 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply post free pool" \
2938 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2939 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2940 sz/1024));
2941 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2942 "reply_post_free_dma = (0x%llx)\n",
2943 ioc->name, (unsigned long long)
2944 ioc->reply_post_free_dma));
2945 total_sz += sz;
2946
2947 ioc->config_page_sz = 512;
2948 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2949 ioc->config_page_sz, &ioc->config_page_dma);
2950 if (!ioc->config_page) {
2951 pr_err(MPT3SAS_FMT
2952 "config page: pci_pool_alloc failed\n",
2953 ioc->name);
2954 goto out;
2955 }
2956 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2957 "config page(0x%p): size(%d)\n",
2958 ioc->name, ioc->config_page, ioc->config_page_sz));
2959 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
2960 ioc->name, (unsigned long long)ioc->config_page_dma));
2961 total_sz += ioc->config_page_sz;
2962
2963 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
2964 ioc->name, total_sz/1024);
2965 pr_info(MPT3SAS_FMT
2966 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
2967 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2968 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
2969 ioc->name, ioc->shost->sg_tablesize);
2970 return 0;
2971
2972 out:
2973 return -ENOMEM;
2974}
2975
2976/**
2977 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
2978 * @ioc: Pointer to MPT_ADAPTER structure
2979 * @cooked: Request raw or cooked IOC state
2980 *
2981 * Returns all IOC Doorbell register bits if cooked==0, else just the
2982 * Doorbell bits in MPI_IOC_STATE_MASK.
2983 */
2984u32
2985mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
2986{
2987 u32 s, sc;
2988
2989 s = readl(&ioc->chip->Doorbell);
2990 sc = s & MPI2_IOC_STATE_MASK;
2991 return cooked ? sc : s;
2992}
2993
2994/**
2995 * _base_wait_on_iocstate - waiting on a particular ioc state
2996 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2997 * @timeout: timeout in second
2998 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2999 *
3000 * Returns 0 for success, non-zero for failure.
3001 */
3002static int
3003_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
3004 int sleep_flag)
3005{
3006 u32 count, cntdn;
3007 u32 current_state;
3008
3009 count = 0;
3010 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3011 do {
3012 current_state = mpt3sas_base_get_iocstate(ioc, 1);
3013 if (current_state == ioc_state)
3014 return 0;
3015 if (count && current_state == MPI2_IOC_STATE_FAULT)
3016 break;
3017 if (sleep_flag == CAN_SLEEP)
3018 usleep_range(1000, 1500);
3019 else
3020 udelay(500);
3021 count++;
3022 } while (--cntdn);
3023
3024 return current_state;
3025}
3026
3027/**
3028 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
3029 * a write to the doorbell)
3030 * @ioc: per adapter object
3031 * @timeout: timeout in second
3032 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3033 *
3034 * Returns 0 for success, non-zero for failure.
3035 *
3036 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
3037 */
3038static int
3039_base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout,
3040 int sleep_flag)
3041{
3042 u32 cntdn, count;
3043 u32 int_status;
3044
3045 count = 0;
3046 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3047 do {
3048 int_status = readl(&ioc->chip->HostInterruptStatus);
3049 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3050 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3051 "%s: successful count(%d), timeout(%d)\n",
3052 ioc->name, __func__, count, timeout));
3053 return 0;
3054 }
3055 if (sleep_flag == CAN_SLEEP)
3056 usleep_range(1000, 1500);
3057 else
3058 udelay(500);
3059 count++;
3060 } while (--cntdn);
3061
3062 pr_err(MPT3SAS_FMT
3063 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3064 ioc->name, __func__, count, int_status);
3065 return -EFAULT;
3066}
3067
3068/**
3069 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
3070 * @ioc: per adapter object
3071 * @timeout: timeout in second
3072 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3073 *
3074 * Returns 0 for success, non-zero for failure.
3075 *
3076 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
3077 * doorbell.
3078 */
3079static int
3080_base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout,
3081 int sleep_flag)
3082{
3083 u32 cntdn, count;
3084 u32 int_status;
3085 u32 doorbell;
3086
3087 count = 0;
3088 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3089 do {
3090 int_status = readl(&ioc->chip->HostInterruptStatus);
3091 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
3092 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3093 "%s: successful count(%d), timeout(%d)\n",
3094 ioc->name, __func__, count, timeout));
3095 return 0;
3096 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
3097 doorbell = readl(&ioc->chip->Doorbell);
3098 if ((doorbell & MPI2_IOC_STATE_MASK) ==
3099 MPI2_IOC_STATE_FAULT) {
3100 mpt3sas_base_fault_info(ioc , doorbell);
3101 return -EFAULT;
3102 }
3103 } else if (int_status == 0xFFFFFFFF)
3104 goto out;
3105
3106 if (sleep_flag == CAN_SLEEP)
3107 usleep_range(1000, 1500);
3108 else
3109 udelay(500);
3110 count++;
3111 } while (--cntdn);
3112
3113 out:
3114 pr_err(MPT3SAS_FMT
3115 "%s: failed due to timeout count(%d), int_status(%x)!\n",
3116 ioc->name, __func__, count, int_status);
3117 return -EFAULT;
3118}
3119
3120/**
3121 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
3122 * @ioc: per adapter object
3123 * @timeout: timeout in second
3124 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3125 *
3126 * Returns 0 for success, non-zero for failure.
3127 *
3128 */
3129static int
3130_base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout,
3131 int sleep_flag)
3132{
3133 u32 cntdn, count;
3134 u32 doorbell_reg;
3135
3136 count = 0;
3137 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
3138 do {
3139 doorbell_reg = readl(&ioc->chip->Doorbell);
3140 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
3141 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3142 "%s: successful count(%d), timeout(%d)\n",
3143 ioc->name, __func__, count, timeout));
3144 return 0;
3145 }
3146 if (sleep_flag == CAN_SLEEP)
3147 usleep_range(1000, 1500);
3148 else
3149 udelay(500);
3150 count++;
3151 } while (--cntdn);
3152
3153 pr_err(MPT3SAS_FMT
3154 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
3155 ioc->name, __func__, count, doorbell_reg);
3156 return -EFAULT;
3157}
3158
3159/**
3160 * _base_send_ioc_reset - send doorbell reset
3161 * @ioc: per adapter object
3162 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
3163 * @timeout: timeout in second
3164 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3165 *
3166 * Returns 0 for success, non-zero for failure.
3167 */
3168static int
3169_base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout,
3170 int sleep_flag)
3171{
3172 u32 ioc_state;
3173 int r = 0;
3174
3175 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
3176 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
3177 ioc->name, __func__);
3178 return -EFAULT;
3179 }
3180
3181 if (!(ioc->facts.IOCCapabilities &
3182 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
3183 return -EFAULT;
3184
3185 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
3186
3187 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
3188 &ioc->chip->Doorbell);
3189 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
3190 r = -EFAULT;
3191 goto out;
3192 }
3193 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
3194 timeout, sleep_flag);
3195 if (ioc_state) {
3196 pr_err(MPT3SAS_FMT
3197 "%s: failed going to ready state (ioc_state=0x%x)\n",
3198 ioc->name, __func__, ioc_state);
3199 r = -EFAULT;
3200 goto out;
3201 }
3202 out:
3203 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
3204 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3205 return r;
3206}
3207
3208/**
3209 * _base_handshake_req_reply_wait - send request thru doorbell interface
3210 * @ioc: per adapter object
3211 * @request_bytes: request length
3212 * @request: pointer having request payload
3213 * @reply_bytes: reply length
3214 * @reply: pointer to reply payload
3215 * @timeout: timeout in second
3216 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3217 *
3218 * Returns 0 for success, non-zero for failure.
3219 */
3220static int
3221_base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
3222 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3223{
3224 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3225 int i;
3226 u8 failed;
3227 u16 dummy;
3228 __le32 *mfp;
3229
3230 /* make sure doorbell is not in use */
3231 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3232 pr_err(MPT3SAS_FMT
3233 "doorbell is in use (line=%d)\n",
3234 ioc->name, __LINE__);
3235 return -EFAULT;
3236 }
3237
3238 /* clear pending doorbell interrupts from previous state changes */
3239 if (readl(&ioc->chip->HostInterruptStatus) &
3240 MPI2_HIS_IOC2SYS_DB_STATUS)
3241 writel(0, &ioc->chip->HostInterruptStatus);
3242
3243 /* send message to ioc */
3244 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3245 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3246 &ioc->chip->Doorbell);
3247
3248 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
3249 pr_err(MPT3SAS_FMT
3250 "doorbell handshake int failed (line=%d)\n",
3251 ioc->name, __LINE__);
3252 return -EFAULT;
3253 }
3254 writel(0, &ioc->chip->HostInterruptStatus);
3255
3256 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3257 pr_err(MPT3SAS_FMT
3258 "doorbell handshake ack failed (line=%d)\n",
3259 ioc->name, __LINE__);
3260 return -EFAULT;
3261 }
3262
3263 /* send message 32-bits at a time */
3264 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3265 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3266 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3267 failed = 1;
3268 }
3269
3270 if (failed) {
3271 pr_err(MPT3SAS_FMT
3272 "doorbell handshake sending request failed (line=%d)\n",
3273 ioc->name, __LINE__);
3274 return -EFAULT;
3275 }
3276
3277 /* now wait for the reply */
3278 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3279 pr_err(MPT3SAS_FMT
3280 "doorbell handshake int failed (line=%d)\n",
3281 ioc->name, __LINE__);
3282 return -EFAULT;
3283 }
3284
3285 /* read the first two 16-bits, it gives the total length of the reply */
3286 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3287 & MPI2_DOORBELL_DATA_MASK);
3288 writel(0, &ioc->chip->HostInterruptStatus);
3289 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3290 pr_err(MPT3SAS_FMT
3291 "doorbell handshake int failed (line=%d)\n",
3292 ioc->name, __LINE__);
3293 return -EFAULT;
3294 }
3295 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3296 & MPI2_DOORBELL_DATA_MASK);
3297 writel(0, &ioc->chip->HostInterruptStatus);
3298
3299 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3300 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3301 pr_err(MPT3SAS_FMT
3302 "doorbell handshake int failed (line=%d)\n",
3303 ioc->name, __LINE__);
3304 return -EFAULT;
3305 }
3306 if (i >= reply_bytes/2) /* overflow case */
3307 dummy = readl(&ioc->chip->Doorbell);
3308 else
3309 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3310 & MPI2_DOORBELL_DATA_MASK);
3311 writel(0, &ioc->chip->HostInterruptStatus);
3312 }
3313
3314 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3315 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3316 dhsprintk(ioc, pr_info(MPT3SAS_FMT
3317 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
3318 }
3319 writel(0, &ioc->chip->HostInterruptStatus);
3320
3321 if (ioc->logging_level & MPT_DEBUG_INIT) {
3322 mfp = (__le32 *)reply;
3323 pr_info("\toffset:data\n");
3324 for (i = 0; i < reply_bytes/4; i++)
3325 pr_info("\t[0x%02x]:%08x\n", i*4,
3326 le32_to_cpu(mfp[i]));
3327 }
3328 return 0;
3329}
3330
3331/**
3332 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
3333 * @ioc: per adapter object
3334 * @mpi_reply: the reply payload from FW
3335 * @mpi_request: the request payload sent to FW
3336 *
3337 * The SAS IO Unit Control Request message allows the host to perform low-level
3338 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3339 * to obtain the IOC assigned device handles for a device if it has other
3340 * identifying information about the device, in addition allows the host to
3341 * remove IOC resources associated with the device.
3342 *
3343 * Returns 0 for success, non-zero for failure.
3344 */
3345int
3346mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
3347 Mpi2SasIoUnitControlReply_t *mpi_reply,
3348 Mpi2SasIoUnitControlRequest_t *mpi_request)
3349{
3350 u16 smid;
3351 u32 ioc_state;
3352 unsigned long timeleft;
3353 u8 issue_reset;
3354 int rc;
3355 void *request;
3356 u16 wait_state_count;
3357
3358 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3359 __func__));
3360
3361 mutex_lock(&ioc->base_cmds.mutex);
3362
3363 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3364 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3365 ioc->name, __func__);
3366 rc = -EAGAIN;
3367 goto out;
3368 }
3369
3370 wait_state_count = 0;
3371 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3372 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3373 if (wait_state_count++ == 10) {
3374 pr_err(MPT3SAS_FMT
3375 "%s: failed due to ioc not operational\n",
3376 ioc->name, __func__);
3377 rc = -EFAULT;
3378 goto out;
3379 }
3380 ssleep(1);
3381 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3382 pr_info(MPT3SAS_FMT
3383 "%s: waiting for operational state(count=%d)\n",
3384 ioc->name, __func__, wait_state_count);
3385 }
3386
3387 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3388 if (!smid) {
3389 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3390 ioc->name, __func__);
3391 rc = -EAGAIN;
3392 goto out;
3393 }
3394
3395 rc = 0;
3396 ioc->base_cmds.status = MPT3_CMD_PENDING;
3397 request = mpt3sas_base_get_msg_frame(ioc, smid);
3398 ioc->base_cmds.smid = smid;
3399 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3400 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3401 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3402 ioc->ioc_link_reset_in_progress = 1;
3403 init_completion(&ioc->base_cmds.done);
3404 mpt3sas_base_put_smid_default(ioc, smid);
3405 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3406 msecs_to_jiffies(10000));
3407 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3408 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3409 ioc->ioc_link_reset_in_progress)
3410 ioc->ioc_link_reset_in_progress = 0;
3411 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3412 pr_err(MPT3SAS_FMT "%s: timeout\n",
3413 ioc->name, __func__);
3414 _debug_dump_mf(mpi_request,
3415 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3416 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
3417 issue_reset = 1;
3418 goto issue_host_reset;
3419 }
3420 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
3421 memcpy(mpi_reply, ioc->base_cmds.reply,
3422 sizeof(Mpi2SasIoUnitControlReply_t));
3423 else
3424 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3425 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3426 goto out;
3427
3428 issue_host_reset:
3429 if (issue_reset)
3430 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3431 FORCE_BIG_HAMMER);
3432 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3433 rc = -EFAULT;
3434 out:
3435 mutex_unlock(&ioc->base_cmds.mutex);
3436 return rc;
3437}
3438
3439/**
3440 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
3441 * @ioc: per adapter object
3442 * @mpi_reply: the reply payload from FW
3443 * @mpi_request: the request payload sent to FW
3444 *
3445 * The SCSI Enclosure Processor request message causes the IOC to
3446 * communicate with SES devices to control LED status signals.
3447 *
3448 * Returns 0 for success, non-zero for failure.
3449 */
3450int
3451mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
3452 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3453{
3454 u16 smid;
3455 u32 ioc_state;
3456 unsigned long timeleft;
3457 u8 issue_reset;
3458 int rc;
3459 void *request;
3460 u16 wait_state_count;
3461
3462 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3463 __func__));
3464
3465 mutex_lock(&ioc->base_cmds.mutex);
3466
3467 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
3468 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
3469 ioc->name, __func__);
3470 rc = -EAGAIN;
3471 goto out;
3472 }
3473
3474 wait_state_count = 0;
3475 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3476 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3477 if (wait_state_count++ == 10) {
3478 pr_err(MPT3SAS_FMT
3479 "%s: failed due to ioc not operational\n",
3480 ioc->name, __func__);
3481 rc = -EFAULT;
3482 goto out;
3483 }
3484 ssleep(1);
3485 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3486 pr_info(MPT3SAS_FMT
3487 "%s: waiting for operational state(count=%d)\n",
3488 ioc->name,
3489 __func__, wait_state_count);
3490 }
3491
3492 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3493 if (!smid) {
3494 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3495 ioc->name, __func__);
3496 rc = -EAGAIN;
3497 goto out;
3498 }
3499
3500 rc = 0;
3501 ioc->base_cmds.status = MPT3_CMD_PENDING;
3502 request = mpt3sas_base_get_msg_frame(ioc, smid);
3503 ioc->base_cmds.smid = smid;
3504 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
3505 init_completion(&ioc->base_cmds.done);
3506 mpt3sas_base_put_smid_default(ioc, smid);
3507 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3508 msecs_to_jiffies(10000));
3509 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
3510 pr_err(MPT3SAS_FMT "%s: timeout\n",
3511 ioc->name, __func__);
3512 _debug_dump_mf(mpi_request,
3513 sizeof(Mpi2SepRequest_t)/4);
3514 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
3515 issue_reset = 1;
3516 goto issue_host_reset;
3517 }
3518 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
3519 memcpy(mpi_reply, ioc->base_cmds.reply,
3520 sizeof(Mpi2SepReply_t));
3521 else
3522 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3523 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3524 goto out;
3525
3526 issue_host_reset:
3527 if (issue_reset)
3528 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3529 FORCE_BIG_HAMMER);
3530 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
3531 rc = -EFAULT;
3532 out:
3533 mutex_unlock(&ioc->base_cmds.mutex);
3534 return rc;
3535}
3536
3537/**
3538 * _base_get_port_facts - obtain port facts reply and save in ioc
3539 * @ioc: per adapter object
3540 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3541 *
3542 * Returns 0 for success, non-zero for failure.
3543 */
3544static int
3545_base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port, int sleep_flag)
3546{
3547 Mpi2PortFactsRequest_t mpi_request;
3548 Mpi2PortFactsReply_t mpi_reply;
3549 struct mpt3sas_port_facts *pfacts;
3550 int mpi_reply_sz, mpi_request_sz, r;
3551
3552 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3553 __func__));
3554
3555 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3556 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3557 memset(&mpi_request, 0, mpi_request_sz);
3558 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3559 mpi_request.PortNumber = port;
3560 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3561 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3562
3563 if (r != 0) {
3564 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3565 ioc->name, __func__, r);
3566 return r;
3567 }
3568
3569 pfacts = &ioc->pfacts[port];
3570 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
3571 pfacts->PortNumber = mpi_reply.PortNumber;
3572 pfacts->VP_ID = mpi_reply.VP_ID;
3573 pfacts->VF_ID = mpi_reply.VF_ID;
3574 pfacts->MaxPostedCmdBuffers =
3575 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3576
3577 return 0;
3578}
3579
3580/**
3581 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3582 * @ioc: per adapter object
3583 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3584 *
3585 * Returns 0 for success, non-zero for failure.
3586 */
3587static int
3588_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3589{
3590 Mpi2IOCFactsRequest_t mpi_request;
3591 Mpi2IOCFactsReply_t mpi_reply;
3592 struct mpt3sas_facts *facts;
3593 int mpi_reply_sz, mpi_request_sz, r;
3594
3595 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3596 __func__));
3597
3598 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3599 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3600 memset(&mpi_request, 0, mpi_request_sz);
3601 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3602 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3603 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3604
3605 if (r != 0) {
3606 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3607 ioc->name, __func__, r);
3608 return r;
3609 }
3610
3611 facts = &ioc->facts;
3612 memset(facts, 0, sizeof(struct mpt3sas_facts));
3613 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3614 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3615 facts->VP_ID = mpi_reply.VP_ID;
3616 facts->VF_ID = mpi_reply.VF_ID;
3617 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3618 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3619 facts->WhoInit = mpi_reply.WhoInit;
3620 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3621 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
3622 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3623 facts->MaxReplyDescriptorPostQueueDepth =
3624 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3625 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3626 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3627 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3628 ioc->ir_firmware = 1;
3629 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3630 facts->IOCRequestFrameSize =
3631 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3632 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3633 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3634 ioc->shost->max_id = -1;
3635 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3636 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3637 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3638 facts->HighPriorityCredit =
3639 le16_to_cpu(mpi_reply.HighPriorityCredit);
3640 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3641 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3642
3643 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3644 "hba queue depth(%d), max chains per io(%d)\n",
3645 ioc->name, facts->RequestCredit,
3646 facts->MaxChainDepth));
3647 dinitprintk(ioc, pr_info(MPT3SAS_FMT
3648 "request frame size(%d), reply frame size(%d)\n", ioc->name,
3649 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3650 return 0;
3651}
3652
3653/**
3654 * _base_send_ioc_init - send ioc_init to firmware
3655 * @ioc: per adapter object
3656 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3657 *
3658 * Returns 0 for success, non-zero for failure.
3659 */
3660static int
3661_base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3662{
3663 Mpi2IOCInitRequest_t mpi_request;
3664 Mpi2IOCInitReply_t mpi_reply;
3665 int r;
3666 struct timeval current_time;
3667 u16 ioc_status;
3668
3669 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3670 __func__));
3671
3672 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3673 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3674 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
3675 mpi_request.VF_ID = 0; /* TODO */
3676 mpi_request.VP_ID = 0;
3677 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3678 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3679
3680 if (_base_is_controller_msix_enabled(ioc))
3681 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
3682 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3683 mpi_request.ReplyDescriptorPostQueueDepth =
3684 cpu_to_le16(ioc->reply_post_queue_depth);
3685 mpi_request.ReplyFreeQueueDepth =
3686 cpu_to_le16(ioc->reply_free_queue_depth);
3687
3688 mpi_request.SenseBufferAddressHigh =
3689 cpu_to_le32((u64)ioc->sense_dma >> 32);
3690 mpi_request.SystemReplyAddressHigh =
3691 cpu_to_le32((u64)ioc->reply_dma >> 32);
3692 mpi_request.SystemRequestFrameBaseAddress =
3693 cpu_to_le64((u64)ioc->request_dma);
3694 mpi_request.ReplyFreeQueueAddress =
3695 cpu_to_le64((u64)ioc->reply_free_dma);
3696 mpi_request.ReplyDescriptorPostQueueAddress =
3697 cpu_to_le64((u64)ioc->reply_post_free_dma);
3698
3699
3700 /* This time stamp specifies number of milliseconds
3701 * since epoch ~ midnight January 1, 1970.
3702 */
3703 do_gettimeofday(&current_time);
3704 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3705 (current_time.tv_usec / 1000));
3706
3707 if (ioc->logging_level & MPT_DEBUG_INIT) {
3708 __le32 *mfp;
3709 int i;
3710
3711 mfp = (__le32 *)&mpi_request;
3712 pr_info("\toffset:data\n");
3713 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
3714 pr_info("\t[0x%02x]:%08x\n", i*4,
3715 le32_to_cpu(mfp[i]));
3716 }
3717
3718 r = _base_handshake_req_reply_wait(ioc,
3719 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3720 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3721 sleep_flag);
3722
3723 if (r != 0) {
3724 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
3725 ioc->name, __func__, r);
3726 return r;
3727 }
3728
3729 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3730 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
3731 mpi_reply.IOCLogInfo) {
3732 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
3733 r = -EIO;
3734 }
3735
3736 return 0;
3737}
3738
3739/**
3740 * mpt3sas_port_enable_done - command completion routine for port enable
3741 * @ioc: per adapter object
3742 * @smid: system request message index
3743 * @msix_index: MSIX table index supplied by the OS
3744 * @reply: reply message frame(lower 32bit addr)
3745 *
3746 * Return 1 meaning mf should be freed from _base_interrupt
3747 * 0 means the mf is freed from this function.
3748 */
3749u8
3750mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3751 u32 reply)
3752{
3753 MPI2DefaultReply_t *mpi_reply;
3754 u16 ioc_status;
3755
3756 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
3757 return 1;
3758
3759 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
3760 if (!mpi_reply)
3761 return 1;
3762
3763 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
3764 return 1;
3765
3766 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
3767 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
3768 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
3769 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
3770 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3771 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3772 ioc->port_enable_failed = 1;
3773
3774 if (ioc->is_driver_loading) {
3775 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3776 mpt3sas_port_enable_complete(ioc);
3777 return 1;
3778 } else {
3779 ioc->start_scan_failed = ioc_status;
3780 ioc->start_scan = 0;
3781 return 1;
3782 }
3783 }
3784 complete(&ioc->port_enable_cmds.done);
3785 return 1;
3786}
3787
3788/**
3789 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3790 * @ioc: per adapter object
3791 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3792 *
3793 * Returns 0 for success, non-zero for failure.
3794 */
3795static int
3796_base_send_port_enable(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3797{
3798 Mpi2PortEnableRequest_t *mpi_request;
3799 Mpi2PortEnableReply_t *mpi_reply;
3800 unsigned long timeleft;
3801 int r = 0;
3802 u16 smid;
3803 u16 ioc_status;
3804
3805 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
3806
3807 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
3808 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
3809 ioc->name, __func__);
3810 return -EAGAIN;
3811 }
3812
3813 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3814 if (!smid) {
3815 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3816 ioc->name, __func__);
3817 return -EAGAIN;
3818 }
3819
3820 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
3821 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3822 ioc->port_enable_cmds.smid = smid;
3823 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3824 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3825
3826 init_completion(&ioc->port_enable_cmds.done);
3827 mpt3sas_base_put_smid_default(ioc, smid);
3828 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
3829 300*HZ);
3830 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
3831 pr_err(MPT3SAS_FMT "%s: timeout\n",
3832 ioc->name, __func__);
3833 _debug_dump_mf(mpi_request,
3834 sizeof(Mpi2PortEnableRequest_t)/4);
3835 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
3836 r = -EFAULT;
3837 else
3838 r = -ETIME;
3839 goto out;
3840 }
3841
3842 mpi_reply = ioc->port_enable_cmds.reply;
3843 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3844 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3845 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
3846 ioc->name, __func__, ioc_status);
3847 r = -EFAULT;
3848 goto out;
3849 }
3850
3851 out:
3852 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
3853 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3854 "SUCCESS" : "FAILED"));
3855 return r;
3856}
3857
3858/**
3859 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
3860 * @ioc: per adapter object
3861 *
3862 * Returns 0 for success, non-zero for failure.
3863 */
3864int
3865mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
3866{
3867 Mpi2PortEnableRequest_t *mpi_request;
3868 u16 smid;
3869
3870 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
3871
3872 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
3873 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
3874 ioc->name, __func__);
3875 return -EAGAIN;
3876 }
3877
3878 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3879 if (!smid) {
3880 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3881 ioc->name, __func__);
3882 return -EAGAIN;
3883 }
3884
3885 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
3886 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3887 ioc->port_enable_cmds.smid = smid;
3888 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3889 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3890
3891 mpt3sas_base_put_smid_default(ioc, smid);
3892 return 0;
3893}
3894
3895/**
3896 * _base_determine_wait_on_discovery - desposition
3897 * @ioc: per adapter object
3898 *
3899 * Decide whether to wait on discovery to complete. Used to either
3900 * locate boot device, or report volumes ahead of physical devices.
3901 *
3902 * Returns 1 for wait, 0 for don't wait
3903 */
3904static int
3905_base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
3906{
3907 /* We wait for discovery to complete if IR firmware is loaded.
3908 * The sas topology events arrive before PD events, so we need time to
3909 * turn on the bit in ioc->pd_handles to indicate PD
3910 * Also, it maybe required to report Volumes ahead of physical
3911 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3912 */
3913 if (ioc->ir_firmware)
3914 return 1;
3915
3916 /* if no Bios, then we don't need to wait */
3917 if (!ioc->bios_pg3.BiosVersion)
3918 return 0;
3919
3920 /* Bios is present, then we drop down here.
3921 *
3922 * If there any entries in the Bios Page 2, then we wait
3923 * for discovery to complete.
3924 */
3925
3926 /* Current Boot Device */
3927 if ((ioc->bios_pg2.CurrentBootDeviceForm &
3928 MPI2_BIOSPAGE2_FORM_MASK) ==
3929 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3930 /* Request Boot Device */
3931 (ioc->bios_pg2.ReqBootDeviceForm &
3932 MPI2_BIOSPAGE2_FORM_MASK) ==
3933 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3934 /* Alternate Request Boot Device */
3935 (ioc->bios_pg2.ReqAltBootDeviceForm &
3936 MPI2_BIOSPAGE2_FORM_MASK) ==
3937 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3938 return 0;
3939
3940 return 1;
3941}
3942
3943/**
3944 * _base_unmask_events - turn on notification for this event
3945 * @ioc: per adapter object
3946 * @event: firmware event
3947 *
3948 * The mask is stored in ioc->event_masks.
3949 */
3950static void
3951_base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
3952{
3953 u32 desired_event;
3954
3955 if (event >= 128)
3956 return;
3957
3958 desired_event = (1 << (event % 32));
3959
3960 if (event < 32)
3961 ioc->event_masks[0] &= ~desired_event;
3962 else if (event < 64)
3963 ioc->event_masks[1] &= ~desired_event;
3964 else if (event < 96)
3965 ioc->event_masks[2] &= ~desired_event;
3966 else if (event < 128)
3967 ioc->event_masks[3] &= ~desired_event;
3968}
3969
3970/**
3971 * _base_event_notification - send event notification
3972 * @ioc: per adapter object
3973 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3974 *
3975 * Returns 0 for success, non-zero for failure.
3976 */
3977static int
3978_base_event_notification(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
3979{
3980 Mpi2EventNotificationRequest_t *mpi_request;
3981 unsigned long timeleft;
3982 u16 smid;
3983 int r = 0;
3984 int i;
3985
3986 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3987 __func__));
3988
3989 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
3990 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
3991 ioc->name, __func__);
3992 return -EAGAIN;
3993 }
3994
3995 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
3996 if (!smid) {
3997 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3998 ioc->name, __func__);
3999 return -EAGAIN;
4000 }
4001 ioc->base_cmds.status = MPT3_CMD_PENDING;
4002 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4003 ioc->base_cmds.smid = smid;
4004 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
4005 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4006 mpi_request->VF_ID = 0; /* TODO */
4007 mpi_request->VP_ID = 0;
4008 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4009 mpi_request->EventMasks[i] =
4010 cpu_to_le32(ioc->event_masks[i]);
4011 init_completion(&ioc->base_cmds.done);
4012 mpt3sas_base_put_smid_default(ioc, smid);
4013 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
4014 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4015 pr_err(MPT3SAS_FMT "%s: timeout\n",
4016 ioc->name, __func__);
4017 _debug_dump_mf(mpi_request,
4018 sizeof(Mpi2EventNotificationRequest_t)/4);
4019 if (ioc->base_cmds.status & MPT3_CMD_RESET)
4020 r = -EFAULT;
4021 else
4022 r = -ETIME;
4023 } else
4024 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
4025 ioc->name, __func__));
4026 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4027 return r;
4028}
4029
4030/**
4031 * mpt3sas_base_validate_event_type - validating event types
4032 * @ioc: per adapter object
4033 * @event: firmware event
4034 *
4035 * This will turn on firmware event notification when application
4036 * ask for that event. We don't mask events that are already enabled.
4037 */
4038void
4039mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
4040{
4041 int i, j;
4042 u32 event_mask, desired_event;
4043 u8 send_update_to_fw;
4044
4045 for (i = 0, send_update_to_fw = 0; i <
4046 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
4047 event_mask = ~event_type[i];
4048 desired_event = 1;
4049 for (j = 0; j < 32; j++) {
4050 if (!(event_mask & desired_event) &&
4051 (ioc->event_masks[i] & desired_event)) {
4052 ioc->event_masks[i] &= ~desired_event;
4053 send_update_to_fw = 1;
4054 }
4055 desired_event = (desired_event << 1);
4056 }
4057 }
4058
4059 if (!send_update_to_fw)
4060 return;
4061
4062 mutex_lock(&ioc->base_cmds.mutex);
4063 _base_event_notification(ioc, CAN_SLEEP);
4064 mutex_unlock(&ioc->base_cmds.mutex);
4065}
4066
4067/**
4068 * _base_diag_reset - the "big hammer" start of day reset
4069 * @ioc: per adapter object
4070 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4071 *
4072 * Returns 0 for success, non-zero for failure.
4073 */
4074static int
4075_base_diag_reset(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4076{
4077 u32 host_diagnostic;
4078 u32 ioc_state;
4079 u32 count;
4080 u32 hcb_size;
4081
4082 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
4083
4084 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
4085 ioc->name));
4086
4087 count = 0;
4088 do {
4089 /* Write magic sequence to WriteSequence register
4090 * Loop until in diagnostic mode
4091 */
4092 drsprintk(ioc, pr_info(MPT3SAS_FMT
4093 "write magic sequence\n", ioc->name));
4094 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4095 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
4096 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
4097 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
4098 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
4099 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
4100 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
4101
4102 /* wait 100 msec */
4103 if (sleep_flag == CAN_SLEEP)
4104 msleep(100);
4105 else
4106 mdelay(100);
4107
4108 if (count++ > 20)
4109 goto out;
4110
4111 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4112 drsprintk(ioc, pr_info(MPT3SAS_FMT
4113 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
4114 ioc->name, count, host_diagnostic));
4115
4116 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
4117
4118 hcb_size = readl(&ioc->chip->HCBSize);
4119
4120 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
4121 ioc->name));
4122 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
4123 &ioc->chip->HostDiagnostic);
4124
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304125 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
4126 if (sleep_flag == CAN_SLEEP)
4127 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
4128 else
4129 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304130
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304131 /* Approximately 300 second max wait */
4132 for (count = 0; count < (300000000 /
4133 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304134
4135 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
4136
4137 if (host_diagnostic == 0xFFFFFFFF)
4138 goto out;
4139 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
4140 break;
4141
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304142 /* Wait to pass the second read delay window */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304143 if (sleep_flag == CAN_SLEEP)
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304144 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4145 / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304146 else
Sreekanth Reddyb453ff82013-06-29 03:51:19 +05304147 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
4148 / 1000);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304149 }
4150
4151 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
4152
4153 drsprintk(ioc, pr_info(MPT3SAS_FMT
4154 "restart the adapter assuming the HCB Address points to good F/W\n",
4155 ioc->name));
4156 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
4157 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
4158 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
4159
4160 drsprintk(ioc, pr_info(MPT3SAS_FMT
4161 "re-enable the HCDW\n", ioc->name));
4162 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
4163 &ioc->chip->HCBSize);
4164 }
4165
4166 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
4167 ioc->name));
4168 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
4169 &ioc->chip->HostDiagnostic);
4170
4171 drsprintk(ioc, pr_info(MPT3SAS_FMT
4172 "disable writes to the diagnostic register\n", ioc->name));
4173 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
4174
4175 drsprintk(ioc, pr_info(MPT3SAS_FMT
4176 "Wait for FW to go to the READY state\n", ioc->name));
4177 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
4178 sleep_flag);
4179 if (ioc_state) {
4180 pr_err(MPT3SAS_FMT
4181 "%s: failed going to ready state (ioc_state=0x%x)\n",
4182 ioc->name, __func__, ioc_state);
4183 goto out;
4184 }
4185
4186 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
4187 return 0;
4188
4189 out:
4190 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
4191 return -EFAULT;
4192}
4193
4194/**
4195 * _base_make_ioc_ready - put controller in READY state
4196 * @ioc: per adapter object
4197 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4198 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4199 *
4200 * Returns 0 for success, non-zero for failure.
4201 */
4202static int
4203_base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4204 enum reset_type type)
4205{
4206 u32 ioc_state;
4207 int rc;
4208 int count;
4209
4210 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4211 __func__));
4212
4213 if (ioc->pci_error_recovery)
4214 return 0;
4215
4216 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4217 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4218 ioc->name, __func__, ioc_state));
4219
4220 /* if in RESET state, it should move to READY state shortly */
4221 count = 0;
4222 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
4223 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
4224 MPI2_IOC_STATE_READY) {
4225 if (count++ == 10) {
4226 pr_err(MPT3SAS_FMT
4227 "%s: failed going to ready state (ioc_state=0x%x)\n",
4228 ioc->name, __func__, ioc_state);
4229 return -EFAULT;
4230 }
4231 if (sleep_flag == CAN_SLEEP)
4232 ssleep(1);
4233 else
4234 mdelay(1000);
4235 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4236 }
4237 }
4238
4239 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4240 return 0;
4241
4242 if (ioc_state & MPI2_DOORBELL_USED) {
4243 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4244 "unexpected doorbell active!\n",
4245 ioc->name));
4246 goto issue_diag_reset;
4247 }
4248
4249 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4250 mpt3sas_base_fault_info(ioc, ioc_state &
4251 MPI2_DOORBELL_DATA_MASK);
4252 goto issue_diag_reset;
4253 }
4254
4255 if (type == FORCE_BIG_HAMMER)
4256 goto issue_diag_reset;
4257
4258 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4259 if (!(_base_send_ioc_reset(ioc,
4260 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4261 return 0;
4262 }
4263
4264 issue_diag_reset:
4265 rc = _base_diag_reset(ioc, CAN_SLEEP);
4266 return rc;
4267}
4268
4269/**
4270 * _base_make_ioc_operational - put controller in OPERATIONAL state
4271 * @ioc: per adapter object
4272 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4273 *
4274 * Returns 0 for success, non-zero for failure.
4275 */
4276static int
4277_base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4278{
4279 int r, i;
4280 unsigned long flags;
4281 u32 reply_address;
4282 u16 smid;
4283 struct _tr_list *delayed_tr, *delayed_tr_next;
4284 struct adapter_reply_queue *reply_q;
4285 long reply_post_free;
4286 u32 reply_post_free_sz;
4287
4288 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4289 __func__));
4290
4291 /* clean the delayed target reset list */
4292 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4293 &ioc->delayed_tr_list, list) {
4294 list_del(&delayed_tr->list);
4295 kfree(delayed_tr);
4296 }
4297
4298
4299 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4300 &ioc->delayed_tr_volume_list, list) {
4301 list_del(&delayed_tr->list);
4302 kfree(delayed_tr);
4303 }
4304
4305 /* initialize the scsi lookup free list */
4306 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4307 INIT_LIST_HEAD(&ioc->free_list);
4308 smid = 1;
4309 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
4310 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
4311 ioc->scsi_lookup[i].cb_idx = 0xFF;
4312 ioc->scsi_lookup[i].smid = smid;
4313 ioc->scsi_lookup[i].scmd = NULL;
4314 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4315 &ioc->free_list);
4316 }
4317
4318 /* hi-priority queue */
4319 INIT_LIST_HEAD(&ioc->hpr_free_list);
4320 smid = ioc->hi_priority_smid;
4321 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4322 ioc->hpr_lookup[i].cb_idx = 0xFF;
4323 ioc->hpr_lookup[i].smid = smid;
4324 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4325 &ioc->hpr_free_list);
4326 }
4327
4328 /* internal queue */
4329 INIT_LIST_HEAD(&ioc->internal_free_list);
4330 smid = ioc->internal_smid;
4331 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4332 ioc->internal_lookup[i].cb_idx = 0xFF;
4333 ioc->internal_lookup[i].smid = smid;
4334 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4335 &ioc->internal_free_list);
4336 }
4337
4338 /* chain pool */
4339 INIT_LIST_HEAD(&ioc->free_chain_list);
4340 for (i = 0; i < ioc->chain_depth; i++)
4341 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4342 &ioc->free_chain_list);
4343
4344 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4345
4346 /* initialize Reply Free Queue */
4347 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4348 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4349 ioc->reply_sz)
4350 ioc->reply_free[i] = cpu_to_le32(reply_address);
4351
4352 /* initialize reply queues */
4353 if (ioc->is_driver_loading)
4354 _base_assign_reply_queues(ioc);
4355
4356 /* initialize Reply Post Free Queue */
4357 reply_post_free = (long)ioc->reply_post_free;
4358 reply_post_free_sz = ioc->reply_post_queue_depth *
4359 sizeof(Mpi2DefaultReplyDescriptor_t);
4360 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4361 reply_q->reply_post_host_index = 0;
4362 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4363 reply_post_free;
4364 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4365 reply_q->reply_post_free[i].Words =
4366 cpu_to_le64(ULLONG_MAX);
4367 if (!_base_is_controller_msix_enabled(ioc))
4368 goto skip_init_reply_post_free_queue;
4369 reply_post_free += reply_post_free_sz;
4370 }
4371 skip_init_reply_post_free_queue:
4372
4373 r = _base_send_ioc_init(ioc, sleep_flag);
4374 if (r)
4375 return r;
4376
4377 /* initialize reply free host index */
4378 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
4379 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
4380
4381 /* initialize reply post host index */
4382 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4383 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4384 &ioc->chip->ReplyPostHostIndex);
4385 if (!_base_is_controller_msix_enabled(ioc))
4386 goto skip_init_reply_post_host_index;
4387 }
4388
4389 skip_init_reply_post_host_index:
4390
4391 _base_unmask_interrupts(ioc);
4392 r = _base_event_notification(ioc, sleep_flag);
4393 if (r)
4394 return r;
4395
4396 if (sleep_flag == CAN_SLEEP)
4397 _base_static_config_pages(ioc);
4398
4399
4400 if (ioc->is_driver_loading) {
4401 ioc->wait_for_discovery_to_complete =
4402 _base_determine_wait_on_discovery(ioc);
4403
4404 return r; /* scan_start and scan_finished support */
4405 }
4406
4407 r = _base_send_port_enable(ioc, sleep_flag);
4408 if (r)
4409 return r;
4410
4411 return r;
4412}
4413
4414/**
4415 * mpt3sas_base_free_resources - free resources controller resources
4416 * @ioc: per adapter object
4417 *
4418 * Return nothing.
4419 */
4420void
4421mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
4422{
4423 struct pci_dev *pdev = ioc->pdev;
4424
4425 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4426 __func__));
4427
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04004428 if (ioc->chip_phys && ioc->chip) {
4429 _base_mask_interrupts(ioc);
4430 ioc->shost_recovery = 1;
4431 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4432 ioc->shost_recovery = 0;
4433 }
4434
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304435 _base_free_irq(ioc);
4436 _base_disable_msix(ioc);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04004437
4438 if (ioc->chip_phys && ioc->chip)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304439 iounmap(ioc->chip);
4440 ioc->chip_phys = 0;
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04004441
4442 if (pci_is_enabled(pdev)) {
4443 pci_release_selected_regions(ioc->pdev, ioc->bars);
4444 pci_disable_pcie_error_reporting(pdev);
4445 pci_disable_device(pdev);
4446 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304447 return;
4448}
4449
4450/**
4451 * mpt3sas_base_attach - attach controller instance
4452 * @ioc: per adapter object
4453 *
4454 * Returns 0 for success, non-zero for failure.
4455 */
4456int
4457mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
4458{
4459 int r, i;
4460 int cpu_id, last_cpu_id = 0;
4461
4462 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4463 __func__));
4464
4465 /* setup cpu_msix_table */
4466 ioc->cpu_count = num_online_cpus();
4467 for_each_online_cpu(cpu_id)
4468 last_cpu_id = cpu_id;
4469 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4470 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4471 ioc->reply_queue_count = 1;
4472 if (!ioc->cpu_msix_table) {
4473 dfailprintk(ioc, pr_info(MPT3SAS_FMT
4474 "allocation for cpu_msix_table failed!!!\n",
4475 ioc->name));
4476 r = -ENOMEM;
4477 goto out_free_resources;
4478 }
4479
4480 r = mpt3sas_base_map_resources(ioc);
4481 if (r)
4482 goto out_free_resources;
4483
4484
4485 pci_set_drvdata(ioc->pdev, ioc->shost);
4486 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4487 if (r)
4488 goto out_free_resources;
4489
4490 /*
4491 * In SAS3.0,
4492 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
4493 * Target Status - all require the IEEE formated scatter gather
4494 * elements.
4495 */
4496
4497 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
4498 ioc->build_sg = &_base_build_sg_ieee;
4499 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
4500 ioc->mpi25 = 1;
4501 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
4502
4503 /*
4504 * These function pointers for other requests that don't
4505 * the require IEEE scatter gather elements.
4506 *
4507 * For example Configuration Pages and SAS IOUNIT Control don't.
4508 */
4509 ioc->build_sg_mpi = &_base_build_sg;
4510 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
4511
4512 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4513 if (r)
4514 goto out_free_resources;
4515
4516 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4517 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
4518 if (!ioc->pfacts) {
4519 r = -ENOMEM;
4520 goto out_free_resources;
4521 }
4522
4523 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4524 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4525 if (r)
4526 goto out_free_resources;
4527 }
4528
4529 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4530 if (r)
4531 goto out_free_resources;
4532
4533 init_waitqueue_head(&ioc->reset_wq);
4534
4535 /* allocate memory pd handle bitmask list */
4536 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4537 if (ioc->facts.MaxDevHandle % 8)
4538 ioc->pd_handles_sz++;
4539 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4540 GFP_KERNEL);
4541 if (!ioc->pd_handles) {
4542 r = -ENOMEM;
4543 goto out_free_resources;
4544 }
4545 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
4546 GFP_KERNEL);
4547 if (!ioc->blocking_handles) {
4548 r = -ENOMEM;
4549 goto out_free_resources;
4550 }
4551
4552 ioc->fwfault_debug = mpt3sas_fwfault_debug;
4553
4554 /* base internal command bits */
4555 mutex_init(&ioc->base_cmds.mutex);
4556 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4557 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4558
4559 /* port_enable command bits */
4560 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4561 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
4562
4563 /* transport internal command bits */
4564 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4565 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
4566 mutex_init(&ioc->transport_cmds.mutex);
4567
4568 /* scsih internal command bits */
4569 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4570 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
4571 mutex_init(&ioc->scsih_cmds.mutex);
4572
4573 /* task management internal command bits */
4574 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4575 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
4576 mutex_init(&ioc->tm_cmds.mutex);
4577
4578 /* config page internal command bits */
4579 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4580 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
4581 mutex_init(&ioc->config_cmds.mutex);
4582
4583 /* ctl module internal command bits */
4584 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4585 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4586 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
4587 mutex_init(&ioc->ctl_cmds.mutex);
4588
4589 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4590 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4591 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4592 !ioc->ctl_cmds.sense) {
4593 r = -ENOMEM;
4594 goto out_free_resources;
4595 }
4596
4597 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4598 ioc->event_masks[i] = -1;
4599
4600 /* here we enable the events we care about */
4601 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4602 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4603 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4604 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4605 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4606 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4607 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4608 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4609 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
4610 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
4611
4612 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
4613 if (r)
4614 goto out_free_resources;
4615
4616 return 0;
4617
4618 out_free_resources:
4619
4620 ioc->remove_host = 1;
4621
4622 mpt3sas_base_free_resources(ioc);
4623 _base_release_memory_pools(ioc);
4624 pci_set_drvdata(ioc->pdev, NULL);
4625 kfree(ioc->cpu_msix_table);
4626 kfree(ioc->pd_handles);
4627 kfree(ioc->blocking_handles);
4628 kfree(ioc->tm_cmds.reply);
4629 kfree(ioc->transport_cmds.reply);
4630 kfree(ioc->scsih_cmds.reply);
4631 kfree(ioc->config_cmds.reply);
4632 kfree(ioc->base_cmds.reply);
4633 kfree(ioc->port_enable_cmds.reply);
4634 kfree(ioc->ctl_cmds.reply);
4635 kfree(ioc->ctl_cmds.sense);
4636 kfree(ioc->pfacts);
4637 ioc->ctl_cmds.reply = NULL;
4638 ioc->base_cmds.reply = NULL;
4639 ioc->tm_cmds.reply = NULL;
4640 ioc->scsih_cmds.reply = NULL;
4641 ioc->transport_cmds.reply = NULL;
4642 ioc->config_cmds.reply = NULL;
4643 ioc->pfacts = NULL;
4644 return r;
4645}
4646
4647
4648/**
4649 * mpt3sas_base_detach - remove controller instance
4650 * @ioc: per adapter object
4651 *
4652 * Return nothing.
4653 */
4654void
4655mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
4656{
4657 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4658 __func__));
4659
4660 mpt3sas_base_stop_watchdog(ioc);
4661 mpt3sas_base_free_resources(ioc);
4662 _base_release_memory_pools(ioc);
4663 pci_set_drvdata(ioc->pdev, NULL);
4664 kfree(ioc->cpu_msix_table);
4665 kfree(ioc->pd_handles);
4666 kfree(ioc->blocking_handles);
4667 kfree(ioc->pfacts);
4668 kfree(ioc->ctl_cmds.reply);
4669 kfree(ioc->ctl_cmds.sense);
4670 kfree(ioc->base_cmds.reply);
4671 kfree(ioc->port_enable_cmds.reply);
4672 kfree(ioc->tm_cmds.reply);
4673 kfree(ioc->transport_cmds.reply);
4674 kfree(ioc->scsih_cmds.reply);
4675 kfree(ioc->config_cmds.reply);
4676}
4677
4678/**
4679 * _base_reset_handler - reset callback handler (for base)
4680 * @ioc: per adapter object
4681 * @reset_phase: phase
4682 *
4683 * The handler for doing any required cleanup or initialization.
4684 *
4685 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
4686 * MPT3_IOC_DONE_RESET
4687 *
4688 * Return nothing.
4689 */
4690static void
4691_base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
4692{
4693 mpt3sas_scsih_reset_handler(ioc, reset_phase);
4694 mpt3sas_ctl_reset_handler(ioc, reset_phase);
4695 switch (reset_phase) {
4696 case MPT3_IOC_PRE_RESET:
4697 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4698 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
4699 break;
4700 case MPT3_IOC_AFTER_RESET:
4701 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4702 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
4703 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
4704 ioc->transport_cmds.status |= MPT3_CMD_RESET;
4705 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4706 complete(&ioc->transport_cmds.done);
4707 }
4708 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
4709 ioc->base_cmds.status |= MPT3_CMD_RESET;
4710 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
4711 complete(&ioc->base_cmds.done);
4712 }
4713 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
4714 ioc->port_enable_failed = 1;
4715 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
4716 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4717 if (ioc->is_driver_loading) {
4718 ioc->start_scan_failed =
4719 MPI2_IOCSTATUS_INTERNAL_ERROR;
4720 ioc->start_scan = 0;
4721 ioc->port_enable_cmds.status =
4722 MPT3_CMD_NOT_USED;
4723 } else
4724 complete(&ioc->port_enable_cmds.done);
4725 }
4726 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
4727 ioc->config_cmds.status |= MPT3_CMD_RESET;
4728 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
4729 ioc->config_cmds.smid = USHRT_MAX;
4730 complete(&ioc->config_cmds.done);
4731 }
4732 break;
4733 case MPT3_IOC_DONE_RESET:
4734 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4735 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
4736 break;
4737 }
4738}
4739
4740/**
4741 * _wait_for_commands_to_complete - reset controller
4742 * @ioc: Pointer to MPT_ADAPTER structure
4743 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4744 *
4745 * This function waiting(3s) for all pending commands to complete
4746 * prior to putting controller in reset.
4747 */
4748static void
4749_wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc, int sleep_flag)
4750{
4751 u32 ioc_state;
4752 unsigned long flags;
4753 u16 i;
4754
4755 ioc->pending_io_count = 0;
4756 if (sleep_flag != CAN_SLEEP)
4757 return;
4758
4759 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4760 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4761 return;
4762
4763 /* pending command count */
4764 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4765 for (i = 0; i < ioc->scsiio_depth; i++)
4766 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4767 ioc->pending_io_count++;
4768 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4769
4770 if (!ioc->pending_io_count)
4771 return;
4772
4773 /* wait for pending commands to complete */
4774 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
4775}
4776
4777/**
4778 * mpt3sas_base_hard_reset_handler - reset controller
4779 * @ioc: Pointer to MPT_ADAPTER structure
4780 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4781 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4782 *
4783 * Returns 0 for success, non-zero for failure.
4784 */
4785int
4786mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc, int sleep_flag,
4787 enum reset_type type)
4788{
4789 int r;
4790 unsigned long flags;
4791 u32 ioc_state;
4792 u8 is_fault = 0, is_trigger = 0;
4793
4794 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
4795 __func__));
4796
4797 if (ioc->pci_error_recovery) {
4798 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
4799 ioc->name, __func__);
4800 r = 0;
4801 goto out_unlocked;
4802 }
4803
4804 if (mpt3sas_fwfault_debug)
4805 mpt3sas_halt_firmware(ioc);
4806
4807 /* TODO - What we really should be doing is pulling
4808 * out all the code associated with NO_SLEEP; its never used.
4809 * That is legacy code from mpt fusion driver, ported over.
4810 * I will leave this BUG_ON here for now till its been resolved.
4811 */
4812 BUG_ON(sleep_flag == NO_SLEEP);
4813
4814 /* wait for an active reset in progress to complete */
4815 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4816 do {
4817 ssleep(1);
4818 } while (ioc->shost_recovery == 1);
4819 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
4820 __func__));
4821 return ioc->ioc_reset_in_progress_status;
4822 }
4823
4824 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4825 ioc->shost_recovery = 1;
4826 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4827
4828 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
4829 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
4830 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
4831 MPT3_DIAG_BUFFER_IS_RELEASED))) {
4832 is_trigger = 1;
4833 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4834 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
4835 is_fault = 1;
4836 }
4837 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
4838 _wait_for_commands_to_complete(ioc, sleep_flag);
4839 _base_mask_interrupts(ioc);
4840 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4841 if (r)
4842 goto out;
4843 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
4844
4845 /* If this hard reset is called while port enable is active, then
4846 * there is no reason to call make_ioc_operational
4847 */
4848 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4849 ioc->remove_host = 1;
4850 r = -EFAULT;
4851 goto out;
4852 }
4853 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
4854 if (r)
4855 goto out;
4856 r = _base_make_ioc_operational(ioc, sleep_flag);
4857 if (!r)
4858 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
4859
4860 out:
4861 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
4862 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4863
4864 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
4865 ioc->ioc_reset_in_progress_status = r;
4866 ioc->shost_recovery = 0;
4867 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4868 ioc->ioc_reset_count++;
4869 mutex_unlock(&ioc->reset_in_progress_mutex);
4870
4871 out_unlocked:
4872 if ((r == 0) && is_trigger) {
4873 if (is_fault)
4874 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
4875 else
4876 mpt3sas_trigger_master(ioc,
4877 MASTER_TRIGGER_ADAPTER_RESET);
4878 }
4879 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
4880 __func__));
4881 return r;
4882}