]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/scsi/mpt3sas/mpt3sas_base.c
scsi: mpt3sas: Handle NVMe PCIe device related events generated from firmware.
[mirror_ubuntu-bionic-kernel.git] / drivers / scsi / mpt3sas / mpt3sas_base.c
1 /*
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
6 * Copyright (C) 2012-2014 LSI Corporation
7 * Copyright (C) 2013-2014 Avago Technologies
8 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * NO WARRANTY
21 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
22 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
23 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
24 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
25 * solely responsible for determining the appropriateness of using and
26 * distributing the Program and assumes all risks associated with its
27 * exercise of rights under this Agreement, including but not limited to
28 * the risks and costs of program errors, damage to or loss of data,
29 * programs or equipment, and unavailability or interruption of operations.
30
31 * DISCLAIMER OF LIABILITY
32 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
33 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
38 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
39
40 * You should have received a copy of the GNU General Public License
41 * along with this program; if not, write to the Free Software
42 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
43 * USA.
44 */
45
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/init.h>
50 #include <linux/slab.h>
51 #include <linux/types.h>
52 #include <linux/pci.h>
53 #include <linux/kdev_t.h>
54 #include <linux/blkdev.h>
55 #include <linux/delay.h>
56 #include <linux/interrupt.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/io.h>
59 #include <linux/time.h>
60 #include <linux/ktime.h>
61 #include <linux/kthread.h>
62 #include <asm/page.h> /* To get host page size per arch */
63 #include <linux/aer.h>
64
65
66 #include "mpt3sas_base.h"
67
68 static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
69
70
71 #define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
72
73 /* maximum controller queue depth */
74 #define MAX_HBA_QUEUE_DEPTH 30000
75 #define MAX_CHAIN_DEPTH 100000
76 static int max_queue_depth = -1;
77 module_param(max_queue_depth, int, 0);
78 MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
79
80 static int max_sgl_entries = -1;
81 module_param(max_sgl_entries, int, 0);
82 MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
83
84 static int msix_disable = -1;
85 module_param(msix_disable, int, 0);
86 MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
87
88 static int smp_affinity_enable = 1;
89 module_param(smp_affinity_enable, int, S_IRUGO);
90 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
91
92 static int max_msix_vectors = -1;
93 module_param(max_msix_vectors, int, 0);
94 MODULE_PARM_DESC(max_msix_vectors,
95 " max msix vectors");
96
97 static int mpt3sas_fwfault_debug;
98 MODULE_PARM_DESC(mpt3sas_fwfault_debug,
99 " enable detection of firmware fault and halt firmware - (default=0)");
100
101 static int
102 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
103
104 /**
105 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
106 *
107 */
108 static int
109 _scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
110 {
111 int ret = param_set_int(val, kp);
112 struct MPT3SAS_ADAPTER *ioc;
113
114 if (ret)
115 return ret;
116
117 /* global ioc spinlock to protect controller list on list operations */
118 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
119 spin_lock(&gioc_lock);
120 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
121 ioc->fwfault_debug = mpt3sas_fwfault_debug;
122 spin_unlock(&gioc_lock);
123 return 0;
124 }
125 module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
126 param_get_int, &mpt3sas_fwfault_debug, 0644);
127
128 /**
129 * mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
130 * @arg: input argument, used to derive ioc
131 *
132 * Return 0 if controller is removed from pci subsystem.
133 * Return -1 for other case.
134 */
135 static int mpt3sas_remove_dead_ioc_func(void *arg)
136 {
137 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
138 struct pci_dev *pdev;
139
140 if ((ioc == NULL))
141 return -1;
142
143 pdev = ioc->pdev;
144 if ((pdev == NULL))
145 return -1;
146 pci_stop_and_remove_bus_device_locked(pdev);
147 return 0;
148 }
149
150 /**
151 * _base_fault_reset_work - workq handling ioc fault conditions
152 * @work: input argument, used to derive ioc
153 * Context: sleep.
154 *
155 * Return nothing.
156 */
157 static void
158 _base_fault_reset_work(struct work_struct *work)
159 {
160 struct MPT3SAS_ADAPTER *ioc =
161 container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
162 unsigned long flags;
163 u32 doorbell;
164 int rc;
165 struct task_struct *p;
166
167
168 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
169 if (ioc->shost_recovery || ioc->pci_error_recovery)
170 goto rearm_timer;
171 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
172
173 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
174 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
175 pr_err(MPT3SAS_FMT "SAS host is non-operational !!!!\n",
176 ioc->name);
177
178 /* It may be possible that EEH recovery can resolve some of
179 * pci bus failure issues rather removing the dead ioc function
180 * by considering controller is in a non-operational state. So
181 * here priority is given to the EEH recovery. If it doesn't
182 * not resolve this issue, mpt3sas driver will consider this
183 * controller to non-operational state and remove the dead ioc
184 * function.
185 */
186 if (ioc->non_operational_loop++ < 5) {
187 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
188 flags);
189 goto rearm_timer;
190 }
191
192 /*
193 * Call _scsih_flush_pending_cmds callback so that we flush all
194 * pending commands back to OS. This call is required to aovid
195 * deadlock at block layer. Dead IOC will fail to do diag reset,
196 * and this call is safe since dead ioc will never return any
197 * command back from HW.
198 */
199 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
200 /*
201 * Set remove_host flag early since kernel thread will
202 * take some time to execute.
203 */
204 ioc->remove_host = 1;
205 /*Remove the Dead Host */
206 p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
207 "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
208 if (IS_ERR(p))
209 pr_err(MPT3SAS_FMT
210 "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
211 ioc->name, __func__);
212 else
213 pr_err(MPT3SAS_FMT
214 "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
215 ioc->name, __func__);
216 return; /* don't rearm timer */
217 }
218
219 ioc->non_operational_loop = 0;
220
221 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
222 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
223 pr_warn(MPT3SAS_FMT "%s: hard reset: %s\n", ioc->name,
224 __func__, (rc == 0) ? "success" : "failed");
225 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
226 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
227 mpt3sas_base_fault_info(ioc, doorbell &
228 MPI2_DOORBELL_DATA_MASK);
229 if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
230 MPI2_IOC_STATE_OPERATIONAL)
231 return; /* don't rearm timer */
232 }
233
234 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
235 rearm_timer:
236 if (ioc->fault_reset_work_q)
237 queue_delayed_work(ioc->fault_reset_work_q,
238 &ioc->fault_reset_work,
239 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
240 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
241 }
242
243 /**
244 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
245 * @ioc: per adapter object
246 * Context: sleep.
247 *
248 * Return nothing.
249 */
250 void
251 mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
252 {
253 unsigned long flags;
254
255 if (ioc->fault_reset_work_q)
256 return;
257
258 /* initialize fault polling */
259
260 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
261 snprintf(ioc->fault_reset_work_q_name,
262 sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
263 ioc->driver_name, ioc->id);
264 ioc->fault_reset_work_q =
265 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
266 if (!ioc->fault_reset_work_q) {
267 pr_err(MPT3SAS_FMT "%s: failed (line=%d)\n",
268 ioc->name, __func__, __LINE__);
269 return;
270 }
271 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
272 if (ioc->fault_reset_work_q)
273 queue_delayed_work(ioc->fault_reset_work_q,
274 &ioc->fault_reset_work,
275 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
276 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
277 }
278
279 /**
280 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
281 * @ioc: per adapter object
282 * Context: sleep.
283 *
284 * Return nothing.
285 */
286 void
287 mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
288 {
289 unsigned long flags;
290 struct workqueue_struct *wq;
291
292 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
293 wq = ioc->fault_reset_work_q;
294 ioc->fault_reset_work_q = NULL;
295 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
296 if (wq) {
297 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
298 flush_workqueue(wq);
299 destroy_workqueue(wq);
300 }
301 }
302
303 /**
304 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
305 * @ioc: per adapter object
306 * @fault_code: fault code
307 *
308 * Return nothing.
309 */
310 void
311 mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc , u16 fault_code)
312 {
313 pr_err(MPT3SAS_FMT "fault_state(0x%04x)!\n",
314 ioc->name, fault_code);
315 }
316
317 /**
318 * mpt3sas_halt_firmware - halt's mpt controller firmware
319 * @ioc: per adapter object
320 *
321 * For debugging timeout related issues. Writing 0xCOFFEE00
322 * to the doorbell register will halt controller firmware. With
323 * the purpose to stop both driver and firmware, the enduser can
324 * obtain a ring buffer from controller UART.
325 */
326 void
327 mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
328 {
329 u32 doorbell;
330
331 if (!ioc->fwfault_debug)
332 return;
333
334 dump_stack();
335
336 doorbell = readl(&ioc->chip->Doorbell);
337 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
338 mpt3sas_base_fault_info(ioc , doorbell);
339 else {
340 writel(0xC0FFEE00, &ioc->chip->Doorbell);
341 pr_err(MPT3SAS_FMT "Firmware is halted due to command timeout\n",
342 ioc->name);
343 }
344
345 if (ioc->fwfault_debug == 2)
346 for (;;)
347 ;
348 else
349 panic("panic in %s\n", __func__);
350 }
351
352 /**
353 * _base_sas_ioc_info - verbose translation of the ioc status
354 * @ioc: per adapter object
355 * @mpi_reply: reply mf payload returned from firmware
356 * @request_hdr: request mf
357 *
358 * Return nothing.
359 */
360 static void
361 _base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
362 MPI2RequestHeader_t *request_hdr)
363 {
364 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
365 MPI2_IOCSTATUS_MASK;
366 char *desc = NULL;
367 u16 frame_sz;
368 char *func_str = NULL;
369
370 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
371 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
372 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
373 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
374 return;
375
376 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
377 return;
378
379 switch (ioc_status) {
380
381 /****************************************************************************
382 * Common IOCStatus values for all replies
383 ****************************************************************************/
384
385 case MPI2_IOCSTATUS_INVALID_FUNCTION:
386 desc = "invalid function";
387 break;
388 case MPI2_IOCSTATUS_BUSY:
389 desc = "busy";
390 break;
391 case MPI2_IOCSTATUS_INVALID_SGL:
392 desc = "invalid sgl";
393 break;
394 case MPI2_IOCSTATUS_INTERNAL_ERROR:
395 desc = "internal error";
396 break;
397 case MPI2_IOCSTATUS_INVALID_VPID:
398 desc = "invalid vpid";
399 break;
400 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
401 desc = "insufficient resources";
402 break;
403 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
404 desc = "insufficient power";
405 break;
406 case MPI2_IOCSTATUS_INVALID_FIELD:
407 desc = "invalid field";
408 break;
409 case MPI2_IOCSTATUS_INVALID_STATE:
410 desc = "invalid state";
411 break;
412 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
413 desc = "op state not supported";
414 break;
415
416 /****************************************************************************
417 * Config IOCStatus values
418 ****************************************************************************/
419
420 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
421 desc = "config invalid action";
422 break;
423 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
424 desc = "config invalid type";
425 break;
426 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
427 desc = "config invalid page";
428 break;
429 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
430 desc = "config invalid data";
431 break;
432 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
433 desc = "config no defaults";
434 break;
435 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
436 desc = "config cant commit";
437 break;
438
439 /****************************************************************************
440 * SCSI IO Reply
441 ****************************************************************************/
442
443 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
444 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
445 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
446 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
447 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
448 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
449 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
450 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
451 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
452 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
453 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
454 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
455 break;
456
457 /****************************************************************************
458 * For use by SCSI Initiator and SCSI Target end-to-end data protection
459 ****************************************************************************/
460
461 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
462 desc = "eedp guard error";
463 break;
464 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
465 desc = "eedp ref tag error";
466 break;
467 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
468 desc = "eedp app tag error";
469 break;
470
471 /****************************************************************************
472 * SCSI Target values
473 ****************************************************************************/
474
475 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
476 desc = "target invalid io index";
477 break;
478 case MPI2_IOCSTATUS_TARGET_ABORTED:
479 desc = "target aborted";
480 break;
481 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
482 desc = "target no conn retryable";
483 break;
484 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
485 desc = "target no connection";
486 break;
487 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
488 desc = "target xfer count mismatch";
489 break;
490 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
491 desc = "target data offset error";
492 break;
493 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
494 desc = "target too much write data";
495 break;
496 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
497 desc = "target iu too short";
498 break;
499 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
500 desc = "target ack nak timeout";
501 break;
502 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
503 desc = "target nak received";
504 break;
505
506 /****************************************************************************
507 * Serial Attached SCSI values
508 ****************************************************************************/
509
510 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
511 desc = "smp request failed";
512 break;
513 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
514 desc = "smp data overrun";
515 break;
516
517 /****************************************************************************
518 * Diagnostic Buffer Post / Diagnostic Release values
519 ****************************************************************************/
520
521 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
522 desc = "diagnostic released";
523 break;
524 default:
525 break;
526 }
527
528 if (!desc)
529 return;
530
531 switch (request_hdr->Function) {
532 case MPI2_FUNCTION_CONFIG:
533 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
534 func_str = "config_page";
535 break;
536 case MPI2_FUNCTION_SCSI_TASK_MGMT:
537 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
538 func_str = "task_mgmt";
539 break;
540 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
541 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
542 func_str = "sas_iounit_ctl";
543 break;
544 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
545 frame_sz = sizeof(Mpi2SepRequest_t);
546 func_str = "enclosure";
547 break;
548 case MPI2_FUNCTION_IOC_INIT:
549 frame_sz = sizeof(Mpi2IOCInitRequest_t);
550 func_str = "ioc_init";
551 break;
552 case MPI2_FUNCTION_PORT_ENABLE:
553 frame_sz = sizeof(Mpi2PortEnableRequest_t);
554 func_str = "port_enable";
555 break;
556 case MPI2_FUNCTION_SMP_PASSTHROUGH:
557 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
558 func_str = "smp_passthru";
559 break;
560 case MPI2_FUNCTION_NVME_ENCAPSULATED:
561 frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
562 ioc->sge_size;
563 func_str = "nvme_encapsulated";
564 break;
565 default:
566 frame_sz = 32;
567 func_str = "unknown";
568 break;
569 }
570
571 pr_warn(MPT3SAS_FMT "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
572 ioc->name, desc, ioc_status, request_hdr, func_str);
573
574 _debug_dump_mf(request_hdr, frame_sz/4);
575 }
576
577 /**
578 * _base_display_event_data - verbose translation of firmware asyn events
579 * @ioc: per adapter object
580 * @mpi_reply: reply mf payload returned from firmware
581 *
582 * Return nothing.
583 */
584 static void
585 _base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
586 Mpi2EventNotificationReply_t *mpi_reply)
587 {
588 char *desc = NULL;
589 u16 event;
590
591 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
592 return;
593
594 event = le16_to_cpu(mpi_reply->Event);
595
596 switch (event) {
597 case MPI2_EVENT_LOG_DATA:
598 desc = "Log Data";
599 break;
600 case MPI2_EVENT_STATE_CHANGE:
601 desc = "Status Change";
602 break;
603 case MPI2_EVENT_HARD_RESET_RECEIVED:
604 desc = "Hard Reset Received";
605 break;
606 case MPI2_EVENT_EVENT_CHANGE:
607 desc = "Event Change";
608 break;
609 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
610 desc = "Device Status Change";
611 break;
612 case MPI2_EVENT_IR_OPERATION_STATUS:
613 if (!ioc->hide_ir_msg)
614 desc = "IR Operation Status";
615 break;
616 case MPI2_EVENT_SAS_DISCOVERY:
617 {
618 Mpi2EventDataSasDiscovery_t *event_data =
619 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
620 pr_info(MPT3SAS_FMT "Discovery: (%s)", ioc->name,
621 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
622 "start" : "stop");
623 if (event_data->DiscoveryStatus)
624 pr_cont(" discovery_status(0x%08x)",
625 le32_to_cpu(event_data->DiscoveryStatus));
626 pr_cont("\n");
627 return;
628 }
629 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
630 desc = "SAS Broadcast Primitive";
631 break;
632 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
633 desc = "SAS Init Device Status Change";
634 break;
635 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
636 desc = "SAS Init Table Overflow";
637 break;
638 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
639 desc = "SAS Topology Change List";
640 break;
641 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
642 desc = "SAS Enclosure Device Status Change";
643 break;
644 case MPI2_EVENT_IR_VOLUME:
645 if (!ioc->hide_ir_msg)
646 desc = "IR Volume";
647 break;
648 case MPI2_EVENT_IR_PHYSICAL_DISK:
649 if (!ioc->hide_ir_msg)
650 desc = "IR Physical Disk";
651 break;
652 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
653 if (!ioc->hide_ir_msg)
654 desc = "IR Configuration Change List";
655 break;
656 case MPI2_EVENT_LOG_ENTRY_ADDED:
657 if (!ioc->hide_ir_msg)
658 desc = "Log Entry Added";
659 break;
660 case MPI2_EVENT_TEMP_THRESHOLD:
661 desc = "Temperature Threshold";
662 break;
663 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
664 desc = "Cable Event";
665 break;
666 case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
667 desc = "PCIE Device Status Change";
668 break;
669 case MPI2_EVENT_PCIE_ENUMERATION:
670 {
671 Mpi26EventDataPCIeEnumeration_t *event_data =
672 (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
673 pr_info(MPT3SAS_FMT "PCIE Enumeration: (%s)", ioc->name,
674 (event_data->ReasonCode ==
675 MPI26_EVENT_PCIE_ENUM_RC_STARTED) ?
676 "start" : "stop");
677 if (event_data->EnumerationStatus)
678 pr_info("enumeration_status(0x%08x)",
679 le32_to_cpu(event_data->EnumerationStatus));
680 pr_info("\n");
681 return;
682 }
683 case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
684 desc = "PCIE Topology Change List";
685 break;
686 }
687
688 if (!desc)
689 return;
690
691 pr_info(MPT3SAS_FMT "%s\n", ioc->name, desc);
692 }
693
694 /**
695 * _base_sas_log_info - verbose translation of firmware log info
696 * @ioc: per adapter object
697 * @log_info: log info
698 *
699 * Return nothing.
700 */
701 static void
702 _base_sas_log_info(struct MPT3SAS_ADAPTER *ioc , u32 log_info)
703 {
704 union loginfo_type {
705 u32 loginfo;
706 struct {
707 u32 subcode:16;
708 u32 code:8;
709 u32 originator:4;
710 u32 bus_type:4;
711 } dw;
712 };
713 union loginfo_type sas_loginfo;
714 char *originator_str = NULL;
715
716 sas_loginfo.loginfo = log_info;
717 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
718 return;
719
720 /* each nexus loss loginfo */
721 if (log_info == 0x31170000)
722 return;
723
724 /* eat the loginfos associated with task aborts */
725 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
726 0x31140000 || log_info == 0x31130000))
727 return;
728
729 switch (sas_loginfo.dw.originator) {
730 case 0:
731 originator_str = "IOP";
732 break;
733 case 1:
734 originator_str = "PL";
735 break;
736 case 2:
737 if (!ioc->hide_ir_msg)
738 originator_str = "IR";
739 else
740 originator_str = "WarpDrive";
741 break;
742 }
743
744 pr_warn(MPT3SAS_FMT
745 "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
746 ioc->name, log_info,
747 originator_str, sas_loginfo.dw.code,
748 sas_loginfo.dw.subcode);
749 }
750
751 /**
752 * _base_display_reply_info -
753 * @ioc: per adapter object
754 * @smid: system request message index
755 * @msix_index: MSIX table index supplied by the OS
756 * @reply: reply message frame(lower 32bit addr)
757 *
758 * Return nothing.
759 */
760 static void
761 _base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
762 u32 reply)
763 {
764 MPI2DefaultReply_t *mpi_reply;
765 u16 ioc_status;
766 u32 loginfo = 0;
767
768 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
769 if (unlikely(!mpi_reply)) {
770 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
771 ioc->name, __FILE__, __LINE__, __func__);
772 return;
773 }
774 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
775
776 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
777 (ioc->logging_level & MPT_DEBUG_REPLY)) {
778 _base_sas_ioc_info(ioc , mpi_reply,
779 mpt3sas_base_get_msg_frame(ioc, smid));
780 }
781
782 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
783 loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
784 _base_sas_log_info(ioc, loginfo);
785 }
786
787 if (ioc_status || loginfo) {
788 ioc_status &= MPI2_IOCSTATUS_MASK;
789 mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
790 }
791 }
792
793 /**
794 * mpt3sas_base_done - base internal command completion routine
795 * @ioc: per adapter object
796 * @smid: system request message index
797 * @msix_index: MSIX table index supplied by the OS
798 * @reply: reply message frame(lower 32bit addr)
799 *
800 * Return 1 meaning mf should be freed from _base_interrupt
801 * 0 means the mf is freed from this function.
802 */
803 u8
804 mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
805 u32 reply)
806 {
807 MPI2DefaultReply_t *mpi_reply;
808
809 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
810 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
811 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
812
813 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
814 return 1;
815
816 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
817 if (mpi_reply) {
818 ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
819 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
820 }
821 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;
822
823 complete(&ioc->base_cmds.done);
824 return 1;
825 }
826
827 /**
828 * _base_async_event - main callback handler for firmware asyn events
829 * @ioc: per adapter object
830 * @msix_index: MSIX table index supplied by the OS
831 * @reply: reply message frame(lower 32bit addr)
832 *
833 * Return 1 meaning mf should be freed from _base_interrupt
834 * 0 means the mf is freed from this function.
835 */
836 static u8
837 _base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
838 {
839 Mpi2EventNotificationReply_t *mpi_reply;
840 Mpi2EventAckRequest_t *ack_request;
841 u16 smid;
842 struct _event_ack_list *delayed_event_ack;
843
844 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
845 if (!mpi_reply)
846 return 1;
847 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
848 return 1;
849
850 _base_display_event_data(ioc, mpi_reply);
851
852 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
853 goto out;
854 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
855 if (!smid) {
856 delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
857 GFP_ATOMIC);
858 if (!delayed_event_ack)
859 goto out;
860 INIT_LIST_HEAD(&delayed_event_ack->list);
861 delayed_event_ack->Event = mpi_reply->Event;
862 delayed_event_ack->EventContext = mpi_reply->EventContext;
863 list_add_tail(&delayed_event_ack->list,
864 &ioc->delayed_event_ack_list);
865 dewtprintk(ioc, pr_info(MPT3SAS_FMT
866 "DELAYED: EVENT ACK: event (0x%04x)\n",
867 ioc->name, le16_to_cpu(mpi_reply->Event)));
868 goto out;
869 }
870
871 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
872 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
873 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
874 ack_request->Event = mpi_reply->Event;
875 ack_request->EventContext = mpi_reply->EventContext;
876 ack_request->VF_ID = 0; /* TODO */
877 ack_request->VP_ID = 0;
878 ioc->put_smid_default(ioc, smid);
879
880 out:
881
882 /* scsih callback handler */
883 mpt3sas_scsih_event_callback(ioc, msix_index, reply);
884
885 /* ctl callback handler */
886 mpt3sas_ctl_event_callback(ioc, msix_index, reply);
887
888 return 1;
889 }
890
891 /**
892 * _base_get_cb_idx - obtain the callback index
893 * @ioc: per adapter object
894 * @smid: system request message index
895 *
896 * Return callback index.
897 */
898 static u8
899 _base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
900 {
901 int i;
902 u8 cb_idx;
903
904 if (smid < ioc->hi_priority_smid) {
905 i = smid - 1;
906 cb_idx = ioc->scsi_lookup[i].cb_idx;
907 } else if (smid < ioc->internal_smid) {
908 i = smid - ioc->hi_priority_smid;
909 cb_idx = ioc->hpr_lookup[i].cb_idx;
910 } else if (smid <= ioc->hba_queue_depth) {
911 i = smid - ioc->internal_smid;
912 cb_idx = ioc->internal_lookup[i].cb_idx;
913 } else
914 cb_idx = 0xFF;
915 return cb_idx;
916 }
917
918 /**
919 * _base_mask_interrupts - disable interrupts
920 * @ioc: per adapter object
921 *
922 * Disabling ResetIRQ, Reply and Doorbell Interrupts
923 *
924 * Return nothing.
925 */
926 static void
927 _base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
928 {
929 u32 him_register;
930
931 ioc->mask_interrupts = 1;
932 him_register = readl(&ioc->chip->HostInterruptMask);
933 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
934 writel(him_register, &ioc->chip->HostInterruptMask);
935 readl(&ioc->chip->HostInterruptMask);
936 }
937
938 /**
939 * _base_unmask_interrupts - enable interrupts
940 * @ioc: per adapter object
941 *
942 * Enabling only Reply Interrupts
943 *
944 * Return nothing.
945 */
946 static void
947 _base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
948 {
949 u32 him_register;
950
951 him_register = readl(&ioc->chip->HostInterruptMask);
952 him_register &= ~MPI2_HIM_RIM;
953 writel(him_register, &ioc->chip->HostInterruptMask);
954 ioc->mask_interrupts = 0;
955 }
956
957 union reply_descriptor {
958 u64 word;
959 struct {
960 u32 low;
961 u32 high;
962 } u;
963 };
964
965 /**
966 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
967 * @irq: irq number (not used)
968 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
969 * @r: pt_regs pointer (not used)
970 *
971 * Return IRQ_HANDLE if processed, else IRQ_NONE.
972 */
973 static irqreturn_t
974 _base_interrupt(int irq, void *bus_id)
975 {
976 struct adapter_reply_queue *reply_q = bus_id;
977 union reply_descriptor rd;
978 u32 completed_cmds;
979 u8 request_desript_type;
980 u16 smid;
981 u8 cb_idx;
982 u32 reply;
983 u8 msix_index = reply_q->msix_index;
984 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
985 Mpi2ReplyDescriptorsUnion_t *rpf;
986 u8 rc;
987
988 if (ioc->mask_interrupts)
989 return IRQ_NONE;
990
991 if (!atomic_add_unless(&reply_q->busy, 1, 1))
992 return IRQ_NONE;
993
994 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
995 request_desript_type = rpf->Default.ReplyFlags
996 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
997 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
998 atomic_dec(&reply_q->busy);
999 return IRQ_NONE;
1000 }
1001
1002 completed_cmds = 0;
1003 cb_idx = 0xFF;
1004 do {
1005 rd.word = le64_to_cpu(rpf->Words);
1006 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
1007 goto out;
1008 reply = 0;
1009 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
1010 if (request_desript_type ==
1011 MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
1012 request_desript_type ==
1013 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
1014 request_desript_type ==
1015 MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
1016 cb_idx = _base_get_cb_idx(ioc, smid);
1017 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1018 (likely(mpt_callbacks[cb_idx] != NULL))) {
1019 rc = mpt_callbacks[cb_idx](ioc, smid,
1020 msix_index, 0);
1021 if (rc)
1022 mpt3sas_base_free_smid(ioc, smid);
1023 }
1024 } else if (request_desript_type ==
1025 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
1026 reply = le32_to_cpu(
1027 rpf->AddressReply.ReplyFrameAddress);
1028 if (reply > ioc->reply_dma_max_address ||
1029 reply < ioc->reply_dma_min_address)
1030 reply = 0;
1031 if (smid) {
1032 cb_idx = _base_get_cb_idx(ioc, smid);
1033 if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
1034 (likely(mpt_callbacks[cb_idx] != NULL))) {
1035 rc = mpt_callbacks[cb_idx](ioc, smid,
1036 msix_index, reply);
1037 if (reply)
1038 _base_display_reply_info(ioc,
1039 smid, msix_index, reply);
1040 if (rc)
1041 mpt3sas_base_free_smid(ioc,
1042 smid);
1043 }
1044 } else {
1045 _base_async_event(ioc, msix_index, reply);
1046 }
1047
1048 /* reply free queue handling */
1049 if (reply) {
1050 ioc->reply_free_host_index =
1051 (ioc->reply_free_host_index ==
1052 (ioc->reply_free_queue_depth - 1)) ?
1053 0 : ioc->reply_free_host_index + 1;
1054 ioc->reply_free[ioc->reply_free_host_index] =
1055 cpu_to_le32(reply);
1056 writel(ioc->reply_free_host_index,
1057 &ioc->chip->ReplyFreeHostIndex);
1058 }
1059 }
1060
1061 rpf->Words = cpu_to_le64(ULLONG_MAX);
1062 reply_q->reply_post_host_index =
1063 (reply_q->reply_post_host_index ==
1064 (ioc->reply_post_queue_depth - 1)) ? 0 :
1065 reply_q->reply_post_host_index + 1;
1066 request_desript_type =
1067 reply_q->reply_post_free[reply_q->reply_post_host_index].
1068 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1069 completed_cmds++;
1070 /* Update the reply post host index after continuously
1071 * processing the threshold number of Reply Descriptors.
1072 * So that FW can find enough entries to post the Reply
1073 * Descriptors in the reply descriptor post queue.
1074 */
1075 if (completed_cmds > ioc->hba_queue_depth/3) {
1076 if (ioc->combined_reply_queue) {
1077 writel(reply_q->reply_post_host_index |
1078 ((msix_index & 7) <<
1079 MPI2_RPHI_MSIX_INDEX_SHIFT),
1080 ioc->replyPostRegisterIndex[msix_index/8]);
1081 } else {
1082 writel(reply_q->reply_post_host_index |
1083 (msix_index <<
1084 MPI2_RPHI_MSIX_INDEX_SHIFT),
1085 &ioc->chip->ReplyPostHostIndex);
1086 }
1087 completed_cmds = 1;
1088 }
1089 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1090 goto out;
1091 if (!reply_q->reply_post_host_index)
1092 rpf = reply_q->reply_post_free;
1093 else
1094 rpf++;
1095 } while (1);
1096
1097 out:
1098
1099 if (!completed_cmds) {
1100 atomic_dec(&reply_q->busy);
1101 return IRQ_NONE;
1102 }
1103
1104 if (ioc->is_warpdrive) {
1105 writel(reply_q->reply_post_host_index,
1106 ioc->reply_post_host_index[msix_index]);
1107 atomic_dec(&reply_q->busy);
1108 return IRQ_HANDLED;
1109 }
1110
1111 /* Update Reply Post Host Index.
1112 * For those HBA's which support combined reply queue feature
1113 * 1. Get the correct Supplemental Reply Post Host Index Register.
1114 * i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
1115 * Index Register address bank i.e replyPostRegisterIndex[],
1116 * 2. Then update this register with new reply host index value
1117 * in ReplyPostIndex field and the MSIxIndex field with
1118 * msix_index value reduced to a value between 0 and 7,
1119 * using a modulo 8 operation. Since each Supplemental Reply Post
1120 * Host Index Register supports 8 MSI-X vectors.
1121 *
1122 * For other HBA's just update the Reply Post Host Index register with
1123 * new reply host index value in ReplyPostIndex Field and msix_index
1124 * value in MSIxIndex field.
1125 */
1126 if (ioc->combined_reply_queue)
1127 writel(reply_q->reply_post_host_index | ((msix_index & 7) <<
1128 MPI2_RPHI_MSIX_INDEX_SHIFT),
1129 ioc->replyPostRegisterIndex[msix_index/8]);
1130 else
1131 writel(reply_q->reply_post_host_index | (msix_index <<
1132 MPI2_RPHI_MSIX_INDEX_SHIFT),
1133 &ioc->chip->ReplyPostHostIndex);
1134 atomic_dec(&reply_q->busy);
1135 return IRQ_HANDLED;
1136 }
1137
1138 /**
1139 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1140 * @ioc: per adapter object
1141 *
1142 */
1143 static inline int
1144 _base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
1145 {
1146 return (ioc->facts.IOCCapabilities &
1147 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1148 }
1149
1150 /**
1151 * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
1152 * @ioc: per adapter object
1153 * Context: non ISR conext
1154 *
1155 * Called when a Task Management request has completed.
1156 *
1157 * Return nothing.
1158 */
1159 void
1160 mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc)
1161 {
1162 struct adapter_reply_queue *reply_q;
1163
1164 /* If MSIX capability is turned off
1165 * then multi-queues are not enabled
1166 */
1167 if (!_base_is_controller_msix_enabled(ioc))
1168 return;
1169
1170 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1171 if (ioc->shost_recovery || ioc->remove_host ||
1172 ioc->pci_error_recovery)
1173 return;
1174 /* TMs are on msix_index == 0 */
1175 if (reply_q->msix_index == 0)
1176 continue;
1177 synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
1178 }
1179 }
1180
1181 /**
1182 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
1183 * @cb_idx: callback index
1184 *
1185 * Return nothing.
1186 */
1187 void
1188 mpt3sas_base_release_callback_handler(u8 cb_idx)
1189 {
1190 mpt_callbacks[cb_idx] = NULL;
1191 }
1192
1193 /**
1194 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
1195 * @cb_func: callback function
1196 *
1197 * Returns cb_func.
1198 */
1199 u8
1200 mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1201 {
1202 u8 cb_idx;
1203
1204 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1205 if (mpt_callbacks[cb_idx] == NULL)
1206 break;
1207
1208 mpt_callbacks[cb_idx] = cb_func;
1209 return cb_idx;
1210 }
1211
1212 /**
1213 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
1214 *
1215 * Return nothing.
1216 */
1217 void
1218 mpt3sas_base_initialize_callback_handler(void)
1219 {
1220 u8 cb_idx;
1221
1222 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1223 mpt3sas_base_release_callback_handler(cb_idx);
1224 }
1225
1226
1227 /**
1228 * _base_build_zero_len_sge - build zero length sg entry
1229 * @ioc: per adapter object
1230 * @paddr: virtual address for SGE
1231 *
1232 * Create a zero length scatter gather entry to insure the IOCs hardware has
1233 * something to use if the target device goes brain dead and tries
1234 * to send data even when none is asked for.
1235 *
1236 * Return nothing.
1237 */
1238 static void
1239 _base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1240 {
1241 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1242 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1243 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1244 MPI2_SGE_FLAGS_SHIFT);
1245 ioc->base_add_sg_single(paddr, flags_length, -1);
1246 }
1247
1248 /**
1249 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1250 * @paddr: virtual address for SGE
1251 * @flags_length: SGE flags and data transfer length
1252 * @dma_addr: Physical address
1253 *
1254 * Return nothing.
1255 */
1256 static void
1257 _base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1258 {
1259 Mpi2SGESimple32_t *sgel = paddr;
1260
1261 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1262 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1263 sgel->FlagsLength = cpu_to_le32(flags_length);
1264 sgel->Address = cpu_to_le32(dma_addr);
1265 }
1266
1267
1268 /**
1269 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1270 * @paddr: virtual address for SGE
1271 * @flags_length: SGE flags and data transfer length
1272 * @dma_addr: Physical address
1273 *
1274 * Return nothing.
1275 */
1276 static void
1277 _base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1278 {
1279 Mpi2SGESimple64_t *sgel = paddr;
1280
1281 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1282 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1283 sgel->FlagsLength = cpu_to_le32(flags_length);
1284 sgel->Address = cpu_to_le64(dma_addr);
1285 }
1286
1287 /**
1288 * _base_get_chain_buffer_tracker - obtain chain tracker
1289 * @ioc: per adapter object
1290 * @smid: smid associated to an IO request
1291 *
1292 * Returns chain tracker(from ioc->free_chain_list)
1293 */
1294 static struct chain_tracker *
1295 _base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1296 {
1297 struct chain_tracker *chain_req;
1298 unsigned long flags;
1299
1300 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1301 if (list_empty(&ioc->free_chain_list)) {
1302 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1303 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1304 "chain buffers not available\n", ioc->name));
1305 return NULL;
1306 }
1307 chain_req = list_entry(ioc->free_chain_list.next,
1308 struct chain_tracker, tracker_list);
1309 list_del_init(&chain_req->tracker_list);
1310 list_add_tail(&chain_req->tracker_list,
1311 &ioc->scsi_lookup[smid - 1].chain_list);
1312 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1313 return chain_req;
1314 }
1315
1316
1317 /**
1318 * _base_build_sg - build generic sg
1319 * @ioc: per adapter object
1320 * @psge: virtual address for SGE
1321 * @data_out_dma: physical address for WRITES
1322 * @data_out_sz: data xfer size for WRITES
1323 * @data_in_dma: physical address for READS
1324 * @data_in_sz: data xfer size for READS
1325 *
1326 * Return nothing.
1327 */
1328 static void
1329 _base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
1330 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1331 size_t data_in_sz)
1332 {
1333 u32 sgl_flags;
1334
1335 if (!data_out_sz && !data_in_sz) {
1336 _base_build_zero_len_sge(ioc, psge);
1337 return;
1338 }
1339
1340 if (data_out_sz && data_in_sz) {
1341 /* WRITE sgel first */
1342 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1343 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
1344 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1345 ioc->base_add_sg_single(psge, sgl_flags |
1346 data_out_sz, data_out_dma);
1347
1348 /* incr sgel */
1349 psge += ioc->sge_size;
1350
1351 /* READ sgel last */
1352 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1353 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1354 MPI2_SGE_FLAGS_END_OF_LIST);
1355 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1356 ioc->base_add_sg_single(psge, sgl_flags |
1357 data_in_sz, data_in_dma);
1358 } else if (data_out_sz) /* WRITE */ {
1359 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1360 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1361 MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
1362 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1363 ioc->base_add_sg_single(psge, sgl_flags |
1364 data_out_sz, data_out_dma);
1365 } else if (data_in_sz) /* READ */ {
1366 sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
1367 MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
1368 MPI2_SGE_FLAGS_END_OF_LIST);
1369 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1370 ioc->base_add_sg_single(psge, sgl_flags |
1371 data_in_sz, data_in_dma);
1372 }
1373 }
1374
1375 /* IEEE format sgls */
1376
1377 /**
1378 * _base_build_nvme_prp - This function is called for NVMe end devices to build
1379 * a native SGL (NVMe PRP). The native SGL is built starting in the first PRP
1380 * entry of the NVMe message (PRP1). If the data buffer is small enough to be
1381 * described entirely using PRP1, then PRP2 is not used. If needed, PRP2 is
1382 * used to describe a larger data buffer. If the data buffer is too large to
1383 * describe using the two PRP entriess inside the NVMe message, then PRP1
1384 * describes the first data memory segment, and PRP2 contains a pointer to a PRP
1385 * list located elsewhere in memory to describe the remaining data memory
1386 * segments. The PRP list will be contiguous.
1387
1388 * The native SGL for NVMe devices is a Physical Region Page (PRP). A PRP
1389 * consists of a list of PRP entries to describe a number of noncontigous
1390 * physical memory segments as a single memory buffer, just as a SGL does. Note
1391 * however, that this function is only used by the IOCTL call, so the memory
1392 * given will be guaranteed to be contiguous. There is no need to translate
1393 * non-contiguous SGL into a PRP in this case. All PRPs will describe
1394 * contiguous space that is one page size each.
1395 *
1396 * Each NVMe message contains two PRP entries. The first (PRP1) either contains
1397 * a PRP list pointer or a PRP element, depending upon the command. PRP2
1398 * contains the second PRP element if the memory being described fits within 2
1399 * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
1400 *
1401 * A PRP list pointer contains the address of a PRP list, structured as a linear
1402 * array of PRP entries. Each PRP entry in this list describes a segment of
1403 * physical memory.
1404 *
1405 * Each 64-bit PRP entry comprises an address and an offset field. The address
1406 * always points at the beginning of a 4KB physical memory page, and the offset
1407 * describes where within that 4KB page the memory segment begins. Only the
1408 * first element in a PRP list may contain a non-zero offest, implying that all
1409 * memory segments following the first begin at the start of a 4KB page.
1410 *
1411 * Each PRP element normally describes 4KB of physical memory, with exceptions
1412 * for the first and last elements in the list. If the memory being described
1413 * by the list begins at a non-zero offset within the first 4KB page, then the
1414 * first PRP element will contain a non-zero offset indicating where the region
1415 * begins within the 4KB page. The last memory segment may end before the end
1416 * of the 4KB segment, depending upon the overall size of the memory being
1417 * described by the PRP list.
1418 *
1419 * Since PRP entries lack any indication of size, the overall data buffer length
1420 * is used to determine where the end of the data memory buffer is located, and
1421 * how many PRP entries are required to describe it.
1422 *
1423 * @ioc: per adapter object
1424 * @smid: system request message index for getting asscociated SGL
1425 * @nvme_encap_request: the NVMe request msg frame pointer
1426 * @data_out_dma: physical address for WRITES
1427 * @data_out_sz: data xfer size for WRITES
1428 * @data_in_dma: physical address for READS
1429 * @data_in_sz: data xfer size for READS
1430 *
1431 * Returns nothing.
1432 */
1433 static void
1434 _base_build_nvme_prp(struct MPT3SAS_ADAPTER *ioc, u16 smid,
1435 Mpi26NVMeEncapsulatedRequest_t *nvme_encap_request,
1436 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
1437 size_t data_in_sz)
1438 {
1439 int prp_size = NVME_PRP_SIZE;
1440 u64 *prp_entry, *prp1_entry, *prp2_entry, *prp_entry_phys;
1441 u64 *prp_page, *prp_page_phys;
1442 u32 offset, entry_len;
1443 u32 page_mask_result, page_mask;
1444 dma_addr_t paddr;
1445 size_t length;
1446
1447 /*
1448 * Not all commands require a data transfer. If no data, just return
1449 * without constructing any PRP.
1450 */
1451 if (!data_in_sz && !data_out_sz)
1452 return;
1453 /*
1454 * Set pointers to PRP1 and PRP2, which are in the NVMe command.
1455 * PRP1 is located at a 24 byte offset from the start of the NVMe
1456 * command. Then set the current PRP entry pointer to PRP1.
1457 */
1458 prp1_entry = (u64 *)(nvme_encap_request->NVMe_Command +
1459 NVME_CMD_PRP1_OFFSET);
1460 prp2_entry = (u64 *)(nvme_encap_request->NVMe_Command +
1461 NVME_CMD_PRP2_OFFSET);
1462 prp_entry = prp1_entry;
1463 /*
1464 * For the PRP entries, use the specially allocated buffer of
1465 * contiguous memory.
1466 */
1467 prp_page = (u64 *)mpt3sas_base_get_pcie_sgl(ioc, smid);
1468 prp_page_phys = (u64 *)mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
1469
1470 /*
1471 * Check if we are within 1 entry of a page boundary we don't
1472 * want our first entry to be a PRP List entry.
1473 */
1474 page_mask = ioc->page_size - 1;
1475 page_mask_result = (uintptr_t)((u8 *)prp_page + prp_size) & page_mask;
1476 if (!page_mask_result) {
1477 /* Bump up to next page boundary. */
1478 prp_page = (u64 *)((u8 *)prp_page + prp_size);
1479 prp_page_phys = (u64 *)((u8 *)prp_page_phys + prp_size);
1480 }
1481
1482 /*
1483 * Set PRP physical pointer, which initially points to the current PRP
1484 * DMA memory page.
1485 */
1486 prp_entry_phys = prp_page_phys;
1487
1488 /* Get physical address and length of the data buffer. */
1489 if (data_in_sz) {
1490 paddr = data_in_dma;
1491 length = data_in_sz;
1492 } else {
1493 paddr = data_out_dma;
1494 length = data_out_sz;
1495 }
1496
1497 /* Loop while the length is not zero. */
1498 while (length) {
1499 /*
1500 * Check if we need to put a list pointer here if we are at
1501 * page boundary - prp_size (8 bytes).
1502 */
1503 page_mask_result =
1504 (uintptr_t)((u8 *)prp_entry_phys + prp_size) & page_mask;
1505 if (!page_mask_result) {
1506 /*
1507 * This is the last entry in a PRP List, so we need to
1508 * put a PRP list pointer here. What this does is:
1509 * - bump the current memory pointer to the next
1510 * address, which will be the next full page.
1511 * - set the PRP Entry to point to that page. This
1512 * is now the PRP List pointer.
1513 * - bump the PRP Entry pointer the start of the
1514 * next page. Since all of this PRP memory is
1515 * contiguous, no need to get a new page - it's
1516 * just the next address.
1517 */
1518 prp_entry_phys++;
1519 *prp_entry = cpu_to_le64((uintptr_t)prp_entry_phys);
1520 prp_entry++;
1521 }
1522
1523 /* Need to handle if entry will be part of a page. */
1524 offset = (u32)paddr & page_mask;
1525 entry_len = ioc->page_size - offset;
1526
1527 if (prp_entry == prp1_entry) {
1528 /*
1529 * Must fill in the first PRP pointer (PRP1) before
1530 * moving on.
1531 */
1532 *prp1_entry = cpu_to_le64((u64)paddr);
1533
1534 /*
1535 * Now point to the second PRP entry within the
1536 * command (PRP2).
1537 */
1538 prp_entry = prp2_entry;
1539 } else if (prp_entry == prp2_entry) {
1540 /*
1541 * Should the PRP2 entry be a PRP List pointer or just
1542 * a regular PRP pointer? If there is more than one
1543 * more page of data, must use a PRP List pointer.
1544 */
1545 if (length > ioc->page_size) {
1546 /*
1547 * PRP2 will contain a PRP List pointer because
1548 * more PRP's are needed with this command. The
1549 * list will start at the beginning of the
1550 * contiguous buffer.
1551 */
1552 *prp2_entry =
1553 cpu_to_le64((uintptr_t)prp_entry_phys);
1554
1555 /*
1556 * The next PRP Entry will be the start of the
1557 * first PRP List.
1558 */
1559 prp_entry = prp_page;
1560 } else {
1561 /*
1562 * After this, the PRP Entries are complete.
1563 * This command uses 2 PRP's and no PRP list.
1564 */
1565 *prp2_entry = cpu_to_le64((u64)paddr);
1566 }
1567 } else {
1568 /*
1569 * Put entry in list and bump the addresses.
1570 *
1571 * After PRP1 and PRP2 are filled in, this will fill in
1572 * all remaining PRP entries in a PRP List, one per
1573 * each time through the loop.
1574 */
1575 *prp_entry = cpu_to_le64((u64)paddr);
1576 prp_entry++;
1577 prp_entry_phys++;
1578 }
1579
1580 /*
1581 * Bump the phys address of the command's data buffer by the
1582 * entry_len.
1583 */
1584 paddr += entry_len;
1585
1586 /* Decrement length accounting for last partial page. */
1587 if (entry_len > length)
1588 length = 0;
1589 else
1590 length -= entry_len;
1591 }
1592 }
1593
1594 /**
1595 * base_make_prp_nvme -
1596 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1597 *
1598 * @ioc: per adapter object
1599 * @scmd: SCSI command from the mid-layer
1600 * @mpi_request: mpi request
1601 * @smid: msg Index
1602 * @sge_count: scatter gather element count.
1603 *
1604 * Returns: true: PRPs are built
1605 * false: IEEE SGLs needs to be built
1606 */
1607 void
1608 base_make_prp_nvme(struct MPT3SAS_ADAPTER *ioc,
1609 struct scsi_cmnd *scmd,
1610 Mpi25SCSIIORequest_t *mpi_request,
1611 u16 smid, int sge_count)
1612 {
1613 int sge_len, offset, num_prp_in_chain = 0;
1614 Mpi25IeeeSgeChain64_t *main_chain_element, *ptr_first_sgl;
1615 u64 *curr_buff;
1616 dma_addr_t msg_phys;
1617 u64 sge_addr;
1618 u32 page_mask, page_mask_result;
1619 struct scatterlist *sg_scmd;
1620 u32 first_prp_len;
1621 int data_len = scsi_bufflen(scmd);
1622 u32 nvme_pg_size;
1623
1624 nvme_pg_size = max_t(u32, ioc->page_size, NVME_PRP_PAGE_SIZE);
1625 /*
1626 * Nvme has a very convoluted prp format. One prp is required
1627 * for each page or partial page. Driver need to split up OS sg_list
1628 * entries if it is longer than one page or cross a page
1629 * boundary. Driver also have to insert a PRP list pointer entry as
1630 * the last entry in each physical page of the PRP list.
1631 *
1632 * NOTE: The first PRP "entry" is actually placed in the first
1633 * SGL entry in the main message as IEEE 64 format. The 2nd
1634 * entry in the main message is the chain element, and the rest
1635 * of the PRP entries are built in the contiguous pcie buffer.
1636 */
1637 page_mask = nvme_pg_size - 1;
1638
1639 /*
1640 * Native SGL is needed.
1641 * Put a chain element in main message frame that points to the first
1642 * chain buffer.
1643 *
1644 * NOTE: The ChainOffset field must be 0 when using a chain pointer to
1645 * a native SGL.
1646 */
1647
1648 /* Set main message chain element pointer */
1649 main_chain_element = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
1650 /*
1651 * For NVMe the chain element needs to be the 2nd SG entry in the main
1652 * message.
1653 */
1654 main_chain_element = (Mpi25IeeeSgeChain64_t *)
1655 ((u8 *)main_chain_element + sizeof(MPI25_IEEE_SGE_CHAIN64));
1656
1657 /*
1658 * For the PRP entries, use the specially allocated buffer of
1659 * contiguous memory. Normal chain buffers can't be used
1660 * because each chain buffer would need to be the size of an OS
1661 * page (4k).
1662 */
1663 curr_buff = mpt3sas_base_get_pcie_sgl(ioc, smid);
1664 msg_phys = (dma_addr_t)mpt3sas_base_get_pcie_sgl_dma(ioc, smid);
1665
1666 main_chain_element->Address = cpu_to_le64(msg_phys);
1667 main_chain_element->NextChainOffset = 0;
1668 main_chain_element->Flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1669 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1670 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1671
1672 /* Build first prp, sge need not to be page aligned*/
1673 ptr_first_sgl = (pMpi25IeeeSgeChain64_t)&mpi_request->SGL;
1674 sg_scmd = scsi_sglist(scmd);
1675 sge_addr = sg_dma_address(sg_scmd);
1676 sge_len = sg_dma_len(sg_scmd);
1677
1678 offset = (u32)(sge_addr & page_mask);
1679 first_prp_len = nvme_pg_size - offset;
1680
1681 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1682 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1683
1684 data_len -= first_prp_len;
1685
1686 if (sge_len > first_prp_len) {
1687 sge_addr += first_prp_len;
1688 sge_len -= first_prp_len;
1689 } else if (data_len && (sge_len == first_prp_len)) {
1690 sg_scmd = sg_next(sg_scmd);
1691 sge_addr = sg_dma_address(sg_scmd);
1692 sge_len = sg_dma_len(sg_scmd);
1693 }
1694
1695 for (;;) {
1696 offset = (u32)(sge_addr & page_mask);
1697
1698 /* Put PRP pointer due to page boundary*/
1699 page_mask_result = (uintptr_t)(curr_buff + 1) & page_mask;
1700 if (unlikely(!page_mask_result)) {
1701 scmd_printk(KERN_NOTICE,
1702 scmd, "page boundary curr_buff: 0x%p\n",
1703 curr_buff);
1704 msg_phys += 8;
1705 *curr_buff = cpu_to_le64(msg_phys);
1706 curr_buff++;
1707 num_prp_in_chain++;
1708 }
1709
1710 *curr_buff = cpu_to_le64(sge_addr);
1711 curr_buff++;
1712 msg_phys += 8;
1713 num_prp_in_chain++;
1714
1715 sge_addr += nvme_pg_size;
1716 sge_len -= nvme_pg_size;
1717 data_len -= nvme_pg_size;
1718
1719 if (data_len <= 0)
1720 break;
1721
1722 if (sge_len > 0)
1723 continue;
1724
1725 sg_scmd = sg_next(sg_scmd);
1726 sge_addr = sg_dma_address(sg_scmd);
1727 sge_len = sg_dma_len(sg_scmd);
1728 }
1729
1730 main_chain_element->Length =
1731 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1732 return;
1733 }
1734
1735 static bool
1736 base_is_prp_possible(struct MPT3SAS_ADAPTER *ioc,
1737 struct _pcie_device *pcie_device, struct scsi_cmnd *scmd, int sge_count)
1738 {
1739 u32 data_length = 0;
1740 struct scatterlist *sg_scmd;
1741 bool build_prp = true;
1742
1743 data_length = cpu_to_le32(scsi_bufflen(scmd));
1744 sg_scmd = scsi_sglist(scmd);
1745
1746 /* If Datalenth is <= 16K and number of SGE’s entries are <= 2
1747 * we built IEEE SGL
1748 */
1749 if ((data_length <= NVME_PRP_PAGE_SIZE*4) && (sge_count <= 2))
1750 build_prp = false;
1751
1752 return build_prp;
1753 }
1754
1755 /**
1756 * _base_check_pcie_native_sgl - This function is called for PCIe end devices to
1757 * determine if the driver needs to build a native SGL. If so, that native
1758 * SGL is built in the special contiguous buffers allocated especially for
1759 * PCIe SGL creation. If the driver will not build a native SGL, return
1760 * TRUE and a normal IEEE SGL will be built. Currently this routine
1761 * supports NVMe.
1762 * @ioc: per adapter object
1763 * @mpi_request: mf request pointer
1764 * @smid: system request message index
1765 * @scmd: scsi command
1766 * @pcie_device: points to the PCIe device's info
1767 *
1768 * Returns 0 if native SGL was built, 1 if no SGL was built
1769 */
1770 static int
1771 _base_check_pcie_native_sgl(struct MPT3SAS_ADAPTER *ioc,
1772 Mpi25SCSIIORequest_t *mpi_request, u16 smid, struct scsi_cmnd *scmd,
1773 struct _pcie_device *pcie_device)
1774 {
1775 struct scatterlist *sg_scmd;
1776 int sges_left;
1777
1778 /* Get the SG list pointer and info. */
1779 sg_scmd = scsi_sglist(scmd);
1780 sges_left = scsi_dma_map(scmd);
1781 if (sges_left < 0) {
1782 sdev_printk(KERN_ERR, scmd->device,
1783 "scsi_dma_map failed: request for %d bytes!\n",
1784 scsi_bufflen(scmd));
1785 return 1;
1786 }
1787
1788 /* Check if we need to build a native SG list. */
1789 if (base_is_prp_possible(ioc, pcie_device,
1790 scmd, sges_left) == 0) {
1791 /* We built a native SG list, just return. */
1792 goto out;
1793 }
1794
1795 /*
1796 * Build native NVMe PRP.
1797 */
1798 base_make_prp_nvme(ioc, scmd, mpi_request,
1799 smid, sges_left);
1800
1801 return 0;
1802 out:
1803 scsi_dma_unmap(scmd);
1804 return 1;
1805 }
1806
1807 /**
1808 * _base_add_sg_single_ieee - add sg element for IEEE format
1809 * @paddr: virtual address for SGE
1810 * @flags: SGE flags
1811 * @chain_offset: number of 128 byte elements from start of segment
1812 * @length: data transfer length
1813 * @dma_addr: Physical address
1814 *
1815 * Return nothing.
1816 */
1817 static void
1818 _base_add_sg_single_ieee(void *paddr, u8 flags, u8 chain_offset, u32 length,
1819 dma_addr_t dma_addr)
1820 {
1821 Mpi25IeeeSgeChain64_t *sgel = paddr;
1822
1823 sgel->Flags = flags;
1824 sgel->NextChainOffset = chain_offset;
1825 sgel->Length = cpu_to_le32(length);
1826 sgel->Address = cpu_to_le64(dma_addr);
1827 }
1828
1829 /**
1830 * _base_build_zero_len_sge_ieee - build zero length sg entry for IEEE format
1831 * @ioc: per adapter object
1832 * @paddr: virtual address for SGE
1833 *
1834 * Create a zero length scatter gather entry to insure the IOCs hardware has
1835 * something to use if the target device goes brain dead and tries
1836 * to send data even when none is asked for.
1837 *
1838 * Return nothing.
1839 */
1840 static void
1841 _base_build_zero_len_sge_ieee(struct MPT3SAS_ADAPTER *ioc, void *paddr)
1842 {
1843 u8 sgl_flags = (MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
1844 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR |
1845 MPI25_IEEE_SGE_FLAGS_END_OF_LIST);
1846
1847 _base_add_sg_single_ieee(paddr, sgl_flags, 0, 0, -1);
1848 }
1849
1850 /**
1851 * _base_build_sg_scmd - main sg creation routine
1852 * pcie_device is unused here!
1853 * @ioc: per adapter object
1854 * @scmd: scsi command
1855 * @smid: system request message index
1856 * @unused: unused pcie_device pointer
1857 * Context: none.
1858 *
1859 * The main routine that builds scatter gather table from a given
1860 * scsi request sent via the .queuecommand main handler.
1861 *
1862 * Returns 0 success, anything else error
1863 */
1864 static int
1865 _base_build_sg_scmd(struct MPT3SAS_ADAPTER *ioc,
1866 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *unused)
1867 {
1868 Mpi2SCSIIORequest_t *mpi_request;
1869 dma_addr_t chain_dma;
1870 struct scatterlist *sg_scmd;
1871 void *sg_local, *chain;
1872 u32 chain_offset;
1873 u32 chain_length;
1874 u32 chain_flags;
1875 int sges_left;
1876 u32 sges_in_segment;
1877 u32 sgl_flags;
1878 u32 sgl_flags_last_element;
1879 u32 sgl_flags_end_buffer;
1880 struct chain_tracker *chain_req;
1881
1882 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
1883
1884 /* init scatter gather flags */
1885 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1886 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1887 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1888 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1889 << MPI2_SGE_FLAGS_SHIFT;
1890 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1891 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1892 << MPI2_SGE_FLAGS_SHIFT;
1893 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1894
1895 sg_scmd = scsi_sglist(scmd);
1896 sges_left = scsi_dma_map(scmd);
1897 if (sges_left < 0) {
1898 sdev_printk(KERN_ERR, scmd->device,
1899 "pci_map_sg failed: request for %d bytes!\n",
1900 scsi_bufflen(scmd));
1901 return -ENOMEM;
1902 }
1903
1904 sg_local = &mpi_request->SGL;
1905 sges_in_segment = ioc->max_sges_in_main_message;
1906 if (sges_left <= sges_in_segment)
1907 goto fill_in_last_segment;
1908
1909 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1910 (sges_in_segment * ioc->sge_size))/4;
1911
1912 /* fill in main message segment when there is a chain following */
1913 while (sges_in_segment) {
1914 if (sges_in_segment == 1)
1915 ioc->base_add_sg_single(sg_local,
1916 sgl_flags_last_element | sg_dma_len(sg_scmd),
1917 sg_dma_address(sg_scmd));
1918 else
1919 ioc->base_add_sg_single(sg_local, sgl_flags |
1920 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1921 sg_scmd = sg_next(sg_scmd);
1922 sg_local += ioc->sge_size;
1923 sges_left--;
1924 sges_in_segment--;
1925 }
1926
1927 /* initializing the chain flags and pointers */
1928 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
1929 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1930 if (!chain_req)
1931 return -1;
1932 chain = chain_req->chain_buffer;
1933 chain_dma = chain_req->chain_buffer_dma;
1934 do {
1935 sges_in_segment = (sges_left <=
1936 ioc->max_sges_in_chain_message) ? sges_left :
1937 ioc->max_sges_in_chain_message;
1938 chain_offset = (sges_left == sges_in_segment) ?
1939 0 : (sges_in_segment * ioc->sge_size)/4;
1940 chain_length = sges_in_segment * ioc->sge_size;
1941 if (chain_offset) {
1942 chain_offset = chain_offset <<
1943 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1944 chain_length += ioc->sge_size;
1945 }
1946 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1947 chain_length, chain_dma);
1948 sg_local = chain;
1949 if (!chain_offset)
1950 goto fill_in_last_segment;
1951
1952 /* fill in chain segments */
1953 while (sges_in_segment) {
1954 if (sges_in_segment == 1)
1955 ioc->base_add_sg_single(sg_local,
1956 sgl_flags_last_element |
1957 sg_dma_len(sg_scmd),
1958 sg_dma_address(sg_scmd));
1959 else
1960 ioc->base_add_sg_single(sg_local, sgl_flags |
1961 sg_dma_len(sg_scmd),
1962 sg_dma_address(sg_scmd));
1963 sg_scmd = sg_next(sg_scmd);
1964 sg_local += ioc->sge_size;
1965 sges_left--;
1966 sges_in_segment--;
1967 }
1968
1969 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
1970 if (!chain_req)
1971 return -1;
1972 chain = chain_req->chain_buffer;
1973 chain_dma = chain_req->chain_buffer_dma;
1974 } while (1);
1975
1976
1977 fill_in_last_segment:
1978
1979 /* fill the last segment */
1980 while (sges_left) {
1981 if (sges_left == 1)
1982 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1983 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1984 else
1985 ioc->base_add_sg_single(sg_local, sgl_flags |
1986 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1987 sg_scmd = sg_next(sg_scmd);
1988 sg_local += ioc->sge_size;
1989 sges_left--;
1990 }
1991
1992 return 0;
1993 }
1994
1995 /**
1996 * _base_build_sg_scmd_ieee - main sg creation routine for IEEE format
1997 * @ioc: per adapter object
1998 * @scmd: scsi command
1999 * @smid: system request message index
2000 * @pcie_device: Pointer to pcie_device. If set, the pcie native sgl will be
2001 * constructed on need.
2002 * Context: none.
2003 *
2004 * The main routine that builds scatter gather table from a given
2005 * scsi request sent via the .queuecommand main handler.
2006 *
2007 * Returns 0 success, anything else error
2008 */
2009 static int
2010 _base_build_sg_scmd_ieee(struct MPT3SAS_ADAPTER *ioc,
2011 struct scsi_cmnd *scmd, u16 smid, struct _pcie_device *pcie_device)
2012 {
2013 Mpi25SCSIIORequest_t *mpi_request;
2014 dma_addr_t chain_dma;
2015 struct scatterlist *sg_scmd;
2016 void *sg_local, *chain;
2017 u32 chain_offset;
2018 u32 chain_length;
2019 int sges_left;
2020 u32 sges_in_segment;
2021 u8 simple_sgl_flags;
2022 u8 simple_sgl_flags_last;
2023 u8 chain_sgl_flags;
2024 struct chain_tracker *chain_req;
2025
2026 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2027
2028 /* init scatter gather flags */
2029 simple_sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2030 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2031 simple_sgl_flags_last = simple_sgl_flags |
2032 MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2033 chain_sgl_flags = MPI2_IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2034 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2035
2036 /* Check if we need to build a native SG list. */
2037 if ((pcie_device) && (_base_check_pcie_native_sgl(ioc, mpi_request,
2038 smid, scmd, pcie_device) == 0)) {
2039 /* We built a native SG list, just return. */
2040 return 0;
2041 }
2042
2043 sg_scmd = scsi_sglist(scmd);
2044 sges_left = scsi_dma_map(scmd);
2045 if (sges_left < 0) {
2046 sdev_printk(KERN_ERR, scmd->device,
2047 "pci_map_sg failed: request for %d bytes!\n",
2048 scsi_bufflen(scmd));
2049 return -ENOMEM;
2050 }
2051
2052 sg_local = &mpi_request->SGL;
2053 sges_in_segment = (ioc->request_sz -
2054 offsetof(Mpi25SCSIIORequest_t, SGL))/ioc->sge_size_ieee;
2055 if (sges_left <= sges_in_segment)
2056 goto fill_in_last_segment;
2057
2058 mpi_request->ChainOffset = (sges_in_segment - 1 /* chain element */) +
2059 (offsetof(Mpi25SCSIIORequest_t, SGL)/ioc->sge_size_ieee);
2060
2061 /* fill in main message segment when there is a chain following */
2062 while (sges_in_segment > 1) {
2063 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2064 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2065 sg_scmd = sg_next(sg_scmd);
2066 sg_local += ioc->sge_size_ieee;
2067 sges_left--;
2068 sges_in_segment--;
2069 }
2070
2071 /* initializing the pointers */
2072 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
2073 if (!chain_req)
2074 return -1;
2075 chain = chain_req->chain_buffer;
2076 chain_dma = chain_req->chain_buffer_dma;
2077 do {
2078 sges_in_segment = (sges_left <=
2079 ioc->max_sges_in_chain_message) ? sges_left :
2080 ioc->max_sges_in_chain_message;
2081 chain_offset = (sges_left == sges_in_segment) ?
2082 0 : sges_in_segment;
2083 chain_length = sges_in_segment * ioc->sge_size_ieee;
2084 if (chain_offset)
2085 chain_length += ioc->sge_size_ieee;
2086 _base_add_sg_single_ieee(sg_local, chain_sgl_flags,
2087 chain_offset, chain_length, chain_dma);
2088
2089 sg_local = chain;
2090 if (!chain_offset)
2091 goto fill_in_last_segment;
2092
2093 /* fill in chain segments */
2094 while (sges_in_segment) {
2095 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2096 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2097 sg_scmd = sg_next(sg_scmd);
2098 sg_local += ioc->sge_size_ieee;
2099 sges_left--;
2100 sges_in_segment--;
2101 }
2102
2103 chain_req = _base_get_chain_buffer_tracker(ioc, smid);
2104 if (!chain_req)
2105 return -1;
2106 chain = chain_req->chain_buffer;
2107 chain_dma = chain_req->chain_buffer_dma;
2108 } while (1);
2109
2110
2111 fill_in_last_segment:
2112
2113 /* fill the last segment */
2114 while (sges_left > 0) {
2115 if (sges_left == 1)
2116 _base_add_sg_single_ieee(sg_local,
2117 simple_sgl_flags_last, 0, sg_dma_len(sg_scmd),
2118 sg_dma_address(sg_scmd));
2119 else
2120 _base_add_sg_single_ieee(sg_local, simple_sgl_flags, 0,
2121 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
2122 sg_scmd = sg_next(sg_scmd);
2123 sg_local += ioc->sge_size_ieee;
2124 sges_left--;
2125 }
2126
2127 return 0;
2128 }
2129
2130 /**
2131 * _base_build_sg_ieee - build generic sg for IEEE format
2132 * @ioc: per adapter object
2133 * @psge: virtual address for SGE
2134 * @data_out_dma: physical address for WRITES
2135 * @data_out_sz: data xfer size for WRITES
2136 * @data_in_dma: physical address for READS
2137 * @data_in_sz: data xfer size for READS
2138 *
2139 * Return nothing.
2140 */
2141 static void
2142 _base_build_sg_ieee(struct MPT3SAS_ADAPTER *ioc, void *psge,
2143 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
2144 size_t data_in_sz)
2145 {
2146 u8 sgl_flags;
2147
2148 if (!data_out_sz && !data_in_sz) {
2149 _base_build_zero_len_sge_ieee(ioc, psge);
2150 return;
2151 }
2152
2153 if (data_out_sz && data_in_sz) {
2154 /* WRITE sgel first */
2155 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2156 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2157 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2158 data_out_dma);
2159
2160 /* incr sgel */
2161 psge += ioc->sge_size_ieee;
2162
2163 /* READ sgel last */
2164 sgl_flags |= MPI25_IEEE_SGE_FLAGS_END_OF_LIST;
2165 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2166 data_in_dma);
2167 } else if (data_out_sz) /* WRITE */ {
2168 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2169 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2170 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2171 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_out_sz,
2172 data_out_dma);
2173 } else if (data_in_sz) /* READ */ {
2174 sgl_flags = MPI2_IEEE_SGE_FLAGS_SIMPLE_ELEMENT |
2175 MPI25_IEEE_SGE_FLAGS_END_OF_LIST |
2176 MPI2_IEEE_SGE_FLAGS_SYSTEM_ADDR;
2177 _base_add_sg_single_ieee(psge, sgl_flags, 0, data_in_sz,
2178 data_in_dma);
2179 }
2180 }
2181
2182 #define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
2183
2184 /**
2185 * _base_config_dma_addressing - set dma addressing
2186 * @ioc: per adapter object
2187 * @pdev: PCI device struct
2188 *
2189 * Returns 0 for success, non-zero for failure.
2190 */
2191 static int
2192 _base_config_dma_addressing(struct MPT3SAS_ADAPTER *ioc, struct pci_dev *pdev)
2193 {
2194 struct sysinfo s;
2195 u64 consistent_dma_mask;
2196
2197 if (ioc->dma_mask)
2198 consistent_dma_mask = DMA_BIT_MASK(64);
2199 else
2200 consistent_dma_mask = DMA_BIT_MASK(32);
2201
2202 if (sizeof(dma_addr_t) > 4) {
2203 const uint64_t required_mask =
2204 dma_get_required_mask(&pdev->dev);
2205 if ((required_mask > DMA_BIT_MASK(32)) &&
2206 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) &&
2207 !pci_set_consistent_dma_mask(pdev, consistent_dma_mask)) {
2208 ioc->base_add_sg_single = &_base_add_sg_single_64;
2209 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
2210 ioc->dma_mask = 64;
2211 goto out;
2212 }
2213 }
2214
2215 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
2216 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
2217 ioc->base_add_sg_single = &_base_add_sg_single_32;
2218 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
2219 ioc->dma_mask = 32;
2220 } else
2221 return -ENODEV;
2222
2223 out:
2224 si_meminfo(&s);
2225 pr_info(MPT3SAS_FMT
2226 "%d BIT PCI BUS DMA ADDRESSING SUPPORTED, total mem (%ld kB)\n",
2227 ioc->name, ioc->dma_mask, convert_to_kb(s.totalram));
2228
2229 return 0;
2230 }
2231
2232 static int
2233 _base_change_consistent_dma_mask(struct MPT3SAS_ADAPTER *ioc,
2234 struct pci_dev *pdev)
2235 {
2236 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) {
2237 if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
2238 return -ENODEV;
2239 }
2240 return 0;
2241 }
2242
2243 /**
2244 * _base_check_enable_msix - checks MSIX capabable.
2245 * @ioc: per adapter object
2246 *
2247 * Check to see if card is capable of MSIX, and set number
2248 * of available msix vectors
2249 */
2250 static int
2251 _base_check_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2252 {
2253 int base;
2254 u16 message_control;
2255
2256 /* Check whether controller SAS2008 B0 controller,
2257 * if it is SAS2008 B0 controller use IO-APIC instead of MSIX
2258 */
2259 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
2260 ioc->pdev->revision == SAS2_PCI_DEVICE_B0_REVISION) {
2261 return -EINVAL;
2262 }
2263
2264 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
2265 if (!base) {
2266 dfailprintk(ioc, pr_info(MPT3SAS_FMT "msix not supported\n",
2267 ioc->name));
2268 return -EINVAL;
2269 }
2270
2271 /* get msix vector count */
2272 /* NUMA_IO not supported for older controllers */
2273 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
2274 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
2275 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
2276 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
2277 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
2278 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
2279 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
2280 ioc->msix_vector_count = 1;
2281 else {
2282 pci_read_config_word(ioc->pdev, base + 2, &message_control);
2283 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
2284 }
2285 dinitprintk(ioc, pr_info(MPT3SAS_FMT
2286 "msix is supported, vector_count(%d)\n",
2287 ioc->name, ioc->msix_vector_count));
2288 return 0;
2289 }
2290
2291 /**
2292 * _base_free_irq - free irq
2293 * @ioc: per adapter object
2294 *
2295 * Freeing respective reply_queue from the list.
2296 */
2297 static void
2298 _base_free_irq(struct MPT3SAS_ADAPTER *ioc)
2299 {
2300 struct adapter_reply_queue *reply_q, *next;
2301
2302 if (list_empty(&ioc->reply_queue_list))
2303 return;
2304
2305 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
2306 list_del(&reply_q->list);
2307 free_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index),
2308 reply_q);
2309 kfree(reply_q);
2310 }
2311 }
2312
2313 /**
2314 * _base_request_irq - request irq
2315 * @ioc: per adapter object
2316 * @index: msix index into vector table
2317 *
2318 * Inserting respective reply_queue into the list.
2319 */
2320 static int
2321 _base_request_irq(struct MPT3SAS_ADAPTER *ioc, u8 index)
2322 {
2323 struct pci_dev *pdev = ioc->pdev;
2324 struct adapter_reply_queue *reply_q;
2325 int r;
2326
2327 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
2328 if (!reply_q) {
2329 pr_err(MPT3SAS_FMT "unable to allocate memory %d!\n",
2330 ioc->name, (int)sizeof(struct adapter_reply_queue));
2331 return -ENOMEM;
2332 }
2333 reply_q->ioc = ioc;
2334 reply_q->msix_index = index;
2335
2336 atomic_set(&reply_q->busy, 0);
2337 if (ioc->msix_enable)
2338 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
2339 ioc->driver_name, ioc->id, index);
2340 else
2341 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
2342 ioc->driver_name, ioc->id);
2343 r = request_irq(pci_irq_vector(pdev, index), _base_interrupt,
2344 IRQF_SHARED, reply_q->name, reply_q);
2345 if (r) {
2346 pr_err(MPT3SAS_FMT "unable to allocate interrupt %d!\n",
2347 reply_q->name, pci_irq_vector(pdev, index));
2348 kfree(reply_q);
2349 return -EBUSY;
2350 }
2351
2352 INIT_LIST_HEAD(&reply_q->list);
2353 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
2354 return 0;
2355 }
2356
2357 /**
2358 * _base_assign_reply_queues - assigning msix index for each cpu
2359 * @ioc: per adapter object
2360 *
2361 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
2362 *
2363 * It would nice if we could call irq_set_affinity, however it is not
2364 * an exported symbol
2365 */
2366 static void
2367 _base_assign_reply_queues(struct MPT3SAS_ADAPTER *ioc)
2368 {
2369 unsigned int cpu, nr_cpus, nr_msix, index = 0;
2370 struct adapter_reply_queue *reply_q;
2371
2372 if (!_base_is_controller_msix_enabled(ioc))
2373 return;
2374
2375 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
2376
2377 nr_cpus = num_online_cpus();
2378 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
2379 ioc->facts.MaxMSIxVectors);
2380 if (!nr_msix)
2381 return;
2382
2383 if (smp_affinity_enable) {
2384 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2385 const cpumask_t *mask = pci_irq_get_affinity(ioc->pdev,
2386 reply_q->msix_index);
2387 if (!mask) {
2388 pr_warn(MPT3SAS_FMT "no affinity for msi %x\n",
2389 ioc->name, reply_q->msix_index);
2390 continue;
2391 }
2392
2393 for_each_cpu(cpu, mask)
2394 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
2395 }
2396 return;
2397 }
2398 cpu = cpumask_first(cpu_online_mask);
2399
2400 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
2401
2402 unsigned int i, group = nr_cpus / nr_msix;
2403
2404 if (cpu >= nr_cpus)
2405 break;
2406
2407 if (index < nr_cpus % nr_msix)
2408 group++;
2409
2410 for (i = 0 ; i < group ; i++) {
2411 ioc->cpu_msix_table[cpu] = reply_q->msix_index;
2412 cpu = cpumask_next(cpu, cpu_online_mask);
2413 }
2414 index++;
2415 }
2416 }
2417
2418 /**
2419 * _base_disable_msix - disables msix
2420 * @ioc: per adapter object
2421 *
2422 */
2423 static void
2424 _base_disable_msix(struct MPT3SAS_ADAPTER *ioc)
2425 {
2426 if (!ioc->msix_enable)
2427 return;
2428 pci_disable_msix(ioc->pdev);
2429 ioc->msix_enable = 0;
2430 }
2431
2432 /**
2433 * _base_enable_msix - enables msix, failback to io_apic
2434 * @ioc: per adapter object
2435 *
2436 */
2437 static int
2438 _base_enable_msix(struct MPT3SAS_ADAPTER *ioc)
2439 {
2440 int r;
2441 int i, local_max_msix_vectors;
2442 u8 try_msix = 0;
2443 unsigned int irq_flags = PCI_IRQ_MSIX;
2444
2445 if (msix_disable == -1 || msix_disable == 0)
2446 try_msix = 1;
2447
2448 if (!try_msix)
2449 goto try_ioapic;
2450
2451 if (_base_check_enable_msix(ioc) != 0)
2452 goto try_ioapic;
2453
2454 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
2455 ioc->msix_vector_count);
2456
2457 printk(MPT3SAS_FMT "MSI-X vectors supported: %d, no of cores"
2458 ": %d, max_msix_vectors: %d\n", ioc->name, ioc->msix_vector_count,
2459 ioc->cpu_count, max_msix_vectors);
2460
2461 if (!ioc->rdpq_array_enable && max_msix_vectors == -1)
2462 local_max_msix_vectors = (reset_devices) ? 1 : 8;
2463 else
2464 local_max_msix_vectors = max_msix_vectors;
2465
2466 if (local_max_msix_vectors > 0)
2467 ioc->reply_queue_count = min_t(int, local_max_msix_vectors,
2468 ioc->reply_queue_count);
2469 else if (local_max_msix_vectors == 0)
2470 goto try_ioapic;
2471
2472 if (ioc->msix_vector_count < ioc->cpu_count)
2473 smp_affinity_enable = 0;
2474
2475 if (smp_affinity_enable)
2476 irq_flags |= PCI_IRQ_AFFINITY;
2477
2478 r = pci_alloc_irq_vectors(ioc->pdev, 1, ioc->reply_queue_count,
2479 irq_flags);
2480 if (r < 0) {
2481 dfailprintk(ioc, pr_info(MPT3SAS_FMT
2482 "pci_alloc_irq_vectors failed (r=%d) !!!\n",
2483 ioc->name, r));
2484 goto try_ioapic;
2485 }
2486
2487 ioc->msix_enable = 1;
2488 ioc->reply_queue_count = r;
2489 for (i = 0; i < ioc->reply_queue_count; i++) {
2490 r = _base_request_irq(ioc, i);
2491 if (r) {
2492 _base_free_irq(ioc);
2493 _base_disable_msix(ioc);
2494 goto try_ioapic;
2495 }
2496 }
2497
2498 return 0;
2499
2500 /* failback to io_apic interrupt routing */
2501 try_ioapic:
2502
2503 ioc->reply_queue_count = 1;
2504 r = pci_alloc_irq_vectors(ioc->pdev, 1, 1, PCI_IRQ_LEGACY);
2505 if (r < 0) {
2506 dfailprintk(ioc, pr_info(MPT3SAS_FMT
2507 "pci_alloc_irq_vector(legacy) failed (r=%d) !!!\n",
2508 ioc->name, r));
2509 } else
2510 r = _base_request_irq(ioc, 0);
2511
2512 return r;
2513 }
2514
2515 /**
2516 * mpt3sas_base_unmap_resources - free controller resources
2517 * @ioc: per adapter object
2518 */
2519 static void
2520 mpt3sas_base_unmap_resources(struct MPT3SAS_ADAPTER *ioc)
2521 {
2522 struct pci_dev *pdev = ioc->pdev;
2523
2524 dexitprintk(ioc, printk(MPT3SAS_FMT "%s\n",
2525 ioc->name, __func__));
2526
2527 _base_free_irq(ioc);
2528 _base_disable_msix(ioc);
2529
2530 if (ioc->combined_reply_queue) {
2531 kfree(ioc->replyPostRegisterIndex);
2532 ioc->replyPostRegisterIndex = NULL;
2533 }
2534
2535 if (ioc->chip_phys) {
2536 iounmap(ioc->chip);
2537 ioc->chip_phys = 0;
2538 }
2539
2540 if (pci_is_enabled(pdev)) {
2541 pci_release_selected_regions(ioc->pdev, ioc->bars);
2542 pci_disable_pcie_error_reporting(pdev);
2543 pci_disable_device(pdev);
2544 }
2545 }
2546
2547 /**
2548 * mpt3sas_base_map_resources - map in controller resources (io/irq/memap)
2549 * @ioc: per adapter object
2550 *
2551 * Returns 0 for success, non-zero for failure.
2552 */
2553 int
2554 mpt3sas_base_map_resources(struct MPT3SAS_ADAPTER *ioc)
2555 {
2556 struct pci_dev *pdev = ioc->pdev;
2557 u32 memap_sz;
2558 u32 pio_sz;
2559 int i, r = 0;
2560 u64 pio_chip = 0;
2561 u64 chip_phys = 0;
2562 struct adapter_reply_queue *reply_q;
2563
2564 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n",
2565 ioc->name, __func__));
2566
2567 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
2568 if (pci_enable_device_mem(pdev)) {
2569 pr_warn(MPT3SAS_FMT "pci_enable_device_mem: failed\n",
2570 ioc->name);
2571 ioc->bars = 0;
2572 return -ENODEV;
2573 }
2574
2575
2576 if (pci_request_selected_regions(pdev, ioc->bars,
2577 ioc->driver_name)) {
2578 pr_warn(MPT3SAS_FMT "pci_request_selected_regions: failed\n",
2579 ioc->name);
2580 ioc->bars = 0;
2581 r = -ENODEV;
2582 goto out_fail;
2583 }
2584
2585 /* AER (Advanced Error Reporting) hooks */
2586 pci_enable_pcie_error_reporting(pdev);
2587
2588 pci_set_master(pdev);
2589
2590
2591 if (_base_config_dma_addressing(ioc, pdev) != 0) {
2592 pr_warn(MPT3SAS_FMT "no suitable DMA mask for %s\n",
2593 ioc->name, pci_name(pdev));
2594 r = -ENODEV;
2595 goto out_fail;
2596 }
2597
2598 for (i = 0, memap_sz = 0, pio_sz = 0; (i < DEVICE_COUNT_RESOURCE) &&
2599 (!memap_sz || !pio_sz); i++) {
2600 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
2601 if (pio_sz)
2602 continue;
2603 pio_chip = (u64)pci_resource_start(pdev, i);
2604 pio_sz = pci_resource_len(pdev, i);
2605 } else if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
2606 if (memap_sz)
2607 continue;
2608 ioc->chip_phys = pci_resource_start(pdev, i);
2609 chip_phys = (u64)ioc->chip_phys;
2610 memap_sz = pci_resource_len(pdev, i);
2611 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
2612 }
2613 }
2614
2615 if (ioc->chip == NULL) {
2616 pr_err(MPT3SAS_FMT "unable to map adapter memory! "
2617 " or resource not found\n", ioc->name);
2618 r = -EINVAL;
2619 goto out_fail;
2620 }
2621
2622 _base_mask_interrupts(ioc);
2623
2624 r = _base_get_ioc_facts(ioc);
2625 if (r)
2626 goto out_fail;
2627
2628 if (!ioc->rdpq_array_enable_assigned) {
2629 ioc->rdpq_array_enable = ioc->rdpq_array_capable;
2630 ioc->rdpq_array_enable_assigned = 1;
2631 }
2632
2633 r = _base_enable_msix(ioc);
2634 if (r)
2635 goto out_fail;
2636
2637 /* Use the Combined reply queue feature only for SAS3 C0 & higher
2638 * revision HBAs and also only when reply queue count is greater than 8
2639 */
2640 if (ioc->combined_reply_queue && ioc->reply_queue_count > 8) {
2641 /* Determine the Supplemental Reply Post Host Index Registers
2642 * Addresse. Supplemental Reply Post Host Index Registers
2643 * starts at offset MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET and
2644 * each register is at offset bytes of
2645 * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET from previous one.
2646 */
2647 ioc->replyPostRegisterIndex = kcalloc(
2648 ioc->combined_reply_index_count,
2649 sizeof(resource_size_t *), GFP_KERNEL);
2650 if (!ioc->replyPostRegisterIndex) {
2651 dfailprintk(ioc, printk(MPT3SAS_FMT
2652 "allocation for reply Post Register Index failed!!!\n",
2653 ioc->name));
2654 r = -ENOMEM;
2655 goto out_fail;
2656 }
2657
2658 for (i = 0; i < ioc->combined_reply_index_count; i++) {
2659 ioc->replyPostRegisterIndex[i] = (resource_size_t *)
2660 ((u8 *)&ioc->chip->Doorbell +
2661 MPI25_SUP_REPLY_POST_HOST_INDEX_OFFSET +
2662 (i * MPT3_SUP_REPLY_POST_HOST_INDEX_REG_OFFSET));
2663 }
2664 } else
2665 ioc->combined_reply_queue = 0;
2666
2667 if (ioc->is_warpdrive) {
2668 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
2669 &ioc->chip->ReplyPostHostIndex;
2670
2671 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
2672 ioc->reply_post_host_index[i] =
2673 (resource_size_t __iomem *)
2674 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
2675 * 4)));
2676 }
2677
2678 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
2679 pr_info(MPT3SAS_FMT "%s: IRQ %d\n",
2680 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
2681 "IO-APIC enabled"),
2682 pci_irq_vector(ioc->pdev, reply_q->msix_index));
2683
2684 pr_info(MPT3SAS_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
2685 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
2686 pr_info(MPT3SAS_FMT "ioport(0x%016llx), size(%d)\n",
2687 ioc->name, (unsigned long long)pio_chip, pio_sz);
2688
2689 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
2690 pci_save_state(pdev);
2691 return 0;
2692
2693 out_fail:
2694 mpt3sas_base_unmap_resources(ioc);
2695 return r;
2696 }
2697
2698 /**
2699 * mpt3sas_base_get_msg_frame - obtain request mf pointer
2700 * @ioc: per adapter object
2701 * @smid: system request message index(smid zero is invalid)
2702 *
2703 * Returns virt pointer to message frame.
2704 */
2705 void *
2706 mpt3sas_base_get_msg_frame(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2707 {
2708 return (void *)(ioc->request + (smid * ioc->request_sz));
2709 }
2710
2711 /**
2712 * mpt3sas_base_get_sense_buffer - obtain a sense buffer virt addr
2713 * @ioc: per adapter object
2714 * @smid: system request message index
2715 *
2716 * Returns virt pointer to sense buffer.
2717 */
2718 void *
2719 mpt3sas_base_get_sense_buffer(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2720 {
2721 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
2722 }
2723
2724 /**
2725 * mpt3sas_base_get_sense_buffer_dma - obtain a sense buffer dma addr
2726 * @ioc: per adapter object
2727 * @smid: system request message index
2728 *
2729 * Returns phys pointer to the low 32bit address of the sense buffer.
2730 */
2731 __le32
2732 mpt3sas_base_get_sense_buffer_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2733 {
2734 return cpu_to_le32(ioc->sense_dma + ((smid - 1) *
2735 SCSI_SENSE_BUFFERSIZE));
2736 }
2737
2738 /**
2739 * mpt3sas_base_get_pcie_sgl - obtain a PCIe SGL virt addr
2740 * @ioc: per adapter object
2741 * @smid: system request message index
2742 *
2743 * Returns virt pointer to a PCIe SGL.
2744 */
2745 void *
2746 mpt3sas_base_get_pcie_sgl(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2747 {
2748 return (void *)(ioc->scsi_lookup[smid - 1].pcie_sg_list.pcie_sgl);
2749 }
2750
2751 /**
2752 * mpt3sas_base_get_pcie_sgl_dma - obtain a PCIe SGL dma addr
2753 * @ioc: per adapter object
2754 * @smid: system request message index
2755 *
2756 * Returns phys pointer to the address of the PCIe buffer.
2757 */
2758 void *
2759 mpt3sas_base_get_pcie_sgl_dma(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2760 {
2761 return (void *)(uintptr_t)
2762 (ioc->scsi_lookup[smid - 1].pcie_sg_list.pcie_sgl_dma);
2763 }
2764
2765 /**
2766 * mpt3sas_base_get_reply_virt_addr - obtain reply frames virt address
2767 * @ioc: per adapter object
2768 * @phys_addr: lower 32 physical addr of the reply
2769 *
2770 * Converts 32bit lower physical addr into a virt address.
2771 */
2772 void *
2773 mpt3sas_base_get_reply_virt_addr(struct MPT3SAS_ADAPTER *ioc, u32 phys_addr)
2774 {
2775 if (!phys_addr)
2776 return NULL;
2777 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
2778 }
2779
2780 static inline u8
2781 _base_get_msix_index(struct MPT3SAS_ADAPTER *ioc)
2782 {
2783 return ioc->cpu_msix_table[raw_smp_processor_id()];
2784 }
2785
2786 /**
2787 * mpt3sas_base_get_smid - obtain a free smid from internal queue
2788 * @ioc: per adapter object
2789 * @cb_idx: callback index
2790 *
2791 * Returns smid (zero is invalid)
2792 */
2793 u16
2794 mpt3sas_base_get_smid(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2795 {
2796 unsigned long flags;
2797 struct request_tracker *request;
2798 u16 smid;
2799
2800 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2801 if (list_empty(&ioc->internal_free_list)) {
2802 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2803 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2804 ioc->name, __func__);
2805 return 0;
2806 }
2807
2808 request = list_entry(ioc->internal_free_list.next,
2809 struct request_tracker, tracker_list);
2810 request->cb_idx = cb_idx;
2811 smid = request->smid;
2812 list_del(&request->tracker_list);
2813 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2814 return smid;
2815 }
2816
2817 /**
2818 * mpt3sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
2819 * @ioc: per adapter object
2820 * @cb_idx: callback index
2821 * @scmd: pointer to scsi command object
2822 *
2823 * Returns smid (zero is invalid)
2824 */
2825 u16
2826 mpt3sas_base_get_smid_scsiio(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx,
2827 struct scsi_cmnd *scmd)
2828 {
2829 unsigned long flags;
2830 struct scsiio_tracker *request;
2831 u16 smid;
2832
2833 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2834 if (list_empty(&ioc->free_list)) {
2835 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2836 pr_err(MPT3SAS_FMT "%s: smid not available\n",
2837 ioc->name, __func__);
2838 return 0;
2839 }
2840
2841 request = list_entry(ioc->free_list.next,
2842 struct scsiio_tracker, tracker_list);
2843 request->scmd = scmd;
2844 request->cb_idx = cb_idx;
2845 smid = request->smid;
2846 request->msix_io = _base_get_msix_index(ioc);
2847 list_del(&request->tracker_list);
2848 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2849 return smid;
2850 }
2851
2852 /**
2853 * mpt3sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
2854 * @ioc: per adapter object
2855 * @cb_idx: callback index
2856 *
2857 * Returns smid (zero is invalid)
2858 */
2859 u16
2860 mpt3sas_base_get_smid_hpr(struct MPT3SAS_ADAPTER *ioc, u8 cb_idx)
2861 {
2862 unsigned long flags;
2863 struct request_tracker *request;
2864 u16 smid;
2865
2866 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2867 if (list_empty(&ioc->hpr_free_list)) {
2868 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2869 return 0;
2870 }
2871
2872 request = list_entry(ioc->hpr_free_list.next,
2873 struct request_tracker, tracker_list);
2874 request->cb_idx = cb_idx;
2875 smid = request->smid;
2876 list_del(&request->tracker_list);
2877 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2878 return smid;
2879 }
2880
2881 /**
2882 * mpt3sas_base_free_smid - put smid back on free_list
2883 * @ioc: per adapter object
2884 * @smid: system request message index
2885 *
2886 * Return nothing.
2887 */
2888 void
2889 mpt3sas_base_free_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
2890 {
2891 unsigned long flags;
2892 int i;
2893 struct chain_tracker *chain_req, *next;
2894
2895 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
2896 if (smid < ioc->hi_priority_smid) {
2897 /* scsiio queue */
2898 i = smid - 1;
2899 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
2900 list_for_each_entry_safe(chain_req, next,
2901 &ioc->scsi_lookup[i].chain_list, tracker_list) {
2902 list_del_init(&chain_req->tracker_list);
2903 list_add(&chain_req->tracker_list,
2904 &ioc->free_chain_list);
2905 }
2906 }
2907 ioc->scsi_lookup[i].cb_idx = 0xFF;
2908 ioc->scsi_lookup[i].scmd = NULL;
2909 ioc->scsi_lookup[i].direct_io = 0;
2910 list_add(&ioc->scsi_lookup[i].tracker_list, &ioc->free_list);
2911 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2912
2913 /*
2914 * See _wait_for_commands_to_complete() call with regards
2915 * to this code.
2916 */
2917 if (ioc->shost_recovery && ioc->pending_io_count) {
2918 if (ioc->pending_io_count == 1)
2919 wake_up(&ioc->reset_wq);
2920 ioc->pending_io_count--;
2921 }
2922 return;
2923 } else if (smid < ioc->internal_smid) {
2924 /* hi-priority */
2925 i = smid - ioc->hi_priority_smid;
2926 ioc->hpr_lookup[i].cb_idx = 0xFF;
2927 list_add(&ioc->hpr_lookup[i].tracker_list, &ioc->hpr_free_list);
2928 } else if (smid <= ioc->hba_queue_depth) {
2929 /* internal queue */
2930 i = smid - ioc->internal_smid;
2931 ioc->internal_lookup[i].cb_idx = 0xFF;
2932 list_add(&ioc->internal_lookup[i].tracker_list,
2933 &ioc->internal_free_list);
2934 }
2935 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
2936 }
2937
2938 /**
2939 * _base_writeq - 64 bit write to MMIO
2940 * @ioc: per adapter object
2941 * @b: data payload
2942 * @addr: address in MMIO space
2943 * @writeq_lock: spin lock
2944 *
2945 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
2946 * care of 32 bit environment where its not quarenteed to send the entire word
2947 * in one transfer.
2948 */
2949 #if defined(writeq) && defined(CONFIG_64BIT)
2950 static inline void
2951 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2952 {
2953 writeq(cpu_to_le64(b), addr);
2954 }
2955 #else
2956 static inline void
2957 _base_writeq(__u64 b, volatile void __iomem *addr, spinlock_t *writeq_lock)
2958 {
2959 unsigned long flags;
2960 __u64 data_out = cpu_to_le64(b);
2961
2962 spin_lock_irqsave(writeq_lock, flags);
2963 writel((u32)(data_out), addr);
2964 writel((u32)(data_out >> 32), (addr + 4));
2965 spin_unlock_irqrestore(writeq_lock, flags);
2966 }
2967 #endif
2968
2969 /**
2970 * _base_put_smid_scsi_io - send SCSI_IO request to firmware
2971 * @ioc: per adapter object
2972 * @smid: system request message index
2973 * @handle: device handle
2974 *
2975 * Return nothing.
2976 */
2977 static void
2978 _base_put_smid_scsi_io(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 handle)
2979 {
2980 Mpi2RequestDescriptorUnion_t descriptor;
2981 u64 *request = (u64 *)&descriptor;
2982
2983
2984 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
2985 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
2986 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
2987 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
2988 descriptor.SCSIIO.LMID = 0;
2989 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
2990 &ioc->scsi_lookup_lock);
2991 }
2992
2993 /**
2994 * _base_put_smid_fast_path - send fast path request to firmware
2995 * @ioc: per adapter object
2996 * @smid: system request message index
2997 * @handle: device handle
2998 *
2999 * Return nothing.
3000 */
3001 static void
3002 _base_put_smid_fast_path(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3003 u16 handle)
3004 {
3005 Mpi2RequestDescriptorUnion_t descriptor;
3006 u64 *request = (u64 *)&descriptor;
3007
3008 descriptor.SCSIIO.RequestFlags =
3009 MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3010 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
3011 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
3012 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
3013 descriptor.SCSIIO.LMID = 0;
3014 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3015 &ioc->scsi_lookup_lock);
3016 }
3017
3018 /**
3019 * _base_put_smid_hi_priority - send Task Management request to firmware
3020 * @ioc: per adapter object
3021 * @smid: system request message index
3022 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0.
3023 * Return nothing.
3024 */
3025 static void
3026 _base_put_smid_hi_priority(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3027 u16 msix_task)
3028 {
3029 Mpi2RequestDescriptorUnion_t descriptor;
3030 u64 *request = (u64 *)&descriptor;
3031
3032 descriptor.HighPriority.RequestFlags =
3033 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3034 descriptor.HighPriority.MSIxIndex = msix_task;
3035 descriptor.HighPriority.SMID = cpu_to_le16(smid);
3036 descriptor.HighPriority.LMID = 0;
3037 descriptor.HighPriority.Reserved1 = 0;
3038 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3039 &ioc->scsi_lookup_lock);
3040 }
3041
3042 /**
3043 * _base_put_smid_nvme_encap - send NVMe encapsulated request to
3044 * firmware
3045 * @ioc: per adapter object
3046 * @smid: system request message index
3047 *
3048 * Return nothing.
3049 */
3050 static void
3051 _base_put_smid_nvme_encap(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3052 {
3053 Mpi2RequestDescriptorUnion_t descriptor;
3054 u64 *request = (u64 *)&descriptor;
3055
3056 descriptor.Default.RequestFlags =
3057 MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3058 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3059 descriptor.Default.SMID = cpu_to_le16(smid);
3060 descriptor.Default.LMID = 0;
3061 descriptor.Default.DescriptorTypeDependent = 0;
3062 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3063 &ioc->scsi_lookup_lock);
3064 }
3065
3066 /**
3067 * _base_put_smid_default - Default, primarily used for config pages
3068 * @ioc: per adapter object
3069 * @smid: system request message index
3070 *
3071 * Return nothing.
3072 */
3073 static void
3074 _base_put_smid_default(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3075 {
3076 Mpi2RequestDescriptorUnion_t descriptor;
3077 u64 *request = (u64 *)&descriptor;
3078
3079 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3080 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
3081 descriptor.Default.SMID = cpu_to_le16(smid);
3082 descriptor.Default.LMID = 0;
3083 descriptor.Default.DescriptorTypeDependent = 0;
3084 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
3085 &ioc->scsi_lookup_lock);
3086 }
3087
3088 /**
3089 * _base_put_smid_scsi_io_atomic - send SCSI_IO request to firmware using
3090 * Atomic Request Descriptor
3091 * @ioc: per adapter object
3092 * @smid: system request message index
3093 * @handle: device handle, unused in this function, for function type match
3094 *
3095 * Return nothing.
3096 */
3097 static void
3098 _base_put_smid_scsi_io_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3099 u16 handle)
3100 {
3101 Mpi26AtomicRequestDescriptor_t descriptor;
3102 u32 *request = (u32 *)&descriptor;
3103
3104 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
3105 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3106 descriptor.SMID = cpu_to_le16(smid);
3107
3108 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3109 }
3110
3111 /**
3112 * _base_put_smid_fast_path_atomic - send fast path request to firmware
3113 * using Atomic Request Descriptor
3114 * @ioc: per adapter object
3115 * @smid: system request message index
3116 * @handle: device handle, unused in this function, for function type match
3117 * Return nothing
3118 */
3119 static void
3120 _base_put_smid_fast_path_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3121 u16 handle)
3122 {
3123 Mpi26AtomicRequestDescriptor_t descriptor;
3124 u32 *request = (u32 *)&descriptor;
3125
3126 descriptor.RequestFlags = MPI25_REQ_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO;
3127 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3128 descriptor.SMID = cpu_to_le16(smid);
3129
3130 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3131 }
3132
3133 /**
3134 * _base_put_smid_hi_priority_atomic - send Task Management request to
3135 * firmware using Atomic Request Descriptor
3136 * @ioc: per adapter object
3137 * @smid: system request message index
3138 * @msix_task: msix_task will be same as msix of IO incase of task abort else 0
3139 *
3140 * Return nothing.
3141 */
3142 static void
3143 _base_put_smid_hi_priority_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3144 u16 msix_task)
3145 {
3146 Mpi26AtomicRequestDescriptor_t descriptor;
3147 u32 *request = (u32 *)&descriptor;
3148
3149 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3150 descriptor.MSIxIndex = msix_task;
3151 descriptor.SMID = cpu_to_le16(smid);
3152
3153 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3154 }
3155
3156 /**
3157 * _base_put_smid_nvme_encap_atomic - send NVMe encapsulated request to
3158 * firmware using Atomic Request Descriptor
3159 * @ioc: per adapter object
3160 * @smid: system request message index
3161 *
3162 * Return nothing.
3163 */
3164 static void
3165 _base_put_smid_nvme_encap_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3166 {
3167 Mpi26AtomicRequestDescriptor_t descriptor;
3168 u32 *request = (u32 *)&descriptor;
3169
3170 descriptor.RequestFlags = MPI26_REQ_DESCRIPT_FLAGS_PCIE_ENCAPSULATED;
3171 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3172 descriptor.SMID = cpu_to_le16(smid);
3173
3174 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3175 }
3176
3177 /**
3178 * _base_put_smid_default - Default, primarily used for config pages
3179 * use Atomic Request Descriptor
3180 * @ioc: per adapter object
3181 * @smid: system request message index
3182 *
3183 * Return nothing.
3184 */
3185 static void
3186 _base_put_smid_default_atomic(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3187 {
3188 Mpi26AtomicRequestDescriptor_t descriptor;
3189 u32 *request = (u32 *)&descriptor;
3190
3191 descriptor.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3192 descriptor.MSIxIndex = _base_get_msix_index(ioc);
3193 descriptor.SMID = cpu_to_le16(smid);
3194
3195 writel(cpu_to_le32(*request), &ioc->chip->AtomicRequestDescriptorPost);
3196 }
3197
3198 /**
3199 * _base_display_OEMs_branding - Display branding string
3200 * @ioc: per adapter object
3201 *
3202 * Return nothing.
3203 */
3204 static void
3205 _base_display_OEMs_branding(struct MPT3SAS_ADAPTER *ioc)
3206 {
3207 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
3208 return;
3209
3210 switch (ioc->pdev->subsystem_vendor) {
3211 case PCI_VENDOR_ID_INTEL:
3212 switch (ioc->pdev->device) {
3213 case MPI2_MFGPAGE_DEVID_SAS2008:
3214 switch (ioc->pdev->subsystem_device) {
3215 case MPT2SAS_INTEL_RMS2LL080_SSDID:
3216 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3217 MPT2SAS_INTEL_RMS2LL080_BRANDING);
3218 break;
3219 case MPT2SAS_INTEL_RMS2LL040_SSDID:
3220 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3221 MPT2SAS_INTEL_RMS2LL040_BRANDING);
3222 break;
3223 case MPT2SAS_INTEL_SSD910_SSDID:
3224 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3225 MPT2SAS_INTEL_SSD910_BRANDING);
3226 break;
3227 default:
3228 pr_info(MPT3SAS_FMT
3229 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3230 ioc->name, ioc->pdev->subsystem_device);
3231 break;
3232 }
3233 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3234 switch (ioc->pdev->subsystem_device) {
3235 case MPT2SAS_INTEL_RS25GB008_SSDID:
3236 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3237 MPT2SAS_INTEL_RS25GB008_BRANDING);
3238 break;
3239 case MPT2SAS_INTEL_RMS25JB080_SSDID:
3240 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3241 MPT2SAS_INTEL_RMS25JB080_BRANDING);
3242 break;
3243 case MPT2SAS_INTEL_RMS25JB040_SSDID:
3244 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3245 MPT2SAS_INTEL_RMS25JB040_BRANDING);
3246 break;
3247 case MPT2SAS_INTEL_RMS25KB080_SSDID:
3248 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3249 MPT2SAS_INTEL_RMS25KB080_BRANDING);
3250 break;
3251 case MPT2SAS_INTEL_RMS25KB040_SSDID:
3252 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3253 MPT2SAS_INTEL_RMS25KB040_BRANDING);
3254 break;
3255 case MPT2SAS_INTEL_RMS25LB040_SSDID:
3256 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3257 MPT2SAS_INTEL_RMS25LB040_BRANDING);
3258 break;
3259 case MPT2SAS_INTEL_RMS25LB080_SSDID:
3260 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3261 MPT2SAS_INTEL_RMS25LB080_BRANDING);
3262 break;
3263 default:
3264 pr_info(MPT3SAS_FMT
3265 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3266 ioc->name, ioc->pdev->subsystem_device);
3267 break;
3268 }
3269 case MPI25_MFGPAGE_DEVID_SAS3008:
3270 switch (ioc->pdev->subsystem_device) {
3271 case MPT3SAS_INTEL_RMS3JC080_SSDID:
3272 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3273 MPT3SAS_INTEL_RMS3JC080_BRANDING);
3274 break;
3275
3276 case MPT3SAS_INTEL_RS3GC008_SSDID:
3277 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3278 MPT3SAS_INTEL_RS3GC008_BRANDING);
3279 break;
3280 case MPT3SAS_INTEL_RS3FC044_SSDID:
3281 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3282 MPT3SAS_INTEL_RS3FC044_BRANDING);
3283 break;
3284 case MPT3SAS_INTEL_RS3UC080_SSDID:
3285 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3286 MPT3SAS_INTEL_RS3UC080_BRANDING);
3287 break;
3288 default:
3289 pr_info(MPT3SAS_FMT
3290 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3291 ioc->name, ioc->pdev->subsystem_device);
3292 break;
3293 }
3294 break;
3295 default:
3296 pr_info(MPT3SAS_FMT
3297 "Intel(R) Controller: Subsystem ID: 0x%X\n",
3298 ioc->name, ioc->pdev->subsystem_device);
3299 break;
3300 }
3301 break;
3302 case PCI_VENDOR_ID_DELL:
3303 switch (ioc->pdev->device) {
3304 case MPI2_MFGPAGE_DEVID_SAS2008:
3305 switch (ioc->pdev->subsystem_device) {
3306 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
3307 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3308 MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING);
3309 break;
3310 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
3311 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3312 MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING);
3313 break;
3314 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
3315 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3316 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING);
3317 break;
3318 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
3319 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3320 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING);
3321 break;
3322 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
3323 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3324 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING);
3325 break;
3326 case MPT2SAS_DELL_PERC_H200_SSDID:
3327 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3328 MPT2SAS_DELL_PERC_H200_BRANDING);
3329 break;
3330 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
3331 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3332 MPT2SAS_DELL_6GBPS_SAS_BRANDING);
3333 break;
3334 default:
3335 pr_info(MPT3SAS_FMT
3336 "Dell 6Gbps HBA: Subsystem ID: 0x%X\n",
3337 ioc->name, ioc->pdev->subsystem_device);
3338 break;
3339 }
3340 break;
3341 case MPI25_MFGPAGE_DEVID_SAS3008:
3342 switch (ioc->pdev->subsystem_device) {
3343 case MPT3SAS_DELL_12G_HBA_SSDID:
3344 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3345 MPT3SAS_DELL_12G_HBA_BRANDING);
3346 break;
3347 default:
3348 pr_info(MPT3SAS_FMT
3349 "Dell 12Gbps HBA: Subsystem ID: 0x%X\n",
3350 ioc->name, ioc->pdev->subsystem_device);
3351 break;
3352 }
3353 break;
3354 default:
3355 pr_info(MPT3SAS_FMT
3356 "Dell HBA: Subsystem ID: 0x%X\n", ioc->name,
3357 ioc->pdev->subsystem_device);
3358 break;
3359 }
3360 break;
3361 case PCI_VENDOR_ID_CISCO:
3362 switch (ioc->pdev->device) {
3363 case MPI25_MFGPAGE_DEVID_SAS3008:
3364 switch (ioc->pdev->subsystem_device) {
3365 case MPT3SAS_CISCO_12G_8E_HBA_SSDID:
3366 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3367 MPT3SAS_CISCO_12G_8E_HBA_BRANDING);
3368 break;
3369 case MPT3SAS_CISCO_12G_8I_HBA_SSDID:
3370 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3371 MPT3SAS_CISCO_12G_8I_HBA_BRANDING);
3372 break;
3373 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3374 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3375 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3376 break;
3377 default:
3378 pr_info(MPT3SAS_FMT
3379 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3380 ioc->name, ioc->pdev->subsystem_device);
3381 break;
3382 }
3383 break;
3384 case MPI25_MFGPAGE_DEVID_SAS3108_1:
3385 switch (ioc->pdev->subsystem_device) {
3386 case MPT3SAS_CISCO_12G_AVILA_HBA_SSDID:
3387 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3388 MPT3SAS_CISCO_12G_AVILA_HBA_BRANDING);
3389 break;
3390 case MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_SSDID:
3391 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3392 MPT3SAS_CISCO_12G_COLUSA_MEZZANINE_HBA_BRANDING
3393 );
3394 break;
3395 default:
3396 pr_info(MPT3SAS_FMT
3397 "Cisco 12Gbps SAS HBA: Subsystem ID: 0x%X\n",
3398 ioc->name, ioc->pdev->subsystem_device);
3399 break;
3400 }
3401 break;
3402 default:
3403 pr_info(MPT3SAS_FMT
3404 "Cisco SAS HBA: Subsystem ID: 0x%X\n",
3405 ioc->name, ioc->pdev->subsystem_device);
3406 break;
3407 }
3408 break;
3409 case MPT2SAS_HP_3PAR_SSVID:
3410 switch (ioc->pdev->device) {
3411 case MPI2_MFGPAGE_DEVID_SAS2004:
3412 switch (ioc->pdev->subsystem_device) {
3413 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
3414 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3415 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
3416 break;
3417 default:
3418 pr_info(MPT3SAS_FMT
3419 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3420 ioc->name, ioc->pdev->subsystem_device);
3421 break;
3422 }
3423 case MPI2_MFGPAGE_DEVID_SAS2308_2:
3424 switch (ioc->pdev->subsystem_device) {
3425 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
3426 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3427 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
3428 break;
3429 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
3430 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3431 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
3432 break;
3433 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
3434 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3435 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
3436 break;
3437 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
3438 pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3439 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
3440 break;
3441 default:
3442 pr_info(MPT3SAS_FMT
3443 "HP 6Gbps SAS HBA: Subsystem ID: 0x%X\n",
3444 ioc->name, ioc->pdev->subsystem_device);
3445 break;
3446 }
3447 default:
3448 pr_info(MPT3SAS_FMT
3449 "HP SAS HBA: Subsystem ID: 0x%X\n",
3450 ioc->name, ioc->pdev->subsystem_device);
3451 break;
3452 }
3453 default:
3454 break;
3455 }
3456 }
3457
3458 /**
3459 * _base_display_ioc_capabilities - Disply IOC's capabilities.
3460 * @ioc: per adapter object
3461 *
3462 * Return nothing.
3463 */
3464 static void
3465 _base_display_ioc_capabilities(struct MPT3SAS_ADAPTER *ioc)
3466 {
3467 int i = 0;
3468 char desc[16];
3469 u32 iounit_pg1_flags;
3470 u32 bios_version;
3471
3472 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
3473 strncpy(desc, ioc->manu_pg0.ChipName, 16);
3474 pr_info(MPT3SAS_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "\
3475 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
3476 ioc->name, desc,
3477 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
3478 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
3479 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
3480 ioc->facts.FWVersion.Word & 0x000000FF,
3481 ioc->pdev->revision,
3482 (bios_version & 0xFF000000) >> 24,
3483 (bios_version & 0x00FF0000) >> 16,
3484 (bios_version & 0x0000FF00) >> 8,
3485 bios_version & 0x000000FF);
3486
3487 _base_display_OEMs_branding(ioc);
3488
3489 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
3490 pr_info("%sNVMe", i ? "," : "");
3491 i++;
3492 }
3493
3494 pr_info(MPT3SAS_FMT "Protocol=(", ioc->name);
3495
3496 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
3497 pr_info("Initiator");
3498 i++;
3499 }
3500
3501 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
3502 pr_info("%sTarget", i ? "," : "");
3503 i++;
3504 }
3505
3506 i = 0;
3507 pr_info("), ");
3508 pr_info("Capabilities=(");
3509
3510 if (!ioc->hide_ir_msg) {
3511 if (ioc->facts.IOCCapabilities &
3512 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
3513 pr_info("Raid");
3514 i++;
3515 }
3516 }
3517
3518 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
3519 pr_info("%sTLR", i ? "," : "");
3520 i++;
3521 }
3522
3523 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
3524 pr_info("%sMulticast", i ? "," : "");
3525 i++;
3526 }
3527
3528 if (ioc->facts.IOCCapabilities &
3529 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
3530 pr_info("%sBIDI Target", i ? "," : "");
3531 i++;
3532 }
3533
3534 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
3535 pr_info("%sEEDP", i ? "," : "");
3536 i++;
3537 }
3538
3539 if (ioc->facts.IOCCapabilities &
3540 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
3541 pr_info("%sSnapshot Buffer", i ? "," : "");
3542 i++;
3543 }
3544
3545 if (ioc->facts.IOCCapabilities &
3546 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
3547 pr_info("%sDiag Trace Buffer", i ? "," : "");
3548 i++;
3549 }
3550
3551 if (ioc->facts.IOCCapabilities &
3552 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
3553 pr_info("%sDiag Extended Buffer", i ? "," : "");
3554 i++;
3555 }
3556
3557 if (ioc->facts.IOCCapabilities &
3558 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
3559 pr_info("%sTask Set Full", i ? "," : "");
3560 i++;
3561 }
3562
3563 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3564 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
3565 pr_info("%sNCQ", i ? "," : "");
3566 i++;
3567 }
3568
3569 pr_info(")\n");
3570 }
3571
3572 /**
3573 * mpt3sas_base_update_missing_delay - change the missing delay timers
3574 * @ioc: per adapter object
3575 * @device_missing_delay: amount of time till device is reported missing
3576 * @io_missing_delay: interval IO is returned when there is a missing device
3577 *
3578 * Return nothing.
3579 *
3580 * Passed on the command line, this function will modify the device missing
3581 * delay, as well as the io missing delay. This should be called at driver
3582 * load time.
3583 */
3584 void
3585 mpt3sas_base_update_missing_delay(struct MPT3SAS_ADAPTER *ioc,
3586 u16 device_missing_delay, u8 io_missing_delay)
3587 {
3588 u16 dmd, dmd_new, dmd_orignal;
3589 u8 io_missing_delay_original;
3590 u16 sz;
3591 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
3592 Mpi2ConfigReply_t mpi_reply;
3593 u8 num_phys = 0;
3594 u16 ioc_status;
3595
3596 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
3597 if (!num_phys)
3598 return;
3599
3600 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
3601 sizeof(Mpi2SasIOUnit1PhyData_t));
3602 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
3603 if (!sas_iounit_pg1) {
3604 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3605 ioc->name, __FILE__, __LINE__, __func__);
3606 goto out;
3607 }
3608 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
3609 sas_iounit_pg1, sz))) {
3610 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3611 ioc->name, __FILE__, __LINE__, __func__);
3612 goto out;
3613 }
3614 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
3615 MPI2_IOCSTATUS_MASK;
3616 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3617 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
3618 ioc->name, __FILE__, __LINE__, __func__);
3619 goto out;
3620 }
3621
3622 /* device missing delay */
3623 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
3624 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3625 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3626 else
3627 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3628 dmd_orignal = dmd;
3629 if (device_missing_delay > 0x7F) {
3630 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
3631 device_missing_delay;
3632 dmd = dmd / 16;
3633 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
3634 } else
3635 dmd = device_missing_delay;
3636 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
3637
3638 /* io missing delay */
3639 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
3640 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
3641
3642 if (!mpt3sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
3643 sz)) {
3644 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
3645 dmd_new = (dmd &
3646 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
3647 else
3648 dmd_new =
3649 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
3650 pr_info(MPT3SAS_FMT "device_missing_delay: old(%d), new(%d)\n",
3651 ioc->name, dmd_orignal, dmd_new);
3652 pr_info(MPT3SAS_FMT "ioc_missing_delay: old(%d), new(%d)\n",
3653 ioc->name, io_missing_delay_original,
3654 io_missing_delay);
3655 ioc->device_missing_delay = dmd_new;
3656 ioc->io_missing_delay = io_missing_delay;
3657 }
3658
3659 out:
3660 kfree(sas_iounit_pg1);
3661 }
3662 /**
3663 * _base_static_config_pages - static start of day config pages
3664 * @ioc: per adapter object
3665 *
3666 * Return nothing.
3667 */
3668 static void
3669 _base_static_config_pages(struct MPT3SAS_ADAPTER *ioc)
3670 {
3671 Mpi2ConfigReply_t mpi_reply;
3672 u32 iounit_pg1_flags;
3673
3674 mpt3sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
3675 if (ioc->ir_firmware)
3676 mpt3sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
3677 &ioc->manu_pg10);
3678
3679 /*
3680 * Ensure correct T10 PI operation if vendor left EEDPTagMode
3681 * flag unset in NVDATA.
3682 */
3683 mpt3sas_config_get_manufacturing_pg11(ioc, &mpi_reply, &ioc->manu_pg11);
3684 if (ioc->manu_pg11.EEDPTagMode == 0) {
3685 pr_err("%s: overriding NVDATA EEDPTagMode setting\n",
3686 ioc->name);
3687 ioc->manu_pg11.EEDPTagMode &= ~0x3;
3688 ioc->manu_pg11.EEDPTagMode |= 0x1;
3689 mpt3sas_config_set_manufacturing_pg11(ioc, &mpi_reply,
3690 &ioc->manu_pg11);
3691 }
3692
3693 mpt3sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
3694 mpt3sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
3695 mpt3sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
3696 mpt3sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
3697 mpt3sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
3698 mpt3sas_config_get_iounit_pg8(ioc, &mpi_reply, &ioc->iounit_pg8);
3699 _base_display_ioc_capabilities(ioc);
3700
3701 /*
3702 * Enable task_set_full handling in iounit_pg1 when the
3703 * facts capabilities indicate that its supported.
3704 */
3705 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
3706 if ((ioc->facts.IOCCapabilities &
3707 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
3708 iounit_pg1_flags &=
3709 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3710 else
3711 iounit_pg1_flags |=
3712 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
3713 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
3714 mpt3sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
3715
3716 if (ioc->iounit_pg8.NumSensors)
3717 ioc->temp_sensors_count = ioc->iounit_pg8.NumSensors;
3718 }
3719
3720 /**
3721 * _base_release_memory_pools - release memory
3722 * @ioc: per adapter object
3723 *
3724 * Free memory allocated from _base_allocate_memory_pools.
3725 *
3726 * Return nothing.
3727 */
3728 static void
3729 _base_release_memory_pools(struct MPT3SAS_ADAPTER *ioc)
3730 {
3731 int i = 0;
3732 struct reply_post_struct *rps;
3733
3734 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3735 __func__));
3736
3737 if (ioc->request) {
3738 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
3739 ioc->request, ioc->request_dma);
3740 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3741 "request_pool(0x%p): free\n",
3742 ioc->name, ioc->request));
3743 ioc->request = NULL;
3744 }
3745
3746 if (ioc->sense) {
3747 dma_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
3748 dma_pool_destroy(ioc->sense_dma_pool);
3749 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3750 "sense_pool(0x%p): free\n",
3751 ioc->name, ioc->sense));
3752 ioc->sense = NULL;
3753 }
3754
3755 if (ioc->reply) {
3756 dma_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
3757 dma_pool_destroy(ioc->reply_dma_pool);
3758 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3759 "reply_pool(0x%p): free\n",
3760 ioc->name, ioc->reply));
3761 ioc->reply = NULL;
3762 }
3763
3764 if (ioc->reply_free) {
3765 dma_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
3766 ioc->reply_free_dma);
3767 dma_pool_destroy(ioc->reply_free_dma_pool);
3768 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3769 "reply_free_pool(0x%p): free\n",
3770 ioc->name, ioc->reply_free));
3771 ioc->reply_free = NULL;
3772 }
3773
3774 if (ioc->reply_post) {
3775 do {
3776 rps = &ioc->reply_post[i];
3777 if (rps->reply_post_free) {
3778 dma_pool_free(
3779 ioc->reply_post_free_dma_pool,
3780 rps->reply_post_free,
3781 rps->reply_post_free_dma);
3782 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3783 "reply_post_free_pool(0x%p): free\n",
3784 ioc->name, rps->reply_post_free));
3785 rps->reply_post_free = NULL;
3786 }
3787 } while (ioc->rdpq_array_enable &&
3788 (++i < ioc->reply_queue_count));
3789
3790 dma_pool_destroy(ioc->reply_post_free_dma_pool);
3791 kfree(ioc->reply_post);
3792 }
3793
3794 if (ioc->pcie_sgl_dma_pool) {
3795 for (i = 0; i < ioc->scsiio_depth; i++) {
3796 if (ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl)
3797 pci_pool_free(ioc->pcie_sgl_dma_pool,
3798 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl,
3799 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl_dma);
3800 }
3801 if (ioc->pcie_sgl_dma_pool)
3802 pci_pool_destroy(ioc->pcie_sgl_dma_pool);
3803 }
3804
3805 if (ioc->config_page) {
3806 dexitprintk(ioc, pr_info(MPT3SAS_FMT
3807 "config_page(0x%p): free\n", ioc->name,
3808 ioc->config_page));
3809 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
3810 ioc->config_page, ioc->config_page_dma);
3811 }
3812
3813 if (ioc->scsi_lookup) {
3814 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
3815 ioc->scsi_lookup = NULL;
3816 }
3817 kfree(ioc->hpr_lookup);
3818 kfree(ioc->internal_lookup);
3819 if (ioc->chain_lookup) {
3820 for (i = 0; i < ioc->chain_depth; i++) {
3821 if (ioc->chain_lookup[i].chain_buffer)
3822 dma_pool_free(ioc->chain_dma_pool,
3823 ioc->chain_lookup[i].chain_buffer,
3824 ioc->chain_lookup[i].chain_buffer_dma);
3825 }
3826 dma_pool_destroy(ioc->chain_dma_pool);
3827 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
3828 ioc->chain_lookup = NULL;
3829 }
3830 }
3831
3832 /**
3833 * _base_allocate_memory_pools - allocate start of day memory pools
3834 * @ioc: per adapter object
3835 *
3836 * Returns 0 success, anything else error
3837 */
3838 static int
3839 _base_allocate_memory_pools(struct MPT3SAS_ADAPTER *ioc)
3840 {
3841 struct mpt3sas_facts *facts;
3842 u16 max_sge_elements;
3843 u16 chains_needed_per_io;
3844 u32 sz, total_sz, reply_post_free_sz;
3845 u32 retry_sz;
3846 u16 max_request_credit, nvme_blocks_needed;
3847 unsigned short sg_tablesize;
3848 u16 sge_size;
3849 int i;
3850
3851 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
3852 __func__));
3853
3854
3855 retry_sz = 0;
3856 facts = &ioc->facts;
3857
3858 /* command line tunables for max sgl entries */
3859 if (max_sgl_entries != -1)
3860 sg_tablesize = max_sgl_entries;
3861 else {
3862 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
3863 sg_tablesize = MPT2SAS_SG_DEPTH;
3864 else
3865 sg_tablesize = MPT3SAS_SG_DEPTH;
3866 }
3867
3868 /* max sgl entries <= MPT_KDUMP_MIN_PHYS_SEGMENTS in KDUMP mode */
3869 if (reset_devices)
3870 sg_tablesize = min_t(unsigned short, sg_tablesize,
3871 MPT_KDUMP_MIN_PHYS_SEGMENTS);
3872
3873 if (sg_tablesize < MPT_MIN_PHYS_SEGMENTS)
3874 sg_tablesize = MPT_MIN_PHYS_SEGMENTS;
3875 else if (sg_tablesize > MPT_MAX_PHYS_SEGMENTS) {
3876 sg_tablesize = min_t(unsigned short, sg_tablesize,
3877 SG_MAX_SEGMENTS);
3878 pr_warn(MPT3SAS_FMT
3879 "sg_tablesize(%u) is bigger than kernel"
3880 " defined SG_CHUNK_SIZE(%u)\n", ioc->name,
3881 sg_tablesize, MPT_MAX_PHYS_SEGMENTS);
3882 }
3883 ioc->shost->sg_tablesize = sg_tablesize;
3884
3885 ioc->internal_depth = min_t(int, (facts->HighPriorityCredit + (5)),
3886 (facts->RequestCredit / 4));
3887 if (ioc->internal_depth < INTERNAL_CMDS_COUNT) {
3888 if (facts->RequestCredit <= (INTERNAL_CMDS_COUNT +
3889 INTERNAL_SCSIIO_CMDS_COUNT)) {
3890 pr_err(MPT3SAS_FMT "IOC doesn't have enough Request \
3891 Credits, it has just %d number of credits\n",
3892 ioc->name, facts->RequestCredit);
3893 return -ENOMEM;
3894 }
3895 ioc->internal_depth = 10;
3896 }
3897
3898 ioc->hi_priority_depth = ioc->internal_depth - (5);
3899 /* command line tunables for max controller queue depth */
3900 if (max_queue_depth != -1 && max_queue_depth != 0) {
3901 max_request_credit = min_t(u16, max_queue_depth +
3902 ioc->internal_depth, facts->RequestCredit);
3903 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
3904 max_request_credit = MAX_HBA_QUEUE_DEPTH;
3905 } else if (reset_devices)
3906 max_request_credit = min_t(u16, facts->RequestCredit,
3907 (MPT3SAS_KDUMP_SCSI_IO_DEPTH + ioc->internal_depth));
3908 else
3909 max_request_credit = min_t(u16, facts->RequestCredit,
3910 MAX_HBA_QUEUE_DEPTH);
3911
3912 /* Firmware maintains additional facts->HighPriorityCredit number of
3913 * credits for HiPriprity Request messages, so hba queue depth will be
3914 * sum of max_request_credit and high priority queue depth.
3915 */
3916 ioc->hba_queue_depth = max_request_credit + ioc->hi_priority_depth;
3917
3918 /* request frame size */
3919 ioc->request_sz = facts->IOCRequestFrameSize * 4;
3920
3921 /* reply frame size */
3922 ioc->reply_sz = facts->ReplyFrameSize * 4;
3923
3924 /* chain segment size */
3925 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
3926 if (facts->IOCMaxChainSegmentSize)
3927 ioc->chain_segment_sz =
3928 facts->IOCMaxChainSegmentSize *
3929 MAX_CHAIN_ELEMT_SZ;
3930 else
3931 /* set to 128 bytes size if IOCMaxChainSegmentSize is zero */
3932 ioc->chain_segment_sz = DEFAULT_NUM_FWCHAIN_ELEMTS *
3933 MAX_CHAIN_ELEMT_SZ;
3934 } else
3935 ioc->chain_segment_sz = ioc->request_sz;
3936
3937 /* calculate the max scatter element size */
3938 sge_size = max_t(u16, ioc->sge_size, ioc->sge_size_ieee);
3939
3940 retry_allocation:
3941 total_sz = 0;
3942 /* calculate number of sg elements left over in the 1st frame */
3943 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
3944 sizeof(Mpi2SGEIOUnion_t)) + sge_size);
3945 ioc->max_sges_in_main_message = max_sge_elements/sge_size;
3946
3947 /* now do the same for a chain buffer */
3948 max_sge_elements = ioc->chain_segment_sz - sge_size;
3949 ioc->max_sges_in_chain_message = max_sge_elements/sge_size;
3950
3951 /*
3952 * MPT3SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
3953 */
3954 chains_needed_per_io = ((ioc->shost->sg_tablesize -
3955 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
3956 + 1;
3957 if (chains_needed_per_io > facts->MaxChainDepth) {
3958 chains_needed_per_io = facts->MaxChainDepth;
3959 ioc->shost->sg_tablesize = min_t(u16,
3960 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
3961 * chains_needed_per_io), ioc->shost->sg_tablesize);
3962 }
3963 ioc->chains_needed_per_io = chains_needed_per_io;
3964
3965 /* reply free queue sizing - taking into account for 64 FW events */
3966 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3967
3968 /* calculate reply descriptor post queue depth */
3969 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
3970 ioc->reply_free_queue_depth + 1 ;
3971 /* align the reply post queue on the next 16 count boundary */
3972 if (ioc->reply_post_queue_depth % 16)
3973 ioc->reply_post_queue_depth += 16 -
3974 (ioc->reply_post_queue_depth % 16);
3975
3976 if (ioc->reply_post_queue_depth >
3977 facts->MaxReplyDescriptorPostQueueDepth) {
3978 ioc->reply_post_queue_depth =
3979 facts->MaxReplyDescriptorPostQueueDepth -
3980 (facts->MaxReplyDescriptorPostQueueDepth % 16);
3981 ioc->hba_queue_depth =
3982 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
3983 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
3984 }
3985
3986 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scatter gather: " \
3987 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
3988 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
3989 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
3990 ioc->chains_needed_per_io));
3991
3992 /* reply post queue, 16 byte align */
3993 reply_post_free_sz = ioc->reply_post_queue_depth *
3994 sizeof(Mpi2DefaultReplyDescriptor_t);
3995
3996 sz = reply_post_free_sz;
3997 if (_base_is_controller_msix_enabled(ioc) && !ioc->rdpq_array_enable)
3998 sz *= ioc->reply_queue_count;
3999
4000 ioc->reply_post = kcalloc((ioc->rdpq_array_enable) ?
4001 (ioc->reply_queue_count):1,
4002 sizeof(struct reply_post_struct), GFP_KERNEL);
4003
4004 if (!ioc->reply_post) {
4005 pr_err(MPT3SAS_FMT "reply_post_free pool: kcalloc failed\n",
4006 ioc->name);
4007 goto out;
4008 }
4009 ioc->reply_post_free_dma_pool = dma_pool_create("reply_post_free pool",
4010 &ioc->pdev->dev, sz, 16, 0);
4011 if (!ioc->reply_post_free_dma_pool) {
4012 pr_err(MPT3SAS_FMT
4013 "reply_post_free pool: dma_pool_create failed\n",
4014 ioc->name);
4015 goto out;
4016 }
4017 i = 0;
4018 do {
4019 ioc->reply_post[i].reply_post_free =
4020 dma_pool_alloc(ioc->reply_post_free_dma_pool,
4021 GFP_KERNEL,
4022 &ioc->reply_post[i].reply_post_free_dma);
4023 if (!ioc->reply_post[i].reply_post_free) {
4024 pr_err(MPT3SAS_FMT
4025 "reply_post_free pool: dma_pool_alloc failed\n",
4026 ioc->name);
4027 goto out;
4028 }
4029 memset(ioc->reply_post[i].reply_post_free, 0, sz);
4030 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4031 "reply post free pool (0x%p): depth(%d),"
4032 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4033 ioc->reply_post[i].reply_post_free,
4034 ioc->reply_post_queue_depth, 8, sz/1024));
4035 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4036 "reply_post_free_dma = (0x%llx)\n", ioc->name,
4037 (unsigned long long)
4038 ioc->reply_post[i].reply_post_free_dma));
4039 total_sz += sz;
4040 } while (ioc->rdpq_array_enable && (++i < ioc->reply_queue_count));
4041
4042 if (ioc->dma_mask == 64) {
4043 if (_base_change_consistent_dma_mask(ioc, ioc->pdev) != 0) {
4044 pr_warn(MPT3SAS_FMT
4045 "no suitable consistent DMA mask for %s\n",
4046 ioc->name, pci_name(ioc->pdev));
4047 goto out;
4048 }
4049 }
4050
4051 ioc->scsiio_depth = ioc->hba_queue_depth -
4052 ioc->hi_priority_depth - ioc->internal_depth;
4053
4054 /* set the scsi host can_queue depth
4055 * with some internal commands that could be outstanding
4056 */
4057 ioc->shost->can_queue = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT;
4058 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4059 "scsi host: can_queue depth (%d)\n",
4060 ioc->name, ioc->shost->can_queue));
4061
4062
4063 /* contiguous pool for request and chains, 16 byte align, one extra "
4064 * "frame for smid=0
4065 */
4066 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
4067 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
4068
4069 /* hi-priority queue */
4070 sz += (ioc->hi_priority_depth * ioc->request_sz);
4071
4072 /* internal queue */
4073 sz += (ioc->internal_depth * ioc->request_sz);
4074
4075 ioc->request_dma_sz = sz;
4076 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
4077 if (!ioc->request) {
4078 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4079 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4080 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
4081 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4082 if (ioc->scsiio_depth < MPT3SAS_SAS_QUEUE_DEPTH)
4083 goto out;
4084 retry_sz = 64;
4085 ioc->hba_queue_depth -= retry_sz;
4086 _base_release_memory_pools(ioc);
4087 goto retry_allocation;
4088 }
4089
4090 if (retry_sz)
4091 pr_err(MPT3SAS_FMT "request pool: pci_alloc_consistent " \
4092 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
4093 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
4094 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
4095
4096 /* hi-priority queue */
4097 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
4098 ioc->request_sz);
4099 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
4100 ioc->request_sz);
4101
4102 /* internal queue */
4103 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
4104 ioc->request_sz);
4105 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
4106 ioc->request_sz);
4107
4108 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4109 "request pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4110 ioc->name, ioc->request, ioc->hba_queue_depth, ioc->request_sz,
4111 (ioc->hba_queue_depth * ioc->request_sz)/1024));
4112
4113 dinitprintk(ioc, pr_info(MPT3SAS_FMT "request pool: dma(0x%llx)\n",
4114 ioc->name, (unsigned long long) ioc->request_dma));
4115 total_sz += sz;
4116
4117 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
4118 ioc->scsi_lookup_pages = get_order(sz);
4119 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
4120 GFP_KERNEL, ioc->scsi_lookup_pages);
4121 if (!ioc->scsi_lookup) {
4122 pr_err(MPT3SAS_FMT "scsi_lookup: get_free_pages failed, sz(%d)\n",
4123 ioc->name, (int)sz);
4124 goto out;
4125 }
4126
4127 dinitprintk(ioc, pr_info(MPT3SAS_FMT "scsiio(0x%p): depth(%d)\n",
4128 ioc->name, ioc->request, ioc->scsiio_depth));
4129
4130 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
4131 sz = ioc->chain_depth * sizeof(struct chain_tracker);
4132 ioc->chain_pages = get_order(sz);
4133 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
4134 GFP_KERNEL, ioc->chain_pages);
4135 if (!ioc->chain_lookup) {
4136 pr_err(MPT3SAS_FMT "chain_lookup: __get_free_pages failed\n",
4137 ioc->name);
4138 goto out;
4139 }
4140 ioc->chain_dma_pool = dma_pool_create("chain pool", &ioc->pdev->dev,
4141 ioc->chain_segment_sz, 16, 0);
4142 if (!ioc->chain_dma_pool) {
4143 pr_err(MPT3SAS_FMT "chain_dma_pool: dma_pool_create failed\n",
4144 ioc->name);
4145 goto out;
4146 }
4147 for (i = 0; i < ioc->chain_depth; i++) {
4148 ioc->chain_lookup[i].chain_buffer = dma_pool_alloc(
4149 ioc->chain_dma_pool , GFP_KERNEL,
4150 &ioc->chain_lookup[i].chain_buffer_dma);
4151 if (!ioc->chain_lookup[i].chain_buffer) {
4152 ioc->chain_depth = i;
4153 goto chain_done;
4154 }
4155 total_sz += ioc->chain_segment_sz;
4156 }
4157 chain_done:
4158 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4159 "chain pool depth(%d), frame_size(%d), pool_size(%d kB)\n",
4160 ioc->name, ioc->chain_depth, ioc->chain_segment_sz,
4161 ((ioc->chain_depth * ioc->chain_segment_sz))/1024));
4162
4163 /* initialize hi-priority queue smid's */
4164 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
4165 sizeof(struct request_tracker), GFP_KERNEL);
4166 if (!ioc->hpr_lookup) {
4167 pr_err(MPT3SAS_FMT "hpr_lookup: kcalloc failed\n",
4168 ioc->name);
4169 goto out;
4170 }
4171 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
4172 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4173 "hi_priority(0x%p): depth(%d), start smid(%d)\n",
4174 ioc->name, ioc->hi_priority,
4175 ioc->hi_priority_depth, ioc->hi_priority_smid));
4176
4177 /* initialize internal queue smid's */
4178 ioc->internal_lookup = kcalloc(ioc->internal_depth,
4179 sizeof(struct request_tracker), GFP_KERNEL);
4180 if (!ioc->internal_lookup) {
4181 pr_err(MPT3SAS_FMT "internal_lookup: kcalloc failed\n",
4182 ioc->name);
4183 goto out;
4184 }
4185 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
4186 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4187 "internal(0x%p): depth(%d), start smid(%d)\n",
4188 ioc->name, ioc->internal,
4189 ioc->internal_depth, ioc->internal_smid));
4190 /*
4191 * The number of NVMe page sized blocks needed is:
4192 * (((sg_tablesize * 8) - 1) / (page_size - 8)) + 1
4193 * ((sg_tablesize * 8) - 1) is the max PRP's minus the first PRP entry
4194 * that is placed in the main message frame. 8 is the size of each PRP
4195 * entry or PRP list pointer entry. 8 is subtracted from page_size
4196 * because of the PRP list pointer entry at the end of a page, so this
4197 * is not counted as a PRP entry. The 1 added page is a round up.
4198 *
4199 * To avoid allocation failures due to the amount of memory that could
4200 * be required for NVMe PRP's, only each set of NVMe blocks will be
4201 * contiguous, so a new set is allocated for each possible I/O.
4202 */
4203 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_NVME_DEVICES) {
4204 nvme_blocks_needed =
4205 (ioc->shost->sg_tablesize * NVME_PRP_SIZE) - 1;
4206 nvme_blocks_needed /= (ioc->page_size - NVME_PRP_SIZE);
4207 nvme_blocks_needed++;
4208
4209 sz = nvme_blocks_needed * ioc->page_size;
4210 ioc->pcie_sgl_dma_pool =
4211 pci_pool_create("PCIe SGL pool", ioc->pdev, sz, 16, 0);
4212 if (!ioc->pcie_sgl_dma_pool) {
4213 pr_info(MPT3SAS_FMT
4214 "PCIe SGL pool: pci_pool_create failed\n",
4215 ioc->name);
4216 goto out;
4217 }
4218 for (i = 0; i < ioc->scsiio_depth; i++) {
4219 ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl =
4220 pci_pool_alloc(ioc->pcie_sgl_dma_pool,
4221 GFP_KERNEL,
4222 &ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl_dma);
4223 if (!ioc->scsi_lookup[i].pcie_sg_list.pcie_sgl) {
4224 pr_info(MPT3SAS_FMT
4225 "PCIe SGL pool: pci_pool_alloc failed\n",
4226 ioc->name);
4227 goto out;
4228 }
4229 }
4230
4231 dinitprintk(ioc, pr_info(MPT3SAS_FMT "PCIe sgl pool depth(%d), "
4232 "element_size(%d), pool_size(%d kB)\n", ioc->name,
4233 ioc->scsiio_depth, sz, (sz * ioc->scsiio_depth)/1024));
4234 total_sz += sz * ioc->scsiio_depth;
4235 }
4236 /* sense buffers, 4 byte align */
4237 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
4238 ioc->sense_dma_pool = dma_pool_create("sense pool", &ioc->pdev->dev, sz,
4239 4, 0);
4240 if (!ioc->sense_dma_pool) {
4241 pr_err(MPT3SAS_FMT "sense pool: dma_pool_create failed\n",
4242 ioc->name);
4243 goto out;
4244 }
4245 ioc->sense = dma_pool_alloc(ioc->sense_dma_pool, GFP_KERNEL,
4246 &ioc->sense_dma);
4247 if (!ioc->sense) {
4248 pr_err(MPT3SAS_FMT "sense pool: dma_pool_alloc failed\n",
4249 ioc->name);
4250 goto out;
4251 }
4252 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4253 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
4254 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
4255 SCSI_SENSE_BUFFERSIZE, sz/1024));
4256 dinitprintk(ioc, pr_info(MPT3SAS_FMT "sense_dma(0x%llx)\n",
4257 ioc->name, (unsigned long long)ioc->sense_dma));
4258 total_sz += sz;
4259
4260 /* reply pool, 4 byte align */
4261 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
4262 ioc->reply_dma_pool = dma_pool_create("reply pool", &ioc->pdev->dev, sz,
4263 4, 0);
4264 if (!ioc->reply_dma_pool) {
4265 pr_err(MPT3SAS_FMT "reply pool: dma_pool_create failed\n",
4266 ioc->name);
4267 goto out;
4268 }
4269 ioc->reply = dma_pool_alloc(ioc->reply_dma_pool, GFP_KERNEL,
4270 &ioc->reply_dma);
4271 if (!ioc->reply) {
4272 pr_err(MPT3SAS_FMT "reply pool: dma_pool_alloc failed\n",
4273 ioc->name);
4274 goto out;
4275 }
4276 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
4277 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
4278 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4279 "reply pool(0x%p): depth(%d), frame_size(%d), pool_size(%d kB)\n",
4280 ioc->name, ioc->reply,
4281 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
4282 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_dma(0x%llx)\n",
4283 ioc->name, (unsigned long long)ioc->reply_dma));
4284 total_sz += sz;
4285
4286 /* reply free queue, 16 byte align */
4287 sz = ioc->reply_free_queue_depth * 4;
4288 ioc->reply_free_dma_pool = dma_pool_create("reply_free pool",
4289 &ioc->pdev->dev, sz, 16, 0);
4290 if (!ioc->reply_free_dma_pool) {
4291 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_create failed\n",
4292 ioc->name);
4293 goto out;
4294 }
4295 ioc->reply_free = dma_pool_alloc(ioc->reply_free_dma_pool, GFP_KERNEL,
4296 &ioc->reply_free_dma);
4297 if (!ioc->reply_free) {
4298 pr_err(MPT3SAS_FMT "reply_free pool: dma_pool_alloc failed\n",
4299 ioc->name);
4300 goto out;
4301 }
4302 memset(ioc->reply_free, 0, sz);
4303 dinitprintk(ioc, pr_info(MPT3SAS_FMT "reply_free pool(0x%p): " \
4304 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
4305 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
4306 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4307 "reply_free_dma (0x%llx)\n",
4308 ioc->name, (unsigned long long)ioc->reply_free_dma));
4309 total_sz += sz;
4310
4311 ioc->config_page_sz = 512;
4312 ioc->config_page = pci_alloc_consistent(ioc->pdev,
4313 ioc->config_page_sz, &ioc->config_page_dma);
4314 if (!ioc->config_page) {
4315 pr_err(MPT3SAS_FMT
4316 "config page: dma_pool_alloc failed\n",
4317 ioc->name);
4318 goto out;
4319 }
4320 dinitprintk(ioc, pr_info(MPT3SAS_FMT
4321 "config page(0x%p): size(%d)\n",
4322 ioc->name, ioc->config_page, ioc->config_page_sz));
4323 dinitprintk(ioc, pr_info(MPT3SAS_FMT "config_page_dma(0x%llx)\n",
4324 ioc->name, (unsigned long long)ioc->config_page_dma));
4325 total_sz += ioc->config_page_sz;
4326
4327 pr_info(MPT3SAS_FMT "Allocated physical memory: size(%d kB)\n",
4328 ioc->name, total_sz/1024);
4329 pr_info(MPT3SAS_FMT
4330 "Current Controller Queue Depth(%d),Max Controller Queue Depth(%d)\n",
4331 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
4332 pr_info(MPT3SAS_FMT "Scatter Gather Elements per IO(%d)\n",
4333 ioc->name, ioc->shost->sg_tablesize);
4334 return 0;
4335
4336 out:
4337 return -ENOMEM;
4338 }
4339
4340 /**
4341 * mpt3sas_base_get_iocstate - Get the current state of a MPT adapter.
4342 * @ioc: Pointer to MPT_ADAPTER structure
4343 * @cooked: Request raw or cooked IOC state
4344 *
4345 * Returns all IOC Doorbell register bits if cooked==0, else just the
4346 * Doorbell bits in MPI_IOC_STATE_MASK.
4347 */
4348 u32
4349 mpt3sas_base_get_iocstate(struct MPT3SAS_ADAPTER *ioc, int cooked)
4350 {
4351 u32 s, sc;
4352
4353 s = readl(&ioc->chip->Doorbell);
4354 sc = s & MPI2_IOC_STATE_MASK;
4355 return cooked ? sc : s;
4356 }
4357
4358 /**
4359 * _base_wait_on_iocstate - waiting on a particular ioc state
4360 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
4361 * @timeout: timeout in second
4362 *
4363 * Returns 0 for success, non-zero for failure.
4364 */
4365 static int
4366 _base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc, u32 ioc_state, int timeout)
4367 {
4368 u32 count, cntdn;
4369 u32 current_state;
4370
4371 count = 0;
4372 cntdn = 1000 * timeout;
4373 do {
4374 current_state = mpt3sas_base_get_iocstate(ioc, 1);
4375 if (current_state == ioc_state)
4376 return 0;
4377 if (count && current_state == MPI2_IOC_STATE_FAULT)
4378 break;
4379
4380 usleep_range(1000, 1500);
4381 count++;
4382 } while (--cntdn);
4383
4384 return current_state;
4385 }
4386
4387 /**
4388 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
4389 * a write to the doorbell)
4390 * @ioc: per adapter object
4391 * @timeout: timeout in second
4392 *
4393 * Returns 0 for success, non-zero for failure.
4394 *
4395 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
4396 */
4397 static int
4398 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc);
4399
4400 static int
4401 _base_wait_for_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
4402 {
4403 u32 cntdn, count;
4404 u32 int_status;
4405
4406 count = 0;
4407 cntdn = 1000 * timeout;
4408 do {
4409 int_status = readl(&ioc->chip->HostInterruptStatus);
4410 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4411 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4412 "%s: successful count(%d), timeout(%d)\n",
4413 ioc->name, __func__, count, timeout));
4414 return 0;
4415 }
4416
4417 usleep_range(1000, 1500);
4418 count++;
4419 } while (--cntdn);
4420
4421 pr_err(MPT3SAS_FMT
4422 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4423 ioc->name, __func__, count, int_status);
4424 return -EFAULT;
4425 }
4426
4427 static int
4428 _base_spin_on_doorbell_int(struct MPT3SAS_ADAPTER *ioc, int timeout)
4429 {
4430 u32 cntdn, count;
4431 u32 int_status;
4432
4433 count = 0;
4434 cntdn = 2000 * timeout;
4435 do {
4436 int_status = readl(&ioc->chip->HostInterruptStatus);
4437 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4438 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4439 "%s: successful count(%d), timeout(%d)\n",
4440 ioc->name, __func__, count, timeout));
4441 return 0;
4442 }
4443
4444 udelay(500);
4445 count++;
4446 } while (--cntdn);
4447
4448 pr_err(MPT3SAS_FMT
4449 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4450 ioc->name, __func__, count, int_status);
4451 return -EFAULT;
4452
4453 }
4454
4455 /**
4456 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
4457 * @ioc: per adapter object
4458 * @timeout: timeout in second
4459 *
4460 * Returns 0 for success, non-zero for failure.
4461 *
4462 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
4463 * doorbell.
4464 */
4465 static int
4466 _base_wait_for_doorbell_ack(struct MPT3SAS_ADAPTER *ioc, int timeout)
4467 {
4468 u32 cntdn, count;
4469 u32 int_status;
4470 u32 doorbell;
4471
4472 count = 0;
4473 cntdn = 1000 * timeout;
4474 do {
4475 int_status = readl(&ioc->chip->HostInterruptStatus);
4476 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
4477 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4478 "%s: successful count(%d), timeout(%d)\n",
4479 ioc->name, __func__, count, timeout));
4480 return 0;
4481 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
4482 doorbell = readl(&ioc->chip->Doorbell);
4483 if ((doorbell & MPI2_IOC_STATE_MASK) ==
4484 MPI2_IOC_STATE_FAULT) {
4485 mpt3sas_base_fault_info(ioc , doorbell);
4486 return -EFAULT;
4487 }
4488 } else if (int_status == 0xFFFFFFFF)
4489 goto out;
4490
4491 usleep_range(1000, 1500);
4492 count++;
4493 } while (--cntdn);
4494
4495 out:
4496 pr_err(MPT3SAS_FMT
4497 "%s: failed due to timeout count(%d), int_status(%x)!\n",
4498 ioc->name, __func__, count, int_status);
4499 return -EFAULT;
4500 }
4501
4502 /**
4503 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
4504 * @ioc: per adapter object
4505 * @timeout: timeout in second
4506 *
4507 * Returns 0 for success, non-zero for failure.
4508 *
4509 */
4510 static int
4511 _base_wait_for_doorbell_not_used(struct MPT3SAS_ADAPTER *ioc, int timeout)
4512 {
4513 u32 cntdn, count;
4514 u32 doorbell_reg;
4515
4516 count = 0;
4517 cntdn = 1000 * timeout;
4518 do {
4519 doorbell_reg = readl(&ioc->chip->Doorbell);
4520 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
4521 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4522 "%s: successful count(%d), timeout(%d)\n",
4523 ioc->name, __func__, count, timeout));
4524 return 0;
4525 }
4526
4527 usleep_range(1000, 1500);
4528 count++;
4529 } while (--cntdn);
4530
4531 pr_err(MPT3SAS_FMT
4532 "%s: failed due to timeout count(%d), doorbell_reg(%x)!\n",
4533 ioc->name, __func__, count, doorbell_reg);
4534 return -EFAULT;
4535 }
4536
4537 /**
4538 * _base_send_ioc_reset - send doorbell reset
4539 * @ioc: per adapter object
4540 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
4541 * @timeout: timeout in second
4542 *
4543 * Returns 0 for success, non-zero for failure.
4544 */
4545 static int
4546 _base_send_ioc_reset(struct MPT3SAS_ADAPTER *ioc, u8 reset_type, int timeout)
4547 {
4548 u32 ioc_state;
4549 int r = 0;
4550
4551 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
4552 pr_err(MPT3SAS_FMT "%s: unknown reset_type\n",
4553 ioc->name, __func__);
4554 return -EFAULT;
4555 }
4556
4557 if (!(ioc->facts.IOCCapabilities &
4558 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
4559 return -EFAULT;
4560
4561 pr_info(MPT3SAS_FMT "sending message unit reset !!\n", ioc->name);
4562
4563 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
4564 &ioc->chip->Doorbell);
4565 if ((_base_wait_for_doorbell_ack(ioc, 15))) {
4566 r = -EFAULT;
4567 goto out;
4568 }
4569 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
4570 if (ioc_state) {
4571 pr_err(MPT3SAS_FMT
4572 "%s: failed going to ready state (ioc_state=0x%x)\n",
4573 ioc->name, __func__, ioc_state);
4574 r = -EFAULT;
4575 goto out;
4576 }
4577 out:
4578 pr_info(MPT3SAS_FMT "message unit reset: %s\n",
4579 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
4580 return r;
4581 }
4582
4583 /**
4584 * _base_handshake_req_reply_wait - send request thru doorbell interface
4585 * @ioc: per adapter object
4586 * @request_bytes: request length
4587 * @request: pointer having request payload
4588 * @reply_bytes: reply length
4589 * @reply: pointer to reply payload
4590 * @timeout: timeout in second
4591 *
4592 * Returns 0 for success, non-zero for failure.
4593 */
4594 static int
4595 _base_handshake_req_reply_wait(struct MPT3SAS_ADAPTER *ioc, int request_bytes,
4596 u32 *request, int reply_bytes, u16 *reply, int timeout)
4597 {
4598 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
4599 int i;
4600 u8 failed;
4601 __le32 *mfp;
4602
4603 /* make sure doorbell is not in use */
4604 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
4605 pr_err(MPT3SAS_FMT
4606 "doorbell is in use (line=%d)\n",
4607 ioc->name, __LINE__);
4608 return -EFAULT;
4609 }
4610
4611 /* clear pending doorbell interrupts from previous state changes */
4612 if (readl(&ioc->chip->HostInterruptStatus) &
4613 MPI2_HIS_IOC2SYS_DB_STATUS)
4614 writel(0, &ioc->chip->HostInterruptStatus);
4615
4616 /* send message to ioc */
4617 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
4618 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
4619 &ioc->chip->Doorbell);
4620
4621 if ((_base_spin_on_doorbell_int(ioc, 5))) {
4622 pr_err(MPT3SAS_FMT
4623 "doorbell handshake int failed (line=%d)\n",
4624 ioc->name, __LINE__);
4625 return -EFAULT;
4626 }
4627 writel(0, &ioc->chip->HostInterruptStatus);
4628
4629 if ((_base_wait_for_doorbell_ack(ioc, 5))) {
4630 pr_err(MPT3SAS_FMT
4631 "doorbell handshake ack failed (line=%d)\n",
4632 ioc->name, __LINE__);
4633 return -EFAULT;
4634 }
4635
4636 /* send message 32-bits at a time */
4637 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
4638 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
4639 if ((_base_wait_for_doorbell_ack(ioc, 5)))
4640 failed = 1;
4641 }
4642
4643 if (failed) {
4644 pr_err(MPT3SAS_FMT
4645 "doorbell handshake sending request failed (line=%d)\n",
4646 ioc->name, __LINE__);
4647 return -EFAULT;
4648 }
4649
4650 /* now wait for the reply */
4651 if ((_base_wait_for_doorbell_int(ioc, timeout))) {
4652 pr_err(MPT3SAS_FMT
4653 "doorbell handshake int failed (line=%d)\n",
4654 ioc->name, __LINE__);
4655 return -EFAULT;
4656 }
4657
4658 /* read the first two 16-bits, it gives the total length of the reply */
4659 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4660 & MPI2_DOORBELL_DATA_MASK);
4661 writel(0, &ioc->chip->HostInterruptStatus);
4662 if ((_base_wait_for_doorbell_int(ioc, 5))) {
4663 pr_err(MPT3SAS_FMT
4664 "doorbell handshake int failed (line=%d)\n",
4665 ioc->name, __LINE__);
4666 return -EFAULT;
4667 }
4668 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4669 & MPI2_DOORBELL_DATA_MASK);
4670 writel(0, &ioc->chip->HostInterruptStatus);
4671
4672 for (i = 2; i < default_reply->MsgLength * 2; i++) {
4673 if ((_base_wait_for_doorbell_int(ioc, 5))) {
4674 pr_err(MPT3SAS_FMT
4675 "doorbell handshake int failed (line=%d)\n",
4676 ioc->name, __LINE__);
4677 return -EFAULT;
4678 }
4679 if (i >= reply_bytes/2) /* overflow case */
4680 readl(&ioc->chip->Doorbell);
4681 else
4682 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
4683 & MPI2_DOORBELL_DATA_MASK);
4684 writel(0, &ioc->chip->HostInterruptStatus);
4685 }
4686
4687 _base_wait_for_doorbell_int(ioc, 5);
4688 if (_base_wait_for_doorbell_not_used(ioc, 5) != 0) {
4689 dhsprintk(ioc, pr_info(MPT3SAS_FMT
4690 "doorbell is in use (line=%d)\n", ioc->name, __LINE__));
4691 }
4692 writel(0, &ioc->chip->HostInterruptStatus);
4693
4694 if (ioc->logging_level & MPT_DEBUG_INIT) {
4695 mfp = (__le32 *)reply;
4696 pr_info("\toffset:data\n");
4697 for (i = 0; i < reply_bytes/4; i++)
4698 pr_info("\t[0x%02x]:%08x\n", i*4,
4699 le32_to_cpu(mfp[i]));
4700 }
4701 return 0;
4702 }
4703
4704 /**
4705 * mpt3sas_base_sas_iounit_control - send sas iounit control to FW
4706 * @ioc: per adapter object
4707 * @mpi_reply: the reply payload from FW
4708 * @mpi_request: the request payload sent to FW
4709 *
4710 * The SAS IO Unit Control Request message allows the host to perform low-level
4711 * operations, such as resets on the PHYs of the IO Unit, also allows the host
4712 * to obtain the IOC assigned device handles for a device if it has other
4713 * identifying information about the device, in addition allows the host to
4714 * remove IOC resources associated with the device.
4715 *
4716 * Returns 0 for success, non-zero for failure.
4717 */
4718 int
4719 mpt3sas_base_sas_iounit_control(struct MPT3SAS_ADAPTER *ioc,
4720 Mpi2SasIoUnitControlReply_t *mpi_reply,
4721 Mpi2SasIoUnitControlRequest_t *mpi_request)
4722 {
4723 u16 smid;
4724 u32 ioc_state;
4725 bool issue_reset = false;
4726 int rc;
4727 void *request;
4728 u16 wait_state_count;
4729
4730 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4731 __func__));
4732
4733 mutex_lock(&ioc->base_cmds.mutex);
4734
4735 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4736 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4737 ioc->name, __func__);
4738 rc = -EAGAIN;
4739 goto out;
4740 }
4741
4742 wait_state_count = 0;
4743 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4744 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4745 if (wait_state_count++ == 10) {
4746 pr_err(MPT3SAS_FMT
4747 "%s: failed due to ioc not operational\n",
4748 ioc->name, __func__);
4749 rc = -EFAULT;
4750 goto out;
4751 }
4752 ssleep(1);
4753 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4754 pr_info(MPT3SAS_FMT
4755 "%s: waiting for operational state(count=%d)\n",
4756 ioc->name, __func__, wait_state_count);
4757 }
4758
4759 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4760 if (!smid) {
4761 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4762 ioc->name, __func__);
4763 rc = -EAGAIN;
4764 goto out;
4765 }
4766
4767 rc = 0;
4768 ioc->base_cmds.status = MPT3_CMD_PENDING;
4769 request = mpt3sas_base_get_msg_frame(ioc, smid);
4770 ioc->base_cmds.smid = smid;
4771 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
4772 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4773 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
4774 ioc->ioc_link_reset_in_progress = 1;
4775 init_completion(&ioc->base_cmds.done);
4776 ioc->put_smid_default(ioc, smid);
4777 wait_for_completion_timeout(&ioc->base_cmds.done,
4778 msecs_to_jiffies(10000));
4779 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
4780 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
4781 ioc->ioc_link_reset_in_progress)
4782 ioc->ioc_link_reset_in_progress = 0;
4783 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4784 pr_err(MPT3SAS_FMT "%s: timeout\n",
4785 ioc->name, __func__);
4786 _debug_dump_mf(mpi_request,
4787 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
4788 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
4789 issue_reset = true;
4790 goto issue_host_reset;
4791 }
4792 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4793 memcpy(mpi_reply, ioc->base_cmds.reply,
4794 sizeof(Mpi2SasIoUnitControlReply_t));
4795 else
4796 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
4797 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4798 goto out;
4799
4800 issue_host_reset:
4801 if (issue_reset)
4802 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
4803 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4804 rc = -EFAULT;
4805 out:
4806 mutex_unlock(&ioc->base_cmds.mutex);
4807 return rc;
4808 }
4809
4810 /**
4811 * mpt3sas_base_scsi_enclosure_processor - sending request to sep device
4812 * @ioc: per adapter object
4813 * @mpi_reply: the reply payload from FW
4814 * @mpi_request: the request payload sent to FW
4815 *
4816 * The SCSI Enclosure Processor request message causes the IOC to
4817 * communicate with SES devices to control LED status signals.
4818 *
4819 * Returns 0 for success, non-zero for failure.
4820 */
4821 int
4822 mpt3sas_base_scsi_enclosure_processor(struct MPT3SAS_ADAPTER *ioc,
4823 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
4824 {
4825 u16 smid;
4826 u32 ioc_state;
4827 bool issue_reset = false;
4828 int rc;
4829 void *request;
4830 u16 wait_state_count;
4831
4832 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4833 __func__));
4834
4835 mutex_lock(&ioc->base_cmds.mutex);
4836
4837 if (ioc->base_cmds.status != MPT3_CMD_NOT_USED) {
4838 pr_err(MPT3SAS_FMT "%s: base_cmd in use\n",
4839 ioc->name, __func__);
4840 rc = -EAGAIN;
4841 goto out;
4842 }
4843
4844 wait_state_count = 0;
4845 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4846 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
4847 if (wait_state_count++ == 10) {
4848 pr_err(MPT3SAS_FMT
4849 "%s: failed due to ioc not operational\n",
4850 ioc->name, __func__);
4851 rc = -EFAULT;
4852 goto out;
4853 }
4854 ssleep(1);
4855 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
4856 pr_info(MPT3SAS_FMT
4857 "%s: waiting for operational state(count=%d)\n",
4858 ioc->name,
4859 __func__, wait_state_count);
4860 }
4861
4862 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
4863 if (!smid) {
4864 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4865 ioc->name, __func__);
4866 rc = -EAGAIN;
4867 goto out;
4868 }
4869
4870 rc = 0;
4871 ioc->base_cmds.status = MPT3_CMD_PENDING;
4872 request = mpt3sas_base_get_msg_frame(ioc, smid);
4873 ioc->base_cmds.smid = smid;
4874 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
4875 init_completion(&ioc->base_cmds.done);
4876 ioc->put_smid_default(ioc, smid);
4877 wait_for_completion_timeout(&ioc->base_cmds.done,
4878 msecs_to_jiffies(10000));
4879 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
4880 pr_err(MPT3SAS_FMT "%s: timeout\n",
4881 ioc->name, __func__);
4882 _debug_dump_mf(mpi_request,
4883 sizeof(Mpi2SepRequest_t)/4);
4884 if (!(ioc->base_cmds.status & MPT3_CMD_RESET))
4885 issue_reset = false;
4886 goto issue_host_reset;
4887 }
4888 if (ioc->base_cmds.status & MPT3_CMD_REPLY_VALID)
4889 memcpy(mpi_reply, ioc->base_cmds.reply,
4890 sizeof(Mpi2SepReply_t));
4891 else
4892 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
4893 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4894 goto out;
4895
4896 issue_host_reset:
4897 if (issue_reset)
4898 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
4899 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
4900 rc = -EFAULT;
4901 out:
4902 mutex_unlock(&ioc->base_cmds.mutex);
4903 return rc;
4904 }
4905
4906 /**
4907 * _base_get_port_facts - obtain port facts reply and save in ioc
4908 * @ioc: per adapter object
4909 *
4910 * Returns 0 for success, non-zero for failure.
4911 */
4912 static int
4913 _base_get_port_facts(struct MPT3SAS_ADAPTER *ioc, int port)
4914 {
4915 Mpi2PortFactsRequest_t mpi_request;
4916 Mpi2PortFactsReply_t mpi_reply;
4917 struct mpt3sas_port_facts *pfacts;
4918 int mpi_reply_sz, mpi_request_sz, r;
4919
4920 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
4921 __func__));
4922
4923 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
4924 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
4925 memset(&mpi_request, 0, mpi_request_sz);
4926 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
4927 mpi_request.PortNumber = port;
4928 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
4929 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
4930
4931 if (r != 0) {
4932 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
4933 ioc->name, __func__, r);
4934 return r;
4935 }
4936
4937 pfacts = &ioc->pfacts[port];
4938 memset(pfacts, 0, sizeof(struct mpt3sas_port_facts));
4939 pfacts->PortNumber = mpi_reply.PortNumber;
4940 pfacts->VP_ID = mpi_reply.VP_ID;
4941 pfacts->VF_ID = mpi_reply.VF_ID;
4942 pfacts->MaxPostedCmdBuffers =
4943 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
4944
4945 return 0;
4946 }
4947
4948 /**
4949 * _base_wait_for_iocstate - Wait until the card is in READY or OPERATIONAL
4950 * @ioc: per adapter object
4951 * @timeout:
4952 *
4953 * Returns 0 for success, non-zero for failure.
4954 */
4955 static int
4956 _base_wait_for_iocstate(struct MPT3SAS_ADAPTER *ioc, int timeout)
4957 {
4958 u32 ioc_state;
4959 int rc;
4960
4961 dinitprintk(ioc, printk(MPT3SAS_FMT "%s\n", ioc->name,
4962 __func__));
4963
4964 if (ioc->pci_error_recovery) {
4965 dfailprintk(ioc, printk(MPT3SAS_FMT
4966 "%s: host in pci error recovery\n", ioc->name, __func__));
4967 return -EFAULT;
4968 }
4969
4970 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
4971 dhsprintk(ioc, printk(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
4972 ioc->name, __func__, ioc_state));
4973
4974 if (((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY) ||
4975 (ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4976 return 0;
4977
4978 if (ioc_state & MPI2_DOORBELL_USED) {
4979 dhsprintk(ioc, printk(MPT3SAS_FMT
4980 "unexpected doorbell active!\n", ioc->name));
4981 goto issue_diag_reset;
4982 }
4983
4984 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4985 mpt3sas_base_fault_info(ioc, ioc_state &
4986 MPI2_DOORBELL_DATA_MASK);
4987 goto issue_diag_reset;
4988 }
4989
4990 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, timeout);
4991 if (ioc_state) {
4992 dfailprintk(ioc, printk(MPT3SAS_FMT
4993 "%s: failed going to ready state (ioc_state=0x%x)\n",
4994 ioc->name, __func__, ioc_state));
4995 return -EFAULT;
4996 }
4997
4998 issue_diag_reset:
4999 rc = _base_diag_reset(ioc);
5000 return rc;
5001 }
5002
5003 /**
5004 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
5005 * @ioc: per adapter object
5006 *
5007 * Returns 0 for success, non-zero for failure.
5008 */
5009 static int
5010 _base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc)
5011 {
5012 Mpi2IOCFactsRequest_t mpi_request;
5013 Mpi2IOCFactsReply_t mpi_reply;
5014 struct mpt3sas_facts *facts;
5015 int mpi_reply_sz, mpi_request_sz, r;
5016
5017 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5018 __func__));
5019
5020 r = _base_wait_for_iocstate(ioc, 10);
5021 if (r) {
5022 dfailprintk(ioc, printk(MPT3SAS_FMT
5023 "%s: failed getting to correct state\n",
5024 ioc->name, __func__));
5025 return r;
5026 }
5027 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
5028 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
5029 memset(&mpi_request, 0, mpi_request_sz);
5030 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
5031 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
5032 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5);
5033
5034 if (r != 0) {
5035 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5036 ioc->name, __func__, r);
5037 return r;
5038 }
5039
5040 facts = &ioc->facts;
5041 memset(facts, 0, sizeof(struct mpt3sas_facts));
5042 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
5043 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
5044 facts->VP_ID = mpi_reply.VP_ID;
5045 facts->VF_ID = mpi_reply.VF_ID;
5046 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
5047 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
5048 facts->WhoInit = mpi_reply.WhoInit;
5049 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
5050 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
5051 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
5052 facts->MaxReplyDescriptorPostQueueDepth =
5053 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
5054 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
5055 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
5056 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
5057 ioc->ir_firmware = 1;
5058 if ((facts->IOCCapabilities &
5059 MPI2_IOCFACTS_CAPABILITY_RDPQ_ARRAY_CAPABLE) && (!reset_devices))
5060 ioc->rdpq_array_capable = 1;
5061 if (facts->IOCCapabilities & MPI26_IOCFACTS_CAPABILITY_ATOMIC_REQ)
5062 ioc->atomic_desc_capable = 1;
5063 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
5064 facts->IOCRequestFrameSize =
5065 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
5066 if (ioc->hba_mpi_version_belonged != MPI2_VERSION) {
5067 facts->IOCMaxChainSegmentSize =
5068 le16_to_cpu(mpi_reply.IOCMaxChainSegmentSize);
5069 }
5070 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
5071 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
5072 ioc->shost->max_id = -1;
5073 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
5074 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
5075 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
5076 facts->HighPriorityCredit =
5077 le16_to_cpu(mpi_reply.HighPriorityCredit);
5078 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
5079 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
5080 facts->CurrentHostPageSize = mpi_reply.CurrentHostPageSize;
5081
5082 /*
5083 * Get the Page Size from IOC Facts. If it's 0, default to 4k.
5084 */
5085 ioc->page_size = 1 << facts->CurrentHostPageSize;
5086 if (ioc->page_size == 1) {
5087 pr_info(MPT3SAS_FMT "CurrentHostPageSize is 0: Setting "
5088 "default host page size to 4k\n", ioc->name);
5089 ioc->page_size = 1 << MPT3SAS_HOST_PAGE_SIZE_4K;
5090 }
5091 dinitprintk(ioc, pr_info(MPT3SAS_FMT "CurrentHostPageSize(%d)\n",
5092 ioc->name, facts->CurrentHostPageSize));
5093
5094 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5095 "hba queue depth(%d), max chains per io(%d)\n",
5096 ioc->name, facts->RequestCredit,
5097 facts->MaxChainDepth));
5098 dinitprintk(ioc, pr_info(MPT3SAS_FMT
5099 "request frame size(%d), reply frame size(%d)\n", ioc->name,
5100 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
5101 return 0;
5102 }
5103
5104 /**
5105 * _base_send_ioc_init - send ioc_init to firmware
5106 * @ioc: per adapter object
5107 *
5108 * Returns 0 for success, non-zero for failure.
5109 */
5110 static int
5111 _base_send_ioc_init(struct MPT3SAS_ADAPTER *ioc)
5112 {
5113 Mpi2IOCInitRequest_t mpi_request;
5114 Mpi2IOCInitReply_t mpi_reply;
5115 int i, r = 0;
5116 ktime_t current_time;
5117 u16 ioc_status;
5118 u32 reply_post_free_array_sz = 0;
5119 Mpi2IOCInitRDPQArrayEntry *reply_post_free_array = NULL;
5120 dma_addr_t reply_post_free_array_dma;
5121
5122 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5123 __func__));
5124
5125 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
5126 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
5127 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
5128 mpi_request.VF_ID = 0; /* TODO */
5129 mpi_request.VP_ID = 0;
5130 mpi_request.MsgVersion = cpu_to_le16(ioc->hba_mpi_version_belonged);
5131 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
5132 mpi_request.HostPageSize = MPT3SAS_HOST_PAGE_SIZE_4K;
5133
5134 if (_base_is_controller_msix_enabled(ioc))
5135 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
5136 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
5137 mpi_request.ReplyDescriptorPostQueueDepth =
5138 cpu_to_le16(ioc->reply_post_queue_depth);
5139 mpi_request.ReplyFreeQueueDepth =
5140 cpu_to_le16(ioc->reply_free_queue_depth);
5141
5142 mpi_request.SenseBufferAddressHigh =
5143 cpu_to_le32((u64)ioc->sense_dma >> 32);
5144 mpi_request.SystemReplyAddressHigh =
5145 cpu_to_le32((u64)ioc->reply_dma >> 32);
5146 mpi_request.SystemRequestFrameBaseAddress =
5147 cpu_to_le64((u64)ioc->request_dma);
5148 mpi_request.ReplyFreeQueueAddress =
5149 cpu_to_le64((u64)ioc->reply_free_dma);
5150
5151 if (ioc->rdpq_array_enable) {
5152 reply_post_free_array_sz = ioc->reply_queue_count *
5153 sizeof(Mpi2IOCInitRDPQArrayEntry);
5154 reply_post_free_array = pci_alloc_consistent(ioc->pdev,
5155 reply_post_free_array_sz, &reply_post_free_array_dma);
5156 if (!reply_post_free_array) {
5157 pr_err(MPT3SAS_FMT
5158 "reply_post_free_array: pci_alloc_consistent failed\n",
5159 ioc->name);
5160 r = -ENOMEM;
5161 goto out;
5162 }
5163 memset(reply_post_free_array, 0, reply_post_free_array_sz);
5164 for (i = 0; i < ioc->reply_queue_count; i++)
5165 reply_post_free_array[i].RDPQBaseAddress =
5166 cpu_to_le64(
5167 (u64)ioc->reply_post[i].reply_post_free_dma);
5168 mpi_request.MsgFlags = MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE;
5169 mpi_request.ReplyDescriptorPostQueueAddress =
5170 cpu_to_le64((u64)reply_post_free_array_dma);
5171 } else {
5172 mpi_request.ReplyDescriptorPostQueueAddress =
5173 cpu_to_le64((u64)ioc->reply_post[0].reply_post_free_dma);
5174 }
5175
5176 /* This time stamp specifies number of milliseconds
5177 * since epoch ~ midnight January 1, 1970.
5178 */
5179 current_time = ktime_get_real();
5180 mpi_request.TimeStamp = cpu_to_le64(ktime_to_ms(current_time));
5181
5182 if (ioc->logging_level & MPT_DEBUG_INIT) {
5183 __le32 *mfp;
5184 int i;
5185
5186 mfp = (__le32 *)&mpi_request;
5187 pr_info("\toffset:data\n");
5188 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
5189 pr_info("\t[0x%02x]:%08x\n", i*4,
5190 le32_to_cpu(mfp[i]));
5191 }
5192
5193 r = _base_handshake_req_reply_wait(ioc,
5194 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
5195 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10);
5196
5197 if (r != 0) {
5198 pr_err(MPT3SAS_FMT "%s: handshake failed (r=%d)\n",
5199 ioc->name, __func__, r);
5200 goto out;
5201 }
5202
5203 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5204 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
5205 mpi_reply.IOCLogInfo) {
5206 pr_err(MPT3SAS_FMT "%s: failed\n", ioc->name, __func__);
5207 r = -EIO;
5208 }
5209
5210 out:
5211 if (reply_post_free_array)
5212 pci_free_consistent(ioc->pdev, reply_post_free_array_sz,
5213 reply_post_free_array,
5214 reply_post_free_array_dma);
5215 return r;
5216 }
5217
5218 /**
5219 * mpt3sas_port_enable_done - command completion routine for port enable
5220 * @ioc: per adapter object
5221 * @smid: system request message index
5222 * @msix_index: MSIX table index supplied by the OS
5223 * @reply: reply message frame(lower 32bit addr)
5224 *
5225 * Return 1 meaning mf should be freed from _base_interrupt
5226 * 0 means the mf is freed from this function.
5227 */
5228 u8
5229 mpt3sas_port_enable_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
5230 u32 reply)
5231 {
5232 MPI2DefaultReply_t *mpi_reply;
5233 u16 ioc_status;
5234
5235 if (ioc->port_enable_cmds.status == MPT3_CMD_NOT_USED)
5236 return 1;
5237
5238 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5239 if (!mpi_reply)
5240 return 1;
5241
5242 if (mpi_reply->Function != MPI2_FUNCTION_PORT_ENABLE)
5243 return 1;
5244
5245 ioc->port_enable_cmds.status &= ~MPT3_CMD_PENDING;
5246 ioc->port_enable_cmds.status |= MPT3_CMD_COMPLETE;
5247 ioc->port_enable_cmds.status |= MPT3_CMD_REPLY_VALID;
5248 memcpy(ioc->port_enable_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
5249 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5250 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5251 ioc->port_enable_failed = 1;
5252
5253 if (ioc->is_driver_loading) {
5254 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
5255 mpt3sas_port_enable_complete(ioc);
5256 return 1;
5257 } else {
5258 ioc->start_scan_failed = ioc_status;
5259 ioc->start_scan = 0;
5260 return 1;
5261 }
5262 }
5263 complete(&ioc->port_enable_cmds.done);
5264 return 1;
5265 }
5266
5267 /**
5268 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
5269 * @ioc: per adapter object
5270 *
5271 * Returns 0 for success, non-zero for failure.
5272 */
5273 static int
5274 _base_send_port_enable(struct MPT3SAS_ADAPTER *ioc)
5275 {
5276 Mpi2PortEnableRequest_t *mpi_request;
5277 Mpi2PortEnableReply_t *mpi_reply;
5278 int r = 0;
5279 u16 smid;
5280 u16 ioc_status;
5281
5282 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5283
5284 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5285 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5286 ioc->name, __func__);
5287 return -EAGAIN;
5288 }
5289
5290 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5291 if (!smid) {
5292 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5293 ioc->name, __func__);
5294 return -EAGAIN;
5295 }
5296
5297 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5298 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5299 ioc->port_enable_cmds.smid = smid;
5300 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5301 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5302
5303 init_completion(&ioc->port_enable_cmds.done);
5304 ioc->put_smid_default(ioc, smid);
5305 wait_for_completion_timeout(&ioc->port_enable_cmds.done, 300*HZ);
5306 if (!(ioc->port_enable_cmds.status & MPT3_CMD_COMPLETE)) {
5307 pr_err(MPT3SAS_FMT "%s: timeout\n",
5308 ioc->name, __func__);
5309 _debug_dump_mf(mpi_request,
5310 sizeof(Mpi2PortEnableRequest_t)/4);
5311 if (ioc->port_enable_cmds.status & MPT3_CMD_RESET)
5312 r = -EFAULT;
5313 else
5314 r = -ETIME;
5315 goto out;
5316 }
5317
5318 mpi_reply = ioc->port_enable_cmds.reply;
5319 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
5320 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5321 pr_err(MPT3SAS_FMT "%s: failed with (ioc_status=0x%08x)\n",
5322 ioc->name, __func__, ioc_status);
5323 r = -EFAULT;
5324 goto out;
5325 }
5326
5327 out:
5328 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
5329 pr_info(MPT3SAS_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
5330 "SUCCESS" : "FAILED"));
5331 return r;
5332 }
5333
5334 /**
5335 * mpt3sas_port_enable - initiate firmware discovery (don't wait for reply)
5336 * @ioc: per adapter object
5337 *
5338 * Returns 0 for success, non-zero for failure.
5339 */
5340 int
5341 mpt3sas_port_enable(struct MPT3SAS_ADAPTER *ioc)
5342 {
5343 Mpi2PortEnableRequest_t *mpi_request;
5344 u16 smid;
5345
5346 pr_info(MPT3SAS_FMT "sending port enable !!\n", ioc->name);
5347
5348 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
5349 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5350 ioc->name, __func__);
5351 return -EAGAIN;
5352 }
5353
5354 smid = mpt3sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
5355 if (!smid) {
5356 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5357 ioc->name, __func__);
5358 return -EAGAIN;
5359 }
5360
5361 ioc->port_enable_cmds.status = MPT3_CMD_PENDING;
5362 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5363 ioc->port_enable_cmds.smid = smid;
5364 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
5365 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
5366
5367 ioc->put_smid_default(ioc, smid);
5368 return 0;
5369 }
5370
5371 /**
5372 * _base_determine_wait_on_discovery - desposition
5373 * @ioc: per adapter object
5374 *
5375 * Decide whether to wait on discovery to complete. Used to either
5376 * locate boot device, or report volumes ahead of physical devices.
5377 *
5378 * Returns 1 for wait, 0 for don't wait
5379 */
5380 static int
5381 _base_determine_wait_on_discovery(struct MPT3SAS_ADAPTER *ioc)
5382 {
5383 /* We wait for discovery to complete if IR firmware is loaded.
5384 * The sas topology events arrive before PD events, so we need time to
5385 * turn on the bit in ioc->pd_handles to indicate PD
5386 * Also, it maybe required to report Volumes ahead of physical
5387 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
5388 */
5389 if (ioc->ir_firmware)
5390 return 1;
5391
5392 /* if no Bios, then we don't need to wait */
5393 if (!ioc->bios_pg3.BiosVersion)
5394 return 0;
5395
5396 /* Bios is present, then we drop down here.
5397 *
5398 * If there any entries in the Bios Page 2, then we wait
5399 * for discovery to complete.
5400 */
5401
5402 /* Current Boot Device */
5403 if ((ioc->bios_pg2.CurrentBootDeviceForm &
5404 MPI2_BIOSPAGE2_FORM_MASK) ==
5405 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5406 /* Request Boot Device */
5407 (ioc->bios_pg2.ReqBootDeviceForm &
5408 MPI2_BIOSPAGE2_FORM_MASK) ==
5409 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
5410 /* Alternate Request Boot Device */
5411 (ioc->bios_pg2.ReqAltBootDeviceForm &
5412 MPI2_BIOSPAGE2_FORM_MASK) ==
5413 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
5414 return 0;
5415
5416 return 1;
5417 }
5418
5419 /**
5420 * _base_unmask_events - turn on notification for this event
5421 * @ioc: per adapter object
5422 * @event: firmware event
5423 *
5424 * The mask is stored in ioc->event_masks.
5425 */
5426 static void
5427 _base_unmask_events(struct MPT3SAS_ADAPTER *ioc, u16 event)
5428 {
5429 u32 desired_event;
5430
5431 if (event >= 128)
5432 return;
5433
5434 desired_event = (1 << (event % 32));
5435
5436 if (event < 32)
5437 ioc->event_masks[0] &= ~desired_event;
5438 else if (event < 64)
5439 ioc->event_masks[1] &= ~desired_event;
5440 else if (event < 96)
5441 ioc->event_masks[2] &= ~desired_event;
5442 else if (event < 128)
5443 ioc->event_masks[3] &= ~desired_event;
5444 }
5445
5446 /**
5447 * _base_event_notification - send event notification
5448 * @ioc: per adapter object
5449 *
5450 * Returns 0 for success, non-zero for failure.
5451 */
5452 static int
5453 _base_event_notification(struct MPT3SAS_ADAPTER *ioc)
5454 {
5455 Mpi2EventNotificationRequest_t *mpi_request;
5456 u16 smid;
5457 int r = 0;
5458 int i;
5459
5460 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5461 __func__));
5462
5463 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
5464 pr_err(MPT3SAS_FMT "%s: internal command already in use\n",
5465 ioc->name, __func__);
5466 return -EAGAIN;
5467 }
5468
5469 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
5470 if (!smid) {
5471 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5472 ioc->name, __func__);
5473 return -EAGAIN;
5474 }
5475 ioc->base_cmds.status = MPT3_CMD_PENDING;
5476 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5477 ioc->base_cmds.smid = smid;
5478 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
5479 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
5480 mpi_request->VF_ID = 0; /* TODO */
5481 mpi_request->VP_ID = 0;
5482 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
5483 mpi_request->EventMasks[i] =
5484 cpu_to_le32(ioc->event_masks[i]);
5485 init_completion(&ioc->base_cmds.done);
5486 ioc->put_smid_default(ioc, smid);
5487 wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
5488 if (!(ioc->base_cmds.status & MPT3_CMD_COMPLETE)) {
5489 pr_err(MPT3SAS_FMT "%s: timeout\n",
5490 ioc->name, __func__);
5491 _debug_dump_mf(mpi_request,
5492 sizeof(Mpi2EventNotificationRequest_t)/4);
5493 if (ioc->base_cmds.status & MPT3_CMD_RESET)
5494 r = -EFAULT;
5495 else
5496 r = -ETIME;
5497 } else
5498 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s: complete\n",
5499 ioc->name, __func__));
5500 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
5501 return r;
5502 }
5503
5504 /**
5505 * mpt3sas_base_validate_event_type - validating event types
5506 * @ioc: per adapter object
5507 * @event: firmware event
5508 *
5509 * This will turn on firmware event notification when application
5510 * ask for that event. We don't mask events that are already enabled.
5511 */
5512 void
5513 mpt3sas_base_validate_event_type(struct MPT3SAS_ADAPTER *ioc, u32 *event_type)
5514 {
5515 int i, j;
5516 u32 event_mask, desired_event;
5517 u8 send_update_to_fw;
5518
5519 for (i = 0, send_update_to_fw = 0; i <
5520 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
5521 event_mask = ~event_type[i];
5522 desired_event = 1;
5523 for (j = 0; j < 32; j++) {
5524 if (!(event_mask & desired_event) &&
5525 (ioc->event_masks[i] & desired_event)) {
5526 ioc->event_masks[i] &= ~desired_event;
5527 send_update_to_fw = 1;
5528 }
5529 desired_event = (desired_event << 1);
5530 }
5531 }
5532
5533 if (!send_update_to_fw)
5534 return;
5535
5536 mutex_lock(&ioc->base_cmds.mutex);
5537 _base_event_notification(ioc);
5538 mutex_unlock(&ioc->base_cmds.mutex);
5539 }
5540
5541 /**
5542 * _base_diag_reset - the "big hammer" start of day reset
5543 * @ioc: per adapter object
5544 *
5545 * Returns 0 for success, non-zero for failure.
5546 */
5547 static int
5548 _base_diag_reset(struct MPT3SAS_ADAPTER *ioc)
5549 {
5550 u32 host_diagnostic;
5551 u32 ioc_state;
5552 u32 count;
5553 u32 hcb_size;
5554
5555 pr_info(MPT3SAS_FMT "sending diag reset !!\n", ioc->name);
5556
5557 drsprintk(ioc, pr_info(MPT3SAS_FMT "clear interrupts\n",
5558 ioc->name));
5559
5560 count = 0;
5561 do {
5562 /* Write magic sequence to WriteSequence register
5563 * Loop until in diagnostic mode
5564 */
5565 drsprintk(ioc, pr_info(MPT3SAS_FMT
5566 "write magic sequence\n", ioc->name));
5567 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
5568 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
5569 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
5570 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
5571 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
5572 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
5573 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
5574
5575 /* wait 100 msec */
5576 msleep(100);
5577
5578 if (count++ > 20)
5579 goto out;
5580
5581 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
5582 drsprintk(ioc, pr_info(MPT3SAS_FMT
5583 "wrote magic sequence: count(%d), host_diagnostic(0x%08x)\n",
5584 ioc->name, count, host_diagnostic));
5585
5586 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
5587
5588 hcb_size = readl(&ioc->chip->HCBSize);
5589
5590 drsprintk(ioc, pr_info(MPT3SAS_FMT "diag reset: issued\n",
5591 ioc->name));
5592 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
5593 &ioc->chip->HostDiagnostic);
5594
5595 /*This delay allows the chip PCIe hardware time to finish reset tasks*/
5596 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
5597
5598 /* Approximately 300 second max wait */
5599 for (count = 0; count < (300000000 /
5600 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
5601
5602 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
5603
5604 if (host_diagnostic == 0xFFFFFFFF)
5605 goto out;
5606 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
5607 break;
5608
5609 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC / 1000);
5610 }
5611
5612 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
5613
5614 drsprintk(ioc, pr_info(MPT3SAS_FMT
5615 "restart the adapter assuming the HCB Address points to good F/W\n",
5616 ioc->name));
5617 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
5618 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
5619 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
5620
5621 drsprintk(ioc, pr_info(MPT3SAS_FMT
5622 "re-enable the HCDW\n", ioc->name));
5623 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
5624 &ioc->chip->HCBSize);
5625 }
5626
5627 drsprintk(ioc, pr_info(MPT3SAS_FMT "restart the adapter\n",
5628 ioc->name));
5629 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
5630 &ioc->chip->HostDiagnostic);
5631
5632 drsprintk(ioc, pr_info(MPT3SAS_FMT
5633 "disable writes to the diagnostic register\n", ioc->name));
5634 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
5635
5636 drsprintk(ioc, pr_info(MPT3SAS_FMT
5637 "Wait for FW to go to the READY state\n", ioc->name));
5638 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20);
5639 if (ioc_state) {
5640 pr_err(MPT3SAS_FMT
5641 "%s: failed going to ready state (ioc_state=0x%x)\n",
5642 ioc->name, __func__, ioc_state);
5643 goto out;
5644 }
5645
5646 pr_info(MPT3SAS_FMT "diag reset: SUCCESS\n", ioc->name);
5647 return 0;
5648
5649 out:
5650 pr_err(MPT3SAS_FMT "diag reset: FAILED\n", ioc->name);
5651 return -EFAULT;
5652 }
5653
5654 /**
5655 * _base_make_ioc_ready - put controller in READY state
5656 * @ioc: per adapter object
5657 * @type: FORCE_BIG_HAMMER or SOFT_RESET
5658 *
5659 * Returns 0 for success, non-zero for failure.
5660 */
5661 static int
5662 _base_make_ioc_ready(struct MPT3SAS_ADAPTER *ioc, enum reset_type type)
5663 {
5664 u32 ioc_state;
5665 int rc;
5666 int count;
5667
5668 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5669 __func__));
5670
5671 if (ioc->pci_error_recovery)
5672 return 0;
5673
5674 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5675 dhsprintk(ioc, pr_info(MPT3SAS_FMT "%s: ioc_state(0x%08x)\n",
5676 ioc->name, __func__, ioc_state));
5677
5678 /* if in RESET state, it should move to READY state shortly */
5679 count = 0;
5680 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_RESET) {
5681 while ((ioc_state & MPI2_IOC_STATE_MASK) !=
5682 MPI2_IOC_STATE_READY) {
5683 if (count++ == 10) {
5684 pr_err(MPT3SAS_FMT
5685 "%s: failed going to ready state (ioc_state=0x%x)\n",
5686 ioc->name, __func__, ioc_state);
5687 return -EFAULT;
5688 }
5689 ssleep(1);
5690 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
5691 }
5692 }
5693
5694 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
5695 return 0;
5696
5697 if (ioc_state & MPI2_DOORBELL_USED) {
5698 dhsprintk(ioc, pr_info(MPT3SAS_FMT
5699 "unexpected doorbell active!\n",
5700 ioc->name));
5701 goto issue_diag_reset;
5702 }
5703
5704 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
5705 mpt3sas_base_fault_info(ioc, ioc_state &
5706 MPI2_DOORBELL_DATA_MASK);
5707 goto issue_diag_reset;
5708 }
5709
5710 if (type == FORCE_BIG_HAMMER)
5711 goto issue_diag_reset;
5712
5713 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
5714 if (!(_base_send_ioc_reset(ioc,
5715 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15))) {
5716 return 0;
5717 }
5718
5719 issue_diag_reset:
5720 rc = _base_diag_reset(ioc);
5721 return rc;
5722 }
5723
5724 /**
5725 * _base_make_ioc_operational - put controller in OPERATIONAL state
5726 * @ioc: per adapter object
5727 *
5728 * Returns 0 for success, non-zero for failure.
5729 */
5730 static int
5731 _base_make_ioc_operational(struct MPT3SAS_ADAPTER *ioc)
5732 {
5733 int r, i, index;
5734 unsigned long flags;
5735 u32 reply_address;
5736 u16 smid;
5737 struct _tr_list *delayed_tr, *delayed_tr_next;
5738 struct _sc_list *delayed_sc, *delayed_sc_next;
5739 struct _event_ack_list *delayed_event_ack, *delayed_event_ack_next;
5740 u8 hide_flag;
5741 struct adapter_reply_queue *reply_q;
5742 Mpi2ReplyDescriptorsUnion_t *reply_post_free_contig;
5743
5744 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5745 __func__));
5746
5747 /* clean the delayed target reset list */
5748 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5749 &ioc->delayed_tr_list, list) {
5750 list_del(&delayed_tr->list);
5751 kfree(delayed_tr);
5752 }
5753
5754
5755 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
5756 &ioc->delayed_tr_volume_list, list) {
5757 list_del(&delayed_tr->list);
5758 kfree(delayed_tr);
5759 }
5760
5761 list_for_each_entry_safe(delayed_sc, delayed_sc_next,
5762 &ioc->delayed_sc_list, list) {
5763 list_del(&delayed_sc->list);
5764 kfree(delayed_sc);
5765 }
5766
5767 list_for_each_entry_safe(delayed_event_ack, delayed_event_ack_next,
5768 &ioc->delayed_event_ack_list, list) {
5769 list_del(&delayed_event_ack->list);
5770 kfree(delayed_event_ack);
5771 }
5772
5773 /* initialize the scsi lookup free list */
5774 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5775 INIT_LIST_HEAD(&ioc->free_list);
5776 smid = 1;
5777 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
5778 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
5779 ioc->scsi_lookup[i].cb_idx = 0xFF;
5780 ioc->scsi_lookup[i].smid = smid;
5781 ioc->scsi_lookup[i].scmd = NULL;
5782 ioc->scsi_lookup[i].direct_io = 0;
5783 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
5784 &ioc->free_list);
5785 }
5786
5787 /* hi-priority queue */
5788 INIT_LIST_HEAD(&ioc->hpr_free_list);
5789 smid = ioc->hi_priority_smid;
5790 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
5791 ioc->hpr_lookup[i].cb_idx = 0xFF;
5792 ioc->hpr_lookup[i].smid = smid;
5793 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
5794 &ioc->hpr_free_list);
5795 }
5796
5797 /* internal queue */
5798 INIT_LIST_HEAD(&ioc->internal_free_list);
5799 smid = ioc->internal_smid;
5800 for (i = 0; i < ioc->internal_depth; i++, smid++) {
5801 ioc->internal_lookup[i].cb_idx = 0xFF;
5802 ioc->internal_lookup[i].smid = smid;
5803 list_add_tail(&ioc->internal_lookup[i].tracker_list,
5804 &ioc->internal_free_list);
5805 }
5806
5807 /* chain pool */
5808 INIT_LIST_HEAD(&ioc->free_chain_list);
5809 for (i = 0; i < ioc->chain_depth; i++)
5810 list_add_tail(&ioc->chain_lookup[i].tracker_list,
5811 &ioc->free_chain_list);
5812
5813 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5814
5815 /* initialize Reply Free Queue */
5816 for (i = 0, reply_address = (u32)ioc->reply_dma ;
5817 i < ioc->reply_free_queue_depth ; i++, reply_address +=
5818 ioc->reply_sz)
5819 ioc->reply_free[i] = cpu_to_le32(reply_address);
5820
5821 /* initialize reply queues */
5822 if (ioc->is_driver_loading)
5823 _base_assign_reply_queues(ioc);
5824
5825 /* initialize Reply Post Free Queue */
5826 index = 0;
5827 reply_post_free_contig = ioc->reply_post[0].reply_post_free;
5828 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5829 /*
5830 * If RDPQ is enabled, switch to the next allocation.
5831 * Otherwise advance within the contiguous region.
5832 */
5833 if (ioc->rdpq_array_enable) {
5834 reply_q->reply_post_free =
5835 ioc->reply_post[index++].reply_post_free;
5836 } else {
5837 reply_q->reply_post_free = reply_post_free_contig;
5838 reply_post_free_contig += ioc->reply_post_queue_depth;
5839 }
5840
5841 reply_q->reply_post_host_index = 0;
5842 for (i = 0; i < ioc->reply_post_queue_depth; i++)
5843 reply_q->reply_post_free[i].Words =
5844 cpu_to_le64(ULLONG_MAX);
5845 if (!_base_is_controller_msix_enabled(ioc))
5846 goto skip_init_reply_post_free_queue;
5847 }
5848 skip_init_reply_post_free_queue:
5849
5850 r = _base_send_ioc_init(ioc);
5851 if (r)
5852 return r;
5853
5854 /* initialize reply free host index */
5855 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
5856 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
5857
5858 /* initialize reply post host index */
5859 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
5860 if (ioc->combined_reply_queue)
5861 writel((reply_q->msix_index & 7)<<
5862 MPI2_RPHI_MSIX_INDEX_SHIFT,
5863 ioc->replyPostRegisterIndex[reply_q->msix_index/8]);
5864 else
5865 writel(reply_q->msix_index <<
5866 MPI2_RPHI_MSIX_INDEX_SHIFT,
5867 &ioc->chip->ReplyPostHostIndex);
5868
5869 if (!_base_is_controller_msix_enabled(ioc))
5870 goto skip_init_reply_post_host_index;
5871 }
5872
5873 skip_init_reply_post_host_index:
5874
5875 _base_unmask_interrupts(ioc);
5876 r = _base_event_notification(ioc);
5877 if (r)
5878 return r;
5879
5880 _base_static_config_pages(ioc);
5881
5882 if (ioc->is_driver_loading) {
5883
5884 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
5885 == 0x80) {
5886 hide_flag = (u8) (
5887 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
5888 MFG_PAGE10_HIDE_SSDS_MASK);
5889 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
5890 ioc->mfg_pg10_hide_flag = hide_flag;
5891 }
5892
5893 ioc->wait_for_discovery_to_complete =
5894 _base_determine_wait_on_discovery(ioc);
5895
5896 return r; /* scan_start and scan_finished support */
5897 }
5898
5899 r = _base_send_port_enable(ioc);
5900 if (r)
5901 return r;
5902
5903 return r;
5904 }
5905
5906 /**
5907 * mpt3sas_base_free_resources - free resources controller resources
5908 * @ioc: per adapter object
5909 *
5910 * Return nothing.
5911 */
5912 void
5913 mpt3sas_base_free_resources(struct MPT3SAS_ADAPTER *ioc)
5914 {
5915 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5916 __func__));
5917
5918 /* synchronizing freeing resource with pci_access_mutex lock */
5919 mutex_lock(&ioc->pci_access_mutex);
5920 if (ioc->chip_phys && ioc->chip) {
5921 _base_mask_interrupts(ioc);
5922 ioc->shost_recovery = 1;
5923 _base_make_ioc_ready(ioc, SOFT_RESET);
5924 ioc->shost_recovery = 0;
5925 }
5926
5927 mpt3sas_base_unmap_resources(ioc);
5928 mutex_unlock(&ioc->pci_access_mutex);
5929 return;
5930 }
5931
5932 /**
5933 * mpt3sas_base_attach - attach controller instance
5934 * @ioc: per adapter object
5935 *
5936 * Returns 0 for success, non-zero for failure.
5937 */
5938 int
5939 mpt3sas_base_attach(struct MPT3SAS_ADAPTER *ioc)
5940 {
5941 int r, i;
5942 int cpu_id, last_cpu_id = 0;
5943
5944 dinitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
5945 __func__));
5946
5947 /* setup cpu_msix_table */
5948 ioc->cpu_count = num_online_cpus();
5949 for_each_online_cpu(cpu_id)
5950 last_cpu_id = cpu_id;
5951 ioc->cpu_msix_table_sz = last_cpu_id + 1;
5952 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
5953 ioc->reply_queue_count = 1;
5954 if (!ioc->cpu_msix_table) {
5955 dfailprintk(ioc, pr_info(MPT3SAS_FMT
5956 "allocation for cpu_msix_table failed!!!\n",
5957 ioc->name));
5958 r = -ENOMEM;
5959 goto out_free_resources;
5960 }
5961
5962 if (ioc->is_warpdrive) {
5963 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
5964 sizeof(resource_size_t *), GFP_KERNEL);
5965 if (!ioc->reply_post_host_index) {
5966 dfailprintk(ioc, pr_info(MPT3SAS_FMT "allocation "
5967 "for reply_post_host_index failed!!!\n",
5968 ioc->name));
5969 r = -ENOMEM;
5970 goto out_free_resources;
5971 }
5972 }
5973
5974 ioc->rdpq_array_enable_assigned = 0;
5975 ioc->dma_mask = 0;
5976 r = mpt3sas_base_map_resources(ioc);
5977 if (r)
5978 goto out_free_resources;
5979
5980 pci_set_drvdata(ioc->pdev, ioc->shost);
5981 r = _base_get_ioc_facts(ioc);
5982 if (r)
5983 goto out_free_resources;
5984
5985 switch (ioc->hba_mpi_version_belonged) {
5986 case MPI2_VERSION:
5987 ioc->build_sg_scmd = &_base_build_sg_scmd;
5988 ioc->build_sg = &_base_build_sg;
5989 ioc->build_zero_len_sge = &_base_build_zero_len_sge;
5990 break;
5991 case MPI25_VERSION:
5992 case MPI26_VERSION:
5993 /*
5994 * In SAS3.0,
5995 * SCSI_IO, SMP_PASSTHRU, SATA_PASSTHRU, Target Assist, and
5996 * Target Status - all require the IEEE formated scatter gather
5997 * elements.
5998 */
5999 ioc->build_sg_scmd = &_base_build_sg_scmd_ieee;
6000 ioc->build_sg = &_base_build_sg_ieee;
6001 ioc->build_nvme_prp = &_base_build_nvme_prp;
6002 ioc->build_zero_len_sge = &_base_build_zero_len_sge_ieee;
6003 ioc->sge_size_ieee = sizeof(Mpi2IeeeSgeSimple64_t);
6004
6005 break;
6006 }
6007
6008 if (ioc->atomic_desc_capable) {
6009 ioc->put_smid_default = &_base_put_smid_default_atomic;
6010 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io_atomic;
6011 ioc->put_smid_fast_path = &_base_put_smid_fast_path_atomic;
6012 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority_atomic;
6013 ioc->put_smid_nvme_encap = &_base_put_smid_nvme_encap_atomic;
6014 } else {
6015 ioc->put_smid_default = &_base_put_smid_default;
6016 ioc->put_smid_scsi_io = &_base_put_smid_scsi_io;
6017 ioc->put_smid_fast_path = &_base_put_smid_fast_path;
6018 ioc->put_smid_hi_priority = &_base_put_smid_hi_priority;
6019 ioc->put_smid_nvme_encap = &_base_put_smid_nvme_encap;
6020 }
6021
6022
6023 /*
6024 * These function pointers for other requests that don't
6025 * the require IEEE scatter gather elements.
6026 *
6027 * For example Configuration Pages and SAS IOUNIT Control don't.
6028 */
6029 ioc->build_sg_mpi = &_base_build_sg;
6030 ioc->build_zero_len_sge_mpi = &_base_build_zero_len_sge;
6031
6032 r = _base_make_ioc_ready(ioc, SOFT_RESET);
6033 if (r)
6034 goto out_free_resources;
6035
6036 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
6037 sizeof(struct mpt3sas_port_facts), GFP_KERNEL);
6038 if (!ioc->pfacts) {
6039 r = -ENOMEM;
6040 goto out_free_resources;
6041 }
6042
6043 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
6044 r = _base_get_port_facts(ioc, i);
6045 if (r)
6046 goto out_free_resources;
6047 }
6048
6049 r = _base_allocate_memory_pools(ioc);
6050 if (r)
6051 goto out_free_resources;
6052
6053 init_waitqueue_head(&ioc->reset_wq);
6054
6055 /* allocate memory pd handle bitmask list */
6056 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
6057 if (ioc->facts.MaxDevHandle % 8)
6058 ioc->pd_handles_sz++;
6059 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
6060 GFP_KERNEL);
6061 if (!ioc->pd_handles) {
6062 r = -ENOMEM;
6063 goto out_free_resources;
6064 }
6065 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
6066 GFP_KERNEL);
6067 if (!ioc->blocking_handles) {
6068 r = -ENOMEM;
6069 goto out_free_resources;
6070 }
6071
6072 /* allocate memory for pending OS device add list */
6073 ioc->pend_os_device_add_sz = (ioc->facts.MaxDevHandle / 8);
6074 if (ioc->facts.MaxDevHandle % 8)
6075 ioc->pend_os_device_add_sz++;
6076 ioc->pend_os_device_add = kzalloc(ioc->pend_os_device_add_sz,
6077 GFP_KERNEL);
6078 if (!ioc->pend_os_device_add)
6079 goto out_free_resources;
6080
6081 ioc->device_remove_in_progress_sz = ioc->pend_os_device_add_sz;
6082 ioc->device_remove_in_progress =
6083 kzalloc(ioc->device_remove_in_progress_sz, GFP_KERNEL);
6084 if (!ioc->device_remove_in_progress)
6085 goto out_free_resources;
6086
6087 ioc->fwfault_debug = mpt3sas_fwfault_debug;
6088
6089 /* base internal command bits */
6090 mutex_init(&ioc->base_cmds.mutex);
6091 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6092 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
6093
6094 /* port_enable command bits */
6095 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6096 ioc->port_enable_cmds.status = MPT3_CMD_NOT_USED;
6097
6098 /* transport internal command bits */
6099 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6100 ioc->transport_cmds.status = MPT3_CMD_NOT_USED;
6101 mutex_init(&ioc->transport_cmds.mutex);
6102
6103 /* scsih internal command bits */
6104 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6105 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6106 mutex_init(&ioc->scsih_cmds.mutex);
6107
6108 /* task management internal command bits */
6109 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6110 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
6111 mutex_init(&ioc->tm_cmds.mutex);
6112
6113 /* config page internal command bits */
6114 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6115 ioc->config_cmds.status = MPT3_CMD_NOT_USED;
6116 mutex_init(&ioc->config_cmds.mutex);
6117
6118 /* ctl module internal command bits */
6119 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
6120 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
6121 ioc->ctl_cmds.status = MPT3_CMD_NOT_USED;
6122 mutex_init(&ioc->ctl_cmds.mutex);
6123
6124 if (!ioc->base_cmds.reply || !ioc->port_enable_cmds.reply ||
6125 !ioc->transport_cmds.reply || !ioc->scsih_cmds.reply ||
6126 !ioc->tm_cmds.reply || !ioc->config_cmds.reply ||
6127 !ioc->ctl_cmds.reply || !ioc->ctl_cmds.sense) {
6128 r = -ENOMEM;
6129 goto out_free_resources;
6130 }
6131
6132 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
6133 ioc->event_masks[i] = -1;
6134
6135 /* here we enable the events we care about */
6136 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
6137 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
6138 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
6139 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
6140 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
6141 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
6142 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
6143 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
6144 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
6145 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
6146 _base_unmask_events(ioc, MPI2_EVENT_TEMP_THRESHOLD);
6147 _base_unmask_events(ioc, MPI2_EVENT_ACTIVE_CABLE_EXCEPTION);
6148 if (ioc->hba_mpi_version_belonged == MPI26_VERSION) {
6149 if (ioc->is_gen35_ioc) {
6150 _base_unmask_events(ioc,
6151 MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE);
6152 _base_unmask_events(ioc, MPI2_EVENT_PCIE_ENUMERATION);
6153 _base_unmask_events(ioc,
6154 MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST);
6155 }
6156 }
6157 r = _base_make_ioc_operational(ioc);
6158 if (r)
6159 goto out_free_resources;
6160
6161 ioc->non_operational_loop = 0;
6162 ioc->got_task_abort_from_ioctl = 0;
6163 return 0;
6164
6165 out_free_resources:
6166
6167 ioc->remove_host = 1;
6168
6169 mpt3sas_base_free_resources(ioc);
6170 _base_release_memory_pools(ioc);
6171 pci_set_drvdata(ioc->pdev, NULL);
6172 kfree(ioc->cpu_msix_table);
6173 if (ioc->is_warpdrive)
6174 kfree(ioc->reply_post_host_index);
6175 kfree(ioc->pd_handles);
6176 kfree(ioc->blocking_handles);
6177 kfree(ioc->device_remove_in_progress);
6178 kfree(ioc->pend_os_device_add);
6179 kfree(ioc->tm_cmds.reply);
6180 kfree(ioc->transport_cmds.reply);
6181 kfree(ioc->scsih_cmds.reply);
6182 kfree(ioc->config_cmds.reply);
6183 kfree(ioc->base_cmds.reply);
6184 kfree(ioc->port_enable_cmds.reply);
6185 kfree(ioc->ctl_cmds.reply);
6186 kfree(ioc->ctl_cmds.sense);
6187 kfree(ioc->pfacts);
6188 ioc->ctl_cmds.reply = NULL;
6189 ioc->base_cmds.reply = NULL;
6190 ioc->tm_cmds.reply = NULL;
6191 ioc->scsih_cmds.reply = NULL;
6192 ioc->transport_cmds.reply = NULL;
6193 ioc->config_cmds.reply = NULL;
6194 ioc->pfacts = NULL;
6195 return r;
6196 }
6197
6198
6199 /**
6200 * mpt3sas_base_detach - remove controller instance
6201 * @ioc: per adapter object
6202 *
6203 * Return nothing.
6204 */
6205 void
6206 mpt3sas_base_detach(struct MPT3SAS_ADAPTER *ioc)
6207 {
6208 dexitprintk(ioc, pr_info(MPT3SAS_FMT "%s\n", ioc->name,
6209 __func__));
6210
6211 mpt3sas_base_stop_watchdog(ioc);
6212 mpt3sas_base_free_resources(ioc);
6213 _base_release_memory_pools(ioc);
6214 pci_set_drvdata(ioc->pdev, NULL);
6215 kfree(ioc->cpu_msix_table);
6216 if (ioc->is_warpdrive)
6217 kfree(ioc->reply_post_host_index);
6218 kfree(ioc->pd_handles);
6219 kfree(ioc->blocking_handles);
6220 kfree(ioc->device_remove_in_progress);
6221 kfree(ioc->pend_os_device_add);
6222 kfree(ioc->pfacts);
6223 kfree(ioc->ctl_cmds.reply);
6224 kfree(ioc->ctl_cmds.sense);
6225 kfree(ioc->base_cmds.reply);
6226 kfree(ioc->port_enable_cmds.reply);
6227 kfree(ioc->tm_cmds.reply);
6228 kfree(ioc->transport_cmds.reply);
6229 kfree(ioc->scsih_cmds.reply);
6230 kfree(ioc->config_cmds.reply);
6231 }
6232
6233 /**
6234 * _base_reset_handler - reset callback handler (for base)
6235 * @ioc: per adapter object
6236 * @reset_phase: phase
6237 *
6238 * The handler for doing any required cleanup or initialization.
6239 *
6240 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
6241 * MPT3_IOC_DONE_RESET
6242 *
6243 * Return nothing.
6244 */
6245 static void
6246 _base_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
6247 {
6248 mpt3sas_scsih_reset_handler(ioc, reset_phase);
6249 mpt3sas_ctl_reset_handler(ioc, reset_phase);
6250 switch (reset_phase) {
6251 case MPT3_IOC_PRE_RESET:
6252 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6253 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
6254 break;
6255 case MPT3_IOC_AFTER_RESET:
6256 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6257 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
6258 if (ioc->transport_cmds.status & MPT3_CMD_PENDING) {
6259 ioc->transport_cmds.status |= MPT3_CMD_RESET;
6260 mpt3sas_base_free_smid(ioc, ioc->transport_cmds.smid);
6261 complete(&ioc->transport_cmds.done);
6262 }
6263 if (ioc->base_cmds.status & MPT3_CMD_PENDING) {
6264 ioc->base_cmds.status |= MPT3_CMD_RESET;
6265 mpt3sas_base_free_smid(ioc, ioc->base_cmds.smid);
6266 complete(&ioc->base_cmds.done);
6267 }
6268 if (ioc->port_enable_cmds.status & MPT3_CMD_PENDING) {
6269 ioc->port_enable_failed = 1;
6270 ioc->port_enable_cmds.status |= MPT3_CMD_RESET;
6271 mpt3sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
6272 if (ioc->is_driver_loading) {
6273 ioc->start_scan_failed =
6274 MPI2_IOCSTATUS_INTERNAL_ERROR;
6275 ioc->start_scan = 0;
6276 ioc->port_enable_cmds.status =
6277 MPT3_CMD_NOT_USED;
6278 } else
6279 complete(&ioc->port_enable_cmds.done);
6280 }
6281 if (ioc->config_cmds.status & MPT3_CMD_PENDING) {
6282 ioc->config_cmds.status |= MPT3_CMD_RESET;
6283 mpt3sas_base_free_smid(ioc, ioc->config_cmds.smid);
6284 ioc->config_cmds.smid = USHRT_MAX;
6285 complete(&ioc->config_cmds.done);
6286 }
6287 break;
6288 case MPT3_IOC_DONE_RESET:
6289 dtmprintk(ioc, pr_info(MPT3SAS_FMT
6290 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
6291 break;
6292 }
6293 }
6294
6295 /**
6296 * _wait_for_commands_to_complete - reset controller
6297 * @ioc: Pointer to MPT_ADAPTER structure
6298 *
6299 * This function waiting(3s) for all pending commands to complete
6300 * prior to putting controller in reset.
6301 */
6302 static void
6303 _wait_for_commands_to_complete(struct MPT3SAS_ADAPTER *ioc)
6304 {
6305 u32 ioc_state;
6306 unsigned long flags;
6307 u16 i;
6308
6309 ioc->pending_io_count = 0;
6310
6311 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6312 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
6313 return;
6314
6315 /* pending command count */
6316 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6317 for (i = 0; i < ioc->scsiio_depth; i++)
6318 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
6319 ioc->pending_io_count++;
6320 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6321
6322 if (!ioc->pending_io_count)
6323 return;
6324
6325 /* wait for pending commands to complete */
6326 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
6327 }
6328
6329 /**
6330 * mpt3sas_base_hard_reset_handler - reset controller
6331 * @ioc: Pointer to MPT_ADAPTER structure
6332 * @type: FORCE_BIG_HAMMER or SOFT_RESET
6333 *
6334 * Returns 0 for success, non-zero for failure.
6335 */
6336 int
6337 mpt3sas_base_hard_reset_handler(struct MPT3SAS_ADAPTER *ioc,
6338 enum reset_type type)
6339 {
6340 int r;
6341 unsigned long flags;
6342 u32 ioc_state;
6343 u8 is_fault = 0, is_trigger = 0;
6344
6345 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: enter\n", ioc->name,
6346 __func__));
6347
6348 if (ioc->pci_error_recovery) {
6349 pr_err(MPT3SAS_FMT "%s: pci error recovery reset\n",
6350 ioc->name, __func__);
6351 r = 0;
6352 goto out_unlocked;
6353 }
6354
6355 if (mpt3sas_fwfault_debug)
6356 mpt3sas_halt_firmware(ioc);
6357
6358 /* wait for an active reset in progress to complete */
6359 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
6360 do {
6361 ssleep(1);
6362 } while (ioc->shost_recovery == 1);
6363 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6364 __func__));
6365 return ioc->ioc_reset_in_progress_status;
6366 }
6367
6368 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6369 ioc->shost_recovery = 1;
6370 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6371
6372 if ((ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6373 MPT3_DIAG_BUFFER_IS_REGISTERED) &&
6374 (!(ioc->diag_buffer_status[MPI2_DIAG_BUF_TYPE_TRACE] &
6375 MPT3_DIAG_BUFFER_IS_RELEASED))) {
6376 is_trigger = 1;
6377 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
6378 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
6379 is_fault = 1;
6380 }
6381 _base_reset_handler(ioc, MPT3_IOC_PRE_RESET);
6382 _wait_for_commands_to_complete(ioc);
6383 _base_mask_interrupts(ioc);
6384 r = _base_make_ioc_ready(ioc, type);
6385 if (r)
6386 goto out;
6387 _base_reset_handler(ioc, MPT3_IOC_AFTER_RESET);
6388
6389 /* If this hard reset is called while port enable is active, then
6390 * there is no reason to call make_ioc_operational
6391 */
6392 if (ioc->is_driver_loading && ioc->port_enable_failed) {
6393 ioc->remove_host = 1;
6394 r = -EFAULT;
6395 goto out;
6396 }
6397 r = _base_get_ioc_facts(ioc);
6398 if (r)
6399 goto out;
6400
6401 if (ioc->rdpq_array_enable && !ioc->rdpq_array_capable)
6402 panic("%s: Issue occurred with flashing controller firmware."
6403 "Please reboot the system and ensure that the correct"
6404 " firmware version is running\n", ioc->name);
6405
6406 r = _base_make_ioc_operational(ioc);
6407 if (!r)
6408 _base_reset_handler(ioc, MPT3_IOC_DONE_RESET);
6409
6410 out:
6411 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: %s\n",
6412 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
6413
6414 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6415 ioc->ioc_reset_in_progress_status = r;
6416 ioc->shost_recovery = 0;
6417 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
6418 ioc->ioc_reset_count++;
6419 mutex_unlock(&ioc->reset_in_progress_mutex);
6420
6421 out_unlocked:
6422 if ((r == 0) && is_trigger) {
6423 if (is_fault)
6424 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_FW_FAULT);
6425 else
6426 mpt3sas_trigger_master(ioc,
6427 MASTER_TRIGGER_ADAPTER_RESET);
6428 }
6429 dtmprintk(ioc, pr_info(MPT3SAS_FMT "%s: exit\n", ioc->name,
6430 __func__));
6431 return r;
6432 }