blob: 22d75a2a6672fbef90334ffdd10fd72566ba3c41 [file] [log] [blame]
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt3sas/mpt3sas_scsih.c
Sreekanth Reddya4ffce02014-09-12 15:35:29 +05305 * Copyright (C) 2012-2014 LSI Corporation
Sreekanth Reddya03bd152015-01-12 11:39:02 +05306 * Copyright (C) 2013-2014 Avago Technologies
7 * (mailto: MPT-FusionLinux.pdl@avagotech.com)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308 *
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
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053045#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/interrupt.h>
55#include <linux/aer.h>
56#include <linux/raid_class.h>
Sreekanth Reddy7786ab62015-11-11 17:30:28 +053057#include <asm/unaligned.h>
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053058
59#include "mpt3sas_base.h"
60
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053061#define RAID_CHANNEL 1
62/* forward proto's */
63static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
64 struct _sas_node *sas_expander);
65static void _firmware_event_work(struct work_struct *work);
66
67static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
68 struct _sas_device *sas_device);
69static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
70 u8 retry_count, u8 is_pd);
71
72static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
73
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053074/* global parameters */
75LIST_HEAD(mpt3sas_ioc_list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +053076/* global ioc lock for list operations */
77DEFINE_SPINLOCK(gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053078
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +053079MODULE_AUTHOR(MPT3SAS_AUTHOR);
80MODULE_DESCRIPTION(MPT3SAS_DESCRIPTION);
81MODULE_LICENSE("GPL");
82MODULE_VERSION(MPT3SAS_DRIVER_VERSION);
83MODULE_ALIAS("mpt2sas");
84
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053085/* local parameters */
86static u8 scsi_io_cb_idx = -1;
87static u8 tm_cb_idx = -1;
88static u8 ctl_cb_idx = -1;
89static u8 base_cb_idx = -1;
90static u8 port_enable_cb_idx = -1;
91static u8 transport_cb_idx = -1;
92static u8 scsih_cb_idx = -1;
93static u8 config_cb_idx = -1;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +053094static int mpt2_ids;
95static int mpt3_ids;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053096
97static u8 tm_tr_cb_idx = -1 ;
98static u8 tm_tr_volume_cb_idx = -1 ;
99static u8 tm_sas_control_cb_idx = -1;
100
101/* command line options */
102static u32 logging_level;
103MODULE_PARM_DESC(logging_level,
104 " bits for enabling additional logging info (default=0)");
105
106
107static ushort max_sectors = 0xFFFF;
108module_param(max_sectors, ushort, 0);
109MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
110
111
112static int missing_delay[2] = {-1, -1};
113module_param_array(missing_delay, int, NULL, 0);
114MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
115
116/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
117#define MPT3SAS_MAX_LUN (16895)
Hannes Reinecke1abf6352014-06-25 15:27:38 +0200118static u64 max_lun = MPT3SAS_MAX_LUN;
119module_param(max_lun, ullong, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530120MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
121
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530122static ushort hbas_to_enumerate;
123module_param(hbas_to_enumerate, ushort, 0);
124MODULE_PARM_DESC(hbas_to_enumerate,
125 " 0 - enumerates both SAS 2.0 & SAS 3.0 generation HBAs\n \
126 1 - enumerates only SAS 2.0 generation HBAs\n \
127 2 - enumerates only SAS 3.0 generation HBAs (default=0)");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530128
129/* diag_buffer_enable is bitwise
130 * bit 0 set = TRACE
131 * bit 1 set = SNAPSHOT
132 * bit 2 set = EXTENDED
133 *
134 * Either bit can be set, or both
135 */
136static int diag_buffer_enable = -1;
137module_param(diag_buffer_enable, int, 0);
138MODULE_PARM_DESC(diag_buffer_enable,
139 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
140static int disable_discovery = -1;
141module_param(disable_discovery, int, 0);
142MODULE_PARM_DESC(disable_discovery, " disable discovery ");
143
144
145/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
146static int prot_mask = -1;
147module_param(prot_mask, int, 0);
148MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
149
150
151/* raid transport support */
Sreekanth Reddy7497392a2015-11-11 17:30:19 +0530152struct raid_template *mpt3sas_raid_template;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530153struct raid_template *mpt2sas_raid_template;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530154
155
156/**
157 * struct sense_info - common structure for obtaining sense keys
158 * @skey: sense key
159 * @asc: additional sense code
160 * @ascq: additional sense code qualifier
161 */
162struct sense_info {
163 u8 skey;
164 u8 asc;
165 u8 ascq;
166};
167
168#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
Sreekanth Reddy0f624c32014-09-12 15:35:26 +0530169#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530170#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
171#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
172#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
173/**
174 * struct fw_event_work - firmware event struct
175 * @list: link list framework
176 * @work: work object (ioc->fault_reset_work_q)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530177 * @ioc: per adapter object
178 * @device_handle: device handle
179 * @VF_ID: virtual function id
180 * @VP_ID: virtual port id
181 * @ignore: flag meaning this event has been marked to ignore
Joe Lawrenceb8ac0cc2016-04-18 10:50:12 -0400182 * @event: firmware event MPI2_EVENT_XXX defined in mpi2_ioc.h
183 * @refcount: kref for this event
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530184 * @event_data: reply event data payload follows
185 *
186 * This object stored on ioc->fw_event_list.
187 */
188struct fw_event_work {
189 struct list_head list;
190 struct work_struct work;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530191
192 struct MPT3SAS_ADAPTER *ioc;
193 u16 device_handle;
194 u8 VF_ID;
195 u8 VP_ID;
196 u8 ignore;
197 u16 event;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +0530198 struct kref refcount;
Joe Lawrence35b62362014-06-25 17:05:34 -0400199 char event_data[0] __aligned(4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530200};
201
Sreekanth Reddy146b16c2015-11-11 17:30:31 +0530202static void fw_event_work_free(struct kref *r)
203{
204 kfree(container_of(r, struct fw_event_work, refcount));
205}
206
207static void fw_event_work_get(struct fw_event_work *fw_work)
208{
209 kref_get(&fw_work->refcount);
210}
211
212static void fw_event_work_put(struct fw_event_work *fw_work)
213{
214 kref_put(&fw_work->refcount, fw_event_work_free);
215}
216
217static struct fw_event_work *alloc_fw_event_work(int len)
218{
219 struct fw_event_work *fw_event;
220
221 fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
222 if (!fw_event)
223 return NULL;
224
225 kref_init(&fw_event->refcount);
226 return fw_event;
227}
228
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530229/**
230 * struct _scsi_io_transfer - scsi io transfer
231 * @handle: sas device handle (assigned by firmware)
232 * @is_raid: flag set for hidden raid components
233 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
234 * @data_length: data transfer length
235 * @data_dma: dma pointer to data
236 * @sense: sense data
237 * @lun: lun number
238 * @cdb_length: cdb length
239 * @cdb: cdb contents
240 * @timeout: timeout for this command
241 * @VF_ID: virtual function id
242 * @VP_ID: virtual port id
243 * @valid_reply: flag set for reply message
244 * @sense_length: sense length
245 * @ioc_status: ioc status
246 * @scsi_state: scsi state
247 * @scsi_status: scsi staus
248 * @log_info: log information
249 * @transfer_length: data length transfer when there is a reply message
250 *
251 * Used for sending internal scsi commands to devices within this module.
252 * Refer to _scsi_send_scsi_io().
253 */
254struct _scsi_io_transfer {
255 u16 handle;
256 u8 is_raid;
257 enum dma_data_direction dir;
258 u32 data_length;
259 dma_addr_t data_dma;
260 u8 sense[SCSI_SENSE_BUFFERSIZE];
261 u32 lun;
262 u8 cdb_length;
263 u8 cdb[32];
264 u8 timeout;
265 u8 VF_ID;
266 u8 VP_ID;
267 u8 valid_reply;
268 /* the following bits are only valid when 'valid_reply = 1' */
269 u32 sense_length;
270 u16 ioc_status;
271 u8 scsi_state;
272 u8 scsi_status;
273 u32 log_info;
274 u32 transfer_length;
275};
276
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530277/**
278 * _scsih_set_debug_level - global setting of ioc->logging_level.
279 *
280 * Note: The logging levels are defined in mpt3sas_debug.h.
281 */
282static int
283_scsih_set_debug_level(const char *val, struct kernel_param *kp)
284{
285 int ret = param_set_int(val, kp);
286 struct MPT3SAS_ADAPTER *ioc;
287
288 if (ret)
289 return ret;
290
291 pr_info("setting logging_level(0x%08x)\n", logging_level);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530292 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530293 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
294 ioc->logging_level = logging_level;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530295 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530296 return 0;
297}
298module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
299 &logging_level, 0644);
300
301/**
302 * _scsih_srch_boot_sas_address - search based on sas_address
303 * @sas_address: sas address
304 * @boot_device: boot device object from bios page 2
305 *
306 * Returns 1 when there's a match, 0 means no match.
307 */
308static inline int
309_scsih_srch_boot_sas_address(u64 sas_address,
310 Mpi2BootDeviceSasWwid_t *boot_device)
311{
312 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
313}
314
315/**
316 * _scsih_srch_boot_device_name - search based on device name
317 * @device_name: device name specified in INDENTIFY fram
318 * @boot_device: boot device object from bios page 2
319 *
320 * Returns 1 when there's a match, 0 means no match.
321 */
322static inline int
323_scsih_srch_boot_device_name(u64 device_name,
324 Mpi2BootDeviceDeviceName_t *boot_device)
325{
326 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
327}
328
329/**
330 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
331 * @enclosure_logical_id: enclosure logical id
332 * @slot_number: slot number
333 * @boot_device: boot device object from bios page 2
334 *
335 * Returns 1 when there's a match, 0 means no match.
336 */
337static inline int
338_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
339 Mpi2BootDeviceEnclosureSlot_t *boot_device)
340{
341 return (enclosure_logical_id == le64_to_cpu(boot_device->
342 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
343 SlotNumber)) ? 1 : 0;
344}
345
346/**
347 * _scsih_is_boot_device - search for matching boot device.
348 * @sas_address: sas address
349 * @device_name: device name specified in INDENTIFY fram
350 * @enclosure_logical_id: enclosure logical id
351 * @slot_number: slot number
352 * @form: specifies boot device form
353 * @boot_device: boot device object from bios page 2
354 *
355 * Returns 1 when there's a match, 0 means no match.
356 */
357static int
358_scsih_is_boot_device(u64 sas_address, u64 device_name,
359 u64 enclosure_logical_id, u16 slot, u8 form,
360 Mpi2BiosPage2BootDevice_t *boot_device)
361{
362 int rc = 0;
363
364 switch (form) {
365 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
366 if (!sas_address)
367 break;
368 rc = _scsih_srch_boot_sas_address(
369 sas_address, &boot_device->SasWwid);
370 break;
371 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
372 if (!enclosure_logical_id)
373 break;
374 rc = _scsih_srch_boot_encl_slot(
375 enclosure_logical_id,
376 slot, &boot_device->EnclosureSlot);
377 break;
378 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
379 if (!device_name)
380 break;
381 rc = _scsih_srch_boot_device_name(
382 device_name, &boot_device->DeviceName);
383 break;
384 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
385 break;
386 }
387
388 return rc;
389}
390
391/**
392 * _scsih_get_sas_address - set the sas_address for given device handle
393 * @handle: device handle
394 * @sas_address: sas address
395 *
396 * Returns 0 success, non-zero when failure
397 */
398static int
399_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
400 u64 *sas_address)
401{
402 Mpi2SasDevicePage0_t sas_device_pg0;
403 Mpi2ConfigReply_t mpi_reply;
404 u32 ioc_status;
405
406 *sas_address = 0;
407
408 if (handle <= ioc->sas_hba.num_phys) {
409 *sas_address = ioc->sas_hba.sas_address;
410 return 0;
411 }
412
413 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
414 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
415 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
416 __FILE__, __LINE__, __func__);
417 return -ENXIO;
418 }
419
420 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
421 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
422 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
423 return 0;
424 }
425
426 /* we hit this becuase the given parent handle doesn't exist */
427 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
428 return -ENXIO;
429
430 /* else error case */
431 pr_err(MPT3SAS_FMT
432 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
433 ioc->name, handle, ioc_status,
434 __FILE__, __LINE__, __func__);
435 return -EIO;
436}
437
438/**
439 * _scsih_determine_boot_device - determine boot device.
440 * @ioc: per adapter object
441 * @device: either sas_device or raid_device object
442 * @is_raid: [flag] 1 = raid object, 0 = sas object
443 *
444 * Determines whether this device should be first reported device to
445 * to scsi-ml or sas transport, this purpose is for persistent boot device.
446 * There are primary, alternate, and current entries in bios page 2. The order
447 * priority is primary, alternate, then current. This routine saves
448 * the corresponding device object and is_raid flag in the ioc object.
449 * The saved data to be used later in _scsih_probe_boot_devices().
450 */
451static void
452_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
453 void *device, u8 is_raid)
454{
455 struct _sas_device *sas_device;
456 struct _raid_device *raid_device;
457 u64 sas_address;
458 u64 device_name;
459 u64 enclosure_logical_id;
460 u16 slot;
461
462 /* only process this function when driver loads */
463 if (!ioc->is_driver_loading)
464 return;
465
466 /* no Bios, return immediately */
467 if (!ioc->bios_pg3.BiosVersion)
468 return;
469
470 if (!is_raid) {
471 sas_device = device;
472 sas_address = sas_device->sas_address;
473 device_name = sas_device->device_name;
474 enclosure_logical_id = sas_device->enclosure_logical_id;
475 slot = sas_device->slot;
476 } else {
477 raid_device = device;
478 sas_address = raid_device->wwid;
479 device_name = 0;
480 enclosure_logical_id = 0;
481 slot = 0;
482 }
483
484 if (!ioc->req_boot_device.device) {
485 if (_scsih_is_boot_device(sas_address, device_name,
486 enclosure_logical_id, slot,
487 (ioc->bios_pg2.ReqBootDeviceForm &
488 MPI2_BIOSPAGE2_FORM_MASK),
489 &ioc->bios_pg2.RequestedBootDevice)) {
490 dinitprintk(ioc, pr_info(MPT3SAS_FMT
491 "%s: req_boot_device(0x%016llx)\n",
492 ioc->name, __func__,
493 (unsigned long long)sas_address));
494 ioc->req_boot_device.device = device;
495 ioc->req_boot_device.is_raid = is_raid;
496 }
497 }
498
499 if (!ioc->req_alt_boot_device.device) {
500 if (_scsih_is_boot_device(sas_address, device_name,
501 enclosure_logical_id, slot,
502 (ioc->bios_pg2.ReqAltBootDeviceForm &
503 MPI2_BIOSPAGE2_FORM_MASK),
504 &ioc->bios_pg2.RequestedAltBootDevice)) {
505 dinitprintk(ioc, pr_info(MPT3SAS_FMT
506 "%s: req_alt_boot_device(0x%016llx)\n",
507 ioc->name, __func__,
508 (unsigned long long)sas_address));
509 ioc->req_alt_boot_device.device = device;
510 ioc->req_alt_boot_device.is_raid = is_raid;
511 }
512 }
513
514 if (!ioc->current_boot_device.device) {
515 if (_scsih_is_boot_device(sas_address, device_name,
516 enclosure_logical_id, slot,
517 (ioc->bios_pg2.CurrentBootDeviceForm &
518 MPI2_BIOSPAGE2_FORM_MASK),
519 &ioc->bios_pg2.CurrentBootDevice)) {
520 dinitprintk(ioc, pr_info(MPT3SAS_FMT
521 "%s: current_boot_device(0x%016llx)\n",
522 ioc->name, __func__,
523 (unsigned long long)sas_address));
524 ioc->current_boot_device.device = device;
525 ioc->current_boot_device.is_raid = is_raid;
526 }
527 }
528}
529
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530530static struct _sas_device *
531__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
532 struct MPT3SAS_TARGET *tgt_priv)
533{
534 struct _sas_device *ret;
535
536 assert_spin_locked(&ioc->sas_device_lock);
537
538 ret = tgt_priv->sdev;
539 if (ret)
540 sas_device_get(ret);
541
542 return ret;
543}
544
545static struct _sas_device *
546mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
547 struct MPT3SAS_TARGET *tgt_priv)
548{
549 struct _sas_device *ret;
550 unsigned long flags;
551
552 spin_lock_irqsave(&ioc->sas_device_lock, flags);
553 ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
554 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
555
556 return ret;
557}
558
559
560struct _sas_device *
561__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
562 u64 sas_address)
563{
564 struct _sas_device *sas_device;
565
566 assert_spin_locked(&ioc->sas_device_lock);
567
568 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
569 if (sas_device->sas_address == sas_address)
570 goto found_device;
571
572 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
573 if (sas_device->sas_address == sas_address)
574 goto found_device;
575
576 return NULL;
577
578found_device:
579 sas_device_get(sas_device);
580 return sas_device;
581}
582
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530583/**
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530584 * mpt3sas_get_sdev_by_addr - sas device search
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530585 * @ioc: per adapter object
586 * @sas_address: sas address
587 * Context: Calling function should acquire ioc->sas_device_lock
588 *
589 * This searches for sas_device based on sas_address, then return sas_device
590 * object.
591 */
592struct _sas_device *
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530593mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530594 u64 sas_address)
595{
596 struct _sas_device *sas_device;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530597 unsigned long flags;
598
599 spin_lock_irqsave(&ioc->sas_device_lock, flags);
600 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
601 sas_address);
602 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
603
604 return sas_device;
605}
606
607static struct _sas_device *
608__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
609{
610 struct _sas_device *sas_device;
611
612 assert_spin_locked(&ioc->sas_device_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530613
614 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530615 if (sas_device->handle == handle)
616 goto found_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530617
618 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530619 if (sas_device->handle == handle)
620 goto found_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530621
622 return NULL;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530623
624found_device:
625 sas_device_get(sas_device);
626 return sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530627}
628
629/**
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530630 * mpt3sas_get_sdev_by_handle - sas device search
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530631 * @ioc: per adapter object
632 * @handle: sas device handle (assigned by firmware)
633 * Context: Calling function should acquire ioc->sas_device_lock
634 *
635 * This searches for sas_device based on sas_address, then return sas_device
636 * object.
637 */
638static struct _sas_device *
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530639mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530640{
641 struct _sas_device *sas_device;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530642 unsigned long flags;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530643
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530644 spin_lock_irqsave(&ioc->sas_device_lock, flags);
645 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
646 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530647
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530648 return sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530649}
650
651/**
652 * _scsih_sas_device_remove - remove sas_device from list.
653 * @ioc: per adapter object
654 * @sas_device: the sas_device object
655 * Context: This function will acquire ioc->sas_device_lock.
656 *
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530657 * If sas_device is on the list, remove it and decrement its reference count.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530658 */
659static void
660_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
661 struct _sas_device *sas_device)
662{
663 unsigned long flags;
664
665 if (!sas_device)
666 return;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530667 pr_info(MPT3SAS_FMT
668 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
669 ioc->name, sas_device->handle,
670 (unsigned long long) sas_device->sas_address);
671
672 if (sas_device->enclosure_handle != 0)
673 pr_info(MPT3SAS_FMT
674 "removing enclosure logical id(0x%016llx), slot(%d)\n",
675 ioc->name, (unsigned long long)
676 sas_device->enclosure_logical_id, sas_device->slot);
677
678 if (sas_device->connector_name[0] != '\0')
679 pr_info(MPT3SAS_FMT
680 "removing enclosure level(0x%04x), connector name( %s)\n",
681 ioc->name, sas_device->enclosure_level,
682 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530683
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530684 /*
685 * The lock serializes access to the list, but we still need to verify
686 * that nobody removed the entry while we were waiting on the lock.
687 */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530688 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530689 if (!list_empty(&sas_device->list)) {
690 list_del_init(&sas_device->list);
691 sas_device_put(sas_device);
692 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530693 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
694}
695
696/**
697 * _scsih_device_remove_by_handle - removing device object by handle
698 * @ioc: per adapter object
699 * @handle: device handle
700 *
701 * Return nothing.
702 */
703static void
704_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
705{
706 struct _sas_device *sas_device;
707 unsigned long flags;
708
709 if (ioc->shost_recovery)
710 return;
711
712 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530713 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
714 if (sas_device) {
715 list_del_init(&sas_device->list);
716 sas_device_put(sas_device);
717 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530718 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530719 if (sas_device) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530720 _scsih_remove_device(ioc, sas_device);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530721 sas_device_put(sas_device);
722 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530723}
724
725/**
726 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
727 * @ioc: per adapter object
728 * @sas_address: device sas_address
729 *
730 * Return nothing.
731 */
732void
733mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
734 u64 sas_address)
735{
736 struct _sas_device *sas_device;
737 unsigned long flags;
738
739 if (ioc->shost_recovery)
740 return;
741
742 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530743 sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
744 if (sas_device) {
745 list_del_init(&sas_device->list);
746 sas_device_put(sas_device);
747 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530748 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530749 if (sas_device) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530750 _scsih_remove_device(ioc, sas_device);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530751 sas_device_put(sas_device);
752 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530753}
754
755/**
756 * _scsih_sas_device_add - insert sas_device to the list.
757 * @ioc: per adapter object
758 * @sas_device: the sas_device object
759 * Context: This function will acquire ioc->sas_device_lock.
760 *
761 * Adding new object to the ioc->sas_device_list.
762 */
763static void
764_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
765 struct _sas_device *sas_device)
766{
767 unsigned long flags;
768
769 dewtprintk(ioc, pr_info(MPT3SAS_FMT
770 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
771 ioc->name, __func__, sas_device->handle,
772 (unsigned long long)sas_device->sas_address));
773
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530774 if (sas_device->enclosure_handle != 0)
775 dewtprintk(ioc, pr_info(MPT3SAS_FMT
776 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
777 ioc->name, __func__, (unsigned long long)
778 sas_device->enclosure_logical_id, sas_device->slot));
779
780 if (sas_device->connector_name[0] != '\0')
781 dewtprintk(ioc, pr_info(MPT3SAS_FMT
782 "%s: enclosure level(0x%04x), connector name( %s)\n",
783 ioc->name, __func__,
784 sas_device->enclosure_level, sas_device->connector_name));
785
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530786 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530787 sas_device_get(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530788 list_add_tail(&sas_device->list, &ioc->sas_device_list);
789 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
790
791 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
792 sas_device->sas_address_parent)) {
793 _scsih_sas_device_remove(ioc, sas_device);
794 } else if (!sas_device->starget) {
795 /*
796 * When asyn scanning is enabled, its not possible to remove
797 * devices while scanning is turned on due to an oops in
798 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
799 */
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +0530800 if (!ioc->is_driver_loading) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530801 mpt3sas_transport_port_remove(ioc,
802 sas_device->sas_address,
803 sas_device->sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +0530804 _scsih_sas_device_remove(ioc, sas_device);
805 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530806 }
807}
808
809/**
810 * _scsih_sas_device_init_add - insert sas_device to the list.
811 * @ioc: per adapter object
812 * @sas_device: the sas_device object
813 * Context: This function will acquire ioc->sas_device_lock.
814 *
815 * Adding new object at driver load time to the ioc->sas_device_init_list.
816 */
817static void
818_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
819 struct _sas_device *sas_device)
820{
821 unsigned long flags;
822
823 dewtprintk(ioc, pr_info(MPT3SAS_FMT
824 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
825 __func__, sas_device->handle,
826 (unsigned long long)sas_device->sas_address));
827
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530828 if (sas_device->enclosure_handle != 0)
829 dewtprintk(ioc, pr_info(MPT3SAS_FMT
830 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
831 ioc->name, __func__, (unsigned long long)
832 sas_device->enclosure_logical_id, sas_device->slot));
833
834 if (sas_device->connector_name[0] != '\0')
835 dewtprintk(ioc, pr_info(MPT3SAS_FMT
836 "%s: enclosure level(0x%04x), connector name( %s)\n",
837 ioc->name, __func__, sas_device->enclosure_level,
838 sas_device->connector_name));
839
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530840 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530841 sas_device_get(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530842 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
843 _scsih_determine_boot_device(ioc, sas_device, 0);
844 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
845}
846
847/**
848 * _scsih_raid_device_find_by_id - raid device search
849 * @ioc: per adapter object
850 * @id: sas device target id
851 * @channel: sas device channel
852 * Context: Calling function should acquire ioc->raid_device_lock
853 *
854 * This searches for raid_device based on target id, then return raid_device
855 * object.
856 */
857static struct _raid_device *
858_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
859{
860 struct _raid_device *raid_device, *r;
861
862 r = NULL;
863 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
864 if (raid_device->id == id && raid_device->channel == channel) {
865 r = raid_device;
866 goto out;
867 }
868 }
869
870 out:
871 return r;
872}
873
874/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530875 * mpt3sas_raid_device_find_by_handle - raid device search
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530876 * @ioc: per adapter object
877 * @handle: sas device handle (assigned by firmware)
878 * Context: Calling function should acquire ioc->raid_device_lock
879 *
880 * This searches for raid_device based on handle, then return raid_device
881 * object.
882 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530883struct _raid_device *
884mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530885{
886 struct _raid_device *raid_device, *r;
887
888 r = NULL;
889 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
890 if (raid_device->handle != handle)
891 continue;
892 r = raid_device;
893 goto out;
894 }
895
896 out:
897 return r;
898}
899
900/**
901 * _scsih_raid_device_find_by_wwid - raid device search
902 * @ioc: per adapter object
903 * @handle: sas device handle (assigned by firmware)
904 * Context: Calling function should acquire ioc->raid_device_lock
905 *
906 * This searches for raid_device based on wwid, then return raid_device
907 * object.
908 */
909static struct _raid_device *
910_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
911{
912 struct _raid_device *raid_device, *r;
913
914 r = NULL;
915 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
916 if (raid_device->wwid != wwid)
917 continue;
918 r = raid_device;
919 goto out;
920 }
921
922 out:
923 return r;
924}
925
926/**
927 * _scsih_raid_device_add - add raid_device object
928 * @ioc: per adapter object
929 * @raid_device: raid_device object
930 *
931 * This is added to the raid_device_list link list.
932 */
933static void
934_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
935 struct _raid_device *raid_device)
936{
937 unsigned long flags;
938
939 dewtprintk(ioc, pr_info(MPT3SAS_FMT
940 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
941 raid_device->handle, (unsigned long long)raid_device->wwid));
942
943 spin_lock_irqsave(&ioc->raid_device_lock, flags);
944 list_add_tail(&raid_device->list, &ioc->raid_device_list);
945 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
946}
947
948/**
949 * _scsih_raid_device_remove - delete raid_device object
950 * @ioc: per adapter object
951 * @raid_device: raid_device object
952 *
953 */
954static void
955_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
956 struct _raid_device *raid_device)
957{
958 unsigned long flags;
959
960 spin_lock_irqsave(&ioc->raid_device_lock, flags);
961 list_del(&raid_device->list);
962 kfree(raid_device);
963 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
964}
965
966/**
967 * mpt3sas_scsih_expander_find_by_handle - expander device search
968 * @ioc: per adapter object
969 * @handle: expander handle (assigned by firmware)
970 * Context: Calling function should acquire ioc->sas_device_lock
971 *
972 * This searches for expander device based on handle, then returns the
973 * sas_node object.
974 */
975struct _sas_node *
976mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
977{
978 struct _sas_node *sas_expander, *r;
979
980 r = NULL;
981 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
982 if (sas_expander->handle != handle)
983 continue;
984 r = sas_expander;
985 goto out;
986 }
987 out:
988 return r;
989}
990
991/**
992 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
993 * @ioc: per adapter object
994 * @sas_address: sas address
995 * Context: Calling function should acquire ioc->sas_node_lock.
996 *
997 * This searches for expander device based on sas_address, then returns the
998 * sas_node object.
999 */
1000struct _sas_node *
1001mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1002 u64 sas_address)
1003{
1004 struct _sas_node *sas_expander, *r;
1005
1006 r = NULL;
1007 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1008 if (sas_expander->sas_address != sas_address)
1009 continue;
1010 r = sas_expander;
1011 goto out;
1012 }
1013 out:
1014 return r;
1015}
1016
1017/**
1018 * _scsih_expander_node_add - insert expander device to the list.
1019 * @ioc: per adapter object
1020 * @sas_expander: the sas_device object
1021 * Context: This function will acquire ioc->sas_node_lock.
1022 *
1023 * Adding new object to the ioc->sas_expander_list.
1024 *
1025 * Return nothing.
1026 */
1027static void
1028_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1029 struct _sas_node *sas_expander)
1030{
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1034 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1035 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1036}
1037
1038/**
1039 * _scsih_is_end_device - determines if device is an end device
1040 * @device_info: bitfield providing information about the device.
1041 * Context: none
1042 *
1043 * Returns 1 if end device.
1044 */
1045static int
1046_scsih_is_end_device(u32 device_info)
1047{
1048 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1049 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1050 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1051 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1052 return 1;
1053 else
1054 return 0;
1055}
1056
1057/**
1058 * _scsih_scsi_lookup_get - returns scmd entry
1059 * @ioc: per adapter object
1060 * @smid: system request message index
1061 *
1062 * Returns the smid stored scmd pointer.
1063 */
1064static struct scsi_cmnd *
1065_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1066{
1067 return ioc->scsi_lookup[smid - 1].scmd;
1068}
1069
1070/**
1071 * _scsih_scsi_lookup_get_clear - returns scmd entry
1072 * @ioc: per adapter object
1073 * @smid: system request message index
1074 *
1075 * Returns the smid stored scmd pointer.
1076 * Then will derefrence the stored scmd pointer.
1077 */
1078static inline struct scsi_cmnd *
1079_scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1080{
1081 unsigned long flags;
1082 struct scsi_cmnd *scmd;
1083
1084 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1085 scmd = ioc->scsi_lookup[smid - 1].scmd;
1086 ioc->scsi_lookup[smid - 1].scmd = NULL;
1087 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1088
1089 return scmd;
1090}
1091
1092/**
1093 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1094 * @ioc: per adapter object
1095 * @smid: system request message index
1096 * @scmd: pointer to scsi command object
1097 * Context: This function will acquire ioc->scsi_lookup_lock.
1098 *
1099 * This will search for a scmd pointer in the scsi_lookup array,
1100 * returning the revelent smid. A returned value of zero means invalid.
1101 */
1102static u16
1103_scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1104 *scmd)
1105{
1106 u16 smid;
1107 unsigned long flags;
1108 int i;
1109
1110 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1111 smid = 0;
1112 for (i = 0; i < ioc->scsiio_depth; i++) {
1113 if (ioc->scsi_lookup[i].scmd == scmd) {
1114 smid = ioc->scsi_lookup[i].smid;
1115 goto out;
1116 }
1117 }
1118 out:
1119 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1120 return smid;
1121}
1122
1123/**
1124 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1125 * @ioc: per adapter object
1126 * @id: target id
1127 * @channel: channel
1128 * Context: This function will acquire ioc->scsi_lookup_lock.
1129 *
1130 * This will search for a matching channel:id in the scsi_lookup array,
1131 * returning 1 if found.
1132 */
1133static u8
1134_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1135 int channel)
1136{
1137 u8 found;
1138 unsigned long flags;
1139 int i;
1140
1141 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1142 found = 0;
1143 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1144 if (ioc->scsi_lookup[i].scmd &&
1145 (ioc->scsi_lookup[i].scmd->device->id == id &&
1146 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1147 found = 1;
1148 goto out;
1149 }
1150 }
1151 out:
1152 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1153 return found;
1154}
1155
1156/**
1157 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1158 * @ioc: per adapter object
1159 * @id: target id
1160 * @lun: lun number
1161 * @channel: channel
1162 * Context: This function will acquire ioc->scsi_lookup_lock.
1163 *
1164 * This will search for a matching channel:id:lun in the scsi_lookup array,
1165 * returning 1 if found.
1166 */
1167static u8
1168_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1169 unsigned int lun, int channel)
1170{
1171 u8 found;
1172 unsigned long flags;
1173 int i;
1174
1175 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1176 found = 0;
1177 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1178 if (ioc->scsi_lookup[i].scmd &&
1179 (ioc->scsi_lookup[i].scmd->device->id == id &&
1180 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1181 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1182 found = 1;
1183 goto out;
1184 }
1185 }
1186 out:
1187 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1188 return found;
1189}
1190
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001191/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301192 * scsih_change_queue_depth - setting device queue depth
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001193 * @sdev: scsi device struct
1194 * @qdepth: requested queue depth
1195 *
1196 * Returns queue depth.
1197 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301198int
1199scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301200{
1201 struct Scsi_Host *shost = sdev->host;
1202 int max_depth;
1203 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1204 struct MPT3SAS_DEVICE *sas_device_priv_data;
1205 struct MPT3SAS_TARGET *sas_target_priv_data;
1206 struct _sas_device *sas_device;
1207 unsigned long flags;
1208
1209 max_depth = shost->can_queue;
1210
1211 /* limit max device queue for SATA to 32 */
1212 sas_device_priv_data = sdev->hostdata;
1213 if (!sas_device_priv_data)
1214 goto not_sata;
1215 sas_target_priv_data = sas_device_priv_data->sas_target;
1216 if (!sas_target_priv_data)
1217 goto not_sata;
1218 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1219 goto not_sata;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301220
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301221 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301222 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1223 if (sas_device) {
1224 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1225 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1226
1227 sas_device_put(sas_device);
1228 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301229 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1230
1231 not_sata:
1232
1233 if (!sdev->tagged_supported)
1234 max_depth = 1;
1235 if (qdepth > max_depth)
1236 qdepth = max_depth;
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001237 return scsi_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301238}
1239
1240/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301241 * scsih_target_alloc - target add routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301242 * @starget: scsi target struct
1243 *
1244 * Returns 0 if ok. Any other return is assumed to be an error and
1245 * the device is ignored.
1246 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301247int
1248scsih_target_alloc(struct scsi_target *starget)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301249{
1250 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1251 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1252 struct MPT3SAS_TARGET *sas_target_priv_data;
1253 struct _sas_device *sas_device;
1254 struct _raid_device *raid_device;
1255 unsigned long flags;
1256 struct sas_rphy *rphy;
1257
Joe Lawrence62c4da42014-06-25 17:04:22 -04001258 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1259 GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301260 if (!sas_target_priv_data)
1261 return -ENOMEM;
1262
1263 starget->hostdata = sas_target_priv_data;
1264 sas_target_priv_data->starget = starget;
1265 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1266
1267 /* RAID volumes */
1268 if (starget->channel == RAID_CHANNEL) {
1269 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1270 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1271 starget->channel);
1272 if (raid_device) {
1273 sas_target_priv_data->handle = raid_device->handle;
1274 sas_target_priv_data->sas_address = raid_device->wwid;
1275 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301276 sas_target_priv_data->raid_device = raid_device;
1277 if (ioc->is_warpdrive)
1278 raid_device->starget = starget;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301279 }
1280 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1281 return 0;
1282 }
1283
1284 /* sas/sata devices */
1285 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1286 rphy = dev_to_rphy(starget->dev.parent);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301287 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301288 rphy->identify.sas_address);
1289
1290 if (sas_device) {
1291 sas_target_priv_data->handle = sas_device->handle;
1292 sas_target_priv_data->sas_address = sas_device->sas_address;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301293 sas_target_priv_data->sdev = sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301294 sas_device->starget = starget;
1295 sas_device->id = starget->id;
1296 sas_device->channel = starget->channel;
1297 if (test_bit(sas_device->handle, ioc->pd_handles))
1298 sas_target_priv_data->flags |=
1299 MPT_TARGET_FLAGS_RAID_COMPONENT;
1300 if (sas_device->fast_path)
1301 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1302 }
1303 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1304
1305 return 0;
1306}
1307
1308/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301309 * scsih_target_destroy - target destroy routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301310 * @starget: scsi target struct
1311 *
1312 * Returns nothing.
1313 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301314void
1315scsih_target_destroy(struct scsi_target *starget)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301316{
1317 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1318 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1319 struct MPT3SAS_TARGET *sas_target_priv_data;
1320 struct _sas_device *sas_device;
1321 struct _raid_device *raid_device;
1322 unsigned long flags;
1323 struct sas_rphy *rphy;
1324
1325 sas_target_priv_data = starget->hostdata;
1326 if (!sas_target_priv_data)
1327 return;
1328
1329 if (starget->channel == RAID_CHANNEL) {
1330 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1331 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1332 starget->channel);
1333 if (raid_device) {
1334 raid_device->starget = NULL;
1335 raid_device->sdev = NULL;
1336 }
1337 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1338 goto out;
1339 }
1340
1341 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1342 rphy = dev_to_rphy(starget->dev.parent);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301343 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301344 if (sas_device && (sas_device->starget == starget) &&
1345 (sas_device->id == starget->id) &&
1346 (sas_device->channel == starget->channel))
1347 sas_device->starget = NULL;
1348
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301349 if (sas_device) {
1350 /*
1351 * Corresponding get() is in _scsih_target_alloc()
1352 */
1353 sas_target_priv_data->sdev = NULL;
1354 sas_device_put(sas_device);
1355
1356 sas_device_put(sas_device);
1357 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301358 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1359
1360 out:
1361 kfree(sas_target_priv_data);
1362 starget->hostdata = NULL;
1363}
1364
1365/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301366 * scsih_slave_alloc - device add routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301367 * @sdev: scsi device struct
1368 *
1369 * Returns 0 if ok. Any other return is assumed to be an error and
1370 * the device is ignored.
1371 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301372int
1373scsih_slave_alloc(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301374{
1375 struct Scsi_Host *shost;
1376 struct MPT3SAS_ADAPTER *ioc;
1377 struct MPT3SAS_TARGET *sas_target_priv_data;
1378 struct MPT3SAS_DEVICE *sas_device_priv_data;
1379 struct scsi_target *starget;
1380 struct _raid_device *raid_device;
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301381 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301382 unsigned long flags;
1383
Joe Lawrence62c4da42014-06-25 17:04:22 -04001384 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1385 GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301386 if (!sas_device_priv_data)
1387 return -ENOMEM;
1388
1389 sas_device_priv_data->lun = sdev->lun;
1390 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1391
1392 starget = scsi_target(sdev);
1393 sas_target_priv_data = starget->hostdata;
1394 sas_target_priv_data->num_luns++;
1395 sas_device_priv_data->sas_target = sas_target_priv_data;
1396 sdev->hostdata = sas_device_priv_data;
1397 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1398 sdev->no_uld_attach = 1;
1399
1400 shost = dev_to_shost(&starget->dev);
1401 ioc = shost_priv(shost);
1402 if (starget->channel == RAID_CHANNEL) {
1403 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1404 raid_device = _scsih_raid_device_find_by_id(ioc,
1405 starget->id, starget->channel);
1406 if (raid_device)
1407 raid_device->sdev = sdev; /* raid is single lun */
1408 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1409 }
1410
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301411 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1412 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301413 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301414 sas_target_priv_data->sas_address);
1415 if (sas_device && (sas_device->starget == NULL)) {
1416 sdev_printk(KERN_INFO, sdev,
1417 "%s : sas_device->starget set to starget @ %d\n",
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301418 __func__, __LINE__);
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301419 sas_device->starget = starget;
1420 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301421
1422 if (sas_device)
1423 sas_device_put(sas_device);
1424
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301425 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1426 }
1427
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301428 return 0;
1429}
1430
1431/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301432 * scsih_slave_destroy - device destroy routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301433 * @sdev: scsi device struct
1434 *
1435 * Returns nothing.
1436 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301437void
1438scsih_slave_destroy(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301439{
1440 struct MPT3SAS_TARGET *sas_target_priv_data;
1441 struct scsi_target *starget;
1442 struct Scsi_Host *shost;
1443 struct MPT3SAS_ADAPTER *ioc;
1444 struct _sas_device *sas_device;
1445 unsigned long flags;
1446
1447 if (!sdev->hostdata)
1448 return;
1449
1450 starget = scsi_target(sdev);
1451 sas_target_priv_data = starget->hostdata;
1452 sas_target_priv_data->num_luns--;
1453
1454 shost = dev_to_shost(&starget->dev);
1455 ioc = shost_priv(shost);
1456
1457 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1458 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301459 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1460 sas_target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301461 if (sas_device && !sas_target_priv_data->num_luns)
1462 sas_device->starget = NULL;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301463
1464 if (sas_device)
1465 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301466 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1467 }
1468
1469 kfree(sdev->hostdata);
1470 sdev->hostdata = NULL;
1471}
1472
1473/**
1474 * _scsih_display_sata_capabilities - sata capabilities
1475 * @ioc: per adapter object
1476 * @handle: device handle
1477 * @sdev: scsi device struct
1478 */
1479static void
1480_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1481 u16 handle, struct scsi_device *sdev)
1482{
1483 Mpi2ConfigReply_t mpi_reply;
1484 Mpi2SasDevicePage0_t sas_device_pg0;
1485 u32 ioc_status;
1486 u16 flags;
1487 u32 device_info;
1488
1489 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1490 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1491 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1492 ioc->name, __FILE__, __LINE__, __func__);
1493 return;
1494 }
1495
1496 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1497 MPI2_IOCSTATUS_MASK;
1498 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1499 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1500 ioc->name, __FILE__, __LINE__, __func__);
1501 return;
1502 }
1503
1504 flags = le16_to_cpu(sas_device_pg0.Flags);
1505 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1506
1507 sdev_printk(KERN_INFO, sdev,
1508 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1509 "sw_preserve(%s)\n",
1510 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1511 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1512 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1513 "n",
1514 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1515 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1516 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1517}
1518
1519/*
1520 * raid transport support -
1521 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1522 * unloading the driver followed by a load - I beleive that the subroutine
1523 * raid_class_release() is not cleaning up properly.
1524 */
1525
1526/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301527 * scsih_is_raid - return boolean indicating device is raid volume
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301528 * @dev the device struct object
1529 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301530int
1531scsih_is_raid(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301532{
1533 struct scsi_device *sdev = to_scsi_device(dev);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301534 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301535
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301536 if (ioc->is_warpdrive)
1537 return 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301538 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1539}
1540
1541/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301542 * scsih_get_resync - get raid volume resync percent complete
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301543 * @dev the device struct object
1544 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301545void
1546scsih_get_resync(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301547{
1548 struct scsi_device *sdev = to_scsi_device(dev);
1549 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1550 static struct _raid_device *raid_device;
1551 unsigned long flags;
1552 Mpi2RaidVolPage0_t vol_pg0;
1553 Mpi2ConfigReply_t mpi_reply;
1554 u32 volume_status_flags;
1555 u8 percent_complete;
1556 u16 handle;
1557
1558 percent_complete = 0;
1559 handle = 0;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301560 if (ioc->is_warpdrive)
1561 goto out;
1562
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301563 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1564 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1565 sdev->channel);
1566 if (raid_device) {
1567 handle = raid_device->handle;
1568 percent_complete = raid_device->percent_complete;
1569 }
1570 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1571
1572 if (!handle)
1573 goto out;
1574
1575 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1576 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1577 sizeof(Mpi2RaidVolPage0_t))) {
1578 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1579 ioc->name, __FILE__, __LINE__, __func__);
1580 percent_complete = 0;
1581 goto out;
1582 }
1583
1584 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1585 if (!(volume_status_flags &
1586 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1587 percent_complete = 0;
1588
1589 out:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301590
1591 switch (ioc->hba_mpi_version_belonged) {
1592 case MPI2_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301593 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301594 break;
1595 case MPI25_VERSION:
1596 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301597 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301598 break;
1599 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301600}
1601
1602/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301603 * scsih_get_state - get raid volume level
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301604 * @dev the device struct object
1605 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301606void
1607scsih_get_state(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301608{
1609 struct scsi_device *sdev = to_scsi_device(dev);
1610 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1611 static struct _raid_device *raid_device;
1612 unsigned long flags;
1613 Mpi2RaidVolPage0_t vol_pg0;
1614 Mpi2ConfigReply_t mpi_reply;
1615 u32 volstate;
1616 enum raid_state state = RAID_STATE_UNKNOWN;
1617 u16 handle = 0;
1618
1619 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1620 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1621 sdev->channel);
1622 if (raid_device)
1623 handle = raid_device->handle;
1624 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1625
1626 if (!raid_device)
1627 goto out;
1628
1629 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1630 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1631 sizeof(Mpi2RaidVolPage0_t))) {
1632 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1633 ioc->name, __FILE__, __LINE__, __func__);
1634 goto out;
1635 }
1636
1637 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1638 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1639 state = RAID_STATE_RESYNCING;
1640 goto out;
1641 }
1642
1643 switch (vol_pg0.VolumeState) {
1644 case MPI2_RAID_VOL_STATE_OPTIMAL:
1645 case MPI2_RAID_VOL_STATE_ONLINE:
1646 state = RAID_STATE_ACTIVE;
1647 break;
1648 case MPI2_RAID_VOL_STATE_DEGRADED:
1649 state = RAID_STATE_DEGRADED;
1650 break;
1651 case MPI2_RAID_VOL_STATE_FAILED:
1652 case MPI2_RAID_VOL_STATE_MISSING:
1653 state = RAID_STATE_OFFLINE;
1654 break;
1655 }
1656 out:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301657 switch (ioc->hba_mpi_version_belonged) {
1658 case MPI2_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301659 raid_set_state(mpt2sas_raid_template, dev, state);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301660 break;
1661 case MPI25_VERSION:
1662 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301663 raid_set_state(mpt3sas_raid_template, dev, state);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301664 break;
1665 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301666}
1667
1668/**
1669 * _scsih_set_level - set raid level
1670 * @sdev: scsi device struct
1671 * @volume_type: volume type
1672 */
1673static void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301674_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1675 struct scsi_device *sdev, u8 volume_type)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301676{
1677 enum raid_level level = RAID_LEVEL_UNKNOWN;
1678
1679 switch (volume_type) {
1680 case MPI2_RAID_VOL_TYPE_RAID0:
1681 level = RAID_LEVEL_0;
1682 break;
1683 case MPI2_RAID_VOL_TYPE_RAID10:
1684 level = RAID_LEVEL_10;
1685 break;
1686 case MPI2_RAID_VOL_TYPE_RAID1E:
1687 level = RAID_LEVEL_1E;
1688 break;
1689 case MPI2_RAID_VOL_TYPE_RAID1:
1690 level = RAID_LEVEL_1;
1691 break;
1692 }
1693
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301694 switch (ioc->hba_mpi_version_belonged) {
1695 case MPI2_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301696 raid_set_level(mpt2sas_raid_template,
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301697 &sdev->sdev_gendev, level);
1698 break;
1699 case MPI25_VERSION:
1700 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301701 raid_set_level(mpt3sas_raid_template,
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301702 &sdev->sdev_gendev, level);
1703 break;
1704 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301705}
1706
1707
1708/**
1709 * _scsih_get_volume_capabilities - volume capabilities
1710 * @ioc: per adapter object
1711 * @sas_device: the raid_device object
1712 *
1713 * Returns 0 for success, else 1
1714 */
1715static int
1716_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1717 struct _raid_device *raid_device)
1718{
1719 Mpi2RaidVolPage0_t *vol_pg0;
1720 Mpi2RaidPhysDiskPage0_t pd_pg0;
1721 Mpi2SasDevicePage0_t sas_device_pg0;
1722 Mpi2ConfigReply_t mpi_reply;
1723 u16 sz;
1724 u8 num_pds;
1725
1726 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1727 &num_pds)) || !num_pds) {
1728 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1729 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1730 __func__));
1731 return 1;
1732 }
1733
1734 raid_device->num_pds = num_pds;
1735 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1736 sizeof(Mpi2RaidVol0PhysDisk_t));
1737 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1738 if (!vol_pg0) {
1739 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1740 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1741 __func__));
1742 return 1;
1743 }
1744
1745 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1746 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1747 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1748 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1749 __func__));
1750 kfree(vol_pg0);
1751 return 1;
1752 }
1753
1754 raid_device->volume_type = vol_pg0->VolumeType;
1755
1756 /* figure out what the underlying devices are by
1757 * obtaining the device_info bits for the 1st device
1758 */
1759 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1760 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1761 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1762 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1763 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1764 le16_to_cpu(pd_pg0.DevHandle)))) {
1765 raid_device->device_info =
1766 le32_to_cpu(sas_device_pg0.DeviceInfo);
1767 }
1768 }
1769
1770 kfree(vol_pg0);
1771 return 0;
1772}
1773
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301774/**
1775 * _scsih_enable_tlr - setting TLR flags
1776 * @ioc: per adapter object
1777 * @sdev: scsi device struct
1778 *
1779 * Enabling Transaction Layer Retries for tape devices when
1780 * vpd page 0x90 is present
1781 *
1782 */
1783static void
1784_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1785{
1786
1787 /* only for TAPE */
1788 if (sdev->type != TYPE_TAPE)
1789 return;
1790
1791 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1792 return;
1793
1794 sas_enable_tlr(sdev);
1795 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1796 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1797 return;
1798
1799}
1800
1801/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301802 * scsih_slave_configure - device configure routine.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301803 * @sdev: scsi device struct
1804 *
1805 * Returns 0 if ok. Any other return is assumed to be an error and
1806 * the device is ignored.
1807 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301808int
1809scsih_slave_configure(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301810{
1811 struct Scsi_Host *shost = sdev->host;
1812 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1813 struct MPT3SAS_DEVICE *sas_device_priv_data;
1814 struct MPT3SAS_TARGET *sas_target_priv_data;
1815 struct _sas_device *sas_device;
1816 struct _raid_device *raid_device;
1817 unsigned long flags;
1818 int qdepth;
1819 u8 ssp_target = 0;
1820 char *ds = "";
1821 char *r_level = "";
1822 u16 handle, volume_handle = 0;
1823 u64 volume_wwid = 0;
1824
1825 qdepth = 1;
1826 sas_device_priv_data = sdev->hostdata;
1827 sas_device_priv_data->configured_lun = 1;
1828 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1829 sas_target_priv_data = sas_device_priv_data->sas_target;
1830 handle = sas_target_priv_data->handle;
1831
1832 /* raid volume handling */
1833 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1834
1835 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301836 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301837 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1838 if (!raid_device) {
1839 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1840 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1841 __LINE__, __func__));
1842 return 1;
1843 }
1844
1845 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1846 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1847 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1848 __LINE__, __func__));
1849 return 1;
1850 }
1851
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301852 /*
1853 * WARPDRIVE: Initialize the required data for Direct IO
1854 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301855 mpt3sas_init_warpdrive_properties(ioc, raid_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301856
1857 /* RAID Queue Depth Support
1858 * IS volume = underlying qdepth of drive type, either
1859 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1860 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1861 */
1862 if (raid_device->device_info &
1863 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1864 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1865 ds = "SSP";
1866 } else {
1867 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1868 if (raid_device->device_info &
1869 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1870 ds = "SATA";
1871 else
1872 ds = "STP";
1873 }
1874
1875 switch (raid_device->volume_type) {
1876 case MPI2_RAID_VOL_TYPE_RAID0:
1877 r_level = "RAID0";
1878 break;
1879 case MPI2_RAID_VOL_TYPE_RAID1E:
1880 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1881 if (ioc->manu_pg10.OEMIdentifier &&
1882 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1883 MFG10_GF0_R10_DISPLAY) &&
1884 !(raid_device->num_pds % 2))
1885 r_level = "RAID10";
1886 else
1887 r_level = "RAID1E";
1888 break;
1889 case MPI2_RAID_VOL_TYPE_RAID1:
1890 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1891 r_level = "RAID1";
1892 break;
1893 case MPI2_RAID_VOL_TYPE_RAID10:
1894 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1895 r_level = "RAID10";
1896 break;
1897 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1898 default:
1899 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1900 r_level = "RAIDX";
1901 break;
1902 }
1903
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301904 if (!ioc->hide_ir_msg)
1905 sdev_printk(KERN_INFO, sdev,
1906 "%s: handle(0x%04x), wwid(0x%016llx),"
1907 " pd_count(%d), type(%s)\n",
1908 r_level, raid_device->handle,
1909 (unsigned long long)raid_device->wwid,
1910 raid_device->num_pds, ds);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301911
Chaitra P B6c197092016-05-06 14:29:30 +05301912 if (shost->max_sectors > MPT3SAS_RAID_MAX_SECTORS) {
1913 blk_queue_max_hw_sectors(sdev->request_queue,
1914 MPT3SAS_RAID_MAX_SECTORS);
1915 sdev_printk(KERN_INFO, sdev,
1916 "Set queue's max_sector to: %u\n",
1917 MPT3SAS_RAID_MAX_SECTORS);
1918 }
1919
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301920 scsih_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301921
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301922 /* raid transport support */
1923 if (!ioc->is_warpdrive)
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301924 _scsih_set_level(ioc, sdev, raid_device->volume_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301925 return 0;
1926 }
1927
1928 /* non-raid handling */
1929 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1930 if (mpt3sas_config_get_volume_handle(ioc, handle,
1931 &volume_handle)) {
1932 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1933 "failure at %s:%d/%s()!\n", ioc->name,
1934 __FILE__, __LINE__, __func__));
1935 return 1;
1936 }
1937 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1938 volume_handle, &volume_wwid)) {
1939 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1940 "failure at %s:%d/%s()!\n", ioc->name,
1941 __FILE__, __LINE__, __func__));
1942 return 1;
1943 }
1944 }
1945
1946 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301947 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301948 sas_device_priv_data->sas_target->sas_address);
1949 if (!sas_device) {
1950 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1951 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1952 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1953 __func__));
1954 return 1;
1955 }
1956
1957 sas_device->volume_handle = volume_handle;
1958 sas_device->volume_wwid = volume_wwid;
1959 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1960 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1961 ssp_target = 1;
Suganath prabu Subramani30158dc2016-01-28 12:07:01 +05301962 if (sas_device->device_info &
1963 MPI2_SAS_DEVICE_INFO_SEP) {
1964 sdev_printk(KERN_WARNING, sdev,
1965 "set ignore_delay_remove for handle(0x%04x)\n",
1966 sas_device_priv_data->sas_target->handle);
1967 sas_device_priv_data->ignore_delay_remove = 1;
1968 ds = "SES";
1969 } else
1970 ds = "SSP";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301971 } else {
1972 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1973 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1974 ds = "STP";
1975 else if (sas_device->device_info &
1976 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1977 ds = "SATA";
1978 }
1979
1980 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1981 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1982 ds, handle, (unsigned long long)sas_device->sas_address,
1983 sas_device->phy, (unsigned long long)sas_device->device_name);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05301984 if (sas_device->enclosure_handle != 0)
1985 sdev_printk(KERN_INFO, sdev,
1986 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1987 ds, (unsigned long long)
1988 sas_device->enclosure_logical_id, sas_device->slot);
1989 if (sas_device->connector_name[0] != '\0')
1990 sdev_printk(KERN_INFO, sdev,
1991 "%s: enclosure level(0x%04x), connector name( %s)\n",
1992 ds, sas_device->enclosure_level,
1993 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301994
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301995 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301996 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1997
1998 if (!ssp_target)
1999 _scsih_display_sata_capabilities(ioc, handle, sdev);
2000
2001
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302002 scsih_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302003
2004 if (ssp_target) {
2005 sas_read_port_mode_page(sdev);
2006 _scsih_enable_tlr(ioc, sdev);
2007 }
2008
2009 return 0;
2010}
2011
2012/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302013 * scsih_bios_param - fetch head, sector, cylinder info for a disk
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302014 * @sdev: scsi device struct
2015 * @bdev: pointer to block device context
2016 * @capacity: device size (in 512 byte sectors)
2017 * @params: three element array to place output:
2018 * params[0] number of heads (max 255)
2019 * params[1] number of sectors (max 63)
2020 * params[2] number of cylinders
2021 *
2022 * Return nothing.
2023 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302024int
2025scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302026 sector_t capacity, int params[])
2027{
2028 int heads;
2029 int sectors;
2030 sector_t cylinders;
2031 ulong dummy;
2032
2033 heads = 64;
2034 sectors = 32;
2035
2036 dummy = heads * sectors;
2037 cylinders = capacity;
2038 sector_div(cylinders, dummy);
2039
2040 /*
2041 * Handle extended translation size for logical drives
2042 * > 1Gb
2043 */
2044 if ((ulong)capacity >= 0x200000) {
2045 heads = 255;
2046 sectors = 63;
2047 dummy = heads * sectors;
2048 cylinders = capacity;
2049 sector_div(cylinders, dummy);
2050 }
2051
2052 /* return result */
2053 params[0] = heads;
2054 params[1] = sectors;
2055 params[2] = cylinders;
2056
2057 return 0;
2058}
2059
2060/**
2061 * _scsih_response_code - translation of device response code
2062 * @ioc: per adapter object
2063 * @response_code: response code returned by the device
2064 *
2065 * Return nothing.
2066 */
2067static void
2068_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2069{
2070 char *desc;
2071
2072 switch (response_code) {
2073 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2074 desc = "task management request completed";
2075 break;
2076 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2077 desc = "invalid frame";
2078 break;
2079 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2080 desc = "task management request not supported";
2081 break;
2082 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2083 desc = "task management request failed";
2084 break;
2085 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2086 desc = "task management request succeeded";
2087 break;
2088 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2089 desc = "invalid lun";
2090 break;
2091 case 0xA:
2092 desc = "overlapped tag attempted";
2093 break;
2094 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2095 desc = "task queued, however not sent to target";
2096 break;
2097 default:
2098 desc = "unknown";
2099 break;
2100 }
2101 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2102 ioc->name, response_code, desc);
2103}
2104
2105/**
2106 * _scsih_tm_done - tm completion routine
2107 * @ioc: per adapter object
2108 * @smid: system request message index
2109 * @msix_index: MSIX table index supplied by the OS
2110 * @reply: reply message frame(lower 32bit addr)
2111 * Context: none.
2112 *
2113 * The callback handler when using scsih_issue_tm.
2114 *
2115 * Return 1 meaning mf should be freed from _base_interrupt
2116 * 0 means the mf is freed from this function.
2117 */
2118static u8
2119_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2120{
2121 MPI2DefaultReply_t *mpi_reply;
2122
2123 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2124 return 1;
2125 if (ioc->tm_cmds.smid != smid)
2126 return 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302127 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2128 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
2129 if (mpi_reply) {
2130 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2131 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2132 }
2133 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2134 complete(&ioc->tm_cmds.done);
2135 return 1;
2136}
2137
2138/**
2139 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2140 * @ioc: per adapter object
2141 * @handle: device handle
2142 *
2143 * During taskmangement request, we need to freeze the device queue.
2144 */
2145void
2146mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2147{
2148 struct MPT3SAS_DEVICE *sas_device_priv_data;
2149 struct scsi_device *sdev;
2150 u8 skip = 0;
2151
2152 shost_for_each_device(sdev, ioc->shost) {
2153 if (skip)
2154 continue;
2155 sas_device_priv_data = sdev->hostdata;
2156 if (!sas_device_priv_data)
2157 continue;
2158 if (sas_device_priv_data->sas_target->handle == handle) {
2159 sas_device_priv_data->sas_target->tm_busy = 1;
2160 skip = 1;
2161 ioc->ignore_loginfos = 1;
2162 }
2163 }
2164}
2165
2166/**
2167 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2168 * @ioc: per adapter object
2169 * @handle: device handle
2170 *
2171 * During taskmangement request, we need to freeze the device queue.
2172 */
2173void
2174mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2175{
2176 struct MPT3SAS_DEVICE *sas_device_priv_data;
2177 struct scsi_device *sdev;
2178 u8 skip = 0;
2179
2180 shost_for_each_device(sdev, ioc->shost) {
2181 if (skip)
2182 continue;
2183 sas_device_priv_data = sdev->hostdata;
2184 if (!sas_device_priv_data)
2185 continue;
2186 if (sas_device_priv_data->sas_target->handle == handle) {
2187 sas_device_priv_data->sas_target->tm_busy = 0;
2188 skip = 1;
2189 ioc->ignore_loginfos = 0;
2190 }
2191 }
2192}
2193
2194/**
2195 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2196 * @ioc: per adapter struct
2197 * @device_handle: device handle
2198 * @channel: the channel assigned by the OS
2199 * @id: the id assigned by the OS
2200 * @lun: lun number
2201 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2202 * @smid_task: smid assigned to the task
2203 * @timeout: timeout in seconds
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302204 * Context: user
2205 *
2206 * A generic API for sending task management requests to firmware.
2207 *
2208 * The callback index is set inside `ioc->tm_cb_idx`.
2209 *
2210 * Return SUCCESS or FAILED.
2211 */
2212int
2213mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
Calvin Owens96902832016-07-28 21:38:20 -07002214 uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302215{
2216 Mpi2SCSITaskManagementRequest_t *mpi_request;
2217 Mpi2SCSITaskManagementReply_t *mpi_reply;
2218 u16 smid = 0;
2219 u32 ioc_state;
2220 unsigned long timeleft;
2221 struct scsiio_tracker *scsi_lookup = NULL;
2222 int rc;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302223 u16 msix_task = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302224
Calvin Owens96902832016-07-28 21:38:20 -07002225 lockdep_assert_held(&ioc->tm_cmds.mutex);
2226
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302227 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2228 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2229 __func__, ioc->name);
Calvin Owens96902832016-07-28 21:38:20 -07002230 return FAILED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302231 }
2232
2233 if (ioc->shost_recovery || ioc->remove_host ||
2234 ioc->pci_error_recovery) {
2235 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2236 __func__, ioc->name);
Calvin Owens96902832016-07-28 21:38:20 -07002237 return FAILED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302238 }
2239
2240 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2241 if (ioc_state & MPI2_DOORBELL_USED) {
2242 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2243 "unexpected doorbell active!\n", ioc->name));
Calvin Owens98c56ad2016-07-28 21:38:21 -07002244 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Calvin Owens96902832016-07-28 21:38:20 -07002245 return (!rc) ? SUCCESS : FAILED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302246 }
2247
2248 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2249 mpt3sas_base_fault_info(ioc, ioc_state &
2250 MPI2_DOORBELL_DATA_MASK);
Calvin Owens98c56ad2016-07-28 21:38:21 -07002251 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Calvin Owens96902832016-07-28 21:38:20 -07002252 return (!rc) ? SUCCESS : FAILED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302253 }
2254
2255 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2256 if (!smid) {
2257 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2258 ioc->name, __func__);
Calvin Owens96902832016-07-28 21:38:20 -07002259 return FAILED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302260 }
2261
2262 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2263 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2264
2265 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2266 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2267 ioc->name, handle, type, smid_task));
2268 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2269 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2270 ioc->tm_cmds.smid = smid;
2271 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2272 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2273 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2274 mpi_request->DevHandle = cpu_to_le16(handle);
2275 mpi_request->TaskType = type;
2276 mpi_request->TaskMID = cpu_to_le16(smid_task);
2277 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2278 mpt3sas_scsih_set_tm_flag(ioc, handle);
2279 init_completion(&ioc->tm_cmds.done);
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05302280 if ((type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK) &&
2281 (scsi_lookup->msix_io < ioc->reply_queue_count))
2282 msix_task = scsi_lookup->msix_io;
2283 else
2284 msix_task = 0;
2285 mpt3sas_base_put_smid_hi_priority(ioc, smid, msix_task);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302286 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2287 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2288 pr_err(MPT3SAS_FMT "%s: timeout\n",
2289 ioc->name, __func__);
2290 _debug_dump_mf(mpi_request,
2291 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2292 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
Calvin Owens98c56ad2016-07-28 21:38:21 -07002293 rc = mpt3sas_base_hard_reset_handler(ioc,
2294 FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302295 rc = (!rc) ? SUCCESS : FAILED;
Calvin Owens96902832016-07-28 21:38:20 -07002296 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302297 }
2298 }
2299
Chaitra P B5f0dfb72016-05-06 14:29:31 +05302300 /* sync IRQs in case those were busy during flush. */
2301 mpt3sas_base_sync_reply_irqs(ioc);
2302
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302303 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2304 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2305 mpi_reply = ioc->tm_cmds.reply;
2306 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2307 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2308 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2309 le32_to_cpu(mpi_reply->IOCLogInfo),
2310 le32_to_cpu(mpi_reply->TerminationCount)));
2311 if (ioc->logging_level & MPT_DEBUG_TM) {
2312 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2313 if (mpi_reply->IOCStatus)
2314 _debug_dump_mf(mpi_request,
2315 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2316 }
2317 }
2318
2319 switch (type) {
2320 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2321 rc = SUCCESS;
2322 if (scsi_lookup->scmd == NULL)
2323 break;
2324 rc = FAILED;
2325 break;
2326
2327 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2328 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2329 rc = FAILED;
2330 else
2331 rc = SUCCESS;
2332 break;
2333 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2334 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2335 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2336 rc = FAILED;
2337 else
2338 rc = SUCCESS;
2339 break;
2340 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2341 rc = SUCCESS;
2342 break;
2343 default:
2344 rc = FAILED;
2345 break;
2346 }
2347
Calvin Owens96902832016-07-28 21:38:20 -07002348out:
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302349 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2350 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302351 return rc;
Calvin Owens96902832016-07-28 21:38:20 -07002352}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302353
Calvin Owens96902832016-07-28 21:38:20 -07002354int mpt3sas_scsih_issue_locked_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle,
2355 uint channel, uint id, uint lun, u8 type, u16 smid_task, ulong timeout)
2356{
2357 int ret;
2358
2359 mutex_lock(&ioc->tm_cmds.mutex);
2360 ret = mpt3sas_scsih_issue_tm(ioc, handle, channel, id, lun, type,
2361 smid_task, timeout);
2362 mutex_unlock(&ioc->tm_cmds.mutex);
2363
2364 return ret;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302365}
2366
2367/**
2368 * _scsih_tm_display_info - displays info about the device
2369 * @ioc: per adapter struct
2370 * @scmd: pointer to scsi command object
2371 *
2372 * Called by task management callback handlers.
2373 */
2374static void
2375_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2376{
2377 struct scsi_target *starget = scmd->device->sdev_target;
2378 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2379 struct _sas_device *sas_device = NULL;
2380 unsigned long flags;
2381 char *device_str = NULL;
2382
2383 if (!priv_target)
2384 return;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05302385 if (ioc->hide_ir_msg)
2386 device_str = "WarpDrive";
2387 else
2388 device_str = "volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302389
2390 scsi_print_command(scmd);
2391 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2392 starget_printk(KERN_INFO, starget,
2393 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2394 device_str, priv_target->handle,
2395 device_str, (unsigned long long)priv_target->sas_address);
2396 } else {
2397 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302398 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302399 if (sas_device) {
2400 if (priv_target->flags &
2401 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2402 starget_printk(KERN_INFO, starget,
2403 "volume handle(0x%04x), "
2404 "volume wwid(0x%016llx)\n",
2405 sas_device->volume_handle,
2406 (unsigned long long)sas_device->volume_wwid);
2407 }
2408 starget_printk(KERN_INFO, starget,
2409 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2410 sas_device->handle,
2411 (unsigned long long)sas_device->sas_address,
2412 sas_device->phy);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05302413 if (sas_device->enclosure_handle != 0)
2414 starget_printk(KERN_INFO, starget,
2415 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2416 (unsigned long long)
2417 sas_device->enclosure_logical_id,
2418 sas_device->slot);
Suganath prabu Subramani869817f2016-01-28 12:07:00 +05302419 if (sas_device->connector_name[0] != '\0')
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05302420 starget_printk(KERN_INFO, starget,
2421 "enclosure level(0x%04x),connector name(%s)\n",
2422 sas_device->enclosure_level,
2423 sas_device->connector_name);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302424
2425 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302426 }
2427 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2428 }
2429}
2430
2431/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302432 * scsih_abort - eh threads main abort routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302433 * @scmd: pointer to scsi command object
2434 *
2435 * Returns SUCCESS if command aborted else FAILED
2436 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302437int
2438scsih_abort(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302439{
2440 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2441 struct MPT3SAS_DEVICE *sas_device_priv_data;
2442 u16 smid;
2443 u16 handle;
2444 int r;
2445
2446 sdev_printk(KERN_INFO, scmd->device,
2447 "attempting task abort! scmd(%p)\n", scmd);
2448 _scsih_tm_display_info(ioc, scmd);
2449
2450 sas_device_priv_data = scmd->device->hostdata;
2451 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2452 sdev_printk(KERN_INFO, scmd->device,
2453 "device been deleted! scmd(%p)\n", scmd);
2454 scmd->result = DID_NO_CONNECT << 16;
2455 scmd->scsi_done(scmd);
2456 r = SUCCESS;
2457 goto out;
2458 }
2459
2460 /* search for the command */
2461 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2462 if (!smid) {
2463 scmd->result = DID_RESET << 16;
2464 r = SUCCESS;
2465 goto out;
2466 }
2467
2468 /* for hidden raid components and volumes this is not supported */
2469 if (sas_device_priv_data->sas_target->flags &
2470 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2471 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2472 scmd->result = DID_RESET << 16;
2473 r = FAILED;
2474 goto out;
2475 }
2476
2477 mpt3sas_halt_firmware(ioc);
2478
2479 handle = sas_device_priv_data->sas_target->handle;
Calvin Owens96902832016-07-28 21:38:20 -07002480 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302481 scmd->device->id, scmd->device->lun,
Calvin Owens96902832016-07-28 21:38:20 -07002482 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302483
2484 out:
2485 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2486 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2487 return r;
2488}
2489
2490/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302491 * scsih_dev_reset - eh threads main device reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302492 * @scmd: pointer to scsi command object
2493 *
2494 * Returns SUCCESS if command aborted else FAILED
2495 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302496int
2497scsih_dev_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302498{
2499 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2500 struct MPT3SAS_DEVICE *sas_device_priv_data;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302501 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302502 u16 handle;
2503 int r;
2504
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302505 struct scsi_target *starget = scmd->device->sdev_target;
2506 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2507
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302508 sdev_printk(KERN_INFO, scmd->device,
2509 "attempting device reset! scmd(%p)\n", scmd);
2510 _scsih_tm_display_info(ioc, scmd);
2511
2512 sas_device_priv_data = scmd->device->hostdata;
2513 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2514 sdev_printk(KERN_INFO, scmd->device,
2515 "device been deleted! scmd(%p)\n", scmd);
2516 scmd->result = DID_NO_CONNECT << 16;
2517 scmd->scsi_done(scmd);
2518 r = SUCCESS;
2519 goto out;
2520 }
2521
2522 /* for hidden raid components obtain the volume_handle */
2523 handle = 0;
2524 if (sas_device_priv_data->sas_target->flags &
2525 MPT_TARGET_FLAGS_RAID_COMPONENT) {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302526 sas_device = mpt3sas_get_sdev_from_target(ioc,
2527 target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302528 if (sas_device)
2529 handle = sas_device->volume_handle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302530 } else
2531 handle = sas_device_priv_data->sas_target->handle;
2532
2533 if (!handle) {
2534 scmd->result = DID_RESET << 16;
2535 r = FAILED;
2536 goto out;
2537 }
2538
Calvin Owens96902832016-07-28 21:38:20 -07002539 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302540 scmd->device->id, scmd->device->lun,
Calvin Owens96902832016-07-28 21:38:20 -07002541 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302542
2543 out:
2544 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2545 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302546
2547 if (sas_device)
2548 sas_device_put(sas_device);
2549
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302550 return r;
2551}
2552
2553/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302554 * scsih_target_reset - eh threads main target reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302555 * @scmd: pointer to scsi command object
2556 *
2557 * Returns SUCCESS if command aborted else FAILED
2558 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302559int
2560scsih_target_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302561{
2562 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2563 struct MPT3SAS_DEVICE *sas_device_priv_data;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302564 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302565 u16 handle;
2566 int r;
2567 struct scsi_target *starget = scmd->device->sdev_target;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302568 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302569
2570 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2571 scmd);
2572 _scsih_tm_display_info(ioc, scmd);
2573
2574 sas_device_priv_data = scmd->device->hostdata;
2575 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2576 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2577 scmd);
2578 scmd->result = DID_NO_CONNECT << 16;
2579 scmd->scsi_done(scmd);
2580 r = SUCCESS;
2581 goto out;
2582 }
2583
2584 /* for hidden raid components obtain the volume_handle */
2585 handle = 0;
2586 if (sas_device_priv_data->sas_target->flags &
2587 MPT_TARGET_FLAGS_RAID_COMPONENT) {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302588 sas_device = mpt3sas_get_sdev_from_target(ioc,
2589 target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302590 if (sas_device)
2591 handle = sas_device->volume_handle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302592 } else
2593 handle = sas_device_priv_data->sas_target->handle;
2594
2595 if (!handle) {
2596 scmd->result = DID_RESET << 16;
2597 r = FAILED;
2598 goto out;
2599 }
2600
Calvin Owens96902832016-07-28 21:38:20 -07002601 r = mpt3sas_scsih_issue_locked_tm(ioc, handle, scmd->device->channel,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302602 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Calvin Owens96902832016-07-28 21:38:20 -07002603 30);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302604
2605 out:
2606 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2607 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302608
2609 if (sas_device)
2610 sas_device_put(sas_device);
2611
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302612 return r;
2613}
2614
2615
2616/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302617 * scsih_host_reset - eh threads main host reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302618 * @scmd: pointer to scsi command object
2619 *
2620 * Returns SUCCESS if command aborted else FAILED
2621 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302622int
2623scsih_host_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302624{
2625 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2626 int r, retval;
2627
2628 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2629 ioc->name, scmd);
2630 scsi_print_command(scmd);
2631
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302632 if (ioc->is_driver_loading) {
2633 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2634 ioc->name);
2635 r = FAILED;
2636 goto out;
2637 }
2638
Calvin Owens98c56ad2016-07-28 21:38:21 -07002639 retval = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302640 r = (retval < 0) ? FAILED : SUCCESS;
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302641out:
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302642 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2643 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2644
2645 return r;
2646}
2647
2648/**
2649 * _scsih_fw_event_add - insert and queue up fw_event
2650 * @ioc: per adapter object
2651 * @fw_event: object describing the event
2652 * Context: This function will acquire ioc->fw_event_lock.
2653 *
2654 * This adds the firmware event object into link list, then queues it up to
2655 * be processed from user context.
2656 *
2657 * Return nothing.
2658 */
2659static void
2660_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2661{
2662 unsigned long flags;
2663
2664 if (ioc->firmware_event_thread == NULL)
2665 return;
2666
2667 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302668 fw_event_work_get(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302669 INIT_LIST_HEAD(&fw_event->list);
2670 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2671 INIT_WORK(&fw_event->work, _firmware_event_work);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302672 fw_event_work_get(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302673 queue_work(ioc->firmware_event_thread, &fw_event->work);
2674 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2675}
2676
2677/**
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302678 * _scsih_fw_event_del_from_list - delete fw_event from the list
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302679 * @ioc: per adapter object
2680 * @fw_event: object describing the event
2681 * Context: This function will acquire ioc->fw_event_lock.
2682 *
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302683 * If the fw_event is on the fw_event_list, remove it and do a put.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302684 *
2685 * Return nothing.
2686 */
2687static void
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302688_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302689 *fw_event)
2690{
2691 unsigned long flags;
2692
2693 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302694 if (!list_empty(&fw_event->list)) {
2695 list_del_init(&fw_event->list);
2696 fw_event_work_put(fw_event);
2697 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302698 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2699}
2700
2701
2702 /**
2703 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2704 * @ioc: per adapter object
2705 * @event_data: trigger event data
2706 *
2707 * Return nothing.
2708 */
2709void
2710mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2711 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2712{
2713 struct fw_event_work *fw_event;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302714 u16 sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302715
2716 if (ioc->is_driver_loading)
2717 return;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302718 sz = sizeof(*event_data);
2719 fw_event = alloc_fw_event_work(sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302720 if (!fw_event)
2721 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302722 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2723 fw_event->ioc = ioc;
2724 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2725 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302726 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302727}
2728
2729/**
2730 * _scsih_error_recovery_delete_devices - remove devices not responding
2731 * @ioc: per adapter object
2732 *
2733 * Return nothing.
2734 */
2735static void
2736_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2737{
2738 struct fw_event_work *fw_event;
2739
2740 if (ioc->is_driver_loading)
2741 return;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302742 fw_event = alloc_fw_event_work(0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302743 if (!fw_event)
2744 return;
2745 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2746 fw_event->ioc = ioc;
2747 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302748 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302749}
2750
2751/**
2752 * mpt3sas_port_enable_complete - port enable completed (fake event)
2753 * @ioc: per adapter object
2754 *
2755 * Return nothing.
2756 */
2757void
2758mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2759{
2760 struct fw_event_work *fw_event;
2761
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302762 fw_event = alloc_fw_event_work(0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302763 if (!fw_event)
2764 return;
2765 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2766 fw_event->ioc = ioc;
2767 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302768 fw_event_work_put(fw_event);
2769}
2770
2771static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2772{
2773 unsigned long flags;
2774 struct fw_event_work *fw_event = NULL;
2775
2776 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2777 if (!list_empty(&ioc->fw_event_list)) {
2778 fw_event = list_first_entry(&ioc->fw_event_list,
2779 struct fw_event_work, list);
2780 list_del_init(&fw_event->list);
2781 }
2782 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2783
2784 return fw_event;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302785}
2786
2787/**
2788 * _scsih_fw_event_cleanup_queue - cleanup event queue
2789 * @ioc: per adapter object
2790 *
2791 * Walk the firmware event queue, either killing timers, or waiting
2792 * for outstanding events to complete
2793 *
2794 * Return nothing.
2795 */
2796static void
2797_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2798{
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302799 struct fw_event_work *fw_event;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302800
2801 if (list_empty(&ioc->fw_event_list) ||
2802 !ioc->firmware_event_thread || in_interrupt())
2803 return;
2804
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302805 while ((fw_event = dequeue_next_fw_event(ioc))) {
2806 /*
2807 * Wait on the fw_event to complete. If this returns 1, then
2808 * the event was never executed, and we need a put for the
Joe Lawrenceb8ac0cc2016-04-18 10:50:12 -04002809 * reference the work had on the fw_event.
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302810 *
2811 * If it did execute, we wait for it to finish, and the put will
2812 * happen from _firmware_event_work()
2813 */
Joe Lawrenceb8ac0cc2016-04-18 10:50:12 -04002814 if (cancel_work_sync(&fw_event->work))
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302815 fw_event_work_put(fw_event);
2816
2817 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302818 }
2819}
2820
2821/**
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302822 * _scsih_internal_device_block - block the sdev device
2823 * @sdev: per device object
2824 * @sas_device_priv_data : per device driver private data
2825 *
2826 * make sure device is blocked without error, if not
2827 * print an error
2828 */
2829static void
2830_scsih_internal_device_block(struct scsi_device *sdev,
2831 struct MPT3SAS_DEVICE *sas_device_priv_data)
2832{
2833 int r = 0;
2834
2835 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2836 sas_device_priv_data->sas_target->handle);
2837 sas_device_priv_data->block = 1;
2838
2839 r = scsi_internal_device_block(sdev);
2840 if (r == -EINVAL)
2841 sdev_printk(KERN_WARNING, sdev,
2842 "device_block failed with return(%d) for handle(0x%04x)\n",
2843 sas_device_priv_data->sas_target->handle, r);
2844}
2845
2846/**
2847 * _scsih_internal_device_unblock - unblock the sdev device
2848 * @sdev: per device object
2849 * @sas_device_priv_data : per device driver private data
2850 * make sure device is unblocked without error, if not retry
2851 * by blocking and then unblocking
2852 */
2853
2854static void
2855_scsih_internal_device_unblock(struct scsi_device *sdev,
2856 struct MPT3SAS_DEVICE *sas_device_priv_data)
2857{
2858 int r = 0;
2859
2860 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2861 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2862 sas_device_priv_data->block = 0;
2863 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2864 if (r == -EINVAL) {
2865 /* The device has been set to SDEV_RUNNING by SD layer during
2866 * device addition but the request queue is still stopped by
2867 * our earlier block call. We need to perform a block again
2868 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2869
2870 sdev_printk(KERN_WARNING, sdev,
2871 "device_unblock failed with return(%d) for handle(0x%04x) "
2872 "performing a block followed by an unblock\n",
2873 sas_device_priv_data->sas_target->handle, r);
2874 sas_device_priv_data->block = 1;
2875 r = scsi_internal_device_block(sdev);
2876 if (r)
2877 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2878 "failed with return(%d) for handle(0x%04x)\n",
2879 sas_device_priv_data->sas_target->handle, r);
2880
2881 sas_device_priv_data->block = 0;
2882 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2883 if (r)
2884 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2885 " failed with return(%d) for handle(0x%04x)\n",
2886 sas_device_priv_data->sas_target->handle, r);
2887 }
2888}
2889
2890/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302891 * _scsih_ublock_io_all_device - unblock every device
2892 * @ioc: per adapter object
2893 *
2894 * change the device state from block to running
2895 */
2896static void
2897_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2898{
2899 struct MPT3SAS_DEVICE *sas_device_priv_data;
2900 struct scsi_device *sdev;
2901
2902 shost_for_each_device(sdev, ioc->shost) {
2903 sas_device_priv_data = sdev->hostdata;
2904 if (!sas_device_priv_data)
2905 continue;
2906 if (!sas_device_priv_data->block)
2907 continue;
2908
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302909 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2910 "device_running, handle(0x%04x)\n",
2911 sas_device_priv_data->sas_target->handle));
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302912 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302913 }
2914}
2915
2916
2917/**
2918 * _scsih_ublock_io_device - prepare device to be deleted
2919 * @ioc: per adapter object
2920 * @sas_addr: sas address
2921 *
2922 * unblock then put device in offline state
2923 */
2924static void
2925_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2926{
2927 struct MPT3SAS_DEVICE *sas_device_priv_data;
2928 struct scsi_device *sdev;
2929
2930 shost_for_each_device(sdev, ioc->shost) {
2931 sas_device_priv_data = sdev->hostdata;
2932 if (!sas_device_priv_data)
2933 continue;
2934 if (sas_device_priv_data->sas_target->sas_address
2935 != sas_address)
2936 continue;
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302937 if (sas_device_priv_data->block)
2938 _scsih_internal_device_unblock(sdev,
2939 sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302940 }
2941}
2942
2943/**
2944 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2945 * @ioc: per adapter object
2946 * @handle: device handle
2947 *
2948 * During device pull we need to appropiately set the sdev state.
2949 */
2950static void
2951_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2952{
2953 struct MPT3SAS_DEVICE *sas_device_priv_data;
2954 struct scsi_device *sdev;
2955
2956 shost_for_each_device(sdev, ioc->shost) {
2957 sas_device_priv_data = sdev->hostdata;
2958 if (!sas_device_priv_data)
2959 continue;
2960 if (sas_device_priv_data->block)
2961 continue;
Suganath prabu Subramani30158dc2016-01-28 12:07:01 +05302962 if (sas_device_priv_data->ignore_delay_remove) {
2963 sdev_printk(KERN_INFO, sdev,
2964 "%s skip device_block for SES handle(0x%04x)\n",
2965 __func__, sas_device_priv_data->sas_target->handle);
2966 continue;
2967 }
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302968 _scsih_internal_device_block(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302969 }
2970}
2971
2972/**
2973 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2974 * @ioc: per adapter object
2975 * @handle: device handle
2976 *
2977 * During device pull we need to appropiately set the sdev state.
2978 */
2979static void
2980_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2981{
2982 struct MPT3SAS_DEVICE *sas_device_priv_data;
2983 struct scsi_device *sdev;
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302984 struct _sas_device *sas_device;
2985
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302986 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302987 if (!sas_device)
2988 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302989
2990 shost_for_each_device(sdev, ioc->shost) {
2991 sas_device_priv_data = sdev->hostdata;
2992 if (!sas_device_priv_data)
2993 continue;
2994 if (sas_device_priv_data->sas_target->handle != handle)
2995 continue;
2996 if (sas_device_priv_data->block)
2997 continue;
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302998 if (sas_device->pend_sas_rphy_add)
2999 continue;
Suganath prabu Subramani30158dc2016-01-28 12:07:01 +05303000 if (sas_device_priv_data->ignore_delay_remove) {
3001 sdev_printk(KERN_INFO, sdev,
3002 "%s skip device_block for SES handle(0x%04x)\n",
3003 __func__, sas_device_priv_data->sas_target->handle);
3004 continue;
3005 }
Sreekanth Reddydf838f92015-06-30 12:25:05 +05303006 _scsih_internal_device_block(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303007 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303008
3009 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303010}
3011
3012/**
3013 * _scsih_block_io_to_children_attached_to_ex
3014 * @ioc: per adapter object
3015 * @sas_expander: the sas_device object
3016 *
3017 * This routine set sdev state to SDEV_BLOCK for all devices
3018 * attached to this expander. This function called when expander is
3019 * pulled.
3020 */
3021static void
3022_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3023 struct _sas_node *sas_expander)
3024{
3025 struct _sas_port *mpt3sas_port;
3026 struct _sas_device *sas_device;
3027 struct _sas_node *expander_sibling;
3028 unsigned long flags;
3029
3030 if (!sas_expander)
3031 return;
3032
3033 list_for_each_entry(mpt3sas_port,
3034 &sas_expander->sas_port_list, port_list) {
3035 if (mpt3sas_port->remote_identify.device_type ==
3036 SAS_END_DEVICE) {
3037 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303038 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3039 mpt3sas_port->remote_identify.sas_address);
3040 if (sas_device) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303041 set_bit(sas_device->handle,
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303042 ioc->blocking_handles);
3043 sas_device_put(sas_device);
3044 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303045 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3046 }
3047 }
3048
3049 list_for_each_entry(mpt3sas_port,
3050 &sas_expander->sas_port_list, port_list) {
3051
3052 if (mpt3sas_port->remote_identify.device_type ==
3053 SAS_EDGE_EXPANDER_DEVICE ||
3054 mpt3sas_port->remote_identify.device_type ==
3055 SAS_FANOUT_EXPANDER_DEVICE) {
3056 expander_sibling =
3057 mpt3sas_scsih_expander_find_by_sas_address(
3058 ioc, mpt3sas_port->remote_identify.sas_address);
3059 _scsih_block_io_to_children_attached_to_ex(ioc,
3060 expander_sibling);
3061 }
3062 }
3063}
3064
3065/**
3066 * _scsih_block_io_to_children_attached_directly
3067 * @ioc: per adapter object
3068 * @event_data: topology change event data
3069 *
3070 * This routine set sdev state to SDEV_BLOCK for all devices
3071 * direct attached during device pull.
3072 */
3073static void
3074_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3075 Mpi2EventDataSasTopologyChangeList_t *event_data)
3076{
3077 int i;
3078 u16 handle;
3079 u16 reason_code;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303080
3081 for (i = 0; i < event_data->NumEntries; i++) {
3082 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3083 if (!handle)
3084 continue;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303085 reason_code = event_data->PHY[i].PhyStatus &
3086 MPI2_EVENT_SAS_TOPO_RC_MASK;
3087 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3088 _scsih_block_io_device(ioc, handle);
3089 }
3090}
3091
3092/**
3093 * _scsih_tm_tr_send - send task management request
3094 * @ioc: per adapter object
3095 * @handle: device handle
3096 * Context: interrupt time.
3097 *
3098 * This code is to initiate the device removal handshake protocol
3099 * with controller firmware. This function will issue target reset
3100 * using high priority request queue. It will send a sas iounit
3101 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3102 *
3103 * This is designed to send muliple task management request at the same
3104 * time to the fifo. If the fifo is full, we will append the request,
3105 * and process it in a future completion.
3106 */
3107static void
3108_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3109{
3110 Mpi2SCSITaskManagementRequest_t *mpi_request;
3111 u16 smid;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303112 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303113 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3114 u64 sas_address = 0;
3115 unsigned long flags;
3116 struct _tr_list *delayed_tr;
3117 u32 ioc_state;
3118
3119 if (ioc->remove_host) {
3120 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3121 "%s: host has been removed: handle(0x%04x)\n",
3122 __func__, ioc->name, handle));
3123 return;
3124 } else if (ioc->pci_error_recovery) {
3125 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3126 "%s: host in pci error recovery: handle(0x%04x)\n",
3127 __func__, ioc->name,
3128 handle));
3129 return;
3130 }
3131 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3132 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3133 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3134 "%s: host is not operational: handle(0x%04x)\n",
3135 __func__, ioc->name,
3136 handle));
3137 return;
3138 }
3139
3140 /* if PD, then return */
3141 if (test_bit(handle, ioc->pd_handles))
3142 return;
3143
3144 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303145 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303146 if (sas_device && sas_device->starget &&
3147 sas_device->starget->hostdata) {
3148 sas_target_priv_data = sas_device->starget->hostdata;
3149 sas_target_priv_data->deleted = 1;
3150 sas_address = sas_device->sas_address;
3151 }
3152 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3153
3154 if (sas_target_priv_data) {
3155 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3156 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3157 ioc->name, handle,
3158 (unsigned long long)sas_address));
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05303159 if (sas_device->enclosure_handle != 0)
3160 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3161 "setting delete flag:enclosure logical id(0x%016llx),"
3162 " slot(%d)\n", ioc->name, (unsigned long long)
3163 sas_device->enclosure_logical_id,
3164 sas_device->slot));
Suganath prabu Subramani869817f2016-01-28 12:07:00 +05303165 if (sas_device->connector_name[0] != '\0')
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05303166 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3167 "setting delete flag: enclosure level(0x%04x),"
3168 " connector name( %s)\n", ioc->name,
3169 sas_device->enclosure_level,
3170 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303171 _scsih_ublock_io_device(ioc, sas_address);
3172 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3173 }
3174
3175 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3176 if (!smid) {
3177 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3178 if (!delayed_tr)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303179 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303180 INIT_LIST_HEAD(&delayed_tr->list);
3181 delayed_tr->handle = handle;
3182 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3183 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3184 "DELAYED:tr:handle(0x%04x), (open)\n",
3185 ioc->name, handle));
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303186 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303187 }
3188
3189 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3190 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3191 ioc->name, handle, smid,
3192 ioc->tm_tr_cb_idx));
3193 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3194 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3195 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3196 mpi_request->DevHandle = cpu_to_le16(handle);
3197 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303198 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303199 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303200
3201out:
3202 if (sas_device)
3203 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303204}
3205
3206/**
3207 * _scsih_tm_tr_complete -
3208 * @ioc: per adapter object
3209 * @smid: system request message index
3210 * @msix_index: MSIX table index supplied by the OS
3211 * @reply: reply message frame(lower 32bit addr)
3212 * Context: interrupt time.
3213 *
3214 * This is the target reset completion routine.
3215 * This code is part of the code to initiate the device removal
3216 * handshake protocol with controller firmware.
3217 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3218 *
3219 * Return 1 meaning mf should be freed from _base_interrupt
3220 * 0 means the mf is freed from this function.
3221 */
3222static u8
3223_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3224 u32 reply)
3225{
3226 u16 handle;
3227 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3228 Mpi2SCSITaskManagementReply_t *mpi_reply =
3229 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3230 Mpi2SasIoUnitControlRequest_t *mpi_request;
3231 u16 smid_sas_ctrl;
3232 u32 ioc_state;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303233 struct _sc_list *delayed_sc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303234
3235 if (ioc->remove_host) {
3236 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3237 "%s: host has been removed\n", __func__, ioc->name));
3238 return 1;
3239 } else if (ioc->pci_error_recovery) {
3240 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3241 "%s: host in pci error recovery\n", __func__,
3242 ioc->name));
3243 return 1;
3244 }
3245 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3246 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3247 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3248 "%s: host is not operational\n", __func__, ioc->name));
3249 return 1;
3250 }
3251 if (unlikely(!mpi_reply)) {
3252 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3253 ioc->name, __FILE__, __LINE__, __func__);
3254 return 1;
3255 }
3256 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3257 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3258 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3259 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3260 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3261 ioc->name, handle,
3262 le16_to_cpu(mpi_reply->DevHandle), smid));
3263 return 0;
3264 }
3265
3266 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3267 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3268 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3269 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3270 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3271 le32_to_cpu(mpi_reply->IOCLogInfo),
3272 le32_to_cpu(mpi_reply->TerminationCount)));
3273
3274 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3275 if (!smid_sas_ctrl) {
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303276 delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3277 if (!delayed_sc)
3278 return _scsih_check_for_pending_tm(ioc, smid);
3279 INIT_LIST_HEAD(&delayed_sc->list);
3280 delayed_sc->handle = mpi_request_tm->DevHandle;
3281 list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3282 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3283 "DELAYED:sc:handle(0x%04x), (open)\n",
3284 ioc->name, handle));
3285 return _scsih_check_for_pending_tm(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303286 }
3287
3288 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3289 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3290 ioc->name, handle, smid_sas_ctrl,
3291 ioc->tm_sas_control_cb_idx));
3292 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3293 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3294 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3295 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3296 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3297 mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3298
3299 return _scsih_check_for_pending_tm(ioc, smid);
3300}
3301
3302
3303/**
3304 * _scsih_sas_control_complete - completion routine
3305 * @ioc: per adapter object
3306 * @smid: system request message index
3307 * @msix_index: MSIX table index supplied by the OS
3308 * @reply: reply message frame(lower 32bit addr)
3309 * Context: interrupt time.
3310 *
3311 * This is the sas iounit control completion routine.
3312 * This code is part of the code to initiate the device removal
3313 * handshake protocol with controller firmware.
3314 *
3315 * Return 1 meaning mf should be freed from _base_interrupt
3316 * 0 means the mf is freed from this function.
3317 */
3318static u8
3319_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3320 u8 msix_index, u32 reply)
3321{
3322 Mpi2SasIoUnitControlReply_t *mpi_reply =
3323 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3324
3325 if (likely(mpi_reply)) {
3326 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3327 "sc_complete:handle(0x%04x), (open) "
3328 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3329 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3330 le16_to_cpu(mpi_reply->IOCStatus),
3331 le32_to_cpu(mpi_reply->IOCLogInfo)));
3332 } else {
3333 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3334 ioc->name, __FILE__, __LINE__, __func__);
3335 }
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303336 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303337}
3338
3339/**
3340 * _scsih_tm_tr_volume_send - send target reset request for volumes
3341 * @ioc: per adapter object
3342 * @handle: device handle
3343 * Context: interrupt time.
3344 *
3345 * This is designed to send muliple task management request at the same
3346 * time to the fifo. If the fifo is full, we will append the request,
3347 * and process it in a future completion.
3348 */
3349static void
3350_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3351{
3352 Mpi2SCSITaskManagementRequest_t *mpi_request;
3353 u16 smid;
3354 struct _tr_list *delayed_tr;
3355
3356 if (ioc->shost_recovery || ioc->remove_host ||
3357 ioc->pci_error_recovery) {
3358 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3359 "%s: host reset in progress!\n",
3360 __func__, ioc->name));
3361 return;
3362 }
3363
3364 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3365 if (!smid) {
3366 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3367 if (!delayed_tr)
3368 return;
3369 INIT_LIST_HEAD(&delayed_tr->list);
3370 delayed_tr->handle = handle;
3371 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3372 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3373 "DELAYED:tr:handle(0x%04x), (open)\n",
3374 ioc->name, handle));
3375 return;
3376 }
3377
3378 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3379 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3380 ioc->name, handle, smid,
3381 ioc->tm_tr_volume_cb_idx));
3382 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3383 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3384 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3385 mpi_request->DevHandle = cpu_to_le16(handle);
3386 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Suganath prabu Subramani03d1fb32016-01-28 12:07:06 +05303387 mpt3sas_base_put_smid_hi_priority(ioc, smid, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303388}
3389
3390/**
3391 * _scsih_tm_volume_tr_complete - target reset completion
3392 * @ioc: per adapter object
3393 * @smid: system request message index
3394 * @msix_index: MSIX table index supplied by the OS
3395 * @reply: reply message frame(lower 32bit addr)
3396 * Context: interrupt time.
3397 *
3398 * Return 1 meaning mf should be freed from _base_interrupt
3399 * 0 means the mf is freed from this function.
3400 */
3401static u8
3402_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3403 u8 msix_index, u32 reply)
3404{
3405 u16 handle;
3406 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3407 Mpi2SCSITaskManagementReply_t *mpi_reply =
3408 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3409
3410 if (ioc->shost_recovery || ioc->remove_host ||
3411 ioc->pci_error_recovery) {
3412 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3413 "%s: host reset in progress!\n",
3414 __func__, ioc->name));
3415 return 1;
3416 }
3417 if (unlikely(!mpi_reply)) {
3418 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3419 ioc->name, __FILE__, __LINE__, __func__);
3420 return 1;
3421 }
3422
3423 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3424 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3425 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3426 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3427 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3428 ioc->name, handle,
3429 le16_to_cpu(mpi_reply->DevHandle), smid));
3430 return 0;
3431 }
3432
3433 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3434 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3435 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3436 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3437 le32_to_cpu(mpi_reply->IOCLogInfo),
3438 le32_to_cpu(mpi_reply->TerminationCount)));
3439
3440 return _scsih_check_for_pending_tm(ioc, smid);
3441}
3442
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303443/**
3444 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3445 * @ioc: per adapter object
3446 * @smid: system request message index
3447 * @event: Event ID
3448 * @event_context: used to track events uniquely
3449 *
3450 * Context - processed in interrupt context.
3451 */
3452void
3453_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3454 u32 event_context)
3455{
3456 Mpi2EventAckRequest_t *ack_request;
3457 int i = smid - ioc->internal_smid;
3458 unsigned long flags;
3459
3460 /* Without releasing the smid just update the
3461 * call back index and reuse the same smid for
3462 * processing this delayed request
3463 */
3464 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3465 ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3466 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3467
3468 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3469 "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3470 ioc->name, le16_to_cpu(event), smid,
3471 ioc->base_cb_idx));
3472 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3473 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3474 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3475 ack_request->Event = event;
3476 ack_request->EventContext = event_context;
3477 ack_request->VF_ID = 0; /* TODO */
3478 ack_request->VP_ID = 0;
3479 mpt3sas_base_put_smid_default(ioc, smid);
3480}
3481
3482/**
3483 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3484 * sas_io_unit_ctrl messages
3485 * @ioc: per adapter object
3486 * @smid: system request message index
3487 * @handle: device handle
3488 *
3489 * Context - processed in interrupt context.
3490 */
3491void
3492_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3493 u16 smid, u16 handle)
3494 {
3495 Mpi2SasIoUnitControlRequest_t *mpi_request;
3496 u32 ioc_state;
3497 int i = smid - ioc->internal_smid;
3498 unsigned long flags;
3499
3500 if (ioc->remove_host) {
3501 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3502 "%s: host has been removed\n",
3503 __func__, ioc->name));
3504 return;
3505 } else if (ioc->pci_error_recovery) {
3506 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3507 "%s: host in pci error recovery\n",
3508 __func__, ioc->name));
3509 return;
3510 }
3511 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3512 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3513 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3514 "%s: host is not operational\n",
3515 __func__, ioc->name));
3516 return;
3517 }
3518
3519 /* Without releasing the smid just update the
3520 * call back index and reuse the same smid for
3521 * processing this delayed request
3522 */
3523 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3524 ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3525 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3526
3527 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3528 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3529 ioc->name, le16_to_cpu(handle), smid,
3530 ioc->tm_sas_control_cb_idx));
3531 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3532 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3533 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3534 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3535 mpi_request->DevHandle = handle;
3536 mpt3sas_base_put_smid_default(ioc, smid);
3537}
3538
3539/**
3540 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3541 * @ioc: per adapter object
3542 * @smid: system request message index
3543 *
3544 * Context: Executed in interrupt context
3545 *
3546 * This will check delayed internal messages list, and process the
3547 * next request.
3548 *
3549 * Return 1 meaning mf should be freed from _base_interrupt
3550 * 0 means the mf is freed from this function.
3551 */
3552u8
3553mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3554{
3555 struct _sc_list *delayed_sc;
3556 struct _event_ack_list *delayed_event_ack;
3557
3558 if (!list_empty(&ioc->delayed_event_ack_list)) {
3559 delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3560 struct _event_ack_list, list);
3561 _scsih_issue_delayed_event_ack(ioc, smid,
3562 delayed_event_ack->Event, delayed_event_ack->EventContext);
3563 list_del(&delayed_event_ack->list);
3564 kfree(delayed_event_ack);
3565 return 0;
3566 }
3567
3568 if (!list_empty(&ioc->delayed_sc_list)) {
3569 delayed_sc = list_entry(ioc->delayed_sc_list.next,
3570 struct _sc_list, list);
3571 _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3572 delayed_sc->handle);
3573 list_del(&delayed_sc->list);
3574 kfree(delayed_sc);
3575 return 0;
3576 }
3577 return 1;
3578}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303579
3580/**
3581 * _scsih_check_for_pending_tm - check for pending task management
3582 * @ioc: per adapter object
3583 * @smid: system request message index
3584 *
3585 * This will check delayed target reset list, and feed the
3586 * next reqeust.
3587 *
3588 * Return 1 meaning mf should be freed from _base_interrupt
3589 * 0 means the mf is freed from this function.
3590 */
3591static u8
3592_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3593{
3594 struct _tr_list *delayed_tr;
3595
3596 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3597 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3598 struct _tr_list, list);
3599 mpt3sas_base_free_smid(ioc, smid);
3600 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3601 list_del(&delayed_tr->list);
3602 kfree(delayed_tr);
3603 return 0;
3604 }
3605
3606 if (!list_empty(&ioc->delayed_tr_list)) {
3607 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3608 struct _tr_list, list);
3609 mpt3sas_base_free_smid(ioc, smid);
3610 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3611 list_del(&delayed_tr->list);
3612 kfree(delayed_tr);
3613 return 0;
3614 }
3615
3616 return 1;
3617}
3618
3619/**
3620 * _scsih_check_topo_delete_events - sanity check on topo events
3621 * @ioc: per adapter object
3622 * @event_data: the event data payload
3623 *
3624 * This routine added to better handle cable breaker.
3625 *
3626 * This handles the case where driver receives multiple expander
3627 * add and delete events in a single shot. When there is a delete event
3628 * the routine will void any pending add events waiting in the event queue.
3629 *
3630 * Return nothing.
3631 */
3632static void
3633_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3634 Mpi2EventDataSasTopologyChangeList_t *event_data)
3635{
3636 struct fw_event_work *fw_event;
3637 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3638 u16 expander_handle;
3639 struct _sas_node *sas_expander;
3640 unsigned long flags;
3641 int i, reason_code;
3642 u16 handle;
3643
3644 for (i = 0 ; i < event_data->NumEntries; i++) {
3645 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3646 if (!handle)
3647 continue;
3648 reason_code = event_data->PHY[i].PhyStatus &
3649 MPI2_EVENT_SAS_TOPO_RC_MASK;
3650 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3651 _scsih_tm_tr_send(ioc, handle);
3652 }
3653
3654 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3655 if (expander_handle < ioc->sas_hba.num_phys) {
3656 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3657 return;
3658 }
3659 if (event_data->ExpStatus ==
3660 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3661 /* put expander attached devices into blocking state */
3662 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3663 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3664 expander_handle);
3665 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3666 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3667 do {
3668 handle = find_first_bit(ioc->blocking_handles,
3669 ioc->facts.MaxDevHandle);
3670 if (handle < ioc->facts.MaxDevHandle)
3671 _scsih_block_io_device(ioc, handle);
3672 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3673 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3674 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3675
3676 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3677 return;
3678
3679 /* mark ignore flag for pending events */
3680 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3681 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3682 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3683 fw_event->ignore)
3684 continue;
Joe Lawrence35b62362014-06-25 17:05:34 -04003685 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3686 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303687 if (local_event_data->ExpStatus ==
3688 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3689 local_event_data->ExpStatus ==
3690 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3691 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3692 expander_handle) {
3693 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3694 "setting ignoring flag\n", ioc->name));
3695 fw_event->ignore = 1;
3696 }
3697 }
3698 }
3699 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3700}
3701
3702/**
3703 * _scsih_set_volume_delete_flag - setting volume delete flag
3704 * @ioc: per adapter object
3705 * @handle: device handle
3706 *
3707 * This returns nothing.
3708 */
3709static void
3710_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3711{
3712 struct _raid_device *raid_device;
3713 struct MPT3SAS_TARGET *sas_target_priv_data;
3714 unsigned long flags;
3715
3716 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303717 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303718 if (raid_device && raid_device->starget &&
3719 raid_device->starget->hostdata) {
3720 sas_target_priv_data =
3721 raid_device->starget->hostdata;
3722 sas_target_priv_data->deleted = 1;
3723 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3724 "setting delete flag: handle(0x%04x), "
3725 "wwid(0x%016llx)\n", ioc->name, handle,
3726 (unsigned long long) raid_device->wwid));
3727 }
3728 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3729}
3730
3731/**
3732 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3733 * @handle: input handle
3734 * @a: handle for volume a
3735 * @b: handle for volume b
3736 *
3737 * IR firmware only supports two raid volumes. The purpose of this
3738 * routine is to set the volume handle in either a or b. When the given
3739 * input handle is non-zero, or when a and b have not been set before.
3740 */
3741static void
3742_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3743{
3744 if (!handle || handle == *a || handle == *b)
3745 return;
3746 if (!*a)
3747 *a = handle;
3748 else if (!*b)
3749 *b = handle;
3750}
3751
3752/**
3753 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3754 * @ioc: per adapter object
3755 * @event_data: the event data payload
3756 * Context: interrupt time.
3757 *
3758 * This routine will send target reset to volume, followed by target
3759 * resets to the PDs. This is called when a PD has been removed, or
3760 * volume has been deleted or removed. When the target reset is sent
3761 * to volume, the PD target resets need to be queued to start upon
3762 * completion of the volume target reset.
3763 *
3764 * Return nothing.
3765 */
3766static void
3767_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3768 Mpi2EventDataIrConfigChangeList_t *event_data)
3769{
3770 Mpi2EventIrConfigElement_t *element;
3771 int i;
3772 u16 handle, volume_handle, a, b;
3773 struct _tr_list *delayed_tr;
3774
3775 a = 0;
3776 b = 0;
3777
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05303778 if (ioc->is_warpdrive)
3779 return;
3780
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303781 /* Volume Resets for Deleted or Removed */
3782 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3783 for (i = 0; i < event_data->NumElements; i++, element++) {
3784 if (le32_to_cpu(event_data->Flags) &
3785 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3786 continue;
3787 if (element->ReasonCode ==
3788 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3789 element->ReasonCode ==
3790 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3791 volume_handle = le16_to_cpu(element->VolDevHandle);
3792 _scsih_set_volume_delete_flag(ioc, volume_handle);
3793 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3794 }
3795 }
3796
3797 /* Volume Resets for UNHIDE events */
3798 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3799 for (i = 0; i < event_data->NumElements; i++, element++) {
3800 if (le32_to_cpu(event_data->Flags) &
3801 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3802 continue;
3803 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3804 volume_handle = le16_to_cpu(element->VolDevHandle);
3805 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3806 }
3807 }
3808
3809 if (a)
3810 _scsih_tm_tr_volume_send(ioc, a);
3811 if (b)
3812 _scsih_tm_tr_volume_send(ioc, b);
3813
3814 /* PD target resets */
3815 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3816 for (i = 0; i < event_data->NumElements; i++, element++) {
3817 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3818 continue;
3819 handle = le16_to_cpu(element->PhysDiskDevHandle);
3820 volume_handle = le16_to_cpu(element->VolDevHandle);
3821 clear_bit(handle, ioc->pd_handles);
3822 if (!volume_handle)
3823 _scsih_tm_tr_send(ioc, handle);
3824 else if (volume_handle == a || volume_handle == b) {
3825 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3826 BUG_ON(!delayed_tr);
3827 INIT_LIST_HEAD(&delayed_tr->list);
3828 delayed_tr->handle = handle;
3829 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3830 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3831 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3832 handle));
3833 } else
3834 _scsih_tm_tr_send(ioc, handle);
3835 }
3836}
3837
3838
3839/**
3840 * _scsih_check_volume_delete_events - set delete flag for volumes
3841 * @ioc: per adapter object
3842 * @event_data: the event data payload
3843 * Context: interrupt time.
3844 *
3845 * This will handle the case when the cable connected to entire volume is
3846 * pulled. We will take care of setting the deleted flag so normal IO will
3847 * not be sent.
3848 *
3849 * Return nothing.
3850 */
3851static void
3852_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3853 Mpi2EventDataIrVolume_t *event_data)
3854{
3855 u32 state;
3856
3857 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3858 return;
3859 state = le32_to_cpu(event_data->NewValue);
3860 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3861 MPI2_RAID_VOL_STATE_FAILED)
3862 _scsih_set_volume_delete_flag(ioc,
3863 le16_to_cpu(event_data->VolDevHandle));
3864}
3865
3866/**
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303867 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3868 * @ioc: per adapter object
3869 * @event_data: the temp threshold event data
3870 * Context: interrupt time.
3871 *
3872 * Return nothing.
3873 */
3874static void
3875_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3876 Mpi2EventDataTemperature_t *event_data)
3877{
3878 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3879 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3880 " exceeded for Sensor: %d !!!\n", ioc->name,
3881 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3882 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3883 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3884 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3885 event_data->SensorNum);
3886 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3887 ioc->name, event_data->CurrentTemperature);
3888 }
3889}
3890
3891/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303892 * _scsih_flush_running_cmds - completing outstanding commands.
3893 * @ioc: per adapter object
3894 *
3895 * The flushing out of all pending scmd commands following host reset,
3896 * where all IO is dropped to the floor.
3897 *
3898 * Return nothing.
3899 */
3900static void
3901_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3902{
3903 struct scsi_cmnd *scmd;
3904 u16 smid;
3905 u16 count = 0;
3906
3907 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3908 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3909 if (!scmd)
3910 continue;
3911 count++;
3912 mpt3sas_base_free_smid(ioc, smid);
3913 scsi_dma_unmap(scmd);
3914 if (ioc->pci_error_recovery)
3915 scmd->result = DID_NO_CONNECT << 16;
3916 else
3917 scmd->result = DID_RESET << 16;
3918 scmd->scsi_done(scmd);
3919 }
3920 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3921 ioc->name, count));
3922}
3923
3924/**
3925 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3926 * @ioc: per adapter object
3927 * @scmd: pointer to scsi command object
3928 * @mpi_request: pointer to the SCSI_IO reqest message frame
3929 *
3930 * Supporting protection 1 and 3.
3931 *
3932 * Returns nothing
3933 */
3934static void
3935_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3936 Mpi2SCSIIORequest_t *mpi_request)
3937{
3938 u16 eedp_flags;
3939 unsigned char prot_op = scsi_get_prot_op(scmd);
3940 unsigned char prot_type = scsi_get_prot_type(scmd);
3941 Mpi25SCSIIORequest_t *mpi_request_3v =
3942 (Mpi25SCSIIORequest_t *)mpi_request;
3943
3944 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3945 return;
3946
3947 if (prot_op == SCSI_PROT_READ_STRIP)
3948 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3949 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3950 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3951 else
3952 return;
3953
3954 switch (prot_type) {
3955 case SCSI_PROT_DIF_TYPE1:
3956 case SCSI_PROT_DIF_TYPE2:
3957
3958 /*
3959 * enable ref/guard checking
3960 * auto increment ref tag
3961 */
3962 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3963 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3964 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3965 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
Chaitra P B648512cc2016-05-06 14:29:28 +05303966 cpu_to_be32(scsi_prot_ref_tag(scmd));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303967 break;
3968
3969 case SCSI_PROT_DIF_TYPE3:
3970
3971 /*
3972 * enable guard checking
3973 */
3974 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3975
3976 break;
3977 }
3978
3979 mpi_request_3v->EEDPBlockSize =
3980 cpu_to_le16(scmd->device->sector_size);
3981 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3982}
3983
3984/**
3985 * _scsih_eedp_error_handling - return sense code for EEDP errors
3986 * @scmd: pointer to scsi command object
3987 * @ioc_status: ioc status
3988 *
3989 * Returns nothing
3990 */
3991static void
3992_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3993{
3994 u8 ascq;
3995
3996 switch (ioc_status) {
3997 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3998 ascq = 0x01;
3999 break;
4000 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4001 ascq = 0x02;
4002 break;
4003 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4004 ascq = 0x03;
4005 break;
4006 default:
4007 ascq = 0x00;
4008 break;
4009 }
4010 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4011 ascq);
4012 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4013 SAM_STAT_CHECK_CONDITION;
4014}
4015
4016
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304017
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304018/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05304019 * scsih_qcmd - main scsi request entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304020 * @scmd: pointer to scsi command object
4021 * @done: function pointer to be invoked on completion
4022 *
4023 * The callback index is set inside `ioc->scsi_io_cb_idx`.
4024 *
4025 * Returns 0 on success. If there's a failure, return either:
4026 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4027 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4028 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05304029int
4030scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304031{
Matthew Wilcoxd8bfbd82014-03-27 16:40:31 -04004032 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304033 struct MPT3SAS_DEVICE *sas_device_priv_data;
4034 struct MPT3SAS_TARGET *sas_target_priv_data;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304035 struct _raid_device *raid_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304036 Mpi2SCSIIORequest_t *mpi_request;
4037 u32 mpi_control;
4038 u16 smid;
4039 u16 handle;
4040
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304041 if (ioc->logging_level & MPT_DEBUG_SCSI)
4042 scsi_print_command(scmd);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304043
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304044 sas_device_priv_data = scmd->device->hostdata;
4045 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4046 scmd->result = DID_NO_CONNECT << 16;
4047 scmd->scsi_done(scmd);
4048 return 0;
4049 }
4050
4051 if (ioc->pci_error_recovery || ioc->remove_host) {
4052 scmd->result = DID_NO_CONNECT << 16;
4053 scmd->scsi_done(scmd);
4054 return 0;
4055 }
4056
4057 sas_target_priv_data = sas_device_priv_data->sas_target;
4058
4059 /* invalid device handle */
4060 handle = sas_target_priv_data->handle;
4061 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4062 scmd->result = DID_NO_CONNECT << 16;
4063 scmd->scsi_done(scmd);
4064 return 0;
4065 }
4066
4067
4068 /* host recovery or link resets sent via IOCTLs */
4069 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4070 return SCSI_MLQUEUE_HOST_BUSY;
4071
4072 /* device has been deleted */
4073 else if (sas_target_priv_data->deleted) {
4074 scmd->result = DID_NO_CONNECT << 16;
4075 scmd->scsi_done(scmd);
4076 return 0;
4077 /* device busy with task managment */
4078 } else if (sas_target_priv_data->tm_busy ||
4079 sas_device_priv_data->block)
4080 return SCSI_MLQUEUE_DEVICE_BUSY;
4081
4082 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4083 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4084 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4085 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4086 else
4087 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4088
4089 /* set tags */
Christoph Hellwig609aa222014-10-30 11:54:58 +01004090 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304091
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304092 /* Make sure Device is not raid volume.
4093 * We do not expose raid functionality to upper layer for warpdrive.
4094 */
4095 if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
Sreekanth Reddy15de0de2015-11-12 23:40:37 +05304096 && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304097 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4098
4099 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4100 if (!smid) {
4101 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4102 ioc->name, __func__);
4103 goto out;
4104 }
4105 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4106 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4107 _scsih_setup_eedp(ioc, scmd, mpi_request);
4108
4109 if (scmd->cmd_len == 32)
4110 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4111 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4112 if (sas_device_priv_data->sas_target->flags &
4113 MPT_TARGET_FLAGS_RAID_COMPONENT)
4114 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4115 else
4116 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4117 mpi_request->DevHandle = cpu_to_le16(handle);
4118 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4119 mpi_request->Control = cpu_to_le32(mpi_control);
4120 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4121 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4122 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4123 mpi_request->SenseBufferLowAddress =
4124 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4125 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4126 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4127 mpi_request->LUN);
4128 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4129
4130 if (mpi_request->DataLength) {
4131 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4132 mpt3sas_base_free_smid(ioc, smid);
4133 goto out;
4134 }
4135 } else
4136 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4137
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304138 raid_device = sas_target_priv_data->raid_device;
4139 if (raid_device && raid_device->direct_io_enabled)
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304140 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304141 smid);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304142
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304143 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4144 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4145 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4146 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
4147 mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
4148 } else
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304149 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4150 le16_to_cpu(mpi_request->DevHandle));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304151 } else
4152 mpt3sas_base_put_smid_default(ioc, smid);
4153 return 0;
4154
4155 out:
4156 return SCSI_MLQUEUE_HOST_BUSY;
4157}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304158
4159/**
4160 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4161 * @sense_buffer: sense data returned by target
4162 * @data: normalized skey/asc/ascq
4163 *
4164 * Return nothing.
4165 */
4166static void
4167_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4168{
4169 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4170 /* descriptor format */
4171 data->skey = sense_buffer[1] & 0x0F;
4172 data->asc = sense_buffer[2];
4173 data->ascq = sense_buffer[3];
4174 } else {
4175 /* fixed format */
4176 data->skey = sense_buffer[2] & 0x0F;
4177 data->asc = sense_buffer[12];
4178 data->ascq = sense_buffer[13];
4179 }
4180}
4181
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304182/**
4183 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4184 * @ioc: per adapter object
4185 * @scmd: pointer to scsi command object
4186 * @mpi_reply: reply mf payload returned from firmware
4187 *
4188 * scsi_status - SCSI Status code returned from target device
4189 * scsi_state - state info associated with SCSI_IO determined by ioc
4190 * ioc_status - ioc supplied status info
4191 *
4192 * Return nothing.
4193 */
4194static void
4195_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4196 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4197{
4198 u32 response_info;
4199 u8 *response_bytes;
4200 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4201 MPI2_IOCSTATUS_MASK;
4202 u8 scsi_state = mpi_reply->SCSIState;
4203 u8 scsi_status = mpi_reply->SCSIStatus;
4204 char *desc_ioc_state = NULL;
4205 char *desc_scsi_status = NULL;
4206 char *desc_scsi_state = ioc->tmp_string;
4207 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4208 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304209 struct scsi_target *starget = scmd->device->sdev_target;
4210 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4211 char *device_str = NULL;
4212
4213 if (!priv_target)
4214 return;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304215 if (ioc->hide_ir_msg)
4216 device_str = "WarpDrive";
4217 else
4218 device_str = "volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304219
4220 if (log_info == 0x31170000)
4221 return;
4222
4223 switch (ioc_status) {
4224 case MPI2_IOCSTATUS_SUCCESS:
4225 desc_ioc_state = "success";
4226 break;
4227 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4228 desc_ioc_state = "invalid function";
4229 break;
4230 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4231 desc_ioc_state = "scsi recovered error";
4232 break;
4233 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4234 desc_ioc_state = "scsi invalid dev handle";
4235 break;
4236 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4237 desc_ioc_state = "scsi device not there";
4238 break;
4239 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4240 desc_ioc_state = "scsi data overrun";
4241 break;
4242 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4243 desc_ioc_state = "scsi data underrun";
4244 break;
4245 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4246 desc_ioc_state = "scsi io data error";
4247 break;
4248 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4249 desc_ioc_state = "scsi protocol error";
4250 break;
4251 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4252 desc_ioc_state = "scsi task terminated";
4253 break;
4254 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4255 desc_ioc_state = "scsi residual mismatch";
4256 break;
4257 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4258 desc_ioc_state = "scsi task mgmt failed";
4259 break;
4260 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4261 desc_ioc_state = "scsi ioc terminated";
4262 break;
4263 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4264 desc_ioc_state = "scsi ext terminated";
4265 break;
4266 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4267 desc_ioc_state = "eedp guard error";
4268 break;
4269 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4270 desc_ioc_state = "eedp ref tag error";
4271 break;
4272 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4273 desc_ioc_state = "eedp app tag error";
4274 break;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05304275 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4276 desc_ioc_state = "insufficient power";
4277 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304278 default:
4279 desc_ioc_state = "unknown";
4280 break;
4281 }
4282
4283 switch (scsi_status) {
4284 case MPI2_SCSI_STATUS_GOOD:
4285 desc_scsi_status = "good";
4286 break;
4287 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4288 desc_scsi_status = "check condition";
4289 break;
4290 case MPI2_SCSI_STATUS_CONDITION_MET:
4291 desc_scsi_status = "condition met";
4292 break;
4293 case MPI2_SCSI_STATUS_BUSY:
4294 desc_scsi_status = "busy";
4295 break;
4296 case MPI2_SCSI_STATUS_INTERMEDIATE:
4297 desc_scsi_status = "intermediate";
4298 break;
4299 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4300 desc_scsi_status = "intermediate condmet";
4301 break;
4302 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4303 desc_scsi_status = "reservation conflict";
4304 break;
4305 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4306 desc_scsi_status = "command terminated";
4307 break;
4308 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4309 desc_scsi_status = "task set full";
4310 break;
4311 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4312 desc_scsi_status = "aca active";
4313 break;
4314 case MPI2_SCSI_STATUS_TASK_ABORTED:
4315 desc_scsi_status = "task aborted";
4316 break;
4317 default:
4318 desc_scsi_status = "unknown";
4319 break;
4320 }
4321
4322 desc_scsi_state[0] = '\0';
4323 if (!scsi_state)
4324 desc_scsi_state = " ";
4325 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4326 strcat(desc_scsi_state, "response info ");
4327 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4328 strcat(desc_scsi_state, "state terminated ");
4329 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4330 strcat(desc_scsi_state, "no status ");
4331 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4332 strcat(desc_scsi_state, "autosense failed ");
4333 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4334 strcat(desc_scsi_state, "autosense valid ");
4335
4336 scsi_print_command(scmd);
4337
4338 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4339 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4340 device_str, (unsigned long long)priv_target->sas_address);
4341 } else {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304342 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304343 if (sas_device) {
4344 pr_warn(MPT3SAS_FMT
4345 "\tsas_address(0x%016llx), phy(%d)\n",
4346 ioc->name, (unsigned long long)
4347 sas_device->sas_address, sas_device->phy);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304348 if (sas_device->enclosure_handle != 0)
4349 pr_warn(MPT3SAS_FMT
4350 "\tenclosure_logical_id(0x%016llx),"
4351 "slot(%d)\n", ioc->name,
4352 (unsigned long long)
4353 sas_device->enclosure_logical_id,
4354 sas_device->slot);
4355 if (sas_device->connector_name[0])
4356 pr_warn(MPT3SAS_FMT
4357 "\tenclosure level(0x%04x),"
4358 " connector name( %s)\n", ioc->name,
4359 sas_device->enclosure_level,
4360 sas_device->connector_name);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304361
4362 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304363 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304364 }
4365
4366 pr_warn(MPT3SAS_FMT
4367 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4368 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4369 desc_ioc_state, ioc_status, smid);
4370 pr_warn(MPT3SAS_FMT
4371 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4372 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4373 scsi_get_resid(scmd));
4374 pr_warn(MPT3SAS_FMT
4375 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4376 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4377 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4378 pr_warn(MPT3SAS_FMT
4379 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4380 ioc->name, desc_scsi_status,
4381 scsi_status, desc_scsi_state, scsi_state);
4382
4383 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4384 struct sense_info data;
4385 _scsih_normalize_sense(scmd->sense_buffer, &data);
4386 pr_warn(MPT3SAS_FMT
4387 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4388 ioc->name, data.skey,
4389 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4390 }
4391
4392 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4393 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4394 response_bytes = (u8 *)&response_info;
4395 _scsih_response_code(ioc, response_bytes[0]);
4396 }
4397}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304398
4399/**
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304400 * _scsih_turn_on_pfa_led - illuminate PFA LED
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304401 * @ioc: per adapter object
4402 * @handle: device handle
4403 * Context: process
4404 *
4405 * Return nothing.
4406 */
4407static void
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304408_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304409{
4410 Mpi2SepReply_t mpi_reply;
4411 Mpi2SepRequest_t mpi_request;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304412 struct _sas_device *sas_device;
4413
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304414 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304415 if (!sas_device)
4416 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304417
4418 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4419 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4420 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4421 mpi_request.SlotStatus =
4422 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4423 mpi_request.DevHandle = cpu_to_le16(handle);
4424 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4425 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4426 &mpi_request)) != 0) {
4427 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4428 __FILE__, __LINE__, __func__);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304429 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304430 }
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304431 sas_device->pfa_led_on = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304432
4433 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4434 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4435 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4436 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4437 le32_to_cpu(mpi_reply.IOCLogInfo)));
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304438 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304439 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304440out:
4441 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304442}
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304443
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304444/**
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304445 * _scsih_turn_off_pfa_led - turn off Fault LED
4446 * @ioc: per adapter object
4447 * @sas_device: sas device whose PFA LED has to turned off
4448 * Context: process
4449 *
4450 * Return nothing.
4451 */
4452static void
4453_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4454 struct _sas_device *sas_device)
4455{
4456 Mpi2SepReply_t mpi_reply;
4457 Mpi2SepRequest_t mpi_request;
4458
4459 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4460 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4461 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4462 mpi_request.SlotStatus = 0;
4463 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4464 mpi_request.DevHandle = 0;
4465 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4466 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4467 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4468 &mpi_request)) != 0) {
4469 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4470 __FILE__, __LINE__, __func__);
4471 return;
4472 }
4473
4474 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4475 dewtprintk(ioc, printk(MPT3SAS_FMT
4476 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4477 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4478 le32_to_cpu(mpi_reply.IOCLogInfo)));
4479 return;
4480 }
4481}
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304482
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304483/**
4484 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304485 * @ioc: per adapter object
4486 * @handle: device handle
4487 * Context: interrupt.
4488 *
4489 * Return nothing.
4490 */
4491static void
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304492_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304493{
4494 struct fw_event_work *fw_event;
4495
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05304496 fw_event = alloc_fw_event_work(0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304497 if (!fw_event)
4498 return;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304499 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304500 fw_event->device_handle = handle;
4501 fw_event->ioc = ioc;
4502 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05304503 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304504}
4505
4506/**
4507 * _scsih_smart_predicted_fault - process smart errors
4508 * @ioc: per adapter object
4509 * @handle: device handle
4510 * Context: interrupt.
4511 *
4512 * Return nothing.
4513 */
4514static void
4515_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4516{
4517 struct scsi_target *starget;
4518 struct MPT3SAS_TARGET *sas_target_priv_data;
4519 Mpi2EventNotificationReply_t *event_reply;
4520 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4521 struct _sas_device *sas_device;
4522 ssize_t sz;
4523 unsigned long flags;
4524
4525 /* only handle non-raid devices */
4526 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304527 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4528 if (!sas_device)
4529 goto out_unlock;
4530
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304531 starget = sas_device->starget;
4532 sas_target_priv_data = starget->hostdata;
4533
4534 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304535 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4536 goto out_unlock;
4537
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304538 if (sas_device->enclosure_handle != 0)
4539 starget_printk(KERN_INFO, starget, "predicted fault, "
4540 "enclosure logical id(0x%016llx), slot(%d)\n",
4541 (unsigned long long)sas_device->enclosure_logical_id,
4542 sas_device->slot);
4543 if (sas_device->connector_name[0] != '\0')
4544 starget_printk(KERN_WARNING, starget, "predicted fault, "
4545 "enclosure level(0x%04x), connector name( %s)\n",
4546 sas_device->enclosure_level,
4547 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304548 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4549
4550 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304551 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304552
4553 /* insert into event log */
4554 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4555 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4556 event_reply = kzalloc(sz, GFP_KERNEL);
4557 if (!event_reply) {
4558 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4559 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304560 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304561 }
4562
4563 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4564 event_reply->Event =
4565 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4566 event_reply->MsgLength = sz/4;
4567 event_reply->EventDataLength =
4568 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4569 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4570 event_reply->EventData;
4571 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4572 event_data->ASC = 0x5D;
4573 event_data->DevHandle = cpu_to_le16(handle);
4574 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4575 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4576 kfree(event_reply);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304577out:
4578 if (sas_device)
4579 sas_device_put(sas_device);
4580 return;
4581
4582out_unlock:
4583 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4584 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304585}
4586
4587/**
4588 * _scsih_io_done - scsi request callback
4589 * @ioc: per adapter object
4590 * @smid: system request message index
4591 * @msix_index: MSIX table index supplied by the OS
4592 * @reply: reply message frame(lower 32bit addr)
4593 *
4594 * Callback handler when using _scsih_qcmd.
4595 *
4596 * Return 1 meaning mf should be freed from _base_interrupt
4597 * 0 means the mf is freed from this function.
4598 */
4599static u8
4600_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4601{
4602 Mpi2SCSIIORequest_t *mpi_request;
4603 Mpi2SCSIIOReply_t *mpi_reply;
4604 struct scsi_cmnd *scmd;
4605 u16 ioc_status;
4606 u32 xfer_cnt;
4607 u8 scsi_state;
4608 u8 scsi_status;
4609 u32 log_info;
4610 struct MPT3SAS_DEVICE *sas_device_priv_data;
4611 u32 response_code = 0;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304612 unsigned long flags;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304613
4614 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4615 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4616 if (scmd == NULL)
4617 return 1;
4618
4619 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4620
4621 if (mpi_reply == NULL) {
4622 scmd->result = DID_OK << 16;
4623 goto out;
4624 }
4625
4626 sas_device_priv_data = scmd->device->hostdata;
4627 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4628 sas_device_priv_data->sas_target->deleted) {
4629 scmd->result = DID_NO_CONNECT << 16;
4630 goto out;
4631 }
4632 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4633
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304634 /*
4635 * WARPDRIVE: If direct_io is set then it is directIO,
4636 * the failed direct I/O should be redirected to volume
4637 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304638 if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304639 ((ioc_status & MPI2_IOCSTATUS_MASK)
4640 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4641 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4642 ioc->scsi_lookup[smid - 1].scmd = scmd;
4643 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304644 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304645 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4646 mpi_request->DevHandle =
4647 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4648 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4649 sas_device_priv_data->sas_target->handle);
4650 return 0;
4651 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304652 /* turning off TLR */
4653 scsi_state = mpi_reply->SCSIState;
4654 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4655 response_code =
4656 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4657 if (!sas_device_priv_data->tlr_snoop_check) {
4658 sas_device_priv_data->tlr_snoop_check++;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304659 if (!ioc->is_warpdrive &&
4660 !scsih_is_raid(&scmd->device->sdev_gendev) &&
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304661 sas_is_tlr_enabled(scmd->device) &&
4662 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4663 sas_disable_tlr(scmd->device);
4664 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4665 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304666 }
4667
4668 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4669 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4670 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4671 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4672 else
4673 log_info = 0;
4674 ioc_status &= MPI2_IOCSTATUS_MASK;
4675 scsi_status = mpi_reply->SCSIStatus;
4676
4677 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4678 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4679 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4680 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4681 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4682 }
4683
4684 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4685 struct sense_info data;
4686 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4687 smid);
4688 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4689 le32_to_cpu(mpi_reply->SenseCount));
4690 memcpy(scmd->sense_buffer, sense_data, sz);
4691 _scsih_normalize_sense(scmd->sense_buffer, &data);
4692 /* failure prediction threshold exceeded */
4693 if (data.asc == 0x5D)
4694 _scsih_smart_predicted_fault(ioc,
4695 le16_to_cpu(mpi_reply->DevHandle));
4696 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304697
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304698 if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4699 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4700 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4701 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4702 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304703 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304704 switch (ioc_status) {
4705 case MPI2_IOCSTATUS_BUSY:
4706 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4707 scmd->result = SAM_STAT_BUSY;
4708 break;
4709
4710 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4711 scmd->result = DID_NO_CONNECT << 16;
4712 break;
4713
4714 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4715 if (sas_device_priv_data->block) {
4716 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4717 goto out;
4718 }
4719 if (log_info == 0x31110630) {
4720 if (scmd->retries > 2) {
4721 scmd->result = DID_NO_CONNECT << 16;
4722 scsi_device_set_state(scmd->device,
4723 SDEV_OFFLINE);
4724 } else {
4725 scmd->result = DID_SOFT_ERROR << 16;
4726 scmd->device->expecting_cc_ua = 1;
4727 }
4728 break;
Sreekanth Reddy3898f082015-06-30 12:25:00 +05304729 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4730 scmd->result = DID_RESET << 16;
4731 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304732 }
4733 scmd->result = DID_SOFT_ERROR << 16;
4734 break;
4735 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4736 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4737 scmd->result = DID_RESET << 16;
4738 break;
4739
4740 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4741 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4742 scmd->result = DID_SOFT_ERROR << 16;
4743 else
4744 scmd->result = (DID_OK << 16) | scsi_status;
4745 break;
4746
4747 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4748 scmd->result = (DID_OK << 16) | scsi_status;
4749
4750 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4751 break;
4752
4753 if (xfer_cnt < scmd->underflow) {
4754 if (scsi_status == SAM_STAT_BUSY)
4755 scmd->result = SAM_STAT_BUSY;
4756 else
4757 scmd->result = DID_SOFT_ERROR << 16;
4758 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4759 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4760 scmd->result = DID_SOFT_ERROR << 16;
4761 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4762 scmd->result = DID_RESET << 16;
4763 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4764 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4765 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4766 scmd->result = (DRIVER_SENSE << 24) |
4767 SAM_STAT_CHECK_CONDITION;
4768 scmd->sense_buffer[0] = 0x70;
4769 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4770 scmd->sense_buffer[12] = 0x20;
4771 scmd->sense_buffer[13] = 0;
4772 }
4773 break;
4774
4775 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4776 scsi_set_resid(scmd, 0);
4777 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4778 case MPI2_IOCSTATUS_SUCCESS:
4779 scmd->result = (DID_OK << 16) | scsi_status;
4780 if (response_code ==
4781 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4782 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4783 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4784 scmd->result = DID_SOFT_ERROR << 16;
4785 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4786 scmd->result = DID_RESET << 16;
4787 break;
4788
4789 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4790 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4791 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4792 _scsih_eedp_error_handling(scmd, ioc_status);
4793 break;
4794
4795 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4796 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4797 case MPI2_IOCSTATUS_INVALID_SGL:
4798 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4799 case MPI2_IOCSTATUS_INVALID_FIELD:
4800 case MPI2_IOCSTATUS_INVALID_STATE:
4801 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4802 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05304803 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304804 default:
4805 scmd->result = DID_SOFT_ERROR << 16;
4806 break;
4807
4808 }
4809
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304810 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4811 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304812
4813 out:
4814
4815 scsi_dma_unmap(scmd);
4816
4817 scmd->scsi_done(scmd);
4818 return 1;
4819}
4820
4821/**
4822 * _scsih_sas_host_refresh - refreshing sas host object contents
4823 * @ioc: per adapter object
4824 * Context: user
4825 *
4826 * During port enable, fw will send topology events for every device. Its
4827 * possible that the handles may change from the previous setting, so this
4828 * code keeping handles updating if changed.
4829 *
4830 * Return nothing.
4831 */
4832static void
4833_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4834{
4835 u16 sz;
4836 u16 ioc_status;
4837 int i;
4838 Mpi2ConfigReply_t mpi_reply;
4839 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4840 u16 attached_handle;
4841 u8 link_rate;
4842
4843 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4844 "updating handles for sas_host(0x%016llx)\n",
4845 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4846
4847 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4848 * sizeof(Mpi2SasIOUnit0PhyData_t));
4849 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4850 if (!sas_iounit_pg0) {
4851 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4852 ioc->name, __FILE__, __LINE__, __func__);
4853 return;
4854 }
4855
4856 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4857 sas_iounit_pg0, sz)) != 0)
4858 goto out;
4859 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4860 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4861 goto out;
4862 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4863 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4864 if (i == 0)
4865 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4866 PhyData[0].ControllerDevHandle);
4867 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4868 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4869 AttachedDevHandle);
4870 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4871 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4872 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4873 attached_handle, i, link_rate);
4874 }
4875 out:
4876 kfree(sas_iounit_pg0);
4877}
4878
4879/**
4880 * _scsih_sas_host_add - create sas host object
4881 * @ioc: per adapter object
4882 *
4883 * Creating host side data object, stored in ioc->sas_hba
4884 *
4885 * Return nothing.
4886 */
4887static void
4888_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4889{
4890 int i;
4891 Mpi2ConfigReply_t mpi_reply;
4892 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4893 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4894 Mpi2SasPhyPage0_t phy_pg0;
4895 Mpi2SasDevicePage0_t sas_device_pg0;
4896 Mpi2SasEnclosurePage0_t enclosure_pg0;
4897 u16 ioc_status;
4898 u16 sz;
4899 u8 device_missing_delay;
Joe Lawrence87aa95d2016-05-25 15:14:28 -04004900 u8 num_phys;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304901
Joe Lawrence87aa95d2016-05-25 15:14:28 -04004902 mpt3sas_config_get_number_hba_phys(ioc, &num_phys);
4903 if (!num_phys) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304904 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4905 ioc->name, __FILE__, __LINE__, __func__);
4906 return;
4907 }
Joe Lawrence87aa95d2016-05-25 15:14:28 -04004908 ioc->sas_hba.phy = kcalloc(num_phys,
4909 sizeof(struct _sas_phy), GFP_KERNEL);
4910 if (!ioc->sas_hba.phy) {
4911 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4912 ioc->name, __FILE__, __LINE__, __func__);
4913 goto out;
4914 }
4915 ioc->sas_hba.num_phys = num_phys;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304916
4917 /* sas_iounit page 0 */
4918 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4919 sizeof(Mpi2SasIOUnit0PhyData_t));
4920 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4921 if (!sas_iounit_pg0) {
4922 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4923 ioc->name, __FILE__, __LINE__, __func__);
4924 return;
4925 }
4926 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4927 sas_iounit_pg0, sz))) {
4928 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4929 ioc->name, __FILE__, __LINE__, __func__);
4930 goto out;
4931 }
4932 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4933 MPI2_IOCSTATUS_MASK;
4934 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4935 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4936 ioc->name, __FILE__, __LINE__, __func__);
4937 goto out;
4938 }
4939
4940 /* sas_iounit page 1 */
4941 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4942 sizeof(Mpi2SasIOUnit1PhyData_t));
4943 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4944 if (!sas_iounit_pg1) {
4945 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4946 ioc->name, __FILE__, __LINE__, __func__);
4947 goto out;
4948 }
4949 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4950 sas_iounit_pg1, sz))) {
4951 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4952 ioc->name, __FILE__, __LINE__, __func__);
4953 goto out;
4954 }
4955 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4956 MPI2_IOCSTATUS_MASK;
4957 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4958 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4959 ioc->name, __FILE__, __LINE__, __func__);
4960 goto out;
4961 }
4962
4963 ioc->io_missing_delay =
4964 sas_iounit_pg1->IODeviceMissingDelay;
4965 device_missing_delay =
4966 sas_iounit_pg1->ReportDeviceMissingDelay;
4967 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4968 ioc->device_missing_delay = (device_missing_delay &
4969 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4970 else
4971 ioc->device_missing_delay = device_missing_delay &
4972 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4973
4974 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304975 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4976 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4977 i))) {
4978 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4979 ioc->name, __FILE__, __LINE__, __func__);
4980 goto out;
4981 }
4982 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4983 MPI2_IOCSTATUS_MASK;
4984 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4985 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4986 ioc->name, __FILE__, __LINE__, __func__);
4987 goto out;
4988 }
4989
4990 if (i == 0)
4991 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4992 PhyData[0].ControllerDevHandle);
4993 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4994 ioc->sas_hba.phy[i].phy_id = i;
4995 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4996 phy_pg0, ioc->sas_hba.parent_dev);
4997 }
4998 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4999 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
5000 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5001 ioc->name, __FILE__, __LINE__, __func__);
5002 goto out;
5003 }
5004 ioc->sas_hba.enclosure_handle =
5005 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5006 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5007 pr_info(MPT3SAS_FMT
5008 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
5009 ioc->name, ioc->sas_hba.handle,
5010 (unsigned long long) ioc->sas_hba.sas_address,
5011 ioc->sas_hba.num_phys) ;
5012
5013 if (ioc->sas_hba.enclosure_handle) {
5014 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5015 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5016 ioc->sas_hba.enclosure_handle)))
5017 ioc->sas_hba.enclosure_logical_id =
5018 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5019 }
5020
5021 out:
5022 kfree(sas_iounit_pg1);
5023 kfree(sas_iounit_pg0);
5024}
5025
5026/**
5027 * _scsih_expander_add - creating expander object
5028 * @ioc: per adapter object
5029 * @handle: expander handle
5030 *
5031 * Creating expander object, stored in ioc->sas_expander_list.
5032 *
5033 * Return 0 for success, else error.
5034 */
5035static int
5036_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5037{
5038 struct _sas_node *sas_expander;
5039 Mpi2ConfigReply_t mpi_reply;
5040 Mpi2ExpanderPage0_t expander_pg0;
5041 Mpi2ExpanderPage1_t expander_pg1;
5042 Mpi2SasEnclosurePage0_t enclosure_pg0;
5043 u32 ioc_status;
5044 u16 parent_handle;
5045 u64 sas_address, sas_address_parent = 0;
5046 int i;
5047 unsigned long flags;
5048 struct _sas_port *mpt3sas_port = NULL;
5049
5050 int rc = 0;
5051
5052 if (!handle)
5053 return -1;
5054
5055 if (ioc->shost_recovery || ioc->pci_error_recovery)
5056 return -1;
5057
5058 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5059 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5060 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5061 ioc->name, __FILE__, __LINE__, __func__);
5062 return -1;
5063 }
5064
5065 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5066 MPI2_IOCSTATUS_MASK;
5067 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5068 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5069 ioc->name, __FILE__, __LINE__, __func__);
5070 return -1;
5071 }
5072
5073 /* handle out of order topology events */
5074 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5075 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5076 != 0) {
5077 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5078 ioc->name, __FILE__, __LINE__, __func__);
5079 return -1;
5080 }
5081 if (sas_address_parent != ioc->sas_hba.sas_address) {
5082 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5083 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5084 sas_address_parent);
5085 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5086 if (!sas_expander) {
5087 rc = _scsih_expander_add(ioc, parent_handle);
5088 if (rc != 0)
5089 return rc;
5090 }
5091 }
5092
5093 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5094 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5095 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5096 sas_address);
5097 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5098
5099 if (sas_expander)
5100 return 0;
5101
5102 sas_expander = kzalloc(sizeof(struct _sas_node),
5103 GFP_KERNEL);
5104 if (!sas_expander) {
5105 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5106 ioc->name, __FILE__, __LINE__, __func__);
5107 return -1;
5108 }
5109
5110 sas_expander->handle = handle;
5111 sas_expander->num_phys = expander_pg0.NumPhys;
5112 sas_expander->sas_address_parent = sas_address_parent;
5113 sas_expander->sas_address = sas_address;
5114
5115 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5116 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5117 handle, parent_handle, (unsigned long long)
5118 sas_expander->sas_address, sas_expander->num_phys);
5119
5120 if (!sas_expander->num_phys)
5121 goto out_fail;
5122 sas_expander->phy = kcalloc(sas_expander->num_phys,
5123 sizeof(struct _sas_phy), GFP_KERNEL);
5124 if (!sas_expander->phy) {
5125 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5126 ioc->name, __FILE__, __LINE__, __func__);
5127 rc = -1;
5128 goto out_fail;
5129 }
5130
5131 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5132 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5133 sas_address_parent);
5134 if (!mpt3sas_port) {
5135 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5136 ioc->name, __FILE__, __LINE__, __func__);
5137 rc = -1;
5138 goto out_fail;
5139 }
5140 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5141
5142 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5143 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5144 &expander_pg1, i, handle))) {
5145 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5146 ioc->name, __FILE__, __LINE__, __func__);
5147 rc = -1;
5148 goto out_fail;
5149 }
5150 sas_expander->phy[i].handle = handle;
5151 sas_expander->phy[i].phy_id = i;
5152
5153 if ((mpt3sas_transport_add_expander_phy(ioc,
5154 &sas_expander->phy[i], expander_pg1,
5155 sas_expander->parent_dev))) {
5156 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5157 ioc->name, __FILE__, __LINE__, __func__);
5158 rc = -1;
5159 goto out_fail;
5160 }
5161 }
5162
5163 if (sas_expander->enclosure_handle) {
5164 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5165 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5166 sas_expander->enclosure_handle)))
5167 sas_expander->enclosure_logical_id =
5168 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5169 }
5170
5171 _scsih_expander_node_add(ioc, sas_expander);
5172 return 0;
5173
5174 out_fail:
5175
5176 if (mpt3sas_port)
5177 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5178 sas_address_parent);
5179 kfree(sas_expander);
5180 return rc;
5181}
5182
5183/**
5184 * mpt3sas_expander_remove - removing expander object
5185 * @ioc: per adapter object
5186 * @sas_address: expander sas_address
5187 *
5188 * Return nothing.
5189 */
5190void
5191mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5192{
5193 struct _sas_node *sas_expander;
5194 unsigned long flags;
5195
5196 if (ioc->shost_recovery)
5197 return;
5198
5199 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5200 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5201 sas_address);
5202 if (sas_expander)
5203 list_del(&sas_expander->list);
5204 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5205 if (sas_expander)
5206 _scsih_expander_node_remove(ioc, sas_expander);
5207}
5208
5209/**
5210 * _scsih_done - internal SCSI_IO callback handler.
5211 * @ioc: per adapter object
5212 * @smid: system request message index
5213 * @msix_index: MSIX table index supplied by the OS
5214 * @reply: reply message frame(lower 32bit addr)
5215 *
5216 * Callback handler when sending internal generated SCSI_IO.
5217 * The callback index passed is `ioc->scsih_cb_idx`
5218 *
5219 * Return 1 meaning mf should be freed from _base_interrupt
5220 * 0 means the mf is freed from this function.
5221 */
5222static u8
5223_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5224{
5225 MPI2DefaultReply_t *mpi_reply;
5226
5227 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5228 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5229 return 1;
5230 if (ioc->scsih_cmds.smid != smid)
5231 return 1;
5232 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5233 if (mpi_reply) {
5234 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5235 mpi_reply->MsgLength*4);
5236 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5237 }
5238 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5239 complete(&ioc->scsih_cmds.done);
5240 return 1;
5241}
5242
5243
5244
5245
5246#define MPT3_MAX_LUNS (255)
5247
5248
5249/**
5250 * _scsih_check_access_status - check access flags
5251 * @ioc: per adapter object
5252 * @sas_address: sas address
5253 * @handle: sas device handle
5254 * @access_flags: errors returned during discovery of the device
5255 *
5256 * Return 0 for success, else failure
5257 */
5258static u8
5259_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5260 u16 handle, u8 access_status)
5261{
5262 u8 rc = 1;
5263 char *desc = NULL;
5264
5265 switch (access_status) {
5266 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5267 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5268 rc = 0;
5269 break;
5270 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5271 desc = "sata capability failed";
5272 break;
5273 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5274 desc = "sata affiliation conflict";
5275 break;
5276 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5277 desc = "route not addressable";
5278 break;
5279 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5280 desc = "smp error not addressable";
5281 break;
5282 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5283 desc = "device blocked";
5284 break;
5285 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5286 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5287 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5288 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5289 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5290 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5291 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5292 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5293 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5294 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5295 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5296 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5297 desc = "sata initialization failed";
5298 break;
5299 default:
5300 desc = "unknown";
5301 break;
5302 }
5303
5304 if (!rc)
5305 return 0;
5306
5307 pr_err(MPT3SAS_FMT
5308 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5309 ioc->name, desc, (unsigned long long)sas_address, handle);
5310 return rc;
5311}
5312
5313/**
5314 * _scsih_check_device - checking device responsiveness
5315 * @ioc: per adapter object
5316 * @parent_sas_address: sas address of parent expander or sas host
5317 * @handle: attached device handle
5318 * @phy_numberv: phy number
5319 * @link_rate: new link rate
5320 *
5321 * Returns nothing.
5322 */
5323static void
5324_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5325 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5326{
5327 Mpi2ConfigReply_t mpi_reply;
5328 Mpi2SasDevicePage0_t sas_device_pg0;
5329 struct _sas_device *sas_device;
5330 u32 ioc_status;
5331 unsigned long flags;
5332 u64 sas_address;
5333 struct scsi_target *starget;
5334 struct MPT3SAS_TARGET *sas_target_priv_data;
5335 u32 device_info;
5336
5337
5338 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5339 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5340 return;
5341
5342 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5343 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5344 return;
5345
5346 /* wide port handling ~ we need only handle device once for the phy that
5347 * is matched in sas device page zero
5348 */
5349 if (phy_number != sas_device_pg0.PhyNum)
5350 return;
5351
5352 /* check if this is end device */
5353 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5354 if (!(_scsih_is_end_device(device_info)))
5355 return;
5356
5357 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5358 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305359 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305360 sas_address);
5361
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305362 if (!sas_device)
5363 goto out_unlock;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305364
5365 if (unlikely(sas_device->handle != handle)) {
5366 starget = sas_device->starget;
5367 sas_target_priv_data = starget->hostdata;
5368 starget_printk(KERN_INFO, starget,
5369 "handle changed from(0x%04x) to (0x%04x)!!!\n",
5370 sas_device->handle, handle);
5371 sas_target_priv_data->handle = handle;
5372 sas_device->handle = handle;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305373 if (sas_device_pg0.Flags &
5374 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5375 sas_device->enclosure_level =
5376 le16_to_cpu(sas_device_pg0.EnclosureLevel);
Calvin Owens310c8e42016-07-27 21:45:51 -07005377 memcpy(sas_device->connector_name,
5378 sas_device_pg0.ConnectorName, 4);
5379 sas_device->connector_name[4] = '\0';
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305380 } else {
5381 sas_device->enclosure_level = 0;
5382 sas_device->connector_name[0] = '\0';
5383 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305384 }
5385
5386 /* check if device is present */
5387 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5388 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5389 pr_err(MPT3SAS_FMT
5390 "device is not present handle(0x%04x), flags!!!\n",
5391 ioc->name, handle);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305392 goto out_unlock;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305393 }
5394
5395 /* check if there were any issues with discovery */
5396 if (_scsih_check_access_status(ioc, sas_address, handle,
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305397 sas_device_pg0.AccessStatus))
5398 goto out_unlock;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305399
5400 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5401 _scsih_ublock_io_device(ioc, sas_address);
5402
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305403 if (sas_device)
5404 sas_device_put(sas_device);
5405 return;
5406
5407out_unlock:
5408 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5409 if (sas_device)
5410 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305411}
5412
5413/**
5414 * _scsih_add_device - creating sas device object
5415 * @ioc: per adapter object
5416 * @handle: sas device handle
5417 * @phy_num: phy number end device attached to
5418 * @is_pd: is this hidden raid component
5419 *
5420 * Creating end device object, stored in ioc->sas_device_list.
5421 *
5422 * Returns 0 for success, non-zero for failure.
5423 */
5424static int
5425_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5426 u8 is_pd)
5427{
5428 Mpi2ConfigReply_t mpi_reply;
5429 Mpi2SasDevicePage0_t sas_device_pg0;
5430 Mpi2SasEnclosurePage0_t enclosure_pg0;
5431 struct _sas_device *sas_device;
5432 u32 ioc_status;
5433 u64 sas_address;
5434 u32 device_info;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305435
5436 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5437 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5438 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5439 ioc->name, __FILE__, __LINE__, __func__);
5440 return -1;
5441 }
5442
5443 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5444 MPI2_IOCSTATUS_MASK;
5445 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5446 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5447 ioc->name, __FILE__, __LINE__, __func__);
5448 return -1;
5449 }
5450
5451 /* check if this is end device */
5452 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5453 if (!(_scsih_is_end_device(device_info)))
5454 return -1;
5455 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5456
5457 /* check if device is present */
5458 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5459 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5460 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5461 ioc->name, handle);
5462 return -1;
5463 }
5464
5465 /* check if there were any issues with discovery */
5466 if (_scsih_check_access_status(ioc, sas_address, handle,
5467 sas_device_pg0.AccessStatus))
5468 return -1;
5469
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305470 sas_device = mpt3sas_get_sdev_by_addr(ioc,
5471 sas_address);
5472 if (sas_device) {
5473 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305474 return -1;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305475 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305476
5477 sas_device = kzalloc(sizeof(struct _sas_device),
5478 GFP_KERNEL);
5479 if (!sas_device) {
5480 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5481 ioc->name, __FILE__, __LINE__, __func__);
5482 return 0;
5483 }
5484
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305485 kref_init(&sas_device->refcount);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305486 sas_device->handle = handle;
5487 if (_scsih_get_sas_address(ioc,
5488 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5489 &sas_device->sas_address_parent) != 0)
5490 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5491 ioc->name, __FILE__, __LINE__, __func__);
5492 sas_device->enclosure_handle =
5493 le16_to_cpu(sas_device_pg0.EnclosureHandle);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305494 if (sas_device->enclosure_handle != 0)
5495 sas_device->slot =
5496 le16_to_cpu(sas_device_pg0.Slot);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305497 sas_device->device_info = device_info;
5498 sas_device->sas_address = sas_address;
5499 sas_device->phy = sas_device_pg0.PhyNum;
5500 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5501 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5502
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305503 if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5504 sas_device->enclosure_level =
5505 le16_to_cpu(sas_device_pg0.EnclosureLevel);
Calvin Owens310c8e42016-07-27 21:45:51 -07005506 memcpy(sas_device->connector_name,
5507 sas_device_pg0.ConnectorName, 4);
5508 sas_device->connector_name[4] = '\0';
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305509 } else {
5510 sas_device->enclosure_level = 0;
5511 sas_device->connector_name[0] = '\0';
5512 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305513 /* get enclosure_logical_id */
5514 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5515 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5516 sas_device->enclosure_handle)))
5517 sas_device->enclosure_logical_id =
5518 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5519
5520 /* get device name */
5521 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5522
5523 if (ioc->wait_for_discovery_to_complete)
5524 _scsih_sas_device_init_add(ioc, sas_device);
5525 else
5526 _scsih_sas_device_add(ioc, sas_device);
5527
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305528 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305529 return 0;
5530}
5531
5532/**
5533 * _scsih_remove_device - removing sas device object
5534 * @ioc: per adapter object
5535 * @sas_device_delete: the sas_device object
5536 *
5537 * Return nothing.
5538 */
5539static void
5540_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5541 struct _sas_device *sas_device)
5542{
5543 struct MPT3SAS_TARGET *sas_target_priv_data;
5544
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05305545 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5546 (sas_device->pfa_led_on)) {
5547 _scsih_turn_off_pfa_led(ioc, sas_device);
5548 sas_device->pfa_led_on = 0;
5549 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305550 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5551 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5552 ioc->name, __func__,
5553 sas_device->handle, (unsigned long long)
5554 sas_device->sas_address));
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305555 if (sas_device->enclosure_handle != 0)
5556 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5557 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5558 ioc->name, __func__,
5559 (unsigned long long)sas_device->enclosure_logical_id,
5560 sas_device->slot));
5561 if (sas_device->connector_name[0] != '\0')
5562 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5563 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5564 ioc->name, __func__,
5565 sas_device->enclosure_level,
5566 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305567
5568 if (sas_device->starget && sas_device->starget->hostdata) {
5569 sas_target_priv_data = sas_device->starget->hostdata;
5570 sas_target_priv_data->deleted = 1;
5571 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5572 sas_target_priv_data->handle =
5573 MPT3SAS_INVALID_DEVICE_HANDLE;
5574 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305575
5576 if (!ioc->hide_drives)
5577 mpt3sas_transport_port_remove(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305578 sas_device->sas_address,
5579 sas_device->sas_address_parent);
5580
5581 pr_info(MPT3SAS_FMT
5582 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5583 ioc->name, sas_device->handle,
5584 (unsigned long long) sas_device->sas_address);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305585 if (sas_device->enclosure_handle != 0)
5586 pr_info(MPT3SAS_FMT
5587 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5588 ioc->name,
5589 (unsigned long long)sas_device->enclosure_logical_id,
5590 sas_device->slot);
5591 if (sas_device->connector_name[0] != '\0')
5592 pr_info(MPT3SAS_FMT
5593 "removing enclosure level(0x%04x), connector name( %s)\n",
5594 ioc->name, sas_device->enclosure_level,
5595 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305596
5597 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5598 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5599 ioc->name, __func__,
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305600 sas_device->handle, (unsigned long long)
5601 sas_device->sas_address));
5602 if (sas_device->enclosure_handle != 0)
5603 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5604 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5605 ioc->name, __func__,
5606 (unsigned long long)sas_device->enclosure_logical_id,
5607 sas_device->slot));
5608 if (sas_device->connector_name[0] != '\0')
5609 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5610 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5611 ioc->name, __func__, sas_device->enclosure_level,
5612 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305613}
5614
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305615/**
5616 * _scsih_sas_topology_change_event_debug - debug for topology event
5617 * @ioc: per adapter object
5618 * @event_data: event data payload
5619 * Context: user.
5620 */
5621static void
5622_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5623 Mpi2EventDataSasTopologyChangeList_t *event_data)
5624{
5625 int i;
5626 u16 handle;
5627 u16 reason_code;
5628 u8 phy_number;
5629 char *status_str = NULL;
5630 u8 link_rate, prev_link_rate;
5631
5632 switch (event_data->ExpStatus) {
5633 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5634 status_str = "add";
5635 break;
5636 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5637 status_str = "remove";
5638 break;
5639 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5640 case 0:
5641 status_str = "responding";
5642 break;
5643 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5644 status_str = "remove delay";
5645 break;
5646 default:
5647 status_str = "unknown status";
5648 break;
5649 }
5650 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5651 ioc->name, status_str);
5652 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5653 "start_phy(%02d), count(%d)\n",
5654 le16_to_cpu(event_data->ExpanderDevHandle),
5655 le16_to_cpu(event_data->EnclosureHandle),
5656 event_data->StartPhyNum, event_data->NumEntries);
5657 for (i = 0; i < event_data->NumEntries; i++) {
5658 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5659 if (!handle)
5660 continue;
5661 phy_number = event_data->StartPhyNum + i;
5662 reason_code = event_data->PHY[i].PhyStatus &
5663 MPI2_EVENT_SAS_TOPO_RC_MASK;
5664 switch (reason_code) {
5665 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5666 status_str = "target add";
5667 break;
5668 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5669 status_str = "target remove";
5670 break;
5671 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5672 status_str = "delay target remove";
5673 break;
5674 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5675 status_str = "link rate change";
5676 break;
5677 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5678 status_str = "target responding";
5679 break;
5680 default:
5681 status_str = "unknown";
5682 break;
5683 }
5684 link_rate = event_data->PHY[i].LinkRate >> 4;
5685 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5686 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5687 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5688 handle, status_str, link_rate, prev_link_rate);
5689
5690 }
5691}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305692
5693/**
5694 * _scsih_sas_topology_change_event - handle topology changes
5695 * @ioc: per adapter object
5696 * @fw_event: The fw_event_work object
5697 * Context: user.
5698 *
5699 */
5700static int
5701_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5702 struct fw_event_work *fw_event)
5703{
5704 int i;
5705 u16 parent_handle, handle;
5706 u16 reason_code;
5707 u8 phy_number, max_phys;
5708 struct _sas_node *sas_expander;
5709 u64 sas_address;
5710 unsigned long flags;
5711 u8 link_rate, prev_link_rate;
Joe Lawrence35b62362014-06-25 17:05:34 -04005712 Mpi2EventDataSasTopologyChangeList_t *event_data =
5713 (Mpi2EventDataSasTopologyChangeList_t *)
5714 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305715
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305716 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5717 _scsih_sas_topology_change_event_debug(ioc, event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305718
5719 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5720 return 0;
5721
5722 if (!ioc->sas_hba.num_phys)
5723 _scsih_sas_host_add(ioc);
5724 else
5725 _scsih_sas_host_refresh(ioc);
5726
5727 if (fw_event->ignore) {
5728 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5729 "ignoring expander event\n", ioc->name));
5730 return 0;
5731 }
5732
5733 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5734
5735 /* handle expander add */
5736 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5737 if (_scsih_expander_add(ioc, parent_handle) != 0)
5738 return 0;
5739
5740 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5741 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5742 parent_handle);
5743 if (sas_expander) {
5744 sas_address = sas_expander->sas_address;
5745 max_phys = sas_expander->num_phys;
5746 } else if (parent_handle < ioc->sas_hba.num_phys) {
5747 sas_address = ioc->sas_hba.sas_address;
5748 max_phys = ioc->sas_hba.num_phys;
5749 } else {
5750 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5751 return 0;
5752 }
5753 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5754
5755 /* handle siblings events */
5756 for (i = 0; i < event_data->NumEntries; i++) {
5757 if (fw_event->ignore) {
5758 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5759 "ignoring expander event\n", ioc->name));
5760 return 0;
5761 }
5762 if (ioc->remove_host || ioc->pci_error_recovery)
5763 return 0;
5764 phy_number = event_data->StartPhyNum + i;
5765 if (phy_number >= max_phys)
5766 continue;
5767 reason_code = event_data->PHY[i].PhyStatus &
5768 MPI2_EVENT_SAS_TOPO_RC_MASK;
5769 if ((event_data->PHY[i].PhyStatus &
5770 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5771 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5772 continue;
5773 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5774 if (!handle)
5775 continue;
5776 link_rate = event_data->PHY[i].LinkRate >> 4;
5777 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5778 switch (reason_code) {
5779 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5780
5781 if (ioc->shost_recovery)
5782 break;
5783
5784 if (link_rate == prev_link_rate)
5785 break;
5786
5787 mpt3sas_transport_update_links(ioc, sas_address,
5788 handle, phy_number, link_rate);
5789
5790 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5791 break;
5792
5793 _scsih_check_device(ioc, sas_address, handle,
5794 phy_number, link_rate);
5795
5796
5797 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5798
5799 if (ioc->shost_recovery)
5800 break;
5801
5802 mpt3sas_transport_update_links(ioc, sas_address,
5803 handle, phy_number, link_rate);
5804
5805 _scsih_add_device(ioc, handle, phy_number, 0);
5806
5807 break;
5808 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5809
5810 _scsih_device_remove_by_handle(ioc, handle);
5811 break;
5812 }
5813 }
5814
5815 /* handle expander removal */
5816 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5817 sas_expander)
5818 mpt3sas_expander_remove(ioc, sas_address);
5819
5820 return 0;
5821}
5822
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305823/**
5824 * _scsih_sas_device_status_change_event_debug - debug for device event
5825 * @event_data: event data payload
5826 * Context: user.
5827 *
5828 * Return nothing.
5829 */
5830static void
5831_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5832 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5833{
5834 char *reason_str = NULL;
5835
5836 switch (event_data->ReasonCode) {
5837 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5838 reason_str = "smart data";
5839 break;
5840 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5841 reason_str = "unsupported device discovered";
5842 break;
5843 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5844 reason_str = "internal device reset";
5845 break;
5846 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5847 reason_str = "internal task abort";
5848 break;
5849 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5850 reason_str = "internal task abort set";
5851 break;
5852 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5853 reason_str = "internal clear task set";
5854 break;
5855 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5856 reason_str = "internal query task";
5857 break;
5858 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5859 reason_str = "sata init failure";
5860 break;
5861 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5862 reason_str = "internal device reset complete";
5863 break;
5864 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5865 reason_str = "internal task abort complete";
5866 break;
5867 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5868 reason_str = "internal async notification";
5869 break;
5870 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5871 reason_str = "expander reduced functionality";
5872 break;
5873 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5874 reason_str = "expander reduced functionality complete";
5875 break;
5876 default:
5877 reason_str = "unknown reason";
5878 break;
5879 }
5880 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5881 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5882 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5883 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5884 le16_to_cpu(event_data->TaskTag));
5885 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5886 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5887 event_data->ASC, event_data->ASCQ);
5888 pr_info("\n");
5889}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305890
5891/**
5892 * _scsih_sas_device_status_change_event - handle device status change
5893 * @ioc: per adapter object
5894 * @fw_event: The fw_event_work object
5895 * Context: user.
5896 *
5897 * Return nothing.
5898 */
5899static void
5900_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5901 struct fw_event_work *fw_event)
5902{
5903 struct MPT3SAS_TARGET *target_priv_data;
5904 struct _sas_device *sas_device;
5905 u64 sas_address;
5906 unsigned long flags;
5907 Mpi2EventDataSasDeviceStatusChange_t *event_data =
Joe Lawrence35b62362014-06-25 17:05:34 -04005908 (Mpi2EventDataSasDeviceStatusChange_t *)
5909 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305910
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305911 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5912 _scsih_sas_device_status_change_event_debug(ioc,
5913 event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305914
5915 /* In MPI Revision K (0xC), the internal device reset complete was
5916 * implemented, so avoid setting tm_busy flag for older firmware.
5917 */
5918 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5919 return;
5920
5921 if (event_data->ReasonCode !=
5922 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5923 event_data->ReasonCode !=
5924 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5925 return;
5926
5927 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5928 sas_address = le64_to_cpu(event_data->SASAddress);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305929 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305930 sas_address);
5931
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305932 if (!sas_device || !sas_device->starget)
5933 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305934
5935 target_priv_data = sas_device->starget->hostdata;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305936 if (!target_priv_data)
5937 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305938
5939 if (event_data->ReasonCode ==
5940 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5941 target_priv_data->tm_busy = 1;
5942 else
5943 target_priv_data->tm_busy = 0;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305944
5945out:
5946 if (sas_device)
5947 sas_device_put(sas_device);
5948
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305949 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305950
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305951}
5952
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305953/**
5954 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5955 * event
5956 * @ioc: per adapter object
5957 * @event_data: event data payload
5958 * Context: user.
5959 *
5960 * Return nothing.
5961 */
5962static void
5963_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5964 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5965{
5966 char *reason_str = NULL;
5967
5968 switch (event_data->ReasonCode) {
5969 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5970 reason_str = "enclosure add";
5971 break;
5972 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5973 reason_str = "enclosure remove";
5974 break;
5975 default:
5976 reason_str = "unknown reason";
5977 break;
5978 }
5979
5980 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5981 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5982 " number slots(%d)\n", ioc->name, reason_str,
5983 le16_to_cpu(event_data->EnclosureHandle),
5984 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5985 le16_to_cpu(event_data->StartSlot));
5986}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305987
5988/**
5989 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5990 * @ioc: per adapter object
5991 * @fw_event: The fw_event_work object
5992 * Context: user.
5993 *
5994 * Return nothing.
5995 */
5996static void
5997_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5998 struct fw_event_work *fw_event)
5999{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306000 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6001 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Joe Lawrence35b62362014-06-25 17:05:34 -04006002 (Mpi2EventDataSasEnclDevStatusChange_t *)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306003 fw_event->event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306004}
6005
6006/**
6007 * _scsih_sas_broadcast_primitive_event - handle broadcast events
6008 * @ioc: per adapter object
6009 * @fw_event: The fw_event_work object
6010 * Context: user.
6011 *
6012 * Return nothing.
6013 */
6014static void
6015_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6016 struct fw_event_work *fw_event)
6017{
6018 struct scsi_cmnd *scmd;
6019 struct scsi_device *sdev;
6020 u16 smid, handle;
6021 u32 lun;
6022 struct MPT3SAS_DEVICE *sas_device_priv_data;
6023 u32 termination_count;
6024 u32 query_count;
6025 Mpi2SCSITaskManagementReply_t *mpi_reply;
Joe Lawrence35b62362014-06-25 17:05:34 -04006026 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6027 (Mpi2EventDataSasBroadcastPrimitive_t *)
6028 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306029 u16 ioc_status;
6030 unsigned long flags;
6031 int r;
6032 u8 max_retries = 0;
6033 u8 task_abort_retries;
6034
6035 mutex_lock(&ioc->tm_cmds.mutex);
6036 pr_info(MPT3SAS_FMT
6037 "%s: enter: phy number(%d), width(%d)\n",
6038 ioc->name, __func__, event_data->PhyNum,
6039 event_data->PortWidth);
6040
6041 _scsih_block_io_all_device(ioc);
6042
6043 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6044 mpi_reply = ioc->tm_cmds.reply;
6045 broadcast_aen_retry:
6046
6047 /* sanity checks for retrying this loop */
6048 if (max_retries++ == 5) {
6049 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6050 ioc->name, __func__));
6051 goto out;
6052 } else if (max_retries > 1)
6053 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6054 ioc->name, __func__, max_retries - 1));
6055
6056 termination_count = 0;
6057 query_count = 0;
6058 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6059 if (ioc->shost_recovery)
6060 goto out;
6061 scmd = _scsih_scsi_lookup_get(ioc, smid);
6062 if (!scmd)
6063 continue;
6064 sdev = scmd->device;
6065 sas_device_priv_data = sdev->hostdata;
6066 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6067 continue;
6068 /* skip hidden raid components */
6069 if (sas_device_priv_data->sas_target->flags &
6070 MPT_TARGET_FLAGS_RAID_COMPONENT)
6071 continue;
6072 /* skip volumes */
6073 if (sas_device_priv_data->sas_target->flags &
6074 MPT_TARGET_FLAGS_VOLUME)
6075 continue;
6076
6077 handle = sas_device_priv_data->sas_target->handle;
6078 lun = sas_device_priv_data->lun;
6079 query_count++;
6080
6081 if (ioc->shost_recovery)
6082 goto out;
6083
6084 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6085 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
Calvin Owens96902832016-07-28 21:38:20 -07006086 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306087 if (r == FAILED) {
6088 sdev_printk(KERN_WARNING, sdev,
6089 "mpt3sas_scsih_issue_tm: FAILED when sending "
6090 "QUERY_TASK: scmd(%p)\n", scmd);
6091 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6092 goto broadcast_aen_retry;
6093 }
6094 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6095 & MPI2_IOCSTATUS_MASK;
6096 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6097 sdev_printk(KERN_WARNING, sdev,
6098 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6099 ioc_status, scmd);
6100 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6101 goto broadcast_aen_retry;
6102 }
6103
6104 /* see if IO is still owned by IOC and target */
6105 if (mpi_reply->ResponseCode ==
6106 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6107 mpi_reply->ResponseCode ==
6108 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6109 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6110 continue;
6111 }
6112 task_abort_retries = 0;
6113 tm_retry:
6114 if (task_abort_retries++ == 60) {
6115 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6116 "%s: ABORT_TASK: giving up\n", ioc->name,
6117 __func__));
6118 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6119 goto broadcast_aen_retry;
6120 }
6121
6122 if (ioc->shost_recovery)
6123 goto out_no_lock;
6124
6125 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
Calvin Owens96902832016-07-28 21:38:20 -07006126 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid,
6127 30);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306128 if (r == FAILED) {
6129 sdev_printk(KERN_WARNING, sdev,
6130 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6131 "scmd(%p)\n", scmd);
6132 goto tm_retry;
6133 }
6134
6135 if (task_abort_retries > 1)
6136 sdev_printk(KERN_WARNING, sdev,
6137 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6138 " scmd(%p)\n",
6139 task_abort_retries - 1, scmd);
6140
6141 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6142 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6143 }
6144
6145 if (ioc->broadcast_aen_pending) {
6146 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6147 "%s: loop back due to pending AEN\n",
6148 ioc->name, __func__));
6149 ioc->broadcast_aen_pending = 0;
6150 goto broadcast_aen_retry;
6151 }
6152
6153 out:
6154 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6155 out_no_lock:
6156
6157 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6158 "%s - exit, query_count = %d termination_count = %d\n",
6159 ioc->name, __func__, query_count, termination_count));
6160
6161 ioc->broadcast_aen_busy = 0;
6162 if (!ioc->shost_recovery)
6163 _scsih_ublock_io_all_device(ioc);
6164 mutex_unlock(&ioc->tm_cmds.mutex);
6165}
6166
6167/**
6168 * _scsih_sas_discovery_event - handle discovery events
6169 * @ioc: per adapter object
6170 * @fw_event: The fw_event_work object
6171 * Context: user.
6172 *
6173 * Return nothing.
6174 */
6175static void
6176_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6177 struct fw_event_work *fw_event)
6178{
Joe Lawrence35b62362014-06-25 17:05:34 -04006179 Mpi2EventDataSasDiscovery_t *event_data =
6180 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306181
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306182 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6183 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6184 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6185 "start" : "stop");
Sreekanth Reddyaf009412015-11-11 17:30:23 +05306186 if (event_data->DiscoveryStatus)
6187 pr_info("discovery_status(0x%08x)",
6188 le32_to_cpu(event_data->DiscoveryStatus));
6189 pr_info("\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306190 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306191
6192 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6193 !ioc->sas_hba.num_phys) {
6194 if (disable_discovery > 0 && ioc->shost_recovery) {
6195 /* Wait for the reset to complete */
6196 while (ioc->shost_recovery)
6197 ssleep(1);
6198 }
6199 _scsih_sas_host_add(ioc);
6200 }
6201}
6202
6203/**
6204 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6205 * @ioc: per adapter object
6206 * @handle: device handle for physical disk
6207 * @phys_disk_num: physical disk number
6208 *
6209 * Return 0 for success, else failure.
6210 */
6211static int
6212_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6213{
6214 Mpi2RaidActionRequest_t *mpi_request;
6215 Mpi2RaidActionReply_t *mpi_reply;
6216 u16 smid;
6217 u8 issue_reset = 0;
6218 int rc = 0;
6219 u16 ioc_status;
6220 u32 log_info;
6221
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306222 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6223 return rc;
6224
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306225 mutex_lock(&ioc->scsih_cmds.mutex);
6226
6227 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6228 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6229 ioc->name, __func__);
6230 rc = -EAGAIN;
6231 goto out;
6232 }
6233 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6234
6235 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6236 if (!smid) {
6237 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6238 ioc->name, __func__);
6239 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6240 rc = -EAGAIN;
6241 goto out;
6242 }
6243
6244 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6245 ioc->scsih_cmds.smid = smid;
6246 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6247
6248 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6249 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6250 mpi_request->PhysDiskNum = phys_disk_num;
6251
6252 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6253 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6254 handle, phys_disk_num));
6255
6256 init_completion(&ioc->scsih_cmds.done);
6257 mpt3sas_base_put_smid_default(ioc, smid);
6258 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6259
6260 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6261 pr_err(MPT3SAS_FMT "%s: timeout\n",
6262 ioc->name, __func__);
6263 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6264 issue_reset = 1;
6265 rc = -EFAULT;
6266 goto out;
6267 }
6268
6269 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6270
6271 mpi_reply = ioc->scsih_cmds.reply;
6272 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6273 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6274 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
6275 else
6276 log_info = 0;
6277 ioc_status &= MPI2_IOCSTATUS_MASK;
6278 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6279 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6280 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6281 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6282 log_info));
6283 rc = -EFAULT;
6284 } else
6285 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6286 "IR RAID_ACTION: completed successfully\n",
6287 ioc->name));
6288 }
6289
6290 out:
6291 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6292 mutex_unlock(&ioc->scsih_cmds.mutex);
6293
6294 if (issue_reset)
Calvin Owens98c56ad2016-07-28 21:38:21 -07006295 mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306296 return rc;
6297}
6298
6299/**
6300 * _scsih_reprobe_lun - reprobing lun
6301 * @sdev: scsi device struct
6302 * @no_uld_attach: sdev->no_uld_attach flag setting
6303 *
6304 **/
6305static void
6306_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6307{
6308 int rc;
6309 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6310 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6311 sdev->no_uld_attach ? "hidding" : "exposing");
6312 rc = scsi_device_reprobe(sdev);
6313}
6314
6315/**
6316 * _scsih_sas_volume_add - add new volume
6317 * @ioc: per adapter object
6318 * @element: IR config element data
6319 * Context: user.
6320 *
6321 * Return nothing.
6322 */
6323static void
6324_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6325 Mpi2EventIrConfigElement_t *element)
6326{
6327 struct _raid_device *raid_device;
6328 unsigned long flags;
6329 u64 wwid;
6330 u16 handle = le16_to_cpu(element->VolDevHandle);
6331 int rc;
6332
6333 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6334 if (!wwid) {
6335 pr_err(MPT3SAS_FMT
6336 "failure at %s:%d/%s()!\n", ioc->name,
6337 __FILE__, __LINE__, __func__);
6338 return;
6339 }
6340
6341 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6342 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6343 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6344
6345 if (raid_device)
6346 return;
6347
6348 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6349 if (!raid_device) {
6350 pr_err(MPT3SAS_FMT
6351 "failure at %s:%d/%s()!\n", ioc->name,
6352 __FILE__, __LINE__, __func__);
6353 return;
6354 }
6355
6356 raid_device->id = ioc->sas_id++;
6357 raid_device->channel = RAID_CHANNEL;
6358 raid_device->handle = handle;
6359 raid_device->wwid = wwid;
6360 _scsih_raid_device_add(ioc, raid_device);
6361 if (!ioc->wait_for_discovery_to_complete) {
6362 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6363 raid_device->id, 0);
6364 if (rc)
6365 _scsih_raid_device_remove(ioc, raid_device);
6366 } else {
6367 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6368 _scsih_determine_boot_device(ioc, raid_device, 1);
6369 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6370 }
6371}
6372
6373/**
6374 * _scsih_sas_volume_delete - delete volume
6375 * @ioc: per adapter object
6376 * @handle: volume device handle
6377 * Context: user.
6378 *
6379 * Return nothing.
6380 */
6381static void
6382_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6383{
6384 struct _raid_device *raid_device;
6385 unsigned long flags;
6386 struct MPT3SAS_TARGET *sas_target_priv_data;
6387 struct scsi_target *starget = NULL;
6388
6389 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306390 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306391 if (raid_device) {
6392 if (raid_device->starget) {
6393 starget = raid_device->starget;
6394 sas_target_priv_data = starget->hostdata;
6395 sas_target_priv_data->deleted = 1;
6396 }
6397 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6398 ioc->name, raid_device->handle,
6399 (unsigned long long) raid_device->wwid);
6400 list_del(&raid_device->list);
6401 kfree(raid_device);
6402 }
6403 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6404 if (starget)
6405 scsi_remove_target(&starget->dev);
6406}
6407
6408/**
6409 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6410 * @ioc: per adapter object
6411 * @element: IR config element data
6412 * Context: user.
6413 *
6414 * Return nothing.
6415 */
6416static void
6417_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6418 Mpi2EventIrConfigElement_t *element)
6419{
6420 struct _sas_device *sas_device;
6421 struct scsi_target *starget = NULL;
6422 struct MPT3SAS_TARGET *sas_target_priv_data;
6423 unsigned long flags;
6424 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6425
6426 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306427 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306428 if (sas_device) {
6429 sas_device->volume_handle = 0;
6430 sas_device->volume_wwid = 0;
6431 clear_bit(handle, ioc->pd_handles);
6432 if (sas_device->starget && sas_device->starget->hostdata) {
6433 starget = sas_device->starget;
6434 sas_target_priv_data = starget->hostdata;
6435 sas_target_priv_data->flags &=
6436 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6437 }
6438 }
6439 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6440 if (!sas_device)
6441 return;
6442
6443 /* exposing raid component */
6444 if (starget)
6445 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306446
6447 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306448}
6449
6450/**
6451 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6452 * @ioc: per adapter object
6453 * @element: IR config element data
6454 * Context: user.
6455 *
6456 * Return nothing.
6457 */
6458static void
6459_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6460 Mpi2EventIrConfigElement_t *element)
6461{
6462 struct _sas_device *sas_device;
6463 struct scsi_target *starget = NULL;
6464 struct MPT3SAS_TARGET *sas_target_priv_data;
6465 unsigned long flags;
6466 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6467 u16 volume_handle = 0;
6468 u64 volume_wwid = 0;
6469
6470 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6471 if (volume_handle)
6472 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6473 &volume_wwid);
6474
6475 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306476 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306477 if (sas_device) {
6478 set_bit(handle, ioc->pd_handles);
6479 if (sas_device->starget && sas_device->starget->hostdata) {
6480 starget = sas_device->starget;
6481 sas_target_priv_data = starget->hostdata;
6482 sas_target_priv_data->flags |=
6483 MPT_TARGET_FLAGS_RAID_COMPONENT;
6484 sas_device->volume_handle = volume_handle;
6485 sas_device->volume_wwid = volume_wwid;
6486 }
6487 }
6488 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6489 if (!sas_device)
6490 return;
6491
6492 /* hiding raid component */
6493 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306494
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306495 if (starget)
6496 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306497
6498 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306499}
6500
6501/**
6502 * _scsih_sas_pd_delete - delete pd component
6503 * @ioc: per adapter object
6504 * @element: IR config element data
6505 * Context: user.
6506 *
6507 * Return nothing.
6508 */
6509static void
6510_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6511 Mpi2EventIrConfigElement_t *element)
6512{
6513 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6514
6515 _scsih_device_remove_by_handle(ioc, handle);
6516}
6517
6518/**
6519 * _scsih_sas_pd_add - remove pd component
6520 * @ioc: per adapter object
6521 * @element: IR config element data
6522 * Context: user.
6523 *
6524 * Return nothing.
6525 */
6526static void
6527_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6528 Mpi2EventIrConfigElement_t *element)
6529{
6530 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306531 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6532 Mpi2ConfigReply_t mpi_reply;
6533 Mpi2SasDevicePage0_t sas_device_pg0;
6534 u32 ioc_status;
6535 u64 sas_address;
6536 u16 parent_handle;
6537
6538 set_bit(handle, ioc->pd_handles);
6539
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306540 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306541 if (sas_device) {
6542 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306543 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306544 return;
6545 }
6546
6547 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6548 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6549 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6550 ioc->name, __FILE__, __LINE__, __func__);
6551 return;
6552 }
6553
6554 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6555 MPI2_IOCSTATUS_MASK;
6556 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6557 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6558 ioc->name, __FILE__, __LINE__, __func__);
6559 return;
6560 }
6561
6562 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6563 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6564 mpt3sas_transport_update_links(ioc, sas_address, handle,
6565 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6566
6567 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6568 _scsih_add_device(ioc, handle, 0, 1);
6569}
6570
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306571/**
6572 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6573 * @ioc: per adapter object
6574 * @event_data: event data payload
6575 * Context: user.
6576 *
6577 * Return nothing.
6578 */
6579static void
6580_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6581 Mpi2EventDataIrConfigChangeList_t *event_data)
6582{
6583 Mpi2EventIrConfigElement_t *element;
6584 u8 element_type;
6585 int i;
6586 char *reason_str = NULL, *element_str = NULL;
6587
6588 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6589
6590 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6591 ioc->name, (le32_to_cpu(event_data->Flags) &
6592 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6593 "foreign" : "native", event_data->NumElements);
6594 for (i = 0; i < event_data->NumElements; i++, element++) {
6595 switch (element->ReasonCode) {
6596 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6597 reason_str = "add";
6598 break;
6599 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6600 reason_str = "remove";
6601 break;
6602 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6603 reason_str = "no change";
6604 break;
6605 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6606 reason_str = "hide";
6607 break;
6608 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6609 reason_str = "unhide";
6610 break;
6611 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6612 reason_str = "volume_created";
6613 break;
6614 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6615 reason_str = "volume_deleted";
6616 break;
6617 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6618 reason_str = "pd_created";
6619 break;
6620 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6621 reason_str = "pd_deleted";
6622 break;
6623 default:
6624 reason_str = "unknown reason";
6625 break;
6626 }
6627 element_type = le16_to_cpu(element->ElementFlags) &
6628 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6629 switch (element_type) {
6630 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6631 element_str = "volume";
6632 break;
6633 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6634 element_str = "phys disk";
6635 break;
6636 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6637 element_str = "hot spare";
6638 break;
6639 default:
6640 element_str = "unknown element";
6641 break;
6642 }
6643 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6644 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6645 reason_str, le16_to_cpu(element->VolDevHandle),
6646 le16_to_cpu(element->PhysDiskDevHandle),
6647 element->PhysDiskNum);
6648 }
6649}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306650
6651/**
6652 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6653 * @ioc: per adapter object
6654 * @fw_event: The fw_event_work object
6655 * Context: user.
6656 *
6657 * Return nothing.
6658 */
6659static void
6660_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6661 struct fw_event_work *fw_event)
6662{
6663 Mpi2EventIrConfigElement_t *element;
6664 int i;
6665 u8 foreign_config;
Joe Lawrence35b62362014-06-25 17:05:34 -04006666 Mpi2EventDataIrConfigChangeList_t *event_data =
6667 (Mpi2EventDataIrConfigChangeList_t *)
6668 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306669
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306670 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6671 (!ioc->hide_ir_msg))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306672 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6673
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306674 foreign_config = (le32_to_cpu(event_data->Flags) &
6675 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6676
6677 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306678 if (ioc->shost_recovery &&
6679 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306680 for (i = 0; i < event_data->NumElements; i++, element++) {
6681 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6682 _scsih_ir_fastpath(ioc,
6683 le16_to_cpu(element->PhysDiskDevHandle),
6684 element->PhysDiskNum);
6685 }
6686 return;
6687 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306688
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306689 for (i = 0; i < event_data->NumElements; i++, element++) {
6690
6691 switch (element->ReasonCode) {
6692 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6693 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6694 if (!foreign_config)
6695 _scsih_sas_volume_add(ioc, element);
6696 break;
6697 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6698 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6699 if (!foreign_config)
6700 _scsih_sas_volume_delete(ioc,
6701 le16_to_cpu(element->VolDevHandle));
6702 break;
6703 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306704 if (!ioc->is_warpdrive)
6705 _scsih_sas_pd_hide(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306706 break;
6707 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306708 if (!ioc->is_warpdrive)
6709 _scsih_sas_pd_expose(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306710 break;
6711 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306712 if (!ioc->is_warpdrive)
6713 _scsih_sas_pd_add(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306714 break;
6715 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306716 if (!ioc->is_warpdrive)
6717 _scsih_sas_pd_delete(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306718 break;
6719 }
6720 }
6721}
6722
6723/**
6724 * _scsih_sas_ir_volume_event - IR volume event
6725 * @ioc: per adapter object
6726 * @fw_event: The fw_event_work object
6727 * Context: user.
6728 *
6729 * Return nothing.
6730 */
6731static void
6732_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6733 struct fw_event_work *fw_event)
6734{
6735 u64 wwid;
6736 unsigned long flags;
6737 struct _raid_device *raid_device;
6738 u16 handle;
6739 u32 state;
6740 int rc;
Joe Lawrence35b62362014-06-25 17:05:34 -04006741 Mpi2EventDataIrVolume_t *event_data =
6742 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306743
6744 if (ioc->shost_recovery)
6745 return;
6746
6747 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6748 return;
6749
6750 handle = le16_to_cpu(event_data->VolDevHandle);
6751 state = le32_to_cpu(event_data->NewValue);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306752 if (!ioc->hide_ir_msg)
6753 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6754 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6755 ioc->name, __func__, handle,
6756 le32_to_cpu(event_data->PreviousValue), state));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306757 switch (state) {
6758 case MPI2_RAID_VOL_STATE_MISSING:
6759 case MPI2_RAID_VOL_STATE_FAILED:
6760 _scsih_sas_volume_delete(ioc, handle);
6761 break;
6762
6763 case MPI2_RAID_VOL_STATE_ONLINE:
6764 case MPI2_RAID_VOL_STATE_DEGRADED:
6765 case MPI2_RAID_VOL_STATE_OPTIMAL:
6766
6767 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306768 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306769 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6770
6771 if (raid_device)
6772 break;
6773
6774 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6775 if (!wwid) {
6776 pr_err(MPT3SAS_FMT
6777 "failure at %s:%d/%s()!\n", ioc->name,
6778 __FILE__, __LINE__, __func__);
6779 break;
6780 }
6781
6782 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6783 if (!raid_device) {
6784 pr_err(MPT3SAS_FMT
6785 "failure at %s:%d/%s()!\n", ioc->name,
6786 __FILE__, __LINE__, __func__);
6787 break;
6788 }
6789
6790 raid_device->id = ioc->sas_id++;
6791 raid_device->channel = RAID_CHANNEL;
6792 raid_device->handle = handle;
6793 raid_device->wwid = wwid;
6794 _scsih_raid_device_add(ioc, raid_device);
6795 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6796 raid_device->id, 0);
6797 if (rc)
6798 _scsih_raid_device_remove(ioc, raid_device);
6799 break;
6800
6801 case MPI2_RAID_VOL_STATE_INITIALIZING:
6802 default:
6803 break;
6804 }
6805}
6806
6807/**
6808 * _scsih_sas_ir_physical_disk_event - PD event
6809 * @ioc: per adapter object
6810 * @fw_event: The fw_event_work object
6811 * Context: user.
6812 *
6813 * Return nothing.
6814 */
6815static void
6816_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6817 struct fw_event_work *fw_event)
6818{
6819 u16 handle, parent_handle;
6820 u32 state;
6821 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306822 Mpi2ConfigReply_t mpi_reply;
6823 Mpi2SasDevicePage0_t sas_device_pg0;
6824 u32 ioc_status;
Joe Lawrence35b62362014-06-25 17:05:34 -04006825 Mpi2EventDataIrPhysicalDisk_t *event_data =
6826 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306827 u64 sas_address;
6828
6829 if (ioc->shost_recovery)
6830 return;
6831
6832 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6833 return;
6834
6835 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6836 state = le32_to_cpu(event_data->NewValue);
6837
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306838 if (!ioc->hide_ir_msg)
6839 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6840 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6841 ioc->name, __func__, handle,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306842 le32_to_cpu(event_data->PreviousValue), state));
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306843
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306844 switch (state) {
6845 case MPI2_RAID_PD_STATE_ONLINE:
6846 case MPI2_RAID_PD_STATE_DEGRADED:
6847 case MPI2_RAID_PD_STATE_REBUILDING:
6848 case MPI2_RAID_PD_STATE_OPTIMAL:
6849 case MPI2_RAID_PD_STATE_HOT_SPARE:
6850
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306851 if (!ioc->is_warpdrive)
6852 set_bit(handle, ioc->pd_handles);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306853
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306854 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6855 if (sas_device) {
6856 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306857 return;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306858 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306859
6860 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6861 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6862 handle))) {
6863 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6864 ioc->name, __FILE__, __LINE__, __func__);
6865 return;
6866 }
6867
6868 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6869 MPI2_IOCSTATUS_MASK;
6870 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6871 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6872 ioc->name, __FILE__, __LINE__, __func__);
6873 return;
6874 }
6875
6876 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6877 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6878 mpt3sas_transport_update_links(ioc, sas_address, handle,
6879 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6880
6881 _scsih_add_device(ioc, handle, 0, 1);
6882
6883 break;
6884
6885 case MPI2_RAID_PD_STATE_OFFLINE:
6886 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6887 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6888 default:
6889 break;
6890 }
6891}
6892
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306893/**
6894 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6895 * @ioc: per adapter object
6896 * @event_data: event data payload
6897 * Context: user.
6898 *
6899 * Return nothing.
6900 */
6901static void
6902_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6903 Mpi2EventDataIrOperationStatus_t *event_data)
6904{
6905 char *reason_str = NULL;
6906
6907 switch (event_data->RAIDOperation) {
6908 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6909 reason_str = "resync";
6910 break;
6911 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6912 reason_str = "online capacity expansion";
6913 break;
6914 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6915 reason_str = "consistency check";
6916 break;
6917 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6918 reason_str = "background init";
6919 break;
6920 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6921 reason_str = "make data consistent";
6922 break;
6923 }
6924
6925 if (!reason_str)
6926 return;
6927
6928 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6929 "\thandle(0x%04x), percent complete(%d)\n",
6930 ioc->name, reason_str,
6931 le16_to_cpu(event_data->VolDevHandle),
6932 event_data->PercentComplete);
6933}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306934
6935/**
6936 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6937 * @ioc: per adapter object
6938 * @fw_event: The fw_event_work object
6939 * Context: user.
6940 *
6941 * Return nothing.
6942 */
6943static void
6944_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6945 struct fw_event_work *fw_event)
6946{
Joe Lawrence35b62362014-06-25 17:05:34 -04006947 Mpi2EventDataIrOperationStatus_t *event_data =
6948 (Mpi2EventDataIrOperationStatus_t *)
6949 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306950 static struct _raid_device *raid_device;
6951 unsigned long flags;
6952 u16 handle;
6953
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306954 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6955 (!ioc->hide_ir_msg))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306956 _scsih_sas_ir_operation_status_event_debug(ioc,
6957 event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306958
6959 /* code added for raid transport support */
6960 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6961
6962 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6963 handle = le16_to_cpu(event_data->VolDevHandle);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306964 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306965 if (raid_device)
6966 raid_device->percent_complete =
6967 event_data->PercentComplete;
6968 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6969 }
6970}
6971
6972/**
6973 * _scsih_prep_device_scan - initialize parameters prior to device scan
6974 * @ioc: per adapter object
6975 *
6976 * Set the deleted flag prior to device scan. If the device is found during
6977 * the scan, then we clear the deleted flag.
6978 */
6979static void
6980_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6981{
6982 struct MPT3SAS_DEVICE *sas_device_priv_data;
6983 struct scsi_device *sdev;
6984
6985 shost_for_each_device(sdev, ioc->shost) {
6986 sas_device_priv_data = sdev->hostdata;
6987 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6988 sas_device_priv_data->sas_target->deleted = 1;
6989 }
6990}
6991
6992/**
6993 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6994 * @ioc: per adapter object
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306995 * @sas_device_pg0: SAS Device page 0
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306996 *
6997 * After host reset, find out whether devices are still responding.
6998 * Used in _scsih_remove_unresponsive_sas_devices.
6999 *
7000 * Return nothing.
7001 */
7002static void
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307003_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
7004Mpi2SasDevicePage0_t *sas_device_pg0)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307005{
7006 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
7007 struct scsi_target *starget;
7008 struct _sas_device *sas_device;
7009 unsigned long flags;
7010
7011 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7012 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307013 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7014 (sas_device->slot == sas_device_pg0->Slot)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307015 sas_device->responding = 1;
7016 starget = sas_device->starget;
7017 if (starget && starget->hostdata) {
7018 sas_target_priv_data = starget->hostdata;
7019 sas_target_priv_data->tm_busy = 0;
7020 sas_target_priv_data->deleted = 0;
7021 } else
7022 sas_target_priv_data = NULL;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307023 if (starget) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307024 starget_printk(KERN_INFO, starget,
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307025 "handle(0x%04x), sas_addr(0x%016llx)\n",
7026 sas_device_pg0->DevHandle,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307027 (unsigned long long)
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307028 sas_device->sas_address);
7029
7030 if (sas_device->enclosure_handle != 0)
7031 starget_printk(KERN_INFO, starget,
7032 "enclosure logical id(0x%016llx),"
7033 " slot(%d)\n",
7034 (unsigned long long)
7035 sas_device->enclosure_logical_id,
7036 sas_device->slot);
7037 }
7038 if (sas_device_pg0->Flags &
7039 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7040 sas_device->enclosure_level =
7041 le16_to_cpu(sas_device_pg0->EnclosureLevel);
7042 memcpy(&sas_device->connector_name[0],
7043 &sas_device_pg0->ConnectorName[0], 4);
7044 } else {
7045 sas_device->enclosure_level = 0;
7046 sas_device->connector_name[0] = '\0';
7047 }
7048
7049 if (sas_device->handle == sas_device_pg0->DevHandle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307050 goto out;
7051 pr_info("\thandle changed from(0x%04x)!!!\n",
7052 sas_device->handle);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307053 sas_device->handle = sas_device_pg0->DevHandle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307054 if (sas_target_priv_data)
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307055 sas_target_priv_data->handle =
7056 sas_device_pg0->DevHandle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307057 goto out;
7058 }
7059 }
7060 out:
7061 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7062}
7063
7064/**
7065 * _scsih_search_responding_sas_devices -
7066 * @ioc: per adapter object
7067 *
7068 * After host reset, find out whether devices are still responding.
7069 * If not remove.
7070 *
7071 * Return nothing.
7072 */
7073static void
7074_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7075{
7076 Mpi2SasDevicePage0_t sas_device_pg0;
7077 Mpi2ConfigReply_t mpi_reply;
7078 u16 ioc_status;
7079 u16 handle;
7080 u32 device_info;
7081
7082 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7083
7084 if (list_empty(&ioc->sas_device_list))
7085 goto out;
7086
7087 handle = 0xFFFF;
7088 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7089 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7090 handle))) {
7091 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7092 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307093 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307094 break;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307095 handle = sas_device_pg0.DevHandle =
7096 le16_to_cpu(sas_device_pg0.DevHandle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307097 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7098 if (!(_scsih_is_end_device(device_info)))
7099 continue;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307100 sas_device_pg0.SASAddress =
7101 le64_to_cpu(sas_device_pg0.SASAddress);
7102 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
7103 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307104 }
7105
7106 out:
7107 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7108 ioc->name);
7109}
7110
7111/**
7112 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7113 * @ioc: per adapter object
7114 * @wwid: world wide identifier for raid volume
7115 * @handle: device handle
7116 *
7117 * After host reset, find out whether devices are still responding.
7118 * Used in _scsih_remove_unresponsive_raid_devices.
7119 *
7120 * Return nothing.
7121 */
7122static void
7123_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7124 u16 handle)
7125{
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307126 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307127 struct scsi_target *starget;
7128 struct _raid_device *raid_device;
7129 unsigned long flags;
7130
7131 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7132 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7133 if (raid_device->wwid == wwid && raid_device->starget) {
7134 starget = raid_device->starget;
7135 if (starget && starget->hostdata) {
7136 sas_target_priv_data = starget->hostdata;
7137 sas_target_priv_data->deleted = 0;
7138 } else
7139 sas_target_priv_data = NULL;
7140 raid_device->responding = 1;
7141 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7142 starget_printk(KERN_INFO, raid_device->starget,
7143 "handle(0x%04x), wwid(0x%016llx)\n", handle,
7144 (unsigned long long)raid_device->wwid);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307145
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307146 /*
7147 * WARPDRIVE: The handles of the PDs might have changed
7148 * across the host reset so re-initialize the
7149 * required data for Direct IO
7150 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05307151 mpt3sas_init_warpdrive_properties(ioc, raid_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307152 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7153 if (raid_device->handle == handle) {
7154 spin_unlock_irqrestore(&ioc->raid_device_lock,
7155 flags);
7156 return;
7157 }
7158 pr_info("\thandle changed from(0x%04x)!!!\n",
7159 raid_device->handle);
7160 raid_device->handle = handle;
7161 if (sas_target_priv_data)
7162 sas_target_priv_data->handle = handle;
7163 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7164 return;
7165 }
7166 }
7167 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7168}
7169
7170/**
7171 * _scsih_search_responding_raid_devices -
7172 * @ioc: per adapter object
7173 *
7174 * After host reset, find out whether devices are still responding.
7175 * If not remove.
7176 *
7177 * Return nothing.
7178 */
7179static void
7180_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7181{
7182 Mpi2RaidVolPage1_t volume_pg1;
7183 Mpi2RaidVolPage0_t volume_pg0;
7184 Mpi2RaidPhysDiskPage0_t pd_pg0;
7185 Mpi2ConfigReply_t mpi_reply;
7186 u16 ioc_status;
7187 u16 handle;
7188 u8 phys_disk_num;
7189
7190 if (!ioc->ir_firmware)
7191 return;
7192
7193 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7194 ioc->name);
7195
7196 if (list_empty(&ioc->raid_device_list))
7197 goto out;
7198
7199 handle = 0xFFFF;
7200 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7201 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7202 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7203 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307204 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307205 break;
7206 handle = le16_to_cpu(volume_pg1.DevHandle);
7207
7208 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7209 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7210 sizeof(Mpi2RaidVolPage0_t)))
7211 continue;
7212
7213 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7214 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7215 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7216 _scsih_mark_responding_raid_device(ioc,
7217 le64_to_cpu(volume_pg1.WWID), handle);
7218 }
7219
7220 /* refresh the pd_handles */
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307221 if (!ioc->is_warpdrive) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307222 phys_disk_num = 0xFF;
7223 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7224 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7225 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7226 phys_disk_num))) {
7227 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7228 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307229 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307230 break;
7231 phys_disk_num = pd_pg0.PhysDiskNum;
7232 handle = le16_to_cpu(pd_pg0.DevHandle);
7233 set_bit(handle, ioc->pd_handles);
7234 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307235 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307236 out:
7237 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7238 ioc->name);
7239}
7240
7241/**
7242 * _scsih_mark_responding_expander - mark a expander as responding
7243 * @ioc: per adapter object
7244 * @sas_address: sas address
7245 * @handle:
7246 *
7247 * After host reset, find out whether devices are still responding.
7248 * Used in _scsih_remove_unresponsive_expanders.
7249 *
7250 * Return nothing.
7251 */
7252static void
7253_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7254 u16 handle)
7255{
7256 struct _sas_node *sas_expander;
7257 unsigned long flags;
7258 int i;
7259
7260 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7261 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7262 if (sas_expander->sas_address != sas_address)
7263 continue;
7264 sas_expander->responding = 1;
7265 if (sas_expander->handle == handle)
7266 goto out;
7267 pr_info("\texpander(0x%016llx): handle changed" \
7268 " from(0x%04x) to (0x%04x)!!!\n",
7269 (unsigned long long)sas_expander->sas_address,
7270 sas_expander->handle, handle);
7271 sas_expander->handle = handle;
7272 for (i = 0 ; i < sas_expander->num_phys ; i++)
7273 sas_expander->phy[i].handle = handle;
7274 goto out;
7275 }
7276 out:
7277 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7278}
7279
7280/**
7281 * _scsih_search_responding_expanders -
7282 * @ioc: per adapter object
7283 *
7284 * After host reset, find out whether devices are still responding.
7285 * If not remove.
7286 *
7287 * Return nothing.
7288 */
7289static void
7290_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7291{
7292 Mpi2ExpanderPage0_t expander_pg0;
7293 Mpi2ConfigReply_t mpi_reply;
7294 u16 ioc_status;
7295 u64 sas_address;
7296 u16 handle;
7297
7298 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7299
7300 if (list_empty(&ioc->sas_expander_list))
7301 goto out;
7302
7303 handle = 0xFFFF;
7304 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7305 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7306
7307 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7308 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307309 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307310 break;
7311
7312 handle = le16_to_cpu(expander_pg0.DevHandle);
7313 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7314 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7315 handle,
7316 (unsigned long long)sas_address);
7317 _scsih_mark_responding_expander(ioc, sas_address, handle);
7318 }
7319
7320 out:
7321 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7322}
7323
7324/**
7325 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7326 * @ioc: per adapter object
7327 *
7328 * Return nothing.
7329 */
7330static void
7331_scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7332{
7333 struct _sas_device *sas_device, *sas_device_next;
7334 struct _sas_node *sas_expander, *sas_expander_next;
7335 struct _raid_device *raid_device, *raid_device_next;
7336 struct list_head tmp_list;
7337 unsigned long flags;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307338 LIST_HEAD(head);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307339
7340 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7341 ioc->name);
7342
7343 /* removing unresponding end devices */
7344 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7345 ioc->name);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307346 /*
7347 * Iterate, pulling off devices marked as non-responding. We become the
7348 * owner for the reference the list had on any object we prune.
7349 */
7350 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307351 list_for_each_entry_safe(sas_device, sas_device_next,
7352 &ioc->sas_device_list, list) {
7353 if (!sas_device->responding)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307354 list_move_tail(&sas_device->list, &head);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307355 else
7356 sas_device->responding = 0;
7357 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307358 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7359
7360 /*
7361 * Now, uninitialize and remove the unresponding devices we pruned.
7362 */
7363 list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7364 _scsih_remove_device(ioc, sas_device);
7365 list_del_init(&sas_device->list);
7366 sas_device_put(sas_device);
7367 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307368
7369 /* removing unresponding volumes */
7370 if (ioc->ir_firmware) {
7371 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7372 ioc->name);
7373 list_for_each_entry_safe(raid_device, raid_device_next,
7374 &ioc->raid_device_list, list) {
7375 if (!raid_device->responding)
7376 _scsih_sas_volume_delete(ioc,
7377 raid_device->handle);
7378 else
7379 raid_device->responding = 0;
7380 }
7381 }
7382
7383 /* removing unresponding expanders */
7384 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7385 ioc->name);
7386 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7387 INIT_LIST_HEAD(&tmp_list);
7388 list_for_each_entry_safe(sas_expander, sas_expander_next,
7389 &ioc->sas_expander_list, list) {
7390 if (!sas_expander->responding)
7391 list_move_tail(&sas_expander->list, &tmp_list);
7392 else
7393 sas_expander->responding = 0;
7394 }
7395 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7396 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7397 list) {
7398 list_del(&sas_expander->list);
7399 _scsih_expander_node_remove(ioc, sas_expander);
7400 }
7401
7402 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7403 ioc->name);
7404
7405 /* unblock devices */
7406 _scsih_ublock_io_all_device(ioc);
7407}
7408
7409static void
7410_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7411 struct _sas_node *sas_expander, u16 handle)
7412{
7413 Mpi2ExpanderPage1_t expander_pg1;
7414 Mpi2ConfigReply_t mpi_reply;
7415 int i;
7416
7417 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7418 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7419 &expander_pg1, i, handle))) {
7420 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7421 ioc->name, __FILE__, __LINE__, __func__);
7422 return;
7423 }
7424
7425 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7426 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7427 expander_pg1.NegotiatedLinkRate >> 4);
7428 }
7429}
7430
7431/**
7432 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7433 * @ioc: per adapter object
7434 *
7435 * Return nothing.
7436 */
7437static void
7438_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7439{
7440 Mpi2ExpanderPage0_t expander_pg0;
7441 Mpi2SasDevicePage0_t sas_device_pg0;
7442 Mpi2RaidVolPage1_t volume_pg1;
7443 Mpi2RaidVolPage0_t volume_pg0;
7444 Mpi2RaidPhysDiskPage0_t pd_pg0;
7445 Mpi2EventIrConfigElement_t element;
7446 Mpi2ConfigReply_t mpi_reply;
7447 u8 phys_disk_num;
7448 u16 ioc_status;
7449 u16 handle, parent_handle;
7450 u64 sas_address;
7451 struct _sas_device *sas_device;
7452 struct _sas_node *expander_device;
7453 static struct _raid_device *raid_device;
7454 u8 retry_count;
7455 unsigned long flags;
7456
7457 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7458
7459 _scsih_sas_host_refresh(ioc);
7460
7461 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7462
7463 /* expanders */
7464 handle = 0xFFFF;
7465 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7466 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7467 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7468 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307469 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7470 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7471 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7472 ioc->name, ioc_status,
7473 le32_to_cpu(mpi_reply.IOCLogInfo));
7474 break;
7475 }
7476 handle = le16_to_cpu(expander_pg0.DevHandle);
7477 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7478 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7479 ioc, le64_to_cpu(expander_pg0.SASAddress));
7480 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7481 if (expander_device)
7482 _scsih_refresh_expander_links(ioc, expander_device,
7483 handle);
7484 else {
7485 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7486 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7487 handle, (unsigned long long)
7488 le64_to_cpu(expander_pg0.SASAddress));
7489 _scsih_expander_add(ioc, handle);
7490 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7491 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7492 handle, (unsigned long long)
7493 le64_to_cpu(expander_pg0.SASAddress));
7494 }
7495 }
7496
7497 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7498 ioc->name);
7499
7500 if (!ioc->ir_firmware)
7501 goto skip_to_sas;
7502
7503 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7504
7505 /* phys disk */
7506 phys_disk_num = 0xFF;
7507 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7508 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7509 phys_disk_num))) {
7510 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7511 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307512 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7513 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7514 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7515 ioc->name, ioc_status,
7516 le32_to_cpu(mpi_reply.IOCLogInfo));
7517 break;
7518 }
7519 phys_disk_num = pd_pg0.PhysDiskNum;
7520 handle = le16_to_cpu(pd_pg0.DevHandle);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307521 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7522 if (sas_device) {
7523 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307524 continue;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307525 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307526 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7527 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7528 handle) != 0)
7529 continue;
7530 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7531 MPI2_IOCSTATUS_MASK;
7532 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7533 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7534 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7535 ioc->name, ioc_status,
7536 le32_to_cpu(mpi_reply.IOCLogInfo));
7537 break;
7538 }
7539 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7540 if (!_scsih_get_sas_address(ioc, parent_handle,
7541 &sas_address)) {
7542 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7543 " handle (0x%04x), sas_addr(0x%016llx)\n",
7544 ioc->name, handle, (unsigned long long)
7545 le64_to_cpu(sas_device_pg0.SASAddress));
7546 mpt3sas_transport_update_links(ioc, sas_address,
7547 handle, sas_device_pg0.PhyNum,
7548 MPI2_SAS_NEG_LINK_RATE_1_5);
7549 set_bit(handle, ioc->pd_handles);
7550 retry_count = 0;
7551 /* This will retry adding the end device.
7552 * _scsih_add_device() will decide on retries and
7553 * return "1" when it should be retried
7554 */
7555 while (_scsih_add_device(ioc, handle, retry_count++,
7556 1)) {
7557 ssleep(1);
7558 }
7559 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7560 " handle (0x%04x), sas_addr(0x%016llx)\n",
7561 ioc->name, handle, (unsigned long long)
7562 le64_to_cpu(sas_device_pg0.SASAddress));
7563 }
7564 }
7565
7566 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7567 ioc->name);
7568
7569 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7570
7571 /* volumes */
7572 handle = 0xFFFF;
7573 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7574 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7575 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7576 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307577 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7578 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7579 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7580 ioc->name, ioc_status,
7581 le32_to_cpu(mpi_reply.IOCLogInfo));
7582 break;
7583 }
7584 handle = le16_to_cpu(volume_pg1.DevHandle);
7585 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7586 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7587 le64_to_cpu(volume_pg1.WWID));
7588 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7589 if (raid_device)
7590 continue;
7591 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7592 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7593 sizeof(Mpi2RaidVolPage0_t)))
7594 continue;
7595 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7596 MPI2_IOCSTATUS_MASK;
7597 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7598 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7599 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7600 ioc->name, ioc_status,
7601 le32_to_cpu(mpi_reply.IOCLogInfo));
7602 break;
7603 }
7604 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7605 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7606 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7607 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7608 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7609 element.VolDevHandle = volume_pg1.DevHandle;
7610 pr_info(MPT3SAS_FMT
7611 "\tBEFORE adding volume: handle (0x%04x)\n",
7612 ioc->name, volume_pg1.DevHandle);
7613 _scsih_sas_volume_add(ioc, &element);
7614 pr_info(MPT3SAS_FMT
7615 "\tAFTER adding volume: handle (0x%04x)\n",
7616 ioc->name, volume_pg1.DevHandle);
7617 }
7618 }
7619
7620 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7621 ioc->name);
7622
7623 skip_to_sas:
7624
7625 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7626 ioc->name);
7627
7628 /* sas devices */
7629 handle = 0xFFFF;
7630 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7631 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7632 handle))) {
7633 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7634 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307635 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7636 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7637 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7638 ioc->name, ioc_status,
7639 le32_to_cpu(mpi_reply.IOCLogInfo));
7640 break;
7641 }
7642 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7643 if (!(_scsih_is_end_device(
7644 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7645 continue;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307646 sas_device = mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307647 le64_to_cpu(sas_device_pg0.SASAddress));
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307648 if (sas_device) {
7649 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307650 continue;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307651 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307652 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7653 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7654 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7655 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7656 handle, (unsigned long long)
7657 le64_to_cpu(sas_device_pg0.SASAddress));
7658 mpt3sas_transport_update_links(ioc, sas_address, handle,
7659 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7660 retry_count = 0;
7661 /* This will retry adding the end device.
7662 * _scsih_add_device() will decide on retries and
7663 * return "1" when it should be retried
7664 */
7665 while (_scsih_add_device(ioc, handle, retry_count++,
7666 0)) {
7667 ssleep(1);
7668 }
7669 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7670 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7671 handle, (unsigned long long)
7672 le64_to_cpu(sas_device_pg0.SASAddress));
7673 }
7674 }
7675 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7676 ioc->name);
7677
7678 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7679}
7680/**
7681 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7682 * @ioc: per adapter object
7683 * @reset_phase: phase
7684 *
7685 * The handler for doing any required cleanup or initialization.
7686 *
7687 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7688 * MPT3_IOC_DONE_RESET
7689 *
7690 * Return nothing.
7691 */
7692void
7693mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7694{
7695 switch (reset_phase) {
7696 case MPT3_IOC_PRE_RESET:
7697 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7698 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7699 break;
7700 case MPT3_IOC_AFTER_RESET:
7701 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7702 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7703 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7704 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7705 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7706 complete(&ioc->scsih_cmds.done);
7707 }
7708 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7709 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7710 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7711 complete(&ioc->tm_cmds.done);
7712 }
7713
7714 _scsih_fw_event_cleanup_queue(ioc);
7715 _scsih_flush_running_cmds(ioc);
7716 break;
7717 case MPT3_IOC_DONE_RESET:
7718 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7719 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7720 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7721 !ioc->sas_hba.num_phys)) {
7722 _scsih_prep_device_scan(ioc);
7723 _scsih_search_responding_sas_devices(ioc);
7724 _scsih_search_responding_raid_devices(ioc);
7725 _scsih_search_responding_expanders(ioc);
7726 _scsih_error_recovery_delete_devices(ioc);
7727 }
7728 break;
7729 }
7730}
7731
7732/**
7733 * _mpt3sas_fw_work - delayed task for processing firmware events
7734 * @ioc: per adapter object
7735 * @fw_event: The fw_event_work object
7736 * Context: user.
7737 *
7738 * Return nothing.
7739 */
7740static void
7741_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7742{
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307743 _scsih_fw_event_del_from_list(ioc, fw_event);
7744
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307745 /* the queue is being flushed so ignore this event */
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307746 if (ioc->remove_host || ioc->pci_error_recovery) {
7747 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307748 return;
7749 }
7750
7751 switch (fw_event->event) {
7752 case MPT3SAS_PROCESS_TRIGGER_DIAG:
Joe Lawrence35b62362014-06-25 17:05:34 -04007753 mpt3sas_process_trigger_data(ioc,
7754 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7755 fw_event->event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307756 break;
7757 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307758 while (scsi_host_in_recovery(ioc->shost) ||
7759 ioc->shost_recovery) {
7760 /*
7761 * If we're unloading, bail. Otherwise, this can become
7762 * an infinite loop.
7763 */
7764 if (ioc->remove_host)
7765 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307766 ssleep(1);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307767 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307768 _scsih_remove_unresponding_sas_devices(ioc);
7769 _scsih_scan_for_devices_after_reset(ioc);
7770 break;
7771 case MPT3SAS_PORT_ENABLE_COMPLETE:
7772 ioc->start_scan = 0;
7773 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7774 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7775 missing_delay[1]);
7776 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7777 "port enable: complete from worker thread\n",
7778 ioc->name));
7779 break;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05307780 case MPT3SAS_TURN_ON_PFA_LED:
7781 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307782 break;
7783 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7784 _scsih_sas_topology_change_event(ioc, fw_event);
7785 break;
7786 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7787 _scsih_sas_device_status_change_event(ioc, fw_event);
7788 break;
7789 case MPI2_EVENT_SAS_DISCOVERY:
7790 _scsih_sas_discovery_event(ioc, fw_event);
7791 break;
7792 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7793 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7794 break;
7795 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7796 _scsih_sas_enclosure_dev_status_change_event(ioc,
7797 fw_event);
7798 break;
7799 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7800 _scsih_sas_ir_config_change_event(ioc, fw_event);
7801 break;
7802 case MPI2_EVENT_IR_VOLUME:
7803 _scsih_sas_ir_volume_event(ioc, fw_event);
7804 break;
7805 case MPI2_EVENT_IR_PHYSICAL_DISK:
7806 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7807 break;
7808 case MPI2_EVENT_IR_OPERATION_STATUS:
7809 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7810 break;
7811 }
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307812out:
7813 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307814}
7815
7816/**
7817 * _firmware_event_work
7818 * @ioc: per adapter object
7819 * @work: The fw_event_work object
7820 * Context: user.
7821 *
7822 * wrappers for the work thread handling firmware events
7823 *
7824 * Return nothing.
7825 */
7826
7827static void
7828_firmware_event_work(struct work_struct *work)
7829{
7830 struct fw_event_work *fw_event = container_of(work,
7831 struct fw_event_work, work);
7832
7833 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7834}
7835
7836/**
7837 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7838 * @ioc: per adapter object
7839 * @msix_index: MSIX table index supplied by the OS
7840 * @reply: reply message frame(lower 32bit addr)
7841 * Context: interrupt.
7842 *
7843 * This function merely adds a new work task into ioc->firmware_event_thread.
7844 * The tasks are worked from _firmware_event_work in user context.
7845 *
7846 * Return 1 meaning mf should be freed from _base_interrupt
7847 * 0 means the mf is freed from this function.
7848 */
7849u8
7850mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7851 u32 reply)
7852{
7853 struct fw_event_work *fw_event;
7854 Mpi2EventNotificationReply_t *mpi_reply;
7855 u16 event;
7856 u16 sz;
Chaitra P Ba470a512016-05-06 14:29:27 +05307857 Mpi26EventDataActiveCableExcept_t *ActiveCableEventData;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307858
7859 /* events turned off due to host reset or driver unloading */
7860 if (ioc->remove_host || ioc->pci_error_recovery)
7861 return 1;
7862
7863 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7864
7865 if (unlikely(!mpi_reply)) {
7866 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7867 ioc->name, __FILE__, __LINE__, __func__);
7868 return 1;
7869 }
7870
7871 event = le16_to_cpu(mpi_reply->Event);
7872
7873 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7874 mpt3sas_trigger_event(ioc, event, 0);
7875
7876 switch (event) {
7877 /* handle these */
7878 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7879 {
7880 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7881 (Mpi2EventDataSasBroadcastPrimitive_t *)
7882 mpi_reply->EventData;
7883
7884 if (baen_data->Primitive !=
7885 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7886 return 1;
7887
7888 if (ioc->broadcast_aen_busy) {
7889 ioc->broadcast_aen_pending++;
7890 return 1;
7891 } else
7892 ioc->broadcast_aen_busy = 1;
7893 break;
7894 }
7895
7896 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7897 _scsih_check_topo_delete_events(ioc,
7898 (Mpi2EventDataSasTopologyChangeList_t *)
7899 mpi_reply->EventData);
7900 break;
7901 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7902 _scsih_check_ir_config_unhide_events(ioc,
7903 (Mpi2EventDataIrConfigChangeList_t *)
7904 mpi_reply->EventData);
7905 break;
7906 case MPI2_EVENT_IR_VOLUME:
7907 _scsih_check_volume_delete_events(ioc,
7908 (Mpi2EventDataIrVolume_t *)
7909 mpi_reply->EventData);
7910 break;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307911 case MPI2_EVENT_LOG_ENTRY_ADDED:
7912 {
7913 Mpi2EventDataLogEntryAdded_t *log_entry;
7914 u32 *log_code;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307915
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307916 if (!ioc->is_warpdrive)
7917 break;
7918
7919 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7920 mpi_reply->EventData;
7921 log_code = (u32 *)log_entry->LogData;
7922
7923 if (le16_to_cpu(log_entry->LogEntryQualifier)
7924 != MPT2_WARPDRIVE_LOGENTRY)
7925 break;
7926
7927 switch (le32_to_cpu(*log_code)) {
7928 case MPT2_WARPDRIVE_LC_SSDT:
7929 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7930 "IO Throttling has occurred in the WarpDrive "
7931 "subsystem. Check WarpDrive documentation for "
7932 "additional details.\n", ioc->name);
7933 break;
7934 case MPT2_WARPDRIVE_LC_SSDLW:
7935 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7936 "Program/Erase Cycles for the WarpDrive subsystem "
7937 "in degraded range. Check WarpDrive documentation "
7938 "for additional details.\n", ioc->name);
7939 break;
7940 case MPT2_WARPDRIVE_LC_SSDLF:
7941 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7942 "There are no Program/Erase Cycles for the "
7943 "WarpDrive subsystem. The storage device will be "
7944 "in read-only mode. Check WarpDrive documentation "
7945 "for additional details.\n", ioc->name);
7946 break;
7947 case MPT2_WARPDRIVE_LC_BRMF:
7948 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7949 "The Backup Rail Monitor has failed on the "
7950 "WarpDrive subsystem. Check WarpDrive "
7951 "documentation for additional details.\n",
7952 ioc->name);
7953 break;
7954 }
7955
7956 break;
7957 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307958 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7959 case MPI2_EVENT_IR_OPERATION_STATUS:
7960 case MPI2_EVENT_SAS_DISCOVERY:
7961 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7962 case MPI2_EVENT_IR_PHYSICAL_DISK:
7963 break;
7964
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05307965 case MPI2_EVENT_TEMP_THRESHOLD:
7966 _scsih_temp_threshold_events(ioc,
7967 (Mpi2EventDataTemperature_t *)
7968 mpi_reply->EventData);
7969 break;
Chaitra P Ba470a512016-05-06 14:29:27 +05307970 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
7971 ActiveCableEventData =
7972 (Mpi26EventDataActiveCableExcept_t *) mpi_reply->EventData;
7973 if (ActiveCableEventData->ReasonCode ==
Dan Carpenter7ebd67e2016-05-12 23:37:38 +03007974 MPI26_EVENT_ACTIVE_CABLE_INSUFFICIENT_POWER) {
Chaitra P Ba470a512016-05-06 14:29:27 +05307975 pr_info(MPT3SAS_FMT "Currently an active cable with ReceptacleID %d",
7976 ioc->name, ActiveCableEventData->ReceptacleID);
7977 pr_info("cannot be powered and devices connected to this active cable");
7978 pr_info("will not be seen. This active cable");
7979 pr_info("requires %d mW of power",
7980 ActiveCableEventData->ActiveCablePowerRequirement);
Dan Carpenter7ebd67e2016-05-12 23:37:38 +03007981 }
Chaitra P Ba470a512016-05-06 14:29:27 +05307982 break;
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05307983
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307984 default: /* ignore the rest */
7985 return 1;
7986 }
7987
Joe Lawrence35b62362014-06-25 17:05:34 -04007988 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307989 fw_event = alloc_fw_event_work(sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307990 if (!fw_event) {
7991 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7992 ioc->name, __FILE__, __LINE__, __func__);
7993 return 1;
7994 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307995
7996 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7997 fw_event->ioc = ioc;
7998 fw_event->VF_ID = mpi_reply->VF_ID;
7999 fw_event->VP_ID = mpi_reply->VP_ID;
8000 fw_event->event = event;
8001 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05308002 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308003 return 1;
8004}
8005
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308006/**
8007 * _scsih_expander_node_remove - removing expander device from list.
8008 * @ioc: per adapter object
8009 * @sas_expander: the sas_device object
8010 * Context: Calling function should acquire ioc->sas_node_lock.
8011 *
8012 * Removing object and freeing associated memory from the
8013 * ioc->sas_expander_list.
8014 *
8015 * Return nothing.
8016 */
8017static void
8018_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
8019 struct _sas_node *sas_expander)
8020{
8021 struct _sas_port *mpt3sas_port, *next;
8022
8023 /* remove sibling ports attached to this expander */
8024 list_for_each_entry_safe(mpt3sas_port, next,
8025 &sas_expander->sas_port_list, port_list) {
8026 if (ioc->shost_recovery)
8027 return;
8028 if (mpt3sas_port->remote_identify.device_type ==
8029 SAS_END_DEVICE)
8030 mpt3sas_device_remove_by_sas_address(ioc,
8031 mpt3sas_port->remote_identify.sas_address);
8032 else if (mpt3sas_port->remote_identify.device_type ==
8033 SAS_EDGE_EXPANDER_DEVICE ||
8034 mpt3sas_port->remote_identify.device_type ==
8035 SAS_FANOUT_EXPANDER_DEVICE)
8036 mpt3sas_expander_remove(ioc,
8037 mpt3sas_port->remote_identify.sas_address);
8038 }
8039
8040 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8041 sas_expander->sas_address_parent);
8042
8043 pr_info(MPT3SAS_FMT
8044 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8045 ioc->name,
8046 sas_expander->handle, (unsigned long long)
8047 sas_expander->sas_address);
8048
8049 kfree(sas_expander->phy);
8050 kfree(sas_expander);
8051}
8052
8053/**
8054 * _scsih_ir_shutdown - IR shutdown notification
8055 * @ioc: per adapter object
8056 *
8057 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8058 * the host system is shutting down.
8059 *
8060 * Return nothing.
8061 */
8062static void
8063_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8064{
8065 Mpi2RaidActionRequest_t *mpi_request;
8066 Mpi2RaidActionReply_t *mpi_reply;
8067 u16 smid;
8068
8069 /* is IR firmware build loaded ? */
8070 if (!ioc->ir_firmware)
8071 return;
8072
8073 /* are there any volumes ? */
8074 if (list_empty(&ioc->raid_device_list))
8075 return;
8076
8077 mutex_lock(&ioc->scsih_cmds.mutex);
8078
8079 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8080 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8081 ioc->name, __func__);
8082 goto out;
8083 }
8084 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8085
8086 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8087 if (!smid) {
8088 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8089 ioc->name, __func__);
8090 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8091 goto out;
8092 }
8093
8094 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8095 ioc->scsih_cmds.smid = smid;
8096 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8097
8098 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8099 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8100
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308101 if (!ioc->hide_ir_msg)
8102 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308103 init_completion(&ioc->scsih_cmds.done);
8104 mpt3sas_base_put_smid_default(ioc, smid);
8105 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8106
8107 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8108 pr_err(MPT3SAS_FMT "%s: timeout\n",
8109 ioc->name, __func__);
8110 goto out;
8111 }
8112
8113 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8114 mpi_reply = ioc->scsih_cmds.reply;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308115 if (!ioc->hide_ir_msg)
8116 pr_info(MPT3SAS_FMT "IR shutdown "
8117 "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8118 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8119 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308120 }
8121
8122 out:
8123 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8124 mutex_unlock(&ioc->scsih_cmds.mutex);
8125}
8126
8127/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308128 * scsih_remove - detach and remove add host
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308129 * @pdev: PCI device struct
8130 *
8131 * Routine called when unloading the driver.
8132 * Return nothing.
8133 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308134void scsih_remove(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308135{
8136 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8137 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8138 struct _sas_port *mpt3sas_port, *next_port;
8139 struct _raid_device *raid_device, *next;
8140 struct MPT3SAS_TARGET *sas_target_priv_data;
8141 struct workqueue_struct *wq;
8142 unsigned long flags;
8143
8144 ioc->remove_host = 1;
8145 _scsih_fw_event_cleanup_queue(ioc);
8146
8147 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8148 wq = ioc->firmware_event_thread;
8149 ioc->firmware_event_thread = NULL;
8150 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8151 if (wq)
8152 destroy_workqueue(wq);
8153
8154 /* release all the volumes */
8155 _scsih_ir_shutdown(ioc);
8156 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8157 list) {
8158 if (raid_device->starget) {
8159 sas_target_priv_data =
8160 raid_device->starget->hostdata;
8161 sas_target_priv_data->deleted = 1;
8162 scsi_remove_target(&raid_device->starget->dev);
8163 }
8164 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8165 ioc->name, raid_device->handle,
8166 (unsigned long long) raid_device->wwid);
8167 _scsih_raid_device_remove(ioc, raid_device);
8168 }
8169
8170 /* free ports attached to the sas_host */
8171 list_for_each_entry_safe(mpt3sas_port, next_port,
8172 &ioc->sas_hba.sas_port_list, port_list) {
8173 if (mpt3sas_port->remote_identify.device_type ==
8174 SAS_END_DEVICE)
8175 mpt3sas_device_remove_by_sas_address(ioc,
8176 mpt3sas_port->remote_identify.sas_address);
8177 else if (mpt3sas_port->remote_identify.device_type ==
8178 SAS_EDGE_EXPANDER_DEVICE ||
8179 mpt3sas_port->remote_identify.device_type ==
8180 SAS_FANOUT_EXPANDER_DEVICE)
8181 mpt3sas_expander_remove(ioc,
8182 mpt3sas_port->remote_identify.sas_address);
8183 }
8184
8185 /* free phys attached to the sas_host */
8186 if (ioc->sas_hba.num_phys) {
8187 kfree(ioc->sas_hba.phy);
8188 ioc->sas_hba.phy = NULL;
8189 ioc->sas_hba.num_phys = 0;
8190 }
8191
8192 sas_remove_host(shost);
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308193 scsi_remove_host(shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308194 mpt3sas_base_detach(ioc);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308195 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308196 list_del(&ioc->list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308197 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308198 scsi_host_put(shost);
8199}
8200
8201/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308202 * scsih_shutdown - routine call during system shutdown
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308203 * @pdev: PCI device struct
8204 *
8205 * Return nothing.
8206 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308207void
8208scsih_shutdown(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308209{
8210 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8211 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8212 struct workqueue_struct *wq;
8213 unsigned long flags;
8214
8215 ioc->remove_host = 1;
8216 _scsih_fw_event_cleanup_queue(ioc);
8217
8218 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8219 wq = ioc->firmware_event_thread;
8220 ioc->firmware_event_thread = NULL;
8221 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8222 if (wq)
8223 destroy_workqueue(wq);
8224
8225 _scsih_ir_shutdown(ioc);
8226 mpt3sas_base_detach(ioc);
8227}
8228
8229
8230/**
8231 * _scsih_probe_boot_devices - reports 1st device
8232 * @ioc: per adapter object
8233 *
8234 * If specified in bios page 2, this routine reports the 1st
8235 * device scsi-ml or sas transport for persistent boot device
8236 * purposes. Please refer to function _scsih_determine_boot_device()
8237 */
8238static void
8239_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8240{
8241 u8 is_raid;
8242 void *device;
8243 struct _sas_device *sas_device;
8244 struct _raid_device *raid_device;
8245 u16 handle;
8246 u64 sas_address_parent;
8247 u64 sas_address;
8248 unsigned long flags;
8249 int rc;
8250
8251 /* no Bios, return immediately */
8252 if (!ioc->bios_pg3.BiosVersion)
8253 return;
8254
8255 device = NULL;
8256 is_raid = 0;
8257 if (ioc->req_boot_device.device) {
8258 device = ioc->req_boot_device.device;
8259 is_raid = ioc->req_boot_device.is_raid;
8260 } else if (ioc->req_alt_boot_device.device) {
8261 device = ioc->req_alt_boot_device.device;
8262 is_raid = ioc->req_alt_boot_device.is_raid;
8263 } else if (ioc->current_boot_device.device) {
8264 device = ioc->current_boot_device.device;
8265 is_raid = ioc->current_boot_device.is_raid;
8266 }
8267
8268 if (!device)
8269 return;
8270
8271 if (is_raid) {
8272 raid_device = device;
8273 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8274 raid_device->id, 0);
8275 if (rc)
8276 _scsih_raid_device_remove(ioc, raid_device);
8277 } else {
8278 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8279 sas_device = device;
8280 handle = sas_device->handle;
8281 sas_address_parent = sas_device->sas_address_parent;
8282 sas_address = sas_device->sas_address;
8283 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8284 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8285
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308286 if (ioc->hide_drives)
8287 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308288 if (!mpt3sas_transport_port_add(ioc, handle,
8289 sas_address_parent)) {
8290 _scsih_sas_device_remove(ioc, sas_device);
8291 } else if (!sas_device->starget) {
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308292 if (!ioc->is_driver_loading) {
8293 mpt3sas_transport_port_remove(ioc,
8294 sas_address,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308295 sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308296 _scsih_sas_device_remove(ioc, sas_device);
8297 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308298 }
8299 }
8300}
8301
8302/**
8303 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8304 * @ioc: per adapter object
8305 *
8306 * Called during initial loading of the driver.
8307 */
8308static void
8309_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8310{
8311 struct _raid_device *raid_device, *raid_next;
8312 int rc;
8313
8314 list_for_each_entry_safe(raid_device, raid_next,
8315 &ioc->raid_device_list, list) {
8316 if (raid_device->starget)
8317 continue;
8318 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8319 raid_device->id, 0);
8320 if (rc)
8321 _scsih_raid_device_remove(ioc, raid_device);
8322 }
8323}
8324
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308325static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8326{
8327 struct _sas_device *sas_device = NULL;
8328 unsigned long flags;
8329
8330 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8331 if (!list_empty(&ioc->sas_device_init_list)) {
8332 sas_device = list_first_entry(&ioc->sas_device_init_list,
8333 struct _sas_device, list);
8334 sas_device_get(sas_device);
8335 }
8336 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8337
8338 return sas_device;
8339}
8340
8341static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8342 struct _sas_device *sas_device)
8343{
8344 unsigned long flags;
8345
8346 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8347
8348 /*
8349 * Since we dropped the lock during the call to port_add(), we need to
8350 * be careful here that somebody else didn't move or delete this item
8351 * while we were busy with other things.
8352 *
8353 * If it was on the list, we need a put() for the reference the list
8354 * had. Either way, we need a get() for the destination list.
8355 */
8356 if (!list_empty(&sas_device->list)) {
8357 list_del_init(&sas_device->list);
8358 sas_device_put(sas_device);
8359 }
8360
8361 sas_device_get(sas_device);
8362 list_add_tail(&sas_device->list, &ioc->sas_device_list);
8363
8364 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8365}
8366
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308367/**
8368 * _scsih_probe_sas - reporting sas devices to sas transport
8369 * @ioc: per adapter object
8370 *
8371 * Called during initial loading of the driver.
8372 */
8373static void
8374_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8375{
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308376 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308377
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308378 if (ioc->hide_drives)
8379 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308380
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308381 while ((sas_device = get_next_sas_device(ioc))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308382 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8383 sas_device->sas_address_parent)) {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308384 _scsih_sas_device_remove(ioc, sas_device);
8385 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308386 continue;
8387 } else if (!sas_device->starget) {
8388 /*
8389 * When asyn scanning is enabled, its not possible to
8390 * remove devices while scanning is turned on due to an
8391 * oops in scsi_sysfs_add_sdev()->add_device()->
8392 * sysfs_addrm_start()
8393 */
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308394 if (!ioc->is_driver_loading) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308395 mpt3sas_transport_port_remove(ioc,
8396 sas_device->sas_address,
8397 sas_device->sas_address_parent);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308398 _scsih_sas_device_remove(ioc, sas_device);
8399 sas_device_put(sas_device);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308400 continue;
8401 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308402 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308403 sas_device_make_active(ioc, sas_device);
8404 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308405 }
8406}
8407
8408/**
8409 * _scsih_probe_devices - probing for devices
8410 * @ioc: per adapter object
8411 *
8412 * Called during initial loading of the driver.
8413 */
8414static void
8415_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8416{
8417 u16 volume_mapping_flags;
8418
8419 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8420 return; /* return when IOC doesn't support initiator mode */
8421
8422 _scsih_probe_boot_devices(ioc);
8423
8424 if (ioc->ir_firmware) {
8425 volume_mapping_flags =
8426 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8427 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8428 if (volume_mapping_flags ==
8429 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8430 _scsih_probe_raid(ioc);
8431 _scsih_probe_sas(ioc);
8432 } else {
8433 _scsih_probe_sas(ioc);
8434 _scsih_probe_raid(ioc);
8435 }
8436 } else
8437 _scsih_probe_sas(ioc);
8438}
8439
8440/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308441 * scsih_scan_start - scsi lld callback for .scan_start
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308442 * @shost: SCSI host pointer
8443 *
8444 * The shost has the ability to discover targets on its own instead
8445 * of scanning the entire bus. In our implemention, we will kick off
8446 * firmware discovery.
8447 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308448void
8449scsih_scan_start(struct Scsi_Host *shost)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308450{
8451 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8452 int rc;
8453 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8454 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8455
8456 if (disable_discovery > 0)
8457 return;
8458
8459 ioc->start_scan = 1;
8460 rc = mpt3sas_port_enable(ioc);
8461
8462 if (rc != 0)
8463 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8464}
8465
8466/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308467 * scsih_scan_finished - scsi lld callback for .scan_finished
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308468 * @shost: SCSI host pointer
8469 * @time: elapsed time of the scan in jiffies
8470 *
8471 * This function will be called periodicallyn until it returns 1 with the
8472 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8473 * we wait for firmware discovery to complete, then return 1.
8474 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308475int
8476scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308477{
8478 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8479
8480 if (disable_discovery > 0) {
8481 ioc->is_driver_loading = 0;
8482 ioc->wait_for_discovery_to_complete = 0;
8483 return 1;
8484 }
8485
8486 if (time >= (300 * HZ)) {
8487 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8488 pr_info(MPT3SAS_FMT
8489 "port enable: FAILED with timeout (timeout=300s)\n",
8490 ioc->name);
8491 ioc->is_driver_loading = 0;
8492 return 1;
8493 }
8494
8495 if (ioc->start_scan)
8496 return 0;
8497
8498 if (ioc->start_scan_failed) {
8499 pr_info(MPT3SAS_FMT
8500 "port enable: FAILED with (ioc_status=0x%08x)\n",
8501 ioc->name, ioc->start_scan_failed);
8502 ioc->is_driver_loading = 0;
8503 ioc->wait_for_discovery_to_complete = 0;
8504 ioc->remove_host = 1;
8505 return 1;
8506 }
8507
8508 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8509 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8510
8511 if (ioc->wait_for_discovery_to_complete) {
8512 ioc->wait_for_discovery_to_complete = 0;
8513 _scsih_probe_devices(ioc);
8514 }
8515 mpt3sas_base_start_watchdog(ioc);
8516 ioc->is_driver_loading = 0;
8517 return 1;
8518}
8519
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308520/* shost template for SAS 2.0 HBA devices */
8521static struct scsi_host_template mpt2sas_driver_template = {
8522 .module = THIS_MODULE,
8523 .name = "Fusion MPT SAS Host",
8524 .proc_name = MPT2SAS_DRIVER_NAME,
8525 .queuecommand = scsih_qcmd,
8526 .target_alloc = scsih_target_alloc,
8527 .slave_alloc = scsih_slave_alloc,
8528 .slave_configure = scsih_slave_configure,
8529 .target_destroy = scsih_target_destroy,
8530 .slave_destroy = scsih_slave_destroy,
8531 .scan_finished = scsih_scan_finished,
8532 .scan_start = scsih_scan_start,
8533 .change_queue_depth = scsih_change_queue_depth,
8534 .eh_abort_handler = scsih_abort,
8535 .eh_device_reset_handler = scsih_dev_reset,
8536 .eh_target_reset_handler = scsih_target_reset,
8537 .eh_host_reset_handler = scsih_host_reset,
8538 .bios_param = scsih_bios_param,
8539 .can_queue = 1,
8540 .this_id = -1,
8541 .sg_tablesize = MPT2SAS_SG_DEPTH,
8542 .max_sectors = 32767,
8543 .cmd_per_lun = 7,
8544 .use_clustering = ENABLE_CLUSTERING,
8545 .shost_attrs = mpt3sas_host_attrs,
8546 .sdev_attrs = mpt3sas_dev_attrs,
8547 .track_queue_depth = 1,
8548};
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308549
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308550/* raid transport support for SAS 2.0 HBA devices */
8551static struct raid_function_template mpt2sas_raid_functions = {
8552 .cookie = &mpt2sas_driver_template,
8553 .is_raid = scsih_is_raid,
8554 .get_resync = scsih_get_resync,
8555 .get_state = scsih_get_state,
8556};
8557
8558/* shost template for SAS 3.0 HBA devices */
8559static struct scsi_host_template mpt3sas_driver_template = {
8560 .module = THIS_MODULE,
8561 .name = "Fusion MPT SAS Host",
8562 .proc_name = MPT3SAS_DRIVER_NAME,
8563 .queuecommand = scsih_qcmd,
8564 .target_alloc = scsih_target_alloc,
8565 .slave_alloc = scsih_slave_alloc,
8566 .slave_configure = scsih_slave_configure,
8567 .target_destroy = scsih_target_destroy,
8568 .slave_destroy = scsih_slave_destroy,
8569 .scan_finished = scsih_scan_finished,
8570 .scan_start = scsih_scan_start,
8571 .change_queue_depth = scsih_change_queue_depth,
8572 .eh_abort_handler = scsih_abort,
8573 .eh_device_reset_handler = scsih_dev_reset,
8574 .eh_target_reset_handler = scsih_target_reset,
8575 .eh_host_reset_handler = scsih_host_reset,
8576 .bios_param = scsih_bios_param,
8577 .can_queue = 1,
8578 .this_id = -1,
8579 .sg_tablesize = MPT3SAS_SG_DEPTH,
8580 .max_sectors = 32767,
8581 .cmd_per_lun = 7,
8582 .use_clustering = ENABLE_CLUSTERING,
8583 .shost_attrs = mpt3sas_host_attrs,
8584 .sdev_attrs = mpt3sas_dev_attrs,
8585 .track_queue_depth = 1,
8586};
8587
8588/* raid transport support for SAS 3.0 HBA devices */
8589static struct raid_function_template mpt3sas_raid_functions = {
8590 .cookie = &mpt3sas_driver_template,
8591 .is_raid = scsih_is_raid,
8592 .get_resync = scsih_get_resync,
8593 .get_state = scsih_get_state,
8594};
8595
8596/**
8597 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8598 * this device belongs to.
8599 * @pdev: PCI device struct
8600 *
8601 * return MPI2_VERSION for SAS 2.0 HBA devices,
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308602 * MPI25_VERSION for SAS 3.0 HBA devices, and
8603 * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308604 */
8605u16
8606_scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8607{
8608
8609 switch (pdev->device) {
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308610 case MPI2_MFGPAGE_DEVID_SSS6200:
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308611 case MPI2_MFGPAGE_DEVID_SAS2004:
8612 case MPI2_MFGPAGE_DEVID_SAS2008:
8613 case MPI2_MFGPAGE_DEVID_SAS2108_1:
8614 case MPI2_MFGPAGE_DEVID_SAS2108_2:
8615 case MPI2_MFGPAGE_DEVID_SAS2108_3:
8616 case MPI2_MFGPAGE_DEVID_SAS2116_1:
8617 case MPI2_MFGPAGE_DEVID_SAS2116_2:
8618 case MPI2_MFGPAGE_DEVID_SAS2208_1:
8619 case MPI2_MFGPAGE_DEVID_SAS2208_2:
8620 case MPI2_MFGPAGE_DEVID_SAS2208_3:
8621 case MPI2_MFGPAGE_DEVID_SAS2208_4:
8622 case MPI2_MFGPAGE_DEVID_SAS2208_5:
8623 case MPI2_MFGPAGE_DEVID_SAS2208_6:
8624 case MPI2_MFGPAGE_DEVID_SAS2308_1:
8625 case MPI2_MFGPAGE_DEVID_SAS2308_2:
8626 case MPI2_MFGPAGE_DEVID_SAS2308_3:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308627 return MPI2_VERSION;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308628 case MPI25_MFGPAGE_DEVID_SAS3004:
8629 case MPI25_MFGPAGE_DEVID_SAS3008:
8630 case MPI25_MFGPAGE_DEVID_SAS3108_1:
8631 case MPI25_MFGPAGE_DEVID_SAS3108_2:
8632 case MPI25_MFGPAGE_DEVID_SAS3108_5:
8633 case MPI25_MFGPAGE_DEVID_SAS3108_6:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308634 return MPI25_VERSION;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308635 case MPI26_MFGPAGE_DEVID_SAS3216:
8636 case MPI26_MFGPAGE_DEVID_SAS3224:
8637 case MPI26_MFGPAGE_DEVID_SAS3316_1:
8638 case MPI26_MFGPAGE_DEVID_SAS3316_2:
8639 case MPI26_MFGPAGE_DEVID_SAS3316_3:
8640 case MPI26_MFGPAGE_DEVID_SAS3316_4:
8641 case MPI26_MFGPAGE_DEVID_SAS3324_1:
8642 case MPI26_MFGPAGE_DEVID_SAS3324_2:
8643 case MPI26_MFGPAGE_DEVID_SAS3324_3:
8644 case MPI26_MFGPAGE_DEVID_SAS3324_4:
8645 return MPI26_VERSION;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308646 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308647 return 0;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308648}
8649
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308650/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308651 * _scsih_probe - attach and add scsi host
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308652 * @pdev: PCI device struct
8653 * @id: pci device id
8654 *
8655 * Returns 0 success, anything else error.
8656 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308657int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308658_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308659{
8660 struct MPT3SAS_ADAPTER *ioc;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308661 struct Scsi_Host *shost = NULL;
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308662 int rv;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308663 u16 hba_mpi_version;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308664
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308665 /* Determine in which MPI version class this pci device belongs */
8666 hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8667 if (hba_mpi_version == 0)
8668 return -ENODEV;
8669
8670 /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8671 * for other generation HBA's return with -ENODEV
8672 */
8673 if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
8674 return -ENODEV;
8675
8676 /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8677 * for other generation HBA's return with -ENODEV
8678 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308679 if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
8680 || hba_mpi_version == MPI26_VERSION)))
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308681 return -ENODEV;
8682
8683 switch (hba_mpi_version) {
8684 case MPI2_VERSION:
8685 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8686 shost = scsi_host_alloc(&mpt2sas_driver_template,
8687 sizeof(struct MPT3SAS_ADAPTER));
8688 if (!shost)
8689 return -ENODEV;
8690 ioc = shost_priv(shost);
8691 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8692 ioc->hba_mpi_version_belonged = hba_mpi_version;
8693 ioc->id = mpt2_ids++;
8694 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8695 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8696 ioc->is_warpdrive = 1;
8697 ioc->hide_ir_msg = 1;
8698 } else
8699 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8700 break;
8701 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308702 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308703 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8704 shost = scsi_host_alloc(&mpt3sas_driver_template,
8705 sizeof(struct MPT3SAS_ADAPTER));
8706 if (!shost)
8707 return -ENODEV;
8708 ioc = shost_priv(shost);
8709 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8710 ioc->hba_mpi_version_belonged = hba_mpi_version;
8711 ioc->id = mpt3_ids++;
8712 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308713 if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8714 pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
8715 (ioc->hba_mpi_version_belonged == MPI26_VERSION))
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308716 ioc->msix96_vector = 1;
8717 break;
8718 default:
8719 return -ENODEV;
8720 }
8721
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308722 INIT_LIST_HEAD(&ioc->list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308723 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308724 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308725 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308726 ioc->shost = shost;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308727 ioc->pdev = pdev;
8728 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8729 ioc->tm_cb_idx = tm_cb_idx;
8730 ioc->ctl_cb_idx = ctl_cb_idx;
8731 ioc->base_cb_idx = base_cb_idx;
8732 ioc->port_enable_cb_idx = port_enable_cb_idx;
8733 ioc->transport_cb_idx = transport_cb_idx;
8734 ioc->scsih_cb_idx = scsih_cb_idx;
8735 ioc->config_cb_idx = config_cb_idx;
8736 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8737 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8738 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8739 ioc->logging_level = logging_level;
8740 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8741 /* misc semaphores and spin locks */
8742 mutex_init(&ioc->reset_in_progress_mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308743 /* initializing pci_access_mutex lock */
8744 mutex_init(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308745 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8746 spin_lock_init(&ioc->scsi_lookup_lock);
8747 spin_lock_init(&ioc->sas_device_lock);
8748 spin_lock_init(&ioc->sas_node_lock);
8749 spin_lock_init(&ioc->fw_event_lock);
8750 spin_lock_init(&ioc->raid_device_lock);
8751 spin_lock_init(&ioc->diag_trigger_lock);
8752
8753 INIT_LIST_HEAD(&ioc->sas_device_list);
8754 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8755 INIT_LIST_HEAD(&ioc->sas_expander_list);
8756 INIT_LIST_HEAD(&ioc->fw_event_list);
8757 INIT_LIST_HEAD(&ioc->raid_device_list);
8758 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8759 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05308760 INIT_LIST_HEAD(&ioc->delayed_sc_list);
8761 INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308762 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04008763 INIT_LIST_HEAD(&ioc->reply_queue_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308764
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308765 sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308766
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308767 /* init shost parameters */
8768 shost->max_cmd_len = 32;
8769 shost->max_lun = max_lun;
8770 shost->transportt = mpt3sas_transport_template;
8771 shost->unique_id = ioc->id;
8772
8773 if (max_sectors != 0xFFFF) {
8774 if (max_sectors < 64) {
8775 shost->max_sectors = 64;
8776 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8777 "for max_sectors, range is 64 to 32767. Assigning "
8778 "value of 64.\n", ioc->name, max_sectors);
8779 } else if (max_sectors > 32767) {
8780 shost->max_sectors = 32767;
8781 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8782 "for max_sectors, range is 64 to 32767. Assigning "
8783 "default value of 32767.\n", ioc->name,
8784 max_sectors);
8785 } else {
8786 shost->max_sectors = max_sectors & 0xFFFE;
8787 pr_info(MPT3SAS_FMT
8788 "The max_sectors value is set to %d\n",
8789 ioc->name, shost->max_sectors);
8790 }
8791 }
8792
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308793 /* register EEDP capabilities with SCSI layer */
8794 if (prot_mask > 0)
8795 scsi_host_set_prot(shost, prot_mask);
8796 else
8797 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8798 | SHOST_DIF_TYPE2_PROTECTION
8799 | SHOST_DIF_TYPE3_PROTECTION);
8800
8801 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8802
8803 /* event thread */
8804 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308805 "fw_event_%s%d", ioc->driver_name, ioc->id);
Sreekanth Reddybdff7852015-06-30 12:25:03 +05308806 ioc->firmware_event_thread = alloc_ordered_workqueue(
8807 ioc->firmware_event_name, WQ_MEM_RECLAIM);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308808 if (!ioc->firmware_event_thread) {
8809 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8810 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308811 rv = -ENODEV;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308812 goto out_thread_fail;
8813 }
8814
8815 ioc->is_driver_loading = 1;
8816 if ((mpt3sas_base_attach(ioc))) {
8817 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8818 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308819 rv = -ENODEV;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308820 goto out_attach_fail;
8821 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308822
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308823 if (ioc->is_warpdrive) {
8824 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8825 ioc->hide_drives = 0;
8826 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8827 ioc->hide_drives = 1;
8828 else {
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308829 if (mpt3sas_get_num_volumes(ioc))
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308830 ioc->hide_drives = 1;
8831 else
8832 ioc->hide_drives = 0;
8833 }
8834 } else
8835 ioc->hide_drives = 0;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308836
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308837 rv = scsi_add_host(shost, &pdev->dev);
8838 if (rv) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308839 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8840 ioc->name, __FILE__, __LINE__, __func__);
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308841 goto out_add_shost_fail;
8842 }
8843
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308844 scsi_scan_host(shost);
8845 return 0;
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308846out_add_shost_fail:
8847 mpt3sas_base_detach(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308848 out_attach_fail:
8849 destroy_workqueue(ioc->firmware_event_thread);
8850 out_thread_fail:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308851 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308852 list_del(&ioc->list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308853 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308854 scsi_host_put(shost);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308855 return rv;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308856}
8857
8858#ifdef CONFIG_PM
8859/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308860 * scsih_suspend - power management suspend main entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308861 * @pdev: PCI device struct
8862 * @state: PM state change to (usually PCI_D3)
8863 *
8864 * Returns 0 success, anything else error.
8865 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308866int
8867scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308868{
8869 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8870 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8871 pci_power_t device_state;
8872
8873 mpt3sas_base_stop_watchdog(ioc);
8874 flush_scheduled_work();
8875 scsi_block_requests(shost);
8876 device_state = pci_choose_state(pdev, state);
8877 pr_info(MPT3SAS_FMT
8878 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8879 ioc->name, pdev, pci_name(pdev), device_state);
8880
8881 pci_save_state(pdev);
8882 mpt3sas_base_free_resources(ioc);
8883 pci_set_power_state(pdev, device_state);
8884 return 0;
8885}
8886
8887/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308888 * scsih_resume - power management resume main entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308889 * @pdev: PCI device struct
8890 *
8891 * Returns 0 success, anything else error.
8892 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308893int
8894scsih_resume(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308895{
8896 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8897 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8898 pci_power_t device_state = pdev->current_state;
8899 int r;
8900
8901 pr_info(MPT3SAS_FMT
8902 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8903 ioc->name, pdev, pci_name(pdev), device_state);
8904
8905 pci_set_power_state(pdev, PCI_D0);
8906 pci_enable_wake(pdev, PCI_D0, 0);
8907 pci_restore_state(pdev);
8908 ioc->pdev = pdev;
8909 r = mpt3sas_base_map_resources(ioc);
8910 if (r)
8911 return r;
8912
Calvin Owens98c56ad2016-07-28 21:38:21 -07008913 mpt3sas_base_hard_reset_handler(ioc, SOFT_RESET);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308914 scsi_unblock_requests(shost);
8915 mpt3sas_base_start_watchdog(ioc);
8916 return 0;
8917}
8918#endif /* CONFIG_PM */
8919
8920/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308921 * scsih_pci_error_detected - Called when a PCI error is detected.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308922 * @pdev: PCI device struct
8923 * @state: PCI channel state
8924 *
8925 * Description: Called when a PCI error is detected.
8926 *
8927 * Return value:
8928 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8929 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308930pci_ers_result_t
8931scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308932{
8933 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8934 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8935
8936 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8937 ioc->name, state);
8938
8939 switch (state) {
8940 case pci_channel_io_normal:
8941 return PCI_ERS_RESULT_CAN_RECOVER;
8942 case pci_channel_io_frozen:
8943 /* Fatal error, prepare for slot reset */
8944 ioc->pci_error_recovery = 1;
8945 scsi_block_requests(ioc->shost);
8946 mpt3sas_base_stop_watchdog(ioc);
8947 mpt3sas_base_free_resources(ioc);
8948 return PCI_ERS_RESULT_NEED_RESET;
8949 case pci_channel_io_perm_failure:
8950 /* Permanent error, prepare for device removal */
8951 ioc->pci_error_recovery = 1;
8952 mpt3sas_base_stop_watchdog(ioc);
8953 _scsih_flush_running_cmds(ioc);
8954 return PCI_ERS_RESULT_DISCONNECT;
8955 }
8956 return PCI_ERS_RESULT_NEED_RESET;
8957}
8958
8959/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308960 * scsih_pci_slot_reset - Called when PCI slot has been reset.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308961 * @pdev: PCI device struct
8962 *
8963 * Description: This routine is called by the pci error recovery
8964 * code after the PCI slot has been reset, just before we
8965 * should resume normal operations.
8966 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308967pci_ers_result_t
8968scsih_pci_slot_reset(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308969{
8970 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8971 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8972 int rc;
8973
8974 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8975 ioc->name);
8976
8977 ioc->pci_error_recovery = 0;
8978 ioc->pdev = pdev;
8979 pci_restore_state(pdev);
8980 rc = mpt3sas_base_map_resources(ioc);
8981 if (rc)
8982 return PCI_ERS_RESULT_DISCONNECT;
8983
Calvin Owens98c56ad2016-07-28 21:38:21 -07008984 rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308985
8986 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8987 (rc == 0) ? "success" : "failed");
8988
8989 if (!rc)
8990 return PCI_ERS_RESULT_RECOVERED;
8991 else
8992 return PCI_ERS_RESULT_DISCONNECT;
8993}
8994
8995/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308996 * scsih_pci_resume() - resume normal ops after PCI reset
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308997 * @pdev: pointer to PCI device
8998 *
8999 * Called when the error recovery driver tells us that its
9000 * OK to resume normal operation. Use completion to allow
9001 * halted scsi ops to resume.
9002 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309003void
9004scsih_pci_resume(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309005{
9006 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9007 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9008
9009 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
9010
9011 pci_cleanup_aer_uncorrect_error_status(pdev);
9012 mpt3sas_base_start_watchdog(ioc);
9013 scsi_unblock_requests(ioc->shost);
9014}
9015
9016/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309017 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309018 * @pdev: pointer to PCI device
9019 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309020pci_ers_result_t
9021scsih_pci_mmio_enabled(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309022{
9023 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9024 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9025
9026 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9027 ioc->name);
9028
9029 /* TODO - dump whatever for debugging purposes */
9030
Kefeng Wang83c3d342016-07-12 17:43:25 +08009031 /* This called only if scsih_pci_error_detected returns
9032 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
9033 * works, no need to reset slot.
9034 */
9035 return PCI_ERS_RESULT_RECOVERED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309036}
9037
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309038/*
9039 * The pci device ids are defined in mpi/mpi2_cnfg.h.
9040 */
9041static const struct pci_device_id mpt3sas_pci_table[] = {
9042 /* Spitfire ~ 2004 */
9043 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9044 PCI_ANY_ID, PCI_ANY_ID },
9045 /* Falcon ~ 2008 */
9046 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9047 PCI_ANY_ID, PCI_ANY_ID },
9048 /* Liberator ~ 2108 */
9049 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9050 PCI_ANY_ID, PCI_ANY_ID },
9051 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9052 PCI_ANY_ID, PCI_ANY_ID },
9053 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9054 PCI_ANY_ID, PCI_ANY_ID },
9055 /* Meteor ~ 2116 */
9056 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9057 PCI_ANY_ID, PCI_ANY_ID },
9058 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9059 PCI_ANY_ID, PCI_ANY_ID },
9060 /* Thunderbolt ~ 2208 */
9061 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9062 PCI_ANY_ID, PCI_ANY_ID },
9063 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9064 PCI_ANY_ID, PCI_ANY_ID },
9065 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9066 PCI_ANY_ID, PCI_ANY_ID },
9067 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9068 PCI_ANY_ID, PCI_ANY_ID },
9069 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9070 PCI_ANY_ID, PCI_ANY_ID },
9071 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9072 PCI_ANY_ID, PCI_ANY_ID },
9073 /* Mustang ~ 2308 */
9074 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9075 PCI_ANY_ID, PCI_ANY_ID },
9076 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9077 PCI_ANY_ID, PCI_ANY_ID },
9078 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9079 PCI_ANY_ID, PCI_ANY_ID },
9080 /* SSS6200 */
9081 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9082 PCI_ANY_ID, PCI_ANY_ID },
9083 /* Fury ~ 3004 and 3008 */
9084 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9085 PCI_ANY_ID, PCI_ANY_ID },
9086 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9087 PCI_ANY_ID, PCI_ANY_ID },
9088 /* Invader ~ 3108 */
9089 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9090 PCI_ANY_ID, PCI_ANY_ID },
9091 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9092 PCI_ANY_ID, PCI_ANY_ID },
9093 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9094 PCI_ANY_ID, PCI_ANY_ID },
9095 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9096 PCI_ANY_ID, PCI_ANY_ID },
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05309097 /* Cutlass ~ 3216 and 3224 */
9098 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9099 PCI_ANY_ID, PCI_ANY_ID },
9100 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9101 PCI_ANY_ID, PCI_ANY_ID },
9102 /* Intruder ~ 3316 and 3324 */
9103 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9104 PCI_ANY_ID, PCI_ANY_ID },
9105 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9106 PCI_ANY_ID, PCI_ANY_ID },
9107 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9108 PCI_ANY_ID, PCI_ANY_ID },
9109 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9110 PCI_ANY_ID, PCI_ANY_ID },
9111 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9112 PCI_ANY_ID, PCI_ANY_ID },
9113 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9114 PCI_ANY_ID, PCI_ANY_ID },
9115 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9116 PCI_ANY_ID, PCI_ANY_ID },
9117 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9118 PCI_ANY_ID, PCI_ANY_ID },
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309119 {0} /* Terminating entry */
9120};
9121MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9122
9123static struct pci_error_handlers _mpt3sas_err_handler = {
9124 .error_detected = scsih_pci_error_detected,
9125 .mmio_enabled = scsih_pci_mmio_enabled,
9126 .slot_reset = scsih_pci_slot_reset,
9127 .resume = scsih_pci_resume,
9128};
9129
9130static struct pci_driver mpt3sas_driver = {
9131 .name = MPT3SAS_DRIVER_NAME,
9132 .id_table = mpt3sas_pci_table,
9133 .probe = _scsih_probe,
9134 .remove = scsih_remove,
9135 .shutdown = scsih_shutdown,
9136 .err_handler = &_mpt3sas_err_handler,
9137#ifdef CONFIG_PM
9138 .suspend = scsih_suspend,
9139 .resume = scsih_resume,
9140#endif
9141};
9142
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309143/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309144 * scsih_init - main entry point for this driver.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309145 *
9146 * Returns 0 success, anything else error.
9147 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309148int
9149scsih_init(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309150{
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309151 mpt2_ids = 0;
9152 mpt3_ids = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309153
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309154 mpt3sas_base_initialize_callback_handler();
9155
9156 /* queuecommand callback hander */
9157 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9158
9159 /* task managment callback handler */
9160 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9161
9162 /* base internal commands callback handler */
9163 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9164 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9165 mpt3sas_port_enable_done);
9166
9167 /* transport internal commands callback handler */
9168 transport_cb_idx = mpt3sas_base_register_callback_handler(
9169 mpt3sas_transport_done);
9170
9171 /* scsih internal commands callback handler */
9172 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9173
9174 /* configuration page API internal commands callback handler */
9175 config_cb_idx = mpt3sas_base_register_callback_handler(
9176 mpt3sas_config_done);
9177
9178 /* ctl module callback handler */
9179 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9180
9181 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9182 _scsih_tm_tr_complete);
9183
9184 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9185 _scsih_tm_volume_tr_complete);
9186
9187 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9188 _scsih_sas_control_complete);
9189
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05309190 return 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309191}
9192
9193/**
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05309194 * scsih_exit - exit point for this driver (when it is a module).
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309195 *
9196 * Returns 0 success, anything else error.
9197 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309198void
9199scsih_exit(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309200{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309201
9202 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9203 mpt3sas_base_release_callback_handler(tm_cb_idx);
9204 mpt3sas_base_release_callback_handler(base_cb_idx);
9205 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9206 mpt3sas_base_release_callback_handler(transport_cb_idx);
9207 mpt3sas_base_release_callback_handler(scsih_cb_idx);
9208 mpt3sas_base_release_callback_handler(config_cb_idx);
9209 mpt3sas_base_release_callback_handler(ctl_cb_idx);
9210
9211 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9212 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9213 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9214
9215/* raid transport support */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309216 if (hbas_to_enumerate != 1)
9217 raid_class_release(mpt3sas_raid_template);
9218 if (hbas_to_enumerate != 2)
9219 raid_class_release(mpt2sas_raid_template);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309220 sas_release_transport(mpt3sas_transport_template);
9221}
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05309222
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309223/**
9224 * _mpt3sas_init - main entry point for this driver.
9225 *
9226 * Returns 0 success, anything else error.
9227 */
9228static int __init
9229_mpt3sas_init(void)
9230{
9231 int error;
9232
9233 pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9234 MPT3SAS_DRIVER_VERSION);
9235
9236 mpt3sas_transport_template =
9237 sas_attach_transport(&mpt3sas_transport_functions);
9238 if (!mpt3sas_transport_template)
9239 return -ENODEV;
9240
9241 /* No need attach mpt3sas raid functions template
9242 * if hbas_to_enumarate value is one.
9243 */
9244 if (hbas_to_enumerate != 1) {
9245 mpt3sas_raid_template =
9246 raid_class_attach(&mpt3sas_raid_functions);
9247 if (!mpt3sas_raid_template) {
9248 sas_release_transport(mpt3sas_transport_template);
9249 return -ENODEV;
9250 }
9251 }
9252
9253 /* No need to attach mpt2sas raid functions template
9254 * if hbas_to_enumarate value is two
9255 */
9256 if (hbas_to_enumerate != 2) {
9257 mpt2sas_raid_template =
9258 raid_class_attach(&mpt2sas_raid_functions);
9259 if (!mpt2sas_raid_template) {
9260 sas_release_transport(mpt3sas_transport_template);
9261 return -ENODEV;
9262 }
9263 }
9264
9265 error = scsih_init();
9266 if (error) {
9267 scsih_exit();
9268 return error;
9269 }
9270
9271 mpt3sas_ctl_init(hbas_to_enumerate);
9272
9273 error = pci_register_driver(&mpt3sas_driver);
9274 if (error)
9275 scsih_exit();
9276
9277 return error;
9278}
9279
9280/**
9281 * _mpt3sas_exit - exit point for this driver (when it is a module).
9282 *
9283 */
9284static void __exit
9285_mpt3sas_exit(void)
9286{
9287 pr_info("mpt3sas version %s unloading\n",
9288 MPT3SAS_DRIVER_VERSION);
9289
9290 pci_unregister_driver(&mpt3sas_driver);
9291
9292 mpt3sas_ctl_exit(hbas_to_enumerate);
9293
9294 scsih_exit();
9295}
9296
9297module_init(_mpt3sas_init);
9298module_exit(_mpt3sas_exit);