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