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