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