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