blob: b12cadad543afda1eacfa6f987610d365dea28b5 [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)
177 * @cancel_pending_work: flag set during reset handling
178 * @ioc: per adapter object
179 * @device_handle: device handle
180 * @VF_ID: virtual function id
181 * @VP_ID: virtual port id
182 * @ignore: flag meaning this event has been marked to ignore
183 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
184 * @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;
191 u8 cancel_pending_work;
192 struct delayed_work delayed_work;
193
194 struct MPT3SAS_ADAPTER *ioc;
195 u16 device_handle;
196 u8 VF_ID;
197 u8 VP_ID;
198 u8 ignore;
199 u16 event;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +0530200 struct kref refcount;
Joe Lawrence35b62362014-06-25 17:05:34 -0400201 char event_data[0] __aligned(4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530202};
203
Sreekanth Reddy146b16c2015-11-11 17:30:31 +0530204static void fw_event_work_free(struct kref *r)
205{
206 kfree(container_of(r, struct fw_event_work, refcount));
207}
208
209static void fw_event_work_get(struct fw_event_work *fw_work)
210{
211 kref_get(&fw_work->refcount);
212}
213
214static void fw_event_work_put(struct fw_event_work *fw_work)
215{
216 kref_put(&fw_work->refcount, fw_event_work_free);
217}
218
219static struct fw_event_work *alloc_fw_event_work(int len)
220{
221 struct fw_event_work *fw_event;
222
223 fw_event = kzalloc(sizeof(*fw_event) + len, GFP_ATOMIC);
224 if (!fw_event)
225 return NULL;
226
227 kref_init(&fw_event->refcount);
228 return fw_event;
229}
230
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530231/**
232 * struct _scsi_io_transfer - scsi io transfer
233 * @handle: sas device handle (assigned by firmware)
234 * @is_raid: flag set for hidden raid components
235 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
236 * @data_length: data transfer length
237 * @data_dma: dma pointer to data
238 * @sense: sense data
239 * @lun: lun number
240 * @cdb_length: cdb length
241 * @cdb: cdb contents
242 * @timeout: timeout for this command
243 * @VF_ID: virtual function id
244 * @VP_ID: virtual port id
245 * @valid_reply: flag set for reply message
246 * @sense_length: sense length
247 * @ioc_status: ioc status
248 * @scsi_state: scsi state
249 * @scsi_status: scsi staus
250 * @log_info: log information
251 * @transfer_length: data length transfer when there is a reply message
252 *
253 * Used for sending internal scsi commands to devices within this module.
254 * Refer to _scsi_send_scsi_io().
255 */
256struct _scsi_io_transfer {
257 u16 handle;
258 u8 is_raid;
259 enum dma_data_direction dir;
260 u32 data_length;
261 dma_addr_t data_dma;
262 u8 sense[SCSI_SENSE_BUFFERSIZE];
263 u32 lun;
264 u8 cdb_length;
265 u8 cdb[32];
266 u8 timeout;
267 u8 VF_ID;
268 u8 VP_ID;
269 u8 valid_reply;
270 /* the following bits are only valid when 'valid_reply = 1' */
271 u32 sense_length;
272 u16 ioc_status;
273 u8 scsi_state;
274 u8 scsi_status;
275 u32 log_info;
276 u32 transfer_length;
277};
278
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530279/**
280 * _scsih_set_debug_level - global setting of ioc->logging_level.
281 *
282 * Note: The logging levels are defined in mpt3sas_debug.h.
283 */
284static int
285_scsih_set_debug_level(const char *val, struct kernel_param *kp)
286{
287 int ret = param_set_int(val, kp);
288 struct MPT3SAS_ADAPTER *ioc;
289
290 if (ret)
291 return ret;
292
293 pr_info("setting logging_level(0x%08x)\n", logging_level);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530294 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530295 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
296 ioc->logging_level = logging_level;
Sreekanth Reddy08c4d552015-11-11 17:30:33 +0530297 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530298 return 0;
299}
300module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
301 &logging_level, 0644);
302
303/**
304 * _scsih_srch_boot_sas_address - search based on sas_address
305 * @sas_address: sas address
306 * @boot_device: boot device object from bios page 2
307 *
308 * Returns 1 when there's a match, 0 means no match.
309 */
310static inline int
311_scsih_srch_boot_sas_address(u64 sas_address,
312 Mpi2BootDeviceSasWwid_t *boot_device)
313{
314 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
315}
316
317/**
318 * _scsih_srch_boot_device_name - search based on device name
319 * @device_name: device name specified in INDENTIFY fram
320 * @boot_device: boot device object from bios page 2
321 *
322 * Returns 1 when there's a match, 0 means no match.
323 */
324static inline int
325_scsih_srch_boot_device_name(u64 device_name,
326 Mpi2BootDeviceDeviceName_t *boot_device)
327{
328 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
329}
330
331/**
332 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
333 * @enclosure_logical_id: enclosure logical id
334 * @slot_number: slot number
335 * @boot_device: boot device object from bios page 2
336 *
337 * Returns 1 when there's a match, 0 means no match.
338 */
339static inline int
340_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
341 Mpi2BootDeviceEnclosureSlot_t *boot_device)
342{
343 return (enclosure_logical_id == le64_to_cpu(boot_device->
344 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
345 SlotNumber)) ? 1 : 0;
346}
347
348/**
349 * _scsih_is_boot_device - search for matching boot device.
350 * @sas_address: sas address
351 * @device_name: device name specified in INDENTIFY fram
352 * @enclosure_logical_id: enclosure logical id
353 * @slot_number: slot number
354 * @form: specifies boot device form
355 * @boot_device: boot device object from bios page 2
356 *
357 * Returns 1 when there's a match, 0 means no match.
358 */
359static int
360_scsih_is_boot_device(u64 sas_address, u64 device_name,
361 u64 enclosure_logical_id, u16 slot, u8 form,
362 Mpi2BiosPage2BootDevice_t *boot_device)
363{
364 int rc = 0;
365
366 switch (form) {
367 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
368 if (!sas_address)
369 break;
370 rc = _scsih_srch_boot_sas_address(
371 sas_address, &boot_device->SasWwid);
372 break;
373 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
374 if (!enclosure_logical_id)
375 break;
376 rc = _scsih_srch_boot_encl_slot(
377 enclosure_logical_id,
378 slot, &boot_device->EnclosureSlot);
379 break;
380 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
381 if (!device_name)
382 break;
383 rc = _scsih_srch_boot_device_name(
384 device_name, &boot_device->DeviceName);
385 break;
386 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
387 break;
388 }
389
390 return rc;
391}
392
393/**
394 * _scsih_get_sas_address - set the sas_address for given device handle
395 * @handle: device handle
396 * @sas_address: sas address
397 *
398 * Returns 0 success, non-zero when failure
399 */
400static int
401_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
402 u64 *sas_address)
403{
404 Mpi2SasDevicePage0_t sas_device_pg0;
405 Mpi2ConfigReply_t mpi_reply;
406 u32 ioc_status;
407
408 *sas_address = 0;
409
410 if (handle <= ioc->sas_hba.num_phys) {
411 *sas_address = ioc->sas_hba.sas_address;
412 return 0;
413 }
414
415 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
416 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
417 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
418 __FILE__, __LINE__, __func__);
419 return -ENXIO;
420 }
421
422 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
423 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
424 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
425 return 0;
426 }
427
428 /* we hit this becuase the given parent handle doesn't exist */
429 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
430 return -ENXIO;
431
432 /* else error case */
433 pr_err(MPT3SAS_FMT
434 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
435 ioc->name, handle, ioc_status,
436 __FILE__, __LINE__, __func__);
437 return -EIO;
438}
439
440/**
441 * _scsih_determine_boot_device - determine boot device.
442 * @ioc: per adapter object
443 * @device: either sas_device or raid_device object
444 * @is_raid: [flag] 1 = raid object, 0 = sas object
445 *
446 * Determines whether this device should be first reported device to
447 * to scsi-ml or sas transport, this purpose is for persistent boot device.
448 * There are primary, alternate, and current entries in bios page 2. The order
449 * priority is primary, alternate, then current. This routine saves
450 * the corresponding device object and is_raid flag in the ioc object.
451 * The saved data to be used later in _scsih_probe_boot_devices().
452 */
453static void
454_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
455 void *device, u8 is_raid)
456{
457 struct _sas_device *sas_device;
458 struct _raid_device *raid_device;
459 u64 sas_address;
460 u64 device_name;
461 u64 enclosure_logical_id;
462 u16 slot;
463
464 /* only process this function when driver loads */
465 if (!ioc->is_driver_loading)
466 return;
467
468 /* no Bios, return immediately */
469 if (!ioc->bios_pg3.BiosVersion)
470 return;
471
472 if (!is_raid) {
473 sas_device = device;
474 sas_address = sas_device->sas_address;
475 device_name = sas_device->device_name;
476 enclosure_logical_id = sas_device->enclosure_logical_id;
477 slot = sas_device->slot;
478 } else {
479 raid_device = device;
480 sas_address = raid_device->wwid;
481 device_name = 0;
482 enclosure_logical_id = 0;
483 slot = 0;
484 }
485
486 if (!ioc->req_boot_device.device) {
487 if (_scsih_is_boot_device(sas_address, device_name,
488 enclosure_logical_id, slot,
489 (ioc->bios_pg2.ReqBootDeviceForm &
490 MPI2_BIOSPAGE2_FORM_MASK),
491 &ioc->bios_pg2.RequestedBootDevice)) {
492 dinitprintk(ioc, pr_info(MPT3SAS_FMT
493 "%s: req_boot_device(0x%016llx)\n",
494 ioc->name, __func__,
495 (unsigned long long)sas_address));
496 ioc->req_boot_device.device = device;
497 ioc->req_boot_device.is_raid = is_raid;
498 }
499 }
500
501 if (!ioc->req_alt_boot_device.device) {
502 if (_scsih_is_boot_device(sas_address, device_name,
503 enclosure_logical_id, slot,
504 (ioc->bios_pg2.ReqAltBootDeviceForm &
505 MPI2_BIOSPAGE2_FORM_MASK),
506 &ioc->bios_pg2.RequestedAltBootDevice)) {
507 dinitprintk(ioc, pr_info(MPT3SAS_FMT
508 "%s: req_alt_boot_device(0x%016llx)\n",
509 ioc->name, __func__,
510 (unsigned long long)sas_address));
511 ioc->req_alt_boot_device.device = device;
512 ioc->req_alt_boot_device.is_raid = is_raid;
513 }
514 }
515
516 if (!ioc->current_boot_device.device) {
517 if (_scsih_is_boot_device(sas_address, device_name,
518 enclosure_logical_id, slot,
519 (ioc->bios_pg2.CurrentBootDeviceForm &
520 MPI2_BIOSPAGE2_FORM_MASK),
521 &ioc->bios_pg2.CurrentBootDevice)) {
522 dinitprintk(ioc, pr_info(MPT3SAS_FMT
523 "%s: current_boot_device(0x%016llx)\n",
524 ioc->name, __func__,
525 (unsigned long long)sas_address));
526 ioc->current_boot_device.device = device;
527 ioc->current_boot_device.is_raid = is_raid;
528 }
529 }
530}
531
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530532static struct _sas_device *
533__mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
534 struct MPT3SAS_TARGET *tgt_priv)
535{
536 struct _sas_device *ret;
537
538 assert_spin_locked(&ioc->sas_device_lock);
539
540 ret = tgt_priv->sdev;
541 if (ret)
542 sas_device_get(ret);
543
544 return ret;
545}
546
547static struct _sas_device *
548mpt3sas_get_sdev_from_target(struct MPT3SAS_ADAPTER *ioc,
549 struct MPT3SAS_TARGET *tgt_priv)
550{
551 struct _sas_device *ret;
552 unsigned long flags;
553
554 spin_lock_irqsave(&ioc->sas_device_lock, flags);
555 ret = __mpt3sas_get_sdev_from_target(ioc, tgt_priv);
556 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
557
558 return ret;
559}
560
561
562struct _sas_device *
563__mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
564 u64 sas_address)
565{
566 struct _sas_device *sas_device;
567
568 assert_spin_locked(&ioc->sas_device_lock);
569
570 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
571 if (sas_device->sas_address == sas_address)
572 goto found_device;
573
574 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
575 if (sas_device->sas_address == sas_address)
576 goto found_device;
577
578 return NULL;
579
580found_device:
581 sas_device_get(sas_device);
582 return sas_device;
583}
584
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530585/**
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530586 * mpt3sas_get_sdev_by_addr - sas device search
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530587 * @ioc: per adapter object
588 * @sas_address: sas address
589 * Context: Calling function should acquire ioc->sas_device_lock
590 *
591 * This searches for sas_device based on sas_address, then return sas_device
592 * object.
593 */
594struct _sas_device *
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530595mpt3sas_get_sdev_by_addr(struct MPT3SAS_ADAPTER *ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530596 u64 sas_address)
597{
598 struct _sas_device *sas_device;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530599 unsigned long flags;
600
601 spin_lock_irqsave(&ioc->sas_device_lock, flags);
602 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
603 sas_address);
604 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
605
606 return sas_device;
607}
608
609static struct _sas_device *
610__mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
611{
612 struct _sas_device *sas_device;
613
614 assert_spin_locked(&ioc->sas_device_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530615
616 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530617 if (sas_device->handle == handle)
618 goto found_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530619
620 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530621 if (sas_device->handle == handle)
622 goto found_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530623
624 return NULL;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530625
626found_device:
627 sas_device_get(sas_device);
628 return sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530629}
630
631/**
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530632 * mpt3sas_get_sdev_by_handle - sas device search
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530633 * @ioc: per adapter object
634 * @handle: sas device handle (assigned by firmware)
635 * Context: Calling function should acquire ioc->sas_device_lock
636 *
637 * This searches for sas_device based on sas_address, then return sas_device
638 * object.
639 */
640static struct _sas_device *
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530641mpt3sas_get_sdev_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530642{
643 struct _sas_device *sas_device;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530644 unsigned long flags;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530645
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530646 spin_lock_irqsave(&ioc->sas_device_lock, flags);
647 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
648 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530649
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530650 return sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530651}
652
653/**
654 * _scsih_sas_device_remove - remove sas_device from list.
655 * @ioc: per adapter object
656 * @sas_device: the sas_device object
657 * Context: This function will acquire ioc->sas_device_lock.
658 *
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530659 * If sas_device is on the list, remove it and decrement its reference count.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530660 */
661static void
662_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
663 struct _sas_device *sas_device)
664{
665 unsigned long flags;
666
667 if (!sas_device)
668 return;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530669 pr_info(MPT3SAS_FMT
670 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
671 ioc->name, sas_device->handle,
672 (unsigned long long) sas_device->sas_address);
673
674 if (sas_device->enclosure_handle != 0)
675 pr_info(MPT3SAS_FMT
676 "removing enclosure logical id(0x%016llx), slot(%d)\n",
677 ioc->name, (unsigned long long)
678 sas_device->enclosure_logical_id, sas_device->slot);
679
680 if (sas_device->connector_name[0] != '\0')
681 pr_info(MPT3SAS_FMT
682 "removing enclosure level(0x%04x), connector name( %s)\n",
683 ioc->name, sas_device->enclosure_level,
684 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530685
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530686 /*
687 * The lock serializes access to the list, but we still need to verify
688 * that nobody removed the entry while we were waiting on the lock.
689 */
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530690 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530691 if (!list_empty(&sas_device->list)) {
692 list_del_init(&sas_device->list);
693 sas_device_put(sas_device);
694 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530695 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
696}
697
698/**
699 * _scsih_device_remove_by_handle - removing device object by handle
700 * @ioc: per adapter object
701 * @handle: device handle
702 *
703 * Return nothing.
704 */
705static void
706_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
707{
708 struct _sas_device *sas_device;
709 unsigned long flags;
710
711 if (ioc->shost_recovery)
712 return;
713
714 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530715 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
716 if (sas_device) {
717 list_del_init(&sas_device->list);
718 sas_device_put(sas_device);
719 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530720 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530721 if (sas_device) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530722 _scsih_remove_device(ioc, sas_device);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530723 sas_device_put(sas_device);
724 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530725}
726
727/**
728 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
729 * @ioc: per adapter object
730 * @sas_address: device sas_address
731 *
732 * Return nothing.
733 */
734void
735mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
736 u64 sas_address)
737{
738 struct _sas_device *sas_device;
739 unsigned long flags;
740
741 if (ioc->shost_recovery)
742 return;
743
744 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530745 sas_device = __mpt3sas_get_sdev_by_addr(ioc, sas_address);
746 if (sas_device) {
747 list_del_init(&sas_device->list);
748 sas_device_put(sas_device);
749 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530750 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530751 if (sas_device) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530752 _scsih_remove_device(ioc, sas_device);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530753 sas_device_put(sas_device);
754 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530755}
756
757/**
758 * _scsih_sas_device_add - insert sas_device to the list.
759 * @ioc: per adapter object
760 * @sas_device: the sas_device object
761 * Context: This function will acquire ioc->sas_device_lock.
762 *
763 * Adding new object to the ioc->sas_device_list.
764 */
765static void
766_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
767 struct _sas_device *sas_device)
768{
769 unsigned long flags;
770
771 dewtprintk(ioc, pr_info(MPT3SAS_FMT
772 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
773 ioc->name, __func__, sas_device->handle,
774 (unsigned long long)sas_device->sas_address));
775
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530776 if (sas_device->enclosure_handle != 0)
777 dewtprintk(ioc, pr_info(MPT3SAS_FMT
778 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
779 ioc->name, __func__, (unsigned long long)
780 sas_device->enclosure_logical_id, sas_device->slot));
781
782 if (sas_device->connector_name[0] != '\0')
783 dewtprintk(ioc, pr_info(MPT3SAS_FMT
784 "%s: enclosure level(0x%04x), connector name( %s)\n",
785 ioc->name, __func__,
786 sas_device->enclosure_level, sas_device->connector_name));
787
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530788 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530789 sas_device_get(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530790 list_add_tail(&sas_device->list, &ioc->sas_device_list);
791 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
792
793 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
794 sas_device->sas_address_parent)) {
795 _scsih_sas_device_remove(ioc, sas_device);
796 } else if (!sas_device->starget) {
797 /*
798 * When asyn scanning is enabled, its not possible to remove
799 * devices while scanning is turned on due to an oops in
800 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
801 */
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +0530802 if (!ioc->is_driver_loading) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530803 mpt3sas_transport_port_remove(ioc,
804 sas_device->sas_address,
805 sas_device->sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +0530806 _scsih_sas_device_remove(ioc, sas_device);
807 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530808 }
809}
810
811/**
812 * _scsih_sas_device_init_add - insert sas_device to the list.
813 * @ioc: per adapter object
814 * @sas_device: the sas_device object
815 * Context: This function will acquire ioc->sas_device_lock.
816 *
817 * Adding new object at driver load time to the ioc->sas_device_init_list.
818 */
819static void
820_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
821 struct _sas_device *sas_device)
822{
823 unsigned long flags;
824
825 dewtprintk(ioc, pr_info(MPT3SAS_FMT
826 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
827 __func__, sas_device->handle,
828 (unsigned long long)sas_device->sas_address));
829
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530830 if (sas_device->enclosure_handle != 0)
831 dewtprintk(ioc, pr_info(MPT3SAS_FMT
832 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
833 ioc->name, __func__, (unsigned long long)
834 sas_device->enclosure_logical_id, sas_device->slot));
835
836 if (sas_device->connector_name[0] != '\0')
837 dewtprintk(ioc, pr_info(MPT3SAS_FMT
838 "%s: enclosure level(0x%04x), connector name( %s)\n",
839 ioc->name, __func__, sas_device->enclosure_level,
840 sas_device->connector_name));
841
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530842 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +0530843 sas_device_get(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530844 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
845 _scsih_determine_boot_device(ioc, sas_device, 0);
846 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
847}
848
849/**
850 * _scsih_raid_device_find_by_id - raid device search
851 * @ioc: per adapter object
852 * @id: sas device target id
853 * @channel: sas device channel
854 * Context: Calling function should acquire ioc->raid_device_lock
855 *
856 * This searches for raid_device based on target id, then return raid_device
857 * object.
858 */
859static struct _raid_device *
860_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
861{
862 struct _raid_device *raid_device, *r;
863
864 r = NULL;
865 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
866 if (raid_device->id == id && raid_device->channel == channel) {
867 r = raid_device;
868 goto out;
869 }
870 }
871
872 out:
873 return r;
874}
875
876/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530877 * mpt3sas_raid_device_find_by_handle - raid device search
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530878 * @ioc: per adapter object
879 * @handle: sas device handle (assigned by firmware)
880 * Context: Calling function should acquire ioc->raid_device_lock
881 *
882 * This searches for raid_device based on handle, then return raid_device
883 * object.
884 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +0530885struct _raid_device *
886mpt3sas_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530887{
888 struct _raid_device *raid_device, *r;
889
890 r = NULL;
891 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
892 if (raid_device->handle != handle)
893 continue;
894 r = raid_device;
895 goto out;
896 }
897
898 out:
899 return r;
900}
901
902/**
903 * _scsih_raid_device_find_by_wwid - raid device search
904 * @ioc: per adapter object
905 * @handle: sas device handle (assigned by firmware)
906 * Context: Calling function should acquire ioc->raid_device_lock
907 *
908 * This searches for raid_device based on wwid, then return raid_device
909 * object.
910 */
911static struct _raid_device *
912_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
913{
914 struct _raid_device *raid_device, *r;
915
916 r = NULL;
917 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
918 if (raid_device->wwid != wwid)
919 continue;
920 r = raid_device;
921 goto out;
922 }
923
924 out:
925 return r;
926}
927
928/**
929 * _scsih_raid_device_add - add raid_device object
930 * @ioc: per adapter object
931 * @raid_device: raid_device object
932 *
933 * This is added to the raid_device_list link list.
934 */
935static void
936_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
937 struct _raid_device *raid_device)
938{
939 unsigned long flags;
940
941 dewtprintk(ioc, pr_info(MPT3SAS_FMT
942 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
943 raid_device->handle, (unsigned long long)raid_device->wwid));
944
945 spin_lock_irqsave(&ioc->raid_device_lock, flags);
946 list_add_tail(&raid_device->list, &ioc->raid_device_list);
947 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
948}
949
950/**
951 * _scsih_raid_device_remove - delete raid_device object
952 * @ioc: per adapter object
953 * @raid_device: raid_device object
954 *
955 */
956static void
957_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
958 struct _raid_device *raid_device)
959{
960 unsigned long flags;
961
962 spin_lock_irqsave(&ioc->raid_device_lock, flags);
963 list_del(&raid_device->list);
964 kfree(raid_device);
965 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
966}
967
968/**
969 * mpt3sas_scsih_expander_find_by_handle - expander device search
970 * @ioc: per adapter object
971 * @handle: expander handle (assigned by firmware)
972 * Context: Calling function should acquire ioc->sas_device_lock
973 *
974 * This searches for expander device based on handle, then returns the
975 * sas_node object.
976 */
977struct _sas_node *
978mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
979{
980 struct _sas_node *sas_expander, *r;
981
982 r = NULL;
983 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
984 if (sas_expander->handle != handle)
985 continue;
986 r = sas_expander;
987 goto out;
988 }
989 out:
990 return r;
991}
992
993/**
994 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
995 * @ioc: per adapter object
996 * @sas_address: sas address
997 * Context: Calling function should acquire ioc->sas_node_lock.
998 *
999 * This searches for expander device based on sas_address, then returns the
1000 * sas_node object.
1001 */
1002struct _sas_node *
1003mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
1004 u64 sas_address)
1005{
1006 struct _sas_node *sas_expander, *r;
1007
1008 r = NULL;
1009 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
1010 if (sas_expander->sas_address != sas_address)
1011 continue;
1012 r = sas_expander;
1013 goto out;
1014 }
1015 out:
1016 return r;
1017}
1018
1019/**
1020 * _scsih_expander_node_add - insert expander device to the list.
1021 * @ioc: per adapter object
1022 * @sas_expander: the sas_device object
1023 * Context: This function will acquire ioc->sas_node_lock.
1024 *
1025 * Adding new object to the ioc->sas_expander_list.
1026 *
1027 * Return nothing.
1028 */
1029static void
1030_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
1031 struct _sas_node *sas_expander)
1032{
1033 unsigned long flags;
1034
1035 spin_lock_irqsave(&ioc->sas_node_lock, flags);
1036 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
1037 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
1038}
1039
1040/**
1041 * _scsih_is_end_device - determines if device is an end device
1042 * @device_info: bitfield providing information about the device.
1043 * Context: none
1044 *
1045 * Returns 1 if end device.
1046 */
1047static int
1048_scsih_is_end_device(u32 device_info)
1049{
1050 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
1051 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
1052 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
1053 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
1054 return 1;
1055 else
1056 return 0;
1057}
1058
1059/**
1060 * _scsih_scsi_lookup_get - returns scmd entry
1061 * @ioc: per adapter object
1062 * @smid: system request message index
1063 *
1064 * Returns the smid stored scmd pointer.
1065 */
1066static struct scsi_cmnd *
1067_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1068{
1069 return ioc->scsi_lookup[smid - 1].scmd;
1070}
1071
1072/**
1073 * _scsih_scsi_lookup_get_clear - returns scmd entry
1074 * @ioc: per adapter object
1075 * @smid: system request message index
1076 *
1077 * Returns the smid stored scmd pointer.
1078 * Then will derefrence the stored scmd pointer.
1079 */
1080static inline struct scsi_cmnd *
1081_scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
1082{
1083 unsigned long flags;
1084 struct scsi_cmnd *scmd;
1085
1086 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1087 scmd = ioc->scsi_lookup[smid - 1].scmd;
1088 ioc->scsi_lookup[smid - 1].scmd = NULL;
1089 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1090
1091 return scmd;
1092}
1093
1094/**
1095 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
1096 * @ioc: per adapter object
1097 * @smid: system request message index
1098 * @scmd: pointer to scsi command object
1099 * Context: This function will acquire ioc->scsi_lookup_lock.
1100 *
1101 * This will search for a scmd pointer in the scsi_lookup array,
1102 * returning the revelent smid. A returned value of zero means invalid.
1103 */
1104static u16
1105_scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
1106 *scmd)
1107{
1108 u16 smid;
1109 unsigned long flags;
1110 int i;
1111
1112 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1113 smid = 0;
1114 for (i = 0; i < ioc->scsiio_depth; i++) {
1115 if (ioc->scsi_lookup[i].scmd == scmd) {
1116 smid = ioc->scsi_lookup[i].smid;
1117 goto out;
1118 }
1119 }
1120 out:
1121 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1122 return smid;
1123}
1124
1125/**
1126 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
1127 * @ioc: per adapter object
1128 * @id: target id
1129 * @channel: channel
1130 * Context: This function will acquire ioc->scsi_lookup_lock.
1131 *
1132 * This will search for a matching channel:id in the scsi_lookup array,
1133 * returning 1 if found.
1134 */
1135static u8
1136_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1137 int channel)
1138{
1139 u8 found;
1140 unsigned long flags;
1141 int i;
1142
1143 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1144 found = 0;
1145 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1146 if (ioc->scsi_lookup[i].scmd &&
1147 (ioc->scsi_lookup[i].scmd->device->id == id &&
1148 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1149 found = 1;
1150 goto out;
1151 }
1152 }
1153 out:
1154 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1155 return found;
1156}
1157
1158/**
1159 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1160 * @ioc: per adapter object
1161 * @id: target id
1162 * @lun: lun number
1163 * @channel: channel
1164 * Context: This function will acquire ioc->scsi_lookup_lock.
1165 *
1166 * This will search for a matching channel:id:lun in the scsi_lookup array,
1167 * returning 1 if found.
1168 */
1169static u8
1170_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1171 unsigned int lun, int channel)
1172{
1173 u8 found;
1174 unsigned long flags;
1175 int i;
1176
1177 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1178 found = 0;
1179 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1180 if (ioc->scsi_lookup[i].scmd &&
1181 (ioc->scsi_lookup[i].scmd->device->id == id &&
1182 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1183 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1184 found = 1;
1185 goto out;
1186 }
1187 }
1188 out:
1189 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1190 return found;
1191}
1192
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001193/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301194 * scsih_change_queue_depth - setting device queue depth
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001195 * @sdev: scsi device struct
1196 * @qdepth: requested queue depth
1197 *
1198 * Returns queue depth.
1199 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301200int
1201scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301202{
1203 struct Scsi_Host *shost = sdev->host;
1204 int max_depth;
1205 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1206 struct MPT3SAS_DEVICE *sas_device_priv_data;
1207 struct MPT3SAS_TARGET *sas_target_priv_data;
1208 struct _sas_device *sas_device;
1209 unsigned long flags;
1210
1211 max_depth = shost->can_queue;
1212
1213 /* limit max device queue for SATA to 32 */
1214 sas_device_priv_data = sdev->hostdata;
1215 if (!sas_device_priv_data)
1216 goto not_sata;
1217 sas_target_priv_data = sas_device_priv_data->sas_target;
1218 if (!sas_target_priv_data)
1219 goto not_sata;
1220 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1221 goto not_sata;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301222
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301223 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301224 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
1225 if (sas_device) {
1226 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1227 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1228
1229 sas_device_put(sas_device);
1230 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301231 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1232
1233 not_sata:
1234
1235 if (!sdev->tagged_supported)
1236 max_depth = 1;
1237 if (qdepth > max_depth)
1238 qdepth = max_depth;
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001239 return scsi_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301240}
1241
1242/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301243 * scsih_target_alloc - target add routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301244 * @starget: scsi target struct
1245 *
1246 * Returns 0 if ok. Any other return is assumed to be an error and
1247 * the device is ignored.
1248 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301249int
1250scsih_target_alloc(struct scsi_target *starget)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301251{
1252 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1253 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1254 struct MPT3SAS_TARGET *sas_target_priv_data;
1255 struct _sas_device *sas_device;
1256 struct _raid_device *raid_device;
1257 unsigned long flags;
1258 struct sas_rphy *rphy;
1259
Joe Lawrence62c4da42014-06-25 17:04:22 -04001260 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1261 GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301262 if (!sas_target_priv_data)
1263 return -ENOMEM;
1264
1265 starget->hostdata = sas_target_priv_data;
1266 sas_target_priv_data->starget = starget;
1267 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1268
1269 /* RAID volumes */
1270 if (starget->channel == RAID_CHANNEL) {
1271 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1272 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1273 starget->channel);
1274 if (raid_device) {
1275 sas_target_priv_data->handle = raid_device->handle;
1276 sas_target_priv_data->sas_address = raid_device->wwid;
1277 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301278 sas_target_priv_data->raid_device = raid_device;
1279 if (ioc->is_warpdrive)
1280 raid_device->starget = starget;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301281 }
1282 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1283 return 0;
1284 }
1285
1286 /* sas/sata devices */
1287 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1288 rphy = dev_to_rphy(starget->dev.parent);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301289 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301290 rphy->identify.sas_address);
1291
1292 if (sas_device) {
1293 sas_target_priv_data->handle = sas_device->handle;
1294 sas_target_priv_data->sas_address = sas_device->sas_address;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301295 sas_target_priv_data->sdev = sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301296 sas_device->starget = starget;
1297 sas_device->id = starget->id;
1298 sas_device->channel = starget->channel;
1299 if (test_bit(sas_device->handle, ioc->pd_handles))
1300 sas_target_priv_data->flags |=
1301 MPT_TARGET_FLAGS_RAID_COMPONENT;
1302 if (sas_device->fast_path)
1303 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1304 }
1305 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1306
1307 return 0;
1308}
1309
1310/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301311 * scsih_target_destroy - target destroy routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301312 * @starget: scsi target struct
1313 *
1314 * Returns nothing.
1315 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301316void
1317scsih_target_destroy(struct scsi_target *starget)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301318{
1319 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1320 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1321 struct MPT3SAS_TARGET *sas_target_priv_data;
1322 struct _sas_device *sas_device;
1323 struct _raid_device *raid_device;
1324 unsigned long flags;
1325 struct sas_rphy *rphy;
1326
1327 sas_target_priv_data = starget->hostdata;
1328 if (!sas_target_priv_data)
1329 return;
1330
1331 if (starget->channel == RAID_CHANNEL) {
1332 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1333 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1334 starget->channel);
1335 if (raid_device) {
1336 raid_device->starget = NULL;
1337 raid_device->sdev = NULL;
1338 }
1339 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1340 goto out;
1341 }
1342
1343 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1344 rphy = dev_to_rphy(starget->dev.parent);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301345 sas_device = __mpt3sas_get_sdev_from_target(ioc, sas_target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301346 if (sas_device && (sas_device->starget == starget) &&
1347 (sas_device->id == starget->id) &&
1348 (sas_device->channel == starget->channel))
1349 sas_device->starget = NULL;
1350
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301351 if (sas_device) {
1352 /*
1353 * Corresponding get() is in _scsih_target_alloc()
1354 */
1355 sas_target_priv_data->sdev = NULL;
1356 sas_device_put(sas_device);
1357
1358 sas_device_put(sas_device);
1359 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301360 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1361
1362 out:
1363 kfree(sas_target_priv_data);
1364 starget->hostdata = NULL;
1365}
1366
1367/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301368 * scsih_slave_alloc - device add routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301369 * @sdev: scsi device struct
1370 *
1371 * Returns 0 if ok. Any other return is assumed to be an error and
1372 * the device is ignored.
1373 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301374int
1375scsih_slave_alloc(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301376{
1377 struct Scsi_Host *shost;
1378 struct MPT3SAS_ADAPTER *ioc;
1379 struct MPT3SAS_TARGET *sas_target_priv_data;
1380 struct MPT3SAS_DEVICE *sas_device_priv_data;
1381 struct scsi_target *starget;
1382 struct _raid_device *raid_device;
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301383 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301384 unsigned long flags;
1385
Joe Lawrence62c4da42014-06-25 17:04:22 -04001386 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1387 GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301388 if (!sas_device_priv_data)
1389 return -ENOMEM;
1390
1391 sas_device_priv_data->lun = sdev->lun;
1392 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1393
1394 starget = scsi_target(sdev);
1395 sas_target_priv_data = starget->hostdata;
1396 sas_target_priv_data->num_luns++;
1397 sas_device_priv_data->sas_target = sas_target_priv_data;
1398 sdev->hostdata = sas_device_priv_data;
1399 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1400 sdev->no_uld_attach = 1;
1401
1402 shost = dev_to_shost(&starget->dev);
1403 ioc = shost_priv(shost);
1404 if (starget->channel == RAID_CHANNEL) {
1405 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1406 raid_device = _scsih_raid_device_find_by_id(ioc,
1407 starget->id, starget->channel);
1408 if (raid_device)
1409 raid_device->sdev = sdev; /* raid is single lun */
1410 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1411 }
1412
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301413 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1414 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301415 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301416 sas_target_priv_data->sas_address);
1417 if (sas_device && (sas_device->starget == NULL)) {
1418 sdev_printk(KERN_INFO, sdev,
1419 "%s : sas_device->starget set to starget @ %d\n",
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301420 __func__, __LINE__);
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301421 sas_device->starget = starget;
1422 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301423
1424 if (sas_device)
1425 sas_device_put(sas_device);
1426
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301427 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1428 }
1429
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301430 return 0;
1431}
1432
1433/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301434 * scsih_slave_destroy - device destroy routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301435 * @sdev: scsi device struct
1436 *
1437 * Returns nothing.
1438 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301439void
1440scsih_slave_destroy(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301441{
1442 struct MPT3SAS_TARGET *sas_target_priv_data;
1443 struct scsi_target *starget;
1444 struct Scsi_Host *shost;
1445 struct MPT3SAS_ADAPTER *ioc;
1446 struct _sas_device *sas_device;
1447 unsigned long flags;
1448
1449 if (!sdev->hostdata)
1450 return;
1451
1452 starget = scsi_target(sdev);
1453 sas_target_priv_data = starget->hostdata;
1454 sas_target_priv_data->num_luns--;
1455
1456 shost = dev_to_shost(&starget->dev);
1457 ioc = shost_priv(shost);
1458
1459 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1460 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301461 sas_device = __mpt3sas_get_sdev_from_target(ioc,
1462 sas_target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301463 if (sas_device && !sas_target_priv_data->num_luns)
1464 sas_device->starget = NULL;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301465
1466 if (sas_device)
1467 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301468 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1469 }
1470
1471 kfree(sdev->hostdata);
1472 sdev->hostdata = NULL;
1473}
1474
1475/**
1476 * _scsih_display_sata_capabilities - sata capabilities
1477 * @ioc: per adapter object
1478 * @handle: device handle
1479 * @sdev: scsi device struct
1480 */
1481static void
1482_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1483 u16 handle, struct scsi_device *sdev)
1484{
1485 Mpi2ConfigReply_t mpi_reply;
1486 Mpi2SasDevicePage0_t sas_device_pg0;
1487 u32 ioc_status;
1488 u16 flags;
1489 u32 device_info;
1490
1491 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1492 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1493 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1494 ioc->name, __FILE__, __LINE__, __func__);
1495 return;
1496 }
1497
1498 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1499 MPI2_IOCSTATUS_MASK;
1500 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1501 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1502 ioc->name, __FILE__, __LINE__, __func__);
1503 return;
1504 }
1505
1506 flags = le16_to_cpu(sas_device_pg0.Flags);
1507 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1508
1509 sdev_printk(KERN_INFO, sdev,
1510 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1511 "sw_preserve(%s)\n",
1512 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1513 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1514 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1515 "n",
1516 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1517 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1518 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1519}
1520
1521/*
1522 * raid transport support -
1523 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1524 * unloading the driver followed by a load - I beleive that the subroutine
1525 * raid_class_release() is not cleaning up properly.
1526 */
1527
1528/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301529 * scsih_is_raid - return boolean indicating device is raid volume
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301530 * @dev the device struct object
1531 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301532int
1533scsih_is_raid(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301534{
1535 struct scsi_device *sdev = to_scsi_device(dev);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301536 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301537
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301538 if (ioc->is_warpdrive)
1539 return 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301540 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1541}
1542
1543/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301544 * scsih_get_resync - get raid volume resync percent complete
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301545 * @dev the device struct object
1546 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301547void
1548scsih_get_resync(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301549{
1550 struct scsi_device *sdev = to_scsi_device(dev);
1551 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1552 static struct _raid_device *raid_device;
1553 unsigned long flags;
1554 Mpi2RaidVolPage0_t vol_pg0;
1555 Mpi2ConfigReply_t mpi_reply;
1556 u32 volume_status_flags;
1557 u8 percent_complete;
1558 u16 handle;
1559
1560 percent_complete = 0;
1561 handle = 0;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301562 if (ioc->is_warpdrive)
1563 goto out;
1564
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301565 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1566 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1567 sdev->channel);
1568 if (raid_device) {
1569 handle = raid_device->handle;
1570 percent_complete = raid_device->percent_complete;
1571 }
1572 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1573
1574 if (!handle)
1575 goto out;
1576
1577 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1578 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1579 sizeof(Mpi2RaidVolPage0_t))) {
1580 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1581 ioc->name, __FILE__, __LINE__, __func__);
1582 percent_complete = 0;
1583 goto out;
1584 }
1585
1586 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1587 if (!(volume_status_flags &
1588 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1589 percent_complete = 0;
1590
1591 out:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301592
1593 switch (ioc->hba_mpi_version_belonged) {
1594 case MPI2_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301595 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301596 break;
1597 case MPI25_VERSION:
1598 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301599 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301600 break;
1601 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301602}
1603
1604/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301605 * scsih_get_state - get raid volume level
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301606 * @dev the device struct object
1607 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301608void
1609scsih_get_state(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301610{
1611 struct scsi_device *sdev = to_scsi_device(dev);
1612 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1613 static struct _raid_device *raid_device;
1614 unsigned long flags;
1615 Mpi2RaidVolPage0_t vol_pg0;
1616 Mpi2ConfigReply_t mpi_reply;
1617 u32 volstate;
1618 enum raid_state state = RAID_STATE_UNKNOWN;
1619 u16 handle = 0;
1620
1621 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1622 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1623 sdev->channel);
1624 if (raid_device)
1625 handle = raid_device->handle;
1626 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1627
1628 if (!raid_device)
1629 goto out;
1630
1631 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1632 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1633 sizeof(Mpi2RaidVolPage0_t))) {
1634 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1635 ioc->name, __FILE__, __LINE__, __func__);
1636 goto out;
1637 }
1638
1639 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1640 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1641 state = RAID_STATE_RESYNCING;
1642 goto out;
1643 }
1644
1645 switch (vol_pg0.VolumeState) {
1646 case MPI2_RAID_VOL_STATE_OPTIMAL:
1647 case MPI2_RAID_VOL_STATE_ONLINE:
1648 state = RAID_STATE_ACTIVE;
1649 break;
1650 case MPI2_RAID_VOL_STATE_DEGRADED:
1651 state = RAID_STATE_DEGRADED;
1652 break;
1653 case MPI2_RAID_VOL_STATE_FAILED:
1654 case MPI2_RAID_VOL_STATE_MISSING:
1655 state = RAID_STATE_OFFLINE;
1656 break;
1657 }
1658 out:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301659 switch (ioc->hba_mpi_version_belonged) {
1660 case MPI2_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301661 raid_set_state(mpt2sas_raid_template, dev, state);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301662 break;
1663 case MPI25_VERSION:
1664 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301665 raid_set_state(mpt3sas_raid_template, dev, state);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301666 break;
1667 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301668}
1669
1670/**
1671 * _scsih_set_level - set raid level
1672 * @sdev: scsi device struct
1673 * @volume_type: volume type
1674 */
1675static void
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301676_scsih_set_level(struct MPT3SAS_ADAPTER *ioc,
1677 struct scsi_device *sdev, u8 volume_type)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301678{
1679 enum raid_level level = RAID_LEVEL_UNKNOWN;
1680
1681 switch (volume_type) {
1682 case MPI2_RAID_VOL_TYPE_RAID0:
1683 level = RAID_LEVEL_0;
1684 break;
1685 case MPI2_RAID_VOL_TYPE_RAID10:
1686 level = RAID_LEVEL_10;
1687 break;
1688 case MPI2_RAID_VOL_TYPE_RAID1E:
1689 level = RAID_LEVEL_1E;
1690 break;
1691 case MPI2_RAID_VOL_TYPE_RAID1:
1692 level = RAID_LEVEL_1;
1693 break;
1694 }
1695
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301696 switch (ioc->hba_mpi_version_belonged) {
1697 case MPI2_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301698 raid_set_level(mpt2sas_raid_template,
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301699 &sdev->sdev_gendev, level);
1700 break;
1701 case MPI25_VERSION:
1702 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301703 raid_set_level(mpt3sas_raid_template,
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05301704 &sdev->sdev_gendev, level);
1705 break;
1706 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301707}
1708
1709
1710/**
1711 * _scsih_get_volume_capabilities - volume capabilities
1712 * @ioc: per adapter object
1713 * @sas_device: the raid_device object
1714 *
1715 * Returns 0 for success, else 1
1716 */
1717static int
1718_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1719 struct _raid_device *raid_device)
1720{
1721 Mpi2RaidVolPage0_t *vol_pg0;
1722 Mpi2RaidPhysDiskPage0_t pd_pg0;
1723 Mpi2SasDevicePage0_t sas_device_pg0;
1724 Mpi2ConfigReply_t mpi_reply;
1725 u16 sz;
1726 u8 num_pds;
1727
1728 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1729 &num_pds)) || !num_pds) {
1730 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1731 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1732 __func__));
1733 return 1;
1734 }
1735
1736 raid_device->num_pds = num_pds;
1737 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1738 sizeof(Mpi2RaidVol0PhysDisk_t));
1739 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1740 if (!vol_pg0) {
1741 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1742 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1743 __func__));
1744 return 1;
1745 }
1746
1747 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1748 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1749 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1750 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1751 __func__));
1752 kfree(vol_pg0);
1753 return 1;
1754 }
1755
1756 raid_device->volume_type = vol_pg0->VolumeType;
1757
1758 /* figure out what the underlying devices are by
1759 * obtaining the device_info bits for the 1st device
1760 */
1761 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1762 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1763 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1764 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1765 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1766 le16_to_cpu(pd_pg0.DevHandle)))) {
1767 raid_device->device_info =
1768 le32_to_cpu(sas_device_pg0.DeviceInfo);
1769 }
1770 }
1771
1772 kfree(vol_pg0);
1773 return 0;
1774}
1775
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301776/**
1777 * _scsih_enable_tlr - setting TLR flags
1778 * @ioc: per adapter object
1779 * @sdev: scsi device struct
1780 *
1781 * Enabling Transaction Layer Retries for tape devices when
1782 * vpd page 0x90 is present
1783 *
1784 */
1785static void
1786_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1787{
1788
1789 /* only for TAPE */
1790 if (sdev->type != TYPE_TAPE)
1791 return;
1792
1793 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1794 return;
1795
1796 sas_enable_tlr(sdev);
1797 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1798 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1799 return;
1800
1801}
1802
1803/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301804 * scsih_slave_configure - device configure routine.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301805 * @sdev: scsi device struct
1806 *
1807 * Returns 0 if ok. Any other return is assumed to be an error and
1808 * the device is ignored.
1809 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301810int
1811scsih_slave_configure(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301812{
1813 struct Scsi_Host *shost = sdev->host;
1814 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1815 struct MPT3SAS_DEVICE *sas_device_priv_data;
1816 struct MPT3SAS_TARGET *sas_target_priv_data;
1817 struct _sas_device *sas_device;
1818 struct _raid_device *raid_device;
1819 unsigned long flags;
1820 int qdepth;
1821 u8 ssp_target = 0;
1822 char *ds = "";
1823 char *r_level = "";
1824 u16 handle, volume_handle = 0;
1825 u64 volume_wwid = 0;
1826
1827 qdepth = 1;
1828 sas_device_priv_data = sdev->hostdata;
1829 sas_device_priv_data->configured_lun = 1;
1830 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1831 sas_target_priv_data = sas_device_priv_data->sas_target;
1832 handle = sas_target_priv_data->handle;
1833
1834 /* raid volume handling */
1835 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1836
1837 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301838 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301839 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1840 if (!raid_device) {
1841 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1842 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1843 __LINE__, __func__));
1844 return 1;
1845 }
1846
1847 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1848 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1849 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1850 __LINE__, __func__));
1851 return 1;
1852 }
1853
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301854 /*
1855 * WARPDRIVE: Initialize the required data for Direct IO
1856 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301857 mpt3sas_init_warpdrive_properties(ioc, raid_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301858
1859 /* RAID Queue Depth Support
1860 * IS volume = underlying qdepth of drive type, either
1861 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1862 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1863 */
1864 if (raid_device->device_info &
1865 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1866 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1867 ds = "SSP";
1868 } else {
1869 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1870 if (raid_device->device_info &
1871 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1872 ds = "SATA";
1873 else
1874 ds = "STP";
1875 }
1876
1877 switch (raid_device->volume_type) {
1878 case MPI2_RAID_VOL_TYPE_RAID0:
1879 r_level = "RAID0";
1880 break;
1881 case MPI2_RAID_VOL_TYPE_RAID1E:
1882 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1883 if (ioc->manu_pg10.OEMIdentifier &&
1884 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1885 MFG10_GF0_R10_DISPLAY) &&
1886 !(raid_device->num_pds % 2))
1887 r_level = "RAID10";
1888 else
1889 r_level = "RAID1E";
1890 break;
1891 case MPI2_RAID_VOL_TYPE_RAID1:
1892 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1893 r_level = "RAID1";
1894 break;
1895 case MPI2_RAID_VOL_TYPE_RAID10:
1896 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1897 r_level = "RAID10";
1898 break;
1899 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1900 default:
1901 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1902 r_level = "RAIDX";
1903 break;
1904 }
1905
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301906 if (!ioc->hide_ir_msg)
1907 sdev_printk(KERN_INFO, sdev,
1908 "%s: handle(0x%04x), wwid(0x%016llx),"
1909 " pd_count(%d), type(%s)\n",
1910 r_level, raid_device->handle,
1911 (unsigned long long)raid_device->wwid,
1912 raid_device->num_pds, ds);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301913
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301914 scsih_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301915
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05301916 /* raid transport support */
1917 if (!ioc->is_warpdrive)
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05301918 _scsih_set_level(ioc, sdev, raid_device->volume_type);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301919 return 0;
1920 }
1921
1922 /* non-raid handling */
1923 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1924 if (mpt3sas_config_get_volume_handle(ioc, handle,
1925 &volume_handle)) {
1926 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1927 "failure at %s:%d/%s()!\n", ioc->name,
1928 __FILE__, __LINE__, __func__));
1929 return 1;
1930 }
1931 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1932 volume_handle, &volume_wwid)) {
1933 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1934 "failure at %s:%d/%s()!\n", ioc->name,
1935 __FILE__, __LINE__, __func__));
1936 return 1;
1937 }
1938 }
1939
1940 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301941 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301942 sas_device_priv_data->sas_target->sas_address);
1943 if (!sas_device) {
1944 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1945 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1946 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1947 __func__));
1948 return 1;
1949 }
1950
1951 sas_device->volume_handle = volume_handle;
1952 sas_device->volume_wwid = volume_wwid;
1953 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1954 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1955 ssp_target = 1;
Suganath prabu Subramani30158dc2016-01-28 12:07:01 +05301956 if (sas_device->device_info &
1957 MPI2_SAS_DEVICE_INFO_SEP) {
1958 sdev_printk(KERN_WARNING, sdev,
1959 "set ignore_delay_remove for handle(0x%04x)\n",
1960 sas_device_priv_data->sas_target->handle);
1961 sas_device_priv_data->ignore_delay_remove = 1;
1962 ds = "SES";
1963 } else
1964 ds = "SSP";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301965 } else {
1966 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1967 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1968 ds = "STP";
1969 else if (sas_device->device_info &
1970 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1971 ds = "SATA";
1972 }
1973
1974 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1975 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1976 ds, handle, (unsigned long long)sas_device->sas_address,
1977 sas_device->phy, (unsigned long long)sas_device->device_name);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05301978 if (sas_device->enclosure_handle != 0)
1979 sdev_printk(KERN_INFO, sdev,
1980 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1981 ds, (unsigned long long)
1982 sas_device->enclosure_logical_id, sas_device->slot);
1983 if (sas_device->connector_name[0] != '\0')
1984 sdev_printk(KERN_INFO, sdev,
1985 "%s: enclosure level(0x%04x), connector name( %s)\n",
1986 ds, sas_device->enclosure_level,
1987 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301988
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05301989 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301990 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1991
1992 if (!ssp_target)
1993 _scsih_display_sata_capabilities(ioc, handle, sdev);
1994
1995
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301996 scsih_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301997
1998 if (ssp_target) {
1999 sas_read_port_mode_page(sdev);
2000 _scsih_enable_tlr(ioc, sdev);
2001 }
2002
2003 return 0;
2004}
2005
2006/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302007 * scsih_bios_param - fetch head, sector, cylinder info for a disk
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302008 * @sdev: scsi device struct
2009 * @bdev: pointer to block device context
2010 * @capacity: device size (in 512 byte sectors)
2011 * @params: three element array to place output:
2012 * params[0] number of heads (max 255)
2013 * params[1] number of sectors (max 63)
2014 * params[2] number of cylinders
2015 *
2016 * Return nothing.
2017 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302018int
2019scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302020 sector_t capacity, int params[])
2021{
2022 int heads;
2023 int sectors;
2024 sector_t cylinders;
2025 ulong dummy;
2026
2027 heads = 64;
2028 sectors = 32;
2029
2030 dummy = heads * sectors;
2031 cylinders = capacity;
2032 sector_div(cylinders, dummy);
2033
2034 /*
2035 * Handle extended translation size for logical drives
2036 * > 1Gb
2037 */
2038 if ((ulong)capacity >= 0x200000) {
2039 heads = 255;
2040 sectors = 63;
2041 dummy = heads * sectors;
2042 cylinders = capacity;
2043 sector_div(cylinders, dummy);
2044 }
2045
2046 /* return result */
2047 params[0] = heads;
2048 params[1] = sectors;
2049 params[2] = cylinders;
2050
2051 return 0;
2052}
2053
2054/**
2055 * _scsih_response_code - translation of device response code
2056 * @ioc: per adapter object
2057 * @response_code: response code returned by the device
2058 *
2059 * Return nothing.
2060 */
2061static void
2062_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
2063{
2064 char *desc;
2065
2066 switch (response_code) {
2067 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2068 desc = "task management request completed";
2069 break;
2070 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2071 desc = "invalid frame";
2072 break;
2073 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2074 desc = "task management request not supported";
2075 break;
2076 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2077 desc = "task management request failed";
2078 break;
2079 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2080 desc = "task management request succeeded";
2081 break;
2082 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2083 desc = "invalid lun";
2084 break;
2085 case 0xA:
2086 desc = "overlapped tag attempted";
2087 break;
2088 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2089 desc = "task queued, however not sent to target";
2090 break;
2091 default:
2092 desc = "unknown";
2093 break;
2094 }
2095 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
2096 ioc->name, response_code, desc);
2097}
2098
2099/**
2100 * _scsih_tm_done - tm completion routine
2101 * @ioc: per adapter object
2102 * @smid: system request message index
2103 * @msix_index: MSIX table index supplied by the OS
2104 * @reply: reply message frame(lower 32bit addr)
2105 * Context: none.
2106 *
2107 * The callback handler when using scsih_issue_tm.
2108 *
2109 * Return 1 meaning mf should be freed from _base_interrupt
2110 * 0 means the mf is freed from this function.
2111 */
2112static u8
2113_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
2114{
2115 MPI2DefaultReply_t *mpi_reply;
2116
2117 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
2118 return 1;
2119 if (ioc->tm_cmds.smid != smid)
2120 return 1;
2121 mpt3sas_base_flush_reply_queues(ioc);
2122 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
2123 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
2124 if (mpi_reply) {
2125 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2126 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
2127 }
2128 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
2129 complete(&ioc->tm_cmds.done);
2130 return 1;
2131}
2132
2133/**
2134 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
2135 * @ioc: per adapter object
2136 * @handle: device handle
2137 *
2138 * During taskmangement request, we need to freeze the device queue.
2139 */
2140void
2141mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2142{
2143 struct MPT3SAS_DEVICE *sas_device_priv_data;
2144 struct scsi_device *sdev;
2145 u8 skip = 0;
2146
2147 shost_for_each_device(sdev, ioc->shost) {
2148 if (skip)
2149 continue;
2150 sas_device_priv_data = sdev->hostdata;
2151 if (!sas_device_priv_data)
2152 continue;
2153 if (sas_device_priv_data->sas_target->handle == handle) {
2154 sas_device_priv_data->sas_target->tm_busy = 1;
2155 skip = 1;
2156 ioc->ignore_loginfos = 1;
2157 }
2158 }
2159}
2160
2161/**
2162 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
2163 * @ioc: per adapter object
2164 * @handle: device handle
2165 *
2166 * During taskmangement request, we need to freeze the device queue.
2167 */
2168void
2169mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2170{
2171 struct MPT3SAS_DEVICE *sas_device_priv_data;
2172 struct scsi_device *sdev;
2173 u8 skip = 0;
2174
2175 shost_for_each_device(sdev, ioc->shost) {
2176 if (skip)
2177 continue;
2178 sas_device_priv_data = sdev->hostdata;
2179 if (!sas_device_priv_data)
2180 continue;
2181 if (sas_device_priv_data->sas_target->handle == handle) {
2182 sas_device_priv_data->sas_target->tm_busy = 0;
2183 skip = 1;
2184 ioc->ignore_loginfos = 0;
2185 }
2186 }
2187}
2188
2189/**
2190 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
2191 * @ioc: per adapter struct
2192 * @device_handle: device handle
2193 * @channel: the channel assigned by the OS
2194 * @id: the id assigned by the OS
2195 * @lun: lun number
2196 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2197 * @smid_task: smid assigned to the task
2198 * @timeout: timeout in seconds
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302199 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2200 * Context: user
2201 *
2202 * A generic API for sending task management requests to firmware.
2203 *
2204 * The callback index is set inside `ioc->tm_cb_idx`.
2205 *
2206 * Return SUCCESS or FAILED.
2207 */
2208int
2209mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2210 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002211 enum mutex_type m_type)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302212{
2213 Mpi2SCSITaskManagementRequest_t *mpi_request;
2214 Mpi2SCSITaskManagementReply_t *mpi_reply;
2215 u16 smid = 0;
2216 u32 ioc_state;
2217 unsigned long timeleft;
2218 struct scsiio_tracker *scsi_lookup = NULL;
2219 int rc;
2220
2221 if (m_type == TM_MUTEX_ON)
2222 mutex_lock(&ioc->tm_cmds.mutex);
2223 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2224 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2225 __func__, ioc->name);
2226 rc = FAILED;
2227 goto err_out;
2228 }
2229
2230 if (ioc->shost_recovery || ioc->remove_host ||
2231 ioc->pci_error_recovery) {
2232 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2233 __func__, ioc->name);
2234 rc = FAILED;
2235 goto err_out;
2236 }
2237
2238 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2239 if (ioc_state & MPI2_DOORBELL_USED) {
2240 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2241 "unexpected doorbell active!\n", ioc->name));
2242 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2243 FORCE_BIG_HAMMER);
2244 rc = (!rc) ? SUCCESS : FAILED;
2245 goto err_out;
2246 }
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);
2251 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2252 FORCE_BIG_HAMMER);
2253 rc = (!rc) ? SUCCESS : FAILED;
2254 goto err_out;
2255 }
2256
2257 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2258 if (!smid) {
2259 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2260 ioc->name, __func__);
2261 rc = FAILED;
2262 goto err_out;
2263 }
2264
2265 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2266 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2267
2268 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2269 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2270 ioc->name, handle, type, smid_task));
2271 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2272 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2273 ioc->tm_cmds.smid = smid;
2274 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2275 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2276 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2277 mpi_request->DevHandle = cpu_to_le16(handle);
2278 mpi_request->TaskType = type;
2279 mpi_request->TaskMID = cpu_to_le16(smid_task);
2280 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2281 mpt3sas_scsih_set_tm_flag(ioc, handle);
2282 init_completion(&ioc->tm_cmds.done);
2283 mpt3sas_base_put_smid_hi_priority(ioc, smid);
2284 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2285 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2286 pr_err(MPT3SAS_FMT "%s: timeout\n",
2287 ioc->name, __func__);
2288 _debug_dump_mf(mpi_request,
2289 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2290 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2291 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2292 FORCE_BIG_HAMMER);
2293 rc = (!rc) ? SUCCESS : FAILED;
2294 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2295 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2296 goto err_out;
2297 }
2298 }
2299
2300 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2301 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2302 mpi_reply = ioc->tm_cmds.reply;
2303 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2304 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2305 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2306 le32_to_cpu(mpi_reply->IOCLogInfo),
2307 le32_to_cpu(mpi_reply->TerminationCount)));
2308 if (ioc->logging_level & MPT_DEBUG_TM) {
2309 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2310 if (mpi_reply->IOCStatus)
2311 _debug_dump_mf(mpi_request,
2312 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2313 }
2314 }
2315
2316 switch (type) {
2317 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2318 rc = SUCCESS;
2319 if (scsi_lookup->scmd == NULL)
2320 break;
2321 rc = FAILED;
2322 break;
2323
2324 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2325 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2326 rc = FAILED;
2327 else
2328 rc = SUCCESS;
2329 break;
2330 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2331 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2332 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2333 rc = FAILED;
2334 else
2335 rc = SUCCESS;
2336 break;
2337 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2338 rc = SUCCESS;
2339 break;
2340 default:
2341 rc = FAILED;
2342 break;
2343 }
2344
2345 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2346 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2347 if (m_type == TM_MUTEX_ON)
2348 mutex_unlock(&ioc->tm_cmds.mutex);
2349
2350 return rc;
2351
2352 err_out:
2353 if (m_type == TM_MUTEX_ON)
2354 mutex_unlock(&ioc->tm_cmds.mutex);
2355 return rc;
2356}
2357
2358/**
2359 * _scsih_tm_display_info - displays info about the device
2360 * @ioc: per adapter struct
2361 * @scmd: pointer to scsi command object
2362 *
2363 * Called by task management callback handlers.
2364 */
2365static void
2366_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2367{
2368 struct scsi_target *starget = scmd->device->sdev_target;
2369 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2370 struct _sas_device *sas_device = NULL;
2371 unsigned long flags;
2372 char *device_str = NULL;
2373
2374 if (!priv_target)
2375 return;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05302376 if (ioc->hide_ir_msg)
2377 device_str = "WarpDrive";
2378 else
2379 device_str = "volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302380
2381 scsi_print_command(scmd);
2382 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2383 starget_printk(KERN_INFO, starget,
2384 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2385 device_str, priv_target->handle,
2386 device_str, (unsigned long long)priv_target->sas_address);
2387 } else {
2388 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302389 sas_device = __mpt3sas_get_sdev_from_target(ioc, priv_target);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302390 if (sas_device) {
2391 if (priv_target->flags &
2392 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2393 starget_printk(KERN_INFO, starget,
2394 "volume handle(0x%04x), "
2395 "volume wwid(0x%016llx)\n",
2396 sas_device->volume_handle,
2397 (unsigned long long)sas_device->volume_wwid);
2398 }
2399 starget_printk(KERN_INFO, starget,
2400 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2401 sas_device->handle,
2402 (unsigned long long)sas_device->sas_address,
2403 sas_device->phy);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05302404 if (sas_device->enclosure_handle != 0)
2405 starget_printk(KERN_INFO, starget,
2406 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2407 (unsigned long long)
2408 sas_device->enclosure_logical_id,
2409 sas_device->slot);
Suganath prabu Subramani869817f2016-01-28 12:07:00 +05302410 if (sas_device->connector_name[0] != '\0')
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05302411 starget_printk(KERN_INFO, starget,
2412 "enclosure level(0x%04x),connector name(%s)\n",
2413 sas_device->enclosure_level,
2414 sas_device->connector_name);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302415
2416 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302417 }
2418 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2419 }
2420}
2421
2422/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302423 * scsih_abort - eh threads main abort routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302424 * @scmd: pointer to scsi command object
2425 *
2426 * Returns SUCCESS if command aborted else FAILED
2427 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302428int
2429scsih_abort(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302430{
2431 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2432 struct MPT3SAS_DEVICE *sas_device_priv_data;
2433 u16 smid;
2434 u16 handle;
2435 int r;
2436
2437 sdev_printk(KERN_INFO, scmd->device,
2438 "attempting task abort! scmd(%p)\n", scmd);
2439 _scsih_tm_display_info(ioc, scmd);
2440
2441 sas_device_priv_data = scmd->device->hostdata;
2442 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2443 sdev_printk(KERN_INFO, scmd->device,
2444 "device been deleted! scmd(%p)\n", scmd);
2445 scmd->result = DID_NO_CONNECT << 16;
2446 scmd->scsi_done(scmd);
2447 r = SUCCESS;
2448 goto out;
2449 }
2450
2451 /* search for the command */
2452 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2453 if (!smid) {
2454 scmd->result = DID_RESET << 16;
2455 r = SUCCESS;
2456 goto out;
2457 }
2458
2459 /* for hidden raid components and volumes this is not supported */
2460 if (sas_device_priv_data->sas_target->flags &
2461 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2462 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2463 scmd->result = DID_RESET << 16;
2464 r = FAILED;
2465 goto out;
2466 }
2467
2468 mpt3sas_halt_firmware(ioc);
2469
2470 handle = sas_device_priv_data->sas_target->handle;
2471 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2472 scmd->device->id, scmd->device->lun,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002473 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302474
2475 out:
2476 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2477 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2478 return r;
2479}
2480
2481/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302482 * scsih_dev_reset - eh threads main device reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302483 * @scmd: pointer to scsi command object
2484 *
2485 * Returns SUCCESS if command aborted else FAILED
2486 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302487int
2488scsih_dev_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302489{
2490 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2491 struct MPT3SAS_DEVICE *sas_device_priv_data;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302492 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302493 u16 handle;
2494 int r;
2495
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302496 struct scsi_target *starget = scmd->device->sdev_target;
2497 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
2498
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302499 sdev_printk(KERN_INFO, scmd->device,
2500 "attempting device reset! scmd(%p)\n", scmd);
2501 _scsih_tm_display_info(ioc, scmd);
2502
2503 sas_device_priv_data = scmd->device->hostdata;
2504 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2505 sdev_printk(KERN_INFO, scmd->device,
2506 "device been deleted! scmd(%p)\n", scmd);
2507 scmd->result = DID_NO_CONNECT << 16;
2508 scmd->scsi_done(scmd);
2509 r = SUCCESS;
2510 goto out;
2511 }
2512
2513 /* for hidden raid components obtain the volume_handle */
2514 handle = 0;
2515 if (sas_device_priv_data->sas_target->flags &
2516 MPT_TARGET_FLAGS_RAID_COMPONENT) {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302517 sas_device = mpt3sas_get_sdev_from_target(ioc,
2518 target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302519 if (sas_device)
2520 handle = sas_device->volume_handle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302521 } else
2522 handle = sas_device_priv_data->sas_target->handle;
2523
2524 if (!handle) {
2525 scmd->result = DID_RESET << 16;
2526 r = FAILED;
2527 goto out;
2528 }
2529
2530 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2531 scmd->device->id, scmd->device->lun,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002532 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302533
2534 out:
2535 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2536 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302537
2538 if (sas_device)
2539 sas_device_put(sas_device);
2540
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302541 return r;
2542}
2543
2544/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302545 * scsih_target_reset - eh threads main target reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302546 * @scmd: pointer to scsi command object
2547 *
2548 * Returns SUCCESS if command aborted else FAILED
2549 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302550int
2551scsih_target_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302552{
2553 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2554 struct MPT3SAS_DEVICE *sas_device_priv_data;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302555 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302556 u16 handle;
2557 int r;
2558 struct scsi_target *starget = scmd->device->sdev_target;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302559 struct MPT3SAS_TARGET *target_priv_data = starget->hostdata;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302560
2561 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2562 scmd);
2563 _scsih_tm_display_info(ioc, scmd);
2564
2565 sas_device_priv_data = scmd->device->hostdata;
2566 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2567 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2568 scmd);
2569 scmd->result = DID_NO_CONNECT << 16;
2570 scmd->scsi_done(scmd);
2571 r = SUCCESS;
2572 goto out;
2573 }
2574
2575 /* for hidden raid components obtain the volume_handle */
2576 handle = 0;
2577 if (sas_device_priv_data->sas_target->flags &
2578 MPT_TARGET_FLAGS_RAID_COMPONENT) {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302579 sas_device = mpt3sas_get_sdev_from_target(ioc,
2580 target_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302581 if (sas_device)
2582 handle = sas_device->volume_handle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302583 } else
2584 handle = sas_device_priv_data->sas_target->handle;
2585
2586 if (!handle) {
2587 scmd->result = DID_RESET << 16;
2588 r = FAILED;
2589 goto out;
2590 }
2591
2592 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2593 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002594 30, TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302595
2596 out:
2597 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2598 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302599
2600 if (sas_device)
2601 sas_device_put(sas_device);
2602
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302603 return r;
2604}
2605
2606
2607/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302608 * scsih_host_reset - eh threads main host reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302609 * @scmd: pointer to scsi command object
2610 *
2611 * Returns SUCCESS if command aborted else FAILED
2612 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302613int
2614scsih_host_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302615{
2616 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2617 int r, retval;
2618
2619 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2620 ioc->name, scmd);
2621 scsi_print_command(scmd);
2622
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302623 if (ioc->is_driver_loading) {
2624 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2625 ioc->name);
2626 r = FAILED;
2627 goto out;
2628 }
2629
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302630 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2631 FORCE_BIG_HAMMER);
2632 r = (retval < 0) ? FAILED : SUCCESS;
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302633out:
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302634 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2635 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2636
2637 return r;
2638}
2639
2640/**
2641 * _scsih_fw_event_add - insert and queue up fw_event
2642 * @ioc: per adapter object
2643 * @fw_event: object describing the event
2644 * Context: This function will acquire ioc->fw_event_lock.
2645 *
2646 * This adds the firmware event object into link list, then queues it up to
2647 * be processed from user context.
2648 *
2649 * Return nothing.
2650 */
2651static void
2652_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2653{
2654 unsigned long flags;
2655
2656 if (ioc->firmware_event_thread == NULL)
2657 return;
2658
2659 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302660 fw_event_work_get(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302661 INIT_LIST_HEAD(&fw_event->list);
2662 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2663 INIT_WORK(&fw_event->work, _firmware_event_work);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302664 fw_event_work_get(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302665 queue_work(ioc->firmware_event_thread, &fw_event->work);
2666 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2667}
2668
2669/**
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302670 * _scsih_fw_event_del_from_list - delete fw_event from the list
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302671 * @ioc: per adapter object
2672 * @fw_event: object describing the event
2673 * Context: This function will acquire ioc->fw_event_lock.
2674 *
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302675 * If the fw_event is on the fw_event_list, remove it and do a put.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302676 *
2677 * Return nothing.
2678 */
2679static void
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302680_scsih_fw_event_del_from_list(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302681 *fw_event)
2682{
2683 unsigned long flags;
2684
2685 spin_lock_irqsave(&ioc->fw_event_lock, flags);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302686 if (!list_empty(&fw_event->list)) {
2687 list_del_init(&fw_event->list);
2688 fw_event_work_put(fw_event);
2689 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302690 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2691}
2692
2693
2694 /**
2695 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2696 * @ioc: per adapter object
2697 * @event_data: trigger event data
2698 *
2699 * Return nothing.
2700 */
2701void
2702mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2703 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2704{
2705 struct fw_event_work *fw_event;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302706 u16 sz;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302707
2708 if (ioc->is_driver_loading)
2709 return;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302710 sz = sizeof(*event_data);
2711 fw_event = alloc_fw_event_work(sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302712 if (!fw_event)
2713 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302714 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2715 fw_event->ioc = ioc;
2716 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2717 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302718 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302719}
2720
2721/**
2722 * _scsih_error_recovery_delete_devices - remove devices not responding
2723 * @ioc: per adapter object
2724 *
2725 * Return nothing.
2726 */
2727static void
2728_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2729{
2730 struct fw_event_work *fw_event;
2731
2732 if (ioc->is_driver_loading)
2733 return;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302734 fw_event = alloc_fw_event_work(0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302735 if (!fw_event)
2736 return;
2737 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2738 fw_event->ioc = ioc;
2739 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302740 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302741}
2742
2743/**
2744 * mpt3sas_port_enable_complete - port enable completed (fake event)
2745 * @ioc: per adapter object
2746 *
2747 * Return nothing.
2748 */
2749void
2750mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2751{
2752 struct fw_event_work *fw_event;
2753
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302754 fw_event = alloc_fw_event_work(0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302755 if (!fw_event)
2756 return;
2757 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2758 fw_event->ioc = ioc;
2759 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302760 fw_event_work_put(fw_event);
2761}
2762
2763static struct fw_event_work *dequeue_next_fw_event(struct MPT3SAS_ADAPTER *ioc)
2764{
2765 unsigned long flags;
2766 struct fw_event_work *fw_event = NULL;
2767
2768 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2769 if (!list_empty(&ioc->fw_event_list)) {
2770 fw_event = list_first_entry(&ioc->fw_event_list,
2771 struct fw_event_work, list);
2772 list_del_init(&fw_event->list);
2773 }
2774 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2775
2776 return fw_event;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302777}
2778
2779/**
2780 * _scsih_fw_event_cleanup_queue - cleanup event queue
2781 * @ioc: per adapter object
2782 *
2783 * Walk the firmware event queue, either killing timers, or waiting
2784 * for outstanding events to complete
2785 *
2786 * Return nothing.
2787 */
2788static void
2789_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2790{
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302791 struct fw_event_work *fw_event;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302792
2793 if (list_empty(&ioc->fw_event_list) ||
2794 !ioc->firmware_event_thread || in_interrupt())
2795 return;
2796
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05302797 while ((fw_event = dequeue_next_fw_event(ioc))) {
2798 /*
2799 * Wait on the fw_event to complete. If this returns 1, then
2800 * the event was never executed, and we need a put for the
2801 * reference the delayed_work had on the fw_event.
2802 *
2803 * If it did execute, we wait for it to finish, and the put will
2804 * happen from _firmware_event_work()
2805 */
2806 if (cancel_delayed_work_sync(&fw_event->delayed_work))
2807 fw_event_work_put(fw_event);
2808
2809 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302810 }
2811}
2812
2813/**
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302814 * _scsih_internal_device_block - block the sdev device
2815 * @sdev: per device object
2816 * @sas_device_priv_data : per device driver private data
2817 *
2818 * make sure device is blocked without error, if not
2819 * print an error
2820 */
2821static void
2822_scsih_internal_device_block(struct scsi_device *sdev,
2823 struct MPT3SAS_DEVICE *sas_device_priv_data)
2824{
2825 int r = 0;
2826
2827 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2828 sas_device_priv_data->sas_target->handle);
2829 sas_device_priv_data->block = 1;
2830
2831 r = scsi_internal_device_block(sdev);
2832 if (r == -EINVAL)
2833 sdev_printk(KERN_WARNING, sdev,
2834 "device_block failed with return(%d) for handle(0x%04x)\n",
2835 sas_device_priv_data->sas_target->handle, r);
2836}
2837
2838/**
2839 * _scsih_internal_device_unblock - unblock the sdev device
2840 * @sdev: per device object
2841 * @sas_device_priv_data : per device driver private data
2842 * make sure device is unblocked without error, if not retry
2843 * by blocking and then unblocking
2844 */
2845
2846static void
2847_scsih_internal_device_unblock(struct scsi_device *sdev,
2848 struct MPT3SAS_DEVICE *sas_device_priv_data)
2849{
2850 int r = 0;
2851
2852 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2853 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2854 sas_device_priv_data->block = 0;
2855 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2856 if (r == -EINVAL) {
2857 /* The device has been set to SDEV_RUNNING by SD layer during
2858 * device addition but the request queue is still stopped by
2859 * our earlier block call. We need to perform a block again
2860 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2861
2862 sdev_printk(KERN_WARNING, sdev,
2863 "device_unblock failed with return(%d) for handle(0x%04x) "
2864 "performing a block followed by an unblock\n",
2865 sas_device_priv_data->sas_target->handle, r);
2866 sas_device_priv_data->block = 1;
2867 r = scsi_internal_device_block(sdev);
2868 if (r)
2869 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2870 "failed with return(%d) for handle(0x%04x)\n",
2871 sas_device_priv_data->sas_target->handle, r);
2872
2873 sas_device_priv_data->block = 0;
2874 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2875 if (r)
2876 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2877 " failed with return(%d) for handle(0x%04x)\n",
2878 sas_device_priv_data->sas_target->handle, r);
2879 }
2880}
2881
2882/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302883 * _scsih_ublock_io_all_device - unblock every device
2884 * @ioc: per adapter object
2885 *
2886 * change the device state from block to running
2887 */
2888static void
2889_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2890{
2891 struct MPT3SAS_DEVICE *sas_device_priv_data;
2892 struct scsi_device *sdev;
2893
2894 shost_for_each_device(sdev, ioc->shost) {
2895 sas_device_priv_data = sdev->hostdata;
2896 if (!sas_device_priv_data)
2897 continue;
2898 if (!sas_device_priv_data->block)
2899 continue;
2900
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302901 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2902 "device_running, handle(0x%04x)\n",
2903 sas_device_priv_data->sas_target->handle));
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302904 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302905 }
2906}
2907
2908
2909/**
2910 * _scsih_ublock_io_device - prepare device to be deleted
2911 * @ioc: per adapter object
2912 * @sas_addr: sas address
2913 *
2914 * unblock then put device in offline state
2915 */
2916static void
2917_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2918{
2919 struct MPT3SAS_DEVICE *sas_device_priv_data;
2920 struct scsi_device *sdev;
2921
2922 shost_for_each_device(sdev, ioc->shost) {
2923 sas_device_priv_data = sdev->hostdata;
2924 if (!sas_device_priv_data)
2925 continue;
2926 if (sas_device_priv_data->sas_target->sas_address
2927 != sas_address)
2928 continue;
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302929 if (sas_device_priv_data->block)
2930 _scsih_internal_device_unblock(sdev,
2931 sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302932 }
2933}
2934
2935/**
2936 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2937 * @ioc: per adapter object
2938 * @handle: device handle
2939 *
2940 * During device pull we need to appropiately set the sdev state.
2941 */
2942static void
2943_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2944{
2945 struct MPT3SAS_DEVICE *sas_device_priv_data;
2946 struct scsi_device *sdev;
2947
2948 shost_for_each_device(sdev, ioc->shost) {
2949 sas_device_priv_data = sdev->hostdata;
2950 if (!sas_device_priv_data)
2951 continue;
2952 if (sas_device_priv_data->block)
2953 continue;
Suganath prabu Subramani30158dc2016-01-28 12:07:01 +05302954 if (sas_device_priv_data->ignore_delay_remove) {
2955 sdev_printk(KERN_INFO, sdev,
2956 "%s skip device_block for SES handle(0x%04x)\n",
2957 __func__, sas_device_priv_data->sas_target->handle);
2958 continue;
2959 }
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302960 _scsih_internal_device_block(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302961 }
2962}
2963
2964/**
2965 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2966 * @ioc: per adapter object
2967 * @handle: device handle
2968 *
2969 * During device pull we need to appropiately set the sdev state.
2970 */
2971static void
2972_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2973{
2974 struct MPT3SAS_DEVICE *sas_device_priv_data;
2975 struct scsi_device *sdev;
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302976 struct _sas_device *sas_device;
2977
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05302978 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302979 if (!sas_device)
2980 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302981
2982 shost_for_each_device(sdev, ioc->shost) {
2983 sas_device_priv_data = sdev->hostdata;
2984 if (!sas_device_priv_data)
2985 continue;
2986 if (sas_device_priv_data->sas_target->handle != handle)
2987 continue;
2988 if (sas_device_priv_data->block)
2989 continue;
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302990 if (sas_device->pend_sas_rphy_add)
2991 continue;
Suganath prabu Subramani30158dc2016-01-28 12:07:01 +05302992 if (sas_device_priv_data->ignore_delay_remove) {
2993 sdev_printk(KERN_INFO, sdev,
2994 "%s skip device_block for SES handle(0x%04x)\n",
2995 __func__, sas_device_priv_data->sas_target->handle);
2996 continue;
2997 }
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302998 _scsih_internal_device_block(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302999 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303000
3001 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303002}
3003
3004/**
3005 * _scsih_block_io_to_children_attached_to_ex
3006 * @ioc: per adapter object
3007 * @sas_expander: the sas_device object
3008 *
3009 * This routine set sdev state to SDEV_BLOCK for all devices
3010 * attached to this expander. This function called when expander is
3011 * pulled.
3012 */
3013static void
3014_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
3015 struct _sas_node *sas_expander)
3016{
3017 struct _sas_port *mpt3sas_port;
3018 struct _sas_device *sas_device;
3019 struct _sas_node *expander_sibling;
3020 unsigned long flags;
3021
3022 if (!sas_expander)
3023 return;
3024
3025 list_for_each_entry(mpt3sas_port,
3026 &sas_expander->sas_port_list, port_list) {
3027 if (mpt3sas_port->remote_identify.device_type ==
3028 SAS_END_DEVICE) {
3029 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303030 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
3031 mpt3sas_port->remote_identify.sas_address);
3032 if (sas_device) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303033 set_bit(sas_device->handle,
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303034 ioc->blocking_handles);
3035 sas_device_put(sas_device);
3036 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303037 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3038 }
3039 }
3040
3041 list_for_each_entry(mpt3sas_port,
3042 &sas_expander->sas_port_list, port_list) {
3043
3044 if (mpt3sas_port->remote_identify.device_type ==
3045 SAS_EDGE_EXPANDER_DEVICE ||
3046 mpt3sas_port->remote_identify.device_type ==
3047 SAS_FANOUT_EXPANDER_DEVICE) {
3048 expander_sibling =
3049 mpt3sas_scsih_expander_find_by_sas_address(
3050 ioc, mpt3sas_port->remote_identify.sas_address);
3051 _scsih_block_io_to_children_attached_to_ex(ioc,
3052 expander_sibling);
3053 }
3054 }
3055}
3056
3057/**
3058 * _scsih_block_io_to_children_attached_directly
3059 * @ioc: per adapter object
3060 * @event_data: topology change event data
3061 *
3062 * This routine set sdev state to SDEV_BLOCK for all devices
3063 * direct attached during device pull.
3064 */
3065static void
3066_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
3067 Mpi2EventDataSasTopologyChangeList_t *event_data)
3068{
3069 int i;
3070 u16 handle;
3071 u16 reason_code;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303072
3073 for (i = 0; i < event_data->NumEntries; i++) {
3074 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3075 if (!handle)
3076 continue;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303077 reason_code = event_data->PHY[i].PhyStatus &
3078 MPI2_EVENT_SAS_TOPO_RC_MASK;
3079 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3080 _scsih_block_io_device(ioc, handle);
3081 }
3082}
3083
3084/**
3085 * _scsih_tm_tr_send - send task management request
3086 * @ioc: per adapter object
3087 * @handle: device handle
3088 * Context: interrupt time.
3089 *
3090 * This code is to initiate the device removal handshake protocol
3091 * with controller firmware. This function will issue target reset
3092 * using high priority request queue. It will send a sas iounit
3093 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
3094 *
3095 * This is designed to send muliple task management request at the same
3096 * time to the fifo. If the fifo is full, we will append the request,
3097 * and process it in a future completion.
3098 */
3099static void
3100_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3101{
3102 Mpi2SCSITaskManagementRequest_t *mpi_request;
3103 u16 smid;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303104 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303105 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
3106 u64 sas_address = 0;
3107 unsigned long flags;
3108 struct _tr_list *delayed_tr;
3109 u32 ioc_state;
3110
3111 if (ioc->remove_host) {
3112 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3113 "%s: host has been removed: handle(0x%04x)\n",
3114 __func__, ioc->name, handle));
3115 return;
3116 } else if (ioc->pci_error_recovery) {
3117 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3118 "%s: host in pci error recovery: handle(0x%04x)\n",
3119 __func__, ioc->name,
3120 handle));
3121 return;
3122 }
3123 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3124 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3125 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3126 "%s: host is not operational: handle(0x%04x)\n",
3127 __func__, ioc->name,
3128 handle));
3129 return;
3130 }
3131
3132 /* if PD, then return */
3133 if (test_bit(handle, ioc->pd_handles))
3134 return;
3135
3136 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303137 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303138 if (sas_device && sas_device->starget &&
3139 sas_device->starget->hostdata) {
3140 sas_target_priv_data = sas_device->starget->hostdata;
3141 sas_target_priv_data->deleted = 1;
3142 sas_address = sas_device->sas_address;
3143 }
3144 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3145
3146 if (sas_target_priv_data) {
3147 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3148 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
3149 ioc->name, handle,
3150 (unsigned long long)sas_address));
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05303151 if (sas_device->enclosure_handle != 0)
3152 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3153 "setting delete flag:enclosure logical id(0x%016llx),"
3154 " slot(%d)\n", ioc->name, (unsigned long long)
3155 sas_device->enclosure_logical_id,
3156 sas_device->slot));
Suganath prabu Subramani869817f2016-01-28 12:07:00 +05303157 if (sas_device->connector_name[0] != '\0')
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05303158 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3159 "setting delete flag: enclosure level(0x%04x),"
3160 " connector name( %s)\n", ioc->name,
3161 sas_device->enclosure_level,
3162 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303163 _scsih_ublock_io_device(ioc, sas_address);
3164 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
3165 }
3166
3167 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3168 if (!smid) {
3169 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3170 if (!delayed_tr)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303171 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303172 INIT_LIST_HEAD(&delayed_tr->list);
3173 delayed_tr->handle = handle;
3174 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3175 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3176 "DELAYED:tr:handle(0x%04x), (open)\n",
3177 ioc->name, handle));
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303178 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303179 }
3180
3181 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3182 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3183 ioc->name, handle, smid,
3184 ioc->tm_tr_cb_idx));
3185 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3186 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3187 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3188 mpi_request->DevHandle = cpu_to_le16(handle);
3189 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3190 mpt3sas_base_put_smid_hi_priority(ioc, smid);
3191 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05303192
3193out:
3194 if (sas_device)
3195 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303196}
3197
3198/**
3199 * _scsih_tm_tr_complete -
3200 * @ioc: per adapter object
3201 * @smid: system request message index
3202 * @msix_index: MSIX table index supplied by the OS
3203 * @reply: reply message frame(lower 32bit addr)
3204 * Context: interrupt time.
3205 *
3206 * This is the target reset completion routine.
3207 * This code is part of the code to initiate the device removal
3208 * handshake protocol with controller firmware.
3209 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
3210 *
3211 * Return 1 meaning mf should be freed from _base_interrupt
3212 * 0 means the mf is freed from this function.
3213 */
3214static u8
3215_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3216 u32 reply)
3217{
3218 u16 handle;
3219 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3220 Mpi2SCSITaskManagementReply_t *mpi_reply =
3221 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3222 Mpi2SasIoUnitControlRequest_t *mpi_request;
3223 u16 smid_sas_ctrl;
3224 u32 ioc_state;
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303225 struct _sc_list *delayed_sc;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303226
3227 if (ioc->remove_host) {
3228 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3229 "%s: host has been removed\n", __func__, ioc->name));
3230 return 1;
3231 } else if (ioc->pci_error_recovery) {
3232 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3233 "%s: host in pci error recovery\n", __func__,
3234 ioc->name));
3235 return 1;
3236 }
3237 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3238 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3239 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3240 "%s: host is not operational\n", __func__, ioc->name));
3241 return 1;
3242 }
3243 if (unlikely(!mpi_reply)) {
3244 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3245 ioc->name, __FILE__, __LINE__, __func__);
3246 return 1;
3247 }
3248 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3249 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3250 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3251 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3252 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3253 ioc->name, handle,
3254 le16_to_cpu(mpi_reply->DevHandle), smid));
3255 return 0;
3256 }
3257
3258 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3259 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3260 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3261 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3262 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3263 le32_to_cpu(mpi_reply->IOCLogInfo),
3264 le32_to_cpu(mpi_reply->TerminationCount)));
3265
3266 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3267 if (!smid_sas_ctrl) {
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303268 delayed_sc = kzalloc(sizeof(*delayed_sc), GFP_ATOMIC);
3269 if (!delayed_sc)
3270 return _scsih_check_for_pending_tm(ioc, smid);
3271 INIT_LIST_HEAD(&delayed_sc->list);
3272 delayed_sc->handle = mpi_request_tm->DevHandle;
3273 list_add_tail(&delayed_sc->list, &ioc->delayed_sc_list);
3274 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3275 "DELAYED:sc:handle(0x%04x), (open)\n",
3276 ioc->name, handle));
3277 return _scsih_check_for_pending_tm(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303278 }
3279
3280 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3281 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3282 ioc->name, handle, smid_sas_ctrl,
3283 ioc->tm_sas_control_cb_idx));
3284 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3285 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3286 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3287 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3288 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3289 mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3290
3291 return _scsih_check_for_pending_tm(ioc, smid);
3292}
3293
3294
3295/**
3296 * _scsih_sas_control_complete - completion routine
3297 * @ioc: per adapter object
3298 * @smid: system request message index
3299 * @msix_index: MSIX table index supplied by the OS
3300 * @reply: reply message frame(lower 32bit addr)
3301 * Context: interrupt time.
3302 *
3303 * This is the sas iounit control completion routine.
3304 * This code is part of the code to initiate the device removal
3305 * handshake protocol with controller firmware.
3306 *
3307 * Return 1 meaning mf should be freed from _base_interrupt
3308 * 0 means the mf is freed from this function.
3309 */
3310static u8
3311_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3312 u8 msix_index, u32 reply)
3313{
3314 Mpi2SasIoUnitControlReply_t *mpi_reply =
3315 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3316
3317 if (likely(mpi_reply)) {
3318 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3319 "sc_complete:handle(0x%04x), (open) "
3320 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3321 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3322 le16_to_cpu(mpi_reply->IOCStatus),
3323 le32_to_cpu(mpi_reply->IOCLogInfo)));
3324 } else {
3325 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3326 ioc->name, __FILE__, __LINE__, __func__);
3327 }
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303328 return mpt3sas_check_for_pending_internal_cmds(ioc, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303329}
3330
3331/**
3332 * _scsih_tm_tr_volume_send - send target reset request for volumes
3333 * @ioc: per adapter object
3334 * @handle: device handle
3335 * Context: interrupt time.
3336 *
3337 * This is designed to send muliple task management request at the same
3338 * time to the fifo. If the fifo is full, we will append the request,
3339 * and process it in a future completion.
3340 */
3341static void
3342_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3343{
3344 Mpi2SCSITaskManagementRequest_t *mpi_request;
3345 u16 smid;
3346 struct _tr_list *delayed_tr;
3347
3348 if (ioc->shost_recovery || ioc->remove_host ||
3349 ioc->pci_error_recovery) {
3350 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3351 "%s: host reset in progress!\n",
3352 __func__, ioc->name));
3353 return;
3354 }
3355
3356 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3357 if (!smid) {
3358 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3359 if (!delayed_tr)
3360 return;
3361 INIT_LIST_HEAD(&delayed_tr->list);
3362 delayed_tr->handle = handle;
3363 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3364 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3365 "DELAYED:tr:handle(0x%04x), (open)\n",
3366 ioc->name, handle));
3367 return;
3368 }
3369
3370 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3371 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3372 ioc->name, handle, smid,
3373 ioc->tm_tr_volume_cb_idx));
3374 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3375 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3376 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3377 mpi_request->DevHandle = cpu_to_le16(handle);
3378 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3379 mpt3sas_base_put_smid_hi_priority(ioc, smid);
3380}
3381
3382/**
3383 * _scsih_tm_volume_tr_complete - target reset completion
3384 * @ioc: per adapter object
3385 * @smid: system request message index
3386 * @msix_index: MSIX table index supplied by the OS
3387 * @reply: reply message frame(lower 32bit addr)
3388 * Context: interrupt time.
3389 *
3390 * Return 1 meaning mf should be freed from _base_interrupt
3391 * 0 means the mf is freed from this function.
3392 */
3393static u8
3394_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3395 u8 msix_index, u32 reply)
3396{
3397 u16 handle;
3398 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3399 Mpi2SCSITaskManagementReply_t *mpi_reply =
3400 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3401
3402 if (ioc->shost_recovery || ioc->remove_host ||
3403 ioc->pci_error_recovery) {
3404 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3405 "%s: host reset in progress!\n",
3406 __func__, ioc->name));
3407 return 1;
3408 }
3409 if (unlikely(!mpi_reply)) {
3410 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3411 ioc->name, __FILE__, __LINE__, __func__);
3412 return 1;
3413 }
3414
3415 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3416 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3417 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3418 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3419 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3420 ioc->name, handle,
3421 le16_to_cpu(mpi_reply->DevHandle), smid));
3422 return 0;
3423 }
3424
3425 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3426 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3427 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3428 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3429 le32_to_cpu(mpi_reply->IOCLogInfo),
3430 le32_to_cpu(mpi_reply->TerminationCount)));
3431
3432 return _scsih_check_for_pending_tm(ioc, smid);
3433}
3434
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05303435/**
3436 * _scsih_issue_delayed_event_ack - issue delayed Event ACK messages
3437 * @ioc: per adapter object
3438 * @smid: system request message index
3439 * @event: Event ID
3440 * @event_context: used to track events uniquely
3441 *
3442 * Context - processed in interrupt context.
3443 */
3444void
3445_scsih_issue_delayed_event_ack(struct MPT3SAS_ADAPTER *ioc, u16 smid, u16 event,
3446 u32 event_context)
3447{
3448 Mpi2EventAckRequest_t *ack_request;
3449 int i = smid - ioc->internal_smid;
3450 unsigned long flags;
3451
3452 /* Without releasing the smid just update the
3453 * call back index and reuse the same smid for
3454 * processing this delayed request
3455 */
3456 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3457 ioc->internal_lookup[i].cb_idx = ioc->base_cb_idx;
3458 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3459
3460 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3461 "EVENT ACK: event(0x%04x), smid(%d), cb(%d)\n",
3462 ioc->name, le16_to_cpu(event), smid,
3463 ioc->base_cb_idx));
3464 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
3465 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
3466 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
3467 ack_request->Event = event;
3468 ack_request->EventContext = event_context;
3469 ack_request->VF_ID = 0; /* TODO */
3470 ack_request->VP_ID = 0;
3471 mpt3sas_base_put_smid_default(ioc, smid);
3472}
3473
3474/**
3475 * _scsih_issue_delayed_sas_io_unit_ctrl - issue delayed
3476 * sas_io_unit_ctrl messages
3477 * @ioc: per adapter object
3478 * @smid: system request message index
3479 * @handle: device handle
3480 *
3481 * Context - processed in interrupt context.
3482 */
3483void
3484_scsih_issue_delayed_sas_io_unit_ctrl(struct MPT3SAS_ADAPTER *ioc,
3485 u16 smid, u16 handle)
3486 {
3487 Mpi2SasIoUnitControlRequest_t *mpi_request;
3488 u32 ioc_state;
3489 int i = smid - ioc->internal_smid;
3490 unsigned long flags;
3491
3492 if (ioc->remove_host) {
3493 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3494 "%s: host has been removed\n",
3495 __func__, ioc->name));
3496 return;
3497 } else if (ioc->pci_error_recovery) {
3498 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3499 "%s: host in pci error recovery\n",
3500 __func__, ioc->name));
3501 return;
3502 }
3503 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
3504 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3505 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3506 "%s: host is not operational\n",
3507 __func__, ioc->name));
3508 return;
3509 }
3510
3511 /* Without releasing the smid just update the
3512 * call back index and reuse the same smid for
3513 * processing this delayed request
3514 */
3515 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
3516 ioc->internal_lookup[i].cb_idx = ioc->tm_sas_control_cb_idx;
3517 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
3518
3519 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3520 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3521 ioc->name, le16_to_cpu(handle), smid,
3522 ioc->tm_sas_control_cb_idx));
3523 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3524 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3525 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3526 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3527 mpi_request->DevHandle = handle;
3528 mpt3sas_base_put_smid_default(ioc, smid);
3529}
3530
3531/**
3532 * _scsih_check_for_pending_internal_cmds - check for pending internal messages
3533 * @ioc: per adapter object
3534 * @smid: system request message index
3535 *
3536 * Context: Executed in interrupt context
3537 *
3538 * This will check delayed internal messages list, and process the
3539 * next request.
3540 *
3541 * Return 1 meaning mf should be freed from _base_interrupt
3542 * 0 means the mf is freed from this function.
3543 */
3544u8
3545mpt3sas_check_for_pending_internal_cmds(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3546{
3547 struct _sc_list *delayed_sc;
3548 struct _event_ack_list *delayed_event_ack;
3549
3550 if (!list_empty(&ioc->delayed_event_ack_list)) {
3551 delayed_event_ack = list_entry(ioc->delayed_event_ack_list.next,
3552 struct _event_ack_list, list);
3553 _scsih_issue_delayed_event_ack(ioc, smid,
3554 delayed_event_ack->Event, delayed_event_ack->EventContext);
3555 list_del(&delayed_event_ack->list);
3556 kfree(delayed_event_ack);
3557 return 0;
3558 }
3559
3560 if (!list_empty(&ioc->delayed_sc_list)) {
3561 delayed_sc = list_entry(ioc->delayed_sc_list.next,
3562 struct _sc_list, list);
3563 _scsih_issue_delayed_sas_io_unit_ctrl(ioc, smid,
3564 delayed_sc->handle);
3565 list_del(&delayed_sc->list);
3566 kfree(delayed_sc);
3567 return 0;
3568 }
3569 return 1;
3570}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303571
3572/**
3573 * _scsih_check_for_pending_tm - check for pending task management
3574 * @ioc: per adapter object
3575 * @smid: system request message index
3576 *
3577 * This will check delayed target reset list, and feed the
3578 * next reqeust.
3579 *
3580 * Return 1 meaning mf should be freed from _base_interrupt
3581 * 0 means the mf is freed from this function.
3582 */
3583static u8
3584_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3585{
3586 struct _tr_list *delayed_tr;
3587
3588 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3589 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3590 struct _tr_list, list);
3591 mpt3sas_base_free_smid(ioc, smid);
3592 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3593 list_del(&delayed_tr->list);
3594 kfree(delayed_tr);
3595 return 0;
3596 }
3597
3598 if (!list_empty(&ioc->delayed_tr_list)) {
3599 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3600 struct _tr_list, list);
3601 mpt3sas_base_free_smid(ioc, smid);
3602 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3603 list_del(&delayed_tr->list);
3604 kfree(delayed_tr);
3605 return 0;
3606 }
3607
3608 return 1;
3609}
3610
3611/**
3612 * _scsih_check_topo_delete_events - sanity check on topo events
3613 * @ioc: per adapter object
3614 * @event_data: the event data payload
3615 *
3616 * This routine added to better handle cable breaker.
3617 *
3618 * This handles the case where driver receives multiple expander
3619 * add and delete events in a single shot. When there is a delete event
3620 * the routine will void any pending add events waiting in the event queue.
3621 *
3622 * Return nothing.
3623 */
3624static void
3625_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3626 Mpi2EventDataSasTopologyChangeList_t *event_data)
3627{
3628 struct fw_event_work *fw_event;
3629 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3630 u16 expander_handle;
3631 struct _sas_node *sas_expander;
3632 unsigned long flags;
3633 int i, reason_code;
3634 u16 handle;
3635
3636 for (i = 0 ; i < event_data->NumEntries; i++) {
3637 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3638 if (!handle)
3639 continue;
3640 reason_code = event_data->PHY[i].PhyStatus &
3641 MPI2_EVENT_SAS_TOPO_RC_MASK;
3642 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3643 _scsih_tm_tr_send(ioc, handle);
3644 }
3645
3646 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3647 if (expander_handle < ioc->sas_hba.num_phys) {
3648 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3649 return;
3650 }
3651 if (event_data->ExpStatus ==
3652 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3653 /* put expander attached devices into blocking state */
3654 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3655 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3656 expander_handle);
3657 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3658 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3659 do {
3660 handle = find_first_bit(ioc->blocking_handles,
3661 ioc->facts.MaxDevHandle);
3662 if (handle < ioc->facts.MaxDevHandle)
3663 _scsih_block_io_device(ioc, handle);
3664 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3665 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3666 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3667
3668 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3669 return;
3670
3671 /* mark ignore flag for pending events */
3672 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3673 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3674 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3675 fw_event->ignore)
3676 continue;
Joe Lawrence35b62362014-06-25 17:05:34 -04003677 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3678 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303679 if (local_event_data->ExpStatus ==
3680 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3681 local_event_data->ExpStatus ==
3682 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3683 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3684 expander_handle) {
3685 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3686 "setting ignoring flag\n", ioc->name));
3687 fw_event->ignore = 1;
3688 }
3689 }
3690 }
3691 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3692}
3693
3694/**
3695 * _scsih_set_volume_delete_flag - setting volume delete flag
3696 * @ioc: per adapter object
3697 * @handle: device handle
3698 *
3699 * This returns nothing.
3700 */
3701static void
3702_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3703{
3704 struct _raid_device *raid_device;
3705 struct MPT3SAS_TARGET *sas_target_priv_data;
3706 unsigned long flags;
3707
3708 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05303709 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303710 if (raid_device && raid_device->starget &&
3711 raid_device->starget->hostdata) {
3712 sas_target_priv_data =
3713 raid_device->starget->hostdata;
3714 sas_target_priv_data->deleted = 1;
3715 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3716 "setting delete flag: handle(0x%04x), "
3717 "wwid(0x%016llx)\n", ioc->name, handle,
3718 (unsigned long long) raid_device->wwid));
3719 }
3720 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3721}
3722
3723/**
3724 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3725 * @handle: input handle
3726 * @a: handle for volume a
3727 * @b: handle for volume b
3728 *
3729 * IR firmware only supports two raid volumes. The purpose of this
3730 * routine is to set the volume handle in either a or b. When the given
3731 * input handle is non-zero, or when a and b have not been set before.
3732 */
3733static void
3734_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3735{
3736 if (!handle || handle == *a || handle == *b)
3737 return;
3738 if (!*a)
3739 *a = handle;
3740 else if (!*b)
3741 *b = handle;
3742}
3743
3744/**
3745 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3746 * @ioc: per adapter object
3747 * @event_data: the event data payload
3748 * Context: interrupt time.
3749 *
3750 * This routine will send target reset to volume, followed by target
3751 * resets to the PDs. This is called when a PD has been removed, or
3752 * volume has been deleted or removed. When the target reset is sent
3753 * to volume, the PD target resets need to be queued to start upon
3754 * completion of the volume target reset.
3755 *
3756 * Return nothing.
3757 */
3758static void
3759_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3760 Mpi2EventDataIrConfigChangeList_t *event_data)
3761{
3762 Mpi2EventIrConfigElement_t *element;
3763 int i;
3764 u16 handle, volume_handle, a, b;
3765 struct _tr_list *delayed_tr;
3766
3767 a = 0;
3768 b = 0;
3769
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05303770 if (ioc->is_warpdrive)
3771 return;
3772
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303773 /* Volume Resets for Deleted or Removed */
3774 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3775 for (i = 0; i < event_data->NumElements; i++, element++) {
3776 if (le32_to_cpu(event_data->Flags) &
3777 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3778 continue;
3779 if (element->ReasonCode ==
3780 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3781 element->ReasonCode ==
3782 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3783 volume_handle = le16_to_cpu(element->VolDevHandle);
3784 _scsih_set_volume_delete_flag(ioc, volume_handle);
3785 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3786 }
3787 }
3788
3789 /* Volume Resets for UNHIDE events */
3790 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3791 for (i = 0; i < event_data->NumElements; i++, element++) {
3792 if (le32_to_cpu(event_data->Flags) &
3793 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3794 continue;
3795 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3796 volume_handle = le16_to_cpu(element->VolDevHandle);
3797 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3798 }
3799 }
3800
3801 if (a)
3802 _scsih_tm_tr_volume_send(ioc, a);
3803 if (b)
3804 _scsih_tm_tr_volume_send(ioc, b);
3805
3806 /* PD target resets */
3807 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3808 for (i = 0; i < event_data->NumElements; i++, element++) {
3809 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3810 continue;
3811 handle = le16_to_cpu(element->PhysDiskDevHandle);
3812 volume_handle = le16_to_cpu(element->VolDevHandle);
3813 clear_bit(handle, ioc->pd_handles);
3814 if (!volume_handle)
3815 _scsih_tm_tr_send(ioc, handle);
3816 else if (volume_handle == a || volume_handle == b) {
3817 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3818 BUG_ON(!delayed_tr);
3819 INIT_LIST_HEAD(&delayed_tr->list);
3820 delayed_tr->handle = handle;
3821 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3822 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3823 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3824 handle));
3825 } else
3826 _scsih_tm_tr_send(ioc, handle);
3827 }
3828}
3829
3830
3831/**
3832 * _scsih_check_volume_delete_events - set delete flag for volumes
3833 * @ioc: per adapter object
3834 * @event_data: the event data payload
3835 * Context: interrupt time.
3836 *
3837 * This will handle the case when the cable connected to entire volume is
3838 * pulled. We will take care of setting the deleted flag so normal IO will
3839 * not be sent.
3840 *
3841 * Return nothing.
3842 */
3843static void
3844_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3845 Mpi2EventDataIrVolume_t *event_data)
3846{
3847 u32 state;
3848
3849 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3850 return;
3851 state = le32_to_cpu(event_data->NewValue);
3852 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3853 MPI2_RAID_VOL_STATE_FAILED)
3854 _scsih_set_volume_delete_flag(ioc,
3855 le16_to_cpu(event_data->VolDevHandle));
3856}
3857
3858/**
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303859 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3860 * @ioc: per adapter object
3861 * @event_data: the temp threshold event data
3862 * Context: interrupt time.
3863 *
3864 * Return nothing.
3865 */
3866static void
3867_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3868 Mpi2EventDataTemperature_t *event_data)
3869{
3870 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3871 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3872 " exceeded for Sensor: %d !!!\n", ioc->name,
3873 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3874 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3875 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3876 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3877 event_data->SensorNum);
3878 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3879 ioc->name, event_data->CurrentTemperature);
3880 }
3881}
3882
3883/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303884 * _scsih_flush_running_cmds - completing outstanding commands.
3885 * @ioc: per adapter object
3886 *
3887 * The flushing out of all pending scmd commands following host reset,
3888 * where all IO is dropped to the floor.
3889 *
3890 * Return nothing.
3891 */
3892static void
3893_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3894{
3895 struct scsi_cmnd *scmd;
3896 u16 smid;
3897 u16 count = 0;
3898
3899 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3900 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3901 if (!scmd)
3902 continue;
3903 count++;
3904 mpt3sas_base_free_smid(ioc, smid);
3905 scsi_dma_unmap(scmd);
3906 if (ioc->pci_error_recovery)
3907 scmd->result = DID_NO_CONNECT << 16;
3908 else
3909 scmd->result = DID_RESET << 16;
3910 scmd->scsi_done(scmd);
3911 }
3912 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3913 ioc->name, count));
3914}
3915
3916/**
3917 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3918 * @ioc: per adapter object
3919 * @scmd: pointer to scsi command object
3920 * @mpi_request: pointer to the SCSI_IO reqest message frame
3921 *
3922 * Supporting protection 1 and 3.
3923 *
3924 * Returns nothing
3925 */
3926static void
3927_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3928 Mpi2SCSIIORequest_t *mpi_request)
3929{
3930 u16 eedp_flags;
3931 unsigned char prot_op = scsi_get_prot_op(scmd);
3932 unsigned char prot_type = scsi_get_prot_type(scmd);
3933 Mpi25SCSIIORequest_t *mpi_request_3v =
3934 (Mpi25SCSIIORequest_t *)mpi_request;
3935
3936 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3937 return;
3938
3939 if (prot_op == SCSI_PROT_READ_STRIP)
3940 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3941 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3942 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3943 else
3944 return;
3945
3946 switch (prot_type) {
3947 case SCSI_PROT_DIF_TYPE1:
3948 case SCSI_PROT_DIF_TYPE2:
3949
3950 /*
3951 * enable ref/guard checking
3952 * auto increment ref tag
3953 */
3954 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3955 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3956 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3957 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3958 cpu_to_be32(scsi_get_lba(scmd));
3959 break;
3960
3961 case SCSI_PROT_DIF_TYPE3:
3962
3963 /*
3964 * enable guard checking
3965 */
3966 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3967
3968 break;
3969 }
3970
3971 mpi_request_3v->EEDPBlockSize =
3972 cpu_to_le16(scmd->device->sector_size);
3973 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3974}
3975
3976/**
3977 * _scsih_eedp_error_handling - return sense code for EEDP errors
3978 * @scmd: pointer to scsi command object
3979 * @ioc_status: ioc status
3980 *
3981 * Returns nothing
3982 */
3983static void
3984_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3985{
3986 u8 ascq;
3987
3988 switch (ioc_status) {
3989 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3990 ascq = 0x01;
3991 break;
3992 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3993 ascq = 0x02;
3994 break;
3995 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3996 ascq = 0x03;
3997 break;
3998 default:
3999 ascq = 0x00;
4000 break;
4001 }
4002 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
4003 ascq);
4004 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
4005 SAM_STAT_CHECK_CONDITION;
4006}
4007
4008
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304009
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304010/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05304011 * scsih_qcmd - main scsi request entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304012 * @scmd: pointer to scsi command object
4013 * @done: function pointer to be invoked on completion
4014 *
4015 * The callback index is set inside `ioc->scsi_io_cb_idx`.
4016 *
4017 * Returns 0 on success. If there's a failure, return either:
4018 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
4019 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
4020 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05304021int
4022scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304023{
Matthew Wilcoxd8bfbd82014-03-27 16:40:31 -04004024 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304025 struct MPT3SAS_DEVICE *sas_device_priv_data;
4026 struct MPT3SAS_TARGET *sas_target_priv_data;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304027 struct _raid_device *raid_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304028 Mpi2SCSIIORequest_t *mpi_request;
4029 u32 mpi_control;
4030 u16 smid;
4031 u16 handle;
4032
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304033 if (ioc->logging_level & MPT_DEBUG_SCSI)
4034 scsi_print_command(scmd);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304035
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304036 sas_device_priv_data = scmd->device->hostdata;
4037 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
4038 scmd->result = DID_NO_CONNECT << 16;
4039 scmd->scsi_done(scmd);
4040 return 0;
4041 }
4042
4043 if (ioc->pci_error_recovery || ioc->remove_host) {
4044 scmd->result = DID_NO_CONNECT << 16;
4045 scmd->scsi_done(scmd);
4046 return 0;
4047 }
4048
4049 sas_target_priv_data = sas_device_priv_data->sas_target;
4050
4051 /* invalid device handle */
4052 handle = sas_target_priv_data->handle;
4053 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
4054 scmd->result = DID_NO_CONNECT << 16;
4055 scmd->scsi_done(scmd);
4056 return 0;
4057 }
4058
4059
4060 /* host recovery or link resets sent via IOCTLs */
4061 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
4062 return SCSI_MLQUEUE_HOST_BUSY;
4063
4064 /* device has been deleted */
4065 else if (sas_target_priv_data->deleted) {
4066 scmd->result = DID_NO_CONNECT << 16;
4067 scmd->scsi_done(scmd);
4068 return 0;
4069 /* device busy with task managment */
4070 } else if (sas_target_priv_data->tm_busy ||
4071 sas_device_priv_data->block)
4072 return SCSI_MLQUEUE_DEVICE_BUSY;
4073
4074 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
4075 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4076 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4077 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4078 else
4079 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4080
4081 /* set tags */
Christoph Hellwig609aa222014-10-30 11:54:58 +01004082 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304083
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304084 /* Make sure Device is not raid volume.
4085 * We do not expose raid functionality to upper layer for warpdrive.
4086 */
4087 if (!ioc->is_warpdrive && !scsih_is_raid(&scmd->device->sdev_gendev)
Sreekanth Reddy15de0de2015-11-12 23:40:37 +05304088 && sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304089 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4090
4091 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
4092 if (!smid) {
4093 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
4094 ioc->name, __func__);
4095 goto out;
4096 }
4097 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4098 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
4099 _scsih_setup_eedp(ioc, scmd, mpi_request);
4100
4101 if (scmd->cmd_len == 32)
4102 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
4103 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4104 if (sas_device_priv_data->sas_target->flags &
4105 MPT_TARGET_FLAGS_RAID_COMPONENT)
4106 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4107 else
4108 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4109 mpi_request->DevHandle = cpu_to_le16(handle);
4110 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4111 mpi_request->Control = cpu_to_le32(mpi_control);
4112 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4113 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4114 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4115 mpi_request->SenseBufferLowAddress =
4116 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
4117 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4118 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4119 mpi_request->LUN);
4120 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4121
4122 if (mpi_request->DataLength) {
4123 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
4124 mpt3sas_base_free_smid(ioc, smid);
4125 goto out;
4126 }
4127 } else
4128 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
4129
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304130 raid_device = sas_target_priv_data->raid_device;
4131 if (raid_device && raid_device->direct_io_enabled)
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304132 mpt3sas_setup_direct_io(ioc, scmd, raid_device, mpi_request,
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304133 smid);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304134
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304135 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
4136 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
4137 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
4138 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
4139 mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
4140 } else
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304141 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4142 le16_to_cpu(mpi_request->DevHandle));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304143 } else
4144 mpt3sas_base_put_smid_default(ioc, smid);
4145 return 0;
4146
4147 out:
4148 return SCSI_MLQUEUE_HOST_BUSY;
4149}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304150
4151/**
4152 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4153 * @sense_buffer: sense data returned by target
4154 * @data: normalized skey/asc/ascq
4155 *
4156 * Return nothing.
4157 */
4158static void
4159_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4160{
4161 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4162 /* descriptor format */
4163 data->skey = sense_buffer[1] & 0x0F;
4164 data->asc = sense_buffer[2];
4165 data->ascq = sense_buffer[3];
4166 } else {
4167 /* fixed format */
4168 data->skey = sense_buffer[2] & 0x0F;
4169 data->asc = sense_buffer[12];
4170 data->ascq = sense_buffer[13];
4171 }
4172}
4173
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304174/**
4175 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
4176 * @ioc: per adapter object
4177 * @scmd: pointer to scsi command object
4178 * @mpi_reply: reply mf payload returned from firmware
4179 *
4180 * scsi_status - SCSI Status code returned from target device
4181 * scsi_state - state info associated with SCSI_IO determined by ioc
4182 * ioc_status - ioc supplied status info
4183 *
4184 * Return nothing.
4185 */
4186static void
4187_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4188 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4189{
4190 u32 response_info;
4191 u8 *response_bytes;
4192 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4193 MPI2_IOCSTATUS_MASK;
4194 u8 scsi_state = mpi_reply->SCSIState;
4195 u8 scsi_status = mpi_reply->SCSIStatus;
4196 char *desc_ioc_state = NULL;
4197 char *desc_scsi_status = NULL;
4198 char *desc_scsi_state = ioc->tmp_string;
4199 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4200 struct _sas_device *sas_device = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304201 struct scsi_target *starget = scmd->device->sdev_target;
4202 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
4203 char *device_str = NULL;
4204
4205 if (!priv_target)
4206 return;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304207 if (ioc->hide_ir_msg)
4208 device_str = "WarpDrive";
4209 else
4210 device_str = "volume";
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304211
4212 if (log_info == 0x31170000)
4213 return;
4214
4215 switch (ioc_status) {
4216 case MPI2_IOCSTATUS_SUCCESS:
4217 desc_ioc_state = "success";
4218 break;
4219 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4220 desc_ioc_state = "invalid function";
4221 break;
4222 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4223 desc_ioc_state = "scsi recovered error";
4224 break;
4225 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4226 desc_ioc_state = "scsi invalid dev handle";
4227 break;
4228 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4229 desc_ioc_state = "scsi device not there";
4230 break;
4231 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4232 desc_ioc_state = "scsi data overrun";
4233 break;
4234 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4235 desc_ioc_state = "scsi data underrun";
4236 break;
4237 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4238 desc_ioc_state = "scsi io data error";
4239 break;
4240 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4241 desc_ioc_state = "scsi protocol error";
4242 break;
4243 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4244 desc_ioc_state = "scsi task terminated";
4245 break;
4246 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4247 desc_ioc_state = "scsi residual mismatch";
4248 break;
4249 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4250 desc_ioc_state = "scsi task mgmt failed";
4251 break;
4252 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4253 desc_ioc_state = "scsi ioc terminated";
4254 break;
4255 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4256 desc_ioc_state = "scsi ext terminated";
4257 break;
4258 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4259 desc_ioc_state = "eedp guard error";
4260 break;
4261 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4262 desc_ioc_state = "eedp ref tag error";
4263 break;
4264 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4265 desc_ioc_state = "eedp app tag error";
4266 break;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05304267 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
4268 desc_ioc_state = "insufficient power";
4269 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304270 default:
4271 desc_ioc_state = "unknown";
4272 break;
4273 }
4274
4275 switch (scsi_status) {
4276 case MPI2_SCSI_STATUS_GOOD:
4277 desc_scsi_status = "good";
4278 break;
4279 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4280 desc_scsi_status = "check condition";
4281 break;
4282 case MPI2_SCSI_STATUS_CONDITION_MET:
4283 desc_scsi_status = "condition met";
4284 break;
4285 case MPI2_SCSI_STATUS_BUSY:
4286 desc_scsi_status = "busy";
4287 break;
4288 case MPI2_SCSI_STATUS_INTERMEDIATE:
4289 desc_scsi_status = "intermediate";
4290 break;
4291 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4292 desc_scsi_status = "intermediate condmet";
4293 break;
4294 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4295 desc_scsi_status = "reservation conflict";
4296 break;
4297 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4298 desc_scsi_status = "command terminated";
4299 break;
4300 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4301 desc_scsi_status = "task set full";
4302 break;
4303 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4304 desc_scsi_status = "aca active";
4305 break;
4306 case MPI2_SCSI_STATUS_TASK_ABORTED:
4307 desc_scsi_status = "task aborted";
4308 break;
4309 default:
4310 desc_scsi_status = "unknown";
4311 break;
4312 }
4313
4314 desc_scsi_state[0] = '\0';
4315 if (!scsi_state)
4316 desc_scsi_state = " ";
4317 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4318 strcat(desc_scsi_state, "response info ");
4319 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4320 strcat(desc_scsi_state, "state terminated ");
4321 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4322 strcat(desc_scsi_state, "no status ");
4323 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4324 strcat(desc_scsi_state, "autosense failed ");
4325 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4326 strcat(desc_scsi_state, "autosense valid ");
4327
4328 scsi_print_command(scmd);
4329
4330 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
4331 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4332 device_str, (unsigned long long)priv_target->sas_address);
4333 } else {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304334 sas_device = mpt3sas_get_sdev_from_target(ioc, priv_target);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304335 if (sas_device) {
4336 pr_warn(MPT3SAS_FMT
4337 "\tsas_address(0x%016llx), phy(%d)\n",
4338 ioc->name, (unsigned long long)
4339 sas_device->sas_address, sas_device->phy);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304340 if (sas_device->enclosure_handle != 0)
4341 pr_warn(MPT3SAS_FMT
4342 "\tenclosure_logical_id(0x%016llx),"
4343 "slot(%d)\n", ioc->name,
4344 (unsigned long long)
4345 sas_device->enclosure_logical_id,
4346 sas_device->slot);
4347 if (sas_device->connector_name[0])
4348 pr_warn(MPT3SAS_FMT
4349 "\tenclosure level(0x%04x),"
4350 " connector name( %s)\n", ioc->name,
4351 sas_device->enclosure_level,
4352 sas_device->connector_name);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304353
4354 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304355 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304356 }
4357
4358 pr_warn(MPT3SAS_FMT
4359 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
4360 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4361 desc_ioc_state, ioc_status, smid);
4362 pr_warn(MPT3SAS_FMT
4363 "\trequest_len(%d), underflow(%d), resid(%d)\n",
4364 ioc->name, scsi_bufflen(scmd), scmd->underflow,
4365 scsi_get_resid(scmd));
4366 pr_warn(MPT3SAS_FMT
4367 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
4368 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4369 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4370 pr_warn(MPT3SAS_FMT
4371 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
4372 ioc->name, desc_scsi_status,
4373 scsi_status, desc_scsi_state, scsi_state);
4374
4375 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4376 struct sense_info data;
4377 _scsih_normalize_sense(scmd->sense_buffer, &data);
4378 pr_warn(MPT3SAS_FMT
4379 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
4380 ioc->name, data.skey,
4381 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
4382 }
4383
4384 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4385 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4386 response_bytes = (u8 *)&response_info;
4387 _scsih_response_code(ioc, response_bytes[0]);
4388 }
4389}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304390
4391/**
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304392 * _scsih_turn_on_pfa_led - illuminate PFA LED
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304393 * @ioc: per adapter object
4394 * @handle: device handle
4395 * Context: process
4396 *
4397 * Return nothing.
4398 */
4399static void
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304400_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304401{
4402 Mpi2SepReply_t mpi_reply;
4403 Mpi2SepRequest_t mpi_request;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304404 struct _sas_device *sas_device;
4405
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304406 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304407 if (!sas_device)
4408 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304409
4410 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4411 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4412 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4413 mpi_request.SlotStatus =
4414 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4415 mpi_request.DevHandle = cpu_to_le16(handle);
4416 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4417 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4418 &mpi_request)) != 0) {
4419 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4420 __FILE__, __LINE__, __func__);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304421 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304422 }
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304423 sas_device->pfa_led_on = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304424
4425 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4426 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4427 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4428 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4429 le32_to_cpu(mpi_reply.IOCLogInfo)));
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304430 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304431 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304432out:
4433 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304434}
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304435
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304436/**
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304437 * _scsih_turn_off_pfa_led - turn off Fault LED
4438 * @ioc: per adapter object
4439 * @sas_device: sas device whose PFA LED has to turned off
4440 * Context: process
4441 *
4442 * Return nothing.
4443 */
4444static void
4445_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4446 struct _sas_device *sas_device)
4447{
4448 Mpi2SepReply_t mpi_reply;
4449 Mpi2SepRequest_t mpi_request;
4450
4451 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4452 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4453 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4454 mpi_request.SlotStatus = 0;
4455 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4456 mpi_request.DevHandle = 0;
4457 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4458 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4459 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4460 &mpi_request)) != 0) {
4461 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4462 __FILE__, __LINE__, __func__);
4463 return;
4464 }
4465
4466 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4467 dewtprintk(ioc, printk(MPT3SAS_FMT
4468 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4469 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4470 le32_to_cpu(mpi_reply.IOCLogInfo)));
4471 return;
4472 }
4473}
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304474
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304475/**
4476 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304477 * @ioc: per adapter object
4478 * @handle: device handle
4479 * Context: interrupt.
4480 *
4481 * Return nothing.
4482 */
4483static void
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304484_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304485{
4486 struct fw_event_work *fw_event;
4487
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05304488 fw_event = alloc_fw_event_work(0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304489 if (!fw_event)
4490 return;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304491 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304492 fw_event->device_handle = handle;
4493 fw_event->ioc = ioc;
4494 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05304495 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304496}
4497
4498/**
4499 * _scsih_smart_predicted_fault - process smart errors
4500 * @ioc: per adapter object
4501 * @handle: device handle
4502 * Context: interrupt.
4503 *
4504 * Return nothing.
4505 */
4506static void
4507_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4508{
4509 struct scsi_target *starget;
4510 struct MPT3SAS_TARGET *sas_target_priv_data;
4511 Mpi2EventNotificationReply_t *event_reply;
4512 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4513 struct _sas_device *sas_device;
4514 ssize_t sz;
4515 unsigned long flags;
4516
4517 /* only handle non-raid devices */
4518 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304519 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
4520 if (!sas_device)
4521 goto out_unlock;
4522
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304523 starget = sas_device->starget;
4524 sas_target_priv_data = starget->hostdata;
4525
4526 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304527 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)))
4528 goto out_unlock;
4529
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304530 if (sas_device->enclosure_handle != 0)
4531 starget_printk(KERN_INFO, starget, "predicted fault, "
4532 "enclosure logical id(0x%016llx), slot(%d)\n",
4533 (unsigned long long)sas_device->enclosure_logical_id,
4534 sas_device->slot);
4535 if (sas_device->connector_name[0] != '\0')
4536 starget_printk(KERN_WARNING, starget, "predicted fault, "
4537 "enclosure level(0x%04x), connector name( %s)\n",
4538 sas_device->enclosure_level,
4539 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304540 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4541
4542 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304543 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304544
4545 /* insert into event log */
4546 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4547 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4548 event_reply = kzalloc(sz, GFP_KERNEL);
4549 if (!event_reply) {
4550 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4551 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304552 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304553 }
4554
4555 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4556 event_reply->Event =
4557 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4558 event_reply->MsgLength = sz/4;
4559 event_reply->EventDataLength =
4560 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4561 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4562 event_reply->EventData;
4563 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4564 event_data->ASC = 0x5D;
4565 event_data->DevHandle = cpu_to_le16(handle);
4566 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4567 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4568 kfree(event_reply);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05304569out:
4570 if (sas_device)
4571 sas_device_put(sas_device);
4572 return;
4573
4574out_unlock:
4575 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4576 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304577}
4578
4579/**
4580 * _scsih_io_done - scsi request callback
4581 * @ioc: per adapter object
4582 * @smid: system request message index
4583 * @msix_index: MSIX table index supplied by the OS
4584 * @reply: reply message frame(lower 32bit addr)
4585 *
4586 * Callback handler when using _scsih_qcmd.
4587 *
4588 * Return 1 meaning mf should be freed from _base_interrupt
4589 * 0 means the mf is freed from this function.
4590 */
4591static u8
4592_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4593{
4594 Mpi2SCSIIORequest_t *mpi_request;
4595 Mpi2SCSIIOReply_t *mpi_reply;
4596 struct scsi_cmnd *scmd;
4597 u16 ioc_status;
4598 u32 xfer_cnt;
4599 u8 scsi_state;
4600 u8 scsi_status;
4601 u32 log_info;
4602 struct MPT3SAS_DEVICE *sas_device_priv_data;
4603 u32 response_code = 0;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304604 unsigned long flags;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304605
4606 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4607 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4608 if (scmd == NULL)
4609 return 1;
4610
4611 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4612
4613 if (mpi_reply == NULL) {
4614 scmd->result = DID_OK << 16;
4615 goto out;
4616 }
4617
4618 sas_device_priv_data = scmd->device->hostdata;
4619 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4620 sas_device_priv_data->sas_target->deleted) {
4621 scmd->result = DID_NO_CONNECT << 16;
4622 goto out;
4623 }
4624 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4625
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304626 /*
4627 * WARPDRIVE: If direct_io is set then it is directIO,
4628 * the failed direct I/O should be redirected to volume
4629 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304630 if (mpt3sas_scsi_direct_io_get(ioc, smid) &&
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304631 ((ioc_status & MPI2_IOCSTATUS_MASK)
4632 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
4633 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4634 ioc->scsi_lookup[smid - 1].scmd = scmd;
4635 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304636 mpt3sas_scsi_direct_io_set(ioc, smid, 0);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304637 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4638 mpi_request->DevHandle =
4639 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4640 mpt3sas_base_put_smid_scsi_io(ioc, smid,
4641 sas_device_priv_data->sas_target->handle);
4642 return 0;
4643 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304644 /* turning off TLR */
4645 scsi_state = mpi_reply->SCSIState;
4646 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4647 response_code =
4648 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4649 if (!sas_device_priv_data->tlr_snoop_check) {
4650 sas_device_priv_data->tlr_snoop_check++;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05304651 if (!ioc->is_warpdrive &&
4652 !scsih_is_raid(&scmd->device->sdev_gendev) &&
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05304653 sas_is_tlr_enabled(scmd->device) &&
4654 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4655 sas_disable_tlr(scmd->device);
4656 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4657 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304658 }
4659
4660 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4661 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4662 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4663 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4664 else
4665 log_info = 0;
4666 ioc_status &= MPI2_IOCSTATUS_MASK;
4667 scsi_status = mpi_reply->SCSIStatus;
4668
4669 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4670 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4671 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4672 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4673 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4674 }
4675
4676 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4677 struct sense_info data;
4678 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4679 smid);
4680 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4681 le32_to_cpu(mpi_reply->SenseCount));
4682 memcpy(scmd->sense_buffer, sense_data, sz);
4683 _scsih_normalize_sense(scmd->sense_buffer, &data);
4684 /* failure prediction threshold exceeded */
4685 if (data.asc == 0x5D)
4686 _scsih_smart_predicted_fault(ioc,
4687 le16_to_cpu(mpi_reply->DevHandle));
4688 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304689
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304690 if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4691 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4692 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4693 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4694 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304695 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304696 switch (ioc_status) {
4697 case MPI2_IOCSTATUS_BUSY:
4698 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4699 scmd->result = SAM_STAT_BUSY;
4700 break;
4701
4702 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4703 scmd->result = DID_NO_CONNECT << 16;
4704 break;
4705
4706 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4707 if (sas_device_priv_data->block) {
4708 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4709 goto out;
4710 }
4711 if (log_info == 0x31110630) {
4712 if (scmd->retries > 2) {
4713 scmd->result = DID_NO_CONNECT << 16;
4714 scsi_device_set_state(scmd->device,
4715 SDEV_OFFLINE);
4716 } else {
4717 scmd->result = DID_SOFT_ERROR << 16;
4718 scmd->device->expecting_cc_ua = 1;
4719 }
4720 break;
Sreekanth Reddy3898f082015-06-30 12:25:00 +05304721 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4722 scmd->result = DID_RESET << 16;
4723 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304724 }
4725 scmd->result = DID_SOFT_ERROR << 16;
4726 break;
4727 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4728 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4729 scmd->result = DID_RESET << 16;
4730 break;
4731
4732 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4733 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4734 scmd->result = DID_SOFT_ERROR << 16;
4735 else
4736 scmd->result = (DID_OK << 16) | scsi_status;
4737 break;
4738
4739 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4740 scmd->result = (DID_OK << 16) | scsi_status;
4741
4742 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4743 break;
4744
4745 if (xfer_cnt < scmd->underflow) {
4746 if (scsi_status == SAM_STAT_BUSY)
4747 scmd->result = SAM_STAT_BUSY;
4748 else
4749 scmd->result = DID_SOFT_ERROR << 16;
4750 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4751 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4752 scmd->result = DID_SOFT_ERROR << 16;
4753 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4754 scmd->result = DID_RESET << 16;
4755 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4756 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4757 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4758 scmd->result = (DRIVER_SENSE << 24) |
4759 SAM_STAT_CHECK_CONDITION;
4760 scmd->sense_buffer[0] = 0x70;
4761 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4762 scmd->sense_buffer[12] = 0x20;
4763 scmd->sense_buffer[13] = 0;
4764 }
4765 break;
4766
4767 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4768 scsi_set_resid(scmd, 0);
4769 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4770 case MPI2_IOCSTATUS_SUCCESS:
4771 scmd->result = (DID_OK << 16) | scsi_status;
4772 if (response_code ==
4773 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4774 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4775 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4776 scmd->result = DID_SOFT_ERROR << 16;
4777 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4778 scmd->result = DID_RESET << 16;
4779 break;
4780
4781 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4782 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4783 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4784 _scsih_eedp_error_handling(scmd, ioc_status);
4785 break;
4786
4787 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4788 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4789 case MPI2_IOCSTATUS_INVALID_SGL:
4790 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4791 case MPI2_IOCSTATUS_INVALID_FIELD:
4792 case MPI2_IOCSTATUS_INVALID_STATE:
4793 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4794 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05304795 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304796 default:
4797 scmd->result = DID_SOFT_ERROR << 16;
4798 break;
4799
4800 }
4801
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304802 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4803 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304804
4805 out:
4806
4807 scsi_dma_unmap(scmd);
4808
4809 scmd->scsi_done(scmd);
4810 return 1;
4811}
4812
4813/**
4814 * _scsih_sas_host_refresh - refreshing sas host object contents
4815 * @ioc: per adapter object
4816 * Context: user
4817 *
4818 * During port enable, fw will send topology events for every device. Its
4819 * possible that the handles may change from the previous setting, so this
4820 * code keeping handles updating if changed.
4821 *
4822 * Return nothing.
4823 */
4824static void
4825_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4826{
4827 u16 sz;
4828 u16 ioc_status;
4829 int i;
4830 Mpi2ConfigReply_t mpi_reply;
4831 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4832 u16 attached_handle;
4833 u8 link_rate;
4834
4835 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4836 "updating handles for sas_host(0x%016llx)\n",
4837 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4838
4839 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4840 * sizeof(Mpi2SasIOUnit0PhyData_t));
4841 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4842 if (!sas_iounit_pg0) {
4843 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4844 ioc->name, __FILE__, __LINE__, __func__);
4845 return;
4846 }
4847
4848 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4849 sas_iounit_pg0, sz)) != 0)
4850 goto out;
4851 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4852 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4853 goto out;
4854 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4855 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4856 if (i == 0)
4857 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4858 PhyData[0].ControllerDevHandle);
4859 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4860 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4861 AttachedDevHandle);
4862 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4863 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4864 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4865 attached_handle, i, link_rate);
4866 }
4867 out:
4868 kfree(sas_iounit_pg0);
4869}
4870
4871/**
4872 * _scsih_sas_host_add - create sas host object
4873 * @ioc: per adapter object
4874 *
4875 * Creating host side data object, stored in ioc->sas_hba
4876 *
4877 * Return nothing.
4878 */
4879static void
4880_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4881{
4882 int i;
4883 Mpi2ConfigReply_t mpi_reply;
4884 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4885 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4886 Mpi2SasPhyPage0_t phy_pg0;
4887 Mpi2SasDevicePage0_t sas_device_pg0;
4888 Mpi2SasEnclosurePage0_t enclosure_pg0;
4889 u16 ioc_status;
4890 u16 sz;
4891 u8 device_missing_delay;
4892
4893 mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4894 if (!ioc->sas_hba.num_phys) {
4895 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4896 ioc->name, __FILE__, __LINE__, __func__);
4897 return;
4898 }
4899
4900 /* sas_iounit page 0 */
4901 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4902 sizeof(Mpi2SasIOUnit0PhyData_t));
4903 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4904 if (!sas_iounit_pg0) {
4905 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4906 ioc->name, __FILE__, __LINE__, __func__);
4907 return;
4908 }
4909 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4910 sas_iounit_pg0, sz))) {
4911 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4912 ioc->name, __FILE__, __LINE__, __func__);
4913 goto out;
4914 }
4915 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4916 MPI2_IOCSTATUS_MASK;
4917 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4918 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4919 ioc->name, __FILE__, __LINE__, __func__);
4920 goto out;
4921 }
4922
4923 /* sas_iounit page 1 */
4924 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4925 sizeof(Mpi2SasIOUnit1PhyData_t));
4926 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4927 if (!sas_iounit_pg1) {
4928 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4929 ioc->name, __FILE__, __LINE__, __func__);
4930 goto out;
4931 }
4932 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4933 sas_iounit_pg1, sz))) {
4934 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4935 ioc->name, __FILE__, __LINE__, __func__);
4936 goto out;
4937 }
4938 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4939 MPI2_IOCSTATUS_MASK;
4940 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4941 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4942 ioc->name, __FILE__, __LINE__, __func__);
4943 goto out;
4944 }
4945
4946 ioc->io_missing_delay =
4947 sas_iounit_pg1->IODeviceMissingDelay;
4948 device_missing_delay =
4949 sas_iounit_pg1->ReportDeviceMissingDelay;
4950 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4951 ioc->device_missing_delay = (device_missing_delay &
4952 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4953 else
4954 ioc->device_missing_delay = device_missing_delay &
4955 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4956
4957 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4958 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4959 sizeof(struct _sas_phy), GFP_KERNEL);
4960 if (!ioc->sas_hba.phy) {
4961 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4962 ioc->name, __FILE__, __LINE__, __func__);
4963 goto out;
4964 }
4965 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4966 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4967 i))) {
4968 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4969 ioc->name, __FILE__, __LINE__, __func__);
4970 goto out;
4971 }
4972 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4973 MPI2_IOCSTATUS_MASK;
4974 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4975 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4976 ioc->name, __FILE__, __LINE__, __func__);
4977 goto out;
4978 }
4979
4980 if (i == 0)
4981 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4982 PhyData[0].ControllerDevHandle);
4983 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4984 ioc->sas_hba.phy[i].phy_id = i;
4985 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4986 phy_pg0, ioc->sas_hba.parent_dev);
4987 }
4988 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4989 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4990 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4991 ioc->name, __FILE__, __LINE__, __func__);
4992 goto out;
4993 }
4994 ioc->sas_hba.enclosure_handle =
4995 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4996 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4997 pr_info(MPT3SAS_FMT
4998 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4999 ioc->name, ioc->sas_hba.handle,
5000 (unsigned long long) ioc->sas_hba.sas_address,
5001 ioc->sas_hba.num_phys) ;
5002
5003 if (ioc->sas_hba.enclosure_handle) {
5004 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5005 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5006 ioc->sas_hba.enclosure_handle)))
5007 ioc->sas_hba.enclosure_logical_id =
5008 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5009 }
5010
5011 out:
5012 kfree(sas_iounit_pg1);
5013 kfree(sas_iounit_pg0);
5014}
5015
5016/**
5017 * _scsih_expander_add - creating expander object
5018 * @ioc: per adapter object
5019 * @handle: expander handle
5020 *
5021 * Creating expander object, stored in ioc->sas_expander_list.
5022 *
5023 * Return 0 for success, else error.
5024 */
5025static int
5026_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5027{
5028 struct _sas_node *sas_expander;
5029 Mpi2ConfigReply_t mpi_reply;
5030 Mpi2ExpanderPage0_t expander_pg0;
5031 Mpi2ExpanderPage1_t expander_pg1;
5032 Mpi2SasEnclosurePage0_t enclosure_pg0;
5033 u32 ioc_status;
5034 u16 parent_handle;
5035 u64 sas_address, sas_address_parent = 0;
5036 int i;
5037 unsigned long flags;
5038 struct _sas_port *mpt3sas_port = NULL;
5039
5040 int rc = 0;
5041
5042 if (!handle)
5043 return -1;
5044
5045 if (ioc->shost_recovery || ioc->pci_error_recovery)
5046 return -1;
5047
5048 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
5049 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
5050 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5051 ioc->name, __FILE__, __LINE__, __func__);
5052 return -1;
5053 }
5054
5055 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5056 MPI2_IOCSTATUS_MASK;
5057 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5058 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5059 ioc->name, __FILE__, __LINE__, __func__);
5060 return -1;
5061 }
5062
5063 /* handle out of order topology events */
5064 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
5065 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
5066 != 0) {
5067 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5068 ioc->name, __FILE__, __LINE__, __func__);
5069 return -1;
5070 }
5071 if (sas_address_parent != ioc->sas_hba.sas_address) {
5072 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5073 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5074 sas_address_parent);
5075 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5076 if (!sas_expander) {
5077 rc = _scsih_expander_add(ioc, parent_handle);
5078 if (rc != 0)
5079 return rc;
5080 }
5081 }
5082
5083 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5084 sas_address = le64_to_cpu(expander_pg0.SASAddress);
5085 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5086 sas_address);
5087 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5088
5089 if (sas_expander)
5090 return 0;
5091
5092 sas_expander = kzalloc(sizeof(struct _sas_node),
5093 GFP_KERNEL);
5094 if (!sas_expander) {
5095 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5096 ioc->name, __FILE__, __LINE__, __func__);
5097 return -1;
5098 }
5099
5100 sas_expander->handle = handle;
5101 sas_expander->num_phys = expander_pg0.NumPhys;
5102 sas_expander->sas_address_parent = sas_address_parent;
5103 sas_expander->sas_address = sas_address;
5104
5105 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
5106 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
5107 handle, parent_handle, (unsigned long long)
5108 sas_expander->sas_address, sas_expander->num_phys);
5109
5110 if (!sas_expander->num_phys)
5111 goto out_fail;
5112 sas_expander->phy = kcalloc(sas_expander->num_phys,
5113 sizeof(struct _sas_phy), GFP_KERNEL);
5114 if (!sas_expander->phy) {
5115 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5116 ioc->name, __FILE__, __LINE__, __func__);
5117 rc = -1;
5118 goto out_fail;
5119 }
5120
5121 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5122 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
5123 sas_address_parent);
5124 if (!mpt3sas_port) {
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 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
5131
5132 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5133 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
5134 &expander_pg1, i, handle))) {
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->phy[i].handle = handle;
5141 sas_expander->phy[i].phy_id = i;
5142
5143 if ((mpt3sas_transport_add_expander_phy(ioc,
5144 &sas_expander->phy[i], expander_pg1,
5145 sas_expander->parent_dev))) {
5146 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5147 ioc->name, __FILE__, __LINE__, __func__);
5148 rc = -1;
5149 goto out_fail;
5150 }
5151 }
5152
5153 if (sas_expander->enclosure_handle) {
5154 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5155 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5156 sas_expander->enclosure_handle)))
5157 sas_expander->enclosure_logical_id =
5158 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5159 }
5160
5161 _scsih_expander_node_add(ioc, sas_expander);
5162 return 0;
5163
5164 out_fail:
5165
5166 if (mpt3sas_port)
5167 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
5168 sas_address_parent);
5169 kfree(sas_expander);
5170 return rc;
5171}
5172
5173/**
5174 * mpt3sas_expander_remove - removing expander object
5175 * @ioc: per adapter object
5176 * @sas_address: expander sas_address
5177 *
5178 * Return nothing.
5179 */
5180void
5181mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
5182{
5183 struct _sas_node *sas_expander;
5184 unsigned long flags;
5185
5186 if (ioc->shost_recovery)
5187 return;
5188
5189 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5190 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
5191 sas_address);
5192 if (sas_expander)
5193 list_del(&sas_expander->list);
5194 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5195 if (sas_expander)
5196 _scsih_expander_node_remove(ioc, sas_expander);
5197}
5198
5199/**
5200 * _scsih_done - internal SCSI_IO callback handler.
5201 * @ioc: per adapter object
5202 * @smid: system request message index
5203 * @msix_index: MSIX table index supplied by the OS
5204 * @reply: reply message frame(lower 32bit addr)
5205 *
5206 * Callback handler when sending internal generated SCSI_IO.
5207 * The callback index passed is `ioc->scsih_cb_idx`
5208 *
5209 * Return 1 meaning mf should be freed from _base_interrupt
5210 * 0 means the mf is freed from this function.
5211 */
5212static u8
5213_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5214{
5215 MPI2DefaultReply_t *mpi_reply;
5216
5217 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
5218 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
5219 return 1;
5220 if (ioc->scsih_cmds.smid != smid)
5221 return 1;
5222 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
5223 if (mpi_reply) {
5224 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5225 mpi_reply->MsgLength*4);
5226 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
5227 }
5228 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
5229 complete(&ioc->scsih_cmds.done);
5230 return 1;
5231}
5232
5233
5234
5235
5236#define MPT3_MAX_LUNS (255)
5237
5238
5239/**
5240 * _scsih_check_access_status - check access flags
5241 * @ioc: per adapter object
5242 * @sas_address: sas address
5243 * @handle: sas device handle
5244 * @access_flags: errors returned during discovery of the device
5245 *
5246 * Return 0 for success, else failure
5247 */
5248static u8
5249_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
5250 u16 handle, u8 access_status)
5251{
5252 u8 rc = 1;
5253 char *desc = NULL;
5254
5255 switch (access_status) {
5256 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5257 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5258 rc = 0;
5259 break;
5260 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5261 desc = "sata capability failed";
5262 break;
5263 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5264 desc = "sata affiliation conflict";
5265 break;
5266 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5267 desc = "route not addressable";
5268 break;
5269 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5270 desc = "smp error not addressable";
5271 break;
5272 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5273 desc = "device blocked";
5274 break;
5275 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5276 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5277 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5278 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5279 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5280 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5281 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5282 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5283 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5284 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5285 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5286 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5287 desc = "sata initialization failed";
5288 break;
5289 default:
5290 desc = "unknown";
5291 break;
5292 }
5293
5294 if (!rc)
5295 return 0;
5296
5297 pr_err(MPT3SAS_FMT
5298 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
5299 ioc->name, desc, (unsigned long long)sas_address, handle);
5300 return rc;
5301}
5302
5303/**
5304 * _scsih_check_device - checking device responsiveness
5305 * @ioc: per adapter object
5306 * @parent_sas_address: sas address of parent expander or sas host
5307 * @handle: attached device handle
5308 * @phy_numberv: phy number
5309 * @link_rate: new link rate
5310 *
5311 * Returns nothing.
5312 */
5313static void
5314_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
5315 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
5316{
5317 Mpi2ConfigReply_t mpi_reply;
5318 Mpi2SasDevicePage0_t sas_device_pg0;
5319 struct _sas_device *sas_device;
5320 u32 ioc_status;
5321 unsigned long flags;
5322 u64 sas_address;
5323 struct scsi_target *starget;
5324 struct MPT3SAS_TARGET *sas_target_priv_data;
5325 u32 device_info;
5326
5327
5328 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5329 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5330 return;
5331
5332 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5333 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5334 return;
5335
5336 /* wide port handling ~ we need only handle device once for the phy that
5337 * is matched in sas device page zero
5338 */
5339 if (phy_number != sas_device_pg0.PhyNum)
5340 return;
5341
5342 /* check if this is end device */
5343 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5344 if (!(_scsih_is_end_device(device_info)))
5345 return;
5346
5347 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5348 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305349 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305350 sas_address);
5351
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305352 if (!sas_device)
5353 goto out_unlock;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305354
5355 if (unlikely(sas_device->handle != handle)) {
5356 starget = sas_device->starget;
5357 sas_target_priv_data = starget->hostdata;
5358 starget_printk(KERN_INFO, starget,
5359 "handle changed from(0x%04x) to (0x%04x)!!!\n",
5360 sas_device->handle, handle);
5361 sas_target_priv_data->handle = handle;
5362 sas_device->handle = handle;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305363 if (sas_device_pg0.Flags &
5364 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5365 sas_device->enclosure_level =
5366 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5367 memcpy(&sas_device->connector_name[0],
5368 &sas_device_pg0.ConnectorName[0], 4);
5369 } else {
5370 sas_device->enclosure_level = 0;
5371 sas_device->connector_name[0] = '\0';
5372 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305373 }
5374
5375 /* check if device is present */
5376 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5377 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5378 pr_err(MPT3SAS_FMT
5379 "device is not present handle(0x%04x), flags!!!\n",
5380 ioc->name, handle);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305381 goto out_unlock;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305382 }
5383
5384 /* check if there were any issues with discovery */
5385 if (_scsih_check_access_status(ioc, sas_address, handle,
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305386 sas_device_pg0.AccessStatus))
5387 goto out_unlock;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305388
5389 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5390 _scsih_ublock_io_device(ioc, sas_address);
5391
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305392 if (sas_device)
5393 sas_device_put(sas_device);
5394 return;
5395
5396out_unlock:
5397 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5398 if (sas_device)
5399 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305400}
5401
5402/**
5403 * _scsih_add_device - creating sas device object
5404 * @ioc: per adapter object
5405 * @handle: sas device handle
5406 * @phy_num: phy number end device attached to
5407 * @is_pd: is this hidden raid component
5408 *
5409 * Creating end device object, stored in ioc->sas_device_list.
5410 *
5411 * Returns 0 for success, non-zero for failure.
5412 */
5413static int
5414_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
5415 u8 is_pd)
5416{
5417 Mpi2ConfigReply_t mpi_reply;
5418 Mpi2SasDevicePage0_t sas_device_pg0;
5419 Mpi2SasEnclosurePage0_t enclosure_pg0;
5420 struct _sas_device *sas_device;
5421 u32 ioc_status;
5422 u64 sas_address;
5423 u32 device_info;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305424
5425 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5426 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5427 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5428 ioc->name, __FILE__, __LINE__, __func__);
5429 return -1;
5430 }
5431
5432 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5433 MPI2_IOCSTATUS_MASK;
5434 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5435 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5436 ioc->name, __FILE__, __LINE__, __func__);
5437 return -1;
5438 }
5439
5440 /* check if this is end device */
5441 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5442 if (!(_scsih_is_end_device(device_info)))
5443 return -1;
5444 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5445
5446 /* check if device is present */
5447 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5448 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5449 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5450 ioc->name, handle);
5451 return -1;
5452 }
5453
5454 /* check if there were any issues with discovery */
5455 if (_scsih_check_access_status(ioc, sas_address, handle,
5456 sas_device_pg0.AccessStatus))
5457 return -1;
5458
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305459 sas_device = mpt3sas_get_sdev_by_addr(ioc,
5460 sas_address);
5461 if (sas_device) {
5462 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305463 return -1;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305464 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305465
5466 sas_device = kzalloc(sizeof(struct _sas_device),
5467 GFP_KERNEL);
5468 if (!sas_device) {
5469 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5470 ioc->name, __FILE__, __LINE__, __func__);
5471 return 0;
5472 }
5473
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305474 kref_init(&sas_device->refcount);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305475 sas_device->handle = handle;
5476 if (_scsih_get_sas_address(ioc,
5477 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5478 &sas_device->sas_address_parent) != 0)
5479 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5480 ioc->name, __FILE__, __LINE__, __func__);
5481 sas_device->enclosure_handle =
5482 le16_to_cpu(sas_device_pg0.EnclosureHandle);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305483 if (sas_device->enclosure_handle != 0)
5484 sas_device->slot =
5485 le16_to_cpu(sas_device_pg0.Slot);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305486 sas_device->device_info = device_info;
5487 sas_device->sas_address = sas_address;
5488 sas_device->phy = sas_device_pg0.PhyNum;
5489 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5490 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5491
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305492 if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5493 sas_device->enclosure_level =
5494 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5495 memcpy(&sas_device->connector_name[0],
5496 &sas_device_pg0.ConnectorName[0], 4);
5497 } else {
5498 sas_device->enclosure_level = 0;
5499 sas_device->connector_name[0] = '\0';
5500 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305501 /* get enclosure_logical_id */
5502 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5503 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5504 sas_device->enclosure_handle)))
5505 sas_device->enclosure_logical_id =
5506 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5507
5508 /* get device name */
5509 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5510
5511 if (ioc->wait_for_discovery_to_complete)
5512 _scsih_sas_device_init_add(ioc, sas_device);
5513 else
5514 _scsih_sas_device_add(ioc, sas_device);
5515
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305516 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305517 return 0;
5518}
5519
5520/**
5521 * _scsih_remove_device - removing sas device object
5522 * @ioc: per adapter object
5523 * @sas_device_delete: the sas_device object
5524 *
5525 * Return nothing.
5526 */
5527static void
5528_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5529 struct _sas_device *sas_device)
5530{
5531 struct MPT3SAS_TARGET *sas_target_priv_data;
5532
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05305533 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5534 (sas_device->pfa_led_on)) {
5535 _scsih_turn_off_pfa_led(ioc, sas_device);
5536 sas_device->pfa_led_on = 0;
5537 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305538 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5539 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5540 ioc->name, __func__,
5541 sas_device->handle, (unsigned long long)
5542 sas_device->sas_address));
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305543 if (sas_device->enclosure_handle != 0)
5544 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5545 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5546 ioc->name, __func__,
5547 (unsigned long long)sas_device->enclosure_logical_id,
5548 sas_device->slot));
5549 if (sas_device->connector_name[0] != '\0')
5550 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5551 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5552 ioc->name, __func__,
5553 sas_device->enclosure_level,
5554 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305555
5556 if (sas_device->starget && sas_device->starget->hostdata) {
5557 sas_target_priv_data = sas_device->starget->hostdata;
5558 sas_target_priv_data->deleted = 1;
5559 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5560 sas_target_priv_data->handle =
5561 MPT3SAS_INVALID_DEVICE_HANDLE;
5562 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05305563
5564 if (!ioc->hide_drives)
5565 mpt3sas_transport_port_remove(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305566 sas_device->sas_address,
5567 sas_device->sas_address_parent);
5568
5569 pr_info(MPT3SAS_FMT
5570 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5571 ioc->name, sas_device->handle,
5572 (unsigned long long) sas_device->sas_address);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305573 if (sas_device->enclosure_handle != 0)
5574 pr_info(MPT3SAS_FMT
5575 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5576 ioc->name,
5577 (unsigned long long)sas_device->enclosure_logical_id,
5578 sas_device->slot);
5579 if (sas_device->connector_name[0] != '\0')
5580 pr_info(MPT3SAS_FMT
5581 "removing enclosure level(0x%04x), connector name( %s)\n",
5582 ioc->name, sas_device->enclosure_level,
5583 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305584
5585 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5586 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5587 ioc->name, __func__,
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305588 sas_device->handle, (unsigned long long)
5589 sas_device->sas_address));
5590 if (sas_device->enclosure_handle != 0)
5591 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5592 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5593 ioc->name, __func__,
5594 (unsigned long long)sas_device->enclosure_logical_id,
5595 sas_device->slot));
5596 if (sas_device->connector_name[0] != '\0')
5597 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5598 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5599 ioc->name, __func__, sas_device->enclosure_level,
5600 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305601}
5602
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305603/**
5604 * _scsih_sas_topology_change_event_debug - debug for topology event
5605 * @ioc: per adapter object
5606 * @event_data: event data payload
5607 * Context: user.
5608 */
5609static void
5610_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5611 Mpi2EventDataSasTopologyChangeList_t *event_data)
5612{
5613 int i;
5614 u16 handle;
5615 u16 reason_code;
5616 u8 phy_number;
5617 char *status_str = NULL;
5618 u8 link_rate, prev_link_rate;
5619
5620 switch (event_data->ExpStatus) {
5621 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5622 status_str = "add";
5623 break;
5624 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5625 status_str = "remove";
5626 break;
5627 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5628 case 0:
5629 status_str = "responding";
5630 break;
5631 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5632 status_str = "remove delay";
5633 break;
5634 default:
5635 status_str = "unknown status";
5636 break;
5637 }
5638 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5639 ioc->name, status_str);
5640 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5641 "start_phy(%02d), count(%d)\n",
5642 le16_to_cpu(event_data->ExpanderDevHandle),
5643 le16_to_cpu(event_data->EnclosureHandle),
5644 event_data->StartPhyNum, event_data->NumEntries);
5645 for (i = 0; i < event_data->NumEntries; i++) {
5646 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5647 if (!handle)
5648 continue;
5649 phy_number = event_data->StartPhyNum + i;
5650 reason_code = event_data->PHY[i].PhyStatus &
5651 MPI2_EVENT_SAS_TOPO_RC_MASK;
5652 switch (reason_code) {
5653 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5654 status_str = "target add";
5655 break;
5656 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5657 status_str = "target remove";
5658 break;
5659 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5660 status_str = "delay target remove";
5661 break;
5662 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5663 status_str = "link rate change";
5664 break;
5665 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5666 status_str = "target responding";
5667 break;
5668 default:
5669 status_str = "unknown";
5670 break;
5671 }
5672 link_rate = event_data->PHY[i].LinkRate >> 4;
5673 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5674 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5675 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5676 handle, status_str, link_rate, prev_link_rate);
5677
5678 }
5679}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305680
5681/**
5682 * _scsih_sas_topology_change_event - handle topology changes
5683 * @ioc: per adapter object
5684 * @fw_event: The fw_event_work object
5685 * Context: user.
5686 *
5687 */
5688static int
5689_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5690 struct fw_event_work *fw_event)
5691{
5692 int i;
5693 u16 parent_handle, handle;
5694 u16 reason_code;
5695 u8 phy_number, max_phys;
5696 struct _sas_node *sas_expander;
5697 u64 sas_address;
5698 unsigned long flags;
5699 u8 link_rate, prev_link_rate;
Joe Lawrence35b62362014-06-25 17:05:34 -04005700 Mpi2EventDataSasTopologyChangeList_t *event_data =
5701 (Mpi2EventDataSasTopologyChangeList_t *)
5702 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305703
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305704 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5705 _scsih_sas_topology_change_event_debug(ioc, event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305706
5707 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5708 return 0;
5709
5710 if (!ioc->sas_hba.num_phys)
5711 _scsih_sas_host_add(ioc);
5712 else
5713 _scsih_sas_host_refresh(ioc);
5714
5715 if (fw_event->ignore) {
5716 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5717 "ignoring expander event\n", ioc->name));
5718 return 0;
5719 }
5720
5721 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5722
5723 /* handle expander add */
5724 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5725 if (_scsih_expander_add(ioc, parent_handle) != 0)
5726 return 0;
5727
5728 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5729 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5730 parent_handle);
5731 if (sas_expander) {
5732 sas_address = sas_expander->sas_address;
5733 max_phys = sas_expander->num_phys;
5734 } else if (parent_handle < ioc->sas_hba.num_phys) {
5735 sas_address = ioc->sas_hba.sas_address;
5736 max_phys = ioc->sas_hba.num_phys;
5737 } else {
5738 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5739 return 0;
5740 }
5741 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5742
5743 /* handle siblings events */
5744 for (i = 0; i < event_data->NumEntries; i++) {
5745 if (fw_event->ignore) {
5746 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5747 "ignoring expander event\n", ioc->name));
5748 return 0;
5749 }
5750 if (ioc->remove_host || ioc->pci_error_recovery)
5751 return 0;
5752 phy_number = event_data->StartPhyNum + i;
5753 if (phy_number >= max_phys)
5754 continue;
5755 reason_code = event_data->PHY[i].PhyStatus &
5756 MPI2_EVENT_SAS_TOPO_RC_MASK;
5757 if ((event_data->PHY[i].PhyStatus &
5758 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5759 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5760 continue;
5761 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5762 if (!handle)
5763 continue;
5764 link_rate = event_data->PHY[i].LinkRate >> 4;
5765 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5766 switch (reason_code) {
5767 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5768
5769 if (ioc->shost_recovery)
5770 break;
5771
5772 if (link_rate == prev_link_rate)
5773 break;
5774
5775 mpt3sas_transport_update_links(ioc, sas_address,
5776 handle, phy_number, link_rate);
5777
5778 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5779 break;
5780
5781 _scsih_check_device(ioc, sas_address, handle,
5782 phy_number, link_rate);
5783
5784
5785 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5786
5787 if (ioc->shost_recovery)
5788 break;
5789
5790 mpt3sas_transport_update_links(ioc, sas_address,
5791 handle, phy_number, link_rate);
5792
5793 _scsih_add_device(ioc, handle, phy_number, 0);
5794
5795 break;
5796 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5797
5798 _scsih_device_remove_by_handle(ioc, handle);
5799 break;
5800 }
5801 }
5802
5803 /* handle expander removal */
5804 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5805 sas_expander)
5806 mpt3sas_expander_remove(ioc, sas_address);
5807
5808 return 0;
5809}
5810
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305811/**
5812 * _scsih_sas_device_status_change_event_debug - debug for device event
5813 * @event_data: event data payload
5814 * Context: user.
5815 *
5816 * Return nothing.
5817 */
5818static void
5819_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5820 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5821{
5822 char *reason_str = NULL;
5823
5824 switch (event_data->ReasonCode) {
5825 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5826 reason_str = "smart data";
5827 break;
5828 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5829 reason_str = "unsupported device discovered";
5830 break;
5831 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5832 reason_str = "internal device reset";
5833 break;
5834 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5835 reason_str = "internal task abort";
5836 break;
5837 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5838 reason_str = "internal task abort set";
5839 break;
5840 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5841 reason_str = "internal clear task set";
5842 break;
5843 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5844 reason_str = "internal query task";
5845 break;
5846 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5847 reason_str = "sata init failure";
5848 break;
5849 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5850 reason_str = "internal device reset complete";
5851 break;
5852 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5853 reason_str = "internal task abort complete";
5854 break;
5855 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5856 reason_str = "internal async notification";
5857 break;
5858 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5859 reason_str = "expander reduced functionality";
5860 break;
5861 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5862 reason_str = "expander reduced functionality complete";
5863 break;
5864 default:
5865 reason_str = "unknown reason";
5866 break;
5867 }
5868 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5869 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5870 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5871 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5872 le16_to_cpu(event_data->TaskTag));
5873 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5874 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5875 event_data->ASC, event_data->ASCQ);
5876 pr_info("\n");
5877}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305878
5879/**
5880 * _scsih_sas_device_status_change_event - handle device status change
5881 * @ioc: per adapter object
5882 * @fw_event: The fw_event_work object
5883 * Context: user.
5884 *
5885 * Return nothing.
5886 */
5887static void
5888_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5889 struct fw_event_work *fw_event)
5890{
5891 struct MPT3SAS_TARGET *target_priv_data;
5892 struct _sas_device *sas_device;
5893 u64 sas_address;
5894 unsigned long flags;
5895 Mpi2EventDataSasDeviceStatusChange_t *event_data =
Joe Lawrence35b62362014-06-25 17:05:34 -04005896 (Mpi2EventDataSasDeviceStatusChange_t *)
5897 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305898
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305899 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5900 _scsih_sas_device_status_change_event_debug(ioc,
5901 event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305902
5903 /* In MPI Revision K (0xC), the internal device reset complete was
5904 * implemented, so avoid setting tm_busy flag for older firmware.
5905 */
5906 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5907 return;
5908
5909 if (event_data->ReasonCode !=
5910 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5911 event_data->ReasonCode !=
5912 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5913 return;
5914
5915 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5916 sas_address = le64_to_cpu(event_data->SASAddress);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305917 sas_device = __mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305918 sas_address);
5919
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305920 if (!sas_device || !sas_device->starget)
5921 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305922
5923 target_priv_data = sas_device->starget->hostdata;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305924 if (!target_priv_data)
5925 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305926
5927 if (event_data->ReasonCode ==
5928 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5929 target_priv_data->tm_busy = 1;
5930 else
5931 target_priv_data->tm_busy = 0;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305932
5933out:
5934 if (sas_device)
5935 sas_device_put(sas_device);
5936
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305937 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05305938
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305939}
5940
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305941/**
5942 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5943 * event
5944 * @ioc: per adapter object
5945 * @event_data: event data payload
5946 * Context: user.
5947 *
5948 * Return nothing.
5949 */
5950static void
5951_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5952 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5953{
5954 char *reason_str = NULL;
5955
5956 switch (event_data->ReasonCode) {
5957 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5958 reason_str = "enclosure add";
5959 break;
5960 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5961 reason_str = "enclosure remove";
5962 break;
5963 default:
5964 reason_str = "unknown reason";
5965 break;
5966 }
5967
5968 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5969 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5970 " number slots(%d)\n", ioc->name, reason_str,
5971 le16_to_cpu(event_data->EnclosureHandle),
5972 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5973 le16_to_cpu(event_data->StartSlot));
5974}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305975
5976/**
5977 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5978 * @ioc: per adapter object
5979 * @fw_event: The fw_event_work object
5980 * Context: user.
5981 *
5982 * Return nothing.
5983 */
5984static void
5985_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5986 struct fw_event_work *fw_event)
5987{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305988 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5989 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Joe Lawrence35b62362014-06-25 17:05:34 -04005990 (Mpi2EventDataSasEnclDevStatusChange_t *)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305991 fw_event->event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305992}
5993
5994/**
5995 * _scsih_sas_broadcast_primitive_event - handle broadcast events
5996 * @ioc: per adapter object
5997 * @fw_event: The fw_event_work object
5998 * Context: user.
5999 *
6000 * Return nothing.
6001 */
6002static void
6003_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
6004 struct fw_event_work *fw_event)
6005{
6006 struct scsi_cmnd *scmd;
6007 struct scsi_device *sdev;
6008 u16 smid, handle;
6009 u32 lun;
6010 struct MPT3SAS_DEVICE *sas_device_priv_data;
6011 u32 termination_count;
6012 u32 query_count;
6013 Mpi2SCSITaskManagementReply_t *mpi_reply;
Joe Lawrence35b62362014-06-25 17:05:34 -04006014 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
6015 (Mpi2EventDataSasBroadcastPrimitive_t *)
6016 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306017 u16 ioc_status;
6018 unsigned long flags;
6019 int r;
6020 u8 max_retries = 0;
6021 u8 task_abort_retries;
6022
6023 mutex_lock(&ioc->tm_cmds.mutex);
6024 pr_info(MPT3SAS_FMT
6025 "%s: enter: phy number(%d), width(%d)\n",
6026 ioc->name, __func__, event_data->PhyNum,
6027 event_data->PortWidth);
6028
6029 _scsih_block_io_all_device(ioc);
6030
6031 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6032 mpi_reply = ioc->tm_cmds.reply;
6033 broadcast_aen_retry:
6034
6035 /* sanity checks for retrying this loop */
6036 if (max_retries++ == 5) {
6037 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
6038 ioc->name, __func__));
6039 goto out;
6040 } else if (max_retries > 1)
6041 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
6042 ioc->name, __func__, max_retries - 1));
6043
6044 termination_count = 0;
6045 query_count = 0;
6046 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
6047 if (ioc->shost_recovery)
6048 goto out;
6049 scmd = _scsih_scsi_lookup_get(ioc, smid);
6050 if (!scmd)
6051 continue;
6052 sdev = scmd->device;
6053 sas_device_priv_data = sdev->hostdata;
6054 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
6055 continue;
6056 /* skip hidden raid components */
6057 if (sas_device_priv_data->sas_target->flags &
6058 MPT_TARGET_FLAGS_RAID_COMPONENT)
6059 continue;
6060 /* skip volumes */
6061 if (sas_device_priv_data->sas_target->flags &
6062 MPT_TARGET_FLAGS_VOLUME)
6063 continue;
6064
6065 handle = sas_device_priv_data->sas_target->handle;
6066 lun = sas_device_priv_data->lun;
6067 query_count++;
6068
6069 if (ioc->shost_recovery)
6070 goto out;
6071
6072 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6073 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04006074 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306075 TM_MUTEX_OFF);
6076 if (r == FAILED) {
6077 sdev_printk(KERN_WARNING, sdev,
6078 "mpt3sas_scsih_issue_tm: FAILED when sending "
6079 "QUERY_TASK: scmd(%p)\n", scmd);
6080 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6081 goto broadcast_aen_retry;
6082 }
6083 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
6084 & MPI2_IOCSTATUS_MASK;
6085 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6086 sdev_printk(KERN_WARNING, sdev,
6087 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
6088 ioc_status, scmd);
6089 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6090 goto broadcast_aen_retry;
6091 }
6092
6093 /* see if IO is still owned by IOC and target */
6094 if (mpi_reply->ResponseCode ==
6095 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
6096 mpi_reply->ResponseCode ==
6097 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
6098 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6099 continue;
6100 }
6101 task_abort_retries = 0;
6102 tm_retry:
6103 if (task_abort_retries++ == 60) {
6104 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6105 "%s: ABORT_TASK: giving up\n", ioc->name,
6106 __func__));
6107 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6108 goto broadcast_aen_retry;
6109 }
6110
6111 if (ioc->shost_recovery)
6112 goto out_no_lock;
6113
6114 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
6115 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04006116 TM_MUTEX_OFF);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306117 if (r == FAILED) {
6118 sdev_printk(KERN_WARNING, sdev,
6119 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
6120 "scmd(%p)\n", scmd);
6121 goto tm_retry;
6122 }
6123
6124 if (task_abort_retries > 1)
6125 sdev_printk(KERN_WARNING, sdev,
6126 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
6127 " scmd(%p)\n",
6128 task_abort_retries - 1, scmd);
6129
6130 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
6131 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
6132 }
6133
6134 if (ioc->broadcast_aen_pending) {
6135 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6136 "%s: loop back due to pending AEN\n",
6137 ioc->name, __func__));
6138 ioc->broadcast_aen_pending = 0;
6139 goto broadcast_aen_retry;
6140 }
6141
6142 out:
6143 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6144 out_no_lock:
6145
6146 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6147 "%s - exit, query_count = %d termination_count = %d\n",
6148 ioc->name, __func__, query_count, termination_count));
6149
6150 ioc->broadcast_aen_busy = 0;
6151 if (!ioc->shost_recovery)
6152 _scsih_ublock_io_all_device(ioc);
6153 mutex_unlock(&ioc->tm_cmds.mutex);
6154}
6155
6156/**
6157 * _scsih_sas_discovery_event - handle discovery events
6158 * @ioc: per adapter object
6159 * @fw_event: The fw_event_work object
6160 * Context: user.
6161 *
6162 * Return nothing.
6163 */
6164static void
6165_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
6166 struct fw_event_work *fw_event)
6167{
Joe Lawrence35b62362014-06-25 17:05:34 -04006168 Mpi2EventDataSasDiscovery_t *event_data =
6169 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306170
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306171 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
6172 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
6173 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6174 "start" : "stop");
Sreekanth Reddyaf009412015-11-11 17:30:23 +05306175 if (event_data->DiscoveryStatus)
6176 pr_info("discovery_status(0x%08x)",
6177 le32_to_cpu(event_data->DiscoveryStatus));
6178 pr_info("\n");
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306179 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306180
6181 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
6182 !ioc->sas_hba.num_phys) {
6183 if (disable_discovery > 0 && ioc->shost_recovery) {
6184 /* Wait for the reset to complete */
6185 while (ioc->shost_recovery)
6186 ssleep(1);
6187 }
6188 _scsih_sas_host_add(ioc);
6189 }
6190}
6191
6192/**
6193 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
6194 * @ioc: per adapter object
6195 * @handle: device handle for physical disk
6196 * @phys_disk_num: physical disk number
6197 *
6198 * Return 0 for success, else failure.
6199 */
6200static int
6201_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
6202{
6203 Mpi2RaidActionRequest_t *mpi_request;
6204 Mpi2RaidActionReply_t *mpi_reply;
6205 u16 smid;
6206 u8 issue_reset = 0;
6207 int rc = 0;
6208 u16 ioc_status;
6209 u32 log_info;
6210
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306211 if (ioc->hba_mpi_version_belonged == MPI2_VERSION)
6212 return rc;
6213
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306214 mutex_lock(&ioc->scsih_cmds.mutex);
6215
6216 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
6217 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
6218 ioc->name, __func__);
6219 rc = -EAGAIN;
6220 goto out;
6221 }
6222 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
6223
6224 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
6225 if (!smid) {
6226 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
6227 ioc->name, __func__);
6228 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6229 rc = -EAGAIN;
6230 goto out;
6231 }
6232
6233 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
6234 ioc->scsih_cmds.smid = smid;
6235 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
6236
6237 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
6238 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
6239 mpi_request->PhysDiskNum = phys_disk_num;
6240
6241 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
6242 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
6243 handle, phys_disk_num));
6244
6245 init_completion(&ioc->scsih_cmds.done);
6246 mpt3sas_base_put_smid_default(ioc, smid);
6247 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
6248
6249 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
6250 pr_err(MPT3SAS_FMT "%s: timeout\n",
6251 ioc->name, __func__);
6252 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
6253 issue_reset = 1;
6254 rc = -EFAULT;
6255 goto out;
6256 }
6257
6258 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
6259
6260 mpi_reply = ioc->scsih_cmds.reply;
6261 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
6262 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
6263 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
6264 else
6265 log_info = 0;
6266 ioc_status &= MPI2_IOCSTATUS_MASK;
6267 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6268 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6269 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
6270 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
6271 log_info));
6272 rc = -EFAULT;
6273 } else
6274 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6275 "IR RAID_ACTION: completed successfully\n",
6276 ioc->name));
6277 }
6278
6279 out:
6280 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
6281 mutex_unlock(&ioc->scsih_cmds.mutex);
6282
6283 if (issue_reset)
6284 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
6285 FORCE_BIG_HAMMER);
6286 return rc;
6287}
6288
6289/**
6290 * _scsih_reprobe_lun - reprobing lun
6291 * @sdev: scsi device struct
6292 * @no_uld_attach: sdev->no_uld_attach flag setting
6293 *
6294 **/
6295static void
6296_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6297{
6298 int rc;
6299 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6300 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6301 sdev->no_uld_attach ? "hidding" : "exposing");
6302 rc = scsi_device_reprobe(sdev);
6303}
6304
6305/**
6306 * _scsih_sas_volume_add - add new volume
6307 * @ioc: per adapter object
6308 * @element: IR config element data
6309 * Context: user.
6310 *
6311 * Return nothing.
6312 */
6313static void
6314_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
6315 Mpi2EventIrConfigElement_t *element)
6316{
6317 struct _raid_device *raid_device;
6318 unsigned long flags;
6319 u64 wwid;
6320 u16 handle = le16_to_cpu(element->VolDevHandle);
6321 int rc;
6322
6323 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6324 if (!wwid) {
6325 pr_err(MPT3SAS_FMT
6326 "failure at %s:%d/%s()!\n", ioc->name,
6327 __FILE__, __LINE__, __func__);
6328 return;
6329 }
6330
6331 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6332 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6333 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6334
6335 if (raid_device)
6336 return;
6337
6338 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6339 if (!raid_device) {
6340 pr_err(MPT3SAS_FMT
6341 "failure at %s:%d/%s()!\n", ioc->name,
6342 __FILE__, __LINE__, __func__);
6343 return;
6344 }
6345
6346 raid_device->id = ioc->sas_id++;
6347 raid_device->channel = RAID_CHANNEL;
6348 raid_device->handle = handle;
6349 raid_device->wwid = wwid;
6350 _scsih_raid_device_add(ioc, raid_device);
6351 if (!ioc->wait_for_discovery_to_complete) {
6352 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6353 raid_device->id, 0);
6354 if (rc)
6355 _scsih_raid_device_remove(ioc, raid_device);
6356 } else {
6357 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6358 _scsih_determine_boot_device(ioc, raid_device, 1);
6359 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6360 }
6361}
6362
6363/**
6364 * _scsih_sas_volume_delete - delete volume
6365 * @ioc: per adapter object
6366 * @handle: volume device handle
6367 * Context: user.
6368 *
6369 * Return nothing.
6370 */
6371static void
6372_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
6373{
6374 struct _raid_device *raid_device;
6375 unsigned long flags;
6376 struct MPT3SAS_TARGET *sas_target_priv_data;
6377 struct scsi_target *starget = NULL;
6378
6379 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306380 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306381 if (raid_device) {
6382 if (raid_device->starget) {
6383 starget = raid_device->starget;
6384 sas_target_priv_data = starget->hostdata;
6385 sas_target_priv_data->deleted = 1;
6386 }
6387 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
6388 ioc->name, raid_device->handle,
6389 (unsigned long long) raid_device->wwid);
6390 list_del(&raid_device->list);
6391 kfree(raid_device);
6392 }
6393 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6394 if (starget)
6395 scsi_remove_target(&starget->dev);
6396}
6397
6398/**
6399 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6400 * @ioc: per adapter object
6401 * @element: IR config element data
6402 * Context: user.
6403 *
6404 * Return nothing.
6405 */
6406static void
6407_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
6408 Mpi2EventIrConfigElement_t *element)
6409{
6410 struct _sas_device *sas_device;
6411 struct scsi_target *starget = NULL;
6412 struct MPT3SAS_TARGET *sas_target_priv_data;
6413 unsigned long flags;
6414 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6415
6416 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306417 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306418 if (sas_device) {
6419 sas_device->volume_handle = 0;
6420 sas_device->volume_wwid = 0;
6421 clear_bit(handle, ioc->pd_handles);
6422 if (sas_device->starget && sas_device->starget->hostdata) {
6423 starget = sas_device->starget;
6424 sas_target_priv_data = starget->hostdata;
6425 sas_target_priv_data->flags &=
6426 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6427 }
6428 }
6429 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6430 if (!sas_device)
6431 return;
6432
6433 /* exposing raid component */
6434 if (starget)
6435 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306436
6437 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306438}
6439
6440/**
6441 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6442 * @ioc: per adapter object
6443 * @element: IR config element data
6444 * Context: user.
6445 *
6446 * Return nothing.
6447 */
6448static void
6449_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6450 Mpi2EventIrConfigElement_t *element)
6451{
6452 struct _sas_device *sas_device;
6453 struct scsi_target *starget = NULL;
6454 struct MPT3SAS_TARGET *sas_target_priv_data;
6455 unsigned long flags;
6456 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6457 u16 volume_handle = 0;
6458 u64 volume_wwid = 0;
6459
6460 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6461 if (volume_handle)
6462 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6463 &volume_wwid);
6464
6465 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306466 sas_device = __mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306467 if (sas_device) {
6468 set_bit(handle, ioc->pd_handles);
6469 if (sas_device->starget && sas_device->starget->hostdata) {
6470 starget = sas_device->starget;
6471 sas_target_priv_data = starget->hostdata;
6472 sas_target_priv_data->flags |=
6473 MPT_TARGET_FLAGS_RAID_COMPONENT;
6474 sas_device->volume_handle = volume_handle;
6475 sas_device->volume_wwid = volume_wwid;
6476 }
6477 }
6478 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6479 if (!sas_device)
6480 return;
6481
6482 /* hiding raid component */
6483 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306484
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306485 if (starget)
6486 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306487
6488 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306489}
6490
6491/**
6492 * _scsih_sas_pd_delete - delete pd component
6493 * @ioc: per adapter object
6494 * @element: IR config element data
6495 * Context: user.
6496 *
6497 * Return nothing.
6498 */
6499static void
6500_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6501 Mpi2EventIrConfigElement_t *element)
6502{
6503 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6504
6505 _scsih_device_remove_by_handle(ioc, handle);
6506}
6507
6508/**
6509 * _scsih_sas_pd_add - remove pd component
6510 * @ioc: per adapter object
6511 * @element: IR config element data
6512 * Context: user.
6513 *
6514 * Return nothing.
6515 */
6516static void
6517_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6518 Mpi2EventIrConfigElement_t *element)
6519{
6520 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306521 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6522 Mpi2ConfigReply_t mpi_reply;
6523 Mpi2SasDevicePage0_t sas_device_pg0;
6524 u32 ioc_status;
6525 u64 sas_address;
6526 u16 parent_handle;
6527
6528 set_bit(handle, ioc->pd_handles);
6529
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306530 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306531 if (sas_device) {
6532 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306533 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306534 return;
6535 }
6536
6537 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6538 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6539 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6540 ioc->name, __FILE__, __LINE__, __func__);
6541 return;
6542 }
6543
6544 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6545 MPI2_IOCSTATUS_MASK;
6546 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6547 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6548 ioc->name, __FILE__, __LINE__, __func__);
6549 return;
6550 }
6551
6552 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6553 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6554 mpt3sas_transport_update_links(ioc, sas_address, handle,
6555 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6556
6557 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6558 _scsih_add_device(ioc, handle, 0, 1);
6559}
6560
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306561/**
6562 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6563 * @ioc: per adapter object
6564 * @event_data: event data payload
6565 * Context: user.
6566 *
6567 * Return nothing.
6568 */
6569static void
6570_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6571 Mpi2EventDataIrConfigChangeList_t *event_data)
6572{
6573 Mpi2EventIrConfigElement_t *element;
6574 u8 element_type;
6575 int i;
6576 char *reason_str = NULL, *element_str = NULL;
6577
6578 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6579
6580 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6581 ioc->name, (le32_to_cpu(event_data->Flags) &
6582 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6583 "foreign" : "native", event_data->NumElements);
6584 for (i = 0; i < event_data->NumElements; i++, element++) {
6585 switch (element->ReasonCode) {
6586 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6587 reason_str = "add";
6588 break;
6589 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6590 reason_str = "remove";
6591 break;
6592 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6593 reason_str = "no change";
6594 break;
6595 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6596 reason_str = "hide";
6597 break;
6598 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6599 reason_str = "unhide";
6600 break;
6601 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6602 reason_str = "volume_created";
6603 break;
6604 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6605 reason_str = "volume_deleted";
6606 break;
6607 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6608 reason_str = "pd_created";
6609 break;
6610 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6611 reason_str = "pd_deleted";
6612 break;
6613 default:
6614 reason_str = "unknown reason";
6615 break;
6616 }
6617 element_type = le16_to_cpu(element->ElementFlags) &
6618 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6619 switch (element_type) {
6620 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6621 element_str = "volume";
6622 break;
6623 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6624 element_str = "phys disk";
6625 break;
6626 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6627 element_str = "hot spare";
6628 break;
6629 default:
6630 element_str = "unknown element";
6631 break;
6632 }
6633 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6634 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6635 reason_str, le16_to_cpu(element->VolDevHandle),
6636 le16_to_cpu(element->PhysDiskDevHandle),
6637 element->PhysDiskNum);
6638 }
6639}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306640
6641/**
6642 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6643 * @ioc: per adapter object
6644 * @fw_event: The fw_event_work object
6645 * Context: user.
6646 *
6647 * Return nothing.
6648 */
6649static void
6650_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6651 struct fw_event_work *fw_event)
6652{
6653 Mpi2EventIrConfigElement_t *element;
6654 int i;
6655 u8 foreign_config;
Joe Lawrence35b62362014-06-25 17:05:34 -04006656 Mpi2EventDataIrConfigChangeList_t *event_data =
6657 (Mpi2EventDataIrConfigChangeList_t *)
6658 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306659
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306660 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6661 (!ioc->hide_ir_msg))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306662 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6663
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306664 foreign_config = (le32_to_cpu(event_data->Flags) &
6665 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6666
6667 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306668 if (ioc->shost_recovery &&
6669 ioc->hba_mpi_version_belonged != MPI2_VERSION) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306670 for (i = 0; i < event_data->NumElements; i++, element++) {
6671 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6672 _scsih_ir_fastpath(ioc,
6673 le16_to_cpu(element->PhysDiskDevHandle),
6674 element->PhysDiskNum);
6675 }
6676 return;
6677 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306678
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306679 for (i = 0; i < event_data->NumElements; i++, element++) {
6680
6681 switch (element->ReasonCode) {
6682 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6683 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6684 if (!foreign_config)
6685 _scsih_sas_volume_add(ioc, element);
6686 break;
6687 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6688 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6689 if (!foreign_config)
6690 _scsih_sas_volume_delete(ioc,
6691 le16_to_cpu(element->VolDevHandle));
6692 break;
6693 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306694 if (!ioc->is_warpdrive)
6695 _scsih_sas_pd_hide(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306696 break;
6697 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306698 if (!ioc->is_warpdrive)
6699 _scsih_sas_pd_expose(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306700 break;
6701 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306702 if (!ioc->is_warpdrive)
6703 _scsih_sas_pd_add(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306704 break;
6705 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306706 if (!ioc->is_warpdrive)
6707 _scsih_sas_pd_delete(ioc, element);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306708 break;
6709 }
6710 }
6711}
6712
6713/**
6714 * _scsih_sas_ir_volume_event - IR volume event
6715 * @ioc: per adapter object
6716 * @fw_event: The fw_event_work object
6717 * Context: user.
6718 *
6719 * Return nothing.
6720 */
6721static void
6722_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6723 struct fw_event_work *fw_event)
6724{
6725 u64 wwid;
6726 unsigned long flags;
6727 struct _raid_device *raid_device;
6728 u16 handle;
6729 u32 state;
6730 int rc;
Joe Lawrence35b62362014-06-25 17:05:34 -04006731 Mpi2EventDataIrVolume_t *event_data =
6732 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306733
6734 if (ioc->shost_recovery)
6735 return;
6736
6737 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6738 return;
6739
6740 handle = le16_to_cpu(event_data->VolDevHandle);
6741 state = le32_to_cpu(event_data->NewValue);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306742 if (!ioc->hide_ir_msg)
6743 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6744 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6745 ioc->name, __func__, handle,
6746 le32_to_cpu(event_data->PreviousValue), state));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306747 switch (state) {
6748 case MPI2_RAID_VOL_STATE_MISSING:
6749 case MPI2_RAID_VOL_STATE_FAILED:
6750 _scsih_sas_volume_delete(ioc, handle);
6751 break;
6752
6753 case MPI2_RAID_VOL_STATE_ONLINE:
6754 case MPI2_RAID_VOL_STATE_DEGRADED:
6755 case MPI2_RAID_VOL_STATE_OPTIMAL:
6756
6757 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306758 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306759 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6760
6761 if (raid_device)
6762 break;
6763
6764 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6765 if (!wwid) {
6766 pr_err(MPT3SAS_FMT
6767 "failure at %s:%d/%s()!\n", ioc->name,
6768 __FILE__, __LINE__, __func__);
6769 break;
6770 }
6771
6772 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6773 if (!raid_device) {
6774 pr_err(MPT3SAS_FMT
6775 "failure at %s:%d/%s()!\n", ioc->name,
6776 __FILE__, __LINE__, __func__);
6777 break;
6778 }
6779
6780 raid_device->id = ioc->sas_id++;
6781 raid_device->channel = RAID_CHANNEL;
6782 raid_device->handle = handle;
6783 raid_device->wwid = wwid;
6784 _scsih_raid_device_add(ioc, raid_device);
6785 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6786 raid_device->id, 0);
6787 if (rc)
6788 _scsih_raid_device_remove(ioc, raid_device);
6789 break;
6790
6791 case MPI2_RAID_VOL_STATE_INITIALIZING:
6792 default:
6793 break;
6794 }
6795}
6796
6797/**
6798 * _scsih_sas_ir_physical_disk_event - PD event
6799 * @ioc: per adapter object
6800 * @fw_event: The fw_event_work object
6801 * Context: user.
6802 *
6803 * Return nothing.
6804 */
6805static void
6806_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6807 struct fw_event_work *fw_event)
6808{
6809 u16 handle, parent_handle;
6810 u32 state;
6811 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306812 Mpi2ConfigReply_t mpi_reply;
6813 Mpi2SasDevicePage0_t sas_device_pg0;
6814 u32 ioc_status;
Joe Lawrence35b62362014-06-25 17:05:34 -04006815 Mpi2EventDataIrPhysicalDisk_t *event_data =
6816 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306817 u64 sas_address;
6818
6819 if (ioc->shost_recovery)
6820 return;
6821
6822 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6823 return;
6824
6825 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6826 state = le32_to_cpu(event_data->NewValue);
6827
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306828 if (!ioc->hide_ir_msg)
6829 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6830 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6831 ioc->name, __func__, handle,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306832 le32_to_cpu(event_data->PreviousValue), state));
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306833
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306834 switch (state) {
6835 case MPI2_RAID_PD_STATE_ONLINE:
6836 case MPI2_RAID_PD_STATE_DEGRADED:
6837 case MPI2_RAID_PD_STATE_REBUILDING:
6838 case MPI2_RAID_PD_STATE_OPTIMAL:
6839 case MPI2_RAID_PD_STATE_HOT_SPARE:
6840
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306841 if (!ioc->is_warpdrive)
6842 set_bit(handle, ioc->pd_handles);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306843
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306844 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
6845 if (sas_device) {
6846 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306847 return;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05306848 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306849
6850 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6851 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6852 handle))) {
6853 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6854 ioc->name, __FILE__, __LINE__, __func__);
6855 return;
6856 }
6857
6858 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6859 MPI2_IOCSTATUS_MASK;
6860 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6861 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6862 ioc->name, __FILE__, __LINE__, __func__);
6863 return;
6864 }
6865
6866 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6867 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6868 mpt3sas_transport_update_links(ioc, sas_address, handle,
6869 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6870
6871 _scsih_add_device(ioc, handle, 0, 1);
6872
6873 break;
6874
6875 case MPI2_RAID_PD_STATE_OFFLINE:
6876 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6877 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6878 default:
6879 break;
6880 }
6881}
6882
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306883/**
6884 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6885 * @ioc: per adapter object
6886 * @event_data: event data payload
6887 * Context: user.
6888 *
6889 * Return nothing.
6890 */
6891static void
6892_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6893 Mpi2EventDataIrOperationStatus_t *event_data)
6894{
6895 char *reason_str = NULL;
6896
6897 switch (event_data->RAIDOperation) {
6898 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6899 reason_str = "resync";
6900 break;
6901 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6902 reason_str = "online capacity expansion";
6903 break;
6904 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6905 reason_str = "consistency check";
6906 break;
6907 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6908 reason_str = "background init";
6909 break;
6910 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6911 reason_str = "make data consistent";
6912 break;
6913 }
6914
6915 if (!reason_str)
6916 return;
6917
6918 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6919 "\thandle(0x%04x), percent complete(%d)\n",
6920 ioc->name, reason_str,
6921 le16_to_cpu(event_data->VolDevHandle),
6922 event_data->PercentComplete);
6923}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306924
6925/**
6926 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6927 * @ioc: per adapter object
6928 * @fw_event: The fw_event_work object
6929 * Context: user.
6930 *
6931 * Return nothing.
6932 */
6933static void
6934_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6935 struct fw_event_work *fw_event)
6936{
Joe Lawrence35b62362014-06-25 17:05:34 -04006937 Mpi2EventDataIrOperationStatus_t *event_data =
6938 (Mpi2EventDataIrOperationStatus_t *)
6939 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306940 static struct _raid_device *raid_device;
6941 unsigned long flags;
6942 u16 handle;
6943
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05306944 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) &&
6945 (!ioc->hide_ir_msg))
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306946 _scsih_sas_ir_operation_status_event_debug(ioc,
6947 event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306948
6949 /* code added for raid transport support */
6950 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6951
6952 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6953 handle = le16_to_cpu(event_data->VolDevHandle);
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05306954 raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306955 if (raid_device)
6956 raid_device->percent_complete =
6957 event_data->PercentComplete;
6958 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6959 }
6960}
6961
6962/**
6963 * _scsih_prep_device_scan - initialize parameters prior to device scan
6964 * @ioc: per adapter object
6965 *
6966 * Set the deleted flag prior to device scan. If the device is found during
6967 * the scan, then we clear the deleted flag.
6968 */
6969static void
6970_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6971{
6972 struct MPT3SAS_DEVICE *sas_device_priv_data;
6973 struct scsi_device *sdev;
6974
6975 shost_for_each_device(sdev, ioc->shost) {
6976 sas_device_priv_data = sdev->hostdata;
6977 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6978 sas_device_priv_data->sas_target->deleted = 1;
6979 }
6980}
6981
6982/**
6983 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6984 * @ioc: per adapter object
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306985 * @sas_device_pg0: SAS Device page 0
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306986 *
6987 * After host reset, find out whether devices are still responding.
6988 * Used in _scsih_remove_unresponsive_sas_devices.
6989 *
6990 * Return nothing.
6991 */
6992static void
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306993_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6994Mpi2SasDevicePage0_t *sas_device_pg0)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306995{
6996 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6997 struct scsi_target *starget;
6998 struct _sas_device *sas_device;
6999 unsigned long flags;
7000
7001 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7002 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307003 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
7004 (sas_device->slot == sas_device_pg0->Slot)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307005 sas_device->responding = 1;
7006 starget = sas_device->starget;
7007 if (starget && starget->hostdata) {
7008 sas_target_priv_data = starget->hostdata;
7009 sas_target_priv_data->tm_busy = 0;
7010 sas_target_priv_data->deleted = 0;
7011 } else
7012 sas_target_priv_data = NULL;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307013 if (starget) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307014 starget_printk(KERN_INFO, starget,
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307015 "handle(0x%04x), sas_addr(0x%016llx)\n",
7016 sas_device_pg0->DevHandle,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307017 (unsigned long long)
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307018 sas_device->sas_address);
7019
7020 if (sas_device->enclosure_handle != 0)
7021 starget_printk(KERN_INFO, starget,
7022 "enclosure logical id(0x%016llx),"
7023 " slot(%d)\n",
7024 (unsigned long long)
7025 sas_device->enclosure_logical_id,
7026 sas_device->slot);
7027 }
7028 if (sas_device_pg0->Flags &
7029 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
7030 sas_device->enclosure_level =
7031 le16_to_cpu(sas_device_pg0->EnclosureLevel);
7032 memcpy(&sas_device->connector_name[0],
7033 &sas_device_pg0->ConnectorName[0], 4);
7034 } else {
7035 sas_device->enclosure_level = 0;
7036 sas_device->connector_name[0] = '\0';
7037 }
7038
7039 if (sas_device->handle == sas_device_pg0->DevHandle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307040 goto out;
7041 pr_info("\thandle changed from(0x%04x)!!!\n",
7042 sas_device->handle);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307043 sas_device->handle = sas_device_pg0->DevHandle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307044 if (sas_target_priv_data)
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307045 sas_target_priv_data->handle =
7046 sas_device_pg0->DevHandle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307047 goto out;
7048 }
7049 }
7050 out:
7051 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7052}
7053
7054/**
7055 * _scsih_search_responding_sas_devices -
7056 * @ioc: per adapter object
7057 *
7058 * After host reset, find out whether devices are still responding.
7059 * If not remove.
7060 *
7061 * Return nothing.
7062 */
7063static void
7064_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7065{
7066 Mpi2SasDevicePage0_t sas_device_pg0;
7067 Mpi2ConfigReply_t mpi_reply;
7068 u16 ioc_status;
7069 u16 handle;
7070 u32 device_info;
7071
7072 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
7073
7074 if (list_empty(&ioc->sas_device_list))
7075 goto out;
7076
7077 handle = 0xFFFF;
7078 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7079 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7080 handle))) {
7081 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7082 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307083 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307084 break;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307085 handle = sas_device_pg0.DevHandle =
7086 le16_to_cpu(sas_device_pg0.DevHandle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307087 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
7088 if (!(_scsih_is_end_device(device_info)))
7089 continue;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05307090 sas_device_pg0.SASAddress =
7091 le64_to_cpu(sas_device_pg0.SASAddress);
7092 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
7093 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307094 }
7095
7096 out:
7097 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
7098 ioc->name);
7099}
7100
7101/**
7102 * _scsih_mark_responding_raid_device - mark a raid_device as responding
7103 * @ioc: per adapter object
7104 * @wwid: world wide identifier for raid volume
7105 * @handle: device handle
7106 *
7107 * After host reset, find out whether devices are still responding.
7108 * Used in _scsih_remove_unresponsive_raid_devices.
7109 *
7110 * Return nothing.
7111 */
7112static void
7113_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
7114 u16 handle)
7115{
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307116 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307117 struct scsi_target *starget;
7118 struct _raid_device *raid_device;
7119 unsigned long flags;
7120
7121 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7122 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
7123 if (raid_device->wwid == wwid && raid_device->starget) {
7124 starget = raid_device->starget;
7125 if (starget && starget->hostdata) {
7126 sas_target_priv_data = starget->hostdata;
7127 sas_target_priv_data->deleted = 0;
7128 } else
7129 sas_target_priv_data = NULL;
7130 raid_device->responding = 1;
7131 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7132 starget_printk(KERN_INFO, raid_device->starget,
7133 "handle(0x%04x), wwid(0x%016llx)\n", handle,
7134 (unsigned long long)raid_device->wwid);
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307135
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307136 /*
7137 * WARPDRIVE: The handles of the PDs might have changed
7138 * across the host reset so re-initialize the
7139 * required data for Direct IO
7140 */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05307141 mpt3sas_init_warpdrive_properties(ioc, raid_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307142 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7143 if (raid_device->handle == handle) {
7144 spin_unlock_irqrestore(&ioc->raid_device_lock,
7145 flags);
7146 return;
7147 }
7148 pr_info("\thandle changed from(0x%04x)!!!\n",
7149 raid_device->handle);
7150 raid_device->handle = handle;
7151 if (sas_target_priv_data)
7152 sas_target_priv_data->handle = handle;
7153 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7154 return;
7155 }
7156 }
7157 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7158}
7159
7160/**
7161 * _scsih_search_responding_raid_devices -
7162 * @ioc: per adapter object
7163 *
7164 * After host reset, find out whether devices are still responding.
7165 * If not remove.
7166 *
7167 * Return nothing.
7168 */
7169static void
7170_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
7171{
7172 Mpi2RaidVolPage1_t volume_pg1;
7173 Mpi2RaidVolPage0_t volume_pg0;
7174 Mpi2RaidPhysDiskPage0_t pd_pg0;
7175 Mpi2ConfigReply_t mpi_reply;
7176 u16 ioc_status;
7177 u16 handle;
7178 u8 phys_disk_num;
7179
7180 if (!ioc->ir_firmware)
7181 return;
7182
7183 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
7184 ioc->name);
7185
7186 if (list_empty(&ioc->raid_device_list))
7187 goto out;
7188
7189 handle = 0xFFFF;
7190 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7191 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7192 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7193 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307194 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307195 break;
7196 handle = le16_to_cpu(volume_pg1.DevHandle);
7197
7198 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7199 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7200 sizeof(Mpi2RaidVolPage0_t)))
7201 continue;
7202
7203 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7204 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7205 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
7206 _scsih_mark_responding_raid_device(ioc,
7207 le64_to_cpu(volume_pg1.WWID), handle);
7208 }
7209
7210 /* refresh the pd_handles */
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307211 if (!ioc->is_warpdrive) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307212 phys_disk_num = 0xFF;
7213 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
7214 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7215 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7216 phys_disk_num))) {
7217 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7218 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307219 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307220 break;
7221 phys_disk_num = pd_pg0.PhysDiskNum;
7222 handle = le16_to_cpu(pd_pg0.DevHandle);
7223 set_bit(handle, ioc->pd_handles);
7224 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307225 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307226 out:
7227 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
7228 ioc->name);
7229}
7230
7231/**
7232 * _scsih_mark_responding_expander - mark a expander as responding
7233 * @ioc: per adapter object
7234 * @sas_address: sas address
7235 * @handle:
7236 *
7237 * After host reset, find out whether devices are still responding.
7238 * Used in _scsih_remove_unresponsive_expanders.
7239 *
7240 * Return nothing.
7241 */
7242static void
7243_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
7244 u16 handle)
7245{
7246 struct _sas_node *sas_expander;
7247 unsigned long flags;
7248 int i;
7249
7250 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7251 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
7252 if (sas_expander->sas_address != sas_address)
7253 continue;
7254 sas_expander->responding = 1;
7255 if (sas_expander->handle == handle)
7256 goto out;
7257 pr_info("\texpander(0x%016llx): handle changed" \
7258 " from(0x%04x) to (0x%04x)!!!\n",
7259 (unsigned long long)sas_expander->sas_address,
7260 sas_expander->handle, handle);
7261 sas_expander->handle = handle;
7262 for (i = 0 ; i < sas_expander->num_phys ; i++)
7263 sas_expander->phy[i].handle = handle;
7264 goto out;
7265 }
7266 out:
7267 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7268}
7269
7270/**
7271 * _scsih_search_responding_expanders -
7272 * @ioc: per adapter object
7273 *
7274 * After host reset, find out whether devices are still responding.
7275 * If not remove.
7276 *
7277 * Return nothing.
7278 */
7279static void
7280_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
7281{
7282 Mpi2ExpanderPage0_t expander_pg0;
7283 Mpi2ConfigReply_t mpi_reply;
7284 u16 ioc_status;
7285 u64 sas_address;
7286 u16 handle;
7287
7288 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
7289
7290 if (list_empty(&ioc->sas_expander_list))
7291 goto out;
7292
7293 handle = 0xFFFF;
7294 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7295 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7296
7297 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7298 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05307299 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307300 break;
7301
7302 handle = le16_to_cpu(expander_pg0.DevHandle);
7303 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7304 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
7305 handle,
7306 (unsigned long long)sas_address);
7307 _scsih_mark_responding_expander(ioc, sas_address, handle);
7308 }
7309
7310 out:
7311 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
7312}
7313
7314/**
7315 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
7316 * @ioc: per adapter object
7317 *
7318 * Return nothing.
7319 */
7320static void
7321_scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
7322{
7323 struct _sas_device *sas_device, *sas_device_next;
7324 struct _sas_node *sas_expander, *sas_expander_next;
7325 struct _raid_device *raid_device, *raid_device_next;
7326 struct list_head tmp_list;
7327 unsigned long flags;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307328 LIST_HEAD(head);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307329
7330 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
7331 ioc->name);
7332
7333 /* removing unresponding end devices */
7334 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
7335 ioc->name);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307336 /*
7337 * Iterate, pulling off devices marked as non-responding. We become the
7338 * owner for the reference the list had on any object we prune.
7339 */
7340 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307341 list_for_each_entry_safe(sas_device, sas_device_next,
7342 &ioc->sas_device_list, list) {
7343 if (!sas_device->responding)
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307344 list_move_tail(&sas_device->list, &head);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307345 else
7346 sas_device->responding = 0;
7347 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307348 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7349
7350 /*
7351 * Now, uninitialize and remove the unresponding devices we pruned.
7352 */
7353 list_for_each_entry_safe(sas_device, sas_device_next, &head, list) {
7354 _scsih_remove_device(ioc, sas_device);
7355 list_del_init(&sas_device->list);
7356 sas_device_put(sas_device);
7357 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307358
7359 /* removing unresponding volumes */
7360 if (ioc->ir_firmware) {
7361 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
7362 ioc->name);
7363 list_for_each_entry_safe(raid_device, raid_device_next,
7364 &ioc->raid_device_list, list) {
7365 if (!raid_device->responding)
7366 _scsih_sas_volume_delete(ioc,
7367 raid_device->handle);
7368 else
7369 raid_device->responding = 0;
7370 }
7371 }
7372
7373 /* removing unresponding expanders */
7374 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
7375 ioc->name);
7376 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7377 INIT_LIST_HEAD(&tmp_list);
7378 list_for_each_entry_safe(sas_expander, sas_expander_next,
7379 &ioc->sas_expander_list, list) {
7380 if (!sas_expander->responding)
7381 list_move_tail(&sas_expander->list, &tmp_list);
7382 else
7383 sas_expander->responding = 0;
7384 }
7385 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7386 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7387 list) {
7388 list_del(&sas_expander->list);
7389 _scsih_expander_node_remove(ioc, sas_expander);
7390 }
7391
7392 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
7393 ioc->name);
7394
7395 /* unblock devices */
7396 _scsih_ublock_io_all_device(ioc);
7397}
7398
7399static void
7400_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
7401 struct _sas_node *sas_expander, u16 handle)
7402{
7403 Mpi2ExpanderPage1_t expander_pg1;
7404 Mpi2ConfigReply_t mpi_reply;
7405 int i;
7406
7407 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7408 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
7409 &expander_pg1, i, handle))) {
7410 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7411 ioc->name, __FILE__, __LINE__, __func__);
7412 return;
7413 }
7414
7415 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
7416 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7417 expander_pg1.NegotiatedLinkRate >> 4);
7418 }
7419}
7420
7421/**
7422 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
7423 * @ioc: per adapter object
7424 *
7425 * Return nothing.
7426 */
7427static void
7428_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
7429{
7430 Mpi2ExpanderPage0_t expander_pg0;
7431 Mpi2SasDevicePage0_t sas_device_pg0;
7432 Mpi2RaidVolPage1_t volume_pg1;
7433 Mpi2RaidVolPage0_t volume_pg0;
7434 Mpi2RaidPhysDiskPage0_t pd_pg0;
7435 Mpi2EventIrConfigElement_t element;
7436 Mpi2ConfigReply_t mpi_reply;
7437 u8 phys_disk_num;
7438 u16 ioc_status;
7439 u16 handle, parent_handle;
7440 u64 sas_address;
7441 struct _sas_device *sas_device;
7442 struct _sas_node *expander_device;
7443 static struct _raid_device *raid_device;
7444 u8 retry_count;
7445 unsigned long flags;
7446
7447 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
7448
7449 _scsih_sas_host_refresh(ioc);
7450
7451 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
7452
7453 /* expanders */
7454 handle = 0xFFFF;
7455 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7456 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7457 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7458 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307459 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7460 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
7461 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7462 ioc->name, ioc_status,
7463 le32_to_cpu(mpi_reply.IOCLogInfo));
7464 break;
7465 }
7466 handle = le16_to_cpu(expander_pg0.DevHandle);
7467 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7468 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7469 ioc, le64_to_cpu(expander_pg0.SASAddress));
7470 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7471 if (expander_device)
7472 _scsih_refresh_expander_links(ioc, expander_device,
7473 handle);
7474 else {
7475 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7476 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7477 handle, (unsigned long long)
7478 le64_to_cpu(expander_pg0.SASAddress));
7479 _scsih_expander_add(ioc, handle);
7480 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7481 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7482 handle, (unsigned long long)
7483 le64_to_cpu(expander_pg0.SASAddress));
7484 }
7485 }
7486
7487 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7488 ioc->name);
7489
7490 if (!ioc->ir_firmware)
7491 goto skip_to_sas;
7492
7493 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7494
7495 /* phys disk */
7496 phys_disk_num = 0xFF;
7497 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7498 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7499 phys_disk_num))) {
7500 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7501 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307502 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7503 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7504 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7505 ioc->name, ioc_status,
7506 le32_to_cpu(mpi_reply.IOCLogInfo));
7507 break;
7508 }
7509 phys_disk_num = pd_pg0.PhysDiskNum;
7510 handle = le16_to_cpu(pd_pg0.DevHandle);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307511 sas_device = mpt3sas_get_sdev_by_handle(ioc, handle);
7512 if (sas_device) {
7513 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307514 continue;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307515 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307516 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7517 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7518 handle) != 0)
7519 continue;
7520 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7521 MPI2_IOCSTATUS_MASK;
7522 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7523 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7524 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7525 ioc->name, ioc_status,
7526 le32_to_cpu(mpi_reply.IOCLogInfo));
7527 break;
7528 }
7529 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7530 if (!_scsih_get_sas_address(ioc, parent_handle,
7531 &sas_address)) {
7532 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7533 " handle (0x%04x), sas_addr(0x%016llx)\n",
7534 ioc->name, handle, (unsigned long long)
7535 le64_to_cpu(sas_device_pg0.SASAddress));
7536 mpt3sas_transport_update_links(ioc, sas_address,
7537 handle, sas_device_pg0.PhyNum,
7538 MPI2_SAS_NEG_LINK_RATE_1_5);
7539 set_bit(handle, ioc->pd_handles);
7540 retry_count = 0;
7541 /* This will retry adding the end device.
7542 * _scsih_add_device() will decide on retries and
7543 * return "1" when it should be retried
7544 */
7545 while (_scsih_add_device(ioc, handle, retry_count++,
7546 1)) {
7547 ssleep(1);
7548 }
7549 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7550 " handle (0x%04x), sas_addr(0x%016llx)\n",
7551 ioc->name, handle, (unsigned long long)
7552 le64_to_cpu(sas_device_pg0.SASAddress));
7553 }
7554 }
7555
7556 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7557 ioc->name);
7558
7559 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7560
7561 /* volumes */
7562 handle = 0xFFFF;
7563 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7564 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7565 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7566 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307567 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7568 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7569 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7570 ioc->name, ioc_status,
7571 le32_to_cpu(mpi_reply.IOCLogInfo));
7572 break;
7573 }
7574 handle = le16_to_cpu(volume_pg1.DevHandle);
7575 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7576 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7577 le64_to_cpu(volume_pg1.WWID));
7578 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7579 if (raid_device)
7580 continue;
7581 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7582 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7583 sizeof(Mpi2RaidVolPage0_t)))
7584 continue;
7585 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7586 MPI2_IOCSTATUS_MASK;
7587 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7588 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7589 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7590 ioc->name, ioc_status,
7591 le32_to_cpu(mpi_reply.IOCLogInfo));
7592 break;
7593 }
7594 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7595 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7596 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7597 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7598 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7599 element.VolDevHandle = volume_pg1.DevHandle;
7600 pr_info(MPT3SAS_FMT
7601 "\tBEFORE adding volume: handle (0x%04x)\n",
7602 ioc->name, volume_pg1.DevHandle);
7603 _scsih_sas_volume_add(ioc, &element);
7604 pr_info(MPT3SAS_FMT
7605 "\tAFTER adding volume: handle (0x%04x)\n",
7606 ioc->name, volume_pg1.DevHandle);
7607 }
7608 }
7609
7610 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7611 ioc->name);
7612
7613 skip_to_sas:
7614
7615 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7616 ioc->name);
7617
7618 /* sas devices */
7619 handle = 0xFFFF;
7620 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7621 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7622 handle))) {
7623 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7624 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307625 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7626 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7627 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7628 ioc->name, ioc_status,
7629 le32_to_cpu(mpi_reply.IOCLogInfo));
7630 break;
7631 }
7632 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7633 if (!(_scsih_is_end_device(
7634 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7635 continue;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307636 sas_device = mpt3sas_get_sdev_by_addr(ioc,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307637 le64_to_cpu(sas_device_pg0.SASAddress));
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307638 if (sas_device) {
7639 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307640 continue;
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05307641 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307642 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7643 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7644 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7645 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7646 handle, (unsigned long long)
7647 le64_to_cpu(sas_device_pg0.SASAddress));
7648 mpt3sas_transport_update_links(ioc, sas_address, handle,
7649 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7650 retry_count = 0;
7651 /* This will retry adding the end device.
7652 * _scsih_add_device() will decide on retries and
7653 * return "1" when it should be retried
7654 */
7655 while (_scsih_add_device(ioc, handle, retry_count++,
7656 0)) {
7657 ssleep(1);
7658 }
7659 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7660 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7661 handle, (unsigned long long)
7662 le64_to_cpu(sas_device_pg0.SASAddress));
7663 }
7664 }
7665 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7666 ioc->name);
7667
7668 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7669}
7670/**
7671 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7672 * @ioc: per adapter object
7673 * @reset_phase: phase
7674 *
7675 * The handler for doing any required cleanup or initialization.
7676 *
7677 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7678 * MPT3_IOC_DONE_RESET
7679 *
7680 * Return nothing.
7681 */
7682void
7683mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7684{
7685 switch (reset_phase) {
7686 case MPT3_IOC_PRE_RESET:
7687 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7688 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7689 break;
7690 case MPT3_IOC_AFTER_RESET:
7691 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7692 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7693 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7694 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7695 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7696 complete(&ioc->scsih_cmds.done);
7697 }
7698 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7699 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7700 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7701 complete(&ioc->tm_cmds.done);
7702 }
7703
7704 _scsih_fw_event_cleanup_queue(ioc);
7705 _scsih_flush_running_cmds(ioc);
7706 break;
7707 case MPT3_IOC_DONE_RESET:
7708 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7709 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7710 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7711 !ioc->sas_hba.num_phys)) {
7712 _scsih_prep_device_scan(ioc);
7713 _scsih_search_responding_sas_devices(ioc);
7714 _scsih_search_responding_raid_devices(ioc);
7715 _scsih_search_responding_expanders(ioc);
7716 _scsih_error_recovery_delete_devices(ioc);
7717 }
7718 break;
7719 }
7720}
7721
7722/**
7723 * _mpt3sas_fw_work - delayed task for processing firmware events
7724 * @ioc: per adapter object
7725 * @fw_event: The fw_event_work object
7726 * Context: user.
7727 *
7728 * Return nothing.
7729 */
7730static void
7731_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7732{
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307733 _scsih_fw_event_del_from_list(ioc, fw_event);
7734
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307735 /* the queue is being flushed so ignore this event */
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307736 if (ioc->remove_host || ioc->pci_error_recovery) {
7737 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307738 return;
7739 }
7740
7741 switch (fw_event->event) {
7742 case MPT3SAS_PROCESS_TRIGGER_DIAG:
Joe Lawrence35b62362014-06-25 17:05:34 -04007743 mpt3sas_process_trigger_data(ioc,
7744 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7745 fw_event->event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307746 break;
7747 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307748 while (scsi_host_in_recovery(ioc->shost) ||
7749 ioc->shost_recovery) {
7750 /*
7751 * If we're unloading, bail. Otherwise, this can become
7752 * an infinite loop.
7753 */
7754 if (ioc->remove_host)
7755 goto out;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307756 ssleep(1);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307757 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307758 _scsih_remove_unresponding_sas_devices(ioc);
7759 _scsih_scan_for_devices_after_reset(ioc);
7760 break;
7761 case MPT3SAS_PORT_ENABLE_COMPLETE:
7762 ioc->start_scan = 0;
7763 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7764 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7765 missing_delay[1]);
7766 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7767 "port enable: complete from worker thread\n",
7768 ioc->name));
7769 break;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05307770 case MPT3SAS_TURN_ON_PFA_LED:
7771 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307772 break;
7773 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7774 _scsih_sas_topology_change_event(ioc, fw_event);
7775 break;
7776 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7777 _scsih_sas_device_status_change_event(ioc, fw_event);
7778 break;
7779 case MPI2_EVENT_SAS_DISCOVERY:
7780 _scsih_sas_discovery_event(ioc, fw_event);
7781 break;
7782 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7783 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7784 break;
7785 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7786 _scsih_sas_enclosure_dev_status_change_event(ioc,
7787 fw_event);
7788 break;
7789 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7790 _scsih_sas_ir_config_change_event(ioc, fw_event);
7791 break;
7792 case MPI2_EVENT_IR_VOLUME:
7793 _scsih_sas_ir_volume_event(ioc, fw_event);
7794 break;
7795 case MPI2_EVENT_IR_PHYSICAL_DISK:
7796 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7797 break;
7798 case MPI2_EVENT_IR_OPERATION_STATUS:
7799 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7800 break;
7801 }
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307802out:
7803 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307804}
7805
7806/**
7807 * _firmware_event_work
7808 * @ioc: per adapter object
7809 * @work: The fw_event_work object
7810 * Context: user.
7811 *
7812 * wrappers for the work thread handling firmware events
7813 *
7814 * Return nothing.
7815 */
7816
7817static void
7818_firmware_event_work(struct work_struct *work)
7819{
7820 struct fw_event_work *fw_event = container_of(work,
7821 struct fw_event_work, work);
7822
7823 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7824}
7825
7826/**
7827 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7828 * @ioc: per adapter object
7829 * @msix_index: MSIX table index supplied by the OS
7830 * @reply: reply message frame(lower 32bit addr)
7831 * Context: interrupt.
7832 *
7833 * This function merely adds a new work task into ioc->firmware_event_thread.
7834 * The tasks are worked from _firmware_event_work in user context.
7835 *
7836 * Return 1 meaning mf should be freed from _base_interrupt
7837 * 0 means the mf is freed from this function.
7838 */
7839u8
7840mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7841 u32 reply)
7842{
7843 struct fw_event_work *fw_event;
7844 Mpi2EventNotificationReply_t *mpi_reply;
7845 u16 event;
7846 u16 sz;
7847
7848 /* events turned off due to host reset or driver unloading */
7849 if (ioc->remove_host || ioc->pci_error_recovery)
7850 return 1;
7851
7852 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7853
7854 if (unlikely(!mpi_reply)) {
7855 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7856 ioc->name, __FILE__, __LINE__, __func__);
7857 return 1;
7858 }
7859
7860 event = le16_to_cpu(mpi_reply->Event);
7861
7862 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7863 mpt3sas_trigger_event(ioc, event, 0);
7864
7865 switch (event) {
7866 /* handle these */
7867 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7868 {
7869 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7870 (Mpi2EventDataSasBroadcastPrimitive_t *)
7871 mpi_reply->EventData;
7872
7873 if (baen_data->Primitive !=
7874 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7875 return 1;
7876
7877 if (ioc->broadcast_aen_busy) {
7878 ioc->broadcast_aen_pending++;
7879 return 1;
7880 } else
7881 ioc->broadcast_aen_busy = 1;
7882 break;
7883 }
7884
7885 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7886 _scsih_check_topo_delete_events(ioc,
7887 (Mpi2EventDataSasTopologyChangeList_t *)
7888 mpi_reply->EventData);
7889 break;
7890 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7891 _scsih_check_ir_config_unhide_events(ioc,
7892 (Mpi2EventDataIrConfigChangeList_t *)
7893 mpi_reply->EventData);
7894 break;
7895 case MPI2_EVENT_IR_VOLUME:
7896 _scsih_check_volume_delete_events(ioc,
7897 (Mpi2EventDataIrVolume_t *)
7898 mpi_reply->EventData);
7899 break;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307900 case MPI2_EVENT_LOG_ENTRY_ADDED:
7901 {
7902 Mpi2EventDataLogEntryAdded_t *log_entry;
7903 u32 *log_code;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307904
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05307905 if (!ioc->is_warpdrive)
7906 break;
7907
7908 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7909 mpi_reply->EventData;
7910 log_code = (u32 *)log_entry->LogData;
7911
7912 if (le16_to_cpu(log_entry->LogEntryQualifier)
7913 != MPT2_WARPDRIVE_LOGENTRY)
7914 break;
7915
7916 switch (le32_to_cpu(*log_code)) {
7917 case MPT2_WARPDRIVE_LC_SSDT:
7918 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7919 "IO Throttling has occurred in the WarpDrive "
7920 "subsystem. Check WarpDrive documentation for "
7921 "additional details.\n", ioc->name);
7922 break;
7923 case MPT2_WARPDRIVE_LC_SSDLW:
7924 pr_warn(MPT3SAS_FMT "WarpDrive Warning: "
7925 "Program/Erase Cycles for the WarpDrive subsystem "
7926 "in degraded range. Check WarpDrive documentation "
7927 "for additional details.\n", ioc->name);
7928 break;
7929 case MPT2_WARPDRIVE_LC_SSDLF:
7930 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7931 "There are no Program/Erase Cycles for the "
7932 "WarpDrive subsystem. The storage device will be "
7933 "in read-only mode. Check WarpDrive documentation "
7934 "for additional details.\n", ioc->name);
7935 break;
7936 case MPT2_WARPDRIVE_LC_BRMF:
7937 pr_err(MPT3SAS_FMT "WarpDrive Fatal Error: "
7938 "The Backup Rail Monitor has failed on the "
7939 "WarpDrive subsystem. Check WarpDrive "
7940 "documentation for additional details.\n",
7941 ioc->name);
7942 break;
7943 }
7944
7945 break;
7946 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307947 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7948 case MPI2_EVENT_IR_OPERATION_STATUS:
7949 case MPI2_EVENT_SAS_DISCOVERY:
7950 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7951 case MPI2_EVENT_IR_PHYSICAL_DISK:
7952 break;
7953
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05307954 case MPI2_EVENT_TEMP_THRESHOLD:
7955 _scsih_temp_threshold_events(ioc,
7956 (Mpi2EventDataTemperature_t *)
7957 mpi_reply->EventData);
7958 break;
7959
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307960 default: /* ignore the rest */
7961 return 1;
7962 }
7963
Joe Lawrence35b62362014-06-25 17:05:34 -04007964 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307965 fw_event = alloc_fw_event_work(sz);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307966 if (!fw_event) {
7967 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7968 ioc->name, __FILE__, __LINE__, __func__);
7969 return 1;
7970 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307971
7972 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7973 fw_event->ioc = ioc;
7974 fw_event->VF_ID = mpi_reply->VF_ID;
7975 fw_event->VP_ID = mpi_reply->VP_ID;
7976 fw_event->event = event;
7977 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy146b16c2015-11-11 17:30:31 +05307978 fw_event_work_put(fw_event);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307979 return 1;
7980}
7981
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307982/**
7983 * _scsih_expander_node_remove - removing expander device from list.
7984 * @ioc: per adapter object
7985 * @sas_expander: the sas_device object
7986 * Context: Calling function should acquire ioc->sas_node_lock.
7987 *
7988 * Removing object and freeing associated memory from the
7989 * ioc->sas_expander_list.
7990 *
7991 * Return nothing.
7992 */
7993static void
7994_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7995 struct _sas_node *sas_expander)
7996{
7997 struct _sas_port *mpt3sas_port, *next;
7998
7999 /* remove sibling ports attached to this expander */
8000 list_for_each_entry_safe(mpt3sas_port, next,
8001 &sas_expander->sas_port_list, port_list) {
8002 if (ioc->shost_recovery)
8003 return;
8004 if (mpt3sas_port->remote_identify.device_type ==
8005 SAS_END_DEVICE)
8006 mpt3sas_device_remove_by_sas_address(ioc,
8007 mpt3sas_port->remote_identify.sas_address);
8008 else if (mpt3sas_port->remote_identify.device_type ==
8009 SAS_EDGE_EXPANDER_DEVICE ||
8010 mpt3sas_port->remote_identify.device_type ==
8011 SAS_FANOUT_EXPANDER_DEVICE)
8012 mpt3sas_expander_remove(ioc,
8013 mpt3sas_port->remote_identify.sas_address);
8014 }
8015
8016 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
8017 sas_expander->sas_address_parent);
8018
8019 pr_info(MPT3SAS_FMT
8020 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
8021 ioc->name,
8022 sas_expander->handle, (unsigned long long)
8023 sas_expander->sas_address);
8024
8025 kfree(sas_expander->phy);
8026 kfree(sas_expander);
8027}
8028
8029/**
8030 * _scsih_ir_shutdown - IR shutdown notification
8031 * @ioc: per adapter object
8032 *
8033 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
8034 * the host system is shutting down.
8035 *
8036 * Return nothing.
8037 */
8038static void
8039_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
8040{
8041 Mpi2RaidActionRequest_t *mpi_request;
8042 Mpi2RaidActionReply_t *mpi_reply;
8043 u16 smid;
8044
8045 /* is IR firmware build loaded ? */
8046 if (!ioc->ir_firmware)
8047 return;
8048
8049 /* are there any volumes ? */
8050 if (list_empty(&ioc->raid_device_list))
8051 return;
8052
8053 mutex_lock(&ioc->scsih_cmds.mutex);
8054
8055 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
8056 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
8057 ioc->name, __func__);
8058 goto out;
8059 }
8060 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
8061
8062 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
8063 if (!smid) {
8064 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
8065 ioc->name, __func__);
8066 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8067 goto out;
8068 }
8069
8070 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
8071 ioc->scsih_cmds.smid = smid;
8072 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
8073
8074 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
8075 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
8076
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308077 if (!ioc->hide_ir_msg)
8078 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308079 init_completion(&ioc->scsih_cmds.done);
8080 mpt3sas_base_put_smid_default(ioc, smid);
8081 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
8082
8083 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
8084 pr_err(MPT3SAS_FMT "%s: timeout\n",
8085 ioc->name, __func__);
8086 goto out;
8087 }
8088
8089 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
8090 mpi_reply = ioc->scsih_cmds.reply;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308091 if (!ioc->hide_ir_msg)
8092 pr_info(MPT3SAS_FMT "IR shutdown "
8093 "(complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
8094 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
8095 le32_to_cpu(mpi_reply->IOCLogInfo));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308096 }
8097
8098 out:
8099 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
8100 mutex_unlock(&ioc->scsih_cmds.mutex);
8101}
8102
8103/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308104 * scsih_remove - detach and remove add host
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308105 * @pdev: PCI device struct
8106 *
8107 * Routine called when unloading the driver.
8108 * Return nothing.
8109 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308110void scsih_remove(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308111{
8112 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8113 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8114 struct _sas_port *mpt3sas_port, *next_port;
8115 struct _raid_device *raid_device, *next;
8116 struct MPT3SAS_TARGET *sas_target_priv_data;
8117 struct workqueue_struct *wq;
8118 unsigned long flags;
8119
8120 ioc->remove_host = 1;
8121 _scsih_fw_event_cleanup_queue(ioc);
8122
8123 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8124 wq = ioc->firmware_event_thread;
8125 ioc->firmware_event_thread = NULL;
8126 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8127 if (wq)
8128 destroy_workqueue(wq);
8129
8130 /* release all the volumes */
8131 _scsih_ir_shutdown(ioc);
8132 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
8133 list) {
8134 if (raid_device->starget) {
8135 sas_target_priv_data =
8136 raid_device->starget->hostdata;
8137 sas_target_priv_data->deleted = 1;
8138 scsi_remove_target(&raid_device->starget->dev);
8139 }
8140 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
8141 ioc->name, raid_device->handle,
8142 (unsigned long long) raid_device->wwid);
8143 _scsih_raid_device_remove(ioc, raid_device);
8144 }
8145
8146 /* free ports attached to the sas_host */
8147 list_for_each_entry_safe(mpt3sas_port, next_port,
8148 &ioc->sas_hba.sas_port_list, port_list) {
8149 if (mpt3sas_port->remote_identify.device_type ==
8150 SAS_END_DEVICE)
8151 mpt3sas_device_remove_by_sas_address(ioc,
8152 mpt3sas_port->remote_identify.sas_address);
8153 else if (mpt3sas_port->remote_identify.device_type ==
8154 SAS_EDGE_EXPANDER_DEVICE ||
8155 mpt3sas_port->remote_identify.device_type ==
8156 SAS_FANOUT_EXPANDER_DEVICE)
8157 mpt3sas_expander_remove(ioc,
8158 mpt3sas_port->remote_identify.sas_address);
8159 }
8160
8161 /* free phys attached to the sas_host */
8162 if (ioc->sas_hba.num_phys) {
8163 kfree(ioc->sas_hba.phy);
8164 ioc->sas_hba.phy = NULL;
8165 ioc->sas_hba.num_phys = 0;
8166 }
8167
8168 sas_remove_host(shost);
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308169 scsi_remove_host(shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308170 mpt3sas_base_detach(ioc);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308171 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308172 list_del(&ioc->list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308173 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308174 scsi_host_put(shost);
8175}
8176
8177/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308178 * scsih_shutdown - routine call during system shutdown
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308179 * @pdev: PCI device struct
8180 *
8181 * Return nothing.
8182 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308183void
8184scsih_shutdown(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308185{
8186 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8187 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8188 struct workqueue_struct *wq;
8189 unsigned long flags;
8190
8191 ioc->remove_host = 1;
8192 _scsih_fw_event_cleanup_queue(ioc);
8193
8194 spin_lock_irqsave(&ioc->fw_event_lock, flags);
8195 wq = ioc->firmware_event_thread;
8196 ioc->firmware_event_thread = NULL;
8197 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
8198 if (wq)
8199 destroy_workqueue(wq);
8200
8201 _scsih_ir_shutdown(ioc);
8202 mpt3sas_base_detach(ioc);
8203}
8204
8205
8206/**
8207 * _scsih_probe_boot_devices - reports 1st device
8208 * @ioc: per adapter object
8209 *
8210 * If specified in bios page 2, this routine reports the 1st
8211 * device scsi-ml or sas transport for persistent boot device
8212 * purposes. Please refer to function _scsih_determine_boot_device()
8213 */
8214static void
8215_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
8216{
8217 u8 is_raid;
8218 void *device;
8219 struct _sas_device *sas_device;
8220 struct _raid_device *raid_device;
8221 u16 handle;
8222 u64 sas_address_parent;
8223 u64 sas_address;
8224 unsigned long flags;
8225 int rc;
8226
8227 /* no Bios, return immediately */
8228 if (!ioc->bios_pg3.BiosVersion)
8229 return;
8230
8231 device = NULL;
8232 is_raid = 0;
8233 if (ioc->req_boot_device.device) {
8234 device = ioc->req_boot_device.device;
8235 is_raid = ioc->req_boot_device.is_raid;
8236 } else if (ioc->req_alt_boot_device.device) {
8237 device = ioc->req_alt_boot_device.device;
8238 is_raid = ioc->req_alt_boot_device.is_raid;
8239 } else if (ioc->current_boot_device.device) {
8240 device = ioc->current_boot_device.device;
8241 is_raid = ioc->current_boot_device.is_raid;
8242 }
8243
8244 if (!device)
8245 return;
8246
8247 if (is_raid) {
8248 raid_device = device;
8249 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8250 raid_device->id, 0);
8251 if (rc)
8252 _scsih_raid_device_remove(ioc, raid_device);
8253 } else {
8254 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8255 sas_device = device;
8256 handle = sas_device->handle;
8257 sas_address_parent = sas_device->sas_address_parent;
8258 sas_address = sas_device->sas_address;
8259 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8260 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8261
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308262 if (ioc->hide_drives)
8263 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308264 if (!mpt3sas_transport_port_add(ioc, handle,
8265 sas_address_parent)) {
8266 _scsih_sas_device_remove(ioc, sas_device);
8267 } else if (!sas_device->starget) {
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308268 if (!ioc->is_driver_loading) {
8269 mpt3sas_transport_port_remove(ioc,
8270 sas_address,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308271 sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308272 _scsih_sas_device_remove(ioc, sas_device);
8273 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308274 }
8275 }
8276}
8277
8278/**
8279 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8280 * @ioc: per adapter object
8281 *
8282 * Called during initial loading of the driver.
8283 */
8284static void
8285_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
8286{
8287 struct _raid_device *raid_device, *raid_next;
8288 int rc;
8289
8290 list_for_each_entry_safe(raid_device, raid_next,
8291 &ioc->raid_device_list, list) {
8292 if (raid_device->starget)
8293 continue;
8294 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8295 raid_device->id, 0);
8296 if (rc)
8297 _scsih_raid_device_remove(ioc, raid_device);
8298 }
8299}
8300
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308301static struct _sas_device *get_next_sas_device(struct MPT3SAS_ADAPTER *ioc)
8302{
8303 struct _sas_device *sas_device = NULL;
8304 unsigned long flags;
8305
8306 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8307 if (!list_empty(&ioc->sas_device_init_list)) {
8308 sas_device = list_first_entry(&ioc->sas_device_init_list,
8309 struct _sas_device, list);
8310 sas_device_get(sas_device);
8311 }
8312 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8313
8314 return sas_device;
8315}
8316
8317static void sas_device_make_active(struct MPT3SAS_ADAPTER *ioc,
8318 struct _sas_device *sas_device)
8319{
8320 unsigned long flags;
8321
8322 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8323
8324 /*
8325 * Since we dropped the lock during the call to port_add(), we need to
8326 * be careful here that somebody else didn't move or delete this item
8327 * while we were busy with other things.
8328 *
8329 * If it was on the list, we need a put() for the reference the list
8330 * had. Either way, we need a get() for the destination list.
8331 */
8332 if (!list_empty(&sas_device->list)) {
8333 list_del_init(&sas_device->list);
8334 sas_device_put(sas_device);
8335 }
8336
8337 sas_device_get(sas_device);
8338 list_add_tail(&sas_device->list, &ioc->sas_device_list);
8339
8340 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
8341}
8342
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308343/**
8344 * _scsih_probe_sas - reporting sas devices to sas transport
8345 * @ioc: per adapter object
8346 *
8347 * Called during initial loading of the driver.
8348 */
8349static void
8350_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
8351{
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308352 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308353
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308354 if (ioc->hide_drives)
8355 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308356
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308357 while ((sas_device = get_next_sas_device(ioc))) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308358 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
8359 sas_device->sas_address_parent)) {
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308360 _scsih_sas_device_remove(ioc, sas_device);
8361 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308362 continue;
8363 } else if (!sas_device->starget) {
8364 /*
8365 * When asyn scanning is enabled, its not possible to
8366 * remove devices while scanning is turned on due to an
8367 * oops in scsi_sysfs_add_sdev()->add_device()->
8368 * sysfs_addrm_start()
8369 */
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308370 if (!ioc->is_driver_loading) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308371 mpt3sas_transport_port_remove(ioc,
8372 sas_device->sas_address,
8373 sas_device->sas_address_parent);
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308374 _scsih_sas_device_remove(ioc, sas_device);
8375 sas_device_put(sas_device);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05308376 continue;
8377 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308378 }
Sreekanth Reddyd1cb5e42015-11-11 17:30:30 +05308379 sas_device_make_active(ioc, sas_device);
8380 sas_device_put(sas_device);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308381 }
8382}
8383
8384/**
8385 * _scsih_probe_devices - probing for devices
8386 * @ioc: per adapter object
8387 *
8388 * Called during initial loading of the driver.
8389 */
8390static void
8391_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
8392{
8393 u16 volume_mapping_flags;
8394
8395 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8396 return; /* return when IOC doesn't support initiator mode */
8397
8398 _scsih_probe_boot_devices(ioc);
8399
8400 if (ioc->ir_firmware) {
8401 volume_mapping_flags =
8402 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8403 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8404 if (volume_mapping_flags ==
8405 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
8406 _scsih_probe_raid(ioc);
8407 _scsih_probe_sas(ioc);
8408 } else {
8409 _scsih_probe_sas(ioc);
8410 _scsih_probe_raid(ioc);
8411 }
8412 } else
8413 _scsih_probe_sas(ioc);
8414}
8415
8416/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308417 * scsih_scan_start - scsi lld callback for .scan_start
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308418 * @shost: SCSI host pointer
8419 *
8420 * The shost has the ability to discover targets on its own instead
8421 * of scanning the entire bus. In our implemention, we will kick off
8422 * firmware discovery.
8423 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308424void
8425scsih_scan_start(struct Scsi_Host *shost)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308426{
8427 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8428 int rc;
8429 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8430 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
8431
8432 if (disable_discovery > 0)
8433 return;
8434
8435 ioc->start_scan = 1;
8436 rc = mpt3sas_port_enable(ioc);
8437
8438 if (rc != 0)
8439 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
8440}
8441
8442/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308443 * scsih_scan_finished - scsi lld callback for .scan_finished
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308444 * @shost: SCSI host pointer
8445 * @time: elapsed time of the scan in jiffies
8446 *
8447 * This function will be called periodicallyn until it returns 1 with the
8448 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8449 * we wait for firmware discovery to complete, then return 1.
8450 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308451int
8452scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308453{
8454 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8455
8456 if (disable_discovery > 0) {
8457 ioc->is_driver_loading = 0;
8458 ioc->wait_for_discovery_to_complete = 0;
8459 return 1;
8460 }
8461
8462 if (time >= (300 * HZ)) {
8463 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8464 pr_info(MPT3SAS_FMT
8465 "port enable: FAILED with timeout (timeout=300s)\n",
8466 ioc->name);
8467 ioc->is_driver_loading = 0;
8468 return 1;
8469 }
8470
8471 if (ioc->start_scan)
8472 return 0;
8473
8474 if (ioc->start_scan_failed) {
8475 pr_info(MPT3SAS_FMT
8476 "port enable: FAILED with (ioc_status=0x%08x)\n",
8477 ioc->name, ioc->start_scan_failed);
8478 ioc->is_driver_loading = 0;
8479 ioc->wait_for_discovery_to_complete = 0;
8480 ioc->remove_host = 1;
8481 return 1;
8482 }
8483
8484 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
8485 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
8486
8487 if (ioc->wait_for_discovery_to_complete) {
8488 ioc->wait_for_discovery_to_complete = 0;
8489 _scsih_probe_devices(ioc);
8490 }
8491 mpt3sas_base_start_watchdog(ioc);
8492 ioc->is_driver_loading = 0;
8493 return 1;
8494}
8495
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308496/* shost template for SAS 2.0 HBA devices */
8497static struct scsi_host_template mpt2sas_driver_template = {
8498 .module = THIS_MODULE,
8499 .name = "Fusion MPT SAS Host",
8500 .proc_name = MPT2SAS_DRIVER_NAME,
8501 .queuecommand = scsih_qcmd,
8502 .target_alloc = scsih_target_alloc,
8503 .slave_alloc = scsih_slave_alloc,
8504 .slave_configure = scsih_slave_configure,
8505 .target_destroy = scsih_target_destroy,
8506 .slave_destroy = scsih_slave_destroy,
8507 .scan_finished = scsih_scan_finished,
8508 .scan_start = scsih_scan_start,
8509 .change_queue_depth = scsih_change_queue_depth,
8510 .eh_abort_handler = scsih_abort,
8511 .eh_device_reset_handler = scsih_dev_reset,
8512 .eh_target_reset_handler = scsih_target_reset,
8513 .eh_host_reset_handler = scsih_host_reset,
8514 .bios_param = scsih_bios_param,
8515 .can_queue = 1,
8516 .this_id = -1,
8517 .sg_tablesize = MPT2SAS_SG_DEPTH,
8518 .max_sectors = 32767,
8519 .cmd_per_lun = 7,
8520 .use_clustering = ENABLE_CLUSTERING,
8521 .shost_attrs = mpt3sas_host_attrs,
8522 .sdev_attrs = mpt3sas_dev_attrs,
8523 .track_queue_depth = 1,
8524};
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308525
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308526/* raid transport support for SAS 2.0 HBA devices */
8527static struct raid_function_template mpt2sas_raid_functions = {
8528 .cookie = &mpt2sas_driver_template,
8529 .is_raid = scsih_is_raid,
8530 .get_resync = scsih_get_resync,
8531 .get_state = scsih_get_state,
8532};
8533
8534/* shost template for SAS 3.0 HBA devices */
8535static struct scsi_host_template mpt3sas_driver_template = {
8536 .module = THIS_MODULE,
8537 .name = "Fusion MPT SAS Host",
8538 .proc_name = MPT3SAS_DRIVER_NAME,
8539 .queuecommand = scsih_qcmd,
8540 .target_alloc = scsih_target_alloc,
8541 .slave_alloc = scsih_slave_alloc,
8542 .slave_configure = scsih_slave_configure,
8543 .target_destroy = scsih_target_destroy,
8544 .slave_destroy = scsih_slave_destroy,
8545 .scan_finished = scsih_scan_finished,
8546 .scan_start = scsih_scan_start,
8547 .change_queue_depth = scsih_change_queue_depth,
8548 .eh_abort_handler = scsih_abort,
8549 .eh_device_reset_handler = scsih_dev_reset,
8550 .eh_target_reset_handler = scsih_target_reset,
8551 .eh_host_reset_handler = scsih_host_reset,
8552 .bios_param = scsih_bios_param,
8553 .can_queue = 1,
8554 .this_id = -1,
8555 .sg_tablesize = MPT3SAS_SG_DEPTH,
8556 .max_sectors = 32767,
8557 .cmd_per_lun = 7,
8558 .use_clustering = ENABLE_CLUSTERING,
8559 .shost_attrs = mpt3sas_host_attrs,
8560 .sdev_attrs = mpt3sas_dev_attrs,
8561 .track_queue_depth = 1,
8562};
8563
8564/* raid transport support for SAS 3.0 HBA devices */
8565static struct raid_function_template mpt3sas_raid_functions = {
8566 .cookie = &mpt3sas_driver_template,
8567 .is_raid = scsih_is_raid,
8568 .get_resync = scsih_get_resync,
8569 .get_state = scsih_get_state,
8570};
8571
8572/**
8573 * _scsih_determine_hba_mpi_version - determine in which MPI version class
8574 * this device belongs to.
8575 * @pdev: PCI device struct
8576 *
8577 * return MPI2_VERSION for SAS 2.0 HBA devices,
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308578 * MPI25_VERSION for SAS 3.0 HBA devices, and
8579 * MPI26 VERSION for Cutlass & Invader SAS 3.0 HBA devices
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308580 */
8581u16
8582_scsih_determine_hba_mpi_version(struct pci_dev *pdev)
8583{
8584
8585 switch (pdev->device) {
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308586 case MPI2_MFGPAGE_DEVID_SSS6200:
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308587 case MPI2_MFGPAGE_DEVID_SAS2004:
8588 case MPI2_MFGPAGE_DEVID_SAS2008:
8589 case MPI2_MFGPAGE_DEVID_SAS2108_1:
8590 case MPI2_MFGPAGE_DEVID_SAS2108_2:
8591 case MPI2_MFGPAGE_DEVID_SAS2108_3:
8592 case MPI2_MFGPAGE_DEVID_SAS2116_1:
8593 case MPI2_MFGPAGE_DEVID_SAS2116_2:
8594 case MPI2_MFGPAGE_DEVID_SAS2208_1:
8595 case MPI2_MFGPAGE_DEVID_SAS2208_2:
8596 case MPI2_MFGPAGE_DEVID_SAS2208_3:
8597 case MPI2_MFGPAGE_DEVID_SAS2208_4:
8598 case MPI2_MFGPAGE_DEVID_SAS2208_5:
8599 case MPI2_MFGPAGE_DEVID_SAS2208_6:
8600 case MPI2_MFGPAGE_DEVID_SAS2308_1:
8601 case MPI2_MFGPAGE_DEVID_SAS2308_2:
8602 case MPI2_MFGPAGE_DEVID_SAS2308_3:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308603 return MPI2_VERSION;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308604 case MPI25_MFGPAGE_DEVID_SAS3004:
8605 case MPI25_MFGPAGE_DEVID_SAS3008:
8606 case MPI25_MFGPAGE_DEVID_SAS3108_1:
8607 case MPI25_MFGPAGE_DEVID_SAS3108_2:
8608 case MPI25_MFGPAGE_DEVID_SAS3108_5:
8609 case MPI25_MFGPAGE_DEVID_SAS3108_6:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308610 return MPI25_VERSION;
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308611 case MPI26_MFGPAGE_DEVID_SAS3216:
8612 case MPI26_MFGPAGE_DEVID_SAS3224:
8613 case MPI26_MFGPAGE_DEVID_SAS3316_1:
8614 case MPI26_MFGPAGE_DEVID_SAS3316_2:
8615 case MPI26_MFGPAGE_DEVID_SAS3316_3:
8616 case MPI26_MFGPAGE_DEVID_SAS3316_4:
8617 case MPI26_MFGPAGE_DEVID_SAS3324_1:
8618 case MPI26_MFGPAGE_DEVID_SAS3324_2:
8619 case MPI26_MFGPAGE_DEVID_SAS3324_3:
8620 case MPI26_MFGPAGE_DEVID_SAS3324_4:
8621 return MPI26_VERSION;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308622 }
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308623 return 0;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308624}
8625
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308626/**
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308627 * _scsih_probe - attach and add scsi host
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308628 * @pdev: PCI device struct
8629 * @id: pci device id
8630 *
8631 * Returns 0 success, anything else error.
8632 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308633int
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308634_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308635{
8636 struct MPT3SAS_ADAPTER *ioc;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308637 struct Scsi_Host *shost = NULL;
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308638 int rv;
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308639 u16 hba_mpi_version;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308640
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308641 /* Determine in which MPI version class this pci device belongs */
8642 hba_mpi_version = _scsih_determine_hba_mpi_version(pdev);
8643 if (hba_mpi_version == 0)
8644 return -ENODEV;
8645
8646 /* Enumerate only SAS 2.0 HBA's if hbas_to_enumerate is one,
8647 * for other generation HBA's return with -ENODEV
8648 */
8649 if ((hbas_to_enumerate == 1) && (hba_mpi_version != MPI2_VERSION))
8650 return -ENODEV;
8651
8652 /* Enumerate only SAS 3.0 HBA's if hbas_to_enumerate is two,
8653 * for other generation HBA's return with -ENODEV
8654 */
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308655 if ((hbas_to_enumerate == 2) && (!(hba_mpi_version == MPI25_VERSION
8656 || hba_mpi_version == MPI26_VERSION)))
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308657 return -ENODEV;
8658
8659 switch (hba_mpi_version) {
8660 case MPI2_VERSION:
8661 /* Use mpt2sas driver host template for SAS 2.0 HBA's */
8662 shost = scsi_host_alloc(&mpt2sas_driver_template,
8663 sizeof(struct MPT3SAS_ADAPTER));
8664 if (!shost)
8665 return -ENODEV;
8666 ioc = shost_priv(shost);
8667 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8668 ioc->hba_mpi_version_belonged = hba_mpi_version;
8669 ioc->id = mpt2_ids++;
8670 sprintf(ioc->driver_name, "%s", MPT2SAS_DRIVER_NAME);
8671 if (pdev->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8672 ioc->is_warpdrive = 1;
8673 ioc->hide_ir_msg = 1;
8674 } else
8675 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
8676 break;
8677 case MPI25_VERSION:
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308678 case MPI26_VERSION:
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308679 /* Use mpt3sas driver host template for SAS 3.0 HBA's */
8680 shost = scsi_host_alloc(&mpt3sas_driver_template,
8681 sizeof(struct MPT3SAS_ADAPTER));
8682 if (!shost)
8683 return -ENODEV;
8684 ioc = shost_priv(shost);
8685 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
8686 ioc->hba_mpi_version_belonged = hba_mpi_version;
8687 ioc->id = mpt3_ids++;
8688 sprintf(ioc->driver_name, "%s", MPT3SAS_DRIVER_NAME);
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05308689 if ((ioc->hba_mpi_version_belonged == MPI25_VERSION &&
8690 pdev->revision >= SAS3_PCI_DEVICE_C0_REVISION) ||
8691 (ioc->hba_mpi_version_belonged == MPI26_VERSION))
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308692 ioc->msix96_vector = 1;
8693 break;
8694 default:
8695 return -ENODEV;
8696 }
8697
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308698 INIT_LIST_HEAD(&ioc->list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308699 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308700 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308701 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308702 ioc->shost = shost;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308703 ioc->pdev = pdev;
8704 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8705 ioc->tm_cb_idx = tm_cb_idx;
8706 ioc->ctl_cb_idx = ctl_cb_idx;
8707 ioc->base_cb_idx = base_cb_idx;
8708 ioc->port_enable_cb_idx = port_enable_cb_idx;
8709 ioc->transport_cb_idx = transport_cb_idx;
8710 ioc->scsih_cb_idx = scsih_cb_idx;
8711 ioc->config_cb_idx = config_cb_idx;
8712 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
8713 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
8714 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
8715 ioc->logging_level = logging_level;
8716 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
8717 /* misc semaphores and spin locks */
8718 mutex_init(&ioc->reset_in_progress_mutex);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308719 /* initializing pci_access_mutex lock */
8720 mutex_init(&ioc->pci_access_mutex);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308721 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8722 spin_lock_init(&ioc->scsi_lookup_lock);
8723 spin_lock_init(&ioc->sas_device_lock);
8724 spin_lock_init(&ioc->sas_node_lock);
8725 spin_lock_init(&ioc->fw_event_lock);
8726 spin_lock_init(&ioc->raid_device_lock);
8727 spin_lock_init(&ioc->diag_trigger_lock);
8728
8729 INIT_LIST_HEAD(&ioc->sas_device_list);
8730 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8731 INIT_LIST_HEAD(&ioc->sas_expander_list);
8732 INIT_LIST_HEAD(&ioc->fw_event_list);
8733 INIT_LIST_HEAD(&ioc->raid_device_list);
8734 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8735 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Suganath prabu Subramanifd0331b2016-01-28 12:07:02 +05308736 INIT_LIST_HEAD(&ioc->delayed_sc_list);
8737 INIT_LIST_HEAD(&ioc->delayed_event_ack_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308738 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04008739 INIT_LIST_HEAD(&ioc->reply_queue_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308740
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308741 sprintf(ioc->name, "%s_cm%d", ioc->driver_name, ioc->id);
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308742
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308743 /* init shost parameters */
8744 shost->max_cmd_len = 32;
8745 shost->max_lun = max_lun;
8746 shost->transportt = mpt3sas_transport_template;
8747 shost->unique_id = ioc->id;
8748
8749 if (max_sectors != 0xFFFF) {
8750 if (max_sectors < 64) {
8751 shost->max_sectors = 64;
8752 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8753 "for max_sectors, range is 64 to 32767. Assigning "
8754 "value of 64.\n", ioc->name, max_sectors);
8755 } else if (max_sectors > 32767) {
8756 shost->max_sectors = 32767;
8757 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8758 "for max_sectors, range is 64 to 32767. Assigning "
8759 "default value of 32767.\n", ioc->name,
8760 max_sectors);
8761 } else {
8762 shost->max_sectors = max_sectors & 0xFFFE;
8763 pr_info(MPT3SAS_FMT
8764 "The max_sectors value is set to %d\n",
8765 ioc->name, shost->max_sectors);
8766 }
8767 }
8768
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308769 /* register EEDP capabilities with SCSI layer */
8770 if (prot_mask > 0)
8771 scsi_host_set_prot(shost, prot_mask);
8772 else
8773 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8774 | SHOST_DIF_TYPE2_PROTECTION
8775 | SHOST_DIF_TYPE3_PROTECTION);
8776
8777 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8778
8779 /* event thread */
8780 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308781 "fw_event_%s%d", ioc->driver_name, ioc->id);
Sreekanth Reddybdff7852015-06-30 12:25:03 +05308782 ioc->firmware_event_thread = alloc_ordered_workqueue(
8783 ioc->firmware_event_name, WQ_MEM_RECLAIM);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308784 if (!ioc->firmware_event_thread) {
8785 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8786 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308787 rv = -ENODEV;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308788 goto out_thread_fail;
8789 }
8790
8791 ioc->is_driver_loading = 1;
8792 if ((mpt3sas_base_attach(ioc))) {
8793 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8794 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308795 rv = -ENODEV;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308796 goto out_attach_fail;
8797 }
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308798
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308799 if (ioc->is_warpdrive) {
8800 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8801 ioc->hide_drives = 0;
8802 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8803 ioc->hide_drives = 1;
8804 else {
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05308805 if (mpt3sas_get_num_volumes(ioc))
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308806 ioc->hide_drives = 1;
8807 else
8808 ioc->hide_drives = 0;
8809 }
8810 } else
8811 ioc->hide_drives = 0;
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05308812
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308813 rv = scsi_add_host(shost, &pdev->dev);
8814 if (rv) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308815 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8816 ioc->name, __FILE__, __LINE__, __func__);
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308817 goto out_add_shost_fail;
8818 }
8819
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308820 scsi_scan_host(shost);
8821 return 0;
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308822out_add_shost_fail:
8823 mpt3sas_base_detach(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308824 out_attach_fail:
8825 destroy_workqueue(ioc->firmware_event_thread);
8826 out_thread_fail:
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308827 spin_lock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308828 list_del(&ioc->list);
Sreekanth Reddy08c4d552015-11-11 17:30:33 +05308829 spin_unlock(&gioc_lock);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308830 scsi_host_put(shost);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308831 return rv;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308832}
8833
8834#ifdef CONFIG_PM
8835/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308836 * scsih_suspend - power management suspend main entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308837 * @pdev: PCI device struct
8838 * @state: PM state change to (usually PCI_D3)
8839 *
8840 * Returns 0 success, anything else error.
8841 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308842int
8843scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308844{
8845 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8846 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8847 pci_power_t device_state;
8848
8849 mpt3sas_base_stop_watchdog(ioc);
8850 flush_scheduled_work();
8851 scsi_block_requests(shost);
8852 device_state = pci_choose_state(pdev, state);
8853 pr_info(MPT3SAS_FMT
8854 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8855 ioc->name, pdev, pci_name(pdev), device_state);
8856
8857 pci_save_state(pdev);
8858 mpt3sas_base_free_resources(ioc);
8859 pci_set_power_state(pdev, device_state);
8860 return 0;
8861}
8862
8863/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308864 * scsih_resume - power management resume main entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308865 * @pdev: PCI device struct
8866 *
8867 * Returns 0 success, anything else error.
8868 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308869int
8870scsih_resume(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308871{
8872 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8873 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8874 pci_power_t device_state = pdev->current_state;
8875 int r;
8876
8877 pr_info(MPT3SAS_FMT
8878 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8879 ioc->name, pdev, pci_name(pdev), device_state);
8880
8881 pci_set_power_state(pdev, PCI_D0);
8882 pci_enable_wake(pdev, PCI_D0, 0);
8883 pci_restore_state(pdev);
8884 ioc->pdev = pdev;
8885 r = mpt3sas_base_map_resources(ioc);
8886 if (r)
8887 return r;
8888
8889 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8890 scsi_unblock_requests(shost);
8891 mpt3sas_base_start_watchdog(ioc);
8892 return 0;
8893}
8894#endif /* CONFIG_PM */
8895
8896/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308897 * scsih_pci_error_detected - Called when a PCI error is detected.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308898 * @pdev: PCI device struct
8899 * @state: PCI channel state
8900 *
8901 * Description: Called when a PCI error is detected.
8902 *
8903 * Return value:
8904 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8905 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308906pci_ers_result_t
8907scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308908{
8909 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8910 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8911
8912 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8913 ioc->name, state);
8914
8915 switch (state) {
8916 case pci_channel_io_normal:
8917 return PCI_ERS_RESULT_CAN_RECOVER;
8918 case pci_channel_io_frozen:
8919 /* Fatal error, prepare for slot reset */
8920 ioc->pci_error_recovery = 1;
8921 scsi_block_requests(ioc->shost);
8922 mpt3sas_base_stop_watchdog(ioc);
8923 mpt3sas_base_free_resources(ioc);
8924 return PCI_ERS_RESULT_NEED_RESET;
8925 case pci_channel_io_perm_failure:
8926 /* Permanent error, prepare for device removal */
8927 ioc->pci_error_recovery = 1;
8928 mpt3sas_base_stop_watchdog(ioc);
8929 _scsih_flush_running_cmds(ioc);
8930 return PCI_ERS_RESULT_DISCONNECT;
8931 }
8932 return PCI_ERS_RESULT_NEED_RESET;
8933}
8934
8935/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308936 * scsih_pci_slot_reset - Called when PCI slot has been reset.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308937 * @pdev: PCI device struct
8938 *
8939 * Description: This routine is called by the pci error recovery
8940 * code after the PCI slot has been reset, just before we
8941 * should resume normal operations.
8942 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308943pci_ers_result_t
8944scsih_pci_slot_reset(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308945{
8946 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8947 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8948 int rc;
8949
8950 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8951 ioc->name);
8952
8953 ioc->pci_error_recovery = 0;
8954 ioc->pdev = pdev;
8955 pci_restore_state(pdev);
8956 rc = mpt3sas_base_map_resources(ioc);
8957 if (rc)
8958 return PCI_ERS_RESULT_DISCONNECT;
8959
8960 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8961 FORCE_BIG_HAMMER);
8962
8963 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8964 (rc == 0) ? "success" : "failed");
8965
8966 if (!rc)
8967 return PCI_ERS_RESULT_RECOVERED;
8968 else
8969 return PCI_ERS_RESULT_DISCONNECT;
8970}
8971
8972/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308973 * scsih_pci_resume() - resume normal ops after PCI reset
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308974 * @pdev: pointer to PCI device
8975 *
8976 * Called when the error recovery driver tells us that its
8977 * OK to resume normal operation. Use completion to allow
8978 * halted scsi ops to resume.
8979 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308980void
8981scsih_pci_resume(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308982{
8983 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8984 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8985
8986 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8987
8988 pci_cleanup_aer_uncorrect_error_status(pdev);
8989 mpt3sas_base_start_watchdog(ioc);
8990 scsi_unblock_requests(ioc->shost);
8991}
8992
8993/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308994 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308995 * @pdev: pointer to PCI device
8996 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308997pci_ers_result_t
8998scsih_pci_mmio_enabled(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308999{
9000 struct Scsi_Host *shost = pci_get_drvdata(pdev);
9001 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
9002
9003 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
9004 ioc->name);
9005
9006 /* TODO - dump whatever for debugging purposes */
9007
9008 /* Request a slot reset. */
9009 return PCI_ERS_RESULT_NEED_RESET;
9010}
9011
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309012/*
9013 * The pci device ids are defined in mpi/mpi2_cnfg.h.
9014 */
9015static const struct pci_device_id mpt3sas_pci_table[] = {
9016 /* Spitfire ~ 2004 */
9017 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
9018 PCI_ANY_ID, PCI_ANY_ID },
9019 /* Falcon ~ 2008 */
9020 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
9021 PCI_ANY_ID, PCI_ANY_ID },
9022 /* Liberator ~ 2108 */
9023 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
9024 PCI_ANY_ID, PCI_ANY_ID },
9025 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
9026 PCI_ANY_ID, PCI_ANY_ID },
9027 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
9028 PCI_ANY_ID, PCI_ANY_ID },
9029 /* Meteor ~ 2116 */
9030 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
9031 PCI_ANY_ID, PCI_ANY_ID },
9032 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
9033 PCI_ANY_ID, PCI_ANY_ID },
9034 /* Thunderbolt ~ 2208 */
9035 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
9036 PCI_ANY_ID, PCI_ANY_ID },
9037 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
9038 PCI_ANY_ID, PCI_ANY_ID },
9039 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
9040 PCI_ANY_ID, PCI_ANY_ID },
9041 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
9042 PCI_ANY_ID, PCI_ANY_ID },
9043 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
9044 PCI_ANY_ID, PCI_ANY_ID },
9045 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
9046 PCI_ANY_ID, PCI_ANY_ID },
9047 /* Mustang ~ 2308 */
9048 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
9049 PCI_ANY_ID, PCI_ANY_ID },
9050 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
9051 PCI_ANY_ID, PCI_ANY_ID },
9052 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
9053 PCI_ANY_ID, PCI_ANY_ID },
9054 /* SSS6200 */
9055 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
9056 PCI_ANY_ID, PCI_ANY_ID },
9057 /* Fury ~ 3004 and 3008 */
9058 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3004,
9059 PCI_ANY_ID, PCI_ANY_ID },
9060 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3008,
9061 PCI_ANY_ID, PCI_ANY_ID },
9062 /* Invader ~ 3108 */
9063 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_1,
9064 PCI_ANY_ID, PCI_ANY_ID },
9065 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_2,
9066 PCI_ANY_ID, PCI_ANY_ID },
9067 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_5,
9068 PCI_ANY_ID, PCI_ANY_ID },
9069 { MPI2_MFGPAGE_VENDORID_LSI, MPI25_MFGPAGE_DEVID_SAS3108_6,
9070 PCI_ANY_ID, PCI_ANY_ID },
Suganath prabu Subramanib130b0d2016-01-28 12:06:58 +05309071 /* Cutlass ~ 3216 and 3224 */
9072 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3216,
9073 PCI_ANY_ID, PCI_ANY_ID },
9074 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3224,
9075 PCI_ANY_ID, PCI_ANY_ID },
9076 /* Intruder ~ 3316 and 3324 */
9077 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_1,
9078 PCI_ANY_ID, PCI_ANY_ID },
9079 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_2,
9080 PCI_ANY_ID, PCI_ANY_ID },
9081 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_3,
9082 PCI_ANY_ID, PCI_ANY_ID },
9083 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3316_4,
9084 PCI_ANY_ID, PCI_ANY_ID },
9085 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_1,
9086 PCI_ANY_ID, PCI_ANY_ID },
9087 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_2,
9088 PCI_ANY_ID, PCI_ANY_ID },
9089 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_3,
9090 PCI_ANY_ID, PCI_ANY_ID },
9091 { MPI2_MFGPAGE_VENDORID_LSI, MPI26_MFGPAGE_DEVID_SAS3324_4,
9092 PCI_ANY_ID, PCI_ANY_ID },
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309093 {0} /* Terminating entry */
9094};
9095MODULE_DEVICE_TABLE(pci, mpt3sas_pci_table);
9096
9097static struct pci_error_handlers _mpt3sas_err_handler = {
9098 .error_detected = scsih_pci_error_detected,
9099 .mmio_enabled = scsih_pci_mmio_enabled,
9100 .slot_reset = scsih_pci_slot_reset,
9101 .resume = scsih_pci_resume,
9102};
9103
9104static struct pci_driver mpt3sas_driver = {
9105 .name = MPT3SAS_DRIVER_NAME,
9106 .id_table = mpt3sas_pci_table,
9107 .probe = _scsih_probe,
9108 .remove = scsih_remove,
9109 .shutdown = scsih_shutdown,
9110 .err_handler = &_mpt3sas_err_handler,
9111#ifdef CONFIG_PM
9112 .suspend = scsih_suspend,
9113 .resume = scsih_resume,
9114#endif
9115};
9116
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309117/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309118 * scsih_init - main entry point for this driver.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309119 *
9120 * Returns 0 success, anything else error.
9121 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309122int
9123scsih_init(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309124{
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309125 mpt2_ids = 0;
9126 mpt3_ids = 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309127
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309128 mpt3sas_base_initialize_callback_handler();
9129
9130 /* queuecommand callback hander */
9131 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
9132
9133 /* task managment callback handler */
9134 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
9135
9136 /* base internal commands callback handler */
9137 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
9138 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
9139 mpt3sas_port_enable_done);
9140
9141 /* transport internal commands callback handler */
9142 transport_cb_idx = mpt3sas_base_register_callback_handler(
9143 mpt3sas_transport_done);
9144
9145 /* scsih internal commands callback handler */
9146 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
9147
9148 /* configuration page API internal commands callback handler */
9149 config_cb_idx = mpt3sas_base_register_callback_handler(
9150 mpt3sas_config_done);
9151
9152 /* ctl module callback handler */
9153 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
9154
9155 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
9156 _scsih_tm_tr_complete);
9157
9158 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
9159 _scsih_tm_volume_tr_complete);
9160
9161 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
9162 _scsih_sas_control_complete);
9163
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05309164 return 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309165}
9166
9167/**
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05309168 * scsih_exit - exit point for this driver (when it is a module).
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309169 *
9170 * Returns 0 success, anything else error.
9171 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05309172void
9173scsih_exit(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309174{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309175
9176 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
9177 mpt3sas_base_release_callback_handler(tm_cb_idx);
9178 mpt3sas_base_release_callback_handler(base_cb_idx);
9179 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
9180 mpt3sas_base_release_callback_handler(transport_cb_idx);
9181 mpt3sas_base_release_callback_handler(scsih_cb_idx);
9182 mpt3sas_base_release_callback_handler(config_cb_idx);
9183 mpt3sas_base_release_callback_handler(ctl_cb_idx);
9184
9185 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
9186 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
9187 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
9188
9189/* raid transport support */
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309190 if (hbas_to_enumerate != 1)
9191 raid_class_release(mpt3sas_raid_template);
9192 if (hbas_to_enumerate != 2)
9193 raid_class_release(mpt2sas_raid_template);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05309194 sas_release_transport(mpt3sas_transport_template);
9195}
Sreekanth Reddy7786ab62015-11-11 17:30:28 +05309196
Sreekanth Reddyc84b06a2015-11-11 17:30:35 +05309197/**
9198 * _mpt3sas_init - main entry point for this driver.
9199 *
9200 * Returns 0 success, anything else error.
9201 */
9202static int __init
9203_mpt3sas_init(void)
9204{
9205 int error;
9206
9207 pr_info("%s version %s loaded\n", MPT3SAS_DRIVER_NAME,
9208 MPT3SAS_DRIVER_VERSION);
9209
9210 mpt3sas_transport_template =
9211 sas_attach_transport(&mpt3sas_transport_functions);
9212 if (!mpt3sas_transport_template)
9213 return -ENODEV;
9214
9215 /* No need attach mpt3sas raid functions template
9216 * if hbas_to_enumarate value is one.
9217 */
9218 if (hbas_to_enumerate != 1) {
9219 mpt3sas_raid_template =
9220 raid_class_attach(&mpt3sas_raid_functions);
9221 if (!mpt3sas_raid_template) {
9222 sas_release_transport(mpt3sas_transport_template);
9223 return -ENODEV;
9224 }
9225 }
9226
9227 /* No need to attach mpt2sas raid functions template
9228 * if hbas_to_enumarate value is two
9229 */
9230 if (hbas_to_enumerate != 2) {
9231 mpt2sas_raid_template =
9232 raid_class_attach(&mpt2sas_raid_functions);
9233 if (!mpt2sas_raid_template) {
9234 sas_release_transport(mpt3sas_transport_template);
9235 return -ENODEV;
9236 }
9237 }
9238
9239 error = scsih_init();
9240 if (error) {
9241 scsih_exit();
9242 return error;
9243 }
9244
9245 mpt3sas_ctl_init(hbas_to_enumerate);
9246
9247 error = pci_register_driver(&mpt3sas_driver);
9248 if (error)
9249 scsih_exit();
9250
9251 return error;
9252}
9253
9254/**
9255 * _mpt3sas_exit - exit point for this driver (when it is a module).
9256 *
9257 */
9258static void __exit
9259_mpt3sas_exit(void)
9260{
9261 pr_info("mpt3sas version %s unloading\n",
9262 MPT3SAS_DRIVER_VERSION);
9263
9264 pci_unregister_driver(&mpt3sas_driver);
9265
9266 mpt3sas_ctl_exit(hbas_to_enumerate);
9267
9268 scsih_exit();
9269}
9270
9271module_init(_mpt3sas_init);
9272module_exit(_mpt3sas_exit);