blob: a08e1beaa03cfd9453a312af1dfa00b67c95a0f3 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * This is the Fusion MPT base driver providing common API layer interface
3 * for access to MPT (Message Passing Technology) firmware.
4 *
5 * This code is based on drivers/scsi/mpt2sas/mpt2_base.c
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05306 * Copyright (C) 2007-2010 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06007 * (mailto:DL-MPTFusionLinux@lsi.com)
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * NO WARRANTY
20 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
21 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
22 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
23 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
24 * solely responsible for determining the appropriateness of using and
25 * distributing the Program and assumes all risks associated with its
26 * exercise of rights under this Agreement, including but not limited to
27 * the risks and costs of program errors, damage to or loss of data,
28 * programs or equipment, and unavailability or interruption of operations.
29
30 * DISCLAIMER OF LIABILITY
31 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
34 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
35 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
36 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
37 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
38
39 * You should have received a copy of the GNU General Public License
40 * along with this program; if not, write to the Free Software
41 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
42 * USA.
43 */
44
45#include <linux/version.h>
46#include <linux/kernel.h>
47#include <linux/module.h>
48#include <linux/errno.h>
49#include <linux/init.h>
50#include <linux/slab.h>
51#include <linux/types.h>
52#include <linux/pci.h>
53#include <linux/kdev_t.h>
54#include <linux/blkdev.h>
55#include <linux/delay.h>
56#include <linux/interrupt.h>
57#include <linux/dma-mapping.h>
58#include <linux/sort.h>
59#include <linux/io.h>
Kashyap, Desaia8ebd762009-09-23 17:29:29 +053060#include <linux/time.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053061#include <linux/aer.h>
Eric Moore635374e2009-03-09 01:21:12 -060062
63#include "mpt2sas_base.h"
64
65static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];
66
67#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */
Eric Moore635374e2009-03-09 01:21:12 -060068
69static int max_queue_depth = -1;
70module_param(max_queue_depth, int, 0);
71MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");
72
73static int max_sgl_entries = -1;
74module_param(max_sgl_entries, int, 0);
75MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");
76
77static int msix_disable = -1;
78module_param(msix_disable, int, 0);
79MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");
80
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +053081static int missing_delay[2] = {-1, -1};
82module_param_array(missing_delay, int, NULL, 0);
83MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
84
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053085/* diag_buffer_enable is bitwise
Kashyap, Desai1b01fe32009-09-23 17:28:59 +053086 * bit 0 set = TRACE
87 * bit 1 set = SNAPSHOT
88 * bit 2 set = EXTENDED
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053089 *
90 * Either bit can be set, or both
91 */
92static int diag_buffer_enable;
93module_param(diag_buffer_enable, int, 0);
Kashyap, Desai1b01fe32009-09-23 17:28:59 +053094MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
95 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
Kashyap, Desai32e0eb52009-09-23 17:28:09 +053096
Kashyap, Desaifa7f3162009-09-23 17:26:58 +053097int mpt2sas_fwfault_debug;
98MODULE_PARM_DESC(mpt2sas_fwfault_debug, " enable detection of firmware fault "
99 "and halt firmware - (default=0)");
100
Kashyap, Desaidd5fd332010-06-17 13:31:17 +0530101static int disable_discovery = -1;
102module_param(disable_discovery, int, 0);
103MODULE_PARM_DESC(disable_discovery, " disable discovery ");
104
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530105/**
106 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
107 *
108 */
109static int
110_scsih_set_fwfault_debug(const char *val, struct kernel_param *kp)
111{
112 int ret = param_set_int(val, kp);
113 struct MPT2SAS_ADAPTER *ioc;
114
115 if (ret)
116 return ret;
117
Kashyap, Desaie75b9b62009-12-16 18:52:39 +0530118 printk(KERN_INFO "setting fwfault_debug(%d)\n", mpt2sas_fwfault_debug);
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530119 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
120 ioc->fwfault_debug = mpt2sas_fwfault_debug;
121 return 0;
122}
123module_param_call(mpt2sas_fwfault_debug, _scsih_set_fwfault_debug,
124 param_get_int, &mpt2sas_fwfault_debug, 0644);
125
Eric Moore635374e2009-03-09 01:21:12 -0600126/**
127 * _base_fault_reset_work - workq handling ioc fault conditions
128 * @work: input argument, used to derive ioc
129 * Context: sleep.
130 *
131 * Return nothing.
132 */
133static void
134_base_fault_reset_work(struct work_struct *work)
135{
136 struct MPT2SAS_ADAPTER *ioc =
137 container_of(work, struct MPT2SAS_ADAPTER, fault_reset_work.work);
138 unsigned long flags;
139 u32 doorbell;
140 int rc;
141
142 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +0530143 if (ioc->shost_recovery)
Eric Moore635374e2009-03-09 01:21:12 -0600144 goto rearm_timer;
145 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
146
147 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
148 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
149 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
150 FORCE_BIG_HAMMER);
151 printk(MPT2SAS_WARN_FMT "%s: hard reset: %s\n", ioc->name,
152 __func__, (rc == 0) ? "success" : "failed");
153 doorbell = mpt2sas_base_get_iocstate(ioc, 0);
154 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
155 mpt2sas_base_fault_info(ioc, doorbell &
156 MPI2_DOORBELL_DATA_MASK);
157 }
158
159 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
160 rearm_timer:
161 if (ioc->fault_reset_work_q)
162 queue_delayed_work(ioc->fault_reset_work_q,
163 &ioc->fault_reset_work,
164 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
165 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
166}
167
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530168/**
169 * mpt2sas_base_start_watchdog - start the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530170 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530171 * Context: sleep.
172 *
173 * Return nothing.
174 */
175void
176mpt2sas_base_start_watchdog(struct MPT2SAS_ADAPTER *ioc)
177{
178 unsigned long flags;
179
180 if (ioc->fault_reset_work_q)
181 return;
182
183 /* initialize fault polling */
184 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
185 snprintf(ioc->fault_reset_work_q_name,
186 sizeof(ioc->fault_reset_work_q_name), "poll_%d_status", ioc->id);
187 ioc->fault_reset_work_q =
188 create_singlethread_workqueue(ioc->fault_reset_work_q_name);
189 if (!ioc->fault_reset_work_q) {
190 printk(MPT2SAS_ERR_FMT "%s: failed (line=%d)\n",
191 ioc->name, __func__, __LINE__);
192 return;
193 }
194 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
195 if (ioc->fault_reset_work_q)
196 queue_delayed_work(ioc->fault_reset_work_q,
197 &ioc->fault_reset_work,
198 msecs_to_jiffies(FAULT_POLLING_INTERVAL));
199 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
200}
201
202/**
203 * mpt2sas_base_stop_watchdog - stop the fault_reset_work_q
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530204 * @ioc: per adapter object
Kashyap, Desaie4750c92009-08-07 19:37:59 +0530205 * Context: sleep.
206 *
207 * Return nothing.
208 */
209void
210mpt2sas_base_stop_watchdog(struct MPT2SAS_ADAPTER *ioc)
211{
212 unsigned long flags;
213 struct workqueue_struct *wq;
214
215 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
216 wq = ioc->fault_reset_work_q;
217 ioc->fault_reset_work_q = NULL;
218 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
219 if (wq) {
220 if (!cancel_delayed_work(&ioc->fault_reset_work))
221 flush_workqueue(wq);
222 destroy_workqueue(wq);
223 }
224}
225
Kashyap, Desaifa7f3162009-09-23 17:26:58 +0530226/**
227 * mpt2sas_base_fault_info - verbose translation of firmware FAULT code
228 * @ioc: per adapter object
229 * @fault_code: fault code
230 *
231 * Return nothing.
232 */
233void
234mpt2sas_base_fault_info(struct MPT2SAS_ADAPTER *ioc , u16 fault_code)
235{
236 printk(MPT2SAS_ERR_FMT "fault_state(0x%04x)!\n",
237 ioc->name, fault_code);
238}
239
240/**
241 * mpt2sas_halt_firmware - halt's mpt controller firmware
242 * @ioc: per adapter object
243 *
244 * For debugging timeout related issues. Writing 0xCOFFEE00
245 * to the doorbell register will halt controller firmware. With
246 * the purpose to stop both driver and firmware, the enduser can
247 * obtain a ring buffer from controller UART.
248 */
249void
250mpt2sas_halt_firmware(struct MPT2SAS_ADAPTER *ioc)
251{
252 u32 doorbell;
253
254 if (!ioc->fwfault_debug)
255 return;
256
257 dump_stack();
258
259 doorbell = readl(&ioc->chip->Doorbell);
260 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT)
261 mpt2sas_base_fault_info(ioc , doorbell);
262 else {
263 writel(0xC0FFEE00, &ioc->chip->Doorbell);
264 printk(MPT2SAS_ERR_FMT "Firmware is halted due to command "
265 "timeout\n", ioc->name);
266 }
267
268 panic("panic in %s\n", __func__);
269}
270
Eric Moore635374e2009-03-09 01:21:12 -0600271#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
272/**
273 * _base_sas_ioc_info - verbose translation of the ioc status
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530274 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600275 * @mpi_reply: reply mf payload returned from firmware
276 * @request_hdr: request mf
277 *
278 * Return nothing.
279 */
280static void
281_base_sas_ioc_info(struct MPT2SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
282 MPI2RequestHeader_t *request_hdr)
283{
284 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
285 MPI2_IOCSTATUS_MASK;
286 char *desc = NULL;
287 u16 frame_sz;
288 char *func_str = NULL;
289
290 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
291 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
292 request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
293 request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
294 return;
295
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530296 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
297 return;
298
Eric Moore635374e2009-03-09 01:21:12 -0600299 switch (ioc_status) {
300
301/****************************************************************************
302* Common IOCStatus values for all replies
303****************************************************************************/
304
305 case MPI2_IOCSTATUS_INVALID_FUNCTION:
306 desc = "invalid function";
307 break;
308 case MPI2_IOCSTATUS_BUSY:
309 desc = "busy";
310 break;
311 case MPI2_IOCSTATUS_INVALID_SGL:
312 desc = "invalid sgl";
313 break;
314 case MPI2_IOCSTATUS_INTERNAL_ERROR:
315 desc = "internal error";
316 break;
317 case MPI2_IOCSTATUS_INVALID_VPID:
318 desc = "invalid vpid";
319 break;
320 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
321 desc = "insufficient resources";
322 break;
323 case MPI2_IOCSTATUS_INVALID_FIELD:
324 desc = "invalid field";
325 break;
326 case MPI2_IOCSTATUS_INVALID_STATE:
327 desc = "invalid state";
328 break;
329 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
330 desc = "op state not supported";
331 break;
332
333/****************************************************************************
334* Config IOCStatus values
335****************************************************************************/
336
337 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
338 desc = "config invalid action";
339 break;
340 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
341 desc = "config invalid type";
342 break;
343 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
344 desc = "config invalid page";
345 break;
346 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
347 desc = "config invalid data";
348 break;
349 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
350 desc = "config no defaults";
351 break;
352 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
353 desc = "config cant commit";
354 break;
355
356/****************************************************************************
357* SCSI IO Reply
358****************************************************************************/
359
360 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
361 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
362 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
363 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
364 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
365 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
366 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
367 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
368 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
369 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
370 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
371 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
372 break;
373
374/****************************************************************************
375* For use by SCSI Initiator and SCSI Target end-to-end data protection
376****************************************************************************/
377
378 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
379 desc = "eedp guard error";
380 break;
381 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
382 desc = "eedp ref tag error";
383 break;
384 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
385 desc = "eedp app tag error";
386 break;
387
388/****************************************************************************
389* SCSI Target values
390****************************************************************************/
391
392 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
393 desc = "target invalid io index";
394 break;
395 case MPI2_IOCSTATUS_TARGET_ABORTED:
396 desc = "target aborted";
397 break;
398 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
399 desc = "target no conn retryable";
400 break;
401 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
402 desc = "target no connection";
403 break;
404 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
405 desc = "target xfer count mismatch";
406 break;
407 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
408 desc = "target data offset error";
409 break;
410 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
411 desc = "target too much write data";
412 break;
413 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
414 desc = "target iu too short";
415 break;
416 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
417 desc = "target ack nak timeout";
418 break;
419 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
420 desc = "target nak received";
421 break;
422
423/****************************************************************************
424* Serial Attached SCSI values
425****************************************************************************/
426
427 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
428 desc = "smp request failed";
429 break;
430 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
431 desc = "smp data overrun";
432 break;
433
434/****************************************************************************
435* Diagnostic Buffer Post / Diagnostic Release values
436****************************************************************************/
437
438 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
439 desc = "diagnostic released";
440 break;
441 default:
442 break;
443 }
444
445 if (!desc)
446 return;
447
448 switch (request_hdr->Function) {
449 case MPI2_FUNCTION_CONFIG:
450 frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
451 func_str = "config_page";
452 break;
453 case MPI2_FUNCTION_SCSI_TASK_MGMT:
454 frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
455 func_str = "task_mgmt";
456 break;
457 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
458 frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
459 func_str = "sas_iounit_ctl";
460 break;
461 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
462 frame_sz = sizeof(Mpi2SepRequest_t);
463 func_str = "enclosure";
464 break;
465 case MPI2_FUNCTION_IOC_INIT:
466 frame_sz = sizeof(Mpi2IOCInitRequest_t);
467 func_str = "ioc_init";
468 break;
469 case MPI2_FUNCTION_PORT_ENABLE:
470 frame_sz = sizeof(Mpi2PortEnableRequest_t);
471 func_str = "port_enable";
472 break;
473 case MPI2_FUNCTION_SMP_PASSTHROUGH:
474 frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
475 func_str = "smp_passthru";
476 break;
477 default:
478 frame_sz = 32;
479 func_str = "unknown";
480 break;
481 }
482
483 printk(MPT2SAS_WARN_FMT "ioc_status: %s(0x%04x), request(0x%p),"
484 " (%s)\n", ioc->name, desc, ioc_status, request_hdr, func_str);
485
486 _debug_dump_mf(request_hdr, frame_sz/4);
487}
488
489/**
490 * _base_display_event_data - verbose translation of firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530491 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600492 * @mpi_reply: reply mf payload returned from firmware
493 *
494 * Return nothing.
495 */
496static void
497_base_display_event_data(struct MPT2SAS_ADAPTER *ioc,
498 Mpi2EventNotificationReply_t *mpi_reply)
499{
500 char *desc = NULL;
501 u16 event;
502
503 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
504 return;
505
506 event = le16_to_cpu(mpi_reply->Event);
507
508 switch (event) {
509 case MPI2_EVENT_LOG_DATA:
510 desc = "Log Data";
511 break;
512 case MPI2_EVENT_STATE_CHANGE:
513 desc = "Status Change";
514 break;
515 case MPI2_EVENT_HARD_RESET_RECEIVED:
516 desc = "Hard Reset Received";
517 break;
518 case MPI2_EVENT_EVENT_CHANGE:
519 desc = "Event Change";
520 break;
Eric Moore635374e2009-03-09 01:21:12 -0600521 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
522 desc = "Device Status Change";
523 break;
524 case MPI2_EVENT_IR_OPERATION_STATUS:
525 desc = "IR Operation Status";
526 break;
527 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530528 {
529 Mpi2EventDataSasDiscovery_t *event_data =
530 (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
531 printk(MPT2SAS_INFO_FMT "Discovery: (%s)", ioc->name,
532 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
533 "start" : "stop");
534 if (event_data->DiscoveryStatus)
535 printk("discovery_status(0x%08x)",
536 le32_to_cpu(event_data->DiscoveryStatus));
Julia Lawall7968f192010-08-05 22:19:36 +0200537 printk("\n");
Kashyap, Desaie94f6742010-03-17 16:24:52 +0530538 return;
539 }
Eric Moore635374e2009-03-09 01:21:12 -0600540 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
541 desc = "SAS Broadcast Primitive";
542 break;
543 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
544 desc = "SAS Init Device Status Change";
545 break;
546 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
547 desc = "SAS Init Table Overflow";
548 break;
549 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
550 desc = "SAS Topology Change List";
551 break;
552 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
553 desc = "SAS Enclosure Device Status Change";
554 break;
555 case MPI2_EVENT_IR_VOLUME:
556 desc = "IR Volume";
557 break;
558 case MPI2_EVENT_IR_PHYSICAL_DISK:
559 desc = "IR Physical Disk";
560 break;
561 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
562 desc = "IR Configuration Change List";
563 break;
564 case MPI2_EVENT_LOG_ENTRY_ADDED:
565 desc = "Log Entry Added";
566 break;
567 }
568
569 if (!desc)
570 return;
571
572 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, desc);
573}
574#endif
575
576/**
577 * _base_sas_log_info - verbose translation of firmware log info
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530578 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600579 * @log_info: log info
580 *
581 * Return nothing.
582 */
583static void
584_base_sas_log_info(struct MPT2SAS_ADAPTER *ioc , u32 log_info)
585{
586 union loginfo_type {
587 u32 loginfo;
588 struct {
589 u32 subcode:16;
590 u32 code:8;
591 u32 originator:4;
592 u32 bus_type:4;
593 } dw;
594 };
595 union loginfo_type sas_loginfo;
596 char *originator_str = NULL;
597
598 sas_loginfo.loginfo = log_info;
599 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
600 return;
601
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +0530602 /* each nexus loss loginfo */
603 if (log_info == 0x31170000)
604 return;
605
Eric Moore635374e2009-03-09 01:21:12 -0600606 /* eat the loginfos associated with task aborts */
607 if (ioc->ignore_loginfos && (log_info == 30050000 || log_info ==
608 0x31140000 || log_info == 0x31130000))
609 return;
610
611 switch (sas_loginfo.dw.originator) {
612 case 0:
613 originator_str = "IOP";
614 break;
615 case 1:
616 originator_str = "PL";
617 break;
618 case 2:
619 originator_str = "IR";
620 break;
621 }
622
623 printk(MPT2SAS_WARN_FMT "log_info(0x%08x): originator(%s), "
624 "code(0x%02x), sub_code(0x%04x)\n", ioc->name, log_info,
625 originator_str, sas_loginfo.dw.code,
626 sas_loginfo.dw.subcode);
627}
628
629/**
Eric Moore635374e2009-03-09 01:21:12 -0600630 * _base_display_reply_info -
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530631 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600632 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530633 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600634 * @reply: reply message frame(lower 32bit addr)
635 *
636 * Return nothing.
637 */
638static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530639_base_display_reply_info(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600640 u32 reply)
641{
642 MPI2DefaultReply_t *mpi_reply;
643 u16 ioc_status;
644
645 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
646 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
647#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
648 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
649 (ioc->logging_level & MPT_DEBUG_REPLY)) {
650 _base_sas_ioc_info(ioc , mpi_reply,
651 mpt2sas_base_get_msg_frame(ioc, smid));
652 }
653#endif
654 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
655 _base_sas_log_info(ioc, le32_to_cpu(mpi_reply->IOCLogInfo));
656}
657
658/**
659 * mpt2sas_base_done - base internal command completion routine
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530660 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600661 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530662 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600663 * @reply: reply message frame(lower 32bit addr)
664 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530665 * Return 1 meaning mf should be freed from _base_interrupt
666 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600667 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530668u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530669mpt2sas_base_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
670 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600671{
672 MPI2DefaultReply_t *mpi_reply;
673
674 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
675 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530676 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600677
678 if (ioc->base_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530679 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600680
681 ioc->base_cmds.status |= MPT2_CMD_COMPLETE;
682 if (mpi_reply) {
683 ioc->base_cmds.status |= MPT2_CMD_REPLY_VALID;
684 memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
685 }
686 ioc->base_cmds.status &= ~MPT2_CMD_PENDING;
687 complete(&ioc->base_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530688 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600689}
690
691/**
692 * _base_async_event - main callback handler for firmware asyn events
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530693 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530694 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -0600695 * @reply: reply message frame(lower 32bit addr)
696 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530697 * Return 1 meaning mf should be freed from _base_interrupt
698 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -0600699 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530700static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530701_base_async_event(struct MPT2SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -0600702{
703 Mpi2EventNotificationReply_t *mpi_reply;
704 Mpi2EventAckRequest_t *ack_request;
705 u16 smid;
706
707 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
708 if (!mpi_reply)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530709 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600710 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530711 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600712#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
713 _base_display_event_data(ioc, mpi_reply);
714#endif
715 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
716 goto out;
717 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
718 if (!smid) {
719 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
720 ioc->name, __func__);
721 goto out;
722 }
723
724 ack_request = mpt2sas_base_get_msg_frame(ioc, smid);
725 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
726 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
727 ack_request->Event = mpi_reply->Event;
728 ack_request->EventContext = mpi_reply->EventContext;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530729 ack_request->VF_ID = 0; /* TODO */
730 ack_request->VP_ID = 0;
731 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600732
733 out:
734
735 /* scsih callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530736 mpt2sas_scsih_event_callback(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600737
738 /* ctl callback handler */
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530739 mpt2sas_ctl_event_callback(ioc, msix_index, reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530740
741 return 1;
Eric Moore635374e2009-03-09 01:21:12 -0600742}
743
744/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530745 * _base_get_cb_idx - obtain the callback index
746 * @ioc: per adapter object
747 * @smid: system request message index
748 *
749 * Return callback index.
750 */
751static u8
752_base_get_cb_idx(struct MPT2SAS_ADAPTER *ioc, u16 smid)
753{
754 int i;
755 u8 cb_idx = 0xFF;
756
757 if (smid >= ioc->hi_priority_smid) {
758 if (smid < ioc->internal_smid) {
759 i = smid - ioc->hi_priority_smid;
760 cb_idx = ioc->hpr_lookup[i].cb_idx;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530761 } else if (smid <= ioc->hba_queue_depth) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530762 i = smid - ioc->internal_smid;
763 cb_idx = ioc->internal_lookup[i].cb_idx;
764 }
765 } else {
766 i = smid - 1;
767 cb_idx = ioc->scsi_lookup[i].cb_idx;
768 }
769 return cb_idx;
770}
771
772/**
Eric Moore635374e2009-03-09 01:21:12 -0600773 * _base_mask_interrupts - disable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530774 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600775 *
776 * Disabling ResetIRQ, Reply and Doorbell Interrupts
777 *
778 * Return nothing.
779 */
780static void
781_base_mask_interrupts(struct MPT2SAS_ADAPTER *ioc)
782{
783 u32 him_register;
784
785 ioc->mask_interrupts = 1;
786 him_register = readl(&ioc->chip->HostInterruptMask);
787 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
788 writel(him_register, &ioc->chip->HostInterruptMask);
789 readl(&ioc->chip->HostInterruptMask);
790}
791
792/**
793 * _base_unmask_interrupts - enable interrupts
Kashyap, Desaicef7a122009-09-23 17:27:41 +0530794 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -0600795 *
796 * Enabling only Reply Interrupts
797 *
798 * Return nothing.
799 */
800static void
801_base_unmask_interrupts(struct MPT2SAS_ADAPTER *ioc)
802{
803 u32 him_register;
804
Eric Moore635374e2009-03-09 01:21:12 -0600805 him_register = readl(&ioc->chip->HostInterruptMask);
806 him_register &= ~MPI2_HIM_RIM;
807 writel(him_register, &ioc->chip->HostInterruptMask);
808 ioc->mask_interrupts = 0;
809}
810
Kashyap, Desai5b768582009-08-20 13:24:31 +0530811union reply_descriptor {
812 u64 word;
813 struct {
814 u32 low;
815 u32 high;
816 } u;
817};
818
Eric Moore635374e2009-03-09 01:21:12 -0600819/**
820 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
821 * @irq: irq number (not used)
822 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
823 * @r: pt_regs pointer (not used)
824 *
825 * Return IRQ_HANDLE if processed, else IRQ_NONE.
826 */
827static irqreturn_t
828_base_interrupt(int irq, void *bus_id)
829{
Eric Moore03ea1112009-04-21 15:37:57 -0600830 union reply_descriptor rd;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530831 u32 completed_cmds;
Eric Moore635374e2009-03-09 01:21:12 -0600832 u8 request_desript_type;
833 u16 smid;
834 u8 cb_idx;
835 u32 reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530836 u8 msix_index;
Eric Moore635374e2009-03-09 01:21:12 -0600837 struct MPT2SAS_ADAPTER *ioc = bus_id;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530838 Mpi2ReplyDescriptorsUnion_t *rpf;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530839 u8 rc;
Eric Moore635374e2009-03-09 01:21:12 -0600840
841 if (ioc->mask_interrupts)
842 return IRQ_NONE;
843
Kashyap, Desai5b768582009-08-20 13:24:31 +0530844 rpf = &ioc->reply_post_free[ioc->reply_post_host_index];
845 request_desript_type = rpf->Default.ReplyFlags
846 & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600847 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
848 return IRQ_NONE;
849
850 completed_cmds = 0;
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530851 cb_idx = 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -0600852 do {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530853 rd.word = rpf->Words;
Eric Moore03ea1112009-04-21 15:37:57 -0600854 if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
Eric Moore635374e2009-03-09 01:21:12 -0600855 goto out;
856 reply = 0;
857 cb_idx = 0xFF;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530858 smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530859 msix_index = rpf->Default.MSIxIndex;
Eric Moore635374e2009-03-09 01:21:12 -0600860 if (request_desript_type ==
861 MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
Kashyap, Desai5b768582009-08-20 13:24:31 +0530862 reply = le32_to_cpu
863 (rpf->AddressReply.ReplyFrameAddress);
Kashyap, Desaidd3741d2010-11-13 04:31:14 +0530864 if (reply > ioc->reply_dma_max_address ||
865 reply < ioc->reply_dma_min_address)
866 reply = 0;
Eric Moore635374e2009-03-09 01:21:12 -0600867 } else if (request_desript_type ==
868 MPI2_RPY_DESCRIPT_FLAGS_TARGET_COMMAND_BUFFER)
869 goto next;
870 else if (request_desript_type ==
871 MPI2_RPY_DESCRIPT_FLAGS_TARGETASSIST_SUCCESS)
872 goto next;
873 if (smid)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530874 cb_idx = _base_get_cb_idx(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600875 if (smid && cb_idx != 0xFF) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530876 rc = mpt_callbacks[cb_idx](ioc, smid, msix_index,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530877 reply);
Eric Moore635374e2009-03-09 01:21:12 -0600878 if (reply)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530879 _base_display_reply_info(ioc, smid, msix_index,
Eric Moore635374e2009-03-09 01:21:12 -0600880 reply);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +0530881 if (rc)
882 mpt2sas_base_free_smid(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -0600883 }
884 if (!smid)
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530885 _base_async_event(ioc, msix_index, reply);
Eric Moore635374e2009-03-09 01:21:12 -0600886
887 /* reply free queue handling */
888 if (reply) {
889 ioc->reply_free_host_index =
890 (ioc->reply_free_host_index ==
891 (ioc->reply_free_queue_depth - 1)) ?
892 0 : ioc->reply_free_host_index + 1;
893 ioc->reply_free[ioc->reply_free_host_index] =
894 cpu_to_le32(reply);
Kashyap, Desai5b768582009-08-20 13:24:31 +0530895 wmb();
Eric Moore635374e2009-03-09 01:21:12 -0600896 writel(ioc->reply_free_host_index,
897 &ioc->chip->ReplyFreeHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600898 }
899
900 next:
Kashyap, Desai5b768582009-08-20 13:24:31 +0530901
902 rpf->Words = ULLONG_MAX;
903 ioc->reply_post_host_index = (ioc->reply_post_host_index ==
904 (ioc->reply_post_queue_depth - 1)) ? 0 :
905 ioc->reply_post_host_index + 1;
Eric Moore635374e2009-03-09 01:21:12 -0600906 request_desript_type =
Kashyap, Desai5b768582009-08-20 13:24:31 +0530907 ioc->reply_post_free[ioc->reply_post_host_index].Default.
908 ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
Eric Moore635374e2009-03-09 01:21:12 -0600909 completed_cmds++;
910 if (request_desript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
911 goto out;
Kashyap, Desai5b768582009-08-20 13:24:31 +0530912 if (!ioc->reply_post_host_index)
913 rpf = ioc->reply_post_free;
914 else
915 rpf++;
Eric Moore635374e2009-03-09 01:21:12 -0600916 } while (1);
917
918 out:
919
920 if (!completed_cmds)
921 return IRQ_NONE;
922
Eric Moore635374e2009-03-09 01:21:12 -0600923 wmb();
Kashyap, Desai5b768582009-08-20 13:24:31 +0530924 writel(ioc->reply_post_host_index, &ioc->chip->ReplyPostHostIndex);
Eric Moore635374e2009-03-09 01:21:12 -0600925 return IRQ_HANDLED;
926}
927
928/**
929 * mpt2sas_base_release_callback_handler - clear interupt callback handler
930 * @cb_idx: callback index
931 *
932 * Return nothing.
933 */
934void
935mpt2sas_base_release_callback_handler(u8 cb_idx)
936{
937 mpt_callbacks[cb_idx] = NULL;
938}
939
940/**
941 * mpt2sas_base_register_callback_handler - obtain index for the interrupt callback handler
942 * @cb_func: callback function
943 *
944 * Returns cb_func.
945 */
946u8
947mpt2sas_base_register_callback_handler(MPT_CALLBACK cb_func)
948{
949 u8 cb_idx;
950
951 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
952 if (mpt_callbacks[cb_idx] == NULL)
953 break;
954
955 mpt_callbacks[cb_idx] = cb_func;
956 return cb_idx;
957}
958
959/**
960 * mpt2sas_base_initialize_callback_handler - initialize the interrupt callback handler
961 *
962 * Return nothing.
963 */
964void
965mpt2sas_base_initialize_callback_handler(void)
966{
967 u8 cb_idx;
968
969 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
970 mpt2sas_base_release_callback_handler(cb_idx);
971}
972
973/**
974 * mpt2sas_base_build_zero_len_sge - build zero length sg entry
975 * @ioc: per adapter object
976 * @paddr: virtual address for SGE
977 *
978 * Create a zero length scatter gather entry to insure the IOCs hardware has
979 * something to use if the target device goes brain dead and tries
980 * to send data even when none is asked for.
981 *
982 * Return nothing.
983 */
984void
985mpt2sas_base_build_zero_len_sge(struct MPT2SAS_ADAPTER *ioc, void *paddr)
986{
987 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
988 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
989 MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
990 MPI2_SGE_FLAGS_SHIFT);
991 ioc->base_add_sg_single(paddr, flags_length, -1);
992}
993
994/**
995 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
996 * @paddr: virtual address for SGE
997 * @flags_length: SGE flags and data transfer length
998 * @dma_addr: Physical address
999 *
1000 * Return nothing.
1001 */
1002static void
1003_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1004{
1005 Mpi2SGESimple32_t *sgel = paddr;
1006
1007 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
1008 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1009 sgel->FlagsLength = cpu_to_le32(flags_length);
1010 sgel->Address = cpu_to_le32(dma_addr);
1011}
1012
1013
1014/**
1015 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
1016 * @paddr: virtual address for SGE
1017 * @flags_length: SGE flags and data transfer length
1018 * @dma_addr: Physical address
1019 *
1020 * Return nothing.
1021 */
1022static void
1023_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
1024{
1025 Mpi2SGESimple64_t *sgel = paddr;
1026
1027 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
1028 MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
1029 sgel->FlagsLength = cpu_to_le32(flags_length);
1030 sgel->Address = cpu_to_le64(dma_addr);
1031}
1032
1033#define convert_to_kb(x) ((x) << (PAGE_SHIFT - 10))
1034
1035/**
1036 * _base_config_dma_addressing - set dma addressing
1037 * @ioc: per adapter object
1038 * @pdev: PCI device struct
1039 *
1040 * Returns 0 for success, non-zero for failure.
1041 */
1042static int
1043_base_config_dma_addressing(struct MPT2SAS_ADAPTER *ioc, struct pci_dev *pdev)
1044{
1045 struct sysinfo s;
1046 char *desc = NULL;
1047
1048 if (sizeof(dma_addr_t) > 4) {
1049 const uint64_t required_mask =
1050 dma_get_required_mask(&pdev->dev);
Yang Hongyange9304382009-04-13 14:40:14 -07001051 if ((required_mask > DMA_BIT_MASK(32)) && !pci_set_dma_mask(pdev,
1052 DMA_BIT_MASK(64)) && !pci_set_consistent_dma_mask(pdev,
1053 DMA_BIT_MASK(64))) {
Eric Moore635374e2009-03-09 01:21:12 -06001054 ioc->base_add_sg_single = &_base_add_sg_single_64;
1055 ioc->sge_size = sizeof(Mpi2SGESimple64_t);
1056 desc = "64";
1057 goto out;
1058 }
1059 }
1060
Yang Hongyange9304382009-04-13 14:40:14 -07001061 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
1062 && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32))) {
Eric Moore635374e2009-03-09 01:21:12 -06001063 ioc->base_add_sg_single = &_base_add_sg_single_32;
1064 ioc->sge_size = sizeof(Mpi2SGESimple32_t);
1065 desc = "32";
1066 } else
1067 return -ENODEV;
1068
1069 out:
1070 si_meminfo(&s);
1071 printk(MPT2SAS_INFO_FMT "%s BIT PCI BUS DMA ADDRESSING SUPPORTED, "
1072 "total mem (%ld kB)\n", ioc->name, desc, convert_to_kb(s.totalram));
1073
1074 return 0;
1075}
1076
1077/**
1078 * _base_save_msix_table - backup msix vector table
1079 * @ioc: per adapter object
1080 *
1081 * This address an errata where diag reset clears out the table
1082 */
1083static void
1084_base_save_msix_table(struct MPT2SAS_ADAPTER *ioc)
1085{
1086 int i;
1087
1088 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1089 return;
1090
1091 for (i = 0; i < ioc->msix_vector_count; i++)
1092 ioc->msix_table_backup[i] = ioc->msix_table[i];
1093}
1094
1095/**
1096 * _base_restore_msix_table - this restores the msix vector table
1097 * @ioc: per adapter object
1098 *
1099 */
1100static void
1101_base_restore_msix_table(struct MPT2SAS_ADAPTER *ioc)
1102{
1103 int i;
1104
1105 if (!ioc->msix_enable || ioc->msix_table_backup == NULL)
1106 return;
1107
1108 for (i = 0; i < ioc->msix_vector_count; i++)
1109 ioc->msix_table[i] = ioc->msix_table_backup[i];
1110}
1111
1112/**
1113 * _base_check_enable_msix - checks MSIX capabable.
1114 * @ioc: per adapter object
1115 *
1116 * Check to see if card is capable of MSIX, and set number
1117 * of avaliable msix vectors
1118 */
1119static int
1120_base_check_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1121{
1122 int base;
1123 u16 message_control;
1124 u32 msix_table_offset;
1125
1126 base = pci_find_capability(ioc->pdev, PCI_CAP_ID_MSIX);
1127 if (!base) {
1128 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "msix not "
1129 "supported\n", ioc->name));
1130 return -EINVAL;
1131 }
1132
1133 /* get msix vector count */
1134 pci_read_config_word(ioc->pdev, base + 2, &message_control);
1135 ioc->msix_vector_count = (message_control & 0x3FF) + 1;
1136
1137 /* get msix table */
1138 pci_read_config_dword(ioc->pdev, base + 4, &msix_table_offset);
1139 msix_table_offset &= 0xFFFFFFF8;
1140 ioc->msix_table = (u32 *)((void *)ioc->chip + msix_table_offset);
1141
1142 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "msix is supported, "
1143 "vector_count(%d), table_offset(0x%08x), table(%p)\n", ioc->name,
1144 ioc->msix_vector_count, msix_table_offset, ioc->msix_table));
1145 return 0;
1146}
1147
1148/**
1149 * _base_disable_msix - disables msix
1150 * @ioc: per adapter object
1151 *
1152 */
1153static void
1154_base_disable_msix(struct MPT2SAS_ADAPTER *ioc)
1155{
1156 if (ioc->msix_enable) {
1157 pci_disable_msix(ioc->pdev);
1158 kfree(ioc->msix_table_backup);
1159 ioc->msix_table_backup = NULL;
1160 ioc->msix_enable = 0;
1161 }
1162}
1163
1164/**
1165 * _base_enable_msix - enables msix, failback to io_apic
1166 * @ioc: per adapter object
1167 *
1168 */
1169static int
1170_base_enable_msix(struct MPT2SAS_ADAPTER *ioc)
1171{
1172 struct msix_entry entries;
1173 int r;
1174 u8 try_msix = 0;
1175
1176 if (msix_disable == -1 || msix_disable == 0)
1177 try_msix = 1;
1178
1179 if (!try_msix)
1180 goto try_ioapic;
1181
1182 if (_base_check_enable_msix(ioc) != 0)
1183 goto try_ioapic;
1184
1185 ioc->msix_table_backup = kcalloc(ioc->msix_vector_count,
1186 sizeof(u32), GFP_KERNEL);
1187 if (!ioc->msix_table_backup) {
1188 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "allocation for "
1189 "msix_table_backup failed!!!\n", ioc->name));
1190 goto try_ioapic;
1191 }
1192
1193 memset(&entries, 0, sizeof(struct msix_entry));
1194 r = pci_enable_msix(ioc->pdev, &entries, 1);
1195 if (r) {
1196 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "pci_enable_msix "
1197 "failed (r=%d) !!!\n", ioc->name, r));
1198 goto try_ioapic;
1199 }
1200
1201 r = request_irq(entries.vector, _base_interrupt, IRQF_SHARED,
1202 ioc->name, ioc);
1203 if (r) {
1204 dfailprintk(ioc, printk(MPT2SAS_INFO_FMT "unable to allocate "
1205 "interrupt %d !!!\n", ioc->name, entries.vector));
1206 pci_disable_msix(ioc->pdev);
1207 goto try_ioapic;
1208 }
1209
1210 ioc->pci_irq = entries.vector;
1211 ioc->msix_enable = 1;
1212 return 0;
1213
1214/* failback to io_apic interrupt routing */
1215 try_ioapic:
1216
1217 r = request_irq(ioc->pdev->irq, _base_interrupt, IRQF_SHARED,
1218 ioc->name, ioc);
1219 if (r) {
1220 printk(MPT2SAS_ERR_FMT "unable to allocate interrupt %d!\n",
1221 ioc->name, ioc->pdev->irq);
1222 r = -EBUSY;
1223 goto out_fail;
1224 }
1225
1226 ioc->pci_irq = ioc->pdev->irq;
1227 return 0;
1228
1229 out_fail:
1230 return r;
1231}
1232
1233/**
1234 * mpt2sas_base_map_resources - map in controller resources (io/irq/memap)
1235 * @ioc: per adapter object
1236 *
1237 * Returns 0 for success, non-zero for failure.
1238 */
1239int
1240mpt2sas_base_map_resources(struct MPT2SAS_ADAPTER *ioc)
1241{
1242 struct pci_dev *pdev = ioc->pdev;
1243 u32 memap_sz;
1244 u32 pio_sz;
1245 int i, r = 0;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301246 u64 pio_chip = 0;
1247 u64 chip_phys = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001248
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05301249 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n",
Eric Moore635374e2009-03-09 01:21:12 -06001250 ioc->name, __func__));
1251
1252 ioc->bars = pci_select_bars(pdev, IORESOURCE_MEM);
1253 if (pci_enable_device_mem(pdev)) {
1254 printk(MPT2SAS_WARN_FMT "pci_enable_device_mem: "
1255 "failed\n", ioc->name);
1256 return -ENODEV;
1257 }
1258
1259
1260 if (pci_request_selected_regions(pdev, ioc->bars,
1261 MPT2SAS_DRIVER_NAME)) {
1262 printk(MPT2SAS_WARN_FMT "pci_request_selected_regions: "
1263 "failed\n", ioc->name);
1264 r = -ENODEV;
1265 goto out_fail;
1266 }
1267
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301268 /* AER (Advanced Error Reporting) hooks */
1269 pci_enable_pcie_error_reporting(pdev);
1270
Eric Moore635374e2009-03-09 01:21:12 -06001271 pci_set_master(pdev);
1272
1273 if (_base_config_dma_addressing(ioc, pdev) != 0) {
1274 printk(MPT2SAS_WARN_FMT "no suitable DMA mask for %s\n",
1275 ioc->name, pci_name(pdev));
1276 r = -ENODEV;
1277 goto out_fail;
1278 }
1279
1280 for (i = 0, memap_sz = 0, pio_sz = 0 ; i < DEVICE_COUNT_RESOURCE; i++) {
Richard A Laryfc193172010-03-12 15:27:06 -08001281 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
Eric Moore635374e2009-03-09 01:21:12 -06001282 if (pio_sz)
1283 continue;
Kashyap, Desai6846e752009-12-16 18:51:45 +05301284 pio_chip = (u64)pci_resource_start(pdev, i);
Eric Moore635374e2009-03-09 01:21:12 -06001285 pio_sz = pci_resource_len(pdev, i);
1286 } else {
1287 if (memap_sz)
1288 continue;
Richard A Laryfc193172010-03-12 15:27:06 -08001289 /* verify memory resource is valid before using */
1290 if (pci_resource_flags(pdev, i) & IORESOURCE_MEM) {
1291 ioc->chip_phys = pci_resource_start(pdev, i);
1292 chip_phys = (u64)ioc->chip_phys;
1293 memap_sz = pci_resource_len(pdev, i);
1294 ioc->chip = ioremap(ioc->chip_phys, memap_sz);
1295 if (ioc->chip == NULL) {
1296 printk(MPT2SAS_ERR_FMT "unable to map "
1297 "adapter memory!\n", ioc->name);
1298 r = -EINVAL;
1299 goto out_fail;
1300 }
Eric Moore635374e2009-03-09 01:21:12 -06001301 }
1302 }
1303 }
1304
Eric Moore635374e2009-03-09 01:21:12 -06001305 _base_mask_interrupts(ioc);
1306 r = _base_enable_msix(ioc);
1307 if (r)
1308 goto out_fail;
1309
1310 printk(MPT2SAS_INFO_FMT "%s: IRQ %d\n",
1311 ioc->name, ((ioc->msix_enable) ? "PCI-MSI-X enabled" :
1312 "IO-APIC enabled"), ioc->pci_irq);
Kashyap, Desai6846e752009-12-16 18:51:45 +05301313 printk(MPT2SAS_INFO_FMT "iomem(0x%016llx), mapped(0x%p), size(%d)\n",
1314 ioc->name, (unsigned long long)chip_phys, ioc->chip, memap_sz);
1315 printk(MPT2SAS_INFO_FMT "ioport(0x%016llx), size(%d)\n",
1316 ioc->name, (unsigned long long)pio_chip, pio_sz);
Eric Moore635374e2009-03-09 01:21:12 -06001317
Eric Moore3cb54692010-07-08 14:44:34 -06001318 /* Save PCI configuration state for recovery from PCI AER/EEH errors */
1319 pci_save_state(pdev);
1320
Eric Moore635374e2009-03-09 01:21:12 -06001321 return 0;
1322
1323 out_fail:
1324 if (ioc->chip_phys)
1325 iounmap(ioc->chip);
1326 ioc->chip_phys = 0;
1327 ioc->pci_irq = -1;
1328 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05301329 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001330 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06001331 return r;
1332}
1333
1334/**
Eric Moore635374e2009-03-09 01:21:12 -06001335 * mpt2sas_base_get_msg_frame - obtain request mf pointer
1336 * @ioc: per adapter object
1337 * @smid: system request message index(smid zero is invalid)
1338 *
1339 * Returns virt pointer to message frame.
1340 */
1341void *
1342mpt2sas_base_get_msg_frame(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1343{
1344 return (void *)(ioc->request + (smid * ioc->request_sz));
1345}
1346
1347/**
1348 * mpt2sas_base_get_sense_buffer - obtain a sense buffer assigned to a mf request
1349 * @ioc: per adapter object
1350 * @smid: system request message index
1351 *
1352 * Returns virt pointer to sense buffer.
1353 */
1354void *
1355mpt2sas_base_get_sense_buffer(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1356{
1357 return (void *)(ioc->sense + ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
1358}
1359
1360/**
1361 * mpt2sas_base_get_sense_buffer_dma - obtain a sense buffer assigned to a mf request
1362 * @ioc: per adapter object
1363 * @smid: system request message index
1364 *
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301365 * Returns phys pointer to the low 32bit address of the sense buffer.
Eric Moore635374e2009-03-09 01:21:12 -06001366 */
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301367__le32
Eric Moore635374e2009-03-09 01:21:12 -06001368mpt2sas_base_get_sense_buffer_dma(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1369{
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05301370 return cpu_to_le32(ioc->sense_dma +
1371 ((smid - 1) * SCSI_SENSE_BUFFERSIZE));
Eric Moore635374e2009-03-09 01:21:12 -06001372}
1373
1374/**
1375 * mpt2sas_base_get_reply_virt_addr - obtain reply frames virt address
1376 * @ioc: per adapter object
1377 * @phys_addr: lower 32 physical addr of the reply
1378 *
1379 * Converts 32bit lower physical addr into a virt address.
1380 */
1381void *
1382mpt2sas_base_get_reply_virt_addr(struct MPT2SAS_ADAPTER *ioc, u32 phys_addr)
1383{
1384 if (!phys_addr)
1385 return NULL;
1386 return ioc->reply + (phys_addr - (u32)ioc->reply_dma);
1387}
1388
1389/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301390 * mpt2sas_base_get_smid - obtain a free smid from internal queue
Eric Moore635374e2009-03-09 01:21:12 -06001391 * @ioc: per adapter object
1392 * @cb_idx: callback index
1393 *
1394 * Returns smid (zero is invalid)
1395 */
1396u16
1397mpt2sas_base_get_smid(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1398{
1399 unsigned long flags;
1400 struct request_tracker *request;
1401 u16 smid;
1402
1403 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301404 if (list_empty(&ioc->internal_free_list)) {
1405 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1406 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1407 ioc->name, __func__);
1408 return 0;
1409 }
1410
1411 request = list_entry(ioc->internal_free_list.next,
1412 struct request_tracker, tracker_list);
1413 request->cb_idx = cb_idx;
1414 smid = request->smid;
1415 list_del(&request->tracker_list);
1416 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1417 return smid;
1418}
1419
1420/**
1421 * mpt2sas_base_get_smid_scsiio - obtain a free smid from scsiio queue
1422 * @ioc: per adapter object
1423 * @cb_idx: callback index
1424 * @scmd: pointer to scsi command object
1425 *
1426 * Returns smid (zero is invalid)
1427 */
1428u16
1429mpt2sas_base_get_smid_scsiio(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx,
1430 struct scsi_cmnd *scmd)
1431{
1432 unsigned long flags;
1433 struct request_tracker *request;
1434 u16 smid;
1435
1436 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001437 if (list_empty(&ioc->free_list)) {
1438 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1439 printk(MPT2SAS_ERR_FMT "%s: smid not available\n",
1440 ioc->name, __func__);
1441 return 0;
1442 }
1443
1444 request = list_entry(ioc->free_list.next,
1445 struct request_tracker, tracker_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301446 request->scmd = scmd;
1447 request->cb_idx = cb_idx;
1448 smid = request->smid;
1449 list_del(&request->tracker_list);
1450 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1451 return smid;
1452}
1453
1454/**
1455 * mpt2sas_base_get_smid_hpr - obtain a free smid from hi-priority queue
1456 * @ioc: per adapter object
1457 * @cb_idx: callback index
1458 *
1459 * Returns smid (zero is invalid)
1460 */
1461u16
1462mpt2sas_base_get_smid_hpr(struct MPT2SAS_ADAPTER *ioc, u8 cb_idx)
1463{
1464 unsigned long flags;
1465 struct request_tracker *request;
1466 u16 smid;
1467
1468 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1469 if (list_empty(&ioc->hpr_free_list)) {
1470 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1471 return 0;
1472 }
1473
1474 request = list_entry(ioc->hpr_free_list.next,
1475 struct request_tracker, tracker_list);
Eric Moore635374e2009-03-09 01:21:12 -06001476 request->cb_idx = cb_idx;
1477 smid = request->smid;
1478 list_del(&request->tracker_list);
1479 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1480 return smid;
1481}
1482
1483
1484/**
1485 * mpt2sas_base_free_smid - put smid back on free_list
1486 * @ioc: per adapter object
1487 * @smid: system request message index
1488 *
1489 * Return nothing.
1490 */
1491void
1492mpt2sas_base_free_smid(struct MPT2SAS_ADAPTER *ioc, u16 smid)
1493{
1494 unsigned long flags;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301495 int i;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301496 struct chain_tracker *chain_req, *next;
Eric Moore635374e2009-03-09 01:21:12 -06001497
1498 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301499 if (smid >= ioc->hi_priority_smid) {
1500 if (smid < ioc->internal_smid) {
1501 /* hi-priority */
1502 i = smid - ioc->hi_priority_smid;
1503 ioc->hpr_lookup[i].cb_idx = 0xFF;
1504 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
1505 &ioc->hpr_free_list);
1506 } else {
1507 /* internal queue */
1508 i = smid - ioc->internal_smid;
1509 ioc->internal_lookup[i].cb_idx = 0xFF;
1510 list_add_tail(&ioc->internal_lookup[i].tracker_list,
1511 &ioc->internal_free_list);
1512 }
1513 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1514 return;
1515 }
1516
1517 /* scsiio queue */
1518 i = smid - 1;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301519 if (!list_empty(&ioc->scsi_lookup[i].chain_list)) {
1520 list_for_each_entry_safe(chain_req, next,
1521 &ioc->scsi_lookup[i].chain_list, tracker_list) {
1522 list_del_init(&chain_req->tracker_list);
1523 list_add_tail(&chain_req->tracker_list,
1524 &ioc->free_chain_list);
1525 }
1526 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301527 ioc->scsi_lookup[i].cb_idx = 0xFF;
1528 ioc->scsi_lookup[i].scmd = NULL;
1529 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
Eric Moore635374e2009-03-09 01:21:12 -06001530 &ioc->free_list);
1531 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1532
1533 /*
1534 * See _wait_for_commands_to_complete() call with regards to this code.
1535 */
1536 if (ioc->shost_recovery && ioc->pending_io_count) {
1537 if (ioc->pending_io_count == 1)
1538 wake_up(&ioc->reset_wq);
1539 ioc->pending_io_count--;
1540 }
1541}
1542
1543/**
1544 * _base_writeq - 64 bit write to MMIO
1545 * @ioc: per adapter object
1546 * @b: data payload
1547 * @addr: address in MMIO space
1548 * @writeq_lock: spin lock
1549 *
1550 * Glue for handling an atomic 64 bit word to MMIO. This special handling takes
1551 * care of 32 bit environment where its not quarenteed to send the entire word
1552 * in one transfer.
1553 */
1554#ifndef writeq
1555static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1556 spinlock_t *writeq_lock)
1557{
1558 unsigned long flags;
1559 __u64 data_out = cpu_to_le64(b);
1560
1561 spin_lock_irqsave(writeq_lock, flags);
1562 writel((u32)(data_out), addr);
1563 writel((u32)(data_out >> 32), (addr + 4));
1564 spin_unlock_irqrestore(writeq_lock, flags);
1565}
1566#else
1567static inline void _base_writeq(__u64 b, volatile void __iomem *addr,
1568 spinlock_t *writeq_lock)
1569{
1570 writeq(cpu_to_le64(b), addr);
1571}
1572#endif
1573
1574/**
1575 * mpt2sas_base_put_smid_scsi_io - send SCSI_IO request to firmware
1576 * @ioc: per adapter object
1577 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001578 * @handle: device handle
1579 *
1580 * Return nothing.
1581 */
1582void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301583mpt2sas_base_put_smid_scsi_io(struct MPT2SAS_ADAPTER *ioc, u16 smid, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06001584{
1585 Mpi2RequestDescriptorUnion_t descriptor;
1586 u64 *request = (u64 *)&descriptor;
1587
1588
1589 descriptor.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301590 descriptor.SCSIIO.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001591 descriptor.SCSIIO.SMID = cpu_to_le16(smid);
1592 descriptor.SCSIIO.DevHandle = cpu_to_le16(handle);
1593 descriptor.SCSIIO.LMID = 0;
1594 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1595 &ioc->scsi_lookup_lock);
1596}
1597
1598
1599/**
1600 * mpt2sas_base_put_smid_hi_priority - send Task Managment request to firmware
1601 * @ioc: per adapter object
1602 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001603 *
1604 * Return nothing.
1605 */
1606void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301607mpt2sas_base_put_smid_hi_priority(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001608{
1609 Mpi2RequestDescriptorUnion_t descriptor;
1610 u64 *request = (u64 *)&descriptor;
1611
1612 descriptor.HighPriority.RequestFlags =
1613 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301614 descriptor.HighPriority.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001615 descriptor.HighPriority.SMID = cpu_to_le16(smid);
1616 descriptor.HighPriority.LMID = 0;
1617 descriptor.HighPriority.Reserved1 = 0;
1618 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1619 &ioc->scsi_lookup_lock);
1620}
1621
1622/**
1623 * mpt2sas_base_put_smid_default - Default, primarily used for config pages
1624 * @ioc: per adapter object
1625 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001626 *
1627 * Return nothing.
1628 */
1629void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301630mpt2sas_base_put_smid_default(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001631{
1632 Mpi2RequestDescriptorUnion_t descriptor;
1633 u64 *request = (u64 *)&descriptor;
1634
1635 descriptor.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301636 descriptor.Default.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001637 descriptor.Default.SMID = cpu_to_le16(smid);
1638 descriptor.Default.LMID = 0;
1639 descriptor.Default.DescriptorTypeDependent = 0;
1640 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1641 &ioc->scsi_lookup_lock);
1642}
1643
1644/**
1645 * mpt2sas_base_put_smid_target_assist - send Target Assist/Status to firmware
1646 * @ioc: per adapter object
1647 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -06001648 * @io_index: value used to track the IO
1649 *
1650 * Return nothing.
1651 */
1652void
1653mpt2sas_base_put_smid_target_assist(struct MPT2SAS_ADAPTER *ioc, u16 smid,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301654 u16 io_index)
Eric Moore635374e2009-03-09 01:21:12 -06001655{
1656 Mpi2RequestDescriptorUnion_t descriptor;
1657 u64 *request = (u64 *)&descriptor;
1658
1659 descriptor.SCSITarget.RequestFlags =
1660 MPI2_REQ_DESCRIPT_FLAGS_SCSI_TARGET;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05301661 descriptor.SCSITarget.MSIxIndex = 0; /* TODO */
Eric Moore635374e2009-03-09 01:21:12 -06001662 descriptor.SCSITarget.SMID = cpu_to_le16(smid);
1663 descriptor.SCSITarget.LMID = 0;
1664 descriptor.SCSITarget.IoIndex = cpu_to_le16(io_index);
1665 _base_writeq(*request, &ioc->chip->RequestDescriptorPostLow,
1666 &ioc->scsi_lookup_lock);
1667}
1668
1669/**
Eric Mooref0f9cc12009-04-21 15:40:48 -06001670 * _base_display_dell_branding - Disply branding string
1671 * @ioc: per adapter object
1672 *
1673 * Return nothing.
1674 */
1675static void
1676_base_display_dell_branding(struct MPT2SAS_ADAPTER *ioc)
1677{
1678 char dell_branding[MPT2SAS_DELL_BRANDING_SIZE];
1679
1680 if (ioc->pdev->subsystem_vendor != PCI_VENDOR_ID_DELL)
1681 return;
1682
1683 memset(dell_branding, 0, MPT2SAS_DELL_BRANDING_SIZE);
1684 switch (ioc->pdev->subsystem_device) {
1685 case MPT2SAS_DELL_6GBPS_SAS_HBA_SSDID:
1686 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_HBA_BRANDING,
1687 MPT2SAS_DELL_BRANDING_SIZE - 1);
1688 break;
1689 case MPT2SAS_DELL_PERC_H200_ADAPTER_SSDID:
1690 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_ADAPTER_BRANDING,
1691 MPT2SAS_DELL_BRANDING_SIZE - 1);
1692 break;
1693 case MPT2SAS_DELL_PERC_H200_INTEGRATED_SSDID:
1694 strncpy(dell_branding,
1695 MPT2SAS_DELL_PERC_H200_INTEGRATED_BRANDING,
1696 MPT2SAS_DELL_BRANDING_SIZE - 1);
1697 break;
1698 case MPT2SAS_DELL_PERC_H200_MODULAR_SSDID:
1699 strncpy(dell_branding,
1700 MPT2SAS_DELL_PERC_H200_MODULAR_BRANDING,
1701 MPT2SAS_DELL_BRANDING_SIZE - 1);
1702 break;
1703 case MPT2SAS_DELL_PERC_H200_EMBEDDED_SSDID:
1704 strncpy(dell_branding,
1705 MPT2SAS_DELL_PERC_H200_EMBEDDED_BRANDING,
1706 MPT2SAS_DELL_BRANDING_SIZE - 1);
1707 break;
1708 case MPT2SAS_DELL_PERC_H200_SSDID:
1709 strncpy(dell_branding, MPT2SAS_DELL_PERC_H200_BRANDING,
1710 MPT2SAS_DELL_BRANDING_SIZE - 1);
1711 break;
1712 case MPT2SAS_DELL_6GBPS_SAS_SSDID:
1713 strncpy(dell_branding, MPT2SAS_DELL_6GBPS_SAS_BRANDING,
1714 MPT2SAS_DELL_BRANDING_SIZE - 1);
1715 break;
1716 default:
1717 sprintf(dell_branding, "0x%4X", ioc->pdev->subsystem_device);
1718 break;
1719 }
1720
1721 printk(MPT2SAS_INFO_FMT "%s: Vendor(0x%04X), Device(0x%04X),"
1722 " SSVID(0x%04X), SSDID(0x%04X)\n", ioc->name, dell_branding,
1723 ioc->pdev->vendor, ioc->pdev->device, ioc->pdev->subsystem_vendor,
1724 ioc->pdev->subsystem_device);
1725}
1726
1727/**
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301728 * _base_display_intel_branding - Display branding string
1729 * @ioc: per adapter object
1730 *
1731 * Return nothing.
1732 */
1733static void
1734_base_display_intel_branding(struct MPT2SAS_ADAPTER *ioc)
1735{
1736 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_INTEL &&
1737 ioc->pdev->device == MPI2_MFGPAGE_DEVID_SAS2008) {
1738
1739 switch (ioc->pdev->subsystem_device) {
1740 case MPT2SAS_INTEL_RMS2LL080_SSDID:
1741 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1742 MPT2SAS_INTEL_RMS2LL080_BRANDING);
1743 break;
1744 case MPT2SAS_INTEL_RMS2LL040_SSDID:
1745 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
1746 MPT2SAS_INTEL_RMS2LL040_BRANDING);
1747 break;
1748 }
1749 }
1750}
1751
1752/**
Eric Moore635374e2009-03-09 01:21:12 -06001753 * _base_display_ioc_capabilities - Disply IOC's capabilities.
1754 * @ioc: per adapter object
1755 *
1756 * Return nothing.
1757 */
1758static void
1759_base_display_ioc_capabilities(struct MPT2SAS_ADAPTER *ioc)
1760{
1761 int i = 0;
1762 char desc[16];
1763 u8 revision;
1764 u32 iounit_pg1_flags;
1765
1766 pci_read_config_byte(ioc->pdev, PCI_CLASS_REVISION, &revision);
1767 strncpy(desc, ioc->manu_pg0.ChipName, 16);
1768 printk(MPT2SAS_INFO_FMT "%s: FWVersion(%02d.%02d.%02d.%02d), "
1769 "ChipRevision(0x%02x), BiosVersion(%02d.%02d.%02d.%02d)\n",
1770 ioc->name, desc,
1771 (ioc->facts.FWVersion.Word & 0xFF000000) >> 24,
1772 (ioc->facts.FWVersion.Word & 0x00FF0000) >> 16,
1773 (ioc->facts.FWVersion.Word & 0x0000FF00) >> 8,
1774 ioc->facts.FWVersion.Word & 0x000000FF,
1775 revision,
1776 (ioc->bios_pg3.BiosVersion & 0xFF000000) >> 24,
1777 (ioc->bios_pg3.BiosVersion & 0x00FF0000) >> 16,
1778 (ioc->bios_pg3.BiosVersion & 0x0000FF00) >> 8,
1779 ioc->bios_pg3.BiosVersion & 0x000000FF);
1780
Kashyap, Desaied79f122009-08-20 13:23:49 +05301781 _base_display_dell_branding(ioc);
Kashyap, Desaifb396be2011-01-04 11:36:37 +05301782 _base_display_intel_branding(ioc);
Kashyap, Desaied79f122009-08-20 13:23:49 +05301783
Eric Moore635374e2009-03-09 01:21:12 -06001784 printk(MPT2SAS_INFO_FMT "Protocol=(", ioc->name);
1785
1786 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR) {
1787 printk("Initiator");
1788 i++;
1789 }
1790
1791 if (ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_TARGET) {
1792 printk("%sTarget", i ? "," : "");
1793 i++;
1794 }
1795
1796 i = 0;
1797 printk("), ");
1798 printk("Capabilities=(");
1799
1800 if (ioc->facts.IOCCapabilities &
1801 MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID) {
1802 printk("Raid");
1803 i++;
1804 }
1805
1806 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR) {
1807 printk("%sTLR", i ? "," : "");
1808 i++;
1809 }
1810
1811 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_MULTICAST) {
1812 printk("%sMulticast", i ? "," : "");
1813 i++;
1814 }
1815
1816 if (ioc->facts.IOCCapabilities &
1817 MPI2_IOCFACTS_CAPABILITY_BIDIRECTIONAL_TARGET) {
1818 printk("%sBIDI Target", i ? "," : "");
1819 i++;
1820 }
1821
1822 if (ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_EEDP) {
1823 printk("%sEEDP", i ? "," : "");
1824 i++;
1825 }
1826
1827 if (ioc->facts.IOCCapabilities &
1828 MPI2_IOCFACTS_CAPABILITY_SNAPSHOT_BUFFER) {
1829 printk("%sSnapshot Buffer", i ? "," : "");
1830 i++;
1831 }
1832
1833 if (ioc->facts.IOCCapabilities &
1834 MPI2_IOCFACTS_CAPABILITY_DIAG_TRACE_BUFFER) {
1835 printk("%sDiag Trace Buffer", i ? "," : "");
1836 i++;
1837 }
1838
1839 if (ioc->facts.IOCCapabilities &
Kashyap, Desai1b01fe32009-09-23 17:28:59 +05301840 MPI2_IOCFACTS_CAPABILITY_EXTENDED_BUFFER) {
1841 printk(KERN_INFO "%sDiag Extended Buffer", i ? "," : "");
1842 i++;
1843 }
1844
1845 if (ioc->facts.IOCCapabilities &
Eric Moore635374e2009-03-09 01:21:12 -06001846 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING) {
1847 printk("%sTask Set Full", i ? "," : "");
1848 i++;
1849 }
1850
1851 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1852 if (!(iounit_pg1_flags & MPI2_IOUNITPAGE1_NATIVE_COMMAND_Q_DISABLE)) {
1853 printk("%sNCQ", i ? "," : "");
1854 i++;
1855 }
1856
1857 printk(")\n");
1858}
1859
1860/**
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05301861 * _base_update_missing_delay - change the missing delay timers
1862 * @ioc: per adapter object
1863 * @device_missing_delay: amount of time till device is reported missing
1864 * @io_missing_delay: interval IO is returned when there is a missing device
1865 *
1866 * Return nothing.
1867 *
1868 * Passed on the command line, this function will modify the device missing
1869 * delay, as well as the io missing delay. This should be called at driver
1870 * load time.
1871 */
1872static void
1873_base_update_missing_delay(struct MPT2SAS_ADAPTER *ioc,
1874 u16 device_missing_delay, u8 io_missing_delay)
1875{
1876 u16 dmd, dmd_new, dmd_orignal;
1877 u8 io_missing_delay_original;
1878 u16 sz;
1879 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
1880 Mpi2ConfigReply_t mpi_reply;
1881 u8 num_phys = 0;
1882 u16 ioc_status;
1883
1884 mpt2sas_config_get_number_hba_phys(ioc, &num_phys);
1885 if (!num_phys)
1886 return;
1887
1888 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (num_phys *
1889 sizeof(Mpi2SasIOUnit1PhyData_t));
1890 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
1891 if (!sas_iounit_pg1) {
1892 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1893 ioc->name, __FILE__, __LINE__, __func__);
1894 goto out;
1895 }
1896 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
1897 sas_iounit_pg1, sz))) {
1898 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1899 ioc->name, __FILE__, __LINE__, __func__);
1900 goto out;
1901 }
1902 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1903 MPI2_IOCSTATUS_MASK;
1904 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1905 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1906 ioc->name, __FILE__, __LINE__, __func__);
1907 goto out;
1908 }
1909
1910 /* device missing delay */
1911 dmd = sas_iounit_pg1->ReportDeviceMissingDelay;
1912 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
1913 dmd = (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
1914 else
1915 dmd = dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1916 dmd_orignal = dmd;
1917 if (device_missing_delay > 0x7F) {
1918 dmd = (device_missing_delay > 0x7F0) ? 0x7F0 :
1919 device_missing_delay;
1920 dmd = dmd / 16;
1921 dmd |= MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16;
1922 } else
1923 dmd = device_missing_delay;
1924 sas_iounit_pg1->ReportDeviceMissingDelay = dmd;
1925
1926 /* io missing delay */
1927 io_missing_delay_original = sas_iounit_pg1->IODeviceMissingDelay;
1928 sas_iounit_pg1->IODeviceMissingDelay = io_missing_delay;
1929
1930 if (!mpt2sas_config_set_sas_iounit_pg1(ioc, &mpi_reply, sas_iounit_pg1,
1931 sz)) {
1932 if (dmd & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
1933 dmd_new = (dmd &
1934 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
1935 else
1936 dmd_new =
1937 dmd & MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
1938 printk(MPT2SAS_INFO_FMT "device_missing_delay: old(%d), "
1939 "new(%d)\n", ioc->name, dmd_orignal, dmd_new);
1940 printk(MPT2SAS_INFO_FMT "ioc_missing_delay: old(%d), "
1941 "new(%d)\n", ioc->name, io_missing_delay_original,
1942 io_missing_delay);
1943 ioc->device_missing_delay = dmd_new;
1944 ioc->io_missing_delay = io_missing_delay;
1945 }
1946
1947out:
1948 kfree(sas_iounit_pg1);
1949}
1950
1951/**
Eric Moore635374e2009-03-09 01:21:12 -06001952 * _base_static_config_pages - static start of day config pages
1953 * @ioc: per adapter object
1954 *
1955 * Return nothing.
1956 */
1957static void
1958_base_static_config_pages(struct MPT2SAS_ADAPTER *ioc)
1959{
1960 Mpi2ConfigReply_t mpi_reply;
1961 u32 iounit_pg1_flags;
1962
1963 mpt2sas_config_get_manufacturing_pg0(ioc, &mpi_reply, &ioc->manu_pg0);
Kashyap, Desaied79f122009-08-20 13:23:49 +05301964 if (ioc->ir_firmware)
1965 mpt2sas_config_get_manufacturing_pg10(ioc, &mpi_reply,
1966 &ioc->manu_pg10);
Eric Moore635374e2009-03-09 01:21:12 -06001967 mpt2sas_config_get_bios_pg2(ioc, &mpi_reply, &ioc->bios_pg2);
1968 mpt2sas_config_get_bios_pg3(ioc, &mpi_reply, &ioc->bios_pg3);
1969 mpt2sas_config_get_ioc_pg8(ioc, &mpi_reply, &ioc->ioc_pg8);
1970 mpt2sas_config_get_iounit_pg0(ioc, &mpi_reply, &ioc->iounit_pg0);
1971 mpt2sas_config_get_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
1972 _base_display_ioc_capabilities(ioc);
1973
1974 /*
1975 * Enable task_set_full handling in iounit_pg1 when the
1976 * facts capabilities indicate that its supported.
1977 */
1978 iounit_pg1_flags = le32_to_cpu(ioc->iounit_pg1.Flags);
1979 if ((ioc->facts.IOCCapabilities &
1980 MPI2_IOCFACTS_CAPABILITY_TASK_SET_FULL_HANDLING))
1981 iounit_pg1_flags &=
1982 ~MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1983 else
1984 iounit_pg1_flags |=
1985 MPI2_IOUNITPAGE1_DISABLE_TASK_SET_FULL_HANDLING;
1986 ioc->iounit_pg1.Flags = cpu_to_le32(iounit_pg1_flags);
Kashyap, Desai5b768582009-08-20 13:24:31 +05301987 mpt2sas_config_set_iounit_pg1(ioc, &mpi_reply, &ioc->iounit_pg1);
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05301988
Eric Moore635374e2009-03-09 01:21:12 -06001989}
1990
1991/**
1992 * _base_release_memory_pools - release memory
1993 * @ioc: per adapter object
1994 *
1995 * Free memory allocated from _base_allocate_memory_pools.
1996 *
1997 * Return nothing.
1998 */
1999static void
2000_base_release_memory_pools(struct MPT2SAS_ADAPTER *ioc)
2001{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302002 int i;
2003
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302004 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002005 __func__));
2006
2007 if (ioc->request) {
2008 pci_free_consistent(ioc->pdev, ioc->request_dma_sz,
2009 ioc->request, ioc->request_dma);
2010 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "request_pool(0x%p)"
2011 ": free\n", ioc->name, ioc->request));
2012 ioc->request = NULL;
2013 }
2014
2015 if (ioc->sense) {
2016 pci_pool_free(ioc->sense_dma_pool, ioc->sense, ioc->sense_dma);
2017 if (ioc->sense_dma_pool)
2018 pci_pool_destroy(ioc->sense_dma_pool);
2019 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_pool(0x%p)"
2020 ": free\n", ioc->name, ioc->sense));
2021 ioc->sense = NULL;
2022 }
2023
2024 if (ioc->reply) {
2025 pci_pool_free(ioc->reply_dma_pool, ioc->reply, ioc->reply_dma);
2026 if (ioc->reply_dma_pool)
2027 pci_pool_destroy(ioc->reply_dma_pool);
2028 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_pool(0x%p)"
2029 ": free\n", ioc->name, ioc->reply));
2030 ioc->reply = NULL;
2031 }
2032
2033 if (ioc->reply_free) {
2034 pci_pool_free(ioc->reply_free_dma_pool, ioc->reply_free,
2035 ioc->reply_free_dma);
2036 if (ioc->reply_free_dma_pool)
2037 pci_pool_destroy(ioc->reply_free_dma_pool);
2038 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_pool"
2039 "(0x%p): free\n", ioc->name, ioc->reply_free));
2040 ioc->reply_free = NULL;
2041 }
2042
2043 if (ioc->reply_post_free) {
2044 pci_pool_free(ioc->reply_post_free_dma_pool,
2045 ioc->reply_post_free, ioc->reply_post_free_dma);
2046 if (ioc->reply_post_free_dma_pool)
2047 pci_pool_destroy(ioc->reply_post_free_dma_pool);
2048 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2049 "reply_post_free_pool(0x%p): free\n", ioc->name,
2050 ioc->reply_post_free));
2051 ioc->reply_post_free = NULL;
2052 }
2053
2054 if (ioc->config_page) {
2055 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT
2056 "config_page(0x%p): free\n", ioc->name,
2057 ioc->config_page));
2058 pci_free_consistent(ioc->pdev, ioc->config_page_sz,
2059 ioc->config_page, ioc->config_page_dma);
2060 }
2061
Kashyap, Desai66a67932010-04-05 14:21:07 +05302062 if (ioc->scsi_lookup) {
2063 free_pages((ulong)ioc->scsi_lookup, ioc->scsi_lookup_pages);
2064 ioc->scsi_lookup = NULL;
2065 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302066 kfree(ioc->hpr_lookup);
2067 kfree(ioc->internal_lookup);
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302068 if (ioc->chain_lookup) {
2069 for (i = 0; i < ioc->chain_depth; i++) {
2070 if (ioc->chain_lookup[i].chain_buffer)
2071 pci_pool_free(ioc->chain_dma_pool,
2072 ioc->chain_lookup[i].chain_buffer,
2073 ioc->chain_lookup[i].chain_buffer_dma);
2074 }
2075 if (ioc->chain_dma_pool)
2076 pci_pool_destroy(ioc->chain_dma_pool);
2077 }
2078 if (ioc->chain_lookup) {
2079 free_pages((ulong)ioc->chain_lookup, ioc->chain_pages);
2080 ioc->chain_lookup = NULL;
2081 }
Eric Moore635374e2009-03-09 01:21:12 -06002082}
2083
2084
2085/**
2086 * _base_allocate_memory_pools - allocate start of day memory pools
2087 * @ioc: per adapter object
2088 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2089 *
2090 * Returns 0 success, anything else error
2091 */
2092static int
2093_base_allocate_memory_pools(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
2094{
2095 Mpi2IOCFactsReply_t *facts;
2096 u32 queue_size, queue_diff;
2097 u16 max_sge_elements;
2098 u16 num_of_reply_frames;
2099 u16 chains_needed_per_io;
2100 u32 sz, total_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002101 u32 retry_sz;
2102 u16 max_request_credit;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302103 int i;
Eric Moore635374e2009-03-09 01:21:12 -06002104
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302105 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002106 __func__));
2107
2108 retry_sz = 0;
2109 facts = &ioc->facts;
2110
2111 /* command line tunables for max sgl entries */
2112 if (max_sgl_entries != -1) {
2113 ioc->shost->sg_tablesize = (max_sgl_entries <
2114 MPT2SAS_SG_DEPTH) ? max_sgl_entries :
2115 MPT2SAS_SG_DEPTH;
2116 } else {
2117 ioc->shost->sg_tablesize = MPT2SAS_SG_DEPTH;
2118 }
2119
2120 /* command line tunables for max controller queue depth */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302121 if (max_queue_depth != -1)
Eric Moore635374e2009-03-09 01:21:12 -06002122 max_request_credit = (max_queue_depth < facts->RequestCredit)
2123 ? max_queue_depth : facts->RequestCredit;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302124 else
2125 max_request_credit = facts->RequestCredit;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302126
2127 ioc->hba_queue_depth = max_request_credit;
2128 ioc->hi_priority_depth = facts->HighPriorityCredit;
2129 ioc->internal_depth = ioc->hi_priority_depth + 5;
Eric Moore635374e2009-03-09 01:21:12 -06002130
2131 /* request frame size */
2132 ioc->request_sz = facts->IOCRequestFrameSize * 4;
2133
2134 /* reply frame size */
2135 ioc->reply_sz = facts->ReplyFrameSize * 4;
2136
2137 retry_allocation:
2138 total_sz = 0;
2139 /* calculate number of sg elements left over in the 1st frame */
2140 max_sge_elements = ioc->request_sz - ((sizeof(Mpi2SCSIIORequest_t) -
2141 sizeof(Mpi2SGEIOUnion_t)) + ioc->sge_size);
2142 ioc->max_sges_in_main_message = max_sge_elements/ioc->sge_size;
2143
2144 /* now do the same for a chain buffer */
2145 max_sge_elements = ioc->request_sz - ioc->sge_size;
2146 ioc->max_sges_in_chain_message = max_sge_elements/ioc->sge_size;
2147
2148 ioc->chain_offset_value_for_main_message =
2149 ((sizeof(Mpi2SCSIIORequest_t) - sizeof(Mpi2SGEIOUnion_t)) +
2150 (ioc->max_sges_in_chain_message * ioc->sge_size)) / 4;
2151
2152 /*
2153 * MPT2SAS_SG_DEPTH = CONFIG_FUSION_MAX_SGE
2154 */
2155 chains_needed_per_io = ((ioc->shost->sg_tablesize -
2156 ioc->max_sges_in_main_message)/ioc->max_sges_in_chain_message)
2157 + 1;
2158 if (chains_needed_per_io > facts->MaxChainDepth) {
2159 chains_needed_per_io = facts->MaxChainDepth;
2160 ioc->shost->sg_tablesize = min_t(u16,
2161 ioc->max_sges_in_main_message + (ioc->max_sges_in_chain_message
2162 * chains_needed_per_io), ioc->shost->sg_tablesize);
2163 }
2164 ioc->chains_needed_per_io = chains_needed_per_io;
2165
2166 /* reply free queue sizing - taking into account for events */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302167 num_of_reply_frames = ioc->hba_queue_depth + 32;
Eric Moore635374e2009-03-09 01:21:12 -06002168
2169 /* number of replies frames can't be a multiple of 16 */
2170 /* decrease number of reply frames by 1 */
2171 if (!(num_of_reply_frames % 16))
2172 num_of_reply_frames--;
2173
2174 /* calculate number of reply free queue entries
2175 * (must be multiple of 16)
2176 */
2177
2178 /* (we know reply_free_queue_depth is not a multiple of 16) */
2179 queue_size = num_of_reply_frames;
2180 queue_size += 16 - (queue_size % 16);
2181 ioc->reply_free_queue_depth = queue_size;
2182
2183 /* reply descriptor post queue sizing */
2184 /* this size should be the number of request frames + number of reply
2185 * frames
2186 */
2187
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302188 queue_size = ioc->hba_queue_depth + num_of_reply_frames + 1;
Eric Moore635374e2009-03-09 01:21:12 -06002189 /* round up to 16 byte boundary */
2190 if (queue_size % 16)
2191 queue_size += 16 - (queue_size % 16);
2192
2193 /* check against IOC maximum reply post queue depth */
2194 if (queue_size > facts->MaxReplyDescriptorPostQueueDepth) {
2195 queue_diff = queue_size -
2196 facts->MaxReplyDescriptorPostQueueDepth;
2197
2198 /* round queue_diff up to multiple of 16 */
2199 if (queue_diff % 16)
2200 queue_diff += 16 - (queue_diff % 16);
2201
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302202 /* adjust hba_queue_depth, reply_free_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002203 * and queue_size
2204 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302205 ioc->hba_queue_depth -= queue_diff;
Eric Moore635374e2009-03-09 01:21:12 -06002206 ioc->reply_free_queue_depth -= queue_diff;
2207 queue_size -= queue_diff;
2208 }
2209 ioc->reply_post_queue_depth = queue_size;
2210
Eric Moore635374e2009-03-09 01:21:12 -06002211 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scatter gather: "
2212 "sge_in_main_msg(%d), sge_per_chain(%d), sge_per_io(%d), "
2213 "chains_per_io(%d)\n", ioc->name, ioc->max_sges_in_main_message,
2214 ioc->max_sges_in_chain_message, ioc->shost->sg_tablesize,
2215 ioc->chains_needed_per_io));
2216
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302217 ioc->scsiio_depth = ioc->hba_queue_depth -
2218 ioc->hi_priority_depth - ioc->internal_depth;
2219
2220 /* set the scsi host can_queue depth
2221 * with some internal commands that could be outstanding
2222 */
2223 ioc->shost->can_queue = ioc->scsiio_depth - (2);
2224 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsi host: "
2225 "can_queue depth (%d)\n", ioc->name, ioc->shost->can_queue));
2226
Eric Moore635374e2009-03-09 01:21:12 -06002227 /* contiguous pool for request and chains, 16 byte align, one extra "
2228 * "frame for smid=0
2229 */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302230 ioc->chain_depth = ioc->chains_needed_per_io * ioc->scsiio_depth;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302231 sz = ((ioc->scsiio_depth + 1) * ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302232
2233 /* hi-priority queue */
2234 sz += (ioc->hi_priority_depth * ioc->request_sz);
2235
2236 /* internal queue */
2237 sz += (ioc->internal_depth * ioc->request_sz);
Eric Moore635374e2009-03-09 01:21:12 -06002238
2239 ioc->request_dma_sz = sz;
2240 ioc->request = pci_alloc_consistent(ioc->pdev, sz, &ioc->request_dma);
2241 if (!ioc->request) {
2242 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302243 "failed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2244 "total(%d kB)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002245 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302246 if (ioc->scsiio_depth < MPT2SAS_SAS_QUEUE_DEPTH)
Eric Moore635374e2009-03-09 01:21:12 -06002247 goto out;
2248 retry_sz += 64;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302249 ioc->hba_queue_depth = max_request_credit - retry_sz;
Eric Moore635374e2009-03-09 01:21:12 -06002250 goto retry_allocation;
2251 }
2252
2253 if (retry_sz)
2254 printk(MPT2SAS_ERR_FMT "request pool: pci_alloc_consistent "
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302255 "succeed: hba_depth(%d), chains_per_io(%d), frame_sz(%d), "
2256 "total(%d kb)\n", ioc->name, ioc->hba_queue_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002257 ioc->chains_needed_per_io, ioc->request_sz, sz/1024);
2258
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302259
2260 /* hi-priority queue */
2261 ioc->hi_priority = ioc->request + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002262 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302263 ioc->hi_priority_dma = ioc->request_dma + ((ioc->scsiio_depth + 1) *
Eric Moore635374e2009-03-09 01:21:12 -06002264 ioc->request_sz);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302265
2266 /* internal queue */
2267 ioc->internal = ioc->hi_priority + (ioc->hi_priority_depth *
2268 ioc->request_sz);
2269 ioc->internal_dma = ioc->hi_priority_dma + (ioc->hi_priority_depth *
2270 ioc->request_sz);
2271
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302272
Eric Moore635374e2009-03-09 01:21:12 -06002273 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool(0x%p): "
2274 "depth(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302275 ioc->request, ioc->hba_queue_depth, ioc->request_sz,
2276 (ioc->hba_queue_depth * ioc->request_sz)/1024));
Eric Moore635374e2009-03-09 01:21:12 -06002277 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request pool: dma(0x%llx)\n",
2278 ioc->name, (unsigned long long) ioc->request_dma));
2279 total_sz += sz;
2280
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302281 sz = ioc->scsiio_depth * sizeof(struct request_tracker);
2282 ioc->scsi_lookup_pages = get_order(sz);
2283 ioc->scsi_lookup = (struct request_tracker *)__get_free_pages(
2284 GFP_KERNEL, ioc->scsi_lookup_pages);
Eric Moore635374e2009-03-09 01:21:12 -06002285 if (!ioc->scsi_lookup) {
Kashyap, Desai89009fb2010-03-17 16:22:52 +05302286 printk(MPT2SAS_ERR_FMT "scsi_lookup: get_free_pages failed, "
2287 "sz(%d)\n", ioc->name, (int)sz);
Eric Moore635374e2009-03-09 01:21:12 -06002288 goto out;
2289 }
2290
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302291 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "scsiio(0x%p): "
2292 "depth(%d)\n", ioc->name, ioc->request,
2293 ioc->scsiio_depth));
2294
Kashyap, Desai35f805b2010-11-13 04:34:06 +05302295 /* loop till the allocation succeeds */
2296 do {
2297 sz = ioc->chain_depth * sizeof(struct chain_tracker);
2298 ioc->chain_pages = get_order(sz);
2299 ioc->chain_lookup = (struct chain_tracker *)__get_free_pages(
2300 GFP_KERNEL, ioc->chain_pages);
2301 if (ioc->chain_lookup == NULL)
2302 ioc->chain_depth -= 100;
2303 } while (ioc->chain_lookup == NULL);
2304 ioc->chain_dma_pool = pci_pool_create("chain pool", ioc->pdev,
2305 ioc->request_sz, 16, 0);
2306 if (!ioc->chain_dma_pool) {
2307 printk(MPT2SAS_ERR_FMT "chain_dma_pool: pci_pool_create "
2308 "failed\n", ioc->name);
2309 goto out;
2310 }
2311 for (i = 0; i < ioc->chain_depth; i++) {
2312 ioc->chain_lookup[i].chain_buffer = pci_pool_alloc(
2313 ioc->chain_dma_pool , GFP_KERNEL,
2314 &ioc->chain_lookup[i].chain_buffer_dma);
2315 if (!ioc->chain_lookup[i].chain_buffer) {
2316 ioc->chain_depth = i;
2317 goto chain_done;
2318 }
2319 total_sz += ioc->request_sz;
2320 }
2321chain_done:
2322 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "chain pool depth"
2323 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name,
2324 ioc->chain_depth, ioc->request_sz, ((ioc->chain_depth *
2325 ioc->request_sz))/1024));
2326
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302327 /* initialize hi-priority queue smid's */
2328 ioc->hpr_lookup = kcalloc(ioc->hi_priority_depth,
2329 sizeof(struct request_tracker), GFP_KERNEL);
2330 if (!ioc->hpr_lookup) {
2331 printk(MPT2SAS_ERR_FMT "hpr_lookup: kcalloc failed\n",
2332 ioc->name);
2333 goto out;
2334 }
2335 ioc->hi_priority_smid = ioc->scsiio_depth + 1;
2336 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hi_priority(0x%p): "
2337 "depth(%d), start smid(%d)\n", ioc->name, ioc->hi_priority,
2338 ioc->hi_priority_depth, ioc->hi_priority_smid));
2339
2340 /* initialize internal queue smid's */
2341 ioc->internal_lookup = kcalloc(ioc->internal_depth,
2342 sizeof(struct request_tracker), GFP_KERNEL);
2343 if (!ioc->internal_lookup) {
2344 printk(MPT2SAS_ERR_FMT "internal_lookup: kcalloc failed\n",
2345 ioc->name);
2346 goto out;
2347 }
2348 ioc->internal_smid = ioc->hi_priority_smid + ioc->hi_priority_depth;
2349 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "internal(0x%p): "
2350 "depth(%d), start smid(%d)\n", ioc->name, ioc->internal,
2351 ioc->internal_depth, ioc->internal_smid));
Eric Moore635374e2009-03-09 01:21:12 -06002352
2353 /* sense buffers, 4 byte align */
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302354 sz = ioc->scsiio_depth * SCSI_SENSE_BUFFERSIZE;
Eric Moore635374e2009-03-09 01:21:12 -06002355 ioc->sense_dma_pool = pci_pool_create("sense pool", ioc->pdev, sz, 4,
2356 0);
2357 if (!ioc->sense_dma_pool) {
2358 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_create failed\n",
2359 ioc->name);
2360 goto out;
2361 }
2362 ioc->sense = pci_pool_alloc(ioc->sense_dma_pool , GFP_KERNEL,
2363 &ioc->sense_dma);
2364 if (!ioc->sense) {
2365 printk(MPT2SAS_ERR_FMT "sense pool: pci_pool_alloc failed\n",
2366 ioc->name);
2367 goto out;
2368 }
2369 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
2370 "sense pool(0x%p): depth(%d), element_size(%d), pool_size"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302371 "(%d kB)\n", ioc->name, ioc->sense, ioc->scsiio_depth,
Eric Moore635374e2009-03-09 01:21:12 -06002372 SCSI_SENSE_BUFFERSIZE, sz/1024));
2373 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "sense_dma(0x%llx)\n",
2374 ioc->name, (unsigned long long)ioc->sense_dma));
2375 total_sz += sz;
2376
2377 /* reply pool, 4 byte align */
2378 sz = ioc->reply_free_queue_depth * ioc->reply_sz;
2379 ioc->reply_dma_pool = pci_pool_create("reply pool", ioc->pdev, sz, 4,
2380 0);
2381 if (!ioc->reply_dma_pool) {
2382 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_create failed\n",
2383 ioc->name);
2384 goto out;
2385 }
2386 ioc->reply = pci_pool_alloc(ioc->reply_dma_pool , GFP_KERNEL,
2387 &ioc->reply_dma);
2388 if (!ioc->reply) {
2389 printk(MPT2SAS_ERR_FMT "reply pool: pci_pool_alloc failed\n",
2390 ioc->name);
2391 goto out;
2392 }
Kashyap, Desaidd3741d2010-11-13 04:31:14 +05302393 ioc->reply_dma_min_address = (u32)(ioc->reply_dma);
2394 ioc->reply_dma_max_address = (u32)(ioc->reply_dma) + sz;
Eric Moore635374e2009-03-09 01:21:12 -06002395 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply pool(0x%p): depth"
2396 "(%d), frame_size(%d), pool_size(%d kB)\n", ioc->name, ioc->reply,
2397 ioc->reply_free_queue_depth, ioc->reply_sz, sz/1024));
2398 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_dma(0x%llx)\n",
2399 ioc->name, (unsigned long long)ioc->reply_dma));
2400 total_sz += sz;
2401
2402 /* reply free queue, 16 byte align */
2403 sz = ioc->reply_free_queue_depth * 4;
2404 ioc->reply_free_dma_pool = pci_pool_create("reply_free pool",
2405 ioc->pdev, sz, 16, 0);
2406 if (!ioc->reply_free_dma_pool) {
2407 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_create "
2408 "failed\n", ioc->name);
2409 goto out;
2410 }
2411 ioc->reply_free = pci_pool_alloc(ioc->reply_free_dma_pool , GFP_KERNEL,
2412 &ioc->reply_free_dma);
2413 if (!ioc->reply_free) {
2414 printk(MPT2SAS_ERR_FMT "reply_free pool: pci_pool_alloc "
2415 "failed\n", ioc->name);
2416 goto out;
2417 }
2418 memset(ioc->reply_free, 0, sz);
2419 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free pool(0x%p): "
2420 "depth(%d), element_size(%d), pool_size(%d kB)\n", ioc->name,
2421 ioc->reply_free, ioc->reply_free_queue_depth, 4, sz/1024));
2422 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_free_dma"
2423 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->reply_free_dma));
2424 total_sz += sz;
2425
2426 /* reply post queue, 16 byte align */
2427 sz = ioc->reply_post_queue_depth * sizeof(Mpi2DefaultReplyDescriptor_t);
2428 ioc->reply_post_free_dma_pool = pci_pool_create("reply_post_free pool",
2429 ioc->pdev, sz, 16, 0);
2430 if (!ioc->reply_post_free_dma_pool) {
2431 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_create "
2432 "failed\n", ioc->name);
2433 goto out;
2434 }
2435 ioc->reply_post_free = pci_pool_alloc(ioc->reply_post_free_dma_pool ,
2436 GFP_KERNEL, &ioc->reply_post_free_dma);
2437 if (!ioc->reply_post_free) {
2438 printk(MPT2SAS_ERR_FMT "reply_post_free pool: pci_pool_alloc "
2439 "failed\n", ioc->name);
2440 goto out;
2441 }
2442 memset(ioc->reply_post_free, 0, sz);
2443 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply post free pool"
2444 "(0x%p): depth(%d), element_size(%d), pool_size(%d kB)\n",
2445 ioc->name, ioc->reply_post_free, ioc->reply_post_queue_depth, 8,
2446 sz/1024));
2447 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "reply_post_free_dma = "
2448 "(0x%llx)\n", ioc->name, (unsigned long long)
2449 ioc->reply_post_free_dma));
2450 total_sz += sz;
2451
2452 ioc->config_page_sz = 512;
2453 ioc->config_page = pci_alloc_consistent(ioc->pdev,
2454 ioc->config_page_sz, &ioc->config_page_dma);
2455 if (!ioc->config_page) {
2456 printk(MPT2SAS_ERR_FMT "config page: pci_pool_alloc "
2457 "failed\n", ioc->name);
2458 goto out;
2459 }
2460 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config page(0x%p): size"
2461 "(%d)\n", ioc->name, ioc->config_page, ioc->config_page_sz));
2462 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "config_page_dma"
2463 "(0x%llx)\n", ioc->name, (unsigned long long)ioc->config_page_dma));
2464 total_sz += ioc->config_page_sz;
2465
2466 printk(MPT2SAS_INFO_FMT "Allocated physical memory: size(%d kB)\n",
2467 ioc->name, total_sz/1024);
2468 printk(MPT2SAS_INFO_FMT "Current Controller Queue Depth(%d), "
2469 "Max Controller Queue Depth(%d)\n",
2470 ioc->name, ioc->shost->can_queue, facts->RequestCredit);
2471 printk(MPT2SAS_INFO_FMT "Scatter Gather Elements per IO(%d)\n",
2472 ioc->name, ioc->shost->sg_tablesize);
2473 return 0;
2474
2475 out:
Eric Moore635374e2009-03-09 01:21:12 -06002476 return -ENOMEM;
2477}
2478
2479
2480/**
2481 * mpt2sas_base_get_iocstate - Get the current state of a MPT adapter.
2482 * @ioc: Pointer to MPT_ADAPTER structure
2483 * @cooked: Request raw or cooked IOC state
2484 *
2485 * Returns all IOC Doorbell register bits if cooked==0, else just the
2486 * Doorbell bits in MPI_IOC_STATE_MASK.
2487 */
2488u32
2489mpt2sas_base_get_iocstate(struct MPT2SAS_ADAPTER *ioc, int cooked)
2490{
2491 u32 s, sc;
2492
2493 s = readl(&ioc->chip->Doorbell);
2494 sc = s & MPI2_IOC_STATE_MASK;
2495 return cooked ? sc : s;
2496}
2497
2498/**
2499 * _base_wait_on_iocstate - waiting on a particular ioc state
2500 * @ioc_state: controller state { READY, OPERATIONAL, or RESET }
2501 * @timeout: timeout in second
2502 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2503 *
2504 * Returns 0 for success, non-zero for failure.
2505 */
2506static int
2507_base_wait_on_iocstate(struct MPT2SAS_ADAPTER *ioc, u32 ioc_state, int timeout,
2508 int sleep_flag)
2509{
2510 u32 count, cntdn;
2511 u32 current_state;
2512
2513 count = 0;
2514 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2515 do {
2516 current_state = mpt2sas_base_get_iocstate(ioc, 1);
2517 if (current_state == ioc_state)
2518 return 0;
2519 if (count && current_state == MPI2_IOC_STATE_FAULT)
2520 break;
2521 if (sleep_flag == CAN_SLEEP)
2522 msleep(1);
2523 else
2524 udelay(500);
2525 count++;
2526 } while (--cntdn);
2527
2528 return current_state;
2529}
2530
2531/**
2532 * _base_wait_for_doorbell_int - waiting for controller interrupt(generated by
2533 * a write to the doorbell)
2534 * @ioc: per adapter object
2535 * @timeout: timeout in second
2536 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2537 *
2538 * Returns 0 for success, non-zero for failure.
2539 *
2540 * Notes: MPI2_HIS_IOC2SYS_DB_STATUS - set to one when IOC writes to doorbell.
2541 */
2542static int
2543_base_wait_for_doorbell_int(struct MPT2SAS_ADAPTER *ioc, int timeout,
2544 int sleep_flag)
2545{
2546 u32 cntdn, count;
2547 u32 int_status;
2548
2549 count = 0;
2550 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2551 do {
2552 int_status = readl(&ioc->chip->HostInterruptStatus);
2553 if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302554 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002555 "successfull count(%d), timeout(%d)\n", ioc->name,
2556 __func__, count, timeout));
2557 return 0;
2558 }
2559 if (sleep_flag == CAN_SLEEP)
2560 msleep(1);
2561 else
2562 udelay(500);
2563 count++;
2564 } while (--cntdn);
2565
2566 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2567 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2568 return -EFAULT;
2569}
2570
2571/**
2572 * _base_wait_for_doorbell_ack - waiting for controller to read the doorbell.
2573 * @ioc: per adapter object
2574 * @timeout: timeout in second
2575 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2576 *
2577 * Returns 0 for success, non-zero for failure.
2578 *
2579 * Notes: MPI2_HIS_SYS2IOC_DB_STATUS - set to one when host writes to
2580 * doorbell.
2581 */
2582static int
2583_base_wait_for_doorbell_ack(struct MPT2SAS_ADAPTER *ioc, int timeout,
2584 int sleep_flag)
2585{
2586 u32 cntdn, count;
2587 u32 int_status;
2588 u32 doorbell;
2589
2590 count = 0;
2591 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2592 do {
2593 int_status = readl(&ioc->chip->HostInterruptStatus);
2594 if (!(int_status & MPI2_HIS_SYS2IOC_DB_STATUS)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302595 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002596 "successfull count(%d), timeout(%d)\n", ioc->name,
2597 __func__, count, timeout));
2598 return 0;
2599 } else if (int_status & MPI2_HIS_IOC2SYS_DB_STATUS) {
2600 doorbell = readl(&ioc->chip->Doorbell);
2601 if ((doorbell & MPI2_IOC_STATE_MASK) ==
2602 MPI2_IOC_STATE_FAULT) {
2603 mpt2sas_base_fault_info(ioc , doorbell);
2604 return -EFAULT;
2605 }
2606 } else if (int_status == 0xFFFFFFFF)
2607 goto out;
2608
2609 if (sleep_flag == CAN_SLEEP)
2610 msleep(1);
2611 else
2612 udelay(500);
2613 count++;
2614 } while (--cntdn);
2615
2616 out:
2617 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2618 "int_status(%x)!\n", ioc->name, __func__, count, int_status);
2619 return -EFAULT;
2620}
2621
2622/**
2623 * _base_wait_for_doorbell_not_used - waiting for doorbell to not be in use
2624 * @ioc: per adapter object
2625 * @timeout: timeout in second
2626 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2627 *
2628 * Returns 0 for success, non-zero for failure.
2629 *
2630 */
2631static int
2632_base_wait_for_doorbell_not_used(struct MPT2SAS_ADAPTER *ioc, int timeout,
2633 int sleep_flag)
2634{
2635 u32 cntdn, count;
2636 u32 doorbell_reg;
2637
2638 count = 0;
2639 cntdn = (sleep_flag == CAN_SLEEP) ? 1000*timeout : 2000*timeout;
2640 do {
2641 doorbell_reg = readl(&ioc->chip->Doorbell);
2642 if (!(doorbell_reg & MPI2_DOORBELL_USED)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302643 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06002644 "successfull count(%d), timeout(%d)\n", ioc->name,
2645 __func__, count, timeout));
2646 return 0;
2647 }
2648 if (sleep_flag == CAN_SLEEP)
2649 msleep(1);
2650 else
2651 udelay(500);
2652 count++;
2653 } while (--cntdn);
2654
2655 printk(MPT2SAS_ERR_FMT "%s: failed due to timeout count(%d), "
2656 "doorbell_reg(%x)!\n", ioc->name, __func__, count, doorbell_reg);
2657 return -EFAULT;
2658}
2659
2660/**
2661 * _base_send_ioc_reset - send doorbell reset
2662 * @ioc: per adapter object
2663 * @reset_type: currently only supports: MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET
2664 * @timeout: timeout in second
2665 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2666 *
2667 * Returns 0 for success, non-zero for failure.
2668 */
2669static int
2670_base_send_ioc_reset(struct MPT2SAS_ADAPTER *ioc, u8 reset_type, int timeout,
2671 int sleep_flag)
2672{
2673 u32 ioc_state;
2674 int r = 0;
2675
2676 if (reset_type != MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET) {
2677 printk(MPT2SAS_ERR_FMT "%s: unknown reset_type\n",
2678 ioc->name, __func__);
2679 return -EFAULT;
2680 }
2681
2682 if (!(ioc->facts.IOCCapabilities &
2683 MPI2_IOCFACTS_CAPABILITY_EVENT_REPLAY))
2684 return -EFAULT;
2685
2686 printk(MPT2SAS_INFO_FMT "sending message unit reset !!\n", ioc->name);
2687
2688 writel(reset_type << MPI2_DOORBELL_FUNCTION_SHIFT,
2689 &ioc->chip->Doorbell);
2690 if ((_base_wait_for_doorbell_ack(ioc, 15, sleep_flag))) {
2691 r = -EFAULT;
2692 goto out;
2693 }
2694 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY,
2695 timeout, sleep_flag);
2696 if (ioc_state) {
2697 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
2698 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
2699 r = -EFAULT;
2700 goto out;
2701 }
2702 out:
2703 printk(MPT2SAS_INFO_FMT "message unit reset: %s\n",
2704 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
2705 return r;
2706}
2707
2708/**
2709 * _base_handshake_req_reply_wait - send request thru doorbell interface
2710 * @ioc: per adapter object
2711 * @request_bytes: request length
2712 * @request: pointer having request payload
2713 * @reply_bytes: reply length
2714 * @reply: pointer to reply payload
2715 * @timeout: timeout in second
2716 * @sleep_flag: CAN_SLEEP or NO_SLEEP
2717 *
2718 * Returns 0 for success, non-zero for failure.
2719 */
2720static int
2721_base_handshake_req_reply_wait(struct MPT2SAS_ADAPTER *ioc, int request_bytes,
2722 u32 *request, int reply_bytes, u16 *reply, int timeout, int sleep_flag)
2723{
2724 MPI2DefaultReply_t *default_reply = (MPI2DefaultReply_t *)reply;
2725 int i;
2726 u8 failed;
2727 u16 dummy;
2728 u32 *mfp;
2729
2730 /* make sure doorbell is not in use */
2731 if ((readl(&ioc->chip->Doorbell) & MPI2_DOORBELL_USED)) {
2732 printk(MPT2SAS_ERR_FMT "doorbell is in use "
2733 " (line=%d)\n", ioc->name, __LINE__);
2734 return -EFAULT;
2735 }
2736
2737 /* clear pending doorbell interrupts from previous state changes */
2738 if (readl(&ioc->chip->HostInterruptStatus) &
2739 MPI2_HIS_IOC2SYS_DB_STATUS)
2740 writel(0, &ioc->chip->HostInterruptStatus);
2741
2742 /* send message to ioc */
2743 writel(((MPI2_FUNCTION_HANDSHAKE<<MPI2_DOORBELL_FUNCTION_SHIFT) |
2744 ((request_bytes/4)<<MPI2_DOORBELL_ADD_DWORDS_SHIFT)),
2745 &ioc->chip->Doorbell);
2746
Kashyap, Desai29786e12009-09-14 11:06:21 +05302747 if ((_base_wait_for_doorbell_int(ioc, 5, NO_SLEEP))) {
Eric Moore635374e2009-03-09 01:21:12 -06002748 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2749 "int failed (line=%d)\n", ioc->name, __LINE__);
2750 return -EFAULT;
2751 }
2752 writel(0, &ioc->chip->HostInterruptStatus);
2753
2754 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag))) {
2755 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2756 "ack failed (line=%d)\n", ioc->name, __LINE__);
2757 return -EFAULT;
2758 }
2759
2760 /* send message 32-bits at a time */
2761 for (i = 0, failed = 0; i < request_bytes/4 && !failed; i++) {
2762 writel(cpu_to_le32(request[i]), &ioc->chip->Doorbell);
2763 if ((_base_wait_for_doorbell_ack(ioc, 5, sleep_flag)))
2764 failed = 1;
2765 }
2766
2767 if (failed) {
2768 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2769 "sending request failed (line=%d)\n", ioc->name, __LINE__);
2770 return -EFAULT;
2771 }
2772
2773 /* now wait for the reply */
2774 if ((_base_wait_for_doorbell_int(ioc, timeout, sleep_flag))) {
2775 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2776 "int failed (line=%d)\n", ioc->name, __LINE__);
2777 return -EFAULT;
2778 }
2779
2780 /* read the first two 16-bits, it gives the total length of the reply */
2781 reply[0] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2782 & MPI2_DOORBELL_DATA_MASK);
2783 writel(0, &ioc->chip->HostInterruptStatus);
2784 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2785 printk(MPT2SAS_ERR_FMT "doorbell handshake "
2786 "int failed (line=%d)\n", ioc->name, __LINE__);
2787 return -EFAULT;
2788 }
2789 reply[1] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2790 & MPI2_DOORBELL_DATA_MASK);
2791 writel(0, &ioc->chip->HostInterruptStatus);
2792
2793 for (i = 2; i < default_reply->MsgLength * 2; i++) {
2794 if ((_base_wait_for_doorbell_int(ioc, 5, sleep_flag))) {
2795 printk(MPT2SAS_ERR_FMT "doorbell "
2796 "handshake int failed (line=%d)\n", ioc->name,
2797 __LINE__);
2798 return -EFAULT;
2799 }
2800 if (i >= reply_bytes/2) /* overflow case */
2801 dummy = readl(&ioc->chip->Doorbell);
2802 else
2803 reply[i] = le16_to_cpu(readl(&ioc->chip->Doorbell)
2804 & MPI2_DOORBELL_DATA_MASK);
2805 writel(0, &ioc->chip->HostInterruptStatus);
2806 }
2807
2808 _base_wait_for_doorbell_int(ioc, 5, sleep_flag);
2809 if (_base_wait_for_doorbell_not_used(ioc, 5, sleep_flag) != 0) {
2810 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "doorbell is in use "
2811 " (line=%d)\n", ioc->name, __LINE__));
2812 }
2813 writel(0, &ioc->chip->HostInterruptStatus);
2814
2815 if (ioc->logging_level & MPT_DEBUG_INIT) {
2816 mfp = (u32 *)reply;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302817 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06002818 for (i = 0; i < reply_bytes/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302819 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06002820 le32_to_cpu(mfp[i]));
2821 }
2822 return 0;
2823}
2824
2825/**
2826 * mpt2sas_base_sas_iounit_control - send sas iounit control to FW
2827 * @ioc: per adapter object
2828 * @mpi_reply: the reply payload from FW
2829 * @mpi_request: the request payload sent to FW
2830 *
2831 * The SAS IO Unit Control Request message allows the host to perform low-level
2832 * operations, such as resets on the PHYs of the IO Unit, also allows the host
2833 * to obtain the IOC assigned device handles for a device if it has other
2834 * identifying information about the device, in addition allows the host to
2835 * remove IOC resources associated with the device.
2836 *
2837 * Returns 0 for success, non-zero for failure.
2838 */
2839int
2840mpt2sas_base_sas_iounit_control(struct MPT2SAS_ADAPTER *ioc,
2841 Mpi2SasIoUnitControlReply_t *mpi_reply,
2842 Mpi2SasIoUnitControlRequest_t *mpi_request)
2843{
2844 u16 smid;
2845 u32 ioc_state;
2846 unsigned long timeleft;
2847 u8 issue_reset;
2848 int rc;
2849 void *request;
2850 u16 wait_state_count;
2851
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302852 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002853 __func__));
2854
2855 mutex_lock(&ioc->base_cmds.mutex);
2856
2857 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2858 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2859 ioc->name, __func__);
2860 rc = -EAGAIN;
2861 goto out;
2862 }
2863
2864 wait_state_count = 0;
2865 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2866 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2867 if (wait_state_count++ == 10) {
2868 printk(MPT2SAS_ERR_FMT
2869 "%s: failed due to ioc not operational\n",
2870 ioc->name, __func__);
2871 rc = -EFAULT;
2872 goto out;
2873 }
2874 ssleep(1);
2875 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2876 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2877 "operational state(count=%d)\n", ioc->name,
2878 __func__, wait_state_count);
2879 }
2880
2881 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2882 if (!smid) {
2883 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2884 ioc->name, __func__);
2885 rc = -EAGAIN;
2886 goto out;
2887 }
2888
2889 rc = 0;
2890 ioc->base_cmds.status = MPT2_CMD_PENDING;
2891 request = mpt2sas_base_get_msg_frame(ioc, smid);
2892 ioc->base_cmds.smid = smid;
2893 memcpy(request, mpi_request, sizeof(Mpi2SasIoUnitControlRequest_t));
2894 if (mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2895 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET)
2896 ioc->ioc_link_reset_in_progress = 1;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302897 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05302898 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06002899 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
2900 msecs_to_jiffies(10000));
2901 if ((mpi_request->Operation == MPI2_SAS_OP_PHY_HARD_RESET ||
2902 mpi_request->Operation == MPI2_SAS_OP_PHY_LINK_RESET) &&
2903 ioc->ioc_link_reset_in_progress)
2904 ioc->ioc_link_reset_in_progress = 0;
2905 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
2906 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2907 ioc->name, __func__);
2908 _debug_dump_mf(mpi_request,
2909 sizeof(Mpi2SasIoUnitControlRequest_t)/4);
2910 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
2911 issue_reset = 1;
2912 goto issue_host_reset;
2913 }
2914 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
2915 memcpy(mpi_reply, ioc->base_cmds.reply,
2916 sizeof(Mpi2SasIoUnitControlReply_t));
2917 else
2918 memset(mpi_reply, 0, sizeof(Mpi2SasIoUnitControlReply_t));
2919 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2920 goto out;
2921
2922 issue_host_reset:
2923 if (issue_reset)
2924 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2925 FORCE_BIG_HAMMER);
2926 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
2927 rc = -EFAULT;
2928 out:
2929 mutex_unlock(&ioc->base_cmds.mutex);
2930 return rc;
2931}
2932
2933
2934/**
2935 * mpt2sas_base_scsi_enclosure_processor - sending request to sep device
2936 * @ioc: per adapter object
2937 * @mpi_reply: the reply payload from FW
2938 * @mpi_request: the request payload sent to FW
2939 *
2940 * The SCSI Enclosure Processor request message causes the IOC to
2941 * communicate with SES devices to control LED status signals.
2942 *
2943 * Returns 0 for success, non-zero for failure.
2944 */
2945int
2946mpt2sas_base_scsi_enclosure_processor(struct MPT2SAS_ADAPTER *ioc,
2947 Mpi2SepReply_t *mpi_reply, Mpi2SepRequest_t *mpi_request)
2948{
2949 u16 smid;
2950 u32 ioc_state;
2951 unsigned long timeleft;
2952 u8 issue_reset;
2953 int rc;
2954 void *request;
2955 u16 wait_state_count;
2956
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302957 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06002958 __func__));
2959
2960 mutex_lock(&ioc->base_cmds.mutex);
2961
2962 if (ioc->base_cmds.status != MPT2_CMD_NOT_USED) {
2963 printk(MPT2SAS_ERR_FMT "%s: base_cmd in use\n",
2964 ioc->name, __func__);
2965 rc = -EAGAIN;
2966 goto out;
2967 }
2968
2969 wait_state_count = 0;
2970 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2971 while (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2972 if (wait_state_count++ == 10) {
2973 printk(MPT2SAS_ERR_FMT
2974 "%s: failed due to ioc not operational\n",
2975 ioc->name, __func__);
2976 rc = -EFAULT;
2977 goto out;
2978 }
2979 ssleep(1);
2980 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
2981 printk(MPT2SAS_INFO_FMT "%s: waiting for "
2982 "operational state(count=%d)\n", ioc->name,
2983 __func__, wait_state_count);
2984 }
2985
2986 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
2987 if (!smid) {
2988 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2989 ioc->name, __func__);
2990 rc = -EAGAIN;
2991 goto out;
2992 }
2993
2994 rc = 0;
2995 ioc->base_cmds.status = MPT2_CMD_PENDING;
2996 request = mpt2sas_base_get_msg_frame(ioc, smid);
2997 ioc->base_cmds.smid = smid;
2998 memcpy(request, mpi_request, sizeof(Mpi2SepReply_t));
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302999 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303000 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003001 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3002 msecs_to_jiffies(10000));
3003 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3004 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3005 ioc->name, __func__);
3006 _debug_dump_mf(mpi_request,
3007 sizeof(Mpi2SepRequest_t)/4);
3008 if (!(ioc->base_cmds.status & MPT2_CMD_RESET))
3009 issue_reset = 1;
3010 goto issue_host_reset;
3011 }
3012 if (ioc->base_cmds.status & MPT2_CMD_REPLY_VALID)
3013 memcpy(mpi_reply, ioc->base_cmds.reply,
3014 sizeof(Mpi2SepReply_t));
3015 else
3016 memset(mpi_reply, 0, sizeof(Mpi2SepReply_t));
3017 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3018 goto out;
3019
3020 issue_host_reset:
3021 if (issue_reset)
3022 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
3023 FORCE_BIG_HAMMER);
3024 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3025 rc = -EFAULT;
3026 out:
3027 mutex_unlock(&ioc->base_cmds.mutex);
3028 return rc;
3029}
3030
3031/**
3032 * _base_get_port_facts - obtain port facts reply and save in ioc
3033 * @ioc: per adapter object
3034 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3035 *
3036 * Returns 0 for success, non-zero for failure.
3037 */
3038static int
3039_base_get_port_facts(struct MPT2SAS_ADAPTER *ioc, int port, int sleep_flag)
3040{
3041 Mpi2PortFactsRequest_t mpi_request;
3042 Mpi2PortFactsReply_t mpi_reply, *pfacts;
3043 int mpi_reply_sz, mpi_request_sz, r;
3044
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303045 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003046 __func__));
3047
3048 mpi_reply_sz = sizeof(Mpi2PortFactsReply_t);
3049 mpi_request_sz = sizeof(Mpi2PortFactsRequest_t);
3050 memset(&mpi_request, 0, mpi_request_sz);
3051 mpi_request.Function = MPI2_FUNCTION_PORT_FACTS;
3052 mpi_request.PortNumber = port;
3053 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3054 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3055
3056 if (r != 0) {
3057 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3058 ioc->name, __func__, r);
3059 return r;
3060 }
3061
3062 pfacts = &ioc->pfacts[port];
3063 memset(pfacts, 0, sizeof(Mpi2PortFactsReply_t));
3064 pfacts->PortNumber = mpi_reply.PortNumber;
3065 pfacts->VP_ID = mpi_reply.VP_ID;
3066 pfacts->VF_ID = mpi_reply.VF_ID;
3067 pfacts->MaxPostedCmdBuffers =
3068 le16_to_cpu(mpi_reply.MaxPostedCmdBuffers);
3069
3070 return 0;
3071}
3072
3073/**
3074 * _base_get_ioc_facts - obtain ioc facts reply and save in ioc
3075 * @ioc: per adapter object
3076 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3077 *
3078 * Returns 0 for success, non-zero for failure.
3079 */
3080static int
3081_base_get_ioc_facts(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3082{
3083 Mpi2IOCFactsRequest_t mpi_request;
3084 Mpi2IOCFactsReply_t mpi_reply, *facts;
3085 int mpi_reply_sz, mpi_request_sz, r;
3086
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303087 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003088 __func__));
3089
3090 mpi_reply_sz = sizeof(Mpi2IOCFactsReply_t);
3091 mpi_request_sz = sizeof(Mpi2IOCFactsRequest_t);
3092 memset(&mpi_request, 0, mpi_request_sz);
3093 mpi_request.Function = MPI2_FUNCTION_IOC_FACTS;
3094 r = _base_handshake_req_reply_wait(ioc, mpi_request_sz,
3095 (u32 *)&mpi_request, mpi_reply_sz, (u16 *)&mpi_reply, 5, CAN_SLEEP);
3096
3097 if (r != 0) {
3098 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3099 ioc->name, __func__, r);
3100 return r;
3101 }
3102
3103 facts = &ioc->facts;
3104 memset(facts, 0, sizeof(Mpi2IOCFactsReply_t));
3105 facts->MsgVersion = le16_to_cpu(mpi_reply.MsgVersion);
3106 facts->HeaderVersion = le16_to_cpu(mpi_reply.HeaderVersion);
3107 facts->VP_ID = mpi_reply.VP_ID;
3108 facts->VF_ID = mpi_reply.VF_ID;
3109 facts->IOCExceptions = le16_to_cpu(mpi_reply.IOCExceptions);
3110 facts->MaxChainDepth = mpi_reply.MaxChainDepth;
3111 facts->WhoInit = mpi_reply.WhoInit;
3112 facts->NumberOfPorts = mpi_reply.NumberOfPorts;
3113 facts->RequestCredit = le16_to_cpu(mpi_reply.RequestCredit);
3114 facts->MaxReplyDescriptorPostQueueDepth =
3115 le16_to_cpu(mpi_reply.MaxReplyDescriptorPostQueueDepth);
3116 facts->ProductID = le16_to_cpu(mpi_reply.ProductID);
3117 facts->IOCCapabilities = le32_to_cpu(mpi_reply.IOCCapabilities);
3118 if ((facts->IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_INTEGRATED_RAID))
3119 ioc->ir_firmware = 1;
3120 facts->FWVersion.Word = le32_to_cpu(mpi_reply.FWVersion.Word);
3121 facts->IOCRequestFrameSize =
3122 le16_to_cpu(mpi_reply.IOCRequestFrameSize);
3123 facts->MaxInitiators = le16_to_cpu(mpi_reply.MaxInitiators);
3124 facts->MaxTargets = le16_to_cpu(mpi_reply.MaxTargets);
3125 ioc->shost->max_id = -1;
3126 facts->MaxSasExpanders = le16_to_cpu(mpi_reply.MaxSasExpanders);
3127 facts->MaxEnclosures = le16_to_cpu(mpi_reply.MaxEnclosures);
3128 facts->ProtocolFlags = le16_to_cpu(mpi_reply.ProtocolFlags);
3129 facts->HighPriorityCredit =
3130 le16_to_cpu(mpi_reply.HighPriorityCredit);
3131 facts->ReplyFrameSize = mpi_reply.ReplyFrameSize;
3132 facts->MaxDevHandle = le16_to_cpu(mpi_reply.MaxDevHandle);
3133
3134 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "hba queue depth(%d), "
3135 "max chains per io(%d)\n", ioc->name, facts->RequestCredit,
3136 facts->MaxChainDepth));
3137 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "request frame size(%d), "
3138 "reply frame size(%d)\n", ioc->name,
3139 facts->IOCRequestFrameSize * 4, facts->ReplyFrameSize * 4));
3140 return 0;
3141}
3142
3143/**
3144 * _base_send_ioc_init - send ioc_init to firmware
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_send_ioc_init(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003152{
3153 Mpi2IOCInitRequest_t mpi_request;
3154 Mpi2IOCInitReply_t mpi_reply;
3155 int r;
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303156 struct timeval current_time;
Kashyap, Desai463217b2009-10-05 15:53:06 +05303157 u16 ioc_status;
Eric Moore635374e2009-03-09 01:21:12 -06003158
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303159 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003160 __func__));
3161
3162 memset(&mpi_request, 0, sizeof(Mpi2IOCInitRequest_t));
3163 mpi_request.Function = MPI2_FUNCTION_IOC_INIT;
3164 mpi_request.WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303165 mpi_request.VF_ID = 0; /* TODO */
3166 mpi_request.VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003167 mpi_request.MsgVersion = cpu_to_le16(MPI2_VERSION);
3168 mpi_request.HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
3169
3170 /* In MPI Revision I (0xA), the SystemReplyFrameSize(offset 0x18) was
3171 * removed and made reserved. For those with older firmware will need
3172 * this fix. It was decided that the Reply and Request frame sizes are
3173 * the same.
3174 */
3175 if ((ioc->facts.HeaderVersion >> 8) < 0xA) {
3176 mpi_request.Reserved7 = cpu_to_le16(ioc->reply_sz);
3177/* mpi_request.SystemReplyFrameSize =
3178 * cpu_to_le16(ioc->reply_sz);
3179 */
3180 }
3181
3182 mpi_request.SystemRequestFrameSize = cpu_to_le16(ioc->request_sz/4);
3183 mpi_request.ReplyDescriptorPostQueueDepth =
3184 cpu_to_le16(ioc->reply_post_queue_depth);
3185 mpi_request.ReplyFreeQueueDepth =
3186 cpu_to_le16(ioc->reply_free_queue_depth);
3187
3188#if BITS_PER_LONG > 32
3189 mpi_request.SenseBufferAddressHigh =
3190 cpu_to_le32(ioc->sense_dma >> 32);
3191 mpi_request.SystemReplyAddressHigh =
3192 cpu_to_le32(ioc->reply_dma >> 32);
3193 mpi_request.SystemRequestFrameBaseAddress =
3194 cpu_to_le64(ioc->request_dma);
3195 mpi_request.ReplyFreeQueueAddress =
3196 cpu_to_le64(ioc->reply_free_dma);
3197 mpi_request.ReplyDescriptorPostQueueAddress =
3198 cpu_to_le64(ioc->reply_post_free_dma);
3199#else
3200 mpi_request.SystemRequestFrameBaseAddress =
3201 cpu_to_le32(ioc->request_dma);
3202 mpi_request.ReplyFreeQueueAddress =
3203 cpu_to_le32(ioc->reply_free_dma);
3204 mpi_request.ReplyDescriptorPostQueueAddress =
3205 cpu_to_le32(ioc->reply_post_free_dma);
3206#endif
3207
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303208 /* This time stamp specifies number of milliseconds
3209 * since epoch ~ midnight January 1, 1970.
3210 */
3211 do_gettimeofday(&current_time);
Kashyap, Desai7921b352010-03-17 16:21:33 +05303212 mpi_request.TimeStamp = cpu_to_le64((u64)current_time.tv_sec * 1000 +
3213 (current_time.tv_usec / 1000));
Kashyap, Desaia8ebd762009-09-23 17:29:29 +05303214
Eric Moore635374e2009-03-09 01:21:12 -06003215 if (ioc->logging_level & MPT_DEBUG_INIT) {
3216 u32 *mfp;
3217 int i;
3218
3219 mfp = (u32 *)&mpi_request;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303220 printk(KERN_INFO "\toffset:data\n");
Eric Moore635374e2009-03-09 01:21:12 -06003221 for (i = 0; i < sizeof(Mpi2IOCInitRequest_t)/4; i++)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303222 printk(KERN_INFO "\t[0x%02x]:%08x\n", i*4,
Eric Moore635374e2009-03-09 01:21:12 -06003223 le32_to_cpu(mfp[i]));
3224 }
3225
3226 r = _base_handshake_req_reply_wait(ioc,
3227 sizeof(Mpi2IOCInitRequest_t), (u32 *)&mpi_request,
3228 sizeof(Mpi2IOCInitReply_t), (u16 *)&mpi_reply, 10,
3229 sleep_flag);
3230
3231 if (r != 0) {
3232 printk(MPT2SAS_ERR_FMT "%s: handshake failed (r=%d)\n",
3233 ioc->name, __func__, r);
3234 return r;
3235 }
3236
Kashyap, Desai463217b2009-10-05 15:53:06 +05303237 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
3238 if (ioc_status != MPI2_IOCSTATUS_SUCCESS ||
Eric Moore635374e2009-03-09 01:21:12 -06003239 mpi_reply.IOCLogInfo) {
3240 printk(MPT2SAS_ERR_FMT "%s: failed\n", ioc->name, __func__);
3241 r = -EIO;
3242 }
3243
3244 return 0;
3245}
3246
3247/**
3248 * _base_send_port_enable - send port_enable(discovery stuff) to firmware
3249 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003250 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3251 *
3252 * Returns 0 for success, non-zero for failure.
3253 */
3254static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303255_base_send_port_enable(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003256{
3257 Mpi2PortEnableRequest_t *mpi_request;
3258 u32 ioc_state;
3259 unsigned long timeleft;
3260 int r = 0;
3261 u16 smid;
3262
3263 printk(MPT2SAS_INFO_FMT "sending port enable !!\n", ioc->name);
3264
3265 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3266 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3267 ioc->name, __func__);
3268 return -EAGAIN;
3269 }
3270
3271 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3272 if (!smid) {
3273 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3274 ioc->name, __func__);
3275 return -EAGAIN;
3276 }
3277
3278 ioc->base_cmds.status = MPT2_CMD_PENDING;
3279 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3280 ioc->base_cmds.smid = smid;
3281 memset(mpi_request, 0, sizeof(Mpi2PortEnableRequest_t));
3282 mpi_request->Function = MPI2_FUNCTION_PORT_ENABLE;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303283 mpi_request->VF_ID = 0; /* TODO */
3284 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003285
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303286 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303287 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003288 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done,
3289 300*HZ);
3290 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3291 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3292 ioc->name, __func__);
3293 _debug_dump_mf(mpi_request,
3294 sizeof(Mpi2PortEnableRequest_t)/4);
3295 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3296 r = -EFAULT;
3297 else
3298 r = -ETIME;
3299 goto out;
3300 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303301 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003302 ioc->name, __func__));
3303
3304 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_OPERATIONAL,
3305 60, sleep_flag);
3306 if (ioc_state) {
3307 printk(MPT2SAS_ERR_FMT "%s: failed going to operational state "
3308 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3309 r = -EFAULT;
3310 }
3311 out:
3312 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3313 printk(MPT2SAS_INFO_FMT "port enable: %s\n",
3314 ioc->name, ((r == 0) ? "SUCCESS" : "FAILED"));
3315 return r;
3316}
3317
3318/**
3319 * _base_unmask_events - turn on notification for this event
3320 * @ioc: per adapter object
3321 * @event: firmware event
3322 *
3323 * The mask is stored in ioc->event_masks.
3324 */
3325static void
3326_base_unmask_events(struct MPT2SAS_ADAPTER *ioc, u16 event)
3327{
3328 u32 desired_event;
3329
3330 if (event >= 128)
3331 return;
3332
3333 desired_event = (1 << (event % 32));
3334
3335 if (event < 32)
3336 ioc->event_masks[0] &= ~desired_event;
3337 else if (event < 64)
3338 ioc->event_masks[1] &= ~desired_event;
3339 else if (event < 96)
3340 ioc->event_masks[2] &= ~desired_event;
3341 else if (event < 128)
3342 ioc->event_masks[3] &= ~desired_event;
3343}
3344
3345/**
3346 * _base_event_notification - send event notification
3347 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003348 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3349 *
3350 * Returns 0 for success, non-zero for failure.
3351 */
3352static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303353_base_event_notification(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003354{
3355 Mpi2EventNotificationRequest_t *mpi_request;
3356 unsigned long timeleft;
3357 u16 smid;
3358 int r = 0;
3359 int i;
3360
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303361 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003362 __func__));
3363
3364 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3365 printk(MPT2SAS_ERR_FMT "%s: internal command already in use\n",
3366 ioc->name, __func__);
3367 return -EAGAIN;
3368 }
3369
3370 smid = mpt2sas_base_get_smid(ioc, ioc->base_cb_idx);
3371 if (!smid) {
3372 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3373 ioc->name, __func__);
3374 return -EAGAIN;
3375 }
3376 ioc->base_cmds.status = MPT2_CMD_PENDING;
3377 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3378 ioc->base_cmds.smid = smid;
3379 memset(mpi_request, 0, sizeof(Mpi2EventNotificationRequest_t));
3380 mpi_request->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303381 mpi_request->VF_ID = 0; /* TODO */
3382 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003383 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3384 mpi_request->EventMasks[i] =
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303385 cpu_to_le32(ioc->event_masks[i]);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303386 mpt2sas_base_put_smid_default(ioc, smid);
Kashyap, Desaibcfb6e62009-09-14 11:05:24 +05303387 init_completion(&ioc->base_cmds.done);
Eric Moore635374e2009-03-09 01:21:12 -06003388 timeleft = wait_for_completion_timeout(&ioc->base_cmds.done, 30*HZ);
3389 if (!(ioc->base_cmds.status & MPT2_CMD_COMPLETE)) {
3390 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
3391 ioc->name, __func__);
3392 _debug_dump_mf(mpi_request,
3393 sizeof(Mpi2EventNotificationRequest_t)/4);
3394 if (ioc->base_cmds.status & MPT2_CMD_RESET)
3395 r = -EFAULT;
3396 else
3397 r = -ETIME;
3398 } else
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303399 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: complete\n",
Eric Moore635374e2009-03-09 01:21:12 -06003400 ioc->name, __func__));
3401 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3402 return r;
3403}
3404
3405/**
3406 * mpt2sas_base_validate_event_type - validating event types
3407 * @ioc: per adapter object
3408 * @event: firmware event
3409 *
3410 * This will turn on firmware event notification when application
3411 * ask for that event. We don't mask events that are already enabled.
3412 */
3413void
3414mpt2sas_base_validate_event_type(struct MPT2SAS_ADAPTER *ioc, u32 *event_type)
3415{
3416 int i, j;
3417 u32 event_mask, desired_event;
3418 u8 send_update_to_fw;
3419
3420 for (i = 0, send_update_to_fw = 0; i <
3421 MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++) {
3422 event_mask = ~event_type[i];
3423 desired_event = 1;
3424 for (j = 0; j < 32; j++) {
3425 if (!(event_mask & desired_event) &&
3426 (ioc->event_masks[i] & desired_event)) {
3427 ioc->event_masks[i] &= ~desired_event;
3428 send_update_to_fw = 1;
3429 }
3430 desired_event = (desired_event << 1);
3431 }
3432 }
3433
3434 if (!send_update_to_fw)
3435 return;
3436
3437 mutex_lock(&ioc->base_cmds.mutex);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303438 _base_event_notification(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003439 mutex_unlock(&ioc->base_cmds.mutex);
3440}
3441
3442/**
3443 * _base_diag_reset - the "big hammer" start of day reset
3444 * @ioc: per adapter object
3445 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3446 *
3447 * Returns 0 for success, non-zero for failure.
3448 */
3449static int
3450_base_diag_reset(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
3451{
3452 u32 host_diagnostic;
3453 u32 ioc_state;
3454 u32 count;
3455 u32 hcb_size;
3456
3457 printk(MPT2SAS_INFO_FMT "sending diag reset !!\n", ioc->name);
3458
3459 _base_save_msix_table(ioc);
3460
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303461 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "clear interrupts\n",
Eric Moore635374e2009-03-09 01:21:12 -06003462 ioc->name));
Eric Moore635374e2009-03-09 01:21:12 -06003463
3464 count = 0;
3465 do {
3466 /* Write magic sequence to WriteSequence register
3467 * Loop until in diagnostic mode
3468 */
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303469 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "write magic "
Eric Moore635374e2009-03-09 01:21:12 -06003470 "sequence\n", ioc->name));
3471 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3472 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &ioc->chip->WriteSequence);
3473 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &ioc->chip->WriteSequence);
3474 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &ioc->chip->WriteSequence);
3475 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &ioc->chip->WriteSequence);
3476 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &ioc->chip->WriteSequence);
3477 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &ioc->chip->WriteSequence);
3478
3479 /* wait 100 msec */
3480 if (sleep_flag == CAN_SLEEP)
3481 msleep(100);
3482 else
3483 mdelay(100);
3484
3485 if (count++ > 20)
3486 goto out;
3487
3488 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303489 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "wrote magic "
Eric Moore635374e2009-03-09 01:21:12 -06003490 "sequence: count(%d), host_diagnostic(0x%08x)\n",
3491 ioc->name, count, host_diagnostic));
3492
3493 } while ((host_diagnostic & MPI2_DIAG_DIAG_WRITE_ENABLE) == 0);
3494
3495 hcb_size = readl(&ioc->chip->HCBSize);
3496
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303497 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "diag reset: issued\n",
Eric Moore635374e2009-03-09 01:21:12 -06003498 ioc->name));
3499 writel(host_diagnostic | MPI2_DIAG_RESET_ADAPTER,
3500 &ioc->chip->HostDiagnostic);
3501
3502 /* don't access any registers for 50 milliseconds */
3503 msleep(50);
3504
3505 /* 300 second max wait */
3506 for (count = 0; count < 3000000 ; count++) {
3507
3508 host_diagnostic = readl(&ioc->chip->HostDiagnostic);
3509
3510 if (host_diagnostic == 0xFFFFFFFF)
3511 goto out;
3512 if (!(host_diagnostic & MPI2_DIAG_RESET_ADAPTER))
3513 break;
3514
3515 /* wait 100 msec */
3516 if (sleep_flag == CAN_SLEEP)
3517 msleep(1);
3518 else
3519 mdelay(1);
3520 }
3521
3522 if (host_diagnostic & MPI2_DIAG_HCB_MODE) {
3523
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303524 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter "
Eric Moore635374e2009-03-09 01:21:12 -06003525 "assuming the HCB Address points to good F/W\n",
3526 ioc->name));
3527 host_diagnostic &= ~MPI2_DIAG_BOOT_DEVICE_SELECT_MASK;
3528 host_diagnostic |= MPI2_DIAG_BOOT_DEVICE_SELECT_HCDW;
3529 writel(host_diagnostic, &ioc->chip->HostDiagnostic);
3530
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303531 drsprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003532 "re-enable the HCDW\n", ioc->name));
3533 writel(hcb_size | MPI2_HCB_SIZE_HCB_ENABLE,
3534 &ioc->chip->HCBSize);
3535 }
3536
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303537 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "restart the adapter\n",
Eric Moore635374e2009-03-09 01:21:12 -06003538 ioc->name));
3539 writel(host_diagnostic & ~MPI2_DIAG_HOLD_IOC_RESET,
3540 &ioc->chip->HostDiagnostic);
3541
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303542 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "disable writes to the "
Eric Moore635374e2009-03-09 01:21:12 -06003543 "diagnostic register\n", ioc->name));
3544 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &ioc->chip->WriteSequence);
3545
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303546 drsprintk(ioc, printk(MPT2SAS_INFO_FMT "Wait for FW to go to the "
Eric Moore635374e2009-03-09 01:21:12 -06003547 "READY state\n", ioc->name));
3548 ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_READY, 20,
3549 sleep_flag);
3550 if (ioc_state) {
3551 printk(MPT2SAS_ERR_FMT "%s: failed going to ready state "
3552 " (ioc_state=0x%x)\n", ioc->name, __func__, ioc_state);
3553 goto out;
3554 }
3555
3556 _base_restore_msix_table(ioc);
3557 printk(MPT2SAS_INFO_FMT "diag reset: SUCCESS\n", ioc->name);
3558 return 0;
3559
3560 out:
3561 printk(MPT2SAS_ERR_FMT "diag reset: FAILED\n", ioc->name);
3562 return -EFAULT;
3563}
3564
3565/**
3566 * _base_make_ioc_ready - put controller in READY state
3567 * @ioc: per adapter object
3568 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3569 * @type: FORCE_BIG_HAMMER or SOFT_RESET
3570 *
3571 * Returns 0 for success, non-zero for failure.
3572 */
3573static int
3574_base_make_ioc_ready(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
3575 enum reset_type type)
3576{
3577 u32 ioc_state;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303578 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06003579
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303580 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003581 __func__));
3582
Eric Moore3cb54692010-07-08 14:44:34 -06003583 if (ioc->pci_error_recovery)
3584 return 0;
3585
Eric Moore635374e2009-03-09 01:21:12 -06003586 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303587 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: ioc_state(0x%08x)\n",
Eric Moore635374e2009-03-09 01:21:12 -06003588 ioc->name, __func__, ioc_state));
3589
3590 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_READY)
3591 return 0;
3592
3593 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303594 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06003595 "active!\n", ioc->name));
3596 goto issue_diag_reset;
3597 }
3598
3599 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
3600 mpt2sas_base_fault_info(ioc, ioc_state &
3601 MPI2_DOORBELL_DATA_MASK);
3602 goto issue_diag_reset;
3603 }
3604
3605 if (type == FORCE_BIG_HAMMER)
3606 goto issue_diag_reset;
3607
3608 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_OPERATIONAL)
3609 if (!(_base_send_ioc_reset(ioc,
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303610 MPI2_FUNCTION_IOC_MESSAGE_UNIT_RESET, 15, CAN_SLEEP))) {
3611 ioc->ioc_reset_count++;
Eric Moore635374e2009-03-09 01:21:12 -06003612 return 0;
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303613 }
Eric Moore635374e2009-03-09 01:21:12 -06003614
3615 issue_diag_reset:
Kashyap, Desaid32a8c12010-06-17 13:36:53 +05303616 rc = _base_diag_reset(ioc, CAN_SLEEP);
3617 ioc->ioc_reset_count++;
3618 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06003619}
3620
3621/**
3622 * _base_make_ioc_operational - put controller in OPERATIONAL state
3623 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06003624 * @sleep_flag: CAN_SLEEP or NO_SLEEP
3625 *
3626 * Returns 0 for success, non-zero for failure.
3627 */
3628static int
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303629_base_make_ioc_operational(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
Eric Moore635374e2009-03-09 01:21:12 -06003630{
3631 int r, i;
3632 unsigned long flags;
3633 u32 reply_address;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303634 u16 smid;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303635 struct _tr_list *delayed_tr, *delayed_tr_next;
Eric Moore635374e2009-03-09 01:21:12 -06003636
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303637 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003638 __func__));
3639
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303640 /* clean the delayed target reset list */
3641 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3642 &ioc->delayed_tr_list, list) {
3643 list_del(&delayed_tr->list);
3644 kfree(delayed_tr);
3645 }
3646
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303647 list_for_each_entry_safe(delayed_tr, delayed_tr_next,
3648 &ioc->delayed_tr_volume_list, list) {
3649 list_del(&delayed_tr->list);
3650 kfree(delayed_tr);
3651 }
3652
Eric Moore635374e2009-03-09 01:21:12 -06003653 /* initialize the scsi lookup free list */
3654 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3655 INIT_LIST_HEAD(&ioc->free_list);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303656 smid = 1;
3657 for (i = 0; i < ioc->scsiio_depth; i++, smid++) {
Kashyap, Desai35f805b2010-11-13 04:34:06 +05303658 INIT_LIST_HEAD(&ioc->scsi_lookup[i].chain_list);
Eric Moore635374e2009-03-09 01:21:12 -06003659 ioc->scsi_lookup[i].cb_idx = 0xFF;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303660 ioc->scsi_lookup[i].smid = smid;
3661 ioc->scsi_lookup[i].scmd = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003662 list_add_tail(&ioc->scsi_lookup[i].tracker_list,
3663 &ioc->free_list);
3664 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303665
3666 /* hi-priority queue */
3667 INIT_LIST_HEAD(&ioc->hpr_free_list);
3668 smid = ioc->hi_priority_smid;
3669 for (i = 0; i < ioc->hi_priority_depth; i++, smid++) {
3670 ioc->hpr_lookup[i].cb_idx = 0xFF;
3671 ioc->hpr_lookup[i].smid = smid;
3672 list_add_tail(&ioc->hpr_lookup[i].tracker_list,
3673 &ioc->hpr_free_list);
3674 }
3675
3676 /* internal queue */
3677 INIT_LIST_HEAD(&ioc->internal_free_list);
3678 smid = ioc->internal_smid;
3679 for (i = 0; i < ioc->internal_depth; i++, smid++) {
3680 ioc->internal_lookup[i].cb_idx = 0xFF;
3681 ioc->internal_lookup[i].smid = smid;
3682 list_add_tail(&ioc->internal_lookup[i].tracker_list,
3683 &ioc->internal_free_list);
3684 }
Kashyap, Desai35f805b2010-11-13 04:34:06 +05303685
3686 /* chain pool */
3687 INIT_LIST_HEAD(&ioc->free_chain_list);
3688 for (i = 0; i < ioc->chain_depth; i++)
3689 list_add_tail(&ioc->chain_lookup[i].tracker_list,
3690 &ioc->free_chain_list);
3691
Eric Moore635374e2009-03-09 01:21:12 -06003692 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3693
3694 /* initialize Reply Free Queue */
3695 for (i = 0, reply_address = (u32)ioc->reply_dma ;
3696 i < ioc->reply_free_queue_depth ; i++, reply_address +=
3697 ioc->reply_sz)
3698 ioc->reply_free[i] = cpu_to_le32(reply_address);
3699
3700 /* initialize Reply Post Free Queue */
3701 for (i = 0; i < ioc->reply_post_queue_depth; i++)
Eric Moore03ea1112009-04-21 15:37:57 -06003702 ioc->reply_post_free[i].Words = ULLONG_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06003703
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303704 r = _base_send_ioc_init(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003705 if (r)
3706 return r;
3707
3708 /* initialize the index's */
3709 ioc->reply_free_host_index = ioc->reply_free_queue_depth - 1;
3710 ioc->reply_post_host_index = 0;
3711 writel(ioc->reply_free_host_index, &ioc->chip->ReplyFreeHostIndex);
3712 writel(0, &ioc->chip->ReplyPostHostIndex);
3713
3714 _base_unmask_interrupts(ioc);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303715 r = _base_event_notification(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003716 if (r)
3717 return r;
3718
3719 if (sleep_flag == CAN_SLEEP)
3720 _base_static_config_pages(ioc);
3721
Kashyap, Desai570c67a2010-06-17 13:43:17 +05303722 if (ioc->wait_for_port_enable_to_complete) {
3723 if (diag_buffer_enable != 0)
3724 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
3725 if (disable_discovery > 0)
3726 return r;
3727 }
Kashyap, Desaidd5fd332010-06-17 13:31:17 +05303728
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303729 r = _base_send_port_enable(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06003730 if (r)
3731 return r;
3732
3733 return r;
3734}
3735
3736/**
3737 * mpt2sas_base_free_resources - free resources controller resources (io/irq/memap)
3738 * @ioc: per adapter object
3739 *
3740 * Return nothing.
3741 */
3742void
3743mpt2sas_base_free_resources(struct MPT2SAS_ADAPTER *ioc)
3744{
3745 struct pci_dev *pdev = ioc->pdev;
3746
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303747 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003748 __func__));
3749
3750 _base_mask_interrupts(ioc);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05303751 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06003752 _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Kashyap, Desai6558bbb2010-03-17 16:23:36 +05303753 ioc->shost_recovery = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003754 if (ioc->pci_irq) {
3755 synchronize_irq(pdev->irq);
3756 free_irq(ioc->pci_irq, ioc);
3757 }
3758 _base_disable_msix(ioc);
3759 if (ioc->chip_phys)
3760 iounmap(ioc->chip);
3761 ioc->pci_irq = -1;
3762 ioc->chip_phys = 0;
3763 pci_release_selected_regions(ioc->pdev, ioc->bars);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05303764 pci_disable_pcie_error_reporting(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06003765 pci_disable_device(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06003766 return;
3767}
3768
3769/**
3770 * mpt2sas_base_attach - attach controller instance
3771 * @ioc: per adapter object
3772 *
3773 * Returns 0 for success, non-zero for failure.
3774 */
3775int
3776mpt2sas_base_attach(struct MPT2SAS_ADAPTER *ioc)
3777{
3778 int r, i;
Eric Moore635374e2009-03-09 01:21:12 -06003779
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303780 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003781 __func__));
3782
3783 r = mpt2sas_base_map_resources(ioc);
3784 if (r)
3785 return r;
3786
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303787 pci_set_drvdata(ioc->pdev, ioc->shost);
Kashyap, Desai96b681c2009-09-23 17:32:06 +05303788 r = _base_get_ioc_facts(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003789 if (r)
3790 goto out_free_resources;
3791
Kashyap, Desai96b681c2009-09-23 17:32:06 +05303792 r = _base_make_ioc_ready(ioc, CAN_SLEEP, SOFT_RESET);
Eric Moore635374e2009-03-09 01:21:12 -06003793 if (r)
3794 goto out_free_resources;
3795
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303796 ioc->pfacts = kcalloc(ioc->facts.NumberOfPorts,
3797 sizeof(Mpi2PortFactsReply_t), GFP_KERNEL);
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303798 if (!ioc->pfacts) {
3799 r = -ENOMEM;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303800 goto out_free_resources;
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303801 }
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303802
3803 for (i = 0 ; i < ioc->facts.NumberOfPorts; i++) {
3804 r = _base_get_port_facts(ioc, i, CAN_SLEEP);
3805 if (r)
3806 goto out_free_resources;
3807 }
3808
Eric Moore635374e2009-03-09 01:21:12 -06003809 r = _base_allocate_memory_pools(ioc, CAN_SLEEP);
3810 if (r)
3811 goto out_free_resources;
3812
3813 init_waitqueue_head(&ioc->reset_wq);
3814
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303815 /* allocate memory pd handle bitmask list */
3816 ioc->pd_handles_sz = (ioc->facts.MaxDevHandle / 8);
3817 if (ioc->facts.MaxDevHandle % 8)
3818 ioc->pd_handles_sz++;
3819 ioc->pd_handles = kzalloc(ioc->pd_handles_sz,
3820 GFP_KERNEL);
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05303821 if (!ioc->pd_handles) {
3822 r = -ENOMEM;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303823 goto out_free_resources;
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05303824 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303825
Kashyap, Desaie75b9b62009-12-16 18:52:39 +05303826 ioc->fwfault_debug = mpt2sas_fwfault_debug;
3827
Eric Moore635374e2009-03-09 01:21:12 -06003828 /* base internal command bits */
3829 mutex_init(&ioc->base_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003830 ioc->base_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3831 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
3832
3833 /* transport internal command bits */
3834 ioc->transport_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3835 ioc->transport_cmds.status = MPT2_CMD_NOT_USED;
3836 mutex_init(&ioc->transport_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003837
Kashyap, Desaid685c262009-11-17 13:16:37 +05303838 /* scsih internal command bits */
3839 ioc->scsih_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3840 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
3841 mutex_init(&ioc->scsih_cmds.mutex);
3842
Eric Moore635374e2009-03-09 01:21:12 -06003843 /* task management internal command bits */
3844 ioc->tm_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3845 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
3846 mutex_init(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003847
3848 /* config page internal command bits */
3849 ioc->config_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
3850 ioc->config_cmds.status = MPT2_CMD_NOT_USED;
3851 mutex_init(&ioc->config_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003852
3853 /* ctl module internal command bits */
3854 ioc->ctl_cmds.reply = kzalloc(ioc->reply_sz, GFP_KERNEL);
Kashyap, Desai769578f2010-06-17 13:49:28 +05303855 ioc->ctl_cmds.sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06003856 ioc->ctl_cmds.status = MPT2_CMD_NOT_USED;
3857 mutex_init(&ioc->ctl_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06003858
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303859 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
3860 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
Kashyap, Desai769578f2010-06-17 13:49:28 +05303861 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply ||
3862 !ioc->ctl_cmds.sense) {
Kashyap, Desaif6aee7b2010-03-17 16:26:48 +05303863 r = -ENOMEM;
3864 goto out_free_resources;
3865 }
3866
Kashyap, Desai3e2e8332010-06-17 13:47:29 +05303867 if (!ioc->base_cmds.reply || !ioc->transport_cmds.reply ||
3868 !ioc->scsih_cmds.reply || !ioc->tm_cmds.reply ||
3869 !ioc->config_cmds.reply || !ioc->ctl_cmds.reply) {
3870 r = -ENOMEM;
3871 goto out_free_resources;
3872 }
3873
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05303874 init_completion(&ioc->shost_recovery_done);
3875
Eric Moore635374e2009-03-09 01:21:12 -06003876 for (i = 0; i < MPI2_EVENT_NOTIFY_EVENTMASK_WORDS; i++)
3877 ioc->event_masks[i] = -1;
3878
3879 /* here we enable the events we care about */
3880 _base_unmask_events(ioc, MPI2_EVENT_SAS_DISCOVERY);
3881 _base_unmask_events(ioc, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
3882 _base_unmask_events(ioc, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
3883 _base_unmask_events(ioc, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
3884 _base_unmask_events(ioc, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
3885 _base_unmask_events(ioc, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
3886 _base_unmask_events(ioc, MPI2_EVENT_IR_VOLUME);
3887 _base_unmask_events(ioc, MPI2_EVENT_IR_PHYSICAL_DISK);
3888 _base_unmask_events(ioc, MPI2_EVENT_IR_OPERATION_STATUS);
Eric Moore635374e2009-03-09 01:21:12 -06003889 _base_unmask_events(ioc, MPI2_EVENT_LOG_ENTRY_ADDED);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303890 r = _base_make_ioc_operational(ioc, CAN_SLEEP);
Eric Moore635374e2009-03-09 01:21:12 -06003891 if (r)
3892 goto out_free_resources;
3893
Kashyap, Desai6cb8ef52010-11-13 04:32:18 +05303894 if (missing_delay[0] != -1 && missing_delay[1] != -1)
3895 _base_update_missing_delay(ioc, missing_delay[0],
3896 missing_delay[1]);
3897
Kashyap, Desaie4750c92009-08-07 19:37:59 +05303898 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06003899 return 0;
3900
3901 out_free_resources:
3902
3903 ioc->remove_host = 1;
3904 mpt2sas_base_free_resources(ioc);
3905 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303906 pci_set_drvdata(ioc->pdev, NULL);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303907 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003908 kfree(ioc->tm_cmds.reply);
3909 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303910 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06003911 kfree(ioc->config_cmds.reply);
3912 kfree(ioc->base_cmds.reply);
3913 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05303914 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06003915 kfree(ioc->pfacts);
3916 ioc->ctl_cmds.reply = NULL;
3917 ioc->base_cmds.reply = NULL;
3918 ioc->tm_cmds.reply = NULL;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303919 ioc->scsih_cmds.reply = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06003920 ioc->transport_cmds.reply = NULL;
3921 ioc->config_cmds.reply = NULL;
3922 ioc->pfacts = NULL;
3923 return r;
3924}
3925
3926
3927/**
3928 * mpt2sas_base_detach - remove controller instance
3929 * @ioc: per adapter object
3930 *
3931 * Return nothing.
3932 */
3933void
3934mpt2sas_base_detach(struct MPT2SAS_ADAPTER *ioc)
3935{
Eric Moore635374e2009-03-09 01:21:12 -06003936
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303937 dexitprintk(ioc, printk(MPT2SAS_INFO_FMT "%s\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06003938 __func__));
3939
Kashyap, Desaie4750c92009-08-07 19:37:59 +05303940 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06003941 mpt2sas_base_free_resources(ioc);
3942 _base_release_memory_pools(ioc);
Kashyap, Desaifcfe6392009-08-07 19:38:48 +05303943 pci_set_drvdata(ioc->pdev, NULL);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303944 kfree(ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003945 kfree(ioc->pfacts);
3946 kfree(ioc->ctl_cmds.reply);
Kashyap, Desai769578f2010-06-17 13:49:28 +05303947 kfree(ioc->ctl_cmds.sense);
Eric Moore635374e2009-03-09 01:21:12 -06003948 kfree(ioc->base_cmds.reply);
3949 kfree(ioc->tm_cmds.reply);
3950 kfree(ioc->transport_cmds.reply);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05303951 kfree(ioc->scsih_cmds.reply);
Eric Moore635374e2009-03-09 01:21:12 -06003952 kfree(ioc->config_cmds.reply);
3953}
3954
3955/**
3956 * _base_reset_handler - reset callback handler (for base)
3957 * @ioc: per adapter object
3958 * @reset_phase: phase
3959 *
3960 * The handler for doing any required cleanup or initialization.
3961 *
3962 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
3963 * MPT2_IOC_DONE_RESET
3964 *
3965 * Return nothing.
3966 */
3967static void
3968_base_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
3969{
3970 switch (reset_phase) {
3971 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303972 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06003973 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
3974 break;
3975 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303976 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06003977 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
3978 if (ioc->transport_cmds.status & MPT2_CMD_PENDING) {
3979 ioc->transport_cmds.status |= MPT2_CMD_RESET;
3980 mpt2sas_base_free_smid(ioc, ioc->transport_cmds.smid);
3981 complete(&ioc->transport_cmds.done);
3982 }
3983 if (ioc->base_cmds.status & MPT2_CMD_PENDING) {
3984 ioc->base_cmds.status |= MPT2_CMD_RESET;
3985 mpt2sas_base_free_smid(ioc, ioc->base_cmds.smid);
3986 complete(&ioc->base_cmds.done);
3987 }
3988 if (ioc->config_cmds.status & MPT2_CMD_PENDING) {
3989 ioc->config_cmds.status |= MPT2_CMD_RESET;
3990 mpt2sas_base_free_smid(ioc, ioc->config_cmds.smid);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07003991 ioc->config_cmds.smid = USHRT_MAX;
Eric Moore635374e2009-03-09 01:21:12 -06003992 complete(&ioc->config_cmds.done);
3993 }
3994 break;
3995 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303996 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Eric Moore635374e2009-03-09 01:21:12 -06003997 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
3998 break;
3999 }
4000 mpt2sas_scsih_reset_handler(ioc, reset_phase);
4001 mpt2sas_ctl_reset_handler(ioc, reset_phase);
4002}
4003
4004/**
4005 * _wait_for_commands_to_complete - reset controller
4006 * @ioc: Pointer to MPT_ADAPTER structure
4007 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4008 *
4009 * This function waiting(3s) for all pending commands to complete
4010 * prior to putting controller in reset.
4011 */
4012static void
4013_wait_for_commands_to_complete(struct MPT2SAS_ADAPTER *ioc, int sleep_flag)
4014{
4015 u32 ioc_state;
4016 unsigned long flags;
4017 u16 i;
4018
4019 ioc->pending_io_count = 0;
4020 if (sleep_flag != CAN_SLEEP)
4021 return;
4022
4023 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
4024 if ((ioc_state & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL)
4025 return;
4026
4027 /* pending command count */
4028 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304029 for (i = 0; i < ioc->scsiio_depth; i++)
Eric Moore635374e2009-03-09 01:21:12 -06004030 if (ioc->scsi_lookup[i].cb_idx != 0xFF)
4031 ioc->pending_io_count++;
4032 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
4033
4034 if (!ioc->pending_io_count)
4035 return;
4036
4037 /* wait for pending commands to complete */
Kashyap, Desaid2742132010-06-17 13:28:55 +05304038 wait_event_timeout(ioc->reset_wq, ioc->pending_io_count == 0, 10 * HZ);
Eric Moore635374e2009-03-09 01:21:12 -06004039}
4040
4041/**
4042 * mpt2sas_base_hard_reset_handler - reset controller
4043 * @ioc: Pointer to MPT_ADAPTER structure
4044 * @sleep_flag: CAN_SLEEP or NO_SLEEP
4045 * @type: FORCE_BIG_HAMMER or SOFT_RESET
4046 *
4047 * Returns 0 for success, non-zero for failure.
4048 */
4049int
4050mpt2sas_base_hard_reset_handler(struct MPT2SAS_ADAPTER *ioc, int sleep_flag,
4051 enum reset_type type)
4052{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304053 int r;
Eric Moore635374e2009-03-09 01:21:12 -06004054 unsigned long flags;
4055
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304056 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06004057 __func__));
4058
Eric Moore3cb54692010-07-08 14:44:34 -06004059 if (ioc->pci_error_recovery) {
4060 printk(MPT2SAS_ERR_FMT "%s: pci error recovery reset\n",
4061 ioc->name, __func__);
4062 r = 0;
4063 goto out;
4064 }
4065
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05304066 if (mpt2sas_fwfault_debug)
4067 mpt2sas_halt_firmware(ioc);
4068
Kashyap, Desaid2742132010-06-17 13:28:55 +05304069 /* TODO - What we really should be doing is pulling
4070 * out all the code associated with NO_SLEEP; its never used.
4071 * That is legacy code from mpt fusion driver, ported over.
4072 * I will leave this BUG_ON here for now till its been resolved.
4073 */
4074 BUG_ON(sleep_flag == NO_SLEEP);
4075
4076 /* wait for an active reset in progress to complete */
4077 if (!mutex_trylock(&ioc->reset_in_progress_mutex)) {
4078 do {
4079 ssleep(1);
4080 } while (ioc->shost_recovery == 1);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304081 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304082 __func__));
4083 return ioc->ioc_reset_in_progress_status;
Eric Moore635374e2009-03-09 01:21:12 -06004084 }
Kashyap, Desaid2742132010-06-17 13:28:55 +05304085
4086 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06004087 ioc->shost_recovery = 1;
Eric Moore635374e2009-03-09 01:21:12 -06004088 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
4089
4090 _base_reset_handler(ioc, MPT2_IOC_PRE_RESET);
4091 _wait_for_commands_to_complete(ioc, sleep_flag);
4092 _base_mask_interrupts(ioc);
4093 r = _base_make_ioc_ready(ioc, sleep_flag, type);
4094 if (r)
4095 goto out;
4096 _base_reset_handler(ioc, MPT2_IOC_AFTER_RESET);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304097 r = _base_make_ioc_operational(ioc, sleep_flag);
Eric Moore635374e2009-03-09 01:21:12 -06004098 if (!r)
4099 _base_reset_handler(ioc, MPT2_IOC_DONE_RESET);
4100 out:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304101 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %s\n",
Eric Moore635374e2009-03-09 01:21:12 -06004102 ioc->name, __func__, ((r == 0) ? "SUCCESS" : "FAILED")));
4103
4104 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304105 ioc->ioc_reset_in_progress_status = r;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304106 ioc->shost_recovery = 0;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05304107 complete(&ioc->shost_recovery_done);
Eric Moore635374e2009-03-09 01:21:12 -06004108 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
Kashyap, Desaid2742132010-06-17 13:28:55 +05304109 mutex_unlock(&ioc->reset_in_progress_mutex);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05304110
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05304111 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit\n", ioc->name,
Kashyap, Desaid2742132010-06-17 13:28:55 +05304112 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06004113 return r;
4114}