]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/scsi/mpt3sas/mpt3sas_scsih.c
scsi: megaraid_sas: fix spelling mistake: "thershold" -> "threshold"
[mirror_ubuntu-focal-kernel.git] / drivers / scsi / mpt3sas / mpt3sas_scsih.c
CommitLineData
f92363d1
SR
1/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
a4ffce0d 5 * Copyright (C) 2012-2014 LSI Corporation
a03bd153
SR
6 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
f92363d1
SR
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
f92363d1
SR
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
ffdadd68 54#include <linux/pci-aspm.h>
f92363d1
SR
55#include <linux/interrupt.h>
56#include <linux/aer.h>
57#include <linux/raid_class.h>
7786ab6a 58#include <asm/unaligned.h>
f92363d1
SR
59
60#include "mpt3sas_base.h"
61
f92363d1
SR
62#define RAID_CHANNEL 1
63/* forward proto's */
64static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
65 struct _sas_node *sas_expander);
66static void _firmware_event_work(struct work_struct *work);
67
68static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
69 struct _sas_device *sas_device);
70static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
71 u8 retry_count, u8 is_pd);
72
73static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
74
f92363d1
SR
75/* global parameters */
76LIST_HEAD(mpt3sas_ioc_list);
08c4d550
SR
77/* global ioc lock for list operations */
78DEFINE_SPINLOCK(gioc_lock);
f92363d1 79
c84b06a4
SR
80MODULE_AUTHOR(MPT3SAS_AUTHOR);
81MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
82MODULE_LICENSE("GPL");
83MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
84MODULE_ALIAS("mpt2sas");
85
f92363d1
SR
86/* local parameters */
87static u8 scsi_io_cb_idx = -1;
88static u8 tm_cb_idx = -1;
89static u8 ctl_cb_idx = -1;
90static u8 base_cb_idx = -1;
91static u8 port_enable_cb_idx = -1;
92static u8 transport_cb_idx = -1;
93static u8 scsih_cb_idx = -1;
94static u8 config_cb_idx = -1;
c84b06a4
SR
95static int mpt2_ids;
96static int mpt3_ids;
f92363d1
SR
97
98static u8 tm_tr_cb_idx = -1 ;
99static u8 tm_tr_volume_cb_idx = -1 ;
100static u8 tm_sas_control_cb_idx = -1;
101
102/* command line options */
103static u32 logging_level;
104MODULE_PARM_DESC(logging_level,
105 " bits for enabling additional logging info (default=0)");
106
107
108static ushort max_sectors = 0xFFFF;
109module_param(max_sectors, ushort, 0);
110MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
111
112
113static int missing_delay[2] = {-1, -1};
114module_param_array(missing_delay, int, NULL, 0);
115MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
116
117/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
118#define MPT3SAS_MAX_LUN (16895)
1abf635d
HR
119static u64 max_lun = MPT3SAS_MAX_LUN;
120module_param(max_lun, ullong, 0);
f92363d1
SR
121MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
122
c84b06a4
SR
123static ushort hbas_to_enumerate;
124module_param(hbas_to_enumerate, ushort, 0);
125MODULE_PARM_DESC(hbas_to_enumerate,
126 " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
127 1 - enumerates only SAS 2.0 generation HBAs\n \
128 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
f92363d1
SR
129
130/* diag_buffer_enable is bitwise
131 * bit 0 set = TRACE
132 * bit 1 set = SNAPSHOT
133 * bit 2 set = EXTENDED
134 *
135 * Either bit can be set, or both
136 */
137static int diag_buffer_enable = -1;
138module_param(diag_buffer_enable, int, 0);
139MODULE_PARM_DESC(diag_buffer_enable,
140 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
141static int disable_discovery = -1;
142module_param(disable_discovery, int, 0);
143MODULE_PARM_DESC(disable_discovery, " disable discovery ");
144
145
146/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
147static int prot_mask = -1;
148module_param(prot_mask, int, 0);
149MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
150
151
152/* raid transport support */
7497392a 153struct raid_template *mpt3sas_raid_template;
c84b06a4 154struct raid_template *mpt2sas_raid_template;
f92363d1
SR
155
156
157/**
158 * struct sense_info - common structure for obtaining sense keys
159 * @skey: sense key
160 * @asc: additional sense code
161 * @ascq: additional sense code qualifier
162 */
163struct sense_info {
164 u8 skey;
165 u8 asc;
166 u8 ascq;
167};
168
169#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
0f624c39 170#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
f92363d1
SR
171#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
172#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
173#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
174/**
175 * struct fw_event_work - firmware event struct
176 * @list: link list framework
177 * @work: work object (ioc->fault_reset_work_q)
f92363d1
SR
178 * @ioc: per adapter object
179 * @device_handle: device handle
180 * @VF_ID: virtual function id
181 * @VP_ID: virtual port id
182 * @ignore: flag meaning this event has been marked to ignore
b8ac0cc7
JL
183 * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
184 * @refcount: kref for this event
f92363d1
SR
185 * @event_data: reply event data payload follows
186 *
187 * This object stored on ioc->fw_event_list.
188 */
189struct fw_event_work {
190 struct list_head list;
191 struct work_struct work;
f92363d1
SR
192
193 struct MPT3SAS_ADAPTER *ioc;
194 u16 device_handle;
195 u8 VF_ID;
196 u8 VP_ID;
197 u8 ignore;
198 u16 event;
146b16c8 199 struct kref refcount;
35b62362 200 char event_data[0] __aligned(4);
f92363d1
SR
201};
202
146b16c8
SR
203static void fw_event_work_free(struct kref *r)
204{
205 kfree(container_of(r, struct fw_event_work, refcount));
206}
207
208static void fw_event_work_get(struct fw_event_work *fw_work)
209{
210 kref_get(&fw_work->refcount);
211}
212
213static void fw_event_work_put(struct fw_event_work *fw_work)
214{
215 kref_put(&fw_work->refcount, fw_event_work_free);
216}
217
218static struct fw_event_work *alloc_fw_event_work(int len)
219{
220 struct fw_event_work *fw_event;
221
222 fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
223 if (!fw_event)
224 return NULL;
225
226 kref_init(&fw_event->refcount);
227 return fw_event;
228}
229
f92363d1
SR
230/**
231 * struct _scsi_io_transfer - scsi io transfer
232 * @handle: sas device handle (assigned by firmware)
233 * @is_raid: flag set for hidden raid components
234 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
235 * @data_length: data transfer length
236 * @data_dma: dma pointer to data
237 * @sense: sense data
238 * @lun: lun number
239 * @cdb_length: cdb length
240 * @cdb: cdb contents
241 * @timeout: timeout for this command
242 * @VF_ID: virtual function id
243 * @VP_ID: virtual port id
244 * @valid_reply: flag set for reply message
245 * @sense_length: sense length
246 * @ioc_status: ioc status
247 * @scsi_state: scsi state
248 * @scsi_status: scsi staus
249 * @log_info: log information
250 * @transfer_length: data length transfer when there is a reply message
251 *
252 * Used for sending internal scsi commands to devices within this module.
253 * Refer to _scsi_send_scsi_io().
254 */
255struct _scsi_io_transfer {
256 u16 handle;
257 u8 is_raid;
258 enum dma_data_direction dir;
259 u32 data_length;
260 dma_addr_t data_dma;
261 u8 sense[SCSI_SENSE_BUFFERSIZE];
262 u32 lun;
263 u8 cdb_length;
264 u8 cdb[32];
265 u8 timeout;
266 u8 VF_ID;
267 u8 VP_ID;
268 u8 valid_reply;
269 /* the following bits are only valid when 'valid_reply = 1' */
270 u32 sense_length;
271 u16 ioc_status;
272 u8 scsi_state;
273 u8 scsi_status;
274 u32 log_info;
275 u32 transfer_length;
276};
277
f92363d1
SR
278/**
279 * _scsih_set_debug_level - global setting of ioc->logging_level.
280 *
281 * Note: The logging levels are defined in mpt3sas_debug.h.
282 */
283static int
284_scsih_set_debug_level(const char *val, struct kernel_param *kp)
285{
286 int ret = param_set_int(val, kp);
287 struct MPT3SAS_ADAPTER *ioc;
288
289 if (ret)
290 return ret;
291
292 pr_info("setting logging_level(0x%08x)\n", logging_level);
08c4d550 293 spin_lock(&gioc_lock);
f92363d1
SR
294 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
295 ioc->logging_level = logging_level;
08c4d550 296 spin_unlock(&gioc_lock);
f92363d1
SR
297 return 0;
298}
299module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
300 &logging_level, 0644);
301
302/**
303 * _scsih_srch_boot_sas_address - search based on sas_address
304 * @sas_address: sas address
305 * @boot_device: boot device object from bios page 2
306 *
307 * Returns 1 when there's a match, 0 means no match.
308 */
309static inline int
310_scsih_srch_boot_sas_address(u64 sas_address,
311 Mpi2BootDeviceSasWwid_t *boot_device)
312{
313 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
314}
315
316/**
317 * _scsih_srch_boot_device_name - search based on device name
318 * @device_name: device name specified in INDENTIFY fram
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_device_name(u64 device_name,
325 Mpi2BootDeviceDeviceName_t *boot_device)
326{
327 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
328}
329
330/**
331 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
332 * @enclosure_logical_id: enclosure logical id
333 * @slot_number: slot number
334 * @boot_device: boot device object from bios page 2
335 *
336 * Returns 1 when there's a match, 0 means no match.
337 */
338static inline int
339_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
340 Mpi2BootDeviceEnclosureSlot_t *boot_device)
341{
342 return (enclosure_logical_id == le64_to_cpu(boot_device->
343 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
344 SlotNumber)) ? 1 : 0;
345}
346
347/**
348 * _scsih_is_boot_device - search for matching boot device.
349 * @sas_address: sas address
350 * @device_name: device name specified in INDENTIFY fram
351 * @enclosure_logical_id: enclosure logical id
352 * @slot_number: slot number
353 * @form: specifies boot device form
354 * @boot_device: boot device object from bios page 2
355 *
356 * Returns 1 when there's a match, 0 means no match.
357 */
358static int
359_scsih_is_boot_device(u64 sas_address, u64 device_name,
360 u64 enclosure_logical_id, u16 slot, u8 form,
361 Mpi2BiosPage2BootDevice_t *boot_device)
362{
363 int rc = 0;
364
365 switch (form) {
366 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
367 if (!sas_address)
368 break;
369 rc = _scsih_srch_boot_sas_address(
370 sas_address, &boot_device->SasWwid);
371 break;
372 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
373 if (!enclosure_logical_id)
374 break;
375 rc = _scsih_srch_boot_encl_slot(
376 enclosure_logical_id,
377 slot, &boot_device->EnclosureSlot);
378 break;
379 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
380 if (!device_name)
381 break;
382 rc = _scsih_srch_boot_device_name(
383 device_name, &boot_device->DeviceName);
384 break;
385 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
386 break;
387 }
388
389 return rc;
390}
391
392/**
393 * _scsih_get_sas_address - set the sas_address for given device handle
394 * @handle: device handle
395 * @sas_address: sas address
396 *
397 * Returns 0 success, non-zero when failure
398 */
399static int
400_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
401 u64 *sas_address)
402{
403 Mpi2SasDevicePage0_t sas_device_pg0;
404 Mpi2ConfigReply_t mpi_reply;
405 u32 ioc_status;
406
407 *sas_address = 0;
408
f92363d1
SR
409 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
410 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
411 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
412 __FILE__, __LINE__, __func__);
413 return -ENXIO;
414 }
415
416 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
417 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
758f8139
SR
418 /* For HBA, vSES doesn't return HBA SAS address. Instead return
419 * vSES's sas address.
420 */
421 if ((handle <= ioc->sas_hba.num_phys) &&
422 (!(le32_to_cpu(sas_device_pg0.DeviceInfo) &
423 MPI2_SAS_DEVICE_INFO_SEP)))
424 *sas_address = ioc->sas_hba.sas_address;
425 else
426 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
f92363d1
SR
427 return 0;
428 }
429
6c7abffc 430 /* we hit this because the given parent handle doesn't exist */
f92363d1
SR
431 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
432 return -ENXIO;
433
434 /* else error case */
435 pr_err(MPT3SAS_FMT
436 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
437 ioc->name, handle, ioc_status,
438 __FILE__, __LINE__, __func__);
439 return -EIO;
440}
441
442/**
443 * _scsih_determine_boot_device - determine boot device.
444 * @ioc: per adapter object
445 * @device: either sas_device or raid_device object
446 * @is_raid: [flag] 1 = raid object, 0 = sas object
447 *
448 * Determines whether this device should be first reported device to
449 * to scsi-ml or sas transport, this purpose is for persistent boot device.
450 * There are primary, alternate, and current entries in bios page 2. The order
451 * priority is primary, alternate, then current. This routine saves
452 * the corresponding device object and is_raid flag in the ioc object.
453 * The saved data to be used later in _scsih_probe_boot_devices().
454 */
455static void
456_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
457 void *device, u8 is_raid)
458{
459 struct _sas_device *sas_device;
460 struct _raid_device *raid_device;
461 u64 sas_address;
462 u64 device_name;
463 u64 enclosure_logical_id;
464 u16 slot;
465
466 /* only process this function when driver loads */
467 if (!ioc->is_driver_loading)
468 return;
469
470 /* no Bios, return immediately */
471 if (!ioc->bios_pg3.BiosVersion)
472 return;
473
474 if (!is_raid) {
475 sas_device = device;
476 sas_address = sas_device->sas_address;
477 device_name = sas_device->device_name;
478 enclosure_logical_id = sas_device->enclosure_logical_id;
479 slot = sas_device->slot;
480 } else {
481 raid_device = device;
482 sas_address = raid_device->wwid;
483 device_name = 0;
484 enclosure_logical_id = 0;
485 slot = 0;
486 }
487
488 if (!ioc->req_boot_device.device) {
489 if (_scsih_is_boot_device(sas_address, device_name,
490 enclosure_logical_id, slot,
491 (ioc->bios_pg2.ReqBootDeviceForm &
492 MPI2_BIOSPAGE2_FORM_MASK),
493 &ioc->bios_pg2.RequestedBootDevice)) {
494 dinitprintk(ioc, pr_info(MPT3SAS_FMT
495 "%s: req_boot_device(0x%016llx)\n",
496 ioc->name, __func__,
497 (unsigned long long)sas_address));
498 ioc->req_boot_device.device = device;
499 ioc->req_boot_device.is_raid = is_raid;
500 }
501 }
502
503 if (!ioc->req_alt_boot_device.device) {
504 if (_scsih_is_boot_device(sas_address, device_name,
505 enclosure_logical_id, slot,
506 (ioc->bios_pg2.ReqAltBootDeviceForm &
507 MPI2_BIOSPAGE2_FORM_MASK),
508 &ioc->bios_pg2.RequestedAltBootDevice)) {
509 dinitprintk(ioc, pr_info(MPT3SAS_FMT
510 "%s: req_alt_boot_device(0x%016llx)\n",
511 ioc->name, __func__,
512 (unsigned long long)sas_address));
513 ioc->req_alt_boot_device.device = device;
514 ioc->req_alt_boot_device.is_raid = is_raid;
515 }
516 }
517
518 if (!ioc->current_boot_device.device) {
519 if (_scsih_is_boot_device(sas_address, device_name,
520 enclosure_logical_id, slot,
521 (ioc->bios_pg2.CurrentBootDeviceForm &
522 MPI2_BIOSPAGE2_FORM_MASK),
523 &ioc->bios_pg2.CurrentBootDevice)) {
524 dinitprintk(ioc, pr_info(MPT3SAS_FMT
525 "%s: current_boot_device(0x%016llx)\n",
526 ioc->name, __func__,
527 (unsigned long long)sas_address));
528 ioc->current_boot_device.device = device;
529 ioc->current_boot_device.is_raid = is_raid;
530 }
531 }
532}
533
d1cb5e49
SR
534static struct _sas_device *
535__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
536 struct MPT3SAS_TARGET *tgt_priv)
537{
538 struct _sas_device *ret;
539
540 assert_spin_locked(&ioc->sas_device_lock);
541
542 ret = tgt_priv->sdev;
543 if (ret)
544 sas_device_get(ret);
545
546 return ret;
547}
548
549static struct _sas_device *
550mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
551 struct MPT3SAS_TARGET *tgt_priv)
552{
553 struct _sas_device *ret;
554 unsigned long flags;
555
556 spin_lock_irqsave(&ioc->sas_device_lock, flags);
557 ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
558 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
559
560 return ret;
561}
562
563
564struct _sas_device *
565__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
566 u64 sas_address)
567{
568 struct _sas_device *sas_device;
569
570 assert_spin_locked(&ioc->sas_device_lock);
571
572 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
573 if (sas_device->sas_address == sas_address)
574 goto found_device;
575
576 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
577 if (sas_device->sas_address == sas_address)
578 goto found_device;
579
580 return NULL;
581
582found_device:
583 sas_device_get(sas_device);
584 return sas_device;
585}
586
f92363d1 587/**
d1cb5e49 588 * mpt3sas_get_sdev_by_addr - sas device search
f92363d1
SR
589 * @ioc: per adapter object
590 * @sas_address: sas address
591 * Context: Calling function should acquire ioc->sas_device_lock
592 *
593 * This searches for sas_device based on sas_address, then return sas_device
594 * object.
595 */
596struct _sas_device *
d1cb5e49 597mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
f92363d1
SR
598 u64 sas_address)
599{
600 struct _sas_device *sas_device;
d1cb5e49
SR
601 unsigned long flags;
602
603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
604 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
605 sas_address);
606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
607
608 return sas_device;
609}
610
611static struct _sas_device *
612__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
613{
614 struct _sas_device *sas_device;
615
616 assert_spin_locked(&ioc->sas_device_lock);
f92363d1
SR
617
618 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
d1cb5e49
SR
619 if (sas_device->handle == handle)
620 goto found_device;
f92363d1
SR
621
622 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
d1cb5e49
SR
623 if (sas_device->handle == handle)
624 goto found_device;
f92363d1
SR
625
626 return NULL;
d1cb5e49
SR
627
628found_device:
629 sas_device_get(sas_device);
630 return sas_device;
f92363d1
SR
631}
632
633/**
d1cb5e49 634 * mpt3sas_get_sdev_by_handle - sas device search
f92363d1
SR
635 * @ioc: per adapter object
636 * @handle: sas device handle (assigned by firmware)
637 * Context: Calling function should acquire ioc->sas_device_lock
638 *
639 * This searches for sas_device based on sas_address, then return sas_device
640 * object.
641 */
642static struct _sas_device *
d1cb5e49 643mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
644{
645 struct _sas_device *sas_device;
d1cb5e49 646 unsigned long flags;
f92363d1 647
d1cb5e49
SR
648 spin_lock_irqsave(&ioc->sas_device_lock, flags);
649 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
650 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
f92363d1 651
d1cb5e49 652 return sas_device;
f92363d1
SR
653}
654
75888956
SR
655/**
656 * _scsih_display_enclosure_chassis_info - display device location info
657 * @ioc: per adapter object
658 * @sas_device: per sas device object
659 * @sdev: scsi device struct
660 * @starget: scsi target struct
661 *
662 * Returns nothing.
663 */
664static void
665_scsih_display_enclosure_chassis_info(struct MPT3SAS_ADAPTER *ioc,
666 struct _sas_device *sas_device, struct scsi_device *sdev,
667 struct scsi_target *starget)
668{
669 if (sdev) {
670 if (sas_device->enclosure_handle != 0)
671 sdev_printk(KERN_INFO, sdev,
672 "enclosure logical id (0x%016llx), slot(%d) \n",
673 (unsigned long long)
674 sas_device->enclosure_logical_id,
675 sas_device->slot);
676 if (sas_device->connector_name[0] != '\0')
677 sdev_printk(KERN_INFO, sdev,
678 "enclosure level(0x%04x), connector name( %s)\n",
679 sas_device->enclosure_level,
680 sas_device->connector_name);
681 if (sas_device->is_chassis_slot_valid)
682 sdev_printk(KERN_INFO, sdev, "chassis slot(0x%04x)\n",
683 sas_device->chassis_slot);
684 } else if (starget) {
685 if (sas_device->enclosure_handle != 0)
686 starget_printk(KERN_INFO, starget,
687 "enclosure logical id(0x%016llx), slot(%d) \n",
688 (unsigned long long)
689 sas_device->enclosure_logical_id,
690 sas_device->slot);
691 if (sas_device->connector_name[0] != '\0')
692 starget_printk(KERN_INFO, starget,
693 "enclosure level(0x%04x), connector name( %s)\n",
694 sas_device->enclosure_level,
695 sas_device->connector_name);
696 if (sas_device->is_chassis_slot_valid)
697 starget_printk(KERN_INFO, starget,
698 "chassis slot(0x%04x)\n",
699 sas_device->chassis_slot);
700 } else {
701 if (sas_device->enclosure_handle != 0)
702 pr_info(MPT3SAS_FMT
703 "enclosure logical id(0x%016llx), slot(%d) \n",
704 ioc->name, (unsigned long long)
705 sas_device->enclosure_logical_id,
706 sas_device->slot);
707 if (sas_device->connector_name[0] != '\0')
708 pr_info(MPT3SAS_FMT
709 "enclosure level(0x%04x), connector name( %s)\n",
710 ioc->name, sas_device->enclosure_level,
711 sas_device->connector_name);
712 if (sas_device->is_chassis_slot_valid)
713 pr_info(MPT3SAS_FMT "chassis slot(0x%04x)\n",
714 ioc->name, sas_device->chassis_slot);
715 }
716}
717
f92363d1
SR
718/**
719 * _scsih_sas_device_remove - remove sas_device from list.
720 * @ioc: per adapter object
721 * @sas_device: the sas_device object
722 * Context: This function will acquire ioc->sas_device_lock.
723 *
d1cb5e49 724 * If sas_device is on the list, remove it and decrement its reference count.
f92363d1
SR
725 */
726static void
727_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
728 struct _sas_device *sas_device)
729{
730 unsigned long flags;
731
732 if (!sas_device)
733 return;
e6d45e3e
SR
734 pr_info(MPT3SAS_FMT
735 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
736 ioc->name, sas_device->handle,
737 (unsigned long long) sas_device->sas_address);
738
75888956 739 _scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
f92363d1 740
d1cb5e49
SR
741 /*
742 * The lock serializes access to the list, but we still need to verify
743 * that nobody removed the entry while we were waiting on the lock.
744 */
f92363d1 745 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
746 if (!list_empty(&sas_device->list)) {
747 list_del_init(&sas_device->list);
748 sas_device_put(sas_device);
749 }
f92363d1
SR
750 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
751}
752
753/**
754 * _scsih_device_remove_by_handle - removing device object by handle
755 * @ioc: per adapter object
756 * @handle: device handle
757 *
758 * Return nothing.
759 */
760static void
761_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
762{
763 struct _sas_device *sas_device;
764 unsigned long flags;
765
766 if (ioc->shost_recovery)
767 return;
768
769 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
770 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
771 if (sas_device) {
772 list_del_init(&sas_device->list);
773 sas_device_put(sas_device);
774 }
f92363d1 775 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
d1cb5e49 776 if (sas_device) {
f92363d1 777 _scsih_remove_device(ioc, sas_device);
d1cb5e49
SR
778 sas_device_put(sas_device);
779 }
f92363d1
SR
780}
781
782/**
783 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
784 * @ioc: per adapter object
785 * @sas_address: device sas_address
786 *
787 * Return nothing.
788 */
789void
790mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
791 u64 sas_address)
792{
793 struct _sas_device *sas_device;
794 unsigned long flags;
795
796 if (ioc->shost_recovery)
797 return;
798
799 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
800 sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
801 if (sas_device) {
802 list_del_init(&sas_device->list);
803 sas_device_put(sas_device);
804 }
f92363d1 805 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
d1cb5e49 806 if (sas_device) {
f92363d1 807 _scsih_remove_device(ioc, sas_device);
d1cb5e49
SR
808 sas_device_put(sas_device);
809 }
f92363d1
SR
810}
811
812/**
813 * _scsih_sas_device_add - insert sas_device to the list.
814 * @ioc: per adapter object
815 * @sas_device: the sas_device object
816 * Context: This function will acquire ioc->sas_device_lock.
817 *
818 * Adding new object to the ioc->sas_device_list.
819 */
820static void
821_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
822 struct _sas_device *sas_device)
823{
824 unsigned long flags;
825
826 dewtprintk(ioc, pr_info(MPT3SAS_FMT
827 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
828 ioc->name, __func__, sas_device->handle,
829 (unsigned long long)sas_device->sas_address));
830
75888956
SR
831 dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
832 NULL, NULL));
e6d45e3e 833
f92363d1 834 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 835 sas_device_get(sas_device);
f92363d1
SR
836 list_add_tail(&sas_device->list, &ioc->sas_device_list);
837 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
838
c696f7b8
SPS
839 if (ioc->hide_drives) {
840 clear_bit(sas_device->handle, ioc->pend_os_device_add);
841 return;
842 }
843
f92363d1
SR
844 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
845 sas_device->sas_address_parent)) {
846 _scsih_sas_device_remove(ioc, sas_device);
847 } else if (!sas_device->starget) {
848 /*
849 * When asyn scanning is enabled, its not possible to remove
850 * devices while scanning is turned on due to an oops in
851 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
852 */
f5edbe77 853 if (!ioc->is_driver_loading) {
f92363d1
SR
854 mpt3sas_transport_port_remove(ioc,
855 sas_device->sas_address,
856 sas_device->sas_address_parent);
f5edbe77
SR
857 _scsih_sas_device_remove(ioc, sas_device);
858 }
c696f7b8
SPS
859 } else
860 clear_bit(sas_device->handle, ioc->pend_os_device_add);
f92363d1
SR
861}
862
863/**
864 * _scsih_sas_device_init_add - insert sas_device to the list.
865 * @ioc: per adapter object
866 * @sas_device: the sas_device object
867 * Context: This function will acquire ioc->sas_device_lock.
868 *
869 * Adding new object at driver load time to the ioc->sas_device_init_list.
870 */
871static void
872_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
873 struct _sas_device *sas_device)
874{
875 unsigned long flags;
876
877 dewtprintk(ioc, pr_info(MPT3SAS_FMT
878 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
879 __func__, sas_device->handle,
880 (unsigned long long)sas_device->sas_address));
881
75888956
SR
882 dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
883 NULL, NULL));
e6d45e3e 884
f92363d1 885 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 886 sas_device_get(sas_device);
f92363d1
SR
887 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
888 _scsih_determine_boot_device(ioc, sas_device, 0);
889 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
890}
891
892/**
893 * _scsih_raid_device_find_by_id - raid device search
894 * @ioc: per adapter object
895 * @id: sas device target id
896 * @channel: sas device channel
897 * Context: Calling function should acquire ioc->raid_device_lock
898 *
899 * This searches for raid_device based on target id, then return raid_device
900 * object.
901 */
902static struct _raid_device *
903_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
904{
905 struct _raid_device *raid_device, *r;
906
907 r = NULL;
908 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
909 if (raid_device->id == id && raid_device->channel == channel) {
910 r = raid_device;
911 goto out;
912 }
913 }
914
915 out:
916 return r;
917}
918
919/**
c84b06a4 920 * mpt3sas_raid_device_find_by_handle - raid device search
f92363d1
SR
921 * @ioc: per adapter object
922 * @handle: sas device handle (assigned by firmware)
923 * Context: Calling function should acquire ioc->raid_device_lock
924 *
925 * This searches for raid_device based on handle, then return raid_device
926 * object.
927 */
c84b06a4
SR
928struct _raid_device *
929mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
930{
931 struct _raid_device *raid_device, *r;
932
933 r = NULL;
934 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
935 if (raid_device->handle != handle)
936 continue;
937 r = raid_device;
938 goto out;
939 }
940
941 out:
942 return r;
943}
944
945/**
946 * _scsih_raid_device_find_by_wwid - raid device search
947 * @ioc: per adapter object
948 * @handle: sas device handle (assigned by firmware)
949 * Context: Calling function should acquire ioc->raid_device_lock
950 *
951 * This searches for raid_device based on wwid, then return raid_device
952 * object.
953 */
954static struct _raid_device *
955_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
956{
957 struct _raid_device *raid_device, *r;
958
959 r = NULL;
960 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
961 if (raid_device->wwid != wwid)
962 continue;
963 r = raid_device;
964 goto out;
965 }
966
967 out:
968 return r;
969}
970
971/**
972 * _scsih_raid_device_add - add raid_device object
973 * @ioc: per adapter object
974 * @raid_device: raid_device object
975 *
976 * This is added to the raid_device_list link list.
977 */
978static void
979_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
980 struct _raid_device *raid_device)
981{
982 unsigned long flags;
983
984 dewtprintk(ioc, pr_info(MPT3SAS_FMT
985 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
986 raid_device->handle, (unsigned long long)raid_device->wwid));
987
988 spin_lock_irqsave(&ioc->raid_device_lock, flags);
989 list_add_tail(&raid_device->list, &ioc->raid_device_list);
990 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
991}
992
993/**
994 * _scsih_raid_device_remove - delete raid_device object
995 * @ioc: per adapter object
996 * @raid_device: raid_device object
997 *
998 */
999static void
1000_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
1001 struct _raid_device *raid_device)
1002{
1003 unsigned long flags;
1004
1005 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1006 list_del(&raid_device->list);
1007 kfree(raid_device);
1008 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1009}
1010
1011/**
1012 * mpt3sas_scsih_expander_find_by_handle - expander device search
1013 * @ioc: per adapter object
1014 * @handle: expander handle (assigned by firmware)
1015 * Context: Calling function should acquire ioc->sas_device_lock
1016 *
1017 * This searches for expander device based on handle, then returns the
1018 * sas_node object.
1019 */
1020struct _sas_node *
1021mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1022{
1023 struct _sas_node *sas_expander, *r;
1024
1025 r = NULL;
1026 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1027 if (sas_expander->handle != handle)
1028 continue;
1029 r = sas_expander;
1030 goto out;
1031 }
1032 out:
1033 return r;
1034}
1035
1036/**
1037 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
1038 * @ioc: per adapter object
1039 * @sas_address: sas address
1040 * Context: Calling function should acquire ioc->sas_node_lock.
1041 *
1042 * This searches for expander device based on sas_address, then returns the
1043 * sas_node object.
1044 */
1045struct _sas_node *
1046mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1047 u64 sas_address)
1048{
1049 struct _sas_node *sas_expander, *r;
1050
1051 r = NULL;
1052 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1053 if (sas_expander->sas_address != sas_address)
1054 continue;
1055 r = sas_expander;
1056 goto out;
1057 }
1058 out:
1059 return r;
1060}
1061
1062/**
1063 * _scsih_expander_node_add - insert expander device to the list.
1064 * @ioc: per adapter object
1065 * @sas_expander: the sas_device object
1066 * Context: This function will acquire ioc->sas_node_lock.
1067 *
1068 * Adding new object to the ioc->sas_expander_list.
1069 *
1070 * Return nothing.
1071 */
1072static void
1073_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1074 struct _sas_node *sas_expander)
1075{
1076 unsigned long flags;
1077
1078 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1079 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1080 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1081}
1082
1083/**
1084 * _scsih_is_end_device - determines if device is an end device
1085 * @device_info: bitfield providing information about the device.
1086 * Context: none
1087 *
1088 * Returns 1 if end device.
1089 */
1090static int
1091_scsih_is_end_device(u32 device_info)
1092{
1093 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1094 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1095 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1096 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1097 return 1;
1098 else
1099 return 0;
1100}
1101
1102/**
1103 * _scsih_scsi_lookup_get - returns scmd entry
1104 * @ioc: per adapter object
1105 * @smid: system request message index
1106 *
1107 * Returns the smid stored scmd pointer.
1108 */
1109static struct scsi_cmnd *
1110_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1111{
1112 return ioc->scsi_lookup[smid - 1].scmd;
1113}
1114
459325c4
C
1115/**
1116 * __scsih_scsi_lookup_get_clear - returns scmd entry without
1117 * holding any lock.
1118 * @ioc: per adapter object
1119 * @smid: system request message index
1120 *
1121 * Returns the smid stored scmd pointer.
1122 * Then will dereference the stored scmd pointer.
1123 */
1124static inline struct scsi_cmnd *
1125__scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc,
1126 u16 smid)
1127{
1128 struct scsi_cmnd *scmd = NULL;
1129
1130 swap(scmd, ioc->scsi_lookup[smid - 1].scmd);
1131
1132 return scmd;
1133}
1134
f92363d1
SR
1135/**
1136 * _scsih_scsi_lookup_get_clear - returns scmd entry
1137 * @ioc: per adapter object
1138 * @smid: system request message index
1139 *
1140 * Returns the smid stored scmd pointer.
1141 * Then will derefrence the stored scmd pointer.
1142 */
1143static inline struct scsi_cmnd *
1144_scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1145{
1146 unsigned long flags;
1147 struct scsi_cmnd *scmd;
1148
1149 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
459325c4 1150 scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
f92363d1
SR
1151 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1152
1153 return scmd;
1154}
1155
1156/**
1157 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1158 * @ioc: per adapter object
1159 * @smid: system request message index
1160 * @scmd: pointer to scsi command object
1161 * Context: This function will acquire ioc->scsi_lookup_lock.
1162 *
1163 * This will search for a scmd pointer in the scsi_lookup array,
1164 * returning the revelent smid. A returned value of zero means invalid.
1165 */
1166static u16
1167_scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1168 *scmd)
1169{
1170 u16 smid;
1171 unsigned long flags;
1172 int i;
1173
1174 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1175 smid = 0;
1176 for (i = 0; i < ioc->scsiio_depth; i++) {
1177 if (ioc->scsi_lookup[i].scmd == scmd) {
1178 smid = ioc->scsi_lookup[i].smid;
1179 goto out;
1180 }
1181 }
1182 out:
1183 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1184 return smid;
1185}
1186
1187/**
1188 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1189 * @ioc: per adapter object
1190 * @id: target id
1191 * @channel: channel
1192 * Context: This function will acquire ioc->scsi_lookup_lock.
1193 *
1194 * This will search for a matching channel:id in the scsi_lookup array,
1195 * returning 1 if found.
1196 */
1197static u8
1198_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1199 int channel)
1200{
1201 u8 found;
1202 unsigned long flags;
1203 int i;
1204
1205 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1206 found = 0;
1207 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1208 if (ioc->scsi_lookup[i].scmd &&
1209 (ioc->scsi_lookup[i].scmd->device->id == id &&
1210 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1211 found = 1;
1212 goto out;
1213 }
1214 }
1215 out:
1216 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1217 return found;
1218}
1219
1220/**
1221 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1222 * @ioc: per adapter object
1223 * @id: target id
1224 * @lun: lun number
1225 * @channel: channel
1226 * Context: This function will acquire ioc->scsi_lookup_lock.
1227 *
1228 * This will search for a matching channel:id:lun in the scsi_lookup array,
1229 * returning 1 if found.
1230 */
1231static u8
1232_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1233 unsigned int lun, int channel)
1234{
1235 u8 found;
1236 unsigned long flags;
1237 int i;
1238
1239 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1240 found = 0;
1241 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1242 if (ioc->scsi_lookup[i].scmd &&
1243 (ioc->scsi_lookup[i].scmd->device->id == id &&
1244 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1245 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1246 found = 1;
1247 goto out;
1248 }
1249 }
1250 out:
1251 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1252 return found;
1253}
1254
cf75d5d6 1255/**
8a7e4c24 1256 * scsih_change_queue_depth - setting device queue depth
cf75d5d6
CH
1257 * @sdev: scsi device struct
1258 * @qdepth: requested queue depth
1259 *
1260 * Returns queue depth.
1261 */
8bbb1cf6 1262static int
8a7e4c24 1263scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
f92363d1
SR
1264{
1265 struct Scsi_Host *shost = sdev->host;
1266 int max_depth;
1267 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1268 struct MPT3SAS_DEVICE *sas_device_priv_data;
1269 struct MPT3SAS_TARGET *sas_target_priv_data;
1270 struct _sas_device *sas_device;
1271 unsigned long flags;
1272
1273 max_depth = shost->can_queue;
1274
1275 /* limit max device queue for SATA to 32 */
1276 sas_device_priv_data = sdev->hostdata;
1277 if (!sas_device_priv_data)
1278 goto not_sata;
1279 sas_target_priv_data = sas_device_priv_data->sas_target;
1280 if (!sas_target_priv_data)
1281 goto not_sata;
1282 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1283 goto not_sata;
d1cb5e49 1284
f92363d1 1285 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
1286 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1287 if (sas_device) {
1288 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1289 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1290
1291 sas_device_put(sas_device);
1292 }
f92363d1
SR
1293 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1294
1295 not_sata:
1296
1297 if (!sdev->tagged_supported)
1298 max_depth = 1;
1299 if (qdepth > max_depth)
1300 qdepth = max_depth;
cf75d5d6 1301 return scsi_change_queue_depth(sdev, qdepth);
f92363d1
SR
1302}
1303
f92363d1 1304/**
8a7e4c24 1305 * scsih_target_alloc - target add routine
f92363d1
SR
1306 * @starget: scsi target struct
1307 *
1308 * Returns 0 if ok. Any other return is assumed to be an error and
1309 * the device is ignored.
1310 */
8bbb1cf6 1311static int
8a7e4c24 1312scsih_target_alloc(struct scsi_target *starget)
f92363d1
SR
1313{
1314 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1315 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1316 struct MPT3SAS_TARGET *sas_target_priv_data;
1317 struct _sas_device *sas_device;
1318 struct _raid_device *raid_device;
1319 unsigned long flags;
1320 struct sas_rphy *rphy;
1321
62c4da44
JL
1322 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1323 GFP_KERNEL);
f92363d1
SR
1324 if (!sas_target_priv_data)
1325 return -ENOMEM;
1326
1327 starget->hostdata = sas_target_priv_data;
1328 sas_target_priv_data->starget = starget;
1329 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1330
1331 /* RAID volumes */
1332 if (starget->channel == RAID_CHANNEL) {
1333 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1334 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1335 starget->channel);
1336 if (raid_device) {
1337 sas_target_priv_data->handle = raid_device->handle;
1338 sas_target_priv_data->sas_address = raid_device->wwid;
1339 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
7786ab6a 1340 if (ioc->is_warpdrive)
6d3a56ed
SR
1341 sas_target_priv_data->raid_device = raid_device;
1342 raid_device->starget = starget;
f92363d1
SR
1343 }
1344 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1345 return 0;
1346 }
1347
1348 /* sas/sata devices */
1349 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1350 rphy = dev_to_rphy(starget->dev.parent);
d1cb5e49 1351 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
1352 rphy->identify.sas_address);
1353
1354 if (sas_device) {
1355 sas_target_priv_data->handle = sas_device->handle;
1356 sas_target_priv_data->sas_address = sas_device->sas_address;
d1cb5e49 1357 sas_target_priv_data->sdev = sas_device;
f92363d1
SR
1358 sas_device->starget = starget;
1359 sas_device->id = starget->id;
1360 sas_device->channel = starget->channel;
1361 if (test_bit(sas_device->handle, ioc->pd_handles))
1362 sas_target_priv_data->flags |=
1363 MPT_TARGET_FLAGS_RAID_COMPONENT;
1364 if (sas_device->fast_path)
1365 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1366 }
1367 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1368
1369 return 0;
1370}
1371
1372/**
8a7e4c24 1373 * scsih_target_destroy - target destroy routine
f92363d1
SR
1374 * @starget: scsi target struct
1375 *
1376 * Returns nothing.
1377 */
8bbb1cf6 1378static void
8a7e4c24 1379scsih_target_destroy(struct scsi_target *starget)
f92363d1
SR
1380{
1381 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1382 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1383 struct MPT3SAS_TARGET *sas_target_priv_data;
1384 struct _sas_device *sas_device;
1385 struct _raid_device *raid_device;
1386 unsigned long flags;
f92363d1
SR
1387
1388 sas_target_priv_data = starget->hostdata;
1389 if (!sas_target_priv_data)
1390 return;
1391
1392 if (starget->channel == RAID_CHANNEL) {
1393 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1394 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1395 starget->channel);
1396 if (raid_device) {
1397 raid_device->starget = NULL;
1398 raid_device->sdev = NULL;
1399 }
1400 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1401 goto out;
1402 }
1403
1404 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 1405 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
f92363d1
SR
1406 if (sas_device && (sas_device->starget == starget) &&
1407 (sas_device->id == starget->id) &&
1408 (sas_device->channel == starget->channel))
1409 sas_device->starget = NULL;
1410
d1cb5e49
SR
1411 if (sas_device) {
1412 /*
1413 * Corresponding get() is in _scsih_target_alloc()
1414 */
1415 sas_target_priv_data->sdev = NULL;
1416 sas_device_put(sas_device);
1417
1418 sas_device_put(sas_device);
1419 }
f92363d1
SR
1420 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1421
1422 out:
1423 kfree(sas_target_priv_data);
1424 starget->hostdata = NULL;
1425}
1426
1427/**
8a7e4c24 1428 * scsih_slave_alloc - device add routine
f92363d1
SR
1429 * @sdev: scsi device struct
1430 *
1431 * Returns 0 if ok. Any other return is assumed to be an error and
1432 * the device is ignored.
1433 */
8bbb1cf6 1434static int
8a7e4c24 1435scsih_slave_alloc(struct scsi_device *sdev)
f92363d1
SR
1436{
1437 struct Scsi_Host *shost;
1438 struct MPT3SAS_ADAPTER *ioc;
1439 struct MPT3SAS_TARGET *sas_target_priv_data;
1440 struct MPT3SAS_DEVICE *sas_device_priv_data;
1441 struct scsi_target *starget;
1442 struct _raid_device *raid_device;
b65cfedf 1443 struct _sas_device *sas_device;
f92363d1
SR
1444 unsigned long flags;
1445
62c4da44
JL
1446 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1447 GFP_KERNEL);
f92363d1
SR
1448 if (!sas_device_priv_data)
1449 return -ENOMEM;
1450
1451 sas_device_priv_data->lun = sdev->lun;
1452 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1453
1454 starget = scsi_target(sdev);
1455 sas_target_priv_data = starget->hostdata;
1456 sas_target_priv_data->num_luns++;
1457 sas_device_priv_data->sas_target = sas_target_priv_data;
1458 sdev->hostdata = sas_device_priv_data;
1459 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1460 sdev->no_uld_attach = 1;
1461
1462 shost = dev_to_shost(&starget->dev);
1463 ioc = shost_priv(shost);
1464 if (starget->channel == RAID_CHANNEL) {
1465 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1466 raid_device = _scsih_raid_device_find_by_id(ioc,
1467 starget->id, starget->channel);
1468 if (raid_device)
1469 raid_device->sdev = sdev; /* raid is single lun */
1470 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1471 }
1472
b65cfedf
SR
1473 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1474 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 1475 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
b65cfedf
SR
1476 sas_target_priv_data->sas_address);
1477 if (sas_device && (sas_device->starget == NULL)) {
1478 sdev_printk(KERN_INFO, sdev,
1479 "%s : sas_device->starget set to starget @ %d\n",
d1cb5e49 1480 __func__, __LINE__);
b65cfedf
SR
1481 sas_device->starget = starget;
1482 }
d1cb5e49
SR
1483
1484 if (sas_device)
1485 sas_device_put(sas_device);
1486
b65cfedf
SR
1487 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1488 }
1489
f92363d1
SR
1490 return 0;
1491}
1492
1493/**
8a7e4c24 1494 * scsih_slave_destroy - device destroy routine
f92363d1
SR
1495 * @sdev: scsi device struct
1496 *
1497 * Returns nothing.
1498 */
8bbb1cf6 1499static void
8a7e4c24 1500scsih_slave_destroy(struct scsi_device *sdev)
f92363d1
SR
1501{
1502 struct MPT3SAS_TARGET *sas_target_priv_data;
1503 struct scsi_target *starget;
1504 struct Scsi_Host *shost;
1505 struct MPT3SAS_ADAPTER *ioc;
1506 struct _sas_device *sas_device;
1507 unsigned long flags;
1508
1509 if (!sdev->hostdata)
1510 return;
1511
1512 starget = scsi_target(sdev);
1513 sas_target_priv_data = starget->hostdata;
1514 sas_target_priv_data->num_luns--;
1515
1516 shost = dev_to_shost(&starget->dev);
1517 ioc = shost_priv(shost);
1518
1519 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1520 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
1521 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1522 sas_target_priv_data);
f92363d1
SR
1523 if (sas_device && !sas_target_priv_data->num_luns)
1524 sas_device->starget = NULL;
d1cb5e49
SR
1525
1526 if (sas_device)
1527 sas_device_put(sas_device);
f92363d1
SR
1528 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1529 }
1530
1531 kfree(sdev->hostdata);
1532 sdev->hostdata = NULL;
1533}
1534
1535/**
1536 * _scsih_display_sata_capabilities - sata capabilities
1537 * @ioc: per adapter object
1538 * @handle: device handle
1539 * @sdev: scsi device struct
1540 */
1541static void
1542_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1543 u16 handle, struct scsi_device *sdev)
1544{
1545 Mpi2ConfigReply_t mpi_reply;
1546 Mpi2SasDevicePage0_t sas_device_pg0;
1547 u32 ioc_status;
1548 u16 flags;
1549 u32 device_info;
1550
1551 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1552 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1553 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1554 ioc->name, __FILE__, __LINE__, __func__);
1555 return;
1556 }
1557
1558 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1559 MPI2_IOCSTATUS_MASK;
1560 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1561 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1562 ioc->name, __FILE__, __LINE__, __func__);
1563 return;
1564 }
1565
1566 flags = le16_to_cpu(sas_device_pg0.Flags);
1567 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1568
1569 sdev_printk(KERN_INFO, sdev,
1570 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1571 "sw_preserve(%s)\n",
1572 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1573 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1574 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1575 "n",
1576 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1577 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1578 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1579}
1580
1581/*
1582 * raid transport support -
1583 * Enabled for SLES11 and newer, in older kernels the driver will panic when
6c7abffc 1584 * unloading the driver followed by a load - I believe that the subroutine
f92363d1
SR
1585 * raid_class_release() is not cleaning up properly.
1586 */
1587
1588/**
8a7e4c24 1589 * scsih_is_raid - return boolean indicating device is raid volume
f92363d1
SR
1590 * @dev the device struct object
1591 */
8bbb1cf6 1592static int
8a7e4c24 1593scsih_is_raid(struct device *dev)
f92363d1
SR
1594{
1595 struct scsi_device *sdev = to_scsi_device(dev);
7786ab6a 1596 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
f92363d1 1597
7786ab6a
SR
1598 if (ioc->is_warpdrive)
1599 return 0;
f92363d1
SR
1600 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1601}
1602
1603/**
8a7e4c24 1604 * scsih_get_resync - get raid volume resync percent complete
f92363d1
SR
1605 * @dev the device struct object
1606 */
8bbb1cf6 1607static void
8a7e4c24 1608scsih_get_resync(struct device *dev)
f92363d1
SR
1609{
1610 struct scsi_device *sdev = to_scsi_device(dev);
1611 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1612 static struct _raid_device *raid_device;
1613 unsigned long flags;
1614 Mpi2RaidVolPage0_t vol_pg0;
1615 Mpi2ConfigReply_t mpi_reply;
1616 u32 volume_status_flags;
1617 u8 percent_complete;
1618 u16 handle;
1619
1620 percent_complete = 0;
1621 handle = 0;
7786ab6a
SR
1622 if (ioc->is_warpdrive)
1623 goto out;
1624
f92363d1
SR
1625 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1626 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1627 sdev->channel);
1628 if (raid_device) {
1629 handle = raid_device->handle;
1630 percent_complete = raid_device->percent_complete;
1631 }
1632 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1633
1634 if (!handle)
1635 goto out;
1636
1637 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1638 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1639 sizeof(Mpi2RaidVolPage0_t))) {
1640 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1641 ioc->name, __FILE__, __LINE__, __func__);
1642 percent_complete = 0;
1643 goto out;
1644 }
1645
1646 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1647 if (!(volume_status_flags &
1648 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1649 percent_complete = 0;
1650
1651 out:
b130b0d5
SS
1652
1653 switch (ioc->hba_mpi_version_belonged) {
1654 case MPI2_VERSION:
c84b06a4 1655 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
b130b0d5
SS
1656 break;
1657 case MPI25_VERSION:
1658 case MPI26_VERSION:
c84b06a4 1659 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
b130b0d5
SS
1660 break;
1661 }
f92363d1
SR
1662}
1663
1664/**
8a7e4c24 1665 * scsih_get_state - get raid volume level
f92363d1
SR
1666 * @dev the device struct object
1667 */
8bbb1cf6 1668static void
8a7e4c24 1669scsih_get_state(struct device *dev)
f92363d1
SR
1670{
1671 struct scsi_device *sdev = to_scsi_device(dev);
1672 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1673 static struct _raid_device *raid_device;
1674 unsigned long flags;
1675 Mpi2RaidVolPage0_t vol_pg0;
1676 Mpi2ConfigReply_t mpi_reply;
1677 u32 volstate;
1678 enum raid_state state = RAID_STATE_UNKNOWN;
1679 u16 handle = 0;
1680
1681 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1682 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1683 sdev->channel);
1684 if (raid_device)
1685 handle = raid_device->handle;
1686 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1687
1688 if (!raid_device)
1689 goto out;
1690
1691 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1692 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1693 sizeof(Mpi2RaidVolPage0_t))) {
1694 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1695 ioc->name, __FILE__, __LINE__, __func__);
1696 goto out;
1697 }
1698
1699 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1700 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1701 state = RAID_STATE_RESYNCING;
1702 goto out;
1703 }
1704
1705 switch (vol_pg0.VolumeState) {
1706 case MPI2_RAID_VOL_STATE_OPTIMAL:
1707 case MPI2_RAID_VOL_STATE_ONLINE:
1708 state = RAID_STATE_ACTIVE;
1709 break;
1710 case MPI2_RAID_VOL_STATE_DEGRADED:
1711 state = RAID_STATE_DEGRADED;
1712 break;
1713 case MPI2_RAID_VOL_STATE_FAILED:
1714 case MPI2_RAID_VOL_STATE_MISSING:
1715 state = RAID_STATE_OFFLINE;
1716 break;
1717 }
1718 out:
b130b0d5
SS
1719 switch (ioc->hba_mpi_version_belonged) {
1720 case MPI2_VERSION:
c84b06a4 1721 raid_set_state(mpt2sas_raid_template, dev, state);
b130b0d5
SS
1722 break;
1723 case MPI25_VERSION:
1724 case MPI26_VERSION:
c84b06a4 1725 raid_set_state(mpt3sas_raid_template, dev, state);
b130b0d5
SS
1726 break;
1727 }
f92363d1
SR
1728}
1729
1730/**
1731 * _scsih_set_level - set raid level
1732 * @sdev: scsi device struct
1733 * @volume_type: volume type
1734 */
1735static void
c84b06a4
SR
1736_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1737 struct scsi_device *sdev, u8 volume_type)
f92363d1
SR
1738{
1739 enum raid_level level = RAID_LEVEL_UNKNOWN;
1740
1741 switch (volume_type) {
1742 case MPI2_RAID_VOL_TYPE_RAID0:
1743 level = RAID_LEVEL_0;
1744 break;
1745 case MPI2_RAID_VOL_TYPE_RAID10:
1746 level = RAID_LEVEL_10;
1747 break;
1748 case MPI2_RAID_VOL_TYPE_RAID1E:
1749 level = RAID_LEVEL_1E;
1750 break;
1751 case MPI2_RAID_VOL_TYPE_RAID1:
1752 level = RAID_LEVEL_1;
1753 break;
1754 }
1755
b130b0d5
SS
1756 switch (ioc->hba_mpi_version_belonged) {
1757 case MPI2_VERSION:
c84b06a4 1758 raid_set_level(mpt2sas_raid_template,
b130b0d5
SS
1759 &sdev->sdev_gendev, level);
1760 break;
1761 case MPI25_VERSION:
1762 case MPI26_VERSION:
c84b06a4 1763 raid_set_level(mpt3sas_raid_template,
b130b0d5
SS
1764 &sdev->sdev_gendev, level);
1765 break;
1766 }
f92363d1
SR
1767}
1768
1769
1770/**
1771 * _scsih_get_volume_capabilities - volume capabilities
1772 * @ioc: per adapter object
1773 * @sas_device: the raid_device object
1774 *
1775 * Returns 0 for success, else 1
1776 */
1777static int
1778_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1779 struct _raid_device *raid_device)
1780{
1781 Mpi2RaidVolPage0_t *vol_pg0;
1782 Mpi2RaidPhysDiskPage0_t pd_pg0;
1783 Mpi2SasDevicePage0_t sas_device_pg0;
1784 Mpi2ConfigReply_t mpi_reply;
1785 u16 sz;
1786 u8 num_pds;
1787
1788 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1789 &num_pds)) || !num_pds) {
1790 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1791 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1792 __func__));
1793 return 1;
1794 }
1795
1796 raid_device->num_pds = num_pds;
1797 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1798 sizeof(Mpi2RaidVol0PhysDisk_t));
1799 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1800 if (!vol_pg0) {
1801 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1802 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1803 __func__));
1804 return 1;
1805 }
1806
1807 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1808 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1809 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1810 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1811 __func__));
1812 kfree(vol_pg0);
1813 return 1;
1814 }
1815
1816 raid_device->volume_type = vol_pg0->VolumeType;
1817
1818 /* figure out what the underlying devices are by
1819 * obtaining the device_info bits for the 1st device
1820 */
1821 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1822 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1823 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1824 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1825 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1826 le16_to_cpu(pd_pg0.DevHandle)))) {
1827 raid_device->device_info =
1828 le32_to_cpu(sas_device_pg0.DeviceInfo);
1829 }
1830 }
1831
1832 kfree(vol_pg0);
1833 return 0;
1834}
1835
f92363d1
SR
1836/**
1837 * _scsih_enable_tlr - setting TLR flags
1838 * @ioc: per adapter object
1839 * @sdev: scsi device struct
1840 *
1841 * Enabling Transaction Layer Retries for tape devices when
1842 * vpd page 0x90 is present
1843 *
1844 */
1845static void
1846_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1847{
1848
1849 /* only for TAPE */
1850 if (sdev->type != TYPE_TAPE)
1851 return;
1852
1853 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1854 return;
1855
1856 sas_enable_tlr(sdev);
1857 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1858 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1859 return;
1860
1861}
1862
1863/**
8a7e4c24 1864 * scsih_slave_configure - device configure routine.
f92363d1
SR
1865 * @sdev: scsi device struct
1866 *
1867 * Returns 0 if ok. Any other return is assumed to be an error and
1868 * the device is ignored.
1869 */
8bbb1cf6 1870static int
8a7e4c24 1871scsih_slave_configure(struct scsi_device *sdev)
f92363d1
SR
1872{
1873 struct Scsi_Host *shost = sdev->host;
1874 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1875 struct MPT3SAS_DEVICE *sas_device_priv_data;
1876 struct MPT3SAS_TARGET *sas_target_priv_data;
1877 struct _sas_device *sas_device;
1878 struct _raid_device *raid_device;
1879 unsigned long flags;
1880 int qdepth;
1881 u8 ssp_target = 0;
1882 char *ds = "";
1883 char *r_level = "";
1884 u16 handle, volume_handle = 0;
1885 u64 volume_wwid = 0;
1886
1887 qdepth = 1;
1888 sas_device_priv_data = sdev->hostdata;
1889 sas_device_priv_data->configured_lun = 1;
1890 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1891 sas_target_priv_data = sas_device_priv_data->sas_target;
1892 handle = sas_target_priv_data->handle;
1893
1894 /* raid volume handling */
1895 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1896
1897 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 1898 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
1899 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1900 if (!raid_device) {
1901 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1902 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1903 __LINE__, __func__));
1904 return 1;
1905 }
1906
1907 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1908 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1909 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1910 __LINE__, __func__));
1911 return 1;
1912 }
1913
7786ab6a
SR
1914 /*
1915 * WARPDRIVE: Initialize the required data for Direct IO
1916 */
c84b06a4 1917 mpt3sas_init_warpdrive_properties(ioc, raid_device);
f92363d1
SR
1918
1919 /* RAID Queue Depth Support
1920 * IS volume = underlying qdepth of drive type, either
1921 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1922 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1923 */
1924 if (raid_device->device_info &
1925 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1926 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1927 ds = "SSP";
1928 } else {
1929 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1930 if (raid_device->device_info &
1931 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1932 ds = "SATA";
1933 else
1934 ds = "STP";
1935 }
1936
1937 switch (raid_device->volume_type) {
1938 case MPI2_RAID_VOL_TYPE_RAID0:
1939 r_level = "RAID0";
1940 break;
1941 case MPI2_RAID_VOL_TYPE_RAID1E:
1942 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1943 if (ioc->manu_pg10.OEMIdentifier &&
1944 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1945 MFG10_GF0_R10_DISPLAY) &&
1946 !(raid_device->num_pds % 2))
1947 r_level = "RAID10";
1948 else
1949 r_level = "RAID1E";
1950 break;
1951 case MPI2_RAID_VOL_TYPE_RAID1:
1952 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1953 r_level = "RAID1";
1954 break;
1955 case MPI2_RAID_VOL_TYPE_RAID10:
1956 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1957 r_level = "RAID10";
1958 break;
1959 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1960 default:
1961 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1962 r_level = "RAIDX";
1963 break;
1964 }
1965
7786ab6a
SR
1966 if (!ioc->hide_ir_msg)
1967 sdev_printk(KERN_INFO, sdev,
1968 "%s: handle(0x%04x), wwid(0x%016llx),"
1969 " pd_count(%d), type(%s)\n",
1970 r_level, raid_device->handle,
1971 (unsigned long long)raid_device->wwid,
1972 raid_device->num_pds, ds);
f92363d1 1973
6c197093
C
1974 if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
1975 blk_queue_max_hw_sectors(sdev->request_queue,
1976 MPT3SAS_RAID_MAX_SECTORS);
1977 sdev_printk(KERN_INFO, sdev,
1978 "Set queue's max_sector to: %u\n",
1979 MPT3SAS_RAID_MAX_SECTORS);
1980 }
1981
8a7e4c24 1982 scsih_change_queue_depth(sdev, qdepth);
f92363d1 1983
7786ab6a
SR
1984 /* raid transport support */
1985 if (!ioc->is_warpdrive)
c84b06a4 1986 _scsih_set_level(ioc, sdev, raid_device->volume_type);
f92363d1
SR
1987 return 0;
1988 }
1989
1990 /* non-raid handling */
1991 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1992 if (mpt3sas_config_get_volume_handle(ioc, handle,
1993 &volume_handle)) {
1994 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1995 "failure at %s:%d/%s()!\n", ioc->name,
1996 __FILE__, __LINE__, __func__));
1997 return 1;
1998 }
1999 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
2000 volume_handle, &volume_wwid)) {
2001 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
2002 "failure at %s:%d/%s()!\n", ioc->name,
2003 __FILE__, __LINE__, __func__));
2004 return 1;
2005 }
2006 }
2007
2008 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 2009 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
2010 sas_device_priv_data->sas_target->sas_address);
2011 if (!sas_device) {
2012 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2013 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
2014 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2015 __func__));
2016 return 1;
2017 }
2018
2019 sas_device->volume_handle = volume_handle;
2020 sas_device->volume_wwid = volume_wwid;
2021 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2022 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
2023 ssp_target = 1;
30158dc9
SS
2024 if (sas_device->device_info &
2025 MPI2_SAS_DEVICE_INFO_SEP) {
2026 sdev_printk(KERN_WARNING, sdev,
2027 "set ignore_delay_remove for handle(0x%04x)\n",
2028 sas_device_priv_data->sas_target->handle);
2029 sas_device_priv_data->ignore_delay_remove = 1;
2030 ds = "SES";
2031 } else
2032 ds = "SSP";
f92363d1
SR
2033 } else {
2034 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
2035 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2036 ds = "STP";
2037 else if (sas_device->device_info &
2038 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2039 ds = "SATA";
2040 }
2041
2042 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
2043 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2044 ds, handle, (unsigned long long)sas_device->sas_address,
2045 sas_device->phy, (unsigned long long)sas_device->device_name);
75888956
SR
2046
2047 _scsih_display_enclosure_chassis_info(NULL, sas_device, sdev, NULL);
f92363d1 2048
d1cb5e49 2049 sas_device_put(sas_device);
f92363d1
SR
2050 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2051
2052 if (!ssp_target)
2053 _scsih_display_sata_capabilities(ioc, handle, sdev);
2054
2055
8a7e4c24 2056 scsih_change_queue_depth(sdev, qdepth);
f92363d1
SR
2057
2058 if (ssp_target) {
2059 sas_read_port_mode_page(sdev);
2060 _scsih_enable_tlr(ioc, sdev);
2061 }
2062
2063 return 0;
2064}
2065
2066/**
8a7e4c24 2067 * scsih_bios_param - fetch head, sector, cylinder info for a disk
f92363d1
SR
2068 * @sdev: scsi device struct
2069 * @bdev: pointer to block device context
2070 * @capacity: device size (in 512 byte sectors)
2071 * @params: three element array to place output:
2072 * params[0] number of heads (max 255)
2073 * params[1] number of sectors (max 63)
2074 * params[2] number of cylinders
2075 *
2076 * Return nothing.
2077 */
8bbb1cf6 2078static int
8a7e4c24 2079scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
f92363d1
SR
2080 sector_t capacity, int params[])
2081{
2082 int heads;
2083 int sectors;
2084 sector_t cylinders;
2085 ulong dummy;
2086
2087 heads = 64;
2088 sectors = 32;
2089
2090 dummy = heads * sectors;
2091 cylinders = capacity;
2092 sector_div(cylinders, dummy);
2093
2094 /*
2095 * Handle extended translation size for logical drives
2096 * > 1Gb
2097 */
2098 if ((ulong)capacity >= 0x200000) {
2099 heads = 255;
2100 sectors = 63;
2101 dummy = heads * sectors;
2102 cylinders = capacity;
2103 sector_div(cylinders, dummy);
2104 }
2105
2106 /* return result */
2107 params[0] = heads;
2108 params[1] = sectors;
2109 params[2] = cylinders;
2110
2111 return 0;
2112}
2113
2114/**
2115 * _scsih_response_code - translation of device response code
2116 * @ioc: per adapter object
2117 * @response_code: response code returned by the device
2118 *
2119 * Return nothing.
2120 */
2121static void
2122_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2123{
2124 char *desc;
2125
2126 switch (response_code) {
2127 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2128 desc = "task management request completed";
2129 break;
2130 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2131 desc = "invalid frame";
2132 break;
2133 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2134 desc = "task management request not supported";
2135 break;
2136 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2137 desc = "task management request failed";
2138 break;
2139 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2140 desc = "task management request succeeded";
2141 break;
2142 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2143 desc = "invalid lun";
2144 break;
2145 case 0xA:
2146 desc = "overlapped tag attempted";
2147 break;
2148 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2149 desc = "task queued, however not sent to target";
2150 break;
2151 default:
2152 desc = "unknown";
2153 break;
2154 }
2155 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2156 ioc->name, response_code, desc);
2157}
2158
2159/**
2160 * _scsih_tm_done - tm completion routine
2161 * @ioc: per adapter object
2162 * @smid: system request message index
2163 * @msix_index: MSIX table index supplied by the OS
2164 * @reply: reply message frame(lower 32bit addr)
2165 * Context: none.
2166 *
2167 * The callback handler when using scsih_issue_tm.
2168 *
2169 * Return 1 meaning mf should be freed from _base_interrupt
2170 * 0 means the mf is freed from this function.
2171 */
2172static u8
2173_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2174{
2175 MPI2DefaultReply_t *mpi_reply;
2176
2177 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2178 return 1;
2179 if (ioc->tm_cmds.smid != smid)
2180 return 1;
f92363d1
SR
2181 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2182 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
2183 if (mpi_reply) {
2184 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2185 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2186 }
2187 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2188 complete(&ioc->tm_cmds.done);
2189 return 1;
2190}
2191
2192/**
2193 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2194 * @ioc: per adapter object
2195 * @handle: device handle
2196 *
2197 * During taskmangement request, we need to freeze the device queue.
2198 */
2199void
2200mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2201{
2202 struct MPT3SAS_DEVICE *sas_device_priv_data;
2203 struct scsi_device *sdev;
2204 u8 skip = 0;
2205
2206 shost_for_each_device(sdev, ioc->shost) {
2207 if (skip)
2208 continue;
2209 sas_device_priv_data = sdev->hostdata;
2210 if (!sas_device_priv_data)
2211 continue;
2212 if (sas_device_priv_data->sas_target->handle == handle) {
2213 sas_device_priv_data->sas_target->tm_busy = 1;
2214 skip = 1;
2215 ioc->ignore_loginfos = 1;
2216 }
2217 }
2218}
2219
2220/**
2221 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2222 * @ioc: per adapter object
2223 * @handle: device handle
2224 *
2225 * During taskmangement request, we need to freeze the device queue.
2226 */
2227void
2228mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2229{
2230 struct MPT3SAS_DEVICE *sas_device_priv_data;
2231 struct scsi_device *sdev;
2232 u8 skip = 0;
2233
2234 shost_for_each_device(sdev, ioc->shost) {
2235 if (skip)
2236 continue;
2237 sas_device_priv_data = sdev->hostdata;
2238 if (!sas_device_priv_data)
2239 continue;
2240 if (sas_device_priv_data->sas_target->handle == handle) {
2241 sas_device_priv_data->sas_target->tm_busy = 0;
2242 skip = 1;
2243 ioc->ignore_loginfos = 0;
2244 }
2245 }
2246}
2247
2248/**
2249 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2250 * @ioc: per adapter struct
2251 * @device_handle: device handle
2252 * @channel: the channel assigned by the OS
2253 * @id: the id assigned by the OS
2254 * @lun: lun number
2255 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2256 * @smid_task: smid assigned to the task
2257 * @timeout: timeout in seconds
f92363d1
SR
2258 * Context: user
2259 *
2260 * A generic API for sending task management requests to firmware.
2261 *
2262 * The callback index is set inside `ioc->tm_cb_idx`.
2263 *
2264 * Return SUCCESS or FAILED.
2265 */
2266int
2267mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
96902835 2268 uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
f92363d1
SR
2269{
2270 Mpi2SCSITaskManagementRequest_t *mpi_request;
2271 Mpi2SCSITaskManagementReply_t *mpi_reply;
2272 u16 smid = 0;
2273 u32 ioc_state;
f92363d1
SR
2274 struct scsiio_tracker *scsi_lookup = NULL;
2275 int rc;
03d1fb3a 2276 u16 msix_task = 0;
f92363d1 2277
96902835
CO
2278 lockdep_assert_held(&ioc->tm_cmds.mutex);
2279
f92363d1
SR
2280 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2281 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2282 __func__, ioc->name);
96902835 2283 return FAILED;
f92363d1
SR
2284 }
2285
2286 if (ioc->shost_recovery || ioc->remove_host ||
2287 ioc->pci_error_recovery) {
2288 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2289 __func__, ioc->name);
96902835 2290 return FAILED;
f92363d1
SR
2291 }
2292
2293 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2294 if (ioc_state & MPI2_DOORBELL_USED) {
2295 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2296 "unexpected doorbell active!\n", ioc->name));
98c56ad3 2297 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
96902835 2298 return (!rc) ? SUCCESS : FAILED;
f92363d1
SR
2299 }
2300
2301 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2302 mpt3sas_base_fault_info(ioc, ioc_state &
2303 MPI2_DOORBELL_DATA_MASK);
98c56ad3 2304 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
96902835 2305 return (!rc) ? SUCCESS : FAILED;
f92363d1
SR
2306 }
2307
2308 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2309 if (!smid) {
2310 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2311 ioc->name, __func__);
96902835 2312 return FAILED;
f92363d1
SR
2313 }
2314
2315 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2316 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2317
2318 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2319 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2320 ioc->name, handle, type, smid_task));
2321 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2322 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2323 ioc->tm_cmds.smid = smid;
2324 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2325 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2326 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2327 mpi_request->DevHandle = cpu_to_le16(handle);
2328 mpi_request->TaskType = type;
2329 mpi_request->TaskMID = cpu_to_le16(smid_task);
2330 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2331 mpt3sas_scsih_set_tm_flag(ioc, handle);
2332 init_completion(&ioc->tm_cmds.done);
03d1fb3a
SS
2333 if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2334 (scsi_lookup->msix_io < ioc->reply_queue_count))
2335 msix_task = scsi_lookup->msix_io;
2336 else
2337 msix_task = 0;
81c16f83 2338 ioc->put_smid_hi_priority(ioc, smid, msix_task);
8bbb1cf6 2339 wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
f92363d1
SR
2340 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2341 pr_err(MPT3SAS_FMT "%s: timeout\n",
2342 ioc->name, __func__);
2343 _debug_dump_mf(mpi_request,
2344 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2345 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
98c56ad3
CO
2346 rc = mpt3sas_base_hard_reset_handler(ioc,
2347 FORCE_BIG_HAMMER);
f92363d1 2348 rc = (!rc) ? SUCCESS : FAILED;
96902835 2349 goto out;
f92363d1
SR
2350 }
2351 }
2352
5f0dfb7a
C
2353 /* sync IRQs in case those were busy during flush. */
2354 mpt3sas_base_sync_reply_irqs(ioc);
2355
f92363d1
SR
2356 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2357 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2358 mpi_reply = ioc->tm_cmds.reply;
2359 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2360 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2361 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2362 le32_to_cpu(mpi_reply->IOCLogInfo),
2363 le32_to_cpu(mpi_reply->TerminationCount)));
2364 if (ioc->logging_level & MPT_DEBUG_TM) {
2365 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2366 if (mpi_reply->IOCStatus)
2367 _debug_dump_mf(mpi_request,
2368 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2369 }
2370 }
2371
2372 switch (type) {
2373 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2374 rc = SUCCESS;
2375 if (scsi_lookup->scmd == NULL)
2376 break;
2377 rc = FAILED;
2378 break;
2379
2380 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2381 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2382 rc = FAILED;
2383 else
2384 rc = SUCCESS;
2385 break;
2386 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2387 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2388 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2389 rc = FAILED;
2390 else
2391 rc = SUCCESS;
2392 break;
2393 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2394 rc = SUCCESS;
2395 break;
2396 default:
2397 rc = FAILED;
2398 break;
2399 }
2400
96902835 2401out:
f92363d1
SR
2402 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2403 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
f92363d1 2404 return rc;
96902835 2405}
f92363d1 2406
96902835
CO
2407int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
2408 uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2409{
2410 int ret;
2411
2412 mutex_lock(&ioc->tm_cmds.mutex);
2413 ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
2414 smid_task, timeout);
2415 mutex_unlock(&ioc->tm_cmds.mutex);
2416
2417 return ret;
f92363d1
SR
2418}
2419
2420/**
2421 * _scsih_tm_display_info - displays info about the device
2422 * @ioc: per adapter struct
2423 * @scmd: pointer to scsi command object
2424 *
2425 * Called by task management callback handlers.
2426 */
2427static void
2428_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2429{
2430 struct scsi_target *starget = scmd->device->sdev_target;
2431 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2432 struct _sas_device *sas_device = NULL;
2433 unsigned long flags;
2434 char *device_str = NULL;
2435
2436 if (!priv_target)
2437 return;
7786ab6a
SR
2438 if (ioc->hide_ir_msg)
2439 device_str = "WarpDrive";
2440 else
2441 device_str = "volume";
f92363d1
SR
2442
2443 scsi_print_command(scmd);
2444 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2445 starget_printk(KERN_INFO, starget,
2446 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2447 device_str, priv_target->handle,
2448 device_str, (unsigned long long)priv_target->sas_address);
2449 } else {
2450 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 2451 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
f92363d1
SR
2452 if (sas_device) {
2453 if (priv_target->flags &
2454 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2455 starget_printk(KERN_INFO, starget,
2456 "volume handle(0x%04x), "
2457 "volume wwid(0x%016llx)\n",
2458 sas_device->volume_handle,
2459 (unsigned long long)sas_device->volume_wwid);
2460 }
2461 starget_printk(KERN_INFO, starget,
2462 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2463 sas_device->handle,
2464 (unsigned long long)sas_device->sas_address,
2465 sas_device->phy);
75888956
SR
2466
2467 _scsih_display_enclosure_chassis_info(NULL, sas_device,
2468 NULL, starget);
d1cb5e49
SR
2469
2470 sas_device_put(sas_device);
f92363d1
SR
2471 }
2472 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2473 }
2474}
2475
2476/**
8a7e4c24 2477 * scsih_abort - eh threads main abort routine
f92363d1
SR
2478 * @scmd: pointer to scsi command object
2479 *
2480 * Returns SUCCESS if command aborted else FAILED
2481 */
8bbb1cf6 2482static int
8a7e4c24 2483scsih_abort(struct scsi_cmnd *scmd)
f92363d1
SR
2484{
2485 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2486 struct MPT3SAS_DEVICE *sas_device_priv_data;
2487 u16 smid;
2488 u16 handle;
2489 int r;
2490
2491 sdev_printk(KERN_INFO, scmd->device,
2492 "attempting task abort! scmd(%p)\n", scmd);
2493 _scsih_tm_display_info(ioc, scmd);
2494
2495 sas_device_priv_data = scmd->device->hostdata;
2496 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2497 sdev_printk(KERN_INFO, scmd->device,
2498 "device been deleted! scmd(%p)\n", scmd);
2499 scmd->result = DID_NO_CONNECT << 16;
2500 scmd->scsi_done(scmd);
2501 r = SUCCESS;
2502 goto out;
2503 }
2504
2505 /* search for the command */
2506 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2507 if (!smid) {
2508 scmd->result = DID_RESET << 16;
2509 r = SUCCESS;
2510 goto out;
2511 }
2512
2513 /* for hidden raid components and volumes this is not supported */
2514 if (sas_device_priv_data->sas_target->flags &
2515 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2516 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2517 scmd->result = DID_RESET << 16;
2518 r = FAILED;
2519 goto out;
2520 }
2521
2522 mpt3sas_halt_firmware(ioc);
2523
2524 handle = sas_device_priv_data->sas_target->handle;
96902835 2525 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
f92363d1 2526 scmd->device->id, scmd->device->lun,
96902835 2527 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
f92363d1
SR
2528
2529 out:
2530 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2531 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2532 return r;
2533}
2534
2535/**
8a7e4c24 2536 * scsih_dev_reset - eh threads main device reset routine
f92363d1
SR
2537 * @scmd: pointer to scsi command object
2538 *
2539 * Returns SUCCESS if command aborted else FAILED
2540 */
8bbb1cf6 2541static int
8a7e4c24 2542scsih_dev_reset(struct scsi_cmnd *scmd)
f92363d1
SR
2543{
2544 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2545 struct MPT3SAS_DEVICE *sas_device_priv_data;
d1cb5e49 2546 struct _sas_device *sas_device = NULL;
f92363d1
SR
2547 u16 handle;
2548 int r;
2549
d1cb5e49
SR
2550 struct scsi_target *starget = scmd->device->sdev_target;
2551 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2552
f92363d1
SR
2553 sdev_printk(KERN_INFO, scmd->device,
2554 "attempting device reset! scmd(%p)\n", scmd);
2555 _scsih_tm_display_info(ioc, scmd);
2556
2557 sas_device_priv_data = scmd->device->hostdata;
2558 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2559 sdev_printk(KERN_INFO, scmd->device,
2560 "device been deleted! scmd(%p)\n", scmd);
2561 scmd->result = DID_NO_CONNECT << 16;
2562 scmd->scsi_done(scmd);
2563 r = SUCCESS;
2564 goto out;
2565 }
2566
2567 /* for hidden raid components obtain the volume_handle */
2568 handle = 0;
2569 if (sas_device_priv_data->sas_target->flags &
2570 MPT_TARGET_FLAGS_RAID_COMPONENT) {
d1cb5e49
SR
2571 sas_device = mpt3sas_get_sdev_from_target(ioc,
2572 target_priv_data);
f92363d1
SR
2573 if (sas_device)
2574 handle = sas_device->volume_handle;
f92363d1
SR
2575 } else
2576 handle = sas_device_priv_data->sas_target->handle;
2577
2578 if (!handle) {
2579 scmd->result = DID_RESET << 16;
2580 r = FAILED;
2581 goto out;
2582 }
2583
96902835 2584 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
f92363d1 2585 scmd->device->id, scmd->device->lun,
96902835 2586 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
f92363d1
SR
2587
2588 out:
2589 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2590 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
d1cb5e49
SR
2591
2592 if (sas_device)
2593 sas_device_put(sas_device);
2594
f92363d1
SR
2595 return r;
2596}
2597
2598/**
8a7e4c24 2599 * scsih_target_reset - eh threads main target reset routine
f92363d1
SR
2600 * @scmd: pointer to scsi command object
2601 *
2602 * Returns SUCCESS if command aborted else FAILED
2603 */
8bbb1cf6 2604static int
8a7e4c24 2605scsih_target_reset(struct scsi_cmnd *scmd)
f92363d1
SR
2606{
2607 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2608 struct MPT3SAS_DEVICE *sas_device_priv_data;
d1cb5e49 2609 struct _sas_device *sas_device = NULL;
f92363d1
SR
2610 u16 handle;
2611 int r;
2612 struct scsi_target *starget = scmd->device->sdev_target;
d1cb5e49 2613 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
f92363d1
SR
2614
2615 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2616 scmd);
2617 _scsih_tm_display_info(ioc, scmd);
2618
2619 sas_device_priv_data = scmd->device->hostdata;
2620 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2621 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2622 scmd);
2623 scmd->result = DID_NO_CONNECT << 16;
2624 scmd->scsi_done(scmd);
2625 r = SUCCESS;
2626 goto out;
2627 }
2628
2629 /* for hidden raid components obtain the volume_handle */
2630 handle = 0;
2631 if (sas_device_priv_data->sas_target->flags &
2632 MPT_TARGET_FLAGS_RAID_COMPONENT) {
d1cb5e49
SR
2633 sas_device = mpt3sas_get_sdev_from_target(ioc,
2634 target_priv_data);
f92363d1
SR
2635 if (sas_device)
2636 handle = sas_device->volume_handle;
f92363d1
SR
2637 } else
2638 handle = sas_device_priv_data->sas_target->handle;
2639
2640 if (!handle) {
2641 scmd->result = DID_RESET << 16;
2642 r = FAILED;
2643 goto out;
2644 }
2645
96902835 2646 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
f92363d1 2647 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
96902835 2648 30);
f92363d1
SR
2649
2650 out:
2651 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2652 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
d1cb5e49
SR
2653
2654 if (sas_device)
2655 sas_device_put(sas_device);
2656
f92363d1
SR
2657 return r;
2658}
2659
2660
2661/**
8a7e4c24 2662 * scsih_host_reset - eh threads main host reset routine
f92363d1
SR
2663 * @scmd: pointer to scsi command object
2664 *
2665 * Returns SUCCESS if command aborted else FAILED
2666 */
8bbb1cf6 2667static int
8a7e4c24 2668scsih_host_reset(struct scsi_cmnd *scmd)
f92363d1
SR
2669{
2670 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2671 int r, retval;
2672
2673 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2674 ioc->name, scmd);
2675 scsi_print_command(scmd);
2676
ddb588be
SR
2677 if (ioc->is_driver_loading) {
2678 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2679 ioc->name);
2680 r = FAILED;
2681 goto out;
2682 }
2683
98c56ad3 2684 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1 2685 r = (retval < 0) ? FAILED : SUCCESS;
ddb588be 2686out:
f92363d1
SR
2687 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2688 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2689
2690 return r;
2691}
2692
2693/**
2694 * _scsih_fw_event_add - insert and queue up fw_event
2695 * @ioc: per adapter object
2696 * @fw_event: object describing the event
2697 * Context: This function will acquire ioc->fw_event_lock.
2698 *
2699 * This adds the firmware event object into link list, then queues it up to
2700 * be processed from user context.
2701 *
2702 * Return nothing.
2703 */
2704static void
2705_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2706{
2707 unsigned long flags;
2708
2709 if (ioc->firmware_event_thread == NULL)
2710 return;
2711
2712 spin_lock_irqsave(&ioc->fw_event_lock, flags);
146b16c8 2713 fw_event_work_get(fw_event);
f92363d1
SR
2714 INIT_LIST_HEAD(&fw_event->list);
2715 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2716 INIT_WORK(&fw_event->work, _firmware_event_work);
146b16c8 2717 fw_event_work_get(fw_event);
f92363d1
SR
2718 queue_work(ioc->firmware_event_thread, &fw_event->work);
2719 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2720}
2721
2722/**
146b16c8 2723 * _scsih_fw_event_del_from_list - delete fw_event from the list
f92363d1
SR
2724 * @ioc: per adapter object
2725 * @fw_event: object describing the event
2726 * Context: This function will acquire ioc->fw_event_lock.
2727 *
146b16c8 2728 * If the fw_event is on the fw_event_list, remove it and do a put.
f92363d1
SR
2729 *
2730 * Return nothing.
2731 */
2732static void
146b16c8 2733_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
f92363d1
SR
2734 *fw_event)
2735{
2736 unsigned long flags;
2737
2738 spin_lock_irqsave(&ioc->fw_event_lock, flags);
146b16c8
SR
2739 if (!list_empty(&fw_event->list)) {
2740 list_del_init(&fw_event->list);
2741 fw_event_work_put(fw_event);
2742 }
f92363d1
SR
2743 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2744}
2745
2746
2747 /**
2748 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2749 * @ioc: per adapter object
2750 * @event_data: trigger event data
2751 *
2752 * Return nothing.
2753 */
2754void
2755mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2756 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2757{
2758 struct fw_event_work *fw_event;
146b16c8 2759 u16 sz;
f92363d1
SR
2760
2761 if (ioc->is_driver_loading)
2762 return;
146b16c8
SR
2763 sz = sizeof(*event_data);
2764 fw_event = alloc_fw_event_work(sz);
f92363d1
SR
2765 if (!fw_event)
2766 return;
f92363d1
SR
2767 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2768 fw_event->ioc = ioc;
2769 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2770 _scsih_fw_event_add(ioc, fw_event);
146b16c8 2771 fw_event_work_put(fw_event);
f92363d1
SR
2772}
2773
2774/**
2775 * _scsih_error_recovery_delete_devices - remove devices not responding
2776 * @ioc: per adapter object
2777 *
2778 * Return nothing.
2779 */
2780static void
2781_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2782{
2783 struct fw_event_work *fw_event;
2784
2785 if (ioc->is_driver_loading)
2786 return;
146b16c8 2787 fw_event = alloc_fw_event_work(0);
f92363d1
SR
2788 if (!fw_event)
2789 return;
2790 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2791 fw_event->ioc = ioc;
2792 _scsih_fw_event_add(ioc, fw_event);
146b16c8 2793 fw_event_work_put(fw_event);
f92363d1
SR
2794}
2795
2796/**
2797 * mpt3sas_port_enable_complete - port enable completed (fake event)
2798 * @ioc: per adapter object
2799 *
2800 * Return nothing.
2801 */
2802void
2803mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2804{
2805 struct fw_event_work *fw_event;
2806
146b16c8 2807 fw_event = alloc_fw_event_work(0);
f92363d1
SR
2808 if (!fw_event)
2809 return;
2810 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2811 fw_event->ioc = ioc;
2812 _scsih_fw_event_add(ioc, fw_event);
146b16c8
SR
2813 fw_event_work_put(fw_event);
2814}
2815
2816static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2817{
2818 unsigned long flags;
2819 struct fw_event_work *fw_event = NULL;
2820
2821 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2822 if (!list_empty(&ioc->fw_event_list)) {
2823 fw_event = list_first_entry(&ioc->fw_event_list,
2824 struct fw_event_work, list);
2825 list_del_init(&fw_event->list);
2826 }
2827 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2828
2829 return fw_event;
f92363d1
SR
2830}
2831
2832/**
2833 * _scsih_fw_event_cleanup_queue - cleanup event queue
2834 * @ioc: per adapter object
2835 *
2836 * Walk the firmware event queue, either killing timers, or waiting
2837 * for outstanding events to complete
2838 *
2839 * Return nothing.
2840 */
2841static void
2842_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2843{
146b16c8 2844 struct fw_event_work *fw_event;
f92363d1
SR
2845
2846 if (list_empty(&ioc->fw_event_list) ||
2847 !ioc->firmware_event_thread || in_interrupt())
2848 return;
2849
146b16c8
SR
2850 while ((fw_event = dequeue_next_fw_event(ioc))) {
2851 /*
2852 * Wait on the fw_event to complete. If this returns 1, then
2853 * the event was never executed, and we need a put for the
b8ac0cc7 2854 * reference the work had on the fw_event.
146b16c8
SR
2855 *
2856 * If it did execute, we wait for it to finish, and the put will
2857 * happen from _firmware_event_work()
2858 */
b8ac0cc7 2859 if (cancel_work_sync(&fw_event->work))
146b16c8
SR
2860 fw_event_work_put(fw_event);
2861
2862 fw_event_work_put(fw_event);
f92363d1
SR
2863 }
2864}
2865
df838f92
SR
2866/**
2867 * _scsih_internal_device_block - block the sdev device
2868 * @sdev: per device object
2869 * @sas_device_priv_data : per device driver private data
2870 *
2871 * make sure device is blocked without error, if not
2872 * print an error
2873 */
2874static void
2875_scsih_internal_device_block(struct scsi_device *sdev,
2876 struct MPT3SAS_DEVICE *sas_device_priv_data)
2877{
2878 int r = 0;
2879
2880 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2881 sas_device_priv_data->sas_target->handle);
2882 sas_device_priv_data->block = 1;
2883
551eb598 2884 r = scsi_internal_device_block_nowait(sdev);
df838f92
SR
2885 if (r == -EINVAL)
2886 sdev_printk(KERN_WARNING, sdev,
2887 "device_block failed with return(%d) for handle(0x%04x)\n",
b2fe6be7 2888 r, sas_device_priv_data->sas_target->handle);
df838f92
SR
2889}
2890
2891/**
2892 * _scsih_internal_device_unblock - unblock the sdev device
2893 * @sdev: per device object
2894 * @sas_device_priv_data : per device driver private data
2895 * make sure device is unblocked without error, if not retry
2896 * by blocking and then unblocking
2897 */
2898
2899static void
2900_scsih_internal_device_unblock(struct scsi_device *sdev,
2901 struct MPT3SAS_DEVICE *sas_device_priv_data)
2902{
2903 int r = 0;
2904
2905 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2906 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2907 sas_device_priv_data->block = 0;
43f7571b 2908 r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
df838f92
SR
2909 if (r == -EINVAL) {
2910 /* The device has been set to SDEV_RUNNING by SD layer during
2911 * device addition but the request queue is still stopped by
2912 * our earlier block call. We need to perform a block again
2913 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2914
2915 sdev_printk(KERN_WARNING, sdev,
2916 "device_unblock failed with return(%d) for handle(0x%04x) "
2917 "performing a block followed by an unblock\n",
b2fe6be7 2918 r, sas_device_priv_data->sas_target->handle);
df838f92 2919 sas_device_priv_data->block = 1;
551eb598 2920 r = scsi_internal_device_block_nowait(sdev);
df838f92
SR
2921 if (r)
2922 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2923 "failed with return(%d) for handle(0x%04x)\n",
b2fe6be7 2924 r, sas_device_priv_data->sas_target->handle);
df838f92
SR
2925
2926 sas_device_priv_data->block = 0;
43f7571b 2927 r = scsi_internal_device_unblock_nowait(sdev, SDEV_RUNNING);
df838f92
SR
2928 if (r)
2929 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2930 " failed with return(%d) for handle(0x%04x)\n",
b2fe6be7 2931 r, sas_device_priv_data->sas_target->handle);
df838f92
SR
2932 }
2933}
2934
f92363d1
SR
2935/**
2936 * _scsih_ublock_io_all_device - unblock every device
2937 * @ioc: per adapter object
2938 *
2939 * change the device state from block to running
2940 */
2941static void
2942_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2943{
2944 struct MPT3SAS_DEVICE *sas_device_priv_data;
2945 struct scsi_device *sdev;
2946
2947 shost_for_each_device(sdev, ioc->shost) {
2948 sas_device_priv_data = sdev->hostdata;
2949 if (!sas_device_priv_data)
2950 continue;
2951 if (!sas_device_priv_data->block)
2952 continue;
2953
f92363d1
SR
2954 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2955 "device_running, handle(0x%04x)\n",
2956 sas_device_priv_data->sas_target->handle));
df838f92 2957 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
f92363d1
SR
2958 }
2959}
2960
2961
2962/**
2963 * _scsih_ublock_io_device - prepare device to be deleted
2964 * @ioc: per adapter object
2965 * @sas_addr: sas address
2966 *
2967 * unblock then put device in offline state
2968 */
2969static void
2970_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2971{
2972 struct MPT3SAS_DEVICE *sas_device_priv_data;
2973 struct scsi_device *sdev;
2974
2975 shost_for_each_device(sdev, ioc->shost) {
2976 sas_device_priv_data = sdev->hostdata;
2977 if (!sas_device_priv_data)
2978 continue;
2979 if (sas_device_priv_data->sas_target->sas_address
2980 != sas_address)
2981 continue;
df838f92
SR
2982 if (sas_device_priv_data->block)
2983 _scsih_internal_device_unblock(sdev,
2984 sas_device_priv_data);
f92363d1
SR
2985 }
2986}
2987
2988/**
2989 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2990 * @ioc: per adapter object
2991 * @handle: device handle
2992 *
6c7abffc 2993 * During device pull we need to appropriately set the sdev state.
f92363d1
SR
2994 */
2995static void
2996_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2997{
2998 struct MPT3SAS_DEVICE *sas_device_priv_data;
2999 struct scsi_device *sdev;
3000
3001 shost_for_each_device(sdev, ioc->shost) {
3002 sas_device_priv_data = sdev->hostdata;
3003 if (!sas_device_priv_data)
3004 continue;
3005 if (sas_device_priv_data->block)
3006 continue;
30158dc9
SS
3007 if (sas_device_priv_data->ignore_delay_remove) {
3008 sdev_printk(KERN_INFO, sdev,
3009 "%s skip device_block for SES handle(0x%04x)\n",
3010 __func__, sas_device_priv_data->sas_target->handle);
3011 continue;
3012 }
df838f92 3013 _scsih_internal_device_block(sdev, sas_device_priv_data);
f92363d1
SR
3014 }
3015}
3016
3017/**
3018 * _scsih_block_io_device - set the device state to SDEV_BLOCK
3019 * @ioc: per adapter object
3020 * @handle: device handle
3021 *
6c7abffc 3022 * During device pull we need to appropriately set the sdev state.
f92363d1
SR
3023 */
3024static void
3025_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3026{
3027 struct MPT3SAS_DEVICE *sas_device_priv_data;
3028 struct scsi_device *sdev;
e4bc7f5c
SR
3029 struct _sas_device *sas_device;
3030
d1cb5e49 3031 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
e4bc7f5c
SR
3032 if (!sas_device)
3033 return;
f92363d1
SR
3034
3035 shost_for_each_device(sdev, ioc->shost) {
3036 sas_device_priv_data = sdev->hostdata;
3037 if (!sas_device_priv_data)
3038 continue;
3039 if (sas_device_priv_data->sas_target->handle != handle)
3040 continue;
3041 if (sas_device_priv_data->block)
3042 continue;
e4bc7f5c
SR
3043 if (sas_device->pend_sas_rphy_add)
3044 continue;
30158dc9
SS
3045 if (sas_device_priv_data->ignore_delay_remove) {
3046 sdev_printk(KERN_INFO, sdev,
3047 "%s skip device_block for SES handle(0x%04x)\n",
3048 __func__, sas_device_priv_data->sas_target->handle);
3049 continue;
3050 }
df838f92 3051 _scsih_internal_device_block(sdev, sas_device_priv_data);
f92363d1 3052 }
d1cb5e49
SR
3053
3054 sas_device_put(sas_device);
f92363d1
SR
3055}
3056
3057/**
3058 * _scsih_block_io_to_children_attached_to_ex
3059 * @ioc: per adapter object
3060 * @sas_expander: the sas_device object
3061 *
3062 * This routine set sdev state to SDEV_BLOCK for all devices
3063 * attached to this expander. This function called when expander is
3064 * pulled.
3065 */
3066static void
3067_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3068 struct _sas_node *sas_expander)
3069{
3070 struct _sas_port *mpt3sas_port;
3071 struct _sas_device *sas_device;
3072 struct _sas_node *expander_sibling;
3073 unsigned long flags;
3074
3075 if (!sas_expander)
3076 return;
3077
3078 list_for_each_entry(mpt3sas_port,
3079 &sas_expander->sas_port_list, port_list) {
3080 if (mpt3sas_port->remote_identify.device_type ==
3081 SAS_END_DEVICE) {
3082 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
3083 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3084 mpt3sas_port->remote_identify.sas_address);
3085 if (sas_device) {
f92363d1 3086 set_bit(sas_device->handle,
d1cb5e49
SR
3087 ioc->blocking_handles);
3088 sas_device_put(sas_device);
3089 }
f92363d1
SR
3090 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3091 }
3092 }
3093
3094 list_for_each_entry(mpt3sas_port,
3095 &sas_expander->sas_port_list, port_list) {
3096
3097 if (mpt3sas_port->remote_identify.device_type ==
3098 SAS_EDGE_EXPANDER_DEVICE ||
3099 mpt3sas_port->remote_identify.device_type ==
3100 SAS_FANOUT_EXPANDER_DEVICE) {
3101 expander_sibling =
3102 mpt3sas_scsih_expander_find_by_sas_address(
3103 ioc, mpt3sas_port->remote_identify.sas_address);
3104 _scsih_block_io_to_children_attached_to_ex(ioc,
3105 expander_sibling);
3106 }
3107 }
3108}
3109
3110/**
3111 * _scsih_block_io_to_children_attached_directly
3112 * @ioc: per adapter object
3113 * @event_data: topology change event data
3114 *
3115 * This routine set sdev state to SDEV_BLOCK for all devices
3116 * direct attached during device pull.
3117 */
3118static void
3119_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3120 Mpi2EventDataSasTopologyChangeList_t *event_data)
3121{
3122 int i;
3123 u16 handle;
3124 u16 reason_code;
f92363d1
SR
3125
3126 for (i = 0; i < event_data->NumEntries; i++) {
3127 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3128 if (!handle)
3129 continue;
f92363d1
SR
3130 reason_code = event_data->PHY[i].PhyStatus &
3131 MPI2_EVENT_SAS_TOPO_RC_MASK;
3132 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3133 _scsih_block_io_device(ioc, handle);
3134 }
3135}
3136
3137/**
3138 * _scsih_tm_tr_send - send task management request
3139 * @ioc: per adapter object
3140 * @handle: device handle
3141 * Context: interrupt time.
3142 *
3143 * This code is to initiate the device removal handshake protocol
3144 * with controller firmware. This function will issue target reset
3145 * using high priority request queue. It will send a sas iounit
3146 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3147 *
3148 * This is designed to send muliple task management request at the same
3149 * time to the fifo. If the fifo is full, we will append the request,
3150 * and process it in a future completion.
3151 */
3152static void
3153_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3154{
3155 Mpi2SCSITaskManagementRequest_t *mpi_request;
3156 u16 smid;
d1cb5e49 3157 struct _sas_device *sas_device = NULL;
f92363d1
SR
3158 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3159 u64 sas_address = 0;
3160 unsigned long flags;
3161 struct _tr_list *delayed_tr;
3162 u32 ioc_state;
3163
3164 if (ioc->remove_host) {
3165 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3166 "%s: host has been removed: handle(0x%04x)\n",
3167 __func__, ioc->name, handle));
3168 return;
3169 } else if (ioc->pci_error_recovery) {
3170 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3171 "%s: host in pci error recovery: handle(0x%04x)\n",
3172 __func__, ioc->name,
3173 handle));
3174 return;
3175 }
3176 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3177 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3178 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3179 "%s: host is not operational: handle(0x%04x)\n",
3180 __func__, ioc->name,
3181 handle));
3182 return;
3183 }
3184
3185 /* if PD, then return */
3186 if (test_bit(handle, ioc->pd_handles))
3187 return;
3188
c696f7b8
SPS
3189 clear_bit(handle, ioc->pend_os_device_add);
3190
f92363d1 3191 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 3192 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
3193 if (sas_device && sas_device->starget &&
3194 sas_device->starget->hostdata) {
3195 sas_target_priv_data = sas_device->starget->hostdata;
3196 sas_target_priv_data->deleted = 1;
3197 sas_address = sas_device->sas_address;
3198 }
3199 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3200
3201 if (sas_target_priv_data) {
3202 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3203 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3204 ioc->name, handle,
3205 (unsigned long long)sas_address));
75888956
SR
3206 dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc,
3207 sas_device, NULL, NULL));
f92363d1
SR
3208 _scsih_ublock_io_device(ioc, sas_address);
3209 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3210 }
3211
3212 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3213 if (!smid) {
3214 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3215 if (!delayed_tr)
d1cb5e49 3216 goto out;
f92363d1
SR
3217 INIT_LIST_HEAD(&delayed_tr->list);
3218 delayed_tr->handle = handle;
3219 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3220 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3221 "DELAYED:tr:handle(0x%04x), (open)\n",
3222 ioc->name, handle));
d1cb5e49 3223 goto out;
f92363d1
SR
3224 }
3225
3226 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3227 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3228 ioc->name, handle, smid,
3229 ioc->tm_tr_cb_idx));
3230 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3231 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3232 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3233 mpi_request->DevHandle = cpu_to_le16(handle);
3234 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
c696f7b8 3235 set_bit(handle, ioc->device_remove_in_progress);
81c16f83 3236 ioc->put_smid_hi_priority(ioc, smid, 0);
f92363d1 3237 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
d1cb5e49
SR
3238
3239out:
3240 if (sas_device)
3241 sas_device_put(sas_device);
f92363d1
SR
3242}
3243
3244/**
3245 * _scsih_tm_tr_complete -
3246 * @ioc: per adapter object
3247 * @smid: system request message index
3248 * @msix_index: MSIX table index supplied by the OS
3249 * @reply: reply message frame(lower 32bit addr)
3250 * Context: interrupt time.
3251 *
3252 * This is the target reset completion routine.
3253 * This code is part of the code to initiate the device removal
3254 * handshake protocol with controller firmware.
3255 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3256 *
3257 * Return 1 meaning mf should be freed from _base_interrupt
3258 * 0 means the mf is freed from this function.
3259 */
3260static u8
3261_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3262 u32 reply)
3263{
3264 u16 handle;
3265 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3266 Mpi2SCSITaskManagementReply_t *mpi_reply =
3267 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3268 Mpi2SasIoUnitControlRequest_t *mpi_request;
3269 u16 smid_sas_ctrl;
3270 u32 ioc_state;
fd0331b3 3271 struct _sc_list *delayed_sc;
f92363d1
SR
3272
3273 if (ioc->remove_host) {
3274 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3275 "%s: host has been removed\n", __func__, ioc->name));
3276 return 1;
3277 } else if (ioc->pci_error_recovery) {
3278 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3279 "%s: host in pci error recovery\n", __func__,
3280 ioc->name));
3281 return 1;
3282 }
3283 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3284 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3285 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3286 "%s: host is not operational\n", __func__, ioc->name));
3287 return 1;
3288 }
3289 if (unlikely(!mpi_reply)) {
3290 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3291 ioc->name, __FILE__, __LINE__, __func__);
3292 return 1;
3293 }
3294 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3295 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3296 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3297 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3298 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3299 ioc->name, handle,
3300 le16_to_cpu(mpi_reply->DevHandle), smid));
3301 return 0;
3302 }
3303
3304 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3305 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3306 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3307 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3308 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3309 le32_to_cpu(mpi_reply->IOCLogInfo),
3310 le32_to_cpu(mpi_reply->TerminationCount)));
3311
3312 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3313 if (!smid_sas_ctrl) {
fd0331b3
SS
3314 delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3315 if (!delayed_sc)
3316 return _scsih_check_for_pending_tm(ioc, smid);
3317 INIT_LIST_HEAD(&delayed_sc->list);
3318 delayed_sc->handle = mpi_request_tm->DevHandle;
3319 list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3320 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3321 "DELAYED:sc:handle(0x%04x), (open)\n",
3322 ioc->name, handle));
3323 return _scsih_check_for_pending_tm(ioc, smid);
f92363d1
SR
3324 }
3325
3326 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3327 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3328 ioc->name, handle, smid_sas_ctrl,
3329 ioc->tm_sas_control_cb_idx));
3330 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3331 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3332 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3333 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3334 mpi_request->DevHandle = mpi_request_tm->DevHandle;
81c16f83 3335 ioc->put_smid_default(ioc, smid_sas_ctrl);
f92363d1
SR
3336
3337 return _scsih_check_for_pending_tm(ioc, smid);
3338}
3339
3340
3341/**
3342 * _scsih_sas_control_complete - completion routine
3343 * @ioc: per adapter object
3344 * @smid: system request message index
3345 * @msix_index: MSIX table index supplied by the OS
3346 * @reply: reply message frame(lower 32bit addr)
3347 * Context: interrupt time.
3348 *
3349 * This is the sas iounit control completion routine.
3350 * This code is part of the code to initiate the device removal
3351 * handshake protocol with controller firmware.
3352 *
3353 * Return 1 meaning mf should be freed from _base_interrupt
3354 * 0 means the mf is freed from this function.
3355 */
3356static u8
3357_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3358 u8 msix_index, u32 reply)
3359{
3360 Mpi2SasIoUnitControlReply_t *mpi_reply =
3361 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3362
3363 if (likely(mpi_reply)) {
3364 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3365 "sc_complete:handle(0x%04x), (open) "
3366 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3367 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3368 le16_to_cpu(mpi_reply->IOCStatus),
3369 le32_to_cpu(mpi_reply->IOCLogInfo)));
c696f7b8
SPS
3370 if (le16_to_cpu(mpi_reply->IOCStatus) ==
3371 MPI2_IOCSTATUS_SUCCESS) {
3372 clear_bit(le16_to_cpu(mpi_reply->DevHandle),
3373 ioc->device_remove_in_progress);
3374 }
f92363d1
SR
3375 } else {
3376 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3377 ioc->name, __FILE__, __LINE__, __func__);
3378 }
fd0331b3 3379 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
f92363d1
SR
3380}
3381
3382/**
3383 * _scsih_tm_tr_volume_send - send target reset request for volumes
3384 * @ioc: per adapter object
3385 * @handle: device handle
3386 * Context: interrupt time.
3387 *
3388 * This is designed to send muliple task management request at the same
3389 * time to the fifo. If the fifo is full, we will append the request,
3390 * and process it in a future completion.
3391 */
3392static void
3393_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3394{
3395 Mpi2SCSITaskManagementRequest_t *mpi_request;
3396 u16 smid;
3397 struct _tr_list *delayed_tr;
3398
3399 if (ioc->shost_recovery || ioc->remove_host ||
3400 ioc->pci_error_recovery) {
3401 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3402 "%s: host reset in progress!\n",
3403 __func__, ioc->name));
3404 return;
3405 }
3406
3407 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3408 if (!smid) {
3409 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3410 if (!delayed_tr)
3411 return;
3412 INIT_LIST_HEAD(&delayed_tr->list);
3413 delayed_tr->handle = handle;
3414 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3415 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3416 "DELAYED:tr:handle(0x%04x), (open)\n",
3417 ioc->name, handle));
3418 return;
3419 }
3420
3421 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3422 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3423 ioc->name, handle, smid,
3424 ioc->tm_tr_volume_cb_idx));
3425 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3426 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3427 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3428 mpi_request->DevHandle = cpu_to_le16(handle);
3429 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
81c16f83 3430 ioc->put_smid_hi_priority(ioc, smid, 0);
f92363d1
SR
3431}
3432
3433/**
3434 * _scsih_tm_volume_tr_complete - target reset completion
3435 * @ioc: per adapter object
3436 * @smid: system request message index
3437 * @msix_index: MSIX table index supplied by the OS
3438 * @reply: reply message frame(lower 32bit addr)
3439 * Context: interrupt time.
3440 *
3441 * Return 1 meaning mf should be freed from _base_interrupt
3442 * 0 means the mf is freed from this function.
3443 */
3444static u8
3445_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3446 u8 msix_index, u32 reply)
3447{
3448 u16 handle;
3449 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3450 Mpi2SCSITaskManagementReply_t *mpi_reply =
3451 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3452
3453 if (ioc->shost_recovery || ioc->remove_host ||
3454 ioc->pci_error_recovery) {
3455 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3456 "%s: host reset in progress!\n",
3457 __func__, ioc->name));
3458 return 1;
3459 }
3460 if (unlikely(!mpi_reply)) {
3461 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3462 ioc->name, __FILE__, __LINE__, __func__);
3463 return 1;
3464 }
3465
3466 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3467 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3468 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3469 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3470 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3471 ioc->name, handle,
3472 le16_to_cpu(mpi_reply->DevHandle), smid));
3473 return 0;
3474 }
3475
3476 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3477 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3478 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3479 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3480 le32_to_cpu(mpi_reply->IOCLogInfo),
3481 le32_to_cpu(mpi_reply->TerminationCount)));
3482
3483 return _scsih_check_for_pending_tm(ioc, smid);
3484}
3485
fd0331b3
SS
3486/**
3487 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3488 * @ioc: per adapter object
3489 * @smid: system request message index
3490 * @event: Event ID
3491 * @event_context: used to track events uniquely
3492 *
3493 * Context - processed in interrupt context.
3494 */
8bbb1cf6 3495static void
fd0331b3
SS
3496_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3497 u32 event_context)
3498{
3499 Mpi2EventAckRequest_t *ack_request;
3500 int i = smid - ioc->internal_smid;
3501 unsigned long flags;
3502
3503 /* Without releasing the smid just update the
3504 * call back index and reuse the same smid for
3505 * processing this delayed request
3506 */
3507 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3508 ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3509 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3510
3511 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3512 "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3513 ioc->name, le16_to_cpu(event), smid,
3514 ioc->base_cb_idx));
3515 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3516 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3517 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3518 ack_request->Event = event;
3519 ack_request->EventContext = event_context;
3520 ack_request->VF_ID = 0; /* TODO */
3521 ack_request->VP_ID = 0;
81c16f83 3522 ioc->put_smid_default(ioc, smid);
fd0331b3
SS
3523}
3524
3525/**
3526 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3527 * sas_io_unit_ctrl messages
3528 * @ioc: per adapter object
3529 * @smid: system request message index
3530 * @handle: device handle
3531 *
3532 * Context - processed in interrupt context.
3533 */
8bbb1cf6 3534static void
fd0331b3
SS
3535_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3536 u16 smid, u16 handle)
3537 {
3538 Mpi2SasIoUnitControlRequest_t *mpi_request;
3539 u32 ioc_state;
3540 int i = smid - ioc->internal_smid;
3541 unsigned long flags;
3542
3543 if (ioc->remove_host) {
3544 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3545 "%s: host has been removed\n",
3546 __func__, ioc->name));
3547 return;
3548 } else if (ioc->pci_error_recovery) {
3549 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3550 "%s: host in pci error recovery\n",
3551 __func__, ioc->name));
3552 return;
3553 }
3554 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3555 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3556 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3557 "%s: host is not operational\n",
3558 __func__, ioc->name));
3559 return;
3560 }
3561
3562 /* Without releasing the smid just update the
3563 * call back index and reuse the same smid for
3564 * processing this delayed request
3565 */
3566 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3567 ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3568 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3569
3570 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3571 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3572 ioc->name, le16_to_cpu(handle), smid,
3573 ioc->tm_sas_control_cb_idx));
3574 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3575 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3576 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3577 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3578 mpi_request->DevHandle = handle;
81c16f83 3579 ioc->put_smid_default(ioc, smid);
fd0331b3
SS
3580}
3581
3582/**
3583 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3584 * @ioc: per adapter object
3585 * @smid: system request message index
3586 *
3587 * Context: Executed in interrupt context
3588 *
3589 * This will check delayed internal messages list, and process the
3590 * next request.
3591 *
3592 * Return 1 meaning mf should be freed from _base_interrupt
3593 * 0 means the mf is freed from this function.
3594 */
3595u8
3596mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3597{
3598 struct _sc_list *delayed_sc;
3599 struct _event_ack_list *delayed_event_ack;
3600
3601 if (!list_empty(&ioc->delayed_event_ack_list)) {
3602 delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3603 struct _event_ack_list, list);
3604 _scsih_issue_delayed_event_ack(ioc, smid,
3605 delayed_event_ack->Event, delayed_event_ack->EventContext);
3606 list_del(&delayed_event_ack->list);
3607 kfree(delayed_event_ack);
3608 return 0;
3609 }
3610
3611 if (!list_empty(&ioc->delayed_sc_list)) {
3612 delayed_sc = list_entry(ioc->delayed_sc_list.next,
3613 struct _sc_list, list);
3614 _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3615 delayed_sc->handle);
3616 list_del(&delayed_sc->list);
3617 kfree(delayed_sc);
3618 return 0;
3619 }
3620 return 1;
3621}
f92363d1
SR
3622
3623/**
3624 * _scsih_check_for_pending_tm - check for pending task management
3625 * @ioc: per adapter object
3626 * @smid: system request message index
3627 *
3628 * This will check delayed target reset list, and feed the
3629 * next reqeust.
3630 *
3631 * Return 1 meaning mf should be freed from _base_interrupt
3632 * 0 means the mf is freed from this function.
3633 */
3634static u8
3635_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3636{
3637 struct _tr_list *delayed_tr;
3638
3639 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3640 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3641 struct _tr_list, list);
3642 mpt3sas_base_free_smid(ioc, smid);
3643 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3644 list_del(&delayed_tr->list);
3645 kfree(delayed_tr);
3646 return 0;
3647 }
3648
3649 if (!list_empty(&ioc->delayed_tr_list)) {
3650 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3651 struct _tr_list, list);
3652 mpt3sas_base_free_smid(ioc, smid);
3653 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3654 list_del(&delayed_tr->list);
3655 kfree(delayed_tr);
3656 return 0;
3657 }
3658
3659 return 1;
3660}
3661
3662/**
3663 * _scsih_check_topo_delete_events - sanity check on topo events
3664 * @ioc: per adapter object
3665 * @event_data: the event data payload
3666 *
3667 * This routine added to better handle cable breaker.
3668 *
3669 * This handles the case where driver receives multiple expander
3670 * add and delete events in a single shot. When there is a delete event
3671 * the routine will void any pending add events waiting in the event queue.
3672 *
3673 * Return nothing.
3674 */
3675static void
3676_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3677 Mpi2EventDataSasTopologyChangeList_t *event_data)
3678{
3679 struct fw_event_work *fw_event;
3680 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3681 u16 expander_handle;
3682 struct _sas_node *sas_expander;
3683 unsigned long flags;
3684 int i, reason_code;
3685 u16 handle;
3686
3687 for (i = 0 ; i < event_data->NumEntries; i++) {
3688 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3689 if (!handle)
3690 continue;
3691 reason_code = event_data->PHY[i].PhyStatus &
3692 MPI2_EVENT_SAS_TOPO_RC_MASK;
3693 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3694 _scsih_tm_tr_send(ioc, handle);
3695 }
3696
3697 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3698 if (expander_handle < ioc->sas_hba.num_phys) {
3699 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3700 return;
3701 }
3702 if (event_data->ExpStatus ==
3703 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3704 /* put expander attached devices into blocking state */
3705 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3706 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3707 expander_handle);
3708 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3709 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3710 do {
3711 handle = find_first_bit(ioc->blocking_handles,
3712 ioc->facts.MaxDevHandle);
3713 if (handle < ioc->facts.MaxDevHandle)
3714 _scsih_block_io_device(ioc, handle);
3715 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3716 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3717 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3718
3719 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3720 return;
3721
3722 /* mark ignore flag for pending events */
3723 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3724 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3725 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3726 fw_event->ignore)
3727 continue;
35b62362
JL
3728 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3729 fw_event->event_data;
f92363d1
SR
3730 if (local_event_data->ExpStatus ==
3731 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3732 local_event_data->ExpStatus ==
3733 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3734 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3735 expander_handle) {
3736 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3737 "setting ignoring flag\n", ioc->name));
3738 fw_event->ignore = 1;
3739 }
3740 }
3741 }
3742 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3743}
3744
3745/**
3746 * _scsih_set_volume_delete_flag - setting volume delete flag
3747 * @ioc: per adapter object
3748 * @handle: device handle
3749 *
3750 * This returns nothing.
3751 */
3752static void
3753_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3754{
3755 struct _raid_device *raid_device;
3756 struct MPT3SAS_TARGET *sas_target_priv_data;
3757 unsigned long flags;
3758
3759 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 3760 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
3761 if (raid_device && raid_device->starget &&
3762 raid_device->starget->hostdata) {
3763 sas_target_priv_data =
3764 raid_device->starget->hostdata;
3765 sas_target_priv_data->deleted = 1;
3766 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3767 "setting delete flag: handle(0x%04x), "
3768 "wwid(0x%016llx)\n", ioc->name, handle,
3769 (unsigned long long) raid_device->wwid));
3770 }
3771 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3772}
3773
3774/**
3775 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3776 * @handle: input handle
3777 * @a: handle for volume a
3778 * @b: handle for volume b
3779 *
3780 * IR firmware only supports two raid volumes. The purpose of this
3781 * routine is to set the volume handle in either a or b. When the given
3782 * input handle is non-zero, or when a and b have not been set before.
3783 */
3784static void
3785_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3786{
3787 if (!handle || handle == *a || handle == *b)
3788 return;
3789 if (!*a)
3790 *a = handle;
3791 else if (!*b)
3792 *b = handle;
3793}
3794
3795/**
3796 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3797 * @ioc: per adapter object
3798 * @event_data: the event data payload
3799 * Context: interrupt time.
3800 *
3801 * This routine will send target reset to volume, followed by target
3802 * resets to the PDs. This is called when a PD has been removed, or
3803 * volume has been deleted or removed. When the target reset is sent
3804 * to volume, the PD target resets need to be queued to start upon
3805 * completion of the volume target reset.
3806 *
3807 * Return nothing.
3808 */
3809static void
3810_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3811 Mpi2EventDataIrConfigChangeList_t *event_data)
3812{
3813 Mpi2EventIrConfigElement_t *element;
3814 int i;
3815 u16 handle, volume_handle, a, b;
3816 struct _tr_list *delayed_tr;
3817
3818 a = 0;
3819 b = 0;
3820
7786ab6a
SR
3821 if (ioc->is_warpdrive)
3822 return;
3823
f92363d1
SR
3824 /* Volume Resets for Deleted or Removed */
3825 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3826 for (i = 0; i < event_data->NumElements; i++, element++) {
3827 if (le32_to_cpu(event_data->Flags) &
3828 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3829 continue;
3830 if (element->ReasonCode ==
3831 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3832 element->ReasonCode ==
3833 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3834 volume_handle = le16_to_cpu(element->VolDevHandle);
3835 _scsih_set_volume_delete_flag(ioc, volume_handle);
3836 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3837 }
3838 }
3839
3840 /* Volume Resets for UNHIDE events */
3841 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3842 for (i = 0; i < event_data->NumElements; i++, element++) {
3843 if (le32_to_cpu(event_data->Flags) &
3844 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3845 continue;
3846 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3847 volume_handle = le16_to_cpu(element->VolDevHandle);
3848 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3849 }
3850 }
3851
3852 if (a)
3853 _scsih_tm_tr_volume_send(ioc, a);
3854 if (b)
3855 _scsih_tm_tr_volume_send(ioc, b);
3856
3857 /* PD target resets */
3858 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3859 for (i = 0; i < event_data->NumElements; i++, element++) {
3860 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3861 continue;
3862 handle = le16_to_cpu(element->PhysDiskDevHandle);
3863 volume_handle = le16_to_cpu(element->VolDevHandle);
3864 clear_bit(handle, ioc->pd_handles);
3865 if (!volume_handle)
3866 _scsih_tm_tr_send(ioc, handle);
3867 else if (volume_handle == a || volume_handle == b) {
3868 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3869 BUG_ON(!delayed_tr);
3870 INIT_LIST_HEAD(&delayed_tr->list);
3871 delayed_tr->handle = handle;
3872 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3873 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3874 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3875 handle));
3876 } else
3877 _scsih_tm_tr_send(ioc, handle);
3878 }
3879}
3880
3881
3882/**
3883 * _scsih_check_volume_delete_events - set delete flag for volumes
3884 * @ioc: per adapter object
3885 * @event_data: the event data payload
3886 * Context: interrupt time.
3887 *
3888 * This will handle the case when the cable connected to entire volume is
3889 * pulled. We will take care of setting the deleted flag so normal IO will
3890 * not be sent.
3891 *
3892 * Return nothing.
3893 */
3894static void
3895_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3896 Mpi2EventDataIrVolume_t *event_data)
3897{
3898 u32 state;
3899
3900 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3901 return;
3902 state = le32_to_cpu(event_data->NewValue);
3903 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3904 MPI2_RAID_VOL_STATE_FAILED)
3905 _scsih_set_volume_delete_flag(ioc,
3906 le16_to_cpu(event_data->VolDevHandle));
3907}
3908
2d8ce8c9
SR
3909/**
3910 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3911 * @ioc: per adapter object
3912 * @event_data: the temp threshold event data
3913 * Context: interrupt time.
3914 *
3915 * Return nothing.
3916 */
3917static void
3918_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3919 Mpi2EventDataTemperature_t *event_data)
3920{
3921 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3922 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3923 " exceeded for Sensor: %d !!!\n", ioc->name,
3924 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3925 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3926 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3927 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3928 event_data->SensorNum);
3929 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3930 ioc->name, event_data->CurrentTemperature);
3931 }
3932}
3933
ffb58456 3934static int _scsih_set_satl_pending(struct scsi_cmnd *scmd, bool pending)
7ff723ad 3935{
ffb58456
JB
3936 struct MPT3SAS_DEVICE *priv = scmd->device->hostdata;
3937
3938 if (scmd->cmnd[0] != ATA_12 && scmd->cmnd[0] != ATA_16)
3939 return 0;
3940
3941 if (pending)
3942 return test_and_set_bit(0, &priv->ata_command_pending);
3943
3944 clear_bit(0, &priv->ata_command_pending);
3945 return 0;
7ff723ad
SP
3946}
3947
f92363d1
SR
3948/**
3949 * _scsih_flush_running_cmds - completing outstanding commands.
3950 * @ioc: per adapter object
3951 *
3952 * The flushing out of all pending scmd commands following host reset,
3953 * where all IO is dropped to the floor.
3954 *
3955 * Return nothing.
3956 */
3957static void
3958_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3959{
3960 struct scsi_cmnd *scmd;
3961 u16 smid;
3962 u16 count = 0;
3963
3964 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3965 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3966 if (!scmd)
3967 continue;
3968 count++;
ffb58456 3969 _scsih_set_satl_pending(scmd, false);
f92363d1
SR
3970 mpt3sas_base_free_smid(ioc, smid);
3971 scsi_dma_unmap(scmd);
3972 if (ioc->pci_error_recovery)
3973 scmd->result = DID_NO_CONNECT << 16;
3974 else
3975 scmd->result = DID_RESET << 16;
3976 scmd->scsi_done(scmd);
3977 }
3978 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3979 ioc->name, count));
3980}
3981
3982/**
3983 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3984 * @ioc: per adapter object
3985 * @scmd: pointer to scsi command object
6c7abffc 3986 * @mpi_request: pointer to the SCSI_IO request message frame
f92363d1
SR
3987 *
3988 * Supporting protection 1 and 3.
3989 *
3990 * Returns nothing
3991 */
3992static void
3993_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3994 Mpi2SCSIIORequest_t *mpi_request)
3995{
3996 u16 eedp_flags;
3997 unsigned char prot_op = scsi_get_prot_op(scmd);
3998 unsigned char prot_type = scsi_get_prot_type(scmd);
3999 Mpi25SCSIIORequest_t *mpi_request_3v =
4000 (Mpi25SCSIIORequest_t *)mpi_request;
4001
4002 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
4003 return;
4004
4005 if (prot_op == SCSI_PROT_READ_STRIP)
4006 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
4007 else if (prot_op == SCSI_PROT_WRITE_INSERT)
4008 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
4009 else
4010 return;
4011
4012 switch (prot_type) {
4013 case SCSI_PROT_DIF_TYPE1:
4014 case SCSI_PROT_DIF_TYPE2:
4015
4016 /*
4017 * enable ref/guard checking
4018 * auto increment ref tag
4019 */
4020 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
4021 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
4022 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
4023 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
648512cc 4024 cpu_to_be32(scsi_prot_ref_tag(scmd));
f92363d1
SR
4025 break;
4026
4027 case SCSI_PROT_DIF_TYPE3:
4028
4029 /*
4030 * enable guard checking
4031 */
4032 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
4033
4034 break;
4035 }
4036
4037 mpi_request_3v->EEDPBlockSize =
4038 cpu_to_le16(scmd->device->sector_size);
186a18e5
SPS
4039
4040 if (ioc->is_gen35_ioc)
4041 eedp_flags |= MPI25_SCSIIO_EEDPFLAGS_APPTAG_DISABLE_MODE;
f92363d1
SR
4042 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
4043}
4044
4045/**
4046 * _scsih_eedp_error_handling - return sense code for EEDP errors
4047 * @scmd: pointer to scsi command object
4048 * @ioc_status: ioc status
4049 *
4050 * Returns nothing
4051 */
4052static void
4053_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
4054{
4055 u8 ascq;
4056
4057 switch (ioc_status) {
4058 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4059 ascq = 0x01;
4060 break;
4061 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4062 ascq = 0x02;
4063 break;
4064 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4065 ascq = 0x03;
4066 break;
4067 default:
4068 ascq = 0x00;
4069 break;
4070 }
4071 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4072 ascq);
4073 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4074 SAM_STAT_CHECK_CONDITION;
4075}
4076
f92363d1 4077/**
8a7e4c24 4078 * scsih_qcmd - main scsi request entry point
f92363d1
SR
4079 * @scmd: pointer to scsi command object
4080 * @done: function pointer to be invoked on completion
4081 *
4082 * The callback index is set inside `ioc->scsi_io_cb_idx`.
4083 *
4084 * Returns 0 on success. If there's a failure, return either:
4085 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4086 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4087 */
8bbb1cf6 4088static int
8a7e4c24 4089scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
f92363d1 4090{
d8bfbd8d 4091 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
f92363d1
SR
4092 struct MPT3SAS_DEVICE *sas_device_priv_data;
4093 struct MPT3SAS_TARGET *sas_target_priv_data;
7786ab6a 4094 struct _raid_device *raid_device;
307d9075
AM
4095 struct request *rq = scmd->request;
4096 int class;
f92363d1
SR
4097 Mpi2SCSIIORequest_t *mpi_request;
4098 u32 mpi_control;
4099 u16 smid;
4100 u16 handle;
4101
f92363d1
SR
4102 if (ioc->logging_level & MPT_DEBUG_SCSI)
4103 scsi_print_command(scmd);
f92363d1 4104
f92363d1
SR
4105 sas_device_priv_data = scmd->device->hostdata;
4106 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4107 scmd->result = DID_NO_CONNECT << 16;
4108 scmd->scsi_done(scmd);
4109 return 0;
4110 }
4111
4112 if (ioc->pci_error_recovery || ioc->remove_host) {
4113 scmd->result = DID_NO_CONNECT << 16;
4114 scmd->scsi_done(scmd);
4115 return 0;
4116 }
4117
ffb58456
JB
4118 /*
4119 * Bug work around for firmware SATL handling. The loop
4120 * is based on atomic operations and ensures consistency
4121 * since we're lockless at this point
4122 */
4123 do {
4124 if (test_bit(0, &sas_device_priv_data->ata_command_pending)) {
4125 scmd->result = SAM_STAT_BUSY;
4126 scmd->scsi_done(scmd);
4127 return 0;
4128 }
4129 } while (_scsih_set_satl_pending(scmd, true));
4130
f92363d1
SR
4131 sas_target_priv_data = sas_device_priv_data->sas_target;
4132
4133 /* invalid device handle */
4134 handle = sas_target_priv_data->handle;
4135 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4136 scmd->result = DID_NO_CONNECT << 16;
4137 scmd->scsi_done(scmd);
4138 return 0;
4139 }
4140
4141
4142 /* host recovery or link resets sent via IOCTLs */
4143 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4144 return SCSI_MLQUEUE_HOST_BUSY;
4145
4146 /* device has been deleted */
4147 else if (sas_target_priv_data->deleted) {
4148 scmd->result = DID_NO_CONNECT << 16;
4149 scmd->scsi_done(scmd);
4150 return 0;
6c7abffc 4151 /* device busy with task management */
f92363d1
SR
4152 } else if (sas_target_priv_data->tm_busy ||
4153 sas_device_priv_data->block)
4154 return SCSI_MLQUEUE_DEVICE_BUSY;
4155
4156 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4157 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4158 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4159 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4160 else
4161 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4162
4163 /* set tags */
609aa22f 4164 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
307d9075
AM
4165 /* NCQ Prio supported, make sure control indicated high priority */
4166 if (sas_device_priv_data->ncq_prio_enable) {
4167 class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
4168 if (class == IOPRIO_CLASS_RT)
4169 mpi_control |= 1 << MPI2_SCSIIO_CONTROL_CMDPRI_SHIFT;
4170 }
7786ab6a
SR
4171 /* Make sure Device is not raid volume.
4172 * We do not expose raid functionality to upper layer for warpdrive.
4173 */
4174 if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
15de0de2 4175 && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
f92363d1
SR
4176 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4177
4178 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4179 if (!smid) {
4180 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4181 ioc->name, __func__);
4182 goto out;
4183 }
4184 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4185 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4186 _scsih_setup_eedp(ioc, scmd, mpi_request);
4187
4188 if (scmd->cmd_len == 32)
4189 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4190 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4191 if (sas_device_priv_data->sas_target->flags &
4192 MPT_TARGET_FLAGS_RAID_COMPONENT)
4193 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4194 else
4195 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4196 mpi_request->DevHandle = cpu_to_le16(handle);
4197 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4198 mpi_request->Control = cpu_to_le32(mpi_control);
4199 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4200 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4201 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4202 mpi_request->SenseBufferLowAddress =
4203 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4204 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4205 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4206 mpi_request->LUN);
4207 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4208
4209 if (mpi_request->DataLength) {
4210 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4211 mpt3sas_base_free_smid(ioc, smid);
4212 goto out;
4213 }
4214 } else
4215 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4216
7786ab6a
SR
4217 raid_device = sas_target_priv_data->raid_device;
4218 if (raid_device && raid_device->direct_io_enabled)
c84b06a4 4219 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
7786ab6a 4220 smid);
7786ab6a 4221
f92363d1
SR
4222 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4223 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4224 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4225 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
81c16f83 4226 ioc->put_smid_fast_path(ioc, smid, handle);
f92363d1 4227 } else
81c16f83 4228 ioc->put_smid_scsi_io(ioc, smid,
7786ab6a 4229 le16_to_cpu(mpi_request->DevHandle));
f92363d1 4230 } else
81c16f83 4231 ioc->put_smid_default(ioc, smid);
f92363d1
SR
4232 return 0;
4233
4234 out:
4235 return SCSI_MLQUEUE_HOST_BUSY;
4236}
f92363d1
SR
4237
4238/**
4239 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4240 * @sense_buffer: sense data returned by target
4241 * @data: normalized skey/asc/ascq
4242 *
4243 * Return nothing.
4244 */
4245static void
4246_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4247{
4248 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4249 /* descriptor format */
4250 data->skey = sense_buffer[1] & 0x0F;
4251 data->asc = sense_buffer[2];
4252 data->ascq = sense_buffer[3];
4253 } else {
4254 /* fixed format */
4255 data->skey = sense_buffer[2] & 0x0F;
4256 data->asc = sense_buffer[12];
4257 data->ascq = sense_buffer[13];
4258 }
4259}
4260
f92363d1
SR
4261/**
4262 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4263 * @ioc: per adapter object
4264 * @scmd: pointer to scsi command object
4265 * @mpi_reply: reply mf payload returned from firmware
4266 *
4267 * scsi_status - SCSI Status code returned from target device
4268 * scsi_state - state info associated with SCSI_IO determined by ioc
4269 * ioc_status - ioc supplied status info
4270 *
4271 * Return nothing.
4272 */
4273static void
4274_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4275 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4276{
4277 u32 response_info;
4278 u8 *response_bytes;
4279 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4280 MPI2_IOCSTATUS_MASK;
4281 u8 scsi_state = mpi_reply->SCSIState;
4282 u8 scsi_status = mpi_reply->SCSIStatus;
4283 char *desc_ioc_state = NULL;
4284 char *desc_scsi_status = NULL;
4285 char *desc_scsi_state = ioc->tmp_string;
4286 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4287 struct _sas_device *sas_device = NULL;
f92363d1
SR
4288 struct scsi_target *starget = scmd->device->sdev_target;
4289 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4290 char *device_str = NULL;
4291
4292 if (!priv_target)
4293 return;
7786ab6a
SR
4294 if (ioc->hide_ir_msg)
4295 device_str = "WarpDrive";
4296 else
4297 device_str = "volume";
f92363d1
SR
4298
4299 if (log_info == 0x31170000)
4300 return;
4301
4302 switch (ioc_status) {
4303 case MPI2_IOCSTATUS_SUCCESS:
4304 desc_ioc_state = "success";
4305 break;
4306 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4307 desc_ioc_state = "invalid function";
4308 break;
4309 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4310 desc_ioc_state = "scsi recovered error";
4311 break;
4312 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4313 desc_ioc_state = "scsi invalid dev handle";
4314 break;
4315 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4316 desc_ioc_state = "scsi device not there";
4317 break;
4318 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4319 desc_ioc_state = "scsi data overrun";
4320 break;
4321 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4322 desc_ioc_state = "scsi data underrun";
4323 break;
4324 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4325 desc_ioc_state = "scsi io data error";
4326 break;
4327 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4328 desc_ioc_state = "scsi protocol error";
4329 break;
4330 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4331 desc_ioc_state = "scsi task terminated";
4332 break;
4333 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4334 desc_ioc_state = "scsi residual mismatch";
4335 break;
4336 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4337 desc_ioc_state = "scsi task mgmt failed";
4338 break;
4339 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4340 desc_ioc_state = "scsi ioc terminated";
4341 break;
4342 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4343 desc_ioc_state = "scsi ext terminated";
4344 break;
4345 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4346 desc_ioc_state = "eedp guard error";
4347 break;
4348 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4349 desc_ioc_state = "eedp ref tag error";
4350 break;
4351 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4352 desc_ioc_state = "eedp app tag error";
4353 break;
b130b0d5
SS
4354 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4355 desc_ioc_state = "insufficient power";
4356 break;
f92363d1
SR
4357 default:
4358 desc_ioc_state = "unknown";
4359 break;
4360 }
4361
4362 switch (scsi_status) {
4363 case MPI2_SCSI_STATUS_GOOD:
4364 desc_scsi_status = "good";
4365 break;
4366 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4367 desc_scsi_status = "check condition";
4368 break;
4369 case MPI2_SCSI_STATUS_CONDITION_MET:
4370 desc_scsi_status = "condition met";
4371 break;
4372 case MPI2_SCSI_STATUS_BUSY:
4373 desc_scsi_status = "busy";
4374 break;
4375 case MPI2_SCSI_STATUS_INTERMEDIATE:
4376 desc_scsi_status = "intermediate";
4377 break;
4378 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4379 desc_scsi_status = "intermediate condmet";
4380 break;
4381 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4382 desc_scsi_status = "reservation conflict";
4383 break;
4384 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4385 desc_scsi_status = "command terminated";
4386 break;
4387 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4388 desc_scsi_status = "task set full";
4389 break;
4390 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4391 desc_scsi_status = "aca active";
4392 break;
4393 case MPI2_SCSI_STATUS_TASK_ABORTED:
4394 desc_scsi_status = "task aborted";
4395 break;
4396 default:
4397 desc_scsi_status = "unknown";
4398 break;
4399 }
4400
4401 desc_scsi_state[0] = '\0';
4402 if (!scsi_state)
4403 desc_scsi_state = " ";
4404 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4405 strcat(desc_scsi_state, "response info ");
4406 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4407 strcat(desc_scsi_state, "state terminated ");
4408 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4409 strcat(desc_scsi_state, "no status ");
4410 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4411 strcat(desc_scsi_state, "autosense failed ");
4412 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4413 strcat(desc_scsi_state, "autosense valid ");
4414
4415 scsi_print_command(scmd);
4416
4417 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4418 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4419 device_str, (unsigned long long)priv_target->sas_address);
4420 } else {
d1cb5e49 4421 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
f92363d1
SR
4422 if (sas_device) {
4423 pr_warn(MPT3SAS_FMT
4424 "\tsas_address(0x%016llx), phy(%d)\n",
4425 ioc->name, (unsigned long long)
4426 sas_device->sas_address, sas_device->phy);
75888956
SR
4427
4428 _scsih_display_enclosure_chassis_info(ioc, sas_device,
4429 NULL, NULL);
d1cb5e49
SR
4430
4431 sas_device_put(sas_device);
f92363d1 4432 }
f92363d1
SR
4433 }
4434
4435 pr_warn(MPT3SAS_FMT
4436 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4437 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4438 desc_ioc_state, ioc_status, smid);
4439 pr_warn(MPT3SAS_FMT
4440 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4441 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4442 scsi_get_resid(scmd));
4443 pr_warn(MPT3SAS_FMT
4444 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4445 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4446 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4447 pr_warn(MPT3SAS_FMT
4448 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4449 ioc->name, desc_scsi_status,
4450 scsi_status, desc_scsi_state, scsi_state);
4451
4452 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4453 struct sense_info data;
4454 _scsih_normalize_sense(scmd->sense_buffer, &data);
4455 pr_warn(MPT3SAS_FMT
4456 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4457 ioc->name, data.skey,
4458 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4459 }
4460
4461 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4462 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4463 response_bytes = (u8 *)&response_info;
4464 _scsih_response_code(ioc, response_bytes[0]);
4465 }
4466}
f92363d1
SR
4467
4468/**
0f624c39 4469 * _scsih_turn_on_pfa_led - illuminate PFA LED
f92363d1
SR
4470 * @ioc: per adapter object
4471 * @handle: device handle
4472 * Context: process
4473 *
4474 * Return nothing.
4475 */
4476static void
0f624c39 4477_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
4478{
4479 Mpi2SepReply_t mpi_reply;
4480 Mpi2SepRequest_t mpi_request;
0f624c39
SR
4481 struct _sas_device *sas_device;
4482
d1cb5e49 4483 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
0f624c39
SR
4484 if (!sas_device)
4485 return;
f92363d1
SR
4486
4487 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4488 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4489 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4490 mpi_request.SlotStatus =
4491 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4492 mpi_request.DevHandle = cpu_to_le16(handle);
4493 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4494 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4495 &mpi_request)) != 0) {
4496 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4497 __FILE__, __LINE__, __func__);
d1cb5e49 4498 goto out;
f92363d1 4499 }
0f624c39 4500 sas_device->pfa_led_on = 1;
f92363d1
SR
4501
4502 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4503 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4504 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4505 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4506 le32_to_cpu(mpi_reply.IOCLogInfo)));
d1cb5e49 4507 goto out;
f92363d1 4508 }
d1cb5e49
SR
4509out:
4510 sas_device_put(sas_device);
f92363d1 4511}
d1cb5e49 4512
0f624c39
SR
4513/**
4514 * _scsih_turn_off_pfa_led - turn off Fault LED
4515 * @ioc: per adapter object
4516 * @sas_device: sas device whose PFA LED has to turned off
4517 * Context: process
4518 *
4519 * Return nothing.
4520 */
4521static void
4522_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4523 struct _sas_device *sas_device)
4524{
4525 Mpi2SepReply_t mpi_reply;
4526 Mpi2SepRequest_t mpi_request;
f92363d1 4527
0f624c39
SR
4528 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4529 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4530 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4531 mpi_request.SlotStatus = 0;
4532 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4533 mpi_request.DevHandle = 0;
4534 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4535 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4536 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4537 &mpi_request)) != 0) {
4538 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4539 __FILE__, __LINE__, __func__);
4540 return;
4541 }
4542
4543 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4544 dewtprintk(ioc, printk(MPT3SAS_FMT
4545 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4546 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4547 le32_to_cpu(mpi_reply.IOCLogInfo)));
4548 return;
4549 }
4550}
d1cb5e49 4551
f92363d1 4552/**
0f624c39 4553 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
f92363d1
SR
4554 * @ioc: per adapter object
4555 * @handle: device handle
4556 * Context: interrupt.
4557 *
4558 * Return nothing.
4559 */
4560static void
0f624c39 4561_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
f92363d1
SR
4562{
4563 struct fw_event_work *fw_event;
4564
146b16c8 4565 fw_event = alloc_fw_event_work(0);
f92363d1
SR
4566 if (!fw_event)
4567 return;
0f624c39 4568 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
f92363d1
SR
4569 fw_event->device_handle = handle;
4570 fw_event->ioc = ioc;
4571 _scsih_fw_event_add(ioc, fw_event);
146b16c8 4572 fw_event_work_put(fw_event);
f92363d1
SR
4573}
4574
4575/**
4576 * _scsih_smart_predicted_fault - process smart errors
4577 * @ioc: per adapter object
4578 * @handle: device handle
4579 * Context: interrupt.
4580 *
4581 * Return nothing.
4582 */
4583static void
4584_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4585{
4586 struct scsi_target *starget;
4587 struct MPT3SAS_TARGET *sas_target_priv_data;
4588 Mpi2EventNotificationReply_t *event_reply;
4589 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4590 struct _sas_device *sas_device;
4591 ssize_t sz;
4592 unsigned long flags;
4593
4594 /* only handle non-raid devices */
4595 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49
SR
4596 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4597 if (!sas_device)
4598 goto out_unlock;
4599
f92363d1
SR
4600 starget = sas_device->starget;
4601 sas_target_priv_data = starget->hostdata;
4602
4603 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
d1cb5e49
SR
4604 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4605 goto out_unlock;
4606
75888956
SR
4607 _scsih_display_enclosure_chassis_info(NULL, sas_device, NULL, starget);
4608
f92363d1
SR
4609 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4610
4611 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
0f624c39 4612 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
f92363d1
SR
4613
4614 /* insert into event log */
4615 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4616 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4617 event_reply = kzalloc(sz, GFP_KERNEL);
4618 if (!event_reply) {
4619 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4620 ioc->name, __FILE__, __LINE__, __func__);
d1cb5e49 4621 goto out;
f92363d1
SR
4622 }
4623
4624 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4625 event_reply->Event =
4626 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4627 event_reply->MsgLength = sz/4;
4628 event_reply->EventDataLength =
4629 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4630 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4631 event_reply->EventData;
4632 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4633 event_data->ASC = 0x5D;
4634 event_data->DevHandle = cpu_to_le16(handle);
4635 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4636 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4637 kfree(event_reply);
d1cb5e49
SR
4638out:
4639 if (sas_device)
4640 sas_device_put(sas_device);
4641 return;
4642
4643out_unlock:
4644 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4645 goto out;
f92363d1
SR
4646}
4647
4648/**
4649 * _scsih_io_done - scsi request callback
4650 * @ioc: per adapter object
4651 * @smid: system request message index
4652 * @msix_index: MSIX table index supplied by the OS
4653 * @reply: reply message frame(lower 32bit addr)
4654 *
4655 * Callback handler when using _scsih_qcmd.
4656 *
4657 * Return 1 meaning mf should be freed from _base_interrupt
4658 * 0 means the mf is freed from this function.
4659 */
4660static u8
4661_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4662{
4663 Mpi2SCSIIORequest_t *mpi_request;
4664 Mpi2SCSIIOReply_t *mpi_reply;
4665 struct scsi_cmnd *scmd;
4666 u16 ioc_status;
4667 u32 xfer_cnt;
4668 u8 scsi_state;
4669 u8 scsi_status;
4670 u32 log_info;
4671 struct MPT3SAS_DEVICE *sas_device_priv_data;
4672 u32 response_code = 0;
7786ab6a 4673 unsigned long flags;
f92363d1
SR
4674
4675 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
459325c4
C
4676
4677 if (ioc->broadcast_aen_busy || ioc->pci_error_recovery ||
4678 ioc->got_task_abort_from_ioctl)
4679 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4680 else
4681 scmd = __scsih_scsi_lookup_get_clear(ioc, smid);
4682
f92363d1
SR
4683 if (scmd == NULL)
4684 return 1;
4685
ffb58456 4686 _scsih_set_satl_pending(scmd, false);
18f6084a 4687
f92363d1
SR
4688 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4689
4690 if (mpi_reply == NULL) {
4691 scmd->result = DID_OK << 16;
4692 goto out;
4693 }
4694
4695 sas_device_priv_data = scmd->device->hostdata;
4696 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4697 sas_device_priv_data->sas_target->deleted) {
4698 scmd->result = DID_NO_CONNECT << 16;
4699 goto out;
4700 }
4701 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4702
7786ab6a
SR
4703 /*
4704 * WARPDRIVE: If direct_io is set then it is directIO,
4705 * the failed direct I/O should be redirected to volume
4706 */
c84b06a4 4707 if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
7786ab6a
SR
4708 ((ioc_status & MPI2_IOCSTATUS_MASK)
4709 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4710 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4711 ioc->scsi_lookup[smid - 1].scmd = scmd;
4712 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
c84b06a4 4713 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
7786ab6a
SR
4714 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4715 mpi_request->DevHandle =
4716 cpu_to_le16(sas_device_priv_data->sas_target->handle);
81c16f83 4717 ioc->put_smid_scsi_io(ioc, smid,
7786ab6a
SR
4718 sas_device_priv_data->sas_target->handle);
4719 return 0;
4720 }
f92363d1
SR
4721 /* turning off TLR */
4722 scsi_state = mpi_reply->SCSIState;
4723 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4724 response_code =
4725 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4726 if (!sas_device_priv_data->tlr_snoop_check) {
4727 sas_device_priv_data->tlr_snoop_check++;
7786ab6a
SR
4728 if (!ioc->is_warpdrive &&
4729 !scsih_is_raid(&scmd->device->sdev_gendev) &&
c84b06a4
SR
4730 sas_is_tlr_enabled(scmd->device) &&
4731 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4732 sas_disable_tlr(scmd->device);
4733 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4734 }
f92363d1
SR
4735 }
4736
4737 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4738 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4739 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4740 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4741 else
4742 log_info = 0;
4743 ioc_status &= MPI2_IOCSTATUS_MASK;
4744 scsi_status = mpi_reply->SCSIStatus;
4745
4746 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4747 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4748 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4749 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4750 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4751 }
4752
4753 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4754 struct sense_info data;
4755 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4756 smid);
4757 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4758 le32_to_cpu(mpi_reply->SenseCount));
4759 memcpy(scmd->sense_buffer, sense_data, sz);
4760 _scsih_normalize_sense(scmd->sense_buffer, &data);
4761 /* failure prediction threshold exceeded */
4762 if (data.asc == 0x5D)
4763 _scsih_smart_predicted_fault(ioc,
4764 le16_to_cpu(mpi_reply->DevHandle));
4765 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
f92363d1 4766
0d667f72 4767 if ((ioc->logging_level & MPT_DEBUG_REPLY) &&
e6d45e3e
SR
4768 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4769 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4770 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4771 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
e6d45e3e 4772 }
f92363d1
SR
4773 switch (ioc_status) {
4774 case MPI2_IOCSTATUS_BUSY:
4775 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4776 scmd->result = SAM_STAT_BUSY;
4777 break;
4778
4779 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4780 scmd->result = DID_NO_CONNECT << 16;
4781 break;
4782
4783 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4784 if (sas_device_priv_data->block) {
4785 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4786 goto out;
4787 }
4788 if (log_info == 0x31110630) {
4789 if (scmd->retries > 2) {
4790 scmd->result = DID_NO_CONNECT << 16;
4791 scsi_device_set_state(scmd->device,
4792 SDEV_OFFLINE);
4793 } else {
4794 scmd->result = DID_SOFT_ERROR << 16;
4795 scmd->device->expecting_cc_ua = 1;
4796 }
4797 break;
3898f08e
SR
4798 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4799 scmd->result = DID_RESET << 16;
4800 break;
2ce9a364
SR
4801 } else if ((scmd->device->channel == RAID_CHANNEL) &&
4802 (scsi_state == (MPI2_SCSI_STATE_TERMINATED |
4803 MPI2_SCSI_STATE_NO_SCSI_STATUS))) {
4804 scmd->result = DID_RESET << 16;
4805 break;
f92363d1
SR
4806 }
4807 scmd->result = DID_SOFT_ERROR << 16;
4808 break;
4809 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4810 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4811 scmd->result = DID_RESET << 16;
4812 break;
4813
4814 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4815 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4816 scmd->result = DID_SOFT_ERROR << 16;
4817 else
4818 scmd->result = (DID_OK << 16) | scsi_status;
4819 break;
4820
4821 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4822 scmd->result = (DID_OK << 16) | scsi_status;
4823
4824 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4825 break;
4826
4827 if (xfer_cnt < scmd->underflow) {
4828 if (scsi_status == SAM_STAT_BUSY)
4829 scmd->result = SAM_STAT_BUSY;
4830 else
4831 scmd->result = DID_SOFT_ERROR << 16;
4832 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4833 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4834 scmd->result = DID_SOFT_ERROR << 16;
4835 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4836 scmd->result = DID_RESET << 16;
4837 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4838 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4839 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4840 scmd->result = (DRIVER_SENSE << 24) |
4841 SAM_STAT_CHECK_CONDITION;
4842 scmd->sense_buffer[0] = 0x70;
4843 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4844 scmd->sense_buffer[12] = 0x20;
4845 scmd->sense_buffer[13] = 0;
4846 }
4847 break;
4848
4849 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4850 scsi_set_resid(scmd, 0);
4851 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4852 case MPI2_IOCSTATUS_SUCCESS:
4853 scmd->result = (DID_OK << 16) | scsi_status;
4854 if (response_code ==
4855 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4856 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4857 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4858 scmd->result = DID_SOFT_ERROR << 16;
4859 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4860 scmd->result = DID_RESET << 16;
4861 break;
4862
4863 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4864 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4865 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4866 _scsih_eedp_error_handling(scmd, ioc_status);
4867 break;
4868
4869 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4870 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4871 case MPI2_IOCSTATUS_INVALID_SGL:
4872 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4873 case MPI2_IOCSTATUS_INVALID_FIELD:
4874 case MPI2_IOCSTATUS_INVALID_STATE:
4875 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4876 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
b130b0d5 4877 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
f92363d1
SR
4878 default:
4879 scmd->result = DID_SOFT_ERROR << 16;
4880 break;
4881
4882 }
4883
f92363d1
SR
4884 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4885 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
f92363d1
SR
4886
4887 out:
4888
4889 scsi_dma_unmap(scmd);
4890
4891 scmd->scsi_done(scmd);
4892 return 1;
4893}
4894
4895/**
4896 * _scsih_sas_host_refresh - refreshing sas host object contents
4897 * @ioc: per adapter object
4898 * Context: user
4899 *
4900 * During port enable, fw will send topology events for every device. Its
4901 * possible that the handles may change from the previous setting, so this
4902 * code keeping handles updating if changed.
4903 *
4904 * Return nothing.
4905 */
4906static void
4907_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4908{
4909 u16 sz;
4910 u16 ioc_status;
4911 int i;
4912 Mpi2ConfigReply_t mpi_reply;
4913 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4914 u16 attached_handle;
4915 u8 link_rate;
4916
4917 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4918 "updating handles for sas_host(0x%016llx)\n",
4919 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4920
4921 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4922 * sizeof(Mpi2SasIOUnit0PhyData_t));
4923 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4924 if (!sas_iounit_pg0) {
4925 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4926 ioc->name, __FILE__, __LINE__, __func__);
4927 return;
4928 }
4929
4930 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4931 sas_iounit_pg0, sz)) != 0)
4932 goto out;
4933 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4934 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4935 goto out;
4936 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4937 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4938 if (i == 0)
4939 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4940 PhyData[0].ControllerDevHandle);
4941 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4942 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4943 AttachedDevHandle);
4944 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4945 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4946 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4947 attached_handle, i, link_rate);
4948 }
4949 out:
4950 kfree(sas_iounit_pg0);
4951}
4952
4953/**
4954 * _scsih_sas_host_add - create sas host object
4955 * @ioc: per adapter object
4956 *
4957 * Creating host side data object, stored in ioc->sas_hba
4958 *
4959 * Return nothing.
4960 */
4961static void
4962_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4963{
4964 int i;
4965 Mpi2ConfigReply_t mpi_reply;
4966 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4967 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4968 Mpi2SasPhyPage0_t phy_pg0;
4969 Mpi2SasDevicePage0_t sas_device_pg0;
4970 Mpi2SasEnclosurePage0_t enclosure_pg0;
4971 u16 ioc_status;
4972 u16 sz;
4973 u8 device_missing_delay;
87aa95d4 4974 u8 num_phys;
f92363d1 4975
87aa95d4
JL
4976 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4977 if (!num_phys) {
f92363d1
SR
4978 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4979 ioc->name, __FILE__, __LINE__, __func__);
4980 return;
4981 }
87aa95d4
JL
4982 ioc->sas_hba.phy = kcalloc(num_phys,
4983 sizeof(struct _sas_phy), GFP_KERNEL);
4984 if (!ioc->sas_hba.phy) {
4985 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4986 ioc->name, __FILE__, __LINE__, __func__);
4987 goto out;
4988 }
4989 ioc->sas_hba.num_phys = num_phys;
f92363d1
SR
4990
4991 /* sas_iounit page 0 */
4992 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4993 sizeof(Mpi2SasIOUnit0PhyData_t));
4994 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4995 if (!sas_iounit_pg0) {
4996 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4997 ioc->name, __FILE__, __LINE__, __func__);
4998 return;
4999 }
5000 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
5001 sas_iounit_pg0, sz))) {
5002 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5003 ioc->name, __FILE__, __LINE__, __func__);
5004 goto out;
5005 }
5006 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5007 MPI2_IOCSTATUS_MASK;
5008 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5009 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5010 ioc->name, __FILE__, __LINE__, __func__);
5011 goto out;
5012 }
5013
5014 /* sas_iounit page 1 */
5015 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
5016 sizeof(Mpi2SasIOUnit1PhyData_t));
5017 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
5018 if (!sas_iounit_pg1) {
5019 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5020 ioc->name, __FILE__, __LINE__, __func__);
5021 goto out;
5022 }
5023 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
5024 sas_iounit_pg1, sz))) {
5025 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5026 ioc->name, __FILE__, __LINE__, __func__);
5027 goto out;
5028 }
5029 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5030 MPI2_IOCSTATUS_MASK;
5031 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5032 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5033 ioc->name, __FILE__, __LINE__, __func__);
5034 goto out;
5035 }
5036
5037 ioc->io_missing_delay =
5038 sas_iounit_pg1->IODeviceMissingDelay;
5039 device_missing_delay =
5040 sas_iounit_pg1->ReportDeviceMissingDelay;
5041 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
5042 ioc->device_missing_delay = (device_missing_delay &
5043 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
5044 else
5045 ioc->device_missing_delay = device_missing_delay &
5046 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
5047
5048 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
f92363d1
SR
5049 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
5050 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
5051 i))) {
5052 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5053 ioc->name, __FILE__, __LINE__, __func__);
5054 goto out;
5055 }
5056 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5057 MPI2_IOCSTATUS_MASK;
5058 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5059 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5060 ioc->name, __FILE__, __LINE__, __func__);
5061 goto out;
5062 }
5063
5064 if (i == 0)
5065 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
5066 PhyData[0].ControllerDevHandle);
5067 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
5068 ioc->sas_hba.phy[i].phy_id = i;
5069 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
5070 phy_pg0, ioc->sas_hba.parent_dev);
5071 }
5072 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5073 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
5074 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5075 ioc->name, __FILE__, __LINE__, __func__);
5076 goto out;
5077 }
5078 ioc->sas_hba.enclosure_handle =
5079 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5080 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5081 pr_info(MPT3SAS_FMT
5082 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
5083 ioc->name, ioc->sas_hba.handle,
5084 (unsigned long long) ioc->sas_hba.sas_address,
5085 ioc->sas_hba.num_phys) ;
5086
5087 if (ioc->sas_hba.enclosure_handle) {
5088 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5089 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5090 ioc->sas_hba.enclosure_handle)))
5091 ioc->sas_hba.enclosure_logical_id =
5092 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5093 }
5094
5095 out:
5096 kfree(sas_iounit_pg1);
5097 kfree(sas_iounit_pg0);
5098}
5099
5100/**
5101 * _scsih_expander_add - creating expander object
5102 * @ioc: per adapter object
5103 * @handle: expander handle
5104 *
5105 * Creating expander object, stored in ioc->sas_expander_list.
5106 *
5107 * Return 0 for success, else error.
5108 */
5109static int
5110_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5111{
5112 struct _sas_node *sas_expander;
5113 Mpi2ConfigReply_t mpi_reply;
5114 Mpi2ExpanderPage0_t expander_pg0;
5115 Mpi2ExpanderPage1_t expander_pg1;
5116 Mpi2SasEnclosurePage0_t enclosure_pg0;
5117 u32 ioc_status;
5118 u16 parent_handle;
5119 u64 sas_address, sas_address_parent = 0;
5120 int i;
5121 unsigned long flags;
5122 struct _sas_port *mpt3sas_port = NULL;
5123
5124 int rc = 0;
5125
5126 if (!handle)
5127 return -1;
5128
5129 if (ioc->shost_recovery || ioc->pci_error_recovery)
5130 return -1;
5131
5132 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5133 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5134 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5135 ioc->name, __FILE__, __LINE__, __func__);
5136 return -1;
5137 }
5138
5139 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5140 MPI2_IOCSTATUS_MASK;
5141 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5142 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5143 ioc->name, __FILE__, __LINE__, __func__);
5144 return -1;
5145 }
5146
5147 /* handle out of order topology events */
5148 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5149 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5150 != 0) {
5151 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5152 ioc->name, __FILE__, __LINE__, __func__);
5153 return -1;
5154 }
5155 if (sas_address_parent != ioc->sas_hba.sas_address) {
5156 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5157 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5158 sas_address_parent);
5159 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5160 if (!sas_expander) {
5161 rc = _scsih_expander_add(ioc, parent_handle);
5162 if (rc != 0)
5163 return rc;
5164 }
5165 }
5166
5167 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5168 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5169 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5170 sas_address);
5171 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5172
5173 if (sas_expander)
5174 return 0;
5175
5176 sas_expander = kzalloc(sizeof(struct _sas_node),
5177 GFP_KERNEL);
5178 if (!sas_expander) {
5179 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5180 ioc->name, __FILE__, __LINE__, __func__);
5181 return -1;
5182 }
5183
5184 sas_expander->handle = handle;
5185 sas_expander->num_phys = expander_pg0.NumPhys;
5186 sas_expander->sas_address_parent = sas_address_parent;
5187 sas_expander->sas_address = sas_address;
5188
5189 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5190 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5191 handle, parent_handle, (unsigned long long)
5192 sas_expander->sas_address, sas_expander->num_phys);
5193
5194 if (!sas_expander->num_phys)
5195 goto out_fail;
5196 sas_expander->phy = kcalloc(sas_expander->num_phys,
5197 sizeof(struct _sas_phy), GFP_KERNEL);
5198 if (!sas_expander->phy) {
5199 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5200 ioc->name, __FILE__, __LINE__, __func__);
5201 rc = -1;
5202 goto out_fail;
5203 }
5204
5205 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5206 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5207 sas_address_parent);
5208 if (!mpt3sas_port) {
5209 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5210 ioc->name, __FILE__, __LINE__, __func__);
5211 rc = -1;
5212 goto out_fail;
5213 }
5214 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5215
5216 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5217 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5218 &expander_pg1, i, handle))) {
5219 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5220 ioc->name, __FILE__, __LINE__, __func__);
5221 rc = -1;
5222 goto out_fail;
5223 }
5224 sas_expander->phy[i].handle = handle;
5225 sas_expander->phy[i].phy_id = i;
5226
5227 if ((mpt3sas_transport_add_expander_phy(ioc,
5228 &sas_expander->phy[i], expander_pg1,
5229 sas_expander->parent_dev))) {
5230 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5231 ioc->name, __FILE__, __LINE__, __func__);
5232 rc = -1;
5233 goto out_fail;
5234 }
5235 }
5236
5237 if (sas_expander->enclosure_handle) {
5238 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5239 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5240 sas_expander->enclosure_handle)))
5241 sas_expander->enclosure_logical_id =
5242 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5243 }
5244
5245 _scsih_expander_node_add(ioc, sas_expander);
5246 return 0;
5247
5248 out_fail:
5249
5250 if (mpt3sas_port)
5251 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5252 sas_address_parent);
5253 kfree(sas_expander);
5254 return rc;
5255}
5256
5257/**
5258 * mpt3sas_expander_remove - removing expander object
5259 * @ioc: per adapter object
5260 * @sas_address: expander sas_address
5261 *
5262 * Return nothing.
5263 */
5264void
5265mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5266{
5267 struct _sas_node *sas_expander;
5268 unsigned long flags;
5269
5270 if (ioc->shost_recovery)
5271 return;
5272
5273 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5274 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5275 sas_address);
f92363d1
SR
5276 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5277 if (sas_expander)
5278 _scsih_expander_node_remove(ioc, sas_expander);
5279}
5280
5281/**
5282 * _scsih_done - internal SCSI_IO callback handler.
5283 * @ioc: per adapter object
5284 * @smid: system request message index
5285 * @msix_index: MSIX table index supplied by the OS
5286 * @reply: reply message frame(lower 32bit addr)
5287 *
5288 * Callback handler when sending internal generated SCSI_IO.
5289 * The callback index passed is `ioc->scsih_cb_idx`
5290 *
5291 * Return 1 meaning mf should be freed from _base_interrupt
5292 * 0 means the mf is freed from this function.
5293 */
5294static u8
5295_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5296{
5297 MPI2DefaultReply_t *mpi_reply;
5298
5299 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5300 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5301 return 1;
5302 if (ioc->scsih_cmds.smid != smid)
5303 return 1;
5304 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5305 if (mpi_reply) {
5306 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5307 mpi_reply->MsgLength*4);
5308 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5309 }
5310 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5311 complete(&ioc->scsih_cmds.done);
5312 return 1;
5313}
5314
5315
5316
5317
5318#define MPT3_MAX_LUNS (255)
5319
5320
5321/**
5322 * _scsih_check_access_status - check access flags
5323 * @ioc: per adapter object
5324 * @sas_address: sas address
5325 * @handle: sas device handle
5326 * @access_flags: errors returned during discovery of the device
5327 *
5328 * Return 0 for success, else failure
5329 */
5330static u8
5331_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5332 u16 handle, u8 access_status)
5333{
5334 u8 rc = 1;
5335 char *desc = NULL;
5336
5337 switch (access_status) {
5338 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5339 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5340 rc = 0;
5341 break;
5342 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5343 desc = "sata capability failed";
5344 break;
5345 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5346 desc = "sata affiliation conflict";
5347 break;
5348 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5349 desc = "route not addressable";
5350 break;
5351 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5352 desc = "smp error not addressable";
5353 break;
5354 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5355 desc = "device blocked";
5356 break;
5357 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5358 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5359 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5360 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5361 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5362 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5363 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5364 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5365 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5366 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5367 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5368 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5369 desc = "sata initialization failed";
5370 break;
5371 default:
5372 desc = "unknown";
5373 break;
5374 }
5375
5376 if (!rc)
5377 return 0;
5378
5379 pr_err(MPT3SAS_FMT
5380 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5381 ioc->name, desc, (unsigned long long)sas_address, handle);
5382 return rc;
5383}
5384
aba5a85c
SR
5385/**
5386 * _scsih_get_enclosure_logicalid_chassis_slot - get device's
5387 * EnclosureLogicalID and ChassisSlot information.
5388 * @ioc: per adapter object
5389 * @sas_device_pg0: SAS device page0
5390 * @sas_device: per sas device object
5391 *
5392 * Returns nothing.
5393 */
5394static void
5395_scsih_get_enclosure_logicalid_chassis_slot(struct MPT3SAS_ADAPTER *ioc,
5396 Mpi2SasDevicePage0_t *sas_device_pg0, struct _sas_device *sas_device)
5397{
5398 Mpi2ConfigReply_t mpi_reply;
5399 Mpi2SasEnclosurePage0_t enclosure_pg0;
5400
5401 if (!sas_device_pg0 || !sas_device)
5402 return;
5403
5404 sas_device->enclosure_handle =
5405 le16_to_cpu(sas_device_pg0->EnclosureHandle);
5406 sas_device->is_chassis_slot_valid = 0;
5407
5408 if (!le16_to_cpu(sas_device_pg0->EnclosureHandle))
5409 return;
5410
5411 if (mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5412 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5413 le16_to_cpu(sas_device_pg0->EnclosureHandle))) {
5414 pr_err(MPT3SAS_FMT
5415 "Enclosure Pg0 read failed for handle(0x%04x)\n",
5416 ioc->name, le16_to_cpu(sas_device_pg0->EnclosureHandle));
5417 return;
5418 }
5419
5420 sas_device->enclosure_logical_id =
5421 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5422
5423 if (le16_to_cpu(enclosure_pg0.Flags) &
5424 MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
5425 sas_device->is_chassis_slot_valid = 1;
5426 sas_device->chassis_slot = enclosure_pg0.ChassisSlot;
5427 }
5428}
5429
5430
f92363d1
SR
5431/**
5432 * _scsih_check_device - checking device responsiveness
5433 * @ioc: per adapter object
5434 * @parent_sas_address: sas address of parent expander or sas host
5435 * @handle: attached device handle
5436 * @phy_numberv: phy number
5437 * @link_rate: new link rate
5438 *
5439 * Returns nothing.
5440 */
5441static void
5442_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5443 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5444{
5445 Mpi2ConfigReply_t mpi_reply;
5446 Mpi2SasDevicePage0_t sas_device_pg0;
5447 struct _sas_device *sas_device;
5448 u32 ioc_status;
5449 unsigned long flags;
5450 u64 sas_address;
5451 struct scsi_target *starget;
5452 struct MPT3SAS_TARGET *sas_target_priv_data;
5453 u32 device_info;
5454
f92363d1
SR
5455 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5456 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5457 return;
5458
5459 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5460 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5461 return;
5462
5463 /* wide port handling ~ we need only handle device once for the phy that
5464 * is matched in sas device page zero
5465 */
5466 if (phy_number != sas_device_pg0.PhyNum)
5467 return;
5468
5469 /* check if this is end device */
5470 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5471 if (!(_scsih_is_end_device(device_info)))
5472 return;
5473
5474 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5475 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
d1cb5e49 5476 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
5477 sas_address);
5478
d1cb5e49
SR
5479 if (!sas_device)
5480 goto out_unlock;
f92363d1
SR
5481
5482 if (unlikely(sas_device->handle != handle)) {
5483 starget = sas_device->starget;
5484 sas_target_priv_data = starget->hostdata;
5485 starget_printk(KERN_INFO, starget,
5486 "handle changed from(0x%04x) to (0x%04x)!!!\n",
5487 sas_device->handle, handle);
5488 sas_target_priv_data->handle = handle;
5489 sas_device->handle = handle;
aa53bb89 5490 if (le16_to_cpu(sas_device_pg0.Flags) &
e6d45e3e
SR
5491 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5492 sas_device->enclosure_level =
aa53bb89 5493 sas_device_pg0.EnclosureLevel;
310c8e40
CO
5494 memcpy(sas_device->connector_name,
5495 sas_device_pg0.ConnectorName, 4);
5496 sas_device->connector_name[4] = '\0';
e6d45e3e
SR
5497 } else {
5498 sas_device->enclosure_level = 0;
5499 sas_device->connector_name[0] = '\0';
5500 }
aba5a85c
SR
5501
5502 _scsih_get_enclosure_logicalid_chassis_slot(ioc,
5503 &sas_device_pg0, sas_device);
f92363d1
SR
5504 }
5505
5506 /* check if device is present */
5507 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5508 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5509 pr_err(MPT3SAS_FMT
5510 "device is not present handle(0x%04x), flags!!!\n",
5511 ioc->name, handle);
d1cb5e49 5512 goto out_unlock;
f92363d1
SR
5513 }
5514
5515 /* check if there were any issues with discovery */
5516 if (_scsih_check_access_status(ioc, sas_address, handle,
d1cb5e49
SR
5517 sas_device_pg0.AccessStatus))
5518 goto out_unlock;
f92363d1
SR
5519
5520 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5521 _scsih_ublock_io_device(ioc, sas_address);
5522
d1cb5e49
SR
5523 if (sas_device)
5524 sas_device_put(sas_device);
5525 return;
5526
5527out_unlock:
5528 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5529 if (sas_device)
5530 sas_device_put(sas_device);
f92363d1
SR
5531}
5532
5533/**
5534 * _scsih_add_device - creating sas device object
5535 * @ioc: per adapter object
5536 * @handle: sas device handle
5537 * @phy_num: phy number end device attached to
5538 * @is_pd: is this hidden raid component
5539 *
5540 * Creating end device object, stored in ioc->sas_device_list.
5541 *
5542 * Returns 0 for success, non-zero for failure.
5543 */
5544static int
5545_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5546 u8 is_pd)
5547{
5548 Mpi2ConfigReply_t mpi_reply;
5549 Mpi2SasDevicePage0_t sas_device_pg0;
5550 Mpi2SasEnclosurePage0_t enclosure_pg0;
5551 struct _sas_device *sas_device;
5552 u32 ioc_status;
5553 u64 sas_address;
5554 u32 device_info;
75888956 5555 int encl_pg0_rc = -1;
f92363d1
SR
5556
5557 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5558 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5559 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5560 ioc->name, __FILE__, __LINE__, __func__);
5561 return -1;
5562 }
5563
5564 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5565 MPI2_IOCSTATUS_MASK;
5566 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5567 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5568 ioc->name, __FILE__, __LINE__, __func__);
5569 return -1;
5570 }
5571
5572 /* check if this is end device */
5573 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5574 if (!(_scsih_is_end_device(device_info)))
5575 return -1;
c696f7b8 5576 set_bit(handle, ioc->pend_os_device_add);
f92363d1
SR
5577 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5578
5579 /* check if device is present */
5580 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5581 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5582 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5583 ioc->name, handle);
5584 return -1;
5585 }
5586
5587 /* check if there were any issues with discovery */
5588 if (_scsih_check_access_status(ioc, sas_address, handle,
5589 sas_device_pg0.AccessStatus))
5590 return -1;
5591
d1cb5e49
SR
5592 sas_device = mpt3sas_get_sdev_by_addr(ioc,
5593 sas_address);
5594 if (sas_device) {
c696f7b8 5595 clear_bit(handle, ioc->pend_os_device_add);
d1cb5e49 5596 sas_device_put(sas_device);
f92363d1 5597 return -1;
d1cb5e49 5598 }
f92363d1 5599
75888956
SR
5600 if (sas_device_pg0.EnclosureHandle) {
5601 encl_pg0_rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5602 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5603 sas_device_pg0.EnclosureHandle);
5604 if (encl_pg0_rc)
5605 pr_info(MPT3SAS_FMT
5606 "Enclosure Pg0 read failed for handle(0x%04x)\n",
5607 ioc->name, sas_device_pg0.EnclosureHandle);
5608 }
5609
f92363d1
SR
5610 sas_device = kzalloc(sizeof(struct _sas_device),
5611 GFP_KERNEL);
5612 if (!sas_device) {
5613 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5614 ioc->name, __FILE__, __LINE__, __func__);
5615 return 0;
5616 }
5617
d1cb5e49 5618 kref_init(&sas_device->refcount);
f92363d1
SR
5619 sas_device->handle = handle;
5620 if (_scsih_get_sas_address(ioc,
5621 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5622 &sas_device->sas_address_parent) != 0)
5623 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5624 ioc->name, __FILE__, __LINE__, __func__);
5625 sas_device->enclosure_handle =
5626 le16_to_cpu(sas_device_pg0.EnclosureHandle);
e6d45e3e
SR
5627 if (sas_device->enclosure_handle != 0)
5628 sas_device->slot =
5629 le16_to_cpu(sas_device_pg0.Slot);
f92363d1
SR
5630 sas_device->device_info = device_info;
5631 sas_device->sas_address = sas_address;
5632 sas_device->phy = sas_device_pg0.PhyNum;
5633 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5634 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5635
aa53bb89
SPS
5636 if (le16_to_cpu(sas_device_pg0.Flags)
5637 & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
e6d45e3e 5638 sas_device->enclosure_level =
aa53bb89 5639 sas_device_pg0.EnclosureLevel;
310c8e40
CO
5640 memcpy(sas_device->connector_name,
5641 sas_device_pg0.ConnectorName, 4);
5642 sas_device->connector_name[4] = '\0';
e6d45e3e
SR
5643 } else {
5644 sas_device->enclosure_level = 0;
5645 sas_device->connector_name[0] = '\0';
5646 }
75888956
SR
5647
5648 /* get enclosure_logical_id & chassis_slot */
5649 sas_device->is_chassis_slot_valid = 0;
5650 if (encl_pg0_rc == 0) {
f92363d1
SR
5651 sas_device->enclosure_logical_id =
5652 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5653
75888956
SR
5654 if (le16_to_cpu(enclosure_pg0.Flags) &
5655 MPI2_SAS_ENCLS0_FLAGS_CHASSIS_SLOT_VALID) {
5656 sas_device->is_chassis_slot_valid = 1;
5657 sas_device->chassis_slot =
5658 enclosure_pg0.ChassisSlot;
5659 }
5660 }
5661
f92363d1
SR
5662 /* get device name */
5663 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5664
5665 if (ioc->wait_for_discovery_to_complete)
5666 _scsih_sas_device_init_add(ioc, sas_device);
5667 else
5668 _scsih_sas_device_add(ioc, sas_device);
5669
d1cb5e49 5670 sas_device_put(sas_device);
f92363d1
SR
5671 return 0;
5672}
5673
5674/**
5675 * _scsih_remove_device - removing sas device object
5676 * @ioc: per adapter object
5677 * @sas_device_delete: the sas_device object
5678 *
5679 * Return nothing.
5680 */
5681static void
5682_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5683 struct _sas_device *sas_device)
5684{
5685 struct MPT3SAS_TARGET *sas_target_priv_data;
5686
0f624c39
SR
5687 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5688 (sas_device->pfa_led_on)) {
5689 _scsih_turn_off_pfa_led(ioc, sas_device);
5690 sas_device->pfa_led_on = 0;
5691 }
75888956 5692
f92363d1
SR
5693 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5694 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5695 ioc->name, __func__,
5696 sas_device->handle, (unsigned long long)
5697 sas_device->sas_address));
75888956
SR
5698
5699 dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
5700 NULL, NULL));
f92363d1
SR
5701
5702 if (sas_device->starget && sas_device->starget->hostdata) {
5703 sas_target_priv_data = sas_device->starget->hostdata;
5704 sas_target_priv_data->deleted = 1;
5705 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5706 sas_target_priv_data->handle =
5707 MPT3SAS_INVALID_DEVICE_HANDLE;
5708 }
7786ab6a
SR
5709
5710 if (!ioc->hide_drives)
5711 mpt3sas_transport_port_remove(ioc,
f92363d1
SR
5712 sas_device->sas_address,
5713 sas_device->sas_address_parent);
5714
5715 pr_info(MPT3SAS_FMT
5716 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5717 ioc->name, sas_device->handle,
5718 (unsigned long long) sas_device->sas_address);
75888956
SR
5719
5720 _scsih_display_enclosure_chassis_info(ioc, sas_device, NULL, NULL);
f92363d1
SR
5721
5722 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5723 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5724 ioc->name, __func__,
e6d45e3e
SR
5725 sas_device->handle, (unsigned long long)
5726 sas_device->sas_address));
75888956
SR
5727 dewtprintk(ioc, _scsih_display_enclosure_chassis_info(ioc, sas_device,
5728 NULL, NULL));
f92363d1
SR
5729}
5730
f92363d1
SR
5731/**
5732 * _scsih_sas_topology_change_event_debug - debug for topology event
5733 * @ioc: per adapter object
5734 * @event_data: event data payload
5735 * Context: user.
5736 */
5737static void
5738_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5739 Mpi2EventDataSasTopologyChangeList_t *event_data)
5740{
5741 int i;
5742 u16 handle;
5743 u16 reason_code;
5744 u8 phy_number;
5745 char *status_str = NULL;
5746 u8 link_rate, prev_link_rate;
5747
5748 switch (event_data->ExpStatus) {
5749 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5750 status_str = "add";
5751 break;
5752 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5753 status_str = "remove";
5754 break;
5755 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5756 case 0:
5757 status_str = "responding";
5758 break;
5759 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5760 status_str = "remove delay";
5761 break;
5762 default:
5763 status_str = "unknown status";
5764 break;
5765 }
5766 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5767 ioc->name, status_str);
5768 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5769 "start_phy(%02d), count(%d)\n",
5770 le16_to_cpu(event_data->ExpanderDevHandle),
5771 le16_to_cpu(event_data->EnclosureHandle),
5772 event_data->StartPhyNum, event_data->NumEntries);
5773 for (i = 0; i < event_data->NumEntries; i++) {
5774 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5775 if (!handle)
5776 continue;
5777 phy_number = event_data->StartPhyNum + i;
5778 reason_code = event_data->PHY[i].PhyStatus &
5779 MPI2_EVENT_SAS_TOPO_RC_MASK;
5780 switch (reason_code) {
5781 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5782 status_str = "target add";
5783 break;
5784 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5785 status_str = "target remove";
5786 break;
5787 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5788 status_str = "delay target remove";
5789 break;
5790 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5791 status_str = "link rate change";
5792 break;
5793 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5794 status_str = "target responding";
5795 break;
5796 default:
5797 status_str = "unknown";
5798 break;
5799 }
5800 link_rate = event_data->PHY[i].LinkRate >> 4;
5801 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5802 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5803 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5804 handle, status_str, link_rate, prev_link_rate);
5805
5806 }
5807}
f92363d1
SR
5808
5809/**
5810 * _scsih_sas_topology_change_event - handle topology changes
5811 * @ioc: per adapter object
5812 * @fw_event: The fw_event_work object
5813 * Context: user.
5814 *
5815 */
5816static int
5817_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5818 struct fw_event_work *fw_event)
5819{
5820 int i;
5821 u16 parent_handle, handle;
5822 u16 reason_code;
5823 u8 phy_number, max_phys;
5824 struct _sas_node *sas_expander;
5825 u64 sas_address;
5826 unsigned long flags;
5827 u8 link_rate, prev_link_rate;
35b62362
JL
5828 Mpi2EventDataSasTopologyChangeList_t *event_data =
5829 (Mpi2EventDataSasTopologyChangeList_t *)
5830 fw_event->event_data;
f92363d1 5831
f92363d1
SR
5832 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5833 _scsih_sas_topology_change_event_debug(ioc, event_data);
f92363d1
SR
5834
5835 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5836 return 0;
5837
5838 if (!ioc->sas_hba.num_phys)
5839 _scsih_sas_host_add(ioc);
5840 else
5841 _scsih_sas_host_refresh(ioc);
5842
5843 if (fw_event->ignore) {
5844 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5845 "ignoring expander event\n", ioc->name));
5846 return 0;
5847 }
5848
5849 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5850
5851 /* handle expander add */
5852 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5853 if (_scsih_expander_add(ioc, parent_handle) != 0)
5854 return 0;
5855
5856 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5857 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5858 parent_handle);
5859 if (sas_expander) {
5860 sas_address = sas_expander->sas_address;
5861 max_phys = sas_expander->num_phys;
5862 } else if (parent_handle < ioc->sas_hba.num_phys) {
5863 sas_address = ioc->sas_hba.sas_address;
5864 max_phys = ioc->sas_hba.num_phys;
5865 } else {
5866 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5867 return 0;
5868 }
5869 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5870
5871 /* handle siblings events */
5872 for (i = 0; i < event_data->NumEntries; i++) {
5873 if (fw_event->ignore) {
5874 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5875 "ignoring expander event\n", ioc->name));
5876 return 0;
5877 }
5878 if (ioc->remove_host || ioc->pci_error_recovery)
5879 return 0;
5880 phy_number = event_data->StartPhyNum + i;
5881 if (phy_number >= max_phys)
5882 continue;
5883 reason_code = event_data->PHY[i].PhyStatus &
5884 MPI2_EVENT_SAS_TOPO_RC_MASK;
5885 if ((event_data->PHY[i].PhyStatus &
5886 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5887 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5888 continue;
5889 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5890 if (!handle)
5891 continue;
5892 link_rate = event_data->PHY[i].LinkRate >> 4;
5893 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5894 switch (reason_code) {
5895 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5896
5897 if (ioc->shost_recovery)
5898 break;
5899
5900 if (link_rate == prev_link_rate)
5901 break;
5902
5903 mpt3sas_transport_update_links(ioc, sas_address,
5904 handle, phy_number, link_rate);
5905
5906 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5907 break;
5908
5909 _scsih_check_device(ioc, sas_address, handle,
5910 phy_number, link_rate);
5911
c696f7b8
SPS
5912 if (!test_bit(handle, ioc->pend_os_device_add))
5913 break;
5914
f92363d1
SR
5915
5916 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5917
5918 if (ioc->shost_recovery)
5919 break;
5920
5921 mpt3sas_transport_update_links(ioc, sas_address,
5922 handle, phy_number, link_rate);
5923
5924 _scsih_add_device(ioc, handle, phy_number, 0);
5925
5926 break;
5927 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5928
5929 _scsih_device_remove_by_handle(ioc, handle);
5930 break;
5931 }
5932 }
5933
5934 /* handle expander removal */
5935 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5936 sas_expander)
5937 mpt3sas_expander_remove(ioc, sas_address);
5938
5939 return 0;
5940}
5941
f92363d1
SR
5942/**
5943 * _scsih_sas_device_status_change_event_debug - debug for device event
5944 * @event_data: event data payload
5945 * Context: user.
5946 *
5947 * Return nothing.
5948 */
5949static void
5950_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5951 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5952{
5953 char *reason_str = NULL;
5954
5955 switch (event_data->ReasonCode) {
5956 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5957 reason_str = "smart data";
5958 break;
5959 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5960 reason_str = "unsupported device discovered";
5961 break;
5962 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5963 reason_str = "internal device reset";
5964 break;
5965 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5966 reason_str = "internal task abort";
5967 break;
5968 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5969 reason_str = "internal task abort set";
5970 break;
5971 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5972 reason_str = "internal clear task set";
5973 break;
5974 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5975 reason_str = "internal query task";
5976 break;
5977 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5978 reason_str = "sata init failure";
5979 break;
5980 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5981 reason_str = "internal device reset complete";
5982 break;
5983 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5984 reason_str = "internal task abort complete";
5985 break;
5986 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5987 reason_str = "internal async notification";
5988 break;
5989 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5990 reason_str = "expander reduced functionality";
5991 break;
5992 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5993 reason_str = "expander reduced functionality complete";
5994 break;
5995 default:
5996 reason_str = "unknown reason";
5997 break;
5998 }
5999 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
6000 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
6001 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
6002 (unsigned long long)le64_to_cpu(event_data->SASAddress),
6003 le16_to_cpu(event_data->TaskTag));
6004 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
6005 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
6006 event_data->ASC, event_data->ASCQ);
6007 pr_info("\n");
6008}
f92363d1
SR
6009
6010/**
6011 * _scsih_sas_device_status_change_event - handle device status change
6012 * @ioc: per adapter object
6013 * @fw_event: The fw_event_work object
6014 * Context: user.
6015 *
6016 * Return nothing.
6017 */
6018static void
6019_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
6020 struct fw_event_work *fw_event)
6021{
6022 struct MPT3SAS_TARGET *target_priv_data;
6023 struct _sas_device *sas_device;
6024 u64 sas_address;
6025 unsigned long flags;
6026 Mpi2EventDataSasDeviceStatusChange_t *event_data =
35b62362
JL
6027 (Mpi2EventDataSasDeviceStatusChange_t *)
6028 fw_event->event_data;
f92363d1 6029
f92363d1
SR
6030 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6031 _scsih_sas_device_status_change_event_debug(ioc,
6032 event_data);
f92363d1
SR
6033
6034 /* In MPI Revision K (0xC), the internal device reset complete was
6035 * implemented, so avoid setting tm_busy flag for older firmware.
6036 */
6037 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
6038 return;
6039
6040 if (event_data->ReasonCode !=
6041 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
6042 event_data->ReasonCode !=
6043 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
6044 return;
6045
6046 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6047 sas_address = le64_to_cpu(event_data->SASAddress);
d1cb5e49 6048 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
f92363d1
SR
6049 sas_address);
6050
d1cb5e49
SR
6051 if (!sas_device || !sas_device->starget)
6052 goto out;
f92363d1
SR
6053
6054 target_priv_data = sas_device->starget->hostdata;
d1cb5e49
SR
6055 if (!target_priv_data)
6056 goto out;
f92363d1
SR
6057
6058 if (event_data->ReasonCode ==
6059 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
6060 target_priv_data->tm_busy = 1;
6061 else
6062 target_priv_data->tm_busy = 0;
d1cb5e49
SR
6063
6064out:
6065 if (sas_device)
6066 sas_device_put(sas_device);
6067
f92363d1 6068 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
d1cb5e49 6069
f92363d1
SR
6070}
6071
f92363d1
SR
6072/**
6073 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
6074 * event
6075 * @ioc: per adapter object
6076 * @event_data: event data payload
6077 * Context: user.
6078 *
6079 * Return nothing.
6080 */
6081static void
6082_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6083 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
6084{
6085 char *reason_str = NULL;
6086
6087 switch (event_data->ReasonCode) {
6088 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
6089 reason_str = "enclosure add";
6090 break;
6091 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
6092 reason_str = "enclosure remove";
6093 break;
6094 default:
6095 reason_str = "unknown reason";
6096 break;
6097 }
6098
6099 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
6100 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
6101 " number slots(%d)\n", ioc->name, reason_str,
6102 le16_to_cpu(event_data->EnclosureHandle),
6103 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
6104 le16_to_cpu(event_data->StartSlot));
6105}
f92363d1
SR
6106
6107/**
6108 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
6109 * @ioc: per adapter object
6110 * @fw_event: The fw_event_work object
6111 * Context: user.
6112 *
6113 * Return nothing.
6114 */
6115static void
6116_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
6117 struct fw_event_work *fw_event)
6118{
f92363d1
SR
6119 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6120 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
35b62362 6121 (Mpi2EventDataSasEnclDevStatusChange_t *)
f92363d1 6122 fw_event->event_data);
f92363d1
SR
6123}
6124
6125/**
6126 * _scsih_sas_broadcast_primitive_event - handle broadcast events
6127 * @ioc: per adapter object
6128 * @fw_event: The fw_event_work object
6129 * Context: user.
6130 *
6131 * Return nothing.
6132 */
6133static void
6134_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6135 struct fw_event_work *fw_event)
6136{
6137 struct scsi_cmnd *scmd;
6138 struct scsi_device *sdev;
6139 u16 smid, handle;
6140 u32 lun;
6141 struct MPT3SAS_DEVICE *sas_device_priv_data;
6142 u32 termination_count;
6143 u32 query_count;
6144 Mpi2SCSITaskManagementReply_t *mpi_reply;
35b62362
JL
6145 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6146 (Mpi2EventDataSasBroadcastPrimitive_t *)
6147 fw_event->event_data;
f92363d1
SR
6148 u16 ioc_status;
6149 unsigned long flags;
6150 int r;
6151 u8 max_retries = 0;
6152 u8 task_abort_retries;
6153
6154 mutex_lock(&ioc->tm_cmds.mutex);
6155 pr_info(MPT3SAS_FMT
6156 "%s: enter: phy number(%d), width(%d)\n",
6157 ioc->name, __func__, event_data->PhyNum,
6158 event_data->PortWidth);
6159
6160 _scsih_block_io_all_device(ioc);
6161
6162 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6163 mpi_reply = ioc->tm_cmds.reply;
6164 broadcast_aen_retry:
6165
6166 /* sanity checks for retrying this loop */
6167 if (max_retries++ == 5) {
6168 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6169 ioc->name, __func__));
6170 goto out;
6171 } else if (max_retries > 1)
6172 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6173 ioc->name, __func__, max_retries - 1));
6174
6175 termination_count = 0;
6176 query_count = 0;
6177 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6178 if (ioc->shost_recovery)
6179 goto out;
6180 scmd = _scsih_scsi_lookup_get(ioc, smid);
6181 if (!scmd)
6182 continue;
6183 sdev = scmd->device;
6184 sas_device_priv_data = sdev->hostdata;
6185 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6186 continue;
6187 /* skip hidden raid components */
6188 if (sas_device_priv_data->sas_target->flags &
6189 MPT_TARGET_FLAGS_RAID_COMPONENT)
6190 continue;
6191 /* skip volumes */
6192 if (sas_device_priv_data->sas_target->flags &
6193 MPT_TARGET_FLAGS_VOLUME)
6194 continue;
6195
6196 handle = sas_device_priv_data->sas_target->handle;
6197 lun = sas_device_priv_data->lun;
6198 query_count++;
6199
6200 if (ioc->shost_recovery)
6201 goto out;
6202
6203 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6204 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
96902835 6205 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
f92363d1
SR
6206 if (r == FAILED) {
6207 sdev_printk(KERN_WARNING, sdev,
6208 "mpt3sas_scsih_issue_tm: FAILED when sending "
6209 "QUERY_TASK: scmd(%p)\n", scmd);
6210 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6211 goto broadcast_aen_retry;
6212 }
6213 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6214 & MPI2_IOCSTATUS_MASK;
6215 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6216 sdev_printk(KERN_WARNING, sdev,
6217 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6218 ioc_status, scmd);
6219 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6220 goto broadcast_aen_retry;
6221 }
6222
6223 /* see if IO is still owned by IOC and target */
6224 if (mpi_reply->ResponseCode ==
6225 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6226 mpi_reply->ResponseCode ==
6227 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6228 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6229 continue;
6230 }
6231 task_abort_retries = 0;
6232 tm_retry:
6233 if (task_abort_retries++ == 60) {
6234 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6235 "%s: ABORT_TASK: giving up\n", ioc->name,
6236 __func__));
6237 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6238 goto broadcast_aen_retry;
6239 }
6240
6241 if (ioc->shost_recovery)
6242 goto out_no_lock;
6243
6244 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
96902835
CO
6245 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
6246 30);
f92363d1
SR
6247 if (r == FAILED) {
6248 sdev_printk(KERN_WARNING, sdev,
6249 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6250 "scmd(%p)\n", scmd);
6251 goto tm_retry;
6252 }
6253
6254 if (task_abort_retries > 1)
6255 sdev_printk(KERN_WARNING, sdev,
6256 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6257 " scmd(%p)\n",
6258 task_abort_retries - 1, scmd);
6259
6260 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6261 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6262 }
6263
6264 if (ioc->broadcast_aen_pending) {
6265 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6266 "%s: loop back due to pending AEN\n",
6267 ioc->name, __func__));
6268 ioc->broadcast_aen_pending = 0;
6269 goto broadcast_aen_retry;
6270 }
6271
6272 out:
6273 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6274 out_no_lock:
6275
6276 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6277 "%s - exit, query_count = %d termination_count = %d\n",
6278 ioc->name, __func__, query_count, termination_count));
6279
6280 ioc->broadcast_aen_busy = 0;
6281 if (!ioc->shost_recovery)
6282 _scsih_ublock_io_all_device(ioc);
6283 mutex_unlock(&ioc->tm_cmds.mutex);
6284}
6285
6286/**
6287 * _scsih_sas_discovery_event - handle discovery events
6288 * @ioc: per adapter object
6289 * @fw_event: The fw_event_work object
6290 * Context: user.
6291 *
6292 * Return nothing.
6293 */
6294static void
6295_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6296 struct fw_event_work *fw_event)
6297{
35b62362
JL
6298 Mpi2EventDataSasDiscovery_t *event_data =
6299 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
f92363d1 6300
f92363d1
SR
6301 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6302 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6303 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6304 "start" : "stop");
af009411
SR
6305 if (event_data->DiscoveryStatus)
6306 pr_info("discovery_status(0x%08x)",
6307 le32_to_cpu(event_data->DiscoveryStatus));
6308 pr_info("\n");
f92363d1 6309 }
f92363d1
SR
6310
6311 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6312 !ioc->sas_hba.num_phys) {
6313 if (disable_discovery > 0 && ioc->shost_recovery) {
6314 /* Wait for the reset to complete */
6315 while (ioc->shost_recovery)
6316 ssleep(1);
6317 }
6318 _scsih_sas_host_add(ioc);
6319 }
6320}
6321
6322/**
6323 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6324 * @ioc: per adapter object
6325 * @handle: device handle for physical disk
6326 * @phys_disk_num: physical disk number
6327 *
6328 * Return 0 for success, else failure.
6329 */
6330static int
6331_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6332{
6333 Mpi2RaidActionRequest_t *mpi_request;
6334 Mpi2RaidActionReply_t *mpi_reply;
6335 u16 smid;
6336 u8 issue_reset = 0;
6337 int rc = 0;
6338 u16 ioc_status;
6339 u32 log_info;
6340
c84b06a4
SR
6341 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6342 return rc;
6343
f92363d1
SR
6344 mutex_lock(&ioc->scsih_cmds.mutex);
6345
6346 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6347 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6348 ioc->name, __func__);
6349 rc = -EAGAIN;
6350 goto out;
6351 }
6352 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6353
6354 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6355 if (!smid) {
6356 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6357 ioc->name, __func__);
6358 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6359 rc = -EAGAIN;
6360 goto out;
6361 }
6362
6363 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6364 ioc->scsih_cmds.smid = smid;
6365 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6366
6367 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6368 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6369 mpi_request->PhysDiskNum = phys_disk_num;
6370
6371 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6372 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6373 handle, phys_disk_num));
6374
6375 init_completion(&ioc->scsih_cmds.done);
81c16f83 6376 ioc->put_smid_default(ioc, smid);
f92363d1
SR
6377 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6378
6379 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6380 pr_err(MPT3SAS_FMT "%s: timeout\n",
6381 ioc->name, __func__);
6382 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6383 issue_reset = 1;
6384 rc = -EFAULT;
6385 goto out;
6386 }
6387
6388 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6389
6390 mpi_reply = ioc->scsih_cmds.reply;
6391 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6392 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6393 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
6394 else
6395 log_info = 0;
6396 ioc_status &= MPI2_IOCSTATUS_MASK;
6397 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6398 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6399 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6400 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6401 log_info));
6402 rc = -EFAULT;
6403 } else
6404 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6405 "IR RAID_ACTION: completed successfully\n",
6406 ioc->name));
6407 }
6408
6409 out:
6410 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6411 mutex_unlock(&ioc->scsih_cmds.mutex);
6412
6413 if (issue_reset)
98c56ad3 6414 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
6415 return rc;
6416}
6417
6418/**
6419 * _scsih_reprobe_lun - reprobing lun
6420 * @sdev: scsi device struct
6421 * @no_uld_attach: sdev->no_uld_attach flag setting
6422 *
6423 **/
6424static void
6425_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6426{
f92363d1
SR
6427 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6428 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6c7abffc 6429 sdev->no_uld_attach ? "hiding" : "exposing");
8bbb1cf6 6430 WARN_ON(scsi_device_reprobe(sdev));
f92363d1
SR
6431}
6432
6433/**
6434 * _scsih_sas_volume_add - add new volume
6435 * @ioc: per adapter object
6436 * @element: IR config element data
6437 * Context: user.
6438 *
6439 * Return nothing.
6440 */
6441static void
6442_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6443 Mpi2EventIrConfigElement_t *element)
6444{
6445 struct _raid_device *raid_device;
6446 unsigned long flags;
6447 u64 wwid;
6448 u16 handle = le16_to_cpu(element->VolDevHandle);
6449 int rc;
6450
6451 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6452 if (!wwid) {
6453 pr_err(MPT3SAS_FMT
6454 "failure at %s:%d/%s()!\n", ioc->name,
6455 __FILE__, __LINE__, __func__);
6456 return;
6457 }
6458
6459 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6460 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6461 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6462
6463 if (raid_device)
6464 return;
6465
6466 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6467 if (!raid_device) {
6468 pr_err(MPT3SAS_FMT
6469 "failure at %s:%d/%s()!\n", ioc->name,
6470 __FILE__, __LINE__, __func__);
6471 return;
6472 }
6473
6474 raid_device->id = ioc->sas_id++;
6475 raid_device->channel = RAID_CHANNEL;
6476 raid_device->handle = handle;
6477 raid_device->wwid = wwid;
6478 _scsih_raid_device_add(ioc, raid_device);
6479 if (!ioc->wait_for_discovery_to_complete) {
6480 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6481 raid_device->id, 0);
6482 if (rc)
6483 _scsih_raid_device_remove(ioc, raid_device);
6484 } else {
6485 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6486 _scsih_determine_boot_device(ioc, raid_device, 1);
6487 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6488 }
6489}
6490
6491/**
6492 * _scsih_sas_volume_delete - delete volume
6493 * @ioc: per adapter object
6494 * @handle: volume device handle
6495 * Context: user.
6496 *
6497 * Return nothing.
6498 */
6499static void
6500_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6501{
6502 struct _raid_device *raid_device;
6503 unsigned long flags;
6504 struct MPT3SAS_TARGET *sas_target_priv_data;
6505 struct scsi_target *starget = NULL;
6506
6507 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 6508 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
6509 if (raid_device) {
6510 if (raid_device->starget) {
6511 starget = raid_device->starget;
6512 sas_target_priv_data = starget->hostdata;
6513 sas_target_priv_data->deleted = 1;
6514 }
6515 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6516 ioc->name, raid_device->handle,
6517 (unsigned long long) raid_device->wwid);
6518 list_del(&raid_device->list);
6519 kfree(raid_device);
6520 }
6521 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6522 if (starget)
6523 scsi_remove_target(&starget->dev);
6524}
6525
6526/**
6527 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6528 * @ioc: per adapter object
6529 * @element: IR config element data
6530 * Context: user.
6531 *
6532 * Return nothing.
6533 */
6534static void
6535_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6536 Mpi2EventIrConfigElement_t *element)
6537{
6538 struct _sas_device *sas_device;
6539 struct scsi_target *starget = NULL;
6540 struct MPT3SAS_TARGET *sas_target_priv_data;
6541 unsigned long flags;
6542 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6543
6544 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 6545 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
6546 if (sas_device) {
6547 sas_device->volume_handle = 0;
6548 sas_device->volume_wwid = 0;
6549 clear_bit(handle, ioc->pd_handles);
6550 if (sas_device->starget && sas_device->starget->hostdata) {
6551 starget = sas_device->starget;
6552 sas_target_priv_data = starget->hostdata;
6553 sas_target_priv_data->flags &=
6554 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6555 }
6556 }
6557 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6558 if (!sas_device)
6559 return;
6560
6561 /* exposing raid component */
6562 if (starget)
6563 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
d1cb5e49
SR
6564
6565 sas_device_put(sas_device);
f92363d1
SR
6566}
6567
6568/**
6569 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6570 * @ioc: per adapter object
6571 * @element: IR config element data
6572 * Context: user.
6573 *
6574 * Return nothing.
6575 */
6576static void
6577_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6578 Mpi2EventIrConfigElement_t *element)
6579{
6580 struct _sas_device *sas_device;
6581 struct scsi_target *starget = NULL;
6582 struct MPT3SAS_TARGET *sas_target_priv_data;
6583 unsigned long flags;
6584 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6585 u16 volume_handle = 0;
6586 u64 volume_wwid = 0;
6587
6588 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6589 if (volume_handle)
6590 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6591 &volume_wwid);
6592
6593 spin_lock_irqsave(&ioc->sas_device_lock, flags);
d1cb5e49 6594 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
6595 if (sas_device) {
6596 set_bit(handle, ioc->pd_handles);
6597 if (sas_device->starget && sas_device->starget->hostdata) {
6598 starget = sas_device->starget;
6599 sas_target_priv_data = starget->hostdata;
6600 sas_target_priv_data->flags |=
6601 MPT_TARGET_FLAGS_RAID_COMPONENT;
6602 sas_device->volume_handle = volume_handle;
6603 sas_device->volume_wwid = volume_wwid;
6604 }
6605 }
6606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6607 if (!sas_device)
6608 return;
6609
6610 /* hiding raid component */
6611 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
c84b06a4 6612
f92363d1
SR
6613 if (starget)
6614 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
d1cb5e49
SR
6615
6616 sas_device_put(sas_device);
f92363d1
SR
6617}
6618
6619/**
6620 * _scsih_sas_pd_delete - delete pd component
6621 * @ioc: per adapter object
6622 * @element: IR config element data
6623 * Context: user.
6624 *
6625 * Return nothing.
6626 */
6627static void
6628_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6629 Mpi2EventIrConfigElement_t *element)
6630{
6631 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6632
6633 _scsih_device_remove_by_handle(ioc, handle);
6634}
6635
6636/**
6637 * _scsih_sas_pd_add - remove pd component
6638 * @ioc: per adapter object
6639 * @element: IR config element data
6640 * Context: user.
6641 *
6642 * Return nothing.
6643 */
6644static void
6645_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6646 Mpi2EventIrConfigElement_t *element)
6647{
6648 struct _sas_device *sas_device;
f92363d1
SR
6649 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6650 Mpi2ConfigReply_t mpi_reply;
6651 Mpi2SasDevicePage0_t sas_device_pg0;
6652 u32 ioc_status;
6653 u64 sas_address;
6654 u16 parent_handle;
6655
6656 set_bit(handle, ioc->pd_handles);
6657
d1cb5e49 6658 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
f92363d1
SR
6659 if (sas_device) {
6660 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
d1cb5e49 6661 sas_device_put(sas_device);
f92363d1
SR
6662 return;
6663 }
6664
6665 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6666 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6667 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6668 ioc->name, __FILE__, __LINE__, __func__);
6669 return;
6670 }
6671
6672 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6673 MPI2_IOCSTATUS_MASK;
6674 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6675 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6676 ioc->name, __FILE__, __LINE__, __func__);
6677 return;
6678 }
6679
6680 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6681 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6682 mpt3sas_transport_update_links(ioc, sas_address, handle,
6683 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6684
6685 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6686 _scsih_add_device(ioc, handle, 0, 1);
6687}
6688
f92363d1
SR
6689/**
6690 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6691 * @ioc: per adapter object
6692 * @event_data: event data payload
6693 * Context: user.
6694 *
6695 * Return nothing.
6696 */
6697static void
6698_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6699 Mpi2EventDataIrConfigChangeList_t *event_data)
6700{
6701 Mpi2EventIrConfigElement_t *element;
6702 u8 element_type;
6703 int i;
6704 char *reason_str = NULL, *element_str = NULL;
6705
6706 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6707
6708 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6709 ioc->name, (le32_to_cpu(event_data->Flags) &
6710 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6711 "foreign" : "native", event_data->NumElements);
6712 for (i = 0; i < event_data->NumElements; i++, element++) {
6713 switch (element->ReasonCode) {
6714 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6715 reason_str = "add";
6716 break;
6717 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6718 reason_str = "remove";
6719 break;
6720 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6721 reason_str = "no change";
6722 break;
6723 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6724 reason_str = "hide";
6725 break;
6726 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6727 reason_str = "unhide";
6728 break;
6729 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6730 reason_str = "volume_created";
6731 break;
6732 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6733 reason_str = "volume_deleted";
6734 break;
6735 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6736 reason_str = "pd_created";
6737 break;
6738 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6739 reason_str = "pd_deleted";
6740 break;
6741 default:
6742 reason_str = "unknown reason";
6743 break;
6744 }
6745 element_type = le16_to_cpu(element->ElementFlags) &
6746 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6747 switch (element_type) {
6748 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6749 element_str = "volume";
6750 break;
6751 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6752 element_str = "phys disk";
6753 break;
6754 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6755 element_str = "hot spare";
6756 break;
6757 default:
6758 element_str = "unknown element";
6759 break;
6760 }
6761 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6762 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6763 reason_str, le16_to_cpu(element->VolDevHandle),
6764 le16_to_cpu(element->PhysDiskDevHandle),
6765 element->PhysDiskNum);
6766 }
6767}
f92363d1
SR
6768
6769/**
6770 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6771 * @ioc: per adapter object
6772 * @fw_event: The fw_event_work object
6773 * Context: user.
6774 *
6775 * Return nothing.
6776 */
6777static void
6778_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6779 struct fw_event_work *fw_event)
6780{
6781 Mpi2EventIrConfigElement_t *element;
6782 int i;
6783 u8 foreign_config;
35b62362
JL
6784 Mpi2EventDataIrConfigChangeList_t *event_data =
6785 (Mpi2EventDataIrConfigChangeList_t *)
6786 fw_event->event_data;
f92363d1 6787
7786ab6a
SR
6788 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6789 (!ioc->hide_ir_msg))
f92363d1
SR
6790 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6791
f92363d1
SR
6792 foreign_config = (le32_to_cpu(event_data->Flags) &
6793 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6794
6795 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
c84b06a4
SR
6796 if (ioc->shost_recovery &&
6797 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
f92363d1
SR
6798 for (i = 0; i < event_data->NumElements; i++, element++) {
6799 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6800 _scsih_ir_fastpath(ioc,
6801 le16_to_cpu(element->PhysDiskDevHandle),
6802 element->PhysDiskNum);
6803 }
6804 return;
6805 }
7786ab6a 6806
f92363d1
SR
6807 for (i = 0; i < event_data->NumElements; i++, element++) {
6808
6809 switch (element->ReasonCode) {
6810 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6811 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6812 if (!foreign_config)
6813 _scsih_sas_volume_add(ioc, element);
6814 break;
6815 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6816 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6817 if (!foreign_config)
6818 _scsih_sas_volume_delete(ioc,
6819 le16_to_cpu(element->VolDevHandle));
6820 break;
6821 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
7786ab6a
SR
6822 if (!ioc->is_warpdrive)
6823 _scsih_sas_pd_hide(ioc, element);
f92363d1
SR
6824 break;
6825 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
7786ab6a
SR
6826 if (!ioc->is_warpdrive)
6827 _scsih_sas_pd_expose(ioc, element);
f92363d1
SR
6828 break;
6829 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
7786ab6a
SR
6830 if (!ioc->is_warpdrive)
6831 _scsih_sas_pd_add(ioc, element);
f92363d1
SR
6832 break;
6833 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
7786ab6a
SR
6834 if (!ioc->is_warpdrive)
6835 _scsih_sas_pd_delete(ioc, element);
f92363d1
SR
6836 break;
6837 }
6838 }
6839}
6840
6841/**
6842 * _scsih_sas_ir_volume_event - IR volume event
6843 * @ioc: per adapter object
6844 * @fw_event: The fw_event_work object
6845 * Context: user.
6846 *
6847 * Return nothing.
6848 */
6849static void
6850_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6851 struct fw_event_work *fw_event)
6852{
6853 u64 wwid;
6854 unsigned long flags;
6855 struct _raid_device *raid_device;
6856 u16 handle;
6857 u32 state;
6858 int rc;
35b62362
JL
6859 Mpi2EventDataIrVolume_t *event_data =
6860 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
f92363d1
SR
6861
6862 if (ioc->shost_recovery)
6863 return;
6864
6865 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6866 return;
6867
6868 handle = le16_to_cpu(event_data->VolDevHandle);
6869 state = le32_to_cpu(event_data->NewValue);
7786ab6a
SR
6870 if (!ioc->hide_ir_msg)
6871 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6872 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6873 ioc->name, __func__, handle,
6874 le32_to_cpu(event_data->PreviousValue), state));
f92363d1
SR
6875 switch (state) {
6876 case MPI2_RAID_VOL_STATE_MISSING:
6877 case MPI2_RAID_VOL_STATE_FAILED:
6878 _scsih_sas_volume_delete(ioc, handle);
6879 break;
6880
6881 case MPI2_RAID_VOL_STATE_ONLINE:
6882 case MPI2_RAID_VOL_STATE_DEGRADED:
6883 case MPI2_RAID_VOL_STATE_OPTIMAL:
6884
6885 spin_lock_irqsave(&ioc->raid_device_lock, flags);
c84b06a4 6886 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
6887 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6888
6889 if (raid_device)
6890 break;
6891
6892 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6893 if (!wwid) {
6894 pr_err(MPT3SAS_FMT
6895 "failure at %s:%d/%s()!\n", ioc->name,
6896 __FILE__, __LINE__, __func__);
6897 break;
6898 }
6899
6900 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6901 if (!raid_device) {
6902 pr_err(MPT3SAS_FMT
6903 "failure at %s:%d/%s()!\n", ioc->name,
6904 __FILE__, __LINE__, __func__);
6905 break;
6906 }
6907
6908 raid_device->id = ioc->sas_id++;
6909 raid_device->channel = RAID_CHANNEL;
6910 raid_device->handle = handle;
6911 raid_device->wwid = wwid;
6912 _scsih_raid_device_add(ioc, raid_device);
6913 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6914 raid_device->id, 0);
6915 if (rc)
6916 _scsih_raid_device_remove(ioc, raid_device);
6917 break;
6918
6919 case MPI2_RAID_VOL_STATE_INITIALIZING:
6920 default:
6921 break;
6922 }
6923}
6924
6925/**
6926 * _scsih_sas_ir_physical_disk_event - PD event
6927 * @ioc: per adapter object
6928 * @fw_event: The fw_event_work object
6929 * Context: user.
6930 *
6931 * Return nothing.
6932 */
6933static void
6934_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6935 struct fw_event_work *fw_event)
6936{
6937 u16 handle, parent_handle;
6938 u32 state;
6939 struct _sas_device *sas_device;
f92363d1
SR
6940 Mpi2ConfigReply_t mpi_reply;
6941 Mpi2SasDevicePage0_t sas_device_pg0;
6942 u32 ioc_status;
35b62362
JL
6943 Mpi2EventDataIrPhysicalDisk_t *event_data =
6944 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
f92363d1
SR
6945 u64 sas_address;
6946
6947 if (ioc->shost_recovery)
6948 return;
6949
6950 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6951 return;
6952
6953 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6954 state = le32_to_cpu(event_data->NewValue);
6955
7786ab6a
SR
6956 if (!ioc->hide_ir_msg)
6957 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6958 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6959 ioc->name, __func__, handle,
f92363d1 6960 le32_to_cpu(event_data->PreviousValue), state));
7786ab6a 6961
f92363d1
SR
6962 switch (state) {
6963 case MPI2_RAID_PD_STATE_ONLINE:
6964 case MPI2_RAID_PD_STATE_DEGRADED:
6965 case MPI2_RAID_PD_STATE_REBUILDING:
6966 case MPI2_RAID_PD_STATE_OPTIMAL:
6967 case MPI2_RAID_PD_STATE_HOT_SPARE:
6968
7786ab6a
SR
6969 if (!ioc->is_warpdrive)
6970 set_bit(handle, ioc->pd_handles);
f92363d1 6971
d1cb5e49
SR
6972 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6973 if (sas_device) {
6974 sas_device_put(sas_device);
f92363d1 6975 return;
d1cb5e49 6976 }
f92363d1
SR
6977
6978 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6979 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6980 handle))) {
6981 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6982 ioc->name, __FILE__, __LINE__, __func__);
6983 return;
6984 }
6985
6986 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6987 MPI2_IOCSTATUS_MASK;
6988 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6989 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6990 ioc->name, __FILE__, __LINE__, __func__);
6991 return;
6992 }
6993
6994 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6995 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6996 mpt3sas_transport_update_links(ioc, sas_address, handle,
6997 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6998
6999 _scsih_add_device(ioc, handle, 0, 1);
7000
7001 break;
7002
7003 case MPI2_RAID_PD_STATE_OFFLINE:
7004 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
7005 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
7006 default:
7007 break;
7008 }
7009}
7010
f92363d1
SR
7011/**
7012 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
7013 * @ioc: per adapter object
7014 * @event_data: event data payload
7015 * Context: user.
7016 *
7017 * Return nothing.
7018 */
7019static void
7020_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
7021 Mpi2EventDataIrOperationStatus_t *event_data)
7022{
7023 char *reason_str = NULL;
7024
7025 switch (event_data->RAIDOperation) {
7026 case MPI2_EVENT_IR_RAIDOP_RESYNC:
7027 reason_str = "resync";
7028 break;
7029 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
7030 reason_str = "online capacity expansion";
7031 break;
7032 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
7033 reason_str = "consistency check";
7034 break;
7035 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
7036 reason_str = "background init";
7037 break;
7038 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
7039 reason_str = "make data consistent";
7040 break;
7041 }
7042
7043 if (!reason_str)
7044 return;
7045
7046 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
7047 "\thandle(0x%04x), percent complete(%d)\n",
7048 ioc->name, reason_str,
7049 le16_to_cpu(event_data->VolDevHandle),
7050 event_data->PercentComplete);
7051}
f92363d1
SR
7052
7053/**
7054 * _scsih_sas_ir_operation_status_event - handle RAID operation events
7055 * @ioc: per adapter object
7056 * @fw_event: The fw_event_work object
7057 * Context: user.
7058 *
7059 * Return nothing.
7060 */
7061static void
7062_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
7063 struct fw_event_work *fw_event)
7064{
35b62362
JL
7065 Mpi2EventDataIrOperationStatus_t *event_data =
7066 (Mpi2EventDataIrOperationStatus_t *)
7067 fw_event->event_data;
f92363d1
SR
7068 static struct _raid_device *raid_device;
7069 unsigned long flags;
7070 u16 handle;
7071
7786ab6a
SR
7072 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
7073 (!ioc->hide_ir_msg))
f92363d1
SR
7074 _scsih_sas_ir_operation_status_event_debug(ioc,
7075 event_data);
f92363d1
SR
7076
7077 /* code added for raid transport support */
7078 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
7079
7080 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7081 handle = le16_to_cpu(event_data->VolDevHandle);
c84b06a4 7082 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
f92363d1
SR
7083 if (raid_device)
7084 raid_device->percent_complete =
7085 event_data->PercentComplete;
7086 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7087 }
7088}
7089
7090/**
7091 * _scsih_prep_device_scan - initialize parameters prior to device scan
7092 * @ioc: per adapter object
7093 *
7094 * Set the deleted flag prior to device scan. If the device is found during
7095 * the scan, then we clear the deleted flag.
7096 */
7097static void
7098_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
7099{
7100 struct MPT3SAS_DEVICE *sas_device_priv_data;
7101 struct scsi_device *sdev;
7102
7103 shost_for_each_device(sdev, ioc->shost) {
7104 sas_device_priv_data = sdev->hostdata;
7105 if (sas_device_priv_data && sas_device_priv_data->sas_target)
7106 sas_device_priv_data->sas_target->deleted = 1;
7107 }
7108}
7109
7110/**
7111 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
7112 * @ioc: per adapter object
e6d45e3e 7113 * @sas_device_pg0: SAS Device page 0
f92363d1
SR
7114 *
7115 * After host reset, find out whether devices are still responding.
7116 * Used in _scsih_remove_unresponsive_sas_devices.
7117 *
7118 * Return nothing.
7119 */
7120static void
e6d45e3e
SR
7121_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
7122Mpi2SasDevicePage0_t *sas_device_pg0)
f92363d1
SR
7123{
7124 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7125 struct scsi_target *starget;
aba5a85c 7126 struct _sas_device *sas_device = NULL;
f92363d1
SR
7127 unsigned long flags;
7128
7129 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7130 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
e6d45e3e
SR
7131 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7132 (sas_device->slot == sas_device_pg0->Slot)) {
f92363d1
SR
7133 sas_device->responding = 1;
7134 starget = sas_device->starget;
7135 if (starget && starget->hostdata) {
7136 sas_target_priv_data = starget->hostdata;
7137 sas_target_priv_data->tm_busy = 0;
7138 sas_target_priv_data->deleted = 0;
7139 } else
7140 sas_target_priv_data = NULL;
e6d45e3e 7141 if (starget) {
f92363d1 7142 starget_printk(KERN_INFO, starget,
e6d45e3e
SR
7143 "handle(0x%04x), sas_addr(0x%016llx)\n",
7144 sas_device_pg0->DevHandle,
f92363d1 7145 (unsigned long long)
e6d45e3e
SR
7146 sas_device->sas_address);
7147
7148 if (sas_device->enclosure_handle != 0)
7149 starget_printk(KERN_INFO, starget,
7150 "enclosure logical id(0x%016llx),"
7151 " slot(%d)\n",
7152 (unsigned long long)
7153 sas_device->enclosure_logical_id,
7154 sas_device->slot);
7155 }
7156 if (sas_device_pg0->Flags &
7157 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7158 sas_device->enclosure_level =
aa53bb89 7159 sas_device_pg0->EnclosureLevel;
e6d45e3e
SR
7160 memcpy(&sas_device->connector_name[0],
7161 &sas_device_pg0->ConnectorName[0], 4);
7162 } else {
7163 sas_device->enclosure_level = 0;
7164 sas_device->connector_name[0] = '\0';
7165 }
7166
aba5a85c
SR
7167 _scsih_get_enclosure_logicalid_chassis_slot(ioc,
7168 sas_device_pg0, sas_device);
75888956 7169
e6d45e3e 7170 if (sas_device->handle == sas_device_pg0->DevHandle)
f92363d1
SR
7171 goto out;
7172 pr_info("\thandle changed from(0x%04x)!!!\n",
7173 sas_device->handle);
e6d45e3e 7174 sas_device->handle = sas_device_pg0->DevHandle;
f92363d1 7175 if (sas_target_priv_data)
e6d45e3e
SR
7176 sas_target_priv_data->handle =
7177 sas_device_pg0->DevHandle;
f92363d1
SR
7178 goto out;
7179 }
7180 }
7181 out:
7182 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7183}
7184
7185/**
7186 * _scsih_search_responding_sas_devices -
7187 * @ioc: per adapter object
7188 *
7189 * After host reset, find out whether devices are still responding.
7190 * If not remove.
7191 *
7192 * Return nothing.
7193 */
7194static void
7195_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7196{
7197 Mpi2SasDevicePage0_t sas_device_pg0;
7198 Mpi2ConfigReply_t mpi_reply;
7199 u16 ioc_status;
7200 u16 handle;
7201 u32 device_info;
7202
7203 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7204
7205 if (list_empty(&ioc->sas_device_list))
7206 goto out;
7207
7208 handle = 0xFFFF;
7209 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7210 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7211 handle))) {
7212 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7213 MPI2_IOCSTATUS_MASK;
14be49ac 7214 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1 7215 break;
e6d45e3e
SR
7216 handle = sas_device_pg0.DevHandle =
7217 le16_to_cpu(sas_device_pg0.DevHandle);
f92363d1
SR
7218 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7219 if (!(_scsih_is_end_device(device_info)))
7220 continue;
e6d45e3e
SR
7221 sas_device_pg0.SASAddress =
7222 le64_to_cpu(sas_device_pg0.SASAddress);
7223 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
aa53bb89 7224 sas_device_pg0.Flags = le16_to_cpu(sas_device_pg0.Flags);
e6d45e3e 7225 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
f92363d1
SR
7226 }
7227
7228 out:
7229 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7230 ioc->name);
7231}
7232
7233/**
7234 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7235 * @ioc: per adapter object
7236 * @wwid: world wide identifier for raid volume
7237 * @handle: device handle
7238 *
7239 * After host reset, find out whether devices are still responding.
7240 * Used in _scsih_remove_unresponsive_raid_devices.
7241 *
7242 * Return nothing.
7243 */
7244static void
7245_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7246 u16 handle)
7247{
7786ab6a 7248 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
f92363d1
SR
7249 struct scsi_target *starget;
7250 struct _raid_device *raid_device;
7251 unsigned long flags;
7252
7253 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7254 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7255 if (raid_device->wwid == wwid && raid_device->starget) {
7256 starget = raid_device->starget;
7257 if (starget && starget->hostdata) {
7258 sas_target_priv_data = starget->hostdata;
7259 sas_target_priv_data->deleted = 0;
7260 } else
7261 sas_target_priv_data = NULL;
7262 raid_device->responding = 1;
7263 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7264 starget_printk(KERN_INFO, raid_device->starget,
7265 "handle(0x%04x), wwid(0x%016llx)\n", handle,
7266 (unsigned long long)raid_device->wwid);
7786ab6a 7267
7786ab6a
SR
7268 /*
7269 * WARPDRIVE: The handles of the PDs might have changed
7270 * across the host reset so re-initialize the
7271 * required data for Direct IO
7272 */
c84b06a4 7273 mpt3sas_init_warpdrive_properties(ioc, raid_device);
f92363d1
SR
7274 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7275 if (raid_device->handle == handle) {
7276 spin_unlock_irqrestore(&ioc->raid_device_lock,
7277 flags);
7278 return;
7279 }
7280 pr_info("\thandle changed from(0x%04x)!!!\n",
7281 raid_device->handle);
7282 raid_device->handle = handle;
7283 if (sas_target_priv_data)
7284 sas_target_priv_data->handle = handle;
7285 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7286 return;
7287 }
7288 }
7289 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7290}
7291
7292/**
7293 * _scsih_search_responding_raid_devices -
7294 * @ioc: per adapter object
7295 *
7296 * After host reset, find out whether devices are still responding.
7297 * If not remove.
7298 *
7299 * Return nothing.
7300 */
7301static void
7302_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7303{
7304 Mpi2RaidVolPage1_t volume_pg1;
7305 Mpi2RaidVolPage0_t volume_pg0;
7306 Mpi2RaidPhysDiskPage0_t pd_pg0;
7307 Mpi2ConfigReply_t mpi_reply;
7308 u16 ioc_status;
7309 u16 handle;
7310 u8 phys_disk_num;
7311
7312 if (!ioc->ir_firmware)
7313 return;
7314
7315 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7316 ioc->name);
7317
7318 if (list_empty(&ioc->raid_device_list))
7319 goto out;
7320
7321 handle = 0xFFFF;
7322 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7323 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7324 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7325 MPI2_IOCSTATUS_MASK;
14be49ac 7326 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1
SR
7327 break;
7328 handle = le16_to_cpu(volume_pg1.DevHandle);
7329
7330 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7331 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7332 sizeof(Mpi2RaidVolPage0_t)))
7333 continue;
7334
7335 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7336 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7337 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7338 _scsih_mark_responding_raid_device(ioc,
7339 le64_to_cpu(volume_pg1.WWID), handle);
7340 }
7341
7342 /* refresh the pd_handles */
7786ab6a 7343 if (!ioc->is_warpdrive) {
f92363d1
SR
7344 phys_disk_num = 0xFF;
7345 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7346 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7347 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7348 phys_disk_num))) {
7349 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7350 MPI2_IOCSTATUS_MASK;
14be49ac 7351 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1
SR
7352 break;
7353 phys_disk_num = pd_pg0.PhysDiskNum;
7354 handle = le16_to_cpu(pd_pg0.DevHandle);
7355 set_bit(handle, ioc->pd_handles);
7356 }
7786ab6a 7357 }
f92363d1
SR
7358 out:
7359 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7360 ioc->name);
7361}
7362
7363/**
7364 * _scsih_mark_responding_expander - mark a expander as responding
7365 * @ioc: per adapter object
aba5a85c 7366 * @expander_pg0:SAS Expander Config Page0
f92363d1
SR
7367 *
7368 * After host reset, find out whether devices are still responding.
7369 * Used in _scsih_remove_unresponsive_expanders.
7370 *
7371 * Return nothing.
7372 */
7373static void
aba5a85c
SR
7374_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc,
7375 Mpi2ExpanderPage0_t *expander_pg0)
f92363d1 7376{
aba5a85c 7377 struct _sas_node *sas_expander = NULL;
f92363d1 7378 unsigned long flags;
aba5a85c
SR
7379 int i, encl_pg0_rc = -1;
7380 Mpi2ConfigReply_t mpi_reply;
7381 Mpi2SasEnclosurePage0_t enclosure_pg0;
7382 u16 handle = le16_to_cpu(expander_pg0->DevHandle);
7383 u64 sas_address = le64_to_cpu(expander_pg0->SASAddress);
7384
7385 if (le16_to_cpu(expander_pg0->EnclosureHandle)) {
7386 encl_pg0_rc = mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
7387 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
7388 le16_to_cpu(expander_pg0->EnclosureHandle));
7389 if (encl_pg0_rc)
7390 pr_info(MPT3SAS_FMT
7391 "Enclosure Pg0 read failed for handle(0x%04x)\n",
7392 ioc->name,
7393 le16_to_cpu(expander_pg0->EnclosureHandle));
7394 }
f92363d1
SR
7395
7396 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7397 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7398 if (sas_expander->sas_address != sas_address)
7399 continue;
7400 sas_expander->responding = 1;
aba5a85c
SR
7401
7402 if (!encl_pg0_rc)
7403 sas_expander->enclosure_logical_id =
7404 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
7405
7406 sas_expander->enclosure_handle =
7407 le16_to_cpu(expander_pg0->EnclosureHandle);
7408
f92363d1
SR
7409 if (sas_expander->handle == handle)
7410 goto out;
7411 pr_info("\texpander(0x%016llx): handle changed" \
7412 " from(0x%04x) to (0x%04x)!!!\n",
7413 (unsigned long long)sas_expander->sas_address,
7414 sas_expander->handle, handle);
7415 sas_expander->handle = handle;
7416 for (i = 0 ; i < sas_expander->num_phys ; i++)
7417 sas_expander->phy[i].handle = handle;
7418 goto out;
7419 }
7420 out:
7421 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7422}
7423
7424/**
7425 * _scsih_search_responding_expanders -
7426 * @ioc: per adapter object
7427 *
7428 * After host reset, find out whether devices are still responding.
7429 * If not remove.
7430 *
7431 * Return nothing.
7432 */
7433static void
7434_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7435{
7436 Mpi2ExpanderPage0_t expander_pg0;
7437 Mpi2ConfigReply_t mpi_reply;
7438 u16 ioc_status;
7439 u64 sas_address;
7440 u16 handle;
7441
7442 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7443
7444 if (list_empty(&ioc->sas_expander_list))
7445 goto out;
7446
7447 handle = 0xFFFF;
7448 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7449 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7450
7451 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7452 MPI2_IOCSTATUS_MASK;
14be49ac 7453 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
f92363d1
SR
7454 break;
7455
7456 handle = le16_to_cpu(expander_pg0.DevHandle);
7457 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7458 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7459 handle,
7460 (unsigned long long)sas_address);
aba5a85c 7461 _scsih_mark_responding_expander(ioc, &expander_pg0);
f92363d1
SR
7462 }
7463
7464 out:
7465 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7466}
7467
7468/**
7469 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7470 * @ioc: per adapter object
7471 *
7472 * Return nothing.
7473 */
7474static void
7475_scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7476{
7477 struct _sas_device *sas_device, *sas_device_next;
7478 struct _sas_node *sas_expander, *sas_expander_next;
7479 struct _raid_device *raid_device, *raid_device_next;
7480 struct list_head tmp_list;
7481 unsigned long flags;
d1cb5e49 7482 LIST_HEAD(head);
f92363d1
SR
7483
7484 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7485 ioc->name);
7486
7487 /* removing unresponding end devices */
7488 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7489 ioc->name);
d1cb5e49
SR
7490 /*
7491 * Iterate, pulling off devices marked as non-responding. We become the
7492 * owner for the reference the list had on any object we prune.
7493 */
7494 spin_lock_irqsave(&ioc->sas_device_lock, flags);
f92363d1
SR
7495 list_for_each_entry_safe(sas_device, sas_device_next,
7496 &ioc->sas_device_list, list) {
7497 if (!sas_device->responding)
d1cb5e49 7498 list_move_tail(&sas_device->list, &head);
f92363d1
SR
7499 else
7500 sas_device->responding = 0;
7501 }
d1cb5e49
SR
7502 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7503
7504 /*
7505 * Now, uninitialize and remove the unresponding devices we pruned.
7506 */
7507 list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7508 _scsih_remove_device(ioc, sas_device);
7509 list_del_init(&sas_device->list);
7510 sas_device_put(sas_device);
7511 }
f92363d1
SR
7512
7513 /* removing unresponding volumes */
7514 if (ioc->ir_firmware) {
7515 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7516 ioc->name);
7517 list_for_each_entry_safe(raid_device, raid_device_next,
7518 &ioc->raid_device_list, list) {
7519 if (!raid_device->responding)
7520 _scsih_sas_volume_delete(ioc,
7521 raid_device->handle);
7522 else
7523 raid_device->responding = 0;
7524 }
7525 }
7526
7527 /* removing unresponding expanders */
7528 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7529 ioc->name);
7530 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7531 INIT_LIST_HEAD(&tmp_list);
7532 list_for_each_entry_safe(sas_expander, sas_expander_next,
7533 &ioc->sas_expander_list, list) {
7534 if (!sas_expander->responding)
7535 list_move_tail(&sas_expander->list, &tmp_list);
7536 else
7537 sas_expander->responding = 0;
7538 }
7539 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7540 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7541 list) {
f92363d1
SR
7542 _scsih_expander_node_remove(ioc, sas_expander);
7543 }
7544
7545 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7546 ioc->name);
7547
7548 /* unblock devices */
7549 _scsih_ublock_io_all_device(ioc);
7550}
7551
7552static void
7553_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7554 struct _sas_node *sas_expander, u16 handle)
7555{
7556 Mpi2ExpanderPage1_t expander_pg1;
7557 Mpi2ConfigReply_t mpi_reply;
7558 int i;
7559
7560 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7561 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7562 &expander_pg1, i, handle))) {
7563 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7564 ioc->name, __FILE__, __LINE__, __func__);
7565 return;
7566 }
7567
7568 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7569 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7570 expander_pg1.NegotiatedLinkRate >> 4);
7571 }
7572}
7573
7574/**
7575 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7576 * @ioc: per adapter object
7577 *
7578 * Return nothing.
7579 */
7580static void
7581_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7582{
7583 Mpi2ExpanderPage0_t expander_pg0;
7584 Mpi2SasDevicePage0_t sas_device_pg0;
7585 Mpi2RaidVolPage1_t volume_pg1;
7586 Mpi2RaidVolPage0_t volume_pg0;
7587 Mpi2RaidPhysDiskPage0_t pd_pg0;
7588 Mpi2EventIrConfigElement_t element;
7589 Mpi2ConfigReply_t mpi_reply;
7590 u8 phys_disk_num;
7591 u16 ioc_status;
7592 u16 handle, parent_handle;
7593 u64 sas_address;
7594 struct _sas_device *sas_device;
7595 struct _sas_node *expander_device;
7596 static struct _raid_device *raid_device;
7597 u8 retry_count;
7598 unsigned long flags;
7599
7600 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7601
7602 _scsih_sas_host_refresh(ioc);
7603
7604 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7605
7606 /* expanders */
7607 handle = 0xFFFF;
7608 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7609 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7610 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7611 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7612 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7613 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7614 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7615 ioc->name, ioc_status,
7616 le32_to_cpu(mpi_reply.IOCLogInfo));
7617 break;
7618 }
7619 handle = le16_to_cpu(expander_pg0.DevHandle);
7620 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7621 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7622 ioc, le64_to_cpu(expander_pg0.SASAddress));
7623 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7624 if (expander_device)
7625 _scsih_refresh_expander_links(ioc, expander_device,
7626 handle);
7627 else {
7628 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7629 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7630 handle, (unsigned long long)
7631 le64_to_cpu(expander_pg0.SASAddress));
7632 _scsih_expander_add(ioc, handle);
7633 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7634 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7635 handle, (unsigned long long)
7636 le64_to_cpu(expander_pg0.SASAddress));
7637 }
7638 }
7639
7640 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7641 ioc->name);
7642
7643 if (!ioc->ir_firmware)
7644 goto skip_to_sas;
7645
7646 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7647
7648 /* phys disk */
7649 phys_disk_num = 0xFF;
7650 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7651 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7652 phys_disk_num))) {
7653 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7654 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7655 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7656 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7657 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7658 ioc->name, ioc_status,
7659 le32_to_cpu(mpi_reply.IOCLogInfo));
7660 break;
7661 }
7662 phys_disk_num = pd_pg0.PhysDiskNum;
7663 handle = le16_to_cpu(pd_pg0.DevHandle);
d1cb5e49
SR
7664 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7665 if (sas_device) {
7666 sas_device_put(sas_device);
f92363d1 7667 continue;
d1cb5e49 7668 }
f92363d1
SR
7669 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7670 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7671 handle) != 0)
7672 continue;
7673 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7674 MPI2_IOCSTATUS_MASK;
7675 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7676 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7677 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7678 ioc->name, ioc_status,
7679 le32_to_cpu(mpi_reply.IOCLogInfo));
7680 break;
7681 }
7682 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7683 if (!_scsih_get_sas_address(ioc, parent_handle,
7684 &sas_address)) {
7685 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7686 " handle (0x%04x), sas_addr(0x%016llx)\n",
7687 ioc->name, handle, (unsigned long long)
7688 le64_to_cpu(sas_device_pg0.SASAddress));
7689 mpt3sas_transport_update_links(ioc, sas_address,
7690 handle, sas_device_pg0.PhyNum,
7691 MPI2_SAS_NEG_LINK_RATE_1_5);
7692 set_bit(handle, ioc->pd_handles);
7693 retry_count = 0;
7694 /* This will retry adding the end device.
7695 * _scsih_add_device() will decide on retries and
7696 * return "1" when it should be retried
7697 */
7698 while (_scsih_add_device(ioc, handle, retry_count++,
7699 1)) {
7700 ssleep(1);
7701 }
7702 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7703 " handle (0x%04x), sas_addr(0x%016llx)\n",
7704 ioc->name, handle, (unsigned long long)
7705 le64_to_cpu(sas_device_pg0.SASAddress));
7706 }
7707 }
7708
7709 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7710 ioc->name);
7711
7712 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7713
7714 /* volumes */
7715 handle = 0xFFFF;
7716 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7717 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7718 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7719 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7720 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7721 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7722 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7723 ioc->name, ioc_status,
7724 le32_to_cpu(mpi_reply.IOCLogInfo));
7725 break;
7726 }
7727 handle = le16_to_cpu(volume_pg1.DevHandle);
7728 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7729 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7730 le64_to_cpu(volume_pg1.WWID));
7731 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7732 if (raid_device)
7733 continue;
7734 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7735 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7736 sizeof(Mpi2RaidVolPage0_t)))
7737 continue;
7738 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7739 MPI2_IOCSTATUS_MASK;
7740 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7741 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7742 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7743 ioc->name, ioc_status,
7744 le32_to_cpu(mpi_reply.IOCLogInfo));
7745 break;
7746 }
7747 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7748 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7749 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7750 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7751 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7752 element.VolDevHandle = volume_pg1.DevHandle;
7753 pr_info(MPT3SAS_FMT
7754 "\tBEFORE adding volume: handle (0x%04x)\n",
7755 ioc->name, volume_pg1.DevHandle);
7756 _scsih_sas_volume_add(ioc, &element);
7757 pr_info(MPT3SAS_FMT
7758 "\tAFTER adding volume: handle (0x%04x)\n",
7759 ioc->name, volume_pg1.DevHandle);
7760 }
7761 }
7762
7763 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7764 ioc->name);
7765
7766 skip_to_sas:
7767
7768 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7769 ioc->name);
7770
7771 /* sas devices */
7772 handle = 0xFFFF;
7773 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7774 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7775 handle))) {
7776 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7777 MPI2_IOCSTATUS_MASK;
f92363d1
SR
7778 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7779 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7780 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7781 ioc->name, ioc_status,
7782 le32_to_cpu(mpi_reply.IOCLogInfo));
7783 break;
7784 }
7785 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7786 if (!(_scsih_is_end_device(
7787 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7788 continue;
d1cb5e49 7789 sas_device = mpt3sas_get_sdev_by_addr(ioc,
f92363d1 7790 le64_to_cpu(sas_device_pg0.SASAddress));
d1cb5e49
SR
7791 if (sas_device) {
7792 sas_device_put(sas_device);
f92363d1 7793 continue;
d1cb5e49 7794 }
f92363d1
SR
7795 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7796 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7797 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7798 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7799 handle, (unsigned long long)
7800 le64_to_cpu(sas_device_pg0.SASAddress));
7801 mpt3sas_transport_update_links(ioc, sas_address, handle,
7802 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7803 retry_count = 0;
7804 /* This will retry adding the end device.
7805 * _scsih_add_device() will decide on retries and
7806 * return "1" when it should be retried
7807 */
7808 while (_scsih_add_device(ioc, handle, retry_count++,
7809 0)) {
7810 ssleep(1);
7811 }
7812 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7813 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7814 handle, (unsigned long long)
7815 le64_to_cpu(sas_device_pg0.SASAddress));
7816 }
7817 }
7818 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7819 ioc->name);
7820
7821 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7822}
7823/**
7824 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7825 * @ioc: per adapter object
7826 * @reset_phase: phase
7827 *
7828 * The handler for doing any required cleanup or initialization.
7829 *
7830 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7831 * MPT3_IOC_DONE_RESET
7832 *
7833 * Return nothing.
7834 */
7835void
7836mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7837{
7838 switch (reset_phase) {
7839 case MPT3_IOC_PRE_RESET:
7840 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7841 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7842 break;
7843 case MPT3_IOC_AFTER_RESET:
7844 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7845 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7846 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7847 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7848 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7849 complete(&ioc->scsih_cmds.done);
7850 }
7851 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7852 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7853 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7854 complete(&ioc->tm_cmds.done);
7855 }
7856
c696f7b8
SPS
7857 memset(ioc->pend_os_device_add, 0, ioc->pend_os_device_add_sz);
7858 memset(ioc->device_remove_in_progress, 0,
7859 ioc->device_remove_in_progress_sz);
f92363d1
SR
7860 _scsih_fw_event_cleanup_queue(ioc);
7861 _scsih_flush_running_cmds(ioc);
7862 break;
7863 case MPT3_IOC_DONE_RESET:
7864 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7865 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7866 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7867 !ioc->sas_hba.num_phys)) {
7868 _scsih_prep_device_scan(ioc);
7869 _scsih_search_responding_sas_devices(ioc);
7870 _scsih_search_responding_raid_devices(ioc);
7871 _scsih_search_responding_expanders(ioc);
7872 _scsih_error_recovery_delete_devices(ioc);
7873 }
7874 break;
7875 }
7876}
7877
7878/**
7879 * _mpt3sas_fw_work - delayed task for processing firmware events
7880 * @ioc: per adapter object
7881 * @fw_event: The fw_event_work object
7882 * Context: user.
7883 *
7884 * Return nothing.
7885 */
7886static void
7887_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7888{
146b16c8
SR
7889 _scsih_fw_event_del_from_list(ioc, fw_event);
7890
f92363d1 7891 /* the queue is being flushed so ignore this event */
146b16c8
SR
7892 if (ioc->remove_host || ioc->pci_error_recovery) {
7893 fw_event_work_put(fw_event);
f92363d1
SR
7894 return;
7895 }
7896
7897 switch (fw_event->event) {
7898 case MPT3SAS_PROCESS_TRIGGER_DIAG:
35b62362
JL
7899 mpt3sas_process_trigger_data(ioc,
7900 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7901 fw_event->event_data);
f92363d1
SR
7902 break;
7903 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
146b16c8
SR
7904 while (scsi_host_in_recovery(ioc->shost) ||
7905 ioc->shost_recovery) {
7906 /*
7907 * If we're unloading, bail. Otherwise, this can become
7908 * an infinite loop.
7909 */
7910 if (ioc->remove_host)
7911 goto out;
f92363d1 7912 ssleep(1);
146b16c8 7913 }
f92363d1
SR
7914 _scsih_remove_unresponding_sas_devices(ioc);
7915 _scsih_scan_for_devices_after_reset(ioc);
7916 break;
7917 case MPT3SAS_PORT_ENABLE_COMPLETE:
7918 ioc->start_scan = 0;
7919 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7920 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7921 missing_delay[1]);
7922 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7923 "port enable: complete from worker thread\n",
7924 ioc->name));
7925 break;
0f624c39
SR
7926 case MPT3SAS_TURN_ON_PFA_LED:
7927 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
f92363d1
SR
7928 break;
7929 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7930 _scsih_sas_topology_change_event(ioc, fw_event);
7931 break;
7932 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7933 _scsih_sas_device_status_change_event(ioc, fw_event);
7934 break;
7935 case MPI2_EVENT_SAS_DISCOVERY:
7936 _scsih_sas_discovery_event(ioc, fw_event);
7937 break;
7938 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7939 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7940 break;
7941 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7942 _scsih_sas_enclosure_dev_status_change_event(ioc,
7943 fw_event);
7944 break;
7945 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7946 _scsih_sas_ir_config_change_event(ioc, fw_event);
7947 break;
7948 case MPI2_EVENT_IR_VOLUME:
7949 _scsih_sas_ir_volume_event(ioc, fw_event);
7950 break;
7951 case MPI2_EVENT_IR_PHYSICAL_DISK:
7952 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7953 break;
7954 case MPI2_EVENT_IR_OPERATION_STATUS:
7955 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7956 break;
7957 }
146b16c8
SR
7958out:
7959 fw_event_work_put(fw_event);
f92363d1
SR
7960}
7961
7962/**
7963 * _firmware_event_work
7964 * @ioc: per adapter object
7965 * @work: The fw_event_work object
7966 * Context: user.
7967 *
7968 * wrappers for the work thread handling firmware events
7969 *
7970 * Return nothing.
7971 */
7972
7973static void
7974_firmware_event_work(struct work_struct *work)
7975{
7976 struct fw_event_work *fw_event = container_of(work,
7977 struct fw_event_work, work);
7978
7979 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7980}
7981
7982/**
7983 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7984 * @ioc: per adapter object
7985 * @msix_index: MSIX table index supplied by the OS
7986 * @reply: reply message frame(lower 32bit addr)
7987 * Context: interrupt.
7988 *
7989 * This function merely adds a new work task into ioc->firmware_event_thread.
7990 * The tasks are worked from _firmware_event_work in user context.
7991 *
7992 * Return 1 meaning mf should be freed from _base_interrupt
7993 * 0 means the mf is freed from this function.
7994 */
7995u8
7996mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7997 u32 reply)
7998{
7999 struct fw_event_work *fw_event;
8000 Mpi2EventNotificationReply_t *mpi_reply;
8001 u16 event;
8002 u16 sz;
a470a51c 8003 Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
f92363d1
SR
8004
8005 /* events turned off due to host reset or driver unloading */
8006 if (ioc->remove_host || ioc->pci_error_recovery)
8007 return 1;
8008
8009 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
8010
8011 if (unlikely(!mpi_reply)) {
8012 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
8013 ioc->name, __FILE__, __LINE__, __func__);
8014 return 1;
8015 }
8016
8017 event = le16_to_cpu(mpi_reply->Event);
8018
8019 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
8020 mpt3sas_trigger_event(ioc, event, 0);
8021
8022 switch (event) {
8023 /* handle these */
8024 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
8025 {
8026 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
8027 (Mpi2EventDataSasBroadcastPrimitive_t *)
8028 mpi_reply->EventData;
8029
8030 if (baen_data->Primitive !=
8031 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
8032 return 1;
8033
8034 if (ioc->broadcast_aen_busy) {
8035 ioc->broadcast_aen_pending++;
8036 return 1;
8037 } else
8038 ioc->broadcast_aen_busy = 1;
8039 break;
8040 }
8041
8042 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
8043 _scsih_check_topo_delete_events(ioc,
8044 (Mpi2EventDataSasTopologyChangeList_t *)
8045 mpi_reply->EventData);
8046 break;
8047 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
8048 _scsih_check_ir_config_unhide_events(ioc,
8049 (Mpi2EventDataIrConfigChangeList_t *)
8050 mpi_reply->EventData);
8051 break;
8052 case MPI2_EVENT_IR_VOLUME:
8053 _scsih_check_volume_delete_events(ioc,
8054 (Mpi2EventDataIrVolume_t *)
8055 mpi_reply->EventData);
8056 break;
7786ab6a
SR
8057 case MPI2_EVENT_LOG_ENTRY_ADDED:
8058 {
8059 Mpi2EventDataLogEntryAdded_t *log_entry;
8060 u32 *log_code;
8061
8062 if (!ioc->is_warpdrive)
8063 break;
8064
8065 log_entry = (Mpi2EventDataLogEntryAdded_t *)
8066 mpi_reply->EventData;
8067 log_code = (u32 *)log_entry->LogData;
f92363d1 8068
7786ab6a
SR
8069 if (le16_to_cpu(log_entry->LogEntryQualifier)
8070 != MPT2_WARPDRIVE_LOGENTRY)
8071 break;
8072
8073 switch (le32_to_cpu(*log_code)) {
8074 case MPT2_WARPDRIVE_LC_SSDT:
8075 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
8076 "IO Throttling has occurred in the WarpDrive "
8077 "subsystem. Check WarpDrive documentation for "
8078 "additional details.\n", ioc->name);
8079 break;
8080 case MPT2_WARPDRIVE_LC_SSDLW:
8081 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
8082 "Program/Erase Cycles for the WarpDrive subsystem "
8083 "in degraded range. Check WarpDrive documentation "
8084 "for additional details.\n", ioc->name);
8085 break;
8086 case MPT2_WARPDRIVE_LC_SSDLF:
8087 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
8088 "There are no Program/Erase Cycles for the "
8089 "WarpDrive subsystem. The storage device will be "
8090 "in read-only mode. Check WarpDrive documentation "
8091 "for additional details.\n", ioc->name);
8092 break;
8093 case MPT2_WARPDRIVE_LC_BRMF:
8094 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
8095 "The Backup Rail Monitor has failed on the "
8096 "WarpDrive subsystem. Check WarpDrive "
8097 "documentation for additional details.\n",
8098 ioc->name);
8099 break;
8100 }
8101
8102 break;
8103 }
f92363d1
SR
8104 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
8105 case MPI2_EVENT_IR_OPERATION_STATUS:
8106 case MPI2_EVENT_SAS_DISCOVERY:
8107 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
8108 case MPI2_EVENT_IR_PHYSICAL_DISK:
8109 break;
8110
2d8ce8c9
SR
8111 case MPI2_EVENT_TEMP_THRESHOLD:
8112 _scsih_temp_threshold_events(ioc,
8113 (Mpi2EventDataTemperature_t *)
8114 mpi_reply->EventData);
8115 break;
a470a51c
C
8116 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
8117 ActiveCableEventData =
8118 (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
6c44c0fe
C
8119 switch (ActiveCableEventData->ReasonCode) {
8120 case MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER:
b99b1993
SR
8121 pr_notice(MPT3SAS_FMT
8122 "Currently an active cable with ReceptacleID %d\n",
8123 ioc->name, ActiveCableEventData->ReceptacleID);
8124 pr_notice("cannot be powered and devices connected\n");
8125 pr_notice("to this active cable will not be seen\n");
8126 pr_notice("This active cable requires %d mW of power\n",
6c44c0fe 8127 ActiveCableEventData->ActiveCablePowerRequirement);
6c44c0fe
C
8128 break;
8129
8130 case MPI26_EVENT_ACTIVE_CABLE_DEGRADED:
b99b1993
SR
8131 pr_notice(MPT3SAS_FMT
8132 "Currently a cable with ReceptacleID %d\n",
8133 ioc->name, ActiveCableEventData->ReceptacleID);
8134 pr_notice(
8135 "is not running at optimal speed(12 Gb/s rate)\n");
6c44c0fe 8136 break;
7ebd67e0 8137 }
6c44c0fe 8138
a470a51c 8139 break;
2d8ce8c9 8140
f92363d1
SR
8141 default: /* ignore the rest */
8142 return 1;
8143 }
8144
f92363d1 8145 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
146b16c8 8146 fw_event = alloc_fw_event_work(sz);
35b62362 8147 if (!fw_event) {
f92363d1
SR
8148 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8149 ioc->name, __FILE__, __LINE__, __func__);
f92363d1
SR
8150 return 1;
8151 }
8152
8153 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
8154 fw_event->ioc = ioc;
8155 fw_event->VF_ID = mpi_reply->VF_ID;
8156 fw_event->VP_ID = mpi_reply->VP_ID;
8157 fw_event->event = event;
8158 _scsih_fw_event_add(ioc, fw_event);
146b16c8 8159 fw_event_work_put(fw_event);
f92363d1
SR
8160 return 1;
8161}
8162
f92363d1
SR
8163/**
8164 * _scsih_expander_node_remove - removing expander device from list.
8165 * @ioc: per adapter object
8166 * @sas_expander: the sas_device object
f92363d1
SR
8167 *
8168 * Removing object and freeing associated memory from the
8169 * ioc->sas_expander_list.
8170 *
8171 * Return nothing.
8172 */
8173static void
8174_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
8175 struct _sas_node *sas_expander)
8176{
8177 struct _sas_port *mpt3sas_port, *next;
bbe3def3 8178 unsigned long flags;
f92363d1
SR
8179
8180 /* remove sibling ports attached to this expander */
8181 list_for_each_entry_safe(mpt3sas_port, next,
8182 &sas_expander->sas_port_list, port_list) {
8183 if (ioc->shost_recovery)
8184 return;
8185 if (mpt3sas_port->remote_identify.device_type ==
8186 SAS_END_DEVICE)
8187 mpt3sas_device_remove_by_sas_address(ioc,
8188 mpt3sas_port->remote_identify.sas_address);
8189 else if (mpt3sas_port->remote_identify.device_type ==
8190 SAS_EDGE_EXPANDER_DEVICE ||
8191 mpt3sas_port->remote_identify.device_type ==
8192 SAS_FANOUT_EXPANDER_DEVICE)
8193 mpt3sas_expander_remove(ioc,
8194 mpt3sas_port->remote_identify.sas_address);
8195 }
8196
8197 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8198 sas_expander->sas_address_parent);
8199
8200 pr_info(MPT3SAS_FMT
8201 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8202 ioc->name,
8203 sas_expander->handle, (unsigned long long)
8204 sas_expander->sas_address);
8205
bbe3def3
SR
8206 spin_lock_irqsave(&ioc->sas_node_lock, flags);
8207 list_del(&sas_expander->list);
8208 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
8209
f92363d1
SR
8210 kfree(sas_expander->phy);
8211 kfree(sas_expander);
8212}
8213
8214/**
8215 * _scsih_ir_shutdown - IR shutdown notification
8216 * @ioc: per adapter object
8217 *
8218 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8219 * the host system is shutting down.
8220 *
8221 * Return nothing.
8222 */
8223static void
8224_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8225{
8226 Mpi2RaidActionRequest_t *mpi_request;
8227 Mpi2RaidActionReply_t *mpi_reply;
8228 u16 smid;
8229
8230 /* is IR firmware build loaded ? */
8231 if (!ioc->ir_firmware)
8232 return;
8233
8234 /* are there any volumes ? */
8235 if (list_empty(&ioc->raid_device_list))
8236 return;
8237
8238 mutex_lock(&ioc->scsih_cmds.mutex);
8239
8240 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8241 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8242 ioc->name, __func__);
8243 goto out;
8244 }
8245 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8246
8247 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8248 if (!smid) {
8249 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8250 ioc->name, __func__);
8251 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8252 goto out;
8253 }
8254
8255 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8256 ioc->scsih_cmds.smid = smid;
8257 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8258
8259 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8260 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8261
7786ab6a
SR
8262 if (!ioc->hide_ir_msg)
8263 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
f92363d1 8264 init_completion(&ioc->scsih_cmds.done);
81c16f83 8265 ioc->put_smid_default(ioc, smid);
f92363d1
SR
8266 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8267
8268 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8269 pr_err(MPT3SAS_FMT "%s: timeout\n",
8270 ioc->name, __func__);
8271 goto out;
8272 }
8273
8274 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8275 mpi_reply = ioc->scsih_cmds.reply;
7786ab6a
SR
8276 if (!ioc->hide_ir_msg)
8277 pr_info(MPT3SAS_FMT "IR shutdown "
8278 "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8279 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8280 le32_to_cpu(mpi_reply->IOCLogInfo));
f92363d1
SR
8281 }
8282
8283 out:
8284 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8285 mutex_unlock(&ioc->scsih_cmds.mutex);
8286}
8287
8288/**
8a7e4c24 8289 * scsih_remove - detach and remove add host
f92363d1
SR
8290 * @pdev: PCI device struct
8291 *
8292 * Routine called when unloading the driver.
8293 * Return nothing.
8294 */
8bbb1cf6 8295static void scsih_remove(struct pci_dev *pdev)
f92363d1
SR
8296{
8297 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8298 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8299 struct _sas_port *mpt3sas_port, *next_port;
8300 struct _raid_device *raid_device, *next;
8301 struct MPT3SAS_TARGET *sas_target_priv_data;
8302 struct workqueue_struct *wq;
8303 unsigned long flags;
8304
8305 ioc->remove_host = 1;
8306 _scsih_fw_event_cleanup_queue(ioc);
8307
8308 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8309 wq = ioc->firmware_event_thread;
8310 ioc->firmware_event_thread = NULL;
8311 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8312 if (wq)
8313 destroy_workqueue(wq);
8314
8315 /* release all the volumes */
8316 _scsih_ir_shutdown(ioc);
8317 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8318 list) {
8319 if (raid_device->starget) {
8320 sas_target_priv_data =
8321 raid_device->starget->hostdata;
8322 sas_target_priv_data->deleted = 1;
8323 scsi_remove_target(&raid_device->starget->dev);
8324 }
8325 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8326 ioc->name, raid_device->handle,
8327 (unsigned long long) raid_device->wwid);
8328 _scsih_raid_device_remove(ioc, raid_device);
8329 }
8330
8331 /* free ports attached to the sas_host */
8332 list_for_each_entry_safe(mpt3sas_port, next_port,
8333 &ioc->sas_hba.sas_port_list, port_list) {
8334 if (mpt3sas_port->remote_identify.device_type ==
8335 SAS_END_DEVICE)
8336 mpt3sas_device_remove_by_sas_address(ioc,
8337 mpt3sas_port->remote_identify.sas_address);
8338 else if (mpt3sas_port->remote_identify.device_type ==
8339 SAS_EDGE_EXPANDER_DEVICE ||
8340 mpt3sas_port->remote_identify.device_type ==
8341 SAS_FANOUT_EXPANDER_DEVICE)
8342 mpt3sas_expander_remove(ioc,
8343 mpt3sas_port->remote_identify.sas_address);
8344 }
8345
8346 /* free phys attached to the sas_host */
8347 if (ioc->sas_hba.num_phys) {
8348 kfree(ioc->sas_hba.phy);
8349 ioc->sas_hba.phy = NULL;
8350 ioc->sas_hba.num_phys = 0;
8351 }
8352
8353 sas_remove_host(shost);
8354 mpt3sas_base_detach(ioc);
08c4d550 8355 spin_lock(&gioc_lock);
f92363d1 8356 list_del(&ioc->list);
08c4d550 8357 spin_unlock(&gioc_lock);
f92363d1
SR
8358 scsi_host_put(shost);
8359}
8360
8361/**
8a7e4c24 8362 * scsih_shutdown - routine call during system shutdown
f92363d1
SR
8363 * @pdev: PCI device struct
8364 *
8365 * Return nothing.
8366 */
8bbb1cf6 8367static void
8a7e4c24 8368scsih_shutdown(struct pci_dev *pdev)
f92363d1
SR
8369{
8370 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8371 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8372 struct workqueue_struct *wq;
8373 unsigned long flags;
8374
8375 ioc->remove_host = 1;
8376 _scsih_fw_event_cleanup_queue(ioc);
8377
8378 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8379 wq = ioc->firmware_event_thread;
8380 ioc->firmware_event_thread = NULL;
8381 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8382 if (wq)
8383 destroy_workqueue(wq);
8384
8385 _scsih_ir_shutdown(ioc);
8386 mpt3sas_base_detach(ioc);
8387}
8388
8389
8390/**
8391 * _scsih_probe_boot_devices - reports 1st device
8392 * @ioc: per adapter object
8393 *
8394 * If specified in bios page 2, this routine reports the 1st
8395 * device scsi-ml or sas transport for persistent boot device
8396 * purposes. Please refer to function _scsih_determine_boot_device()
8397 */
8398static void
8399_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8400{
8401 u8 is_raid;
8402 void *device;
8403 struct _sas_device *sas_device;
8404 struct _raid_device *raid_device;
8405 u16 handle;
8406 u64 sas_address_parent;
8407 u64 sas_address;
8408 unsigned long flags;
8409 int rc;
8410
8411 /* no Bios, return immediately */
8412 if (!ioc->bios_pg3.BiosVersion)
8413 return;
8414
8415 device = NULL;
8416 is_raid = 0;
8417 if (ioc->req_boot_device.device) {
8418 device = ioc->req_boot_device.device;
8419 is_raid = ioc->req_boot_device.is_raid;
8420 } else if (ioc->req_alt_boot_device.device) {
8421 device = ioc->req_alt_boot_device.device;
8422 is_raid = ioc->req_alt_boot_device.is_raid;
8423 } else if (ioc->current_boot_device.device) {
8424 device = ioc->current_boot_device.device;
8425 is_raid = ioc->current_boot_device.is_raid;
8426 }
8427
8428 if (!device)
8429 return;
8430
8431 if (is_raid) {
8432 raid_device = device;
8433 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8434 raid_device->id, 0);
8435 if (rc)
8436 _scsih_raid_device_remove(ioc, raid_device);
8437 } else {
8438 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8439 sas_device = device;
8440 handle = sas_device->handle;
8441 sas_address_parent = sas_device->sas_address_parent;
8442 sas_address = sas_device->sas_address;
8443 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8444 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8445
7786ab6a
SR
8446 if (ioc->hide_drives)
8447 return;
f92363d1
SR
8448 if (!mpt3sas_transport_port_add(ioc, handle,
8449 sas_address_parent)) {
8450 _scsih_sas_device_remove(ioc, sas_device);
8451 } else if (!sas_device->starget) {
f5edbe77
SR
8452 if (!ioc->is_driver_loading) {
8453 mpt3sas_transport_port_remove(ioc,
8454 sas_address,
f92363d1 8455 sas_address_parent);
f5edbe77
SR
8456 _scsih_sas_device_remove(ioc, sas_device);
8457 }
f92363d1
SR
8458 }
8459 }
8460}
8461
8462/**
8463 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8464 * @ioc: per adapter object
8465 *
8466 * Called during initial loading of the driver.
8467 */
8468static void
8469_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8470{
8471 struct _raid_device *raid_device, *raid_next;
8472 int rc;
8473
8474 list_for_each_entry_safe(raid_device, raid_next,
8475 &ioc->raid_device_list, list) {
8476 if (raid_device->starget)
8477 continue;
8478 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8479 raid_device->id, 0);
8480 if (rc)
8481 _scsih_raid_device_remove(ioc, raid_device);
8482 }
8483}
8484
d1cb5e49
SR
8485static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8486{
8487 struct _sas_device *sas_device = NULL;
8488 unsigned long flags;
8489
8490 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8491 if (!list_empty(&ioc->sas_device_init_list)) {
8492 sas_device = list_first_entry(&ioc->sas_device_init_list,
8493 struct _sas_device, list);
8494 sas_device_get(sas_device);
8495 }
8496 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8497
8498 return sas_device;
8499}
8500
8501static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8502 struct _sas_device *sas_device)
8503{
8504 unsigned long flags;
8505
8506 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8507
8508 /*
8509 * Since we dropped the lock during the call to port_add(), we need to
8510 * be careful here that somebody else didn't move or delete this item
8511 * while we were busy with other things.
8512 *
8513 * If it was on the list, we need a put() for the reference the list
8514 * had. Either way, we need a get() for the destination list.
8515 */
8516 if (!list_empty(&sas_device->list)) {
8517 list_del_init(&sas_device->list);
8518 sas_device_put(sas_device);
8519 }
8520
8521 sas_device_get(sas_device);
8522 list_add_tail(&sas_device->list, &ioc->sas_device_list);
8523
8524 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8525}
8526
f92363d1
SR
8527/**
8528 * _scsih_probe_sas - reporting sas devices to sas transport
8529 * @ioc: per adapter object
8530 *
8531 * Called during initial loading of the driver.
8532 */
8533static void
8534_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8535{
d1cb5e49 8536 struct _sas_device *sas_device;
f92363d1 8537
d1cb5e49
SR
8538 if (ioc->hide_drives)
8539 return;
7786ab6a 8540
d1cb5e49 8541 while ((sas_device = get_next_sas_device(ioc))) {
f92363d1
SR
8542 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8543 sas_device->sas_address_parent)) {
d1cb5e49
SR
8544 _scsih_sas_device_remove(ioc, sas_device);
8545 sas_device_put(sas_device);
f92363d1
SR
8546 continue;
8547 } else if (!sas_device->starget) {
8548 /*
8549 * When asyn scanning is enabled, its not possible to
8550 * remove devices while scanning is turned on due to an
8551 * oops in scsi_sysfs_add_sdev()->add_device()->
8552 * sysfs_addrm_start()
8553 */
f5edbe77 8554 if (!ioc->is_driver_loading) {
f92363d1
SR
8555 mpt3sas_transport_port_remove(ioc,
8556 sas_device->sas_address,
8557 sas_device->sas_address_parent);
d1cb5e49
SR
8558 _scsih_sas_device_remove(ioc, sas_device);
8559 sas_device_put(sas_device);
f5edbe77
SR
8560 continue;
8561 }
f92363d1 8562 }
d1cb5e49
SR
8563 sas_device_make_active(ioc, sas_device);
8564 sas_device_put(sas_device);
f92363d1
SR
8565 }
8566}
8567
8568/**
8569 * _scsih_probe_devices - probing for devices
8570 * @ioc: per adapter object
8571 *
8572 * Called during initial loading of the driver.
8573 */
8574static void
8575_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8576{
8577 u16 volume_mapping_flags;
8578
8579 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8580 return; /* return when IOC doesn't support initiator mode */
8581
8582 _scsih_probe_boot_devices(ioc);
8583
8584 if (ioc->ir_firmware) {
8585 volume_mapping_flags =
8586 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8587 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8588 if (volume_mapping_flags ==
8589 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8590 _scsih_probe_raid(ioc);
8591 _scsih_probe_sas(ioc);
8592 } else {
8593 _scsih_probe_sas(ioc);
8594 _scsih_probe_raid(ioc);
8595 }
8596 } else
8597 _scsih_probe_sas(ioc);
8598}
8599
8600/**
8a7e4c24 8601 * scsih_scan_start - scsi lld callback for .scan_start
f92363d1
SR
8602 * @shost: SCSI host pointer
8603 *
8604 * The shost has the ability to discover targets on its own instead
8605 * of scanning the entire bus. In our implemention, we will kick off
8606 * firmware discovery.
8607 */
8bbb1cf6 8608static void
8a7e4c24 8609scsih_scan_start(struct Scsi_Host *shost)
f92363d1
SR
8610{
8611 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8612 int rc;
8613 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8614 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8615
8616 if (disable_discovery > 0)
8617 return;
8618
8619 ioc->start_scan = 1;
8620 rc = mpt3sas_port_enable(ioc);
8621
8622 if (rc != 0)
8623 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8624}
8625
8626/**
8a7e4c24 8627 * scsih_scan_finished - scsi lld callback for .scan_finished
f92363d1
SR
8628 * @shost: SCSI host pointer
8629 * @time: elapsed time of the scan in jiffies
8630 *
8631 * This function will be called periodicallyn until it returns 1 with the
8632 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8633 * we wait for firmware discovery to complete, then return 1.
8634 */
8bbb1cf6 8635static int
8a7e4c24 8636scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
f92363d1
SR
8637{
8638 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8639
8640 if (disable_discovery > 0) {
8641 ioc->is_driver_loading = 0;
8642 ioc->wait_for_discovery_to_complete = 0;
8643 return 1;
8644 }
8645
8646 if (time >= (300 * HZ)) {
8647 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8648 pr_info(MPT3SAS_FMT
8649 "port enable: FAILED with timeout (timeout=300s)\n",
8650 ioc->name);
8651 ioc->is_driver_loading = 0;
8652 return 1;
8653 }
8654
8655 if (ioc->start_scan)
8656 return 0;
8657
8658 if (ioc->start_scan_failed) {
8659 pr_info(MPT3SAS_FMT
8660 "port enable: FAILED with (ioc_status=0x%08x)\n",
8661 ioc->name, ioc->start_scan_failed);
8662 ioc->is_driver_loading = 0;
8663 ioc->wait_for_discovery_to_complete = 0;
8664 ioc->remove_host = 1;
8665 return 1;
8666 }
8667
8668 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8669 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8670
8671 if (ioc->wait_for_discovery_to_complete) {
8672 ioc->wait_for_discovery_to_complete = 0;
8673 _scsih_probe_devices(ioc);
8674 }
8675 mpt3sas_base_start_watchdog(ioc);
8676 ioc->is_driver_loading = 0;
8677 return 1;
8678}
8679
c84b06a4
SR
8680/* shost template for SAS 2.0 HBA devices */
8681static struct scsi_host_template mpt2sas_driver_template = {
8682 .module = THIS_MODULE,
8683 .name = "Fusion MPT SAS Host",
8684 .proc_name = MPT2SAS_DRIVER_NAME,
8685 .queuecommand = scsih_qcmd,
8686 .target_alloc = scsih_target_alloc,
8687 .slave_alloc = scsih_slave_alloc,
8688 .slave_configure = scsih_slave_configure,
8689 .target_destroy = scsih_target_destroy,
8690 .slave_destroy = scsih_slave_destroy,
8691 .scan_finished = scsih_scan_finished,
8692 .scan_start = scsih_scan_start,
8693 .change_queue_depth = scsih_change_queue_depth,
8694 .eh_abort_handler = scsih_abort,
8695 .eh_device_reset_handler = scsih_dev_reset,
8696 .eh_target_reset_handler = scsih_target_reset,
8697 .eh_host_reset_handler = scsih_host_reset,
8698 .bios_param = scsih_bios_param,
8699 .can_queue = 1,
8700 .this_id = -1,
8701 .sg_tablesize = MPT2SAS_SG_DEPTH,
8702 .max_sectors = 32767,
8703 .cmd_per_lun = 7,
8704 .use_clustering = ENABLE_CLUSTERING,
8705 .shost_attrs = mpt3sas_host_attrs,
8706 .sdev_attrs = mpt3sas_dev_attrs,
8707 .track_queue_depth = 1,
8708};
8709
8710/* raid transport support for SAS 2.0 HBA devices */
8711static struct raid_function_template mpt2sas_raid_functions = {
8712 .cookie = &mpt2sas_driver_template,
8713 .is_raid = scsih_is_raid,
8714 .get_resync = scsih_get_resync,
8715 .get_state = scsih_get_state,
8716};
d357e84d 8717
c84b06a4
SR
8718/* shost template for SAS 3.0 HBA devices */
8719static struct scsi_host_template mpt3sas_driver_template = {
8720 .module = THIS_MODULE,
8721 .name = "Fusion MPT SAS Host",
8722 .proc_name = MPT3SAS_DRIVER_NAME,
8723 .queuecommand = scsih_qcmd,
8724 .target_alloc = scsih_target_alloc,
8725 .slave_alloc = scsih_slave_alloc,
8726 .slave_configure = scsih_slave_configure,
8727 .target_destroy = scsih_target_destroy,
8728 .slave_destroy = scsih_slave_destroy,
8729 .scan_finished = scsih_scan_finished,
8730 .scan_start = scsih_scan_start,
8731 .change_queue_depth = scsih_change_queue_depth,
8732 .eh_abort_handler = scsih_abort,
8733 .eh_device_reset_handler = scsih_dev_reset,
8734 .eh_target_reset_handler = scsih_target_reset,
8735 .eh_host_reset_handler = scsih_host_reset,
8736 .bios_param = scsih_bios_param,
8737 .can_queue = 1,
8738 .this_id = -1,
8739 .sg_tablesize = MPT3SAS_SG_DEPTH,
8740 .max_sectors = 32767,
8741 .cmd_per_lun = 7,
8742 .use_clustering = ENABLE_CLUSTERING,
8743 .shost_attrs = mpt3sas_host_attrs,
8744 .sdev_attrs = mpt3sas_dev_attrs,
8745 .track_queue_depth = 1,
8746};
8747
8748/* raid transport support for SAS 3.0 HBA devices */
8749static struct raid_function_template mpt3sas_raid_functions = {
8750 .cookie = &mpt3sas_driver_template,
8751 .is_raid = scsih_is_raid,
8752 .get_resync = scsih_get_resync,
8753 .get_state = scsih_get_state,
8754};
8755
8756/**
8757 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8758 * this device belongs to.
8759 * @pdev: PCI device struct
8760 *
8761 * return MPI2_VERSION for SAS 2.0 HBA devices,
b130b0d5
SS
8762 * MPI25_VERSION for SAS 3.0 HBA devices, and
8763 * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
c84b06a4 8764 */
8bbb1cf6 8765static u16
c84b06a4
SR
8766_scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8767{
8768
8769 switch (pdev->device) {
7786ab6a 8770 case MPI2_MFGPAGE_DEVID_SSS6200:
d357e84d
SR
8771 case MPI2_MFGPAGE_DEVID_SAS2004:
8772 case MPI2_MFGPAGE_DEVID_SAS2008:
8773 case MPI2_MFGPAGE_DEVID_SAS2108_1:
8774 case MPI2_MFGPAGE_DEVID_SAS2108_2:
8775 case MPI2_MFGPAGE_DEVID_SAS2108_3:
8776 case MPI2_MFGPAGE_DEVID_SAS2116_1:
8777 case MPI2_MFGPAGE_DEVID_SAS2116_2:
8778 case MPI2_MFGPAGE_DEVID_SAS2208_1:
8779 case MPI2_MFGPAGE_DEVID_SAS2208_2:
8780 case MPI2_MFGPAGE_DEVID_SAS2208_3:
8781 case MPI2_MFGPAGE_DEVID_SAS2208_4:
8782 case MPI2_MFGPAGE_DEVID_SAS2208_5:
8783 case MPI2_MFGPAGE_DEVID_SAS2208_6:
8784 case MPI2_MFGPAGE_DEVID_SAS2308_1:
8785 case MPI2_MFGPAGE_DEVID_SAS2308_2:
8786 case MPI2_MFGPAGE_DEVID_SAS2308_3:
c84b06a4 8787 return MPI2_VERSION;
d357e84d
SR
8788 case MPI25_MFGPAGE_DEVID_SAS3004:
8789 case MPI25_MFGPAGE_DEVID_SAS3008:
8790 case MPI25_MFGPAGE_DEVID_SAS3108_1:
8791 case MPI25_MFGPAGE_DEVID_SAS3108_2:
8792 case MPI25_MFGPAGE_DEVID_SAS3108_5:
8793 case MPI25_MFGPAGE_DEVID_SAS3108_6:
c84b06a4 8794 return MPI25_VERSION;
b130b0d5
SS
8795 case MPI26_MFGPAGE_DEVID_SAS3216:
8796 case MPI26_MFGPAGE_DEVID_SAS3224:
8797 case MPI26_MFGPAGE_DEVID_SAS3316_1:
8798 case MPI26_MFGPAGE_DEVID_SAS3316_2:
8799 case MPI26_MFGPAGE_DEVID_SAS3316_3:
8800 case MPI26_MFGPAGE_DEVID_SAS3316_4:
8801 case MPI26_MFGPAGE_DEVID_SAS3324_1:
8802 case MPI26_MFGPAGE_DEVID_SAS3324_2:
8803 case MPI26_MFGPAGE_DEVID_SAS3324_3:
8804 case MPI26_MFGPAGE_DEVID_SAS3324_4:
998f26ae
SPS
8805 case MPI26_MFGPAGE_DEVID_SAS3508:
8806 case MPI26_MFGPAGE_DEVID_SAS3508_1:
8807 case MPI26_MFGPAGE_DEVID_SAS3408:
8808 case MPI26_MFGPAGE_DEVID_SAS3516:
8809 case MPI26_MFGPAGE_DEVID_SAS3516_1:
8810 case MPI26_MFGPAGE_DEVID_SAS3416:
15fd7c74 8811 case MPI26_MFGPAGE_DEVID_SAS3616:
b130b0d5 8812 return MPI26_VERSION;
d357e84d 8813 }
c84b06a4 8814 return 0;
d357e84d
SR
8815}
8816
f92363d1 8817/**
c84b06a4 8818 * _scsih_probe - attach and add scsi host
f92363d1
SR
8819 * @pdev: PCI device struct
8820 * @id: pci device id
8821 *
8822 * Returns 0 success, anything else error.
8823 */
8bbb1cf6 8824static int
c84b06a4 8825_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
f92363d1
SR
8826{
8827 struct MPT3SAS_ADAPTER *ioc;
c84b06a4 8828 struct Scsi_Host *shost = NULL;
b65f1d4d 8829 int rv;
c84b06a4
SR
8830 u16 hba_mpi_version;
8831
8832 /* Determine in which MPI version class this pci device belongs */
8833 hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8834 if (hba_mpi_version == 0)
8835 return -ENODEV;
8836
8837 /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8838 * for other generation HBA's return with -ENODEV
8839 */
8840 if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
8841 return -ENODEV;
8842
8843 /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8844 * for other generation HBA's return with -ENODEV
8845 */
b130b0d5
SS
8846 if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
8847 || hba_mpi_version == MPI26_VERSION)))
c84b06a4
SR
8848 return -ENODEV;
8849
8850 switch (hba_mpi_version) {
8851 case MPI2_VERSION:
ffdadd68 8852 pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S |
8853 PCIE_LINK_STATE_L1 | PCIE_LINK_STATE_CLKPM);
c84b06a4
SR
8854 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8855 shost = scsi_host_alloc(&mpt2sas_driver_template,
8856 sizeof(struct MPT3SAS_ADAPTER));
8857 if (!shost)
8858 return -ENODEV;
8859 ioc = shost_priv(shost);
8860 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8861 ioc->hba_mpi_version_belonged = hba_mpi_version;
8862 ioc->id = mpt2_ids++;
8863 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8864 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8865 ioc->is_warpdrive = 1;
8866 ioc->hide_ir_msg = 1;
8867 } else
8868 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8869 break;
8870 case MPI25_VERSION:
b130b0d5 8871 case MPI26_VERSION:
c84b06a4
SR
8872 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8873 shost = scsi_host_alloc(&mpt3sas_driver_template,
8874 sizeof(struct MPT3SAS_ADAPTER));
8875 if (!shost)
8876 return -ENODEV;
8877 ioc = shost_priv(shost);
8878 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8879 ioc->hba_mpi_version_belonged = hba_mpi_version;
8880 ioc->id = mpt3_ids++;
8881 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
998f26ae
SPS
8882 switch (pdev->device) {
8883 case MPI26_MFGPAGE_DEVID_SAS3508:
8884 case MPI26_MFGPAGE_DEVID_SAS3508_1:
8885 case MPI26_MFGPAGE_DEVID_SAS3408:
8886 case MPI26_MFGPAGE_DEVID_SAS3516:
8887 case MPI26_MFGPAGE_DEVID_SAS3516_1:
8888 case MPI26_MFGPAGE_DEVID_SAS3416:
15fd7c74 8889 case MPI26_MFGPAGE_DEVID_SAS3616:
998f26ae
SPS
8890 ioc->is_gen35_ioc = 1;
8891 break;
8892 default:
8893 ioc->is_gen35_ioc = 0;
8894 }
b130b0d5
SS
8895 if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8896 pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
0bb337c9
SPS
8897 (ioc->hba_mpi_version_belonged == MPI26_VERSION)) {
8898 ioc->combined_reply_queue = 1;
8899 if (ioc->is_gen35_ioc)
8900 ioc->combined_reply_index_count =
8901 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G35;
8902 else
8903 ioc->combined_reply_index_count =
8904 MPT3_SUP_REPLY_POST_HOST_INDEX_REG_COUNT_G3;
8905 }
c84b06a4
SR
8906 break;
8907 default:
8908 return -ENODEV;
8909 }
f92363d1 8910
f92363d1 8911 INIT_LIST_HEAD(&ioc->list);
08c4d550 8912 spin_lock(&gioc_lock);
f92363d1 8913 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
08c4d550 8914 spin_unlock(&gioc_lock);
f92363d1 8915 ioc->shost = shost;
f92363d1
SR
8916 ioc->pdev = pdev;
8917 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8918 ioc->tm_cb_idx = tm_cb_idx;
8919 ioc->ctl_cb_idx = ctl_cb_idx;
8920 ioc->base_cb_idx = base_cb_idx;
8921 ioc->port_enable_cb_idx = port_enable_cb_idx;
8922 ioc->transport_cb_idx = transport_cb_idx;
8923 ioc->scsih_cb_idx = scsih_cb_idx;
8924 ioc->config_cb_idx = config_cb_idx;
8925 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8926 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8927 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8928 ioc->logging_level = logging_level;
8929 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8930 /* misc semaphores and spin locks */
8931 mutex_init(&ioc->reset_in_progress_mutex);
08c4d550
SR
8932 /* initializing pci_access_mutex lock */
8933 mutex_init(&ioc->pci_access_mutex);
f92363d1
SR
8934 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8935 spin_lock_init(&ioc->scsi_lookup_lock);
8936 spin_lock_init(&ioc->sas_device_lock);
8937 spin_lock_init(&ioc->sas_node_lock);
8938 spin_lock_init(&ioc->fw_event_lock);
8939 spin_lock_init(&ioc->raid_device_lock);
8940 spin_lock_init(&ioc->diag_trigger_lock);
8941
8942 INIT_LIST_HEAD(&ioc->sas_device_list);
8943 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8944 INIT_LIST_HEAD(&ioc->sas_expander_list);
8945 INIT_LIST_HEAD(&ioc->fw_event_list);
8946 INIT_LIST_HEAD(&ioc->raid_device_list);
8947 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8948 INIT_LIST_HEAD(&ioc->delayed_tr_list);
fd0331b3
SS
8949 INIT_LIST_HEAD(&ioc->delayed_sc_list);
8950 INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
f92363d1 8951 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
cf9bd21a 8952 INIT_LIST_HEAD(&ioc->reply_queue_list);
f92363d1 8953
c84b06a4 8954 sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
d357e84d 8955
f92363d1
SR
8956 /* init shost parameters */
8957 shost->max_cmd_len = 32;
8958 shost->max_lun = max_lun;
8959 shost->transportt = mpt3sas_transport_template;
8960 shost->unique_id = ioc->id;
8961
8962 if (max_sectors != 0xFFFF) {
8963 if (max_sectors < 64) {
8964 shost->max_sectors = 64;
8965 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8966 "for max_sectors, range is 64 to 32767. Assigning "
8967 "value of 64.\n", ioc->name, max_sectors);
8968 } else if (max_sectors > 32767) {
8969 shost->max_sectors = 32767;
8970 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8971 "for max_sectors, range is 64 to 32767. Assigning "
8972 "default value of 32767.\n", ioc->name,
8973 max_sectors);
8974 } else {
8975 shost->max_sectors = max_sectors & 0xFFFE;
8976 pr_info(MPT3SAS_FMT
8977 "The max_sectors value is set to %d\n",
8978 ioc->name, shost->max_sectors);
8979 }
8980 }
8981
f92363d1
SR
8982 /* register EEDP capabilities with SCSI layer */
8983 if (prot_mask > 0)
8984 scsi_host_set_prot(shost, prot_mask);
8985 else
8986 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8987 | SHOST_DIF_TYPE2_PROTECTION
8988 | SHOST_DIF_TYPE3_PROTECTION);
8989
8990 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8991
8992 /* event thread */
8993 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
c84b06a4 8994 "fw_event_%s%d", ioc->driver_name, ioc->id);
bdff785e
SR
8995 ioc->firmware_event_thread = alloc_ordered_workqueue(
8996 ioc->firmware_event_name, WQ_MEM_RECLAIM);
f92363d1
SR
8997 if (!ioc->firmware_event_thread) {
8998 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8999 ioc->name, __FILE__, __LINE__, __func__);
b65f1d4d 9000 rv = -ENODEV;
f92363d1
SR
9001 goto out_thread_fail;
9002 }
9003
9004 ioc->is_driver_loading = 1;
9005 if ((mpt3sas_base_attach(ioc))) {
9006 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
9007 ioc->name, __FILE__, __LINE__, __func__);
b65f1d4d 9008 rv = -ENODEV;
f92363d1
SR
9009 goto out_attach_fail;
9010 }
7786ab6a 9011
7786ab6a
SR
9012 if (ioc->is_warpdrive) {
9013 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
9014 ioc->hide_drives = 0;
9015 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
9016 ioc->hide_drives = 1;
9017 else {
c84b06a4 9018 if (mpt3sas_get_num_volumes(ioc))
7786ab6a
SR
9019 ioc->hide_drives = 1;
9020 else
9021 ioc->hide_drives = 0;
9022 }
9023 } else
9024 ioc->hide_drives = 0;
7786ab6a 9025
b65f1d4d
SR
9026 rv = scsi_add_host(shost, &pdev->dev);
9027 if (rv) {
4dc06fd8
RS
9028 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
9029 ioc->name, __FILE__, __LINE__, __func__);
4dc06fd8
RS
9030 goto out_add_shost_fail;
9031 }
9032
f92363d1
SR
9033 scsi_scan_host(shost);
9034 return 0;
4dc06fd8
RS
9035out_add_shost_fail:
9036 mpt3sas_base_detach(ioc);
f92363d1
SR
9037 out_attach_fail:
9038 destroy_workqueue(ioc->firmware_event_thread);
9039 out_thread_fail:
08c4d550 9040 spin_lock(&gioc_lock);
f92363d1 9041 list_del(&ioc->list);
08c4d550 9042 spin_unlock(&gioc_lock);
f92363d1 9043 scsi_host_put(shost);
b65f1d4d 9044 return rv;
f92363d1
SR
9045}
9046
9047#ifdef CONFIG_PM
9048/**
8a7e4c24 9049 * scsih_suspend - power management suspend main entry point
f92363d1
SR
9050 * @pdev: PCI device struct
9051 * @state: PM state change to (usually PCI_D3)
9052 *
9053 * Returns 0 success, anything else error.
9054 */
8bbb1cf6 9055static int
8a7e4c24 9056scsih_suspend(struct pci_dev *pdev, pm_message_t state)
f92363d1
SR
9057{
9058 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9059 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9060 pci_power_t device_state;
9061
9062 mpt3sas_base_stop_watchdog(ioc);
9063 flush_scheduled_work();
9064 scsi_block_requests(shost);
9065 device_state = pci_choose_state(pdev, state);
9066 pr_info(MPT3SAS_FMT
9067 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
9068 ioc->name, pdev, pci_name(pdev), device_state);
9069
9070 pci_save_state(pdev);
9071 mpt3sas_base_free_resources(ioc);
9072 pci_set_power_state(pdev, device_state);
9073 return 0;
9074}
9075
9076/**
8a7e4c24 9077 * scsih_resume - power management resume main entry point
f92363d1
SR
9078 * @pdev: PCI device struct
9079 *
9080 * Returns 0 success, anything else error.
9081 */
8bbb1cf6 9082static int
8a7e4c24 9083scsih_resume(struct pci_dev *pdev)
f92363d1
SR
9084{
9085 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9086 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9087 pci_power_t device_state = pdev->current_state;
9088 int r;
9089
9090 pr_info(MPT3SAS_FMT
9091 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
9092 ioc->name, pdev, pci_name(pdev), device_state);
9093
9094 pci_set_power_state(pdev, PCI_D0);
9095 pci_enable_wake(pdev, PCI_D0, 0);
9096 pci_restore_state(pdev);
9097 ioc->pdev = pdev;
9098 r = mpt3sas_base_map_resources(ioc);
9099 if (r)
9100 return r;
9101
98c56ad3 9102 mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
f92363d1
SR
9103 scsi_unblock_requests(shost);
9104 mpt3sas_base_start_watchdog(ioc);
9105 return 0;
9106}
9107#endif /* CONFIG_PM */
9108
9109/**
8a7e4c24 9110 * scsih_pci_error_detected - Called when a PCI error is detected.
f92363d1
SR
9111 * @pdev: PCI device struct
9112 * @state: PCI channel state
9113 *
9114 * Description: Called when a PCI error is detected.
9115 *
9116 * Return value:
9117 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
9118 */
8bbb1cf6 9119static pci_ers_result_t
8a7e4c24 9120scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
f92363d1
SR
9121{
9122 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9123 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9124
9125 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
9126 ioc->name, state);
9127
9128 switch (state) {
9129 case pci_channel_io_normal:
9130 return PCI_ERS_RESULT_CAN_RECOVER;
9131 case pci_channel_io_frozen:
9132 /* Fatal error, prepare for slot reset */
9133 ioc->pci_error_recovery = 1;
9134 scsi_block_requests(ioc->shost);
9135 mpt3sas_base_stop_watchdog(ioc);
9136 mpt3sas_base_free_resources(ioc);
9137 return PCI_ERS_RESULT_NEED_RESET;
9138 case pci_channel_io_perm_failure:
9139 /* Permanent error, prepare for device removal */
9140 ioc->pci_error_recovery = 1;
9141 mpt3sas_base_stop_watchdog(ioc);
9142 _scsih_flush_running_cmds(ioc);
9143 return PCI_ERS_RESULT_DISCONNECT;
9144 }
9145 return PCI_ERS_RESULT_NEED_RESET;
9146}
9147
9148/**
8a7e4c24 9149 * scsih_pci_slot_reset - Called when PCI slot has been reset.
f92363d1
SR
9150 * @pdev: PCI device struct
9151 *
9152 * Description: This routine is called by the pci error recovery
9153 * code after the PCI slot has been reset, just before we
9154 * should resume normal operations.
9155 */
8bbb1cf6 9156static pci_ers_result_t
8a7e4c24 9157scsih_pci_slot_reset(struct pci_dev *pdev)
f92363d1
SR
9158{
9159 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9160 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9161 int rc;
9162
9163 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
9164 ioc->name);
9165
9166 ioc->pci_error_recovery = 0;
9167 ioc->pdev = pdev;
9168 pci_restore_state(pdev);
9169 rc = mpt3sas_base_map_resources(ioc);
9170 if (rc)
9171 return PCI_ERS_RESULT_DISCONNECT;
9172
98c56ad3 9173 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
f92363d1
SR
9174
9175 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
9176 (rc == 0) ? "success" : "failed");
9177
9178 if (!rc)
9179 return PCI_ERS_RESULT_RECOVERED;
9180 else
9181 return PCI_ERS_RESULT_DISCONNECT;
9182}
9183
9184/**
8a7e4c24 9185 * scsih_pci_resume() - resume normal ops after PCI reset
f92363d1
SR
9186 * @pdev: pointer to PCI device
9187 *
9188 * Called when the error recovery driver tells us that its
9189 * OK to resume normal operation. Use completion to allow
9190 * halted scsi ops to resume.
9191 */
8bbb1cf6 9192static void
8a7e4c24 9193scsih_pci_resume(struct pci_dev *pdev)
f92363d1
SR
9194{
9195 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9196 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9197
9198 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
9199
9200 pci_cleanup_aer_uncorrect_error_status(pdev);
9201 mpt3sas_base_start_watchdog(ioc);
9202 scsi_unblock_requests(ioc->shost);
9203}
9204
9205/**
8a7e4c24 9206 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
f92363d1
SR
9207 * @pdev: pointer to PCI device
9208 */
8bbb1cf6 9209static pci_ers_result_t
8a7e4c24 9210scsih_pci_mmio_enabled(struct pci_dev *pdev)
f92363d1
SR
9211{
9212 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9213 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9214
9215 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9216 ioc->name);
9217
9218 /* TODO - dump whatever for debugging purposes */
9219
83c3d340
KW
9220 /* This called only if scsih_pci_error_detected returns
9221 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
9222 * works, no need to reset slot.
9223 */
9224 return PCI_ERS_RESULT_RECOVERED;
f92363d1
SR
9225}
9226
307d9075
AM
9227/**
9228 * scsih__ncq_prio_supp - Check for NCQ command priority support
9229 * @sdev: scsi device struct
9230 *
9231 * This is called when a user indicates they would like to enable
9232 * ncq command priorities. This works only on SATA devices.
9233 */
9234bool scsih_ncq_prio_supp(struct scsi_device *sdev)
9235{
9236 unsigned char *buf;
9237 bool ncq_prio_supp = false;
9238
9239 if (!scsi_device_supports_vpd(sdev))
9240 return ncq_prio_supp;
9241
9242 buf = kmalloc(SCSI_VPD_PG_LEN, GFP_KERNEL);
9243 if (!buf)
9244 return ncq_prio_supp;
9245
9246 if (!scsi_get_vpd_page(sdev, 0x89, buf, SCSI_VPD_PG_LEN))
9247 ncq_prio_supp = (buf[213] >> 4) & 1;
9248
9249 kfree(buf);
9250 return ncq_prio_supp;
9251}
c84b06a4
SR
9252/*
9253 * The pci device ids are defined in mpi/mpi2_cnfg.h.
9254 */
9255static const struct pci_device_id mpt3sas_pci_table[] = {
9256 /* Spitfire ~ 2004 */
9257 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9258 PCI_ANY_ID, PCI_ANY_ID },
9259 /* Falcon ~ 2008 */
9260 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9261 PCI_ANY_ID, PCI_ANY_ID },
9262 /* Liberator ~ 2108 */
9263 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9264 PCI_ANY_ID, PCI_ANY_ID },
9265 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9266 PCI_ANY_ID, PCI_ANY_ID },
9267 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9268 PCI_ANY_ID, PCI_ANY_ID },
9269 /* Meteor ~ 2116 */
9270 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9271 PCI_ANY_ID, PCI_ANY_ID },
9272 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9273 PCI_ANY_ID, PCI_ANY_ID },
9274 /* Thunderbolt ~ 2208 */
9275 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9276 PCI_ANY_ID, PCI_ANY_ID },
9277 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9278 PCI_ANY_ID, PCI_ANY_ID },
9279 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9280 PCI_ANY_ID, PCI_ANY_ID },
9281 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9282 PCI_ANY_ID, PCI_ANY_ID },
9283 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9284 PCI_ANY_ID, PCI_ANY_ID },
9285 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9286 PCI_ANY_ID, PCI_ANY_ID },
9287 /* Mustang ~ 2308 */
9288 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9289 PCI_ANY_ID, PCI_ANY_ID },
9290 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9291 PCI_ANY_ID, PCI_ANY_ID },
9292 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9293 PCI_ANY_ID, PCI_ANY_ID },
9294 /* SSS6200 */
9295 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9296 PCI_ANY_ID, PCI_ANY_ID },
9297 /* Fury ~ 3004 and 3008 */
9298 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9299 PCI_ANY_ID, PCI_ANY_ID },
9300 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9301 PCI_ANY_ID, PCI_ANY_ID },
9302 /* Invader ~ 3108 */
9303 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9304 PCI_ANY_ID, PCI_ANY_ID },
9305 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9306 PCI_ANY_ID, PCI_ANY_ID },
9307 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9308 PCI_ANY_ID, PCI_ANY_ID },
9309 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9310 PCI_ANY_ID, PCI_ANY_ID },
b130b0d5
SS
9311 /* Cutlass ~ 3216 and 3224 */
9312 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9313 PCI_ANY_ID, PCI_ANY_ID },
9314 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9315 PCI_ANY_ID, PCI_ANY_ID },
9316 /* Intruder ~ 3316 and 3324 */
9317 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9318 PCI_ANY_ID, PCI_ANY_ID },
9319 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9320 PCI_ANY_ID, PCI_ANY_ID },
9321 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9322 PCI_ANY_ID, PCI_ANY_ID },
9323 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9324 PCI_ANY_ID, PCI_ANY_ID },
9325 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9326 PCI_ANY_ID, PCI_ANY_ID },
9327 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9328 PCI_ANY_ID, PCI_ANY_ID },
9329 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9330 PCI_ANY_ID, PCI_ANY_ID },
9331 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9332 PCI_ANY_ID, PCI_ANY_ID },
998f26ae
SPS
9333 /* Ventura, Crusader, Harpoon & Tomcat ~ 3516, 3416, 3508 & 3408*/
9334 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508,
9335 PCI_ANY_ID, PCI_ANY_ID },
9336 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3508_1,
9337 PCI_ANY_ID, PCI_ANY_ID },
9338 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3408,
9339 PCI_ANY_ID, PCI_ANY_ID },
9340 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516,
9341 PCI_ANY_ID, PCI_ANY_ID },
9342 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3516_1,
9343 PCI_ANY_ID, PCI_ANY_ID },
9344 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3416,
9345 PCI_ANY_ID, PCI_ANY_ID },
15fd7c74
SR
9346 /* Mercator ~ 3616*/
9347 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3616,
9348 PCI_ANY_ID, PCI_ANY_ID },
c84b06a4
SR
9349 {0} /* Terminating entry */
9350};
9351MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9352
9353static struct pci_error_handlers _mpt3sas_err_handler = {
9354 .error_detected = scsih_pci_error_detected,
9355 .mmio_enabled = scsih_pci_mmio_enabled,
9356 .slot_reset = scsih_pci_slot_reset,
9357 .resume = scsih_pci_resume,
9358};
9359
9360static struct pci_driver mpt3sas_driver = {
9361 .name = MPT3SAS_DRIVER_NAME,
9362 .id_table = mpt3sas_pci_table,
9363 .probe = _scsih_probe,
9364 .remove = scsih_remove,
9365 .shutdown = scsih_shutdown,
9366 .err_handler = &_mpt3sas_err_handler,
9367#ifdef CONFIG_PM
9368 .suspend = scsih_suspend,
9369 .resume = scsih_resume,
9370#endif
9371};
9372
f92363d1 9373/**
8a7e4c24 9374 * scsih_init - main entry point for this driver.
f92363d1
SR
9375 *
9376 * Returns 0 success, anything else error.
9377 */
8bbb1cf6 9378static int
8a7e4c24 9379scsih_init(void)
f92363d1 9380{
c84b06a4
SR
9381 mpt2_ids = 0;
9382 mpt3_ids = 0;
f92363d1 9383
f92363d1
SR
9384 mpt3sas_base_initialize_callback_handler();
9385
9386 /* queuecommand callback hander */
9387 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9388
6c7abffc 9389 /* task management callback handler */
f92363d1
SR
9390 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9391
9392 /* base internal commands callback handler */
9393 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9394 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9395 mpt3sas_port_enable_done);
9396
9397 /* transport internal commands callback handler */
9398 transport_cb_idx = mpt3sas_base_register_callback_handler(
9399 mpt3sas_transport_done);
9400
9401 /* scsih internal commands callback handler */
9402 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9403
9404 /* configuration page API internal commands callback handler */
9405 config_cb_idx = mpt3sas_base_register_callback_handler(
9406 mpt3sas_config_done);
9407
9408 /* ctl module callback handler */
9409 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9410
9411 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9412 _scsih_tm_tr_complete);
9413
9414 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9415 _scsih_tm_volume_tr_complete);
9416
9417 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9418 _scsih_sas_control_complete);
9419
7497392a 9420 return 0;
f92363d1
SR
9421}
9422
9423/**
7497392a 9424 * scsih_exit - exit point for this driver (when it is a module).
f92363d1
SR
9425 *
9426 * Returns 0 success, anything else error.
9427 */
8bbb1cf6 9428static void
8a7e4c24 9429scsih_exit(void)
f92363d1 9430{
f92363d1
SR
9431
9432 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9433 mpt3sas_base_release_callback_handler(tm_cb_idx);
9434 mpt3sas_base_release_callback_handler(base_cb_idx);
9435 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9436 mpt3sas_base_release_callback_handler(transport_cb_idx);
9437 mpt3sas_base_release_callback_handler(scsih_cb_idx);
9438 mpt3sas_base_release_callback_handler(config_cb_idx);
9439 mpt3sas_base_release_callback_handler(ctl_cb_idx);
9440
9441 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9442 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9443 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9444
9445/* raid transport support */
c84b06a4
SR
9446 if (hbas_to_enumerate != 1)
9447 raid_class_release(mpt3sas_raid_template);
9448 if (hbas_to_enumerate != 2)
9449 raid_class_release(mpt2sas_raid_template);
f92363d1
SR
9450 sas_release_transport(mpt3sas_transport_template);
9451}
7786ab6a 9452
c84b06a4
SR
9453/**
9454 * _mpt3sas_init - main entry point for this driver.
9455 *
9456 * Returns 0 success, anything else error.
9457 */
9458static int __init
9459_mpt3sas_init(void)
9460{
9461 int error;
9462
9463 pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9464 MPT3SAS_DRIVER_VERSION);
9465
9466 mpt3sas_transport_template =
9467 sas_attach_transport(&mpt3sas_transport_functions);
9468 if (!mpt3sas_transport_template)
9469 return -ENODEV;
9470
9471 /* No need attach mpt3sas raid functions template
9472 * if hbas_to_enumarate value is one.
9473 */
9474 if (hbas_to_enumerate != 1) {
9475 mpt3sas_raid_template =
9476 raid_class_attach(&mpt3sas_raid_functions);
9477 if (!mpt3sas_raid_template) {
9478 sas_release_transport(mpt3sas_transport_template);
9479 return -ENODEV;
9480 }
9481 }
9482
9483 /* No need to attach mpt2sas raid functions template
9484 * if hbas_to_enumarate value is two
9485 */
9486 if (hbas_to_enumerate != 2) {
9487 mpt2sas_raid_template =
9488 raid_class_attach(&mpt2sas_raid_functions);
9489 if (!mpt2sas_raid_template) {
9490 sas_release_transport(mpt3sas_transport_template);
9491 return -ENODEV;
9492 }
9493 }
9494
9495 error = scsih_init();
9496 if (error) {
9497 scsih_exit();
9498 return error;
9499 }
9500
9501 mpt3sas_ctl_init(hbas_to_enumerate);
9502
9503 error = pci_register_driver(&mpt3sas_driver);
9504 if (error)
9505 scsih_exit();
9506
9507 return error;
9508}
9509
9510/**
9511 * _mpt3sas_exit - exit point for this driver (when it is a module).
9512 *
9513 */
9514static void __exit
9515_mpt3sas_exit(void)
9516{
9517 pr_info("mpt3sas version %s unloading\n",
9518 MPT3SAS_DRIVER_VERSION);
9519
9520 pci_unregister_driver(&mpt3sas_driver);
9521
9522 mpt3sas_ctl_exit(hbas_to_enumerate);
9523
9524 scsih_exit();
9525}
9526
9527module_init(_mpt3sas_init);
9528module_exit(_mpt3sas_exit);