blob: ef323e9a3e19f187a27129359af2238bf5b02a6a [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
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05306 * Copyright (C) 2007-2010 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
45#include <linux/version.h>
46#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/kdev_t.h>
54#include <linux/blkdev.h>
55#include <linux/delay.h>
56#include <linux/interrupt.h>
57#include <linux/dma-mapping.h>
58#include <linux/sort.h>
59#include <linux/io.h>
Kashyap, Desaia8ebd762009-09-23 17:29:29 +053060#include <linux/time.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
69static int max_queue_depth = -1;
70module_param(max_queue_depth, int, 0);
71MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
72
73static int max_sgl_entries = -1;
74module_param(max_sgl_entries, int, 0);
75MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
76
77static int msix_disable = -1;
78module_param(msix_disable, int, 0);
79MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
80
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +053081static int missing_delay[2] = {-1, -1};
82module_param_array(missing_delay, int, NULL, 0);
83MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
84
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053085/* diag_buffer_enable is bitwise
Kashyap, Desai1b01fe32009-09-23 17:28:59 +053086 * bit 0 set = TRACE
87 * bit 1 set = SNAPSHOT
88 * bit 2 set = EXTENDED
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053089 *
90 * Either bit can be set, or both
91 */
92static int diag_buffer_enable;
93module_param(diag_buffer_enable, int, 0);
Kashyap, Desai1b01fe32009-09-23 17:28:59 +053094MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
95 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053096
Kashyap, Desaic97951e2011-06-14 10:54:56 +053097static int mpt2sas_fwfault_debug;
Kashyap, Desaifa7f3162009-09-23 17:26:58 +053098MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
99 "and halt firmware - (default=0)");
100
Kashyap, Desaidd5fd332010-06-17 13:31:17 +0530101static int disable_discovery = -1;
102module_param(disable_discovery, int, 0);
103MODULE_PARM_DESC(disable_discovery, " disable discovery ");
104
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530105/**
106 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
107 *
108 */
109static int
110_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
111{
112 int ret = param_set_int(val, kp);
113 struct MPT2SAS_ADAPTER *ioc;
114
115 if (ret)
116 return ret;
117
Kashyap, Desaie75b9b62009-12-16 18:52:39 +0530118 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530119 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
120 ioc->fwfault_debug = mpt2sas_fwfault_debug;
121 return 0;
122}
123module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
124 param_get_int, &mpt2sas_fwfault_debug, 0644);
125
Eric Moore635374e2009-03-09 01:21:12 -0600126/**
127 * _base_fault_reset_work - workq handling ioc fault conditions
128 * @work: input argument, used to derive ioc
129 * Context: sleep.
130 *
131 * Return nothing.
132 */
133static void
134_base_fault_reset_work(struct work_struct *work)
135{
136 struct MPT2SAS_ADAPTER *ioc =
137 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
138 unsigned long flags;
139 u32 doorbell;
140 int rc;
141
142 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530143 if (ioc->shost_recovery)
Eric Moore635374e2009-03-09 01:21:12 -0600144 goto rearm_timer;
145 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
146
147 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
148 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
149 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
150 FORCE_BIG_HAMMER);
151 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
152 __func__, (rc == 0) ? "success" : "failed");
153 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
154 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
155 mpt2sas_base_fault_info(ioc, doorbell &
156 MPI2_DOORBELL_DATA_MASK);
157 }
158
159 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
160 rearm_timer:
161 if (ioc->fault_reset_work_q)
162 queue_delayed_work(ioc->fault_reset_work_q,
163 &ioc->fault_reset_work,
164 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
165 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
166}
167
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530168/**
169 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530170 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530171 * Context: sleep.
172 *
173 * Return nothing.
174 */
175void
176mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
177{
178 unsigned long flags;
179
180 if (ioc->fault_reset_work_q)
181 return;
182
183 /* initialize fault polling */
184 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
185 snprintf(ioc->fault_reset_work_q_name,
186 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
187 ioc->fault_reset_work_q =
188 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
189 if (!ioc->fault_reset_work_q) {
190 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
191 ioc->name, __func__, __LINE__);
192 return;
193 }
194 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
195 if (ioc->fault_reset_work_q)
196 queue_delayed_work(ioc->fault_reset_work_q,
197 &ioc->fault_reset_work,
198 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
199 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
200}
201
202/**
203 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530204 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530205 * Context: sleep.
206 *
207 * Return nothing.
208 */
209void
210mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
211{
212 unsigned long flags;
213 struct workqueue_struct *wq;
214
215 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
216 wq = ioc->fault_reset_work_q;
217 ioc->fault_reset_work_q = NULL;
218 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
219 if (wq) {
220 if (!cancel_delayed_work(&ioc->fault_reset_work))
221 flush_workqueue(wq);
222 destroy_workqueue(wq);
223 }
224}
225
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530226/**
227 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
228 * @ioc: per adapter object
229 * @fault_code: fault code
230 *
231 * Return nothing.
232 */
233void
234mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
235{
236 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
237 ioc->name, fault_code);
238}
239
240/**
241 * mpt2sas_halt_firmware - halt's mpt controller firmware
242 * @ioc: per adapter object
243 *
244 * For debugging timeout related issues. Writing 0xCOFFEE00
245 * to the doorbell register will halt controller firmware. With
246 * the purpose to stop both driver and firmware, the enduser can
247 * obtain a ring buffer from controller UART.
248 */
249void
250mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
251{
252 u32 doorbell;
253
254 if (!ioc->fwfault_debug)
255 return;
256
257 dump_stack();
258
259 doorbell = readl(&ioc->chip->Doorbell);
260 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
261 mpt2sas_base_fault_info(ioc , doorbell);
262 else {
263 writel(0xC0FFEE00, &ioc->chip->Doorbell);
264 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
265 "timeout\n", ioc->name);
266 }
267
268 panic("panic in %s\n", __func__);
269}
270
Eric Moore635374e2009-03-09 01:21:12 -0600271#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
272/**
273 * _base_sas_ioc_info - verbose translation of the ioc status
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530274 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600275 * @mpi_reply: reply mf payload returned from firmware
276 * @request_hdr: request mf
277 *
278 * Return nothing.
279 */
280static void
281_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
282 MPI2RequestHeader_t *request_hdr)
283{
284 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
285 MPI2_IOCSTATUS_MASK;
286 char *desc = NULL;
287 u16 frame_sz;
288 char *func_str = NULL;
289
290 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
291 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
292 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
293 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
294 return;
295
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530296 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
297 return;
298
Eric Moore635374e2009-03-09 01:21:12 -0600299 switch (ioc_status) {
300
301/****************************************************************************
302* Common IOCStatus values for all replies
303****************************************************************************/
304
305 case MPI2_IOCSTATUS_INVALID_FUNCTION:
306 desc = "invalid function";
307 break;
308 case MPI2_IOCSTATUS_BUSY:
309 desc = "busy";
310 break;
311 case MPI2_IOCSTATUS_INVALID_SGL:
312 desc = "invalid sgl";
313 break;
314 case MPI2_IOCSTATUS_INTERNAL_ERROR:
315 desc = "internal error";
316 break;
317 case MPI2_IOCSTATUS_INVALID_VPID:
318 desc = "invalid vpid";
319 break;
320 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
321 desc = "insufficient resources";
322 break;
323 case MPI2_IOCSTATUS_INVALID_FIELD:
324 desc = "invalid field";
325 break;
326 case MPI2_IOCSTATUS_INVALID_STATE:
327 desc = "invalid state";
328 break;
329 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
330 desc = "op state not supported";
331 break;
332
333/****************************************************************************
334* Config IOCStatus values
335****************************************************************************/
336
337 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
338 desc = "config invalid action";
339 break;
340 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
341 desc = "config invalid type";
342 break;
343 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
344 desc = "config invalid page";
345 break;
346 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
347 desc = "config invalid data";
348 break;
349 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
350 desc = "config no defaults";
351 break;
352 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
353 desc = "config cant commit";
354 break;
355
356/****************************************************************************
357* SCSI IO Reply
358****************************************************************************/
359
360 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
361 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
362 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
363 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
364 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
365 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
366 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
367 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
368 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
369 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
370 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
371 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
372 break;
373
374/****************************************************************************
375* For use by SCSI Initiator and SCSI Target end-to-end data protection
376****************************************************************************/
377
378 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
379 desc = "eedp guard error";
380 break;
381 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
382 desc = "eedp ref tag error";
383 break;
384 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
385 desc = "eedp app tag error";
386 break;
387
388/****************************************************************************
389* SCSI Target values
390****************************************************************************/
391
392 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
393 desc = "target invalid io index";
394 break;
395 case MPI2_IOCSTATUS_TARGET_ABORTED:
396 desc = "target aborted";
397 break;
398 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
399 desc = "target no conn retryable";
400 break;
401 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
402 desc = "target no connection";
403 break;
404 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
405 desc = "target xfer count mismatch";
406 break;
407 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
408 desc = "target data offset error";
409 break;
410 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
411 desc = "target too much write data";
412 break;
413 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
414 desc = "target iu too short";
415 break;
416 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
417 desc = "target ack nak timeout";
418 break;
419 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
420 desc = "target nak received";
421 break;
422
423/****************************************************************************
424* Serial Attached SCSI values
425****************************************************************************/
426
427 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
428 desc = "smp request failed";
429 break;
430 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
431 desc = "smp data overrun";
432 break;
433
434/****************************************************************************
435* Diagnostic Buffer Post / Diagnostic Release values
436****************************************************************************/
437
438 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
439 desc = "diagnostic released";
440 break;
441 default:
442 break;
443 }
444
445 if (!desc)
446 return;
447
448 switch (request_hdr->Function) {
449 case MPI2_FUNCTION_CONFIG:
450 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
451 func_str = "config_page";
452 break;
453 case MPI2_FUNCTION_SCSI_TASK_MGMT:
454 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
455 func_str = "task_mgmt";
456 break;
457 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
458 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
459 func_str = "sas_iounit_ctl";
460 break;
461 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
462 frame_sz = sizeof(Mpi2SepRequest_t);
463 func_str = "enclosure";
464 break;
465 case MPI2_FUNCTION_IOC_INIT:
466 frame_sz = sizeof(Mpi2IOCInitRequest_t);
467 func_str = "ioc_init";
468 break;
469 case MPI2_FUNCTION_PORT_ENABLE:
470 frame_sz = sizeof(Mpi2PortEnableRequest_t);
471 func_str = "port_enable";
472 break;
473 case MPI2_FUNCTION_SMP_PASSTHROUGH:
474 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
475 func_str = "smp_passthru";
476 break;
477 default:
478 frame_sz = 32;
479 func_str = "unknown";
480 break;
481 }
482
483 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
484 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
485
486 _debug_dump_mf(request_hdr, frame_sz/4);
487}
488
489/**
490 * _base_display_event_data - verbose translation of firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530491 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600492 * @mpi_reply: reply mf payload returned from firmware
493 *
494 * Return nothing.
495 */
496static void
497_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
498 Mpi2EventNotificationReply_t *mpi_reply)
499{
500 char *desc = NULL;
501 u16 event;
502
503 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
504 return;
505
506 event = le16_to_cpu(mpi_reply->Event);
507
508 switch (event) {
509 case MPI2_EVENT_LOG_DATA:
510 desc = "Log Data";
511 break;
512 case MPI2_EVENT_STATE_CHANGE:
513 desc = "Status Change";
514 break;
515 case MPI2_EVENT_HARD_RESET_RECEIVED:
516 desc = "Hard Reset Received";
517 break;
518 case MPI2_EVENT_EVENT_CHANGE:
519 desc = "Event Change";
520 break;
Eric Moore635374e2009-03-09 01:21:12 -0600521 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
522 desc = "Device Status Change";
523 break;
524 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530525 if (!ioc->hide_ir_msg)
526 desc = "IR Operation Status";
Eric Moore635374e2009-03-09 01:21:12 -0600527 break;
528 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530529 {
530 Mpi2EventDataSasDiscovery_t *event_data =
531 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
532 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
533 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
534 "start" : "stop");
535 if (event_data->DiscoveryStatus)
536 printk("discovery_status(0x%08x)",
537 le32_to_cpu(event_data->DiscoveryStatus));
Julia Lawall7968f192010-08-05 22:19:36 +0200538 printk("\n");
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530539 return;
540 }
Eric Moore635374e2009-03-09 01:21:12 -0600541 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
542 desc = "SAS Broadcast Primitive";
543 break;
544 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
545 desc = "SAS Init Device Status Change";
546 break;
547 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
548 desc = "SAS Init Table Overflow";
549 break;
550 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
551 desc = "SAS Topology Change List";
552 break;
553 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
554 desc = "SAS Enclosure Device Status Change";
555 break;
556 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530557 if (!ioc->hide_ir_msg)
558 desc = "IR Volume";
Eric Moore635374e2009-03-09 01:21:12 -0600559 break;
560 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530561 if (!ioc->hide_ir_msg)
562 desc = "IR Physical Disk";
Eric Moore635374e2009-03-09 01:21:12 -0600563 break;
564 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530565 if (!ioc->hide_ir_msg)
566 desc = "IR Configuration Change List";
Eric Moore635374e2009-03-09 01:21:12 -0600567 break;
568 case MPI2_EVENT_LOG_ENTRY_ADDED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530569 if (!ioc->hide_ir_msg)
570 desc = "Log Entry Added";
Eric Moore635374e2009-03-09 01:21:12 -0600571 break;
572 }
573
574 if (!desc)
575 return;
576
577 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
578}
579#endif
580
581/**
582 * _base_sas_log_info - verbose translation of firmware log info
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530583 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600584 * @log_info: log info
585 *
586 * Return nothing.
587 */
588static void
589_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
590{
591 union loginfo_type {
592 u32 loginfo;
593 struct {
594 u32 subcode:16;
595 u32 code:8;
596 u32 originator:4;
597 u32 bus_type:4;
598 } dw;
599 };
600 union loginfo_type sas_loginfo;
601 char *originator_str = NULL;
602
603 sas_loginfo.loginfo = log_info;
604 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
605 return;
606
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +0530607 /* each nexus loss loginfo */
608 if (log_info == 0x31170000)
609 return;
610
Eric Moore635374e2009-03-09 01:21:12 -0600611 /* eat the loginfos associated with task aborts */
612 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
613 0x31140000 || log_info == 0x31130000))
614 return;
615
616 switch (sas_loginfo.dw.originator) {
617 case 0:
618 originator_str = "IOP";
619 break;
620 case 1:
621 originator_str = "PL";
622 break;
623 case 2:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530624 if (!ioc->hide_ir_msg)
625 originator_str = "IR";
626 else
627 originator_str = "WarpDrive";
Eric Moore635374e2009-03-09 01:21:12 -0600628 break;
629 }
630
631 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
632 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
633 originator_str, sas_loginfo.dw.code,
634 sas_loginfo.dw.subcode);
635}
636
637/**
Eric Moore635374e2009-03-09 01:21:12 -0600638 * _base_display_reply_info -
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530639 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600640 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530641 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600642 * @reply: reply message frame(lower 32bit addr)
643 *
644 * Return nothing.
645 */
646static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530647_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600648 u32 reply)
649{
650 MPI2DefaultReply_t *mpi_reply;
651 u16 ioc_status;
652
653 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
654 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
655#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
656 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
657 (ioc->logging_level & MPT_DEBUG_REPLY)) {
658 _base_sas_ioc_info(ioc , mpi_reply,
659 mpt2sas_base_get_msg_frame(ioc, smid));
660 }
661#endif
662 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
663 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
664}
665
666/**
667 * mpt2sas_base_done - base internal command completion routine
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530668 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600669 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530670 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600671 * @reply: reply message frame(lower 32bit addr)
672 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530673 * Return 1 meaning mf should be freed from _base_interrupt
674 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600675 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530676u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530677mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
678 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600679{
680 MPI2DefaultReply_t *mpi_reply;
681
682 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
683 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530684 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600685
686 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530687 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600688
689 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
690 if (mpi_reply) {
691 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
692 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
693 }
694 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
695 complete(&ioc->base_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530696 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600697}
698
699/**
700 * _base_async_event - main callback handler for firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530701 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530702 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600703 * @reply: reply message frame(lower 32bit addr)
704 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530705 * Return 1 meaning mf should be freed from _base_interrupt
706 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600707 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530708static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530709_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600710{
711 Mpi2EventNotificationReply_t *mpi_reply;
712 Mpi2EventAckRequest_t *ack_request;
713 u16 smid;
714
715 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
716 if (!mpi_reply)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530717 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600718 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530719 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600720#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
721 _base_display_event_data(ioc, mpi_reply);
722#endif
723 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
724 goto out;
725 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
726 if (!smid) {
727 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
728 ioc->name, __func__);
729 goto out;
730 }
731
732 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
733 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
734 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
735 ack_request->Event = mpi_reply->Event;
736 ack_request->EventContext = mpi_reply->EventContext;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530737 ack_request->VF_ID = 0; /* TODO */
738 ack_request->VP_ID = 0;
739 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600740
741 out:
742
743 /* scsih callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530744 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600745
746 /* ctl callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530747 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530748
749 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600750}
751
752/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530753 * _base_get_cb_idx - obtain the callback index
754 * @ioc: per adapter object
755 * @smid: system request message index
756 *
757 * Return callback index.
758 */
759static u8
760_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
761{
762 int i;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +0530763 u8 cb_idx;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530764
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +0530765 if (smid < ioc->hi_priority_smid) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530766 i = smid - 1;
767 cb_idx = ioc->scsi_lookup[i].cb_idx;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +0530768 } else if (smid < ioc->internal_smid) {
769 i = smid - ioc->hi_priority_smid;
770 cb_idx = ioc->hpr_lookup[i].cb_idx;
771 } else if (smid <= ioc->hba_queue_depth) {
772 i = smid - ioc->internal_smid;
773 cb_idx = ioc->internal_lookup[i].cb_idx;
774 } else
775 cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530776 return cb_idx;
777}
778
779/**
Eric Moore635374e2009-03-09 01:21:12 -0600780 * _base_mask_interrupts - disable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530781 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600782 *
783 * Disabling ResetIRQ, Reply and Doorbell Interrupts
784 *
785 * Return nothing.
786 */
787static void
788_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
789{
790 u32 him_register;
791
792 ioc->mask_interrupts = 1;
793 him_register = readl(&ioc->chip->HostInterruptMask);
794 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
795 writel(him_register, &ioc->chip->HostInterruptMask);
796 readl(&ioc->chip->HostInterruptMask);
797}
798
799/**
800 * _base_unmask_interrupts - enable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530801 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600802 *
803 * Enabling only Reply Interrupts
804 *
805 * Return nothing.
806 */
807static void
808_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
809{
810 u32 him_register;
811
Eric Moore635374e2009-03-09 01:21:12 -0600812 him_register = readl(&ioc->chip->HostInterruptMask);
813 him_register &= ~MPI2_HIM_RIM;
814 writel(him_register, &ioc->chip->HostInterruptMask);
815 ioc->mask_interrupts = 0;
816}
817
Kashyap, Desai5b768582009-08-20 13:24:31 +0530818union reply_descriptor {
819 u64 word;
820 struct {
821 u32 low;
822 u32 high;
823 } u;
824};
825
Eric Moore635374e2009-03-09 01:21:12 -0600826/**
827 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
828 * @irq: irq number (not used)
829 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
830 * @r: pt_regs pointer (not used)
831 *
832 * Return IRQ_HANDLE if processed, else IRQ_NONE.
833 */
834static irqreturn_t
835_base_interrupt(int irq, void *bus_id)
836{
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530837 struct adapter_reply_queue *reply_q = bus_id;
Eric Moore03ea1112009-04-21 15:37:57 -0600838 union reply_descriptor rd;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530839 u32 completed_cmds;
Eric Moore635374e2009-03-09 01:21:12 -0600840 u8 request_desript_type;
841 u16 smid;
842 u8 cb_idx;
843 u32 reply;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530844 u8 msix_index = reply_q->msix_index;
845 struct MPT2SAS_ADAPTER *ioc = reply_q->ioc;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530846 Mpi2ReplyDescriptorsUnion_t *rpf;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530847 u8 rc;
Eric Moore635374e2009-03-09 01:21:12 -0600848
849 if (ioc->mask_interrupts)
850 return IRQ_NONE;
851
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530852 if (!atomic_add_unless(&reply_q->busy, 1, 1))
853 return IRQ_NONE;
854
855 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
Kashyap, Desai5b768582009-08-20 13:24:31 +0530856 request_desript_type = rpf->Default.ReplyFlags
857 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530858 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
859 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600860 return IRQ_NONE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530861 }
Eric Moore635374e2009-03-09 01:21:12 -0600862
863 completed_cmds = 0;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530864 cb_idx = 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -0600865 do {
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530866 rd.word = le64_to_cpu(rpf->Words);
Eric Moore03ea1112009-04-21 15:37:57 -0600867 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
Eric Moore635374e2009-03-09 01:21:12 -0600868 goto out;
869 reply = 0;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530870 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Eric Moore635374e2009-03-09 01:21:12 -0600871 if (request_desript_type ==
872 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530873 reply = le32_to_cpu
874 (rpf->AddressReply.ReplyFrameAddress);
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530875 if (reply > ioc->reply_dma_max_address ||
876 reply < ioc->reply_dma_min_address)
877 reply = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600878 } else if (request_desript_type ==
879 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
880 goto next;
881 else if (request_desript_type ==
882 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
883 goto next;
884 if (smid)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530885 cb_idx = _base_get_cb_idx(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600886 if (smid && cb_idx != 0xFF) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530887 rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530888 reply);
Eric Moore635374e2009-03-09 01:21:12 -0600889 if (reply)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530890 _base_display_reply_info(ioc, smid, msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600891 reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530892 if (rc)
893 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600894 }
895 if (!smid)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530896 _base_async_event(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600897
898 /* reply free queue handling */
899 if (reply) {
900 ioc->reply_free_host_index =
901 (ioc->reply_free_host_index ==
902 (ioc->reply_free_queue_depth - 1)) ?
903 0 : ioc->reply_free_host_index + 1;
904 ioc->reply_free[ioc->reply_free_host_index] =
905 cpu_to_le32(reply);
Kashyap, Desai5b768582009-08-20 13:24:31 +0530906 wmb();
Eric Moore635374e2009-03-09 01:21:12 -0600907 writel(ioc->reply_free_host_index,
908 &ioc->chip->ReplyFreeHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600909 }
910
911 next:
Kashyap, Desai5b768582009-08-20 13:24:31 +0530912
Kashyap, Desaic97951e2011-06-14 10:54:56 +0530913 rpf->Words = cpu_to_le64(ULLONG_MAX);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530914 reply_q->reply_post_host_index =
915 (reply_q->reply_post_host_index ==
Kashyap, Desai5b768582009-08-20 13:24:31 +0530916 (ioc->reply_post_queue_depth - 1)) ? 0 :
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530917 reply_q->reply_post_host_index + 1;
Eric Moore635374e2009-03-09 01:21:12 -0600918 request_desript_type =
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530919 reply_q->reply_post_free[reply_q->reply_post_host_index].
920 Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600921 completed_cmds++;
922 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
923 goto out;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530924 if (!reply_q->reply_post_host_index)
925 rpf = reply_q->reply_post_free;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530926 else
927 rpf++;
Eric Moore635374e2009-03-09 01:21:12 -0600928 } while (1);
929
930 out:
931
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530932 if (!completed_cmds) {
933 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600934 return IRQ_NONE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530935 }
Eric Moore635374e2009-03-09 01:21:12 -0600936 wmb();
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530937 if (ioc->is_warpdrive) {
938 writel(reply_q->reply_post_host_index,
939 ioc->reply_post_host_index[msix_index]);
940 atomic_dec(&reply_q->busy);
941 return IRQ_HANDLED;
942 }
943 writel(reply_q->reply_post_host_index | (msix_index <<
944 MPI2_RPHI_MSIX_INDEX_SHIFT), &ioc->chip->ReplyPostHostIndex);
945 atomic_dec(&reply_q->busy);
Eric Moore635374e2009-03-09 01:21:12 -0600946 return IRQ_HANDLED;
947}
948
949/**
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +0530950 * _base_is_controller_msix_enabled - is controller support muli-reply queues
951 * @ioc: per adapter object
952 *
953 */
954static inline int
955_base_is_controller_msix_enabled(struct MPT2SAS_ADAPTER *ioc)
956{
957 return (ioc->facts.IOCCapabilities &
958 MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
959}
960
961/**
962 * mpt2sas_base_flush_reply_queues - flushing the MSIX reply queues
963 * @ioc: per adapter object
964 * Context: ISR conext
965 *
966 * Called when a Task Management request has completed. We want
967 * to flush the other reply queues so all the outstanding IO has been
968 * completed back to OS before we process the TM completetion.
969 *
970 * Return nothing.
971 */
972void
973mpt2sas_base_flush_reply_queues(struct MPT2SAS_ADAPTER *ioc)
974{
975 struct adapter_reply_queue *reply_q;
976
977 /* If MSIX capability is turned off
978 * then multi-queues are not enabled
979 */
980 if (!_base_is_controller_msix_enabled(ioc))
981 return;
982
983 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
984 if (ioc->shost_recovery)
985 return;
986 /* TMs are on msix_index == 0 */
987 if (reply_q->msix_index == 0)
988 continue;
989 _base_interrupt(reply_q->vector, (void *)reply_q);
990 }
991}
992
993/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300994 * mpt2sas_base_release_callback_handler - clear interrupt callback handler
Eric Moore635374e2009-03-09 01:21:12 -0600995 * @cb_idx: callback index
996 *
997 * Return nothing.
998 */
999void
1000mpt2sas_base_release_callback_handler(u8 cb_idx)
1001{
1002 mpt_callbacks[cb_idx] = NULL;
1003}
1004
1005/**
1006 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
1007 * @cb_func: callback function
1008 *
1009 * Returns cb_func.
1010 */
1011u8
1012mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
1013{
1014 u8 cb_idx;
1015
1016 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
1017 if (mpt_callbacks[cb_idx] == NULL)
1018 break;
1019
1020 mpt_callbacks[cb_idx] = cb_func;
1021 return cb_idx;
1022}
1023
1024/**
1025 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
1026 *
1027 * Return nothing.
1028 */
1029void
1030mpt2sas_base_initialize_callback_handler(void)
1031{
1032 u8 cb_idx;
1033
1034 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
1035 mpt2sas_base_release_callback_handler(cb_idx);
1036}
1037
1038/**
1039 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
1040 * @ioc: per adapter object
1041 * @paddr: virtual address for SGE
1042 *
1043 * Create a zero length scatter gather entry to insure the IOCs hardware has
1044 * something to use if the target device goes brain dead and tries
1045 * to send data even when none is asked for.
1046 *
1047 * Return nothing.
1048 */
1049void
1050mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
1051{
1052 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
1053 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
1054 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
1055 MPI2_SGE_FLAGS_SHIFT);
1056 ioc->base_add_sg_single(paddr, flags_length, -1);
1057}
1058
1059/**
1060 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1061 * @paddr: virtual address for SGE
1062 * @flags_length: SGE flags and data transfer length
1063 * @dma_addr: Physical address
1064 *
1065 * Return nothing.
1066 */
1067static void
1068_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1069{
1070 Mpi2SGESimple32_t *sgel = paddr;
1071
1072 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1073 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1074 sgel->FlagsLength = cpu_to_le32(flags_length);
1075 sgel->Address = cpu_to_le32(dma_addr);
1076}
1077
1078
1079/**
1080 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1081 * @paddr: virtual address for SGE
1082 * @flags_length: SGE flags and data transfer length
1083 * @dma_addr: Physical address
1084 *
1085 * Return nothing.
1086 */
1087static void
1088_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1089{
1090 Mpi2SGESimple64_t *sgel = paddr;
1091
1092 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1093 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1094 sgel->FlagsLength = cpu_to_le32(flags_length);
1095 sgel->Address = cpu_to_le64(dma_addr);
1096}
1097
1098#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1099
1100/**
1101 * _base_config_dma_addressing - set dma addressing
1102 * @ioc: per adapter object
1103 * @pdev: PCI device struct
1104 *
1105 * Returns 0 for success, non-zero for failure.
1106 */
1107static int
1108_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1109{
1110 struct sysinfo s;
1111 char *desc = NULL;
1112
1113 if (sizeof(dma_addr_t) > 4) {
1114 const uint64_t required_mask =
1115 dma_get_required_mask(&pdev->dev);
Yang Hongyange9304382009-04-13 14:40:14 -07001116 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1117 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1118 DMA_BIT_MASK(64))) {
Eric Moore635374e2009-03-09 01:21:12 -06001119 ioc->base_add_sg_single = &_base_add_sg_single_64;
1120 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1121 desc = "64";
1122 goto out;
1123 }
1124 }
1125
Yang Hongyange9304382009-04-13 14:40:14 -07001126 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1127 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eric Moore635374e2009-03-09 01:21:12 -06001128 ioc->base_add_sg_single = &_base_add_sg_single_32;
1129 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1130 desc = "32";
1131 } else
1132 return -ENODEV;
1133
1134 out:
1135 si_meminfo(&s);
1136 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1137 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1138
1139 return 0;
1140}
1141
1142/**
Eric Moore635374e2009-03-09 01:21:12 -06001143 * _base_check_enable_msix - checks MSIX capabable.
1144 * @ioc: per adapter object
1145 *
1146 * Check to see if card is capable of MSIX, and set number
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001147 * of available msix vectors
Eric Moore635374e2009-03-09 01:21:12 -06001148 */
1149static int
1150_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1151{
1152 int base;
1153 u16 message_control;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301154
Eric Moore635374e2009-03-09 01:21:12 -06001155
1156 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1157 if (!base) {
1158 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1159 "supported\n", ioc->name));
1160 return -EINVAL;
1161 }
1162
1163 /* get msix vector count */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301164 /* NUMA_IO not supported for older controllers */
1165 if (ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2004 ||
1166 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008 ||
1167 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_1 ||
1168 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_2 ||
1169 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2108_3 ||
1170 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_1 ||
1171 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2116_2)
1172 ioc->msix_vector_count = 1;
1173 else {
1174 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1175 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1176 }
Eric Moore635374e2009-03-09 01:21:12 -06001177 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301178 "vector_count(%d)\n", ioc->name, ioc->msix_vector_count));
1179
Eric Moore635374e2009-03-09 01:21:12 -06001180 return 0;
1181}
1182
1183/**
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301184 * _base_free_irq - free irq
1185 * @ioc: per adapter object
1186 *
1187 * Freeing respective reply_queue from the list.
1188 */
1189static void
1190_base_free_irq(struct MPT2SAS_ADAPTER *ioc)
1191{
1192 struct adapter_reply_queue *reply_q, *next;
1193
1194 if (list_empty(&ioc->reply_queue_list))
1195 return;
1196
1197 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
1198 list_del(&reply_q->list);
1199 synchronize_irq(reply_q->vector);
1200 free_irq(reply_q->vector, reply_q);
1201 kfree(reply_q);
1202 }
1203}
1204
1205/**
1206 * _base_request_irq - request irq
1207 * @ioc: per adapter object
1208 * @index: msix index into vector table
1209 * @vector: irq vector
1210 *
1211 * Inserting respective reply_queue into the list.
1212 */
1213static int
1214_base_request_irq(struct MPT2SAS_ADAPTER *ioc, u8 index, u32 vector)
1215{
1216 struct adapter_reply_queue *reply_q;
1217 int r;
1218
1219 reply_q = kzalloc(sizeof(struct adapter_reply_queue), GFP_KERNEL);
1220 if (!reply_q) {
1221 printk(MPT2SAS_ERR_FMT "unable to allocate memory %d!\n",
1222 ioc->name, (int)sizeof(struct adapter_reply_queue));
1223 return -ENOMEM;
1224 }
1225 reply_q->ioc = ioc;
1226 reply_q->msix_index = index;
1227 reply_q->vector = vector;
1228 atomic_set(&reply_q->busy, 0);
1229 if (ioc->msix_enable)
1230 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d-msix%d",
1231 MPT2SAS_DRIVER_NAME, ioc->id, index);
1232 else
1233 snprintf(reply_q->name, MPT_NAME_LENGTH, "%s%d",
1234 MPT2SAS_DRIVER_NAME, ioc->id);
1235 r = request_irq(vector, _base_interrupt, IRQF_SHARED, reply_q->name,
1236 reply_q);
1237 if (r) {
1238 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1239 reply_q->name, vector);
1240 kfree(reply_q);
1241 return -EBUSY;
1242 }
1243
1244 INIT_LIST_HEAD(&reply_q->list);
1245 list_add_tail(&reply_q->list, &ioc->reply_queue_list);
1246 return 0;
1247}
1248
1249/**
1250 * _base_assign_reply_queues - assigning msix index for each cpu
1251 * @ioc: per adapter object
1252 *
1253 * The enduser would need to set the affinity via /proc/irq/#/smp_affinity
1254 *
1255 * It would nice if we could call irq_set_affinity, however it is not
1256 * an exported symbol
1257 */
1258static void
1259_base_assign_reply_queues(struct MPT2SAS_ADAPTER *ioc)
1260{
1261 struct adapter_reply_queue *reply_q;
1262 int cpu_id;
1263 int cpu_grouping, loop, grouping, grouping_mod;
1264
1265 if (!_base_is_controller_msix_enabled(ioc))
1266 return;
1267
1268 memset(ioc->cpu_msix_table, 0, ioc->cpu_msix_table_sz);
1269 /* when there are more cpus than available msix vectors,
1270 * then group cpus togeather on same irq
1271 */
1272 if (ioc->cpu_count > ioc->msix_vector_count) {
1273 grouping = ioc->cpu_count / ioc->msix_vector_count;
1274 grouping_mod = ioc->cpu_count % ioc->msix_vector_count;
1275 if (grouping < 2 || (grouping == 2 && !grouping_mod))
1276 cpu_grouping = 2;
1277 else if (grouping < 4 || (grouping == 4 && !grouping_mod))
1278 cpu_grouping = 4;
1279 else if (grouping < 8 || (grouping == 8 && !grouping_mod))
1280 cpu_grouping = 8;
1281 else
1282 cpu_grouping = 16;
1283 } else
1284 cpu_grouping = 0;
1285
1286 loop = 0;
1287 reply_q = list_entry(ioc->reply_queue_list.next,
1288 struct adapter_reply_queue, list);
1289 for_each_online_cpu(cpu_id) {
1290 if (!cpu_grouping) {
1291 ioc->cpu_msix_table[cpu_id] = reply_q->msix_index;
1292 reply_q = list_entry(reply_q->list.next,
1293 struct adapter_reply_queue, list);
1294 } else {
1295 if (loop < cpu_grouping) {
1296 ioc->cpu_msix_table[cpu_id] =
1297 reply_q->msix_index;
1298 loop++;
1299 } else {
1300 reply_q = list_entry(reply_q->list.next,
1301 struct adapter_reply_queue, list);
1302 ioc->cpu_msix_table[cpu_id] =
1303 reply_q->msix_index;
1304 loop = 1;
1305 }
1306 }
1307 }
1308}
1309
1310/**
Eric Moore635374e2009-03-09 01:21:12 -06001311 * _base_disable_msix - disables msix
1312 * @ioc: per adapter object
1313 *
1314 */
1315static void
1316_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1317{
1318 if (ioc->msix_enable) {
1319 pci_disable_msix(ioc->pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001320 ioc->msix_enable = 0;
1321 }
1322}
1323
1324/**
1325 * _base_enable_msix - enables msix, failback to io_apic
1326 * @ioc: per adapter object
1327 *
1328 */
1329static int
1330_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1331{
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301332 struct msix_entry *entries, *a;
Eric Moore635374e2009-03-09 01:21:12 -06001333 int r;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301334 int i;
Eric Moore635374e2009-03-09 01:21:12 -06001335 u8 try_msix = 0;
1336
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301337 INIT_LIST_HEAD(&ioc->reply_queue_list);
1338
Eric Moore635374e2009-03-09 01:21:12 -06001339 if (msix_disable == -1 || msix_disable == 0)
1340 try_msix = 1;
1341
1342 if (!try_msix)
1343 goto try_ioapic;
1344
1345 if (_base_check_enable_msix(ioc) != 0)
1346 goto try_ioapic;
1347
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301348 ioc->reply_queue_count = min_t(u8, ioc->cpu_count,
1349 ioc->msix_vector_count);
1350
1351 entries = kcalloc(ioc->reply_queue_count, sizeof(struct msix_entry),
1352 GFP_KERNEL);
1353 if (!entries) {
1354 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "kcalloc "
1355 "failed @ at %s:%d/%s() !!!\n", ioc->name, __FILE__,
1356 __LINE__, __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001357 goto try_ioapic;
1358 }
1359
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301360 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++)
1361 a->entry = i;
1362
1363 r = pci_enable_msix(ioc->pdev, entries, ioc->reply_queue_count);
Eric Moore635374e2009-03-09 01:21:12 -06001364 if (r) {
1365 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1366 "failed (r=%d) !!!\n", ioc->name, r));
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301367 kfree(entries);
Eric Moore635374e2009-03-09 01:21:12 -06001368 goto try_ioapic;
1369 }
1370
Eric Moore635374e2009-03-09 01:21:12 -06001371 ioc->msix_enable = 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301372 for (i = 0, a = entries; i < ioc->reply_queue_count; i++, a++) {
1373 r = _base_request_irq(ioc, i, a->vector);
1374 if (r) {
1375 _base_free_irq(ioc);
1376 _base_disable_msix(ioc);
1377 kfree(entries);
1378 goto try_ioapic;
1379 }
1380 }
1381
1382 kfree(entries);
Eric Moore635374e2009-03-09 01:21:12 -06001383 return 0;
1384
1385/* failback to io_apic interrupt routing */
1386 try_ioapic:
1387
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301388 r = _base_request_irq(ioc, 0, ioc->pdev->irq);
Eric Moore635374e2009-03-09 01:21:12 -06001389
Eric Moore635374e2009-03-09 01:21:12 -06001390 return r;
1391}
1392
1393/**
1394 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1395 * @ioc: per adapter object
1396 *
1397 * Returns 0 for success, non-zero for failure.
1398 */
1399int
1400mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1401{
1402 struct pci_dev *pdev = ioc->pdev;
1403 u32 memap_sz;
1404 u32 pio_sz;
1405 int i, r = 0;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301406 u64 pio_chip = 0;
1407 u64 chip_phys = 0;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301408 struct adapter_reply_queue *reply_q;
Eric Moore635374e2009-03-09 01:21:12 -06001409
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301410 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
Eric Moore635374e2009-03-09 01:21:12 -06001411 ioc->name, __func__));
1412
1413 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1414 if (pci_enable_device_mem(pdev)) {
1415 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1416 "failed\n", ioc->name);
1417 return -ENODEV;
1418 }
1419
1420
1421 if (pci_request_selected_regions(pdev, ioc->bars,
1422 MPT2SAS_DRIVER_NAME)) {
1423 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1424 "failed\n", ioc->name);
1425 r = -ENODEV;
1426 goto out_fail;
1427 }
1428
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301429 /* AER (Advanced Error Reporting) hooks */
1430 pci_enable_pcie_error_reporting(pdev);
1431
Eric Moore635374e2009-03-09 01:21:12 -06001432 pci_set_master(pdev);
1433
1434 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1435 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1436 ioc->name, pci_name(pdev));
1437 r = -ENODEV;
1438 goto out_fail;
1439 }
1440
1441 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
Richard A Laryfc193172010-03-12 15:27:06 -08001442 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Eric Moore635374e2009-03-09 01:21:12 -06001443 if (pio_sz)
1444 continue;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301445 pio_chip = (u64)pci_resource_start(pdev, i);
Eric Moore635374e2009-03-09 01:21:12 -06001446 pio_sz = pci_resource_len(pdev, i);
1447 } else {
1448 if (memap_sz)
1449 continue;
Richard A Laryfc193172010-03-12 15:27:06 -08001450 /* verify memory resource is valid before using */
1451 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1452 ioc->chip_phys = pci_resource_start(pdev, i);
1453 chip_phys = (u64)ioc->chip_phys;
1454 memap_sz = pci_resource_len(pdev, i);
1455 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1456 if (ioc->chip == NULL) {
1457 printk(MPT2SAS_ERR_FMT "unable to map "
1458 "adapter memory!\n", ioc->name);
1459 r = -EINVAL;
1460 goto out_fail;
1461 }
Eric Moore635374e2009-03-09 01:21:12 -06001462 }
1463 }
1464 }
1465
Eric Moore635374e2009-03-09 01:21:12 -06001466 _base_mask_interrupts(ioc);
1467 r = _base_enable_msix(ioc);
1468 if (r)
1469 goto out_fail;
1470
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301471 list_for_each_entry(reply_q, &ioc->reply_queue_list, list)
1472 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1473 reply_q->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1474 "IO-APIC enabled"), reply_q->vector);
1475
Kashyap, Desai6846e752009-12-16 18:51:45 +05301476 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1477 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1478 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1479 ioc->name, (unsigned long long)pio_chip, pio_sz);
Eric Moore635374e2009-03-09 01:21:12 -06001480
Eric Moore3cb54692010-07-08 14:44:34 -06001481 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1482 pci_save_state(pdev);
1483
Eric Moore635374e2009-03-09 01:21:12 -06001484 return 0;
1485
1486 out_fail:
1487 if (ioc->chip_phys)
1488 iounmap(ioc->chip);
1489 ioc->chip_phys = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001490 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301491 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001492 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001493 return r;
1494}
1495
1496/**
Eric Moore635374e2009-03-09 01:21:12 -06001497 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1498 * @ioc: per adapter object
1499 * @smid: system request message index(smid zero is invalid)
1500 *
1501 * Returns virt pointer to message frame.
1502 */
1503void *
1504mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1505{
1506 return (void *)(ioc->request + (smid * ioc->request_sz));
1507}
1508
1509/**
1510 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1511 * @ioc: per adapter object
1512 * @smid: system request message index
1513 *
1514 * Returns virt pointer to sense buffer.
1515 */
1516void *
1517mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1518{
1519 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1520}
1521
1522/**
1523 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1524 * @ioc: per adapter object
1525 * @smid: system request message index
1526 *
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301527 * Returns phys pointer to the low 32bit address of the sense buffer.
Eric Moore635374e2009-03-09 01:21:12 -06001528 */
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301529__le32
Eric Moore635374e2009-03-09 01:21:12 -06001530mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1531{
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301532 return cpu_to_le32(ioc->sense_dma +
1533 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
Eric Moore635374e2009-03-09 01:21:12 -06001534}
1535
1536/**
1537 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1538 * @ioc: per adapter object
1539 * @phys_addr: lower 32 physical addr of the reply
1540 *
1541 * Converts 32bit lower physical addr into a virt address.
1542 */
1543void *
1544mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1545{
1546 if (!phys_addr)
1547 return NULL;
1548 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1549}
1550
1551/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301552 * mpt2sas_base_get_smid - obtain a free smid from internal queue
Eric Moore635374e2009-03-09 01:21:12 -06001553 * @ioc: per adapter object
1554 * @cb_idx: callback index
1555 *
1556 * Returns smid (zero is invalid)
1557 */
1558u16
1559mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1560{
1561 unsigned long flags;
1562 struct request_tracker *request;
1563 u16 smid;
1564
1565 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301566 if (list_empty(&ioc->internal_free_list)) {
1567 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1568 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1569 ioc->name, __func__);
1570 return 0;
1571 }
1572
1573 request = list_entry(ioc->internal_free_list.next,
1574 struct request_tracker, tracker_list);
1575 request->cb_idx = cb_idx;
1576 smid = request->smid;
1577 list_del(&request->tracker_list);
1578 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1579 return smid;
1580}
1581
1582/**
1583 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1584 * @ioc: per adapter object
1585 * @cb_idx: callback index
1586 * @scmd: pointer to scsi command object
1587 *
1588 * Returns smid (zero is invalid)
1589 */
1590u16
1591mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1592 struct scsi_cmnd *scmd)
1593{
1594 unsigned long flags;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301595 struct scsiio_tracker *request;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301596 u16 smid;
1597
1598 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001599 if (list_empty(&ioc->free_list)) {
1600 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1601 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1602 ioc->name, __func__);
1603 return 0;
1604 }
1605
1606 request = list_entry(ioc->free_list.next,
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301607 struct scsiio_tracker, tracker_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301608 request->scmd = scmd;
1609 request->cb_idx = cb_idx;
1610 smid = request->smid;
1611 list_del(&request->tracker_list);
1612 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1613 return smid;
1614}
1615
1616/**
1617 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1618 * @ioc: per adapter object
1619 * @cb_idx: callback index
1620 *
1621 * Returns smid (zero is invalid)
1622 */
1623u16
1624mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1625{
1626 unsigned long flags;
1627 struct request_tracker *request;
1628 u16 smid;
1629
1630 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1631 if (list_empty(&ioc->hpr_free_list)) {
1632 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1633 return 0;
1634 }
1635
1636 request = list_entry(ioc->hpr_free_list.next,
1637 struct request_tracker, tracker_list);
Eric Moore635374e2009-03-09 01:21:12 -06001638 request->cb_idx = cb_idx;
1639 smid = request->smid;
1640 list_del(&request->tracker_list);
1641 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1642 return smid;
1643}
1644
1645
1646/**
1647 * mpt2sas_base_free_smid - put smid back on free_list
1648 * @ioc: per adapter object
1649 * @smid: system request message index
1650 *
1651 * Return nothing.
1652 */
1653void
1654mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1655{
1656 unsigned long flags;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301657 int i;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301658 struct chain_tracker *chain_req, *next;
Eric Moore635374e2009-03-09 01:21:12 -06001659
1660 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301661 if (smid < ioc->hi_priority_smid) {
1662 /* scsiio queue */
1663 i = smid - 1;
1664 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1665 list_for_each_entry_safe(chain_req, next,
1666 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1667 list_del_init(&chain_req->tracker_list);
1668 list_add_tail(&chain_req->tracker_list,
1669 &ioc->free_chain_list);
1670 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301671 }
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301672 ioc->scsi_lookup[i].cb_idx = 0xFF;
1673 ioc->scsi_lookup[i].scmd = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301674 ioc->scsi_lookup[i].direct_io = 0;
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301675 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
1676 &ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301677 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301678
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301679 /*
1680 * See _wait_for_commands_to_complete() call with regards
1681 * to this code.
1682 */
1683 if (ioc->shost_recovery && ioc->pending_io_count) {
1684 if (ioc->pending_io_count == 1)
1685 wake_up(&ioc->reset_wq);
1686 ioc->pending_io_count--;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301687 }
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05301688 return;
1689 } else if (smid < ioc->internal_smid) {
1690 /* hi-priority */
1691 i = smid - ioc->hi_priority_smid;
1692 ioc->hpr_lookup[i].cb_idx = 0xFF;
1693 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1694 &ioc->hpr_free_list);
1695 } else if (smid <= ioc->hba_queue_depth) {
1696 /* internal queue */
1697 i = smid - ioc->internal_smid;
1698 ioc->internal_lookup[i].cb_idx = 0xFF;
1699 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1700 &ioc->internal_free_list);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301701 }
Eric Moore635374e2009-03-09 01:21:12 -06001702 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001703}
1704
1705/**
1706 * _base_writeq - 64 bit write to MMIO
1707 * @ioc: per adapter object
1708 * @b: data payload
1709 * @addr: address in MMIO space
1710 * @writeq_lock: spin lock
1711 *
1712 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1713 * care of 32 bit environment where its not quarenteed to send the entire word
1714 * in one transfer.
1715 */
1716#ifndef writeq
1717static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1718 spinlock_t *writeq_lock)
1719{
1720 unsigned long flags;
1721 __u64 data_out = cpu_to_le64(b);
1722
1723 spin_lock_irqsave(writeq_lock, flags);
1724 writel((u32)(data_out), addr);
1725 writel((u32)(data_out >> 32), (addr + 4));
1726 spin_unlock_irqrestore(writeq_lock, flags);
1727}
1728#else
1729static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1730 spinlock_t *writeq_lock)
1731{
1732 writeq(cpu_to_le64(b), addr);
1733}
1734#endif
1735
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301736static inline u8
1737_base_get_msix_index(struct MPT2SAS_ADAPTER *ioc)
1738{
1739 return ioc->cpu_msix_table[smp_processor_id()];
1740}
1741
Eric Moore635374e2009-03-09 01:21:12 -06001742/**
1743 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1744 * @ioc: per adapter object
1745 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001746 * @handle: device handle
1747 *
1748 * Return nothing.
1749 */
1750void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301751mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06001752{
1753 Mpi2RequestDescriptorUnion_t descriptor;
1754 u64 *request = (u64 *)&descriptor;
1755
1756
1757 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301758 descriptor.SCSIIO.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001759 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1760 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1761 descriptor.SCSIIO.LMID = 0;
1762 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1763 &ioc->scsi_lookup_lock);
1764}
1765
1766
1767/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001768 * mpt2sas_base_put_smid_hi_priority - send Task Management request to firmware
Eric Moore635374e2009-03-09 01:21:12 -06001769 * @ioc: per adapter object
1770 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001771 *
1772 * Return nothing.
1773 */
1774void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301775mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001776{
1777 Mpi2RequestDescriptorUnion_t descriptor;
1778 u64 *request = (u64 *)&descriptor;
1779
1780 descriptor.HighPriority.RequestFlags =
1781 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301782 descriptor.HighPriority.MSIxIndex = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001783 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1784 descriptor.HighPriority.LMID = 0;
1785 descriptor.HighPriority.Reserved1 = 0;
1786 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1787 &ioc->scsi_lookup_lock);
1788}
1789
1790/**
1791 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1792 * @ioc: per adapter object
1793 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001794 *
1795 * Return nothing.
1796 */
1797void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301798mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001799{
1800 Mpi2RequestDescriptorUnion_t descriptor;
1801 u64 *request = (u64 *)&descriptor;
1802
1803 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301804 descriptor.Default.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001805 descriptor.Default.SMID = cpu_to_le16(smid);
1806 descriptor.Default.LMID = 0;
1807 descriptor.Default.DescriptorTypeDependent = 0;
1808 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1809 &ioc->scsi_lookup_lock);
1810}
1811
1812/**
1813 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1814 * @ioc: per adapter object
1815 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001816 * @io_index: value used to track the IO
1817 *
1818 * Return nothing.
1819 */
1820void
1821mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301822 u16 io_index)
Eric Moore635374e2009-03-09 01:21:12 -06001823{
1824 Mpi2RequestDescriptorUnion_t descriptor;
1825 u64 *request = (u64 *)&descriptor;
1826
1827 descriptor.SCSITarget.RequestFlags =
1828 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05301829 descriptor.SCSITarget.MSIxIndex = _base_get_msix_index(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06001830 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1831 descriptor.SCSITarget.LMID = 0;
1832 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1833 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1834 &ioc->scsi_lookup_lock);
1835}
1836
1837/**
Eric Mooref0f9cc12009-04-21 15:40:48 -06001838 * _base_display_dell_branding - Disply branding string
1839 * @ioc: per adapter object
1840 *
1841 * Return nothing.
1842 */
1843static void
1844_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1845{
1846 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1847
1848 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1849 return;
1850
1851 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1852 switch (ioc->pdev->subsystem_device) {
1853 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1854 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1855 MPT2SAS_DELL_BRANDING_SIZE - 1);
1856 break;
1857 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1858 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1859 MPT2SAS_DELL_BRANDING_SIZE - 1);
1860 break;
1861 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1862 strncpy(dell_branding,
1863 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1864 MPT2SAS_DELL_BRANDING_SIZE - 1);
1865 break;
1866 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1867 strncpy(dell_branding,
1868 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1869 MPT2SAS_DELL_BRANDING_SIZE - 1);
1870 break;
1871 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1872 strncpy(dell_branding,
1873 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1874 MPT2SAS_DELL_BRANDING_SIZE - 1);
1875 break;
1876 case MPT2SAS_DELL_PERC_H200_SSDID:
1877 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1878 MPT2SAS_DELL_BRANDING_SIZE - 1);
1879 break;
1880 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1881 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1882 MPT2SAS_DELL_BRANDING_SIZE - 1);
1883 break;
1884 default:
1885 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1886 break;
1887 }
1888
1889 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1890 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1891 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1892 ioc->pdev->subsystem_device);
1893}
1894
1895/**
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301896 * _base_display_intel_branding - Display branding string
1897 * @ioc: per adapter object
1898 *
1899 * Return nothing.
1900 */
1901static void
1902_base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1903{
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301904 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1905 return;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301906
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301907 switch (ioc->pdev->device) {
1908 case MPI2_MFGPAGE_DEVID_SAS2008:
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301909 switch (ioc->pdev->subsystem_device) {
1910 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1911 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1912 MPT2SAS_INTEL_RMS2LL080_BRANDING);
1913 break;
1914 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1915 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1916 MPT2SAS_INTEL_RMS2LL040_BRANDING);
1917 break;
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301918 default:
1919 break;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301920 }
Kashyap, Desaiab3e5f62011-06-14 10:57:31 +05301921 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1922 switch (ioc->pdev->subsystem_device) {
1923 case MPT2SAS_INTEL_RS25GB008_SSDID:
1924 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1925 MPT2SAS_INTEL_RS25GB008_BRANDING);
1926 break;
1927 default:
1928 break;
1929 }
1930 default:
1931 break;
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301932 }
1933}
1934
1935/**
Kashyap, Desai0a2385c2011-03-15 20:04:26 +05301936 * _base_display_hp_branding - Display branding string
1937 * @ioc: per adapter object
1938 *
1939 * Return nothing.
1940 */
1941static void
1942_base_display_hp_branding(struct MPT2SAS_ADAPTER *ioc)
1943{
1944 if (ioc->pdev->subsystem_vendor != MPT2SAS_HP_3PAR_SSVID)
1945 return;
1946
1947 switch (ioc->pdev->device) {
1948 case MPI2_MFGPAGE_DEVID_SAS2004:
1949 switch (ioc->pdev->subsystem_device) {
1950 case MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_SSDID:
1951 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1952 MPT2SAS_HP_DAUGHTER_2_4_INTERNAL_BRANDING);
1953 break;
1954 default:
1955 break;
1956 }
1957 case MPI2_MFGPAGE_DEVID_SAS2308_2:
1958 switch (ioc->pdev->subsystem_device) {
1959 case MPT2SAS_HP_2_4_INTERNAL_SSDID:
1960 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1961 MPT2SAS_HP_2_4_INTERNAL_BRANDING);
1962 break;
1963 case MPT2SAS_HP_2_4_EXTERNAL_SSDID:
1964 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1965 MPT2SAS_HP_2_4_EXTERNAL_BRANDING);
1966 break;
1967 case MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_SSDID:
1968 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1969 MPT2SAS_HP_1_4_INTERNAL_1_4_EXTERNAL_BRANDING);
1970 break;
1971 case MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_SSDID:
1972 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1973 MPT2SAS_HP_EMBEDDED_2_4_INTERNAL_BRANDING);
1974 break;
1975 default:
1976 break;
1977 }
1978 default:
1979 break;
1980 }
1981}
1982
1983/**
Eric Moore635374e2009-03-09 01:21:12 -06001984 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1985 * @ioc: per adapter object
1986 *
1987 * Return nothing.
1988 */
1989static void
1990_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1991{
1992 int i = 0;
1993 char desc[16];
1994 u8 revision;
1995 u32 iounit_pg1_flags;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301996 u32 bios_version;
Eric Moore635374e2009-03-09 01:21:12 -06001997
Kashyap, Desaic97951e2011-06-14 10:54:56 +05301998 bios_version = le32_to_cpu(ioc->bios_pg3.BiosVersion);
Eric Moore635374e2009-03-09 01:21:12 -06001999 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
2000 strncpy(desc, ioc->manu_pg0.ChipName, 16);
2001 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
2002 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
2003 ioc->name, desc,
2004 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
2005 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
2006 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
2007 ioc->facts.FWVersion.Word & 0x000000FF,
2008 revision,
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302009 (bios_version & 0xFF000000) >> 24,
2010 (bios_version & 0x00FF0000) >> 16,
2011 (bios_version & 0x0000FF00) >> 8,
2012 bios_version & 0x000000FF);
Eric Moore635374e2009-03-09 01:21:12 -06002013
Kashyap, Desaied79f122009-08-20 13:23:49 +05302014 _base_display_dell_branding(ioc);
Kashyap, Desaifb396be2011-01-04 11:36:37 +05302015 _base_display_intel_branding(ioc);
Kashyap, Desai0a2385c2011-03-15 20:04:26 +05302016 _base_display_hp_branding(ioc);
Kashyap, Desaied79f122009-08-20 13:23:49 +05302017
Eric Moore635374e2009-03-09 01:21:12 -06002018 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
2019
2020 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
2021 printk("Initiator");
2022 i++;
2023 }
2024
2025 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
2026 printk("%sTarget", i ? "," : "");
2027 i++;
2028 }
2029
2030 i = 0;
2031 printk("), ");
2032 printk("Capabilities=(");
2033
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302034 if (!ioc->hide_ir_msg) {
2035 if (ioc->facts.IOCCapabilities &
2036 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
2037 printk("Raid");
2038 i++;
2039 }
Eric Moore635374e2009-03-09 01:21:12 -06002040 }
2041
2042 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
2043 printk("%sTLR", i ? "," : "");
2044 i++;
2045 }
2046
2047 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
2048 printk("%sMulticast", i ? "," : "");
2049 i++;
2050 }
2051
2052 if (ioc->facts.IOCCapabilities &
2053 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
2054 printk("%sBIDI Target", i ? "," : "");
2055 i++;
2056 }
2057
2058 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
2059 printk("%sEEDP", i ? "," : "");
2060 i++;
2061 }
2062
2063 if (ioc->facts.IOCCapabilities &
2064 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
2065 printk("%sSnapshot Buffer", i ? "," : "");
2066 i++;
2067 }
2068
2069 if (ioc->facts.IOCCapabilities &
2070 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
2071 printk("%sDiag Trace Buffer", i ? "," : "");
2072 i++;
2073 }
2074
2075 if (ioc->facts.IOCCapabilities &
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05302076 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
2077 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
2078 i++;
2079 }
2080
2081 if (ioc->facts.IOCCapabilities &
Eric Moore635374e2009-03-09 01:21:12 -06002082 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
2083 printk("%sTask Set Full", i ? "," : "");
2084 i++;
2085 }
2086
2087 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2088 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
2089 printk("%sNCQ", i ? "," : "");
2090 i++;
2091 }
2092
2093 printk(")\n");
2094}
2095
2096/**
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302097 * _base_update_missing_delay - change the missing delay timers
2098 * @ioc: per adapter object
2099 * @device_missing_delay: amount of time till device is reported missing
2100 * @io_missing_delay: interval IO is returned when there is a missing device
2101 *
2102 * Return nothing.
2103 *
2104 * Passed on the command line, this function will modify the device missing
2105 * delay, as well as the io missing delay. This should be called at driver
2106 * load time.
2107 */
2108static void
2109_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
2110 u16 device_missing_delay, u8 io_missing_delay)
2111{
2112 u16 dmd, dmd_new, dmd_orignal;
2113 u8 io_missing_delay_original;
2114 u16 sz;
2115 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
2116 Mpi2ConfigReply_t mpi_reply;
2117 u8 num_phys = 0;
2118 u16 ioc_status;
2119
2120 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
2121 if (!num_phys)
2122 return;
2123
2124 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
2125 sizeof(Mpi2SasIOUnit1PhyData_t));
2126 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
2127 if (!sas_iounit_pg1) {
2128 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2129 ioc->name, __FILE__, __LINE__, __func__);
2130 goto out;
2131 }
2132 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
2133 sas_iounit_pg1, sz))) {
2134 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2135 ioc->name, __FILE__, __LINE__, __func__);
2136 goto out;
2137 }
2138 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
2139 MPI2_IOCSTATUS_MASK;
2140 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
2141 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
2142 ioc->name, __FILE__, __LINE__, __func__);
2143 goto out;
2144 }
2145
2146 /* device missing delay */
2147 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
2148 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2149 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2150 else
2151 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2152 dmd_orignal = dmd;
2153 if (device_missing_delay > 0x7F) {
2154 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
2155 device_missing_delay;
2156 dmd = dmd / 16;
2157 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
2158 } else
2159 dmd = device_missing_delay;
2160 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
2161
2162 /* io missing delay */
2163 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
2164 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
2165
2166 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
2167 sz)) {
2168 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
2169 dmd_new = (dmd &
2170 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
2171 else
2172 dmd_new =
2173 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
2174 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
2175 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
2176 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
2177 "new(%d)\n", ioc->name, io_missing_delay_original,
2178 io_missing_delay);
2179 ioc->device_missing_delay = dmd_new;
2180 ioc->io_missing_delay = io_missing_delay;
2181 }
2182
2183out:
2184 kfree(sas_iounit_pg1);
2185}
2186
2187/**
Eric Moore635374e2009-03-09 01:21:12 -06002188 * _base_static_config_pages - static start of day config pages
2189 * @ioc: per adapter object
2190 *
2191 * Return nothing.
2192 */
2193static void
2194_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
2195{
2196 Mpi2ConfigReply_t mpi_reply;
2197 u32 iounit_pg1_flags;
2198
2199 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
Kashyap, Desaied79f122009-08-20 13:23:49 +05302200 if (ioc->ir_firmware)
2201 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
2202 &ioc->manu_pg10);
Eric Moore635374e2009-03-09 01:21:12 -06002203 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
2204 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
2205 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
2206 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
2207 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
2208 _base_display_ioc_capabilities(ioc);
2209
2210 /*
2211 * Enable task_set_full handling in iounit_pg1 when the
2212 * facts capabilities indicate that its supported.
2213 */
2214 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
2215 if ((ioc->facts.IOCCapabilities &
2216 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
2217 iounit_pg1_flags &=
2218 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2219 else
2220 iounit_pg1_flags |=
2221 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
2222 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302223 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05302224
Eric Moore635374e2009-03-09 01:21:12 -06002225}
2226
2227/**
2228 * _base_release_memory_pools - release memory
2229 * @ioc: per adapter object
2230 *
2231 * Free memory allocated from _base_allocate_memory_pools.
2232 *
2233 * Return nothing.
2234 */
2235static void
2236_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2237{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302238 int i;
2239
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302240 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002241 __func__));
2242
2243 if (ioc->request) {
2244 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2245 ioc->request, ioc->request_dma);
2246 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2247 ": free\n", ioc->name, ioc->request));
2248 ioc->request = NULL;
2249 }
2250
2251 if (ioc->sense) {
2252 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2253 if (ioc->sense_dma_pool)
2254 pci_pool_destroy(ioc->sense_dma_pool);
2255 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2256 ": free\n", ioc->name, ioc->sense));
2257 ioc->sense = NULL;
2258 }
2259
2260 if (ioc->reply) {
2261 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2262 if (ioc->reply_dma_pool)
2263 pci_pool_destroy(ioc->reply_dma_pool);
2264 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2265 ": free\n", ioc->name, ioc->reply));
2266 ioc->reply = NULL;
2267 }
2268
2269 if (ioc->reply_free) {
2270 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2271 ioc->reply_free_dma);
2272 if (ioc->reply_free_dma_pool)
2273 pci_pool_destroy(ioc->reply_free_dma_pool);
2274 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2275 "(0x%p): free\n", ioc->name, ioc->reply_free));
2276 ioc->reply_free = NULL;
2277 }
2278
2279 if (ioc->reply_post_free) {
2280 pci_pool_free(ioc->reply_post_free_dma_pool,
2281 ioc->reply_post_free, ioc->reply_post_free_dma);
2282 if (ioc->reply_post_free_dma_pool)
2283 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2284 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2285 "reply_post_free_pool(0x%p): free\n", ioc->name,
2286 ioc->reply_post_free));
2287 ioc->reply_post_free = NULL;
2288 }
2289
2290 if (ioc->config_page) {
2291 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2292 "config_page(0x%p): free\n", ioc->name,
2293 ioc->config_page));
2294 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2295 ioc->config_page, ioc->config_page_dma);
2296 }
2297
Kashyap, Desai66a67932010-04-05 14:21:07 +05302298 if (ioc->scsi_lookup) {
2299 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2300 ioc->scsi_lookup = NULL;
2301 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302302 kfree(ioc->hpr_lookup);
2303 kfree(ioc->internal_lookup);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302304 if (ioc->chain_lookup) {
2305 for (i = 0; i < ioc->chain_depth; i++) {
2306 if (ioc->chain_lookup[i].chain_buffer)
2307 pci_pool_free(ioc->chain_dma_pool,
2308 ioc->chain_lookup[i].chain_buffer,
2309 ioc->chain_lookup[i].chain_buffer_dma);
2310 }
2311 if (ioc->chain_dma_pool)
2312 pci_pool_destroy(ioc->chain_dma_pool);
2313 }
2314 if (ioc->chain_lookup) {
2315 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2316 ioc->chain_lookup = NULL;
2317 }
Eric Moore635374e2009-03-09 01:21:12 -06002318}
2319
2320
2321/**
2322 * _base_allocate_memory_pools - allocate start of day memory pools
2323 * @ioc: per adapter object
2324 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2325 *
2326 * Returns 0 success, anything else error
2327 */
2328static int
2329_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2330{
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302331 struct mpt2sas_facts *facts;
Eric Moore635374e2009-03-09 01:21:12 -06002332 u32 queue_size, queue_diff;
2333 u16 max_sge_elements;
2334 u16 num_of_reply_frames;
2335 u16 chains_needed_per_io;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302336 u32 sz, total_sz, reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002337 u32 retry_sz;
2338 u16 max_request_credit;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302339 int i;
Eric Moore635374e2009-03-09 01:21:12 -06002340
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302341 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002342 __func__));
2343
2344 retry_sz = 0;
2345 facts = &ioc->facts;
2346
2347 /* command line tunables for max sgl entries */
2348 if (max_sgl_entries != -1) {
2349 ioc->shost->sg_tablesize = (max_sgl_entries <
2350 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2351 MPT2SAS_SG_DEPTH;
2352 } else {
2353 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2354 }
2355
2356 /* command line tunables for max controller queue depth */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302357 if (max_queue_depth != -1)
Eric Moore635374e2009-03-09 01:21:12 -06002358 max_request_credit = (max_queue_depth < facts->RequestCredit)
2359 ? max_queue_depth : facts->RequestCredit;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302360 else
2361 max_request_credit = facts->RequestCredit;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302362
2363 ioc->hba_queue_depth = max_request_credit;
2364 ioc->hi_priority_depth = facts->HighPriorityCredit;
2365 ioc->internal_depth = ioc->hi_priority_depth + 5;
Eric Moore635374e2009-03-09 01:21:12 -06002366
2367 /* request frame size */
2368 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2369
2370 /* reply frame size */
2371 ioc->reply_sz = facts->ReplyFrameSize * 4;
2372
2373 retry_allocation:
2374 total_sz = 0;
2375 /* calculate number of sg elements left over in the 1st frame */
2376 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2377 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2378 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2379
2380 /* now do the same for a chain buffer */
2381 max_sge_elements = ioc->request_sz - ioc->sge_size;
2382 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2383
2384 ioc->chain_offset_value_for_main_message =
2385 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2386 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2387
2388 /*
2389 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2390 */
2391 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2392 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2393 + 1;
2394 if (chains_needed_per_io > facts->MaxChainDepth) {
2395 chains_needed_per_io = facts->MaxChainDepth;
2396 ioc->shost->sg_tablesize = min_t(u16,
2397 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2398 * chains_needed_per_io), ioc->shost->sg_tablesize);
2399 }
2400 ioc->chains_needed_per_io = chains_needed_per_io;
2401
2402 /* reply free queue sizing - taking into account for events */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302403 num_of_reply_frames = ioc->hba_queue_depth + 32;
Eric Moore635374e2009-03-09 01:21:12 -06002404
2405 /* number of replies frames can't be a multiple of 16 */
2406 /* decrease number of reply frames by 1 */
2407 if (!(num_of_reply_frames % 16))
2408 num_of_reply_frames--;
2409
2410 /* calculate number of reply free queue entries
2411 * (must be multiple of 16)
2412 */
2413
2414 /* (we know reply_free_queue_depth is not a multiple of 16) */
2415 queue_size = num_of_reply_frames;
2416 queue_size += 16 - (queue_size % 16);
2417 ioc->reply_free_queue_depth = queue_size;
2418
2419 /* reply descriptor post queue sizing */
2420 /* this size should be the number of request frames + number of reply
2421 * frames
2422 */
2423
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302424 queue_size = ioc->hba_queue_depth + num_of_reply_frames + 1;
Eric Moore635374e2009-03-09 01:21:12 -06002425 /* round up to 16 byte boundary */
2426 if (queue_size % 16)
2427 queue_size += 16 - (queue_size % 16);
2428
2429 /* check against IOC maximum reply post queue depth */
2430 if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
2431 queue_diff = queue_size -
2432 facts->MaxReplyDescriptorPostQueueDepth;
2433
2434 /* round queue_diff up to multiple of 16 */
2435 if (queue_diff % 16)
2436 queue_diff += 16 - (queue_diff % 16);
2437
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302438 /* adjust hba_queue_depth, reply_free_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002439 * and queue_size
2440 */
Kashyap, Desai11e1b962011-01-04 11:34:57 +05302441 ioc->hba_queue_depth -= (queue_diff / 2);
2442 ioc->reply_free_queue_depth -= (queue_diff / 2);
2443 queue_size = facts->MaxReplyDescriptorPostQueueDepth;
Eric Moore635374e2009-03-09 01:21:12 -06002444 }
2445 ioc->reply_post_queue_depth = queue_size;
2446
Eric Moore635374e2009-03-09 01:21:12 -06002447 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2448 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2449 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2450 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2451 ioc->chains_needed_per_io));
2452
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302453 ioc->scsiio_depth = ioc->hba_queue_depth -
2454 ioc->hi_priority_depth - ioc->internal_depth;
2455
2456 /* set the scsi host can_queue depth
2457 * with some internal commands that could be outstanding
2458 */
2459 ioc->shost->can_queue = ioc->scsiio_depth - (2);
2460 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2461 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2462
Eric Moore635374e2009-03-09 01:21:12 -06002463 /* contiguous pool for request and chains, 16 byte align, one extra "
2464 * "frame for smid=0
2465 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302466 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302467 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302468
2469 /* hi-priority queue */
2470 sz += (ioc->hi_priority_depth * ioc->request_sz);
2471
2472 /* internal queue */
2473 sz += (ioc->internal_depth * ioc->request_sz);
Eric Moore635374e2009-03-09 01:21:12 -06002474
2475 ioc->request_dma_sz = sz;
2476 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2477 if (!ioc->request) {
2478 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302479 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2480 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002481 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302482 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
Eric Moore635374e2009-03-09 01:21:12 -06002483 goto out;
2484 retry_sz += 64;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302485 ioc->hba_queue_depth = max_request_credit - retry_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002486 goto retry_allocation;
2487 }
2488
2489 if (retry_sz)
2490 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302491 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2492 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002493 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2494
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302495
2496 /* hi-priority queue */
2497 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002498 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302499 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002500 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302501
2502 /* internal queue */
2503 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2504 ioc->request_sz);
2505 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2506 ioc->request_sz);
2507
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302508
Eric Moore635374e2009-03-09 01:21:12 -06002509 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2510 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302511 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2512 (ioc->hba_queue_depth * ioc->request_sz)/1024));
Eric Moore635374e2009-03-09 01:21:12 -06002513 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2514 ioc->name, (unsigned long long) ioc->request_dma));
2515 total_sz += sz;
2516
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05302517 sz = ioc->scsiio_depth * sizeof(struct scsiio_tracker);
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302518 ioc->scsi_lookup_pages = get_order(sz);
Kashyap, Desaid5bd3491c2011-01-04 11:39:20 +05302519 ioc->scsi_lookup = (struct scsiio_tracker *)__get_free_pages(
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302520 GFP_KERNEL, ioc->scsi_lookup_pages);
Eric Moore635374e2009-03-09 01:21:12 -06002521 if (!ioc->scsi_lookup) {
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302522 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2523 "sz(%d)\n", ioc->name, (int)sz);
Eric Moore635374e2009-03-09 01:21:12 -06002524 goto out;
2525 }
2526
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302527 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2528 "depth(%d)\n", ioc->name, ioc->request,
2529 ioc->scsiio_depth));
2530
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302531 /* loop till the allocation succeeds */
2532 do {
2533 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2534 ioc->chain_pages = get_order(sz);
2535 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2536 GFP_KERNEL, ioc->chain_pages);
2537 if (ioc->chain_lookup == NULL)
2538 ioc->chain_depth -= 100;
2539 } while (ioc->chain_lookup == NULL);
2540 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2541 ioc->request_sz, 16, 0);
2542 if (!ioc->chain_dma_pool) {
2543 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2544 "failed\n", ioc->name);
2545 goto out;
2546 }
2547 for (i = 0; i < ioc->chain_depth; i++) {
2548 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2549 ioc->chain_dma_pool , GFP_KERNEL,
2550 &ioc->chain_lookup[i].chain_buffer_dma);
2551 if (!ioc->chain_lookup[i].chain_buffer) {
2552 ioc->chain_depth = i;
2553 goto chain_done;
2554 }
2555 total_sz += ioc->request_sz;
2556 }
2557chain_done:
2558 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2559 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2560 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2561 ioc->request_sz))/1024));
2562
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302563 /* initialize hi-priority queue smid's */
2564 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2565 sizeof(struct request_tracker), GFP_KERNEL);
2566 if (!ioc->hpr_lookup) {
2567 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2568 ioc->name);
2569 goto out;
2570 }
2571 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2572 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2573 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2574 ioc->hi_priority_depth, ioc->hi_priority_smid));
2575
2576 /* initialize internal queue smid's */
2577 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2578 sizeof(struct request_tracker), GFP_KERNEL);
2579 if (!ioc->internal_lookup) {
2580 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2581 ioc->name);
2582 goto out;
2583 }
2584 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2585 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2586 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2587 ioc->internal_depth, ioc->internal_smid));
Eric Moore635374e2009-03-09 01:21:12 -06002588
2589 /* sense buffers, 4 byte align */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302590 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Eric Moore635374e2009-03-09 01:21:12 -06002591 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2592 0);
2593 if (!ioc->sense_dma_pool) {
2594 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2595 ioc->name);
2596 goto out;
2597 }
2598 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2599 &ioc->sense_dma);
2600 if (!ioc->sense) {
2601 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2602 ioc->name);
2603 goto out;
2604 }
2605 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2606 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302607 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002608 SCSI_SENSE_BUFFERSIZE, sz/1024));
2609 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2610 ioc->name, (unsigned long long)ioc->sense_dma));
2611 total_sz += sz;
2612
2613 /* reply pool, 4 byte align */
2614 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2615 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2616 0);
2617 if (!ioc->reply_dma_pool) {
2618 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2619 ioc->name);
2620 goto out;
2621 }
2622 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2623 &ioc->reply_dma);
2624 if (!ioc->reply) {
2625 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2626 ioc->name);
2627 goto out;
2628 }
Kashyap, Desaidd3741d2010-11-13 04:31:14 +05302629 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2630 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
Eric Moore635374e2009-03-09 01:21:12 -06002631 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2632 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2633 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2634 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2635 ioc->name, (unsigned long long)ioc->reply_dma));
2636 total_sz += sz;
2637
2638 /* reply free queue, 16 byte align */
2639 sz = ioc->reply_free_queue_depth * 4;
2640 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2641 ioc->pdev, sz, 16, 0);
2642 if (!ioc->reply_free_dma_pool) {
2643 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2644 "failed\n", ioc->name);
2645 goto out;
2646 }
2647 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2648 &ioc->reply_free_dma);
2649 if (!ioc->reply_free) {
2650 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2651 "failed\n", ioc->name);
2652 goto out;
2653 }
2654 memset(ioc->reply_free, 0, sz);
2655 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2656 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2657 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2658 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2659 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2660 total_sz += sz;
2661
2662 /* reply post queue, 16 byte align */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302663 reply_post_free_sz = ioc->reply_post_queue_depth *
2664 sizeof(Mpi2DefaultReplyDescriptor_t);
2665 if (_base_is_controller_msix_enabled(ioc))
2666 sz = reply_post_free_sz * ioc->reply_queue_count;
2667 else
2668 sz = reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002669 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2670 ioc->pdev, sz, 16, 0);
2671 if (!ioc->reply_post_free_dma_pool) {
2672 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2673 "failed\n", ioc->name);
2674 goto out;
2675 }
2676 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2677 GFP_KERNEL, &ioc->reply_post_free_dma);
2678 if (!ioc->reply_post_free) {
2679 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2680 "failed\n", ioc->name);
2681 goto out;
2682 }
2683 memset(ioc->reply_post_free, 0, sz);
2684 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2685 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2686 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2687 sz/1024));
2688 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2689 "(0x%llx)\n", ioc->name, (unsigned long long)
2690 ioc->reply_post_free_dma));
2691 total_sz += sz;
2692
2693 ioc->config_page_sz = 512;
2694 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2695 ioc->config_page_sz, &ioc->config_page_dma);
2696 if (!ioc->config_page) {
2697 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2698 "failed\n", ioc->name);
2699 goto out;
2700 }
2701 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2702 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2703 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2704 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2705 total_sz += ioc->config_page_sz;
2706
2707 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2708 ioc->name, total_sz/1024);
2709 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2710 "Max Controller Queue Depth(%d)\n",
2711 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2712 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2713 ioc->name, ioc->shost->sg_tablesize);
2714 return 0;
2715
2716 out:
Eric Moore635374e2009-03-09 01:21:12 -06002717 return -ENOMEM;
2718}
2719
2720
2721/**
2722 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2723 * @ioc: Pointer to MPT_ADAPTER structure
2724 * @cooked: Request raw or cooked IOC state
2725 *
2726 * Returns all IOC Doorbell register bits if cooked==0, else just the
2727 * Doorbell bits in MPI_IOC_STATE_MASK.
2728 */
2729u32
2730mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2731{
2732 u32 s, sc;
2733
2734 s = readl(&ioc->chip->Doorbell);
2735 sc = s & MPI2_IOC_STATE_MASK;
2736 return cooked ? sc : s;
2737}
2738
2739/**
2740 * _base_wait_on_iocstate - waiting on a particular ioc state
2741 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2742 * @timeout: timeout in second
2743 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2744 *
2745 * Returns 0 for success, non-zero for failure.
2746 */
2747static int
2748_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2749 int sleep_flag)
2750{
2751 u32 count, cntdn;
2752 u32 current_state;
2753
2754 count = 0;
2755 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2756 do {
2757 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2758 if (current_state == ioc_state)
2759 return 0;
2760 if (count && current_state == MPI2_IOC_STATE_FAULT)
2761 break;
2762 if (sleep_flag == CAN_SLEEP)
2763 msleep(1);
2764 else
2765 udelay(500);
2766 count++;
2767 } while (--cntdn);
2768
2769 return current_state;
2770}
2771
2772/**
2773 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2774 * a write to the doorbell)
2775 * @ioc: per adapter object
2776 * @timeout: timeout in second
2777 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2778 *
2779 * Returns 0 for success, non-zero for failure.
2780 *
2781 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2782 */
2783static int
2784_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2785 int sleep_flag)
2786{
2787 u32 cntdn, count;
2788 u32 int_status;
2789
2790 count = 0;
2791 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2792 do {
2793 int_status = readl(&ioc->chip->HostInterruptStatus);
2794 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302795 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002796 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002797 __func__, count, timeout));
2798 return 0;
2799 }
2800 if (sleep_flag == CAN_SLEEP)
2801 msleep(1);
2802 else
2803 udelay(500);
2804 count++;
2805 } while (--cntdn);
2806
2807 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2808 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2809 return -EFAULT;
2810}
2811
2812/**
2813 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2814 * @ioc: per adapter object
2815 * @timeout: timeout in second
2816 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2817 *
2818 * Returns 0 for success, non-zero for failure.
2819 *
2820 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2821 * doorbell.
2822 */
2823static int
2824_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2825 int sleep_flag)
2826{
2827 u32 cntdn, count;
2828 u32 int_status;
2829 u32 doorbell;
2830
2831 count = 0;
2832 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2833 do {
2834 int_status = readl(&ioc->chip->HostInterruptStatus);
2835 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302836 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002837 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002838 __func__, count, timeout));
2839 return 0;
2840 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2841 doorbell = readl(&ioc->chip->Doorbell);
2842 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2843 MPI2_IOC_STATE_FAULT) {
2844 mpt2sas_base_fault_info(ioc , doorbell);
2845 return -EFAULT;
2846 }
2847 } else if (int_status == 0xFFFFFFFF)
2848 goto out;
2849
2850 if (sleep_flag == CAN_SLEEP)
2851 msleep(1);
2852 else
2853 udelay(500);
2854 count++;
2855 } while (--cntdn);
2856
2857 out:
2858 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2859 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2860 return -EFAULT;
2861}
2862
2863/**
2864 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2865 * @ioc: per adapter object
2866 * @timeout: timeout in second
2867 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2868 *
2869 * Returns 0 for success, non-zero for failure.
2870 *
2871 */
2872static int
2873_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2874 int sleep_flag)
2875{
2876 u32 cntdn, count;
2877 u32 doorbell_reg;
2878
2879 count = 0;
2880 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2881 do {
2882 doorbell_reg = readl(&ioc->chip->Doorbell);
2883 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302884 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002885 "successful count(%d), timeout(%d)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002886 __func__, count, timeout));
2887 return 0;
2888 }
2889 if (sleep_flag == CAN_SLEEP)
2890 msleep(1);
2891 else
2892 udelay(500);
2893 count++;
2894 } while (--cntdn);
2895
2896 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2897 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2898 return -EFAULT;
2899}
2900
2901/**
2902 * _base_send_ioc_reset - send doorbell reset
2903 * @ioc: per adapter object
2904 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2905 * @timeout: timeout in second
2906 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2907 *
2908 * Returns 0 for success, non-zero for failure.
2909 */
2910static int
2911_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2912 int sleep_flag)
2913{
2914 u32 ioc_state;
2915 int r = 0;
2916
2917 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2918 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2919 ioc->name, __func__);
2920 return -EFAULT;
2921 }
2922
2923 if (!(ioc->facts.IOCCapabilities &
2924 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2925 return -EFAULT;
2926
2927 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2928
2929 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2930 &ioc->chip->Doorbell);
2931 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2932 r = -EFAULT;
2933 goto out;
2934 }
2935 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2936 timeout, sleep_flag);
2937 if (ioc_state) {
2938 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2939 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2940 r = -EFAULT;
2941 goto out;
2942 }
2943 out:
2944 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2945 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2946 return r;
2947}
2948
2949/**
2950 * _base_handshake_req_reply_wait - send request thru doorbell interface
2951 * @ioc: per adapter object
2952 * @request_bytes: request length
2953 * @request: pointer having request payload
2954 * @reply_bytes: reply length
2955 * @reply: pointer to reply payload
2956 * @timeout: timeout in second
2957 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2958 *
2959 * Returns 0 for success, non-zero for failure.
2960 */
2961static int
2962_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2963 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2964{
2965 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2966 int i;
2967 u8 failed;
2968 u16 dummy;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302969 __le32 *mfp;
Eric Moore635374e2009-03-09 01:21:12 -06002970
2971 /* make sure doorbell is not in use */
2972 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2973 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2974 " (line=%d)\n", ioc->name, __LINE__);
2975 return -EFAULT;
2976 }
2977
2978 /* clear pending doorbell interrupts from previous state changes */
2979 if (readl(&ioc->chip->HostInterruptStatus) &
2980 MPI2_HIS_IOC2SYS_DB_STATUS)
2981 writel(0, &ioc->chip->HostInterruptStatus);
2982
2983 /* send message to ioc */
2984 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2985 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2986 &ioc->chip->Doorbell);
2987
Kashyap, Desai29786e12009-09-14 11:06:21 +05302988 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
Eric Moore635374e2009-03-09 01:21:12 -06002989 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2990 "int failed (line=%d)\n", ioc->name, __LINE__);
2991 return -EFAULT;
2992 }
2993 writel(0, &ioc->chip->HostInterruptStatus);
2994
2995 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2996 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2997 "ack failed (line=%d)\n", ioc->name, __LINE__);
2998 return -EFAULT;
2999 }
3000
3001 /* send message 32-bits at a time */
3002 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
3003 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
3004 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
3005 failed = 1;
3006 }
3007
3008 if (failed) {
3009 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3010 "sending request failed (line=%d)\n", ioc->name, __LINE__);
3011 return -EFAULT;
3012 }
3013
3014 /* now wait for the reply */
3015 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
3016 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3017 "int failed (line=%d)\n", ioc->name, __LINE__);
3018 return -EFAULT;
3019 }
3020
3021 /* read the first two 16-bits, it gives the total length of the reply */
3022 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3023 & MPI2_DOORBELL_DATA_MASK);
3024 writel(0, &ioc->chip->HostInterruptStatus);
3025 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3026 printk(MPT2SAS_ERR_FMT "doorbell handshake "
3027 "int failed (line=%d)\n", ioc->name, __LINE__);
3028 return -EFAULT;
3029 }
3030 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3031 & MPI2_DOORBELL_DATA_MASK);
3032 writel(0, &ioc->chip->HostInterruptStatus);
3033
3034 for (i = 2; i < default_reply->MsgLength * 2; i++) {
3035 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
3036 printk(MPT2SAS_ERR_FMT "doorbell "
3037 "handshake int failed (line=%d)\n", ioc->name,
3038 __LINE__);
3039 return -EFAULT;
3040 }
3041 if (i >= reply_bytes/2) /* overflow case */
3042 dummy = readl(&ioc->chip->Doorbell);
3043 else
3044 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
3045 & MPI2_DOORBELL_DATA_MASK);
3046 writel(0, &ioc->chip->HostInterruptStatus);
3047 }
3048
3049 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
3050 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
3051 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
3052 " (line=%d)\n", ioc->name, __LINE__));
3053 }
3054 writel(0, &ioc->chip->HostInterruptStatus);
3055
3056 if (ioc->logging_level & MPT_DEBUG_INIT) {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303057 mfp = (__le32 *)reply;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303058 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003059 for (i = 0; i < reply_bytes/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303060 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003061 le32_to_cpu(mfp[i]));
3062 }
3063 return 0;
3064}
3065
3066/**
3067 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
3068 * @ioc: per adapter object
3069 * @mpi_reply: the reply payload from FW
3070 * @mpi_request: the request payload sent to FW
3071 *
3072 * The SAS IO Unit Control Request message allows the host to perform low-level
3073 * operations, such as resets on the PHYs of the IO Unit, also allows the host
3074 * to obtain the IOC assigned device handles for a device if it has other
3075 * identifying information about the device, in addition allows the host to
3076 * remove IOC resources associated with the device.
3077 *
3078 * Returns 0 for success, non-zero for failure.
3079 */
3080int
3081mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
3082 Mpi2SasIoUnitControlReply_t *mpi_reply,
3083 Mpi2SasIoUnitControlRequest_t *mpi_request)
3084{
3085 u16 smid;
3086 u32 ioc_state;
3087 unsigned long timeleft;
3088 u8 issue_reset;
3089 int rc;
3090 void *request;
3091 u16 wait_state_count;
3092
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303093 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003094 __func__));
3095
3096 mutex_lock(&ioc->base_cmds.mutex);
3097
3098 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3099 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3100 ioc->name, __func__);
3101 rc = -EAGAIN;
3102 goto out;
3103 }
3104
3105 wait_state_count = 0;
3106 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3107 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3108 if (wait_state_count++ == 10) {
3109 printk(MPT2SAS_ERR_FMT
3110 "%s: failed due to ioc not operational\n",
3111 ioc->name, __func__);
3112 rc = -EFAULT;
3113 goto out;
3114 }
3115 ssleep(1);
3116 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3117 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3118 "operational state(count=%d)\n", ioc->name,
3119 __func__, wait_state_count);
3120 }
3121
3122 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3123 if (!smid) {
3124 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3125 ioc->name, __func__);
3126 rc = -EAGAIN;
3127 goto out;
3128 }
3129
3130 rc = 0;
3131 ioc->base_cmds.status = MPT2_CMD_PENDING;
3132 request = mpt2sas_base_get_msg_frame(ioc, smid);
3133 ioc->base_cmds.smid = smid;
3134 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
3135 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3136 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
3137 ioc->ioc_link_reset_in_progress = 1;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303138 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303139 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003140 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3141 msecs_to_jiffies(10000));
3142 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
3143 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
3144 ioc->ioc_link_reset_in_progress)
3145 ioc->ioc_link_reset_in_progress = 0;
3146 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3147 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3148 ioc->name, __func__);
3149 _debug_dump_mf(mpi_request,
3150 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
3151 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3152 issue_reset = 1;
3153 goto issue_host_reset;
3154 }
3155 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3156 memcpy(mpi_reply, ioc->base_cmds.reply,
3157 sizeof(Mpi2SasIoUnitControlReply_t));
3158 else
3159 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
3160 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3161 goto out;
3162
3163 issue_host_reset:
3164 if (issue_reset)
3165 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3166 FORCE_BIG_HAMMER);
3167 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3168 rc = -EFAULT;
3169 out:
3170 mutex_unlock(&ioc->base_cmds.mutex);
3171 return rc;
3172}
3173
3174
3175/**
3176 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
3177 * @ioc: per adapter object
3178 * @mpi_reply: the reply payload from FW
3179 * @mpi_request: the request payload sent to FW
3180 *
3181 * The SCSI Enclosure Processor request message causes the IOC to
3182 * communicate with SES devices to control LED status signals.
3183 *
3184 * Returns 0 for success, non-zero for failure.
3185 */
3186int
3187mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
3188 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
3189{
3190 u16 smid;
3191 u32 ioc_state;
3192 unsigned long timeleft;
3193 u8 issue_reset;
3194 int rc;
3195 void *request;
3196 u16 wait_state_count;
3197
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303198 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003199 __func__));
3200
3201 mutex_lock(&ioc->base_cmds.mutex);
3202
3203 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
3204 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
3205 ioc->name, __func__);
3206 rc = -EAGAIN;
3207 goto out;
3208 }
3209
3210 wait_state_count = 0;
3211 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3212 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3213 if (wait_state_count++ == 10) {
3214 printk(MPT2SAS_ERR_FMT
3215 "%s: failed due to ioc not operational\n",
3216 ioc->name, __func__);
3217 rc = -EFAULT;
3218 goto out;
3219 }
3220 ssleep(1);
3221 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3222 printk(MPT2SAS_INFO_FMT "%s: waiting for "
3223 "operational state(count=%d)\n", ioc->name,
3224 __func__, wait_state_count);
3225 }
3226
3227 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3228 if (!smid) {
3229 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3230 ioc->name, __func__);
3231 rc = -EAGAIN;
3232 goto out;
3233 }
3234
3235 rc = 0;
3236 ioc->base_cmds.status = MPT2_CMD_PENDING;
3237 request = mpt2sas_base_get_msg_frame(ioc, smid);
3238 ioc->base_cmds.smid = smid;
3239 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303240 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303241 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003242 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3243 msecs_to_jiffies(10000));
3244 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3245 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3246 ioc->name, __func__);
3247 _debug_dump_mf(mpi_request,
3248 sizeof(Mpi2SepRequest_t)/4);
3249 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3250 issue_reset = 1;
3251 goto issue_host_reset;
3252 }
3253 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3254 memcpy(mpi_reply, ioc->base_cmds.reply,
3255 sizeof(Mpi2SepReply_t));
3256 else
3257 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3258 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3259 goto out;
3260
3261 issue_host_reset:
3262 if (issue_reset)
3263 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3264 FORCE_BIG_HAMMER);
3265 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3266 rc = -EFAULT;
3267 out:
3268 mutex_unlock(&ioc->base_cmds.mutex);
3269 return rc;
3270}
3271
3272/**
3273 * _base_get_port_facts - obtain port facts reply and save in ioc
3274 * @ioc: per adapter object
3275 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3276 *
3277 * Returns 0 for success, non-zero for failure.
3278 */
3279static int
3280_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3281{
3282 Mpi2PortFactsRequest_t mpi_request;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303283 Mpi2PortFactsReply_t mpi_reply;
3284 struct mpt2sas_port_facts *pfacts;
Eric Moore635374e2009-03-09 01:21:12 -06003285 int mpi_reply_sz, mpi_request_sz, r;
3286
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303287 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003288 __func__));
3289
3290 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3291 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3292 memset(&mpi_request, 0, mpi_request_sz);
3293 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3294 mpi_request.PortNumber = port;
3295 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3296 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3297
3298 if (r != 0) {
3299 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3300 ioc->name, __func__, r);
3301 return r;
3302 }
3303
3304 pfacts = &ioc->pfacts[port];
3305 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
3306 pfacts->PortNumber = mpi_reply.PortNumber;
3307 pfacts->VP_ID = mpi_reply.VP_ID;
3308 pfacts->VF_ID = mpi_reply.VF_ID;
3309 pfacts->MaxPostedCmdBuffers =
3310 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3311
3312 return 0;
3313}
3314
3315/**
3316 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3317 * @ioc: per adapter object
3318 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3319 *
3320 * Returns 0 for success, non-zero for failure.
3321 */
3322static int
3323_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3324{
3325 Mpi2IOCFactsRequest_t mpi_request;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303326 Mpi2IOCFactsReply_t mpi_reply;
3327 struct mpt2sas_facts *facts;
Eric Moore635374e2009-03-09 01:21:12 -06003328 int mpi_reply_sz, mpi_request_sz, r;
3329
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303330 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003331 __func__));
3332
3333 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3334 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3335 memset(&mpi_request, 0, mpi_request_sz);
3336 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3337 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3338 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3339
3340 if (r != 0) {
3341 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3342 ioc->name, __func__, r);
3343 return r;
3344 }
3345
3346 facts = &ioc->facts;
3347 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
3348 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3349 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3350 facts->VP_ID = mpi_reply.VP_ID;
3351 facts->VF_ID = mpi_reply.VF_ID;
3352 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3353 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3354 facts->WhoInit = mpi_reply.WhoInit;
3355 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303356 facts->MaxMSIxVectors = mpi_reply.MaxMSIxVectors;
Eric Moore635374e2009-03-09 01:21:12 -06003357 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3358 facts->MaxReplyDescriptorPostQueueDepth =
3359 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3360 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3361 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3362 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3363 ioc->ir_firmware = 1;
3364 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3365 facts->IOCRequestFrameSize =
3366 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3367 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3368 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3369 ioc->shost->max_id = -1;
3370 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3371 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3372 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3373 facts->HighPriorityCredit =
3374 le16_to_cpu(mpi_reply.HighPriorityCredit);
3375 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3376 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3377
3378 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3379 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3380 facts->MaxChainDepth));
3381 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3382 "reply frame size(%d)\n", ioc->name,
3383 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3384 return 0;
3385}
3386
3387/**
3388 * _base_send_ioc_init - send ioc_init to firmware
3389 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003390 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3391 *
3392 * Returns 0 for success, non-zero for failure.
3393 */
3394static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303395_base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003396{
3397 Mpi2IOCInitRequest_t mpi_request;
3398 Mpi2IOCInitReply_t mpi_reply;
3399 int r;
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303400 struct timeval current_time;
Kashyap, Desai463217b2009-10-05 15:53:06 +05303401 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003402
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303403 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003404 __func__));
3405
3406 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3407 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3408 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303409 mpi_request.VF_ID = 0; /* TODO */
3410 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003411 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3412 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3413
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303414 if (_base_is_controller_msix_enabled(ioc))
3415 mpi_request.HostMSIxVectors = ioc->reply_queue_count;
Eric Moore635374e2009-03-09 01:21:12 -06003416 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3417 mpi_request.ReplyDescriptorPostQueueDepth =
3418 cpu_to_le16(ioc->reply_post_queue_depth);
3419 mpi_request.ReplyFreeQueueDepth =
3420 cpu_to_le16(ioc->reply_free_queue_depth);
3421
Eric Moore635374e2009-03-09 01:21:12 -06003422 mpi_request.SenseBufferAddressHigh =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303423 cpu_to_le32((u64)ioc->sense_dma >> 32);
Eric Moore635374e2009-03-09 01:21:12 -06003424 mpi_request.SystemReplyAddressHigh =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303425 cpu_to_le32((u64)ioc->reply_dma >> 32);
Eric Moore635374e2009-03-09 01:21:12 -06003426 mpi_request.SystemRequestFrameBaseAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303427 cpu_to_le64((u64)ioc->request_dma);
Eric Moore635374e2009-03-09 01:21:12 -06003428 mpi_request.ReplyFreeQueueAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303429 cpu_to_le64((u64)ioc->reply_free_dma);
Eric Moore635374e2009-03-09 01:21:12 -06003430 mpi_request.ReplyDescriptorPostQueueAddress =
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303431 cpu_to_le64((u64)ioc->reply_post_free_dma);
3432
Eric Moore635374e2009-03-09 01:21:12 -06003433
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303434 /* This time stamp specifies number of milliseconds
3435 * since epoch ~ midnight January 1, 1970.
3436 */
3437 do_gettimeofday(&current_time);
Kashyap, Desai7921b35c2010-03-17 16:21:33 +05303438 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3439 (current_time.tv_usec / 1000));
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303440
Eric Moore635374e2009-03-09 01:21:12 -06003441 if (ioc->logging_level & MPT_DEBUG_INIT) {
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303442 __le32 *mfp;
Eric Moore635374e2009-03-09 01:21:12 -06003443 int i;
3444
Kashyap, Desaic97951e2011-06-14 10:54:56 +05303445 mfp = (__le32 *)&mpi_request;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303446 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003447 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303448 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003449 le32_to_cpu(mfp[i]));
3450 }
3451
3452 r = _base_handshake_req_reply_wait(ioc,
3453 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3454 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3455 sleep_flag);
3456
3457 if (r != 0) {
3458 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3459 ioc->name, __func__, r);
3460 return r;
3461 }
3462
Kashyap, Desai463217b2009-10-05 15:53:06 +05303463 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3464 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
Eric Moore635374e2009-03-09 01:21:12 -06003465 mpi_reply.IOCLogInfo) {
3466 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3467 r = -EIO;
3468 }
3469
3470 return 0;
3471}
3472
3473/**
3474 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3475 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003476 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3477 *
3478 * Returns 0 for success, non-zero for failure.
3479 */
3480static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303481_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003482{
3483 Mpi2PortEnableRequest_t *mpi_request;
3484 u32 ioc_state;
3485 unsigned long timeleft;
3486 int r = 0;
3487 u16 smid;
3488
3489 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3490
3491 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3492 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3493 ioc->name, __func__);
3494 return -EAGAIN;
3495 }
3496
3497 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3498 if (!smid) {
3499 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3500 ioc->name, __func__);
3501 return -EAGAIN;
3502 }
3503
3504 ioc->base_cmds.status = MPT2_CMD_PENDING;
3505 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3506 ioc->base_cmds.smid = smid;
3507 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3508 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303509 mpi_request->VF_ID = 0; /* TODO */
3510 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003511
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303512 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303513 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003514 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3515 300*HZ);
3516 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3517 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3518 ioc->name, __func__);
3519 _debug_dump_mf(mpi_request,
3520 sizeof(Mpi2PortEnableRequest_t)/4);
3521 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3522 r = -EFAULT;
3523 else
3524 r = -ETIME;
3525 goto out;
3526 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303527 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003528 ioc->name, __func__));
3529
3530 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
3531 60, sleep_flag);
3532 if (ioc_state) {
3533 printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
3534 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3535 r = -EFAULT;
3536 }
3537 out:
3538 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3539 printk(MPT2SAS_INFO_FMT "port enable: %s\n",
3540 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3541 return r;
3542}
3543
3544/**
3545 * _base_unmask_events - turn on notification for this event
3546 * @ioc: per adapter object
3547 * @event: firmware event
3548 *
3549 * The mask is stored in ioc->event_masks.
3550 */
3551static void
3552_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3553{
3554 u32 desired_event;
3555
3556 if (event >= 128)
3557 return;
3558
3559 desired_event = (1 << (event % 32));
3560
3561 if (event < 32)
3562 ioc->event_masks[0] &= ~desired_event;
3563 else if (event < 64)
3564 ioc->event_masks[1] &= ~desired_event;
3565 else if (event < 96)
3566 ioc->event_masks[2] &= ~desired_event;
3567 else if (event < 128)
3568 ioc->event_masks[3] &= ~desired_event;
3569}
3570
3571/**
3572 * _base_event_notification - send event notification
3573 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003574 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3575 *
3576 * Returns 0 for success, non-zero for failure.
3577 */
3578static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303579_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003580{
3581 Mpi2EventNotificationRequest_t *mpi_request;
3582 unsigned long timeleft;
3583 u16 smid;
3584 int r = 0;
3585 int i;
3586
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303587 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003588 __func__));
3589
3590 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3591 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3592 ioc->name, __func__);
3593 return -EAGAIN;
3594 }
3595
3596 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3597 if (!smid) {
3598 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3599 ioc->name, __func__);
3600 return -EAGAIN;
3601 }
3602 ioc->base_cmds.status = MPT2_CMD_PENDING;
3603 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3604 ioc->base_cmds.smid = smid;
3605 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3606 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303607 mpi_request->VF_ID = 0; /* TODO */
3608 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003609 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3610 mpi_request->EventMasks[i] =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303611 cpu_to_le32(ioc->event_masks[i]);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303612 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303613 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003614 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3615 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3616 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3617 ioc->name, __func__);
3618 _debug_dump_mf(mpi_request,
3619 sizeof(Mpi2EventNotificationRequest_t)/4);
3620 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3621 r = -EFAULT;
3622 else
3623 r = -ETIME;
3624 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303625 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003626 ioc->name, __func__));
3627 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3628 return r;
3629}
3630
3631/**
3632 * mpt2sas_base_validate_event_type - validating event types
3633 * @ioc: per adapter object
3634 * @event: firmware event
3635 *
3636 * This will turn on firmware event notification when application
3637 * ask for that event. We don't mask events that are already enabled.
3638 */
3639void
3640mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3641{
3642 int i, j;
3643 u32 event_mask, desired_event;
3644 u8 send_update_to_fw;
3645
3646 for (i = 0, send_update_to_fw = 0; i <
3647 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3648 event_mask = ~event_type[i];
3649 desired_event = 1;
3650 for (j = 0; j < 32; j++) {
3651 if (!(event_mask & desired_event) &&
3652 (ioc->event_masks[i] & desired_event)) {
3653 ioc->event_masks[i] &= ~desired_event;
3654 send_update_to_fw = 1;
3655 }
3656 desired_event = (desired_event << 1);
3657 }
3658 }
3659
3660 if (!send_update_to_fw)
3661 return;
3662
3663 mutex_lock(&ioc->base_cmds.mutex);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303664 _base_event_notification(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003665 mutex_unlock(&ioc->base_cmds.mutex);
3666}
3667
3668/**
3669 * _base_diag_reset - the "big hammer" start of day reset
3670 * @ioc: per adapter object
3671 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3672 *
3673 * Returns 0 for success, non-zero for failure.
3674 */
3675static int
3676_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3677{
3678 u32 host_diagnostic;
3679 u32 ioc_state;
3680 u32 count;
3681 u32 hcb_size;
3682
3683 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303684 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
Eric Moore635374e2009-03-09 01:21:12 -06003685 ioc->name));
Eric Moore635374e2009-03-09 01:21:12 -06003686
3687 count = 0;
3688 do {
3689 /* Write magic sequence to WriteSequence register
3690 * Loop until in diagnostic mode
3691 */
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303692 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
Eric Moore635374e2009-03-09 01:21:12 -06003693 "sequence\n", ioc->name));
3694 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3695 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3696 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3697 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3698 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3699 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3700 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3701
3702 /* wait 100 msec */
3703 if (sleep_flag == CAN_SLEEP)
3704 msleep(100);
3705 else
3706 mdelay(100);
3707
3708 if (count++ > 20)
3709 goto out;
3710
3711 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303712 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
Eric Moore635374e2009-03-09 01:21:12 -06003713 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3714 ioc->name, count, host_diagnostic));
3715
3716 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3717
3718 hcb_size = readl(&ioc->chip->HCBSize);
3719
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303720 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
Eric Moore635374e2009-03-09 01:21:12 -06003721 ioc->name));
3722 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3723 &ioc->chip->HostDiagnostic);
3724
3725 /* don't access any registers for 50 milliseconds */
3726 msleep(50);
3727
3728 /* 300 second max wait */
3729 for (count = 0; count < 3000000 ; count++) {
3730
3731 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3732
3733 if (host_diagnostic == 0xFFFFFFFF)
3734 goto out;
3735 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3736 break;
3737
3738 /* wait 100 msec */
3739 if (sleep_flag == CAN_SLEEP)
3740 msleep(1);
3741 else
3742 mdelay(1);
3743 }
3744
3745 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3746
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303747 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
Eric Moore635374e2009-03-09 01:21:12 -06003748 "assuming the HCB Address points to good F/W\n",
3749 ioc->name));
3750 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3751 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3752 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3753
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303754 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003755 "re-enable the HCDW\n", ioc->name));
3756 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3757 &ioc->chip->HCBSize);
3758 }
3759
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303760 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
Eric Moore635374e2009-03-09 01:21:12 -06003761 ioc->name));
3762 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3763 &ioc->chip->HostDiagnostic);
3764
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303765 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
Eric Moore635374e2009-03-09 01:21:12 -06003766 "diagnostic register\n", ioc->name));
3767 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3768
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303769 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
Eric Moore635374e2009-03-09 01:21:12 -06003770 "READY state\n", ioc->name));
3771 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3772 sleep_flag);
3773 if (ioc_state) {
3774 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3775 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3776 goto out;
3777 }
3778
Eric Moore635374e2009-03-09 01:21:12 -06003779 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3780 return 0;
3781
3782 out:
3783 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3784 return -EFAULT;
3785}
3786
3787/**
3788 * _base_make_ioc_ready - put controller in READY state
3789 * @ioc: per adapter object
3790 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3791 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3792 *
3793 * Returns 0 for success, non-zero for failure.
3794 */
3795static int
3796_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3797 enum reset_type type)
3798{
3799 u32 ioc_state;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303800 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06003801
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303802 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003803 __func__));
3804
Eric Moore3cb54692010-07-08 14:44:34 -06003805 if (ioc->pci_error_recovery)
3806 return 0;
3807
Eric Moore635374e2009-03-09 01:21:12 -06003808 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303809 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
Eric Moore635374e2009-03-09 01:21:12 -06003810 ioc->name, __func__, ioc_state));
3811
3812 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3813 return 0;
3814
3815 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303816 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06003817 "active!\n", ioc->name));
3818 goto issue_diag_reset;
3819 }
3820
3821 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3822 mpt2sas_base_fault_info(ioc, ioc_state &
3823 MPI2_DOORBELL_DATA_MASK);
3824 goto issue_diag_reset;
3825 }
3826
3827 if (type == FORCE_BIG_HAMMER)
3828 goto issue_diag_reset;
3829
3830 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3831 if (!(_base_send_ioc_reset(ioc,
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303832 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
3833 ioc->ioc_reset_count++;
Eric Moore635374e2009-03-09 01:21:12 -06003834 return 0;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303835 }
Eric Moore635374e2009-03-09 01:21:12 -06003836
3837 issue_diag_reset:
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303838 rc = _base_diag_reset(ioc, CAN_SLEEP);
3839 ioc->ioc_reset_count++;
3840 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06003841}
3842
3843/**
3844 * _base_make_ioc_operational - put controller in OPERATIONAL state
3845 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003846 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3847 *
3848 * Returns 0 for success, non-zero for failure.
3849 */
3850static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303851_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003852{
3853 int r, i;
3854 unsigned long flags;
3855 u32 reply_address;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303856 u16 smid;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303857 struct _tr_list *delayed_tr, *delayed_tr_next;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303858 u8 hide_flag;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303859 struct adapter_reply_queue *reply_q;
3860 long reply_post_free;
3861 u32 reply_post_free_sz;
Eric Moore635374e2009-03-09 01:21:12 -06003862
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303863 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003864 __func__));
3865
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303866 /* clean the delayed target reset list */
3867 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3868 &ioc->delayed_tr_list, list) {
3869 list_del(&delayed_tr->list);
3870 kfree(delayed_tr);
3871 }
3872
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303873 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3874 &ioc->delayed_tr_volume_list, list) {
3875 list_del(&delayed_tr->list);
3876 kfree(delayed_tr);
3877 }
3878
Eric Moore635374e2009-03-09 01:21:12 -06003879 /* initialize the scsi lookup free list */
3880 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3881 INIT_LIST_HEAD(&ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303882 smid = 1;
3883 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
Kashyap, Desai35f805b2010-11-13 04:34:06 +05303884 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
Eric Moore635374e2009-03-09 01:21:12 -06003885 ioc->scsi_lookup[i].cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303886 ioc->scsi_lookup[i].smid = smid;
3887 ioc->scsi_lookup[i].scmd = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303888 ioc->scsi_lookup[i].direct_io = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003889 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3890 &ioc->free_list);
3891 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303892
3893 /* hi-priority queue */
3894 INIT_LIST_HEAD(&ioc->hpr_free_list);
3895 smid = ioc->hi_priority_smid;
3896 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
3897 ioc->hpr_lookup[i].cb_idx = 0xFF;
3898 ioc->hpr_lookup[i].smid = smid;
3899 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
3900 &ioc->hpr_free_list);
3901 }
3902
3903 /* internal queue */
3904 INIT_LIST_HEAD(&ioc->internal_free_list);
3905 smid = ioc->internal_smid;
3906 for (i = 0; i < ioc->internal_depth; i++, smid++) {
3907 ioc->internal_lookup[i].cb_idx = 0xFF;
3908 ioc->internal_lookup[i].smid = smid;
3909 list_add_tail(&ioc->internal_lookup[i].tracker_list,
3910 &ioc->internal_free_list);
3911 }
Kashyap, Desai35f805b2010-11-13 04:34:06 +05303912
3913 /* chain pool */
3914 INIT_LIST_HEAD(&ioc->free_chain_list);
3915 for (i = 0; i < ioc->chain_depth; i++)
3916 list_add_tail(&ioc->chain_lookup[i].tracker_list,
3917 &ioc->free_chain_list);
3918
Eric Moore635374e2009-03-09 01:21:12 -06003919 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3920
3921 /* initialize Reply Free Queue */
3922 for (i = 0, reply_address = (u32)ioc->reply_dma ;
3923 i < ioc->reply_free_queue_depth ; i++, reply_address +=
3924 ioc->reply_sz)
3925 ioc->reply_free[i] = cpu_to_le32(reply_address);
3926
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303927 /* initialize reply queues */
3928 _base_assign_reply_queues(ioc);
3929
Eric Moore635374e2009-03-09 01:21:12 -06003930 /* initialize Reply Post Free Queue */
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303931 reply_post_free = (long)ioc->reply_post_free;
3932 reply_post_free_sz = ioc->reply_post_queue_depth *
3933 sizeof(Mpi2DefaultReplyDescriptor_t);
3934 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
3935 reply_q->reply_post_host_index = 0;
3936 reply_q->reply_post_free = (Mpi2ReplyDescriptorsUnion_t *)
3937 reply_post_free;
3938 for (i = 0; i < ioc->reply_post_queue_depth; i++)
3939 reply_q->reply_post_free[i].Words =
3940 cpu_to_le64(ULLONG_MAX);
3941 if (!_base_is_controller_msix_enabled(ioc))
3942 goto skip_init_reply_post_free_queue;
3943 reply_post_free += reply_post_free_sz;
3944 }
3945 skip_init_reply_post_free_queue:
Eric Moore635374e2009-03-09 01:21:12 -06003946
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303947 r = _base_send_ioc_init(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003948 if (r)
3949 return r;
3950
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303951 /* initialize reply free host index */
Eric Moore635374e2009-03-09 01:21:12 -06003952 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
Eric Moore635374e2009-03-09 01:21:12 -06003953 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05303954
3955 /* initialize reply post host index */
3956 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
3957 writel(reply_q->msix_index << MPI2_RPHI_MSIX_INDEX_SHIFT,
3958 &ioc->chip->ReplyPostHostIndex);
3959 if (!_base_is_controller_msix_enabled(ioc))
3960 goto skip_init_reply_post_host_index;
3961 }
3962
3963 skip_init_reply_post_host_index:
Eric Moore635374e2009-03-09 01:21:12 -06003964
3965 _base_unmask_interrupts(ioc);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303966 r = _base_event_notification(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003967 if (r)
3968 return r;
3969
3970 if (sleep_flag == CAN_SLEEP)
3971 _base_static_config_pages(ioc);
3972
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303973 if (ioc->wait_for_port_enable_to_complete && ioc->is_warpdrive) {
3974 if (ioc->manu_pg10.OEMIdentifier == 0x80) {
3975 hide_flag = (u8) (ioc->manu_pg10.OEMSpecificFlags0 &
3976 MFG_PAGE10_HIDE_SSDS_MASK);
3977 if (hide_flag != MFG_PAGE10_HIDE_SSDS_MASK)
3978 ioc->mfg_pg10_hide_flag = hide_flag;
3979 }
3980 }
3981
Kashyap, Desai570c67a2010-06-17 13:43:17 +05303982 if (ioc->wait_for_port_enable_to_complete) {
3983 if (diag_buffer_enable != 0)
3984 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
3985 if (disable_discovery > 0)
3986 return r;
3987 }
Kashyap, Desaidd5fd332010-06-17 13:31:17 +05303988
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303989 r = _base_send_port_enable(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003990 if (r)
3991 return r;
3992
3993 return r;
3994}
3995
3996/**
3997 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
3998 * @ioc: per adapter object
3999 *
4000 * Return nothing.
4001 */
4002void
4003mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
4004{
4005 struct pci_dev *pdev = ioc->pdev;
4006
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304007 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004008 __func__));
4009
4010 _base_mask_interrupts(ioc);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304011 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004012 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05304013 ioc->shost_recovery = 0;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304014 _base_free_irq(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004015 _base_disable_msix(ioc);
4016 if (ioc->chip_phys)
4017 iounmap(ioc->chip);
Eric Moore635374e2009-03-09 01:21:12 -06004018 ioc->chip_phys = 0;
4019 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05304020 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06004021 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06004022 return;
4023}
4024
4025/**
4026 * mpt2sas_base_attach - attach controller instance
4027 * @ioc: per adapter object
4028 *
4029 * Returns 0 for success, non-zero for failure.
4030 */
4031int
4032mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
4033{
4034 int r, i;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304035 int cpu_id, last_cpu_id = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004036
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304037 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004038 __func__));
4039
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304040 /* setup cpu_msix_table */
4041 ioc->cpu_count = num_online_cpus();
4042 for_each_online_cpu(cpu_id)
4043 last_cpu_id = cpu_id;
4044 ioc->cpu_msix_table_sz = last_cpu_id + 1;
4045 ioc->cpu_msix_table = kzalloc(ioc->cpu_msix_table_sz, GFP_KERNEL);
4046 ioc->reply_queue_count = 1;
4047 if (!ioc->cpu_msix_table) {
4048 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
4049 "cpu_msix_table failed!!!\n", ioc->name));
4050 r = -ENOMEM;
4051 goto out_free_resources;
4052 }
4053
4054 if (ioc->is_warpdrive) {
4055 ioc->reply_post_host_index = kcalloc(ioc->cpu_msix_table_sz,
4056 sizeof(resource_size_t *), GFP_KERNEL);
4057 if (!ioc->reply_post_host_index) {
4058 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation "
4059 "for cpu_msix_table failed!!!\n", ioc->name));
4060 r = -ENOMEM;
4061 goto out_free_resources;
4062 }
4063 }
4064
Eric Moore635374e2009-03-09 01:21:12 -06004065 r = mpt2sas_base_map_resources(ioc);
4066 if (r)
4067 return r;
4068
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304069 if (ioc->is_warpdrive) {
4070 ioc->reply_post_host_index[0] =
4071 (resource_size_t *)&ioc->chip->ReplyPostHostIndex;
4072
4073 for (i = 1; i < ioc->cpu_msix_table_sz; i++)
4074 ioc->reply_post_host_index[i] = (resource_size_t *)
4075 ((u8 *)&ioc->chip->Doorbell + (0x4000 + ((i - 1)
4076 * 4)));
4077 }
4078
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304079 pci_set_drvdata(ioc->pdev, ioc->shost);
Kashyap, Desai96b681c2009-09-23 17:32:06 +05304080 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06004081 if (r)
4082 goto out_free_resources;
4083
Kashyap, Desai96b681c2009-09-23 17:32:06 +05304084 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Eric Moore635374e2009-03-09 01:21:12 -06004085 if (r)
4086 goto out_free_resources;
4087
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304088 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
4089 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304090 if (!ioc->pfacts) {
4091 r = -ENOMEM;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304092 goto out_free_resources;
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304093 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304094
4095 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
4096 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
4097 if (r)
4098 goto out_free_resources;
4099 }
4100
Eric Moore635374e2009-03-09 01:21:12 -06004101 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
4102 if (r)
4103 goto out_free_resources;
4104
4105 init_waitqueue_head(&ioc->reset_wq);
4106
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304107 /* allocate memory pd handle bitmask list */
4108 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
4109 if (ioc->facts.MaxDevHandle % 8)
4110 ioc->pd_handles_sz++;
4111 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
4112 GFP_KERNEL);
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304113 if (!ioc->pd_handles) {
4114 r = -ENOMEM;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304115 goto out_free_resources;
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304116 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304117
Kashyap, Desaie75b9b62009-12-16 18:52:39 +05304118 ioc->fwfault_debug = mpt2sas_fwfault_debug;
4119
Eric Moore635374e2009-03-09 01:21:12 -06004120 /* base internal command bits */
4121 mutex_init(&ioc->base_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004122 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4123 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
4124
4125 /* transport internal command bits */
4126 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4127 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
4128 mutex_init(&ioc->transport_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004129
Kashyap, Desaid685c262009-11-17 13:16:37 +05304130 /* scsih internal command bits */
4131 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4132 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
4133 mutex_init(&ioc->scsih_cmds.mutex);
4134
Eric Moore635374e2009-03-09 01:21:12 -06004135 /* task management internal command bits */
4136 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4137 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
4138 mutex_init(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004139
4140 /* config page internal command bits */
4141 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
4142 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
4143 mutex_init(&ioc->config_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004144
4145 /* ctl module internal command bits */
4146 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304147 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06004148 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
4149 mutex_init(&ioc->ctl_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06004150
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304151 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4152 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
Kashyap, Desai769578f2010-06-17 13:49:28 +05304153 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
4154 !ioc->ctl_cmds.sense) {
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05304155 r = -ENOMEM;
4156 goto out_free_resources;
4157 }
4158
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05304159 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
4160 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
4161 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
4162 r = -ENOMEM;
4163 goto out_free_resources;
4164 }
4165
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05304166 init_completion(&ioc->shost_recovery_done);
4167
Eric Moore635374e2009-03-09 01:21:12 -06004168 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
4169 ioc->event_masks[i] = -1;
4170
4171 /* here we enable the events we care about */
4172 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
4173 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
4174 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
4175 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4176 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
4177 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
4178 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
4179 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
4180 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
Eric Moore635374e2009-03-09 01:21:12 -06004181 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304182 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06004183 if (r)
4184 goto out_free_resources;
4185
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05304186 if (missing_delay[0] != -1 && missing_delay[1] != -1)
4187 _base_update_missing_delay(ioc, missing_delay[0],
4188 missing_delay[1]);
4189
Kashyap, Desaie4750c92009-08-07 19:37:59 +05304190 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004191 return 0;
4192
4193 out_free_resources:
4194
4195 ioc->remove_host = 1;
4196 mpt2sas_base_free_resources(ioc);
4197 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304198 pci_set_drvdata(ioc->pdev, NULL);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304199 kfree(ioc->cpu_msix_table);
4200 if (ioc->is_warpdrive)
4201 kfree(ioc->reply_post_host_index);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304202 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06004203 kfree(ioc->tm_cmds.reply);
4204 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304205 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004206 kfree(ioc->config_cmds.reply);
4207 kfree(ioc->base_cmds.reply);
4208 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304209 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06004210 kfree(ioc->pfacts);
4211 ioc->ctl_cmds.reply = NULL;
4212 ioc->base_cmds.reply = NULL;
4213 ioc->tm_cmds.reply = NULL;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304214 ioc->scsih_cmds.reply = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004215 ioc->transport_cmds.reply = NULL;
4216 ioc->config_cmds.reply = NULL;
4217 ioc->pfacts = NULL;
4218 return r;
4219}
4220
4221
4222/**
4223 * mpt2sas_base_detach - remove controller instance
4224 * @ioc: per adapter object
4225 *
4226 * Return nothing.
4227 */
4228void
4229mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
4230{
Eric Moore635374e2009-03-09 01:21:12 -06004231
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304232 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004233 __func__));
4234
Kashyap, Desaie4750c92009-08-07 19:37:59 +05304235 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06004236 mpt2sas_base_free_resources(ioc);
4237 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05304238 pci_set_drvdata(ioc->pdev, NULL);
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05304239 kfree(ioc->cpu_msix_table);
4240 if (ioc->is_warpdrive)
4241 kfree(ioc->reply_post_host_index);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05304242 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06004243 kfree(ioc->pfacts);
4244 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05304245 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06004246 kfree(ioc->base_cmds.reply);
4247 kfree(ioc->tm_cmds.reply);
4248 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304249 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06004250 kfree(ioc->config_cmds.reply);
4251}
4252
4253/**
4254 * _base_reset_handler - reset callback handler (for base)
4255 * @ioc: per adapter object
4256 * @reset_phase: phase
4257 *
4258 * The handler for doing any required cleanup or initialization.
4259 *
4260 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
4261 * MPT2_IOC_DONE_RESET
4262 *
4263 * Return nothing.
4264 */
4265static void
4266_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
4267{
Kashyap, Desai42244892011-01-04 11:38:39 +05304268 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4269 mpt2sas_ctl_reset_handler(ioc, reset_phase);
Eric Moore635374e2009-03-09 01:21:12 -06004270 switch (reset_phase) {
4271 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304272 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004273 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
4274 break;
4275 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304276 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004277 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
4278 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
4279 ioc->transport_cmds.status |= MPT2_CMD_RESET;
4280 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
4281 complete(&ioc->transport_cmds.done);
4282 }
4283 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
4284 ioc->base_cmds.status |= MPT2_CMD_RESET;
4285 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
4286 complete(&ioc->base_cmds.done);
4287 }
4288 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
4289 ioc->config_cmds.status |= MPT2_CMD_RESET;
4290 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07004291 ioc->config_cmds.smid = USHRT_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06004292 complete(&ioc->config_cmds.done);
4293 }
4294 break;
4295 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304296 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06004297 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
4298 break;
4299 }
Eric Moore635374e2009-03-09 01:21:12 -06004300}
4301
4302/**
4303 * _wait_for_commands_to_complete - reset controller
4304 * @ioc: Pointer to MPT_ADAPTER structure
4305 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4306 *
4307 * This function waiting(3s) for all pending commands to complete
4308 * prior to putting controller in reset.
4309 */
4310static void
4311_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4312{
4313 u32 ioc_state;
4314 unsigned long flags;
4315 u16 i;
4316
4317 ioc->pending_io_count = 0;
4318 if (sleep_flag != CAN_SLEEP)
4319 return;
4320
4321 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4322 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4323 return;
4324
4325 /* pending command count */
4326 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304327 for (i = 0; i < ioc->scsiio_depth; i++)
Eric Moore635374e2009-03-09 01:21:12 -06004328 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4329 ioc->pending_io_count++;
4330 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4331
4332 if (!ioc->pending_io_count)
4333 return;
4334
4335 /* wait for pending commands to complete */
Kashyap, Desaid2742132010-06-17 13:28:55 +05304336 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
Eric Moore635374e2009-03-09 01:21:12 -06004337}
4338
4339/**
4340 * mpt2sas_base_hard_reset_handler - reset controller
4341 * @ioc: Pointer to MPT_ADAPTER structure
4342 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4343 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4344 *
4345 * Returns 0 for success, non-zero for failure.
4346 */
4347int
4348mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4349 enum reset_type type)
4350{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304351 int r;
Eric Moore635374e2009-03-09 01:21:12 -06004352 unsigned long flags;
Kashyap, Desai42244892011-01-04 11:38:39 +05304353 u8 pe_complete = ioc->wait_for_port_enable_to_complete;
Eric Moore635374e2009-03-09 01:21:12 -06004354
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304355 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004356 __func__));
4357
Eric Moore3cb54692010-07-08 14:44:34 -06004358 if (ioc->pci_error_recovery) {
4359 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4360 ioc->name, __func__);
4361 r = 0;
4362 goto out;
4363 }
4364
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05304365 if (mpt2sas_fwfault_debug)
4366 mpt2sas_halt_firmware(ioc);
4367
Kashyap, Desaid2742132010-06-17 13:28:55 +05304368 /* TODO - What we really should be doing is pulling
4369 * out all the code associated with NO_SLEEP; its never used.
4370 * That is legacy code from mpt fusion driver, ported over.
4371 * I will leave this BUG_ON here for now till its been resolved.
4372 */
4373 BUG_ON(sleep_flag == NO_SLEEP);
4374
4375 /* wait for an active reset in progress to complete */
4376 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4377 do {
4378 ssleep(1);
4379 } while (ioc->shost_recovery == 1);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304380 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304381 __func__));
4382 return ioc->ioc_reset_in_progress_status;
Eric Moore635374e2009-03-09 01:21:12 -06004383 }
Kashyap, Desaid2742132010-06-17 13:28:55 +05304384
4385 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06004386 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004387 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4388
4389 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4390 _wait_for_commands_to_complete(ioc, sleep_flag);
4391 _base_mask_interrupts(ioc);
4392 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4393 if (r)
4394 goto out;
4395 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
Kashyap, Desai42244892011-01-04 11:38:39 +05304396
4397 /* If this hard reset is called while port enable is active, then
4398 * there is no reason to call make_ioc_operational
4399 */
4400 if (pe_complete) {
4401 r = -EFAULT;
4402 goto out;
4403 }
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304404 r = _base_make_ioc_operational(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004405 if (!r)
4406 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4407 out:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304408 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
Eric Moore635374e2009-03-09 01:21:12 -06004409 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4410
4411 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304412 ioc->ioc_reset_in_progress_status = r;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304413 ioc->shost_recovery = 0;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05304414 complete(&ioc->shost_recovery_done);
Eric Moore635374e2009-03-09 01:21:12 -06004415 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304416 mutex_unlock(&ioc->reset_in_progress_mutex);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05304417
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304418 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304419 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06004420 return r;
4421}