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