blob: 0e30827d44e715f8a8ca55615b8ddc240dc9825a [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 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +053068#define MPT2SAS_MAX_REQUEST_QUEUE 600 /* maximum controller queue depth */
Eric Moore635374e2009-03-09 01:21:12 -060069
70static int max_queue_depth = -1;
71module_param(max_queue_depth, int, 0);
72MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
73
74static int max_sgl_entries = -1;
75module_param(max_sgl_entries, int, 0);
76MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
77
78static int msix_disable = -1;
79module_param(msix_disable, int, 0);
80MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
81
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +053082static int missing_delay[2] = {-1, -1};
83module_param_array(missing_delay, int, NULL, 0);
84MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
85
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053086/* diag_buffer_enable is bitwise
Kashyap, Desai1b01fe32009-09-23 17:28:59 +053087 * bit 0 set = TRACE
88 * bit 1 set = SNAPSHOT
89 * bit 2 set = EXTENDED
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053090 *
91 * Either bit can be set, or both
92 */
93static int diag_buffer_enable;
94module_param(diag_buffer_enable, int, 0);
Kashyap, Desai1b01fe32009-09-23 17:28:59 +053095MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
96 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053097
Kashyap, Desaifa7f3162009-09-23 17:26:58 +053098int mpt2sas_fwfault_debug;
99MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
100 "and halt firmware - (default=0)");
101
Kashyap, Desaidd5fd332010-06-17 13:31:17 +0530102static int disable_discovery = -1;
103module_param(disable_discovery, int, 0);
104MODULE_PARM_DESC(disable_discovery, " disable discovery ");
105
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530106/**
107 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
108 *
109 */
110static int
111_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
112{
113 int ret = param_set_int(val, kp);
114 struct MPT2SAS_ADAPTER *ioc;
115
116 if (ret)
117 return ret;
118
Kashyap, Desaie75b9b62009-12-16 18:52:39 +0530119 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530120 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
121 ioc->fwfault_debug = mpt2sas_fwfault_debug;
122 return 0;
123}
124module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
125 param_get_int, &mpt2sas_fwfault_debug, 0644);
126
Eric Moore635374e2009-03-09 01:21:12 -0600127/**
128 * _base_fault_reset_work - workq handling ioc fault conditions
129 * @work: input argument, used to derive ioc
130 * Context: sleep.
131 *
132 * Return nothing.
133 */
134static void
135_base_fault_reset_work(struct work_struct *work)
136{
137 struct MPT2SAS_ADAPTER *ioc =
138 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
139 unsigned long flags;
140 u32 doorbell;
141 int rc;
142
143 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530144 if (ioc->shost_recovery)
Eric Moore635374e2009-03-09 01:21:12 -0600145 goto rearm_timer;
146 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
147
148 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
149 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
150 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
151 FORCE_BIG_HAMMER);
152 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
153 __func__, (rc == 0) ? "success" : "failed");
154 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
155 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
156 mpt2sas_base_fault_info(ioc, doorbell &
157 MPI2_DOORBELL_DATA_MASK);
158 }
159
160 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
161 rearm_timer:
162 if (ioc->fault_reset_work_q)
163 queue_delayed_work(ioc->fault_reset_work_q,
164 &ioc->fault_reset_work,
165 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
166 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
167}
168
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530169/**
170 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530171 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530172 * Context: sleep.
173 *
174 * Return nothing.
175 */
176void
177mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
178{
179 unsigned long flags;
180
181 if (ioc->fault_reset_work_q)
182 return;
183
184 /* initialize fault polling */
185 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
186 snprintf(ioc->fault_reset_work_q_name,
187 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
188 ioc->fault_reset_work_q =
189 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
190 if (!ioc->fault_reset_work_q) {
191 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
192 ioc->name, __func__, __LINE__);
193 return;
194 }
195 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
196 if (ioc->fault_reset_work_q)
197 queue_delayed_work(ioc->fault_reset_work_q,
198 &ioc->fault_reset_work,
199 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
200 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
201}
202
203/**
204 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530205 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530206 * Context: sleep.
207 *
208 * Return nothing.
209 */
210void
211mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
212{
213 unsigned long flags;
214 struct workqueue_struct *wq;
215
216 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
217 wq = ioc->fault_reset_work_q;
218 ioc->fault_reset_work_q = NULL;
219 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
220 if (wq) {
221 if (!cancel_delayed_work(&ioc->fault_reset_work))
222 flush_workqueue(wq);
223 destroy_workqueue(wq);
224 }
225}
226
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530227/**
228 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
229 * @ioc: per adapter object
230 * @fault_code: fault code
231 *
232 * Return nothing.
233 */
234void
235mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
236{
237 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
238 ioc->name, fault_code);
239}
240
241/**
242 * mpt2sas_halt_firmware - halt's mpt controller firmware
243 * @ioc: per adapter object
244 *
245 * For debugging timeout related issues. Writing 0xCOFFEE00
246 * to the doorbell register will halt controller firmware. With
247 * the purpose to stop both driver and firmware, the enduser can
248 * obtain a ring buffer from controller UART.
249 */
250void
251mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
252{
253 u32 doorbell;
254
255 if (!ioc->fwfault_debug)
256 return;
257
258 dump_stack();
259
260 doorbell = readl(&ioc->chip->Doorbell);
261 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
262 mpt2sas_base_fault_info(ioc , doorbell);
263 else {
264 writel(0xC0FFEE00, &ioc->chip->Doorbell);
265 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
266 "timeout\n", ioc->name);
267 }
268
269 panic("panic in %s\n", __func__);
270}
271
Eric Moore635374e2009-03-09 01:21:12 -0600272#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
273/**
274 * _base_sas_ioc_info - verbose translation of the ioc status
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530275 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600276 * @mpi_reply: reply mf payload returned from firmware
277 * @request_hdr: request mf
278 *
279 * Return nothing.
280 */
281static void
282_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
283 MPI2RequestHeader_t *request_hdr)
284{
285 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
286 MPI2_IOCSTATUS_MASK;
287 char *desc = NULL;
288 u16 frame_sz;
289 char *func_str = NULL;
290
291 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
292 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
293 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
294 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
295 return;
296
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530297 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
298 return;
299
Eric Moore635374e2009-03-09 01:21:12 -0600300 switch (ioc_status) {
301
302/****************************************************************************
303* Common IOCStatus values for all replies
304****************************************************************************/
305
306 case MPI2_IOCSTATUS_INVALID_FUNCTION:
307 desc = "invalid function";
308 break;
309 case MPI2_IOCSTATUS_BUSY:
310 desc = "busy";
311 break;
312 case MPI2_IOCSTATUS_INVALID_SGL:
313 desc = "invalid sgl";
314 break;
315 case MPI2_IOCSTATUS_INTERNAL_ERROR:
316 desc = "internal error";
317 break;
318 case MPI2_IOCSTATUS_INVALID_VPID:
319 desc = "invalid vpid";
320 break;
321 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
322 desc = "insufficient resources";
323 break;
324 case MPI2_IOCSTATUS_INVALID_FIELD:
325 desc = "invalid field";
326 break;
327 case MPI2_IOCSTATUS_INVALID_STATE:
328 desc = "invalid state";
329 break;
330 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
331 desc = "op state not supported";
332 break;
333
334/****************************************************************************
335* Config IOCStatus values
336****************************************************************************/
337
338 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
339 desc = "config invalid action";
340 break;
341 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
342 desc = "config invalid type";
343 break;
344 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
345 desc = "config invalid page";
346 break;
347 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
348 desc = "config invalid data";
349 break;
350 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
351 desc = "config no defaults";
352 break;
353 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
354 desc = "config cant commit";
355 break;
356
357/****************************************************************************
358* SCSI IO Reply
359****************************************************************************/
360
361 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
362 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
363 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
364 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
365 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
366 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
367 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
368 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
369 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
370 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
371 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
372 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
373 break;
374
375/****************************************************************************
376* For use by SCSI Initiator and SCSI Target end-to-end data protection
377****************************************************************************/
378
379 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
380 desc = "eedp guard error";
381 break;
382 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
383 desc = "eedp ref tag error";
384 break;
385 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
386 desc = "eedp app tag error";
387 break;
388
389/****************************************************************************
390* SCSI Target values
391****************************************************************************/
392
393 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
394 desc = "target invalid io index";
395 break;
396 case MPI2_IOCSTATUS_TARGET_ABORTED:
397 desc = "target aborted";
398 break;
399 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
400 desc = "target no conn retryable";
401 break;
402 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
403 desc = "target no connection";
404 break;
405 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
406 desc = "target xfer count mismatch";
407 break;
408 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
409 desc = "target data offset error";
410 break;
411 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
412 desc = "target too much write data";
413 break;
414 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
415 desc = "target iu too short";
416 break;
417 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
418 desc = "target ack nak timeout";
419 break;
420 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
421 desc = "target nak received";
422 break;
423
424/****************************************************************************
425* Serial Attached SCSI values
426****************************************************************************/
427
428 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
429 desc = "smp request failed";
430 break;
431 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
432 desc = "smp data overrun";
433 break;
434
435/****************************************************************************
436* Diagnostic Buffer Post / Diagnostic Release values
437****************************************************************************/
438
439 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
440 desc = "diagnostic released";
441 break;
442 default:
443 break;
444 }
445
446 if (!desc)
447 return;
448
449 switch (request_hdr->Function) {
450 case MPI2_FUNCTION_CONFIG:
451 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
452 func_str = "config_page";
453 break;
454 case MPI2_FUNCTION_SCSI_TASK_MGMT:
455 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
456 func_str = "task_mgmt";
457 break;
458 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
459 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
460 func_str = "sas_iounit_ctl";
461 break;
462 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
463 frame_sz = sizeof(Mpi2SepRequest_t);
464 func_str = "enclosure";
465 break;
466 case MPI2_FUNCTION_IOC_INIT:
467 frame_sz = sizeof(Mpi2IOCInitRequest_t);
468 func_str = "ioc_init";
469 break;
470 case MPI2_FUNCTION_PORT_ENABLE:
471 frame_sz = sizeof(Mpi2PortEnableRequest_t);
472 func_str = "port_enable";
473 break;
474 case MPI2_FUNCTION_SMP_PASSTHROUGH:
475 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
476 func_str = "smp_passthru";
477 break;
478 default:
479 frame_sz = 32;
480 func_str = "unknown";
481 break;
482 }
483
484 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
485 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
486
487 _debug_dump_mf(request_hdr, frame_sz/4);
488}
489
490/**
491 * _base_display_event_data - verbose translation of firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530492 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600493 * @mpi_reply: reply mf payload returned from firmware
494 *
495 * Return nothing.
496 */
497static void
498_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
499 Mpi2EventNotificationReply_t *mpi_reply)
500{
501 char *desc = NULL;
502 u16 event;
503
504 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
505 return;
506
507 event = le16_to_cpu(mpi_reply->Event);
508
509 switch (event) {
510 case MPI2_EVENT_LOG_DATA:
511 desc = "Log Data";
512 break;
513 case MPI2_EVENT_STATE_CHANGE:
514 desc = "Status Change";
515 break;
516 case MPI2_EVENT_HARD_RESET_RECEIVED:
517 desc = "Hard Reset Received";
518 break;
519 case MPI2_EVENT_EVENT_CHANGE:
520 desc = "Event Change";
521 break;
522 case MPI2_EVENT_TASK_SET_FULL:
523 desc = "Task Set Full";
524 break;
525 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
526 desc = "Device Status Change";
527 break;
528 case MPI2_EVENT_IR_OPERATION_STATUS:
529 desc = "IR Operation Status";
530 break;
531 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530532 {
533 Mpi2EventDataSasDiscovery_t *event_data =
534 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
535 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
536 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
537 "start" : "stop");
538 if (event_data->DiscoveryStatus)
539 printk("discovery_status(0x%08x)",
540 le32_to_cpu(event_data->DiscoveryStatus));
Julia Lawall7968f192010-08-05 22:19:36 +0200541 printk("\n");
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530542 return;
543 }
Eric Moore635374e2009-03-09 01:21:12 -0600544 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
545 desc = "SAS Broadcast Primitive";
546 break;
547 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
548 desc = "SAS Init Device Status Change";
549 break;
550 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
551 desc = "SAS Init Table Overflow";
552 break;
553 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
554 desc = "SAS Topology Change List";
555 break;
556 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
557 desc = "SAS Enclosure Device Status Change";
558 break;
559 case MPI2_EVENT_IR_VOLUME:
560 desc = "IR Volume";
561 break;
562 case MPI2_EVENT_IR_PHYSICAL_DISK:
563 desc = "IR Physical Disk";
564 break;
565 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
566 desc = "IR Configuration Change List";
567 break;
568 case MPI2_EVENT_LOG_ENTRY_ADDED:
569 desc = "Log Entry Added";
570 break;
571 }
572
573 if (!desc)
574 return;
575
576 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
577}
578#endif
579
580/**
581 * _base_sas_log_info - verbose translation of firmware log info
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530582 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600583 * @log_info: log info
584 *
585 * Return nothing.
586 */
587static void
588_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
589{
590 union loginfo_type {
591 u32 loginfo;
592 struct {
593 u32 subcode:16;
594 u32 code:8;
595 u32 originator:4;
596 u32 bus_type:4;
597 } dw;
598 };
599 union loginfo_type sas_loginfo;
600 char *originator_str = NULL;
601
602 sas_loginfo.loginfo = log_info;
603 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
604 return;
605
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +0530606 /* each nexus loss loginfo */
607 if (log_info == 0x31170000)
608 return;
609
Eric Moore635374e2009-03-09 01:21:12 -0600610 /* eat the loginfos associated with task aborts */
611 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
612 0x31140000 || log_info == 0x31130000))
613 return;
614
615 switch (sas_loginfo.dw.originator) {
616 case 0:
617 originator_str = "IOP";
618 break;
619 case 1:
620 originator_str = "PL";
621 break;
622 case 2:
623 originator_str = "IR";
624 break;
625 }
626
627 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
628 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
629 originator_str, sas_loginfo.dw.code,
630 sas_loginfo.dw.subcode);
631}
632
633/**
Eric Moore635374e2009-03-09 01:21:12 -0600634 * _base_display_reply_info -
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530635 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600636 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530637 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600638 * @reply: reply message frame(lower 32bit addr)
639 *
640 * Return nothing.
641 */
642static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530643_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600644 u32 reply)
645{
646 MPI2DefaultReply_t *mpi_reply;
647 u16 ioc_status;
648
649 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
650 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
651#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
652 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
653 (ioc->logging_level & MPT_DEBUG_REPLY)) {
654 _base_sas_ioc_info(ioc , mpi_reply,
655 mpt2sas_base_get_msg_frame(ioc, smid));
656 }
657#endif
658 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
659 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
660}
661
662/**
663 * mpt2sas_base_done - base internal command completion routine
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530664 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600665 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530666 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600667 * @reply: reply message frame(lower 32bit addr)
668 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530669 * Return 1 meaning mf should be freed from _base_interrupt
670 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600671 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530672u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530673mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
674 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600675{
676 MPI2DefaultReply_t *mpi_reply;
677
678 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
679 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530680 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600681
682 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530683 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600684
685 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
686 if (mpi_reply) {
687 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
688 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
689 }
690 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
691 complete(&ioc->base_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530692 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600693}
694
695/**
696 * _base_async_event - main callback handler for firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530697 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530698 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600699 * @reply: reply message frame(lower 32bit addr)
700 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530701 * Return 1 meaning mf should be freed from _base_interrupt
702 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600703 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530704static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530705_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600706{
707 Mpi2EventNotificationReply_t *mpi_reply;
708 Mpi2EventAckRequest_t *ack_request;
709 u16 smid;
710
711 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
712 if (!mpi_reply)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530713 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600714 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530715 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600716#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
717 _base_display_event_data(ioc, mpi_reply);
718#endif
719 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
720 goto out;
721 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
722 if (!smid) {
723 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
724 ioc->name, __func__);
725 goto out;
726 }
727
728 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
729 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
730 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
731 ack_request->Event = mpi_reply->Event;
732 ack_request->EventContext = mpi_reply->EventContext;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530733 ack_request->VF_ID = 0; /* TODO */
734 ack_request->VP_ID = 0;
735 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600736
737 out:
738
739 /* scsih callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530740 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600741
742 /* ctl callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530743 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530744
745 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600746}
747
748/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530749 * _base_get_cb_idx - obtain the callback index
750 * @ioc: per adapter object
751 * @smid: system request message index
752 *
753 * Return callback index.
754 */
755static u8
756_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
757{
758 int i;
759 u8 cb_idx = 0xFF;
760
761 if (smid >= ioc->hi_priority_smid) {
762 if (smid < ioc->internal_smid) {
763 i = smid - ioc->hi_priority_smid;
764 cb_idx = ioc->hpr_lookup[i].cb_idx;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530765 } else if (smid <= ioc->hba_queue_depth) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530766 i = smid - ioc->internal_smid;
767 cb_idx = ioc->internal_lookup[i].cb_idx;
768 }
769 } else {
770 i = smid - 1;
771 cb_idx = ioc->scsi_lookup[i].cb_idx;
772 }
773 return cb_idx;
774}
775
776/**
Eric Moore635374e2009-03-09 01:21:12 -0600777 * _base_mask_interrupts - disable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530778 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600779 *
780 * Disabling ResetIRQ, Reply and Doorbell Interrupts
781 *
782 * Return nothing.
783 */
784static void
785_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
786{
787 u32 him_register;
788
789 ioc->mask_interrupts = 1;
790 him_register = readl(&ioc->chip->HostInterruptMask);
791 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
792 writel(him_register, &ioc->chip->HostInterruptMask);
793 readl(&ioc->chip->HostInterruptMask);
794}
795
796/**
797 * _base_unmask_interrupts - enable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530798 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600799 *
800 * Enabling only Reply Interrupts
801 *
802 * Return nothing.
803 */
804static void
805_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
806{
807 u32 him_register;
808
Eric Moore635374e2009-03-09 01:21:12 -0600809 him_register = readl(&ioc->chip->HostInterruptMask);
810 him_register &= ~MPI2_HIM_RIM;
811 writel(him_register, &ioc->chip->HostInterruptMask);
812 ioc->mask_interrupts = 0;
813}
814
Kashyap, Desai5b768582009-08-20 13:24:31 +0530815union reply_descriptor {
816 u64 word;
817 struct {
818 u32 low;
819 u32 high;
820 } u;
821};
822
Eric Moore635374e2009-03-09 01:21:12 -0600823/**
824 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
825 * @irq: irq number (not used)
826 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
827 * @r: pt_regs pointer (not used)
828 *
829 * Return IRQ_HANDLE if processed, else IRQ_NONE.
830 */
831static irqreturn_t
832_base_interrupt(int irq, void *bus_id)
833{
Eric Moore03ea1112009-04-21 15:37:57 -0600834 union reply_descriptor rd;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530835 u32 completed_cmds;
Eric Moore635374e2009-03-09 01:21:12 -0600836 u8 request_desript_type;
837 u16 smid;
838 u8 cb_idx;
839 u32 reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530840 u8 msix_index;
Eric Moore635374e2009-03-09 01:21:12 -0600841 struct MPT2SAS_ADAPTER *ioc = bus_id;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530842 Mpi2ReplyDescriptorsUnion_t *rpf;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530843 u8 rc;
Eric Moore635374e2009-03-09 01:21:12 -0600844
845 if (ioc->mask_interrupts)
846 return IRQ_NONE;
847
Kashyap, Desai5b768582009-08-20 13:24:31 +0530848 rpf = &ioc->reply_post_free[ioc->reply_post_host_index];
849 request_desript_type = rpf->Default.ReplyFlags
850 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600851 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
852 return IRQ_NONE;
853
854 completed_cmds = 0;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530855 cb_idx = 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -0600856 do {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530857 rd.word = rpf->Words;
Eric Moore03ea1112009-04-21 15:37:57 -0600858 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
Eric Moore635374e2009-03-09 01:21:12 -0600859 goto out;
860 reply = 0;
861 cb_idx = 0xFF;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530862 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530863 msix_index = rpf->Default.MSIxIndex;
Eric Moore635374e2009-03-09 01:21:12 -0600864 if (request_desript_type ==
865 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530866 reply = le32_to_cpu
867 (rpf->AddressReply.ReplyFrameAddress);
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530868 if (reply > ioc->reply_dma_max_address ||
869 reply < ioc->reply_dma_min_address)
870 reply = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600871 } else if (request_desript_type ==
872 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
873 goto next;
874 else if (request_desript_type ==
875 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
876 goto next;
877 if (smid)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530878 cb_idx = _base_get_cb_idx(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600879 if (smid && cb_idx != 0xFF) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530880 rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530881 reply);
Eric Moore635374e2009-03-09 01:21:12 -0600882 if (reply)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530883 _base_display_reply_info(ioc, smid, msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600884 reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530885 if (rc)
886 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600887 }
888 if (!smid)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530889 _base_async_event(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600890
891 /* reply free queue handling */
892 if (reply) {
893 ioc->reply_free_host_index =
894 (ioc->reply_free_host_index ==
895 (ioc->reply_free_queue_depth - 1)) ?
896 0 : ioc->reply_free_host_index + 1;
897 ioc->reply_free[ioc->reply_free_host_index] =
898 cpu_to_le32(reply);
Kashyap, Desai5b768582009-08-20 13:24:31 +0530899 wmb();
Eric Moore635374e2009-03-09 01:21:12 -0600900 writel(ioc->reply_free_host_index,
901 &ioc->chip->ReplyFreeHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600902 }
903
904 next:
Kashyap, Desai5b768582009-08-20 13:24:31 +0530905
906 rpf->Words = ULLONG_MAX;
907 ioc->reply_post_host_index = (ioc->reply_post_host_index ==
908 (ioc->reply_post_queue_depth - 1)) ? 0 :
909 ioc->reply_post_host_index + 1;
Eric Moore635374e2009-03-09 01:21:12 -0600910 request_desript_type =
Kashyap, Desai5b768582009-08-20 13:24:31 +0530911 ioc->reply_post_free[ioc->reply_post_host_index].Default.
912 ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600913 completed_cmds++;
914 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
915 goto out;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530916 if (!ioc->reply_post_host_index)
917 rpf = ioc->reply_post_free;
918 else
919 rpf++;
Eric Moore635374e2009-03-09 01:21:12 -0600920 } while (1);
921
922 out:
923
924 if (!completed_cmds)
925 return IRQ_NONE;
926
Eric Moore635374e2009-03-09 01:21:12 -0600927 wmb();
Kashyap, Desai5b768582009-08-20 13:24:31 +0530928 writel(ioc->reply_post_host_index, &ioc->chip->ReplyPostHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600929 return IRQ_HANDLED;
930}
931
932/**
933 * mpt2sas_base_release_callback_handler - clear interupt callback handler
934 * @cb_idx: callback index
935 *
936 * Return nothing.
937 */
938void
939mpt2sas_base_release_callback_handler(u8 cb_idx)
940{
941 mpt_callbacks[cb_idx] = NULL;
942}
943
944/**
945 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
946 * @cb_func: callback function
947 *
948 * Returns cb_func.
949 */
950u8
951mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
952{
953 u8 cb_idx;
954
955 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
956 if (mpt_callbacks[cb_idx] == NULL)
957 break;
958
959 mpt_callbacks[cb_idx] = cb_func;
960 return cb_idx;
961}
962
963/**
964 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
965 *
966 * Return nothing.
967 */
968void
969mpt2sas_base_initialize_callback_handler(void)
970{
971 u8 cb_idx;
972
973 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
974 mpt2sas_base_release_callback_handler(cb_idx);
975}
976
977/**
978 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
979 * @ioc: per adapter object
980 * @paddr: virtual address for SGE
981 *
982 * Create a zero length scatter gather entry to insure the IOCs hardware has
983 * something to use if the target device goes brain dead and tries
984 * to send data even when none is asked for.
985 *
986 * Return nothing.
987 */
988void
989mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
990{
991 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
992 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
993 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
994 MPI2_SGE_FLAGS_SHIFT);
995 ioc->base_add_sg_single(paddr, flags_length, -1);
996}
997
998/**
999 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
1000 * @paddr: virtual address for SGE
1001 * @flags_length: SGE flags and data transfer length
1002 * @dma_addr: Physical address
1003 *
1004 * Return nothing.
1005 */
1006static void
1007_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1008{
1009 Mpi2SGESimple32_t *sgel = paddr;
1010
1011 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1012 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1013 sgel->FlagsLength = cpu_to_le32(flags_length);
1014 sgel->Address = cpu_to_le32(dma_addr);
1015}
1016
1017
1018/**
1019 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1020 * @paddr: virtual address for SGE
1021 * @flags_length: SGE flags and data transfer length
1022 * @dma_addr: Physical address
1023 *
1024 * Return nothing.
1025 */
1026static void
1027_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1028{
1029 Mpi2SGESimple64_t *sgel = paddr;
1030
1031 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1032 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1033 sgel->FlagsLength = cpu_to_le32(flags_length);
1034 sgel->Address = cpu_to_le64(dma_addr);
1035}
1036
1037#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1038
1039/**
1040 * _base_config_dma_addressing - set dma addressing
1041 * @ioc: per adapter object
1042 * @pdev: PCI device struct
1043 *
1044 * Returns 0 for success, non-zero for failure.
1045 */
1046static int
1047_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1048{
1049 struct sysinfo s;
1050 char *desc = NULL;
1051
1052 if (sizeof(dma_addr_t) > 4) {
1053 const uint64_t required_mask =
1054 dma_get_required_mask(&pdev->dev);
Yang Hongyange9304382009-04-13 14:40:14 -07001055 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1056 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1057 DMA_BIT_MASK(64))) {
Eric Moore635374e2009-03-09 01:21:12 -06001058 ioc->base_add_sg_single = &_base_add_sg_single_64;
1059 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1060 desc = "64";
1061 goto out;
1062 }
1063 }
1064
Yang Hongyange9304382009-04-13 14:40:14 -07001065 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1066 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eric Moore635374e2009-03-09 01:21:12 -06001067 ioc->base_add_sg_single = &_base_add_sg_single_32;
1068 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1069 desc = "32";
1070 } else
1071 return -ENODEV;
1072
1073 out:
1074 si_meminfo(&s);
1075 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1076 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1077
1078 return 0;
1079}
1080
1081/**
1082 * _base_save_msix_table - backup msix vector table
1083 * @ioc: per adapter object
1084 *
1085 * This address an errata where diag reset clears out the table
1086 */
1087static void
1088_base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
1089{
1090 int i;
1091
1092 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1093 return;
1094
1095 for (i = 0; i < ioc->msix_vector_count; i++)
1096 ioc->msix_table_backup[i] = ioc->msix_table[i];
1097}
1098
1099/**
1100 * _base_restore_msix_table - this restores the msix vector table
1101 * @ioc: per adapter object
1102 *
1103 */
1104static void
1105_base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
1106{
1107 int i;
1108
1109 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1110 return;
1111
1112 for (i = 0; i < ioc->msix_vector_count; i++)
1113 ioc->msix_table[i] = ioc->msix_table_backup[i];
1114}
1115
1116/**
1117 * _base_check_enable_msix - checks MSIX capabable.
1118 * @ioc: per adapter object
1119 *
1120 * Check to see if card is capable of MSIX, and set number
1121 * of avaliable msix vectors
1122 */
1123static int
1124_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1125{
1126 int base;
1127 u16 message_control;
1128 u32 msix_table_offset;
1129
1130 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1131 if (!base) {
1132 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1133 "supported\n", ioc->name));
1134 return -EINVAL;
1135 }
1136
1137 /* get msix vector count */
1138 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1139 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1140
1141 /* get msix table */
1142 pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
1143 msix_table_offset &= 0xFFFFFFF8;
1144 ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
1145
1146 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1147 "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
1148 ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
1149 return 0;
1150}
1151
1152/**
1153 * _base_disable_msix - disables msix
1154 * @ioc: per adapter object
1155 *
1156 */
1157static void
1158_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1159{
1160 if (ioc->msix_enable) {
1161 pci_disable_msix(ioc->pdev);
1162 kfree(ioc->msix_table_backup);
1163 ioc->msix_table_backup = NULL;
1164 ioc->msix_enable = 0;
1165 }
1166}
1167
1168/**
1169 * _base_enable_msix - enables msix, failback to io_apic
1170 * @ioc: per adapter object
1171 *
1172 */
1173static int
1174_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1175{
1176 struct msix_entry entries;
1177 int r;
1178 u8 try_msix = 0;
1179
1180 if (msix_disable == -1 || msix_disable == 0)
1181 try_msix = 1;
1182
1183 if (!try_msix)
1184 goto try_ioapic;
1185
1186 if (_base_check_enable_msix(ioc) != 0)
1187 goto try_ioapic;
1188
1189 ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
1190 sizeof(u32), GFP_KERNEL);
1191 if (!ioc->msix_table_backup) {
1192 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
1193 "msix_table_backup failed!!!\n", ioc->name));
1194 goto try_ioapic;
1195 }
1196
1197 memset(&entries, 0, sizeof(struct msix_entry));
1198 r = pci_enable_msix(ioc->pdev, &entries, 1);
1199 if (r) {
1200 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1201 "failed (r=%d) !!!\n", ioc->name, r));
1202 goto try_ioapic;
1203 }
1204
1205 r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
1206 ioc->name, ioc);
1207 if (r) {
1208 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
1209 "interrupt %d !!!\n", ioc->name, entries.vector));
1210 pci_disable_msix(ioc->pdev);
1211 goto try_ioapic;
1212 }
1213
1214 ioc->pci_irq = entries.vector;
1215 ioc->msix_enable = 1;
1216 return 0;
1217
1218/* failback to io_apic interrupt routing */
1219 try_ioapic:
1220
1221 r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
1222 ioc->name, ioc);
1223 if (r) {
1224 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1225 ioc->name, ioc->pdev->irq);
1226 r = -EBUSY;
1227 goto out_fail;
1228 }
1229
1230 ioc->pci_irq = ioc->pdev->irq;
1231 return 0;
1232
1233 out_fail:
1234 return r;
1235}
1236
1237/**
1238 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1239 * @ioc: per adapter object
1240 *
1241 * Returns 0 for success, non-zero for failure.
1242 */
1243int
1244mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1245{
1246 struct pci_dev *pdev = ioc->pdev;
1247 u32 memap_sz;
1248 u32 pio_sz;
1249 int i, r = 0;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301250 u64 pio_chip = 0;
1251 u64 chip_phys = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001252
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301253 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
Eric Moore635374e2009-03-09 01:21:12 -06001254 ioc->name, __func__));
1255
1256 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1257 if (pci_enable_device_mem(pdev)) {
1258 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1259 "failed\n", ioc->name);
1260 return -ENODEV;
1261 }
1262
1263
1264 if (pci_request_selected_regions(pdev, ioc->bars,
1265 MPT2SAS_DRIVER_NAME)) {
1266 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1267 "failed\n", ioc->name);
1268 r = -ENODEV;
1269 goto out_fail;
1270 }
1271
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301272 /* AER (Advanced Error Reporting) hooks */
1273 pci_enable_pcie_error_reporting(pdev);
1274
Eric Moore635374e2009-03-09 01:21:12 -06001275 pci_set_master(pdev);
1276
1277 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1278 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1279 ioc->name, pci_name(pdev));
1280 r = -ENODEV;
1281 goto out_fail;
1282 }
1283
1284 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
Richard A Laryfc193172010-03-12 15:27:06 -08001285 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Eric Moore635374e2009-03-09 01:21:12 -06001286 if (pio_sz)
1287 continue;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301288 pio_chip = (u64)pci_resource_start(pdev, i);
Eric Moore635374e2009-03-09 01:21:12 -06001289 pio_sz = pci_resource_len(pdev, i);
1290 } else {
1291 if (memap_sz)
1292 continue;
Richard A Laryfc193172010-03-12 15:27:06 -08001293 /* verify memory resource is valid before using */
1294 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1295 ioc->chip_phys = pci_resource_start(pdev, i);
1296 chip_phys = (u64)ioc->chip_phys;
1297 memap_sz = pci_resource_len(pdev, i);
1298 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1299 if (ioc->chip == NULL) {
1300 printk(MPT2SAS_ERR_FMT "unable to map "
1301 "adapter memory!\n", ioc->name);
1302 r = -EINVAL;
1303 goto out_fail;
1304 }
Eric Moore635374e2009-03-09 01:21:12 -06001305 }
1306 }
1307 }
1308
Eric Moore635374e2009-03-09 01:21:12 -06001309 _base_mask_interrupts(ioc);
1310 r = _base_enable_msix(ioc);
1311 if (r)
1312 goto out_fail;
1313
1314 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1315 ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1316 "IO-APIC enabled"), ioc->pci_irq);
Kashyap, Desai6846e752009-12-16 18:51:45 +05301317 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1318 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1319 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1320 ioc->name, (unsigned long long)pio_chip, pio_sz);
Eric Moore635374e2009-03-09 01:21:12 -06001321
Eric Moore3cb54692010-07-08 14:44:34 -06001322 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1323 pci_save_state(pdev);
1324
Eric Moore635374e2009-03-09 01:21:12 -06001325 return 0;
1326
1327 out_fail:
1328 if (ioc->chip_phys)
1329 iounmap(ioc->chip);
1330 ioc->chip_phys = 0;
1331 ioc->pci_irq = -1;
1332 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301333 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001334 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001335 return r;
1336}
1337
1338/**
Eric Moore635374e2009-03-09 01:21:12 -06001339 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1340 * @ioc: per adapter object
1341 * @smid: system request message index(smid zero is invalid)
1342 *
1343 * Returns virt pointer to message frame.
1344 */
1345void *
1346mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1347{
1348 return (void *)(ioc->request + (smid * ioc->request_sz));
1349}
1350
1351/**
1352 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1353 * @ioc: per adapter object
1354 * @smid: system request message index
1355 *
1356 * Returns virt pointer to sense buffer.
1357 */
1358void *
1359mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1360{
1361 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1362}
1363
1364/**
1365 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1366 * @ioc: per adapter object
1367 * @smid: system request message index
1368 *
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301369 * Returns phys pointer to the low 32bit address of the sense buffer.
Eric Moore635374e2009-03-09 01:21:12 -06001370 */
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301371__le32
Eric Moore635374e2009-03-09 01:21:12 -06001372mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1373{
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301374 return cpu_to_le32(ioc->sense_dma +
1375 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
Eric Moore635374e2009-03-09 01:21:12 -06001376}
1377
1378/**
1379 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1380 * @ioc: per adapter object
1381 * @phys_addr: lower 32 physical addr of the reply
1382 *
1383 * Converts 32bit lower physical addr into a virt address.
1384 */
1385void *
1386mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1387{
1388 if (!phys_addr)
1389 return NULL;
1390 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1391}
1392
1393/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301394 * mpt2sas_base_get_smid - obtain a free smid from internal queue
Eric Moore635374e2009-03-09 01:21:12 -06001395 * @ioc: per adapter object
1396 * @cb_idx: callback index
1397 *
1398 * Returns smid (zero is invalid)
1399 */
1400u16
1401mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1402{
1403 unsigned long flags;
1404 struct request_tracker *request;
1405 u16 smid;
1406
1407 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301408 if (list_empty(&ioc->internal_free_list)) {
1409 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1410 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1411 ioc->name, __func__);
1412 return 0;
1413 }
1414
1415 request = list_entry(ioc->internal_free_list.next,
1416 struct request_tracker, tracker_list);
1417 request->cb_idx = cb_idx;
1418 smid = request->smid;
1419 list_del(&request->tracker_list);
1420 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1421 return smid;
1422}
1423
1424/**
1425 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1426 * @ioc: per adapter object
1427 * @cb_idx: callback index
1428 * @scmd: pointer to scsi command object
1429 *
1430 * Returns smid (zero is invalid)
1431 */
1432u16
1433mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1434 struct scsi_cmnd *scmd)
1435{
1436 unsigned long flags;
1437 struct request_tracker *request;
1438 u16 smid;
1439
1440 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001441 if (list_empty(&ioc->free_list)) {
1442 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1443 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1444 ioc->name, __func__);
1445 return 0;
1446 }
1447
1448 request = list_entry(ioc->free_list.next,
1449 struct request_tracker, tracker_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301450 request->scmd = scmd;
1451 request->cb_idx = cb_idx;
1452 smid = request->smid;
1453 list_del(&request->tracker_list);
1454 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1455 return smid;
1456}
1457
1458/**
1459 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1460 * @ioc: per adapter object
1461 * @cb_idx: callback index
1462 *
1463 * Returns smid (zero is invalid)
1464 */
1465u16
1466mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1467{
1468 unsigned long flags;
1469 struct request_tracker *request;
1470 u16 smid;
1471
1472 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1473 if (list_empty(&ioc->hpr_free_list)) {
1474 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1475 return 0;
1476 }
1477
1478 request = list_entry(ioc->hpr_free_list.next,
1479 struct request_tracker, tracker_list);
Eric Moore635374e2009-03-09 01:21:12 -06001480 request->cb_idx = cb_idx;
1481 smid = request->smid;
1482 list_del(&request->tracker_list);
1483 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1484 return smid;
1485}
1486
1487
1488/**
1489 * mpt2sas_base_free_smid - put smid back on free_list
1490 * @ioc: per adapter object
1491 * @smid: system request message index
1492 *
1493 * Return nothing.
1494 */
1495void
1496mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1497{
1498 unsigned long flags;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301499 int i;
Eric Moore635374e2009-03-09 01:21:12 -06001500
1501 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301502 if (smid >= ioc->hi_priority_smid) {
1503 if (smid < ioc->internal_smid) {
1504 /* hi-priority */
1505 i = smid - ioc->hi_priority_smid;
1506 ioc->hpr_lookup[i].cb_idx = 0xFF;
1507 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1508 &ioc->hpr_free_list);
1509 } else {
1510 /* internal queue */
1511 i = smid - ioc->internal_smid;
1512 ioc->internal_lookup[i].cb_idx = 0xFF;
1513 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1514 &ioc->internal_free_list);
1515 }
1516 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1517 return;
1518 }
1519
1520 /* scsiio queue */
1521 i = smid - 1;
1522 ioc->scsi_lookup[i].cb_idx = 0xFF;
1523 ioc->scsi_lookup[i].scmd = NULL;
1524 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
Eric Moore635374e2009-03-09 01:21:12 -06001525 &ioc->free_list);
1526 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1527
1528 /*
1529 * See _wait_for_commands_to_complete() call with regards to this code.
1530 */
1531 if (ioc->shost_recovery && ioc->pending_io_count) {
1532 if (ioc->pending_io_count == 1)
1533 wake_up(&ioc->reset_wq);
1534 ioc->pending_io_count--;
1535 }
1536}
1537
1538/**
1539 * _base_writeq - 64 bit write to MMIO
1540 * @ioc: per adapter object
1541 * @b: data payload
1542 * @addr: address in MMIO space
1543 * @writeq_lock: spin lock
1544 *
1545 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1546 * care of 32 bit environment where its not quarenteed to send the entire word
1547 * in one transfer.
1548 */
1549#ifndef writeq
1550static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1551 spinlock_t *writeq_lock)
1552{
1553 unsigned long flags;
1554 __u64 data_out = cpu_to_le64(b);
1555
1556 spin_lock_irqsave(writeq_lock, flags);
1557 writel((u32)(data_out), addr);
1558 writel((u32)(data_out >> 32), (addr + 4));
1559 spin_unlock_irqrestore(writeq_lock, flags);
1560}
1561#else
1562static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1563 spinlock_t *writeq_lock)
1564{
1565 writeq(cpu_to_le64(b), addr);
1566}
1567#endif
1568
1569/**
1570 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1571 * @ioc: per adapter object
1572 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001573 * @handle: device handle
1574 *
1575 * Return nothing.
1576 */
1577void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301578mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06001579{
1580 Mpi2RequestDescriptorUnion_t descriptor;
1581 u64 *request = (u64 *)&descriptor;
1582
1583
1584 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301585 descriptor.SCSIIO.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001586 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1587 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1588 descriptor.SCSIIO.LMID = 0;
1589 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1590 &ioc->scsi_lookup_lock);
1591}
1592
1593
1594/**
1595 * mpt2sas_base_put_smid_hi_priority - send Task Managment request to firmware
1596 * @ioc: per adapter object
1597 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001598 *
1599 * Return nothing.
1600 */
1601void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301602mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001603{
1604 Mpi2RequestDescriptorUnion_t descriptor;
1605 u64 *request = (u64 *)&descriptor;
1606
1607 descriptor.HighPriority.RequestFlags =
1608 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301609 descriptor.HighPriority.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001610 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1611 descriptor.HighPriority.LMID = 0;
1612 descriptor.HighPriority.Reserved1 = 0;
1613 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1614 &ioc->scsi_lookup_lock);
1615}
1616
1617/**
1618 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1619 * @ioc: per adapter object
1620 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001621 *
1622 * Return nothing.
1623 */
1624void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301625mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001626{
1627 Mpi2RequestDescriptorUnion_t descriptor;
1628 u64 *request = (u64 *)&descriptor;
1629
1630 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301631 descriptor.Default.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001632 descriptor.Default.SMID = cpu_to_le16(smid);
1633 descriptor.Default.LMID = 0;
1634 descriptor.Default.DescriptorTypeDependent = 0;
1635 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1636 &ioc->scsi_lookup_lock);
1637}
1638
1639/**
1640 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1641 * @ioc: per adapter object
1642 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001643 * @io_index: value used to track the IO
1644 *
1645 * Return nothing.
1646 */
1647void
1648mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301649 u16 io_index)
Eric Moore635374e2009-03-09 01:21:12 -06001650{
1651 Mpi2RequestDescriptorUnion_t descriptor;
1652 u64 *request = (u64 *)&descriptor;
1653
1654 descriptor.SCSITarget.RequestFlags =
1655 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301656 descriptor.SCSITarget.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001657 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1658 descriptor.SCSITarget.LMID = 0;
1659 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1660 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1661 &ioc->scsi_lookup_lock);
1662}
1663
1664/**
Eric Mooref0f9cc12009-04-21 15:40:48 -06001665 * _base_display_dell_branding - Disply branding string
1666 * @ioc: per adapter object
1667 *
1668 * Return nothing.
1669 */
1670static void
1671_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1672{
1673 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1674
1675 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1676 return;
1677
1678 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1679 switch (ioc->pdev->subsystem_device) {
1680 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1681 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1682 MPT2SAS_DELL_BRANDING_SIZE - 1);
1683 break;
1684 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1685 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1686 MPT2SAS_DELL_BRANDING_SIZE - 1);
1687 break;
1688 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1689 strncpy(dell_branding,
1690 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1691 MPT2SAS_DELL_BRANDING_SIZE - 1);
1692 break;
1693 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1694 strncpy(dell_branding,
1695 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1696 MPT2SAS_DELL_BRANDING_SIZE - 1);
1697 break;
1698 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1699 strncpy(dell_branding,
1700 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1701 MPT2SAS_DELL_BRANDING_SIZE - 1);
1702 break;
1703 case MPT2SAS_DELL_PERC_H200_SSDID:
1704 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1705 MPT2SAS_DELL_BRANDING_SIZE - 1);
1706 break;
1707 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1708 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1709 MPT2SAS_DELL_BRANDING_SIZE - 1);
1710 break;
1711 default:
1712 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1713 break;
1714 }
1715
1716 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1717 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1718 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1719 ioc->pdev->subsystem_device);
1720}
1721
1722/**
Eric Moore635374e2009-03-09 01:21:12 -06001723 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1724 * @ioc: per adapter object
1725 *
1726 * Return nothing.
1727 */
1728static void
1729_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1730{
1731 int i = 0;
1732 char desc[16];
1733 u8 revision;
1734 u32 iounit_pg1_flags;
1735
1736 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1737 strncpy(desc, ioc->manu_pg0.ChipName, 16);
1738 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
1739 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
1740 ioc->name, desc,
1741 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
1742 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
1743 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
1744 ioc->facts.FWVersion.Word & 0x000000FF,
1745 revision,
1746 (ioc->bios_pg3.BiosVersion & 0xFF000000) >> 24,
1747 (ioc->bios_pg3.BiosVersion & 0x00FF0000) >> 16,
1748 (ioc->bios_pg3.BiosVersion & 0x0000FF00) >> 8,
1749 ioc->bios_pg3.BiosVersion & 0x000000FF);
1750
Kashyap, Desaied79f122009-08-20 13:23:49 +05301751 _base_display_dell_branding(ioc);
1752
Eric Moore635374e2009-03-09 01:21:12 -06001753 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
1754
1755 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
1756 printk("Initiator");
1757 i++;
1758 }
1759
1760 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
1761 printk("%sTarget", i ? "," : "");
1762 i++;
1763 }
1764
1765 i = 0;
1766 printk("), ");
1767 printk("Capabilities=(");
1768
1769 if (ioc->facts.IOCCapabilities &
1770 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
1771 printk("Raid");
1772 i++;
1773 }
1774
1775 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
1776 printk("%sTLR", i ? "," : "");
1777 i++;
1778 }
1779
1780 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
1781 printk("%sMulticast", i ? "," : "");
1782 i++;
1783 }
1784
1785 if (ioc->facts.IOCCapabilities &
1786 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
1787 printk("%sBIDI Target", i ? "," : "");
1788 i++;
1789 }
1790
1791 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
1792 printk("%sEEDP", i ? "," : "");
1793 i++;
1794 }
1795
1796 if (ioc->facts.IOCCapabilities &
1797 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
1798 printk("%sSnapshot Buffer", i ? "," : "");
1799 i++;
1800 }
1801
1802 if (ioc->facts.IOCCapabilities &
1803 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
1804 printk("%sDiag Trace Buffer", i ? "," : "");
1805 i++;
1806 }
1807
1808 if (ioc->facts.IOCCapabilities &
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05301809 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
1810 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
1811 i++;
1812 }
1813
1814 if (ioc->facts.IOCCapabilities &
Eric Moore635374e2009-03-09 01:21:12 -06001815 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
1816 printk("%sTask Set Full", i ? "," : "");
1817 i++;
1818 }
1819
1820 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1821 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
1822 printk("%sNCQ", i ? "," : "");
1823 i++;
1824 }
1825
1826 printk(")\n");
1827}
1828
1829/**
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05301830 * _base_update_missing_delay - change the missing delay timers
1831 * @ioc: per adapter object
1832 * @device_missing_delay: amount of time till device is reported missing
1833 * @io_missing_delay: interval IO is returned when there is a missing device
1834 *
1835 * Return nothing.
1836 *
1837 * Passed on the command line, this function will modify the device missing
1838 * delay, as well as the io missing delay. This should be called at driver
1839 * load time.
1840 */
1841static void
1842_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
1843 u16 device_missing_delay, u8 io_missing_delay)
1844{
1845 u16 dmd, dmd_new, dmd_orignal;
1846 u8 io_missing_delay_original;
1847 u16 sz;
1848 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1849 Mpi2ConfigReply_t mpi_reply;
1850 u8 num_phys = 0;
1851 u16 ioc_status;
1852
1853 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
1854 if (!num_phys)
1855 return;
1856
1857 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
1858 sizeof(Mpi2SasIOUnit1PhyData_t));
1859 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1860 if (!sas_iounit_pg1) {
1861 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1862 ioc->name, __FILE__, __LINE__, __func__);
1863 goto out;
1864 }
1865 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1866 sas_iounit_pg1, sz))) {
1867 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1868 ioc->name, __FILE__, __LINE__, __func__);
1869 goto out;
1870 }
1871 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1872 MPI2_IOCSTATUS_MASK;
1873 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1874 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1875 ioc->name, __FILE__, __LINE__, __func__);
1876 goto out;
1877 }
1878
1879 /* device missing delay */
1880 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
1881 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
1882 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
1883 else
1884 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1885 dmd_orignal = dmd;
1886 if (device_missing_delay > 0x7F) {
1887 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
1888 device_missing_delay;
1889 dmd = dmd / 16;
1890 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
1891 } else
1892 dmd = device_missing_delay;
1893 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
1894
1895 /* io missing delay */
1896 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
1897 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
1898
1899 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1900 sz)) {
1901 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
1902 dmd_new = (dmd &
1903 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
1904 else
1905 dmd_new =
1906 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1907 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
1908 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
1909 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
1910 "new(%d)\n", ioc->name, io_missing_delay_original,
1911 io_missing_delay);
1912 ioc->device_missing_delay = dmd_new;
1913 ioc->io_missing_delay = io_missing_delay;
1914 }
1915
1916out:
1917 kfree(sas_iounit_pg1);
1918}
1919
1920/**
Eric Moore635374e2009-03-09 01:21:12 -06001921 * _base_static_config_pages - static start of day config pages
1922 * @ioc: per adapter object
1923 *
1924 * Return nothing.
1925 */
1926static void
1927_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
1928{
1929 Mpi2ConfigReply_t mpi_reply;
1930 u32 iounit_pg1_flags;
1931
1932 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
Kashyap, Desaied79f122009-08-20 13:23:49 +05301933 if (ioc->ir_firmware)
1934 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
1935 &ioc->manu_pg10);
Eric Moore635374e2009-03-09 01:21:12 -06001936 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
1937 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
1938 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
1939 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
1940 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
1941 _base_display_ioc_capabilities(ioc);
1942
1943 /*
1944 * Enable task_set_full handling in iounit_pg1 when the
1945 * facts capabilities indicate that its supported.
1946 */
1947 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1948 if ((ioc->facts.IOCCapabilities &
1949 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
1950 iounit_pg1_flags &=
1951 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1952 else
1953 iounit_pg1_flags |=
1954 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1955 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
Kashyap, Desai5b768582009-08-20 13:24:31 +05301956 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05301957
Eric Moore635374e2009-03-09 01:21:12 -06001958}
1959
1960/**
1961 * _base_release_memory_pools - release memory
1962 * @ioc: per adapter object
1963 *
1964 * Free memory allocated from _base_allocate_memory_pools.
1965 *
1966 * Return nothing.
1967 */
1968static void
1969_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
1970{
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301971 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06001972 __func__));
1973
1974 if (ioc->request) {
1975 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
1976 ioc->request, ioc->request_dma);
1977 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
1978 ": free\n", ioc->name, ioc->request));
1979 ioc->request = NULL;
1980 }
1981
1982 if (ioc->sense) {
1983 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
1984 if (ioc->sense_dma_pool)
1985 pci_pool_destroy(ioc->sense_dma_pool);
1986 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
1987 ": free\n", ioc->name, ioc->sense));
1988 ioc->sense = NULL;
1989 }
1990
1991 if (ioc->reply) {
1992 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
1993 if (ioc->reply_dma_pool)
1994 pci_pool_destroy(ioc->reply_dma_pool);
1995 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
1996 ": free\n", ioc->name, ioc->reply));
1997 ioc->reply = NULL;
1998 }
1999
2000 if (ioc->reply_free) {
2001 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2002 ioc->reply_free_dma);
2003 if (ioc->reply_free_dma_pool)
2004 pci_pool_destroy(ioc->reply_free_dma_pool);
2005 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2006 "(0x%p): free\n", ioc->name, ioc->reply_free));
2007 ioc->reply_free = NULL;
2008 }
2009
2010 if (ioc->reply_post_free) {
2011 pci_pool_free(ioc->reply_post_free_dma_pool,
2012 ioc->reply_post_free, ioc->reply_post_free_dma);
2013 if (ioc->reply_post_free_dma_pool)
2014 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2015 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2016 "reply_post_free_pool(0x%p): free\n", ioc->name,
2017 ioc->reply_post_free));
2018 ioc->reply_post_free = NULL;
2019 }
2020
2021 if (ioc->config_page) {
2022 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2023 "config_page(0x%p): free\n", ioc->name,
2024 ioc->config_page));
2025 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2026 ioc->config_page, ioc->config_page_dma);
2027 }
2028
Kashyap, Desai66a67932010-04-05 14:21:07 +05302029 if (ioc->scsi_lookup) {
2030 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2031 ioc->scsi_lookup = NULL;
2032 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302033 kfree(ioc->hpr_lookup);
2034 kfree(ioc->internal_lookup);
Eric Moore635374e2009-03-09 01:21:12 -06002035}
2036
2037
2038/**
2039 * _base_allocate_memory_pools - allocate start of day memory pools
2040 * @ioc: per adapter object
2041 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2042 *
2043 * Returns 0 success, anything else error
2044 */
2045static int
2046_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2047{
2048 Mpi2IOCFactsReply_t *facts;
2049 u32 queue_size, queue_diff;
2050 u16 max_sge_elements;
2051 u16 num_of_reply_frames;
2052 u16 chains_needed_per_io;
2053 u32 sz, total_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002054 u32 retry_sz;
2055 u16 max_request_credit;
2056
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302057 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002058 __func__));
2059
2060 retry_sz = 0;
2061 facts = &ioc->facts;
2062
2063 /* command line tunables for max sgl entries */
2064 if (max_sgl_entries != -1) {
2065 ioc->shost->sg_tablesize = (max_sgl_entries <
2066 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2067 MPT2SAS_SG_DEPTH;
2068 } else {
2069 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2070 }
2071
2072 /* command line tunables for max controller queue depth */
2073 if (max_queue_depth != -1) {
2074 max_request_credit = (max_queue_depth < facts->RequestCredit)
2075 ? max_queue_depth : facts->RequestCredit;
2076 } else {
2077 max_request_credit = (facts->RequestCredit >
2078 MPT2SAS_MAX_REQUEST_QUEUE) ? MPT2SAS_MAX_REQUEST_QUEUE :
2079 facts->RequestCredit;
2080 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302081
2082 ioc->hba_queue_depth = max_request_credit;
2083 ioc->hi_priority_depth = facts->HighPriorityCredit;
2084 ioc->internal_depth = ioc->hi_priority_depth + 5;
Eric Moore635374e2009-03-09 01:21:12 -06002085
2086 /* request frame size */
2087 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2088
2089 /* reply frame size */
2090 ioc->reply_sz = facts->ReplyFrameSize * 4;
2091
2092 retry_allocation:
2093 total_sz = 0;
2094 /* calculate number of sg elements left over in the 1st frame */
2095 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2096 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2097 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2098
2099 /* now do the same for a chain buffer */
2100 max_sge_elements = ioc->request_sz - ioc->sge_size;
2101 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2102
2103 ioc->chain_offset_value_for_main_message =
2104 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2105 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2106
2107 /*
2108 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2109 */
2110 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2111 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2112 + 1;
2113 if (chains_needed_per_io > facts->MaxChainDepth) {
2114 chains_needed_per_io = facts->MaxChainDepth;
2115 ioc->shost->sg_tablesize = min_t(u16,
2116 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2117 * chains_needed_per_io), ioc->shost->sg_tablesize);
2118 }
2119 ioc->chains_needed_per_io = chains_needed_per_io;
2120
2121 /* reply free queue sizing - taking into account for events */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302122 num_of_reply_frames = ioc->hba_queue_depth + 32;
Eric Moore635374e2009-03-09 01:21:12 -06002123
2124 /* number of replies frames can't be a multiple of 16 */
2125 /* decrease number of reply frames by 1 */
2126 if (!(num_of_reply_frames % 16))
2127 num_of_reply_frames--;
2128
2129 /* calculate number of reply free queue entries
2130 * (must be multiple of 16)
2131 */
2132
2133 /* (we know reply_free_queue_depth is not a multiple of 16) */
2134 queue_size = num_of_reply_frames;
2135 queue_size += 16 - (queue_size % 16);
2136 ioc->reply_free_queue_depth = queue_size;
2137
2138 /* reply descriptor post queue sizing */
2139 /* this size should be the number of request frames + number of reply
2140 * frames
2141 */
2142
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302143 queue_size = ioc->hba_queue_depth + num_of_reply_frames + 1;
Eric Moore635374e2009-03-09 01:21:12 -06002144 /* round up to 16 byte boundary */
2145 if (queue_size % 16)
2146 queue_size += 16 - (queue_size % 16);
2147
2148 /* check against IOC maximum reply post queue depth */
2149 if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
2150 queue_diff = queue_size -
2151 facts->MaxReplyDescriptorPostQueueDepth;
2152
2153 /* round queue_diff up to multiple of 16 */
2154 if (queue_diff % 16)
2155 queue_diff += 16 - (queue_diff % 16);
2156
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302157 /* adjust hba_queue_depth, reply_free_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002158 * and queue_size
2159 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302160 ioc->hba_queue_depth -= queue_diff;
Eric Moore635374e2009-03-09 01:21:12 -06002161 ioc->reply_free_queue_depth -= queue_diff;
2162 queue_size -= queue_diff;
2163 }
2164 ioc->reply_post_queue_depth = queue_size;
2165
Eric Moore635374e2009-03-09 01:21:12 -06002166 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2167 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2168 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2169 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2170 ioc->chains_needed_per_io));
2171
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302172 ioc->scsiio_depth = ioc->hba_queue_depth -
2173 ioc->hi_priority_depth - ioc->internal_depth;
2174
2175 /* set the scsi host can_queue depth
2176 * with some internal commands that could be outstanding
2177 */
2178 ioc->shost->can_queue = ioc->scsiio_depth - (2);
2179 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2180 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2181
Eric Moore635374e2009-03-09 01:21:12 -06002182 /* contiguous pool for request and chains, 16 byte align, one extra "
2183 * "frame for smid=0
2184 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302185 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2186 sz = ((ioc->scsiio_depth + 1 + ioc->chain_depth) * ioc->request_sz);
2187
2188 /* hi-priority queue */
2189 sz += (ioc->hi_priority_depth * ioc->request_sz);
2190
2191 /* internal queue */
2192 sz += (ioc->internal_depth * ioc->request_sz);
Eric Moore635374e2009-03-09 01:21:12 -06002193
2194 ioc->request_dma_sz = sz;
2195 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2196 if (!ioc->request) {
2197 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302198 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2199 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002200 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302201 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
Eric Moore635374e2009-03-09 01:21:12 -06002202 goto out;
2203 retry_sz += 64;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302204 ioc->hba_queue_depth = max_request_credit - retry_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002205 goto retry_allocation;
2206 }
2207
2208 if (retry_sz)
2209 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302210 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2211 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002212 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2213
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302214
2215 /* hi-priority queue */
2216 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002217 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302218 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002219 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302220
2221 /* internal queue */
2222 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2223 ioc->request_sz);
2224 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2225 ioc->request_sz);
2226
2227 ioc->chain = ioc->internal + (ioc->internal_depth *
2228 ioc->request_sz);
2229 ioc->chain_dma = ioc->internal_dma + (ioc->internal_depth *
2230 ioc->request_sz);
2231
Eric Moore635374e2009-03-09 01:21:12 -06002232 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2233 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302234 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2235 (ioc->hba_queue_depth * ioc->request_sz)/1024));
Eric Moore635374e2009-03-09 01:21:12 -06002236 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool(0x%p): depth"
2237 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->chain,
2238 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2239 ioc->request_sz))/1024));
2240 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2241 ioc->name, (unsigned long long) ioc->request_dma));
2242 total_sz += sz;
2243
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302244 sz = ioc->scsiio_depth * sizeof(struct request_tracker);
2245 ioc->scsi_lookup_pages = get_order(sz);
2246 ioc->scsi_lookup = (struct request_tracker *)__get_free_pages(
2247 GFP_KERNEL, ioc->scsi_lookup_pages);
Eric Moore635374e2009-03-09 01:21:12 -06002248 if (!ioc->scsi_lookup) {
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302249 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2250 "sz(%d)\n", ioc->name, (int)sz);
Eric Moore635374e2009-03-09 01:21:12 -06002251 goto out;
2252 }
2253
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302254 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2255 "depth(%d)\n", ioc->name, ioc->request,
2256 ioc->scsiio_depth));
2257
2258 /* initialize hi-priority queue smid's */
2259 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2260 sizeof(struct request_tracker), GFP_KERNEL);
2261 if (!ioc->hpr_lookup) {
2262 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2263 ioc->name);
2264 goto out;
2265 }
2266 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2267 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2268 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2269 ioc->hi_priority_depth, ioc->hi_priority_smid));
2270
2271 /* initialize internal queue smid's */
2272 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2273 sizeof(struct request_tracker), GFP_KERNEL);
2274 if (!ioc->internal_lookup) {
2275 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2276 ioc->name);
2277 goto out;
2278 }
2279 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2280 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2281 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2282 ioc->internal_depth, ioc->internal_smid));
Eric Moore635374e2009-03-09 01:21:12 -06002283
2284 /* sense buffers, 4 byte align */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302285 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Eric Moore635374e2009-03-09 01:21:12 -06002286 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2287 0);
2288 if (!ioc->sense_dma_pool) {
2289 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2290 ioc->name);
2291 goto out;
2292 }
2293 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2294 &ioc->sense_dma);
2295 if (!ioc->sense) {
2296 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2297 ioc->name);
2298 goto out;
2299 }
2300 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2301 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302302 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002303 SCSI_SENSE_BUFFERSIZE, sz/1024));
2304 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2305 ioc->name, (unsigned long long)ioc->sense_dma));
2306 total_sz += sz;
2307
2308 /* reply pool, 4 byte align */
2309 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2310 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2311 0);
2312 if (!ioc->reply_dma_pool) {
2313 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2314 ioc->name);
2315 goto out;
2316 }
2317 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2318 &ioc->reply_dma);
2319 if (!ioc->reply) {
2320 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2321 ioc->name);
2322 goto out;
2323 }
Kashyap, Desaidd3741d2010-11-13 04:31:14 +05302324 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2325 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
Eric Moore635374e2009-03-09 01:21:12 -06002326 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2327 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2328 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2329 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2330 ioc->name, (unsigned long long)ioc->reply_dma));
2331 total_sz += sz;
2332
2333 /* reply free queue, 16 byte align */
2334 sz = ioc->reply_free_queue_depth * 4;
2335 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2336 ioc->pdev, sz, 16, 0);
2337 if (!ioc->reply_free_dma_pool) {
2338 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2339 "failed\n", ioc->name);
2340 goto out;
2341 }
2342 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2343 &ioc->reply_free_dma);
2344 if (!ioc->reply_free) {
2345 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2346 "failed\n", ioc->name);
2347 goto out;
2348 }
2349 memset(ioc->reply_free, 0, sz);
2350 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2351 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2352 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2353 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2354 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2355 total_sz += sz;
2356
2357 /* reply post queue, 16 byte align */
2358 sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
2359 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2360 ioc->pdev, sz, 16, 0);
2361 if (!ioc->reply_post_free_dma_pool) {
2362 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2363 "failed\n", ioc->name);
2364 goto out;
2365 }
2366 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2367 GFP_KERNEL, &ioc->reply_post_free_dma);
2368 if (!ioc->reply_post_free) {
2369 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2370 "failed\n", ioc->name);
2371 goto out;
2372 }
2373 memset(ioc->reply_post_free, 0, sz);
2374 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2375 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2376 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2377 sz/1024));
2378 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2379 "(0x%llx)\n", ioc->name, (unsigned long long)
2380 ioc->reply_post_free_dma));
2381 total_sz += sz;
2382
2383 ioc->config_page_sz = 512;
2384 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2385 ioc->config_page_sz, &ioc->config_page_dma);
2386 if (!ioc->config_page) {
2387 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2388 "failed\n", ioc->name);
2389 goto out;
2390 }
2391 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2392 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2393 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2394 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2395 total_sz += ioc->config_page_sz;
2396
2397 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2398 ioc->name, total_sz/1024);
2399 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2400 "Max Controller Queue Depth(%d)\n",
2401 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2402 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2403 ioc->name, ioc->shost->sg_tablesize);
2404 return 0;
2405
2406 out:
2407 _base_release_memory_pools(ioc);
2408 return -ENOMEM;
2409}
2410
2411
2412/**
2413 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2414 * @ioc: Pointer to MPT_ADAPTER structure
2415 * @cooked: Request raw or cooked IOC state
2416 *
2417 * Returns all IOC Doorbell register bits if cooked==0, else just the
2418 * Doorbell bits in MPI_IOC_STATE_MASK.
2419 */
2420u32
2421mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2422{
2423 u32 s, sc;
2424
2425 s = readl(&ioc->chip->Doorbell);
2426 sc = s & MPI2_IOC_STATE_MASK;
2427 return cooked ? sc : s;
2428}
2429
2430/**
2431 * _base_wait_on_iocstate - waiting on a particular ioc state
2432 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2433 * @timeout: timeout in second
2434 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2435 *
2436 * Returns 0 for success, non-zero for failure.
2437 */
2438static int
2439_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2440 int sleep_flag)
2441{
2442 u32 count, cntdn;
2443 u32 current_state;
2444
2445 count = 0;
2446 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2447 do {
2448 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2449 if (current_state == ioc_state)
2450 return 0;
2451 if (count && current_state == MPI2_IOC_STATE_FAULT)
2452 break;
2453 if (sleep_flag == CAN_SLEEP)
2454 msleep(1);
2455 else
2456 udelay(500);
2457 count++;
2458 } while (--cntdn);
2459
2460 return current_state;
2461}
2462
2463/**
2464 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2465 * a write to the doorbell)
2466 * @ioc: per adapter object
2467 * @timeout: timeout in second
2468 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2469 *
2470 * Returns 0 for success, non-zero for failure.
2471 *
2472 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2473 */
2474static int
2475_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2476 int sleep_flag)
2477{
2478 u32 cntdn, count;
2479 u32 int_status;
2480
2481 count = 0;
2482 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2483 do {
2484 int_status = readl(&ioc->chip->HostInterruptStatus);
2485 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302486 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002487 "successfull count(%d), timeout(%d)\n", ioc->name,
2488 __func__, count, timeout));
2489 return 0;
2490 }
2491 if (sleep_flag == CAN_SLEEP)
2492 msleep(1);
2493 else
2494 udelay(500);
2495 count++;
2496 } while (--cntdn);
2497
2498 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2499 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2500 return -EFAULT;
2501}
2502
2503/**
2504 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2505 * @ioc: per adapter object
2506 * @timeout: timeout in second
2507 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2508 *
2509 * Returns 0 for success, non-zero for failure.
2510 *
2511 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2512 * doorbell.
2513 */
2514static int
2515_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2516 int sleep_flag)
2517{
2518 u32 cntdn, count;
2519 u32 int_status;
2520 u32 doorbell;
2521
2522 count = 0;
2523 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2524 do {
2525 int_status = readl(&ioc->chip->HostInterruptStatus);
2526 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302527 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002528 "successfull count(%d), timeout(%d)\n", ioc->name,
2529 __func__, count, timeout));
2530 return 0;
2531 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2532 doorbell = readl(&ioc->chip->Doorbell);
2533 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2534 MPI2_IOC_STATE_FAULT) {
2535 mpt2sas_base_fault_info(ioc , doorbell);
2536 return -EFAULT;
2537 }
2538 } else if (int_status == 0xFFFFFFFF)
2539 goto out;
2540
2541 if (sleep_flag == CAN_SLEEP)
2542 msleep(1);
2543 else
2544 udelay(500);
2545 count++;
2546 } while (--cntdn);
2547
2548 out:
2549 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2550 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2551 return -EFAULT;
2552}
2553
2554/**
2555 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2556 * @ioc: per adapter object
2557 * @timeout: timeout in second
2558 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2559 *
2560 * Returns 0 for success, non-zero for failure.
2561 *
2562 */
2563static int
2564_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2565 int sleep_flag)
2566{
2567 u32 cntdn, count;
2568 u32 doorbell_reg;
2569
2570 count = 0;
2571 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2572 do {
2573 doorbell_reg = readl(&ioc->chip->Doorbell);
2574 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302575 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002576 "successfull count(%d), timeout(%d)\n", ioc->name,
2577 __func__, count, timeout));
2578 return 0;
2579 }
2580 if (sleep_flag == CAN_SLEEP)
2581 msleep(1);
2582 else
2583 udelay(500);
2584 count++;
2585 } while (--cntdn);
2586
2587 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2588 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2589 return -EFAULT;
2590}
2591
2592/**
2593 * _base_send_ioc_reset - send doorbell reset
2594 * @ioc: per adapter object
2595 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2596 * @timeout: timeout in second
2597 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2598 *
2599 * Returns 0 for success, non-zero for failure.
2600 */
2601static int
2602_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2603 int sleep_flag)
2604{
2605 u32 ioc_state;
2606 int r = 0;
2607
2608 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2609 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2610 ioc->name, __func__);
2611 return -EFAULT;
2612 }
2613
2614 if (!(ioc->facts.IOCCapabilities &
2615 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2616 return -EFAULT;
2617
2618 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2619
2620 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2621 &ioc->chip->Doorbell);
2622 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2623 r = -EFAULT;
2624 goto out;
2625 }
2626 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2627 timeout, sleep_flag);
2628 if (ioc_state) {
2629 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2630 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2631 r = -EFAULT;
2632 goto out;
2633 }
2634 out:
2635 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2636 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2637 return r;
2638}
2639
2640/**
2641 * _base_handshake_req_reply_wait - send request thru doorbell interface
2642 * @ioc: per adapter object
2643 * @request_bytes: request length
2644 * @request: pointer having request payload
2645 * @reply_bytes: reply length
2646 * @reply: pointer to reply payload
2647 * @timeout: timeout in second
2648 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2649 *
2650 * Returns 0 for success, non-zero for failure.
2651 */
2652static int
2653_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2654 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2655{
2656 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2657 int i;
2658 u8 failed;
2659 u16 dummy;
2660 u32 *mfp;
2661
2662 /* make sure doorbell is not in use */
2663 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2664 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2665 " (line=%d)\n", ioc->name, __LINE__);
2666 return -EFAULT;
2667 }
2668
2669 /* clear pending doorbell interrupts from previous state changes */
2670 if (readl(&ioc->chip->HostInterruptStatus) &
2671 MPI2_HIS_IOC2SYS_DB_STATUS)
2672 writel(0, &ioc->chip->HostInterruptStatus);
2673
2674 /* send message to ioc */
2675 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2676 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2677 &ioc->chip->Doorbell);
2678
Kashyap, Desai29786e12009-09-14 11:06:21 +05302679 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
Eric Moore635374e2009-03-09 01:21:12 -06002680 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2681 "int failed (line=%d)\n", ioc->name, __LINE__);
2682 return -EFAULT;
2683 }
2684 writel(0, &ioc->chip->HostInterruptStatus);
2685
2686 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2687 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2688 "ack failed (line=%d)\n", ioc->name, __LINE__);
2689 return -EFAULT;
2690 }
2691
2692 /* send message 32-bits at a time */
2693 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2694 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2695 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2696 failed = 1;
2697 }
2698
2699 if (failed) {
2700 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2701 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2702 return -EFAULT;
2703 }
2704
2705 /* now wait for the reply */
2706 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2707 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2708 "int failed (line=%d)\n", ioc->name, __LINE__);
2709 return -EFAULT;
2710 }
2711
2712 /* read the first two 16-bits, it gives the total length of the reply */
2713 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2714 & MPI2_DOORBELL_DATA_MASK);
2715 writel(0, &ioc->chip->HostInterruptStatus);
2716 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2717 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2718 "int failed (line=%d)\n", ioc->name, __LINE__);
2719 return -EFAULT;
2720 }
2721 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2722 & MPI2_DOORBELL_DATA_MASK);
2723 writel(0, &ioc->chip->HostInterruptStatus);
2724
2725 for (i = 2; i < default_reply->MsgLength * 2; i++) {
2726 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2727 printk(MPT2SAS_ERR_FMT "doorbell "
2728 "handshake int failed (line=%d)\n", ioc->name,
2729 __LINE__);
2730 return -EFAULT;
2731 }
2732 if (i >= reply_bytes/2) /* overflow case */
2733 dummy = readl(&ioc->chip->Doorbell);
2734 else
2735 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2736 & MPI2_DOORBELL_DATA_MASK);
2737 writel(0, &ioc->chip->HostInterruptStatus);
2738 }
2739
2740 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
2741 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
2742 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
2743 " (line=%d)\n", ioc->name, __LINE__));
2744 }
2745 writel(0, &ioc->chip->HostInterruptStatus);
2746
2747 if (ioc->logging_level & MPT_DEBUG_INIT) {
2748 mfp = (u32 *)reply;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302749 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06002750 for (i = 0; i < reply_bytes/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302751 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06002752 le32_to_cpu(mfp[i]));
2753 }
2754 return 0;
2755}
2756
2757/**
2758 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
2759 * @ioc: per adapter object
2760 * @mpi_reply: the reply payload from FW
2761 * @mpi_request: the request payload sent to FW
2762 *
2763 * The SAS IO Unit Control Request message allows the host to perform low-level
2764 * operations, such as resets on the PHYs of the IO Unit, also allows the host
2765 * to obtain the IOC assigned device handles for a device if it has other
2766 * identifying information about the device, in addition allows the host to
2767 * remove IOC resources associated with the device.
2768 *
2769 * Returns 0 for success, non-zero for failure.
2770 */
2771int
2772mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
2773 Mpi2SasIoUnitControlReply_t *mpi_reply,
2774 Mpi2SasIoUnitControlRequest_t *mpi_request)
2775{
2776 u16 smid;
2777 u32 ioc_state;
2778 unsigned long timeleft;
2779 u8 issue_reset;
2780 int rc;
2781 void *request;
2782 u16 wait_state_count;
2783
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302784 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002785 __func__));
2786
2787 mutex_lock(&ioc->base_cmds.mutex);
2788
2789 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2790 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2791 ioc->name, __func__);
2792 rc = -EAGAIN;
2793 goto out;
2794 }
2795
2796 wait_state_count = 0;
2797 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2798 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2799 if (wait_state_count++ == 10) {
2800 printk(MPT2SAS_ERR_FMT
2801 "%s: failed due to ioc not operational\n",
2802 ioc->name, __func__);
2803 rc = -EFAULT;
2804 goto out;
2805 }
2806 ssleep(1);
2807 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2808 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2809 "operational state(count=%d)\n", ioc->name,
2810 __func__, wait_state_count);
2811 }
2812
2813 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2814 if (!smid) {
2815 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2816 ioc->name, __func__);
2817 rc = -EAGAIN;
2818 goto out;
2819 }
2820
2821 rc = 0;
2822 ioc->base_cmds.status = MPT2_CMD_PENDING;
2823 request = mpt2sas_base_get_msg_frame(ioc, smid);
2824 ioc->base_cmds.smid = smid;
2825 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
2826 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2827 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
2828 ioc->ioc_link_reset_in_progress = 1;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302829 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302830 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06002831 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2832 msecs_to_jiffies(10000));
2833 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2834 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
2835 ioc->ioc_link_reset_in_progress)
2836 ioc->ioc_link_reset_in_progress = 0;
2837 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2838 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2839 ioc->name, __func__);
2840 _debug_dump_mf(mpi_request,
2841 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
2842 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2843 issue_reset = 1;
2844 goto issue_host_reset;
2845 }
2846 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2847 memcpy(mpi_reply, ioc->base_cmds.reply,
2848 sizeof(Mpi2SasIoUnitControlReply_t));
2849 else
2850 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
2851 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2852 goto out;
2853
2854 issue_host_reset:
2855 if (issue_reset)
2856 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2857 FORCE_BIG_HAMMER);
2858 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2859 rc = -EFAULT;
2860 out:
2861 mutex_unlock(&ioc->base_cmds.mutex);
2862 return rc;
2863}
2864
2865
2866/**
2867 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
2868 * @ioc: per adapter object
2869 * @mpi_reply: the reply payload from FW
2870 * @mpi_request: the request payload sent to FW
2871 *
2872 * The SCSI Enclosure Processor request message causes the IOC to
2873 * communicate with SES devices to control LED status signals.
2874 *
2875 * Returns 0 for success, non-zero for failure.
2876 */
2877int
2878mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
2879 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
2880{
2881 u16 smid;
2882 u32 ioc_state;
2883 unsigned long timeleft;
2884 u8 issue_reset;
2885 int rc;
2886 void *request;
2887 u16 wait_state_count;
2888
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302889 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002890 __func__));
2891
2892 mutex_lock(&ioc->base_cmds.mutex);
2893
2894 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2895 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2896 ioc->name, __func__);
2897 rc = -EAGAIN;
2898 goto out;
2899 }
2900
2901 wait_state_count = 0;
2902 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2903 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2904 if (wait_state_count++ == 10) {
2905 printk(MPT2SAS_ERR_FMT
2906 "%s: failed due to ioc not operational\n",
2907 ioc->name, __func__);
2908 rc = -EFAULT;
2909 goto out;
2910 }
2911 ssleep(1);
2912 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2913 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2914 "operational state(count=%d)\n", ioc->name,
2915 __func__, wait_state_count);
2916 }
2917
2918 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2919 if (!smid) {
2920 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2921 ioc->name, __func__);
2922 rc = -EAGAIN;
2923 goto out;
2924 }
2925
2926 rc = 0;
2927 ioc->base_cmds.status = MPT2_CMD_PENDING;
2928 request = mpt2sas_base_get_msg_frame(ioc, smid);
2929 ioc->base_cmds.smid = smid;
2930 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302931 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302932 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06002933 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2934 msecs_to_jiffies(10000));
2935 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2936 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2937 ioc->name, __func__);
2938 _debug_dump_mf(mpi_request,
2939 sizeof(Mpi2SepRequest_t)/4);
2940 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2941 issue_reset = 1;
2942 goto issue_host_reset;
2943 }
2944 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2945 memcpy(mpi_reply, ioc->base_cmds.reply,
2946 sizeof(Mpi2SepReply_t));
2947 else
2948 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
2949 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2950 goto out;
2951
2952 issue_host_reset:
2953 if (issue_reset)
2954 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2955 FORCE_BIG_HAMMER);
2956 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2957 rc = -EFAULT;
2958 out:
2959 mutex_unlock(&ioc->base_cmds.mutex);
2960 return rc;
2961}
2962
2963/**
2964 * _base_get_port_facts - obtain port facts reply and save in ioc
2965 * @ioc: per adapter object
2966 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2967 *
2968 * Returns 0 for success, non-zero for failure.
2969 */
2970static int
2971_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
2972{
2973 Mpi2PortFactsRequest_t mpi_request;
2974 Mpi2PortFactsReply_t mpi_reply, *pfacts;
2975 int mpi_reply_sz, mpi_request_sz, r;
2976
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302977 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002978 __func__));
2979
2980 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
2981 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
2982 memset(&mpi_request, 0, mpi_request_sz);
2983 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
2984 mpi_request.PortNumber = port;
2985 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2986 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2987
2988 if (r != 0) {
2989 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2990 ioc->name, __func__, r);
2991 return r;
2992 }
2993
2994 pfacts = &ioc->pfacts[port];
2995 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
2996 pfacts->PortNumber = mpi_reply.PortNumber;
2997 pfacts->VP_ID = mpi_reply.VP_ID;
2998 pfacts->VF_ID = mpi_reply.VF_ID;
2999 pfacts->MaxPostedCmdBuffers =
3000 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3001
3002 return 0;
3003}
3004
3005/**
3006 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3007 * @ioc: per adapter object
3008 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3009 *
3010 * Returns 0 for success, non-zero for failure.
3011 */
3012static int
3013_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3014{
3015 Mpi2IOCFactsRequest_t mpi_request;
3016 Mpi2IOCFactsReply_t mpi_reply, *facts;
3017 int mpi_reply_sz, mpi_request_sz, r;
3018
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303019 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003020 __func__));
3021
3022 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3023 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3024 memset(&mpi_request, 0, mpi_request_sz);
3025 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3026 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3027 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3028
3029 if (r != 0) {
3030 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3031 ioc->name, __func__, r);
3032 return r;
3033 }
3034
3035 facts = &ioc->facts;
3036 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
3037 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3038 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3039 facts->VP_ID = mpi_reply.VP_ID;
3040 facts->VF_ID = mpi_reply.VF_ID;
3041 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3042 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3043 facts->WhoInit = mpi_reply.WhoInit;
3044 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3045 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3046 facts->MaxReplyDescriptorPostQueueDepth =
3047 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3048 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3049 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3050 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3051 ioc->ir_firmware = 1;
3052 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3053 facts->IOCRequestFrameSize =
3054 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3055 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3056 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3057 ioc->shost->max_id = -1;
3058 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3059 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3060 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3061 facts->HighPriorityCredit =
3062 le16_to_cpu(mpi_reply.HighPriorityCredit);
3063 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3064 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3065
3066 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3067 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3068 facts->MaxChainDepth));
3069 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3070 "reply frame size(%d)\n", ioc->name,
3071 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3072 return 0;
3073}
3074
3075/**
3076 * _base_send_ioc_init - send ioc_init to firmware
3077 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003078 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3079 *
3080 * Returns 0 for success, non-zero for failure.
3081 */
3082static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303083_base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003084{
3085 Mpi2IOCInitRequest_t mpi_request;
3086 Mpi2IOCInitReply_t mpi_reply;
3087 int r;
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303088 struct timeval current_time;
Kashyap, Desai463217b2009-10-05 15:53:06 +05303089 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003090
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303091 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003092 __func__));
3093
3094 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3095 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3096 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303097 mpi_request.VF_ID = 0; /* TODO */
3098 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003099 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3100 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3101
3102 /* In MPI Revision I (0xA), the SystemReplyFrameSize(offset 0x18) was
3103 * removed and made reserved. For those with older firmware will need
3104 * this fix. It was decided that the Reply and Request frame sizes are
3105 * the same.
3106 */
3107 if ((ioc->facts.HeaderVersion >> 8) < 0xA) {
3108 mpi_request.Reserved7 = cpu_to_le16(ioc->reply_sz);
3109/* mpi_request.SystemReplyFrameSize =
3110 * cpu_to_le16(ioc->reply_sz);
3111 */
3112 }
3113
3114 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3115 mpi_request.ReplyDescriptorPostQueueDepth =
3116 cpu_to_le16(ioc->reply_post_queue_depth);
3117 mpi_request.ReplyFreeQueueDepth =
3118 cpu_to_le16(ioc->reply_free_queue_depth);
3119
3120#if BITS_PER_LONG > 32
3121 mpi_request.SenseBufferAddressHigh =
3122 cpu_to_le32(ioc->sense_dma >> 32);
3123 mpi_request.SystemReplyAddressHigh =
3124 cpu_to_le32(ioc->reply_dma >> 32);
3125 mpi_request.SystemRequestFrameBaseAddress =
3126 cpu_to_le64(ioc->request_dma);
3127 mpi_request.ReplyFreeQueueAddress =
3128 cpu_to_le64(ioc->reply_free_dma);
3129 mpi_request.ReplyDescriptorPostQueueAddress =
3130 cpu_to_le64(ioc->reply_post_free_dma);
3131#else
3132 mpi_request.SystemRequestFrameBaseAddress =
3133 cpu_to_le32(ioc->request_dma);
3134 mpi_request.ReplyFreeQueueAddress =
3135 cpu_to_le32(ioc->reply_free_dma);
3136 mpi_request.ReplyDescriptorPostQueueAddress =
3137 cpu_to_le32(ioc->reply_post_free_dma);
3138#endif
3139
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303140 /* This time stamp specifies number of milliseconds
3141 * since epoch ~ midnight January 1, 1970.
3142 */
3143 do_gettimeofday(&current_time);
Kashyap, Desai7921b35c2010-03-17 16:21:33 +05303144 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3145 (current_time.tv_usec / 1000));
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303146
Eric Moore635374e2009-03-09 01:21:12 -06003147 if (ioc->logging_level & MPT_DEBUG_INIT) {
3148 u32 *mfp;
3149 int i;
3150
3151 mfp = (u32 *)&mpi_request;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303152 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003153 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303154 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003155 le32_to_cpu(mfp[i]));
3156 }
3157
3158 r = _base_handshake_req_reply_wait(ioc,
3159 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3160 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3161 sleep_flag);
3162
3163 if (r != 0) {
3164 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3165 ioc->name, __func__, r);
3166 return r;
3167 }
3168
Kashyap, Desai463217b2009-10-05 15:53:06 +05303169 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3170 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
Eric Moore635374e2009-03-09 01:21:12 -06003171 mpi_reply.IOCLogInfo) {
3172 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3173 r = -EIO;
3174 }
3175
3176 return 0;
3177}
3178
3179/**
3180 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3181 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003182 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3183 *
3184 * Returns 0 for success, non-zero for failure.
3185 */
3186static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303187_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003188{
3189 Mpi2PortEnableRequest_t *mpi_request;
3190 u32 ioc_state;
3191 unsigned long timeleft;
3192 int r = 0;
3193 u16 smid;
3194
3195 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3196
3197 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3198 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3199 ioc->name, __func__);
3200 return -EAGAIN;
3201 }
3202
3203 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3204 if (!smid) {
3205 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3206 ioc->name, __func__);
3207 return -EAGAIN;
3208 }
3209
3210 ioc->base_cmds.status = MPT2_CMD_PENDING;
3211 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3212 ioc->base_cmds.smid = smid;
3213 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3214 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303215 mpi_request->VF_ID = 0; /* TODO */
3216 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003217
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303218 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303219 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003220 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3221 300*HZ);
3222 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3223 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3224 ioc->name, __func__);
3225 _debug_dump_mf(mpi_request,
3226 sizeof(Mpi2PortEnableRequest_t)/4);
3227 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3228 r = -EFAULT;
3229 else
3230 r = -ETIME;
3231 goto out;
3232 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303233 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003234 ioc->name, __func__));
3235
3236 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
3237 60, sleep_flag);
3238 if (ioc_state) {
3239 printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
3240 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3241 r = -EFAULT;
3242 }
3243 out:
3244 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3245 printk(MPT2SAS_INFO_FMT "port enable: %s\n",
3246 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3247 return r;
3248}
3249
3250/**
3251 * _base_unmask_events - turn on notification for this event
3252 * @ioc: per adapter object
3253 * @event: firmware event
3254 *
3255 * The mask is stored in ioc->event_masks.
3256 */
3257static void
3258_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3259{
3260 u32 desired_event;
3261
3262 if (event >= 128)
3263 return;
3264
3265 desired_event = (1 << (event % 32));
3266
3267 if (event < 32)
3268 ioc->event_masks[0] &= ~desired_event;
3269 else if (event < 64)
3270 ioc->event_masks[1] &= ~desired_event;
3271 else if (event < 96)
3272 ioc->event_masks[2] &= ~desired_event;
3273 else if (event < 128)
3274 ioc->event_masks[3] &= ~desired_event;
3275}
3276
3277/**
3278 * _base_event_notification - send event notification
3279 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003280 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3281 *
3282 * Returns 0 for success, non-zero for failure.
3283 */
3284static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303285_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003286{
3287 Mpi2EventNotificationRequest_t *mpi_request;
3288 unsigned long timeleft;
3289 u16 smid;
3290 int r = 0;
3291 int i;
3292
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303293 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003294 __func__));
3295
3296 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3297 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3298 ioc->name, __func__);
3299 return -EAGAIN;
3300 }
3301
3302 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3303 if (!smid) {
3304 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3305 ioc->name, __func__);
3306 return -EAGAIN;
3307 }
3308 ioc->base_cmds.status = MPT2_CMD_PENDING;
3309 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3310 ioc->base_cmds.smid = smid;
3311 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3312 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303313 mpi_request->VF_ID = 0; /* TODO */
3314 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003315 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3316 mpi_request->EventMasks[i] =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303317 cpu_to_le32(ioc->event_masks[i]);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303318 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303319 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003320 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3321 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3322 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3323 ioc->name, __func__);
3324 _debug_dump_mf(mpi_request,
3325 sizeof(Mpi2EventNotificationRequest_t)/4);
3326 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3327 r = -EFAULT;
3328 else
3329 r = -ETIME;
3330 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303331 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003332 ioc->name, __func__));
3333 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3334 return r;
3335}
3336
3337/**
3338 * mpt2sas_base_validate_event_type - validating event types
3339 * @ioc: per adapter object
3340 * @event: firmware event
3341 *
3342 * This will turn on firmware event notification when application
3343 * ask for that event. We don't mask events that are already enabled.
3344 */
3345void
3346mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3347{
3348 int i, j;
3349 u32 event_mask, desired_event;
3350 u8 send_update_to_fw;
3351
3352 for (i = 0, send_update_to_fw = 0; i <
3353 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3354 event_mask = ~event_type[i];
3355 desired_event = 1;
3356 for (j = 0; j < 32; j++) {
3357 if (!(event_mask & desired_event) &&
3358 (ioc->event_masks[i] & desired_event)) {
3359 ioc->event_masks[i] &= ~desired_event;
3360 send_update_to_fw = 1;
3361 }
3362 desired_event = (desired_event << 1);
3363 }
3364 }
3365
3366 if (!send_update_to_fw)
3367 return;
3368
3369 mutex_lock(&ioc->base_cmds.mutex);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303370 _base_event_notification(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003371 mutex_unlock(&ioc->base_cmds.mutex);
3372}
3373
3374/**
3375 * _base_diag_reset - the "big hammer" start of day reset
3376 * @ioc: per adapter object
3377 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3378 *
3379 * Returns 0 for success, non-zero for failure.
3380 */
3381static int
3382_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3383{
3384 u32 host_diagnostic;
3385 u32 ioc_state;
3386 u32 count;
3387 u32 hcb_size;
3388
3389 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3390
3391 _base_save_msix_table(ioc);
3392
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303393 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
Eric Moore635374e2009-03-09 01:21:12 -06003394 ioc->name));
Eric Moore635374e2009-03-09 01:21:12 -06003395
3396 count = 0;
3397 do {
3398 /* Write magic sequence to WriteSequence register
3399 * Loop until in diagnostic mode
3400 */
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303401 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
Eric Moore635374e2009-03-09 01:21:12 -06003402 "sequence\n", ioc->name));
3403 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3404 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3405 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3406 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3407 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3408 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3409 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3410
3411 /* wait 100 msec */
3412 if (sleep_flag == CAN_SLEEP)
3413 msleep(100);
3414 else
3415 mdelay(100);
3416
3417 if (count++ > 20)
3418 goto out;
3419
3420 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303421 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
Eric Moore635374e2009-03-09 01:21:12 -06003422 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3423 ioc->name, count, host_diagnostic));
3424
3425 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3426
3427 hcb_size = readl(&ioc->chip->HCBSize);
3428
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303429 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
Eric Moore635374e2009-03-09 01:21:12 -06003430 ioc->name));
3431 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3432 &ioc->chip->HostDiagnostic);
3433
3434 /* don't access any registers for 50 milliseconds */
3435 msleep(50);
3436
3437 /* 300 second max wait */
3438 for (count = 0; count < 3000000 ; count++) {
3439
3440 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3441
3442 if (host_diagnostic == 0xFFFFFFFF)
3443 goto out;
3444 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3445 break;
3446
3447 /* wait 100 msec */
3448 if (sleep_flag == CAN_SLEEP)
3449 msleep(1);
3450 else
3451 mdelay(1);
3452 }
3453
3454 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3455
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303456 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
Eric Moore635374e2009-03-09 01:21:12 -06003457 "assuming the HCB Address points to good F/W\n",
3458 ioc->name));
3459 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3460 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3461 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3462
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303463 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003464 "re-enable the HCDW\n", ioc->name));
3465 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3466 &ioc->chip->HCBSize);
3467 }
3468
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303469 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
Eric Moore635374e2009-03-09 01:21:12 -06003470 ioc->name));
3471 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3472 &ioc->chip->HostDiagnostic);
3473
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303474 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
Eric Moore635374e2009-03-09 01:21:12 -06003475 "diagnostic register\n", ioc->name));
3476 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3477
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303478 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
Eric Moore635374e2009-03-09 01:21:12 -06003479 "READY state\n", ioc->name));
3480 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3481 sleep_flag);
3482 if (ioc_state) {
3483 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3484 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3485 goto out;
3486 }
3487
3488 _base_restore_msix_table(ioc);
3489 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3490 return 0;
3491
3492 out:
3493 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3494 return -EFAULT;
3495}
3496
3497/**
3498 * _base_make_ioc_ready - put controller in READY state
3499 * @ioc: per adapter object
3500 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3501 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3502 *
3503 * Returns 0 for success, non-zero for failure.
3504 */
3505static int
3506_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3507 enum reset_type type)
3508{
3509 u32 ioc_state;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303510 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06003511
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303512 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003513 __func__));
3514
Eric Moore3cb54692010-07-08 14:44:34 -06003515 if (ioc->pci_error_recovery)
3516 return 0;
3517
Eric Moore635374e2009-03-09 01:21:12 -06003518 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303519 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
Eric Moore635374e2009-03-09 01:21:12 -06003520 ioc->name, __func__, ioc_state));
3521
3522 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3523 return 0;
3524
3525 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303526 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06003527 "active!\n", ioc->name));
3528 goto issue_diag_reset;
3529 }
3530
3531 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3532 mpt2sas_base_fault_info(ioc, ioc_state &
3533 MPI2_DOORBELL_DATA_MASK);
3534 goto issue_diag_reset;
3535 }
3536
3537 if (type == FORCE_BIG_HAMMER)
3538 goto issue_diag_reset;
3539
3540 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3541 if (!(_base_send_ioc_reset(ioc,
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303542 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
3543 ioc->ioc_reset_count++;
Eric Moore635374e2009-03-09 01:21:12 -06003544 return 0;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303545 }
Eric Moore635374e2009-03-09 01:21:12 -06003546
3547 issue_diag_reset:
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303548 rc = _base_diag_reset(ioc, CAN_SLEEP);
3549 ioc->ioc_reset_count++;
3550 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06003551}
3552
3553/**
3554 * _base_make_ioc_operational - put controller in OPERATIONAL state
3555 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003556 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3557 *
3558 * Returns 0 for success, non-zero for failure.
3559 */
3560static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303561_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003562{
3563 int r, i;
3564 unsigned long flags;
3565 u32 reply_address;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303566 u16 smid;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303567 struct _tr_list *delayed_tr, *delayed_tr_next;
Eric Moore635374e2009-03-09 01:21:12 -06003568
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303569 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003570 __func__));
3571
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303572 /* clean the delayed target reset list */
3573 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3574 &ioc->delayed_tr_list, list) {
3575 list_del(&delayed_tr->list);
3576 kfree(delayed_tr);
3577 }
3578
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303579 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3580 &ioc->delayed_tr_volume_list, list) {
3581 list_del(&delayed_tr->list);
3582 kfree(delayed_tr);
3583 }
3584
Eric Moore635374e2009-03-09 01:21:12 -06003585 /* initialize the scsi lookup free list */
3586 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3587 INIT_LIST_HEAD(&ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303588 smid = 1;
3589 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06003590 ioc->scsi_lookup[i].cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303591 ioc->scsi_lookup[i].smid = smid;
3592 ioc->scsi_lookup[i].scmd = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003593 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3594 &ioc->free_list);
3595 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303596
3597 /* hi-priority queue */
3598 INIT_LIST_HEAD(&ioc->hpr_free_list);
3599 smid = ioc->hi_priority_smid;
3600 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
3601 ioc->hpr_lookup[i].cb_idx = 0xFF;
3602 ioc->hpr_lookup[i].smid = smid;
3603 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
3604 &ioc->hpr_free_list);
3605 }
3606
3607 /* internal queue */
3608 INIT_LIST_HEAD(&ioc->internal_free_list);
3609 smid = ioc->internal_smid;
3610 for (i = 0; i < ioc->internal_depth; i++, smid++) {
3611 ioc->internal_lookup[i].cb_idx = 0xFF;
3612 ioc->internal_lookup[i].smid = smid;
3613 list_add_tail(&ioc->internal_lookup[i].tracker_list,
3614 &ioc->internal_free_list);
3615 }
Eric Moore635374e2009-03-09 01:21:12 -06003616 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3617
3618 /* initialize Reply Free Queue */
3619 for (i = 0, reply_address = (u32)ioc->reply_dma ;
3620 i < ioc->reply_free_queue_depth ; i++, reply_address +=
3621 ioc->reply_sz)
3622 ioc->reply_free[i] = cpu_to_le32(reply_address);
3623
3624 /* initialize Reply Post Free Queue */
3625 for (i = 0; i < ioc->reply_post_queue_depth; i++)
Eric Moore03ea1112009-04-21 15:37:57 -06003626 ioc->reply_post_free[i].Words = ULLONG_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06003627
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303628 r = _base_send_ioc_init(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003629 if (r)
3630 return r;
3631
3632 /* initialize the index's */
3633 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
3634 ioc->reply_post_host_index = 0;
3635 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
3636 writel(0, &ioc->chip->ReplyPostHostIndex);
3637
3638 _base_unmask_interrupts(ioc);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303639 r = _base_event_notification(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003640 if (r)
3641 return r;
3642
3643 if (sleep_flag == CAN_SLEEP)
3644 _base_static_config_pages(ioc);
3645
Kashyap, Desai570c67a2010-06-17 13:43:17 +05303646 if (ioc->wait_for_port_enable_to_complete) {
3647 if (diag_buffer_enable != 0)
3648 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
3649 if (disable_discovery > 0)
3650 return r;
3651 }
Kashyap, Desaidd5fd332010-06-17 13:31:17 +05303652
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303653 r = _base_send_port_enable(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003654 if (r)
3655 return r;
3656
3657 return r;
3658}
3659
3660/**
3661 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
3662 * @ioc: per adapter object
3663 *
3664 * Return nothing.
3665 */
3666void
3667mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
3668{
3669 struct pci_dev *pdev = ioc->pdev;
3670
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303671 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003672 __func__));
3673
3674 _base_mask_interrupts(ioc);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05303675 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06003676 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05303677 ioc->shost_recovery = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003678 if (ioc->pci_irq) {
3679 synchronize_irq(pdev->irq);
3680 free_irq(ioc->pci_irq, ioc);
3681 }
3682 _base_disable_msix(ioc);
3683 if (ioc->chip_phys)
3684 iounmap(ioc->chip);
3685 ioc->pci_irq = -1;
3686 ioc->chip_phys = 0;
3687 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05303688 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06003689 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06003690 return;
3691}
3692
3693/**
3694 * mpt2sas_base_attach - attach controller instance
3695 * @ioc: per adapter object
3696 *
3697 * Returns 0 for success, non-zero for failure.
3698 */
3699int
3700mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
3701{
3702 int r, i;
Eric Moore635374e2009-03-09 01:21:12 -06003703
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303704 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003705 __func__));
3706
3707 r = mpt2sas_base_map_resources(ioc);
3708 if (r)
3709 return r;
3710
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303711 pci_set_drvdata(ioc->pdev, ioc->shost);
Kashyap, Desai96b681c2009-09-23 17:32:06 +05303712 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003713 if (r)
3714 goto out_free_resources;
3715
Kashyap, Desai96b681c2009-09-23 17:32:06 +05303716 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Eric Moore635374e2009-03-09 01:21:12 -06003717 if (r)
3718 goto out_free_resources;
3719
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303720 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
3721 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303722 if (!ioc->pfacts) {
3723 r = -ENOMEM;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303724 goto out_free_resources;
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303725 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303726
3727 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
3728 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
3729 if (r)
3730 goto out_free_resources;
3731 }
3732
Eric Moore635374e2009-03-09 01:21:12 -06003733 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
3734 if (r)
3735 goto out_free_resources;
3736
3737 init_waitqueue_head(&ioc->reset_wq);
3738
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303739 /* allocate memory pd handle bitmask list */
3740 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
3741 if (ioc->facts.MaxDevHandle % 8)
3742 ioc->pd_handles_sz++;
3743 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
3744 GFP_KERNEL);
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05303745 if (!ioc->pd_handles) {
3746 r = -ENOMEM;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303747 goto out_free_resources;
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05303748 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303749
Kashyap, Desaie75b9b62009-12-16 18:52:39 +05303750 ioc->fwfault_debug = mpt2sas_fwfault_debug;
3751
Eric Moore635374e2009-03-09 01:21:12 -06003752 /* base internal command bits */
3753 mutex_init(&ioc->base_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003754 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3755 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3756
3757 /* transport internal command bits */
3758 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3759 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
3760 mutex_init(&ioc->transport_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003761
Kashyap, Desaid685c262009-11-17 13:16:37 +05303762 /* scsih internal command bits */
3763 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3764 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
3765 mutex_init(&ioc->scsih_cmds.mutex);
3766
Eric Moore635374e2009-03-09 01:21:12 -06003767 /* task management internal command bits */
3768 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3769 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3770 mutex_init(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003771
3772 /* config page internal command bits */
3773 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3774 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
3775 mutex_init(&ioc->config_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003776
3777 /* ctl module internal command bits */
3778 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
Kashyap, Desai769578f2010-06-17 13:49:28 +05303779 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06003780 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
3781 mutex_init(&ioc->ctl_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003782
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303783 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
3784 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
Kashyap, Desai769578f2010-06-17 13:49:28 +05303785 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
3786 !ioc->ctl_cmds.sense) {
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303787 r = -ENOMEM;
3788 goto out_free_resources;
3789 }
3790
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05303791 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
3792 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
3793 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
3794 r = -ENOMEM;
3795 goto out_free_resources;
3796 }
3797
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05303798 init_completion(&ioc->shost_recovery_done);
3799
Eric Moore635374e2009-03-09 01:21:12 -06003800 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3801 ioc->event_masks[i] = -1;
3802
3803 /* here we enable the events we care about */
3804 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
3805 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
3806 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3807 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3808 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
3809 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
3810 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
3811 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
3812 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
3813 _base_unmask_events(ioc, MPI2_EVENT_TASK_SET_FULL);
3814 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303815 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003816 if (r)
3817 goto out_free_resources;
3818
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05303819 if (missing_delay[0] != -1 && missing_delay[1] != -1)
3820 _base_update_missing_delay(ioc, missing_delay[0],
3821 missing_delay[1]);
3822
Kashyap, Desaie4750c92009-08-07 19:37:59 +05303823 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06003824 return 0;
3825
3826 out_free_resources:
3827
3828 ioc->remove_host = 1;
3829 mpt2sas_base_free_resources(ioc);
3830 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303831 pci_set_drvdata(ioc->pdev, NULL);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303832 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003833 kfree(ioc->tm_cmds.reply);
3834 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303835 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06003836 kfree(ioc->config_cmds.reply);
3837 kfree(ioc->base_cmds.reply);
3838 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05303839 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06003840 kfree(ioc->pfacts);
3841 ioc->ctl_cmds.reply = NULL;
3842 ioc->base_cmds.reply = NULL;
3843 ioc->tm_cmds.reply = NULL;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303844 ioc->scsih_cmds.reply = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003845 ioc->transport_cmds.reply = NULL;
3846 ioc->config_cmds.reply = NULL;
3847 ioc->pfacts = NULL;
3848 return r;
3849}
3850
3851
3852/**
3853 * mpt2sas_base_detach - remove controller instance
3854 * @ioc: per adapter object
3855 *
3856 * Return nothing.
3857 */
3858void
3859mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
3860{
Eric Moore635374e2009-03-09 01:21:12 -06003861
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303862 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003863 __func__));
3864
Kashyap, Desaie4750c92009-08-07 19:37:59 +05303865 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06003866 mpt2sas_base_free_resources(ioc);
3867 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303868 pci_set_drvdata(ioc->pdev, NULL);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303869 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003870 kfree(ioc->pfacts);
3871 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05303872 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06003873 kfree(ioc->base_cmds.reply);
3874 kfree(ioc->tm_cmds.reply);
3875 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303876 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06003877 kfree(ioc->config_cmds.reply);
3878}
3879
3880/**
3881 * _base_reset_handler - reset callback handler (for base)
3882 * @ioc: per adapter object
3883 * @reset_phase: phase
3884 *
3885 * The handler for doing any required cleanup or initialization.
3886 *
3887 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
3888 * MPT2_IOC_DONE_RESET
3889 *
3890 * Return nothing.
3891 */
3892static void
3893_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
3894{
3895 switch (reset_phase) {
3896 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303897 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06003898 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
3899 break;
3900 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303901 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06003902 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
3903 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
3904 ioc->transport_cmds.status |= MPT2_CMD_RESET;
3905 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
3906 complete(&ioc->transport_cmds.done);
3907 }
3908 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3909 ioc->base_cmds.status |= MPT2_CMD_RESET;
3910 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
3911 complete(&ioc->base_cmds.done);
3912 }
3913 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
3914 ioc->config_cmds.status |= MPT2_CMD_RESET;
3915 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003916 ioc->config_cmds.smid = USHRT_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06003917 complete(&ioc->config_cmds.done);
3918 }
3919 break;
3920 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303921 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06003922 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
3923 break;
3924 }
3925 mpt2sas_scsih_reset_handler(ioc, reset_phase);
3926 mpt2sas_ctl_reset_handler(ioc, reset_phase);
3927}
3928
3929/**
3930 * _wait_for_commands_to_complete - reset controller
3931 * @ioc: Pointer to MPT_ADAPTER structure
3932 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3933 *
3934 * This function waiting(3s) for all pending commands to complete
3935 * prior to putting controller in reset.
3936 */
3937static void
3938_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3939{
3940 u32 ioc_state;
3941 unsigned long flags;
3942 u16 i;
3943
3944 ioc->pending_io_count = 0;
3945 if (sleep_flag != CAN_SLEEP)
3946 return;
3947
3948 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3949 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
3950 return;
3951
3952 /* pending command count */
3953 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303954 for (i = 0; i < ioc->scsiio_depth; i++)
Eric Moore635374e2009-03-09 01:21:12 -06003955 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
3956 ioc->pending_io_count++;
3957 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3958
3959 if (!ioc->pending_io_count)
3960 return;
3961
3962 /* wait for pending commands to complete */
Kashyap, Desaid2742132010-06-17 13:28:55 +05303963 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
Eric Moore635374e2009-03-09 01:21:12 -06003964}
3965
3966/**
3967 * mpt2sas_base_hard_reset_handler - reset controller
3968 * @ioc: Pointer to MPT_ADAPTER structure
3969 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3970 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3971 *
3972 * Returns 0 for success, non-zero for failure.
3973 */
3974int
3975mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3976 enum reset_type type)
3977{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303978 int r;
Eric Moore635374e2009-03-09 01:21:12 -06003979 unsigned long flags;
3980
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303981 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003982 __func__));
3983
Eric Moore3cb54692010-07-08 14:44:34 -06003984 if (ioc->pci_error_recovery) {
3985 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
3986 ioc->name, __func__);
3987 r = 0;
3988 goto out;
3989 }
3990
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05303991 if (mpt2sas_fwfault_debug)
3992 mpt2sas_halt_firmware(ioc);
3993
Kashyap, Desaid2742132010-06-17 13:28:55 +05303994 /* TODO - What we really should be doing is pulling
3995 * out all the code associated with NO_SLEEP; its never used.
3996 * That is legacy code from mpt fusion driver, ported over.
3997 * I will leave this BUG_ON here for now till its been resolved.
3998 */
3999 BUG_ON(sleep_flag == NO_SLEEP);
4000
4001 /* wait for an active reset in progress to complete */
4002 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4003 do {
4004 ssleep(1);
4005 } while (ioc->shost_recovery == 1);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304006 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304007 __func__));
4008 return ioc->ioc_reset_in_progress_status;
Eric Moore635374e2009-03-09 01:21:12 -06004009 }
Kashyap, Desaid2742132010-06-17 13:28:55 +05304010
4011 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06004012 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004013 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4014
4015 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4016 _wait_for_commands_to_complete(ioc, sleep_flag);
4017 _base_mask_interrupts(ioc);
4018 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4019 if (r)
4020 goto out;
4021 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304022 r = _base_make_ioc_operational(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004023 if (!r)
4024 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4025 out:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304026 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
Eric Moore635374e2009-03-09 01:21:12 -06004027 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4028
4029 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304030 ioc->ioc_reset_in_progress_status = r;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304031 ioc->shost_recovery = 0;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05304032 complete(&ioc->shost_recovery_done);
Eric Moore635374e2009-03-09 01:21:12 -06004033 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304034 mutex_unlock(&ioc->reset_in_progress_mutex);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05304035
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304036 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304037 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06004038 return r;
4039}