blob: 233627d23d0fe56c2529299363497fe12542e629 [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>
57
58#include "mpt3sas_base.h"
59
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053060#define RAID_CHANNEL 1
61/* forward proto's */
62static void _scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
63 struct _sas_node *sas_expander);
64static void _firmware_event_work(struct work_struct *work);
65
66static void _scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
67 struct _sas_device *sas_device);
68static int _scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle,
69 u8 retry_count, u8 is_pd);
70
71static u8 _scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid);
72
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053073
74/* global parameters */
75LIST_HEAD(mpt3sas_ioc_list);
Sreekanth Reddyd357e842015-11-11 17:30:22 +053076char driver_name[MPT_NAME_LENGTH];
Sreekanth Reddyf92363d2012-11-30 07:44:21 +053077
78/* local parameters */
79static u8 scsi_io_cb_idx = -1;
80static u8 tm_cb_idx = -1;
81static u8 ctl_cb_idx = -1;
82static u8 base_cb_idx = -1;
83static u8 port_enable_cb_idx = -1;
84static u8 transport_cb_idx = -1;
85static u8 scsih_cb_idx = -1;
86static u8 config_cb_idx = -1;
87static int mpt_ids;
88
89static u8 tm_tr_cb_idx = -1 ;
90static u8 tm_tr_volume_cb_idx = -1 ;
91static u8 tm_sas_control_cb_idx = -1;
92
93/* command line options */
94static u32 logging_level;
95MODULE_PARM_DESC(logging_level,
96 " bits for enabling additional logging info (default=0)");
97
98
99static ushort max_sectors = 0xFFFF;
100module_param(max_sectors, ushort, 0);
101MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
102
103
104static int missing_delay[2] = {-1, -1};
105module_param_array(missing_delay, int, NULL, 0);
106MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
107
108/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
109#define MPT3SAS_MAX_LUN (16895)
Hannes Reinecke1abf6352014-06-25 15:27:38 +0200110static u64 max_lun = MPT3SAS_MAX_LUN;
111module_param(max_lun, ullong, 0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530112MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
113
114
115
116
117/* diag_buffer_enable is bitwise
118 * bit 0 set = TRACE
119 * bit 1 set = SNAPSHOT
120 * bit 2 set = EXTENDED
121 *
122 * Either bit can be set, or both
123 */
124static int diag_buffer_enable = -1;
125module_param(diag_buffer_enable, int, 0);
126MODULE_PARM_DESC(diag_buffer_enable,
127 " post diag buffers (TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
128static int disable_discovery = -1;
129module_param(disable_discovery, int, 0);
130MODULE_PARM_DESC(disable_discovery, " disable discovery ");
131
132
133/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
134static int prot_mask = -1;
135module_param(prot_mask, int, 0);
136MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
137
138
139/* raid transport support */
Sreekanth Reddy7497392a2015-11-11 17:30:19 +0530140struct raid_template *mpt3sas_raid_template;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530141
142
143/**
144 * struct sense_info - common structure for obtaining sense keys
145 * @skey: sense key
146 * @asc: additional sense code
147 * @ascq: additional sense code qualifier
148 */
149struct sense_info {
150 u8 skey;
151 u8 asc;
152 u8 ascq;
153};
154
155#define MPT3SAS_PROCESS_TRIGGER_DIAG (0xFFFB)
Sreekanth Reddy0f624c32014-09-12 15:35:26 +0530156#define MPT3SAS_TURN_ON_PFA_LED (0xFFFC)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530157#define MPT3SAS_PORT_ENABLE_COMPLETE (0xFFFD)
158#define MPT3SAS_ABRT_TASK_SET (0xFFFE)
159#define MPT3SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
160/**
161 * struct fw_event_work - firmware event struct
162 * @list: link list framework
163 * @work: work object (ioc->fault_reset_work_q)
164 * @cancel_pending_work: flag set during reset handling
165 * @ioc: per adapter object
166 * @device_handle: device handle
167 * @VF_ID: virtual function id
168 * @VP_ID: virtual port id
169 * @ignore: flag meaning this event has been marked to ignore
170 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
171 * @event_data: reply event data payload follows
172 *
173 * This object stored on ioc->fw_event_list.
174 */
175struct fw_event_work {
176 struct list_head list;
177 struct work_struct work;
178 u8 cancel_pending_work;
179 struct delayed_work delayed_work;
180
181 struct MPT3SAS_ADAPTER *ioc;
182 u16 device_handle;
183 u8 VF_ID;
184 u8 VP_ID;
185 u8 ignore;
186 u16 event;
Joe Lawrence35b62362014-06-25 17:05:34 -0400187 char event_data[0] __aligned(4);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530188};
189
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530190/**
191 * struct _scsi_io_transfer - scsi io transfer
192 * @handle: sas device handle (assigned by firmware)
193 * @is_raid: flag set for hidden raid components
194 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
195 * @data_length: data transfer length
196 * @data_dma: dma pointer to data
197 * @sense: sense data
198 * @lun: lun number
199 * @cdb_length: cdb length
200 * @cdb: cdb contents
201 * @timeout: timeout for this command
202 * @VF_ID: virtual function id
203 * @VP_ID: virtual port id
204 * @valid_reply: flag set for reply message
205 * @sense_length: sense length
206 * @ioc_status: ioc status
207 * @scsi_state: scsi state
208 * @scsi_status: scsi staus
209 * @log_info: log information
210 * @transfer_length: data length transfer when there is a reply message
211 *
212 * Used for sending internal scsi commands to devices within this module.
213 * Refer to _scsi_send_scsi_io().
214 */
215struct _scsi_io_transfer {
216 u16 handle;
217 u8 is_raid;
218 enum dma_data_direction dir;
219 u32 data_length;
220 dma_addr_t data_dma;
221 u8 sense[SCSI_SENSE_BUFFERSIZE];
222 u32 lun;
223 u8 cdb_length;
224 u8 cdb[32];
225 u8 timeout;
226 u8 VF_ID;
227 u8 VP_ID;
228 u8 valid_reply;
229 /* the following bits are only valid when 'valid_reply = 1' */
230 u32 sense_length;
231 u16 ioc_status;
232 u8 scsi_state;
233 u8 scsi_status;
234 u32 log_info;
235 u32 transfer_length;
236};
237
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530238/**
239 * _scsih_set_debug_level - global setting of ioc->logging_level.
240 *
241 * Note: The logging levels are defined in mpt3sas_debug.h.
242 */
243static int
244_scsih_set_debug_level(const char *val, struct kernel_param *kp)
245{
246 int ret = param_set_int(val, kp);
247 struct MPT3SAS_ADAPTER *ioc;
248
249 if (ret)
250 return ret;
251
252 pr_info("setting logging_level(0x%08x)\n", logging_level);
253 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
254 ioc->logging_level = logging_level;
255 return 0;
256}
257module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
258 &logging_level, 0644);
259
260/**
261 * _scsih_srch_boot_sas_address - search based on sas_address
262 * @sas_address: sas address
263 * @boot_device: boot device object from bios page 2
264 *
265 * Returns 1 when there's a match, 0 means no match.
266 */
267static inline int
268_scsih_srch_boot_sas_address(u64 sas_address,
269 Mpi2BootDeviceSasWwid_t *boot_device)
270{
271 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
272}
273
274/**
275 * _scsih_srch_boot_device_name - search based on device name
276 * @device_name: device name specified in INDENTIFY fram
277 * @boot_device: boot device object from bios page 2
278 *
279 * Returns 1 when there's a match, 0 means no match.
280 */
281static inline int
282_scsih_srch_boot_device_name(u64 device_name,
283 Mpi2BootDeviceDeviceName_t *boot_device)
284{
285 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
286}
287
288/**
289 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
290 * @enclosure_logical_id: enclosure logical id
291 * @slot_number: slot number
292 * @boot_device: boot device object from bios page 2
293 *
294 * Returns 1 when there's a match, 0 means no match.
295 */
296static inline int
297_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
298 Mpi2BootDeviceEnclosureSlot_t *boot_device)
299{
300 return (enclosure_logical_id == le64_to_cpu(boot_device->
301 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
302 SlotNumber)) ? 1 : 0;
303}
304
305/**
306 * _scsih_is_boot_device - search for matching boot device.
307 * @sas_address: sas address
308 * @device_name: device name specified in INDENTIFY fram
309 * @enclosure_logical_id: enclosure logical id
310 * @slot_number: slot number
311 * @form: specifies boot device form
312 * @boot_device: boot device object from bios page 2
313 *
314 * Returns 1 when there's a match, 0 means no match.
315 */
316static int
317_scsih_is_boot_device(u64 sas_address, u64 device_name,
318 u64 enclosure_logical_id, u16 slot, u8 form,
319 Mpi2BiosPage2BootDevice_t *boot_device)
320{
321 int rc = 0;
322
323 switch (form) {
324 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
325 if (!sas_address)
326 break;
327 rc = _scsih_srch_boot_sas_address(
328 sas_address, &boot_device->SasWwid);
329 break;
330 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
331 if (!enclosure_logical_id)
332 break;
333 rc = _scsih_srch_boot_encl_slot(
334 enclosure_logical_id,
335 slot, &boot_device->EnclosureSlot);
336 break;
337 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
338 if (!device_name)
339 break;
340 rc = _scsih_srch_boot_device_name(
341 device_name, &boot_device->DeviceName);
342 break;
343 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
344 break;
345 }
346
347 return rc;
348}
349
350/**
351 * _scsih_get_sas_address - set the sas_address for given device handle
352 * @handle: device handle
353 * @sas_address: sas address
354 *
355 * Returns 0 success, non-zero when failure
356 */
357static int
358_scsih_get_sas_address(struct MPT3SAS_ADAPTER *ioc, u16 handle,
359 u64 *sas_address)
360{
361 Mpi2SasDevicePage0_t sas_device_pg0;
362 Mpi2ConfigReply_t mpi_reply;
363 u32 ioc_status;
364
365 *sas_address = 0;
366
367 if (handle <= ioc->sas_hba.num_phys) {
368 *sas_address = ioc->sas_hba.sas_address;
369 return 0;
370 }
371
372 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
373 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
374 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
375 __FILE__, __LINE__, __func__);
376 return -ENXIO;
377 }
378
379 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
380 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
381 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
382 return 0;
383 }
384
385 /* we hit this becuase the given parent handle doesn't exist */
386 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
387 return -ENXIO;
388
389 /* else error case */
390 pr_err(MPT3SAS_FMT
391 "handle(0x%04x), ioc_status(0x%04x), failure at %s:%d/%s()!\n",
392 ioc->name, handle, ioc_status,
393 __FILE__, __LINE__, __func__);
394 return -EIO;
395}
396
397/**
398 * _scsih_determine_boot_device - determine boot device.
399 * @ioc: per adapter object
400 * @device: either sas_device or raid_device object
401 * @is_raid: [flag] 1 = raid object, 0 = sas object
402 *
403 * Determines whether this device should be first reported device to
404 * to scsi-ml or sas transport, this purpose is for persistent boot device.
405 * There are primary, alternate, and current entries in bios page 2. The order
406 * priority is primary, alternate, then current. This routine saves
407 * the corresponding device object and is_raid flag in the ioc object.
408 * The saved data to be used later in _scsih_probe_boot_devices().
409 */
410static void
411_scsih_determine_boot_device(struct MPT3SAS_ADAPTER *ioc,
412 void *device, u8 is_raid)
413{
414 struct _sas_device *sas_device;
415 struct _raid_device *raid_device;
416 u64 sas_address;
417 u64 device_name;
418 u64 enclosure_logical_id;
419 u16 slot;
420
421 /* only process this function when driver loads */
422 if (!ioc->is_driver_loading)
423 return;
424
425 /* no Bios, return immediately */
426 if (!ioc->bios_pg3.BiosVersion)
427 return;
428
429 if (!is_raid) {
430 sas_device = device;
431 sas_address = sas_device->sas_address;
432 device_name = sas_device->device_name;
433 enclosure_logical_id = sas_device->enclosure_logical_id;
434 slot = sas_device->slot;
435 } else {
436 raid_device = device;
437 sas_address = raid_device->wwid;
438 device_name = 0;
439 enclosure_logical_id = 0;
440 slot = 0;
441 }
442
443 if (!ioc->req_boot_device.device) {
444 if (_scsih_is_boot_device(sas_address, device_name,
445 enclosure_logical_id, slot,
446 (ioc->bios_pg2.ReqBootDeviceForm &
447 MPI2_BIOSPAGE2_FORM_MASK),
448 &ioc->bios_pg2.RequestedBootDevice)) {
449 dinitprintk(ioc, pr_info(MPT3SAS_FMT
450 "%s: req_boot_device(0x%016llx)\n",
451 ioc->name, __func__,
452 (unsigned long long)sas_address));
453 ioc->req_boot_device.device = device;
454 ioc->req_boot_device.is_raid = is_raid;
455 }
456 }
457
458 if (!ioc->req_alt_boot_device.device) {
459 if (_scsih_is_boot_device(sas_address, device_name,
460 enclosure_logical_id, slot,
461 (ioc->bios_pg2.ReqAltBootDeviceForm &
462 MPI2_BIOSPAGE2_FORM_MASK),
463 &ioc->bios_pg2.RequestedAltBootDevice)) {
464 dinitprintk(ioc, pr_info(MPT3SAS_FMT
465 "%s: req_alt_boot_device(0x%016llx)\n",
466 ioc->name, __func__,
467 (unsigned long long)sas_address));
468 ioc->req_alt_boot_device.device = device;
469 ioc->req_alt_boot_device.is_raid = is_raid;
470 }
471 }
472
473 if (!ioc->current_boot_device.device) {
474 if (_scsih_is_boot_device(sas_address, device_name,
475 enclosure_logical_id, slot,
476 (ioc->bios_pg2.CurrentBootDeviceForm &
477 MPI2_BIOSPAGE2_FORM_MASK),
478 &ioc->bios_pg2.CurrentBootDevice)) {
479 dinitprintk(ioc, pr_info(MPT3SAS_FMT
480 "%s: current_boot_device(0x%016llx)\n",
481 ioc->name, __func__,
482 (unsigned long long)sas_address));
483 ioc->current_boot_device.device = device;
484 ioc->current_boot_device.is_raid = is_raid;
485 }
486 }
487}
488
489/**
490 * mpt3sas_scsih_sas_device_find_by_sas_address - sas device search
491 * @ioc: per adapter object
492 * @sas_address: sas address
493 * Context: Calling function should acquire ioc->sas_device_lock
494 *
495 * This searches for sas_device based on sas_address, then return sas_device
496 * object.
497 */
498struct _sas_device *
499mpt3sas_scsih_sas_device_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
500 u64 sas_address)
501{
502 struct _sas_device *sas_device;
503
504 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
505 if (sas_device->sas_address == sas_address)
506 return sas_device;
507
508 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
509 if (sas_device->sas_address == sas_address)
510 return sas_device;
511
512 return NULL;
513}
514
515/**
516 * _scsih_sas_device_find_by_handle - sas device search
517 * @ioc: per adapter object
518 * @handle: sas device handle (assigned by firmware)
519 * Context: Calling function should acquire ioc->sas_device_lock
520 *
521 * This searches for sas_device based on sas_address, then return sas_device
522 * object.
523 */
524static struct _sas_device *
525_scsih_sas_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
526{
527 struct _sas_device *sas_device;
528
529 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
530 if (sas_device->handle == handle)
531 return sas_device;
532
533 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
534 if (sas_device->handle == handle)
535 return sas_device;
536
537 return NULL;
538}
539
540/**
541 * _scsih_sas_device_remove - remove sas_device from list.
542 * @ioc: per adapter object
543 * @sas_device: the sas_device object
544 * Context: This function will acquire ioc->sas_device_lock.
545 *
546 * Removing object and freeing associated memory from the ioc->sas_device_list.
547 */
548static void
549_scsih_sas_device_remove(struct MPT3SAS_ADAPTER *ioc,
550 struct _sas_device *sas_device)
551{
552 unsigned long flags;
553
554 if (!sas_device)
555 return;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530556 pr_info(MPT3SAS_FMT
557 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
558 ioc->name, sas_device->handle,
559 (unsigned long long) sas_device->sas_address);
560
561 if (sas_device->enclosure_handle != 0)
562 pr_info(MPT3SAS_FMT
563 "removing enclosure logical id(0x%016llx), slot(%d)\n",
564 ioc->name, (unsigned long long)
565 sas_device->enclosure_logical_id, sas_device->slot);
566
567 if (sas_device->connector_name[0] != '\0')
568 pr_info(MPT3SAS_FMT
569 "removing enclosure level(0x%04x), connector name( %s)\n",
570 ioc->name, sas_device->enclosure_level,
571 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530572
573 spin_lock_irqsave(&ioc->sas_device_lock, flags);
574 list_del(&sas_device->list);
575 kfree(sas_device);
576 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
577}
578
579/**
580 * _scsih_device_remove_by_handle - removing device object by handle
581 * @ioc: per adapter object
582 * @handle: device handle
583 *
584 * Return nothing.
585 */
586static void
587_scsih_device_remove_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
588{
589 struct _sas_device *sas_device;
590 unsigned long flags;
591
592 if (ioc->shost_recovery)
593 return;
594
595 spin_lock_irqsave(&ioc->sas_device_lock, flags);
596 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
597 if (sas_device)
598 list_del(&sas_device->list);
599 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
600 if (sas_device)
601 _scsih_remove_device(ioc, sas_device);
602}
603
604/**
605 * mpt3sas_device_remove_by_sas_address - removing device object by sas address
606 * @ioc: per adapter object
607 * @sas_address: device sas_address
608 *
609 * Return nothing.
610 */
611void
612mpt3sas_device_remove_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
613 u64 sas_address)
614{
615 struct _sas_device *sas_device;
616 unsigned long flags;
617
618 if (ioc->shost_recovery)
619 return;
620
621 spin_lock_irqsave(&ioc->sas_device_lock, flags);
622 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
623 sas_address);
624 if (sas_device)
625 list_del(&sas_device->list);
626 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
627 if (sas_device)
628 _scsih_remove_device(ioc, sas_device);
629}
630
631/**
632 * _scsih_sas_device_add - insert sas_device to the list.
633 * @ioc: per adapter object
634 * @sas_device: the sas_device object
635 * Context: This function will acquire ioc->sas_device_lock.
636 *
637 * Adding new object to the ioc->sas_device_list.
638 */
639static void
640_scsih_sas_device_add(struct MPT3SAS_ADAPTER *ioc,
641 struct _sas_device *sas_device)
642{
643 unsigned long flags;
644
645 dewtprintk(ioc, pr_info(MPT3SAS_FMT
646 "%s: handle(0x%04x), sas_addr(0x%016llx)\n",
647 ioc->name, __func__, sas_device->handle,
648 (unsigned long long)sas_device->sas_address));
649
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530650 if (sas_device->enclosure_handle != 0)
651 dewtprintk(ioc, pr_info(MPT3SAS_FMT
652 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
653 ioc->name, __func__, (unsigned long long)
654 sas_device->enclosure_logical_id, sas_device->slot));
655
656 if (sas_device->connector_name[0] != '\0')
657 dewtprintk(ioc, pr_info(MPT3SAS_FMT
658 "%s: enclosure level(0x%04x), connector name( %s)\n",
659 ioc->name, __func__,
660 sas_device->enclosure_level, sas_device->connector_name));
661
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530662 spin_lock_irqsave(&ioc->sas_device_lock, flags);
663 list_add_tail(&sas_device->list, &ioc->sas_device_list);
664 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
665
666 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
667 sas_device->sas_address_parent)) {
668 _scsih_sas_device_remove(ioc, sas_device);
669 } else if (!sas_device->starget) {
670 /*
671 * When asyn scanning is enabled, its not possible to remove
672 * devices while scanning is turned on due to an oops in
673 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
674 */
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +0530675 if (!ioc->is_driver_loading) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530676 mpt3sas_transport_port_remove(ioc,
677 sas_device->sas_address,
678 sas_device->sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +0530679 _scsih_sas_device_remove(ioc, sas_device);
680 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530681 }
682}
683
684/**
685 * _scsih_sas_device_init_add - insert sas_device to the list.
686 * @ioc: per adapter object
687 * @sas_device: the sas_device object
688 * Context: This function will acquire ioc->sas_device_lock.
689 *
690 * Adding new object at driver load time to the ioc->sas_device_init_list.
691 */
692static void
693_scsih_sas_device_init_add(struct MPT3SAS_ADAPTER *ioc,
694 struct _sas_device *sas_device)
695{
696 unsigned long flags;
697
698 dewtprintk(ioc, pr_info(MPT3SAS_FMT
699 "%s: handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
700 __func__, sas_device->handle,
701 (unsigned long long)sas_device->sas_address));
702
Sreekanth Reddye6d45e32015-06-30 12:24:52 +0530703 if (sas_device->enclosure_handle != 0)
704 dewtprintk(ioc, pr_info(MPT3SAS_FMT
705 "%s: enclosure logical id(0x%016llx), slot( %d)\n",
706 ioc->name, __func__, (unsigned long long)
707 sas_device->enclosure_logical_id, sas_device->slot));
708
709 if (sas_device->connector_name[0] != '\0')
710 dewtprintk(ioc, pr_info(MPT3SAS_FMT
711 "%s: enclosure level(0x%04x), connector name( %s)\n",
712 ioc->name, __func__, sas_device->enclosure_level,
713 sas_device->connector_name));
714
Sreekanth Reddyf92363d2012-11-30 07:44:21 +0530715 spin_lock_irqsave(&ioc->sas_device_lock, flags);
716 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
717 _scsih_determine_boot_device(ioc, sas_device, 0);
718 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
719}
720
721/**
722 * _scsih_raid_device_find_by_id - raid device search
723 * @ioc: per adapter object
724 * @id: sas device target id
725 * @channel: sas device channel
726 * Context: Calling function should acquire ioc->raid_device_lock
727 *
728 * This searches for raid_device based on target id, then return raid_device
729 * object.
730 */
731static struct _raid_device *
732_scsih_raid_device_find_by_id(struct MPT3SAS_ADAPTER *ioc, int id, int channel)
733{
734 struct _raid_device *raid_device, *r;
735
736 r = NULL;
737 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
738 if (raid_device->id == id && raid_device->channel == channel) {
739 r = raid_device;
740 goto out;
741 }
742 }
743
744 out:
745 return r;
746}
747
748/**
749 * _scsih_raid_device_find_by_handle - raid device search
750 * @ioc: per adapter object
751 * @handle: sas device handle (assigned by firmware)
752 * Context: Calling function should acquire ioc->raid_device_lock
753 *
754 * This searches for raid_device based on handle, then return raid_device
755 * object.
756 */
757static struct _raid_device *
758_scsih_raid_device_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
759{
760 struct _raid_device *raid_device, *r;
761
762 r = NULL;
763 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
764 if (raid_device->handle != handle)
765 continue;
766 r = raid_device;
767 goto out;
768 }
769
770 out:
771 return r;
772}
773
774/**
775 * _scsih_raid_device_find_by_wwid - raid device search
776 * @ioc: per adapter object
777 * @handle: sas device handle (assigned by firmware)
778 * Context: Calling function should acquire ioc->raid_device_lock
779 *
780 * This searches for raid_device based on wwid, then return raid_device
781 * object.
782 */
783static struct _raid_device *
784_scsih_raid_device_find_by_wwid(struct MPT3SAS_ADAPTER *ioc, u64 wwid)
785{
786 struct _raid_device *raid_device, *r;
787
788 r = NULL;
789 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
790 if (raid_device->wwid != wwid)
791 continue;
792 r = raid_device;
793 goto out;
794 }
795
796 out:
797 return r;
798}
799
800/**
801 * _scsih_raid_device_add - add raid_device object
802 * @ioc: per adapter object
803 * @raid_device: raid_device object
804 *
805 * This is added to the raid_device_list link list.
806 */
807static void
808_scsih_raid_device_add(struct MPT3SAS_ADAPTER *ioc,
809 struct _raid_device *raid_device)
810{
811 unsigned long flags;
812
813 dewtprintk(ioc, pr_info(MPT3SAS_FMT
814 "%s: handle(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
815 raid_device->handle, (unsigned long long)raid_device->wwid));
816
817 spin_lock_irqsave(&ioc->raid_device_lock, flags);
818 list_add_tail(&raid_device->list, &ioc->raid_device_list);
819 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
820}
821
822/**
823 * _scsih_raid_device_remove - delete raid_device object
824 * @ioc: per adapter object
825 * @raid_device: raid_device object
826 *
827 */
828static void
829_scsih_raid_device_remove(struct MPT3SAS_ADAPTER *ioc,
830 struct _raid_device *raid_device)
831{
832 unsigned long flags;
833
834 spin_lock_irqsave(&ioc->raid_device_lock, flags);
835 list_del(&raid_device->list);
836 kfree(raid_device);
837 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
838}
839
840/**
841 * mpt3sas_scsih_expander_find_by_handle - expander device search
842 * @ioc: per adapter object
843 * @handle: expander handle (assigned by firmware)
844 * Context: Calling function should acquire ioc->sas_device_lock
845 *
846 * This searches for expander device based on handle, then returns the
847 * sas_node object.
848 */
849struct _sas_node *
850mpt3sas_scsih_expander_find_by_handle(struct MPT3SAS_ADAPTER *ioc, u16 handle)
851{
852 struct _sas_node *sas_expander, *r;
853
854 r = NULL;
855 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
856 if (sas_expander->handle != handle)
857 continue;
858 r = sas_expander;
859 goto out;
860 }
861 out:
862 return r;
863}
864
865/**
866 * mpt3sas_scsih_expander_find_by_sas_address - expander device search
867 * @ioc: per adapter object
868 * @sas_address: sas address
869 * Context: Calling function should acquire ioc->sas_node_lock.
870 *
871 * This searches for expander device based on sas_address, then returns the
872 * sas_node object.
873 */
874struct _sas_node *
875mpt3sas_scsih_expander_find_by_sas_address(struct MPT3SAS_ADAPTER *ioc,
876 u64 sas_address)
877{
878 struct _sas_node *sas_expander, *r;
879
880 r = NULL;
881 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
882 if (sas_expander->sas_address != sas_address)
883 continue;
884 r = sas_expander;
885 goto out;
886 }
887 out:
888 return r;
889}
890
891/**
892 * _scsih_expander_node_add - insert expander device to the list.
893 * @ioc: per adapter object
894 * @sas_expander: the sas_device object
895 * Context: This function will acquire ioc->sas_node_lock.
896 *
897 * Adding new object to the ioc->sas_expander_list.
898 *
899 * Return nothing.
900 */
901static void
902_scsih_expander_node_add(struct MPT3SAS_ADAPTER *ioc,
903 struct _sas_node *sas_expander)
904{
905 unsigned long flags;
906
907 spin_lock_irqsave(&ioc->sas_node_lock, flags);
908 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
909 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
910}
911
912/**
913 * _scsih_is_end_device - determines if device is an end device
914 * @device_info: bitfield providing information about the device.
915 * Context: none
916 *
917 * Returns 1 if end device.
918 */
919static int
920_scsih_is_end_device(u32 device_info)
921{
922 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
923 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
924 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
925 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
926 return 1;
927 else
928 return 0;
929}
930
931/**
932 * _scsih_scsi_lookup_get - returns scmd entry
933 * @ioc: per adapter object
934 * @smid: system request message index
935 *
936 * Returns the smid stored scmd pointer.
937 */
938static struct scsi_cmnd *
939_scsih_scsi_lookup_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
940{
941 return ioc->scsi_lookup[smid - 1].scmd;
942}
943
944/**
945 * _scsih_scsi_lookup_get_clear - returns scmd entry
946 * @ioc: per adapter object
947 * @smid: system request message index
948 *
949 * Returns the smid stored scmd pointer.
950 * Then will derefrence the stored scmd pointer.
951 */
952static inline struct scsi_cmnd *
953_scsih_scsi_lookup_get_clear(struct MPT3SAS_ADAPTER *ioc, u16 smid)
954{
955 unsigned long flags;
956 struct scsi_cmnd *scmd;
957
958 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
959 scmd = ioc->scsi_lookup[smid - 1].scmd;
960 ioc->scsi_lookup[smid - 1].scmd = NULL;
961 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
962
963 return scmd;
964}
965
966/**
967 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
968 * @ioc: per adapter object
969 * @smid: system request message index
970 * @scmd: pointer to scsi command object
971 * Context: This function will acquire ioc->scsi_lookup_lock.
972 *
973 * This will search for a scmd pointer in the scsi_lookup array,
974 * returning the revelent smid. A returned value of zero means invalid.
975 */
976static u16
977_scsih_scsi_lookup_find_by_scmd(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd
978 *scmd)
979{
980 u16 smid;
981 unsigned long flags;
982 int i;
983
984 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
985 smid = 0;
986 for (i = 0; i < ioc->scsiio_depth; i++) {
987 if (ioc->scsi_lookup[i].scmd == scmd) {
988 smid = ioc->scsi_lookup[i].smid;
989 goto out;
990 }
991 }
992 out:
993 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
994 return smid;
995}
996
997/**
998 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
999 * @ioc: per adapter object
1000 * @id: target id
1001 * @channel: channel
1002 * Context: This function will acquire ioc->scsi_lookup_lock.
1003 *
1004 * This will search for a matching channel:id in the scsi_lookup array,
1005 * returning 1 if found.
1006 */
1007static u8
1008_scsih_scsi_lookup_find_by_target(struct MPT3SAS_ADAPTER *ioc, int id,
1009 int channel)
1010{
1011 u8 found;
1012 unsigned long flags;
1013 int i;
1014
1015 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1016 found = 0;
1017 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1018 if (ioc->scsi_lookup[i].scmd &&
1019 (ioc->scsi_lookup[i].scmd->device->id == id &&
1020 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
1021 found = 1;
1022 goto out;
1023 }
1024 }
1025 out:
1026 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1027 return found;
1028}
1029
1030/**
1031 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
1032 * @ioc: per adapter object
1033 * @id: target id
1034 * @lun: lun number
1035 * @channel: channel
1036 * Context: This function will acquire ioc->scsi_lookup_lock.
1037 *
1038 * This will search for a matching channel:id:lun in the scsi_lookup array,
1039 * returning 1 if found.
1040 */
1041static u8
1042_scsih_scsi_lookup_find_by_lun(struct MPT3SAS_ADAPTER *ioc, int id,
1043 unsigned int lun, int channel)
1044{
1045 u8 found;
1046 unsigned long flags;
1047 int i;
1048
1049 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1050 found = 0;
1051 for (i = 0 ; i < ioc->scsiio_depth; i++) {
1052 if (ioc->scsi_lookup[i].scmd &&
1053 (ioc->scsi_lookup[i].scmd->device->id == id &&
1054 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1055 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1056 found = 1;
1057 goto out;
1058 }
1059 }
1060 out:
1061 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1062 return found;
1063}
1064
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001065/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301066 * scsih_change_queue_depth - setting device queue depth
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001067 * @sdev: scsi device struct
1068 * @qdepth: requested queue depth
1069 *
1070 * Returns queue depth.
1071 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301072int
1073scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301074{
1075 struct Scsi_Host *shost = sdev->host;
1076 int max_depth;
1077 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1078 struct MPT3SAS_DEVICE *sas_device_priv_data;
1079 struct MPT3SAS_TARGET *sas_target_priv_data;
1080 struct _sas_device *sas_device;
1081 unsigned long flags;
1082
1083 max_depth = shost->can_queue;
1084
1085 /* limit max device queue for SATA to 32 */
1086 sas_device_priv_data = sdev->hostdata;
1087 if (!sas_device_priv_data)
1088 goto not_sata;
1089 sas_target_priv_data = sas_device_priv_data->sas_target;
1090 if (!sas_target_priv_data)
1091 goto not_sata;
1092 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1093 goto not_sata;
1094 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1095 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1096 sas_device_priv_data->sas_target->sas_address);
1097 if (sas_device && sas_device->device_info &
1098 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1099 max_depth = MPT3SAS_SATA_QUEUE_DEPTH;
1100 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1101
1102 not_sata:
1103
1104 if (!sdev->tagged_supported)
1105 max_depth = 1;
1106 if (qdepth > max_depth)
1107 qdepth = max_depth;
Christoph Hellwigcf75d5d2014-11-13 15:13:20 +01001108 return scsi_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301109}
1110
1111/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301112 * scsih_target_alloc - target add routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301113 * @starget: scsi target struct
1114 *
1115 * Returns 0 if ok. Any other return is assumed to be an error and
1116 * the device is ignored.
1117 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301118int
1119scsih_target_alloc(struct scsi_target *starget)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301120{
1121 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1122 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1123 struct MPT3SAS_TARGET *sas_target_priv_data;
1124 struct _sas_device *sas_device;
1125 struct _raid_device *raid_device;
1126 unsigned long flags;
1127 struct sas_rphy *rphy;
1128
Joe Lawrence62c4da42014-06-25 17:04:22 -04001129 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1130 GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301131 if (!sas_target_priv_data)
1132 return -ENOMEM;
1133
1134 starget->hostdata = sas_target_priv_data;
1135 sas_target_priv_data->starget = starget;
1136 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
1137
1138 /* RAID volumes */
1139 if (starget->channel == RAID_CHANNEL) {
1140 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1141 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1142 starget->channel);
1143 if (raid_device) {
1144 sas_target_priv_data->handle = raid_device->handle;
1145 sas_target_priv_data->sas_address = raid_device->wwid;
1146 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
1147 raid_device->starget = starget;
1148 }
1149 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1150 return 0;
1151 }
1152
1153 /* sas/sata devices */
1154 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1155 rphy = dev_to_rphy(starget->dev.parent);
1156 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1157 rphy->identify.sas_address);
1158
1159 if (sas_device) {
1160 sas_target_priv_data->handle = sas_device->handle;
1161 sas_target_priv_data->sas_address = sas_device->sas_address;
1162 sas_device->starget = starget;
1163 sas_device->id = starget->id;
1164 sas_device->channel = starget->channel;
1165 if (test_bit(sas_device->handle, ioc->pd_handles))
1166 sas_target_priv_data->flags |=
1167 MPT_TARGET_FLAGS_RAID_COMPONENT;
1168 if (sas_device->fast_path)
1169 sas_target_priv_data->flags |= MPT_TARGET_FASTPATH_IO;
1170 }
1171 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1172
1173 return 0;
1174}
1175
1176/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301177 * scsih_target_destroy - target destroy routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301178 * @starget: scsi target struct
1179 *
1180 * Returns nothing.
1181 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301182void
1183scsih_target_destroy(struct scsi_target *starget)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301184{
1185 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1186 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1187 struct MPT3SAS_TARGET *sas_target_priv_data;
1188 struct _sas_device *sas_device;
1189 struct _raid_device *raid_device;
1190 unsigned long flags;
1191 struct sas_rphy *rphy;
1192
1193 sas_target_priv_data = starget->hostdata;
1194 if (!sas_target_priv_data)
1195 return;
1196
1197 if (starget->channel == RAID_CHANNEL) {
1198 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1199 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1200 starget->channel);
1201 if (raid_device) {
1202 raid_device->starget = NULL;
1203 raid_device->sdev = NULL;
1204 }
1205 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1206 goto out;
1207 }
1208
1209 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1210 rphy = dev_to_rphy(starget->dev.parent);
1211 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1212 rphy->identify.sas_address);
1213 if (sas_device && (sas_device->starget == starget) &&
1214 (sas_device->id == starget->id) &&
1215 (sas_device->channel == starget->channel))
1216 sas_device->starget = NULL;
1217
1218 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1219
1220 out:
1221 kfree(sas_target_priv_data);
1222 starget->hostdata = NULL;
1223}
1224
1225/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301226 * scsih_slave_alloc - device add routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301227 * @sdev: scsi device struct
1228 *
1229 * Returns 0 if ok. Any other return is assumed to be an error and
1230 * the device is ignored.
1231 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301232int
1233scsih_slave_alloc(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301234{
1235 struct Scsi_Host *shost;
1236 struct MPT3SAS_ADAPTER *ioc;
1237 struct MPT3SAS_TARGET *sas_target_priv_data;
1238 struct MPT3SAS_DEVICE *sas_device_priv_data;
1239 struct scsi_target *starget;
1240 struct _raid_device *raid_device;
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301241 struct _sas_device *sas_device;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301242 unsigned long flags;
1243
Joe Lawrence62c4da42014-06-25 17:04:22 -04001244 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1245 GFP_KERNEL);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301246 if (!sas_device_priv_data)
1247 return -ENOMEM;
1248
1249 sas_device_priv_data->lun = sdev->lun;
1250 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1251
1252 starget = scsi_target(sdev);
1253 sas_target_priv_data = starget->hostdata;
1254 sas_target_priv_data->num_luns++;
1255 sas_device_priv_data->sas_target = sas_target_priv_data;
1256 sdev->hostdata = sas_device_priv_data;
1257 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1258 sdev->no_uld_attach = 1;
1259
1260 shost = dev_to_shost(&starget->dev);
1261 ioc = shost_priv(shost);
1262 if (starget->channel == RAID_CHANNEL) {
1263 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1264 raid_device = _scsih_raid_device_find_by_id(ioc,
1265 starget->id, starget->channel);
1266 if (raid_device)
1267 raid_device->sdev = sdev; /* raid is single lun */
1268 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1269 }
1270
Sreekanth Reddyb65cfed2013-06-29 03:52:03 +05301271 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1272 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1273 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1274 sas_target_priv_data->sas_address);
1275 if (sas_device && (sas_device->starget == NULL)) {
1276 sdev_printk(KERN_INFO, sdev,
1277 "%s : sas_device->starget set to starget @ %d\n",
1278 __func__, __LINE__);
1279 sas_device->starget = starget;
1280 }
1281 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1282 }
1283
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301284 return 0;
1285}
1286
1287/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301288 * scsih_slave_destroy - device destroy routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301289 * @sdev: scsi device struct
1290 *
1291 * Returns nothing.
1292 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301293void
1294scsih_slave_destroy(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301295{
1296 struct MPT3SAS_TARGET *sas_target_priv_data;
1297 struct scsi_target *starget;
1298 struct Scsi_Host *shost;
1299 struct MPT3SAS_ADAPTER *ioc;
1300 struct _sas_device *sas_device;
1301 unsigned long flags;
1302
1303 if (!sdev->hostdata)
1304 return;
1305
1306 starget = scsi_target(sdev);
1307 sas_target_priv_data = starget->hostdata;
1308 sas_target_priv_data->num_luns--;
1309
1310 shost = dev_to_shost(&starget->dev);
1311 ioc = shost_priv(shost);
1312
1313 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1314 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1315 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1316 sas_target_priv_data->sas_address);
1317 if (sas_device && !sas_target_priv_data->num_luns)
1318 sas_device->starget = NULL;
1319 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1320 }
1321
1322 kfree(sdev->hostdata);
1323 sdev->hostdata = NULL;
1324}
1325
1326/**
1327 * _scsih_display_sata_capabilities - sata capabilities
1328 * @ioc: per adapter object
1329 * @handle: device handle
1330 * @sdev: scsi device struct
1331 */
1332static void
1333_scsih_display_sata_capabilities(struct MPT3SAS_ADAPTER *ioc,
1334 u16 handle, struct scsi_device *sdev)
1335{
1336 Mpi2ConfigReply_t mpi_reply;
1337 Mpi2SasDevicePage0_t sas_device_pg0;
1338 u32 ioc_status;
1339 u16 flags;
1340 u32 device_info;
1341
1342 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1343 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
1344 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1345 ioc->name, __FILE__, __LINE__, __func__);
1346 return;
1347 }
1348
1349 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1350 MPI2_IOCSTATUS_MASK;
1351 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1352 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1353 ioc->name, __FILE__, __LINE__, __func__);
1354 return;
1355 }
1356
1357 flags = le16_to_cpu(sas_device_pg0.Flags);
1358 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
1359
1360 sdev_printk(KERN_INFO, sdev,
1361 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1362 "sw_preserve(%s)\n",
1363 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1364 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1365 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1366 "n",
1367 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1368 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1369 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1370}
1371
1372/*
1373 * raid transport support -
1374 * Enabled for SLES11 and newer, in older kernels the driver will panic when
1375 * unloading the driver followed by a load - I beleive that the subroutine
1376 * raid_class_release() is not cleaning up properly.
1377 */
1378
1379/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301380 * scsih_is_raid - return boolean indicating device is raid volume
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301381 * @dev the device struct object
1382 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301383int
1384scsih_is_raid(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301385{
1386 struct scsi_device *sdev = to_scsi_device(dev);
1387
1388 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1389}
1390
1391/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301392 * scsih_get_resync - get raid volume resync percent complete
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301393 * @dev the device struct object
1394 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301395void
1396scsih_get_resync(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301397{
1398 struct scsi_device *sdev = to_scsi_device(dev);
1399 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1400 static struct _raid_device *raid_device;
1401 unsigned long flags;
1402 Mpi2RaidVolPage0_t vol_pg0;
1403 Mpi2ConfigReply_t mpi_reply;
1404 u32 volume_status_flags;
1405 u8 percent_complete;
1406 u16 handle;
1407
1408 percent_complete = 0;
1409 handle = 0;
1410 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1411 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1412 sdev->channel);
1413 if (raid_device) {
1414 handle = raid_device->handle;
1415 percent_complete = raid_device->percent_complete;
1416 }
1417 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1418
1419 if (!handle)
1420 goto out;
1421
1422 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1423 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1424 sizeof(Mpi2RaidVolPage0_t))) {
1425 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1426 ioc->name, __FILE__, __LINE__, __func__);
1427 percent_complete = 0;
1428 goto out;
1429 }
1430
1431 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1432 if (!(volume_status_flags &
1433 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1434 percent_complete = 0;
1435
1436 out:
1437 raid_set_resync(mpt3sas_raid_template, dev, percent_complete);
1438}
1439
1440/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301441 * scsih_get_state - get raid volume level
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301442 * @dev the device struct object
1443 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301444void
1445scsih_get_state(struct device *dev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301446{
1447 struct scsi_device *sdev = to_scsi_device(dev);
1448 struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
1449 static struct _raid_device *raid_device;
1450 unsigned long flags;
1451 Mpi2RaidVolPage0_t vol_pg0;
1452 Mpi2ConfigReply_t mpi_reply;
1453 u32 volstate;
1454 enum raid_state state = RAID_STATE_UNKNOWN;
1455 u16 handle = 0;
1456
1457 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1458 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1459 sdev->channel);
1460 if (raid_device)
1461 handle = raid_device->handle;
1462 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1463
1464 if (!raid_device)
1465 goto out;
1466
1467 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1468 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
1469 sizeof(Mpi2RaidVolPage0_t))) {
1470 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
1471 ioc->name, __FILE__, __LINE__, __func__);
1472 goto out;
1473 }
1474
1475 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1476 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1477 state = RAID_STATE_RESYNCING;
1478 goto out;
1479 }
1480
1481 switch (vol_pg0.VolumeState) {
1482 case MPI2_RAID_VOL_STATE_OPTIMAL:
1483 case MPI2_RAID_VOL_STATE_ONLINE:
1484 state = RAID_STATE_ACTIVE;
1485 break;
1486 case MPI2_RAID_VOL_STATE_DEGRADED:
1487 state = RAID_STATE_DEGRADED;
1488 break;
1489 case MPI2_RAID_VOL_STATE_FAILED:
1490 case MPI2_RAID_VOL_STATE_MISSING:
1491 state = RAID_STATE_OFFLINE;
1492 break;
1493 }
1494 out:
1495 raid_set_state(mpt3sas_raid_template, dev, state);
1496}
1497
1498/**
1499 * _scsih_set_level - set raid level
1500 * @sdev: scsi device struct
1501 * @volume_type: volume type
1502 */
1503static void
1504_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
1505{
1506 enum raid_level level = RAID_LEVEL_UNKNOWN;
1507
1508 switch (volume_type) {
1509 case MPI2_RAID_VOL_TYPE_RAID0:
1510 level = RAID_LEVEL_0;
1511 break;
1512 case MPI2_RAID_VOL_TYPE_RAID10:
1513 level = RAID_LEVEL_10;
1514 break;
1515 case MPI2_RAID_VOL_TYPE_RAID1E:
1516 level = RAID_LEVEL_1E;
1517 break;
1518 case MPI2_RAID_VOL_TYPE_RAID1:
1519 level = RAID_LEVEL_1;
1520 break;
1521 }
1522
1523 raid_set_level(mpt3sas_raid_template, &sdev->sdev_gendev, level);
1524}
1525
1526
1527/**
1528 * _scsih_get_volume_capabilities - volume capabilities
1529 * @ioc: per adapter object
1530 * @sas_device: the raid_device object
1531 *
1532 * Returns 0 for success, else 1
1533 */
1534static int
1535_scsih_get_volume_capabilities(struct MPT3SAS_ADAPTER *ioc,
1536 struct _raid_device *raid_device)
1537{
1538 Mpi2RaidVolPage0_t *vol_pg0;
1539 Mpi2RaidPhysDiskPage0_t pd_pg0;
1540 Mpi2SasDevicePage0_t sas_device_pg0;
1541 Mpi2ConfigReply_t mpi_reply;
1542 u16 sz;
1543 u8 num_pds;
1544
1545 if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
1546 &num_pds)) || !num_pds) {
1547 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1548 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1549 __func__));
1550 return 1;
1551 }
1552
1553 raid_device->num_pds = num_pds;
1554 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1555 sizeof(Mpi2RaidVol0PhysDisk_t));
1556 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1557 if (!vol_pg0) {
1558 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1559 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1560 __func__));
1561 return 1;
1562 }
1563
1564 if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1565 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1566 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1567 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1568 __func__));
1569 kfree(vol_pg0);
1570 return 1;
1571 }
1572
1573 raid_device->volume_type = vol_pg0->VolumeType;
1574
1575 /* figure out what the underlying devices are by
1576 * obtaining the device_info bits for the 1st device
1577 */
1578 if (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1579 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1580 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1581 if (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1582 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1583 le16_to_cpu(pd_pg0.DevHandle)))) {
1584 raid_device->device_info =
1585 le32_to_cpu(sas_device_pg0.DeviceInfo);
1586 }
1587 }
1588
1589 kfree(vol_pg0);
1590 return 0;
1591}
1592
1593
1594
1595/**
1596 * _scsih_enable_tlr - setting TLR flags
1597 * @ioc: per adapter object
1598 * @sdev: scsi device struct
1599 *
1600 * Enabling Transaction Layer Retries for tape devices when
1601 * vpd page 0x90 is present
1602 *
1603 */
1604static void
1605_scsih_enable_tlr(struct MPT3SAS_ADAPTER *ioc, struct scsi_device *sdev)
1606{
1607
1608 /* only for TAPE */
1609 if (sdev->type != TYPE_TAPE)
1610 return;
1611
1612 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1613 return;
1614
1615 sas_enable_tlr(sdev);
1616 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1617 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1618 return;
1619
1620}
1621
1622/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301623 * scsih_slave_configure - device configure routine.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301624 * @sdev: scsi device struct
1625 *
1626 * Returns 0 if ok. Any other return is assumed to be an error and
1627 * the device is ignored.
1628 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301629int
1630scsih_slave_configure(struct scsi_device *sdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301631{
1632 struct Scsi_Host *shost = sdev->host;
1633 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
1634 struct MPT3SAS_DEVICE *sas_device_priv_data;
1635 struct MPT3SAS_TARGET *sas_target_priv_data;
1636 struct _sas_device *sas_device;
1637 struct _raid_device *raid_device;
1638 unsigned long flags;
1639 int qdepth;
1640 u8 ssp_target = 0;
1641 char *ds = "";
1642 char *r_level = "";
1643 u16 handle, volume_handle = 0;
1644 u64 volume_wwid = 0;
1645
1646 qdepth = 1;
1647 sas_device_priv_data = sdev->hostdata;
1648 sas_device_priv_data->configured_lun = 1;
1649 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1650 sas_target_priv_data = sas_device_priv_data->sas_target;
1651 handle = sas_target_priv_data->handle;
1652
1653 /* raid volume handling */
1654 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1655
1656 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1657 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1658 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1659 if (!raid_device) {
1660 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1661 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1662 __LINE__, __func__));
1663 return 1;
1664 }
1665
1666 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1667 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1668 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1669 __LINE__, __func__));
1670 return 1;
1671 }
1672
1673
1674 /* RAID Queue Depth Support
1675 * IS volume = underlying qdepth of drive type, either
1676 * MPT3SAS_SAS_QUEUE_DEPTH or MPT3SAS_SATA_QUEUE_DEPTH
1677 * IM/IME/R10 = 128 (MPT3SAS_RAID_QUEUE_DEPTH)
1678 */
1679 if (raid_device->device_info &
1680 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1681 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1682 ds = "SSP";
1683 } else {
1684 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1685 if (raid_device->device_info &
1686 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1687 ds = "SATA";
1688 else
1689 ds = "STP";
1690 }
1691
1692 switch (raid_device->volume_type) {
1693 case MPI2_RAID_VOL_TYPE_RAID0:
1694 r_level = "RAID0";
1695 break;
1696 case MPI2_RAID_VOL_TYPE_RAID1E:
1697 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1698 if (ioc->manu_pg10.OEMIdentifier &&
1699 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
1700 MFG10_GF0_R10_DISPLAY) &&
1701 !(raid_device->num_pds % 2))
1702 r_level = "RAID10";
1703 else
1704 r_level = "RAID1E";
1705 break;
1706 case MPI2_RAID_VOL_TYPE_RAID1:
1707 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1708 r_level = "RAID1";
1709 break;
1710 case MPI2_RAID_VOL_TYPE_RAID10:
1711 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1712 r_level = "RAID10";
1713 break;
1714 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1715 default:
1716 qdepth = MPT3SAS_RAID_QUEUE_DEPTH;
1717 r_level = "RAIDX";
1718 break;
1719 }
1720
1721 sdev_printk(KERN_INFO, sdev,
1722 "%s: handle(0x%04x), wwid(0x%016llx), pd_count(%d), type(%s)\n",
1723 r_level, raid_device->handle,
1724 (unsigned long long)raid_device->wwid,
1725 raid_device->num_pds, ds);
1726
1727
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301728 scsih_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301729
1730/* raid transport support */
1731 _scsih_set_level(sdev, raid_device->volume_type);
1732 return 0;
1733 }
1734
1735 /* non-raid handling */
1736 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
1737 if (mpt3sas_config_get_volume_handle(ioc, handle,
1738 &volume_handle)) {
1739 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1740 "failure at %s:%d/%s()!\n", ioc->name,
1741 __FILE__, __LINE__, __func__));
1742 return 1;
1743 }
1744 if (volume_handle && mpt3sas_config_get_volume_wwid(ioc,
1745 volume_handle, &volume_wwid)) {
1746 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1747 "failure at %s:%d/%s()!\n", ioc->name,
1748 __FILE__, __LINE__, __func__));
1749 return 1;
1750 }
1751 }
1752
1753 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1754 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
1755 sas_device_priv_data->sas_target->sas_address);
1756 if (!sas_device) {
1757 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1758 dfailprintk(ioc, pr_warn(MPT3SAS_FMT
1759 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1760 __func__));
1761 return 1;
1762 }
1763
1764 sas_device->volume_handle = volume_handle;
1765 sas_device->volume_wwid = volume_wwid;
1766 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1767 qdepth = MPT3SAS_SAS_QUEUE_DEPTH;
1768 ssp_target = 1;
1769 ds = "SSP";
1770 } else {
1771 qdepth = MPT3SAS_SATA_QUEUE_DEPTH;
1772 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
1773 ds = "STP";
1774 else if (sas_device->device_info &
1775 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1776 ds = "SATA";
1777 }
1778
1779 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), " \
1780 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
1781 ds, handle, (unsigned long long)sas_device->sas_address,
1782 sas_device->phy, (unsigned long long)sas_device->device_name);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05301783 if (sas_device->enclosure_handle != 0)
1784 sdev_printk(KERN_INFO, sdev,
1785 "%s: enclosure_logical_id(0x%016llx), slot(%d)\n",
1786 ds, (unsigned long long)
1787 sas_device->enclosure_logical_id, sas_device->slot);
1788 if (sas_device->connector_name[0] != '\0')
1789 sdev_printk(KERN_INFO, sdev,
1790 "%s: enclosure level(0x%04x), connector name( %s)\n",
1791 ds, sas_device->enclosure_level,
1792 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301793
1794 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1795
1796 if (!ssp_target)
1797 _scsih_display_sata_capabilities(ioc, handle, sdev);
1798
1799
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301800 scsih_change_queue_depth(sdev, qdepth);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301801
1802 if (ssp_target) {
1803 sas_read_port_mode_page(sdev);
1804 _scsih_enable_tlr(ioc, sdev);
1805 }
1806
1807 return 0;
1808}
1809
1810/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301811 * scsih_bios_param - fetch head, sector, cylinder info for a disk
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301812 * @sdev: scsi device struct
1813 * @bdev: pointer to block device context
1814 * @capacity: device size (in 512 byte sectors)
1815 * @params: three element array to place output:
1816 * params[0] number of heads (max 255)
1817 * params[1] number of sectors (max 63)
1818 * params[2] number of cylinders
1819 *
1820 * Return nothing.
1821 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05301822int
1823scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05301824 sector_t capacity, int params[])
1825{
1826 int heads;
1827 int sectors;
1828 sector_t cylinders;
1829 ulong dummy;
1830
1831 heads = 64;
1832 sectors = 32;
1833
1834 dummy = heads * sectors;
1835 cylinders = capacity;
1836 sector_div(cylinders, dummy);
1837
1838 /*
1839 * Handle extended translation size for logical drives
1840 * > 1Gb
1841 */
1842 if ((ulong)capacity >= 0x200000) {
1843 heads = 255;
1844 sectors = 63;
1845 dummy = heads * sectors;
1846 cylinders = capacity;
1847 sector_div(cylinders, dummy);
1848 }
1849
1850 /* return result */
1851 params[0] = heads;
1852 params[1] = sectors;
1853 params[2] = cylinders;
1854
1855 return 0;
1856}
1857
1858/**
1859 * _scsih_response_code - translation of device response code
1860 * @ioc: per adapter object
1861 * @response_code: response code returned by the device
1862 *
1863 * Return nothing.
1864 */
1865static void
1866_scsih_response_code(struct MPT3SAS_ADAPTER *ioc, u8 response_code)
1867{
1868 char *desc;
1869
1870 switch (response_code) {
1871 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1872 desc = "task management request completed";
1873 break;
1874 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1875 desc = "invalid frame";
1876 break;
1877 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1878 desc = "task management request not supported";
1879 break;
1880 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1881 desc = "task management request failed";
1882 break;
1883 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1884 desc = "task management request succeeded";
1885 break;
1886 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1887 desc = "invalid lun";
1888 break;
1889 case 0xA:
1890 desc = "overlapped tag attempted";
1891 break;
1892 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1893 desc = "task queued, however not sent to target";
1894 break;
1895 default:
1896 desc = "unknown";
1897 break;
1898 }
1899 pr_warn(MPT3SAS_FMT "response_code(0x%01x): %s\n",
1900 ioc->name, response_code, desc);
1901}
1902
1903/**
1904 * _scsih_tm_done - tm completion routine
1905 * @ioc: per adapter object
1906 * @smid: system request message index
1907 * @msix_index: MSIX table index supplied by the OS
1908 * @reply: reply message frame(lower 32bit addr)
1909 * Context: none.
1910 *
1911 * The callback handler when using scsih_issue_tm.
1912 *
1913 * Return 1 meaning mf should be freed from _base_interrupt
1914 * 0 means the mf is freed from this function.
1915 */
1916static u8
1917_scsih_tm_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
1918{
1919 MPI2DefaultReply_t *mpi_reply;
1920
1921 if (ioc->tm_cmds.status == MPT3_CMD_NOT_USED)
1922 return 1;
1923 if (ioc->tm_cmds.smid != smid)
1924 return 1;
1925 mpt3sas_base_flush_reply_queues(ioc);
1926 ioc->tm_cmds.status |= MPT3_CMD_COMPLETE;
1927 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
1928 if (mpi_reply) {
1929 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
1930 ioc->tm_cmds.status |= MPT3_CMD_REPLY_VALID;
1931 }
1932 ioc->tm_cmds.status &= ~MPT3_CMD_PENDING;
1933 complete(&ioc->tm_cmds.done);
1934 return 1;
1935}
1936
1937/**
1938 * mpt3sas_scsih_set_tm_flag - set per target tm_busy
1939 * @ioc: per adapter object
1940 * @handle: device handle
1941 *
1942 * During taskmangement request, we need to freeze the device queue.
1943 */
1944void
1945mpt3sas_scsih_set_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1946{
1947 struct MPT3SAS_DEVICE *sas_device_priv_data;
1948 struct scsi_device *sdev;
1949 u8 skip = 0;
1950
1951 shost_for_each_device(sdev, ioc->shost) {
1952 if (skip)
1953 continue;
1954 sas_device_priv_data = sdev->hostdata;
1955 if (!sas_device_priv_data)
1956 continue;
1957 if (sas_device_priv_data->sas_target->handle == handle) {
1958 sas_device_priv_data->sas_target->tm_busy = 1;
1959 skip = 1;
1960 ioc->ignore_loginfos = 1;
1961 }
1962 }
1963}
1964
1965/**
1966 * mpt3sas_scsih_clear_tm_flag - clear per target tm_busy
1967 * @ioc: per adapter object
1968 * @handle: device handle
1969 *
1970 * During taskmangement request, we need to freeze the device queue.
1971 */
1972void
1973mpt3sas_scsih_clear_tm_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
1974{
1975 struct MPT3SAS_DEVICE *sas_device_priv_data;
1976 struct scsi_device *sdev;
1977 u8 skip = 0;
1978
1979 shost_for_each_device(sdev, ioc->shost) {
1980 if (skip)
1981 continue;
1982 sas_device_priv_data = sdev->hostdata;
1983 if (!sas_device_priv_data)
1984 continue;
1985 if (sas_device_priv_data->sas_target->handle == handle) {
1986 sas_device_priv_data->sas_target->tm_busy = 0;
1987 skip = 1;
1988 ioc->ignore_loginfos = 0;
1989 }
1990 }
1991}
1992
1993/**
1994 * mpt3sas_scsih_issue_tm - main routine for sending tm requests
1995 * @ioc: per adapter struct
1996 * @device_handle: device handle
1997 * @channel: the channel assigned by the OS
1998 * @id: the id assigned by the OS
1999 * @lun: lun number
2000 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2001 * @smid_task: smid assigned to the task
2002 * @timeout: timeout in seconds
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302003 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
2004 * Context: user
2005 *
2006 * A generic API for sending task management requests to firmware.
2007 *
2008 * The callback index is set inside `ioc->tm_cb_idx`.
2009 *
2010 * Return SUCCESS or FAILED.
2011 */
2012int
2013mpt3sas_scsih_issue_tm(struct MPT3SAS_ADAPTER *ioc, u16 handle, uint channel,
2014 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002015 enum mutex_type m_type)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302016{
2017 Mpi2SCSITaskManagementRequest_t *mpi_request;
2018 Mpi2SCSITaskManagementReply_t *mpi_reply;
2019 u16 smid = 0;
2020 u32 ioc_state;
2021 unsigned long timeleft;
2022 struct scsiio_tracker *scsi_lookup = NULL;
2023 int rc;
2024
2025 if (m_type == TM_MUTEX_ON)
2026 mutex_lock(&ioc->tm_cmds.mutex);
2027 if (ioc->tm_cmds.status != MPT3_CMD_NOT_USED) {
2028 pr_info(MPT3SAS_FMT "%s: tm_cmd busy!!!\n",
2029 __func__, ioc->name);
2030 rc = FAILED;
2031 goto err_out;
2032 }
2033
2034 if (ioc->shost_recovery || ioc->remove_host ||
2035 ioc->pci_error_recovery) {
2036 pr_info(MPT3SAS_FMT "%s: host reset in progress!\n",
2037 __func__, ioc->name);
2038 rc = FAILED;
2039 goto err_out;
2040 }
2041
2042 ioc_state = mpt3sas_base_get_iocstate(ioc, 0);
2043 if (ioc_state & MPI2_DOORBELL_USED) {
2044 dhsprintk(ioc, pr_info(MPT3SAS_FMT
2045 "unexpected doorbell active!\n", ioc->name));
2046 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2047 FORCE_BIG_HAMMER);
2048 rc = (!rc) ? SUCCESS : FAILED;
2049 goto err_out;
2050 }
2051
2052 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2053 mpt3sas_base_fault_info(ioc, ioc_state &
2054 MPI2_DOORBELL_DATA_MASK);
2055 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2056 FORCE_BIG_HAMMER);
2057 rc = (!rc) ? SUCCESS : FAILED;
2058 goto err_out;
2059 }
2060
2061 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
2062 if (!smid) {
2063 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
2064 ioc->name, __func__);
2065 rc = FAILED;
2066 goto err_out;
2067 }
2068
2069 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2070 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2071
2072 dtmprintk(ioc, pr_info(MPT3SAS_FMT
2073 "sending tm: handle(0x%04x), task_type(0x%02x), smid(%d)\n",
2074 ioc->name, handle, type, smid_task));
2075 ioc->tm_cmds.status = MPT3_CMD_PENDING;
2076 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2077 ioc->tm_cmds.smid = smid;
2078 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2079 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
2080 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2081 mpi_request->DevHandle = cpu_to_le16(handle);
2082 mpi_request->TaskType = type;
2083 mpi_request->TaskMID = cpu_to_le16(smid_task);
2084 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2085 mpt3sas_scsih_set_tm_flag(ioc, handle);
2086 init_completion(&ioc->tm_cmds.done);
2087 mpt3sas_base_put_smid_hi_priority(ioc, smid);
2088 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
2089 if (!(ioc->tm_cmds.status & MPT3_CMD_COMPLETE)) {
2090 pr_err(MPT3SAS_FMT "%s: timeout\n",
2091 ioc->name, __func__);
2092 _debug_dump_mf(mpi_request,
2093 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2094 if (!(ioc->tm_cmds.status & MPT3_CMD_RESET)) {
2095 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2096 FORCE_BIG_HAMMER);
2097 rc = (!rc) ? SUCCESS : FAILED;
2098 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2099 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2100 goto err_out;
2101 }
2102 }
2103
2104 if (ioc->tm_cmds.status & MPT3_CMD_REPLY_VALID) {
2105 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
2106 mpi_reply = ioc->tm_cmds.reply;
2107 dtmprintk(ioc, pr_info(MPT3SAS_FMT "complete tm: " \
2108 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2109 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2110 le32_to_cpu(mpi_reply->IOCLogInfo),
2111 le32_to_cpu(mpi_reply->TerminationCount)));
2112 if (ioc->logging_level & MPT_DEBUG_TM) {
2113 _scsih_response_code(ioc, mpi_reply->ResponseCode);
2114 if (mpi_reply->IOCStatus)
2115 _debug_dump_mf(mpi_request,
2116 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2117 }
2118 }
2119
2120 switch (type) {
2121 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2122 rc = SUCCESS;
2123 if (scsi_lookup->scmd == NULL)
2124 break;
2125 rc = FAILED;
2126 break;
2127
2128 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2129 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2130 rc = FAILED;
2131 else
2132 rc = SUCCESS;
2133 break;
2134 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
2135 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2136 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2137 rc = FAILED;
2138 else
2139 rc = SUCCESS;
2140 break;
2141 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2142 rc = SUCCESS;
2143 break;
2144 default:
2145 rc = FAILED;
2146 break;
2147 }
2148
2149 mpt3sas_scsih_clear_tm_flag(ioc, handle);
2150 ioc->tm_cmds.status = MPT3_CMD_NOT_USED;
2151 if (m_type == TM_MUTEX_ON)
2152 mutex_unlock(&ioc->tm_cmds.mutex);
2153
2154 return rc;
2155
2156 err_out:
2157 if (m_type == TM_MUTEX_ON)
2158 mutex_unlock(&ioc->tm_cmds.mutex);
2159 return rc;
2160}
2161
2162/**
2163 * _scsih_tm_display_info - displays info about the device
2164 * @ioc: per adapter struct
2165 * @scmd: pointer to scsi command object
2166 *
2167 * Called by task management callback handlers.
2168 */
2169static void
2170_scsih_tm_display_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2171{
2172 struct scsi_target *starget = scmd->device->sdev_target;
2173 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
2174 struct _sas_device *sas_device = NULL;
2175 unsigned long flags;
2176 char *device_str = NULL;
2177
2178 if (!priv_target)
2179 return;
2180 device_str = "volume";
2181
2182 scsi_print_command(scmd);
2183 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2184 starget_printk(KERN_INFO, starget,
2185 "%s handle(0x%04x), %s wwid(0x%016llx)\n",
2186 device_str, priv_target->handle,
2187 device_str, (unsigned long long)priv_target->sas_address);
2188 } else {
2189 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2190 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
2191 priv_target->sas_address);
2192 if (sas_device) {
2193 if (priv_target->flags &
2194 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2195 starget_printk(KERN_INFO, starget,
2196 "volume handle(0x%04x), "
2197 "volume wwid(0x%016llx)\n",
2198 sas_device->volume_handle,
2199 (unsigned long long)sas_device->volume_wwid);
2200 }
2201 starget_printk(KERN_INFO, starget,
2202 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2203 sas_device->handle,
2204 (unsigned long long)sas_device->sas_address,
2205 sas_device->phy);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05302206 if (sas_device->enclosure_handle != 0)
2207 starget_printk(KERN_INFO, starget,
2208 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2209 (unsigned long long)
2210 sas_device->enclosure_logical_id,
2211 sas_device->slot);
2212 if (sas_device->connector_name)
2213 starget_printk(KERN_INFO, starget,
2214 "enclosure level(0x%04x),connector name(%s)\n",
2215 sas_device->enclosure_level,
2216 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302217 }
2218 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2219 }
2220}
2221
2222/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302223 * scsih_abort - eh threads main abort routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302224 * @scmd: pointer to scsi command object
2225 *
2226 * Returns SUCCESS if command aborted else FAILED
2227 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302228int
2229scsih_abort(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302230{
2231 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2232 struct MPT3SAS_DEVICE *sas_device_priv_data;
2233 u16 smid;
2234 u16 handle;
2235 int r;
2236
2237 sdev_printk(KERN_INFO, scmd->device,
2238 "attempting task abort! scmd(%p)\n", scmd);
2239 _scsih_tm_display_info(ioc, scmd);
2240
2241 sas_device_priv_data = scmd->device->hostdata;
2242 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2243 sdev_printk(KERN_INFO, scmd->device,
2244 "device been deleted! scmd(%p)\n", scmd);
2245 scmd->result = DID_NO_CONNECT << 16;
2246 scmd->scsi_done(scmd);
2247 r = SUCCESS;
2248 goto out;
2249 }
2250
2251 /* search for the command */
2252 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2253 if (!smid) {
2254 scmd->result = DID_RESET << 16;
2255 r = SUCCESS;
2256 goto out;
2257 }
2258
2259 /* for hidden raid components and volumes this is not supported */
2260 if (sas_device_priv_data->sas_target->flags &
2261 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2262 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2263 scmd->result = DID_RESET << 16;
2264 r = FAILED;
2265 goto out;
2266 }
2267
2268 mpt3sas_halt_firmware(ioc);
2269
2270 handle = sas_device_priv_data->sas_target->handle;
2271 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2272 scmd->device->id, scmd->device->lun,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002273 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302274
2275 out:
2276 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2277 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2278 return r;
2279}
2280
2281/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302282 * scsih_dev_reset - eh threads main device reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302283 * @scmd: pointer to scsi command object
2284 *
2285 * Returns SUCCESS if command aborted else FAILED
2286 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302287int
2288scsih_dev_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302289{
2290 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2291 struct MPT3SAS_DEVICE *sas_device_priv_data;
2292 struct _sas_device *sas_device;
2293 unsigned long flags;
2294 u16 handle;
2295 int r;
2296
2297 sdev_printk(KERN_INFO, scmd->device,
2298 "attempting device reset! scmd(%p)\n", scmd);
2299 _scsih_tm_display_info(ioc, scmd);
2300
2301 sas_device_priv_data = scmd->device->hostdata;
2302 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2303 sdev_printk(KERN_INFO, scmd->device,
2304 "device been deleted! scmd(%p)\n", scmd);
2305 scmd->result = DID_NO_CONNECT << 16;
2306 scmd->scsi_done(scmd);
2307 r = SUCCESS;
2308 goto out;
2309 }
2310
2311 /* for hidden raid components obtain the volume_handle */
2312 handle = 0;
2313 if (sas_device_priv_data->sas_target->flags &
2314 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2315 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2316 sas_device = _scsih_sas_device_find_by_handle(ioc,
2317 sas_device_priv_data->sas_target->handle);
2318 if (sas_device)
2319 handle = sas_device->volume_handle;
2320 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2321 } else
2322 handle = sas_device_priv_data->sas_target->handle;
2323
2324 if (!handle) {
2325 scmd->result = DID_RESET << 16;
2326 r = FAILED;
2327 goto out;
2328 }
2329
2330 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2331 scmd->device->id, scmd->device->lun,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002332 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302333
2334 out:
2335 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2336 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2337 return r;
2338}
2339
2340/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302341 * scsih_target_reset - eh threads main target reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302342 * @scmd: pointer to scsi command object
2343 *
2344 * Returns SUCCESS if command aborted else FAILED
2345 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302346int
2347scsih_target_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302348{
2349 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2350 struct MPT3SAS_DEVICE *sas_device_priv_data;
2351 struct _sas_device *sas_device;
2352 unsigned long flags;
2353 u16 handle;
2354 int r;
2355 struct scsi_target *starget = scmd->device->sdev_target;
2356
2357 starget_printk(KERN_INFO, starget, "attempting target reset! scmd(%p)\n",
2358 scmd);
2359 _scsih_tm_display_info(ioc, scmd);
2360
2361 sas_device_priv_data = scmd->device->hostdata;
2362 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
2363 starget_printk(KERN_INFO, starget, "target been deleted! scmd(%p)\n",
2364 scmd);
2365 scmd->result = DID_NO_CONNECT << 16;
2366 scmd->scsi_done(scmd);
2367 r = SUCCESS;
2368 goto out;
2369 }
2370
2371 /* for hidden raid components obtain the volume_handle */
2372 handle = 0;
2373 if (sas_device_priv_data->sas_target->flags &
2374 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2375 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2376 sas_device = _scsih_sas_device_find_by_handle(ioc,
2377 sas_device_priv_data->sas_target->handle);
2378 if (sas_device)
2379 handle = sas_device->volume_handle;
2380 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2381 } else
2382 handle = sas_device_priv_data->sas_target->handle;
2383
2384 if (!handle) {
2385 scmd->result = DID_RESET << 16;
2386 r = FAILED;
2387 goto out;
2388 }
2389
2390 r = mpt3sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2391 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04002392 30, TM_MUTEX_ON);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302393
2394 out:
2395 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2396 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2397 return r;
2398}
2399
2400
2401/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302402 * scsih_host_reset - eh threads main host reset routine
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302403 * @scmd: pointer to scsi command object
2404 *
2405 * Returns SUCCESS if command aborted else FAILED
2406 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05302407int
2408scsih_host_reset(struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302409{
2410 struct MPT3SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2411 int r, retval;
2412
2413 pr_info(MPT3SAS_FMT "attempting host reset! scmd(%p)\n",
2414 ioc->name, scmd);
2415 scsi_print_command(scmd);
2416
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302417 if (ioc->is_driver_loading) {
2418 pr_info(MPT3SAS_FMT "Blocking the host reset\n",
2419 ioc->name);
2420 r = FAILED;
2421 goto out;
2422 }
2423
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302424 retval = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2425 FORCE_BIG_HAMMER);
2426 r = (retval < 0) ? FAILED : SUCCESS;
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302427out:
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302428 pr_info(MPT3SAS_FMT "host reset: %s scmd(%p)\n",
2429 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2430
2431 return r;
2432}
2433
2434/**
2435 * _scsih_fw_event_add - insert and queue up fw_event
2436 * @ioc: per adapter object
2437 * @fw_event: object describing the event
2438 * Context: This function will acquire ioc->fw_event_lock.
2439 *
2440 * This adds the firmware event object into link list, then queues it up to
2441 * be processed from user context.
2442 *
2443 * Return nothing.
2444 */
2445static void
2446_scsih_fw_event_add(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2447{
2448 unsigned long flags;
2449
2450 if (ioc->firmware_event_thread == NULL)
2451 return;
2452
2453 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2454 INIT_LIST_HEAD(&fw_event->list);
2455 list_add_tail(&fw_event->list, &ioc->fw_event_list);
2456 INIT_WORK(&fw_event->work, _firmware_event_work);
2457 queue_work(ioc->firmware_event_thread, &fw_event->work);
2458 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2459}
2460
2461/**
2462 * _scsih_fw_event_free - delete fw_event
2463 * @ioc: per adapter object
2464 * @fw_event: object describing the event
2465 * Context: This function will acquire ioc->fw_event_lock.
2466 *
2467 * This removes firmware event object from link list, frees associated memory.
2468 *
2469 * Return nothing.
2470 */
2471static void
2472_scsih_fw_event_free(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work
2473 *fw_event)
2474{
2475 unsigned long flags;
2476
2477 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2478 list_del(&fw_event->list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302479 kfree(fw_event);
2480 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2481}
2482
2483
2484 /**
2485 * mpt3sas_send_trigger_data_event - send event for processing trigger data
2486 * @ioc: per adapter object
2487 * @event_data: trigger event data
2488 *
2489 * Return nothing.
2490 */
2491void
2492mpt3sas_send_trigger_data_event(struct MPT3SAS_ADAPTER *ioc,
2493 struct SL_WH_TRIGGERS_EVENT_DATA_T *event_data)
2494{
2495 struct fw_event_work *fw_event;
2496
2497 if (ioc->is_driver_loading)
2498 return;
Joe Lawrence35b62362014-06-25 17:05:34 -04002499 fw_event = kzalloc(sizeof(*fw_event) + sizeof(*event_data),
2500 GFP_ATOMIC);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302501 if (!fw_event)
2502 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302503 fw_event->event = MPT3SAS_PROCESS_TRIGGER_DIAG;
2504 fw_event->ioc = ioc;
2505 memcpy(fw_event->event_data, event_data, sizeof(*event_data));
2506 _scsih_fw_event_add(ioc, fw_event);
2507}
2508
2509/**
2510 * _scsih_error_recovery_delete_devices - remove devices not responding
2511 * @ioc: per adapter object
2512 *
2513 * Return nothing.
2514 */
2515static void
2516_scsih_error_recovery_delete_devices(struct MPT3SAS_ADAPTER *ioc)
2517{
2518 struct fw_event_work *fw_event;
2519
2520 if (ioc->is_driver_loading)
2521 return;
2522 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2523 if (!fw_event)
2524 return;
2525 fw_event->event = MPT3SAS_REMOVE_UNRESPONDING_DEVICES;
2526 fw_event->ioc = ioc;
2527 _scsih_fw_event_add(ioc, fw_event);
2528}
2529
2530/**
2531 * mpt3sas_port_enable_complete - port enable completed (fake event)
2532 * @ioc: per adapter object
2533 *
2534 * Return nothing.
2535 */
2536void
2537mpt3sas_port_enable_complete(struct MPT3SAS_ADAPTER *ioc)
2538{
2539 struct fw_event_work *fw_event;
2540
2541 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2542 if (!fw_event)
2543 return;
2544 fw_event->event = MPT3SAS_PORT_ENABLE_COMPLETE;
2545 fw_event->ioc = ioc;
2546 _scsih_fw_event_add(ioc, fw_event);
2547}
2548
2549/**
2550 * _scsih_fw_event_cleanup_queue - cleanup event queue
2551 * @ioc: per adapter object
2552 *
2553 * Walk the firmware event queue, either killing timers, or waiting
2554 * for outstanding events to complete
2555 *
2556 * Return nothing.
2557 */
2558static void
2559_scsih_fw_event_cleanup_queue(struct MPT3SAS_ADAPTER *ioc)
2560{
2561 struct fw_event_work *fw_event, *next;
2562
2563 if (list_empty(&ioc->fw_event_list) ||
2564 !ioc->firmware_event_thread || in_interrupt())
2565 return;
2566
2567 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05302568 if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302569 _scsih_fw_event_free(ioc, fw_event);
2570 continue;
2571 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302572 }
2573}
2574
2575/**
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302576 * _scsih_internal_device_block - block the sdev device
2577 * @sdev: per device object
2578 * @sas_device_priv_data : per device driver private data
2579 *
2580 * make sure device is blocked without error, if not
2581 * print an error
2582 */
2583static void
2584_scsih_internal_device_block(struct scsi_device *sdev,
2585 struct MPT3SAS_DEVICE *sas_device_priv_data)
2586{
2587 int r = 0;
2588
2589 sdev_printk(KERN_INFO, sdev, "device_block, handle(0x%04x)\n",
2590 sas_device_priv_data->sas_target->handle);
2591 sas_device_priv_data->block = 1;
2592
2593 r = scsi_internal_device_block(sdev);
2594 if (r == -EINVAL)
2595 sdev_printk(KERN_WARNING, sdev,
2596 "device_block failed with return(%d) for handle(0x%04x)\n",
2597 sas_device_priv_data->sas_target->handle, r);
2598}
2599
2600/**
2601 * _scsih_internal_device_unblock - unblock the sdev device
2602 * @sdev: per device object
2603 * @sas_device_priv_data : per device driver private data
2604 * make sure device is unblocked without error, if not retry
2605 * by blocking and then unblocking
2606 */
2607
2608static void
2609_scsih_internal_device_unblock(struct scsi_device *sdev,
2610 struct MPT3SAS_DEVICE *sas_device_priv_data)
2611{
2612 int r = 0;
2613
2614 sdev_printk(KERN_WARNING, sdev, "device_unblock and setting to running, "
2615 "handle(0x%04x)\n", sas_device_priv_data->sas_target->handle);
2616 sas_device_priv_data->block = 0;
2617 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2618 if (r == -EINVAL) {
2619 /* The device has been set to SDEV_RUNNING by SD layer during
2620 * device addition but the request queue is still stopped by
2621 * our earlier block call. We need to perform a block again
2622 * to get the device to SDEV_BLOCK and then to SDEV_RUNNING */
2623
2624 sdev_printk(KERN_WARNING, sdev,
2625 "device_unblock failed with return(%d) for handle(0x%04x) "
2626 "performing a block followed by an unblock\n",
2627 sas_device_priv_data->sas_target->handle, r);
2628 sas_device_priv_data->block = 1;
2629 r = scsi_internal_device_block(sdev);
2630 if (r)
2631 sdev_printk(KERN_WARNING, sdev, "retried device_block "
2632 "failed with return(%d) for handle(0x%04x)\n",
2633 sas_device_priv_data->sas_target->handle, r);
2634
2635 sas_device_priv_data->block = 0;
2636 r = scsi_internal_device_unblock(sdev, SDEV_RUNNING);
2637 if (r)
2638 sdev_printk(KERN_WARNING, sdev, "retried device_unblock"
2639 " failed with return(%d) for handle(0x%04x)\n",
2640 sas_device_priv_data->sas_target->handle, r);
2641 }
2642}
2643
2644/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302645 * _scsih_ublock_io_all_device - unblock every device
2646 * @ioc: per adapter object
2647 *
2648 * change the device state from block to running
2649 */
2650static void
2651_scsih_ublock_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2652{
2653 struct MPT3SAS_DEVICE *sas_device_priv_data;
2654 struct scsi_device *sdev;
2655
2656 shost_for_each_device(sdev, ioc->shost) {
2657 sas_device_priv_data = sdev->hostdata;
2658 if (!sas_device_priv_data)
2659 continue;
2660 if (!sas_device_priv_data->block)
2661 continue;
2662
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302663 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2664 "device_running, handle(0x%04x)\n",
2665 sas_device_priv_data->sas_target->handle));
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302666 _scsih_internal_device_unblock(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302667 }
2668}
2669
2670
2671/**
2672 * _scsih_ublock_io_device - prepare device to be deleted
2673 * @ioc: per adapter object
2674 * @sas_addr: sas address
2675 *
2676 * unblock then put device in offline state
2677 */
2678static void
2679_scsih_ublock_io_device(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
2680{
2681 struct MPT3SAS_DEVICE *sas_device_priv_data;
2682 struct scsi_device *sdev;
2683
2684 shost_for_each_device(sdev, ioc->shost) {
2685 sas_device_priv_data = sdev->hostdata;
2686 if (!sas_device_priv_data)
2687 continue;
2688 if (sas_device_priv_data->sas_target->sas_address
2689 != sas_address)
2690 continue;
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302691 if (sas_device_priv_data->block)
2692 _scsih_internal_device_unblock(sdev,
2693 sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302694 }
2695}
2696
2697/**
2698 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2699 * @ioc: per adapter object
2700 * @handle: device handle
2701 *
2702 * During device pull we need to appropiately set the sdev state.
2703 */
2704static void
2705_scsih_block_io_all_device(struct MPT3SAS_ADAPTER *ioc)
2706{
2707 struct MPT3SAS_DEVICE *sas_device_priv_data;
2708 struct scsi_device *sdev;
2709
2710 shost_for_each_device(sdev, ioc->shost) {
2711 sas_device_priv_data = sdev->hostdata;
2712 if (!sas_device_priv_data)
2713 continue;
2714 if (sas_device_priv_data->block)
2715 continue;
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302716 _scsih_internal_device_block(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302717 }
2718}
2719
2720/**
2721 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2722 * @ioc: per adapter object
2723 * @handle: device handle
2724 *
2725 * During device pull we need to appropiately set the sdev state.
2726 */
2727static void
2728_scsih_block_io_device(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2729{
2730 struct MPT3SAS_DEVICE *sas_device_priv_data;
2731 struct scsi_device *sdev;
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302732 struct _sas_device *sas_device;
2733
2734 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2735 if (!sas_device)
2736 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302737
2738 shost_for_each_device(sdev, ioc->shost) {
2739 sas_device_priv_data = sdev->hostdata;
2740 if (!sas_device_priv_data)
2741 continue;
2742 if (sas_device_priv_data->sas_target->handle != handle)
2743 continue;
2744 if (sas_device_priv_data->block)
2745 continue;
Sreekanth Reddye4bc7f52015-06-30 12:24:49 +05302746 if (sas_device->pend_sas_rphy_add)
2747 continue;
Sreekanth Reddydf838f92015-06-30 12:25:05 +05302748 _scsih_internal_device_block(sdev, sas_device_priv_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302749 }
2750}
2751
2752/**
2753 * _scsih_block_io_to_children_attached_to_ex
2754 * @ioc: per adapter object
2755 * @sas_expander: the sas_device object
2756 *
2757 * This routine set sdev state to SDEV_BLOCK for all devices
2758 * attached to this expander. This function called when expander is
2759 * pulled.
2760 */
2761static void
2762_scsih_block_io_to_children_attached_to_ex(struct MPT3SAS_ADAPTER *ioc,
2763 struct _sas_node *sas_expander)
2764{
2765 struct _sas_port *mpt3sas_port;
2766 struct _sas_device *sas_device;
2767 struct _sas_node *expander_sibling;
2768 unsigned long flags;
2769
2770 if (!sas_expander)
2771 return;
2772
2773 list_for_each_entry(mpt3sas_port,
2774 &sas_expander->sas_port_list, port_list) {
2775 if (mpt3sas_port->remote_identify.device_type ==
2776 SAS_END_DEVICE) {
2777 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2778 sas_device =
2779 mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
2780 mpt3sas_port->remote_identify.sas_address);
2781 if (sas_device)
2782 set_bit(sas_device->handle,
2783 ioc->blocking_handles);
2784 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2785 }
2786 }
2787
2788 list_for_each_entry(mpt3sas_port,
2789 &sas_expander->sas_port_list, port_list) {
2790
2791 if (mpt3sas_port->remote_identify.device_type ==
2792 SAS_EDGE_EXPANDER_DEVICE ||
2793 mpt3sas_port->remote_identify.device_type ==
2794 SAS_FANOUT_EXPANDER_DEVICE) {
2795 expander_sibling =
2796 mpt3sas_scsih_expander_find_by_sas_address(
2797 ioc, mpt3sas_port->remote_identify.sas_address);
2798 _scsih_block_io_to_children_attached_to_ex(ioc,
2799 expander_sibling);
2800 }
2801 }
2802}
2803
2804/**
2805 * _scsih_block_io_to_children_attached_directly
2806 * @ioc: per adapter object
2807 * @event_data: topology change event data
2808 *
2809 * This routine set sdev state to SDEV_BLOCK for all devices
2810 * direct attached during device pull.
2811 */
2812static void
2813_scsih_block_io_to_children_attached_directly(struct MPT3SAS_ADAPTER *ioc,
2814 Mpi2EventDataSasTopologyChangeList_t *event_data)
2815{
2816 int i;
2817 u16 handle;
2818 u16 reason_code;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302819
2820 for (i = 0; i < event_data->NumEntries; i++) {
2821 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2822 if (!handle)
2823 continue;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302824 reason_code = event_data->PHY[i].PhyStatus &
2825 MPI2_EVENT_SAS_TOPO_RC_MASK;
2826 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2827 _scsih_block_io_device(ioc, handle);
2828 }
2829}
2830
2831/**
2832 * _scsih_tm_tr_send - send task management request
2833 * @ioc: per adapter object
2834 * @handle: device handle
2835 * Context: interrupt time.
2836 *
2837 * This code is to initiate the device removal handshake protocol
2838 * with controller firmware. This function will issue target reset
2839 * using high priority request queue. It will send a sas iounit
2840 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
2841 *
2842 * This is designed to send muliple task management request at the same
2843 * time to the fifo. If the fifo is full, we will append the request,
2844 * and process it in a future completion.
2845 */
2846static void
2847_scsih_tm_tr_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
2848{
2849 Mpi2SCSITaskManagementRequest_t *mpi_request;
2850 u16 smid;
2851 struct _sas_device *sas_device;
2852 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
2853 u64 sas_address = 0;
2854 unsigned long flags;
2855 struct _tr_list *delayed_tr;
2856 u32 ioc_state;
2857
2858 if (ioc->remove_host) {
2859 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2860 "%s: host has been removed: handle(0x%04x)\n",
2861 __func__, ioc->name, handle));
2862 return;
2863 } else if (ioc->pci_error_recovery) {
2864 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2865 "%s: host in pci error recovery: handle(0x%04x)\n",
2866 __func__, ioc->name,
2867 handle));
2868 return;
2869 }
2870 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2871 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2872 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2873 "%s: host is not operational: handle(0x%04x)\n",
2874 __func__, ioc->name,
2875 handle));
2876 return;
2877 }
2878
2879 /* if PD, then return */
2880 if (test_bit(handle, ioc->pd_handles))
2881 return;
2882
2883 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2884 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
2885 if (sas_device && sas_device->starget &&
2886 sas_device->starget->hostdata) {
2887 sas_target_priv_data = sas_device->starget->hostdata;
2888 sas_target_priv_data->deleted = 1;
2889 sas_address = sas_device->sas_address;
2890 }
2891 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2892
2893 if (sas_target_priv_data) {
2894 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2895 "setting delete flag: handle(0x%04x), sas_addr(0x%016llx)\n",
2896 ioc->name, handle,
2897 (unsigned long long)sas_address));
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05302898 if (sas_device->enclosure_handle != 0)
2899 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2900 "setting delete flag:enclosure logical id(0x%016llx),"
2901 " slot(%d)\n", ioc->name, (unsigned long long)
2902 sas_device->enclosure_logical_id,
2903 sas_device->slot));
2904 if (sas_device->connector_name)
2905 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2906 "setting delete flag: enclosure level(0x%04x),"
2907 " connector name( %s)\n", ioc->name,
2908 sas_device->enclosure_level,
2909 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05302910 _scsih_ublock_io_device(ioc, sas_address);
2911 sas_target_priv_data->handle = MPT3SAS_INVALID_DEVICE_HANDLE;
2912 }
2913
2914 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2915 if (!smid) {
2916 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2917 if (!delayed_tr)
2918 return;
2919 INIT_LIST_HEAD(&delayed_tr->list);
2920 delayed_tr->handle = handle;
2921 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2922 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2923 "DELAYED:tr:handle(0x%04x), (open)\n",
2924 ioc->name, handle));
2925 return;
2926 }
2927
2928 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2929 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
2930 ioc->name, handle, smid,
2931 ioc->tm_tr_cb_idx));
2932 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
2933 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2934 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2935 mpi_request->DevHandle = cpu_to_le16(handle);
2936 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
2937 mpt3sas_base_put_smid_hi_priority(ioc, smid);
2938 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_DEVICE_REMOVAL);
2939}
2940
2941/**
2942 * _scsih_tm_tr_complete -
2943 * @ioc: per adapter object
2944 * @smid: system request message index
2945 * @msix_index: MSIX table index supplied by the OS
2946 * @reply: reply message frame(lower 32bit addr)
2947 * Context: interrupt time.
2948 *
2949 * This is the target reset completion routine.
2950 * This code is part of the code to initiate the device removal
2951 * handshake protocol with controller firmware.
2952 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
2953 *
2954 * Return 1 meaning mf should be freed from _base_interrupt
2955 * 0 means the mf is freed from this function.
2956 */
2957static u8
2958_scsih_tm_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
2959 u32 reply)
2960{
2961 u16 handle;
2962 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
2963 Mpi2SCSITaskManagementReply_t *mpi_reply =
2964 mpt3sas_base_get_reply_virt_addr(ioc, reply);
2965 Mpi2SasIoUnitControlRequest_t *mpi_request;
2966 u16 smid_sas_ctrl;
2967 u32 ioc_state;
2968
2969 if (ioc->remove_host) {
2970 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2971 "%s: host has been removed\n", __func__, ioc->name));
2972 return 1;
2973 } else if (ioc->pci_error_recovery) {
2974 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2975 "%s: host in pci error recovery\n", __func__,
2976 ioc->name));
2977 return 1;
2978 }
2979 ioc_state = mpt3sas_base_get_iocstate(ioc, 1);
2980 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
2981 dewtprintk(ioc, pr_info(MPT3SAS_FMT
2982 "%s: host is not operational\n", __func__, ioc->name));
2983 return 1;
2984 }
2985 if (unlikely(!mpi_reply)) {
2986 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
2987 ioc->name, __FILE__, __LINE__, __func__);
2988 return 1;
2989 }
2990 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
2991 handle = le16_to_cpu(mpi_request_tm->DevHandle);
2992 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
2993 dewtprintk(ioc, pr_err(MPT3SAS_FMT
2994 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
2995 ioc->name, handle,
2996 le16_to_cpu(mpi_reply->DevHandle), smid));
2997 return 0;
2998 }
2999
3000 mpt3sas_trigger_master(ioc, MASTER_TRIGGER_TASK_MANAGMENT);
3001 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3002 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3003 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3004 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3005 le32_to_cpu(mpi_reply->IOCLogInfo),
3006 le32_to_cpu(mpi_reply->TerminationCount)));
3007
3008 smid_sas_ctrl = mpt3sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3009 if (!smid_sas_ctrl) {
3010 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3011 ioc->name, __func__);
3012 return 1;
3013 }
3014
3015 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3016 "sc_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3017 ioc->name, handle, smid_sas_ctrl,
3018 ioc->tm_sas_control_cb_idx));
3019 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3020 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3021 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3022 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
3023 mpi_request->DevHandle = mpi_request_tm->DevHandle;
3024 mpt3sas_base_put_smid_default(ioc, smid_sas_ctrl);
3025
3026 return _scsih_check_for_pending_tm(ioc, smid);
3027}
3028
3029
3030/**
3031 * _scsih_sas_control_complete - completion routine
3032 * @ioc: per adapter object
3033 * @smid: system request message index
3034 * @msix_index: MSIX table index supplied by the OS
3035 * @reply: reply message frame(lower 32bit addr)
3036 * Context: interrupt time.
3037 *
3038 * This is the sas iounit control completion routine.
3039 * This code is part of the code to initiate the device removal
3040 * handshake protocol with controller firmware.
3041 *
3042 * Return 1 meaning mf should be freed from _base_interrupt
3043 * 0 means the mf is freed from this function.
3044 */
3045static u8
3046_scsih_sas_control_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3047 u8 msix_index, u32 reply)
3048{
3049 Mpi2SasIoUnitControlReply_t *mpi_reply =
3050 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3051
3052 if (likely(mpi_reply)) {
3053 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3054 "sc_complete:handle(0x%04x), (open) "
3055 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3056 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3057 le16_to_cpu(mpi_reply->IOCStatus),
3058 le32_to_cpu(mpi_reply->IOCLogInfo)));
3059 } else {
3060 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3061 ioc->name, __FILE__, __LINE__, __func__);
3062 }
3063 return 1;
3064}
3065
3066/**
3067 * _scsih_tm_tr_volume_send - send target reset request for volumes
3068 * @ioc: per adapter object
3069 * @handle: device handle
3070 * Context: interrupt time.
3071 *
3072 * This is designed to send muliple task management request at the same
3073 * time to the fifo. If the fifo is full, we will append the request,
3074 * and process it in a future completion.
3075 */
3076static void
3077_scsih_tm_tr_volume_send(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3078{
3079 Mpi2SCSITaskManagementRequest_t *mpi_request;
3080 u16 smid;
3081 struct _tr_list *delayed_tr;
3082
3083 if (ioc->shost_recovery || ioc->remove_host ||
3084 ioc->pci_error_recovery) {
3085 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3086 "%s: host reset in progress!\n",
3087 __func__, ioc->name));
3088 return;
3089 }
3090
3091 smid = mpt3sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3092 if (!smid) {
3093 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3094 if (!delayed_tr)
3095 return;
3096 INIT_LIST_HEAD(&delayed_tr->list);
3097 delayed_tr->handle = handle;
3098 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3099 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3100 "DELAYED:tr:handle(0x%04x), (open)\n",
3101 ioc->name, handle));
3102 return;
3103 }
3104
3105 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3106 "tr_send:handle(0x%04x), (open), smid(%d), cb(%d)\n",
3107 ioc->name, handle, smid,
3108 ioc->tm_tr_volume_cb_idx));
3109 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3110 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3111 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3112 mpi_request->DevHandle = cpu_to_le16(handle);
3113 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3114 mpt3sas_base_put_smid_hi_priority(ioc, smid);
3115}
3116
3117/**
3118 * _scsih_tm_volume_tr_complete - target reset completion
3119 * @ioc: per adapter object
3120 * @smid: system request message index
3121 * @msix_index: MSIX table index supplied by the OS
3122 * @reply: reply message frame(lower 32bit addr)
3123 * Context: interrupt time.
3124 *
3125 * Return 1 meaning mf should be freed from _base_interrupt
3126 * 0 means the mf is freed from this function.
3127 */
3128static u8
3129_scsih_tm_volume_tr_complete(struct MPT3SAS_ADAPTER *ioc, u16 smid,
3130 u8 msix_index, u32 reply)
3131{
3132 u16 handle;
3133 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3134 Mpi2SCSITaskManagementReply_t *mpi_reply =
3135 mpt3sas_base_get_reply_virt_addr(ioc, reply);
3136
3137 if (ioc->shost_recovery || ioc->remove_host ||
3138 ioc->pci_error_recovery) {
3139 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3140 "%s: host reset in progress!\n",
3141 __func__, ioc->name));
3142 return 1;
3143 }
3144 if (unlikely(!mpi_reply)) {
3145 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3146 ioc->name, __FILE__, __LINE__, __func__);
3147 return 1;
3148 }
3149
3150 mpi_request_tm = mpt3sas_base_get_msg_frame(ioc, smid);
3151 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3152 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3153 dewtprintk(ioc, pr_err(MPT3SAS_FMT
3154 "spurious interrupt: handle(0x%04x:0x%04x), smid(%d)!!!\n",
3155 ioc->name, handle,
3156 le16_to_cpu(mpi_reply->DevHandle), smid));
3157 return 0;
3158 }
3159
3160 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3161 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3162 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3163 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3164 le32_to_cpu(mpi_reply->IOCLogInfo),
3165 le32_to_cpu(mpi_reply->TerminationCount)));
3166
3167 return _scsih_check_for_pending_tm(ioc, smid);
3168}
3169
3170
3171/**
3172 * _scsih_check_for_pending_tm - check for pending task management
3173 * @ioc: per adapter object
3174 * @smid: system request message index
3175 *
3176 * This will check delayed target reset list, and feed the
3177 * next reqeust.
3178 *
3179 * Return 1 meaning mf should be freed from _base_interrupt
3180 * 0 means the mf is freed from this function.
3181 */
3182static u8
3183_scsih_check_for_pending_tm(struct MPT3SAS_ADAPTER *ioc, u16 smid)
3184{
3185 struct _tr_list *delayed_tr;
3186
3187 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3188 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3189 struct _tr_list, list);
3190 mpt3sas_base_free_smid(ioc, smid);
3191 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3192 list_del(&delayed_tr->list);
3193 kfree(delayed_tr);
3194 return 0;
3195 }
3196
3197 if (!list_empty(&ioc->delayed_tr_list)) {
3198 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3199 struct _tr_list, list);
3200 mpt3sas_base_free_smid(ioc, smid);
3201 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3202 list_del(&delayed_tr->list);
3203 kfree(delayed_tr);
3204 return 0;
3205 }
3206
3207 return 1;
3208}
3209
3210/**
3211 * _scsih_check_topo_delete_events - sanity check on topo events
3212 * @ioc: per adapter object
3213 * @event_data: the event data payload
3214 *
3215 * This routine added to better handle cable breaker.
3216 *
3217 * This handles the case where driver receives multiple expander
3218 * add and delete events in a single shot. When there is a delete event
3219 * the routine will void any pending add events waiting in the event queue.
3220 *
3221 * Return nothing.
3222 */
3223static void
3224_scsih_check_topo_delete_events(struct MPT3SAS_ADAPTER *ioc,
3225 Mpi2EventDataSasTopologyChangeList_t *event_data)
3226{
3227 struct fw_event_work *fw_event;
3228 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3229 u16 expander_handle;
3230 struct _sas_node *sas_expander;
3231 unsigned long flags;
3232 int i, reason_code;
3233 u16 handle;
3234
3235 for (i = 0 ; i < event_data->NumEntries; i++) {
3236 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3237 if (!handle)
3238 continue;
3239 reason_code = event_data->PHY[i].PhyStatus &
3240 MPI2_EVENT_SAS_TOPO_RC_MASK;
3241 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3242 _scsih_tm_tr_send(ioc, handle);
3243 }
3244
3245 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3246 if (expander_handle < ioc->sas_hba.num_phys) {
3247 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3248 return;
3249 }
3250 if (event_data->ExpStatus ==
3251 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3252 /* put expander attached devices into blocking state */
3253 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3254 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
3255 expander_handle);
3256 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3257 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3258 do {
3259 handle = find_first_bit(ioc->blocking_handles,
3260 ioc->facts.MaxDevHandle);
3261 if (handle < ioc->facts.MaxDevHandle)
3262 _scsih_block_io_device(ioc, handle);
3263 } while (test_and_clear_bit(handle, ioc->blocking_handles));
3264 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3265 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3266
3267 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3268 return;
3269
3270 /* mark ignore flag for pending events */
3271 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3272 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3273 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3274 fw_event->ignore)
3275 continue;
Joe Lawrence35b62362014-06-25 17:05:34 -04003276 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3277 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303278 if (local_event_data->ExpStatus ==
3279 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3280 local_event_data->ExpStatus ==
3281 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3282 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3283 expander_handle) {
3284 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3285 "setting ignoring flag\n", ioc->name));
3286 fw_event->ignore = 1;
3287 }
3288 }
3289 }
3290 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3291}
3292
3293/**
3294 * _scsih_set_volume_delete_flag - setting volume delete flag
3295 * @ioc: per adapter object
3296 * @handle: device handle
3297 *
3298 * This returns nothing.
3299 */
3300static void
3301_scsih_set_volume_delete_flag(struct MPT3SAS_ADAPTER *ioc, u16 handle)
3302{
3303 struct _raid_device *raid_device;
3304 struct MPT3SAS_TARGET *sas_target_priv_data;
3305 unsigned long flags;
3306
3307 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3308 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3309 if (raid_device && raid_device->starget &&
3310 raid_device->starget->hostdata) {
3311 sas_target_priv_data =
3312 raid_device->starget->hostdata;
3313 sas_target_priv_data->deleted = 1;
3314 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3315 "setting delete flag: handle(0x%04x), "
3316 "wwid(0x%016llx)\n", ioc->name, handle,
3317 (unsigned long long) raid_device->wwid));
3318 }
3319 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3320}
3321
3322/**
3323 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3324 * @handle: input handle
3325 * @a: handle for volume a
3326 * @b: handle for volume b
3327 *
3328 * IR firmware only supports two raid volumes. The purpose of this
3329 * routine is to set the volume handle in either a or b. When the given
3330 * input handle is non-zero, or when a and b have not been set before.
3331 */
3332static void
3333_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3334{
3335 if (!handle || handle == *a || handle == *b)
3336 return;
3337 if (!*a)
3338 *a = handle;
3339 else if (!*b)
3340 *b = handle;
3341}
3342
3343/**
3344 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3345 * @ioc: per adapter object
3346 * @event_data: the event data payload
3347 * Context: interrupt time.
3348 *
3349 * This routine will send target reset to volume, followed by target
3350 * resets to the PDs. This is called when a PD has been removed, or
3351 * volume has been deleted or removed. When the target reset is sent
3352 * to volume, the PD target resets need to be queued to start upon
3353 * completion of the volume target reset.
3354 *
3355 * Return nothing.
3356 */
3357static void
3358_scsih_check_ir_config_unhide_events(struct MPT3SAS_ADAPTER *ioc,
3359 Mpi2EventDataIrConfigChangeList_t *event_data)
3360{
3361 Mpi2EventIrConfigElement_t *element;
3362 int i;
3363 u16 handle, volume_handle, a, b;
3364 struct _tr_list *delayed_tr;
3365
3366 a = 0;
3367 b = 0;
3368
3369 /* Volume Resets for Deleted or Removed */
3370 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3371 for (i = 0; i < event_data->NumElements; i++, element++) {
3372 if (le32_to_cpu(event_data->Flags) &
3373 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3374 continue;
3375 if (element->ReasonCode ==
3376 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3377 element->ReasonCode ==
3378 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3379 volume_handle = le16_to_cpu(element->VolDevHandle);
3380 _scsih_set_volume_delete_flag(ioc, volume_handle);
3381 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3382 }
3383 }
3384
3385 /* Volume Resets for UNHIDE events */
3386 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3387 for (i = 0; i < event_data->NumElements; i++, element++) {
3388 if (le32_to_cpu(event_data->Flags) &
3389 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3390 continue;
3391 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3392 volume_handle = le16_to_cpu(element->VolDevHandle);
3393 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3394 }
3395 }
3396
3397 if (a)
3398 _scsih_tm_tr_volume_send(ioc, a);
3399 if (b)
3400 _scsih_tm_tr_volume_send(ioc, b);
3401
3402 /* PD target resets */
3403 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3404 for (i = 0; i < event_data->NumElements; i++, element++) {
3405 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3406 continue;
3407 handle = le16_to_cpu(element->PhysDiskDevHandle);
3408 volume_handle = le16_to_cpu(element->VolDevHandle);
3409 clear_bit(handle, ioc->pd_handles);
3410 if (!volume_handle)
3411 _scsih_tm_tr_send(ioc, handle);
3412 else if (volume_handle == a || volume_handle == b) {
3413 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3414 BUG_ON(!delayed_tr);
3415 INIT_LIST_HEAD(&delayed_tr->list);
3416 delayed_tr->handle = handle;
3417 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3418 dewtprintk(ioc, pr_info(MPT3SAS_FMT
3419 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3420 handle));
3421 } else
3422 _scsih_tm_tr_send(ioc, handle);
3423 }
3424}
3425
3426
3427/**
3428 * _scsih_check_volume_delete_events - set delete flag for volumes
3429 * @ioc: per adapter object
3430 * @event_data: the event data payload
3431 * Context: interrupt time.
3432 *
3433 * This will handle the case when the cable connected to entire volume is
3434 * pulled. We will take care of setting the deleted flag so normal IO will
3435 * not be sent.
3436 *
3437 * Return nothing.
3438 */
3439static void
3440_scsih_check_volume_delete_events(struct MPT3SAS_ADAPTER *ioc,
3441 Mpi2EventDataIrVolume_t *event_data)
3442{
3443 u32 state;
3444
3445 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3446 return;
3447 state = le32_to_cpu(event_data->NewValue);
3448 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3449 MPI2_RAID_VOL_STATE_FAILED)
3450 _scsih_set_volume_delete_flag(ioc,
3451 le16_to_cpu(event_data->VolDevHandle));
3452}
3453
3454/**
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303455 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3456 * @ioc: per adapter object
3457 * @event_data: the temp threshold event data
3458 * Context: interrupt time.
3459 *
3460 * Return nothing.
3461 */
3462static void
3463_scsih_temp_threshold_events(struct MPT3SAS_ADAPTER *ioc,
3464 Mpi2EventDataTemperature_t *event_data)
3465{
3466 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3467 pr_err(MPT3SAS_FMT "Temperature Threshold flags %s%s%s%s"
3468 " exceeded for Sensor: %d !!!\n", ioc->name,
3469 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3470 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3471 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3472 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3473 event_data->SensorNum);
3474 pr_err(MPT3SAS_FMT "Current Temp In Celsius: %d\n",
3475 ioc->name, event_data->CurrentTemperature);
3476 }
3477}
3478
3479/**
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303480 * _scsih_flush_running_cmds - completing outstanding commands.
3481 * @ioc: per adapter object
3482 *
3483 * The flushing out of all pending scmd commands following host reset,
3484 * where all IO is dropped to the floor.
3485 *
3486 * Return nothing.
3487 */
3488static void
3489_scsih_flush_running_cmds(struct MPT3SAS_ADAPTER *ioc)
3490{
3491 struct scsi_cmnd *scmd;
3492 u16 smid;
3493 u16 count = 0;
3494
3495 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
3496 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
3497 if (!scmd)
3498 continue;
3499 count++;
3500 mpt3sas_base_free_smid(ioc, smid);
3501 scsi_dma_unmap(scmd);
3502 if (ioc->pci_error_recovery)
3503 scmd->result = DID_NO_CONNECT << 16;
3504 else
3505 scmd->result = DID_RESET << 16;
3506 scmd->scsi_done(scmd);
3507 }
3508 dtmprintk(ioc, pr_info(MPT3SAS_FMT "completing %d cmds\n",
3509 ioc->name, count));
3510}
3511
3512/**
3513 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3514 * @ioc: per adapter object
3515 * @scmd: pointer to scsi command object
3516 * @mpi_request: pointer to the SCSI_IO reqest message frame
3517 *
3518 * Supporting protection 1 and 3.
3519 *
3520 * Returns nothing
3521 */
3522static void
3523_scsih_setup_eedp(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3524 Mpi2SCSIIORequest_t *mpi_request)
3525{
3526 u16 eedp_flags;
3527 unsigned char prot_op = scsi_get_prot_op(scmd);
3528 unsigned char prot_type = scsi_get_prot_type(scmd);
3529 Mpi25SCSIIORequest_t *mpi_request_3v =
3530 (Mpi25SCSIIORequest_t *)mpi_request;
3531
3532 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
3533 return;
3534
3535 if (prot_op == SCSI_PROT_READ_STRIP)
3536 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3537 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3538 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3539 else
3540 return;
3541
3542 switch (prot_type) {
3543 case SCSI_PROT_DIF_TYPE1:
3544 case SCSI_PROT_DIF_TYPE2:
3545
3546 /*
3547 * enable ref/guard checking
3548 * auto increment ref tag
3549 */
3550 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3551 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3552 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3553 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3554 cpu_to_be32(scsi_get_lba(scmd));
3555 break;
3556
3557 case SCSI_PROT_DIF_TYPE3:
3558
3559 /*
3560 * enable guard checking
3561 */
3562 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3563
3564 break;
3565 }
3566
3567 mpi_request_3v->EEDPBlockSize =
3568 cpu_to_le16(scmd->device->sector_size);
3569 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
3570}
3571
3572/**
3573 * _scsih_eedp_error_handling - return sense code for EEDP errors
3574 * @scmd: pointer to scsi command object
3575 * @ioc_status: ioc status
3576 *
3577 * Returns nothing
3578 */
3579static void
3580_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3581{
3582 u8 ascq;
3583
3584 switch (ioc_status) {
3585 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3586 ascq = 0x01;
3587 break;
3588 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3589 ascq = 0x02;
3590 break;
3591 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3592 ascq = 0x03;
3593 break;
3594 default:
3595 ascq = 0x00;
3596 break;
3597 }
3598 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10,
3599 ascq);
3600 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
3601 SAM_STAT_CHECK_CONDITION;
3602}
3603
3604
3605/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303606 * scsih_qcmd - main scsi request entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303607 * @scmd: pointer to scsi command object
3608 * @done: function pointer to be invoked on completion
3609 *
3610 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3611 *
3612 * Returns 0 on success. If there's a failure, return either:
3613 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3614 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3615 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05303616int
3617scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303618{
Matthew Wilcoxd8bfbd82014-03-27 16:40:31 -04003619 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303620 struct MPT3SAS_DEVICE *sas_device_priv_data;
3621 struct MPT3SAS_TARGET *sas_target_priv_data;
3622 Mpi2SCSIIORequest_t *mpi_request;
3623 u32 mpi_control;
3624 u16 smid;
3625 u16 handle;
3626
3627#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3628 if (ioc->logging_level & MPT_DEBUG_SCSI)
3629 scsi_print_command(scmd);
3630#endif
3631
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303632 sas_device_priv_data = scmd->device->hostdata;
3633 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
3634 scmd->result = DID_NO_CONNECT << 16;
3635 scmd->scsi_done(scmd);
3636 return 0;
3637 }
3638
3639 if (ioc->pci_error_recovery || ioc->remove_host) {
3640 scmd->result = DID_NO_CONNECT << 16;
3641 scmd->scsi_done(scmd);
3642 return 0;
3643 }
3644
3645 sas_target_priv_data = sas_device_priv_data->sas_target;
3646
3647 /* invalid device handle */
3648 handle = sas_target_priv_data->handle;
3649 if (handle == MPT3SAS_INVALID_DEVICE_HANDLE) {
3650 scmd->result = DID_NO_CONNECT << 16;
3651 scmd->scsi_done(scmd);
3652 return 0;
3653 }
3654
3655
3656 /* host recovery or link resets sent via IOCTLs */
3657 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
3658 return SCSI_MLQUEUE_HOST_BUSY;
3659
3660 /* device has been deleted */
3661 else if (sas_target_priv_data->deleted) {
3662 scmd->result = DID_NO_CONNECT << 16;
3663 scmd->scsi_done(scmd);
3664 return 0;
3665 /* device busy with task managment */
3666 } else if (sas_target_priv_data->tm_busy ||
3667 sas_device_priv_data->block)
3668 return SCSI_MLQUEUE_DEVICE_BUSY;
3669
3670 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3671 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3672 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3673 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3674 else
3675 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3676
3677 /* set tags */
Christoph Hellwig609aa222014-10-30 11:54:58 +01003678 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303679
3680 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
3681 scmd->cmd_len != 32)
3682 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3683
3684 smid = mpt3sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
3685 if (!smid) {
3686 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
3687 ioc->name, __func__);
3688 goto out;
3689 }
3690 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
3691 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
3692 _scsih_setup_eedp(ioc, scmd, mpi_request);
3693
3694 if (scmd->cmd_len == 32)
3695 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
3696 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3697 if (sas_device_priv_data->sas_target->flags &
3698 MPT_TARGET_FLAGS_RAID_COMPONENT)
3699 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3700 else
3701 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3702 mpi_request->DevHandle = cpu_to_le16(handle);
3703 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3704 mpi_request->Control = cpu_to_le32(mpi_control);
3705 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3706 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3707 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3708 mpi_request->SenseBufferLowAddress =
3709 mpt3sas_base_get_sense_buffer_dma(ioc, smid);
3710 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3711 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3712 mpi_request->LUN);
3713 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3714
3715 if (mpi_request->DataLength) {
3716 if (ioc->build_sg_scmd(ioc, scmd, smid)) {
3717 mpt3sas_base_free_smid(ioc, smid);
3718 goto out;
3719 }
3720 } else
3721 ioc->build_zero_len_sge(ioc, &mpi_request->SGL);
3722
3723 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST)) {
3724 if (sas_target_priv_data->flags & MPT_TARGET_FASTPATH_IO) {
3725 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len |
3726 MPI25_SCSIIO_IOFLAGS_FAST_PATH);
3727 mpt3sas_base_put_smid_fast_path(ioc, smid, handle);
3728 } else
3729 mpt3sas_base_put_smid_scsi_io(ioc, smid, handle);
3730 } else
3731 mpt3sas_base_put_smid_default(ioc, smid);
3732 return 0;
3733
3734 out:
3735 return SCSI_MLQUEUE_HOST_BUSY;
3736}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303737
3738/**
3739 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3740 * @sense_buffer: sense data returned by target
3741 * @data: normalized skey/asc/ascq
3742 *
3743 * Return nothing.
3744 */
3745static void
3746_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3747{
3748 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3749 /* descriptor format */
3750 data->skey = sense_buffer[1] & 0x0F;
3751 data->asc = sense_buffer[2];
3752 data->ascq = sense_buffer[3];
3753 } else {
3754 /* fixed format */
3755 data->skey = sense_buffer[2] & 0x0F;
3756 data->asc = sense_buffer[12];
3757 data->ascq = sense_buffer[13];
3758 }
3759}
3760
3761#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
3762/**
3763 * _scsih_scsi_ioc_info - translated non-succesfull SCSI_IO request
3764 * @ioc: per adapter object
3765 * @scmd: pointer to scsi command object
3766 * @mpi_reply: reply mf payload returned from firmware
3767 *
3768 * scsi_status - SCSI Status code returned from target device
3769 * scsi_state - state info associated with SCSI_IO determined by ioc
3770 * ioc_status - ioc supplied status info
3771 *
3772 * Return nothing.
3773 */
3774static void
3775_scsih_scsi_ioc_info(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3776 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3777{
3778 u32 response_info;
3779 u8 *response_bytes;
3780 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3781 MPI2_IOCSTATUS_MASK;
3782 u8 scsi_state = mpi_reply->SCSIState;
3783 u8 scsi_status = mpi_reply->SCSIStatus;
3784 char *desc_ioc_state = NULL;
3785 char *desc_scsi_status = NULL;
3786 char *desc_scsi_state = ioc->tmp_string;
3787 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
3788 struct _sas_device *sas_device = NULL;
3789 unsigned long flags;
3790 struct scsi_target *starget = scmd->device->sdev_target;
3791 struct MPT3SAS_TARGET *priv_target = starget->hostdata;
3792 char *device_str = NULL;
3793
3794 if (!priv_target)
3795 return;
3796 device_str = "volume";
3797
3798 if (log_info == 0x31170000)
3799 return;
3800
3801 switch (ioc_status) {
3802 case MPI2_IOCSTATUS_SUCCESS:
3803 desc_ioc_state = "success";
3804 break;
3805 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3806 desc_ioc_state = "invalid function";
3807 break;
3808 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3809 desc_ioc_state = "scsi recovered error";
3810 break;
3811 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3812 desc_ioc_state = "scsi invalid dev handle";
3813 break;
3814 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3815 desc_ioc_state = "scsi device not there";
3816 break;
3817 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3818 desc_ioc_state = "scsi data overrun";
3819 break;
3820 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3821 desc_ioc_state = "scsi data underrun";
3822 break;
3823 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3824 desc_ioc_state = "scsi io data error";
3825 break;
3826 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3827 desc_ioc_state = "scsi protocol error";
3828 break;
3829 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3830 desc_ioc_state = "scsi task terminated";
3831 break;
3832 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3833 desc_ioc_state = "scsi residual mismatch";
3834 break;
3835 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3836 desc_ioc_state = "scsi task mgmt failed";
3837 break;
3838 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3839 desc_ioc_state = "scsi ioc terminated";
3840 break;
3841 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3842 desc_ioc_state = "scsi ext terminated";
3843 break;
3844 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3845 desc_ioc_state = "eedp guard error";
3846 break;
3847 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3848 desc_ioc_state = "eedp ref tag error";
3849 break;
3850 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3851 desc_ioc_state = "eedp app tag error";
3852 break;
3853 default:
3854 desc_ioc_state = "unknown";
3855 break;
3856 }
3857
3858 switch (scsi_status) {
3859 case MPI2_SCSI_STATUS_GOOD:
3860 desc_scsi_status = "good";
3861 break;
3862 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3863 desc_scsi_status = "check condition";
3864 break;
3865 case MPI2_SCSI_STATUS_CONDITION_MET:
3866 desc_scsi_status = "condition met";
3867 break;
3868 case MPI2_SCSI_STATUS_BUSY:
3869 desc_scsi_status = "busy";
3870 break;
3871 case MPI2_SCSI_STATUS_INTERMEDIATE:
3872 desc_scsi_status = "intermediate";
3873 break;
3874 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3875 desc_scsi_status = "intermediate condmet";
3876 break;
3877 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3878 desc_scsi_status = "reservation conflict";
3879 break;
3880 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3881 desc_scsi_status = "command terminated";
3882 break;
3883 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3884 desc_scsi_status = "task set full";
3885 break;
3886 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3887 desc_scsi_status = "aca active";
3888 break;
3889 case MPI2_SCSI_STATUS_TASK_ABORTED:
3890 desc_scsi_status = "task aborted";
3891 break;
3892 default:
3893 desc_scsi_status = "unknown";
3894 break;
3895 }
3896
3897 desc_scsi_state[0] = '\0';
3898 if (!scsi_state)
3899 desc_scsi_state = " ";
3900 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3901 strcat(desc_scsi_state, "response info ");
3902 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3903 strcat(desc_scsi_state, "state terminated ");
3904 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3905 strcat(desc_scsi_state, "no status ");
3906 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3907 strcat(desc_scsi_state, "autosense failed ");
3908 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
3909 strcat(desc_scsi_state, "autosense valid ");
3910
3911 scsi_print_command(scmd);
3912
3913 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
3914 pr_warn(MPT3SAS_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
3915 device_str, (unsigned long long)priv_target->sas_address);
3916 } else {
3917 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3918 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
3919 priv_target->sas_address);
3920 if (sas_device) {
3921 pr_warn(MPT3SAS_FMT
3922 "\tsas_address(0x%016llx), phy(%d)\n",
3923 ioc->name, (unsigned long long)
3924 sas_device->sas_address, sas_device->phy);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05303925 if (sas_device->enclosure_handle != 0)
3926 pr_warn(MPT3SAS_FMT
3927 "\tenclosure_logical_id(0x%016llx),"
3928 "slot(%d)\n", ioc->name,
3929 (unsigned long long)
3930 sas_device->enclosure_logical_id,
3931 sas_device->slot);
3932 if (sas_device->connector_name[0])
3933 pr_warn(MPT3SAS_FMT
3934 "\tenclosure level(0x%04x),"
3935 " connector name( %s)\n", ioc->name,
3936 sas_device->enclosure_level,
3937 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303938 }
3939 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3940 }
3941
3942 pr_warn(MPT3SAS_FMT
3943 "\thandle(0x%04x), ioc_status(%s)(0x%04x), smid(%d)\n",
3944 ioc->name, le16_to_cpu(mpi_reply->DevHandle),
3945 desc_ioc_state, ioc_status, smid);
3946 pr_warn(MPT3SAS_FMT
3947 "\trequest_len(%d), underflow(%d), resid(%d)\n",
3948 ioc->name, scsi_bufflen(scmd), scmd->underflow,
3949 scsi_get_resid(scmd));
3950 pr_warn(MPT3SAS_FMT
3951 "\ttag(%d), transfer_count(%d), sc->result(0x%08x)\n",
3952 ioc->name, le16_to_cpu(mpi_reply->TaskTag),
3953 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
3954 pr_warn(MPT3SAS_FMT
3955 "\tscsi_status(%s)(0x%02x), scsi_state(%s)(0x%02x)\n",
3956 ioc->name, desc_scsi_status,
3957 scsi_status, desc_scsi_state, scsi_state);
3958
3959 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
3960 struct sense_info data;
3961 _scsih_normalize_sense(scmd->sense_buffer, &data);
3962 pr_warn(MPT3SAS_FMT
3963 "\t[sense_key,asc,ascq]: [0x%02x,0x%02x,0x%02x], count(%d)\n",
3964 ioc->name, data.skey,
3965 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
3966 }
3967
3968 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
3969 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
3970 response_bytes = (u8 *)&response_info;
3971 _scsih_response_code(ioc, response_bytes[0]);
3972 }
3973}
3974#endif
3975
3976/**
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05303977 * _scsih_turn_on_pfa_led - illuminate PFA LED
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303978 * @ioc: per adapter object
3979 * @handle: device handle
3980 * Context: process
3981 *
3982 * Return nothing.
3983 */
3984static void
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05303985_scsih_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303986{
3987 Mpi2SepReply_t mpi_reply;
3988 Mpi2SepRequest_t mpi_request;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05303989 struct _sas_device *sas_device;
3990
3991 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
3992 if (!sas_device)
3993 return;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05303994
3995 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
3996 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
3997 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
3998 mpi_request.SlotStatus =
3999 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4000 mpi_request.DevHandle = cpu_to_le16(handle);
4001 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4002 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4003 &mpi_request)) != 0) {
4004 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4005 __FILE__, __LINE__, __func__);
4006 return;
4007 }
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304008 sas_device->pfa_led_on = 1;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304009
4010 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4011 dewtprintk(ioc, pr_info(MPT3SAS_FMT
4012 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4013 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4014 le32_to_cpu(mpi_reply.IOCLogInfo)));
4015 return;
4016 }
4017}
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304018/**
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304019 * _scsih_turn_off_pfa_led - turn off Fault LED
4020 * @ioc: per adapter object
4021 * @sas_device: sas device whose PFA LED has to turned off
4022 * Context: process
4023 *
4024 * Return nothing.
4025 */
4026static void
4027_scsih_turn_off_pfa_led(struct MPT3SAS_ADAPTER *ioc,
4028 struct _sas_device *sas_device)
4029{
4030 Mpi2SepReply_t mpi_reply;
4031 Mpi2SepRequest_t mpi_request;
4032
4033 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4034 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4035 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4036 mpi_request.SlotStatus = 0;
4037 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4038 mpi_request.DevHandle = 0;
4039 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4040 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4041 if ((mpt3sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4042 &mpi_request)) != 0) {
4043 printk(MPT3SAS_FMT "failure at %s:%d/%s()!\n", ioc->name,
4044 __FILE__, __LINE__, __func__);
4045 return;
4046 }
4047
4048 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4049 dewtprintk(ioc, printk(MPT3SAS_FMT
4050 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4051 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4052 le32_to_cpu(mpi_reply.IOCLogInfo)));
4053 return;
4054 }
4055}
4056/**
4057 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304058 * @ioc: per adapter object
4059 * @handle: device handle
4060 * Context: interrupt.
4061 *
4062 * Return nothing.
4063 */
4064static void
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304065_scsih_send_event_to_turn_on_pfa_led(struct MPT3SAS_ADAPTER *ioc, u16 handle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304066{
4067 struct fw_event_work *fw_event;
4068
4069 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4070 if (!fw_event)
4071 return;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304072 fw_event->event = MPT3SAS_TURN_ON_PFA_LED;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304073 fw_event->device_handle = handle;
4074 fw_event->ioc = ioc;
4075 _scsih_fw_event_add(ioc, fw_event);
4076}
4077
4078/**
4079 * _scsih_smart_predicted_fault - process smart errors
4080 * @ioc: per adapter object
4081 * @handle: device handle
4082 * Context: interrupt.
4083 *
4084 * Return nothing.
4085 */
4086static void
4087_scsih_smart_predicted_fault(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4088{
4089 struct scsi_target *starget;
4090 struct MPT3SAS_TARGET *sas_target_priv_data;
4091 Mpi2EventNotificationReply_t *event_reply;
4092 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4093 struct _sas_device *sas_device;
4094 ssize_t sz;
4095 unsigned long flags;
4096
4097 /* only handle non-raid devices */
4098 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4099 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4100 if (!sas_device) {
4101 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4102 return;
4103 }
4104 starget = sas_device->starget;
4105 sas_target_priv_data = starget->hostdata;
4106
4107 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4108 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4109 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4110 return;
4111 }
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304112 if (sas_device->enclosure_handle != 0)
4113 starget_printk(KERN_INFO, starget, "predicted fault, "
4114 "enclosure logical id(0x%016llx), slot(%d)\n",
4115 (unsigned long long)sas_device->enclosure_logical_id,
4116 sas_device->slot);
4117 if (sas_device->connector_name[0] != '\0')
4118 starget_printk(KERN_WARNING, starget, "predicted fault, "
4119 "enclosure level(0x%04x), connector name( %s)\n",
4120 sas_device->enclosure_level,
4121 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304122 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4123
4124 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05304125 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304126
4127 /* insert into event log */
4128 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4129 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4130 event_reply = kzalloc(sz, GFP_KERNEL);
4131 if (!event_reply) {
4132 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4133 ioc->name, __FILE__, __LINE__, __func__);
4134 return;
4135 }
4136
4137 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4138 event_reply->Event =
4139 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4140 event_reply->MsgLength = sz/4;
4141 event_reply->EventDataLength =
4142 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4143 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4144 event_reply->EventData;
4145 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4146 event_data->ASC = 0x5D;
4147 event_data->DevHandle = cpu_to_le16(handle);
4148 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4149 mpt3sas_ctl_add_to_event_log(ioc, event_reply);
4150 kfree(event_reply);
4151}
4152
4153/**
4154 * _scsih_io_done - scsi request callback
4155 * @ioc: per adapter object
4156 * @smid: system request message index
4157 * @msix_index: MSIX table index supplied by the OS
4158 * @reply: reply message frame(lower 32bit addr)
4159 *
4160 * Callback handler when using _scsih_qcmd.
4161 *
4162 * Return 1 meaning mf should be freed from _base_interrupt
4163 * 0 means the mf is freed from this function.
4164 */
4165static u8
4166_scsih_io_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4167{
4168 Mpi2SCSIIORequest_t *mpi_request;
4169 Mpi2SCSIIOReply_t *mpi_reply;
4170 struct scsi_cmnd *scmd;
4171 u16 ioc_status;
4172 u32 xfer_cnt;
4173 u8 scsi_state;
4174 u8 scsi_status;
4175 u32 log_info;
4176 struct MPT3SAS_DEVICE *sas_device_priv_data;
4177 u32 response_code = 0;
4178
4179 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4180 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
4181 if (scmd == NULL)
4182 return 1;
4183
4184 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
4185
4186 if (mpi_reply == NULL) {
4187 scmd->result = DID_OK << 16;
4188 goto out;
4189 }
4190
4191 sas_device_priv_data = scmd->device->hostdata;
4192 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4193 sas_device_priv_data->sas_target->deleted) {
4194 scmd->result = DID_NO_CONNECT << 16;
4195 goto out;
4196 }
4197 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4198
4199 /* turning off TLR */
4200 scsi_state = mpi_reply->SCSIState;
4201 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4202 response_code =
4203 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
4204 if (!sas_device_priv_data->tlr_snoop_check) {
4205 sas_device_priv_data->tlr_snoop_check++;
4206 if ((sas_device_priv_data->flags & MPT_DEVICE_TLR_ON) &&
4207 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME)
4208 sas_device_priv_data->flags &=
4209 ~MPT_DEVICE_TLR_ON;
4210 }
4211
4212 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4213 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4214 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4215 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4216 else
4217 log_info = 0;
4218 ioc_status &= MPI2_IOCSTATUS_MASK;
4219 scsi_status = mpi_reply->SCSIStatus;
4220
4221 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4222 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4223 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4224 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4225 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4226 }
4227
4228 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4229 struct sense_info data;
4230 const void *sense_data = mpt3sas_base_get_sense_buffer(ioc,
4231 smid);
4232 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
4233 le32_to_cpu(mpi_reply->SenseCount));
4234 memcpy(scmd->sense_buffer, sense_data, sz);
4235 _scsih_normalize_sense(scmd->sense_buffer, &data);
4236 /* failure prediction threshold exceeded */
4237 if (data.asc == 0x5D)
4238 _scsih_smart_predicted_fault(ioc,
4239 le16_to_cpu(mpi_reply->DevHandle));
4240 mpt3sas_trigger_scsi(ioc, data.skey, data.asc, data.ascq);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304241
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304242#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4243 if (!(ioc->logging_level & MPT_DEBUG_REPLY) &&
4244 ((scmd->sense_buffer[2] == UNIT_ATTENTION) ||
4245 (scmd->sense_buffer[2] == MEDIUM_ERROR) ||
4246 (scmd->sense_buffer[2] == HARDWARE_ERROR)))
4247 _scsih_scsi_ioc_info(ioc, scmd, mpi_reply, smid);
4248#endif
4249 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304250 switch (ioc_status) {
4251 case MPI2_IOCSTATUS_BUSY:
4252 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4253 scmd->result = SAM_STAT_BUSY;
4254 break;
4255
4256 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4257 scmd->result = DID_NO_CONNECT << 16;
4258 break;
4259
4260 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4261 if (sas_device_priv_data->block) {
4262 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4263 goto out;
4264 }
4265 if (log_info == 0x31110630) {
4266 if (scmd->retries > 2) {
4267 scmd->result = DID_NO_CONNECT << 16;
4268 scsi_device_set_state(scmd->device,
4269 SDEV_OFFLINE);
4270 } else {
4271 scmd->result = DID_SOFT_ERROR << 16;
4272 scmd->device->expecting_cc_ua = 1;
4273 }
4274 break;
Sreekanth Reddy3898f082015-06-30 12:25:00 +05304275 } else if (log_info == VIRTUAL_IO_FAILED_RETRY) {
4276 scmd->result = DID_RESET << 16;
4277 break;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304278 }
4279 scmd->result = DID_SOFT_ERROR << 16;
4280 break;
4281 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4282 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4283 scmd->result = DID_RESET << 16;
4284 break;
4285
4286 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4287 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4288 scmd->result = DID_SOFT_ERROR << 16;
4289 else
4290 scmd->result = (DID_OK << 16) | scsi_status;
4291 break;
4292
4293 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4294 scmd->result = (DID_OK << 16) | scsi_status;
4295
4296 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4297 break;
4298
4299 if (xfer_cnt < scmd->underflow) {
4300 if (scsi_status == SAM_STAT_BUSY)
4301 scmd->result = SAM_STAT_BUSY;
4302 else
4303 scmd->result = DID_SOFT_ERROR << 16;
4304 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4305 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4306 scmd->result = DID_SOFT_ERROR << 16;
4307 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4308 scmd->result = DID_RESET << 16;
4309 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4310 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4311 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4312 scmd->result = (DRIVER_SENSE << 24) |
4313 SAM_STAT_CHECK_CONDITION;
4314 scmd->sense_buffer[0] = 0x70;
4315 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4316 scmd->sense_buffer[12] = 0x20;
4317 scmd->sense_buffer[13] = 0;
4318 }
4319 break;
4320
4321 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4322 scsi_set_resid(scmd, 0);
4323 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4324 case MPI2_IOCSTATUS_SUCCESS:
4325 scmd->result = (DID_OK << 16) | scsi_status;
4326 if (response_code ==
4327 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4328 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4329 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
4330 scmd->result = DID_SOFT_ERROR << 16;
4331 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4332 scmd->result = DID_RESET << 16;
4333 break;
4334
4335 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4336 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4337 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4338 _scsih_eedp_error_handling(scmd, ioc_status);
4339 break;
4340
4341 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4342 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4343 case MPI2_IOCSTATUS_INVALID_SGL:
4344 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4345 case MPI2_IOCSTATUS_INVALID_FIELD:
4346 case MPI2_IOCSTATUS_INVALID_STATE:
4347 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4348 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4349 default:
4350 scmd->result = DID_SOFT_ERROR << 16;
4351 break;
4352
4353 }
4354
4355#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
4356 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4357 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4358#endif
4359
4360 out:
4361
4362 scsi_dma_unmap(scmd);
4363
4364 scmd->scsi_done(scmd);
4365 return 1;
4366}
4367
4368/**
4369 * _scsih_sas_host_refresh - refreshing sas host object contents
4370 * @ioc: per adapter object
4371 * Context: user
4372 *
4373 * During port enable, fw will send topology events for every device. Its
4374 * possible that the handles may change from the previous setting, so this
4375 * code keeping handles updating if changed.
4376 *
4377 * Return nothing.
4378 */
4379static void
4380_scsih_sas_host_refresh(struct MPT3SAS_ADAPTER *ioc)
4381{
4382 u16 sz;
4383 u16 ioc_status;
4384 int i;
4385 Mpi2ConfigReply_t mpi_reply;
4386 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4387 u16 attached_handle;
4388 u8 link_rate;
4389
4390 dtmprintk(ioc, pr_info(MPT3SAS_FMT
4391 "updating handles for sas_host(0x%016llx)\n",
4392 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4393
4394 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4395 * sizeof(Mpi2SasIOUnit0PhyData_t));
4396 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4397 if (!sas_iounit_pg0) {
4398 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4399 ioc->name, __FILE__, __LINE__, __func__);
4400 return;
4401 }
4402
4403 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4404 sas_iounit_pg0, sz)) != 0)
4405 goto out;
4406 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4407 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4408 goto out;
4409 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4410 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
4411 if (i == 0)
4412 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4413 PhyData[0].ControllerDevHandle);
4414 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4415 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4416 AttachedDevHandle);
4417 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4418 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
4419 mpt3sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
4420 attached_handle, i, link_rate);
4421 }
4422 out:
4423 kfree(sas_iounit_pg0);
4424}
4425
4426/**
4427 * _scsih_sas_host_add - create sas host object
4428 * @ioc: per adapter object
4429 *
4430 * Creating host side data object, stored in ioc->sas_hba
4431 *
4432 * Return nothing.
4433 */
4434static void
4435_scsih_sas_host_add(struct MPT3SAS_ADAPTER *ioc)
4436{
4437 int i;
4438 Mpi2ConfigReply_t mpi_reply;
4439 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4440 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4441 Mpi2SasPhyPage0_t phy_pg0;
4442 Mpi2SasDevicePage0_t sas_device_pg0;
4443 Mpi2SasEnclosurePage0_t enclosure_pg0;
4444 u16 ioc_status;
4445 u16 sz;
4446 u8 device_missing_delay;
4447
4448 mpt3sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4449 if (!ioc->sas_hba.num_phys) {
4450 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4451 ioc->name, __FILE__, __LINE__, __func__);
4452 return;
4453 }
4454
4455 /* sas_iounit page 0 */
4456 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4457 sizeof(Mpi2SasIOUnit0PhyData_t));
4458 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4459 if (!sas_iounit_pg0) {
4460 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4461 ioc->name, __FILE__, __LINE__, __func__);
4462 return;
4463 }
4464 if ((mpt3sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4465 sas_iounit_pg0, sz))) {
4466 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4467 ioc->name, __FILE__, __LINE__, __func__);
4468 goto out;
4469 }
4470 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4471 MPI2_IOCSTATUS_MASK;
4472 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4473 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4474 ioc->name, __FILE__, __LINE__, __func__);
4475 goto out;
4476 }
4477
4478 /* sas_iounit page 1 */
4479 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4480 sizeof(Mpi2SasIOUnit1PhyData_t));
4481 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4482 if (!sas_iounit_pg1) {
4483 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4484 ioc->name, __FILE__, __LINE__, __func__);
4485 goto out;
4486 }
4487 if ((mpt3sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4488 sas_iounit_pg1, sz))) {
4489 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4490 ioc->name, __FILE__, __LINE__, __func__);
4491 goto out;
4492 }
4493 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4494 MPI2_IOCSTATUS_MASK;
4495 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4496 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4497 ioc->name, __FILE__, __LINE__, __func__);
4498 goto out;
4499 }
4500
4501 ioc->io_missing_delay =
4502 sas_iounit_pg1->IODeviceMissingDelay;
4503 device_missing_delay =
4504 sas_iounit_pg1->ReportDeviceMissingDelay;
4505 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4506 ioc->device_missing_delay = (device_missing_delay &
4507 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4508 else
4509 ioc->device_missing_delay = device_missing_delay &
4510 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4511
4512 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4513 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4514 sizeof(struct _sas_phy), GFP_KERNEL);
4515 if (!ioc->sas_hba.phy) {
4516 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4517 ioc->name, __FILE__, __LINE__, __func__);
4518 goto out;
4519 }
4520 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4521 if ((mpt3sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4522 i))) {
4523 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4524 ioc->name, __FILE__, __LINE__, __func__);
4525 goto out;
4526 }
4527 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4528 MPI2_IOCSTATUS_MASK;
4529 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4530 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4531 ioc->name, __FILE__, __LINE__, __func__);
4532 goto out;
4533 }
4534
4535 if (i == 0)
4536 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4537 PhyData[0].ControllerDevHandle);
4538 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4539 ioc->sas_hba.phy[i].phy_id = i;
4540 mpt3sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4541 phy_pg0, ioc->sas_hba.parent_dev);
4542 }
4543 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4544 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
4545 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4546 ioc->name, __FILE__, __LINE__, __func__);
4547 goto out;
4548 }
4549 ioc->sas_hba.enclosure_handle =
4550 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4551 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4552 pr_info(MPT3SAS_FMT
4553 "host_add: handle(0x%04x), sas_addr(0x%016llx), phys(%d)\n",
4554 ioc->name, ioc->sas_hba.handle,
4555 (unsigned long long) ioc->sas_hba.sas_address,
4556 ioc->sas_hba.num_phys) ;
4557
4558 if (ioc->sas_hba.enclosure_handle) {
4559 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4560 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4561 ioc->sas_hba.enclosure_handle)))
4562 ioc->sas_hba.enclosure_logical_id =
4563 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4564 }
4565
4566 out:
4567 kfree(sas_iounit_pg1);
4568 kfree(sas_iounit_pg0);
4569}
4570
4571/**
4572 * _scsih_expander_add - creating expander object
4573 * @ioc: per adapter object
4574 * @handle: expander handle
4575 *
4576 * Creating expander object, stored in ioc->sas_expander_list.
4577 *
4578 * Return 0 for success, else error.
4579 */
4580static int
4581_scsih_expander_add(struct MPT3SAS_ADAPTER *ioc, u16 handle)
4582{
4583 struct _sas_node *sas_expander;
4584 Mpi2ConfigReply_t mpi_reply;
4585 Mpi2ExpanderPage0_t expander_pg0;
4586 Mpi2ExpanderPage1_t expander_pg1;
4587 Mpi2SasEnclosurePage0_t enclosure_pg0;
4588 u32 ioc_status;
4589 u16 parent_handle;
4590 u64 sas_address, sas_address_parent = 0;
4591 int i;
4592 unsigned long flags;
4593 struct _sas_port *mpt3sas_port = NULL;
4594
4595 int rc = 0;
4596
4597 if (!handle)
4598 return -1;
4599
4600 if (ioc->shost_recovery || ioc->pci_error_recovery)
4601 return -1;
4602
4603 if ((mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4604 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4605 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4606 ioc->name, __FILE__, __LINE__, __func__);
4607 return -1;
4608 }
4609
4610 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4611 MPI2_IOCSTATUS_MASK;
4612 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4613 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4614 ioc->name, __FILE__, __LINE__, __func__);
4615 return -1;
4616 }
4617
4618 /* handle out of order topology events */
4619 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
4620 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4621 != 0) {
4622 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4623 ioc->name, __FILE__, __LINE__, __func__);
4624 return -1;
4625 }
4626 if (sas_address_parent != ioc->sas_hba.sas_address) {
4627 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4628 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4629 sas_address_parent);
4630 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4631 if (!sas_expander) {
4632 rc = _scsih_expander_add(ioc, parent_handle);
4633 if (rc != 0)
4634 return rc;
4635 }
4636 }
4637
4638 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4639 sas_address = le64_to_cpu(expander_pg0.SASAddress);
4640 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4641 sas_address);
4642 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4643
4644 if (sas_expander)
4645 return 0;
4646
4647 sas_expander = kzalloc(sizeof(struct _sas_node),
4648 GFP_KERNEL);
4649 if (!sas_expander) {
4650 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4651 ioc->name, __FILE__, __LINE__, __func__);
4652 return -1;
4653 }
4654
4655 sas_expander->handle = handle;
4656 sas_expander->num_phys = expander_pg0.NumPhys;
4657 sas_expander->sas_address_parent = sas_address_parent;
4658 sas_expander->sas_address = sas_address;
4659
4660 pr_info(MPT3SAS_FMT "expander_add: handle(0x%04x)," \
4661 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
4662 handle, parent_handle, (unsigned long long)
4663 sas_expander->sas_address, sas_expander->num_phys);
4664
4665 if (!sas_expander->num_phys)
4666 goto out_fail;
4667 sas_expander->phy = kcalloc(sas_expander->num_phys,
4668 sizeof(struct _sas_phy), GFP_KERNEL);
4669 if (!sas_expander->phy) {
4670 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4671 ioc->name, __FILE__, __LINE__, __func__);
4672 rc = -1;
4673 goto out_fail;
4674 }
4675
4676 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4677 mpt3sas_port = mpt3sas_transport_port_add(ioc, handle,
4678 sas_address_parent);
4679 if (!mpt3sas_port) {
4680 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4681 ioc->name, __FILE__, __LINE__, __func__);
4682 rc = -1;
4683 goto out_fail;
4684 }
4685 sas_expander->parent_dev = &mpt3sas_port->rphy->dev;
4686
4687 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4688 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
4689 &expander_pg1, i, handle))) {
4690 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4691 ioc->name, __FILE__, __LINE__, __func__);
4692 rc = -1;
4693 goto out_fail;
4694 }
4695 sas_expander->phy[i].handle = handle;
4696 sas_expander->phy[i].phy_id = i;
4697
4698 if ((mpt3sas_transport_add_expander_phy(ioc,
4699 &sas_expander->phy[i], expander_pg1,
4700 sas_expander->parent_dev))) {
4701 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4702 ioc->name, __FILE__, __LINE__, __func__);
4703 rc = -1;
4704 goto out_fail;
4705 }
4706 }
4707
4708 if (sas_expander->enclosure_handle) {
4709 if (!(mpt3sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4710 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4711 sas_expander->enclosure_handle)))
4712 sas_expander->enclosure_logical_id =
4713 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4714 }
4715
4716 _scsih_expander_node_add(ioc, sas_expander);
4717 return 0;
4718
4719 out_fail:
4720
4721 if (mpt3sas_port)
4722 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
4723 sas_address_parent);
4724 kfree(sas_expander);
4725 return rc;
4726}
4727
4728/**
4729 * mpt3sas_expander_remove - removing expander object
4730 * @ioc: per adapter object
4731 * @sas_address: expander sas_address
4732 *
4733 * Return nothing.
4734 */
4735void
4736mpt3sas_expander_remove(struct MPT3SAS_ADAPTER *ioc, u64 sas_address)
4737{
4738 struct _sas_node *sas_expander;
4739 unsigned long flags;
4740
4741 if (ioc->shost_recovery)
4742 return;
4743
4744 spin_lock_irqsave(&ioc->sas_node_lock, flags);
4745 sas_expander = mpt3sas_scsih_expander_find_by_sas_address(ioc,
4746 sas_address);
4747 if (sas_expander)
4748 list_del(&sas_expander->list);
4749 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4750 if (sas_expander)
4751 _scsih_expander_node_remove(ioc, sas_expander);
4752}
4753
4754/**
4755 * _scsih_done - internal SCSI_IO callback handler.
4756 * @ioc: per adapter object
4757 * @smid: system request message index
4758 * @msix_index: MSIX table index supplied by the OS
4759 * @reply: reply message frame(lower 32bit addr)
4760 *
4761 * Callback handler when sending internal generated SCSI_IO.
4762 * The callback index passed is `ioc->scsih_cb_idx`
4763 *
4764 * Return 1 meaning mf should be freed from _base_interrupt
4765 * 0 means the mf is freed from this function.
4766 */
4767static u8
4768_scsih_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4769{
4770 MPI2DefaultReply_t *mpi_reply;
4771
4772 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
4773 if (ioc->scsih_cmds.status == MPT3_CMD_NOT_USED)
4774 return 1;
4775 if (ioc->scsih_cmds.smid != smid)
4776 return 1;
4777 ioc->scsih_cmds.status |= MPT3_CMD_COMPLETE;
4778 if (mpi_reply) {
4779 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4780 mpi_reply->MsgLength*4);
4781 ioc->scsih_cmds.status |= MPT3_CMD_REPLY_VALID;
4782 }
4783 ioc->scsih_cmds.status &= ~MPT3_CMD_PENDING;
4784 complete(&ioc->scsih_cmds.done);
4785 return 1;
4786}
4787
4788
4789
4790
4791#define MPT3_MAX_LUNS (255)
4792
4793
4794/**
4795 * _scsih_check_access_status - check access flags
4796 * @ioc: per adapter object
4797 * @sas_address: sas address
4798 * @handle: sas device handle
4799 * @access_flags: errors returned during discovery of the device
4800 *
4801 * Return 0 for success, else failure
4802 */
4803static u8
4804_scsih_check_access_status(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
4805 u16 handle, u8 access_status)
4806{
4807 u8 rc = 1;
4808 char *desc = NULL;
4809
4810 switch (access_status) {
4811 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4812 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4813 rc = 0;
4814 break;
4815 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4816 desc = "sata capability failed";
4817 break;
4818 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4819 desc = "sata affiliation conflict";
4820 break;
4821 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4822 desc = "route not addressable";
4823 break;
4824 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4825 desc = "smp error not addressable";
4826 break;
4827 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4828 desc = "device blocked";
4829 break;
4830 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4831 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4832 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4833 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4834 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4835 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4836 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4837 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4838 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4839 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4840 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4841 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4842 desc = "sata initialization failed";
4843 break;
4844 default:
4845 desc = "unknown";
4846 break;
4847 }
4848
4849 if (!rc)
4850 return 0;
4851
4852 pr_err(MPT3SAS_FMT
4853 "discovery errors(%s): sas_address(0x%016llx), handle(0x%04x)\n",
4854 ioc->name, desc, (unsigned long long)sas_address, handle);
4855 return rc;
4856}
4857
4858/**
4859 * _scsih_check_device - checking device responsiveness
4860 * @ioc: per adapter object
4861 * @parent_sas_address: sas address of parent expander or sas host
4862 * @handle: attached device handle
4863 * @phy_numberv: phy number
4864 * @link_rate: new link rate
4865 *
4866 * Returns nothing.
4867 */
4868static void
4869_scsih_check_device(struct MPT3SAS_ADAPTER *ioc,
4870 u64 parent_sas_address, u16 handle, u8 phy_number, u8 link_rate)
4871{
4872 Mpi2ConfigReply_t mpi_reply;
4873 Mpi2SasDevicePage0_t sas_device_pg0;
4874 struct _sas_device *sas_device;
4875 u32 ioc_status;
4876 unsigned long flags;
4877 u64 sas_address;
4878 struct scsi_target *starget;
4879 struct MPT3SAS_TARGET *sas_target_priv_data;
4880 u32 device_info;
4881
4882
4883 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4884 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4885 return;
4886
4887 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4888 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4889 return;
4890
4891 /* wide port handling ~ we need only handle device once for the phy that
4892 * is matched in sas device page zero
4893 */
4894 if (phy_number != sas_device_pg0.PhyNum)
4895 return;
4896
4897 /* check if this is end device */
4898 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4899 if (!(_scsih_is_end_device(device_info)))
4900 return;
4901
4902 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4903 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4904 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
4905 sas_address);
4906
4907 if (!sas_device) {
4908 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4909 return;
4910 }
4911
4912 if (unlikely(sas_device->handle != handle)) {
4913 starget = sas_device->starget;
4914 sas_target_priv_data = starget->hostdata;
4915 starget_printk(KERN_INFO, starget,
4916 "handle changed from(0x%04x) to (0x%04x)!!!\n",
4917 sas_device->handle, handle);
4918 sas_target_priv_data->handle = handle;
4919 sas_device->handle = handle;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05304920 if (sas_device_pg0.Flags &
4921 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
4922 sas_device->enclosure_level =
4923 le16_to_cpu(sas_device_pg0.EnclosureLevel);
4924 memcpy(&sas_device->connector_name[0],
4925 &sas_device_pg0.ConnectorName[0], 4);
4926 } else {
4927 sas_device->enclosure_level = 0;
4928 sas_device->connector_name[0] = '\0';
4929 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05304930 }
4931
4932 /* check if device is present */
4933 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4934 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4935 pr_err(MPT3SAS_FMT
4936 "device is not present handle(0x%04x), flags!!!\n",
4937 ioc->name, handle);
4938 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4939 return;
4940 }
4941
4942 /* check if there were any issues with discovery */
4943 if (_scsih_check_access_status(ioc, sas_address, handle,
4944 sas_device_pg0.AccessStatus)) {
4945 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4946 return;
4947 }
4948
4949 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4950 _scsih_ublock_io_device(ioc, sas_address);
4951
4952}
4953
4954/**
4955 * _scsih_add_device - creating sas device object
4956 * @ioc: per adapter object
4957 * @handle: sas device handle
4958 * @phy_num: phy number end device attached to
4959 * @is_pd: is this hidden raid component
4960 *
4961 * Creating end device object, stored in ioc->sas_device_list.
4962 *
4963 * Returns 0 for success, non-zero for failure.
4964 */
4965static int
4966_scsih_add_device(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phy_num,
4967 u8 is_pd)
4968{
4969 Mpi2ConfigReply_t mpi_reply;
4970 Mpi2SasDevicePage0_t sas_device_pg0;
4971 Mpi2SasEnclosurePage0_t enclosure_pg0;
4972 struct _sas_device *sas_device;
4973 u32 ioc_status;
4974 u64 sas_address;
4975 u32 device_info;
4976 unsigned long flags;
4977
4978 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4979 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4980 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4981 ioc->name, __FILE__, __LINE__, __func__);
4982 return -1;
4983 }
4984
4985 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4986 MPI2_IOCSTATUS_MASK;
4987 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4988 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
4989 ioc->name, __FILE__, __LINE__, __func__);
4990 return -1;
4991 }
4992
4993 /* check if this is end device */
4994 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4995 if (!(_scsih_is_end_device(device_info)))
4996 return -1;
4997 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4998
4999 /* check if device is present */
5000 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5001 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5002 pr_err(MPT3SAS_FMT "device is not present handle(0x04%x)!!!\n",
5003 ioc->name, handle);
5004 return -1;
5005 }
5006
5007 /* check if there were any issues with discovery */
5008 if (_scsih_check_access_status(ioc, sas_address, handle,
5009 sas_device_pg0.AccessStatus))
5010 return -1;
5011
5012 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5013 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
5014 sas_address);
5015 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5016
5017 if (sas_device)
5018 return -1;
5019
5020 sas_device = kzalloc(sizeof(struct _sas_device),
5021 GFP_KERNEL);
5022 if (!sas_device) {
5023 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5024 ioc->name, __FILE__, __LINE__, __func__);
5025 return 0;
5026 }
5027
5028 sas_device->handle = handle;
5029 if (_scsih_get_sas_address(ioc,
5030 le16_to_cpu(sas_device_pg0.ParentDevHandle),
5031 &sas_device->sas_address_parent) != 0)
5032 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
5033 ioc->name, __FILE__, __LINE__, __func__);
5034 sas_device->enclosure_handle =
5035 le16_to_cpu(sas_device_pg0.EnclosureHandle);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305036 if (sas_device->enclosure_handle != 0)
5037 sas_device->slot =
5038 le16_to_cpu(sas_device_pg0.Slot);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305039 sas_device->device_info = device_info;
5040 sas_device->sas_address = sas_address;
5041 sas_device->phy = sas_device_pg0.PhyNum;
5042 sas_device->fast_path = (le16_to_cpu(sas_device_pg0.Flags) &
5043 MPI25_SAS_DEVICE0_FLAGS_FAST_PATH_CAPABLE) ? 1 : 0;
5044
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305045 if (sas_device_pg0.Flags & MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
5046 sas_device->enclosure_level =
5047 le16_to_cpu(sas_device_pg0.EnclosureLevel);
5048 memcpy(&sas_device->connector_name[0],
5049 &sas_device_pg0.ConnectorName[0], 4);
5050 } else {
5051 sas_device->enclosure_level = 0;
5052 sas_device->connector_name[0] = '\0';
5053 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305054 /* get enclosure_logical_id */
5055 if (sas_device->enclosure_handle && !(mpt3sas_config_get_enclosure_pg0(
5056 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5057 sas_device->enclosure_handle)))
5058 sas_device->enclosure_logical_id =
5059 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5060
5061 /* get device name */
5062 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5063
5064 if (ioc->wait_for_discovery_to_complete)
5065 _scsih_sas_device_init_add(ioc, sas_device);
5066 else
5067 _scsih_sas_device_add(ioc, sas_device);
5068
5069 return 0;
5070}
5071
5072/**
5073 * _scsih_remove_device - removing sas device object
5074 * @ioc: per adapter object
5075 * @sas_device_delete: the sas_device object
5076 *
5077 * Return nothing.
5078 */
5079static void
5080_scsih_remove_device(struct MPT3SAS_ADAPTER *ioc,
5081 struct _sas_device *sas_device)
5082{
5083 struct MPT3SAS_TARGET *sas_target_priv_data;
5084
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05305085 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5086 (sas_device->pfa_led_on)) {
5087 _scsih_turn_off_pfa_led(ioc, sas_device);
5088 sas_device->pfa_led_on = 0;
5089 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305090 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5091 "%s: enter: handle(0x%04x), sas_addr(0x%016llx)\n",
5092 ioc->name, __func__,
5093 sas_device->handle, (unsigned long long)
5094 sas_device->sas_address));
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305095 if (sas_device->enclosure_handle != 0)
5096 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5097 "%s: enter: enclosure logical id(0x%016llx), slot(%d)\n",
5098 ioc->name, __func__,
5099 (unsigned long long)sas_device->enclosure_logical_id,
5100 sas_device->slot));
5101 if (sas_device->connector_name[0] != '\0')
5102 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5103 "%s: enter: enclosure level(0x%04x), connector name( %s)\n",
5104 ioc->name, __func__,
5105 sas_device->enclosure_level,
5106 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305107
5108 if (sas_device->starget && sas_device->starget->hostdata) {
5109 sas_target_priv_data = sas_device->starget->hostdata;
5110 sas_target_priv_data->deleted = 1;
5111 _scsih_ublock_io_device(ioc, sas_device->sas_address);
5112 sas_target_priv_data->handle =
5113 MPT3SAS_INVALID_DEVICE_HANDLE;
5114 }
5115 mpt3sas_transport_port_remove(ioc,
5116 sas_device->sas_address,
5117 sas_device->sas_address_parent);
5118
5119 pr_info(MPT3SAS_FMT
5120 "removing handle(0x%04x), sas_addr(0x%016llx)\n",
5121 ioc->name, sas_device->handle,
5122 (unsigned long long) sas_device->sas_address);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305123 if (sas_device->enclosure_handle != 0)
5124 pr_info(MPT3SAS_FMT
5125 "removing : enclosure logical id(0x%016llx), slot(%d)\n",
5126 ioc->name,
5127 (unsigned long long)sas_device->enclosure_logical_id,
5128 sas_device->slot);
5129 if (sas_device->connector_name[0] != '\0')
5130 pr_info(MPT3SAS_FMT
5131 "removing enclosure level(0x%04x), connector name( %s)\n",
5132 ioc->name, sas_device->enclosure_level,
5133 sas_device->connector_name);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305134
5135 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5136 "%s: exit: handle(0x%04x), sas_addr(0x%016llx)\n",
5137 ioc->name, __func__,
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05305138 sas_device->handle, (unsigned long long)
5139 sas_device->sas_address));
5140 if (sas_device->enclosure_handle != 0)
5141 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5142 "%s: exit: enclosure logical id(0x%016llx), slot(%d)\n",
5143 ioc->name, __func__,
5144 (unsigned long long)sas_device->enclosure_logical_id,
5145 sas_device->slot));
5146 if (sas_device->connector_name[0] != '\0')
5147 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5148 "%s: exit: enclosure level(0x%04x), connector name(%s)\n",
5149 ioc->name, __func__, sas_device->enclosure_level,
5150 sas_device->connector_name));
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305151
5152 kfree(sas_device);
5153}
5154
5155#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5156/**
5157 * _scsih_sas_topology_change_event_debug - debug for topology event
5158 * @ioc: per adapter object
5159 * @event_data: event data payload
5160 * Context: user.
5161 */
5162static void
5163_scsih_sas_topology_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5164 Mpi2EventDataSasTopologyChangeList_t *event_data)
5165{
5166 int i;
5167 u16 handle;
5168 u16 reason_code;
5169 u8 phy_number;
5170 char *status_str = NULL;
5171 u8 link_rate, prev_link_rate;
5172
5173 switch (event_data->ExpStatus) {
5174 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5175 status_str = "add";
5176 break;
5177 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5178 status_str = "remove";
5179 break;
5180 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
5181 case 0:
5182 status_str = "responding";
5183 break;
5184 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5185 status_str = "remove delay";
5186 break;
5187 default:
5188 status_str = "unknown status";
5189 break;
5190 }
5191 pr_info(MPT3SAS_FMT "sas topology change: (%s)\n",
5192 ioc->name, status_str);
5193 pr_info("\thandle(0x%04x), enclosure_handle(0x%04x) " \
5194 "start_phy(%02d), count(%d)\n",
5195 le16_to_cpu(event_data->ExpanderDevHandle),
5196 le16_to_cpu(event_data->EnclosureHandle),
5197 event_data->StartPhyNum, event_data->NumEntries);
5198 for (i = 0; i < event_data->NumEntries; i++) {
5199 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5200 if (!handle)
5201 continue;
5202 phy_number = event_data->StartPhyNum + i;
5203 reason_code = event_data->PHY[i].PhyStatus &
5204 MPI2_EVENT_SAS_TOPO_RC_MASK;
5205 switch (reason_code) {
5206 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5207 status_str = "target add";
5208 break;
5209 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5210 status_str = "target remove";
5211 break;
5212 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
5213 status_str = "delay target remove";
5214 break;
5215 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5216 status_str = "link rate change";
5217 break;
5218 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
5219 status_str = "target responding";
5220 break;
5221 default:
5222 status_str = "unknown";
5223 break;
5224 }
5225 link_rate = event_data->PHY[i].LinkRate >> 4;
5226 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5227 pr_info("\tphy(%02d), attached_handle(0x%04x): %s:" \
5228 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5229 handle, status_str, link_rate, prev_link_rate);
5230
5231 }
5232}
5233#endif
5234
5235/**
5236 * _scsih_sas_topology_change_event - handle topology changes
5237 * @ioc: per adapter object
5238 * @fw_event: The fw_event_work object
5239 * Context: user.
5240 *
5241 */
5242static int
5243_scsih_sas_topology_change_event(struct MPT3SAS_ADAPTER *ioc,
5244 struct fw_event_work *fw_event)
5245{
5246 int i;
5247 u16 parent_handle, handle;
5248 u16 reason_code;
5249 u8 phy_number, max_phys;
5250 struct _sas_node *sas_expander;
5251 u64 sas_address;
5252 unsigned long flags;
5253 u8 link_rate, prev_link_rate;
Joe Lawrence35b62362014-06-25 17:05:34 -04005254 Mpi2EventDataSasTopologyChangeList_t *event_data =
5255 (Mpi2EventDataSasTopologyChangeList_t *)
5256 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305257
5258#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5259 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5260 _scsih_sas_topology_change_event_debug(ioc, event_data);
5261#endif
5262
5263 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
5264 return 0;
5265
5266 if (!ioc->sas_hba.num_phys)
5267 _scsih_sas_host_add(ioc);
5268 else
5269 _scsih_sas_host_refresh(ioc);
5270
5271 if (fw_event->ignore) {
5272 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5273 "ignoring expander event\n", ioc->name));
5274 return 0;
5275 }
5276
5277 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5278
5279 /* handle expander add */
5280 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5281 if (_scsih_expander_add(ioc, parent_handle) != 0)
5282 return 0;
5283
5284 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5285 sas_expander = mpt3sas_scsih_expander_find_by_handle(ioc,
5286 parent_handle);
5287 if (sas_expander) {
5288 sas_address = sas_expander->sas_address;
5289 max_phys = sas_expander->num_phys;
5290 } else if (parent_handle < ioc->sas_hba.num_phys) {
5291 sas_address = ioc->sas_hba.sas_address;
5292 max_phys = ioc->sas_hba.num_phys;
5293 } else {
5294 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5295 return 0;
5296 }
5297 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5298
5299 /* handle siblings events */
5300 for (i = 0; i < event_data->NumEntries; i++) {
5301 if (fw_event->ignore) {
5302 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5303 "ignoring expander event\n", ioc->name));
5304 return 0;
5305 }
5306 if (ioc->remove_host || ioc->pci_error_recovery)
5307 return 0;
5308 phy_number = event_data->StartPhyNum + i;
5309 if (phy_number >= max_phys)
5310 continue;
5311 reason_code = event_data->PHY[i].PhyStatus &
5312 MPI2_EVENT_SAS_TOPO_RC_MASK;
5313 if ((event_data->PHY[i].PhyStatus &
5314 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5315 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
5316 continue;
5317 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5318 if (!handle)
5319 continue;
5320 link_rate = event_data->PHY[i].LinkRate >> 4;
5321 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
5322 switch (reason_code) {
5323 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
5324
5325 if (ioc->shost_recovery)
5326 break;
5327
5328 if (link_rate == prev_link_rate)
5329 break;
5330
5331 mpt3sas_transport_update_links(ioc, sas_address,
5332 handle, phy_number, link_rate);
5333
5334 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5335 break;
5336
5337 _scsih_check_device(ioc, sas_address, handle,
5338 phy_number, link_rate);
5339
5340
5341 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
5342
5343 if (ioc->shost_recovery)
5344 break;
5345
5346 mpt3sas_transport_update_links(ioc, sas_address,
5347 handle, phy_number, link_rate);
5348
5349 _scsih_add_device(ioc, handle, phy_number, 0);
5350
5351 break;
5352 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
5353
5354 _scsih_device_remove_by_handle(ioc, handle);
5355 break;
5356 }
5357 }
5358
5359 /* handle expander removal */
5360 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5361 sas_expander)
5362 mpt3sas_expander_remove(ioc, sas_address);
5363
5364 return 0;
5365}
5366
5367#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5368/**
5369 * _scsih_sas_device_status_change_event_debug - debug for device event
5370 * @event_data: event data payload
5371 * Context: user.
5372 *
5373 * Return nothing.
5374 */
5375static void
5376_scsih_sas_device_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5377 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5378{
5379 char *reason_str = NULL;
5380
5381 switch (event_data->ReasonCode) {
5382 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5383 reason_str = "smart data";
5384 break;
5385 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5386 reason_str = "unsupported device discovered";
5387 break;
5388 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5389 reason_str = "internal device reset";
5390 break;
5391 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5392 reason_str = "internal task abort";
5393 break;
5394 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5395 reason_str = "internal task abort set";
5396 break;
5397 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5398 reason_str = "internal clear task set";
5399 break;
5400 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5401 reason_str = "internal query task";
5402 break;
5403 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5404 reason_str = "sata init failure";
5405 break;
5406 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5407 reason_str = "internal device reset complete";
5408 break;
5409 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5410 reason_str = "internal task abort complete";
5411 break;
5412 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5413 reason_str = "internal async notification";
5414 break;
5415 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5416 reason_str = "expander reduced functionality";
5417 break;
5418 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5419 reason_str = "expander reduced functionality complete";
5420 break;
5421 default:
5422 reason_str = "unknown reason";
5423 break;
5424 }
5425 pr_info(MPT3SAS_FMT "device status change: (%s)\n"
5426 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5427 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5428 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5429 le16_to_cpu(event_data->TaskTag));
5430 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
5431 pr_info(MPT3SAS_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
5432 event_data->ASC, event_data->ASCQ);
5433 pr_info("\n");
5434}
5435#endif
5436
5437/**
5438 * _scsih_sas_device_status_change_event - handle device status change
5439 * @ioc: per adapter object
5440 * @fw_event: The fw_event_work object
5441 * Context: user.
5442 *
5443 * Return nothing.
5444 */
5445static void
5446_scsih_sas_device_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5447 struct fw_event_work *fw_event)
5448{
5449 struct MPT3SAS_TARGET *target_priv_data;
5450 struct _sas_device *sas_device;
5451 u64 sas_address;
5452 unsigned long flags;
5453 Mpi2EventDataSasDeviceStatusChange_t *event_data =
Joe Lawrence35b62362014-06-25 17:05:34 -04005454 (Mpi2EventDataSasDeviceStatusChange_t *)
5455 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305456
5457#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5458 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5459 _scsih_sas_device_status_change_event_debug(ioc,
5460 event_data);
5461#endif
5462
5463 /* In MPI Revision K (0xC), the internal device reset complete was
5464 * implemented, so avoid setting tm_busy flag for older firmware.
5465 */
5466 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5467 return;
5468
5469 if (event_data->ReasonCode !=
5470 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
5471 event_data->ReasonCode !=
5472 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
5473 return;
5474
5475 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5476 sas_address = le64_to_cpu(event_data->SASAddress);
5477 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
5478 sas_address);
5479
5480 if (!sas_device || !sas_device->starget) {
5481 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5482 return;
5483 }
5484
5485 target_priv_data = sas_device->starget->hostdata;
5486 if (!target_priv_data) {
5487 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5488 return;
5489 }
5490
5491 if (event_data->ReasonCode ==
5492 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5493 target_priv_data->tm_busy = 1;
5494 else
5495 target_priv_data->tm_busy = 0;
5496 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5497}
5498
5499#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5500/**
5501 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure
5502 * event
5503 * @ioc: per adapter object
5504 * @event_data: event data payload
5505 * Context: user.
5506 *
5507 * Return nothing.
5508 */
5509static void
5510_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
5511 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5512{
5513 char *reason_str = NULL;
5514
5515 switch (event_data->ReasonCode) {
5516 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5517 reason_str = "enclosure add";
5518 break;
5519 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5520 reason_str = "enclosure remove";
5521 break;
5522 default:
5523 reason_str = "unknown reason";
5524 break;
5525 }
5526
5527 pr_info(MPT3SAS_FMT "enclosure status change: (%s)\n"
5528 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5529 " number slots(%d)\n", ioc->name, reason_str,
5530 le16_to_cpu(event_data->EnclosureHandle),
5531 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5532 le16_to_cpu(event_data->StartSlot));
5533}
5534#endif
5535
5536/**
5537 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5538 * @ioc: per adapter object
5539 * @fw_event: The fw_event_work object
5540 * Context: user.
5541 *
5542 * Return nothing.
5543 */
5544static void
5545_scsih_sas_enclosure_dev_status_change_event(struct MPT3SAS_ADAPTER *ioc,
5546 struct fw_event_work *fw_event)
5547{
5548#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5549 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5550 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Joe Lawrence35b62362014-06-25 17:05:34 -04005551 (Mpi2EventDataSasEnclDevStatusChange_t *)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305552 fw_event->event_data);
5553#endif
5554}
5555
5556/**
5557 * _scsih_sas_broadcast_primitive_event - handle broadcast events
5558 * @ioc: per adapter object
5559 * @fw_event: The fw_event_work object
5560 * Context: user.
5561 *
5562 * Return nothing.
5563 */
5564static void
5565_scsih_sas_broadcast_primitive_event(struct MPT3SAS_ADAPTER *ioc,
5566 struct fw_event_work *fw_event)
5567{
5568 struct scsi_cmnd *scmd;
5569 struct scsi_device *sdev;
5570 u16 smid, handle;
5571 u32 lun;
5572 struct MPT3SAS_DEVICE *sas_device_priv_data;
5573 u32 termination_count;
5574 u32 query_count;
5575 Mpi2SCSITaskManagementReply_t *mpi_reply;
Joe Lawrence35b62362014-06-25 17:05:34 -04005576 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5577 (Mpi2EventDataSasBroadcastPrimitive_t *)
5578 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305579 u16 ioc_status;
5580 unsigned long flags;
5581 int r;
5582 u8 max_retries = 0;
5583 u8 task_abort_retries;
5584
5585 mutex_lock(&ioc->tm_cmds.mutex);
5586 pr_info(MPT3SAS_FMT
5587 "%s: enter: phy number(%d), width(%d)\n",
5588 ioc->name, __func__, event_data->PhyNum,
5589 event_data->PortWidth);
5590
5591 _scsih_block_io_all_device(ioc);
5592
5593 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5594 mpi_reply = ioc->tm_cmds.reply;
5595 broadcast_aen_retry:
5596
5597 /* sanity checks for retrying this loop */
5598 if (max_retries++ == 5) {
5599 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: giving up\n",
5600 ioc->name, __func__));
5601 goto out;
5602 } else if (max_retries > 1)
5603 dewtprintk(ioc, pr_info(MPT3SAS_FMT "%s: %d retry\n",
5604 ioc->name, __func__, max_retries - 1));
5605
5606 termination_count = 0;
5607 query_count = 0;
5608 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
5609 if (ioc->shost_recovery)
5610 goto out;
5611 scmd = _scsih_scsi_lookup_get(ioc, smid);
5612 if (!scmd)
5613 continue;
5614 sdev = scmd->device;
5615 sas_device_priv_data = sdev->hostdata;
5616 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5617 continue;
5618 /* skip hidden raid components */
5619 if (sas_device_priv_data->sas_target->flags &
5620 MPT_TARGET_FLAGS_RAID_COMPONENT)
5621 continue;
5622 /* skip volumes */
5623 if (sas_device_priv_data->sas_target->flags &
5624 MPT_TARGET_FLAGS_VOLUME)
5625 continue;
5626
5627 handle = sas_device_priv_data->sas_target->handle;
5628 lun = sas_device_priv_data->lun;
5629 query_count++;
5630
5631 if (ioc->shost_recovery)
5632 goto out;
5633
5634 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5635 r = mpt3sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04005636 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305637 TM_MUTEX_OFF);
5638 if (r == FAILED) {
5639 sdev_printk(KERN_WARNING, sdev,
5640 "mpt3sas_scsih_issue_tm: FAILED when sending "
5641 "QUERY_TASK: scmd(%p)\n", scmd);
5642 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5643 goto broadcast_aen_retry;
5644 }
5645 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5646 & MPI2_IOCSTATUS_MASK;
5647 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5648 sdev_printk(KERN_WARNING, sdev,
5649 "query task: FAILED with IOCSTATUS(0x%04x), scmd(%p)\n",
5650 ioc_status, scmd);
5651 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5652 goto broadcast_aen_retry;
5653 }
5654
5655 /* see if IO is still owned by IOC and target */
5656 if (mpi_reply->ResponseCode ==
5657 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5658 mpi_reply->ResponseCode ==
5659 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
5660 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5661 continue;
5662 }
5663 task_abort_retries = 0;
5664 tm_retry:
5665 if (task_abort_retries++ == 60) {
5666 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5667 "%s: ABORT_TASK: giving up\n", ioc->name,
5668 __func__));
5669 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5670 goto broadcast_aen_retry;
5671 }
5672
5673 if (ioc->shost_recovery)
5674 goto out_no_lock;
5675
5676 r = mpt3sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5677 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Matthew Wilcoxc62e46d2014-03-27 16:40:30 -04005678 TM_MUTEX_OFF);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305679 if (r == FAILED) {
5680 sdev_printk(KERN_WARNING, sdev,
5681 "mpt3sas_scsih_issue_tm: ABORT_TASK: FAILED : "
5682 "scmd(%p)\n", scmd);
5683 goto tm_retry;
5684 }
5685
5686 if (task_abort_retries > 1)
5687 sdev_printk(KERN_WARNING, sdev,
5688 "mpt3sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5689 " scmd(%p)\n",
5690 task_abort_retries - 1, scmd);
5691
5692 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
5693 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5694 }
5695
5696 if (ioc->broadcast_aen_pending) {
5697 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5698 "%s: loop back due to pending AEN\n",
5699 ioc->name, __func__));
5700 ioc->broadcast_aen_pending = 0;
5701 goto broadcast_aen_retry;
5702 }
5703
5704 out:
5705 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5706 out_no_lock:
5707
5708 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5709 "%s - exit, query_count = %d termination_count = %d\n",
5710 ioc->name, __func__, query_count, termination_count));
5711
5712 ioc->broadcast_aen_busy = 0;
5713 if (!ioc->shost_recovery)
5714 _scsih_ublock_io_all_device(ioc);
5715 mutex_unlock(&ioc->tm_cmds.mutex);
5716}
5717
5718/**
5719 * _scsih_sas_discovery_event - handle discovery events
5720 * @ioc: per adapter object
5721 * @fw_event: The fw_event_work object
5722 * Context: user.
5723 *
5724 * Return nothing.
5725 */
5726static void
5727_scsih_sas_discovery_event(struct MPT3SAS_ADAPTER *ioc,
5728 struct fw_event_work *fw_event)
5729{
Joe Lawrence35b62362014-06-25 17:05:34 -04005730 Mpi2EventDataSasDiscovery_t *event_data =
5731 (Mpi2EventDataSasDiscovery_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05305732
5733#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
5734 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
5735 pr_info(MPT3SAS_FMT "discovery event: (%s)", ioc->name,
5736 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5737 "start" : "stop");
5738 if (event_data->DiscoveryStatus)
5739 pr_info("discovery_status(0x%08x)",
5740 le32_to_cpu(event_data->DiscoveryStatus));
5741 pr_info("\n");
5742 }
5743#endif
5744
5745 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5746 !ioc->sas_hba.num_phys) {
5747 if (disable_discovery > 0 && ioc->shost_recovery) {
5748 /* Wait for the reset to complete */
5749 while (ioc->shost_recovery)
5750 ssleep(1);
5751 }
5752 _scsih_sas_host_add(ioc);
5753 }
5754}
5755
5756/**
5757 * _scsih_ir_fastpath - turn on fastpath for IR physdisk
5758 * @ioc: per adapter object
5759 * @handle: device handle for physical disk
5760 * @phys_disk_num: physical disk number
5761 *
5762 * Return 0 for success, else failure.
5763 */
5764static int
5765_scsih_ir_fastpath(struct MPT3SAS_ADAPTER *ioc, u16 handle, u8 phys_disk_num)
5766{
5767 Mpi2RaidActionRequest_t *mpi_request;
5768 Mpi2RaidActionReply_t *mpi_reply;
5769 u16 smid;
5770 u8 issue_reset = 0;
5771 int rc = 0;
5772 u16 ioc_status;
5773 u32 log_info;
5774
5775
5776 mutex_lock(&ioc->scsih_cmds.mutex);
5777
5778 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
5779 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
5780 ioc->name, __func__);
5781 rc = -EAGAIN;
5782 goto out;
5783 }
5784 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
5785
5786 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
5787 if (!smid) {
5788 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
5789 ioc->name, __func__);
5790 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5791 rc = -EAGAIN;
5792 goto out;
5793 }
5794
5795 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
5796 ioc->scsih_cmds.smid = smid;
5797 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
5798
5799 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
5800 mpi_request->Action = MPI2_RAID_ACTION_PHYSDISK_HIDDEN;
5801 mpi_request->PhysDiskNum = phys_disk_num;
5802
5803 dewtprintk(ioc, pr_info(MPT3SAS_FMT "IR RAID_ACTION: turning fast "\
5804 "path on for handle(0x%04x), phys_disk_num (0x%02x)\n", ioc->name,
5805 handle, phys_disk_num));
5806
5807 init_completion(&ioc->scsih_cmds.done);
5808 mpt3sas_base_put_smid_default(ioc, smid);
5809 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
5810
5811 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
5812 pr_err(MPT3SAS_FMT "%s: timeout\n",
5813 ioc->name, __func__);
5814 if (!(ioc->scsih_cmds.status & MPT3_CMD_RESET))
5815 issue_reset = 1;
5816 rc = -EFAULT;
5817 goto out;
5818 }
5819
5820 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
5821
5822 mpi_reply = ioc->scsih_cmds.reply;
5823 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
5824 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
5825 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
5826 else
5827 log_info = 0;
5828 ioc_status &= MPI2_IOCSTATUS_MASK;
5829 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5830 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5831 "IR RAID_ACTION: failed: ioc_status(0x%04x), "
5832 "loginfo(0x%08x)!!!\n", ioc->name, ioc_status,
5833 log_info));
5834 rc = -EFAULT;
5835 } else
5836 dewtprintk(ioc, pr_info(MPT3SAS_FMT
5837 "IR RAID_ACTION: completed successfully\n",
5838 ioc->name));
5839 }
5840
5841 out:
5842 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
5843 mutex_unlock(&ioc->scsih_cmds.mutex);
5844
5845 if (issue_reset)
5846 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
5847 FORCE_BIG_HAMMER);
5848 return rc;
5849}
5850
5851/**
5852 * _scsih_reprobe_lun - reprobing lun
5853 * @sdev: scsi device struct
5854 * @no_uld_attach: sdev->no_uld_attach flag setting
5855 *
5856 **/
5857static void
5858_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5859{
5860 int rc;
5861 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5862 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5863 sdev->no_uld_attach ? "hidding" : "exposing");
5864 rc = scsi_device_reprobe(sdev);
5865}
5866
5867/**
5868 * _scsih_sas_volume_add - add new volume
5869 * @ioc: per adapter object
5870 * @element: IR config element data
5871 * Context: user.
5872 *
5873 * Return nothing.
5874 */
5875static void
5876_scsih_sas_volume_add(struct MPT3SAS_ADAPTER *ioc,
5877 Mpi2EventIrConfigElement_t *element)
5878{
5879 struct _raid_device *raid_device;
5880 unsigned long flags;
5881 u64 wwid;
5882 u16 handle = le16_to_cpu(element->VolDevHandle);
5883 int rc;
5884
5885 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
5886 if (!wwid) {
5887 pr_err(MPT3SAS_FMT
5888 "failure at %s:%d/%s()!\n", ioc->name,
5889 __FILE__, __LINE__, __func__);
5890 return;
5891 }
5892
5893 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5894 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5895 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5896
5897 if (raid_device)
5898 return;
5899
5900 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5901 if (!raid_device) {
5902 pr_err(MPT3SAS_FMT
5903 "failure at %s:%d/%s()!\n", ioc->name,
5904 __FILE__, __LINE__, __func__);
5905 return;
5906 }
5907
5908 raid_device->id = ioc->sas_id++;
5909 raid_device->channel = RAID_CHANNEL;
5910 raid_device->handle = handle;
5911 raid_device->wwid = wwid;
5912 _scsih_raid_device_add(ioc, raid_device);
5913 if (!ioc->wait_for_discovery_to_complete) {
5914 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5915 raid_device->id, 0);
5916 if (rc)
5917 _scsih_raid_device_remove(ioc, raid_device);
5918 } else {
5919 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5920 _scsih_determine_boot_device(ioc, raid_device, 1);
5921 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5922 }
5923}
5924
5925/**
5926 * _scsih_sas_volume_delete - delete volume
5927 * @ioc: per adapter object
5928 * @handle: volume device handle
5929 * Context: user.
5930 *
5931 * Return nothing.
5932 */
5933static void
5934_scsih_sas_volume_delete(struct MPT3SAS_ADAPTER *ioc, u16 handle)
5935{
5936 struct _raid_device *raid_device;
5937 unsigned long flags;
5938 struct MPT3SAS_TARGET *sas_target_priv_data;
5939 struct scsi_target *starget = NULL;
5940
5941 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5942 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5943 if (raid_device) {
5944 if (raid_device->starget) {
5945 starget = raid_device->starget;
5946 sas_target_priv_data = starget->hostdata;
5947 sas_target_priv_data->deleted = 1;
5948 }
5949 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
5950 ioc->name, raid_device->handle,
5951 (unsigned long long) raid_device->wwid);
5952 list_del(&raid_device->list);
5953 kfree(raid_device);
5954 }
5955 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5956 if (starget)
5957 scsi_remove_target(&starget->dev);
5958}
5959
5960/**
5961 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5962 * @ioc: per adapter object
5963 * @element: IR config element data
5964 * Context: user.
5965 *
5966 * Return nothing.
5967 */
5968static void
5969_scsih_sas_pd_expose(struct MPT3SAS_ADAPTER *ioc,
5970 Mpi2EventIrConfigElement_t *element)
5971{
5972 struct _sas_device *sas_device;
5973 struct scsi_target *starget = NULL;
5974 struct MPT3SAS_TARGET *sas_target_priv_data;
5975 unsigned long flags;
5976 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5977
5978 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5979 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5980 if (sas_device) {
5981 sas_device->volume_handle = 0;
5982 sas_device->volume_wwid = 0;
5983 clear_bit(handle, ioc->pd_handles);
5984 if (sas_device->starget && sas_device->starget->hostdata) {
5985 starget = sas_device->starget;
5986 sas_target_priv_data = starget->hostdata;
5987 sas_target_priv_data->flags &=
5988 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5989 }
5990 }
5991 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5992 if (!sas_device)
5993 return;
5994
5995 /* exposing raid component */
5996 if (starget)
5997 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
5998}
5999
6000/**
6001 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6002 * @ioc: per adapter object
6003 * @element: IR config element data
6004 * Context: user.
6005 *
6006 * Return nothing.
6007 */
6008static void
6009_scsih_sas_pd_hide(struct MPT3SAS_ADAPTER *ioc,
6010 Mpi2EventIrConfigElement_t *element)
6011{
6012 struct _sas_device *sas_device;
6013 struct scsi_target *starget = NULL;
6014 struct MPT3SAS_TARGET *sas_target_priv_data;
6015 unsigned long flags;
6016 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6017 u16 volume_handle = 0;
6018 u64 volume_wwid = 0;
6019
6020 mpt3sas_config_get_volume_handle(ioc, handle, &volume_handle);
6021 if (volume_handle)
6022 mpt3sas_config_get_volume_wwid(ioc, volume_handle,
6023 &volume_wwid);
6024
6025 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6026 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6027 if (sas_device) {
6028 set_bit(handle, ioc->pd_handles);
6029 if (sas_device->starget && sas_device->starget->hostdata) {
6030 starget = sas_device->starget;
6031 sas_target_priv_data = starget->hostdata;
6032 sas_target_priv_data->flags |=
6033 MPT_TARGET_FLAGS_RAID_COMPONENT;
6034 sas_device->volume_handle = volume_handle;
6035 sas_device->volume_wwid = volume_wwid;
6036 }
6037 }
6038 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6039 if (!sas_device)
6040 return;
6041
6042 /* hiding raid component */
6043 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6044 if (starget)
6045 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
6046}
6047
6048/**
6049 * _scsih_sas_pd_delete - delete pd component
6050 * @ioc: per adapter object
6051 * @element: IR config element data
6052 * Context: user.
6053 *
6054 * Return nothing.
6055 */
6056static void
6057_scsih_sas_pd_delete(struct MPT3SAS_ADAPTER *ioc,
6058 Mpi2EventIrConfigElement_t *element)
6059{
6060 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6061
6062 _scsih_device_remove_by_handle(ioc, handle);
6063}
6064
6065/**
6066 * _scsih_sas_pd_add - remove pd component
6067 * @ioc: per adapter object
6068 * @element: IR config element data
6069 * Context: user.
6070 *
6071 * Return nothing.
6072 */
6073static void
6074_scsih_sas_pd_add(struct MPT3SAS_ADAPTER *ioc,
6075 Mpi2EventIrConfigElement_t *element)
6076{
6077 struct _sas_device *sas_device;
6078 unsigned long flags;
6079 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6080 Mpi2ConfigReply_t mpi_reply;
6081 Mpi2SasDevicePage0_t sas_device_pg0;
6082 u32 ioc_status;
6083 u64 sas_address;
6084 u16 parent_handle;
6085
6086 set_bit(handle, ioc->pd_handles);
6087
6088 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6089 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6090 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6091 if (sas_device) {
6092 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6093 return;
6094 }
6095
6096 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6097 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6098 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6099 ioc->name, __FILE__, __LINE__, __func__);
6100 return;
6101 }
6102
6103 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6104 MPI2_IOCSTATUS_MASK;
6105 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6106 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6107 ioc->name, __FILE__, __LINE__, __func__);
6108 return;
6109 }
6110
6111 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6112 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6113 mpt3sas_transport_update_links(ioc, sas_address, handle,
6114 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6115
6116 _scsih_ir_fastpath(ioc, handle, element->PhysDiskNum);
6117 _scsih_add_device(ioc, handle, 0, 1);
6118}
6119
6120#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6121/**
6122 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6123 * @ioc: per adapter object
6124 * @event_data: event data payload
6125 * Context: user.
6126 *
6127 * Return nothing.
6128 */
6129static void
6130_scsih_sas_ir_config_change_event_debug(struct MPT3SAS_ADAPTER *ioc,
6131 Mpi2EventDataIrConfigChangeList_t *event_data)
6132{
6133 Mpi2EventIrConfigElement_t *element;
6134 u8 element_type;
6135 int i;
6136 char *reason_str = NULL, *element_str = NULL;
6137
6138 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6139
6140 pr_info(MPT3SAS_FMT "raid config change: (%s), elements(%d)\n",
6141 ioc->name, (le32_to_cpu(event_data->Flags) &
6142 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6143 "foreign" : "native", event_data->NumElements);
6144 for (i = 0; i < event_data->NumElements; i++, element++) {
6145 switch (element->ReasonCode) {
6146 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6147 reason_str = "add";
6148 break;
6149 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6150 reason_str = "remove";
6151 break;
6152 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6153 reason_str = "no change";
6154 break;
6155 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6156 reason_str = "hide";
6157 break;
6158 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6159 reason_str = "unhide";
6160 break;
6161 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6162 reason_str = "volume_created";
6163 break;
6164 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6165 reason_str = "volume_deleted";
6166 break;
6167 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6168 reason_str = "pd_created";
6169 break;
6170 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6171 reason_str = "pd_deleted";
6172 break;
6173 default:
6174 reason_str = "unknown reason";
6175 break;
6176 }
6177 element_type = le16_to_cpu(element->ElementFlags) &
6178 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6179 switch (element_type) {
6180 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6181 element_str = "volume";
6182 break;
6183 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6184 element_str = "phys disk";
6185 break;
6186 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6187 element_str = "hot spare";
6188 break;
6189 default:
6190 element_str = "unknown element";
6191 break;
6192 }
6193 pr_info("\t(%s:%s), vol handle(0x%04x), " \
6194 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6195 reason_str, le16_to_cpu(element->VolDevHandle),
6196 le16_to_cpu(element->PhysDiskDevHandle),
6197 element->PhysDiskNum);
6198 }
6199}
6200#endif
6201
6202/**
6203 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6204 * @ioc: per adapter object
6205 * @fw_event: The fw_event_work object
6206 * Context: user.
6207 *
6208 * Return nothing.
6209 */
6210static void
6211_scsih_sas_ir_config_change_event(struct MPT3SAS_ADAPTER *ioc,
6212 struct fw_event_work *fw_event)
6213{
6214 Mpi2EventIrConfigElement_t *element;
6215 int i;
6216 u8 foreign_config;
Joe Lawrence35b62362014-06-25 17:05:34 -04006217 Mpi2EventDataIrConfigChangeList_t *event_data =
6218 (Mpi2EventDataIrConfigChangeList_t *)
6219 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306220
6221#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6222 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6223 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6224
6225#endif
6226
6227 foreign_config = (le32_to_cpu(event_data->Flags) &
6228 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
6229
6230 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6231 if (ioc->shost_recovery) {
6232
6233 for (i = 0; i < event_data->NumElements; i++, element++) {
6234 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_HIDE)
6235 _scsih_ir_fastpath(ioc,
6236 le16_to_cpu(element->PhysDiskDevHandle),
6237 element->PhysDiskNum);
6238 }
6239 return;
6240 }
6241 for (i = 0; i < event_data->NumElements; i++, element++) {
6242
6243 switch (element->ReasonCode) {
6244 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6245 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6246 if (!foreign_config)
6247 _scsih_sas_volume_add(ioc, element);
6248 break;
6249 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6250 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6251 if (!foreign_config)
6252 _scsih_sas_volume_delete(ioc,
6253 le16_to_cpu(element->VolDevHandle));
6254 break;
6255 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6256 _scsih_sas_pd_hide(ioc, element);
6257 break;
6258 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6259 _scsih_sas_pd_expose(ioc, element);
6260 break;
6261 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6262 _scsih_sas_pd_add(ioc, element);
6263 break;
6264 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6265 _scsih_sas_pd_delete(ioc, element);
6266 break;
6267 }
6268 }
6269}
6270
6271/**
6272 * _scsih_sas_ir_volume_event - IR volume event
6273 * @ioc: per adapter object
6274 * @fw_event: The fw_event_work object
6275 * Context: user.
6276 *
6277 * Return nothing.
6278 */
6279static void
6280_scsih_sas_ir_volume_event(struct MPT3SAS_ADAPTER *ioc,
6281 struct fw_event_work *fw_event)
6282{
6283 u64 wwid;
6284 unsigned long flags;
6285 struct _raid_device *raid_device;
6286 u16 handle;
6287 u32 state;
6288 int rc;
Joe Lawrence35b62362014-06-25 17:05:34 -04006289 Mpi2EventDataIrVolume_t *event_data =
6290 (Mpi2EventDataIrVolume_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306291
6292 if (ioc->shost_recovery)
6293 return;
6294
6295 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6296 return;
6297
6298 handle = le16_to_cpu(event_data->VolDevHandle);
6299 state = le32_to_cpu(event_data->NewValue);
6300 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6301 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6302 ioc->name, __func__, handle,
6303 le32_to_cpu(event_data->PreviousValue), state));
6304 switch (state) {
6305 case MPI2_RAID_VOL_STATE_MISSING:
6306 case MPI2_RAID_VOL_STATE_FAILED:
6307 _scsih_sas_volume_delete(ioc, handle);
6308 break;
6309
6310 case MPI2_RAID_VOL_STATE_ONLINE:
6311 case MPI2_RAID_VOL_STATE_DEGRADED:
6312 case MPI2_RAID_VOL_STATE_OPTIMAL:
6313
6314 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6315 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6316 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6317
6318 if (raid_device)
6319 break;
6320
6321 mpt3sas_config_get_volume_wwid(ioc, handle, &wwid);
6322 if (!wwid) {
6323 pr_err(MPT3SAS_FMT
6324 "failure at %s:%d/%s()!\n", ioc->name,
6325 __FILE__, __LINE__, __func__);
6326 break;
6327 }
6328
6329 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6330 if (!raid_device) {
6331 pr_err(MPT3SAS_FMT
6332 "failure at %s:%d/%s()!\n", ioc->name,
6333 __FILE__, __LINE__, __func__);
6334 break;
6335 }
6336
6337 raid_device->id = ioc->sas_id++;
6338 raid_device->channel = RAID_CHANNEL;
6339 raid_device->handle = handle;
6340 raid_device->wwid = wwid;
6341 _scsih_raid_device_add(ioc, raid_device);
6342 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6343 raid_device->id, 0);
6344 if (rc)
6345 _scsih_raid_device_remove(ioc, raid_device);
6346 break;
6347
6348 case MPI2_RAID_VOL_STATE_INITIALIZING:
6349 default:
6350 break;
6351 }
6352}
6353
6354/**
6355 * _scsih_sas_ir_physical_disk_event - PD event
6356 * @ioc: per adapter object
6357 * @fw_event: The fw_event_work object
6358 * Context: user.
6359 *
6360 * Return nothing.
6361 */
6362static void
6363_scsih_sas_ir_physical_disk_event(struct MPT3SAS_ADAPTER *ioc,
6364 struct fw_event_work *fw_event)
6365{
6366 u16 handle, parent_handle;
6367 u32 state;
6368 struct _sas_device *sas_device;
6369 unsigned long flags;
6370 Mpi2ConfigReply_t mpi_reply;
6371 Mpi2SasDevicePage0_t sas_device_pg0;
6372 u32 ioc_status;
Joe Lawrence35b62362014-06-25 17:05:34 -04006373 Mpi2EventDataIrPhysicalDisk_t *event_data =
6374 (Mpi2EventDataIrPhysicalDisk_t *) fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306375 u64 sas_address;
6376
6377 if (ioc->shost_recovery)
6378 return;
6379
6380 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6381 return;
6382
6383 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6384 state = le32_to_cpu(event_data->NewValue);
6385
6386 dewtprintk(ioc, pr_info(MPT3SAS_FMT
6387 "%s: handle(0x%04x), old(0x%08x), new(0x%08x)\n",
6388 ioc->name, __func__, handle,
6389 le32_to_cpu(event_data->PreviousValue), state));
6390 switch (state) {
6391 case MPI2_RAID_PD_STATE_ONLINE:
6392 case MPI2_RAID_PD_STATE_DEGRADED:
6393 case MPI2_RAID_PD_STATE_REBUILDING:
6394 case MPI2_RAID_PD_STATE_OPTIMAL:
6395 case MPI2_RAID_PD_STATE_HOT_SPARE:
6396
6397 set_bit(handle, ioc->pd_handles);
6398 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6399 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6400 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6401
6402 if (sas_device)
6403 return;
6404
6405 if ((mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6406 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6407 handle))) {
6408 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6409 ioc->name, __FILE__, __LINE__, __func__);
6410 return;
6411 }
6412
6413 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6414 MPI2_IOCSTATUS_MASK;
6415 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6416 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6417 ioc->name, __FILE__, __LINE__, __func__);
6418 return;
6419 }
6420
6421 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6422 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6423 mpt3sas_transport_update_links(ioc, sas_address, handle,
6424 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
6425
6426 _scsih_add_device(ioc, handle, 0, 1);
6427
6428 break;
6429
6430 case MPI2_RAID_PD_STATE_OFFLINE:
6431 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6432 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
6433 default:
6434 break;
6435 }
6436}
6437
6438#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6439/**
6440 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6441 * @ioc: per adapter object
6442 * @event_data: event data payload
6443 * Context: user.
6444 *
6445 * Return nothing.
6446 */
6447static void
6448_scsih_sas_ir_operation_status_event_debug(struct MPT3SAS_ADAPTER *ioc,
6449 Mpi2EventDataIrOperationStatus_t *event_data)
6450{
6451 char *reason_str = NULL;
6452
6453 switch (event_data->RAIDOperation) {
6454 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6455 reason_str = "resync";
6456 break;
6457 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6458 reason_str = "online capacity expansion";
6459 break;
6460 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6461 reason_str = "consistency check";
6462 break;
6463 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6464 reason_str = "background init";
6465 break;
6466 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6467 reason_str = "make data consistent";
6468 break;
6469 }
6470
6471 if (!reason_str)
6472 return;
6473
6474 pr_info(MPT3SAS_FMT "raid operational status: (%s)" \
6475 "\thandle(0x%04x), percent complete(%d)\n",
6476 ioc->name, reason_str,
6477 le16_to_cpu(event_data->VolDevHandle),
6478 event_data->PercentComplete);
6479}
6480#endif
6481
6482/**
6483 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6484 * @ioc: per adapter object
6485 * @fw_event: The fw_event_work object
6486 * Context: user.
6487 *
6488 * Return nothing.
6489 */
6490static void
6491_scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
6492 struct fw_event_work *fw_event)
6493{
Joe Lawrence35b62362014-06-25 17:05:34 -04006494 Mpi2EventDataIrOperationStatus_t *event_data =
6495 (Mpi2EventDataIrOperationStatus_t *)
6496 fw_event->event_data;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306497 static struct _raid_device *raid_device;
6498 unsigned long flags;
6499 u16 handle;
6500
6501#ifdef CONFIG_SCSI_MPT3SAS_LOGGING
6502 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6503 _scsih_sas_ir_operation_status_event_debug(ioc,
6504 event_data);
6505#endif
6506
6507 /* code added for raid transport support */
6508 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6509
6510 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6511 handle = le16_to_cpu(event_data->VolDevHandle);
6512 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6513 if (raid_device)
6514 raid_device->percent_complete =
6515 event_data->PercentComplete;
6516 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6517 }
6518}
6519
6520/**
6521 * _scsih_prep_device_scan - initialize parameters prior to device scan
6522 * @ioc: per adapter object
6523 *
6524 * Set the deleted flag prior to device scan. If the device is found during
6525 * the scan, then we clear the deleted flag.
6526 */
6527static void
6528_scsih_prep_device_scan(struct MPT3SAS_ADAPTER *ioc)
6529{
6530 struct MPT3SAS_DEVICE *sas_device_priv_data;
6531 struct scsi_device *sdev;
6532
6533 shost_for_each_device(sdev, ioc->shost) {
6534 sas_device_priv_data = sdev->hostdata;
6535 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6536 sas_device_priv_data->sas_target->deleted = 1;
6537 }
6538}
6539
6540/**
6541 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6542 * @ioc: per adapter object
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306543 * @sas_device_pg0: SAS Device page 0
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306544 *
6545 * After host reset, find out whether devices are still responding.
6546 * Used in _scsih_remove_unresponsive_sas_devices.
6547 *
6548 * Return nothing.
6549 */
6550static void
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306551_scsih_mark_responding_sas_device(struct MPT3SAS_ADAPTER *ioc,
6552Mpi2SasDevicePage0_t *sas_device_pg0)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306553{
6554 struct MPT3SAS_TARGET *sas_target_priv_data = NULL;
6555 struct scsi_target *starget;
6556 struct _sas_device *sas_device;
6557 unsigned long flags;
6558
6559 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6560 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306561 if ((sas_device->sas_address == sas_device_pg0->SASAddress) &&
6562 (sas_device->slot == sas_device_pg0->Slot)) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306563 sas_device->responding = 1;
6564 starget = sas_device->starget;
6565 if (starget && starget->hostdata) {
6566 sas_target_priv_data = starget->hostdata;
6567 sas_target_priv_data->tm_busy = 0;
6568 sas_target_priv_data->deleted = 0;
6569 } else
6570 sas_target_priv_data = NULL;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306571 if (starget) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306572 starget_printk(KERN_INFO, starget,
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306573 "handle(0x%04x), sas_addr(0x%016llx)\n",
6574 sas_device_pg0->DevHandle,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306575 (unsigned long long)
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306576 sas_device->sas_address);
6577
6578 if (sas_device->enclosure_handle != 0)
6579 starget_printk(KERN_INFO, starget,
6580 "enclosure logical id(0x%016llx),"
6581 " slot(%d)\n",
6582 (unsigned long long)
6583 sas_device->enclosure_logical_id,
6584 sas_device->slot);
6585 }
6586 if (sas_device_pg0->Flags &
6587 MPI2_SAS_DEVICE0_FLAGS_ENCL_LEVEL_VALID) {
6588 sas_device->enclosure_level =
6589 le16_to_cpu(sas_device_pg0->EnclosureLevel);
6590 memcpy(&sas_device->connector_name[0],
6591 &sas_device_pg0->ConnectorName[0], 4);
6592 } else {
6593 sas_device->enclosure_level = 0;
6594 sas_device->connector_name[0] = '\0';
6595 }
6596
6597 if (sas_device->handle == sas_device_pg0->DevHandle)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306598 goto out;
6599 pr_info("\thandle changed from(0x%04x)!!!\n",
6600 sas_device->handle);
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306601 sas_device->handle = sas_device_pg0->DevHandle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306602 if (sas_target_priv_data)
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306603 sas_target_priv_data->handle =
6604 sas_device_pg0->DevHandle;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306605 goto out;
6606 }
6607 }
6608 out:
6609 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6610}
6611
6612/**
6613 * _scsih_search_responding_sas_devices -
6614 * @ioc: per adapter object
6615 *
6616 * After host reset, find out whether devices are still responding.
6617 * If not remove.
6618 *
6619 * Return nothing.
6620 */
6621static void
6622_scsih_search_responding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6623{
6624 Mpi2SasDevicePage0_t sas_device_pg0;
6625 Mpi2ConfigReply_t mpi_reply;
6626 u16 ioc_status;
6627 u16 handle;
6628 u32 device_info;
6629
6630 pr_info(MPT3SAS_FMT "search for end-devices: start\n", ioc->name);
6631
6632 if (list_empty(&ioc->sas_device_list))
6633 goto out;
6634
6635 handle = 0xFFFF;
6636 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6637 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6638 handle))) {
6639 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6640 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05306641 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306642 break;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306643 handle = sas_device_pg0.DevHandle =
6644 le16_to_cpu(sas_device_pg0.DevHandle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306645 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6646 if (!(_scsih_is_end_device(device_info)))
6647 continue;
Sreekanth Reddye6d45e32015-06-30 12:24:52 +05306648 sas_device_pg0.SASAddress =
6649 le64_to_cpu(sas_device_pg0.SASAddress);
6650 sas_device_pg0.Slot = le16_to_cpu(sas_device_pg0.Slot);
6651 _scsih_mark_responding_sas_device(ioc, &sas_device_pg0);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306652 }
6653
6654 out:
6655 pr_info(MPT3SAS_FMT "search for end-devices: complete\n",
6656 ioc->name);
6657}
6658
6659/**
6660 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6661 * @ioc: per adapter object
6662 * @wwid: world wide identifier for raid volume
6663 * @handle: device handle
6664 *
6665 * After host reset, find out whether devices are still responding.
6666 * Used in _scsih_remove_unresponsive_raid_devices.
6667 *
6668 * Return nothing.
6669 */
6670static void
6671_scsih_mark_responding_raid_device(struct MPT3SAS_ADAPTER *ioc, u64 wwid,
6672 u16 handle)
6673{
6674 struct MPT3SAS_TARGET *sas_target_priv_data;
6675 struct scsi_target *starget;
6676 struct _raid_device *raid_device;
6677 unsigned long flags;
6678
6679 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6680 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6681 if (raid_device->wwid == wwid && raid_device->starget) {
6682 starget = raid_device->starget;
6683 if (starget && starget->hostdata) {
6684 sas_target_priv_data = starget->hostdata;
6685 sas_target_priv_data->deleted = 0;
6686 } else
6687 sas_target_priv_data = NULL;
6688 raid_device->responding = 1;
6689 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6690 starget_printk(KERN_INFO, raid_device->starget,
6691 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6692 (unsigned long long)raid_device->wwid);
6693 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6694 if (raid_device->handle == handle) {
6695 spin_unlock_irqrestore(&ioc->raid_device_lock,
6696 flags);
6697 return;
6698 }
6699 pr_info("\thandle changed from(0x%04x)!!!\n",
6700 raid_device->handle);
6701 raid_device->handle = handle;
6702 if (sas_target_priv_data)
6703 sas_target_priv_data->handle = handle;
6704 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6705 return;
6706 }
6707 }
6708 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6709}
6710
6711/**
6712 * _scsih_search_responding_raid_devices -
6713 * @ioc: per adapter object
6714 *
6715 * After host reset, find out whether devices are still responding.
6716 * If not remove.
6717 *
6718 * Return nothing.
6719 */
6720static void
6721_scsih_search_responding_raid_devices(struct MPT3SAS_ADAPTER *ioc)
6722{
6723 Mpi2RaidVolPage1_t volume_pg1;
6724 Mpi2RaidVolPage0_t volume_pg0;
6725 Mpi2RaidPhysDiskPage0_t pd_pg0;
6726 Mpi2ConfigReply_t mpi_reply;
6727 u16 ioc_status;
6728 u16 handle;
6729 u8 phys_disk_num;
6730
6731 if (!ioc->ir_firmware)
6732 return;
6733
6734 pr_info(MPT3SAS_FMT "search for raid volumes: start\n",
6735 ioc->name);
6736
6737 if (list_empty(&ioc->raid_device_list))
6738 goto out;
6739
6740 handle = 0xFFFF;
6741 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6742 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6743 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6744 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05306745 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306746 break;
6747 handle = le16_to_cpu(volume_pg1.DevHandle);
6748
6749 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6750 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6751 sizeof(Mpi2RaidVolPage0_t)))
6752 continue;
6753
6754 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6755 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6756 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6757 _scsih_mark_responding_raid_device(ioc,
6758 le64_to_cpu(volume_pg1.WWID), handle);
6759 }
6760
6761 /* refresh the pd_handles */
6762 phys_disk_num = 0xFF;
6763 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6764 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6765 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6766 phys_disk_num))) {
6767 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6768 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05306769 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306770 break;
6771 phys_disk_num = pd_pg0.PhysDiskNum;
6772 handle = le16_to_cpu(pd_pg0.DevHandle);
6773 set_bit(handle, ioc->pd_handles);
6774 }
6775 out:
6776 pr_info(MPT3SAS_FMT "search for responding raid volumes: complete\n",
6777 ioc->name);
6778}
6779
6780/**
6781 * _scsih_mark_responding_expander - mark a expander as responding
6782 * @ioc: per adapter object
6783 * @sas_address: sas address
6784 * @handle:
6785 *
6786 * After host reset, find out whether devices are still responding.
6787 * Used in _scsih_remove_unresponsive_expanders.
6788 *
6789 * Return nothing.
6790 */
6791static void
6792_scsih_mark_responding_expander(struct MPT3SAS_ADAPTER *ioc, u64 sas_address,
6793 u16 handle)
6794{
6795 struct _sas_node *sas_expander;
6796 unsigned long flags;
6797 int i;
6798
6799 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6800 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
6801 if (sas_expander->sas_address != sas_address)
6802 continue;
6803 sas_expander->responding = 1;
6804 if (sas_expander->handle == handle)
6805 goto out;
6806 pr_info("\texpander(0x%016llx): handle changed" \
6807 " from(0x%04x) to (0x%04x)!!!\n",
6808 (unsigned long long)sas_expander->sas_address,
6809 sas_expander->handle, handle);
6810 sas_expander->handle = handle;
6811 for (i = 0 ; i < sas_expander->num_phys ; i++)
6812 sas_expander->phy[i].handle = handle;
6813 goto out;
6814 }
6815 out:
6816 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6817}
6818
6819/**
6820 * _scsih_search_responding_expanders -
6821 * @ioc: per adapter object
6822 *
6823 * After host reset, find out whether devices are still responding.
6824 * If not remove.
6825 *
6826 * Return nothing.
6827 */
6828static void
6829_scsih_search_responding_expanders(struct MPT3SAS_ADAPTER *ioc)
6830{
6831 Mpi2ExpanderPage0_t expander_pg0;
6832 Mpi2ConfigReply_t mpi_reply;
6833 u16 ioc_status;
6834 u64 sas_address;
6835 u16 handle;
6836
6837 pr_info(MPT3SAS_FMT "search for expanders: start\n", ioc->name);
6838
6839 if (list_empty(&ioc->sas_expander_list))
6840 goto out;
6841
6842 handle = 0xFFFF;
6843 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6844 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6845
6846 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6847 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy14be49a2013-06-29 03:53:02 +05306848 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306849 break;
6850
6851 handle = le16_to_cpu(expander_pg0.DevHandle);
6852 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6853 pr_info("\texpander present: handle(0x%04x), sas_addr(0x%016llx)\n",
6854 handle,
6855 (unsigned long long)sas_address);
6856 _scsih_mark_responding_expander(ioc, sas_address, handle);
6857 }
6858
6859 out:
6860 pr_info(MPT3SAS_FMT "search for expanders: complete\n", ioc->name);
6861}
6862
6863/**
6864 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
6865 * @ioc: per adapter object
6866 *
6867 * Return nothing.
6868 */
6869static void
6870_scsih_remove_unresponding_sas_devices(struct MPT3SAS_ADAPTER *ioc)
6871{
6872 struct _sas_device *sas_device, *sas_device_next;
6873 struct _sas_node *sas_expander, *sas_expander_next;
6874 struct _raid_device *raid_device, *raid_device_next;
6875 struct list_head tmp_list;
6876 unsigned long flags;
6877
6878 pr_info(MPT3SAS_FMT "removing unresponding devices: start\n",
6879 ioc->name);
6880
6881 /* removing unresponding end devices */
6882 pr_info(MPT3SAS_FMT "removing unresponding devices: end-devices\n",
6883 ioc->name);
6884 list_for_each_entry_safe(sas_device, sas_device_next,
6885 &ioc->sas_device_list, list) {
6886 if (!sas_device->responding)
6887 mpt3sas_device_remove_by_sas_address(ioc,
6888 sas_device->sas_address);
6889 else
6890 sas_device->responding = 0;
6891 }
6892
6893 /* removing unresponding volumes */
6894 if (ioc->ir_firmware) {
6895 pr_info(MPT3SAS_FMT "removing unresponding devices: volumes\n",
6896 ioc->name);
6897 list_for_each_entry_safe(raid_device, raid_device_next,
6898 &ioc->raid_device_list, list) {
6899 if (!raid_device->responding)
6900 _scsih_sas_volume_delete(ioc,
6901 raid_device->handle);
6902 else
6903 raid_device->responding = 0;
6904 }
6905 }
6906
6907 /* removing unresponding expanders */
6908 pr_info(MPT3SAS_FMT "removing unresponding devices: expanders\n",
6909 ioc->name);
6910 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6911 INIT_LIST_HEAD(&tmp_list);
6912 list_for_each_entry_safe(sas_expander, sas_expander_next,
6913 &ioc->sas_expander_list, list) {
6914 if (!sas_expander->responding)
6915 list_move_tail(&sas_expander->list, &tmp_list);
6916 else
6917 sas_expander->responding = 0;
6918 }
6919 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6920 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
6921 list) {
6922 list_del(&sas_expander->list);
6923 _scsih_expander_node_remove(ioc, sas_expander);
6924 }
6925
6926 pr_info(MPT3SAS_FMT "removing unresponding devices: complete\n",
6927 ioc->name);
6928
6929 /* unblock devices */
6930 _scsih_ublock_io_all_device(ioc);
6931}
6932
6933static void
6934_scsih_refresh_expander_links(struct MPT3SAS_ADAPTER *ioc,
6935 struct _sas_node *sas_expander, u16 handle)
6936{
6937 Mpi2ExpanderPage1_t expander_pg1;
6938 Mpi2ConfigReply_t mpi_reply;
6939 int i;
6940
6941 for (i = 0 ; i < sas_expander->num_phys ; i++) {
6942 if ((mpt3sas_config_get_expander_pg1(ioc, &mpi_reply,
6943 &expander_pg1, i, handle))) {
6944 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
6945 ioc->name, __FILE__, __LINE__, __func__);
6946 return;
6947 }
6948
6949 mpt3sas_transport_update_links(ioc, sas_expander->sas_address,
6950 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6951 expander_pg1.NegotiatedLinkRate >> 4);
6952 }
6953}
6954
6955/**
6956 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
6957 * @ioc: per adapter object
6958 *
6959 * Return nothing.
6960 */
6961static void
6962_scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
6963{
6964 Mpi2ExpanderPage0_t expander_pg0;
6965 Mpi2SasDevicePage0_t sas_device_pg0;
6966 Mpi2RaidVolPage1_t volume_pg1;
6967 Mpi2RaidVolPage0_t volume_pg0;
6968 Mpi2RaidPhysDiskPage0_t pd_pg0;
6969 Mpi2EventIrConfigElement_t element;
6970 Mpi2ConfigReply_t mpi_reply;
6971 u8 phys_disk_num;
6972 u16 ioc_status;
6973 u16 handle, parent_handle;
6974 u64 sas_address;
6975 struct _sas_device *sas_device;
6976 struct _sas_node *expander_device;
6977 static struct _raid_device *raid_device;
6978 u8 retry_count;
6979 unsigned long flags;
6980
6981 pr_info(MPT3SAS_FMT "scan devices: start\n", ioc->name);
6982
6983 _scsih_sas_host_refresh(ioc);
6984
6985 pr_info(MPT3SAS_FMT "\tscan devices: expanders start\n", ioc->name);
6986
6987 /* expanders */
6988 handle = 0xFFFF;
6989 while (!(mpt3sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6990 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6991 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6992 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05306993 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6994 pr_info(MPT3SAS_FMT "\tbreak from expander scan: " \
6995 "ioc_status(0x%04x), loginfo(0x%08x)\n",
6996 ioc->name, ioc_status,
6997 le32_to_cpu(mpi_reply.IOCLogInfo));
6998 break;
6999 }
7000 handle = le16_to_cpu(expander_pg0.DevHandle);
7001 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7002 expander_device = mpt3sas_scsih_expander_find_by_sas_address(
7003 ioc, le64_to_cpu(expander_pg0.SASAddress));
7004 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7005 if (expander_device)
7006 _scsih_refresh_expander_links(ioc, expander_device,
7007 handle);
7008 else {
7009 pr_info(MPT3SAS_FMT "\tBEFORE adding expander: " \
7010 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7011 handle, (unsigned long long)
7012 le64_to_cpu(expander_pg0.SASAddress));
7013 _scsih_expander_add(ioc, handle);
7014 pr_info(MPT3SAS_FMT "\tAFTER adding expander: " \
7015 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7016 handle, (unsigned long long)
7017 le64_to_cpu(expander_pg0.SASAddress));
7018 }
7019 }
7020
7021 pr_info(MPT3SAS_FMT "\tscan devices: expanders complete\n",
7022 ioc->name);
7023
7024 if (!ioc->ir_firmware)
7025 goto skip_to_sas;
7026
7027 pr_info(MPT3SAS_FMT "\tscan devices: phys disk start\n", ioc->name);
7028
7029 /* phys disk */
7030 phys_disk_num = 0xFF;
7031 while (!(mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7032 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7033 phys_disk_num))) {
7034 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7035 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307036 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7037 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan: "\
7038 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7039 ioc->name, ioc_status,
7040 le32_to_cpu(mpi_reply.IOCLogInfo));
7041 break;
7042 }
7043 phys_disk_num = pd_pg0.PhysDiskNum;
7044 handle = le16_to_cpu(pd_pg0.DevHandle);
7045 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7046 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7047 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7048 if (sas_device)
7049 continue;
7050 if (mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7051 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7052 handle) != 0)
7053 continue;
7054 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7055 MPI2_IOCSTATUS_MASK;
7056 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7057 pr_info(MPT3SAS_FMT "\tbreak from phys disk scan " \
7058 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7059 ioc->name, ioc_status,
7060 le32_to_cpu(mpi_reply.IOCLogInfo));
7061 break;
7062 }
7063 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7064 if (!_scsih_get_sas_address(ioc, parent_handle,
7065 &sas_address)) {
7066 pr_info(MPT3SAS_FMT "\tBEFORE adding phys disk: " \
7067 " handle (0x%04x), sas_addr(0x%016llx)\n",
7068 ioc->name, handle, (unsigned long long)
7069 le64_to_cpu(sas_device_pg0.SASAddress));
7070 mpt3sas_transport_update_links(ioc, sas_address,
7071 handle, sas_device_pg0.PhyNum,
7072 MPI2_SAS_NEG_LINK_RATE_1_5);
7073 set_bit(handle, ioc->pd_handles);
7074 retry_count = 0;
7075 /* This will retry adding the end device.
7076 * _scsih_add_device() will decide on retries and
7077 * return "1" when it should be retried
7078 */
7079 while (_scsih_add_device(ioc, handle, retry_count++,
7080 1)) {
7081 ssleep(1);
7082 }
7083 pr_info(MPT3SAS_FMT "\tAFTER adding phys disk: " \
7084 " handle (0x%04x), sas_addr(0x%016llx)\n",
7085 ioc->name, handle, (unsigned long long)
7086 le64_to_cpu(sas_device_pg0.SASAddress));
7087 }
7088 }
7089
7090 pr_info(MPT3SAS_FMT "\tscan devices: phys disk complete\n",
7091 ioc->name);
7092
7093 pr_info(MPT3SAS_FMT "\tscan devices: volumes start\n", ioc->name);
7094
7095 /* volumes */
7096 handle = 0xFFFF;
7097 while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7098 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7099 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7100 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307101 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7102 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7103 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7104 ioc->name, ioc_status,
7105 le32_to_cpu(mpi_reply.IOCLogInfo));
7106 break;
7107 }
7108 handle = le16_to_cpu(volume_pg1.DevHandle);
7109 spin_lock_irqsave(&ioc->raid_device_lock, flags);
7110 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7111 le64_to_cpu(volume_pg1.WWID));
7112 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
7113 if (raid_device)
7114 continue;
7115 if (mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7116 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7117 sizeof(Mpi2RaidVolPage0_t)))
7118 continue;
7119 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7120 MPI2_IOCSTATUS_MASK;
7121 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7122 pr_info(MPT3SAS_FMT "\tbreak from volume scan: " \
7123 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7124 ioc->name, ioc_status,
7125 le32_to_cpu(mpi_reply.IOCLogInfo));
7126 break;
7127 }
7128 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7129 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7130 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7131 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7132 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7133 element.VolDevHandle = volume_pg1.DevHandle;
7134 pr_info(MPT3SAS_FMT
7135 "\tBEFORE adding volume: handle (0x%04x)\n",
7136 ioc->name, volume_pg1.DevHandle);
7137 _scsih_sas_volume_add(ioc, &element);
7138 pr_info(MPT3SAS_FMT
7139 "\tAFTER adding volume: handle (0x%04x)\n",
7140 ioc->name, volume_pg1.DevHandle);
7141 }
7142 }
7143
7144 pr_info(MPT3SAS_FMT "\tscan devices: volumes complete\n",
7145 ioc->name);
7146
7147 skip_to_sas:
7148
7149 pr_info(MPT3SAS_FMT "\tscan devices: end devices start\n",
7150 ioc->name);
7151
7152 /* sas devices */
7153 handle = 0xFFFF;
7154 while (!(mpt3sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7155 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7156 handle))) {
7157 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7158 MPI2_IOCSTATUS_MASK;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307159 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7160 pr_info(MPT3SAS_FMT "\tbreak from end device scan:"\
7161 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7162 ioc->name, ioc_status,
7163 le32_to_cpu(mpi_reply.IOCLogInfo));
7164 break;
7165 }
7166 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7167 if (!(_scsih_is_end_device(
7168 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7169 continue;
7170 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7171 sas_device = mpt3sas_scsih_sas_device_find_by_sas_address(ioc,
7172 le64_to_cpu(sas_device_pg0.SASAddress));
7173 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7174 if (sas_device)
7175 continue;
7176 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7177 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7178 pr_info(MPT3SAS_FMT "\tBEFORE adding end device: " \
7179 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7180 handle, (unsigned long long)
7181 le64_to_cpu(sas_device_pg0.SASAddress));
7182 mpt3sas_transport_update_links(ioc, sas_address, handle,
7183 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7184 retry_count = 0;
7185 /* This will retry adding the end device.
7186 * _scsih_add_device() will decide on retries and
7187 * return "1" when it should be retried
7188 */
7189 while (_scsih_add_device(ioc, handle, retry_count++,
7190 0)) {
7191 ssleep(1);
7192 }
7193 pr_info(MPT3SAS_FMT "\tAFTER adding end device: " \
7194 "handle (0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7195 handle, (unsigned long long)
7196 le64_to_cpu(sas_device_pg0.SASAddress));
7197 }
7198 }
7199 pr_info(MPT3SAS_FMT "\tscan devices: end devices complete\n",
7200 ioc->name);
7201
7202 pr_info(MPT3SAS_FMT "scan devices: complete\n", ioc->name);
7203}
7204/**
7205 * mpt3sas_scsih_reset_handler - reset callback handler (for scsih)
7206 * @ioc: per adapter object
7207 * @reset_phase: phase
7208 *
7209 * The handler for doing any required cleanup or initialization.
7210 *
7211 * The reset phase can be MPT3_IOC_PRE_RESET, MPT3_IOC_AFTER_RESET,
7212 * MPT3_IOC_DONE_RESET
7213 *
7214 * Return nothing.
7215 */
7216void
7217mpt3sas_scsih_reset_handler(struct MPT3SAS_ADAPTER *ioc, int reset_phase)
7218{
7219 switch (reset_phase) {
7220 case MPT3_IOC_PRE_RESET:
7221 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7222 "%s: MPT3_IOC_PRE_RESET\n", ioc->name, __func__));
7223 break;
7224 case MPT3_IOC_AFTER_RESET:
7225 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7226 "%s: MPT3_IOC_AFTER_RESET\n", ioc->name, __func__));
7227 if (ioc->scsih_cmds.status & MPT3_CMD_PENDING) {
7228 ioc->scsih_cmds.status |= MPT3_CMD_RESET;
7229 mpt3sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7230 complete(&ioc->scsih_cmds.done);
7231 }
7232 if (ioc->tm_cmds.status & MPT3_CMD_PENDING) {
7233 ioc->tm_cmds.status |= MPT3_CMD_RESET;
7234 mpt3sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7235 complete(&ioc->tm_cmds.done);
7236 }
7237
7238 _scsih_fw_event_cleanup_queue(ioc);
7239 _scsih_flush_running_cmds(ioc);
7240 break;
7241 case MPT3_IOC_DONE_RESET:
7242 dtmprintk(ioc, pr_info(MPT3SAS_FMT
7243 "%s: MPT3_IOC_DONE_RESET\n", ioc->name, __func__));
7244 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7245 !ioc->sas_hba.num_phys)) {
7246 _scsih_prep_device_scan(ioc);
7247 _scsih_search_responding_sas_devices(ioc);
7248 _scsih_search_responding_raid_devices(ioc);
7249 _scsih_search_responding_expanders(ioc);
7250 _scsih_error_recovery_delete_devices(ioc);
7251 }
7252 break;
7253 }
7254}
7255
7256/**
7257 * _mpt3sas_fw_work - delayed task for processing firmware events
7258 * @ioc: per adapter object
7259 * @fw_event: The fw_event_work object
7260 * Context: user.
7261 *
7262 * Return nothing.
7263 */
7264static void
7265_mpt3sas_fw_work(struct MPT3SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
7266{
7267 /* the queue is being flushed so ignore this event */
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05307268 if (ioc->remove_host ||
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307269 ioc->pci_error_recovery) {
7270 _scsih_fw_event_free(ioc, fw_event);
7271 return;
7272 }
7273
7274 switch (fw_event->event) {
7275 case MPT3SAS_PROCESS_TRIGGER_DIAG:
Joe Lawrence35b62362014-06-25 17:05:34 -04007276 mpt3sas_process_trigger_data(ioc,
7277 (struct SL_WH_TRIGGERS_EVENT_DATA_T *)
7278 fw_event->event_data);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307279 break;
7280 case MPT3SAS_REMOVE_UNRESPONDING_DEVICES:
7281 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
7282 ssleep(1);
7283 _scsih_remove_unresponding_sas_devices(ioc);
7284 _scsih_scan_for_devices_after_reset(ioc);
7285 break;
7286 case MPT3SAS_PORT_ENABLE_COMPLETE:
7287 ioc->start_scan = 0;
7288 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7289 mpt3sas_base_update_missing_delay(ioc, missing_delay[0],
7290 missing_delay[1]);
7291 dewtprintk(ioc, pr_info(MPT3SAS_FMT
7292 "port enable: complete from worker thread\n",
7293 ioc->name));
7294 break;
Sreekanth Reddy0f624c32014-09-12 15:35:26 +05307295 case MPT3SAS_TURN_ON_PFA_LED:
7296 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307297 break;
7298 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7299 _scsih_sas_topology_change_event(ioc, fw_event);
7300 break;
7301 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7302 _scsih_sas_device_status_change_event(ioc, fw_event);
7303 break;
7304 case MPI2_EVENT_SAS_DISCOVERY:
7305 _scsih_sas_discovery_event(ioc, fw_event);
7306 break;
7307 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7308 _scsih_sas_broadcast_primitive_event(ioc, fw_event);
7309 break;
7310 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7311 _scsih_sas_enclosure_dev_status_change_event(ioc,
7312 fw_event);
7313 break;
7314 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7315 _scsih_sas_ir_config_change_event(ioc, fw_event);
7316 break;
7317 case MPI2_EVENT_IR_VOLUME:
7318 _scsih_sas_ir_volume_event(ioc, fw_event);
7319 break;
7320 case MPI2_EVENT_IR_PHYSICAL_DISK:
7321 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
7322 break;
7323 case MPI2_EVENT_IR_OPERATION_STATUS:
7324 _scsih_sas_ir_operation_status_event(ioc, fw_event);
7325 break;
7326 }
7327 _scsih_fw_event_free(ioc, fw_event);
7328}
7329
7330/**
7331 * _firmware_event_work
7332 * @ioc: per adapter object
7333 * @work: The fw_event_work object
7334 * Context: user.
7335 *
7336 * wrappers for the work thread handling firmware events
7337 *
7338 * Return nothing.
7339 */
7340
7341static void
7342_firmware_event_work(struct work_struct *work)
7343{
7344 struct fw_event_work *fw_event = container_of(work,
7345 struct fw_event_work, work);
7346
7347 _mpt3sas_fw_work(fw_event->ioc, fw_event);
7348}
7349
7350/**
7351 * mpt3sas_scsih_event_callback - firmware event handler (called at ISR time)
7352 * @ioc: per adapter object
7353 * @msix_index: MSIX table index supplied by the OS
7354 * @reply: reply message frame(lower 32bit addr)
7355 * Context: interrupt.
7356 *
7357 * This function merely adds a new work task into ioc->firmware_event_thread.
7358 * The tasks are worked from _firmware_event_work in user context.
7359 *
7360 * Return 1 meaning mf should be freed from _base_interrupt
7361 * 0 means the mf is freed from this function.
7362 */
7363u8
7364mpt3sas_scsih_event_callback(struct MPT3SAS_ADAPTER *ioc, u8 msix_index,
7365 u32 reply)
7366{
7367 struct fw_event_work *fw_event;
7368 Mpi2EventNotificationReply_t *mpi_reply;
7369 u16 event;
7370 u16 sz;
7371
7372 /* events turned off due to host reset or driver unloading */
7373 if (ioc->remove_host || ioc->pci_error_recovery)
7374 return 1;
7375
7376 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
7377
7378 if (unlikely(!mpi_reply)) {
7379 pr_err(MPT3SAS_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7380 ioc->name, __FILE__, __LINE__, __func__);
7381 return 1;
7382 }
7383
7384 event = le16_to_cpu(mpi_reply->Event);
7385
7386 if (event != MPI2_EVENT_LOG_ENTRY_ADDED)
7387 mpt3sas_trigger_event(ioc, event, 0);
7388
7389 switch (event) {
7390 /* handle these */
7391 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7392 {
7393 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7394 (Mpi2EventDataSasBroadcastPrimitive_t *)
7395 mpi_reply->EventData;
7396
7397 if (baen_data->Primitive !=
7398 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
7399 return 1;
7400
7401 if (ioc->broadcast_aen_busy) {
7402 ioc->broadcast_aen_pending++;
7403 return 1;
7404 } else
7405 ioc->broadcast_aen_busy = 1;
7406 break;
7407 }
7408
7409 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7410 _scsih_check_topo_delete_events(ioc,
7411 (Mpi2EventDataSasTopologyChangeList_t *)
7412 mpi_reply->EventData);
7413 break;
7414 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7415 _scsih_check_ir_config_unhide_events(ioc,
7416 (Mpi2EventDataIrConfigChangeList_t *)
7417 mpi_reply->EventData);
7418 break;
7419 case MPI2_EVENT_IR_VOLUME:
7420 _scsih_check_volume_delete_events(ioc,
7421 (Mpi2EventDataIrVolume_t *)
7422 mpi_reply->EventData);
7423 break;
7424
7425 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7426 case MPI2_EVENT_IR_OPERATION_STATUS:
7427 case MPI2_EVENT_SAS_DISCOVERY:
7428 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7429 case MPI2_EVENT_IR_PHYSICAL_DISK:
7430 break;
7431
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05307432 case MPI2_EVENT_TEMP_THRESHOLD:
7433 _scsih_temp_threshold_events(ioc,
7434 (Mpi2EventDataTemperature_t *)
7435 mpi_reply->EventData);
7436 break;
7437
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307438 default: /* ignore the rest */
7439 return 1;
7440 }
7441
Joe Lawrence35b62362014-06-25 17:05:34 -04007442 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7443 fw_event = kzalloc(sizeof(*fw_event) + sz, GFP_ATOMIC);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307444 if (!fw_event) {
7445 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
7446 ioc->name, __FILE__, __LINE__, __func__);
7447 return 1;
7448 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307449
7450 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
7451 fw_event->ioc = ioc;
7452 fw_event->VF_ID = mpi_reply->VF_ID;
7453 fw_event->VP_ID = mpi_reply->VP_ID;
7454 fw_event->event = event;
7455 _scsih_fw_event_add(ioc, fw_event);
7456 return 1;
7457}
7458
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307459/**
7460 * _scsih_expander_node_remove - removing expander device from list.
7461 * @ioc: per adapter object
7462 * @sas_expander: the sas_device object
7463 * Context: Calling function should acquire ioc->sas_node_lock.
7464 *
7465 * Removing object and freeing associated memory from the
7466 * ioc->sas_expander_list.
7467 *
7468 * Return nothing.
7469 */
7470static void
7471_scsih_expander_node_remove(struct MPT3SAS_ADAPTER *ioc,
7472 struct _sas_node *sas_expander)
7473{
7474 struct _sas_port *mpt3sas_port, *next;
7475
7476 /* remove sibling ports attached to this expander */
7477 list_for_each_entry_safe(mpt3sas_port, next,
7478 &sas_expander->sas_port_list, port_list) {
7479 if (ioc->shost_recovery)
7480 return;
7481 if (mpt3sas_port->remote_identify.device_type ==
7482 SAS_END_DEVICE)
7483 mpt3sas_device_remove_by_sas_address(ioc,
7484 mpt3sas_port->remote_identify.sas_address);
7485 else if (mpt3sas_port->remote_identify.device_type ==
7486 SAS_EDGE_EXPANDER_DEVICE ||
7487 mpt3sas_port->remote_identify.device_type ==
7488 SAS_FANOUT_EXPANDER_DEVICE)
7489 mpt3sas_expander_remove(ioc,
7490 mpt3sas_port->remote_identify.sas_address);
7491 }
7492
7493 mpt3sas_transport_port_remove(ioc, sas_expander->sas_address,
7494 sas_expander->sas_address_parent);
7495
7496 pr_info(MPT3SAS_FMT
7497 "expander_remove: handle(0x%04x), sas_addr(0x%016llx)\n",
7498 ioc->name,
7499 sas_expander->handle, (unsigned long long)
7500 sas_expander->sas_address);
7501
7502 kfree(sas_expander->phy);
7503 kfree(sas_expander);
7504}
7505
7506/**
7507 * _scsih_ir_shutdown - IR shutdown notification
7508 * @ioc: per adapter object
7509 *
7510 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7511 * the host system is shutting down.
7512 *
7513 * Return nothing.
7514 */
7515static void
7516_scsih_ir_shutdown(struct MPT3SAS_ADAPTER *ioc)
7517{
7518 Mpi2RaidActionRequest_t *mpi_request;
7519 Mpi2RaidActionReply_t *mpi_reply;
7520 u16 smid;
7521
7522 /* is IR firmware build loaded ? */
7523 if (!ioc->ir_firmware)
7524 return;
7525
7526 /* are there any volumes ? */
7527 if (list_empty(&ioc->raid_device_list))
7528 return;
7529
7530 mutex_lock(&ioc->scsih_cmds.mutex);
7531
7532 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
7533 pr_err(MPT3SAS_FMT "%s: scsih_cmd in use\n",
7534 ioc->name, __func__);
7535 goto out;
7536 }
7537 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
7538
7539 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7540 if (!smid) {
7541 pr_err(MPT3SAS_FMT "%s: failed obtaining a smid\n",
7542 ioc->name, __func__);
7543 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7544 goto out;
7545 }
7546
7547 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
7548 ioc->scsih_cmds.smid = smid;
7549 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7550
7551 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7552 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7553
7554 pr_info(MPT3SAS_FMT "IR shutdown (sending)\n", ioc->name);
7555 init_completion(&ioc->scsih_cmds.done);
7556 mpt3sas_base_put_smid_default(ioc, smid);
7557 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7558
7559 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
7560 pr_err(MPT3SAS_FMT "%s: timeout\n",
7561 ioc->name, __func__);
7562 goto out;
7563 }
7564
7565 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
7566 mpi_reply = ioc->scsih_cmds.reply;
7567 pr_info(MPT3SAS_FMT
7568 "IR shutdown (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
7569 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7570 le32_to_cpu(mpi_reply->IOCLogInfo));
7571 }
7572
7573 out:
7574 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
7575 mutex_unlock(&ioc->scsih_cmds.mutex);
7576}
7577
7578/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307579 * scsih_remove - detach and remove add host
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307580 * @pdev: PCI device struct
7581 *
7582 * Routine called when unloading the driver.
7583 * Return nothing.
7584 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307585void scsih_remove(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307586{
7587 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7588 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7589 struct _sas_port *mpt3sas_port, *next_port;
7590 struct _raid_device *raid_device, *next;
7591 struct MPT3SAS_TARGET *sas_target_priv_data;
7592 struct workqueue_struct *wq;
7593 unsigned long flags;
7594
7595 ioc->remove_host = 1;
7596 _scsih_fw_event_cleanup_queue(ioc);
7597
7598 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7599 wq = ioc->firmware_event_thread;
7600 ioc->firmware_event_thread = NULL;
7601 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7602 if (wq)
7603 destroy_workqueue(wq);
7604
7605 /* release all the volumes */
7606 _scsih_ir_shutdown(ioc);
7607 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7608 list) {
7609 if (raid_device->starget) {
7610 sas_target_priv_data =
7611 raid_device->starget->hostdata;
7612 sas_target_priv_data->deleted = 1;
7613 scsi_remove_target(&raid_device->starget->dev);
7614 }
7615 pr_info(MPT3SAS_FMT "removing handle(0x%04x), wwid(0x%016llx)\n",
7616 ioc->name, raid_device->handle,
7617 (unsigned long long) raid_device->wwid);
7618 _scsih_raid_device_remove(ioc, raid_device);
7619 }
7620
7621 /* free ports attached to the sas_host */
7622 list_for_each_entry_safe(mpt3sas_port, next_port,
7623 &ioc->sas_hba.sas_port_list, port_list) {
7624 if (mpt3sas_port->remote_identify.device_type ==
7625 SAS_END_DEVICE)
7626 mpt3sas_device_remove_by_sas_address(ioc,
7627 mpt3sas_port->remote_identify.sas_address);
7628 else if (mpt3sas_port->remote_identify.device_type ==
7629 SAS_EDGE_EXPANDER_DEVICE ||
7630 mpt3sas_port->remote_identify.device_type ==
7631 SAS_FANOUT_EXPANDER_DEVICE)
7632 mpt3sas_expander_remove(ioc,
7633 mpt3sas_port->remote_identify.sas_address);
7634 }
7635
7636 /* free phys attached to the sas_host */
7637 if (ioc->sas_hba.num_phys) {
7638 kfree(ioc->sas_hba.phy);
7639 ioc->sas_hba.phy = NULL;
7640 ioc->sas_hba.num_phys = 0;
7641 }
7642
7643 sas_remove_host(shost);
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05307644 scsi_remove_host(shost);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307645 mpt3sas_base_detach(ioc);
7646 list_del(&ioc->list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307647 scsi_host_put(shost);
7648}
7649
7650/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307651 * scsih_shutdown - routine call during system shutdown
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307652 * @pdev: PCI device struct
7653 *
7654 * Return nothing.
7655 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307656void
7657scsih_shutdown(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307658{
7659 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7660 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7661 struct workqueue_struct *wq;
7662 unsigned long flags;
7663
7664 ioc->remove_host = 1;
7665 _scsih_fw_event_cleanup_queue(ioc);
7666
7667 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7668 wq = ioc->firmware_event_thread;
7669 ioc->firmware_event_thread = NULL;
7670 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7671 if (wq)
7672 destroy_workqueue(wq);
7673
7674 _scsih_ir_shutdown(ioc);
7675 mpt3sas_base_detach(ioc);
7676}
7677
7678
7679/**
7680 * _scsih_probe_boot_devices - reports 1st device
7681 * @ioc: per adapter object
7682 *
7683 * If specified in bios page 2, this routine reports the 1st
7684 * device scsi-ml or sas transport for persistent boot device
7685 * purposes. Please refer to function _scsih_determine_boot_device()
7686 */
7687static void
7688_scsih_probe_boot_devices(struct MPT3SAS_ADAPTER *ioc)
7689{
7690 u8 is_raid;
7691 void *device;
7692 struct _sas_device *sas_device;
7693 struct _raid_device *raid_device;
7694 u16 handle;
7695 u64 sas_address_parent;
7696 u64 sas_address;
7697 unsigned long flags;
7698 int rc;
7699
7700 /* no Bios, return immediately */
7701 if (!ioc->bios_pg3.BiosVersion)
7702 return;
7703
7704 device = NULL;
7705 is_raid = 0;
7706 if (ioc->req_boot_device.device) {
7707 device = ioc->req_boot_device.device;
7708 is_raid = ioc->req_boot_device.is_raid;
7709 } else if (ioc->req_alt_boot_device.device) {
7710 device = ioc->req_alt_boot_device.device;
7711 is_raid = ioc->req_alt_boot_device.is_raid;
7712 } else if (ioc->current_boot_device.device) {
7713 device = ioc->current_boot_device.device;
7714 is_raid = ioc->current_boot_device.is_raid;
7715 }
7716
7717 if (!device)
7718 return;
7719
7720 if (is_raid) {
7721 raid_device = device;
7722 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7723 raid_device->id, 0);
7724 if (rc)
7725 _scsih_raid_device_remove(ioc, raid_device);
7726 } else {
7727 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7728 sas_device = device;
7729 handle = sas_device->handle;
7730 sas_address_parent = sas_device->sas_address_parent;
7731 sas_address = sas_device->sas_address;
7732 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7733 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7734
7735 if (!mpt3sas_transport_port_add(ioc, handle,
7736 sas_address_parent)) {
7737 _scsih_sas_device_remove(ioc, sas_device);
7738 } else if (!sas_device->starget) {
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05307739 if (!ioc->is_driver_loading) {
7740 mpt3sas_transport_port_remove(ioc,
7741 sas_address,
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307742 sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05307743 _scsih_sas_device_remove(ioc, sas_device);
7744 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307745 }
7746 }
7747}
7748
7749/**
7750 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7751 * @ioc: per adapter object
7752 *
7753 * Called during initial loading of the driver.
7754 */
7755static void
7756_scsih_probe_raid(struct MPT3SAS_ADAPTER *ioc)
7757{
7758 struct _raid_device *raid_device, *raid_next;
7759 int rc;
7760
7761 list_for_each_entry_safe(raid_device, raid_next,
7762 &ioc->raid_device_list, list) {
7763 if (raid_device->starget)
7764 continue;
7765 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7766 raid_device->id, 0);
7767 if (rc)
7768 _scsih_raid_device_remove(ioc, raid_device);
7769 }
7770}
7771
7772/**
7773 * _scsih_probe_sas - reporting sas devices to sas transport
7774 * @ioc: per adapter object
7775 *
7776 * Called during initial loading of the driver.
7777 */
7778static void
7779_scsih_probe_sas(struct MPT3SAS_ADAPTER *ioc)
7780{
7781 struct _sas_device *sas_device, *next;
7782 unsigned long flags;
7783
7784 /* SAS Device List */
7785 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7786 list) {
7787
7788 if (!mpt3sas_transport_port_add(ioc, sas_device->handle,
7789 sas_device->sas_address_parent)) {
7790 list_del(&sas_device->list);
7791 kfree(sas_device);
7792 continue;
7793 } else if (!sas_device->starget) {
7794 /*
7795 * When asyn scanning is enabled, its not possible to
7796 * remove devices while scanning is turned on due to an
7797 * oops in scsi_sysfs_add_sdev()->add_device()->
7798 * sysfs_addrm_start()
7799 */
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05307800 if (!ioc->is_driver_loading) {
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307801 mpt3sas_transport_port_remove(ioc,
7802 sas_device->sas_address,
7803 sas_device->sas_address_parent);
Sreekanth Reddyf5edbe72013-06-29 03:54:51 +05307804 list_del(&sas_device->list);
7805 kfree(sas_device);
7806 continue;
7807 }
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307808 }
7809
7810 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7811 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7812 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7813 }
7814}
7815
7816/**
7817 * _scsih_probe_devices - probing for devices
7818 * @ioc: per adapter object
7819 *
7820 * Called during initial loading of the driver.
7821 */
7822static void
7823_scsih_probe_devices(struct MPT3SAS_ADAPTER *ioc)
7824{
7825 u16 volume_mapping_flags;
7826
7827 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7828 return; /* return when IOC doesn't support initiator mode */
7829
7830 _scsih_probe_boot_devices(ioc);
7831
7832 if (ioc->ir_firmware) {
7833 volume_mapping_flags =
7834 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7835 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7836 if (volume_mapping_flags ==
7837 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
7838 _scsih_probe_raid(ioc);
7839 _scsih_probe_sas(ioc);
7840 } else {
7841 _scsih_probe_sas(ioc);
7842 _scsih_probe_raid(ioc);
7843 }
7844 } else
7845 _scsih_probe_sas(ioc);
7846}
7847
7848/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307849 * scsih_scan_start - scsi lld callback for .scan_start
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307850 * @shost: SCSI host pointer
7851 *
7852 * The shost has the ability to discover targets on its own instead
7853 * of scanning the entire bus. In our implemention, we will kick off
7854 * firmware discovery.
7855 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307856void
7857scsih_scan_start(struct Scsi_Host *shost)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307858{
7859 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7860 int rc;
7861 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7862 mpt3sas_enable_diag_buffer(ioc, diag_buffer_enable);
7863
7864 if (disable_discovery > 0)
7865 return;
7866
7867 ioc->start_scan = 1;
7868 rc = mpt3sas_port_enable(ioc);
7869
7870 if (rc != 0)
7871 pr_info(MPT3SAS_FMT "port enable: FAILED\n", ioc->name);
7872}
7873
7874/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307875 * scsih_scan_finished - scsi lld callback for .scan_finished
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307876 * @shost: SCSI host pointer
7877 * @time: elapsed time of the scan in jiffies
7878 *
7879 * This function will be called periodicallyn until it returns 1 with the
7880 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7881 * we wait for firmware discovery to complete, then return 1.
7882 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307883int
7884scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307885{
7886 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
7887
7888 if (disable_discovery > 0) {
7889 ioc->is_driver_loading = 0;
7890 ioc->wait_for_discovery_to_complete = 0;
7891 return 1;
7892 }
7893
7894 if (time >= (300 * HZ)) {
7895 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7896 pr_info(MPT3SAS_FMT
7897 "port enable: FAILED with timeout (timeout=300s)\n",
7898 ioc->name);
7899 ioc->is_driver_loading = 0;
7900 return 1;
7901 }
7902
7903 if (ioc->start_scan)
7904 return 0;
7905
7906 if (ioc->start_scan_failed) {
7907 pr_info(MPT3SAS_FMT
7908 "port enable: FAILED with (ioc_status=0x%08x)\n",
7909 ioc->name, ioc->start_scan_failed);
7910 ioc->is_driver_loading = 0;
7911 ioc->wait_for_discovery_to_complete = 0;
7912 ioc->remove_host = 1;
7913 return 1;
7914 }
7915
7916 pr_info(MPT3SAS_FMT "port enable: SUCCESS\n", ioc->name);
7917 ioc->base_cmds.status = MPT3_CMD_NOT_USED;
7918
7919 if (ioc->wait_for_discovery_to_complete) {
7920 ioc->wait_for_discovery_to_complete = 0;
7921 _scsih_probe_devices(ioc);
7922 }
7923 mpt3sas_base_start_watchdog(ioc);
7924 ioc->is_driver_loading = 0;
7925 return 1;
7926}
7927
Sreekanth Reddyd357e842015-11-11 17:30:22 +05307928void
7929_scsih_determine_hba_mpi_version(struct MPT3SAS_ADAPTER *ioc) {
7930
7931 switch (ioc->pdev->device) {
7932 case MPI2_MFGPAGE_DEVID_SAS2004:
7933 case MPI2_MFGPAGE_DEVID_SAS2008:
7934 case MPI2_MFGPAGE_DEVID_SAS2108_1:
7935 case MPI2_MFGPAGE_DEVID_SAS2108_2:
7936 case MPI2_MFGPAGE_DEVID_SAS2108_3:
7937 case MPI2_MFGPAGE_DEVID_SAS2116_1:
7938 case MPI2_MFGPAGE_DEVID_SAS2116_2:
7939 case MPI2_MFGPAGE_DEVID_SAS2208_1:
7940 case MPI2_MFGPAGE_DEVID_SAS2208_2:
7941 case MPI2_MFGPAGE_DEVID_SAS2208_3:
7942 case MPI2_MFGPAGE_DEVID_SAS2208_4:
7943 case MPI2_MFGPAGE_DEVID_SAS2208_5:
7944 case MPI2_MFGPAGE_DEVID_SAS2208_6:
7945 case MPI2_MFGPAGE_DEVID_SAS2308_1:
7946 case MPI2_MFGPAGE_DEVID_SAS2308_2:
7947 case MPI2_MFGPAGE_DEVID_SAS2308_3:
7948 ioc->hba_mpi_version_belonged = MPI2_VERSION;
7949 break;
7950 case MPI25_MFGPAGE_DEVID_SAS3004:
7951 case MPI25_MFGPAGE_DEVID_SAS3008:
7952 case MPI25_MFGPAGE_DEVID_SAS3108_1:
7953 case MPI25_MFGPAGE_DEVID_SAS3108_2:
7954 case MPI25_MFGPAGE_DEVID_SAS3108_5:
7955 case MPI25_MFGPAGE_DEVID_SAS3108_6:
7956 ioc->hba_mpi_version_belonged = MPI25_VERSION;
7957 break;
7958 }
7959}
7960
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307961/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307962 * scsih_probe - attach and add scsi host
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307963 * @pdev: PCI device struct
7964 * @id: pci device id
7965 *
7966 * Returns 0 success, anything else error.
7967 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05307968int
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05307969scsih_probe(struct pci_dev *pdev, struct Scsi_Host *shost)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307970{
7971 struct MPT3SAS_ADAPTER *ioc;
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05307972 int rv;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307973
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307974 /* init local params */
7975 ioc = shost_priv(shost);
7976 memset(ioc, 0, sizeof(struct MPT3SAS_ADAPTER));
7977 INIT_LIST_HEAD(&ioc->list);
7978 list_add_tail(&ioc->list, &mpt3sas_ioc_list);
7979 ioc->shost = shost;
7980 ioc->id = mpt_ids++;
Sreekanth Reddyd357e842015-11-11 17:30:22 +05307981
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05307982 ioc->pdev = pdev;
7983 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7984 ioc->tm_cb_idx = tm_cb_idx;
7985 ioc->ctl_cb_idx = ctl_cb_idx;
7986 ioc->base_cb_idx = base_cb_idx;
7987 ioc->port_enable_cb_idx = port_enable_cb_idx;
7988 ioc->transport_cb_idx = transport_cb_idx;
7989 ioc->scsih_cb_idx = scsih_cb_idx;
7990 ioc->config_cb_idx = config_cb_idx;
7991 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
7992 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
7993 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
7994 ioc->logging_level = logging_level;
7995 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
7996 /* misc semaphores and spin locks */
7997 mutex_init(&ioc->reset_in_progress_mutex);
7998 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7999 spin_lock_init(&ioc->scsi_lookup_lock);
8000 spin_lock_init(&ioc->sas_device_lock);
8001 spin_lock_init(&ioc->sas_node_lock);
8002 spin_lock_init(&ioc->fw_event_lock);
8003 spin_lock_init(&ioc->raid_device_lock);
8004 spin_lock_init(&ioc->diag_trigger_lock);
8005
8006 INIT_LIST_HEAD(&ioc->sas_device_list);
8007 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8008 INIT_LIST_HEAD(&ioc->sas_expander_list);
8009 INIT_LIST_HEAD(&ioc->fw_event_list);
8010 INIT_LIST_HEAD(&ioc->raid_device_list);
8011 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
8012 INIT_LIST_HEAD(&ioc->delayed_tr_list);
8013 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Joe Lawrencecf9bd21a2013-08-08 16:45:39 -04008014 INIT_LIST_HEAD(&ioc->reply_queue_list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308015
Sreekanth Reddyd357e842015-11-11 17:30:22 +05308016 _scsih_determine_hba_mpi_version(ioc);
8017 sprintf(ioc->name, "%s_cm%d", driver_name, ioc->id);
8018
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308019 /* init shost parameters */
8020 shost->max_cmd_len = 32;
8021 shost->max_lun = max_lun;
8022 shost->transportt = mpt3sas_transport_template;
8023 shost->unique_id = ioc->id;
8024
8025 if (max_sectors != 0xFFFF) {
8026 if (max_sectors < 64) {
8027 shost->max_sectors = 64;
8028 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8029 "for max_sectors, range is 64 to 32767. Assigning "
8030 "value of 64.\n", ioc->name, max_sectors);
8031 } else if (max_sectors > 32767) {
8032 shost->max_sectors = 32767;
8033 pr_warn(MPT3SAS_FMT "Invalid value %d passed " \
8034 "for max_sectors, range is 64 to 32767. Assigning "
8035 "default value of 32767.\n", ioc->name,
8036 max_sectors);
8037 } else {
8038 shost->max_sectors = max_sectors & 0xFFFE;
8039 pr_info(MPT3SAS_FMT
8040 "The max_sectors value is set to %d\n",
8041 ioc->name, shost->max_sectors);
8042 }
8043 }
8044
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308045 /* register EEDP capabilities with SCSI layer */
8046 if (prot_mask > 0)
8047 scsi_host_set_prot(shost, prot_mask);
8048 else
8049 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8050 | SHOST_DIF_TYPE2_PROTECTION
8051 | SHOST_DIF_TYPE3_PROTECTION);
8052
8053 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
8054
8055 /* event thread */
8056 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8057 "fw_event%d", ioc->id);
Sreekanth Reddybdff7852015-06-30 12:25:03 +05308058 ioc->firmware_event_thread = alloc_ordered_workqueue(
8059 ioc->firmware_event_name, WQ_MEM_RECLAIM);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308060 if (!ioc->firmware_event_thread) {
8061 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8062 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308063 rv = -ENODEV;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308064 goto out_thread_fail;
8065 }
8066
8067 ioc->is_driver_loading = 1;
8068 if ((mpt3sas_base_attach(ioc))) {
8069 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8070 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308071 rv = -ENODEV;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308072 goto out_attach_fail;
8073 }
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308074 rv = scsi_add_host(shost, &pdev->dev);
8075 if (rv) {
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308076 pr_err(MPT3SAS_FMT "failure at %s:%d/%s()!\n",
8077 ioc->name, __FILE__, __LINE__, __func__);
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308078 goto out_add_shost_fail;
8079 }
8080
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308081 scsi_scan_host(shost);
8082 return 0;
Reddy, Sreekanth4dc06fd2014-07-14 12:01:35 +05308083out_add_shost_fail:
8084 mpt3sas_base_detach(ioc);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308085 out_attach_fail:
8086 destroy_workqueue(ioc->firmware_event_thread);
8087 out_thread_fail:
8088 list_del(&ioc->list);
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308089 scsi_host_put(shost);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308090 return rv;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308091}
8092
8093#ifdef CONFIG_PM
8094/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308095 * scsih_suspend - power management suspend main entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308096 * @pdev: PCI device struct
8097 * @state: PM state change to (usually PCI_D3)
8098 *
8099 * Returns 0 success, anything else error.
8100 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308101int
8102scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308103{
8104 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8105 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8106 pci_power_t device_state;
8107
8108 mpt3sas_base_stop_watchdog(ioc);
8109 flush_scheduled_work();
8110 scsi_block_requests(shost);
8111 device_state = pci_choose_state(pdev, state);
8112 pr_info(MPT3SAS_FMT
8113 "pdev=0x%p, slot=%s, entering operating state [D%d]\n",
8114 ioc->name, pdev, pci_name(pdev), device_state);
8115
8116 pci_save_state(pdev);
8117 mpt3sas_base_free_resources(ioc);
8118 pci_set_power_state(pdev, device_state);
8119 return 0;
8120}
8121
8122/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308123 * scsih_resume - power management resume main entry point
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308124 * @pdev: PCI device struct
8125 *
8126 * Returns 0 success, anything else error.
8127 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308128int
8129scsih_resume(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308130{
8131 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8132 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8133 pci_power_t device_state = pdev->current_state;
8134 int r;
8135
8136 pr_info(MPT3SAS_FMT
8137 "pdev=0x%p, slot=%s, previous operating state [D%d]\n",
8138 ioc->name, pdev, pci_name(pdev), device_state);
8139
8140 pci_set_power_state(pdev, PCI_D0);
8141 pci_enable_wake(pdev, PCI_D0, 0);
8142 pci_restore_state(pdev);
8143 ioc->pdev = pdev;
8144 r = mpt3sas_base_map_resources(ioc);
8145 if (r)
8146 return r;
8147
8148 mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8149 scsi_unblock_requests(shost);
8150 mpt3sas_base_start_watchdog(ioc);
8151 return 0;
8152}
8153#endif /* CONFIG_PM */
8154
8155/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308156 * scsih_pci_error_detected - Called when a PCI error is detected.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308157 * @pdev: PCI device struct
8158 * @state: PCI channel state
8159 *
8160 * Description: Called when a PCI error is detected.
8161 *
8162 * Return value:
8163 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8164 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308165pci_ers_result_t
8166scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308167{
8168 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8169 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8170
8171 pr_info(MPT3SAS_FMT "PCI error: detected callback, state(%d)!!\n",
8172 ioc->name, state);
8173
8174 switch (state) {
8175 case pci_channel_io_normal:
8176 return PCI_ERS_RESULT_CAN_RECOVER;
8177 case pci_channel_io_frozen:
8178 /* Fatal error, prepare for slot reset */
8179 ioc->pci_error_recovery = 1;
8180 scsi_block_requests(ioc->shost);
8181 mpt3sas_base_stop_watchdog(ioc);
8182 mpt3sas_base_free_resources(ioc);
8183 return PCI_ERS_RESULT_NEED_RESET;
8184 case pci_channel_io_perm_failure:
8185 /* Permanent error, prepare for device removal */
8186 ioc->pci_error_recovery = 1;
8187 mpt3sas_base_stop_watchdog(ioc);
8188 _scsih_flush_running_cmds(ioc);
8189 return PCI_ERS_RESULT_DISCONNECT;
8190 }
8191 return PCI_ERS_RESULT_NEED_RESET;
8192}
8193
8194/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308195 * scsih_pci_slot_reset - Called when PCI slot has been reset.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308196 * @pdev: PCI device struct
8197 *
8198 * Description: This routine is called by the pci error recovery
8199 * code after the PCI slot has been reset, just before we
8200 * should resume normal operations.
8201 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308202pci_ers_result_t
8203scsih_pci_slot_reset(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308204{
8205 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8206 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8207 int rc;
8208
8209 pr_info(MPT3SAS_FMT "PCI error: slot reset callback!!\n",
8210 ioc->name);
8211
8212 ioc->pci_error_recovery = 0;
8213 ioc->pdev = pdev;
8214 pci_restore_state(pdev);
8215 rc = mpt3sas_base_map_resources(ioc);
8216 if (rc)
8217 return PCI_ERS_RESULT_DISCONNECT;
8218
8219 rc = mpt3sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8220 FORCE_BIG_HAMMER);
8221
8222 pr_warn(MPT3SAS_FMT "hard reset: %s\n", ioc->name,
8223 (rc == 0) ? "success" : "failed");
8224
8225 if (!rc)
8226 return PCI_ERS_RESULT_RECOVERED;
8227 else
8228 return PCI_ERS_RESULT_DISCONNECT;
8229}
8230
8231/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308232 * scsih_pci_resume() - resume normal ops after PCI reset
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308233 * @pdev: pointer to PCI device
8234 *
8235 * Called when the error recovery driver tells us that its
8236 * OK to resume normal operation. Use completion to allow
8237 * halted scsi ops to resume.
8238 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308239void
8240scsih_pci_resume(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308241{
8242 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8243 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8244
8245 pr_info(MPT3SAS_FMT "PCI error: resume callback!!\n", ioc->name);
8246
8247 pci_cleanup_aer_uncorrect_error_status(pdev);
8248 mpt3sas_base_start_watchdog(ioc);
8249 scsi_unblock_requests(ioc->shost);
8250}
8251
8252/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308253 * scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308254 * @pdev: pointer to PCI device
8255 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308256pci_ers_result_t
8257scsih_pci_mmio_enabled(struct pci_dev *pdev)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308258{
8259 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8260 struct MPT3SAS_ADAPTER *ioc = shost_priv(shost);
8261
8262 pr_info(MPT3SAS_FMT "PCI error: mmio enabled callback!!\n",
8263 ioc->name);
8264
8265 /* TODO - dump whatever for debugging purposes */
8266
8267 /* Request a slot reset. */
8268 return PCI_ERS_RESULT_NEED_RESET;
8269}
8270
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308271/**
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308272 * scsih_init - main entry point for this driver.
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308273 *
8274 * Returns 0 success, anything else error.
8275 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308276int
8277scsih_init(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308278{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308279 mpt_ids = 0;
8280
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308281 mpt3sas_base_initialize_callback_handler();
8282
8283 /* queuecommand callback hander */
8284 scsi_io_cb_idx = mpt3sas_base_register_callback_handler(_scsih_io_done);
8285
8286 /* task managment callback handler */
8287 tm_cb_idx = mpt3sas_base_register_callback_handler(_scsih_tm_done);
8288
8289 /* base internal commands callback handler */
8290 base_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_base_done);
8291 port_enable_cb_idx = mpt3sas_base_register_callback_handler(
8292 mpt3sas_port_enable_done);
8293
8294 /* transport internal commands callback handler */
8295 transport_cb_idx = mpt3sas_base_register_callback_handler(
8296 mpt3sas_transport_done);
8297
8298 /* scsih internal commands callback handler */
8299 scsih_cb_idx = mpt3sas_base_register_callback_handler(_scsih_done);
8300
8301 /* configuration page API internal commands callback handler */
8302 config_cb_idx = mpt3sas_base_register_callback_handler(
8303 mpt3sas_config_done);
8304
8305 /* ctl module callback handler */
8306 ctl_cb_idx = mpt3sas_base_register_callback_handler(mpt3sas_ctl_done);
8307
8308 tm_tr_cb_idx = mpt3sas_base_register_callback_handler(
8309 _scsih_tm_tr_complete);
8310
8311 tm_tr_volume_cb_idx = mpt3sas_base_register_callback_handler(
8312 _scsih_tm_volume_tr_complete);
8313
8314 tm_sas_control_cb_idx = mpt3sas_base_register_callback_handler(
8315 _scsih_sas_control_complete);
8316
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05308317 return 0;
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308318}
8319
8320/**
Sreekanth Reddy7497392a2015-11-11 17:30:19 +05308321 * scsih_exit - exit point for this driver (when it is a module).
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308322 *
8323 * Returns 0 success, anything else error.
8324 */
Sreekanth Reddy8a7e4c22015-11-11 17:30:18 +05308325void
8326scsih_exit(void)
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308327{
Sreekanth Reddyf92363d2012-11-30 07:44:21 +05308328
8329 mpt3sas_base_release_callback_handler(scsi_io_cb_idx);
8330 mpt3sas_base_release_callback_handler(tm_cb_idx);
8331 mpt3sas_base_release_callback_handler(base_cb_idx);
8332 mpt3sas_base_release_callback_handler(port_enable_cb_idx);
8333 mpt3sas_base_release_callback_handler(transport_cb_idx);
8334 mpt3sas_base_release_callback_handler(scsih_cb_idx);
8335 mpt3sas_base_release_callback_handler(config_cb_idx);
8336 mpt3sas_base_release_callback_handler(ctl_cb_idx);
8337
8338 mpt3sas_base_release_callback_handler(tm_tr_cb_idx);
8339 mpt3sas_base_release_callback_handler(tm_tr_volume_cb_idx);
8340 mpt3sas_base_release_callback_handler(tm_sas_control_cb_idx);
8341
8342/* raid transport support */
8343 raid_class_release(mpt3sas_raid_template);
8344 sas_release_transport(mpt3sas_transport_template);
8345}