blob: cf51b48dd7d96e66189be22426de290ea6bb2b44 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
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/mpt2sas/mpt2_base.c
Sreekanth Reddyd648d5a2013-07-25 11:28:01 +05306 * Copyright (C) 2007-2013 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06007 * (mailto:DL-MPTFusionLinux@lsi.com)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
29
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
43 */
44
Eric Moore635374e2009-03-09 01:21:12 -060045#include <linux/kernel.h>
46#include <linux/module.h>
47#include <linux/errno.h>
48#include <linux/init.h>
49#include <linux/slab.h>
50#include <linux/types.h>
51#include <linux/pci.h>
52#include <linux/kdev_t.h>
53#include <linux/blkdev.h>
54#include <linux/delay.h>
55#include <linux/interrupt.h>
56#include <linux/dma-mapping.h>
57#include <linux/sort.h>
58#include <linux/io.h>
Kashyap, Desaia8ebd762009-09-23 17:29:29 +053059#include <linux/time.h>
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +053060#include <linux/kthread.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053061#include <linux/aer.h>
Eric Moore635374e2009-03-09 01:21:12 -060062
63#include "mpt2sas_base.h"
64
65static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
66
67#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
Eric Moore635374e2009-03-09 01:21:12 -060068
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +053069#define MAX_HBA_QUEUE_DEPTH 30000
70#define MAX_CHAIN_DEPTH 100000
Eric Moore635374e2009-03-09 01:21:12 -060071static int max_queue_depth = -1;
72module_param(max_queue_depth, int, 0);
73MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
74
75static int max_sgl_entries = -1;
76module_param(max_sgl_entries, int, 0);
77MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
78
79static int msix_disable = -1;
80module_param(msix_disable, int, 0);
81MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
82
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053083static int mpt2sas_fwfault_debug;
84MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
85 "and halt firmware - (default=0)");
86
87static int disable_discovery = -1;
88module_param(disable_discovery, int, 0);
89MODULE_PARM_DESC(disable_discovery, " disable discovery ");
90
Kashyap, Desaifa7f3162009-09-23 17:26:58 +053091/**
92 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
93 *
94 */
95static int
96_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
97{
98 int ret = param_set_int(val, kp);
99 struct MPT2SAS_ADAPTER *ioc;
100
101 if (ret)
102 return ret;
103
Kashyap, Desaie75b9b62009-12-16 18:52:39 +0530104 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530105 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
106 ioc->fwfault_debug = mpt2sas_fwfault_debug;
107 return 0;
108}
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530109
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530110module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
111 param_get_int, &mpt2sas_fwfault_debug, 0644);
112
Eric Moore635374e2009-03-09 01:21:12 -0600113/**
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530114 * mpt2sas_remove_dead_ioc_func - kthread context to remove dead ioc
115 * @arg: input argument, used to derive ioc
116 *
117 * Return 0 if controller is removed from pci subsystem.
118 * Return -1 for other case.
119 */
120static int mpt2sas_remove_dead_ioc_func(void *arg)
121{
122 struct MPT2SAS_ADAPTER *ioc = (struct MPT2SAS_ADAPTER *)arg;
123 struct pci_dev *pdev;
124
125 if ((ioc == NULL))
126 return -1;
127
128 pdev = ioc->pdev;
129 if ((pdev == NULL))
130 return -1;
Rafael J. Wysocki64cdb412014-01-10 15:27:56 +0100131 pci_stop_and_remove_bus_device_locked(pdev);
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530132 return 0;
133}
134
135
136/**
Eric Moore635374e2009-03-09 01:21:12 -0600137 * _base_fault_reset_work - workq handling ioc fault conditions
138 * @work: input argument, used to derive ioc
139 * Context: sleep.
140 *
141 * Return nothing.
142 */
143static void
144_base_fault_reset_work(struct work_struct *work)
145{
146 struct MPT2SAS_ADAPTER *ioc =
147 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
148 unsigned long flags;
149 u32 doorbell;
150 int rc;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530151 struct task_struct *p;
Eric Moore635374e2009-03-09 01:21:12 -0600152
153 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Sreekanth Reddyb4730fb2012-12-18 14:45:30 +0100154 if (ioc->shost_recovery || ioc->pci_error_recovery)
Eric Moore635374e2009-03-09 01:21:12 -0600155 goto rearm_timer;
156 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
157
158 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530159 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
160 printk(MPT2SAS_INFO_FMT "%s : SAS host is non-operational !!!!\n",
161 ioc->name, __func__);
162
Sreekanth Reddyb4730fb2012-12-18 14:45:30 +0100163 /* It may be possible that EEH recovery can resolve some of
164 * pci bus failure issues rather removing the dead ioc function
165 * by considering controller is in a non-operational state. So
166 * here priority is given to the EEH recovery. If it doesn't
167 * not resolve this issue, mpt2sas driver will consider this
168 * controller to non-operational state and remove the dead ioc
169 * function.
170 */
171 if (ioc->non_operational_loop++ < 5) {
172 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
173 flags);
174 goto rearm_timer;
175 }
176
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +0530177 /*
178 * Call _scsih_flush_pending_cmds callback so that we flush all
179 * pending commands back to OS. This call is required to aovid
180 * deadlock at block layer. Dead IOC will fail to do diag reset,
181 * and this call is safe since dead ioc will never return any
182 * command back from HW.
183 */
184 ioc->schedule_dead_ioc_flush_running_cmds(ioc);
185 /*
186 * Set remove_host flag early since kernel thread will
187 * take some time to execute.
188 */
189 ioc->remove_host = 1;
190 /*Remove the Dead Host */
191 p = kthread_run(mpt2sas_remove_dead_ioc_func, ioc,
192 "mpt2sas_dead_ioc_%d", ioc->id);
193 if (IS_ERR(p)) {
194 printk(MPT2SAS_ERR_FMT
195 "%s: Running mpt2sas_dead_ioc thread failed !!!!\n",
196 ioc->name, __func__);
197 } else {
198 printk(MPT2SAS_ERR_FMT
199 "%s: Running mpt2sas_dead_ioc thread success !!!!\n",
200 ioc->name, __func__);
201 }
202
203 return; /* don't rearm timer */
204 }
205
Sreekanth Reddyb4730fb2012-12-18 14:45:30 +0100206 ioc->non_operational_loop = 0;
207
Eric Moore635374e2009-03-09 01:21:12 -0600208 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
209 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
210 FORCE_BIG_HAMMER);
211 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
212 __func__, (rc == 0) ? "success" : "failed");
213 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
214 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
215 mpt2sas_base_fault_info(ioc, doorbell &
216 MPI2_DOORBELL_DATA_MASK);
217 }
218
219 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
220 rearm_timer:
221 if (ioc->fault_reset_work_q)
222 queue_delayed_work(ioc->fault_reset_work_q,
223 &ioc->fault_reset_work,
224 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
225 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
226}
227
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530228/**
229 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530230 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530231 * Context: sleep.
232 *
233 * Return nothing.
234 */
235void
236mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
237{
238 unsigned long flags;
239
240 if (ioc->fault_reset_work_q)
241 return;
242
243 /* initialize fault polling */
244 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
245 snprintf(ioc->fault_reset_work_q_name,
246 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
247 ioc->fault_reset_work_q =
248 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
249 if (!ioc->fault_reset_work_q) {
250 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
251 ioc->name, __func__, __LINE__);
252 return;
253 }
254 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
255 if (ioc->fault_reset_work_q)
256 queue_delayed_work(ioc->fault_reset_work_q,
257 &ioc->fault_reset_work,
258 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
259 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
260}
261
262/**
263 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530264 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530265 * Context: sleep.
266 *
267 * Return nothing.
268 */
269void
270mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
271{
272 unsigned long flags;
273 struct workqueue_struct *wq;
274
275 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
276 wq = ioc->fault_reset_work_q;
277 ioc->fault_reset_work_q = NULL;
278 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
279 if (wq) {
Reddy, Sreekanth02b77082014-07-14 12:00:49 +0530280 if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530281 flush_workqueue(wq);
282 destroy_workqueue(wq);
283 }
284}
285
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530286/**
287 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
288 * @ioc: per adapter object
289 * @fault_code: fault code
290 *
291 * Return nothing.
292 */
293void
294mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
295{
296 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
297 ioc->name, fault_code);
298}
299
300/**
301 * mpt2sas_halt_firmware - halt's mpt controller firmware
302 * @ioc: per adapter object
303 *
304 * For debugging timeout related issues. Writing 0xCOFFEE00
305 * to the doorbell register will halt controller firmware. With
306 * the purpose to stop both driver and firmware, the enduser can
307 * obtain a ring buffer from controller UART.
308 */
309void
310mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
311{
312 u32 doorbell;
313
314 if (!ioc->fwfault_debug)
315 return;
316
317 dump_stack();
318
319 doorbell = readl(&ioc->chip->Doorbell);
320 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
321 mpt2sas_base_fault_info(ioc , doorbell);
322 else {
323 writel(0xC0FFEE00, &ioc->chip->Doorbell);
324 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
325 "timeout\n", ioc->name);
326 }
327
328 panic("panic in %s\n", __func__);
329}
330
Eric Moore635374e2009-03-09 01:21:12 -0600331#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
332/**
333 * _base_sas_ioc_info - verbose translation of the ioc status
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530334 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600335 * @mpi_reply: reply mf payload returned from firmware
336 * @request_hdr: request mf
337 *
338 * Return nothing.
339 */
340static void
341_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
342 MPI2RequestHeader_t *request_hdr)
343{
344 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
345 MPI2_IOCSTATUS_MASK;
346 char *desc = NULL;
347 u16 frame_sz;
348 char *func_str = NULL;
349
350 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
351 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
352 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
353 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
354 return;
355
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530356 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
357 return;
358
Eric Moore635374e2009-03-09 01:21:12 -0600359 switch (ioc_status) {
360
361/****************************************************************************
362* Common IOCStatus values for all replies
363****************************************************************************/
364
365 case MPI2_IOCSTATUS_INVALID_FUNCTION:
366 desc = "invalid function";
367 break;
368 case MPI2_IOCSTATUS_BUSY:
369 desc = "busy";
370 break;
371 case MPI2_IOCSTATUS_INVALID_SGL:
372 desc = "invalid sgl";
373 break;
374 case MPI2_IOCSTATUS_INTERNAL_ERROR:
375 desc = "internal error";
376 break;
377 case MPI2_IOCSTATUS_INVALID_VPID:
378 desc = "invalid vpid";
379 break;
380 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
381 desc = "insufficient resources";
382 break;
383 case MPI2_IOCSTATUS_INVALID_FIELD:
384 desc = "invalid field";
385 break;
386 case MPI2_IOCSTATUS_INVALID_STATE:
387 desc = "invalid state";
388 break;
389 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
390 desc = "op state not supported";
391 break;
392
393/****************************************************************************
394* Config IOCStatus values
395****************************************************************************/
396
397 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
398 desc = "config invalid action";
399 break;
400 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
401 desc = "config invalid type";
402 break;
403 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
404 desc = "config invalid page";
405 break;
406 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
407 desc = "config invalid data";
408 break;
409 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
410 desc = "config no defaults";
411 break;
412 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
413 desc = "config cant commit";
414 break;
415
416/****************************************************************************
417* SCSI IO Reply
418****************************************************************************/
419
420 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
421 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
422 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
423 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
424 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
425 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
426 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
427 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
428 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
429 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
430 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
431 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
432 break;
433
434/****************************************************************************
435* For use by SCSI Initiator and SCSI Target end-to-end data protection
436****************************************************************************/
437
438 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
439 desc = "eedp guard error";
440 break;
441 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
442 desc = "eedp ref tag error";
443 break;
444 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
445 desc = "eedp app tag error";
446 break;
447
448/****************************************************************************
449* SCSI Target values
450****************************************************************************/
451
452 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
453 desc = "target invalid io index";
454 break;
455 case MPI2_IOCSTATUS_TARGET_ABORTED:
456 desc = "target aborted";
457 break;
458 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
459 desc = "target no conn retryable";
460 break;
461 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
462 desc = "target no connection";
463 break;
464 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
465 desc = "target xfer count mismatch";
466 break;
467 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
468 desc = "target data offset error";
469 break;
470 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
471 desc = "target too much write data";
472 break;
473 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
474 desc = "target iu too short";
475 break;
476 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
477 desc = "target ack nak timeout";
478 break;
479 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
480 desc = "target nak received";
481 break;
482
483/****************************************************************************
484* Serial Attached SCSI values
485****************************************************************************/
486
487 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
488 desc = "smp request failed";
489 break;
490 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
491 desc = "smp data overrun";
492 break;
493
494/****************************************************************************
495* Diagnostic Buffer Post / Diagnostic Release values
496****************************************************************************/
497
498 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
499 desc = "diagnostic released";
500 break;
501 default:
502 break;
503 }
504
505 if (!desc)
506 return;
507
508 switch (request_hdr->Function) {
509 case MPI2_FUNCTION_CONFIG:
510 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
511 func_str = "config_page";
512 break;
513 case MPI2_FUNCTION_SCSI_TASK_MGMT:
514 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
515 func_str = "task_mgmt";
516 break;
517 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
518 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
519 func_str = "sas_iounit_ctl";
520 break;
521 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
522 frame_sz = sizeof(Mpi2SepRequest_t);
523 func_str = "enclosure";
524 break;
525 case MPI2_FUNCTION_IOC_INIT:
526 frame_sz = sizeof(Mpi2IOCInitRequest_t);
527 func_str = "ioc_init";
528 break;
529 case MPI2_FUNCTION_PORT_ENABLE:
530 frame_sz = sizeof(Mpi2PortEnableRequest_t);
531 func_str = "port_enable";
532 break;
533 case MPI2_FUNCTION_SMP_PASSTHROUGH:
534 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
535 func_str = "smp_passthru";
536 break;
537 default:
538 frame_sz = 32;
539 func_str = "unknown";
540 break;
541 }
542
543 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
544 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
545
546 _debug_dump_mf(request_hdr, frame_sz/4);
547}
548
549/**
550 * _base_display_event_data - verbose translation of firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530551 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600552 * @mpi_reply: reply mf payload returned from firmware
553 *
554 * Return nothing.
555 */
556static void
557_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
558 Mpi2EventNotificationReply_t *mpi_reply)
559{
560 char *desc = NULL;
561 u16 event;
562
563 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
564 return;
565
566 event = le16_to_cpu(mpi_reply->Event);
567
568 switch (event) {
569 case MPI2_EVENT_LOG_DATA:
570 desc = "Log Data";
571 break;
572 case MPI2_EVENT_STATE_CHANGE:
573 desc = "Status Change";
574 break;
575 case MPI2_EVENT_HARD_RESET_RECEIVED:
576 desc = "Hard Reset Received";
577 break;
578 case MPI2_EVENT_EVENT_CHANGE:
579 desc = "Event Change";
580 break;
Eric Moore635374e2009-03-09 01:21:12 -0600581 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
582 desc = "Device Status Change";
583 break;
584 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530585 if (!ioc->hide_ir_msg)
586 desc = "IR Operation Status";
Eric Moore635374e2009-03-09 01:21:12 -0600587 break;
588 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530589 {
590 Mpi2EventDataSasDiscovery_t *event_data =
591 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
592 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
593 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
594 "start" : "stop");
595 if (event_data->DiscoveryStatus)
596 printk("discovery_status(0x%08x)",
597 le32_to_cpu(event_data->DiscoveryStatus));
Julia Lawall7968f192010-08-05 22:19:36 +0200598 printk("\n");
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530599 return;
600 }
Eric Moore635374e2009-03-09 01:21:12 -0600601 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
602 desc = "SAS Broadcast Primitive";
603 break;
604 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
605 desc = "SAS Init Device Status Change";
606 break;
607 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
608 desc = "SAS Init Table Overflow";
609 break;
610 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
611 desc = "SAS Topology Change List";
612 break;
613 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
614 desc = "SAS Enclosure Device Status Change";
615 break;
616 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530617 if (!ioc->hide_ir_msg)
618 desc = "IR Volume";
Eric Moore635374e2009-03-09 01:21:12 -0600619 break;
620 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530621 if (!ioc->hide_ir_msg)
622 desc = "IR Physical Disk";
Eric Moore635374e2009-03-09 01:21:12 -0600623 break;
624 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530625 if (!ioc->hide_ir_msg)
626 desc = "IR Configuration Change List";
Eric Moore635374e2009-03-09 01:21:12 -0600627 break;
628 case MPI2_EVENT_LOG_ENTRY_ADDED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530629 if (!ioc->hide_ir_msg)
630 desc = "Log Entry Added";
Eric Moore635374e2009-03-09 01:21:12 -0600631 break;
632 }
633
634 if (!desc)
635 return;
636
637 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
638}
639#endif
640
641/**
642 * _base_sas_log_info - verbose translation of firmware log info
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530643 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600644 * @log_info: log info
645 *
646 * Return nothing.
647 */
648static void
649_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
650{
651 union loginfo_type {
652 u32 loginfo;
653 struct {
654 u32 subcode:16;
655 u32 code:8;
656 u32 originator:4;
657 u32 bus_type:4;
658 } dw;
659 };
660 union loginfo_type sas_loginfo;
661 char *originator_str = NULL;
662
663 sas_loginfo.loginfo = log_info;
664 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
665 return;
666
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +0530667 /* each nexus loss loginfo */
668 if (log_info == 0x31170000)
669 return;
670
Eric Moore635374e2009-03-09 01:21:12 -0600671 /* eat the loginfos associated with task aborts */
Sathisha Nanjappa714be352012-03-13 11:59:28 -0700672 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
Eric Moore635374e2009-03-09 01:21:12 -0600673 0x31140000 || log_info == 0x31130000))
674 return;
675
676 switch (sas_loginfo.dw.originator) {
677 case 0:
678 originator_str = "IOP";
679 break;
680 case 1:
681 originator_str = "PL";
682 break;
683 case 2:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530684 if (!ioc->hide_ir_msg)
685 originator_str = "IR";
686 else
687 originator_str = "WarpDrive";
Eric Moore635374e2009-03-09 01:21:12 -0600688 break;
689 }
690
691 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
692 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
693 originator_str, sas_loginfo.dw.code,
694 sas_loginfo.dw.subcode);
695}
696
697/**
Eric Moore635374e2009-03-09 01:21:12 -0600698 * _base_display_reply_info -
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530699 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600700 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530701 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600702 * @reply: reply message frame(lower 32bit addr)
703 *
704 * Return nothing.
705 */
706static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530707_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600708 u32 reply)
709{
710 MPI2DefaultReply_t *mpi_reply;
711 u16 ioc_status;
712
713 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +0530714 if (unlikely(!mpi_reply)) {
715 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
716 ioc->name, __FILE__, __LINE__, __func__);
717 return;
718 }
Eric Moore635374e2009-03-09 01:21:12 -0600719 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
720#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
721 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
722 (ioc->logging_level & MPT_DEBUG_REPLY)) {
723 _base_sas_ioc_info(ioc , mpi_reply,
724 mpt2sas_base_get_msg_frame(ioc, smid));
725 }
726#endif
727 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
728 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
729}
730
731/**
732 * mpt2sas_base_done - base internal command completion routine
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530733 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600734 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530735 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600736 * @reply: reply message frame(lower 32bit addr)
737 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530738 * Return 1 meaning mf should be freed from _base_interrupt
739 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600740 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530741u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530742mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
743 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600744{
745 MPI2DefaultReply_t *mpi_reply;
746
747 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
748 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530749 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600750
751 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530752 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600753
754 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
755 if (mpi_reply) {
756 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
757 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
758 }
759 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530760
Eric Moore635374e2009-03-09 01:21:12 -0600761 complete(&ioc->base_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530762 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600763}
764
765/**
766 * _base_async_event - main callback handler for firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530767 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530768 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600769 * @reply: reply message frame(lower 32bit addr)
770 *
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +0530771 * Returns void.
Eric Moore635374e2009-03-09 01:21:12 -0600772 */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +0530773static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530774_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600775{
776 Mpi2EventNotificationReply_t *mpi_reply;
777 Mpi2EventAckRequest_t *ack_request;
778 u16 smid;
779
780 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
781 if (!mpi_reply)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +0530782 return;
Eric Moore635374e2009-03-09 01:21:12 -0600783 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +0530784 return;
Eric Moore635374e2009-03-09 01:21:12 -0600785#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
786 _base_display_event_data(ioc, mpi_reply);
787#endif
788 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
789 goto out;
790 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
791 if (!smid) {
792 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
793 ioc->name, __func__);
794 goto out;
795 }
796
797 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
798 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
799 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
800 ack_request->Event = mpi_reply->Event;
801 ack_request->EventContext = mpi_reply->EventContext;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530802 ack_request->VF_ID = 0; /* TODO */
803 ack_request->VP_ID = 0;
804 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600805
806 out:
807
808 /* scsih callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530809 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600810
811 /* ctl callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530812 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530813
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +0530814 return;
Eric Moore635374e2009-03-09 01:21:12 -0600815}
816
817/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530818 * _base_get_cb_idx - obtain the callback index
819 * @ioc: per adapter object
820 * @smid: system request message index
821 *
822 * Return callback index.
823 */
824static u8
825_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
826{
827 int i;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +0530828 u8 cb_idx;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530829
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +0530830 if (smid < ioc->hi_priority_smid) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530831 i = smid - 1;
832 cb_idx = ioc->scsi_lookup[i].cb_idx;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +0530833 } else if (smid < ioc->internal_smid) {
834 i = smid - ioc->hi_priority_smid;
835 cb_idx = ioc->hpr_lookup[i].cb_idx;
836 } else if (smid <= ioc->hba_queue_depth) {
837 i = smid - ioc->internal_smid;
838 cb_idx = ioc->internal_lookup[i].cb_idx;
839 } else
840 cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530841 return cb_idx;
842}
843
844/**
Eric Moore635374e2009-03-09 01:21:12 -0600845 * _base_mask_interrupts - disable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530846 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600847 *
848 * Disabling ResetIRQ, Reply and Doorbell Interrupts
849 *
850 * Return nothing.
851 */
852static void
853_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
854{
855 u32 him_register;
856
857 ioc->mask_interrupts = 1;
858 him_register = readl(&ioc->chip->HostInterruptMask);
859 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
860 writel(him_register, &ioc->chip->HostInterruptMask);
861 readl(&ioc->chip->HostInterruptMask);
862}
863
864/**
865 * _base_unmask_interrupts - enable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530866 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600867 *
868 * Enabling only Reply Interrupts
869 *
870 * Return nothing.
871 */
872static void
873_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
874{
875 u32 him_register;
876
Eric Moore635374e2009-03-09 01:21:12 -0600877 him_register = readl(&ioc->chip->HostInterruptMask);
878 him_register &= ~MPI2_HIM_RIM;
879 writel(him_register, &ioc->chip->HostInterruptMask);
880 ioc->mask_interrupts = 0;
881}
882
Kashyap, Desai5b768582009-08-20 13:24:31 +0530883union reply_descriptor {
884 u64 word;
885 struct {
886 u32 low;
887 u32 high;
888 } u;
889};
890
Eric Moore635374e2009-03-09 01:21:12 -0600891/**
892 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
893 * @irq: irq number (not used)
894 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
895 * @r: pt_regs pointer (not used)
896 *
897 * Return IRQ_HANDLE if processed, else IRQ_NONE.
898 */
899static irqreturn_t
900_base_interrupt(int irq, void *bus_id)
901{
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530902 struct adapter_reply_queue *reply_q = bus_id;
Eric Moore03ea1112009-04-21 15:37:57 -0600903 union reply_descriptor rd;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530904 u32 completed_cmds;
Eric Moore635374e2009-03-09 01:21:12 -0600905 u8 request_desript_type;
906 u16 smid;
907 u8 cb_idx;
908 u32 reply;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530909 u8 msix_index = reply_q->msix_index;
910 struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530911 Mpi2ReplyDescriptorsUnion_t *rpf;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530912 u8 rc;
Eric Moore635374e2009-03-09 01:21:12 -0600913
914 if (ioc->mask_interrupts)
915 return IRQ_NONE;
916
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530917 if (!atomic_add_unless(&reply_q->busy, 1, 1))
918 return IRQ_NONE;
919
920 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
Kashyap, Desai5b768582009-08-20 13:24:31 +0530921 request_desript_type = rpf->Default.ReplyFlags
922 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530923 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
924 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600925 return IRQ_NONE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530926 }
Eric Moore635374e2009-03-09 01:21:12 -0600927
928 completed_cmds = 0;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530929 cb_idx = 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -0600930 do {
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530931 rd.word = le64_to_cpu(rpf->Words);
Eric Moore03ea1112009-04-21 15:37:57 -0600932 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
Eric Moore635374e2009-03-09 01:21:12 -0600933 goto out;
934 reply = 0;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530935 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Eric Moore635374e2009-03-09 01:21:12 -0600936 if (request_desript_type ==
937 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530938 reply = le32_to_cpu
939 (rpf->AddressReply.ReplyFrameAddress);
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530940 if (reply > ioc->reply_dma_max_address ||
941 reply < ioc->reply_dma_min_address)
942 reply = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600943 } else if (request_desript_type ==
944 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
945 goto next;
946 else if (request_desript_type ==
947 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
948 goto next;
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +0530949 if (smid) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530950 cb_idx = _base_get_cb_idx(ioc, smid);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +0530951 if ((likely(cb_idx < MPT_MAX_CALLBACKS))
952 && (likely(mpt_callbacks[cb_idx] != NULL))) {
953 rc = mpt_callbacks[cb_idx](ioc, smid,
954 msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600955 if (reply)
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +0530956 _base_display_reply_info(ioc, smid,
957 msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530958 if (rc)
959 mpt2sas_base_free_smid(ioc, smid);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +0530960 }
Eric Moore635374e2009-03-09 01:21:12 -0600961 }
962 if (!smid)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530963 _base_async_event(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600964
965 /* reply free queue handling */
966 if (reply) {
967 ioc->reply_free_host_index =
968 (ioc->reply_free_host_index ==
969 (ioc->reply_free_queue_depth - 1)) ?
970 0 : ioc->reply_free_host_index + 1;
971 ioc->reply_free[ioc->reply_free_host_index] =
972 cpu_to_le32(reply);
Kashyap, Desai5b768582009-08-20 13:24:31 +0530973 wmb();
Eric Moore635374e2009-03-09 01:21:12 -0600974 writel(ioc->reply_free_host_index,
975 &ioc->chip->ReplyFreeHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600976 }
977
978 next:
Kashyap, Desai5b768582009-08-20 13:24:31 +0530979
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530980 rpf->Words = cpu_to_le64(ULLONG_MAX);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530981 reply_q->reply_post_host_index =
982 (reply_q->reply_post_host_index ==
Kashyap, Desai5b768582009-08-20 13:24:31 +0530983 (ioc->reply_post_queue_depth - 1)) ? 0 :
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530984 reply_q->reply_post_host_index + 1;
Eric Moore635374e2009-03-09 01:21:12 -0600985 request_desript_type =
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530986 reply_q->reply_post_free[reply_q->reply_post_host_index].
987 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600988 completed_cmds++;
989 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
990 goto out;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530991 if (!reply_q->reply_post_host_index)
992 rpf = reply_q->reply_post_free;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530993 else
994 rpf++;
Eric Moore635374e2009-03-09 01:21:12 -0600995 } while (1);
996
997 out:
998
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530999 if (!completed_cmds) {
1000 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -06001001 return IRQ_NONE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301002 }
Eric Moore635374e2009-03-09 01:21:12 -06001003 wmb();
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301004 if (ioc->is_warpdrive) {
1005 writel(reply_q->reply_post_host_index,
1006 ioc->reply_post_host_index[msix_index]);
1007 atomic_dec(&reply_q->busy);
1008 return IRQ_HANDLED;
1009 }
1010 writel(reply_q->reply_post_host_index | (msix_index <<
1011 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
1012 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -06001013 return IRQ_HANDLED;
1014}
1015
1016/**
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301017 * _base_is_controller_msix_enabled - is controller support muli-reply queues
1018 * @ioc: per adapter object
1019 *
1020 */
1021static inline int
1022_base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
1023{
1024 return (ioc->facts.IOCCapabilities &
1025 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
1026}
1027
1028/**
1029 * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
1030 * @ioc: per adapter object
1031 * Context: ISR conext
1032 *
1033 * Called when a Task Management request has completed. We want
1034 * to flush the other reply queues so all the outstanding IO has been
1035 * completed back to OS before we process the TM completetion.
1036 *
1037 * Return nothing.
1038 */
1039void
1040mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1041{
1042 struct adapter_reply_queue *reply_q;
1043
1044 /* If MSIX capability is turned off
1045 * then multi-queues are not enabled
1046 */
1047 if (!_base_is_controller_msix_enabled(ioc))
1048 return;
1049
1050 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
1051 if (ioc->shost_recovery)
1052 return;
1053 /* TMs are on msix_index == 0 */
1054 if (reply_q->msix_index == 0)
1055 continue;
1056 _base_interrupt(reply_q->vector, (void *)reply_q);
1057 }
1058}
1059
1060/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001061 * mpt2sas_base_release_callback_handler - clear interrupt callback handler
Eric Moore635374e2009-03-09 01:21:12 -06001062 * @cb_idx: callback index
1063 *
1064 * Return nothing.
1065 */
1066void
1067mpt2sas_base_release_callback_handler(u8 cb_idx)
1068{
1069 mpt_callbacks[cb_idx] = NULL;
1070}
1071
1072/**
1073 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
1074 * @cb_func: callback function
1075 *
1076 * Returns cb_func.
1077 */
1078u8
1079mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1080{
1081 u8 cb_idx;
1082
1083 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1084 if (mpt_callbacks[cb_idx] == NULL)
1085 break;
1086
1087 mpt_callbacks[cb_idx] = cb_func;
1088 return cb_idx;
1089}
1090
1091/**
1092 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
1093 *
1094 * Return nothing.
1095 */
1096void
1097mpt2sas_base_initialize_callback_handler(void)
1098{
1099 u8 cb_idx;
1100
1101 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1102 mpt2sas_base_release_callback_handler(cb_idx);
1103}
1104
1105/**
1106 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
1107 * @ioc: per adapter object
1108 * @paddr: virtual address for SGE
1109 *
1110 * Create a zero length scatter gather entry to insure the IOCs hardware has
1111 * something to use if the target device goes brain dead and tries
1112 * to send data even when none is asked for.
1113 *
1114 * Return nothing.
1115 */
1116void
1117mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
1118{
1119 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1120 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1121 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1122 MPI2_SGE_FLAGS_SHIFT);
1123 ioc->base_add_sg_single(paddr, flags_length, -1);
1124}
1125
1126/**
1127 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1128 * @paddr: virtual address for SGE
1129 * @flags_length: SGE flags and data transfer length
1130 * @dma_addr: Physical address
1131 *
1132 * Return nothing.
1133 */
1134static void
1135_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1136{
1137 Mpi2SGESimple32_t *sgel = paddr;
1138
1139 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1140 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1141 sgel->FlagsLength = cpu_to_le32(flags_length);
1142 sgel->Address = cpu_to_le32(dma_addr);
1143}
1144
1145
1146/**
1147 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1148 * @paddr: virtual address for SGE
1149 * @flags_length: SGE flags and data transfer length
1150 * @dma_addr: Physical address
1151 *
1152 * Return nothing.
1153 */
1154static void
1155_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1156{
1157 Mpi2SGESimple64_t *sgel = paddr;
1158
1159 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1160 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1161 sgel->FlagsLength = cpu_to_le32(flags_length);
1162 sgel->Address = cpu_to_le64(dma_addr);
1163}
1164
1165#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1166
1167/**
1168 * _base_config_dma_addressing - set dma addressing
1169 * @ioc: per adapter object
1170 * @pdev: PCI device struct
1171 *
1172 * Returns 0 for success, non-zero for failure.
1173 */
1174static int
1175_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1176{
1177 struct sysinfo s;
1178 char *desc = NULL;
1179
1180 if (sizeof(dma_addr_t) > 4) {
1181 const uint64_t required_mask =
1182 dma_get_required_mask(&pdev->dev);
Yang Hongyange9304382009-04-13 14:40:14 -07001183 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1184 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1185 DMA_BIT_MASK(64))) {
Eric Moore635374e2009-03-09 01:21:12 -06001186 ioc->base_add_sg_single = &_base_add_sg_single_64;
1187 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1188 desc = "64";
1189 goto out;
1190 }
1191 }
1192
Yang Hongyange9304382009-04-13 14:40:14 -07001193 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1194 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eric Moore635374e2009-03-09 01:21:12 -06001195 ioc->base_add_sg_single = &_base_add_sg_single_32;
1196 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1197 desc = "32";
1198 } else
1199 return -ENODEV;
1200
1201 out:
1202 si_meminfo(&s);
1203 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1204 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1205
1206 return 0;
1207}
1208
1209/**
Eric Moore635374e2009-03-09 01:21:12 -06001210 * _base_check_enable_msix - checks MSIX capabable.
1211 * @ioc: per adapter object
1212 *
1213 * Check to see if card is capable of MSIX, and set number
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001214 * of available msix vectors
Eric Moore635374e2009-03-09 01:21:12 -06001215 */
1216static int
1217_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1218{
1219 int base;
1220 u16 message_control;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301221
Eric Moore635374e2009-03-09 01:21:12 -06001222
sreekanth.reddy@lsi.com10cce6d2012-08-22 16:55:13 +05301223 /* Check whether controller SAS2008 B0 controller,
1224 if it is SAS2008 B0 controller use IO-APIC instead of MSIX */
1225 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 &&
1226 ioc->pdev->revision == 0x01) {
1227 return -EINVAL;
1228 }
1229
Eric Moore635374e2009-03-09 01:21:12 -06001230 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1231 if (!base) {
1232 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1233 "supported\n", ioc->name));
1234 return -EINVAL;
1235 }
1236
1237 /* get msix vector count */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301238 /* NUMA_IO not supported for older controllers */
1239 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1240 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1241 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1242 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1243 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1244 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1245 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1246 ioc->msix_vector_count = 1;
1247 else {
1248 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1249 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1250 }
Eric Moore635374e2009-03-09 01:21:12 -06001251 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301252 "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1253
Eric Moore635374e2009-03-09 01:21:12 -06001254 return 0;
1255}
1256
1257/**
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301258 * _base_free_irq - free irq
1259 * @ioc: per adapter object
1260 *
1261 * Freeing respective reply_queue from the list.
1262 */
1263static void
1264_base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1265{
1266 struct adapter_reply_queue *reply_q, *next;
1267
1268 if (list_empty(&ioc->reply_queue_list))
1269 return;
1270
1271 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1272 list_del(&reply_q->list);
1273 synchronize_irq(reply_q->vector);
1274 free_irq(reply_q->vector, reply_q);
1275 kfree(reply_q);
1276 }
1277}
1278
1279/**
1280 * _base_request_irq - request irq
1281 * @ioc: per adapter object
1282 * @index: msix index into vector table
1283 * @vector: irq vector
1284 *
1285 * Inserting respective reply_queue into the list.
1286 */
1287static int
1288_base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1289{
1290 struct adapter_reply_queue *reply_q;
1291 int r;
1292
1293 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1294 if (!reply_q) {
1295 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1296 ioc->name, (int)sizeof(struct adapter_reply_queue));
1297 return -ENOMEM;
1298 }
1299 reply_q->ioc = ioc;
1300 reply_q->msix_index = index;
1301 reply_q->vector = vector;
1302 atomic_set(&reply_q->busy, 0);
1303 if (ioc->msix_enable)
1304 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1305 MPT2SAS_DRIVER_NAME, ioc->id, index);
1306 else
1307 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1308 MPT2SAS_DRIVER_NAME, ioc->id);
1309 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1310 reply_q);
1311 if (r) {
1312 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1313 reply_q->name, vector);
1314 kfree(reply_q);
1315 return -EBUSY;
1316 }
1317
1318 INIT_LIST_HEAD(&reply_q->list);
1319 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1320 return 0;
1321}
1322
1323/**
1324 * _base_assign_reply_queues - assigning msix index for each cpu
1325 * @ioc: per adapter object
1326 *
1327 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1328 *
1329 * It would nice if we could call irq_set_affinity, however it is not
1330 * an exported symbol
1331 */
1332static void
1333_base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1334{
Martin K. Petersencbbb7b32014-01-03 19:16:55 -05001335 unsigned int cpu, nr_cpus, nr_msix, index = 0;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301336
1337 if (!_base_is_controller_msix_enabled(ioc))
1338 return;
1339
1340 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301341
Martin K. Petersencbbb7b32014-01-03 19:16:55 -05001342 nr_cpus = num_online_cpus();
1343 nr_msix = ioc->reply_queue_count = min(ioc->reply_queue_count,
1344 ioc->facts.MaxMSIxVectors);
1345 if (!nr_msix)
1346 return;
1347
1348 cpu = cpumask_first(cpu_online_mask);
1349
1350 do {
1351 unsigned int i, group = nr_cpus / nr_msix;
1352
1353 if (index < nr_cpus % nr_msix)
1354 group++;
1355
1356 for (i = 0 ; i < group ; i++) {
1357 ioc->cpu_msix_table[cpu] = index;
1358 cpu = cpumask_next(cpu, cpu_online_mask);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301359 }
Martin K. Petersencbbb7b32014-01-03 19:16:55 -05001360
1361 index++;
1362
1363 } while (cpu < nr_cpus);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301364}
1365
1366/**
Eric Moore635374e2009-03-09 01:21:12 -06001367 * _base_disable_msix - disables msix
1368 * @ioc: per adapter object
1369 *
1370 */
1371static void
1372_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1373{
1374 if (ioc->msix_enable) {
1375 pci_disable_msix(ioc->pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001376 ioc->msix_enable = 0;
1377 }
1378}
1379
1380/**
1381 * _base_enable_msix - enables msix, failback to io_apic
1382 * @ioc: per adapter object
1383 *
1384 */
1385static int
1386_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1387{
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301388 struct msix_entry *entries, *a;
Eric Moore635374e2009-03-09 01:21:12 -06001389 int r;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301390 int i;
Eric Moore635374e2009-03-09 01:21:12 -06001391 u8 try_msix = 0;
1392
1393 if (msix_disable == -1 || msix_disable == 0)
1394 try_msix = 1;
1395
1396 if (!try_msix)
1397 goto try_ioapic;
1398
1399 if (_base_check_enable_msix(ioc) != 0)
1400 goto try_ioapic;
1401
Roland Dreier2f73b9a2011-11-30 16:30:33 -08001402 ioc->reply_queue_count = min_t(int, ioc->cpu_count,
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301403 ioc->msix_vector_count);
1404
1405 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1406 GFP_KERNEL);
1407 if (!entries) {
1408 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1409 "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1410 __LINE__, __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001411 goto try_ioapic;
1412 }
1413
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301414 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1415 a->entry = i;
1416
Alexander Gordeev52674c62014-08-18 08:01:45 +02001417 r = pci_enable_msix_exact(ioc->pdev, entries, ioc->reply_queue_count);
Eric Moore635374e2009-03-09 01:21:12 -06001418 if (r) {
Alexander Gordeev52674c62014-08-18 08:01:45 +02001419 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT
1420 "pci_enable_msix_exact failed (r=%d) !!!\n", ioc->name, r));
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301421 kfree(entries);
Eric Moore635374e2009-03-09 01:21:12 -06001422 goto try_ioapic;
1423 }
1424
Eric Moore635374e2009-03-09 01:21:12 -06001425 ioc->msix_enable = 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301426 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1427 r = _base_request_irq(ioc, i, a->vector);
1428 if (r) {
1429 _base_free_irq(ioc);
1430 _base_disable_msix(ioc);
1431 kfree(entries);
1432 goto try_ioapic;
1433 }
1434 }
1435
1436 kfree(entries);
Eric Moore635374e2009-03-09 01:21:12 -06001437 return 0;
1438
1439/* failback to io_apic interrupt routing */
1440 try_ioapic:
1441
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301442 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
Eric Moore635374e2009-03-09 01:21:12 -06001443
Eric Moore635374e2009-03-09 01:21:12 -06001444 return r;
1445}
1446
1447/**
1448 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1449 * @ioc: per adapter object
1450 *
1451 * Returns 0 for success, non-zero for failure.
1452 */
1453int
1454mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1455{
1456 struct pci_dev *pdev = ioc->pdev;
1457 u32 memap_sz;
1458 u32 pio_sz;
1459 int i, r = 0;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301460 u64 pio_chip = 0;
1461 u64 chip_phys = 0;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301462 struct adapter_reply_queue *reply_q;
Eric Moore635374e2009-03-09 01:21:12 -06001463
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301464 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
Eric Moore635374e2009-03-09 01:21:12 -06001465 ioc->name, __func__));
1466
1467 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1468 if (pci_enable_device_mem(pdev)) {
1469 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1470 "failed\n", ioc->name);
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04001471 ioc->bars = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001472 return -ENODEV;
1473 }
1474
1475
1476 if (pci_request_selected_regions(pdev, ioc->bars,
1477 MPT2SAS_DRIVER_NAME)) {
1478 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1479 "failed\n", ioc->name);
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04001480 ioc->bars = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001481 r = -ENODEV;
1482 goto out_fail;
1483 }
1484
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301485 /* AER (Advanced Error Reporting) hooks */
1486 pci_enable_pcie_error_reporting(pdev);
1487
Eric Moore635374e2009-03-09 01:21:12 -06001488 pci_set_master(pdev);
1489
1490 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1491 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1492 ioc->name, pci_name(pdev));
1493 r = -ENODEV;
1494 goto out_fail;
1495 }
1496
1497 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
Richard A Laryfc193172010-03-12 15:27:06 -08001498 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Eric Moore635374e2009-03-09 01:21:12 -06001499 if (pio_sz)
1500 continue;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301501 pio_chip = (u64)pci_resource_start(pdev, i);
Eric Moore635374e2009-03-09 01:21:12 -06001502 pio_sz = pci_resource_len(pdev, i);
1503 } else {
1504 if (memap_sz)
1505 continue;
Richard A Laryfc193172010-03-12 15:27:06 -08001506 /* verify memory resource is valid before using */
1507 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1508 ioc->chip_phys = pci_resource_start(pdev, i);
1509 chip_phys = (u64)ioc->chip_phys;
1510 memap_sz = pci_resource_len(pdev, i);
1511 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1512 if (ioc->chip == NULL) {
1513 printk(MPT2SAS_ERR_FMT "unable to map "
1514 "adapter memory!\n", ioc->name);
1515 r = -EINVAL;
1516 goto out_fail;
1517 }
Eric Moore635374e2009-03-09 01:21:12 -06001518 }
1519 }
1520 }
1521
Eric Moore635374e2009-03-09 01:21:12 -06001522 _base_mask_interrupts(ioc);
1523 r = _base_enable_msix(ioc);
1524 if (r)
1525 goto out_fail;
1526
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301527 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1528 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1529 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1530 "IO-APIC enabled"), reply_q->vector);
1531
Kashyap, Desai6846e752009-12-16 18:51:45 +05301532 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1533 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1534 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1535 ioc->name, (unsigned long long)pio_chip, pio_sz);
Eric Moore635374e2009-03-09 01:21:12 -06001536
Eric Moore3cb54692010-07-08 14:44:34 -06001537 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1538 pci_save_state(pdev);
1539
Eric Moore635374e2009-03-09 01:21:12 -06001540 return 0;
1541
1542 out_fail:
1543 if (ioc->chip_phys)
1544 iounmap(ioc->chip);
1545 ioc->chip_phys = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001546 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301547 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001548 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001549 return r;
1550}
1551
1552/**
Eric Moore635374e2009-03-09 01:21:12 -06001553 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1554 * @ioc: per adapter object
1555 * @smid: system request message index(smid zero is invalid)
1556 *
1557 * Returns virt pointer to message frame.
1558 */
1559void *
1560mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1561{
1562 return (void *)(ioc->request + (smid * ioc->request_sz));
1563}
1564
1565/**
1566 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1567 * @ioc: per adapter object
1568 * @smid: system request message index
1569 *
1570 * Returns virt pointer to sense buffer.
1571 */
1572void *
1573mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1574{
1575 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1576}
1577
1578/**
1579 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1580 * @ioc: per adapter object
1581 * @smid: system request message index
1582 *
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301583 * Returns phys pointer to the low 32bit address of the sense buffer.
Eric Moore635374e2009-03-09 01:21:12 -06001584 */
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301585__le32
Eric Moore635374e2009-03-09 01:21:12 -06001586mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1587{
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301588 return cpu_to_le32(ioc->sense_dma +
1589 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
Eric Moore635374e2009-03-09 01:21:12 -06001590}
1591
1592/**
1593 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1594 * @ioc: per adapter object
1595 * @phys_addr: lower 32 physical addr of the reply
1596 *
1597 * Converts 32bit lower physical addr into a virt address.
1598 */
1599void *
1600mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1601{
1602 if (!phys_addr)
1603 return NULL;
1604 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1605}
1606
1607/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301608 * mpt2sas_base_get_smid - obtain a free smid from internal queue
Eric Moore635374e2009-03-09 01:21:12 -06001609 * @ioc: per adapter object
1610 * @cb_idx: callback index
1611 *
1612 * Returns smid (zero is invalid)
1613 */
1614u16
1615mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1616{
1617 unsigned long flags;
1618 struct request_tracker *request;
1619 u16 smid;
1620
1621 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301622 if (list_empty(&ioc->internal_free_list)) {
1623 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1624 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1625 ioc->name, __func__);
1626 return 0;
1627 }
1628
1629 request = list_entry(ioc->internal_free_list.next,
1630 struct request_tracker, tracker_list);
1631 request->cb_idx = cb_idx;
1632 smid = request->smid;
1633 list_del(&request->tracker_list);
1634 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1635 return smid;
1636}
1637
1638/**
1639 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1640 * @ioc: per adapter object
1641 * @cb_idx: callback index
1642 * @scmd: pointer to scsi command object
1643 *
1644 * Returns smid (zero is invalid)
1645 */
1646u16
1647mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1648 struct scsi_cmnd *scmd)
1649{
1650 unsigned long flags;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301651 struct scsiio_tracker *request;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301652 u16 smid;
1653
1654 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001655 if (list_empty(&ioc->free_list)) {
1656 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1657 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1658 ioc->name, __func__);
1659 return 0;
1660 }
1661
1662 request = list_entry(ioc->free_list.next,
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301663 struct scsiio_tracker, tracker_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301664 request->scmd = scmd;
1665 request->cb_idx = cb_idx;
1666 smid = request->smid;
1667 list_del(&request->tracker_list);
1668 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1669 return smid;
1670}
1671
1672/**
1673 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1674 * @ioc: per adapter object
1675 * @cb_idx: callback index
1676 *
1677 * Returns smid (zero is invalid)
1678 */
1679u16
1680mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1681{
1682 unsigned long flags;
1683 struct request_tracker *request;
1684 u16 smid;
1685
1686 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1687 if (list_empty(&ioc->hpr_free_list)) {
1688 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1689 return 0;
1690 }
1691
1692 request = list_entry(ioc->hpr_free_list.next,
1693 struct request_tracker, tracker_list);
Eric Moore635374e2009-03-09 01:21:12 -06001694 request->cb_idx = cb_idx;
1695 smid = request->smid;
1696 list_del(&request->tracker_list);
1697 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1698 return smid;
1699}
1700
1701
1702/**
1703 * mpt2sas_base_free_smid - put smid back on free_list
1704 * @ioc: per adapter object
1705 * @smid: system request message index
1706 *
1707 * Return nothing.
1708 */
1709void
1710mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1711{
1712 unsigned long flags;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301713 int i;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301714 struct chain_tracker *chain_req, *next;
Eric Moore635374e2009-03-09 01:21:12 -06001715
1716 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301717 if (smid < ioc->hi_priority_smid) {
1718 /* scsiio queue */
1719 i = smid - 1;
1720 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1721 list_for_each_entry_safe(chain_req, next,
1722 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1723 list_del_init(&chain_req->tracker_list);
Matthew Wilcox049b3e82014-03-27 16:40:34 -04001724 list_add(&chain_req->tracker_list,
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301725 &ioc->free_chain_list);
1726 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301727 }
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301728 ioc->scsi_lookup[i].cb_idx = 0xFF;
1729 ioc->scsi_lookup[i].scmd = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301730 ioc->scsi_lookup[i].direct_io = 0;
Matthew Wilcox049b3e82014-03-27 16:40:34 -04001731 list_add(&ioc->scsi_lookup[i].tracker_list,
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301732 &ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301733 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301734
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301735 /*
1736 * See _wait_for_commands_to_complete() call with regards
1737 * to this code.
1738 */
1739 if (ioc->shost_recovery && ioc->pending_io_count) {
1740 if (ioc->pending_io_count == 1)
1741 wake_up(&ioc->reset_wq);
1742 ioc->pending_io_count--;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301743 }
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301744 return;
1745 } else if (smid < ioc->internal_smid) {
1746 /* hi-priority */
1747 i = smid - ioc->hi_priority_smid;
1748 ioc->hpr_lookup[i].cb_idx = 0xFF;
Matthew Wilcox049b3e82014-03-27 16:40:34 -04001749 list_add(&ioc->hpr_lookup[i].tracker_list,
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301750 &ioc->hpr_free_list);
1751 } else if (smid <= ioc->hba_queue_depth) {
1752 /* internal queue */
1753 i = smid - ioc->internal_smid;
1754 ioc->internal_lookup[i].cb_idx = 0xFF;
Matthew Wilcox049b3e82014-03-27 16:40:34 -04001755 list_add(&ioc->internal_lookup[i].tracker_list,
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301756 &ioc->internal_free_list);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301757 }
Eric Moore635374e2009-03-09 01:21:12 -06001758 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001759}
1760
1761/**
1762 * _base_writeq - 64 bit write to MMIO
1763 * @ioc: per adapter object
1764 * @b: data payload
1765 * @addr: address in MMIO space
1766 * @writeq_lock: spin lock
1767 *
1768 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1769 * care of 32 bit environment where its not quarenteed to send the entire word
1770 * in one transfer.
1771 */
1772#ifndef writeq
1773static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1774 spinlock_t *writeq_lock)
1775{
1776 unsigned long flags;
1777 __u64 data_out = cpu_to_le64(b);
1778
1779 spin_lock_irqsave(writeq_lock, flags);
1780 writel((u32)(data_out), addr);
1781 writel((u32)(data_out >> 32), (addr + 4));
1782 spin_unlock_irqrestore(writeq_lock, flags);
1783}
1784#else
1785static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1786 spinlock_t *writeq_lock)
1787{
1788 writeq(cpu_to_le64(b), addr);
1789}
1790#endif
1791
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301792static inline u8
1793_base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1794{
nagalakshmi.nandigama@lsi.coma2c65852012-04-17 11:25:04 +05301795 return ioc->cpu_msix_table[raw_smp_processor_id()];
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301796}
1797
Eric Moore635374e2009-03-09 01:21:12 -06001798/**
1799 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1800 * @ioc: per adapter object
1801 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001802 * @handle: device handle
1803 *
1804 * Return nothing.
1805 */
1806void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301807mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06001808{
1809 Mpi2RequestDescriptorUnion_t descriptor;
1810 u64 *request = (u64 *)&descriptor;
1811
1812
1813 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301814 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001815 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1816 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1817 descriptor.SCSIIO.LMID = 0;
1818 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1819 &ioc->scsi_lookup_lock);
1820}
1821
1822
1823/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001824 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
Eric Moore635374e2009-03-09 01:21:12 -06001825 * @ioc: per adapter object
1826 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001827 *
1828 * Return nothing.
1829 */
1830void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301831mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001832{
1833 Mpi2RequestDescriptorUnion_t descriptor;
1834 u64 *request = (u64 *)&descriptor;
1835
1836 descriptor.HighPriority.RequestFlags =
1837 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301838 descriptor.HighPriority.MSIxIndex = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001839 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1840 descriptor.HighPriority.LMID = 0;
1841 descriptor.HighPriority.Reserved1 = 0;
1842 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1843 &ioc->scsi_lookup_lock);
1844}
1845
1846/**
1847 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1848 * @ioc: per adapter object
1849 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001850 *
1851 * Return nothing.
1852 */
1853void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301854mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001855{
1856 Mpi2RequestDescriptorUnion_t descriptor;
1857 u64 *request = (u64 *)&descriptor;
1858
1859 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301860 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001861 descriptor.Default.SMID = cpu_to_le16(smid);
1862 descriptor.Default.LMID = 0;
1863 descriptor.Default.DescriptorTypeDependent = 0;
1864 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1865 &ioc->scsi_lookup_lock);
1866}
1867
1868/**
1869 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1870 * @ioc: per adapter object
1871 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001872 * @io_index: value used to track the IO
1873 *
1874 * Return nothing.
1875 */
1876void
1877mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301878 u16 io_index)
Eric Moore635374e2009-03-09 01:21:12 -06001879{
1880 Mpi2RequestDescriptorUnion_t descriptor;
1881 u64 *request = (u64 *)&descriptor;
1882
1883 descriptor.SCSITarget.RequestFlags =
1884 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301885 descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001886 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1887 descriptor.SCSITarget.LMID = 0;
1888 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1889 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1890 &ioc->scsi_lookup_lock);
1891}
1892
1893/**
Eric Mooref0f9cc12009-04-21 15:40:48 -06001894 * _base_display_dell_branding - Disply branding string
1895 * @ioc: per adapter object
1896 *
1897 * Return nothing.
1898 */
1899static void
1900_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1901{
1902 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1903
1904 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1905 return;
1906
1907 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1908 switch (ioc->pdev->subsystem_device) {
1909 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1910 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1911 MPT2SAS_DELL_BRANDING_SIZE - 1);
1912 break;
1913 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1914 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1915 MPT2SAS_DELL_BRANDING_SIZE - 1);
1916 break;
1917 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1918 strncpy(dell_branding,
1919 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1920 MPT2SAS_DELL_BRANDING_SIZE - 1);
1921 break;
1922 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1923 strncpy(dell_branding,
1924 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1925 MPT2SAS_DELL_BRANDING_SIZE - 1);
1926 break;
1927 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1928 strncpy(dell_branding,
1929 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1930 MPT2SAS_DELL_BRANDING_SIZE - 1);
1931 break;
1932 case MPT2SAS_DELL_PERC_H200_SSDID:
1933 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1934 MPT2SAS_DELL_BRANDING_SIZE - 1);
1935 break;
1936 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1937 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1938 MPT2SAS_DELL_BRANDING_SIZE - 1);
1939 break;
1940 default:
1941 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1942 break;
1943 }
1944
1945 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1946 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1947 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1948 ioc->pdev->subsystem_device);
1949}
1950
1951/**
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301952 * _base_display_intel_branding - Display branding string
1953 * @ioc: per adapter object
1954 *
1955 * Return nothing.
1956 */
1957static void
1958_base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1959{
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301960 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1961 return;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301962
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301963 switch (ioc->pdev->device) {
1964 case MPI2_MFGPAGE_DEVID_SAS2008:
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301965 switch (ioc->pdev->subsystem_device) {
1966 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1967 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1968 MPT2SAS_INTEL_RMS2LL080_BRANDING);
1969 break;
1970 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1971 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1972 MPT2SAS_INTEL_RMS2LL040_BRANDING);
1973 break;
sreekanth.reddy@lsi.come17eee42012-07-17 15:56:05 +05301974 case MPT2SAS_INTEL_SSD910_SSDID:
nagalakshmi.nandigama@lsi.com70e73f92011-12-01 07:43:00 +05301975 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
sreekanth.reddy@lsi.come17eee42012-07-17 15:56:05 +05301976 MPT2SAS_INTEL_SSD910_BRANDING);
nagalakshmi.nandigama@lsi.com70e73f92011-12-01 07:43:00 +05301977 break;
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301978 default:
1979 break;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301980 }
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301981 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1982 switch (ioc->pdev->subsystem_device) {
1983 case MPT2SAS_INTEL_RS25GB008_SSDID:
1984 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1985 MPT2SAS_INTEL_RS25GB008_BRANDING);
1986 break;
nagalakshmi.nandigama@lsi.com8bad3052011-12-01 07:52:42 +05301987 case MPT2SAS_INTEL_RMS25JB080_SSDID:
1988 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1989 MPT2SAS_INTEL_RMS25JB080_BRANDING);
1990 break;
1991 case MPT2SAS_INTEL_RMS25JB040_SSDID:
1992 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1993 MPT2SAS_INTEL_RMS25JB040_BRANDING);
1994 break;
1995 case MPT2SAS_INTEL_RMS25KB080_SSDID:
1996 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1997 MPT2SAS_INTEL_RMS25KB080_BRANDING);
1998 break;
1999 case MPT2SAS_INTEL_RMS25KB040_SSDID:
2000 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2001 MPT2SAS_INTEL_RMS25KB040_BRANDING);
2002 break;
Sreekanth Reddy7887ea72013-02-01 21:49:30 +05302003 case MPT2SAS_INTEL_RMS25LB040_SSDID:
2004 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2005 MPT2SAS_INTEL_RMS25LB040_BRANDING);
2006 break;
2007 case MPT2SAS_INTEL_RMS25LB080_SSDID:
2008 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2009 MPT2SAS_INTEL_RMS25LB080_BRANDING);
2010 break;
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05302011 default:
2012 break;
2013 }
2014 default:
2015 break;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05302016 }
2017}
2018
2019/**
Kashyap, Desai0a2385c2011-03-15 20:04:26 +05302020 * _base_display_hp_branding - Display branding string
2021 * @ioc: per adapter object
2022 *
2023 * Return nothing.
2024 */
2025static void
2026_base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
2027{
2028 if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
2029 return;
2030
2031 switch (ioc->pdev->device) {
2032 case MPI2_MFGPAGE_DEVID_SAS2004:
2033 switch (ioc->pdev->subsystem_device) {
2034 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
2035 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2036 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
2037 break;
2038 default:
2039 break;
2040 }
2041 case MPI2_MFGPAGE_DEVID_SAS2308_2:
2042 switch (ioc->pdev->subsystem_device) {
2043 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
2044 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2045 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
2046 break;
2047 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
2048 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2049 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
2050 break;
2051 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
2052 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2053 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
2054 break;
2055 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
2056 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
2057 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
2058 break;
2059 default:
2060 break;
2061 }
2062 default:
2063 break;
2064 }
2065}
2066
2067/**
Eric Moore635374e2009-03-09 01:21:12 -06002068 * _base_display_ioc_capabilities - Disply IOC's capabilities.
2069 * @ioc: per adapter object
2070 *
2071 * Return nothing.
2072 */
2073static void
2074_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
2075{
2076 int i = 0;
2077 char desc[16];
Eric Moore635374e2009-03-09 01:21:12 -06002078 u32 iounit_pg1_flags;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302079 u32 bios_version;
Eric Moore635374e2009-03-09 01:21:12 -06002080
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302081 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
Eric Moore635374e2009-03-09 01:21:12 -06002082 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2083 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2084 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2085 ioc->name, desc,
2086 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2087 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2088 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2089 ioc->facts.FWVersion.Word & 0x000000FF,
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03002090 ioc->pdev->revision,
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302091 (bios_version & 0xFF000000) >> 24,
2092 (bios_version & 0x00FF0000) >> 16,
2093 (bios_version & 0x0000FF00) >> 8,
2094 bios_version & 0x000000FF);
Eric Moore635374e2009-03-09 01:21:12 -06002095
Kashyap, Desaied79f122009-08-20 13:23:49 +05302096 _base_display_dell_branding(ioc);
Kashyap, Desaifb396be2011-01-04 11:36:37 +05302097 _base_display_intel_branding(ioc);
Kashyap, Desai0a2385c2011-03-15 20:04:26 +05302098 _base_display_hp_branding(ioc);
Kashyap, Desaied79f122009-08-20 13:23:49 +05302099
Eric Moore635374e2009-03-09 01:21:12 -06002100 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2101
2102 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2103 printk("Initiator");
2104 i++;
2105 }
2106
2107 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2108 printk("%sTarget", i ? "," : "");
2109 i++;
2110 }
2111
2112 i = 0;
2113 printk("), ");
2114 printk("Capabilities=(");
2115
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302116 if (!ioc->hide_ir_msg) {
2117 if (ioc->facts.IOCCapabilities &
2118 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2119 printk("Raid");
2120 i++;
2121 }
Eric Moore635374e2009-03-09 01:21:12 -06002122 }
2123
2124 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2125 printk("%sTLR", i ? "," : "");
2126 i++;
2127 }
2128
2129 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2130 printk("%sMulticast", i ? "," : "");
2131 i++;
2132 }
2133
2134 if (ioc->facts.IOCCapabilities &
2135 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2136 printk("%sBIDI Target", i ? "," : "");
2137 i++;
2138 }
2139
2140 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2141 printk("%sEEDP", i ? "," : "");
2142 i++;
2143 }
2144
2145 if (ioc->facts.IOCCapabilities &
2146 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2147 printk("%sSnapshot Buffer", i ? "," : "");
2148 i++;
2149 }
2150
2151 if (ioc->facts.IOCCapabilities &
2152 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2153 printk("%sDiag Trace Buffer", i ? "," : "");
2154 i++;
2155 }
2156
2157 if (ioc->facts.IOCCapabilities &
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05302158 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2159 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2160 i++;
2161 }
2162
2163 if (ioc->facts.IOCCapabilities &
Eric Moore635374e2009-03-09 01:21:12 -06002164 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2165 printk("%sTask Set Full", i ? "," : "");
2166 i++;
2167 }
2168
2169 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2170 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2171 printk("%sNCQ", i ? "," : "");
2172 i++;
2173 }
2174
2175 printk(")\n");
2176}
2177
2178/**
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +05302179 * mpt2sas_base_update_missing_delay - change the missing delay timers
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302180 * @ioc: per adapter object
2181 * @device_missing_delay: amount of time till device is reported missing
2182 * @io_missing_delay: interval IO is returned when there is a missing device
2183 *
2184 * Return nothing.
2185 *
2186 * Passed on the command line, this function will modify the device missing
2187 * delay, as well as the io missing delay. This should be called at driver
2188 * load time.
2189 */
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +05302190void
2191mpt2sas_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302192 u16 device_missing_delay, u8 io_missing_delay)
2193{
2194 u16 dmd, dmd_new, dmd_orignal;
2195 u8 io_missing_delay_original;
2196 u16 sz;
2197 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2198 Mpi2ConfigReply_t mpi_reply;
2199 u8 num_phys = 0;
2200 u16 ioc_status;
2201
2202 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2203 if (!num_phys)
2204 return;
2205
2206 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2207 sizeof(Mpi2SasIOUnit1PhyData_t));
2208 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2209 if (!sas_iounit_pg1) {
2210 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2211 ioc->name, __FILE__, __LINE__, __func__);
2212 goto out;
2213 }
2214 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2215 sas_iounit_pg1, sz))) {
2216 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2217 ioc->name, __FILE__, __LINE__, __func__);
2218 goto out;
2219 }
2220 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2221 MPI2_IOCSTATUS_MASK;
2222 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2223 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2224 ioc->name, __FILE__, __LINE__, __func__);
2225 goto out;
2226 }
2227
2228 /* device missing delay */
2229 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2230 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2231 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2232 else
2233 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2234 dmd_orignal = dmd;
2235 if (device_missing_delay > 0x7F) {
2236 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2237 device_missing_delay;
2238 dmd = dmd / 16;
2239 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2240 } else
2241 dmd = device_missing_delay;
2242 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2243
2244 /* io missing delay */
2245 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2246 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2247
2248 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2249 sz)) {
2250 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2251 dmd_new = (dmd &
2252 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2253 else
2254 dmd_new =
2255 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2256 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2257 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2258 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2259 "new(%d)\n", ioc->name, io_missing_delay_original,
2260 io_missing_delay);
2261 ioc->device_missing_delay = dmd_new;
2262 ioc->io_missing_delay = io_missing_delay;
2263 }
2264
2265out:
2266 kfree(sas_iounit_pg1);
2267}
2268
2269/**
Eric Moore635374e2009-03-09 01:21:12 -06002270 * _base_static_config_pages - static start of day config pages
2271 * @ioc: per adapter object
2272 *
2273 * Return nothing.
2274 */
2275static void
2276_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2277{
2278 Mpi2ConfigReply_t mpi_reply;
2279 u32 iounit_pg1_flags;
2280
2281 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
Kashyap, Desaied79f122009-08-20 13:23:49 +05302282 if (ioc->ir_firmware)
2283 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2284 &ioc->manu_pg10);
Eric Moore635374e2009-03-09 01:21:12 -06002285 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2286 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2287 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2288 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2289 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2290 _base_display_ioc_capabilities(ioc);
2291
2292 /*
2293 * Enable task_set_full handling in iounit_pg1 when the
2294 * facts capabilities indicate that its supported.
2295 */
2296 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2297 if ((ioc->facts.IOCCapabilities &
2298 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2299 iounit_pg1_flags &=
2300 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2301 else
2302 iounit_pg1_flags |=
2303 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2304 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302305 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302306
Eric Moore635374e2009-03-09 01:21:12 -06002307}
2308
2309/**
2310 * _base_release_memory_pools - release memory
2311 * @ioc: per adapter object
2312 *
2313 * Free memory allocated from _base_allocate_memory_pools.
2314 *
2315 * Return nothing.
2316 */
2317static void
2318_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2319{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302320 int i;
2321
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302322 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002323 __func__));
2324
2325 if (ioc->request) {
2326 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2327 ioc->request, ioc->request_dma);
2328 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2329 ": free\n", ioc->name, ioc->request));
2330 ioc->request = NULL;
2331 }
2332
2333 if (ioc->sense) {
2334 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2335 if (ioc->sense_dma_pool)
2336 pci_pool_destroy(ioc->sense_dma_pool);
2337 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2338 ": free\n", ioc->name, ioc->sense));
2339 ioc->sense = NULL;
2340 }
2341
2342 if (ioc->reply) {
2343 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2344 if (ioc->reply_dma_pool)
2345 pci_pool_destroy(ioc->reply_dma_pool);
2346 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2347 ": free\n", ioc->name, ioc->reply));
2348 ioc->reply = NULL;
2349 }
2350
2351 if (ioc->reply_free) {
2352 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2353 ioc->reply_free_dma);
2354 if (ioc->reply_free_dma_pool)
2355 pci_pool_destroy(ioc->reply_free_dma_pool);
2356 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2357 "(0x%p): free\n", ioc->name, ioc->reply_free));
2358 ioc->reply_free = NULL;
2359 }
2360
2361 if (ioc->reply_post_free) {
2362 pci_pool_free(ioc->reply_post_free_dma_pool,
2363 ioc->reply_post_free, ioc->reply_post_free_dma);
2364 if (ioc->reply_post_free_dma_pool)
2365 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2366 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2367 "reply_post_free_pool(0x%p): free\n", ioc->name,
2368 ioc->reply_post_free));
2369 ioc->reply_post_free = NULL;
2370 }
2371
2372 if (ioc->config_page) {
2373 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2374 "config_page(0x%p): free\n", ioc->name,
2375 ioc->config_page));
2376 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2377 ioc->config_page, ioc->config_page_dma);
2378 }
2379
Kashyap, Desai66a67932010-04-05 14:21:07 +05302380 if (ioc->scsi_lookup) {
2381 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2382 ioc->scsi_lookup = NULL;
2383 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302384 kfree(ioc->hpr_lookup);
2385 kfree(ioc->internal_lookup);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302386 if (ioc->chain_lookup) {
2387 for (i = 0; i < ioc->chain_depth; i++) {
2388 if (ioc->chain_lookup[i].chain_buffer)
2389 pci_pool_free(ioc->chain_dma_pool,
2390 ioc->chain_lookup[i].chain_buffer,
2391 ioc->chain_lookup[i].chain_buffer_dma);
2392 }
2393 if (ioc->chain_dma_pool)
2394 pci_pool_destroy(ioc->chain_dma_pool);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302395 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2396 ioc->chain_lookup = NULL;
2397 }
Eric Moore635374e2009-03-09 01:21:12 -06002398}
2399
2400
2401/**
2402 * _base_allocate_memory_pools - allocate start of day memory pools
2403 * @ioc: per adapter object
2404 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2405 *
2406 * Returns 0 success, anything else error
2407 */
2408static int
2409_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2410{
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302411 struct mpt2sas_facts *facts;
Eric Moore635374e2009-03-09 01:21:12 -06002412 u16 max_sge_elements;
Eric Moore635374e2009-03-09 01:21:12 -06002413 u16 chains_needed_per_io;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302414 u32 sz, total_sz, reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002415 u32 retry_sz;
2416 u16 max_request_credit;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302417 int i;
Eric Moore635374e2009-03-09 01:21:12 -06002418
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302419 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002420 __func__));
2421
2422 retry_sz = 0;
2423 facts = &ioc->facts;
2424
2425 /* command line tunables for max sgl entries */
2426 if (max_sgl_entries != -1) {
2427 ioc->shost->sg_tablesize = (max_sgl_entries <
2428 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2429 MPT2SAS_SG_DEPTH;
2430 } else {
2431 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2432 }
2433
2434 /* command line tunables for max controller queue depth */
sreekanth.reddy@lsi.com338b1312012-07-17 15:57:05 +05302435 if (max_queue_depth != -1 && max_queue_depth != 0) {
2436 max_request_credit = min_t(u16, max_queue_depth +
2437 ioc->hi_priority_depth + ioc->internal_depth,
2438 facts->RequestCredit);
2439 if (max_request_credit > MAX_HBA_QUEUE_DEPTH)
2440 max_request_credit = MAX_HBA_QUEUE_DEPTH;
2441 } else
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302442 max_request_credit = min_t(u16, facts->RequestCredit,
2443 MAX_HBA_QUEUE_DEPTH);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302444
2445 ioc->hba_queue_depth = max_request_credit;
2446 ioc->hi_priority_depth = facts->HighPriorityCredit;
2447 ioc->internal_depth = ioc->hi_priority_depth + 5;
Eric Moore635374e2009-03-09 01:21:12 -06002448
2449 /* request frame size */
2450 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2451
2452 /* reply frame size */
2453 ioc->reply_sz = facts->ReplyFrameSize * 4;
2454
2455 retry_allocation:
2456 total_sz = 0;
2457 /* calculate number of sg elements left over in the 1st frame */
2458 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2459 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2460 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2461
2462 /* now do the same for a chain buffer */
2463 max_sge_elements = ioc->request_sz - ioc->sge_size;
2464 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2465
2466 ioc->chain_offset_value_for_main_message =
2467 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2468 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2469
2470 /*
2471 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2472 */
2473 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2474 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2475 + 1;
2476 if (chains_needed_per_io > facts->MaxChainDepth) {
2477 chains_needed_per_io = facts->MaxChainDepth;
2478 ioc->shost->sg_tablesize = min_t(u16,
2479 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2480 * chains_needed_per_io), ioc->shost->sg_tablesize);
2481 }
2482 ioc->chains_needed_per_io = chains_needed_per_io;
2483
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302484 /* reply free queue sizing - taking into account for 64 FW events */
2485 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
Eric Moore635374e2009-03-09 01:21:12 -06002486
Sreekanth Reddy148124d2013-02-01 22:02:04 +05302487 /* calculate reply descriptor post queue depth */
2488 ioc->reply_post_queue_depth = ioc->hba_queue_depth +
2489 ioc->reply_free_queue_depth + 1;
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302490 /* align the reply post queue on the next 16 count boundary */
Sreekanth Reddy148124d2013-02-01 22:02:04 +05302491 if (ioc->reply_post_queue_depth % 16)
2492 ioc->reply_post_queue_depth += 16 -
2493 (ioc->reply_post_queue_depth % 16);
2494
2495
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302496 if (ioc->reply_post_queue_depth >
2497 facts->MaxReplyDescriptorPostQueueDepth) {
Sreekanth Reddy148124d2013-02-01 22:02:04 +05302498 ioc->reply_post_queue_depth =
2499 facts->MaxReplyDescriptorPostQueueDepth -
2500 (facts->MaxReplyDescriptorPostQueueDepth % 16);
2501 ioc->hba_queue_depth =
2502 ((ioc->reply_post_queue_depth - 64) / 2) - 1;
2503 ioc->reply_free_queue_depth = ioc->hba_queue_depth + 64;
Eric Moore635374e2009-03-09 01:21:12 -06002504 }
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302505
Eric Moore635374e2009-03-09 01:21:12 -06002506 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2507 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2508 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2509 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2510 ioc->chains_needed_per_io));
2511
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302512 ioc->scsiio_depth = ioc->hba_queue_depth -
2513 ioc->hi_priority_depth - ioc->internal_depth;
2514
2515 /* set the scsi host can_queue depth
2516 * with some internal commands that could be outstanding
2517 */
sreekanth.reddy@lsi.com338b1312012-07-17 15:57:05 +05302518 ioc->shost->can_queue = ioc->scsiio_depth;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302519 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2520 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2521
Eric Moore635374e2009-03-09 01:21:12 -06002522 /* contiguous pool for request and chains, 16 byte align, one extra "
2523 * "frame for smid=0
2524 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302525 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302526 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302527
2528 /* hi-priority queue */
2529 sz += (ioc->hi_priority_depth * ioc->request_sz);
2530
2531 /* internal queue */
2532 sz += (ioc->internal_depth * ioc->request_sz);
Eric Moore635374e2009-03-09 01:21:12 -06002533
2534 ioc->request_dma_sz = sz;
2535 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2536 if (!ioc->request) {
2537 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302538 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2539 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002540 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302541 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
Eric Moore635374e2009-03-09 01:21:12 -06002542 goto out;
2543 retry_sz += 64;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302544 ioc->hba_queue_depth = max_request_credit - retry_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002545 goto retry_allocation;
2546 }
2547
2548 if (retry_sz)
2549 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302550 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2551 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002552 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2553
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302554
2555 /* hi-priority queue */
2556 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002557 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302558 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002559 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302560
2561 /* internal queue */
2562 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2563 ioc->request_sz);
2564 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2565 ioc->request_sz);
2566
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302567
Eric Moore635374e2009-03-09 01:21:12 -06002568 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2569 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302570 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2571 (ioc->hba_queue_depth * ioc->request_sz)/1024));
Eric Moore635374e2009-03-09 01:21:12 -06002572 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2573 ioc->name, (unsigned long long) ioc->request_dma));
2574 total_sz += sz;
2575
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05302576 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302577 ioc->scsi_lookup_pages = get_order(sz);
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05302578 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302579 GFP_KERNEL, ioc->scsi_lookup_pages);
Eric Moore635374e2009-03-09 01:21:12 -06002580 if (!ioc->scsi_lookup) {
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302581 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2582 "sz(%d)\n", ioc->name, (int)sz);
Eric Moore635374e2009-03-09 01:21:12 -06002583 goto out;
2584 }
2585
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302586 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2587 "depth(%d)\n", ioc->name, ioc->request,
2588 ioc->scsiio_depth));
2589
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05302590 ioc->chain_depth = min_t(u32, ioc->chain_depth, MAX_CHAIN_DEPTH);
2591 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2592 ioc->chain_pages = get_order(sz);
2593
2594 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2595 GFP_KERNEL, ioc->chain_pages);
Tomas Henzlc834b1c2012-02-13 18:29:58 +01002596 if (!ioc->chain_lookup) {
2597 printk(MPT2SAS_ERR_FMT "chain_lookup: get_free_pages failed, "
2598 "sz(%d)\n", ioc->name, (int)sz);
2599 goto out;
2600 }
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302601 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2602 ioc->request_sz, 16, 0);
2603 if (!ioc->chain_dma_pool) {
2604 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2605 "failed\n", ioc->name);
2606 goto out;
2607 }
2608 for (i = 0; i < ioc->chain_depth; i++) {
2609 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2610 ioc->chain_dma_pool , GFP_KERNEL,
2611 &ioc->chain_lookup[i].chain_buffer_dma);
2612 if (!ioc->chain_lookup[i].chain_buffer) {
2613 ioc->chain_depth = i;
2614 goto chain_done;
2615 }
2616 total_sz += ioc->request_sz;
2617 }
2618chain_done:
2619 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2620 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2621 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2622 ioc->request_sz))/1024));
2623
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302624 /* initialize hi-priority queue smid's */
2625 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2626 sizeof(struct request_tracker), GFP_KERNEL);
2627 if (!ioc->hpr_lookup) {
2628 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2629 ioc->name);
2630 goto out;
2631 }
2632 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2633 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2634 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2635 ioc->hi_priority_depth, ioc->hi_priority_smid));
2636
2637 /* initialize internal queue smid's */
2638 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2639 sizeof(struct request_tracker), GFP_KERNEL);
2640 if (!ioc->internal_lookup) {
2641 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2642 ioc->name);
2643 goto out;
2644 }
2645 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2646 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2647 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2648 ioc->internal_depth, ioc->internal_smid));
Eric Moore635374e2009-03-09 01:21:12 -06002649
2650 /* sense buffers, 4 byte align */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302651 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Eric Moore635374e2009-03-09 01:21:12 -06002652 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2653 0);
2654 if (!ioc->sense_dma_pool) {
2655 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2656 ioc->name);
2657 goto out;
2658 }
2659 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2660 &ioc->sense_dma);
2661 if (!ioc->sense) {
2662 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2663 ioc->name);
2664 goto out;
2665 }
2666 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2667 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302668 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002669 SCSI_SENSE_BUFFERSIZE, sz/1024));
2670 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2671 ioc->name, (unsigned long long)ioc->sense_dma));
2672 total_sz += sz;
2673
2674 /* reply pool, 4 byte align */
2675 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2676 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2677 0);
2678 if (!ioc->reply_dma_pool) {
2679 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2680 ioc->name);
2681 goto out;
2682 }
2683 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2684 &ioc->reply_dma);
2685 if (!ioc->reply) {
2686 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2687 ioc->name);
2688 goto out;
2689 }
Kashyap, Desaidd3741d2010-11-13 04:31:14 +05302690 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2691 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
Eric Moore635374e2009-03-09 01:21:12 -06002692 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2693 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2694 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2695 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2696 ioc->name, (unsigned long long)ioc->reply_dma));
2697 total_sz += sz;
2698
2699 /* reply free queue, 16 byte align */
2700 sz = ioc->reply_free_queue_depth * 4;
2701 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2702 ioc->pdev, sz, 16, 0);
2703 if (!ioc->reply_free_dma_pool) {
2704 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2705 "failed\n", ioc->name);
2706 goto out;
2707 }
2708 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2709 &ioc->reply_free_dma);
2710 if (!ioc->reply_free) {
2711 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2712 "failed\n", ioc->name);
2713 goto out;
2714 }
2715 memset(ioc->reply_free, 0, sz);
2716 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2717 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2718 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2719 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2720 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2721 total_sz += sz;
2722
2723 /* reply post queue, 16 byte align */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302724 reply_post_free_sz = ioc->reply_post_queue_depth *
2725 sizeof(Mpi2DefaultReplyDescriptor_t);
2726 if (_base_is_controller_msix_enabled(ioc))
2727 sz = reply_post_free_sz * ioc->reply_queue_count;
2728 else
2729 sz = reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002730 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2731 ioc->pdev, sz, 16, 0);
2732 if (!ioc->reply_post_free_dma_pool) {
2733 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2734 "failed\n", ioc->name);
2735 goto out;
2736 }
2737 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2738 GFP_KERNEL, &ioc->reply_post_free_dma);
2739 if (!ioc->reply_post_free) {
2740 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2741 "failed\n", ioc->name);
2742 goto out;
2743 }
2744 memset(ioc->reply_post_free, 0, sz);
2745 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2746 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2747 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2748 sz/1024));
2749 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2750 "(0x%llx)\n", ioc->name, (unsigned long long)
2751 ioc->reply_post_free_dma));
2752 total_sz += sz;
2753
2754 ioc->config_page_sz = 512;
2755 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2756 ioc->config_page_sz, &ioc->config_page_dma);
2757 if (!ioc->config_page) {
2758 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2759 "failed\n", ioc->name);
2760 goto out;
2761 }
2762 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2763 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2764 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2765 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2766 total_sz += ioc->config_page_sz;
2767
2768 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2769 ioc->name, total_sz/1024);
2770 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2771 "Max Controller Queue Depth(%d)\n",
2772 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2773 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2774 ioc->name, ioc->shost->sg_tablesize);
2775 return 0;
2776
2777 out:
Eric Moore635374e2009-03-09 01:21:12 -06002778 return -ENOMEM;
2779}
2780
2781
2782/**
2783 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2784 * @ioc: Pointer to MPT_ADAPTER structure
2785 * @cooked: Request raw or cooked IOC state
2786 *
2787 * Returns all IOC Doorbell register bits if cooked==0, else just the
2788 * Doorbell bits in MPI_IOC_STATE_MASK.
2789 */
2790u32
2791mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2792{
2793 u32 s, sc;
2794
2795 s = readl(&ioc->chip->Doorbell);
2796 sc = s & MPI2_IOC_STATE_MASK;
2797 return cooked ? sc : s;
2798}
2799
2800/**
2801 * _base_wait_on_iocstate - waiting on a particular ioc state
2802 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2803 * @timeout: timeout in second
2804 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2805 *
2806 * Returns 0 for success, non-zero for failure.
2807 */
2808static int
2809_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2810 int sleep_flag)
2811{
2812 u32 count, cntdn;
2813 u32 current_state;
2814
2815 count = 0;
2816 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2817 do {
2818 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2819 if (current_state == ioc_state)
2820 return 0;
2821 if (count && current_state == MPI2_IOC_STATE_FAULT)
2822 break;
2823 if (sleep_flag == CAN_SLEEP)
2824 msleep(1);
2825 else
2826 udelay(500);
2827 count++;
2828 } while (--cntdn);
2829
2830 return current_state;
2831}
2832
2833/**
2834 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2835 * a write to the doorbell)
2836 * @ioc: per adapter object
2837 * @timeout: timeout in second
2838 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2839 *
2840 * Returns 0 for success, non-zero for failure.
2841 *
2842 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2843 */
2844static int
2845_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2846 int sleep_flag)
2847{
2848 u32 cntdn, count;
2849 u32 int_status;
2850
2851 count = 0;
2852 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2853 do {
2854 int_status = readl(&ioc->chip->HostInterruptStatus);
2855 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302856 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002857 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002858 __func__, count, timeout));
2859 return 0;
2860 }
2861 if (sleep_flag == CAN_SLEEP)
2862 msleep(1);
2863 else
2864 udelay(500);
2865 count++;
2866 } while (--cntdn);
2867
2868 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2869 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2870 return -EFAULT;
2871}
2872
2873/**
2874 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2875 * @ioc: per adapter object
2876 * @timeout: timeout in second
2877 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2878 *
2879 * Returns 0 for success, non-zero for failure.
2880 *
2881 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2882 * doorbell.
2883 */
2884static int
2885_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2886 int sleep_flag)
2887{
2888 u32 cntdn, count;
2889 u32 int_status;
2890 u32 doorbell;
2891
2892 count = 0;
2893 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2894 do {
2895 int_status = readl(&ioc->chip->HostInterruptStatus);
2896 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302897 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002898 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002899 __func__, count, timeout));
2900 return 0;
2901 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2902 doorbell = readl(&ioc->chip->Doorbell);
2903 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2904 MPI2_IOC_STATE_FAULT) {
2905 mpt2sas_base_fault_info(ioc , doorbell);
2906 return -EFAULT;
2907 }
2908 } else if (int_status == 0xFFFFFFFF)
2909 goto out;
2910
2911 if (sleep_flag == CAN_SLEEP)
2912 msleep(1);
2913 else
2914 udelay(500);
2915 count++;
2916 } while (--cntdn);
2917
2918 out:
2919 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2920 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2921 return -EFAULT;
2922}
2923
2924/**
2925 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2926 * @ioc: per adapter object
2927 * @timeout: timeout in second
2928 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2929 *
2930 * Returns 0 for success, non-zero for failure.
2931 *
2932 */
2933static int
2934_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2935 int sleep_flag)
2936{
2937 u32 cntdn, count;
2938 u32 doorbell_reg;
2939
2940 count = 0;
2941 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2942 do {
2943 doorbell_reg = readl(&ioc->chip->Doorbell);
2944 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302945 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002946 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002947 __func__, count, timeout));
2948 return 0;
2949 }
2950 if (sleep_flag == CAN_SLEEP)
2951 msleep(1);
2952 else
2953 udelay(500);
2954 count++;
2955 } while (--cntdn);
2956
2957 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2958 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2959 return -EFAULT;
2960}
2961
2962/**
2963 * _base_send_ioc_reset - send doorbell reset
2964 * @ioc: per adapter object
2965 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2966 * @timeout: timeout in second
2967 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2968 *
2969 * Returns 0 for success, non-zero for failure.
2970 */
2971static int
2972_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2973 int sleep_flag)
2974{
2975 u32 ioc_state;
2976 int r = 0;
2977
2978 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2979 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2980 ioc->name, __func__);
2981 return -EFAULT;
2982 }
2983
2984 if (!(ioc->facts.IOCCapabilities &
2985 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2986 return -EFAULT;
2987
2988 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2989
2990 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2991 &ioc->chip->Doorbell);
2992 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2993 r = -EFAULT;
2994 goto out;
2995 }
2996 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2997 timeout, sleep_flag);
2998 if (ioc_state) {
2999 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3000 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3001 r = -EFAULT;
3002 goto out;
3003 }
3004 out:
3005 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
3006 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3007 return r;
3008}
3009
3010/**
3011 * _base_handshake_req_reply_wait - send request thru doorbell interface
3012 * @ioc: per adapter object
3013 * @request_bytes: request length
3014 * @request: pointer having request payload
3015 * @reply_bytes: reply length
3016 * @reply: pointer to reply payload
3017 * @timeout: timeout in second
3018 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3019 *
3020 * Returns 0 for success, non-zero for failure.
3021 */
3022static int
3023_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
3024 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
3025{
3026 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
3027 int i;
3028 u8 failed;
3029 u16 dummy;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303030 __le32 *mfp;
Eric Moore635374e2009-03-09 01:21:12 -06003031
3032 /* make sure doorbell is not in use */
3033 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
3034 printk(MPT2SAS_ERR_FMT "doorbell is in use "
3035 " (line=%d)\n", ioc->name, __LINE__);
3036 return -EFAULT;
3037 }
3038
3039 /* clear pending doorbell interrupts from previous state changes */
3040 if (readl(&ioc->chip->HostInterruptStatus) &
3041 MPI2_HIS_IOC2SYS_DB_STATUS)
3042 writel(0, &ioc->chip->HostInterruptStatus);
3043
3044 /* send message to ioc */
3045 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
3046 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
3047 &ioc->chip->Doorbell);
3048
Kashyap, Desai29786e12009-09-14 11:06:21 +05303049 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
Eric Moore635374e2009-03-09 01:21:12 -06003050 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3051 "int failed (line=%d)\n", ioc->name, __LINE__);
3052 return -EFAULT;
3053 }
3054 writel(0, &ioc->chip->HostInterruptStatus);
3055
3056 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
3057 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3058 "ack failed (line=%d)\n", ioc->name, __LINE__);
3059 return -EFAULT;
3060 }
3061
3062 /* send message 32-bits at a time */
3063 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3064 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3065 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3066 failed = 1;
3067 }
3068
3069 if (failed) {
3070 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3071 "sending request failed (line=%d)\n", ioc->name, __LINE__);
3072 return -EFAULT;
3073 }
3074
3075 /* now wait for the reply */
3076 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3077 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3078 "int failed (line=%d)\n", ioc->name, __LINE__);
3079 return -EFAULT;
3080 }
3081
3082 /* read the first two 16-bits, it gives the total length of the reply */
3083 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3084 & MPI2_DOORBELL_DATA_MASK);
3085 writel(0, &ioc->chip->HostInterruptStatus);
3086 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3087 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3088 "int failed (line=%d)\n", ioc->name, __LINE__);
3089 return -EFAULT;
3090 }
3091 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3092 & MPI2_DOORBELL_DATA_MASK);
3093 writel(0, &ioc->chip->HostInterruptStatus);
3094
3095 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3096 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3097 printk(MPT2SAS_ERR_FMT "doorbell "
3098 "handshake int failed (line=%d)\n", ioc->name,
3099 __LINE__);
3100 return -EFAULT;
3101 }
3102 if (i >= reply_bytes/2) /* overflow case */
3103 dummy = readl(&ioc->chip->Doorbell);
3104 else
3105 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3106 & MPI2_DOORBELL_DATA_MASK);
3107 writel(0, &ioc->chip->HostInterruptStatus);
3108 }
3109
3110 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3111 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3112 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3113 " (line=%d)\n", ioc->name, __LINE__));
3114 }
3115 writel(0, &ioc->chip->HostInterruptStatus);
3116
3117 if (ioc->logging_level & MPT_DEBUG_INIT) {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303118 mfp = (__le32 *)reply;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303119 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003120 for (i = 0; i < reply_bytes/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303121 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003122 le32_to_cpu(mfp[i]));
3123 }
3124 return 0;
3125}
3126
3127/**
3128 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3129 * @ioc: per adapter object
3130 * @mpi_reply: the reply payload from FW
3131 * @mpi_request: the request payload sent to FW
3132 *
3133 * The SAS IO Unit Control Request message allows the host to perform low-level
3134 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3135 * to obtain the IOC assigned device handles for a device if it has other
3136 * identifying information about the device, in addition allows the host to
3137 * remove IOC resources associated with the device.
3138 *
3139 * Returns 0 for success, non-zero for failure.
3140 */
3141int
3142mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3143 Mpi2SasIoUnitControlReply_t *mpi_reply,
3144 Mpi2SasIoUnitControlRequest_t *mpi_request)
3145{
3146 u16 smid;
3147 u32 ioc_state;
3148 unsigned long timeleft;
3149 u8 issue_reset;
3150 int rc;
3151 void *request;
3152 u16 wait_state_count;
3153
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303154 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003155 __func__));
3156
3157 mutex_lock(&ioc->base_cmds.mutex);
3158
3159 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3160 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3161 ioc->name, __func__);
3162 rc = -EAGAIN;
3163 goto out;
3164 }
3165
3166 wait_state_count = 0;
3167 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3168 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3169 if (wait_state_count++ == 10) {
3170 printk(MPT2SAS_ERR_FMT
3171 "%s: failed due to ioc not operational\n",
3172 ioc->name, __func__);
3173 rc = -EFAULT;
3174 goto out;
3175 }
3176 ssleep(1);
3177 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3178 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3179 "operational state(count=%d)\n", ioc->name,
3180 __func__, wait_state_count);
3181 }
3182
3183 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3184 if (!smid) {
3185 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3186 ioc->name, __func__);
3187 rc = -EAGAIN;
3188 goto out;
3189 }
3190
3191 rc = 0;
3192 ioc->base_cmds.status = MPT2_CMD_PENDING;
3193 request = mpt2sas_base_get_msg_frame(ioc, smid);
3194 ioc->base_cmds.smid = smid;
3195 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3196 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3197 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3198 ioc->ioc_link_reset_in_progress = 1;
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303199 init_completion(&ioc->base_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05303200 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003201 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3202 msecs_to_jiffies(10000));
3203 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3204 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3205 ioc->ioc_link_reset_in_progress)
3206 ioc->ioc_link_reset_in_progress = 0;
3207 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3208 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3209 ioc->name, __func__);
3210 _debug_dump_mf(mpi_request,
3211 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3212 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3213 issue_reset = 1;
3214 goto issue_host_reset;
3215 }
3216 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3217 memcpy(mpi_reply, ioc->base_cmds.reply,
3218 sizeof(Mpi2SasIoUnitControlReply_t));
3219 else
3220 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3221 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3222 goto out;
3223
3224 issue_host_reset:
3225 if (issue_reset)
3226 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3227 FORCE_BIG_HAMMER);
3228 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3229 rc = -EFAULT;
3230 out:
3231 mutex_unlock(&ioc->base_cmds.mutex);
3232 return rc;
3233}
3234
3235
3236/**
3237 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3238 * @ioc: per adapter object
3239 * @mpi_reply: the reply payload from FW
3240 * @mpi_request: the request payload sent to FW
3241 *
3242 * The SCSI Enclosure Processor request message causes the IOC to
3243 * communicate with SES devices to control LED status signals.
3244 *
3245 * Returns 0 for success, non-zero for failure.
3246 */
3247int
3248mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3249 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3250{
3251 u16 smid;
3252 u32 ioc_state;
3253 unsigned long timeleft;
3254 u8 issue_reset;
3255 int rc;
3256 void *request;
3257 u16 wait_state_count;
3258
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303259 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003260 __func__));
3261
3262 mutex_lock(&ioc->base_cmds.mutex);
3263
3264 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3265 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3266 ioc->name, __func__);
3267 rc = -EAGAIN;
3268 goto out;
3269 }
3270
3271 wait_state_count = 0;
3272 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3273 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3274 if (wait_state_count++ == 10) {
3275 printk(MPT2SAS_ERR_FMT
3276 "%s: failed due to ioc not operational\n",
3277 ioc->name, __func__);
3278 rc = -EFAULT;
3279 goto out;
3280 }
3281 ssleep(1);
3282 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3283 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3284 "operational state(count=%d)\n", ioc->name,
3285 __func__, wait_state_count);
3286 }
3287
3288 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3289 if (!smid) {
3290 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3291 ioc->name, __func__);
3292 rc = -EAGAIN;
3293 goto out;
3294 }
3295
3296 rc = 0;
3297 ioc->base_cmds.status = MPT2_CMD_PENDING;
3298 request = mpt2sas_base_get_msg_frame(ioc, smid);
3299 ioc->base_cmds.smid = smid;
3300 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303301 init_completion(&ioc->base_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05303302 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003303 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3304 msecs_to_jiffies(10000));
3305 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3306 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3307 ioc->name, __func__);
3308 _debug_dump_mf(mpi_request,
3309 sizeof(Mpi2SepRequest_t)/4);
3310 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3311 issue_reset = 1;
3312 goto issue_host_reset;
3313 }
3314 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3315 memcpy(mpi_reply, ioc->base_cmds.reply,
3316 sizeof(Mpi2SepReply_t));
3317 else
3318 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3319 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3320 goto out;
3321
3322 issue_host_reset:
3323 if (issue_reset)
3324 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3325 FORCE_BIG_HAMMER);
3326 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3327 rc = -EFAULT;
3328 out:
3329 mutex_unlock(&ioc->base_cmds.mutex);
3330 return rc;
3331}
3332
3333/**
3334 * _base_get_port_facts - obtain port facts reply and save in ioc
3335 * @ioc: per adapter object
3336 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3337 *
3338 * Returns 0 for success, non-zero for failure.
3339 */
3340static int
3341_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3342{
3343 Mpi2PortFactsRequest_t mpi_request;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303344 Mpi2PortFactsReply_t mpi_reply;
3345 struct mpt2sas_port_facts *pfacts;
Eric Moore635374e2009-03-09 01:21:12 -06003346 int mpi_reply_sz, mpi_request_sz, r;
3347
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303348 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003349 __func__));
3350
3351 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3352 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3353 memset(&mpi_request, 0, mpi_request_sz);
3354 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3355 mpi_request.PortNumber = port;
3356 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3357 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3358
3359 if (r != 0) {
3360 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3361 ioc->name, __func__, r);
3362 return r;
3363 }
3364
3365 pfacts = &ioc->pfacts[port];
nagalakshmi.nandigama@lsi.come42fafc2012-03-20 12:10:01 +05303366 memset(pfacts, 0, sizeof(struct mpt2sas_port_facts));
Eric Moore635374e2009-03-09 01:21:12 -06003367 pfacts->PortNumber = mpi_reply.PortNumber;
3368 pfacts->VP_ID = mpi_reply.VP_ID;
3369 pfacts->VF_ID = mpi_reply.VF_ID;
3370 pfacts->MaxPostedCmdBuffers =
3371 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3372
3373 return 0;
3374}
3375
3376/**
3377 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3378 * @ioc: per adapter object
3379 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3380 *
3381 * Returns 0 for success, non-zero for failure.
3382 */
3383static int
3384_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3385{
3386 Mpi2IOCFactsRequest_t mpi_request;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303387 Mpi2IOCFactsReply_t mpi_reply;
3388 struct mpt2sas_facts *facts;
Eric Moore635374e2009-03-09 01:21:12 -06003389 int mpi_reply_sz, mpi_request_sz, r;
3390
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303391 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003392 __func__));
3393
3394 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3395 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3396 memset(&mpi_request, 0, mpi_request_sz);
3397 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3398 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3399 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3400
3401 if (r != 0) {
3402 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3403 ioc->name, __func__, r);
3404 return r;
3405 }
3406
3407 facts = &ioc->facts;
nagalakshmi.nandigama@lsi.come42fafc2012-03-20 12:10:01 +05303408 memset(facts, 0, sizeof(struct mpt2sas_facts));
Eric Moore635374e2009-03-09 01:21:12 -06003409 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3410 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3411 facts->VP_ID = mpi_reply.VP_ID;
3412 facts->VF_ID = mpi_reply.VF_ID;
3413 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3414 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3415 facts->WhoInit = mpi_reply.WhoInit;
3416 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303417 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
Eric Moore635374e2009-03-09 01:21:12 -06003418 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3419 facts->MaxReplyDescriptorPostQueueDepth =
3420 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3421 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3422 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3423 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3424 ioc->ir_firmware = 1;
3425 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3426 facts->IOCRequestFrameSize =
3427 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3428 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3429 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3430 ioc->shost->max_id = -1;
3431 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3432 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3433 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3434 facts->HighPriorityCredit =
3435 le16_to_cpu(mpi_reply.HighPriorityCredit);
3436 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3437 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3438
3439 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3440 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3441 facts->MaxChainDepth));
3442 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3443 "reply frame size(%d)\n", ioc->name,
3444 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3445 return 0;
3446}
3447
3448/**
3449 * _base_send_ioc_init - send ioc_init to firmware
3450 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003451 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3452 *
3453 * Returns 0 for success, non-zero for failure.
3454 */
3455static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303456_base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003457{
3458 Mpi2IOCInitRequest_t mpi_request;
3459 Mpi2IOCInitReply_t mpi_reply;
3460 int r;
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303461 struct timeval current_time;
Kashyap, Desai463217b2009-10-05 15:53:06 +05303462 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003463
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303464 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003465 __func__));
3466
3467 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3468 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3469 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303470 mpi_request.VF_ID = 0; /* TODO */
3471 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003472 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3473 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3474
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303475 if (_base_is_controller_msix_enabled(ioc))
3476 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
Eric Moore635374e2009-03-09 01:21:12 -06003477 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3478 mpi_request.ReplyDescriptorPostQueueDepth =
3479 cpu_to_le16(ioc->reply_post_queue_depth);
3480 mpi_request.ReplyFreeQueueDepth =
3481 cpu_to_le16(ioc->reply_free_queue_depth);
3482
Eric Moore635374e2009-03-09 01:21:12 -06003483 mpi_request.SenseBufferAddressHigh =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303484 cpu_to_le32((u64)ioc->sense_dma >> 32);
Eric Moore635374e2009-03-09 01:21:12 -06003485 mpi_request.SystemReplyAddressHigh =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303486 cpu_to_le32((u64)ioc->reply_dma >> 32);
Eric Moore635374e2009-03-09 01:21:12 -06003487 mpi_request.SystemRequestFrameBaseAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303488 cpu_to_le64((u64)ioc->request_dma);
Eric Moore635374e2009-03-09 01:21:12 -06003489 mpi_request.ReplyFreeQueueAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303490 cpu_to_le64((u64)ioc->reply_free_dma);
Eric Moore635374e2009-03-09 01:21:12 -06003491 mpi_request.ReplyDescriptorPostQueueAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303492 cpu_to_le64((u64)ioc->reply_post_free_dma);
3493
Eric Moore635374e2009-03-09 01:21:12 -06003494
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303495 /* This time stamp specifies number of milliseconds
3496 * since epoch ~ midnight January 1, 1970.
3497 */
3498 do_gettimeofday(&current_time);
Kashyap, Desai7921b35c2010-03-17 16:21:33 +05303499 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3500 (current_time.tv_usec / 1000));
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303501
Eric Moore635374e2009-03-09 01:21:12 -06003502 if (ioc->logging_level & MPT_DEBUG_INIT) {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303503 __le32 *mfp;
Eric Moore635374e2009-03-09 01:21:12 -06003504 int i;
3505
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303506 mfp = (__le32 *)&mpi_request;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303507 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003508 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303509 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003510 le32_to_cpu(mfp[i]));
3511 }
3512
3513 r = _base_handshake_req_reply_wait(ioc,
3514 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3515 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3516 sleep_flag);
3517
3518 if (r != 0) {
3519 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3520 ioc->name, __func__, r);
3521 return r;
3522 }
3523
Kashyap, Desai463217b2009-10-05 15:53:06 +05303524 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3525 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
Eric Moore635374e2009-03-09 01:21:12 -06003526 mpi_reply.IOCLogInfo) {
3527 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3528 r = -EIO;
3529 }
3530
3531 return 0;
3532}
3533
3534/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303535 * mpt2sas_port_enable_done - command completion routine for port enable
3536 * @ioc: per adapter object
3537 * @smid: system request message index
3538 * @msix_index: MSIX table index supplied by the OS
3539 * @reply: reply message frame(lower 32bit addr)
3540 *
3541 * Return 1 meaning mf should be freed from _base_interrupt
3542 * 0 means the mf is freed from this function.
3543 */
3544u8
3545mpt2sas_port_enable_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3546 u32 reply)
3547{
3548 MPI2DefaultReply_t *mpi_reply;
3549 u16 ioc_status;
3550
3551 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
3552 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
3553 return 1;
3554
3555 if (ioc->port_enable_cmds.status == MPT2_CMD_NOT_USED)
3556 return 1;
3557
3558 ioc->port_enable_cmds.status |= MPT2_CMD_COMPLETE;
3559 if (mpi_reply) {
3560 ioc->port_enable_cmds.status |= MPT2_CMD_REPLY_VALID;
3561 memcpy(ioc->port_enable_cmds.reply, mpi_reply,
3562 mpi_reply->MsgLength*4);
3563 }
3564 ioc->port_enable_cmds.status &= ~MPT2_CMD_PENDING;
3565
3566 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3567
3568 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
3569 ioc->port_enable_failed = 1;
3570
3571 if (ioc->is_driver_loading) {
3572 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
3573 mpt2sas_port_enable_complete(ioc);
3574 return 1;
3575 } else {
3576 ioc->start_scan_failed = ioc_status;
3577 ioc->start_scan = 0;
3578 return 1;
3579 }
3580 }
3581 complete(&ioc->port_enable_cmds.done);
3582 return 1;
3583}
3584
3585
3586/**
Eric Moore635374e2009-03-09 01:21:12 -06003587 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3588 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003589 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3590 *
3591 * Returns 0 for success, non-zero for failure.
3592 */
3593static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303594_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003595{
3596 Mpi2PortEnableRequest_t *mpi_request;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303597 Mpi2PortEnableReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06003598 unsigned long timeleft;
3599 int r = 0;
3600 u16 smid;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303601 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003602
3603 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3604
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303605 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
Eric Moore635374e2009-03-09 01:21:12 -06003606 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3607 ioc->name, __func__);
3608 return -EAGAIN;
3609 }
3610
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303611 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06003612 if (!smid) {
3613 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3614 ioc->name, __func__);
3615 return -EAGAIN;
3616 }
3617
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303618 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
Eric Moore635374e2009-03-09 01:21:12 -06003619 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303620 ioc->port_enable_cmds.smid = smid;
Eric Moore635374e2009-03-09 01:21:12 -06003621 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3622 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
Eric Moore635374e2009-03-09 01:21:12 -06003623
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303624 init_completion(&ioc->port_enable_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303625 mpt2sas_base_put_smid_default(ioc, smid);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303626 timeleft = wait_for_completion_timeout(&ioc->port_enable_cmds.done,
Eric Moore635374e2009-03-09 01:21:12 -06003627 300*HZ);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303628 if (!(ioc->port_enable_cmds.status & MPT2_CMD_COMPLETE)) {
Eric Moore635374e2009-03-09 01:21:12 -06003629 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3630 ioc->name, __func__);
3631 _debug_dump_mf(mpi_request,
3632 sizeof(Mpi2PortEnableRequest_t)/4);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303633 if (ioc->port_enable_cmds.status & MPT2_CMD_RESET)
Eric Moore635374e2009-03-09 01:21:12 -06003634 r = -EFAULT;
3635 else
3636 r = -ETIME;
3637 goto out;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303638 }
3639 mpi_reply = ioc->port_enable_cmds.reply;
Eric Moore635374e2009-03-09 01:21:12 -06003640
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303641 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) & MPI2_IOCSTATUS_MASK;
3642 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
3643 printk(MPT2SAS_ERR_FMT "%s: failed with (ioc_status=0x%08x)\n",
3644 ioc->name, __func__, ioc_status);
Eric Moore635374e2009-03-09 01:21:12 -06003645 r = -EFAULT;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303646 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06003647 }
3648 out:
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303649 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
3650 printk(MPT2SAS_INFO_FMT "port enable: %s\n", ioc->name, ((r == 0) ?
3651 "SUCCESS" : "FAILED"));
Eric Moore635374e2009-03-09 01:21:12 -06003652 return r;
3653}
3654
3655/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05303656 * mpt2sas_port_enable - initiate firmware discovery (don't wait for reply)
3657 * @ioc: per adapter object
3658 *
3659 * Returns 0 for success, non-zero for failure.
3660 */
3661int
3662mpt2sas_port_enable(struct MPT2SAS_ADAPTER *ioc)
3663{
3664 Mpi2PortEnableRequest_t *mpi_request;
3665 u16 smid;
3666
3667 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3668
3669 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
3670 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3671 ioc->name, __func__);
3672 return -EAGAIN;
3673 }
3674
3675 smid = mpt2sas_base_get_smid(ioc, ioc->port_enable_cb_idx);
3676 if (!smid) {
3677 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3678 ioc->name, __func__);
3679 return -EAGAIN;
3680 }
3681
3682 ioc->port_enable_cmds.status = MPT2_CMD_PENDING;
3683 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3684 ioc->port_enable_cmds.smid = smid;
3685 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3686 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
3687
3688 mpt2sas_base_put_smid_default(ioc, smid);
3689 return 0;
3690}
3691
3692/**
3693 * _base_determine_wait_on_discovery - desposition
3694 * @ioc: per adapter object
3695 *
3696 * Decide whether to wait on discovery to complete. Used to either
3697 * locate boot device, or report volumes ahead of physical devices.
3698 *
3699 * Returns 1 for wait, 0 for don't wait
3700 */
3701static int
3702_base_determine_wait_on_discovery(struct MPT2SAS_ADAPTER *ioc)
3703{
3704 /* We wait for discovery to complete if IR firmware is loaded.
3705 * The sas topology events arrive before PD events, so we need time to
3706 * turn on the bit in ioc->pd_handles to indicate PD
3707 * Also, it maybe required to report Volumes ahead of physical
3708 * devices when MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING is set.
3709 */
3710 if (ioc->ir_firmware)
3711 return 1;
3712
3713 /* if no Bios, then we don't need to wait */
3714 if (!ioc->bios_pg3.BiosVersion)
3715 return 0;
3716
3717 /* Bios is present, then we drop down here.
3718 *
3719 * If there any entries in the Bios Page 2, then we wait
3720 * for discovery to complete.
3721 */
3722
3723 /* Current Boot Device */
3724 if ((ioc->bios_pg2.CurrentBootDeviceForm &
3725 MPI2_BIOSPAGE2_FORM_MASK) ==
3726 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3727 /* Request Boot Device */
3728 (ioc->bios_pg2.ReqBootDeviceForm &
3729 MPI2_BIOSPAGE2_FORM_MASK) ==
3730 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED &&
3731 /* Alternate Request Boot Device */
3732 (ioc->bios_pg2.ReqAltBootDeviceForm &
3733 MPI2_BIOSPAGE2_FORM_MASK) ==
3734 MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED)
3735 return 0;
3736
3737 return 1;
3738}
3739
3740
3741/**
Eric Moore635374e2009-03-09 01:21:12 -06003742 * _base_unmask_events - turn on notification for this event
3743 * @ioc: per adapter object
3744 * @event: firmware event
3745 *
3746 * The mask is stored in ioc->event_masks.
3747 */
3748static void
3749_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3750{
3751 u32 desired_event;
3752
3753 if (event >= 128)
3754 return;
3755
3756 desired_event = (1 << (event % 32));
3757
3758 if (event < 32)
3759 ioc->event_masks[0] &= ~desired_event;
3760 else if (event < 64)
3761 ioc->event_masks[1] &= ~desired_event;
3762 else if (event < 96)
3763 ioc->event_masks[2] &= ~desired_event;
3764 else if (event < 128)
3765 ioc->event_masks[3] &= ~desired_event;
3766}
3767
3768/**
3769 * _base_event_notification - send event notification
3770 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003771 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3772 *
3773 * Returns 0 for success, non-zero for failure.
3774 */
3775static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303776_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003777{
3778 Mpi2EventNotificationRequest_t *mpi_request;
3779 unsigned long timeleft;
3780 u16 smid;
3781 int r = 0;
3782 int i;
3783
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303784 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003785 __func__));
3786
3787 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3788 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3789 ioc->name, __func__);
3790 return -EAGAIN;
3791 }
3792
3793 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3794 if (!smid) {
3795 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3796 ioc->name, __func__);
3797 return -EAGAIN;
3798 }
3799 ioc->base_cmds.status = MPT2_CMD_PENDING;
3800 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3801 ioc->base_cmds.smid = smid;
3802 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3803 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303804 mpi_request->VF_ID = 0; /* TODO */
3805 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003806 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3807 mpi_request->EventMasks[i] =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303808 cpu_to_le32(ioc->event_masks[i]);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303809 init_completion(&ioc->base_cmds.done);
nagalakshmi.nandigama@lsi.comf01690d2011-12-01 07:43:58 +05303810 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003811 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3812 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3813 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3814 ioc->name, __func__);
3815 _debug_dump_mf(mpi_request,
3816 sizeof(Mpi2EventNotificationRequest_t)/4);
3817 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3818 r = -EFAULT;
3819 else
3820 r = -ETIME;
3821 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303822 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003823 ioc->name, __func__));
3824 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3825 return r;
3826}
3827
3828/**
3829 * mpt2sas_base_validate_event_type - validating event types
3830 * @ioc: per adapter object
3831 * @event: firmware event
3832 *
3833 * This will turn on firmware event notification when application
3834 * ask for that event. We don't mask events that are already enabled.
3835 */
3836void
3837mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3838{
3839 int i, j;
3840 u32 event_mask, desired_event;
3841 u8 send_update_to_fw;
3842
3843 for (i = 0, send_update_to_fw = 0; i <
3844 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3845 event_mask = ~event_type[i];
3846 desired_event = 1;
3847 for (j = 0; j < 32; j++) {
3848 if (!(event_mask & desired_event) &&
3849 (ioc->event_masks[i] & desired_event)) {
3850 ioc->event_masks[i] &= ~desired_event;
3851 send_update_to_fw = 1;
3852 }
3853 desired_event = (desired_event << 1);
3854 }
3855 }
3856
3857 if (!send_update_to_fw)
3858 return;
3859
3860 mutex_lock(&ioc->base_cmds.mutex);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303861 _base_event_notification(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003862 mutex_unlock(&ioc->base_cmds.mutex);
3863}
3864
3865/**
3866 * _base_diag_reset - the "big hammer" start of day reset
3867 * @ioc: per adapter object
3868 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3869 *
3870 * Returns 0 for success, non-zero for failure.
3871 */
3872static int
3873_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3874{
3875 u32 host_diagnostic;
3876 u32 ioc_state;
3877 u32 count;
3878 u32 hcb_size;
3879
3880 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303881 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
Eric Moore635374e2009-03-09 01:21:12 -06003882 ioc->name));
Eric Moore635374e2009-03-09 01:21:12 -06003883
3884 count = 0;
3885 do {
3886 /* Write magic sequence to WriteSequence register
3887 * Loop until in diagnostic mode
3888 */
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303889 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
Eric Moore635374e2009-03-09 01:21:12 -06003890 "sequence\n", ioc->name));
3891 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3892 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3893 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3894 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3895 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3896 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3897 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3898
3899 /* wait 100 msec */
3900 if (sleep_flag == CAN_SLEEP)
3901 msleep(100);
3902 else
3903 mdelay(100);
3904
3905 if (count++ > 20)
3906 goto out;
3907
3908 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303909 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
Eric Moore635374e2009-03-09 01:21:12 -06003910 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3911 ioc->name, count, host_diagnostic));
3912
3913 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3914
3915 hcb_size = readl(&ioc->chip->HCBSize);
3916
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303917 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
Eric Moore635374e2009-03-09 01:21:12 -06003918 ioc->name));
3919 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3920 &ioc->chip->HostDiagnostic);
3921
Sreekanth Reddyca6832e2013-02-01 21:55:43 +05303922 /* This delay allows the chip PCIe hardware time to finish reset tasks*/
3923 if (sleep_flag == CAN_SLEEP)
3924 msleep(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
3925 else
3926 mdelay(MPI2_HARD_RESET_PCIE_FIRST_READ_DELAY_MICRO_SEC/1000);
Eric Moore635374e2009-03-09 01:21:12 -06003927
Sreekanth Reddyca6832e2013-02-01 21:55:43 +05303928 /* Approximately 300 second max wait */
3929 for (count = 0; count < (300000000 /
3930 MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC); count++) {
Eric Moore635374e2009-03-09 01:21:12 -06003931
3932 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3933
3934 if (host_diagnostic == 0xFFFFFFFF)
3935 goto out;
3936 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3937 break;
3938
Sreekanth Reddyca6832e2013-02-01 21:55:43 +05303939 /* Wait to pass the second read delay window */
Eric Moore635374e2009-03-09 01:21:12 -06003940 if (sleep_flag == CAN_SLEEP)
Sreekanth Reddyca6832e2013-02-01 21:55:43 +05303941 msleep(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
3942 /1000);
Eric Moore635374e2009-03-09 01:21:12 -06003943 else
Sreekanth Reddyca6832e2013-02-01 21:55:43 +05303944 mdelay(MPI2_HARD_RESET_PCIE_SECOND_READ_DELAY_MICRO_SEC
3945 /1000);
Eric Moore635374e2009-03-09 01:21:12 -06003946 }
3947
3948 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3949
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303950 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
Eric Moore635374e2009-03-09 01:21:12 -06003951 "assuming the HCB Address points to good F/W\n",
3952 ioc->name));
3953 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3954 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3955 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3956
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303957 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003958 "re-enable the HCDW\n", ioc->name));
3959 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3960 &ioc->chip->HCBSize);
3961 }
3962
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303963 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
Eric Moore635374e2009-03-09 01:21:12 -06003964 ioc->name));
3965 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3966 &ioc->chip->HostDiagnostic);
3967
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303968 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
Eric Moore635374e2009-03-09 01:21:12 -06003969 "diagnostic register\n", ioc->name));
3970 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3971
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303972 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
Eric Moore635374e2009-03-09 01:21:12 -06003973 "READY state\n", ioc->name));
3974 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3975 sleep_flag);
3976 if (ioc_state) {
3977 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3978 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3979 goto out;
3980 }
3981
Eric Moore635374e2009-03-09 01:21:12 -06003982 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3983 return 0;
3984
3985 out:
3986 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3987 return -EFAULT;
3988}
3989
3990/**
3991 * _base_make_ioc_ready - put controller in READY state
3992 * @ioc: per adapter object
3993 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3994 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3995 *
3996 * Returns 0 for success, non-zero for failure.
3997 */
3998static int
3999_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4000 enum reset_type type)
4001{
4002 u32 ioc_state;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304003 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06004004
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304005 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004006 __func__));
4007
Eric Moore3cb54692010-07-08 14:44:34 -06004008 if (ioc->pci_error_recovery)
4009 return 0;
4010
Eric Moore635374e2009-03-09 01:21:12 -06004011 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304012 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
Eric Moore635374e2009-03-09 01:21:12 -06004013 ioc->name, __func__, ioc_state));
4014
4015 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
4016 return 0;
4017
4018 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304019 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06004020 "active!\n", ioc->name));
4021 goto issue_diag_reset;
4022 }
4023
4024 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
4025 mpt2sas_base_fault_info(ioc, ioc_state &
4026 MPI2_DOORBELL_DATA_MASK);
4027 goto issue_diag_reset;
4028 }
4029
4030 if (type == FORCE_BIG_HAMMER)
4031 goto issue_diag_reset;
4032
4033 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
4034 if (!(_base_send_ioc_reset(ioc,
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304035 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
4036 ioc->ioc_reset_count++;
Eric Moore635374e2009-03-09 01:21:12 -06004037 return 0;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304038 }
Eric Moore635374e2009-03-09 01:21:12 -06004039
4040 issue_diag_reset:
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05304041 rc = _base_diag_reset(ioc, CAN_SLEEP);
4042 ioc->ioc_reset_count++;
4043 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06004044}
4045
4046/**
4047 * _base_make_ioc_operational - put controller in OPERATIONAL state
4048 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004049 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4050 *
4051 * Returns 0 for success, non-zero for failure.
4052 */
4053static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304054_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06004055{
4056 int r, i;
4057 unsigned long flags;
4058 u32 reply_address;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304059 u16 smid;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304060 struct _tr_list *delayed_tr, *delayed_tr_next;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304061 u8 hide_flag;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304062 struct adapter_reply_queue *reply_q;
4063 long reply_post_free;
4064 u32 reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06004065
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304066 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004067 __func__));
4068
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304069 /* clean the delayed target reset list */
4070 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4071 &ioc->delayed_tr_list, list) {
4072 list_del(&delayed_tr->list);
4073 kfree(delayed_tr);
4074 }
4075
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304076 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
4077 &ioc->delayed_tr_volume_list, list) {
4078 list_del(&delayed_tr->list);
4079 kfree(delayed_tr);
4080 }
4081
Eric Moore635374e2009-03-09 01:21:12 -06004082 /* initialize the scsi lookup free list */
4083 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4084 INIT_LIST_HEAD(&ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304085 smid = 1;
4086 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
Kashyap, Desai35f805b2010-11-13 04:34:06 +05304087 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
Eric Moore635374e2009-03-09 01:21:12 -06004088 ioc->scsi_lookup[i].cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304089 ioc->scsi_lookup[i].smid = smid;
4090 ioc->scsi_lookup[i].scmd = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304091 ioc->scsi_lookup[i].direct_io = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004092 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
4093 &ioc->free_list);
4094 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304095
4096 /* hi-priority queue */
4097 INIT_LIST_HEAD(&ioc->hpr_free_list);
4098 smid = ioc->hi_priority_smid;
4099 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
4100 ioc->hpr_lookup[i].cb_idx = 0xFF;
4101 ioc->hpr_lookup[i].smid = smid;
4102 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
4103 &ioc->hpr_free_list);
4104 }
4105
4106 /* internal queue */
4107 INIT_LIST_HEAD(&ioc->internal_free_list);
4108 smid = ioc->internal_smid;
4109 for (i = 0; i < ioc->internal_depth; i++, smid++) {
4110 ioc->internal_lookup[i].cb_idx = 0xFF;
4111 ioc->internal_lookup[i].smid = smid;
4112 list_add_tail(&ioc->internal_lookup[i].tracker_list,
4113 &ioc->internal_free_list);
4114 }
Kashyap, Desai35f805b2010-11-13 04:34:06 +05304115
4116 /* chain pool */
4117 INIT_LIST_HEAD(&ioc->free_chain_list);
4118 for (i = 0; i < ioc->chain_depth; i++)
4119 list_add_tail(&ioc->chain_lookup[i].tracker_list,
4120 &ioc->free_chain_list);
4121
Eric Moore635374e2009-03-09 01:21:12 -06004122 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4123
4124 /* initialize Reply Free Queue */
4125 for (i = 0, reply_address = (u32)ioc->reply_dma ;
4126 i < ioc->reply_free_queue_depth ; i++, reply_address +=
4127 ioc->reply_sz)
4128 ioc->reply_free[i] = cpu_to_le32(reply_address);
4129
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304130 /* initialize reply queues */
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05304131 if (ioc->is_driver_loading)
4132 _base_assign_reply_queues(ioc);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304133
Eric Moore635374e2009-03-09 01:21:12 -06004134 /* initialize Reply Post Free Queue */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304135 reply_post_free = (long)ioc->reply_post_free;
4136 reply_post_free_sz = ioc->reply_post_queue_depth *
4137 sizeof(Mpi2DefaultReplyDescriptor_t);
4138 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4139 reply_q->reply_post_host_index = 0;
4140 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
4141 reply_post_free;
4142 for (i = 0; i < ioc->reply_post_queue_depth; i++)
4143 reply_q->reply_post_free[i].Words =
4144 cpu_to_le64(ULLONG_MAX);
4145 if (!_base_is_controller_msix_enabled(ioc))
4146 goto skip_init_reply_post_free_queue;
4147 reply_post_free += reply_post_free_sz;
4148 }
4149 skip_init_reply_post_free_queue:
Eric Moore635374e2009-03-09 01:21:12 -06004150
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304151 r = _base_send_ioc_init(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004152 if (r)
4153 return r;
4154
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304155 /* initialize reply free host index */
Eric Moore635374e2009-03-09 01:21:12 -06004156 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
Eric Moore635374e2009-03-09 01:21:12 -06004157 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304158
4159 /* initialize reply post host index */
4160 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
4161 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
4162 &ioc->chip->ReplyPostHostIndex);
4163 if (!_base_is_controller_msix_enabled(ioc))
4164 goto skip_init_reply_post_host_index;
4165 }
4166
4167 skip_init_reply_post_host_index:
Eric Moore635374e2009-03-09 01:21:12 -06004168
4169 _base_unmask_interrupts(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304170
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304171 r = _base_event_notification(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004172 if (r)
4173 return r;
4174
4175 if (sleep_flag == CAN_SLEEP)
4176 _base_static_config_pages(ioc);
4177
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304178
4179 if (ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05304180 if (ioc->is_warpdrive && ioc->manu_pg10.OEMIdentifier
4181 == 0x80) {
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05304182 hide_flag = (u8) (
4183 le32_to_cpu(ioc->manu_pg10.OEMSpecificFlags0) &
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304184 MFG_PAGE10_HIDE_SSDS_MASK);
4185 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
4186 ioc->mfg_pg10_hide_flag = hide_flag;
4187 }
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05304188 ioc->wait_for_discovery_to_complete =
4189 _base_determine_wait_on_discovery(ioc);
4190 return r; /* scan_start and scan_finished support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304191 }
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304192 r = _base_send_port_enable(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004193 if (r)
4194 return r;
4195
4196 return r;
4197}
4198
4199/**
4200 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
4201 * @ioc: per adapter object
4202 *
4203 * Return nothing.
4204 */
4205void
4206mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4207{
4208 struct pci_dev *pdev = ioc->pdev;
4209
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304210 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004211 __func__));
4212
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04004213 if (ioc->chip_phys && ioc->chip) {
4214 _base_mask_interrupts(ioc);
4215 ioc->shost_recovery = 1;
4216 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
4217 ioc->shost_recovery = 0;
4218 }
4219
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304220 _base_free_irq(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004221 _base_disable_msix(ioc);
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04004222
4223 if (ioc->chip_phys && ioc->chip)
Eric Moore635374e2009-03-09 01:21:12 -06004224 iounmap(ioc->chip);
Eric Moore635374e2009-03-09 01:21:12 -06004225 ioc->chip_phys = 0;
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04004226
4227 if (pci_is_enabled(pdev)) {
4228 pci_release_selected_regions(ioc->pdev, ioc->bars);
4229 pci_disable_pcie_error_reporting(pdev);
4230 pci_disable_device(pdev);
4231 }
Eric Moore635374e2009-03-09 01:21:12 -06004232 return;
4233}
4234
4235/**
4236 * mpt2sas_base_attach - attach controller instance
4237 * @ioc: per adapter object
4238 *
4239 * Returns 0 for success, non-zero for failure.
4240 */
4241int
4242mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4243{
4244 int r, i;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304245 int cpu_id, last_cpu_id = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004246
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304247 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004248 __func__));
4249
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304250 /* setup cpu_msix_table */
4251 ioc->cpu_count = num_online_cpus();
4252 for_each_online_cpu(cpu_id)
4253 last_cpu_id = cpu_id;
4254 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4255 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4256 ioc->reply_queue_count = 1;
4257 if (!ioc->cpu_msix_table) {
4258 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4259 "cpu_msix_table failed!!!\n", ioc->name));
4260 r = -ENOMEM;
4261 goto out_free_resources;
4262 }
4263
4264 if (ioc->is_warpdrive) {
4265 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4266 sizeof(resource_size_t *), GFP_KERNEL);
4267 if (!ioc->reply_post_host_index) {
4268 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4269 "for cpu_msix_table failed!!!\n", ioc->name));
4270 r = -ENOMEM;
4271 goto out_free_resources;
4272 }
4273 }
4274
Eric Moore635374e2009-03-09 01:21:12 -06004275 r = mpt2sas_base_map_resources(ioc);
4276 if (r)
Roland Dreierc24a1712011-11-30 17:14:22 -08004277 goto out_free_resources;
Eric Moore635374e2009-03-09 01:21:12 -06004278
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304279 if (ioc->is_warpdrive) {
Joe Lawrence42de5972014-06-25 17:04:06 -04004280 ioc->reply_post_host_index[0] = (resource_size_t __iomem *)
4281 &ioc->chip->ReplyPostHostIndex;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304282
4283 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
Joe Lawrence42de5972014-06-25 17:04:06 -04004284 ioc->reply_post_host_index[i] =
4285 (resource_size_t __iomem *)
4286 ((u8 __iomem *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304287 * 4)));
4288 }
4289
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304290 pci_set_drvdata(ioc->pdev, ioc->shost);
Kashyap, Desai96b681c2009-09-23 17:32:06 +05304291 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06004292 if (r)
4293 goto out_free_resources;
4294
Kashyap, Desai96b681c2009-09-23 17:32:06 +05304295 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Eric Moore635374e2009-03-09 01:21:12 -06004296 if (r)
4297 goto out_free_resources;
4298
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304299 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
nagalakshmi.nandigama@lsi.come42fafc2012-03-20 12:10:01 +05304300 sizeof(struct mpt2sas_port_facts), GFP_KERNEL);
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304301 if (!ioc->pfacts) {
4302 r = -ENOMEM;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304303 goto out_free_resources;
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304304 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304305
4306 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4307 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4308 if (r)
4309 goto out_free_resources;
4310 }
4311
Eric Moore635374e2009-03-09 01:21:12 -06004312 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4313 if (r)
4314 goto out_free_resources;
4315
4316 init_waitqueue_head(&ioc->reset_wq);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304317 /* allocate memory pd handle bitmask list */
4318 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4319 if (ioc->facts.MaxDevHandle % 8)
4320 ioc->pd_handles_sz++;
4321 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4322 GFP_KERNEL);
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304323 if (!ioc->pd_handles) {
4324 r = -ENOMEM;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304325 goto out_free_resources;
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304326 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304327 ioc->blocking_handles = kzalloc(ioc->pd_handles_sz,
4328 GFP_KERNEL);
4329 if (!ioc->blocking_handles) {
4330 r = -ENOMEM;
4331 goto out_free_resources;
4332 }
Kashyap, Desaie75b9b62009-12-16 18:52:39 +05304333 ioc->fwfault_debug = mpt2sas_fwfault_debug;
4334
Eric Moore635374e2009-03-09 01:21:12 -06004335 /* base internal command bits */
4336 mutex_init(&ioc->base_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004337 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4338 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4339
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304340 /* port_enable command bits */
4341 ioc->port_enable_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4342 ioc->port_enable_cmds.status = MPT2_CMD_NOT_USED;
4343
Eric Moore635374e2009-03-09 01:21:12 -06004344 /* transport internal command bits */
4345 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4346 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4347 mutex_init(&ioc->transport_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004348
Kashyap, Desaid685c262009-11-17 13:16:37 +05304349 /* scsih internal command bits */
4350 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4351 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4352 mutex_init(&ioc->scsih_cmds.mutex);
4353
Eric Moore635374e2009-03-09 01:21:12 -06004354 /* task management internal command bits */
4355 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4356 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4357 mutex_init(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004358
4359 /* config page internal command bits */
4360 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4361 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4362 mutex_init(&ioc->config_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004363
4364 /* ctl module internal command bits */
4365 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304366 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06004367 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4368 mutex_init(&ioc->ctl_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004369
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304370 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4371 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
Kashyap, Desai769578f2010-06-17 13:49:28 +05304372 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4373 !ioc->ctl_cmds.sense) {
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304374 r = -ENOMEM;
4375 goto out_free_resources;
4376 }
4377
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304378 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4379 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4380 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4381 r = -ENOMEM;
4382 goto out_free_resources;
4383 }
4384
Eric Moore635374e2009-03-09 01:21:12 -06004385 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4386 ioc->event_masks[i] = -1;
4387
4388 /* here we enable the events we care about */
4389 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4390 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4391 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4392 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4393 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4394 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4395 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4396 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4397 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
Eric Moore635374e2009-03-09 01:21:12 -06004398 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304399 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06004400 if (r)
4401 goto out_free_resources;
4402
Sreekanth Reddyb4730fb2012-12-18 14:45:30 +01004403 ioc->non_operational_loop = 0;
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05304404
Eric Moore635374e2009-03-09 01:21:12 -06004405 return 0;
4406
4407 out_free_resources:
4408
4409 ioc->remove_host = 1;
4410 mpt2sas_base_free_resources(ioc);
4411 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304412 pci_set_drvdata(ioc->pdev, NULL);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304413 kfree(ioc->cpu_msix_table);
4414 if (ioc->is_warpdrive)
4415 kfree(ioc->reply_post_host_index);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304416 kfree(ioc->pd_handles);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304417 kfree(ioc->blocking_handles);
Eric Moore635374e2009-03-09 01:21:12 -06004418 kfree(ioc->tm_cmds.reply);
4419 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304420 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004421 kfree(ioc->config_cmds.reply);
4422 kfree(ioc->base_cmds.reply);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304423 kfree(ioc->port_enable_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004424 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304425 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06004426 kfree(ioc->pfacts);
4427 ioc->ctl_cmds.reply = NULL;
4428 ioc->base_cmds.reply = NULL;
4429 ioc->tm_cmds.reply = NULL;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304430 ioc->scsih_cmds.reply = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004431 ioc->transport_cmds.reply = NULL;
4432 ioc->config_cmds.reply = NULL;
4433 ioc->pfacts = NULL;
4434 return r;
4435}
4436
4437
4438/**
4439 * mpt2sas_base_detach - remove controller instance
4440 * @ioc: per adapter object
4441 *
4442 * Return nothing.
4443 */
4444void
4445mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4446{
Eric Moore635374e2009-03-09 01:21:12 -06004447
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304448 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004449 __func__));
4450
Kashyap, Desaie4750c92009-08-07 19:37:59 +05304451 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004452 mpt2sas_base_free_resources(ioc);
4453 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304454 pci_set_drvdata(ioc->pdev, NULL);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304455 kfree(ioc->cpu_msix_table);
4456 if (ioc->is_warpdrive)
4457 kfree(ioc->reply_post_host_index);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304458 kfree(ioc->pd_handles);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304459 kfree(ioc->blocking_handles);
Eric Moore635374e2009-03-09 01:21:12 -06004460 kfree(ioc->pfacts);
4461 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304462 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06004463 kfree(ioc->base_cmds.reply);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304464 kfree(ioc->port_enable_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004465 kfree(ioc->tm_cmds.reply);
4466 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304467 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004468 kfree(ioc->config_cmds.reply);
4469}
4470
4471/**
4472 * _base_reset_handler - reset callback handler (for base)
4473 * @ioc: per adapter object
4474 * @reset_phase: phase
4475 *
4476 * The handler for doing any required cleanup or initialization.
4477 *
4478 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4479 * MPT2_IOC_DONE_RESET
4480 *
4481 * Return nothing.
4482 */
4483static void
4484_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4485{
Kashyap, Desai42244892011-01-04 11:38:39 +05304486 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4487 mpt2sas_ctl_reset_handler(ioc, reset_phase);
Eric Moore635374e2009-03-09 01:21:12 -06004488 switch (reset_phase) {
4489 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304490 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004491 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4492 break;
4493 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304494 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004495 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4496 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4497 ioc->transport_cmds.status |= MPT2_CMD_RESET;
4498 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4499 complete(&ioc->transport_cmds.done);
4500 }
4501 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4502 ioc->base_cmds.status |= MPT2_CMD_RESET;
4503 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4504 complete(&ioc->base_cmds.done);
4505 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304506 if (ioc->port_enable_cmds.status & MPT2_CMD_PENDING) {
4507 ioc->port_enable_failed = 1;
4508 ioc->port_enable_cmds.status |= MPT2_CMD_RESET;
4509 mpt2sas_base_free_smid(ioc, ioc->port_enable_cmds.smid);
4510 if (ioc->is_driver_loading) {
4511 ioc->start_scan_failed =
4512 MPI2_IOCSTATUS_INTERNAL_ERROR;
4513 ioc->start_scan = 0;
4514 ioc->port_enable_cmds.status =
4515 MPT2_CMD_NOT_USED;
4516 } else
4517 complete(&ioc->port_enable_cmds.done);
4518
4519 }
Eric Moore635374e2009-03-09 01:21:12 -06004520 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4521 ioc->config_cmds.status |= MPT2_CMD_RESET;
4522 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07004523 ioc->config_cmds.smid = USHRT_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06004524 complete(&ioc->config_cmds.done);
4525 }
4526 break;
4527 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304528 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004529 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4530 break;
4531 }
Eric Moore635374e2009-03-09 01:21:12 -06004532}
4533
4534/**
4535 * _wait_for_commands_to_complete - reset controller
4536 * @ioc: Pointer to MPT_ADAPTER structure
4537 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4538 *
4539 * This function waiting(3s) for all pending commands to complete
4540 * prior to putting controller in reset.
4541 */
4542static void
4543_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4544{
4545 u32 ioc_state;
4546 unsigned long flags;
4547 u16 i;
4548
4549 ioc->pending_io_count = 0;
4550 if (sleep_flag != CAN_SLEEP)
4551 return;
4552
4553 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4554 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4555 return;
4556
4557 /* pending command count */
4558 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304559 for (i = 0; i < ioc->scsiio_depth; i++)
Eric Moore635374e2009-03-09 01:21:12 -06004560 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4561 ioc->pending_io_count++;
4562 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4563
4564 if (!ioc->pending_io_count)
4565 return;
4566
4567 /* wait for pending commands to complete */
Kashyap, Desaid2742132010-06-17 13:28:55 +05304568 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
Eric Moore635374e2009-03-09 01:21:12 -06004569}
4570
4571/**
4572 * mpt2sas_base_hard_reset_handler - reset controller
4573 * @ioc: Pointer to MPT_ADAPTER structure
4574 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4575 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4576 *
4577 * Returns 0 for success, non-zero for failure.
4578 */
4579int
4580mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4581 enum reset_type type)
4582{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304583 int r;
Eric Moore635374e2009-03-09 01:21:12 -06004584 unsigned long flags;
4585
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304586 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004587 __func__));
4588
Eric Moore3cb54692010-07-08 14:44:34 -06004589 if (ioc->pci_error_recovery) {
4590 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4591 ioc->name, __func__);
4592 r = 0;
Alexey Khoroshilov7fbd7642011-08-26 00:36:23 +04004593 goto out_unlocked;
Eric Moore3cb54692010-07-08 14:44:34 -06004594 }
4595
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05304596 if (mpt2sas_fwfault_debug)
4597 mpt2sas_halt_firmware(ioc);
4598
Kashyap, Desaid2742132010-06-17 13:28:55 +05304599 /* TODO - What we really should be doing is pulling
4600 * out all the code associated with NO_SLEEP; its never used.
4601 * That is legacy code from mpt fusion driver, ported over.
4602 * I will leave this BUG_ON here for now till its been resolved.
4603 */
4604 BUG_ON(sleep_flag == NO_SLEEP);
4605
4606 /* wait for an active reset in progress to complete */
4607 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4608 do {
4609 ssleep(1);
4610 } while (ioc->shost_recovery == 1);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304611 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304612 __func__));
4613 return ioc->ioc_reset_in_progress_status;
Eric Moore635374e2009-03-09 01:21:12 -06004614 }
Kashyap, Desaid2742132010-06-17 13:28:55 +05304615
4616 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06004617 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004618 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4619
4620 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4621 _wait_for_commands_to_complete(ioc, sleep_flag);
4622 _base_mask_interrupts(ioc);
4623 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4624 if (r)
4625 goto out;
4626 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
Kashyap, Desai42244892011-01-04 11:38:39 +05304627
4628 /* If this hard reset is called while port enable is active, then
4629 * there is no reason to call make_ioc_operational
4630 */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05304631 if (ioc->is_driver_loading && ioc->port_enable_failed) {
4632 ioc->remove_host = 1;
Kashyap, Desai42244892011-01-04 11:38:39 +05304633 r = -EFAULT;
4634 goto out;
4635 }
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304636 r = _base_make_ioc_operational(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004637 if (!r)
4638 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4639 out:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304640 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
Eric Moore635374e2009-03-09 01:21:12 -06004641 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4642
4643 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304644 ioc->ioc_reset_in_progress_status = r;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304645 ioc->shost_recovery = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004646 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304647 mutex_unlock(&ioc->reset_in_progress_mutex);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05304648
Alexey Khoroshilov7fbd7642011-08-26 00:36:23 +04004649 out_unlocked:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304650 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304651 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06004652 return r;
4653}