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