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