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