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