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