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