blob: 1d2374b5a0a199010a5d897ac64c734e4bade681 [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, Desai19d3ebe2009-09-14 11:01:36 +05306 * Copyright (C) 2007-2009 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>
60
61#include "mpt2sas_base.h"
62
63static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
64
65#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +053066#define MPT2SAS_MAX_REQUEST_QUEUE 600 /* maximum controller queue depth */
Eric Moore635374e2009-03-09 01:21:12 -060067
68static int max_queue_depth = -1;
69module_param(max_queue_depth, int, 0);
70MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
71
72static int max_sgl_entries = -1;
73module_param(max_sgl_entries, int, 0);
74MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
75
76static int msix_disable = -1;
77module_param(msix_disable, int, 0);
78MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
79
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053080/* diag_buffer_enable is bitwise
81 * bit 0 set = MPI2_DIAG_BUF_TYPE_TRACE(1)
82 * bit 1 set = MPI2_DIAG_BUF_TYPE_SNAPSHOT(2)
83 *
84 * Either bit can be set, or both
85 */
86static int diag_buffer_enable;
87module_param(diag_buffer_enable, int, 0);
88MODULE_PARM_DESC(diag_buffer_enable, " enable diag buffer at driver load "
89 "time (TRACE=1/SNAP=2/default=0)");
90
Kashyap, Desaifa7f3162009-09-23 17:26:58 +053091int mpt2sas_fwfault_debug;
92MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
93 "and halt firmware - (default=0)");
94
95/**
96 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
97 *
98 */
99static int
100_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
101{
102 int ret = param_set_int(val, kp);
103 struct MPT2SAS_ADAPTER *ioc;
104
105 if (ret)
106 return ret;
107
108 printk(KERN_INFO "setting logging_level(0x%08x)\n",
109 mpt2sas_fwfault_debug);
110 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
111 ioc->fwfault_debug = mpt2sas_fwfault_debug;
112 return 0;
113}
114module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
115 param_get_int, &mpt2sas_fwfault_debug, 0644);
116
Eric Moore635374e2009-03-09 01:21:12 -0600117/**
118 * _base_fault_reset_work - workq handling ioc fault conditions
119 * @work: input argument, used to derive ioc
120 * Context: sleep.
121 *
122 * Return nothing.
123 */
124static void
125_base_fault_reset_work(struct work_struct *work)
126{
127 struct MPT2SAS_ADAPTER *ioc =
128 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
129 unsigned long flags;
130 u32 doorbell;
131 int rc;
132
133 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530134 if (ioc->shost_recovery)
Eric Moore635374e2009-03-09 01:21:12 -0600135 goto rearm_timer;
136 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
137
138 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
139 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
140 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
141 FORCE_BIG_HAMMER);
142 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
143 __func__, (rc == 0) ? "success" : "failed");
144 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
145 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
146 mpt2sas_base_fault_info(ioc, doorbell &
147 MPI2_DOORBELL_DATA_MASK);
148 }
149
150 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
151 rearm_timer:
152 if (ioc->fault_reset_work_q)
153 queue_delayed_work(ioc->fault_reset_work_q,
154 &ioc->fault_reset_work,
155 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
156 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
157}
158
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530159/**
160 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530161 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530162 * Context: sleep.
163 *
164 * Return nothing.
165 */
166void
167mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
168{
169 unsigned long flags;
170
171 if (ioc->fault_reset_work_q)
172 return;
173
174 /* initialize fault polling */
175 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
176 snprintf(ioc->fault_reset_work_q_name,
177 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
178 ioc->fault_reset_work_q =
179 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
180 if (!ioc->fault_reset_work_q) {
181 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
182 ioc->name, __func__, __LINE__);
183 return;
184 }
185 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
186 if (ioc->fault_reset_work_q)
187 queue_delayed_work(ioc->fault_reset_work_q,
188 &ioc->fault_reset_work,
189 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
190 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
191}
192
193/**
194 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530195 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530196 * Context: sleep.
197 *
198 * Return nothing.
199 */
200void
201mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
202{
203 unsigned long flags;
204 struct workqueue_struct *wq;
205
206 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
207 wq = ioc->fault_reset_work_q;
208 ioc->fault_reset_work_q = NULL;
209 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
210 if (wq) {
211 if (!cancel_delayed_work(&ioc->fault_reset_work))
212 flush_workqueue(wq);
213 destroy_workqueue(wq);
214 }
215}
216
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530217/**
218 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
219 * @ioc: per adapter object
220 * @fault_code: fault code
221 *
222 * Return nothing.
223 */
224void
225mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
226{
227 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
228 ioc->name, fault_code);
229}
230
231/**
232 * mpt2sas_halt_firmware - halt's mpt controller firmware
233 * @ioc: per adapter object
234 *
235 * For debugging timeout related issues. Writing 0xCOFFEE00
236 * to the doorbell register will halt controller firmware. With
237 * the purpose to stop both driver and firmware, the enduser can
238 * obtain a ring buffer from controller UART.
239 */
240void
241mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
242{
243 u32 doorbell;
244
245 if (!ioc->fwfault_debug)
246 return;
247
248 dump_stack();
249
250 doorbell = readl(&ioc->chip->Doorbell);
251 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
252 mpt2sas_base_fault_info(ioc , doorbell);
253 else {
254 writel(0xC0FFEE00, &ioc->chip->Doorbell);
255 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
256 "timeout\n", ioc->name);
257 }
258
259 panic("panic in %s\n", __func__);
260}
261
Eric Moore635374e2009-03-09 01:21:12 -0600262#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
263/**
264 * _base_sas_ioc_info - verbose translation of the ioc status
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530265 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600266 * @mpi_reply: reply mf payload returned from firmware
267 * @request_hdr: request mf
268 *
269 * Return nothing.
270 */
271static void
272_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
273 MPI2RequestHeader_t *request_hdr)
274{
275 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
276 MPI2_IOCSTATUS_MASK;
277 char *desc = NULL;
278 u16 frame_sz;
279 char *func_str = NULL;
280
281 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
282 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
283 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
284 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
285 return;
286
287 switch (ioc_status) {
288
289/****************************************************************************
290* Common IOCStatus values for all replies
291****************************************************************************/
292
293 case MPI2_IOCSTATUS_INVALID_FUNCTION:
294 desc = "invalid function";
295 break;
296 case MPI2_IOCSTATUS_BUSY:
297 desc = "busy";
298 break;
299 case MPI2_IOCSTATUS_INVALID_SGL:
300 desc = "invalid sgl";
301 break;
302 case MPI2_IOCSTATUS_INTERNAL_ERROR:
303 desc = "internal error";
304 break;
305 case MPI2_IOCSTATUS_INVALID_VPID:
306 desc = "invalid vpid";
307 break;
308 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
309 desc = "insufficient resources";
310 break;
311 case MPI2_IOCSTATUS_INVALID_FIELD:
312 desc = "invalid field";
313 break;
314 case MPI2_IOCSTATUS_INVALID_STATE:
315 desc = "invalid state";
316 break;
317 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
318 desc = "op state not supported";
319 break;
320
321/****************************************************************************
322* Config IOCStatus values
323****************************************************************************/
324
325 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
326 desc = "config invalid action";
327 break;
328 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
329 desc = "config invalid type";
330 break;
331 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
332 desc = "config invalid page";
333 break;
334 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
335 desc = "config invalid data";
336 break;
337 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
338 desc = "config no defaults";
339 break;
340 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
341 desc = "config cant commit";
342 break;
343
344/****************************************************************************
345* SCSI IO Reply
346****************************************************************************/
347
348 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
349 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
350 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
351 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
352 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
353 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
354 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
355 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
356 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
357 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
358 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
359 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
360 break;
361
362/****************************************************************************
363* For use by SCSI Initiator and SCSI Target end-to-end data protection
364****************************************************************************/
365
366 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
367 desc = "eedp guard error";
368 break;
369 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
370 desc = "eedp ref tag error";
371 break;
372 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
373 desc = "eedp app tag error";
374 break;
375
376/****************************************************************************
377* SCSI Target values
378****************************************************************************/
379
380 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
381 desc = "target invalid io index";
382 break;
383 case MPI2_IOCSTATUS_TARGET_ABORTED:
384 desc = "target aborted";
385 break;
386 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
387 desc = "target no conn retryable";
388 break;
389 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
390 desc = "target no connection";
391 break;
392 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
393 desc = "target xfer count mismatch";
394 break;
395 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
396 desc = "target data offset error";
397 break;
398 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
399 desc = "target too much write data";
400 break;
401 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
402 desc = "target iu too short";
403 break;
404 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
405 desc = "target ack nak timeout";
406 break;
407 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
408 desc = "target nak received";
409 break;
410
411/****************************************************************************
412* Serial Attached SCSI values
413****************************************************************************/
414
415 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
416 desc = "smp request failed";
417 break;
418 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
419 desc = "smp data overrun";
420 break;
421
422/****************************************************************************
423* Diagnostic Buffer Post / Diagnostic Release values
424****************************************************************************/
425
426 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
427 desc = "diagnostic released";
428 break;
429 default:
430 break;
431 }
432
433 if (!desc)
434 return;
435
436 switch (request_hdr->Function) {
437 case MPI2_FUNCTION_CONFIG:
438 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
439 func_str = "config_page";
440 break;
441 case MPI2_FUNCTION_SCSI_TASK_MGMT:
442 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
443 func_str = "task_mgmt";
444 break;
445 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
446 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
447 func_str = "sas_iounit_ctl";
448 break;
449 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
450 frame_sz = sizeof(Mpi2SepRequest_t);
451 func_str = "enclosure";
452 break;
453 case MPI2_FUNCTION_IOC_INIT:
454 frame_sz = sizeof(Mpi2IOCInitRequest_t);
455 func_str = "ioc_init";
456 break;
457 case MPI2_FUNCTION_PORT_ENABLE:
458 frame_sz = sizeof(Mpi2PortEnableRequest_t);
459 func_str = "port_enable";
460 break;
461 case MPI2_FUNCTION_SMP_PASSTHROUGH:
462 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
463 func_str = "smp_passthru";
464 break;
465 default:
466 frame_sz = 32;
467 func_str = "unknown";
468 break;
469 }
470
471 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
472 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
473
474 _debug_dump_mf(request_hdr, frame_sz/4);
475}
476
477/**
478 * _base_display_event_data - verbose translation of firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530479 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600480 * @mpi_reply: reply mf payload returned from firmware
481 *
482 * Return nothing.
483 */
484static void
485_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
486 Mpi2EventNotificationReply_t *mpi_reply)
487{
488 char *desc = NULL;
489 u16 event;
490
491 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
492 return;
493
494 event = le16_to_cpu(mpi_reply->Event);
495
496 switch (event) {
497 case MPI2_EVENT_LOG_DATA:
498 desc = "Log Data";
499 break;
500 case MPI2_EVENT_STATE_CHANGE:
501 desc = "Status Change";
502 break;
503 case MPI2_EVENT_HARD_RESET_RECEIVED:
504 desc = "Hard Reset Received";
505 break;
506 case MPI2_EVENT_EVENT_CHANGE:
507 desc = "Event Change";
508 break;
509 case MPI2_EVENT_TASK_SET_FULL:
510 desc = "Task Set Full";
511 break;
512 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
513 desc = "Device Status Change";
514 break;
515 case MPI2_EVENT_IR_OPERATION_STATUS:
516 desc = "IR Operation Status";
517 break;
518 case MPI2_EVENT_SAS_DISCOVERY:
519 desc = "Discovery";
520 break;
521 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
522 desc = "SAS Broadcast Primitive";
523 break;
524 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
525 desc = "SAS Init Device Status Change";
526 break;
527 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
528 desc = "SAS Init Table Overflow";
529 break;
530 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
531 desc = "SAS Topology Change List";
532 break;
533 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
534 desc = "SAS Enclosure Device Status Change";
535 break;
536 case MPI2_EVENT_IR_VOLUME:
537 desc = "IR Volume";
538 break;
539 case MPI2_EVENT_IR_PHYSICAL_DISK:
540 desc = "IR Physical Disk";
541 break;
542 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
543 desc = "IR Configuration Change List";
544 break;
545 case MPI2_EVENT_LOG_ENTRY_ADDED:
546 desc = "Log Entry Added";
547 break;
548 }
549
550 if (!desc)
551 return;
552
553 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
554}
555#endif
556
557/**
558 * _base_sas_log_info - verbose translation of firmware log info
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530559 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600560 * @log_info: log info
561 *
562 * Return nothing.
563 */
564static void
565_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
566{
567 union loginfo_type {
568 u32 loginfo;
569 struct {
570 u32 subcode:16;
571 u32 code:8;
572 u32 originator:4;
573 u32 bus_type:4;
574 } dw;
575 };
576 union loginfo_type sas_loginfo;
577 char *originator_str = NULL;
578
579 sas_loginfo.loginfo = log_info;
580 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
581 return;
582
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +0530583 /* each nexus loss loginfo */
584 if (log_info == 0x31170000)
585 return;
586
Eric Moore635374e2009-03-09 01:21:12 -0600587 /* eat the loginfos associated with task aborts */
588 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
589 0x31140000 || log_info == 0x31130000))
590 return;
591
592 switch (sas_loginfo.dw.originator) {
593 case 0:
594 originator_str = "IOP";
595 break;
596 case 1:
597 originator_str = "PL";
598 break;
599 case 2:
600 originator_str = "IR";
601 break;
602 }
603
604 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
605 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
606 originator_str, sas_loginfo.dw.code,
607 sas_loginfo.dw.subcode);
608}
609
610/**
Eric Moore635374e2009-03-09 01:21:12 -0600611 * _base_display_reply_info -
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530612 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600613 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530614 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600615 * @reply: reply message frame(lower 32bit addr)
616 *
617 * Return nothing.
618 */
619static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530620_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600621 u32 reply)
622{
623 MPI2DefaultReply_t *mpi_reply;
624 u16 ioc_status;
625
626 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
627 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
628#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
629 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
630 (ioc->logging_level & MPT_DEBUG_REPLY)) {
631 _base_sas_ioc_info(ioc , mpi_reply,
632 mpt2sas_base_get_msg_frame(ioc, smid));
633 }
634#endif
635 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
636 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
637}
638
639/**
640 * mpt2sas_base_done - base internal command completion routine
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530641 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600642 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530643 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600644 * @reply: reply message frame(lower 32bit addr)
645 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530646 * Return 1 meaning mf should be freed from _base_interrupt
647 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600648 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530649u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530650mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
651 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600652{
653 MPI2DefaultReply_t *mpi_reply;
654
655 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
656 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530657 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600658
659 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530660 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600661
662 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
663 if (mpi_reply) {
664 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
665 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
666 }
667 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
668 complete(&ioc->base_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530669 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600670}
671
672/**
673 * _base_async_event - main callback handler for firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530674 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530675 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600676 * @reply: reply message frame(lower 32bit addr)
677 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530678 * Return 1 meaning mf should be freed from _base_interrupt
679 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600680 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530681static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530682_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600683{
684 Mpi2EventNotificationReply_t *mpi_reply;
685 Mpi2EventAckRequest_t *ack_request;
686 u16 smid;
687
688 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
689 if (!mpi_reply)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530690 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600691 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530692 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600693#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
694 _base_display_event_data(ioc, mpi_reply);
695#endif
696 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
697 goto out;
698 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
699 if (!smid) {
700 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
701 ioc->name, __func__);
702 goto out;
703 }
704
705 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
706 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
707 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
708 ack_request->Event = mpi_reply->Event;
709 ack_request->EventContext = mpi_reply->EventContext;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530710 ack_request->VF_ID = 0; /* TODO */
711 ack_request->VP_ID = 0;
712 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600713
714 out:
715
716 /* scsih callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530717 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600718
719 /* ctl callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530720 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530721
722 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600723}
724
725/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530726 * _base_get_cb_idx - obtain the callback index
727 * @ioc: per adapter object
728 * @smid: system request message index
729 *
730 * Return callback index.
731 */
732static u8
733_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
734{
735 int i;
736 u8 cb_idx = 0xFF;
737
738 if (smid >= ioc->hi_priority_smid) {
739 if (smid < ioc->internal_smid) {
740 i = smid - ioc->hi_priority_smid;
741 cb_idx = ioc->hpr_lookup[i].cb_idx;
742 } else {
743 i = smid - ioc->internal_smid;
744 cb_idx = ioc->internal_lookup[i].cb_idx;
745 }
746 } else {
747 i = smid - 1;
748 cb_idx = ioc->scsi_lookup[i].cb_idx;
749 }
750 return cb_idx;
751}
752
753/**
Eric Moore635374e2009-03-09 01:21:12 -0600754 * _base_mask_interrupts - disable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530755 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600756 *
757 * Disabling ResetIRQ, Reply and Doorbell Interrupts
758 *
759 * Return nothing.
760 */
761static void
762_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
763{
764 u32 him_register;
765
766 ioc->mask_interrupts = 1;
767 him_register = readl(&ioc->chip->HostInterruptMask);
768 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
769 writel(him_register, &ioc->chip->HostInterruptMask);
770 readl(&ioc->chip->HostInterruptMask);
771}
772
773/**
774 * _base_unmask_interrupts - enable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530775 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600776 *
777 * Enabling only Reply Interrupts
778 *
779 * Return nothing.
780 */
781static void
782_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
783{
784 u32 him_register;
785
Eric Moore635374e2009-03-09 01:21:12 -0600786 him_register = readl(&ioc->chip->HostInterruptMask);
787 him_register &= ~MPI2_HIM_RIM;
788 writel(him_register, &ioc->chip->HostInterruptMask);
789 ioc->mask_interrupts = 0;
790}
791
Kashyap, Desai5b768582009-08-20 13:24:31 +0530792union reply_descriptor {
793 u64 word;
794 struct {
795 u32 low;
796 u32 high;
797 } u;
798};
799
Eric Moore635374e2009-03-09 01:21:12 -0600800/**
801 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
802 * @irq: irq number (not used)
803 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
804 * @r: pt_regs pointer (not used)
805 *
806 * Return IRQ_HANDLE if processed, else IRQ_NONE.
807 */
808static irqreturn_t
809_base_interrupt(int irq, void *bus_id)
810{
Eric Moore03ea1112009-04-21 15:37:57 -0600811 union reply_descriptor rd;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530812 u32 completed_cmds;
Eric Moore635374e2009-03-09 01:21:12 -0600813 u8 request_desript_type;
814 u16 smid;
815 u8 cb_idx;
816 u32 reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530817 u8 msix_index;
Eric Moore635374e2009-03-09 01:21:12 -0600818 struct MPT2SAS_ADAPTER *ioc = bus_id;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530819 Mpi2ReplyDescriptorsUnion_t *rpf;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530820 u8 rc;
Eric Moore635374e2009-03-09 01:21:12 -0600821
822 if (ioc->mask_interrupts)
823 return IRQ_NONE;
824
Kashyap, Desai5b768582009-08-20 13:24:31 +0530825 rpf = &ioc->reply_post_free[ioc->reply_post_host_index];
826 request_desript_type = rpf->Default.ReplyFlags
827 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600828 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
829 return IRQ_NONE;
830
831 completed_cmds = 0;
832 do {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530833 rd.word = rpf->Words;
Eric Moore03ea1112009-04-21 15:37:57 -0600834 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
Eric Moore635374e2009-03-09 01:21:12 -0600835 goto out;
836 reply = 0;
837 cb_idx = 0xFF;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530838 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530839 msix_index = rpf->Default.MSIxIndex;
Eric Moore635374e2009-03-09 01:21:12 -0600840 if (request_desript_type ==
841 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530842 reply = le32_to_cpu
843 (rpf->AddressReply.ReplyFrameAddress);
Eric Moore635374e2009-03-09 01:21:12 -0600844 } else if (request_desript_type ==
845 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
846 goto next;
847 else if (request_desript_type ==
848 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
849 goto next;
850 if (smid)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530851 cb_idx = _base_get_cb_idx(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600852 if (smid && cb_idx != 0xFF) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530853 rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530854 reply);
Eric Moore635374e2009-03-09 01:21:12 -0600855 if (reply)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530856 _base_display_reply_info(ioc, smid, msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600857 reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530858 if (rc)
859 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600860 }
861 if (!smid)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530862 _base_async_event(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600863
864 /* reply free queue handling */
865 if (reply) {
866 ioc->reply_free_host_index =
867 (ioc->reply_free_host_index ==
868 (ioc->reply_free_queue_depth - 1)) ?
869 0 : ioc->reply_free_host_index + 1;
870 ioc->reply_free[ioc->reply_free_host_index] =
871 cpu_to_le32(reply);
Kashyap, Desai5b768582009-08-20 13:24:31 +0530872 wmb();
Eric Moore635374e2009-03-09 01:21:12 -0600873 writel(ioc->reply_free_host_index,
874 &ioc->chip->ReplyFreeHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600875 }
876
877 next:
Kashyap, Desai5b768582009-08-20 13:24:31 +0530878
879 rpf->Words = ULLONG_MAX;
880 ioc->reply_post_host_index = (ioc->reply_post_host_index ==
881 (ioc->reply_post_queue_depth - 1)) ? 0 :
882 ioc->reply_post_host_index + 1;
Eric Moore635374e2009-03-09 01:21:12 -0600883 request_desript_type =
Kashyap, Desai5b768582009-08-20 13:24:31 +0530884 ioc->reply_post_free[ioc->reply_post_host_index].Default.
885 ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600886 completed_cmds++;
887 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
888 goto out;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530889 if (!ioc->reply_post_host_index)
890 rpf = ioc->reply_post_free;
891 else
892 rpf++;
Eric Moore635374e2009-03-09 01:21:12 -0600893 } while (1);
894
895 out:
896
897 if (!completed_cmds)
898 return IRQ_NONE;
899
Eric Moore635374e2009-03-09 01:21:12 -0600900 wmb();
Kashyap, Desai5b768582009-08-20 13:24:31 +0530901 writel(ioc->reply_post_host_index, &ioc->chip->ReplyPostHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600902 return IRQ_HANDLED;
903}
904
905/**
906 * mpt2sas_base_release_callback_handler - clear interupt callback handler
907 * @cb_idx: callback index
908 *
909 * Return nothing.
910 */
911void
912mpt2sas_base_release_callback_handler(u8 cb_idx)
913{
914 mpt_callbacks[cb_idx] = NULL;
915}
916
917/**
918 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
919 * @cb_func: callback function
920 *
921 * Returns cb_func.
922 */
923u8
924mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
925{
926 u8 cb_idx;
927
928 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
929 if (mpt_callbacks[cb_idx] == NULL)
930 break;
931
932 mpt_callbacks[cb_idx] = cb_func;
933 return cb_idx;
934}
935
936/**
937 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
938 *
939 * Return nothing.
940 */
941void
942mpt2sas_base_initialize_callback_handler(void)
943{
944 u8 cb_idx;
945
946 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
947 mpt2sas_base_release_callback_handler(cb_idx);
948}
949
950/**
951 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
952 * @ioc: per adapter object
953 * @paddr: virtual address for SGE
954 *
955 * Create a zero length scatter gather entry to insure the IOCs hardware has
956 * something to use if the target device goes brain dead and tries
957 * to send data even when none is asked for.
958 *
959 * Return nothing.
960 */
961void
962mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
963{
964 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
965 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
966 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
967 MPI2_SGE_FLAGS_SHIFT);
968 ioc->base_add_sg_single(paddr, flags_length, -1);
969}
970
971/**
972 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
973 * @paddr: virtual address for SGE
974 * @flags_length: SGE flags and data transfer length
975 * @dma_addr: Physical address
976 *
977 * Return nothing.
978 */
979static void
980_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
981{
982 Mpi2SGESimple32_t *sgel = paddr;
983
984 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
985 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
986 sgel->FlagsLength = cpu_to_le32(flags_length);
987 sgel->Address = cpu_to_le32(dma_addr);
988}
989
990
991/**
992 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
993 * @paddr: virtual address for SGE
994 * @flags_length: SGE flags and data transfer length
995 * @dma_addr: Physical address
996 *
997 * Return nothing.
998 */
999static void
1000_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1001{
1002 Mpi2SGESimple64_t *sgel = paddr;
1003
1004 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1005 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1006 sgel->FlagsLength = cpu_to_le32(flags_length);
1007 sgel->Address = cpu_to_le64(dma_addr);
1008}
1009
1010#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1011
1012/**
1013 * _base_config_dma_addressing - set dma addressing
1014 * @ioc: per adapter object
1015 * @pdev: PCI device struct
1016 *
1017 * Returns 0 for success, non-zero for failure.
1018 */
1019static int
1020_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1021{
1022 struct sysinfo s;
1023 char *desc = NULL;
1024
1025 if (sizeof(dma_addr_t) > 4) {
1026 const uint64_t required_mask =
1027 dma_get_required_mask(&pdev->dev);
Yang Hongyange9304382009-04-13 14:40:14 -07001028 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1029 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1030 DMA_BIT_MASK(64))) {
Eric Moore635374e2009-03-09 01:21:12 -06001031 ioc->base_add_sg_single = &_base_add_sg_single_64;
1032 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1033 desc = "64";
1034 goto out;
1035 }
1036 }
1037
Yang Hongyange9304382009-04-13 14:40:14 -07001038 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1039 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eric Moore635374e2009-03-09 01:21:12 -06001040 ioc->base_add_sg_single = &_base_add_sg_single_32;
1041 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1042 desc = "32";
1043 } else
1044 return -ENODEV;
1045
1046 out:
1047 si_meminfo(&s);
1048 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1049 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1050
1051 return 0;
1052}
1053
1054/**
1055 * _base_save_msix_table - backup msix vector table
1056 * @ioc: per adapter object
1057 *
1058 * This address an errata where diag reset clears out the table
1059 */
1060static void
1061_base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
1062{
1063 int i;
1064
1065 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1066 return;
1067
1068 for (i = 0; i < ioc->msix_vector_count; i++)
1069 ioc->msix_table_backup[i] = ioc->msix_table[i];
1070}
1071
1072/**
1073 * _base_restore_msix_table - this restores the msix vector table
1074 * @ioc: per adapter object
1075 *
1076 */
1077static void
1078_base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
1079{
1080 int i;
1081
1082 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1083 return;
1084
1085 for (i = 0; i < ioc->msix_vector_count; i++)
1086 ioc->msix_table[i] = ioc->msix_table_backup[i];
1087}
1088
1089/**
1090 * _base_check_enable_msix - checks MSIX capabable.
1091 * @ioc: per adapter object
1092 *
1093 * Check to see if card is capable of MSIX, and set number
1094 * of avaliable msix vectors
1095 */
1096static int
1097_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1098{
1099 int base;
1100 u16 message_control;
1101 u32 msix_table_offset;
1102
1103 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1104 if (!base) {
1105 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1106 "supported\n", ioc->name));
1107 return -EINVAL;
1108 }
1109
1110 /* get msix vector count */
1111 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1112 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1113
1114 /* get msix table */
1115 pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
1116 msix_table_offset &= 0xFFFFFFF8;
1117 ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
1118
1119 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1120 "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
1121 ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
1122 return 0;
1123}
1124
1125/**
1126 * _base_disable_msix - disables msix
1127 * @ioc: per adapter object
1128 *
1129 */
1130static void
1131_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1132{
1133 if (ioc->msix_enable) {
1134 pci_disable_msix(ioc->pdev);
1135 kfree(ioc->msix_table_backup);
1136 ioc->msix_table_backup = NULL;
1137 ioc->msix_enable = 0;
1138 }
1139}
1140
1141/**
1142 * _base_enable_msix - enables msix, failback to io_apic
1143 * @ioc: per adapter object
1144 *
1145 */
1146static int
1147_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1148{
1149 struct msix_entry entries;
1150 int r;
1151 u8 try_msix = 0;
1152
1153 if (msix_disable == -1 || msix_disable == 0)
1154 try_msix = 1;
1155
1156 if (!try_msix)
1157 goto try_ioapic;
1158
1159 if (_base_check_enable_msix(ioc) != 0)
1160 goto try_ioapic;
1161
1162 ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
1163 sizeof(u32), GFP_KERNEL);
1164 if (!ioc->msix_table_backup) {
1165 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
1166 "msix_table_backup failed!!!\n", ioc->name));
1167 goto try_ioapic;
1168 }
1169
1170 memset(&entries, 0, sizeof(struct msix_entry));
1171 r = pci_enable_msix(ioc->pdev, &entries, 1);
1172 if (r) {
1173 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1174 "failed (r=%d) !!!\n", ioc->name, r));
1175 goto try_ioapic;
1176 }
1177
1178 r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
1179 ioc->name, ioc);
1180 if (r) {
1181 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
1182 "interrupt %d !!!\n", ioc->name, entries.vector));
1183 pci_disable_msix(ioc->pdev);
1184 goto try_ioapic;
1185 }
1186
1187 ioc->pci_irq = entries.vector;
1188 ioc->msix_enable = 1;
1189 return 0;
1190
1191/* failback to io_apic interrupt routing */
1192 try_ioapic:
1193
1194 r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
1195 ioc->name, ioc);
1196 if (r) {
1197 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1198 ioc->name, ioc->pdev->irq);
1199 r = -EBUSY;
1200 goto out_fail;
1201 }
1202
1203 ioc->pci_irq = ioc->pdev->irq;
1204 return 0;
1205
1206 out_fail:
1207 return r;
1208}
1209
1210/**
1211 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1212 * @ioc: per adapter object
1213 *
1214 * Returns 0 for success, non-zero for failure.
1215 */
1216int
1217mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1218{
1219 struct pci_dev *pdev = ioc->pdev;
1220 u32 memap_sz;
1221 u32 pio_sz;
1222 int i, r = 0;
1223
1224 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n",
1225 ioc->name, __func__));
1226
1227 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1228 if (pci_enable_device_mem(pdev)) {
1229 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1230 "failed\n", ioc->name);
1231 return -ENODEV;
1232 }
1233
1234
1235 if (pci_request_selected_regions(pdev, ioc->bars,
1236 MPT2SAS_DRIVER_NAME)) {
1237 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1238 "failed\n", ioc->name);
1239 r = -ENODEV;
1240 goto out_fail;
1241 }
1242
1243 pci_set_master(pdev);
1244
1245 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1246 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1247 ioc->name, pci_name(pdev));
1248 r = -ENODEV;
1249 goto out_fail;
1250 }
1251
1252 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
1253 if (pci_resource_flags(pdev, i) & PCI_BASE_ADDRESS_SPACE_IO) {
1254 if (pio_sz)
1255 continue;
1256 ioc->pio_chip = pci_resource_start(pdev, i);
1257 pio_sz = pci_resource_len(pdev, i);
1258 } else {
1259 if (memap_sz)
1260 continue;
1261 ioc->chip_phys = pci_resource_start(pdev, i);
1262 memap_sz = pci_resource_len(pdev, i);
1263 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1264 if (ioc->chip == NULL) {
1265 printk(MPT2SAS_ERR_FMT "unable to map adapter "
1266 "memory!\n", ioc->name);
1267 r = -EINVAL;
1268 goto out_fail;
1269 }
1270 }
1271 }
1272
Eric Moore635374e2009-03-09 01:21:12 -06001273 _base_mask_interrupts(ioc);
1274 r = _base_enable_msix(ioc);
1275 if (r)
1276 goto out_fail;
1277
1278 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1279 ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1280 "IO-APIC enabled"), ioc->pci_irq);
1281 printk(MPT2SAS_INFO_FMT "iomem(0x%lx), mapped(0x%p), size(%d)\n",
1282 ioc->name, ioc->chip_phys, ioc->chip, memap_sz);
1283 printk(MPT2SAS_INFO_FMT "ioport(0x%lx), size(%d)\n",
1284 ioc->name, ioc->pio_chip, pio_sz);
1285
1286 return 0;
1287
1288 out_fail:
1289 if (ioc->chip_phys)
1290 iounmap(ioc->chip);
1291 ioc->chip_phys = 0;
1292 ioc->pci_irq = -1;
1293 pci_release_selected_regions(ioc->pdev, ioc->bars);
1294 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001295 return r;
1296}
1297
1298/**
Eric Moore635374e2009-03-09 01:21:12 -06001299 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1300 * @ioc: per adapter object
1301 * @smid: system request message index(smid zero is invalid)
1302 *
1303 * Returns virt pointer to message frame.
1304 */
1305void *
1306mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1307{
1308 return (void *)(ioc->request + (smid * ioc->request_sz));
1309}
1310
1311/**
1312 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1313 * @ioc: per adapter object
1314 * @smid: system request message index
1315 *
1316 * Returns virt pointer to sense buffer.
1317 */
1318void *
1319mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1320{
1321 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1322}
1323
1324/**
1325 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1326 * @ioc: per adapter object
1327 * @smid: system request message index
1328 *
1329 * Returns phys pointer to sense buffer.
1330 */
1331dma_addr_t
1332mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1333{
1334 return ioc->sense_dma + ((smid - 1) * SCSI_SENSE_BUFFERSIZE);
1335}
1336
1337/**
1338 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1339 * @ioc: per adapter object
1340 * @phys_addr: lower 32 physical addr of the reply
1341 *
1342 * Converts 32bit lower physical addr into a virt address.
1343 */
1344void *
1345mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1346{
1347 if (!phys_addr)
1348 return NULL;
1349 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1350}
1351
1352/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301353 * mpt2sas_base_get_smid - obtain a free smid from internal queue
Eric Moore635374e2009-03-09 01:21:12 -06001354 * @ioc: per adapter object
1355 * @cb_idx: callback index
1356 *
1357 * Returns smid (zero is invalid)
1358 */
1359u16
1360mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1361{
1362 unsigned long flags;
1363 struct request_tracker *request;
1364 u16 smid;
1365
1366 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301367 if (list_empty(&ioc->internal_free_list)) {
1368 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1369 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1370 ioc->name, __func__);
1371 return 0;
1372 }
1373
1374 request = list_entry(ioc->internal_free_list.next,
1375 struct request_tracker, tracker_list);
1376 request->cb_idx = cb_idx;
1377 smid = request->smid;
1378 list_del(&request->tracker_list);
1379 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1380 return smid;
1381}
1382
1383/**
1384 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1385 * @ioc: per adapter object
1386 * @cb_idx: callback index
1387 * @scmd: pointer to scsi command object
1388 *
1389 * Returns smid (zero is invalid)
1390 */
1391u16
1392mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1393 struct scsi_cmnd *scmd)
1394{
1395 unsigned long flags;
1396 struct request_tracker *request;
1397 u16 smid;
1398
1399 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001400 if (list_empty(&ioc->free_list)) {
1401 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1402 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1403 ioc->name, __func__);
1404 return 0;
1405 }
1406
1407 request = list_entry(ioc->free_list.next,
1408 struct request_tracker, tracker_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301409 request->scmd = scmd;
1410 request->cb_idx = cb_idx;
1411 smid = request->smid;
1412 list_del(&request->tracker_list);
1413 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1414 return smid;
1415}
1416
1417/**
1418 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1419 * @ioc: per adapter object
1420 * @cb_idx: callback index
1421 *
1422 * Returns smid (zero is invalid)
1423 */
1424u16
1425mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1426{
1427 unsigned long flags;
1428 struct request_tracker *request;
1429 u16 smid;
1430
1431 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1432 if (list_empty(&ioc->hpr_free_list)) {
1433 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1434 return 0;
1435 }
1436
1437 request = list_entry(ioc->hpr_free_list.next,
1438 struct request_tracker, tracker_list);
Eric Moore635374e2009-03-09 01:21:12 -06001439 request->cb_idx = cb_idx;
1440 smid = request->smid;
1441 list_del(&request->tracker_list);
1442 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1443 return smid;
1444}
1445
1446
1447/**
1448 * mpt2sas_base_free_smid - put smid back on free_list
1449 * @ioc: per adapter object
1450 * @smid: system request message index
1451 *
1452 * Return nothing.
1453 */
1454void
1455mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1456{
1457 unsigned long flags;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301458 int i;
Eric Moore635374e2009-03-09 01:21:12 -06001459
1460 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301461 if (smid >= ioc->hi_priority_smid) {
1462 if (smid < ioc->internal_smid) {
1463 /* hi-priority */
1464 i = smid - ioc->hi_priority_smid;
1465 ioc->hpr_lookup[i].cb_idx = 0xFF;
1466 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1467 &ioc->hpr_free_list);
1468 } else {
1469 /* internal queue */
1470 i = smid - ioc->internal_smid;
1471 ioc->internal_lookup[i].cb_idx = 0xFF;
1472 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1473 &ioc->internal_free_list);
1474 }
1475 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1476 return;
1477 }
1478
1479 /* scsiio queue */
1480 i = smid - 1;
1481 ioc->scsi_lookup[i].cb_idx = 0xFF;
1482 ioc->scsi_lookup[i].scmd = NULL;
1483 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
Eric Moore635374e2009-03-09 01:21:12 -06001484 &ioc->free_list);
1485 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1486
1487 /*
1488 * See _wait_for_commands_to_complete() call with regards to this code.
1489 */
1490 if (ioc->shost_recovery && ioc->pending_io_count) {
1491 if (ioc->pending_io_count == 1)
1492 wake_up(&ioc->reset_wq);
1493 ioc->pending_io_count--;
1494 }
1495}
1496
1497/**
1498 * _base_writeq - 64 bit write to MMIO
1499 * @ioc: per adapter object
1500 * @b: data payload
1501 * @addr: address in MMIO space
1502 * @writeq_lock: spin lock
1503 *
1504 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1505 * care of 32 bit environment where its not quarenteed to send the entire word
1506 * in one transfer.
1507 */
1508#ifndef writeq
1509static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1510 spinlock_t *writeq_lock)
1511{
1512 unsigned long flags;
1513 __u64 data_out = cpu_to_le64(b);
1514
1515 spin_lock_irqsave(writeq_lock, flags);
1516 writel((u32)(data_out), addr);
1517 writel((u32)(data_out >> 32), (addr + 4));
1518 spin_unlock_irqrestore(writeq_lock, flags);
1519}
1520#else
1521static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1522 spinlock_t *writeq_lock)
1523{
1524 writeq(cpu_to_le64(b), addr);
1525}
1526#endif
1527
1528/**
1529 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1530 * @ioc: per adapter object
1531 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001532 * @handle: device handle
1533 *
1534 * Return nothing.
1535 */
1536void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301537mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06001538{
1539 Mpi2RequestDescriptorUnion_t descriptor;
1540 u64 *request = (u64 *)&descriptor;
1541
1542
1543 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301544 descriptor.SCSIIO.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001545 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1546 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1547 descriptor.SCSIIO.LMID = 0;
1548 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1549 &ioc->scsi_lookup_lock);
1550}
1551
1552
1553/**
1554 * mpt2sas_base_put_smid_hi_priority - send Task Managment request to firmware
1555 * @ioc: per adapter object
1556 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001557 *
1558 * Return nothing.
1559 */
1560void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301561mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001562{
1563 Mpi2RequestDescriptorUnion_t descriptor;
1564 u64 *request = (u64 *)&descriptor;
1565
1566 descriptor.HighPriority.RequestFlags =
1567 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301568 descriptor.HighPriority.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001569 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1570 descriptor.HighPriority.LMID = 0;
1571 descriptor.HighPriority.Reserved1 = 0;
1572 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1573 &ioc->scsi_lookup_lock);
1574}
1575
1576/**
1577 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1578 * @ioc: per adapter object
1579 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001580 *
1581 * Return nothing.
1582 */
1583void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301584mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001585{
1586 Mpi2RequestDescriptorUnion_t descriptor;
1587 u64 *request = (u64 *)&descriptor;
1588
1589 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301590 descriptor.Default.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001591 descriptor.Default.SMID = cpu_to_le16(smid);
1592 descriptor.Default.LMID = 0;
1593 descriptor.Default.DescriptorTypeDependent = 0;
1594 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1595 &ioc->scsi_lookup_lock);
1596}
1597
1598/**
1599 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1600 * @ioc: per adapter object
1601 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001602 * @io_index: value used to track the IO
1603 *
1604 * Return nothing.
1605 */
1606void
1607mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301608 u16 io_index)
Eric Moore635374e2009-03-09 01:21:12 -06001609{
1610 Mpi2RequestDescriptorUnion_t descriptor;
1611 u64 *request = (u64 *)&descriptor;
1612
1613 descriptor.SCSITarget.RequestFlags =
1614 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301615 descriptor.SCSITarget.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001616 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1617 descriptor.SCSITarget.LMID = 0;
1618 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1619 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1620 &ioc->scsi_lookup_lock);
1621}
1622
1623/**
Eric Mooref0f9cc12009-04-21 15:40:48 -06001624 * _base_display_dell_branding - Disply branding string
1625 * @ioc: per adapter object
1626 *
1627 * Return nothing.
1628 */
1629static void
1630_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1631{
1632 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1633
1634 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1635 return;
1636
1637 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1638 switch (ioc->pdev->subsystem_device) {
1639 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1640 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1641 MPT2SAS_DELL_BRANDING_SIZE - 1);
1642 break;
1643 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1644 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1645 MPT2SAS_DELL_BRANDING_SIZE - 1);
1646 break;
1647 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1648 strncpy(dell_branding,
1649 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1650 MPT2SAS_DELL_BRANDING_SIZE - 1);
1651 break;
1652 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1653 strncpy(dell_branding,
1654 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1655 MPT2SAS_DELL_BRANDING_SIZE - 1);
1656 break;
1657 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1658 strncpy(dell_branding,
1659 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1660 MPT2SAS_DELL_BRANDING_SIZE - 1);
1661 break;
1662 case MPT2SAS_DELL_PERC_H200_SSDID:
1663 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1664 MPT2SAS_DELL_BRANDING_SIZE - 1);
1665 break;
1666 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1667 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1668 MPT2SAS_DELL_BRANDING_SIZE - 1);
1669 break;
1670 default:
1671 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1672 break;
1673 }
1674
1675 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1676 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1677 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1678 ioc->pdev->subsystem_device);
1679}
1680
1681/**
Eric Moore635374e2009-03-09 01:21:12 -06001682 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1683 * @ioc: per adapter object
1684 *
1685 * Return nothing.
1686 */
1687static void
1688_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1689{
1690 int i = 0;
1691 char desc[16];
1692 u8 revision;
1693 u32 iounit_pg1_flags;
1694
1695 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1696 strncpy(desc, ioc->manu_pg0.ChipName, 16);
1697 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
1698 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
1699 ioc->name, desc,
1700 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
1701 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
1702 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
1703 ioc->facts.FWVersion.Word & 0x000000FF,
1704 revision,
1705 (ioc->bios_pg3.BiosVersion & 0xFF000000) >> 24,
1706 (ioc->bios_pg3.BiosVersion & 0x00FF0000) >> 16,
1707 (ioc->bios_pg3.BiosVersion & 0x0000FF00) >> 8,
1708 ioc->bios_pg3.BiosVersion & 0x000000FF);
1709
Kashyap, Desaied79f122009-08-20 13:23:49 +05301710 _base_display_dell_branding(ioc);
1711
Eric Moore635374e2009-03-09 01:21:12 -06001712 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
1713
1714 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
1715 printk("Initiator");
1716 i++;
1717 }
1718
1719 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
1720 printk("%sTarget", i ? "," : "");
1721 i++;
1722 }
1723
1724 i = 0;
1725 printk("), ");
1726 printk("Capabilities=(");
1727
1728 if (ioc->facts.IOCCapabilities &
1729 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
1730 printk("Raid");
1731 i++;
1732 }
1733
1734 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
1735 printk("%sTLR", i ? "," : "");
1736 i++;
1737 }
1738
1739 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
1740 printk("%sMulticast", i ? "," : "");
1741 i++;
1742 }
1743
1744 if (ioc->facts.IOCCapabilities &
1745 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
1746 printk("%sBIDI Target", i ? "," : "");
1747 i++;
1748 }
1749
1750 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
1751 printk("%sEEDP", i ? "," : "");
1752 i++;
1753 }
1754
1755 if (ioc->facts.IOCCapabilities &
1756 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
1757 printk("%sSnapshot Buffer", i ? "," : "");
1758 i++;
1759 }
1760
1761 if (ioc->facts.IOCCapabilities &
1762 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
1763 printk("%sDiag Trace Buffer", i ? "," : "");
1764 i++;
1765 }
1766
1767 if (ioc->facts.IOCCapabilities &
1768 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
1769 printk("%sTask Set Full", i ? "," : "");
1770 i++;
1771 }
1772
1773 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1774 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
1775 printk("%sNCQ", i ? "," : "");
1776 i++;
1777 }
1778
1779 printk(")\n");
1780}
1781
1782/**
1783 * _base_static_config_pages - static start of day config pages
1784 * @ioc: per adapter object
1785 *
1786 * Return nothing.
1787 */
1788static void
1789_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
1790{
1791 Mpi2ConfigReply_t mpi_reply;
1792 u32 iounit_pg1_flags;
1793
1794 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
Kashyap, Desaied79f122009-08-20 13:23:49 +05301795 if (ioc->ir_firmware)
1796 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
1797 &ioc->manu_pg10);
Eric Moore635374e2009-03-09 01:21:12 -06001798 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
1799 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
1800 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
1801 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
1802 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
1803 _base_display_ioc_capabilities(ioc);
1804
1805 /*
1806 * Enable task_set_full handling in iounit_pg1 when the
1807 * facts capabilities indicate that its supported.
1808 */
1809 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1810 if ((ioc->facts.IOCCapabilities &
1811 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
1812 iounit_pg1_flags &=
1813 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1814 else
1815 iounit_pg1_flags |=
1816 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1817 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
Kashyap, Desai5b768582009-08-20 13:24:31 +05301818 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Eric Moore635374e2009-03-09 01:21:12 -06001819}
1820
1821/**
1822 * _base_release_memory_pools - release memory
1823 * @ioc: per adapter object
1824 *
1825 * Free memory allocated from _base_allocate_memory_pools.
1826 *
1827 * Return nothing.
1828 */
1829static void
1830_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
1831{
1832 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1833 __func__));
1834
1835 if (ioc->request) {
1836 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
1837 ioc->request, ioc->request_dma);
1838 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
1839 ": free\n", ioc->name, ioc->request));
1840 ioc->request = NULL;
1841 }
1842
1843 if (ioc->sense) {
1844 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
1845 if (ioc->sense_dma_pool)
1846 pci_pool_destroy(ioc->sense_dma_pool);
1847 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
1848 ": free\n", ioc->name, ioc->sense));
1849 ioc->sense = NULL;
1850 }
1851
1852 if (ioc->reply) {
1853 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
1854 if (ioc->reply_dma_pool)
1855 pci_pool_destroy(ioc->reply_dma_pool);
1856 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
1857 ": free\n", ioc->name, ioc->reply));
1858 ioc->reply = NULL;
1859 }
1860
1861 if (ioc->reply_free) {
1862 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
1863 ioc->reply_free_dma);
1864 if (ioc->reply_free_dma_pool)
1865 pci_pool_destroy(ioc->reply_free_dma_pool);
1866 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
1867 "(0x%p): free\n", ioc->name, ioc->reply_free));
1868 ioc->reply_free = NULL;
1869 }
1870
1871 if (ioc->reply_post_free) {
1872 pci_pool_free(ioc->reply_post_free_dma_pool,
1873 ioc->reply_post_free, ioc->reply_post_free_dma);
1874 if (ioc->reply_post_free_dma_pool)
1875 pci_pool_destroy(ioc->reply_post_free_dma_pool);
1876 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
1877 "reply_post_free_pool(0x%p): free\n", ioc->name,
1878 ioc->reply_post_free));
1879 ioc->reply_post_free = NULL;
1880 }
1881
1882 if (ioc->config_page) {
1883 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
1884 "config_page(0x%p): free\n", ioc->name,
1885 ioc->config_page));
1886 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
1887 ioc->config_page, ioc->config_page_dma);
1888 }
1889
1890 kfree(ioc->scsi_lookup);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301891 kfree(ioc->hpr_lookup);
1892 kfree(ioc->internal_lookup);
Eric Moore635374e2009-03-09 01:21:12 -06001893}
1894
1895
1896/**
1897 * _base_allocate_memory_pools - allocate start of day memory pools
1898 * @ioc: per adapter object
1899 * @sleep_flag: CAN_SLEEP or NO_SLEEP
1900 *
1901 * Returns 0 success, anything else error
1902 */
1903static int
1904_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
1905{
1906 Mpi2IOCFactsReply_t *facts;
1907 u32 queue_size, queue_diff;
1908 u16 max_sge_elements;
1909 u16 num_of_reply_frames;
1910 u16 chains_needed_per_io;
1911 u32 sz, total_sz;
Eric Moore635374e2009-03-09 01:21:12 -06001912 u32 retry_sz;
1913 u16 max_request_credit;
1914
1915 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
1916 __func__));
1917
1918 retry_sz = 0;
1919 facts = &ioc->facts;
1920
1921 /* command line tunables for max sgl entries */
1922 if (max_sgl_entries != -1) {
1923 ioc->shost->sg_tablesize = (max_sgl_entries <
1924 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
1925 MPT2SAS_SG_DEPTH;
1926 } else {
1927 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
1928 }
1929
1930 /* command line tunables for max controller queue depth */
1931 if (max_queue_depth != -1) {
1932 max_request_credit = (max_queue_depth < facts->RequestCredit)
1933 ? max_queue_depth : facts->RequestCredit;
1934 } else {
1935 max_request_credit = (facts->RequestCredit >
1936 MPT2SAS_MAX_REQUEST_QUEUE) ? MPT2SAS_MAX_REQUEST_QUEUE :
1937 facts->RequestCredit;
1938 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301939
1940 ioc->hba_queue_depth = max_request_credit;
1941 ioc->hi_priority_depth = facts->HighPriorityCredit;
1942 ioc->internal_depth = ioc->hi_priority_depth + 5;
Eric Moore635374e2009-03-09 01:21:12 -06001943
1944 /* request frame size */
1945 ioc->request_sz = facts->IOCRequestFrameSize * 4;
1946
1947 /* reply frame size */
1948 ioc->reply_sz = facts->ReplyFrameSize * 4;
1949
1950 retry_allocation:
1951 total_sz = 0;
1952 /* calculate number of sg elements left over in the 1st frame */
1953 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
1954 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
1955 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
1956
1957 /* now do the same for a chain buffer */
1958 max_sge_elements = ioc->request_sz - ioc->sge_size;
1959 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
1960
1961 ioc->chain_offset_value_for_main_message =
1962 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
1963 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
1964
1965 /*
1966 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
1967 */
1968 chains_needed_per_io = ((ioc->shost->sg_tablesize -
1969 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
1970 + 1;
1971 if (chains_needed_per_io > facts->MaxChainDepth) {
1972 chains_needed_per_io = facts->MaxChainDepth;
1973 ioc->shost->sg_tablesize = min_t(u16,
1974 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
1975 * chains_needed_per_io), ioc->shost->sg_tablesize);
1976 }
1977 ioc->chains_needed_per_io = chains_needed_per_io;
1978
1979 /* reply free queue sizing - taking into account for events */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301980 num_of_reply_frames = ioc->hba_queue_depth + 32;
Eric Moore635374e2009-03-09 01:21:12 -06001981
1982 /* number of replies frames can't be a multiple of 16 */
1983 /* decrease number of reply frames by 1 */
1984 if (!(num_of_reply_frames % 16))
1985 num_of_reply_frames--;
1986
1987 /* calculate number of reply free queue entries
1988 * (must be multiple of 16)
1989 */
1990
1991 /* (we know reply_free_queue_depth is not a multiple of 16) */
1992 queue_size = num_of_reply_frames;
1993 queue_size += 16 - (queue_size % 16);
1994 ioc->reply_free_queue_depth = queue_size;
1995
1996 /* reply descriptor post queue sizing */
1997 /* this size should be the number of request frames + number of reply
1998 * frames
1999 */
2000
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302001 queue_size = ioc->hba_queue_depth + num_of_reply_frames + 1;
Eric Moore635374e2009-03-09 01:21:12 -06002002 /* round up to 16 byte boundary */
2003 if (queue_size % 16)
2004 queue_size += 16 - (queue_size % 16);
2005
2006 /* check against IOC maximum reply post queue depth */
2007 if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
2008 queue_diff = queue_size -
2009 facts->MaxReplyDescriptorPostQueueDepth;
2010
2011 /* round queue_diff up to multiple of 16 */
2012 if (queue_diff % 16)
2013 queue_diff += 16 - (queue_diff % 16);
2014
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302015 /* adjust hba_queue_depth, reply_free_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002016 * and queue_size
2017 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302018 ioc->hba_queue_depth -= queue_diff;
Eric Moore635374e2009-03-09 01:21:12 -06002019 ioc->reply_free_queue_depth -= queue_diff;
2020 queue_size -= queue_diff;
2021 }
2022 ioc->reply_post_queue_depth = queue_size;
2023
Eric Moore635374e2009-03-09 01:21:12 -06002024 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2025 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2026 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2027 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2028 ioc->chains_needed_per_io));
2029
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302030 ioc->scsiio_depth = ioc->hba_queue_depth -
2031 ioc->hi_priority_depth - ioc->internal_depth;
2032
2033 /* set the scsi host can_queue depth
2034 * with some internal commands that could be outstanding
2035 */
2036 ioc->shost->can_queue = ioc->scsiio_depth - (2);
2037 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2038 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2039
Eric Moore635374e2009-03-09 01:21:12 -06002040 /* contiguous pool for request and chains, 16 byte align, one extra "
2041 * "frame for smid=0
2042 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302043 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
2044 sz = ((ioc->scsiio_depth + 1 + ioc->chain_depth) * ioc->request_sz);
2045
2046 /* hi-priority queue */
2047 sz += (ioc->hi_priority_depth * ioc->request_sz);
2048
2049 /* internal queue */
2050 sz += (ioc->internal_depth * ioc->request_sz);
Eric Moore635374e2009-03-09 01:21:12 -06002051
2052 ioc->request_dma_sz = sz;
2053 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2054 if (!ioc->request) {
2055 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302056 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2057 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002058 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302059 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
Eric Moore635374e2009-03-09 01:21:12 -06002060 goto out;
2061 retry_sz += 64;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302062 ioc->hba_queue_depth = max_request_credit - retry_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002063 goto retry_allocation;
2064 }
2065
2066 if (retry_sz)
2067 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302068 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2069 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002070 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2071
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302072
2073 /* hi-priority queue */
2074 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002075 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302076 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002077 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302078
2079 /* internal queue */
2080 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2081 ioc->request_sz);
2082 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2083 ioc->request_sz);
2084
2085 ioc->chain = ioc->internal + (ioc->internal_depth *
2086 ioc->request_sz);
2087 ioc->chain_dma = ioc->internal_dma + (ioc->internal_depth *
2088 ioc->request_sz);
2089
Eric Moore635374e2009-03-09 01:21:12 -06002090 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2091 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302092 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2093 (ioc->hba_queue_depth * ioc->request_sz)/1024));
Eric Moore635374e2009-03-09 01:21:12 -06002094 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool(0x%p): depth"
2095 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->chain,
2096 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2097 ioc->request_sz))/1024));
2098 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2099 ioc->name, (unsigned long long) ioc->request_dma));
2100 total_sz += sz;
2101
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302102 ioc->scsi_lookup = kcalloc(ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002103 sizeof(struct request_tracker), GFP_KERNEL);
2104 if (!ioc->scsi_lookup) {
2105 printk(MPT2SAS_ERR_FMT "scsi_lookup: kcalloc failed\n",
2106 ioc->name);
2107 goto out;
2108 }
2109
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302110 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2111 "depth(%d)\n", ioc->name, ioc->request,
2112 ioc->scsiio_depth));
2113
2114 /* initialize hi-priority queue smid's */
2115 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2116 sizeof(struct request_tracker), GFP_KERNEL);
2117 if (!ioc->hpr_lookup) {
2118 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2119 ioc->name);
2120 goto out;
2121 }
2122 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2123 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2124 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2125 ioc->hi_priority_depth, ioc->hi_priority_smid));
2126
2127 /* initialize internal queue smid's */
2128 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2129 sizeof(struct request_tracker), GFP_KERNEL);
2130 if (!ioc->internal_lookup) {
2131 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2132 ioc->name);
2133 goto out;
2134 }
2135 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2136 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2137 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2138 ioc->internal_depth, ioc->internal_smid));
Eric Moore635374e2009-03-09 01:21:12 -06002139
2140 /* sense buffers, 4 byte align */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302141 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Eric Moore635374e2009-03-09 01:21:12 -06002142 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2143 0);
2144 if (!ioc->sense_dma_pool) {
2145 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2146 ioc->name);
2147 goto out;
2148 }
2149 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2150 &ioc->sense_dma);
2151 if (!ioc->sense) {
2152 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2153 ioc->name);
2154 goto out;
2155 }
2156 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2157 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302158 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002159 SCSI_SENSE_BUFFERSIZE, sz/1024));
2160 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2161 ioc->name, (unsigned long long)ioc->sense_dma));
2162 total_sz += sz;
2163
2164 /* reply pool, 4 byte align */
2165 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2166 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2167 0);
2168 if (!ioc->reply_dma_pool) {
2169 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2170 ioc->name);
2171 goto out;
2172 }
2173 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2174 &ioc->reply_dma);
2175 if (!ioc->reply) {
2176 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2177 ioc->name);
2178 goto out;
2179 }
2180 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2181 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2182 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2183 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2184 ioc->name, (unsigned long long)ioc->reply_dma));
2185 total_sz += sz;
2186
2187 /* reply free queue, 16 byte align */
2188 sz = ioc->reply_free_queue_depth * 4;
2189 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2190 ioc->pdev, sz, 16, 0);
2191 if (!ioc->reply_free_dma_pool) {
2192 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2193 "failed\n", ioc->name);
2194 goto out;
2195 }
2196 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2197 &ioc->reply_free_dma);
2198 if (!ioc->reply_free) {
2199 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2200 "failed\n", ioc->name);
2201 goto out;
2202 }
2203 memset(ioc->reply_free, 0, sz);
2204 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2205 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2206 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2207 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2208 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2209 total_sz += sz;
2210
2211 /* reply post queue, 16 byte align */
2212 sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
2213 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2214 ioc->pdev, sz, 16, 0);
2215 if (!ioc->reply_post_free_dma_pool) {
2216 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2217 "failed\n", ioc->name);
2218 goto out;
2219 }
2220 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2221 GFP_KERNEL, &ioc->reply_post_free_dma);
2222 if (!ioc->reply_post_free) {
2223 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2224 "failed\n", ioc->name);
2225 goto out;
2226 }
2227 memset(ioc->reply_post_free, 0, sz);
2228 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2229 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2230 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2231 sz/1024));
2232 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2233 "(0x%llx)\n", ioc->name, (unsigned long long)
2234 ioc->reply_post_free_dma));
2235 total_sz += sz;
2236
2237 ioc->config_page_sz = 512;
2238 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2239 ioc->config_page_sz, &ioc->config_page_dma);
2240 if (!ioc->config_page) {
2241 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2242 "failed\n", ioc->name);
2243 goto out;
2244 }
2245 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2246 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2247 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2248 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2249 total_sz += ioc->config_page_sz;
2250
2251 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2252 ioc->name, total_sz/1024);
2253 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2254 "Max Controller Queue Depth(%d)\n",
2255 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2256 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2257 ioc->name, ioc->shost->sg_tablesize);
2258 return 0;
2259
2260 out:
2261 _base_release_memory_pools(ioc);
2262 return -ENOMEM;
2263}
2264
2265
2266/**
2267 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2268 * @ioc: Pointer to MPT_ADAPTER structure
2269 * @cooked: Request raw or cooked IOC state
2270 *
2271 * Returns all IOC Doorbell register bits if cooked==0, else just the
2272 * Doorbell bits in MPI_IOC_STATE_MASK.
2273 */
2274u32
2275mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2276{
2277 u32 s, sc;
2278
2279 s = readl(&ioc->chip->Doorbell);
2280 sc = s & MPI2_IOC_STATE_MASK;
2281 return cooked ? sc : s;
2282}
2283
2284/**
2285 * _base_wait_on_iocstate - waiting on a particular ioc state
2286 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2287 * @timeout: timeout in second
2288 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2289 *
2290 * Returns 0 for success, non-zero for failure.
2291 */
2292static int
2293_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2294 int sleep_flag)
2295{
2296 u32 count, cntdn;
2297 u32 current_state;
2298
2299 count = 0;
2300 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2301 do {
2302 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2303 if (current_state == ioc_state)
2304 return 0;
2305 if (count && current_state == MPI2_IOC_STATE_FAULT)
2306 break;
2307 if (sleep_flag == CAN_SLEEP)
2308 msleep(1);
2309 else
2310 udelay(500);
2311 count++;
2312 } while (--cntdn);
2313
2314 return current_state;
2315}
2316
2317/**
2318 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2319 * a write to the doorbell)
2320 * @ioc: per adapter object
2321 * @timeout: timeout in second
2322 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2323 *
2324 * Returns 0 for success, non-zero for failure.
2325 *
2326 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2327 */
2328static int
2329_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2330 int sleep_flag)
2331{
2332 u32 cntdn, count;
2333 u32 int_status;
2334
2335 count = 0;
2336 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2337 do {
2338 int_status = readl(&ioc->chip->HostInterruptStatus);
2339 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2340 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2341 "successfull count(%d), timeout(%d)\n", ioc->name,
2342 __func__, count, timeout));
2343 return 0;
2344 }
2345 if (sleep_flag == CAN_SLEEP)
2346 msleep(1);
2347 else
2348 udelay(500);
2349 count++;
2350 } while (--cntdn);
2351
2352 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2353 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2354 return -EFAULT;
2355}
2356
2357/**
2358 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2359 * @ioc: per adapter object
2360 * @timeout: timeout in second
2361 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2362 *
2363 * Returns 0 for success, non-zero for failure.
2364 *
2365 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2366 * doorbell.
2367 */
2368static int
2369_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2370 int sleep_flag)
2371{
2372 u32 cntdn, count;
2373 u32 int_status;
2374 u32 doorbell;
2375
2376 count = 0;
2377 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2378 do {
2379 int_status = readl(&ioc->chip->HostInterruptStatus);
2380 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
2381 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2382 "successfull count(%d), timeout(%d)\n", ioc->name,
2383 __func__, count, timeout));
2384 return 0;
2385 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2386 doorbell = readl(&ioc->chip->Doorbell);
2387 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2388 MPI2_IOC_STATE_FAULT) {
2389 mpt2sas_base_fault_info(ioc , doorbell);
2390 return -EFAULT;
2391 }
2392 } else if (int_status == 0xFFFFFFFF)
2393 goto out;
2394
2395 if (sleep_flag == CAN_SLEEP)
2396 msleep(1);
2397 else
2398 udelay(500);
2399 count++;
2400 } while (--cntdn);
2401
2402 out:
2403 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2404 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2405 return -EFAULT;
2406}
2407
2408/**
2409 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2410 * @ioc: per adapter object
2411 * @timeout: timeout in second
2412 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2413 *
2414 * Returns 0 for success, non-zero for failure.
2415 *
2416 */
2417static int
2418_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2419 int sleep_flag)
2420{
2421 u32 cntdn, count;
2422 u32 doorbell_reg;
2423
2424 count = 0;
2425 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2426 do {
2427 doorbell_reg = readl(&ioc->chip->Doorbell);
2428 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
2429 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
2430 "successfull count(%d), timeout(%d)\n", ioc->name,
2431 __func__, count, timeout));
2432 return 0;
2433 }
2434 if (sleep_flag == CAN_SLEEP)
2435 msleep(1);
2436 else
2437 udelay(500);
2438 count++;
2439 } while (--cntdn);
2440
2441 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2442 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2443 return -EFAULT;
2444}
2445
2446/**
2447 * _base_send_ioc_reset - send doorbell reset
2448 * @ioc: per adapter object
2449 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2450 * @timeout: timeout in second
2451 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2452 *
2453 * Returns 0 for success, non-zero for failure.
2454 */
2455static int
2456_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2457 int sleep_flag)
2458{
2459 u32 ioc_state;
2460 int r = 0;
2461
2462 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2463 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2464 ioc->name, __func__);
2465 return -EFAULT;
2466 }
2467
2468 if (!(ioc->facts.IOCCapabilities &
2469 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2470 return -EFAULT;
2471
2472 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2473
2474 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2475 &ioc->chip->Doorbell);
2476 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2477 r = -EFAULT;
2478 goto out;
2479 }
2480 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2481 timeout, sleep_flag);
2482 if (ioc_state) {
2483 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2484 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2485 r = -EFAULT;
2486 goto out;
2487 }
2488 out:
2489 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2490 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2491 return r;
2492}
2493
2494/**
2495 * _base_handshake_req_reply_wait - send request thru doorbell interface
2496 * @ioc: per adapter object
2497 * @request_bytes: request length
2498 * @request: pointer having request payload
2499 * @reply_bytes: reply length
2500 * @reply: pointer to reply payload
2501 * @timeout: timeout in second
2502 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2503 *
2504 * Returns 0 for success, non-zero for failure.
2505 */
2506static int
2507_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2508 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2509{
2510 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2511 int i;
2512 u8 failed;
2513 u16 dummy;
2514 u32 *mfp;
2515
2516 /* make sure doorbell is not in use */
2517 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2518 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2519 " (line=%d)\n", ioc->name, __LINE__);
2520 return -EFAULT;
2521 }
2522
2523 /* clear pending doorbell interrupts from previous state changes */
2524 if (readl(&ioc->chip->HostInterruptStatus) &
2525 MPI2_HIS_IOC2SYS_DB_STATUS)
2526 writel(0, &ioc->chip->HostInterruptStatus);
2527
2528 /* send message to ioc */
2529 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2530 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2531 &ioc->chip->Doorbell);
2532
Kashyap, Desai29786e12009-09-14 11:06:21 +05302533 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
Eric Moore635374e2009-03-09 01:21:12 -06002534 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2535 "int failed (line=%d)\n", ioc->name, __LINE__);
2536 return -EFAULT;
2537 }
2538 writel(0, &ioc->chip->HostInterruptStatus);
2539
2540 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2541 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2542 "ack failed (line=%d)\n", ioc->name, __LINE__);
2543 return -EFAULT;
2544 }
2545
2546 /* send message 32-bits at a time */
2547 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2548 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2549 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2550 failed = 1;
2551 }
2552
2553 if (failed) {
2554 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2555 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2556 return -EFAULT;
2557 }
2558
2559 /* now wait for the reply */
2560 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2561 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2562 "int failed (line=%d)\n", ioc->name, __LINE__);
2563 return -EFAULT;
2564 }
2565
2566 /* read the first two 16-bits, it gives the total length of the reply */
2567 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2568 & MPI2_DOORBELL_DATA_MASK);
2569 writel(0, &ioc->chip->HostInterruptStatus);
2570 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2571 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2572 "int failed (line=%d)\n", ioc->name, __LINE__);
2573 return -EFAULT;
2574 }
2575 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2576 & MPI2_DOORBELL_DATA_MASK);
2577 writel(0, &ioc->chip->HostInterruptStatus);
2578
2579 for (i = 2; i < default_reply->MsgLength * 2; i++) {
2580 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2581 printk(MPT2SAS_ERR_FMT "doorbell "
2582 "handshake int failed (line=%d)\n", ioc->name,
2583 __LINE__);
2584 return -EFAULT;
2585 }
2586 if (i >= reply_bytes/2) /* overflow case */
2587 dummy = readl(&ioc->chip->Doorbell);
2588 else
2589 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2590 & MPI2_DOORBELL_DATA_MASK);
2591 writel(0, &ioc->chip->HostInterruptStatus);
2592 }
2593
2594 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
2595 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
2596 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
2597 " (line=%d)\n", ioc->name, __LINE__));
2598 }
2599 writel(0, &ioc->chip->HostInterruptStatus);
2600
2601 if (ioc->logging_level & MPT_DEBUG_INIT) {
2602 mfp = (u32 *)reply;
2603 printk(KERN_DEBUG "\toffset:data\n");
2604 for (i = 0; i < reply_bytes/4; i++)
2605 printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
2606 le32_to_cpu(mfp[i]));
2607 }
2608 return 0;
2609}
2610
2611/**
2612 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
2613 * @ioc: per adapter object
2614 * @mpi_reply: the reply payload from FW
2615 * @mpi_request: the request payload sent to FW
2616 *
2617 * The SAS IO Unit Control Request message allows the host to perform low-level
2618 * operations, such as resets on the PHYs of the IO Unit, also allows the host
2619 * to obtain the IOC assigned device handles for a device if it has other
2620 * identifying information about the device, in addition allows the host to
2621 * remove IOC resources associated with the device.
2622 *
2623 * Returns 0 for success, non-zero for failure.
2624 */
2625int
2626mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
2627 Mpi2SasIoUnitControlReply_t *mpi_reply,
2628 Mpi2SasIoUnitControlRequest_t *mpi_request)
2629{
2630 u16 smid;
2631 u32 ioc_state;
2632 unsigned long timeleft;
2633 u8 issue_reset;
2634 int rc;
2635 void *request;
2636 u16 wait_state_count;
2637
2638 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2639 __func__));
2640
2641 mutex_lock(&ioc->base_cmds.mutex);
2642
2643 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2644 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2645 ioc->name, __func__);
2646 rc = -EAGAIN;
2647 goto out;
2648 }
2649
2650 wait_state_count = 0;
2651 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2652 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2653 if (wait_state_count++ == 10) {
2654 printk(MPT2SAS_ERR_FMT
2655 "%s: failed due to ioc not operational\n",
2656 ioc->name, __func__);
2657 rc = -EFAULT;
2658 goto out;
2659 }
2660 ssleep(1);
2661 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2662 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2663 "operational state(count=%d)\n", ioc->name,
2664 __func__, wait_state_count);
2665 }
2666
2667 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2668 if (!smid) {
2669 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2670 ioc->name, __func__);
2671 rc = -EAGAIN;
2672 goto out;
2673 }
2674
2675 rc = 0;
2676 ioc->base_cmds.status = MPT2_CMD_PENDING;
2677 request = mpt2sas_base_get_msg_frame(ioc, smid);
2678 ioc->base_cmds.smid = smid;
2679 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
2680 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2681 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
2682 ioc->ioc_link_reset_in_progress = 1;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302683 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302684 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06002685 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2686 msecs_to_jiffies(10000));
2687 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2688 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
2689 ioc->ioc_link_reset_in_progress)
2690 ioc->ioc_link_reset_in_progress = 0;
2691 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2692 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2693 ioc->name, __func__);
2694 _debug_dump_mf(mpi_request,
2695 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
2696 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2697 issue_reset = 1;
2698 goto issue_host_reset;
2699 }
2700 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2701 memcpy(mpi_reply, ioc->base_cmds.reply,
2702 sizeof(Mpi2SasIoUnitControlReply_t));
2703 else
2704 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
2705 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2706 goto out;
2707
2708 issue_host_reset:
2709 if (issue_reset)
2710 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2711 FORCE_BIG_HAMMER);
2712 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2713 rc = -EFAULT;
2714 out:
2715 mutex_unlock(&ioc->base_cmds.mutex);
2716 return rc;
2717}
2718
2719
2720/**
2721 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
2722 * @ioc: per adapter object
2723 * @mpi_reply: the reply payload from FW
2724 * @mpi_request: the request payload sent to FW
2725 *
2726 * The SCSI Enclosure Processor request message causes the IOC to
2727 * communicate with SES devices to control LED status signals.
2728 *
2729 * Returns 0 for success, non-zero for failure.
2730 */
2731int
2732mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
2733 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
2734{
2735 u16 smid;
2736 u32 ioc_state;
2737 unsigned long timeleft;
2738 u8 issue_reset;
2739 int rc;
2740 void *request;
2741 u16 wait_state_count;
2742
2743 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2744 __func__));
2745
2746 mutex_lock(&ioc->base_cmds.mutex);
2747
2748 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2749 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2750 ioc->name, __func__);
2751 rc = -EAGAIN;
2752 goto out;
2753 }
2754
2755 wait_state_count = 0;
2756 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2757 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2758 if (wait_state_count++ == 10) {
2759 printk(MPT2SAS_ERR_FMT
2760 "%s: failed due to ioc not operational\n",
2761 ioc->name, __func__);
2762 rc = -EFAULT;
2763 goto out;
2764 }
2765 ssleep(1);
2766 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2767 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2768 "operational state(count=%d)\n", ioc->name,
2769 __func__, wait_state_count);
2770 }
2771
2772 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2773 if (!smid) {
2774 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2775 ioc->name, __func__);
2776 rc = -EAGAIN;
2777 goto out;
2778 }
2779
2780 rc = 0;
2781 ioc->base_cmds.status = MPT2_CMD_PENDING;
2782 request = mpt2sas_base_get_msg_frame(ioc, smid);
2783 ioc->base_cmds.smid = smid;
2784 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302785 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302786 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06002787 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2788 msecs_to_jiffies(10000));
2789 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2790 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2791 ioc->name, __func__);
2792 _debug_dump_mf(mpi_request,
2793 sizeof(Mpi2SepRequest_t)/4);
2794 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2795 issue_reset = 1;
2796 goto issue_host_reset;
2797 }
2798 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2799 memcpy(mpi_reply, ioc->base_cmds.reply,
2800 sizeof(Mpi2SepReply_t));
2801 else
2802 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
2803 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2804 goto out;
2805
2806 issue_host_reset:
2807 if (issue_reset)
2808 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2809 FORCE_BIG_HAMMER);
2810 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2811 rc = -EFAULT;
2812 out:
2813 mutex_unlock(&ioc->base_cmds.mutex);
2814 return rc;
2815}
2816
2817/**
2818 * _base_get_port_facts - obtain port facts reply and save in ioc
2819 * @ioc: per adapter object
2820 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2821 *
2822 * Returns 0 for success, non-zero for failure.
2823 */
2824static int
2825_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
2826{
2827 Mpi2PortFactsRequest_t mpi_request;
2828 Mpi2PortFactsReply_t mpi_reply, *pfacts;
2829 int mpi_reply_sz, mpi_request_sz, r;
2830
2831 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2832 __func__));
2833
2834 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
2835 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
2836 memset(&mpi_request, 0, mpi_request_sz);
2837 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
2838 mpi_request.PortNumber = port;
2839 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2840 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2841
2842 if (r != 0) {
2843 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2844 ioc->name, __func__, r);
2845 return r;
2846 }
2847
2848 pfacts = &ioc->pfacts[port];
2849 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
2850 pfacts->PortNumber = mpi_reply.PortNumber;
2851 pfacts->VP_ID = mpi_reply.VP_ID;
2852 pfacts->VF_ID = mpi_reply.VF_ID;
2853 pfacts->MaxPostedCmdBuffers =
2854 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
2855
2856 return 0;
2857}
2858
2859/**
2860 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
2861 * @ioc: per adapter object
2862 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2863 *
2864 * Returns 0 for success, non-zero for failure.
2865 */
2866static int
2867_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2868{
2869 Mpi2IOCFactsRequest_t mpi_request;
2870 Mpi2IOCFactsReply_t mpi_reply, *facts;
2871 int mpi_reply_sz, mpi_request_sz, r;
2872
2873 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2874 __func__));
2875
2876 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
2877 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
2878 memset(&mpi_request, 0, mpi_request_sz);
2879 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
2880 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
2881 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
2882
2883 if (r != 0) {
2884 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
2885 ioc->name, __func__, r);
2886 return r;
2887 }
2888
2889 facts = &ioc->facts;
2890 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
2891 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
2892 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
2893 facts->VP_ID = mpi_reply.VP_ID;
2894 facts->VF_ID = mpi_reply.VF_ID;
2895 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
2896 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
2897 facts->WhoInit = mpi_reply.WhoInit;
2898 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
2899 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
2900 facts->MaxReplyDescriptorPostQueueDepth =
2901 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
2902 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
2903 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
2904 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
2905 ioc->ir_firmware = 1;
2906 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
2907 facts->IOCRequestFrameSize =
2908 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
2909 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
2910 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
2911 ioc->shost->max_id = -1;
2912 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
2913 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
2914 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
2915 facts->HighPriorityCredit =
2916 le16_to_cpu(mpi_reply.HighPriorityCredit);
2917 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
2918 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
2919
2920 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
2921 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
2922 facts->MaxChainDepth));
2923 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
2924 "reply frame size(%d)\n", ioc->name,
2925 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
2926 return 0;
2927}
2928
2929/**
2930 * _base_send_ioc_init - send ioc_init to firmware
2931 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002932 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2933 *
2934 * Returns 0 for success, non-zero for failure.
2935 */
2936static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302937_base_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06002938{
2939 Mpi2IOCInitRequest_t mpi_request;
2940 Mpi2IOCInitReply_t mpi_reply;
2941 int r;
2942
2943 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
2944 __func__));
2945
2946 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
2947 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
2948 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302949 mpi_request.VF_ID = 0; /* TODO */
2950 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002951 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
2952 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
2953
2954 /* In MPI Revision I (0xA), the SystemReplyFrameSize(offset 0x18) was
2955 * removed and made reserved. For those with older firmware will need
2956 * this fix. It was decided that the Reply and Request frame sizes are
2957 * the same.
2958 */
2959 if ((ioc->facts.HeaderVersion >> 8) < 0xA) {
2960 mpi_request.Reserved7 = cpu_to_le16(ioc->reply_sz);
2961/* mpi_request.SystemReplyFrameSize =
2962 * cpu_to_le16(ioc->reply_sz);
2963 */
2964 }
2965
2966 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
2967 mpi_request.ReplyDescriptorPostQueueDepth =
2968 cpu_to_le16(ioc->reply_post_queue_depth);
2969 mpi_request.ReplyFreeQueueDepth =
2970 cpu_to_le16(ioc->reply_free_queue_depth);
2971
2972#if BITS_PER_LONG > 32
2973 mpi_request.SenseBufferAddressHigh =
2974 cpu_to_le32(ioc->sense_dma >> 32);
2975 mpi_request.SystemReplyAddressHigh =
2976 cpu_to_le32(ioc->reply_dma >> 32);
2977 mpi_request.SystemRequestFrameBaseAddress =
2978 cpu_to_le64(ioc->request_dma);
2979 mpi_request.ReplyFreeQueueAddress =
2980 cpu_to_le64(ioc->reply_free_dma);
2981 mpi_request.ReplyDescriptorPostQueueAddress =
2982 cpu_to_le64(ioc->reply_post_free_dma);
2983#else
2984 mpi_request.SystemRequestFrameBaseAddress =
2985 cpu_to_le32(ioc->request_dma);
2986 mpi_request.ReplyFreeQueueAddress =
2987 cpu_to_le32(ioc->reply_free_dma);
2988 mpi_request.ReplyDescriptorPostQueueAddress =
2989 cpu_to_le32(ioc->reply_post_free_dma);
2990#endif
2991
2992 if (ioc->logging_level & MPT_DEBUG_INIT) {
2993 u32 *mfp;
2994 int i;
2995
2996 mfp = (u32 *)&mpi_request;
2997 printk(KERN_DEBUG "\toffset:data\n");
2998 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
2999 printk(KERN_DEBUG "\t[0x%02x]:%08x\n", i*4,
3000 le32_to_cpu(mfp[i]));
3001 }
3002
3003 r = _base_handshake_req_reply_wait(ioc,
3004 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3005 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3006 sleep_flag);
3007
3008 if (r != 0) {
3009 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3010 ioc->name, __func__, r);
3011 return r;
3012 }
3013
3014 if (mpi_reply.IOCStatus != MPI2_IOCSTATUS_SUCCESS ||
3015 mpi_reply.IOCLogInfo) {
3016 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3017 r = -EIO;
3018 }
3019
3020 return 0;
3021}
3022
3023/**
3024 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3025 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003026 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3027 *
3028 * Returns 0 for success, non-zero for failure.
3029 */
3030static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303031_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003032{
3033 Mpi2PortEnableRequest_t *mpi_request;
3034 u32 ioc_state;
3035 unsigned long timeleft;
3036 int r = 0;
3037 u16 smid;
3038
3039 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3040
3041 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3042 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3043 ioc->name, __func__);
3044 return -EAGAIN;
3045 }
3046
3047 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3048 if (!smid) {
3049 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3050 ioc->name, __func__);
3051 return -EAGAIN;
3052 }
3053
3054 ioc->base_cmds.status = MPT2_CMD_PENDING;
3055 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3056 ioc->base_cmds.smid = smid;
3057 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3058 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303059 mpi_request->VF_ID = 0; /* TODO */
3060 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003061
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303062 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303063 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003064 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3065 300*HZ);
3066 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3067 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3068 ioc->name, __func__);
3069 _debug_dump_mf(mpi_request,
3070 sizeof(Mpi2PortEnableRequest_t)/4);
3071 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3072 r = -EFAULT;
3073 else
3074 r = -ETIME;
3075 goto out;
3076 } else
3077 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
3078 ioc->name, __func__));
3079
3080 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
3081 60, sleep_flag);
3082 if (ioc_state) {
3083 printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
3084 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3085 r = -EFAULT;
3086 }
3087 out:
3088 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3089 printk(MPT2SAS_INFO_FMT "port enable: %s\n",
3090 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3091 return r;
3092}
3093
3094/**
3095 * _base_unmask_events - turn on notification for this event
3096 * @ioc: per adapter object
3097 * @event: firmware event
3098 *
3099 * The mask is stored in ioc->event_masks.
3100 */
3101static void
3102_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3103{
3104 u32 desired_event;
3105
3106 if (event >= 128)
3107 return;
3108
3109 desired_event = (1 << (event % 32));
3110
3111 if (event < 32)
3112 ioc->event_masks[0] &= ~desired_event;
3113 else if (event < 64)
3114 ioc->event_masks[1] &= ~desired_event;
3115 else if (event < 96)
3116 ioc->event_masks[2] &= ~desired_event;
3117 else if (event < 128)
3118 ioc->event_masks[3] &= ~desired_event;
3119}
3120
3121/**
3122 * _base_event_notification - send event notification
3123 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003124 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3125 *
3126 * Returns 0 for success, non-zero for failure.
3127 */
3128static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303129_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003130{
3131 Mpi2EventNotificationRequest_t *mpi_request;
3132 unsigned long timeleft;
3133 u16 smid;
3134 int r = 0;
3135 int i;
3136
3137 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3138 __func__));
3139
3140 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3141 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3142 ioc->name, __func__);
3143 return -EAGAIN;
3144 }
3145
3146 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3147 if (!smid) {
3148 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3149 ioc->name, __func__);
3150 return -EAGAIN;
3151 }
3152 ioc->base_cmds.status = MPT2_CMD_PENDING;
3153 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3154 ioc->base_cmds.smid = smid;
3155 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3156 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303157 mpi_request->VF_ID = 0; /* TODO */
3158 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003159 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3160 mpi_request->EventMasks[i] =
3161 le32_to_cpu(ioc->event_masks[i]);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303162 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303163 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003164 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3165 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3166 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3167 ioc->name, __func__);
3168 _debug_dump_mf(mpi_request,
3169 sizeof(Mpi2EventNotificationRequest_t)/4);
3170 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3171 r = -EFAULT;
3172 else
3173 r = -ETIME;
3174 } else
3175 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: complete\n",
3176 ioc->name, __func__));
3177 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3178 return r;
3179}
3180
3181/**
3182 * mpt2sas_base_validate_event_type - validating event types
3183 * @ioc: per adapter object
3184 * @event: firmware event
3185 *
3186 * This will turn on firmware event notification when application
3187 * ask for that event. We don't mask events that are already enabled.
3188 */
3189void
3190mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3191{
3192 int i, j;
3193 u32 event_mask, desired_event;
3194 u8 send_update_to_fw;
3195
3196 for (i = 0, send_update_to_fw = 0; i <
3197 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3198 event_mask = ~event_type[i];
3199 desired_event = 1;
3200 for (j = 0; j < 32; j++) {
3201 if (!(event_mask & desired_event) &&
3202 (ioc->event_masks[i] & desired_event)) {
3203 ioc->event_masks[i] &= ~desired_event;
3204 send_update_to_fw = 1;
3205 }
3206 desired_event = (desired_event << 1);
3207 }
3208 }
3209
3210 if (!send_update_to_fw)
3211 return;
3212
3213 mutex_lock(&ioc->base_cmds.mutex);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303214 _base_event_notification(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003215 mutex_unlock(&ioc->base_cmds.mutex);
3216}
3217
3218/**
3219 * _base_diag_reset - the "big hammer" start of day reset
3220 * @ioc: per adapter object
3221 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3222 *
3223 * Returns 0 for success, non-zero for failure.
3224 */
3225static int
3226_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3227{
3228 u32 host_diagnostic;
3229 u32 ioc_state;
3230 u32 count;
3231 u32 hcb_size;
3232
3233 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3234
3235 _base_save_msix_table(ioc);
3236
3237 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "clear interrupts\n",
3238 ioc->name));
Eric Moore635374e2009-03-09 01:21:12 -06003239
3240 count = 0;
3241 do {
3242 /* Write magic sequence to WriteSequence register
3243 * Loop until in diagnostic mode
3244 */
3245 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "write magic "
3246 "sequence\n", ioc->name));
3247 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3248 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3249 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3250 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3251 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3252 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3253 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3254
3255 /* wait 100 msec */
3256 if (sleep_flag == CAN_SLEEP)
3257 msleep(100);
3258 else
3259 mdelay(100);
3260
3261 if (count++ > 20)
3262 goto out;
3263
3264 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3265 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "wrote magic "
3266 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3267 ioc->name, count, host_diagnostic));
3268
3269 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3270
3271 hcb_size = readl(&ioc->chip->HCBSize);
3272
3273 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "diag reset: issued\n",
3274 ioc->name));
3275 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3276 &ioc->chip->HostDiagnostic);
3277
3278 /* don't access any registers for 50 milliseconds */
3279 msleep(50);
3280
3281 /* 300 second max wait */
3282 for (count = 0; count < 3000000 ; count++) {
3283
3284 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3285
3286 if (host_diagnostic == 0xFFFFFFFF)
3287 goto out;
3288 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3289 break;
3290
3291 /* wait 100 msec */
3292 if (sleep_flag == CAN_SLEEP)
3293 msleep(1);
3294 else
3295 mdelay(1);
3296 }
3297
3298 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3299
3300 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter "
3301 "assuming the HCB Address points to good F/W\n",
3302 ioc->name));
3303 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3304 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3305 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3306
3307 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT
3308 "re-enable the HCDW\n", ioc->name));
3309 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3310 &ioc->chip->HCBSize);
3311 }
3312
3313 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "restart the adapter\n",
3314 ioc->name));
3315 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3316 &ioc->chip->HostDiagnostic);
3317
3318 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "disable writes to the "
3319 "diagnostic register\n", ioc->name));
3320 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3321
3322 drsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "Wait for FW to go to the "
3323 "READY state\n", ioc->name));
3324 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3325 sleep_flag);
3326 if (ioc_state) {
3327 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3328 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3329 goto out;
3330 }
3331
3332 _base_restore_msix_table(ioc);
3333 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3334 return 0;
3335
3336 out:
3337 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3338 return -EFAULT;
3339}
3340
3341/**
3342 * _base_make_ioc_ready - put controller in READY state
3343 * @ioc: per adapter object
3344 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3345 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3346 *
3347 * Returns 0 for success, non-zero for failure.
3348 */
3349static int
3350_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3351 enum reset_type type)
3352{
3353 u32 ioc_state;
3354
3355 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3356 __func__));
3357
3358 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3359 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: ioc_state(0x%08x)\n",
3360 ioc->name, __func__, ioc_state));
3361
3362 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3363 return 0;
3364
3365 if (ioc_state & MPI2_DOORBELL_USED) {
3366 dhsprintk(ioc, printk(MPT2SAS_DEBUG_FMT "unexpected doorbell "
3367 "active!\n", ioc->name));
3368 goto issue_diag_reset;
3369 }
3370
3371 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3372 mpt2sas_base_fault_info(ioc, ioc_state &
3373 MPI2_DOORBELL_DATA_MASK);
3374 goto issue_diag_reset;
3375 }
3376
3377 if (type == FORCE_BIG_HAMMER)
3378 goto issue_diag_reset;
3379
3380 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3381 if (!(_base_send_ioc_reset(ioc,
3382 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP)))
3383 return 0;
3384
3385 issue_diag_reset:
3386 return _base_diag_reset(ioc, CAN_SLEEP);
3387}
3388
3389/**
3390 * _base_make_ioc_operational - put controller in OPERATIONAL state
3391 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003392 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3393 *
3394 * Returns 0 for success, non-zero for failure.
3395 */
3396static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303397_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003398{
3399 int r, i;
3400 unsigned long flags;
3401 u32 reply_address;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303402 u16 smid;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303403 struct _tr_list *delayed_tr, *delayed_tr_next;
Eric Moore635374e2009-03-09 01:21:12 -06003404
3405 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3406 __func__));
3407
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303408 /* clean the delayed target reset list */
3409 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3410 &ioc->delayed_tr_list, list) {
3411 list_del(&delayed_tr->list);
3412 kfree(delayed_tr);
3413 }
3414
Eric Moore635374e2009-03-09 01:21:12 -06003415 /* initialize the scsi lookup free list */
3416 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3417 INIT_LIST_HEAD(&ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303418 smid = 1;
3419 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06003420 ioc->scsi_lookup[i].cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303421 ioc->scsi_lookup[i].smid = smid;
3422 ioc->scsi_lookup[i].scmd = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003423 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3424 &ioc->free_list);
3425 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303426
3427 /* hi-priority queue */
3428 INIT_LIST_HEAD(&ioc->hpr_free_list);
3429 smid = ioc->hi_priority_smid;
3430 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
3431 ioc->hpr_lookup[i].cb_idx = 0xFF;
3432 ioc->hpr_lookup[i].smid = smid;
3433 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
3434 &ioc->hpr_free_list);
3435 }
3436
3437 /* internal queue */
3438 INIT_LIST_HEAD(&ioc->internal_free_list);
3439 smid = ioc->internal_smid;
3440 for (i = 0; i < ioc->internal_depth; i++, smid++) {
3441 ioc->internal_lookup[i].cb_idx = 0xFF;
3442 ioc->internal_lookup[i].smid = smid;
3443 list_add_tail(&ioc->internal_lookup[i].tracker_list,
3444 &ioc->internal_free_list);
3445 }
Eric Moore635374e2009-03-09 01:21:12 -06003446 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3447
3448 /* initialize Reply Free Queue */
3449 for (i = 0, reply_address = (u32)ioc->reply_dma ;
3450 i < ioc->reply_free_queue_depth ; i++, reply_address +=
3451 ioc->reply_sz)
3452 ioc->reply_free[i] = cpu_to_le32(reply_address);
3453
3454 /* initialize Reply Post Free Queue */
3455 for (i = 0; i < ioc->reply_post_queue_depth; i++)
Eric Moore03ea1112009-04-21 15:37:57 -06003456 ioc->reply_post_free[i].Words = ULLONG_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06003457
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303458 r = _base_send_ioc_init(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003459 if (r)
3460 return r;
3461
3462 /* initialize the index's */
3463 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
3464 ioc->reply_post_host_index = 0;
3465 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
3466 writel(0, &ioc->chip->ReplyPostHostIndex);
3467
3468 _base_unmask_interrupts(ioc);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303469 r = _base_event_notification(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003470 if (r)
3471 return r;
3472
3473 if (sleep_flag == CAN_SLEEP)
3474 _base_static_config_pages(ioc);
3475
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303476 r = _base_send_port_enable(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003477 if (r)
3478 return r;
3479
3480 return r;
3481}
3482
3483/**
3484 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
3485 * @ioc: per adapter object
3486 *
3487 * Return nothing.
3488 */
3489void
3490mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
3491{
3492 struct pci_dev *pdev = ioc->pdev;
3493
3494 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3495 __func__));
3496
3497 _base_mask_interrupts(ioc);
3498 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
3499 if (ioc->pci_irq) {
3500 synchronize_irq(pdev->irq);
3501 free_irq(ioc->pci_irq, ioc);
3502 }
3503 _base_disable_msix(ioc);
3504 if (ioc->chip_phys)
3505 iounmap(ioc->chip);
3506 ioc->pci_irq = -1;
3507 ioc->chip_phys = 0;
3508 pci_release_selected_regions(ioc->pdev, ioc->bars);
3509 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06003510 return;
3511}
3512
3513/**
3514 * mpt2sas_base_attach - attach controller instance
3515 * @ioc: per adapter object
3516 *
3517 * Returns 0 for success, non-zero for failure.
3518 */
3519int
3520mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
3521{
3522 int r, i;
Eric Moore635374e2009-03-09 01:21:12 -06003523
3524 dinitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3525 __func__));
3526
3527 r = mpt2sas_base_map_resources(ioc);
3528 if (r)
3529 return r;
3530
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303531 pci_set_drvdata(ioc->pdev, ioc->shost);
Eric Moore635374e2009-03-09 01:21:12 -06003532 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
3533 if (r)
3534 goto out_free_resources;
3535
3536 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
3537 if (r)
3538 goto out_free_resources;
3539
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303540 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
3541 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
3542 if (!ioc->pfacts)
3543 goto out_free_resources;
3544
3545 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
3546 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
3547 if (r)
3548 goto out_free_resources;
3549 }
3550
Eric Moore635374e2009-03-09 01:21:12 -06003551 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
3552 if (r)
3553 goto out_free_resources;
3554
3555 init_waitqueue_head(&ioc->reset_wq);
3556
3557 /* base internal command bits */
3558 mutex_init(&ioc->base_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003559 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3560 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3561
3562 /* transport internal command bits */
3563 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3564 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
3565 mutex_init(&ioc->transport_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003566
3567 /* task management internal command bits */
3568 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3569 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3570 mutex_init(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003571
3572 /* config page internal command bits */
3573 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3574 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
3575 mutex_init(&ioc->config_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003576
3577 /* ctl module internal command bits */
3578 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3579 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
3580 mutex_init(&ioc->ctl_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003581
3582 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3583 ioc->event_masks[i] = -1;
3584
3585 /* here we enable the events we care about */
3586 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
3587 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
3588 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3589 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3590 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
3591 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
3592 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
3593 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
3594 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
3595 _base_unmask_events(ioc, MPI2_EVENT_TASK_SET_FULL);
3596 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303597 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003598 if (r)
3599 goto out_free_resources;
3600
Kashyap, Desaie4750c92009-08-07 19:37:59 +05303601 mpt2sas_base_start_watchdog(ioc);
Kashyap, Desai32e0eb52009-09-23 17:28:09 +05303602 if (diag_buffer_enable != 0)
3603 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
Eric Moore635374e2009-03-09 01:21:12 -06003604 return 0;
3605
3606 out_free_resources:
3607
3608 ioc->remove_host = 1;
3609 mpt2sas_base_free_resources(ioc);
3610 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303611 pci_set_drvdata(ioc->pdev, NULL);
Eric Moore635374e2009-03-09 01:21:12 -06003612 kfree(ioc->tm_cmds.reply);
3613 kfree(ioc->transport_cmds.reply);
3614 kfree(ioc->config_cmds.reply);
3615 kfree(ioc->base_cmds.reply);
3616 kfree(ioc->ctl_cmds.reply);
3617 kfree(ioc->pfacts);
3618 ioc->ctl_cmds.reply = NULL;
3619 ioc->base_cmds.reply = NULL;
3620 ioc->tm_cmds.reply = NULL;
3621 ioc->transport_cmds.reply = NULL;
3622 ioc->config_cmds.reply = NULL;
3623 ioc->pfacts = NULL;
3624 return r;
3625}
3626
3627
3628/**
3629 * mpt2sas_base_detach - remove controller instance
3630 * @ioc: per adapter object
3631 *
3632 * Return nothing.
3633 */
3634void
3635mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
3636{
Eric Moore635374e2009-03-09 01:21:12 -06003637
3638 dexitprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s\n", ioc->name,
3639 __func__));
3640
Kashyap, Desaie4750c92009-08-07 19:37:59 +05303641 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06003642 mpt2sas_base_free_resources(ioc);
3643 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303644 pci_set_drvdata(ioc->pdev, NULL);
Eric Moore635374e2009-03-09 01:21:12 -06003645 kfree(ioc->pfacts);
3646 kfree(ioc->ctl_cmds.reply);
3647 kfree(ioc->base_cmds.reply);
3648 kfree(ioc->tm_cmds.reply);
3649 kfree(ioc->transport_cmds.reply);
3650 kfree(ioc->config_cmds.reply);
3651}
3652
3653/**
3654 * _base_reset_handler - reset callback handler (for base)
3655 * @ioc: per adapter object
3656 * @reset_phase: phase
3657 *
3658 * The handler for doing any required cleanup or initialization.
3659 *
3660 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
3661 * MPT2_IOC_DONE_RESET
3662 *
3663 * Return nothing.
3664 */
3665static void
3666_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
3667{
3668 switch (reset_phase) {
3669 case MPT2_IOC_PRE_RESET:
3670 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3671 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
3672 break;
3673 case MPT2_IOC_AFTER_RESET:
3674 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3675 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
3676 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
3677 ioc->transport_cmds.status |= MPT2_CMD_RESET;
3678 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
3679 complete(&ioc->transport_cmds.done);
3680 }
3681 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3682 ioc->base_cmds.status |= MPT2_CMD_RESET;
3683 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
3684 complete(&ioc->base_cmds.done);
3685 }
3686 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
3687 ioc->config_cmds.status |= MPT2_CMD_RESET;
3688 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
Kashyap, Desai5b768582009-08-20 13:24:31 +05303689 ioc->config_cmds.smid = USHORT_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06003690 complete(&ioc->config_cmds.done);
3691 }
3692 break;
3693 case MPT2_IOC_DONE_RESET:
3694 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: "
3695 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
3696 break;
3697 }
3698 mpt2sas_scsih_reset_handler(ioc, reset_phase);
3699 mpt2sas_ctl_reset_handler(ioc, reset_phase);
3700}
3701
3702/**
3703 * _wait_for_commands_to_complete - reset controller
3704 * @ioc: Pointer to MPT_ADAPTER structure
3705 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3706 *
3707 * This function waiting(3s) for all pending commands to complete
3708 * prior to putting controller in reset.
3709 */
3710static void
3711_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3712{
3713 u32 ioc_state;
3714 unsigned long flags;
3715 u16 i;
3716
3717 ioc->pending_io_count = 0;
3718 if (sleep_flag != CAN_SLEEP)
3719 return;
3720
3721 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
3722 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
3723 return;
3724
3725 /* pending command count */
3726 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303727 for (i = 0; i < ioc->scsiio_depth; i++)
Eric Moore635374e2009-03-09 01:21:12 -06003728 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
3729 ioc->pending_io_count++;
3730 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3731
3732 if (!ioc->pending_io_count)
3733 return;
3734
3735 /* wait for pending commands to complete */
3736 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 3 * HZ);
3737}
3738
3739/**
3740 * mpt2sas_base_hard_reset_handler - reset controller
3741 * @ioc: Pointer to MPT_ADAPTER structure
3742 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3743 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3744 *
3745 * Returns 0 for success, non-zero for failure.
3746 */
3747int
3748mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3749 enum reset_type type)
3750{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303751 int r;
Eric Moore635374e2009-03-09 01:21:12 -06003752 unsigned long flags;
3753
3754 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: enter\n", ioc->name,
3755 __func__));
3756
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05303757 if (mpt2sas_fwfault_debug)
3758 mpt2sas_halt_firmware(ioc);
3759
Eric Moore635374e2009-03-09 01:21:12 -06003760 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303761 if (ioc->shost_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06003762 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3763 printk(MPT2SAS_ERR_FMT "%s: busy\n",
3764 ioc->name, __func__);
3765 return -EBUSY;
3766 }
Eric Moore635374e2009-03-09 01:21:12 -06003767 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06003768 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
3769
3770 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
3771 _wait_for_commands_to_complete(ioc, sleep_flag);
3772 _base_mask_interrupts(ioc);
3773 r = _base_make_ioc_ready(ioc, sleep_flag, type);
3774 if (r)
3775 goto out;
3776 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303777 r = _base_make_ioc_operational(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003778 if (!r)
3779 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
3780 out:
3781 dtmprintk(ioc, printk(MPT2SAS_DEBUG_FMT "%s: %s\n",
3782 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
3783
3784 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05303785 ioc->shost_recovery = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003786 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05303787
3788 if (!r)
3789 _base_reset_handler(ioc, MPT2_IOC_RUNNING);
Eric Moore635374e2009-03-09 01:21:12 -06003790 return r;
3791}