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